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