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_swab.h>
53 #include <obd_class.h>
55 #include "mgc_internal.h"
57 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id,
62 if (len > sizeof(resname)) {
63 CERROR("name too long: %s\n", name);
67 CERROR("missing name: %s\n", name);
70 memcpy(&resname, name, len);
72 /* Always use the same endianness for the resid */
73 memset(res_id, 0, sizeof(*res_id));
74 res_id->name[0] = cpu_to_le64(resname);
75 /* XXX: unfortunately, sptlprc and config llog share one lock */
78 case CONFIG_T_SPTLRPC:
81 case CONFIG_T_RECOVER:
88 res_id->name[1] = cpu_to_le64(resname);
89 CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.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 static int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id,
108 /* logname consists of "fsname-nodetype".
109 * e.g. "lustre-MDT0001", "SUN-000-client"
110 * there is an exception: llog "params" */
111 name_end = strrchr(logname, '-');
113 len = strlen(logname);
115 len = name_end - logname;
116 return mgc_name2resid(logname, len, res_id, type);
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);
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)
139 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
140 atomic_read(&cld->cld_refcount));
141 LASSERT(atomic_read(&cld->cld_refcount) > 0);
143 /* spinlock to make sure no item with 0 refcount in the list */
144 if (atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
145 list_del(&cld->cld_list_chain);
146 spin_unlock(&config_list_lock);
148 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
150 if (cld->cld_recover)
151 config_log_put(cld->cld_recover);
152 if (cld->cld_sptlrpc)
153 config_log_put(cld->cld_sptlrpc);
155 config_log_put(cld->cld_params);
156 if (cld_is_sptlrpc(cld))
157 sptlrpc_conf_log_stop(cld->cld_logname);
159 class_export_put(cld->cld_mgcexp);
160 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
166 /* Find a config log by name */
168 struct config_llog_data *config_log_find(char *logname,
169 struct config_llog_instance *cfg)
171 struct config_llog_data *cld;
172 struct config_llog_data *found = NULL;
176 LASSERT(logname != NULL);
178 instance = cfg ? cfg->cfg_instance : NULL;
179 spin_lock(&config_list_lock);
180 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
181 /* check if instance equals */
182 if (instance != cld->cld_cfg.cfg_instance)
185 /* instance may be NULL, should check name */
186 if (strcmp(logname, cld->cld_logname) == 0) {
192 atomic_inc(&found->cld_refcount);
193 LASSERT(found->cld_stopping == 0 || cld_is_sptlrpc(found) == 0);
195 spin_unlock(&config_list_lock);
200 struct config_llog_data *do_config_log_add(struct obd_device *obd,
203 struct config_llog_instance *cfg,
204 struct super_block *sb)
206 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 strcpy(cld->cld_logname, logname);
221 cld->cld_cfg.cfg_callback = class_config_llog_handler;
222 mutex_init(&cld->cld_lock);
223 cld->cld_cfg.cfg_last_idx = 0;
224 cld->cld_cfg.cfg_flags = 0;
225 cld->cld_cfg.cfg_sb = sb;
226 cld->cld_type = type;
227 atomic_set(&cld->cld_refcount, 1);
229 /* Keep the mgc around until we are done */
230 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
232 if (cld_is_sptlrpc(cld)) {
233 sptlrpc_conf_log_start(logname);
234 cld->cld_cfg.cfg_obdname = obd->obd_name;
237 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
239 spin_lock(&config_list_lock);
240 list_add(&cld->cld_list_chain, &config_llog_list);
241 spin_unlock(&config_list_lock);
248 if (cld_is_sptlrpc(cld)) {
249 rc = mgc_process_log(obd, cld);
250 if (rc && rc != -ENOENT)
251 CERROR("failed processing sptlrpc log: %d\n", rc);
257 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
259 struct config_llog_instance *cfg,
260 struct super_block *sb)
262 struct config_llog_instance lcfg = *cfg;
263 struct lustre_sb_info *lsi = s2lsi(sb);
264 struct config_llog_data *cld;
270 /* for osp-on-ost, see lustre_start_osp() */
271 if (IS_MDT(lsi) && lcfg.cfg_instance)
274 /* we have to use different llog for clients and mdts for cmd
275 * where only clients are notified if one of cmd server restarts */
276 LASSERT(strlen(fsname) < sizeof(logname) / 2);
277 strcpy(logname, fsname);
278 if (IS_SERVER(lsi)) { /* mdt */
279 LASSERT(lcfg.cfg_instance == NULL);
280 lcfg.cfg_instance = sb;
281 strcat(logname, "-mdtir");
283 LASSERT(lcfg.cfg_instance != NULL);
284 strcat(logname, "-cliir");
287 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
291 static struct config_llog_data *config_params_log_add(struct obd_device *obd,
292 struct config_llog_instance *cfg, struct super_block *sb)
294 struct config_llog_instance lcfg = *cfg;
295 struct config_llog_data *cld;
297 lcfg.cfg_instance = sb;
299 cld = do_config_log_add(obd, PARAMS_FILENAME, CONFIG_T_PARAMS,
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 int config_log_add(struct obd_device *obd, char *logname,
311 struct config_llog_instance *cfg,
312 struct super_block *sb)
314 struct lustre_sb_info *lsi = s2lsi(sb);
315 struct config_llog_data *cld;
316 struct config_llog_data *sptlrpc_cld;
317 struct config_llog_data *params_cld;
323 CDEBUG(D_MGC, "adding config log %s:%p\n", logname, cfg->cfg_instance);
326 * for each regular log, the depended sptlrpc log name is
327 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
329 ptr = strrchr(logname, '-');
330 if (ptr == NULL || ptr - logname > 8) {
331 CERROR("logname %s is too long\n", logname);
335 memcpy(seclogname, logname, ptr - logname);
336 strcpy(seclogname + (ptr - logname), "-sptlrpc");
338 sptlrpc_cld = config_log_find(seclogname, NULL);
339 if (sptlrpc_cld == NULL) {
340 sptlrpc_cld = do_config_log_add(obd, seclogname,
341 CONFIG_T_SPTLRPC, NULL, NULL);
342 if (IS_ERR(sptlrpc_cld)) {
343 CERROR("can't create sptlrpc log: %s\n", seclogname);
344 GOTO(out_err, rc = PTR_ERR(sptlrpc_cld));
347 params_cld = config_params_log_add(obd, cfg, sb);
348 if (IS_ERR(params_cld)) {
349 rc = PTR_ERR(params_cld);
350 CERROR("%s: can't create params log: rc = %d\n",
355 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
357 CERROR("can't create log: %s\n", logname);
358 GOTO(out_err2, rc = PTR_ERR(cld));
361 cld->cld_sptlrpc = sptlrpc_cld;
362 cld->cld_params = params_cld;
364 LASSERT(lsi->lsi_lmd);
365 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)) {
366 struct config_llog_data *recover_cld;
367 ptr = strrchr(seclogname, '-');
372 CERROR("%s: sptlrpc log name not correct, %s: "
373 "rc = %d\n", obd->obd_name, seclogname, -EINVAL);
377 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
378 if (IS_ERR(recover_cld))
379 GOTO(out_err3, rc = PTR_ERR(recover_cld));
380 cld->cld_recover = recover_cld;
389 config_log_put(params_cld);
392 config_log_put(sptlrpc_cld);
398 DEFINE_MUTEX(llog_process_lock);
400 /** Stop watching for updates on this log.
402 static int config_log_end(char *logname, struct config_llog_instance *cfg)
404 struct config_llog_data *cld;
405 struct config_llog_data *cld_sptlrpc = NULL;
406 struct config_llog_data *cld_params = NULL;
407 struct config_llog_data *cld_recover = NULL;
411 cld = config_log_find(logname, cfg);
415 mutex_lock(&cld->cld_lock);
417 * if cld_stopping is set, it means we didn't start the log thus
418 * not owning the start ref. this can happen after previous umount:
419 * the cld still hanging there waiting for lock cancel, and we
420 * remount again but failed in the middle and call log_end without
423 if (unlikely(cld->cld_stopping)) {
424 mutex_unlock(&cld->cld_lock);
425 /* drop the ref from the find */
430 cld->cld_stopping = 1;
432 cld_recover = cld->cld_recover;
433 cld->cld_recover = NULL;
434 mutex_unlock(&cld->cld_lock);
437 mutex_lock(&cld_recover->cld_lock);
438 cld_recover->cld_stopping = 1;
439 mutex_unlock(&cld_recover->cld_lock);
440 config_log_put(cld_recover);
443 spin_lock(&config_list_lock);
444 cld_sptlrpc = cld->cld_sptlrpc;
445 cld->cld_sptlrpc = NULL;
446 cld_params = cld->cld_params;
447 cld->cld_params = NULL;
448 spin_unlock(&config_list_lock);
451 config_log_put(cld_sptlrpc);
454 mutex_lock(&cld_params->cld_lock);
455 cld_params->cld_stopping = 1;
456 mutex_unlock(&cld_params->cld_lock);
457 config_log_put(cld_params);
460 /* drop the ref from the find */
462 /* drop the start ref */
465 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
470 #ifdef CONFIG_PROC_FS
471 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
473 struct obd_device *obd = data;
474 struct obd_import *imp;
475 struct obd_connect_data *ocd;
476 struct config_llog_data *cld;
479 LASSERT(obd != NULL);
480 LPROCFS_CLIMP_CHECK(obd);
481 imp = obd->u.cli.cl_import;
482 ocd = &imp->imp_connect_data;
484 seq_printf(m, "imperative_recovery: %s\n",
485 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
486 seq_printf(m, "client_state:\n");
488 spin_lock(&config_list_lock);
489 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
490 if (cld->cld_recover == NULL)
492 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
494 cld->cld_recover->cld_cfg.cfg_last_idx);
496 spin_unlock(&config_list_lock);
498 LPROCFS_CLIMP_EXIT(obd);
503 /* reenqueue any lost locks */
504 #define RQ_RUNNING 0x1
508 #define RQ_PRECLEANUP 0x10
509 static int rq_state = 0;
510 static wait_queue_head_t rq_waitq;
511 static DECLARE_COMPLETION(rq_exit);
512 static DECLARE_COMPLETION(rq_start);
514 static void do_requeue(struct config_llog_data *cld)
519 LASSERT(atomic_read(&cld->cld_refcount) > 0);
521 /* Do not run mgc_process_log on a disconnected export or an
522 * export which is being disconnected. Take the client
523 * semaphore to make the check non-racy. */
524 down_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
525 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
526 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
527 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
528 if (rc && rc != -ENOENT)
529 CERROR("failed processing log: %d\n", rc);
531 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
534 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
539 /* this timeout represents how many seconds MGC should wait before
540 * requeue config and recover lock to the MGS. We need to randomize this
541 * in order to not flood the MGS.
543 #define MGC_TIMEOUT_MIN_SECONDS 5
544 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
546 static int mgc_requeue_thread(void *data)
552 CDEBUG(D_MGC, "Starting requeue thread\n");
554 /* Keep trying failed locks periodically */
555 spin_lock(&config_list_lock);
556 rq_state |= RQ_RUNNING;
558 struct l_wait_info lwi;
559 struct config_llog_data *cld, *cld_prev;
560 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
561 int stopped = !!(rq_state & RQ_STOP);
564 /* Any new or requeued lostlocks will change the state */
565 rq_state &= ~(RQ_NOW | RQ_LATER);
566 spin_unlock(&config_list_lock);
573 /* Always wait a few seconds to allow the server who
574 caused the lock revocation to finish its setup, plus some
575 random so everyone doesn't try to reconnect at once. */
576 to = msecs_to_jiffies(MGC_TIMEOUT_MIN_SECONDS * MSEC_PER_SEC);
577 /* rand is centi-seconds */
578 to += msecs_to_jiffies(rand * MSEC_PER_SEC / 100);
579 lwi = LWI_TIMEOUT(to, NULL, NULL);
580 l_wait_event(rq_waitq, rq_state & (RQ_STOP | RQ_PRECLEANUP),
584 * iterate & processing through the list. for each cld, process
585 * its depending sptlrpc cld firstly (if any) and then itself.
587 * it's guaranteed any item in the list must have
588 * reference > 0; and if cld_lostlock is set, at
589 * least one reference is taken by the previous enqueue.
593 spin_lock(&config_list_lock);
594 rq_state &= ~RQ_PRECLEANUP;
595 list_for_each_entry(cld, &config_llog_list,
597 if (!cld->cld_lostlock)
600 spin_unlock(&config_list_lock);
602 LASSERT(atomic_read(&cld->cld_refcount) > 0);
604 /* Whether we enqueued again or not in mgc_process_log,
605 * we're done with the ref from the old enqueue */
607 config_log_put(cld_prev);
610 cld->cld_lostlock = 0;
611 if (likely(!stopped))
614 spin_lock(&config_list_lock);
616 spin_unlock(&config_list_lock);
618 config_log_put(cld_prev);
620 /* break after scanning the list so that we can drop
621 * refcount to losing lock clds */
622 if (unlikely(stopped)) {
623 spin_lock(&config_list_lock);
627 /* Wait a bit to see if anyone else needs a requeue */
628 lwi = (struct l_wait_info) { 0 };
629 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
631 spin_lock(&config_list_lock);
633 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
634 rq_state &= ~RQ_RUNNING;
635 spin_unlock(&config_list_lock);
639 CDEBUG(D_MGC, "Ending requeue thread\n");
643 /* Add a cld to the list to requeue. Start the requeue thread if needed.
644 We are responsible for dropping the config log reference from here on out. */
645 static void mgc_requeue_add(struct config_llog_data *cld)
649 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
650 cld->cld_logname, atomic_read(&cld->cld_refcount),
651 cld->cld_stopping, rq_state);
652 LASSERT(atomic_read(&cld->cld_refcount) > 0);
654 mutex_lock(&cld->cld_lock);
655 if (cld->cld_stopping || cld->cld_lostlock) {
656 mutex_unlock(&cld->cld_lock);
659 /* this refcount will be released in mgc_requeue_thread. */
661 cld->cld_lostlock = 1;
662 mutex_unlock(&cld->cld_lock);
664 /* Hold lock for rq_state */
665 spin_lock(&config_list_lock);
666 if (rq_state & RQ_STOP) {
667 spin_unlock(&config_list_lock);
668 cld->cld_lostlock = 0;
672 spin_unlock(&config_list_lock);
678 /********************** class fns **********************/
679 static int mgc_local_llog_init(const struct lu_env *env,
680 struct obd_device *obd,
681 struct obd_device *disk)
683 struct llog_ctxt *ctxt;
688 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
693 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
695 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
701 static int mgc_local_llog_fini(const struct lu_env *env,
702 struct obd_device *obd)
704 struct llog_ctxt *ctxt;
708 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
709 llog_cleanup(env, ctxt);
714 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb)
716 struct lustre_sb_info *lsi = s2lsi(sb);
717 struct client_obd *cli = &obd->u.cli;
718 struct lu_fid rfid, fid;
719 struct dt_object *root, *dto;
726 LASSERT(lsi->lsi_dt_dev);
732 /* The mgc fs exclusion mutex. Only one fs can be setup at a time. */
733 mutex_lock(&cli->cl_mgc_mutex);
735 /* Setup the configs dir */
736 rc = lu_env_init(env, LCT_MG_THREAD);
740 fid.f_seq = FID_SEQ_LOCAL_NAME;
743 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
748 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
752 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
753 &cli->cl_mgc_los->los_dev->dd_lu_dev, NULL);
754 if (unlikely(IS_ERR(root)))
755 GOTO(out_los, rc = PTR_ERR(root));
757 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
759 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
760 lu_object_put_nocache(env, &root->do_lu);
762 GOTO(out_los, rc = PTR_ERR(dto));
764 cli->cl_mgc_configs_dir = dto;
766 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
767 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
771 /* We take an obd ref to insure that we can't get to mgc_cleanup
772 * without calling mgc_fs_cleanup first. */
773 class_incref(obd, "mgc_fs", obd);
775 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
779 lu_object_put(env, &cli->cl_mgc_configs_dir->do_lu);
780 cli->cl_mgc_configs_dir = NULL;
784 local_oid_storage_fini(env, cli->cl_mgc_los);
785 cli->cl_mgc_los = NULL;
786 mutex_unlock(&cli->cl_mgc_mutex);
795 static int mgc_fs_cleanup(struct obd_device *obd)
798 struct client_obd *cli = &obd->u.cli;
803 LASSERT(cli->cl_mgc_los != NULL);
805 rc = lu_env_init(&env, LCT_MG_THREAD);
809 mgc_local_llog_fini(&env, obd);
811 lu_object_put_nocache(&env, &cli->cl_mgc_configs_dir->do_lu);
812 cli->cl_mgc_configs_dir = NULL;
814 local_oid_storage_fini(&env, cli->cl_mgc_los);
815 cli->cl_mgc_los = NULL;
819 class_decref(obd, "mgc_fs", obd);
820 mutex_unlock(&cli->cl_mgc_mutex);
825 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
827 struct llog_ctxt *ctxt;
832 /* setup only remote ctxt, the local disk context is switched per each
833 * filesystem during mgc_fs_setup() */
834 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
839 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
842 llog_initiator_connect(ctxt);
848 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
850 struct llog_ctxt *ctxt;
854 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
856 llog_cleanup(env, ctxt);
862 static atomic_t mgc_count = ATOMIC_INIT(0);
863 static int mgc_precleanup(struct obd_device *obd)
869 if (atomic_dec_and_test(&mgc_count)) {
870 LASSERT(rq_state & RQ_RUNNING);
871 /* stop requeue thread */
874 /* wakeup requeue thread to clean our cld */
875 temp = RQ_NOW | RQ_PRECLEANUP;
878 spin_lock(&config_list_lock);
880 spin_unlock(&config_list_lock);
884 wait_for_completion(&rq_exit);
885 obd_cleanup_client_import(obd);
887 rc = mgc_llog_fini(NULL, obd);
889 CERROR("failed to cleanup llogging subsystems\n");
894 static int mgc_cleanup(struct obd_device *obd)
899 /* COMPAT_146 - old config logs may have added profiles we don't
901 if (obd->obd_type->typ_refcnt <= 1)
902 /* Only for the last mgc */
903 class_del_profiles();
905 lprocfs_obd_cleanup(obd);
908 rc = client_obd_cleanup(obd);
912 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
914 struct task_struct *task;
918 rc = ptlrpcd_addref();
922 rc = client_obd_setup(obd, lcfg);
924 GOTO(err_decref, rc);
926 rc = mgc_llog_init(NULL, obd);
928 CERROR("failed to setup llogging subsystems\n");
929 GOTO(err_cleanup, rc);
932 #ifdef CONFIG_PROC_FS
933 obd->obd_vars = lprocfs_mgc_obd_vars;
934 lprocfs_obd_setup(obd);
936 sptlrpc_lprocfs_cliobd_attach(obd);
938 if (atomic_inc_return(&mgc_count) == 1) {
940 init_waitqueue_head(&rq_waitq);
942 /* start requeue thread */
943 task = kthread_run(mgc_requeue_thread, NULL, "ll_cfg_requeue");
946 CERROR("%s: cannot start requeue thread: rc = %d; "
947 "no more log updates\n",
949 GOTO(err_cleanup, rc);
951 /* rc is the task_struct pointer of mgc_requeue_thread. */
953 wait_for_completion(&rq_start);
959 client_obd_cleanup(obd);
965 /* based on ll_mdc_blocking_ast */
966 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
967 void *data, int flag)
969 struct lustre_handle lockh;
970 struct config_llog_data *cld = (struct config_llog_data *)data;
975 case LDLM_CB_BLOCKING:
976 /* mgs wants the lock, give it up... */
977 LDLM_DEBUG(lock, "MGC blocking CB");
978 ldlm_lock2handle(lock, &lockh);
979 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
981 case LDLM_CB_CANCELING:
982 /* We've given up the lock, prepare ourselves to update. */
983 LDLM_DEBUG(lock, "MGC cancel CB");
985 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
986 PLDLMRES(lock->l_resource),
987 (char *)&lock->l_resource->lr_name.name[0]);
990 CDEBUG(D_INFO, "missing data, won't requeue\n");
994 /* held at mgc_process_log(). */
995 LASSERT(atomic_read(&cld->cld_refcount) > 0);
996 /* Are we done with this log? */
997 if (cld->cld_stopping) {
998 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
1000 config_log_put(cld);
1003 /* Make sure not to re-enqueue when the mgc is stopping
1004 (we get called from client_disconnect_export) */
1005 if (lock->l_conn_export == NULL ||
1006 lock->l_conn_export->exp_obd->u.cli.cl_conn_count == 0) {
1007 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
1009 config_log_put(cld);
1013 /* Re-enqueue now */
1014 mgc_requeue_add(cld);
1015 config_log_put(cld);
1024 /* Not sure where this should go... */
1025 /* This is the timeout value for MGS_CONNECT request plus a ping interval, such
1026 * that we can have a chance to try the secondary MGS if any. */
1027 #define MGC_ENQUEUE_LIMIT (INITIAL_CONNECT_TIMEOUT + (AT_OFF ? 0 : at_min) \
1029 #define MGC_TARGET_REG_LIMIT 10
1030 #define MGC_SEND_PARAM_LIMIT 10
1032 /* Send parameter to MGS*/
1033 static int mgc_set_mgs_param(struct obd_export *exp,
1034 struct mgs_send_param *msp)
1036 struct ptlrpc_request *req;
1037 struct mgs_send_param *req_msp, *rep_msp;
1041 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1042 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
1047 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1049 ptlrpc_req_finished(req);
1053 memcpy(req_msp, msp, sizeof(*req_msp));
1054 ptlrpc_request_set_replen(req);
1056 /* Limit how long we will wait for the enqueue to complete */
1057 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
1058 rc = ptlrpc_queue_wait(req);
1060 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1061 memcpy(msp, rep_msp, sizeof(*rep_msp));
1064 ptlrpc_req_finished(req);
1069 /* Take a config lock so we can get cancel notifications */
1070 static int mgc_enqueue(struct obd_export *exp, enum ldlm_type type,
1071 union ldlm_policy_data *policy, enum ldlm_mode mode,
1072 __u64 *flags, void *bl_cb, void *cp_cb, void *gl_cb,
1073 void *data, __u32 lvb_len, void *lvb_swabber,
1074 struct lustre_handle *lockh)
1076 struct config_llog_data *cld = (struct config_llog_data *)data;
1077 struct ldlm_enqueue_info einfo = {
1080 .ei_cb_bl = mgc_blocking_ast,
1081 .ei_cb_cp = ldlm_completion_ast,
1083 struct ptlrpc_request *req;
1084 int short_limit = cld_is_sptlrpc(cld);
1088 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
1089 cld->cld_resid.name[0]);
1091 /* We need a callback for every lockholder, so don't try to
1092 ldlm_lock_match (see rev 1.1.2.11.2.47) */
1093 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1094 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1099 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1100 ptlrpc_request_set_replen(req);
1102 /* check if this is server or client */
1103 if (cld->cld_cfg.cfg_sb) {
1104 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
1105 if (lsi && IS_SERVER(lsi))
1108 /* Limit how long we will wait for the enqueue to complete */
1109 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
1110 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1111 NULL, 0, LVB_T_NONE, lockh, 0);
1112 /* A failed enqueue should still call the mgc_blocking_ast,
1113 where it will be requeued if needed ("grant failed"). */
1114 ptlrpc_req_finished(req);
1118 static int mgc_cancel(struct obd_export *exp, enum ldlm_mode mode,
1119 struct lustre_handle *lockh)
1123 ldlm_lock_decref(lockh, mode);
1128 static void mgc_notify_active(struct obd_device *unused)
1130 /* wakeup mgc_requeue_thread to requeue mgc lock */
1131 spin_lock(&config_list_lock);
1133 spin_unlock(&config_list_lock);
1136 /* TODO: Help the MGS rebuild nidtbl. -jay */
1139 /* Send target_reg message to MGS */
1140 static int mgc_target_register(struct obd_export *exp,
1141 struct mgs_target_info *mti)
1143 struct ptlrpc_request *req;
1144 struct mgs_target_info *req_mti, *rep_mti;
1148 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1149 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1154 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1156 ptlrpc_req_finished(req);
1160 memcpy(req_mti, mti, sizeof(*req_mti));
1161 ptlrpc_request_set_replen(req);
1162 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1163 /* Limit how long we will wait for the enqueue to complete */
1164 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1166 rc = ptlrpc_queue_wait(req);
1168 rep_mti = req_capsule_server_get(&req->rq_pill,
1169 &RMF_MGS_TARGET_INFO);
1170 memcpy(mti, rep_mti, sizeof(*rep_mti));
1171 CDEBUG(D_MGC, "register %s got index = %d\n",
1172 mti->mti_svname, mti->mti_stripe_index);
1174 ptlrpc_req_finished(req);
1179 static int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1180 u32 keylen, void *key,
1181 u32 vallen, void *val,
1182 struct ptlrpc_request_set *set)
1187 /* Turn off initial_recov after we try all backup servers once */
1188 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1189 struct obd_import *imp = class_exp2cliimp(exp);
1191 if (vallen != sizeof(int))
1193 value = *(int *)val;
1194 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1195 imp->imp_obd->obd_name, value,
1196 imp->imp_deactive, imp->imp_invalid,
1197 imp->imp_replayable, imp->imp_obd->obd_replayable,
1198 ptlrpc_import_state_name(imp->imp_state));
1199 /* Resurrect if we previously died */
1200 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1201 imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1202 ptlrpc_reconnect_import(imp);
1205 /* FIXME move this to mgc_process_config */
1206 if (KEY_IS(KEY_REGISTER_TARGET)) {
1207 struct mgs_target_info *mti;
1208 if (vallen != sizeof(struct mgs_target_info))
1210 mti = (struct mgs_target_info *)val;
1211 CDEBUG(D_MGC, "register_target %s %#x\n",
1212 mti->mti_svname, mti->mti_flags);
1213 rc = mgc_target_register(exp, mti);
1216 if (KEY_IS(KEY_SET_FS)) {
1217 struct super_block *sb = (struct super_block *)val;
1219 if (vallen != sizeof(struct super_block))
1222 rc = mgc_fs_setup(exp->exp_obd, sb);
1225 if (KEY_IS(KEY_CLEAR_FS)) {
1228 rc = mgc_fs_cleanup(exp->exp_obd);
1231 if (KEY_IS(KEY_SET_INFO)) {
1232 struct mgs_send_param *msp;
1234 msp = (struct mgs_send_param *)val;
1235 rc = mgc_set_mgs_param(exp, msp);
1238 if (KEY_IS(KEY_MGSSEC)) {
1239 struct client_obd *cli = &exp->exp_obd->u.cli;
1240 struct sptlrpc_flavor flvr;
1243 * empty string means using current flavor, if which haven't
1244 * been set yet, set it as null.
1246 * if flavor has been set previously, check the asking flavor
1247 * must match the existing one.
1250 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1256 rc = sptlrpc_parse_flavor(val, &flvr);
1258 CERROR("invalid sptlrpc flavor %s to MGS\n",
1264 * caller already hold a mutex
1266 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1267 cli->cl_flvr_mgc = flvr;
1268 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1269 sizeof(flvr)) != 0) {
1272 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1274 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1275 "currently %s is in use\n",
1285 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1286 __u32 keylen, void *key, __u32 *vallen, void *val)
1290 if (KEY_IS(KEY_CONN_DATA)) {
1291 struct obd_import *imp = class_exp2cliimp(exp);
1292 struct obd_connect_data *data = val;
1294 if (*vallen == sizeof(*data)) {
1295 *data = imp->imp_connect_data;
1303 static int mgc_import_event(struct obd_device *obd,
1304 struct obd_import *imp,
1305 enum obd_import_event event)
1309 LASSERT(imp->imp_obd == obd);
1310 CDEBUG(D_MGC, "import event %#x\n", event);
1313 case IMP_EVENT_DISCON:
1314 /* MGC imports should not wait for recovery */
1315 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1316 ptlrpc_pinger_ir_down();
1318 case IMP_EVENT_INACTIVE:
1320 case IMP_EVENT_INVALIDATE: {
1321 struct ldlm_namespace *ns = obd->obd_namespace;
1322 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1325 case IMP_EVENT_ACTIVE:
1326 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1327 /* Clearing obd_no_recov allows us to continue pinging */
1328 obd->obd_no_recov = 0;
1329 mgc_notify_active(obd);
1330 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1331 ptlrpc_pinger_ir_up();
1335 case IMP_EVENT_DEACTIVATE:
1336 case IMP_EVENT_ACTIVATE:
1339 CERROR("Unknown import event %#x\n", event);
1346 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_CACHE_SHIFT),
1347 CONFIG_READ_NRPAGES = 4
1350 static int mgc_apply_recover_logs(struct obd_device *mgc,
1351 struct config_llog_data *cld,
1353 void *data, int datalen, bool mne_swab)
1355 struct config_llog_instance *cfg = &cld->cld_cfg;
1356 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1357 struct mgs_nidtbl_entry *entry;
1358 struct lustre_cfg *lcfg;
1359 struct lustre_cfg_bufs bufs;
1360 u64 prev_version = 0;
1369 LASSERT(cfg->cfg_instance != NULL);
1370 LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1372 OBD_ALLOC(inst, PAGE_CACHE_SIZE);
1376 if (!IS_SERVER(lsi)) {
1377 pos = snprintf(inst, PAGE_CACHE_SIZE, "%p", cfg->cfg_instance);
1378 if (pos >= PAGE_CACHE_SIZE) {
1379 OBD_FREE(inst, PAGE_CACHE_SIZE);
1383 LASSERT(IS_MDT(lsi));
1384 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1387 OBD_FREE(inst, PAGE_CACHE_SIZE);
1395 bufsz = PAGE_CACHE_SIZE - pos;
1397 while (datalen > 0) {
1398 int entry_len = sizeof(*entry);
1400 struct obd_device *obd;
1407 if (datalen < sizeof(*entry))
1410 entry = (typeof(entry))(data + off);
1413 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1415 if (entry->mne_nid_count == 0) /* at least one nid entry */
1417 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1420 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1421 if (datalen < entry_len) /* must have entry_len at least */
1424 /* Keep this swab for normal mixed endian handling. LU-1644 */
1426 lustre_swab_mgs_nidtbl_entry(entry);
1427 if (entry->mne_length > PAGE_CACHE_SIZE) {
1428 CERROR("MNE too large (%u)\n", entry->mne_length);
1432 if (entry->mne_length < entry_len)
1435 off += entry->mne_length;
1436 datalen -= entry->mne_length;
1440 if (entry->mne_version > max_version) {
1441 CERROR("entry index(%lld) is over max_index(%lld)\n",
1442 entry->mne_version, max_version);
1446 if (prev_version >= entry->mne_version) {
1447 CERROR("index unsorted, prev %lld, now %lld\n",
1448 prev_version, entry->mne_version);
1451 prev_version = entry->mne_version;
1454 * Write a string with format "nid::instance" to
1455 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1458 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1459 memset(buf, 0, bufsz);
1463 /* lustre-OST0001-osc-<instance #> */
1464 strcpy(obdname, cld->cld_logname);
1465 cname = strrchr(obdname, '-');
1466 if (cname == NULL) {
1467 CERROR("mgc %s: invalid logname %s\n",
1468 mgc->obd_name, obdname);
1472 pos = cname - obdname;
1474 pos += sprintf(obdname + pos, "-%s%04x",
1475 is_ost ? "OST" : "MDT", entry->mne_index);
1477 cname = is_ost ? "osc" : "mdc",
1478 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1479 lustre_cfg_bufs_reset(&bufs, obdname);
1481 /* find the obd by obdname */
1482 obd = class_name2obd(obdname);
1484 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1485 mgc->obd_name, obdname);
1487 /* this is a safe race, when the ost is starting up...*/
1491 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1494 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1497 down_read(&obd->u.cli.cl_sem);
1498 if (obd->u.cli.cl_import == NULL) {
1499 /* client does not connect to the OST yet */
1500 up_read(&obd->u.cli.cl_sem);
1505 /* iterate all nids to find one */
1506 /* find uuid by nid */
1508 for (i = 0; i < entry->mne_nid_count; i++) {
1509 rc = client_import_find_conn(obd->u.cli.cl_import,
1511 (struct obd_uuid *)uuid);
1516 up_read(&obd->u.cli.cl_sem);
1518 CERROR("mgc: cannot find uuid by nid %s\n",
1519 libcfs_nid2str(entry->u.nids[0]));
1523 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1524 uuid, libcfs_nid2str(entry->u.nids[0]));
1526 pos += strlen(uuid);
1527 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1528 LASSERT(pos < bufsz);
1530 lustre_cfg_bufs_set_string(&bufs, 1, params);
1532 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1538 CDEBUG(D_INFO, "ir apply logs "LPD64"/"LPD64" for %s -> %s\n",
1539 prev_version, max_version, obdname, params);
1541 rc = class_process_config(lcfg);
1542 lustre_cfg_free(lcfg);
1544 CDEBUG(D_INFO, "process config for %s error %d\n",
1547 /* continue, even one with error */
1550 OBD_FREE(inst, PAGE_CACHE_SIZE);
1555 * This function is called if this client was notified for target restarting
1556 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery logs.
1558 static int mgc_process_recover_log(struct obd_device *obd,
1559 struct config_llog_data *cld)
1561 struct ptlrpc_request *req = NULL;
1562 struct config_llog_instance *cfg = &cld->cld_cfg;
1563 struct mgs_config_body *body;
1564 struct mgs_config_res *res;
1565 struct ptlrpc_bulk_desc *desc;
1566 struct page **pages;
1569 bool mne_swab = false;
1575 /* allocate buffer for bulk transfer.
1576 * if this is the first time for this mgs to read logs,
1577 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1578 * once; otherwise, it only reads increment of logs, this should be
1579 * small and CONFIG_READ_NRPAGES will be used.
1581 nrpages = CONFIG_READ_NRPAGES;
1582 if (cfg->cfg_last_idx == 0) /* the first time */
1583 nrpages = CONFIG_READ_NRPAGES_INIT;
1585 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1587 GOTO(out, rc = -ENOMEM);
1589 for (i = 0; i < nrpages; i++) {
1590 pages[i] = alloc_page(GFP_IOFS);
1591 if (pages[i] == NULL)
1592 GOTO(out, rc = -ENOMEM);
1596 LASSERT(cld_is_recover(cld));
1597 LASSERT(mutex_is_locked(&cld->cld_lock));
1598 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1599 &RQF_MGS_CONFIG_READ);
1601 GOTO(out, rc = -ENOMEM);
1603 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1608 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1609 LASSERT(body != NULL);
1610 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1611 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1612 >= sizeof(body->mcb_name))
1613 GOTO(out, rc = -E2BIG);
1614 body->mcb_offset = cfg->cfg_last_idx + 1;
1615 body->mcb_type = cld->cld_type;
1616 body->mcb_bits = PAGE_CACHE_SHIFT;
1617 body->mcb_units = nrpages;
1619 /* allocate bulk transfer descriptor */
1620 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1,
1621 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1623 &ptlrpc_bulk_kiov_pin_ops);
1625 GOTO(out, rc = -ENOMEM);
1627 for (i = 0; i < nrpages; i++)
1628 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1631 ptlrpc_request_set_replen(req);
1632 rc = ptlrpc_queue_wait(req);
1636 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1637 if (res->mcr_size < res->mcr_offset)
1638 GOTO(out, rc = -EINVAL);
1640 /* always update the index even though it might have errors with
1641 * handling the recover logs */
1642 cfg->cfg_last_idx = res->mcr_offset;
1643 eof = res->mcr_offset == res->mcr_size;
1645 CDEBUG(D_INFO, "Latest version "LPD64", more %d.\n",
1646 res->mcr_offset, eof == false);
1648 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1650 GOTO(out, rc = ealen);
1652 if (ealen > nrpages << PAGE_CACHE_SHIFT)
1653 GOTO(out, rc = -EINVAL);
1655 if (ealen == 0) { /* no logs transferred */
1661 mne_swab = !!ptlrpc_rep_need_swab(req);
1662 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
1663 /* This import flag means the server did an extra swab of IR MNE
1664 * records (fixed in LU-1252), reverse it here if needed. LU-1644 */
1665 if (unlikely(req->rq_import->imp_need_mne_swab))
1666 mne_swab = !mne_swab;
1669 for (i = 0; i < nrpages && ealen > 0; i++) {
1673 ptr = kmap(pages[i]);
1674 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1675 min_t(int, ealen, PAGE_CACHE_SIZE),
1679 CWARN("Process recover log %s error %d\n",
1680 cld->cld_logname, rc2);
1684 ealen -= PAGE_CACHE_SIZE;
1689 ptlrpc_req_finished(req);
1691 if (rc == 0 && !eof)
1695 for (i = 0; i < nrpages; i++) {
1696 if (pages[i] == NULL)
1698 __free_page(pages[i]);
1700 OBD_FREE(pages, sizeof(*pages) * nrpages);
1705 /* Copy a remote log locally */
1706 static int mgc_llog_local_copy(const struct lu_env *env,
1707 struct obd_device *obd,
1708 struct llog_ctxt *rctxt,
1709 struct llog_ctxt *lctxt, char *logname)
1717 * - copy it to backup using llog_backup()
1718 * - copy remote llog to logname using llog_backup()
1719 * - if failed then move bakup to logname again
1722 OBD_ALLOC(temp_log, strlen(logname) + 2);
1725 sprintf(temp_log, "%sT", logname);
1727 /* make a copy of local llog at first */
1728 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1729 if (rc < 0 && rc != -ENOENT)
1731 /* copy remote llog to the local copy */
1732 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1733 if (rc == -ENOENT) {
1734 /* no remote llog, delete local one too */
1735 llog_erase(env, lctxt, NULL, logname);
1736 } else if (rc < 0) {
1737 /* error during backup, get local one back from the copy */
1738 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1740 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1741 obd->obd_name, logname, rc);
1743 llog_erase(env, lctxt, NULL, temp_log);
1744 OBD_FREE(temp_log, strlen(logname) + 2);
1748 /* local_only means it cannot get remote llogs */
1749 static int mgc_process_cfg_log(struct obd_device *mgc,
1750 struct config_llog_data *cld, int local_only)
1752 struct llog_ctxt *ctxt, *lctxt = NULL;
1753 struct client_obd *cli = &mgc->u.cli;
1754 struct lustre_sb_info *lsi = NULL;
1756 bool sptlrpc_started = false;
1762 LASSERT(mutex_is_locked(&cld->cld_lock));
1765 * local copy of sptlrpc log is controlled elsewhere, don't try to
1768 if (cld_is_sptlrpc(cld) && local_only)
1771 if (cld->cld_cfg.cfg_sb)
1772 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1778 rc = lu_env_init(env, LCT_MG_THREAD);
1782 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1785 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1787 /* Copy the setup log locally if we can. Don't mess around if we're
1788 * running an MGS though (logs are already local). */
1789 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1790 cli->cl_mgc_configs_dir != NULL &&
1791 lu2dt_dev(cli->cl_mgc_configs_dir->do_lu.lo_dev) ==
1794 /* Only try to copy log if we have the lock. */
1795 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1797 if (local_only || rc) {
1798 if (strcmp(cld->cld_logname, PARAMS_FILENAME) != 0 &&
1799 llog_is_empty(env, lctxt, cld->cld_logname)) {
1800 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1801 "log %s and no local copy."
1802 "\n", cld->cld_logname);
1803 GOTO(out_pop, rc = -ENOENT);
1805 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1806 "copy for now, will try to update later.\n",
1810 /* Now, whether we copied or not, start using the local llog.
1811 * If we failed to copy, we'll start using whatever the old
1813 llog_ctxt_put(ctxt);
1817 if (local_only) /* no local log at client side */
1818 GOTO(out_pop, rc = -EIO);
1821 if (cld_is_sptlrpc(cld)) {
1822 sptlrpc_conf_log_update_begin(cld->cld_logname);
1823 sptlrpc_started = true;
1826 /* logname and instance info should be the same, so use our
1827 * copy of the instance for the update. The cfg_last_idx will
1828 * be updated here. */
1829 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1834 __llog_ctxt_put(env, ctxt);
1836 __llog_ctxt_put(env, lctxt);
1839 * update settings on existing OBDs. doing it inside
1840 * of llog_process_lock so no device is attaching/detaching
1842 * the logname must be <fsname>-sptlrpc
1844 if (sptlrpc_started) {
1845 LASSERT(cld_is_sptlrpc(cld));
1846 sptlrpc_conf_log_update_end(cld->cld_logname);
1847 class_notify_sptlrpc_conf(cld->cld_logname,
1848 strlen(cld->cld_logname) -
1849 strlen("-sptlrpc"));
1858 static bool mgc_import_in_recovery(struct obd_import *imp)
1860 bool in_recovery = true;
1862 spin_lock(&imp->imp_lock);
1863 if (imp->imp_state == LUSTRE_IMP_FULL ||
1864 imp->imp_state == LUSTRE_IMP_CLOSED)
1865 in_recovery = false;
1866 spin_unlock(&imp->imp_lock);
1872 * Get a configuration log from the MGS and process it.
1874 * This function is called for both clients and servers to process the
1875 * configuration log from the MGS. The MGC enqueues a DLM lock on the
1876 * log from the MGS, and if the lock gets revoked the MGC will be notified
1877 * by the lock cancellation callback that the config log has changed,
1878 * and will enqueue another MGS lock on it, and then continue processing
1879 * the new additions to the end of the log.
1881 * Since the MGC import is not replayable, if the import is being evicted
1882 * (rcl == -ESHUTDOWN, \see ptlrpc_import_delay_req()), retry to process
1883 * the log until recovery is finished or the import is closed.
1885 * Make a local copy of the log before parsing it if appropriate (non-MGS
1886 * server) so that the server can start even when the MGS is down.
1888 * There shouldn't be multiple processes running process_log at once --
1889 * sounds like badness. It actually might be fine, as long as they're not
1890 * trying to update from the same log simultaneously, in which case we
1891 * should use a per-log semaphore instead of cld_lock.
1893 * \param[in] mgc MGC device by which to fetch the configuration log
1894 * \param[in] cld log processing state (stored in lock callback data)
1896 * \retval 0 on success
1897 * \retval negative errno on failure
1899 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1901 struct lustre_handle lockh = { 0 };
1902 __u64 flags = LDLM_FL_NO_LRU;
1907 LASSERT(cld != NULL);
1909 /* I don't want multiple processes running process_log at once --
1910 sounds like badness. It actually might be fine, as long as
1911 we're not trying to update from the same log
1912 simultaneously (in which case we should use a per-log sem.) */
1914 mutex_lock(&cld->cld_lock);
1915 if (cld->cld_stopping) {
1916 mutex_unlock(&cld->cld_lock);
1920 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1922 CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
1923 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1925 /* Get the cfg lock on the llog */
1926 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, LDLM_PLAIN, NULL,
1927 LCK_CR, &flags, NULL, NULL, NULL,
1928 cld, 0, NULL, &lockh);
1930 /* Get the cld, it will be released in mgc_blocking_ast. */
1931 config_log_get(cld);
1932 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1935 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1937 if (rcl == -ESHUTDOWN &&
1938 atomic_read(&mgc->u.cli.cl_mgc_refcount) > 0 && !retry) {
1939 struct obd_import *imp;
1940 struct l_wait_info lwi;
1941 int secs = cfs_time_seconds(obd_timeout);
1943 mutex_unlock(&cld->cld_lock);
1944 imp = class_exp2cliimp(mgc->u.cli.cl_mgc_mgsexp);
1946 /* Let's force the pinger, and wait the import to be
1947 * connected, note: since mgc import is non-replayable,
1948 * and even the import state is disconnected, it does
1949 * not mean the "recovery" is stopped, so we will keep
1950 * waitting until timeout or the import state is
1952 ptlrpc_pinger_force(imp);
1954 lwi = LWI_TIMEOUT(secs, NULL, NULL);
1955 l_wait_event(imp->imp_recovery_waitq,
1956 !mgc_import_in_recovery(imp), &lwi);
1958 if (imp->imp_state == LUSTRE_IMP_FULL) {
1962 mutex_lock(&cld->cld_lock);
1963 cld->cld_lostlock = 1;
1966 /* mark cld_lostlock so that it will requeue
1967 * after MGC becomes available. */
1968 cld->cld_lostlock = 1;
1970 /* Get extra reference, it will be put in requeue thread */
1971 config_log_get(cld);
1975 if (cld_is_recover(cld)) {
1976 rc = 0; /* this is not a fatal error for recover log */
1978 rc = mgc_process_recover_log(mgc, cld);
1980 CERROR("%s: recover log %s failed: rc = %d"
1981 "not fatal.\n", mgc->obd_name,
1982 cld->cld_logname, rc);
1984 cld->cld_lostlock = 1;
1988 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1991 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1992 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1994 mutex_unlock(&cld->cld_lock);
1996 /* Now drop the lock so MGS can revoke it */
1998 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, LCK_CR, &lockh);
2000 CERROR("Can't drop cfg lock: %d\n", rcl);
2007 /** Called from lustre_process_log.
2008 * LCFG_LOG_START gets the config log from the MGS, processes it to start
2009 * any services, and adds it to the list logs to watch (follow).
2011 static int mgc_process_config(struct obd_device *obd, size_t len, void *buf)
2013 struct lustre_cfg *lcfg = buf;
2014 struct config_llog_instance *cfg = NULL;
2019 switch(lcfg->lcfg_command) {
2020 case LCFG_LOV_ADD_OBD: {
2021 /* Overloading this cfg command: register a new target */
2022 struct mgs_target_info *mti;
2024 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
2025 sizeof(struct mgs_target_info))
2026 GOTO(out, rc = -EINVAL);
2028 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
2029 CDEBUG(D_MGC, "add_target %s %#x\n",
2030 mti->mti_svname, mti->mti_flags);
2031 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
2034 case LCFG_LOV_DEL_OBD:
2035 /* Unregister has no meaning at the moment. */
2036 CERROR("lov_del_obd unimplemented\n");
2039 case LCFG_SPTLRPC_CONF: {
2040 rc = sptlrpc_process_config(lcfg);
2043 case LCFG_LOG_START: {
2044 struct config_llog_data *cld;
2045 struct super_block *sb;
2047 logname = lustre_cfg_string(lcfg, 1);
2048 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
2049 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
2051 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
2054 /* We're only called through here on the initial mount */
2055 rc = config_log_add(obd, logname, cfg, sb);
2058 cld = config_log_find(logname, cfg);
2065 /* FIXME only set this for old logs! Right now this forces
2066 us to always skip the "inside markers" check */
2067 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
2069 rc = mgc_process_log(obd, cld);
2070 if (rc == 0 && cld->cld_recover != NULL) {
2071 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
2072 imp_connect_data, IMP_RECOV)) {
2073 rc = mgc_process_log(obd, cld->cld_recover);
2075 struct config_llog_data *cir = cld->cld_recover;
2076 cld->cld_recover = NULL;
2077 config_log_put(cir);
2080 CERROR("Cannot process recover llog %d\n", rc);
2083 if (rc == 0 && cld->cld_params != NULL) {
2084 rc = mgc_process_log(obd, cld->cld_params);
2085 if (rc == -ENOENT) {
2086 CDEBUG(D_MGC, "There is no params "
2087 "config file yet\n");
2090 /* params log is optional */
2092 CERROR("%s: can't process params llog: rc = %d\n",
2095 config_log_put(cld);
2099 case LCFG_LOG_END: {
2100 logname = lustre_cfg_string(lcfg, 1);
2102 if (lcfg->lcfg_bufcount >= 2)
2103 cfg = (struct config_llog_instance *)lustre_cfg_buf(
2105 rc = config_log_end(logname, cfg);
2109 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
2110 GOTO(out, rc = -EINVAL);
2118 static struct obd_ops mgc_obd_ops = {
2119 .o_owner = THIS_MODULE,
2120 .o_setup = mgc_setup,
2121 .o_precleanup = mgc_precleanup,
2122 .o_cleanup = mgc_cleanup,
2123 .o_add_conn = client_import_add_conn,
2124 .o_del_conn = client_import_del_conn,
2125 .o_connect = client_connect_import,
2126 .o_disconnect = client_disconnect_export,
2127 .o_set_info_async = mgc_set_info_async,
2128 .o_get_info = mgc_get_info,
2129 .o_import_event = mgc_import_event,
2130 .o_process_config = mgc_process_config,
2133 static int __init mgc_init(void)
2135 return class_register_type(&mgc_obd_ops, NULL, true, NULL,
2136 LUSTRE_MGC_NAME, NULL);
2139 static void __exit mgc_exit(void)
2141 class_unregister_type(LUSTRE_MGC_NAME);
2144 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2145 MODULE_DESCRIPTION("Lustre Management Client");
2146 MODULE_VERSION(LUSTRE_VERSION_STRING);
2147 MODULE_LICENSE("GPL");
2149 module_init(mgc_init);
2150 module_exit(mgc_exit);