Whamcloud - gitweb
be44842d4a8be59603378a203a80dc949701957f
[fs/lustre-release.git] / lustre / mgs / mgs_handler.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2010, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/mgs/mgs_handler.c
32  *
33  * Author: Nathan Rutman <nathan@clusterfs.com>
34  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
35  * Author: Mikhail Pershin <tappro@whamcloud.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_MGS
39 #define D_MGS D_CONFIG
40
41 #include <obd_class.h>
42 #include <lprocfs_status.h>
43 #include <uapi/linux/lustre/lustre_ioctl.h>
44 #include <uapi/linux/lustre/lustre_param.h>
45 #include <uapi/linux/lustre/lustre_barrier_user.h>
46
47 #include "mgs_internal.h"
48
49 /*
50  * Regular MGS handlers
51  */
52 static int mgs_connect(struct tgt_session_info *tsi)
53 {
54         struct ptlrpc_request   *req = tgt_ses_req(tsi);
55         int                      rc;
56
57         ENTRY;
58
59         CFS_FAIL_TIMEOUT(OBD_FAIL_MGS_CONNECT_NET, cfs_fail_val);
60         rc = tgt_connect(tsi);
61         if (rc)
62                 RETURN(rc);
63
64         if (lustre_msg_get_conn_cnt(req->rq_reqmsg) > 1)
65                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
66
67         RETURN(0);
68 }
69
70 static int mgs_disconnect(struct tgt_session_info *tsi)
71 {
72         int rc;
73
74         ENTRY;
75
76         LASSERT(tsi->tsi_exp);
77
78         rc = tgt_disconnect(tsi);
79         if (rc)
80                 RETURN(err_serious(rc));
81         RETURN(0);
82 }
83
84 static int mgs_exception(struct tgt_session_info *tsi)
85 {
86         ENTRY;
87
88         tgt_counter_incr(tsi->tsi_exp, LPROC_MGS_EXCEPTION);
89
90         RETURN(0);
91 }
92
93 static inline bool str_starts_with(const char *str, const char *prefix)
94 {
95         return strncmp(str, prefix, strlen(prefix)) == 0;
96 }
97
98 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 18, 53, 0)
99 static int mgs_set_info(struct tgt_session_info *tsi)
100 {
101         struct mgs_thread_info *mgi;
102         struct mgs_send_param *msp;
103         size_t param_len;
104         char *s;
105
106         ENTRY;
107
108         mgi = mgs_env_info(tsi->tsi_env);
109         if (IS_ERR(mgi))
110                 RETURN(err_serious(PTR_ERR(mgi)));
111
112         msp = req_capsule_client_get(tsi->tsi_pill, &RMF_MGS_SEND_PARAM);
113         if (msp == NULL)
114                 RETURN(err_serious(-EFAULT));
115
116         param_len = strnlen(msp->mgs_param, sizeof(msp->mgs_param));
117         if (param_len == 0 || param_len == sizeof(msp->mgs_param))
118                 RETURN(-EINVAL);
119
120         /* We only allow '*.lov.stripe{size,count,offset}=*' from an RPC. */
121         s = strchr(msp->mgs_param, '.');
122         if (s == NULL)
123                 RETURN(-EINVAL);
124
125         if (!str_starts_with(s + 1, "lov.stripesize=") &&
126             !str_starts_with(s + 1, "lov.stripecount=") &&
127             !str_starts_with(s + 1, "lov.stripeoffset="))
128                 RETURN(-EINVAL);
129
130         /* do nothing */
131         CDEBUG(D_MGS, "%s: ignoring set info '%s'\n",
132                tgt_name(tsi->tsi_tgt), msp->mgs_param);
133         RETURN(0);
134 }
135 #endif
136
137 enum ast_type {
138         AST_CONFIG      = 1,
139         AST_PARAMS      = 2,
140         AST_IR          = 3,
141         AST_BARRIER     = 4,
142 };
143
144 static int mgs_completion_ast_generic(struct ldlm_lock *lock, __u64 flags,
145                                      void *cbdata, enum ast_type type)
146 {
147         ENTRY;
148
149         if (!(flags & LDLM_FL_BLOCKED_MASK)) {
150                 struct fs_db *fsdb;
151
152                 /* l_ast_data is used as a marker to avoid cancel ldlm lock
153                  * twice. See LU-2317. */
154                 lock_res_and_lock(lock);
155                 fsdb = (struct fs_db *)lock->l_ast_data;
156                 lock->l_ast_data = NULL;
157                 unlock_res_and_lock(lock);
158
159                 if (fsdb != NULL) {
160                         struct lustre_handle lockh;
161
162                         switch(type) {
163                                 case AST_CONFIG:
164                                         /* clear the bit before lock put */
165                                         clear_bit(FSDB_REVOKING_LOCK,
166                                                   &fsdb->fsdb_flags);
167                                         break;
168                                 case AST_PARAMS:
169                                         clear_bit(FSDB_REVOKING_PARAMS,
170                                                   &fsdb->fsdb_flags);
171                                         break;
172                                 case AST_IR:
173                                         mgs_ir_notify_complete(fsdb);
174                                         break;
175                                 case AST_BARRIER:
176                                         break;
177                                 default:
178                                         LBUG();
179                         }
180
181                         ldlm_lock2handle(lock, &lockh);
182                         ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
183                 }
184         }
185
186         RETURN(ldlm_completion_ast(lock, flags, cbdata));
187 }
188
189 static int mgs_completion_ast_config(struct ldlm_lock *lock, __u64 flags,
190                                      void *cbdata)
191 {
192         return mgs_completion_ast_generic(lock, flags, cbdata, AST_CONFIG);
193 }
194
195 static int mgs_completion_ast_params(struct ldlm_lock *lock, __u64 flags,
196                                      void *cbdata)
197 {
198         return mgs_completion_ast_generic(lock, flags, cbdata, AST_PARAMS);
199 }
200
201 static int mgs_completion_ast_ir(struct ldlm_lock *lock, __u64 flags,
202                                  void *cbdata)
203 {
204         return mgs_completion_ast_generic(lock, flags, cbdata, AST_IR);
205 }
206
207 static int mgs_completion_ast_barrier(struct ldlm_lock *lock, __u64 flags,
208                                       void *cbdata)
209 {
210         return mgs_completion_ast_generic(lock, flags, cbdata, AST_BARRIER);
211 }
212
213 void mgs_revoke_lock(struct mgs_device *mgs, struct fs_db *fsdb, int type)
214 {
215         ldlm_completion_callback cp = NULL;
216         struct lustre_handle     lockh = {
217                 .cookie = 0,
218         };
219         struct ldlm_res_id       res_id;
220         __u64 flags = LDLM_FL_ATOMIC_CB;
221         int rc;
222         ENTRY;
223
224         LASSERT(fsdb->fsdb_name[0] != '\0');
225         rc = mgc_fsname2resid(fsdb->fsdb_name, &res_id, type);
226         LASSERT(rc == 0);
227         switch (type) {
228         case CONFIG_T_CONFIG:
229         case CONFIG_T_NODEMAP:
230                 cp = mgs_completion_ast_config;
231                 if (test_and_set_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags))
232                         rc = -EALREADY;
233                 break;
234         case CONFIG_T_PARAMS:
235                 cp = mgs_completion_ast_params;
236                 if (test_and_set_bit(FSDB_REVOKING_PARAMS, &fsdb->fsdb_flags))
237                         rc = -EALREADY;
238                 break;
239         case CONFIG_T_RECOVER:
240                 cp = mgs_completion_ast_ir;
241                 break;
242         case CONFIG_T_BARRIER:
243                 cp = mgs_completion_ast_barrier;
244                 break;
245         default:
246                 break;
247         }
248
249         if (!rc) {
250                 LASSERT(cp != NULL);
251                 rc = ldlm_cli_enqueue_local(NULL, mgs->mgs_obd->obd_namespace,
252                                             &res_id, LDLM_PLAIN, NULL, LCK_EX,
253                                             &flags, ldlm_blocking_ast, cp,
254                                             NULL, fsdb, 0, LVB_T_NONE, NULL,
255                                             &lockh);
256                 if (rc != ELDLM_OK) {
257                         CERROR("%s: can't take cfg lock for %#llx/%#llx : rc = %d\n",
258                                mgs->mgs_obd->obd_name,
259                                le64_to_cpu(res_id.name[0]),
260                                le64_to_cpu(res_id.name[1]), rc);
261
262                         if (type == CONFIG_T_CONFIG)
263                                 clear_bit(FSDB_REVOKING_LOCK,
264                                           &fsdb->fsdb_flags);
265
266                         if (type == CONFIG_T_PARAMS)
267                                 clear_bit(FSDB_REVOKING_PARAMS,
268                                           &fsdb->fsdb_flags);
269                 }
270                 /* lock has been cancelled in completion_ast. */
271         }
272
273         RETURN_EXIT;
274 }
275
276 /* rc=0 means ok
277       1 means update
278      <0 means error */
279 static int mgs_check_target(const struct lu_env *env,
280                             struct mgs_device *mgs,
281                             struct mgs_target_info *mti)
282 {
283         int rc;
284         ENTRY;
285
286         rc = mgs_check_index(env, mgs, mti);
287         if (rc == 0) {
288                 LCONSOLE_ERROR_MSG(0x13b, "%s claims to have registered, but "
289                                    "this MGS does not know about it, preventing "
290                                    "registration.\n", mti->mti_svname);
291                 rc = -ENOENT;
292         } else if (rc == -1) {
293                 LCONSOLE_ERROR_MSG(0x13c, "Client log %s-client has "
294                                    "disappeared! Regenerating all logs.\n",
295                                    mti->mti_fsname);
296                 mti->mti_flags |= LDD_F_WRITECONF;
297                 rc = 1;
298         } else {
299                 /* Index is correctly marked as used */
300                 rc = 0;
301         }
302
303         RETURN(rc);
304 }
305
306 /* Ensure this is not a failover node that is connecting first*/
307 static int mgs_check_failover_reg(struct mgs_target_info *mti)
308 {
309         lnet_nid_t nid;
310         char *ptr;
311         int i;
312
313         ptr = mti->mti_params;
314         while (class_find_param(ptr, PARAM_FAILNODE, &ptr) == 0) {
315                 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
316                         for (i = 0; i < mti->mti_nid_count; i++) {
317                                 if (nid == mti->mti_nids[i]) {
318                                         LCONSOLE_WARN("Denying initial registration attempt from nid %s, specified as failover\n",
319                                                       libcfs_nid2str(nid));
320                                         return -EADDRNOTAVAIL;
321                                 }
322                         }
323                 }
324         }
325         return 0;
326 }
327
328 /* Called whenever a target starts up.  Flags indicate first connect, etc. */
329 static int mgs_target_reg(struct tgt_session_info *tsi)
330 {
331         struct obd_device *obd = tsi->tsi_exp->exp_obd;
332         struct mgs_device *mgs = exp2mgs_dev(tsi->tsi_exp);
333         struct mgs_target_info *mti, *rep_mti;
334         struct fs_db *b_fsdb = NULL; /* barrier fsdb */
335         struct fs_db *c_fsdb = NULL; /* config fsdb */
336         char barrier_name[20];
337         int opc;
338         int rc = 0;
339
340         ENTRY;
341
342         rc = lu_env_refill((struct lu_env *)tsi->tsi_env);
343         if (rc)
344                 return err_serious(rc);
345
346         tgt_counter_incr(tsi->tsi_exp, LPROC_MGS_TARGET_REG);
347
348         mti = req_capsule_client_get(tsi->tsi_pill, &RMF_MGS_TARGET_INFO);
349         if (mti == NULL) {
350                 DEBUG_REQ(D_HA, tgt_ses_req(tsi), "no mgs_send_param");
351                 RETURN(err_serious(-EFAULT));
352         }
353
354         down_read(&mgs->mgs_barrier_rwsem);
355
356         if (OCD_HAS_FLAG(&tgt_ses_req(tsi)->rq_export->exp_connect_data,
357                          IMP_RECOV))
358                 opc = mti->mti_flags & LDD_F_OPC_MASK;
359         else
360                 opc = LDD_F_OPC_REG;
361
362         if (opc == LDD_F_OPC_READY) {
363                 CDEBUG(D_MGS, "fs: %s index: %d is ready to reconnect.\n",
364                         mti->mti_fsname, mti->mti_stripe_index);
365                 rc = mgs_ir_update(tsi->tsi_env, mgs, mti);
366                 if (rc) {
367                         LASSERT(!(mti->mti_flags & LDD_F_IR_CAPABLE));
368                         CERROR("%s: Update IR return failure: rc = %d\n",
369                                mti->mti_fsname, rc);
370                 }
371
372                 GOTO(out_norevoke, rc);
373         }
374
375         /* Do not support unregistering right now. */
376         if (opc != LDD_F_OPC_REG)
377                 GOTO(out_norevoke, rc = -EINVAL);
378
379         snprintf(barrier_name, sizeof(barrier_name) - 1, "%s-%s",
380                  mti->mti_fsname, BARRIER_FILENAME);
381         rc = mgs_find_or_make_fsdb(tsi->tsi_env, mgs, barrier_name, &b_fsdb);
382         if (rc) {
383                 CERROR("%s: Can't get db for %s: rc = %d\n",
384                        mti->mti_fsname, barrier_name, rc);
385
386                 GOTO(out_norevoke, rc);
387         }
388
389         CDEBUG(D_MGS, "fs: %s index: %d is registered to MGS.\n",
390                mti->mti_fsname, mti->mti_stripe_index);
391
392         if (mti->mti_flags & LDD_F_SV_TYPE_MDT) {
393                 if (b_fsdb->fsdb_barrier_status == BS_FREEZING_P1 ||
394                     b_fsdb->fsdb_barrier_status == BS_FREEZING_P2 ||
395                     b_fsdb->fsdb_barrier_status == BS_FROZEN) {
396                         LCONSOLE_WARN("%s: the system is in barrier, refuse "
397                                       "the connection from MDT %s temporary\n",
398                                       obd->obd_name, mti->mti_svname);
399
400                         GOTO(out_norevoke, rc = -EBUSY);
401                 }
402
403                 if (!(exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_BARRIER) &&
404                     !b_fsdb->fsdb_barrier_disabled) {
405                         LCONSOLE_WARN("%s: the MDT %s does not support write "
406                                       "barrier, so disable barrier on the "
407                                       "whole system.\n",
408                                       obd->obd_name, mti->mti_svname);
409
410                         b_fsdb->fsdb_barrier_disabled = 1;
411                 }
412         }
413
414         if (mti->mti_flags & LDD_F_NEED_INDEX)
415                 mti->mti_flags |= LDD_F_WRITECONF;
416
417         if (!(mti->mti_flags & (LDD_F_WRITECONF | LDD_F_UPDATE))) {
418                 /* We're just here as a startup ping. */
419                 CDEBUG(D_MGS, "Server %s is running on %s\n",
420                        mti->mti_svname, obd_export_nid2str(tsi->tsi_exp));
421                 rc = mgs_check_target(tsi->tsi_env, mgs, mti);
422                 /* above will set appropriate mti flags */
423                 if (rc <= 0)
424                         /* Nothing wrong, or fatal error */
425                         GOTO(out_norevoke, rc);
426         } else if (!(mti->mti_flags & LDD_F_NO_PRIMNODE)) {
427                 rc = mgs_check_failover_reg(mti);
428                 if (rc)
429                         GOTO(out_norevoke, rc);
430         }
431
432         OBD_FAIL_TIMEOUT(OBD_FAIL_MGS_PAUSE_TARGET_REG, 10);
433
434         if (mti->mti_flags & LDD_F_WRITECONF) {
435                 if (mti->mti_flags & LDD_F_SV_TYPE_MDT &&
436                     mti->mti_stripe_index == 0) {
437                         mgs_put_fsdb(mgs, b_fsdb);
438                         b_fsdb = NULL;
439                         rc = mgs_erase_logs(tsi->tsi_env, mgs,
440                                             mti->mti_fsname);
441                         LCONSOLE_WARN("%s: Logs for fs %s were removed by user "
442                                       "request.  All servers must be restarted "
443                                       "in order to regenerate the logs: rc = %d"
444                                       "\n", obd->obd_name, mti->mti_fsname, rc);
445                         if (rc && rc != -ENOENT)
446                                 GOTO(out_norevoke, rc);
447
448                         rc = mgs_find_or_make_fsdb(tsi->tsi_env, mgs,
449                                                    barrier_name, &b_fsdb);
450                         if (rc) {
451                                 CERROR("Can't get db for %s: %d\n",
452                                        barrier_name, rc);
453
454                                 GOTO(out_norevoke, rc);
455                         }
456
457                         if (!(exp_connect_flags(tsi->tsi_exp) &
458                               OBD_CONNECT_BARRIER)) {
459                                 LCONSOLE_WARN("%s: the MDT %s does not support "
460                                               "write barrier, disable barrier "
461                                               "on the whole system.\n",
462                                               obd->obd_name, mti->mti_svname);
463
464                                 b_fsdb->fsdb_barrier_disabled = 1;
465                         }
466                 } else if (mti->mti_flags &
467                            (LDD_F_SV_TYPE_OST | LDD_F_SV_TYPE_MDT)) {
468                         rc = mgs_erase_log(tsi->tsi_env, mgs, mti->mti_svname);
469                         LCONSOLE_WARN("%s: Regenerating %s log by user "
470                                       "request: rc = %d\n",
471                                       obd->obd_name, mti->mti_svname, rc);
472                         if (rc)
473                                 GOTO(out_norevoke, rc);
474                 }
475
476                 mti->mti_flags |= LDD_F_UPDATE;
477         }
478
479         rc = mgs_find_or_make_fsdb(tsi->tsi_env, mgs, mti->mti_fsname, &c_fsdb);
480         if (rc) {
481                 CERROR("Can't get db for %s: %d\n", mti->mti_fsname, rc);
482
483                 GOTO(out_norevoke, rc);
484         }
485
486         /*
487          * Log writing contention is handled by the fsdb_mutex.
488          *
489          * It should be alright if someone was reading while we were
490          * updating the logs - if we revoke at the end they will just update
491          * from where they left off.
492          */
493         if (mti->mti_flags & LDD_F_UPDATE) {
494                 CDEBUG(D_MGS, "updating %s, index=%d\n", mti->mti_svname,
495                        mti->mti_stripe_index);
496
497                 /* create or update the target log
498                    and update the client/mdt logs */
499                 rc = mgs_write_log_target(tsi->tsi_env, mgs, mti, c_fsdb);
500                 if (rc) {
501                         CERROR("Failed to write %s log (%d)\n",
502                                mti->mti_svname, rc);
503                         GOTO(out, rc);
504                 }
505
506                 mti->mti_flags &= ~(LDD_F_VIRGIN | LDD_F_UPDATE |
507                                     LDD_F_NEED_INDEX | LDD_F_WRITECONF);
508                 mti->mti_flags |= LDD_F_REWRITE_LDD;
509         }
510
511 out:
512         mgs_revoke_lock(mgs, c_fsdb, CONFIG_T_CONFIG);
513
514 out_norevoke:
515         if (!rc && mti->mti_flags & LDD_F_SV_TYPE_MDT && b_fsdb) {
516                 if (!c_fsdb) {
517                         rc = mgs_find_or_make_fsdb(tsi->tsi_env, mgs,
518                                                    mti->mti_fsname, &c_fsdb);
519                         if (rc)
520                                 CERROR("Fail to get db for %s: %d\n",
521                                        mti->mti_fsname, rc);
522                 }
523
524                 if (c_fsdb) {
525                         memcpy(b_fsdb->fsdb_mdt_index_map,
526                                c_fsdb->fsdb_mdt_index_map, INDEX_MAP_SIZE);
527                         b_fsdb->fsdb_mdt_count = c_fsdb->fsdb_mdt_count;
528                 }
529         }
530
531         up_read(&mgs->mgs_barrier_rwsem);
532
533         CDEBUG(D_MGS, "replying with %s, index=%d, rc=%d\n", mti->mti_svname,
534                mti->mti_stripe_index, rc);
535          /* An error flag is set in the mti reply rather than an error code */
536         if (rc)
537                 mti->mti_flags |= LDD_F_ERROR;
538
539         /* send back the whole mti in the reply */
540         rep_mti = req_capsule_server_get(tsi->tsi_pill, &RMF_MGS_TARGET_INFO);
541         *rep_mti = *mti;
542
543         /* Flush logs to disk */
544         dt_sync(tsi->tsi_env, mgs->mgs_bottom);
545         if (b_fsdb)
546                 mgs_put_fsdb(mgs, b_fsdb);
547         if (c_fsdb)
548                 mgs_put_fsdb(mgs, c_fsdb);
549         RETURN(rc);
550 }
551
552 /* Called whenever a target cleans up. */
553 static int mgs_target_del(struct tgt_session_info *tsi)
554 {
555         ENTRY;
556
557         tgt_counter_incr(tsi->tsi_exp, LPROC_MGS_TARGET_DEL);
558
559         RETURN(0);
560 }
561
562 static int mgs_config_read(struct tgt_session_info *tsi)
563 {
564         struct ptlrpc_request   *req = tgt_ses_req(tsi);
565         struct mgs_config_body  *body;
566         int                      rc;
567
568         ENTRY;
569
570         body = req_capsule_client_get(tsi->tsi_pill, &RMF_MGS_CONFIG_BODY);
571         if (body == NULL) {
572                 DEBUG_REQ(D_HA, req, "no mgs_config_body");
573                 RETURN(err_serious(-EFAULT));
574         }
575
576         switch (body->mcb_type) {
577         case CONFIG_T_RECOVER:
578                 rc = mgs_get_ir_logs(req);
579                 break;
580         case CONFIG_T_NODEMAP:
581                 rc = nodemap_get_config_req(req->rq_export->exp_obd, req);
582                 break;
583         case CONFIG_T_CONFIG:
584                 rc = -EOPNOTSUPP;
585                 break;
586         default:
587                 rc = -EINVAL;
588                 break;
589         }
590
591         RETURN(rc);
592 }
593
594 static int mgs_llog_open(struct tgt_session_info *tsi)
595 {
596         struct mgs_thread_info  *mgi;
597         struct ptlrpc_request   *req = tgt_ses_req(tsi);
598         char                    *logname;
599         int                      rc;
600
601         ENTRY;
602
603         rc = tgt_llog_open(tsi);
604         if (rc)
605                 RETURN(rc);
606
607         /*
608          * For old clients there is no direct way of knowing which file system
609          * a client is operating at the MGS side. But we need to pick up those
610          * clients so that the MGS can mark the corresponding file system as
611          * non-IR capable because old clients are not ready to be notified.
612          *
613          * Therefore we attempt to detect the file systems name by hacking the
614          * llog operation which is currently used by the clients to fetch
615          * configuration logs. At present this is fine because this is the
616          * ONLY llog operation between mgc and the MGS.
617          *
618          * If extra llog operation are going to be added, this function needs
619          * further work.
620          *
621          * When releases prior than 2.0 are not supported, the following code
622          * can be removed.
623          */
624         mgi = mgs_env_info(tsi->tsi_env);
625         if (IS_ERR(mgi))
626                 RETURN(PTR_ERR(mgi));
627
628         logname = req_capsule_client_get(tsi->tsi_pill, &RMF_NAME);
629         if (logname) {
630                 char *ptr = strrchr(logname, '-');
631                 int   len = (ptr != NULL) ? (int)(ptr - logname) : 0;
632
633                 if (ptr == NULL || len >= sizeof(mgi->mgi_fsname)) {
634                         if (strcmp(logname, PARAMS_FILENAME) != 0)
635                                 LCONSOLE_WARN("%s: non-config logname "
636                                               "received: %s\n",
637                                               tgt_name(tsi->tsi_tgt),
638                                               logname);
639                         /* not error, this can be llog test name */
640                 } else {
641                         strncpy(mgi->mgi_fsname, logname, len);
642                         mgi->mgi_fsname[len] = 0;
643
644                         rc = mgs_fsc_attach(tsi->tsi_env, tsi->tsi_exp,
645                                             mgi->mgi_fsname);
646                         if (rc && rc != -EEXIST) {
647                                 LCONSOLE_WARN("%s: Unable to add client %s "
648                                               "to file system %s: %d\n",
649                                               tgt_name(tsi->tsi_tgt),
650                                               libcfs_nid2str(req->rq_peer.nid),
651                                               mgi->mgi_fsname, rc);
652                         } else {
653                                 rc = 0;
654                         }
655                 }
656         } else {
657                 CERROR("%s: no logname in request\n", tgt_name(tsi->tsi_tgt));
658                 RETURN(-EINVAL);
659         }
660         RETURN(rc);
661 }
662
663 static inline int mgs_init_export(struct obd_export *exp)
664 {
665         struct mgs_export_data *data = &exp->u.eu_mgs_data;
666
667         /* init mgs_export_data for fsc */
668         spin_lock_init(&data->med_lock);
669         INIT_LIST_HEAD(&data->med_clients);
670
671         spin_lock(&exp->exp_lock);
672         exp->exp_connecting = 1;
673         spin_unlock(&exp->exp_lock);
674
675         /* self-export doesn't need client data and ldlm initialization */
676         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
677                                      &exp->exp_client_uuid)))
678                 return 0;
679         return ldlm_init_export(exp);
680 }
681
682 static inline int mgs_destroy_export(struct obd_export *exp)
683 {
684         ENTRY;
685
686         target_destroy_export(exp);
687         mgs_client_free(exp);
688
689         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
690                                      &exp->exp_client_uuid)))
691                 RETURN(0);
692
693         ldlm_destroy_export(exp);
694
695         RETURN(0);
696 }
697
698 static int mgs_extract_fs_pool(char *arg, char *fsname, char *poolname)
699 {
700         size_t len;
701         char *ptr;
702
703         ENTRY;
704         /* Validate name */
705         for (ptr = arg; *ptr != '\0'; ptr++) {
706                 if (!isalnum(*ptr) && *ptr != '_' && *ptr != '-' && *ptr != '.')
707                         return -EINVAL;
708         }
709
710         /* Test for fsname.poolname format.
711          * strlen() test if poolname is empty
712          */
713         ptr = strchr(arg, '.');
714         if (!ptr || !strlen(ptr))
715                 return -EINVAL;
716         ptr++;
717
718         /* Also make sure poolname is not to long. */
719         if (strlen(ptr) > LOV_MAXPOOLNAME)
720                 return -ENAMETOOLONG;
721         strlcpy(poolname, ptr, LOV_MAXPOOLNAME + 1);
722
723         /* Test if fsname is empty */
724         len = strlen(arg) - strlen(ptr) - 1;
725         if (!len)
726                 return -EINVAL;
727
728         /* or too long */
729         if (len > LUSTRE_MAXFSNAME)
730                 return -ENAMETOOLONG;
731
732         strncpy(fsname, arg, len);
733
734         RETURN(0);
735 }
736
737 static int mgs_iocontrol_nodemap(const struct lu_env *env,
738                                  struct mgs_device *mgs,
739                                  struct obd_ioctl_data *data)
740 {
741         struct lustre_cfg       *lcfg = NULL;
742         struct fs_db            *fsdb;
743         lnet_nid_t              nid;
744         const char              *nodemap_name = NULL;
745         const char              *nidstr = NULL;
746         const char              *client_idstr = NULL;
747         const char              *idtype_str = NULL;
748         char                    *param = NULL;
749         char                    fs_idstr[16];
750         char                    name_buf[LUSTRE_NODEMAP_NAME_LENGTH + 1];
751         int                     rc = 0;
752         unsigned long           client_id;
753         __u32                   fs_id;
754         __u32                   cmd;
755         int                     idtype;
756
757         ENTRY;
758
759         if (data->ioc_type != LUSTRE_CFG_TYPE) {
760                 CERROR("%s: unknown cfg record type: %d\n",
761                        mgs->mgs_obd->obd_name, data->ioc_type);
762                 GOTO(out, rc = -EINVAL);
763         }
764
765         if (data->ioc_plen1 > PAGE_SIZE)
766                 GOTO(out, rc = -E2BIG);
767
768         OBD_ALLOC(lcfg, data->ioc_plen1);
769         if (lcfg == NULL)
770                 GOTO(out, rc = -ENOMEM);
771
772         if (copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
773                 GOTO(out_lcfg, rc = -EFAULT);
774
775         cmd = lcfg->lcfg_command;
776
777         switch (cmd) {
778         case LCFG_NODEMAP_ACTIVATE:
779                 if (lcfg->lcfg_bufcount != 2)
780                         GOTO(out_lcfg, rc = -EINVAL);
781                 param = lustre_cfg_string(lcfg, 1);
782                 if (strcmp(param, "1") == 0)
783                                 nodemap_activate(1);
784                 else
785                                 nodemap_activate(0);
786                 break;
787         case LCFG_NODEMAP_ADD:
788         case LCFG_NODEMAP_DEL:
789                 if (lcfg->lcfg_bufcount != 2)
790                         GOTO(out_lcfg, rc = -EINVAL);
791                 nodemap_name = lustre_cfg_string(lcfg, 1);
792                 rc = mgs_nodemap_cmd(env, mgs, cmd, nodemap_name, param);
793                 break;
794         case LCFG_NODEMAP_TEST_NID:
795                 if (lcfg->lcfg_bufcount != 2)
796                         GOTO(out_lcfg, rc = -EINVAL);
797                 nidstr = lustre_cfg_string(lcfg, 1);
798                 nid = libcfs_str2nid(nidstr);
799                 nodemap_test_nid(nid, name_buf, sizeof(name_buf));
800                 rc = copy_to_user(data->ioc_pbuf1, name_buf,
801                                   min_t(size_t, data->ioc_plen1,
802                                         sizeof(name_buf)));
803                 if (rc != 0)
804                         GOTO(out_lcfg, rc = -EFAULT);
805                 break;
806         case LCFG_NODEMAP_TEST_ID:
807                 if (lcfg->lcfg_bufcount != 4)
808                         GOTO(out_lcfg, rc = -EINVAL);
809                 nidstr = lustre_cfg_string(lcfg, 1);
810                 idtype_str = lustre_cfg_string(lcfg, 2);
811                 client_idstr = lustre_cfg_string(lcfg, 3);
812
813                 nid = libcfs_str2nid(nidstr);
814                 if (strcmp(idtype_str, "uid") == 0)
815                         idtype = NODEMAP_UID;
816                 else
817                         idtype = NODEMAP_GID;
818
819                 rc = kstrtoul(client_idstr, 10, &client_id);
820                 if (rc != 0)
821                         GOTO(out_lcfg, rc = -EINVAL);
822
823                 rc = nodemap_test_id(nid, idtype, client_id, &fs_id);
824                 if (rc < 0)
825                         GOTO(out_lcfg, rc = -EINVAL);
826
827                 if (data->ioc_plen1 < sizeof(fs_idstr))
828                         GOTO(out_lcfg, rc = -EINVAL);
829
830                 snprintf(fs_idstr, sizeof(fs_idstr), "%u", fs_id);
831                 if (copy_to_user(data->ioc_pbuf1, fs_idstr,
832                                  sizeof(fs_idstr)) != 0)
833                         GOTO(out_lcfg, rc = -EINVAL);
834                 break;
835         case LCFG_NODEMAP_ADD_RANGE:
836         case LCFG_NODEMAP_DEL_RANGE:
837         case LCFG_NODEMAP_ADD_UIDMAP:
838         case LCFG_NODEMAP_DEL_UIDMAP:
839         case LCFG_NODEMAP_ADD_GIDMAP:
840         case LCFG_NODEMAP_DEL_GIDMAP:
841         case LCFG_NODEMAP_SET_FILESET:
842         case LCFG_NODEMAP_SET_SEPOL:
843                 if (lcfg->lcfg_bufcount != 3)
844                         GOTO(out_lcfg, rc = -EINVAL);
845                 nodemap_name = lustre_cfg_string(lcfg, 1);
846                 param = lustre_cfg_string(lcfg, 2);
847                 rc = mgs_nodemap_cmd(env, mgs, cmd, nodemap_name, param);
848                 break;
849         case LCFG_NODEMAP_ADMIN:
850         case LCFG_NODEMAP_TRUSTED:
851         case LCFG_NODEMAP_DENY_UNKNOWN:
852         case LCFG_NODEMAP_SQUASH_UID:
853         case LCFG_NODEMAP_SQUASH_GID:
854         case LCFG_NODEMAP_MAP_MODE:
855         case LCFG_NODEMAP_AUDIT_MODE:
856         case LCFG_NODEMAP_FORBID_ENCRYPT:
857                 if (lcfg->lcfg_bufcount != 4)
858                         GOTO(out_lcfg, rc = -EINVAL);
859                 nodemap_name = lustre_cfg_string(lcfg, 1);
860                 param = lustre_cfg_string(lcfg, 3);
861                 rc = mgs_nodemap_cmd(env, mgs, cmd, nodemap_name, param);
862                 break;
863         default:
864                 rc = -ENOTTY;
865         }
866
867         if (rc != 0) {
868                 CERROR("%s: OBD_IOC_NODEMAP command %X for %s: rc = %d\n",
869                        mgs->mgs_obd->obd_name, lcfg->lcfg_command,
870                        nodemap_name, rc);
871                 GOTO(out_lcfg, rc);
872         }
873
874         /* revoke nodemap lock */
875         rc = mgs_find_or_make_fsdb(env, mgs, LUSTRE_NODEMAP_NAME, &fsdb);
876         if (rc < 0) {
877                 CWARN("%s: cannot make nodemap fsdb: rc = %d\n",
878                       mgs->mgs_obd->obd_name, rc);
879         } else {
880                 mgs_revoke_lock(mgs, fsdb, CONFIG_T_NODEMAP);
881                 mgs_put_fsdb(mgs, fsdb);
882         }
883
884 out_lcfg:
885         OBD_FREE(lcfg, data->ioc_plen1);
886 out:
887         RETURN(rc);
888 }
889
890 static int mgs_iocontrol_pool(const struct lu_env *env,
891                               struct mgs_device *mgs,
892                               struct obd_ioctl_data *data)
893 {
894         struct mgs_thread_info *mgi = mgs_env_info(env);
895         int rc;
896         struct lustre_cfg *lcfg = NULL;
897         char *poolname = NULL;
898         ENTRY;
899
900         OBD_ALLOC(poolname, LOV_MAXPOOLNAME + 1);
901         if (poolname == NULL)
902                 RETURN(-ENOMEM);
903
904         if (data->ioc_type != LUSTRE_CFG_TYPE) {
905                 CERROR("%s: unknown cfg record type: %d\n",
906                        mgs->mgs_obd->obd_name, data->ioc_type);
907                 GOTO(out_pool, rc = -EINVAL);
908         }
909
910         if (data->ioc_plen1 > PAGE_SIZE)
911                 GOTO(out_pool, rc = -E2BIG);
912
913         OBD_ALLOC(lcfg, data->ioc_plen1);
914         if (lcfg == NULL)
915                 GOTO(out_pool, rc = -ENOMEM);
916
917         if (copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
918                 GOTO(out_lcfg, rc = -EFAULT);
919
920         if (lcfg->lcfg_bufcount < 2)
921                 GOTO(out_lcfg, rc = -EFAULT);
922
923         /* first arg is always <fsname>.<poolname> */
924         rc = mgs_extract_fs_pool(lustre_cfg_string(lcfg, 1), mgi->mgi_fsname,
925                                  poolname);
926         if (rc)
927                 GOTO(out_lcfg, rc);
928
929         switch (lcfg->lcfg_command) {
930         case LCFG_POOL_NEW:
931                 if (lcfg->lcfg_bufcount != 2)
932                         GOTO(out_lcfg, rc = -EINVAL);
933                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_NEW, mgi->mgi_fsname,
934                                   poolname, NULL);
935                 break;
936         case LCFG_POOL_ADD:
937                 if (lcfg->lcfg_bufcount != 3)
938                         GOTO(out_lcfg, rc = -EINVAL);
939                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_ADD, mgi->mgi_fsname,
940                                   poolname, lustre_cfg_string(lcfg, 2));
941                 break;
942         case LCFG_POOL_REM:
943                 if (lcfg->lcfg_bufcount != 3)
944                         GOTO(out_lcfg, rc = -EINVAL);
945                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_REM, mgi->mgi_fsname,
946                                   poolname, lustre_cfg_string(lcfg, 2));
947                 break;
948         case LCFG_POOL_DEL:
949                 if (lcfg->lcfg_bufcount != 2)
950                         GOTO(out_lcfg, rc = -EINVAL);
951                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_DEL, mgi->mgi_fsname,
952                                   poolname, NULL);
953                 break;
954         default:
955                  rc = -EINVAL;
956         }
957
958         if (rc) {
959                 CERROR("OBD_IOC_POOL err %d, cmd %X for pool %s.%s\n",
960                        rc, lcfg->lcfg_command, mgi->mgi_fsname, poolname);
961                 GOTO(out_lcfg, rc);
962         }
963
964 out_lcfg:
965         OBD_FREE(lcfg, data->ioc_plen1);
966 out_pool:
967         OBD_FREE(poolname, LOV_MAXPOOLNAME + 1);
968         RETURN(rc);
969 }
970
971 /* from mdt_iocontrol */
972 static int mgs_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
973                          void *karg, void __user *uarg)
974 {
975         struct mgs_device *mgs = exp2mgs_dev(exp);
976         struct obd_ioctl_data *data = karg;
977         struct lu_env env;
978         int rc = 0;
979
980         ENTRY;
981         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
982
983         rc = lu_env_init(&env, LCT_MG_THREAD);
984         if (rc)
985                 RETURN(rc);
986
987         switch (cmd) {
988
989         case OBD_IOC_PARAM: {
990                 struct lustre_cfg *lcfg;
991
992                 if (data->ioc_type != LUSTRE_CFG_TYPE) {
993                         CERROR("%s: unknown cfg record type: %d\n",
994                                mgs->mgs_obd->obd_name, data->ioc_type);
995                         GOTO(out, rc = -EINVAL);
996                 }
997
998                 OBD_ALLOC(lcfg, data->ioc_plen1);
999                 if (lcfg == NULL)
1000                         GOTO(out, rc = -ENOMEM);
1001                 if (copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
1002                         GOTO(out_free, rc = -EFAULT);
1003
1004                 if (lcfg->lcfg_bufcount < 1)
1005                         GOTO(out_free, rc = -EINVAL);
1006
1007                 rc = mgs_set_param(&env, mgs, lcfg);
1008                 if (rc)
1009                         CERROR("%s: setparam err: rc = %d\n",
1010                                exp->exp_obd->obd_name, rc);
1011 out_free:
1012                 OBD_FREE(lcfg, data->ioc_plen1);
1013                 break;
1014         }
1015
1016         case OBD_IOC_REPLACE_NIDS: {
1017                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
1018                         rc = -EINVAL;
1019                         CERROR("%s: no device or fsname specified: rc = %d\n",
1020                                exp->exp_obd->obd_name, rc);
1021                         break;
1022                 }
1023
1024                 if (data->ioc_inllen1 > MTI_NAME_MAXLEN) {
1025                         rc = -EOVERFLOW;
1026                         CERROR("%s: device or fsname is too long: rc = %d\n",
1027                                exp->exp_obd->obd_name, rc);
1028                         break;
1029                 }
1030
1031                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
1032                         rc = -EINVAL;
1033                         CERROR("%s: device or fsname is not NUL terminated: "
1034                                "rc = %d\n", exp->exp_obd->obd_name, rc);
1035                         break;
1036                 }
1037
1038                 if (!data->ioc_inllen2 || !data->ioc_inlbuf2) {
1039                         rc = -EINVAL;
1040                         CERROR("%s: no NIDs specified: rc = %d\n",
1041                                exp->exp_obd->obd_name, rc);
1042                         break;
1043                 }
1044
1045                 if (data->ioc_inlbuf2[data->ioc_inllen2 - 1] != 0) {
1046                         rc = -EINVAL;
1047                         CERROR("%s: NID list is not NUL terminated: "
1048                                "rc = %d\n", exp->exp_obd->obd_name, rc);
1049                         break;
1050                 }
1051
1052                 /* replace nids in llog */
1053                 rc = mgs_replace_nids(&env, mgs, data->ioc_inlbuf1,
1054                                       data->ioc_inlbuf2);
1055                 if (rc)
1056                         CERROR("%s: error replacing nids: rc = %d\n",
1057                                exp->exp_obd->obd_name, rc);
1058
1059                 break;
1060         }
1061
1062         case OBD_IOC_CLEAR_CONFIGS: {
1063                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
1064                         rc = -EINVAL;
1065                         CERROR("%s: no device or fsname specified: rc = %d\n",
1066                                exp->exp_obd->obd_name, rc);
1067                         break;
1068                 }
1069
1070                 if (data->ioc_inllen1 > MTI_NAME_MAXLEN) {
1071                         rc = -EOVERFLOW;
1072                         CERROR("%s: device or fsname is too long: rc = %d\n",
1073                                exp->exp_obd->obd_name, rc);
1074                         break;
1075                 }
1076
1077                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
1078                         rc = -EINVAL;
1079                         CERROR("%s: device or fsname is not NUL terminated: "
1080                                "rc = %d\n", exp->exp_obd->obd_name, rc);
1081                         break;
1082                 }
1083
1084                 /* remove records marked SKIP from config logs */
1085                 rc = mgs_clear_configs(&env, mgs, data->ioc_inlbuf1);
1086                 if (rc)
1087                         CERROR("%s: error clearing config log: rc = %d\n",
1088                                exp->exp_obd->obd_name, rc);
1089
1090                 break;
1091         }
1092
1093         case OBD_IOC_POOL:
1094                 rc = mgs_iocontrol_pool(&env, mgs, data);
1095                 break;
1096
1097         case OBD_IOC_BARRIER:
1098                 rc = mgs_iocontrol_barrier(&env, mgs, data);
1099                 break;
1100
1101         case OBD_IOC_NODEMAP:
1102                 rc = mgs_iocontrol_nodemap(&env, mgs, data);
1103                 break;
1104
1105         case OBD_IOC_LCFG_FORK:
1106                 rc = mgs_lcfg_fork(&env, mgs, data->ioc_inlbuf1,
1107                                    data->ioc_inlbuf2);
1108                 break;
1109
1110         case OBD_IOC_LCFG_ERASE:
1111                 rc = mgs_lcfg_erase(&env, mgs, data->ioc_inlbuf1);
1112                 break;
1113
1114         case OBD_IOC_CATLOGLIST:
1115                 rc = mgs_list_logs(&env, mgs, data);
1116                 break;
1117         case OBD_IOC_LLOG_CANCEL:
1118         case OBD_IOC_LLOG_REMOVE:
1119         case OBD_IOC_LLOG_CHECK:
1120         case OBD_IOC_LLOG_INFO:
1121         case OBD_IOC_LLOG_PRINT: {
1122                 struct llog_ctxt *ctxt;
1123
1124                 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1125                 rc = llog_ioctl(&env, ctxt, cmd, data);
1126                 llog_ctxt_put(ctxt);
1127                 break;
1128         }
1129
1130         default:
1131                 CERROR("%s: unknown command %#x\n",
1132                        mgs->mgs_obd->obd_name,  cmd);
1133                 rc = -ENOTTY;
1134                 break;
1135         }
1136 out:
1137         lu_env_fini(&env);
1138         RETURN(rc);
1139 }
1140
1141 static int mgs_connect_to_osd(struct mgs_device *m, const char *nextdev)
1142 {
1143         struct obd_connect_data *data = NULL;
1144         struct obd_device       *obd;
1145         int                      rc;
1146         ENTRY;
1147
1148         OBD_ALLOC_PTR(data);
1149         if (data == NULL)
1150                 RETURN(-ENOMEM);
1151
1152         obd = class_name2obd(nextdev);
1153         if (obd == NULL) {
1154                 CERROR("can't locate next device: %s\n", nextdev);
1155                 GOTO(out, rc = -ENOTCONN);
1156         }
1157
1158         data->ocd_version = LUSTRE_VERSION_CODE;
1159
1160         rc = obd_connect(NULL, &m->mgs_bottom_exp, obd,
1161                          &obd->obd_uuid, data, NULL);
1162         if (rc) {
1163                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
1164                 GOTO(out, rc);
1165         }
1166
1167         m->mgs_bottom = lu2dt_dev(m->mgs_bottom_exp->exp_obd->obd_lu_dev);
1168         m->mgs_dt_dev.dd_lu_dev.ld_site = m->mgs_bottom->dd_lu_dev.ld_site;
1169         LASSERT(m->mgs_dt_dev.dd_lu_dev.ld_site);
1170 out:
1171         OBD_FREE_PTR(data);
1172         RETURN(rc);
1173 }
1174
1175 static struct tgt_handler mgs_mgs_handlers[] = {
1176 TGT_RPC_HANDLER(MGS_FIRST_OPC,
1177                 0,                      MGS_CONNECT,     mgs_connect,
1178                 &RQF_CONNECT, LUSTRE_OBD_VERSION),
1179 TGT_RPC_HANDLER(MGS_FIRST_OPC,
1180                 0,                      MGS_DISCONNECT,  mgs_disconnect,
1181                 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
1182 TGT_MGS_HDL_VAR(0,                      MGS_EXCEPTION,   mgs_exception),
1183 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 18, 53, 0)
1184 TGT_MGS_HDL(HAS_REPLY | IS_MUTABLE,     MGS_SET_INFO,    mgs_set_info),
1185 #endif
1186 TGT_MGS_HDL(HAS_REPLY | IS_MUTABLE,     MGS_TARGET_REG,  mgs_target_reg),
1187 TGT_MGS_HDL_VAR(0,                      MGS_TARGET_DEL,  mgs_target_del),
1188 TGT_MGS_HDL(HAS_REPLY,                  MGS_CONFIG_READ, mgs_config_read),
1189 };
1190
1191 static struct tgt_handler mgs_obd_handlers[] = {
1192 TGT_OBD_HDL(0,  OBD_PING,       tgt_obd_ping),
1193 };
1194
1195 static struct tgt_handler mgs_dlm_handlers[] = {
1196 [LDLM_ENQUEUE - LDLM_FIRST_OPC] = {
1197         .th_name = "LDLM_ENQUEUE",
1198         /* don't use th_fail_id for MGS to don't interfere with MDS tests.
1199          * * There are no tests for MGS with OBD_FAIL_LDLM_ENQUEUE_NET so it
1200          * * is safe. If such tests will be needed we have to distinguish
1201          * * MDS and MGS fail ids, e.g use OBD_FAIL_MGS_ENQUEUE_NET for MGS
1202          * * instead of common OBD_FAIL_LDLM_ENQUEUE_NET */
1203         .th_fail_id = 0,
1204         .th_opc = LDLM_ENQUEUE,
1205         .th_flags = HAS_KEY,
1206         .th_act = tgt_enqueue,
1207         .th_fmt = &RQF_LDLM_ENQUEUE,
1208         .th_version = LUSTRE_DLM_VERSION,
1209         },
1210 };
1211
1212 static struct tgt_handler mgs_llog_handlers[] = {
1213 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_CREATE,      mgs_llog_open),
1214 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_NEXT_BLOCK,  tgt_llog_next_block),
1215 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
1216 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_PREV_BLOCK,  tgt_llog_prev_block),
1217 };
1218
1219 static struct tgt_opc_slice mgs_common_slice[] = {
1220         {
1221                 .tos_opc_start = MGS_FIRST_OPC,
1222                 .tos_opc_end   = MGS_LAST_OPC,
1223                 .tos_hs        = mgs_mgs_handlers
1224         },
1225         {
1226                 .tos_opc_start = OBD_FIRST_OPC,
1227                 .tos_opc_end   = OBD_LAST_OPC,
1228                 .tos_hs        = mgs_obd_handlers
1229         },
1230         {
1231                 .tos_opc_start = LDLM_FIRST_OPC,
1232                 .tos_opc_end   = LDLM_LAST_OPC,
1233                 .tos_hs        = mgs_dlm_handlers
1234         },
1235         {
1236                 .tos_opc_start = LLOG_FIRST_OPC,
1237                 .tos_opc_end   = LLOG_LAST_OPC,
1238                 .tos_hs        = mgs_llog_handlers
1239         },
1240         {
1241                 .tos_opc_start = SEC_FIRST_OPC,
1242                 .tos_opc_end   = SEC_LAST_OPC,
1243                 .tos_hs        = tgt_sec_ctx_handlers
1244         },
1245         {
1246                 .tos_hs        = NULL
1247         }
1248 };
1249
1250 static int mgs_init0(const struct lu_env *env, struct mgs_device *mgs,
1251                      struct lu_device_type *ldt, struct lustre_cfg *lcfg)
1252 {
1253         struct ptlrpc_service_conf       conf;
1254         struct obd_device               *obd;
1255         struct lustre_mount_info        *lmi;
1256         struct llog_ctxt                *ctxt;
1257         int                              rc;
1258
1259         ENTRY;
1260
1261         lmi = server_get_mount(lustre_cfg_string(lcfg, 0));
1262         if (lmi == NULL)
1263                 RETURN(-ENODEV);
1264
1265         mgs->mgs_dt_dev.dd_lu_dev.ld_ops = &mgs_lu_ops;
1266
1267         rc = mgs_connect_to_osd(mgs, lustre_cfg_string(lcfg, 3));
1268         if (rc)
1269                 GOTO(err_lmi, rc);
1270
1271         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1272         LASSERT(obd);
1273         mgs->mgs_obd = obd;
1274         mgs->mgs_obd->obd_lu_dev = &mgs->mgs_dt_dev.dd_lu_dev;
1275
1276         obd->u.obt.obt_magic = OBT_MAGIC;
1277         obd->u.obt.obt_instance = 0;
1278
1279         /* namespace for mgs llog */
1280         obd->obd_namespace = ldlm_namespace_new(obd ,"MGS",
1281                                                 LDLM_NAMESPACE_SERVER,
1282                                                 LDLM_NAMESPACE_MODEST,
1283                                                 LDLM_NS_TYPE_MGT);
1284         if (IS_ERR(obd->obd_namespace)) {
1285                 rc = PTR_ERR(obd->obd_namespace);
1286                 CERROR("%s: unable to create server namespace: rc = %d\n",
1287                        obd->obd_name, rc);
1288                 obd->obd_namespace = NULL;
1289                 GOTO(err_ops, rc);
1290         }
1291
1292         /* No recovery for MGCs */
1293         obd->obd_replayable = 0;
1294
1295         rc = tgt_init(env, &mgs->mgs_lut, obd, mgs->mgs_bottom,
1296                       mgs_common_slice, OBD_FAIL_MGS_ALL_REQUEST_NET,
1297                       OBD_FAIL_MGS_ALL_REPLY_NET);
1298         if (rc)
1299                 GOTO(err_ns, rc);
1300
1301         rc = mgs_fs_setup(env, mgs);
1302         if (rc) {
1303                 CERROR("%s: MGS filesystem method init failed: rc = %d\n",
1304                        obd->obd_name, rc);
1305                 GOTO(err_tgt, rc);
1306         }
1307
1308         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT,
1309                         obd, &llog_osd_ops);
1310         if (rc)
1311                 GOTO(err_fs, rc);
1312
1313         /* XXX: we need this trick till N:1 stack is supported
1314          * set "current" directory for named llogs */
1315         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1316         LASSERT(ctxt);
1317         ctxt->loc_dir = mgs->mgs_configs_dir;
1318         llog_ctxt_put(ctxt);
1319
1320         /* Internal mgs setup */
1321         mgs_init_fsdb_list(mgs);
1322         mutex_init(&mgs->mgs_mutex);
1323         mgs->mgs_start_time = ktime_get_real_seconds();
1324         spin_lock_init(&mgs->mgs_lock);
1325         mutex_init(&mgs->mgs_health_mutex);
1326         init_rwsem(&mgs->mgs_barrier_rwsem);
1327
1328         rc = mgs_lcfg_rename(env, mgs);
1329         if (rc)
1330                 GOTO(err_llog, rc);
1331
1332         rc = lproc_mgs_setup(mgs, lustre_cfg_string(lcfg, 3));
1333         if (rc != 0) {
1334                 CERROR("%s: cannot initialize proc entry: rc = %d\n",
1335                        obd->obd_name, rc);
1336                 GOTO(err_llog, rc);
1337         }
1338
1339         /* Setup params fsdb and log, so that other servers can make a local
1340          * copy successfully when they are mounted. See LU-4783 */
1341         rc = mgs_params_fsdb_setup(env, mgs);
1342         if (rc)
1343                 /* params fsdb and log can be setup later */
1344                 CERROR("%s: %s fsdb and log setup failed: rc = %d\n",
1345                        obd->obd_name, PARAMS_FILENAME, rc);
1346
1347         /* Setup _mgs fsdb, useful for srpc */
1348         mgs__mgs_fsdb_setup(env, mgs);
1349
1350         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1351                            "mgs_ldlm_client", &obd->obd_ldlm_client);
1352
1353         conf = (typeof(conf)) {
1354                 .psc_name               = LUSTRE_MGS_NAME,
1355                 .psc_watchdog_factor    = MGS_SERVICE_WATCHDOG_FACTOR,
1356                 .psc_buf                = {
1357                         .bc_nbufs               = MGS_NBUFS,
1358                         .bc_buf_size            = MGS_BUFSIZE,
1359                         .bc_req_max_size        = MGS_MAXREQSIZE,
1360                         .bc_rep_max_size        = MGS_MAXREPSIZE,
1361                         .bc_req_portal          = MGS_REQUEST_PORTAL,
1362                         .bc_rep_portal          = MGC_REPLY_PORTAL,
1363                 },
1364                 .psc_thr                = {
1365                         .tc_thr_name            = "ll_mgs",
1366                         .tc_nthrs_init          = MGS_NTHRS_INIT,
1367                         .tc_nthrs_max           = MGS_NTHRS_MAX,
1368                         .tc_ctx_tags            = LCT_MG_THREAD,
1369                 },
1370                 .psc_ops                = {
1371                         .so_req_handler         = tgt_request_handle,
1372                         .so_req_printer         = target_print_req,
1373                 },
1374         };
1375
1376         /* Start the service threads */
1377         mgs->mgs_service = ptlrpc_register_service(&conf, &obd->obd_kset,
1378                                                    obd->obd_debugfs_entry);
1379         if (IS_ERR(mgs->mgs_service)) {
1380                 rc = PTR_ERR(mgs->mgs_service);
1381                 CERROR("failed to start mgs service: %d\n", rc);
1382                 mgs->mgs_service = NULL;
1383                 GOTO(err_lproc, rc);
1384         }
1385
1386         ping_evictor_start();
1387
1388         CDEBUG(D_INFO, "MGS %s started\n", obd->obd_name);
1389
1390         /* device stack is not yet fully setup to keep no objects behind */
1391         lu_site_purge(env, mgs2lu_dev(mgs)->ld_site, ~0);
1392         RETURN(0);
1393 err_lproc:
1394         mgs_params_fsdb_cleanup(env, mgs);
1395         lproc_mgs_cleanup(mgs);
1396 err_llog:
1397         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1398         if (ctxt) {
1399                 ctxt->loc_dir = NULL;
1400                 llog_cleanup(env, ctxt);
1401         }
1402 err_tgt:
1403         tgt_fini(env, &mgs->mgs_lut);
1404 err_fs:
1405         /* No extra cleanup needed for llog_init_commit_thread() */
1406         mgs_fs_cleanup(env, mgs);
1407 err_ns:
1408         ldlm_namespace_free(obd->obd_namespace, NULL, 0);
1409         obd->obd_namespace = NULL;
1410 err_ops:
1411         lu_site_purge(env, mgs2lu_dev(mgs)->ld_site, ~0);
1412         lu_site_print(env, mgs2lu_dev(mgs)->ld_site,
1413                       &mgs2lu_dev(mgs)->ld_site->ls_obj_hash.nelems,
1414                       D_OTHER, lu_cdebug_printer);
1415         obd_disconnect(mgs->mgs_bottom_exp);
1416 err_lmi:
1417         if (lmi)
1418                 server_put_mount(lustre_cfg_string(lcfg, 0), true);
1419         RETURN(rc);
1420 }
1421
1422 static struct lu_device *mgs_device_free(const struct lu_env *env,
1423                                          struct lu_device *lu)
1424 {
1425         struct mgs_device *mgs = lu2mgs_dev(lu);
1426         ENTRY;
1427
1428         dt_device_fini(&mgs->mgs_dt_dev);
1429         OBD_FREE_PTR(mgs);
1430         RETURN(NULL);
1431 }
1432
1433 static int mgs_process_config(const struct lu_env *env,
1434                               struct lu_device *dev,
1435                               struct lustre_cfg *lcfg)
1436 {
1437         LBUG();
1438         return 0;
1439 }
1440
1441 static int mgs_object_init(const struct lu_env *env, struct lu_object *o,
1442                            const struct lu_object_conf *unused)
1443 {
1444         struct mgs_device *d = lu2mgs_dev(o->lo_dev);
1445         struct lu_device  *under;
1446         struct lu_object  *below;
1447         int                rc = 0;
1448         ENTRY;
1449
1450         /* do no set .do_ops as mgs calls to bottom osd directly */
1451
1452         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
1453                         PFID(lu_object_fid(o)));
1454
1455         under = &d->mgs_bottom->dd_lu_dev;
1456         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
1457         if (below != NULL)
1458                 lu_object_add(o, below);
1459         else
1460                 rc = -ENOMEM;
1461
1462         return rc;
1463 }
1464
1465 static void mgs_object_free(const struct lu_env *env, struct lu_object *o)
1466 {
1467         struct mgs_object *obj = lu2mgs_obj(o);
1468         struct lu_object_header *h = o->lo_header;
1469
1470         dt_object_fini(&obj->mgo_obj);
1471         lu_object_header_fini(h);
1472         OBD_FREE_PRE(obj, sizeof(*obj), "kfreed");
1473         kfree_rcu(obj, mgo_header.loh_rcu);
1474 }
1475
1476 static int mgs_object_print(const struct lu_env *env, void *cookie,
1477                             lu_printer_t p, const struct lu_object *l)
1478 {
1479         const struct mgs_object *o = lu2mgs_obj((struct lu_object *) l);
1480
1481         return (*p)(env, cookie, LUSTRE_MGS_NAME"-object@%p", o);
1482 }
1483
1484 static const struct lu_object_operations mgs_lu_obj_ops = {
1485         .loo_object_init        = mgs_object_init,
1486         .loo_object_free        = mgs_object_free,
1487         .loo_object_print       = mgs_object_print,
1488 };
1489
1490 static struct lu_object *mgs_object_alloc(const struct lu_env *env,
1491                                           const struct lu_object_header *hdr,
1492                                           struct lu_device *d)
1493 {
1494         struct lu_object_header *h;
1495         struct mgs_object       *o;
1496         struct lu_object        *l;
1497
1498         LASSERT(hdr == NULL);
1499
1500         OBD_ALLOC_PTR(o);
1501         if (o != NULL) {
1502                 l = &o->mgo_obj.do_lu;
1503                 h = &o->mgo_header;
1504
1505                 lu_object_header_init(h);
1506                 dt_object_init(&o->mgo_obj, h, d);
1507                 lu_object_add_top(h, l);
1508
1509                 l->lo_ops = &mgs_lu_obj_ops;
1510
1511                 return l;
1512         } else {
1513                 return NULL;
1514         }
1515 }
1516
1517 const struct lu_device_operations mgs_lu_ops = {
1518         .ldo_object_alloc       = mgs_object_alloc,
1519         .ldo_process_config     = mgs_process_config,
1520 };
1521
1522 static struct lu_device *mgs_device_alloc(const struct lu_env *env,
1523                                           struct lu_device_type *type,
1524                                           struct lustre_cfg *lcfg)
1525 {
1526         struct mgs_device *mgs;
1527         struct lu_device  *ludev;
1528
1529         OBD_ALLOC_PTR(mgs);
1530         if (mgs == NULL) {
1531                 ludev = ERR_PTR(-ENOMEM);
1532         } else {
1533                 int rc;
1534
1535                 ludev = mgs2lu_dev(mgs);
1536                 dt_device_init(&mgs->mgs_dt_dev, type);
1537                 rc = mgs_init0(env, mgs, type, lcfg);
1538                 if (rc != 0) {
1539                         mgs_device_free(env, ludev);
1540                         ludev = ERR_PTR(rc);
1541                 }
1542         }
1543         return ludev;
1544 }
1545
1546 static struct lu_device *mgs_device_fini(const struct lu_env *env,
1547                                          struct lu_device *d)
1548 {
1549         struct mgs_device       *mgs = lu2mgs_dev(d);
1550         struct obd_device       *obd = mgs->mgs_obd;
1551         struct llog_ctxt        *ctxt;
1552
1553         ENTRY;
1554
1555         LASSERT(mgs->mgs_bottom);
1556
1557         class_disconnect_exports(obd);
1558
1559         ping_evictor_stop();
1560
1561         mutex_lock(&mgs->mgs_health_mutex);
1562         ptlrpc_unregister_service(mgs->mgs_service);
1563         mutex_unlock(&mgs->mgs_health_mutex);
1564
1565         mgs_params_fsdb_cleanup(env, mgs);
1566         mgs_cleanup_fsdb_list(mgs);
1567
1568         ldlm_namespace_free_prior(obd->obd_namespace, NULL, 1);
1569         obd_exports_barrier(obd);
1570         obd_zombie_barrier();
1571
1572         tgt_fini(env, &mgs->mgs_lut);
1573         lproc_mgs_cleanup(mgs);
1574
1575         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1576         if (ctxt) {
1577                 ctxt->loc_dir = NULL;
1578                 llog_cleanup(env, ctxt);
1579         }
1580
1581         mgs_fs_cleanup(env, mgs);
1582
1583         ldlm_namespace_free_post(obd->obd_namespace);
1584         obd->obd_namespace = NULL;
1585
1586         lu_site_purge(env, d->ld_site, ~0);
1587         lu_site_print(env, d->ld_site, &d->ld_site->ls_obj_hash.nelems,
1588                       D_OTHER, lu_cdebug_printer);
1589         LASSERT(mgs->mgs_bottom_exp);
1590         obd_disconnect(mgs->mgs_bottom_exp);
1591
1592         server_put_mount(obd->obd_name, true);
1593
1594         RETURN(NULL);
1595 }
1596
1597 /* context key constructor/destructor: mgs_key_init, mgs_key_fini */
1598 LU_KEY_INIT_FINI(mgs, struct mgs_thread_info);
1599
1600 LU_TYPE_INIT_FINI(mgs, &mgs_thread_key);
1601
1602 LU_CONTEXT_KEY_DEFINE(mgs, LCT_MG_THREAD);
1603
1604 static const struct lu_device_type_operations mgs_device_type_ops = {
1605         .ldto_init              = mgs_type_init,
1606         .ldto_fini              = mgs_type_fini,
1607
1608         .ldto_start             = mgs_type_start,
1609         .ldto_stop              = mgs_type_stop,
1610
1611         .ldto_device_alloc      = mgs_device_alloc,
1612         .ldto_device_free       = mgs_device_free,
1613
1614         .ldto_device_fini       = mgs_device_fini
1615 };
1616
1617 static struct lu_device_type mgs_device_type = {
1618         .ldt_tags       = LU_DEVICE_DT,
1619         .ldt_name       = LUSTRE_MGS_NAME,
1620         .ldt_ops        = &mgs_device_type_ops,
1621         .ldt_ctx_tags   = LCT_MG_THREAD
1622 };
1623
1624 static int mgs_obd_reconnect(const struct lu_env *env, struct obd_export *exp,
1625                              struct obd_device *obd, struct obd_uuid *cluuid,
1626                              struct obd_connect_data *data, void *localdata)
1627 {
1628         ENTRY;
1629
1630         if (exp == NULL || obd == NULL || cluuid == NULL)
1631                 RETURN(-EINVAL);
1632
1633         tgt_counter_incr(exp, LPROC_MGS_CONNECT);
1634
1635         if (data != NULL) {
1636                 data->ocd_connect_flags &= MGS_CONNECT_SUPPORTED;
1637
1638                 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
1639                         data->ocd_connect_flags2 &= MGS_CONNECT_SUPPORTED2;
1640
1641                 exp->exp_connect_data = *data;
1642                 data->ocd_version = LUSTRE_VERSION_CODE;
1643         }
1644
1645         RETURN(mgs_export_stats_init(obd, exp, localdata));
1646 }
1647
1648 static int mgs_obd_connect(const struct lu_env *env, struct obd_export **exp,
1649                            struct obd_device *obd, struct obd_uuid *cluuid,
1650                            struct obd_connect_data *data, void *localdata)
1651 {
1652         struct obd_export       *lexp;
1653         struct lustre_handle     conn = {
1654                 .cookie = 0,
1655         };
1656         int                      rc;
1657
1658         ENTRY;
1659
1660         if (exp == NULL || obd == NULL || cluuid == NULL)
1661                 RETURN(-EINVAL);
1662
1663         rc = class_connect(&conn, obd, cluuid);
1664         if (rc)
1665                 RETURN(rc);
1666
1667         lexp = class_conn2export(&conn);
1668         if (lexp == NULL)
1669                 RETURN(-EFAULT);
1670
1671         rc = mgs_obd_reconnect(env, lexp, obd, cluuid, data, localdata);
1672         if (rc)
1673                 GOTO(out_disconnect, rc);
1674
1675         *exp = lexp;
1676
1677         RETURN(rc);
1678
1679 out_disconnect:
1680         class_disconnect(lexp);
1681
1682         return rc;
1683 }
1684
1685 static int mgs_obd_disconnect(struct obd_export *exp)
1686 {
1687         int rc;
1688
1689         ENTRY;
1690
1691         LASSERT(exp);
1692
1693         mgs_fsc_cleanup(exp);
1694
1695         class_export_get(exp);
1696         tgt_counter_incr(exp, LPROC_MGS_DISCONNECT);
1697
1698         rc = server_disconnect_export(exp);
1699         class_export_put(exp);
1700         RETURN(rc);
1701 }
1702
1703 static int mgs_health_check(const struct lu_env *env, struct obd_device *obd)
1704 {
1705         struct mgs_device *mgs = lu2mgs_dev(obd->obd_lu_dev);
1706         int rc = 0;
1707
1708         mutex_lock(&mgs->mgs_health_mutex);
1709         rc |= ptlrpc_service_health_check(mgs->mgs_service);
1710         mutex_unlock(&mgs->mgs_health_mutex);
1711
1712         return rc != 0 ? 1 : 0;
1713 }
1714
1715 /* use obd ops to offer management infrastructure */
1716 static const struct obd_ops mgs_obd_device_ops = {
1717         .o_owner                = THIS_MODULE,
1718         .o_connect              = mgs_obd_connect,
1719         .o_reconnect            = mgs_obd_reconnect,
1720         .o_disconnect           = mgs_obd_disconnect,
1721         .o_init_export          = mgs_init_export,
1722         .o_destroy_export       = mgs_destroy_export,
1723         .o_iocontrol            = mgs_iocontrol,
1724         .o_health_check         = mgs_health_check,
1725 };
1726
1727 static int __init mgs_init(void)
1728 {
1729         return class_register_type(&mgs_obd_device_ops, NULL, true,
1730                                    LUSTRE_MGS_NAME, &mgs_device_type);
1731 }
1732
1733 static void __exit mgs_exit(void)
1734 {
1735         class_unregister_type(LUSTRE_MGS_NAME);
1736 }
1737
1738 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
1739 MODULE_DESCRIPTION("Lustre Management Server (MGS)");
1740 MODULE_VERSION(LUSTRE_VERSION_STRING);
1741 MODULE_LICENSE("GPL");
1742
1743 module_init(mgs_init);
1744 module_exit(mgs_exit);