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, 2013, 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 <obd_class.h>
46 #include <lustre_dlm.h>
47 #include <lprocfs_status.h>
48 #include <lustre_log.h>
49 #include <lustre_disk.h>
50 #include <dt_object.h>
52 #include "mgc_internal.h"
54 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id,
59 if (len > sizeof(resname)) {
60 CERROR("name too long: %s\n", name);
64 CERROR("missing name: %s\n", name);
67 memcpy(&resname, name, len);
69 /* Always use the same endianness for the resid */
70 memset(res_id, 0, sizeof(*res_id));
71 res_id->name[0] = cpu_to_le64(resname);
72 /* XXX: unfortunately, sptlprc and config llog share one lock */
75 case CONFIG_T_SPTLRPC:
78 case CONFIG_T_RECOVER:
85 res_id->name[1] = cpu_to_le64(resname);
86 CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.8s)\n", name,
87 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
91 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id, int type)
93 /* fsname is at most 8 chars long, maybe contain "-".
94 * e.g. "lustre", "SUN-000" */
95 return mgc_name2resid(fsname, strlen(fsname), res_id, type);
97 EXPORT_SYMBOL(mgc_fsname2resid);
99 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id, int type)
104 /* logname consists of "fsname-nodetype".
105 * e.g. "lustre-MDT0001", "SUN-000-client"
106 * there is an exception: llog "params" */
107 name_end = strrchr(logname, '-');
109 len = strlen(logname);
111 len = name_end - logname;
112 return mgc_name2resid(logname, len, res_id, type);
115 /********************** config llog list **********************/
116 static struct list_head config_llog_list = LIST_HEAD_INIT(config_llog_list);
117 static DEFINE_SPINLOCK(config_list_lock);
119 /* Take a reference to a config log */
120 static int config_log_get(struct config_llog_data *cld)
123 atomic_inc(&cld->cld_refcount);
124 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
125 atomic_read(&cld->cld_refcount));
129 /* Drop a reference to a config log. When no longer referenced,
130 we can free the config log data */
131 static void config_log_put(struct config_llog_data *cld)
135 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
136 atomic_read(&cld->cld_refcount));
137 LASSERT(atomic_read(&cld->cld_refcount) > 0);
139 /* spinlock to make sure no item with 0 refcount in the list */
140 if (atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
141 list_del(&cld->cld_list_chain);
142 spin_unlock(&config_list_lock);
144 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
146 if (cld->cld_recover)
147 config_log_put(cld->cld_recover);
148 if (cld->cld_sptlrpc)
149 config_log_put(cld->cld_sptlrpc);
151 config_log_put(cld->cld_params);
152 if (cld_is_sptlrpc(cld))
153 sptlrpc_conf_log_stop(cld->cld_logname);
155 class_export_put(cld->cld_mgcexp);
156 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
162 /* Find a config log by name */
164 struct config_llog_data *config_log_find(char *logname,
165 struct config_llog_instance *cfg)
167 struct config_llog_data *cld;
168 struct config_llog_data *found = NULL;
172 LASSERT(logname != NULL);
174 instance = cfg ? cfg->cfg_instance : NULL;
175 spin_lock(&config_list_lock);
176 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
177 /* check if instance equals */
178 if (instance != cld->cld_cfg.cfg_instance)
181 /* instance may be NULL, should check name */
182 if (strcmp(logname, cld->cld_logname) == 0) {
188 atomic_inc(&found->cld_refcount);
189 LASSERT(found->cld_stopping == 0 || cld_is_sptlrpc(found) == 0);
191 spin_unlock(&config_list_lock);
196 struct config_llog_data *do_config_log_add(struct obd_device *obd,
199 struct config_llog_instance *cfg,
200 struct super_block *sb)
202 struct config_llog_data *cld;
206 CDEBUG(D_MGC, "do adding config log %s:%p\n", logname,
207 cfg ? cfg->cfg_instance : 0);
209 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
211 RETURN(ERR_PTR(-ENOMEM));
213 strcpy(cld->cld_logname, logname);
217 cld->cld_cfg.cfg_callback = class_config_llog_handler;
218 mutex_init(&cld->cld_lock);
219 cld->cld_cfg.cfg_last_idx = 0;
220 cld->cld_cfg.cfg_flags = 0;
221 cld->cld_cfg.cfg_sb = sb;
222 cld->cld_type = type;
223 atomic_set(&cld->cld_refcount, 1);
225 /* Keep the mgc around until we are done */
226 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
228 if (cld_is_sptlrpc(cld)) {
229 sptlrpc_conf_log_start(logname);
230 cld->cld_cfg.cfg_obdname = obd->obd_name;
233 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
235 spin_lock(&config_list_lock);
236 list_add(&cld->cld_list_chain, &config_llog_list);
237 spin_unlock(&config_list_lock);
244 if (cld_is_sptlrpc(cld)) {
245 rc = mgc_process_log(obd, cld);
246 if (rc && rc != -ENOENT)
247 CERROR("failed processing sptlrpc log: %d\n", rc);
253 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
255 struct config_llog_instance *cfg,
256 struct super_block *sb)
258 struct config_llog_instance lcfg = *cfg;
259 struct lustre_sb_info *lsi = s2lsi(sb);
260 struct config_llog_data *cld;
266 /* for osp-on-ost, see lustre_start_osp() */
267 if (IS_MDT(lsi) && lcfg.cfg_instance)
270 /* we have to use different llog for clients and mdts for cmd
271 * where only clients are notified if one of cmd server restarts */
272 LASSERT(strlen(fsname) < sizeof(logname) / 2);
273 strcpy(logname, fsname);
274 if (IS_SERVER(lsi)) { /* mdt */
275 LASSERT(lcfg.cfg_instance == NULL);
276 lcfg.cfg_instance = sb;
277 strcat(logname, "-mdtir");
279 LASSERT(lcfg.cfg_instance != NULL);
280 strcat(logname, "-cliir");
283 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
287 static struct config_llog_data *config_params_log_add(struct obd_device *obd,
288 struct config_llog_instance *cfg, struct super_block *sb)
290 struct config_llog_instance lcfg = *cfg;
291 struct config_llog_data *cld;
293 lcfg.cfg_instance = sb;
295 cld = do_config_log_add(obd, PARAMS_FILENAME, CONFIG_T_PARAMS,
301 /** Add this log to the list of active logs watched by an MGC.
302 * Active means we're watching for updates.
303 * We have one active log per "mount" - client instance or servername.
304 * Each instance may be at a different point in the log.
306 static int config_log_add(struct obd_device *obd, char *logname,
307 struct config_llog_instance *cfg,
308 struct super_block *sb)
310 struct lustre_sb_info *lsi = s2lsi(sb);
311 struct config_llog_data *cld;
312 struct config_llog_data *sptlrpc_cld;
313 struct config_llog_data *params_cld;
319 CDEBUG(D_MGC, "adding config log %s:%p\n", logname, cfg->cfg_instance);
322 * for each regular log, the depended sptlrpc log name is
323 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
325 ptr = strrchr(logname, '-');
326 if (ptr == NULL || ptr - logname > 8) {
327 CERROR("logname %s is too long\n", logname);
331 memcpy(seclogname, logname, ptr - logname);
332 strcpy(seclogname + (ptr - logname), "-sptlrpc");
334 sptlrpc_cld = config_log_find(seclogname, NULL);
335 if (sptlrpc_cld == NULL) {
336 sptlrpc_cld = do_config_log_add(obd, seclogname,
337 CONFIG_T_SPTLRPC, NULL, NULL);
338 if (IS_ERR(sptlrpc_cld)) {
339 CERROR("can't create sptlrpc log: %s\n", seclogname);
340 GOTO(out_err, rc = PTR_ERR(sptlrpc_cld));
343 params_cld = config_params_log_add(obd, cfg, sb);
344 if (IS_ERR(params_cld)) {
345 rc = PTR_ERR(params_cld);
346 CERROR("%s: can't create params log: rc = %d\n",
351 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
353 CERROR("can't create log: %s\n", logname);
354 GOTO(out_err2, rc = PTR_ERR(cld));
357 cld->cld_sptlrpc = sptlrpc_cld;
358 cld->cld_params = params_cld;
360 LASSERT(lsi->lsi_lmd);
361 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)) {
362 struct config_llog_data *recover_cld;
363 ptr = strrchr(seclogname, '-');
368 CERROR("%s: sptlrpc log name not correct, %s: "
369 "rc = %d\n", obd->obd_name, seclogname, -EINVAL);
373 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
374 if (IS_ERR(recover_cld))
375 GOTO(out_err3, rc = PTR_ERR(recover_cld));
376 cld->cld_recover = recover_cld;
385 config_log_put(params_cld);
388 config_log_put(sptlrpc_cld);
394 DEFINE_MUTEX(llog_process_lock);
396 /** Stop watching for updates on this log.
398 static int config_log_end(char *logname, struct config_llog_instance *cfg)
400 struct config_llog_data *cld;
401 struct config_llog_data *cld_sptlrpc = NULL;
402 struct config_llog_data *cld_params = NULL;
403 struct config_llog_data *cld_recover = NULL;
407 cld = config_log_find(logname, cfg);
411 mutex_lock(&cld->cld_lock);
413 * if cld_stopping is set, it means we didn't start the log thus
414 * not owning the start ref. this can happen after previous umount:
415 * the cld still hanging there waiting for lock cancel, and we
416 * remount again but failed in the middle and call log_end without
419 if (unlikely(cld->cld_stopping)) {
420 mutex_unlock(&cld->cld_lock);
421 /* drop the ref from the find */
426 cld->cld_stopping = 1;
428 cld_recover = cld->cld_recover;
429 cld->cld_recover = NULL;
430 mutex_unlock(&cld->cld_lock);
433 mutex_lock(&cld_recover->cld_lock);
434 cld_recover->cld_stopping = 1;
435 mutex_unlock(&cld_recover->cld_lock);
436 config_log_put(cld_recover);
439 spin_lock(&config_list_lock);
440 cld_sptlrpc = cld->cld_sptlrpc;
441 cld->cld_sptlrpc = NULL;
442 cld_params = cld->cld_params;
443 cld->cld_params = NULL;
444 spin_unlock(&config_list_lock);
447 config_log_put(cld_sptlrpc);
450 mutex_lock(&cld_params->cld_lock);
451 cld_params->cld_stopping = 1;
452 mutex_unlock(&cld_params->cld_lock);
453 config_log_put(cld_params);
456 /* drop the ref from the find */
458 /* drop the start ref */
461 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
467 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
469 struct obd_device *obd = data;
470 struct obd_import *imp = obd->u.cli.cl_import;
471 struct obd_connect_data *ocd = &imp->imp_connect_data;
472 struct config_llog_data *cld;
475 seq_printf(m, "imperative_recovery: %s\n",
476 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
477 seq_printf(m, "client_state:\n");
479 spin_lock(&config_list_lock);
480 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
481 if (cld->cld_recover == NULL)
483 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
485 cld->cld_recover->cld_cfg.cfg_last_idx);
487 spin_unlock(&config_list_lock);
493 /* reenqueue any lost locks */
494 #define RQ_RUNNING 0x1
498 #define RQ_PRECLEANUP 0x10
499 static int rq_state = 0;
500 static wait_queue_head_t rq_waitq;
501 static DECLARE_COMPLETION(rq_exit);
502 static DECLARE_COMPLETION(rq_start);
504 static void do_requeue(struct config_llog_data *cld)
509 LASSERT(atomic_read(&cld->cld_refcount) > 0);
511 /* Do not run mgc_process_log on a disconnected export or an
512 * export which is being disconnected. Take the client
513 * semaphore to make the check non-racy. */
514 down_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
515 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
516 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
517 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
518 if (rc && rc != -ENOENT)
519 CERROR("failed processing log: %d\n", rc);
521 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
524 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
529 /* this timeout represents how many seconds MGC should wait before
530 * requeue config and recover lock to the MGS. We need to randomize this
531 * in order to not flood the MGS.
533 #define MGC_TIMEOUT_MIN_SECONDS 5
534 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
536 static int mgc_requeue_thread(void *data)
542 CDEBUG(D_MGC, "Starting requeue thread\n");
544 /* Keep trying failed locks periodically */
545 spin_lock(&config_list_lock);
546 rq_state |= RQ_RUNNING;
548 struct l_wait_info lwi;
549 struct config_llog_data *cld, *cld_prev;
550 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
551 int stopped = !!(rq_state & RQ_STOP);
554 /* Any new or requeued lostlocks will change the state */
555 rq_state &= ~(RQ_NOW | RQ_LATER);
556 spin_unlock(&config_list_lock);
563 /* Always wait a few seconds to allow the server who
564 caused the lock revocation to finish its setup, plus some
565 random so everyone doesn't try to reconnect at once. */
566 to = MGC_TIMEOUT_MIN_SECONDS * HZ;
567 to += rand * HZ / 100; /* rand is centi-seconds */
568 lwi = LWI_TIMEOUT(to, NULL, NULL);
569 l_wait_event(rq_waitq, rq_state & (RQ_STOP | RQ_PRECLEANUP),
573 * iterate & processing through the list. for each cld, process
574 * its depending sptlrpc cld firstly (if any) and then itself.
576 * it's guaranteed any item in the list must have
577 * reference > 0; and if cld_lostlock is set, at
578 * least one reference is taken by the previous enqueue.
582 spin_lock(&config_list_lock);
583 rq_state &= ~RQ_PRECLEANUP;
584 list_for_each_entry(cld, &config_llog_list,
586 if (!cld->cld_lostlock)
589 spin_unlock(&config_list_lock);
591 LASSERT(atomic_read(&cld->cld_refcount) > 0);
593 /* Whether we enqueued again or not in mgc_process_log,
594 * we're done with the ref from the old enqueue */
596 config_log_put(cld_prev);
599 cld->cld_lostlock = 0;
600 if (likely(!stopped))
603 spin_lock(&config_list_lock);
605 spin_unlock(&config_list_lock);
607 config_log_put(cld_prev);
609 /* break after scanning the list so that we can drop
610 * refcount to losing lock clds */
611 if (unlikely(stopped)) {
612 spin_lock(&config_list_lock);
616 /* Wait a bit to see if anyone else needs a requeue */
617 lwi = (struct l_wait_info) { 0 };
618 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
620 spin_lock(&config_list_lock);
622 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
623 rq_state &= ~RQ_RUNNING;
624 spin_unlock(&config_list_lock);
628 CDEBUG(D_MGC, "Ending requeue thread\n");
632 /* Add a cld to the list to requeue. Start the requeue thread if needed.
633 We are responsible for dropping the config log reference from here on out. */
634 static void mgc_requeue_add(struct config_llog_data *cld)
638 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
639 cld->cld_logname, atomic_read(&cld->cld_refcount),
640 cld->cld_stopping, rq_state);
641 LASSERT(atomic_read(&cld->cld_refcount) > 0);
643 mutex_lock(&cld->cld_lock);
644 if (cld->cld_stopping || cld->cld_lostlock) {
645 mutex_unlock(&cld->cld_lock);
648 /* this refcount will be released in mgc_requeue_thread. */
650 cld->cld_lostlock = 1;
651 mutex_unlock(&cld->cld_lock);
653 /* Hold lock for rq_state */
654 spin_lock(&config_list_lock);
655 if (rq_state & RQ_STOP) {
656 spin_unlock(&config_list_lock);
657 cld->cld_lostlock = 0;
661 spin_unlock(&config_list_lock);
667 /********************** class fns **********************/
668 static int mgc_local_llog_init(const struct lu_env *env,
669 struct obd_device *obd,
670 struct obd_device *disk)
672 struct llog_ctxt *ctxt;
677 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
682 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
684 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
690 static int mgc_local_llog_fini(const struct lu_env *env,
691 struct obd_device *obd)
693 struct llog_ctxt *ctxt;
697 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
698 llog_cleanup(env, ctxt);
703 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb)
705 struct lustre_sb_info *lsi = s2lsi(sb);
706 struct client_obd *cli = &obd->u.cli;
707 struct lu_fid rfid, fid;
708 struct dt_object *root, *dto;
715 LASSERT(lsi->lsi_dt_dev);
721 /* The mgc fs exclusion mutex. Only one fs can be setup at a time. */
722 mutex_lock(&cli->cl_mgc_mutex);
724 /* Setup the configs dir */
725 rc = lu_env_init(env, LCT_MG_THREAD);
729 fid.f_seq = FID_SEQ_LOCAL_NAME;
732 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
737 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
741 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
742 &cli->cl_mgc_los->los_dev->dd_lu_dev, NULL);
743 if (unlikely(IS_ERR(root)))
744 GOTO(out_los, rc = PTR_ERR(root));
746 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
748 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
749 lu_object_put_nocache(env, &root->do_lu);
751 GOTO(out_los, rc = PTR_ERR(dto));
753 cli->cl_mgc_configs_dir = dto;
755 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
756 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
760 /* We take an obd ref to insure that we can't get to mgc_cleanup
761 * without calling mgc_fs_cleanup first. */
762 class_incref(obd, "mgc_fs", obd);
764 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
768 lu_object_put(env, &cli->cl_mgc_configs_dir->do_lu);
769 cli->cl_mgc_configs_dir = NULL;
773 local_oid_storage_fini(env, cli->cl_mgc_los);
774 cli->cl_mgc_los = NULL;
775 mutex_unlock(&cli->cl_mgc_mutex);
784 static int mgc_fs_cleanup(struct obd_device *obd)
787 struct client_obd *cli = &obd->u.cli;
792 LASSERT(cli->cl_mgc_los != NULL);
794 rc = lu_env_init(&env, LCT_MG_THREAD);
798 mgc_local_llog_fini(&env, obd);
800 lu_object_put_nocache(&env, &cli->cl_mgc_configs_dir->do_lu);
801 cli->cl_mgc_configs_dir = NULL;
803 local_oid_storage_fini(&env, cli->cl_mgc_los);
804 cli->cl_mgc_los = NULL;
808 class_decref(obd, "mgc_fs", obd);
809 mutex_unlock(&cli->cl_mgc_mutex);
814 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
816 struct llog_ctxt *ctxt;
821 /* setup only remote ctxt, the local disk context is switched per each
822 * filesystem during mgc_fs_setup() */
823 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
828 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
831 llog_initiator_connect(ctxt);
837 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
839 struct llog_ctxt *ctxt;
843 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
845 llog_cleanup(env, ctxt);
851 static atomic_t mgc_count = ATOMIC_INIT(0);
852 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
859 case OBD_CLEANUP_EARLY:
861 case OBD_CLEANUP_EXPORTS:
862 if (atomic_dec_and_test(&mgc_count)) {
863 LASSERT(rq_state & RQ_RUNNING);
864 /* stop requeue thread */
867 /* wakeup requeue thread to clean our cld */
868 temp = RQ_NOW | RQ_PRECLEANUP;
870 spin_lock(&config_list_lock);
872 spin_unlock(&config_list_lock);
875 wait_for_completion(&rq_exit);
876 obd_cleanup_client_import(obd);
877 rc = mgc_llog_fini(NULL, obd);
879 CERROR("failed to cleanup llogging subsystems\n");
885 static int mgc_cleanup(struct obd_device *obd)
890 /* COMPAT_146 - old config logs may have added profiles we don't
892 if (obd->obd_type->typ_refcnt <= 1)
893 /* Only for the last mgc */
894 class_del_profiles();
896 lprocfs_obd_cleanup(obd);
899 rc = client_obd_cleanup(obd);
903 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
905 struct task_struct *task;
909 rc = ptlrpcd_addref();
913 rc = client_obd_setup(obd, lcfg);
915 GOTO(err_decref, rc);
917 rc = mgc_llog_init(NULL, obd);
919 CERROR("failed to setup llogging subsystems\n");
920 GOTO(err_cleanup, rc);
924 obd->obd_vars = lprocfs_mgc_obd_vars;
925 lprocfs_obd_setup(obd);
927 sptlrpc_lprocfs_cliobd_attach(obd);
929 if (atomic_inc_return(&mgc_count) == 1) {
931 init_waitqueue_head(&rq_waitq);
933 /* start requeue thread */
934 task = kthread_run(mgc_requeue_thread, NULL, "ll_cfg_requeue");
937 CERROR("%s: cannot start requeue thread: rc = %d; "
938 "no more log updates\n",
940 GOTO(err_cleanup, rc);
942 /* rc is the task_struct pointer of mgc_requeue_thread. */
944 wait_for_completion(&rq_start);
950 client_obd_cleanup(obd);
956 /* based on ll_mdc_blocking_ast */
957 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
958 void *data, int flag)
960 struct lustre_handle lockh;
961 struct config_llog_data *cld = (struct config_llog_data *)data;
966 case LDLM_CB_BLOCKING:
967 /* mgs wants the lock, give it up... */
968 LDLM_DEBUG(lock, "MGC blocking CB");
969 ldlm_lock2handle(lock, &lockh);
970 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
972 case LDLM_CB_CANCELING:
973 /* We've given up the lock, prepare ourselves to update. */
974 LDLM_DEBUG(lock, "MGC cancel CB");
976 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
977 PLDLMRES(lock->l_resource),
978 (char *)&lock->l_resource->lr_name.name[0]);
981 CDEBUG(D_INFO, "missing data, won't requeue\n");
985 /* held at mgc_process_log(). */
986 LASSERT(atomic_read(&cld->cld_refcount) > 0);
987 /* Are we done with this log? */
988 if (cld->cld_stopping) {
989 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
994 /* Make sure not to re-enqueue when the mgc is stopping
995 (we get called from client_disconnect_export) */
996 if (!lock->l_conn_export ||
997 !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
998 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
1000 config_log_put(cld);
1004 /* Re-enqueue now */
1005 mgc_requeue_add(cld);
1006 config_log_put(cld);
1015 /* Not sure where this should go... */
1016 /* This is the timeout value for MGS_CONNECT request plus a ping interval, such
1017 * that we can have a chance to try the secondary MGS if any. */
1018 #define MGC_ENQUEUE_LIMIT (INITIAL_CONNECT_TIMEOUT + (AT_OFF ? 0 : at_min) \
1020 #define MGC_TARGET_REG_LIMIT 10
1021 #define MGC_SEND_PARAM_LIMIT 10
1023 /* Send parameter to MGS*/
1024 static int mgc_set_mgs_param(struct obd_export *exp,
1025 struct mgs_send_param *msp)
1027 struct ptlrpc_request *req;
1028 struct mgs_send_param *req_msp, *rep_msp;
1032 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1033 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
1038 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1040 ptlrpc_req_finished(req);
1044 memcpy(req_msp, msp, sizeof(*req_msp));
1045 ptlrpc_request_set_replen(req);
1047 /* Limit how long we will wait for the enqueue to complete */
1048 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
1049 rc = ptlrpc_queue_wait(req);
1051 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1052 memcpy(msp, rep_msp, sizeof(*rep_msp));
1055 ptlrpc_req_finished(req);
1060 /* Take a config lock so we can get cancel notifications */
1061 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
1062 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
1063 __u64 *flags, void *bl_cb, void *cp_cb, void *gl_cb,
1064 void *data, __u32 lvb_len, void *lvb_swabber,
1065 struct lustre_handle *lockh)
1067 struct config_llog_data *cld = (struct config_llog_data *)data;
1068 struct ldlm_enqueue_info einfo = {
1071 .ei_cb_bl = mgc_blocking_ast,
1072 .ei_cb_cp = ldlm_completion_ast,
1074 struct ptlrpc_request *req;
1075 int short_limit = cld_is_sptlrpc(cld);
1079 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
1080 cld->cld_resid.name[0]);
1082 /* We need a callback for every lockholder, so don't try to
1083 ldlm_lock_match (see rev 1.1.2.11.2.47) */
1084 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1085 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1090 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1091 ptlrpc_request_set_replen(req);
1093 /* check if this is server or client */
1094 if (cld->cld_cfg.cfg_sb) {
1095 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
1096 if (lsi && IS_SERVER(lsi))
1099 /* Limit how long we will wait for the enqueue to complete */
1100 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
1101 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1102 NULL, 0, LVB_T_NONE, lockh, 0);
1103 /* A failed enqueue should still call the mgc_blocking_ast,
1104 where it will be requeued if needed ("grant failed"). */
1105 ptlrpc_req_finished(req);
1109 static int mgc_cancel(struct obd_export *exp, ldlm_mode_t mode,
1110 struct lustre_handle *lockh)
1114 ldlm_lock_decref(lockh, mode);
1119 static void mgc_notify_active(struct obd_device *unused)
1121 /* wakeup mgc_requeue_thread to requeue mgc lock */
1122 spin_lock(&config_list_lock);
1124 spin_unlock(&config_list_lock);
1127 /* TODO: Help the MGS rebuild nidtbl. -jay */
1130 /* Send target_reg message to MGS */
1131 static int mgc_target_register(struct obd_export *exp,
1132 struct mgs_target_info *mti)
1134 struct ptlrpc_request *req;
1135 struct mgs_target_info *req_mti, *rep_mti;
1139 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1140 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1145 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1147 ptlrpc_req_finished(req);
1151 memcpy(req_mti, mti, sizeof(*req_mti));
1152 ptlrpc_request_set_replen(req);
1153 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1154 /* Limit how long we will wait for the enqueue to complete */
1155 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1157 rc = ptlrpc_queue_wait(req);
1159 rep_mti = req_capsule_server_get(&req->rq_pill,
1160 &RMF_MGS_TARGET_INFO);
1161 memcpy(mti, rep_mti, sizeof(*rep_mti));
1162 CDEBUG(D_MGC, "register %s got index = %d\n",
1163 mti->mti_svname, mti->mti_stripe_index);
1165 ptlrpc_req_finished(req);
1170 int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1171 obd_count keylen, void *key, obd_count vallen,
1172 void *val, struct ptlrpc_request_set *set)
1177 /* Turn off initial_recov after we try all backup servers once */
1178 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1179 struct obd_import *imp = class_exp2cliimp(exp);
1181 if (vallen != sizeof(int))
1183 value = *(int *)val;
1184 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1185 imp->imp_obd->obd_name, value,
1186 imp->imp_deactive, imp->imp_invalid,
1187 imp->imp_replayable, imp->imp_obd->obd_replayable,
1188 ptlrpc_import_state_name(imp->imp_state));
1189 /* Resurrect if we previously died */
1190 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1191 imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1192 ptlrpc_reconnect_import(imp);
1195 /* FIXME move this to mgc_process_config */
1196 if (KEY_IS(KEY_REGISTER_TARGET)) {
1197 struct mgs_target_info *mti;
1198 if (vallen != sizeof(struct mgs_target_info))
1200 mti = (struct mgs_target_info *)val;
1201 CDEBUG(D_MGC, "register_target %s %#x\n",
1202 mti->mti_svname, mti->mti_flags);
1203 rc = mgc_target_register(exp, mti);
1206 if (KEY_IS(KEY_SET_FS)) {
1207 struct super_block *sb = (struct super_block *)val;
1209 if (vallen != sizeof(struct super_block))
1212 rc = mgc_fs_setup(exp->exp_obd, sb);
1215 if (KEY_IS(KEY_CLEAR_FS)) {
1218 rc = mgc_fs_cleanup(exp->exp_obd);
1221 if (KEY_IS(KEY_SET_INFO)) {
1222 struct mgs_send_param *msp;
1224 msp = (struct mgs_send_param *)val;
1225 rc = mgc_set_mgs_param(exp, msp);
1228 if (KEY_IS(KEY_MGSSEC)) {
1229 struct client_obd *cli = &exp->exp_obd->u.cli;
1230 struct sptlrpc_flavor flvr;
1233 * empty string means using current flavor, if which haven't
1234 * been set yet, set it as null.
1236 * if flavor has been set previously, check the asking flavor
1237 * must match the existing one.
1240 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1246 rc = sptlrpc_parse_flavor(val, &flvr);
1248 CERROR("invalid sptlrpc flavor %s to MGS\n",
1254 * caller already hold a mutex
1256 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1257 cli->cl_flvr_mgc = flvr;
1258 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1259 sizeof(flvr)) != 0) {
1262 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1264 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1265 "currently %s is in use\n",
1275 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1276 __u32 keylen, void *key, __u32 *vallen, void *val,
1277 struct lov_stripe_md *unused)
1281 if (KEY_IS(KEY_CONN_DATA)) {
1282 struct obd_import *imp = class_exp2cliimp(exp);
1283 struct obd_connect_data *data = val;
1285 if (*vallen == sizeof(*data)) {
1286 *data = imp->imp_connect_data;
1294 static int mgc_import_event(struct obd_device *obd,
1295 struct obd_import *imp,
1296 enum obd_import_event event)
1300 LASSERT(imp->imp_obd == obd);
1301 CDEBUG(D_MGC, "import event %#x\n", event);
1304 case IMP_EVENT_DISCON:
1305 /* MGC imports should not wait for recovery */
1306 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1307 ptlrpc_pinger_ir_down();
1309 case IMP_EVENT_INACTIVE:
1311 case IMP_EVENT_INVALIDATE: {
1312 struct ldlm_namespace *ns = obd->obd_namespace;
1313 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1316 case IMP_EVENT_ACTIVE:
1317 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1318 /* Clearing obd_no_recov allows us to continue pinging */
1319 obd->obd_no_recov = 0;
1320 mgc_notify_active(obd);
1321 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1322 ptlrpc_pinger_ir_up();
1326 case IMP_EVENT_DEACTIVATE:
1327 case IMP_EVENT_ACTIVATE:
1330 CERROR("Unknown import event %#x\n", event);
1337 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_CACHE_SHIFT),
1338 CONFIG_READ_NRPAGES = 4
1341 static int mgc_apply_recover_logs(struct obd_device *mgc,
1342 struct config_llog_data *cld,
1344 void *data, int datalen, bool mne_swab)
1346 struct config_llog_instance *cfg = &cld->cld_cfg;
1347 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1348 struct mgs_nidtbl_entry *entry;
1349 struct lustre_cfg *lcfg;
1350 struct lustre_cfg_bufs bufs;
1351 u64 prev_version = 0;
1360 LASSERT(cfg->cfg_instance != NULL);
1361 LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1363 OBD_ALLOC(inst, PAGE_CACHE_SIZE);
1367 if (!IS_SERVER(lsi)) {
1368 pos = snprintf(inst, PAGE_CACHE_SIZE, "%p", cfg->cfg_instance);
1369 if (pos >= PAGE_CACHE_SIZE) {
1370 OBD_FREE(inst, PAGE_CACHE_SIZE);
1374 LASSERT(IS_MDT(lsi));
1375 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1378 OBD_FREE(inst, PAGE_CACHE_SIZE);
1386 bufsz = PAGE_CACHE_SIZE - pos;
1388 while (datalen > 0) {
1389 int entry_len = sizeof(*entry);
1391 struct obd_device *obd;
1398 if (datalen < sizeof(*entry))
1401 entry = (typeof(entry))(data + off);
1404 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1406 if (entry->mne_nid_count == 0) /* at least one nid entry */
1408 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1411 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1412 if (datalen < entry_len) /* must have entry_len at least */
1415 /* Keep this swab for normal mixed endian handling. LU-1644 */
1417 lustre_swab_mgs_nidtbl_entry(entry);
1418 if (entry->mne_length > PAGE_CACHE_SIZE) {
1419 CERROR("MNE too large (%u)\n", entry->mne_length);
1423 if (entry->mne_length < entry_len)
1426 off += entry->mne_length;
1427 datalen -= entry->mne_length;
1431 if (entry->mne_version > max_version) {
1432 CERROR("entry index(%lld) is over max_index(%lld)\n",
1433 entry->mne_version, max_version);
1437 if (prev_version >= entry->mne_version) {
1438 CERROR("index unsorted, prev %lld, now %lld\n",
1439 prev_version, entry->mne_version);
1442 prev_version = entry->mne_version;
1445 * Write a string with format "nid::instance" to
1446 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1449 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1450 memset(buf, 0, bufsz);
1454 /* lustre-OST0001-osc-<instance #> */
1455 strcpy(obdname, cld->cld_logname);
1456 cname = strrchr(obdname, '-');
1457 if (cname == NULL) {
1458 CERROR("mgc %s: invalid logname %s\n",
1459 mgc->obd_name, obdname);
1463 pos = cname - obdname;
1465 pos += sprintf(obdname + pos, "-%s%04x",
1466 is_ost ? "OST" : "MDT", entry->mne_index);
1468 cname = is_ost ? "osc" : "mdc",
1469 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1470 lustre_cfg_bufs_reset(&bufs, obdname);
1472 /* find the obd by obdname */
1473 obd = class_name2obd(obdname);
1475 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1476 mgc->obd_name, obdname);
1478 /* this is a safe race, when the ost is starting up...*/
1482 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1485 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1488 down_read(&obd->u.cli.cl_sem);
1489 if (obd->u.cli.cl_import == NULL) {
1490 /* client does not connect to the OST yet */
1491 up_read(&obd->u.cli.cl_sem);
1496 /* TODO: iterate all nids to find one */
1497 /* find uuid by nid */
1498 rc = client_import_find_conn(obd->u.cli.cl_import,
1500 (struct obd_uuid *)uuid);
1501 up_read(&obd->u.cli.cl_sem);
1503 CERROR("mgc: cannot find uuid by nid %s\n",
1504 libcfs_nid2str(entry->u.nids[0]));
1508 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1509 uuid, libcfs_nid2str(entry->u.nids[0]));
1511 pos += strlen(uuid);
1512 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1513 LASSERT(pos < bufsz);
1515 lustre_cfg_bufs_set_string(&bufs, 1, params);
1518 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1520 CERROR("mgc: cannot allocate memory\n");
1524 CDEBUG(D_INFO, "ir apply logs "LPD64"/"LPD64" for %s -> %s\n",
1525 prev_version, max_version, obdname, params);
1527 rc = class_process_config(lcfg);
1528 lustre_cfg_free(lcfg);
1530 CDEBUG(D_INFO, "process config for %s error %d\n",
1533 /* continue, even one with error */
1536 OBD_FREE(inst, PAGE_CACHE_SIZE);
1541 * This function is called if this client was notified for target restarting
1542 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery logs.
1544 static int mgc_process_recover_log(struct obd_device *obd,
1545 struct config_llog_data *cld)
1547 struct ptlrpc_request *req = NULL;
1548 struct config_llog_instance *cfg = &cld->cld_cfg;
1549 struct mgs_config_body *body;
1550 struct mgs_config_res *res;
1551 struct ptlrpc_bulk_desc *desc;
1552 struct page **pages;
1555 bool mne_swab = false;
1561 /* allocate buffer for bulk transfer.
1562 * if this is the first time for this mgs to read logs,
1563 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1564 * once; otherwise, it only reads increment of logs, this should be
1565 * small and CONFIG_READ_NRPAGES will be used.
1567 nrpages = CONFIG_READ_NRPAGES;
1568 if (cfg->cfg_last_idx == 0) /* the first time */
1569 nrpages = CONFIG_READ_NRPAGES_INIT;
1571 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1573 GOTO(out, rc = -ENOMEM);
1575 for (i = 0; i < nrpages; i++) {
1576 pages[i] = alloc_page(GFP_IOFS);
1577 if (pages[i] == NULL)
1578 GOTO(out, rc = -ENOMEM);
1582 LASSERT(cld_is_recover(cld));
1583 LASSERT(mutex_is_locked(&cld->cld_lock));
1584 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1585 &RQF_MGS_CONFIG_READ);
1587 GOTO(out, rc = -ENOMEM);
1589 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1594 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1595 LASSERT(body != NULL);
1596 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1597 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1598 >= sizeof(body->mcb_name))
1599 GOTO(out, rc = -E2BIG);
1600 body->mcb_offset = cfg->cfg_last_idx + 1;
1601 body->mcb_type = cld->cld_type;
1602 body->mcb_bits = PAGE_CACHE_SHIFT;
1603 body->mcb_units = nrpages;
1605 /* allocate bulk transfer descriptor */
1606 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1, BULK_PUT_SINK,
1609 GOTO(out, rc = -ENOMEM);
1611 for (i = 0; i < nrpages; i++)
1612 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1614 ptlrpc_request_set_replen(req);
1615 rc = ptlrpc_queue_wait(req);
1619 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1620 if (res->mcr_size < res->mcr_offset)
1621 GOTO(out, rc = -EINVAL);
1623 /* always update the index even though it might have errors with
1624 * handling the recover logs */
1625 cfg->cfg_last_idx = res->mcr_offset;
1626 eof = res->mcr_offset == res->mcr_size;
1628 CDEBUG(D_INFO, "Latest version "LPD64", more %d.\n",
1629 res->mcr_offset, eof == false);
1631 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1633 GOTO(out, rc = ealen);
1635 if (ealen > nrpages << PAGE_CACHE_SHIFT)
1636 GOTO(out, rc = -EINVAL);
1638 if (ealen == 0) { /* no logs transferred */
1644 mne_swab = !!ptlrpc_rep_need_swab(req);
1645 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
1646 /* This import flag means the server did an extra swab of IR MNE
1647 * records (fixed in LU-1252), reverse it here if needed. LU-1644 */
1648 if (unlikely(req->rq_import->imp_need_mne_swab))
1649 mne_swab = !mne_swab;
1652 for (i = 0; i < nrpages && ealen > 0; i++) {
1656 ptr = kmap(pages[i]);
1657 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1658 min_t(int, ealen, PAGE_CACHE_SIZE),
1662 CWARN("Process recover log %s error %d\n",
1663 cld->cld_logname, rc2);
1667 ealen -= PAGE_CACHE_SIZE;
1672 ptlrpc_req_finished(req);
1674 if (rc == 0 && !eof)
1678 for (i = 0; i < nrpages; i++) {
1679 if (pages[i] == NULL)
1681 __free_page(pages[i]);
1683 OBD_FREE(pages, sizeof(*pages) * nrpages);
1688 /* Copy a remote log locally */
1689 static int mgc_llog_local_copy(const struct lu_env *env,
1690 struct obd_device *obd,
1691 struct llog_ctxt *rctxt,
1692 struct llog_ctxt *lctxt, char *logname)
1700 * - copy it to backup using llog_backup()
1701 * - copy remote llog to logname using llog_backup()
1702 * - if failed then move bakup to logname again
1705 OBD_ALLOC(temp_log, strlen(logname) + 1);
1708 sprintf(temp_log, "%sT", logname);
1710 /* make a copy of local llog at first */
1711 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1712 if (rc < 0 && rc != -ENOENT)
1714 /* copy remote llog to the local copy */
1715 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1716 if (rc == -ENOENT) {
1717 /* no remote llog, delete local one too */
1718 llog_erase(env, lctxt, NULL, logname);
1719 } else if (rc < 0) {
1720 /* error during backup, get local one back from the copy */
1721 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1723 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1724 obd->obd_name, logname, rc);
1726 llog_erase(env, lctxt, NULL, temp_log);
1727 OBD_FREE(temp_log, strlen(logname) + 1);
1731 /* local_only means it cannot get remote llogs */
1732 static int mgc_process_cfg_log(struct obd_device *mgc,
1733 struct config_llog_data *cld, int local_only)
1735 struct llog_ctxt *ctxt, *lctxt = NULL;
1736 struct client_obd *cli = &mgc->u.cli;
1737 struct lustre_sb_info *lsi = NULL;
1739 bool sptlrpc_started = false;
1745 LASSERT(mutex_is_locked(&cld->cld_lock));
1748 * local copy of sptlrpc log is controlled elsewhere, don't try to
1751 if (cld_is_sptlrpc(cld) && local_only)
1754 if (cld->cld_cfg.cfg_sb)
1755 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1761 rc = lu_env_init(env, LCT_MG_THREAD);
1765 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1768 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1770 /* Copy the setup log locally if we can. Don't mess around if we're
1771 * running an MGS though (logs are already local). */
1772 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1773 cli->cl_mgc_configs_dir != NULL &&
1774 lu2dt_dev(cli->cl_mgc_configs_dir->do_lu.lo_dev) ==
1777 /* Only try to copy log if we have the lock. */
1778 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1780 if (local_only || rc) {
1781 if (strcmp(cld->cld_logname, PARAMS_FILENAME) != 0 &&
1782 llog_is_empty(env, lctxt, cld->cld_logname)) {
1783 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1784 "log %s and no local copy."
1785 "\n", cld->cld_logname);
1786 GOTO(out_pop, rc = -ENOENT);
1788 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1789 "copy for now, will try to update later.\n",
1793 /* Now, whether we copied or not, start using the local llog.
1794 * If we failed to copy, we'll start using whatever the old
1796 llog_ctxt_put(ctxt);
1800 if (local_only) /* no local log at client side */
1801 GOTO(out_pop, rc = -EIO);
1804 if (cld_is_sptlrpc(cld)) {
1805 sptlrpc_conf_log_update_begin(cld->cld_logname);
1806 sptlrpc_started = true;
1809 /* logname and instance info should be the same, so use our
1810 * copy of the instance for the update. The cfg_last_idx will
1811 * be updated here. */
1812 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1817 __llog_ctxt_put(env, ctxt);
1819 __llog_ctxt_put(env, lctxt);
1822 * update settings on existing OBDs. doing it inside
1823 * of llog_process_lock so no device is attaching/detaching
1825 * the logname must be <fsname>-sptlrpc
1827 if (sptlrpc_started) {
1828 LASSERT(cld_is_sptlrpc(cld));
1829 sptlrpc_conf_log_update_end(cld->cld_logname);
1830 class_notify_sptlrpc_conf(cld->cld_logname,
1831 strlen(cld->cld_logname) -
1832 strlen("-sptlrpc"));
1841 /** Get a config log from the MGS and process it.
1842 * This func is called for both clients and servers.
1843 * Copy the log locally before parsing it if appropriate (non-MGS server)
1845 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1847 struct lustre_handle lockh = { 0 };
1848 __u64 flags = LDLM_FL_NO_LRU;
1854 /* I don't want multiple processes running process_log at once --
1855 sounds like badness. It actually might be fine, as long as
1856 we're not trying to update from the same log
1857 simultaneously (in which case we should use a per-log sem.) */
1858 mutex_lock(&cld->cld_lock);
1859 if (cld->cld_stopping) {
1860 mutex_unlock(&cld->cld_lock);
1864 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1866 CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
1867 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1869 /* Get the cfg lock on the llog */
1870 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1871 LCK_CR, &flags, NULL, NULL, NULL,
1872 cld, 0, NULL, &lockh);
1874 /* Get the cld, it will be released in mgc_blocking_ast. */
1875 config_log_get(cld);
1876 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1879 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1881 /* mark cld_lostlock so that it will requeue
1882 * after MGC becomes available. */
1883 cld->cld_lostlock = 1;
1884 /* Get extra reference, it will be put in requeue thread */
1885 config_log_get(cld);
1889 if (cld_is_recover(cld)) {
1890 rc = 0; /* this is not a fatal error for recover log */
1892 rc = mgc_process_recover_log(mgc, cld);
1894 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1897 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1898 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1900 mutex_unlock(&cld->cld_lock);
1902 /* Now drop the lock so MGS can revoke it */
1904 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, LCK_CR, &lockh);
1906 CERROR("Can't drop cfg lock: %d\n", rcl);
1913 /** Called from lustre_process_log.
1914 * LCFG_LOG_START gets the config log from the MGS, processes it to start
1915 * any services, and adds it to the list logs to watch (follow).
1917 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1919 struct lustre_cfg *lcfg = buf;
1920 struct config_llog_instance *cfg = NULL;
1925 switch(lcfg->lcfg_command) {
1926 case LCFG_LOV_ADD_OBD: {
1927 /* Overloading this cfg command: register a new target */
1928 struct mgs_target_info *mti;
1930 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1931 sizeof(struct mgs_target_info))
1932 GOTO(out, rc = -EINVAL);
1934 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1935 CDEBUG(D_MGC, "add_target %s %#x\n",
1936 mti->mti_svname, mti->mti_flags);
1937 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1940 case LCFG_LOV_DEL_OBD:
1941 /* Unregister has no meaning at the moment. */
1942 CERROR("lov_del_obd unimplemented\n");
1945 case LCFG_SPTLRPC_CONF: {
1946 rc = sptlrpc_process_config(lcfg);
1949 case LCFG_LOG_START: {
1950 struct config_llog_data *cld;
1951 struct super_block *sb;
1953 logname = lustre_cfg_string(lcfg, 1);
1954 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1955 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1957 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1960 /* We're only called through here on the initial mount */
1961 rc = config_log_add(obd, logname, cfg, sb);
1964 cld = config_log_find(logname, cfg);
1971 /* FIXME only set this for old logs! Right now this forces
1972 us to always skip the "inside markers" check */
1973 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1975 rc = mgc_process_log(obd, cld);
1976 if (rc == 0 && cld->cld_recover != NULL) {
1977 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
1978 imp_connect_data, IMP_RECOV)) {
1979 rc = mgc_process_log(obd, cld->cld_recover);
1981 struct config_llog_data *cir = cld->cld_recover;
1982 cld->cld_recover = NULL;
1983 config_log_put(cir);
1986 CERROR("Cannot process recover llog %d\n", rc);
1989 if (rc == 0 && cld->cld_params != NULL) {
1990 rc = mgc_process_log(obd, cld->cld_params);
1991 if (rc == -ENOENT) {
1992 CDEBUG(D_MGC, "There is no params "
1993 "config file yet\n");
1996 /* params log is optional */
1998 CERROR("%s: can't process params llog: rc = %d\n",
2001 config_log_put(cld);
2005 case LCFG_LOG_END: {
2006 logname = lustre_cfg_string(lcfg, 1);
2008 if (lcfg->lcfg_bufcount >= 2)
2009 cfg = (struct config_llog_instance *)lustre_cfg_buf(
2011 rc = config_log_end(logname, cfg);
2015 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
2016 GOTO(out, rc = -EINVAL);
2024 struct obd_ops mgc_obd_ops = {
2025 .o_owner = THIS_MODULE,
2026 .o_setup = mgc_setup,
2027 .o_precleanup = mgc_precleanup,
2028 .o_cleanup = mgc_cleanup,
2029 .o_add_conn = client_import_add_conn,
2030 .o_del_conn = client_import_del_conn,
2031 .o_connect = client_connect_import,
2032 .o_disconnect = client_disconnect_export,
2033 .o_set_info_async = mgc_set_info_async,
2034 .o_get_info = mgc_get_info,
2035 .o_import_event = mgc_import_event,
2036 .o_process_config = mgc_process_config,
2039 int __init mgc_init(void)
2041 return class_register_type(&mgc_obd_ops, NULL, true, NULL,
2042 LUSTRE_MGC_NAME, NULL);
2045 static void /*__exit*/ mgc_exit(void)
2047 class_unregister_type(LUSTRE_MGC_NAME);
2050 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2051 MODULE_DESCRIPTION("Lustre Management Client");
2052 MODULE_LICENSE("GPL");
2054 module_init(mgc_init);
2055 module_exit(mgc_exit);