Whamcloud - gitweb
LU-4795 obd: Remove server_{get,put}_mount_2() functions
[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         rc = tgt_connect(tsi);
64         if (rc)
65                 RETURN(rc);
66
67         if (lustre_msg_get_conn_cnt(req->rq_reqmsg) > 1)
68                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
69
70         RETURN(0);
71 }
72
73 static int mgs_disconnect(struct tgt_session_info *tsi)
74 {
75         int rc;
76
77         ENTRY;
78
79         LASSERT(tsi->tsi_exp);
80
81         rc = tgt_disconnect(tsi);
82         if (rc)
83                 RETURN(err_serious(rc));
84         RETURN(0);
85 }
86
87 static int mgs_exception(struct tgt_session_info *tsi)
88 {
89         ENTRY;
90
91         tgt_counter_incr(tsi->tsi_exp, LPROC_MGS_EXCEPTION);
92
93         RETURN(0);
94 }
95
96 static int mgs_set_info(struct tgt_session_info *tsi)
97 {
98         struct mgs_thread_info  *mgi;
99         struct mgs_send_param   *msp, *rep_msp;
100         struct lustre_cfg       *lcfg;
101         int                      rc;
102
103         ENTRY;
104
105         mgi = mgs_env_info(tsi->tsi_env);
106         if (IS_ERR(mgi))
107                 RETURN(err_serious(PTR_ERR(mgi)));
108
109         msp = req_capsule_client_get(tsi->tsi_pill, &RMF_MGS_SEND_PARAM);
110         if (msp == NULL)
111                 RETURN(err_serious(-EFAULT));
112
113         /* Construct lustre_cfg structure to pass to function mgs_setparam */
114         lustre_cfg_bufs_reset(&mgi->mgi_bufs, NULL);
115         lustre_cfg_bufs_set_string(&mgi->mgi_bufs, 1, msp->mgs_param);
116         lcfg = lustre_cfg_new(LCFG_PARAM, &mgi->mgi_bufs);
117
118         rc = mgs_setparam(tsi->tsi_env, exp2mgs_dev(tsi->tsi_exp), lcfg,
119                           mgi->mgi_fsname);
120         if (rc) {
121                 LCONSOLE_WARN("%s: Unable to set parameter %s for %s: %d\n",
122                               tgt_name(tsi->tsi_tgt), msp->mgs_param,
123                               mgi->mgi_fsname, rc);
124                 GOTO(out_cfg, rc);
125         }
126
127         /* send back the whole msp in the reply */
128         rep_msp = req_capsule_server_get(tsi->tsi_pill, &RMF_MGS_SEND_PARAM);
129         *rep_msp = *msp;
130         EXIT;
131 out_cfg:
132         lustre_cfg_free(lcfg);
133         return rc;
134 }
135
136 enum ast_type {
137                 AST_CONFIG = 1,
138                 AST_PARAMS = 2,
139                 AST_IR = 3
140 };
141
142 static int mgs_completion_ast_generic(struct ldlm_lock *lock, __u64 flags,
143                                      void *cbdata, enum ast_type type)
144 {
145         ENTRY;
146
147         if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
148                        LDLM_FL_BLOCK_CONV))) {
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 = (int)(ptr - logname);
543
544                 if (ptr == NULL || len >= sizeof(mgi->mgi_fsname)) {
545                         LCONSOLE_WARN("%s: non-config logname received: %s\n",
546                                       tgt_name(tsi->tsi_tgt), logname);
547                         /* not error, this can be llog test name */
548                 } else {
549                         strncpy(mgi->mgi_fsname, logname, len);
550                         mgi->mgi_fsname[len] = 0;
551
552                         rc = mgs_fsc_attach(tsi->tsi_env, tsi->tsi_exp,
553                                             mgi->mgi_fsname);
554                         if (rc && rc != -EEXIST) {
555                                 LCONSOLE_WARN("%s: Unable to add client %s "
556                                               "to file system %s: %d\n",
557                                               tgt_name(tsi->tsi_tgt),
558                                               libcfs_nid2str(req->rq_peer.nid),
559                                               mgi->mgi_fsname, rc);
560                         } else {
561                                 rc = 0;
562                         }
563                 }
564         } else {
565                 CERROR("%s: no logname in request\n", tgt_name(tsi->tsi_tgt));
566                 RETURN(-EINVAL);
567         }
568         RETURN(rc);
569 }
570
571 static inline int mgs_init_export(struct obd_export *exp)
572 {
573         struct mgs_export_data *data = &exp->u.eu_mgs_data;
574
575         /* init mgs_export_data for fsc */
576         spin_lock_init(&data->med_lock);
577         CFS_INIT_LIST_HEAD(&data->med_clients);
578
579         spin_lock(&exp->exp_lock);
580         exp->exp_connecting = 1;
581         spin_unlock(&exp->exp_lock);
582
583         /* self-export doesn't need client data and ldlm initialization */
584         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
585                                      &exp->exp_client_uuid)))
586                 return 0;
587         return ldlm_init_export(exp);
588 }
589
590 static inline int mgs_destroy_export(struct obd_export *exp)
591 {
592         ENTRY;
593
594         target_destroy_export(exp);
595         mgs_client_free(exp);
596
597         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
598                                      &exp->exp_client_uuid)))
599                 RETURN(0);
600
601         ldlm_destroy_export(exp);
602
603         RETURN(0);
604 }
605
606 static int mgs_extract_fs_pool(char * arg, char *fsname, char *poolname)
607 {
608         char *ptr;
609
610         ENTRY;
611         for (ptr = arg;  (*ptr != '\0') && (*ptr != '.'); ptr++ ) {
612                 *fsname = *ptr;
613                 fsname++;
614         }
615         if (*ptr == '\0')
616                 return -EINVAL;
617         *fsname = '\0';
618         ptr++;
619         strcpy(poolname, ptr);
620
621         RETURN(0);
622 }
623
624 static int mgs_iocontrol_nodemap(const struct lu_env *env,
625                                  struct mgs_device *mgs,
626                                  struct obd_ioctl_data *data)
627 {
628         struct lustre_cfg       *lcfg = NULL;
629         struct lu_nodemap       *nodemap;
630         lnet_nid_t              nid;
631         const char              *nodemap_name = NULL;
632         const char              *nidstr = NULL;
633         const char              *client_idstr = NULL;
634         const char              *idtype_str = NULL;
635         char                    *param = NULL;
636         char                    fs_idstr[16];
637         int                     rc = 0;
638         __u32                   client_id;
639         __u32                   fs_id;
640         __u32                   cmd;
641         int                     idtype;
642
643         ENTRY;
644
645         if (data->ioc_type != LUSTRE_CFG_TYPE) {
646                 CERROR("%s: unknown cfg record type: %d\n",
647                        mgs->mgs_obd->obd_name, data->ioc_type);
648                 GOTO(out, rc = -EINVAL);
649         }
650
651         if (data->ioc_plen1 > PAGE_CACHE_SIZE)
652                 GOTO(out, rc = -E2BIG);
653
654         OBD_ALLOC(lcfg, data->ioc_plen1);
655         if (lcfg == NULL)
656                 GOTO(out, rc = -ENOMEM);
657
658         if (copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
659                 GOTO(out_lcfg, rc = -EFAULT);
660
661         cmd = lcfg->lcfg_command;
662
663         switch (cmd) {
664         case LCFG_NODEMAP_ACTIVATE:
665                 if (lcfg->lcfg_bufcount != 2)
666                         GOTO(out_lcfg, rc = -EINVAL);
667                 param = lustre_cfg_string(lcfg, 1);
668                 if (strcmp(param, "1") == 0)
669                                 nodemap_activate(1);
670                 else
671                                 nodemap_activate(0);
672                 break;
673         case LCFG_NODEMAP_ADD:
674         case LCFG_NODEMAP_DEL:
675                 if (lcfg->lcfg_bufcount != 2)
676                         GOTO(out_lcfg, rc = -EINVAL);
677                 nodemap_name = lustre_cfg_string(lcfg, 1);
678                 rc = mgs_nodemap_cmd(env, mgs, cmd, nodemap_name, param);
679                 break;
680         case LCFG_NODEMAP_TEST_NID:
681                 if (lcfg->lcfg_bufcount != 2)
682                         GOTO(out_lcfg, rc = -EINVAL);
683                 nidstr = lustre_cfg_string(lcfg, 1);
684                 nid = libcfs_str2nid(nidstr);
685                 nodemap = nodemap_classify_nid(nid);
686                 memset(data->ioc_pbuf1, 0, data->ioc_plen1);
687                 if (copy_to_user(data->ioc_pbuf1, nodemap->nm_name,
688                                  strlen(nodemap->nm_name)) != 0)
689                         GOTO(out_lcfg, rc = -EFAULT);
690                 break;
691         case LCFG_NODEMAP_TEST_ID:
692                 if (lcfg->lcfg_bufcount != 4)
693                         GOTO(out_lcfg, rc = -EINVAL);
694                 nidstr = lustre_cfg_string(lcfg, 1);
695                 idtype_str = lustre_cfg_string(lcfg, 2);
696                 client_idstr = lustre_cfg_string(lcfg, 3);
697
698                 nid = libcfs_str2nid(nidstr);
699                 nodemap = nodemap_classify_nid(nid);
700                 client_id = simple_strtoul(client_idstr, NULL, 10);
701
702                 if (strcmp(idtype_str, "uid") == 0)
703                         idtype = NODEMAP_UID;
704                 else
705                         idtype = NODEMAP_GID;
706
707                 fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
708                                        client_id);
709
710                 if (data->ioc_plen1 < sizeof(fs_idstr))
711                         GOTO(out_lcfg, rc = -EINVAL);
712
713                 snprintf(fs_idstr, sizeof(fs_idstr), "%u", fs_id);
714                 if (copy_to_user(data->ioc_pbuf1, fs_idstr,
715                                  sizeof(fs_idstr)) != 0)
716                         GOTO(out_lcfg, rc = -EINVAL);
717                 break;
718         case LCFG_NODEMAP_ADD_RANGE:
719         case LCFG_NODEMAP_DEL_RANGE:
720         case LCFG_NODEMAP_ADD_UIDMAP:
721         case LCFG_NODEMAP_DEL_UIDMAP:
722         case LCFG_NODEMAP_ADD_GIDMAP:
723         case LCFG_NODEMAP_DEL_GIDMAP:
724                 if (lcfg->lcfg_bufcount != 3)
725                         GOTO(out_lcfg, rc = -EINVAL);
726                 nodemap_name = lustre_cfg_string(lcfg, 1);
727                 param = lustre_cfg_string(lcfg, 2);
728                 rc = mgs_nodemap_cmd(env, mgs, cmd, nodemap_name, param);
729                 break;
730         case LCFG_NODEMAP_ADMIN:
731         case LCFG_NODEMAP_TRUSTED:
732         case LCFG_NODEMAP_SQUASH_UID:
733         case LCFG_NODEMAP_SQUASH_GID:
734                 if (lcfg->lcfg_bufcount != 4)
735                         GOTO(out_lcfg, rc = -EINVAL);
736                 nodemap_name = lustre_cfg_string(lcfg, 1);
737                 param = lustre_cfg_string(lcfg, 3);
738                 rc = mgs_nodemap_cmd(env, mgs, cmd, nodemap_name, param);
739                 break;
740         default:
741                 rc = -ENOTTY;
742         }
743
744         if (rc != 0) {
745                 CERROR("%s: OBD_IOC_NODEMAP command %X for %s: rc = %d\n",
746                        mgs->mgs_obd->obd_name, lcfg->lcfg_command,
747                        nodemap_name, rc);
748                 GOTO(out_lcfg, rc);
749         }
750
751 out_lcfg:
752         OBD_FREE(lcfg, data->ioc_plen1);
753 out:
754         RETURN(rc);
755 }
756
757 static int mgs_iocontrol_pool(const struct lu_env *env,
758                               struct mgs_device *mgs,
759                               struct obd_ioctl_data *data)
760 {
761         struct mgs_thread_info *mgi = mgs_env_info(env);
762         int rc;
763         struct lustre_cfg *lcfg = NULL;
764         char *poolname = NULL;
765         ENTRY;
766
767         OBD_ALLOC(poolname, LOV_MAXPOOLNAME + 1);
768         if (poolname == NULL)
769                 RETURN(-ENOMEM);
770
771         if (data->ioc_type != LUSTRE_CFG_TYPE) {
772                 CERROR("%s: unknown cfg record type: %d\n",
773                        mgs->mgs_obd->obd_name, data->ioc_type);
774                 GOTO(out_pool, rc = -EINVAL);
775         }
776
777         if (data->ioc_plen1 > PAGE_CACHE_SIZE)
778                 GOTO(out_pool, rc = -E2BIG);
779
780         OBD_ALLOC(lcfg, data->ioc_plen1);
781         if (lcfg == NULL)
782                 GOTO(out_pool, rc = -ENOMEM);
783
784         if (copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
785                 GOTO(out_lcfg, rc = -EFAULT);
786
787         if (lcfg->lcfg_bufcount < 2)
788                 GOTO(out_lcfg, rc = -EFAULT);
789
790         /* first arg is always <fsname>.<poolname> */
791         rc = mgs_extract_fs_pool(lustre_cfg_string(lcfg, 1), mgi->mgi_fsname,
792                                  poolname);
793         if (rc)
794                 GOTO(out_lcfg, rc);
795
796         switch (lcfg->lcfg_command) {
797         case LCFG_POOL_NEW:
798                 if (lcfg->lcfg_bufcount != 2)
799                         GOTO(out_lcfg, rc = -EINVAL);
800                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_NEW, mgi->mgi_fsname,
801                                   poolname, NULL);
802                 break;
803         case LCFG_POOL_ADD:
804                 if (lcfg->lcfg_bufcount != 3)
805                         GOTO(out_lcfg, rc = -EINVAL);
806                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_ADD, mgi->mgi_fsname,
807                                   poolname, lustre_cfg_string(lcfg, 2));
808                 break;
809         case LCFG_POOL_REM:
810                 if (lcfg->lcfg_bufcount != 3)
811                         GOTO(out_lcfg, rc = -EINVAL);
812                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_REM, mgi->mgi_fsname,
813                                   poolname, lustre_cfg_string(lcfg, 2));
814                 break;
815         case LCFG_POOL_DEL:
816                 if (lcfg->lcfg_bufcount != 2)
817                         GOTO(out_lcfg, rc = -EINVAL);
818                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_DEL, mgi->mgi_fsname,
819                                   poolname, NULL);
820                 break;
821         default:
822                  rc = -EINVAL;
823         }
824
825         if (rc) {
826                 CERROR("OBD_IOC_POOL err %d, cmd %X for pool %s.%s\n",
827                        rc, lcfg->lcfg_command, mgi->mgi_fsname, poolname);
828                 GOTO(out_lcfg, rc);
829         }
830
831 out_lcfg:
832         OBD_FREE(lcfg, data->ioc_plen1);
833 out_pool:
834         OBD_FREE(poolname, LOV_MAXPOOLNAME + 1);
835         RETURN(rc);
836 }
837
838 /* from mdt_iocontrol */
839 int mgs_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
840                   void *karg, void *uarg)
841 {
842         struct mgs_device *mgs = exp2mgs_dev(exp);
843         struct obd_ioctl_data *data = karg;
844         struct lu_env env;
845         int rc = 0;
846
847         ENTRY;
848         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
849
850         rc = lu_env_init(&env, LCT_MG_THREAD);
851         if (rc)
852                 RETURN(rc);
853
854         switch (cmd) {
855
856         case OBD_IOC_PARAM: {
857                 struct mgs_thread_info *mgi = mgs_env_info(&env);
858                 struct lustre_cfg *lcfg;
859
860                 if (data->ioc_type != LUSTRE_CFG_TYPE) {
861                         CERROR("%s: unknown cfg record type: %d\n",
862                                mgs->mgs_obd->obd_name, data->ioc_type);
863                         GOTO(out, rc = -EINVAL);
864                 }
865
866                 OBD_ALLOC(lcfg, data->ioc_plen1);
867                 if (lcfg == NULL)
868                         GOTO(out, rc = -ENOMEM);
869                 if (copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
870                         GOTO(out_free, rc = -EFAULT);
871
872                 if (lcfg->lcfg_bufcount < 1)
873                         GOTO(out_free, rc = -EINVAL);
874
875                 rc = mgs_setparam(&env, mgs, lcfg, mgi->mgi_fsname);
876                 if (rc)
877                         CERROR("%s: setparam err: rc = %d\n",
878                                exp->exp_obd->obd_name, rc);
879 out_free:
880                 OBD_FREE(lcfg, data->ioc_plen1);
881                 break;
882         }
883
884         case OBD_IOC_REPLACE_NIDS: {
885                 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
886                         CERROR("No device name specified!\n");
887                         rc = -EINVAL;
888                         break;
889                 }
890
891                 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
892                         CERROR("Device name is not NUL terminated!\n");
893                         rc = -EINVAL;
894                         break;
895                 }
896
897                 if (data->ioc_plen1 > MTI_NAME_MAXLEN) {
898                         CERROR("Device name is too long\n");
899                         rc = -EOVERFLOW;
900                         break;
901                 }
902
903                 if (!data->ioc_inllen2 || !data->ioc_inlbuf2) {
904                         CERROR("No NIDs were specified!\n");
905                         rc = -EINVAL;
906                         break;
907                 }
908
909                 if (data->ioc_inlbuf2[data->ioc_inllen2 - 1] != 0) {
910                         CERROR("NID list is not NUL terminated!\n");
911                         rc = -EINVAL;
912                         break;
913                 }
914
915                 /* replace nids in llog */
916                 rc = mgs_replace_nids(&env, mgs, data->ioc_inlbuf1,
917                                       data->ioc_inlbuf2);
918                 if (rc)
919                         CERROR("%s: error replacing nids: rc = %d\n",
920                                exp->exp_obd->obd_name, rc);
921
922                 break;
923         }
924
925         case OBD_IOC_POOL:
926                 rc = mgs_iocontrol_pool(&env, mgs, data);
927                 break;
928
929         case OBD_IOC_NODEMAP:
930                 rc = mgs_iocontrol_nodemap(&env, mgs, data);
931                 break;
932
933         case OBD_IOC_DUMP_LOG: {
934                 struct llog_ctxt *ctxt;
935
936                 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
937                 rc = class_config_dump_llog(&env, ctxt, data->ioc_inlbuf1,
938                                             NULL);
939                 llog_ctxt_put(ctxt);
940
941                 break;
942         }
943
944         case OBD_IOC_CATLOGLIST:
945                 rc = mgs_list_logs(&env, mgs, data);
946                 break;
947         case OBD_IOC_LLOG_CANCEL:
948         case OBD_IOC_LLOG_REMOVE:
949         case OBD_IOC_LLOG_CHECK:
950         case OBD_IOC_LLOG_INFO:
951         case OBD_IOC_LLOG_PRINT: {
952                 struct llog_ctxt *ctxt;
953
954                 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
955                 rc = llog_ioctl(&env, ctxt, cmd, data);
956                 llog_ctxt_put(ctxt);
957                 break;
958         }
959
960         default:
961                 CERROR("%s: unknown command %#x\n",
962                        mgs->mgs_obd->obd_name,  cmd);
963                 rc = -ENOTTY;
964                 break;
965         }
966 out:
967         lu_env_fini(&env);
968         RETURN(rc);
969 }
970
971 static int mgs_connect_to_osd(struct mgs_device *m, const char *nextdev)
972 {
973         struct obd_connect_data *data = NULL;
974         struct obd_device       *obd;
975         int                      rc;
976         ENTRY;
977
978         OBD_ALLOC_PTR(data);
979         if (data == NULL)
980                 RETURN(-ENOMEM);
981
982         obd = class_name2obd(nextdev);
983         if (obd == NULL) {
984                 CERROR("can't locate next device: %s\n", nextdev);
985                 GOTO(out, rc = -ENOTCONN);
986         }
987
988         data->ocd_version = LUSTRE_VERSION_CODE;
989
990         rc = obd_connect(NULL, &m->mgs_bottom_exp, obd,
991                          &obd->obd_uuid, data, NULL);
992         if (rc) {
993                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
994                 GOTO(out, rc);
995         }
996
997         m->mgs_bottom = lu2dt_dev(m->mgs_bottom_exp->exp_obd->obd_lu_dev);
998         m->mgs_dt_dev.dd_lu_dev.ld_site = m->mgs_bottom->dd_lu_dev.ld_site;
999         LASSERT(m->mgs_dt_dev.dd_lu_dev.ld_site);
1000 out:
1001         OBD_FREE_PTR(data);
1002         RETURN(rc);
1003 }
1004
1005 static struct tgt_handler mgs_mgs_handlers[] = {
1006 TGT_RPC_HANDLER(MGS_FIRST_OPC,
1007                 0,                      MGS_CONNECT,     mgs_connect,
1008                 &RQF_CONNECT, LUSTRE_OBD_VERSION),
1009 TGT_RPC_HANDLER(MGS_FIRST_OPC,
1010                 0,                      MGS_DISCONNECT,  mgs_disconnect,
1011                 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
1012 TGT_MGS_HDL_VAR(0,                      MGS_EXCEPTION,   mgs_exception),
1013 TGT_MGS_HDL    (HABEO_REFERO | MUTABOR, MGS_SET_INFO,    mgs_set_info),
1014 TGT_MGS_HDL    (HABEO_REFERO | MUTABOR, MGS_TARGET_REG,  mgs_target_reg),
1015 TGT_MGS_HDL_VAR(0,                      MGS_TARGET_DEL,  mgs_target_del),
1016 TGT_MGS_HDL    (HABEO_REFERO,           MGS_CONFIG_READ, mgs_config_read),
1017 };
1018
1019 static struct tgt_handler mgs_obd_handlers[] = {
1020 TGT_OBD_HDL(0,  OBD_PING,       tgt_obd_ping),
1021 };
1022
1023 static struct tgt_handler mgs_dlm_handlers[] = {
1024 [LDLM_ENQUEUE - LDLM_FIRST_OPC] = {
1025         .th_name = "LDLM_ENQUEUE",
1026         /* don't use th_fail_id for MGS to don't interfere with MDS tests.
1027          * * There are no tests for MGS with OBD_FAIL_LDLM_ENQUEUE_NET so it
1028          * * is safe. If such tests will be needed we have to distinguish
1029          * * MDS and MGS fail ids, e.g use OBD_FAIL_MGS_ENQUEUE_NET for MGS
1030          * * instead of common OBD_FAIL_LDLM_ENQUEUE_NET */
1031         .th_fail_id = 0,
1032         .th_opc = LDLM_ENQUEUE,
1033         .th_flags = HABEO_CLAVIS,
1034         .th_act = tgt_enqueue,
1035         .th_fmt = &RQF_LDLM_ENQUEUE,
1036         .th_version = LUSTRE_DLM_VERSION,
1037         },
1038 };
1039
1040 static struct tgt_handler mgs_llog_handlers[] = {
1041 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_CREATE,      mgs_llog_open),
1042 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_NEXT_BLOCK,  tgt_llog_next_block),
1043 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
1044 TGT_LLOG_HDL_VAR(0,     LLOG_ORIGIN_HANDLE_CLOSE,       tgt_llog_close),
1045 TGT_LLOG_HDL    (0,     LLOG_ORIGIN_HANDLE_PREV_BLOCK,  tgt_llog_prev_block),
1046 };
1047
1048 static struct tgt_opc_slice mgs_common_slice[] = {
1049         {
1050                 .tos_opc_start = MGS_FIRST_OPC,
1051                 .tos_opc_end   = MGS_LAST_OPC,
1052                 .tos_hs        = mgs_mgs_handlers
1053         },
1054         {
1055                 .tos_opc_start = OBD_FIRST_OPC,
1056                 .tos_opc_end   = OBD_LAST_OPC,
1057                 .tos_hs        = mgs_obd_handlers
1058         },
1059         {
1060                 .tos_opc_start = LDLM_FIRST_OPC,
1061                 .tos_opc_end   = LDLM_LAST_OPC,
1062                 .tos_hs        = mgs_dlm_handlers
1063         },
1064         {
1065                 .tos_opc_start = LLOG_FIRST_OPC,
1066                 .tos_opc_end   = LLOG_LAST_OPC,
1067                 .tos_hs        = mgs_llog_handlers
1068         },
1069         {
1070                 .tos_opc_start = SEC_FIRST_OPC,
1071                 .tos_opc_end   = SEC_LAST_OPC,
1072                 .tos_hs        = tgt_sec_ctx_handlers
1073         },
1074         {
1075                 .tos_hs        = NULL
1076         }
1077 };
1078
1079 static int mgs_init0(const struct lu_env *env, struct mgs_device *mgs,
1080                      struct lu_device_type *ldt, struct lustre_cfg *lcfg)
1081 {
1082         struct ptlrpc_service_conf       conf;
1083         struct obd_device               *obd;
1084         struct lustre_mount_info        *lmi;
1085         struct llog_ctxt                *ctxt;
1086         struct fs_db                    *fsdb = NULL;
1087         int                              rc;
1088
1089         ENTRY;
1090
1091         lmi = server_get_mount(lustre_cfg_string(lcfg, 0));
1092         if (lmi == NULL)
1093                 RETURN(-ENODEV);
1094
1095         mgs->mgs_dt_dev.dd_lu_dev.ld_ops = &mgs_lu_ops;
1096
1097         rc = mgs_connect_to_osd(mgs, lustre_cfg_string(lcfg, 3));
1098         if (rc)
1099                 GOTO(err_lmi, rc);
1100
1101         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1102         LASSERT(obd);
1103         mgs->mgs_obd = obd;
1104         mgs->mgs_obd->obd_lu_dev = &mgs->mgs_dt_dev.dd_lu_dev;
1105
1106         obd->u.obt.obt_magic = OBT_MAGIC;
1107         obd->u.obt.obt_instance = 0;
1108
1109         /* namespace for mgs llog */
1110         obd->obd_namespace = ldlm_namespace_new(obd ,"MGS",
1111                                                 LDLM_NAMESPACE_SERVER,
1112                                                 LDLM_NAMESPACE_MODEST,
1113                                                 LDLM_NS_TYPE_MGT);
1114         if (obd->obd_namespace == NULL)
1115                 GOTO(err_ops, rc = -ENOMEM);
1116
1117         /* No recovery for MGCs */
1118         obd->obd_replayable = 0;
1119
1120         rc = tgt_init(env, &mgs->mgs_lut, obd, mgs->mgs_bottom,
1121                       mgs_common_slice, OBD_FAIL_MGS_ALL_REQUEST_NET,
1122                       OBD_FAIL_MGS_ALL_REPLY_NET);
1123         if (rc)
1124                 GOTO(err_ns, rc);
1125
1126         rc = mgs_fs_setup(env, mgs);
1127         if (rc) {
1128                 CERROR("%s: MGS filesystem method init failed: rc = %d\n",
1129                        obd->obd_name, rc);
1130                 GOTO(err_tgt, rc);
1131         }
1132
1133         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT,
1134                         obd, &llog_osd_ops);
1135         if (rc)
1136                 GOTO(err_fs, rc);
1137
1138         /* XXX: we need this trick till N:1 stack is supported
1139          * set "current" directory for named llogs */
1140         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1141         LASSERT(ctxt);
1142         ctxt->loc_dir = mgs->mgs_configs_dir;
1143         llog_ctxt_put(ctxt);
1144
1145         /* Internal mgs setup */
1146         mgs_init_fsdb_list(mgs);
1147         mutex_init(&mgs->mgs_mutex);
1148         mgs->mgs_start_time = cfs_time_current_sec();
1149         spin_lock_init(&mgs->mgs_lock);
1150
1151         rc = lproc_mgs_setup(mgs, lustre_cfg_string(lcfg, 3));
1152         if (rc != 0) {
1153                 CERROR("%s: cannot initialize proc entry: rc = %d\n",
1154                        obd->obd_name, rc);
1155                 GOTO(err_llog, rc);
1156         }
1157
1158         /* Setup params fsdb and log, so that other servers can make a local
1159          * copy successfully when they are mounted. See LU-4783 */
1160         rc = mgs_params_fsdb_setup(env, mgs, fsdb);
1161         if (rc)
1162                 /* params fsdb and log can be setup later */
1163                 CERROR("%s: %s fsdb and log setup failed: rc = %d\n",
1164                        obd->obd_name, PARAMS_FILENAME, rc);
1165
1166         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1167                            "mgs_ldlm_client", &obd->obd_ldlm_client);
1168
1169         conf = (typeof(conf)) {
1170                 .psc_name               = LUSTRE_MGS_NAME,
1171                 .psc_watchdog_factor    = MGS_SERVICE_WATCHDOG_FACTOR,
1172                 .psc_buf                = {
1173                         .bc_nbufs               = MGS_NBUFS,
1174                         .bc_buf_size            = MGS_BUFSIZE,
1175                         .bc_req_max_size        = MGS_MAXREQSIZE,
1176                         .bc_rep_max_size        = MGS_MAXREPSIZE,
1177                         .bc_req_portal          = MGS_REQUEST_PORTAL,
1178                         .bc_rep_portal          = MGC_REPLY_PORTAL,
1179                 },
1180                 .psc_thr                = {
1181                         .tc_thr_name            = "ll_mgs",
1182                         .tc_nthrs_init          = MGS_NTHRS_INIT,
1183                         .tc_nthrs_max           = MGS_NTHRS_MAX,
1184                         .tc_ctx_tags            = LCT_MG_THREAD,
1185                 },
1186                 .psc_ops                = {
1187                         .so_req_handler         = tgt_request_handle,
1188                         .so_req_printer         = target_print_req,
1189                 },
1190         };
1191
1192         /* Start the service threads */
1193         mgs->mgs_service = ptlrpc_register_service(&conf, obd->obd_proc_entry);
1194         if (IS_ERR(mgs->mgs_service)) {
1195                 rc = PTR_ERR(mgs->mgs_service);
1196                 CERROR("failed to start mgs service: %d\n", rc);
1197                 mgs->mgs_service = NULL;
1198                 GOTO(err_lproc, rc);
1199         }
1200
1201         ping_evictor_start();
1202
1203         CDEBUG(D_INFO, "MGS %s started\n", obd->obd_name);
1204
1205         /* device stack is not yet fully setup to keep no objects behind */
1206         lu_site_purge(env, mgs2lu_dev(mgs)->ld_site, ~0);
1207         RETURN(0);
1208 err_lproc:
1209         mgs_params_fsdb_cleanup(env, mgs);
1210         lproc_mgs_cleanup(mgs);
1211 err_llog:
1212         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1213         if (ctxt) {
1214                 ctxt->loc_dir = NULL;
1215                 llog_cleanup(env, ctxt);
1216         }
1217 err_tgt:
1218         tgt_fini(env, &mgs->mgs_lut);
1219 err_fs:
1220         /* No extra cleanup needed for llog_init_commit_thread() */
1221         mgs_fs_cleanup(env, mgs);
1222 err_ns:
1223         ldlm_namespace_free(obd->obd_namespace, NULL, 0);
1224         obd->obd_namespace = NULL;
1225 err_ops:
1226         lu_site_purge(env, mgs2lu_dev(mgs)->ld_site, ~0);
1227         if (!cfs_hash_is_empty(mgs2lu_dev(mgs)->ld_site->ls_obj_hash)) {
1228                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1229                 lu_site_print(env, mgs2lu_dev(mgs)->ld_site, &msgdata,
1230                                 lu_cdebug_printer);
1231         }
1232         obd_disconnect(mgs->mgs_bottom_exp);
1233 err_lmi:
1234         if (lmi)
1235                 server_put_mount(lustre_cfg_string(lcfg, 0), true);
1236         RETURN(rc);
1237 }
1238
1239 static struct lu_device *mgs_device_free(const struct lu_env *env,
1240                                          struct lu_device *lu)
1241 {
1242         struct mgs_device *mgs = lu2mgs_dev(lu);
1243         ENTRY;
1244
1245         dt_device_fini(&mgs->mgs_dt_dev);
1246         OBD_FREE_PTR(mgs);
1247         RETURN(NULL);
1248 }
1249
1250 static int mgs_process_config(const struct lu_env *env,
1251                               struct lu_device *dev,
1252                               struct lustre_cfg *lcfg)
1253 {
1254         LBUG();
1255         return 0;
1256 }
1257
1258 static int mgs_object_init(const struct lu_env *env, struct lu_object *o,
1259                            const struct lu_object_conf *unused)
1260 {
1261         struct mgs_device *d = lu2mgs_dev(o->lo_dev);
1262         struct lu_device  *under;
1263         struct lu_object  *below;
1264         int                rc = 0;
1265         ENTRY;
1266
1267         /* do no set .do_ops as mgs calls to bottom osd directly */
1268
1269         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
1270                         PFID(lu_object_fid(o)));
1271
1272         under = &d->mgs_bottom->dd_lu_dev;
1273         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
1274         if (below != NULL)
1275                 lu_object_add(o, below);
1276         else
1277                 rc = -ENOMEM;
1278
1279         return 0;
1280 }
1281
1282 static void mgs_object_free(const struct lu_env *env, struct lu_object *o)
1283 {
1284         struct mgs_object *obj = lu2mgs_obj(o);
1285         struct lu_object_header *h = o->lo_header;
1286
1287         dt_object_fini(&obj->mgo_obj);
1288         lu_object_header_fini(h);
1289         OBD_FREE_PTR(obj);
1290 }
1291
1292 static int mgs_object_print(const struct lu_env *env, void *cookie,
1293                             lu_printer_t p, const struct lu_object *l)
1294 {
1295         const struct mgs_object *o = lu2mgs_obj((struct lu_object *) l);
1296
1297         return (*p)(env, cookie, LUSTRE_MGS_NAME"-object@%p", o);
1298 }
1299
1300 struct lu_object_operations mgs_lu_obj_ops = {
1301         .loo_object_init        = mgs_object_init,
1302         .loo_object_free        = mgs_object_free,
1303         .loo_object_print       = mgs_object_print,
1304 };
1305
1306 struct lu_object *mgs_object_alloc(const struct lu_env *env,
1307                                    const struct lu_object_header *hdr,
1308                                    struct lu_device *d)
1309 {
1310         struct lu_object_header *h;
1311         struct mgs_object       *o;
1312         struct lu_object        *l;
1313
1314         LASSERT(hdr == NULL);
1315
1316         OBD_ALLOC_PTR(o);
1317         if (o != NULL) {
1318                 l = &o->mgo_obj.do_lu;
1319                 h = &o->mgo_header;
1320
1321                 lu_object_header_init(h);
1322                 dt_object_init(&o->mgo_obj, h, d);
1323                 lu_object_add_top(h, l);
1324
1325                 l->lo_ops = &mgs_lu_obj_ops;
1326
1327                 return l;
1328         } else {
1329                 return NULL;
1330         }
1331 }
1332
1333 const struct lu_device_operations mgs_lu_ops = {
1334         .ldo_object_alloc       = mgs_object_alloc,
1335         .ldo_process_config     = mgs_process_config,
1336 };
1337
1338 static struct lu_device *mgs_device_alloc(const struct lu_env *env,
1339                                           struct lu_device_type *type,
1340                                           struct lustre_cfg *lcfg)
1341 {
1342         struct mgs_device *mgs;
1343         struct lu_device  *ludev;
1344
1345         OBD_ALLOC_PTR(mgs);
1346         if (mgs == NULL) {
1347                 ludev = ERR_PTR(-ENOMEM);
1348         } else {
1349                 int rc;
1350
1351                 ludev = mgs2lu_dev(mgs);
1352                 dt_device_init(&mgs->mgs_dt_dev, type);
1353                 rc = mgs_init0(env, mgs, type, lcfg);
1354                 if (rc != 0) {
1355                         mgs_device_free(env, ludev);
1356                         ludev = ERR_PTR(rc);
1357                 }
1358         }
1359         return ludev;
1360 }
1361
1362 static struct lu_device *mgs_device_fini(const struct lu_env *env,
1363                                          struct lu_device *d)
1364 {
1365         struct mgs_device       *mgs = lu2mgs_dev(d);
1366         struct obd_device       *obd = mgs->mgs_obd;
1367         struct llog_ctxt        *ctxt;
1368
1369         ENTRY;
1370
1371         LASSERT(mgs->mgs_bottom);
1372
1373         class_disconnect_exports(obd);
1374
1375         ping_evictor_stop();
1376
1377         ptlrpc_unregister_service(mgs->mgs_service);
1378
1379         obd_exports_barrier(obd);
1380         obd_zombie_barrier();
1381
1382         tgt_fini(env, &mgs->mgs_lut);
1383         mgs_params_fsdb_cleanup(env, mgs);
1384         mgs_cleanup_fsdb_list(mgs);
1385         lproc_mgs_cleanup(mgs);
1386
1387         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1388         if (ctxt) {
1389                 ctxt->loc_dir = NULL;
1390                 llog_cleanup(env, ctxt);
1391         }
1392
1393         mgs_fs_cleanup(env, mgs);
1394
1395         ldlm_namespace_free(obd->obd_namespace, NULL, 1);
1396         obd->obd_namespace = NULL;
1397
1398         lu_site_purge(env, d->ld_site, ~0);
1399         if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
1400                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1401                 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
1402         }
1403
1404         LASSERT(mgs->mgs_bottom_exp);
1405         obd_disconnect(mgs->mgs_bottom_exp);
1406
1407         server_put_mount(obd->obd_name, true);
1408
1409         RETURN(NULL);
1410 }
1411
1412 /* context key constructor/destructor: mgs_key_init, mgs_key_fini */
1413 LU_KEY_INIT_FINI(mgs, struct mgs_thread_info);
1414
1415 LU_TYPE_INIT_FINI(mgs, &mgs_thread_key);
1416
1417 LU_CONTEXT_KEY_DEFINE(mgs, LCT_MG_THREAD);
1418
1419 static struct lu_device_type_operations mgs_device_type_ops = {
1420         .ldto_init              = mgs_type_init,
1421         .ldto_fini              = mgs_type_fini,
1422
1423         .ldto_start             = mgs_type_start,
1424         .ldto_stop              = mgs_type_stop,
1425
1426         .ldto_device_alloc      = mgs_device_alloc,
1427         .ldto_device_free       = mgs_device_free,
1428
1429         .ldto_device_fini       = mgs_device_fini
1430 };
1431
1432 static struct lu_device_type mgs_device_type = {
1433         .ldt_tags       = LU_DEVICE_DT,
1434         .ldt_name       = LUSTRE_MGS_NAME,
1435         .ldt_ops        = &mgs_device_type_ops,
1436         .ldt_ctx_tags   = LCT_MG_THREAD
1437 };
1438
1439 static int mgs_obd_connect(const struct lu_env *env, struct obd_export **exp,
1440                            struct obd_device *obd, struct obd_uuid *cluuid,
1441                            struct obd_connect_data *data, void *localdata)
1442 {
1443         struct obd_export       *lexp;
1444         struct lustre_handle     conn = { 0 };
1445         int                      rc;
1446
1447         ENTRY;
1448
1449         if (exp == NULL || obd == NULL || cluuid == NULL)
1450                 RETURN(-EINVAL);
1451
1452         rc = class_connect(&conn, obd, cluuid);
1453         if (rc)
1454                 RETURN(rc);
1455
1456         lexp = class_conn2export(&conn);
1457         if (lexp == NULL)
1458                 RETURN(-EFAULT);
1459
1460         if (data != NULL) {
1461                 data->ocd_connect_flags &= MGS_CONNECT_SUPPORTED;
1462                 data->ocd_version = LUSTRE_VERSION_CODE;
1463                 lexp->exp_connect_data = *data;
1464         }
1465
1466         tgt_counter_incr(lexp, LPROC_MGS_CONNECT);
1467
1468         rc = mgs_export_stats_init(obd, lexp, localdata);
1469         if (rc)
1470                 class_disconnect(lexp);
1471         else
1472                 *exp = lexp;
1473
1474         RETURN(rc);
1475 }
1476
1477 static int mgs_obd_reconnect(const struct lu_env *env, struct obd_export *exp,
1478                              struct obd_device *obd, struct obd_uuid *cluuid,
1479                              struct obd_connect_data *data, void *localdata)
1480 {
1481         ENTRY;
1482
1483         if (exp == NULL || obd == NULL || cluuid == NULL)
1484                 RETURN(-EINVAL);
1485
1486         tgt_counter_incr(exp, LPROC_MGS_CONNECT);
1487
1488         if (data != NULL) {
1489                 data->ocd_connect_flags &= MGS_CONNECT_SUPPORTED;
1490                 data->ocd_version = LUSTRE_VERSION_CODE;
1491                 exp->exp_connect_data = *data;
1492         }
1493
1494         RETURN(mgs_export_stats_init(obd, exp, localdata));
1495 }
1496
1497 static int mgs_obd_disconnect(struct obd_export *exp)
1498 {
1499         int rc;
1500
1501         ENTRY;
1502
1503         LASSERT(exp);
1504
1505         mgs_fsc_cleanup(exp);
1506
1507         class_export_get(exp);
1508         tgt_counter_incr(exp, LPROC_MGS_DISCONNECT);
1509
1510         rc = server_disconnect_export(exp);
1511         class_export_put(exp);
1512         RETURN(rc);
1513 }
1514
1515 /* use obd ops to offer management infrastructure */
1516 static struct obd_ops mgs_obd_device_ops = {
1517         .o_owner                = THIS_MODULE,
1518         .o_connect              = mgs_obd_connect,
1519         .o_reconnect            = mgs_obd_reconnect,
1520         .o_disconnect           = mgs_obd_disconnect,
1521         .o_init_export          = mgs_init_export,
1522         .o_destroy_export       = mgs_destroy_export,
1523         .o_iocontrol            = mgs_iocontrol,
1524 };
1525
1526 static int __init mgs_init(void)
1527 {
1528         return class_register_type(&mgs_obd_device_ops, NULL, true, NULL,
1529 #ifndef HAVE_ONLY_PROCFS_SEQ
1530                                    NULL,
1531 #endif
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);