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);
108 /********************** config llog list **********************/
109 static CFS_LIST_HEAD(config_llog_list);
110 static cfs_spinlock_t config_list_lock = CFS_SPIN_LOCK_UNLOCKED;
112 /* Take a reference to a config log */
113 static int config_log_get(struct config_llog_data *cld)
116 if (cld->cld_stopping)
118 cfs_atomic_inc(&cld->cld_refcount);
119 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
120 cfs_atomic_read(&cld->cld_refcount));
124 /* Drop a reference to a config log. When no longer referenced,
125 we can free the config log data */
126 static void config_log_put(struct config_llog_data *cld)
130 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
131 cfs_atomic_read(&cld->cld_refcount));
132 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
134 /* spinlock to make sure no item with 0 refcount in the list */
135 cfs_spin_lock(&config_list_lock);
136 if (unlikely(cfs_atomic_dec_and_test(&cld->cld_refcount))) {
137 cfs_list_del(&cld->cld_list_chain);
138 cfs_spin_unlock(&config_list_lock);
140 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
142 if (cld->cld_sptlrpc)
143 config_log_put(cld->cld_sptlrpc);
144 if (cld->cld_is_sptlrpc)
145 sptlrpc_conf_log_stop(cld->cld_logname);
147 class_export_put(cld->cld_mgcexp);
148 OBD_FREE(cld->cld_logname, strlen(cld->cld_logname) + 1);
149 if (cld->cld_cfg.cfg_instance != NULL)
150 OBD_FREE(cld->cld_cfg.cfg_instance,
151 strlen(cld->cld_cfg.cfg_instance) + 1);
152 OBD_FREE(cld, sizeof(*cld));
154 cfs_spin_unlock(&config_list_lock);
160 /* Find a config log by name */
162 struct config_llog_data *config_log_find(char *logname,
163 struct config_llog_instance *cfg)
165 struct config_llog_data *cld;
166 char *logid = logname;
167 int match_instance = 0;
170 if (cfg && cfg->cfg_instance) {
172 logid = cfg->cfg_instance;
175 CERROR("No log specified\n");
176 RETURN(ERR_PTR(-EINVAL));
179 cfs_spin_lock(&config_list_lock);
180 cfs_list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
181 if (match_instance && cld->cld_cfg.cfg_instance &&
182 strcmp(logid, cld->cld_cfg.cfg_instance) == 0)
184 if (!match_instance &&
185 strcmp(logid, cld->cld_logname) == 0)
188 cfs_spin_unlock(&config_list_lock);
190 CDEBUG(D_CONFIG, "can't get log %s\n", logid);
191 RETURN(ERR_PTR(-ENOENT));
193 cfs_atomic_inc(&cld->cld_refcount);
194 cfs_spin_unlock(&config_list_lock);
195 LASSERT(cld->cld_stopping == 0 || cld->cld_is_sptlrpc == 0);
200 struct config_llog_data *do_config_log_add(struct obd_device *obd,
202 unsigned int is_sptlrpc,
203 struct config_llog_instance *cfg,
204 struct super_block *sb)
206 struct config_llog_data *cld;
210 CDEBUG(D_MGC, "do adding config log %s:%s\n", logname,
211 cfg ? cfg->cfg_instance : "NULL");
213 OBD_ALLOC(cld, sizeof(*cld));
215 RETURN(ERR_PTR(-ENOMEM));
216 OBD_ALLOC(cld->cld_logname, strlen(logname) + 1);
217 if (!cld->cld_logname) {
218 OBD_FREE(cld, sizeof(*cld));
219 RETURN(ERR_PTR(-ENOMEM));
221 strcpy(cld->cld_logname, logname);
224 cld->cld_cfg.cfg_last_idx = 0;
225 cld->cld_cfg.cfg_flags = 0;
226 cld->cld_cfg.cfg_sb = sb;
227 cld->cld_is_sptlrpc = is_sptlrpc;
228 cfs_atomic_set(&cld->cld_refcount, 1);
230 /* Keep the mgc around until we are done */
231 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
233 if (cfg && cfg->cfg_instance != NULL) {
234 OBD_ALLOC(cld->cld_cfg.cfg_instance,
235 strlen(cfg->cfg_instance) + 1);
236 strcpy(cld->cld_cfg.cfg_instance, cfg->cfg_instance);
240 sptlrpc_conf_log_start(logname);
241 cld->cld_cfg.cfg_obdname = obd->obd_name;
244 rc = mgc_logname2resid(logname, &cld->cld_resid);
246 cfs_spin_lock(&config_list_lock);
247 cfs_list_add(&cld->cld_list_chain, &config_llog_list);
248 cfs_spin_unlock(&config_list_lock);
256 rc = mgc_process_log(obd, cld);
258 CERROR("failed processing sptlrpc log: %d\n", rc);
265 * Add this log to our list of active logs.
266 * We have one active log per "mount" - client instance or servername.
267 * Each instance may be at a different point in the log.
269 static int config_log_add(struct obd_device *obd, char *logname,
270 struct config_llog_instance *cfg,
271 struct super_block *sb)
273 struct config_llog_data *cld, *sptlrpc_cld;
278 CDEBUG(D_MGC, "adding config log %s:%s\n", logname, cfg->cfg_instance);
281 * for each regular log, the depended sptlrpc log name is
282 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
284 ptr = strrchr(logname, '-');
285 if (ptr == NULL || ptr - logname > 8) {
286 CERROR("logname %s is too long\n", logname);
290 memcpy(seclogname, logname, ptr - logname);
291 strcpy(seclogname + (ptr - logname), "-sptlrpc");
293 sptlrpc_cld = config_log_find(seclogname, NULL);
294 if (IS_ERR(sptlrpc_cld)) {
295 sptlrpc_cld = do_config_log_add(obd, seclogname, 1, NULL, NULL);
296 if (IS_ERR(sptlrpc_cld)) {
297 CERROR("can't create sptlrpc log: %s\n", seclogname);
298 RETURN(PTR_ERR(sptlrpc_cld));
302 cld = do_config_log_add(obd, logname, 0, cfg, sb);
304 CERROR("can't create log: %s\n", logname);
305 config_log_put(sptlrpc_cld);
306 RETURN(PTR_ERR(cld));
309 cld->cld_sptlrpc = sptlrpc_cld;
314 CFS_DECLARE_MUTEX(llog_process_lock);
316 /* Stop watching for updates on this log. */
317 static int config_log_end(char *logname, struct config_llog_instance *cfg)
319 struct config_llog_data *cld, *cld_sptlrpc = NULL;
323 cld = config_log_find(logname, cfg);
325 RETURN(PTR_ERR(cld));
327 cfs_down(&llog_process_lock);
329 * if cld_stopping is set, it means we didn't start the log thus
330 * not owning the start ref. this can happen after previous umount:
331 * the cld still hanging there waiting for lock cancel, and we
332 * remount again but failed in the middle and call log_end without
335 if (unlikely(cld->cld_stopping)) {
336 cfs_up(&llog_process_lock);
337 /* drop the ref from the find */
342 cld->cld_stopping = 1;
343 cfs_up(&llog_process_lock);
345 cfs_spin_lock(&config_list_lock);
346 cld_sptlrpc = cld->cld_sptlrpc;
347 cld->cld_sptlrpc = NULL;
348 cfs_spin_unlock(&config_list_lock);
351 config_log_put(cld_sptlrpc);
353 /* drop the ref from the find */
355 /* drop the start ref */
358 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
363 /* reenqueue any lost locks */
364 #define RQ_RUNNING 0x1
368 static int rq_state = 0;
369 static cfs_waitq_t rq_waitq;
371 static int mgc_requeue_add(struct config_llog_data *cld, int later);
373 static void do_requeue(struct config_llog_data *cld)
375 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
377 /* Do not run mgc_process_log on a disconnected export or an
378 export which is being disconnected. Take the client
379 semaphore to make the check non-racy. */
380 cfs_down_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
381 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
382 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
383 mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
385 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
388 cfs_up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
390 /* Whether we enqueued again or not in mgc_process_log, we're done
391 * with the ref from the old enqueue */
395 static int mgc_requeue_thread(void *data)
397 struct l_wait_info lwi_now, lwi_later;
398 struct config_llog_data *cld, *cld_next, *cld_prev;
399 char name[] = "ll_cfg_requeue";
405 CDEBUG(D_MGC, "Starting requeue thread\n");
407 lwi_later = LWI_TIMEOUT(60 * CFS_HZ, NULL, NULL);
408 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP), &lwi_later);
410 /* Keep trying failed locks periodically */
411 cfs_spin_lock(&config_list_lock);
412 while (rq_state & (RQ_NOW | RQ_LATER)) {
413 /* Any new or requeued lostlocks will change the state */
414 rq_state &= ~(RQ_NOW | RQ_LATER);
415 cfs_spin_unlock(&config_list_lock);
417 /* Always wait a few seconds to allow the server who
418 caused the lock revocation to finish its setup, plus some
419 random so everyone doesn't try to reconnect at once. */
420 lwi_now = LWI_TIMEOUT(3 * CFS_HZ + (ll_rand() & 0xff) * \
423 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi_now);
426 * iterate & processing through the list. for each cld, process
427 * its depending sptlrpc cld firstly (if any) and then itself.
429 * it's guaranteed any item in the list must have
430 * reference > 0; and if cld_lostlock is set, at
431 * least one reference is taken by the previous enqueue.
433 * Note: releasing a cld might lead to itself and its depended
434 * sptlrpc cld be unlinked from the list. to safely iterate
435 * we need to take a reference on next cld before processing.
439 cfs_spin_lock(&config_list_lock);
440 cfs_list_for_each_entry_safe(cld, cld_next, &config_llog_list,
442 if (cld->cld_list_chain.next != &config_llog_list)
443 cfs_atomic_inc(&cld_next->cld_refcount);
445 if (cld->cld_lostlock) {
446 if (cld->cld_sptlrpc &&
447 cld->cld_sptlrpc->cld_lostlock) {
448 cld->cld_sptlrpc->cld_lostlock = 0;
450 cfs_spin_unlock(&config_list_lock);
451 do_requeue(cld->cld_sptlrpc);
452 cfs_spin_lock(&config_list_lock);
453 LASSERT(cld->cld_lostlock);
456 cld->cld_lostlock = 0;
458 cfs_spin_unlock(&config_list_lock);
460 cfs_spin_lock(&config_list_lock);
465 cfs_spin_unlock(&config_list_lock);
466 config_log_put(cld_prev);
467 cfs_spin_lock(&config_list_lock);
472 cfs_spin_unlock(&config_list_lock);
474 /* Wait a bit to see if anyone else needs a requeue */
475 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
477 cfs_spin_lock(&config_list_lock);
479 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
480 rq_state &= ~RQ_RUNNING;
481 cfs_spin_unlock(&config_list_lock);
483 CDEBUG(D_MGC, "Ending requeue thread\n");
487 /* Add a cld to the list to requeue. Start the requeue thread if needed.
488 We are responsible for dropping the config log reference from here on out. */
489 static int mgc_requeue_add(struct config_llog_data *cld, int later)
493 CDEBUG(D_INFO, "log %s: requeue (l=%d r=%d sp=%d st=%x)\n",
494 cld->cld_logname, later, cfs_atomic_read(&cld->cld_refcount),
495 cld->cld_stopping, rq_state);
496 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
498 /* Hold lock for rq_state */
499 cfs_spin_lock(&config_list_lock);
501 if (cld->cld_stopping || (rq_state & RQ_STOP)) {
502 cld->cld_lostlock = 0;
503 cfs_spin_unlock(&config_list_lock);
508 cld->cld_lostlock = 1;
510 if (!(rq_state & RQ_RUNNING)) {
511 LASSERT(rq_state == 0);
512 rq_state = RQ_RUNNING | (later ? RQ_LATER : RQ_NOW);
513 cfs_spin_unlock(&config_list_lock);
514 rc = cfs_kernel_thread(mgc_requeue_thread, 0,
515 CLONE_VM | CLONE_FILES);
517 CERROR("log %s: cannot start requeue thread (%d),"
518 "no more log updates!\n", cld->cld_logname, rc);
519 /* Drop the ref, since the rq thread won't */
520 cld->cld_lostlock = 0;
526 rq_state |= later ? RQ_LATER : RQ_NOW;
527 cfs_spin_unlock(&config_list_lock);
528 cfs_waitq_signal(&rq_waitq);
534 /********************** class fns **********************/
536 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb,
537 struct vfsmount *mnt)
539 struct lvfs_run_ctxt saved;
540 struct lustre_sb_info *lsi = s2lsi(sb);
541 struct client_obd *cli = &obd->u.cli;
542 struct dentry *dentry;
548 LASSERT(lsi->lsi_srv_mnt == mnt);
550 /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
551 cfs_down(&cli->cl_mgc_sem);
553 cfs_cleanup_group_info();
555 obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
556 if (IS_ERR(obd->obd_fsops)) {
557 cfs_up(&cli->cl_mgc_sem);
558 CERROR("No fstype %s rc=%ld\n", MT_STR(lsi->lsi_ldd),
559 PTR_ERR(obd->obd_fsops));
560 RETURN(PTR_ERR(obd->obd_fsops));
563 cli->cl_mgc_vfsmnt = mnt;
564 fsfilt_setup(obd, mnt->mnt_sb);
566 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
567 obd->obd_lvfs_ctxt.pwdmnt = mnt;
568 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
569 obd->obd_lvfs_ctxt.fs = get_ds();
571 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
572 dentry = lookup_one_len(MOUNT_CONFIGS_DIR, cfs_fs_pwd(current->fs),
573 strlen(MOUNT_CONFIGS_DIR));
574 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
575 if (IS_ERR(dentry)) {
576 err = PTR_ERR(dentry);
577 CERROR("cannot lookup %s directory: rc = %d\n",
578 MOUNT_CONFIGS_DIR, err);
581 cli->cl_mgc_configs_dir = dentry;
583 /* We take an obd ref to insure that we can't get to mgc_cleanup
584 without calling mgc_fs_cleanup first. */
585 class_incref(obd, "mgc_fs", obd);
587 label = fsfilt_get_label(obd, mnt->mnt_sb);
589 CDEBUG(D_MGC, "MGC using disk labelled=%s\n", label);
591 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
595 fsfilt_put_ops(obd->obd_fsops);
596 obd->obd_fsops = NULL;
597 cli->cl_mgc_vfsmnt = NULL;
598 cfs_up(&cli->cl_mgc_sem);
602 static int mgc_fs_cleanup(struct obd_device *obd)
604 struct client_obd *cli = &obd->u.cli;
608 LASSERT(cli->cl_mgc_vfsmnt != NULL);
610 if (cli->cl_mgc_configs_dir != NULL) {
611 struct lvfs_run_ctxt saved;
612 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
613 l_dput(cli->cl_mgc_configs_dir);
614 cli->cl_mgc_configs_dir = NULL;
615 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
616 class_decref(obd, "mgc_fs", obd);
619 cli->cl_mgc_vfsmnt = NULL;
621 fsfilt_put_ops(obd->obd_fsops);
623 cfs_up(&cli->cl_mgc_sem);
628 static cfs_atomic_t mgc_count = CFS_ATOMIC_INIT(0);
629 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
635 case OBD_CLEANUP_EARLY:
637 case OBD_CLEANUP_EXPORTS:
638 if (cfs_atomic_dec_and_test(&mgc_count)) {
639 /* Kick the requeue waitq - cld's should all be
641 cfs_spin_lock(&config_list_lock);
643 cfs_spin_unlock(&config_list_lock);
644 cfs_waitq_signal(&rq_waitq);
646 rc = obd_llog_finish(obd, 0);
648 CERROR("failed to cleanup llogging subsystems\n");
654 static int mgc_cleanup(struct obd_device *obd)
656 struct client_obd *cli = &obd->u.cli;
660 LASSERT(cli->cl_mgc_vfsmnt == NULL);
662 /* COMPAT_146 - old config logs may have added profiles we don't
664 if (obd->obd_type->typ_refcnt <= 1)
665 /* Only for the last mgc */
666 class_del_profiles();
668 lprocfs_obd_cleanup(obd);
671 rc = client_obd_cleanup(obd);
675 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
677 struct lprocfs_static_vars lvars;
683 rc = client_obd_setup(obd, lcfg);
685 GOTO(err_decref, rc);
687 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
689 CERROR("failed to setup llogging subsystems\n");
690 GOTO(err_cleanup, rc);
693 lprocfs_mgc_init_vars(&lvars);
694 lprocfs_obd_setup(obd, lvars.obd_vars);
695 sptlrpc_lprocfs_cliobd_attach(obd);
697 cfs_spin_lock(&config_list_lock);
698 cfs_atomic_inc(&mgc_count);
699 if (cfs_atomic_read(&mgc_count) == 1) {
700 rq_state &= ~RQ_STOP;
701 cfs_waitq_init(&rq_waitq);
703 cfs_spin_unlock(&config_list_lock);
708 client_obd_cleanup(obd);
714 /* based on ll_mdc_blocking_ast */
715 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
716 void *data, int flag)
718 struct lustre_handle lockh;
719 struct config_llog_data *cld = (struct config_llog_data *)data;
724 case LDLM_CB_BLOCKING:
725 /* mgs wants the lock, give it up... */
726 LDLM_DEBUG(lock, "MGC blocking CB");
727 ldlm_lock2handle(lock, &lockh);
728 rc = ldlm_cli_cancel(&lockh);
730 case LDLM_CB_CANCELING: {
731 /* We've given up the lock, prepare ourselves to update. */
732 LDLM_DEBUG(lock, "MGC cancel CB");
734 CDEBUG(D_MGC, "Lock res "LPX64" (%.8s)\n",
735 lock->l_resource->lr_name.name[0],
736 (char *)&lock->l_resource->lr_name.name[0]);
739 CERROR("missing data, won't requeue\n");
742 /* Are we done with this log? */
743 if (cld->cld_stopping) {
744 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
749 /* Make sure not to re-enqueue when the mgc is stopping
750 (we get called from client_disconnect_export) */
751 if (!lock->l_conn_export ||
752 !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
753 CDEBUG(D_MGC, "log %s: disconnecting, won't requeue\n",
758 /* Did we fail to get the lock? */
759 if (lock->l_req_mode != lock->l_granted_mode &&
760 !cld->cld_is_sptlrpc) {
761 CDEBUG(D_MGC, "log %s: original grant failed, will "
762 "requeue later\n", cld->cld_logname);
763 /* Try to re-enqueue later */
764 rc = mgc_requeue_add(cld, 1);
768 rc = mgc_requeue_add(cld, 0);
777 CERROR("%s CB failed %d:\n", flag == LDLM_CB_BLOCKING ?
778 "blocking" : "cancel", rc);
779 LDLM_ERROR(lock, "MGC ast");
784 /* Send parameter to MGS*/
785 static int mgc_set_mgs_param(struct obd_export *exp,
786 struct mgs_send_param *msp)
788 struct ptlrpc_request *req;
789 struct mgs_send_param *req_msp, *rep_msp;
793 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
794 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
799 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
801 ptlrpc_req_finished(req);
805 memcpy(req_msp, msp, sizeof(*req_msp));
806 ptlrpc_request_set_replen(req);
808 rc = ptlrpc_queue_wait(req);
810 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
811 memcpy(msp, rep_msp, sizeof(*rep_msp));
814 ptlrpc_req_finished(req);
819 /* Take a config lock so we can get cancel notifications */
820 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
821 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
822 int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
823 void *data, __u32 lvb_len, void *lvb_swabber,
824 struct lustre_handle *lockh)
826 struct config_llog_data *cld = (struct config_llog_data *)data;
827 struct ldlm_enqueue_info einfo = { type, mode, mgc_blocking_ast,
828 ldlm_completion_ast, NULL, NULL, data};
833 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
834 cld->cld_resid.name[0]);
836 /* We can only drop this config log ref when we drop the lock */
837 if (config_log_get(cld))
838 RETURN(ELDLM_LOCK_ABORTED);
840 /* We need a callback for every lockholder, so don't try to
841 ldlm_lock_match (see rev 1.1.2.11.2.47) */
843 rc = ldlm_cli_enqueue(exp, NULL, &einfo, &cld->cld_resid, NULL, flags,
845 /* A failed enqueue should still call the mgc_blocking_ast,
846 where it will be requeued if needed ("grant failed"). */
851 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
852 __u32 mode, struct lustre_handle *lockh)
856 ldlm_lock_decref(lockh, mode);
862 static int mgc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
863 void *karg, void *uarg)
865 struct obd_device *obd = exp->exp_obd;
866 struct obd_ioctl_data *data = karg;
867 struct llog_ctxt *ctxt;
868 struct lvfs_run_ctxt saved;
872 if (!cfs_try_module_get(THIS_MODULE)) {
873 CERROR("Can't get module. Is it alive?");
877 /* REPLicator context */
878 case OBD_IOC_PARSE: {
879 CERROR("MGC parsing llog %s\n", data->ioc_inlbuf1);
880 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
881 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
885 case OBD_IOC_LLOG_INFO:
886 case OBD_IOC_LLOG_PRINT: {
887 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
888 rc = llog_ioctl(ctxt, cmd, data);
893 /* ORIGinator context */
894 case OBD_IOC_DUMP_LOG: {
895 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
896 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
897 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
898 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
905 CERROR("mgc_ioctl(): unrecognised ioctl %#x\n", cmd);
906 GOTO(out, rc = -ENOTTY);
909 cfs_module_put(THIS_MODULE);
915 /* Send target_reg message to MGS */
916 static int mgc_target_register(struct obd_export *exp,
917 struct mgs_target_info *mti)
919 struct ptlrpc_request *req;
920 struct mgs_target_info *req_mti, *rep_mti;
924 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
925 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
930 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
932 ptlrpc_req_finished(req);
936 memcpy(req_mti, mti, sizeof(*req_mti));
937 ptlrpc_request_set_replen(req);
938 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
940 rc = ptlrpc_queue_wait(req);
942 rep_mti = req_capsule_server_get(&req->rq_pill,
943 &RMF_MGS_TARGET_INFO);
944 memcpy(mti, rep_mti, sizeof(*rep_mti));
945 CDEBUG(D_MGC, "register %s got index = %d\n",
946 mti->mti_svname, mti->mti_stripe_index);
948 ptlrpc_req_finished(req);
953 int mgc_set_info_async(struct obd_export *exp, obd_count keylen,
954 void *key, obd_count vallen, void *val,
955 struct ptlrpc_request_set *set)
957 struct obd_import *imp = class_exp2cliimp(exp);
961 /* Try to "recover" the initial connection; i.e. retry */
962 if (KEY_IS(KEY_INIT_RECOV)) {
963 if (vallen != sizeof(int))
965 cfs_spin_lock(&imp->imp_lock);
966 imp->imp_initial_recov = *(int *)val;
967 cfs_spin_unlock(&imp->imp_lock);
968 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
969 exp->exp_obd->obd_name, imp->imp_initial_recov);
972 /* Turn off initial_recov after we try all backup servers once */
973 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
975 if (vallen != sizeof(int))
978 cfs_spin_lock(&imp->imp_lock);
979 imp->imp_initial_recov_bk = value > 0;
980 /* Even after the initial connection, give up all comms if
981 nobody answers the first time. */
982 imp->imp_recon_bk = 1;
983 cfs_spin_unlock(&imp->imp_lock);
984 CDEBUG(D_MGC, "InitRecov %s %d/%d:d%d:i%d:r%d:or%d:%s\n",
985 imp->imp_obd->obd_name, value, imp->imp_initial_recov,
986 imp->imp_deactive, imp->imp_invalid,
987 imp->imp_replayable, imp->imp_obd->obd_replayable,
988 ptlrpc_import_state_name(imp->imp_state));
989 /* Resurrect if we previously died */
990 if (imp->imp_invalid || value > 1)
991 ptlrpc_reconnect_import(imp);
994 /* FIXME move this to mgc_process_config */
995 if (KEY_IS(KEY_REGISTER_TARGET)) {
996 struct mgs_target_info *mti;
997 if (vallen != sizeof(struct mgs_target_info))
999 mti = (struct mgs_target_info *)val;
1000 CDEBUG(D_MGC, "register_target %s %#x\n",
1001 mti->mti_svname, mti->mti_flags);
1002 rc = mgc_target_register(exp, mti);
1005 if (KEY_IS(KEY_SET_FS)) {
1006 struct super_block *sb = (struct super_block *)val;
1007 struct lustre_sb_info *lsi;
1008 if (vallen != sizeof(struct super_block))
1011 rc = mgc_fs_setup(exp->exp_obd, sb, lsi->lsi_srv_mnt);
1013 CERROR("set_fs got %d\n", rc);
1017 if (KEY_IS(KEY_CLEAR_FS)) {
1020 rc = mgc_fs_cleanup(exp->exp_obd);
1022 CERROR("clear_fs got %d\n", rc);
1026 if (KEY_IS(KEY_SET_INFO)) {
1027 struct mgs_send_param *msp;
1029 msp = (struct mgs_send_param *)val;
1030 rc = mgc_set_mgs_param(exp, msp);
1033 if (KEY_IS(KEY_MGSSEC)) {
1034 struct client_obd *cli = &exp->exp_obd->u.cli;
1035 struct sptlrpc_flavor flvr;
1038 * empty string means using current flavor, if which haven't
1039 * been set yet, set it as null.
1041 * if flavor has been set previously, check the asking flavor
1042 * must match the existing one.
1045 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1051 rc = sptlrpc_parse_flavor(val, &flvr);
1053 CERROR("invalid sptlrpc flavor %s to MGS\n",
1059 * caller already hold a mutex
1061 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1062 cli->cl_flvr_mgc = flvr;
1063 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1064 sizeof(flvr)) != 0) {
1067 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1069 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1070 "currently %s is in use\n",
1080 static int mgc_import_event(struct obd_device *obd,
1081 struct obd_import *imp,
1082 enum obd_import_event event)
1086 LASSERT(imp->imp_obd == obd);
1087 CDEBUG(D_MGC, "import event %#x\n", event);
1090 case IMP_EVENT_DISCON:
1091 /* MGC imports should not wait for recovery */
1093 case IMP_EVENT_INACTIVE:
1095 case IMP_EVENT_INVALIDATE: {
1096 struct ldlm_namespace *ns = obd->obd_namespace;
1097 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1100 case IMP_EVENT_ACTIVE:
1101 LCONSOLE_WARN("%s: Reactivating import\n", obd->obd_name);
1102 /* Clearing obd_no_recov allows us to continue pinging */
1103 obd->obd_no_recov = 0;
1108 CERROR("Unknown import event %#x\n", event);
1114 static int mgc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
1115 struct obd_device *tgt, int *index)
1117 struct llog_ctxt *ctxt;
1121 LASSERT(olg == &obd->obd_olg);
1123 rc = llog_setup(obd, olg, LLOG_CONFIG_ORIG_CTXT, tgt, 0, NULL,
1128 rc = llog_setup(obd, olg, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1131 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1133 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1138 llog_initiator_connect(ctxt);
1139 llog_ctxt_put(ctxt);
1141 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1149 static int mgc_llog_finish(struct obd_device *obd, int count)
1151 struct llog_ctxt *ctxt;
1152 int rc = 0, rc2 = 0;
1155 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1157 rc = llog_cleanup(ctxt);
1159 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1161 rc2 = llog_cleanup(ctxt);
1169 /* identical to mgs_log_is_empty */
1170 static int mgc_llog_is_empty(struct obd_device *obd, struct llog_ctxt *ctxt,
1173 struct lvfs_run_ctxt saved;
1174 struct llog_handle *llh;
1177 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1178 rc = llog_create(ctxt, &llh, NULL, name);
1180 llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1181 rc = llog_get_size(llh);
1184 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1185 /* header is record 1 */
1189 static int mgc_copy_handler(struct llog_handle *llh, struct llog_rec_hdr *rec,
1192 struct llog_rec_hdr local_rec = *rec;
1193 struct llog_handle *local_llh = (struct llog_handle *)data;
1194 char *cfg_buf = (char*) (rec + 1);
1195 struct lustre_cfg *lcfg;
1199 /* Append all records */
1200 local_rec.lrh_len -= sizeof(*rec) + sizeof(struct llog_rec_tail);
1201 rc = llog_write_rec(local_llh, &local_rec, NULL, 0,
1202 (void *)cfg_buf, -1);
1204 lcfg = (struct lustre_cfg *)cfg_buf;
1205 CDEBUG(D_INFO, "idx=%d, rc=%d, len=%d, cmd %x %s %s\n",
1206 rec->lrh_index, rc, rec->lrh_len, lcfg->lcfg_command,
1207 lustre_cfg_string(lcfg, 0), lustre_cfg_string(lcfg, 1));
1212 /* Copy a remote log locally */
1213 static int mgc_copy_llog(struct obd_device *obd, struct llog_ctxt *rctxt,
1214 struct llog_ctxt *lctxt, char *logname)
1216 struct llog_handle *local_llh, *remote_llh;
1217 struct obd_uuid *uuid;
1222 /* Write new log to a temp name, then vfs_rename over logname
1223 upon successful completion. */
1225 OBD_ALLOC(temp_log, strlen(logname) + 1);
1228 sprintf(temp_log, "%sT", logname);
1230 /* Make sure there's no old temp log */
1231 rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1234 rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, NULL);
1237 rc = llog_destroy(local_llh);
1238 llog_free_handle(local_llh);
1242 /* open local log */
1243 rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1247 /* set the log header uuid for fun */
1248 OBD_ALLOC_PTR(uuid);
1249 obd_str2uuid(uuid, logname);
1250 rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, uuid);
1253 GOTO(out_closel, rc);
1255 /* open remote log */
1256 rc = llog_create(rctxt, &remote_llh, NULL, logname);
1258 GOTO(out_closel, rc);
1259 rc = llog_init_handle(remote_llh, LLOG_F_IS_PLAIN, NULL);
1261 GOTO(out_closer, rc);
1263 /* Copy remote log */
1264 rc = llog_process(remote_llh, mgc_copy_handler,(void *)local_llh, NULL);
1267 rc2 = llog_close(remote_llh);
1271 rc2 = llog_close(local_llh);
1275 /* We've copied the remote log to the local temp log, now
1276 replace the old local log with the temp log. */
1278 struct client_obd *cli = &obd->u.cli;
1280 LASSERT(cli->cl_mgc_configs_dir);
1281 rc = lustre_rename(cli->cl_mgc_configs_dir, cli->cl_mgc_vfsmnt,
1284 CDEBUG(D_MGC, "Copied remote log %s (%d)\n", logname, rc);
1287 CERROR("Failed to copy remote log %s (%d)\n", logname, rc);
1288 OBD_FREE(temp_log, strlen(logname) + 1);
1292 /* Get a config log from the MGS and process it.
1293 This func is called for both clients and servers. */
1294 int mgc_process_log(struct obd_device *mgc,
1295 struct config_llog_data *cld)
1297 struct llog_ctxt *ctxt, *lctxt;
1298 struct lustre_handle lockh;
1299 struct client_obd *cli = &mgc->u.cli;
1300 struct lvfs_run_ctxt saved;
1301 struct lustre_sb_info *lsi = NULL;
1302 int rc = 0, rcl, flags = 0, must_pop = 0;
1307 /* I don't want multiple processes running process_log at once --
1308 sounds like badness. It actually might be fine, as long as
1309 we're not trying to update from the same log
1310 simultaneously (in which case we should use a per-log sem.) */
1311 cfs_down(&llog_process_lock);
1313 if (cld->cld_stopping) {
1314 cfs_up(&llog_process_lock);
1318 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1320 if (cld->cld_cfg.cfg_sb)
1321 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1323 CDEBUG(D_MGC, "Process log %s:%s from %d\n", cld->cld_logname,
1324 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1326 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1328 CERROR("missing llog context\n");
1329 cfs_up(&llog_process_lock);
1333 /* Get the cfg lock on the llog */
1334 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1335 LCK_CR, &flags, NULL, NULL, NULL,
1336 cld, 0, NULL, &lockh);
1338 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1340 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1343 * local copy of sptlrpc log is controlled elsewhere, don't try to
1346 if (rcl && cld->cld_is_sptlrpc)
1349 /* Copy the setup log locally if we can. Don't mess around if we're
1350 running an MGS though (logs are already local). */
1351 if (lctxt && lsi && (lsi->lsi_flags & LSI_SERVER) &&
1352 (lsi->lsi_srv_mnt == cli->cl_mgc_vfsmnt) &&
1353 !IS_MGS(lsi->lsi_ldd)) {
1354 push_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1357 /* Only try to copy log if we have the lock. */
1358 rc = mgc_copy_llog(mgc, ctxt, lctxt, cld->cld_logname);
1360 if (mgc_llog_is_empty(mgc, lctxt, cld->cld_logname)) {
1361 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1362 "log %s and no local copy."
1363 "\n", cld->cld_logname);
1364 GOTO(out_pop, rc = -ENOTCONN);
1366 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1367 "copy for now, will try to update later.\n",
1370 /* Now, whether we copied or not, start using the local llog.
1371 If we failed to copy, we'll start using whatever the old
1373 llog_ctxt_put(ctxt);
1377 if (cld->cld_is_sptlrpc)
1378 sptlrpc_conf_log_update_begin(cld->cld_logname);
1380 /* logname and instance info should be the same, so use our
1381 copy of the instance for the update. The cfg_last_idx will
1383 rc = class_config_parse_llog(ctxt, cld->cld_logname, &cld->cld_cfg);
1385 llog_ctxt_put(ctxt);
1387 llog_ctxt_put(lctxt);
1389 pop_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1392 * update settings on existing OBDs. doing it inside
1393 * of llog_process_lock so no device is attaching/detaching
1395 * the logname must be <fsname>-sptlrpc
1397 if (cld->cld_is_sptlrpc && rcl == 0) {
1398 sptlrpc_conf_log_update_end(cld->cld_logname);
1399 class_notify_sptlrpc_conf(cld->cld_logname,
1400 strlen(cld->cld_logname) -
1401 strlen("-sptlrpc"));
1404 /* Now drop the lock so MGS can revoke it */
1406 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1409 CERROR("Can't drop cfg lock: %d\n", rcl);
1412 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1413 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1415 cfs_up(&llog_process_lock);
1420 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1422 struct lustre_cfg *lcfg = buf;
1427 switch(cmd = lcfg->lcfg_command) {
1428 case LCFG_LOV_ADD_OBD: {
1429 /* Add any new target, not just osts */
1430 struct mgs_target_info *mti;
1432 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1433 sizeof(struct mgs_target_info))
1434 GOTO(out, rc = -EINVAL);
1436 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1437 CDEBUG(D_MGC, "add_target %s %#x\n",
1438 mti->mti_svname, mti->mti_flags);
1439 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1442 case LCFG_LOV_DEL_OBD:
1443 /* Remove target from the fs? */
1445 CERROR("lov_del_obd unimplemented\n");
1448 case LCFG_SPTLRPC_CONF: {
1449 rc = sptlrpc_process_config(lcfg);
1452 case LCFG_LOG_START: {
1453 struct config_llog_data *cld;
1454 struct config_llog_instance *cfg;
1455 struct super_block *sb;
1456 char *logname = lustre_cfg_string(lcfg, 1);
1457 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1458 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1460 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1463 /* We're only called through here on the initial mount */
1464 rc = config_log_add(obd, logname, cfg, sb);
1467 cld = config_log_find(logname, cfg);
1474 /* FIXME only set this for old logs! Right now this forces
1475 us to always skip the "inside markers" check */
1476 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1478 rc = mgc_process_log(obd, cld);
1479 config_log_put(cld);
1483 case LCFG_LOG_END: {
1484 struct config_llog_instance *cfg = NULL;
1485 char *logname = lustre_cfg_string(lcfg, 1);
1486 if (lcfg->lcfg_bufcount >= 2)
1487 cfg = (struct config_llog_instance *)lustre_cfg_buf(
1489 rc = config_log_end(logname, cfg);
1493 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1494 GOTO(out, rc = -EINVAL);
1502 struct obd_ops mgc_obd_ops = {
1503 .o_owner = THIS_MODULE,
1504 .o_setup = mgc_setup,
1505 .o_precleanup = mgc_precleanup,
1506 .o_cleanup = mgc_cleanup,
1507 .o_add_conn = client_import_add_conn,
1508 .o_del_conn = client_import_del_conn,
1509 .o_connect = client_connect_import,
1510 .o_disconnect = client_disconnect_export,
1511 //.o_enqueue = mgc_enqueue,
1512 .o_cancel = mgc_cancel,
1513 //.o_iocontrol = mgc_iocontrol,
1514 .o_set_info_async = mgc_set_info_async,
1515 .o_import_event = mgc_import_event,
1516 .o_llog_init = mgc_llog_init,
1517 .o_llog_finish = mgc_llog_finish,
1518 .o_process_config = mgc_process_config,
1521 int __init mgc_init(void)
1523 return class_register_type(&mgc_obd_ops, NULL, NULL,
1524 LUSTRE_MGC_NAME, NULL);
1528 static void /*__exit*/ mgc_exit(void)
1530 class_unregister_type(LUSTRE_MGC_NAME);
1533 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1534 MODULE_DESCRIPTION("Lustre Management Client");
1535 MODULE_LICENSE("GPL");
1537 module_init(mgc_init);
1538 module_exit(mgc_exit);