4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mgc/mgc_request.c
34 * Author: Nathan Rutman <nathan@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_MGC
38 #define D_MGC D_CONFIG /*|D_WARNING*/
40 #include <linux/module.h>
41 #include <linux/kthread.h>
42 #include <linux/random.h>
44 #include <dt_object.h>
45 #include <lprocfs_status.h>
46 #include <lustre_dlm.h>
47 #include <lustre_disk.h>
48 #include <lustre_log.h>
49 #include <lustre_nodemap.h>
50 #include <lustre_swab.h>
51 #include <obd_class.h>
52 #include <lustre_barrier.h>
54 #include "mgc_internal.h"
56 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id,
61 if (len > sizeof(resname)) {
62 CERROR("name too long: %s\n", name);
66 CERROR("missing name: %s\n", name);
69 memcpy(&resname, name, len);
71 /* Always use the same endianness for the resid */
72 memset(res_id, 0, sizeof(*res_id));
73 res_id->name[0] = cpu_to_le64(resname);
74 /* XXX: unfortunately, sptlprc and config llog share one lock */
77 case CONFIG_T_SPTLRPC:
80 case CONFIG_T_RECOVER:
82 case CONFIG_T_NODEMAP:
83 case CONFIG_T_BARRIER:
89 res_id->name[1] = cpu_to_le64(resname);
90 CDEBUG(D_MGC, "log %s to resid %#llx/%#llx (%.8s)\n", name,
91 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
95 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id, int 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, int type)
108 /* logname consists of "fsname-nodetype".
109 * e.g. "lustre-MDT0001", "SUN-000-client"
110 * there is an exception: llog "params" */
111 name_end = strrchr(logname, '-');
113 len = strlen(logname);
115 len = name_end - logname;
116 return mgc_name2resid(logname, len, res_id, type);
118 EXPORT_SYMBOL(mgc_logname2resid);
120 /********************** config llog list **********************/
121 static LIST_HEAD(config_llog_list);
122 static DEFINE_SPINLOCK(config_list_lock); /* protects config_llog_list */
124 /* Take a reference to a config log */
125 static int config_log_get(struct config_llog_data *cld)
128 atomic_inc(&cld->cld_refcount);
129 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
130 atomic_read(&cld->cld_refcount));
134 /* Drop a reference to a config log. When no longer referenced,
135 we can free the config log data */
136 static void config_log_put(struct config_llog_data *cld)
143 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
144 atomic_read(&cld->cld_refcount));
145 LASSERT(atomic_read(&cld->cld_refcount) > 0);
147 /* spinlock to make sure no item with 0 refcount in the list */
148 if (atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
149 list_del(&cld->cld_list_chain);
150 spin_unlock(&config_list_lock);
152 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
154 config_log_put(cld->cld_barrier);
155 config_log_put(cld->cld_recover);
156 config_log_put(cld->cld_params);
157 config_log_put(cld->cld_nodemap);
158 config_log_put(cld->cld_sptlrpc);
159 if (cld_is_sptlrpc(cld))
160 sptlrpc_conf_log_stop(cld->cld_logname);
162 class_export_put(cld->cld_mgcexp);
163 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
169 /* Find a config log by name */
171 struct config_llog_data *config_log_find(char *logname,
172 struct config_llog_instance *cfg)
174 struct config_llog_data *cld;
175 struct config_llog_data *found = NULL;
176 unsigned long cfg_instance;
179 LASSERT(logname != NULL);
181 cfg_instance = cfg ? cfg->cfg_instance : 0;
182 spin_lock(&config_list_lock);
183 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
184 /* check if cfg_instance is the one we want */
185 if (cfg_instance != cld->cld_cfg.cfg_instance)
188 /* instance may be NULL, should check name */
189 if (strcmp(logname, cld->cld_logname) == 0) {
191 config_log_get(found);
195 spin_unlock(&config_list_lock);
200 struct config_llog_data *do_config_log_add(struct obd_device *obd,
203 struct config_llog_instance *cfg,
204 struct super_block *sb)
206 struct config_llog_data *cld;
211 CDEBUG(D_MGC, "do adding config log %s-%016lx\n", logname,
212 cfg ? cfg->cfg_instance : 0);
214 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
216 RETURN(ERR_PTR(-ENOMEM));
218 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
220 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
224 strcpy(cld->cld_logname, logname);
228 cld->cld_cfg.cfg_callback = class_config_llog_handler;
229 mutex_init(&cld->cld_lock);
230 cld->cld_cfg.cfg_last_idx = 0;
231 cld->cld_cfg.cfg_flags = 0;
232 cld->cld_cfg.cfg_sb = sb;
233 cld->cld_type = type;
234 atomic_set(&cld->cld_refcount, 1);
236 /* Keep the mgc around until we are done */
237 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
239 if (cld_is_sptlrpc(cld))
240 sptlrpc_conf_log_start(logname);
242 spin_lock(&config_list_lock);
243 list_add(&cld->cld_list_chain, &config_llog_list);
244 spin_unlock(&config_list_lock);
246 if (cld_is_sptlrpc(cld) || cld_is_nodemap(cld) || cld_is_barrier(cld)) {
247 rc = mgc_process_log(obd, cld);
248 if (rc && rc != -ENOENT)
249 CERROR("%s: failed processing log, type %d: rc = %d\n",
250 obd->obd_name, type, rc);
256 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
258 struct config_llog_instance *cfg,
259 struct super_block *sb)
261 struct config_llog_instance lcfg = *cfg;
262 struct lustre_sb_info *lsi = s2lsi(sb);
263 struct config_llog_data *cld;
269 /* for osp-on-ost, see lustre_start_osp() */
270 if (IS_MDT(lsi) && lcfg.cfg_instance)
273 /* We have to use different llog for clients and MDTs for DNE,
274 * where only clients are notified if one of DNE server restarts.
276 LASSERT(strlen(fsname) < sizeof(logname) / 2);
277 strncpy(logname, fsname, sizeof(logname));
278 if (IS_SERVER(lsi)) { /* mdt */
279 LASSERT(lcfg.cfg_instance == 0);
280 lcfg.cfg_instance = ll_get_cfg_instance(sb);
281 strncat(logname, "-mdtir", sizeof(logname));
283 LASSERT(lcfg.cfg_instance != 0);
284 strncat(logname, "-cliir", sizeof(logname));
287 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
291 static struct config_llog_data *config_log_find_or_add(struct obd_device *obd,
292 char *logname, struct super_block *sb, int type,
293 struct config_llog_instance *cfg)
295 struct config_llog_instance lcfg = *cfg;
296 struct config_llog_data *cld;
298 /* Note class_config_llog_handler() depends on getting "obd" back */
299 lcfg.cfg_instance = sb ? ll_get_cfg_instance(sb) : (unsigned long)obd;
301 cld = config_log_find(logname, &lcfg);
302 if (unlikely(cld != NULL))
305 return do_config_log_add(obd, logname, type, &lcfg, sb);
308 /** Add this log to the list of active logs watched by an MGC.
309 * Active means we're watching for updates.
310 * We have one active log per "mount" - client instance or servername.
311 * Each instance may be at a different point in the log.
313 static struct config_llog_data *
314 config_log_add(struct obd_device *obd, char *logname,
315 struct config_llog_instance *cfg, struct super_block *sb)
317 struct lustre_sb_info *lsi = s2lsi(sb);
318 struct config_llog_data *cld = NULL;
319 struct config_llog_data *sptlrpc_cld = NULL;
320 struct config_llog_data *params_cld = NULL;
321 struct config_llog_data *nodemap_cld = NULL;
322 struct config_llog_data *barrier_cld = NULL;
329 CDEBUG(D_MGC, "add config log %s-%016lx\n", logname,
333 * for each regular log, the depended sptlrpc log name is
334 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
336 ptr = strrchr(logname, '-');
337 if (ptr == NULL || ptr - logname > 8) {
338 CERROR("logname %s is too long\n", logname);
339 RETURN(ERR_PTR(-EINVAL));
342 memcpy(seclogname, logname, ptr - logname);
343 strcpy(seclogname + (ptr - logname), "-sptlrpc");
345 if (cfg->cfg_sub_clds & CONFIG_SUB_SPTLRPC) {
346 sptlrpc_cld = config_log_find_or_add(obd, seclogname, NULL,
347 CONFIG_T_SPTLRPC, cfg);
348 if (IS_ERR(sptlrpc_cld)) {
349 CERROR("%s: can't create sptlrpc log %s: rc = %ld\n",
350 obd->obd_name, seclogname, PTR_ERR(sptlrpc_cld));
355 if (!IS_MGS(lsi) && cfg->cfg_sub_clds & CONFIG_SUB_NODEMAP) {
356 nodemap_cld = config_log_find_or_add(obd, LUSTRE_NODEMAP_NAME,
357 NULL, CONFIG_T_NODEMAP,
359 if (IS_ERR(nodemap_cld)) {
360 rc = PTR_ERR(nodemap_cld);
361 CERROR("%s: cannot create nodemap log: rc = %d\n",
363 GOTO(out_sptlrpc, rc);
367 if (cfg->cfg_sub_clds & CONFIG_SUB_PARAMS) {
368 params_cld = config_log_find_or_add(obd, PARAMS_FILENAME, sb,
369 CONFIG_T_PARAMS, cfg);
370 if (IS_ERR(params_cld)) {
371 rc = PTR_ERR(params_cld);
372 CERROR("%s: can't create params log: rc = %d\n",
374 GOTO(out_nodemap, rc);
378 if (IS_MDT(s2lsi(sb)) && cfg->cfg_sub_clds & CONFIG_SUB_BARRIER) {
379 snprintf(seclogname + (ptr - logname), sizeof(seclogname) - 1,
380 "-%s", BARRIER_FILENAME);
381 barrier_cld = config_log_find_or_add(obd, seclogname, sb,
382 CONFIG_T_BARRIER, cfg);
383 if (IS_ERR(barrier_cld)) {
384 rc = PTR_ERR(barrier_cld);
385 CERROR("%s: can't create barrier log: rc = %d\n",
387 GOTO(out_params, rc);
391 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
394 CERROR("%s: can't create log: rc = %d\n",
396 GOTO(out_barrier, rc = PTR_ERR(cld));
399 LASSERT(lsi->lsi_lmd);
400 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR) &&
401 cfg->cfg_sub_clds & CONFIG_SUB_RECOVER) {
402 struct config_llog_data *recover_cld;
404 ptr = strrchr(seclogname, '-');
408 CERROR("%s: sptlrpc log name not correct, %s: "
409 "rc = %d\n", obd->obd_name, seclogname, -EINVAL);
410 GOTO(out_cld, rc = -EINVAL);
413 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
414 if (IS_ERR(recover_cld)) {
415 rc = PTR_ERR(recover_cld);
416 CERROR("%s: can't create recover log: rc = %d\n",
421 mutex_lock(&cld->cld_lock);
423 cld->cld_recover = recover_cld;
427 mutex_lock(&cld->cld_lock);
428 cld->cld_params = params_cld;
429 cld->cld_barrier = barrier_cld;
430 cld->cld_nodemap = nodemap_cld;
431 cld->cld_sptlrpc = sptlrpc_cld;
432 mutex_unlock(&cld->cld_lock);
439 config_log_put(barrier_cld);
441 config_log_put(params_cld);
443 config_log_put(nodemap_cld);
445 config_log_put(sptlrpc_cld);
450 static inline void config_mark_cld_stop(struct config_llog_data *cld)
453 mutex_lock(&cld->cld_lock);
454 spin_lock(&config_list_lock);
455 cld->cld_stopping = 1;
456 spin_unlock(&config_list_lock);
457 mutex_unlock(&cld->cld_lock);
461 /** Stop watching for updates on this log.
463 static int config_log_end(char *logname, struct config_llog_instance *cfg)
465 struct config_llog_data *cld;
466 struct config_llog_data *cld_sptlrpc = NULL;
467 struct config_llog_data *cld_params = NULL;
468 struct config_llog_data *cld_recover = NULL;
469 struct config_llog_data *cld_nodemap = NULL;
470 struct config_llog_data *cld_barrier = NULL;
475 cld = config_log_find(logname, cfg);
479 mutex_lock(&cld->cld_lock);
481 * if cld_stopping is set, it means we didn't start the log thus
482 * not owning the start ref. this can happen after previous umount:
483 * the cld still hanging there waiting for lock cancel, and we
484 * remount again but failed in the middle and call log_end without
487 if (unlikely(cld->cld_stopping)) {
488 mutex_unlock(&cld->cld_lock);
489 /* drop the ref from the find */
494 spin_lock(&config_list_lock);
495 cld->cld_stopping = 1;
496 spin_unlock(&config_list_lock);
498 cld_recover = cld->cld_recover;
499 cld->cld_recover = NULL;
500 cld_params = cld->cld_params;
501 cld->cld_params = NULL;
502 cld_nodemap = cld->cld_nodemap;
503 cld->cld_nodemap = NULL;
504 cld_barrier = cld->cld_barrier;
505 cld->cld_barrier = NULL;
506 cld_sptlrpc = cld->cld_sptlrpc;
507 cld->cld_sptlrpc = NULL;
508 mutex_unlock(&cld->cld_lock);
510 config_mark_cld_stop(cld_recover);
511 config_log_put(cld_recover);
513 config_mark_cld_stop(cld_params);
514 config_log_put(cld_params);
516 /* don't set cld_stopping on nm lock as other targets may be active */
517 config_log_put(cld_nodemap);
520 mutex_lock(&cld_barrier->cld_lock);
521 cld_barrier->cld_stopping = 1;
522 mutex_unlock(&cld_barrier->cld_lock);
523 config_log_put(cld_barrier);
526 config_log_put(cld_sptlrpc);
528 /* drop the ref from the find */
530 /* drop the start ref */
533 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
538 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
540 struct obd_device *obd = data;
541 struct obd_import *imp;
542 struct obd_connect_data *ocd;
543 struct config_llog_data *cld;
547 LPROCFS_CLIMP_CHECK(obd);
548 imp = obd->u.cli.cl_import;
549 ocd = &imp->imp_connect_data;
551 seq_printf(m, "imperative_recovery: %s\n",
552 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
553 seq_printf(m, "client_state:\n");
555 spin_lock(&config_list_lock);
556 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
557 if (cld->cld_recover == NULL)
559 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
561 cld->cld_recover->cld_cfg.cfg_last_idx);
563 spin_unlock(&config_list_lock);
565 LPROCFS_CLIMP_EXIT(obd);
569 /* reenqueue any lost locks */
570 #define RQ_RUNNING 0x1
574 #define RQ_PRECLEANUP 0x10
575 static int rq_state = 0;
576 static wait_queue_head_t rq_waitq;
577 static DECLARE_COMPLETION(rq_exit);
578 static DECLARE_COMPLETION(rq_start);
580 static void do_requeue(struct config_llog_data *cld)
585 LASSERT(atomic_read(&cld->cld_refcount) > 0);
588 * Do not run mgc_process_log on a disconnected export or an
589 * export which is being disconnected. Take the client
590 * semaphore to make the check non-racy.
592 down_read_nested(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem,
594 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
595 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
596 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
597 if (rc && rc != -ENOENT)
598 CERROR("failed processing log: %d\n", rc);
600 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
603 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
608 /* this timeout represents how many seconds MGC should wait before
609 * requeue config and recover lock to the MGS. We need to randomize this
610 * in order to not flood the MGS.
612 #define MGC_TIMEOUT_MIN_SECONDS 5
613 #define MGC_TIMEOUT_RAND_CENTISEC 500
615 static int mgc_requeue_thread(void *data)
621 CDEBUG(D_MGC, "Starting requeue thread\n");
623 /* Keep trying failed locks periodically */
624 spin_lock(&config_list_lock);
625 rq_state |= RQ_RUNNING;
626 while (!(rq_state & RQ_STOP)) {
627 struct l_wait_info lwi;
628 struct config_llog_data *cld, *cld_prev;
629 int rand = prandom_u32_max(MGC_TIMEOUT_RAND_CENTISEC);
632 /* Any new or requeued lostlocks will change the state */
633 rq_state &= ~(RQ_NOW | RQ_LATER);
634 spin_unlock(&config_list_lock);
641 /* Always wait a few seconds to allow the server who
642 caused the lock revocation to finish its setup, plus some
643 random so everyone doesn't try to reconnect at once. */
644 to = cfs_time_seconds(MGC_TIMEOUT_MIN_SECONDS * 100 + rand);
645 /* rand is centi-seconds */
646 lwi = LWI_TIMEOUT(to / 100, NULL, NULL);
647 l_wait_event(rq_waitq, rq_state & (RQ_STOP | RQ_PRECLEANUP),
651 * iterate & processing through the list. for each cld, process
652 * its depending sptlrpc cld firstly (if any) and then itself.
654 * it's guaranteed any item in the list must have
655 * reference > 0; and if cld_lostlock is set, at
656 * least one reference is taken by the previous enqueue.
660 spin_lock(&config_list_lock);
661 rq_state &= ~RQ_PRECLEANUP;
662 list_for_each_entry(cld, &config_llog_list,
664 if (!cld->cld_lostlock || cld->cld_stopping)
667 /* hold reference to avoid being freed during
668 * subsequent processing. */
670 cld->cld_lostlock = 0;
671 spin_unlock(&config_list_lock);
673 config_log_put(cld_prev);
676 if (likely(!(rq_state & RQ_STOP))) {
678 spin_lock(&config_list_lock);
680 spin_lock(&config_list_lock);
684 spin_unlock(&config_list_lock);
685 config_log_put(cld_prev);
687 /* Wait a bit to see if anyone else needs a requeue */
688 wait_event_idle(rq_waitq, rq_state & (RQ_NOW | RQ_STOP));
689 spin_lock(&config_list_lock);
692 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
693 rq_state &= ~RQ_RUNNING;
694 spin_unlock(&config_list_lock);
698 CDEBUG(D_MGC, "Ending requeue thread\n");
702 /* Add a cld to the list to requeue. Start the requeue thread if needed.
703 We are responsible for dropping the config log reference from here on out. */
704 static void mgc_requeue_add(struct config_llog_data *cld)
709 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
710 cld->cld_logname, atomic_read(&cld->cld_refcount),
711 cld->cld_stopping, rq_state);
712 LASSERT(atomic_read(&cld->cld_refcount) > 0);
714 mutex_lock(&cld->cld_lock);
715 spin_lock(&config_list_lock);
716 if (!(rq_state & RQ_STOP) && !cld->cld_stopping && !cld->cld_lostlock) {
717 cld->cld_lostlock = 1;
721 spin_unlock(&config_list_lock);
722 mutex_unlock(&cld->cld_lock);
729 /********************** class fns **********************/
730 static int mgc_local_llog_init(const struct lu_env *env,
731 struct obd_device *obd,
732 struct obd_device *disk)
734 struct llog_ctxt *ctxt;
739 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
744 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
746 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
752 static int mgc_local_llog_fini(const struct lu_env *env,
753 struct obd_device *obd)
755 struct llog_ctxt *ctxt;
759 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
760 llog_cleanup(env, ctxt);
765 static int mgc_fs_setup(const struct lu_env *env, struct obd_device *obd,
766 struct super_block *sb)
768 struct lustre_sb_info *lsi = s2lsi(sb);
769 struct client_obd *cli = &obd->u.cli;
770 struct lu_fid rfid, fid;
771 struct dt_object *root, *dto;
777 LASSERT(lsi->lsi_dt_dev);
779 /* The mgc fs exclusion mutex. Only one fs can be setup at a time. */
780 mutex_lock(&cli->cl_mgc_mutex);
782 /* Setup the configs dir */
783 fid.f_seq = FID_SEQ_LOCAL_NAME;
786 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
791 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
795 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
796 &cli->cl_mgc_los->los_dev->dd_lu_dev, NULL);
797 if (unlikely(IS_ERR(root)))
798 GOTO(out_los, rc = PTR_ERR(root));
800 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
802 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
803 dt_object_put_nocache(env, root);
805 GOTO(out_los, rc = PTR_ERR(dto));
807 cli->cl_mgc_configs_dir = dto;
809 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
810 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
814 /* We take an obd ref to insure that we can't get to mgc_cleanup
815 * without calling mgc_fs_cleanup first. */
816 class_incref(obd, "mgc_fs", obd);
818 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
822 dt_object_put(env, cli->cl_mgc_configs_dir);
823 cli->cl_mgc_configs_dir = NULL;
827 local_oid_storage_fini(env, cli->cl_mgc_los);
828 cli->cl_mgc_los = NULL;
829 mutex_unlock(&cli->cl_mgc_mutex);
834 static int mgc_fs_cleanup(const struct lu_env *env, struct obd_device *obd)
836 struct client_obd *cli = &obd->u.cli;
839 LASSERT(cli->cl_mgc_los != NULL);
841 mgc_local_llog_fini(env, obd);
843 dt_object_put_nocache(env, cli->cl_mgc_configs_dir);
844 cli->cl_mgc_configs_dir = NULL;
846 local_oid_storage_fini(env, cli->cl_mgc_los);
847 cli->cl_mgc_los = NULL;
849 class_decref(obd, "mgc_fs", obd);
850 mutex_unlock(&cli->cl_mgc_mutex);
855 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
857 struct llog_ctxt *ctxt;
862 /* setup only remote ctxt, the local disk context is switched per each
863 * filesystem during mgc_fs_setup() */
864 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
869 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
872 llog_initiator_connect(ctxt);
878 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
880 struct llog_ctxt *ctxt;
884 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
886 llog_cleanup(env, ctxt);
892 static atomic_t mgc_count = ATOMIC_INIT(0);
893 static int mgc_precleanup(struct obd_device *obd)
899 if (atomic_dec_and_test(&mgc_count)) {
900 LASSERT(rq_state & RQ_RUNNING);
901 /* stop requeue thread */
904 /* wakeup requeue thread to clean our cld */
905 temp = RQ_NOW | RQ_PRECLEANUP;
908 spin_lock(&config_list_lock);
910 spin_unlock(&config_list_lock);
914 wait_for_completion(&rq_exit);
915 obd_cleanup_client_import(obd);
917 rc = mgc_llog_fini(NULL, obd);
919 CERROR("failed to cleanup llogging subsystems\n");
924 static int mgc_cleanup(struct obd_device *obd)
929 /* COMPAT_146 - old config logs may have added profiles we don't
931 if (atomic_read(&obd->obd_type->typ_refcnt) <= 1)
932 /* Only for the last mgc */
933 class_del_profiles();
935 lprocfs_obd_cleanup(obd);
938 rc = client_obd_cleanup(obd);
942 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
944 struct task_struct *task;
948 rc = ptlrpcd_addref();
952 rc = client_obd_setup(obd, lcfg);
954 GOTO(err_decref, rc);
956 rc = mgc_llog_init(NULL, obd);
958 CERROR("failed to setup llogging subsystems\n");
959 GOTO(err_cleanup, rc);
962 rc = mgc_tunables_init(obd);
966 if (atomic_inc_return(&mgc_count) == 1) {
968 init_waitqueue_head(&rq_waitq);
970 /* start requeue thread */
971 task = kthread_run(mgc_requeue_thread, NULL, "ll_cfg_requeue");
974 CERROR("%s: cannot start requeue thread: rc = %d; "
975 "no more log updates\n",
979 /* rc is the task_struct pointer of mgc_requeue_thread. */
981 wait_for_completion(&rq_start);
987 lprocfs_obd_cleanup(obd);
989 client_obd_cleanup(obd);
995 /* based on ll_mdc_blocking_ast */
996 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
997 void *data, int flag)
999 struct lustre_handle lockh;
1000 struct config_llog_data *cld = (struct config_llog_data *)data;
1005 case LDLM_CB_BLOCKING:
1006 /* mgs wants the lock, give it up... */
1007 LDLM_DEBUG(lock, "MGC blocking CB");
1008 ldlm_lock2handle(lock, &lockh);
1009 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
1011 case LDLM_CB_CANCELING:
1012 /* We've given up the lock, prepare ourselves to update. */
1013 LDLM_DEBUG(lock, "MGC cancel CB");
1015 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
1016 PLDLMRES(lock->l_resource),
1017 (char *)&lock->l_resource->lr_name.name[0]);
1020 CDEBUG(D_INFO, "missing data, won't requeue\n");
1024 /* held at mgc_process_log(). */
1025 LASSERT(atomic_read(&cld->cld_refcount) > 0);
1027 lock->l_ast_data = NULL;
1028 /* Are we done with this log? */
1029 if (cld->cld_stopping) {
1030 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
1032 config_log_put(cld);
1035 /* Make sure not to re-enqueue when the mgc is stopping
1036 (we get called from client_disconnect_export) */
1037 if (lock->l_conn_export == NULL ||
1038 lock->l_conn_export->exp_obd->u.cli.cl_conn_count == 0) {
1039 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
1041 config_log_put(cld);
1045 /* Re-enqueue now */
1046 mgc_requeue_add(cld);
1047 config_log_put(cld);
1056 /* Not sure where this should go... */
1057 /* This is the timeout value for MGS_CONNECT request plus a ping interval, such
1058 * that we can have a chance to try the secondary MGS if any. */
1059 #define MGC_ENQUEUE_LIMIT (INITIAL_CONNECT_TIMEOUT + (AT_OFF ? 0 : at_min) \
1061 #define MGC_TARGET_REG_LIMIT 10
1062 #define MGC_TARGET_REG_LIMIT_MAX RECONNECT_DELAY_MAX
1063 #define MGC_SEND_PARAM_LIMIT 10
1065 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
1066 /* Send parameter to MGS*/
1067 static int mgc_set_mgs_param(struct obd_export *exp,
1068 struct mgs_send_param *msp)
1070 struct ptlrpc_request *req;
1071 struct mgs_send_param *req_msp, *rep_msp;
1075 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1076 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
1081 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1083 ptlrpc_req_finished(req);
1087 memcpy(req_msp, msp, sizeof(*req_msp));
1088 ptlrpc_request_set_replen(req);
1090 /* Limit how long we will wait for the enqueue to complete */
1091 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
1092 rc = ptlrpc_queue_wait(req);
1094 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1095 memcpy(msp, rep_msp, sizeof(*rep_msp));
1098 ptlrpc_req_finished(req);
1104 /* Take a config lock so we can get cancel notifications */
1105 static int mgc_enqueue(struct obd_export *exp, enum ldlm_type type,
1106 union ldlm_policy_data *policy, enum ldlm_mode mode,
1107 __u64 *flags, ldlm_glimpse_callback glimpse_callback,
1108 void *data, __u32 lvb_len, void *lvb_swabber,
1109 struct lustre_handle *lockh)
1111 struct config_llog_data *cld = (struct config_llog_data *)data;
1112 struct ldlm_enqueue_info einfo = {
1115 .ei_cb_bl = mgc_blocking_ast,
1116 .ei_cb_cp = ldlm_completion_ast,
1117 .ei_cb_gl = glimpse_callback,
1119 struct ptlrpc_request *req;
1120 int short_limit = cld_is_sptlrpc(cld);
1124 CDEBUG(D_MGC, "Enqueue for %s (res %#llx)\n", cld->cld_logname,
1125 cld->cld_resid.name[0]);
1127 /* We need a callback for every lockholder, so don't try to
1128 ldlm_lock_match (see rev 1.1.2.11.2.47) */
1129 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1130 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1135 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1136 ptlrpc_request_set_replen(req);
1138 /* check if this is server or client */
1139 if (cld->cld_cfg.cfg_sb) {
1140 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
1141 if (lsi && IS_SERVER(lsi))
1144 /* Limit how long we will wait for the enqueue to complete */
1145 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
1146 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1147 NULL, 0, LVB_T_NONE, lockh, 0);
1148 /* A failed enqueue should still call the mgc_blocking_ast,
1149 where it will be requeued if needed ("grant failed"). */
1150 ptlrpc_req_finished(req);
1154 static int mgc_cancel(struct obd_export *exp, enum ldlm_mode mode,
1155 struct lustre_handle *lockh)
1159 ldlm_lock_decref(lockh, mode);
1164 static void mgc_notify_active(struct obd_device *unused)
1166 /* wakeup mgc_requeue_thread to requeue mgc lock */
1167 spin_lock(&config_list_lock);
1169 spin_unlock(&config_list_lock);
1172 /* TODO: Help the MGS rebuild nidtbl. -jay */
1175 /* Send target_reg message to MGS */
1176 static int mgc_target_register(struct obd_export *exp,
1177 struct mgs_target_info *mti)
1179 struct ptlrpc_request *req;
1180 struct mgs_target_info *req_mti, *rep_mti;
1184 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1185 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1190 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1192 ptlrpc_req_finished(req);
1196 memcpy(req_mti, mti, sizeof(*req_mti));
1197 ptlrpc_request_set_replen(req);
1198 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1199 /* Limit how long we will wait for the enqueue to complete */
1200 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1202 /* if the target needs to regenerate the config log in MGS, it's better
1203 * to use some longer limit to let MGC have time to change connection to
1204 * another MGS (or try again with the same MGS) for the target (server)
1205 * will fail and exit if the request expired due to delay limit. */
1206 if (mti->mti_flags & (LDD_F_UPDATE | LDD_F_NEED_INDEX))
1207 req->rq_delay_limit = MGC_TARGET_REG_LIMIT_MAX;
1209 rc = ptlrpc_queue_wait(req);
1211 rep_mti = req_capsule_server_get(&req->rq_pill,
1212 &RMF_MGS_TARGET_INFO);
1213 memcpy(mti, rep_mti, sizeof(*rep_mti));
1214 CDEBUG(D_MGC, "register %s got index = %d\n",
1215 mti->mti_svname, mti->mti_stripe_index);
1217 ptlrpc_req_finished(req);
1222 static int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1223 u32 keylen, void *key,
1224 u32 vallen, void *val,
1225 struct ptlrpc_request_set *set)
1230 /* Turn off initial_recov after we try all backup servers once */
1231 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1232 struct obd_import *imp = class_exp2cliimp(exp);
1234 if (vallen != sizeof(int))
1236 value = *(int *)val;
1237 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1238 imp->imp_obd->obd_name, value,
1239 imp->imp_deactive, imp->imp_invalid,
1240 imp->imp_replayable, imp->imp_obd->obd_replayable,
1241 ptlrpc_import_state_name(imp->imp_state));
1242 /* Resurrect the import immediately if
1243 * 1. we previously got disconnected,
1244 * 2. value > 1 (at the same node with MGS)
1246 if (imp->imp_state == LUSTRE_IMP_DISCON || value > 1)
1247 ptlrpc_reconnect_import(imp);
1252 /* FIXME move this to mgc_process_config */
1253 if (KEY_IS(KEY_REGISTER_TARGET)) {
1254 struct mgs_target_info *mti;
1255 if (vallen != sizeof(struct mgs_target_info))
1257 mti = (struct mgs_target_info *)val;
1258 CDEBUG(D_MGC, "register_target %s %#x\n",
1259 mti->mti_svname, mti->mti_flags);
1260 rc = mgc_target_register(exp, mti);
1263 if (KEY_IS(KEY_SET_FS)) {
1264 struct super_block *sb = (struct super_block *)val;
1266 if (vallen != sizeof(struct super_block))
1269 rc = mgc_fs_setup(env, exp->exp_obd, sb);
1272 if (KEY_IS(KEY_CLEAR_FS)) {
1275 rc = mgc_fs_cleanup(env, exp->exp_obd);
1278 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
1279 if (KEY_IS(KEY_SET_INFO)) {
1280 struct mgs_send_param *msp;
1282 msp = (struct mgs_send_param *)val;
1283 rc = mgc_set_mgs_param(exp, msp);
1287 if (KEY_IS(KEY_MGSSEC)) {
1288 struct client_obd *cli = &exp->exp_obd->u.cli;
1289 struct sptlrpc_flavor flvr;
1292 * empty string means using current flavor, if which haven't
1293 * been set yet, set it as null.
1295 * if flavor has been set previously, check the asking flavor
1296 * must match the existing one.
1299 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1305 rc = sptlrpc_parse_flavor(val, &flvr);
1307 CERROR("invalid sptlrpc flavor %s to MGS\n",
1313 * caller already hold a mutex
1315 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1316 cli->cl_flvr_mgc = flvr;
1317 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1318 sizeof(flvr)) != 0) {
1321 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1323 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1324 "currently %s is in use\n",
1334 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1335 __u32 keylen, void *key, __u32 *vallen, void *val)
1339 if (KEY_IS(KEY_CONN_DATA)) {
1340 struct obd_import *imp = class_exp2cliimp(exp);
1341 struct obd_connect_data *data = val;
1343 if (*vallen == sizeof(*data)) {
1344 *data = imp->imp_connect_data;
1352 static int mgc_import_event(struct obd_device *obd,
1353 struct obd_import *imp,
1354 enum obd_import_event event)
1358 LASSERT(imp->imp_obd == obd);
1359 CDEBUG(D_MGC, "import event %#x\n", event);
1362 case IMP_EVENT_DISCON:
1363 /* MGC imports should not wait for recovery */
1364 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1365 ptlrpc_pinger_ir_down();
1367 case IMP_EVENT_INACTIVE:
1369 case IMP_EVENT_INVALIDATE: {
1370 struct ldlm_namespace *ns = obd->obd_namespace;
1371 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1374 case IMP_EVENT_ACTIVE:
1375 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1376 /* Clearing obd_no_recov allows us to continue pinging */
1377 obd->obd_no_recov = 0;
1378 mgc_notify_active(obd);
1379 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1380 ptlrpc_pinger_ir_up();
1384 case IMP_EVENT_DEACTIVATE:
1385 case IMP_EVENT_ACTIVATE:
1388 CERROR("Unknown import event %#x\n", event);
1395 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_SHIFT),
1396 CONFIG_READ_NRPAGES = 4
1399 static int mgc_apply_recover_logs(struct obd_device *mgc,
1400 struct config_llog_data *cld,
1402 void *data, int datalen, bool mne_swab)
1404 struct config_llog_instance *cfg = &cld->cld_cfg;
1405 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1406 struct mgs_nidtbl_entry *entry;
1407 struct lustre_cfg *lcfg;
1408 struct lustre_cfg_bufs bufs;
1409 u64 prev_version = 0;
1418 LASSERT(cfg->cfg_instance != 0);
1419 LASSERT(ll_get_cfg_instance(cfg->cfg_sb) == cfg->cfg_instance);
1421 OBD_ALLOC(inst, PAGE_SIZE);
1425 if (!IS_SERVER(lsi)) {
1426 pos = snprintf(inst, PAGE_SIZE, "%016lx", cfg->cfg_instance);
1427 if (pos >= PAGE_SIZE) {
1428 OBD_FREE(inst, PAGE_SIZE);
1431 #ifdef HAVE_SERVER_SUPPORT
1433 LASSERT(IS_MDT(lsi));
1434 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1437 OBD_FREE(inst, PAGE_SIZE);
1441 #endif /* HAVE_SERVER_SUPPORT */
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 OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount,
1589 lustre_cfg_init(lcfg, LCFG_PARAM, &bufs);
1591 CDEBUG(D_INFO, "ir apply logs %lld/%lld for %s -> %s\n",
1592 prev_version, max_version, obdname, params);
1594 rc = class_process_config(lcfg);
1595 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
1596 lcfg->lcfg_buflens));
1598 CDEBUG(D_INFO, "process config for %s error %d\n",
1601 /* continue, even one with error */
1604 OBD_FREE(inst, PAGE_SIZE);
1609 * This function is called if this client was notified for target restarting
1610 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery or
1613 static int mgc_process_recover_nodemap_log(struct obd_device *obd,
1614 struct config_llog_data *cld)
1616 struct ptlrpc_connection *mgc_conn;
1617 struct ptlrpc_request *req = NULL;
1618 struct config_llog_instance *cfg = &cld->cld_cfg;
1619 struct mgs_config_body *body;
1620 struct mgs_config_res *res;
1621 struct nodemap_config *new_config = NULL;
1622 struct lu_nodemap *recent_nodemap = NULL;
1623 struct ptlrpc_bulk_desc *desc;
1624 struct page **pages = NULL;
1625 __u64 config_read_offset = 0;
1626 __u8 nodemap_cur_pass = 0;
1629 bool mne_swab = false;
1635 mgc_conn = class_exp2cliimp(cld->cld_mgcexp)->imp_connection;
1637 /* don't need to get local config */
1638 if (cld_is_nodemap(cld) &&
1639 (LNET_NETTYP(LNET_NIDNET(mgc_conn->c_peer.nid)) == LOLND))
1642 /* allocate buffer for bulk transfer.
1643 * if this is the first time for this mgs to read logs,
1644 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1645 * once; otherwise, it only reads increment of logs, this should be
1646 * small and CONFIG_READ_NRPAGES will be used.
1648 nrpages = CONFIG_READ_NRPAGES;
1649 if (cfg->cfg_last_idx == 0 || cld_is_nodemap(cld))
1650 nrpages = CONFIG_READ_NRPAGES_INIT;
1652 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1654 GOTO(out, rc = -ENOMEM);
1656 for (i = 0; i < nrpages; i++) {
1657 pages[i] = alloc_page(GFP_KERNEL);
1658 if (pages[i] == NULL)
1659 GOTO(out, rc = -ENOMEM);
1663 #ifdef HAVE_SERVER_SUPPORT
1664 if (cld_is_nodemap(cld) && config_read_offset == 0) {
1665 new_config = nodemap_config_alloc();
1666 if (IS_ERR(new_config)) {
1667 rc = PTR_ERR(new_config);
1673 LASSERT(cld_is_recover(cld) || cld_is_nodemap(cld));
1674 LASSERT(mutex_is_locked(&cld->cld_lock));
1675 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1676 &RQF_MGS_CONFIG_READ);
1678 GOTO(out, rc = -ENOMEM);
1680 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1685 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1686 LASSERT(body != NULL);
1687 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1688 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1689 >= sizeof(body->mcb_name))
1690 GOTO(out, rc = -E2BIG);
1691 if (cld_is_nodemap(cld))
1692 body->mcb_offset = config_read_offset;
1694 body->mcb_offset = cfg->cfg_last_idx + 1;
1695 body->mcb_type = cld->cld_type;
1696 body->mcb_bits = PAGE_SHIFT;
1697 body->mcb_units = nrpages;
1698 body->mcb_nm_cur_pass = nodemap_cur_pass;
1700 /* allocate bulk transfer descriptor */
1701 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1,
1702 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1704 &ptlrpc_bulk_kiov_pin_ops);
1706 GOTO(out, rc = -ENOMEM);
1708 for (i = 0; i < nrpages; i++)
1709 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1712 ptlrpc_request_set_replen(req);
1713 rc = ptlrpc_queue_wait(req);
1717 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1719 GOTO(out, rc = -EPROTO);
1721 if (cld_is_nodemap(cld)) {
1722 config_read_offset = res->mcr_offset;
1723 eof = config_read_offset == II_END_OFF;
1724 nodemap_cur_pass = res->mcr_nm_cur_pass;
1726 if (res->mcr_size < res->mcr_offset)
1727 GOTO(out, rc = -EINVAL);
1729 /* always update the index even though it might have errors with
1730 * handling the recover logs
1732 cfg->cfg_last_idx = res->mcr_offset;
1733 eof = res->mcr_offset == res->mcr_size;
1735 CDEBUG(D_INFO, "Latest version %lld, more %d.\n",
1736 res->mcr_offset, eof == false);
1739 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1741 GOTO(out, rc = ealen);
1743 if (ealen > nrpages << PAGE_SHIFT)
1744 GOTO(out, rc = -EINVAL);
1746 if (ealen == 0) { /* no logs transferred */
1747 #ifdef HAVE_SERVER_SUPPORT
1748 /* config changed since first read RPC */
1749 if (cld_is_nodemap(cld) && config_read_offset == 0) {
1750 CDEBUG(D_INFO, "nodemap config changed in transit, retrying\n");
1751 GOTO(out, rc = -EAGAIN);
1759 mne_swab = ptlrpc_rep_need_swab(req);
1761 /* When a nodemap config is received, we build a new nodemap config,
1762 * with new nodemap structs. We keep track of the most recently added
1763 * nodemap since the config is read ordered by nodemap_id, and so it
1764 * is likely that the next record will be related. Because access to
1765 * the nodemaps is single threaded until the nodemap_config is active,
1766 * we don't need to reference count with recent_nodemap, though
1767 * recent_nodemap should be set to NULL when the nodemap_config
1768 * is either destroyed or set active.
1770 for (i = 0; i < nrpages && ealen > 0; i++) {
1774 ptr = kmap(pages[i]);
1775 if (cld_is_nodemap(cld))
1776 rc2 = nodemap_process_idx_pages(new_config, ptr,
1779 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset,
1786 CWARN("%s: error processing %s log %s: rc = %d\n",
1788 cld_is_nodemap(cld) ? "nodemap" : "recovery",
1791 GOTO(out, rc = rc2);
1799 ptlrpc_req_finished(req);
1803 if (rc == 0 && !eof)
1806 #ifdef HAVE_SERVER_SUPPORT
1807 if (new_config != NULL) {
1808 /* recent_nodemap cannot be used after set_active/dealloc */
1810 nodemap_config_set_active_mgc(new_config);
1812 nodemap_config_dealloc(new_config);
1817 for (i = 0; i < nrpages; i++) {
1818 if (pages[i] == NULL)
1820 __free_page(pages[i]);
1822 OBD_FREE(pages, sizeof(*pages) * nrpages);
1827 static int mgc_barrier_glimpse_ast(struct ldlm_lock *lock, void *data)
1829 struct config_llog_data *cld = lock->l_ast_data;
1833 if (cld->cld_stopping)
1836 rc = barrier_handler(s2lsi(cld->cld_cfg.cfg_sb)->lsi_dt_dev,
1837 (struct ptlrpc_request *)data);
1842 /* Copy a remote log locally */
1843 static int mgc_llog_local_copy(const struct lu_env *env,
1844 struct obd_device *obd,
1845 struct llog_ctxt *rctxt,
1846 struct llog_ctxt *lctxt, char *logname)
1854 * - copy it to backup using llog_backup()
1855 * - copy remote llog to logname using llog_backup()
1856 * - if failed then move bakup to logname again
1859 OBD_ALLOC(temp_log, strlen(logname) + 2);
1862 sprintf(temp_log, "%sT", logname);
1864 /* make a copy of local llog at first */
1865 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1866 if (rc < 0 && rc != -ENOENT)
1868 /* copy remote llog to the local copy */
1869 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1870 if (rc == -ENOENT) {
1871 /* no remote llog, delete local one too */
1872 llog_erase(env, lctxt, NULL, logname);
1873 } else if (rc < 0) {
1874 /* error during backup, get local one back from the copy */
1875 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1877 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1878 obd->obd_name, logname, rc);
1880 llog_erase(env, lctxt, NULL, temp_log);
1881 OBD_FREE(temp_log, strlen(logname) + 2);
1885 /* local_only means it cannot get remote llogs */
1886 static int mgc_process_cfg_log(struct obd_device *mgc,
1887 struct config_llog_data *cld, int local_only)
1889 struct llog_ctxt *ctxt, *lctxt = NULL;
1890 struct client_obd *cli = &mgc->u.cli;
1891 struct lustre_sb_info *lsi = NULL;
1898 LASSERT(mutex_is_locked(&cld->cld_lock));
1900 if (cld->cld_cfg.cfg_sb)
1901 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1907 rc = lu_env_init(env, LCT_MG_THREAD);
1911 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1914 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1916 /* Copy the setup log locally if we can. Don't mess around if we're
1917 * running an MGS though (logs are already local). */
1918 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1919 cli->cl_mgc_configs_dir != NULL &&
1920 lu2dt_dev(cli->cl_mgc_configs_dir->do_lu.lo_dev) ==
1922 if (!local_only && !lsi->lsi_dt_dev->dd_rdonly)
1923 /* Only try to copy log if we have the lock. */
1924 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1926 if (local_only || rc) {
1927 if (strcmp(cld->cld_logname, PARAMS_FILENAME) != 0 &&
1928 llog_is_empty(env, lctxt, cld->cld_logname)) {
1929 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1930 "log %s and no local copy."
1931 "\n", cld->cld_logname);
1932 GOTO(out_pop, rc = -ENOENT);
1934 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1935 "copy for now, will try to update later.\n",
1939 /* Now, whether we copied or not, start using the local llog.
1940 * If we failed to copy, we'll start using whatever the old
1942 llog_ctxt_put(ctxt);
1946 if (local_only) /* no local log at client side */
1947 GOTO(out_pop, rc = -EIO);
1951 if (lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1952 lsi->lsi_dt_dev->dd_rdonly) {
1953 struct llog_ctxt *rctxt;
1955 /* Under readonly mode, we may have no local copy or local
1956 * copy is incomplete, so try to use remote llog firstly. */
1957 rctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1960 rc = class_config_parse_llog(env, rctxt, cld->cld_logname,
1962 llog_ctxt_put(rctxt);
1965 if (rc && rc != -ENOENT)
1966 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1970 * update settings on existing OBDs.
1971 * the logname must be <fsname>-sptlrpc
1973 if (rc == 0 && cld_is_sptlrpc(cld))
1974 class_notify_sptlrpc_conf(cld->cld_logname,
1975 strlen(cld->cld_logname) -
1976 strlen("-sptlrpc"));
1980 __llog_ctxt_put(env, ctxt);
1982 __llog_ctxt_put(env, lctxt);
1990 static bool mgc_import_in_recovery(struct obd_import *imp)
1992 bool in_recovery = true;
1994 spin_lock(&imp->imp_lock);
1995 if (imp->imp_state == LUSTRE_IMP_FULL ||
1996 imp->imp_state == LUSTRE_IMP_CLOSED)
1997 in_recovery = false;
1998 spin_unlock(&imp->imp_lock);
2004 * Get a configuration log from the MGS and process it.
2006 * This function is called for both clients and servers to process the
2007 * configuration log from the MGS. The MGC enqueues a DLM lock on the
2008 * log from the MGS, and if the lock gets revoked the MGC will be notified
2009 * by the lock cancellation callback that the config log has changed,
2010 * and will enqueue another MGS lock on it, and then continue processing
2011 * the new additions to the end of the log.
2013 * Since the MGC import is not replayable, if the import is being evicted
2014 * (rcl == -ESHUTDOWN, \see ptlrpc_import_delay_req()), retry to process
2015 * the log until recovery is finished or the import is closed.
2017 * Make a local copy of the log before parsing it if appropriate (non-MGS
2018 * server) so that the server can start even when the MGS is down.
2020 * There shouldn't be multiple processes running process_log at once --
2021 * sounds like badness. It actually might be fine, as long as they're not
2022 * trying to update from the same log simultaneously, in which case we
2023 * should use a per-log semaphore instead of cld_lock.
2025 * \param[in] mgc MGC device by which to fetch the configuration log
2026 * \param[in] cld log processing state (stored in lock callback data)
2028 * \retval 0 on success
2029 * \retval negative errno on failure
2031 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
2033 struct lustre_handle lockh = { 0 };
2034 __u64 flags = LDLM_FL_NO_LRU;
2039 LASSERT(cld != NULL);
2041 /* I don't want multiple processes running process_log at once --
2042 sounds like badness. It actually might be fine, as long as
2043 we're not trying to update from the same log
2044 simultaneously (in which case we should use a per-log sem.) */
2046 mutex_lock(&cld->cld_lock);
2047 if (cld->cld_stopping) {
2048 mutex_unlock(&cld->cld_lock);
2052 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
2054 CDEBUG(D_MGC, "Process log %s-%016lx from %d\n", cld->cld_logname,
2055 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
2057 /* Get the cfg lock on the llog */
2058 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, LDLM_PLAIN, NULL,
2060 cld_is_barrier(cld) ? mgc_barrier_glimpse_ast : NULL,
2061 cld, 0, NULL, &lockh);
2063 /* Get the cld, it will be released in mgc_blocking_ast. */
2064 config_log_get(cld);
2065 rc = ldlm_lock_set_data(&lockh, (void *)cld);
2068 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
2070 if (rcl == -ESHUTDOWN &&
2071 atomic_read(&mgc->u.cli.cl_mgc_refcount) > 0 && !retry) {
2072 struct obd_import *imp;
2073 struct l_wait_info lwi;
2074 long timeout = cfs_time_seconds(obd_timeout);
2076 mutex_unlock(&cld->cld_lock);
2077 imp = class_exp2cliimp(mgc->u.cli.cl_mgc_mgsexp);
2079 /* Let's force the pinger, and wait the import to be
2080 * connected, note: since mgc import is non-replayable,
2081 * and even the import state is disconnected, it does
2082 * not mean the "recovery" is stopped, so we will keep
2083 * waitting until timeout or the import state is
2085 ptlrpc_pinger_force(imp);
2087 lwi = LWI_TIMEOUT(timeout, NULL, NULL);
2088 l_wait_event(imp->imp_recovery_waitq,
2089 !mgc_import_in_recovery(imp), &lwi);
2091 if (imp->imp_state == LUSTRE_IMP_FULL) {
2095 mutex_lock(&cld->cld_lock);
2096 /* unlock/lock mutex, so check stopping again */
2097 if (cld->cld_stopping) {
2098 mutex_unlock(&cld->cld_lock);
2101 spin_lock(&config_list_lock);
2102 cld->cld_lostlock = 1;
2103 spin_unlock(&config_list_lock);
2106 /* mark cld_lostlock so that it will requeue
2107 * after MGC becomes available. */
2108 spin_lock(&config_list_lock);
2109 cld->cld_lostlock = 1;
2110 spin_unlock(&config_list_lock);
2114 if (cld_is_recover(cld) || cld_is_nodemap(cld)) {
2116 rc = mgc_process_recover_nodemap_log(mgc, cld);
2117 else if (cld_is_nodemap(cld))
2120 if (cld_is_recover(cld) && rc) {
2122 CERROR("%s: recover log %s failed, not fatal: rc = %d\n",
2123 mgc->obd_name, cld->cld_logname, rc);
2124 spin_lock(&config_list_lock);
2125 cld->cld_lostlock = 1;
2126 spin_unlock(&config_list_lock);
2128 rc = 0; /* this is not a fatal error for recover log */
2130 } else if (!cld_is_barrier(cld)) {
2131 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
2134 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
2135 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
2137 mutex_unlock(&cld->cld_lock);
2139 /* Now drop the lock so MGS can revoke it */
2141 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, LCK_CR, &lockh);
2143 CERROR("Can't drop cfg lock: %d\n", rcl);
2146 /* requeue nodemap lock immediately if transfer was interrupted */
2147 if (cld_is_nodemap(cld) && rc == -EAGAIN) {
2148 mgc_requeue_add(cld);
2156 /** Called from lustre_process_log.
2157 * LCFG_LOG_START gets the config log from the MGS, processes it to start
2158 * any services, and adds it to the list logs to watch (follow).
2160 static int mgc_process_config(struct obd_device *obd, size_t len, void *buf)
2162 struct lustre_cfg *lcfg = buf;
2163 struct config_llog_instance *cfg = NULL;
2168 switch(lcfg->lcfg_command) {
2169 case LCFG_LOV_ADD_OBD: {
2170 /* Overloading this cfg command: register a new target */
2171 struct mgs_target_info *mti;
2173 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
2174 sizeof(struct mgs_target_info))
2175 GOTO(out, rc = -EINVAL);
2177 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
2178 CDEBUG(D_MGC, "add_target %s %#x\n",
2179 mti->mti_svname, mti->mti_flags);
2180 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
2183 case LCFG_LOV_DEL_OBD:
2184 /* Unregister has no meaning at the moment. */
2185 CERROR("lov_del_obd unimplemented\n");
2188 case LCFG_SPTLRPC_CONF: {
2189 rc = sptlrpc_process_config(lcfg);
2192 case LCFG_LOG_START: {
2193 struct config_llog_data *cld;
2194 struct super_block *sb;
2196 logname = lustre_cfg_string(lcfg, 1);
2197 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
2198 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
2200 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
2203 /* We're only called through here on the initial mount */
2204 cld = config_log_add(obd, logname, cfg, sb);
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, false, 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);