4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/mgc/mgc_request.c
33 * Author: Nathan Rutman <nathan@clusterfs.com>
36 #define DEBUG_SUBSYSTEM S_MGC
37 #define D_MGC D_CONFIG /*|D_WARNING*/
39 #include <linux/module.h>
40 #include <linux/random.h>
42 #include <lprocfs_status.h>
43 #include <lustre_dlm.h>
44 #include <lustre_disk.h>
45 #include <lustre_log.h>
46 #include <lustre_swab.h>
47 #include <obd_class.h>
49 #include "mgc_internal.h"
51 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id,
52 enum mgs_cfg_type type)
56 if (len > sizeof(resname)) {
57 CERROR("name too long: %s\n", name);
61 CERROR("missing name: %s\n", name);
64 memcpy(&resname, name, len);
66 /* Always use the same endianness for the resid */
67 memset(res_id, 0, sizeof(*res_id));
68 res_id->name[0] = cpu_to_le64(resname);
69 /* XXX: unfortunately, sptlprc and config llog share one lock */
71 case MGS_CFG_T_CONFIG:
72 case MGS_CFG_T_SPTLRPC:
75 case MGS_CFG_T_RECOVER:
76 case MGS_CFG_T_PARAMS:
77 #ifdef HAVE_SERVER_SUPPORT
78 case MGS_CFG_T_NODEMAP:
79 case MGS_CFG_T_BARRIER:
86 res_id->name[1] = cpu_to_le64(resname);
87 CDEBUG(D_MGC, "log %s to resid %#llx/%#llx (%.8s)\n", name,
88 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
92 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id,
93 enum mgs_cfg_type type)
95 /* fsname is at most 8 chars long, can contain "-". eg. lustre, lu-0 */
96 return mgc_name2resid(fsname, strlen(fsname), res_id, type);
98 EXPORT_SYMBOL(mgc_fsname2resid);
100 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id,
101 enum mgs_cfg_type type)
106 /* logname consists of "fsname-nodetype". eg. "lustre-MDT0001",
107 * "SUN-000-client" there is an exception: llog "params"
109 name_end = strrchr(logname, '-');
111 len = strlen(logname);
113 len = name_end - logname;
114 return mgc_name2resid(logname, len, res_id, type);
116 EXPORT_SYMBOL(mgc_logname2resid);
118 /********************** config llog list **********************/
119 static LIST_HEAD(config_llog_list);
120 static DEFINE_SPINLOCK(config_list_lock); /* protects config_llog_list */
122 /* Take a reference to a config log */
123 static int config_log_get(struct config_llog_data *cld)
126 refcount_inc(&cld->cld_refcount);
127 CDEBUG(D_INFO, "log %s (%p) refs %d\n", cld->cld_logname, cld,
128 refcount_read(&cld->cld_refcount));
132 /* Drop a reference to a config log. When no longer referenced, We can free the
135 static void config_log_put(struct config_llog_data *cld)
142 CDEBUG(D_INFO, "log %s(%p) refs %d\n", cld->cld_logname, cld,
143 refcount_read(&cld->cld_refcount));
145 /* spinlock to make sure no item with 0 refcount in the list */
146 if (refcount_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
147 list_del(&cld->cld_list_chain);
148 spin_unlock(&config_list_lock);
150 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
151 #ifdef HAVE_SERVER_SUPPORT
152 config_log_put(cld->cld_barrier);
153 config_log_put(cld->cld_nodemap);
155 config_log_put(cld->cld_recover);
156 config_log_put(cld->cld_params);
157 config_log_put(cld->cld_sptlrpc);
158 if (cld_is_sptlrpc(cld)) {
159 cld->cld_stopping = 1;
160 sptlrpc_conf_log_stop(cld->cld_logname);
163 class_export_put(cld->cld_mgcexp);
164 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
170 /* Find a config log by name */
172 struct config_llog_data *config_log_find(char *logname,
173 struct config_llog_instance *cfg)
175 struct config_llog_data *cld;
176 struct config_llog_data *found = NULL;
177 unsigned long cfg_instance;
180 LASSERT(logname != NULL);
182 cfg_instance = cfg ? cfg->cfg_instance : 0;
183 spin_lock(&config_list_lock);
184 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
185 /* check if cfg_instance is the one we want */
186 if (cfg_instance != cld->cld_cfg.cfg_instance)
189 /* instance may be NULL, should check name */
190 if (strcmp(logname, cld->cld_logname) == 0) {
192 config_log_get(found);
196 spin_unlock(&config_list_lock);
201 struct config_llog_data *do_config_log_add(struct obd_device *obd,
203 enum mgs_cfg_type type,
204 struct config_llog_instance *cfg,
205 struct super_block *sb)
207 struct config_llog_data *cld;
213 CDEBUG(D_MGC, "do adding config log %s-%016lx\n", logname,
214 cfg ? cfg->cfg_instance : 0);
216 logname_size = strlen(logname) + 1;
217 OBD_ALLOC(cld, sizeof(*cld) + logname_size);
219 RETURN(ERR_PTR(-ENOMEM));
221 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
223 OBD_FREE(cld, sizeof(*cld) + logname_size);
227 strscpy(cld->cld_logname, logname, logname_size);
231 cld->cld_cfg.cfg_callback = class_config_llog_handler;
232 mutex_init(&cld->cld_lock);
233 cld->cld_cfg.cfg_last_idx = 0;
234 cld->cld_cfg.cfg_flags = 0;
235 cld->cld_cfg.cfg_sb = sb;
236 cld->cld_type = type;
237 refcount_set(&cld->cld_refcount, 1);
239 /* Keep the mgc around until we are done */
240 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
242 if (cld_is_sptlrpc(cld))
243 sptlrpc_conf_log_start(logname);
245 spin_lock(&config_list_lock);
246 list_add(&cld->cld_list_chain, &config_llog_list);
247 spin_unlock(&config_list_lock);
249 if (cld_is_sptlrpc(cld) || cld_is_nodemap(cld) || cld_is_barrier(cld)) {
250 rc = mgc_process_log(obd, cld);
251 if (rc && rc != -ENOENT)
252 CERROR("%s: failed processing log, type %d: rc = %d\n",
253 obd->obd_name, type, rc);
259 static struct config_llog_data *
260 config_recover_log_add(struct obd_device *obd, char *fsname,
261 struct config_llog_instance *cfg,
262 struct super_block *sb)
264 struct config_llog_instance lcfg = *cfg;
265 struct config_llog_data *cld;
266 bool is_server = IS_SERVER(s2lsi(sb));
269 #ifdef HAVE_SERVER_SUPPORT
270 if (IS_OST(s2lsi(sb)))
273 /* for osp-on-ost, see lustre_start_osp() */
274 if (IS_MDT(s2lsi(sb)) && lcfg.cfg_instance)
277 /* We have to use different llog for clients and MDTs for DNE,
278 * where only clients are notified if one of DNE server restarts.
280 LASSERT(strlen(fsname) < sizeof(logname) / 2);
281 strncpy(logname, fsname, sizeof(logname));
283 LASSERT(is_server ? lcfg.cfg_instance == 0 : lcfg.cfg_instance != 0);
285 lcfg.cfg_instance = ll_get_cfg_instance(sb);
286 scnprintf(logname, sizeof(logname), "%s-%s", fsname,
287 is_server ? "mdtir" : "cliir");
289 cld = do_config_log_add(obd, logname, MGS_CFG_T_RECOVER, &lcfg, sb);
293 static struct config_llog_data *
294 config_log_find_or_add(struct obd_device *obd, char *logname,
295 struct super_block *sb, enum mgs_cfg_type type,
296 struct config_llog_instance *cfg)
298 struct config_llog_instance lcfg = *cfg;
299 struct config_llog_data *cld;
301 /* Note class_config_llog_handler() depends on getting "obd" back */
302 /* for sptlrpc, sb is only provided to be able to make a local copy,
303 * not for the instance
305 if (sb && type != MGS_CFG_T_SPTLRPC)
306 lcfg.cfg_instance = ll_get_cfg_instance(sb);
308 lcfg.cfg_instance = (unsigned long)obd;
310 cld = config_log_find(logname, &lcfg);
311 if (unlikely(cld != NULL))
314 return do_config_log_add(obd, logname, type, &lcfg, sb);
317 /** Add this log to the list of active logs watched by an MGC.
318 * Active means we're watching for updates.
319 * We have one active log per "mount" - client instance or servername.
320 * Each instance may be at a different point in the log.
322 static struct config_llog_data *
323 config_log_add(struct obd_device *obd, char *logname,
324 struct config_llog_instance *cfg, struct super_block *sb)
326 struct lustre_sb_info *lsi = s2lsi(sb);
327 struct config_llog_data *cld = NULL;
328 struct config_llog_data *sptlrpc_cld = NULL;
329 struct config_llog_data *params_cld = NULL;
330 #ifdef HAVE_SERVER_SUPPORT
331 struct config_llog_data *nodemap_cld = NULL;
332 struct config_llog_data *barrier_cld = NULL;
341 CDEBUG(D_MGC, "add config log %s-%016lx\n", logname,
345 * for each regular log, the depended sptlrpc log name is
346 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
348 ptr = strrchr(logname, '-');
349 if (ptr == NULL || ptr - logname > 8) {
350 CERROR("logname %s is too long\n", logname);
351 RETURN(ERR_PTR(-EINVAL));
354 memcpy(seclogname, logname, ptr - logname);
355 strcpy(seclogname + (ptr - logname), "-sptlrpc");
357 if (cfg->cfg_sub_clds & CONFIG_SUB_SPTLRPC) {
358 sptlrpc_cld = config_log_find_or_add(obd, seclogname, sb,
359 MGS_CFG_T_SPTLRPC, cfg);
360 if (IS_ERR(sptlrpc_cld)) {
361 rc = PTR_ERR(sptlrpc_cld);
362 CERROR("%s: can't create sptlrpc log %s: rc = %d\n",
363 obd->obd_name, seclogname, rc);
368 if (cfg->cfg_sub_clds & CONFIG_SUB_PARAMS) {
369 params_cld = config_log_find_or_add(obd, PARAMS_FILENAME, sb,
370 MGS_CFG_T_PARAMS, cfg);
371 if (IS_ERR(params_cld)) {
372 rc = PTR_ERR(params_cld);
373 CERROR("%s: can't create params log: rc = %d\n",
375 GOTO(out_sptlrpc, rc);
379 #ifdef HAVE_SERVER_SUPPORT
380 if (!IS_MGS(lsi) && cfg->cfg_sub_clds & CONFIG_SUB_NODEMAP) {
381 nodemap_cld = config_log_find_or_add(obd, LUSTRE_NODEMAP_NAME,
382 NULL, MGS_CFG_T_NODEMAP,
384 if (IS_ERR(nodemap_cld)) {
385 rc = PTR_ERR(nodemap_cld);
386 CERROR("%s: cannot create nodemap log: rc = %d\n",
388 GOTO(out_params, rc);
392 if (IS_MDT(s2lsi(sb)) && cfg->cfg_sub_clds & CONFIG_SUB_BARRIER) {
393 snprintf(seclogname + (ptr - logname), sizeof(seclogname) - 1,
394 "-%s", BARRIER_FILENAME);
395 barrier_cld = config_log_find_or_add(obd, seclogname, sb,
396 MGS_CFG_T_BARRIER, cfg);
397 if (IS_ERR(barrier_cld)) {
398 rc = PTR_ERR(barrier_cld);
399 CERROR("%s: can't create barrier log: rc = %d\n",
401 GOTO(out_nodemap, rc);
405 cld = do_config_log_add(obd, logname, MGS_CFG_T_CONFIG, cfg, sb);
408 CERROR("%s: can't create log: rc = %d\n",
410 #ifdef HAVE_SERVER_SUPPORT
411 GOTO(out_barrier, rc);
413 GOTO(out_params, rc);
417 LASSERT(lsi->lsi_lmd);
418 if (!test_bit(LMD_FLG_NOIR, lsi->lsi_lmd->lmd_flags) &&
419 cfg->cfg_sub_clds & CONFIG_SUB_RECOVER) {
420 struct config_llog_data *recover_cld;
422 ptr = strrchr(seclogname, '-');
426 CERROR("%s: sptlrpc log name not correct, %s: rc = %d\n",
427 obd->obd_name, seclogname, -EINVAL);
428 GOTO(out_cld, rc = -EINVAL);
431 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
432 if (IS_ERR(recover_cld)) {
433 rc = PTR_ERR(recover_cld);
434 CERROR("%s: can't create recover log: rc = %d\n",
439 mutex_lock(&cld->cld_lock);
441 cld->cld_recover = recover_cld;
445 mutex_lock(&cld->cld_lock);
446 #ifdef HAVE_SERVER_SUPPORT
447 cld->cld_barrier = barrier_cld;
448 cld->cld_nodemap = nodemap_cld;
450 cld->cld_params = params_cld;
451 cld->cld_sptlrpc = sptlrpc_cld;
452 mutex_unlock(&cld->cld_lock);
458 #ifdef HAVE_SERVER_SUPPORT
460 config_log_put(barrier_cld);
462 config_log_put(nodemap_cld);
465 config_log_put(params_cld);
467 config_log_put(sptlrpc_cld);
472 DEFINE_MUTEX(llog_process_lock);
474 static inline void config_mark_cld_stop_nolock(struct config_llog_data *cld)
478 spin_lock(&config_list_lock);
479 cld->cld_stopping = 1;
480 spin_unlock(&config_list_lock);
482 CDEBUG(D_INFO, "lockh %#llx\n", cld->cld_lockh.cookie);
483 if (!ldlm_lock_addref_try(&cld->cld_lockh, LCK_CR))
484 ldlm_lock_decref_and_cancel(&cld->cld_lockh, LCK_CR);
487 static inline void config_mark_cld_stop(struct config_llog_data *cld)
490 mutex_lock(&cld->cld_lock);
491 config_mark_cld_stop_nolock(cld);
492 mutex_unlock(&cld->cld_lock);
496 /** Stop watching for updates on this log.
498 static int config_log_end(char *logname, struct config_llog_instance *cfg)
500 struct config_llog_data *cld;
501 struct config_llog_data *cld_sptlrpc = NULL;
502 struct config_llog_data *cld_params = NULL;
503 struct config_llog_data *cld_recover = NULL;
504 struct config_llog_data *cld_nodemap = NULL;
505 struct config_llog_data *cld_barrier = NULL;
510 cld = config_log_find(logname, cfg);
514 mutex_lock(&cld->cld_lock);
516 * if cld_stopping is set, it means we didn't start the log thus
517 * not owning the start ref. this can happen after previous umount:
518 * the cld still hanging there waiting for lock cancel, and we
519 * remount again but failed in the middle and call log_end without
522 if (unlikely(cld->cld_stopping)) {
523 mutex_unlock(&cld->cld_lock);
524 /* drop the ref from the find */
529 cld_recover = cld->cld_recover;
530 cld->cld_recover = NULL;
531 cld_params = cld->cld_params;
532 cld->cld_params = NULL;
533 cld_nodemap = cld->cld_nodemap;
534 cld->cld_nodemap = NULL;
535 cld_barrier = cld->cld_barrier;
536 cld->cld_barrier = NULL;
537 cld_sptlrpc = cld->cld_sptlrpc;
538 cld->cld_sptlrpc = NULL;
540 config_mark_cld_stop_nolock(cld);
541 mutex_unlock(&cld->cld_lock);
543 config_mark_cld_stop(cld_recover);
544 config_log_put(cld_recover);
545 config_mark_cld_stop(cld_params);
546 config_log_put(cld_params);
547 config_mark_cld_stop(cld_barrier);
548 config_log_put(cld_barrier);
549 /* don't explicitly set cld_stopping on sptlrpc lock here, as other
550 * targets may be active, it will be done in config_log_put if necessary
552 config_log_put(cld_sptlrpc);
553 /* don't set cld_stopping on nm lock as other targets may be active */
554 config_log_put(cld_nodemap);
556 /* drop the ref from the find */
558 /* drop the start ref */
561 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
566 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
568 struct obd_device *obd = data;
569 struct obd_import *imp;
570 struct obd_connect_data *ocd;
571 struct config_llog_data *cld;
576 with_imp_locked(obd, imp, rc) {
577 ocd = &imp->imp_connect_data;
579 seq_printf(m, "imperative_recovery: %s\n",
580 OCD_HAS_FLAG(ocd, IMP_RECOV) ?
581 "ENABLED" : "DISABLED");
586 seq_puts(m, "client_state:\n");
588 spin_lock(&config_list_lock);
589 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
590 if (cld->cld_recover == NULL)
592 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
594 cld->cld_recover->cld_cfg.cfg_last_idx);
596 spin_unlock(&config_list_lock);
601 /* reenqueue any lost locks */
602 #define RQ_RUNNING 0x1
606 #define RQ_PRECLEANUP 0x10
608 static wait_queue_head_t rq_waitq;
609 static DECLARE_COMPLETION(rq_exit);
610 static DECLARE_COMPLETION(rq_start);
612 static void do_requeue(struct config_llog_data *cld)
619 * Do not run mgc_process_log on a disconnected export or an
620 * export which is being disconnected. Take the client
621 * semaphore to make the check non-racy.
623 down_read_nested(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem,
625 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
626 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
627 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
628 if (rc && rc != -ENOENT)
629 CERROR("failed processing log: %d\n", rc);
631 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
634 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
639 static int mgc_requeue_thread(void *data)
646 CDEBUG(D_MGC, "Starting requeue thread\n");
648 /* Keep trying failed locks periodically */
649 spin_lock(&config_list_lock);
650 rq_state |= RQ_RUNNING;
651 while (!(rq_state & RQ_STOP)) {
652 struct config_llog_data *cld, *cld_prev;
655 /* Any new or requeued lostlocks will change the state */
656 rq_state &= ~(RQ_NOW | RQ_LATER);
657 spin_unlock(&config_list_lock);
664 /* Always wait a few seconds to allow the server who
665 * caused the lock revocation to finish its setup, plus some
666 * random so everyone doesn't try to reconnect at once.
668 to = mgc_requeue_timeout_min == 0 ? 1 : mgc_requeue_timeout_min;
669 to = cfs_time_seconds(mgc_requeue_timeout_min) +
670 get_random_u32_below(cfs_time_seconds(to));
671 wait_event_idle_timeout(rq_waitq,
672 rq_state & (RQ_STOP | RQ_PRECLEANUP),
676 * iterate & processing through the list. for each cld, process
677 * its depending sptlrpc cld firstly (if any) and then itself.
679 * it's guaranteed any item in the list must have
680 * reference > 0; and if cld_lostlock is set, at
681 * least one reference is taken by the previous enqueue.
685 spin_lock(&config_list_lock);
686 rq_state &= ~RQ_PRECLEANUP;
687 list_for_each_entry(cld, &config_llog_list,
689 if (!cld->cld_lostlock || cld->cld_stopping)
692 /* hold reference to avoid being freed during
693 * subsequent processing.
696 cld->cld_lostlock = 0;
697 spin_unlock(&config_list_lock);
699 config_log_put(cld_prev);
702 if (likely(!(rq_state & RQ_STOP))) {
704 spin_lock(&config_list_lock);
706 spin_lock(&config_list_lock);
710 spin_unlock(&config_list_lock);
711 config_log_put(cld_prev);
713 /* Wait a bit to see if anyone else needs a requeue */
714 wait_event_idle(rq_waitq, rq_state & (RQ_NOW | RQ_STOP));
715 spin_lock(&config_list_lock);
718 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
719 rq_state &= ~RQ_RUNNING;
720 spin_unlock(&config_list_lock);
724 CDEBUG(D_MGC, "Ending requeue thread\n");
728 /* Add a cld to the list to requeue. Start the requeue thread if needed.
729 * We are responsible for dropping the config log reference from here on out.
731 static void mgc_requeue_add(struct config_llog_data *cld)
737 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
738 cld->cld_logname, refcount_read(&cld->cld_refcount),
739 cld->cld_stopping, rq_state);
741 /* lets cancel an existent lock to mark cld as "lostlock" */
742 CDEBUG(D_INFO, "lockh %#llx\n", cld->cld_lockh.cookie);
743 if (!ldlm_lock_addref_try(&cld->cld_lockh, LCK_CR))
744 ldlm_lock_decref_and_cancel(&cld->cld_lockh, LCK_CR);
746 mutex_lock(&cld->cld_lock);
747 spin_lock(&config_list_lock);
748 if (!(rq_state & RQ_STOP) && !cld->cld_stopping) {
749 cld->cld_lostlock = 1;
753 spin_unlock(&config_list_lock);
754 mutex_unlock(&cld->cld_lock);
761 /********************** class fns **********************/
762 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
764 struct llog_ctxt *ctxt;
769 /* setup only remote ctxt, the local disk context is switched per each
770 * filesystem during mgc_fs_setup()
772 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
777 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
780 llog_initiator_connect(ctxt);
786 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
788 struct llog_ctxt *ctxt;
792 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
794 llog_cleanup(env, ctxt);
800 static atomic_t mgc_count = ATOMIC_INIT(0);
801 static int mgc_precleanup(struct obd_device *obd)
808 if (atomic_dec_and_test(&mgc_count)) {
809 LASSERT(rq_state & RQ_RUNNING);
810 /* stop requeue thread */
813 /* wakeup requeue thread to clean our cld */
814 temp = RQ_NOW | RQ_PRECLEANUP;
817 spin_lock(&config_list_lock);
819 spin_unlock(&config_list_lock);
823 wait_for_completion(&rq_exit);
824 obd_cleanup_client_import(obd);
826 rc = mgc_llog_fini(NULL, obd);
828 CERROR("failed to cleanup llogging subsystems\n");
833 static int mgc_cleanup(struct obd_device *obd)
839 /* COMPAT_146 - old config logs may have added profiles secretly */
840 if (atomic_read(&obd->obd_type->typ_refcnt) <= 1)
841 /* Only for the last mgc */
842 class_del_profiles();
844 lprocfs_obd_cleanup(obd);
847 rc = client_obd_cleanup(obd);
851 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
853 struct task_struct *task;
858 rc = ptlrpcd_addref();
862 rc = client_obd_setup(obd, lcfg);
864 GOTO(err_decref, rc);
866 rc = mgc_llog_init(NULL, obd);
868 CERROR("failed to setup llogging subsystems\n");
869 GOTO(err_cleanup, rc);
872 rc = mgc_tunables_init(obd);
876 if (atomic_inc_return(&mgc_count) == 1) {
878 init_waitqueue_head(&rq_waitq);
880 /* start requeue thread */
881 task = kthread_run(mgc_requeue_thread, NULL, "ll_cfg_requeue");
884 CERROR("%s: cannot start requeue thread: rc = %d; "
885 "no more log updates\n",
889 /* rc is the task_struct pointer of mgc_requeue_thread. */
891 wait_for_completion(&rq_start);
897 lprocfs_obd_cleanup(obd);
899 client_obd_cleanup(obd);
905 /* based on ll_mdc_blocking_ast */
906 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
907 void *data, int flag)
909 struct lustre_handle lockh;
910 struct config_llog_data *cld = (struct config_llog_data *)data;
916 case LDLM_CB_BLOCKING:
917 /* mgs wants the lock, give it up... */
918 LDLM_DEBUG(lock, "MGC blocking CB");
919 ldlm_lock2handle(lock, &lockh);
920 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
922 case LDLM_CB_CANCELING:
923 /* We've given up the lock, prepare ourselves to update. */
924 LDLM_DEBUG(lock, "MGC cancel CB");
926 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
927 PLDLMRES(lock->l_resource),
928 (char *)&lock->l_resource->lr_name.name[0]);
931 CDEBUG(D_INFO, "missing data, won't requeue\n");
935 lock->l_ast_data = NULL;
936 cld->cld_lockh.cookie = 0;
937 /* Are we done with this log? */
938 if (cld->cld_stopping) {
939 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
944 /* Make sure not to re-enqueue when the mgc is stopping
945 * (we get called from client_disconnect_export)
947 if (lock->l_conn_export == NULL ||
948 lock->l_conn_export->exp_obd->u.cli.cl_conn_count == 0) {
949 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
956 mgc_requeue_add(cld);
966 /* Take a config lock so we can get cancel notifications */
967 static int mgc_enqueue(struct obd_export *exp, enum ldlm_type type,
968 union ldlm_policy_data *policy, enum ldlm_mode mode,
969 __u64 *flags, ldlm_glimpse_callback glimpse_callback,
970 void *data, __u32 lvb_len, void *lvb_swabber,
971 struct lustre_handle *lockh)
973 struct config_llog_data *cld = (struct config_llog_data *)data;
974 struct ldlm_enqueue_info einfo = {
977 .ei_cb_bl = mgc_blocking_ast,
978 .ei_cb_cp = ldlm_completion_ast,
979 .ei_cb_gl = glimpse_callback,
981 struct ptlrpc_request *req;
982 int short_limit = cld_is_sptlrpc(cld);
990 CDEBUG(D_MGC, "Enqueue for %s (res %#llx)\n", cld->cld_logname,
991 cld->cld_resid.name[0]);
993 /* We need a callback for every lockholder, so don't try to
994 * ldlm_lock_match (see rev 1.1.2.11.2.47)
996 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
997 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1002 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1003 ptlrpc_request_set_replen(req);
1005 /* check if this is server or client */
1006 if (cld->cld_cfg.cfg_sb &&
1007 IS_SERVER(s2lsi(cld->cld_cfg.cfg_sb)))
1010 /* Limit how long we will wait for the enqueue to complete */
1011 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT(exp->exp_obd);
1012 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1013 NULL, 0, LVB_T_NONE, lockh, 0);
1014 /* A failed enqueue should still call the mgc_blocking_ast,
1015 * where it will be requeued if needed ("grant failed").
1017 ptlrpc_req_finished(req);
1021 static int mgc_cancel(struct obd_export *exp, enum ldlm_mode mode,
1022 struct lustre_handle *lockh)
1026 ldlm_lock_decref(lockh, mode);
1031 static void mgc_notify_active(struct obd_device *unused)
1033 /* wakeup mgc_requeue_thread to requeue mgc lock */
1034 spin_lock(&config_list_lock);
1036 spin_unlock(&config_list_lock);
1039 /* TODO: Help the MGS rebuild nidtbl. -jay */
1042 static int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1043 u32 keylen, void *key,
1044 u32 vallen, void *val,
1045 struct ptlrpc_request_set *set)
1051 /* Turn off initial_recov after we try all backup servers once */
1052 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1053 struct obd_import *imp = class_exp2cliimp(exp);
1056 if (vallen != sizeof(int))
1058 value = *(int *)val;
1059 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1060 imp->imp_obd->obd_name, value,
1061 imp->imp_deactive, imp->imp_invalid,
1062 imp->imp_replayable, imp->imp_obd->obd_replayable,
1063 ptlrpc_import_state_name(imp->imp_state));
1064 /* Resurrect the import immediately if
1065 * 1. we previously got disconnected,
1066 * 2. value > 1 (at the same node with MGS)
1069 struct adaptive_timeout *at;
1071 at = &imp->imp_at.iat_net_latency;
1072 at_reset(at, INITIAL_CONNECT_TIMEOUT);
1075 if (imp->imp_state == LUSTRE_IMP_DISCON || value > 1)
1076 ptlrpc_reconnect_import(imp);
1081 if (KEY_IS(KEY_MGSSEC)) {
1082 struct client_obd *cli = &exp->exp_obd->u.cli;
1083 struct sptlrpc_flavor flvr;
1086 * empty string means using current flavor, if which haven't
1087 * been set yet, set it as null.
1089 * if flavor has been set previously, check the asking flavor
1090 * must match the existing one.
1093 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1099 rc = sptlrpc_parse_flavor(val, &flvr);
1101 CERROR("invalid sptlrpc flavor %s to MGS\n",
1106 /* caller already hold a mutex */
1107 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1108 cli->cl_flvr_mgc = flvr;
1109 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1110 sizeof(flvr)) != 0) {
1113 sptlrpc_flavor2name(&cli->cl_flvr_mgc, str,
1115 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but currently %s is in use\n",
1122 #ifdef HAVE_SERVER_SUPPORT
1123 rc = mgc_set_info_async_server(env, exp, keylen, key, vallen, val, set);
1128 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1129 __u32 keylen, void *key, __u32 *vallen, void *val)
1133 if (KEY_IS(KEY_CONN_DATA)) {
1134 struct obd_import *imp = class_exp2cliimp(exp);
1135 struct obd_connect_data *data = val;
1137 if (*vallen == sizeof(*data)) {
1138 *data = imp->imp_connect_data;
1146 static int mgc_import_event(struct obd_device *obd,
1147 struct obd_import *imp,
1148 enum obd_import_event event)
1152 LASSERT(imp->imp_obd == obd);
1153 CDEBUG(D_MGC, "import event %#x\n", event);
1156 case IMP_EVENT_DISCON:
1157 /* MGC imports should not wait for recovery */
1158 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1159 ptlrpc_pinger_ir_down();
1161 case IMP_EVENT_INACTIVE:
1163 case IMP_EVENT_INVALIDATE: {
1164 struct ldlm_namespace *ns = obd->obd_namespace;
1166 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1169 case IMP_EVENT_ACTIVE:
1170 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1171 /* Clearing obd_no_recov allows us to continue pinging */
1172 obd->obd_no_recov = 0;
1173 mgc_notify_active(obd);
1174 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1175 ptlrpc_pinger_ir_up();
1179 case IMP_EVENT_DEACTIVATE:
1180 case IMP_EVENT_ACTIVATE:
1183 CERROR("Unknown import event %#x\n", event);
1189 static int mgc_apply_recover_logs(struct obd_device *mgc,
1190 struct config_llog_data *cld,
1192 void *data, int datalen, bool mne_swab)
1194 struct config_llog_instance *cfg = &cld->cld_cfg;
1195 struct lustre_cfg *lcfg;
1196 struct lustre_cfg_bufs bufs;
1197 u64 prev_version = 0;
1198 char inst[MTI_NAME_MAXLEN + 1];
1204 unsigned long dynamic_nids;
1207 LASSERT(cfg->cfg_instance != 0);
1208 LASSERT(ll_get_cfg_instance(cfg->cfg_sb) == cfg->cfg_instance);
1210 /* get dynamic nids setting */
1211 dynamic_nids = mgc->obd_dynamic_nids;
1213 if (!IS_SERVER(s2lsi(cfg->cfg_sb))) {
1214 pos = snprintf(inst, sizeof(inst), "%016lx", cfg->cfg_instance);
1215 if (pos >= PAGE_SIZE)
1217 #ifdef HAVE_SERVER_SUPPORT
1219 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1221 LASSERT(IS_MDT(lsi));
1222 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1226 #endif /* HAVE_SERVER_SUPPORT */
1229 OBD_ALLOC(buf, PAGE_SIZE);
1235 while (datalen > 0) {
1236 struct mgs_nidtbl_entry *entry = (data + off);
1237 struct lnet_nid *nidlist = NULL;
1238 int entry_len = sizeof(*entry);
1239 struct obd_device *obd;
1240 struct obd_import *imp;
1241 struct obd_uuid *uuid;
1249 if (datalen < entry_len) /* really short on data */
1252 /* swab non nid data */
1254 lustre_swab_mgs_nidtbl_entry_header(entry);
1256 if (entry->mne_nid_count == 0) /* at least one nid entry */
1259 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1260 if (datalen < entry_len) /* must have entry_len at least */
1263 if (entry->mne_length > PAGE_SIZE) {
1264 CERROR("MNE too large (%u)\n", entry->mne_length);
1268 /* improper mne_lenth */
1269 if (entry->mne_length < entry_len)
1272 /* entry length reports larger than all the data passed in */
1273 if (datalen < entry->mne_length)
1276 /* Looks OK. Can process this entry? else move to next entry */
1277 off += entry->mne_length;
1278 datalen -= entry->mne_length;
1282 if (entry->mne_version > max_version) {
1283 CERROR("entry index(%lld) is over max_index(%lld)\n",
1284 entry->mne_version, max_version);
1288 if (prev_version >= entry->mne_version) {
1289 CERROR("index unsorted, prev %lld, now %lld\n",
1290 prev_version, entry->mne_version);
1293 prev_version = entry->mne_version;
1295 if (entry->mne_nid_type == 0) {
1296 struct lnet_nid *nid;
1299 OBD_ALLOC_PTR_ARRAY(nidlist, entry->mne_nid_count);
1305 /* Keep this nid data swab for normal mixed
1306 * endian handling. LU-1644
1309 lustre_swab_mgs_nidtbl_entry_content(entry);
1311 /* Turn old NID format to newer format. */
1313 for (i = 0; i < entry->mne_nid_count; i++) {
1314 lnet_nid4_to_nid(entry->u.nids[i], nid);
1315 nid += sizeof(struct lnet_nid);
1318 /* Handle the case if struct lnet_nid is expanded in
1319 * the future. The MGS should prevent this but just
1322 if (entry->mne_nid_size > sizeof(struct lnet_nid))
1325 nidlist = entry->u.nidlist;
1329 * Write a string with format "nid::instance" to
1330 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1332 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1333 memset(buf, 0, bufsz);
1337 /* lustre-OST0001-osc-<instance #> */
1338 strcpy(obdname, cld->cld_logname);
1339 cname = strrchr(obdname, '-');
1340 if (cname == NULL) {
1341 CERROR("mgc %s: invalid logname %s\n",
1342 mgc->obd_name, obdname);
1343 if (entry->mne_nid_type == 0)
1344 OBD_FREE_PTR_ARRAY(nidlist,
1345 entry->mne_nid_count);
1349 pos = cname - obdname;
1351 pos += sprintf(obdname + pos, "-%s%04x",
1352 is_ost ? "OST" : "MDT", entry->mne_index);
1354 cname = is_ost ? "osc" : "mdc",
1355 pos += snprintf(obdname + pos, bufsz, "-%s-%s", cname,
1357 lustre_cfg_bufs_reset(&bufs, obdname);
1359 /* find the obd by obdname */
1360 obd = class_name2obd(obdname);
1362 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1363 mgc->obd_name, obdname);
1365 /* this is a safe race, when the ost is starting up...*/
1369 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1372 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1373 uuid = (struct obd_uuid *)(buf + pos);
1375 with_imp_locked(obd, imp, rc) {
1376 struct obd_uuid server_uuid;
1380 /* iterate all nids to find one */
1381 /* find uuid by nid */
1382 /* create import entries if they don't exist */
1383 rc = client_import_add_nids_to_conn(imp, nidlist,
1384 entry->mne_nid_count,
1385 entry->mne_nid_size,
1387 if (rc != -ENOENT || !dynamic_nids)
1390 /* create a new connection for this import */
1391 primary_nid = libcfs_nidstr(&nidlist[0]);
1392 prim_nid_len = strlen(primary_nid) + 1;
1393 if (prim_nid_len > UUID_MAX)
1396 strncpy(server_uuid.uuid, primary_nid,
1399 CDEBUG(D_INFO, "Adding a connection for %s\n",
1402 rc = client_import_dyn_add_conn(imp, &server_uuid,
1405 CERROR("%s: Failed to add new connection with NID '%s' to import: rc = %d\n",
1406 obd->obd_name, primary_nid, rc);
1410 rc = client_import_add_nids_to_conn(imp, nidlist,
1411 entry->mne_nid_count,
1412 entry->mne_nid_size,
1415 CERROR("%s: failed to lookup UUID: rc = %d\n",
1420 if (rc == -ENODEV) {
1421 /* client does not connect to the OST yet */
1426 if (rc < 0 && rc != -ENOSPC) {
1427 CERROR("mgc: cannot find UUID by nid '%s': rc = %d\n",
1428 libcfs_nidstr(&nidlist[0]), rc);
1430 /* For old NID format case the nidlist was allocated. */
1431 if (entry->mne_nid_type == 0)
1432 OBD_FREE_PTR_ARRAY(nidlist,
1433 entry->mne_nid_count);
1437 CDEBUG(D_INFO, "Found UUID '%s' by NID '%s'\n",
1438 uuid->uuid, libcfs_nidstr(&nidlist[0]));
1440 pos += strlen(uuid->uuid);
1441 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1442 LASSERT(pos < bufsz);
1444 lustre_cfg_bufs_set_string(&bufs, 1, params);
1446 OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount,
1452 lustre_cfg_init(lcfg, LCFG_PARAM, &bufs);
1454 CDEBUG(D_INFO, "ir apply logs %lld/%lld for %s -> %s\n",
1455 prev_version, max_version, obdname, params);
1457 rc = class_process_config(lcfg);
1458 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
1459 lcfg->lcfg_buflens));
1461 CDEBUG(D_INFO, "process config for %s error %d\n",
1464 /* continue, even one with error */
1466 /* For old NID format case the nidlist was allocated. */
1467 if (entry->mne_nid_type == 0)
1468 OBD_FREE_PTR_ARRAY(nidlist, entry->mne_nid_count);
1471 OBD_FREE(buf, PAGE_SIZE);
1477 * This function is called if this client was notified for target restarting
1478 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery or
1481 static int mgc_process_recover_log(struct obd_device *obd,
1482 struct config_llog_data *cld)
1484 struct ptlrpc_request *req = NULL;
1485 struct config_llog_instance *cfg = &cld->cld_cfg;
1486 struct mgs_config_body *body;
1487 struct mgs_config_res *res;
1488 struct ptlrpc_bulk_desc *desc;
1489 struct page **pages = NULL;
1492 bool mne_swab = false;
1499 /* allocate buffer for bulk transfer.
1500 * if this is the first time for this mgs to read logs,
1501 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1502 * once; otherwise, it only reads increment of logs, this should be
1503 * small and CONFIG_READ_NRPAGES will be used.
1505 nrpages = CONFIG_READ_NRPAGES;
1506 if (cfg->cfg_last_idx == 0)
1507 nrpages = CONFIG_READ_NRPAGES_INIT;
1509 OBD_ALLOC_PTR_ARRAY_LARGE(pages, nrpages);
1511 GOTO(out, rc = -ENOMEM);
1513 for (i = 0; i < nrpages; i++) {
1514 pages[i] = alloc_page(GFP_KERNEL);
1515 if (pages[i] == NULL)
1516 GOTO(out, rc = -ENOMEM);
1520 LASSERT(cld_is_recover(cld));
1521 LASSERT(mutex_is_locked(&cld->cld_lock));
1522 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1523 &RQF_MGS_CONFIG_READ);
1525 GOTO(out, rc = -ENOMEM);
1527 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1532 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1533 LASSERT(body != NULL);
1534 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1535 rc = strscpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name));
1538 body->mcb_offset = cfg->cfg_last_idx + 1;
1539 body->mcb_type = cld->cld_type;
1540 body->mcb_bits = PAGE_SHIFT;
1541 body->mcb_units = nrpages;
1542 body->mcb_rec_nid_size = sizeof(struct lnet_nid);
1544 /* allocate bulk transfer descriptor */
1545 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1,
1546 PTLRPC_BULK_PUT_SINK,
1548 &ptlrpc_bulk_kiov_pin_ops);
1550 GOTO(out, rc = -ENOMEM);
1552 for (i = 0; i < nrpages; i++)
1553 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1556 ptlrpc_request_set_replen(req);
1557 rc = ptlrpc_queue_wait(req);
1561 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1563 GOTO(out, rc = -EPROTO);
1565 if (res->mcr_size < res->mcr_offset)
1566 GOTO(out, rc = -EINVAL);
1568 /* always update the index even though it might have errors with
1569 * handling the recover logs
1571 cfg->cfg_last_idx = res->mcr_offset;
1572 eof = res->mcr_offset == res->mcr_size;
1574 CDEBUG(D_INFO, "Latest version %lld, more %d.\n",
1575 res->mcr_offset, eof == false);
1577 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1579 GOTO(out, rc = ealen);
1581 if (ealen > nrpages << PAGE_SHIFT)
1582 GOTO(out, rc = -EINVAL);
1584 if (ealen == 0) { /* no logs transferred */
1590 mne_swab = req_capsule_rep_need_swab(&req->rq_pill);
1592 for (i = 0; i < nrpages && ealen > 0; i++) {
1596 ptr = kmap(pages[i]);
1597 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1598 min_t(int, ealen, PAGE_SIZE),
1602 CWARN("%s: error processing %s log recovery: rc = %d\n",
1606 GOTO(out, rc = rc2);
1614 ptlrpc_req_finished(req);
1618 if (rc == 0 && !eof)
1622 for (i = 0; i < nrpages; i++) {
1623 if (pages[i] == NULL)
1625 __free_page(pages[i]);
1627 OBD_FREE_PTR_ARRAY_LARGE(pages, nrpages);
1632 /* local_only means it cannot get remote llogs */
1633 static int mgc_process_cfg_log(struct obd_device *mgc,
1634 struct config_llog_data *cld, int local_only)
1636 struct llog_ctxt *ctxt;
1637 struct lustre_sb_info *lsi = NULL;
1643 LASSERT(mutex_is_locked(&cld->cld_lock));
1645 if (cld->cld_cfg.cfg_sb)
1646 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1648 /* sptlrpc llog must not keep ref to sb,
1649 * it was just needed to get lsi
1651 if (cld_is_sptlrpc(cld))
1652 cld->cld_cfg.cfg_sb = NULL;
1658 rc = lu_env_init(env, LCT_MG_THREAD);
1662 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1664 #ifdef HAVE_SERVER_SUPPORT
1665 rc = mgc_process_server_cfg_log(env, &ctxt, lsi, mgc, cld,
1667 if (rc == -EIO && local_only)
1669 if (rc && rc != -ENOENT)
1672 GOTO(out_pop, rc = -EIO);
1674 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1678 * update settings on existing OBDs.
1679 * the logname must be <fsname>-sptlrpc
1681 if (rc == 0 && cld_is_sptlrpc(cld))
1682 class_notify_sptlrpc_conf(cld->cld_logname,
1683 strlen(cld->cld_logname) -
1684 strlen("-sptlrpc"));
1688 __llog_ctxt_put(env, ctxt);
1695 static bool mgc_import_in_recovery(struct obd_import *imp)
1697 bool in_recovery = true;
1699 spin_lock(&imp->imp_lock);
1700 if (imp->imp_state == LUSTRE_IMP_FULL ||
1701 imp->imp_state == LUSTRE_IMP_CLOSED)
1702 in_recovery = false;
1703 spin_unlock(&imp->imp_lock);
1709 * Get a configuration log from the MGS and process it.
1711 * This function is called for both clients and servers to process the
1712 * configuration log from the MGS. The MGC enqueues a DLM lock on the
1713 * log from the MGS, and if the lock gets revoked the MGC will be notified
1714 * by the lock cancellation callback that the config log has changed,
1715 * and will enqueue another MGS lock on it, and then continue processing
1716 * the new additions to the end of the log.
1718 * Since the MGC import is not replayable, if the import is being evicted
1719 * (rcl == -ESHUTDOWN, \see ptlrpc_import_delay_req()), retry to process
1720 * the log until recovery is finished or the import is closed.
1722 * Make a local copy of the log before parsing it if appropriate (non-MGS
1723 * server) so that the server can start even when the MGS is down.
1725 * There shouldn't be multiple processes running process_log at once --
1726 * sounds like badness. It actually might be fine, as long as they're not
1727 * trying to update from the same log simultaneously, in which case we
1728 * should use a per-log semaphore instead of cld_lock.
1730 * \param[in] mgc MGC device by which to fetch the configuration log
1731 * \param[in] cld log processing state (stored in lock callback data)
1733 * \retval 0 on success
1734 * \retval negative errno on failure
1736 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1738 struct lustre_handle lockh = { 0 };
1739 __u64 flags = LDLM_FL_NO_LRU;
1745 LASSERT(cld != NULL);
1747 /* I don't want multiple processes running process_log at once --
1748 * sounds like badness. It actually might be fine, as long as
1749 * we're not trying to update from the same log
1750 * simultaneously (in which case we should use a per-log sem.)
1753 mutex_lock(&cld->cld_lock);
1754 if (cld->cld_stopping) {
1755 mutex_unlock(&cld->cld_lock);
1759 CFS_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1761 CDEBUG(D_MGC, "Process log %s-%016lx from %d\n", cld->cld_logname,
1762 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1764 /* Get the cfg lock on the llog */
1765 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, LDLM_PLAIN, NULL,
1767 cld_is_barrier(cld) ? mgc_barrier_glimpse_ast : NULL,
1768 cld, 0, NULL, &lockh);
1770 /* Get the cld, it will be released in mgc_blocking_ast. */
1771 config_log_get(cld);
1772 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1773 LASSERT(!lustre_handle_is_used(&cld->cld_lockh));
1775 cld->cld_lockh = lockh;
1777 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1778 cld->cld_lockh.cookie = 0;
1780 if (rcl == -ESHUTDOWN &&
1781 atomic_read(&mgc->u.cli.cl_mgc_refcount) > 0 && !retry) {
1782 struct obd_import *imp;
1783 long timeout = cfs_time_seconds(obd_timeout);
1785 mutex_unlock(&cld->cld_lock);
1786 imp = class_exp2cliimp(mgc->u.cli.cl_mgc_mgsexp);
1788 /* Let's force the pinger, and wait the import to be
1789 * connected, note: since mgc import is non-replayable,
1790 * and even the import state is disconnected, it does
1791 * not mean the "recovery" is stopped, so we will keep
1792 * waitting until timeout or the import state is
1795 ptlrpc_pinger_force(imp);
1797 wait_event_idle_timeout(imp->imp_recovery_waitq,
1798 !mgc_import_in_recovery(imp),
1801 if (imp->imp_state == LUSTRE_IMP_FULL) {
1805 mutex_lock(&cld->cld_lock);
1806 /* unlock/lock mutex, so check stopping again */
1807 if (cld->cld_stopping) {
1808 mutex_unlock(&cld->cld_lock);
1811 spin_lock(&config_list_lock);
1812 cld->cld_lostlock = 1;
1813 spin_unlock(&config_list_lock);
1816 /* mark cld_lostlock so that it will requeue
1817 * after MGC becomes available.
1819 spin_lock(&config_list_lock);
1820 cld->cld_lostlock = 1;
1821 spin_unlock(&config_list_lock);
1825 if (cld_is_recover(cld) && !rcl)
1826 rc = mgc_process_recover_log(mgc, cld);
1827 #ifdef HAVE_SERVER_SUPPORT
1828 else if (cld_is_nodemap(cld)) {
1832 rc = mgc_process_nodemap_log(mgc, cld);
1835 else if (!cld_is_barrier(cld))
1836 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1838 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1839 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1840 if (rc != -ETIMEDOUT && rc != -EIO && rc != -EAGAIN) {
1841 cld->cld_processed = 1;
1845 /* Now drop the lock so MGS can revoke it */
1847 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, LCK_CR, &lockh);
1849 CERROR("Can't drop cfg lock: %d\n", rcl);
1851 mutex_unlock(&cld->cld_lock);
1853 /* requeue nodemap lock immediately if transfer was interrupted */
1854 if ((cld_is_nodemap(cld) && rc == -EAGAIN) ||
1855 (cld_is_recover(cld) && rc)) {
1856 if (cld_is_recover(cld))
1857 CWARN("%s: IR log %s failed, not fatal: rc = %d\n",
1858 mgc->obd_name, cld->cld_logname, rc);
1859 mgc_requeue_add(cld);
1867 /* Called from lustre_process_log.
1868 * LCFG_LOG_START gets the config log from the MGS, processes it to start
1869 * any services, and adds it to the list logs to watch (follow).
1871 static int mgc_process_config(struct obd_device *obd, size_t len, void *buf)
1873 struct lustre_cfg *lcfg = buf;
1874 struct config_llog_instance *cfg = NULL;
1879 #ifdef HAVE_SERVER_SUPPORT
1880 rc = mgc_process_config_server(obd, len, buf);
1885 switch (lcfg->lcfg_command) {
1886 case LCFG_SPTLRPC_CONF: {
1887 rc = sptlrpc_process_config(lcfg);
1890 case LCFG_LOG_START: {
1891 struct config_llog_data *cld;
1892 struct super_block *sb;
1894 logname = lustre_cfg_string(lcfg, 1);
1895 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1896 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1898 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1901 /* We're only called through here on the initial mount */
1902 cld = config_log_add(obd, logname, cfg, sb);
1908 /* if it exists, the sptlrpc config log really needs to be
1909 * correctly processed before processing other logs,
1910 * otherwise client might use incorrect sec flavor
1912 if (cld->cld_sptlrpc && !cld->cld_sptlrpc->cld_processed) {
1913 unsigned int timeout = 120;
1915 /* we do not want to wait forever,
1916 * we prefer a (excessively) long timeout
1918 timeout = max(20 * mgc_requeue_timeout_min, timeout);
1919 wait_event_idle_timeout(rq_waitq,
1920 cld->cld_sptlrpc->cld_processed,
1921 cfs_time_seconds(timeout));
1924 rc = mgc_process_log(obd, cld);
1925 if (rc == 0 && cld->cld_recover != NULL) {
1926 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
1927 imp_connect_data, IMP_RECOV)) {
1928 rc = mgc_process_log(obd, cld->cld_recover);
1930 struct config_llog_data *cir;
1932 mutex_lock(&cld->cld_lock);
1933 cir = cld->cld_recover;
1934 cld->cld_recover = NULL;
1935 mutex_unlock(&cld->cld_lock);
1936 config_log_put(cir);
1940 CERROR("Cannot process recover llog %d\n", rc);
1943 if (rc == 0 && cld->cld_params != NULL) {
1944 rc = mgc_process_log(obd, cld->cld_params);
1945 if (rc == -ENOENT) {
1947 "There is no params config file yet\n");
1950 /* params log is optional */
1952 CERROR("%s: can't process params llog: rc = %d\n",
1958 case LCFG_LOG_END: {
1959 logname = lustre_cfg_string(lcfg, 1);
1961 if (lcfg->lcfg_bufcount >= 2)
1962 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1963 rc = config_log_end(logname, cfg);
1967 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1975 static const struct obd_ops mgc_obd_ops = {
1976 .o_owner = THIS_MODULE,
1977 .o_setup = mgc_setup,
1978 .o_precleanup = mgc_precleanup,
1979 .o_cleanup = mgc_cleanup,
1980 .o_add_conn = client_import_add_conn,
1981 .o_del_conn = client_import_del_conn,
1982 .o_connect = client_connect_import,
1983 .o_disconnect = client_disconnect_export,
1984 .o_set_info_async = mgc_set_info_async,
1985 .o_get_info = mgc_get_info,
1986 .o_import_event = mgc_import_event,
1987 .o_process_config = mgc_process_config,
1990 static int mgc_param_requeue_timeout_min_set(const char *val,
1991 cfs_kernel_param_arg_t *kp)
1996 rc = kstrtouint(val, 0, &num);
2002 mgc_requeue_timeout_min = num;
2007 static const struct kernel_param_ops param_ops_requeue_timeout_min = {
2008 .set = mgc_param_requeue_timeout_min_set,
2009 .get = param_get_uint,
2012 #define param_check_requeue_timeout_min(name, p) \
2013 __param_check(name, p, unsigned int)
2015 unsigned int mgc_requeue_timeout_min = MGC_TIMEOUT_MIN_SECONDS;
2016 #ifdef HAVE_KERNEL_PARAM_OPS
2017 module_param(mgc_requeue_timeout_min, requeue_timeout_min, 0644);
2019 module_param_call(mgc_requeue_timeout_min, mgc_param_requeue_timeout_min_set,
2020 param_get_uint, ¶m_ops_requeue_timeout_min, 0644);
2022 MODULE_PARM_DESC(mgc_requeue_timeout_min, "Minimal requeue time to refresh logs");
2024 static int __init mgc_init(void)
2028 rc = libcfs_setup();
2032 return class_register_type(&mgc_obd_ops, NULL, false,
2033 LUSTRE_MGC_NAME, NULL);
2036 static void __exit mgc_exit(void)
2038 class_unregister_type(LUSTRE_MGC_NAME);
2041 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2042 MODULE_DESCRIPTION("Lustre Management Client");
2043 MODULE_VERSION(LUSTRE_VERSION_STRING);
2044 MODULE_LICENSE("GPL");
2046 module_init(mgc_init);
2047 module_exit(mgc_exit);