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 <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,
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:
84 res_id->name[1] = cpu_to_le64(resname);
85 CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.8s)\n", name,
86 res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
90 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id, int type)
92 /* fsname is at most 8 chars long, maybe contain "-".
93 * e.g. "lustre", "SUN-000" */
94 return mgc_name2resid(fsname, strlen(fsname), res_id, type);
96 EXPORT_SYMBOL(mgc_fsname2resid);
98 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id, int type)
103 /* logname consists of "fsname-nodetype".
104 * e.g. "lustre-MDT0001", "SUN-000-client" */
105 name_end = strrchr(logname, '-');
107 len = name_end - logname;
108 return mgc_name2resid(logname, len, res_id, type);
111 /********************** config llog list **********************/
112 static LIST_HEAD(config_llog_list);
113 static DEFINE_SPINLOCK(config_list_lock);
115 /* Take a reference to a config log */
116 static int config_log_get(struct config_llog_data *cld)
118 atomic_inc(&cld->cld_refcount);
119 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
120 atomic_read(&cld->cld_refcount));
124 /* Drop a reference to a config log. When no longer referenced,
125 we can free the config log data */
126 static void config_log_put(struct config_llog_data *cld)
128 CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
129 atomic_read(&cld->cld_refcount));
130 LASSERT(atomic_read(&cld->cld_refcount) > 0);
132 /* spinlock to make sure no item with 0 refcount in the list */
133 if (atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
134 list_del(&cld->cld_list_chain);
135 spin_unlock(&config_list_lock);
137 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
139 if (cld->cld_recover)
140 config_log_put(cld->cld_recover);
141 if (cld->cld_sptlrpc)
142 config_log_put(cld->cld_sptlrpc);
143 if (cld_is_sptlrpc(cld))
144 sptlrpc_conf_log_stop(cld->cld_logname);
146 class_export_put(cld->cld_mgcexp);
147 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
151 /* Find a config log by name */
153 struct config_llog_data *config_log_find(char *logname,
154 struct config_llog_instance *cfg)
156 struct config_llog_data *cld;
157 struct config_llog_data *found = NULL;
160 LASSERT(logname != NULL);
162 instance = cfg ? cfg->cfg_instance : NULL;
163 spin_lock(&config_list_lock);
164 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
165 /* check if instance equals */
166 if (instance != cld->cld_cfg.cfg_instance)
169 /* instance may be NULL, should check name */
170 if (strcmp(logname, cld->cld_logname) == 0) {
176 atomic_inc(&found->cld_refcount);
177 LASSERT(found->cld_stopping == 0 || cld_is_sptlrpc(found) == 0);
179 spin_unlock(&config_list_lock);
184 struct config_llog_data *do_config_log_add(struct obd_device *obd,
187 struct config_llog_instance *cfg,
188 struct super_block *sb)
190 struct config_llog_data *cld;
193 CDEBUG(D_MGC, "do adding config log %s:%p\n", logname,
194 cfg ? cfg->cfg_instance : 0);
196 OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
198 return ERR_PTR(-ENOMEM);
200 strcpy(cld->cld_logname, logname);
204 cld->cld_cfg.cfg_callback = class_config_llog_handler;
205 mutex_init(&cld->cld_lock);
206 cld->cld_cfg.cfg_last_idx = 0;
207 cld->cld_cfg.cfg_flags = 0;
208 cld->cld_cfg.cfg_sb = sb;
209 cld->cld_type = type;
210 atomic_set(&cld->cld_refcount, 1);
212 /* Keep the mgc around until we are done */
213 cld->cld_mgcexp = class_export_get(obd->obd_self_export);
215 if (cld_is_sptlrpc(cld)) {
216 sptlrpc_conf_log_start(logname);
217 cld->cld_cfg.cfg_obdname = obd->obd_name;
220 rc = mgc_logname2resid(logname, &cld->cld_resid, type);
222 spin_lock(&config_list_lock);
223 list_add(&cld->cld_list_chain, &config_llog_list);
224 spin_unlock(&config_list_lock);
231 if (cld_is_sptlrpc(cld)) {
232 rc = mgc_process_log(obd, cld);
233 if (rc && rc != -ENOENT)
234 CERROR("failed processing sptlrpc log: %d\n", rc);
240 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
242 struct config_llog_instance *cfg,
243 struct super_block *sb)
245 struct config_llog_instance lcfg = *cfg;
246 struct lustre_sb_info *lsi = s2lsi(sb);
247 struct config_llog_data *cld;
253 /* for osp-on-ost, see lustre_start_osp() */
254 if (IS_MDT(lsi) && lcfg.cfg_instance)
257 /* we have to use different llog for clients and mdts for cmd
258 * where only clients are notified if one of cmd server restarts */
259 LASSERT(strlen(fsname) < sizeof(logname) / 2);
260 strcpy(logname, fsname);
261 if (IS_SERVER(lsi)) { /* mdt */
262 LASSERT(lcfg.cfg_instance == NULL);
263 lcfg.cfg_instance = sb;
264 strcat(logname, "-mdtir");
266 LASSERT(lcfg.cfg_instance != NULL);
267 strcat(logname, "-cliir");
270 cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
275 /** Add this log to the list of active logs watched by an MGC.
276 * Active means we're watching for updates.
277 * We have one active log per "mount" - client instance or servername.
278 * Each instance may be at a different point in the log.
280 static int config_log_add(struct obd_device *obd, char *logname,
281 struct config_llog_instance *cfg,
282 struct super_block *sb)
284 struct lustre_sb_info *lsi = s2lsi(sb);
285 struct config_llog_data *cld;
286 struct config_llog_data *sptlrpc_cld;
290 CDEBUG(D_MGC, "adding config log %s:%p\n", logname, cfg->cfg_instance);
293 * for each regular log, the depended sptlrpc log name is
294 * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
296 ptr = strrchr(logname, '-');
297 if (ptr == NULL || ptr - logname > 8) {
298 CERROR("logname %s is too long\n", logname);
302 memcpy(seclogname, logname, ptr - logname);
303 strcpy(seclogname + (ptr - logname), "-sptlrpc");
305 sptlrpc_cld = config_log_find(seclogname, NULL);
306 if (sptlrpc_cld == NULL) {
307 sptlrpc_cld = do_config_log_add(obd, seclogname,
308 CONFIG_T_SPTLRPC, NULL, NULL);
309 if (IS_ERR(sptlrpc_cld)) {
310 CERROR("can't create sptlrpc log: %s\n", seclogname);
311 return PTR_ERR(sptlrpc_cld);
315 cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
317 CERROR("can't create log: %s\n", logname);
318 config_log_put(sptlrpc_cld);
322 cld->cld_sptlrpc = sptlrpc_cld;
324 LASSERT(lsi->lsi_lmd);
325 if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)) {
326 struct config_llog_data *recover_cld;
327 *strrchr(seclogname, '-') = 0;
328 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
329 if (IS_ERR(recover_cld)) {
331 return PTR_ERR(recover_cld);
333 cld->cld_recover = recover_cld;
339 DEFINE_MUTEX(llog_process_lock);
341 /** Stop watching for updates on this log.
343 static int config_log_end(char *logname, struct config_llog_instance *cfg)
345 struct config_llog_data *cld;
346 struct config_llog_data *cld_sptlrpc = NULL;
347 struct config_llog_data *cld_recover = NULL;
350 cld = config_log_find(logname, cfg);
354 mutex_lock(&cld->cld_lock);
356 * if cld_stopping is set, it means we didn't start the log thus
357 * not owning the start ref. this can happen after previous umount:
358 * the cld still hanging there waiting for lock cancel, and we
359 * remount again but failed in the middle and call log_end without
362 if (unlikely(cld->cld_stopping)) {
363 mutex_unlock(&cld->cld_lock);
364 /* drop the ref from the find */
369 cld->cld_stopping = 1;
371 cld_recover = cld->cld_recover;
372 cld->cld_recover = NULL;
373 mutex_unlock(&cld->cld_lock);
376 mutex_lock(&cld_recover->cld_lock);
377 cld_recover->cld_stopping = 1;
378 mutex_unlock(&cld_recover->cld_lock);
379 config_log_put(cld_recover);
382 spin_lock(&config_list_lock);
383 cld_sptlrpc = cld->cld_sptlrpc;
384 cld->cld_sptlrpc = NULL;
385 spin_unlock(&config_list_lock);
388 config_log_put(cld_sptlrpc);
390 /* drop the ref from the find */
392 /* drop the start ref */
395 CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
400 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
402 struct obd_device *obd = data;
403 struct obd_import *imp = obd->u.cli.cl_import;
404 struct obd_connect_data *ocd = &imp->imp_connect_data;
405 struct config_llog_data *cld;
407 seq_printf(m, "imperative_recovery: %s\n",
408 OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ENABLED" : "DISABLED");
409 seq_printf(m, "client_state:\n");
411 spin_lock(&config_list_lock);
412 list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
413 if (cld->cld_recover == NULL)
415 seq_printf(m, " - { client: %s, nidtbl_version: %u }\n",
417 cld->cld_recover->cld_cfg.cfg_last_idx);
419 spin_unlock(&config_list_lock);
424 /* reenqueue any lost locks */
425 #define RQ_RUNNING 0x1
429 static int rq_state = 0;
430 static wait_queue_head_t rq_waitq;
431 static DECLARE_COMPLETION(rq_exit);
433 static void do_requeue(struct config_llog_data *cld)
435 LASSERT(atomic_read(&cld->cld_refcount) > 0);
437 /* Do not run mgc_process_log on a disconnected export or an
438 export which is being disconnected. Take the client
439 semaphore to make the check non-racy. */
440 down_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
441 if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
442 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
443 mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
445 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
448 up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
451 /* this timeout represents how many seconds MGC should wait before
452 * requeue config and recover lock to the MGS. We need to randomize this
453 * in order to not flood the MGS.
455 #define MGC_TIMEOUT_MIN_SECONDS 5
456 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
458 static int mgc_requeue_thread(void *data)
462 CDEBUG(D_MGC, "Starting requeue thread\n");
464 /* Keep trying failed locks periodically */
465 spin_lock(&config_list_lock);
466 rq_state |= RQ_RUNNING;
468 struct l_wait_info lwi;
469 struct config_llog_data *cld, *cld_prev;
470 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
471 int stopped = !!(rq_state & RQ_STOP);
474 /* Any new or requeued lostlocks will change the state */
475 rq_state &= ~(RQ_NOW | RQ_LATER);
476 spin_unlock(&config_list_lock);
478 /* Always wait a few seconds to allow the server who
479 caused the lock revocation to finish its setup, plus some
480 random so everyone doesn't try to reconnect at once. */
481 to = MGC_TIMEOUT_MIN_SECONDS * HZ;
482 to += rand * HZ / 100; /* rand is centi-seconds */
483 lwi = LWI_TIMEOUT(to, NULL, NULL);
484 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi);
487 * iterate & processing through the list. for each cld, process
488 * its depending sptlrpc cld firstly (if any) and then itself.
490 * it's guaranteed any item in the list must have
491 * reference > 0; and if cld_lostlock is set, at
492 * least one reference is taken by the previous enqueue.
496 spin_lock(&config_list_lock);
497 list_for_each_entry(cld, &config_llog_list,
499 if (!cld->cld_lostlock)
502 spin_unlock(&config_list_lock);
504 LASSERT(atomic_read(&cld->cld_refcount) > 0);
506 /* Whether we enqueued again or not in mgc_process_log,
507 * we're done with the ref from the old enqueue */
509 config_log_put(cld_prev);
512 cld->cld_lostlock = 0;
513 if (likely(!stopped))
516 spin_lock(&config_list_lock);
518 spin_unlock(&config_list_lock);
520 config_log_put(cld_prev);
522 /* break after scanning the list so that we can drop
523 * refcount to losing lock clds */
524 if (unlikely(stopped)) {
525 spin_lock(&config_list_lock);
529 /* Wait a bit to see if anyone else needs a requeue */
530 lwi = (struct l_wait_info) { 0 };
531 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
533 spin_lock(&config_list_lock);
535 /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
536 rq_state &= ~RQ_RUNNING;
537 spin_unlock(&config_list_lock);
541 CDEBUG(D_MGC, "Ending requeue thread\n");
545 /* Add a cld to the list to requeue. Start the requeue thread if needed.
546 We are responsible for dropping the config log reference from here on out. */
547 static void mgc_requeue_add(struct config_llog_data *cld)
549 CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
550 cld->cld_logname, atomic_read(&cld->cld_refcount),
551 cld->cld_stopping, rq_state);
552 LASSERT(atomic_read(&cld->cld_refcount) > 0);
554 mutex_lock(&cld->cld_lock);
555 if (cld->cld_stopping || cld->cld_lostlock) {
556 mutex_unlock(&cld->cld_lock);
559 /* this refcount will be released in mgc_requeue_thread. */
561 cld->cld_lostlock = 1;
562 mutex_unlock(&cld->cld_lock);
564 /* Hold lock for rq_state */
565 spin_lock(&config_list_lock);
566 if (rq_state & RQ_STOP) {
567 spin_unlock(&config_list_lock);
568 cld->cld_lostlock = 0;
572 spin_unlock(&config_list_lock);
577 /********************** class fns **********************/
578 static int mgc_local_llog_init(const struct lu_env *env,
579 struct obd_device *obd,
580 struct obd_device *disk)
582 struct llog_ctxt *ctxt;
585 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, disk,
590 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
592 ctxt->loc_dir = obd->u.cli.cl_mgc_configs_dir;
598 static int mgc_local_llog_fini(const struct lu_env *env,
599 struct obd_device *obd)
601 struct llog_ctxt *ctxt;
603 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
604 llog_cleanup(env, ctxt);
609 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb)
611 struct lustre_sb_info *lsi = s2lsi(sb);
612 struct client_obd *cli = &obd->u.cli;
613 struct lu_fid rfid, fid;
614 struct dt_object *root, *dto;
619 LASSERT(lsi->lsi_dt_dev);
625 /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
626 down(&cli->cl_mgc_sem);
628 cfs_cleanup_group_info();
630 /* Setup the configs dir */
631 rc = lu_env_init(env, LCT_MG_THREAD);
635 fid.f_seq = FID_SEQ_LOCAL_NAME;
638 rc = local_oid_storage_init(env, lsi->lsi_dt_dev, &fid,
643 rc = dt_root_get(env, lsi->lsi_dt_dev, &rfid);
647 root = dt_locate_at(env, lsi->lsi_dt_dev, &rfid,
648 &cli->cl_mgc_los->los_dev->dd_lu_dev);
649 if (unlikely(IS_ERR(root)))
650 GOTO(out_los, rc = PTR_ERR(root));
652 dto = local_file_find_or_create(env, cli->cl_mgc_los, root,
654 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
655 lu_object_put_nocache(env, &root->do_lu);
657 GOTO(out_los, rc = PTR_ERR(dto));
659 cli->cl_mgc_configs_dir = dto;
661 LASSERT(lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt);
662 rc = mgc_local_llog_init(env, obd, lsi->lsi_osd_exp->exp_obd);
666 /* We take an obd ref to insure that we can't get to mgc_cleanup
667 * without calling mgc_fs_cleanup first. */
668 class_incref(obd, "mgc_fs", obd);
670 /* We keep the cl_mgc_sem until mgc_fs_cleanup */
673 lu_object_put(env, &cli->cl_mgc_configs_dir->do_lu);
674 cli->cl_mgc_configs_dir = NULL;
678 local_oid_storage_fini(env, cli->cl_mgc_los);
679 cli->cl_mgc_los = NULL;
680 up(&cli->cl_mgc_sem);
689 static int mgc_fs_cleanup(struct obd_device *obd)
692 struct client_obd *cli = &obd->u.cli;
695 LASSERT(cli->cl_mgc_los != NULL);
697 rc = lu_env_init(&env, LCT_MG_THREAD);
701 mgc_local_llog_fini(&env, obd);
703 lu_object_put_nocache(&env, &cli->cl_mgc_configs_dir->do_lu);
704 cli->cl_mgc_configs_dir = NULL;
706 local_oid_storage_fini(&env, cli->cl_mgc_los);
707 cli->cl_mgc_los = NULL;
711 class_decref(obd, "mgc_fs", obd);
712 up(&cli->cl_mgc_sem);
717 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
719 struct llog_ctxt *ctxt;
722 /* setup only remote ctxt, the local disk context is switched per each
723 * filesystem during mgc_fs_setup() */
724 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
729 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
732 llog_initiator_connect(ctxt);
738 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
740 struct llog_ctxt *ctxt;
742 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
744 llog_cleanup(env, ctxt);
749 static atomic_t mgc_count = ATOMIC_INIT(0);
750 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
755 case OBD_CLEANUP_EARLY:
757 case OBD_CLEANUP_EXPORTS:
758 if (atomic_dec_and_test(&mgc_count)) {
760 /* stop requeue thread */
761 spin_lock(&config_list_lock);
762 running = rq_state & RQ_RUNNING;
765 spin_unlock(&config_list_lock);
768 wait_for_completion(&rq_exit);
771 obd_cleanup_client_import(obd);
772 rc = mgc_llog_fini(NULL, obd);
774 CERROR("failed to cleanup llogging subsystems\n");
780 static int mgc_cleanup(struct obd_device *obd)
784 /* COMPAT_146 - old config logs may have added profiles we don't
786 if (obd->obd_type->typ_refcnt <= 1)
787 /* Only for the last mgc */
788 class_del_profiles();
790 lprocfs_obd_cleanup(obd);
793 rc = client_obd_cleanup(obd);
797 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
799 struct lprocfs_static_vars lvars;
804 rc = client_obd_setup(obd, lcfg);
806 GOTO(err_decref, rc);
808 rc = mgc_llog_init(NULL, obd);
810 CERROR("failed to setup llogging subsystems\n");
811 GOTO(err_cleanup, rc);
814 lprocfs_mgc_init_vars(&lvars);
815 lprocfs_obd_setup(obd, lvars.obd_vars);
816 sptlrpc_lprocfs_cliobd_attach(obd);
818 if (atomic_inc_return(&mgc_count) == 1) {
820 init_waitqueue_head(&rq_waitq);
822 /* start requeue thread */
823 rc = PTR_ERR(kthread_run(mgc_requeue_thread, NULL,
825 if (IS_ERR_VALUE(rc)) {
826 CERROR("%s: Cannot start requeue thread (%d),"
827 "no more log updates!\n",
829 GOTO(err_cleanup, rc);
831 /* rc is the task_struct pointer of mgc_requeue_thread. */
838 client_obd_cleanup(obd);
844 /* based on ll_mdc_blocking_ast */
845 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
846 void *data, int flag)
848 struct lustre_handle lockh;
849 struct config_llog_data *cld = (struct config_llog_data *)data;
853 case LDLM_CB_BLOCKING:
854 /* mgs wants the lock, give it up... */
855 LDLM_DEBUG(lock, "MGC blocking CB");
856 ldlm_lock2handle(lock, &lockh);
857 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
859 case LDLM_CB_CANCELING:
860 /* We've given up the lock, prepare ourselves to update. */
861 LDLM_DEBUG(lock, "MGC cancel CB");
863 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
864 PLDLMRES(lock->l_resource),
865 (char *)&lock->l_resource->lr_name.name[0]);
868 CDEBUG(D_INFO, "missing data, won't requeue\n");
872 /* held at mgc_process_log(). */
873 LASSERT(atomic_read(&cld->cld_refcount) > 0);
874 /* Are we done with this log? */
875 if (cld->cld_stopping) {
876 CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
881 /* Make sure not to re-enqueue when the mgc is stopping
882 (we get called from client_disconnect_export) */
883 if (!lock->l_conn_export ||
884 !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
885 CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
892 mgc_requeue_add(cld);
902 /* Not sure where this should go... */
903 #define MGC_ENQUEUE_LIMIT 50
904 #define MGC_TARGET_REG_LIMIT 10
905 #define MGC_SEND_PARAM_LIMIT 10
907 /* Send parameter to MGS*/
908 static int mgc_set_mgs_param(struct obd_export *exp,
909 struct mgs_send_param *msp)
911 struct ptlrpc_request *req;
912 struct mgs_send_param *req_msp, *rep_msp;
915 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
916 &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
921 req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
923 ptlrpc_req_finished(req);
927 memcpy(req_msp, msp, sizeof(*req_msp));
928 ptlrpc_request_set_replen(req);
930 /* Limit how long we will wait for the enqueue to complete */
931 req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
932 rc = ptlrpc_queue_wait(req);
934 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
935 memcpy(msp, rep_msp, sizeof(*rep_msp));
938 ptlrpc_req_finished(req);
943 /* Take a config lock so we can get cancel notifications */
944 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
945 __u32 type, ldlm_policy_data_t *policy, __u32 mode,
946 __u64 *flags, void *bl_cb, void *cp_cb, void *gl_cb,
947 void *data, __u32 lvb_len, void *lvb_swabber,
948 struct lustre_handle *lockh)
950 struct config_llog_data *cld = (struct config_llog_data *)data;
951 struct ldlm_enqueue_info einfo = {
954 .ei_cb_bl = mgc_blocking_ast,
955 .ei_cb_cp = ldlm_completion_ast,
957 struct ptlrpc_request *req;
958 int short_limit = cld_is_sptlrpc(cld);
961 CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
962 cld->cld_resid.name[0]);
964 /* We need a callback for every lockholder, so don't try to
965 ldlm_lock_match (see rev 1.1.2.11.2.47) */
966 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
967 &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
972 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
973 ptlrpc_request_set_replen(req);
975 /* check if this is server or client */
976 if (cld->cld_cfg.cfg_sb) {
977 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
978 if (lsi && IS_SERVER(lsi))
981 /* Limit how long we will wait for the enqueue to complete */
982 req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
983 rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
984 NULL, 0, LVB_T_NONE, lockh, 0);
985 /* A failed enqueue should still call the mgc_blocking_ast,
986 where it will be requeued if needed ("grant failed"). */
987 ptlrpc_req_finished(req);
991 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
992 __u32 mode, struct lustre_handle *lockh)
994 ldlm_lock_decref(lockh, mode);
999 static void mgc_notify_active(struct obd_device *unused)
1001 /* wakeup mgc_requeue_thread to requeue mgc lock */
1002 spin_lock(&config_list_lock);
1004 spin_unlock(&config_list_lock);
1007 /* TODO: Help the MGS rebuild nidtbl. -jay */
1010 /* Send target_reg message to MGS */
1011 static int mgc_target_register(struct obd_export *exp,
1012 struct mgs_target_info *mti)
1014 struct ptlrpc_request *req;
1015 struct mgs_target_info *req_mti, *rep_mti;
1018 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1019 &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
1024 req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
1026 ptlrpc_req_finished(req);
1030 memcpy(req_mti, mti, sizeof(*req_mti));
1031 ptlrpc_request_set_replen(req);
1032 CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
1033 /* Limit how long we will wait for the enqueue to complete */
1034 req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
1036 rc = ptlrpc_queue_wait(req);
1038 rep_mti = req_capsule_server_get(&req->rq_pill,
1039 &RMF_MGS_TARGET_INFO);
1040 memcpy(mti, rep_mti, sizeof(*rep_mti));
1041 CDEBUG(D_MGC, "register %s got index = %d\n",
1042 mti->mti_svname, mti->mti_stripe_index);
1044 ptlrpc_req_finished(req);
1049 int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1050 obd_count keylen, void *key, obd_count vallen,
1051 void *val, struct ptlrpc_request_set *set)
1055 /* Turn off initial_recov after we try all backup servers once */
1056 if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1057 struct obd_import *imp = class_exp2cliimp(exp);
1059 if (vallen != sizeof(int))
1061 value = *(int *)val;
1062 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1063 imp->imp_obd->obd_name, value,
1064 imp->imp_deactive, imp->imp_invalid,
1065 imp->imp_replayable, imp->imp_obd->obd_replayable,
1066 ptlrpc_import_state_name(imp->imp_state));
1067 /* Resurrect if we previously died */
1068 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1069 imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1070 ptlrpc_reconnect_import(imp);
1073 /* FIXME move this to mgc_process_config */
1074 if (KEY_IS(KEY_REGISTER_TARGET)) {
1075 struct mgs_target_info *mti;
1076 if (vallen != sizeof(struct mgs_target_info))
1078 mti = (struct mgs_target_info *)val;
1079 CDEBUG(D_MGC, "register_target %s %#x\n",
1080 mti->mti_svname, mti->mti_flags);
1081 rc = mgc_target_register(exp, mti);
1084 if (KEY_IS(KEY_SET_FS)) {
1085 struct super_block *sb = (struct super_block *)val;
1087 if (vallen != sizeof(struct super_block))
1090 rc = mgc_fs_setup(exp->exp_obd, sb);
1092 CERROR("set_fs got %d\n", rc);
1096 if (KEY_IS(KEY_CLEAR_FS)) {
1099 rc = mgc_fs_cleanup(exp->exp_obd);
1101 CERROR("clear_fs got %d\n", rc);
1105 if (KEY_IS(KEY_SET_INFO)) {
1106 struct mgs_send_param *msp;
1108 msp = (struct mgs_send_param *)val;
1109 rc = mgc_set_mgs_param(exp, msp);
1112 if (KEY_IS(KEY_MGSSEC)) {
1113 struct client_obd *cli = &exp->exp_obd->u.cli;
1114 struct sptlrpc_flavor flvr;
1117 * empty string means using current flavor, if which haven't
1118 * been set yet, set it as null.
1120 * if flavor has been set previously, check the asking flavor
1121 * must match the existing one.
1124 if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1130 rc = sptlrpc_parse_flavor(val, &flvr);
1132 CERROR("invalid sptlrpc flavor %s to MGS\n",
1138 * caller already hold a mutex
1140 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1141 cli->cl_flvr_mgc = flvr;
1142 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1143 sizeof(flvr)) != 0) {
1146 sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1148 LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1149 "currently %s is in use\n",
1159 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1160 __u32 keylen, void *key, __u32 *vallen, void *val,
1161 struct lov_stripe_md *unused)
1165 if (KEY_IS(KEY_CONN_DATA)) {
1166 struct obd_import *imp = class_exp2cliimp(exp);
1167 struct obd_connect_data *data = val;
1169 if (*vallen == sizeof(*data)) {
1170 *data = imp->imp_connect_data;
1178 static int mgc_import_event(struct obd_device *obd,
1179 struct obd_import *imp,
1180 enum obd_import_event event)
1184 LASSERT(imp->imp_obd == obd);
1185 CDEBUG(D_MGC, "import event %#x\n", event);
1188 case IMP_EVENT_DISCON:
1189 /* MGC imports should not wait for recovery */
1190 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1191 ptlrpc_pinger_ir_down();
1193 case IMP_EVENT_INACTIVE:
1195 case IMP_EVENT_INVALIDATE: {
1196 struct ldlm_namespace *ns = obd->obd_namespace;
1197 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1200 case IMP_EVENT_ACTIVE:
1201 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1202 /* Clearing obd_no_recov allows us to continue pinging */
1203 obd->obd_no_recov = 0;
1204 mgc_notify_active(obd);
1205 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1206 ptlrpc_pinger_ir_up();
1210 case IMP_EVENT_DEACTIVATE:
1211 case IMP_EVENT_ACTIVATE:
1214 CERROR("Unknown import event %#x\n", event);
1221 CONFIG_READ_NRPAGES_INIT = 1 << (20 - PAGE_CACHE_SHIFT),
1222 CONFIG_READ_NRPAGES = 4
1225 static int mgc_apply_recover_logs(struct obd_device *mgc,
1226 struct config_llog_data *cld,
1228 void *data, int datalen, bool mne_swab)
1230 struct config_llog_instance *cfg = &cld->cld_cfg;
1231 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1232 struct mgs_nidtbl_entry *entry;
1233 struct lustre_cfg *lcfg;
1234 struct lustre_cfg_bufs bufs;
1235 u64 prev_version = 0;
1243 LASSERT(cfg->cfg_instance != NULL);
1244 LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1246 OBD_ALLOC(inst, PAGE_CACHE_SIZE);
1250 if (!IS_SERVER(lsi)) {
1251 pos = snprintf(inst, PAGE_CACHE_SIZE, "%p", cfg->cfg_instance);
1252 if (pos >= PAGE_CACHE_SIZE) {
1253 OBD_FREE(inst, PAGE_CACHE_SIZE);
1257 LASSERT(IS_MDT(lsi));
1258 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1261 OBD_FREE(inst, PAGE_CACHE_SIZE);
1269 bufsz = PAGE_CACHE_SIZE - pos;
1271 while (datalen > 0) {
1272 int entry_len = sizeof(*entry);
1274 struct obd_device *obd;
1281 if (datalen < sizeof(*entry))
1284 entry = (typeof(entry))(data + off);
1287 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1289 if (entry->mne_nid_count == 0) /* at least one nid entry */
1291 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1294 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1295 if (datalen < entry_len) /* must have entry_len at least */
1298 /* Keep this swab for normal mixed endian handling. LU-1644 */
1300 lustre_swab_mgs_nidtbl_entry(entry);
1301 if (entry->mne_length > PAGE_CACHE_SIZE) {
1302 CERROR("MNE too large (%u)\n", entry->mne_length);
1306 if (entry->mne_length < entry_len)
1309 off += entry->mne_length;
1310 datalen -= entry->mne_length;
1314 if (entry->mne_version > max_version) {
1315 CERROR("entry index(%lld) is over max_index(%lld)\n",
1316 entry->mne_version, max_version);
1320 if (prev_version >= entry->mne_version) {
1321 CERROR("index unsorted, prev %lld, now %lld\n",
1322 prev_version, entry->mne_version);
1325 prev_version = entry->mne_version;
1328 * Write a string with format "nid::instance" to
1329 * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1332 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1333 memset(buf, 0, bufsz);
1337 /* lustre-OST0001-osc-<instance #> */
1338 strcpy(obdname, cld->cld_logname);
1339 cname = strrchr(obdname, '-');
1340 if (cname == NULL) {
1341 CERROR("mgc %s: invalid logname %s\n",
1342 mgc->obd_name, obdname);
1346 pos = cname - obdname;
1348 pos += sprintf(obdname + pos, "-%s%04x",
1349 is_ost ? "OST" : "MDT", entry->mne_index);
1351 cname = is_ost ? "osc" : "mdc",
1352 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1353 lustre_cfg_bufs_reset(&bufs, obdname);
1355 /* find the obd by obdname */
1356 obd = class_name2obd(obdname);
1358 CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1359 mgc->obd_name, obdname);
1361 /* this is a safe race, when the ost is starting up...*/
1365 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1368 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1371 down_read(&obd->u.cli.cl_sem);
1372 if (obd->u.cli.cl_import == NULL) {
1373 /* client does not connect to the OST yet */
1374 up_read(&obd->u.cli.cl_sem);
1379 /* TODO: iterate all nids to find one */
1380 /* find uuid by nid */
1381 rc = client_import_find_conn(obd->u.cli.cl_import,
1383 (struct obd_uuid *)uuid);
1384 up_read(&obd->u.cli.cl_sem);
1386 CERROR("mgc: cannot find uuid by nid %s\n",
1387 libcfs_nid2str(entry->u.nids[0]));
1391 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1392 uuid, libcfs_nid2str(entry->u.nids[0]));
1394 pos += strlen(uuid);
1395 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1396 LASSERT(pos < bufsz);
1398 lustre_cfg_bufs_set_string(&bufs, 1, params);
1401 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1403 CERROR("mgc: cannot allocate memory\n");
1407 CDEBUG(D_INFO, "ir apply logs "LPD64"/"LPD64" for %s -> %s\n",
1408 prev_version, max_version, obdname, params);
1410 rc = class_process_config(lcfg);
1411 lustre_cfg_free(lcfg);
1413 CDEBUG(D_INFO, "process config for %s error %d\n",
1416 /* continue, even one with error */
1419 OBD_FREE(inst, PAGE_CACHE_SIZE);
1424 * This function is called if this client was notified for target restarting
1425 * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery logs.
1427 static int mgc_process_recover_log(struct obd_device *obd,
1428 struct config_llog_data *cld)
1430 struct ptlrpc_request *req = NULL;
1431 struct config_llog_instance *cfg = &cld->cld_cfg;
1432 struct mgs_config_body *body;
1433 struct mgs_config_res *res;
1434 struct ptlrpc_bulk_desc *desc;
1435 struct page **pages;
1438 bool mne_swab = false;
1443 /* allocate buffer for bulk transfer.
1444 * if this is the first time for this mgs to read logs,
1445 * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1446 * once; otherwise, it only reads increment of logs, this should be
1447 * small and CONFIG_READ_NRPAGES will be used.
1449 nrpages = CONFIG_READ_NRPAGES;
1450 if (cfg->cfg_last_idx == 0) /* the first time */
1451 nrpages = CONFIG_READ_NRPAGES_INIT;
1453 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1455 GOTO(out, rc = -ENOMEM);
1457 for (i = 0; i < nrpages; i++) {
1458 pages[i] = alloc_page(GFP_IOFS);
1459 if (pages[i] == NULL)
1460 GOTO(out, rc = -ENOMEM);
1464 LASSERT(cld_is_recover(cld));
1465 LASSERT(mutex_is_locked(&cld->cld_lock));
1466 req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1467 &RQF_MGS_CONFIG_READ);
1469 GOTO(out, rc = -ENOMEM);
1471 rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1476 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1477 LASSERT(body != NULL);
1478 LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1479 if (strlcpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name))
1480 >= sizeof(body->mcb_name))
1481 GOTO(out, rc = -E2BIG);
1482 body->mcb_offset = cfg->cfg_last_idx + 1;
1483 body->mcb_type = cld->cld_type;
1484 body->mcb_bits = PAGE_CACHE_SHIFT;
1485 body->mcb_units = nrpages;
1487 /* allocate bulk transfer descriptor */
1488 desc = ptlrpc_prep_bulk_imp(req, nrpages, 1, BULK_PUT_SINK,
1491 GOTO(out, rc = -ENOMEM);
1493 for (i = 0; i < nrpages; i++)
1494 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1496 ptlrpc_request_set_replen(req);
1497 rc = ptlrpc_queue_wait(req);
1501 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1502 if (res->mcr_size < res->mcr_offset)
1503 GOTO(out, rc = -EINVAL);
1505 /* always update the index even though it might have errors with
1506 * handling the recover logs */
1507 cfg->cfg_last_idx = res->mcr_offset;
1508 eof = res->mcr_offset == res->mcr_size;
1510 CDEBUG(D_INFO, "Latest version "LPD64", more %d.\n",
1511 res->mcr_offset, eof == false);
1513 ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1515 GOTO(out, rc = ealen);
1517 if (ealen > nrpages << PAGE_CACHE_SHIFT)
1518 GOTO(out, rc = -EINVAL);
1520 if (ealen == 0) { /* no logs transferred */
1526 mne_swab = !!ptlrpc_rep_need_swab(req);
1527 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 50, 0)
1528 /* This import flag means the server did an extra swab of IR MNE
1529 * records (fixed in LU-1252), reverse it here if needed. LU-1644 */
1530 if (unlikely(req->rq_import->imp_need_mne_swab))
1531 mne_swab = !mne_swab;
1533 #warning "LU-1644: Remove old OBD_CONNECT_MNE_SWAB fixup and imp_need_mne_swab"
1536 for (i = 0; i < nrpages && ealen > 0; i++) {
1540 ptr = kmap(pages[i]);
1541 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1542 min_t(int, ealen, PAGE_CACHE_SIZE),
1546 CWARN("Process recover log %s error %d\n",
1547 cld->cld_logname, rc2);
1551 ealen -= PAGE_CACHE_SIZE;
1556 ptlrpc_req_finished(req);
1558 if (rc == 0 && !eof)
1562 for (i = 0; i < nrpages; i++) {
1563 if (pages[i] == NULL)
1565 __free_page(pages[i]);
1567 OBD_FREE(pages, sizeof(*pages) * nrpages);
1572 static int mgc_llog_local_copy(const struct lu_env *env,
1573 struct obd_device *obd,
1574 struct llog_ctxt *rctxt,
1575 struct llog_ctxt *lctxt, char *logname)
1583 * - copy it to backup using llog_backup()
1584 * - copy remote llog to logname using llog_backup()
1585 * - if failed then move bakup to logname again
1588 OBD_ALLOC(temp_log, strlen(logname) + 1);
1591 sprintf(temp_log, "%sT", logname);
1593 /* make a copy of local llog at first */
1594 rc = llog_backup(env, obd, lctxt, lctxt, logname, temp_log);
1595 if (rc < 0 && rc != -ENOENT)
1597 /* copy remote llog to the local copy */
1598 rc = llog_backup(env, obd, rctxt, lctxt, logname, logname);
1599 if (rc == -ENOENT) {
1600 /* no remote llog, delete local one too */
1601 llog_erase(env, lctxt, NULL, logname);
1602 } else if (rc < 0) {
1603 /* error during backup, get local one back from the copy */
1604 llog_backup(env, obd, lctxt, lctxt, temp_log, logname);
1606 CERROR("%s: failed to copy remote log %s: rc = %d\n",
1607 obd->obd_name, logname, rc);
1609 llog_erase(env, lctxt, NULL, temp_log);
1610 OBD_FREE(temp_log, strlen(logname) + 1);
1614 /* local_only means it cannot get remote llogs */
1615 static int mgc_process_cfg_log(struct obd_device *mgc,
1616 struct config_llog_data *cld, int local_only)
1618 struct llog_ctxt *ctxt, *lctxt = NULL;
1619 struct dt_object *cl_mgc_dir = mgc->u.cli.cl_mgc_configs_dir;
1620 struct lustre_sb_info *lsi = NULL;
1622 bool sptlrpc_started = false;
1626 LASSERT(mutex_is_locked(&cld->cld_lock));
1629 * local copy of sptlrpc log is controlled elsewhere, don't try to
1632 if (cld_is_sptlrpc(cld) && local_only)
1635 if (cld->cld_cfg.cfg_sb)
1636 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1642 rc = lu_env_init(env, LCT_MG_THREAD);
1646 ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1649 lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1651 /* Copy the setup log locally if we can. Don't mess around if we're
1652 * running an MGS though (logs are already local). */
1653 if (lctxt && lsi && IS_SERVER(lsi) && !IS_MGS(lsi) &&
1654 cl_mgc_dir != NULL &&
1655 lu2dt_dev(cl_mgc_dir->do_lu.lo_dev) == lsi->lsi_dt_dev) {
1657 /* Only try to copy log if we have the lock. */
1658 rc = mgc_llog_local_copy(env, mgc, ctxt, lctxt,
1660 if (local_only || rc) {
1661 if (llog_is_empty(env, lctxt, cld->cld_logname)) {
1662 LCONSOLE_ERROR_MSG(0x13a,
1663 "Failed to get MGS log %s and no local copy.\n",
1665 GOTO(out_pop, rc = -ENOTCONN);
1668 "Failed to get MGS log %s, using local copy for now, will try to update later.\n",
1671 /* Now, whether we copied or not, start using the local llog.
1672 * If we failed to copy, we'll start using whatever the old
1674 llog_ctxt_put(ctxt);
1678 if (local_only) /* no local log at client side */
1679 GOTO(out_pop, rc = -EIO);
1682 if (cld_is_sptlrpc(cld)) {
1683 sptlrpc_conf_log_update_begin(cld->cld_logname);
1684 sptlrpc_started = true;
1687 /* logname and instance info should be the same, so use our
1688 * copy of the instance for the update. The cfg_last_idx will
1689 * be updated here. */
1690 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1694 __llog_ctxt_put(env, ctxt);
1696 __llog_ctxt_put(env, lctxt);
1699 * update settings on existing OBDs. doing it inside
1700 * of llog_process_lock so no device is attaching/detaching
1702 * the logname must be <fsname>-sptlrpc
1704 if (sptlrpc_started) {
1705 LASSERT(cld_is_sptlrpc(cld));
1706 sptlrpc_conf_log_update_end(cld->cld_logname);
1707 class_notify_sptlrpc_conf(cld->cld_logname,
1708 strlen(cld->cld_logname) -
1709 strlen("-sptlrpc"));
1718 /** Get a config log from the MGS and process it.
1719 * This func is called for both clients and servers.
1720 * Copy the log locally before parsing it if appropriate (non-MGS server)
1722 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1724 struct lustre_handle lockh = { 0 };
1725 __u64 flags = LDLM_FL_NO_LRU;
1730 /* I don't want multiple processes running process_log at once --
1731 sounds like badness. It actually might be fine, as long as
1732 we're not trying to update from the same log
1733 simultaneously (in which case we should use a per-log sem.) */
1734 mutex_lock(&cld->cld_lock);
1735 if (cld->cld_stopping) {
1736 mutex_unlock(&cld->cld_lock);
1740 OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1742 CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
1743 cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1745 /* Get the cfg lock on the llog */
1746 rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1747 LCK_CR, &flags, NULL, NULL, NULL,
1748 cld, 0, NULL, &lockh);
1750 /* Get the cld, it will be released in mgc_blocking_ast. */
1751 config_log_get(cld);
1752 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1755 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1757 /* mark cld_lostlock so that it will requeue
1758 * after MGC becomes available. */
1759 cld->cld_lostlock = 1;
1760 /* Get extra reference, it will be put in requeue thread */
1761 config_log_get(cld);
1765 if (cld_is_recover(cld)) {
1766 rc = 0; /* this is not a fatal error for recover log */
1768 rc = mgc_process_recover_log(mgc, cld);
1770 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1773 CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1774 mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1776 mutex_unlock(&cld->cld_lock);
1778 /* Now drop the lock so MGS can revoke it */
1780 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1783 CERROR("Can't drop cfg lock: %d\n", rcl);
1790 /** Called from lustre_process_log.
1791 * LCFG_LOG_START gets the config log from the MGS, processes it to start
1792 * any services, and adds it to the list logs to watch (follow).
1794 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1796 struct lustre_cfg *lcfg = buf;
1797 struct config_llog_instance *cfg = NULL;
1801 switch(lcfg->lcfg_command) {
1802 case LCFG_LOV_ADD_OBD: {
1803 /* Overloading this cfg command: register a new target */
1804 struct mgs_target_info *mti;
1806 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1807 sizeof(struct mgs_target_info))
1808 GOTO(out, rc = -EINVAL);
1810 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1811 CDEBUG(D_MGC, "add_target %s %#x\n",
1812 mti->mti_svname, mti->mti_flags);
1813 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1816 case LCFG_LOV_DEL_OBD:
1817 /* Unregister has no meaning at the moment. */
1818 CERROR("lov_del_obd unimplemented\n");
1821 case LCFG_SPTLRPC_CONF: {
1822 rc = sptlrpc_process_config(lcfg);
1825 case LCFG_LOG_START: {
1826 struct config_llog_data *cld;
1827 struct super_block *sb;
1829 logname = lustre_cfg_string(lcfg, 1);
1830 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1831 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1833 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1836 /* We're only called through here on the initial mount */
1837 rc = config_log_add(obd, logname, cfg, sb);
1840 cld = config_log_find(logname, cfg);
1847 /* FIXME only set this for old logs! Right now this forces
1848 us to always skip the "inside markers" check */
1849 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1851 rc = mgc_process_log(obd, cld);
1852 if (rc == 0 && cld->cld_recover != NULL) {
1853 if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
1854 imp_connect_data, IMP_RECOV)) {
1855 rc = mgc_process_log(obd, cld->cld_recover);
1857 struct config_llog_data *cir = cld->cld_recover;
1858 cld->cld_recover = NULL;
1859 config_log_put(cir);
1862 CERROR("Cannot process recover llog %d\n", rc);
1864 config_log_put(cld);
1868 case LCFG_LOG_END: {
1869 logname = lustre_cfg_string(lcfg, 1);
1871 if (lcfg->lcfg_bufcount >= 2)
1872 cfg = (struct config_llog_instance *)lustre_cfg_buf(
1874 rc = config_log_end(logname, cfg);
1878 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1879 GOTO(out, rc = -EINVAL);
1887 struct obd_ops mgc_obd_ops = {
1888 .o_owner = THIS_MODULE,
1889 .o_setup = mgc_setup,
1890 .o_precleanup = mgc_precleanup,
1891 .o_cleanup = mgc_cleanup,
1892 .o_add_conn = client_import_add_conn,
1893 .o_del_conn = client_import_del_conn,
1894 .o_connect = client_connect_import,
1895 .o_disconnect = client_disconnect_export,
1896 //.o_enqueue = mgc_enqueue,
1897 .o_cancel = mgc_cancel,
1898 //.o_iocontrol = mgc_iocontrol,
1899 .o_set_info_async = mgc_set_info_async,
1900 .o_get_info = mgc_get_info,
1901 .o_import_event = mgc_import_event,
1902 .o_process_config = mgc_process_config,
1905 int __init mgc_init(void)
1907 return class_register_type(&mgc_obd_ops, NULL, NULL,
1908 LUSTRE_MGC_NAME, NULL);
1911 static void /*__exit*/ mgc_exit(void)
1913 class_unregister_type(LUSTRE_MGC_NAME);
1916 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1917 MODULE_DESCRIPTION("Lustre Management Client");
1918 MODULE_LICENSE("GPL");
1920 module_init(mgc_init);
1921 module_exit(mgc_exit);