1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mgc/mgc_request.c
38 * Author: Nathan Rutman <nathan@clusterfs.com>
42 # define EXPORT_SYMTAB
44 #define DEBUG_SUBSYSTEM S_MGC
45 #define D_MGC D_CONFIG /*|D_WARNING*/
48 # include <linux/module.h>
49 # include <linux/pagemap.h>
50 # include <linux/miscdevice.h>
51 # include <linux/init.h>
53 # include <liblustre.h>
56 #include <obd_class.h>
57 #include <lustre_dlm.h>
58 #include <lprocfs_status.h>
59 #include <lustre_log.h>
60 #include <lustre_fsfilt.h>
61 #include <lustre_disk.h>
62 #include "mgc_internal.h"
64 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id)
69 CERROR("name too long: %s\n", name);
73 CERROR("missing name: %s\n", name);
76 memcpy(&resname, name, len);
78 memset(res_id, 0, sizeof(*res_id));
80 /* Always use the same endianness for the resid */
81 res_id->name[0] = cpu_to_le64(resname);
82 CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.8s)\n", name,
83 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
87 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id)
89 /* fsname is at most 8 chars long, maybe contain "-".
90 * e.g. "lustre", "SUN-000" */
91 return mgc_name2resid(fsname, strlen(fsname), res_id);
93 EXPORT_SYMBOL(mgc_fsname2resid);
95 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id)
100 /* logname consists of "fsname-nodetype".
101 * e.g. "lustre-MDT0001", "SUN-000-client" */
102 name_end = strrchr(logname, '-');
104 len = name_end - logname;
105 return mgc_name2resid(logname, len, res_id);
109 /********************** config llog list **********************/
110 static struct list_head config_llog_list = LIST_HEAD_INIT(config_llog_list);
111 static spinlock_t config_list_lock = SPIN_LOCK_UNLOCKED;
113 /* Take a reference to a config log */
114 static int config_log_get(struct config_llog_data *cld)
117 if (cld->cld_stopping)
119 atomic_inc(&cld->cld_refcount);
120 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
121 atomic_read(&cld->cld_refcount));
125 /* Drop a reference to a config log. When no longer referenced,
126 we can free the config log data */
127 static void config_log_put(struct config_llog_data *cld)
130 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
131 atomic_read(&cld->cld_refcount));
132 if (atomic_dec_and_test(&cld->cld_refcount)) {
133 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
134 class_export_put(cld->cld_mgcexp);
135 spin_lock(&config_list_lock);
136 list_del(&cld->cld_list_chain);
137 spin_unlock(&config_list_lock);
138 OBD_FREE(cld->cld_logname, strlen(cld->cld_logname) + 1);
139 if (cld->cld_cfg.cfg_instance != NULL)
140 OBD_FREE(cld->cld_cfg.cfg_instance,
141 strlen(cld->cld_cfg.cfg_instance) + 1);
142 OBD_FREE(cld, sizeof(*cld));
147 /* Find a config log by name */
148 static struct config_llog_data *config_log_find(char *logname,
149 struct config_llog_instance *cfg)
151 struct list_head *tmp;
152 struct config_llog_data *cld;
153 char *logid = logname;
154 int match_instance = 0;
157 if (cfg && cfg->cfg_instance) {
159 logid = cfg->cfg_instance;
162 CERROR("No log specified\n");
163 RETURN(ERR_PTR(-EINVAL));
166 spin_lock(&config_list_lock);
167 list_for_each(tmp, &config_llog_list) {
168 cld = list_entry(tmp, struct config_llog_data, cld_list_chain);
169 if (match_instance && cld->cld_cfg.cfg_instance &&
170 strcmp(logid, cld->cld_cfg.cfg_instance) == 0)
172 if (!match_instance &&
173 strcmp(logid, cld->cld_logname) == 0)
176 spin_unlock(&config_list_lock);
178 CDEBUG(D_CONFIG, "can't get log %s\n", logid);
179 RETURN(ERR_PTR(-ENOENT));
181 atomic_inc(&cld->cld_refcount);
182 spin_unlock(&config_list_lock);
186 /* Add this log to our list of active logs.
187 We have one active log per "mount" - client instance or servername.
188 Each instance may be at a different point in the log. */
189 static int config_log_add(char *logname, struct config_llog_instance *cfg,
190 struct super_block *sb)
192 struct config_llog_data *cld;
193 struct lustre_sb_info *lsi = s2lsi(sb);
197 CDEBUG(D_MGC, "adding config log %s:%s\n", logname, cfg->cfg_instance);
199 OBD_ALLOC(cld, sizeof(*cld));
202 OBD_ALLOC(cld->cld_logname, strlen(logname) + 1);
203 if (!cld->cld_logname) {
204 OBD_FREE(cld, sizeof(*cld));
207 strcpy(cld->cld_logname, logname);
209 cld->cld_cfg.cfg_last_idx = 0;
210 cld->cld_cfg.cfg_flags = 0;
211 cld->cld_cfg.cfg_sb = sb;
212 atomic_set(&cld->cld_refcount, 1);
214 /* Keep the mgc around until we are done */
215 cld->cld_mgcexp = class_export_get(lsi->lsi_mgc->obd_self_export);
217 if (cfg->cfg_instance != NULL) {
218 OBD_ALLOC(cld->cld_cfg.cfg_instance,
219 strlen(cfg->cfg_instance) + 1);
220 strcpy(cld->cld_cfg.cfg_instance, cfg->cfg_instance);
222 rc = mgc_logname2resid(logname, &cld->cld_resid);
223 spin_lock(&config_list_lock);
224 list_add(&cld->cld_list_chain, &config_llog_list);
225 spin_unlock(&config_list_lock);
235 DECLARE_MUTEX(llog_process_lock);
237 /* Stop watching for updates on this log. */
238 static int config_log_end(char *logname, struct config_llog_instance *cfg)
240 struct config_llog_data *cld;
244 cld = config_log_find(logname, cfg);
246 RETURN(PTR_ERR(cld));
247 /* drop the ref from the find */
250 down(&llog_process_lock);
251 cld->cld_stopping = 1;
252 up(&llog_process_lock);
254 /* drop the start ref */
256 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
261 /* reenqueue any lost locks */
262 #define RQ_RUNNING 0x1
266 static int rq_state = 0;
267 static cfs_waitq_t rq_waitq;
269 static int mgc_process_log(struct obd_device *mgc,
270 struct config_llog_data *cld);
272 static int mgc_requeue_thread(void *data)
274 struct l_wait_info lwi_now, lwi_later;
275 struct config_llog_data *cld, *n;
276 char name[] = "ll_cfg_requeue";
282 CDEBUG(D_MGC, "Starting requeue thread\n");
284 lwi_later = LWI_TIMEOUT(60 * HZ, NULL, NULL);
285 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP), &lwi_later);
287 /* Keep trying failed locks periodically */
288 spin_lock(&config_list_lock);
289 while (rq_state & (RQ_NOW | RQ_LATER)) {
290 /* Any new or requeued lostlocks will change the state */
291 rq_state &= ~(RQ_NOW | RQ_LATER);
292 spin_unlock(&config_list_lock);
294 /* Always wait a few seconds to allow the server who
295 caused the lock revocation to finish its setup, plus some
296 random so everyone doesn't try to reconnect at once. */
297 lwi_now = LWI_TIMEOUT(3 * HZ + (ll_rand() & 0xff) * (HZ / 100),
299 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi_now);
301 spin_lock(&config_list_lock);
302 list_for_each_entry_safe(cld, n, &config_llog_list,
304 spin_unlock(&config_list_lock);
306 if (cld->cld_lostlock) {
307 CDEBUG(D_MGC, "updating log %s\n",
309 cld->cld_lostlock = 0;
310 rc = mgc_process_log(cld->cld_mgcexp->exp_obd,
312 /* Whether we enqueued again or not in
313 mgc_process_log, we're done with the ref
314 from the old enqueue */
318 spin_lock(&config_list_lock);
320 spin_unlock(&config_list_lock);
322 /* Wait a bit to see if anyone else needs a requeue */
323 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
325 spin_lock(&config_list_lock);
327 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
328 rq_state &= ~RQ_RUNNING;
329 spin_unlock(&config_list_lock);
331 CDEBUG(D_MGC, "Ending requeue thread\n");
335 /* Add a cld to the list to requeue. Start the requeue thread if needed.
336 We are responsible for dropping the config log reference from here on out. */
337 static int mgc_requeue_add(struct config_llog_data *cld, int later)
341 CDEBUG(D_INFO, "log %s: requeue (l=%d r=%d sp=%d st=%x)\n",
342 cld->cld_logname, later, atomic_read(&cld->cld_refcount),
343 cld->cld_stopping, rq_state);
345 /* Hold lock for rq_state */
346 spin_lock(&config_list_lock);
348 if (cld->cld_stopping || (rq_state & RQ_STOP)) {
349 spin_unlock(&config_list_lock);
354 cld->cld_lostlock = 1;
356 if (!(rq_state & RQ_RUNNING)) {
357 LASSERT(rq_state == 0);
358 rq_state = RQ_RUNNING | (later ? RQ_LATER : RQ_NOW);
359 spin_unlock(&config_list_lock);
360 rc = cfs_kernel_thread(mgc_requeue_thread, 0,
361 CLONE_VM | CLONE_FILES);
363 CERROR("log %s: cannot start requeue thread (%d),"
364 "no more log updates!\n", cld->cld_logname, rc);
365 /* Drop the ref, since the rq thread won't */
366 cld->cld_lostlock = 0;
372 rq_state |= later ? RQ_LATER : RQ_NOW;
373 spin_unlock(&config_list_lock);
374 cfs_waitq_signal(&rq_waitq);
380 /********************** class fns **********************/
382 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb,
383 struct vfsmount *mnt)
385 struct lvfs_run_ctxt saved;
386 struct lustre_sb_info *lsi = s2lsi(sb);
387 struct client_obd *cli = &obd->u.cli;
388 struct dentry *dentry;
394 LASSERT(lsi->lsi_srv_mnt == mnt);
396 /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
397 down(&cli->cl_mgc_sem);
399 cleanup_group_info();
401 obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
402 if (IS_ERR(obd->obd_fsops)) {
403 up(&cli->cl_mgc_sem);
404 CERROR("No fstype %s rc=%ld\n", MT_STR(lsi->lsi_ldd),
405 PTR_ERR(obd->obd_fsops));
406 RETURN(PTR_ERR(obd->obd_fsops));
409 cli->cl_mgc_vfsmnt = mnt;
410 fsfilt_setup(obd, mnt->mnt_sb);
412 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
413 obd->obd_lvfs_ctxt.pwdmnt = mnt;
414 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
415 obd->obd_lvfs_ctxt.fs = get_ds();
417 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
418 dentry = lookup_one_len(MOUNT_CONFIGS_DIR, cfs_fs_pwd(current->fs),
419 strlen(MOUNT_CONFIGS_DIR));
420 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
421 if (IS_ERR(dentry)) {
422 err = PTR_ERR(dentry);
423 CERROR("cannot lookup %s directory: rc = %d\n",
424 MOUNT_CONFIGS_DIR, err);
427 cli->cl_mgc_configs_dir = dentry;
429 /* We take an obd ref to insure that we can't get to mgc_cleanup
430 without calling mgc_fs_cleanup first. */
433 label = fsfilt_get_label(obd, mnt->mnt_sb);
435 CDEBUG(D_MGC, "MGC using disk labelled=%s\n", label);
437 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
441 fsfilt_put_ops(obd->obd_fsops);
442 obd->obd_fsops = NULL;
443 cli->cl_mgc_vfsmnt = NULL;
444 up(&cli->cl_mgc_sem);
448 static int mgc_fs_cleanup(struct obd_device *obd)
450 struct client_obd *cli = &obd->u.cli;
454 LASSERT(cli->cl_mgc_vfsmnt != NULL);
456 if (cli->cl_mgc_configs_dir != NULL) {
457 struct lvfs_run_ctxt saved;
458 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
459 l_dput(cli->cl_mgc_configs_dir);
460 cli->cl_mgc_configs_dir = NULL;
461 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
465 cli->cl_mgc_vfsmnt = NULL;
467 fsfilt_put_ops(obd->obd_fsops);
469 up(&cli->cl_mgc_sem);
474 static atomic_t mgc_count = ATOMIC_INIT(0);
475 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
481 case OBD_CLEANUP_EARLY:
483 case OBD_CLEANUP_EXPORTS:
484 if (atomic_dec_and_test(&mgc_count)) {
485 /* Kick the requeue waitq - cld's should all be
487 spin_lock(&config_list_lock);
489 spin_unlock(&config_list_lock);
490 cfs_waitq_signal(&rq_waitq);
492 /* client import will not have been cleaned. */
493 down_write(&obd->u.cli.cl_sem);
494 if (obd->u.cli.cl_import) {
495 struct obd_import *imp;
496 imp = obd->u.cli.cl_import;
497 CERROR("client import never connected\n");
498 class_destroy_import(imp);
499 obd->u.cli.cl_import = NULL;
501 up_write(&obd->u.cli.cl_sem);
503 rc = obd_llog_finish(obd, 0);
505 CERROR("failed to cleanup llogging subsystems\n");
507 case OBD_CLEANUP_SELF_EXP:
509 case OBD_CLEANUP_OBD:
515 static int mgc_cleanup(struct obd_device *obd)
517 struct client_obd *cli = &obd->u.cli;
521 LASSERT(cli->cl_mgc_vfsmnt == NULL);
523 /* COMPAT_146 - old config logs may have added profiles we don't
525 if (obd->obd_type->typ_refcnt <= 1)
526 /* Only for the last mgc */
527 class_del_profiles();
529 lprocfs_obd_cleanup(obd);
532 rc = client_obd_cleanup(obd);
536 static int mgc_setup(struct obd_device *obd, obd_count len, void *buf)
538 struct lprocfs_static_vars lvars;
544 rc = client_obd_setup(obd, len, buf);
546 GOTO(err_decref, rc);
548 rc = obd_llog_init(obd, obd, NULL);
550 CERROR("failed to setup llogging subsystems\n");
551 GOTO(err_cleanup, rc);
554 lprocfs_mgc_init_vars(&lvars);
555 lprocfs_obd_setup(obd, lvars.obd_vars);
557 spin_lock(&config_list_lock);
558 atomic_inc(&mgc_count);
559 if (atomic_read(&mgc_count) == 1) {
560 rq_state &= ~RQ_STOP;
561 cfs_waitq_init(&rq_waitq);
563 spin_unlock(&config_list_lock);
568 client_obd_cleanup(obd);
574 /* based on ll_mdc_blocking_ast */
575 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
576 void *data, int flag)
578 struct lustre_handle lockh;
579 struct config_llog_data *cld = (struct config_llog_data *)data;
584 case LDLM_CB_BLOCKING:
585 /* mgs wants the lock, give it up... */
586 LDLM_DEBUG(lock, "MGC blocking CB");
587 ldlm_lock2handle(lock, &lockh);
588 rc = ldlm_cli_cancel(&lockh);
590 case LDLM_CB_CANCELING: {
591 /* We've given up the lock, prepare ourselves to update. */
592 LDLM_DEBUG(lock, "MGC cancel CB");
594 CDEBUG(D_MGC, "Lock res "LPX64" (%.8s)\n",
595 lock->l_resource->lr_name.name[0],
596 (char *)&lock->l_resource->lr_name.name[0]);
599 CERROR("missing data, won't requeue\n");
602 /* Are we done with this log? */
603 if (cld->cld_stopping) {
604 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
609 /* Make sure not to re-enqueue when the mgc is stopping
610 (we get called from client_disconnect_export) */
611 if (!lock->l_conn_export ||
612 !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
613 CDEBUG(D_MGC, "log %s: disconnecting, won't requeue\n",
618 /* Did we fail to get the lock? */
619 if (lock->l_req_mode != lock->l_granted_mode) {
620 CDEBUG(D_MGC, "log %s: original grant failed, will "
621 "requeue later\n", cld->cld_logname);
622 /* Try to re-enqueue later */
623 rc = mgc_requeue_add(cld, 1);
627 rc = mgc_requeue_add(cld, 0);
636 CERROR("%s CB failed %d:\n", flag == LDLM_CB_BLOCKING ?
637 "blocking" : "cancel", rc);
638 LDLM_ERROR(lock, "MGC ast");
643 /* Take a config lock so we can get cancel notifications */
644 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
645 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
646 int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
647 void *data, __u32 lvb_len, void *lvb_swabber,
648 struct lustre_handle *lockh)
650 struct config_llog_data *cld = (struct config_llog_data *)data;
651 struct ldlm_enqueue_info einfo = { type, mode, mgc_blocking_ast,
652 ldlm_completion_ast, NULL, data};
657 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
658 cld->cld_resid.name[0]);
660 /* We can only drop this config log ref when we drop the lock */
661 if (config_log_get(cld))
662 RETURN(ELDLM_LOCK_ABORTED);
664 /* We need a callback for every lockholder, so don't try to
665 ldlm_lock_match (see rev 1.1.2.11.2.47) */
667 rc = ldlm_cli_enqueue(exp, NULL, &einfo, cld->cld_resid,
668 NULL, flags, NULL, 0, NULL, lockh, 0);
669 /* A failed enqueue should still call the mgc_blocking_ast,
670 where it will be requeued if needed ("grant failed"). */
675 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
676 __u32 mode, struct lustre_handle *lockh, int flags,
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;
746 int size[] = { sizeof(struct ptlrpc_body), sizeof(*req_mti) };
747 int rep_size[] = { sizeof(struct ptlrpc_body), sizeof(*mti) };
751 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MGS_VERSION,
752 MGS_TARGET_REG, 2, size, NULL);
756 req_mti = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*req_mti));
758 ptlrpc_req_finished(req);
762 memcpy(req_mti, mti, sizeof(*req_mti));
763 ptlrpc_req_set_repsize(req, 2, rep_size);
764 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
766 rc = ptlrpc_queue_wait(req);
768 rep_mti = lustre_swab_repbuf(req, REPLY_REC_OFF,
770 lustre_swab_mgs_target_info);
771 memcpy(mti, rep_mti, sizeof(*rep_mti));
772 CDEBUG(D_MGC, "register %s got index = %d\n",
773 mti->mti_svname, mti->mti_stripe_index);
775 ptlrpc_req_finished(req);
780 /* Send parameter to MGS*/
781 static int mgc_set_mgs_param(struct obd_export *exp,
782 struct mgs_send_param *msp)
784 struct ptlrpc_request *req;
785 struct mgs_send_param *req_msp, *rep_msp;
786 int size[] = { sizeof(struct ptlrpc_body), sizeof(*req_msp) };
787 __u32 rep_size[] = { sizeof(struct ptlrpc_body), sizeof(*msp) };
791 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MGS_VERSION,
792 MGS_SET_INFO, 2, size, NULL);
796 req_msp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*req_msp));
798 ptlrpc_req_finished(req);
802 memcpy(req_msp, msp, sizeof(*req_msp));
803 ptlrpc_req_set_repsize(req, 2, rep_size);
805 rc = ptlrpc_queue_wait(req);
807 rep_msp = lustre_swab_repbuf(req, REPLY_REC_OFF,
808 sizeof(*rep_msp), NULL);
809 memcpy(msp, rep_msp, sizeof(*rep_msp));
812 ptlrpc_req_finished(req);
817 int mgc_set_info_async(struct obd_export *exp, obd_count keylen,
818 void *key, obd_count vallen, void *val,
819 struct ptlrpc_request_set *set)
821 struct obd_import *imp = class_exp2cliimp(exp);
825 /* Try to "recover" the initial connection; i.e. retry */
826 if (KEY_IS(KEY_INIT_RECOV)) {
827 if (vallen != sizeof(int))
829 spin_lock(&imp->imp_lock);
830 imp->imp_initial_recov = *(int *)val;
831 spin_unlock(&imp->imp_lock);
832 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
833 exp->exp_obd->obd_name, imp->imp_initial_recov);
836 /* Turn off initial_recov after we try all backup servers once */
837 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
839 if (vallen != sizeof(int))
842 spin_lock(&imp->imp_lock);
843 imp->imp_initial_recov_bk = value > 0;
844 /* Even after the initial connection, give up all comms if
845 nobody answers the first time. */
846 imp->imp_recon_bk = 1;
847 spin_unlock(&imp->imp_lock);
848 CDEBUG(D_MGC, "InitRecov %s %d/%d:d%d:i%d:r%d:or%d:%s\n",
849 imp->imp_obd->obd_name, value, imp->imp_initial_recov,
850 imp->imp_deactive, imp->imp_invalid,
851 imp->imp_replayable, imp->imp_obd->obd_replayable,
852 ptlrpc_import_state_name(imp->imp_state));
853 /* Resurrect if we previously died */
854 if (imp->imp_invalid || value > 1)
855 ptlrpc_reconnect_import(imp);
858 /* FIXME move this to mgc_process_config */
859 if (KEY_IS(KEY_REGISTER_TARGET)) {
860 struct mgs_target_info *mti;
861 if (vallen != sizeof(struct mgs_target_info))
863 mti = (struct mgs_target_info *)val;
864 CDEBUG(D_MGC, "register_target %s %#x\n",
865 mti->mti_svname, mti->mti_flags);
866 rc = mgc_target_register(exp, mti);
869 if (KEY_IS(KEY_SET_FS)) {
870 struct super_block *sb = (struct super_block *)val;
871 struct lustre_sb_info *lsi;
872 if (vallen != sizeof(struct super_block))
875 rc = mgc_fs_setup(exp->exp_obd, sb, lsi->lsi_srv_mnt);
877 CERROR("set_fs got %d\n", rc);
881 if (KEY_IS(KEY_CLEAR_FS)) {
884 rc = mgc_fs_cleanup(exp->exp_obd);
886 CERROR("clear_fs got %d\n", rc);
890 if (KEY_IS(KEY_SET_INFO)) {
891 struct mgs_send_param *msp;
893 msp = (struct mgs_send_param *)val;
894 rc = mgc_set_mgs_param(exp, msp);
901 static int mgc_import_event(struct obd_device *obd,
902 struct obd_import *imp,
903 enum obd_import_event event)
907 LASSERT(imp->imp_obd == obd);
908 CDEBUG(D_MGC, "import event %#x\n", event);
911 case IMP_EVENT_DISCON:
913 case IMP_EVENT_INACTIVE:
915 case IMP_EVENT_INVALIDATE: {
916 struct ldlm_namespace *ns = obd->obd_namespace;
917 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
920 case IMP_EVENT_ACTIVE:
921 LCONSOLE_WARN("%s: Reactivating import\n", obd->obd_name);
922 /* Clearing obd_no_recov allows us to continue pinging */
923 obd->obd_no_recov = 0;
928 CERROR("Unknown import event %#x\n", event);
934 static int mgc_llog_init(struct obd_device *obd, struct obd_device *disk_obd,
937 struct llog_ctxt *ctxt;
941 rc = llog_setup(obd, LLOG_CONFIG_ORIG_CTXT, disk_obd, 0, NULL,
946 rc = llog_setup(obd, LLOG_CONFIG_REPL_CTXT, disk_obd, 0, NULL,
949 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
950 llog_initiator_connect(ctxt);
953 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
961 static int mgc_llog_finish(struct obd_device *obd, int count)
966 rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_REPL_CTXT));
967 rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
972 /* identical to mgs_log_is_empty */
973 static int mgc_llog_is_empty(struct obd_device *obd, struct llog_ctxt *ctxt,
976 struct lvfs_run_ctxt saved;
977 struct llog_handle *llh;
980 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
981 rc = llog_create(ctxt, &llh, NULL, name);
983 llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
984 rc = llog_get_size(llh);
987 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
988 /* header is record 1 */
992 static int mgc_copy_handler(struct llog_handle *llh, struct llog_rec_hdr *rec,
995 struct llog_rec_hdr local_rec = *rec;
996 struct llog_handle *local_llh = (struct llog_handle *)data;
997 char *cfg_buf = (char*) (rec + 1);
998 struct lustre_cfg *lcfg;
1002 /* Append all records */
1003 local_rec.lrh_len -= sizeof(*rec) + sizeof(struct llog_rec_tail);
1004 rc = llog_write_rec(local_llh, &local_rec, NULL, 0,
1005 (void *)cfg_buf, -1);
1007 lcfg = (struct lustre_cfg *)cfg_buf;
1008 CDEBUG(D_INFO, "idx=%d, rc=%d, len=%d, cmd %x %s %s\n",
1009 rec->lrh_index, rc, rec->lrh_len, lcfg->lcfg_command,
1010 lustre_cfg_string(lcfg, 0), lustre_cfg_string(lcfg, 1));
1015 /* Copy a remote log locally */
1016 static int mgc_copy_llog(struct obd_device *obd, struct llog_ctxt *rctxt,
1017 struct llog_ctxt *lctxt, char *logname)
1019 struct llog_handle *local_llh, *remote_llh;
1020 struct obd_uuid *uuid;
1025 /* Write new log to a temp name, then vfs_rename over logname
1026 upon successful completion. */
1028 OBD_ALLOC(temp_log, strlen(logname) + 1);
1031 sprintf(temp_log, "%sT", logname);
1033 /* Make sure there's no old temp log */
1034 rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1037 rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, NULL);
1040 rc = llog_destroy(local_llh);
1041 llog_free_handle(local_llh);
1045 /* open local log */
1046 rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1050 /* set the log header uuid for fun */
1051 OBD_ALLOC_PTR(uuid);
1052 obd_str2uuid(uuid, logname);
1053 rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, uuid);
1056 GOTO(out_closel, rc);
1058 /* open remote log */
1059 rc = llog_create(rctxt, &remote_llh, NULL, logname);
1061 GOTO(out_closel, rc);
1062 rc = llog_init_handle(remote_llh, LLOG_F_IS_PLAIN, NULL);
1064 GOTO(out_closer, rc);
1066 /* Copy remote log */
1067 rc = llog_process(remote_llh, mgc_copy_handler,(void *)local_llh, NULL);
1070 rc2 = llog_close(remote_llh);
1074 rc2 = llog_close(local_llh);
1078 /* We've copied the remote log to the local temp log, now
1079 replace the old local log with the temp log. */
1081 struct client_obd *cli = &obd->u.cli;
1083 LASSERT(cli->cl_mgc_configs_dir);
1084 rc = lustre_rename(cli->cl_mgc_configs_dir, cli->cl_mgc_vfsmnt,
1087 CDEBUG(D_MGC, "Copied remote log %s (%d)\n", logname, rc);
1090 CERROR("Failed to copy remote log %s (%d)\n", logname, rc);
1091 OBD_FREE(temp_log, strlen(logname) + 1);
1095 /* Get a config log from the MGS and process it.
1096 This func is called for both clients and servers. */
1097 static int mgc_process_log(struct obd_device *mgc,
1098 struct config_llog_data *cld)
1100 struct llog_ctxt *ctxt, *lctxt;
1101 struct lustre_handle lockh;
1102 struct client_obd *cli = &mgc->u.cli;
1103 struct lvfs_run_ctxt saved;
1104 struct lustre_sb_info *lsi;
1105 int rc = 0, rcl, flags = 0, must_pop = 0;
1108 if (!cld || !cld->cld_cfg.cfg_sb) {
1109 /* This should never happen */
1110 CERROR("Missing cld, aborting log update\n");
1114 /* I don't want mutliple processes running process_log at once --
1115 sounds like badness. It actually might be fine, as long as
1116 we're not trying to update from the same log
1117 simultaneously (in which case we should use a per-log sem.) */
1118 down(&llog_process_lock);
1120 if (cld->cld_stopping) {
1121 up(&llog_process_lock);
1125 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1127 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1129 CDEBUG(D_MGC, "Process log %s:%s from %d\n", cld->cld_logname,
1130 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1132 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1134 CERROR("missing llog context\n");
1135 up(&llog_process_lock);
1139 /* Get the cfg lock on the llog */
1140 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1141 LCK_CR, &flags, NULL, NULL, NULL,
1142 cld, 0, NULL, &lockh);
1144 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1146 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1148 /* Copy the setup log locally if we can. Don't mess around if we're
1149 running an MGS though (logs are already local). */
1150 if (lctxt && lsi && (lsi->lsi_flags & LSI_SERVER) &&
1151 (lsi->lsi_srv_mnt == cli->cl_mgc_vfsmnt) &&
1152 !IS_MGS(lsi->lsi_ldd)) {
1153 push_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1156 /* Only try to copy log if we have the lock. */
1157 rc = mgc_copy_llog(mgc, ctxt, lctxt, cld->cld_logname);
1159 if (mgc_llog_is_empty(mgc, lctxt, cld->cld_logname)) {
1160 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1161 "log %s and no local copy."
1162 "\n", cld->cld_logname);
1163 GOTO(out_pop, rc = -ENOTCONN);
1165 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1166 "copy for now, will try to update later.\n",
1169 /* Now, whether we copied or not, start using the local llog.
1170 If we failed to copy, we'll start using whatever the old
1172 llog_ctxt_put(ctxt);
1176 /* logname and instance info should be the same, so use our
1177 copy of the instance for the update. The cfg_last_idx will
1179 rc = class_config_parse_llog(ctxt, cld->cld_logname, &cld->cld_cfg);
1182 llog_ctxt_put(ctxt);
1184 llog_ctxt_put(lctxt);
1186 pop_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1188 /* Now drop the lock so MGS can revoke it */
1190 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1191 LCK_CR, &lockh, 0, 0);
1193 CERROR("Can't drop cfg lock: %d\n", rcl);
1196 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1197 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1199 up(&llog_process_lock);
1204 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1206 struct lustre_cfg *lcfg = buf;
1211 switch(cmd = lcfg->lcfg_command) {
1212 case LCFG_LOV_ADD_OBD: {
1213 /* Add any new target, not just osts */
1214 struct mgs_target_info *mti;
1216 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1217 sizeof(struct mgs_target_info))
1218 GOTO(out, rc = -EINVAL);
1220 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1221 CDEBUG(D_MGC, "add_target %s %#x\n",
1222 mti->mti_svname, mti->mti_flags);
1223 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1226 case LCFG_LOV_DEL_OBD:
1227 /* Remove target from the fs? */
1229 CERROR("lov_del_obd unimplemented\n");
1232 case LCFG_LOG_START: {
1233 struct config_llog_data *cld;
1234 struct config_llog_instance *cfg;
1235 struct super_block *sb;
1236 char *logname = lustre_cfg_string(lcfg, 1);
1237 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1238 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1240 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1243 /* We're only called through here on the initial mount */
1244 rc = config_log_add(logname, cfg, sb);
1247 cld = config_log_find(logname, cfg);
1254 /* FIXME only set this for old logs! Right now this forces
1255 us to always skip the "inside markers" check */
1256 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1258 rc = mgc_process_log(obd, cld);
1259 config_log_put(cld);
1263 case LCFG_LOG_END: {
1264 struct config_llog_instance *cfg = NULL;
1265 char *logname = lustre_cfg_string(lcfg, 1);
1266 if (lcfg->lcfg_bufcount >= 2)
1267 cfg = (struct config_llog_instance *)lustre_cfg_buf(
1269 rc = config_log_end(logname, cfg);
1273 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1274 GOTO(out, rc = -EINVAL);
1282 struct obd_ops mgc_obd_ops = {
1283 .o_owner = THIS_MODULE,
1284 .o_setup = mgc_setup,
1285 .o_precleanup = mgc_precleanup,
1286 .o_cleanup = mgc_cleanup,
1287 .o_add_conn = client_import_add_conn,
1288 .o_del_conn = client_import_del_conn,
1289 .o_connect = client_connect_import,
1290 .o_disconnect = client_disconnect_export,
1291 //.o_enqueue = mgc_enqueue,
1292 .o_cancel = mgc_cancel,
1293 //.o_iocontrol = mgc_iocontrol,
1294 .o_set_info_async = mgc_set_info_async,
1295 .o_import_event = mgc_import_event,
1296 .o_llog_init = mgc_llog_init,
1297 .o_llog_finish = mgc_llog_finish,
1298 .o_process_config = mgc_process_config,
1301 int __init mgc_init(void)
1303 return class_register_type(&mgc_obd_ops, NULL, LUSTRE_MGC_NAME);
1307 static void /*__exit*/ mgc_exit(void)
1309 class_unregister_type(LUSTRE_MGC_NAME);
1312 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1313 MODULE_DESCRIPTION("Lustre Management Client");
1314 MODULE_LICENSE("GPL");
1316 module_init(mgc_init);
1317 module_exit(mgc_exit);