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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mgc/mgc_request.c
38 * Author: Nathan Rutman <nathan@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_MGC
42 #define D_MGC D_CONFIG /*|D_WARNING*/
44 #include <linux/module.h>
45 #include <linux/kthread.h>
47 #include <dt_object.h>
48 #include <lprocfs_status.h>
49 #include <lustre_dlm.h>
50 #include <lustre_disk.h>
51 #include <lustre_log.h>
52 #include <lustre_nodemap.h>
53 #include <lustre_swab.h>
54 #include <obd_class.h>
56 #include "mgc_internal.h"
58 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id,
63 if (len > sizeof(resname)) {
64 CERROR("name too long: %s\n", name);
68 CERROR("missing name: %s\n", name);
71 memcpy(&resname, name, len);
73 /* Always use the same endianness for the resid */
74 memset(res_id, 0, sizeof(*res_id));
75 res_id->name[0] = cpu_to_le64(resname);
76 /* XXX: unfortunately, sptlprc and config llog share one lock */
79 case CONFIG_T_SPTLRPC:
82 case CONFIG_T_RECOVER:
84 case CONFIG_T_NODEMAP:
90 res_id->name[1] = cpu_to_le64(resname);
91 CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.8s)\n", name,
92 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
96 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id, int type)
98 /* fsname is at most 8 chars long, maybe contain "-".
99 * e.g. "lustre", "SUN-000" */
100 return mgc_name2resid(fsname, strlen(fsname), res_id, type);
102 EXPORT_SYMBOL(mgc_fsname2resid);
104 static int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id,
110 /* logname consists of "fsname-nodetype".
111 * e.g. "lustre-MDT0001", "SUN-000-client"
112 * there is an exception: llog "params" */
113 name_end = strrchr(logname, '-');
115 len = strlen(logname);
117 len = name_end - logname;
118 return mgc_name2resid(logname, len, res_id, type);
121 /********************** config llog list **********************/
122 static struct list_head config_llog_list = LIST_HEAD_INIT(config_llog_list);
123 static DEFINE_SPINLOCK(config_list_lock);
125 /* Take a reference to a config log */
126 static int config_log_get(struct config_llog_data *cld)
129 atomic_inc(&cld->cld_refcount);
130 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
131 atomic_read(&cld->cld_refcount));
135 /* Drop a reference to a config log. When no longer referenced,
136 we can free the config log data */
137 static void config_log_put(struct config_llog_data *cld)
141 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
142 atomic_read(&cld->cld_refcount));
143 LASSERT(atomic_read(&cld->cld_refcount) > 0);
145 /* spinlock to make sure no item with 0 refcount in the list */
146 if (atomic_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);
152 if (cld->cld_recover)
153 config_log_put(cld->cld_recover);
154 if (cld->cld_sptlrpc)
155 config_log_put(cld->cld_sptlrpc);
157 config_log_put(cld->cld_params);
158 if (cld->cld_nodemap)
159 config_log_put(cld->cld_nodemap);
160 if (cld_is_sptlrpc(cld))
161 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;
180 LASSERT(logname != NULL);
182 instance = cfg ? cfg->cfg_instance : NULL;
183 spin_lock(&config_list_lock);
184 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
185 /* check if instance equals */
186 if (instance != cld->cld_cfg.cfg_instance)
189 /* instance may be NULL, should check name */
190 if (strcmp(logname, cld->cld_logname) == 0) {
196 atomic_inc(&found->cld_refcount);
197 LASSERT(found->cld_stopping == 0 || cld_is_sptlrpc(found) == 0);
199 spin_unlock(&config_list_lock);
204 struct config_llog_data *do_config_log_add(struct obd_device *obd,
207 struct config_llog_instance *cfg,
208 struct super_block *sb)
210 struct config_llog_data *cld;
215 CDEBUG(D_MGC, "do adding config log %s:%p\n", logname,
216 cfg ? cfg->cfg_instance : NULL);
218 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
220 RETURN(ERR_PTR(-ENOMEM));
222 strcpy(cld->cld_logname, logname);
226 cld->cld_cfg.cfg_callback = class_config_llog_handler;
227 mutex_init(&cld->cld_lock);
228 cld->cld_cfg.cfg_last_idx = 0;
229 cld->cld_cfg.cfg_flags = 0;
230 cld->cld_cfg.cfg_sb = sb;
231 cld->cld_type = type;
232 atomic_set(&cld->cld_refcount, 1);
234 /* Keep the mgc around until we are done */
235 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
237 if (cld_is_sptlrpc(cld)) {
238 sptlrpc_conf_log_start(logname);
239 cld->cld_cfg.cfg_obdname = obd->obd_name;
242 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
244 spin_lock(&config_list_lock);
245 list_add(&cld->cld_list_chain, &config_llog_list);
246 spin_unlock(&config_list_lock);
253 if (cld_is_sptlrpc(cld) || cld_is_nodemap(cld)) {
254 rc = mgc_process_log(obd, cld);
255 if (rc && rc != -ENOENT)
256 CERROR("%s: failed processing log, type %d: rc = %d\n",
257 obd->obd_name, type, rc);
263 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
265 struct config_llog_instance *cfg,
266 struct super_block *sb)
268 struct config_llog_instance lcfg = *cfg;
269 struct lustre_sb_info *lsi = s2lsi(sb);
270 struct config_llog_data *cld;
276 /* for osp-on-ost, see lustre_start_osp() */
277 if (IS_MDT(lsi) && lcfg.cfg_instance)
280 /* we have to use different llog for clients and mdts for cmd
281 * where only clients are notified if one of cmd server restarts */
282 LASSERT(strlen(fsname) < sizeof(logname) / 2);
283 strcpy(logname, fsname);
284 if (IS_SERVER(lsi)) { /* mdt */
285 LASSERT(lcfg.cfg_instance == NULL);
286 lcfg.cfg_instance = sb;
287 strcat(logname, "-mdtir");
289 LASSERT(lcfg.cfg_instance != NULL);
290 strcat(logname, "-cliir");
293 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
297 static struct config_llog_data *config_params_log_add(struct obd_device *obd,
298 struct config_llog_instance *cfg, struct super_block *sb)
300 struct config_llog_instance lcfg = *cfg;
301 struct config_llog_data *cld;
303 lcfg.cfg_instance = sb;
305 cld = do_config_log_add(obd, PARAMS_FILENAME, CONFIG_T_PARAMS,
311 /** Add this log to the list of active logs watched by an MGC.
312 * Active means we're watching for updates.
313 * We have one active log per "mount" - client instance or servername.
314 * Each instance may be at a different point in the log.
316 static int config_log_add(struct obd_device *obd, char *logname,
317 struct config_llog_instance *cfg,
318 struct super_block *sb)
320 struct lustre_sb_info *lsi = s2lsi(sb);
321 struct config_llog_data *cld;
322 struct config_llog_data *sptlrpc_cld;
323 struct config_llog_data *params_cld;
324 struct config_llog_data *nodemap_cld;
330 CDEBUG(D_MGC, "adding config log %s:%p\n", logname, cfg->cfg_instance);
333 * for each regular log, the depended sptlrpc log name is
334 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
336 ptr = strrchr(logname, '-');
337 if (ptr == NULL || ptr - logname > 8) {
338 CERROR("logname %s is too long\n", logname);
342 memcpy(seclogname, logname, ptr - logname);
343 strcpy(seclogname + (ptr - logname), "-sptlrpc");
345 sptlrpc_cld = config_log_find(seclogname, NULL);
346 if (sptlrpc_cld == NULL) {
347 sptlrpc_cld = do_config_log_add(obd, seclogname,
348 CONFIG_T_SPTLRPC, NULL, NULL);
349 if (IS_ERR(sptlrpc_cld)) {
350 CERROR("can't create sptlrpc log: %s\n", seclogname);
351 GOTO(out, rc = PTR_ERR(sptlrpc_cld));
355 nodemap_cld = config_log_find(LUSTRE_NODEMAP_NAME, NULL);
356 if (!nodemap_cld && IS_SERVER(lsi) && !IS_MGS(lsi)) {
357 nodemap_cld = do_config_log_add(obd, LUSTRE_NODEMAP_NAME,
358 CONFIG_T_NODEMAP, NULL, NULL);
359 if (IS_ERR(nodemap_cld)) {
360 rc = PTR_ERR(nodemap_cld);
361 CERROR("%s: cannot create nodemap log: rc = %d\n",
363 GOTO(out_sptlrpc, rc);
367 params_cld = config_params_log_add(obd, cfg, sb);
368 if (IS_ERR(params_cld)) {
369 rc = PTR_ERR(params_cld);
370 CERROR("%s: can't create params log: rc = %d\n",
372 GOTO(out_nodemap, rc);
375 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
377 CERROR("can't create log: %s\n", logname);
378 GOTO(out_params, rc = PTR_ERR(cld));
381 LASSERT(lsi->lsi_lmd);
382 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)) {
383 struct config_llog_data *recover_cld;
384 ptr = strrchr(seclogname, '-');
389 CERROR("%s: sptlrpc log name not correct, %s: "
390 "rc = %d\n", obd->obd_name, seclogname, -EINVAL);
394 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
395 if (IS_ERR(recover_cld))
396 GOTO(out_cld, rc = PTR_ERR(recover_cld));
397 cld->cld_recover = recover_cld;
400 cld->cld_sptlrpc = sptlrpc_cld;
401 cld->cld_params = params_cld;
402 cld->cld_nodemap = nodemap_cld;
410 config_log_put(params_cld);
413 config_log_put(nodemap_cld);
416 config_log_put(sptlrpc_cld);
422 DEFINE_MUTEX(llog_process_lock);
424 /** Stop watching for updates on this log.
426 static int config_log_end(char *logname, struct config_llog_instance *cfg)
428 struct config_llog_data *cld;
429 struct config_llog_data *cld_sptlrpc = NULL;
430 struct config_llog_data *cld_params = NULL;
431 struct config_llog_data *cld_recover = NULL;
432 struct config_llog_data *cld_nodemap = NULL;
437 cld = config_log_find(logname, cfg);
441 mutex_lock(&cld->cld_lock);
443 * if cld_stopping is set, it means we didn't start the log thus
444 * not owning the start ref. this can happen after previous umount:
445 * the cld still hanging there waiting for lock cancel, and we
446 * remount again but failed in the middle and call log_end without
449 if (unlikely(cld->cld_stopping)) {
450 mutex_unlock(&cld->cld_lock);
451 /* drop the ref from the find */
456 cld->cld_stopping = 1;
458 cld_recover = cld->cld_recover;
459 cld->cld_recover = NULL;
460 mutex_unlock(&cld->cld_lock);
463 mutex_lock(&cld_recover->cld_lock);
464 cld_recover->cld_stopping = 1;
465 mutex_unlock(&cld_recover->cld_lock);
466 config_log_put(cld_recover);
469 spin_lock(&config_list_lock);
470 cld_sptlrpc = cld->cld_sptlrpc;
471 cld->cld_sptlrpc = NULL;
472 cld_params = cld->cld_params;
473 cld->cld_params = NULL;
474 cld_nodemap = cld->cld_nodemap;
475 cld->cld_nodemap = NULL;
476 spin_unlock(&config_list_lock);
479 config_log_put(cld_sptlrpc);
482 mutex_lock(&cld_params->cld_lock);
483 cld_params->cld_stopping = 1;
484 mutex_unlock(&cld_params->cld_lock);
485 config_log_put(cld_params);
489 mutex_lock(&cld_nodemap->cld_lock);
490 cld_nodemap->cld_stopping = 1;
491 mutex_unlock(&cld_nodemap->cld_lock);
492 config_log_put(cld_nodemap);
495 /* drop the ref from the find */
497 /* drop the start ref */
500 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
505 #ifdef CONFIG_PROC_FS
506 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
508 struct obd_device *obd = data;
509 struct obd_import *imp;
510 struct obd_connect_data *ocd;
511 struct config_llog_data *cld;
514 LASSERT(obd != NULL);
515 LPROCFS_CLIMP_CHECK(obd);
516 imp = obd->u.cli.cl_import;
517 ocd = &imp->imp_connect_data;
519 seq_printf(m, "imperative_recovery: %s\n",
520 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
521 seq_printf(m, "client_state:\n");
523 spin_lock(&config_list_lock);
524 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
525 if (cld->cld_recover == NULL)
527 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
529 cld->cld_recover->cld_cfg.cfg_last_idx);
531 spin_unlock(&config_list_lock);
533 LPROCFS_CLIMP_EXIT(obd);
538 /* reenqueue any lost locks */
539 #define RQ_RUNNING 0x1
543 #define RQ_PRECLEANUP 0x10
544 static int rq_state = 0;
545 static wait_queue_head_t rq_waitq;
546 static DECLARE_COMPLETION(rq_exit);
547 static DECLARE_COMPLETION(rq_start);
549 static void do_requeue(struct config_llog_data *cld)
554 LASSERT(atomic_read(&cld->cld_refcount) > 0);
557 * Do not run mgc_process_log on a disconnected export or an
558 * export which is being disconnected. Take the client
559 * semaphore to make the check non-racy.
561 down_read_nested(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem,
563 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
564 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
565 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
566 if (rc && rc != -ENOENT)
567 CERROR("failed processing log: %d\n", rc);
569 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
572 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
577 /* this timeout represents how many seconds MGC should wait before
578 * requeue config and recover lock to the MGS. We need to randomize this
579 * in order to not flood the MGS.
581 #define MGC_TIMEOUT_MIN_SECONDS 5
582 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
584 static int mgc_requeue_thread(void *data)
590 CDEBUG(D_MGC, "Starting requeue thread\n");
592 /* Keep trying failed locks periodically */
593 spin_lock(&config_list_lock);
594 rq_state |= RQ_RUNNING;
596 struct l_wait_info lwi;
597 struct config_llog_data *cld, *cld_prev;
598 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
599 int stopped = !!(rq_state & RQ_STOP);
602 /* Any new or requeued lostlocks will change the state */
603 rq_state &= ~(RQ_NOW | RQ_LATER);
604 spin_unlock(&config_list_lock);
611 /* Always wait a few seconds to allow the server who
612 caused the lock revocation to finish its setup, plus some
613 random so everyone doesn't try to reconnect at once. */
614 to = msecs_to_jiffies(MGC_TIMEOUT_MIN_SECONDS * MSEC_PER_SEC);
615 /* rand is centi-seconds */
616 to += msecs_to_jiffies(rand * MSEC_PER_SEC / 100);
617 lwi = LWI_TIMEOUT(to, NULL, NULL);
618 l_wait_event(rq_waitq, rq_state & (RQ_STOP | RQ_PRECLEANUP),
622 * iterate & processing through the list. for each cld, process
623 * its depending sptlrpc cld firstly (if any) and then itself.
625 * it's guaranteed any item in the list must have
626 * reference > 0; and if cld_lostlock is set, at
627 * least one reference is taken by the previous enqueue.
631 spin_lock(&config_list_lock);
632 rq_state &= ~RQ_PRECLEANUP;
633 list_for_each_entry(cld, &config_llog_list,
635 if (!cld->cld_lostlock)
638 spin_unlock(&config_list_lock);
640 LASSERT(atomic_read(&cld->cld_refcount) > 0);
642 /* Whether we enqueued again or not in mgc_process_log,
643 * we're done with the ref from the old enqueue */
645 config_log_put(cld_prev);
648 cld->cld_lostlock = 0;
649 if (likely(!stopped))
652 spin_lock(&config_list_lock);
654 spin_unlock(&config_list_lock);
656 config_log_put(cld_prev);
658 /* break after scanning the list so that we can drop
659 * refcount to losing lock clds */
660 if (unlikely(stopped)) {
661 spin_lock(&config_list_lock);
665 /* Wait a bit to see if anyone else needs a requeue */
666 lwi = (struct l_wait_info) { 0 };
667 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
669 spin_lock(&config_list_lock);
671 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
672 rq_state &= ~RQ_RUNNING;
673 spin_unlock(&config_list_lock);
677 CDEBUG(D_MGC, "Ending requeue thread\n");
681 /* Add a cld to the list to requeue. Start the requeue thread if needed.
682 We are responsible for dropping the config log reference from here on out. */
683 static void mgc_requeue_add(struct config_llog_data *cld)
687 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
688 cld->cld_logname, atomic_read(&cld->cld_refcount),
689 cld->cld_stopping, rq_state);
690 LASSERT(atomic_read(&cld->cld_refcount) > 0);
692 mutex_lock(&cld->cld_lock);
693 if (cld->cld_stopping || cld->cld_lostlock) {
694 mutex_unlock(&cld->cld_lock);
697 /* this refcount will be released in mgc_requeue_thread. */
699 cld->cld_lostlock = 1;
700 mutex_unlock(&cld->cld_lock);
702 /* Hold lock for rq_state */
703 spin_lock(&config_list_lock);
704 if (rq_state & RQ_STOP) {
705 spin_unlock(&config_list_lock);
706 cld->cld_lostlock = 0;
710 spin_unlock(&config_list_lock);
716 /********************** class fns **********************/
717 static int mgc_local_llog_init(const struct lu_env *env,
718 struct obd_device *obd,
719 struct obd_device *disk)
721 struct llog_ctxt *ctxt;
726 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
731 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
733 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
739 static int mgc_local_llog_fini(const struct lu_env *env,
740 struct obd_device *obd)
742 struct llog_ctxt *ctxt;
746 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
747 llog_cleanup(env, ctxt);
752 static int mgc_fs_setup(const struct lu_env *env, struct obd_device *obd,
753 struct super_block *sb)
755 struct lustre_sb_info *lsi = s2lsi(sb);
756 struct client_obd *cli = &obd->u.cli;
757 struct lu_fid rfid, fid;
758 struct dt_object *root, *dto;
764 LASSERT(lsi->lsi_dt_dev);
766 /* The mgc fs exclusion mutex. Only one fs can be setup at a time. */
767 mutex_lock(&cli->cl_mgc_mutex);
769 /* Setup the configs dir */
770 fid.f_seq = FID_SEQ_LOCAL_NAME;
773 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
778 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
782 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
783 &cli->cl_mgc_los->los_dev->dd_lu_dev, NULL);
784 if (unlikely(IS_ERR(root)))
785 GOTO(out_los, rc = PTR_ERR(root));
787 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
789 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
790 lu_object_put_nocache(env, &root->do_lu);
792 GOTO(out_los, rc = PTR_ERR(dto));
794 cli->cl_mgc_configs_dir = dto;
796 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
797 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
801 /* We take an obd ref to insure that we can't get to mgc_cleanup
802 * without calling mgc_fs_cleanup first. */
803 class_incref(obd, "mgc_fs", obd);
805 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
809 lu_object_put(env, &cli->cl_mgc_configs_dir->do_lu);
810 cli->cl_mgc_configs_dir = NULL;
814 local_oid_storage_fini(env, cli->cl_mgc_los);
815 cli->cl_mgc_los = NULL;
816 mutex_unlock(&cli->cl_mgc_mutex);
821 static int mgc_fs_cleanup(const struct lu_env *env, struct obd_device *obd)
823 struct client_obd *cli = &obd->u.cli;
826 LASSERT(cli->cl_mgc_los != NULL);
828 mgc_local_llog_fini(env, obd);
830 lu_object_put_nocache(env, &cli->cl_mgc_configs_dir->do_lu);
831 cli->cl_mgc_configs_dir = NULL;
833 local_oid_storage_fini(env, cli->cl_mgc_los);
834 cli->cl_mgc_los = NULL;
836 class_decref(obd, "mgc_fs", obd);
837 mutex_unlock(&cli->cl_mgc_mutex);
842 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
844 struct llog_ctxt *ctxt;
849 /* setup only remote ctxt, the local disk context is switched per each
850 * filesystem during mgc_fs_setup() */
851 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
856 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
859 llog_initiator_connect(ctxt);
865 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
867 struct llog_ctxt *ctxt;
871 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
873 llog_cleanup(env, ctxt);
879 static atomic_t mgc_count = ATOMIC_INIT(0);
880 static int mgc_precleanup(struct obd_device *obd)
886 if (atomic_dec_and_test(&mgc_count)) {
887 LASSERT(rq_state & RQ_RUNNING);
888 /* stop requeue thread */
891 /* wakeup requeue thread to clean our cld */
892 temp = RQ_NOW | RQ_PRECLEANUP;
895 spin_lock(&config_list_lock);
897 spin_unlock(&config_list_lock);
901 wait_for_completion(&rq_exit);
902 obd_cleanup_client_import(obd);
904 rc = mgc_llog_fini(NULL, obd);
906 CERROR("failed to cleanup llogging subsystems\n");
911 static int mgc_cleanup(struct obd_device *obd)
916 /* COMPAT_146 - old config logs may have added profiles we don't
918 if (obd->obd_type->typ_refcnt <= 1)
919 /* Only for the last mgc */
920 class_del_profiles();
922 lprocfs_obd_cleanup(obd);
925 rc = client_obd_cleanup(obd);
929 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
931 struct task_struct *task;
935 rc = ptlrpcd_addref();
939 rc = client_obd_setup(obd, lcfg);
941 GOTO(err_decref, rc);
943 rc = mgc_llog_init(NULL, obd);
945 CERROR("failed to setup llogging subsystems\n");
946 GOTO(err_cleanup, rc);
949 #ifdef CONFIG_PROC_FS
950 obd->obd_vars = lprocfs_mgc_obd_vars;
951 lprocfs_obd_setup(obd);
953 sptlrpc_lprocfs_cliobd_attach(obd);
955 if (atomic_inc_return(&mgc_count) == 1) {
957 init_waitqueue_head(&rq_waitq);
959 /* start requeue thread */
960 task = kthread_run(mgc_requeue_thread, NULL, "ll_cfg_requeue");
963 CERROR("%s: cannot start requeue thread: rc = %d; "
964 "no more log updates\n",
966 GOTO(err_cleanup, rc);
968 /* rc is the task_struct pointer of mgc_requeue_thread. */
970 wait_for_completion(&rq_start);
976 client_obd_cleanup(obd);
982 /* based on ll_mdc_blocking_ast */
983 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
984 void *data, int flag)
986 struct lustre_handle lockh;
987 struct config_llog_data *cld = (struct config_llog_data *)data;
992 case LDLM_CB_BLOCKING:
993 /* mgs wants the lock, give it up... */
994 LDLM_DEBUG(lock, "MGC blocking CB");
995 ldlm_lock2handle(lock, &lockh);
996 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
998 case LDLM_CB_CANCELING:
999 /* We've given up the lock, prepare ourselves to update. */
1000 LDLM_DEBUG(lock, "MGC cancel CB");
1002 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
1003 PLDLMRES(lock->l_resource),
1004 (char *)&lock->l_resource->lr_name.name[0]);
1007 CDEBUG(D_INFO, "missing data, won't requeue\n");
1011 /* held at mgc_process_log(). */
1012 LASSERT(atomic_read(&cld->cld_refcount) > 0);
1013 /* Are we done with this log? */
1014 if (cld->cld_stopping) {
1015 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
1017 config_log_put(cld);
1020 /* Make sure not to re-enqueue when the mgc is stopping
1021 (we get called from client_disconnect_export) */
1022 if (lock->l_conn_export == NULL ||
1023 lock->l_conn_export->exp_obd->u.cli.cl_conn_count == 0) {
1024 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
1026 config_log_put(cld);
1030 /* Re-enqueue now */
1031 mgc_requeue_add(cld);
1032 config_log_put(cld);
1041 /* Not sure where this should go... */
1042 /* This is the timeout value for MGS_CONNECT request plus a ping interval, such
1043 * that we can have a chance to try the secondary MGS if any. */
1044 #define MGC_ENQUEUE_LIMIT (INITIAL_CONNECT_TIMEOUT + (AT_OFF ? 0 : at_min) \
1046 #define MGC_TARGET_REG_LIMIT 10
1047 #define MGC_SEND_PARAM_LIMIT 10
1049 /* Send parameter to MGS*/
1050 static int mgc_set_mgs_param(struct obd_export *exp,
1051 struct mgs_send_param *msp)
1053 struct ptlrpc_request *req;
1054 struct mgs_send_param *req_msp, *rep_msp;
1058 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1059 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
1064 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1066 ptlrpc_req_finished(req);
1070 memcpy(req_msp, msp, sizeof(*req_msp));
1071 ptlrpc_request_set_replen(req);
1073 /* Limit how long we will wait for the enqueue to complete */
1074 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
1075 rc = ptlrpc_queue_wait(req);
1077 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1078 memcpy(msp, rep_msp, sizeof(*rep_msp));
1081 ptlrpc_req_finished(req);
1086 /* Take a config lock so we can get cancel notifications */
1087 static int mgc_enqueue(struct obd_export *exp, enum ldlm_type type,
1088 union ldlm_policy_data *policy, enum ldlm_mode mode,
1089 __u64 *flags, void *bl_cb, void *cp_cb, void *gl_cb,
1090 void *data, __u32 lvb_len, void *lvb_swabber,
1091 struct lustre_handle *lockh)
1093 struct config_llog_data *cld = (struct config_llog_data *)data;
1094 struct ldlm_enqueue_info einfo = {
1097 .ei_cb_bl = mgc_blocking_ast,
1098 .ei_cb_cp = ldlm_completion_ast,
1100 struct ptlrpc_request *req;
1101 int short_limit = cld_is_sptlrpc(cld);
1105 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
1106 cld->cld_resid.name[0]);
1108 /* We need a callback for every lockholder, so don't try to
1109 ldlm_lock_match (see rev 1.1.2.11.2.47) */
1110 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1111 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1116 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1117 ptlrpc_request_set_replen(req);
1119 /* check if this is server or client */
1120 if (cld->cld_cfg.cfg_sb) {
1121 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
1122 if (lsi && IS_SERVER(lsi))
1125 /* Limit how long we will wait for the enqueue to complete */
1126 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
1127 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1128 NULL, 0, LVB_T_NONE, lockh, 0);
1129 /* A failed enqueue should still call the mgc_blocking_ast,
1130 where it will be requeued if needed ("grant failed"). */
1131 ptlrpc_req_finished(req);
1135 static int mgc_cancel(struct obd_export *exp, enum ldlm_mode mode,
1136 struct lustre_handle *lockh)
1140 ldlm_lock_decref(lockh, mode);
1145 static void mgc_notify_active(struct obd_device *unused)
1147 /* wakeup mgc_requeue_thread to requeue mgc lock */
1148 spin_lock(&config_list_lock);
1150 spin_unlock(&config_list_lock);
1153 /* TODO: Help the MGS rebuild nidtbl. -jay */
1156 /* Send target_reg message to MGS */
1157 static int mgc_target_register(struct obd_export *exp,
1158 struct mgs_target_info *mti)
1160 struct ptlrpc_request *req;
1161 struct mgs_target_info *req_mti, *rep_mti;
1165 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1166 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1171 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1173 ptlrpc_req_finished(req);
1177 memcpy(req_mti, mti, sizeof(*req_mti));
1178 ptlrpc_request_set_replen(req);
1179 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1180 /* Limit how long we will wait for the enqueue to complete */
1181 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1183 rc = ptlrpc_queue_wait(req);
1185 rep_mti = req_capsule_server_get(&req->rq_pill,
1186 &RMF_MGS_TARGET_INFO);
1187 memcpy(mti, rep_mti, sizeof(*rep_mti));
1188 CDEBUG(D_MGC, "register %s got index = %d\n",
1189 mti->mti_svname, mti->mti_stripe_index);
1191 ptlrpc_req_finished(req);
1196 static int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1197 u32 keylen, void *key,
1198 u32 vallen, void *val,
1199 struct ptlrpc_request_set *set)
1204 /* Turn off initial_recov after we try all backup servers once */
1205 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1206 struct obd_import *imp = class_exp2cliimp(exp);
1208 if (vallen != sizeof(int))
1210 value = *(int *)val;
1211 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1212 imp->imp_obd->obd_name, value,
1213 imp->imp_deactive, imp->imp_invalid,
1214 imp->imp_replayable, imp->imp_obd->obd_replayable,
1215 ptlrpc_import_state_name(imp->imp_state));
1216 /* Resurrect if we previously died */
1217 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1218 imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1219 ptlrpc_reconnect_import(imp);
1222 /* FIXME move this to mgc_process_config */
1223 if (KEY_IS(KEY_REGISTER_TARGET)) {
1224 struct mgs_target_info *mti;
1225 if (vallen != sizeof(struct mgs_target_info))
1227 mti = (struct mgs_target_info *)val;
1228 CDEBUG(D_MGC, "register_target %s %#x\n",
1229 mti->mti_svname, mti->mti_flags);
1230 rc = mgc_target_register(exp, mti);
1233 if (KEY_IS(KEY_SET_FS)) {
1234 struct super_block *sb = (struct super_block *)val;
1236 if (vallen != sizeof(struct super_block))
1239 rc = mgc_fs_setup(env, exp->exp_obd, sb);
1242 if (KEY_IS(KEY_CLEAR_FS)) {
1245 rc = mgc_fs_cleanup(env, exp->exp_obd);
1248 if (KEY_IS(KEY_SET_INFO)) {
1249 struct mgs_send_param *msp;
1251 msp = (struct mgs_send_param *)val;
1252 rc = mgc_set_mgs_param(exp, msp);
1255 if (KEY_IS(KEY_MGSSEC)) {
1256 struct client_obd *cli = &exp->exp_obd->u.cli;
1257 struct sptlrpc_flavor flvr;
1260 * empty string means using current flavor, if which haven't
1261 * been set yet, set it as null.
1263 * if flavor has been set previously, check the asking flavor
1264 * must match the existing one.
1267 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1273 rc = sptlrpc_parse_flavor(val, &flvr);
1275 CERROR("invalid sptlrpc flavor %s to MGS\n",
1281 * caller already hold a mutex
1283 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1284 cli->cl_flvr_mgc = flvr;
1285 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1286 sizeof(flvr)) != 0) {
1289 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1291 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1292 "currently %s is in use\n",
1302 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1303 __u32 keylen, void *key, __u32 *vallen, void *val)
1307 if (KEY_IS(KEY_CONN_DATA)) {
1308 struct obd_import *imp = class_exp2cliimp(exp);
1309 struct obd_connect_data *data = val;
1311 if (*vallen == sizeof(*data)) {
1312 *data = imp->imp_connect_data;
1320 static int mgc_import_event(struct obd_device *obd,
1321 struct obd_import *imp,
1322 enum obd_import_event event)
1326 LASSERT(imp->imp_obd == obd);
1327 CDEBUG(D_MGC, "import event %#x\n", event);
1330 case IMP_EVENT_DISCON:
1331 /* MGC imports should not wait for recovery */
1332 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1333 ptlrpc_pinger_ir_down();
1335 case IMP_EVENT_INACTIVE:
1337 case IMP_EVENT_INVALIDATE: {
1338 struct ldlm_namespace *ns = obd->obd_namespace;
1339 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1342 case IMP_EVENT_ACTIVE:
1343 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1344 /* Clearing obd_no_recov allows us to continue pinging */
1345 obd->obd_no_recov = 0;
1346 mgc_notify_active(obd);
1347 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1348 ptlrpc_pinger_ir_up();
1352 case IMP_EVENT_DEACTIVATE:
1353 case IMP_EVENT_ACTIVATE:
1356 CERROR("Unknown import event %#x\n", event);
1363 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_CACHE_SHIFT),
1364 CONFIG_READ_NRPAGES = 4
1367 static int mgc_apply_recover_logs(struct obd_device *mgc,
1368 struct config_llog_data *cld,
1370 void *data, int datalen, bool mne_swab)
1372 struct config_llog_instance *cfg = &cld->cld_cfg;
1373 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1374 struct mgs_nidtbl_entry *entry;
1375 struct lustre_cfg *lcfg;
1376 struct lustre_cfg_bufs bufs;
1377 u64 prev_version = 0;
1386 LASSERT(cfg->cfg_instance != NULL);
1387 LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1389 OBD_ALLOC(inst, PAGE_CACHE_SIZE);
1393 if (!IS_SERVER(lsi)) {
1394 pos = snprintf(inst, PAGE_CACHE_SIZE, "%p", cfg->cfg_instance);
1395 if (pos >= PAGE_CACHE_SIZE) {
1396 OBD_FREE(inst, PAGE_CACHE_SIZE);
1400 LASSERT(IS_MDT(lsi));
1401 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1404 OBD_FREE(inst, PAGE_CACHE_SIZE);
1412 bufsz = PAGE_CACHE_SIZE - pos;
1414 while (datalen > 0) {
1415 int entry_len = sizeof(*entry);
1417 struct obd_device *obd;
1424 if (datalen < sizeof(*entry))
1427 entry = (typeof(entry))(data + off);
1430 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1432 if (entry->mne_nid_count == 0) /* at least one nid entry */
1434 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1437 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1438 if (datalen < entry_len) /* must have entry_len at least */
1441 /* Keep this swab for normal mixed endian handling. LU-1644 */
1443 lustre_swab_mgs_nidtbl_entry(entry);
1444 if (entry->mne_length > PAGE_CACHE_SIZE) {
1445 CERROR("MNE too large (%u)\n", entry->mne_length);
1449 if (entry->mne_length < entry_len)
1452 off += entry->mne_length;
1453 datalen -= entry->mne_length;
1457 if (entry->mne_version > max_version) {
1458 CERROR("entry index(%lld) is over max_index(%lld)\n",
1459 entry->mne_version, max_version);
1463 if (prev_version >= entry->mne_version) {
1464 CERROR("index unsorted, prev %lld, now %lld\n",
1465 prev_version, entry->mne_version);
1468 prev_version = entry->mne_version;
1471 * Write a string with format "nid::instance" to
1472 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1475 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1476 memset(buf, 0, bufsz);
1480 /* lustre-OST0001-osc-<instance #> */
1481 strcpy(obdname, cld->cld_logname);
1482 cname = strrchr(obdname, '-');
1483 if (cname == NULL) {
1484 CERROR("mgc %s: invalid logname %s\n",
1485 mgc->obd_name, obdname);
1489 pos = cname - obdname;
1491 pos += sprintf(obdname + pos, "-%s%04x",
1492 is_ost ? "OST" : "MDT", entry->mne_index);
1494 cname = is_ost ? "osc" : "mdc",
1495 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1496 lustre_cfg_bufs_reset(&bufs, obdname);
1498 /* find the obd by obdname */
1499 obd = class_name2obd(obdname);
1501 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1502 mgc->obd_name, obdname);
1504 /* this is a safe race, when the ost is starting up...*/
1508 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1511 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1514 down_read(&obd->u.cli.cl_sem);
1515 if (obd->u.cli.cl_import == NULL) {
1516 /* client does not connect to the OST yet */
1517 up_read(&obd->u.cli.cl_sem);
1522 /* iterate all nids to find one */
1523 /* find uuid by nid */
1525 for (i = 0; i < entry->mne_nid_count; i++) {
1526 rc = client_import_find_conn(obd->u.cli.cl_import,
1528 (struct obd_uuid *)uuid);
1533 up_read(&obd->u.cli.cl_sem);
1535 CERROR("mgc: cannot find uuid by nid %s\n",
1536 libcfs_nid2str(entry->u.nids[0]));
1540 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1541 uuid, libcfs_nid2str(entry->u.nids[0]));
1543 pos += strlen(uuid);
1544 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1545 LASSERT(pos < bufsz);
1547 lustre_cfg_bufs_set_string(&bufs, 1, params);
1549 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1555 CDEBUG(D_INFO, "ir apply logs "LPD64"/"LPD64" for %s -> %s\n",
1556 prev_version, max_version, obdname, params);
1558 rc = class_process_config(lcfg);
1559 lustre_cfg_free(lcfg);
1561 CDEBUG(D_INFO, "process config for %s error %d\n",
1564 /* continue, even one with error */
1567 OBD_FREE(inst, PAGE_CACHE_SIZE);
1572 * This function is called if this client was notified for target restarting
1573 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery or
1576 static int mgc_process_recover_nodemap_log(struct obd_device *obd,
1577 struct config_llog_data *cld)
1579 struct ptlrpc_request *req = NULL;
1580 struct config_llog_instance *cfg = &cld->cld_cfg;
1581 struct mgs_config_body *body;
1582 struct mgs_config_res *res;
1583 struct nodemap_config *new_config = NULL;
1584 struct lu_nodemap *recent_nodemap = NULL;
1585 struct ptlrpc_bulk_desc *desc;
1586 struct page **pages;
1587 __u64 config_read_offset = 0;
1590 bool mne_swab = false;
1597 /* allocate buffer for bulk transfer.
1598 * if this is the first time for this mgs to read logs,
1599 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1600 * once; otherwise, it only reads increment of logs, this should be
1601 * small and CONFIG_READ_NRPAGES will be used.
1603 nrpages = CONFIG_READ_NRPAGES;
1604 if (cfg->cfg_last_idx == 0 || cld_is_nodemap(cld))
1605 nrpages = CONFIG_READ_NRPAGES_INIT;
1607 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1609 GOTO(out, rc = -ENOMEM);
1611 for (i = 0; i < nrpages; i++) {
1612 pages[i] = alloc_page(GFP_KERNEL);
1613 if (pages[i] == NULL)
1614 GOTO(out, rc = -ENOMEM);
1618 #ifdef HAVE_SERVER_SUPPORT
1619 if (cld_is_nodemap(cld) && config_read_offset == 0) {
1620 new_config = nodemap_config_alloc();
1621 if (IS_ERR(new_config)) {
1622 rc = PTR_ERR(new_config);
1628 LASSERT(cld_is_recover(cld) || cld_is_nodemap(cld));
1629 LASSERT(mutex_is_locked(&cld->cld_lock));
1630 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1631 &RQF_MGS_CONFIG_READ);
1633 GOTO(out, rc = -ENOMEM);
1635 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1640 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1641 LASSERT(body != NULL);
1642 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1643 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1644 >= sizeof(body->mcb_name))
1645 GOTO(out, rc = -E2BIG);
1646 if (cld_is_nodemap(cld))
1647 body->mcb_offset = config_read_offset;
1649 body->mcb_offset = cfg->cfg_last_idx + 1;
1650 body->mcb_type = cld->cld_type;
1651 body->mcb_bits = PAGE_CACHE_SHIFT;
1652 body->mcb_units = nrpages;
1654 /* allocate bulk transfer descriptor */
1655 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1,
1656 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1658 &ptlrpc_bulk_kiov_pin_ops);
1660 GOTO(out, rc = -ENOMEM);
1662 for (i = 0; i < nrpages; i++)
1663 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1666 ptlrpc_request_set_replen(req);
1667 rc = ptlrpc_queue_wait(req);
1671 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1673 GOTO(out, rc = -EPROTO);
1675 if (cld_is_nodemap(cld)) {
1676 config_read_offset = res->mcr_offset;
1677 eof = config_read_offset == II_END_OFF;
1679 if (res->mcr_size < res->mcr_offset)
1680 GOTO(out, rc = -EINVAL);
1682 /* always update the index even though it might have errors with
1683 * handling the recover logs
1685 cfg->cfg_last_idx = res->mcr_offset;
1686 eof = res->mcr_offset == res->mcr_size;
1688 CDEBUG(D_INFO, "Latest version "LPD64", more %d.\n",
1689 res->mcr_offset, eof == false);
1692 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1694 GOTO(out, rc = ealen);
1696 if (ealen > nrpages << PAGE_CACHE_SHIFT)
1697 GOTO(out, rc = -EINVAL);
1699 if (ealen == 0) { /* no logs transferred */
1700 #ifdef HAVE_SERVER_SUPPORT
1701 /* config changed since first read RPC */
1702 if (cld_is_nodemap(cld) && config_read_offset == 0) {
1703 recent_nodemap = NULL;
1704 nodemap_config_dealloc(new_config);
1707 CDEBUG(D_INFO, "nodemap config changed in transit, retrying\n");
1709 /* setting eof to false, we request config again */
1719 mne_swab = !!ptlrpc_rep_need_swab(req);
1720 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
1721 /* This import flag means the server did an extra swab of IR MNE
1722 * records (fixed in LU-1252), reverse it here if needed. LU-1644 */
1723 if (unlikely(req->rq_import->imp_need_mne_swab))
1724 mne_swab = !mne_swab;
1727 /* When a nodemap config is received, we build a new nodemap config,
1728 * with new nodemap structs. We keep track of the most recently added
1729 * nodemap since the config is read ordered by nodemap_id, and so it
1730 * is likely that the next record will be related. Because access to
1731 * the nodemaps is single threaded until the nodemap_config is active,
1732 * we don't need to reference count with recent_nodemap, though
1733 * recent_nodemap should be set to NULL when the nodemap_config
1734 * is either destroyed or set active.
1736 for (i = 0; i < nrpages && ealen > 0; i++) {
1740 ptr = kmap(pages[i]);
1741 if (cld_is_nodemap(cld))
1742 rc2 = nodemap_process_idx_pages(new_config, ptr,
1745 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset,
1752 CWARN("%s: error processing %s log %s: rc = %d\n",
1754 cld_is_nodemap(cld) ? "nodemap" : "recovery",
1760 ealen -= PAGE_CACHE_SIZE;
1765 ptlrpc_req_finished(req);
1769 if (rc == 0 && !eof)
1772 #ifdef HAVE_SERVER_SUPPORT
1773 if (new_config != NULL) {
1774 /* recent_nodemap cannot be used after set_active/dealloc */
1776 nodemap_config_set_active_mgc(new_config);
1778 nodemap_config_dealloc(new_config);
1783 for (i = 0; i < nrpages; i++) {
1784 if (pages[i] == NULL)
1786 __free_page(pages[i]);
1788 OBD_FREE(pages, sizeof(*pages) * nrpages);
1793 /* Copy a remote log locally */
1794 static int mgc_llog_local_copy(const struct lu_env *env,
1795 struct obd_device *obd,
1796 struct llog_ctxt *rctxt,
1797 struct llog_ctxt *lctxt, char *logname)
1805 * - copy it to backup using llog_backup()
1806 * - copy remote llog to logname using llog_backup()
1807 * - if failed then move bakup to logname again
1810 OBD_ALLOC(temp_log, strlen(logname) + 2);
1813 sprintf(temp_log, "%sT", logname);
1815 /* make a copy of local llog at first */
1816 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1817 if (rc < 0 && rc != -ENOENT)
1819 /* copy remote llog to the local copy */
1820 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1821 if (rc == -ENOENT) {
1822 /* no remote llog, delete local one too */
1823 llog_erase(env, lctxt, NULL, logname);
1824 } else if (rc < 0) {
1825 /* error during backup, get local one back from the copy */
1826 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1828 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1829 obd->obd_name, logname, rc);
1831 llog_erase(env, lctxt, NULL, temp_log);
1832 OBD_FREE(temp_log, strlen(logname) + 2);
1836 /* local_only means it cannot get remote llogs */
1837 static int mgc_process_cfg_log(struct obd_device *mgc,
1838 struct config_llog_data *cld, int local_only)
1840 struct llog_ctxt *ctxt, *lctxt = NULL;
1841 struct client_obd *cli = &mgc->u.cli;
1842 struct lustre_sb_info *lsi = NULL;
1844 bool sptlrpc_started = false;
1850 LASSERT(mutex_is_locked(&cld->cld_lock));
1853 * local copy of sptlrpc log is controlled elsewhere, don't try to
1856 if (cld_is_sptlrpc(cld) && local_only)
1859 if (cld->cld_cfg.cfg_sb)
1860 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1866 rc = lu_env_init(env, LCT_MG_THREAD);
1870 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1873 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1875 /* Copy the setup log locally if we can. Don't mess around if we're
1876 * running an MGS though (logs are already local). */
1877 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1878 cli->cl_mgc_configs_dir != NULL &&
1879 lu2dt_dev(cli->cl_mgc_configs_dir->do_lu.lo_dev) ==
1882 /* Only try to copy log if we have the lock. */
1883 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1885 if (local_only || rc) {
1886 if (strcmp(cld->cld_logname, PARAMS_FILENAME) != 0 &&
1887 llog_is_empty(env, lctxt, cld->cld_logname)) {
1888 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1889 "log %s and no local copy."
1890 "\n", cld->cld_logname);
1891 GOTO(out_pop, rc = -ENOENT);
1893 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1894 "copy for now, will try to update later.\n",
1898 /* Now, whether we copied or not, start using the local llog.
1899 * If we failed to copy, we'll start using whatever the old
1901 llog_ctxt_put(ctxt);
1905 if (local_only) /* no local log at client side */
1906 GOTO(out_pop, rc = -EIO);
1909 if (cld_is_sptlrpc(cld)) {
1910 sptlrpc_conf_log_update_begin(cld->cld_logname);
1911 sptlrpc_started = true;
1914 /* logname and instance info should be the same, so use our
1915 * copy of the instance for the update. The cfg_last_idx will
1916 * be updated here. */
1917 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1922 __llog_ctxt_put(env, ctxt);
1924 __llog_ctxt_put(env, lctxt);
1927 * update settings on existing OBDs. doing it inside
1928 * of llog_process_lock so no device is attaching/detaching
1930 * the logname must be <fsname>-sptlrpc
1932 if (sptlrpc_started) {
1933 LASSERT(cld_is_sptlrpc(cld));
1934 sptlrpc_conf_log_update_end(cld->cld_logname);
1935 class_notify_sptlrpc_conf(cld->cld_logname,
1936 strlen(cld->cld_logname) -
1937 strlen("-sptlrpc"));
1946 static bool mgc_import_in_recovery(struct obd_import *imp)
1948 bool in_recovery = true;
1950 spin_lock(&imp->imp_lock);
1951 if (imp->imp_state == LUSTRE_IMP_FULL ||
1952 imp->imp_state == LUSTRE_IMP_CLOSED)
1953 in_recovery = false;
1954 spin_unlock(&imp->imp_lock);
1960 * Get a configuration log from the MGS and process it.
1962 * This function is called for both clients and servers to process the
1963 * configuration log from the MGS. The MGC enqueues a DLM lock on the
1964 * log from the MGS, and if the lock gets revoked the MGC will be notified
1965 * by the lock cancellation callback that the config log has changed,
1966 * and will enqueue another MGS lock on it, and then continue processing
1967 * the new additions to the end of the log.
1969 * Since the MGC import is not replayable, if the import is being evicted
1970 * (rcl == -ESHUTDOWN, \see ptlrpc_import_delay_req()), retry to process
1971 * the log until recovery is finished or the import is closed.
1973 * Make a local copy of the log before parsing it if appropriate (non-MGS
1974 * server) so that the server can start even when the MGS is down.
1976 * There shouldn't be multiple processes running process_log at once --
1977 * sounds like badness. It actually might be fine, as long as they're not
1978 * trying to update from the same log simultaneously, in which case we
1979 * should use a per-log semaphore instead of cld_lock.
1981 * \param[in] mgc MGC device by which to fetch the configuration log
1982 * \param[in] cld log processing state (stored in lock callback data)
1984 * \retval 0 on success
1985 * \retval negative errno on failure
1987 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1989 struct lustre_handle lockh = { 0 };
1990 __u64 flags = LDLM_FL_NO_LRU;
1995 LASSERT(cld != NULL);
1997 /* I don't want multiple processes running process_log at once --
1998 sounds like badness. It actually might be fine, as long as
1999 we're not trying to update from the same log
2000 simultaneously (in which case we should use a per-log sem.) */
2002 mutex_lock(&cld->cld_lock);
2003 if (cld->cld_stopping) {
2004 mutex_unlock(&cld->cld_lock);
2008 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
2010 CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
2011 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
2013 /* Get the cfg lock on the llog */
2014 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, LDLM_PLAIN, NULL,
2015 LCK_CR, &flags, NULL, NULL, NULL,
2016 cld, 0, NULL, &lockh);
2018 /* Get the cld, it will be released in mgc_blocking_ast. */
2019 config_log_get(cld);
2020 rc = ldlm_lock_set_data(&lockh, (void *)cld);
2023 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
2025 if (rcl == -ESHUTDOWN &&
2026 atomic_read(&mgc->u.cli.cl_mgc_refcount) > 0 && !retry) {
2027 struct obd_import *imp;
2028 struct l_wait_info lwi;
2029 int secs = cfs_time_seconds(obd_timeout);
2031 mutex_unlock(&cld->cld_lock);
2032 imp = class_exp2cliimp(mgc->u.cli.cl_mgc_mgsexp);
2034 /* Let's force the pinger, and wait the import to be
2035 * connected, note: since mgc import is non-replayable,
2036 * and even the import state is disconnected, it does
2037 * not mean the "recovery" is stopped, so we will keep
2038 * waitting until timeout or the import state is
2040 ptlrpc_pinger_force(imp);
2042 lwi = LWI_TIMEOUT(secs, NULL, NULL);
2043 l_wait_event(imp->imp_recovery_waitq,
2044 !mgc_import_in_recovery(imp), &lwi);
2046 if (imp->imp_state == LUSTRE_IMP_FULL) {
2050 mutex_lock(&cld->cld_lock);
2051 cld->cld_lostlock = 1;
2054 /* mark cld_lostlock so that it will requeue
2055 * after MGC becomes available. */
2056 cld->cld_lostlock = 1;
2058 /* Get extra reference, it will be put in requeue thread */
2059 config_log_get(cld);
2063 if (cld_is_recover(cld) || cld_is_nodemap(cld)) {
2065 rc = mgc_process_recover_nodemap_log(mgc, cld);
2066 else if (cld_is_nodemap(cld))
2069 if (cld_is_recover(cld) && rc) {
2071 CERROR("%s: recover log %s failed, not fatal: rc = %d\n",
2072 mgc->obd_name, cld->cld_logname, rc);
2073 cld->cld_lostlock = 1;
2075 rc = 0; /* this is not a fatal error for recover log */
2078 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
2081 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
2082 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
2084 mutex_unlock(&cld->cld_lock);
2086 /* Now drop the lock so MGS can revoke it */
2088 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, LCK_CR, &lockh);
2090 CERROR("Can't drop cfg lock: %d\n", rcl);
2097 /** Called from lustre_process_log.
2098 * LCFG_LOG_START gets the config log from the MGS, processes it to start
2099 * any services, and adds it to the list logs to watch (follow).
2101 static int mgc_process_config(struct obd_device *obd, size_t len, void *buf)
2103 struct lustre_cfg *lcfg = buf;
2104 struct config_llog_instance *cfg = NULL;
2109 switch(lcfg->lcfg_command) {
2110 case LCFG_LOV_ADD_OBD: {
2111 /* Overloading this cfg command: register a new target */
2112 struct mgs_target_info *mti;
2114 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
2115 sizeof(struct mgs_target_info))
2116 GOTO(out, rc = -EINVAL);
2118 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
2119 CDEBUG(D_MGC, "add_target %s %#x\n",
2120 mti->mti_svname, mti->mti_flags);
2121 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
2124 case LCFG_LOV_DEL_OBD:
2125 /* Unregister has no meaning at the moment. */
2126 CERROR("lov_del_obd unimplemented\n");
2129 case LCFG_SPTLRPC_CONF: {
2130 rc = sptlrpc_process_config(lcfg);
2133 case LCFG_LOG_START: {
2134 struct config_llog_data *cld;
2135 struct super_block *sb;
2137 logname = lustre_cfg_string(lcfg, 1);
2138 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
2139 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
2141 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
2144 /* We're only called through here on the initial mount */
2145 rc = config_log_add(obd, logname, cfg, sb);
2148 cld = config_log_find(logname, cfg);
2155 /* FIXME only set this for old logs! Right now this forces
2156 us to always skip the "inside markers" check */
2157 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
2159 rc = mgc_process_log(obd, cld);
2160 if (rc == 0 && cld->cld_recover != NULL) {
2161 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
2162 imp_connect_data, IMP_RECOV)) {
2163 rc = mgc_process_log(obd, cld->cld_recover);
2165 struct config_llog_data *cir = cld->cld_recover;
2166 cld->cld_recover = NULL;
2167 config_log_put(cir);
2170 CERROR("Cannot process recover llog %d\n", rc);
2173 if (rc == 0 && cld->cld_params != NULL) {
2174 rc = mgc_process_log(obd, cld->cld_params);
2175 if (rc == -ENOENT) {
2176 CDEBUG(D_MGC, "There is no params "
2177 "config file yet\n");
2180 /* params log is optional */
2182 CERROR("%s: can't process params llog: rc = %d\n",
2185 config_log_put(cld);
2189 case LCFG_LOG_END: {
2190 logname = lustre_cfg_string(lcfg, 1);
2192 if (lcfg->lcfg_bufcount >= 2)
2193 cfg = (struct config_llog_instance *)lustre_cfg_buf(
2195 rc = config_log_end(logname, cfg);
2199 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
2200 GOTO(out, rc = -EINVAL);
2208 static struct obd_ops mgc_obd_ops = {
2209 .o_owner = THIS_MODULE,
2210 .o_setup = mgc_setup,
2211 .o_precleanup = mgc_precleanup,
2212 .o_cleanup = mgc_cleanup,
2213 .o_add_conn = client_import_add_conn,
2214 .o_del_conn = client_import_del_conn,
2215 .o_connect = client_connect_import,
2216 .o_disconnect = client_disconnect_export,
2217 .o_set_info_async = mgc_set_info_async,
2218 .o_get_info = mgc_get_info,
2219 .o_import_event = mgc_import_event,
2220 .o_process_config = mgc_process_config,
2223 static int __init mgc_init(void)
2225 return class_register_type(&mgc_obd_ops, NULL, true, NULL,
2226 LUSTRE_MGC_NAME, NULL);
2229 static void __exit mgc_exit(void)
2231 class_unregister_type(LUSTRE_MGC_NAME);
2234 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2235 MODULE_DESCRIPTION("Lustre Management Client");
2236 MODULE_VERSION(LUSTRE_VERSION_STRING);
2237 MODULE_LICENSE("GPL");
2239 module_init(mgc_init);
2240 module_exit(mgc_exit);