4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
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>
41 #define DEBUG_SUBSYSTEM S_MGC
42 #define D_MGC D_CONFIG /*|D_WARNING*/
45 # include <linux/module.h>
46 # include <linux/pagemap.h>
47 # include <linux/miscdevice.h>
48 # include <linux/init.h>
50 # include <liblustre.h>
53 #include <obd_class.h>
54 #include <lustre_dlm.h>
55 #include <lprocfs_status.h>
56 #include <lustre_log.h>
57 #include <lustre_fsfilt.h>
58 #include <lustre_disk.h>
59 #include "mgc_internal.h"
61 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id,
67 CERROR("name too long: %s\n", name);
71 CERROR("missing name: %s\n", name);
74 memcpy(&resname, name, len);
76 /* Always use the same endianness for the resid */
77 memset(res_id, 0, sizeof(*res_id));
78 res_id->name[0] = cpu_to_le64(resname);
79 /* XXX: unfortunately, sptlprc and config llog share one lock */
82 case CONFIG_T_SPTLRPC:
85 case CONFIG_T_RECOVER:
91 res_id->name[1] = cpu_to_le64(resname);
92 CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.8s)\n", name,
93 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
97 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id, int type)
99 /* fsname is at most 8 chars long, maybe contain "-".
100 * e.g. "lustre", "SUN-000" */
101 return mgc_name2resid(fsname, strlen(fsname), res_id, type);
103 EXPORT_SYMBOL(mgc_fsname2resid);
105 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id, int type)
110 /* logname consists of "fsname-nodetype".
111 * e.g. "lustre-MDT0001", "SUN-000-client" */
112 name_end = strrchr(logname, '-');
114 len = name_end - logname;
115 return mgc_name2resid(logname, len, res_id, type);
118 /********************** config llog list **********************/
119 static CFS_LIST_HEAD(config_llog_list);
120 static DEFINE_SPINLOCK(config_list_lock);
122 /* Take a reference to a config log */
123 static int config_log_get(struct config_llog_data *cld)
126 cfs_atomic_inc(&cld->cld_refcount);
127 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
128 cfs_atomic_read(&cld->cld_refcount));
132 /* Drop a reference to a config log. When no longer referenced,
133 we can free the config log data */
134 static void config_log_put(struct config_llog_data *cld)
138 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
139 cfs_atomic_read(&cld->cld_refcount));
140 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
142 /* spinlock to make sure no item with 0 refcount in the list */
143 if (cfs_atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
144 cfs_list_del(&cld->cld_list_chain);
145 spin_unlock(&config_list_lock);
147 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
149 if (cld->cld_recover)
150 config_log_put(cld->cld_recover);
151 if (cld->cld_sptlrpc)
152 config_log_put(cld->cld_sptlrpc);
153 if (cld_is_sptlrpc(cld))
154 sptlrpc_conf_log_stop(cld->cld_logname);
156 class_export_put(cld->cld_mgcexp);
157 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
163 /* Find a config log by name */
165 struct config_llog_data *config_log_find(char *logname,
166 struct config_llog_instance *cfg)
168 struct config_llog_data *cld;
169 struct config_llog_data *found = NULL;
173 LASSERT(logname != NULL);
175 instance = cfg ? cfg->cfg_instance : NULL;
176 spin_lock(&config_list_lock);
177 cfs_list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
178 /* check if instance equals */
179 if (instance != cld->cld_cfg.cfg_instance)
182 /* instance may be NULL, should check name */
183 if (strcmp(logname, cld->cld_logname) == 0) {
189 cfs_atomic_inc(&found->cld_refcount);
190 LASSERT(found->cld_stopping == 0 || cld_is_sptlrpc(found) == 0);
192 spin_unlock(&config_list_lock);
197 struct config_llog_data *do_config_log_add(struct obd_device *obd,
200 struct config_llog_instance *cfg,
201 struct super_block *sb)
203 struct config_llog_data *cld;
207 CDEBUG(D_MGC, "do adding config log %s:%p\n", logname,
208 cfg ? cfg->cfg_instance : 0);
210 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
212 RETURN(ERR_PTR(-ENOMEM));
214 strcpy(cld->cld_logname, logname);
218 cld->cld_cfg.cfg_callback = class_config_llog_handler;
219 mutex_init(&cld->cld_lock);
220 cld->cld_cfg.cfg_last_idx = 0;
221 cld->cld_cfg.cfg_flags = 0;
222 cld->cld_cfg.cfg_sb = sb;
223 cld->cld_type = type;
224 cfs_atomic_set(&cld->cld_refcount, 1);
226 /* Keep the mgc around until we are done */
227 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
229 if (cld_is_sptlrpc(cld)) {
230 sptlrpc_conf_log_start(logname);
231 cld->cld_cfg.cfg_obdname = obd->obd_name;
234 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
236 spin_lock(&config_list_lock);
237 cfs_list_add(&cld->cld_list_chain, &config_llog_list);
238 spin_unlock(&config_list_lock);
245 if (cld_is_sptlrpc(cld)) {
246 rc = mgc_process_log(obd, cld);
247 if (rc && rc != -ENOENT)
248 CERROR("failed processing sptlrpc log: %d\n", rc);
254 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
256 struct config_llog_instance *cfg,
257 struct super_block *sb)
259 struct config_llog_instance lcfg = *cfg;
260 struct lustre_sb_info *lsi = s2lsi(sb);
261 struct config_llog_data *cld;
267 /* for osp-on-ost, see lustre_start_osp() */
268 if (IS_MDT(lsi) && lcfg.cfg_instance)
271 /* we have to use different llog for clients and mdts for cmd
272 * where only clients are notified if one of cmd server restarts */
273 LASSERT(strlen(fsname) < sizeof(logname) / 2);
274 strcpy(logname, fsname);
275 if (IS_SERVER(lsi)) { /* mdt */
276 LASSERT(lcfg.cfg_instance == NULL);
277 lcfg.cfg_instance = sb;
278 strcat(logname, "-mdtir");
280 LASSERT(lcfg.cfg_instance != NULL);
281 strcat(logname, "-cliir");
284 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
289 /** Add this log to the list of active logs watched by an MGC.
290 * Active means we're watching for updates.
291 * We have one active log per "mount" - client instance or servername.
292 * Each instance may be at a different point in the log.
294 static int config_log_add(struct obd_device *obd, char *logname,
295 struct config_llog_instance *cfg,
296 struct super_block *sb)
298 struct lustre_sb_info *lsi = s2lsi(sb);
299 struct config_llog_data *cld;
300 struct config_llog_data *sptlrpc_cld;
305 CDEBUG(D_MGC, "adding config log %s:%p\n", logname, cfg->cfg_instance);
308 * for each regular log, the depended sptlrpc log name is
309 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
311 ptr = strrchr(logname, '-');
312 if (ptr == NULL || ptr - logname > 8) {
313 CERROR("logname %s is too long\n", logname);
317 memcpy(seclogname, logname, ptr - logname);
318 strcpy(seclogname + (ptr - logname), "-sptlrpc");
320 sptlrpc_cld = config_log_find(seclogname, NULL);
321 if (sptlrpc_cld == NULL) {
322 sptlrpc_cld = do_config_log_add(obd, seclogname,
323 CONFIG_T_SPTLRPC, NULL, NULL);
324 if (IS_ERR(sptlrpc_cld)) {
325 CERROR("can't create sptlrpc log: %s\n", seclogname);
326 RETURN(PTR_ERR(sptlrpc_cld));
330 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
332 CERROR("can't create log: %s\n", logname);
333 config_log_put(sptlrpc_cld);
334 RETURN(PTR_ERR(cld));
337 cld->cld_sptlrpc = sptlrpc_cld;
339 LASSERT(lsi->lsi_lmd);
340 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)) {
341 struct config_llog_data *recover_cld;
342 *strrchr(seclogname, '-') = 0;
343 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
344 if (IS_ERR(recover_cld)) {
346 RETURN(PTR_ERR(recover_cld));
348 cld->cld_recover = recover_cld;
354 DEFINE_MUTEX(llog_process_lock);
356 /** Stop watching for updates on this log.
358 static int config_log_end(char *logname, struct config_llog_instance *cfg)
360 struct config_llog_data *cld;
361 struct config_llog_data *cld_sptlrpc = NULL;
362 struct config_llog_data *cld_recover = NULL;
366 cld = config_log_find(logname, cfg);
370 mutex_lock(&cld->cld_lock);
372 * if cld_stopping is set, it means we didn't start the log thus
373 * not owning the start ref. this can happen after previous umount:
374 * the cld still hanging there waiting for lock cancel, and we
375 * remount again but failed in the middle and call log_end without
378 if (unlikely(cld->cld_stopping)) {
379 mutex_unlock(&cld->cld_lock);
380 /* drop the ref from the find */
385 cld->cld_stopping = 1;
387 cld_recover = cld->cld_recover;
388 cld->cld_recover = NULL;
389 mutex_unlock(&cld->cld_lock);
392 mutex_lock(&cld_recover->cld_lock);
393 cld_recover->cld_stopping = 1;
394 mutex_unlock(&cld_recover->cld_lock);
395 config_log_put(cld_recover);
398 spin_lock(&config_list_lock);
399 cld_sptlrpc = cld->cld_sptlrpc;
400 cld->cld_sptlrpc = NULL;
401 spin_unlock(&config_list_lock);
404 config_log_put(cld_sptlrpc);
406 /* drop the ref from the find */
408 /* drop the start ref */
411 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
416 int lprocfs_mgc_rd_ir_state(char *page, char **start, off_t off,
417 int count, int *eof, void *data)
419 struct obd_device *obd = data;
420 struct obd_import *imp = obd->u.cli.cl_import;
421 struct obd_connect_data *ocd = &imp->imp_connect_data;
422 struct config_llog_data *cld;
426 rc = snprintf(page, count, "imperative_recovery: %s\n",
427 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
428 rc += snprintf(page + rc, count - rc, "client_state:\n");
430 spin_lock(&config_list_lock);
431 cfs_list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
432 if (cld->cld_recover == NULL)
434 rc += snprintf(page + rc, count - rc,
435 " - { client: %s, nidtbl_version: %u }\n",
437 cld->cld_recover->cld_cfg.cfg_last_idx);
439 spin_unlock(&config_list_lock);
444 /* reenqueue any lost locks */
445 #define RQ_RUNNING 0x1
449 static int rq_state = 0;
450 static cfs_waitq_t rq_waitq;
451 static DECLARE_COMPLETION(rq_exit);
453 static void do_requeue(struct config_llog_data *cld)
456 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
458 /* Do not run mgc_process_log on a disconnected export or an
459 export which is being disconnected. Take the client
460 semaphore to make the check non-racy. */
461 down_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
462 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
463 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
464 mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
466 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
469 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
474 /* this timeout represents how many seconds MGC should wait before
475 * requeue config and recover lock to the MGS. We need to randomize this
476 * in order to not flood the MGS.
478 #define MGC_TIMEOUT_MIN_SECONDS 5
479 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
481 static int mgc_requeue_thread(void *data)
483 char name[] = "ll_cfg_requeue";
489 CDEBUG(D_MGC, "Starting requeue thread\n");
491 /* Keep trying failed locks periodically */
492 spin_lock(&config_list_lock);
493 rq_state |= RQ_RUNNING;
495 struct l_wait_info lwi;
496 struct config_llog_data *cld, *cld_prev;
497 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
498 int stopped = !!(rq_state & RQ_STOP);
501 /* Any new or requeued lostlocks will change the state */
502 rq_state &= ~(RQ_NOW | RQ_LATER);
503 spin_unlock(&config_list_lock);
505 /* Always wait a few seconds to allow the server who
506 caused the lock revocation to finish its setup, plus some
507 random so everyone doesn't try to reconnect at once. */
508 to = MGC_TIMEOUT_MIN_SECONDS * CFS_HZ;
509 to += rand * CFS_HZ / 100; /* rand is centi-seconds */
510 lwi = LWI_TIMEOUT(to, NULL, NULL);
511 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi);
514 * iterate & processing through the list. for each cld, process
515 * its depending sptlrpc cld firstly (if any) and then itself.
517 * it's guaranteed any item in the list must have
518 * reference > 0; and if cld_lostlock is set, at
519 * least one reference is taken by the previous enqueue.
523 spin_lock(&config_list_lock);
524 cfs_list_for_each_entry(cld, &config_llog_list,
526 if (!cld->cld_lostlock)
529 spin_unlock(&config_list_lock);
531 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
533 /* Whether we enqueued again or not in mgc_process_log,
534 * we're done with the ref from the old enqueue */
536 config_log_put(cld_prev);
539 cld->cld_lostlock = 0;
540 if (likely(!stopped))
543 spin_lock(&config_list_lock);
545 spin_unlock(&config_list_lock);
547 config_log_put(cld_prev);
549 /* break after scanning the list so that we can drop
550 * refcount to losing lock clds */
551 if (unlikely(stopped)) {
552 spin_lock(&config_list_lock);
556 /* Wait a bit to see if anyone else needs a requeue */
557 lwi = (struct l_wait_info) { 0 };
558 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
560 spin_lock(&config_list_lock);
562 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
563 rq_state &= ~RQ_RUNNING;
564 spin_unlock(&config_list_lock);
568 CDEBUG(D_MGC, "Ending requeue thread\n");
572 /* Add a cld to the list to requeue. Start the requeue thread if needed.
573 We are responsible for dropping the config log reference from here on out. */
574 static void mgc_requeue_add(struct config_llog_data *cld)
578 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
579 cld->cld_logname, cfs_atomic_read(&cld->cld_refcount),
580 cld->cld_stopping, rq_state);
581 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
583 mutex_lock(&cld->cld_lock);
584 if (cld->cld_stopping || cld->cld_lostlock) {
585 mutex_unlock(&cld->cld_lock);
588 /* this refcount will be released in mgc_requeue_thread. */
590 cld->cld_lostlock = 1;
591 mutex_unlock(&cld->cld_lock);
593 /* Hold lock for rq_state */
594 spin_lock(&config_list_lock);
595 if (rq_state & RQ_STOP) {
596 spin_unlock(&config_list_lock);
597 cld->cld_lostlock = 0;
601 spin_unlock(&config_list_lock);
602 cfs_waitq_signal(&rq_waitq);
607 /********************** class fns **********************/
609 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb,
610 struct vfsmount *mnt)
612 struct lvfs_run_ctxt saved;
613 struct lustre_sb_info *lsi = s2lsi(sb);
614 struct client_obd *cli = &obd->u.cli;
615 struct dentry *dentry;
621 LASSERT(lsi->lsi_srv_mnt == mnt);
623 /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
624 down(&cli->cl_mgc_sem);
626 cfs_cleanup_group_info();
628 obd->obd_fsops = fsfilt_get_ops(lsi->lsi_fstype);
629 if (IS_ERR(obd->obd_fsops)) {
630 up(&cli->cl_mgc_sem);
631 CERROR("%s: No fstype %s: rc = %ld\n", lsi->lsi_fstype,
632 obd->obd_name, PTR_ERR(obd->obd_fsops));
633 RETURN(PTR_ERR(obd->obd_fsops));
636 cli->cl_mgc_vfsmnt = mnt;
637 err = fsfilt_setup(obd, mnt->mnt_sb);
641 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
642 obd->obd_lvfs_ctxt.pwdmnt = mnt;
643 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
644 obd->obd_lvfs_ctxt.fs = get_ds();
646 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
647 dentry = ll_lookup_one_len(MOUNT_CONFIGS_DIR, cfs_fs_pwd(current->fs),
648 strlen(MOUNT_CONFIGS_DIR));
649 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
650 if (IS_ERR(dentry)) {
651 err = PTR_ERR(dentry);
652 CERROR("cannot lookup %s directory: rc = %d\n",
653 MOUNT_CONFIGS_DIR, err);
656 cli->cl_mgc_configs_dir = dentry;
658 /* We take an obd ref to insure that we can't get to mgc_cleanup
659 without calling mgc_fs_cleanup first. */
660 class_incref(obd, "mgc_fs", obd);
662 label = fsfilt_get_label(obd, mnt->mnt_sb);
664 CDEBUG(D_MGC, "MGC using disk labelled=%s\n", label);
666 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
670 fsfilt_put_ops(obd->obd_fsops);
671 obd->obd_fsops = NULL;
672 cli->cl_mgc_vfsmnt = NULL;
673 up(&cli->cl_mgc_sem);
677 static int mgc_fs_cleanup(struct obd_device *obd)
679 struct client_obd *cli = &obd->u.cli;
683 LASSERT(cli->cl_mgc_vfsmnt != NULL);
685 if (cli->cl_mgc_configs_dir != NULL) {
686 struct lvfs_run_ctxt saved;
687 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
688 l_dput(cli->cl_mgc_configs_dir);
689 cli->cl_mgc_configs_dir = NULL;
690 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
691 class_decref(obd, "mgc_fs", obd);
694 cli->cl_mgc_vfsmnt = NULL;
696 fsfilt_put_ops(obd->obd_fsops);
698 up(&cli->cl_mgc_sem);
703 static cfs_atomic_t mgc_count = CFS_ATOMIC_INIT(0);
704 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
710 case OBD_CLEANUP_EARLY:
712 case OBD_CLEANUP_EXPORTS:
713 if (cfs_atomic_dec_and_test(&mgc_count)) {
715 /* stop requeue thread */
716 spin_lock(&config_list_lock);
717 running = rq_state & RQ_RUNNING;
720 spin_unlock(&config_list_lock);
722 cfs_waitq_signal(&rq_waitq);
723 wait_for_completion(&rq_exit);
726 obd_cleanup_client_import(obd);
727 rc = obd_llog_finish(obd, 0);
729 CERROR("failed to cleanup llogging subsystems\n");
735 static int mgc_cleanup(struct obd_device *obd)
737 struct client_obd *cli = &obd->u.cli;
741 LASSERT(cli->cl_mgc_vfsmnt == NULL);
743 /* COMPAT_146 - old config logs may have added profiles we don't
745 if (obd->obd_type->typ_refcnt <= 1)
746 /* Only for the last mgc */
747 class_del_profiles();
749 lprocfs_obd_cleanup(obd);
752 rc = client_obd_cleanup(obd);
756 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
758 struct lprocfs_static_vars lvars;
764 rc = client_obd_setup(obd, lcfg);
766 GOTO(err_decref, rc);
768 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
770 CERROR("failed to setup llogging subsystems\n");
771 GOTO(err_cleanup, rc);
774 lprocfs_mgc_init_vars(&lvars);
775 lprocfs_obd_setup(obd, lvars.obd_vars);
776 sptlrpc_lprocfs_cliobd_attach(obd);
778 if (cfs_atomic_inc_return(&mgc_count) == 1) {
780 cfs_waitq_init(&rq_waitq);
782 /* start requeue thread */
783 rc = cfs_create_thread(mgc_requeue_thread, NULL,
786 CERROR("%s: Cannot start requeue thread (%d),"
787 "no more log updates!\n",
789 GOTO(err_cleanup, rc);
791 /* rc is the pid of mgc_requeue_thread. */
798 client_obd_cleanup(obd);
804 /* based on ll_mdc_blocking_ast */
805 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
806 void *data, int flag)
808 struct lustre_handle lockh;
809 struct config_llog_data *cld = (struct config_llog_data *)data;
814 case LDLM_CB_BLOCKING:
815 /* mgs wants the lock, give it up... */
816 LDLM_DEBUG(lock, "MGC blocking CB");
817 ldlm_lock2handle(lock, &lockh);
818 rc = ldlm_cli_cancel(&lockh);
820 case LDLM_CB_CANCELING:
821 /* We've given up the lock, prepare ourselves to update. */
822 LDLM_DEBUG(lock, "MGC cancel CB");
824 CDEBUG(D_MGC, "Lock res "LPX64" (%.8s)\n",
825 lock->l_resource->lr_name.name[0],
826 (char *)&lock->l_resource->lr_name.name[0]);
829 CDEBUG(D_INFO, "missing data, won't requeue\n");
833 /* held at mgc_process_log(). */
834 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
835 /* Are we done with this log? */
836 if (cld->cld_stopping) {
837 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
842 /* Make sure not to re-enqueue when the mgc is stopping
843 (we get called from client_disconnect_export) */
844 if (!lock->l_conn_export ||
845 !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
846 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
853 mgc_requeue_add(cld);
863 /* Not sure where this should go... */
864 #define MGC_ENQUEUE_LIMIT 50
865 #define MGC_TARGET_REG_LIMIT 10
866 #define MGC_SEND_PARAM_LIMIT 10
868 /* Send parameter to MGS*/
869 static int mgc_set_mgs_param(struct obd_export *exp,
870 struct mgs_send_param *msp)
872 struct ptlrpc_request *req;
873 struct mgs_send_param *req_msp, *rep_msp;
877 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
878 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
883 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
885 ptlrpc_req_finished(req);
889 memcpy(req_msp, msp, sizeof(*req_msp));
890 ptlrpc_request_set_replen(req);
892 /* Limit how long we will wait for the enqueue to complete */
893 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
894 rc = ptlrpc_queue_wait(req);
896 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
897 memcpy(msp, rep_msp, sizeof(*rep_msp));
900 ptlrpc_req_finished(req);
905 /* Take a config lock so we can get cancel notifications */
906 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
907 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
908 __u64 *flags, void *bl_cb, void *cp_cb, void *gl_cb,
909 void *data, __u32 lvb_len, void *lvb_swabber,
910 struct lustre_handle *lockh)
912 struct config_llog_data *cld = (struct config_llog_data *)data;
913 struct ldlm_enqueue_info einfo = { type, mode, mgc_blocking_ast,
914 ldlm_completion_ast, NULL, NULL, NULL };
915 struct ptlrpc_request *req;
916 int short_limit = cld_is_sptlrpc(cld);
920 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
921 cld->cld_resid.name[0]);
923 /* We need a callback for every lockholder, so don't try to
924 ldlm_lock_match (see rev 1.1.2.11.2.47) */
925 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
926 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
931 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
932 ptlrpc_request_set_replen(req);
934 /* check if this is server or client */
935 if (cld->cld_cfg.cfg_sb) {
936 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
937 if (lsi && IS_SERVER(lsi))
940 /* Limit how long we will wait for the enqueue to complete */
941 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
942 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
943 NULL, 0, LVB_T_NONE, lockh, 0);
944 /* A failed enqueue should still call the mgc_blocking_ast,
945 where it will be requeued if needed ("grant failed"). */
946 ptlrpc_req_finished(req);
950 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
951 __u32 mode, struct lustre_handle *lockh)
955 ldlm_lock_decref(lockh, mode);
960 static void mgc_notify_active(struct obd_device *unused)
962 /* wakeup mgc_requeue_thread to requeue mgc lock */
963 spin_lock(&config_list_lock);
965 spin_unlock(&config_list_lock);
966 cfs_waitq_signal(&rq_waitq);
968 /* TODO: Help the MGS rebuild nidtbl. -jay */
971 /* Send target_reg message to MGS */
972 static int mgc_target_register(struct obd_export *exp,
973 struct mgs_target_info *mti)
975 struct ptlrpc_request *req;
976 struct mgs_target_info *req_mti, *rep_mti;
980 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
981 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
986 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
988 ptlrpc_req_finished(req);
992 memcpy(req_mti, mti, sizeof(*req_mti));
993 ptlrpc_request_set_replen(req);
994 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
995 /* Limit how long we will wait for the enqueue to complete */
996 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
998 rc = ptlrpc_queue_wait(req);
1000 rep_mti = req_capsule_server_get(&req->rq_pill,
1001 &RMF_MGS_TARGET_INFO);
1002 memcpy(mti, rep_mti, sizeof(*rep_mti));
1003 CDEBUG(D_MGC, "register %s got index = %d\n",
1004 mti->mti_svname, mti->mti_stripe_index);
1006 ptlrpc_req_finished(req);
1011 int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1012 obd_count keylen, void *key, obd_count vallen,
1013 void *val, struct ptlrpc_request_set *set)
1018 /* Turn off initial_recov after we try all backup servers once */
1019 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1020 struct obd_import *imp = class_exp2cliimp(exp);
1022 if (vallen != sizeof(int))
1024 value = *(int *)val;
1025 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1026 imp->imp_obd->obd_name, value,
1027 imp->imp_deactive, imp->imp_invalid,
1028 imp->imp_replayable, imp->imp_obd->obd_replayable,
1029 ptlrpc_import_state_name(imp->imp_state));
1030 /* Resurrect if we previously died */
1031 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1032 imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1033 ptlrpc_reconnect_import(imp);
1036 /* FIXME move this to mgc_process_config */
1037 if (KEY_IS(KEY_REGISTER_TARGET)) {
1038 struct mgs_target_info *mti;
1039 if (vallen != sizeof(struct mgs_target_info))
1041 mti = (struct mgs_target_info *)val;
1042 CDEBUG(D_MGC, "register_target %s %#x\n",
1043 mti->mti_svname, mti->mti_flags);
1044 rc = mgc_target_register(exp, mti);
1047 if (KEY_IS(KEY_SET_FS)) {
1048 struct super_block *sb = (struct super_block *)val;
1049 struct lustre_sb_info *lsi;
1050 if (vallen != sizeof(struct super_block))
1053 rc = mgc_fs_setup(exp->exp_obd, sb, lsi->lsi_srv_mnt);
1055 CERROR("set_fs got %d\n", rc);
1059 if (KEY_IS(KEY_CLEAR_FS)) {
1062 rc = mgc_fs_cleanup(exp->exp_obd);
1064 CERROR("clear_fs got %d\n", rc);
1068 if (KEY_IS(KEY_SET_INFO)) {
1069 struct mgs_send_param *msp;
1071 msp = (struct mgs_send_param *)val;
1072 rc = mgc_set_mgs_param(exp, msp);
1075 if (KEY_IS(KEY_MGSSEC)) {
1076 struct client_obd *cli = &exp->exp_obd->u.cli;
1077 struct sptlrpc_flavor flvr;
1080 * empty string means using current flavor, if which haven't
1081 * been set yet, set it as null.
1083 * if flavor has been set previously, check the asking flavor
1084 * must match the existing one.
1087 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1093 rc = sptlrpc_parse_flavor(val, &flvr);
1095 CERROR("invalid sptlrpc flavor %s to MGS\n",
1101 * caller already hold a mutex
1103 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1104 cli->cl_flvr_mgc = flvr;
1105 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1106 sizeof(flvr)) != 0) {
1109 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1111 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1112 "currently %s is in use\n",
1122 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1123 __u32 keylen, void *key, __u32 *vallen, void *val,
1124 struct lov_stripe_md *unused)
1128 if (KEY_IS(KEY_CONN_DATA)) {
1129 struct obd_import *imp = class_exp2cliimp(exp);
1130 struct obd_connect_data *data = val;
1132 if (*vallen == sizeof(*data)) {
1133 *data = imp->imp_connect_data;
1141 static int mgc_import_event(struct obd_device *obd,
1142 struct obd_import *imp,
1143 enum obd_import_event event)
1147 LASSERT(imp->imp_obd == obd);
1148 CDEBUG(D_MGC, "import event %#x\n", event);
1151 case IMP_EVENT_DISCON:
1152 /* MGC imports should not wait for recovery */
1154 case IMP_EVENT_INACTIVE:
1156 case IMP_EVENT_INVALIDATE: {
1157 struct ldlm_namespace *ns = obd->obd_namespace;
1158 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1161 case IMP_EVENT_ACTIVE:
1162 LCONSOLE_WARN("%s: Reactivating import\n", obd->obd_name);
1163 /* Clearing obd_no_recov allows us to continue pinging */
1164 obd->obd_no_recov = 0;
1165 mgc_notify_active(obd);
1169 case IMP_EVENT_DEACTIVATE:
1170 case IMP_EVENT_ACTIVATE:
1173 CERROR("Unknown import event %#x\n", event);
1179 static int mgc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
1180 struct obd_device *tgt, int *index)
1182 struct llog_ctxt *ctxt;
1186 LASSERT(olg == &obd->obd_olg);
1188 #ifdef HAVE_LDISKFS_OSD
1189 rc = llog_setup(NULL, obd, olg, LLOG_CONFIG_ORIG_CTXT, tgt,
1195 rc = llog_setup(NULL, obd, olg, LLOG_CONFIG_REPL_CTXT, tgt,
1200 ctxt = llog_group_get_ctxt(olg, LLOG_CONFIG_REPL_CTXT);
1202 GOTO(out, rc = -ENODEV);
1204 llog_initiator_connect(ctxt);
1205 llog_ctxt_put(ctxt);
1209 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1211 llog_cleanup(NULL, ctxt);
1215 static int mgc_llog_finish(struct obd_device *obd, int count)
1217 struct llog_ctxt *ctxt;
1221 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1223 llog_cleanup(NULL, ctxt);
1225 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1227 llog_cleanup(NULL, ctxt);
1232 CONFIG_READ_NRPAGES_INIT = 1 << (20 - CFS_PAGE_SHIFT),
1233 CONFIG_READ_NRPAGES = 4
1236 static int mgc_apply_recover_logs(struct obd_device *mgc,
1237 struct config_llog_data *cld,
1239 void *data, int datalen, bool mne_swab)
1241 struct config_llog_instance *cfg = &cld->cld_cfg;
1242 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1243 struct mgs_nidtbl_entry *entry;
1244 struct lustre_cfg *lcfg;
1245 struct lustre_cfg_bufs bufs;
1246 u64 prev_version = 0;
1255 LASSERT(cfg->cfg_instance != NULL);
1256 LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1258 OBD_ALLOC(inst, CFS_PAGE_SIZE);
1262 if (!IS_SERVER(lsi)) {
1263 pos = sprintf(inst, "%p", cfg->cfg_instance);
1265 LASSERT(IS_MDT(lsi));
1266 rc = server_name2svname(lsi->lsi_svname, inst, NULL);
1274 bufsz = CFS_PAGE_SIZE - pos;
1276 while (datalen > 0) {
1277 int entry_len = sizeof(*entry);
1279 struct obd_device *obd;
1286 if (datalen < sizeof(*entry))
1289 entry = (typeof(entry))(data + off);
1292 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1294 if (entry->mne_nid_count == 0) /* at least one nid entry */
1296 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1299 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1300 if (datalen < entry_len) /* must have entry_len at least */
1303 /* Keep this swab for normal mixed endian handling. LU-1644 */
1305 lustre_swab_mgs_nidtbl_entry(entry);
1306 if (entry->mne_length > CFS_PAGE_SIZE) {
1307 CERROR("MNE too large (%u)\n", entry->mne_length);
1311 if (entry->mne_length < entry_len)
1314 off += entry->mne_length;
1315 datalen -= entry->mne_length;
1319 if (entry->mne_version > max_version) {
1320 CERROR("entry index(%lld) is over max_index(%lld)\n",
1321 entry->mne_version, max_version);
1325 if (prev_version >= entry->mne_version) {
1326 CERROR("index unsorted, prev %lld, now %lld\n",
1327 prev_version, entry->mne_version);
1330 prev_version = entry->mne_version;
1333 * Write a string with format "nid::instance" to
1334 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1337 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1338 memset(buf, 0, bufsz);
1342 /* lustre-OST0001-osc-<instance #> */
1343 strcpy(obdname, cld->cld_logname);
1344 cname = strrchr(obdname, '-');
1345 if (cname == NULL) {
1346 CERROR("mgc %s: invalid logname %s\n",
1347 mgc->obd_name, obdname);
1351 pos = cname - obdname;
1353 pos += sprintf(obdname + pos, "-%s%04x",
1354 is_ost ? "OST" : "MDT", entry->mne_index);
1356 cname = is_ost ? "osc" : "mdc",
1357 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1358 lustre_cfg_bufs_reset(&bufs, obdname);
1360 /* find the obd by obdname */
1361 obd = class_name2obd(obdname);
1363 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1364 mgc->obd_name, obdname);
1366 /* this is a safe race, when the ost is starting up...*/
1370 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1373 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1376 down_read(&obd->u.cli.cl_sem);
1377 if (obd->u.cli.cl_import == NULL) {
1378 /* client does not connect to the OST yet */
1379 up_read(&obd->u.cli.cl_sem);
1384 /* TODO: iterate all nids to find one */
1385 /* find uuid by nid */
1386 rc = client_import_find_conn(obd->u.cli.cl_import,
1388 (struct obd_uuid *)uuid);
1389 up_read(&obd->u.cli.cl_sem);
1391 CERROR("mgc: cannot find uuid by nid %s\n",
1392 libcfs_nid2str(entry->u.nids[0]));
1396 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1397 uuid, libcfs_nid2str(entry->u.nids[0]));
1399 pos += strlen(uuid);
1400 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1401 LASSERT(pos < bufsz);
1403 lustre_cfg_bufs_set_string(&bufs, 1, params);
1406 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1408 CERROR("mgc: cannot allocate memory\n");
1412 CDEBUG(D_INFO, "ir apply logs "LPD64"/"LPD64" for %s -> %s\n",
1413 prev_version, max_version, obdname, params);
1415 rc = class_process_config(lcfg);
1416 lustre_cfg_free(lcfg);
1418 CDEBUG(D_INFO, "process config for %s error %d\n",
1421 /* continue, even one with error */
1424 OBD_FREE(inst, CFS_PAGE_SIZE);
1429 * This function is called if this client was notified for target restarting
1430 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery logs.
1432 static int mgc_process_recover_log(struct obd_device *obd,
1433 struct config_llog_data *cld)
1435 struct ptlrpc_request *req = NULL;
1436 struct config_llog_instance *cfg = &cld->cld_cfg;
1437 struct mgs_config_body *body;
1438 struct mgs_config_res *res;
1439 struct ptlrpc_bulk_desc *desc;
1443 bool mne_swab = false;
1449 /* allocate buffer for bulk transfer.
1450 * if this is the first time for this mgs to read logs,
1451 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1452 * once; otherwise, it only reads increment of logs, this should be
1453 * small and CONFIG_READ_NRPAGES will be used.
1455 nrpages = CONFIG_READ_NRPAGES;
1456 if (cfg->cfg_last_idx == 0) /* the first time */
1457 nrpages = CONFIG_READ_NRPAGES_INIT;
1459 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1461 GOTO(out, rc = -ENOMEM);
1463 for (i = 0; i < nrpages; i++) {
1464 pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1465 if (pages[i] == NULL)
1466 GOTO(out, rc = -ENOMEM);
1470 LASSERT(cld_is_recover(cld));
1471 LASSERT(mutex_is_locked(&cld->cld_lock));
1472 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1473 &RQF_MGS_CONFIG_READ);
1475 GOTO(out, rc = -ENOMEM);
1477 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1482 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1483 LASSERT(body != NULL);
1484 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1485 strncpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name));
1486 body->mcb_offset = cfg->cfg_last_idx + 1;
1487 body->mcb_type = cld->cld_type;
1488 body->mcb_bits = CFS_PAGE_SHIFT;
1489 body->mcb_units = nrpages;
1491 /* allocate bulk transfer descriptor */
1492 desc = ptlrpc_prep_bulk_imp(req, nrpages, BULK_PUT_SINK,
1495 GOTO(out, rc = -ENOMEM);
1497 for (i = 0; i < nrpages; i++)
1498 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, CFS_PAGE_SIZE);
1500 ptlrpc_request_set_replen(req);
1501 rc = ptlrpc_queue_wait(req);
1505 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1506 if (res->mcr_size < res->mcr_offset)
1507 GOTO(out, rc = -EINVAL);
1509 /* always update the index even though it might have errors with
1510 * handling the recover logs */
1511 cfg->cfg_last_idx = res->mcr_offset;
1512 eof = res->mcr_offset == res->mcr_size;
1514 CDEBUG(D_INFO, "Latest version "LPD64", more %d.\n",
1515 res->mcr_offset, eof == false);
1517 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1519 GOTO(out, rc = ealen);
1521 if (ealen > nrpages << CFS_PAGE_SHIFT)
1522 GOTO(out, rc = -EINVAL);
1524 if (ealen == 0) { /* no logs transferred */
1530 mne_swab = !!ptlrpc_rep_need_swab(req);
1531 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 50, 0)
1532 /* This import flag means the server did an extra swab of IR MNE
1533 * records (fixed in LU-1252), reverse it here if needed. LU-1644 */
1534 if (unlikely(req->rq_import->imp_need_mne_swab))
1535 mne_swab = !mne_swab;
1537 #warning "LU-1644: Remove old OBD_CONNECT_MNE_SWAB fixup and imp_need_mne_swab"
1540 for (i = 0; i < nrpages && ealen > 0; i++) {
1544 ptr = cfs_kmap(pages[i]);
1545 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1546 min_t(int, ealen, CFS_PAGE_SIZE),
1548 cfs_kunmap(pages[i]);
1550 CWARN("Process recover log %s error %d\n",
1551 cld->cld_logname, rc2);
1555 ealen -= CFS_PAGE_SIZE;
1560 ptlrpc_req_finished(req);
1562 if (rc == 0 && !eof)
1566 for (i = 0; i < nrpages; i++) {
1567 if (pages[i] == NULL)
1569 cfs_free_page(pages[i]);
1571 OBD_FREE(pages, sizeof(*pages) * nrpages);
1576 #ifdef HAVE_LDISKFS_OSD
1579 * XXX: mgc_copy_llog() does not support osd-based llogs yet
1582 /* identical to mgs_log_is_empty */
1583 static int mgc_llog_is_empty(struct obd_device *obd, struct llog_ctxt *ctxt,
1586 struct lvfs_run_ctxt saved;
1587 struct llog_handle *llh;
1590 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1591 rc = llog_open(NULL, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1593 llog_init_handle(NULL, llh, LLOG_F_IS_PLAIN, NULL);
1594 rc = llog_get_size(llh);
1595 llog_close(NULL, llh);
1596 } else if (rc == -ENOENT) {
1599 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1600 /* header is record 1 */
1604 static int mgc_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1605 struct llog_rec_hdr *rec, void *data)
1607 struct llog_rec_hdr local_rec = *rec;
1608 struct llog_handle *local_llh = (struct llog_handle *)data;
1609 char *cfg_buf = (char*) (rec + 1);
1610 struct lustre_cfg *lcfg;
1614 /* Append all records */
1615 local_rec.lrh_len -= sizeof(*rec) + sizeof(struct llog_rec_tail);
1616 rc = llog_write(env, local_llh, &local_rec, NULL, 0,
1617 (void *)cfg_buf, -1);
1619 lcfg = (struct lustre_cfg *)cfg_buf;
1620 CDEBUG(D_INFO, "idx=%d, rc=%d, len=%d, cmd %x %s %s\n",
1621 rec->lrh_index, rc, rec->lrh_len, lcfg->lcfg_command,
1622 lustre_cfg_string(lcfg, 0), lustre_cfg_string(lcfg, 1));
1627 /* Copy a remote log locally */
1628 static int mgc_copy_llog(struct obd_device *obd, struct llog_ctxt *rctxt,
1629 struct llog_ctxt *lctxt, char *logname)
1631 struct llog_handle *local_llh, *remote_llh;
1632 struct obd_uuid *uuid;
1637 /* Write new log to a temp name, then vfs_rename over logname
1638 upon successful completion. */
1640 OBD_ALLOC(temp_log, strlen(logname) + 1);
1643 sprintf(temp_log, "%sT", logname);
1645 /* Make sure there's no old temp log */
1646 rc = llog_erase(NULL, lctxt, NULL, temp_log);
1647 if (rc < 0 && rc != -ENOENT)
1650 /* open local log */
1651 rc = llog_open_create(NULL, lctxt, &local_llh, NULL, temp_log);
1655 /* set the log header uuid for fun */
1656 OBD_ALLOC_PTR(uuid);
1657 obd_str2uuid(uuid, logname);
1658 rc = llog_init_handle(NULL, local_llh, LLOG_F_IS_PLAIN, uuid);
1661 GOTO(out_closel, rc);
1663 /* open remote log */
1664 rc = llog_open(NULL, rctxt, &remote_llh, NULL, logname,
1669 GOTO(out_closel, rc);
1672 rc = llog_init_handle(NULL, remote_llh, LLOG_F_IS_PLAIN, NULL);
1674 GOTO(out_closer, rc);
1676 /* Copy remote log */
1677 rc = llog_process(NULL, remote_llh, mgc_copy_handler,
1678 (void *)local_llh, NULL);
1681 rc2 = llog_close(NULL, remote_llh);
1685 rc2 = llog_close(NULL, local_llh);
1689 /* We've copied the remote log to the local temp log, now
1690 replace the old local log with the temp log. */
1692 struct client_obd *cli = &obd->u.cli;
1695 LASSERT(cli->cl_mgc_configs_dir);
1696 rc = lustre_rename(cli->cl_mgc_configs_dir, cli->cl_mgc_vfsmnt,
1699 CDEBUG(D_MGC, "Copied remote log %s (%d)\n", logname, rc);
1702 CERROR("Failed to copy remote log %s (%d)\n", logname, rc);
1703 OBD_FREE(temp_log, strlen(logname) + 1);
1708 /* local_only means it cannot get remote llogs */
1709 static int mgc_process_cfg_log(struct obd_device *mgc,
1710 struct config_llog_data *cld,
1713 struct llog_ctxt *ctxt, *lctxt = NULL;
1714 #ifdef HAVE_LDISKFS_OSD
1715 struct client_obd *cli = &mgc->u.cli;
1717 struct lvfs_run_ctxt *saved_ctxt;
1718 struct lustre_sb_info *lsi = NULL;
1719 int rc = 0, must_pop = 0;
1720 bool sptlrpc_started = false;
1725 LASSERT(mutex_is_locked(&cld->cld_lock));
1728 * local copy of sptlrpc log is controlled elsewhere, don't try to
1731 if (cld_is_sptlrpc(cld) && local_only)
1734 if (cld->cld_cfg.cfg_sb)
1735 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1737 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1739 CERROR("missing llog context\n");
1743 OBD_ALLOC_PTR(saved_ctxt);
1744 if (saved_ctxt == NULL)
1747 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1749 #ifdef HAVE_LDISKFS_OSD
1751 * XXX: at the moment mgc_copy_llog() works with lvfs-based llogs
1753 /* Copy the setup log locally if we can. Don't mess around if we're
1754 running an MGS though (logs are already local). */
1755 if (lctxt && lsi && IS_SERVER(lsi) &&
1756 (lsi->lsi_srv_mnt == cli->cl_mgc_vfsmnt) &&
1757 !IS_MGS(lsi) && lsi->lsi_srv_mnt) {
1758 push_ctxt(saved_ctxt, &mgc->obd_lvfs_ctxt, NULL);
1761 /* Only try to copy log if we have the lock. */
1762 rc = mgc_copy_llog(mgc, ctxt, lctxt, cld->cld_logname);
1763 if (local_only || rc) {
1764 if (mgc_llog_is_empty(mgc, lctxt, cld->cld_logname)) {
1765 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1766 "log %s and no local copy."
1767 "\n", cld->cld_logname);
1768 GOTO(out_pop, rc = -ENOTCONN);
1770 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1771 "copy for now, will try to update later.\n",
1774 /* Now, whether we copied or not, start using the local llog.
1775 If we failed to copy, we'll start using whatever the old
1777 llog_ctxt_put(ctxt);
1782 if (local_only) { /* no local log at client side */
1783 GOTO(out_pop, rc = -EIO);
1786 if (cld_is_sptlrpc(cld)) {
1787 sptlrpc_conf_log_update_begin(cld->cld_logname);
1788 sptlrpc_started = true;
1791 /* logname and instance info should be the same, so use our
1792 copy of the instance for the update. The cfg_last_idx will
1794 rc = class_config_parse_llog(NULL, ctxt, cld->cld_logname,
1799 llog_ctxt_put(ctxt);
1801 llog_ctxt_put(lctxt);
1803 pop_ctxt(saved_ctxt, &mgc->obd_lvfs_ctxt, NULL);
1805 OBD_FREE_PTR(saved_ctxt);
1807 * update settings on existing OBDs. doing it inside
1808 * of llog_process_lock so no device is attaching/detaching
1810 * the logname must be <fsname>-sptlrpc
1812 if (sptlrpc_started) {
1813 LASSERT(cld_is_sptlrpc(cld));
1814 sptlrpc_conf_log_update_end(cld->cld_logname);
1815 class_notify_sptlrpc_conf(cld->cld_logname,
1816 strlen(cld->cld_logname) -
1817 strlen("-sptlrpc"));
1823 /** Get a config log from the MGS and process it.
1824 * This func is called for both clients and servers.
1825 * Copy the log locally before parsing it if appropriate (non-MGS server)
1827 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1829 struct lustre_handle lockh = { 0 };
1830 __u64 flags = LDLM_FL_NO_LRU;
1836 /* I don't want multiple processes running process_log at once --
1837 sounds like badness. It actually might be fine, as long as
1838 we're not trying to update from the same log
1839 simultaneously (in which case we should use a per-log sem.) */
1840 mutex_lock(&cld->cld_lock);
1841 if (cld->cld_stopping) {
1842 mutex_unlock(&cld->cld_lock);
1846 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1848 CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
1849 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1851 /* Get the cfg lock on the llog */
1852 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1853 LCK_CR, &flags, NULL, NULL, NULL,
1854 cld, 0, NULL, &lockh);
1856 /* Get the cld, it will be released in mgc_blocking_ast. */
1857 config_log_get(cld);
1858 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1861 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1863 /* mark cld_lostlock so that it will requeue
1864 * after MGC becomes available. */
1865 cld->cld_lostlock = 1;
1866 /* Get extra reference, it will be put in requeue thread */
1867 config_log_get(cld);
1871 if (cld_is_recover(cld)) {
1872 rc = 0; /* this is not a fatal error for recover log */
1874 rc = mgc_process_recover_log(mgc, cld);
1876 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1879 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1880 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1882 mutex_unlock(&cld->cld_lock);
1884 /* Now drop the lock so MGS can revoke it */
1886 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1889 CERROR("Can't drop cfg lock: %d\n", rcl);
1896 /** Called from lustre_process_log.
1897 * LCFG_LOG_START gets the config log from the MGS, processes it to start
1898 * any services, and adds it to the list logs to watch (follow).
1900 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1902 struct lustre_cfg *lcfg = buf;
1903 struct config_llog_instance *cfg = NULL;
1908 switch(lcfg->lcfg_command) {
1909 case LCFG_LOV_ADD_OBD: {
1910 /* Overloading this cfg command: register a new target */
1911 struct mgs_target_info *mti;
1913 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1914 sizeof(struct mgs_target_info))
1915 GOTO(out, rc = -EINVAL);
1917 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1918 CDEBUG(D_MGC, "add_target %s %#x\n",
1919 mti->mti_svname, mti->mti_flags);
1920 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1923 case LCFG_LOV_DEL_OBD:
1924 /* Unregister has no meaning at the moment. */
1925 CERROR("lov_del_obd unimplemented\n");
1928 case LCFG_SPTLRPC_CONF: {
1929 rc = sptlrpc_process_config(lcfg);
1932 case LCFG_LOG_START: {
1933 struct config_llog_data *cld;
1934 struct super_block *sb;
1936 logname = lustre_cfg_string(lcfg, 1);
1937 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1938 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1940 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1943 /* We're only called through here on the initial mount */
1944 rc = config_log_add(obd, logname, cfg, sb);
1947 cld = config_log_find(logname, cfg);
1954 /* FIXME only set this for old logs! Right now this forces
1955 us to always skip the "inside markers" check */
1956 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1958 rc = mgc_process_log(obd, cld);
1959 if (rc == 0 && cld->cld_recover != NULL) {
1960 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
1961 imp_connect_data, IMP_RECOV)) {
1962 rc = mgc_process_log(obd, cld->cld_recover);
1964 struct config_llog_data *cir = cld->cld_recover;
1965 cld->cld_recover = NULL;
1966 config_log_put(cir);
1969 CERROR("Cannot process recover llog %d\n", rc);
1971 config_log_put(cld);
1975 case LCFG_LOG_END: {
1976 logname = lustre_cfg_string(lcfg, 1);
1978 if (lcfg->lcfg_bufcount >= 2)
1979 cfg = (struct config_llog_instance *)lustre_cfg_buf(
1981 rc = config_log_end(logname, cfg);
1985 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1986 GOTO(out, rc = -EINVAL);
1994 struct obd_ops mgc_obd_ops = {
1995 .o_owner = THIS_MODULE,
1996 .o_setup = mgc_setup,
1997 .o_precleanup = mgc_precleanup,
1998 .o_cleanup = mgc_cleanup,
1999 .o_add_conn = client_import_add_conn,
2000 .o_del_conn = client_import_del_conn,
2001 .o_connect = client_connect_import,
2002 .o_disconnect = client_disconnect_export,
2003 //.o_enqueue = mgc_enqueue,
2004 .o_cancel = mgc_cancel,
2005 //.o_iocontrol = mgc_iocontrol,
2006 .o_set_info_async = mgc_set_info_async,
2007 .o_get_info = mgc_get_info,
2008 .o_import_event = mgc_import_event,
2009 .o_llog_init = mgc_llog_init,
2010 .o_llog_finish = mgc_llog_finish,
2011 .o_process_config = mgc_process_config,
2014 int __init mgc_init(void)
2016 return class_register_type(&mgc_obd_ops, NULL, NULL,
2017 LUSTRE_MGC_NAME, NULL);
2021 static void /*__exit*/ mgc_exit(void)
2023 class_unregister_type(LUSTRE_MGC_NAME);
2026 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2027 MODULE_DESCRIPTION("Lustre Management Client");
2028 MODULE_LICENSE("GPL");
2030 module_init(mgc_init);
2031 module_exit(mgc_exit);