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