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;
212 CDEBUG(D_MGC, "do adding config log %s-%016lx\n", logname,
213 cfg ? cfg->cfg_instance : 0);
215 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
217 RETURN(ERR_PTR(-ENOMEM));
219 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
221 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
225 strcpy(cld->cld_logname, logname);
229 cld->cld_cfg.cfg_callback = class_config_llog_handler;
230 mutex_init(&cld->cld_lock);
231 cld->cld_cfg.cfg_last_idx = 0;
232 cld->cld_cfg.cfg_flags = 0;
233 cld->cld_cfg.cfg_sb = sb;
234 cld->cld_type = type;
235 refcount_set(&cld->cld_refcount, 1);
237 /* Keep the mgc around until we are done */
238 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
240 if (cld_is_sptlrpc(cld))
241 sptlrpc_conf_log_start(logname);
243 spin_lock(&config_list_lock);
244 list_add(&cld->cld_list_chain, &config_llog_list);
245 spin_unlock(&config_list_lock);
247 if (cld_is_sptlrpc(cld) || cld_is_nodemap(cld) || cld_is_barrier(cld)) {
248 rc = mgc_process_log(obd, cld);
249 if (rc && rc != -ENOENT)
250 CERROR("%s: failed processing log, type %d: rc = %d\n",
251 obd->obd_name, type, rc);
257 static struct config_llog_data *
258 config_recover_log_add(struct obd_device *obd, char *fsname,
259 struct config_llog_instance *cfg,
260 struct super_block *sb)
262 struct config_llog_instance lcfg = *cfg;
263 struct config_llog_data *cld;
264 bool is_server = IS_SERVER(s2lsi(sb));
267 #ifdef HAVE_SERVER_SUPPORT
268 if (IS_OST(s2lsi(sb)))
271 /* for osp-on-ost, see lustre_start_osp() */
272 if (IS_MDT(s2lsi(sb)) && 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));
281 LASSERT(is_server ? lcfg.cfg_instance == 0 : lcfg.cfg_instance != 0);
283 lcfg.cfg_instance = ll_get_cfg_instance(sb);
284 scnprintf(logname, sizeof(logname), "%s-%s", fsname,
285 is_server ? "mdtir" : "cliir");
287 cld = do_config_log_add(obd, logname, MGS_CFG_T_RECOVER, &lcfg, sb);
291 static struct config_llog_data *
292 config_log_find_or_add(struct obd_device *obd, char *logname,
293 struct super_block *sb, enum mgs_cfg_type type,
294 struct config_llog_instance *cfg)
296 struct config_llog_instance lcfg = *cfg;
297 struct config_llog_data *cld;
299 /* Note class_config_llog_handler() depends on getting "obd" back */
300 /* for sptlrpc, sb is only provided to be able to make a local copy,
301 * not for the instance
303 if (sb && type != MGS_CFG_T_SPTLRPC)
304 lcfg.cfg_instance = ll_get_cfg_instance(sb);
306 lcfg.cfg_instance = (unsigned long)obd;
308 cld = config_log_find(logname, &lcfg);
309 if (unlikely(cld != NULL))
312 return do_config_log_add(obd, logname, type, &lcfg, sb);
315 /** Add this log to the list of active logs watched by an MGC.
316 * Active means we're watching for updates.
317 * We have one active log per "mount" - client instance or servername.
318 * Each instance may be at a different point in the log.
320 static struct config_llog_data *
321 config_log_add(struct obd_device *obd, char *logname,
322 struct config_llog_instance *cfg, struct super_block *sb)
324 struct lustre_sb_info *lsi = s2lsi(sb);
325 struct config_llog_data *cld = NULL;
326 struct config_llog_data *sptlrpc_cld = NULL;
327 struct config_llog_data *params_cld = NULL;
328 #ifdef HAVE_SERVER_SUPPORT
329 struct config_llog_data *nodemap_cld = NULL;
330 struct config_llog_data *barrier_cld = NULL;
339 CDEBUG(D_MGC, "add config log %s-%016lx\n", logname,
343 * for each regular log, the depended sptlrpc log name is
344 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
346 ptr = strrchr(logname, '-');
347 if (ptr == NULL || ptr - logname > 8) {
348 CERROR("logname %s is too long\n", logname);
349 RETURN(ERR_PTR(-EINVAL));
352 memcpy(seclogname, logname, ptr - logname);
353 strcpy(seclogname + (ptr - logname), "-sptlrpc");
355 if (cfg->cfg_sub_clds & CONFIG_SUB_SPTLRPC) {
356 sptlrpc_cld = config_log_find_or_add(obd, seclogname, sb,
357 MGS_CFG_T_SPTLRPC, cfg);
358 if (IS_ERR(sptlrpc_cld)) {
359 rc = PTR_ERR(sptlrpc_cld);
360 CERROR("%s: can't create sptlrpc log %s: rc = %d\n",
361 obd->obd_name, seclogname, rc);
366 if (cfg->cfg_sub_clds & CONFIG_SUB_PARAMS) {
367 params_cld = config_log_find_or_add(obd, PARAMS_FILENAME, sb,
368 MGS_CFG_T_PARAMS, cfg);
369 if (IS_ERR(params_cld)) {
370 rc = PTR_ERR(params_cld);
371 CERROR("%s: can't create params log: rc = %d\n",
373 GOTO(out_sptlrpc, rc);
377 #ifdef HAVE_SERVER_SUPPORT
378 if (!IS_MGS(lsi) && cfg->cfg_sub_clds & CONFIG_SUB_NODEMAP) {
379 nodemap_cld = config_log_find_or_add(obd, LUSTRE_NODEMAP_NAME,
380 NULL, MGS_CFG_T_NODEMAP,
382 if (IS_ERR(nodemap_cld)) {
383 rc = PTR_ERR(nodemap_cld);
384 CERROR("%s: cannot create nodemap log: rc = %d\n",
386 GOTO(out_params, rc);
390 if (IS_MDT(s2lsi(sb)) && cfg->cfg_sub_clds & CONFIG_SUB_BARRIER) {
391 snprintf(seclogname + (ptr - logname), sizeof(seclogname) - 1,
392 "-%s", BARRIER_FILENAME);
393 barrier_cld = config_log_find_or_add(obd, seclogname, sb,
394 MGS_CFG_T_BARRIER, cfg);
395 if (IS_ERR(barrier_cld)) {
396 rc = PTR_ERR(barrier_cld);
397 CERROR("%s: can't create barrier log: rc = %d\n",
399 GOTO(out_nodemap, rc);
403 cld = do_config_log_add(obd, logname, MGS_CFG_T_CONFIG, cfg, sb);
406 CERROR("%s: can't create log: rc = %d\n",
408 #ifdef HAVE_SERVER_SUPPORT
409 GOTO(out_barrier, rc);
411 GOTO(out_params, rc);
415 LASSERT(lsi->lsi_lmd);
416 if (!test_bit(LMD_FLG_NOIR, lsi->lsi_lmd->lmd_flags) &&
417 cfg->cfg_sub_clds & CONFIG_SUB_RECOVER) {
418 struct config_llog_data *recover_cld;
420 ptr = strrchr(seclogname, '-');
424 CERROR("%s: sptlrpc log name not correct, %s: rc = %d\n",
425 obd->obd_name, seclogname, -EINVAL);
426 GOTO(out_cld, rc = -EINVAL);
429 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
430 if (IS_ERR(recover_cld)) {
431 rc = PTR_ERR(recover_cld);
432 CERROR("%s: can't create recover log: rc = %d\n",
437 mutex_lock(&cld->cld_lock);
439 cld->cld_recover = recover_cld;
443 mutex_lock(&cld->cld_lock);
444 #ifdef HAVE_SERVER_SUPPORT
445 cld->cld_barrier = barrier_cld;
446 cld->cld_nodemap = nodemap_cld;
448 cld->cld_params = params_cld;
449 cld->cld_sptlrpc = sptlrpc_cld;
450 mutex_unlock(&cld->cld_lock);
456 #ifdef HAVE_SERVER_SUPPORT
458 config_log_put(barrier_cld);
460 config_log_put(nodemap_cld);
463 config_log_put(params_cld);
465 config_log_put(sptlrpc_cld);
470 DEFINE_MUTEX(llog_process_lock);
472 static inline void config_mark_cld_stop_nolock(struct config_llog_data *cld)
476 spin_lock(&config_list_lock);
477 cld->cld_stopping = 1;
478 spin_unlock(&config_list_lock);
480 CDEBUG(D_INFO, "lockh %#llx\n", cld->cld_lockh.cookie);
481 if (!ldlm_lock_addref_try(&cld->cld_lockh, LCK_CR))
482 ldlm_lock_decref_and_cancel(&cld->cld_lockh, LCK_CR);
485 static inline void config_mark_cld_stop(struct config_llog_data *cld)
488 mutex_lock(&cld->cld_lock);
489 config_mark_cld_stop_nolock(cld);
490 mutex_unlock(&cld->cld_lock);
494 /** Stop watching for updates on this log.
496 static int config_log_end(char *logname, struct config_llog_instance *cfg)
498 struct config_llog_data *cld;
499 struct config_llog_data *cld_sptlrpc = NULL;
500 struct config_llog_data *cld_params = NULL;
501 struct config_llog_data *cld_recover = NULL;
502 struct config_llog_data *cld_nodemap = NULL;
503 struct config_llog_data *cld_barrier = NULL;
508 cld = config_log_find(logname, cfg);
512 mutex_lock(&cld->cld_lock);
514 * if cld_stopping is set, it means we didn't start the log thus
515 * not owning the start ref. this can happen after previous umount:
516 * the cld still hanging there waiting for lock cancel, and we
517 * remount again but failed in the middle and call log_end without
520 if (unlikely(cld->cld_stopping)) {
521 mutex_unlock(&cld->cld_lock);
522 /* drop the ref from the find */
527 cld_recover = cld->cld_recover;
528 cld->cld_recover = NULL;
529 cld_params = cld->cld_params;
530 cld->cld_params = NULL;
531 cld_nodemap = cld->cld_nodemap;
532 cld->cld_nodemap = NULL;
533 cld_barrier = cld->cld_barrier;
534 cld->cld_barrier = NULL;
535 cld_sptlrpc = cld->cld_sptlrpc;
536 cld->cld_sptlrpc = NULL;
538 config_mark_cld_stop_nolock(cld);
539 mutex_unlock(&cld->cld_lock);
541 config_mark_cld_stop(cld_recover);
542 config_log_put(cld_recover);
543 config_mark_cld_stop(cld_params);
544 config_log_put(cld_params);
545 config_mark_cld_stop(cld_barrier);
546 config_log_put(cld_barrier);
547 /* don't explicitly set cld_stopping on sptlrpc lock here, as other
548 * targets may be active, it will be done in config_log_put if necessary
550 config_log_put(cld_sptlrpc);
551 /* don't set cld_stopping on nm lock as other targets may be active */
552 config_log_put(cld_nodemap);
554 /* drop the ref from the find */
556 /* drop the start ref */
559 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
564 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
566 struct obd_device *obd = data;
567 struct obd_import *imp;
568 struct obd_connect_data *ocd;
569 struct config_llog_data *cld;
574 with_imp_locked(obd, imp, rc) {
575 ocd = &imp->imp_connect_data;
577 seq_printf(m, "imperative_recovery: %s\n",
578 OCD_HAS_FLAG(ocd, IMP_RECOV) ?
579 "ENABLED" : "DISABLED");
584 seq_puts(m, "client_state:\n");
586 spin_lock(&config_list_lock);
587 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
588 if (cld->cld_recover == NULL)
590 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
592 cld->cld_recover->cld_cfg.cfg_last_idx);
594 spin_unlock(&config_list_lock);
599 /* reenqueue any lost locks */
600 #define RQ_RUNNING 0x1
604 #define RQ_PRECLEANUP 0x10
606 static wait_queue_head_t rq_waitq;
607 static DECLARE_COMPLETION(rq_exit);
608 static DECLARE_COMPLETION(rq_start);
610 static void do_requeue(struct config_llog_data *cld)
617 * Do not run mgc_process_log on a disconnected export or an
618 * export which is being disconnected. Take the client
619 * semaphore to make the check non-racy.
621 down_read_nested(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem,
623 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
624 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
625 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
626 if (rc && rc != -ENOENT)
627 CERROR("failed processing log: %d\n", rc);
629 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
632 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
637 static int mgc_requeue_thread(void *data)
644 CDEBUG(D_MGC, "Starting requeue thread\n");
646 /* Keep trying failed locks periodically */
647 spin_lock(&config_list_lock);
648 rq_state |= RQ_RUNNING;
649 while (!(rq_state & RQ_STOP)) {
650 struct config_llog_data *cld, *cld_prev;
653 /* Any new or requeued lostlocks will change the state */
654 rq_state &= ~(RQ_NOW | RQ_LATER);
655 spin_unlock(&config_list_lock);
662 /* Always wait a few seconds to allow the server who
663 * caused the lock revocation to finish its setup, plus some
664 * random so everyone doesn't try to reconnect at once.
666 to = mgc_requeue_timeout_min == 0 ? 1 : mgc_requeue_timeout_min;
667 to = cfs_time_seconds(mgc_requeue_timeout_min) +
668 get_random_u32_below(cfs_time_seconds(to));
669 wait_event_idle_timeout(rq_waitq,
670 rq_state & (RQ_STOP | RQ_PRECLEANUP),
674 * iterate & processing through the list. for each cld, process
675 * its depending sptlrpc cld firstly (if any) and then itself.
677 * it's guaranteed any item in the list must have
678 * reference > 0; and if cld_lostlock is set, at
679 * least one reference is taken by the previous enqueue.
683 spin_lock(&config_list_lock);
684 rq_state &= ~RQ_PRECLEANUP;
685 list_for_each_entry(cld, &config_llog_list,
687 if (!cld->cld_lostlock || cld->cld_stopping)
690 /* hold reference to avoid being freed during
691 * subsequent processing.
694 cld->cld_lostlock = 0;
695 spin_unlock(&config_list_lock);
697 config_log_put(cld_prev);
700 if (likely(!(rq_state & RQ_STOP))) {
702 spin_lock(&config_list_lock);
704 spin_lock(&config_list_lock);
708 spin_unlock(&config_list_lock);
709 config_log_put(cld_prev);
711 /* Wait a bit to see if anyone else needs a requeue */
712 wait_event_idle(rq_waitq, rq_state & (RQ_NOW | RQ_STOP));
713 spin_lock(&config_list_lock);
716 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
717 rq_state &= ~RQ_RUNNING;
718 spin_unlock(&config_list_lock);
722 CDEBUG(D_MGC, "Ending requeue thread\n");
726 /* Add a cld to the list to requeue. Start the requeue thread if needed.
727 * We are responsible for dropping the config log reference from here on out.
729 static void mgc_requeue_add(struct config_llog_data *cld)
735 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
736 cld->cld_logname, refcount_read(&cld->cld_refcount),
737 cld->cld_stopping, rq_state);
739 /* lets cancel an existent lock to mark cld as "lostlock" */
740 CDEBUG(D_INFO, "lockh %#llx\n", cld->cld_lockh.cookie);
741 if (!ldlm_lock_addref_try(&cld->cld_lockh, LCK_CR))
742 ldlm_lock_decref_and_cancel(&cld->cld_lockh, LCK_CR);
744 mutex_lock(&cld->cld_lock);
745 spin_lock(&config_list_lock);
746 if (!(rq_state & RQ_STOP) && !cld->cld_stopping) {
747 cld->cld_lostlock = 1;
751 spin_unlock(&config_list_lock);
752 mutex_unlock(&cld->cld_lock);
759 /********************** class fns **********************/
760 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
762 struct llog_ctxt *ctxt;
767 /* setup only remote ctxt, the local disk context is switched per each
768 * filesystem during mgc_fs_setup()
770 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
775 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
778 llog_initiator_connect(ctxt);
784 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
786 struct llog_ctxt *ctxt;
790 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
792 llog_cleanup(env, ctxt);
798 static atomic_t mgc_count = ATOMIC_INIT(0);
799 static int mgc_precleanup(struct obd_device *obd)
806 if (atomic_dec_and_test(&mgc_count)) {
807 LASSERT(rq_state & RQ_RUNNING);
808 /* stop requeue thread */
811 /* wakeup requeue thread to clean our cld */
812 temp = RQ_NOW | RQ_PRECLEANUP;
815 spin_lock(&config_list_lock);
817 spin_unlock(&config_list_lock);
821 wait_for_completion(&rq_exit);
822 obd_cleanup_client_import(obd);
824 rc = mgc_llog_fini(NULL, obd);
826 CERROR("failed to cleanup llogging subsystems\n");
831 static int mgc_cleanup(struct obd_device *obd)
837 /* COMPAT_146 - old config logs may have added profiles secretly */
838 if (atomic_read(&obd->obd_type->typ_refcnt) <= 1)
839 /* Only for the last mgc */
840 class_del_profiles();
842 lprocfs_obd_cleanup(obd);
845 rc = client_obd_cleanup(obd);
849 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
851 struct task_struct *task;
856 rc = ptlrpcd_addref();
860 rc = client_obd_setup(obd, lcfg);
862 GOTO(err_decref, rc);
864 rc = mgc_llog_init(NULL, obd);
866 CERROR("failed to setup llogging subsystems\n");
867 GOTO(err_cleanup, rc);
870 rc = mgc_tunables_init(obd);
874 if (atomic_inc_return(&mgc_count) == 1) {
876 init_waitqueue_head(&rq_waitq);
878 /* start requeue thread */
879 task = kthread_run(mgc_requeue_thread, NULL, "ll_cfg_requeue");
882 CERROR("%s: cannot start requeue thread: rc = %d; "
883 "no more log updates\n",
887 /* rc is the task_struct pointer of mgc_requeue_thread. */
889 wait_for_completion(&rq_start);
895 lprocfs_obd_cleanup(obd);
897 client_obd_cleanup(obd);
903 /* based on ll_mdc_blocking_ast */
904 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
905 void *data, int flag)
907 struct lustre_handle lockh;
908 struct config_llog_data *cld = (struct config_llog_data *)data;
914 case LDLM_CB_BLOCKING:
915 /* mgs wants the lock, give it up... */
916 LDLM_DEBUG(lock, "MGC blocking CB");
917 ldlm_lock2handle(lock, &lockh);
918 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
920 case LDLM_CB_CANCELING:
921 /* We've given up the lock, prepare ourselves to update. */
922 LDLM_DEBUG(lock, "MGC cancel CB");
924 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
925 PLDLMRES(lock->l_resource),
926 (char *)&lock->l_resource->lr_name.name[0]);
929 CDEBUG(D_INFO, "missing data, won't requeue\n");
933 lock->l_ast_data = NULL;
934 cld->cld_lockh.cookie = 0;
935 /* Are we done with this log? */
936 if (cld->cld_stopping) {
937 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
942 /* Make sure not to re-enqueue when the mgc is stopping
943 * (we get called from client_disconnect_export)
945 if (lock->l_conn_export == NULL ||
946 lock->l_conn_export->exp_obd->u.cli.cl_conn_count == 0) {
947 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
954 mgc_requeue_add(cld);
964 /* Take a config lock so we can get cancel notifications */
965 static int mgc_enqueue(struct obd_export *exp, enum ldlm_type type,
966 union ldlm_policy_data *policy, enum ldlm_mode mode,
967 __u64 *flags, ldlm_glimpse_callback glimpse_callback,
968 void *data, __u32 lvb_len, void *lvb_swabber,
969 struct lustre_handle *lockh)
971 struct config_llog_data *cld = (struct config_llog_data *)data;
972 struct ldlm_enqueue_info einfo = {
975 .ei_cb_bl = mgc_blocking_ast,
976 .ei_cb_cp = ldlm_completion_ast,
977 .ei_cb_gl = glimpse_callback,
979 struct ptlrpc_request *req;
980 int short_limit = cld_is_sptlrpc(cld);
988 CDEBUG(D_MGC, "Enqueue for %s (res %#llx)\n", cld->cld_logname,
989 cld->cld_resid.name[0]);
991 /* We need a callback for every lockholder, so don't try to
992 * ldlm_lock_match (see rev 1.1.2.11.2.47)
994 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
995 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1000 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1001 ptlrpc_request_set_replen(req);
1003 /* check if this is server or client */
1004 if (cld->cld_cfg.cfg_sb &&
1005 IS_SERVER(s2lsi(cld->cld_cfg.cfg_sb)))
1008 /* Limit how long we will wait for the enqueue to complete */
1009 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT(exp->exp_obd);
1010 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1011 NULL, 0, LVB_T_NONE, lockh, 0);
1012 /* A failed enqueue should still call the mgc_blocking_ast,
1013 * where it will be requeued if needed ("grant failed").
1015 ptlrpc_req_finished(req);
1019 static int mgc_cancel(struct obd_export *exp, enum ldlm_mode mode,
1020 struct lustre_handle *lockh)
1024 ldlm_lock_decref(lockh, mode);
1029 static void mgc_notify_active(struct obd_device *unused)
1031 /* wakeup mgc_requeue_thread to requeue mgc lock */
1032 spin_lock(&config_list_lock);
1034 spin_unlock(&config_list_lock);
1037 /* TODO: Help the MGS rebuild nidtbl. -jay */
1040 static int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1041 u32 keylen, void *key,
1042 u32 vallen, void *val,
1043 struct ptlrpc_request_set *set)
1049 /* Turn off initial_recov after we try all backup servers once */
1050 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1051 struct obd_import *imp = class_exp2cliimp(exp);
1054 if (vallen != sizeof(int))
1056 value = *(int *)val;
1057 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1058 imp->imp_obd->obd_name, value,
1059 imp->imp_deactive, imp->imp_invalid,
1060 imp->imp_replayable, imp->imp_obd->obd_replayable,
1061 ptlrpc_import_state_name(imp->imp_state));
1062 /* Resurrect the import immediately if
1063 * 1. we previously got disconnected,
1064 * 2. value > 1 (at the same node with MGS)
1067 struct adaptive_timeout *at;
1069 at = &imp->imp_at.iat_net_latency;
1070 at_reset(at, INITIAL_CONNECT_TIMEOUT);
1073 if (imp->imp_state == LUSTRE_IMP_DISCON || value > 1)
1074 ptlrpc_reconnect_import(imp);
1079 if (KEY_IS(KEY_MGSSEC)) {
1080 struct client_obd *cli = &exp->exp_obd->u.cli;
1081 struct sptlrpc_flavor flvr;
1084 * empty string means using current flavor, if which haven't
1085 * been set yet, set it as null.
1087 * if flavor has been set previously, check the asking flavor
1088 * must match the existing one.
1091 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1097 rc = sptlrpc_parse_flavor(val, &flvr);
1099 CERROR("invalid sptlrpc flavor %s to MGS\n",
1104 /* caller already hold a mutex */
1105 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1106 cli->cl_flvr_mgc = flvr;
1107 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1108 sizeof(flvr)) != 0) {
1111 sptlrpc_flavor2name(&cli->cl_flvr_mgc, str,
1113 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but currently %s is in use\n",
1120 #ifdef HAVE_SERVER_SUPPORT
1121 rc = mgc_set_info_async_server(env, exp, keylen, key, vallen, val, set);
1126 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1127 __u32 keylen, void *key, __u32 *vallen, void *val)
1131 if (KEY_IS(KEY_CONN_DATA)) {
1132 struct obd_import *imp = class_exp2cliimp(exp);
1133 struct obd_connect_data *data = val;
1135 if (*vallen == sizeof(*data)) {
1136 *data = imp->imp_connect_data;
1144 static int mgc_import_event(struct obd_device *obd,
1145 struct obd_import *imp,
1146 enum obd_import_event event)
1150 LASSERT(imp->imp_obd == obd);
1151 CDEBUG(D_MGC, "import event %#x\n", event);
1154 case IMP_EVENT_DISCON:
1155 /* MGC imports should not wait for recovery */
1156 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1157 ptlrpc_pinger_ir_down();
1159 case IMP_EVENT_INACTIVE:
1161 case IMP_EVENT_INVALIDATE: {
1162 struct ldlm_namespace *ns = obd->obd_namespace;
1164 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1167 case IMP_EVENT_ACTIVE:
1168 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1169 /* Clearing obd_no_recov allows us to continue pinging */
1170 obd->obd_no_recov = 0;
1171 mgc_notify_active(obd);
1172 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1173 ptlrpc_pinger_ir_up();
1177 case IMP_EVENT_DEACTIVATE:
1178 case IMP_EVENT_ACTIVATE:
1181 CERROR("Unknown import event %#x\n", event);
1187 static int mgc_apply_recover_logs(struct obd_device *mgc,
1188 struct config_llog_data *cld,
1190 void *data, int datalen, bool mne_swab)
1192 struct config_llog_instance *cfg = &cld->cld_cfg;
1193 struct lustre_cfg *lcfg;
1194 struct lustre_cfg_bufs bufs;
1195 u64 prev_version = 0;
1196 char inst[MTI_NAME_MAXLEN + 1];
1202 unsigned long dynamic_nids;
1205 LASSERT(cfg->cfg_instance != 0);
1206 LASSERT(ll_get_cfg_instance(cfg->cfg_sb) == cfg->cfg_instance);
1208 /* get dynamic nids setting */
1209 dynamic_nids = mgc->obd_dynamic_nids;
1211 if (!IS_SERVER(s2lsi(cfg->cfg_sb))) {
1212 pos = snprintf(inst, sizeof(inst), "%016lx", cfg->cfg_instance);
1213 if (pos >= PAGE_SIZE)
1215 #ifdef HAVE_SERVER_SUPPORT
1217 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1219 LASSERT(IS_MDT(lsi));
1220 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1224 #endif /* HAVE_SERVER_SUPPORT */
1227 OBD_ALLOC(buf, PAGE_SIZE);
1233 while (datalen > 0) {
1234 struct mgs_nidtbl_entry *entry = (data + off);
1235 struct lnet_nid *nidlist = NULL;
1236 int entry_len = sizeof(*entry);
1237 struct obd_device *obd;
1238 struct obd_import *imp;
1239 struct obd_uuid *uuid;
1247 if (datalen < entry_len) /* really short on data */
1250 /* swab non nid data */
1252 lustre_swab_mgs_nidtbl_entry_header(entry);
1254 if (entry->mne_nid_count == 0) /* at least one nid entry */
1257 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1258 if (datalen < entry_len) /* must have entry_len at least */
1261 if (entry->mne_length > PAGE_SIZE) {
1262 CERROR("MNE too large (%u)\n", entry->mne_length);
1266 /* improper mne_lenth */
1267 if (entry->mne_length < entry_len)
1270 /* entry length reports larger than all the data passed in */
1271 if (datalen < entry->mne_length)
1274 /* Looks OK. Can process this entry? else move to next entry */
1275 off += entry->mne_length;
1276 datalen -= entry->mne_length;
1280 if (entry->mne_version > max_version) {
1281 CERROR("entry index(%lld) is over max_index(%lld)\n",
1282 entry->mne_version, max_version);
1286 if (prev_version >= entry->mne_version) {
1287 CERROR("index unsorted, prev %lld, now %lld\n",
1288 prev_version, entry->mne_version);
1291 prev_version = entry->mne_version;
1293 if (entry->mne_nid_type == 0) {
1294 struct lnet_nid *nid;
1297 OBD_ALLOC_PTR_ARRAY(nidlist, entry->mne_nid_count);
1303 /* Keep this nid data swab for normal mixed
1304 * endian handling. LU-1644
1307 lustre_swab_mgs_nidtbl_entry_content(entry);
1309 /* Turn old NID format to newer format. */
1311 for (i = 0; i < entry->mne_nid_count; i++) {
1312 lnet_nid4_to_nid(entry->u.nids[i], nid);
1313 nid += sizeof(struct lnet_nid);
1316 /* Handle the case if struct lnet_nid is expanded in
1317 * the future. The MGS should prevent this but just
1320 if (entry->mne_nid_size > sizeof(struct lnet_nid))
1323 nidlist = entry->u.nidlist;
1327 * Write a string with format "nid::instance" to
1328 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1330 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1331 memset(buf, 0, bufsz);
1335 /* lustre-OST0001-osc-<instance #> */
1336 strcpy(obdname, cld->cld_logname);
1337 cname = strrchr(obdname, '-');
1338 if (cname == NULL) {
1339 CERROR("mgc %s: invalid logname %s\n",
1340 mgc->obd_name, obdname);
1341 if (entry->mne_nid_type == 0)
1342 OBD_FREE_PTR_ARRAY(nidlist,
1343 entry->mne_nid_count);
1347 pos = cname - obdname;
1349 pos += sprintf(obdname + pos, "-%s%04x",
1350 is_ost ? "OST" : "MDT", entry->mne_index);
1352 cname = is_ost ? "osc" : "mdc",
1353 pos += snprintf(obdname + pos, bufsz, "-%s-%s", cname,
1355 lustre_cfg_bufs_reset(&bufs, obdname);
1357 /* find the obd by obdname */
1358 obd = class_name2obd(obdname);
1360 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1361 mgc->obd_name, obdname);
1363 /* this is a safe race, when the ost is starting up...*/
1367 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1370 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1371 uuid = (struct obd_uuid *)(buf + pos);
1373 with_imp_locked(obd, imp, rc) {
1374 struct obd_uuid server_uuid;
1378 /* iterate all nids to find one */
1379 /* find uuid by nid */
1380 /* create import entries if they don't exist */
1381 rc = client_import_add_nids_to_conn(imp, nidlist,
1382 entry->mne_nid_count,
1383 entry->mne_nid_size,
1385 if (rc != -ENOENT || !dynamic_nids)
1388 /* create a new connection for this import */
1389 primary_nid = libcfs_nidstr(&nidlist[0]);
1390 prim_nid_len = strlen(primary_nid) + 1;
1391 if (prim_nid_len > UUID_MAX)
1394 strncpy(server_uuid.uuid, primary_nid,
1397 CDEBUG(D_INFO, "Adding a connection for %s\n",
1400 rc = client_import_dyn_add_conn(imp, &server_uuid,
1403 CERROR("%s: Failed to add new connection with NID '%s' to import: rc = %d\n",
1404 obd->obd_name, primary_nid, rc);
1408 rc = client_import_add_nids_to_conn(imp, nidlist,
1409 entry->mne_nid_count,
1410 entry->mne_nid_size,
1413 CERROR("%s: failed to lookup UUID: rc = %d\n",
1418 if (rc == -ENODEV) {
1419 /* client does not connect to the OST yet */
1424 if (rc < 0 && rc != -ENOSPC) {
1425 CERROR("mgc: cannot find UUID by nid '%s': rc = %d\n",
1426 libcfs_nidstr(&nidlist[0]), rc);
1428 /* For old NID format case the nidlist was allocated. */
1429 if (entry->mne_nid_type == 0)
1430 OBD_FREE_PTR_ARRAY(nidlist,
1431 entry->mne_nid_count);
1435 CDEBUG(D_INFO, "Found UUID '%s' by NID '%s'\n",
1436 uuid->uuid, libcfs_nidstr(&nidlist[0]));
1438 pos += strlen(uuid->uuid);
1439 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1440 LASSERT(pos < bufsz);
1442 lustre_cfg_bufs_set_string(&bufs, 1, params);
1444 OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount,
1450 lustre_cfg_init(lcfg, LCFG_PARAM, &bufs);
1452 CDEBUG(D_INFO, "ir apply logs %lld/%lld for %s -> %s\n",
1453 prev_version, max_version, obdname, params);
1455 rc = class_process_config(lcfg);
1456 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
1457 lcfg->lcfg_buflens));
1459 CDEBUG(D_INFO, "process config for %s error %d\n",
1462 /* continue, even one with error */
1464 /* For old NID format case the nidlist was allocated. */
1465 if (entry->mne_nid_type == 0)
1466 OBD_FREE_PTR_ARRAY(nidlist, entry->mne_nid_count);
1469 OBD_FREE(buf, PAGE_SIZE);
1475 * This function is called if this client was notified for target restarting
1476 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery or
1479 static int mgc_process_recover_log(struct obd_device *obd,
1480 struct config_llog_data *cld)
1482 struct ptlrpc_request *req = NULL;
1483 struct config_llog_instance *cfg = &cld->cld_cfg;
1484 struct mgs_config_body *body;
1485 struct mgs_config_res *res;
1486 struct ptlrpc_bulk_desc *desc;
1487 struct page **pages = NULL;
1490 bool mne_swab = false;
1497 /* allocate buffer for bulk transfer.
1498 * if this is the first time for this mgs to read logs,
1499 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1500 * once; otherwise, it only reads increment of logs, this should be
1501 * small and CONFIG_READ_NRPAGES will be used.
1503 nrpages = CONFIG_READ_NRPAGES;
1504 if (cfg->cfg_last_idx == 0)
1505 nrpages = CONFIG_READ_NRPAGES_INIT;
1507 OBD_ALLOC_PTR_ARRAY_LARGE(pages, nrpages);
1509 GOTO(out, rc = -ENOMEM);
1511 for (i = 0; i < nrpages; i++) {
1512 pages[i] = alloc_page(GFP_KERNEL);
1513 if (pages[i] == NULL)
1514 GOTO(out, rc = -ENOMEM);
1518 LASSERT(cld_is_recover(cld));
1519 LASSERT(mutex_is_locked(&cld->cld_lock));
1520 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1521 &RQF_MGS_CONFIG_READ);
1523 GOTO(out, rc = -ENOMEM);
1525 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1530 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1531 LASSERT(body != NULL);
1532 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1533 rc = strscpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name));
1536 body->mcb_offset = cfg->cfg_last_idx + 1;
1537 body->mcb_type = cld->cld_type;
1538 body->mcb_bits = PAGE_SHIFT;
1539 body->mcb_units = nrpages;
1540 body->mcb_rec_nid_size = sizeof(struct lnet_nid);
1542 /* allocate bulk transfer descriptor */
1543 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1,
1544 PTLRPC_BULK_PUT_SINK,
1546 &ptlrpc_bulk_kiov_pin_ops);
1548 GOTO(out, rc = -ENOMEM);
1550 for (i = 0; i < nrpages; i++)
1551 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1554 ptlrpc_request_set_replen(req);
1555 rc = ptlrpc_queue_wait(req);
1559 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1561 GOTO(out, rc = -EPROTO);
1563 if (res->mcr_size < res->mcr_offset)
1564 GOTO(out, rc = -EINVAL);
1566 /* always update the index even though it might have errors with
1567 * handling the recover logs
1569 cfg->cfg_last_idx = res->mcr_offset;
1570 eof = res->mcr_offset == res->mcr_size;
1572 CDEBUG(D_INFO, "Latest version %lld, more %d.\n",
1573 res->mcr_offset, eof == false);
1575 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1577 GOTO(out, rc = ealen);
1579 if (ealen > nrpages << PAGE_SHIFT)
1580 GOTO(out, rc = -EINVAL);
1582 if (ealen == 0) { /* no logs transferred */
1588 mne_swab = req_capsule_rep_need_swab(&req->rq_pill);
1590 for (i = 0; i < nrpages && ealen > 0; i++) {
1594 ptr = kmap(pages[i]);
1595 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1596 min_t(int, ealen, PAGE_SIZE),
1600 CWARN("%s: error processing %s log recovery: rc = %d\n",
1604 GOTO(out, rc = rc2);
1612 ptlrpc_req_finished(req);
1616 if (rc == 0 && !eof)
1620 for (i = 0; i < nrpages; i++) {
1621 if (pages[i] == NULL)
1623 __free_page(pages[i]);
1625 OBD_FREE_PTR_ARRAY_LARGE(pages, nrpages);
1630 /* local_only means it cannot get remote llogs */
1631 static int mgc_process_cfg_log(struct obd_device *mgc,
1632 struct config_llog_data *cld, int local_only)
1634 struct llog_ctxt *ctxt;
1635 struct lustre_sb_info *lsi = NULL;
1641 LASSERT(mutex_is_locked(&cld->cld_lock));
1643 if (cld->cld_cfg.cfg_sb)
1644 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1646 /* sptlrpc llog must not keep ref to sb,
1647 * it was just needed to get lsi
1649 if (cld_is_sptlrpc(cld))
1650 cld->cld_cfg.cfg_sb = NULL;
1656 rc = lu_env_init(env, LCT_MG_THREAD);
1660 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1662 #ifdef HAVE_SERVER_SUPPORT
1663 rc = mgc_process_server_cfg_log(env, &ctxt, lsi, mgc, cld,
1665 if (rc == -EIO && local_only)
1667 if (rc && rc != -ENOENT)
1670 GOTO(out_pop, rc = -EIO);
1672 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1676 * update settings on existing OBDs.
1677 * the logname must be <fsname>-sptlrpc
1679 if (rc == 0 && cld_is_sptlrpc(cld))
1680 class_notify_sptlrpc_conf(cld->cld_logname,
1681 strlen(cld->cld_logname) -
1682 strlen("-sptlrpc"));
1686 __llog_ctxt_put(env, ctxt);
1693 static bool mgc_import_in_recovery(struct obd_import *imp)
1695 bool in_recovery = true;
1697 spin_lock(&imp->imp_lock);
1698 if (imp->imp_state == LUSTRE_IMP_FULL ||
1699 imp->imp_state == LUSTRE_IMP_CLOSED)
1700 in_recovery = false;
1701 spin_unlock(&imp->imp_lock);
1707 * Get a configuration log from the MGS and process it.
1709 * This function is called for both clients and servers to process the
1710 * configuration log from the MGS. The MGC enqueues a DLM lock on the
1711 * log from the MGS, and if the lock gets revoked the MGC will be notified
1712 * by the lock cancellation callback that the config log has changed,
1713 * and will enqueue another MGS lock on it, and then continue processing
1714 * the new additions to the end of the log.
1716 * Since the MGC import is not replayable, if the import is being evicted
1717 * (rcl == -ESHUTDOWN, \see ptlrpc_import_delay_req()), retry to process
1718 * the log until recovery is finished or the import is closed.
1720 * Make a local copy of the log before parsing it if appropriate (non-MGS
1721 * server) so that the server can start even when the MGS is down.
1723 * There shouldn't be multiple processes running process_log at once --
1724 * sounds like badness. It actually might be fine, as long as they're not
1725 * trying to update from the same log simultaneously, in which case we
1726 * should use a per-log semaphore instead of cld_lock.
1728 * \param[in] mgc MGC device by which to fetch the configuration log
1729 * \param[in] cld log processing state (stored in lock callback data)
1731 * \retval 0 on success
1732 * \retval negative errno on failure
1734 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1736 struct lustre_handle lockh = { 0 };
1737 __u64 flags = LDLM_FL_NO_LRU;
1743 LASSERT(cld != NULL);
1745 /* I don't want multiple processes running process_log at once --
1746 * sounds like badness. It actually might be fine, as long as
1747 * we're not trying to update from the same log
1748 * simultaneously (in which case we should use a per-log sem.)
1751 mutex_lock(&cld->cld_lock);
1752 if (cld->cld_stopping) {
1753 mutex_unlock(&cld->cld_lock);
1757 CFS_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1759 CDEBUG(D_MGC, "Process log %s-%016lx from %d\n", cld->cld_logname,
1760 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1762 /* Get the cfg lock on the llog */
1763 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, LDLM_PLAIN, NULL,
1765 cld_is_barrier(cld) ? mgc_barrier_glimpse_ast : NULL,
1766 cld, 0, NULL, &lockh);
1768 /* Get the cld, it will be released in mgc_blocking_ast. */
1769 config_log_get(cld);
1770 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1771 LASSERT(!lustre_handle_is_used(&cld->cld_lockh));
1773 cld->cld_lockh = lockh;
1775 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1776 cld->cld_lockh.cookie = 0;
1778 if (rcl == -ESHUTDOWN &&
1779 atomic_read(&mgc->u.cli.cl_mgc_refcount) > 0 && !retry) {
1780 struct obd_import *imp;
1781 long timeout = cfs_time_seconds(obd_timeout);
1783 mutex_unlock(&cld->cld_lock);
1784 imp = class_exp2cliimp(mgc->u.cli.cl_mgc_mgsexp);
1786 /* Let's force the pinger, and wait the import to be
1787 * connected, note: since mgc import is non-replayable,
1788 * and even the import state is disconnected, it does
1789 * not mean the "recovery" is stopped, so we will keep
1790 * waitting until timeout or the import state is
1793 ptlrpc_pinger_force(imp);
1795 wait_event_idle_timeout(imp->imp_recovery_waitq,
1796 !mgc_import_in_recovery(imp),
1799 if (imp->imp_state == LUSTRE_IMP_FULL) {
1803 mutex_lock(&cld->cld_lock);
1804 /* unlock/lock mutex, so check stopping again */
1805 if (cld->cld_stopping) {
1806 mutex_unlock(&cld->cld_lock);
1809 spin_lock(&config_list_lock);
1810 cld->cld_lostlock = 1;
1811 spin_unlock(&config_list_lock);
1814 /* mark cld_lostlock so that it will requeue
1815 * after MGC becomes available.
1817 spin_lock(&config_list_lock);
1818 cld->cld_lostlock = 1;
1819 spin_unlock(&config_list_lock);
1823 if (cld_is_recover(cld) && !rcl)
1824 rc = mgc_process_recover_log(mgc, cld);
1825 #ifdef HAVE_SERVER_SUPPORT
1826 else if (cld_is_nodemap(cld)) {
1830 rc = mgc_process_nodemap_log(mgc, cld);
1833 else if (!cld_is_barrier(cld))
1834 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1836 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1837 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1838 if (rc != -ETIMEDOUT && rc != -EIO && rc != -EAGAIN) {
1839 cld->cld_processed = 1;
1843 /* Now drop the lock so MGS can revoke it */
1845 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, LCK_CR, &lockh);
1847 CERROR("Can't drop cfg lock: %d\n", rcl);
1849 mutex_unlock(&cld->cld_lock);
1851 /* requeue nodemap lock immediately if transfer was interrupted */
1852 if ((cld_is_nodemap(cld) && rc == -EAGAIN) ||
1853 (cld_is_recover(cld) && rc)) {
1854 if (cld_is_recover(cld))
1855 CWARN("%s: IR log %s failed, not fatal: rc = %d\n",
1856 mgc->obd_name, cld->cld_logname, rc);
1857 mgc_requeue_add(cld);
1865 /* Called from lustre_process_log.
1866 * LCFG_LOG_START gets the config log from the MGS, processes it to start
1867 * any services, and adds it to the list logs to watch (follow).
1869 static int mgc_process_config(struct obd_device *obd, size_t len, void *buf)
1871 struct lustre_cfg *lcfg = buf;
1872 struct config_llog_instance *cfg = NULL;
1877 #ifdef HAVE_SERVER_SUPPORT
1878 rc = mgc_process_config_server(obd, len, buf);
1883 switch (lcfg->lcfg_command) {
1884 case LCFG_SPTLRPC_CONF: {
1885 rc = sptlrpc_process_config(lcfg);
1888 case LCFG_LOG_START: {
1889 struct config_llog_data *cld;
1890 struct super_block *sb;
1892 logname = lustre_cfg_string(lcfg, 1);
1893 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1894 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1896 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1899 /* We're only called through here on the initial mount */
1900 cld = config_log_add(obd, logname, cfg, sb);
1906 /* if it exists, the sptlrpc config log really needs to be
1907 * correctly processed before processing other logs,
1908 * otherwise client might use incorrect sec flavor
1910 if (cld->cld_sptlrpc && !cld->cld_sptlrpc->cld_processed) {
1911 unsigned int timeout = 120;
1913 /* we do not want to wait forever,
1914 * we prefer a (excessively) long timeout
1916 timeout = max(20 * mgc_requeue_timeout_min, timeout);
1917 wait_event_idle_timeout(rq_waitq,
1918 cld->cld_sptlrpc->cld_processed,
1919 cfs_time_seconds(timeout));
1922 rc = mgc_process_log(obd, cld);
1923 if (rc == 0 && cld->cld_recover != NULL) {
1924 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
1925 imp_connect_data, IMP_RECOV)) {
1926 rc = mgc_process_log(obd, cld->cld_recover);
1928 struct config_llog_data *cir;
1930 mutex_lock(&cld->cld_lock);
1931 cir = cld->cld_recover;
1932 cld->cld_recover = NULL;
1933 mutex_unlock(&cld->cld_lock);
1934 config_log_put(cir);
1938 CERROR("Cannot process recover llog %d\n", rc);
1941 if (rc == 0 && cld->cld_params != NULL) {
1942 rc = mgc_process_log(obd, cld->cld_params);
1943 if (rc == -ENOENT) {
1945 "There is no params config file yet\n");
1948 /* params log is optional */
1950 CERROR("%s: can't process params llog: rc = %d\n",
1956 case LCFG_LOG_END: {
1957 logname = lustre_cfg_string(lcfg, 1);
1959 if (lcfg->lcfg_bufcount >= 2)
1960 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1961 rc = config_log_end(logname, cfg);
1965 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1973 static const struct obd_ops mgc_obd_ops = {
1974 .o_owner = THIS_MODULE,
1975 .o_setup = mgc_setup,
1976 .o_precleanup = mgc_precleanup,
1977 .o_cleanup = mgc_cleanup,
1978 .o_add_conn = client_import_add_conn,
1979 .o_del_conn = client_import_del_conn,
1980 .o_connect = client_connect_import,
1981 .o_disconnect = client_disconnect_export,
1982 .o_set_info_async = mgc_set_info_async,
1983 .o_get_info = mgc_get_info,
1984 .o_import_event = mgc_import_event,
1985 .o_process_config = mgc_process_config,
1988 static int mgc_param_requeue_timeout_min_set(const char *val,
1989 cfs_kernel_param_arg_t *kp)
1994 rc = kstrtouint(val, 0, &num);
2000 mgc_requeue_timeout_min = num;
2005 static const struct kernel_param_ops param_ops_requeue_timeout_min = {
2006 .set = mgc_param_requeue_timeout_min_set,
2007 .get = param_get_uint,
2010 #define param_check_requeue_timeout_min(name, p) \
2011 __param_check(name, p, unsigned int)
2013 unsigned int mgc_requeue_timeout_min = MGC_TIMEOUT_MIN_SECONDS;
2014 #ifdef HAVE_KERNEL_PARAM_OPS
2015 module_param(mgc_requeue_timeout_min, requeue_timeout_min, 0644);
2017 module_param_call(mgc_requeue_timeout_min, mgc_param_requeue_timeout_min_set,
2018 param_get_uint, ¶m_ops_requeue_timeout_min, 0644);
2020 MODULE_PARM_DESC(mgc_requeue_timeout_min, "Minimal requeue time to refresh logs");
2022 static int __init mgc_init(void)
2026 rc = libcfs_setup();
2030 return class_register_type(&mgc_obd_ops, NULL, false,
2031 LUSTRE_MGC_NAME, NULL);
2034 static void __exit mgc_exit(void)
2036 class_unregister_type(LUSTRE_MGC_NAME);
2039 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2040 MODULE_DESCRIPTION("Lustre Management Client");
2041 MODULE_VERSION(LUSTRE_VERSION_STRING);
2042 MODULE_LICENSE("GPL");
2044 module_init(mgc_init);
2045 module_exit(mgc_exit);