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, 2016, 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>
52 #include "mgc_internal.h"
54 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id,
59 if (len > sizeof(resname)) {
60 CERROR("name too long: %s\n", name);
64 CERROR("missing name: %s\n", name);
67 memcpy(&resname, name, len);
69 /* Always use the same endianness for the resid */
70 memset(res_id, 0, sizeof(*res_id));
71 res_id->name[0] = cpu_to_le64(resname);
72 /* XXX: unfortunately, sptlprc and config llog share one lock */
75 case CONFIG_T_SPTLRPC:
78 case CONFIG_T_RECOVER:
80 case CONFIG_T_NODEMAP:
81 case CONFIG_T_BARRIER:
87 res_id->name[1] = cpu_to_le64(resname);
88 CDEBUG(D_MGC, "log %s to resid %#llx/%#llx (%.8s)\n", name,
89 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
93 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id, int type)
95 /* fsname is at most 8 chars long, maybe contain "-".
96 * e.g. "lustre", "SUN-000" */
97 return mgc_name2resid(fsname, strlen(fsname), res_id, type);
99 EXPORT_SYMBOL(mgc_fsname2resid);
101 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id, int type)
106 /* logname consists of "fsname-nodetype".
107 * e.g. "lustre-MDT0001", "SUN-000-client"
108 * there is an exception: llog "params" */
109 name_end = strrchr(logname, '-');
111 len = strlen(logname);
113 len = name_end - logname;
114 return mgc_name2resid(logname, len, res_id, type);
116 EXPORT_SYMBOL(mgc_logname2resid);
118 /********************** config llog list **********************/
119 static struct list_head config_llog_list = LIST_HEAD_INIT(config_llog_list);
120 static DEFINE_SPINLOCK(config_list_lock);
122 /* Take a reference to a config log */
123 static int config_log_get(struct config_llog_data *cld)
126 atomic_inc(&cld->cld_refcount);
127 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
128 atomic_read(&cld->cld_refcount));
132 /* Drop a reference to a config log. When no longer referenced,
133 we can free the config log data */
134 static void config_log_put(struct config_llog_data *cld)
141 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
142 atomic_read(&cld->cld_refcount));
143 LASSERT(atomic_read(&cld->cld_refcount) > 0);
145 /* spinlock to make sure no item with 0 refcount in the list */
146 if (atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
147 list_del(&cld->cld_list_chain);
148 spin_unlock(&config_list_lock);
150 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
152 if (cld->cld_barrier)
153 config_log_put(cld->cld_barrier);
154 if (cld->cld_recover)
155 config_log_put(cld->cld_recover);
157 config_log_put(cld->cld_params);
158 if (cld->cld_nodemap)
159 config_log_put(cld->cld_nodemap);
160 if (cld->cld_sptlrpc)
161 config_log_put(cld->cld_sptlrpc);
162 if (cld_is_sptlrpc(cld))
163 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;
182 LASSERT(logname != NULL);
184 instance = cfg ? cfg->cfg_instance : NULL;
185 spin_lock(&config_list_lock);
186 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
187 /* check if instance equals */
188 if (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,
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:%p\n", logname,
215 cfg ? cfg->cfg_instance : NULL);
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);
244 cld->cld_cfg.cfg_obdname = obd->obd_name;
247 spin_lock(&config_list_lock);
248 list_add(&cld->cld_list_chain, &config_llog_list);
249 spin_unlock(&config_list_lock);
251 if (cld_is_sptlrpc(cld) || cld_is_nodemap(cld) || cld_is_barrier(cld)) {
252 rc = mgc_process_log(obd, cld);
253 if (rc && rc != -ENOENT)
254 CERROR("%s: failed processing log, type %d: rc = %d\n",
255 obd->obd_name, type, rc);
261 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
263 struct config_llog_instance *cfg,
264 struct super_block *sb)
266 struct config_llog_instance lcfg = *cfg;
267 struct lustre_sb_info *lsi = s2lsi(sb);
268 struct config_llog_data *cld;
274 /* for osp-on-ost, see lustre_start_osp() */
275 if (IS_MDT(lsi) && lcfg.cfg_instance)
278 /* we have to use different llog for clients and mdts for cmd
279 * where only clients are notified if one of cmd server restarts */
280 LASSERT(strlen(fsname) < sizeof(logname) / 2);
281 strcpy(logname, fsname);
282 if (IS_SERVER(lsi)) { /* mdt */
283 LASSERT(lcfg.cfg_instance == NULL);
284 lcfg.cfg_instance = sb;
285 strcat(logname, "-mdtir");
287 LASSERT(lcfg.cfg_instance != NULL);
288 strcat(logname, "-cliir");
291 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
295 static struct config_llog_data *config_log_find_or_add(struct obd_device *obd,
296 char *logname, struct super_block *sb, int type,
297 struct config_llog_instance *cfg)
299 struct config_llog_instance lcfg = *cfg;
300 struct config_llog_data *cld;
302 lcfg.cfg_instance = sb != NULL ? (void *)sb : (void *)obd;
303 cld = config_log_find(logname, &lcfg);
304 if (unlikely(cld != NULL))
307 return do_config_log_add(obd, logname, type, &lcfg, sb);
310 /** Add this log to the list of active logs watched by an MGC.
311 * Active means we're watching for updates.
312 * We have one active log per "mount" - client instance or servername.
313 * Each instance may be at a different point in the log.
315 static struct config_llog_data *
316 config_log_add(struct obd_device *obd, char *logname,
317 struct config_llog_instance *cfg, struct super_block *sb)
319 struct lustre_sb_info *lsi = s2lsi(sb);
320 struct config_llog_data *cld = NULL;
321 struct config_llog_data *sptlrpc_cld = NULL;
322 struct config_llog_data *params_cld = NULL;
323 struct config_llog_data *nodemap_cld = NULL;
324 struct config_llog_data *barrier_cld = NULL;
331 CDEBUG(D_MGC, "adding config log %s:%p\n", logname, cfg->cfg_instance);
334 * for each regular log, the depended sptlrpc log name is
335 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
337 ptr = strrchr(logname, '-');
338 if (ptr == NULL || ptr - logname > 8) {
339 CERROR("logname %s is too long\n", logname);
340 RETURN(ERR_PTR(-EINVAL));
343 memcpy(seclogname, logname, ptr - logname);
344 strcpy(seclogname + (ptr - logname), "-sptlrpc");
346 if (cfg->cfg_sub_clds & CONFIG_T_SPTLRPC) {
347 sptlrpc_cld = config_log_find_or_add(obd, seclogname, NULL,
348 CONFIG_T_SPTLRPC, cfg);
349 if (IS_ERR(sptlrpc_cld)) {
350 CERROR("%s: can't create sptlrpc log %s: rc = %ld\n",
351 obd->obd_name, seclogname, PTR_ERR(sptlrpc_cld));
356 if (!IS_MGS(lsi) && cfg->cfg_sub_clds & CONFIG_T_NODEMAP) {
357 nodemap_cld = config_log_find_or_add(obd, LUSTRE_NODEMAP_NAME,
358 NULL, CONFIG_T_NODEMAP,
360 if (IS_ERR(nodemap_cld)) {
361 rc = PTR_ERR(nodemap_cld);
362 CERROR("%s: cannot create nodemap log: rc = %d\n",
364 GOTO(out_sptlrpc, rc);
368 if (cfg->cfg_sub_clds & CONFIG_T_PARAMS) {
369 params_cld = config_log_find_or_add(obd, PARAMS_FILENAME, sb,
370 CONFIG_T_PARAMS, cfg);
371 if (IS_ERR(params_cld)) {
372 rc = PTR_ERR(params_cld);
373 CERROR("%s: can't create params log: rc = %d\n",
375 GOTO(out_nodemap, rc);
379 if (IS_MDT(s2lsi(sb))) {
380 snprintf(seclogname + (ptr - logname), sizeof(seclogname) - 1,
381 "-%s", BARRIER_FILENAME);
382 barrier_cld = config_log_find_or_add(obd, seclogname, sb,
383 CONFIG_T_BARRIER, cfg);
384 if (IS_ERR(barrier_cld)) {
385 rc = PTR_ERR(barrier_cld);
386 CERROR("%s: can't create barrier log: rc = %d\n",
388 GOTO(out_params, rc);
392 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
395 CERROR("%s: can't create log: rc = %d\n",
397 GOTO(out_barrier, rc = PTR_ERR(cld));
400 LASSERT(lsi->lsi_lmd);
401 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR) &&
402 cfg->cfg_sub_clds & CONFIG_T_RECOVER) {
403 struct config_llog_data *recover_cld;
405 ptr = strrchr(seclogname, '-');
409 CERROR("%s: sptlrpc log name not correct, %s: "
410 "rc = %d\n", obd->obd_name, seclogname, -EINVAL);
411 GOTO(out_cld, rc = -EINVAL);
414 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
415 if (IS_ERR(recover_cld)) {
416 rc = PTR_ERR(recover_cld);
417 CERROR("%s: can't create recover log: rc = %d\n",
422 mutex_lock(&cld->cld_lock);
424 cld->cld_recover = recover_cld;
428 mutex_lock(&cld->cld_lock);
429 cld->cld_params = params_cld;
430 cld->cld_barrier = barrier_cld;
431 cld->cld_nodemap = nodemap_cld;
432 cld->cld_sptlrpc = sptlrpc_cld;
433 mutex_unlock(&cld->cld_lock);
440 config_log_put(barrier_cld);
442 config_log_put(params_cld);
444 config_log_put(nodemap_cld);
446 config_log_put(sptlrpc_cld);
451 DEFINE_MUTEX(llog_process_lock);
453 static inline void config_mark_cld_stop(struct config_llog_data *cld)
455 mutex_lock(&cld->cld_lock);
456 spin_lock(&config_list_lock);
457 cld->cld_stopping = 1;
458 spin_unlock(&config_list_lock);
459 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);
512 config_mark_cld_stop(cld_recover);
513 config_log_put(cld_recover);
517 config_mark_cld_stop(cld_params);
518 config_log_put(cld_params);
521 /* don't set cld_stopping on nm lock as other targets may be active */
523 config_log_put(cld_nodemap);
526 mutex_lock(&cld_barrier->cld_lock);
527 cld_barrier->cld_stopping = 1;
528 mutex_unlock(&cld_barrier->cld_lock);
529 config_log_put(cld_barrier);
533 config_log_put(cld_sptlrpc);
535 /* drop the ref from the find */
537 /* drop the start ref */
540 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
545 #ifdef CONFIG_PROC_FS
546 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
548 struct obd_device *obd = data;
549 struct obd_import *imp;
550 struct obd_connect_data *ocd;
551 struct config_llog_data *cld;
554 LASSERT(obd != NULL);
555 LPROCFS_CLIMP_CHECK(obd);
556 imp = obd->u.cli.cl_import;
557 ocd = &imp->imp_connect_data;
559 seq_printf(m, "imperative_recovery: %s\n",
560 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
561 seq_printf(m, "client_state:\n");
563 spin_lock(&config_list_lock);
564 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
565 if (cld->cld_recover == NULL)
567 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
569 cld->cld_recover->cld_cfg.cfg_last_idx);
571 spin_unlock(&config_list_lock);
573 LPROCFS_CLIMP_EXIT(obd);
578 /* reenqueue any lost locks */
579 #define RQ_RUNNING 0x1
583 #define RQ_PRECLEANUP 0x10
584 static int rq_state = 0;
585 static wait_queue_head_t rq_waitq;
586 static DECLARE_COMPLETION(rq_exit);
587 static DECLARE_COMPLETION(rq_start);
589 static void do_requeue(struct config_llog_data *cld)
594 LASSERT(atomic_read(&cld->cld_refcount) > 0);
597 * Do not run mgc_process_log on a disconnected export or an
598 * export which is being disconnected. Take the client
599 * semaphore to make the check non-racy.
601 down_read_nested(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem,
603 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
604 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
605 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
606 if (rc && rc != -ENOENT)
607 CERROR("failed processing log: %d\n", rc);
609 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
612 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
617 /* this timeout represents how many seconds MGC should wait before
618 * requeue config and recover lock to the MGS. We need to randomize this
619 * in order to not flood the MGS.
621 #define MGC_TIMEOUT_MIN_SECONDS 5
622 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
624 static int mgc_requeue_thread(void *data)
630 CDEBUG(D_MGC, "Starting requeue thread\n");
632 /* Keep trying failed locks periodically */
633 spin_lock(&config_list_lock);
634 rq_state |= RQ_RUNNING;
635 while (!(rq_state & RQ_STOP)) {
636 struct l_wait_info lwi;
637 struct config_llog_data *cld, *cld_prev;
638 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
641 /* Any new or requeued lostlocks will change the state */
642 rq_state &= ~(RQ_NOW | RQ_LATER);
643 spin_unlock(&config_list_lock);
650 /* Always wait a few seconds to allow the server who
651 caused the lock revocation to finish its setup, plus some
652 random so everyone doesn't try to reconnect at once. */
653 to = msecs_to_jiffies(MGC_TIMEOUT_MIN_SECONDS * MSEC_PER_SEC);
654 /* rand is centi-seconds */
655 to += msecs_to_jiffies(rand * MSEC_PER_SEC / 100);
656 lwi = LWI_TIMEOUT(to, NULL, NULL);
657 l_wait_event(rq_waitq, rq_state & (RQ_STOP | RQ_PRECLEANUP),
661 * iterate & processing through the list. for each cld, process
662 * its depending sptlrpc cld firstly (if any) and then itself.
664 * it's guaranteed any item in the list must have
665 * reference > 0; and if cld_lostlock is set, at
666 * least one reference is taken by the previous enqueue.
670 spin_lock(&config_list_lock);
671 rq_state &= ~RQ_PRECLEANUP;
672 list_for_each_entry(cld, &config_llog_list,
674 if (!cld->cld_lostlock || cld->cld_stopping)
677 /* hold reference to avoid being freed during
678 * subsequent processing. */
680 cld->cld_lostlock = 0;
681 spin_unlock(&config_list_lock);
684 config_log_put(cld_prev);
687 if (likely(!(rq_state & RQ_STOP))) {
689 spin_lock(&config_list_lock);
691 spin_lock(&config_list_lock);
695 spin_unlock(&config_list_lock);
697 config_log_put(cld_prev);
699 /* Wait a bit to see if anyone else needs a requeue */
700 lwi = (struct l_wait_info) { 0 };
701 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
703 spin_lock(&config_list_lock);
706 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
707 rq_state &= ~RQ_RUNNING;
708 spin_unlock(&config_list_lock);
712 CDEBUG(D_MGC, "Ending requeue thread\n");
716 /* Add a cld to the list to requeue. Start the requeue thread if needed.
717 We are responsible for dropping the config log reference from here on out. */
718 static void mgc_requeue_add(struct config_llog_data *cld)
723 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
724 cld->cld_logname, atomic_read(&cld->cld_refcount),
725 cld->cld_stopping, rq_state);
726 LASSERT(atomic_read(&cld->cld_refcount) > 0);
728 mutex_lock(&cld->cld_lock);
729 spin_lock(&config_list_lock);
730 if (!(rq_state & RQ_STOP) && !cld->cld_stopping && !cld->cld_lostlock) {
731 cld->cld_lostlock = 1;
735 spin_unlock(&config_list_lock);
736 mutex_unlock(&cld->cld_lock);
743 /********************** class fns **********************/
744 static int mgc_local_llog_init(const struct lu_env *env,
745 struct obd_device *obd,
746 struct obd_device *disk)
748 struct llog_ctxt *ctxt;
753 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
758 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
760 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
766 static int mgc_local_llog_fini(const struct lu_env *env,
767 struct obd_device *obd)
769 struct llog_ctxt *ctxt;
773 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
774 llog_cleanup(env, ctxt);
779 static int mgc_fs_setup(const struct lu_env *env, struct obd_device *obd,
780 struct super_block *sb)
782 struct lustre_sb_info *lsi = s2lsi(sb);
783 struct client_obd *cli = &obd->u.cli;
784 struct lu_fid rfid, fid;
785 struct dt_object *root, *dto;
791 LASSERT(lsi->lsi_dt_dev);
793 /* The mgc fs exclusion mutex. Only one fs can be setup at a time. */
794 mutex_lock(&cli->cl_mgc_mutex);
796 /* Setup the configs dir */
797 fid.f_seq = FID_SEQ_LOCAL_NAME;
800 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
805 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
809 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
810 &cli->cl_mgc_los->los_dev->dd_lu_dev, NULL);
811 if (unlikely(IS_ERR(root)))
812 GOTO(out_los, rc = PTR_ERR(root));
814 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
816 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
817 lu_object_put_nocache(env, &root->do_lu);
819 GOTO(out_los, rc = PTR_ERR(dto));
821 cli->cl_mgc_configs_dir = dto;
823 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
824 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
828 /* We take an obd ref to insure that we can't get to mgc_cleanup
829 * without calling mgc_fs_cleanup first. */
830 class_incref(obd, "mgc_fs", obd);
832 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
836 lu_object_put(env, &cli->cl_mgc_configs_dir->do_lu);
837 cli->cl_mgc_configs_dir = NULL;
841 local_oid_storage_fini(env, cli->cl_mgc_los);
842 cli->cl_mgc_los = NULL;
843 mutex_unlock(&cli->cl_mgc_mutex);
848 static int mgc_fs_cleanup(const struct lu_env *env, struct obd_device *obd)
850 struct client_obd *cli = &obd->u.cli;
853 LASSERT(cli->cl_mgc_los != NULL);
855 mgc_local_llog_fini(env, obd);
857 lu_object_put_nocache(env, &cli->cl_mgc_configs_dir->do_lu);
858 cli->cl_mgc_configs_dir = NULL;
860 local_oid_storage_fini(env, cli->cl_mgc_los);
861 cli->cl_mgc_los = NULL;
863 class_decref(obd, "mgc_fs", obd);
864 mutex_unlock(&cli->cl_mgc_mutex);
869 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
871 struct llog_ctxt *ctxt;
876 /* setup only remote ctxt, the local disk context is switched per each
877 * filesystem during mgc_fs_setup() */
878 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
883 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
886 llog_initiator_connect(ctxt);
892 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
894 struct llog_ctxt *ctxt;
898 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
900 llog_cleanup(env, ctxt);
906 static atomic_t mgc_count = ATOMIC_INIT(0);
907 static int mgc_precleanup(struct obd_device *obd)
913 if (atomic_dec_and_test(&mgc_count)) {
914 LASSERT(rq_state & RQ_RUNNING);
915 /* stop requeue thread */
918 /* wakeup requeue thread to clean our cld */
919 temp = RQ_NOW | RQ_PRECLEANUP;
922 spin_lock(&config_list_lock);
924 spin_unlock(&config_list_lock);
928 wait_for_completion(&rq_exit);
929 obd_cleanup_client_import(obd);
931 rc = mgc_llog_fini(NULL, obd);
933 CERROR("failed to cleanup llogging subsystems\n");
938 static int mgc_cleanup(struct obd_device *obd)
943 /* COMPAT_146 - old config logs may have added profiles we don't
945 if (obd->obd_type->typ_refcnt <= 1)
946 /* Only for the last mgc */
947 class_del_profiles();
949 lprocfs_obd_cleanup(obd);
952 rc = client_obd_cleanup(obd);
956 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
958 struct task_struct *task;
962 rc = ptlrpcd_addref();
966 rc = client_obd_setup(obd, lcfg);
968 GOTO(err_decref, rc);
970 rc = mgc_llog_init(NULL, obd);
972 CERROR("failed to setup llogging subsystems\n");
973 GOTO(err_cleanup, rc);
976 #ifdef CONFIG_PROC_FS
977 obd->obd_vars = lprocfs_mgc_obd_vars;
978 lprocfs_obd_setup(obd);
980 sptlrpc_lprocfs_cliobd_attach(obd);
982 if (atomic_inc_return(&mgc_count) == 1) {
984 init_waitqueue_head(&rq_waitq);
986 /* start requeue thread */
987 task = kthread_run(mgc_requeue_thread, NULL, "ll_cfg_requeue");
990 CERROR("%s: cannot start requeue thread: rc = %d; "
991 "no more log updates\n",
993 GOTO(err_cleanup, rc);
995 /* rc is the task_struct pointer of mgc_requeue_thread. */
997 wait_for_completion(&rq_start);
1003 client_obd_cleanup(obd);
1009 /* based on ll_mdc_blocking_ast */
1010 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
1011 void *data, int flag)
1013 struct lustre_handle lockh;
1014 struct config_llog_data *cld = (struct config_llog_data *)data;
1019 case LDLM_CB_BLOCKING:
1020 /* mgs wants the lock, give it up... */
1021 LDLM_DEBUG(lock, "MGC blocking CB");
1022 ldlm_lock2handle(lock, &lockh);
1023 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
1025 case LDLM_CB_CANCELING:
1026 /* We've given up the lock, prepare ourselves to update. */
1027 LDLM_DEBUG(lock, "MGC cancel CB");
1029 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
1030 PLDLMRES(lock->l_resource),
1031 (char *)&lock->l_resource->lr_name.name[0]);
1034 CDEBUG(D_INFO, "missing data, won't requeue\n");
1038 /* held at mgc_process_log(). */
1039 LASSERT(atomic_read(&cld->cld_refcount) > 0);
1041 lock->l_ast_data = NULL;
1042 /* Are we done with this log? */
1043 if (cld->cld_stopping) {
1044 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
1046 config_log_put(cld);
1049 /* Make sure not to re-enqueue when the mgc is stopping
1050 (we get called from client_disconnect_export) */
1051 if (lock->l_conn_export == NULL ||
1052 lock->l_conn_export->exp_obd->u.cli.cl_conn_count == 0) {
1053 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
1055 config_log_put(cld);
1059 /* Re-enqueue now */
1060 mgc_requeue_add(cld);
1061 config_log_put(cld);
1070 /* Not sure where this should go... */
1071 /* This is the timeout value for MGS_CONNECT request plus a ping interval, such
1072 * that we can have a chance to try the secondary MGS if any. */
1073 #define MGC_ENQUEUE_LIMIT (INITIAL_CONNECT_TIMEOUT + (AT_OFF ? 0 : at_min) \
1075 #define MGC_TARGET_REG_LIMIT 10
1076 #define MGC_SEND_PARAM_LIMIT 10
1078 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
1079 /* Send parameter to MGS*/
1080 static int mgc_set_mgs_param(struct obd_export *exp,
1081 struct mgs_send_param *msp)
1083 struct ptlrpc_request *req;
1084 struct mgs_send_param *req_msp, *rep_msp;
1088 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1089 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
1094 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1096 ptlrpc_req_finished(req);
1100 memcpy(req_msp, msp, sizeof(*req_msp));
1101 ptlrpc_request_set_replen(req);
1103 /* Limit how long we will wait for the enqueue to complete */
1104 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
1105 rc = ptlrpc_queue_wait(req);
1107 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1108 memcpy(msp, rep_msp, sizeof(*rep_msp));
1111 ptlrpc_req_finished(req);
1117 /* Take a config lock so we can get cancel notifications */
1118 static int mgc_enqueue(struct obd_export *exp, enum ldlm_type type,
1119 union ldlm_policy_data *policy, enum ldlm_mode mode,
1120 __u64 *flags, ldlm_glimpse_callback glimpse_callback,
1121 void *data, __u32 lvb_len, void *lvb_swabber,
1122 struct lustre_handle *lockh)
1124 struct config_llog_data *cld = (struct config_llog_data *)data;
1125 struct ldlm_enqueue_info einfo = {
1128 .ei_cb_bl = mgc_blocking_ast,
1129 .ei_cb_cp = ldlm_completion_ast,
1130 .ei_cb_gl = glimpse_callback,
1132 struct ptlrpc_request *req;
1133 int short_limit = cld_is_sptlrpc(cld);
1137 CDEBUG(D_MGC, "Enqueue for %s (res %#llx)\n", cld->cld_logname,
1138 cld->cld_resid.name[0]);
1140 /* We need a callback for every lockholder, so don't try to
1141 ldlm_lock_match (see rev 1.1.2.11.2.47) */
1142 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1143 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1148 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1149 ptlrpc_request_set_replen(req);
1151 /* check if this is server or client */
1152 if (cld->cld_cfg.cfg_sb) {
1153 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
1154 if (lsi && IS_SERVER(lsi))
1157 /* Limit how long we will wait for the enqueue to complete */
1158 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
1159 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1160 NULL, 0, LVB_T_NONE, lockh, 0);
1161 /* A failed enqueue should still call the mgc_blocking_ast,
1162 where it will be requeued if needed ("grant failed"). */
1163 ptlrpc_req_finished(req);
1167 static int mgc_cancel(struct obd_export *exp, enum ldlm_mode mode,
1168 struct lustre_handle *lockh)
1172 ldlm_lock_decref(lockh, mode);
1177 static void mgc_notify_active(struct obd_device *unused)
1179 /* wakeup mgc_requeue_thread to requeue mgc lock */
1180 spin_lock(&config_list_lock);
1182 spin_unlock(&config_list_lock);
1185 /* TODO: Help the MGS rebuild nidtbl. -jay */
1188 /* Send target_reg message to MGS */
1189 static int mgc_target_register(struct obd_export *exp,
1190 struct mgs_target_info *mti)
1192 struct ptlrpc_request *req;
1193 struct mgs_target_info *req_mti, *rep_mti;
1197 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1198 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1203 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1205 ptlrpc_req_finished(req);
1209 memcpy(req_mti, mti, sizeof(*req_mti));
1210 ptlrpc_request_set_replen(req);
1211 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1212 /* Limit how long we will wait for the enqueue to complete */
1213 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1215 rc = ptlrpc_queue_wait(req);
1217 rep_mti = req_capsule_server_get(&req->rq_pill,
1218 &RMF_MGS_TARGET_INFO);
1219 memcpy(mti, rep_mti, sizeof(*rep_mti));
1220 CDEBUG(D_MGC, "register %s got index = %d\n",
1221 mti->mti_svname, mti->mti_stripe_index);
1223 ptlrpc_req_finished(req);
1228 static int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1229 u32 keylen, void *key,
1230 u32 vallen, void *val,
1231 struct ptlrpc_request_set *set)
1236 /* Turn off initial_recov after we try all backup servers once */
1237 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1238 struct obd_import *imp = class_exp2cliimp(exp);
1240 if (vallen != sizeof(int))
1242 value = *(int *)val;
1243 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1244 imp->imp_obd->obd_name, value,
1245 imp->imp_deactive, imp->imp_invalid,
1246 imp->imp_replayable, imp->imp_obd->obd_replayable,
1247 ptlrpc_import_state_name(imp->imp_state));
1248 /* Resurrect if we previously died */
1249 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1250 imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1251 ptlrpc_reconnect_import(imp);
1254 /* FIXME move this to mgc_process_config */
1255 if (KEY_IS(KEY_REGISTER_TARGET)) {
1256 struct mgs_target_info *mti;
1257 if (vallen != sizeof(struct mgs_target_info))
1259 mti = (struct mgs_target_info *)val;
1260 CDEBUG(D_MGC, "register_target %s %#x\n",
1261 mti->mti_svname, mti->mti_flags);
1262 rc = mgc_target_register(exp, mti);
1265 if (KEY_IS(KEY_SET_FS)) {
1266 struct super_block *sb = (struct super_block *)val;
1268 if (vallen != sizeof(struct super_block))
1271 rc = mgc_fs_setup(env, exp->exp_obd, sb);
1274 if (KEY_IS(KEY_CLEAR_FS)) {
1277 rc = mgc_fs_cleanup(env, exp->exp_obd);
1280 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
1281 if (KEY_IS(KEY_SET_INFO)) {
1282 struct mgs_send_param *msp;
1284 msp = (struct mgs_send_param *)val;
1285 rc = mgc_set_mgs_param(exp, msp);
1289 if (KEY_IS(KEY_MGSSEC)) {
1290 struct client_obd *cli = &exp->exp_obd->u.cli;
1291 struct sptlrpc_flavor flvr;
1294 * empty string means using current flavor, if which haven't
1295 * been set yet, set it as null.
1297 * if flavor has been set previously, check the asking flavor
1298 * must match the existing one.
1301 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1307 rc = sptlrpc_parse_flavor(val, &flvr);
1309 CERROR("invalid sptlrpc flavor %s to MGS\n",
1315 * caller already hold a mutex
1317 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1318 cli->cl_flvr_mgc = flvr;
1319 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1320 sizeof(flvr)) != 0) {
1323 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1325 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1326 "currently %s is in use\n",
1336 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1337 __u32 keylen, void *key, __u32 *vallen, void *val)
1341 if (KEY_IS(KEY_CONN_DATA)) {
1342 struct obd_import *imp = class_exp2cliimp(exp);
1343 struct obd_connect_data *data = val;
1345 if (*vallen == sizeof(*data)) {
1346 *data = imp->imp_connect_data;
1354 static int mgc_import_event(struct obd_device *obd,
1355 struct obd_import *imp,
1356 enum obd_import_event event)
1360 LASSERT(imp->imp_obd == obd);
1361 CDEBUG(D_MGC, "import event %#x\n", event);
1364 case IMP_EVENT_DISCON:
1365 /* MGC imports should not wait for recovery */
1366 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1367 ptlrpc_pinger_ir_down();
1369 case IMP_EVENT_INACTIVE:
1371 case IMP_EVENT_INVALIDATE: {
1372 struct ldlm_namespace *ns = obd->obd_namespace;
1373 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1376 case IMP_EVENT_ACTIVE:
1377 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1378 /* Clearing obd_no_recov allows us to continue pinging */
1379 obd->obd_no_recov = 0;
1380 mgc_notify_active(obd);
1381 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1382 ptlrpc_pinger_ir_up();
1386 case IMP_EVENT_DEACTIVATE:
1387 case IMP_EVENT_ACTIVATE:
1390 CERROR("Unknown import event %#x\n", event);
1397 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_SHIFT),
1398 CONFIG_READ_NRPAGES = 4
1401 static int mgc_apply_recover_logs(struct obd_device *mgc,
1402 struct config_llog_data *cld,
1404 void *data, int datalen, bool mne_swab)
1406 struct config_llog_instance *cfg = &cld->cld_cfg;
1407 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1408 struct mgs_nidtbl_entry *entry;
1409 struct lustre_cfg *lcfg;
1410 struct lustre_cfg_bufs bufs;
1411 u64 prev_version = 0;
1420 LASSERT(cfg->cfg_instance != NULL);
1421 LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1423 OBD_ALLOC(inst, PAGE_SIZE);
1427 if (!IS_SERVER(lsi)) {
1428 pos = snprintf(inst, PAGE_SIZE, "%p", cfg->cfg_instance);
1429 if (pos >= PAGE_SIZE) {
1430 OBD_FREE(inst, PAGE_SIZE);
1434 LASSERT(IS_MDT(lsi));
1435 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1438 OBD_FREE(inst, PAGE_SIZE);
1446 bufsz = PAGE_SIZE - pos;
1448 while (datalen > 0) {
1449 int entry_len = sizeof(*entry);
1451 struct obd_device *obd;
1458 if (datalen < sizeof(*entry))
1461 entry = (typeof(entry))(data + off);
1464 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1466 if (entry->mne_nid_count == 0) /* at least one nid entry */
1468 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1471 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1472 if (datalen < entry_len) /* must have entry_len at least */
1475 /* Keep this swab for normal mixed endian handling. LU-1644 */
1477 lustre_swab_mgs_nidtbl_entry(entry);
1478 if (entry->mne_length > PAGE_SIZE) {
1479 CERROR("MNE too large (%u)\n", entry->mne_length);
1483 if (entry->mne_length < entry_len)
1486 off += entry->mne_length;
1487 datalen -= entry->mne_length;
1491 if (entry->mne_version > max_version) {
1492 CERROR("entry index(%lld) is over max_index(%lld)\n",
1493 entry->mne_version, max_version);
1497 if (prev_version >= entry->mne_version) {
1498 CERROR("index unsorted, prev %lld, now %lld\n",
1499 prev_version, entry->mne_version);
1502 prev_version = entry->mne_version;
1505 * Write a string with format "nid::instance" to
1506 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1509 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1510 memset(buf, 0, bufsz);
1514 /* lustre-OST0001-osc-<instance #> */
1515 strcpy(obdname, cld->cld_logname);
1516 cname = strrchr(obdname, '-');
1517 if (cname == NULL) {
1518 CERROR("mgc %s: invalid logname %s\n",
1519 mgc->obd_name, obdname);
1523 pos = cname - obdname;
1525 pos += sprintf(obdname + pos, "-%s%04x",
1526 is_ost ? "OST" : "MDT", entry->mne_index);
1528 cname = is_ost ? "osc" : "mdc",
1529 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1530 lustre_cfg_bufs_reset(&bufs, obdname);
1532 /* find the obd by obdname */
1533 obd = class_name2obd(obdname);
1535 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1536 mgc->obd_name, obdname);
1538 /* this is a safe race, when the ost is starting up...*/
1542 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1545 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1548 down_read(&obd->u.cli.cl_sem);
1549 if (obd->u.cli.cl_import == NULL) {
1550 /* client does not connect to the OST yet */
1551 up_read(&obd->u.cli.cl_sem);
1556 /* iterate all nids to find one */
1557 /* find uuid by nid */
1559 for (i = 0; i < entry->mne_nid_count; i++) {
1560 rc = client_import_find_conn(obd->u.cli.cl_import,
1562 (struct obd_uuid *)uuid);
1567 up_read(&obd->u.cli.cl_sem);
1569 CERROR("mgc: cannot find uuid by nid %s\n",
1570 libcfs_nid2str(entry->u.nids[0]));
1574 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1575 uuid, libcfs_nid2str(entry->u.nids[0]));
1577 pos += strlen(uuid);
1578 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1579 LASSERT(pos < bufsz);
1581 lustre_cfg_bufs_set_string(&bufs, 1, params);
1583 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1589 CDEBUG(D_INFO, "ir apply logs %lld/%lld for %s -> %s\n",
1590 prev_version, max_version, obdname, params);
1592 rc = class_process_config(lcfg);
1593 lustre_cfg_free(lcfg);
1595 CDEBUG(D_INFO, "process config for %s error %d\n",
1598 /* continue, even one with error */
1601 OBD_FREE(inst, PAGE_SIZE);
1606 * This function is called if this client was notified for target restarting
1607 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery or
1610 static int mgc_process_recover_nodemap_log(struct obd_device *obd,
1611 struct config_llog_data *cld)
1613 struct ptlrpc_connection *mgc_conn;
1614 struct ptlrpc_request *req = NULL;
1615 struct config_llog_instance *cfg = &cld->cld_cfg;
1616 struct mgs_config_body *body;
1617 struct mgs_config_res *res;
1618 struct nodemap_config *new_config = NULL;
1619 struct lu_nodemap *recent_nodemap = NULL;
1620 struct ptlrpc_bulk_desc *desc;
1621 struct page **pages = NULL;
1622 __u64 config_read_offset = 0;
1623 __u8 nodemap_cur_pass = 0;
1626 bool mne_swab = false;
1632 mgc_conn = class_exp2cliimp(cld->cld_mgcexp)->imp_connection;
1634 /* don't need to get local config */
1635 if (cld_is_nodemap(cld) &&
1636 (LNET_NETTYP(LNET_NIDNET(mgc_conn->c_peer.nid)) == LOLND))
1639 /* allocate buffer for bulk transfer.
1640 * if this is the first time for this mgs to read logs,
1641 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1642 * once; otherwise, it only reads increment of logs, this should be
1643 * small and CONFIG_READ_NRPAGES will be used.
1645 nrpages = CONFIG_READ_NRPAGES;
1646 if (cfg->cfg_last_idx == 0 || cld_is_nodemap(cld))
1647 nrpages = CONFIG_READ_NRPAGES_INIT;
1649 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1651 GOTO(out, rc = -ENOMEM);
1653 for (i = 0; i < nrpages; i++) {
1654 pages[i] = alloc_page(GFP_KERNEL);
1655 if (pages[i] == NULL)
1656 GOTO(out, rc = -ENOMEM);
1660 #ifdef HAVE_SERVER_SUPPORT
1661 if (cld_is_nodemap(cld) && config_read_offset == 0) {
1662 new_config = nodemap_config_alloc();
1663 if (IS_ERR(new_config)) {
1664 rc = PTR_ERR(new_config);
1670 LASSERT(cld_is_recover(cld) || cld_is_nodemap(cld));
1671 LASSERT(mutex_is_locked(&cld->cld_lock));
1672 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1673 &RQF_MGS_CONFIG_READ);
1675 GOTO(out, rc = -ENOMEM);
1677 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1682 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1683 LASSERT(body != NULL);
1684 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1685 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1686 >= sizeof(body->mcb_name))
1687 GOTO(out, rc = -E2BIG);
1688 if (cld_is_nodemap(cld))
1689 body->mcb_offset = config_read_offset;
1691 body->mcb_offset = cfg->cfg_last_idx + 1;
1692 body->mcb_type = cld->cld_type;
1693 body->mcb_bits = PAGE_SHIFT;
1694 body->mcb_units = nrpages;
1695 body->mcb_nm_cur_pass = nodemap_cur_pass;
1697 /* allocate bulk transfer descriptor */
1698 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1,
1699 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1701 &ptlrpc_bulk_kiov_pin_ops);
1703 GOTO(out, rc = -ENOMEM);
1705 for (i = 0; i < nrpages; i++)
1706 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1709 ptlrpc_request_set_replen(req);
1710 rc = ptlrpc_queue_wait(req);
1714 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1716 GOTO(out, rc = -EPROTO);
1718 if (cld_is_nodemap(cld)) {
1719 config_read_offset = res->mcr_offset;
1720 eof = config_read_offset == II_END_OFF;
1721 nodemap_cur_pass = res->mcr_nm_cur_pass;
1723 if (res->mcr_size < res->mcr_offset)
1724 GOTO(out, rc = -EINVAL);
1726 /* always update the index even though it might have errors with
1727 * handling the recover logs
1729 cfg->cfg_last_idx = res->mcr_offset;
1730 eof = res->mcr_offset == res->mcr_size;
1732 CDEBUG(D_INFO, "Latest version %lld, more %d.\n",
1733 res->mcr_offset, eof == false);
1736 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1738 GOTO(out, rc = ealen);
1740 if (ealen > nrpages << PAGE_SHIFT)
1741 GOTO(out, rc = -EINVAL);
1743 if (ealen == 0) { /* no logs transferred */
1744 #ifdef HAVE_SERVER_SUPPORT
1745 /* config changed since first read RPC */
1746 if (cld_is_nodemap(cld) && config_read_offset == 0) {
1747 recent_nodemap = NULL;
1748 nodemap_config_dealloc(new_config);
1751 CDEBUG(D_INFO, "nodemap config changed in transit, retrying\n");
1753 /* setting eof to false, we request config again */
1763 mne_swab = !!ptlrpc_rep_need_swab(req);
1764 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
1765 /* This import flag means the server did an extra swab of IR MNE
1766 * records (fixed in LU-1252), reverse it here if needed. LU-1644 */
1767 if (unlikely(req->rq_import->imp_need_mne_swab))
1768 mne_swab = !mne_swab;
1771 /* When a nodemap config is received, we build a new nodemap config,
1772 * with new nodemap structs. We keep track of the most recently added
1773 * nodemap since the config is read ordered by nodemap_id, and so it
1774 * is likely that the next record will be related. Because access to
1775 * the nodemaps is single threaded until the nodemap_config is active,
1776 * we don't need to reference count with recent_nodemap, though
1777 * recent_nodemap should be set to NULL when the nodemap_config
1778 * is either destroyed or set active.
1780 for (i = 0; i < nrpages && ealen > 0; i++) {
1784 ptr = kmap(pages[i]);
1785 if (cld_is_nodemap(cld))
1786 rc2 = nodemap_process_idx_pages(new_config, ptr,
1789 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset,
1796 CWARN("%s: error processing %s log %s: rc = %d\n",
1798 cld_is_nodemap(cld) ? "nodemap" : "recovery",
1801 GOTO(out, rc = rc2);
1809 ptlrpc_req_finished(req);
1813 if (rc == 0 && !eof)
1816 #ifdef HAVE_SERVER_SUPPORT
1817 if (new_config != NULL) {
1818 /* recent_nodemap cannot be used after set_active/dealloc */
1820 nodemap_config_set_active_mgc(new_config);
1822 nodemap_config_dealloc(new_config);
1827 for (i = 0; i < nrpages; i++) {
1828 if (pages[i] == NULL)
1830 __free_page(pages[i]);
1832 OBD_FREE(pages, sizeof(*pages) * nrpages);
1837 static int mgc_barrier_glimpse_ast(struct ldlm_lock *lock, void *data)
1839 /* XXX: It will be implemented in subsequent patch. */
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;
1894 bool sptlrpc_started = false;
1900 LASSERT(mutex_is_locked(&cld->cld_lock));
1903 * local copy of sptlrpc log is controlled elsewhere, don't try to
1906 if (cld_is_sptlrpc(cld) && local_only)
1909 if (cld->cld_cfg.cfg_sb)
1910 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1916 rc = lu_env_init(env, LCT_MG_THREAD);
1920 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1923 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1925 /* Copy the setup log locally if we can. Don't mess around if we're
1926 * running an MGS though (logs are already local). */
1927 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1928 cli->cl_mgc_configs_dir != NULL &&
1929 lu2dt_dev(cli->cl_mgc_configs_dir->do_lu.lo_dev) ==
1932 /* Only try to copy log if we have the lock. */
1933 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1935 if (local_only || rc) {
1936 if (strcmp(cld->cld_logname, PARAMS_FILENAME) != 0 &&
1937 llog_is_empty(env, lctxt, cld->cld_logname)) {
1938 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1939 "log %s and no local copy."
1940 "\n", cld->cld_logname);
1941 GOTO(out_pop, rc = -ENOENT);
1943 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1944 "copy for now, will try to update later.\n",
1948 /* Now, whether we copied or not, start using the local llog.
1949 * If we failed to copy, we'll start using whatever the old
1951 llog_ctxt_put(ctxt);
1955 if (local_only) /* no local log at client side */
1956 GOTO(out_pop, rc = -EIO);
1959 if (cld_is_sptlrpc(cld)) {
1960 sptlrpc_conf_log_update_begin(cld->cld_logname);
1961 sptlrpc_started = true;
1964 /* logname and instance info should be the same, so use our
1965 * copy of the instance for the update. The cfg_last_idx will
1966 * be updated here. */
1967 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1972 __llog_ctxt_put(env, ctxt);
1974 __llog_ctxt_put(env, lctxt);
1977 * update settings on existing OBDs. doing it inside
1978 * of llog_process_lock so no device is attaching/detaching
1980 * the logname must be <fsname>-sptlrpc
1982 if (sptlrpc_started) {
1983 LASSERT(cld_is_sptlrpc(cld));
1984 sptlrpc_conf_log_update_end(cld->cld_logname);
1985 class_notify_sptlrpc_conf(cld->cld_logname,
1986 strlen(cld->cld_logname) -
1987 strlen("-sptlrpc"));
1996 static bool mgc_import_in_recovery(struct obd_import *imp)
1998 bool in_recovery = true;
2000 spin_lock(&imp->imp_lock);
2001 if (imp->imp_state == LUSTRE_IMP_FULL ||
2002 imp->imp_state == LUSTRE_IMP_CLOSED)
2003 in_recovery = false;
2004 spin_unlock(&imp->imp_lock);
2010 * Get a configuration log from the MGS and process it.
2012 * This function is called for both clients and servers to process the
2013 * configuration log from the MGS. The MGC enqueues a DLM lock on the
2014 * log from the MGS, and if the lock gets revoked the MGC will be notified
2015 * by the lock cancellation callback that the config log has changed,
2016 * and will enqueue another MGS lock on it, and then continue processing
2017 * the new additions to the end of the log.
2019 * Since the MGC import is not replayable, if the import is being evicted
2020 * (rcl == -ESHUTDOWN, \see ptlrpc_import_delay_req()), retry to process
2021 * the log until recovery is finished or the import is closed.
2023 * Make a local copy of the log before parsing it if appropriate (non-MGS
2024 * server) so that the server can start even when the MGS is down.
2026 * There shouldn't be multiple processes running process_log at once --
2027 * sounds like badness. It actually might be fine, as long as they're not
2028 * trying to update from the same log simultaneously, in which case we
2029 * should use a per-log semaphore instead of cld_lock.
2031 * \param[in] mgc MGC device by which to fetch the configuration log
2032 * \param[in] cld log processing state (stored in lock callback data)
2034 * \retval 0 on success
2035 * \retval negative errno on failure
2037 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
2039 struct lustre_handle lockh = { 0 };
2040 __u64 flags = LDLM_FL_NO_LRU;
2045 LASSERT(cld != NULL);
2047 /* I don't want multiple processes running process_log at once --
2048 sounds like badness. It actually might be fine, as long as
2049 we're not trying to update from the same log
2050 simultaneously (in which case we should use a per-log sem.) */
2052 mutex_lock(&cld->cld_lock);
2053 if (cld->cld_stopping) {
2054 mutex_unlock(&cld->cld_lock);
2058 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
2060 CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
2061 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
2063 /* Get the cfg lock on the llog */
2064 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, LDLM_PLAIN, NULL,
2066 cld_is_barrier(cld) ? mgc_barrier_glimpse_ast : NULL,
2067 cld, 0, NULL, &lockh);
2069 /* Get the cld, it will be released in mgc_blocking_ast. */
2070 config_log_get(cld);
2071 rc = ldlm_lock_set_data(&lockh, (void *)cld);
2074 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
2076 if (rcl == -ESHUTDOWN &&
2077 atomic_read(&mgc->u.cli.cl_mgc_refcount) > 0 && !retry) {
2078 struct obd_import *imp;
2079 struct l_wait_info lwi;
2080 int secs = 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 lwi = LWI_TIMEOUT(secs, NULL, NULL);
2094 l_wait_event(imp->imp_recovery_waitq,
2095 !mgc_import_in_recovery(imp), &lwi);
2097 if (imp->imp_state == LUSTRE_IMP_FULL) {
2101 mutex_lock(&cld->cld_lock);
2102 spin_lock(&config_list_lock);
2103 cld->cld_lostlock = 1;
2104 spin_unlock(&config_list_lock);
2107 /* mark cld_lostlock so that it will requeue
2108 * after MGC becomes available. */
2109 spin_lock(&config_list_lock);
2110 cld->cld_lostlock = 1;
2111 spin_unlock(&config_list_lock);
2115 if (cld_is_recover(cld) || cld_is_nodemap(cld)) {
2117 rc = mgc_process_recover_nodemap_log(mgc, cld);
2118 else if (cld_is_nodemap(cld))
2121 if (cld_is_recover(cld) && rc) {
2123 CERROR("%s: recover log %s failed, not fatal: rc = %d\n",
2124 mgc->obd_name, cld->cld_logname, rc);
2125 spin_lock(&config_list_lock);
2126 cld->cld_lostlock = 1;
2127 spin_unlock(&config_list_lock);
2129 rc = 0; /* this is not a fatal error for recover log */
2131 } else if (!cld_is_barrier(cld)) {
2132 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
2135 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
2136 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
2138 mutex_unlock(&cld->cld_lock);
2140 /* Now drop the lock so MGS can revoke it */
2142 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, LCK_CR, &lockh);
2144 CERROR("Can't drop cfg lock: %d\n", rcl);
2151 /** Called from lustre_process_log.
2152 * LCFG_LOG_START gets the config log from the MGS, processes it to start
2153 * any services, and adds it to the list logs to watch (follow).
2155 static int mgc_process_config(struct obd_device *obd, size_t len, void *buf)
2157 struct lustre_cfg *lcfg = buf;
2158 struct config_llog_instance *cfg = NULL;
2163 switch(lcfg->lcfg_command) {
2164 case LCFG_LOV_ADD_OBD: {
2165 /* Overloading this cfg command: register a new target */
2166 struct mgs_target_info *mti;
2168 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
2169 sizeof(struct mgs_target_info))
2170 GOTO(out, rc = -EINVAL);
2172 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
2173 CDEBUG(D_MGC, "add_target %s %#x\n",
2174 mti->mti_svname, mti->mti_flags);
2175 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
2178 case LCFG_LOV_DEL_OBD:
2179 /* Unregister has no meaning at the moment. */
2180 CERROR("lov_del_obd unimplemented\n");
2183 case LCFG_SPTLRPC_CONF: {
2184 rc = sptlrpc_process_config(lcfg);
2187 case LCFG_LOG_START: {
2188 struct config_llog_data *cld;
2189 struct super_block *sb;
2191 logname = lustre_cfg_string(lcfg, 1);
2192 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
2193 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
2195 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
2198 /* We're only called through here on the initial mount */
2199 cld = config_log_add(obd, logname, cfg, sb);
2206 /* FIXME only set this for old logs! Right now this forces
2207 us to always skip the "inside markers" check */
2208 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
2210 rc = mgc_process_log(obd, cld);
2211 if (rc == 0 && cld->cld_recover != NULL) {
2212 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
2213 imp_connect_data, IMP_RECOV)) {
2214 rc = mgc_process_log(obd, cld->cld_recover);
2216 struct config_llog_data *cir;
2218 mutex_lock(&cld->cld_lock);
2219 cir = cld->cld_recover;
2220 cld->cld_recover = NULL;
2221 mutex_unlock(&cld->cld_lock);
2222 config_log_put(cir);
2226 CERROR("Cannot process recover llog %d\n", rc);
2229 if (rc == 0 && cld->cld_params != NULL) {
2230 rc = mgc_process_log(obd, cld->cld_params);
2231 if (rc == -ENOENT) {
2232 CDEBUG(D_MGC, "There is no params "
2233 "config file yet\n");
2236 /* params log is optional */
2238 CERROR("%s: can't process params llog: rc = %d\n",
2244 case LCFG_LOG_END: {
2245 logname = lustre_cfg_string(lcfg, 1);
2247 if (lcfg->lcfg_bufcount >= 2)
2248 cfg = (struct config_llog_instance *)lustre_cfg_buf(
2250 rc = config_log_end(logname, cfg);
2254 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
2255 GOTO(out, rc = -EINVAL);
2263 static struct obd_ops mgc_obd_ops = {
2264 .o_owner = THIS_MODULE,
2265 .o_setup = mgc_setup,
2266 .o_precleanup = mgc_precleanup,
2267 .o_cleanup = mgc_cleanup,
2268 .o_add_conn = client_import_add_conn,
2269 .o_del_conn = client_import_del_conn,
2270 .o_connect = client_connect_import,
2271 .o_disconnect = client_disconnect_export,
2272 .o_set_info_async = mgc_set_info_async,
2273 .o_get_info = mgc_get_info,
2274 .o_import_event = mgc_import_event,
2275 .o_process_config = mgc_process_config,
2278 static int __init mgc_init(void)
2280 return class_register_type(&mgc_obd_ops, NULL, true, NULL,
2281 LUSTRE_MGC_NAME, NULL);
2284 static void __exit mgc_exit(void)
2286 class_unregister_type(LUSTRE_MGC_NAME);
2289 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2290 MODULE_DESCRIPTION("Lustre Management Client");
2291 MODULE_VERSION(LUSTRE_VERSION_STRING);
2292 MODULE_LICENSE("GPL");
2294 module_init(mgc_init);
2295 module_exit(mgc_exit);