4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mgc/mgc_request.c
34 * Author: Nathan Rutman <nathan@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_MGC
38 #define D_MGC D_CONFIG /*|D_WARNING*/
40 #include <linux/module.h>
41 #include <linux/kthread.h>
43 #include <dt_object.h>
44 #include <lprocfs_status.h>
45 #include <lustre_dlm.h>
46 #include <lustre_disk.h>
47 #include <lustre_log.h>
48 #include <lustre_nodemap.h>
49 #include <lustre_swab.h>
50 #include <obd_class.h>
51 #include <lustre_barrier.h>
53 #include "mgc_internal.h"
55 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id,
60 if (len > sizeof(resname)) {
61 CERROR("name too long: %s\n", name);
65 CERROR("missing name: %s\n", name);
68 memcpy(&resname, name, len);
70 /* Always use the same endianness for the resid */
71 memset(res_id, 0, sizeof(*res_id));
72 res_id->name[0] = cpu_to_le64(resname);
73 /* XXX: unfortunately, sptlprc and config llog share one lock */
76 case CONFIG_T_SPTLRPC:
79 case CONFIG_T_RECOVER:
81 case CONFIG_T_NODEMAP:
82 case CONFIG_T_BARRIER:
88 res_id->name[1] = cpu_to_le64(resname);
89 CDEBUG(D_MGC, "log %s to resid %#llx/%#llx (%.8s)\n", name,
90 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
94 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id, int type)
96 /* fsname is at most 8 chars long, maybe contain "-".
97 * e.g. "lustre", "SUN-000" */
98 return mgc_name2resid(fsname, strlen(fsname), res_id, type);
100 EXPORT_SYMBOL(mgc_fsname2resid);
102 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id, int type)
107 /* logname consists of "fsname-nodetype".
108 * e.g. "lustre-MDT0001", "SUN-000-client"
109 * there is an exception: llog "params" */
110 name_end = strrchr(logname, '-');
112 len = strlen(logname);
114 len = name_end - logname;
115 return mgc_name2resid(logname, len, res_id, type);
117 EXPORT_SYMBOL(mgc_logname2resid);
119 /********************** config llog list **********************/
120 static struct list_head config_llog_list = LIST_HEAD_INIT(config_llog_list);
121 static DEFINE_SPINLOCK(config_list_lock); /* protects config_llog_list */
123 /* Take a reference to a config log */
124 static int config_log_get(struct config_llog_data *cld)
127 atomic_inc(&cld->cld_refcount);
128 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
129 atomic_read(&cld->cld_refcount));
133 /* Drop a reference to a config log. When no longer referenced,
134 we can free the config log data */
135 static void config_log_put(struct config_llog_data *cld)
142 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
143 atomic_read(&cld->cld_refcount));
144 LASSERT(atomic_read(&cld->cld_refcount) > 0);
146 /* spinlock to make sure no item with 0 refcount in the list */
147 if (atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
148 list_del(&cld->cld_list_chain);
149 spin_unlock(&config_list_lock);
151 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
153 config_log_put(cld->cld_barrier);
154 config_log_put(cld->cld_recover);
155 config_log_put(cld->cld_params);
156 config_log_put(cld->cld_nodemap);
157 config_log_put(cld->cld_sptlrpc);
158 if (cld_is_sptlrpc(cld))
159 sptlrpc_conf_log_stop(cld->cld_logname);
161 class_export_put(cld->cld_mgcexp);
162 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
168 /* Find a config log by name */
170 struct config_llog_data *config_log_find(char *logname,
171 struct config_llog_instance *cfg)
173 struct config_llog_data *cld;
174 struct config_llog_data *found = NULL;
178 LASSERT(logname != NULL);
180 instance = cfg ? cfg->cfg_instance : NULL;
181 spin_lock(&config_list_lock);
182 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
183 /* check if instance equals */
184 if (instance != cld->cld_cfg.cfg_instance)
187 /* instance may be NULL, should check name */
188 if (strcmp(logname, cld->cld_logname) == 0) {
190 config_log_get(found);
194 spin_unlock(&config_list_lock);
199 struct config_llog_data *do_config_log_add(struct obd_device *obd,
202 struct config_llog_instance *cfg,
203 struct super_block *sb)
205 struct config_llog_data *cld;
210 CDEBUG(D_MGC, "do adding config log %s:%p\n", logname,
211 cfg ? cfg->cfg_instance : NULL);
213 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
215 RETURN(ERR_PTR(-ENOMEM));
217 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
219 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
223 strcpy(cld->cld_logname, logname);
227 cld->cld_cfg.cfg_callback = class_config_llog_handler;
228 mutex_init(&cld->cld_lock);
229 cld->cld_cfg.cfg_last_idx = 0;
230 cld->cld_cfg.cfg_flags = 0;
231 cld->cld_cfg.cfg_sb = sb;
232 cld->cld_type = type;
233 atomic_set(&cld->cld_refcount, 1);
235 /* Keep the mgc around until we are done */
236 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
238 if (cld_is_sptlrpc(cld))
239 sptlrpc_conf_log_start(logname);
241 spin_lock(&config_list_lock);
242 list_add(&cld->cld_list_chain, &config_llog_list);
243 spin_unlock(&config_list_lock);
245 if (cld_is_sptlrpc(cld) || cld_is_nodemap(cld) || cld_is_barrier(cld)) {
246 rc = mgc_process_log(obd, cld);
247 if (rc && rc != -ENOENT)
248 CERROR("%s: failed processing log, type %d: rc = %d\n",
249 obd->obd_name, type, rc);
255 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
257 struct config_llog_instance *cfg,
258 struct super_block *sb)
260 struct config_llog_instance lcfg = *cfg;
261 struct lustre_sb_info *lsi = s2lsi(sb);
262 struct config_llog_data *cld;
268 /* for osp-on-ost, see lustre_start_osp() */
269 if (IS_MDT(lsi) && lcfg.cfg_instance)
272 /* we have to use different llog for clients and mdts for cmd
273 * where only clients are notified if one of cmd server restarts */
274 LASSERT(strlen(fsname) < sizeof(logname) / 2);
275 strcpy(logname, fsname);
276 if (IS_SERVER(lsi)) { /* mdt */
277 LASSERT(lcfg.cfg_instance == NULL);
278 lcfg.cfg_instance = sb;
279 strcat(logname, "-mdtir");
281 LASSERT(lcfg.cfg_instance != NULL);
282 strcat(logname, "-cliir");
285 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
289 static struct config_llog_data *config_log_find_or_add(struct obd_device *obd,
290 char *logname, struct super_block *sb, int type,
291 struct config_llog_instance *cfg)
293 struct config_llog_instance lcfg = *cfg;
294 struct config_llog_data *cld;
296 lcfg.cfg_instance = sb != NULL ? (void *)sb : (void *)obd;
298 cld = config_log_find(logname, &lcfg);
299 if (unlikely(cld != NULL))
302 return do_config_log_add(obd, logname, type, &lcfg, sb);
305 /** Add this log to the list of active logs watched by an MGC.
306 * Active means we're watching for updates.
307 * We have one active log per "mount" - client instance or servername.
308 * Each instance may be at a different point in the log.
310 static struct config_llog_data *
311 config_log_add(struct obd_device *obd, char *logname,
312 struct config_llog_instance *cfg, struct super_block *sb)
314 struct lustre_sb_info *lsi = s2lsi(sb);
315 struct config_llog_data *cld = NULL;
316 struct config_llog_data *sptlrpc_cld = NULL;
317 struct config_llog_data *params_cld = NULL;
318 struct config_llog_data *nodemap_cld = NULL;
319 struct config_llog_data *barrier_cld = NULL;
326 CDEBUG(D_MGC, "adding config log %s:%p\n", logname, cfg->cfg_instance);
329 * for each regular log, the depended sptlrpc log name is
330 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
332 ptr = strrchr(logname, '-');
333 if (ptr == NULL || ptr - logname > 8) {
334 CERROR("logname %s is too long\n", logname);
335 RETURN(ERR_PTR(-EINVAL));
338 memcpy(seclogname, logname, ptr - logname);
339 strcpy(seclogname + (ptr - logname), "-sptlrpc");
341 if (cfg->cfg_sub_clds & CONFIG_SUB_SPTLRPC) {
342 sptlrpc_cld = config_log_find_or_add(obd, seclogname, NULL,
343 CONFIG_T_SPTLRPC, cfg);
344 if (IS_ERR(sptlrpc_cld)) {
345 CERROR("%s: can't create sptlrpc log %s: rc = %ld\n",
346 obd->obd_name, seclogname, PTR_ERR(sptlrpc_cld));
351 if (!IS_MGS(lsi) && cfg->cfg_sub_clds & CONFIG_SUB_NODEMAP) {
352 nodemap_cld = config_log_find_or_add(obd, LUSTRE_NODEMAP_NAME,
353 NULL, CONFIG_T_NODEMAP,
355 if (IS_ERR(nodemap_cld)) {
356 rc = PTR_ERR(nodemap_cld);
357 CERROR("%s: cannot create nodemap log: rc = %d\n",
359 GOTO(out_sptlrpc, rc);
363 if (cfg->cfg_sub_clds & CONFIG_SUB_PARAMS) {
364 params_cld = config_log_find_or_add(obd, PARAMS_FILENAME, sb,
365 CONFIG_T_PARAMS, cfg);
366 if (IS_ERR(params_cld)) {
367 rc = PTR_ERR(params_cld);
368 CERROR("%s: can't create params log: rc = %d\n",
370 GOTO(out_nodemap, rc);
374 if (IS_MDT(s2lsi(sb)) && cfg->cfg_sub_clds & CONFIG_SUB_BARRIER) {
375 snprintf(seclogname + (ptr - logname), sizeof(seclogname) - 1,
376 "-%s", BARRIER_FILENAME);
377 barrier_cld = config_log_find_or_add(obd, seclogname, sb,
378 CONFIG_T_BARRIER, cfg);
379 if (IS_ERR(barrier_cld)) {
380 rc = PTR_ERR(barrier_cld);
381 CERROR("%s: can't create barrier log: rc = %d\n",
383 GOTO(out_params, rc);
387 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
390 CERROR("%s: can't create log: rc = %d\n",
392 GOTO(out_barrier, rc = PTR_ERR(cld));
395 LASSERT(lsi->lsi_lmd);
396 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR) &&
397 cfg->cfg_sub_clds & CONFIG_SUB_RECOVER) {
398 struct config_llog_data *recover_cld;
400 ptr = strrchr(seclogname, '-');
404 CERROR("%s: sptlrpc log name not correct, %s: "
405 "rc = %d\n", obd->obd_name, seclogname, -EINVAL);
406 GOTO(out_cld, rc = -EINVAL);
409 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
410 if (IS_ERR(recover_cld)) {
411 rc = PTR_ERR(recover_cld);
412 CERROR("%s: can't create recover log: rc = %d\n",
417 mutex_lock(&cld->cld_lock);
419 cld->cld_recover = recover_cld;
423 mutex_lock(&cld->cld_lock);
424 cld->cld_params = params_cld;
425 cld->cld_barrier = barrier_cld;
426 cld->cld_nodemap = nodemap_cld;
427 cld->cld_sptlrpc = sptlrpc_cld;
428 mutex_unlock(&cld->cld_lock);
435 config_log_put(barrier_cld);
437 config_log_put(params_cld);
439 config_log_put(nodemap_cld);
441 config_log_put(sptlrpc_cld);
446 DEFINE_MUTEX(llog_process_lock);
448 static inline void config_mark_cld_stop(struct config_llog_data *cld)
451 mutex_lock(&cld->cld_lock);
452 spin_lock(&config_list_lock);
453 cld->cld_stopping = 1;
454 spin_unlock(&config_list_lock);
455 mutex_unlock(&cld->cld_lock);
459 /** Stop watching for updates on this log.
461 static int config_log_end(char *logname, struct config_llog_instance *cfg)
463 struct config_llog_data *cld;
464 struct config_llog_data *cld_sptlrpc = NULL;
465 struct config_llog_data *cld_params = NULL;
466 struct config_llog_data *cld_recover = NULL;
467 struct config_llog_data *cld_nodemap = NULL;
468 struct config_llog_data *cld_barrier = NULL;
473 cld = config_log_find(logname, cfg);
477 mutex_lock(&cld->cld_lock);
479 * if cld_stopping is set, it means we didn't start the log thus
480 * not owning the start ref. this can happen after previous umount:
481 * the cld still hanging there waiting for lock cancel, and we
482 * remount again but failed in the middle and call log_end without
485 if (unlikely(cld->cld_stopping)) {
486 mutex_unlock(&cld->cld_lock);
487 /* drop the ref from the find */
492 spin_lock(&config_list_lock);
493 cld->cld_stopping = 1;
494 spin_unlock(&config_list_lock);
496 cld_recover = cld->cld_recover;
497 cld->cld_recover = NULL;
498 cld_params = cld->cld_params;
499 cld->cld_params = NULL;
500 cld_nodemap = cld->cld_nodemap;
501 cld->cld_nodemap = NULL;
502 cld_barrier = cld->cld_barrier;
503 cld->cld_barrier = NULL;
504 cld_sptlrpc = cld->cld_sptlrpc;
505 cld->cld_sptlrpc = NULL;
506 mutex_unlock(&cld->cld_lock);
508 config_mark_cld_stop(cld_recover);
509 config_log_put(cld_recover);
511 config_mark_cld_stop(cld_params);
512 config_log_put(cld_params);
514 /* don't set cld_stopping on nm lock as other targets may be active */
515 config_log_put(cld_nodemap);
518 mutex_lock(&cld_barrier->cld_lock);
519 cld_barrier->cld_stopping = 1;
520 mutex_unlock(&cld_barrier->cld_lock);
521 config_log_put(cld_barrier);
524 config_log_put(cld_sptlrpc);
526 /* drop the ref from the find */
528 /* drop the start ref */
531 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
536 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
538 struct obd_device *obd = data;
539 struct obd_import *imp;
540 struct obd_connect_data *ocd;
541 struct config_llog_data *cld;
545 LPROCFS_CLIMP_CHECK(obd);
546 imp = obd->u.cli.cl_import;
547 ocd = &imp->imp_connect_data;
549 seq_printf(m, "imperative_recovery: %s\n",
550 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
551 seq_printf(m, "client_state:\n");
553 spin_lock(&config_list_lock);
554 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
555 if (cld->cld_recover == NULL)
557 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
559 cld->cld_recover->cld_cfg.cfg_last_idx);
561 spin_unlock(&config_list_lock);
563 LPROCFS_CLIMP_EXIT(obd);
567 /* reenqueue any lost locks */
568 #define RQ_RUNNING 0x1
572 #define RQ_PRECLEANUP 0x10
573 static int rq_state = 0;
574 static wait_queue_head_t rq_waitq;
575 static DECLARE_COMPLETION(rq_exit);
576 static DECLARE_COMPLETION(rq_start);
578 static void do_requeue(struct config_llog_data *cld)
583 LASSERT(atomic_read(&cld->cld_refcount) > 0);
586 * Do not run mgc_process_log on a disconnected export or an
587 * export which is being disconnected. Take the client
588 * semaphore to make the check non-racy.
590 down_read_nested(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem,
592 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
593 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
594 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
595 if (rc && rc != -ENOENT)
596 CERROR("failed processing log: %d\n", rc);
598 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
601 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
606 /* this timeout represents how many seconds MGC should wait before
607 * requeue config and recover lock to the MGS. We need to randomize this
608 * in order to not flood the MGS.
610 #define MGC_TIMEOUT_MIN_SECONDS 5
611 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
613 static int mgc_requeue_thread(void *data)
619 CDEBUG(D_MGC, "Starting requeue thread\n");
621 /* Keep trying failed locks periodically */
622 spin_lock(&config_list_lock);
623 rq_state |= RQ_RUNNING;
624 while (!(rq_state & RQ_STOP)) {
625 struct l_wait_info lwi;
626 struct config_llog_data *cld, *cld_prev;
627 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
630 /* Any new or requeued lostlocks will change the state */
631 rq_state &= ~(RQ_NOW | RQ_LATER);
632 spin_unlock(&config_list_lock);
639 /* Always wait a few seconds to allow the server who
640 caused the lock revocation to finish its setup, plus some
641 random so everyone doesn't try to reconnect at once. */
642 to = msecs_to_jiffies(MGC_TIMEOUT_MIN_SECONDS * MSEC_PER_SEC);
643 /* rand is centi-seconds */
644 to += msecs_to_jiffies(rand * MSEC_PER_SEC / 100);
645 lwi = LWI_TIMEOUT(to, NULL, NULL);
646 l_wait_event(rq_waitq, rq_state & (RQ_STOP | RQ_PRECLEANUP),
650 * iterate & processing through the list. for each cld, process
651 * its depending sptlrpc cld firstly (if any) and then itself.
653 * it's guaranteed any item in the list must have
654 * reference > 0; and if cld_lostlock is set, at
655 * least one reference is taken by the previous enqueue.
659 spin_lock(&config_list_lock);
660 rq_state &= ~RQ_PRECLEANUP;
661 list_for_each_entry(cld, &config_llog_list,
663 if (!cld->cld_lostlock || cld->cld_stopping)
666 /* hold reference to avoid being freed during
667 * subsequent processing. */
669 cld->cld_lostlock = 0;
670 spin_unlock(&config_list_lock);
672 config_log_put(cld_prev);
675 if (likely(!(rq_state & RQ_STOP))) {
677 spin_lock(&config_list_lock);
679 spin_lock(&config_list_lock);
683 spin_unlock(&config_list_lock);
684 config_log_put(cld_prev);
686 /* Wait a bit to see if anyone else needs a requeue */
687 lwi = (struct l_wait_info) { 0 };
688 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
690 spin_lock(&config_list_lock);
693 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
694 rq_state &= ~RQ_RUNNING;
695 spin_unlock(&config_list_lock);
699 CDEBUG(D_MGC, "Ending requeue thread\n");
703 /* Add a cld to the list to requeue. Start the requeue thread if needed.
704 We are responsible for dropping the config log reference from here on out. */
705 static void mgc_requeue_add(struct config_llog_data *cld)
710 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
711 cld->cld_logname, atomic_read(&cld->cld_refcount),
712 cld->cld_stopping, rq_state);
713 LASSERT(atomic_read(&cld->cld_refcount) > 0);
715 mutex_lock(&cld->cld_lock);
716 spin_lock(&config_list_lock);
717 if (!(rq_state & RQ_STOP) && !cld->cld_stopping && !cld->cld_lostlock) {
718 cld->cld_lostlock = 1;
722 spin_unlock(&config_list_lock);
723 mutex_unlock(&cld->cld_lock);
730 /********************** class fns **********************/
731 static int mgc_local_llog_init(const struct lu_env *env,
732 struct obd_device *obd,
733 struct obd_device *disk)
735 struct llog_ctxt *ctxt;
740 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
745 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
747 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
753 static int mgc_local_llog_fini(const struct lu_env *env,
754 struct obd_device *obd)
756 struct llog_ctxt *ctxt;
760 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
761 llog_cleanup(env, ctxt);
766 static int mgc_fs_setup(const struct lu_env *env, struct obd_device *obd,
767 struct super_block *sb)
769 struct lustre_sb_info *lsi = s2lsi(sb);
770 struct client_obd *cli = &obd->u.cli;
771 struct lu_fid rfid, fid;
772 struct dt_object *root, *dto;
778 LASSERT(lsi->lsi_dt_dev);
780 /* The mgc fs exclusion mutex. Only one fs can be setup at a time. */
781 mutex_lock(&cli->cl_mgc_mutex);
783 /* Setup the configs dir */
784 fid.f_seq = FID_SEQ_LOCAL_NAME;
787 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
792 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
796 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
797 &cli->cl_mgc_los->los_dev->dd_lu_dev, NULL);
798 if (unlikely(IS_ERR(root)))
799 GOTO(out_los, rc = PTR_ERR(root));
801 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
803 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
804 dt_object_put_nocache(env, root);
806 GOTO(out_los, rc = PTR_ERR(dto));
808 cli->cl_mgc_configs_dir = dto;
810 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
811 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
815 /* We take an obd ref to insure that we can't get to mgc_cleanup
816 * without calling mgc_fs_cleanup first. */
817 class_incref(obd, "mgc_fs", obd);
819 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
823 dt_object_put(env, cli->cl_mgc_configs_dir);
824 cli->cl_mgc_configs_dir = NULL;
828 local_oid_storage_fini(env, cli->cl_mgc_los);
829 cli->cl_mgc_los = NULL;
830 mutex_unlock(&cli->cl_mgc_mutex);
835 static int mgc_fs_cleanup(const struct lu_env *env, struct obd_device *obd)
837 struct client_obd *cli = &obd->u.cli;
840 LASSERT(cli->cl_mgc_los != NULL);
842 mgc_local_llog_fini(env, obd);
844 dt_object_put_nocache(env, cli->cl_mgc_configs_dir);
845 cli->cl_mgc_configs_dir = NULL;
847 local_oid_storage_fini(env, cli->cl_mgc_los);
848 cli->cl_mgc_los = NULL;
850 class_decref(obd, "mgc_fs", obd);
851 mutex_unlock(&cli->cl_mgc_mutex);
856 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
858 struct llog_ctxt *ctxt;
863 /* setup only remote ctxt, the local disk context is switched per each
864 * filesystem during mgc_fs_setup() */
865 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
870 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
873 llog_initiator_connect(ctxt);
879 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
881 struct llog_ctxt *ctxt;
885 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
887 llog_cleanup(env, ctxt);
893 static atomic_t mgc_count = ATOMIC_INIT(0);
894 static int mgc_precleanup(struct obd_device *obd)
900 if (atomic_dec_and_test(&mgc_count)) {
901 LASSERT(rq_state & RQ_RUNNING);
902 /* stop requeue thread */
905 /* wakeup requeue thread to clean our cld */
906 temp = RQ_NOW | RQ_PRECLEANUP;
909 spin_lock(&config_list_lock);
911 spin_unlock(&config_list_lock);
915 wait_for_completion(&rq_exit);
916 obd_cleanup_client_import(obd);
918 rc = mgc_llog_fini(NULL, obd);
920 CERROR("failed to cleanup llogging subsystems\n");
925 static int mgc_cleanup(struct obd_device *obd)
930 /* COMPAT_146 - old config logs may have added profiles we don't
932 if (obd->obd_type->typ_refcnt <= 1)
933 /* Only for the last mgc */
934 class_del_profiles();
936 lprocfs_obd_cleanup(obd);
939 rc = client_obd_cleanup(obd);
943 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
945 struct task_struct *task;
949 rc = ptlrpcd_addref();
953 rc = client_obd_setup(obd, lcfg);
955 GOTO(err_decref, rc);
957 rc = mgc_llog_init(NULL, obd);
959 CERROR("failed to setup llogging subsystems\n");
960 GOTO(err_cleanup, rc);
963 rc = mgc_tunables_init(obd);
967 if (atomic_inc_return(&mgc_count) == 1) {
969 init_waitqueue_head(&rq_waitq);
971 /* start requeue thread */
972 task = kthread_run(mgc_requeue_thread, NULL, "ll_cfg_requeue");
975 CERROR("%s: cannot start requeue thread: rc = %d; "
976 "no more log updates\n",
980 /* rc is the task_struct pointer of mgc_requeue_thread. */
982 wait_for_completion(&rq_start);
988 lprocfs_obd_cleanup(obd);
990 client_obd_cleanup(obd);
996 /* based on ll_mdc_blocking_ast */
997 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
998 void *data, int flag)
1000 struct lustre_handle lockh;
1001 struct config_llog_data *cld = (struct config_llog_data *)data;
1006 case LDLM_CB_BLOCKING:
1007 /* mgs wants the lock, give it up... */
1008 LDLM_DEBUG(lock, "MGC blocking CB");
1009 ldlm_lock2handle(lock, &lockh);
1010 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
1012 case LDLM_CB_CANCELING:
1013 /* We've given up the lock, prepare ourselves to update. */
1014 LDLM_DEBUG(lock, "MGC cancel CB");
1016 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
1017 PLDLMRES(lock->l_resource),
1018 (char *)&lock->l_resource->lr_name.name[0]);
1021 CDEBUG(D_INFO, "missing data, won't requeue\n");
1025 /* held at mgc_process_log(). */
1026 LASSERT(atomic_read(&cld->cld_refcount) > 0);
1028 lock->l_ast_data = NULL;
1029 /* Are we done with this log? */
1030 if (cld->cld_stopping) {
1031 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
1033 config_log_put(cld);
1036 /* Make sure not to re-enqueue when the mgc is stopping
1037 (we get called from client_disconnect_export) */
1038 if (lock->l_conn_export == NULL ||
1039 lock->l_conn_export->exp_obd->u.cli.cl_conn_count == 0) {
1040 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
1042 config_log_put(cld);
1046 /* Re-enqueue now */
1047 mgc_requeue_add(cld);
1048 config_log_put(cld);
1057 /* Not sure where this should go... */
1058 /* This is the timeout value for MGS_CONNECT request plus a ping interval, such
1059 * that we can have a chance to try the secondary MGS if any. */
1060 #define MGC_ENQUEUE_LIMIT (INITIAL_CONNECT_TIMEOUT + (AT_OFF ? 0 : at_min) \
1062 #define MGC_TARGET_REG_LIMIT 10
1063 #define MGC_TARGET_REG_LIMIT_MAX RECONNECT_DELAY_MAX
1064 #define MGC_SEND_PARAM_LIMIT 10
1066 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
1067 /* Send parameter to MGS*/
1068 static int mgc_set_mgs_param(struct obd_export *exp,
1069 struct mgs_send_param *msp)
1071 struct ptlrpc_request *req;
1072 struct mgs_send_param *req_msp, *rep_msp;
1076 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1077 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
1082 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1084 ptlrpc_req_finished(req);
1088 memcpy(req_msp, msp, sizeof(*req_msp));
1089 ptlrpc_request_set_replen(req);
1091 /* Limit how long we will wait for the enqueue to complete */
1092 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
1093 rc = ptlrpc_queue_wait(req);
1095 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1096 memcpy(msp, rep_msp, sizeof(*rep_msp));
1099 ptlrpc_req_finished(req);
1105 /* Take a config lock so we can get cancel notifications */
1106 static int mgc_enqueue(struct obd_export *exp, enum ldlm_type type,
1107 union ldlm_policy_data *policy, enum ldlm_mode mode,
1108 __u64 *flags, ldlm_glimpse_callback glimpse_callback,
1109 void *data, __u32 lvb_len, void *lvb_swabber,
1110 struct lustre_handle *lockh)
1112 struct config_llog_data *cld = (struct config_llog_data *)data;
1113 struct ldlm_enqueue_info einfo = {
1116 .ei_cb_bl = mgc_blocking_ast,
1117 .ei_cb_cp = ldlm_completion_ast,
1118 .ei_cb_gl = glimpse_callback,
1120 struct ptlrpc_request *req;
1121 int short_limit = cld_is_sptlrpc(cld);
1125 CDEBUG(D_MGC, "Enqueue for %s (res %#llx)\n", cld->cld_logname,
1126 cld->cld_resid.name[0]);
1128 /* We need a callback for every lockholder, so don't try to
1129 ldlm_lock_match (see rev 1.1.2.11.2.47) */
1130 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1131 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1136 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1137 ptlrpc_request_set_replen(req);
1139 /* check if this is server or client */
1140 if (cld->cld_cfg.cfg_sb) {
1141 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
1142 if (lsi && IS_SERVER(lsi))
1145 /* Limit how long we will wait for the enqueue to complete */
1146 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
1147 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1148 NULL, 0, LVB_T_NONE, lockh, 0);
1149 /* A failed enqueue should still call the mgc_blocking_ast,
1150 where it will be requeued if needed ("grant failed"). */
1151 ptlrpc_req_finished(req);
1155 static int mgc_cancel(struct obd_export *exp, enum ldlm_mode mode,
1156 struct lustre_handle *lockh)
1160 ldlm_lock_decref(lockh, mode);
1165 static void mgc_notify_active(struct obd_device *unused)
1167 /* wakeup mgc_requeue_thread to requeue mgc lock */
1168 spin_lock(&config_list_lock);
1170 spin_unlock(&config_list_lock);
1173 /* TODO: Help the MGS rebuild nidtbl. -jay */
1176 /* Send target_reg message to MGS */
1177 static int mgc_target_register(struct obd_export *exp,
1178 struct mgs_target_info *mti)
1180 struct ptlrpc_request *req;
1181 struct mgs_target_info *req_mti, *rep_mti;
1185 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1186 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1191 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1193 ptlrpc_req_finished(req);
1197 memcpy(req_mti, mti, sizeof(*req_mti));
1198 ptlrpc_request_set_replen(req);
1199 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1200 /* Limit how long we will wait for the enqueue to complete */
1201 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1203 /* if the target needs to regenerate the config log in MGS, it's better
1204 * to use some longer limit to let MGC have time to change connection to
1205 * another MGS (or try again with the same MGS) for the target (server)
1206 * will fail and exit if the request expired due to delay limit. */
1207 if (mti->mti_flags & (LDD_F_UPDATE | LDD_F_NEED_INDEX))
1208 req->rq_delay_limit = MGC_TARGET_REG_LIMIT_MAX;
1210 rc = ptlrpc_queue_wait(req);
1212 rep_mti = req_capsule_server_get(&req->rq_pill,
1213 &RMF_MGS_TARGET_INFO);
1214 memcpy(mti, rep_mti, sizeof(*rep_mti));
1215 CDEBUG(D_MGC, "register %s got index = %d\n",
1216 mti->mti_svname, mti->mti_stripe_index);
1218 ptlrpc_req_finished(req);
1223 static int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1224 u32 keylen, void *key,
1225 u32 vallen, void *val,
1226 struct ptlrpc_request_set *set)
1231 /* Turn off initial_recov after we try all backup servers once */
1232 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1233 struct obd_import *imp = class_exp2cliimp(exp);
1235 if (vallen != sizeof(int))
1237 value = *(int *)val;
1238 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1239 imp->imp_obd->obd_name, value,
1240 imp->imp_deactive, imp->imp_invalid,
1241 imp->imp_replayable, imp->imp_obd->obd_replayable,
1242 ptlrpc_import_state_name(imp->imp_state));
1243 /* Resurrect the import immediately if
1244 * 1. we previously got disconnected,
1245 * 2. value > 1 (at the same node with MGS)
1247 if (imp->imp_state == LUSTRE_IMP_DISCON || value > 1)
1248 ptlrpc_reconnect_import(imp);
1253 /* FIXME move this to mgc_process_config */
1254 if (KEY_IS(KEY_REGISTER_TARGET)) {
1255 struct mgs_target_info *mti;
1256 if (vallen != sizeof(struct mgs_target_info))
1258 mti = (struct mgs_target_info *)val;
1259 CDEBUG(D_MGC, "register_target %s %#x\n",
1260 mti->mti_svname, mti->mti_flags);
1261 rc = mgc_target_register(exp, mti);
1264 if (KEY_IS(KEY_SET_FS)) {
1265 struct super_block *sb = (struct super_block *)val;
1267 if (vallen != sizeof(struct super_block))
1270 rc = mgc_fs_setup(env, exp->exp_obd, sb);
1273 if (KEY_IS(KEY_CLEAR_FS)) {
1276 rc = mgc_fs_cleanup(env, exp->exp_obd);
1279 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 13, 53, 0)
1280 if (KEY_IS(KEY_SET_INFO)) {
1281 struct mgs_send_param *msp;
1283 msp = (struct mgs_send_param *)val;
1284 rc = mgc_set_mgs_param(exp, msp);
1288 if (KEY_IS(KEY_MGSSEC)) {
1289 struct client_obd *cli = &exp->exp_obd->u.cli;
1290 struct sptlrpc_flavor flvr;
1293 * empty string means using current flavor, if which haven't
1294 * been set yet, set it as null.
1296 * if flavor has been set previously, check the asking flavor
1297 * must match the existing one.
1300 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1306 rc = sptlrpc_parse_flavor(val, &flvr);
1308 CERROR("invalid sptlrpc flavor %s to MGS\n",
1314 * caller already hold a mutex
1316 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1317 cli->cl_flvr_mgc = flvr;
1318 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1319 sizeof(flvr)) != 0) {
1322 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1324 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1325 "currently %s is in use\n",
1335 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1336 __u32 keylen, void *key, __u32 *vallen, void *val)
1340 if (KEY_IS(KEY_CONN_DATA)) {
1341 struct obd_import *imp = class_exp2cliimp(exp);
1342 struct obd_connect_data *data = val;
1344 if (*vallen == sizeof(*data)) {
1345 *data = imp->imp_connect_data;
1353 static int mgc_import_event(struct obd_device *obd,
1354 struct obd_import *imp,
1355 enum obd_import_event event)
1359 LASSERT(imp->imp_obd == obd);
1360 CDEBUG(D_MGC, "import event %#x\n", event);
1363 case IMP_EVENT_DISCON:
1364 /* MGC imports should not wait for recovery */
1365 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1366 ptlrpc_pinger_ir_down();
1368 case IMP_EVENT_INACTIVE:
1370 case IMP_EVENT_INVALIDATE: {
1371 struct ldlm_namespace *ns = obd->obd_namespace;
1372 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1375 case IMP_EVENT_ACTIVE:
1376 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1377 /* Clearing obd_no_recov allows us to continue pinging */
1378 obd->obd_no_recov = 0;
1379 mgc_notify_active(obd);
1380 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1381 ptlrpc_pinger_ir_up();
1385 case IMP_EVENT_DEACTIVATE:
1386 case IMP_EVENT_ACTIVATE:
1389 CERROR("Unknown import event %#x\n", event);
1396 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_SHIFT),
1397 CONFIG_READ_NRPAGES = 4
1400 static int mgc_apply_recover_logs(struct obd_device *mgc,
1401 struct config_llog_data *cld,
1403 void *data, int datalen, bool mne_swab)
1405 struct config_llog_instance *cfg = &cld->cld_cfg;
1406 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1407 struct mgs_nidtbl_entry *entry;
1408 struct lustre_cfg *lcfg;
1409 struct lustre_cfg_bufs bufs;
1410 u64 prev_version = 0;
1419 LASSERT(cfg->cfg_instance != NULL);
1420 LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1422 OBD_ALLOC(inst, PAGE_SIZE);
1426 if (!IS_SERVER(lsi)) {
1427 pos = snprintf(inst, PAGE_SIZE, "%p", cfg->cfg_instance);
1428 if (pos >= PAGE_SIZE) {
1429 OBD_FREE(inst, PAGE_SIZE);
1433 LASSERT(IS_MDT(lsi));
1434 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1437 OBD_FREE(inst, PAGE_SIZE);
1445 bufsz = PAGE_SIZE - pos;
1447 while (datalen > 0) {
1448 int entry_len = sizeof(*entry);
1450 struct obd_device *obd;
1457 if (datalen < sizeof(*entry))
1460 entry = (typeof(entry))(data + off);
1463 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1465 if (entry->mne_nid_count == 0) /* at least one nid entry */
1467 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1470 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1471 if (datalen < entry_len) /* must have entry_len at least */
1474 /* Keep this swab for normal mixed endian handling. LU-1644 */
1476 lustre_swab_mgs_nidtbl_entry(entry);
1477 if (entry->mne_length > PAGE_SIZE) {
1478 CERROR("MNE too large (%u)\n", entry->mne_length);
1482 if (entry->mne_length < entry_len)
1485 off += entry->mne_length;
1486 datalen -= entry->mne_length;
1490 if (entry->mne_version > max_version) {
1491 CERROR("entry index(%lld) is over max_index(%lld)\n",
1492 entry->mne_version, max_version);
1496 if (prev_version >= entry->mne_version) {
1497 CERROR("index unsorted, prev %lld, now %lld\n",
1498 prev_version, entry->mne_version);
1501 prev_version = entry->mne_version;
1504 * Write a string with format "nid::instance" to
1505 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1508 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1509 memset(buf, 0, bufsz);
1513 /* lustre-OST0001-osc-<instance #> */
1514 strcpy(obdname, cld->cld_logname);
1515 cname = strrchr(obdname, '-');
1516 if (cname == NULL) {
1517 CERROR("mgc %s: invalid logname %s\n",
1518 mgc->obd_name, obdname);
1522 pos = cname - obdname;
1524 pos += sprintf(obdname + pos, "-%s%04x",
1525 is_ost ? "OST" : "MDT", entry->mne_index);
1527 cname = is_ost ? "osc" : "mdc",
1528 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1529 lustre_cfg_bufs_reset(&bufs, obdname);
1531 /* find the obd by obdname */
1532 obd = class_name2obd(obdname);
1534 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1535 mgc->obd_name, obdname);
1537 /* this is a safe race, when the ost is starting up...*/
1541 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1544 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1547 down_read(&obd->u.cli.cl_sem);
1548 if (obd->u.cli.cl_import == NULL) {
1549 /* client does not connect to the OST yet */
1550 up_read(&obd->u.cli.cl_sem);
1555 /* iterate all nids to find one */
1556 /* find uuid by nid */
1558 for (i = 0; i < entry->mne_nid_count; i++) {
1559 rc = client_import_find_conn(obd->u.cli.cl_import,
1561 (struct obd_uuid *)uuid);
1566 up_read(&obd->u.cli.cl_sem);
1568 CERROR("mgc: cannot find uuid by nid %s\n",
1569 libcfs_nid2str(entry->u.nids[0]));
1573 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1574 uuid, libcfs_nid2str(entry->u.nids[0]));
1576 pos += strlen(uuid);
1577 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1578 LASSERT(pos < bufsz);
1580 lustre_cfg_bufs_set_string(&bufs, 1, params);
1582 OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount,
1588 lustre_cfg_init(lcfg, LCFG_PARAM, &bufs);
1590 CDEBUG(D_INFO, "ir apply logs %lld/%lld for %s -> %s\n",
1591 prev_version, max_version, obdname, params);
1593 rc = class_process_config(lcfg);
1594 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
1595 lcfg->lcfg_buflens));
1597 CDEBUG(D_INFO, "process config for %s error %d\n",
1600 /* continue, even one with error */
1603 OBD_FREE(inst, PAGE_SIZE);
1608 * This function is called if this client was notified for target restarting
1609 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery or
1612 static int mgc_process_recover_nodemap_log(struct obd_device *obd,
1613 struct config_llog_data *cld)
1615 struct ptlrpc_connection *mgc_conn;
1616 struct ptlrpc_request *req = NULL;
1617 struct config_llog_instance *cfg = &cld->cld_cfg;
1618 struct mgs_config_body *body;
1619 struct mgs_config_res *res;
1620 struct nodemap_config *new_config = NULL;
1621 struct lu_nodemap *recent_nodemap = NULL;
1622 struct ptlrpc_bulk_desc *desc;
1623 struct page **pages = NULL;
1624 __u64 config_read_offset = 0;
1625 __u8 nodemap_cur_pass = 0;
1628 bool mne_swab = false;
1634 mgc_conn = class_exp2cliimp(cld->cld_mgcexp)->imp_connection;
1636 /* don't need to get local config */
1637 if (cld_is_nodemap(cld) &&
1638 (LNET_NETTYP(LNET_NIDNET(mgc_conn->c_peer.nid)) == LOLND))
1641 /* allocate buffer for bulk transfer.
1642 * if this is the first time for this mgs to read logs,
1643 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1644 * once; otherwise, it only reads increment of logs, this should be
1645 * small and CONFIG_READ_NRPAGES will be used.
1647 nrpages = CONFIG_READ_NRPAGES;
1648 if (cfg->cfg_last_idx == 0 || cld_is_nodemap(cld))
1649 nrpages = CONFIG_READ_NRPAGES_INIT;
1651 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1653 GOTO(out, rc = -ENOMEM);
1655 for (i = 0; i < nrpages; i++) {
1656 pages[i] = alloc_page(GFP_KERNEL);
1657 if (pages[i] == NULL)
1658 GOTO(out, rc = -ENOMEM);
1662 #ifdef HAVE_SERVER_SUPPORT
1663 if (cld_is_nodemap(cld) && config_read_offset == 0) {
1664 new_config = nodemap_config_alloc();
1665 if (IS_ERR(new_config)) {
1666 rc = PTR_ERR(new_config);
1672 LASSERT(cld_is_recover(cld) || cld_is_nodemap(cld));
1673 LASSERT(mutex_is_locked(&cld->cld_lock));
1674 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1675 &RQF_MGS_CONFIG_READ);
1677 GOTO(out, rc = -ENOMEM);
1679 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1684 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1685 LASSERT(body != NULL);
1686 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1687 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1688 >= sizeof(body->mcb_name))
1689 GOTO(out, rc = -E2BIG);
1690 if (cld_is_nodemap(cld))
1691 body->mcb_offset = config_read_offset;
1693 body->mcb_offset = cfg->cfg_last_idx + 1;
1694 body->mcb_type = cld->cld_type;
1695 body->mcb_bits = PAGE_SHIFT;
1696 body->mcb_units = nrpages;
1697 body->mcb_nm_cur_pass = nodemap_cur_pass;
1699 /* allocate bulk transfer descriptor */
1700 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1,
1701 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1703 &ptlrpc_bulk_kiov_pin_ops);
1705 GOTO(out, rc = -ENOMEM);
1707 for (i = 0; i < nrpages; i++)
1708 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1711 ptlrpc_request_set_replen(req);
1712 rc = ptlrpc_queue_wait(req);
1716 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1718 GOTO(out, rc = -EPROTO);
1720 if (cld_is_nodemap(cld)) {
1721 config_read_offset = res->mcr_offset;
1722 eof = config_read_offset == II_END_OFF;
1723 nodemap_cur_pass = res->mcr_nm_cur_pass;
1725 if (res->mcr_size < res->mcr_offset)
1726 GOTO(out, rc = -EINVAL);
1728 /* always update the index even though it might have errors with
1729 * handling the recover logs
1731 cfg->cfg_last_idx = res->mcr_offset;
1732 eof = res->mcr_offset == res->mcr_size;
1734 CDEBUG(D_INFO, "Latest version %lld, more %d.\n",
1735 res->mcr_offset, eof == false);
1738 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1740 GOTO(out, rc = ealen);
1742 if (ealen > nrpages << PAGE_SHIFT)
1743 GOTO(out, rc = -EINVAL);
1745 if (ealen == 0) { /* no logs transferred */
1746 #ifdef HAVE_SERVER_SUPPORT
1747 /* config changed since first read RPC */
1748 if (cld_is_nodemap(cld) && config_read_offset == 0) {
1749 CDEBUG(D_INFO, "nodemap config changed in transit, retrying\n");
1750 GOTO(out, rc = -EAGAIN);
1758 mne_swab = ptlrpc_rep_need_swab(req);
1760 /* When a nodemap config is received, we build a new nodemap config,
1761 * with new nodemap structs. We keep track of the most recently added
1762 * nodemap since the config is read ordered by nodemap_id, and so it
1763 * is likely that the next record will be related. Because access to
1764 * the nodemaps is single threaded until the nodemap_config is active,
1765 * we don't need to reference count with recent_nodemap, though
1766 * recent_nodemap should be set to NULL when the nodemap_config
1767 * is either destroyed or set active.
1769 for (i = 0; i < nrpages && ealen > 0; i++) {
1773 ptr = kmap(pages[i]);
1774 if (cld_is_nodemap(cld))
1775 rc2 = nodemap_process_idx_pages(new_config, ptr,
1778 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset,
1785 CWARN("%s: error processing %s log %s: rc = %d\n",
1787 cld_is_nodemap(cld) ? "nodemap" : "recovery",
1790 GOTO(out, rc = rc2);
1798 ptlrpc_req_finished(req);
1802 if (rc == 0 && !eof)
1805 #ifdef HAVE_SERVER_SUPPORT
1806 if (new_config != NULL) {
1807 /* recent_nodemap cannot be used after set_active/dealloc */
1809 nodemap_config_set_active_mgc(new_config);
1811 nodemap_config_dealloc(new_config);
1816 for (i = 0; i < nrpages; i++) {
1817 if (pages[i] == NULL)
1819 __free_page(pages[i]);
1821 OBD_FREE(pages, sizeof(*pages) * nrpages);
1826 static int mgc_barrier_glimpse_ast(struct ldlm_lock *lock, void *data)
1828 struct config_llog_data *cld = lock->l_ast_data;
1832 if (cld->cld_stopping)
1835 rc = barrier_handler(s2lsi(cld->cld_cfg.cfg_sb)->lsi_dt_dev,
1836 (struct ptlrpc_request *)data);
1841 /* Copy a remote log locally */
1842 static int mgc_llog_local_copy(const struct lu_env *env,
1843 struct obd_device *obd,
1844 struct llog_ctxt *rctxt,
1845 struct llog_ctxt *lctxt, char *logname)
1853 * - copy it to backup using llog_backup()
1854 * - copy remote llog to logname using llog_backup()
1855 * - if failed then move bakup to logname again
1858 OBD_ALLOC(temp_log, strlen(logname) + 2);
1861 sprintf(temp_log, "%sT", logname);
1863 /* make a copy of local llog at first */
1864 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1865 if (rc < 0 && rc != -ENOENT)
1867 /* copy remote llog to the local copy */
1868 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1869 if (rc == -ENOENT) {
1870 /* no remote llog, delete local one too */
1871 llog_erase(env, lctxt, NULL, logname);
1872 } else if (rc < 0) {
1873 /* error during backup, get local one back from the copy */
1874 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1876 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1877 obd->obd_name, logname, rc);
1879 llog_erase(env, lctxt, NULL, temp_log);
1880 OBD_FREE(temp_log, strlen(logname) + 2);
1884 /* local_only means it cannot get remote llogs */
1885 static int mgc_process_cfg_log(struct obd_device *mgc,
1886 struct config_llog_data *cld, int local_only)
1888 struct llog_ctxt *ctxt, *lctxt = NULL;
1889 struct client_obd *cli = &mgc->u.cli;
1890 struct lustre_sb_info *lsi = NULL;
1897 LASSERT(mutex_is_locked(&cld->cld_lock));
1899 if (cld->cld_cfg.cfg_sb)
1900 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1906 rc = lu_env_init(env, LCT_MG_THREAD);
1910 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1913 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1915 /* Copy the setup log locally if we can. Don't mess around if we're
1916 * running an MGS though (logs are already local). */
1917 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1918 cli->cl_mgc_configs_dir != NULL &&
1919 lu2dt_dev(cli->cl_mgc_configs_dir->do_lu.lo_dev) ==
1921 if (!local_only && !lsi->lsi_dt_dev->dd_rdonly)
1922 /* Only try to copy log if we have the lock. */
1923 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1925 if (local_only || rc) {
1926 if (strcmp(cld->cld_logname, PARAMS_FILENAME) != 0 &&
1927 llog_is_empty(env, lctxt, cld->cld_logname)) {
1928 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1929 "log %s and no local copy."
1930 "\n", cld->cld_logname);
1931 GOTO(out_pop, rc = -ENOENT);
1933 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1934 "copy for now, will try to update later.\n",
1938 /* Now, whether we copied or not, start using the local llog.
1939 * If we failed to copy, we'll start using whatever the old
1941 llog_ctxt_put(ctxt);
1945 if (local_only) /* no local log at client side */
1946 GOTO(out_pop, rc = -EIO);
1950 if (lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1951 lsi->lsi_dt_dev->dd_rdonly) {
1952 struct llog_ctxt *rctxt;
1954 /* Under readonly mode, we may have no local copy or local
1955 * copy is incomplete, so try to use remote llog firstly. */
1956 rctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1959 rc = class_config_parse_llog(env, rctxt, cld->cld_logname,
1961 llog_ctxt_put(rctxt);
1964 if (rc && rc != -ENOENT)
1965 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1969 * update settings on existing OBDs. doing it inside
1970 * of llog_process_lock so no device is attaching/detaching
1972 * the logname must be <fsname>-sptlrpc
1974 if (rc == 0 && cld_is_sptlrpc(cld))
1975 class_notify_sptlrpc_conf(cld->cld_logname,
1976 strlen(cld->cld_logname) -
1977 strlen("-sptlrpc"));
1981 __llog_ctxt_put(env, ctxt);
1983 __llog_ctxt_put(env, lctxt);
1991 static bool mgc_import_in_recovery(struct obd_import *imp)
1993 bool in_recovery = true;
1995 spin_lock(&imp->imp_lock);
1996 if (imp->imp_state == LUSTRE_IMP_FULL ||
1997 imp->imp_state == LUSTRE_IMP_CLOSED)
1998 in_recovery = false;
1999 spin_unlock(&imp->imp_lock);
2005 * Get a configuration log from the MGS and process it.
2007 * This function is called for both clients and servers to process the
2008 * configuration log from the MGS. The MGC enqueues a DLM lock on the
2009 * log from the MGS, and if the lock gets revoked the MGC will be notified
2010 * by the lock cancellation callback that the config log has changed,
2011 * and will enqueue another MGS lock on it, and then continue processing
2012 * the new additions to the end of the log.
2014 * Since the MGC import is not replayable, if the import is being evicted
2015 * (rcl == -ESHUTDOWN, \see ptlrpc_import_delay_req()), retry to process
2016 * the log until recovery is finished or the import is closed.
2018 * Make a local copy of the log before parsing it if appropriate (non-MGS
2019 * server) so that the server can start even when the MGS is down.
2021 * There shouldn't be multiple processes running process_log at once --
2022 * sounds like badness. It actually might be fine, as long as they're not
2023 * trying to update from the same log simultaneously, in which case we
2024 * should use a per-log semaphore instead of cld_lock.
2026 * \param[in] mgc MGC device by which to fetch the configuration log
2027 * \param[in] cld log processing state (stored in lock callback data)
2029 * \retval 0 on success
2030 * \retval negative errno on failure
2032 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
2034 struct lustre_handle lockh = { 0 };
2035 __u64 flags = LDLM_FL_NO_LRU;
2040 LASSERT(cld != NULL);
2042 /* I don't want multiple processes running process_log at once --
2043 sounds like badness. It actually might be fine, as long as
2044 we're not trying to update from the same log
2045 simultaneously (in which case we should use a per-log sem.) */
2047 mutex_lock(&cld->cld_lock);
2048 if (cld->cld_stopping) {
2049 mutex_unlock(&cld->cld_lock);
2053 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
2055 CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
2056 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
2058 /* Get the cfg lock on the llog */
2059 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, LDLM_PLAIN, NULL,
2061 cld_is_barrier(cld) ? mgc_barrier_glimpse_ast : NULL,
2062 cld, 0, NULL, &lockh);
2064 /* Get the cld, it will be released in mgc_blocking_ast. */
2065 config_log_get(cld);
2066 rc = ldlm_lock_set_data(&lockh, (void *)cld);
2069 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
2071 if (rcl == -ESHUTDOWN &&
2072 atomic_read(&mgc->u.cli.cl_mgc_refcount) > 0 && !retry) {
2073 struct obd_import *imp;
2074 struct l_wait_info lwi;
2075 int secs = cfs_time_seconds(obd_timeout);
2077 mutex_unlock(&cld->cld_lock);
2078 imp = class_exp2cliimp(mgc->u.cli.cl_mgc_mgsexp);
2080 /* Let's force the pinger, and wait the import to be
2081 * connected, note: since mgc import is non-replayable,
2082 * and even the import state is disconnected, it does
2083 * not mean the "recovery" is stopped, so we will keep
2084 * waitting until timeout or the import state is
2086 ptlrpc_pinger_force(imp);
2088 lwi = LWI_TIMEOUT(secs, NULL, NULL);
2089 l_wait_event(imp->imp_recovery_waitq,
2090 !mgc_import_in_recovery(imp), &lwi);
2092 if (imp->imp_state == LUSTRE_IMP_FULL) {
2096 mutex_lock(&cld->cld_lock);
2097 /* unlock/lock mutex, so check stopping again */
2098 if (cld->cld_stopping) {
2099 mutex_unlock(&cld->cld_lock);
2102 spin_lock(&config_list_lock);
2103 cld->cld_lostlock = 1;
2104 spin_unlock(&config_list_lock);
2107 /* mark cld_lostlock so that it will requeue
2108 * after MGC becomes available. */
2109 spin_lock(&config_list_lock);
2110 cld->cld_lostlock = 1;
2111 spin_unlock(&config_list_lock);
2115 if (cld_is_recover(cld) || cld_is_nodemap(cld)) {
2117 rc = mgc_process_recover_nodemap_log(mgc, cld);
2118 else if (cld_is_nodemap(cld))
2121 if (cld_is_recover(cld) && rc) {
2123 CERROR("%s: recover log %s failed, not fatal: rc = %d\n",
2124 mgc->obd_name, cld->cld_logname, rc);
2125 spin_lock(&config_list_lock);
2126 cld->cld_lostlock = 1;
2127 spin_unlock(&config_list_lock);
2129 rc = 0; /* this is not a fatal error for recover log */
2131 } else if (!cld_is_barrier(cld)) {
2132 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
2135 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
2136 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
2138 mutex_unlock(&cld->cld_lock);
2140 /* Now drop the lock so MGS can revoke it */
2142 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, LCK_CR, &lockh);
2144 CERROR("Can't drop cfg lock: %d\n", rcl);
2147 /* requeue nodemap lock immediately if transfer was interrupted */
2148 if (cld_is_nodemap(cld) && rc == -EAGAIN) {
2149 mgc_requeue_add(cld);
2157 /** Called from lustre_process_log.
2158 * LCFG_LOG_START gets the config log from the MGS, processes it to start
2159 * any services, and adds it to the list logs to watch (follow).
2161 static int mgc_process_config(struct obd_device *obd, size_t len, void *buf)
2163 struct lustre_cfg *lcfg = buf;
2164 struct config_llog_instance *cfg = NULL;
2169 switch(lcfg->lcfg_command) {
2170 case LCFG_LOV_ADD_OBD: {
2171 /* Overloading this cfg command: register a new target */
2172 struct mgs_target_info *mti;
2174 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
2175 sizeof(struct mgs_target_info))
2176 GOTO(out, rc = -EINVAL);
2178 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
2179 CDEBUG(D_MGC, "add_target %s %#x\n",
2180 mti->mti_svname, mti->mti_flags);
2181 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
2184 case LCFG_LOV_DEL_OBD:
2185 /* Unregister has no meaning at the moment. */
2186 CERROR("lov_del_obd unimplemented\n");
2189 case LCFG_SPTLRPC_CONF: {
2190 rc = sptlrpc_process_config(lcfg);
2193 case LCFG_LOG_START: {
2194 struct config_llog_data *cld;
2195 struct super_block *sb;
2197 logname = lustre_cfg_string(lcfg, 1);
2198 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
2199 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
2201 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
2204 /* We're only called through here on the initial mount */
2205 cld = config_log_add(obd, logname, cfg, sb);
2211 rc = mgc_process_log(obd, cld);
2212 if (rc == 0 && cld->cld_recover != NULL) {
2213 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
2214 imp_connect_data, IMP_RECOV)) {
2215 rc = mgc_process_log(obd, cld->cld_recover);
2217 struct config_llog_data *cir;
2219 mutex_lock(&cld->cld_lock);
2220 cir = cld->cld_recover;
2221 cld->cld_recover = NULL;
2222 mutex_unlock(&cld->cld_lock);
2223 config_log_put(cir);
2227 CERROR("Cannot process recover llog %d\n", rc);
2230 if (rc == 0 && cld->cld_params != NULL) {
2231 rc = mgc_process_log(obd, cld->cld_params);
2232 if (rc == -ENOENT) {
2233 CDEBUG(D_MGC, "There is no params "
2234 "config file yet\n");
2237 /* params log is optional */
2239 CERROR("%s: can't process params llog: rc = %d\n",
2245 case LCFG_LOG_END: {
2246 logname = lustre_cfg_string(lcfg, 1);
2248 if (lcfg->lcfg_bufcount >= 2)
2249 cfg = (struct config_llog_instance *)lustre_cfg_buf(
2251 rc = config_log_end(logname, cfg);
2255 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
2256 GOTO(out, rc = -EINVAL);
2264 static struct obd_ops mgc_obd_ops = {
2265 .o_owner = THIS_MODULE,
2266 .o_setup = mgc_setup,
2267 .o_precleanup = mgc_precleanup,
2268 .o_cleanup = mgc_cleanup,
2269 .o_add_conn = client_import_add_conn,
2270 .o_del_conn = client_import_del_conn,
2271 .o_connect = client_connect_import,
2272 .o_disconnect = client_disconnect_export,
2273 .o_set_info_async = mgc_set_info_async,
2274 .o_get_info = mgc_get_info,
2275 .o_import_event = mgc_import_event,
2276 .o_process_config = mgc_process_config,
2279 static int __init mgc_init(void)
2281 return class_register_type(&mgc_obd_ops, NULL, false, NULL,
2282 LUSTRE_MGC_NAME, NULL);
2285 static void __exit mgc_exit(void)
2287 class_unregister_type(LUSTRE_MGC_NAME);
2290 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2291 MODULE_DESCRIPTION("Lustre Management Client");
2292 MODULE_VERSION(LUSTRE_VERSION_STRING);
2293 MODULE_LICENSE("GPL");
2295 module_init(mgc_init);
2296 module_exit(mgc_exit);