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