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 <lprocfs_status.h>
45 #include <lustre_log.h>
46 #include <lustre_fsfilt.h>
47 #include <lustre_disk.h>
48 #include "mgc_internal.h"
50 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id)
55 CERROR("name too long: %s\n", name);
59 CERROR("missing name: %s\n", name);
62 memcpy(&resname, name, len);
64 memset(res_id, 0, sizeof(*res_id));
66 /* Always use the same endianness for the resid */
67 res_id->name[0] = cpu_to_le64(resname);
68 CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.8s)\n", name,
69 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
73 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id)
75 /* fsname is at most 8 chars long, maybe contain "-".
76 * e.g. "lustre", "CFS-000" */
77 return mgc_name2resid(fsname, strlen(fsname), res_id);
79 EXPORT_SYMBOL(mgc_fsname2resid);
81 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id)
86 /* logname consists of "fsname-nodetype".
87 * e.g. "lustre-MDT0001", "CFS-000-client" */
88 name_end = strrchr(logname, '-');
90 len = name_end - logname;
91 return mgc_name2resid(logname, len, res_id);
94 /********************** config llog list **********************/
95 static CFS_LIST_HEAD(config_llog_list);
96 static spinlock_t config_list_lock = SPIN_LOCK_UNLOCKED;
98 /* Take a reference to a config log */
99 static int config_log_get(struct config_llog_data *cld)
102 if (cld->cld_stopping)
104 atomic_inc(&cld->cld_refcount);
105 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
106 atomic_read(&cld->cld_refcount));
110 /* Drop a reference to a config log. When no longer referenced,
111 we can free the config log data */
112 static void config_log_put(struct config_llog_data *cld)
115 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
116 atomic_read(&cld->cld_refcount));
117 if (atomic_dec_and_test(&cld->cld_refcount)) {
118 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
119 class_export_put(cld->cld_mgcexp);
120 spin_lock(&config_list_lock);
121 list_del(&cld->cld_list_chain);
122 spin_unlock(&config_list_lock);
123 OBD_FREE(cld->cld_logname, strlen(cld->cld_logname) + 1);
124 if (cld->cld_cfg.cfg_instance != NULL)
125 OBD_FREE(cld->cld_cfg.cfg_instance,
126 strlen(cld->cld_cfg.cfg_instance) + 1);
127 OBD_FREE(cld, sizeof(*cld));
132 /* Find a config log by name */
133 static struct config_llog_data *config_log_find(char *logname,
134 struct config_llog_instance *cfg)
136 struct list_head *tmp;
137 struct config_llog_data *cld;
138 char *logid = logname;
139 int match_instance = 0;
142 if (cfg && cfg->cfg_instance) {
144 logid = cfg->cfg_instance;
147 CERROR("No log specified\n");
148 RETURN(ERR_PTR(-EINVAL));
151 spin_lock(&config_list_lock);
152 list_for_each(tmp, &config_llog_list) {
153 cld = list_entry(tmp, struct config_llog_data, cld_list_chain);
154 if (match_instance && cld->cld_cfg.cfg_instance &&
155 strcmp(logid, cld->cld_cfg.cfg_instance) == 0)
157 if (!match_instance &&
158 strcmp(logid, cld->cld_logname) == 0)
161 spin_unlock(&config_list_lock);
163 CDEBUG(D_CONFIG, "can't get log %s\n", logid);
164 RETURN(ERR_PTR(-ENOENT));
166 atomic_inc(&cld->cld_refcount);
167 spin_unlock(&config_list_lock);
171 /* Add this log to our list of active logs.
172 We have one active log per "mount" - client instance or servername.
173 Each instance may be at a different point in the log. */
174 static int config_log_add(char *logname, struct config_llog_instance *cfg,
175 struct super_block *sb)
177 struct config_llog_data *cld;
178 struct lustre_sb_info *lsi = s2lsi(sb);
182 CDEBUG(D_MGC, "adding config log %s:%s\n", logname, cfg->cfg_instance);
184 OBD_ALLOC(cld, sizeof(*cld));
187 OBD_ALLOC(cld->cld_logname, strlen(logname) + 1);
188 if (!cld->cld_logname) {
189 OBD_FREE(cld, sizeof(*cld));
192 strcpy(cld->cld_logname, logname);
194 cld->cld_cfg.cfg_last_idx = 0;
195 cld->cld_cfg.cfg_flags = 0;
196 cld->cld_cfg.cfg_sb = sb;
197 atomic_set(&cld->cld_refcount, 1);
199 /* Keep the mgc around until we are done */
200 cld->cld_mgcexp = class_export_get(lsi->lsi_mgc->obd_self_export);
202 if (cfg->cfg_instance != NULL) {
203 OBD_ALLOC(cld->cld_cfg.cfg_instance,
204 strlen(cfg->cfg_instance) + 1);
205 strcpy(cld->cld_cfg.cfg_instance, cfg->cfg_instance);
207 rc = mgc_logname2resid(logname, &cld->cld_resid);
208 spin_lock(&config_list_lock);
209 list_add(&cld->cld_list_chain, &config_llog_list);
210 spin_unlock(&config_list_lock);
220 /* Stop watching for updates on this log. */
221 static int config_log_end(char *logname, struct config_llog_instance *cfg)
223 struct config_llog_data *cld;
227 cld = config_log_find(logname, cfg);
229 RETURN(PTR_ERR(cld));
230 /* drop the ref from the find */
233 cld->cld_stopping = 1;
234 /* drop the start ref */
236 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
241 /* reenqueue any lost locks */
242 #define RQ_RUNNING 0x1
246 static int rq_state = 0;
247 static cfs_waitq_t rq_waitq;
249 static int mgc_process_log(struct obd_device *mgc,
250 struct config_llog_data *cld);
251 static int mgc_requeue_add(struct config_llog_data *cld, int later);
253 static int mgc_requeue_thread(void *data)
255 struct l_wait_info lwi_now, lwi_later;
256 struct config_llog_data *cld, *n;
257 char name[] = "ll_cfg_requeue";
261 ptlrpc_daemonize(name);
263 CDEBUG(D_MGC, "Starting requeue thread\n");
265 lwi_later = LWI_TIMEOUT(60 * HZ, NULL, NULL);
266 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP), &lwi_later);
268 /* Keep trying failed locks periodically */
269 spin_lock(&config_list_lock);
270 while (rq_state & (RQ_NOW | RQ_LATER)) {
271 /* Any new or requeued lostlocks will change the state */
272 rq_state &= ~(RQ_NOW | RQ_LATER);
273 spin_unlock(&config_list_lock);
275 /* Always wait a few seconds to allow the server who
276 caused the lock revocation to finish its setup, plus some
277 random so everyone doesn't try to reconnect at once. */
278 lwi_now = LWI_TIMEOUT(3 * HZ + (ll_rand() & 0xff) * (HZ / 100),
280 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi_now);
282 spin_lock(&config_list_lock);
283 list_for_each_entry_safe(cld, n, &config_llog_list,
285 spin_unlock(&config_list_lock);
286 if (cld->cld_lostlock) {
287 CDEBUG(D_MGC, "updating log %s\n",
289 cld->cld_lostlock = 0;
290 rc = mgc_process_log(cld->cld_mgcexp->exp_obd,
292 /* Whether we enqueued again or not in
293 mgc_process_log, we're done with the ref
294 from the old enqueue */
297 spin_lock(&config_list_lock);
299 spin_unlock(&config_list_lock);
301 /* Wait a bit to see if anyone else needs a requeue */
302 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
304 spin_lock(&config_list_lock);
306 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
307 rq_state &= ~RQ_RUNNING;
308 spin_unlock(&config_list_lock);
310 CDEBUG(D_MGC, "Ending requeue thread\n");
314 /* Add a cld to the list to requeue. Start the requeue thread if needed.
315 We are responsible for dropping the config log reference from here on out. */
316 static int mgc_requeue_add(struct config_llog_data *cld, int later)
320 CDEBUG(D_INFO, "log %s: requeue (l=%d r=%d sp=%d st=%x)\n",
321 cld->cld_logname, later, atomic_read(&cld->cld_refcount),
322 cld->cld_stopping, rq_state);
324 /* Hold lock for rq_state */
325 spin_lock(&config_list_lock);
326 cld->cld_lostlock = 1;
328 if (cld->cld_stopping || (rq_state & RQ_STOP)) {
329 spin_unlock(&config_list_lock);
334 if (!(rq_state & RQ_RUNNING)) {
335 LASSERT(rq_state == 0);
336 rq_state = RQ_RUNNING | (later ? RQ_LATER : RQ_NOW);
337 spin_unlock(&config_list_lock);
338 rc = cfs_kernel_thread(mgc_requeue_thread, 0,
339 CLONE_VM | CLONE_FILES);
341 CERROR("log %s: cannot start requeue thread (%d),"
342 "no more log updates!\n", cld->cld_logname, rc);
343 /* Drop the ref, since the rq thread won't */
344 cld->cld_lostlock = 0;
350 rq_state |= later ? RQ_LATER : RQ_NOW;
351 spin_unlock(&config_list_lock);
352 cfs_waitq_signal(&rq_waitq);
358 /********************** class fns **********************/
360 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb,
361 struct vfsmount *mnt)
363 struct lvfs_run_ctxt saved;
364 struct lustre_sb_info *lsi = s2lsi(sb);
365 struct client_obd *cli = &obd->u.cli;
366 struct dentry *dentry;
372 LASSERT(lsi->lsi_srv_mnt == mnt);
374 /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
375 down(&cli->cl_mgc_sem);
377 cleanup_group_info();
379 obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
380 if (IS_ERR(obd->obd_fsops)) {
381 up(&cli->cl_mgc_sem);
382 CERROR("No fstype %s rc=%ld\n", MT_STR(lsi->lsi_ldd),
383 PTR_ERR(obd->obd_fsops));
384 RETURN(PTR_ERR(obd->obd_fsops));
387 cli->cl_mgc_vfsmnt = mnt;
388 fsfilt_setup(obd, mnt->mnt_sb);
390 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
391 obd->obd_lvfs_ctxt.pwdmnt = mnt;
392 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
393 obd->obd_lvfs_ctxt.fs = get_ds();
395 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
396 dentry = lookup_one_len(MOUNT_CONFIGS_DIR, current->fs->pwd,
397 strlen(MOUNT_CONFIGS_DIR));
398 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
399 if (IS_ERR(dentry)) {
400 err = PTR_ERR(dentry);
401 CERROR("cannot lookup %s directory: rc = %d\n",
402 MOUNT_CONFIGS_DIR, err);
405 cli->cl_mgc_configs_dir = dentry;
407 /* We take an obd ref to insure that we can't get to mgc_cleanup
408 without calling mgc_fs_cleanup first. */
411 label = fsfilt_get_label(obd, mnt->mnt_sb);
413 CDEBUG(D_MGC, "MGC using disk labelled=%s\n", label);
415 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
419 fsfilt_put_ops(obd->obd_fsops);
420 obd->obd_fsops = NULL;
421 cli->cl_mgc_vfsmnt = NULL;
422 up(&cli->cl_mgc_sem);
426 static int mgc_fs_cleanup(struct obd_device *obd)
428 struct client_obd *cli = &obd->u.cli;
432 LASSERT(cli->cl_mgc_vfsmnt != NULL);
434 if (cli->cl_mgc_configs_dir != NULL) {
435 struct lvfs_run_ctxt saved;
436 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
437 l_dput(cli->cl_mgc_configs_dir);
438 cli->cl_mgc_configs_dir = NULL;
439 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
443 cli->cl_mgc_vfsmnt = NULL;
445 fsfilt_put_ops(obd->obd_fsops);
447 up(&cli->cl_mgc_sem);
452 static atomic_t mgc_count = ATOMIC_INIT(0);
453 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
459 case OBD_CLEANUP_EARLY:
461 case OBD_CLEANUP_EXPORTS:
462 if (atomic_dec_and_test(&mgc_count)) {
463 /* Kick the requeue waitq - cld's should all be
465 spin_lock(&config_list_lock);
467 spin_unlock(&config_list_lock);
468 cfs_waitq_signal(&rq_waitq);
470 rc = obd_llog_finish(obd, 0);
472 CERROR("failed to cleanup llogging subsystems\n");
474 case OBD_CLEANUP_SELF_EXP:
476 case OBD_CLEANUP_OBD:
482 static int mgc_cleanup(struct obd_device *obd)
484 struct client_obd *cli = &obd->u.cli;
488 LASSERT(cli->cl_mgc_vfsmnt == NULL);
490 /* COMPAT_146 - old config logs may have added profiles we don't
492 if (obd->obd_type->typ_refcnt <= 1)
493 /* Only for the last mgc */
494 class_del_profiles();
496 lprocfs_obd_cleanup(obd);
499 rc = client_obd_cleanup(obd);
503 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
505 struct lprocfs_static_vars lvars;
511 rc = client_obd_setup(obd, lcfg);
513 GOTO(err_decref, rc);
515 rc = obd_llog_init(obd, &obd->obd_olg, obd, 0, NULL, NULL);
517 CERROR("failed to setup llogging subsystems\n");
518 GOTO(err_cleanup, rc);
521 lprocfs_mgc_init_vars(&lvars);
522 lprocfs_obd_setup(obd, lvars.obd_vars);
524 spin_lock(&config_list_lock);
525 atomic_inc(&mgc_count);
526 if (atomic_read(&mgc_count) == 1) {
527 rq_state &= ~RQ_STOP;
528 cfs_waitq_init(&rq_waitq);
530 spin_unlock(&config_list_lock);
535 client_obd_cleanup(obd);
541 /* based on ll_mdc_blocking_ast */
542 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
543 void *data, int flag)
545 struct lustre_handle lockh;
546 struct config_llog_data *cld = (struct config_llog_data *)data;
551 case LDLM_CB_BLOCKING:
552 /* mgs wants the lock, give it up... */
553 LDLM_DEBUG(lock, "MGC blocking CB");
554 ldlm_lock2handle(lock, &lockh);
555 rc = ldlm_cli_cancel(&lockh);
557 case LDLM_CB_CANCELING: {
558 /* We've given up the lock, prepare ourselves to update. */
559 LDLM_DEBUG(lock, "MGC cancel CB");
561 CDEBUG(D_MGC, "Lock res "LPX64" (%.8s)\n",
562 lock->l_resource->lr_name.name[0],
563 (char *)&lock->l_resource->lr_name.name[0]);
566 CERROR("missing data, won't requeue\n");
569 /* Are we done with this log? */
570 if (cld->cld_stopping) {
571 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
576 /* Make sure not to re-enqueue when the mgc is stopping
577 (we get called from client_disconnect_export) */
578 if (!lock->l_conn_export ||
579 !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
580 CDEBUG(D_MGC, "log %s: disconnecting, won't requeue\n",
585 /* Did we fail to get the lock? */
586 if (lock->l_req_mode != lock->l_granted_mode) {
587 CDEBUG(D_MGC, "log %s: original grant failed, will "
588 "requeue later\n", cld->cld_logname);
589 /* Try to re-enqueue later */
590 rc = mgc_requeue_add(cld, 1);
594 rc = mgc_requeue_add(cld, 0);
603 CERROR("%s CB failed %d:\n", flag == LDLM_CB_BLOCKING ?
604 "blocking" : "cancel", rc);
605 LDLM_ERROR(lock, "MGC ast");
610 /* Send parameter to MGS*/
611 static int mgc_set_mgs_param(struct obd_export *exp,
612 struct mgs_send_param *msp)
614 struct ptlrpc_request *req;
615 struct mgs_send_param *req_msp, *rep_msp;
616 int size[] = { sizeof(struct ptlrpc_body), sizeof(*req_msp) };
617 int rep_size[] = { sizeof(struct ptlrpc_body), sizeof(*msp) };
621 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MGS_VERSION,
622 MGS_SET_INFO, 2, size, NULL);
626 req_msp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*req_msp));
630 memcpy(req_msp, msp, sizeof(*req_msp));
631 ptlrpc_req_set_repsize(req, 2, rep_size);
632 rc = ptlrpc_queue_wait(req);
634 rep_msp = lustre_swab_repbuf(req, REPLY_REC_OFF,
635 sizeof(*rep_msp), NULL);
636 memcpy(msp, rep_msp, sizeof(*rep_msp));
639 ptlrpc_req_finished(req);
644 /* Take a config lock so we can get cancel notifications */
645 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
646 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
647 int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
648 void *data, __u32 lvb_len, void *lvb_swabber,
649 struct lustre_handle *lockh)
651 struct config_llog_data *cld = (struct config_llog_data *)data;
652 struct ldlm_enqueue_info einfo = { type, mode, mgc_blocking_ast,
653 ldlm_completion_ast, NULL, data};
658 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
659 cld->cld_resid.name[0]);
661 /* We can only drop this config log ref when we drop the lock */
662 if (config_log_get(cld))
663 RETURN(ELDLM_LOCK_ABORTED);
665 /* We need a callback for every lockholder, so don't try to
666 ldlm_lock_match (see rev 1.1.2.11.2.47) */
668 rc = ldlm_cli_enqueue(exp, NULL, &einfo, &cld->cld_resid,
669 NULL, flags, NULL, 0, NULL, lockh, 0);
670 /* A failed enqueue should still call the mgc_blocking_ast,
671 where it will be requeued if needed ("grant failed"). */
676 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
677 __u32 mode, struct lustre_handle *lockh)
681 ldlm_lock_decref(lockh, mode);
687 static int mgc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
688 void *karg, void *uarg)
690 struct obd_device *obd = exp->exp_obd;
691 struct obd_ioctl_data *data = karg;
692 struct llog_ctxt *ctxt;
693 struct lvfs_run_ctxt saved;
697 if (!try_module_get(THIS_MODULE)) {
698 CERROR("Can't get module. Is it alive?");
702 /* REPLicator context */
703 case OBD_IOC_PARSE: {
704 CERROR("MGC parsing llog %s\n", data->ioc_inlbuf1);
705 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
706 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
710 case OBD_IOC_LLOG_INFO:
711 case OBD_IOC_LLOG_PRINT: {
712 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
713 rc = llog_ioctl(ctxt, cmd, data);
718 /* ORIGinator context */
719 case OBD_IOC_DUMP_LOG: {
720 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
721 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
722 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
723 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
730 CERROR("mgc_ioctl(): unrecognised ioctl %#x\n", cmd);
731 GOTO(out, rc = -ENOTTY);
734 module_put(THIS_MODULE);
740 /* Send target_reg message to MGS */
741 static int mgc_target_register(struct obd_export *exp,
742 struct mgs_target_info *mti)
744 struct ptlrpc_request *req;
745 struct mgs_target_info *req_mti, *rep_mti;
749 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
750 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
755 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
756 memcpy(req_mti, mti, sizeof(*req_mti));
758 ptlrpc_request_set_replen(req);
760 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
762 rc = ptlrpc_queue_wait(req);
764 rep_mti = req_capsule_server_get(&req->rq_pill,
765 &RMF_MGS_TARGET_INFO);
766 memcpy(mti, rep_mti, sizeof(*rep_mti));
767 CDEBUG(D_MGC, "register %s got index = %d\n",
768 mti->mti_svname, mti->mti_stripe_index);
770 ptlrpc_req_finished(req);
775 int mgc_set_info_async(struct obd_export *exp, obd_count keylen,
776 void *key, obd_count vallen, void *val,
777 struct ptlrpc_request_set *set)
779 struct obd_import *imp = class_exp2cliimp(exp);
783 /* Try to "recover" the initial connection; i.e. retry */
784 if (KEY_IS(KEY_INIT_RECOV)) {
785 if (vallen != sizeof(int))
787 spin_lock(&imp->imp_lock);
788 imp->imp_initial_recov = *(int *)val;
789 spin_unlock(&imp->imp_lock);
790 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
791 exp->exp_obd->obd_name, imp->imp_initial_recov);
794 /* Turn off initial_recov after we try all backup servers once */
795 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
797 if (vallen != sizeof(int))
800 spin_lock(&imp->imp_lock);
801 imp->imp_initial_recov_bk = value > 0;
802 /* Even after the initial connection, give up all comms if
803 nobody answers the first time. */
804 imp->imp_recon_bk = 1;
805 spin_unlock(&imp->imp_lock);
806 CDEBUG(D_MGC, "InitRecov %s %d/%d:d%d:i%d:r%d:or%d:%s\n",
807 imp->imp_obd->obd_name, value, imp->imp_initial_recov,
808 imp->imp_deactive, imp->imp_invalid,
809 imp->imp_replayable, imp->imp_obd->obd_replayable,
810 ptlrpc_import_state_name(imp->imp_state));
811 /* Resurrect if we previously died */
812 if (imp->imp_invalid || value > 1)
813 ptlrpc_reconnect_import(imp);
816 /* FIXME move this to mgc_process_config */
817 if (KEY_IS(KEY_REGISTER_TARGET)) {
818 struct mgs_target_info *mti;
819 if (vallen != sizeof(struct mgs_target_info))
821 mti = (struct mgs_target_info *)val;
822 CDEBUG(D_MGC, "register_target %s %#x\n",
823 mti->mti_svname, mti->mti_flags);
824 rc = mgc_target_register(exp, mti);
827 if (KEY_IS(KEY_SET_FS)) {
828 struct super_block *sb = (struct super_block *)val;
829 struct lustre_sb_info *lsi;
830 if (vallen != sizeof(struct super_block))
833 rc = mgc_fs_setup(exp->exp_obd, sb, lsi->lsi_srv_mnt);
835 CERROR("set_fs got %d\n", rc);
839 if (KEY_IS(KEY_CLEAR_FS)) {
842 rc = mgc_fs_cleanup(exp->exp_obd);
844 CERROR("clear_fs got %d\n", rc);
848 if (KEY_IS(KEY_SET_INFO)) {
849 struct mgs_send_param *msp;
851 msp = (struct mgs_send_param *)val;
852 rc = mgc_set_mgs_param(exp, msp);
859 static int mgc_import_event(struct obd_device *obd,
860 struct obd_import *imp,
861 enum obd_import_event event)
865 LASSERT(imp->imp_obd == obd);
866 CDEBUG(D_MGC, "import event %#x\n", event);
869 case IMP_EVENT_DISCON:
870 /* MGC imports should not wait for recovery */
872 case IMP_EVENT_INACTIVE:
874 case IMP_EVENT_INVALIDATE: {
875 struct ldlm_namespace *ns = obd->obd_namespace;
876 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
879 case IMP_EVENT_ACTIVE:
880 LCONSOLE_WARN("%s: Reactivating import\n", obd->obd_name);
881 /* Clearing obd_no_recov allows us to continue pinging */
882 obd->obd_no_recov = 0;
887 CERROR("Unknown import event %#x\n", event);
893 static int mgc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
894 struct obd_device *tgt, int count,
895 struct llog_catid *logid, struct obd_uuid *uuid)
897 struct llog_ctxt *ctxt;
901 LASSERT(olg == &obd->obd_olg);
903 rc = llog_setup(obd, olg, LLOG_CONFIG_ORIG_CTXT, tgt, 0, NULL,
908 rc = llog_setup(obd, olg, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
911 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
912 llog_initiator_connect(ctxt);
919 static int mgc_llog_finish(struct obd_device *obd, int count)
921 struct llog_ctxt *ctxt;
925 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
927 rc = llog_cleanup(ctxt);
929 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
931 rc2 = llog_cleanup(ctxt);
939 /* identical to mgs_log_is_empty */
940 static int mgc_llog_is_empty(struct obd_device *obd, struct llog_ctxt *ctxt,
943 struct lvfs_run_ctxt saved;
944 struct llog_handle *llh;
947 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
948 rc = llog_create(ctxt, &llh, NULL, name);
950 llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
951 rc = llog_get_size(llh);
954 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
955 /* header is record 1 */
959 static int mgc_copy_handler(struct llog_handle *llh, struct llog_rec_hdr *rec,
962 struct llog_rec_hdr local_rec = *rec;
963 struct llog_handle *local_llh = (struct llog_handle *)data;
964 char *cfg_buf = (char*) (rec + 1);
965 struct lustre_cfg *lcfg;
969 /* Append all records */
970 local_rec.lrh_len -= sizeof(*rec) + sizeof(struct llog_rec_tail);
971 rc = llog_write_rec(local_llh, &local_rec, NULL, 0,
972 (void *)cfg_buf, -1);
974 lcfg = (struct lustre_cfg *)cfg_buf;
975 CDEBUG(D_INFO, "idx=%d, rc=%d, len=%d, cmd %x %s %s\n",
976 rec->lrh_index, rc, rec->lrh_len, lcfg->lcfg_command,
977 lustre_cfg_string(lcfg, 0), lustre_cfg_string(lcfg, 1));
982 /* Copy a remote log locally */
983 static int mgc_copy_llog(struct obd_device *obd, struct llog_ctxt *rctxt,
984 struct llog_ctxt *lctxt, char *logname)
986 struct llog_handle *local_llh, *remote_llh;
987 struct obd_uuid *uuid;
992 /* Write new log to a temp name, then vfs_rename over logname
993 upon successful completion. */
995 OBD_ALLOC(temp_log, strlen(logname) + 1);
998 sprintf(temp_log, "%sT", logname);
1000 /* Make sure there's no old temp log */
1001 rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1004 rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, NULL);
1007 rc = llog_destroy(local_llh);
1008 llog_free_handle(local_llh);
1012 /* open local log */
1013 rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1017 /* set the log header uuid for fun */
1018 OBD_ALLOC_PTR(uuid);
1019 obd_str2uuid(uuid, logname);
1020 rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, uuid);
1023 GOTO(out_closel, rc);
1025 /* open remote log */
1026 rc = llog_create(rctxt, &remote_llh, NULL, logname);
1028 GOTO(out_closel, rc);
1029 rc = llog_init_handle(remote_llh, LLOG_F_IS_PLAIN, NULL);
1031 GOTO(out_closer, rc);
1033 /* Copy remote log */
1034 rc = llog_process(remote_llh, mgc_copy_handler,(void *)local_llh, NULL);
1037 rc2 = llog_close(remote_llh);
1041 rc2 = llog_close(local_llh);
1045 /* We've copied the remote log to the local temp log, now
1046 replace the old local log with the temp log. */
1048 struct client_obd *cli = &obd->u.cli;
1050 LASSERT(cli->cl_mgc_configs_dir);
1051 rc = lustre_rename(cli->cl_mgc_configs_dir, temp_log, logname);
1053 CDEBUG(D_MGC, "Copied remote log %s (%d)\n", logname, rc);
1056 CERROR("Failed to copy remote log %s (%d)\n", logname, rc);
1057 OBD_FREE(temp_log, strlen(logname) + 1);
1061 DECLARE_MUTEX(llog_process_lock);
1063 /* Get a config log from the MGS and process it.
1064 This func is called for both clients and servers. */
1065 static int mgc_process_log(struct obd_device *mgc,
1066 struct config_llog_data *cld)
1068 struct llog_ctxt *ctxt, *lctxt;
1069 struct lustre_handle lockh;
1070 struct client_obd *cli = &mgc->u.cli;
1071 struct lvfs_run_ctxt saved;
1072 struct lustre_sb_info *lsi;
1073 int rc = 0, rcl, flags = 0, must_pop = 0;
1076 if (!cld || !cld->cld_cfg.cfg_sb) {
1077 /* This should never happen */
1078 CERROR("Missing cld, aborting log update\n");
1081 if (cld->cld_stopping)
1084 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PROCESS_LOG, 20);
1086 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1088 CDEBUG(D_MGC, "Process log %s:%s from %d\n", cld->cld_logname,
1089 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1091 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1093 CERROR("missing llog context\n");
1097 /* I don't want mutliple processes running process_log at once --
1098 sounds like badness. It actually might be fine, as long as
1099 we're not trying to update from the same log
1100 simultaneously (in which case we should use a per-log sem.) */
1101 down(&llog_process_lock);
1103 /* Get the cfg lock on the llog */
1104 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1105 LCK_CR, &flags, NULL, NULL, NULL,
1106 cld, 0, NULL, &lockh);
1108 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1110 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1112 /* Copy the setup log locally if we can. Don't mess around if we're
1113 running an MGS though (logs are already local). */
1114 if (lctxt && lsi && (lsi->lsi_flags & LSI_SERVER) &&
1115 (lsi->lsi_srv_mnt == cli->cl_mgc_vfsmnt) &&
1116 !IS_MGS(lsi->lsi_ldd)) {
1117 push_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1120 /* Only try to copy log if we have the lock. */
1121 rc = mgc_copy_llog(mgc, ctxt, lctxt, cld->cld_logname);
1123 if (mgc_llog_is_empty(mgc, lctxt, cld->cld_logname)) {
1124 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1125 "log %s and no local copy."
1126 "\n", cld->cld_logname);
1127 GOTO(out_pop, rc = -ENOTCONN);
1129 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1130 "copy for now, will try to update later.\n",
1133 /* Now, whether we copied or not, start using the local llog.
1134 If we failed to copy, we'll start using whatever the old
1136 llog_ctxt_put(ctxt);
1140 /* logname and instance info should be the same, so use our
1141 copy of the instance for the update. The cfg_last_idx will
1143 rc = class_config_parse_llog(ctxt, cld->cld_logname, &cld->cld_cfg);
1145 llog_ctxt_put(ctxt);
1147 llog_ctxt_put(lctxt);
1149 pop_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1151 /* Now drop the lock so MGS can revoke it */
1153 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1156 CERROR("Can't drop cfg lock: %d\n", rcl);
1159 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1160 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1162 up(&llog_process_lock);
1167 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1169 struct lustre_cfg *lcfg = buf;
1174 switch(cmd = lcfg->lcfg_command) {
1175 case LCFG_LOV_ADD_OBD: {
1176 /* Add any new target, not just osts */
1177 struct mgs_target_info *mti;
1179 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1180 sizeof(struct mgs_target_info))
1181 GOTO(out, rc = -EINVAL);
1183 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1184 CDEBUG(D_MGC, "add_target %s %#x\n",
1185 mti->mti_svname, mti->mti_flags);
1186 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1189 case LCFG_LOV_DEL_OBD:
1190 /* Remove target from the fs? */
1192 CERROR("lov_del_obd unimplemented\n");
1195 case LCFG_LOG_START: {
1196 struct config_llog_data *cld;
1197 struct config_llog_instance *cfg;
1198 struct super_block *sb;
1199 char *logname = lustre_cfg_string(lcfg, 1);
1200 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1201 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1203 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1206 /* We're only called through here on the initial mount */
1207 rc = config_log_add(logname, cfg, sb);
1210 cld = config_log_find(logname, cfg);
1217 /* FIXME only set this for old logs! Right now this forces
1218 us to always skip the "inside markers" check */
1219 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1221 rc = mgc_process_log(obd, cld);
1222 config_log_put(cld);
1226 case LCFG_LOG_END: {
1227 struct config_llog_instance *cfg = NULL;
1228 char *logname = lustre_cfg_string(lcfg, 1);
1229 if (lcfg->lcfg_bufcount >= 2)
1230 cfg = (struct config_llog_instance *)lustre_cfg_buf(
1232 rc = config_log_end(logname, cfg);
1236 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1237 GOTO(out, rc = -EINVAL);
1245 struct obd_ops mgc_obd_ops = {
1246 .o_owner = THIS_MODULE,
1247 .o_setup = mgc_setup,
1248 .o_precleanup = mgc_precleanup,
1249 .o_cleanup = mgc_cleanup,
1250 .o_add_conn = client_import_add_conn,
1251 .o_del_conn = client_import_del_conn,
1252 .o_connect = client_connect_import,
1253 .o_disconnect = client_disconnect_export,
1254 //.o_enqueue = mgc_enqueue,
1255 .o_cancel = mgc_cancel,
1256 //.o_iocontrol = mgc_iocontrol,
1257 .o_set_info_async = mgc_set_info_async,
1258 .o_import_event = mgc_import_event,
1259 .o_llog_init = mgc_llog_init,
1260 .o_llog_finish = mgc_llog_finish,
1261 .o_process_config = mgc_process_config,
1264 int __init mgc_init(void)
1266 return class_register_type(&mgc_obd_ops, NULL, NULL,
1267 LUSTRE_MGC_NAME, NULL);
1271 static void /*__exit*/ mgc_exit(void)
1273 class_unregister_type(LUSTRE_MGC_NAME);
1276 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1277 MODULE_DESCRIPTION("Lustre Management Client");
1278 MODULE_LICENSE("GPL");
1280 module_init(mgc_init);
1281 module_exit(mgc_exit);