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