4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mgc/mgc_request.c
38 * Author: Nathan Rutman <nathan@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_MGC
42 #define D_MGC D_CONFIG /*|D_WARNING*/
44 #include <linux/module.h>
45 #include <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 static int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id,
105 /* logname consists of "fsname-nodetype".
106 * e.g. "lustre-MDT0001", "SUN-000-client"
107 * there is an exception: llog "params" */
108 name_end = strrchr(logname, '-');
110 len = strlen(logname);
112 len = name_end - logname;
113 return mgc_name2resid(logname, len, res_id, type);
116 /********************** config llog list **********************/
117 static struct list_head config_llog_list = LIST_HEAD_INIT(config_llog_list);
118 static DEFINE_SPINLOCK(config_list_lock);
120 /* Take a reference to a config log */
121 static int config_log_get(struct config_llog_data *cld)
124 atomic_inc(&cld->cld_refcount);
125 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
126 atomic_read(&cld->cld_refcount));
130 /* Drop a reference to a config log. When no longer referenced,
131 we can free the config log data */
132 static void config_log_put(struct config_llog_data *cld)
136 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
137 atomic_read(&cld->cld_refcount));
138 LASSERT(atomic_read(&cld->cld_refcount) > 0);
140 /* spinlock to make sure no item with 0 refcount in the list */
141 if (atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
142 list_del(&cld->cld_list_chain);
143 spin_unlock(&config_list_lock);
145 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
147 if (cld->cld_recover)
148 config_log_put(cld->cld_recover);
149 if (cld->cld_sptlrpc)
150 config_log_put(cld->cld_sptlrpc);
152 config_log_put(cld->cld_params);
153 if (cld_is_sptlrpc(cld))
154 sptlrpc_conf_log_stop(cld->cld_logname);
156 class_export_put(cld->cld_mgcexp);
157 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
163 /* Find a config log by name */
165 struct config_llog_data *config_log_find(char *logname,
166 struct config_llog_instance *cfg)
168 struct config_llog_data *cld;
169 struct config_llog_data *found = NULL;
173 LASSERT(logname != NULL);
175 instance = cfg ? cfg->cfg_instance : NULL;
176 spin_lock(&config_list_lock);
177 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
178 /* check if instance equals */
179 if (instance != cld->cld_cfg.cfg_instance)
182 /* instance may be NULL, should check name */
183 if (strcmp(logname, cld->cld_logname) == 0) {
189 atomic_inc(&found->cld_refcount);
190 LASSERT(found->cld_stopping == 0 || cld_is_sptlrpc(found) == 0);
192 spin_unlock(&config_list_lock);
197 struct config_llog_data *do_config_log_add(struct obd_device *obd,
200 struct config_llog_instance *cfg,
201 struct super_block *sb)
203 struct config_llog_data *cld;
207 CDEBUG(D_MGC, "do adding config log %s:%p\n", logname,
208 cfg ? cfg->cfg_instance : NULL);
210 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
212 RETURN(ERR_PTR(-ENOMEM));
214 strcpy(cld->cld_logname, logname);
218 cld->cld_cfg.cfg_callback = class_config_llog_handler;
219 mutex_init(&cld->cld_lock);
220 cld->cld_cfg.cfg_last_idx = 0;
221 cld->cld_cfg.cfg_flags = 0;
222 cld->cld_cfg.cfg_sb = sb;
223 cld->cld_type = type;
224 atomic_set(&cld->cld_refcount, 1);
226 /* Keep the mgc around until we are done */
227 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
229 if (cld_is_sptlrpc(cld)) {
230 sptlrpc_conf_log_start(logname);
231 cld->cld_cfg.cfg_obdname = obd->obd_name;
234 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
236 spin_lock(&config_list_lock);
237 list_add(&cld->cld_list_chain, &config_llog_list);
238 spin_unlock(&config_list_lock);
245 if (cld_is_sptlrpc(cld)) {
246 rc = mgc_process_log(obd, cld);
247 if (rc && rc != -ENOENT)
248 CERROR("failed processing sptlrpc log: %d\n", rc);
254 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
256 struct config_llog_instance *cfg,
257 struct super_block *sb)
259 struct config_llog_instance lcfg = *cfg;
260 struct lustre_sb_info *lsi = s2lsi(sb);
261 struct config_llog_data *cld;
267 /* for osp-on-ost, see lustre_start_osp() */
268 if (IS_MDT(lsi) && lcfg.cfg_instance)
271 /* we have to use different llog for clients and mdts for cmd
272 * where only clients are notified if one of cmd server restarts */
273 LASSERT(strlen(fsname) < sizeof(logname) / 2);
274 strcpy(logname, fsname);
275 if (IS_SERVER(lsi)) { /* mdt */
276 LASSERT(lcfg.cfg_instance == NULL);
277 lcfg.cfg_instance = sb;
278 strcat(logname, "-mdtir");
280 LASSERT(lcfg.cfg_instance != NULL);
281 strcat(logname, "-cliir");
284 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
288 static struct config_llog_data *config_params_log_add(struct obd_device *obd,
289 struct config_llog_instance *cfg, struct super_block *sb)
291 struct config_llog_instance lcfg = *cfg;
292 struct config_llog_data *cld;
294 lcfg.cfg_instance = sb;
296 cld = do_config_log_add(obd, PARAMS_FILENAME, CONFIG_T_PARAMS,
302 /** Add this log to the list of active logs watched by an MGC.
303 * Active means we're watching for updates.
304 * We have one active log per "mount" - client instance or servername.
305 * Each instance may be at a different point in the log.
307 static int config_log_add(struct obd_device *obd, char *logname,
308 struct config_llog_instance *cfg,
309 struct super_block *sb)
311 struct lustre_sb_info *lsi = s2lsi(sb);
312 struct config_llog_data *cld;
313 struct config_llog_data *sptlrpc_cld;
314 struct config_llog_data *params_cld;
320 CDEBUG(D_MGC, "adding config log %s:%p\n", logname, cfg->cfg_instance);
323 * for each regular log, the depended sptlrpc log name is
324 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
326 ptr = strrchr(logname, '-');
327 if (ptr == NULL || ptr - logname > 8) {
328 CERROR("logname %s is too long\n", logname);
332 memcpy(seclogname, logname, ptr - logname);
333 strcpy(seclogname + (ptr - logname), "-sptlrpc");
335 sptlrpc_cld = config_log_find(seclogname, NULL);
336 if (sptlrpc_cld == NULL) {
337 sptlrpc_cld = do_config_log_add(obd, seclogname,
338 CONFIG_T_SPTLRPC, NULL, NULL);
339 if (IS_ERR(sptlrpc_cld)) {
340 CERROR("can't create sptlrpc log: %s\n", seclogname);
341 GOTO(out_err, rc = PTR_ERR(sptlrpc_cld));
344 params_cld = config_params_log_add(obd, cfg, sb);
345 if (IS_ERR(params_cld)) {
346 rc = PTR_ERR(params_cld);
347 CERROR("%s: can't create params log: rc = %d\n",
352 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
354 CERROR("can't create log: %s\n", logname);
355 GOTO(out_err2, rc = PTR_ERR(cld));
358 cld->cld_sptlrpc = sptlrpc_cld;
359 cld->cld_params = params_cld;
361 LASSERT(lsi->lsi_lmd);
362 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)) {
363 struct config_llog_data *recover_cld;
364 ptr = strrchr(seclogname, '-');
369 CERROR("%s: sptlrpc log name not correct, %s: "
370 "rc = %d\n", obd->obd_name, seclogname, -EINVAL);
374 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
375 if (IS_ERR(recover_cld))
376 GOTO(out_err3, rc = PTR_ERR(recover_cld));
377 cld->cld_recover = recover_cld;
386 config_log_put(params_cld);
389 config_log_put(sptlrpc_cld);
395 DEFINE_MUTEX(llog_process_lock);
397 /** Stop watching for updates on this log.
399 static int config_log_end(char *logname, struct config_llog_instance *cfg)
401 struct config_llog_data *cld;
402 struct config_llog_data *cld_sptlrpc = NULL;
403 struct config_llog_data *cld_params = NULL;
404 struct config_llog_data *cld_recover = NULL;
408 cld = config_log_find(logname, cfg);
412 mutex_lock(&cld->cld_lock);
414 * if cld_stopping is set, it means we didn't start the log thus
415 * not owning the start ref. this can happen after previous umount:
416 * the cld still hanging there waiting for lock cancel, and we
417 * remount again but failed in the middle and call log_end without
420 if (unlikely(cld->cld_stopping)) {
421 mutex_unlock(&cld->cld_lock);
422 /* drop the ref from the find */
427 cld->cld_stopping = 1;
429 cld_recover = cld->cld_recover;
430 cld->cld_recover = NULL;
431 mutex_unlock(&cld->cld_lock);
434 mutex_lock(&cld_recover->cld_lock);
435 cld_recover->cld_stopping = 1;
436 mutex_unlock(&cld_recover->cld_lock);
437 config_log_put(cld_recover);
440 spin_lock(&config_list_lock);
441 cld_sptlrpc = cld->cld_sptlrpc;
442 cld->cld_sptlrpc = NULL;
443 cld_params = cld->cld_params;
444 cld->cld_params = NULL;
445 spin_unlock(&config_list_lock);
448 config_log_put(cld_sptlrpc);
451 mutex_lock(&cld_params->cld_lock);
452 cld_params->cld_stopping = 1;
453 mutex_unlock(&cld_params->cld_lock);
454 config_log_put(cld_params);
457 /* drop the ref from the find */
459 /* drop the start ref */
462 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
467 #ifdef CONFIG_PROC_FS
468 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
470 struct obd_device *obd = data;
471 struct obd_import *imp;
472 struct obd_connect_data *ocd;
473 struct config_llog_data *cld;
476 LASSERT(obd != NULL);
477 LPROCFS_CLIMP_CHECK(obd);
478 imp = obd->u.cli.cl_import;
479 ocd = &imp->imp_connect_data;
481 seq_printf(m, "imperative_recovery: %s\n",
482 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
483 seq_printf(m, "client_state:\n");
485 spin_lock(&config_list_lock);
486 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
487 if (cld->cld_recover == NULL)
489 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
491 cld->cld_recover->cld_cfg.cfg_last_idx);
493 spin_unlock(&config_list_lock);
495 LPROCFS_CLIMP_EXIT(obd);
500 /* reenqueue any lost locks */
501 #define RQ_RUNNING 0x1
505 #define RQ_PRECLEANUP 0x10
506 static int rq_state = 0;
507 static wait_queue_head_t rq_waitq;
508 static DECLARE_COMPLETION(rq_exit);
509 static DECLARE_COMPLETION(rq_start);
511 static void do_requeue(struct config_llog_data *cld)
516 LASSERT(atomic_read(&cld->cld_refcount) > 0);
518 /* Do not run mgc_process_log on a disconnected export or an
519 * export which is being disconnected. Take the client
520 * semaphore to make the check non-racy. */
521 down_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
522 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
523 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
524 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
525 if (rc && rc != -ENOENT)
526 CERROR("failed processing log: %d\n", rc);
528 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
531 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
536 /* this timeout represents how many seconds MGC should wait before
537 * requeue config and recover lock to the MGS. We need to randomize this
538 * in order to not flood the MGS.
540 #define MGC_TIMEOUT_MIN_SECONDS 5
541 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
543 static int mgc_requeue_thread(void *data)
549 CDEBUG(D_MGC, "Starting requeue thread\n");
551 /* Keep trying failed locks periodically */
552 spin_lock(&config_list_lock);
553 rq_state |= RQ_RUNNING;
555 struct l_wait_info lwi;
556 struct config_llog_data *cld, *cld_prev;
557 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
558 int stopped = !!(rq_state & RQ_STOP);
561 /* Any new or requeued lostlocks will change the state */
562 rq_state &= ~(RQ_NOW | RQ_LATER);
563 spin_unlock(&config_list_lock);
570 /* Always wait a few seconds to allow the server who
571 caused the lock revocation to finish its setup, plus some
572 random so everyone doesn't try to reconnect at once. */
573 to = msecs_to_jiffies(MGC_TIMEOUT_MIN_SECONDS * MSEC_PER_SEC);
574 /* rand is centi-seconds */
575 to += msecs_to_jiffies(rand * MSEC_PER_SEC / 100);
576 lwi = LWI_TIMEOUT(to, NULL, NULL);
577 l_wait_event(rq_waitq, rq_state & (RQ_STOP | RQ_PRECLEANUP),
581 * iterate & processing through the list. for each cld, process
582 * its depending sptlrpc cld firstly (if any) and then itself.
584 * it's guaranteed any item in the list must have
585 * reference > 0; and if cld_lostlock is set, at
586 * least one reference is taken by the previous enqueue.
590 spin_lock(&config_list_lock);
591 rq_state &= ~RQ_PRECLEANUP;
592 list_for_each_entry(cld, &config_llog_list,
594 if (!cld->cld_lostlock)
597 spin_unlock(&config_list_lock);
599 LASSERT(atomic_read(&cld->cld_refcount) > 0);
601 /* Whether we enqueued again or not in mgc_process_log,
602 * we're done with the ref from the old enqueue */
604 config_log_put(cld_prev);
607 cld->cld_lostlock = 0;
608 if (likely(!stopped))
611 spin_lock(&config_list_lock);
613 spin_unlock(&config_list_lock);
615 config_log_put(cld_prev);
617 /* break after scanning the list so that we can drop
618 * refcount to losing lock clds */
619 if (unlikely(stopped)) {
620 spin_lock(&config_list_lock);
624 /* Wait a bit to see if anyone else needs a requeue */
625 lwi = (struct l_wait_info) { 0 };
626 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
628 spin_lock(&config_list_lock);
630 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
631 rq_state &= ~RQ_RUNNING;
632 spin_unlock(&config_list_lock);
636 CDEBUG(D_MGC, "Ending requeue thread\n");
640 /* Add a cld to the list to requeue. Start the requeue thread if needed.
641 We are responsible for dropping the config log reference from here on out. */
642 static void mgc_requeue_add(struct config_llog_data *cld)
646 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
647 cld->cld_logname, atomic_read(&cld->cld_refcount),
648 cld->cld_stopping, rq_state);
649 LASSERT(atomic_read(&cld->cld_refcount) > 0);
651 mutex_lock(&cld->cld_lock);
652 if (cld->cld_stopping || cld->cld_lostlock) {
653 mutex_unlock(&cld->cld_lock);
656 /* this refcount will be released in mgc_requeue_thread. */
658 cld->cld_lostlock = 1;
659 mutex_unlock(&cld->cld_lock);
661 /* Hold lock for rq_state */
662 spin_lock(&config_list_lock);
663 if (rq_state & RQ_STOP) {
664 spin_unlock(&config_list_lock);
665 cld->cld_lostlock = 0;
669 spin_unlock(&config_list_lock);
675 /********************** class fns **********************/
676 static int mgc_local_llog_init(const struct lu_env *env,
677 struct obd_device *obd,
678 struct obd_device *disk)
680 struct llog_ctxt *ctxt;
685 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
690 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
692 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
698 static int mgc_local_llog_fini(const struct lu_env *env,
699 struct obd_device *obd)
701 struct llog_ctxt *ctxt;
705 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
706 llog_cleanup(env, ctxt);
711 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb)
713 struct lustre_sb_info *lsi = s2lsi(sb);
714 struct client_obd *cli = &obd->u.cli;
715 struct lu_fid rfid, fid;
716 struct dt_object *root, *dto;
723 LASSERT(lsi->lsi_dt_dev);
729 /* The mgc fs exclusion mutex. Only one fs can be setup at a time. */
730 mutex_lock(&cli->cl_mgc_mutex);
732 /* Setup the configs dir */
733 rc = lu_env_init(env, LCT_MG_THREAD);
737 fid.f_seq = FID_SEQ_LOCAL_NAME;
740 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
745 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
749 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
750 &cli->cl_mgc_los->los_dev->dd_lu_dev, NULL);
751 if (unlikely(IS_ERR(root)))
752 GOTO(out_los, rc = PTR_ERR(root));
754 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
756 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
757 lu_object_put_nocache(env, &root->do_lu);
759 GOTO(out_los, rc = PTR_ERR(dto));
761 cli->cl_mgc_configs_dir = dto;
763 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
764 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
768 /* We take an obd ref to insure that we can't get to mgc_cleanup
769 * without calling mgc_fs_cleanup first. */
770 class_incref(obd, "mgc_fs", obd);
772 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
776 lu_object_put(env, &cli->cl_mgc_configs_dir->do_lu);
777 cli->cl_mgc_configs_dir = NULL;
781 local_oid_storage_fini(env, cli->cl_mgc_los);
782 cli->cl_mgc_los = NULL;
783 mutex_unlock(&cli->cl_mgc_mutex);
792 static int mgc_fs_cleanup(struct obd_device *obd)
795 struct client_obd *cli = &obd->u.cli;
800 LASSERT(cli->cl_mgc_los != NULL);
802 rc = lu_env_init(&env, LCT_MG_THREAD);
806 mgc_local_llog_fini(&env, obd);
808 lu_object_put_nocache(&env, &cli->cl_mgc_configs_dir->do_lu);
809 cli->cl_mgc_configs_dir = NULL;
811 local_oid_storage_fini(&env, cli->cl_mgc_los);
812 cli->cl_mgc_los = NULL;
816 class_decref(obd, "mgc_fs", obd);
817 mutex_unlock(&cli->cl_mgc_mutex);
822 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
824 struct llog_ctxt *ctxt;
829 /* setup only remote ctxt, the local disk context is switched per each
830 * filesystem during mgc_fs_setup() */
831 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
836 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
839 llog_initiator_connect(ctxt);
845 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
847 struct llog_ctxt *ctxt;
851 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
853 llog_cleanup(env, ctxt);
859 static atomic_t mgc_count = ATOMIC_INIT(0);
860 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
867 case OBD_CLEANUP_EARLY:
869 case OBD_CLEANUP_EXPORTS:
870 if (atomic_dec_and_test(&mgc_count)) {
871 LASSERT(rq_state & RQ_RUNNING);
872 /* stop requeue thread */
875 /* wakeup requeue thread to clean our cld */
876 temp = RQ_NOW | RQ_PRECLEANUP;
878 spin_lock(&config_list_lock);
880 spin_unlock(&config_list_lock);
883 wait_for_completion(&rq_exit);
884 obd_cleanup_client_import(obd);
885 rc = mgc_llog_fini(NULL, obd);
887 CERROR("failed to cleanup llogging subsystems\n");
893 static int mgc_cleanup(struct obd_device *obd)
898 /* COMPAT_146 - old config logs may have added profiles we don't
900 if (obd->obd_type->typ_refcnt <= 1)
901 /* Only for the last mgc */
902 class_del_profiles();
904 lprocfs_obd_cleanup(obd);
907 rc = client_obd_cleanup(obd);
911 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
913 struct task_struct *task;
917 rc = ptlrpcd_addref();
921 rc = client_obd_setup(obd, lcfg);
923 GOTO(err_decref, rc);
925 rc = mgc_llog_init(NULL, obd);
927 CERROR("failed to setup llogging subsystems\n");
928 GOTO(err_cleanup, rc);
931 #ifdef CONFIG_PROC_FS
932 obd->obd_vars = lprocfs_mgc_obd_vars;
933 lprocfs_obd_setup(obd);
935 sptlrpc_lprocfs_cliobd_attach(obd);
937 if (atomic_inc_return(&mgc_count) == 1) {
939 init_waitqueue_head(&rq_waitq);
941 /* start requeue thread */
942 task = kthread_run(mgc_requeue_thread, NULL, "ll_cfg_requeue");
945 CERROR("%s: cannot start requeue thread: rc = %d; "
946 "no more log updates\n",
948 GOTO(err_cleanup, rc);
950 /* rc is the task_struct pointer of mgc_requeue_thread. */
952 wait_for_completion(&rq_start);
958 client_obd_cleanup(obd);
964 /* based on ll_mdc_blocking_ast */
965 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
966 void *data, int flag)
968 struct lustre_handle lockh;
969 struct config_llog_data *cld = (struct config_llog_data *)data;
974 case LDLM_CB_BLOCKING:
975 /* mgs wants the lock, give it up... */
976 LDLM_DEBUG(lock, "MGC blocking CB");
977 ldlm_lock2handle(lock, &lockh);
978 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
980 case LDLM_CB_CANCELING:
981 /* We've given up the lock, prepare ourselves to update. */
982 LDLM_DEBUG(lock, "MGC cancel CB");
984 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
985 PLDLMRES(lock->l_resource),
986 (char *)&lock->l_resource->lr_name.name[0]);
989 CDEBUG(D_INFO, "missing data, won't requeue\n");
993 /* held at mgc_process_log(). */
994 LASSERT(atomic_read(&cld->cld_refcount) > 0);
995 /* Are we done with this log? */
996 if (cld->cld_stopping) {
997 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
1002 /* Make sure not to re-enqueue when the mgc is stopping
1003 (we get called from client_disconnect_export) */
1004 if (lock->l_conn_export == NULL ||
1005 lock->l_conn_export->exp_obd->u.cli.cl_conn_count == 0) {
1006 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
1008 config_log_put(cld);
1012 /* Re-enqueue now */
1013 mgc_requeue_add(cld);
1014 config_log_put(cld);
1023 /* Not sure where this should go... */
1024 /* This is the timeout value for MGS_CONNECT request plus a ping interval, such
1025 * that we can have a chance to try the secondary MGS if any. */
1026 #define MGC_ENQUEUE_LIMIT (INITIAL_CONNECT_TIMEOUT + (AT_OFF ? 0 : at_min) \
1028 #define MGC_TARGET_REG_LIMIT 10
1029 #define MGC_SEND_PARAM_LIMIT 10
1031 /* Send parameter to MGS*/
1032 static int mgc_set_mgs_param(struct obd_export *exp,
1033 struct mgs_send_param *msp)
1035 struct ptlrpc_request *req;
1036 struct mgs_send_param *req_msp, *rep_msp;
1040 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1041 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
1046 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1048 ptlrpc_req_finished(req);
1052 memcpy(req_msp, msp, sizeof(*req_msp));
1053 ptlrpc_request_set_replen(req);
1055 /* Limit how long we will wait for the enqueue to complete */
1056 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
1057 rc = ptlrpc_queue_wait(req);
1059 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
1060 memcpy(msp, rep_msp, sizeof(*rep_msp));
1063 ptlrpc_req_finished(req);
1068 /* Take a config lock so we can get cancel notifications */
1069 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
1070 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
1071 __u64 *flags, void *bl_cb, void *cp_cb, void *gl_cb,
1072 void *data, __u32 lvb_len, void *lvb_swabber,
1073 struct lustre_handle *lockh)
1075 struct config_llog_data *cld = (struct config_llog_data *)data;
1076 struct ldlm_enqueue_info einfo = {
1079 .ei_cb_bl = mgc_blocking_ast,
1080 .ei_cb_cp = ldlm_completion_ast,
1082 struct ptlrpc_request *req;
1083 int short_limit = cld_is_sptlrpc(cld);
1087 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
1088 cld->cld_resid.name[0]);
1090 /* We need a callback for every lockholder, so don't try to
1091 ldlm_lock_match (see rev 1.1.2.11.2.47) */
1092 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1093 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
1098 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1099 ptlrpc_request_set_replen(req);
1101 /* check if this is server or client */
1102 if (cld->cld_cfg.cfg_sb) {
1103 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
1104 if (lsi && IS_SERVER(lsi))
1107 /* Limit how long we will wait for the enqueue to complete */
1108 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
1109 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1110 NULL, 0, LVB_T_NONE, lockh, 0);
1111 /* A failed enqueue should still call the mgc_blocking_ast,
1112 where it will be requeued if needed ("grant failed"). */
1113 ptlrpc_req_finished(req);
1117 static int mgc_cancel(struct obd_export *exp, ldlm_mode_t mode,
1118 struct lustre_handle *lockh)
1122 ldlm_lock_decref(lockh, mode);
1127 static void mgc_notify_active(struct obd_device *unused)
1129 /* wakeup mgc_requeue_thread to requeue mgc lock */
1130 spin_lock(&config_list_lock);
1132 spin_unlock(&config_list_lock);
1135 /* TODO: Help the MGS rebuild nidtbl. -jay */
1138 /* Send target_reg message to MGS */
1139 static int mgc_target_register(struct obd_export *exp,
1140 struct mgs_target_info *mti)
1142 struct ptlrpc_request *req;
1143 struct mgs_target_info *req_mti, *rep_mti;
1147 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1148 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1153 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1155 ptlrpc_req_finished(req);
1159 memcpy(req_mti, mti, sizeof(*req_mti));
1160 ptlrpc_request_set_replen(req);
1161 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1162 /* Limit how long we will wait for the enqueue to complete */
1163 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1165 rc = ptlrpc_queue_wait(req);
1167 rep_mti = req_capsule_server_get(&req->rq_pill,
1168 &RMF_MGS_TARGET_INFO);
1169 memcpy(mti, rep_mti, sizeof(*rep_mti));
1170 CDEBUG(D_MGC, "register %s got index = %d\n",
1171 mti->mti_svname, mti->mti_stripe_index);
1173 ptlrpc_req_finished(req);
1178 static int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1179 u32 keylen, void *key,
1180 u32 vallen, void *val,
1181 struct ptlrpc_request_set *set)
1186 /* Turn off initial_recov after we try all backup servers once */
1187 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1188 struct obd_import *imp = class_exp2cliimp(exp);
1190 if (vallen != sizeof(int))
1192 value = *(int *)val;
1193 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1194 imp->imp_obd->obd_name, value,
1195 imp->imp_deactive, imp->imp_invalid,
1196 imp->imp_replayable, imp->imp_obd->obd_replayable,
1197 ptlrpc_import_state_name(imp->imp_state));
1198 /* Resurrect if we previously died */
1199 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1200 imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1201 ptlrpc_reconnect_import(imp);
1204 /* FIXME move this to mgc_process_config */
1205 if (KEY_IS(KEY_REGISTER_TARGET)) {
1206 struct mgs_target_info *mti;
1207 if (vallen != sizeof(struct mgs_target_info))
1209 mti = (struct mgs_target_info *)val;
1210 CDEBUG(D_MGC, "register_target %s %#x\n",
1211 mti->mti_svname, mti->mti_flags);
1212 rc = mgc_target_register(exp, mti);
1215 if (KEY_IS(KEY_SET_FS)) {
1216 struct super_block *sb = (struct super_block *)val;
1218 if (vallen != sizeof(struct super_block))
1221 rc = mgc_fs_setup(exp->exp_obd, sb);
1224 if (KEY_IS(KEY_CLEAR_FS)) {
1227 rc = mgc_fs_cleanup(exp->exp_obd);
1230 if (KEY_IS(KEY_SET_INFO)) {
1231 struct mgs_send_param *msp;
1233 msp = (struct mgs_send_param *)val;
1234 rc = mgc_set_mgs_param(exp, msp);
1237 if (KEY_IS(KEY_MGSSEC)) {
1238 struct client_obd *cli = &exp->exp_obd->u.cli;
1239 struct sptlrpc_flavor flvr;
1242 * empty string means using current flavor, if which haven't
1243 * been set yet, set it as null.
1245 * if flavor has been set previously, check the asking flavor
1246 * must match the existing one.
1249 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1255 rc = sptlrpc_parse_flavor(val, &flvr);
1257 CERROR("invalid sptlrpc flavor %s to MGS\n",
1263 * caller already hold a mutex
1265 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1266 cli->cl_flvr_mgc = flvr;
1267 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1268 sizeof(flvr)) != 0) {
1271 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1273 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1274 "currently %s is in use\n",
1284 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1285 __u32 keylen, void *key,
1286 __u32 *vallen, void *val,
1287 struct lov_stripe_md *unused)
1291 if (KEY_IS(KEY_CONN_DATA)) {
1292 struct obd_import *imp = class_exp2cliimp(exp);
1293 struct obd_connect_data *data = val;
1295 if (*vallen == sizeof(*data)) {
1296 *data = imp->imp_connect_data;
1304 static int mgc_import_event(struct obd_device *obd,
1305 struct obd_import *imp,
1306 enum obd_import_event event)
1310 LASSERT(imp->imp_obd == obd);
1311 CDEBUG(D_MGC, "import event %#x\n", event);
1314 case IMP_EVENT_DISCON:
1315 /* MGC imports should not wait for recovery */
1316 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1317 ptlrpc_pinger_ir_down();
1319 case IMP_EVENT_INACTIVE:
1321 case IMP_EVENT_INVALIDATE: {
1322 struct ldlm_namespace *ns = obd->obd_namespace;
1323 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1326 case IMP_EVENT_ACTIVE:
1327 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1328 /* Clearing obd_no_recov allows us to continue pinging */
1329 obd->obd_no_recov = 0;
1330 mgc_notify_active(obd);
1331 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1332 ptlrpc_pinger_ir_up();
1336 case IMP_EVENT_DEACTIVATE:
1337 case IMP_EVENT_ACTIVATE:
1340 CERROR("Unknown import event %#x\n", event);
1347 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_CACHE_SHIFT),
1348 CONFIG_READ_NRPAGES = 4
1351 static int mgc_apply_recover_logs(struct obd_device *mgc,
1352 struct config_llog_data *cld,
1354 void *data, int datalen, bool mne_swab)
1356 struct config_llog_instance *cfg = &cld->cld_cfg;
1357 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1358 struct mgs_nidtbl_entry *entry;
1359 struct lustre_cfg *lcfg;
1360 struct lustre_cfg_bufs bufs;
1361 u64 prev_version = 0;
1370 LASSERT(cfg->cfg_instance != NULL);
1371 LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1373 OBD_ALLOC(inst, PAGE_CACHE_SIZE);
1377 if (!IS_SERVER(lsi)) {
1378 pos = snprintf(inst, PAGE_CACHE_SIZE, "%p", cfg->cfg_instance);
1379 if (pos >= PAGE_CACHE_SIZE) {
1380 OBD_FREE(inst, PAGE_CACHE_SIZE);
1384 LASSERT(IS_MDT(lsi));
1385 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1388 OBD_FREE(inst, PAGE_CACHE_SIZE);
1396 bufsz = PAGE_CACHE_SIZE - pos;
1398 while (datalen > 0) {
1399 int entry_len = sizeof(*entry);
1401 struct obd_device *obd;
1408 if (datalen < sizeof(*entry))
1411 entry = (typeof(entry))(data + off);
1414 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1416 if (entry->mne_nid_count == 0) /* at least one nid entry */
1418 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1421 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1422 if (datalen < entry_len) /* must have entry_len at least */
1425 /* Keep this swab for normal mixed endian handling. LU-1644 */
1427 lustre_swab_mgs_nidtbl_entry(entry);
1428 if (entry->mne_length > PAGE_CACHE_SIZE) {
1429 CERROR("MNE too large (%u)\n", entry->mne_length);
1433 if (entry->mne_length < entry_len)
1436 off += entry->mne_length;
1437 datalen -= entry->mne_length;
1441 if (entry->mne_version > max_version) {
1442 CERROR("entry index(%lld) is over max_index(%lld)\n",
1443 entry->mne_version, max_version);
1447 if (prev_version >= entry->mne_version) {
1448 CERROR("index unsorted, prev %lld, now %lld\n",
1449 prev_version, entry->mne_version);
1452 prev_version = entry->mne_version;
1455 * Write a string with format "nid::instance" to
1456 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1459 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1460 memset(buf, 0, bufsz);
1464 /* lustre-OST0001-osc-<instance #> */
1465 strcpy(obdname, cld->cld_logname);
1466 cname = strrchr(obdname, '-');
1467 if (cname == NULL) {
1468 CERROR("mgc %s: invalid logname %s\n",
1469 mgc->obd_name, obdname);
1473 pos = cname - obdname;
1475 pos += sprintf(obdname + pos, "-%s%04x",
1476 is_ost ? "OST" : "MDT", entry->mne_index);
1478 cname = is_ost ? "osc" : "mdc",
1479 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1480 lustre_cfg_bufs_reset(&bufs, obdname);
1482 /* find the obd by obdname */
1483 obd = class_name2obd(obdname);
1485 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1486 mgc->obd_name, obdname);
1488 /* this is a safe race, when the ost is starting up...*/
1492 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1495 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1498 down_read(&obd->u.cli.cl_sem);
1499 if (obd->u.cli.cl_import == NULL) {
1500 /* client does not connect to the OST yet */
1501 up_read(&obd->u.cli.cl_sem);
1506 /* iterate all nids to find one */
1507 /* find uuid by nid */
1509 for (i = 0; i < entry->mne_nid_count; i++) {
1510 rc = client_import_find_conn(obd->u.cli.cl_import,
1512 (struct obd_uuid *)uuid);
1517 up_read(&obd->u.cli.cl_sem);
1519 CERROR("mgc: cannot find uuid by nid %s\n",
1520 libcfs_nid2str(entry->u.nids[0]));
1524 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1525 uuid, libcfs_nid2str(entry->u.nids[0]));
1527 pos += strlen(uuid);
1528 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1529 LASSERT(pos < bufsz);
1531 lustre_cfg_bufs_set_string(&bufs, 1, params);
1533 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1539 CDEBUG(D_INFO, "ir apply logs "LPD64"/"LPD64" for %s -> %s\n",
1540 prev_version, max_version, obdname, params);
1542 rc = class_process_config(lcfg);
1543 lustre_cfg_free(lcfg);
1545 CDEBUG(D_INFO, "process config for %s error %d\n",
1548 /* continue, even one with error */
1551 OBD_FREE(inst, PAGE_CACHE_SIZE);
1556 * This function is called if this client was notified for target restarting
1557 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery logs.
1559 static int mgc_process_recover_log(struct obd_device *obd,
1560 struct config_llog_data *cld)
1562 struct ptlrpc_request *req = NULL;
1563 struct config_llog_instance *cfg = &cld->cld_cfg;
1564 struct mgs_config_body *body;
1565 struct mgs_config_res *res;
1566 struct ptlrpc_bulk_desc *desc;
1567 struct page **pages;
1570 bool mne_swab = false;
1576 /* allocate buffer for bulk transfer.
1577 * if this is the first time for this mgs to read logs,
1578 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1579 * once; otherwise, it only reads increment of logs, this should be
1580 * small and CONFIG_READ_NRPAGES will be used.
1582 nrpages = CONFIG_READ_NRPAGES;
1583 if (cfg->cfg_last_idx == 0) /* the first time */
1584 nrpages = CONFIG_READ_NRPAGES_INIT;
1586 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1588 GOTO(out, rc = -ENOMEM);
1590 for (i = 0; i < nrpages; i++) {
1591 pages[i] = alloc_page(GFP_IOFS);
1592 if (pages[i] == NULL)
1593 GOTO(out, rc = -ENOMEM);
1597 LASSERT(cld_is_recover(cld));
1598 LASSERT(mutex_is_locked(&cld->cld_lock));
1599 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1600 &RQF_MGS_CONFIG_READ);
1602 GOTO(out, rc = -ENOMEM);
1604 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1609 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1610 LASSERT(body != NULL);
1611 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1612 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1613 >= sizeof(body->mcb_name))
1614 GOTO(out, rc = -E2BIG);
1615 body->mcb_offset = cfg->cfg_last_idx + 1;
1616 body->mcb_type = cld->cld_type;
1617 body->mcb_bits = PAGE_CACHE_SHIFT;
1618 body->mcb_units = nrpages;
1620 /* allocate bulk transfer descriptor */
1621 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1, BULK_PUT_SINK,
1624 GOTO(out, rc = -ENOMEM);
1626 for (i = 0; i < nrpages; i++)
1627 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1629 ptlrpc_request_set_replen(req);
1630 rc = ptlrpc_queue_wait(req);
1634 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1635 if (res->mcr_size < res->mcr_offset)
1636 GOTO(out, rc = -EINVAL);
1638 /* always update the index even though it might have errors with
1639 * handling the recover logs */
1640 cfg->cfg_last_idx = res->mcr_offset;
1641 eof = res->mcr_offset == res->mcr_size;
1643 CDEBUG(D_INFO, "Latest version "LPD64", more %d.\n",
1644 res->mcr_offset, eof == false);
1646 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1648 GOTO(out, rc = ealen);
1650 if (ealen > nrpages << PAGE_CACHE_SHIFT)
1651 GOTO(out, rc = -EINVAL);
1653 if (ealen == 0) { /* no logs transferred */
1659 mne_swab = !!ptlrpc_rep_need_swab(req);
1660 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
1661 /* This import flag means the server did an extra swab of IR MNE
1662 * records (fixed in LU-1252), reverse it here if needed. LU-1644 */
1663 if (unlikely(req->rq_import->imp_need_mne_swab))
1664 mne_swab = !mne_swab;
1667 for (i = 0; i < nrpages && ealen > 0; i++) {
1671 ptr = kmap(pages[i]);
1672 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1673 min_t(int, ealen, PAGE_CACHE_SIZE),
1677 CWARN("Process recover log %s error %d\n",
1678 cld->cld_logname, rc2);
1682 ealen -= PAGE_CACHE_SIZE;
1687 ptlrpc_req_finished(req);
1689 if (rc == 0 && !eof)
1693 for (i = 0; i < nrpages; i++) {
1694 if (pages[i] == NULL)
1696 __free_page(pages[i]);
1698 OBD_FREE(pages, sizeof(*pages) * nrpages);
1703 /* Copy a remote log locally */
1704 static int mgc_llog_local_copy(const struct lu_env *env,
1705 struct obd_device *obd,
1706 struct llog_ctxt *rctxt,
1707 struct llog_ctxt *lctxt, char *logname)
1715 * - copy it to backup using llog_backup()
1716 * - copy remote llog to logname using llog_backup()
1717 * - if failed then move bakup to logname again
1720 OBD_ALLOC(temp_log, strlen(logname) + 1);
1723 sprintf(temp_log, "%sT", logname);
1725 /* make a copy of local llog at first */
1726 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1727 if (rc < 0 && rc != -ENOENT)
1729 /* copy remote llog to the local copy */
1730 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1731 if (rc == -ENOENT) {
1732 /* no remote llog, delete local one too */
1733 llog_erase(env, lctxt, NULL, logname);
1734 } else if (rc < 0) {
1735 /* error during backup, get local one back from the copy */
1736 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1738 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1739 obd->obd_name, logname, rc);
1741 llog_erase(env, lctxt, NULL, temp_log);
1742 OBD_FREE(temp_log, strlen(logname) + 1);
1746 /* local_only means it cannot get remote llogs */
1747 static int mgc_process_cfg_log(struct obd_device *mgc,
1748 struct config_llog_data *cld, int local_only)
1750 struct llog_ctxt *ctxt, *lctxt = NULL;
1751 struct client_obd *cli = &mgc->u.cli;
1752 struct lustre_sb_info *lsi = NULL;
1754 bool sptlrpc_started = false;
1760 LASSERT(mutex_is_locked(&cld->cld_lock));
1763 * local copy of sptlrpc log is controlled elsewhere, don't try to
1766 if (cld_is_sptlrpc(cld) && local_only)
1769 if (cld->cld_cfg.cfg_sb)
1770 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1776 rc = lu_env_init(env, LCT_MG_THREAD);
1780 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1783 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1785 /* Copy the setup log locally if we can. Don't mess around if we're
1786 * running an MGS though (logs are already local). */
1787 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1788 cli->cl_mgc_configs_dir != NULL &&
1789 lu2dt_dev(cli->cl_mgc_configs_dir->do_lu.lo_dev) ==
1792 /* Only try to copy log if we have the lock. */
1793 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1795 if (local_only || rc) {
1796 if (strcmp(cld->cld_logname, PARAMS_FILENAME) != 0 &&
1797 llog_is_empty(env, lctxt, cld->cld_logname)) {
1798 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1799 "log %s and no local copy."
1800 "\n", cld->cld_logname);
1801 GOTO(out_pop, rc = -ENOENT);
1803 CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1804 "copy for now, will try to update later.\n",
1808 /* Now, whether we copied or not, start using the local llog.
1809 * If we failed to copy, we'll start using whatever the old
1811 llog_ctxt_put(ctxt);
1815 if (local_only) /* no local log at client side */
1816 GOTO(out_pop, rc = -EIO);
1819 if (cld_is_sptlrpc(cld)) {
1820 sptlrpc_conf_log_update_begin(cld->cld_logname);
1821 sptlrpc_started = true;
1824 /* logname and instance info should be the same, so use our
1825 * copy of the instance for the update. The cfg_last_idx will
1826 * be updated here. */
1827 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1832 __llog_ctxt_put(env, ctxt);
1834 __llog_ctxt_put(env, lctxt);
1837 * update settings on existing OBDs. doing it inside
1838 * of llog_process_lock so no device is attaching/detaching
1840 * the logname must be <fsname>-sptlrpc
1842 if (sptlrpc_started) {
1843 LASSERT(cld_is_sptlrpc(cld));
1844 sptlrpc_conf_log_update_end(cld->cld_logname);
1845 class_notify_sptlrpc_conf(cld->cld_logname,
1846 strlen(cld->cld_logname) -
1847 strlen("-sptlrpc"));
1856 /** Get a config log from the MGS and process it.
1857 * This func is called for both clients and servers.
1858 * Copy the log locally before parsing it if appropriate (non-MGS server)
1860 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1862 struct lustre_handle lockh = { 0 };
1863 __u64 flags = LDLM_FL_NO_LRU;
1869 /* I don't want multiple processes running process_log at once --
1870 sounds like badness. It actually might be fine, as long as
1871 we're not trying to update from the same log
1872 simultaneously (in which case we should use a per-log sem.) */
1873 mutex_lock(&cld->cld_lock);
1874 if (cld->cld_stopping) {
1875 mutex_unlock(&cld->cld_lock);
1879 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1881 CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
1882 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1884 /* Get the cfg lock on the llog */
1885 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1886 LCK_CR, &flags, NULL, NULL, NULL,
1887 cld, 0, NULL, &lockh);
1889 /* Get the cld, it will be released in mgc_blocking_ast. */
1890 config_log_get(cld);
1891 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1894 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1896 /* mark cld_lostlock so that it will requeue
1897 * after MGC becomes available. */
1898 cld->cld_lostlock = 1;
1899 /* Get extra reference, it will be put in requeue thread */
1900 config_log_get(cld);
1904 if (cld_is_recover(cld)) {
1905 rc = 0; /* this is not a fatal error for recover log */
1907 rc = mgc_process_recover_log(mgc, cld);
1909 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1912 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1913 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1915 mutex_unlock(&cld->cld_lock);
1917 /* Now drop the lock so MGS can revoke it */
1919 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, LCK_CR, &lockh);
1921 CERROR("Can't drop cfg lock: %d\n", rcl);
1928 /** Called from lustre_process_log.
1929 * LCFG_LOG_START gets the config log from the MGS, processes it to start
1930 * any services, and adds it to the list logs to watch (follow).
1932 static int mgc_process_config(struct obd_device *obd, size_t len, void *buf)
1934 struct lustre_cfg *lcfg = buf;
1935 struct config_llog_instance *cfg = NULL;
1940 switch(lcfg->lcfg_command) {
1941 case LCFG_LOV_ADD_OBD: {
1942 /* Overloading this cfg command: register a new target */
1943 struct mgs_target_info *mti;
1945 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1946 sizeof(struct mgs_target_info))
1947 GOTO(out, rc = -EINVAL);
1949 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1950 CDEBUG(D_MGC, "add_target %s %#x\n",
1951 mti->mti_svname, mti->mti_flags);
1952 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1955 case LCFG_LOV_DEL_OBD:
1956 /* Unregister has no meaning at the moment. */
1957 CERROR("lov_del_obd unimplemented\n");
1960 case LCFG_SPTLRPC_CONF: {
1961 rc = sptlrpc_process_config(lcfg);
1964 case LCFG_LOG_START: {
1965 struct config_llog_data *cld;
1966 struct super_block *sb;
1968 logname = lustre_cfg_string(lcfg, 1);
1969 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1970 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1972 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1975 /* We're only called through here on the initial mount */
1976 rc = config_log_add(obd, logname, cfg, sb);
1979 cld = config_log_find(logname, cfg);
1986 /* FIXME only set this for old logs! Right now this forces
1987 us to always skip the "inside markers" check */
1988 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1990 rc = mgc_process_log(obd, cld);
1991 if (rc == 0 && cld->cld_recover != NULL) {
1992 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
1993 imp_connect_data, IMP_RECOV)) {
1994 rc = mgc_process_log(obd, cld->cld_recover);
1996 struct config_llog_data *cir = cld->cld_recover;
1997 cld->cld_recover = NULL;
1998 config_log_put(cir);
2001 CERROR("Cannot process recover llog %d\n", rc);
2004 if (rc == 0 && cld->cld_params != NULL) {
2005 rc = mgc_process_log(obd, cld->cld_params);
2006 if (rc == -ENOENT) {
2007 CDEBUG(D_MGC, "There is no params "
2008 "config file yet\n");
2011 /* params log is optional */
2013 CERROR("%s: can't process params llog: rc = %d\n",
2016 config_log_put(cld);
2020 case LCFG_LOG_END: {
2021 logname = lustre_cfg_string(lcfg, 1);
2023 if (lcfg->lcfg_bufcount >= 2)
2024 cfg = (struct config_llog_instance *)lustre_cfg_buf(
2026 rc = config_log_end(logname, cfg);
2030 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
2031 GOTO(out, rc = -EINVAL);
2039 static struct obd_ops mgc_obd_ops = {
2040 .o_owner = THIS_MODULE,
2041 .o_setup = mgc_setup,
2042 .o_precleanup = mgc_precleanup,
2043 .o_cleanup = mgc_cleanup,
2044 .o_add_conn = client_import_add_conn,
2045 .o_del_conn = client_import_del_conn,
2046 .o_connect = client_connect_import,
2047 .o_disconnect = client_disconnect_export,
2048 .o_set_info_async = mgc_set_info_async,
2049 .o_get_info = mgc_get_info,
2050 .o_import_event = mgc_import_event,
2051 .o_process_config = mgc_process_config,
2054 static int __init mgc_init(void)
2056 return class_register_type(&mgc_obd_ops, NULL, true, NULL,
2057 LUSTRE_MGC_NAME, NULL);
2060 static void /*__exit*/ mgc_exit(void)
2062 class_unregister_type(LUSTRE_MGC_NAME);
2065 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2066 MODULE_DESCRIPTION("Lustre Management Client");
2067 MODULE_LICENSE("GPL");
2069 module_init(mgc_init);
2070 module_exit(mgc_exit);