1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/mgc/mgc_request.c
5 * Lustre Management Client
7 * Copyright (C) 2006 Cluster File Systems, Inc.
8 * Author: Nathan Rutman <nathan@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 # define EXPORT_SYMTAB
30 #define DEBUG_SUBSYSTEM S_MGC
31 #define D_MGC D_CONFIG /*|D_WARNING*/
34 # include <linux/module.h>
35 # include <linux/pagemap.h>
36 # include <linux/miscdevice.h>
37 # include <linux/init.h>
39 # include <liblustre.h>
42 #include <obd_class.h>
43 #include <lustre_dlm.h>
44 #include <lustre_log.h>
45 #include <lustre_fsfilt.h>
46 #include <lustre_disk.h>
48 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id)
54 /* fsname is at most 8 chars long at the beginning of the logname
55 e.g. "lustre-MDT0001" or "lustre" */
56 name_end = strrchr(logname, '-');
58 len = name_end - logname;
60 len = strlen(logname);
62 CERROR("fsname too long: %s\n", logname);
66 CERROR("missing fsname: %s\n", logname);
69 memcpy(&resname, logname, len);
71 memset(res_id, 0, sizeof(*res_id));
73 /* Always use the same endianness for the resid */
74 res_id->name[0] = cpu_to_le64(resname);
75 CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.8s)\n", logname,
76 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
79 EXPORT_SYMBOL(mgc_logname2resid);
81 /********************** config llog list **********************/
82 static struct list_head config_llog_list = LIST_HEAD_INIT(config_llog_list);
83 static spinlock_t config_list_lock = SPIN_LOCK_UNLOCKED;
85 /* Take a reference to a config log */
86 static int config_log_get(struct config_llog_data *cld)
89 if (cld->cld_stopping)
91 atomic_inc(&cld->cld_refcount);
92 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
93 atomic_read(&cld->cld_refcount));
97 /* Drop a reference to a config log. When no longer referenced,
98 we can free the config log data */
99 static void config_log_put(struct config_llog_data *cld)
102 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
103 atomic_read(&cld->cld_refcount));
104 if (atomic_dec_and_test(&cld->cld_refcount)) {
105 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
106 class_export_put(cld->cld_mgcexp);
107 spin_lock(&config_list_lock);
108 list_del(&cld->cld_list_chain);
109 spin_unlock(&config_list_lock);
110 OBD_FREE(cld->cld_logname, strlen(cld->cld_logname) + 1);
111 if (cld->cld_cfg.cfg_instance != NULL)
112 OBD_FREE(cld->cld_cfg.cfg_instance,
113 strlen(cld->cld_cfg.cfg_instance) + 1);
114 OBD_FREE(cld, sizeof(*cld));
119 /* Find a config log by name */
120 static struct config_llog_data *config_log_find(char *logname,
121 struct config_llog_instance *cfg)
123 struct list_head *tmp;
124 struct config_llog_data *cld;
125 char *logid = logname;
126 int match_instance = 0;
129 if (cfg && cfg->cfg_instance) {
131 logid = cfg->cfg_instance;
134 CERROR("No log specified\n");
135 RETURN(ERR_PTR(-EINVAL));
138 spin_lock(&config_list_lock);
139 list_for_each(tmp, &config_llog_list) {
140 cld = list_entry(tmp, struct config_llog_data, cld_list_chain);
141 if (match_instance && cld->cld_cfg.cfg_instance &&
142 strcmp(logid, cld->cld_cfg.cfg_instance) == 0)
144 if (!match_instance &&
145 strcmp(logid, cld->cld_logname) == 0)
148 spin_unlock(&config_list_lock);
150 CDEBUG(D_CONFIG, "can't get log %s\n", logid);
151 RETURN(ERR_PTR(-ENOENT));
153 atomic_inc(&cld->cld_refcount);
154 spin_unlock(&config_list_lock);
158 /* Add this log to our list of active logs.
159 We have one active log per "mount" - client instance or servername.
160 Each instance may be at a different point in the log. */
161 static int config_log_add(char *logname, struct config_llog_instance *cfg,
162 struct super_block *sb)
164 struct config_llog_data *cld;
165 struct lustre_sb_info *lsi = s2lsi(sb);
169 CDEBUG(D_MGC, "adding config log %s:%s\n", logname, cfg->cfg_instance);
171 OBD_ALLOC(cld, sizeof(*cld));
174 OBD_ALLOC(cld->cld_logname, strlen(logname) + 1);
175 if (!cld->cld_logname) {
176 OBD_FREE(cld, sizeof(*cld));
179 strcpy(cld->cld_logname, logname);
181 cld->cld_cfg.cfg_last_idx = 0;
182 cld->cld_cfg.cfg_flags = 0;
183 cld->cld_cfg.cfg_sb = sb;
184 atomic_set(&cld->cld_refcount, 1);
186 /* Keep the mgc around until we are done */
187 cld->cld_mgcexp = class_export_get(lsi->lsi_mgc->obd_self_export);
189 if (cfg->cfg_instance != NULL) {
190 OBD_ALLOC(cld->cld_cfg.cfg_instance,
191 strlen(cfg->cfg_instance) + 1);
192 strcpy(cld->cld_cfg.cfg_instance, cfg->cfg_instance);
194 rc = mgc_logname2resid(logname, &cld->cld_resid);
195 spin_lock(&config_list_lock);
196 list_add(&cld->cld_list_chain, &config_llog_list);
197 spin_unlock(&config_list_lock);
207 /* Stop watching for updates on this log. */
208 static int config_log_end(char *logname, struct config_llog_instance *cfg)
210 struct config_llog_data *cld;
214 cld = config_log_find(logname, cfg);
216 RETURN(PTR_ERR(cld));
217 /* drop the ref from the find */
220 cld->cld_stopping = 1;
221 /* drop the start ref */
223 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
228 /* reenqueue any lost locks */
229 #define RQ_RUNNING 0x1
233 static int rq_state = 0;
234 static cfs_waitq_t rq_waitq;
236 static int mgc_process_log(struct obd_device *mgc,
237 struct config_llog_data *cld);
239 static int mgc_requeue_thread(void *data)
241 struct l_wait_info lwi_now, lwi_later;
242 struct config_llog_data *cld, *n;
243 char name[] = "ll_cfg_requeue";
247 ptlrpc_daemonize(name);
249 CDEBUG(D_MGC, "Starting requeue thread\n");
251 lwi_later = LWI_TIMEOUT(60 * HZ, NULL, NULL);
252 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP), &lwi_later);
254 /* Keep trying failed locks periodically */
255 spin_lock(&config_list_lock);
256 while (rq_state & (RQ_NOW | RQ_LATER)) {
257 /* Any new or requeued lostlocks will change the state */
258 rq_state &= ~(RQ_NOW | RQ_LATER);
259 spin_unlock(&config_list_lock);
261 /* Always wait a few seconds to allow the server who
262 caused the lock revocation to finish its setup, plus some
263 random so everyone doesn't try to reconnect at once. */
264 lwi_now = LWI_TIMEOUT(3 * HZ + (ll_rand() & 0xff) * (HZ / 100),
266 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi_now);
268 spin_lock(&config_list_lock);
269 list_for_each_entry_safe(cld, n, &config_llog_list,
271 spin_unlock(&config_list_lock);
272 if (cld->cld_lostlock) {
273 CDEBUG(D_MGC, "updating log %s\n",
275 cld->cld_lostlock = 0;
276 rc = mgc_process_log(cld->cld_mgcexp->exp_obd,
278 /* Whether we enqueued again or not in
279 mgc_process_log, we're done with the ref
280 from the old enqueue */
283 spin_lock(&config_list_lock);
285 spin_unlock(&config_list_lock);
287 /* Wait a bit to see if anyone else needs a requeue */
288 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
290 spin_lock(&config_list_lock);
292 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
293 rq_state &= ~RQ_RUNNING;
294 spin_unlock(&config_list_lock);
296 CDEBUG(D_MGC, "Ending requeue thread\n");
300 /* Add a cld to the list to requeue. Start the requeue thread if needed.
301 We are responsible for dropping the config log reference from here on out. */
302 static int mgc_requeue_add(struct config_llog_data *cld, int later)
306 CDEBUG(D_INFO, "log %s: requeue (l=%d r=%d sp=%d st=%x)\n",
307 cld->cld_logname, later, atomic_read(&cld->cld_refcount),
308 cld->cld_stopping, rq_state);
310 /* Hold lock for rq_state */
311 spin_lock(&config_list_lock);
312 cld->cld_lostlock = 1;
314 if (cld->cld_stopping || (rq_state & RQ_STOP)) {
315 spin_unlock(&config_list_lock);
320 if (!(rq_state & RQ_RUNNING)) {
321 LASSERT(rq_state == 0);
322 rq_state = RQ_RUNNING | (later ? RQ_LATER : RQ_NOW);
323 spin_unlock(&config_list_lock);
324 rc = cfs_kernel_thread(mgc_requeue_thread, 0,
325 CLONE_VM | CLONE_FILES);
327 CERROR("log %s: cannot start requeue thread (%d),"
328 "no more log updates!\n", cld->cld_logname, rc);
329 /* Drop the ref, since the rq thread won't */
330 cld->cld_lostlock = 0;
336 rq_state |= later ? RQ_LATER : RQ_NOW;
337 spin_unlock(&config_list_lock);
338 cfs_waitq_signal(&rq_waitq);
344 /********************** class fns **********************/
346 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb,
347 struct vfsmount *mnt)
349 struct lvfs_run_ctxt saved;
350 struct lustre_sb_info *lsi = s2lsi(sb);
351 struct client_obd *cli = &obd->u.cli;
352 struct dentry *dentry;
358 LASSERT(lsi->lsi_srv_mnt == mnt);
360 /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
361 down(&cli->cl_mgc_sem);
363 cleanup_group_info();
365 obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
366 if (IS_ERR(obd->obd_fsops)) {
367 up(&cli->cl_mgc_sem);
368 CERROR("No fstype %s rc=%ld\n", MT_STR(lsi->lsi_ldd),
369 PTR_ERR(obd->obd_fsops));
370 RETURN(PTR_ERR(obd->obd_fsops));
373 cli->cl_mgc_vfsmnt = mnt;
374 fsfilt_setup(obd, mnt->mnt_sb);
376 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
377 obd->obd_lvfs_ctxt.pwdmnt = mnt;
378 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
379 obd->obd_lvfs_ctxt.fs = get_ds();
381 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
382 dentry = lookup_one_len(MOUNT_CONFIGS_DIR, current->fs->pwd,
383 strlen(MOUNT_CONFIGS_DIR));
384 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
385 if (IS_ERR(dentry)) {
386 err = PTR_ERR(dentry);
387 CERROR("cannot lookup %s directory: rc = %d\n",
388 MOUNT_CONFIGS_DIR, err);
391 cli->cl_mgc_configs_dir = dentry;
393 /* We take an obd ref to insure that we can't get to mgc_cleanup
394 without calling mgc_fs_cleanup first. */
397 label = fsfilt_get_label(obd, mnt->mnt_sb);
399 CDEBUG(D_MGC, "MGC using disk labelled=%s\n", label);
401 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
405 fsfilt_put_ops(obd->obd_fsops);
406 obd->obd_fsops = NULL;
407 cli->cl_mgc_vfsmnt = NULL;
408 up(&cli->cl_mgc_sem);
412 static int mgc_fs_cleanup(struct obd_device *obd)
414 struct client_obd *cli = &obd->u.cli;
418 LASSERT(cli->cl_mgc_vfsmnt != NULL);
420 if (cli->cl_mgc_configs_dir != NULL) {
421 struct lvfs_run_ctxt saved;
422 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
423 l_dput(cli->cl_mgc_configs_dir);
424 cli->cl_mgc_configs_dir = NULL;
425 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
429 cli->cl_mgc_vfsmnt = NULL;
431 fsfilt_put_ops(obd->obd_fsops);
433 up(&cli->cl_mgc_sem);
438 static atomic_t mgc_count = ATOMIC_INIT(0);
439 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
445 case OBD_CLEANUP_EARLY:
447 case OBD_CLEANUP_EXPORTS:
448 if (atomic_dec_and_test(&mgc_count)) {
449 /* Kick the requeue waitq - cld's should all be
451 spin_lock(&config_list_lock);
453 spin_unlock(&config_list_lock);
454 cfs_waitq_signal(&rq_waitq);
457 case OBD_CLEANUP_SELF_EXP:
458 rc = obd_llog_finish(obd, 0);
460 CERROR("failed to cleanup llogging subsystems\n");
462 case OBD_CLEANUP_OBD:
468 static int mgc_cleanup(struct obd_device *obd)
470 struct client_obd *cli = &obd->u.cli;
474 LASSERT(cli->cl_mgc_vfsmnt == NULL);
476 /* COMPAT_146 - old config logs may have added profiles we don't
478 if (obd->obd_type->typ_refcnt <= 1)
479 /* Only for the last mgc */
480 class_del_profiles();
484 rc = client_obd_cleanup(obd);
488 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
495 rc = client_obd_setup(obd, lcfg);
497 GOTO(err_decref, rc);
499 rc = obd_llog_init(obd, NULL, obd, 0, NULL, NULL);
501 CERROR("failed to setup llogging subsystems\n");
502 GOTO(err_cleanup, rc);
505 spin_lock(&config_list_lock);
506 atomic_inc(&mgc_count);
507 if (atomic_read(&mgc_count) == 1) {
508 rq_state &= ~RQ_STOP;
509 cfs_waitq_init(&rq_waitq);
511 spin_unlock(&config_list_lock);
516 client_obd_cleanup(obd);
522 /* based on ll_mdc_blocking_ast */
523 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
524 void *data, int flag)
526 struct lustre_handle lockh;
527 struct config_llog_data *cld = (struct config_llog_data *)data;
532 case LDLM_CB_BLOCKING:
533 /* mgs wants the lock, give it up... */
534 LDLM_DEBUG(lock, "MGC blocking CB");
535 ldlm_lock2handle(lock, &lockh);
536 rc = ldlm_cli_cancel(&lockh);
538 case LDLM_CB_CANCELING: {
539 /* We've given up the lock, prepare ourselves to update. */
540 LDLM_DEBUG(lock, "MGC cancel CB");
542 CDEBUG(D_MGC, "Lock res "LPX64" (%.8s)\n",
543 lock->l_resource->lr_name.name[0],
544 (char *)&lock->l_resource->lr_name.name[0]);
547 CERROR("missing data, won't requeue\n");
550 /* Are we done with this log? */
551 if (cld->cld_stopping) {
552 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
557 /* Make sure not to re-enqueue when the mgc is stopping
558 (we get called from client_disconnect_export) */
559 if (!lock->l_conn_export ||
560 !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
561 CDEBUG(D_MGC, "log %s: disconnecting, won't requeue\n",
566 /* Did we fail to get the lock? */
567 if (lock->l_req_mode != lock->l_granted_mode) {
568 CDEBUG(D_MGC, "log %s: original grant failed, will "
569 "requeue later\n", cld->cld_logname);
570 /* Try to re-enqueue later */
571 rc = mgc_requeue_add(cld, 1);
575 rc = mgc_requeue_add(cld, 0);
584 CERROR("%s CB failed %d:\n", flag == LDLM_CB_BLOCKING ?
585 "blocking" : "cancel", rc);
586 LDLM_ERROR(lock, "MGC ast");
591 /* Send parameter to MGS*/
592 static int mgc_set_mgs_param(struct obd_export *exp,
593 struct mgs_send_param *msp)
595 struct ptlrpc_request *req;
596 struct mgs_send_param *req_msp, *rep_msp;
597 int size[] = { sizeof(struct ptlrpc_body), sizeof(*req_msp) };
598 int rep_size[] = { sizeof(struct ptlrpc_body), sizeof(*msp) };
602 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MGS_VERSION,
603 MGS_SET_INFO, 2, size, NULL);
607 req_msp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*req_msp));
611 memcpy(req_msp, msp, sizeof(*req_msp));
612 ptlrpc_req_set_repsize(req, 2, rep_size);
613 rc = ptlrpc_queue_wait(req);
615 rep_msp = lustre_swab_repbuf(req, REPLY_REC_OFF,
616 sizeof(*rep_msp), NULL);
617 memcpy(msp, rep_msp, sizeof(*rep_msp));
620 ptlrpc_req_finished(req);
625 /* Take a config lock so we can get cancel notifications */
626 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
627 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
628 int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
629 void *data, __u32 lvb_len, void *lvb_swabber,
630 struct lustre_handle *lockh)
632 struct config_llog_data *cld = (struct config_llog_data *)data;
633 struct ldlm_enqueue_info einfo = { type, mode, mgc_blocking_ast,
634 ldlm_completion_ast, NULL, data};
639 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
640 cld->cld_resid.name[0]);
642 /* We can only drop this config log ref when we drop the lock */
643 if (config_log_get(cld))
644 RETURN(ELDLM_LOCK_ABORTED);
646 /* We need a callback for every lockholder, so don't try to
647 ldlm_lock_match (see rev 1.1.2.11.2.47) */
649 rc = ldlm_cli_enqueue(exp, NULL, &einfo, &cld->cld_resid,
650 NULL, flags, NULL, 0, NULL, lockh, 0);
651 /* A failed enqueue should still call the mgc_blocking_ast,
652 where it will be requeued if needed ("grant failed"). */
657 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
658 __u32 mode, struct lustre_handle *lockh)
662 ldlm_lock_decref(lockh, mode);
668 static int mgc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
669 void *karg, void *uarg)
671 struct obd_device *obd = exp->exp_obd;
672 struct obd_ioctl_data *data = karg;
673 struct llog_ctxt *ctxt;
674 struct lvfs_run_ctxt saved;
678 if (!try_module_get(THIS_MODULE)) {
679 CERROR("Can't get module. Is it alive?");
683 /* REPLicator context */
684 case OBD_IOC_PARSE: {
685 CERROR("MGC parsing llog %s\n", data->ioc_inlbuf1);
686 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
687 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
691 case OBD_IOC_LLOG_INFO:
692 case OBD_IOC_LLOG_PRINT: {
693 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
694 rc = llog_ioctl(ctxt, cmd, data);
699 /* ORIGinator context */
700 case OBD_IOC_DUMP_LOG: {
701 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
702 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
703 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
704 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
711 CERROR("mgc_ioctl(): unrecognised ioctl %#x\n", cmd);
712 GOTO(out, rc = -ENOTTY);
715 module_put(THIS_MODULE);
721 /* Send target_reg message to MGS */
722 static int mgc_target_register(struct obd_export *exp,
723 struct mgs_target_info *mti)
725 struct ptlrpc_request *req;
726 struct mgs_target_info *req_mti, *rep_mti;
727 int size[] = { sizeof(struct ptlrpc_body), sizeof(*req_mti) };
728 int rep_size[] = { sizeof(struct ptlrpc_body), sizeof(*mti) };
732 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MGS_VERSION,
733 MGS_TARGET_REG, 2, size, NULL);
737 req_mti = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*req_mti));
740 memcpy(req_mti, mti, sizeof(*req_mti));
742 ptlrpc_req_set_repsize(req, 2, rep_size);
744 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
746 rc = ptlrpc_queue_wait(req);
748 rep_mti = lustre_swab_repbuf(req, REPLY_REC_OFF,
750 lustre_swab_mgs_target_info);
751 memcpy(mti, rep_mti, sizeof(*rep_mti));
752 CDEBUG(D_MGC, "register %s got index = %d\n",
753 mti->mti_svname, mti->mti_stripe_index);
755 ptlrpc_req_finished(req);
760 int mgc_reconnect_import(struct obd_import *imp)
762 /* Force a new connect attempt */
763 ptlrpc_invalidate_import(imp);
764 /* Do a fresh connect next time by zeroing the handle */
765 ptlrpc_disconnect_import(imp, 1);
766 /* Wait for all invalidate calls to finish */
767 if (atomic_read(&imp->imp_inval_count) > 0) {
769 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
770 rc = l_wait_event(imp->imp_recovery_waitq,
771 (atomic_read(&imp->imp_inval_count) == 0),
774 CERROR("Interrupted, inval=%d\n",
775 atomic_read(&imp->imp_inval_count));
778 /* Allow reconnect attempts */
779 imp->imp_obd->obd_no_recov = 0;
780 /* Remove 'invalid' flag */
781 ptlrpc_activate_import(imp);
782 /* Attempt a new connect */
783 ptlrpc_recover_import(imp, NULL);
787 int mgc_set_info_async(struct obd_export *exp, obd_count keylen,
788 void *key, obd_count vallen, void *val,
789 struct ptlrpc_request_set *set)
791 struct obd_import *imp = class_exp2cliimp(exp);
795 /* Try to "recover" the initial connection; i.e. retry */
796 if (KEY_IS(KEY_INIT_RECOV)) {
797 if (vallen != sizeof(int))
799 spin_lock(&imp->imp_lock);
800 imp->imp_initial_recov = *(int *)val;
801 spin_unlock(&imp->imp_lock);
802 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
803 exp->exp_obd->obd_name, imp->imp_initial_recov);
806 /* Turn off initial_recov after we try all backup servers once */
807 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
809 if (vallen != sizeof(int))
812 spin_lock(&imp->imp_lock);
813 imp->imp_initial_recov_bk = value > 0;
814 /* Even after the initial connection, give up all comms if
815 nobody answers the first time. */
816 imp->imp_recon_bk = 1;
817 spin_unlock(&imp->imp_lock);
818 CDEBUG(D_MGC, "InitRecov %s %d/%d:d%d:i%d:r%d:or%d:%s\n",
819 imp->imp_obd->obd_name, value, imp->imp_initial_recov,
820 imp->imp_deactive, imp->imp_invalid,
821 imp->imp_replayable, imp->imp_obd->obd_replayable,
822 ptlrpc_import_state_name(imp->imp_state));
823 /* Resurrect if we previously died */
824 if (imp->imp_invalid || value > 1)
825 mgc_reconnect_import(imp);
828 /* FIXME move this to mgc_process_config */
829 if (KEY_IS("register_target")) {
830 struct mgs_target_info *mti;
831 if (vallen != sizeof(struct mgs_target_info))
833 mti = (struct mgs_target_info *)val;
834 CDEBUG(D_MGC, "register_target %s %#x\n",
835 mti->mti_svname, mti->mti_flags);
836 rc = mgc_target_register(exp, mti);
839 if (KEY_IS("set_fs")) {
840 struct super_block *sb = (struct super_block *)val;
841 struct lustre_sb_info *lsi;
842 if (vallen != sizeof(struct super_block))
845 rc = mgc_fs_setup(exp->exp_obd, sb, lsi->lsi_srv_mnt);
847 CERROR("set_fs got %d\n", rc);
851 if (KEY_IS("clear_fs")) {
854 rc = mgc_fs_cleanup(exp->exp_obd);
856 CERROR("clear_fs got %d\n", rc);
860 if (KEY_IS(KEY_SET_INFO)) {
861 struct mgs_send_param *msp;
863 msp = (struct mgs_send_param *)val;
864 rc = mgc_set_mgs_param(exp, msp);
871 static int mgc_import_event(struct obd_device *obd,
872 struct obd_import *imp,
873 enum obd_import_event event)
877 LASSERT(imp->imp_obd == obd);
878 CDEBUG(D_MGC, "import event %#x\n", event);
881 case IMP_EVENT_DISCON:
882 /* MGC imports should not wait for recovery */
883 ptlrpc_invalidate_import(imp);
885 case IMP_EVENT_INACTIVE:
887 case IMP_EVENT_INVALIDATE: {
888 struct ldlm_namespace *ns = obd->obd_namespace;
889 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
892 case IMP_EVENT_ACTIVE:
893 LCONSOLE_WARN("%s: Reactivating import\n", obd->obd_name);
898 CERROR("Unknown import event %#x\n", event);
904 static int mgc_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
905 struct obd_device *tgt, int count,
906 struct llog_catid *logid, struct obd_uuid *uuid)
908 struct llog_ctxt *ctxt;
912 rc = llog_setup(obd, llogs, LLOG_CONFIG_ORIG_CTXT, tgt, 0, NULL,
917 rc = llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
920 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
921 ctxt->loc_imp = obd->u.cli.cl_import;
927 static int mgc_llog_finish(struct obd_device *obd, int count)
932 rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_REPL_CTXT));
933 rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
938 /* identical to mgs_log_is_empty */
939 static int mgc_llog_is_empty(struct obd_device *obd, struct llog_ctxt *ctxt,
942 struct lvfs_run_ctxt saved;
943 struct llog_handle *llh;
946 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
947 rc = llog_create(ctxt, &llh, NULL, name);
949 llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
950 rc = llog_get_size(llh);
953 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
954 /* header is record 1 */
958 static int mgc_copy_handler(struct llog_handle *llh, struct llog_rec_hdr *rec,
961 struct llog_rec_hdr local_rec = *rec;
962 struct llog_handle *local_llh = (struct llog_handle *)data;
963 char *cfg_buf = (char*) (rec + 1);
964 struct lustre_cfg *lcfg;
968 /* Append all records */
969 local_rec.lrh_len -= sizeof(*rec) + sizeof(struct llog_rec_tail);
970 rc = llog_write_rec(local_llh, &local_rec, NULL, 0,
971 (void *)cfg_buf, -1);
973 lcfg = (struct lustre_cfg *)cfg_buf;
974 CDEBUG(D_INFO, "idx=%d, rc=%d, len=%d, cmd %x %s %s\n",
975 rec->lrh_index, rc, rec->lrh_len, lcfg->lcfg_command,
976 lustre_cfg_string(lcfg, 0), lustre_cfg_string(lcfg, 1));
981 /* Copy a remote log locally */
982 static int mgc_copy_llog(struct obd_device *obd, struct llog_ctxt *rctxt,
983 struct llog_ctxt *lctxt, char *logname)
985 struct llog_handle *local_llh, *remote_llh;
986 struct obd_uuid *uuid;
991 /* Write new log to a temp name, then vfs_rename over logname
992 upon successful completion. */
994 OBD_ALLOC(temp_log, strlen(logname) + 1);
997 sprintf(temp_log, "%sT", logname);
999 /* Make sure there's no old temp log */
1000 rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1003 rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, NULL);
1006 rc = llog_destroy(local_llh);
1007 llog_free_handle(local_llh);
1011 /* open local log */
1012 rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1016 /* set the log header uuid for fun */
1017 OBD_ALLOC_PTR(uuid);
1018 obd_str2uuid(uuid, logname);
1019 rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, uuid);
1022 GOTO(out_closel, rc);
1024 /* open remote log */
1025 rc = llog_create(rctxt, &remote_llh, NULL, logname);
1027 GOTO(out_closel, rc);
1028 rc = llog_init_handle(remote_llh, LLOG_F_IS_PLAIN, NULL);
1030 GOTO(out_closer, rc);
1032 /* Copy remote log */
1033 rc = llog_process(remote_llh, mgc_copy_handler,(void *)local_llh, NULL);
1036 rc2 = llog_close(remote_llh);
1040 rc2 = llog_close(local_llh);
1044 /* We've copied the remote log to the local temp log, now
1045 replace the old local log with the temp log. */
1047 struct client_obd *cli = &obd->u.cli;
1049 LASSERT(cli->cl_mgc_configs_dir);
1050 rc = lustre_rename(cli->cl_mgc_configs_dir, temp_log, logname);
1052 CDEBUG(D_MGC, "Copied remote log %s (%d)\n", logname, rc);
1055 CERROR("Failed to copy remote log %s (%d)\n", logname, rc);
1056 OBD_FREE(temp_log, strlen(logname) + 1);
1060 DECLARE_MUTEX(llog_process_lock);
1062 /* Get a config log from the MGS and process it.
1063 This func is called for both clients and servers. */
1064 static int mgc_process_log(struct obd_device *mgc,
1065 struct config_llog_data *cld)
1067 struct llog_ctxt *ctxt, *lctxt;
1068 struct lustre_handle lockh;
1069 struct client_obd *cli = &mgc->u.cli;
1070 struct lvfs_run_ctxt saved;
1071 struct lustre_sb_info *lsi;
1072 int rc = 0, rcl, flags = 0, must_pop = 0;
1075 if (!cld || !cld->cld_cfg.cfg_sb) {
1076 /* This should never happen */
1077 CERROR("Missing cld, aborting log update\n");
1080 if (cld->cld_stopping)
1083 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PROCESS_LOG, 20);
1085 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1087 CDEBUG(D_MGC, "Process log %s:%s from %d\n", cld->cld_logname,
1088 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1090 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1092 CERROR("missing llog context\n");
1096 /* I don't want mutliple processes running process_log at once --
1097 sounds like badness. It actually might be fine, as long as
1098 we're not trying to update from the same log
1099 simultaneously (in which case we should use a per-log sem.) */
1100 down(&llog_process_lock);
1102 /* Get the cfg lock on the llog */
1103 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1104 LCK_CR, &flags, NULL, NULL, NULL,
1105 cld, 0, NULL, &lockh);
1107 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1109 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1111 /* Copy the setup log locally if we can. Don't mess around if we're
1112 running an MGS though (logs are already local). */
1113 if (lctxt && lsi && (lsi->lsi_flags & LSI_SERVER) &&
1114 (lsi->lsi_srv_mnt == cli->cl_mgc_vfsmnt) &&
1115 !IS_MGS(lsi->lsi_ldd)) {
1116 push_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1119 /* Only try to copy log if we have the lock. */
1120 rc = mgc_copy_llog(mgc, ctxt, lctxt, cld->cld_logname);
1122 if (mgc_llog_is_empty(mgc, lctxt, cld->cld_logname)) {
1123 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1124 "log %s and no local copy."
1125 "\n", cld->cld_logname);
1126 GOTO(out_pop, rc = -ENOTCONN);
1128 LCONSOLE_WARN("Failed to get MGS log %s, using "
1129 "local copy.\n", cld->cld_logname);
1131 /* Now, whether we copied or not, start using the local llog.
1132 If we failed to copy, we'll start using whatever the old
1137 /* logname and instance info should be the same, so use our
1138 copy of the instance for the update. The cfg_last_idx will
1140 rc = class_config_parse_llog(ctxt, cld->cld_logname, &cld->cld_cfg);
1144 pop_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1146 /* Now drop the lock so MGS can revoke it */
1148 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1151 CERROR("Can't drop cfg lock: %d\n", rcl);
1154 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1155 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1157 up(&llog_process_lock);
1162 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1164 struct lustre_cfg *lcfg = buf;
1169 switch(cmd = lcfg->lcfg_command) {
1170 case LCFG_LOV_ADD_OBD: {
1171 /* Add any new target, not just osts */
1172 struct mgs_target_info *mti;
1174 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1175 sizeof(struct mgs_target_info))
1176 GOTO(out, rc = -EINVAL);
1178 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1179 CDEBUG(D_MGC, "add_target %s %#x\n",
1180 mti->mti_svname, mti->mti_flags);
1181 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1184 case LCFG_LOV_DEL_OBD:
1185 /* Remove target from the fs? */
1187 CERROR("lov_del_obd unimplemented\n");
1190 case LCFG_LOG_START: {
1191 struct config_llog_data *cld;
1192 struct config_llog_instance *cfg;
1193 struct super_block *sb;
1194 char *logname = lustre_cfg_string(lcfg, 1);
1195 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1196 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1198 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1201 /* We're only called through here on the initial mount */
1202 rc = config_log_add(logname, cfg, sb);
1205 cld = config_log_find(logname, cfg);
1212 /* FIXME only set this for old logs! Right now this forces
1213 us to always skip the "inside markers" check */
1214 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1216 rc = mgc_process_log(obd, cld);
1217 config_log_put(cld);
1221 case LCFG_LOG_END: {
1222 struct config_llog_instance *cfg = NULL;
1223 char *logname = lustre_cfg_string(lcfg, 1);
1224 if (lcfg->lcfg_bufcount >= 2)
1225 cfg = (struct config_llog_instance *)lustre_cfg_buf(
1227 rc = config_log_end(logname, cfg);
1231 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1232 GOTO(out, rc = -EINVAL);
1240 struct obd_ops mgc_obd_ops = {
1241 .o_owner = THIS_MODULE,
1242 .o_setup = mgc_setup,
1243 .o_precleanup = mgc_precleanup,
1244 .o_cleanup = mgc_cleanup,
1245 .o_add_conn = client_import_add_conn,
1246 .o_del_conn = client_import_del_conn,
1247 .o_connect = client_connect_import,
1248 .o_disconnect = client_disconnect_export,
1249 //.o_enqueue = mgc_enqueue,
1250 .o_cancel = mgc_cancel,
1251 //.o_iocontrol = mgc_iocontrol,
1252 .o_set_info_async = mgc_set_info_async,
1253 .o_import_event = mgc_import_event,
1254 .o_llog_init = mgc_llog_init,
1255 .o_llog_finish = mgc_llog_finish,
1256 .o_process_config = mgc_process_config,
1259 int __init mgc_init(void)
1261 return class_register_type(&mgc_obd_ops, NULL, NULL,
1262 LUSTRE_MGC_NAME, NULL);
1266 static void /*__exit*/ mgc_exit(void)
1268 class_unregister_type(LUSTRE_MGC_NAME);
1271 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1272 MODULE_DESCRIPTION("Lustre Management Client");
1273 MODULE_LICENSE("GPL");
1275 module_init(mgc_init);
1276 module_exit(mgc_exit);