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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mgc/mgc_request.c
34 * Author: Nathan Rutman <nathan@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_MGC
38 #define D_MGC D_CONFIG /*|D_WARNING*/
40 #include <linux/module.h>
41 #include <linux/kthread.h>
43 #include <dt_object.h>
44 #include <lprocfs_status.h>
45 #include <lustre_dlm.h>
46 #include <lustre_disk.h>
47 #include <lustre_log.h>
48 #include <lustre_nodemap.h>
49 #include <lustre_swab.h>
50 #include <obd_class.h>
51 #include <lustre_barrier.h>
53 #include "mgc_internal.h"
55 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id,
60 if (len > sizeof(resname)) {
61 CERROR("name too long: %s\n", name);
65 CERROR("missing name: %s\n", name);
68 memcpy(&resname, name, len);
70 /* Always use the same endianness for the resid */
71 memset(res_id, 0, sizeof(*res_id));
72 res_id->name[0] = cpu_to_le64(resname);
73 /* XXX: unfortunately, sptlprc and config llog share one lock */
76 case CONFIG_T_SPTLRPC:
79 case CONFIG_T_RECOVER:
81 case CONFIG_T_NODEMAP:
82 case CONFIG_T_BARRIER:
88 res_id->name[1] = cpu_to_le64(resname);
89 CDEBUG(D_MGC, "log %s to resid %#llx/%#llx (%.8s)\n", name,
90 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
94 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id, int type)
96 /* fsname is at most 8 chars long, maybe contain "-".
97 * e.g. "lustre", "SUN-000" */
98 return mgc_name2resid(fsname, strlen(fsname), res_id, type);
100 EXPORT_SYMBOL(mgc_fsname2resid);
102 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id, int type)
107 /* logname consists of "fsname-nodetype".
108 * e.g. "lustre-MDT0001", "SUN-000-client"
109 * there is an exception: llog "params" */
110 name_end = strrchr(logname, '-');
112 len = strlen(logname);
114 len = name_end - logname;
115 return mgc_name2resid(logname, len, res_id, type);
117 EXPORT_SYMBOL(mgc_logname2resid);
119 /********************** config llog list **********************/
120 static struct list_head config_llog_list = LIST_HEAD_INIT(config_llog_list);
121 static DEFINE_SPINLOCK(config_list_lock); /* protects config_llog_list */
123 /* Take a reference to a config log */
124 static int config_log_get(struct config_llog_data *cld)
127 atomic_inc(&cld->cld_refcount);
128 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
129 atomic_read(&cld->cld_refcount));
133 /* Drop a reference to a config log. When no longer referenced,
134 we can free the config log data */
135 static void config_log_put(struct config_llog_data *cld)
142 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
143 atomic_read(&cld->cld_refcount));
144 LASSERT(atomic_read(&cld->cld_refcount) > 0);
146 /* spinlock to make sure no item with 0 refcount in the list */
147 if (atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
148 list_del(&cld->cld_list_chain);
149 spin_unlock(&config_list_lock);
151 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
153 config_log_put(cld->cld_barrier);
154 config_log_put(cld->cld_recover);
155 config_log_put(cld->cld_params);
156 config_log_put(cld->cld_nodemap);
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;
175 unsigned long cfg_instance;
178 LASSERT(logname != NULL);
180 cfg_instance = cfg ? cfg->cfg_instance : 0;
181 spin_lock(&config_list_lock);
182 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
183 /* check if cfg_instance is the one we want */
184 if (cfg_instance != cld->cld_cfg.cfg_instance)
187 /* instance may be NULL, should check name */
188 if (strcmp(logname, cld->cld_logname) == 0) {
190 config_log_get(found);
194 spin_unlock(&config_list_lock);
199 struct config_llog_data *do_config_log_add(struct obd_device *obd,
202 struct config_llog_instance *cfg,
203 struct super_block *sb)
205 struct config_llog_data *cld;
210 CDEBUG(D_MGC, "do adding config log %s-%016lx\n", logname,
211 cfg ? cfg->cfg_instance : 0);
213 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
215 RETURN(ERR_PTR(-ENOMEM));
217 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
219 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
223 strcpy(cld->cld_logname, logname);
227 cld->cld_cfg.cfg_callback = class_config_llog_handler;
228 mutex_init(&cld->cld_lock);
229 cld->cld_cfg.cfg_last_idx = 0;
230 cld->cld_cfg.cfg_flags = 0;
231 cld->cld_cfg.cfg_sb = sb;
232 cld->cld_type = type;
233 atomic_set(&cld->cld_refcount, 1);
235 /* Keep the mgc around until we are done */
236 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
238 if (cld_is_sptlrpc(cld))
239 sptlrpc_conf_log_start(logname);
241 spin_lock(&config_list_lock);
242 list_add(&cld->cld_list_chain, &config_llog_list);
243 spin_unlock(&config_list_lock);
245 if (cld_is_sptlrpc(cld) || cld_is_nodemap(cld) || cld_is_barrier(cld)) {
246 rc = mgc_process_log(obd, cld);
247 if (rc && rc != -ENOENT)
248 CERROR("%s: failed processing log, type %d: rc = %d\n",
249 obd->obd_name, type, rc);
255 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
257 struct config_llog_instance *cfg,
258 struct super_block *sb)
260 struct config_llog_instance lcfg = *cfg;
261 struct lustre_sb_info *lsi = s2lsi(sb);
262 struct config_llog_data *cld;
268 /* for osp-on-ost, see lustre_start_osp() */
269 if (IS_MDT(lsi) && lcfg.cfg_instance)
272 /* We have to use different llog for clients and MDTs for DNE,
273 * where only clients are notified if one of DNE server restarts.
275 LASSERT(strlen(fsname) < sizeof(logname) / 2);
276 strncpy(logname, fsname, sizeof(logname));
277 if (IS_SERVER(lsi)) { /* mdt */
278 LASSERT(lcfg.cfg_instance == 0);
279 lcfg.cfg_instance = ll_get_cfg_instance(sb);
280 strncat(logname, "-mdtir", sizeof(logname));
282 LASSERT(lcfg.cfg_instance != 0);
283 strncat(logname, "-cliir", sizeof(logname));
286 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
290 static struct config_llog_data *config_log_find_or_add(struct obd_device *obd,
291 char *logname, struct super_block *sb, int type,
292 struct config_llog_instance *cfg)
294 struct config_llog_instance lcfg = *cfg;
295 struct config_llog_data *cld;
297 /* Note class_config_llog_handler() depends on getting "obd" back */
298 lcfg.cfg_instance = sb ? ll_get_cfg_instance(sb) : (unsigned long)obd;
300 cld = config_log_find(logname, &lcfg);
301 if (unlikely(cld != NULL))
304 return do_config_log_add(obd, logname, type, &lcfg, sb);
307 /** Add this log to the list of active logs watched by an MGC.
308 * Active means we're watching for updates.
309 * We have one active log per "mount" - client instance or servername.
310 * Each instance may be at a different point in the log.
312 static struct config_llog_data *
313 config_log_add(struct obd_device *obd, char *logname,
314 struct config_llog_instance *cfg, struct super_block *sb)
316 struct lustre_sb_info *lsi = s2lsi(sb);
317 struct config_llog_data *cld = NULL;
318 struct config_llog_data *sptlrpc_cld = NULL;
319 struct config_llog_data *params_cld = NULL;
320 struct config_llog_data *nodemap_cld = NULL;
321 struct config_llog_data *barrier_cld = NULL;
328 CDEBUG(D_MGC, "add config log %s-%016lx\n", logname,
332 * for each regular log, the depended sptlrpc log name is
333 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
335 ptr = strrchr(logname, '-');
336 if (ptr == NULL || ptr - logname > 8) {
337 CERROR("logname %s is too long\n", logname);
338 RETURN(ERR_PTR(-EINVAL));
341 memcpy(seclogname, logname, ptr - logname);
342 strcpy(seclogname + (ptr - logname), "-sptlrpc");
344 if (cfg->cfg_sub_clds & CONFIG_SUB_SPTLRPC) {
345 sptlrpc_cld = config_log_find_or_add(obd, seclogname, NULL,
346 CONFIG_T_SPTLRPC, cfg);
347 if (IS_ERR(sptlrpc_cld)) {
348 CERROR("%s: can't create sptlrpc log %s: rc = %ld\n",
349 obd->obd_name, seclogname, PTR_ERR(sptlrpc_cld));
354 if (!IS_MGS(lsi) && cfg->cfg_sub_clds & CONFIG_SUB_NODEMAP) {
355 nodemap_cld = config_log_find_or_add(obd, LUSTRE_NODEMAP_NAME,
356 NULL, CONFIG_T_NODEMAP,
358 if (IS_ERR(nodemap_cld)) {
359 rc = PTR_ERR(nodemap_cld);
360 CERROR("%s: cannot create nodemap log: rc = %d\n",
362 GOTO(out_sptlrpc, rc);
366 if (cfg->cfg_sub_clds & CONFIG_SUB_PARAMS) {
367 params_cld = config_log_find_or_add(obd, PARAMS_FILENAME, sb,
368 CONFIG_T_PARAMS, cfg);
369 if (IS_ERR(params_cld)) {
370 rc = PTR_ERR(params_cld);
371 CERROR("%s: can't create params log: rc = %d\n",
373 GOTO(out_nodemap, rc);
377 if (IS_MDT(s2lsi(sb)) && cfg->cfg_sub_clds & CONFIG_SUB_BARRIER) {
378 snprintf(seclogname + (ptr - logname), sizeof(seclogname) - 1,
379 "-%s", BARRIER_FILENAME);
380 barrier_cld = config_log_find_or_add(obd, seclogname, sb,
381 CONFIG_T_BARRIER, cfg);
382 if (IS_ERR(barrier_cld)) {
383 rc = PTR_ERR(barrier_cld);
384 CERROR("%s: can't create barrier log: rc = %d\n",
386 GOTO(out_params, rc);
390 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
393 CERROR("%s: can't create log: rc = %d\n",
395 GOTO(out_barrier, rc = PTR_ERR(cld));
398 LASSERT(lsi->lsi_lmd);
399 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR) &&
400 cfg->cfg_sub_clds & CONFIG_SUB_RECOVER) {
401 struct config_llog_data *recover_cld;
403 ptr = strrchr(seclogname, '-');
407 CERROR("%s: sptlrpc log name not correct, %s: "
408 "rc = %d\n", obd->obd_name, seclogname, -EINVAL);
409 GOTO(out_cld, rc = -EINVAL);
412 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
413 if (IS_ERR(recover_cld)) {
414 rc = PTR_ERR(recover_cld);
415 CERROR("%s: can't create recover log: rc = %d\n",
420 mutex_lock(&cld->cld_lock);
422 cld->cld_recover = recover_cld;
426 mutex_lock(&cld->cld_lock);
427 cld->cld_params = params_cld;
428 cld->cld_barrier = barrier_cld;
429 cld->cld_nodemap = nodemap_cld;
430 cld->cld_sptlrpc = sptlrpc_cld;
431 mutex_unlock(&cld->cld_lock);
438 config_log_put(barrier_cld);
440 config_log_put(params_cld);
442 config_log_put(nodemap_cld);
444 config_log_put(sptlrpc_cld);
449 DEFINE_MUTEX(llog_process_lock);
451 static inline void config_mark_cld_stop(struct config_llog_data *cld)
454 mutex_lock(&cld->cld_lock);
455 spin_lock(&config_list_lock);
456 cld->cld_stopping = 1;
457 spin_unlock(&config_list_lock);
458 mutex_unlock(&cld->cld_lock);
462 /** Stop watching for updates on this log.
464 static int config_log_end(char *logname, struct config_llog_instance *cfg)
466 struct config_llog_data *cld;
467 struct config_llog_data *cld_sptlrpc = NULL;
468 struct config_llog_data *cld_params = NULL;
469 struct config_llog_data *cld_recover = NULL;
470 struct config_llog_data *cld_nodemap = NULL;
471 struct config_llog_data *cld_barrier = NULL;
476 cld = config_log_find(logname, cfg);
480 mutex_lock(&cld->cld_lock);
482 * if cld_stopping is set, it means we didn't start the log thus
483 * not owning the start ref. this can happen after previous umount:
484 * the cld still hanging there waiting for lock cancel, and we
485 * remount again but failed in the middle and call log_end without
488 if (unlikely(cld->cld_stopping)) {
489 mutex_unlock(&cld->cld_lock);
490 /* drop the ref from the find */
495 spin_lock(&config_list_lock);
496 cld->cld_stopping = 1;
497 spin_unlock(&config_list_lock);
499 cld_recover = cld->cld_recover;
500 cld->cld_recover = NULL;
501 cld_params = cld->cld_params;
502 cld->cld_params = NULL;
503 cld_nodemap = cld->cld_nodemap;
504 cld->cld_nodemap = NULL;
505 cld_barrier = cld->cld_barrier;
506 cld->cld_barrier = NULL;
507 cld_sptlrpc = cld->cld_sptlrpc;
508 cld->cld_sptlrpc = NULL;
509 mutex_unlock(&cld->cld_lock);
511 config_mark_cld_stop(cld_recover);
512 config_log_put(cld_recover);
514 config_mark_cld_stop(cld_params);
515 config_log_put(cld_params);
517 /* don't set cld_stopping on nm lock as other targets may be active */
518 config_log_put(cld_nodemap);
521 mutex_lock(&cld_barrier->cld_lock);
522 cld_barrier->cld_stopping = 1;
523 mutex_unlock(&cld_barrier->cld_lock);
524 config_log_put(cld_barrier);
527 config_log_put(cld_sptlrpc);
529 /* drop the ref from the find */
531 /* drop the start ref */
534 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
539 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
541 struct obd_device *obd = data;
542 struct obd_import *imp;
543 struct obd_connect_data *ocd;
544 struct config_llog_data *cld;
548 LPROCFS_CLIMP_CHECK(obd);
549 imp = obd->u.cli.cl_import;
550 ocd = &imp->imp_connect_data;
552 seq_printf(m, "imperative_recovery: %s\n",
553 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
554 seq_printf(m, "client_state:\n");
556 spin_lock(&config_list_lock);
557 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
558 if (cld->cld_recover == NULL)
560 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
562 cld->cld_recover->cld_cfg.cfg_last_idx);
564 spin_unlock(&config_list_lock);
566 LPROCFS_CLIMP_EXIT(obd);
570 /* reenqueue any lost locks */
571 #define RQ_RUNNING 0x1
575 #define RQ_PRECLEANUP 0x10
576 static int rq_state = 0;
577 static wait_queue_head_t rq_waitq;
578 static DECLARE_COMPLETION(rq_exit);
579 static DECLARE_COMPLETION(rq_start);
581 static void do_requeue(struct config_llog_data *cld)
586 LASSERT(atomic_read(&cld->cld_refcount) > 0);
589 * Do not run mgc_process_log on a disconnected export or an
590 * export which is being disconnected. Take the client
591 * semaphore to make the check non-racy.
593 down_read_nested(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem,
595 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
596 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
597 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
598 if (rc && rc != -ENOENT)
599 CERROR("failed processing log: %d\n", rc);
601 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
604 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
609 /* this timeout represents how many seconds MGC should wait before
610 * requeue config and recover lock to the MGS. We need to randomize this
611 * in order to not flood the MGS.
613 #define MGC_TIMEOUT_MIN_SECONDS 5
614 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
616 static int mgc_requeue_thread(void *data)
622 CDEBUG(D_MGC, "Starting requeue thread\n");
624 /* Keep trying failed locks periodically */
625 spin_lock(&config_list_lock);
626 rq_state |= RQ_RUNNING;
627 while (!(rq_state & RQ_STOP)) {
628 struct l_wait_info lwi;
629 struct config_llog_data *cld, *cld_prev;
630 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
633 /* Any new or requeued lostlocks will change the state */
634 rq_state &= ~(RQ_NOW | RQ_LATER);
635 spin_unlock(&config_list_lock);
642 /* Always wait a few seconds to allow the server who
643 caused the lock revocation to finish its setup, plus some
644 random so everyone doesn't try to reconnect at once. */
645 to = msecs_to_jiffies(MGC_TIMEOUT_MIN_SECONDS * MSEC_PER_SEC);
646 /* rand is centi-seconds */
647 to += msecs_to_jiffies(rand * MSEC_PER_SEC / 100);
648 lwi = LWI_TIMEOUT(to, NULL, NULL);
649 l_wait_event(rq_waitq, rq_state & (RQ_STOP | RQ_PRECLEANUP),
653 * iterate & processing through the list. for each cld, process
654 * its depending sptlrpc cld firstly (if any) and then itself.
656 * it's guaranteed any item in the list must have
657 * reference > 0; and if cld_lostlock is set, at
658 * least one reference is taken by the previous enqueue.
662 spin_lock(&config_list_lock);
663 rq_state &= ~RQ_PRECLEANUP;
664 list_for_each_entry(cld, &config_llog_list,
666 if (!cld->cld_lostlock || cld->cld_stopping)
669 /* hold reference to avoid being freed during
670 * subsequent processing. */
672 cld->cld_lostlock = 0;
673 spin_unlock(&config_list_lock);
675 config_log_put(cld_prev);
678 if (likely(!(rq_state & RQ_STOP))) {
680 spin_lock(&config_list_lock);
682 spin_lock(&config_list_lock);
686 spin_unlock(&config_list_lock);
687 config_log_put(cld_prev);
689 /* Wait a bit to see if anyone else needs a requeue */
690 lwi = (struct l_wait_info) { 0 };
691 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
693 spin_lock(&config_list_lock);
696 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
697 rq_state &= ~RQ_RUNNING;
698 spin_unlock(&config_list_lock);
702 CDEBUG(D_MGC, "Ending requeue thread\n");
706 /* Add a cld to the list to requeue. Start the requeue thread if needed.
707 We are responsible for dropping the config log reference from here on out. */
708 static void mgc_requeue_add(struct config_llog_data *cld)
713 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
714 cld->cld_logname, atomic_read(&cld->cld_refcount),
715 cld->cld_stopping, rq_state);
716 LASSERT(atomic_read(&cld->cld_refcount) > 0);
718 mutex_lock(&cld->cld_lock);
719 spin_lock(&config_list_lock);
720 if (!(rq_state & RQ_STOP) && !cld->cld_stopping && !cld->cld_lostlock) {
721 cld->cld_lostlock = 1;
725 spin_unlock(&config_list_lock);
726 mutex_unlock(&cld->cld_lock);
733 /********************** class fns **********************/
734 static int mgc_local_llog_init(const struct lu_env *env,
735 struct obd_device *obd,
736 struct obd_device *disk)
738 struct llog_ctxt *ctxt;
743 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
748 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
750 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
756 static int mgc_local_llog_fini(const struct lu_env *env,
757 struct obd_device *obd)
759 struct llog_ctxt *ctxt;
763 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
764 llog_cleanup(env, ctxt);
769 static int mgc_fs_setup(const struct lu_env *env, struct obd_device *obd,
770 struct super_block *sb)
772 struct lustre_sb_info *lsi = s2lsi(sb);
773 struct client_obd *cli = &obd->u.cli;
774 struct lu_fid rfid, fid;
775 struct dt_object *root, *dto;
781 LASSERT(lsi->lsi_dt_dev);
783 /* The mgc fs exclusion mutex. Only one fs can be setup at a time. */
784 mutex_lock(&cli->cl_mgc_mutex);
786 /* Setup the configs dir */
787 fid.f_seq = FID_SEQ_LOCAL_NAME;
790 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
795 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
799 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
800 &cli->cl_mgc_los->los_dev->dd_lu_dev, NULL);
801 if (unlikely(IS_ERR(root)))
802 GOTO(out_los, rc = PTR_ERR(root));
804 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
806 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
807 dt_object_put_nocache(env, root);
809 GOTO(out_los, rc = PTR_ERR(dto));
811 cli->cl_mgc_configs_dir = dto;
813 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
814 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
818 /* We take an obd ref to insure that we can't get to mgc_cleanup
819 * without calling mgc_fs_cleanup first. */
820 class_incref(obd, "mgc_fs", obd);
822 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
826 dt_object_put(env, cli->cl_mgc_configs_dir);
827 cli->cl_mgc_configs_dir = NULL;
831 local_oid_storage_fini(env, cli->cl_mgc_los);
832 cli->cl_mgc_los = NULL;
833 mutex_unlock(&cli->cl_mgc_mutex);
838 static int mgc_fs_cleanup(const struct lu_env *env, struct obd_device *obd)
840 struct client_obd *cli = &obd->u.cli;
843 LASSERT(cli->cl_mgc_los != NULL);
845 mgc_local_llog_fini(env, obd);
847 dt_object_put_nocache(env, cli->cl_mgc_configs_dir);
848 cli->cl_mgc_configs_dir = NULL;
850 local_oid_storage_fini(env, cli->cl_mgc_los);
851 cli->cl_mgc_los = NULL;
853 class_decref(obd, "mgc_fs", obd);
854 mutex_unlock(&cli->cl_mgc_mutex);
859 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
861 struct llog_ctxt *ctxt;
866 /* setup only remote ctxt, the local disk context is switched per each
867 * filesystem during mgc_fs_setup() */
868 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
873 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
876 llog_initiator_connect(ctxt);
882 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
884 struct llog_ctxt *ctxt;
888 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
890 llog_cleanup(env, ctxt);
896 static atomic_t mgc_count = ATOMIC_INIT(0);
897 static int mgc_precleanup(struct obd_device *obd)
903 if (atomic_dec_and_test(&mgc_count)) {
904 LASSERT(rq_state & RQ_RUNNING);
905 /* stop requeue thread */
908 /* wakeup requeue thread to clean our cld */
909 temp = RQ_NOW | RQ_PRECLEANUP;
912 spin_lock(&config_list_lock);
914 spin_unlock(&config_list_lock);
918 wait_for_completion(&rq_exit);
919 obd_cleanup_client_import(obd);
921 rc = mgc_llog_fini(NULL, obd);
923 CERROR("failed to cleanup llogging subsystems\n");
928 static int mgc_cleanup(struct obd_device *obd)
933 /* COMPAT_146 - old config logs may have added profiles we don't
935 if (obd->obd_type->typ_refcnt <= 1)
936 /* Only for the last mgc */
937 class_del_profiles();
939 lprocfs_obd_cleanup(obd);
942 rc = client_obd_cleanup(obd);
946 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
948 struct task_struct *task;
952 rc = ptlrpcd_addref();
956 rc = client_obd_setup(obd, lcfg);
958 GOTO(err_decref, rc);
960 rc = mgc_llog_init(NULL, obd);
962 CERROR("failed to setup llogging subsystems\n");
963 GOTO(err_cleanup, rc);
966 rc = mgc_tunables_init(obd);
970 if (atomic_inc_return(&mgc_count) == 1) {
972 init_waitqueue_head(&rq_waitq);
974 /* start requeue thread */
975 task = kthread_run(mgc_requeue_thread, NULL, "ll_cfg_requeue");
978 CERROR("%s: cannot start requeue thread: rc = %d; "
979 "no more log updates\n",
983 /* rc is the task_struct pointer of mgc_requeue_thread. */
985 wait_for_completion(&rq_start);
991 lprocfs_obd_cleanup(obd);
993 client_obd_cleanup(obd);
999 /* based on ll_mdc_blocking_ast */
1000 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
1001 void *data, int flag)
1003 struct lustre_handle lockh;
1004 struct config_llog_data *cld = (struct config_llog_data *)data;
1009 case LDLM_CB_BLOCKING:
1010 /* mgs wants the lock, give it up... */
1011 LDLM_DEBUG(lock, "MGC blocking CB");
1012 ldlm_lock2handle(lock, &lockh);
1013 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
1015 case LDLM_CB_CANCELING:
1016 /* We've given up the lock, prepare ourselves to update. */
1017 LDLM_DEBUG(lock, "MGC cancel CB");
1019 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
1020 PLDLMRES(lock->l_resource),
1021 (char *)&lock->l_resource->lr_name.name[0]);
1024 CDEBUG(D_INFO, "missing data, won't requeue\n");
1028 /* held at mgc_process_log(). */
1029 LASSERT(atomic_read(&cld->cld_refcount) > 0);
1031 lock->l_ast_data = NULL;
1032 /* Are we done with this log? */
1033 if (cld->cld_stopping) {
1034 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
1036 config_log_put(cld);
1039 /* Make sure not to re-enqueue when the mgc is stopping
1040 (we get called from client_disconnect_export) */
1041 if (lock->l_conn_export == NULL ||
1042 lock->l_conn_export->exp_obd->u.cli.cl_conn_count == 0) {
1043 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
1045 config_log_put(cld);
1049 /* Re-enqueue now */
1050 mgc_requeue_add(cld);
1051 config_log_put(cld);
1060 /* Not sure where this should go... */
1061 /* This is the timeout value for MGS_CONNECT request plus a ping interval, such
1062 * that we can have a chance to try the secondary MGS if any. */
1063 #define MGC_ENQUEUE_LIMIT (INITIAL_CONNECT_TIMEOUT + (AT_OFF ? 0 : at_min) \
1065 #define MGC_TARGET_REG_LIMIT 10
1066 #define MGC_TARGET_REG_LIMIT_MAX RECONNECT_DELAY_MAX
1067 #define MGC_SEND_PARAM_LIMIT 10
1069 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
1070 /* Send parameter to MGS*/
1071 static int mgc_set_mgs_param(struct obd_export *exp,
1072 struct mgs_send_param *msp)
1074 struct ptlrpc_request *req;
1075 struct mgs_send_param *req_msp, *rep_msp;
1079 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1080 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
1085 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1087 ptlrpc_req_finished(req);
1091 memcpy(req_msp, msp, sizeof(*req_msp));
1092 ptlrpc_request_set_replen(req);
1094 /* Limit how long we will wait for the enqueue to complete */
1095 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
1096 rc = ptlrpc_queue_wait(req);
1098 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1099 memcpy(msp, rep_msp, sizeof(*rep_msp));
1102 ptlrpc_req_finished(req);
1108 /* Take a config lock so we can get cancel notifications */
1109 static int mgc_enqueue(struct obd_export *exp, enum ldlm_type type,
1110 union ldlm_policy_data *policy, enum ldlm_mode mode,
1111 __u64 *flags, ldlm_glimpse_callback glimpse_callback,
1112 void *data, __u32 lvb_len, void *lvb_swabber,
1113 struct lustre_handle *lockh)
1115 struct config_llog_data *cld = (struct config_llog_data *)data;
1116 struct ldlm_enqueue_info einfo = {
1119 .ei_cb_bl = mgc_blocking_ast,
1120 .ei_cb_cp = ldlm_completion_ast,
1121 .ei_cb_gl = glimpse_callback,
1123 struct ptlrpc_request *req;
1124 int short_limit = cld_is_sptlrpc(cld);
1128 CDEBUG(D_MGC, "Enqueue for %s (res %#llx)\n", cld->cld_logname,
1129 cld->cld_resid.name[0]);
1131 /* We need a callback for every lockholder, so don't try to
1132 ldlm_lock_match (see rev 1.1.2.11.2.47) */
1133 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1134 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1139 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1140 ptlrpc_request_set_replen(req);
1142 /* check if this is server or client */
1143 if (cld->cld_cfg.cfg_sb) {
1144 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
1145 if (lsi && IS_SERVER(lsi))
1148 /* Limit how long we will wait for the enqueue to complete */
1149 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
1150 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1151 NULL, 0, LVB_T_NONE, lockh, 0);
1152 /* A failed enqueue should still call the mgc_blocking_ast,
1153 where it will be requeued if needed ("grant failed"). */
1154 ptlrpc_req_finished(req);
1158 static int mgc_cancel(struct obd_export *exp, enum ldlm_mode mode,
1159 struct lustre_handle *lockh)
1163 ldlm_lock_decref(lockh, mode);
1168 static void mgc_notify_active(struct obd_device *unused)
1170 /* wakeup mgc_requeue_thread to requeue mgc lock */
1171 spin_lock(&config_list_lock);
1173 spin_unlock(&config_list_lock);
1176 /* TODO: Help the MGS rebuild nidtbl. -jay */
1179 /* Send target_reg message to MGS */
1180 static int mgc_target_register(struct obd_export *exp,
1181 struct mgs_target_info *mti)
1183 struct ptlrpc_request *req;
1184 struct mgs_target_info *req_mti, *rep_mti;
1188 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1189 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1194 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1196 ptlrpc_req_finished(req);
1200 memcpy(req_mti, mti, sizeof(*req_mti));
1201 ptlrpc_request_set_replen(req);
1202 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1203 /* Limit how long we will wait for the enqueue to complete */
1204 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1206 /* if the target needs to regenerate the config log in MGS, it's better
1207 * to use some longer limit to let MGC have time to change connection to
1208 * another MGS (or try again with the same MGS) for the target (server)
1209 * will fail and exit if the request expired due to delay limit. */
1210 if (mti->mti_flags & (LDD_F_UPDATE | LDD_F_NEED_INDEX))
1211 req->rq_delay_limit = MGC_TARGET_REG_LIMIT_MAX;
1213 rc = ptlrpc_queue_wait(req);
1215 rep_mti = req_capsule_server_get(&req->rq_pill,
1216 &RMF_MGS_TARGET_INFO);
1217 memcpy(mti, rep_mti, sizeof(*rep_mti));
1218 CDEBUG(D_MGC, "register %s got index = %d\n",
1219 mti->mti_svname, mti->mti_stripe_index);
1221 ptlrpc_req_finished(req);
1226 static int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1227 u32 keylen, void *key,
1228 u32 vallen, void *val,
1229 struct ptlrpc_request_set *set)
1234 /* Turn off initial_recov after we try all backup servers once */
1235 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1236 struct obd_import *imp = class_exp2cliimp(exp);
1238 if (vallen != sizeof(int))
1240 value = *(int *)val;
1241 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1242 imp->imp_obd->obd_name, value,
1243 imp->imp_deactive, imp->imp_invalid,
1244 imp->imp_replayable, imp->imp_obd->obd_replayable,
1245 ptlrpc_import_state_name(imp->imp_state));
1246 /* Resurrect the import immediately if
1247 * 1. we previously got disconnected,
1248 * 2. value > 1 (at the same node with MGS)
1250 if (imp->imp_state == LUSTRE_IMP_DISCON || value > 1)
1251 ptlrpc_reconnect_import(imp);
1256 /* FIXME move this to mgc_process_config */
1257 if (KEY_IS(KEY_REGISTER_TARGET)) {
1258 struct mgs_target_info *mti;
1259 if (vallen != sizeof(struct mgs_target_info))
1261 mti = (struct mgs_target_info *)val;
1262 CDEBUG(D_MGC, "register_target %s %#x\n",
1263 mti->mti_svname, mti->mti_flags);
1264 rc = mgc_target_register(exp, mti);
1267 if (KEY_IS(KEY_SET_FS)) {
1268 struct super_block *sb = (struct super_block *)val;
1270 if (vallen != sizeof(struct super_block))
1273 rc = mgc_fs_setup(env, exp->exp_obd, sb);
1276 if (KEY_IS(KEY_CLEAR_FS)) {
1279 rc = mgc_fs_cleanup(env, exp->exp_obd);
1282 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
1283 if (KEY_IS(KEY_SET_INFO)) {
1284 struct mgs_send_param *msp;
1286 msp = (struct mgs_send_param *)val;
1287 rc = mgc_set_mgs_param(exp, msp);
1291 if (KEY_IS(KEY_MGSSEC)) {
1292 struct client_obd *cli = &exp->exp_obd->u.cli;
1293 struct sptlrpc_flavor flvr;
1296 * empty string means using current flavor, if which haven't
1297 * been set yet, set it as null.
1299 * if flavor has been set previously, check the asking flavor
1300 * must match the existing one.
1303 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1309 rc = sptlrpc_parse_flavor(val, &flvr);
1311 CERROR("invalid sptlrpc flavor %s to MGS\n",
1317 * caller already hold a mutex
1319 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1320 cli->cl_flvr_mgc = flvr;
1321 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1322 sizeof(flvr)) != 0) {
1325 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1327 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1328 "currently %s is in use\n",
1338 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1339 __u32 keylen, void *key, __u32 *vallen, void *val)
1343 if (KEY_IS(KEY_CONN_DATA)) {
1344 struct obd_import *imp = class_exp2cliimp(exp);
1345 struct obd_connect_data *data = val;
1347 if (*vallen == sizeof(*data)) {
1348 *data = imp->imp_connect_data;
1356 static int mgc_import_event(struct obd_device *obd,
1357 struct obd_import *imp,
1358 enum obd_import_event event)
1362 LASSERT(imp->imp_obd == obd);
1363 CDEBUG(D_MGC, "import event %#x\n", event);
1366 case IMP_EVENT_DISCON:
1367 /* MGC imports should not wait for recovery */
1368 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1369 ptlrpc_pinger_ir_down();
1371 case IMP_EVENT_INACTIVE:
1373 case IMP_EVENT_INVALIDATE: {
1374 struct ldlm_namespace *ns = obd->obd_namespace;
1375 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1378 case IMP_EVENT_ACTIVE:
1379 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1380 /* Clearing obd_no_recov allows us to continue pinging */
1381 obd->obd_no_recov = 0;
1382 mgc_notify_active(obd);
1383 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1384 ptlrpc_pinger_ir_up();
1388 case IMP_EVENT_DEACTIVATE:
1389 case IMP_EVENT_ACTIVATE:
1392 CERROR("Unknown import event %#x\n", event);
1399 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_SHIFT),
1400 CONFIG_READ_NRPAGES = 4
1403 static int mgc_apply_recover_logs(struct obd_device *mgc,
1404 struct config_llog_data *cld,
1406 void *data, int datalen, bool mne_swab)
1408 struct config_llog_instance *cfg = &cld->cld_cfg;
1409 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1410 struct mgs_nidtbl_entry *entry;
1411 struct lustre_cfg *lcfg;
1412 struct lustre_cfg_bufs bufs;
1413 u64 prev_version = 0;
1422 LASSERT(cfg->cfg_instance != 0);
1423 LASSERT(ll_get_cfg_instance(cfg->cfg_sb) == cfg->cfg_instance);
1425 OBD_ALLOC(inst, PAGE_SIZE);
1429 if (!IS_SERVER(lsi)) {
1430 pos = snprintf(inst, PAGE_SIZE, "%016lx", cfg->cfg_instance);
1431 if (pos >= PAGE_SIZE) {
1432 OBD_FREE(inst, PAGE_SIZE);
1436 LASSERT(IS_MDT(lsi));
1437 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1440 OBD_FREE(inst, PAGE_SIZE);
1448 bufsz = PAGE_SIZE - pos;
1450 while (datalen > 0) {
1451 int entry_len = sizeof(*entry);
1453 struct obd_device *obd;
1460 if (datalen < sizeof(*entry))
1463 entry = (typeof(entry))(data + off);
1466 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1468 if (entry->mne_nid_count == 0) /* at least one nid entry */
1470 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1473 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1474 if (datalen < entry_len) /* must have entry_len at least */
1477 /* Keep this swab for normal mixed endian handling. LU-1644 */
1479 lustre_swab_mgs_nidtbl_entry(entry);
1480 if (entry->mne_length > PAGE_SIZE) {
1481 CERROR("MNE too large (%u)\n", entry->mne_length);
1485 if (entry->mne_length < entry_len)
1488 off += entry->mne_length;
1489 datalen -= entry->mne_length;
1493 if (entry->mne_version > max_version) {
1494 CERROR("entry index(%lld) is over max_index(%lld)\n",
1495 entry->mne_version, max_version);
1499 if (prev_version >= entry->mne_version) {
1500 CERROR("index unsorted, prev %lld, now %lld\n",
1501 prev_version, entry->mne_version);
1504 prev_version = entry->mne_version;
1507 * Write a string with format "nid::instance" to
1508 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1511 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1512 memset(buf, 0, bufsz);
1516 /* lustre-OST0001-osc-<instance #> */
1517 strcpy(obdname, cld->cld_logname);
1518 cname = strrchr(obdname, '-');
1519 if (cname == NULL) {
1520 CERROR("mgc %s: invalid logname %s\n",
1521 mgc->obd_name, obdname);
1525 pos = cname - obdname;
1527 pos += sprintf(obdname + pos, "-%s%04x",
1528 is_ost ? "OST" : "MDT", entry->mne_index);
1530 cname = is_ost ? "osc" : "mdc",
1531 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1532 lustre_cfg_bufs_reset(&bufs, obdname);
1534 /* find the obd by obdname */
1535 obd = class_name2obd(obdname);
1537 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1538 mgc->obd_name, obdname);
1540 /* this is a safe race, when the ost is starting up...*/
1544 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1547 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1550 down_read(&obd->u.cli.cl_sem);
1551 if (obd->u.cli.cl_import == NULL) {
1552 /* client does not connect to the OST yet */
1553 up_read(&obd->u.cli.cl_sem);
1558 /* iterate all nids to find one */
1559 /* find uuid by nid */
1561 for (i = 0; i < entry->mne_nid_count; i++) {
1562 rc = client_import_find_conn(obd->u.cli.cl_import,
1564 (struct obd_uuid *)uuid);
1569 up_read(&obd->u.cli.cl_sem);
1571 CERROR("mgc: cannot find uuid by nid %s\n",
1572 libcfs_nid2str(entry->u.nids[0]));
1576 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1577 uuid, libcfs_nid2str(entry->u.nids[0]));
1579 pos += strlen(uuid);
1580 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1581 LASSERT(pos < bufsz);
1583 lustre_cfg_bufs_set_string(&bufs, 1, params);
1585 OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount,
1591 lustre_cfg_init(lcfg, LCFG_PARAM, &bufs);
1593 CDEBUG(D_INFO, "ir apply logs %lld/%lld for %s -> %s\n",
1594 prev_version, max_version, obdname, params);
1596 rc = class_process_config(lcfg);
1597 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
1598 lcfg->lcfg_buflens));
1600 CDEBUG(D_INFO, "process config for %s error %d\n",
1603 /* continue, even one with error */
1606 OBD_FREE(inst, PAGE_SIZE);
1611 * This function is called if this client was notified for target restarting
1612 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery or
1615 static int mgc_process_recover_nodemap_log(struct obd_device *obd,
1616 struct config_llog_data *cld)
1618 struct ptlrpc_connection *mgc_conn;
1619 struct ptlrpc_request *req = NULL;
1620 struct config_llog_instance *cfg = &cld->cld_cfg;
1621 struct mgs_config_body *body;
1622 struct mgs_config_res *res;
1623 struct nodemap_config *new_config = NULL;
1624 struct lu_nodemap *recent_nodemap = NULL;
1625 struct ptlrpc_bulk_desc *desc;
1626 struct page **pages = NULL;
1627 __u64 config_read_offset = 0;
1628 __u8 nodemap_cur_pass = 0;
1631 bool mne_swab = false;
1637 mgc_conn = class_exp2cliimp(cld->cld_mgcexp)->imp_connection;
1639 /* don't need to get local config */
1640 if (cld_is_nodemap(cld) && LNetIsPeerLocal(mgc_conn->c_peer.nid))
1643 /* allocate buffer for bulk transfer.
1644 * if this is the first time for this mgs to read logs,
1645 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1646 * once; otherwise, it only reads increment of logs, this should be
1647 * small and CONFIG_READ_NRPAGES will be used.
1649 nrpages = CONFIG_READ_NRPAGES;
1650 if (cfg->cfg_last_idx == 0 || cld_is_nodemap(cld))
1651 nrpages = CONFIG_READ_NRPAGES_INIT;
1653 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1655 GOTO(out, rc = -ENOMEM);
1657 for (i = 0; i < nrpages; i++) {
1658 pages[i] = alloc_page(GFP_KERNEL);
1659 if (pages[i] == NULL)
1660 GOTO(out, rc = -ENOMEM);
1664 #ifdef HAVE_SERVER_SUPPORT
1665 if (cld_is_nodemap(cld) && config_read_offset == 0) {
1666 new_config = nodemap_config_alloc();
1667 if (IS_ERR(new_config)) {
1668 rc = PTR_ERR(new_config);
1674 LASSERT(cld_is_recover(cld) || cld_is_nodemap(cld));
1675 LASSERT(mutex_is_locked(&cld->cld_lock));
1676 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1677 &RQF_MGS_CONFIG_READ);
1679 GOTO(out, rc = -ENOMEM);
1681 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1686 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1687 LASSERT(body != NULL);
1688 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1689 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1690 >= sizeof(body->mcb_name))
1691 GOTO(out, rc = -E2BIG);
1692 if (cld_is_nodemap(cld))
1693 body->mcb_offset = config_read_offset;
1695 body->mcb_offset = cfg->cfg_last_idx + 1;
1696 body->mcb_type = cld->cld_type;
1697 body->mcb_bits = PAGE_SHIFT;
1698 body->mcb_units = nrpages;
1699 body->mcb_nm_cur_pass = nodemap_cur_pass;
1701 /* allocate bulk transfer descriptor */
1702 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1,
1703 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1705 &ptlrpc_bulk_kiov_pin_ops);
1707 GOTO(out, rc = -ENOMEM);
1709 for (i = 0; i < nrpages; i++)
1710 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1713 ptlrpc_request_set_replen(req);
1714 rc = ptlrpc_queue_wait(req);
1718 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1720 GOTO(out, rc = -EPROTO);
1722 if (cld_is_nodemap(cld)) {
1723 config_read_offset = res->mcr_offset;
1724 eof = config_read_offset == II_END_OFF;
1725 nodemap_cur_pass = res->mcr_nm_cur_pass;
1727 if (res->mcr_size < res->mcr_offset)
1728 GOTO(out, rc = -EINVAL);
1730 /* always update the index even though it might have errors with
1731 * handling the recover logs
1733 cfg->cfg_last_idx = res->mcr_offset;
1734 eof = res->mcr_offset == res->mcr_size;
1736 CDEBUG(D_INFO, "Latest version %lld, more %d.\n",
1737 res->mcr_offset, eof == false);
1740 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1742 GOTO(out, rc = ealen);
1744 if (ealen > nrpages << PAGE_SHIFT)
1745 GOTO(out, rc = -EINVAL);
1747 if (ealen == 0) { /* no logs transferred */
1748 #ifdef HAVE_SERVER_SUPPORT
1749 /* config changed since first read RPC */
1750 if (cld_is_nodemap(cld) && config_read_offset == 0) {
1751 CDEBUG(D_INFO, "nodemap config changed in transit, retrying\n");
1752 GOTO(out, rc = -EAGAIN);
1760 mne_swab = ptlrpc_rep_need_swab(req);
1762 /* When a nodemap config is received, we build a new nodemap config,
1763 * with new nodemap structs. We keep track of the most recently added
1764 * nodemap since the config is read ordered by nodemap_id, and so it
1765 * is likely that the next record will be related. Because access to
1766 * the nodemaps is single threaded until the nodemap_config is active,
1767 * we don't need to reference count with recent_nodemap, though
1768 * recent_nodemap should be set to NULL when the nodemap_config
1769 * is either destroyed or set active.
1771 for (i = 0; i < nrpages && ealen > 0; i++) {
1775 ptr = kmap(pages[i]);
1776 if (cld_is_nodemap(cld))
1777 rc2 = nodemap_process_idx_pages(new_config, ptr,
1780 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset,
1787 CWARN("%s: error processing %s log %s: rc = %d\n",
1789 cld_is_nodemap(cld) ? "nodemap" : "recovery",
1792 GOTO(out, rc = rc2);
1800 ptlrpc_req_finished(req);
1804 if (rc == 0 && !eof)
1807 #ifdef HAVE_SERVER_SUPPORT
1808 if (new_config != NULL) {
1809 /* recent_nodemap cannot be used after set_active/dealloc */
1811 nodemap_config_set_active_mgc(new_config);
1813 nodemap_config_dealloc(new_config);
1818 for (i = 0; i < nrpages; i++) {
1819 if (pages[i] == NULL)
1821 __free_page(pages[i]);
1823 OBD_FREE(pages, sizeof(*pages) * nrpages);
1828 static int mgc_barrier_glimpse_ast(struct ldlm_lock *lock, void *data)
1830 struct config_llog_data *cld = lock->l_ast_data;
1834 if (cld->cld_stopping)
1837 rc = barrier_handler(s2lsi(cld->cld_cfg.cfg_sb)->lsi_dt_dev,
1838 (struct ptlrpc_request *)data);
1843 /* Copy a remote log locally */
1844 static int mgc_llog_local_copy(const struct lu_env *env,
1845 struct obd_device *obd,
1846 struct llog_ctxt *rctxt,
1847 struct llog_ctxt *lctxt, char *logname)
1855 * - copy it to backup using llog_backup()
1856 * - copy remote llog to logname using llog_backup()
1857 * - if failed then move bakup to logname again
1860 OBD_ALLOC(temp_log, strlen(logname) + 2);
1863 sprintf(temp_log, "%sT", logname);
1865 /* make a copy of local llog at first */
1866 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1867 if (rc < 0 && rc != -ENOENT)
1869 /* copy remote llog to the local copy */
1870 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1871 if (rc == -ENOENT) {
1872 /* no remote llog, delete local one too */
1873 llog_erase(env, lctxt, NULL, logname);
1874 } else if (rc < 0) {
1875 /* error during backup, get local one back from the copy */
1876 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1878 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1879 obd->obd_name, logname, rc);
1881 llog_erase(env, lctxt, NULL, temp_log);
1882 OBD_FREE(temp_log, strlen(logname) + 2);
1886 /* local_only means it cannot get remote llogs */
1887 static int mgc_process_cfg_log(struct obd_device *mgc,
1888 struct config_llog_data *cld, int local_only)
1890 struct llog_ctxt *ctxt, *lctxt = NULL;
1891 struct client_obd *cli = &mgc->u.cli;
1892 struct lustre_sb_info *lsi = NULL;
1899 LASSERT(mutex_is_locked(&cld->cld_lock));
1901 if (cld->cld_cfg.cfg_sb)
1902 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1908 rc = lu_env_init(env, LCT_MG_THREAD);
1912 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1915 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1917 /* Copy the setup log locally if we can. Don't mess around if we're
1918 * running an MGS though (logs are already local). */
1919 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1920 cli->cl_mgc_configs_dir != NULL &&
1921 lu2dt_dev(cli->cl_mgc_configs_dir->do_lu.lo_dev) ==
1923 if (!local_only && !lsi->lsi_dt_dev->dd_rdonly)
1924 /* Only try to copy log if we have the lock. */
1925 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1927 if (local_only || rc) {
1928 if (strcmp(cld->cld_logname, PARAMS_FILENAME) != 0 &&
1929 llog_is_empty(env, lctxt, cld->cld_logname)) {
1930 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1931 "log %s and no local copy."
1932 "\n", cld->cld_logname);
1933 GOTO(out_pop, rc = -ENOENT);
1935 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1936 "copy for now, will try to update later.\n",
1940 /* Now, whether we copied or not, start using the local llog.
1941 * If we failed to copy, we'll start using whatever the old
1943 llog_ctxt_put(ctxt);
1947 if (local_only) /* no local log at client side */
1948 GOTO(out_pop, rc = -EIO);
1952 if (lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1953 lsi->lsi_dt_dev->dd_rdonly) {
1954 struct llog_ctxt *rctxt;
1956 /* Under readonly mode, we may have no local copy or local
1957 * copy is incomplete, so try to use remote llog firstly. */
1958 rctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1961 rc = class_config_parse_llog(env, rctxt, cld->cld_logname,
1963 llog_ctxt_put(rctxt);
1966 if (rc && rc != -ENOENT)
1967 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1971 * update settings on existing OBDs. doing it inside
1972 * of llog_process_lock so no device is attaching/detaching
1974 * the logname must be <fsname>-sptlrpc
1976 if (rc == 0 && cld_is_sptlrpc(cld))
1977 class_notify_sptlrpc_conf(cld->cld_logname,
1978 strlen(cld->cld_logname) -
1979 strlen("-sptlrpc"));
1983 __llog_ctxt_put(env, ctxt);
1985 __llog_ctxt_put(env, lctxt);
1993 static bool mgc_import_in_recovery(struct obd_import *imp)
1995 bool in_recovery = true;
1997 spin_lock(&imp->imp_lock);
1998 if (imp->imp_state == LUSTRE_IMP_FULL ||
1999 imp->imp_state == LUSTRE_IMP_CLOSED)
2000 in_recovery = false;
2001 spin_unlock(&imp->imp_lock);
2007 * Get a configuration log from the MGS and process it.
2009 * This function is called for both clients and servers to process the
2010 * configuration log from the MGS. The MGC enqueues a DLM lock on the
2011 * log from the MGS, and if the lock gets revoked the MGC will be notified
2012 * by the lock cancellation callback that the config log has changed,
2013 * and will enqueue another MGS lock on it, and then continue processing
2014 * the new additions to the end of the log.
2016 * Since the MGC import is not replayable, if the import is being evicted
2017 * (rcl == -ESHUTDOWN, \see ptlrpc_import_delay_req()), retry to process
2018 * the log until recovery is finished or the import is closed.
2020 * Make a local copy of the log before parsing it if appropriate (non-MGS
2021 * server) so that the server can start even when the MGS is down.
2023 * There shouldn't be multiple processes running process_log at once --
2024 * sounds like badness. It actually might be fine, as long as they're not
2025 * trying to update from the same log simultaneously, in which case we
2026 * should use a per-log semaphore instead of cld_lock.
2028 * \param[in] mgc MGC device by which to fetch the configuration log
2029 * \param[in] cld log processing state (stored in lock callback data)
2031 * \retval 0 on success
2032 * \retval negative errno on failure
2034 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
2036 struct lustre_handle lockh = { 0 };
2037 __u64 flags = LDLM_FL_NO_LRU;
2042 LASSERT(cld != NULL);
2044 /* I don't want multiple processes running process_log at once --
2045 sounds like badness. It actually might be fine, as long as
2046 we're not trying to update from the same log
2047 simultaneously (in which case we should use a per-log sem.) */
2049 mutex_lock(&cld->cld_lock);
2050 if (cld->cld_stopping) {
2051 mutex_unlock(&cld->cld_lock);
2055 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
2057 CDEBUG(D_MGC, "Process log %s-%016lx from %d\n", cld->cld_logname,
2058 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
2060 /* Get the cfg lock on the llog */
2061 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, LDLM_PLAIN, NULL,
2063 cld_is_barrier(cld) ? mgc_barrier_glimpse_ast : NULL,
2064 cld, 0, NULL, &lockh);
2066 /* Get the cld, it will be released in mgc_blocking_ast. */
2067 config_log_get(cld);
2068 rc = ldlm_lock_set_data(&lockh, (void *)cld);
2071 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
2073 if (rcl == -ESHUTDOWN &&
2074 atomic_read(&mgc->u.cli.cl_mgc_refcount) > 0 && !retry) {
2075 struct obd_import *imp;
2076 struct l_wait_info lwi;
2077 int secs = cfs_time_seconds(obd_timeout);
2079 mutex_unlock(&cld->cld_lock);
2080 imp = class_exp2cliimp(mgc->u.cli.cl_mgc_mgsexp);
2082 /* Let's force the pinger, and wait the import to be
2083 * connected, note: since mgc import is non-replayable,
2084 * and even the import state is disconnected, it does
2085 * not mean the "recovery" is stopped, so we will keep
2086 * waitting until timeout or the import state is
2088 ptlrpc_pinger_force(imp);
2090 lwi = LWI_TIMEOUT(secs, NULL, NULL);
2091 l_wait_event(imp->imp_recovery_waitq,
2092 !mgc_import_in_recovery(imp), &lwi);
2094 if (imp->imp_state == LUSTRE_IMP_FULL) {
2098 mutex_lock(&cld->cld_lock);
2099 /* unlock/lock mutex, so check stopping again */
2100 if (cld->cld_stopping) {
2101 mutex_unlock(&cld->cld_lock);
2104 spin_lock(&config_list_lock);
2105 cld->cld_lostlock = 1;
2106 spin_unlock(&config_list_lock);
2109 /* mark cld_lostlock so that it will requeue
2110 * after MGC becomes available. */
2111 spin_lock(&config_list_lock);
2112 cld->cld_lostlock = 1;
2113 spin_unlock(&config_list_lock);
2117 if (cld_is_recover(cld) || cld_is_nodemap(cld)) {
2119 rc = mgc_process_recover_nodemap_log(mgc, cld);
2120 else if (cld_is_nodemap(cld))
2123 if (cld_is_recover(cld) && rc) {
2125 CERROR("%s: recover log %s failed, not fatal: rc = %d\n",
2126 mgc->obd_name, cld->cld_logname, rc);
2127 spin_lock(&config_list_lock);
2128 cld->cld_lostlock = 1;
2129 spin_unlock(&config_list_lock);
2131 rc = 0; /* this is not a fatal error for recover log */
2133 } else if (!cld_is_barrier(cld)) {
2134 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
2137 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
2138 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
2140 mutex_unlock(&cld->cld_lock);
2142 /* Now drop the lock so MGS can revoke it */
2144 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, LCK_CR, &lockh);
2146 CERROR("Can't drop cfg lock: %d\n", rcl);
2149 /* requeue nodemap lock immediately if transfer was interrupted */
2150 if (cld_is_nodemap(cld) && rc == -EAGAIN) {
2151 mgc_requeue_add(cld);
2159 /** Called from lustre_process_log.
2160 * LCFG_LOG_START gets the config log from the MGS, processes it to start
2161 * any services, and adds it to the list logs to watch (follow).
2163 static int mgc_process_config(struct obd_device *obd, size_t len, void *buf)
2165 struct lustre_cfg *lcfg = buf;
2166 struct config_llog_instance *cfg = NULL;
2171 switch(lcfg->lcfg_command) {
2172 case LCFG_LOV_ADD_OBD: {
2173 /* Overloading this cfg command: register a new target */
2174 struct mgs_target_info *mti;
2176 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
2177 sizeof(struct mgs_target_info))
2178 GOTO(out, rc = -EINVAL);
2180 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
2181 CDEBUG(D_MGC, "add_target %s %#x\n",
2182 mti->mti_svname, mti->mti_flags);
2183 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
2186 case LCFG_LOV_DEL_OBD:
2187 /* Unregister has no meaning at the moment. */
2188 CERROR("lov_del_obd unimplemented\n");
2191 case LCFG_SPTLRPC_CONF: {
2192 rc = sptlrpc_process_config(lcfg);
2195 case LCFG_LOG_START: {
2196 struct config_llog_data *cld;
2197 struct super_block *sb;
2199 logname = lustre_cfg_string(lcfg, 1);
2200 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
2201 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
2203 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
2206 /* We're only called through here on the initial mount */
2207 cld = config_log_add(obd, logname, cfg, sb);
2213 rc = mgc_process_log(obd, cld);
2214 if (rc == 0 && cld->cld_recover != NULL) {
2215 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
2216 imp_connect_data, IMP_RECOV)) {
2217 rc = mgc_process_log(obd, cld->cld_recover);
2219 struct config_llog_data *cir;
2221 mutex_lock(&cld->cld_lock);
2222 cir = cld->cld_recover;
2223 cld->cld_recover = NULL;
2224 mutex_unlock(&cld->cld_lock);
2225 config_log_put(cir);
2229 CERROR("Cannot process recover llog %d\n", rc);
2232 if (rc == 0 && cld->cld_params != NULL) {
2233 rc = mgc_process_log(obd, cld->cld_params);
2234 if (rc == -ENOENT) {
2235 CDEBUG(D_MGC, "There is no params "
2236 "config file yet\n");
2239 /* params log is optional */
2241 CERROR("%s: can't process params llog: rc = %d\n",
2247 case LCFG_LOG_END: {
2248 logname = lustre_cfg_string(lcfg, 1);
2250 if (lcfg->lcfg_bufcount >= 2)
2251 cfg = (struct config_llog_instance *)lustre_cfg_buf(
2253 rc = config_log_end(logname, cfg);
2257 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
2258 GOTO(out, rc = -EINVAL);
2266 static struct obd_ops mgc_obd_ops = {
2267 .o_owner = THIS_MODULE,
2268 .o_setup = mgc_setup,
2269 .o_precleanup = mgc_precleanup,
2270 .o_cleanup = mgc_cleanup,
2271 .o_add_conn = client_import_add_conn,
2272 .o_del_conn = client_import_del_conn,
2273 .o_connect = client_connect_import,
2274 .o_disconnect = client_disconnect_export,
2275 .o_set_info_async = mgc_set_info_async,
2276 .o_get_info = mgc_get_info,
2277 .o_import_event = mgc_import_event,
2278 .o_process_config = mgc_process_config,
2281 static int __init mgc_init(void)
2283 return class_register_type(&mgc_obd_ops, NULL, false, NULL,
2284 LUSTRE_MGC_NAME, NULL);
2287 static void __exit mgc_exit(void)
2289 class_unregister_type(LUSTRE_MGC_NAME);
2292 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2293 MODULE_DESCRIPTION("Lustre Management Client");
2294 MODULE_VERSION(LUSTRE_VERSION_STRING);
2295 MODULE_LICENSE("GPL");
2297 module_init(mgc_init);
2298 module_exit(mgc_exit);