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 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id)
53 CERROR("name too long: %s\n", name);
57 CERROR("missing name: %s\n", name);
60 memcpy(&resname, name, len);
62 memset(res_id, 0, sizeof(*res_id));
64 /* Always use the same endianness for the resid */
65 res_id->name[0] = cpu_to_le64(resname);
66 CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.8s)\n", name,
67 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
71 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id)
73 /* fsname is at most 8 chars long, maybe contain "-".
74 * e.g. "lustre", "CFS-000" */
75 return mgc_name2resid(fsname, strlen(fsname), res_id);
77 EXPORT_SYMBOL(mgc_fsname2resid);
79 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id)
84 /* logname consists of "fsname-nodetype".
85 * e.g. "lustre-MDT0001", "CFS-000-client" */
86 name_end = strrchr(logname, '-');
88 len = name_end - logname;
89 return mgc_name2resid(logname, len, res_id);
92 /********************** config llog list **********************/
93 static struct list_head config_llog_list = LIST_HEAD_INIT(config_llog_list);
94 static spinlock_t config_list_lock = SPIN_LOCK_UNLOCKED;
96 /* Take a reference to a config log */
97 static int config_log_get(struct config_llog_data *cld)
100 if (cld->cld_stopping)
102 atomic_inc(&cld->cld_refcount);
103 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
104 atomic_read(&cld->cld_refcount));
108 /* Drop a reference to a config log. When no longer referenced,
109 we can free the config log data */
110 static void config_log_put(struct config_llog_data *cld)
113 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
114 atomic_read(&cld->cld_refcount));
115 if (atomic_dec_and_test(&cld->cld_refcount)) {
116 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
117 class_export_put(cld->cld_mgcexp);
118 spin_lock(&config_list_lock);
119 list_del(&cld->cld_list_chain);
120 spin_unlock(&config_list_lock);
121 OBD_FREE(cld->cld_logname, strlen(cld->cld_logname) + 1);
122 if (cld->cld_cfg.cfg_instance != NULL)
123 OBD_FREE(cld->cld_cfg.cfg_instance,
124 strlen(cld->cld_cfg.cfg_instance) + 1);
125 OBD_FREE(cld, sizeof(*cld));
130 /* Find a config log by name */
131 static struct config_llog_data *config_log_find(char *logname,
132 struct config_llog_instance *cfg)
134 struct list_head *tmp;
135 struct config_llog_data *cld;
136 char *logid = logname;
137 int match_instance = 0;
140 if (cfg && cfg->cfg_instance) {
142 logid = cfg->cfg_instance;
145 CERROR("No log specified\n");
146 RETURN(ERR_PTR(-EINVAL));
149 spin_lock(&config_list_lock);
150 list_for_each(tmp, &config_llog_list) {
151 cld = list_entry(tmp, struct config_llog_data, cld_list_chain);
152 if (match_instance && cld->cld_cfg.cfg_instance &&
153 strcmp(logid, cld->cld_cfg.cfg_instance) == 0)
155 if (!match_instance &&
156 strcmp(logid, cld->cld_logname) == 0)
159 spin_unlock(&config_list_lock);
161 CDEBUG(D_CONFIG, "can't get log %s\n", logid);
162 RETURN(ERR_PTR(-ENOENT));
164 atomic_inc(&cld->cld_refcount);
165 spin_unlock(&config_list_lock);
169 /* Add this log to our list of active logs.
170 We have one active log per "mount" - client instance or servername.
171 Each instance may be at a different point in the log. */
172 static int config_log_add(char *logname, struct config_llog_instance *cfg,
173 struct super_block *sb)
175 struct config_llog_data *cld;
176 struct lustre_sb_info *lsi = s2lsi(sb);
180 CDEBUG(D_MGC, "adding config log %s:%s\n", logname, cfg->cfg_instance);
182 OBD_ALLOC(cld, sizeof(*cld));
185 OBD_ALLOC(cld->cld_logname, strlen(logname) + 1);
186 if (!cld->cld_logname) {
187 OBD_FREE(cld, sizeof(*cld));
190 strcpy(cld->cld_logname, logname);
192 cld->cld_cfg.cfg_last_idx = 0;
193 cld->cld_cfg.cfg_flags = 0;
194 cld->cld_cfg.cfg_sb = sb;
195 atomic_set(&cld->cld_refcount, 1);
197 /* Keep the mgc around until we are done */
198 cld->cld_mgcexp = class_export_get(lsi->lsi_mgc->obd_self_export);
200 if (cfg->cfg_instance != NULL) {
201 OBD_ALLOC(cld->cld_cfg.cfg_instance,
202 strlen(cfg->cfg_instance) + 1);
203 strcpy(cld->cld_cfg.cfg_instance, cfg->cfg_instance);
205 rc = mgc_logname2resid(logname, &cld->cld_resid);
206 spin_lock(&config_list_lock);
207 list_add(&cld->cld_list_chain, &config_llog_list);
208 spin_unlock(&config_list_lock);
218 /* Stop watching for updates on this log. */
219 static int config_log_end(char *logname, struct config_llog_instance *cfg)
221 struct config_llog_data *cld;
225 cld = config_log_find(logname, cfg);
227 RETURN(PTR_ERR(cld));
228 /* drop the ref from the find */
231 cld->cld_stopping = 1;
232 /* drop the start ref */
234 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
239 /* reenqueue any lost locks */
240 #define RQ_RUNNING 0x1
244 static int rq_state = 0;
245 static cfs_waitq_t rq_waitq;
247 static int mgc_process_log(struct obd_device *mgc,
248 struct config_llog_data *cld);
250 static int mgc_requeue_thread(void *data)
252 struct l_wait_info lwi_now, lwi_later;
253 struct config_llog_data *cld, *n;
254 char name[] = "ll_cfg_requeue";
258 ptlrpc_daemonize(name);
260 CDEBUG(D_MGC, "Starting requeue thread\n");
262 lwi_later = LWI_TIMEOUT(60 * HZ, NULL, NULL);
263 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP), &lwi_later);
265 /* Keep trying failed locks periodically */
266 spin_lock(&config_list_lock);
267 while (rq_state & (RQ_NOW | RQ_LATER)) {
268 /* Any new or requeued lostlocks will change the state */
269 rq_state &= ~(RQ_NOW | RQ_LATER);
270 spin_unlock(&config_list_lock);
272 /* Always wait a few seconds to allow the server who
273 caused the lock revocation to finish its setup, plus some
274 random so everyone doesn't try to reconnect at once. */
275 lwi_now = LWI_TIMEOUT(3 * HZ + (ll_rand() & 0xff) * (HZ / 100),
277 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi_now);
279 spin_lock(&config_list_lock);
280 list_for_each_entry_safe(cld, n, &config_llog_list,
282 spin_unlock(&config_list_lock);
283 if (cld->cld_lostlock) {
284 CDEBUG(D_MGC, "updating log %s\n",
286 cld->cld_lostlock = 0;
287 rc = mgc_process_log(cld->cld_mgcexp->exp_obd,
289 /* Whether we enqueued again or not in
290 mgc_process_log, we're done with the ref
291 from the old enqueue */
294 spin_lock(&config_list_lock);
296 spin_unlock(&config_list_lock);
298 /* Wait a bit to see if anyone else needs a requeue */
299 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
301 spin_lock(&config_list_lock);
303 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
304 rq_state &= ~RQ_RUNNING;
305 spin_unlock(&config_list_lock);
307 CDEBUG(D_MGC, "Ending requeue thread\n");
311 /* Add a cld to the list to requeue. Start the requeue thread if needed.
312 We are responsible for dropping the config log reference from here on out. */
313 static int mgc_requeue_add(struct config_llog_data *cld, int later)
317 CDEBUG(D_INFO, "log %s: requeue (l=%d r=%d sp=%d st=%x)\n",
318 cld->cld_logname, later, atomic_read(&cld->cld_refcount),
319 cld->cld_stopping, rq_state);
321 /* Hold lock for rq_state */
322 spin_lock(&config_list_lock);
323 cld->cld_lostlock = 1;
325 if (cld->cld_stopping || (rq_state & RQ_STOP)) {
326 spin_unlock(&config_list_lock);
331 if (!(rq_state & RQ_RUNNING)) {
332 LASSERT(rq_state == 0);
333 rq_state = RQ_RUNNING | (later ? RQ_LATER : RQ_NOW);
334 spin_unlock(&config_list_lock);
335 rc = cfs_kernel_thread(mgc_requeue_thread, 0,
336 CLONE_VM | CLONE_FILES);
338 CERROR("log %s: cannot start requeue thread (%d),"
339 "no more log updates!\n", cld->cld_logname, rc);
340 /* Drop the ref, since the rq thread won't */
341 cld->cld_lostlock = 0;
347 rq_state |= later ? RQ_LATER : RQ_NOW;
348 spin_unlock(&config_list_lock);
349 cfs_waitq_signal(&rq_waitq);
355 /********************** class fns **********************/
357 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb,
358 struct vfsmount *mnt)
360 struct lvfs_run_ctxt saved;
361 struct lustre_sb_info *lsi = s2lsi(sb);
362 struct client_obd *cli = &obd->u.cli;
363 struct dentry *dentry;
369 LASSERT(lsi->lsi_srv_mnt == mnt);
371 /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
372 down(&cli->cl_mgc_sem);
374 cleanup_group_info();
376 obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
377 if (IS_ERR(obd->obd_fsops)) {
378 up(&cli->cl_mgc_sem);
379 CERROR("No fstype %s rc=%ld\n", MT_STR(lsi->lsi_ldd),
380 PTR_ERR(obd->obd_fsops));
381 RETURN(PTR_ERR(obd->obd_fsops));
384 cli->cl_mgc_vfsmnt = mnt;
385 fsfilt_setup(obd, mnt->mnt_sb);
387 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
388 obd->obd_lvfs_ctxt.pwdmnt = mnt;
389 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
390 obd->obd_lvfs_ctxt.fs = get_ds();
392 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
393 dentry = lookup_one_len(MOUNT_CONFIGS_DIR, current->fs->pwd,
394 strlen(MOUNT_CONFIGS_DIR));
395 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
396 if (IS_ERR(dentry)) {
397 err = PTR_ERR(dentry);
398 CERROR("cannot lookup %s directory: rc = %d\n",
399 MOUNT_CONFIGS_DIR, err);
402 cli->cl_mgc_configs_dir = dentry;
404 /* We take an obd ref to insure that we can't get to mgc_cleanup
405 without calling mgc_fs_cleanup first. */
408 label = fsfilt_get_label(obd, mnt->mnt_sb);
410 CDEBUG(D_MGC, "MGC using disk labelled=%s\n", label);
412 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
416 fsfilt_put_ops(obd->obd_fsops);
417 obd->obd_fsops = NULL;
418 cli->cl_mgc_vfsmnt = NULL;
419 up(&cli->cl_mgc_sem);
423 static int mgc_fs_cleanup(struct obd_device *obd)
425 struct client_obd *cli = &obd->u.cli;
429 LASSERT(cli->cl_mgc_vfsmnt != NULL);
431 if (cli->cl_mgc_configs_dir != NULL) {
432 struct lvfs_run_ctxt saved;
433 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
434 l_dput(cli->cl_mgc_configs_dir);
435 cli->cl_mgc_configs_dir = NULL;
436 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
440 cli->cl_mgc_vfsmnt = NULL;
442 fsfilt_put_ops(obd->obd_fsops);
444 up(&cli->cl_mgc_sem);
449 static atomic_t mgc_count = ATOMIC_INIT(0);
450 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
456 case OBD_CLEANUP_EARLY:
458 case OBD_CLEANUP_EXPORTS:
459 if (atomic_dec_and_test(&mgc_count)) {
460 /* Kick the requeue waitq - cld's should all be
462 spin_lock(&config_list_lock);
464 spin_unlock(&config_list_lock);
465 cfs_waitq_signal(&rq_waitq);
468 case OBD_CLEANUP_SELF_EXP:
469 rc = obd_llog_finish(obd, 0);
471 CERROR("failed to cleanup llogging subsystems\n");
473 case OBD_CLEANUP_OBD:
479 static int mgc_cleanup(struct obd_device *obd)
481 struct client_obd *cli = &obd->u.cli;
485 LASSERT(cli->cl_mgc_vfsmnt == NULL);
487 /* COMPAT_146 - old config logs may have added profiles we don't
489 if (obd->obd_type->typ_refcnt <= 1)
490 /* Only for the last mgc */
491 class_del_profiles();
495 rc = client_obd_cleanup(obd);
499 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
506 rc = client_obd_setup(obd, lcfg);
508 GOTO(err_decref, rc);
510 rc = obd_llog_init(obd, NULL, obd, 0, NULL, NULL);
512 CERROR("failed to setup llogging subsystems\n");
513 GOTO(err_cleanup, rc);
516 spin_lock(&config_list_lock);
517 atomic_inc(&mgc_count);
518 if (atomic_read(&mgc_count) == 1) {
519 rq_state &= ~RQ_STOP;
520 cfs_waitq_init(&rq_waitq);
522 spin_unlock(&config_list_lock);
527 client_obd_cleanup(obd);
533 /* based on ll_mdc_blocking_ast */
534 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
535 void *data, int flag)
537 struct lustre_handle lockh;
538 struct config_llog_data *cld = (struct config_llog_data *)data;
543 case LDLM_CB_BLOCKING:
544 /* mgs wants the lock, give it up... */
545 LDLM_DEBUG(lock, "MGC blocking CB");
546 ldlm_lock2handle(lock, &lockh);
547 rc = ldlm_cli_cancel(&lockh);
549 case LDLM_CB_CANCELING: {
550 /* We've given up the lock, prepare ourselves to update. */
551 LDLM_DEBUG(lock, "MGC cancel CB");
553 CDEBUG(D_MGC, "Lock res "LPX64" (%.8s)\n",
554 lock->l_resource->lr_name.name[0],
555 (char *)&lock->l_resource->lr_name.name[0]);
558 CERROR("missing data, won't requeue\n");
561 /* Are we done with this log? */
562 if (cld->cld_stopping) {
563 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
568 /* Make sure not to re-enqueue when the mgc is stopping
569 (we get called from client_disconnect_export) */
570 if (!lock->l_conn_export ||
571 !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
572 CDEBUG(D_MGC, "log %s: disconnecting, won't requeue\n",
577 /* Did we fail to get the lock? */
578 if (lock->l_req_mode != lock->l_granted_mode) {
579 CDEBUG(D_MGC, "log %s: original grant failed, will "
580 "requeue later\n", cld->cld_logname);
581 /* Try to re-enqueue later */
582 rc = mgc_requeue_add(cld, 1);
586 rc = mgc_requeue_add(cld, 0);
595 CERROR("%s CB failed %d:\n", flag == LDLM_CB_BLOCKING ?
596 "blocking" : "cancel", rc);
597 LDLM_ERROR(lock, "MGC ast");
602 /* Send parameter to MGS*/
603 static int mgc_set_mgs_param(struct obd_export *exp,
604 struct mgs_send_param *msp)
606 struct ptlrpc_request *req;
607 struct mgs_send_param *req_msp, *rep_msp;
608 int size[] = { sizeof(struct ptlrpc_body), sizeof(*req_msp) };
609 int rep_size[] = { sizeof(struct ptlrpc_body), sizeof(*msp) };
613 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MGS_VERSION,
614 MGS_SET_INFO, 2, size, NULL);
618 req_msp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*req_msp));
622 memcpy(req_msp, msp, sizeof(*req_msp));
623 ptlrpc_req_set_repsize(req, 2, rep_size);
624 rc = ptlrpc_queue_wait(req);
626 rep_msp = lustre_swab_repbuf(req, REPLY_REC_OFF,
627 sizeof(*rep_msp), NULL);
628 memcpy(msp, rep_msp, sizeof(*rep_msp));
631 ptlrpc_req_finished(req);
636 /* Take a config lock so we can get cancel notifications */
637 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
638 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
639 int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
640 void *data, __u32 lvb_len, void *lvb_swabber,
641 struct lustre_handle *lockh)
643 struct config_llog_data *cld = (struct config_llog_data *)data;
644 struct ldlm_enqueue_info einfo = { type, mode, mgc_blocking_ast,
645 ldlm_completion_ast, NULL, data};
650 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
651 cld->cld_resid.name[0]);
653 /* We can only drop this config log ref when we drop the lock */
654 if (config_log_get(cld))
655 RETURN(ELDLM_LOCK_ABORTED);
657 /* We need a callback for every lockholder, so don't try to
658 ldlm_lock_match (see rev 1.1.2.11.2.47) */
660 rc = ldlm_cli_enqueue(exp, NULL, &einfo, &cld->cld_resid,
661 NULL, flags, NULL, 0, NULL, lockh, 0);
662 /* A failed enqueue should still call the mgc_blocking_ast,
663 where it will be requeued if needed ("grant failed"). */
668 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
669 __u32 mode, struct lustre_handle *lockh)
673 ldlm_lock_decref(lockh, mode);
679 static int mgc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
680 void *karg, void *uarg)
682 struct obd_device *obd = exp->exp_obd;
683 struct obd_ioctl_data *data = karg;
684 struct llog_ctxt *ctxt;
685 struct lvfs_run_ctxt saved;
689 if (!try_module_get(THIS_MODULE)) {
690 CERROR("Can't get module. Is it alive?");
694 /* REPLicator context */
695 case OBD_IOC_PARSE: {
696 CERROR("MGC parsing llog %s\n", data->ioc_inlbuf1);
697 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
698 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
702 case OBD_IOC_LLOG_INFO:
703 case OBD_IOC_LLOG_PRINT: {
704 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
705 rc = llog_ioctl(ctxt, cmd, data);
710 /* ORIGinator context */
711 case OBD_IOC_DUMP_LOG: {
712 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
713 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
714 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
715 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
722 CERROR("mgc_ioctl(): unrecognised ioctl %#x\n", cmd);
723 GOTO(out, rc = -ENOTTY);
726 module_put(THIS_MODULE);
732 /* Send target_reg message to MGS */
733 static int mgc_target_register(struct obd_export *exp,
734 struct mgs_target_info *mti)
736 struct ptlrpc_request *req;
737 struct mgs_target_info *req_mti, *rep_mti;
738 int size[] = { sizeof(struct ptlrpc_body), sizeof(*req_mti) };
739 int rep_size[] = { sizeof(struct ptlrpc_body), sizeof(*mti) };
743 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MGS_VERSION,
744 MGS_TARGET_REG, 2, size, NULL);
748 req_mti = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*req_mti));
751 memcpy(req_mti, mti, sizeof(*req_mti));
753 ptlrpc_req_set_repsize(req, 2, rep_size);
755 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
757 rc = ptlrpc_queue_wait(req);
759 rep_mti = lustre_swab_repbuf(req, REPLY_REC_OFF,
761 lustre_swab_mgs_target_info);
762 memcpy(mti, rep_mti, sizeof(*rep_mti));
763 CDEBUG(D_MGC, "register %s got index = %d\n",
764 mti->mti_svname, mti->mti_stripe_index);
766 ptlrpc_req_finished(req);
771 int mgc_reconnect_import(struct obd_import *imp)
773 /* Force a new connect attempt */
774 ptlrpc_invalidate_import(imp);
775 /* Do a fresh connect next time by zeroing the handle */
776 ptlrpc_disconnect_import(imp, 1);
777 /* Wait for all invalidate calls to finish */
778 if (atomic_read(&imp->imp_inval_count) > 0) {
780 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
781 rc = l_wait_event(imp->imp_recovery_waitq,
782 (atomic_read(&imp->imp_inval_count) == 0),
785 CERROR("Interrupted, inval=%d\n",
786 atomic_read(&imp->imp_inval_count));
789 /* Allow reconnect attempts */
790 imp->imp_obd->obd_no_recov = 0;
791 /* Remove 'invalid' flag */
792 ptlrpc_activate_import(imp);
793 /* Attempt a new connect */
794 ptlrpc_recover_import(imp, NULL);
798 int mgc_set_info_async(struct obd_export *exp, obd_count keylen,
799 void *key, obd_count vallen, void *val,
800 struct ptlrpc_request_set *set)
802 struct obd_import *imp = class_exp2cliimp(exp);
806 /* Try to "recover" the initial connection; i.e. retry */
807 if (KEY_IS(KEY_INIT_RECOV)) {
808 if (vallen != sizeof(int))
810 spin_lock(&imp->imp_lock);
811 imp->imp_initial_recov = *(int *)val;
812 spin_unlock(&imp->imp_lock);
813 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
814 exp->exp_obd->obd_name, imp->imp_initial_recov);
817 /* Turn off initial_recov after we try all backup servers once */
818 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
820 if (vallen != sizeof(int))
823 spin_lock(&imp->imp_lock);
824 imp->imp_initial_recov_bk = value > 0;
825 /* Even after the initial connection, give up all comms if
826 nobody answers the first time. */
827 imp->imp_recon_bk = 1;
828 spin_unlock(&imp->imp_lock);
829 CDEBUG(D_MGC, "InitRecov %s %d/%d:d%d:i%d:r%d:or%d:%s\n",
830 imp->imp_obd->obd_name, value, imp->imp_initial_recov,
831 imp->imp_deactive, imp->imp_invalid,
832 imp->imp_replayable, imp->imp_obd->obd_replayable,
833 ptlrpc_import_state_name(imp->imp_state));
834 /* Resurrect if we previously died */
835 if (imp->imp_invalid || value > 1)
836 mgc_reconnect_import(imp);
839 /* FIXME move this to mgc_process_config */
840 if (KEY_IS("register_target")) {
841 struct mgs_target_info *mti;
842 if (vallen != sizeof(struct mgs_target_info))
844 mti = (struct mgs_target_info *)val;
845 CDEBUG(D_MGC, "register_target %s %#x\n",
846 mti->mti_svname, mti->mti_flags);
847 rc = mgc_target_register(exp, mti);
850 if (KEY_IS("set_fs")) {
851 struct super_block *sb = (struct super_block *)val;
852 struct lustre_sb_info *lsi;
853 if (vallen != sizeof(struct super_block))
856 rc = mgc_fs_setup(exp->exp_obd, sb, lsi->lsi_srv_mnt);
858 CERROR("set_fs got %d\n", rc);
862 if (KEY_IS("clear_fs")) {
865 rc = mgc_fs_cleanup(exp->exp_obd);
867 CERROR("clear_fs got %d\n", rc);
871 if (KEY_IS(KEY_SET_INFO)) {
872 struct mgs_send_param *msp;
874 msp = (struct mgs_send_param *)val;
875 rc = mgc_set_mgs_param(exp, msp);
882 static int mgc_import_event(struct obd_device *obd,
883 struct obd_import *imp,
884 enum obd_import_event event)
888 LASSERT(imp->imp_obd == obd);
889 CDEBUG(D_MGC, "import event %#x\n", event);
892 case IMP_EVENT_DISCON:
893 /* MGC imports should not wait for recovery */
894 ptlrpc_invalidate_import(imp);
896 case IMP_EVENT_INACTIVE:
898 case IMP_EVENT_INVALIDATE: {
899 struct ldlm_namespace *ns = obd->obd_namespace;
900 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
903 case IMP_EVENT_ACTIVE:
904 LCONSOLE_WARN("%s: Reactivating import\n", obd->obd_name);
909 CERROR("Unknown import event %#x\n", event);
915 static int mgc_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
916 struct obd_device *tgt, int count,
917 struct llog_catid *logid, struct obd_uuid *uuid)
919 struct llog_ctxt *ctxt;
923 rc = llog_setup(obd, llogs, LLOG_CONFIG_ORIG_CTXT, tgt, 0, NULL,
928 rc = llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
931 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
932 ctxt->loc_imp = obd->u.cli.cl_import;
938 static int mgc_llog_finish(struct obd_device *obd, int count)
943 rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_REPL_CTXT));
944 rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
949 /* identical to mgs_log_is_empty */
950 static int mgc_llog_is_empty(struct obd_device *obd, struct llog_ctxt *ctxt,
953 struct lvfs_run_ctxt saved;
954 struct llog_handle *llh;
957 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
958 rc = llog_create(ctxt, &llh, NULL, name);
960 llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
961 rc = llog_get_size(llh);
964 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
965 /* header is record 1 */
969 static int mgc_copy_handler(struct llog_handle *llh, struct llog_rec_hdr *rec,
972 struct llog_rec_hdr local_rec = *rec;
973 struct llog_handle *local_llh = (struct llog_handle *)data;
974 char *cfg_buf = (char*) (rec + 1);
975 struct lustre_cfg *lcfg;
979 /* Append all records */
980 local_rec.lrh_len -= sizeof(*rec) + sizeof(struct llog_rec_tail);
981 rc = llog_write_rec(local_llh, &local_rec, NULL, 0,
982 (void *)cfg_buf, -1);
984 lcfg = (struct lustre_cfg *)cfg_buf;
985 CDEBUG(D_INFO, "idx=%d, rc=%d, len=%d, cmd %x %s %s\n",
986 rec->lrh_index, rc, rec->lrh_len, lcfg->lcfg_command,
987 lustre_cfg_string(lcfg, 0), lustre_cfg_string(lcfg, 1));
992 /* Copy a remote log locally */
993 static int mgc_copy_llog(struct obd_device *obd, struct llog_ctxt *rctxt,
994 struct llog_ctxt *lctxt, char *logname)
996 struct llog_handle *local_llh, *remote_llh;
997 struct obd_uuid *uuid;
1002 /* Write new log to a temp name, then vfs_rename over logname
1003 upon successful completion. */
1005 OBD_ALLOC(temp_log, strlen(logname) + 1);
1008 sprintf(temp_log, "%sT", logname);
1010 /* Make sure there's no old temp log */
1011 rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1014 rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, NULL);
1017 rc = llog_destroy(local_llh);
1018 llog_free_handle(local_llh);
1022 /* open local log */
1023 rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1027 /* set the log header uuid for fun */
1028 OBD_ALLOC_PTR(uuid);
1029 obd_str2uuid(uuid, logname);
1030 rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, uuid);
1033 GOTO(out_closel, rc);
1035 /* open remote log */
1036 rc = llog_create(rctxt, &remote_llh, NULL, logname);
1038 GOTO(out_closel, rc);
1039 rc = llog_init_handle(remote_llh, LLOG_F_IS_PLAIN, NULL);
1041 GOTO(out_closer, rc);
1043 /* Copy remote log */
1044 rc = llog_process(remote_llh, mgc_copy_handler,(void *)local_llh, NULL);
1047 rc2 = llog_close(remote_llh);
1051 rc2 = llog_close(local_llh);
1055 /* We've copied the remote log to the local temp log, now
1056 replace the old local log with the temp log. */
1058 struct client_obd *cli = &obd->u.cli;
1060 LASSERT(cli->cl_mgc_configs_dir);
1061 rc = lustre_rename(cli->cl_mgc_configs_dir, temp_log, logname);
1063 CDEBUG(D_MGC, "Copied remote log %s (%d)\n", logname, rc);
1066 CERROR("Failed to copy remote log %s (%d)\n", logname, rc);
1067 OBD_FREE(temp_log, strlen(logname) + 1);
1071 DECLARE_MUTEX(llog_process_lock);
1073 /* Get a config log from the MGS and process it.
1074 This func is called for both clients and servers. */
1075 static int mgc_process_log(struct obd_device *mgc,
1076 struct config_llog_data *cld)
1078 struct llog_ctxt *ctxt, *lctxt;
1079 struct lustre_handle lockh;
1080 struct client_obd *cli = &mgc->u.cli;
1081 struct lvfs_run_ctxt saved;
1082 struct lustre_sb_info *lsi;
1083 int rc = 0, rcl, flags = 0, must_pop = 0;
1086 if (!cld || !cld->cld_cfg.cfg_sb) {
1087 /* This should never happen */
1088 CERROR("Missing cld, aborting log update\n");
1091 if (cld->cld_stopping)
1094 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PROCESS_LOG, 20);
1096 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1098 CDEBUG(D_MGC, "Process log %s:%s from %d\n", cld->cld_logname,
1099 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1101 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1103 CERROR("missing llog context\n");
1107 /* I don't want mutliple processes running process_log at once --
1108 sounds like badness. It actually might be fine, as long as
1109 we're not trying to update from the same log
1110 simultaneously (in which case we should use a per-log sem.) */
1111 down(&llog_process_lock);
1113 /* Get the cfg lock on the llog */
1114 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1115 LCK_CR, &flags, NULL, NULL, NULL,
1116 cld, 0, NULL, &lockh);
1118 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1120 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1122 /* Copy the setup log locally if we can. Don't mess around if we're
1123 running an MGS though (logs are already local). */
1124 if (lctxt && lsi && (lsi->lsi_flags & LSI_SERVER) &&
1125 (lsi->lsi_srv_mnt == cli->cl_mgc_vfsmnt) &&
1126 !IS_MGS(lsi->lsi_ldd)) {
1127 push_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1130 /* Only try to copy log if we have the lock. */
1131 rc = mgc_copy_llog(mgc, ctxt, lctxt, cld->cld_logname);
1133 if (mgc_llog_is_empty(mgc, lctxt, cld->cld_logname)) {
1134 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1135 "log %s and no local copy."
1136 "\n", cld->cld_logname);
1137 GOTO(out_pop, rc = -ENOTCONN);
1139 LCONSOLE_WARN("Failed to get MGS log %s, using "
1140 "local copy.\n", cld->cld_logname);
1142 /* Now, whether we copied or not, start using the local llog.
1143 If we failed to copy, we'll start using whatever the old
1148 /* logname and instance info should be the same, so use our
1149 copy of the instance for the update. The cfg_last_idx will
1151 rc = class_config_parse_llog(ctxt, cld->cld_logname, &cld->cld_cfg);
1155 pop_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1157 /* Now drop the lock so MGS can revoke it */
1159 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1162 CERROR("Can't drop cfg lock: %d\n", rcl);
1165 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1166 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1168 up(&llog_process_lock);
1173 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1175 struct lustre_cfg *lcfg = buf;
1180 switch(cmd = lcfg->lcfg_command) {
1181 case LCFG_LOV_ADD_OBD: {
1182 /* Add any new target, not just osts */
1183 struct mgs_target_info *mti;
1185 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1186 sizeof(struct mgs_target_info))
1187 GOTO(out, rc = -EINVAL);
1189 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1190 CDEBUG(D_MGC, "add_target %s %#x\n",
1191 mti->mti_svname, mti->mti_flags);
1192 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1195 case LCFG_LOV_DEL_OBD:
1196 /* Remove target from the fs? */
1198 CERROR("lov_del_obd unimplemented\n");
1201 case LCFG_LOG_START: {
1202 struct config_llog_data *cld;
1203 struct config_llog_instance *cfg;
1204 struct super_block *sb;
1205 char *logname = lustre_cfg_string(lcfg, 1);
1206 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1207 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1209 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1212 /* We're only called through here on the initial mount */
1213 rc = config_log_add(logname, cfg, sb);
1216 cld = config_log_find(logname, cfg);
1223 /* FIXME only set this for old logs! Right now this forces
1224 us to always skip the "inside markers" check */
1225 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1227 rc = mgc_process_log(obd, cld);
1228 config_log_put(cld);
1232 case LCFG_LOG_END: {
1233 struct config_llog_instance *cfg = NULL;
1234 char *logname = lustre_cfg_string(lcfg, 1);
1235 if (lcfg->lcfg_bufcount >= 2)
1236 cfg = (struct config_llog_instance *)lustre_cfg_buf(
1238 rc = config_log_end(logname, cfg);
1242 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1243 GOTO(out, rc = -EINVAL);
1251 struct obd_ops mgc_obd_ops = {
1252 .o_owner = THIS_MODULE,
1253 .o_setup = mgc_setup,
1254 .o_precleanup = mgc_precleanup,
1255 .o_cleanup = mgc_cleanup,
1256 .o_add_conn = client_import_add_conn,
1257 .o_del_conn = client_import_del_conn,
1258 .o_connect = client_connect_import,
1259 .o_disconnect = client_disconnect_export,
1260 //.o_enqueue = mgc_enqueue,
1261 .o_cancel = mgc_cancel,
1262 //.o_iocontrol = mgc_iocontrol,
1263 .o_set_info_async = mgc_set_info_async,
1264 .o_import_event = mgc_import_event,
1265 .o_llog_init = mgc_llog_init,
1266 .o_llog_finish = mgc_llog_finish,
1267 .o_process_config = mgc_process_config,
1270 int __init mgc_init(void)
1272 return class_register_type(&mgc_obd_ops, NULL, NULL,
1273 LUSTRE_MGC_NAME, NULL);
1277 static void /*__exit*/ mgc_exit(void)
1279 class_unregister_type(LUSTRE_MGC_NAME);
1282 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1283 MODULE_DESCRIPTION("Lustre Management Client");
1284 MODULE_LICENSE("GPL");
1286 module_init(mgc_init);
1287 module_exit(mgc_exit);