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 cfs_atomic_inc(&cld->cld_refcount);
124 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
125 cfs_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 cfs_atomic_read(&cld->cld_refcount));
137 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
139 /* spinlock to make sure no item with 0 refcount in the list */
140 if (cfs_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 cfs_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 cfs_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 *strrchr(seclogname, '-') = 0;
364 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
365 if (IS_ERR(recover_cld))
366 GOTO(out_err3, rc = PTR_ERR(recover_cld));
367 cld->cld_recover = recover_cld;
376 config_log_put(params_cld);
379 config_log_put(sptlrpc_cld);
385 DEFINE_MUTEX(llog_process_lock);
387 /** Stop watching for updates on this log.
389 static int config_log_end(char *logname, struct config_llog_instance *cfg)
391 struct config_llog_data *cld;
392 struct config_llog_data *cld_sptlrpc = NULL;
393 struct config_llog_data *cld_params = NULL;
394 struct config_llog_data *cld_recover = NULL;
398 cld = config_log_find(logname, cfg);
402 mutex_lock(&cld->cld_lock);
404 * if cld_stopping is set, it means we didn't start the log thus
405 * not owning the start ref. this can happen after previous umount:
406 * the cld still hanging there waiting for lock cancel, and we
407 * remount again but failed in the middle and call log_end without
410 if (unlikely(cld->cld_stopping)) {
411 mutex_unlock(&cld->cld_lock);
412 /* drop the ref from the find */
417 cld->cld_stopping = 1;
419 cld_recover = cld->cld_recover;
420 cld->cld_recover = NULL;
421 mutex_unlock(&cld->cld_lock);
424 mutex_lock(&cld_recover->cld_lock);
425 cld_recover->cld_stopping = 1;
426 mutex_unlock(&cld_recover->cld_lock);
427 config_log_put(cld_recover);
430 spin_lock(&config_list_lock);
431 cld_sptlrpc = cld->cld_sptlrpc;
432 cld->cld_sptlrpc = NULL;
433 cld_params = cld->cld_params;
434 cld->cld_params = NULL;
435 spin_unlock(&config_list_lock);
438 config_log_put(cld_sptlrpc);
441 mutex_lock(&cld_params->cld_lock);
442 cld_params->cld_stopping = 1;
443 mutex_unlock(&cld_params->cld_lock);
444 config_log_put(cld_params);
447 /* drop the ref from the find */
449 /* drop the start ref */
452 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
457 int lprocfs_mgc_rd_ir_state(char *page, char **start, off_t off,
458 int count, int *eof, void *data)
460 struct obd_device *obd = data;
461 struct obd_import *imp = obd->u.cli.cl_import;
462 struct obd_connect_data *ocd = &imp->imp_connect_data;
463 struct config_llog_data *cld;
467 rc = snprintf(page, count, "imperative_recovery: %s\n",
468 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
469 rc += snprintf(page + rc, count - rc, "client_state:\n");
471 spin_lock(&config_list_lock);
472 cfs_list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
473 if (cld->cld_recover == NULL)
475 rc += snprintf(page + rc, count - rc,
476 " - { client: %s, nidtbl_version: %u }\n",
478 cld->cld_recover->cld_cfg.cfg_last_idx);
480 spin_unlock(&config_list_lock);
485 /* reenqueue any lost locks */
486 #define RQ_RUNNING 0x1
490 static int rq_state = 0;
491 static cfs_waitq_t rq_waitq;
492 static DECLARE_COMPLETION(rq_exit);
494 static void do_requeue(struct config_llog_data *cld)
497 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
499 /* Do not run mgc_process_log on a disconnected export or an
500 export which is being disconnected. Take the client
501 semaphore to make the check non-racy. */
502 down_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
503 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
504 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
505 mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
507 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
510 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
515 /* this timeout represents how many seconds MGC should wait before
516 * requeue config and recover lock to the MGS. We need to randomize this
517 * in order to not flood the MGS.
519 #define MGC_TIMEOUT_MIN_SECONDS 5
520 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
522 static int mgc_requeue_thread(void *data)
527 CDEBUG(D_MGC, "Starting requeue thread\n");
529 /* Keep trying failed locks periodically */
530 spin_lock(&config_list_lock);
531 rq_state |= RQ_RUNNING;
533 struct l_wait_info lwi;
534 struct config_llog_data *cld, *cld_prev;
535 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
536 int stopped = !!(rq_state & RQ_STOP);
539 /* Any new or requeued lostlocks will change the state */
540 rq_state &= ~(RQ_NOW | RQ_LATER);
541 spin_unlock(&config_list_lock);
543 /* Always wait a few seconds to allow the server who
544 caused the lock revocation to finish its setup, plus some
545 random so everyone doesn't try to reconnect at once. */
546 to = MGC_TIMEOUT_MIN_SECONDS * HZ;
547 to += rand * HZ / 100; /* rand is centi-seconds */
548 lwi = LWI_TIMEOUT(to, NULL, NULL);
549 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi);
552 * iterate & processing through the list. for each cld, process
553 * its depending sptlrpc cld firstly (if any) and then itself.
555 * it's guaranteed any item in the list must have
556 * reference > 0; and if cld_lostlock is set, at
557 * least one reference is taken by the previous enqueue.
561 spin_lock(&config_list_lock);
562 cfs_list_for_each_entry(cld, &config_llog_list,
564 if (!cld->cld_lostlock)
567 spin_unlock(&config_list_lock);
569 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
571 /* Whether we enqueued again or not in mgc_process_log,
572 * we're done with the ref from the old enqueue */
574 config_log_put(cld_prev);
577 cld->cld_lostlock = 0;
578 if (likely(!stopped))
581 spin_lock(&config_list_lock);
583 spin_unlock(&config_list_lock);
585 config_log_put(cld_prev);
587 /* break after scanning the list so that we can drop
588 * refcount to losing lock clds */
589 if (unlikely(stopped)) {
590 spin_lock(&config_list_lock);
594 /* Wait a bit to see if anyone else needs a requeue */
595 lwi = (struct l_wait_info) { 0 };
596 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
598 spin_lock(&config_list_lock);
600 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
601 rq_state &= ~RQ_RUNNING;
602 spin_unlock(&config_list_lock);
606 CDEBUG(D_MGC, "Ending requeue thread\n");
610 /* Add a cld to the list to requeue. Start the requeue thread if needed.
611 We are responsible for dropping the config log reference from here on out. */
612 static void mgc_requeue_add(struct config_llog_data *cld)
616 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
617 cld->cld_logname, cfs_atomic_read(&cld->cld_refcount),
618 cld->cld_stopping, rq_state);
619 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
621 mutex_lock(&cld->cld_lock);
622 if (cld->cld_stopping || cld->cld_lostlock) {
623 mutex_unlock(&cld->cld_lock);
626 /* this refcount will be released in mgc_requeue_thread. */
628 cld->cld_lostlock = 1;
629 mutex_unlock(&cld->cld_lock);
631 /* Hold lock for rq_state */
632 spin_lock(&config_list_lock);
633 if (rq_state & RQ_STOP) {
634 spin_unlock(&config_list_lock);
635 cld->cld_lostlock = 0;
639 spin_unlock(&config_list_lock);
640 cfs_waitq_signal(&rq_waitq);
645 /********************** class fns **********************/
646 static int mgc_local_llog_init(const struct lu_env *env,
647 struct obd_device *obd,
648 struct obd_device *disk)
650 struct llog_ctxt *ctxt;
655 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
660 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
662 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
668 static int mgc_local_llog_fini(const struct lu_env *env,
669 struct obd_device *obd)
671 struct llog_ctxt *ctxt;
675 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
676 llog_cleanup(env, ctxt);
681 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb)
683 struct lustre_sb_info *lsi = s2lsi(sb);
684 struct client_obd *cli = &obd->u.cli;
685 struct lu_fid rfid, fid;
686 struct dt_object *root, *dto;
693 LASSERT(lsi->lsi_dt_dev);
699 /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
700 down(&cli->cl_mgc_sem);
702 cfs_cleanup_group_info();
704 /* Setup the configs dir */
705 rc = lu_env_init(env, LCT_MG_THREAD);
709 fid.f_seq = FID_SEQ_LOCAL_NAME;
712 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
717 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
721 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
722 &cli->cl_mgc_los->los_dev->dd_lu_dev);
723 if (unlikely(IS_ERR(root)))
724 GOTO(out_los, rc = PTR_ERR(root));
726 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
728 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
729 lu_object_put_nocache(env, &root->do_lu);
731 GOTO(out_los, rc = PTR_ERR(dto));
733 cli->cl_mgc_configs_dir = dto;
735 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
736 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
740 /* We take an obd ref to insure that we can't get to mgc_cleanup
741 * without calling mgc_fs_cleanup first. */
742 class_incref(obd, "mgc_fs", obd);
744 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
748 lu_object_put(env, &cli->cl_mgc_configs_dir->do_lu);
749 cli->cl_mgc_configs_dir = NULL;
753 local_oid_storage_fini(env, cli->cl_mgc_los);
754 cli->cl_mgc_los = NULL;
755 up(&cli->cl_mgc_sem);
764 static int mgc_fs_cleanup(struct obd_device *obd)
767 struct client_obd *cli = &obd->u.cli;
772 LASSERT(cli->cl_mgc_los != NULL);
774 rc = lu_env_init(&env, LCT_MG_THREAD);
778 mgc_local_llog_fini(&env, obd);
780 lu_object_put_nocache(&env, &cli->cl_mgc_configs_dir->do_lu);
781 cli->cl_mgc_configs_dir = NULL;
783 local_oid_storage_fini(&env, cli->cl_mgc_los);
784 cli->cl_mgc_los = NULL;
788 class_decref(obd, "mgc_fs", obd);
789 up(&cli->cl_mgc_sem);
794 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
796 struct llog_ctxt *ctxt;
801 /* setup only remote ctxt, the local disk context is switched per each
802 * filesystem during mgc_fs_setup() */
803 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
808 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
811 llog_initiator_connect(ctxt);
817 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
819 struct llog_ctxt *ctxt;
823 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
825 llog_cleanup(env, ctxt);
831 static cfs_atomic_t mgc_count = CFS_ATOMIC_INIT(0);
832 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
838 case OBD_CLEANUP_EARLY:
840 case OBD_CLEANUP_EXPORTS:
841 if (cfs_atomic_dec_and_test(&mgc_count)) {
843 /* stop requeue thread */
844 spin_lock(&config_list_lock);
845 running = rq_state & RQ_RUNNING;
848 spin_unlock(&config_list_lock);
850 cfs_waitq_signal(&rq_waitq);
851 wait_for_completion(&rq_exit);
854 obd_cleanup_client_import(obd);
855 rc = mgc_llog_fini(NULL, obd);
857 CERROR("failed to cleanup llogging subsystems\n");
863 static int mgc_cleanup(struct obd_device *obd)
868 /* COMPAT_146 - old config logs may have added profiles we don't
870 if (obd->obd_type->typ_refcnt <= 1)
871 /* Only for the last mgc */
872 class_del_profiles();
874 lprocfs_obd_cleanup(obd);
877 rc = client_obd_cleanup(obd);
881 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
883 struct lprocfs_static_vars lvars;
889 rc = client_obd_setup(obd, lcfg);
891 GOTO(err_decref, rc);
893 rc = mgc_llog_init(NULL, obd);
895 CERROR("failed to setup llogging subsystems\n");
896 GOTO(err_cleanup, rc);
899 lprocfs_mgc_init_vars(&lvars);
900 lprocfs_obd_setup(obd, lvars.obd_vars);
901 sptlrpc_lprocfs_cliobd_attach(obd);
903 if (cfs_atomic_inc_return(&mgc_count) == 1) {
905 cfs_waitq_init(&rq_waitq);
907 /* start requeue thread */
908 rc = PTR_ERR(kthread_run(mgc_requeue_thread, NULL,
910 if (IS_ERR_VALUE(rc)) {
911 CERROR("%s: Cannot start requeue thread (%d),"
912 "no more log updates!\n",
914 GOTO(err_cleanup, rc);
916 /* rc is the task_struct pointer of mgc_requeue_thread. */
923 client_obd_cleanup(obd);
929 /* based on ll_mdc_blocking_ast */
930 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
931 void *data, int flag)
933 struct lustre_handle lockh;
934 struct config_llog_data *cld = (struct config_llog_data *)data;
939 case LDLM_CB_BLOCKING:
940 /* mgs wants the lock, give it up... */
941 LDLM_DEBUG(lock, "MGC blocking CB");
942 ldlm_lock2handle(lock, &lockh);
943 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
945 case LDLM_CB_CANCELING:
946 /* We've given up the lock, prepare ourselves to update. */
947 LDLM_DEBUG(lock, "MGC cancel CB");
949 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
950 PLDLMRES(lock->l_resource),
951 (char *)&lock->l_resource->lr_name.name[0]);
954 CDEBUG(D_INFO, "missing data, won't requeue\n");
958 /* held at mgc_process_log(). */
959 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
960 /* Are we done with this log? */
961 if (cld->cld_stopping) {
962 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
967 /* Make sure not to re-enqueue when the mgc is stopping
968 (we get called from client_disconnect_export) */
969 if (!lock->l_conn_export ||
970 !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
971 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
978 mgc_requeue_add(cld);
988 /* Not sure where this should go... */
989 #define MGC_ENQUEUE_LIMIT 50
990 #define MGC_TARGET_REG_LIMIT 10
991 #define MGC_SEND_PARAM_LIMIT 10
993 /* Send parameter to MGS*/
994 static int mgc_set_mgs_param(struct obd_export *exp,
995 struct mgs_send_param *msp)
997 struct ptlrpc_request *req;
998 struct mgs_send_param *req_msp, *rep_msp;
1002 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1003 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
1008 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1010 ptlrpc_req_finished(req);
1014 memcpy(req_msp, msp, sizeof(*req_msp));
1015 ptlrpc_request_set_replen(req);
1017 /* Limit how long we will wait for the enqueue to complete */
1018 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
1019 rc = ptlrpc_queue_wait(req);
1021 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1022 memcpy(msp, rep_msp, sizeof(*rep_msp));
1025 ptlrpc_req_finished(req);
1030 /* Take a config lock so we can get cancel notifications */
1031 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
1032 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
1033 __u64 *flags, void *bl_cb, void *cp_cb, void *gl_cb,
1034 void *data, __u32 lvb_len, void *lvb_swabber,
1035 struct lustre_handle *lockh)
1037 struct config_llog_data *cld = (struct config_llog_data *)data;
1038 struct ldlm_enqueue_info einfo = {
1041 .ei_cb_bl = mgc_blocking_ast,
1042 .ei_cb_cp = ldlm_completion_ast,
1044 struct ptlrpc_request *req;
1045 int short_limit = cld_is_sptlrpc(cld);
1049 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
1050 cld->cld_resid.name[0]);
1052 /* We need a callback for every lockholder, so don't try to
1053 ldlm_lock_match (see rev 1.1.2.11.2.47) */
1054 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1055 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1060 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1061 ptlrpc_request_set_replen(req);
1063 /* check if this is server or client */
1064 if (cld->cld_cfg.cfg_sb) {
1065 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
1066 if (lsi && IS_SERVER(lsi))
1069 /* Limit how long we will wait for the enqueue to complete */
1070 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
1071 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1072 NULL, 0, LVB_T_NONE, lockh, 0);
1073 /* A failed enqueue should still call the mgc_blocking_ast,
1074 where it will be requeued if needed ("grant failed"). */
1075 ptlrpc_req_finished(req);
1079 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
1080 __u32 mode, struct lustre_handle *lockh)
1084 ldlm_lock_decref(lockh, mode);
1089 static void mgc_notify_active(struct obd_device *unused)
1091 /* wakeup mgc_requeue_thread to requeue mgc lock */
1092 spin_lock(&config_list_lock);
1094 spin_unlock(&config_list_lock);
1095 cfs_waitq_signal(&rq_waitq);
1097 /* TODO: Help the MGS rebuild nidtbl. -jay */
1100 /* Send target_reg message to MGS */
1101 static int mgc_target_register(struct obd_export *exp,
1102 struct mgs_target_info *mti)
1104 struct ptlrpc_request *req;
1105 struct mgs_target_info *req_mti, *rep_mti;
1109 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1110 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1115 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1117 ptlrpc_req_finished(req);
1121 memcpy(req_mti, mti, sizeof(*req_mti));
1122 ptlrpc_request_set_replen(req);
1123 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1124 /* Limit how long we will wait for the enqueue to complete */
1125 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1127 rc = ptlrpc_queue_wait(req);
1129 rep_mti = req_capsule_server_get(&req->rq_pill,
1130 &RMF_MGS_TARGET_INFO);
1131 memcpy(mti, rep_mti, sizeof(*rep_mti));
1132 CDEBUG(D_MGC, "register %s got index = %d\n",
1133 mti->mti_svname, mti->mti_stripe_index);
1135 ptlrpc_req_finished(req);
1140 int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1141 obd_count keylen, void *key, obd_count vallen,
1142 void *val, struct ptlrpc_request_set *set)
1147 /* Turn off initial_recov after we try all backup servers once */
1148 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1149 struct obd_import *imp = class_exp2cliimp(exp);
1151 if (vallen != sizeof(int))
1153 value = *(int *)val;
1154 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1155 imp->imp_obd->obd_name, value,
1156 imp->imp_deactive, imp->imp_invalid,
1157 imp->imp_replayable, imp->imp_obd->obd_replayable,
1158 ptlrpc_import_state_name(imp->imp_state));
1159 /* Resurrect if we previously died */
1160 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1161 imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1162 ptlrpc_reconnect_import(imp);
1165 /* FIXME move this to mgc_process_config */
1166 if (KEY_IS(KEY_REGISTER_TARGET)) {
1167 struct mgs_target_info *mti;
1168 if (vallen != sizeof(struct mgs_target_info))
1170 mti = (struct mgs_target_info *)val;
1171 CDEBUG(D_MGC, "register_target %s %#x\n",
1172 mti->mti_svname, mti->mti_flags);
1173 rc = mgc_target_register(exp, mti);
1176 if (KEY_IS(KEY_SET_FS)) {
1177 struct super_block *sb = (struct super_block *)val;
1179 if (vallen != sizeof(struct super_block))
1182 rc = mgc_fs_setup(exp->exp_obd, sb);
1185 if (KEY_IS(KEY_CLEAR_FS)) {
1188 rc = mgc_fs_cleanup(exp->exp_obd);
1191 if (KEY_IS(KEY_SET_INFO)) {
1192 struct mgs_send_param *msp;
1194 msp = (struct mgs_send_param *)val;
1195 rc = mgc_set_mgs_param(exp, msp);
1198 if (KEY_IS(KEY_MGSSEC)) {
1199 struct client_obd *cli = &exp->exp_obd->u.cli;
1200 struct sptlrpc_flavor flvr;
1203 * empty string means using current flavor, if which haven't
1204 * been set yet, set it as null.
1206 * if flavor has been set previously, check the asking flavor
1207 * must match the existing one.
1210 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1216 rc = sptlrpc_parse_flavor(val, &flvr);
1218 CERROR("invalid sptlrpc flavor %s to MGS\n",
1224 * caller already hold a mutex
1226 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1227 cli->cl_flvr_mgc = flvr;
1228 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1229 sizeof(flvr)) != 0) {
1232 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1234 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1235 "currently %s is in use\n",
1245 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1246 __u32 keylen, void *key, __u32 *vallen, void *val,
1247 struct lov_stripe_md *unused)
1251 if (KEY_IS(KEY_CONN_DATA)) {
1252 struct obd_import *imp = class_exp2cliimp(exp);
1253 struct obd_connect_data *data = val;
1255 if (*vallen == sizeof(*data)) {
1256 *data = imp->imp_connect_data;
1264 static int mgc_import_event(struct obd_device *obd,
1265 struct obd_import *imp,
1266 enum obd_import_event event)
1270 LASSERT(imp->imp_obd == obd);
1271 CDEBUG(D_MGC, "import event %#x\n", event);
1274 case IMP_EVENT_DISCON:
1275 /* MGC imports should not wait for recovery */
1276 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1277 ptlrpc_pinger_ir_down();
1279 case IMP_EVENT_INACTIVE:
1281 case IMP_EVENT_INVALIDATE: {
1282 struct ldlm_namespace *ns = obd->obd_namespace;
1283 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1286 case IMP_EVENT_ACTIVE:
1287 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1288 /* Clearing obd_no_recov allows us to continue pinging */
1289 obd->obd_no_recov = 0;
1290 mgc_notify_active(obd);
1291 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1292 ptlrpc_pinger_ir_up();
1296 case IMP_EVENT_DEACTIVATE:
1297 case IMP_EVENT_ACTIVATE:
1300 CERROR("Unknown import event %#x\n", event);
1307 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_CACHE_SHIFT),
1308 CONFIG_READ_NRPAGES = 4
1311 static int mgc_apply_recover_logs(struct obd_device *mgc,
1312 struct config_llog_data *cld,
1314 void *data, int datalen, bool mne_swab)
1316 struct config_llog_instance *cfg = &cld->cld_cfg;
1317 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1318 struct mgs_nidtbl_entry *entry;
1319 struct lustre_cfg *lcfg;
1320 struct lustre_cfg_bufs bufs;
1321 u64 prev_version = 0;
1330 LASSERT(cfg->cfg_instance != NULL);
1331 LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1333 OBD_ALLOC(inst, PAGE_CACHE_SIZE);
1337 if (!IS_SERVER(lsi)) {
1338 pos = snprintf(inst, PAGE_CACHE_SIZE, "%p", cfg->cfg_instance);
1339 if (pos >= PAGE_CACHE_SIZE) {
1340 OBD_FREE(inst, PAGE_CACHE_SIZE);
1344 LASSERT(IS_MDT(lsi));
1345 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1348 OBD_FREE(inst, PAGE_CACHE_SIZE);
1356 bufsz = PAGE_CACHE_SIZE - pos;
1358 while (datalen > 0) {
1359 int entry_len = sizeof(*entry);
1361 struct obd_device *obd;
1368 if (datalen < sizeof(*entry))
1371 entry = (typeof(entry))(data + off);
1374 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1376 if (entry->mne_nid_count == 0) /* at least one nid entry */
1378 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1381 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1382 if (datalen < entry_len) /* must have entry_len at least */
1385 /* Keep this swab for normal mixed endian handling. LU-1644 */
1387 lustre_swab_mgs_nidtbl_entry(entry);
1388 if (entry->mne_length > PAGE_CACHE_SIZE) {
1389 CERROR("MNE too large (%u)\n", entry->mne_length);
1393 if (entry->mne_length < entry_len)
1396 off += entry->mne_length;
1397 datalen -= entry->mne_length;
1401 if (entry->mne_version > max_version) {
1402 CERROR("entry index(%lld) is over max_index(%lld)\n",
1403 entry->mne_version, max_version);
1407 if (prev_version >= entry->mne_version) {
1408 CERROR("index unsorted, prev %lld, now %lld\n",
1409 prev_version, entry->mne_version);
1412 prev_version = entry->mne_version;
1415 * Write a string with format "nid::instance" to
1416 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1419 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1420 memset(buf, 0, bufsz);
1424 /* lustre-OST0001-osc-<instance #> */
1425 strcpy(obdname, cld->cld_logname);
1426 cname = strrchr(obdname, '-');
1427 if (cname == NULL) {
1428 CERROR("mgc %s: invalid logname %s\n",
1429 mgc->obd_name, obdname);
1433 pos = cname - obdname;
1435 pos += sprintf(obdname + pos, "-%s%04x",
1436 is_ost ? "OST" : "MDT", entry->mne_index);
1438 cname = is_ost ? "osc" : "mdc",
1439 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1440 lustre_cfg_bufs_reset(&bufs, obdname);
1442 /* find the obd by obdname */
1443 obd = class_name2obd(obdname);
1445 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1446 mgc->obd_name, obdname);
1448 /* this is a safe race, when the ost is starting up...*/
1452 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1455 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1458 down_read(&obd->u.cli.cl_sem);
1459 if (obd->u.cli.cl_import == NULL) {
1460 /* client does not connect to the OST yet */
1461 up_read(&obd->u.cli.cl_sem);
1466 /* TODO: iterate all nids to find one */
1467 /* find uuid by nid */
1468 rc = client_import_find_conn(obd->u.cli.cl_import,
1470 (struct obd_uuid *)uuid);
1471 up_read(&obd->u.cli.cl_sem);
1473 CERROR("mgc: cannot find uuid by nid %s\n",
1474 libcfs_nid2str(entry->u.nids[0]));
1478 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1479 uuid, libcfs_nid2str(entry->u.nids[0]));
1481 pos += strlen(uuid);
1482 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1483 LASSERT(pos < bufsz);
1485 lustre_cfg_bufs_set_string(&bufs, 1, params);
1488 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1490 CERROR("mgc: cannot allocate memory\n");
1494 CDEBUG(D_INFO, "ir apply logs "LPD64"/"LPD64" for %s -> %s\n",
1495 prev_version, max_version, obdname, params);
1497 rc = class_process_config(lcfg);
1498 lustre_cfg_free(lcfg);
1500 CDEBUG(D_INFO, "process config for %s error %d\n",
1503 /* continue, even one with error */
1506 OBD_FREE(inst, PAGE_CACHE_SIZE);
1511 * This function is called if this client was notified for target restarting
1512 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery logs.
1514 static int mgc_process_recover_log(struct obd_device *obd,
1515 struct config_llog_data *cld)
1517 struct ptlrpc_request *req = NULL;
1518 struct config_llog_instance *cfg = &cld->cld_cfg;
1519 struct mgs_config_body *body;
1520 struct mgs_config_res *res;
1521 struct ptlrpc_bulk_desc *desc;
1522 struct page **pages;
1525 bool mne_swab = false;
1531 /* allocate buffer for bulk transfer.
1532 * if this is the first time for this mgs to read logs,
1533 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1534 * once; otherwise, it only reads increment of logs, this should be
1535 * small and CONFIG_READ_NRPAGES will be used.
1537 nrpages = CONFIG_READ_NRPAGES;
1538 if (cfg->cfg_last_idx == 0) /* the first time */
1539 nrpages = CONFIG_READ_NRPAGES_INIT;
1541 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1543 GOTO(out, rc = -ENOMEM);
1545 for (i = 0; i < nrpages; i++) {
1546 pages[i] = alloc_page(GFP_IOFS);
1547 if (pages[i] == NULL)
1548 GOTO(out, rc = -ENOMEM);
1552 LASSERT(cld_is_recover(cld));
1553 LASSERT(mutex_is_locked(&cld->cld_lock));
1554 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1555 &RQF_MGS_CONFIG_READ);
1557 GOTO(out, rc = -ENOMEM);
1559 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1564 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1565 LASSERT(body != NULL);
1566 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1567 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1568 >= sizeof(body->mcb_name))
1569 GOTO(out, rc = -E2BIG);
1570 body->mcb_offset = cfg->cfg_last_idx + 1;
1571 body->mcb_type = cld->cld_type;
1572 body->mcb_bits = PAGE_CACHE_SHIFT;
1573 body->mcb_units = nrpages;
1575 /* allocate bulk transfer descriptor */
1576 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1, BULK_PUT_SINK,
1579 GOTO(out, rc = -ENOMEM);
1581 for (i = 0; i < nrpages; i++)
1582 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1584 ptlrpc_request_set_replen(req);
1585 rc = ptlrpc_queue_wait(req);
1589 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1590 if (res->mcr_size < res->mcr_offset)
1591 GOTO(out, rc = -EINVAL);
1593 /* always update the index even though it might have errors with
1594 * handling the recover logs */
1595 cfg->cfg_last_idx = res->mcr_offset;
1596 eof = res->mcr_offset == res->mcr_size;
1598 CDEBUG(D_INFO, "Latest version "LPD64", more %d.\n",
1599 res->mcr_offset, eof == false);
1601 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1603 GOTO(out, rc = ealen);
1605 if (ealen > nrpages << PAGE_CACHE_SHIFT)
1606 GOTO(out, rc = -EINVAL);
1608 if (ealen == 0) { /* no logs transferred */
1614 mne_swab = !!ptlrpc_rep_need_swab(req);
1615 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 50, 0)
1616 /* This import flag means the server did an extra swab of IR MNE
1617 * records (fixed in LU-1252), reverse it here if needed. LU-1644 */
1618 if (unlikely(req->rq_import->imp_need_mne_swab))
1619 mne_swab = !mne_swab;
1621 #warning "LU-1644: Remove old OBD_CONNECT_MNE_SWAB fixup and imp_need_mne_swab"
1624 for (i = 0; i < nrpages && ealen > 0; i++) {
1628 ptr = kmap(pages[i]);
1629 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1630 min_t(int, ealen, PAGE_CACHE_SIZE),
1634 CWARN("Process recover log %s error %d\n",
1635 cld->cld_logname, rc2);
1639 ealen -= PAGE_CACHE_SIZE;
1644 ptlrpc_req_finished(req);
1646 if (rc == 0 && !eof)
1650 for (i = 0; i < nrpages; i++) {
1651 if (pages[i] == NULL)
1653 __free_page(pages[i]);
1655 OBD_FREE(pages, sizeof(*pages) * nrpages);
1660 /* Copy a remote log locally */
1661 static int mgc_llog_local_copy(const struct lu_env *env,
1662 struct obd_device *obd,
1663 struct llog_ctxt *rctxt,
1664 struct llog_ctxt *lctxt, char *logname)
1672 * - copy it to backup using llog_backup()
1673 * - copy remote llog to logname using llog_backup()
1674 * - if failed then move bakup to logname again
1677 OBD_ALLOC(temp_log, strlen(logname) + 1);
1680 sprintf(temp_log, "%sT", logname);
1682 /* make a copy of local llog at first */
1683 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1684 if (rc < 0 && rc != -ENOENT)
1686 /* copy remote llog to the local copy */
1687 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1688 if (rc == -ENOENT) {
1689 /* no remote llog, delete local one too */
1690 llog_erase(env, lctxt, NULL, logname);
1691 } else if (rc < 0) {
1692 /* error during backup, get local one back from the copy */
1693 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1695 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1696 obd->obd_name, logname, rc);
1698 llog_erase(env, lctxt, NULL, temp_log);
1699 OBD_FREE(temp_log, strlen(logname) + 1);
1703 /* local_only means it cannot get remote llogs */
1704 static int mgc_process_cfg_log(struct obd_device *mgc,
1705 struct config_llog_data *cld, int local_only)
1707 struct llog_ctxt *ctxt, *lctxt = NULL;
1708 struct client_obd *cli = &mgc->u.cli;
1709 struct lustre_sb_info *lsi = NULL;
1711 bool sptlrpc_started = false;
1717 LASSERT(mutex_is_locked(&cld->cld_lock));
1720 * local copy of sptlrpc log is controlled elsewhere, don't try to
1723 if (cld_is_sptlrpc(cld) && local_only)
1726 if (cld->cld_cfg.cfg_sb)
1727 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1733 rc = lu_env_init(env, LCT_MG_THREAD);
1737 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1740 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1742 /* Copy the setup log locally if we can. Don't mess around if we're
1743 * running an MGS though (logs are already local). */
1744 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1745 cli->cl_mgc_configs_dir != NULL &&
1746 lu2dt_dev(cli->cl_mgc_configs_dir->do_lu.lo_dev) ==
1749 /* Only try to copy log if we have the lock. */
1750 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1752 if (local_only || rc) {
1753 if (llog_is_empty(env, lctxt, cld->cld_logname)) {
1754 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1755 "log %s and no local copy."
1756 "\n", cld->cld_logname);
1757 GOTO(out_pop, rc = -ENOENT);
1759 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1760 "copy for now, will try to update later.\n",
1763 /* Now, whether we copied or not, start using the local llog.
1764 * If we failed to copy, we'll start using whatever the old
1766 llog_ctxt_put(ctxt);
1770 if (local_only) /* no local log at client side */
1771 GOTO(out_pop, rc = -EIO);
1774 if (cld_is_sptlrpc(cld)) {
1775 sptlrpc_conf_log_update_begin(cld->cld_logname);
1776 sptlrpc_started = true;
1779 /* logname and instance info should be the same, so use our
1780 * copy of the instance for the update. The cfg_last_idx will
1781 * be updated here. */
1782 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1787 __llog_ctxt_put(env, ctxt);
1789 __llog_ctxt_put(env, lctxt);
1792 * update settings on existing OBDs. doing it inside
1793 * of llog_process_lock so no device is attaching/detaching
1795 * the logname must be <fsname>-sptlrpc
1797 if (sptlrpc_started) {
1798 LASSERT(cld_is_sptlrpc(cld));
1799 sptlrpc_conf_log_update_end(cld->cld_logname);
1800 class_notify_sptlrpc_conf(cld->cld_logname,
1801 strlen(cld->cld_logname) -
1802 strlen("-sptlrpc"));
1811 /** Get a config log from the MGS and process it.
1812 * This func is called for both clients and servers.
1813 * Copy the log locally before parsing it if appropriate (non-MGS server)
1815 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1817 struct lustre_handle lockh = { 0 };
1818 __u64 flags = LDLM_FL_NO_LRU;
1824 /* I don't want multiple processes running process_log at once --
1825 sounds like badness. It actually might be fine, as long as
1826 we're not trying to update from the same log
1827 simultaneously (in which case we should use a per-log sem.) */
1828 mutex_lock(&cld->cld_lock);
1829 if (cld->cld_stopping) {
1830 mutex_unlock(&cld->cld_lock);
1834 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1836 CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
1837 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1839 /* Get the cfg lock on the llog */
1840 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1841 LCK_CR, &flags, NULL, NULL, NULL,
1842 cld, 0, NULL, &lockh);
1844 /* Get the cld, it will be released in mgc_blocking_ast. */
1845 config_log_get(cld);
1846 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1849 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1851 /* mark cld_lostlock so that it will requeue
1852 * after MGC becomes available. */
1853 cld->cld_lostlock = 1;
1854 /* Get extra reference, it will be put in requeue thread */
1855 config_log_get(cld);
1859 if (cld_is_recover(cld)) {
1860 rc = 0; /* this is not a fatal error for recover log */
1862 rc = mgc_process_recover_log(mgc, cld);
1864 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1867 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1868 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1870 mutex_unlock(&cld->cld_lock);
1872 /* Now drop the lock so MGS can revoke it */
1874 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1877 CERROR("Can't drop cfg lock: %d\n", rcl);
1884 /** Called from lustre_process_log.
1885 * LCFG_LOG_START gets the config log from the MGS, processes it to start
1886 * any services, and adds it to the list logs to watch (follow).
1888 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1890 struct lustre_cfg *lcfg = buf;
1891 struct config_llog_instance *cfg = NULL;
1896 switch(lcfg->lcfg_command) {
1897 case LCFG_LOV_ADD_OBD: {
1898 /* Overloading this cfg command: register a new target */
1899 struct mgs_target_info *mti;
1901 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1902 sizeof(struct mgs_target_info))
1903 GOTO(out, rc = -EINVAL);
1905 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1906 CDEBUG(D_MGC, "add_target %s %#x\n",
1907 mti->mti_svname, mti->mti_flags);
1908 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1911 case LCFG_LOV_DEL_OBD:
1912 /* Unregister has no meaning at the moment. */
1913 CERROR("lov_del_obd unimplemented\n");
1916 case LCFG_SPTLRPC_CONF: {
1917 rc = sptlrpc_process_config(lcfg);
1920 case LCFG_LOG_START: {
1921 struct config_llog_data *cld;
1922 struct super_block *sb;
1924 logname = lustre_cfg_string(lcfg, 1);
1925 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1926 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1928 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1931 /* We're only called through here on the initial mount */
1932 rc = config_log_add(obd, logname, cfg, sb);
1935 cld = config_log_find(logname, cfg);
1942 /* FIXME only set this for old logs! Right now this forces
1943 us to always skip the "inside markers" check */
1944 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1946 rc = mgc_process_log(obd, cld);
1947 if (rc == 0 && cld->cld_recover != NULL) {
1948 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
1949 imp_connect_data, IMP_RECOV)) {
1950 rc = mgc_process_log(obd, cld->cld_recover);
1952 struct config_llog_data *cir = cld->cld_recover;
1953 cld->cld_recover = NULL;
1954 config_log_put(cir);
1957 CERROR("Cannot process recover llog %d\n", rc);
1960 if (rc == 0 && cld->cld_params != NULL) {
1961 rc = mgc_process_log(obd, cld->cld_params);
1962 if (rc == -ENOENT) {
1963 CDEBUG(D_MGC, "There is no params"
1964 "config file yet\n");
1967 /* params log is optional */
1969 CERROR("%s: can't process params llog: rc = %d\n",
1972 config_log_put(cld);
1976 case LCFG_LOG_END: {
1977 logname = lustre_cfg_string(lcfg, 1);
1979 if (lcfg->lcfg_bufcount >= 2)
1980 cfg = (struct config_llog_instance *)lustre_cfg_buf(
1982 rc = config_log_end(logname, cfg);
1986 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1987 GOTO(out, rc = -EINVAL);
1995 struct obd_ops mgc_obd_ops = {
1996 .o_owner = THIS_MODULE,
1997 .o_setup = mgc_setup,
1998 .o_precleanup = mgc_precleanup,
1999 .o_cleanup = mgc_cleanup,
2000 .o_add_conn = client_import_add_conn,
2001 .o_del_conn = client_import_del_conn,
2002 .o_connect = client_connect_import,
2003 .o_disconnect = client_disconnect_export,
2004 //.o_enqueue = mgc_enqueue,
2005 .o_cancel = mgc_cancel,
2006 //.o_iocontrol = mgc_iocontrol,
2007 .o_set_info_async = mgc_set_info_async,
2008 .o_get_info = mgc_get_info,
2009 .o_import_event = mgc_import_event,
2010 .o_process_config = mgc_process_config,
2013 int __init mgc_init(void)
2015 return class_register_type(&mgc_obd_ops, NULL, NULL,
2016 LUSTRE_MGC_NAME, NULL);
2020 static void /*__exit*/ mgc_exit(void)
2022 class_unregister_type(LUSTRE_MGC_NAME);
2025 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2026 MODULE_DESCRIPTION("Lustre Management Client");
2027 MODULE_LICENSE("GPL");
2029 module_init(mgc_init);
2030 module_exit(mgc_exit);