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>
49 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id)
55 /* fsname is at most 8 chars long at the beginning of the logname
56 e.g. "lustre-MDT0001" or "lustre" */
57 name_end = strrchr(logname, '-');
59 len = name_end - logname;
61 len = strlen(logname);
63 CERROR("fsname too long: %s\n", logname);
67 CERROR("missing fsname: %s\n", logname);
70 memcpy(&resname, logname, len);
72 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 CERROR("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);
273 if (cld->cld_lostlock) {
274 CDEBUG(D_MGC, "updating log %s\n",
276 cld->cld_lostlock = 0;
277 rc = mgc_process_log(cld->cld_mgcexp->exp_obd,
279 /* Whether we enqueued again or not in
280 mgc_process_log, we're done with the ref
281 from the old enqueue */
285 spin_lock(&config_list_lock);
287 spin_unlock(&config_list_lock);
289 /* Wait a bit to see if anyone else needs a requeue */
290 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
292 spin_lock(&config_list_lock);
294 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
295 rq_state &= ~RQ_RUNNING;
296 spin_unlock(&config_list_lock);
298 CDEBUG(D_MGC, "Ending requeue thread\n");
302 /* Add a cld to the list to requeue. Start the requeue thread if needed.
303 We are responsible for dropping the config log reference from here on out. */
304 static int mgc_requeue_add(struct config_llog_data *cld, int later)
308 CDEBUG(D_INFO, "log %s: requeue (l=%d r=%d sp=%d st=%x)\n",
309 cld->cld_logname, later, atomic_read(&cld->cld_refcount),
310 cld->cld_stopping, rq_state);
312 /* Hold lock for rq_state */
313 spin_lock(&config_list_lock);
314 cld->cld_lostlock = 1;
316 if (cld->cld_stopping || (rq_state & RQ_STOP)) {
317 spin_unlock(&config_list_lock);
322 if (!(rq_state & RQ_RUNNING)) {
323 LASSERT(rq_state == 0);
324 rq_state = RQ_RUNNING | (later ? RQ_LATER : RQ_NOW);
325 spin_unlock(&config_list_lock);
326 rc = cfs_kernel_thread(mgc_requeue_thread, 0,
327 CLONE_VM | CLONE_FILES);
329 CERROR("log %s: cannot start requeue thread (%d),"
330 "no more log updates!\n", cld->cld_logname, rc);
331 /* Drop the ref, since the rq thread won't */
332 cld->cld_lostlock = 0;
338 rq_state |= later ? RQ_LATER : RQ_NOW;
339 spin_unlock(&config_list_lock);
340 cfs_waitq_signal(&rq_waitq);
346 /********************** class fns **********************/
348 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb,
349 struct vfsmount *mnt)
351 struct lvfs_run_ctxt saved;
352 struct lustre_sb_info *lsi = s2lsi(sb);
353 struct client_obd *cli = &obd->u.cli;
354 struct dentry *dentry;
360 LASSERT(lsi->lsi_srv_mnt == mnt);
362 /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
363 down(&cli->cl_mgc_sem);
365 cleanup_group_info();
367 obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
368 if (IS_ERR(obd->obd_fsops)) {
369 up(&cli->cl_mgc_sem);
370 CERROR("No fstype %s rc=%ld\n", MT_STR(lsi->lsi_ldd),
371 PTR_ERR(obd->obd_fsops));
372 RETURN(PTR_ERR(obd->obd_fsops));
375 cli->cl_mgc_vfsmnt = mnt;
376 fsfilt_setup(obd, mnt->mnt_sb);
378 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
379 obd->obd_lvfs_ctxt.pwdmnt = mnt;
380 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
381 obd->obd_lvfs_ctxt.fs = get_ds();
383 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
384 dentry = lookup_one_len(MOUNT_CONFIGS_DIR, current->fs->pwd,
385 strlen(MOUNT_CONFIGS_DIR));
386 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
387 if (IS_ERR(dentry)) {
388 err = PTR_ERR(dentry);
389 CERROR("cannot lookup %s directory: rc = %d\n",
390 MOUNT_CONFIGS_DIR, err);
393 cli->cl_mgc_configs_dir = dentry;
395 /* We take an obd ref to insure that we can't get to mgc_cleanup
396 without calling mgc_fs_cleanup first. */
399 label = fsfilt_get_label(obd, mnt->mnt_sb);
401 CDEBUG(D_MGC, "MGC using disk labelled=%s\n", label);
403 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
407 fsfilt_put_ops(obd->obd_fsops);
408 obd->obd_fsops = NULL;
409 cli->cl_mgc_vfsmnt = NULL;
410 up(&cli->cl_mgc_sem);
414 static int mgc_fs_cleanup(struct obd_device *obd)
416 struct client_obd *cli = &obd->u.cli;
420 LASSERT(cli->cl_mgc_vfsmnt != NULL);
422 if (cli->cl_mgc_configs_dir != NULL) {
423 struct lvfs_run_ctxt saved;
424 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
425 l_dput(cli->cl_mgc_configs_dir);
426 cli->cl_mgc_configs_dir = NULL;
427 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
431 cli->cl_mgc_vfsmnt = NULL;
433 fsfilt_put_ops(obd->obd_fsops);
435 up(&cli->cl_mgc_sem);
440 static atomic_t mgc_count = ATOMIC_INIT(0);
441 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
447 case OBD_CLEANUP_EARLY:
449 case OBD_CLEANUP_EXPORTS:
450 if (atomic_dec_and_test(&mgc_count)) {
451 /* Kick the requeue waitq - cld's should all be
453 spin_lock(&config_list_lock);
455 spin_unlock(&config_list_lock);
456 cfs_waitq_signal(&rq_waitq);
459 case OBD_CLEANUP_SELF_EXP:
460 rc = obd_llog_finish(obd, 0);
462 CERROR("failed to cleanup llogging subsystems\n");
464 case OBD_CLEANUP_OBD:
470 static int mgc_cleanup(struct obd_device *obd)
472 struct client_obd *cli = &obd->u.cli;
476 LASSERT(cli->cl_mgc_vfsmnt == NULL);
478 /* COMPAT_146 - old config logs may have added profiles we don't
480 if (obd->obd_type->typ_refcnt <= 1)
481 /* Only for the last mgc */
482 class_del_profiles();
486 rc = client_obd_cleanup(obd);
490 static int mgc_setup(struct obd_device *obd, obd_count len, void *buf)
497 rc = client_obd_setup(obd, len, buf);
499 GOTO(err_decref, rc);
501 rc = obd_llog_init(obd, obd, 0, NULL, NULL);
503 CERROR("failed to setup llogging subsystems\n");
504 GOTO(err_cleanup, rc);
507 spin_lock(&config_list_lock);
508 atomic_inc(&mgc_count);
509 if (atomic_read(&mgc_count) == 1) {
510 rq_state &= ~RQ_STOP;
511 cfs_waitq_init(&rq_waitq);
513 spin_unlock(&config_list_lock);
518 client_obd_cleanup(obd);
524 /* based on ll_mdc_blocking_ast */
525 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
526 void *data, int flag)
528 struct lustre_handle lockh;
529 struct config_llog_data *cld = (struct config_llog_data *)data;
534 case LDLM_CB_BLOCKING:
535 /* mgs wants the lock, give it up... */
536 LDLM_DEBUG(lock, "MGC blocking CB");
537 ldlm_lock2handle(lock, &lockh);
538 rc = ldlm_cli_cancel(&lockh);
540 case LDLM_CB_CANCELING: {
541 /* We've given up the lock, prepare ourselves to update. */
542 LDLM_DEBUG(lock, "MGC cancel CB");
544 CDEBUG(D_MGC, "Lock res "LPX64" (%.8s)\n",
545 lock->l_resource->lr_name.name[0],
546 (char *)&lock->l_resource->lr_name.name[0]);
549 CERROR("missing data, won't requeue\n");
552 /* Are we done with this log? */
553 if (cld->cld_stopping) {
554 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
559 /* Make sure not to re-enqueue when the mgc is stopping
560 (we get called from client_disconnect_export) */
561 if (!lock->l_conn_export ||
562 !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
563 CDEBUG(D_MGC, "log %s: disconnecting, won't requeue\n",
568 /* Did we fail to get the lock? */
569 if (lock->l_req_mode != lock->l_granted_mode) {
570 CDEBUG(D_MGC, "log %s: original grant failed, will "
571 "requeue later\n", cld->cld_logname);
572 /* Try to re-enqueue later */
573 rc = mgc_requeue_add(cld, 1);
577 rc = mgc_requeue_add(cld, 0);
586 CERROR("%s CB failed %d:\n", flag == LDLM_CB_BLOCKING ?
587 "blocking" : "cancel", rc);
588 LDLM_ERROR(lock, "MGC ast");
593 /* Take a config lock so we can get cancel notifications */
594 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
595 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
596 int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
597 void *data, __u32 lvb_len, void *lvb_swabber,
598 struct lustre_handle *lockh)
600 struct config_llog_data *cld = (struct config_llog_data *)data;
604 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
605 cld->cld_resid.name[0]);
607 /* We can only drop this config log ref when we drop the lock */
608 if (config_log_get(cld))
609 RETURN(ELDLM_LOCK_ABORTED);
611 /* We need a callback for every lockholder, so don't try to
612 ldlm_lock_match (see rev 1.1.2.11.2.47) */
614 rc = ldlm_cli_enqueue(exp, NULL, cld->cld_resid,
615 type, NULL, mode, flags,
616 mgc_blocking_ast, ldlm_completion_ast, NULL,
617 data, NULL, 0, NULL, lockh, 0);
618 /* A failed enqueue should still call the mgc_blocking_ast,
619 where it will be requeued if needed ("grant failed"). */
624 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
625 __u32 mode, struct lustre_handle *lockh)
629 ldlm_lock_decref(lockh, mode);
635 static int mgc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
636 void *karg, void *uarg)
638 struct obd_device *obd = exp->exp_obd;
639 struct obd_ioctl_data *data = karg;
640 struct llog_ctxt *ctxt;
641 struct lvfs_run_ctxt saved;
645 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
648 if (!try_module_get(THIS_MODULE)) {
649 CERROR("Can't get module. Is it alive?");
654 /* REPLicator context */
655 case OBD_IOC_PARSE: {
656 CERROR("MGC parsing llog %s\n", data->ioc_inlbuf1);
657 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
658 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
662 case OBD_IOC_LLOG_INFO:
663 case OBD_IOC_LLOG_PRINT: {
664 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
665 rc = llog_ioctl(ctxt, cmd, data);
670 /* ORIGinator context */
671 case OBD_IOC_DUMP_LOG: {
672 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
673 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
674 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
675 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
682 CERROR("mgc_ioctl(): unrecognised ioctl %#x\n", cmd);
683 GOTO(out, rc = -ENOTTY);
686 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
689 module_put(THIS_MODULE);
696 /* Send target_reg message to MGS */
697 static int mgc_target_register(struct obd_export *exp,
698 struct mgs_target_info *mti)
700 struct ptlrpc_request *req;
701 struct mgs_target_info *req_mti, *rep_mti;
702 int size[] = { sizeof(struct ptlrpc_body), sizeof(*req_mti) };
703 int rep_size[] = { sizeof(struct ptlrpc_body), sizeof(*mti) };
707 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MGS_VERSION,
708 MGS_TARGET_REG, 2, size, NULL);
712 req_mti = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*req_mti));
715 memcpy(req_mti, mti, sizeof(*req_mti));
717 ptlrpc_req_set_repsize(req, 2, rep_size);
719 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
721 rc = ptlrpc_queue_wait(req);
723 rep_mti = lustre_swab_repbuf(req, REPLY_REC_OFF,
725 lustre_swab_mgs_target_info);
726 memcpy(mti, rep_mti, sizeof(*rep_mti));
727 CDEBUG(D_MGC, "register %s got index = %d\n",
728 mti->mti_svname, mti->mti_stripe_index);
730 ptlrpc_req_finished(req);
735 int mgc_set_info_async(struct obd_export *exp, obd_count keylen,
736 void *key, obd_count vallen, void *val,
737 struct ptlrpc_request_set *set)
739 struct obd_import *imp = class_exp2cliimp(exp);
743 /* Try to "recover" the initial connection; i.e. retry */
744 if (KEY_IS(KEY_INIT_RECOV)) {
745 if (vallen != sizeof(int))
747 imp->imp_initial_recov = *(int *)val;
748 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
749 exp->exp_obd->obd_name, imp->imp_initial_recov);
752 /* Turn off initial_recov after we try all backup servers once */
753 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
755 if (vallen != sizeof(int))
758 imp->imp_initial_recov_bk = value > 0;
759 /* Even after the initial connection, give up all comms if
760 nobody answers the first time. */
761 imp->imp_recon_bk = 1;
762 CDEBUG(D_MGC, "InitRecov %s %d/%d:d%d:i%d:r%d:or%d:%s\n",
763 imp->imp_obd->obd_name, value, imp->imp_initial_recov,
764 imp->imp_deactive, imp->imp_invalid,
765 imp->imp_replayable, imp->imp_obd->obd_replayable,
766 ptlrpc_import_state_name(imp->imp_state));
767 /* Resurrect if we previously died */
768 if (imp->imp_invalid || value > 1) {
769 /* See client_disconnect_export */
770 /* Allow reconnect attempts */
771 imp->imp_obd->obd_no_recov = 0;
772 /* Force a new connect attempt */
773 /* (can't put these in obdclass, module loop) */
774 ptlrpc_invalidate_import(imp);
775 /* Do a fresh connect next time by zeroing the handle */
776 ptlrpc_disconnect_import(imp, 1);
777 /* Remove 'invalid' flag */
778 ptlrpc_activate_import(imp);
779 /* Attempt a new connect */
780 ptlrpc_recover_import(imp, NULL);
784 /* FIXME move this to mgc_process_config */
785 if (KEY_IS("register_target")) {
786 struct mgs_target_info *mti;
787 if (vallen != sizeof(struct mgs_target_info))
789 mti = (struct mgs_target_info *)val;
790 CDEBUG(D_MGC, "register_target %s %#x\n",
791 mti->mti_svname, mti->mti_flags);
792 rc = mgc_target_register(exp, mti);
795 if (KEY_IS("set_fs")) {
796 struct super_block *sb = (struct super_block *)val;
797 struct lustre_sb_info *lsi;
798 if (vallen != sizeof(struct super_block))
801 rc = mgc_fs_setup(exp->exp_obd, sb, lsi->lsi_srv_mnt);
803 CERROR("set_fs got %d\n", rc);
807 if (KEY_IS("clear_fs")) {
810 rc = mgc_fs_cleanup(exp->exp_obd);
812 CERROR("clear_fs got %d\n", rc);
820 static int mgc_import_event(struct obd_device *obd,
821 struct obd_import *imp,
822 enum obd_import_event event)
826 LASSERT(imp->imp_obd == obd);
827 CDEBUG(D_MGC, "import event %#x\n", event);
830 case IMP_EVENT_DISCON:
831 /* MGC imports should not wait for recovery */
832 ptlrpc_invalidate_import(imp);
834 case IMP_EVENT_INACTIVE:
836 case IMP_EVENT_INVALIDATE: {
837 struct ldlm_namespace *ns = obd->obd_namespace;
838 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
841 case IMP_EVENT_ACTIVE:
842 LCONSOLE_WARN("%s: Reactivating import\n", obd->obd_name);
847 CERROR("Unknown import event %#x\n", event);
853 static int mgc_llog_init(struct obd_device *obd, struct obd_device *tgt,
854 int count, struct llog_catid *logid,
855 struct obd_uuid *uuid)
857 struct llog_ctxt *ctxt;
861 rc = llog_setup(obd, LLOG_CONFIG_ORIG_CTXT, tgt, 0, NULL,
866 rc = llog_setup(obd, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
869 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
870 ctxt->loc_imp = obd->u.cli.cl_import;
876 static int mgc_llog_finish(struct obd_device *obd, int count)
881 rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_REPL_CTXT));
882 rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
887 /* identical to mgs_log_is_empty */
888 static int mgc_llog_is_empty(struct obd_device *obd, struct llog_ctxt *ctxt,
891 struct lvfs_run_ctxt saved;
892 struct llog_handle *llh;
895 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
896 rc = llog_create(ctxt, &llh, NULL, name);
898 llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
899 rc = llog_get_size(llh);
902 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
903 /* header is record 1 */
907 static int mgc_copy_handler(struct llog_handle *llh, struct llog_rec_hdr *rec,
910 struct llog_rec_hdr local_rec = *rec;
911 struct llog_handle *local_llh = (struct llog_handle *)data;
912 char *cfg_buf = (char*) (rec + 1);
913 struct lustre_cfg *lcfg;
917 /* Append all records */
918 local_rec.lrh_len -= sizeof(*rec) + sizeof(struct llog_rec_tail);
919 rc = llog_write_rec(local_llh, &local_rec, NULL, 0,
920 (void *)cfg_buf, -1);
922 lcfg = (struct lustre_cfg *)cfg_buf;
923 CDEBUG(D_INFO, "idx=%d, rc=%d, len=%d, cmd %x %s %s\n",
924 rec->lrh_index, rc, rec->lrh_len, lcfg->lcfg_command,
925 lustre_cfg_string(lcfg, 0), lustre_cfg_string(lcfg, 1));
930 /* Copy a remote log locally */
931 static int mgc_copy_llog(struct obd_device *obd, struct llog_ctxt *rctxt,
932 struct llog_ctxt *lctxt, char *logname)
934 struct llog_handle *local_llh, *remote_llh;
935 struct obd_uuid *uuid;
940 /* Write new log to a temp name, then vfs_rename over logname
941 upon successful completion. */
943 OBD_ALLOC(temp_log, strlen(logname) + 1);
946 sprintf(temp_log, "%sT", logname);
948 /* Make sure there's no old temp log */
949 rc = llog_create(lctxt, &local_llh, NULL, temp_log);
952 rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, NULL);
955 rc = llog_destroy(local_llh);
956 llog_free_handle(local_llh);
961 rc = llog_create(lctxt, &local_llh, NULL, temp_log);
965 /* set the log header uuid for fun */
967 obd_str2uuid(uuid, logname);
968 rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, uuid);
971 GOTO(out_closel, rc);
973 /* open remote log */
974 rc = llog_create(rctxt, &remote_llh, NULL, logname);
976 GOTO(out_closel, rc);
977 rc = llog_init_handle(remote_llh, LLOG_F_IS_PLAIN, NULL);
979 GOTO(out_closer, rc);
981 /* Copy remote log */
982 rc = llog_process(remote_llh, mgc_copy_handler,(void *)local_llh, NULL);
985 rc2 = llog_close(remote_llh);
989 rc2 = llog_close(local_llh);
993 /* We've copied the remote log to the local temp log, now
994 replace the old local log with the temp log. */
996 struct client_obd *cli = &obd->u.cli;
998 LASSERT(cli->cl_mgc_configs_dir);
999 rc = lustre_rename(cli->cl_mgc_configs_dir, temp_log, logname);
1001 CDEBUG(D_MGC, "Copied remote log %s (%d)\n", logname, rc);
1004 CERROR("Failed to copy remote log %s (%d)\n", logname, rc);
1005 OBD_FREE(temp_log, strlen(logname) + 1);
1009 DECLARE_MUTEX(llog_process_lock);
1011 /* Get a config log from the MGS and process it.
1012 This func is called for both clients and servers. */
1013 static int mgc_process_log(struct obd_device *mgc,
1014 struct config_llog_data *cld)
1016 struct llog_ctxt *ctxt, *lctxt;
1017 struct lustre_handle lockh;
1018 struct client_obd *cli = &mgc->u.cli;
1019 struct lvfs_run_ctxt saved;
1020 struct lustre_sb_info *lsi;
1021 int rc = 0, rcl, flags = 0, must_pop = 0;
1024 if (!cld || !cld->cld_cfg.cfg_sb) {
1025 /* This should never happen */
1026 CERROR("Missing cld, aborting log update\n");
1029 if (cld->cld_stopping)
1032 if (cld->cld_cfg.cfg_flags & CFG_F_SERVER146)
1033 /* If we started from an old MDT, don't bother trying to
1034 get log updates from the MGS */
1037 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PROCESS_LOG, 20);
1039 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1041 CDEBUG(D_MGC, "Process log %s:%s from %d\n", cld->cld_logname,
1042 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1044 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1046 CERROR("missing llog context\n");
1050 /* I don't want mutliple processes running process_log at once --
1051 sounds like badness. It actually might be fine, as long as
1052 we're not trying to update from the same log
1053 simultaneously (in which case we should use a per-log sem.) */
1054 down(&llog_process_lock);
1056 /* Get the cfg lock on the llog */
1057 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1058 LCK_CR, &flags, NULL, NULL, NULL,
1059 cld, 0, NULL, &lockh);
1061 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1063 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1065 /* Copy the setup log locally if we can. Don't mess around if we're
1066 running an MGS though (logs are already local). */
1067 if (lctxt && lsi && (lsi->lsi_flags & LSI_SERVER) &&
1068 (lsi->lsi_srv_mnt == cli->cl_mgc_vfsmnt) &&
1069 !IS_MGS(lsi->lsi_ldd)) {
1070 push_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1073 /* Only try to copy log if we have the lock. */
1074 rc = mgc_copy_llog(mgc, ctxt, lctxt, cld->cld_logname);
1076 if (mgc_llog_is_empty(mgc, lctxt, cld->cld_logname)) {
1077 LCONSOLE_ERROR("Failed to get MGS log %s "
1078 "and no local copy.\n",
1080 GOTO(out_pop, rc = -ENOTCONN);
1082 LCONSOLE_WARN("Failed to get MGS log %s, using "
1083 "local copy.\n", cld->cld_logname);
1085 /* Now, whether we copied or not, start using the local llog.
1086 If we failed to copy, we'll start using whatever the old
1091 /* logname and instance info should be the same, so use our
1092 copy of the instance for the update. The cfg_last_idx will
1094 rc = class_config_parse_llog(ctxt, cld->cld_logname, &cld->cld_cfg);
1098 pop_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1100 /* Now drop the lock so MGS can revoke it */
1102 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1105 CERROR("Can't drop cfg lock: %d\n", rcl);
1108 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1109 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1111 up(&llog_process_lock);
1116 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1118 struct lustre_cfg *lcfg = buf;
1123 switch(cmd = lcfg->lcfg_command) {
1124 case LCFG_LOV_ADD_OBD: {
1125 /* Add any new target, not just osts */
1126 struct mgs_target_info *mti;
1128 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1129 sizeof(struct mgs_target_info))
1130 GOTO(out, rc = -EINVAL);
1132 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1133 CDEBUG(D_MGC, "add_target %s %#x\n",
1134 mti->mti_svname, mti->mti_flags);
1135 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1138 case LCFG_LOV_DEL_OBD:
1139 /* Remove target from the fs? */
1141 CERROR("lov_del_obd unimplemented\n");
1144 case LCFG_LOG_START: {
1145 struct config_llog_data *cld;
1146 struct config_llog_instance *cfg;
1147 struct super_block *sb;
1148 char *logname = lustre_cfg_string(lcfg, 1);
1149 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1150 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1152 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1155 /* We're only called through here on the initial mount */
1156 rc = config_log_add(logname, cfg, sb);
1159 cld = config_log_find(logname, cfg);
1166 /* FIXME only set this for old logs! Right now this forces
1167 us to always skip the "inside markers" check */
1168 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1170 rc = mgc_process_log(obd, cld);
1171 config_log_put(cld);
1175 case LCFG_LOG_END: {
1176 struct config_llog_instance *cfg = NULL;
1177 char *logname = lustre_cfg_string(lcfg, 1);
1178 if (lcfg->lcfg_bufcount >= 2)
1179 cfg = (struct config_llog_instance *)lustre_cfg_buf(
1181 rc = config_log_end(logname, cfg);
1185 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1186 GOTO(out, rc = -EINVAL);
1194 struct obd_ops mgc_obd_ops = {
1195 .o_owner = THIS_MODULE,
1196 .o_setup = mgc_setup,
1197 .o_precleanup = mgc_precleanup,
1198 .o_cleanup = mgc_cleanup,
1199 .o_add_conn = client_import_add_conn,
1200 .o_del_conn = client_import_del_conn,
1201 .o_connect = client_connect_import,
1202 .o_disconnect = client_disconnect_export,
1203 //.o_enqueue = mgc_enqueue,
1204 .o_cancel = mgc_cancel,
1205 //.o_iocontrol = mgc_iocontrol,
1206 .o_set_info_async = mgc_set_info_async,
1207 .o_import_event = mgc_import_event,
1208 .o_llog_init = mgc_llog_init,
1209 .o_llog_finish = mgc_llog_finish,
1210 .o_process_config = mgc_process_config,
1213 int __init mgc_init(void)
1215 return class_register_type(&mgc_obd_ops, NULL, LUSTRE_MGC_NAME);
1219 static void /*__exit*/ mgc_exit(void)
1221 class_unregister_type(LUSTRE_MGC_NAME);
1224 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1225 MODULE_DESCRIPTION("Lustre Management Client");
1226 MODULE_LICENSE("GPL");
1228 module_init(mgc_init);
1229 module_exit(mgc_exit);