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, 2012, 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 "../include/obd_class.h"
46 #include "../include/lustre_dlm.h"
47 #include "../include/lprocfs_status.h"
48 #include "../include/lustre_log.h"
49 #include "../include/lustre_disk.h"
50 #include "../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 %#llx/%#llx (%.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 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)
122 atomic_inc(&cld->cld_refcount);
123 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
124 atomic_read(&cld->cld_refcount));
128 /* Drop a reference to a config log. When no longer referenced,
129 we can free the config log data */
130 static void config_log_put(struct config_llog_data *cld)
132 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
133 atomic_read(&cld->cld_refcount));
134 LASSERT(atomic_read(&cld->cld_refcount) > 0);
136 /* spinlock to make sure no item with 0 refcount in the list */
137 if (atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
138 list_del(&cld->cld_list_chain);
139 spin_unlock(&config_list_lock);
141 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
143 if (cld->cld_recover)
144 config_log_put(cld->cld_recover);
145 if (cld->cld_sptlrpc)
146 config_log_put(cld->cld_sptlrpc);
148 config_log_put(cld->cld_params);
149 if (cld_is_sptlrpc(cld))
150 sptlrpc_conf_log_stop(cld->cld_logname);
152 class_export_put(cld->cld_mgcexp);
153 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
157 /* Find a config log by name */
159 struct config_llog_data *config_log_find(char *logname,
160 struct config_llog_instance *cfg)
162 struct config_llog_data *cld;
163 struct config_llog_data *found = NULL;
166 LASSERT(logname != NULL);
168 instance = cfg ? cfg->cfg_instance : NULL;
169 spin_lock(&config_list_lock);
170 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
171 /* check if instance equals */
172 if (instance != cld->cld_cfg.cfg_instance)
175 /* instance may be NULL, should check name */
176 if (strcmp(logname, cld->cld_logname) == 0) {
182 atomic_inc(&found->cld_refcount);
183 LASSERT(found->cld_stopping == 0 || cld_is_sptlrpc(found) == 0);
185 spin_unlock(&config_list_lock);
190 struct config_llog_data *do_config_log_add(struct obd_device *obd,
193 struct config_llog_instance *cfg,
194 struct super_block *sb)
196 struct config_llog_data *cld;
199 CDEBUG(D_MGC, "do adding config log %s:%p\n", logname,
200 cfg ? cfg->cfg_instance : NULL);
202 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
204 return ERR_PTR(-ENOMEM);
206 strcpy(cld->cld_logname, logname);
210 cld->cld_cfg.cfg_callback = class_config_llog_handler;
211 mutex_init(&cld->cld_lock);
212 cld->cld_cfg.cfg_last_idx = 0;
213 cld->cld_cfg.cfg_flags = 0;
214 cld->cld_cfg.cfg_sb = sb;
215 cld->cld_type = type;
216 atomic_set(&cld->cld_refcount, 1);
218 /* Keep the mgc around until we are done */
219 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
221 if (cld_is_sptlrpc(cld)) {
222 sptlrpc_conf_log_start(logname);
223 cld->cld_cfg.cfg_obdname = obd->obd_name;
226 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
228 spin_lock(&config_list_lock);
229 list_add(&cld->cld_list_chain, &config_llog_list);
230 spin_unlock(&config_list_lock);
237 if (cld_is_sptlrpc(cld)) {
238 rc = mgc_process_log(obd, cld);
239 if (rc && rc != -ENOENT)
240 CERROR("failed processing sptlrpc log: %d\n", rc);
246 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
248 struct config_llog_instance *cfg,
249 struct super_block *sb)
251 struct config_llog_instance lcfg = *cfg;
252 struct lustre_sb_info *lsi = s2lsi(sb);
253 struct config_llog_data *cld;
259 /* for osp-on-ost, see lustre_start_osp() */
260 if (IS_MDT(lsi) && lcfg.cfg_instance)
263 /* we have to use different llog for clients and mdts for cmd
264 * where only clients are notified if one of cmd server restarts */
265 LASSERT(strlen(fsname) < sizeof(logname) / 2);
266 strcpy(logname, fsname);
267 if (IS_SERVER(lsi)) { /* mdt */
268 LASSERT(lcfg.cfg_instance == NULL);
269 lcfg.cfg_instance = sb;
270 strcat(logname, "-mdtir");
272 LASSERT(lcfg.cfg_instance != NULL);
273 strcat(logname, "-cliir");
276 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
280 static struct config_llog_data *config_params_log_add(struct obd_device *obd,
281 struct config_llog_instance *cfg, struct super_block *sb)
283 struct config_llog_instance lcfg = *cfg;
284 struct config_llog_data *cld;
286 lcfg.cfg_instance = sb;
288 cld = do_config_log_add(obd, PARAMS_FILENAME, CONFIG_T_PARAMS,
294 /** Add this log to the list of active logs watched by an MGC.
295 * Active means we're watching for updates.
296 * We have one active log per "mount" - client instance or servername.
297 * Each instance may be at a different point in the log.
299 static int config_log_add(struct obd_device *obd, char *logname,
300 struct config_llog_instance *cfg,
301 struct super_block *sb)
303 struct lustre_sb_info *lsi = s2lsi(sb);
304 struct config_llog_data *cld;
305 struct config_llog_data *sptlrpc_cld;
306 struct config_llog_data *params_cld;
311 CDEBUG(D_MGC, "adding config log %s:%p\n", logname, cfg->cfg_instance);
314 * for each regular log, the depended sptlrpc log name is
315 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
317 ptr = strrchr(logname, '-');
318 if (ptr == NULL || ptr - logname > 8) {
319 CERROR("logname %s is too long\n", logname);
323 memcpy(seclogname, logname, ptr - logname);
324 strcpy(seclogname + (ptr - logname), "-sptlrpc");
326 sptlrpc_cld = config_log_find(seclogname, NULL);
327 if (sptlrpc_cld == NULL) {
328 sptlrpc_cld = do_config_log_add(obd, seclogname,
329 CONFIG_T_SPTLRPC, NULL, NULL);
330 if (IS_ERR(sptlrpc_cld)) {
331 CERROR("can't create sptlrpc log: %s\n", seclogname);
332 GOTO(out_err, rc = PTR_ERR(sptlrpc_cld));
335 params_cld = config_params_log_add(obd, cfg, sb);
336 if (IS_ERR(params_cld)) {
337 rc = PTR_ERR(params_cld);
338 CERROR("%s: can't create params log: rc = %d\n",
343 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
345 CERROR("can't create log: %s\n", logname);
346 GOTO(out_err2, rc = PTR_ERR(cld));
349 cld->cld_sptlrpc = sptlrpc_cld;
350 cld->cld_params = params_cld;
352 LASSERT(lsi->lsi_lmd);
353 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)) {
354 struct config_llog_data *recover_cld;
355 *strrchr(seclogname, '-') = 0;
356 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
357 if (IS_ERR(recover_cld))
358 GOTO(out_err3, rc = PTR_ERR(recover_cld));
359 cld->cld_recover = recover_cld;
368 config_log_put(params_cld);
371 config_log_put(sptlrpc_cld);
377 DEFINE_MUTEX(llog_process_lock);
379 /** Stop watching for updates on this log.
381 static int config_log_end(char *logname, struct config_llog_instance *cfg)
383 struct config_llog_data *cld;
384 struct config_llog_data *cld_sptlrpc = NULL;
385 struct config_llog_data *cld_params = NULL;
386 struct config_llog_data *cld_recover = NULL;
389 cld = config_log_find(logname, cfg);
393 mutex_lock(&cld->cld_lock);
395 * if cld_stopping is set, it means we didn't start the log thus
396 * not owning the start ref. this can happen after previous umount:
397 * the cld still hanging there waiting for lock cancel, and we
398 * remount again but failed in the middle and call log_end without
401 if (unlikely(cld->cld_stopping)) {
402 mutex_unlock(&cld->cld_lock);
403 /* drop the ref from the find */
408 cld->cld_stopping = 1;
410 cld_recover = cld->cld_recover;
411 cld->cld_recover = NULL;
412 mutex_unlock(&cld->cld_lock);
415 mutex_lock(&cld_recover->cld_lock);
416 cld_recover->cld_stopping = 1;
417 mutex_unlock(&cld_recover->cld_lock);
418 config_log_put(cld_recover);
421 spin_lock(&config_list_lock);
422 cld_sptlrpc = cld->cld_sptlrpc;
423 cld->cld_sptlrpc = NULL;
424 cld_params = cld->cld_params;
425 cld->cld_params = NULL;
426 spin_unlock(&config_list_lock);
429 config_log_put(cld_sptlrpc);
432 mutex_lock(&cld_params->cld_lock);
433 cld_params->cld_stopping = 1;
434 mutex_unlock(&cld_params->cld_lock);
435 config_log_put(cld_params);
438 /* drop the ref from the find */
440 /* drop the start ref */
443 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
448 #if defined (CONFIG_PROC_FS)
449 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
451 struct obd_device *obd = data;
452 struct obd_import *imp = obd->u.cli.cl_import;
453 struct obd_connect_data *ocd = &imp->imp_connect_data;
454 struct config_llog_data *cld;
456 seq_printf(m, "imperative_recovery: %s\n",
457 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
458 seq_printf(m, "client_state:\n");
460 spin_lock(&config_list_lock);
461 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
462 if (cld->cld_recover == NULL)
464 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
466 cld->cld_recover->cld_cfg.cfg_last_idx);
468 spin_unlock(&config_list_lock);
474 /* reenqueue any lost locks */
475 #define RQ_RUNNING 0x1
479 static int rq_state = 0;
480 static wait_queue_head_t rq_waitq;
481 static DECLARE_COMPLETION(rq_exit);
483 static void do_requeue(struct config_llog_data *cld)
485 LASSERT(atomic_read(&cld->cld_refcount) > 0);
487 /* Do not run mgc_process_log on a disconnected export or an
488 export which is being disconnected. Take the client
489 semaphore to make the check non-racy. */
490 down_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
491 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
492 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
493 mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
495 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
498 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
501 /* this timeout represents how many seconds MGC should wait before
502 * requeue config and recover lock to the MGS. We need to randomize this
503 * in order to not flood the MGS.
505 #define MGC_TIMEOUT_MIN_SECONDS 5
506 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
508 static int mgc_requeue_thread(void *data)
512 CDEBUG(D_MGC, "Starting requeue thread\n");
514 /* Keep trying failed locks periodically */
515 spin_lock(&config_list_lock);
516 rq_state |= RQ_RUNNING;
518 struct l_wait_info lwi;
519 struct config_llog_data *cld, *cld_prev;
520 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
521 int stopped = !!(rq_state & RQ_STOP);
524 /* Any new or requeued lostlocks will change the state */
525 rq_state &= ~(RQ_NOW | RQ_LATER);
526 spin_unlock(&config_list_lock);
528 /* Always wait a few seconds to allow the server who
529 caused the lock revocation to finish its setup, plus some
530 random so everyone doesn't try to reconnect at once. */
531 to = MGC_TIMEOUT_MIN_SECONDS * HZ;
532 to += rand * HZ / 100; /* rand is centi-seconds */
533 lwi = LWI_TIMEOUT(to, NULL, NULL);
534 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi);
537 * iterate & processing through the list. for each cld, process
538 * its depending sptlrpc cld firstly (if any) and then itself.
540 * it's guaranteed any item in the list must have
541 * reference > 0; and if cld_lostlock is set, at
542 * least one reference is taken by the previous enqueue.
546 spin_lock(&config_list_lock);
547 list_for_each_entry(cld, &config_llog_list,
549 if (!cld->cld_lostlock)
552 spin_unlock(&config_list_lock);
554 LASSERT(atomic_read(&cld->cld_refcount) > 0);
556 /* Whether we enqueued again or not in mgc_process_log,
557 * we're done with the ref from the old enqueue */
559 config_log_put(cld_prev);
562 cld->cld_lostlock = 0;
563 if (likely(!stopped))
566 spin_lock(&config_list_lock);
568 spin_unlock(&config_list_lock);
570 config_log_put(cld_prev);
572 /* break after scanning the list so that we can drop
573 * refcount to losing lock clds */
574 if (unlikely(stopped)) {
575 spin_lock(&config_list_lock);
579 /* Wait a bit to see if anyone else needs a requeue */
580 lwi = (struct l_wait_info) { 0 };
581 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
583 spin_lock(&config_list_lock);
585 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
586 rq_state &= ~RQ_RUNNING;
587 spin_unlock(&config_list_lock);
591 CDEBUG(D_MGC, "Ending requeue thread\n");
595 /* Add a cld to the list to requeue. Start the requeue thread if needed.
596 We are responsible for dropping the config log reference from here on out. */
597 static void mgc_requeue_add(struct config_llog_data *cld)
599 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
600 cld->cld_logname, atomic_read(&cld->cld_refcount),
601 cld->cld_stopping, rq_state);
602 LASSERT(atomic_read(&cld->cld_refcount) > 0);
604 mutex_lock(&cld->cld_lock);
605 if (cld->cld_stopping || cld->cld_lostlock) {
606 mutex_unlock(&cld->cld_lock);
609 /* this refcount will be released in mgc_requeue_thread. */
611 cld->cld_lostlock = 1;
612 mutex_unlock(&cld->cld_lock);
614 /* Hold lock for rq_state */
615 spin_lock(&config_list_lock);
616 if (rq_state & RQ_STOP) {
617 spin_unlock(&config_list_lock);
618 cld->cld_lostlock = 0;
622 spin_unlock(&config_list_lock);
627 /********************** class fns **********************/
628 static int mgc_local_llog_init(const struct lu_env *env,
629 struct obd_device *obd,
630 struct obd_device *disk)
632 struct llog_ctxt *ctxt;
635 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
640 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
642 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
648 static int mgc_local_llog_fini(const struct lu_env *env,
649 struct obd_device *obd)
651 struct llog_ctxt *ctxt;
653 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
654 llog_cleanup(env, ctxt);
659 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb)
661 struct lustre_sb_info *lsi = s2lsi(sb);
662 struct client_obd *cli = &obd->u.cli;
663 struct lu_fid rfid, fid;
664 struct dt_object *root, *dto;
669 LASSERT(lsi->lsi_dt_dev);
675 /* The mgc fs exclusion mutex. Only one fs can be setup at a time. */
676 mutex_lock(&cli->cl_mgc_mutex);
678 cfs_cleanup_group_info();
680 /* Setup the configs dir */
681 rc = lu_env_init(env, LCT_MG_THREAD);
685 fid.f_seq = FID_SEQ_LOCAL_NAME;
688 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
693 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
697 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
698 &cli->cl_mgc_los->los_dev->dd_lu_dev);
699 if (unlikely(IS_ERR(root)))
700 GOTO(out_los, rc = PTR_ERR(root));
702 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
704 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
705 lu_object_put_nocache(env, &root->do_lu);
707 GOTO(out_los, rc = PTR_ERR(dto));
709 cli->cl_mgc_configs_dir = dto;
711 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
712 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
716 /* We take an obd ref to insure that we can't get to mgc_cleanup
717 * without calling mgc_fs_cleanup first. */
718 class_incref(obd, "mgc_fs", obd);
720 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
723 lu_object_put(env, &cli->cl_mgc_configs_dir->do_lu);
724 cli->cl_mgc_configs_dir = NULL;
728 local_oid_storage_fini(env, cli->cl_mgc_los);
729 cli->cl_mgc_los = NULL;
730 mutex_unlock(&cli->cl_mgc_mutex);
739 static int mgc_fs_cleanup(struct obd_device *obd)
742 struct client_obd *cli = &obd->u.cli;
745 LASSERT(cli->cl_mgc_los != NULL);
747 rc = lu_env_init(&env, LCT_MG_THREAD);
751 mgc_local_llog_fini(&env, obd);
753 lu_object_put_nocache(&env, &cli->cl_mgc_configs_dir->do_lu);
754 cli->cl_mgc_configs_dir = NULL;
756 local_oid_storage_fini(&env, cli->cl_mgc_los);
757 cli->cl_mgc_los = NULL;
761 class_decref(obd, "mgc_fs", obd);
762 mutex_unlock(&cli->cl_mgc_mutex);
767 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
769 struct llog_ctxt *ctxt;
772 /* setup only remote ctxt, the local disk context is switched per each
773 * filesystem during mgc_fs_setup() */
774 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
779 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
782 llog_initiator_connect(ctxt);
788 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
790 struct llog_ctxt *ctxt;
792 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
794 llog_cleanup(env, ctxt);
799 static atomic_t mgc_count = ATOMIC_INIT(0);
800 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
805 case OBD_CLEANUP_EARLY:
807 case OBD_CLEANUP_EXPORTS:
808 if (atomic_dec_and_test(&mgc_count)) {
810 /* stop requeue thread */
811 spin_lock(&config_list_lock);
812 running = rq_state & RQ_RUNNING;
815 spin_unlock(&config_list_lock);
818 wait_for_completion(&rq_exit);
821 obd_cleanup_client_import(obd);
822 rc = mgc_llog_fini(NULL, obd);
824 CERROR("failed to cleanup llogging subsystems\n");
830 static int mgc_cleanup(struct obd_device *obd)
834 /* COMPAT_146 - old config logs may have added profiles we don't
836 if (obd->obd_type->typ_refcnt <= 1)
837 /* Only for the last mgc */
838 class_del_profiles();
840 lprocfs_obd_cleanup(obd);
843 rc = client_obd_cleanup(obd);
847 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
849 struct lprocfs_static_vars lvars;
854 rc = client_obd_setup(obd, lcfg);
856 GOTO(err_decref, rc);
858 rc = mgc_llog_init(NULL, obd);
860 CERROR("failed to setup llogging subsystems\n");
861 GOTO(err_cleanup, rc);
864 lprocfs_mgc_init_vars(&lvars);
865 lprocfs_obd_setup(obd, lvars.obd_vars);
866 sptlrpc_lprocfs_cliobd_attach(obd);
868 if (atomic_inc_return(&mgc_count) == 1) {
870 init_waitqueue_head(&rq_waitq);
872 /* start requeue thread */
873 rc = PTR_ERR(kthread_run(mgc_requeue_thread, NULL,
875 if (IS_ERR_VALUE(rc)) {
876 CERROR("%s: Cannot start requeue thread (%d),"
877 "no more log updates!\n",
879 GOTO(err_cleanup, rc);
881 /* rc is the task_struct pointer of mgc_requeue_thread. */
888 client_obd_cleanup(obd);
894 /* based on ll_mdc_blocking_ast */
895 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
896 void *data, int flag)
898 struct lustre_handle lockh;
899 struct config_llog_data *cld = (struct config_llog_data *)data;
903 case LDLM_CB_BLOCKING:
904 /* mgs wants the lock, give it up... */
905 LDLM_DEBUG(lock, "MGC blocking CB");
906 ldlm_lock2handle(lock, &lockh);
907 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
909 case LDLM_CB_CANCELING:
910 /* We've given up the lock, prepare ourselves to update. */
911 LDLM_DEBUG(lock, "MGC cancel CB");
913 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
914 PLDLMRES(lock->l_resource),
915 (char *)&lock->l_resource->lr_name.name[0]);
918 CDEBUG(D_INFO, "missing data, won't requeue\n");
922 /* held at mgc_process_log(). */
923 LASSERT(atomic_read(&cld->cld_refcount) > 0);
924 /* Are we done with this log? */
925 if (cld->cld_stopping) {
926 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
931 /* Make sure not to re-enqueue when the mgc is stopping
932 (we get called from client_disconnect_export) */
933 if (!lock->l_conn_export ||
934 !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
935 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
942 mgc_requeue_add(cld);
952 /* Not sure where this should go... */
953 /* This is the timeout value for MGS_CONNECT request plus a ping interval, such
954 * that we can have a chance to try the secondary MGS if any. */
955 #define MGC_ENQUEUE_LIMIT (INITIAL_CONNECT_TIMEOUT + (AT_OFF ? 0 : at_min) \
957 #define MGC_TARGET_REG_LIMIT 10
958 #define MGC_SEND_PARAM_LIMIT 10
960 /* Send parameter to MGS*/
961 static int mgc_set_mgs_param(struct obd_export *exp,
962 struct mgs_send_param *msp)
964 struct ptlrpc_request *req;
965 struct mgs_send_param *req_msp, *rep_msp;
968 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
969 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
974 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
976 ptlrpc_req_finished(req);
980 memcpy(req_msp, msp, sizeof(*req_msp));
981 ptlrpc_request_set_replen(req);
983 /* Limit how long we will wait for the enqueue to complete */
984 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
985 rc = ptlrpc_queue_wait(req);
987 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
988 memcpy(msp, rep_msp, sizeof(*rep_msp));
991 ptlrpc_req_finished(req);
996 /* Take a config lock so we can get cancel notifications */
997 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
998 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
999 __u64 *flags, void *bl_cb, void *cp_cb, void *gl_cb,
1000 void *data, __u32 lvb_len, void *lvb_swabber,
1001 struct lustre_handle *lockh)
1003 struct config_llog_data *cld = (struct config_llog_data *)data;
1004 struct ldlm_enqueue_info einfo = {
1007 .ei_cb_bl = mgc_blocking_ast,
1008 .ei_cb_cp = ldlm_completion_ast,
1010 struct ptlrpc_request *req;
1011 int short_limit = cld_is_sptlrpc(cld);
1014 CDEBUG(D_MGC, "Enqueue for %s (res %#llx)\n", cld->cld_logname,
1015 cld->cld_resid.name[0]);
1017 /* We need a callback for every lockholder, so don't try to
1018 ldlm_lock_match (see rev 1.1.2.11.2.47) */
1019 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1020 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1025 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1026 ptlrpc_request_set_replen(req);
1028 /* check if this is server or client */
1029 if (cld->cld_cfg.cfg_sb) {
1030 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
1031 if (lsi && IS_SERVER(lsi))
1034 /* Limit how long we will wait for the enqueue to complete */
1035 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
1036 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1037 NULL, 0, LVB_T_NONE, lockh, 0);
1038 /* A failed enqueue should still call the mgc_blocking_ast,
1039 where it will be requeued if needed ("grant failed"). */
1040 ptlrpc_req_finished(req);
1044 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
1045 __u32 mode, struct lustre_handle *lockh)
1047 ldlm_lock_decref(lockh, mode);
1052 static void mgc_notify_active(struct obd_device *unused)
1054 /* wakeup mgc_requeue_thread to requeue mgc lock */
1055 spin_lock(&config_list_lock);
1057 spin_unlock(&config_list_lock);
1060 /* TODO: Help the MGS rebuild nidtbl. -jay */
1063 /* Send target_reg message to MGS */
1064 static int mgc_target_register(struct obd_export *exp,
1065 struct mgs_target_info *mti)
1067 struct ptlrpc_request *req;
1068 struct mgs_target_info *req_mti, *rep_mti;
1071 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1072 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1077 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1079 ptlrpc_req_finished(req);
1083 memcpy(req_mti, mti, sizeof(*req_mti));
1084 ptlrpc_request_set_replen(req);
1085 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1086 /* Limit how long we will wait for the enqueue to complete */
1087 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1089 rc = ptlrpc_queue_wait(req);
1091 rep_mti = req_capsule_server_get(&req->rq_pill,
1092 &RMF_MGS_TARGET_INFO);
1093 memcpy(mti, rep_mti, sizeof(*rep_mti));
1094 CDEBUG(D_MGC, "register %s got index = %d\n",
1095 mti->mti_svname, mti->mti_stripe_index);
1097 ptlrpc_req_finished(req);
1102 int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1103 obd_count keylen, void *key, obd_count vallen,
1104 void *val, struct ptlrpc_request_set *set)
1108 /* Turn off initial_recov after we try all backup servers once */
1109 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1110 struct obd_import *imp = class_exp2cliimp(exp);
1112 if (vallen != sizeof(int))
1114 value = *(int *)val;
1115 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1116 imp->imp_obd->obd_name, value,
1117 imp->imp_deactive, imp->imp_invalid,
1118 imp->imp_replayable, imp->imp_obd->obd_replayable,
1119 ptlrpc_import_state_name(imp->imp_state));
1120 /* Resurrect if we previously died */
1121 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1122 imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1123 ptlrpc_reconnect_import(imp);
1126 /* FIXME move this to mgc_process_config */
1127 if (KEY_IS(KEY_REGISTER_TARGET)) {
1128 struct mgs_target_info *mti;
1129 if (vallen != sizeof(struct mgs_target_info))
1131 mti = (struct mgs_target_info *)val;
1132 CDEBUG(D_MGC, "register_target %s %#x\n",
1133 mti->mti_svname, mti->mti_flags);
1134 rc = mgc_target_register(exp, mti);
1137 if (KEY_IS(KEY_SET_FS)) {
1138 struct super_block *sb = (struct super_block *)val;
1140 if (vallen != sizeof(struct super_block))
1143 rc = mgc_fs_setup(exp->exp_obd, sb);
1145 CERROR("set_fs got %d\n", rc);
1149 if (KEY_IS(KEY_CLEAR_FS)) {
1152 rc = mgc_fs_cleanup(exp->exp_obd);
1154 CERROR("clear_fs got %d\n", rc);
1158 if (KEY_IS(KEY_SET_INFO)) {
1159 struct mgs_send_param *msp;
1161 msp = (struct mgs_send_param *)val;
1162 rc = mgc_set_mgs_param(exp, msp);
1165 if (KEY_IS(KEY_MGSSEC)) {
1166 struct client_obd *cli = &exp->exp_obd->u.cli;
1167 struct sptlrpc_flavor flvr;
1170 * empty string means using current flavor, if which haven't
1171 * been set yet, set it as null.
1173 * if flavor has been set previously, check the asking flavor
1174 * must match the existing one.
1177 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1183 rc = sptlrpc_parse_flavor(val, &flvr);
1185 CERROR("invalid sptlrpc flavor %s to MGS\n",
1191 * caller already hold a mutex
1193 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1194 cli->cl_flvr_mgc = flvr;
1195 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1196 sizeof(flvr)) != 0) {
1199 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1201 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1202 "currently %s is in use\n",
1212 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1213 __u32 keylen, void *key, __u32 *vallen, void *val,
1214 struct lov_stripe_md *unused)
1218 if (KEY_IS(KEY_CONN_DATA)) {
1219 struct obd_import *imp = class_exp2cliimp(exp);
1220 struct obd_connect_data *data = val;
1222 if (*vallen == sizeof(*data)) {
1223 *data = imp->imp_connect_data;
1231 static int mgc_import_event(struct obd_device *obd,
1232 struct obd_import *imp,
1233 enum obd_import_event event)
1237 LASSERT(imp->imp_obd == obd);
1238 CDEBUG(D_MGC, "import event %#x\n", event);
1241 case IMP_EVENT_DISCON:
1242 /* MGC imports should not wait for recovery */
1243 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1244 ptlrpc_pinger_ir_down();
1246 case IMP_EVENT_INACTIVE:
1248 case IMP_EVENT_INVALIDATE: {
1249 struct ldlm_namespace *ns = obd->obd_namespace;
1250 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1253 case IMP_EVENT_ACTIVE:
1254 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1255 /* Clearing obd_no_recov allows us to continue pinging */
1256 obd->obd_no_recov = 0;
1257 mgc_notify_active(obd);
1258 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1259 ptlrpc_pinger_ir_up();
1263 case IMP_EVENT_DEACTIVATE:
1264 case IMP_EVENT_ACTIVATE:
1267 CERROR("Unknown import event %#x\n", event);
1274 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_CACHE_SHIFT),
1275 CONFIG_READ_NRPAGES = 4
1278 static int mgc_apply_recover_logs(struct obd_device *mgc,
1279 struct config_llog_data *cld,
1281 void *data, int datalen, bool mne_swab)
1283 struct config_llog_instance *cfg = &cld->cld_cfg;
1284 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1285 struct mgs_nidtbl_entry *entry;
1286 struct lustre_cfg *lcfg;
1287 struct lustre_cfg_bufs bufs;
1288 u64 prev_version = 0;
1296 LASSERT(cfg->cfg_instance != NULL);
1297 LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1299 OBD_ALLOC(inst, PAGE_CACHE_SIZE);
1303 if (!IS_SERVER(lsi)) {
1304 pos = snprintf(inst, PAGE_CACHE_SIZE, "%p", cfg->cfg_instance);
1305 if (pos >= PAGE_CACHE_SIZE) {
1306 OBD_FREE(inst, PAGE_CACHE_SIZE);
1310 LASSERT(IS_MDT(lsi));
1311 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1314 OBD_FREE(inst, PAGE_CACHE_SIZE);
1322 bufsz = PAGE_CACHE_SIZE - pos;
1324 while (datalen > 0) {
1325 int entry_len = sizeof(*entry);
1327 struct obd_device *obd;
1334 if (datalen < sizeof(*entry))
1337 entry = (typeof(entry))(data + off);
1340 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1342 if (entry->mne_nid_count == 0) /* at least one nid entry */
1344 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1347 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1348 if (datalen < entry_len) /* must have entry_len at least */
1351 /* Keep this swab for normal mixed endian handling. LU-1644 */
1353 lustre_swab_mgs_nidtbl_entry(entry);
1354 if (entry->mne_length > PAGE_CACHE_SIZE) {
1355 CERROR("MNE too large (%u)\n", entry->mne_length);
1359 if (entry->mne_length < entry_len)
1362 off += entry->mne_length;
1363 datalen -= entry->mne_length;
1367 if (entry->mne_version > max_version) {
1368 CERROR("entry index(%lld) is over max_index(%lld)\n",
1369 entry->mne_version, max_version);
1373 if (prev_version >= entry->mne_version) {
1374 CERROR("index unsorted, prev %lld, now %lld\n",
1375 prev_version, entry->mne_version);
1378 prev_version = entry->mne_version;
1381 * Write a string with format "nid::instance" to
1382 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1385 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1386 memset(buf, 0, bufsz);
1390 /* lustre-OST0001-osc-<instance #> */
1391 strcpy(obdname, cld->cld_logname);
1392 cname = strrchr(obdname, '-');
1393 if (cname == NULL) {
1394 CERROR("mgc %s: invalid logname %s\n",
1395 mgc->obd_name, obdname);
1399 pos = cname - obdname;
1401 pos += sprintf(obdname + pos, "-%s%04x",
1402 is_ost ? "OST" : "MDT", entry->mne_index);
1404 cname = is_ost ? "osc" : "mdc",
1405 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1406 lustre_cfg_bufs_reset(&bufs, obdname);
1408 /* find the obd by obdname */
1409 obd = class_name2obd(obdname);
1411 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1412 mgc->obd_name, obdname);
1414 /* this is a safe race, when the ost is starting up...*/
1418 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1421 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1424 down_read(&obd->u.cli.cl_sem);
1425 if (obd->u.cli.cl_import == NULL) {
1426 /* client does not connect to the OST yet */
1427 up_read(&obd->u.cli.cl_sem);
1432 /* TODO: iterate all nids to find one */
1433 /* find uuid by nid */
1434 rc = client_import_find_conn(obd->u.cli.cl_import,
1436 (struct obd_uuid *)uuid);
1437 up_read(&obd->u.cli.cl_sem);
1439 CERROR("mgc: cannot find uuid by nid %s\n",
1440 libcfs_nid2str(entry->u.nids[0]));
1444 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1445 uuid, libcfs_nid2str(entry->u.nids[0]));
1447 pos += strlen(uuid);
1448 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1449 LASSERT(pos < bufsz);
1451 lustre_cfg_bufs_set_string(&bufs, 1, params);
1454 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1456 CERROR("mgc: cannot allocate memory\n");
1460 CDEBUG(D_INFO, "ir apply logs %lld/%lld for %s -> %s\n",
1461 prev_version, max_version, obdname, params);
1463 rc = class_process_config(lcfg);
1464 lustre_cfg_free(lcfg);
1466 CDEBUG(D_INFO, "process config for %s error %d\n",
1469 /* continue, even one with error */
1472 OBD_FREE(inst, PAGE_CACHE_SIZE);
1477 * This function is called if this client was notified for target restarting
1478 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery logs.
1480 static int mgc_process_recover_log(struct obd_device *obd,
1481 struct config_llog_data *cld)
1483 struct ptlrpc_request *req = NULL;
1484 struct config_llog_instance *cfg = &cld->cld_cfg;
1485 struct mgs_config_body *body;
1486 struct mgs_config_res *res;
1487 struct ptlrpc_bulk_desc *desc;
1488 struct page **pages;
1491 bool mne_swab = false;
1496 /* allocate buffer for bulk transfer.
1497 * if this is the first time for this mgs to read logs,
1498 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1499 * once; otherwise, it only reads increment of logs, this should be
1500 * small and CONFIG_READ_NRPAGES will be used.
1502 nrpages = CONFIG_READ_NRPAGES;
1503 if (cfg->cfg_last_idx == 0) /* the first time */
1504 nrpages = CONFIG_READ_NRPAGES_INIT;
1506 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1508 GOTO(out, rc = -ENOMEM);
1510 for (i = 0; i < nrpages; i++) {
1511 pages[i] = alloc_page(GFP_IOFS);
1512 if (pages[i] == NULL)
1513 GOTO(out, rc = -ENOMEM);
1517 LASSERT(cld_is_recover(cld));
1518 LASSERT(mutex_is_locked(&cld->cld_lock));
1519 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1520 &RQF_MGS_CONFIG_READ);
1522 GOTO(out, rc = -ENOMEM);
1524 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1529 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1530 LASSERT(body != NULL);
1531 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1532 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1533 >= sizeof(body->mcb_name))
1534 GOTO(out, rc = -E2BIG);
1535 body->mcb_offset = cfg->cfg_last_idx + 1;
1536 body->mcb_type = cld->cld_type;
1537 body->mcb_bits = PAGE_CACHE_SHIFT;
1538 body->mcb_units = nrpages;
1540 /* allocate bulk transfer descriptor */
1541 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1, BULK_PUT_SINK,
1544 GOTO(out, rc = -ENOMEM);
1546 for (i = 0; i < nrpages; i++)
1547 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1549 ptlrpc_request_set_replen(req);
1550 rc = ptlrpc_queue_wait(req);
1554 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1555 if (res->mcr_size < res->mcr_offset)
1556 GOTO(out, rc = -EINVAL);
1558 /* always update the index even though it might have errors with
1559 * handling the recover logs */
1560 cfg->cfg_last_idx = res->mcr_offset;
1561 eof = res->mcr_offset == res->mcr_size;
1563 CDEBUG(D_INFO, "Latest version %lld, more %d.\n",
1564 res->mcr_offset, eof == false);
1566 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1568 GOTO(out, rc = ealen);
1570 if (ealen > nrpages << PAGE_CACHE_SHIFT)
1571 GOTO(out, rc = -EINVAL);
1573 if (ealen == 0) { /* no logs transferred */
1579 mne_swab = !!ptlrpc_rep_need_swab(req);
1580 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 50, 0)
1581 /* This import flag means the server did an extra swab of IR MNE
1582 * records (fixed in LU-1252), reverse it here if needed. LU-1644 */
1583 if (unlikely(req->rq_import->imp_need_mne_swab))
1584 mne_swab = !mne_swab;
1586 #warning "LU-1644: Remove old OBD_CONNECT_MNE_SWAB fixup and imp_need_mne_swab"
1589 for (i = 0; i < nrpages && ealen > 0; i++) {
1593 ptr = kmap(pages[i]);
1594 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1595 min_t(int, ealen, PAGE_CACHE_SIZE),
1599 CWARN("Process recover log %s error %d\n",
1600 cld->cld_logname, rc2);
1604 ealen -= PAGE_CACHE_SIZE;
1609 ptlrpc_req_finished(req);
1611 if (rc == 0 && !eof)
1615 for (i = 0; i < nrpages; i++) {
1616 if (pages[i] == NULL)
1618 __free_page(pages[i]);
1620 OBD_FREE(pages, sizeof(*pages) * nrpages);
1625 static int mgc_llog_local_copy(const struct lu_env *env,
1626 struct obd_device *obd,
1627 struct llog_ctxt *rctxt,
1628 struct llog_ctxt *lctxt, char *logname)
1636 * - copy it to backup using llog_backup()
1637 * - copy remote llog to logname using llog_backup()
1638 * - if failed then move backup to logname again
1641 OBD_ALLOC(temp_log, strlen(logname) + 1);
1644 sprintf(temp_log, "%sT", logname);
1646 /* make a copy of local llog at first */
1647 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1648 if (rc < 0 && rc != -ENOENT)
1650 /* copy remote llog to the local copy */
1651 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1652 if (rc == -ENOENT) {
1653 /* no remote llog, delete local one too */
1654 llog_erase(env, lctxt, NULL, logname);
1655 } else if (rc < 0) {
1656 /* error during backup, get local one back from the copy */
1657 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1659 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1660 obd->obd_name, logname, rc);
1662 llog_erase(env, lctxt, NULL, temp_log);
1663 OBD_FREE(temp_log, strlen(logname) + 1);
1667 /* local_only means it cannot get remote llogs */
1668 static int mgc_process_cfg_log(struct obd_device *mgc,
1669 struct config_llog_data *cld, int local_only)
1671 struct llog_ctxt *ctxt, *lctxt = NULL;
1672 struct dt_object *cl_mgc_dir = mgc->u.cli.cl_mgc_configs_dir;
1673 struct lustre_sb_info *lsi = NULL;
1675 bool sptlrpc_started = false;
1679 LASSERT(mutex_is_locked(&cld->cld_lock));
1682 * local copy of sptlrpc log is controlled elsewhere, don't try to
1685 if (cld_is_sptlrpc(cld) && local_only)
1688 if (cld->cld_cfg.cfg_sb)
1689 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1695 rc = lu_env_init(env, LCT_MG_THREAD);
1699 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1702 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1704 /* Copy the setup log locally if we can. Don't mess around if we're
1705 * running an MGS though (logs are already local). */
1706 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1707 cl_mgc_dir != NULL &&
1708 lu2dt_dev(cl_mgc_dir->do_lu.lo_dev) == lsi->lsi_dt_dev) {
1710 /* Only try to copy log if we have the lock. */
1711 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1713 if (local_only || rc) {
1714 if (llog_is_empty(env, lctxt, cld->cld_logname)) {
1715 LCONSOLE_ERROR_MSG(0x13a,
1716 "Failed to get MGS log %s and no local copy.\n",
1718 GOTO(out_pop, rc = -ENOENT);
1721 "Failed to get MGS log %s, using local copy for now, will try to update later.\n",
1724 /* Now, whether we copied or not, start using the local llog.
1725 * If we failed to copy, we'll start using whatever the old
1727 llog_ctxt_put(ctxt);
1731 if (local_only) /* no local log at client side */
1732 GOTO(out_pop, rc = -EIO);
1735 if (cld_is_sptlrpc(cld)) {
1736 sptlrpc_conf_log_update_begin(cld->cld_logname);
1737 sptlrpc_started = true;
1740 /* logname and instance info should be the same, so use our
1741 * copy of the instance for the update. The cfg_last_idx will
1742 * be updated here. */
1743 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1747 __llog_ctxt_put(env, ctxt);
1749 __llog_ctxt_put(env, lctxt);
1752 * update settings on existing OBDs. doing it inside
1753 * of llog_process_lock so no device is attaching/detaching
1755 * the logname must be <fsname>-sptlrpc
1757 if (sptlrpc_started) {
1758 LASSERT(cld_is_sptlrpc(cld));
1759 sptlrpc_conf_log_update_end(cld->cld_logname);
1760 class_notify_sptlrpc_conf(cld->cld_logname,
1761 strlen(cld->cld_logname) -
1762 strlen("-sptlrpc"));
1771 /** Get a config log from the MGS and process it.
1772 * This func is called for both clients and servers.
1773 * Copy the log locally before parsing it if appropriate (non-MGS server)
1775 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1777 struct lustre_handle lockh = { 0 };
1778 __u64 flags = LDLM_FL_NO_LRU;
1783 /* I don't want multiple processes running process_log at once --
1784 sounds like badness. It actually might be fine, as long as
1785 we're not trying to update from the same log
1786 simultaneously (in which case we should use a per-log sem.) */
1787 mutex_lock(&cld->cld_lock);
1788 if (cld->cld_stopping) {
1789 mutex_unlock(&cld->cld_lock);
1793 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1795 CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
1796 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1798 /* Get the cfg lock on the llog */
1799 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1800 LCK_CR, &flags, NULL, NULL, NULL,
1801 cld, 0, NULL, &lockh);
1803 /* Get the cld, it will be released in mgc_blocking_ast. */
1804 config_log_get(cld);
1805 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1808 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1810 /* mark cld_lostlock so that it will requeue
1811 * after MGC becomes available. */
1812 cld->cld_lostlock = 1;
1813 /* Get extra reference, it will be put in requeue thread */
1814 config_log_get(cld);
1818 if (cld_is_recover(cld)) {
1819 rc = 0; /* this is not a fatal error for recover log */
1821 rc = mgc_process_recover_log(mgc, cld);
1823 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1826 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1827 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1829 mutex_unlock(&cld->cld_lock);
1831 /* Now drop the lock so MGS can revoke it */
1833 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1836 CERROR("Can't drop cfg lock: %d\n", rcl);
1843 /** Called from lustre_process_log.
1844 * LCFG_LOG_START gets the config log from the MGS, processes it to start
1845 * any services, and adds it to the list logs to watch (follow).
1847 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1849 struct lustre_cfg *lcfg = buf;
1850 struct config_llog_instance *cfg = NULL;
1854 switch (lcfg->lcfg_command) {
1855 case LCFG_LOV_ADD_OBD: {
1856 /* Overloading this cfg command: register a new target */
1857 struct mgs_target_info *mti;
1859 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1860 sizeof(struct mgs_target_info))
1861 GOTO(out, rc = -EINVAL);
1863 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1864 CDEBUG(D_MGC, "add_target %s %#x\n",
1865 mti->mti_svname, mti->mti_flags);
1866 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1869 case LCFG_LOV_DEL_OBD:
1870 /* Unregister has no meaning at the moment. */
1871 CERROR("lov_del_obd unimplemented\n");
1874 case LCFG_SPTLRPC_CONF: {
1875 rc = sptlrpc_process_config(lcfg);
1878 case LCFG_LOG_START: {
1879 struct config_llog_data *cld;
1880 struct super_block *sb;
1882 logname = lustre_cfg_string(lcfg, 1);
1883 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1884 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1886 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1889 /* We're only called through here on the initial mount */
1890 rc = config_log_add(obd, logname, cfg, sb);
1893 cld = config_log_find(logname, cfg);
1900 /* FIXME only set this for old logs! Right now this forces
1901 us to always skip the "inside markers" check */
1902 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1904 rc = mgc_process_log(obd, cld);
1905 if (rc == 0 && cld->cld_recover != NULL) {
1906 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
1907 imp_connect_data, IMP_RECOV)) {
1908 rc = mgc_process_log(obd, cld->cld_recover);
1910 struct config_llog_data *cir = cld->cld_recover;
1911 cld->cld_recover = NULL;
1912 config_log_put(cir);
1915 CERROR("Cannot process recover llog %d\n", rc);
1918 if (rc == 0 && cld->cld_params != NULL) {
1919 rc = mgc_process_log(obd, cld->cld_params);
1920 if (rc == -ENOENT) {
1922 "There is no params config file yet\n");
1925 /* params log is optional */
1928 "%s: can't process params llog: rc = %d\n",
1931 config_log_put(cld);
1935 case LCFG_LOG_END: {
1936 logname = lustre_cfg_string(lcfg, 1);
1938 if (lcfg->lcfg_bufcount >= 2)
1939 cfg = (struct config_llog_instance *)lustre_cfg_buf(
1941 rc = config_log_end(logname, cfg);
1945 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1946 GOTO(out, rc = -EINVAL);
1954 struct obd_ops mgc_obd_ops = {
1955 .o_owner = THIS_MODULE,
1956 .o_setup = mgc_setup,
1957 .o_precleanup = mgc_precleanup,
1958 .o_cleanup = mgc_cleanup,
1959 .o_add_conn = client_import_add_conn,
1960 .o_del_conn = client_import_del_conn,
1961 .o_connect = client_connect_import,
1962 .o_disconnect = client_disconnect_export,
1963 /* .o_enqueue = mgc_enqueue, */
1964 .o_cancel = mgc_cancel,
1965 /* .o_iocontrol = mgc_iocontrol, */
1966 .o_set_info_async = mgc_set_info_async,
1967 .o_get_info = mgc_get_info,
1968 .o_import_event = mgc_import_event,
1969 .o_process_config = mgc_process_config,
1972 int __init mgc_init(void)
1974 return class_register_type(&mgc_obd_ops, NULL, NULL,
1975 LUSTRE_MGC_NAME, NULL);
1978 static void /*__exit*/ mgc_exit(void)
1980 class_unregister_type(LUSTRE_MGC_NAME);
1983 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1984 MODULE_DESCRIPTION("Lustre Management Client");
1985 MODULE_LICENSE("GPL");
1987 module_init(mgc_init);
1988 module_exit(mgc_exit);