Whamcloud - gitweb
LU-1710 lvb: variable sized LVB support
[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) 2011, 2012, Whamcloud, Inc.
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_param.h>
49
50 #include "mgs_internal.h"
51
52 /* Establish a connection to the MGS.*/
53 static int mgs_connect(const struct lu_env *env,
54                        struct obd_export **exp, struct obd_device *obd,
55                        struct obd_uuid *cluuid, struct obd_connect_data *data,
56                        void *localdata)
57 {
58         struct obd_export *lexp;
59         struct lustre_handle conn = { 0 };
60         int rc;
61         ENTRY;
62
63         if (!exp || !obd || !cluuid)
64                 RETURN(-EINVAL);
65
66         rc = class_connect(&conn, obd, cluuid);
67         if (rc)
68                 RETURN(rc);
69
70         lexp = class_conn2export(&conn);
71         if (lexp == NULL)
72                 GOTO(out, rc = -EFAULT);
73
74         mgs_counter_incr(lexp, LPROC_MGS_CONNECT);
75
76         if (data != NULL) {
77                 data->ocd_connect_flags &= MGS_CONNECT_SUPPORTED;
78                 lexp->exp_connect_flags = data->ocd_connect_flags;
79                 data->ocd_version = LUSTRE_VERSION_CODE;
80         }
81
82         rc = mgs_export_stats_init(obd, lexp, localdata);
83
84 out:
85         if (rc) {
86                 class_disconnect(lexp);
87         } else {
88                 *exp = lexp;
89         }
90
91         RETURN(rc);
92 }
93
94 static int mgs_reconnect(const struct lu_env *env,
95                          struct obd_export *exp, struct obd_device *obd,
96                          struct obd_uuid *cluuid, struct obd_connect_data *data,
97                          void *localdata)
98 {
99         ENTRY;
100
101         if (exp == NULL || obd == NULL || cluuid == NULL)
102                 RETURN(-EINVAL);
103
104         mgs_counter_incr(exp, LPROC_MGS_CONNECT);
105
106         if (data != NULL) {
107                 data->ocd_connect_flags &= MGS_CONNECT_SUPPORTED;
108                 exp->exp_connect_flags = data->ocd_connect_flags;
109                 data->ocd_version = LUSTRE_VERSION_CODE;
110         }
111
112         RETURN(mgs_export_stats_init(obd, exp, localdata));
113 }
114
115 static int mgs_disconnect(struct obd_export *exp)
116 {
117         int rc;
118         ENTRY;
119
120         LASSERT(exp);
121
122         mgs_fsc_cleanup(exp);
123
124         class_export_get(exp);
125         mgs_counter_incr(exp, LPROC_MGS_DISCONNECT);
126
127         rc = server_disconnect_export(exp);
128         class_export_put(exp);
129         RETURN(rc);
130 }
131
132 static int mgs_handle(struct ptlrpc_request *req);
133
134 static int mgs_completion_ast_config(struct ldlm_lock *lock, __u64 flags,
135                                      void *cbdata)
136 {
137         ENTRY;
138
139         if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
140                        LDLM_FL_BLOCK_CONV))) {
141                 struct fs_db *fsdb = (struct fs_db *)lock->l_ast_data;
142                 struct lustre_handle lockh;
143
144                 /* clear the bit before lock put */
145                 clear_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags);
146
147                 ldlm_lock2handle(lock, &lockh);
148                 ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
149         }
150
151         RETURN(ldlm_completion_ast(lock, flags, cbdata));
152 }
153
154 static int mgs_completion_ast_ir(struct ldlm_lock *lock, __u64 flags,
155                                  void *cbdata)
156 {
157         ENTRY;
158
159         if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
160                        LDLM_FL_BLOCK_CONV))) {
161                 struct fs_db *fsdb;
162
163                 /* l_ast_data is used as a marker to avoid cancel ldlm lock
164                  * twice. See LU-1259. */
165                 lock_res_and_lock(lock);
166                 fsdb = (struct fs_db *)lock->l_ast_data;
167                 lock->l_ast_data = NULL;
168                 unlock_res_and_lock(lock);
169
170                 if (fsdb != NULL) {
171                         struct lustre_handle lockh;
172
173                         mgs_ir_notify_complete(fsdb);
174
175                         ldlm_lock2handle(lock, &lockh);
176                         ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
177                 }
178         }
179
180         RETURN(ldlm_completion_ast(lock, flags, cbdata));
181 }
182
183 void mgs_revoke_lock(struct mgs_device *mgs, struct fs_db *fsdb, int type)
184 {
185         ldlm_completion_callback cp = NULL;
186         struct lustre_handle     lockh = { 0 };
187         struct ldlm_res_id       res_id;
188         __u64 flags = LDLM_FL_ATOMIC_CB;
189         int rc;
190         ENTRY;
191
192         LASSERT(fsdb->fsdb_name[0] != '\0');
193         rc = mgc_fsname2resid(fsdb->fsdb_name, &res_id, type);
194         LASSERT(rc == 0);
195
196         switch (type) {
197         case CONFIG_T_CONFIG:
198                 cp = mgs_completion_ast_config;
199                 if (test_and_set_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags))
200                         rc = -EALREADY;
201                 break;
202         case CONFIG_T_RECOVER:
203                 cp = mgs_completion_ast_ir;
204         default:
205                 break;
206         }
207
208         if (!rc) {
209                 LASSERT(cp != NULL);
210                 rc = ldlm_cli_enqueue_local(mgs->mgs_obd->obd_namespace,
211                                             &res_id, LDLM_PLAIN, NULL, LCK_EX,
212                                             &flags, ldlm_blocking_ast, cp,
213                                             NULL, fsdb, 0, LVB_T_NONE, NULL,
214                                             &lockh);
215                 if (rc != ELDLM_OK) {
216                         CERROR("can't take cfg lock for "LPX64"/"LPX64"(%d)\n",
217                                le64_to_cpu(res_id.name[0]),
218                                le64_to_cpu(res_id.name[1]), rc);
219
220                         if (type == CONFIG_T_CONFIG)
221                                 clear_bit(FSDB_REVOKING_LOCK,
222                                               &fsdb->fsdb_flags);
223                 }
224                 /* lock has been cancelled in completion_ast. */
225         }
226
227         RETURN_EXIT;
228 }
229
230 /* rc=0 means ok
231       1 means update
232      <0 means error */
233 static int mgs_check_target(const struct lu_env *env,
234                             struct mgs_device *mgs,
235                             struct mgs_target_info *mti)
236 {
237         int rc;
238         ENTRY;
239
240         rc = mgs_check_index(env, mgs, mti);
241         if (rc == 0) {
242                 LCONSOLE_ERROR_MSG(0x13b, "%s claims to have registered, but "
243                                    "this MGS does not know about it, preventing "
244                                    "registration.\n", mti->mti_svname);
245                 rc = -ENOENT;
246         } else if (rc == -1) {
247                 LCONSOLE_ERROR_MSG(0x13c, "Client log %s-client has "
248                                    "disappeared! Regenerating all logs.\n",
249                                    mti->mti_fsname);
250                 mti->mti_flags |= LDD_F_WRITECONF;
251                 rc = 1;
252         } else {
253                 /* Index is correctly marked as used */
254
255                 /* If the logs don't contain the mti_nids then add
256                    them as failover nids */
257                 rc = mgs_check_failnid(env, mgs, mti);
258         }
259
260         RETURN(rc);
261 }
262
263 /* Ensure this is not a failover node that is connecting first*/
264 static int mgs_check_failover_reg(struct mgs_target_info *mti)
265 {
266         lnet_nid_t nid;
267         char *ptr;
268         int i;
269
270         ptr = mti->mti_params;
271         while (class_find_param(ptr, PARAM_FAILNODE, &ptr) == 0) {
272                 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
273                         for (i = 0; i < mti->mti_nid_count; i++) {
274                                 if (nid == mti->mti_nids[i]) {
275                                         LCONSOLE_WARN("Denying initial registra"
276                                                       "tion attempt from nid %s"
277                                                       ", specified as failover"
278                                                       "\n",libcfs_nid2str(nid));
279                                         return -EADDRNOTAVAIL;
280                                 }
281                         }
282                 }
283         }
284         return 0;
285 }
286
287 /* Called whenever a target starts up.  Flags indicate first connect, etc. */
288 static int mgs_handle_target_reg(struct ptlrpc_request *req)
289 {
290         struct obd_device *obd = req->rq_export->exp_obd;
291         struct mgs_device *mgs = exp2mgs_dev(req->rq_export);
292         struct lu_env     *env = req->rq_svc_thread->t_env;
293         struct mgs_target_info *mti, *rep_mti;
294         struct fs_db *fsdb;
295         int opc;
296         int rc = 0;
297         ENTRY;
298
299         mgs_counter_incr(req->rq_export, LPROC_MGS_TARGET_REG);
300
301         mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
302
303         opc = mti->mti_flags & LDD_F_OPC_MASK;
304         if (opc == LDD_F_OPC_READY) {
305                 CDEBUG(D_MGS, "fs: %s index: %d is ready to reconnect.\n",
306                        mti->mti_fsname, mti->mti_stripe_index);
307                 rc = mgs_ir_update(env, mgs, mti);
308                 if (rc) {
309                         LASSERT(!(mti->mti_flags & LDD_F_IR_CAPABLE));
310                         CERROR("Update IR return with %d(ignore and IR "
311                                "disabled)\n", rc);
312                 }
313                 GOTO(out_nolock, rc);
314         }
315
316         /* Do not support unregistering right now. */
317         if (opc != LDD_F_OPC_REG)
318                 GOTO(out_nolock, rc = -EINVAL);
319
320         CDEBUG(D_MGS, "fs: %s index: %d is registered to MGS.\n",
321                mti->mti_fsname, mti->mti_stripe_index);
322
323         if (mti->mti_flags & LDD_F_NEED_INDEX)
324                 mti->mti_flags |= LDD_F_WRITECONF;
325
326         if (!(mti->mti_flags & (LDD_F_WRITECONF | LDD_F_UPGRADE14 |
327                                 LDD_F_UPDATE))) {
328                 /* We're just here as a startup ping. */
329                 CDEBUG(D_MGS, "Server %s is running on %s\n",
330                        mti->mti_svname, obd_export_nid2str(req->rq_export));
331                 rc = mgs_check_target(env, mgs, mti);
332                 /* above will set appropriate mti flags */
333                 if (rc <= 0)
334                         /* Nothing wrong, or fatal error */
335                         GOTO(out_nolock, rc);
336         } else if (!(mti->mti_flags & LDD_F_NO_PRIMNODE)) {
337                 rc = mgs_check_failover_reg(mti);
338                 if (rc)
339                         GOTO(out_nolock, rc);
340         }
341
342         OBD_FAIL_TIMEOUT(OBD_FAIL_MGS_PAUSE_TARGET_REG, 10);
343
344         if (mti->mti_flags & LDD_F_WRITECONF) {
345                 if (mti->mti_flags & LDD_F_SV_TYPE_MDT &&
346                     mti->mti_stripe_index == 0) {
347                         rc = mgs_erase_logs(env, mgs, mti->mti_fsname);
348                         LCONSOLE_WARN("%s: Logs for fs %s were removed by user "
349                                       "request.  All servers must be restarted "
350                                       "in order to regenerate the logs."
351                                       "\n", obd->obd_name, mti->mti_fsname);
352                 } else if (mti->mti_flags &
353                            (LDD_F_SV_TYPE_OST | LDD_F_SV_TYPE_MDT)) {
354                         rc = mgs_erase_log(env, mgs, mti->mti_svname);
355                         LCONSOLE_WARN("%s: Regenerating %s log by user "
356                                       "request.\n",
357                                       obd->obd_name, mti->mti_svname);
358                 }
359                 mti->mti_flags |= LDD_F_UPDATE;
360                 /* Erased logs means start from scratch. */
361                 mti->mti_flags &= ~LDD_F_UPGRADE14;
362                 if (rc)
363                         GOTO(out_nolock, rc);
364         }
365
366         rc = mgs_find_or_make_fsdb(env, mgs, mti->mti_fsname, &fsdb);
367         if (rc) {
368                 CERROR("Can't get db for %s: %d\n", mti->mti_fsname, rc);
369                 GOTO(out_nolock, rc);
370         }
371
372         /*
373          * Log writing contention is handled by the fsdb_mutex.
374          *
375          * It should be alright if someone was reading while we were
376          * updating the logs - if we revoke at the end they will just update
377          * from where they left off.
378          */
379
380         if (mti->mti_flags & LDD_F_UPGRADE14) {
381                 CERROR("Can't upgrade from 1.4 (%d)\n", rc);
382                 GOTO(out, rc);
383         }
384
385         if (mti->mti_flags & LDD_F_UPDATE) {
386                 CDEBUG(D_MGS, "updating %s, index=%d\n", mti->mti_svname,
387                        mti->mti_stripe_index);
388
389                 /* create or update the target log
390                    and update the client/mdt logs */
391                 rc = mgs_write_log_target(env, mgs, mti, fsdb);
392                 if (rc) {
393                         CERROR("Failed to write %s log (%d)\n",
394                                mti->mti_svname, rc);
395                         GOTO(out, rc);
396                 }
397
398                 mti->mti_flags &= ~(LDD_F_VIRGIN | LDD_F_UPDATE |
399                                     LDD_F_NEED_INDEX | LDD_F_WRITECONF |
400                                     LDD_F_UPGRADE14);
401                 mti->mti_flags |= LDD_F_REWRITE_LDD;
402         }
403
404 out:
405         mgs_revoke_lock(mgs, fsdb, CONFIG_T_CONFIG);
406
407 out_nolock:
408         CDEBUG(D_MGS, "replying with %s, index=%d, rc=%d\n", mti->mti_svname,
409                mti->mti_stripe_index, rc);
410         req->rq_status = rc;
411         if (rc)
412                 /* we need an error flag to tell the target what's going on,
413                  * instead of just doing it by error code only. */
414                 mti->mti_flags |= LDD_F_ERROR;
415
416         rc = req_capsule_server_pack(&req->rq_pill);
417         if (rc)
418                 RETURN(rc);
419
420         /* send back the whole mti in the reply */
421         rep_mti = req_capsule_server_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
422         *rep_mti = *mti;
423
424         /* Flush logs to disk */
425         dt_sync(req->rq_svc_thread->t_env, mgs->mgs_bottom);
426         RETURN(rc);
427 }
428
429 static int mgs_set_info_rpc(struct ptlrpc_request *req)
430 {
431         struct mgs_device *mgs = exp2mgs_dev(req->rq_export);
432         struct lu_env     *env = req->rq_svc_thread->t_env;
433         struct mgs_send_param *msp, *rep_msp;
434         struct mgs_thread_info *mgi = mgs_env_info(env);
435         int rc;
436         struct lustre_cfg *lcfg;
437         ENTRY;
438
439         msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
440         LASSERT(msp);
441
442         /* Construct lustre_cfg structure to pass to function mgs_setparam */
443         lustre_cfg_bufs_reset(&mgi->mgi_bufs, NULL);
444         lustre_cfg_bufs_set_string(&mgi->mgi_bufs, 1, msp->mgs_param);
445         lcfg = lustre_cfg_new(LCFG_PARAM, &mgi->mgi_bufs);
446         if (IS_ERR(lcfg))
447                 GOTO(out, rc = PTR_ERR(lcfg));
448         rc = mgs_setparam(env, mgs, lcfg, mgi->mgi_fsname);
449         if (rc) {
450                 CERROR("Error %d in setting the parameter %s for fs %s\n",
451                        rc, msp->mgs_param, mgi->mgi_fsname);
452                 GOTO(out_cfg, rc);
453         }
454
455         rc = req_capsule_server_pack(&req->rq_pill);
456         if (rc == 0) {
457                 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
458                 rep_msp = msp;
459         }
460 out_cfg:
461         lustre_cfg_free(lcfg);
462 out:
463         RETURN(rc);
464 }
465
466 static int mgs_config_read(struct ptlrpc_request *req)
467 {
468         struct mgs_config_body *body;
469         int rc;
470         ENTRY;
471
472         body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
473         if (body == NULL)
474                 RETURN(-EINVAL);
475
476         switch (body->mcb_type) {
477         case CONFIG_T_RECOVER:
478                 rc = mgs_get_ir_logs(req);
479                 break;
480
481         case CONFIG_T_CONFIG:
482                 rc = -ENOTSUPP;
483                 break;
484
485         default:
486                 rc = -EINVAL;
487                 break;
488         }
489
490         RETURN(rc);
491 }
492
493 /*
494  * similar as in ost_connect_check_sptlrpc()
495  */
496 static int mgs_connect_check_sptlrpc(struct ptlrpc_request *req)
497 {
498         struct obd_export     *exp = req->rq_export;
499         struct mgs_device     *mgs = exp2mgs_dev(req->rq_export);
500         struct lu_env         *env = req->rq_svc_thread->t_env;
501         struct fs_db          *fsdb;
502         struct sptlrpc_flavor  flvr;
503         int                    rc = 0;
504
505         if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
506                 rc = mgs_find_or_make_fsdb(env, mgs, MGSSELF_NAME, &fsdb);
507                 if (rc)
508                         return rc;
509
510                 mutex_lock(&fsdb->fsdb_mutex);
511                 if (sptlrpc_rule_set_choose(&fsdb->fsdb_srpc_gen,
512                                             LUSTRE_SP_MGC, LUSTRE_SP_MGS,
513                                             req->rq_peer.nid,
514                                             &flvr) == 0) {
515                         /* by defualt allow any flavors */
516                         flvr.sf_rpc = SPTLRPC_FLVR_ANY;
517                 }
518                 mutex_unlock(&fsdb->fsdb_mutex);
519
520                 spin_lock(&exp->exp_lock);
521
522                 exp->exp_sp_peer = req->rq_sp_from;
523                 exp->exp_flvr = flvr;
524
525                 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
526                     exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
527                         CERROR("invalid rpc flavor %x, expect %x, from %s\n",
528                                req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
529                                libcfs_nid2str(req->rq_peer.nid));
530                         rc = -EACCES;
531                 }
532
533                 spin_unlock(&exp->exp_lock);
534         } else {
535                 if (exp->exp_sp_peer != req->rq_sp_from) {
536                         CERROR("RPC source %s doesn't match %s\n",
537                                sptlrpc_part2name(req->rq_sp_from),
538                                sptlrpc_part2name(exp->exp_sp_peer));
539                         rc = -EACCES;
540                 } else {
541                         rc = sptlrpc_target_export_check(exp, req);
542                 }
543         }
544
545         return rc;
546 }
547
548 /* Called whenever a target cleans up. */
549 /* XXX - Currently unused */
550 static int mgs_handle_target_del(struct ptlrpc_request *req)
551 {
552         ENTRY;
553         mgs_counter_incr(req->rq_export, LPROC_MGS_TARGET_DEL);
554         RETURN(0);
555 }
556
557 /* XXX - Currently unused */
558 static int mgs_handle_exception(struct ptlrpc_request *req)
559 {
560         ENTRY;
561         mgs_counter_incr(req->rq_export, LPROC_MGS_EXCEPTION);
562         RETURN(0);
563 }
564
565 /*
566  * For old clients there is no direct way of knowing which filesystems
567  * a client is operating at the MGS side. But we need to pick up those
568  * clients so that the MGS can mark the corresponding filesystem as
569  * non-IR capable because old clients are not ready to be notified.
570  *
571  * This is why we have this _hack_ function. We detect the filesystem's
572  * name by hacking llog operation which is currently used by the clients
573  * to fetch configuration logs. At present this is fine because this is
574  * the ONLY llog operation between mgc and the MGS.
575  *
576  * If extra llog operation is going to be added, this function needs fixing.
577  *
578  * If releases prior than 2.0 are not supported, we can remove this function.
579  */
580 static int mgs_handle_fslog_hack(struct ptlrpc_request *req)
581 {
582         char *logname;
583         char fsname[16];
584         char *ptr;
585         int rc;
586
587         /* XXX: We suppose that llog at mgs is only used for
588          * fetching file system log */
589         logname = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
590         if (logname == NULL) {
591                 CERROR("No logname, is llog on MGS used for something else?\n");
592                 return -EINVAL;
593         }
594
595         ptr = strchr(logname, '-');
596         rc = (int)(ptr - logname);
597         if (ptr == NULL || rc >= sizeof(fsname)) {
598                 CERROR("Invalid logname received: %s\n", logname);
599                 return -EINVAL;
600         }
601
602         strncpy(fsname, logname, rc);
603         fsname[rc] = 0;
604         rc = mgs_fsc_attach(req->rq_svc_thread->t_env, req->rq_export, fsname);
605         if (rc < 0 && rc != -EEXIST)
606                 CERROR("add fs client %s returns %d\n", fsname, rc);
607
608         return rc;
609 }
610
611 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
612 int mgs_handle(struct ptlrpc_request *req)
613 {
614         int fail = OBD_FAIL_MGS_ALL_REPLY_NET;
615         int opc, rc = 0;
616         ENTRY;
617
618         req_capsule_init(&req->rq_pill, req, RCL_SERVER);
619         CFS_FAIL_TIMEOUT_MS(OBD_FAIL_MGS_PAUSE_REQ, cfs_fail_val);
620         if (CFS_FAIL_CHECK(OBD_FAIL_MGS_ALL_REQUEST_NET))
621                 RETURN(0);
622
623         LASSERT(current->journal_info == NULL);
624         opc = lustre_msg_get_opc(req->rq_reqmsg);
625
626         if (opc == SEC_CTX_INIT ||
627             opc == SEC_CTX_INIT_CONT ||
628             opc == SEC_CTX_FINI)
629                 GOTO(out, rc = 0);
630
631         if (opc != MGS_CONNECT) {
632                 if (!class_connected_export(req->rq_export)) {
633                         DEBUG_REQ(D_MGS, req, "operation on unconnected MGS\n");
634                         req->rq_status = -ENOTCONN;
635                         GOTO(out, rc = -ENOTCONN);
636                 }
637         }
638
639         switch (opc) {
640         case MGS_CONNECT:
641                 DEBUG_REQ(D_MGS, req, "connect");
642                 /* MGS and MDS have same request format for connect */
643                 req_capsule_set(&req->rq_pill, &RQF_MDS_CONNECT);
644                 rc = target_handle_connect(req);
645                 if (rc == 0)
646                         rc = mgs_connect_check_sptlrpc(req);
647
648                 if (!rc && (lustre_msg_get_conn_cnt(req->rq_reqmsg) > 1))
649                         /* Make clients trying to reconnect after a MGS restart
650                            happy; also requires obd_replayable */
651                         lustre_msg_add_op_flags(req->rq_repmsg,
652                                                 MSG_CONNECT_RECONNECT);
653                 break;
654         case MGS_DISCONNECT:
655                 DEBUG_REQ(D_MGS, req, "disconnect");
656                 /* MGS and MDS have same request format for disconnect */
657                 req_capsule_set(&req->rq_pill, &RQF_MDS_DISCONNECT);
658                 rc = target_handle_disconnect(req);
659                 req->rq_status = rc;            /* superfluous? */
660                 break;
661         case MGS_EXCEPTION:
662                 DEBUG_REQ(D_MGS, req, "exception");
663                 rc = mgs_handle_exception(req);
664                 break;
665         case MGS_TARGET_REG:
666                 DEBUG_REQ(D_MGS, req, "target add");
667                 req_capsule_set(&req->rq_pill, &RQF_MGS_TARGET_REG);
668                 rc = mgs_handle_target_reg(req);
669                 break;
670         case MGS_TARGET_DEL:
671                 DEBUG_REQ(D_MGS, req, "target del");
672                 rc = mgs_handle_target_del(req);
673                 break;
674         case MGS_SET_INFO:
675                 DEBUG_REQ(D_MGS, req, "set_info");
676                 req_capsule_set(&req->rq_pill, &RQF_MGS_SET_INFO);
677                 rc = mgs_set_info_rpc(req);
678                 break;
679         case MGS_CONFIG_READ:
680                 DEBUG_REQ(D_MGS, req, "read config");
681                 req_capsule_set(&req->rq_pill, &RQF_MGS_CONFIG_READ);
682                 rc = mgs_config_read(req);
683                 break;
684         case LDLM_ENQUEUE:
685                 DEBUG_REQ(D_MGS, req, "enqueue");
686                 req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
687                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
688                                          ldlm_server_blocking_ast, NULL);
689                 break;
690         case LDLM_BL_CALLBACK:
691         case LDLM_CP_CALLBACK:
692                 DEBUG_REQ(D_MGS, req, "callback");
693                 CERROR("callbacks should not happen on MGS\n");
694                 LBUG();
695                 break;
696
697         case OBD_PING:
698                 DEBUG_REQ(D_INFO, req, "ping");
699                 req_capsule_set(&req->rq_pill, &RQF_OBD_PING);
700                 rc = target_handle_ping(req);
701                 break;
702         case OBD_LOG_CANCEL:
703                 DEBUG_REQ(D_MGS, req, "log cancel");
704                 rc = -ENOTSUPP; /* la la la */
705                 break;
706
707         case LLOG_ORIGIN_HANDLE_CREATE:
708                 DEBUG_REQ(D_MGS, req, "llog_open");
709                 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
710                 rc = llog_origin_handle_open(req);
711                 if (rc == 0)
712                         (void)mgs_handle_fslog_hack(req);
713                 break;
714         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
715                 DEBUG_REQ(D_MGS, req, "llog next block");
716                 req_capsule_set(&req->rq_pill,
717                                 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
718                 rc = llog_origin_handle_next_block(req);
719                 break;
720         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
721                 DEBUG_REQ(D_MGS, req, "llog prev block");
722                 req_capsule_set(&req->rq_pill,
723                                 &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
724                 rc = llog_origin_handle_prev_block(req);
725                 break;
726         case LLOG_ORIGIN_HANDLE_READ_HEADER:
727                 DEBUG_REQ(D_MGS, req, "llog read header");
728                 req_capsule_set(&req->rq_pill,
729                                 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
730                 rc = llog_origin_handle_read_header(req);
731                 break;
732         case LLOG_ORIGIN_HANDLE_CLOSE:
733                 DEBUG_REQ(D_MGS, req, "llog close");
734                 rc = llog_origin_handle_close(req);
735                 break;
736         default:
737                 rc = -EOPNOTSUPP;
738         }
739
740         LASSERT(current->journal_info == NULL);
741         if (rc) {
742                 DEBUG_REQ(D_MGS, req, "MGS fail to handle opc = %d: rc = %d\n",
743                           opc, rc);
744                 req->rq_status = rc;
745                 rc = ptlrpc_error(req);
746                 RETURN(rc);
747         }
748 out:
749         target_send_reply(req, rc, fail);
750         RETURN(0);
751 }
752
753 static inline int mgs_init_export(struct obd_export *exp)
754 {
755         struct mgs_export_data *data = &exp->u.eu_mgs_data;
756
757         /* init mgs_export_data for fsc */
758         spin_lock_init(&data->med_lock);
759         CFS_INIT_LIST_HEAD(&data->med_clients);
760
761         spin_lock(&exp->exp_lock);
762         exp->exp_connecting = 1;
763         spin_unlock(&exp->exp_lock);
764
765         /* self-export doesn't need client data and ldlm initialization */
766         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
767                                      &exp->exp_client_uuid)))
768                 return 0;
769         return ldlm_init_export(exp);
770 }
771
772 static inline int mgs_destroy_export(struct obd_export *exp)
773 {
774         ENTRY;
775
776         target_destroy_export(exp);
777         mgs_client_free(exp);
778
779         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
780                                      &exp->exp_client_uuid)))
781                 RETURN(0);
782
783         ldlm_destroy_export(exp);
784
785         RETURN(0);
786 }
787
788 static int mgs_extract_fs_pool(char * arg, char *fsname, char *poolname)
789 {
790         char *ptr;
791
792         ENTRY;
793         for (ptr = arg;  (*ptr != '\0') && (*ptr != '.'); ptr++ ) {
794                 *fsname = *ptr;
795                 fsname++;
796         }
797         if (*ptr == '\0')
798                 return -EINVAL;
799         *fsname = '\0';
800         ptr++;
801         strcpy(poolname, ptr);
802
803         RETURN(0);
804 }
805
806 static int mgs_iocontrol_pool(const struct lu_env *env,
807                               struct mgs_device *mgs,
808                               struct obd_ioctl_data *data)
809 {
810         struct mgs_thread_info *mgi = mgs_env_info(env);
811         int rc;
812         struct lustre_cfg *lcfg = NULL;
813         char *poolname = NULL;
814         ENTRY;
815
816         OBD_ALLOC(poolname, LOV_MAXPOOLNAME + 1);
817         if (poolname == NULL)
818                 RETURN(-ENOMEM);
819
820         if (data->ioc_type != LUSTRE_CFG_TYPE) {
821                 CERROR("%s: unknown cfg record type: %d\n",
822                        mgs->mgs_obd->obd_name, data->ioc_type);
823                 GOTO(out_pool, rc = -EINVAL);
824         }
825
826         if (data->ioc_plen1 > CFS_PAGE_SIZE)
827                 GOTO(out_pool, rc = -E2BIG);
828
829         OBD_ALLOC(lcfg, data->ioc_plen1);
830         if (lcfg == NULL)
831                 GOTO(out_pool, rc = -ENOMEM);
832
833         if (cfs_copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
834                 GOTO(out_lcfg, rc = -EFAULT);
835
836         if (lcfg->lcfg_bufcount < 2)
837                 GOTO(out_lcfg, rc = -EFAULT);
838
839         /* first arg is always <fsname>.<poolname> */
840         rc = mgs_extract_fs_pool(lustre_cfg_string(lcfg, 1), mgi->mgi_fsname,
841                                  poolname);
842         if (rc)
843                 GOTO(out_lcfg, rc);
844
845         switch (lcfg->lcfg_command) {
846         case LCFG_POOL_NEW:
847                 if (lcfg->lcfg_bufcount != 2)
848                         GOTO(out_lcfg, rc = -EINVAL);
849                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_NEW, mgi->mgi_fsname,
850                                   poolname, NULL);
851                 break;
852         case LCFG_POOL_ADD:
853                 if (lcfg->lcfg_bufcount != 3)
854                         GOTO(out_lcfg, rc = -EINVAL);
855                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_ADD, mgi->mgi_fsname,
856                                   poolname, lustre_cfg_string(lcfg, 2));
857                 break;
858         case LCFG_POOL_REM:
859                 if (lcfg->lcfg_bufcount != 3)
860                         GOTO(out_lcfg, rc = -EINVAL);
861                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_REM, mgi->mgi_fsname,
862                                   poolname, lustre_cfg_string(lcfg, 2));
863                 break;
864         case LCFG_POOL_DEL:
865                 if (lcfg->lcfg_bufcount != 2)
866                         GOTO(out_lcfg, rc = -EINVAL);
867                 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_DEL, mgi->mgi_fsname,
868                                   poolname, NULL);
869                 break;
870         default:
871                  rc = -EINVAL;
872         }
873
874         if (rc) {
875                 CERROR("OBD_IOC_POOL err %d, cmd %X for pool %s.%s\n",
876                        rc, lcfg->lcfg_command, mgi->mgi_fsname, poolname);
877                 GOTO(out_lcfg, rc);
878         }
879
880 out_lcfg:
881         OBD_FREE(lcfg, data->ioc_plen1);
882 out_pool:
883         OBD_FREE(poolname, LOV_MAXPOOLNAME + 1);
884         RETURN(rc);
885 }
886
887 /* from mdt_iocontrol */
888 int mgs_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
889                   void *karg, void *uarg)
890 {
891         struct mgs_device *mgs = exp2mgs_dev(exp);
892         struct obd_ioctl_data *data = karg;
893         struct lu_env env;
894         int rc = 0;
895
896         ENTRY;
897         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
898
899         rc = lu_env_init(&env, LCT_MG_THREAD);
900         if (rc)
901                 RETURN(rc);
902
903         switch (cmd) {
904
905         case OBD_IOC_PARAM: {
906                 struct mgs_thread_info *mgi = mgs_env_info(&env);
907                 struct lustre_cfg *lcfg;
908
909                 if (data->ioc_type != LUSTRE_CFG_TYPE) {
910                         CERROR("%s: unknown cfg record type: %d\n",
911                                mgs->mgs_obd->obd_name, data->ioc_type);
912                         GOTO(out, rc = -EINVAL);
913                 }
914
915                 OBD_ALLOC(lcfg, data->ioc_plen1);
916                 if (lcfg == NULL)
917                         GOTO(out, rc = -ENOMEM);
918                 if (cfs_copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
919                         GOTO(out_free, rc = -EFAULT);
920
921                 if (lcfg->lcfg_bufcount < 1)
922                         GOTO(out_free, rc = -EINVAL);
923
924                 rc = mgs_setparam(&env, mgs, lcfg, mgi->mgi_fsname);
925                 if (rc)
926                         CERROR("%s: setparam err: rc = %d\n",
927                                exp->exp_obd->obd_name, rc);
928 out_free:
929                 OBD_FREE(lcfg, data->ioc_plen1);
930                 break;
931         }
932
933         case OBD_IOC_POOL:
934                 rc = mgs_iocontrol_pool(&env, mgs, data);
935                 break;
936
937         case OBD_IOC_DUMP_LOG: {
938                 struct llog_ctxt *ctxt;
939
940                 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
941                 rc = class_config_dump_llog(&env, ctxt, data->ioc_inlbuf1,
942                                             NULL);
943                 llog_ctxt_put(ctxt);
944
945                 break;
946         }
947
948         case OBD_IOC_LLOG_CANCEL:
949         case OBD_IOC_LLOG_REMOVE:
950         case OBD_IOC_LLOG_CHECK:
951         case OBD_IOC_LLOG_INFO:
952         case OBD_IOC_LLOG_PRINT: {
953                 struct llog_ctxt *ctxt;
954
955                 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
956                 rc = llog_ioctl(&env, ctxt, cmd, data);
957                 llog_ctxt_put(ctxt);
958                 break;
959         }
960
961         default:
962                 CERROR("%s: unknown command %#x\n",
963                        mgs->mgs_obd->obd_name,  cmd);
964                 rc = -ENOTTY;
965                 break;
966         }
967 out:
968         lu_env_fini(&env);
969         RETURN(rc);
970 }
971
972 static int mgs_connect_to_osd(struct mgs_device *m, const char *nextdev)
973 {
974         struct obd_connect_data *data = NULL;
975         struct obd_device       *obd;
976         int                      rc;
977         ENTRY;
978
979         OBD_ALLOC_PTR(data);
980         if (data == NULL)
981                 RETURN(-ENOMEM);
982
983         obd = class_name2obd(nextdev);
984         if (obd == NULL) {
985                 CERROR("can't locate next device: %s\n", nextdev);
986                 GOTO(out, rc = -ENOTCONN);
987         }
988
989         data->ocd_version = LUSTRE_VERSION_CODE;
990
991         rc = obd_connect(NULL, &m->mgs_bottom_exp, obd,
992                          &obd->obd_uuid, data, NULL);
993         if (rc) {
994                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
995                 GOTO(out, rc);
996         }
997
998         m->mgs_bottom = lu2dt_dev(m->mgs_bottom_exp->exp_obd->obd_lu_dev);
999         m->mgs_dt_dev.dd_lu_dev.ld_site = m->mgs_bottom->dd_lu_dev.ld_site;
1000         LASSERT(m->mgs_dt_dev.dd_lu_dev.ld_site);
1001 out:
1002         OBD_FREE_PTR(data);
1003         RETURN(rc);
1004 }
1005
1006
1007 static int mgs_init0(const struct lu_env *env, struct mgs_device *mgs,
1008                      struct lu_device_type *ldt, struct lustre_cfg *lcfg)
1009 {
1010         static struct ptlrpc_service_conf        conf;
1011         struct lprocfs_static_vars               lvars = { 0 };
1012         struct obd_device                       *obd;
1013         struct lustre_mount_info                *lmi;
1014         struct llog_ctxt                        *ctxt;
1015         int                                      rc;
1016
1017         ENTRY;
1018
1019         lmi = server_get_mount(lustre_cfg_string(lcfg, 0));
1020         if (lmi == NULL)
1021                 RETURN(-ENODEV);
1022
1023         mgs->mgs_dt_dev.dd_lu_dev.ld_ops = &mgs_lu_ops;
1024
1025         rc = mgs_connect_to_osd(mgs, lustre_cfg_string(lcfg, 3));
1026         if (rc)
1027                 RETURN(rc);
1028
1029         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1030         LASSERT(obd);
1031         mgs->mgs_obd = obd;
1032         mgs->mgs_obd->obd_lu_dev = &mgs->mgs_dt_dev.dd_lu_dev;
1033
1034         obd->u.obt.obt_magic = OBT_MAGIC;
1035         obd->u.obt.obt_instance = 0;
1036
1037         /* namespace for mgs llog */
1038         obd->obd_namespace = ldlm_namespace_new(obd ,"MGS",
1039                                                 LDLM_NAMESPACE_SERVER,
1040                                                 LDLM_NAMESPACE_MODEST,
1041                                                 LDLM_NS_TYPE_MGT);
1042         if (obd->obd_namespace == NULL)
1043                 GOTO(err_ops, rc = -ENOMEM);
1044
1045         /* ldlm setup */
1046         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1047                            "mgs_ldlm_client", &obd->obd_ldlm_client);
1048
1049         rc = mgs_fs_setup(env, mgs);
1050         if (rc) {
1051                 CERROR("%s: MGS filesystem method init failed: rc = %d\n",
1052                        obd->obd_name, rc);
1053                 GOTO(err_ns, rc);
1054         }
1055
1056         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT,
1057                         obd, &llog_osd_ops);
1058         if (rc)
1059                 GOTO(err_fs, rc);
1060
1061         /* XXX: we need this trick till N:1 stack is supported
1062          * set "current" directory for named llogs */
1063         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1064         LASSERT(ctxt);
1065         ctxt->loc_dir = mgs->mgs_configs_dir;
1066         llog_ctxt_put(ctxt);
1067
1068         /* No recovery for MGC's */
1069         obd->obd_replayable = 0;
1070
1071         /* Internal mgs setup */
1072         mgs_init_fsdb_list(mgs);
1073         mutex_init(&mgs->mgs_mutex);
1074         mgs->mgs_start_time = cfs_time_current_sec();
1075         spin_lock_init(&mgs->mgs_lock);
1076
1077         /* Setup proc */
1078         lprocfs_mgs_init_vars(&lvars);
1079         if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0) {
1080                 lproc_mgs_setup(mgs, lustre_cfg_string(lcfg, 3));
1081                 rc = lprocfs_alloc_md_stats(obd, LPROC_MGS_LAST);
1082                 if (rc)
1083                         GOTO(err_llog, rc);
1084         }
1085
1086         conf = (typeof(conf)) {
1087                 .psc_name               = LUSTRE_MGS_NAME,
1088                 .psc_watchdog_factor    = MGS_SERVICE_WATCHDOG_FACTOR,
1089                 .psc_buf                = {
1090                         .bc_nbufs               = MGS_NBUFS,
1091                         .bc_buf_size            = MGS_BUFSIZE,
1092                         .bc_req_max_size        = MGS_MAXREQSIZE,
1093                         .bc_rep_max_size        = MGS_MAXREPSIZE,
1094                         .bc_req_portal          = MGS_REQUEST_PORTAL,
1095                         .bc_rep_portal          = MGC_REPLY_PORTAL,
1096                 },
1097                 .psc_thr                = {
1098                         .tc_thr_name            = "ll_mgs",
1099                         .tc_nthrs_init          = MGS_NTHRS_INIT,
1100                         .tc_nthrs_max           = MGS_NTHRS_MAX,
1101                         .tc_ctx_tags            = LCT_MG_THREAD,
1102                 },
1103                 .psc_ops                = {
1104                         .so_req_handler         = mgs_handle,
1105                         .so_req_printer         = target_print_req,
1106                 },
1107         };
1108         /* Start the service threads */
1109         mgs->mgs_service = ptlrpc_register_service(&conf, obd->obd_proc_entry);
1110         if (IS_ERR(mgs->mgs_service)) {
1111                 rc = PTR_ERR(mgs->mgs_service);
1112                 CERROR("failed to start service: %d\n", rc);
1113                 GOTO(err_lproc, rc);
1114         }
1115
1116         ping_evictor_start();
1117
1118         CDEBUG(D_INFO, "MGS %s started\n", obd->obd_name);
1119
1120         /* device stack is not yet fully setup to keep no objects behind */
1121         lu_site_purge(env, mgs2lu_dev(mgs)->ld_site, ~0);
1122         RETURN(0);
1123
1124 err_lproc:
1125         lproc_mgs_cleanup(mgs);
1126 err_llog:
1127         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1128         if (ctxt) {
1129                 ctxt->loc_dir = NULL;
1130                 llog_cleanup(env, ctxt);
1131         }
1132 err_fs:
1133         /* No extra cleanup needed for llog_init_commit_thread() */
1134         mgs_fs_cleanup(env, mgs);
1135 err_ns:
1136         ldlm_namespace_free(obd->obd_namespace, NULL, 0);
1137         obd->obd_namespace = NULL;
1138 err_ops:
1139         lu_site_purge(env, mgs2lu_dev(mgs)->ld_site, ~0);
1140         if (!cfs_hash_is_empty(mgs2lu_dev(mgs)->ld_site->ls_obj_hash)) {
1141                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1142                 lu_site_print(env, mgs2lu_dev(mgs)->ld_site, &msgdata,
1143                                 lu_cdebug_printer);
1144         }
1145         obd_disconnect(mgs->mgs_bottom_exp);
1146         RETURN(rc);
1147 }
1148
1149 static struct lu_device *mgs_device_free(const struct lu_env *env,
1150                                          struct lu_device *lu)
1151 {
1152         struct mgs_device *mgs = lu2mgs_dev(lu);
1153         ENTRY;
1154
1155         dt_device_fini(&mgs->mgs_dt_dev);
1156         OBD_FREE_PTR(mgs);
1157         RETURN(NULL);
1158 }
1159
1160 static int mgs_process_config(const struct lu_env *env,
1161                               struct lu_device *dev,
1162                               struct lustre_cfg *lcfg)
1163 {
1164         LBUG();
1165         return 0;
1166 }
1167
1168 static int mgs_object_init(const struct lu_env *env, struct lu_object *o,
1169                            const struct lu_object_conf *unused)
1170 {
1171         struct mgs_device *d = lu2mgs_dev(o->lo_dev);
1172         struct lu_device  *under;
1173         struct lu_object  *below;
1174         int                rc = 0;
1175         ENTRY;
1176
1177         /* do no set .do_ops as mgs calls to bottom osd directly */
1178
1179         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
1180                         PFID(lu_object_fid(o)));
1181
1182         under = &d->mgs_bottom->dd_lu_dev;
1183         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
1184         if (below != NULL)
1185                 lu_object_add(o, below);
1186         else
1187                 rc = -ENOMEM;
1188
1189         return 0;
1190 }
1191
1192 static void mgs_object_free(const struct lu_env *env, struct lu_object *o)
1193 {
1194         struct mgs_object *obj = lu2mgs_obj(o);
1195         struct lu_object_header *h = o->lo_header;
1196
1197         dt_object_fini(&obj->mgo_obj);
1198         lu_object_header_fini(h);
1199         OBD_FREE_PTR(obj);
1200 }
1201
1202 static int mgs_object_print(const struct lu_env *env, void *cookie,
1203                             lu_printer_t p, const struct lu_object *l)
1204 {
1205         const struct mgs_object *o = lu2mgs_obj((struct lu_object *) l);
1206
1207         return (*p)(env, cookie, LUSTRE_MGS_NAME"-object@%p", o);
1208 }
1209
1210 struct lu_object_operations mgs_lu_obj_ops = {
1211         .loo_object_init        = mgs_object_init,
1212         .loo_object_free        = mgs_object_free,
1213         .loo_object_print       = mgs_object_print,
1214 };
1215
1216 struct lu_object *mgs_object_alloc(const struct lu_env *env,
1217                                    const struct lu_object_header *hdr,
1218                                    struct lu_device *d)
1219 {
1220         struct lu_object_header *h;
1221         struct mgs_object       *o;
1222         struct lu_object        *l;
1223
1224         LASSERT(hdr == NULL);
1225
1226         OBD_ALLOC_PTR(o);
1227         if (o != NULL) {
1228                 l = &o->mgo_obj.do_lu;
1229                 h = &o->mgo_header;
1230
1231                 lu_object_header_init(h);
1232                 dt_object_init(&o->mgo_obj, h, d);
1233                 lu_object_add_top(h, l);
1234
1235                 l->lo_ops = &mgs_lu_obj_ops;
1236
1237                 return l;
1238         } else {
1239                 return NULL;
1240         }
1241 }
1242
1243 const struct lu_device_operations mgs_lu_ops = {
1244         .ldo_object_alloc       = mgs_object_alloc,
1245         .ldo_process_config     = mgs_process_config,
1246 };
1247
1248 static struct lu_device *mgs_device_alloc(const struct lu_env *env,
1249                                           struct lu_device_type *type,
1250                                           struct lustre_cfg *lcfg)
1251 {
1252         struct mgs_device *mgs;
1253         struct lu_device  *ludev;
1254
1255         OBD_ALLOC_PTR(mgs);
1256         if (mgs == NULL) {
1257                 ludev = ERR_PTR(-ENOMEM);
1258         } else {
1259                 int rc;
1260
1261                 ludev = mgs2lu_dev(mgs);
1262                 dt_device_init(&mgs->mgs_dt_dev, type);
1263                 rc = mgs_init0(env, mgs, type, lcfg);
1264                 if (rc != 0) {
1265                         mgs_device_free(env, ludev);
1266                         ludev = ERR_PTR(rc);
1267                 }
1268         }
1269         return ludev;
1270 }
1271
1272 static struct lu_device *mgs_device_fini(const struct lu_env *env,
1273                                          struct lu_device *d)
1274 {
1275         struct mgs_device       *mgs = lu2mgs_dev(d);
1276         struct obd_device       *obd = mgs->mgs_obd;
1277         struct llog_ctxt        *ctxt;
1278
1279         ENTRY;
1280
1281         LASSERT(mgs->mgs_bottom);
1282
1283         ping_evictor_stop();
1284
1285         ptlrpc_unregister_service(mgs->mgs_service);
1286
1287         obd_exports_barrier(obd);
1288         obd_zombie_barrier();
1289
1290         mgs_cleanup_fsdb_list(mgs);
1291         lproc_mgs_cleanup(mgs);
1292
1293         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1294         if (ctxt) {
1295                 ctxt->loc_dir = NULL;
1296                 llog_cleanup(env, ctxt);
1297         }
1298
1299         mgs_fs_cleanup(env, mgs);
1300
1301         ldlm_namespace_free(obd->obd_namespace, NULL, 1);
1302         obd->obd_namespace = NULL;
1303
1304         lu_site_purge(env, d->ld_site, ~0);
1305         if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
1306                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1307                 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
1308         }
1309
1310         LASSERT(mgs->mgs_bottom_exp);
1311         obd_disconnect(mgs->mgs_bottom_exp);
1312
1313         server_put_mount(obd->obd_name, NULL);
1314
1315         RETURN(NULL);
1316 }
1317
1318 /* context key constructor/destructor: mgs_key_init, mgs_key_fini */
1319 LU_KEY_INIT_FINI(mgs, struct mgs_thread_info);
1320
1321 LU_TYPE_INIT_FINI(mgs, &mgs_thread_key);
1322
1323 LU_CONTEXT_KEY_DEFINE(mgs, LCT_MG_THREAD);
1324
1325 static struct lu_device_type_operations mgs_device_type_ops = {
1326         .ldto_init              = mgs_type_init,
1327         .ldto_fini              = mgs_type_fini,
1328
1329         .ldto_start             = mgs_type_start,
1330         .ldto_stop              = mgs_type_stop,
1331
1332         .ldto_device_alloc      = mgs_device_alloc,
1333         .ldto_device_free       = mgs_device_free,
1334
1335         .ldto_device_fini       = mgs_device_fini
1336 };
1337
1338 static struct lu_device_type mgs_device_type = {
1339         .ldt_tags       = LU_DEVICE_DT,
1340         .ldt_name       = LUSTRE_MGS_NAME,
1341         .ldt_ops        = &mgs_device_type_ops,
1342         .ldt_ctx_tags   = LCT_MG_THREAD
1343 };
1344
1345
1346 /* use obd ops to offer management infrastructure */
1347 static struct obd_ops mgs_obd_ops = {
1348         .o_owner           = THIS_MODULE,
1349         .o_connect         = mgs_connect,
1350         .o_reconnect       = mgs_reconnect,
1351         .o_disconnect      = mgs_disconnect,
1352         .o_init_export     = mgs_init_export,
1353         .o_destroy_export  = mgs_destroy_export,
1354         .o_iocontrol       = mgs_iocontrol,
1355 };
1356
1357 static int __init mgs_init(void)
1358 {
1359         struct lprocfs_static_vars lvars;
1360
1361         lprocfs_mgs_init_vars(&lvars);
1362         class_register_type(&mgs_obd_ops, NULL, lvars.module_vars,
1363                             LUSTRE_MGS_NAME, &mgs_device_type);
1364
1365         return 0;
1366 }
1367
1368 static void /*__exit*/ mgs_exit(void)
1369 {
1370         class_unregister_type(LUSTRE_MGS_NAME);
1371 }
1372
1373 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1374 MODULE_DESCRIPTION("Lustre  Management Server (MGS)");
1375 MODULE_LICENSE("GPL");
1376
1377 module_init(mgs_init);
1378 module_exit(mgs_exit);