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, 2014, 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>
46 #include <obd_class.h>
47 #include <lustre_dlm.h>
48 #include <lprocfs_status.h>
49 #include <lustre_log.h>
50 #include <lustre_disk.h>
51 #include <dt_object.h>
53 #include "mgc_internal.h"
55 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id,
60 if (len > sizeof(resname)) {
61 CERROR("name too long: %s\n", name);
65 CERROR("missing name: %s\n", name);
68 memcpy(&resname, name, len);
70 /* Always use the same endianness for the resid */
71 memset(res_id, 0, sizeof(*res_id));
72 res_id->name[0] = cpu_to_le64(resname);
73 /* XXX: unfortunately, sptlprc and config llog share one lock */
76 case CONFIG_T_SPTLRPC:
79 case CONFIG_T_RECOVER:
86 res_id->name[1] = cpu_to_le64(resname);
87 CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.8s)\n", name,
88 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
92 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id, int type)
94 /* fsname is at most 8 chars long, maybe contain "-".
95 * e.g. "lustre", "SUN-000" */
96 return mgc_name2resid(fsname, strlen(fsname), res_id, type);
98 EXPORT_SYMBOL(mgc_fsname2resid);
100 static int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id,
106 /* logname consists of "fsname-nodetype".
107 * e.g. "lustre-MDT0001", "SUN-000-client"
108 * there is an exception: llog "params" */
109 name_end = strrchr(logname, '-');
111 len = strlen(logname);
113 len = name_end - logname;
114 return mgc_name2resid(logname, len, res_id, type);
117 /********************** config llog list **********************/
118 static struct list_head config_llog_list = LIST_HEAD_INIT(config_llog_list);
119 static DEFINE_SPINLOCK(config_list_lock);
121 /* Take a reference to a config log */
122 static int config_log_get(struct config_llog_data *cld)
125 atomic_inc(&cld->cld_refcount);
126 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
127 atomic_read(&cld->cld_refcount));
131 /* Drop a reference to a config log. When no longer referenced,
132 we can free the config log data */
133 static void config_log_put(struct config_llog_data *cld)
137 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
138 atomic_read(&cld->cld_refcount));
139 LASSERT(atomic_read(&cld->cld_refcount) > 0);
141 /* spinlock to make sure no item with 0 refcount in the list */
142 if (atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
143 list_del(&cld->cld_list_chain);
144 spin_unlock(&config_list_lock);
146 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
148 if (cld->cld_recover)
149 config_log_put(cld->cld_recover);
150 if (cld->cld_sptlrpc)
151 config_log_put(cld->cld_sptlrpc);
153 config_log_put(cld->cld_params);
154 if (cld_is_sptlrpc(cld))
155 sptlrpc_conf_log_stop(cld->cld_logname);
157 class_export_put(cld->cld_mgcexp);
158 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
164 /* Find a config log by name */
166 struct config_llog_data *config_log_find(char *logname,
167 struct config_llog_instance *cfg)
169 struct config_llog_data *cld;
170 struct config_llog_data *found = NULL;
174 LASSERT(logname != NULL);
176 instance = cfg ? cfg->cfg_instance : NULL;
177 spin_lock(&config_list_lock);
178 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
179 /* check if instance equals */
180 if (instance != cld->cld_cfg.cfg_instance)
183 /* instance may be NULL, should check name */
184 if (strcmp(logname, cld->cld_logname) == 0) {
190 atomic_inc(&found->cld_refcount);
191 LASSERT(found->cld_stopping == 0 || cld_is_sptlrpc(found) == 0);
193 spin_unlock(&config_list_lock);
198 struct config_llog_data *do_config_log_add(struct obd_device *obd,
201 struct config_llog_instance *cfg,
202 struct super_block *sb)
204 struct config_llog_data *cld;
208 CDEBUG(D_MGC, "do adding config log %s:%p\n", logname,
209 cfg ? cfg->cfg_instance : NULL);
211 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
213 RETURN(ERR_PTR(-ENOMEM));
215 strcpy(cld->cld_logname, logname);
219 cld->cld_cfg.cfg_callback = class_config_llog_handler;
220 mutex_init(&cld->cld_lock);
221 cld->cld_cfg.cfg_last_idx = 0;
222 cld->cld_cfg.cfg_flags = 0;
223 cld->cld_cfg.cfg_sb = sb;
224 cld->cld_type = type;
225 atomic_set(&cld->cld_refcount, 1);
227 /* Keep the mgc around until we are done */
228 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
230 if (cld_is_sptlrpc(cld)) {
231 sptlrpc_conf_log_start(logname);
232 cld->cld_cfg.cfg_obdname = obd->obd_name;
235 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
237 spin_lock(&config_list_lock);
238 list_add(&cld->cld_list_chain, &config_llog_list);
239 spin_unlock(&config_list_lock);
246 if (cld_is_sptlrpc(cld)) {
247 rc = mgc_process_log(obd, cld);
248 if (rc && rc != -ENOENT)
249 CERROR("failed processing sptlrpc log: %d\n", rc);
255 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
257 struct config_llog_instance *cfg,
258 struct super_block *sb)
260 struct config_llog_instance lcfg = *cfg;
261 struct lustre_sb_info *lsi = s2lsi(sb);
262 struct config_llog_data *cld;
268 /* for osp-on-ost, see lustre_start_osp() */
269 if (IS_MDT(lsi) && lcfg.cfg_instance)
272 /* we have to use different llog for clients and mdts for cmd
273 * where only clients are notified if one of cmd server restarts */
274 LASSERT(strlen(fsname) < sizeof(logname) / 2);
275 strcpy(logname, fsname);
276 if (IS_SERVER(lsi)) { /* mdt */
277 LASSERT(lcfg.cfg_instance == NULL);
278 lcfg.cfg_instance = sb;
279 strcat(logname, "-mdtir");
281 LASSERT(lcfg.cfg_instance != NULL);
282 strcat(logname, "-cliir");
285 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
289 static struct config_llog_data *config_params_log_add(struct obd_device *obd,
290 struct config_llog_instance *cfg, struct super_block *sb)
292 struct config_llog_instance lcfg = *cfg;
293 struct config_llog_data *cld;
295 lcfg.cfg_instance = sb;
297 cld = do_config_log_add(obd, PARAMS_FILENAME, CONFIG_T_PARAMS,
303 /** Add this log to the list of active logs watched by an MGC.
304 * Active means we're watching for updates.
305 * We have one active log per "mount" - client instance or servername.
306 * Each instance may be at a different point in the log.
308 static int config_log_add(struct obd_device *obd, char *logname,
309 struct config_llog_instance *cfg,
310 struct super_block *sb)
312 struct lustre_sb_info *lsi = s2lsi(sb);
313 struct config_llog_data *cld;
314 struct config_llog_data *sptlrpc_cld;
315 struct config_llog_data *params_cld;
321 CDEBUG(D_MGC, "adding config log %s:%p\n", logname, cfg->cfg_instance);
324 * for each regular log, the depended sptlrpc log name is
325 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
327 ptr = strrchr(logname, '-');
328 if (ptr == NULL || ptr - logname > 8) {
329 CERROR("logname %s is too long\n", logname);
333 memcpy(seclogname, logname, ptr - logname);
334 strcpy(seclogname + (ptr - logname), "-sptlrpc");
336 sptlrpc_cld = config_log_find(seclogname, NULL);
337 if (sptlrpc_cld == NULL) {
338 sptlrpc_cld = do_config_log_add(obd, seclogname,
339 CONFIG_T_SPTLRPC, NULL, NULL);
340 if (IS_ERR(sptlrpc_cld)) {
341 CERROR("can't create sptlrpc log: %s\n", seclogname);
342 GOTO(out_err, rc = PTR_ERR(sptlrpc_cld));
345 params_cld = config_params_log_add(obd, cfg, sb);
346 if (IS_ERR(params_cld)) {
347 rc = PTR_ERR(params_cld);
348 CERROR("%s: can't create params log: rc = %d\n",
353 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
355 CERROR("can't create log: %s\n", logname);
356 GOTO(out_err2, rc = PTR_ERR(cld));
359 cld->cld_sptlrpc = sptlrpc_cld;
360 cld->cld_params = params_cld;
362 LASSERT(lsi->lsi_lmd);
363 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)) {
364 struct config_llog_data *recover_cld;
365 ptr = strrchr(seclogname, '-');
370 CERROR("%s: sptlrpc log name not correct, %s: "
371 "rc = %d\n", obd->obd_name, seclogname, -EINVAL);
375 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
376 if (IS_ERR(recover_cld))
377 GOTO(out_err3, rc = PTR_ERR(recover_cld));
378 cld->cld_recover = recover_cld;
387 config_log_put(params_cld);
390 config_log_put(sptlrpc_cld);
396 DEFINE_MUTEX(llog_process_lock);
398 /** Stop watching for updates on this log.
400 static int config_log_end(char *logname, struct config_llog_instance *cfg)
402 struct config_llog_data *cld;
403 struct config_llog_data *cld_sptlrpc = NULL;
404 struct config_llog_data *cld_params = NULL;
405 struct config_llog_data *cld_recover = NULL;
409 cld = config_log_find(logname, cfg);
413 mutex_lock(&cld->cld_lock);
415 * if cld_stopping is set, it means we didn't start the log thus
416 * not owning the start ref. this can happen after previous umount:
417 * the cld still hanging there waiting for lock cancel, and we
418 * remount again but failed in the middle and call log_end without
421 if (unlikely(cld->cld_stopping)) {
422 mutex_unlock(&cld->cld_lock);
423 /* drop the ref from the find */
428 cld->cld_stopping = 1;
430 cld_recover = cld->cld_recover;
431 cld->cld_recover = NULL;
432 mutex_unlock(&cld->cld_lock);
435 mutex_lock(&cld_recover->cld_lock);
436 cld_recover->cld_stopping = 1;
437 mutex_unlock(&cld_recover->cld_lock);
438 config_log_put(cld_recover);
441 spin_lock(&config_list_lock);
442 cld_sptlrpc = cld->cld_sptlrpc;
443 cld->cld_sptlrpc = NULL;
444 cld_params = cld->cld_params;
445 cld->cld_params = NULL;
446 spin_unlock(&config_list_lock);
449 config_log_put(cld_sptlrpc);
452 mutex_lock(&cld_params->cld_lock);
453 cld_params->cld_stopping = 1;
454 mutex_unlock(&cld_params->cld_lock);
455 config_log_put(cld_params);
458 /* drop the ref from the find */
460 /* drop the start ref */
463 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
468 #ifdef CONFIG_PROC_FS
469 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
471 struct obd_device *obd = data;
472 struct obd_import *imp;
473 struct obd_connect_data *ocd;
474 struct config_llog_data *cld;
477 LASSERT(obd != NULL);
478 LPROCFS_CLIMP_CHECK(obd);
479 imp = obd->u.cli.cl_import;
480 ocd = &imp->imp_connect_data;
482 seq_printf(m, "imperative_recovery: %s\n",
483 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
484 seq_printf(m, "client_state:\n");
486 spin_lock(&config_list_lock);
487 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
488 if (cld->cld_recover == NULL)
490 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
492 cld->cld_recover->cld_cfg.cfg_last_idx);
494 spin_unlock(&config_list_lock);
496 LPROCFS_CLIMP_EXIT(obd);
501 /* reenqueue any lost locks */
502 #define RQ_RUNNING 0x1
506 #define RQ_PRECLEANUP 0x10
507 static int rq_state = 0;
508 static wait_queue_head_t rq_waitq;
509 static DECLARE_COMPLETION(rq_exit);
510 static DECLARE_COMPLETION(rq_start);
512 static void do_requeue(struct config_llog_data *cld)
517 LASSERT(atomic_read(&cld->cld_refcount) > 0);
519 /* Do not run mgc_process_log on a disconnected export or an
520 * export which is being disconnected. Take the client
521 * semaphore to make the check non-racy. */
522 down_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
523 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
524 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
525 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
526 if (rc && rc != -ENOENT)
527 CERROR("failed processing log: %d\n", rc);
529 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
532 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
537 /* this timeout represents how many seconds MGC should wait before
538 * requeue config and recover lock to the MGS. We need to randomize this
539 * in order to not flood the MGS.
541 #define MGC_TIMEOUT_MIN_SECONDS 5
542 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
544 static int mgc_requeue_thread(void *data)
550 CDEBUG(D_MGC, "Starting requeue thread\n");
552 /* Keep trying failed locks periodically */
553 spin_lock(&config_list_lock);
554 rq_state |= RQ_RUNNING;
556 struct l_wait_info lwi;
557 struct config_llog_data *cld, *cld_prev;
558 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
559 int stopped = !!(rq_state & RQ_STOP);
562 /* Any new or requeued lostlocks will change the state */
563 rq_state &= ~(RQ_NOW | RQ_LATER);
564 spin_unlock(&config_list_lock);
571 /* Always wait a few seconds to allow the server who
572 caused the lock revocation to finish its setup, plus some
573 random so everyone doesn't try to reconnect at once. */
574 to = msecs_to_jiffies(MGC_TIMEOUT_MIN_SECONDS * MSEC_PER_SEC);
575 /* rand is centi-seconds */
576 to += msecs_to_jiffies(rand * MSEC_PER_SEC / 100);
577 lwi = LWI_TIMEOUT(to, NULL, NULL);
578 l_wait_event(rq_waitq, rq_state & (RQ_STOP | RQ_PRECLEANUP),
582 * iterate & processing through the list. for each cld, process
583 * its depending sptlrpc cld firstly (if any) and then itself.
585 * it's guaranteed any item in the list must have
586 * reference > 0; and if cld_lostlock is set, at
587 * least one reference is taken by the previous enqueue.
591 spin_lock(&config_list_lock);
592 rq_state &= ~RQ_PRECLEANUP;
593 list_for_each_entry(cld, &config_llog_list,
595 if (!cld->cld_lostlock)
598 spin_unlock(&config_list_lock);
600 LASSERT(atomic_read(&cld->cld_refcount) > 0);
602 /* Whether we enqueued again or not in mgc_process_log,
603 * we're done with the ref from the old enqueue */
605 config_log_put(cld_prev);
608 cld->cld_lostlock = 0;
609 if (likely(!stopped))
612 spin_lock(&config_list_lock);
614 spin_unlock(&config_list_lock);
616 config_log_put(cld_prev);
618 /* break after scanning the list so that we can drop
619 * refcount to losing lock clds */
620 if (unlikely(stopped)) {
621 spin_lock(&config_list_lock);
625 /* Wait a bit to see if anyone else needs a requeue */
626 lwi = (struct l_wait_info) { 0 };
627 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
629 spin_lock(&config_list_lock);
631 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
632 rq_state &= ~RQ_RUNNING;
633 spin_unlock(&config_list_lock);
637 CDEBUG(D_MGC, "Ending requeue thread\n");
641 /* Add a cld to the list to requeue. Start the requeue thread if needed.
642 We are responsible for dropping the config log reference from here on out. */
643 static void mgc_requeue_add(struct config_llog_data *cld)
647 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
648 cld->cld_logname, atomic_read(&cld->cld_refcount),
649 cld->cld_stopping, rq_state);
650 LASSERT(atomic_read(&cld->cld_refcount) > 0);
652 mutex_lock(&cld->cld_lock);
653 if (cld->cld_stopping || cld->cld_lostlock) {
654 mutex_unlock(&cld->cld_lock);
657 /* this refcount will be released in mgc_requeue_thread. */
659 cld->cld_lostlock = 1;
660 mutex_unlock(&cld->cld_lock);
662 /* Hold lock for rq_state */
663 spin_lock(&config_list_lock);
664 if (rq_state & RQ_STOP) {
665 spin_unlock(&config_list_lock);
666 cld->cld_lostlock = 0;
670 spin_unlock(&config_list_lock);
676 /********************** class fns **********************/
677 static int mgc_local_llog_init(const struct lu_env *env,
678 struct obd_device *obd,
679 struct obd_device *disk)
681 struct llog_ctxt *ctxt;
686 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
691 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
693 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
699 static int mgc_local_llog_fini(const struct lu_env *env,
700 struct obd_device *obd)
702 struct llog_ctxt *ctxt;
706 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
707 llog_cleanup(env, ctxt);
712 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb)
714 struct lustre_sb_info *lsi = s2lsi(sb);
715 struct client_obd *cli = &obd->u.cli;
716 struct lu_fid rfid, fid;
717 struct dt_object *root, *dto;
724 LASSERT(lsi->lsi_dt_dev);
730 /* The mgc fs exclusion mutex. Only one fs can be setup at a time. */
731 mutex_lock(&cli->cl_mgc_mutex);
733 /* Setup the configs dir */
734 rc = lu_env_init(env, LCT_MG_THREAD);
738 fid.f_seq = FID_SEQ_LOCAL_NAME;
741 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
746 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
750 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
751 &cli->cl_mgc_los->los_dev->dd_lu_dev, NULL);
752 if (unlikely(IS_ERR(root)))
753 GOTO(out_los, rc = PTR_ERR(root));
755 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
757 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
758 lu_object_put_nocache(env, &root->do_lu);
760 GOTO(out_los, rc = PTR_ERR(dto));
762 cli->cl_mgc_configs_dir = dto;
764 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
765 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
769 /* We take an obd ref to insure that we can't get to mgc_cleanup
770 * without calling mgc_fs_cleanup first. */
771 class_incref(obd, "mgc_fs", obd);
773 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
777 lu_object_put(env, &cli->cl_mgc_configs_dir->do_lu);
778 cli->cl_mgc_configs_dir = NULL;
782 local_oid_storage_fini(env, cli->cl_mgc_los);
783 cli->cl_mgc_los = NULL;
784 mutex_unlock(&cli->cl_mgc_mutex);
793 static int mgc_fs_cleanup(struct obd_device *obd)
796 struct client_obd *cli = &obd->u.cli;
801 LASSERT(cli->cl_mgc_los != NULL);
803 rc = lu_env_init(&env, LCT_MG_THREAD);
807 mgc_local_llog_fini(&env, obd);
809 lu_object_put_nocache(&env, &cli->cl_mgc_configs_dir->do_lu);
810 cli->cl_mgc_configs_dir = NULL;
812 local_oid_storage_fini(&env, cli->cl_mgc_los);
813 cli->cl_mgc_los = NULL;
817 class_decref(obd, "mgc_fs", obd);
818 mutex_unlock(&cli->cl_mgc_mutex);
823 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
825 struct llog_ctxt *ctxt;
830 /* setup only remote ctxt, the local disk context is switched per each
831 * filesystem during mgc_fs_setup() */
832 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
837 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
840 llog_initiator_connect(ctxt);
846 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
848 struct llog_ctxt *ctxt;
852 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
854 llog_cleanup(env, ctxt);
860 static atomic_t mgc_count = ATOMIC_INIT(0);
861 static int mgc_precleanup(struct obd_device *obd)
867 if (atomic_dec_and_test(&mgc_count)) {
868 LASSERT(rq_state & RQ_RUNNING);
869 /* stop requeue thread */
872 /* wakeup requeue thread to clean our cld */
873 temp = RQ_NOW | RQ_PRECLEANUP;
876 spin_lock(&config_list_lock);
878 spin_unlock(&config_list_lock);
882 wait_for_completion(&rq_exit);
883 obd_cleanup_client_import(obd);
885 rc = mgc_llog_fini(NULL, obd);
887 CERROR("failed to cleanup llogging subsystems\n");
892 static int mgc_cleanup(struct obd_device *obd)
897 /* COMPAT_146 - old config logs may have added profiles we don't
899 if (obd->obd_type->typ_refcnt <= 1)
900 /* Only for the last mgc */
901 class_del_profiles();
903 lprocfs_obd_cleanup(obd);
906 rc = client_obd_cleanup(obd);
910 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
912 struct task_struct *task;
916 rc = ptlrpcd_addref();
920 rc = client_obd_setup(obd, lcfg);
922 GOTO(err_decref, rc);
924 rc = mgc_llog_init(NULL, obd);
926 CERROR("failed to setup llogging subsystems\n");
927 GOTO(err_cleanup, rc);
930 #ifdef CONFIG_PROC_FS
931 obd->obd_vars = lprocfs_mgc_obd_vars;
932 lprocfs_obd_setup(obd);
934 sptlrpc_lprocfs_cliobd_attach(obd);
936 if (atomic_inc_return(&mgc_count) == 1) {
938 init_waitqueue_head(&rq_waitq);
940 /* start requeue thread */
941 task = kthread_run(mgc_requeue_thread, NULL, "ll_cfg_requeue");
944 CERROR("%s: cannot start requeue thread: rc = %d; "
945 "no more log updates\n",
947 GOTO(err_cleanup, rc);
949 /* rc is the task_struct pointer of mgc_requeue_thread. */
951 wait_for_completion(&rq_start);
957 client_obd_cleanup(obd);
963 /* based on ll_mdc_blocking_ast */
964 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
965 void *data, int flag)
967 struct lustre_handle lockh;
968 struct config_llog_data *cld = (struct config_llog_data *)data;
973 case LDLM_CB_BLOCKING:
974 /* mgs wants the lock, give it up... */
975 LDLM_DEBUG(lock, "MGC blocking CB");
976 ldlm_lock2handle(lock, &lockh);
977 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
979 case LDLM_CB_CANCELING:
980 /* We've given up the lock, prepare ourselves to update. */
981 LDLM_DEBUG(lock, "MGC cancel CB");
983 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
984 PLDLMRES(lock->l_resource),
985 (char *)&lock->l_resource->lr_name.name[0]);
988 CDEBUG(D_INFO, "missing data, won't requeue\n");
992 /* held at mgc_process_log(). */
993 LASSERT(atomic_read(&cld->cld_refcount) > 0);
994 /* Are we done with this log? */
995 if (cld->cld_stopping) {
996 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
1001 /* Make sure not to re-enqueue when the mgc is stopping
1002 (we get called from client_disconnect_export) */
1003 if (lock->l_conn_export == NULL ||
1004 lock->l_conn_export->exp_obd->u.cli.cl_conn_count == 0) {
1005 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
1007 config_log_put(cld);
1011 /* Re-enqueue now */
1012 mgc_requeue_add(cld);
1013 config_log_put(cld);
1022 /* Not sure where this should go... */
1023 /* This is the timeout value for MGS_CONNECT request plus a ping interval, such
1024 * that we can have a chance to try the secondary MGS if any. */
1025 #define MGC_ENQUEUE_LIMIT (INITIAL_CONNECT_TIMEOUT + (AT_OFF ? 0 : at_min) \
1027 #define MGC_TARGET_REG_LIMIT 10
1028 #define MGC_SEND_PARAM_LIMIT 10
1030 /* Send parameter to MGS*/
1031 static int mgc_set_mgs_param(struct obd_export *exp,
1032 struct mgs_send_param *msp)
1034 struct ptlrpc_request *req;
1035 struct mgs_send_param *req_msp, *rep_msp;
1039 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1040 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
1045 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1047 ptlrpc_req_finished(req);
1051 memcpy(req_msp, msp, sizeof(*req_msp));
1052 ptlrpc_request_set_replen(req);
1054 /* Limit how long we will wait for the enqueue to complete */
1055 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
1056 rc = ptlrpc_queue_wait(req);
1058 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1059 memcpy(msp, rep_msp, sizeof(*rep_msp));
1062 ptlrpc_req_finished(req);
1067 /* Take a config lock so we can get cancel notifications */
1068 static int mgc_enqueue(struct obd_export *exp, enum ldlm_type type,
1069 union ldlm_policy_data *policy, enum ldlm_mode mode,
1070 __u64 *flags, void *bl_cb, void *cp_cb, void *gl_cb,
1071 void *data, __u32 lvb_len, void *lvb_swabber,
1072 struct lustre_handle *lockh)
1074 struct config_llog_data *cld = (struct config_llog_data *)data;
1075 struct ldlm_enqueue_info einfo = {
1078 .ei_cb_bl = mgc_blocking_ast,
1079 .ei_cb_cp = ldlm_completion_ast,
1081 struct ptlrpc_request *req;
1082 int short_limit = cld_is_sptlrpc(cld);
1086 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
1087 cld->cld_resid.name[0]);
1089 /* We need a callback for every lockholder, so don't try to
1090 ldlm_lock_match (see rev 1.1.2.11.2.47) */
1091 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1092 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1097 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1098 ptlrpc_request_set_replen(req);
1100 /* check if this is server or client */
1101 if (cld->cld_cfg.cfg_sb) {
1102 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
1103 if (lsi && IS_SERVER(lsi))
1106 /* Limit how long we will wait for the enqueue to complete */
1107 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
1108 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1109 NULL, 0, LVB_T_NONE, lockh, 0);
1110 /* A failed enqueue should still call the mgc_blocking_ast,
1111 where it will be requeued if needed ("grant failed"). */
1112 ptlrpc_req_finished(req);
1116 static int mgc_cancel(struct obd_export *exp, enum ldlm_mode mode,
1117 struct lustre_handle *lockh)
1121 ldlm_lock_decref(lockh, mode);
1126 static void mgc_notify_active(struct obd_device *unused)
1128 /* wakeup mgc_requeue_thread to requeue mgc lock */
1129 spin_lock(&config_list_lock);
1131 spin_unlock(&config_list_lock);
1134 /* TODO: Help the MGS rebuild nidtbl. -jay */
1137 /* Send target_reg message to MGS */
1138 static int mgc_target_register(struct obd_export *exp,
1139 struct mgs_target_info *mti)
1141 struct ptlrpc_request *req;
1142 struct mgs_target_info *req_mti, *rep_mti;
1146 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1147 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1152 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1154 ptlrpc_req_finished(req);
1158 memcpy(req_mti, mti, sizeof(*req_mti));
1159 ptlrpc_request_set_replen(req);
1160 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1161 /* Limit how long we will wait for the enqueue to complete */
1162 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1164 rc = ptlrpc_queue_wait(req);
1166 rep_mti = req_capsule_server_get(&req->rq_pill,
1167 &RMF_MGS_TARGET_INFO);
1168 memcpy(mti, rep_mti, sizeof(*rep_mti));
1169 CDEBUG(D_MGC, "register %s got index = %d\n",
1170 mti->mti_svname, mti->mti_stripe_index);
1172 ptlrpc_req_finished(req);
1177 static int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1178 u32 keylen, void *key,
1179 u32 vallen, void *val,
1180 struct ptlrpc_request_set *set)
1185 /* Turn off initial_recov after we try all backup servers once */
1186 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1187 struct obd_import *imp = class_exp2cliimp(exp);
1189 if (vallen != sizeof(int))
1191 value = *(int *)val;
1192 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1193 imp->imp_obd->obd_name, value,
1194 imp->imp_deactive, imp->imp_invalid,
1195 imp->imp_replayable, imp->imp_obd->obd_replayable,
1196 ptlrpc_import_state_name(imp->imp_state));
1197 /* Resurrect if we previously died */
1198 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1199 imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1200 ptlrpc_reconnect_import(imp);
1203 /* FIXME move this to mgc_process_config */
1204 if (KEY_IS(KEY_REGISTER_TARGET)) {
1205 struct mgs_target_info *mti;
1206 if (vallen != sizeof(struct mgs_target_info))
1208 mti = (struct mgs_target_info *)val;
1209 CDEBUG(D_MGC, "register_target %s %#x\n",
1210 mti->mti_svname, mti->mti_flags);
1211 rc = mgc_target_register(exp, mti);
1214 if (KEY_IS(KEY_SET_FS)) {
1215 struct super_block *sb = (struct super_block *)val;
1217 if (vallen != sizeof(struct super_block))
1220 rc = mgc_fs_setup(exp->exp_obd, sb);
1223 if (KEY_IS(KEY_CLEAR_FS)) {
1226 rc = mgc_fs_cleanup(exp->exp_obd);
1229 if (KEY_IS(KEY_SET_INFO)) {
1230 struct mgs_send_param *msp;
1232 msp = (struct mgs_send_param *)val;
1233 rc = mgc_set_mgs_param(exp, msp);
1236 if (KEY_IS(KEY_MGSSEC)) {
1237 struct client_obd *cli = &exp->exp_obd->u.cli;
1238 struct sptlrpc_flavor flvr;
1241 * empty string means using current flavor, if which haven't
1242 * been set yet, set it as null.
1244 * if flavor has been set previously, check the asking flavor
1245 * must match the existing one.
1248 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1254 rc = sptlrpc_parse_flavor(val, &flvr);
1256 CERROR("invalid sptlrpc flavor %s to MGS\n",
1262 * caller already hold a mutex
1264 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1265 cli->cl_flvr_mgc = flvr;
1266 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1267 sizeof(flvr)) != 0) {
1270 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1272 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1273 "currently %s is in use\n",
1283 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1284 __u32 keylen, void *key, __u32 *vallen, void *val)
1288 if (KEY_IS(KEY_CONN_DATA)) {
1289 struct obd_import *imp = class_exp2cliimp(exp);
1290 struct obd_connect_data *data = val;
1292 if (*vallen == sizeof(*data)) {
1293 *data = imp->imp_connect_data;
1301 static int mgc_import_event(struct obd_device *obd,
1302 struct obd_import *imp,
1303 enum obd_import_event event)
1307 LASSERT(imp->imp_obd == obd);
1308 CDEBUG(D_MGC, "import event %#x\n", event);
1311 case IMP_EVENT_DISCON:
1312 /* MGC imports should not wait for recovery */
1313 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1314 ptlrpc_pinger_ir_down();
1316 case IMP_EVENT_INACTIVE:
1318 case IMP_EVENT_INVALIDATE: {
1319 struct ldlm_namespace *ns = obd->obd_namespace;
1320 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1323 case IMP_EVENT_ACTIVE:
1324 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1325 /* Clearing obd_no_recov allows us to continue pinging */
1326 obd->obd_no_recov = 0;
1327 mgc_notify_active(obd);
1328 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1329 ptlrpc_pinger_ir_up();
1333 case IMP_EVENT_DEACTIVATE:
1334 case IMP_EVENT_ACTIVATE:
1337 CERROR("Unknown import event %#x\n", event);
1344 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_CACHE_SHIFT),
1345 CONFIG_READ_NRPAGES = 4
1348 static int mgc_apply_recover_logs(struct obd_device *mgc,
1349 struct config_llog_data *cld,
1351 void *data, int datalen, bool mne_swab)
1353 struct config_llog_instance *cfg = &cld->cld_cfg;
1354 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1355 struct mgs_nidtbl_entry *entry;
1356 struct lustre_cfg *lcfg;
1357 struct lustre_cfg_bufs bufs;
1358 u64 prev_version = 0;
1367 LASSERT(cfg->cfg_instance != NULL);
1368 LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1370 OBD_ALLOC(inst, PAGE_CACHE_SIZE);
1374 if (!IS_SERVER(lsi)) {
1375 pos = snprintf(inst, PAGE_CACHE_SIZE, "%p", cfg->cfg_instance);
1376 if (pos >= PAGE_CACHE_SIZE) {
1377 OBD_FREE(inst, PAGE_CACHE_SIZE);
1381 LASSERT(IS_MDT(lsi));
1382 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1385 OBD_FREE(inst, PAGE_CACHE_SIZE);
1393 bufsz = PAGE_CACHE_SIZE - pos;
1395 while (datalen > 0) {
1396 int entry_len = sizeof(*entry);
1398 struct obd_device *obd;
1405 if (datalen < sizeof(*entry))
1408 entry = (typeof(entry))(data + off);
1411 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1413 if (entry->mne_nid_count == 0) /* at least one nid entry */
1415 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1418 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1419 if (datalen < entry_len) /* must have entry_len at least */
1422 /* Keep this swab for normal mixed endian handling. LU-1644 */
1424 lustre_swab_mgs_nidtbl_entry(entry);
1425 if (entry->mne_length > PAGE_CACHE_SIZE) {
1426 CERROR("MNE too large (%u)\n", entry->mne_length);
1430 if (entry->mne_length < entry_len)
1433 off += entry->mne_length;
1434 datalen -= entry->mne_length;
1438 if (entry->mne_version > max_version) {
1439 CERROR("entry index(%lld) is over max_index(%lld)\n",
1440 entry->mne_version, max_version);
1444 if (prev_version >= entry->mne_version) {
1445 CERROR("index unsorted, prev %lld, now %lld\n",
1446 prev_version, entry->mne_version);
1449 prev_version = entry->mne_version;
1452 * Write a string with format "nid::instance" to
1453 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1456 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1457 memset(buf, 0, bufsz);
1461 /* lustre-OST0001-osc-<instance #> */
1462 strcpy(obdname, cld->cld_logname);
1463 cname = strrchr(obdname, '-');
1464 if (cname == NULL) {
1465 CERROR("mgc %s: invalid logname %s\n",
1466 mgc->obd_name, obdname);
1470 pos = cname - obdname;
1472 pos += sprintf(obdname + pos, "-%s%04x",
1473 is_ost ? "OST" : "MDT", entry->mne_index);
1475 cname = is_ost ? "osc" : "mdc",
1476 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1477 lustre_cfg_bufs_reset(&bufs, obdname);
1479 /* find the obd by obdname */
1480 obd = class_name2obd(obdname);
1482 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1483 mgc->obd_name, obdname);
1485 /* this is a safe race, when the ost is starting up...*/
1489 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1492 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1495 down_read(&obd->u.cli.cl_sem);
1496 if (obd->u.cli.cl_import == NULL) {
1497 /* client does not connect to the OST yet */
1498 up_read(&obd->u.cli.cl_sem);
1503 /* iterate all nids to find one */
1504 /* find uuid by nid */
1506 for (i = 0; i < entry->mne_nid_count; i++) {
1507 rc = client_import_find_conn(obd->u.cli.cl_import,
1509 (struct obd_uuid *)uuid);
1514 up_read(&obd->u.cli.cl_sem);
1516 CERROR("mgc: cannot find uuid by nid %s\n",
1517 libcfs_nid2str(entry->u.nids[0]));
1521 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1522 uuid, libcfs_nid2str(entry->u.nids[0]));
1524 pos += strlen(uuid);
1525 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1526 LASSERT(pos < bufsz);
1528 lustre_cfg_bufs_set_string(&bufs, 1, params);
1530 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1536 CDEBUG(D_INFO, "ir apply logs "LPD64"/"LPD64" for %s -> %s\n",
1537 prev_version, max_version, obdname, params);
1539 rc = class_process_config(lcfg);
1540 lustre_cfg_free(lcfg);
1542 CDEBUG(D_INFO, "process config for %s error %d\n",
1545 /* continue, even one with error */
1548 OBD_FREE(inst, PAGE_CACHE_SIZE);
1553 * This function is called if this client was notified for target restarting
1554 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery logs.
1556 static int mgc_process_recover_log(struct obd_device *obd,
1557 struct config_llog_data *cld)
1559 struct ptlrpc_request *req = NULL;
1560 struct config_llog_instance *cfg = &cld->cld_cfg;
1561 struct mgs_config_body *body;
1562 struct mgs_config_res *res;
1563 struct ptlrpc_bulk_desc *desc;
1564 struct page **pages;
1567 bool mne_swab = false;
1573 /* allocate buffer for bulk transfer.
1574 * if this is the first time for this mgs to read logs,
1575 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1576 * once; otherwise, it only reads increment of logs, this should be
1577 * small and CONFIG_READ_NRPAGES will be used.
1579 nrpages = CONFIG_READ_NRPAGES;
1580 if (cfg->cfg_last_idx == 0) /* the first time */
1581 nrpages = CONFIG_READ_NRPAGES_INIT;
1583 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1585 GOTO(out, rc = -ENOMEM);
1587 for (i = 0; i < nrpages; i++) {
1588 pages[i] = alloc_page(GFP_IOFS);
1589 if (pages[i] == NULL)
1590 GOTO(out, rc = -ENOMEM);
1594 LASSERT(cld_is_recover(cld));
1595 LASSERT(mutex_is_locked(&cld->cld_lock));
1596 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1597 &RQF_MGS_CONFIG_READ);
1599 GOTO(out, rc = -ENOMEM);
1601 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1606 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1607 LASSERT(body != NULL);
1608 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1609 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1610 >= sizeof(body->mcb_name))
1611 GOTO(out, rc = -E2BIG);
1612 body->mcb_offset = cfg->cfg_last_idx + 1;
1613 body->mcb_type = cld->cld_type;
1614 body->mcb_bits = PAGE_CACHE_SHIFT;
1615 body->mcb_units = nrpages;
1617 /* allocate bulk transfer descriptor */
1618 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1,
1619 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1621 &ptlrpc_bulk_kiov_pin_ops);
1623 GOTO(out, rc = -ENOMEM);
1625 for (i = 0; i < nrpages; i++)
1626 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1629 ptlrpc_request_set_replen(req);
1630 rc = ptlrpc_queue_wait(req);
1634 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1635 if (res->mcr_size < res->mcr_offset)
1636 GOTO(out, rc = -EINVAL);
1638 /* always update the index even though it might have errors with
1639 * handling the recover logs */
1640 cfg->cfg_last_idx = res->mcr_offset;
1641 eof = res->mcr_offset == res->mcr_size;
1643 CDEBUG(D_INFO, "Latest version "LPD64", more %d.\n",
1644 res->mcr_offset, eof == false);
1646 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1648 GOTO(out, rc = ealen);
1650 if (ealen > nrpages << PAGE_CACHE_SHIFT)
1651 GOTO(out, rc = -EINVAL);
1653 if (ealen == 0) { /* no logs transferred */
1659 mne_swab = !!ptlrpc_rep_need_swab(req);
1660 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
1661 /* This import flag means the server did an extra swab of IR MNE
1662 * records (fixed in LU-1252), reverse it here if needed. LU-1644 */
1663 if (unlikely(req->rq_import->imp_need_mne_swab))
1664 mne_swab = !mne_swab;
1667 for (i = 0; i < nrpages && ealen > 0; i++) {
1671 ptr = kmap(pages[i]);
1672 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1673 min_t(int, ealen, PAGE_CACHE_SIZE),
1677 CWARN("Process recover log %s error %d\n",
1678 cld->cld_logname, rc2);
1682 ealen -= PAGE_CACHE_SIZE;
1687 ptlrpc_req_finished(req);
1689 if (rc == 0 && !eof)
1693 for (i = 0; i < nrpages; i++) {
1694 if (pages[i] == NULL)
1696 __free_page(pages[i]);
1698 OBD_FREE(pages, sizeof(*pages) * nrpages);
1703 /* Copy a remote log locally */
1704 static int mgc_llog_local_copy(const struct lu_env *env,
1705 struct obd_device *obd,
1706 struct llog_ctxt *rctxt,
1707 struct llog_ctxt *lctxt, char *logname)
1715 * - copy it to backup using llog_backup()
1716 * - copy remote llog to logname using llog_backup()
1717 * - if failed then move bakup to logname again
1720 OBD_ALLOC(temp_log, strlen(logname) + 2);
1723 sprintf(temp_log, "%sT", logname);
1725 /* make a copy of local llog at first */
1726 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1727 if (rc < 0 && rc != -ENOENT)
1729 /* copy remote llog to the local copy */
1730 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1731 if (rc == -ENOENT) {
1732 /* no remote llog, delete local one too */
1733 llog_erase(env, lctxt, NULL, logname);
1734 } else if (rc < 0) {
1735 /* error during backup, get local one back from the copy */
1736 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1738 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1739 obd->obd_name, logname, rc);
1741 llog_erase(env, lctxt, NULL, temp_log);
1742 OBD_FREE(temp_log, strlen(logname) + 2);
1746 /* local_only means it cannot get remote llogs */
1747 static int mgc_process_cfg_log(struct obd_device *mgc,
1748 struct config_llog_data *cld, int local_only)
1750 struct llog_ctxt *ctxt, *lctxt = NULL;
1751 struct client_obd *cli = &mgc->u.cli;
1752 struct lustre_sb_info *lsi = NULL;
1754 bool sptlrpc_started = false;
1760 LASSERT(mutex_is_locked(&cld->cld_lock));
1763 * local copy of sptlrpc log is controlled elsewhere, don't try to
1766 if (cld_is_sptlrpc(cld) && local_only)
1769 if (cld->cld_cfg.cfg_sb)
1770 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1776 rc = lu_env_init(env, LCT_MG_THREAD);
1780 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1783 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1785 /* Copy the setup log locally if we can. Don't mess around if we're
1786 * running an MGS though (logs are already local). */
1787 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1788 cli->cl_mgc_configs_dir != NULL &&
1789 lu2dt_dev(cli->cl_mgc_configs_dir->do_lu.lo_dev) ==
1792 /* Only try to copy log if we have the lock. */
1793 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1795 if (local_only || rc) {
1796 if (strcmp(cld->cld_logname, PARAMS_FILENAME) != 0 &&
1797 llog_is_empty(env, lctxt, cld->cld_logname)) {
1798 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1799 "log %s and no local copy."
1800 "\n", cld->cld_logname);
1801 GOTO(out_pop, rc = -ENOENT);
1803 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1804 "copy for now, will try to update later.\n",
1808 /* Now, whether we copied or not, start using the local llog.
1809 * If we failed to copy, we'll start using whatever the old
1811 llog_ctxt_put(ctxt);
1815 if (local_only) /* no local log at client side */
1816 GOTO(out_pop, rc = -EIO);
1819 if (cld_is_sptlrpc(cld)) {
1820 sptlrpc_conf_log_update_begin(cld->cld_logname);
1821 sptlrpc_started = true;
1824 /* logname and instance info should be the same, so use our
1825 * copy of the instance for the update. The cfg_last_idx will
1826 * be updated here. */
1827 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1832 __llog_ctxt_put(env, ctxt);
1834 __llog_ctxt_put(env, lctxt);
1837 * update settings on existing OBDs. doing it inside
1838 * of llog_process_lock so no device is attaching/detaching
1840 * the logname must be <fsname>-sptlrpc
1842 if (sptlrpc_started) {
1843 LASSERT(cld_is_sptlrpc(cld));
1844 sptlrpc_conf_log_update_end(cld->cld_logname);
1845 class_notify_sptlrpc_conf(cld->cld_logname,
1846 strlen(cld->cld_logname) -
1847 strlen("-sptlrpc"));
1856 static bool mgc_import_in_recovery(struct obd_import *imp)
1858 bool in_recovery = true;
1860 spin_lock(&imp->imp_lock);
1861 if (imp->imp_state == LUSTRE_IMP_FULL ||
1862 imp->imp_state == LUSTRE_IMP_CLOSED)
1863 in_recovery = false;
1864 spin_unlock(&imp->imp_lock);
1870 * Get a configuration log from the MGS and process it.
1872 * This function is called for both clients and servers to process the
1873 * configuration log from the MGS. The MGC enqueues a DLM lock on the
1874 * log from the MGS, and if the lock gets revoked the MGC will be notified
1875 * by the lock cancellation callback that the config log has changed,
1876 * and will enqueue another MGS lock on it, and then continue processing
1877 * the new additions to the end of the log.
1879 * Since the MGC import is not replayable, if the import is being evicted
1880 * (rcl == -ESHUTDOWN, \see ptlrpc_import_delay_req()), retry to process
1881 * the log until recovery is finished or the import is closed.
1883 * Make a local copy of the log before parsing it if appropriate (non-MGS
1884 * server) so that the server can start even when the MGS is down.
1886 * There shouldn't be multiple processes running process_log at once --
1887 * sounds like badness. It actually might be fine, as long as they're not
1888 * trying to update from the same log simultaneously, in which case we
1889 * should use a per-log semaphore instead of cld_lock.
1891 * \param[in] mgc MGC device by which to fetch the configuration log
1892 * \param[in] cld log processing state (stored in lock callback data)
1894 * \retval 0 on success
1895 * \retval negative errno on failure
1897 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1899 struct lustre_handle lockh = { 0 };
1900 __u64 flags = LDLM_FL_NO_LRU;
1905 LASSERT(cld != NULL);
1907 /* I don't want multiple processes running process_log at once --
1908 sounds like badness. It actually might be fine, as long as
1909 we're not trying to update from the same log
1910 simultaneously (in which case we should use a per-log sem.) */
1912 mutex_lock(&cld->cld_lock);
1913 if (cld->cld_stopping) {
1914 mutex_unlock(&cld->cld_lock);
1918 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1920 CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
1921 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1923 /* Get the cfg lock on the llog */
1924 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, LDLM_PLAIN, NULL,
1925 LCK_CR, &flags, NULL, NULL, NULL,
1926 cld, 0, NULL, &lockh);
1928 /* Get the cld, it will be released in mgc_blocking_ast. */
1929 config_log_get(cld);
1930 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1933 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1935 if (rcl == -ESHUTDOWN &&
1936 atomic_read(&mgc->u.cli.cl_mgc_refcount) > 0 && !retry) {
1937 struct obd_import *imp;
1938 struct l_wait_info lwi;
1939 int secs = cfs_time_seconds(obd_timeout);
1941 mutex_unlock(&cld->cld_lock);
1942 imp = class_exp2cliimp(mgc->u.cli.cl_mgc_mgsexp);
1944 /* Let's force the pinger, and wait the import to be
1945 * connected, note: since mgc import is non-replayable,
1946 * and even the import state is disconnected, it does
1947 * not mean the "recovery" is stopped, so we will keep
1948 * waitting until timeout or the import state is
1950 ptlrpc_pinger_force(imp);
1952 lwi = LWI_TIMEOUT(secs, NULL, NULL);
1953 l_wait_event(imp->imp_recovery_waitq,
1954 !mgc_import_in_recovery(imp), &lwi);
1956 if (imp->imp_state == LUSTRE_IMP_FULL) {
1960 mutex_lock(&cld->cld_lock);
1961 cld->cld_lostlock = 1;
1964 /* mark cld_lostlock so that it will requeue
1965 * after MGC becomes available. */
1966 cld->cld_lostlock = 1;
1968 /* Get extra reference, it will be put in requeue thread */
1969 config_log_get(cld);
1973 if (cld_is_recover(cld)) {
1974 rc = 0; /* this is not a fatal error for recover log */
1976 rc = mgc_process_recover_log(mgc, cld);
1978 CERROR("%s: recover log %s failed: rc = %d"
1979 "not fatal.\n", mgc->obd_name,
1980 cld->cld_logname, rc);
1982 cld->cld_lostlock = 1;
1986 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1989 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1990 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1992 mutex_unlock(&cld->cld_lock);
1994 /* Now drop the lock so MGS can revoke it */
1996 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, LCK_CR, &lockh);
1998 CERROR("Can't drop cfg lock: %d\n", rcl);
2005 /** Called from lustre_process_log.
2006 * LCFG_LOG_START gets the config log from the MGS, processes it to start
2007 * any services, and adds it to the list logs to watch (follow).
2009 static int mgc_process_config(struct obd_device *obd, size_t len, void *buf)
2011 struct lustre_cfg *lcfg = buf;
2012 struct config_llog_instance *cfg = NULL;
2017 switch(lcfg->lcfg_command) {
2018 case LCFG_LOV_ADD_OBD: {
2019 /* Overloading this cfg command: register a new target */
2020 struct mgs_target_info *mti;
2022 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
2023 sizeof(struct mgs_target_info))
2024 GOTO(out, rc = -EINVAL);
2026 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
2027 CDEBUG(D_MGC, "add_target %s %#x\n",
2028 mti->mti_svname, mti->mti_flags);
2029 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
2032 case LCFG_LOV_DEL_OBD:
2033 /* Unregister has no meaning at the moment. */
2034 CERROR("lov_del_obd unimplemented\n");
2037 case LCFG_SPTLRPC_CONF: {
2038 rc = sptlrpc_process_config(lcfg);
2041 case LCFG_LOG_START: {
2042 struct config_llog_data *cld;
2043 struct super_block *sb;
2045 logname = lustre_cfg_string(lcfg, 1);
2046 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
2047 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
2049 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
2052 /* We're only called through here on the initial mount */
2053 rc = config_log_add(obd, logname, cfg, sb);
2056 cld = config_log_find(logname, cfg);
2063 /* FIXME only set this for old logs! Right now this forces
2064 us to always skip the "inside markers" check */
2065 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
2067 rc = mgc_process_log(obd, cld);
2068 if (rc == 0 && cld->cld_recover != NULL) {
2069 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
2070 imp_connect_data, IMP_RECOV)) {
2071 rc = mgc_process_log(obd, cld->cld_recover);
2073 struct config_llog_data *cir = cld->cld_recover;
2074 cld->cld_recover = NULL;
2075 config_log_put(cir);
2078 CERROR("Cannot process recover llog %d\n", rc);
2081 if (rc == 0 && cld->cld_params != NULL) {
2082 rc = mgc_process_log(obd, cld->cld_params);
2083 if (rc == -ENOENT) {
2084 CDEBUG(D_MGC, "There is no params "
2085 "config file yet\n");
2088 /* params log is optional */
2090 CERROR("%s: can't process params llog: rc = %d\n",
2093 config_log_put(cld);
2097 case LCFG_LOG_END: {
2098 logname = lustre_cfg_string(lcfg, 1);
2100 if (lcfg->lcfg_bufcount >= 2)
2101 cfg = (struct config_llog_instance *)lustre_cfg_buf(
2103 rc = config_log_end(logname, cfg);
2107 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
2108 GOTO(out, rc = -EINVAL);
2116 static struct obd_ops mgc_obd_ops = {
2117 .o_owner = THIS_MODULE,
2118 .o_setup = mgc_setup,
2119 .o_precleanup = mgc_precleanup,
2120 .o_cleanup = mgc_cleanup,
2121 .o_add_conn = client_import_add_conn,
2122 .o_del_conn = client_import_del_conn,
2123 .o_connect = client_connect_import,
2124 .o_disconnect = client_disconnect_export,
2125 .o_set_info_async = mgc_set_info_async,
2126 .o_get_info = mgc_get_info,
2127 .o_import_event = mgc_import_event,
2128 .o_process_config = mgc_process_config,
2131 static int __init mgc_init(void)
2133 return class_register_type(&mgc_obd_ops, NULL, true, NULL,
2134 LUSTRE_MGC_NAME, NULL);
2137 static void /*__exit*/ mgc_exit(void)
2139 class_unregister_type(LUSTRE_MGC_NAME);
2142 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2143 MODULE_DESCRIPTION("Lustre Management Client");
2144 MODULE_LICENSE("GPL");
2146 module_init(mgc_init);
2147 module_exit(mgc_exit);