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 (%p) refs %d\n", cld->cld_logname, cld,
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(%p) refs %d\n", cld->cld_logname, cld,
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 cld->cld_stopping = 1;
161 sptlrpc_conf_log_stop(cld->cld_logname);
164 class_export_put(cld->cld_mgcexp);
165 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
171 /* Find a config log by name */
173 struct config_llog_data *config_log_find(char *logname,
174 struct config_llog_instance *cfg)
176 struct config_llog_data *cld;
177 struct config_llog_data *found = NULL;
178 unsigned long cfg_instance;
181 LASSERT(logname != NULL);
183 cfg_instance = cfg ? cfg->cfg_instance : 0;
184 spin_lock(&config_list_lock);
185 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
186 /* check if cfg_instance is the one we want */
187 if (cfg_instance != cld->cld_cfg.cfg_instance)
190 /* instance may be NULL, should check name */
191 if (strcmp(logname, cld->cld_logname) == 0) {
193 config_log_get(found);
197 spin_unlock(&config_list_lock);
202 struct config_llog_data *do_config_log_add(struct obd_device *obd,
205 struct config_llog_instance *cfg,
206 struct super_block *sb)
208 struct config_llog_data *cld;
213 CDEBUG(D_MGC, "do adding config log %s-%016lx\n", logname,
214 cfg ? cfg->cfg_instance : 0);
216 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
218 RETURN(ERR_PTR(-ENOMEM));
220 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
222 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
226 strcpy(cld->cld_logname, logname);
230 cld->cld_cfg.cfg_callback = class_config_llog_handler;
231 mutex_init(&cld->cld_lock);
232 cld->cld_cfg.cfg_last_idx = 0;
233 cld->cld_cfg.cfg_flags = 0;
234 cld->cld_cfg.cfg_sb = sb;
235 cld->cld_type = type;
236 atomic_set(&cld->cld_refcount, 1);
238 /* Keep the mgc around until we are done */
239 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
241 if (cld_is_sptlrpc(cld))
242 sptlrpc_conf_log_start(logname);
244 spin_lock(&config_list_lock);
245 list_add(&cld->cld_list_chain, &config_llog_list);
246 spin_unlock(&config_list_lock);
248 if (cld_is_sptlrpc(cld) || cld_is_nodemap(cld) || cld_is_barrier(cld)) {
249 rc = mgc_process_log(obd, cld);
250 if (rc && rc != -ENOENT)
251 CERROR("%s: failed processing log, type %d: rc = %d\n",
252 obd->obd_name, type, rc);
258 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
260 struct config_llog_instance *cfg,
261 struct super_block *sb)
263 struct config_llog_instance lcfg = *cfg;
264 struct lustre_sb_info *lsi = s2lsi(sb);
265 struct config_llog_data *cld;
271 /* for osp-on-ost, see lustre_start_osp() */
272 if (IS_MDT(lsi) && lcfg.cfg_instance)
275 /* We have to use different llog for clients and MDTs for DNE,
276 * where only clients are notified if one of DNE server restarts.
278 LASSERT(strlen(fsname) < sizeof(logname) / 2);
279 strncpy(logname, fsname, sizeof(logname));
280 if (IS_SERVER(lsi)) { /* mdt */
281 LASSERT(lcfg.cfg_instance == 0);
282 lcfg.cfg_instance = ll_get_cfg_instance(sb);
283 strncat(logname, "-mdtir", sizeof(logname));
285 LASSERT(lcfg.cfg_instance != 0);
286 strncat(logname, "-cliir", sizeof(logname));
289 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
293 static struct config_llog_data *config_log_find_or_add(struct obd_device *obd,
294 char *logname, struct super_block *sb, int type,
295 struct config_llog_instance *cfg)
297 struct config_llog_instance lcfg = *cfg;
298 struct config_llog_data *cld;
300 /* Note class_config_llog_handler() depends on getting "obd" back */
301 lcfg.cfg_instance = sb ? ll_get_cfg_instance(sb) : (unsigned long)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, "add config log %s-%016lx\n", logname,
335 * for each regular log, the depended sptlrpc log name is
336 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
338 ptr = strrchr(logname, '-');
339 if (ptr == NULL || ptr - logname > 8) {
340 CERROR("logname %s is too long\n", logname);
341 RETURN(ERR_PTR(-EINVAL));
344 memcpy(seclogname, logname, ptr - logname);
345 strcpy(seclogname + (ptr - logname), "-sptlrpc");
347 if (cfg->cfg_sub_clds & CONFIG_SUB_SPTLRPC) {
348 sptlrpc_cld = config_log_find_or_add(obd, seclogname, NULL,
349 CONFIG_T_SPTLRPC, cfg);
350 if (IS_ERR(sptlrpc_cld)) {
351 CERROR("%s: can't create sptlrpc log %s: rc = %ld\n",
352 obd->obd_name, seclogname, PTR_ERR(sptlrpc_cld));
357 if (!IS_MGS(lsi) && cfg->cfg_sub_clds & CONFIG_SUB_NODEMAP) {
358 nodemap_cld = config_log_find_or_add(obd, LUSTRE_NODEMAP_NAME,
359 NULL, CONFIG_T_NODEMAP,
361 if (IS_ERR(nodemap_cld)) {
362 rc = PTR_ERR(nodemap_cld);
363 CERROR("%s: cannot create nodemap log: rc = %d\n",
365 GOTO(out_sptlrpc, rc);
369 if (cfg->cfg_sub_clds & CONFIG_SUB_PARAMS) {
370 params_cld = config_log_find_or_add(obd, PARAMS_FILENAME, sb,
371 CONFIG_T_PARAMS, cfg);
372 if (IS_ERR(params_cld)) {
373 rc = PTR_ERR(params_cld);
374 CERROR("%s: can't create params log: rc = %d\n",
376 GOTO(out_nodemap, rc);
380 if (IS_MDT(s2lsi(sb)) && cfg->cfg_sub_clds & CONFIG_SUB_BARRIER) {
381 snprintf(seclogname + (ptr - logname), sizeof(seclogname) - 1,
382 "-%s", BARRIER_FILENAME);
383 barrier_cld = config_log_find_or_add(obd, seclogname, sb,
384 CONFIG_T_BARRIER, cfg);
385 if (IS_ERR(barrier_cld)) {
386 rc = PTR_ERR(barrier_cld);
387 CERROR("%s: can't create barrier log: rc = %d\n",
389 GOTO(out_params, rc);
393 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
396 CERROR("%s: can't create log: rc = %d\n",
398 GOTO(out_barrier, rc = PTR_ERR(cld));
401 LASSERT(lsi->lsi_lmd);
402 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR) &&
403 cfg->cfg_sub_clds & CONFIG_SUB_RECOVER) {
404 struct config_llog_data *recover_cld;
406 ptr = strrchr(seclogname, '-');
410 CERROR("%s: sptlrpc log name not correct, %s: "
411 "rc = %d\n", obd->obd_name, seclogname, -EINVAL);
412 GOTO(out_cld, rc = -EINVAL);
415 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
416 if (IS_ERR(recover_cld)) {
417 rc = PTR_ERR(recover_cld);
418 CERROR("%s: can't create recover log: rc = %d\n",
423 mutex_lock(&cld->cld_lock);
425 cld->cld_recover = recover_cld;
429 mutex_lock(&cld->cld_lock);
430 cld->cld_params = params_cld;
431 cld->cld_barrier = barrier_cld;
432 cld->cld_nodemap = nodemap_cld;
433 cld->cld_sptlrpc = sptlrpc_cld;
434 mutex_unlock(&cld->cld_lock);
441 config_log_put(barrier_cld);
443 config_log_put(params_cld);
445 config_log_put(nodemap_cld);
447 config_log_put(sptlrpc_cld);
452 DEFINE_MUTEX(llog_process_lock);
454 static inline void config_mark_cld_stop_nolock(struct config_llog_data *cld)
458 spin_lock(&config_list_lock);
459 cld->cld_stopping = 1;
460 spin_unlock(&config_list_lock);
462 CDEBUG(D_INFO, "lockh %#llx\n", cld->cld_lockh.cookie);
463 if (!ldlm_lock_addref_try(&cld->cld_lockh, LCK_CR))
464 ldlm_lock_decref_and_cancel(&cld->cld_lockh, LCK_CR);
467 static inline void config_mark_cld_stop(struct config_llog_data *cld)
470 mutex_lock(&cld->cld_lock);
471 config_mark_cld_stop_nolock(cld);
472 mutex_unlock(&cld->cld_lock);
476 /** Stop watching for updates on this log.
478 static int config_log_end(char *logname, struct config_llog_instance *cfg)
480 struct config_llog_data *cld;
481 struct config_llog_data *cld_sptlrpc = NULL;
482 struct config_llog_data *cld_params = NULL;
483 struct config_llog_data *cld_recover = NULL;
484 struct config_llog_data *cld_nodemap = NULL;
485 struct config_llog_data *cld_barrier = NULL;
490 cld = config_log_find(logname, cfg);
494 mutex_lock(&cld->cld_lock);
496 * if cld_stopping is set, it means we didn't start the log thus
497 * not owning the start ref. this can happen after previous umount:
498 * the cld still hanging there waiting for lock cancel, and we
499 * remount again but failed in the middle and call log_end without
502 if (unlikely(cld->cld_stopping)) {
503 mutex_unlock(&cld->cld_lock);
504 /* drop the ref from the find */
509 cld_recover = cld->cld_recover;
510 cld->cld_recover = NULL;
511 cld_params = cld->cld_params;
512 cld->cld_params = NULL;
513 cld_nodemap = cld->cld_nodemap;
514 cld->cld_nodemap = NULL;
515 cld_barrier = cld->cld_barrier;
516 cld->cld_barrier = NULL;
517 cld_sptlrpc = cld->cld_sptlrpc;
518 cld->cld_sptlrpc = NULL;
520 config_mark_cld_stop_nolock(cld);
521 mutex_unlock(&cld->cld_lock);
523 config_mark_cld_stop(cld_recover);
524 config_log_put(cld_recover);
525 config_mark_cld_stop(cld_params);
526 config_log_put(cld_params);
527 config_mark_cld_stop(cld_barrier);
528 config_log_put(cld_barrier);
529 /* don't explicitly set cld_stopping on sptlrpc lock here, as other
530 * targets may be active, it will be done in config_log_put if necessary
532 config_log_put(cld_sptlrpc);
533 /* don't set cld_stopping on nm lock as other targets may be active */
534 config_log_put(cld_nodemap);
536 /* drop the ref from the find */
538 /* drop the start ref */
541 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
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;
556 with_imp_locked(obd, imp, rc) {
557 ocd = &imp->imp_connect_data;
559 seq_printf(m, "imperative_recovery: %s\n",
560 OCD_HAS_FLAG(ocd, IMP_RECOV) ?
561 "ENABLED" : "DISABLED");
566 seq_printf(m, "client_state:\n");
568 spin_lock(&config_list_lock);
569 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
570 if (cld->cld_recover == NULL)
572 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
574 cld->cld_recover->cld_cfg.cfg_last_idx);
576 spin_unlock(&config_list_lock);
581 /* reenqueue any lost locks */
582 #define RQ_RUNNING 0x1
586 #define RQ_PRECLEANUP 0x10
587 static int rq_state = 0;
588 static wait_queue_head_t rq_waitq;
589 static DECLARE_COMPLETION(rq_exit);
590 static DECLARE_COMPLETION(rq_start);
592 static void do_requeue(struct config_llog_data *cld)
597 LASSERT(atomic_read(&cld->cld_refcount) > 0);
600 * Do not run mgc_process_log on a disconnected export or an
601 * export which is being disconnected. Take the client
602 * semaphore to make the check non-racy.
604 down_read_nested(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem,
606 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
607 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
608 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
609 if (rc && rc != -ENOENT)
610 CERROR("failed processing log: %d\n", rc);
612 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
615 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
620 /* this timeout represents how many seconds MGC should wait before
621 * requeue config and recover lock to the MGS. We need to randomize this
622 * in order to not flood the MGS.
624 #define MGC_TIMEOUT_MIN_SECONDS 5
625 #define MGC_TIMEOUT_RAND_CENTISEC 500
627 static int mgc_requeue_thread(void *data)
633 CDEBUG(D_MGC, "Starting requeue thread\n");
635 /* Keep trying failed locks periodically */
636 spin_lock(&config_list_lock);
637 rq_state |= RQ_RUNNING;
638 while (!(rq_state & RQ_STOP)) {
639 struct config_llog_data *cld, *cld_prev;
640 int rand = prandom_u32_max(MGC_TIMEOUT_RAND_CENTISEC);
643 /* Any new or requeued lostlocks will change the state */
644 rq_state &= ~(RQ_NOW | RQ_LATER);
645 spin_unlock(&config_list_lock);
652 /* Always wait a few seconds to allow the server who
653 * caused the lock revocation to finish its setup, plus some
654 * random so everyone doesn't try to reconnect at once.
656 to = cfs_time_seconds(MGC_TIMEOUT_MIN_SECONDS * 100 + rand);
657 /* rand is centi-seconds */
658 wait_event_idle_timeout(rq_waitq,
659 rq_state & (RQ_STOP | RQ_PRECLEANUP),
663 * iterate & processing through the list. for each cld, process
664 * its depending sptlrpc cld firstly (if any) and then itself.
666 * it's guaranteed any item in the list must have
667 * reference > 0; and if cld_lostlock is set, at
668 * least one reference is taken by the previous enqueue.
672 spin_lock(&config_list_lock);
673 rq_state &= ~RQ_PRECLEANUP;
674 list_for_each_entry(cld, &config_llog_list,
676 if (!cld->cld_lostlock || cld->cld_stopping)
679 /* hold reference to avoid being freed during
680 * subsequent processing. */
682 cld->cld_lostlock = 0;
683 spin_unlock(&config_list_lock);
685 config_log_put(cld_prev);
688 if (likely(!(rq_state & RQ_STOP))) {
690 spin_lock(&config_list_lock);
692 spin_lock(&config_list_lock);
696 spin_unlock(&config_list_lock);
697 config_log_put(cld_prev);
699 /* Wait a bit to see if anyone else needs a requeue */
700 wait_event_idle(rq_waitq, rq_state & (RQ_NOW | RQ_STOP));
701 spin_lock(&config_list_lock);
704 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
705 rq_state &= ~RQ_RUNNING;
706 spin_unlock(&config_list_lock);
710 CDEBUG(D_MGC, "Ending requeue thread\n");
714 /* Add a cld to the list to requeue. Start the requeue thread if needed.
715 We are responsible for dropping the config log reference from here on out. */
716 static void mgc_requeue_add(struct config_llog_data *cld)
721 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
722 cld->cld_logname, atomic_read(&cld->cld_refcount),
723 cld->cld_stopping, rq_state);
724 LASSERT(atomic_read(&cld->cld_refcount) > 0);
726 /* lets cancel an existent lock to mark cld as "lostlock" */
727 CDEBUG(D_INFO, "lockh %#llx\n", cld->cld_lockh.cookie);
728 if (!ldlm_lock_addref_try(&cld->cld_lockh, LCK_CR))
729 ldlm_lock_decref_and_cancel(&cld->cld_lockh, LCK_CR);
731 mutex_lock(&cld->cld_lock);
732 spin_lock(&config_list_lock);
733 if (!(rq_state & RQ_STOP) && !cld->cld_stopping) {
734 cld->cld_lostlock = 1;
738 spin_unlock(&config_list_lock);
739 mutex_unlock(&cld->cld_lock);
746 /********************** class fns **********************/
747 static int mgc_local_llog_init(const struct lu_env *env,
748 struct obd_device *obd,
749 struct obd_device *disk)
751 struct llog_ctxt *ctxt;
756 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
761 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
763 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
769 static int mgc_local_llog_fini(const struct lu_env *env,
770 struct obd_device *obd)
772 struct llog_ctxt *ctxt;
776 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
777 llog_cleanup(env, ctxt);
782 static int mgc_fs_setup(const struct lu_env *env, struct obd_device *obd,
783 struct super_block *sb)
785 struct lustre_sb_info *lsi = s2lsi(sb);
786 struct client_obd *cli = &obd->u.cli;
787 struct lu_fid rfid, fid;
788 struct dt_object *root, *dto;
794 LASSERT(lsi->lsi_dt_dev);
796 /* The mgc fs exclusion mutex. Only one fs can be setup at a time. */
797 mutex_lock(&cli->cl_mgc_mutex);
799 /* Setup the configs dir */
800 fid.f_seq = FID_SEQ_LOCAL_NAME;
803 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
808 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
812 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
813 &cli->cl_mgc_los->los_dev->dd_lu_dev, NULL);
814 if (unlikely(IS_ERR(root)))
815 GOTO(out_los, rc = PTR_ERR(root));
817 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
819 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
820 dt_object_put_nocache(env, root);
822 GOTO(out_los, rc = PTR_ERR(dto));
824 cli->cl_mgc_configs_dir = dto;
826 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
827 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
831 /* We take an obd ref to insure that we can't get to mgc_cleanup
832 * without calling mgc_fs_cleanup first. */
833 class_incref(obd, "mgc_fs", obd);
835 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
839 dt_object_put(env, cli->cl_mgc_configs_dir);
840 cli->cl_mgc_configs_dir = NULL;
844 local_oid_storage_fini(env, cli->cl_mgc_los);
845 cli->cl_mgc_los = NULL;
846 mutex_unlock(&cli->cl_mgc_mutex);
851 static int mgc_fs_cleanup(const struct lu_env *env, struct obd_device *obd)
853 struct client_obd *cli = &obd->u.cli;
856 LASSERT(cli->cl_mgc_los != NULL);
858 mgc_local_llog_fini(env, obd);
860 dt_object_put_nocache(env, cli->cl_mgc_configs_dir);
861 cli->cl_mgc_configs_dir = NULL;
863 local_oid_storage_fini(env, cli->cl_mgc_los);
864 cli->cl_mgc_los = NULL;
866 class_decref(obd, "mgc_fs", obd);
867 mutex_unlock(&cli->cl_mgc_mutex);
872 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
874 struct llog_ctxt *ctxt;
879 /* setup only remote ctxt, the local disk context is switched per each
880 * filesystem during mgc_fs_setup() */
881 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
886 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
889 llog_initiator_connect(ctxt);
895 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
897 struct llog_ctxt *ctxt;
901 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
903 llog_cleanup(env, ctxt);
909 static atomic_t mgc_count = ATOMIC_INIT(0);
910 static int mgc_precleanup(struct obd_device *obd)
916 if (atomic_dec_and_test(&mgc_count)) {
917 LASSERT(rq_state & RQ_RUNNING);
918 /* stop requeue thread */
921 /* wakeup requeue thread to clean our cld */
922 temp = RQ_NOW | RQ_PRECLEANUP;
925 spin_lock(&config_list_lock);
927 spin_unlock(&config_list_lock);
931 wait_for_completion(&rq_exit);
932 obd_cleanup_client_import(obd);
934 rc = mgc_llog_fini(NULL, obd);
936 CERROR("failed to cleanup llogging subsystems\n");
941 static int mgc_cleanup(struct obd_device *obd)
946 /* COMPAT_146 - old config logs may have added profiles we don't
948 if (atomic_read(&obd->obd_type->typ_refcnt) <= 1)
949 /* Only for the last mgc */
950 class_del_profiles();
952 lprocfs_obd_cleanup(obd);
955 rc = client_obd_cleanup(obd);
959 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
961 struct task_struct *task;
965 rc = ptlrpcd_addref();
969 rc = client_obd_setup(obd, lcfg);
971 GOTO(err_decref, rc);
973 rc = mgc_llog_init(NULL, obd);
975 CERROR("failed to setup llogging subsystems\n");
976 GOTO(err_cleanup, rc);
979 rc = mgc_tunables_init(obd);
983 if (atomic_inc_return(&mgc_count) == 1) {
985 init_waitqueue_head(&rq_waitq);
987 /* start requeue thread */
988 task = kthread_run(mgc_requeue_thread, NULL, "ll_cfg_requeue");
991 CERROR("%s: cannot start requeue thread: rc = %d; "
992 "no more log updates\n",
996 /* rc is the task_struct pointer of mgc_requeue_thread. */
998 wait_for_completion(&rq_start);
1004 lprocfs_obd_cleanup(obd);
1006 client_obd_cleanup(obd);
1012 /* based on ll_mdc_blocking_ast */
1013 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
1014 void *data, int flag)
1016 struct lustre_handle lockh;
1017 struct config_llog_data *cld = (struct config_llog_data *)data;
1022 case LDLM_CB_BLOCKING:
1023 /* mgs wants the lock, give it up... */
1024 LDLM_DEBUG(lock, "MGC blocking CB");
1025 ldlm_lock2handle(lock, &lockh);
1026 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
1028 case LDLM_CB_CANCELING:
1029 /* We've given up the lock, prepare ourselves to update. */
1030 LDLM_DEBUG(lock, "MGC cancel CB");
1032 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
1033 PLDLMRES(lock->l_resource),
1034 (char *)&lock->l_resource->lr_name.name[0]);
1037 CDEBUG(D_INFO, "missing data, won't requeue\n");
1041 /* held at mgc_process_log(). */
1042 LASSERT(atomic_read(&cld->cld_refcount) > 0);
1044 lock->l_ast_data = NULL;
1045 cld->cld_lockh.cookie = 0;
1046 /* Are we done with this log? */
1047 if (cld->cld_stopping) {
1048 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
1050 config_log_put(cld);
1053 /* Make sure not to re-enqueue when the mgc is stopping
1054 (we get called from client_disconnect_export) */
1055 if (lock->l_conn_export == NULL ||
1056 lock->l_conn_export->exp_obd->u.cli.cl_conn_count == 0) {
1057 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
1059 config_log_put(cld);
1063 /* Re-enqueue now */
1064 mgc_requeue_add(cld);
1065 config_log_put(cld);
1074 /* Not sure where this should go... */
1075 /* This is the timeout value for MGS_CONNECT request plus a ping interval, such
1076 * that we can have a chance to try the secondary MGS if any. */
1077 #define MGC_ENQUEUE_LIMIT (INITIAL_CONNECT_TIMEOUT + (AT_OFF ? 0 : at_min) \
1079 #define MGC_TARGET_REG_LIMIT 10
1080 #define MGC_TARGET_REG_LIMIT_MAX RECONNECT_DELAY_MAX
1081 #define MGC_SEND_PARAM_LIMIT 10
1083 /* Take a config lock so we can get cancel notifications */
1084 static int mgc_enqueue(struct obd_export *exp, enum ldlm_type type,
1085 union ldlm_policy_data *policy, enum ldlm_mode mode,
1086 __u64 *flags, ldlm_glimpse_callback glimpse_callback,
1087 void *data, __u32 lvb_len, void *lvb_swabber,
1088 struct lustre_handle *lockh)
1090 struct config_llog_data *cld = (struct config_llog_data *)data;
1091 struct ldlm_enqueue_info einfo = {
1094 .ei_cb_bl = mgc_blocking_ast,
1095 .ei_cb_cp = ldlm_completion_ast,
1096 .ei_cb_gl = glimpse_callback,
1098 struct ptlrpc_request *req;
1099 int short_limit = cld_is_sptlrpc(cld);
1106 CDEBUG(D_MGC, "Enqueue for %s (res %#llx)\n", cld->cld_logname,
1107 cld->cld_resid.name[0]);
1109 /* We need a callback for every lockholder, so don't try to
1110 ldlm_lock_match (see rev 1.1.2.11.2.47) */
1111 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1112 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1117 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1118 ptlrpc_request_set_replen(req);
1120 /* check if this is server or client */
1121 if (cld->cld_cfg.cfg_sb) {
1122 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
1123 if (lsi && IS_SERVER(lsi))
1126 /* Limit how long we will wait for the enqueue to complete */
1127 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
1128 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1129 NULL, 0, LVB_T_NONE, lockh, 0);
1130 /* A failed enqueue should still call the mgc_blocking_ast,
1131 where it will be requeued if needed ("grant failed"). */
1132 ptlrpc_req_finished(req);
1136 static int mgc_cancel(struct obd_export *exp, enum ldlm_mode mode,
1137 struct lustre_handle *lockh)
1141 ldlm_lock_decref(lockh, mode);
1146 static void mgc_notify_active(struct obd_device *unused)
1148 /* wakeup mgc_requeue_thread to requeue mgc lock */
1149 spin_lock(&config_list_lock);
1151 spin_unlock(&config_list_lock);
1154 /* TODO: Help the MGS rebuild nidtbl. -jay */
1157 /* Send target_reg message to MGS */
1158 static int mgc_target_register(struct obd_export *exp,
1159 struct mgs_target_info *mti)
1161 struct ptlrpc_request *req;
1162 struct mgs_target_info *req_mti, *rep_mti;
1166 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1167 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1172 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1174 ptlrpc_req_finished(req);
1178 memcpy(req_mti, mti, sizeof(*req_mti));
1179 ptlrpc_request_set_replen(req);
1180 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1181 /* Limit how long we will wait for the enqueue to complete */
1182 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1184 /* if the target needs to regenerate the config log in MGS, it's better
1185 * to use some longer limit to let MGC have time to change connection to
1186 * another MGS (or try again with the same MGS) for the target (server)
1187 * will fail and exit if the request expired due to delay limit. */
1188 if (mti->mti_flags & (LDD_F_UPDATE | LDD_F_NEED_INDEX))
1189 req->rq_delay_limit = MGC_TARGET_REG_LIMIT_MAX;
1191 rc = ptlrpc_queue_wait(req);
1193 rep_mti = req_capsule_server_get(&req->rq_pill,
1194 &RMF_MGS_TARGET_INFO);
1195 memcpy(mti, rep_mti, sizeof(*rep_mti));
1196 CDEBUG(D_MGC, "register %s got index = %d\n",
1197 mti->mti_svname, mti->mti_stripe_index);
1199 ptlrpc_req_finished(req);
1204 static int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1205 u32 keylen, void *key,
1206 u32 vallen, void *val,
1207 struct ptlrpc_request_set *set)
1212 /* Turn off initial_recov after we try all backup servers once */
1213 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1214 struct obd_import *imp = class_exp2cliimp(exp);
1216 if (vallen != sizeof(int))
1218 value = *(int *)val;
1219 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1220 imp->imp_obd->obd_name, value,
1221 imp->imp_deactive, imp->imp_invalid,
1222 imp->imp_replayable, imp->imp_obd->obd_replayable,
1223 ptlrpc_import_state_name(imp->imp_state));
1224 /* Resurrect the import immediately if
1225 * 1. we previously got disconnected,
1226 * 2. value > 1 (at the same node with MGS)
1228 if (imp->imp_state == LUSTRE_IMP_DISCON || value > 1)
1229 ptlrpc_reconnect_import(imp);
1234 /* FIXME move this to mgc_process_config */
1235 if (KEY_IS(KEY_REGISTER_TARGET)) {
1236 struct mgs_target_info *mti;
1237 if (vallen != sizeof(struct mgs_target_info))
1239 mti = (struct mgs_target_info *)val;
1240 CDEBUG(D_MGC, "register_target %s %#x\n",
1241 mti->mti_svname, mti->mti_flags);
1242 rc = mgc_target_register(exp, mti);
1245 if (KEY_IS(KEY_SET_FS)) {
1246 struct super_block *sb = (struct super_block *)val;
1248 if (vallen != sizeof(struct super_block))
1251 rc = mgc_fs_setup(env, exp->exp_obd, sb);
1254 if (KEY_IS(KEY_CLEAR_FS)) {
1257 rc = mgc_fs_cleanup(env, exp->exp_obd);
1260 if (KEY_IS(KEY_MGSSEC)) {
1261 struct client_obd *cli = &exp->exp_obd->u.cli;
1262 struct sptlrpc_flavor flvr;
1265 * empty string means using current flavor, if which haven't
1266 * been set yet, set it as null.
1268 * if flavor has been set previously, check the asking flavor
1269 * must match the existing one.
1272 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1278 rc = sptlrpc_parse_flavor(val, &flvr);
1280 CERROR("invalid sptlrpc flavor %s to MGS\n",
1286 * caller already hold a mutex
1288 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1289 cli->cl_flvr_mgc = flvr;
1290 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1291 sizeof(flvr)) != 0) {
1294 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1296 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1297 "currently %s is in use\n",
1307 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1308 __u32 keylen, void *key, __u32 *vallen, void *val)
1312 if (KEY_IS(KEY_CONN_DATA)) {
1313 struct obd_import *imp = class_exp2cliimp(exp);
1314 struct obd_connect_data *data = val;
1316 if (*vallen == sizeof(*data)) {
1317 *data = imp->imp_connect_data;
1325 static int mgc_import_event(struct obd_device *obd,
1326 struct obd_import *imp,
1327 enum obd_import_event event)
1331 LASSERT(imp->imp_obd == obd);
1332 CDEBUG(D_MGC, "import event %#x\n", event);
1335 case IMP_EVENT_DISCON:
1336 /* MGC imports should not wait for recovery */
1337 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1338 ptlrpc_pinger_ir_down();
1340 case IMP_EVENT_INACTIVE:
1342 case IMP_EVENT_INVALIDATE: {
1343 struct ldlm_namespace *ns = obd->obd_namespace;
1344 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1347 case IMP_EVENT_ACTIVE:
1348 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1349 /* Clearing obd_no_recov allows us to continue pinging */
1350 obd->obd_no_recov = 0;
1351 mgc_notify_active(obd);
1352 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1353 ptlrpc_pinger_ir_up();
1357 case IMP_EVENT_DEACTIVATE:
1358 case IMP_EVENT_ACTIVATE:
1361 CERROR("Unknown import event %#x\n", event);
1368 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_SHIFT),
1369 CONFIG_READ_NRPAGES = 4
1372 static int mgc_apply_recover_logs(struct obd_device *mgc,
1373 struct config_llog_data *cld,
1375 void *data, int datalen, bool mne_swab)
1377 struct config_llog_instance *cfg = &cld->cld_cfg;
1378 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1379 struct mgs_nidtbl_entry *entry;
1380 struct lustre_cfg *lcfg;
1381 struct lustre_cfg_bufs bufs;
1382 u64 prev_version = 0;
1391 LASSERT(cfg->cfg_instance != 0);
1392 LASSERT(ll_get_cfg_instance(cfg->cfg_sb) == cfg->cfg_instance);
1394 OBD_ALLOC(inst, PAGE_SIZE);
1398 if (!IS_SERVER(lsi)) {
1399 pos = snprintf(inst, PAGE_SIZE, "%016lx", cfg->cfg_instance);
1400 if (pos >= PAGE_SIZE) {
1401 OBD_FREE(inst, PAGE_SIZE);
1404 #ifdef HAVE_SERVER_SUPPORT
1406 LASSERT(IS_MDT(lsi));
1407 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1410 OBD_FREE(inst, PAGE_SIZE);
1414 #endif /* HAVE_SERVER_SUPPORT */
1419 bufsz = PAGE_SIZE - pos;
1421 while (datalen > 0) {
1422 int entry_len = sizeof(*entry);
1424 struct obd_device *obd;
1431 if (datalen < sizeof(*entry))
1434 entry = (typeof(entry))(data + off);
1437 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1439 if (entry->mne_nid_count == 0) /* at least one nid entry */
1441 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1444 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1445 if (datalen < entry_len) /* must have entry_len at least */
1448 /* Keep this swab for normal mixed endian handling. LU-1644 */
1450 lustre_swab_mgs_nidtbl_entry(entry);
1451 if (entry->mne_length > PAGE_SIZE) {
1452 CERROR("MNE too large (%u)\n", entry->mne_length);
1456 if (entry->mne_length < entry_len)
1459 off += entry->mne_length;
1460 datalen -= entry->mne_length;
1464 if (entry->mne_version > max_version) {
1465 CERROR("entry index(%lld) is over max_index(%lld)\n",
1466 entry->mne_version, max_version);
1470 if (prev_version >= entry->mne_version) {
1471 CERROR("index unsorted, prev %lld, now %lld\n",
1472 prev_version, entry->mne_version);
1475 prev_version = entry->mne_version;
1478 * Write a string with format "nid::instance" to
1479 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1482 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1483 memset(buf, 0, bufsz);
1487 /* lustre-OST0001-osc-<instance #> */
1488 strcpy(obdname, cld->cld_logname);
1489 cname = strrchr(obdname, '-');
1490 if (cname == NULL) {
1491 CERROR("mgc %s: invalid logname %s\n",
1492 mgc->obd_name, obdname);
1496 pos = cname - obdname;
1498 pos += sprintf(obdname + pos, "-%s%04x",
1499 is_ost ? "OST" : "MDT", entry->mne_index);
1501 cname = is_ost ? "osc" : "mdc",
1502 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1503 lustre_cfg_bufs_reset(&bufs, obdname);
1505 /* find the obd by obdname */
1506 obd = class_name2obd(obdname);
1508 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1509 mgc->obd_name, obdname);
1511 /* this is a safe race, when the ost is starting up...*/
1515 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1518 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1521 down_read(&obd->u.cli.cl_sem);
1522 if (obd->u.cli.cl_import == NULL) {
1523 /* client does not connect to the OST yet */
1524 up_read(&obd->u.cli.cl_sem);
1529 /* iterate all nids to find one */
1530 /* find uuid by nid */
1532 for (i = 0; i < entry->mne_nid_count; i++) {
1533 rc = client_import_find_conn(obd->u.cli.cl_import,
1535 (struct obd_uuid *)uuid);
1540 up_read(&obd->u.cli.cl_sem);
1542 CERROR("mgc: cannot find uuid by nid %s\n",
1543 libcfs_nid2str(entry->u.nids[0]));
1547 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1548 uuid, libcfs_nid2str(entry->u.nids[0]));
1550 pos += strlen(uuid);
1551 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1552 LASSERT(pos < bufsz);
1554 lustre_cfg_bufs_set_string(&bufs, 1, params);
1556 OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount,
1562 lustre_cfg_init(lcfg, LCFG_PARAM, &bufs);
1564 CDEBUG(D_INFO, "ir apply logs %lld/%lld for %s -> %s\n",
1565 prev_version, max_version, obdname, params);
1567 rc = class_process_config(lcfg);
1568 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
1569 lcfg->lcfg_buflens));
1571 CDEBUG(D_INFO, "process config for %s error %d\n",
1574 /* continue, even one with error */
1577 OBD_FREE(inst, PAGE_SIZE);
1582 * This function is called if this client was notified for target restarting
1583 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery or
1586 static int mgc_process_recover_nodemap_log(struct obd_device *obd,
1587 struct config_llog_data *cld)
1589 struct ptlrpc_connection *mgc_conn;
1590 struct ptlrpc_request *req = NULL;
1591 struct config_llog_instance *cfg = &cld->cld_cfg;
1592 struct mgs_config_body *body;
1593 struct mgs_config_res *res;
1594 struct nodemap_config *new_config = NULL;
1595 struct lu_nodemap *recent_nodemap = NULL;
1596 struct ptlrpc_bulk_desc *desc;
1597 struct page **pages = NULL;
1598 __u64 config_read_offset = 0;
1599 __u8 nodemap_cur_pass = 0;
1602 bool mne_swab = false;
1608 mgc_conn = class_exp2cliimp(cld->cld_mgcexp)->imp_connection;
1610 /* don't need to get local config */
1611 if (cld_is_nodemap(cld) && LNetIsPeerLocal(mgc_conn->c_peer.nid))
1614 /* allocate buffer for bulk transfer.
1615 * if this is the first time for this mgs to read logs,
1616 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1617 * once; otherwise, it only reads increment of logs, this should be
1618 * small and CONFIG_READ_NRPAGES will be used.
1620 nrpages = CONFIG_READ_NRPAGES;
1621 if (cfg->cfg_last_idx == 0 || cld_is_nodemap(cld))
1622 nrpages = CONFIG_READ_NRPAGES_INIT;
1624 OBD_ALLOC_PTR_ARRAY(pages, nrpages);
1626 GOTO(out, rc = -ENOMEM);
1628 for (i = 0; i < nrpages; i++) {
1629 pages[i] = alloc_page(GFP_KERNEL);
1630 if (pages[i] == NULL)
1631 GOTO(out, rc = -ENOMEM);
1635 #ifdef HAVE_SERVER_SUPPORT
1636 if (cld_is_nodemap(cld) && config_read_offset == 0) {
1637 new_config = nodemap_config_alloc();
1638 if (IS_ERR(new_config)) {
1639 rc = PTR_ERR(new_config);
1645 LASSERT(cld_is_recover(cld) || cld_is_nodemap(cld));
1646 LASSERT(mutex_is_locked(&cld->cld_lock));
1647 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1648 &RQF_MGS_CONFIG_READ);
1650 GOTO(out, rc = -ENOMEM);
1652 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1657 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1658 LASSERT(body != NULL);
1659 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1660 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1661 >= sizeof(body->mcb_name))
1662 GOTO(out, rc = -E2BIG);
1663 if (cld_is_nodemap(cld))
1664 body->mcb_offset = config_read_offset;
1666 body->mcb_offset = cfg->cfg_last_idx + 1;
1667 body->mcb_type = cld->cld_type;
1668 body->mcb_bits = PAGE_SHIFT;
1669 body->mcb_units = nrpages;
1670 body->mcb_nm_cur_pass = nodemap_cur_pass;
1672 /* allocate bulk transfer descriptor */
1673 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1,
1674 PTLRPC_BULK_PUT_SINK,
1676 &ptlrpc_bulk_kiov_pin_ops);
1678 GOTO(out, rc = -ENOMEM);
1680 for (i = 0; i < nrpages; i++)
1681 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1684 ptlrpc_request_set_replen(req);
1685 rc = ptlrpc_queue_wait(req);
1689 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1691 GOTO(out, rc = -EPROTO);
1693 if (cld_is_nodemap(cld)) {
1694 config_read_offset = res->mcr_offset;
1695 eof = config_read_offset == II_END_OFF;
1696 nodemap_cur_pass = res->mcr_nm_cur_pass;
1698 if (res->mcr_size < res->mcr_offset)
1699 GOTO(out, rc = -EINVAL);
1701 /* always update the index even though it might have errors with
1702 * handling the recover logs
1704 cfg->cfg_last_idx = res->mcr_offset;
1705 eof = res->mcr_offset == res->mcr_size;
1707 CDEBUG(D_INFO, "Latest version %lld, more %d.\n",
1708 res->mcr_offset, eof == false);
1711 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1713 GOTO(out, rc = ealen);
1715 if (ealen > nrpages << PAGE_SHIFT)
1716 GOTO(out, rc = -EINVAL);
1718 if (ealen == 0) { /* no logs transferred */
1719 #ifdef HAVE_SERVER_SUPPORT
1720 /* config changed since first read RPC */
1721 if (cld_is_nodemap(cld) && config_read_offset == 0) {
1722 CDEBUG(D_INFO, "nodemap config changed in transit, retrying\n");
1723 GOTO(out, rc = -EAGAIN);
1731 mne_swab = ptlrpc_rep_need_swab(req);
1733 /* When a nodemap config is received, we build a new nodemap config,
1734 * with new nodemap structs. We keep track of the most recently added
1735 * nodemap since the config is read ordered by nodemap_id, and so it
1736 * is likely that the next record will be related. Because access to
1737 * the nodemaps is single threaded until the nodemap_config is active,
1738 * we don't need to reference count with recent_nodemap, though
1739 * recent_nodemap should be set to NULL when the nodemap_config
1740 * is either destroyed or set active.
1742 for (i = 0; i < nrpages && ealen > 0; i++) {
1746 ptr = kmap(pages[i]);
1747 if (cld_is_nodemap(cld))
1748 rc2 = nodemap_process_idx_pages(new_config, ptr,
1751 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset,
1758 CWARN("%s: error processing %s log %s: rc = %d\n",
1760 cld_is_nodemap(cld) ? "nodemap" : "recovery",
1763 GOTO(out, rc = rc2);
1771 ptlrpc_req_finished(req);
1775 if (rc == 0 && !eof)
1778 #ifdef HAVE_SERVER_SUPPORT
1779 if (new_config != NULL) {
1780 /* recent_nodemap cannot be used after set_active/dealloc */
1782 nodemap_config_set_active_mgc(new_config);
1784 nodemap_config_dealloc(new_config);
1789 for (i = 0; i < nrpages; i++) {
1790 if (pages[i] == NULL)
1792 __free_page(pages[i]);
1794 OBD_FREE_PTR_ARRAY(pages, nrpages);
1799 static int mgc_barrier_glimpse_ast(struct ldlm_lock *lock, void *data)
1801 struct config_llog_data *cld = lock->l_ast_data;
1805 if (cld->cld_stopping)
1808 rc = barrier_handler(s2lsi(cld->cld_cfg.cfg_sb)->lsi_dt_dev,
1809 (struct ptlrpc_request *)data);
1814 /* Copy a remote log locally */
1815 static int mgc_llog_local_copy(const struct lu_env *env,
1816 struct obd_device *obd,
1817 struct llog_ctxt *rctxt,
1818 struct llog_ctxt *lctxt, char *logname)
1826 * - copy it to backup using llog_backup()
1827 * - copy remote llog to logname using llog_backup()
1828 * - if failed then move bakup to logname again
1831 OBD_ALLOC(temp_log, strlen(logname) + 2);
1834 sprintf(temp_log, "%sT", logname);
1836 /* make a copy of local llog at first */
1837 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1838 if (rc < 0 && rc != -ENOENT)
1840 /* copy remote llog to the local copy */
1841 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1842 if (rc == -ENOENT) {
1843 /* no remote llog, delete local one too */
1844 llog_erase(env, lctxt, NULL, logname);
1845 } else if (rc < 0) {
1846 /* error during backup, get local one back from the copy */
1847 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1849 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1850 obd->obd_name, logname, rc);
1852 llog_erase(env, lctxt, NULL, temp_log);
1853 OBD_FREE(temp_log, strlen(logname) + 2);
1857 /* local_only means it cannot get remote llogs */
1858 static int mgc_process_cfg_log(struct obd_device *mgc,
1859 struct config_llog_data *cld, int local_only)
1861 struct llog_ctxt *ctxt, *lctxt = NULL;
1862 struct client_obd *cli = &mgc->u.cli;
1863 struct lustre_sb_info *lsi = NULL;
1870 LASSERT(mutex_is_locked(&cld->cld_lock));
1872 if (cld->cld_cfg.cfg_sb)
1873 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1879 rc = lu_env_init(env, LCT_MG_THREAD);
1883 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1886 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1888 /* Copy the setup log locally if we can. Don't mess around if we're
1889 * running an MGS though (logs are already local). */
1890 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1891 cli->cl_mgc_configs_dir != NULL &&
1892 lu2dt_dev(cli->cl_mgc_configs_dir->do_lu.lo_dev) ==
1894 if (!local_only && !lsi->lsi_dt_dev->dd_rdonly)
1895 /* Only try to copy log if we have the lock. */
1896 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1898 if (local_only || rc) {
1899 if (strcmp(cld->cld_logname, PARAMS_FILENAME) != 0 &&
1900 llog_is_empty(env, lctxt, cld->cld_logname)) {
1901 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1902 "log %s and no local copy."
1903 "\n", cld->cld_logname);
1904 GOTO(out_pop, rc = -ENOENT);
1906 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1907 "copy for now, will try to update later.\n",
1911 /* Now, whether we copied or not, start using the local llog.
1912 * If we failed to copy, we'll start using whatever the old
1914 llog_ctxt_put(ctxt);
1918 if (local_only) /* no local log at client side */
1919 GOTO(out_pop, rc = -EIO);
1923 if (lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1924 lsi->lsi_dt_dev->dd_rdonly) {
1925 struct llog_ctxt *rctxt;
1927 /* Under readonly mode, we may have no local copy or local
1928 * copy is incomplete, so try to use remote llog firstly. */
1929 rctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1932 rc = class_config_parse_llog(env, rctxt, cld->cld_logname,
1934 llog_ctxt_put(rctxt);
1937 if (rc && rc != -ENOENT)
1938 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1942 * update settings on existing OBDs.
1943 * the logname must be <fsname>-sptlrpc
1945 if (rc == 0 && cld_is_sptlrpc(cld))
1946 class_notify_sptlrpc_conf(cld->cld_logname,
1947 strlen(cld->cld_logname) -
1948 strlen("-sptlrpc"));
1952 __llog_ctxt_put(env, ctxt);
1954 __llog_ctxt_put(env, lctxt);
1962 static bool mgc_import_in_recovery(struct obd_import *imp)
1964 bool in_recovery = true;
1966 spin_lock(&imp->imp_lock);
1967 if (imp->imp_state == LUSTRE_IMP_FULL ||
1968 imp->imp_state == LUSTRE_IMP_CLOSED)
1969 in_recovery = false;
1970 spin_unlock(&imp->imp_lock);
1976 * Get a configuration log from the MGS and process it.
1978 * This function is called for both clients and servers to process the
1979 * configuration log from the MGS. The MGC enqueues a DLM lock on the
1980 * log from the MGS, and if the lock gets revoked the MGC will be notified
1981 * by the lock cancellation callback that the config log has changed,
1982 * and will enqueue another MGS lock on it, and then continue processing
1983 * the new additions to the end of the log.
1985 * Since the MGC import is not replayable, if the import is being evicted
1986 * (rcl == -ESHUTDOWN, \see ptlrpc_import_delay_req()), retry to process
1987 * the log until recovery is finished or the import is closed.
1989 * Make a local copy of the log before parsing it if appropriate (non-MGS
1990 * server) so that the server can start even when the MGS is down.
1992 * There shouldn't be multiple processes running process_log at once --
1993 * sounds like badness. It actually might be fine, as long as they're not
1994 * trying to update from the same log simultaneously, in which case we
1995 * should use a per-log semaphore instead of cld_lock.
1997 * \param[in] mgc MGC device by which to fetch the configuration log
1998 * \param[in] cld log processing state (stored in lock callback data)
2000 * \retval 0 on success
2001 * \retval negative errno on failure
2003 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
2005 struct lustre_handle lockh = { 0 };
2006 __u64 flags = LDLM_FL_NO_LRU;
2011 LASSERT(cld != NULL);
2013 /* I don't want multiple processes running process_log at once --
2014 sounds like badness. It actually might be fine, as long as
2015 we're not trying to update from the same log
2016 simultaneously (in which case we should use a per-log sem.) */
2018 mutex_lock(&cld->cld_lock);
2019 if (cld->cld_stopping) {
2020 mutex_unlock(&cld->cld_lock);
2024 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
2026 CDEBUG(D_MGC, "Process log %s-%016lx from %d\n", cld->cld_logname,
2027 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
2029 /* Get the cfg lock on the llog */
2030 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, LDLM_PLAIN, NULL,
2032 cld_is_barrier(cld) ? mgc_barrier_glimpse_ast : NULL,
2033 cld, 0, NULL, &lockh);
2035 /* Get the cld, it will be released in mgc_blocking_ast. */
2036 config_log_get(cld);
2037 rc = ldlm_lock_set_data(&lockh, (void *)cld);
2038 LASSERT(!lustre_handle_is_used(&cld->cld_lockh));
2040 cld->cld_lockh = lockh;
2042 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
2043 cld->cld_lockh.cookie = 0;
2045 if (rcl == -ESHUTDOWN &&
2046 atomic_read(&mgc->u.cli.cl_mgc_refcount) > 0 && !retry) {
2047 struct obd_import *imp;
2048 long timeout = cfs_time_seconds(obd_timeout);
2050 mutex_unlock(&cld->cld_lock);
2051 imp = class_exp2cliimp(mgc->u.cli.cl_mgc_mgsexp);
2053 /* Let's force the pinger, and wait the import to be
2054 * connected, note: since mgc import is non-replayable,
2055 * and even the import state is disconnected, it does
2056 * not mean the "recovery" is stopped, so we will keep
2057 * waitting until timeout or the import state is
2059 ptlrpc_pinger_force(imp);
2061 wait_event_idle_timeout(imp->imp_recovery_waitq,
2062 !mgc_import_in_recovery(imp),
2065 if (imp->imp_state == LUSTRE_IMP_FULL) {
2069 mutex_lock(&cld->cld_lock);
2070 /* unlock/lock mutex, so check stopping again */
2071 if (cld->cld_stopping) {
2072 mutex_unlock(&cld->cld_lock);
2075 spin_lock(&config_list_lock);
2076 cld->cld_lostlock = 1;
2077 spin_unlock(&config_list_lock);
2080 /* mark cld_lostlock so that it will requeue
2081 * after MGC becomes available. */
2082 spin_lock(&config_list_lock);
2083 cld->cld_lostlock = 1;
2084 spin_unlock(&config_list_lock);
2088 if (cld_is_recover(cld) || cld_is_nodemap(cld)) {
2090 rc = mgc_process_recover_nodemap_log(mgc, cld);
2091 else if (cld_is_nodemap(cld))
2094 } else if (!cld_is_barrier(cld)) {
2095 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
2098 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
2099 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
2101 /* Now drop the lock so MGS can revoke it */
2103 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, LCK_CR, &lockh);
2105 CERROR("Can't drop cfg lock: %d\n", rcl);
2107 mutex_unlock(&cld->cld_lock);
2109 /* requeue nodemap lock immediately if transfer was interrupted */
2110 if ((cld_is_nodemap(cld) && rc == -EAGAIN) ||
2111 (cld_is_recover(cld) && rc)) {
2112 if (cld_is_recover(cld))
2113 CWARN("%s: IR log %s failed, not fatal: rc = %d\n",
2114 mgc->obd_name, cld->cld_logname, rc);
2115 mgc_requeue_add(cld);
2123 /** Called from lustre_process_log.
2124 * LCFG_LOG_START gets the config log from the MGS, processes it to start
2125 * any services, and adds it to the list logs to watch (follow).
2127 static int mgc_process_config(struct obd_device *obd, size_t len, void *buf)
2129 struct lustre_cfg *lcfg = buf;
2130 struct config_llog_instance *cfg = NULL;
2135 switch(lcfg->lcfg_command) {
2136 case LCFG_LOV_ADD_OBD: {
2137 /* Overloading this cfg command: register a new target */
2138 struct mgs_target_info *mti;
2140 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
2141 sizeof(struct mgs_target_info))
2142 GOTO(out, rc = -EINVAL);
2144 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
2145 CDEBUG(D_MGC, "add_target %s %#x\n",
2146 mti->mti_svname, mti->mti_flags);
2147 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
2150 case LCFG_LOV_DEL_OBD:
2151 /* Unregister has no meaning at the moment. */
2152 CERROR("lov_del_obd unimplemented\n");
2155 case LCFG_SPTLRPC_CONF: {
2156 rc = sptlrpc_process_config(lcfg);
2159 case LCFG_LOG_START: {
2160 struct config_llog_data *cld;
2161 struct super_block *sb;
2163 logname = lustre_cfg_string(lcfg, 1);
2164 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
2165 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
2167 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
2170 /* We're only called through here on the initial mount */
2171 cld = config_log_add(obd, logname, cfg, sb);
2177 rc = mgc_process_log(obd, cld);
2178 if (rc == 0 && cld->cld_recover != NULL) {
2179 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
2180 imp_connect_data, IMP_RECOV)) {
2181 rc = mgc_process_log(obd, cld->cld_recover);
2183 struct config_llog_data *cir;
2185 mutex_lock(&cld->cld_lock);
2186 cir = cld->cld_recover;
2187 cld->cld_recover = NULL;
2188 mutex_unlock(&cld->cld_lock);
2189 config_log_put(cir);
2193 CERROR("Cannot process recover llog %d\n", rc);
2196 if (rc == 0 && cld->cld_params != NULL) {
2197 rc = mgc_process_log(obd, cld->cld_params);
2198 if (rc == -ENOENT) {
2199 CDEBUG(D_MGC, "There is no params "
2200 "config file yet\n");
2203 /* params log is optional */
2205 CERROR("%s: can't process params llog: rc = %d\n",
2211 case LCFG_LOG_END: {
2212 logname = lustre_cfg_string(lcfg, 1);
2214 if (lcfg->lcfg_bufcount >= 2)
2215 cfg = (struct config_llog_instance *)lustre_cfg_buf(
2217 rc = config_log_end(logname, cfg);
2221 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
2222 GOTO(out, rc = -EINVAL);
2230 static const struct obd_ops mgc_obd_ops = {
2231 .o_owner = THIS_MODULE,
2232 .o_setup = mgc_setup,
2233 .o_precleanup = mgc_precleanup,
2234 .o_cleanup = mgc_cleanup,
2235 .o_add_conn = client_import_add_conn,
2236 .o_del_conn = client_import_del_conn,
2237 .o_connect = client_connect_import,
2238 .o_disconnect = client_disconnect_export,
2239 .o_set_info_async = mgc_set_info_async,
2240 .o_get_info = mgc_get_info,
2241 .o_import_event = mgc_import_event,
2242 .o_process_config = mgc_process_config,
2245 static int __init mgc_init(void)
2247 return class_register_type(&mgc_obd_ops, NULL, false, NULL,
2248 LUSTRE_MGC_NAME, NULL);
2251 static void __exit mgc_exit(void)
2253 class_unregister_type(LUSTRE_MGC_NAME);
2256 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2257 MODULE_DESCRIPTION("Lustre Management Client");
2258 MODULE_VERSION(LUSTRE_VERSION_STRING);
2259 MODULE_LICENSE("GPL");
2261 module_init(mgc_init);
2262 module_exit(mgc_exit);