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 CFS_LIST_HEAD(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 cfs_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 cfs_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 cfs_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("sptlrpc log name not correct: %s", seclogname);
372 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
373 if (IS_ERR(recover_cld))
374 GOTO(out_err3, rc = PTR_ERR(recover_cld));
375 cld->cld_recover = recover_cld;
384 config_log_put(params_cld);
387 config_log_put(sptlrpc_cld);
393 DEFINE_MUTEX(llog_process_lock);
395 /** Stop watching for updates on this log.
397 static int config_log_end(char *logname, struct config_llog_instance *cfg)
399 struct config_llog_data *cld;
400 struct config_llog_data *cld_sptlrpc = NULL;
401 struct config_llog_data *cld_params = NULL;
402 struct config_llog_data *cld_recover = NULL;
406 cld = config_log_find(logname, cfg);
410 mutex_lock(&cld->cld_lock);
412 * if cld_stopping is set, it means we didn't start the log thus
413 * not owning the start ref. this can happen after previous umount:
414 * the cld still hanging there waiting for lock cancel, and we
415 * remount again but failed in the middle and call log_end without
418 if (unlikely(cld->cld_stopping)) {
419 mutex_unlock(&cld->cld_lock);
420 /* drop the ref from the find */
425 cld->cld_stopping = 1;
427 cld_recover = cld->cld_recover;
428 cld->cld_recover = NULL;
429 mutex_unlock(&cld->cld_lock);
432 mutex_lock(&cld_recover->cld_lock);
433 cld_recover->cld_stopping = 1;
434 mutex_unlock(&cld_recover->cld_lock);
435 config_log_put(cld_recover);
438 spin_lock(&config_list_lock);
439 cld_sptlrpc = cld->cld_sptlrpc;
440 cld->cld_sptlrpc = NULL;
441 cld_params = cld->cld_params;
442 cld->cld_params = NULL;
443 spin_unlock(&config_list_lock);
446 config_log_put(cld_sptlrpc);
449 mutex_lock(&cld_params->cld_lock);
450 cld_params->cld_stopping = 1;
451 mutex_unlock(&cld_params->cld_lock);
452 config_log_put(cld_params);
455 /* drop the ref from the find */
457 /* drop the start ref */
460 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
466 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
468 struct obd_device *obd = data;
469 struct obd_import *imp = obd->u.cli.cl_import;
470 struct obd_connect_data *ocd = &imp->imp_connect_data;
471 struct config_llog_data *cld;
474 seq_printf(m, "imperative_recovery: %s\n",
475 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
476 seq_printf(m, "client_state:\n");
478 spin_lock(&config_list_lock);
479 cfs_list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
480 if (cld->cld_recover == NULL)
482 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
484 cld->cld_recover->cld_cfg.cfg_last_idx);
486 spin_unlock(&config_list_lock);
492 /* reenqueue any lost locks */
493 #define RQ_RUNNING 0x1
497 static int rq_state = 0;
498 static wait_queue_head_t rq_waitq;
499 static DECLARE_COMPLETION(rq_exit);
501 static void do_requeue(struct config_llog_data *cld)
506 LASSERT(atomic_read(&cld->cld_refcount) > 0);
508 /* Do not run mgc_process_log on a disconnected export or an
509 * export which is being disconnected. Take the client
510 * semaphore to make the check non-racy. */
511 down_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
512 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
513 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
514 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
515 if (rc && rc != -ENOENT)
516 CERROR("failed processing log: %d\n", rc);
518 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
521 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
526 /* this timeout represents how many seconds MGC should wait before
527 * requeue config and recover lock to the MGS. We need to randomize this
528 * in order to not flood the MGS.
530 #define MGC_TIMEOUT_MIN_SECONDS 5
531 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
533 static int mgc_requeue_thread(void *data)
538 CDEBUG(D_MGC, "Starting requeue thread\n");
540 /* Keep trying failed locks periodically */
541 spin_lock(&config_list_lock);
542 rq_state |= RQ_RUNNING;
544 struct l_wait_info lwi;
545 struct config_llog_data *cld, *cld_prev;
546 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
547 int stopped = !!(rq_state & RQ_STOP);
550 /* Any new or requeued lostlocks will change the state */
551 rq_state &= ~(RQ_NOW | RQ_LATER);
552 spin_unlock(&config_list_lock);
554 /* Always wait a few seconds to allow the server who
555 caused the lock revocation to finish its setup, plus some
556 random so everyone doesn't try to reconnect at once. */
557 to = MGC_TIMEOUT_MIN_SECONDS * HZ;
558 to += rand * HZ / 100; /* rand is centi-seconds */
559 lwi = LWI_TIMEOUT(to, NULL, NULL);
560 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi);
563 * iterate & processing through the list. for each cld, process
564 * its depending sptlrpc cld firstly (if any) and then itself.
566 * it's guaranteed any item in the list must have
567 * reference > 0; and if cld_lostlock is set, at
568 * least one reference is taken by the previous enqueue.
572 spin_lock(&config_list_lock);
573 cfs_list_for_each_entry(cld, &config_llog_list,
575 if (!cld->cld_lostlock)
578 spin_unlock(&config_list_lock);
580 LASSERT(atomic_read(&cld->cld_refcount) > 0);
582 /* Whether we enqueued again or not in mgc_process_log,
583 * we're done with the ref from the old enqueue */
585 config_log_put(cld_prev);
588 cld->cld_lostlock = 0;
589 if (likely(!stopped))
592 spin_lock(&config_list_lock);
594 spin_unlock(&config_list_lock);
596 config_log_put(cld_prev);
598 /* break after scanning the list so that we can drop
599 * refcount to losing lock clds */
600 if (unlikely(stopped)) {
601 spin_lock(&config_list_lock);
605 /* Wait a bit to see if anyone else needs a requeue */
606 lwi = (struct l_wait_info) { 0 };
607 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
609 spin_lock(&config_list_lock);
611 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
612 rq_state &= ~RQ_RUNNING;
613 spin_unlock(&config_list_lock);
617 CDEBUG(D_MGC, "Ending requeue thread\n");
621 /* Add a cld to the list to requeue. Start the requeue thread if needed.
622 We are responsible for dropping the config log reference from here on out. */
623 static void mgc_requeue_add(struct config_llog_data *cld)
627 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
628 cld->cld_logname, atomic_read(&cld->cld_refcount),
629 cld->cld_stopping, rq_state);
630 LASSERT(atomic_read(&cld->cld_refcount) > 0);
632 mutex_lock(&cld->cld_lock);
633 if (cld->cld_stopping || cld->cld_lostlock) {
634 mutex_unlock(&cld->cld_lock);
637 /* this refcount will be released in mgc_requeue_thread. */
639 cld->cld_lostlock = 1;
640 mutex_unlock(&cld->cld_lock);
642 /* Hold lock for rq_state */
643 spin_lock(&config_list_lock);
644 if (rq_state & RQ_STOP) {
645 spin_unlock(&config_list_lock);
646 cld->cld_lostlock = 0;
650 spin_unlock(&config_list_lock);
656 /********************** class fns **********************/
657 static int mgc_local_llog_init(const struct lu_env *env,
658 struct obd_device *obd,
659 struct obd_device *disk)
661 struct llog_ctxt *ctxt;
666 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
671 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
673 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
679 static int mgc_local_llog_fini(const struct lu_env *env,
680 struct obd_device *obd)
682 struct llog_ctxt *ctxt;
686 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
687 llog_cleanup(env, ctxt);
692 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb)
694 struct lustre_sb_info *lsi = s2lsi(sb);
695 struct client_obd *cli = &obd->u.cli;
696 struct lu_fid rfid, fid;
697 struct dt_object *root, *dto;
704 LASSERT(lsi->lsi_dt_dev);
710 /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
711 down(&cli->cl_mgc_sem);
713 cfs_cleanup_group_info();
715 /* Setup the configs dir */
716 rc = lu_env_init(env, LCT_MG_THREAD);
720 fid.f_seq = FID_SEQ_LOCAL_NAME;
723 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
728 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
732 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
733 &cli->cl_mgc_los->los_dev->dd_lu_dev, NULL);
734 if (unlikely(IS_ERR(root)))
735 GOTO(out_los, rc = PTR_ERR(root));
737 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
739 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
740 lu_object_put_nocache(env, &root->do_lu);
742 GOTO(out_los, rc = PTR_ERR(dto));
744 cli->cl_mgc_configs_dir = dto;
746 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
747 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
751 /* We take an obd ref to insure that we can't get to mgc_cleanup
752 * without calling mgc_fs_cleanup first. */
753 class_incref(obd, "mgc_fs", obd);
755 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
759 lu_object_put(env, &cli->cl_mgc_configs_dir->do_lu);
760 cli->cl_mgc_configs_dir = NULL;
764 local_oid_storage_fini(env, cli->cl_mgc_los);
765 cli->cl_mgc_los = NULL;
766 up(&cli->cl_mgc_sem);
775 static int mgc_fs_cleanup(struct obd_device *obd)
778 struct client_obd *cli = &obd->u.cli;
783 LASSERT(cli->cl_mgc_los != NULL);
785 rc = lu_env_init(&env, LCT_MG_THREAD);
789 mgc_local_llog_fini(&env, obd);
791 lu_object_put_nocache(&env, &cli->cl_mgc_configs_dir->do_lu);
792 cli->cl_mgc_configs_dir = NULL;
794 local_oid_storage_fini(&env, cli->cl_mgc_los);
795 cli->cl_mgc_los = NULL;
799 class_decref(obd, "mgc_fs", obd);
800 up(&cli->cl_mgc_sem);
805 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
807 struct llog_ctxt *ctxt;
812 /* setup only remote ctxt, the local disk context is switched per each
813 * filesystem during mgc_fs_setup() */
814 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
819 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
822 llog_initiator_connect(ctxt);
828 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
830 struct llog_ctxt *ctxt;
834 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
836 llog_cleanup(env, ctxt);
842 static atomic_t mgc_count = ATOMIC_INIT(0);
843 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
849 case OBD_CLEANUP_EARLY:
851 case OBD_CLEANUP_EXPORTS:
852 if (atomic_dec_and_test(&mgc_count)) {
854 /* stop requeue thread */
855 spin_lock(&config_list_lock);
856 running = rq_state & RQ_RUNNING;
859 spin_unlock(&config_list_lock);
862 wait_for_completion(&rq_exit);
865 obd_cleanup_client_import(obd);
866 rc = mgc_llog_fini(NULL, obd);
868 CERROR("failed to cleanup llogging subsystems\n");
874 static int mgc_cleanup(struct obd_device *obd)
879 /* COMPAT_146 - old config logs may have added profiles we don't
881 if (obd->obd_type->typ_refcnt <= 1)
882 /* Only for the last mgc */
883 class_del_profiles();
885 lprocfs_obd_cleanup(obd);
888 rc = client_obd_cleanup(obd);
892 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
897 rc = ptlrpcd_addref();
901 rc = client_obd_setup(obd, lcfg);
903 GOTO(err_decref, rc);
905 rc = mgc_llog_init(NULL, obd);
907 CERROR("failed to setup llogging subsystems\n");
908 GOTO(err_cleanup, rc);
912 obd->obd_vars = lprocfs_mgc_obd_vars;
913 lprocfs_seq_obd_setup(obd);
915 sptlrpc_lprocfs_cliobd_attach(obd);
917 if (atomic_inc_return(&mgc_count) == 1) {
919 init_waitqueue_head(&rq_waitq);
921 /* start requeue thread */
922 rc = PTR_ERR(kthread_run(mgc_requeue_thread, NULL,
924 if (IS_ERR_VALUE(rc)) {
925 CERROR("%s: Cannot start requeue thread (%d),"
926 "no more log updates!\n",
928 GOTO(err_cleanup, rc);
930 /* rc is the task_struct pointer of mgc_requeue_thread. */
937 client_obd_cleanup(obd);
943 /* based on ll_mdc_blocking_ast */
944 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
945 void *data, int flag)
947 struct lustre_handle lockh;
948 struct config_llog_data *cld = (struct config_llog_data *)data;
953 case LDLM_CB_BLOCKING:
954 /* mgs wants the lock, give it up... */
955 LDLM_DEBUG(lock, "MGC blocking CB");
956 ldlm_lock2handle(lock, &lockh);
957 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
959 case LDLM_CB_CANCELING:
960 /* We've given up the lock, prepare ourselves to update. */
961 LDLM_DEBUG(lock, "MGC cancel CB");
963 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
964 PLDLMRES(lock->l_resource),
965 (char *)&lock->l_resource->lr_name.name[0]);
968 CDEBUG(D_INFO, "missing data, won't requeue\n");
972 /* held at mgc_process_log(). */
973 LASSERT(atomic_read(&cld->cld_refcount) > 0);
974 /* Are we done with this log? */
975 if (cld->cld_stopping) {
976 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
981 /* Make sure not to re-enqueue when the mgc is stopping
982 (we get called from client_disconnect_export) */
983 if (!lock->l_conn_export ||
984 !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
985 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
992 mgc_requeue_add(cld);
1002 /* Not sure where this should go... */
1003 #define MGC_ENQUEUE_LIMIT 50
1004 #define MGC_TARGET_REG_LIMIT 10
1005 #define MGC_SEND_PARAM_LIMIT 10
1007 /* Send parameter to MGS*/
1008 static int mgc_set_mgs_param(struct obd_export *exp,
1009 struct mgs_send_param *msp)
1011 struct ptlrpc_request *req;
1012 struct mgs_send_param *req_msp, *rep_msp;
1016 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1017 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
1022 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1024 ptlrpc_req_finished(req);
1028 memcpy(req_msp, msp, sizeof(*req_msp));
1029 ptlrpc_request_set_replen(req);
1031 /* Limit how long we will wait for the enqueue to complete */
1032 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
1033 rc = ptlrpc_queue_wait(req);
1035 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1036 memcpy(msp, rep_msp, sizeof(*rep_msp));
1039 ptlrpc_req_finished(req);
1044 /* Take a config lock so we can get cancel notifications */
1045 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
1046 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
1047 __u64 *flags, void *bl_cb, void *cp_cb, void *gl_cb,
1048 void *data, __u32 lvb_len, void *lvb_swabber,
1049 struct lustre_handle *lockh)
1051 struct config_llog_data *cld = (struct config_llog_data *)data;
1052 struct ldlm_enqueue_info einfo = {
1055 .ei_cb_bl = mgc_blocking_ast,
1056 .ei_cb_cp = ldlm_completion_ast,
1058 struct ptlrpc_request *req;
1059 int short_limit = cld_is_sptlrpc(cld);
1063 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
1064 cld->cld_resid.name[0]);
1066 /* We need a callback for every lockholder, so don't try to
1067 ldlm_lock_match (see rev 1.1.2.11.2.47) */
1068 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1069 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1074 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1075 ptlrpc_request_set_replen(req);
1077 /* check if this is server or client */
1078 if (cld->cld_cfg.cfg_sb) {
1079 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
1080 if (lsi && IS_SERVER(lsi))
1083 /* Limit how long we will wait for the enqueue to complete */
1084 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
1085 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1086 NULL, 0, LVB_T_NONE, lockh, 0);
1087 /* A failed enqueue should still call the mgc_blocking_ast,
1088 where it will be requeued if needed ("grant failed"). */
1089 ptlrpc_req_finished(req);
1093 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
1094 __u32 mode, struct lustre_handle *lockh)
1098 ldlm_lock_decref(lockh, mode);
1103 static void mgc_notify_active(struct obd_device *unused)
1105 /* wakeup mgc_requeue_thread to requeue mgc lock */
1106 spin_lock(&config_list_lock);
1108 spin_unlock(&config_list_lock);
1111 /* TODO: Help the MGS rebuild nidtbl. -jay */
1114 /* Send target_reg message to MGS */
1115 static int mgc_target_register(struct obd_export *exp,
1116 struct mgs_target_info *mti)
1118 struct ptlrpc_request *req;
1119 struct mgs_target_info *req_mti, *rep_mti;
1123 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1124 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1129 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1131 ptlrpc_req_finished(req);
1135 memcpy(req_mti, mti, sizeof(*req_mti));
1136 ptlrpc_request_set_replen(req);
1137 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1138 /* Limit how long we will wait for the enqueue to complete */
1139 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1141 rc = ptlrpc_queue_wait(req);
1143 rep_mti = req_capsule_server_get(&req->rq_pill,
1144 &RMF_MGS_TARGET_INFO);
1145 memcpy(mti, rep_mti, sizeof(*rep_mti));
1146 CDEBUG(D_MGC, "register %s got index = %d\n",
1147 mti->mti_svname, mti->mti_stripe_index);
1149 ptlrpc_req_finished(req);
1154 int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1155 obd_count keylen, void *key, obd_count vallen,
1156 void *val, struct ptlrpc_request_set *set)
1161 /* Turn off initial_recov after we try all backup servers once */
1162 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1163 struct obd_import *imp = class_exp2cliimp(exp);
1165 if (vallen != sizeof(int))
1167 value = *(int *)val;
1168 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1169 imp->imp_obd->obd_name, value,
1170 imp->imp_deactive, imp->imp_invalid,
1171 imp->imp_replayable, imp->imp_obd->obd_replayable,
1172 ptlrpc_import_state_name(imp->imp_state));
1173 /* Resurrect if we previously died */
1174 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1175 imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1176 ptlrpc_reconnect_import(imp);
1179 /* FIXME move this to mgc_process_config */
1180 if (KEY_IS(KEY_REGISTER_TARGET)) {
1181 struct mgs_target_info *mti;
1182 if (vallen != sizeof(struct mgs_target_info))
1184 mti = (struct mgs_target_info *)val;
1185 CDEBUG(D_MGC, "register_target %s %#x\n",
1186 mti->mti_svname, mti->mti_flags);
1187 rc = mgc_target_register(exp, mti);
1190 if (KEY_IS(KEY_SET_FS)) {
1191 struct super_block *sb = (struct super_block *)val;
1193 if (vallen != sizeof(struct super_block))
1196 rc = mgc_fs_setup(exp->exp_obd, sb);
1199 if (KEY_IS(KEY_CLEAR_FS)) {
1202 rc = mgc_fs_cleanup(exp->exp_obd);
1205 if (KEY_IS(KEY_SET_INFO)) {
1206 struct mgs_send_param *msp;
1208 msp = (struct mgs_send_param *)val;
1209 rc = mgc_set_mgs_param(exp, msp);
1212 if (KEY_IS(KEY_MGSSEC)) {
1213 struct client_obd *cli = &exp->exp_obd->u.cli;
1214 struct sptlrpc_flavor flvr;
1217 * empty string means using current flavor, if which haven't
1218 * been set yet, set it as null.
1220 * if flavor has been set previously, check the asking flavor
1221 * must match the existing one.
1224 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1230 rc = sptlrpc_parse_flavor(val, &flvr);
1232 CERROR("invalid sptlrpc flavor %s to MGS\n",
1238 * caller already hold a mutex
1240 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1241 cli->cl_flvr_mgc = flvr;
1242 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1243 sizeof(flvr)) != 0) {
1246 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1248 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1249 "currently %s is in use\n",
1259 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1260 __u32 keylen, void *key, __u32 *vallen, void *val,
1261 struct lov_stripe_md *unused)
1265 if (KEY_IS(KEY_CONN_DATA)) {
1266 struct obd_import *imp = class_exp2cliimp(exp);
1267 struct obd_connect_data *data = val;
1269 if (*vallen == sizeof(*data)) {
1270 *data = imp->imp_connect_data;
1278 static int mgc_import_event(struct obd_device *obd,
1279 struct obd_import *imp,
1280 enum obd_import_event event)
1284 LASSERT(imp->imp_obd == obd);
1285 CDEBUG(D_MGC, "import event %#x\n", event);
1288 case IMP_EVENT_DISCON:
1289 /* MGC imports should not wait for recovery */
1290 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1291 ptlrpc_pinger_ir_down();
1293 case IMP_EVENT_INACTIVE:
1295 case IMP_EVENT_INVALIDATE: {
1296 struct ldlm_namespace *ns = obd->obd_namespace;
1297 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1300 case IMP_EVENT_ACTIVE:
1301 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1302 /* Clearing obd_no_recov allows us to continue pinging */
1303 obd->obd_no_recov = 0;
1304 mgc_notify_active(obd);
1305 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1306 ptlrpc_pinger_ir_up();
1310 case IMP_EVENT_DEACTIVATE:
1311 case IMP_EVENT_ACTIVATE:
1314 CERROR("Unknown import event %#x\n", event);
1321 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_CACHE_SHIFT),
1322 CONFIG_READ_NRPAGES = 4
1325 static int mgc_apply_recover_logs(struct obd_device *mgc,
1326 struct config_llog_data *cld,
1328 void *data, int datalen, bool mne_swab)
1330 struct config_llog_instance *cfg = &cld->cld_cfg;
1331 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1332 struct mgs_nidtbl_entry *entry;
1333 struct lustre_cfg *lcfg;
1334 struct lustre_cfg_bufs bufs;
1335 u64 prev_version = 0;
1344 LASSERT(cfg->cfg_instance != NULL);
1345 LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1347 OBD_ALLOC(inst, PAGE_CACHE_SIZE);
1351 if (!IS_SERVER(lsi)) {
1352 pos = snprintf(inst, PAGE_CACHE_SIZE, "%p", cfg->cfg_instance);
1353 if (pos >= PAGE_CACHE_SIZE) {
1354 OBD_FREE(inst, PAGE_CACHE_SIZE);
1358 LASSERT(IS_MDT(lsi));
1359 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1362 OBD_FREE(inst, PAGE_CACHE_SIZE);
1370 bufsz = PAGE_CACHE_SIZE - pos;
1372 while (datalen > 0) {
1373 int entry_len = sizeof(*entry);
1375 struct obd_device *obd;
1382 if (datalen < sizeof(*entry))
1385 entry = (typeof(entry))(data + off);
1388 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1390 if (entry->mne_nid_count == 0) /* at least one nid entry */
1392 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1395 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1396 if (datalen < entry_len) /* must have entry_len at least */
1399 /* Keep this swab for normal mixed endian handling. LU-1644 */
1401 lustre_swab_mgs_nidtbl_entry(entry);
1402 if (entry->mne_length > PAGE_CACHE_SIZE) {
1403 CERROR("MNE too large (%u)\n", entry->mne_length);
1407 if (entry->mne_length < entry_len)
1410 off += entry->mne_length;
1411 datalen -= entry->mne_length;
1415 if (entry->mne_version > max_version) {
1416 CERROR("entry index(%lld) is over max_index(%lld)\n",
1417 entry->mne_version, max_version);
1421 if (prev_version >= entry->mne_version) {
1422 CERROR("index unsorted, prev %lld, now %lld\n",
1423 prev_version, entry->mne_version);
1426 prev_version = entry->mne_version;
1429 * Write a string with format "nid::instance" to
1430 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1433 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1434 memset(buf, 0, bufsz);
1438 /* lustre-OST0001-osc-<instance #> */
1439 strcpy(obdname, cld->cld_logname);
1440 cname = strrchr(obdname, '-');
1441 if (cname == NULL) {
1442 CERROR("mgc %s: invalid logname %s\n",
1443 mgc->obd_name, obdname);
1447 pos = cname - obdname;
1449 pos += sprintf(obdname + pos, "-%s%04x",
1450 is_ost ? "OST" : "MDT", entry->mne_index);
1452 cname = is_ost ? "osc" : "mdc",
1453 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1454 lustre_cfg_bufs_reset(&bufs, obdname);
1456 /* find the obd by obdname */
1457 obd = class_name2obd(obdname);
1459 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1460 mgc->obd_name, obdname);
1462 /* this is a safe race, when the ost is starting up...*/
1466 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1469 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1472 down_read(&obd->u.cli.cl_sem);
1473 if (obd->u.cli.cl_import == NULL) {
1474 /* client does not connect to the OST yet */
1475 up_read(&obd->u.cli.cl_sem);
1480 /* TODO: iterate all nids to find one */
1481 /* find uuid by nid */
1482 rc = client_import_find_conn(obd->u.cli.cl_import,
1484 (struct obd_uuid *)uuid);
1485 up_read(&obd->u.cli.cl_sem);
1487 CERROR("mgc: cannot find uuid by nid %s\n",
1488 libcfs_nid2str(entry->u.nids[0]));
1492 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1493 uuid, libcfs_nid2str(entry->u.nids[0]));
1495 pos += strlen(uuid);
1496 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1497 LASSERT(pos < bufsz);
1499 lustre_cfg_bufs_set_string(&bufs, 1, params);
1502 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1504 CERROR("mgc: cannot allocate memory\n");
1508 CDEBUG(D_INFO, "ir apply logs "LPD64"/"LPD64" for %s -> %s\n",
1509 prev_version, max_version, obdname, params);
1511 rc = class_process_config(lcfg);
1512 lustre_cfg_free(lcfg);
1514 CDEBUG(D_INFO, "process config for %s error %d\n",
1517 /* continue, even one with error */
1520 OBD_FREE(inst, PAGE_CACHE_SIZE);
1525 * This function is called if this client was notified for target restarting
1526 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery logs.
1528 static int mgc_process_recover_log(struct obd_device *obd,
1529 struct config_llog_data *cld)
1531 struct ptlrpc_request *req = NULL;
1532 struct config_llog_instance *cfg = &cld->cld_cfg;
1533 struct mgs_config_body *body;
1534 struct mgs_config_res *res;
1535 struct ptlrpc_bulk_desc *desc;
1536 struct page **pages;
1539 bool mne_swab = false;
1545 /* allocate buffer for bulk transfer.
1546 * if this is the first time for this mgs to read logs,
1547 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1548 * once; otherwise, it only reads increment of logs, this should be
1549 * small and CONFIG_READ_NRPAGES will be used.
1551 nrpages = CONFIG_READ_NRPAGES;
1552 if (cfg->cfg_last_idx == 0) /* the first time */
1553 nrpages = CONFIG_READ_NRPAGES_INIT;
1555 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1557 GOTO(out, rc = -ENOMEM);
1559 for (i = 0; i < nrpages; i++) {
1560 pages[i] = alloc_page(GFP_IOFS);
1561 if (pages[i] == NULL)
1562 GOTO(out, rc = -ENOMEM);
1566 LASSERT(cld_is_recover(cld));
1567 LASSERT(mutex_is_locked(&cld->cld_lock));
1568 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1569 &RQF_MGS_CONFIG_READ);
1571 GOTO(out, rc = -ENOMEM);
1573 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1578 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1579 LASSERT(body != NULL);
1580 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1581 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1582 >= sizeof(body->mcb_name))
1583 GOTO(out, rc = -E2BIG);
1584 body->mcb_offset = cfg->cfg_last_idx + 1;
1585 body->mcb_type = cld->cld_type;
1586 body->mcb_bits = PAGE_CACHE_SHIFT;
1587 body->mcb_units = nrpages;
1589 /* allocate bulk transfer descriptor */
1590 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1, BULK_PUT_SINK,
1593 GOTO(out, rc = -ENOMEM);
1595 for (i = 0; i < nrpages; i++)
1596 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1598 ptlrpc_request_set_replen(req);
1599 rc = ptlrpc_queue_wait(req);
1603 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1604 if (res->mcr_size < res->mcr_offset)
1605 GOTO(out, rc = -EINVAL);
1607 /* always update the index even though it might have errors with
1608 * handling the recover logs */
1609 cfg->cfg_last_idx = res->mcr_offset;
1610 eof = res->mcr_offset == res->mcr_size;
1612 CDEBUG(D_INFO, "Latest version "LPD64", more %d.\n",
1613 res->mcr_offset, eof == false);
1615 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1617 GOTO(out, rc = ealen);
1619 if (ealen > nrpages << PAGE_CACHE_SHIFT)
1620 GOTO(out, rc = -EINVAL);
1622 if (ealen == 0) { /* no logs transferred */
1628 mne_swab = !!ptlrpc_rep_need_swab(req);
1629 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 50, 0)
1630 /* This import flag means the server did an extra swab of IR MNE
1631 * records (fixed in LU-1252), reverse it here if needed. LU-1644 */
1632 if (unlikely(req->rq_import->imp_need_mne_swab))
1633 mne_swab = !mne_swab;
1635 #warning "LU-1644: Remove old OBD_CONNECT_MNE_SWAB fixup and imp_need_mne_swab"
1638 for (i = 0; i < nrpages && ealen > 0; i++) {
1642 ptr = kmap(pages[i]);
1643 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1644 min_t(int, ealen, PAGE_CACHE_SIZE),
1648 CWARN("Process recover log %s error %d\n",
1649 cld->cld_logname, rc2);
1653 ealen -= PAGE_CACHE_SIZE;
1658 ptlrpc_req_finished(req);
1660 if (rc == 0 && !eof)
1664 for (i = 0; i < nrpages; i++) {
1665 if (pages[i] == NULL)
1667 __free_page(pages[i]);
1669 OBD_FREE(pages, sizeof(*pages) * nrpages);
1674 /* Copy a remote log locally */
1675 static int mgc_llog_local_copy(const struct lu_env *env,
1676 struct obd_device *obd,
1677 struct llog_ctxt *rctxt,
1678 struct llog_ctxt *lctxt, char *logname)
1686 * - copy it to backup using llog_backup()
1687 * - copy remote llog to logname using llog_backup()
1688 * - if failed then move bakup to logname again
1691 OBD_ALLOC(temp_log, strlen(logname) + 1);
1694 sprintf(temp_log, "%sT", logname);
1696 /* make a copy of local llog at first */
1697 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1698 if (rc < 0 && rc != -ENOENT)
1700 /* copy remote llog to the local copy */
1701 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1702 if (rc == -ENOENT) {
1703 /* no remote llog, delete local one too */
1704 llog_erase(env, lctxt, NULL, logname);
1705 } else if (rc < 0) {
1706 /* error during backup, get local one back from the copy */
1707 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1709 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1710 obd->obd_name, logname, rc);
1712 llog_erase(env, lctxt, NULL, temp_log);
1713 OBD_FREE(temp_log, strlen(logname) + 1);
1717 /* local_only means it cannot get remote llogs */
1718 static int mgc_process_cfg_log(struct obd_device *mgc,
1719 struct config_llog_data *cld, int local_only)
1721 struct llog_ctxt *ctxt, *lctxt = NULL;
1722 struct client_obd *cli = &mgc->u.cli;
1723 struct lustre_sb_info *lsi = NULL;
1725 bool sptlrpc_started = false;
1731 LASSERT(mutex_is_locked(&cld->cld_lock));
1734 * local copy of sptlrpc log is controlled elsewhere, don't try to
1737 if (cld_is_sptlrpc(cld) && local_only)
1740 if (cld->cld_cfg.cfg_sb)
1741 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1747 rc = lu_env_init(env, LCT_MG_THREAD);
1751 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1754 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1756 /* Copy the setup log locally if we can. Don't mess around if we're
1757 * running an MGS though (logs are already local). */
1758 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1759 cli->cl_mgc_configs_dir != NULL &&
1760 lu2dt_dev(cli->cl_mgc_configs_dir->do_lu.lo_dev) ==
1763 /* Only try to copy log if we have the lock. */
1764 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1766 if (local_only || rc) {
1767 if (llog_is_empty(env, lctxt, cld->cld_logname)) {
1768 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1769 "log %s and no local copy."
1770 "\n", cld->cld_logname);
1771 GOTO(out_pop, rc = -ENOENT);
1773 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1774 "copy for now, will try to update later.\n",
1777 /* Now, whether we copied or not, start using the local llog.
1778 * If we failed to copy, we'll start using whatever the old
1780 llog_ctxt_put(ctxt);
1784 if (local_only) /* no local log at client side */
1785 GOTO(out_pop, rc = -EIO);
1788 if (cld_is_sptlrpc(cld)) {
1789 sptlrpc_conf_log_update_begin(cld->cld_logname);
1790 sptlrpc_started = true;
1793 /* logname and instance info should be the same, so use our
1794 * copy of the instance for the update. The cfg_last_idx will
1795 * be updated here. */
1796 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1801 __llog_ctxt_put(env, ctxt);
1803 __llog_ctxt_put(env, lctxt);
1806 * update settings on existing OBDs. doing it inside
1807 * of llog_process_lock so no device is attaching/detaching
1809 * the logname must be <fsname>-sptlrpc
1811 if (sptlrpc_started) {
1812 LASSERT(cld_is_sptlrpc(cld));
1813 sptlrpc_conf_log_update_end(cld->cld_logname);
1814 class_notify_sptlrpc_conf(cld->cld_logname,
1815 strlen(cld->cld_logname) -
1816 strlen("-sptlrpc"));
1825 /** Get a config log from the MGS and process it.
1826 * This func is called for both clients and servers.
1827 * Copy the log locally before parsing it if appropriate (non-MGS server)
1829 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1831 struct lustre_handle lockh = { 0 };
1832 __u64 flags = LDLM_FL_NO_LRU;
1838 /* I don't want multiple processes running process_log at once --
1839 sounds like badness. It actually might be fine, as long as
1840 we're not trying to update from the same log
1841 simultaneously (in which case we should use a per-log sem.) */
1842 mutex_lock(&cld->cld_lock);
1843 if (cld->cld_stopping) {
1844 mutex_unlock(&cld->cld_lock);
1848 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1850 CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
1851 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1853 /* Get the cfg lock on the llog */
1854 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1855 LCK_CR, &flags, NULL, NULL, NULL,
1856 cld, 0, NULL, &lockh);
1858 /* Get the cld, it will be released in mgc_blocking_ast. */
1859 config_log_get(cld);
1860 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1863 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1865 /* mark cld_lostlock so that it will requeue
1866 * after MGC becomes available. */
1867 cld->cld_lostlock = 1;
1868 /* Get extra reference, it will be put in requeue thread */
1869 config_log_get(cld);
1873 if (cld_is_recover(cld)) {
1874 rc = 0; /* this is not a fatal error for recover log */
1876 rc = mgc_process_recover_log(mgc, cld);
1878 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1881 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1882 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1884 mutex_unlock(&cld->cld_lock);
1886 /* Now drop the lock so MGS can revoke it */
1888 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1891 CERROR("Can't drop cfg lock: %d\n", rcl);
1898 /** Called from lustre_process_log.
1899 * LCFG_LOG_START gets the config log from the MGS, processes it to start
1900 * any services, and adds it to the list logs to watch (follow).
1902 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1904 struct lustre_cfg *lcfg = buf;
1905 struct config_llog_instance *cfg = NULL;
1910 switch(lcfg->lcfg_command) {
1911 case LCFG_LOV_ADD_OBD: {
1912 /* Overloading this cfg command: register a new target */
1913 struct mgs_target_info *mti;
1915 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1916 sizeof(struct mgs_target_info))
1917 GOTO(out, rc = -EINVAL);
1919 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1920 CDEBUG(D_MGC, "add_target %s %#x\n",
1921 mti->mti_svname, mti->mti_flags);
1922 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1925 case LCFG_LOV_DEL_OBD:
1926 /* Unregister has no meaning at the moment. */
1927 CERROR("lov_del_obd unimplemented\n");
1930 case LCFG_SPTLRPC_CONF: {
1931 rc = sptlrpc_process_config(lcfg);
1934 case LCFG_LOG_START: {
1935 struct config_llog_data *cld;
1936 struct super_block *sb;
1938 logname = lustre_cfg_string(lcfg, 1);
1939 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1940 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1942 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1945 /* We're only called through here on the initial mount */
1946 rc = config_log_add(obd, logname, cfg, sb);
1949 cld = config_log_find(logname, cfg);
1956 /* FIXME only set this for old logs! Right now this forces
1957 us to always skip the "inside markers" check */
1958 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1960 rc = mgc_process_log(obd, cld);
1961 if (rc == 0 && cld->cld_recover != NULL) {
1962 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
1963 imp_connect_data, IMP_RECOV)) {
1964 rc = mgc_process_log(obd, cld->cld_recover);
1966 struct config_llog_data *cir = cld->cld_recover;
1967 cld->cld_recover = NULL;
1968 config_log_put(cir);
1971 CERROR("Cannot process recover llog %d\n", rc);
1974 if (rc == 0 && cld->cld_params != NULL) {
1975 rc = mgc_process_log(obd, cld->cld_params);
1976 if (rc == -ENOENT) {
1977 CDEBUG(D_MGC, "There is no params"
1978 "config file yet\n");
1981 /* params log is optional */
1983 CERROR("%s: can't process params llog: rc = %d\n",
1986 config_log_put(cld);
1990 case LCFG_LOG_END: {
1991 logname = lustre_cfg_string(lcfg, 1);
1993 if (lcfg->lcfg_bufcount >= 2)
1994 cfg = (struct config_llog_instance *)lustre_cfg_buf(
1996 rc = config_log_end(logname, cfg);
2000 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
2001 GOTO(out, rc = -EINVAL);
2009 struct obd_ops mgc_obd_ops = {
2010 .o_owner = THIS_MODULE,
2011 .o_setup = mgc_setup,
2012 .o_precleanup = mgc_precleanup,
2013 .o_cleanup = mgc_cleanup,
2014 .o_add_conn = client_import_add_conn,
2015 .o_del_conn = client_import_del_conn,
2016 .o_connect = client_connect_import,
2017 .o_disconnect = client_disconnect_export,
2018 //.o_enqueue = mgc_enqueue,
2019 .o_cancel = mgc_cancel,
2020 //.o_iocontrol = mgc_iocontrol,
2021 .o_set_info_async = mgc_set_info_async,
2022 .o_get_info = mgc_get_info,
2023 .o_import_event = mgc_import_event,
2024 .o_process_config = mgc_process_config,
2027 int __init mgc_init(void)
2029 return class_register_type(&mgc_obd_ops, NULL, NULL,
2030 #ifndef HAVE_ONLY_PROCFS_SEQ
2033 LUSTRE_MGC_NAME, NULL);
2037 static void /*__exit*/ mgc_exit(void)
2039 class_unregister_type(LUSTRE_MGC_NAME);
2042 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2043 MODULE_DESCRIPTION("Lustre Management Client");
2044 MODULE_LICENSE("GPL");
2046 module_init(mgc_init);
2047 module_exit(mgc_exit);