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/
31 * lustre/mgc/mgc_request.c
33 * Author: Nathan Rutman <nathan@clusterfs.com>
36 #define DEBUG_SUBSYSTEM S_MGC
37 #define D_MGC D_CONFIG /*|D_WARNING*/
39 #include <linux/module.h>
40 #include <linux/kthread.h>
41 #include <linux/random.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,
56 enum mgs_cfg_type type)
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 */
75 case MGS_CFG_T_CONFIG:
76 case MGS_CFG_T_SPTLRPC:
79 case MGS_CFG_T_RECOVER:
80 case MGS_CFG_T_PARAMS:
81 case MGS_CFG_T_NODEMAP:
82 case MGS_CFG_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,
95 enum mgs_cfg_type type)
97 /* fsname is at most 8 chars long, maybe contain "-".
98 * e.g. "lustre", "SUN-000" */
99 return mgc_name2resid(fsname, strlen(fsname), res_id, type);
101 EXPORT_SYMBOL(mgc_fsname2resid);
103 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id,
104 enum mgs_cfg_type type)
109 /* logname consists of "fsname-nodetype".
110 * e.g. "lustre-MDT0001", "SUN-000-client"
111 * there is an exception: llog "params" */
112 name_end = strrchr(logname, '-');
114 len = strlen(logname);
116 len = name_end - logname;
117 return mgc_name2resid(logname, len, res_id, type);
119 EXPORT_SYMBOL(mgc_logname2resid);
121 /********************** config llog list **********************/
122 static LIST_HEAD(config_llog_list);
123 static DEFINE_SPINLOCK(config_list_lock); /* protects config_llog_list */
125 /* Take a reference to a config log */
126 static int config_log_get(struct config_llog_data *cld)
129 atomic_inc(&cld->cld_refcount);
130 CDEBUG(D_INFO, "log %s (%p) refs %d\n", cld->cld_logname, cld,
131 atomic_read(&cld->cld_refcount));
135 /* Drop a reference to a config log. When no longer referenced,
136 we can free the config log data */
137 static void config_log_put(struct config_llog_data *cld)
144 CDEBUG(D_INFO, "log %s(%p) refs %d\n", cld->cld_logname, cld,
145 atomic_read(&cld->cld_refcount));
146 LASSERT(atomic_read(&cld->cld_refcount) > 0);
148 /* spinlock to make sure no item with 0 refcount in the list */
149 if (atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
150 list_del(&cld->cld_list_chain);
151 spin_unlock(&config_list_lock);
153 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
155 config_log_put(cld->cld_barrier);
156 config_log_put(cld->cld_recover);
157 config_log_put(cld->cld_params);
158 config_log_put(cld->cld_nodemap);
159 config_log_put(cld->cld_sptlrpc);
160 if (cld_is_sptlrpc(cld)) {
161 cld->cld_stopping = 1;
162 sptlrpc_conf_log_stop(cld->cld_logname);
165 class_export_put(cld->cld_mgcexp);
166 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
172 /* Find a config log by name */
174 struct config_llog_data *config_log_find(char *logname,
175 struct config_llog_instance *cfg)
177 struct config_llog_data *cld;
178 struct config_llog_data *found = NULL;
179 unsigned long cfg_instance;
182 LASSERT(logname != NULL);
184 cfg_instance = cfg ? cfg->cfg_instance : 0;
185 spin_lock(&config_list_lock);
186 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
187 /* check if cfg_instance is the one we want */
188 if (cfg_instance != cld->cld_cfg.cfg_instance)
191 /* instance may be NULL, should check name */
192 if (strcmp(logname, cld->cld_logname) == 0) {
194 config_log_get(found);
198 spin_unlock(&config_list_lock);
203 struct config_llog_data *do_config_log_add(struct obd_device *obd,
205 enum mgs_cfg_type type,
206 struct config_llog_instance *cfg,
207 struct super_block *sb)
209 struct config_llog_data *cld;
214 CDEBUG(D_MGC, "do adding config log %s-%016lx\n", logname,
215 cfg ? cfg->cfg_instance : 0);
217 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
219 RETURN(ERR_PTR(-ENOMEM));
221 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
223 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
227 strcpy(cld->cld_logname, logname);
231 cld->cld_cfg.cfg_callback = class_config_llog_handler;
232 mutex_init(&cld->cld_lock);
233 cld->cld_cfg.cfg_last_idx = 0;
234 cld->cld_cfg.cfg_flags = 0;
235 cld->cld_cfg.cfg_sb = sb;
236 cld->cld_type = type;
237 atomic_set(&cld->cld_refcount, 1);
239 /* Keep the mgc around until we are done */
240 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
242 if (cld_is_sptlrpc(cld))
243 sptlrpc_conf_log_start(logname);
245 spin_lock(&config_list_lock);
246 list_add(&cld->cld_list_chain, &config_llog_list);
247 spin_unlock(&config_list_lock);
249 if (cld_is_sptlrpc(cld) || cld_is_nodemap(cld) || cld_is_barrier(cld)) {
250 rc = mgc_process_log(obd, cld);
251 if (rc && rc != -ENOENT)
252 CERROR("%s: failed processing log, type %d: rc = %d\n",
253 obd->obd_name, type, rc);
259 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
261 struct config_llog_instance *cfg,
262 struct super_block *sb)
264 struct config_llog_instance lcfg = *cfg;
265 struct lustre_sb_info *lsi = s2lsi(sb);
266 struct config_llog_data *cld;
272 /* for osp-on-ost, see lustre_start_osp() */
273 if (IS_MDT(lsi) && lcfg.cfg_instance)
276 /* We have to use different llog for clients and MDTs for DNE,
277 * where only clients are notified if one of DNE server restarts.
279 LASSERT(strlen(fsname) < sizeof(logname) / 2);
280 strncpy(logname, fsname, sizeof(logname));
281 if (IS_SERVER(lsi)) { /* mdt */
282 LASSERT(lcfg.cfg_instance == 0);
283 lcfg.cfg_instance = ll_get_cfg_instance(sb);
284 strncat(logname, "-mdtir", sizeof(logname));
286 LASSERT(lcfg.cfg_instance != 0);
287 strncat(logname, "-cliir", sizeof(logname));
290 cld = do_config_log_add(obd, logname, MGS_CFG_T_RECOVER, &lcfg, sb);
294 static struct config_llog_data *
295 config_log_find_or_add(struct obd_device *obd, char *logname,
296 struct super_block *sb, enum mgs_cfg_type type,
297 struct config_llog_instance *cfg)
299 struct config_llog_instance lcfg = *cfg;
300 struct config_llog_data *cld;
302 /* Note class_config_llog_handler() depends on getting "obd" back */
303 lcfg.cfg_instance = sb ? ll_get_cfg_instance(sb) : (unsigned long)obd;
305 cld = config_log_find(logname, &lcfg);
306 if (unlikely(cld != NULL))
309 return do_config_log_add(obd, logname, type, &lcfg, sb);
312 /** Add this log to the list of active logs watched by an MGC.
313 * Active means we're watching for updates.
314 * We have one active log per "mount" - client instance or servername.
315 * Each instance may be at a different point in the log.
317 static struct config_llog_data *
318 config_log_add(struct obd_device *obd, char *logname,
319 struct config_llog_instance *cfg, struct super_block *sb)
321 struct lustre_sb_info *lsi = s2lsi(sb);
322 struct config_llog_data *cld = NULL;
323 struct config_llog_data *sptlrpc_cld = NULL;
324 struct config_llog_data *params_cld = NULL;
325 struct config_llog_data *nodemap_cld = NULL;
326 struct config_llog_data *barrier_cld = NULL;
333 CDEBUG(D_MGC, "add config log %s-%016lx\n", logname,
337 * for each regular log, the depended sptlrpc log name is
338 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
340 ptr = strrchr(logname, '-');
341 if (ptr == NULL || ptr - logname > 8) {
342 CERROR("logname %s is too long\n", logname);
343 RETURN(ERR_PTR(-EINVAL));
346 memcpy(seclogname, logname, ptr - logname);
347 strcpy(seclogname + (ptr - logname), "-sptlrpc");
349 if (cfg->cfg_sub_clds & CONFIG_SUB_SPTLRPC) {
350 sptlrpc_cld = config_log_find_or_add(obd, seclogname, NULL,
351 MGS_CFG_T_SPTLRPC, cfg);
352 if (IS_ERR(sptlrpc_cld)) {
353 CERROR("%s: can't create sptlrpc log %s: rc = %ld\n",
354 obd->obd_name, seclogname, PTR_ERR(sptlrpc_cld));
359 if (!IS_MGS(lsi) && cfg->cfg_sub_clds & CONFIG_SUB_NODEMAP) {
360 nodemap_cld = config_log_find_or_add(obd, LUSTRE_NODEMAP_NAME,
361 NULL, MGS_CFG_T_NODEMAP,
363 if (IS_ERR(nodemap_cld)) {
364 rc = PTR_ERR(nodemap_cld);
365 CERROR("%s: cannot create nodemap log: rc = %d\n",
367 GOTO(out_sptlrpc, rc);
371 if (cfg->cfg_sub_clds & CONFIG_SUB_PARAMS) {
372 params_cld = config_log_find_or_add(obd, PARAMS_FILENAME, sb,
373 MGS_CFG_T_PARAMS, cfg);
374 if (IS_ERR(params_cld)) {
375 rc = PTR_ERR(params_cld);
376 CERROR("%s: can't create params log: rc = %d\n",
378 GOTO(out_nodemap, rc);
382 if (IS_MDT(s2lsi(sb)) && cfg->cfg_sub_clds & CONFIG_SUB_BARRIER) {
383 snprintf(seclogname + (ptr - logname), sizeof(seclogname) - 1,
384 "-%s", BARRIER_FILENAME);
385 barrier_cld = config_log_find_or_add(obd, seclogname, sb,
386 MGS_CFG_T_BARRIER, cfg);
387 if (IS_ERR(barrier_cld)) {
388 rc = PTR_ERR(barrier_cld);
389 CERROR("%s: can't create barrier log: rc = %d\n",
391 GOTO(out_params, rc);
395 cld = do_config_log_add(obd, logname, MGS_CFG_T_CONFIG, cfg, sb);
398 CERROR("%s: can't create log: rc = %d\n",
400 GOTO(out_barrier, rc = PTR_ERR(cld));
403 LASSERT(lsi->lsi_lmd);
404 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR) &&
405 cfg->cfg_sub_clds & CONFIG_SUB_RECOVER) {
406 struct config_llog_data *recover_cld;
408 ptr = strrchr(seclogname, '-');
412 CERROR("%s: sptlrpc log name not correct, %s: "
413 "rc = %d\n", obd->obd_name, seclogname, -EINVAL);
414 GOTO(out_cld, rc = -EINVAL);
417 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
418 if (IS_ERR(recover_cld)) {
419 rc = PTR_ERR(recover_cld);
420 CERROR("%s: can't create recover log: rc = %d\n",
425 mutex_lock(&cld->cld_lock);
427 cld->cld_recover = recover_cld;
431 mutex_lock(&cld->cld_lock);
432 cld->cld_params = params_cld;
433 cld->cld_barrier = barrier_cld;
434 cld->cld_nodemap = nodemap_cld;
435 cld->cld_sptlrpc = sptlrpc_cld;
436 mutex_unlock(&cld->cld_lock);
443 config_log_put(barrier_cld);
445 config_log_put(params_cld);
447 config_log_put(nodemap_cld);
449 config_log_put(sptlrpc_cld);
454 DEFINE_MUTEX(llog_process_lock);
456 static inline void config_mark_cld_stop_nolock(struct config_llog_data *cld)
460 spin_lock(&config_list_lock);
461 cld->cld_stopping = 1;
462 spin_unlock(&config_list_lock);
464 CDEBUG(D_INFO, "lockh %#llx\n", cld->cld_lockh.cookie);
465 if (!ldlm_lock_addref_try(&cld->cld_lockh, LCK_CR))
466 ldlm_lock_decref_and_cancel(&cld->cld_lockh, LCK_CR);
469 static inline void config_mark_cld_stop(struct config_llog_data *cld)
472 mutex_lock(&cld->cld_lock);
473 config_mark_cld_stop_nolock(cld);
474 mutex_unlock(&cld->cld_lock);
478 /** Stop watching for updates on this log.
480 static int config_log_end(char *logname, struct config_llog_instance *cfg)
482 struct config_llog_data *cld;
483 struct config_llog_data *cld_sptlrpc = NULL;
484 struct config_llog_data *cld_params = NULL;
485 struct config_llog_data *cld_recover = NULL;
486 struct config_llog_data *cld_nodemap = NULL;
487 struct config_llog_data *cld_barrier = NULL;
492 cld = config_log_find(logname, cfg);
496 mutex_lock(&cld->cld_lock);
498 * if cld_stopping is set, it means we didn't start the log thus
499 * not owning the start ref. this can happen after previous umount:
500 * the cld still hanging there waiting for lock cancel, and we
501 * remount again but failed in the middle and call log_end without
504 if (unlikely(cld->cld_stopping)) {
505 mutex_unlock(&cld->cld_lock);
506 /* drop the ref from the find */
511 cld_recover = cld->cld_recover;
512 cld->cld_recover = NULL;
513 cld_params = cld->cld_params;
514 cld->cld_params = NULL;
515 cld_nodemap = cld->cld_nodemap;
516 cld->cld_nodemap = NULL;
517 cld_barrier = cld->cld_barrier;
518 cld->cld_barrier = NULL;
519 cld_sptlrpc = cld->cld_sptlrpc;
520 cld->cld_sptlrpc = NULL;
522 config_mark_cld_stop_nolock(cld);
523 mutex_unlock(&cld->cld_lock);
525 config_mark_cld_stop(cld_recover);
526 config_log_put(cld_recover);
527 config_mark_cld_stop(cld_params);
528 config_log_put(cld_params);
529 config_mark_cld_stop(cld_barrier);
530 config_log_put(cld_barrier);
531 /* don't explicitly set cld_stopping on sptlrpc lock here, as other
532 * targets may be active, it will be done in config_log_put if necessary
534 config_log_put(cld_sptlrpc);
535 /* don't set cld_stopping on nm lock as other targets may be active */
536 config_log_put(cld_nodemap);
538 /* drop the ref from the find */
540 /* drop the start ref */
543 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
548 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
550 struct obd_device *obd = data;
551 struct obd_import *imp;
552 struct obd_connect_data *ocd;
553 struct config_llog_data *cld;
558 with_imp_locked(obd, imp, rc) {
559 ocd = &imp->imp_connect_data;
561 seq_printf(m, "imperative_recovery: %s\n",
562 OCD_HAS_FLAG(ocd, IMP_RECOV) ?
563 "ENABLED" : "DISABLED");
568 seq_printf(m, "client_state:\n");
570 spin_lock(&config_list_lock);
571 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
572 if (cld->cld_recover == NULL)
574 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
576 cld->cld_recover->cld_cfg.cfg_last_idx);
578 spin_unlock(&config_list_lock);
583 /* reenqueue any lost locks */
584 #define RQ_RUNNING 0x1
588 #define RQ_PRECLEANUP 0x10
589 static int rq_state = 0;
590 static wait_queue_head_t rq_waitq;
591 static DECLARE_COMPLETION(rq_exit);
592 static DECLARE_COMPLETION(rq_start);
594 static void do_requeue(struct config_llog_data *cld)
599 LASSERT(atomic_read(&cld->cld_refcount) > 0);
602 * Do not run mgc_process_log on a disconnected export or an
603 * export which is being disconnected. Take the client
604 * semaphore to make the check non-racy.
606 down_read_nested(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem,
608 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
609 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
610 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
611 if (rc && rc != -ENOENT)
612 CERROR("failed processing log: %d\n", rc);
614 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
617 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
622 static int mgc_requeue_thread(void *data)
628 CDEBUG(D_MGC, "Starting requeue thread\n");
630 /* Keep trying failed locks periodically */
631 spin_lock(&config_list_lock);
632 rq_state |= RQ_RUNNING;
633 while (!(rq_state & RQ_STOP)) {
634 struct config_llog_data *cld, *cld_prev;
637 /* Any new or requeued lostlocks will change the state */
638 rq_state &= ~(RQ_NOW | RQ_LATER);
639 spin_unlock(&config_list_lock);
646 /* Always wait a few seconds to allow the server who
647 * caused the lock revocation to finish its setup, plus some
648 * random so everyone doesn't try to reconnect at once.
650 to = mgc_requeue_timeout_min == 0 ? 1 : mgc_requeue_timeout_min;
651 to = cfs_time_seconds(mgc_requeue_timeout_min) +
652 prandom_u32_max(cfs_time_seconds(to));
653 wait_event_idle_timeout(rq_waitq,
654 rq_state & (RQ_STOP | RQ_PRECLEANUP), to);
657 * iterate & processing through the list. for each cld, process
658 * its depending sptlrpc cld firstly (if any) and then itself.
660 * it's guaranteed any item in the list must have
661 * reference > 0; and if cld_lostlock is set, at
662 * least one reference is taken by the previous enqueue.
666 spin_lock(&config_list_lock);
667 rq_state &= ~RQ_PRECLEANUP;
668 list_for_each_entry(cld, &config_llog_list,
670 if (!cld->cld_lostlock || cld->cld_stopping)
673 /* hold reference to avoid being freed during
674 * subsequent processing. */
676 cld->cld_lostlock = 0;
677 spin_unlock(&config_list_lock);
679 config_log_put(cld_prev);
682 if (likely(!(rq_state & RQ_STOP))) {
684 spin_lock(&config_list_lock);
686 spin_lock(&config_list_lock);
690 spin_unlock(&config_list_lock);
691 config_log_put(cld_prev);
693 /* Wait a bit to see if anyone else needs a requeue */
694 wait_event_idle(rq_waitq, rq_state & (RQ_NOW | RQ_STOP));
695 spin_lock(&config_list_lock);
698 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
699 rq_state &= ~RQ_RUNNING;
700 spin_unlock(&config_list_lock);
704 CDEBUG(D_MGC, "Ending requeue thread\n");
708 /* Add a cld to the list to requeue. Start the requeue thread if needed.
709 We are responsible for dropping the config log reference from here on out. */
710 static void mgc_requeue_add(struct config_llog_data *cld)
715 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
716 cld->cld_logname, atomic_read(&cld->cld_refcount),
717 cld->cld_stopping, rq_state);
718 LASSERT(atomic_read(&cld->cld_refcount) > 0);
720 /* lets cancel an existent lock to mark cld as "lostlock" */
721 CDEBUG(D_INFO, "lockh %#llx\n", cld->cld_lockh.cookie);
722 if (!ldlm_lock_addref_try(&cld->cld_lockh, LCK_CR))
723 ldlm_lock_decref_and_cancel(&cld->cld_lockh, LCK_CR);
725 mutex_lock(&cld->cld_lock);
726 spin_lock(&config_list_lock);
727 if (!(rq_state & RQ_STOP) && !cld->cld_stopping) {
728 cld->cld_lostlock = 1;
732 spin_unlock(&config_list_lock);
733 mutex_unlock(&cld->cld_lock);
740 /********************** class fns **********************/
741 static int mgc_local_llog_init(const struct lu_env *env,
742 struct obd_device *obd,
743 struct obd_device *disk)
745 struct llog_ctxt *ctxt;
750 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
755 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
757 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
763 static int mgc_local_llog_fini(const struct lu_env *env,
764 struct obd_device *obd)
766 struct llog_ctxt *ctxt;
770 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
771 llog_cleanup(env, ctxt);
776 static int mgc_fs_setup(const struct lu_env *env, struct obd_device *obd,
777 struct super_block *sb)
779 struct lustre_sb_info *lsi = s2lsi(sb);
780 struct client_obd *cli = &obd->u.cli;
781 struct lu_fid rfid, fid;
782 struct dt_object *root, *dto;
788 LASSERT(lsi->lsi_dt_dev);
790 /* The mgc fs exclusion mutex. Only one fs can be setup at a time. */
791 mutex_lock(&cli->cl_mgc_mutex);
793 /* Setup the configs dir */
794 fid.f_seq = FID_SEQ_LOCAL_NAME;
797 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
802 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
806 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
807 &cli->cl_mgc_los->los_dev->dd_lu_dev, NULL);
808 if (unlikely(IS_ERR(root)))
809 GOTO(out_los, rc = PTR_ERR(root));
811 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
813 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
814 dt_object_put_nocache(env, root);
816 GOTO(out_los, rc = PTR_ERR(dto));
818 cli->cl_mgc_configs_dir = dto;
820 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
821 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
825 /* We take an obd ref to insure that we can't get to mgc_cleanup
826 * without calling mgc_fs_cleanup first. */
827 class_incref(obd, "mgc_fs", obd);
829 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
833 dt_object_put(env, cli->cl_mgc_configs_dir);
834 cli->cl_mgc_configs_dir = NULL;
838 local_oid_storage_fini(env, cli->cl_mgc_los);
839 cli->cl_mgc_los = NULL;
840 mutex_unlock(&cli->cl_mgc_mutex);
845 static int mgc_fs_cleanup(const struct lu_env *env, struct obd_device *obd)
847 struct client_obd *cli = &obd->u.cli;
850 LASSERT(cli->cl_mgc_los != NULL);
852 mgc_local_llog_fini(env, obd);
854 dt_object_put_nocache(env, cli->cl_mgc_configs_dir);
855 cli->cl_mgc_configs_dir = NULL;
857 local_oid_storage_fini(env, cli->cl_mgc_los);
858 cli->cl_mgc_los = NULL;
860 class_decref(obd, "mgc_fs", obd);
861 mutex_unlock(&cli->cl_mgc_mutex);
866 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
868 struct llog_ctxt *ctxt;
873 /* setup only remote ctxt, the local disk context is switched per each
874 * filesystem during mgc_fs_setup() */
875 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
880 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
883 llog_initiator_connect(ctxt);
889 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
891 struct llog_ctxt *ctxt;
895 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
897 llog_cleanup(env, ctxt);
903 static atomic_t mgc_count = ATOMIC_INIT(0);
904 static int mgc_precleanup(struct obd_device *obd)
910 if (atomic_dec_and_test(&mgc_count)) {
911 LASSERT(rq_state & RQ_RUNNING);
912 /* stop requeue thread */
915 /* wakeup requeue thread to clean our cld */
916 temp = RQ_NOW | RQ_PRECLEANUP;
919 spin_lock(&config_list_lock);
921 spin_unlock(&config_list_lock);
925 wait_for_completion(&rq_exit);
926 obd_cleanup_client_import(obd);
928 rc = mgc_llog_fini(NULL, obd);
930 CERROR("failed to cleanup llogging subsystems\n");
935 static int mgc_cleanup(struct obd_device *obd)
940 /* COMPAT_146 - old config logs may have added profiles we don't
942 if (atomic_read(&obd->obd_type->typ_refcnt) <= 1)
943 /* Only for the last mgc */
944 class_del_profiles();
946 lprocfs_obd_cleanup(obd);
949 rc = client_obd_cleanup(obd);
953 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
955 struct task_struct *task;
959 rc = ptlrpcd_addref();
963 rc = client_obd_setup(obd, lcfg);
965 GOTO(err_decref, rc);
967 rc = mgc_llog_init(NULL, obd);
969 CERROR("failed to setup llogging subsystems\n");
970 GOTO(err_cleanup, rc);
973 rc = mgc_tunables_init(obd);
977 if (atomic_inc_return(&mgc_count) == 1) {
979 init_waitqueue_head(&rq_waitq);
981 /* start requeue thread */
982 task = kthread_run(mgc_requeue_thread, NULL, "ll_cfg_requeue");
985 CERROR("%s: cannot start requeue thread: rc = %d; "
986 "no more log updates\n",
990 /* rc is the task_struct pointer of mgc_requeue_thread. */
992 wait_for_completion(&rq_start);
998 lprocfs_obd_cleanup(obd);
1000 client_obd_cleanup(obd);
1006 /* based on ll_mdc_blocking_ast */
1007 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
1008 void *data, int flag)
1010 struct lustre_handle lockh;
1011 struct config_llog_data *cld = (struct config_llog_data *)data;
1016 case LDLM_CB_BLOCKING:
1017 /* mgs wants the lock, give it up... */
1018 LDLM_DEBUG(lock, "MGC blocking CB");
1019 ldlm_lock2handle(lock, &lockh);
1020 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
1022 case LDLM_CB_CANCELING:
1023 /* We've given up the lock, prepare ourselves to update. */
1024 LDLM_DEBUG(lock, "MGC cancel CB");
1026 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
1027 PLDLMRES(lock->l_resource),
1028 (char *)&lock->l_resource->lr_name.name[0]);
1031 CDEBUG(D_INFO, "missing data, won't requeue\n");
1035 /* held at mgc_process_log(). */
1036 LASSERT(atomic_read(&cld->cld_refcount) > 0);
1038 lock->l_ast_data = NULL;
1039 cld->cld_lockh.cookie = 0;
1040 /* Are we done with this log? */
1041 if (cld->cld_stopping) {
1042 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
1044 config_log_put(cld);
1047 /* Make sure not to re-enqueue when the mgc is stopping
1048 (we get called from client_disconnect_export) */
1049 if (lock->l_conn_export == NULL ||
1050 lock->l_conn_export->exp_obd->u.cli.cl_conn_count == 0) {
1051 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
1053 config_log_put(cld);
1057 /* Re-enqueue now */
1058 mgc_requeue_add(cld);
1059 config_log_put(cld);
1068 /* Not sure where this should go... */
1069 /* This is the timeout value for MGS_CONNECT request plus a ping interval, such
1070 * that we can have a chance to try the secondary MGS if any. */
1071 #define MGC_ENQUEUE_LIMIT (INITIAL_CONNECT_TIMEOUT + (AT_OFF ? 0 : at_min) \
1073 #define MGC_TARGET_REG_LIMIT 10
1074 #define MGC_TARGET_REG_LIMIT_MAX RECONNECT_DELAY_MAX
1075 #define MGC_SEND_PARAM_LIMIT 10
1077 /* Take a config lock so we can get cancel notifications */
1078 static int mgc_enqueue(struct obd_export *exp, enum ldlm_type type,
1079 union ldlm_policy_data *policy, enum ldlm_mode mode,
1080 __u64 *flags, ldlm_glimpse_callback glimpse_callback,
1081 void *data, __u32 lvb_len, void *lvb_swabber,
1082 struct lustre_handle *lockh)
1084 struct config_llog_data *cld = (struct config_llog_data *)data;
1085 struct ldlm_enqueue_info einfo = {
1088 .ei_cb_bl = mgc_blocking_ast,
1089 .ei_cb_cp = ldlm_completion_ast,
1090 .ei_cb_gl = glimpse_callback,
1092 struct ptlrpc_request *req;
1093 int short_limit = cld_is_sptlrpc(cld);
1100 CDEBUG(D_MGC, "Enqueue for %s (res %#llx)\n", cld->cld_logname,
1101 cld->cld_resid.name[0]);
1103 /* We need a callback for every lockholder, so don't try to
1104 ldlm_lock_match (see rev 1.1.2.11.2.47) */
1105 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1106 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1111 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1112 ptlrpc_request_set_replen(req);
1114 /* check if this is server or client */
1115 if (cld->cld_cfg.cfg_sb) {
1116 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
1117 if (lsi && IS_SERVER(lsi))
1120 /* Limit how long we will wait for the enqueue to complete */
1121 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
1122 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1123 NULL, 0, LVB_T_NONE, lockh, 0);
1124 /* A failed enqueue should still call the mgc_blocking_ast,
1125 where it will be requeued if needed ("grant failed"). */
1126 ptlrpc_req_finished(req);
1130 static int mgc_cancel(struct obd_export *exp, enum ldlm_mode mode,
1131 struct lustre_handle *lockh)
1135 ldlm_lock_decref(lockh, mode);
1140 static void mgc_notify_active(struct obd_device *unused)
1142 /* wakeup mgc_requeue_thread to requeue mgc lock */
1143 spin_lock(&config_list_lock);
1145 spin_unlock(&config_list_lock);
1148 /* TODO: Help the MGS rebuild nidtbl. -jay */
1151 /* Send target_reg message to MGS */
1152 static int mgc_target_register(struct obd_export *exp,
1153 struct mgs_target_info *mti)
1155 struct ptlrpc_request *req;
1156 struct mgs_target_info *req_mti, *rep_mti;
1160 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1161 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1166 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1168 ptlrpc_req_finished(req);
1172 memcpy(req_mti, mti, sizeof(*req_mti));
1173 ptlrpc_request_set_replen(req);
1174 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1175 /* Limit how long we will wait for the enqueue to complete */
1176 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1178 /* if the target needs to regenerate the config log in MGS, it's better
1179 * to use some longer limit to let MGC have time to change connection to
1180 * another MGS (or try again with the same MGS) for the target (server)
1181 * will fail and exit if the request expired due to delay limit. */
1182 if (mti->mti_flags & (LDD_F_UPDATE | LDD_F_NEED_INDEX))
1183 req->rq_delay_limit = MGC_TARGET_REG_LIMIT_MAX;
1185 rc = ptlrpc_queue_wait(req);
1187 rep_mti = req_capsule_server_get(&req->rq_pill,
1188 &RMF_MGS_TARGET_INFO);
1189 memcpy(mti, rep_mti, sizeof(*rep_mti));
1190 CDEBUG(D_MGC, "register %s got index = %d\n",
1191 mti->mti_svname, mti->mti_stripe_index);
1193 ptlrpc_req_finished(req);
1198 static int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1199 u32 keylen, void *key,
1200 u32 vallen, void *val,
1201 struct ptlrpc_request_set *set)
1206 /* Turn off initial_recov after we try all backup servers once */
1207 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1208 struct obd_import *imp = class_exp2cliimp(exp);
1210 if (vallen != sizeof(int))
1212 value = *(int *)val;
1213 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1214 imp->imp_obd->obd_name, value,
1215 imp->imp_deactive, imp->imp_invalid,
1216 imp->imp_replayable, imp->imp_obd->obd_replayable,
1217 ptlrpc_import_state_name(imp->imp_state));
1218 /* Resurrect the import immediately if
1219 * 1. we previously got disconnected,
1220 * 2. value > 1 (at the same node with MGS)
1222 if (imp->imp_state == LUSTRE_IMP_DISCON || value > 1)
1223 ptlrpc_reconnect_import(imp);
1228 /* FIXME move this to mgc_process_config */
1229 if (KEY_IS(KEY_REGISTER_TARGET)) {
1230 struct mgs_target_info *mti;
1231 if (vallen != sizeof(struct mgs_target_info))
1233 mti = (struct mgs_target_info *)val;
1234 CDEBUG(D_MGC, "register_target %s %#x\n",
1235 mti->mti_svname, mti->mti_flags);
1236 rc = mgc_target_register(exp, mti);
1239 if (KEY_IS(KEY_SET_FS)) {
1240 struct super_block *sb = (struct super_block *)val;
1242 if (vallen != sizeof(struct super_block))
1245 rc = mgc_fs_setup(env, exp->exp_obd, sb);
1248 if (KEY_IS(KEY_CLEAR_FS)) {
1251 rc = mgc_fs_cleanup(env, exp->exp_obd);
1254 if (KEY_IS(KEY_MGSSEC)) {
1255 struct client_obd *cli = &exp->exp_obd->u.cli;
1256 struct sptlrpc_flavor flvr;
1259 * empty string means using current flavor, if which haven't
1260 * been set yet, set it as null.
1262 * if flavor has been set previously, check the asking flavor
1263 * must match the existing one.
1266 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1272 rc = sptlrpc_parse_flavor(val, &flvr);
1274 CERROR("invalid sptlrpc flavor %s to MGS\n",
1280 * caller already hold a mutex
1282 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1283 cli->cl_flvr_mgc = flvr;
1284 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1285 sizeof(flvr)) != 0) {
1288 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1290 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1291 "currently %s is in use\n",
1301 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1302 __u32 keylen, void *key, __u32 *vallen, void *val)
1306 if (KEY_IS(KEY_CONN_DATA)) {
1307 struct obd_import *imp = class_exp2cliimp(exp);
1308 struct obd_connect_data *data = val;
1310 if (*vallen == sizeof(*data)) {
1311 *data = imp->imp_connect_data;
1319 static int mgc_import_event(struct obd_device *obd,
1320 struct obd_import *imp,
1321 enum obd_import_event event)
1325 LASSERT(imp->imp_obd == obd);
1326 CDEBUG(D_MGC, "import event %#x\n", event);
1329 case IMP_EVENT_DISCON:
1330 /* MGC imports should not wait for recovery */
1331 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1332 ptlrpc_pinger_ir_down();
1334 case IMP_EVENT_INACTIVE:
1336 case IMP_EVENT_INVALIDATE: {
1337 struct ldlm_namespace *ns = obd->obd_namespace;
1338 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1341 case IMP_EVENT_ACTIVE:
1342 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1343 /* Clearing obd_no_recov allows us to continue pinging */
1344 obd->obd_no_recov = 0;
1345 mgc_notify_active(obd);
1346 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1347 ptlrpc_pinger_ir_up();
1351 case IMP_EVENT_DEACTIVATE:
1352 case IMP_EVENT_ACTIVATE:
1355 CERROR("Unknown import event %#x\n", event);
1362 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_SHIFT),
1363 CONFIG_READ_NRPAGES = 4
1366 static int mgc_apply_recover_logs(struct obd_device *mgc,
1367 struct config_llog_data *cld,
1369 void *data, int datalen, bool mne_swab)
1371 struct config_llog_instance *cfg = &cld->cld_cfg;
1372 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1373 struct mgs_nidtbl_entry *entry;
1374 struct lustre_cfg *lcfg;
1375 struct lustre_cfg_bufs bufs;
1376 u64 prev_version = 0;
1377 char inst[MTI_NAME_MAXLEN + 1];
1383 unsigned long dynamic_nids;
1386 LASSERT(cfg->cfg_instance != 0);
1387 LASSERT(ll_get_cfg_instance(cfg->cfg_sb) == cfg->cfg_instance);
1389 /* get dynamic nids setting */
1390 dynamic_nids = mgc->obd_dynamic_nids;
1392 if (!IS_SERVER(lsi)) {
1393 pos = snprintf(inst, sizeof(inst), "%016lx", cfg->cfg_instance);
1394 if (pos >= PAGE_SIZE)
1396 #ifdef HAVE_SERVER_SUPPORT
1398 LASSERT(IS_MDT(lsi));
1399 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1403 #endif /* HAVE_SERVER_SUPPORT */
1406 OBD_ALLOC(buf, PAGE_SIZE);
1412 while (datalen > 0) {
1413 int entry_len = sizeof(*entry);
1415 struct obd_device *obd;
1416 struct obd_import *imp;
1423 if (datalen < sizeof(*entry))
1426 entry = (typeof(entry))(data + off);
1429 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1431 if (entry->mne_nid_count == 0) /* at least one nid entry */
1433 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1436 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1437 if (datalen < entry_len) /* must have entry_len at least */
1440 /* Keep this swab for normal mixed endian handling. LU-1644 */
1442 lustre_swab_mgs_nidtbl_entry(entry);
1443 if (entry->mne_length > PAGE_SIZE) {
1444 CERROR("MNE too large (%u)\n", entry->mne_length);
1448 if (entry->mne_length < entry_len)
1451 off += entry->mne_length;
1452 datalen -= entry->mne_length;
1456 if (entry->mne_version > max_version) {
1457 CERROR("entry index(%lld) is over max_index(%lld)\n",
1458 entry->mne_version, max_version);
1462 if (prev_version >= entry->mne_version) {
1463 CERROR("index unsorted, prev %lld, now %lld\n",
1464 prev_version, entry->mne_version);
1467 prev_version = entry->mne_version;
1470 * Write a string with format "nid::instance" to
1471 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1474 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1475 memset(buf, 0, bufsz);
1479 /* lustre-OST0001-osc-<instance #> */
1480 strcpy(obdname, cld->cld_logname);
1481 cname = strrchr(obdname, '-');
1482 if (cname == NULL) {
1483 CERROR("mgc %s: invalid logname %s\n",
1484 mgc->obd_name, obdname);
1488 pos = cname - obdname;
1490 pos += sprintf(obdname + pos, "-%s%04x",
1491 is_ost ? "OST" : "MDT", entry->mne_index);
1493 cname = is_ost ? "osc" : "mdc",
1494 pos += snprintf(obdname + pos, bufsz, "-%s-%s", cname, inst);
1495 lustre_cfg_bufs_reset(&bufs, obdname);
1497 /* find the obd by obdname */
1498 obd = class_name2obd(obdname);
1500 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1501 mgc->obd_name, obdname);
1503 /* this is a safe race, when the ost is starting up...*/
1507 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1510 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1513 with_imp_locked(obd, imp, rc) {
1514 /* iterate all nids to find one */
1515 /* find uuid by nid */
1516 /* create import entries if they don't exist */
1517 rc = client_import_add_nids_to_conn(
1518 imp, entry->u.nids, entry->mne_nid_count,
1519 (struct obd_uuid *)uuid);
1521 if (rc == -ENOENT && dynamic_nids) {
1522 /* create a new connection for this import */
1524 libcfs_nid2str(entry->u.nids[0]);
1525 int prim_nid_len = strlen(primary_nid) + 1;
1526 struct obd_uuid server_uuid;
1528 if (prim_nid_len > UUID_MAX)
1530 strncpy(server_uuid.uuid, primary_nid,
1533 CDEBUG(D_INFO, "Adding a connection for %s\n",
1536 rc = client_import_dyn_add_conn(
1537 imp, &server_uuid, entry->u.nids[0], 1);
1539 CERROR("%s: Failed to add new connection with NID '%s' to import: rc = %d\n",
1540 obd->obd_name, primary_nid, rc);
1543 rc = client_import_add_nids_to_conn(
1545 entry->mne_nid_count,
1546 (struct obd_uuid *)uuid);
1548 CERROR("%s: failed to lookup UUID: rc = %d\n",
1555 if (rc == -ENODEV) {
1556 /* client does not connect to the OST yet */
1561 if (rc < 0 && rc != -ENOSPC) {
1562 CERROR("mgc: cannot find UUID by nid '%s': rc = %d\n",
1563 libcfs_nid2str(entry->u.nids[0]), rc);
1567 CDEBUG(D_INFO, "Found UUID '%s' by NID '%s'\n",
1568 uuid, libcfs_nid2str(entry->u.nids[0]));
1570 pos += strlen(uuid);
1571 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1572 LASSERT(pos < bufsz);
1574 lustre_cfg_bufs_set_string(&bufs, 1, params);
1576 OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount,
1582 lustre_cfg_init(lcfg, LCFG_PARAM, &bufs);
1584 CDEBUG(D_INFO, "ir apply logs %lld/%lld for %s -> %s\n",
1585 prev_version, max_version, obdname, params);
1587 rc = class_process_config(lcfg);
1588 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
1589 lcfg->lcfg_buflens));
1591 CDEBUG(D_INFO, "process config for %s error %d\n",
1594 /* continue, even one with error */
1597 OBD_FREE(buf, PAGE_SIZE);
1603 * This function is called if this client was notified for target restarting
1604 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery or
1607 static int mgc_process_recover_nodemap_log(struct obd_device *obd,
1608 struct config_llog_data *cld)
1610 struct ptlrpc_connection *mgc_conn;
1611 struct ptlrpc_request *req = NULL;
1612 struct config_llog_instance *cfg = &cld->cld_cfg;
1613 struct mgs_config_body *body;
1614 struct mgs_config_res *res;
1615 struct nodemap_config *new_config = NULL;
1616 struct lu_nodemap *recent_nodemap = NULL;
1617 struct ptlrpc_bulk_desc *desc;
1618 struct page **pages = NULL;
1619 __u64 config_read_offset = 0;
1620 __u8 nodemap_cur_pass = 0;
1623 bool mne_swab = false;
1629 mgc_conn = class_exp2cliimp(cld->cld_mgcexp)->imp_connection;
1631 /* don't need to get local config */
1632 if (cld_is_nodemap(cld) && LNetIsPeerLocal(mgc_conn->c_peer.nid))
1635 /* allocate buffer for bulk transfer.
1636 * if this is the first time for this mgs to read logs,
1637 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1638 * once; otherwise, it only reads increment of logs, this should be
1639 * small and CONFIG_READ_NRPAGES will be used.
1641 nrpages = CONFIG_READ_NRPAGES;
1642 if (cfg->cfg_last_idx == 0 || cld_is_nodemap(cld))
1643 nrpages = CONFIG_READ_NRPAGES_INIT;
1645 OBD_ALLOC_PTR_ARRAY_LARGE(pages, nrpages);
1647 GOTO(out, rc = -ENOMEM);
1649 for (i = 0; i < nrpages; i++) {
1650 pages[i] = alloc_page(GFP_KERNEL);
1651 if (pages[i] == NULL)
1652 GOTO(out, rc = -ENOMEM);
1656 #ifdef HAVE_SERVER_SUPPORT
1657 if (cld_is_nodemap(cld) && config_read_offset == 0) {
1658 new_config = nodemap_config_alloc();
1659 if (IS_ERR(new_config)) {
1660 rc = PTR_ERR(new_config);
1666 LASSERT(cld_is_recover(cld) || cld_is_nodemap(cld));
1667 LASSERT(mutex_is_locked(&cld->cld_lock));
1668 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1669 &RQF_MGS_CONFIG_READ);
1671 GOTO(out, rc = -ENOMEM);
1673 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1678 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1679 LASSERT(body != NULL);
1680 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1681 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1682 >= sizeof(body->mcb_name))
1683 GOTO(out, rc = -E2BIG);
1684 if (cld_is_nodemap(cld))
1685 body->mcb_offset = config_read_offset;
1687 body->mcb_offset = cfg->cfg_last_idx + 1;
1688 body->mcb_type = cld->cld_type;
1689 body->mcb_bits = PAGE_SHIFT;
1690 body->mcb_units = nrpages;
1691 body->mcb_nm_cur_pass = nodemap_cur_pass;
1693 /* allocate bulk transfer descriptor */
1694 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1,
1695 PTLRPC_BULK_PUT_SINK,
1697 &ptlrpc_bulk_kiov_pin_ops);
1699 GOTO(out, rc = -ENOMEM);
1701 for (i = 0; i < nrpages; i++)
1702 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1705 ptlrpc_request_set_replen(req);
1706 rc = ptlrpc_queue_wait(req);
1710 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1712 GOTO(out, rc = -EPROTO);
1714 if (cld_is_nodemap(cld)) {
1715 config_read_offset = res->mcr_offset;
1716 eof = config_read_offset == II_END_OFF;
1717 nodemap_cur_pass = res->mcr_nm_cur_pass;
1719 if (res->mcr_size < res->mcr_offset)
1720 GOTO(out, rc = -EINVAL);
1722 /* always update the index even though it might have errors with
1723 * handling the recover logs
1725 cfg->cfg_last_idx = res->mcr_offset;
1726 eof = res->mcr_offset == res->mcr_size;
1728 CDEBUG(D_INFO, "Latest version %lld, more %d.\n",
1729 res->mcr_offset, eof == false);
1732 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1734 GOTO(out, rc = ealen);
1736 if (ealen > nrpages << PAGE_SHIFT)
1737 GOTO(out, rc = -EINVAL);
1739 if (ealen == 0) { /* no logs transferred */
1740 #ifdef HAVE_SERVER_SUPPORT
1741 /* config changed since first read RPC */
1742 if (cld_is_nodemap(cld) && config_read_offset == 0) {
1743 CDEBUG(D_INFO, "nodemap config changed in transit, retrying\n");
1744 GOTO(out, rc = -EAGAIN);
1752 mne_swab = req_capsule_rep_need_swab(&req->rq_pill);
1754 /* When a nodemap config is received, we build a new nodemap config,
1755 * with new nodemap structs. We keep track of the most recently added
1756 * nodemap since the config is read ordered by nodemap_id, and so it
1757 * is likely that the next record will be related. Because access to
1758 * the nodemaps is single threaded until the nodemap_config is active,
1759 * we don't need to reference count with recent_nodemap, though
1760 * recent_nodemap should be set to NULL when the nodemap_config
1761 * is either destroyed or set active.
1763 for (i = 0; i < nrpages && ealen > 0; i++) {
1767 ptr = kmap(pages[i]);
1768 if (cld_is_nodemap(cld))
1769 rc2 = nodemap_process_idx_pages(new_config, ptr,
1772 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset,
1779 CWARN("%s: error processing %s log %s: rc = %d\n",
1781 cld_is_nodemap(cld) ? "nodemap" : "recovery",
1784 GOTO(out, rc = rc2);
1792 ptlrpc_req_finished(req);
1796 if (rc == 0 && !eof)
1799 #ifdef HAVE_SERVER_SUPPORT
1800 if (new_config != NULL) {
1801 /* recent_nodemap cannot be used after set_active/dealloc */
1803 nodemap_config_set_active_mgc(new_config);
1805 nodemap_config_dealloc(new_config);
1810 for (i = 0; i < nrpages; i++) {
1811 if (pages[i] == NULL)
1813 __free_page(pages[i]);
1815 OBD_FREE_PTR_ARRAY_LARGE(pages, nrpages);
1820 static int mgc_barrier_glimpse_ast(struct ldlm_lock *lock, void *data)
1822 struct config_llog_data *cld = lock->l_ast_data;
1826 if (cld->cld_stopping)
1829 rc = barrier_handler(s2lsi(cld->cld_cfg.cfg_sb)->lsi_dt_dev,
1830 (struct ptlrpc_request *)data);
1835 /* Copy a remote log locally */
1836 static int mgc_llog_local_copy(const struct lu_env *env,
1837 struct obd_device *obd,
1838 struct llog_ctxt *rctxt,
1839 struct llog_ctxt *lctxt, char *logname)
1847 * - copy it to backup using llog_backup()
1848 * - copy remote llog to logname using llog_backup()
1849 * - if failed then move bakup to logname again
1852 OBD_ALLOC(temp_log, strlen(logname) + 2);
1855 sprintf(temp_log, "%sT", logname);
1857 /* make a copy of local llog at first */
1858 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1859 if (rc < 0 && rc != -ENOENT)
1861 /* copy remote llog to the local copy */
1862 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1863 if (rc == -ENOENT) {
1864 /* no remote llog, delete local one too */
1865 llog_erase(env, lctxt, NULL, logname);
1866 } else if (rc < 0) {
1867 /* error during backup, get local one back from the copy */
1868 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1870 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1871 obd->obd_name, logname, rc);
1873 llog_erase(env, lctxt, NULL, temp_log);
1874 OBD_FREE(temp_log, strlen(logname) + 2);
1878 /* local_only means it cannot get remote llogs */
1879 static int mgc_process_cfg_log(struct obd_device *mgc,
1880 struct config_llog_data *cld, int local_only)
1882 struct llog_ctxt *ctxt, *lctxt = NULL;
1883 struct client_obd *cli = &mgc->u.cli;
1884 struct lustre_sb_info *lsi = NULL;
1891 LASSERT(mutex_is_locked(&cld->cld_lock));
1893 if (cld->cld_cfg.cfg_sb)
1894 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1900 rc = lu_env_init(env, LCT_MG_THREAD);
1904 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1907 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1909 /* Copy the setup log locally if we can. Don't mess around if we're
1910 * running an MGS though (logs are already local). */
1911 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1912 cli->cl_mgc_configs_dir != NULL &&
1913 lu2dt_dev(cli->cl_mgc_configs_dir->do_lu.lo_dev) ==
1915 if (!local_only && !lsi->lsi_dt_dev->dd_rdonly) {
1916 /* Only try to copy log if we have the lock. */
1917 CDEBUG(D_INFO, "%s: copy local log %s\n",
1918 mgc->obd_name, cld->cld_logname);
1920 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1923 lsi->lsi_flags &= ~LDD_F_NO_LOCAL_LOGS;
1925 if (local_only || rc) {
1926 if (unlikely(lsi->lsi_flags & LDD_F_NO_LOCAL_LOGS)
1928 CWARN("%s: local log %s are not valid and/or remote logs are not accessbile rc = %d\n",
1929 mgc->obd_name, cld->cld_logname, rc);
1930 GOTO(out_pop, rc = -EIO);
1933 if (strcmp(cld->cld_logname, PARAMS_FILENAME) != 0 &&
1934 llog_is_empty(env, lctxt, cld->cld_logname)) {
1935 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS log %s and no local copy.\n",
1937 GOTO(out_pop, rc = -ENOENT);
1939 CDEBUG(D_MGC, "%s: Failed to get MGS log %s, using local copy for now, will try to update later.\n",
1940 mgc->obd_name, cld->cld_logname);
1943 /* Now, whether we copied or not, start using the local llog.
1944 * If we failed to copy, we'll start using whatever the old
1946 llog_ctxt_put(ctxt);
1950 if (local_only) /* no local log at client side */
1951 GOTO(out_pop, rc = -EIO);
1955 if (lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1956 lsi->lsi_dt_dev->dd_rdonly) {
1957 struct llog_ctxt *rctxt;
1959 /* Under readonly mode, we may have no local copy or local
1960 * copy is incomplete, so try to use remote llog firstly. */
1961 rctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1964 rc = class_config_parse_llog(env, rctxt, cld->cld_logname,
1966 llog_ctxt_put(rctxt);
1969 if (rc && rc != -ENOENT)
1970 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1974 * update settings on existing OBDs.
1975 * the logname must be <fsname>-sptlrpc
1977 if (rc == 0 && cld_is_sptlrpc(cld))
1978 class_notify_sptlrpc_conf(cld->cld_logname,
1979 strlen(cld->cld_logname) -
1980 strlen("-sptlrpc"));
1984 __llog_ctxt_put(env, ctxt);
1986 __llog_ctxt_put(env, lctxt);
1994 static bool mgc_import_in_recovery(struct obd_import *imp)
1996 bool in_recovery = true;
1998 spin_lock(&imp->imp_lock);
1999 if (imp->imp_state == LUSTRE_IMP_FULL ||
2000 imp->imp_state == LUSTRE_IMP_CLOSED)
2001 in_recovery = false;
2002 spin_unlock(&imp->imp_lock);
2008 * Get a configuration log from the MGS and process it.
2010 * This function is called for both clients and servers to process the
2011 * configuration log from the MGS. The MGC enqueues a DLM lock on the
2012 * log from the MGS, and if the lock gets revoked the MGC will be notified
2013 * by the lock cancellation callback that the config log has changed,
2014 * and will enqueue another MGS lock on it, and then continue processing
2015 * the new additions to the end of the log.
2017 * Since the MGC import is not replayable, if the import is being evicted
2018 * (rcl == -ESHUTDOWN, \see ptlrpc_import_delay_req()), retry to process
2019 * the log until recovery is finished or the import is closed.
2021 * Make a local copy of the log before parsing it if appropriate (non-MGS
2022 * server) so that the server can start even when the MGS is down.
2024 * There shouldn't be multiple processes running process_log at once --
2025 * sounds like badness. It actually might be fine, as long as they're not
2026 * trying to update from the same log simultaneously, in which case we
2027 * should use a per-log semaphore instead of cld_lock.
2029 * \param[in] mgc MGC device by which to fetch the configuration log
2030 * \param[in] cld log processing state (stored in lock callback data)
2032 * \retval 0 on success
2033 * \retval negative errno on failure
2035 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
2037 struct lustre_handle lockh = { 0 };
2038 __u64 flags = LDLM_FL_NO_LRU;
2043 LASSERT(cld != NULL);
2045 /* I don't want multiple processes running process_log at once --
2046 sounds like badness. It actually might be fine, as long as
2047 we're not trying to update from the same log
2048 simultaneously (in which case we should use a per-log sem.) */
2050 mutex_lock(&cld->cld_lock);
2051 if (cld->cld_stopping) {
2052 mutex_unlock(&cld->cld_lock);
2056 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
2058 CDEBUG(D_MGC, "Process log %s-%016lx from %d\n", cld->cld_logname,
2059 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
2061 /* Get the cfg lock on the llog */
2062 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, LDLM_PLAIN, NULL,
2064 cld_is_barrier(cld) ? mgc_barrier_glimpse_ast : NULL,
2065 cld, 0, NULL, &lockh);
2067 /* Get the cld, it will be released in mgc_blocking_ast. */
2068 config_log_get(cld);
2069 rc = ldlm_lock_set_data(&lockh, (void *)cld);
2070 LASSERT(!lustre_handle_is_used(&cld->cld_lockh));
2072 cld->cld_lockh = lockh;
2074 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
2075 cld->cld_lockh.cookie = 0;
2077 if (rcl == -ESHUTDOWN &&
2078 atomic_read(&mgc->u.cli.cl_mgc_refcount) > 0 && !retry) {
2079 struct obd_import *imp;
2080 long timeout = cfs_time_seconds(obd_timeout);
2082 mutex_unlock(&cld->cld_lock);
2083 imp = class_exp2cliimp(mgc->u.cli.cl_mgc_mgsexp);
2085 /* Let's force the pinger, and wait the import to be
2086 * connected, note: since mgc import is non-replayable,
2087 * and even the import state is disconnected, it does
2088 * not mean the "recovery" is stopped, so we will keep
2089 * waitting until timeout or the import state is
2091 ptlrpc_pinger_force(imp);
2093 wait_event_idle_timeout(imp->imp_recovery_waitq,
2094 !mgc_import_in_recovery(imp),
2097 if (imp->imp_state == LUSTRE_IMP_FULL) {
2101 mutex_lock(&cld->cld_lock);
2102 /* unlock/lock mutex, so check stopping again */
2103 if (cld->cld_stopping) {
2104 mutex_unlock(&cld->cld_lock);
2107 spin_lock(&config_list_lock);
2108 cld->cld_lostlock = 1;
2109 spin_unlock(&config_list_lock);
2112 /* mark cld_lostlock so that it will requeue
2113 * after MGC becomes available. */
2114 spin_lock(&config_list_lock);
2115 cld->cld_lostlock = 1;
2116 spin_unlock(&config_list_lock);
2120 if (cld_is_recover(cld) || cld_is_nodemap(cld)) {
2122 rc = mgc_process_recover_nodemap_log(mgc, cld);
2123 else if (cld_is_nodemap(cld))
2126 } else if (!cld_is_barrier(cld)) {
2127 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
2130 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
2131 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
2133 /* Now drop the lock so MGS can revoke it */
2135 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, LCK_CR, &lockh);
2137 CERROR("Can't drop cfg lock: %d\n", rcl);
2139 mutex_unlock(&cld->cld_lock);
2141 /* requeue nodemap lock immediately if transfer was interrupted */
2142 if ((cld_is_nodemap(cld) && rc == -EAGAIN) ||
2143 (cld_is_recover(cld) && rc)) {
2144 if (cld_is_recover(cld))
2145 CWARN("%s: IR log %s failed, not fatal: rc = %d\n",
2146 mgc->obd_name, cld->cld_logname, rc);
2147 mgc_requeue_add(cld);
2155 /** Called from lustre_process_log.
2156 * LCFG_LOG_START gets the config log from the MGS, processes it to start
2157 * any services, and adds it to the list logs to watch (follow).
2159 static int mgc_process_config(struct obd_device *obd, size_t len, void *buf)
2161 struct lustre_cfg *lcfg = buf;
2162 struct config_llog_instance *cfg = NULL;
2167 switch(lcfg->lcfg_command) {
2168 case LCFG_LOV_ADD_OBD: {
2169 /* Overloading this cfg command: register a new target */
2170 struct mgs_target_info *mti;
2172 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
2173 sizeof(struct mgs_target_info))
2174 GOTO(out, rc = -EINVAL);
2176 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
2177 CDEBUG(D_MGC, "add_target %s %#x\n",
2178 mti->mti_svname, mti->mti_flags);
2179 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
2182 case LCFG_LOV_DEL_OBD:
2183 /* Unregister has no meaning at the moment. */
2184 CERROR("lov_del_obd unimplemented\n");
2187 case LCFG_SPTLRPC_CONF: {
2188 rc = sptlrpc_process_config(lcfg);
2191 case LCFG_LOG_START: {
2192 struct config_llog_data *cld;
2193 struct super_block *sb;
2195 logname = lustre_cfg_string(lcfg, 1);
2196 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
2197 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
2199 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
2202 /* We're only called through here on the initial mount */
2203 cld = config_log_add(obd, logname, cfg, sb);
2209 rc = mgc_process_log(obd, cld);
2210 if (rc == 0 && cld->cld_recover != NULL) {
2211 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
2212 imp_connect_data, IMP_RECOV)) {
2213 rc = mgc_process_log(obd, cld->cld_recover);
2215 struct config_llog_data *cir;
2217 mutex_lock(&cld->cld_lock);
2218 cir = cld->cld_recover;
2219 cld->cld_recover = NULL;
2220 mutex_unlock(&cld->cld_lock);
2221 config_log_put(cir);
2225 CERROR("Cannot process recover llog %d\n", rc);
2228 if (rc == 0 && cld->cld_params != NULL) {
2229 rc = mgc_process_log(obd, cld->cld_params);
2230 if (rc == -ENOENT) {
2231 CDEBUG(D_MGC, "There is no params "
2232 "config file yet\n");
2235 /* params log is optional */
2237 CERROR("%s: can't process params llog: rc = %d\n",
2243 case LCFG_LOG_END: {
2244 logname = lustre_cfg_string(lcfg, 1);
2246 if (lcfg->lcfg_bufcount >= 2)
2247 cfg = (struct config_llog_instance *)lustre_cfg_buf(
2249 rc = config_log_end(logname, cfg);
2253 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
2254 GOTO(out, rc = -EINVAL);
2262 static const struct obd_ops mgc_obd_ops = {
2263 .o_owner = THIS_MODULE,
2264 .o_setup = mgc_setup,
2265 .o_precleanup = mgc_precleanup,
2266 .o_cleanup = mgc_cleanup,
2267 .o_add_conn = client_import_add_conn,
2268 .o_del_conn = client_import_del_conn,
2269 .o_connect = client_connect_import,
2270 .o_disconnect = client_disconnect_export,
2271 .o_set_info_async = mgc_set_info_async,
2272 .o_get_info = mgc_get_info,
2273 .o_import_event = mgc_import_event,
2274 .o_process_config = mgc_process_config,
2277 static int mgc_param_requeue_timeout_min_set(const char *val,
2278 cfs_kernel_param_arg_t *kp)
2283 rc = kstrtouint(val, 0, &num);
2289 mgc_requeue_timeout_min = num;
2294 static const struct kernel_param_ops param_ops_requeue_timeout_min = {
2295 .set = mgc_param_requeue_timeout_min_set,
2296 .get = param_get_uint,
2299 #define param_check_requeue_timeout_min(name, p) \
2300 __param_check(name, p, unsigned int)
2302 unsigned int mgc_requeue_timeout_min = MGC_TIMEOUT_MIN_SECONDS;
2303 #ifdef HAVE_KERNEL_PARAM_OPS
2304 module_param(mgc_requeue_timeout_min, requeue_timeout_min, 0644);
2306 module_param_call(mgc_requeue_timeout_min, mgc_param_requeue_timeout_min_set,
2307 param_get_uint, ¶m_ops_requeue_timeout_min, 0644);
2309 MODULE_PARM_DESC(mgc_requeue_timeout_min, "Minimal requeue time to refresh logs");
2311 static int __init mgc_init(void)
2313 return class_register_type(&mgc_obd_ops, NULL, false,
2314 LUSTRE_MGC_NAME, NULL);
2317 static void __exit mgc_exit(void)
2319 class_unregister_type(LUSTRE_MGC_NAME);
2322 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2323 MODULE_DESCRIPTION("Lustre Management Client");
2324 MODULE_VERSION(LUSTRE_VERSION_STRING);
2325 MODULE_LICENSE("GPL");
2327 module_init(mgc_init);
2328 module_exit(mgc_exit);