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