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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/mgc/mgc_request.c
40 * Author: Nathan Rutman <nathan@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MGC
47 #define D_MGC D_CONFIG /*|D_WARNING*/
50 # include <linux/module.h>
51 # include <linux/pagemap.h>
52 # include <linux/miscdevice.h>
53 # include <linux/init.h>
55 # include <liblustre.h>
58 #include <obd_class.h>
59 #include <lustre_dlm.h>
60 #include <lprocfs_status.h>
61 #include <lustre_log.h>
62 #include <lustre_fsfilt.h>
63 #include <lustre_disk.h>
64 #include "mgc_internal.h"
66 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id,
72 CERROR("name too long: %s\n", name);
76 CERROR("missing name: %s\n", name);
79 memcpy(&resname, name, len);
81 /* Always use the same endianness for the resid */
82 memset(res_id, 0, sizeof(*res_id));
83 res_id->name[0] = cpu_to_le64(resname);
84 /* XXX: unfortunately, sptlprc and config llog share one lock */
87 case CONFIG_T_SPTLRPC:
90 case CONFIG_T_RECOVER:
96 res_id->name[1] = cpu_to_le64(resname);
97 CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.8s)\n", name,
98 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
102 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id, int type)
104 /* fsname is at most 8 chars long, maybe contain "-".
105 * e.g. "lustre", "SUN-000" */
106 return mgc_name2resid(fsname, strlen(fsname), res_id, type);
108 EXPORT_SYMBOL(mgc_fsname2resid);
110 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id, int type)
115 /* logname consists of "fsname-nodetype".
116 * e.g. "lustre-MDT0001", "SUN-000-client" */
117 name_end = strrchr(logname, '-');
119 len = name_end - logname;
120 return mgc_name2resid(logname, len, res_id, type);
123 /********************** config llog list **********************/
124 static CFS_LIST_HEAD(config_llog_list);
125 static cfs_spinlock_t config_list_lock = CFS_SPIN_LOCK_UNLOCKED;
127 /* Take a reference to a config log */
128 static int config_log_get(struct config_llog_data *cld)
131 cfs_atomic_inc(&cld->cld_refcount);
132 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
133 cfs_atomic_read(&cld->cld_refcount));
137 /* Drop a reference to a config log. When no longer referenced,
138 we can free the config log data */
139 static void config_log_put(struct config_llog_data *cld)
143 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
144 cfs_atomic_read(&cld->cld_refcount));
145 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
147 /* spinlock to make sure no item with 0 refcount in the list */
148 if (cfs_atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
149 cfs_list_del(&cld->cld_list_chain);
150 cfs_spin_unlock(&config_list_lock);
152 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
154 if (cld->cld_recover)
155 config_log_put(cld->cld_recover);
156 if (cld->cld_sptlrpc)
157 config_log_put(cld->cld_sptlrpc);
158 if (cld_is_sptlrpc(cld))
159 sptlrpc_conf_log_stop(cld->cld_logname);
161 class_export_put(cld->cld_mgcexp);
162 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
168 /* Find a config log by name */
170 struct config_llog_data *config_log_find(char *logname,
171 struct config_llog_instance *cfg)
173 struct config_llog_data *cld;
174 struct config_llog_data *found = NULL;
178 LASSERT(logname != NULL);
180 instance = cfg ? cfg->cfg_instance : NULL;
181 cfs_spin_lock(&config_list_lock);
182 cfs_list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
183 /* check if instance equals */
184 if (instance != cld->cld_cfg.cfg_instance)
187 /* instance may be NULL, should check name */
188 if (strcmp(logname, cld->cld_logname) == 0) {
194 cfs_atomic_inc(&found->cld_refcount);
195 LASSERT(found->cld_stopping == 0 || cld_is_sptlrpc(found) == 0);
197 cfs_spin_unlock(&config_list_lock);
202 struct config_llog_data *do_config_log_add(struct obd_device *obd,
205 struct config_llog_instance *cfg,
206 struct super_block *sb)
208 struct config_llog_data *cld;
212 CDEBUG(D_MGC, "do adding config log %s:%p\n", logname,
213 cfg ? cfg->cfg_instance : 0);
215 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
217 RETURN(ERR_PTR(-ENOMEM));
219 strcpy(cld->cld_logname, logname);
222 cfs_mutex_init(&cld->cld_lock);
223 cld->cld_cfg.cfg_last_idx = 0;
224 cld->cld_cfg.cfg_flags = 0;
225 cld->cld_cfg.cfg_sb = sb;
226 cld->cld_type = type;
227 cfs_atomic_set(&cld->cld_refcount, 1);
229 /* Keep the mgc around until we are done */
230 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
232 if (cld_is_sptlrpc(cld)) {
233 sptlrpc_conf_log_start(logname);
234 cld->cld_cfg.cfg_obdname = obd->obd_name;
237 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
239 cfs_spin_lock(&config_list_lock);
240 cfs_list_add(&cld->cld_list_chain, &config_llog_list);
241 cfs_spin_unlock(&config_list_lock);
248 if (cld_is_sptlrpc(cld)) {
249 rc = mgc_process_log(obd, cld);
251 CERROR("failed processing sptlrpc log: %d\n", rc);
257 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
259 struct config_llog_instance *cfg,
260 struct super_block *sb)
262 struct config_llog_instance lcfg = *cfg;
263 struct lustre_sb_info *lsi = s2lsi(sb);
264 struct config_llog_data *cld;
267 if ((lsi->lsi_flags & LSI_SERVER) && !IS_MDT(lsi->lsi_ldd))
270 /* we have to use different llog for clients and mdts for cmd
271 * where only clients are notified if one of cmd server restarts */
272 LASSERT(strlen(fsname) < sizeof(logname) / 2);
273 strcpy(logname, fsname);
274 if (lsi->lsi_flags & LSI_SERVER) { /* mdt */
275 LASSERT(lcfg.cfg_instance == NULL);
276 lcfg.cfg_instance = sb;
277 strcat(logname, "-mdtir");
279 LASSERT(lcfg.cfg_instance != NULL);
280 strcat(logname, "-cliir");
283 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
288 /** Add this log to the list of active logs watched by an MGC.
289 * Active means we're watching for updates.
290 * We have one active log per "mount" - client instance or servername.
291 * Each instance may be at a different point in the log.
293 static int config_log_add(struct obd_device *obd, char *logname,
294 struct config_llog_instance *cfg,
295 struct super_block *sb)
297 struct lustre_sb_info *lsi = s2lsi(sb);
298 struct config_llog_data *cld;
299 struct config_llog_data *sptlrpc_cld;
304 CDEBUG(D_MGC, "adding config log %s:%p\n", logname, cfg->cfg_instance);
307 * for each regular log, the depended sptlrpc log name is
308 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
310 ptr = strrchr(logname, '-');
311 if (ptr == NULL || ptr - logname > 8) {
312 CERROR("logname %s is too long\n", logname);
316 memcpy(seclogname, logname, ptr - logname);
317 strcpy(seclogname + (ptr - logname), "-sptlrpc");
319 sptlrpc_cld = config_log_find(seclogname, NULL);
320 if (sptlrpc_cld == NULL) {
321 sptlrpc_cld = do_config_log_add(obd, seclogname,
322 CONFIG_T_SPTLRPC, NULL, NULL);
323 if (IS_ERR(sptlrpc_cld)) {
324 CERROR("can't create sptlrpc log: %s\n", seclogname);
325 RETURN(PTR_ERR(sptlrpc_cld));
329 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
331 CERROR("can't create log: %s\n", logname);
332 config_log_put(sptlrpc_cld);
333 RETURN(PTR_ERR(cld));
336 cld->cld_sptlrpc = sptlrpc_cld;
338 LASSERT(lsi->lsi_lmd);
339 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)) {
340 struct config_llog_data *recover_cld;
341 *strrchr(seclogname, '-') = 0;
342 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
343 if (IS_ERR(recover_cld)) {
345 RETURN(PTR_ERR(recover_cld));
347 cld->cld_recover = recover_cld;
353 CFS_DECLARE_MUTEX(llog_process_lock);
355 /** Stop watching for updates on this log.
357 static int config_log_end(char *logname, struct config_llog_instance *cfg)
359 struct config_llog_data *cld;
360 struct config_llog_data *cld_sptlrpc = NULL;
361 struct config_llog_data *cld_recover = NULL;
365 cld = config_log_find(logname, cfg);
369 cfs_mutex_lock(&cld->cld_lock);
371 * if cld_stopping is set, it means we didn't start the log thus
372 * not owning the start ref. this can happen after previous umount:
373 * the cld still hanging there waiting for lock cancel, and we
374 * remount again but failed in the middle and call log_end without
377 if (unlikely(cld->cld_stopping)) {
378 cfs_mutex_unlock(&cld->cld_lock);
379 /* drop the ref from the find */
384 cld->cld_stopping = 1;
386 cld_recover = cld->cld_recover;
387 cld->cld_recover = NULL;
388 cfs_mutex_unlock(&cld->cld_lock);
391 cfs_mutex_lock(&cld_recover->cld_lock);
392 cld_recover->cld_stopping = 1;
393 cfs_mutex_unlock(&cld_recover->cld_lock);
394 config_log_put(cld_recover);
397 cfs_spin_lock(&config_list_lock);
398 cld_sptlrpc = cld->cld_sptlrpc;
399 cld->cld_sptlrpc = NULL;
400 cfs_spin_unlock(&config_list_lock);
403 config_log_put(cld_sptlrpc);
405 /* drop the ref from the find */
407 /* drop the start ref */
410 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
415 int lprocfs_mgc_rd_ir_state(char *page, char **start, off_t off,
416 int count, int *eof, void *data)
418 struct obd_device *obd = data;
419 struct obd_import *imp = obd->u.cli.cl_import;
420 struct obd_connect_data *ocd = &imp->imp_connect_data;
421 struct config_llog_data *cld;
425 rc = snprintf(page, count, "imperative_recovery: %s\n",
426 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ON" : "OFF");
427 rc += snprintf(page + rc, count - rc, "client_state:\n");
429 cfs_spin_lock(&config_list_lock);
430 cfs_list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
431 if (cld->cld_recover == NULL)
433 rc += snprintf(page + rc, count - rc,
434 " - { client: %s, nidtbl_version: %u }\n",
436 cld->cld_recover->cld_cfg.cfg_last_idx);
438 cfs_spin_unlock(&config_list_lock);
443 /* reenqueue any lost locks */
444 #define RQ_RUNNING 0x1
448 static int rq_state = 0;
449 static cfs_waitq_t rq_waitq;
450 static CFS_DECLARE_COMPLETION(rq_exit);
452 static void do_requeue(struct config_llog_data *cld)
455 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
457 /* Do not run mgc_process_log on a disconnected export or an
458 export which is being disconnected. Take the client
459 semaphore to make the check non-racy. */
460 cfs_down_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
461 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
462 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
463 mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
465 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
468 cfs_up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
473 /* this timeout represents how many seconds MGC should wait before
474 * requeue config and recover lock to the MGS. We need to randomize this
475 * in order to not flood the MGS.
477 #define MGC_TIMEOUT_MIN_SECONDS 5
478 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
480 static int mgc_requeue_thread(void *data)
482 char name[] = "ll_cfg_requeue";
488 CDEBUG(D_MGC, "Starting requeue thread\n");
490 /* Keep trying failed locks periodically */
491 cfs_spin_lock(&config_list_lock);
492 rq_state |= RQ_RUNNING;
494 struct l_wait_info lwi;
495 struct config_llog_data *cld, *cld_prev;
496 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
497 int stopped = !!(rq_state & RQ_STOP);
500 /* Any new or requeued lostlocks will change the state */
501 rq_state &= ~(RQ_NOW | RQ_LATER);
502 cfs_spin_unlock(&config_list_lock);
504 /* Always wait a few seconds to allow the server who
505 caused the lock revocation to finish its setup, plus some
506 random so everyone doesn't try to reconnect at once. */
507 to = MGC_TIMEOUT_MIN_SECONDS * CFS_HZ;
508 to += rand * CFS_HZ / 100; /* rand is centi-seconds */
509 lwi = LWI_TIMEOUT(to, NULL, NULL);
510 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi);
513 * iterate & processing through the list. for each cld, process
514 * its depending sptlrpc cld firstly (if any) and then itself.
516 * it's guaranteed any item in the list must have
517 * reference > 0; and if cld_lostlock is set, at
518 * least one reference is taken by the previous enqueue.
522 cfs_spin_lock(&config_list_lock);
523 cfs_list_for_each_entry(cld, &config_llog_list,
525 if (!cld->cld_lostlock)
528 cfs_spin_unlock(&config_list_lock);
530 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
532 /* Whether we enqueued again or not in mgc_process_log,
533 * we're done with the ref from the old enqueue */
535 config_log_put(cld_prev);
538 cld->cld_lostlock = 0;
539 if (likely(!stopped))
542 cfs_spin_lock(&config_list_lock);
544 cfs_spin_unlock(&config_list_lock);
546 config_log_put(cld_prev);
548 /* break after scanning the list so that we can drop
549 * refcount to losing lock clds */
550 if (unlikely(stopped)) {
551 cfs_spin_lock(&config_list_lock);
555 /* Wait a bit to see if anyone else needs a requeue */
556 lwi = (struct l_wait_info) { 0 };
557 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
559 cfs_spin_lock(&config_list_lock);
561 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
562 rq_state &= ~RQ_RUNNING;
563 cfs_spin_unlock(&config_list_lock);
565 cfs_complete(&rq_exit);
567 CDEBUG(D_MGC, "Ending requeue thread\n");
571 /* Add a cld to the list to requeue. Start the requeue thread if needed.
572 We are responsible for dropping the config log reference from here on out. */
573 static void mgc_requeue_add(struct config_llog_data *cld)
577 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
578 cld->cld_logname, cfs_atomic_read(&cld->cld_refcount),
579 cld->cld_stopping, rq_state);
580 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
582 cfs_mutex_lock(&cld->cld_lock);
583 if (cld->cld_stopping || cld->cld_lostlock) {
584 cfs_mutex_unlock(&cld->cld_lock);
587 /* this refcount will be released in mgc_requeue_thread. */
589 cld->cld_lostlock = 1;
590 cfs_mutex_unlock(&cld->cld_lock);
592 /* Hold lock for rq_state */
593 cfs_spin_lock(&config_list_lock);
594 if (rq_state & RQ_STOP) {
595 cfs_spin_unlock(&config_list_lock);
596 cld->cld_lostlock = 0;
600 cfs_spin_unlock(&config_list_lock);
601 cfs_waitq_signal(&rq_waitq);
606 /********************** class fns **********************/
608 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb,
609 struct vfsmount *mnt)
611 struct lvfs_run_ctxt saved;
612 struct lustre_sb_info *lsi = s2lsi(sb);
613 struct client_obd *cli = &obd->u.cli;
614 struct dentry *dentry;
620 LASSERT(lsi->lsi_srv_mnt == mnt);
622 /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
623 cfs_down(&cli->cl_mgc_sem);
625 cfs_cleanup_group_info();
627 obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
628 if (IS_ERR(obd->obd_fsops)) {
629 cfs_up(&cli->cl_mgc_sem);
630 CERROR("No fstype %s rc=%ld\n", MT_STR(lsi->lsi_ldd),
631 PTR_ERR(obd->obd_fsops));
632 RETURN(PTR_ERR(obd->obd_fsops));
635 cli->cl_mgc_vfsmnt = mnt;
636 fsfilt_setup(obd, mnt->mnt_sb);
638 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
639 obd->obd_lvfs_ctxt.pwdmnt = mnt;
640 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
641 obd->obd_lvfs_ctxt.fs = get_ds();
643 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
644 dentry = ll_lookup_one_len(MOUNT_CONFIGS_DIR, cfs_fs_pwd(current->fs),
645 strlen(MOUNT_CONFIGS_DIR));
646 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
647 if (IS_ERR(dentry)) {
648 err = PTR_ERR(dentry);
649 CERROR("cannot lookup %s directory: rc = %d\n",
650 MOUNT_CONFIGS_DIR, err);
653 cli->cl_mgc_configs_dir = dentry;
655 /* We take an obd ref to insure that we can't get to mgc_cleanup
656 without calling mgc_fs_cleanup first. */
657 class_incref(obd, "mgc_fs", obd);
659 label = fsfilt_get_label(obd, mnt->mnt_sb);
661 CDEBUG(D_MGC, "MGC using disk labelled=%s\n", label);
663 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
667 fsfilt_put_ops(obd->obd_fsops);
668 obd->obd_fsops = NULL;
669 cli->cl_mgc_vfsmnt = NULL;
670 cfs_up(&cli->cl_mgc_sem);
674 static int mgc_fs_cleanup(struct obd_device *obd)
676 struct client_obd *cli = &obd->u.cli;
680 LASSERT(cli->cl_mgc_vfsmnt != NULL);
682 if (cli->cl_mgc_configs_dir != NULL) {
683 struct lvfs_run_ctxt saved;
684 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
685 l_dput(cli->cl_mgc_configs_dir);
686 cli->cl_mgc_configs_dir = NULL;
687 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
688 class_decref(obd, "mgc_fs", obd);
691 cli->cl_mgc_vfsmnt = NULL;
693 fsfilt_put_ops(obd->obd_fsops);
695 cfs_up(&cli->cl_mgc_sem);
700 static cfs_atomic_t mgc_count = CFS_ATOMIC_INIT(0);
701 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
707 case OBD_CLEANUP_EARLY:
709 case OBD_CLEANUP_EXPORTS:
710 if (cfs_atomic_dec_and_test(&mgc_count)) {
712 /* stop requeue thread */
713 cfs_spin_lock(&config_list_lock);
714 running = rq_state & RQ_RUNNING;
717 cfs_spin_unlock(&config_list_lock);
719 cfs_waitq_signal(&rq_waitq);
720 cfs_wait_for_completion(&rq_exit);
723 obd_cleanup_client_import(obd);
724 rc = obd_llog_finish(obd, 0);
726 CERROR("failed to cleanup llogging subsystems\n");
732 static int mgc_cleanup(struct obd_device *obd)
734 struct client_obd *cli = &obd->u.cli;
738 LASSERT(cli->cl_mgc_vfsmnt == NULL);
740 /* COMPAT_146 - old config logs may have added profiles we don't
742 if (obd->obd_type->typ_refcnt <= 1)
743 /* Only for the last mgc */
744 class_del_profiles();
746 lprocfs_obd_cleanup(obd);
749 rc = client_obd_cleanup(obd);
753 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
755 struct lprocfs_static_vars lvars;
761 rc = client_obd_setup(obd, lcfg);
763 GOTO(err_decref, rc);
765 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
767 CERROR("failed to setup llogging subsystems\n");
768 GOTO(err_cleanup, rc);
771 lprocfs_mgc_init_vars(&lvars);
772 lprocfs_obd_setup(obd, lvars.obd_vars);
773 sptlrpc_lprocfs_cliobd_attach(obd);
775 if (cfs_atomic_inc_return(&mgc_count) == 1) {
777 cfs_waitq_init(&rq_waitq);
779 /* start requeue thread */
780 rc = cfs_create_thread(mgc_requeue_thread, NULL,
783 CERROR("%s: Cannot start requeue thread (%d),"
784 "no more log updates!\n",
786 GOTO(err_cleanup, rc);
788 /* rc is the pid of mgc_requeue_thread. */
795 client_obd_cleanup(obd);
801 /* based on ll_mdc_blocking_ast */
802 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
803 void *data, int flag)
805 struct lustre_handle lockh;
806 struct config_llog_data *cld = (struct config_llog_data *)data;
811 case LDLM_CB_BLOCKING:
812 /* mgs wants the lock, give it up... */
813 LDLM_DEBUG(lock, "MGC blocking CB");
814 ldlm_lock2handle(lock, &lockh);
815 rc = ldlm_cli_cancel(&lockh);
817 case LDLM_CB_CANCELING:
818 /* We've given up the lock, prepare ourselves to update. */
819 LDLM_DEBUG(lock, "MGC cancel CB");
821 CDEBUG(D_MGC, "Lock res "LPX64" (%.8s)\n",
822 lock->l_resource->lr_name.name[0],
823 (char *)&lock->l_resource->lr_name.name[0]);
826 CDEBUG(D_INFO, "missing data, won't requeue\n");
830 /* held at mgc_process_log(). */
831 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
832 /* Are we done with this log? */
833 if (cld->cld_stopping) {
834 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
839 /* Make sure not to re-enqueue when the mgc is stopping
840 (we get called from client_disconnect_export) */
841 if (!lock->l_conn_export ||
842 !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
843 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
850 mgc_requeue_add(cld);
860 /* Not sure where this should go... */
861 #define MGC_ENQUEUE_LIMIT 50
862 #define MGC_TARGET_REG_LIMIT 10
863 #define MGC_SEND_PARAM_LIMIT 10
865 /* Send parameter to MGS*/
866 static int mgc_set_mgs_param(struct obd_export *exp,
867 struct mgs_send_param *msp)
869 struct ptlrpc_request *req;
870 struct mgs_send_param *req_msp, *rep_msp;
874 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
875 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
880 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
882 ptlrpc_req_finished(req);
886 memcpy(req_msp, msp, sizeof(*req_msp));
887 ptlrpc_request_set_replen(req);
889 /* Limit how long we will wait for the enqueue to complete */
890 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
891 rc = ptlrpc_queue_wait(req);
893 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
894 memcpy(msp, rep_msp, sizeof(*rep_msp));
897 ptlrpc_req_finished(req);
902 /* Take a config lock so we can get cancel notifications */
903 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
904 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
905 int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
906 void *data, __u32 lvb_len, void *lvb_swabber,
907 struct lustre_handle *lockh)
909 struct config_llog_data *cld = (struct config_llog_data *)data;
910 struct ldlm_enqueue_info einfo = { type, mode, mgc_blocking_ast,
911 ldlm_completion_ast, NULL, NULL, NULL };
912 struct ptlrpc_request *req;
913 int short_limit = cld_is_sptlrpc(cld);
917 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
918 cld->cld_resid.name[0]);
920 /* We need a callback for every lockholder, so don't try to
921 ldlm_lock_match (see rev 1.1.2.11.2.47) */
922 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
923 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
927 ptlrpc_request_set_replen(req);
929 /* check if this is server or client */
930 if (cld->cld_cfg.cfg_sb) {
931 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
932 if (lsi && (lsi->lsi_flags & LSI_SERVER))
935 /* Limit how long we will wait for the enqueue to complete */
936 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
937 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
939 /* A failed enqueue should still call the mgc_blocking_ast,
940 where it will be requeued if needed ("grant failed"). */
941 ptlrpc_req_finished(req);
945 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
946 __u32 mode, struct lustre_handle *lockh)
950 ldlm_lock_decref(lockh, mode);
955 static void mgc_notify_active(struct obd_device *unused)
957 /* wakeup mgc_requeue_thread to requeue mgc lock */
958 cfs_spin_lock(&config_list_lock);
960 cfs_spin_unlock(&config_list_lock);
961 cfs_waitq_signal(&rq_waitq);
963 /* TODO: Help the MGS rebuild nidtbl. -jay */
966 /* Send target_reg message to MGS */
967 static int mgc_target_register(struct obd_export *exp,
968 struct mgs_target_info *mti)
970 struct ptlrpc_request *req;
971 struct mgs_target_info *req_mti, *rep_mti;
975 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
976 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
981 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
983 ptlrpc_req_finished(req);
987 memcpy(req_mti, mti, sizeof(*req_mti));
988 ptlrpc_request_set_replen(req);
989 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
990 /* Limit how long we will wait for the enqueue to complete */
991 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
993 rc = ptlrpc_queue_wait(req);
995 rep_mti = req_capsule_server_get(&req->rq_pill,
996 &RMF_MGS_TARGET_INFO);
997 memcpy(mti, rep_mti, sizeof(*rep_mti));
998 CDEBUG(D_MGC, "register %s got index = %d\n",
999 mti->mti_svname, mti->mti_stripe_index);
1001 ptlrpc_req_finished(req);
1006 int mgc_set_info_async(struct obd_export *exp, obd_count keylen,
1007 void *key, obd_count vallen, void *val,
1008 struct ptlrpc_request_set *set)
1013 /* Turn off initial_recov after we try all backup servers once */
1014 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1015 struct obd_import *imp = class_exp2cliimp(exp);
1017 if (vallen != sizeof(int))
1019 value = *(int *)val;
1020 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1021 imp->imp_obd->obd_name, value,
1022 imp->imp_deactive, imp->imp_invalid,
1023 imp->imp_replayable, imp->imp_obd->obd_replayable,
1024 ptlrpc_import_state_name(imp->imp_state));
1025 /* Resurrect if we previously died */
1026 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1027 imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1028 ptlrpc_reconnect_import(imp);
1031 /* FIXME move this to mgc_process_config */
1032 if (KEY_IS(KEY_REGISTER_TARGET)) {
1033 struct mgs_target_info *mti;
1034 if (vallen != sizeof(struct mgs_target_info))
1036 mti = (struct mgs_target_info *)val;
1037 CDEBUG(D_MGC, "register_target %s %#x\n",
1038 mti->mti_svname, mti->mti_flags);
1039 rc = mgc_target_register(exp, mti);
1042 if (KEY_IS(KEY_SET_FS)) {
1043 struct super_block *sb = (struct super_block *)val;
1044 struct lustre_sb_info *lsi;
1045 if (vallen != sizeof(struct super_block))
1048 rc = mgc_fs_setup(exp->exp_obd, sb, lsi->lsi_srv_mnt);
1050 CERROR("set_fs got %d\n", rc);
1054 if (KEY_IS(KEY_CLEAR_FS)) {
1057 rc = mgc_fs_cleanup(exp->exp_obd);
1059 CERROR("clear_fs got %d\n", rc);
1063 if (KEY_IS(KEY_SET_INFO)) {
1064 struct mgs_send_param *msp;
1066 msp = (struct mgs_send_param *)val;
1067 rc = mgc_set_mgs_param(exp, msp);
1070 if (KEY_IS(KEY_MGSSEC)) {
1071 struct client_obd *cli = &exp->exp_obd->u.cli;
1072 struct sptlrpc_flavor flvr;
1075 * empty string means using current flavor, if which haven't
1076 * been set yet, set it as null.
1078 * if flavor has been set previously, check the asking flavor
1079 * must match the existing one.
1082 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1088 rc = sptlrpc_parse_flavor(val, &flvr);
1090 CERROR("invalid sptlrpc flavor %s to MGS\n",
1096 * caller already hold a mutex
1098 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1099 cli->cl_flvr_mgc = flvr;
1100 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1101 sizeof(flvr)) != 0) {
1104 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1106 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1107 "currently %s is in use\n",
1117 static int mgc_get_info(struct obd_export *exp, __u32 keylen, void *key,
1118 __u32 *vallen, void *val, struct lov_stripe_md *unused)
1122 if (KEY_IS(KEY_CONN_DATA)) {
1123 struct obd_import *imp = class_exp2cliimp(exp);
1124 struct obd_connect_data *data = val;
1126 if (*vallen == sizeof(*data)) {
1127 *data = imp->imp_connect_data;
1135 static int mgc_import_event(struct obd_device *obd,
1136 struct obd_import *imp,
1137 enum obd_import_event event)
1141 LASSERT(imp->imp_obd == obd);
1142 CDEBUG(D_MGC, "import event %#x\n", event);
1145 case IMP_EVENT_DISCON:
1146 /* MGC imports should not wait for recovery */
1148 case IMP_EVENT_INACTIVE:
1150 case IMP_EVENT_INVALIDATE: {
1151 struct ldlm_namespace *ns = obd->obd_namespace;
1152 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1155 case IMP_EVENT_ACTIVE:
1156 LCONSOLE_WARN("%s: Reactivating import\n", obd->obd_name);
1157 /* Clearing obd_no_recov allows us to continue pinging */
1158 obd->obd_no_recov = 0;
1159 mgc_notify_active(obd);
1163 case IMP_EVENT_DEACTIVATE:
1164 case IMP_EVENT_ACTIVATE:
1167 CERROR("Unknown import event %#x\n", event);
1173 static int mgc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
1174 struct obd_device *tgt, int *index)
1176 struct llog_ctxt *ctxt;
1180 LASSERT(olg == &obd->obd_olg);
1182 rc = llog_setup(obd, olg, LLOG_CONFIG_ORIG_CTXT, tgt, 0, NULL,
1187 rc = llog_setup(obd, olg, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1190 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1192 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1197 llog_initiator_connect(ctxt);
1198 llog_ctxt_put(ctxt);
1200 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1208 static int mgc_llog_finish(struct obd_device *obd, int count)
1210 struct llog_ctxt *ctxt;
1211 int rc = 0, rc2 = 0;
1214 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1216 rc = llog_cleanup(ctxt);
1218 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1220 rc2 = llog_cleanup(ctxt);
1229 CONFIG_READ_NRPAGES_INIT = 1 << (20 - CFS_PAGE_SHIFT),
1230 CONFIG_READ_NRPAGES = 4
1233 static int mgc_apply_recover_logs(struct obd_device *mgc,
1234 struct config_llog_data *cld,
1236 void *data, int datalen)
1238 struct config_llog_instance *cfg = &cld->cld_cfg;
1239 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1240 struct mgs_nidtbl_entry *entry;
1241 struct lustre_cfg *lcfg;
1242 struct lustre_cfg_bufs bufs;
1243 u64 prev_version = 0;
1252 LASSERT(cfg->cfg_instance != NULL);
1253 LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1255 OBD_ALLOC(inst, CFS_PAGE_SIZE);
1259 if (!(lsi->lsi_flags & LSI_SERVER)) {
1260 pos = sprintf(inst, "%p", cfg->cfg_instance);
1262 LASSERT(IS_MDT(lsi->lsi_ldd));
1263 pos = sprintf(inst, "MDT%04x", lsi->lsi_ldd->ldd_svindex);
1268 bufsz = CFS_PAGE_SIZE - pos;
1270 while (datalen > 0) {
1271 int entry_len = sizeof(*entry);
1273 struct obd_device *obd;
1280 if (datalen < sizeof(*entry))
1283 entry = (typeof(entry))(data + off);
1286 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1288 if (entry->mne_nid_count == 0) /* at least one nid entry */
1290 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1293 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1294 if (datalen < entry_len) /* must have entry_len at least */
1297 lustre_swab_mgs_nidtbl_entry(entry);
1298 LASSERT(entry->mne_length <= CFS_PAGE_SIZE);
1299 if (entry->mne_length < entry_len)
1302 off += entry->mne_length;
1303 datalen -= entry->mne_length;
1307 if (entry->mne_version > max_version) {
1308 CERROR("entry index(%lld) is over max_index(%lld)\n",
1309 entry->mne_version, max_version);
1313 if (prev_version >= entry->mne_version) {
1314 CERROR("index unsorted, prev %lld, now %lld\n",
1315 prev_version, entry->mne_version);
1318 prev_version = entry->mne_version;
1321 * Write a string with format "nid::instance" to
1322 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1325 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1326 memset(buf, 0, bufsz);
1330 /* lustre-OST0001-osc-<instance #> */
1331 strcpy(obdname, cld->cld_logname);
1332 cname = strrchr(obdname, '-');
1333 if (cname == NULL) {
1334 CERROR("mgc %s: invalid logname %s\n",
1335 mgc->obd_name, obdname);
1339 pos = cname - obdname;
1341 pos += sprintf(obdname + pos, "-%s%04x",
1342 is_ost ? "OST" : "MDT", entry->mne_index);
1344 cname = is_ost ? "osc" : "mdc",
1345 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1346 lustre_cfg_bufs_reset(&bufs, obdname);
1348 /* find the obd by obdname */
1349 obd = class_name2obd(obdname);
1351 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1352 mgc->obd_name, obdname);
1354 /* this is a safe race, when the ost is starting up...*/
1358 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1361 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1364 /* TODO: iterate all nids to find one */
1365 /* find uuid by nid */
1366 rc = client_import_find_conn(obd->u.cli.cl_import,
1368 (struct obd_uuid *)uuid);
1370 CERROR("mgc: cannot find uuid by nid %s\n",
1371 libcfs_nid2str(entry->u.nids[0]));
1375 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1376 uuid, libcfs_nid2str(entry->u.nids[0]));
1378 pos += strlen(uuid);
1379 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1380 LASSERT(pos < bufsz);
1382 lustre_cfg_bufs_set_string(&bufs, 1, params);
1385 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1387 CERROR("mgc: cannot allocate memory\n");
1391 CDEBUG(D_INFO, "ir apply logs "LPD64"/"LPD64" for %s -> %s\n",
1392 prev_version, max_version, obdname, params);
1394 rc = class_process_config(lcfg);
1395 lustre_cfg_free(lcfg);
1397 CDEBUG(D_INFO, "process config for %s error %d\n",
1400 /* continue, even one with error */
1403 OBD_FREE(inst, CFS_PAGE_SIZE);
1408 * This function is called if this client was notified for target restarting
1409 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery logs.
1411 static int mgc_process_recover_log(struct obd_device *obd,
1412 struct config_llog_data *cld)
1414 struct ptlrpc_request *req = NULL;
1415 struct config_llog_instance *cfg = &cld->cld_cfg;
1416 struct mgs_config_body *body;
1417 struct mgs_config_res *res;
1418 struct ptlrpc_bulk_desc *desc;
1427 /* allocate buffer for bulk transfer.
1428 * if this is the first time for this mgs to read logs,
1429 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1430 * once; otherwise, it only reads increment of logs, this should be
1431 * small and CONFIG_READ_NRPAGES will be used.
1433 nrpages = CONFIG_READ_NRPAGES;
1434 if (cfg->cfg_last_idx == 0) /* the first time */
1435 nrpages = CONFIG_READ_NRPAGES_INIT;
1437 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1439 GOTO(out, rc = -ENOMEM);
1441 for (i = 0; i < nrpages; i++) {
1442 pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1443 if (pages[i] == NULL)
1444 GOTO(out, rc = -ENOMEM);
1448 LASSERT(cld_is_recover(cld));
1449 LASSERT(cfs_mutex_is_locked(&cld->cld_lock));
1450 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1451 &RQF_MGS_CONFIG_READ);
1453 GOTO(out, rc = -ENOMEM);
1455 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1460 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1461 LASSERT(body != NULL);
1462 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1463 strncpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name));
1464 body->mcb_offset = cfg->cfg_last_idx + 1;
1465 body->mcb_type = cld->cld_type;
1466 body->mcb_bits = CFS_PAGE_SHIFT;
1467 body->mcb_units = nrpages;
1469 /* allocate bulk transfer descriptor */
1470 desc = ptlrpc_prep_bulk_imp(req, nrpages, BULK_PUT_SINK,
1473 GOTO(out, rc = -ENOMEM);
1475 for (i = 0; i < nrpages; i++)
1476 ptlrpc_prep_bulk_page(desc, pages[i], 0, CFS_PAGE_SIZE);
1478 ptlrpc_request_set_replen(req);
1479 rc = ptlrpc_queue_wait(req);
1483 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1484 if (res->mcr_size < res->mcr_offset)
1485 GOTO(out, rc = -EINVAL);
1487 /* always update the index even though it might have errors with
1488 * handling the recover logs */
1489 cfg->cfg_last_idx = res->mcr_offset;
1490 eof = res->mcr_offset == res->mcr_size;
1492 CDEBUG(D_INFO, "Latest version "LPD64", more %d.\n",
1493 res->mcr_offset, eof == false);
1495 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1497 GOTO(out, rc = ealen);
1499 if (ealen > nrpages << CFS_PAGE_SHIFT)
1500 GOTO(out, rc = -EINVAL);
1502 if (ealen == 0) { /* no logs transferred */
1508 for (i = 0; i < nrpages && ealen > 0; i++) {
1512 ptr = cfs_kmap(pages[i]);
1513 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1514 min_t(int, ealen, CFS_PAGE_SIZE));
1515 cfs_kunmap(pages[i]);
1517 CWARN("Process recover log %s error %d\n",
1518 cld->cld_logname, rc2);
1522 ealen -= CFS_PAGE_SIZE;
1527 ptlrpc_req_finished(req);
1529 if (rc == 0 && !eof)
1533 for (i = 0; i < nrpages; i++) {
1534 if (pages[i] == NULL)
1536 cfs_free_page(pages[i]);
1538 OBD_FREE(pages, sizeof(*pages) * nrpages);
1543 /* identical to mgs_log_is_empty */
1544 static int mgc_llog_is_empty(struct obd_device *obd, struct llog_ctxt *ctxt,
1547 struct lvfs_run_ctxt saved;
1548 struct llog_handle *llh;
1551 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1552 rc = llog_create(ctxt, &llh, NULL, name);
1554 llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1555 rc = llog_get_size(llh);
1558 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1559 /* header is record 1 */
1563 static int mgc_copy_handler(struct llog_handle *llh, struct llog_rec_hdr *rec,
1566 struct llog_rec_hdr local_rec = *rec;
1567 struct llog_handle *local_llh = (struct llog_handle *)data;
1568 char *cfg_buf = (char*) (rec + 1);
1569 struct lustre_cfg *lcfg;
1573 /* Append all records */
1574 local_rec.lrh_len -= sizeof(*rec) + sizeof(struct llog_rec_tail);
1575 rc = llog_write_rec(local_llh, &local_rec, NULL, 0,
1576 (void *)cfg_buf, -1);
1578 lcfg = (struct lustre_cfg *)cfg_buf;
1579 CDEBUG(D_INFO, "idx=%d, rc=%d, len=%d, cmd %x %s %s\n",
1580 rec->lrh_index, rc, rec->lrh_len, lcfg->lcfg_command,
1581 lustre_cfg_string(lcfg, 0), lustre_cfg_string(lcfg, 1));
1586 /* Copy a remote log locally */
1587 static int mgc_copy_llog(struct obd_device *obd, struct llog_ctxt *rctxt,
1588 struct llog_ctxt *lctxt, char *logname)
1590 struct llog_handle *local_llh, *remote_llh;
1591 struct obd_uuid *uuid;
1596 /* Write new log to a temp name, then vfs_rename over logname
1597 upon successful completion. */
1599 OBD_ALLOC(temp_log, strlen(logname) + 1);
1602 sprintf(temp_log, "%sT", logname);
1604 /* Make sure there's no old temp log */
1605 rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1608 rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, NULL);
1611 rc = llog_destroy(local_llh);
1612 llog_free_handle(local_llh);
1616 /* open local log */
1617 rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1621 /* set the log header uuid for fun */
1622 OBD_ALLOC_PTR(uuid);
1623 obd_str2uuid(uuid, logname);
1624 rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, uuid);
1627 GOTO(out_closel, rc);
1629 /* open remote log */
1630 rc = llog_create(rctxt, &remote_llh, NULL, logname);
1632 GOTO(out_closel, rc);
1633 rc = llog_init_handle(remote_llh, LLOG_F_IS_PLAIN, NULL);
1635 GOTO(out_closer, rc);
1637 /* Copy remote log */
1638 rc = llog_process(remote_llh, mgc_copy_handler,(void *)local_llh, NULL);
1641 rc2 = llog_close(remote_llh);
1645 rc2 = llog_close(local_llh);
1649 /* We've copied the remote log to the local temp log, now
1650 replace the old local log with the temp log. */
1652 struct client_obd *cli = &obd->u.cli;
1654 LASSERT(cli->cl_mgc_configs_dir);
1655 rc = lustre_rename(cli->cl_mgc_configs_dir, cli->cl_mgc_vfsmnt,
1658 CDEBUG(D_MGC, "Copied remote log %s (%d)\n", logname, rc);
1661 CERROR("Failed to copy remote log %s (%d)\n", logname, rc);
1662 OBD_FREE(temp_log, strlen(logname) + 1);
1666 /* local_only means it cannot get remote llogs */
1667 static int mgc_process_cfg_log(struct obd_device *mgc,
1668 struct config_llog_data *cld,
1671 struct llog_ctxt *ctxt, *lctxt = NULL;
1672 struct client_obd *cli = &mgc->u.cli;
1673 struct lvfs_run_ctxt *saved_ctxt;
1674 struct lustre_sb_info *lsi = NULL;
1675 int rc = 0, must_pop = 0;
1676 bool sptlrpc_started = false;
1681 LASSERT(cfs_mutex_is_locked(&cld->cld_lock));
1684 * local copy of sptlrpc log is controlled elsewhere, don't try to
1687 if (cld_is_sptlrpc(cld) && local_only)
1690 if (cld->cld_cfg.cfg_sb)
1691 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1693 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1695 CERROR("missing llog context\n");
1699 OBD_ALLOC_PTR(saved_ctxt);
1700 if (saved_ctxt == NULL)
1703 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1705 /* Copy the setup log locally if we can. Don't mess around if we're
1706 running an MGS though (logs are already local). */
1707 if (lctxt && lsi && (lsi->lsi_flags & LSI_SERVER) &&
1708 (lsi->lsi_srv_mnt == cli->cl_mgc_vfsmnt) &&
1709 !IS_MGS(lsi->lsi_ldd)) {
1710 push_ctxt(saved_ctxt, &mgc->obd_lvfs_ctxt, NULL);
1713 /* Only try to copy log if we have the lock. */
1714 rc = mgc_copy_llog(mgc, ctxt, lctxt, cld->cld_logname);
1715 if (local_only || rc) {
1716 if (mgc_llog_is_empty(mgc, lctxt, cld->cld_logname)) {
1717 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1718 "log %s and no local copy."
1719 "\n", cld->cld_logname);
1720 GOTO(out_pop, rc = -ENOTCONN);
1722 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1723 "copy for now, will try to update later.\n",
1726 /* Now, whether we copied or not, start using the local llog.
1727 If we failed to copy, we'll start using whatever the old
1729 llog_ctxt_put(ctxt);
1732 } else if (local_only) { /* no local log at client side */
1733 GOTO(out_pop, rc = -EIO);
1736 if (cld_is_sptlrpc(cld)) {
1737 sptlrpc_conf_log_update_begin(cld->cld_logname);
1738 sptlrpc_started = true;
1741 /* logname and instance info should be the same, so use our
1742 copy of the instance for the update. The cfg_last_idx will
1744 rc = class_config_parse_llog(ctxt, cld->cld_logname, &cld->cld_cfg);
1748 llog_ctxt_put(ctxt);
1750 llog_ctxt_put(lctxt);
1752 pop_ctxt(saved_ctxt, &mgc->obd_lvfs_ctxt, NULL);
1754 OBD_FREE_PTR(saved_ctxt);
1756 * update settings on existing OBDs. doing it inside
1757 * of llog_process_lock so no device is attaching/detaching
1759 * the logname must be <fsname>-sptlrpc
1761 if (sptlrpc_started) {
1762 LASSERT(cld_is_sptlrpc(cld));
1763 sptlrpc_conf_log_update_end(cld->cld_logname);
1764 class_notify_sptlrpc_conf(cld->cld_logname,
1765 strlen(cld->cld_logname) -
1766 strlen("-sptlrpc"));
1772 /** Get a config log from the MGS and process it.
1773 * This func is called for both clients and servers.
1774 * Copy the log locally before parsing it if appropriate (non-MGS server)
1776 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1778 struct lustre_handle lockh = { 0 };
1779 int rc = 0, rcl, flags = LDLM_FL_NO_LRU;
1784 /* I don't want multiple processes running process_log at once --
1785 sounds like badness. It actually might be fine, as long as
1786 we're not trying to update from the same log
1787 simultaneously (in which case we should use a per-log sem.) */
1788 cfs_mutex_lock(&cld->cld_lock);
1789 if (cld->cld_stopping) {
1790 cfs_mutex_unlock(&cld->cld_lock);
1794 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1796 CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
1797 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1799 /* Get the cfg lock on the llog */
1800 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1801 LCK_CR, &flags, NULL, NULL, NULL,
1802 cld, 0, NULL, &lockh);
1804 /* Get the cld, it will be released in mgc_blocking_ast. */
1805 config_log_get(cld);
1806 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1809 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1811 /* mark cld_lostlock so that it will requeue
1812 * after MGC becomes available. */
1813 cld->cld_lostlock = 1;
1814 /* Get extra reference, it will be put in requeue thread */
1815 config_log_get(cld);
1819 if (cld_is_recover(cld)) {
1820 rc = 0; /* this is not a fatal error for recover log */
1822 rc = mgc_process_recover_log(mgc, cld);
1824 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1827 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1828 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1830 cfs_mutex_unlock(&cld->cld_lock);
1832 /* Now drop the lock so MGS can revoke it */
1834 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1837 CERROR("Can't drop cfg lock: %d\n", rcl);
1844 /** Called from lustre_process_log.
1845 * LCFG_LOG_START gets the config log from the MGS, processes it to start
1846 * any services, and adds it to the list logs to watch (follow).
1848 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1850 struct lustre_cfg *lcfg = buf;
1851 struct config_llog_instance *cfg = NULL;
1856 switch(lcfg->lcfg_command) {
1857 case LCFG_LOV_ADD_OBD: {
1858 /* Overloading this cfg command: register a new target */
1859 struct mgs_target_info *mti;
1861 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1862 sizeof(struct mgs_target_info))
1863 GOTO(out, rc = -EINVAL);
1865 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1866 CDEBUG(D_MGC, "add_target %s %#x\n",
1867 mti->mti_svname, mti->mti_flags);
1868 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1871 case LCFG_LOV_DEL_OBD:
1872 /* Unregister has no meaning at the moment. */
1873 CERROR("lov_del_obd unimplemented\n");
1876 case LCFG_SPTLRPC_CONF: {
1877 rc = sptlrpc_process_config(lcfg);
1880 case LCFG_LOG_START: {
1881 struct config_llog_data *cld;
1882 struct super_block *sb;
1884 logname = lustre_cfg_string(lcfg, 1);
1885 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1886 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1888 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1891 /* We're only called through here on the initial mount */
1892 rc = config_log_add(obd, logname, cfg, sb);
1895 cld = config_log_find(logname, cfg);
1902 /* FIXME only set this for old logs! Right now this forces
1903 us to always skip the "inside markers" check */
1904 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1906 rc = mgc_process_log(obd, cld);
1907 if (rc == 0 && cld->cld_recover != NULL) {
1908 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
1909 imp_connect_data, IMP_RECOV)) {
1910 rc = mgc_process_log(obd, cld->cld_recover);
1912 struct config_llog_data *cir = cld->cld_recover;
1913 cld->cld_recover = NULL;
1914 config_log_put(cir);
1917 CERROR("Cannot process recover llog %d\n", rc);
1919 config_log_put(cld);
1923 case LCFG_LOG_END: {
1924 logname = lustre_cfg_string(lcfg, 1);
1926 if (lcfg->lcfg_bufcount >= 2)
1927 cfg = (struct config_llog_instance *)lustre_cfg_buf(
1929 rc = config_log_end(logname, cfg);
1933 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1934 GOTO(out, rc = -EINVAL);
1942 struct obd_ops mgc_obd_ops = {
1943 .o_owner = THIS_MODULE,
1944 .o_setup = mgc_setup,
1945 .o_precleanup = mgc_precleanup,
1946 .o_cleanup = mgc_cleanup,
1947 .o_add_conn = client_import_add_conn,
1948 .o_del_conn = client_import_del_conn,
1949 .o_connect = client_connect_import,
1950 .o_disconnect = client_disconnect_export,
1951 //.o_enqueue = mgc_enqueue,
1952 .o_cancel = mgc_cancel,
1953 //.o_iocontrol = mgc_iocontrol,
1954 .o_set_info_async = mgc_set_info_async,
1955 .o_get_info = mgc_get_info,
1956 .o_import_event = mgc_import_event,
1957 .o_llog_init = mgc_llog_init,
1958 .o_llog_finish = mgc_llog_finish,
1959 .o_process_config = mgc_process_config,
1962 int __init mgc_init(void)
1964 return class_register_type(&mgc_obd_ops, NULL, NULL,
1965 LUSTRE_MGC_NAME, NULL);
1969 static void /*__exit*/ mgc_exit(void)
1971 class_unregister_type(LUSTRE_MGC_NAME);
1974 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1975 MODULE_DESCRIPTION("Lustre Management Client");
1976 MODULE_LICENSE("GPL");
1978 module_init(mgc_init);
1979 module_exit(mgc_exit);