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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2013, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/obdclass/obd_mount_server.c
33 * Server mount routines
35 * Author: Nathan Rutman <nathan@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_CLASS
40 #define D_MOUNT (D_SUPER | D_CONFIG /* | D_WARNING */)
41 #define PRINT_CMD CDEBUG
42 #define PRINT_MASK (D_SUPER | D_CONFIG)
44 #include <linux/types.h>
45 #ifdef HAVE_LINUX_SELINUX_IS_ENABLED
46 #include <linux/selinux.h>
48 #include <linux/statfs.h>
49 #include <linux/version.h>
50 #include <linux/delay.h>
52 #include <llog_swab.h>
53 #include <lustre_disk.h>
54 #include <uapi/linux/lustre/lustre_ioctl.h>
55 #include <lustre_log.h>
56 #include <uapi/linux/lustre/lustre_param.h>
58 #include <obd_class.h>
60 /*********** mount lookup *********/
62 static DEFINE_MUTEX(lustre_mount_info_lock);
63 static LIST_HEAD(server_mount_info_list);
65 static struct lustre_mount_info *server_find_mount(const char *name)
67 struct list_head *tmp;
68 struct lustre_mount_info *lmi;
71 list_for_each(tmp, &server_mount_info_list) {
72 lmi = list_entry(tmp, struct lustre_mount_info,
74 if (strcmp(name, lmi->lmi_name) == 0)
80 /* we must register an obd for a mount before we call the setup routine.
81 *_setup will call lustre_get_mount to get the mnt struct
82 * by obd_name, since we can't pass the pointer to setup.
84 static int server_register_mount(const char *name, struct super_block *sb)
86 struct lustre_mount_info *lmi;
92 OBD_ALLOC(lmi, sizeof(*lmi));
95 OBD_ALLOC(name_cp, strlen(name) + 1);
97 OBD_FREE(lmi, sizeof(*lmi));
100 strcpy(name_cp, name);
102 mutex_lock(&lustre_mount_info_lock);
104 if (server_find_mount(name)) {
105 mutex_unlock(&lustre_mount_info_lock);
106 OBD_FREE(lmi, sizeof(*lmi));
107 OBD_FREE(name_cp, strlen(name) + 1);
108 CERROR("Already registered %s\n", name);
111 lmi->lmi_name = name_cp;
113 list_add(&lmi->lmi_list_chain, &server_mount_info_list);
115 mutex_unlock(&lustre_mount_info_lock);
117 CDEBUG(D_MOUNT, "register mount %p from %s\n", sb, name);
122 /* when an obd no longer needs a mount */
123 static int server_deregister_mount(const char *name)
125 struct lustre_mount_info *lmi;
128 mutex_lock(&lustre_mount_info_lock);
129 lmi = server_find_mount(name);
131 mutex_unlock(&lustre_mount_info_lock);
132 CERROR("%s not registered\n", name);
136 CDEBUG(D_MOUNT, "deregister mount %p from %s\n", lmi->lmi_sb, name);
138 OBD_FREE(lmi->lmi_name, strlen(lmi->lmi_name) + 1);
139 list_del(&lmi->lmi_list_chain);
140 OBD_FREE(lmi, sizeof(*lmi));
141 mutex_unlock(&lustre_mount_info_lock);
143 OBD_RACE(OBD_FAIL_MDS_LLOG_UMOUNT_RACE);
147 /* obd's look up a registered mount using their obdname. This is just
148 * for initial obd setup to find the mount struct. It should not be
149 * called every time you want to mntget.
151 struct lustre_mount_info *server_get_mount(const char *name)
153 struct lustre_mount_info *lmi;
154 struct lustre_sb_info *lsi;
157 mutex_lock(&lustre_mount_info_lock);
158 lmi = server_find_mount(name);
159 mutex_unlock(&lustre_mount_info_lock);
161 CERROR("Can't find mount for %s\n", name);
164 lsi = s2lsi(lmi->lmi_sb);
166 atomic_inc(&lsi->lsi_mounts);
168 CDEBUG(D_MOUNT, "get mount %p from %s, refs=%d\n", lmi->lmi_sb,
169 name, atomic_read(&lsi->lsi_mounts));
173 EXPORT_SYMBOL(server_get_mount);
176 * server_put_mount: to be called from obd_cleanup methods
178 * @dereg_mnt: 0 or 1 depending on whether the mount is to be deregistered or
181 * The caller decides whether server_deregister_mount() needs to be called or
182 * not. Calling of server_deregister_mount() does not depend on refcounting on
183 * lsi because we could have say the mgs and mds on the same node and we
184 * unmount the mds, then the ref on the lsi would still be non-zero but we
185 * would still want to deregister the mds mount.
187 int server_put_mount(const char *name, bool dereg_mnt)
189 struct lustre_mount_info *lmi;
190 struct lustre_sb_info *lsi;
193 mutex_lock(&lustre_mount_info_lock);
194 lmi = server_find_mount(name);
195 mutex_unlock(&lustre_mount_info_lock);
197 CERROR("Can't find mount for %s\n", name);
200 lsi = s2lsi(lmi->lmi_sb);
202 CDEBUG(D_MOUNT, "put mount %p from %s, refs=%d\n",
203 lmi->lmi_sb, name, atomic_read(&lsi->lsi_mounts));
205 if (lustre_put_lsi(lmi->lmi_sb))
206 CDEBUG(D_MOUNT, "Last put of mount %p from %s\n",
210 /* this obd should never need the mount again */
211 server_deregister_mount(name);
215 EXPORT_SYMBOL(server_put_mount);
217 /* Set up a MGS to serve startup logs */
218 static int server_start_mgs(struct super_block *sb)
220 struct lustre_sb_info *lsi = s2lsi(sb);
221 struct lustre_mount_info *lmi;
225 /* It is impossible to have more than 1 MGS per node, since
226 * MGC wouldn't know which to connect to
228 lmi = server_find_mount(LUSTRE_MGS_OBDNAME);
230 lsi = s2lsi(lmi->lmi_sb);
231 LCONSOLE_ERROR_MSG(0x15d,
232 "The MGS service was already started from server\n");
236 CDEBUG(D_CONFIG, "Start MGS service %s\n", LUSTRE_MGS_OBDNAME);
238 rc = server_register_mount(LUSTRE_MGS_OBDNAME, sb);
240 GOTO(report_err, rc);
242 rc = lustre_start_simple(LUSTRE_MGS_OBDNAME, LUSTRE_MGS_NAME,
243 LUSTRE_MGS_OBDNAME, NULL, NULL,
244 lsi->lsi_osd_obdname, NULL);
245 /* server_deregister_mount() is not called previously, for lsi
246 * and other stuff can't be freed cleanly when mgs calls
247 * server_put_mount() in error handling case (see b=17758),
248 * this problem is caused by a bug in mgs_init0, which forgot
249 * calling server_put_mount in error case.
252 server_deregister_mount(LUSTRE_MGS_OBDNAME);
254 LCONSOLE_ERROR_MSG(0x15e,
255 "Failed to start MGS '%s' (%d). Is the 'mgs' module loaded?\n",
256 LUSTRE_MGS_OBDNAME, rc);
261 static int server_stop_mgs(struct super_block *sb)
263 struct obd_device *obd;
265 struct lustre_mount_info *lmi;
268 /* Do not stop MGS if this device is not the running MGT */
269 lmi = server_find_mount(LUSTRE_MGS_OBDNAME);
270 if (lmi && lmi->lmi_sb != sb)
273 CDEBUG(D_MOUNT, "Stop MGS service %s\n", LUSTRE_MGS_OBDNAME);
275 /* There better be only one MGS */
276 obd = class_name2obd(LUSTRE_MGS_OBDNAME);
278 CDEBUG(D_CONFIG, "mgs %s not running\n", LUSTRE_MGS_OBDNAME);
282 /* The MGS should always stop when we say so */
284 rc = class_manual_cleanup(obd);
288 /* Since there's only one mgc per node, we have to change it's fs to get
289 * access to the right disk.
291 static int server_mgc_set_fs(const struct lu_env *env,
292 struct obd_device *mgc, struct super_block *sb)
294 struct lustre_sb_info *lsi = s2lsi(sb);
298 CDEBUG(D_MOUNT, "Set mgc disk for %s\n", lsi->lsi_lmd->lmd_dev);
300 /* cl_mgc_sem in mgc insures we sleep if the mgc_fs is busy */
301 rc = obd_set_info_async(env, mgc->obd_self_export,
302 sizeof(KEY_SET_FS), KEY_SET_FS,
303 sizeof(*sb), sb, NULL);
305 CERROR("can't set_fs %d\n", rc);
310 static int server_mgc_clear_fs(const struct lu_env *env,
311 struct obd_device *mgc)
316 CDEBUG(D_MOUNT, "Unassign mgc disk\n");
317 rc = obd_set_info_async(env, mgc->obd_self_export,
318 sizeof(KEY_CLEAR_FS), KEY_CLEAR_FS,
323 static inline bool is_mdc_device(const char *devname)
327 ptr = strrchr(devname, '-');
328 return ptr && strcmp(ptr, "-mdc") == 0;
331 static inline bool tgt_is_mdt(const char *tgtname, u32 *idx)
335 type = server_name2index(tgtname, idx, NULL);
337 return type == LDD_F_SV_TYPE_MDT;
341 * Convert OST/MDT name(fsname-{MDT,OST}xxxx) to a lwp name with the @idx:yyyy
342 * (fsname-MDTyyyy-lwp-{MDT,OST}xxxx)
344 int tgt_name2lwp_name(const char *tgt_name, char *lwp_name, int len, u32 idx)
351 OBD_ALLOC(fsname, MTI_NAME_MAXLEN);
355 rc = server_name2fsname(tgt_name, fsname, &tgt);
357 CERROR("%s: failed to get fsname from tgt_name: rc = %d\n",
362 if (*tgt != '-' && *tgt != ':') {
363 CERROR("%s: invalid tgt_name name!\n", tgt_name);
364 GOTO(cleanup, rc = -EINVAL);
368 if (strncmp(tgt, "OST", 3) != 0 && strncmp(tgt, "MDT", 3) != 0) {
369 CERROR("%s is not an OST or MDT target!\n", tgt_name);
370 GOTO(cleanup, rc = -EINVAL);
372 snprintf(lwp_name, len, "%s-MDT%04x-%s-%s",
373 fsname, idx, LUSTRE_LWP_NAME, tgt);
375 GOTO(cleanup, rc = 0);
379 OBD_FREE(fsname, MTI_NAME_MAXLEN);
383 EXPORT_SYMBOL(tgt_name2lwp_name);
385 static LIST_HEAD(lwp_register_list);
386 static DEFINE_SPINLOCK(lwp_register_list_lock);
388 static void lustre_put_lwp_item(struct lwp_register_item *lri)
390 if (atomic_dec_and_test(&lri->lri_ref)) {
391 LASSERT(list_empty(&lri->lri_list));
394 class_export_put(*lri->lri_exp);
399 int lustre_register_lwp_item(const char *lwpname, struct obd_export **exp,
400 register_lwp_cb cb_func, void *cb_data)
402 struct obd_device *lwp;
403 struct lwp_register_item *lri;
407 LASSERTF(strlen(lwpname) < MTI_NAME_MAXLEN, "lwpname is too long %s\n",
409 LASSERT(exp && !*exp);
415 lwp = class_name2obd(lwpname);
416 if (lwp && lwp->obd_set_up == 1) {
417 struct obd_uuid *uuid;
424 memcpy(uuid->uuid, lwpname, strlen(lwpname));
425 *exp = obd_uuid_lookup(lwp, uuid);
429 memcpy(lri->lri_name, lwpname, strlen(lwpname));
431 lri->lri_cb_func = cb_func;
432 lri->lri_cb_data = cb_data;
433 INIT_LIST_HEAD(&lri->lri_list);
435 * Initialize the lri_ref at 2, one will be released before
436 * current function returned via lustre_put_lwp_item(), the
437 * other will be released in lustre_deregister_lwp_item().
439 atomic_set(&lri->lri_ref, 2);
441 spin_lock(&lwp_register_list_lock);
442 list_add(&lri->lri_list, &lwp_register_list);
445 spin_unlock(&lwp_register_list_lock);
449 lustre_put_lwp_item(lri);
453 EXPORT_SYMBOL(lustre_register_lwp_item);
455 void lustre_deregister_lwp_item(struct obd_export **exp)
457 struct lwp_register_item *lri;
458 bool removed = false;
461 spin_lock(&lwp_register_list_lock);
462 list_for_each_entry(lri, &lwp_register_list, lri_list) {
463 if (exp == lri->lri_exp) {
464 list_del_init(&lri->lri_list);
469 spin_unlock(&lwp_register_list_lock);
474 /* See lustre_notify_lwp_list(), in some extreme race conditions,
475 * the notify callback could be still on the fly, we need to wait
476 * for the callback done before moving on to free the data used
479 while (atomic_read(&lri->lri_ref) > 1) {
480 CDEBUG(D_MOUNT, "lri reference count %u, repeat: %d\n",
481 atomic_read(&lri->lri_ref), repeat);
483 schedule_timeout_interruptible(cfs_time_seconds(1));
485 lustre_put_lwp_item(lri);
487 EXPORT_SYMBOL(lustre_deregister_lwp_item);
489 struct obd_export *lustre_find_lwp_by_index(const char *dev, u32 idx)
491 struct lustre_mount_info *lmi;
492 struct lustre_sb_info *lsi;
493 struct obd_device *lwp;
494 struct obd_export *exp = NULL;
499 lmi = server_get_mount(dev);
503 lsi = s2lsi(lmi->lmi_sb);
504 rc = server_name2fsname(lsi->lsi_svname, fsname, NULL);
506 CERROR("%s: failed to get fsname: rc = %d\n",
507 lsi->lsi_svname, rc);
511 snprintf(lwp_name, sizeof(lwp_name), "%s-MDT%04x", fsname, idx);
512 mutex_lock(&lsi->lsi_lwp_mutex);
513 list_for_each_entry(lwp, &lsi->lsi_lwp_list, obd_lwp_list) {
514 char *ptr = strstr(lwp->obd_name, lwp_name);
516 if (ptr && lwp->obd_lwp_export) {
517 exp = class_export_get(lwp->obd_lwp_export);
521 mutex_unlock(&lsi->lsi_lwp_mutex);
524 server_put_mount(dev, false);
528 EXPORT_SYMBOL(lustre_find_lwp_by_index);
530 void lustre_notify_lwp_list(struct obd_export *exp)
532 struct lwp_register_item *lri;
536 spin_lock(&lwp_register_list_lock);
537 list_for_each_entry(lri, &lwp_register_list, lri_list) {
538 if (strcmp(exp->exp_obd->obd_name, lri->lri_name))
542 *lri->lri_exp = class_export_get(exp);
543 if (!lri->lri_cb_func)
545 atomic_inc(&lri->lri_ref);
546 spin_unlock(&lwp_register_list_lock);
548 lri->lri_cb_func(lri->lri_cb_data);
549 lustre_put_lwp_item(lri);
551 /* Others may have changed the list after we unlock, we have
552 * to rescan the list from the beginning. Usually, the list
553 * 'lwp_register_list' is very short, and there is 'guard'
554 * lri::lri_exp that will prevent the callback to be done
555 * repeatedly. So rescanning the list has no problem.
559 spin_unlock(&lwp_register_list_lock);
561 EXPORT_SYMBOL(lustre_notify_lwp_list);
563 static int lustre_lwp_connect(struct obd_device *lwp, bool is_mdt)
566 struct lu_context session_ctx;
567 struct obd_export *exp;
568 struct obd_uuid *uuid = NULL;
569 struct obd_connect_data *data = NULL;
573 /* log has been fully processed, let clients connect */
574 rc = lu_env_init(&env, lwp->obd_lu_dev->ld_type->ldt_ctx_tags);
578 lu_context_init(&session_ctx, LCT_SERVER_SESSION);
579 session_ctx.lc_thread = NULL;
580 lu_context_enter(&session_ctx);
581 env.le_ses = &session_ctx;
585 GOTO(out, rc = -ENOMEM);
587 data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_INDEX;
588 data->ocd_version = LUSTRE_VERSION_CODE;
589 data->ocd_connect_flags |= OBD_CONNECT_FID | OBD_CONNECT_AT |
590 OBD_CONNECT_LRU_RESIZE | OBD_CONNECT_FULL20 |
591 OBD_CONNECT_LVB_TYPE | OBD_CONNECT_LIGHTWEIGHT |
592 OBD_CONNECT_LFSCK | OBD_CONNECT_BULK_MBITS;
595 data->ocd_connect_flags |= OBD_CONNECT_MDS_MDS;
599 GOTO(out, rc = -ENOMEM);
601 if (strlen(lwp->obd_name) > sizeof(uuid->uuid)) {
602 CERROR("%s: Too long lwp name %s, max_size is %d\n",
603 lwp->obd_name, lwp->obd_name, (int)sizeof(uuid->uuid));
604 GOTO(out, rc = -EINVAL);
607 /* Use lwp name as the uuid, so we find the export by lwp name later */
608 memcpy(uuid->uuid, lwp->obd_name, strlen(lwp->obd_name));
609 rc = obd_connect(&env, &exp, lwp, uuid, data, NULL);
611 CERROR("%s: connect failed: rc = %d\n", lwp->obd_name, rc);
613 if (unlikely(lwp->obd_lwp_export))
614 class_export_put(lwp->obd_lwp_export);
615 lwp->obd_lwp_export = class_export_get(exp);
627 lu_context_exit(&session_ctx);
628 lu_context_fini(&session_ctx);
634 * lwp is used by slaves (Non-MDT0 targets) to manage the connection to MDT0,
635 * or from the OSTx to MDTy.
637 static int lustre_lwp_setup(struct lustre_cfg *lcfg, struct lustre_sb_info *lsi,
640 struct obd_device *obd;
641 char *lwpname = NULL;
642 char *lwpuuid = NULL;
647 lnet_nid4_to_nid(lcfg->lcfg_nid, &nid);
648 rc = class_add_uuid(lustre_cfg_string(lcfg, 1), &nid);
650 CERROR("%s: Can't add uuid: rc =%d\n", lsi->lsi_svname, rc);
654 OBD_ALLOC(lwpname, MTI_NAME_MAXLEN);
656 GOTO(out, rc = -ENOMEM);
658 rc = tgt_name2lwp_name(lsi->lsi_svname, lwpname, MTI_NAME_MAXLEN, idx);
660 CERROR("%s: failed to generate lwp name: rc = %d\n",
661 lsi->lsi_svname, rc);
665 OBD_ALLOC(lwpuuid, MTI_NAME_MAXLEN);
667 GOTO(out, rc = -ENOMEM);
669 sprintf(lwpuuid, "%s_UUID", lwpname);
670 rc = lustre_start_simple(lwpname, LUSTRE_LWP_NAME,
671 lwpuuid, lustre_cfg_string(lcfg, 1),
674 CERROR("%s: setup up failed: rc %d\n", lwpname, rc);
678 obd = class_name2obd(lwpname);
681 rc = lustre_lwp_connect(obd, strstr(lsi->lsi_svname, "-MDT") != NULL);
683 CERROR("%s: connect failed: rc = %d\n", lwpname, rc);
687 obd->u.cli.cl_max_mds_easize = MAX_MD_SIZE;
688 mutex_lock(&lsi->lsi_lwp_mutex);
689 list_add_tail(&obd->obd_lwp_list, &lsi->lsi_lwp_list);
690 mutex_unlock(&lsi->lsi_lwp_mutex);
693 OBD_FREE(lwpname, MTI_NAME_MAXLEN);
695 OBD_FREE(lwpuuid, MTI_NAME_MAXLEN);
700 /* the caller is responsible for memory free */
701 static struct obd_device *lustre_find_lwp(struct lustre_sb_info *lsi,
702 char **lwpname, u32 idx)
704 struct obd_device *lwp;
709 LASSERT(IS_OST(lsi) || IS_MDT(lsi));
711 OBD_ALLOC(*lwpname, MTI_NAME_MAXLEN);
712 if (*lwpname == NULL)
713 RETURN(ERR_PTR(-ENOMEM));
715 rc = tgt_name2lwp_name(lsi->lsi_svname, *lwpname, MTI_NAME_MAXLEN, idx);
717 CERROR("%s: failed to generate lwp name: rc = %d\n",
718 lsi->lsi_svname, rc);
719 GOTO(out, rc = -EINVAL);
722 lwp = class_name2obd(*lwpname);
726 if (*lwpname != NULL) {
727 OBD_FREE(*lwpname, MTI_NAME_MAXLEN);
733 RETURN(lwp ? lwp : ERR_PTR(-ENOENT));
736 static int lustre_lwp_add_conn(struct lustre_cfg *cfg,
737 struct lustre_sb_info *lsi, u32 idx)
739 struct lustre_cfg_bufs *bufs = NULL;
740 struct lustre_cfg *lcfg = NULL;
741 char *lwpname = NULL;
742 struct obd_device *lwp;
746 lwp = lustre_find_lwp(lsi, &lwpname, idx);
748 CERROR("%s: can't find lwp device.\n", lsi->lsi_svname);
749 GOTO(out, rc = PTR_ERR(lwp));
755 GOTO(out, rc = -ENOMEM);
757 lustre_cfg_bufs_reset(bufs, lwpname);
758 lustre_cfg_bufs_set_string(bufs, 1,
759 lustre_cfg_string(cfg, 1));
761 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
763 GOTO(out_cfg, rc = -ENOMEM);
764 lustre_cfg_init(lcfg, LCFG_ADD_CONN, bufs);
766 rc = class_add_conn(lwp, lcfg);
768 CERROR("%s: can't add conn: rc = %d\n", lwpname, rc);
771 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
772 lcfg->lcfg_buflens));
778 OBD_FREE(lwpname, MTI_NAME_MAXLEN);
783 * Retrieve MDT nids from the client log, then start the lwp device.
784 * there are only two scenarios which would include mdt nid.
786 * marker 5 (flags=0x01, v2.1.54.0) lustre-MDTyyyy 'add mdc' xxx-
787 * add_uuid nid=192.168.122.162@tcp(0x20000c0a87aa2) 0: 1:192.168.122.162@tcp
788 * attach 0:lustre-MDTyyyy-mdc 1:mdc 2:lustre-clilmv_UUID
789 * setup 0:lustre-MDTyyyy-mdc 1:lustre-MDTyyyy_UUID 2:192.168.122.162@tcp
790 * add_uuid nid=192.168.172.1@tcp(0x20000c0a8ac01) 0: 1:192.168.172.1@tcp
791 * add_conn 0:lustre-MDTyyyy-mdc 1:192.168.172.1@tcp
792 * modify_mdc_tgts add 0:lustre-clilmv 1:lustre-MDTyyyy_UUID xxxx
793 * marker 5 (flags=0x02, v2.1.54.0) lustre-MDTyyyy 'add mdc' xxxx-
795 * marker 7 (flags=0x01, v2.1.54.0) lustre-MDTyyyy 'add failnid' xxxx-
796 * add_uuid nid=192.168.122.2@tcp(0x20000c0a87a02) 0: 1:192.168.122.2@tcp
797 * add_conn 0:lustre-MDTyyyy-mdc 1:192.168.122.2@tcp
798 * marker 7 (flags=0x02, v2.1.54.0) lustre-MDTyyyy 'add failnid' xxxx-
800 static int client_lwp_config_process(const struct lu_env *env,
801 struct llog_handle *handle,
802 struct llog_rec_hdr *rec, void *data)
804 struct config_llog_instance *cfg = data;
805 int cfg_len = rec->lrh_len;
806 char *cfg_buf = (char *) (rec + 1);
807 struct lustre_cfg *lcfg = NULL;
808 struct lustre_sb_info *lsi;
809 int rc = 0, swab = 0;
812 if (rec->lrh_type != OBD_CFG_REC) {
813 CERROR("Unknown llog record type %#x encountered\n",
819 GOTO(out, rc = -EINVAL);
820 lsi = s2lsi(cfg->cfg_sb);
822 lcfg = (struct lustre_cfg *)cfg_buf;
823 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
824 lustre_swab_lustre_cfg(lcfg);
828 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
832 switch (lcfg->lcfg_command) {
834 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
836 lustre_swab_cfg_marker(marker, swab,
837 LUSTRE_CFG_BUFLEN(lcfg, 1));
838 if (marker->cm_flags & CM_SKIP ||
839 marker->cm_flags & CM_EXCLUDE)
842 if (!tgt_is_mdt(marker->cm_tgtname, &cfg->cfg_lwp_idx))
845 if (IS_MDT(lsi) && cfg->cfg_lwp_idx != 0)
848 if (!strncmp(marker->cm_comment, "add mdc", 7) ||
849 !strncmp(marker->cm_comment, "add failnid", 11)) {
850 if (marker->cm_flags & CM_START) {
851 cfg->cfg_flags = CFG_F_MARKER;
852 /* This hack is to differentiate the
853 * ADD_UUID is come from "add mdc" record
854 * or from "add failnid" record.
856 if (!strncmp(marker->cm_comment,
858 cfg->cfg_flags |= CFG_F_SKIP;
859 } else if (marker->cm_flags & CM_END) {
865 case LCFG_ADD_UUID: {
866 if (cfg->cfg_flags == CFG_F_MARKER) {
867 rc = lustre_lwp_setup(lcfg, lsi, cfg->cfg_lwp_idx);
868 /* XXX: process only the first nid as
869 * we don't need another instance of lwp
871 cfg->cfg_flags |= CFG_F_SKIP;
872 } else if (cfg->cfg_flags == (CFG_F_MARKER | CFG_F_SKIP)) {
875 lnet_nid4_to_nid(lcfg->lcfg_nid, &nid);
876 rc = class_add_uuid(lustre_cfg_string(lcfg, 1), &nid);
878 CERROR("%s: Fail to add uuid, rc:%d\n",
879 lsi->lsi_svname, rc);
883 case LCFG_ADD_CONN: {
884 char *devname = lustre_cfg_string(lcfg, 0);
888 if (!is_mdc_device(devname))
891 if (!(cfg->cfg_flags & CFG_F_MARKER)) {
892 CDEBUG(D_CONFIG, "Skipping add_conn for %s, rec %d\n",
893 devname, rec->lrh_index);
897 /* add_conn should follow by add_uuid. This
898 * guarantee lwp device was created
900 if (!(cfg->cfg_flags & CFG_F_SKIP)) {
901 CWARN("Error at config for %s rec %d, add_conn should follow by add_uuid\n",
902 devname, rec->lrh_index);
905 ptr = strrchr(devname, '-');
910 if (!tgt_is_mdt(devname, &idx)) {
916 if (IS_MDT(lsi) && idx != 0)
919 rc = lustre_lwp_add_conn(lcfg, lsi, idx);
929 static int lustre_disconnect_lwp(struct super_block *sb)
931 struct lustre_sb_info *lsi = s2lsi(sb);
932 struct obd_device *lwp;
933 char *logname = NULL;
934 struct lustre_cfg_bufs *bufs = NULL;
935 struct config_llog_instance *cfg = NULL;
940 if (likely(lsi->lsi_lwp_started)) {
941 OBD_ALLOC(logname, MTI_NAME_MAXLEN);
945 rc = server_name2fsname(lsi->lsi_svname, logname, NULL);
947 CERROR("%s: failed to get fsname from svname: rc = %d\n",
948 lsi->lsi_svname, rc);
949 GOTO(out, rc = -EINVAL);
952 strcat(logname, "-client");
955 GOTO(out, rc = -ENOMEM);
958 cfg->cfg_instance = ll_get_cfg_instance(sb);
959 rc = lustre_end_log(sb, logname, cfg);
960 if (rc != 0 && rc != -ENOENT)
963 lsi->lsi_lwp_started = 0;
968 GOTO(out, rc = -ENOMEM);
970 mutex_lock(&lsi->lsi_lwp_mutex);
971 list_for_each_entry(lwp, &lsi->lsi_lwp_list, obd_lwp_list) {
972 struct lustre_cfg *lcfg;
974 if (likely(lwp->obd_lwp_export)) {
975 class_export_put(lwp->obd_lwp_export);
976 lwp->obd_lwp_export = NULL;
979 lustre_cfg_bufs_reset(bufs, lwp->obd_name);
980 lustre_cfg_bufs_set_string(bufs, 1, NULL);
981 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount,
987 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
989 /* Disconnect import first. NULL is passed for the '@env',
990 * since it will not be used.
992 rc = lwp->obd_lu_dev->ld_ops->ldo_process_config(NULL,
993 lwp->obd_lu_dev, lcfg);
994 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
995 lcfg->lcfg_buflens));
996 if (rc != 0 && rc != -ETIMEDOUT && rc != -ENODEV &&
997 rc != -ENOTCONN && rc != -ESHUTDOWN) {
998 CERROR("%s: fail to disconnect LWP: rc = %d\n",
1003 mutex_unlock(&lsi->lsi_lwp_mutex);
1013 OBD_FREE(logname, MTI_NAME_MAXLEN);
1015 return rc1 != 0 ? rc1 : rc;
1019 * Stop the lwp for an OST/MDT target.
1021 static int lustre_stop_lwp(struct super_block *sb)
1023 struct lustre_sb_info *lsi = s2lsi(sb);
1024 struct obd_device *lwp;
1029 mutex_lock(&lsi->lsi_lwp_mutex);
1030 while (!list_empty(&lsi->lsi_lwp_list)) {
1031 lwp = list_entry(lsi->lsi_lwp_list.next, struct obd_device,
1033 list_del_init(&lwp->obd_lwp_list);
1035 mutex_unlock(&lsi->lsi_lwp_mutex);
1037 rc = class_manual_cleanup(lwp);
1039 CERROR("%s: fail to stop LWP: rc = %d\n",
1043 mutex_lock(&lsi->lsi_lwp_mutex);
1045 mutex_unlock(&lsi->lsi_lwp_mutex);
1047 RETURN(rc1 != 0 ? rc1 : rc);
1051 * Start the lwp(fsname-MDTyyyy-lwp-{MDT,OST}xxxx) for a MDT/OST or MDT target.
1053 static int lustre_start_lwp(struct super_block *sb)
1055 struct lustre_sb_info *lsi = s2lsi(sb);
1056 struct config_llog_instance *cfg = NULL;
1061 if (unlikely(lsi->lsi_lwp_started))
1064 OBD_ALLOC(logname, MTI_NAME_MAXLEN);
1065 if (logname == NULL)
1068 rc = server_name2fsname(lsi->lsi_svname, logname, NULL);
1070 CERROR("%s: failed to get fsname from svname: rc = %d\n",
1071 lsi->lsi_svname, rc);
1072 GOTO(out, rc = -EINVAL);
1075 strcat(logname, "-client");
1078 GOTO(out, rc = -ENOMEM);
1080 cfg->cfg_callback = client_lwp_config_process;
1081 cfg->cfg_instance = ll_get_cfg_instance(sb);
1082 rc = lustre_process_log(sb, logname, cfg);
1083 /* need to remove config llog from mgc */
1084 lsi->lsi_lwp_started = 1;
1089 OBD_FREE(logname, MTI_NAME_MAXLEN);
1096 static DEFINE_MUTEX(server_start_lock);
1098 /* Stop MDS/OSS if nobody is using them */
1099 static int server_stop_servers(int lsiflags)
1101 struct obd_device *obd = NULL;
1102 struct obd_type *type = NULL;
1107 mutex_lock(&server_start_lock);
1109 /* Either an MDT or an OST or neither */
1110 /* if this was an MDT, and there are no more MDT's, clean up the MDS */
1111 if (lsiflags & LDD_F_SV_TYPE_MDT) {
1112 obd = class_name2obd(LUSTRE_MDS_OBDNAME);
1113 type = class_search_type(LUSTRE_MDT_NAME);
1114 } else if (lsiflags & LDD_F_SV_TYPE_OST) {
1115 /* if this was an OST, and there are no more OST's, clean up the OSS */
1116 obd = class_name2obd(LUSTRE_OSS_OBDNAME);
1117 type = class_search_type(LUSTRE_OST_NAME);
1120 /* server_stop_servers is a pair of server_start_targets
1121 * Here we put type which was taken at server_start_targets.
1122 * If type is NULL then there is a wrong logic around type or
1125 LASSERTF(type, "Server flags %d, obd %s\n", lsiflags,
1126 obd ? obd->obd_name : "NULL");
1128 type_last = (atomic_read(&type->typ_refcnt) == 1);
1130 class_put_type(type);
1131 if (obd && type_last) {
1133 /* obd_fail doesn't mean much on a server obd */
1134 rc = class_manual_cleanup(obd);
1137 /* put reference taken by class_search_type */
1138 kobject_put(&type->typ_kobj);
1140 mutex_unlock(&server_start_lock);
1145 int server_mti_print(const char *title, struct mgs_target_info *mti)
1147 PRINT_CMD(PRINT_MASK, "mti %s\n", title);
1148 PRINT_CMD(PRINT_MASK, "server: %s\n", mti->mti_svname);
1149 PRINT_CMD(PRINT_MASK, "fs: %s\n", mti->mti_fsname);
1150 PRINT_CMD(PRINT_MASK, "uuid: %s\n", mti->mti_uuid);
1151 PRINT_CMD(PRINT_MASK, "ver: %d flags: %#x\n",
1152 mti->mti_config_ver, mti->mti_flags);
1156 /* Generate data for registration */
1157 static int server_lsi2mti(struct lustre_sb_info *lsi,
1158 struct mgs_target_info *mti)
1160 struct lnet_processid id;
1165 if (!IS_SERVER(lsi))
1168 if (strlcpy(mti->mti_svname, lsi->lsi_svname, sizeof(mti->mti_svname))
1169 >= sizeof(mti->mti_svname))
1172 mti->mti_nid_count = 0;
1173 while (LNetGetId(i++, &id) != -ENOENT) {
1174 if (nid_is_lo0(&id.nid))
1177 /* server use --servicenode param, only allow specified
1178 * nids be registered
1180 if (test_bit(LMD_FLG_NO_PRIMNODE, lsi->lsi_lmd->lmd_flags) &&
1181 class_match_nid(lsi->lsi_lmd->lmd_params,
1182 PARAM_FAILNODE, &id.nid) < 1)
1185 if (!class_find_param(lsi->lsi_lmd->lmd_params,
1186 PARAM_NETWORK, NULL)) {
1187 if (LNetGetPeerDiscoveryStatus()) {
1188 CERROR("LNet Dynamic Peer Discovery is enabled"
1189 " on this node. 'network' option used in"
1190 " mkfs.lustre cannot be taken into"
1196 /* match specified network */
1197 if (!class_match_net(lsi->lsi_lmd->lmd_params,
1198 PARAM_NETWORK, LNET_NID_NET(&id.nid)))
1201 mti->mti_nids[mti->mti_nid_count] = lnet_nid_to_nid4(&id.nid);
1202 mti->mti_nid_count++;
1203 if (mti->mti_nid_count >= MTI_NIDS_MAX) {
1204 CWARN("Only using first %d nids for %s\n",
1205 mti->mti_nid_count, mti->mti_svname);
1210 if (mti->mti_nid_count == 0) {
1211 CERROR("Failed to get NID for server %s, please check whether the target is specifed with improper --servicenode or --network options.\n",
1216 mti->mti_lustre_ver = LUSTRE_VERSION_CODE;
1217 mti->mti_config_ver = 0;
1219 rc = server_name2fsname(lsi->lsi_svname, mti->mti_fsname, NULL);
1223 rc = server_name2index(lsi->lsi_svname, &mti->mti_stripe_index, NULL);
1226 /* Orion requires index to be set */
1227 LASSERT(!(rc & LDD_F_NEED_INDEX));
1228 /* keep only LDD flags */
1229 mti->mti_flags = lsi->lsi_flags & LDD_F_MASK;
1230 if (mti->mti_flags & (LDD_F_WRITECONF | LDD_F_VIRGIN))
1231 mti->mti_flags |= LDD_F_UPDATE;
1232 cplen = strlcpy(mti->mti_params, lsi->lsi_lmd->lmd_params,
1233 sizeof(mti->mti_params));
1234 if (cplen >= sizeof(mti->mti_params))
1239 /* Register an old or new target with the MGS. If needed MGS will construct
1240 * startup logs and assign index
1242 static int server_register_target(struct lustre_sb_info *lsi)
1244 struct obd_device *mgc = lsi->lsi_mgc;
1245 struct mgs_target_info *mti = NULL;
1253 if (!IS_SERVER(lsi))
1260 rc = server_lsi2mti(lsi, mti);
1264 CDEBUG(D_MOUNT, "Registration %s, fs=%s, %s, index=%04x, flags=%#x\n",
1265 mti->mti_svname, mti->mti_fsname,
1266 libcfs_nid2str(mti->mti_nids[0]), mti->mti_stripe_index,
1269 /* we cannot ignore registration failure if MGS logs must be updated. */
1270 must_succeed = !!(lsi->lsi_flags &
1271 (LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_WRITECONF |
1273 mti->mti_flags |= LDD_F_OPC_REG;
1276 /* Register the target */
1277 /* FIXME use mgc_process_config instead */
1278 rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1279 sizeof(KEY_REGISTER_TARGET),
1280 KEY_REGISTER_TARGET,
1281 sizeof(*mti), mti, NULL);
1283 if (mti->mti_flags & LDD_F_ERROR) {
1284 LCONSOLE_ERROR_MSG(0x160,
1285 "%s: the MGS refuses to allow this server to start: rc = %d. Please see messages on the MGS.\n",
1286 lsi->lsi_svname, rc);
1287 } else if (must_succeed) {
1288 if ((rc == -ESHUTDOWN || rc == -EIO) && ++tried < 5) {
1289 /* The connection with MGS is not established.
1290 * Try again after 2 seconds. Interruptable.
1292 schedule_timeout_interruptible(cfs_time_seconds(2));
1293 if (!signal_pending(current))
1297 LCONSOLE_ERROR_MSG(0x15f,
1298 "%s: cannot register this server with the MGS: rc = %d. Is the MGS running?\n",
1299 lsi->lsi_svname, rc);
1302 "%s: error registering with the MGS: rc = %d (not fatal)\n",
1303 lsi->lsi_svname, rc);
1304 /* reset the error code for non-fatal error. */
1317 * Notify the MGS that this target is ready.
1318 * Used by IR - if the MGS receives this message, it will notify clients.
1320 static int server_notify_target(struct super_block *sb, struct obd_device *obd)
1322 struct lustre_sb_info *lsi = s2lsi(sb);
1323 struct obd_device *mgc = lsi->lsi_mgc;
1324 struct mgs_target_info *mti = NULL;
1330 if (!(IS_SERVER(lsi)))
1336 rc = server_lsi2mti(lsi, mti);
1340 mti->mti_instance = obd2obt(obd)->obt_instance;
1341 mti->mti_flags |= LDD_F_OPC_READY;
1343 /* FIXME use mgc_process_config instead */
1344 rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1345 sizeof(KEY_REGISTER_TARGET),
1346 KEY_REGISTER_TARGET,
1347 sizeof(*mti), mti, NULL);
1349 /* Imperative recovery: if the mgs informs us to use IR? */
1350 if (!rc && !(mti->mti_flags & LDD_F_ERROR) &&
1351 (mti->mti_flags & LDD_F_IR_CAPABLE))
1352 lsi->lsi_flags |= LDD_F_IR_CAPABLE;
1361 /* Start server targets: MDTs and OSTs */
1362 static int server_start_targets(struct super_block *sb)
1364 struct obd_device *obd;
1365 struct lustre_sb_info *lsi = s2lsi(sb);
1366 struct config_llog_instance cfg;
1367 struct lu_env mgc_env;
1368 struct lu_device *dev;
1369 char *name_service, *obd_name_service = NULL;
1370 struct obd_type *type = NULL;
1374 CDEBUG(D_MOUNT, "starting target %s\n", lsi->lsi_svname);
1376 LASSERTF(IS_MDT(lsi) || IS_OST(lsi), "designed for MDT or OST only\n");
1379 obd_name_service = LUSTRE_MDS_OBDNAME;
1380 name_service = LUSTRE_MDS_NAME;
1382 obd_name_service = LUSTRE_OSS_OBDNAME;
1383 name_service = LUSTRE_OSS_NAME;
1386 /* make sure MDS/OSS is started */
1387 mutex_lock(&server_start_lock);
1388 obd = class_name2obd(obd_name_service);
1390 rc = lustre_start_simple(obd_name_service, name_service,
1392 LUSTRE_MDS_OBDNAME"_uuid" :
1393 LUSTRE_OSS_OBDNAME"_uuid"),
1394 NULL, NULL, NULL, NULL);
1396 mutex_unlock(&server_start_lock);
1397 CERROR("failed to start %s: %d\n",
1398 obd_name_service, rc);
1402 /* hold a type reference and put it at server_stop_servers */
1403 type = class_get_type(IS_MDT(lsi) ?
1404 LUSTRE_MDT_NAME : LUSTRE_OST_NAME);
1406 mutex_unlock(&server_start_lock);
1407 GOTO(out_stop_service, rc = -ENODEV);
1409 lsi->lsi_server_started = 1;
1410 mutex_unlock(&server_start_lock);
1411 if (OBD_FAIL_PRECHECK(OBD_FAIL_OBD_STOP_MDS_RACE) &&
1413 OBD_RACE(OBD_FAIL_OBD_STOP_MDS_RACE);
1414 msleep(2 * MSEC_PER_SEC);
1417 rc = lu_env_init(&mgc_env, LCT_MG_THREAD);
1419 GOTO(out_stop_service, rc);
1421 /* Set the mgc fs to our server disk. This allows the MGC to
1422 * read and write configs locally, in case it can't talk to the MGS.
1424 rc = server_mgc_set_fs(&mgc_env, lsi->lsi_mgc, sb);
1428 /* Register with MGS */
1429 rc = server_register_target(lsi);
1433 /* Let the target look up the mount using the target's name
1434 * (we can't pass the sb or mnt through class_process_config.)
1436 rc = server_register_mount(lsi->lsi_svname, sb);
1440 /* Start targets using the llog named for the target */
1441 memset(&cfg, 0, sizeof(cfg));
1442 cfg.cfg_callback = class_config_llog_handler;
1443 cfg.cfg_sub_clds = CONFIG_SUB_SERVER;
1444 rc = lustre_process_log(sb, lsi->lsi_svname, &cfg);
1446 CERROR("failed to start server %s: %d\n",
1447 lsi->lsi_svname, rc);
1448 /* Do NOT call server_deregister_mount() here. This makes it
1449 * impossible to find mount later in cleanup time and leaves
1450 * @lsi and othder stuff leaked. -umka
1455 obd = class_name2obd(lsi->lsi_svname);
1457 CERROR("no server named %s was started\n", lsi->lsi_svname);
1458 GOTO(out_mgc, rc = -ENXIO);
1461 if (IS_OST(lsi) || IS_MDT(lsi)) {
1462 rc = lustre_start_lwp(sb);
1464 CERROR("%s: failed to start LWP: %d\n",
1465 lsi->lsi_svname, rc);
1470 server_notify_target(sb, obd);
1472 /* calculate recovery timeout, do it after lustre_process_log */
1473 server_calc_timeout(lsi, obd);
1475 /* log has been fully processed, let clients connect */
1476 dev = obd->obd_lu_dev;
1477 if (dev && dev->ld_ops->ldo_prepare) {
1480 rc = lu_env_init(&env, dev->ld_type->ldt_ctx_tags);
1482 struct lu_context session_ctx;
1484 lu_context_init(&session_ctx, LCT_SERVER_SESSION);
1485 session_ctx.lc_thread = NULL;
1486 lu_context_enter(&session_ctx);
1487 env.le_ses = &session_ctx;
1489 rc = dev->ld_ops->ldo_prepare(&env, NULL, dev);
1492 lu_context_exit(&session_ctx);
1493 lu_context_fini(&session_ctx);
1497 /* abort recovery only on the complete stack:
1498 * many devices can be involved
1500 if ((test_bit(LMD_FLG_ABORT_RECOV, lsi->lsi_lmd->lmd_flags) ||
1501 (test_bit(LMD_FLG_ABORT_RECOV_MDT, lsi->lsi_lmd->lmd_flags))) &&
1502 (OBP(obd, iocontrol))) {
1503 struct obd_ioctl_data karg;
1505 if (test_bit(LMD_FLG_ABORT_RECOV, lsi->lsi_lmd->lmd_flags))
1506 karg.ioc_type = OBD_FLG_ABORT_RECOV_OST;
1508 karg.ioc_type = OBD_FLG_ABORT_RECOV_MDT;
1510 obd_iocontrol(OBD_IOC_ABORT_RECOVERY, obd->obd_self_export, 0,
1515 /* Release the mgc fs for others to use */
1516 server_mgc_clear_fs(&mgc_env, lsi->lsi_mgc);
1518 lu_env_fini(&mgc_env);
1520 /* in case of error upper function call
1521 * server_put_super->server_stop_servers()
1527 static int lsi_prepare(struct lustre_sb_info *lsi)
1529 const char *osd_type;
1536 LASSERT(lsi->lsi_lmd);
1538 /* The server name is given as a mount line option */
1539 if (!lsi->lsi_lmd->lmd_profile) {
1540 LCONSOLE_ERROR("Can't determine server name\n");
1544 /* Determine osd type */
1545 if (!lsi->lsi_lmd->lmd_osd_type) {
1546 osd_type = LUSTRE_OSD_LDISKFS_NAME;
1549 osd_type = lsi->lsi_lmd->lmd_osd_type;
1550 fstype = lsi->lsi_lmd->lmd_osd_type;
1553 if (strlen(lsi->lsi_lmd->lmd_profile) >= sizeof(lsi->lsi_svname) ||
1554 strlen(osd_type) >= sizeof(lsi->lsi_osd_type) ||
1555 strlen(fstype) >= sizeof(lsi->lsi_fstype))
1556 RETURN(-ENAMETOOLONG);
1558 strlcpy(lsi->lsi_svname, lsi->lsi_lmd->lmd_profile,
1559 sizeof(lsi->lsi_svname));
1560 strlcpy(lsi->lsi_osd_type, osd_type, sizeof(lsi->lsi_osd_type));
1561 /* XXX: a temp. solution for components using ldiskfs
1562 * to be removed in one of the subsequent patches
1564 strlcpy(lsi->lsi_fstype, fstype, sizeof(lsi->lsi_fstype));
1566 /* Determine server type */
1567 rc = server_name2index(lsi->lsi_svname, &index, NULL);
1569 if (test_bit(LMD_FLG_MGS, lsi->lsi_lmd->lmd_flags)) {
1570 /* Assume we're a bare MGS */
1572 set_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags);
1574 LCONSOLE_ERROR("Can't determine server type of '%s'\n",
1579 lsi->lsi_flags |= rc;
1581 /* Add mount line flags that used to be in ldd:
1582 * writeconf, mgs, anything else?
1584 lsi->lsi_flags |= test_bit(LMD_FLG_WRITECONF, lsi->lsi_lmd->lmd_flags) ?
1585 LDD_F_WRITECONF : 0;
1586 lsi->lsi_flags |= test_bit(LMD_FLG_NO_LOCAL_LOGS, lsi->lsi_lmd->lmd_flags) ?
1587 LDD_F_NO_LOCAL_LOGS : 0;
1588 lsi->lsi_flags |= test_bit(LMD_FLG_VIRGIN, lsi->lsi_lmd->lmd_flags) ?
1590 lsi->lsi_flags |= test_bit(LMD_FLG_UPDATE, lsi->lsi_lmd->lmd_flags) ?
1592 lsi->lsi_flags |= test_bit(LMD_FLG_MGS, lsi->lsi_lmd->lmd_flags) ?
1593 LDD_F_SV_TYPE_MGS : 0;
1594 lsi->lsi_flags |= test_bit(LMD_FLG_NO_PRIMNODE, lsi->lsi_lmd->lmd_flags) ?
1595 LDD_F_NO_PRIMNODE : 0;
1600 /*************** server mount ******************/
1602 /** Start the shutdown of servers at umount.
1604 static void server_put_super(struct super_block *sb)
1606 struct lustre_sb_info *lsi = s2lsi(sb);
1607 struct obd_device *obd;
1608 char *tmpname, *extraname = NULL;
1610 int lsiflags = lsi->lsi_flags;
1611 bool stop_servers = lsi->lsi_server_started;
1614 LASSERT(IS_SERVER(lsi));
1616 tmpname_sz = strlen(lsi->lsi_svname) + 1;
1617 OBD_ALLOC(tmpname, tmpname_sz);
1618 memcpy(tmpname, lsi->lsi_svname, tmpname_sz);
1619 CDEBUG(D_MOUNT, "server put_super %s\n", tmpname);
1620 if (IS_MDT(lsi) && test_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags))
1621 snprintf(tmpname, tmpname_sz, "MGS");
1623 /* disconnect the lwp first to drain off the inflight request */
1624 if (IS_OST(lsi) || IS_MDT(lsi)) {
1627 rc = lustre_disconnect_lwp(sb);
1628 if (rc != 0 && rc != -ETIMEDOUT && rc != -ENODEV &&
1629 rc != -ENOTCONN && rc != -ESHUTDOWN)
1630 CWARN("%s: failed to disconnect lwp: rc= %d\n",
1634 /* Stop the target */
1635 if (!test_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags) &&
1636 (IS_MDT(lsi) || IS_OST(lsi))) {
1637 struct lustre_profile *lprof = NULL;
1639 /* tell the mgc to drop the config log */
1640 lustre_end_log(sb, lsi->lsi_svname, NULL);
1642 /* COMPAT_146 - profile may get deleted in mgc_cleanup.
1643 * If there are any setup/cleanup errors, save the lov
1644 * name for safety cleanup later.
1646 lprof = class_get_profile(lsi->lsi_svname);
1649 OBD_ALLOC(extraname, strlen(lprof->lp_dt) + 1);
1650 strncpy(extraname, lprof->lp_dt,
1651 strlen(lprof->lp_dt) + 1);
1653 class_put_profile(lprof);
1656 obd = class_name2obd(lsi->lsi_svname);
1658 CDEBUG(D_MOUNT, "stopping %s\n", obd->obd_name);
1659 if (lsiflags & LSI_UMOUNT_FAILOVER)
1661 /* We can't seem to give an error return code
1662 * to .put_super, so we better make sure we clean up!
1665 class_manual_cleanup(obd);
1666 if (OBD_FAIL_PRECHECK(OBD_FAIL_OBD_STOP_MDS_RACE)) {
1669 server_name2index(lsi->lsi_svname, &idx, NULL);
1670 /* sleeping for MDT0001 */
1672 OBD_RACE(OBD_FAIL_OBD_STOP_MDS_RACE);
1675 CERROR("no obd %s\n", lsi->lsi_svname);
1676 server_deregister_mount(lsi->lsi_svname);
1680 /* If they wanted the mgs to stop separately from the mdt, they
1681 * should have put it on a different device.
1683 lustre_stop_mgc(sb);
1685 /* if MDS start with --nomgs, don't stop MGS then */
1686 if (!test_bit(LMD_FLG_NOMGS, lsi->lsi_lmd->lmd_flags))
1687 server_stop_mgs(sb);
1690 if (IS_OST(lsi) || IS_MDT(lsi)) {
1691 if (lustre_stop_lwp(sb) < 0)
1692 CERROR("%s: failed to stop lwp!\n", tmpname);
1695 /* Drop a ref to the mounted disk */
1698 /* wait till all in-progress cleanups are done
1699 * specifically we're interested in ofd cleanup
1702 obd_zombie_barrier();
1704 /* Stop the servers (MDS, OSS) if no longer needed. We must wait
1705 * until the target is really gone so that our type refcount check
1709 server_stop_servers(lsiflags);
1711 /* In case of startup or cleanup err, stop related obds */
1713 obd = class_name2obd(extraname);
1715 CWARN("Cleaning orphaned obd %s\n", extraname);
1717 class_manual_cleanup(obd);
1719 OBD_FREE(extraname, strlen(extraname) + 1);
1722 LCONSOLE(D_WARNING, "server umount %s complete\n", tmpname);
1723 OBD_FREE(tmpname, tmpname_sz);
1727 /* Called only for 'umount -f' */
1728 static void server_umount_begin(struct super_block *sb)
1730 struct lustre_sb_info *lsi = s2lsi(sb);
1733 CDEBUG(D_MOUNT, "umount -f\n");
1734 /* umount = failover
1736 * no third way to do non-force, non-failover
1738 lsi->lsi_flags &= ~LSI_UMOUNT_FAILOVER;
1742 static int server_statfs(struct dentry *dentry, struct kstatfs *buf)
1744 struct super_block *sb = dentry->d_sb;
1745 struct lustre_sb_info *lsi = s2lsi(sb);
1746 struct obd_statfs statfs;
1750 if (lsi->lsi_dt_dev) {
1751 rc = dt_statfs(NULL, lsi->lsi_dt_dev, &statfs);
1753 statfs_unpack(buf, &statfs);
1754 buf->f_type = sb->s_magic;
1760 buf->f_type = sb->s_magic;
1761 buf->f_bsize = sb->s_blocksize;
1767 buf->f_namelen = NAME_MAX;
1771 int server_show_options(struct seq_file *seq, struct dentry *dentry)
1773 struct lustre_sb_info *lsi;
1774 struct lustre_mount_data *lmd;
1775 struct obd_statfs osfs;
1776 struct super_block *sb;
1779 LASSERT(seq && dentry);
1780 lsi = s2lsi(dentry->d_sb);
1784 if (lsi->lsi_dt_dev) {
1785 rc = dt_statfs(NULL, lsi->lsi_dt_dev, &osfs);
1787 /* Check FS State for OS_STATFS_READONLY
1788 * (Read only) flag. If it is not set then
1789 * toggle back the s_flag's SB_RDONLY bit.
1790 * The SB_RDONLY bit is always set for OST/MDT
1791 * during server prep (server_fill_super_common())
1794 * Also, if server is mounted with "rdonly_dev"
1795 * (LMD_FLG_DEV_RDONLY) then force flag to be 'ro'
1797 if (!test_bit(LMD_FLG_DEV_RDONLY, lmd->lmd_flags) &&
1798 !(osfs.os_state & OS_STATFS_READONLY))
1799 sb->s_flags &= ~SB_RDONLY;
1803 seq_printf(seq, ",svname=%s", lmd->lmd_profile);
1805 if (test_bit(LMD_FLG_ABORT_RECOV, lmd->lmd_flags))
1806 seq_puts(seq, ",abort_recov");
1808 if (test_bit(LMD_FLG_NOIR, lmd->lmd_flags))
1809 seq_puts(seq, ",noir");
1811 if (test_bit(LMD_FLG_NOSVC, lmd->lmd_flags))
1812 seq_puts(seq, ",nosvc");
1814 if (test_bit(LMD_FLG_NOMGS, lmd->lmd_flags))
1815 seq_puts(seq, ",nomgs");
1817 if (test_bit(LMD_FLG_NOSCRUB, lmd->lmd_flags))
1818 seq_puts(seq, ",noscrub");
1820 if (test_bit(LMD_FLG_SKIP_LFSCK, lmd->lmd_flags))
1821 seq_puts(seq, ",skip_lfsck");
1823 if (test_bit(LMD_FLG_DEV_RDONLY, lmd->lmd_flags))
1824 seq_puts(seq, ",rdonly_dev");
1826 if (test_bit(LMD_FLG_MGS, lmd->lmd_flags))
1827 seq_puts(seq, ",mgs");
1830 seq_printf(seq, ",mgsnode=%s", lmd->lmd_mgs);
1832 if (lmd->lmd_osd_type)
1833 seq_printf(seq, ",osd=%s", lmd->lmd_osd_type);
1835 if (lmd->lmd_opts) {
1837 seq_puts(seq, lmd->lmd_opts);
1843 /** The operations we support directly on the superblock:
1844 * mount, umount, and df.
1846 static const struct super_operations server_ops = {
1847 .put_super = server_put_super,
1848 .umount_begin = server_umount_begin, /* umount -f */
1849 .statfs = server_statfs,
1850 .show_options = server_show_options,
1853 #if defined(HAVE_USER_NAMESPACE_ARG)
1854 # define USERNS_ARG mnt_userns,
1857 # ifdef HAVE_INODEOPS_ENHANCED_GETATTR
1858 # define server_getattr(ns, path, st, rq, fl) server_getattr(path, st, rq, fl)
1863 * inode operations for Lustre server mountpoints
1865 #if defined(HAVE_USER_NAMESPACE_ARG) || defined(HAVE_INODEOPS_ENHANCED_GETATTR)
1866 static int server_getattr(struct user_namespace *mnt_userns,
1867 const struct path *path, struct kstat *stat,
1868 u32 request_mask, unsigned int flags)
1870 struct inode *inode = d_inode(path->dentry);
1872 static int server_getattr(struct vfsmount *mnt, struct dentry *de,
1875 struct inode *inode = de->d_inode;
1877 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
1878 struct vfsmount *root_mnt;
1879 struct inode *root_inode;
1881 root_mnt = dt_mnt_get(lsi->lsi_dt_dev);
1882 if (IS_ERR(root_mnt))
1883 root_inode = igrab(inode);
1885 root_inode = igrab(root_mnt->mnt_sb->s_root->d_inode);
1889 CDEBUG(D_SUPER, "%s: root_inode from %s ino=%lu, dev=%x\n",
1890 lsi->lsi_svname, root_inode == inode ? "lsi" : "vfsmnt",
1891 root_inode->i_ino, root_inode->i_rdev);
1892 generic_fillattr(USERNS_ARG root_inode, stat);
1898 #ifdef HAVE_IOP_XATTR
1899 static ssize_t server_getxattr(struct dentry *dentry, const char *name,
1900 void *buffer, size_t size)
1902 if (!selinux_is_enabled())
1907 static int server_setxattr(struct dentry *dentry, const char *name,
1908 const void *value, size_t size, int flags)
1914 static ssize_t server_listxattr(struct dentry *d_entry, char *name,
1920 static bool is_cmd_supported(unsigned int cmd)
1922 CDEBUG(D_SUPER, "ioctl cmd=%x\n", cmd);
1927 case LL_IOC_RESIZE_FS:
1936 static long server_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1938 struct lustre_sb_info *lsi = s2lsi(file_inode(filp)->i_sb);
1939 struct vfsmount *root_mnt;
1940 struct file *root_filp;
1941 struct inode *root_inode;
1944 if (!is_cmd_supported(cmd))
1947 root_mnt = dt_mnt_get(lsi->lsi_dt_dev);
1948 if (IS_ERR(root_mnt))
1951 root_inode = igrab(root_mnt->mnt_root->d_inode);
1955 root_filp = alloc_file_pseudo(root_inode, root_mnt, "/",
1956 O_RDWR | O_NOATIME, root_inode->i_fop);
1957 if (root_inode->i_fop && root_inode->i_fop->unlocked_ioctl)
1958 err = root_inode->i_fop->unlocked_ioctl(root_filp, cmd, arg);
1964 static const struct inode_operations server_inode_operations = {
1965 .getattr = server_getattr,
1966 #ifdef HAVE_IOP_XATTR
1967 .setxattr = server_setxattr,
1968 .getxattr = server_getxattr,
1970 .listxattr = server_listxattr,
1973 static const struct file_operations server_file_operations = {
1974 .unlocked_ioctl = server_ioctl,
1977 #define log2(n) ffz(~(n))
1978 #define LUSTRE_SUPER_MAGIC 0x0BD00BD1
1980 static int server_fill_super_common(struct super_block *sb)
1982 struct inode *root = NULL;
1985 CDEBUG(D_MOUNT, "Server sb, dev=%d\n", (int)sb->s_dev);
1987 sb->s_blocksize = 4096;
1988 sb->s_blocksize_bits = log2(sb->s_blocksize);
1989 sb->s_magic = LUSTRE_SUPER_MAGIC;
1990 sb->s_maxbytes = 0; /* we don't allow file IO on server mountpoints */
1991 sb->s_flags |= SB_RDONLY;
1992 sb->s_op = &server_ops;
1994 root = new_inode(sb);
1996 CERROR("Can't make root inode\n");
2000 /* returns -EIO for every operation */
2001 /* make_bad_inode(root); -- badness - can't umount */
2002 /* apparently we need to be a directory for the mount to finish */
2003 root->i_mode = S_IFDIR;
2004 root->i_op = &server_inode_operations;
2005 root->i_fop = &server_file_operations;
2006 sb->s_root = d_make_root(root);
2008 CERROR("%s: can't make root dentry\n", sb->s_id);
2015 static int osd_start(struct lustre_sb_info *lsi, unsigned long mflags)
2017 struct lustre_mount_data *lmd = lsi->lsi_lmd;
2018 struct obd_device *obd;
2019 struct dt_device_param p;
2020 char flagstr[20 + 1 + 10 + 1];
2026 "Attempting to start %s, type=%s, lsifl=%x, mountfl=%lx\n",
2027 lsi->lsi_svname, lsi->lsi_osd_type, lsi->lsi_flags, mflags);
2029 sprintf(lsi->lsi_osd_obdname, "%s-osd", lsi->lsi_svname);
2030 strcpy(lsi->lsi_osd_uuid, lsi->lsi_osd_obdname);
2031 strcat(lsi->lsi_osd_uuid, "_UUID");
2032 bitmap_to_arr32(&lmd_flags, lmd->lmd_flags, LMD_FLG_NUM_FLAGS);
2033 snprintf(flagstr, sizeof(flagstr), "%lu:%u", mflags, lmd_flags);
2035 obd = class_name2obd(lsi->lsi_osd_obdname);
2037 rc = lustre_start_simple(lsi->lsi_osd_obdname,
2039 lsi->lsi_osd_uuid, lmd->lmd_dev,
2040 flagstr, lsi->lsi_lmd->lmd_opts,
2044 obd = class_name2obd(lsi->lsi_osd_obdname);
2047 CDEBUG(D_MOUNT, "%s already started\n", lsi->lsi_osd_obdname);
2048 /* but continue setup to allow special case of MDT and internal
2049 * MGT being started separately.
2051 if (!((IS_MGS(lsi) &&
2052 test_bit(LMD_FLG_NOMGS, lsi->lsi_lmd->lmd_flags)) ||
2054 test_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags))))
2058 rc = obd_connect(NULL, &lsi->lsi_osd_exp,
2059 obd, &obd->obd_uuid, NULL, NULL);
2063 class_manual_cleanup(obd);
2064 lsi->lsi_dt_dev = NULL;
2068 LASSERT(obd->obd_lu_dev);
2069 lu_device_get(obd->obd_lu_dev);
2070 lsi->lsi_dt_dev = lu2dt_dev(obd->obd_lu_dev);
2071 LASSERT(lsi->lsi_dt_dev);
2073 /* set disk context for llog usage */
2074 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
2075 obd->obd_lvfs_ctxt.dt = lsi->lsi_dt_dev;
2077 dt_conf_get(NULL, lsi->lsi_dt_dev, &p);
2082 /** Fill in the superblock info for a Lustre server.
2083 * Mount the device with the correct options.
2084 * Read the on-disk config file.
2085 * Start the services.
2087 int server_fill_super(struct super_block *sb)
2089 struct lustre_sb_info *lsi = s2lsi(sb);
2093 /* to simulate target mount race */
2094 OBD_RACE(OBD_FAIL_TGT_MOUNT_RACE);
2096 rc = lsi_prepare(lsi);
2102 /* Start low level OSD */
2103 rc = osd_start(lsi, sb->s_flags);
2105 CERROR("Unable to start osd on %s: %d\n",
2106 lsi->lsi_lmd->lmd_dev, rc);
2111 CDEBUG(D_MOUNT, "Found service %s on device %s\n",
2112 lsi->lsi_svname, lsi->lsi_lmd->lmd_dev);
2114 if (class_name2obd(lsi->lsi_svname)) {
2115 LCONSOLE_ERROR_MSG(0x161,
2116 "The target named %s is already running. Double-mount may have compromised the disk journal.\n",
2122 /* Start MGS before MGC */
2123 if (IS_MGS(lsi) && !test_bit(LMD_FLG_NOMGS, lsi->lsi_lmd->lmd_flags)) {
2124 rc = server_start_mgs(sb);
2129 /* Start MGC before servers */
2130 rc = lustre_start_mgc(sb);
2134 /* Set up all obd devices for service */
2135 if (!test_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags) &&
2136 (IS_OST(lsi) || IS_MDT(lsi))) {
2137 rc = server_start_targets(sb);
2139 CERROR("Unable to start targets: %d\n", rc);
2142 /* FIXME overmount client here, or can we just start a
2143 * client log and client_fill_super on this sb? We
2144 * need to make sure server_put_super gets called too
2145 * - ll_put_super calls lustre_common_put_super; check
2146 * there for LSI_SERVER flag, call s_p_s if so.
2148 * Probably should start client from new thread so we
2149 * can return. Client will not finish until all
2150 * servers are connected. Note - MGS-only server does
2151 * NOT get a client, since there is no lustre fs
2152 * associated - the MGS is for all lustre fs's
2156 rc = server_fill_super_common(sb);
2162 /* We jump here in case of failure while starting targets or MGS.
2163 * In this case we can't just put @mnt and have to do real cleanup
2164 * with stoping targets, etc.
2166 server_put_super(sb);
2169 EXPORT_SYMBOL(server_fill_super);
2172 * Calculate timeout value for a target.
2174 void server_calc_timeout(struct lustre_sb_info *lsi, struct obd_device *obd)
2176 struct lustre_mount_data *lmd;
2180 bool has_ir = !!(lsi->lsi_flags & LDD_F_IR_CAPABLE);
2181 int min = OBD_RECOVERY_TIME_MIN;
2183 LASSERT(IS_SERVER(lsi));
2187 soft = lmd->lmd_recovery_time_soft;
2188 hard = lmd->lmd_recovery_time_hard;
2189 has_ir = has_ir && !test_bit(LMD_FLG_NOIR, lmd->lmd_flags);
2190 obd->obd_no_ir = !has_ir;
2194 soft = OBD_RECOVERY_TIME_SOFT;
2196 hard = OBD_RECOVERY_TIME_HARD;
2198 /* target may have ir_factor configured. */
2199 factor = OBD_IR_FACTOR_DEFAULT;
2200 if (obd->obd_recovery_ir_factor)
2201 factor = obd->obd_recovery_ir_factor;
2204 int new_soft = soft;
2206 /* adjust timeout value by imperative recovery */
2207 new_soft = (soft * factor) / OBD_IR_FACTOR_MAX;
2208 /* make sure the timeout is not too short */
2209 new_soft = max(min, new_soft);
2211 LCONSOLE_INFO("%s: Imperative Recovery enabled, recovery window shrunk from %d-%d down to %d-%d\n",
2212 obd->obd_name, soft, hard, new_soft, hard);
2216 LCONSOLE_INFO("%s: Imperative Recovery not enabled, recovery window %d-%d\n",
2217 obd->obd_name, soft, hard);
2221 obd->obd_recovery_timeout = max_t(time64_t, obd->obd_recovery_timeout,
2223 obd->obd_recovery_time_hard = hard;
2224 obd->obd_recovery_ir_factor = factor;
2228 * This is the entry point for the mount call into Lustre.
2229 * This is called when a server target is mounted,
2230 * and this is where we start setting things up.
2231 * @param data Mount options (e.g. -o flock,abort_recov)
2233 static int lustre_tgt_fill_super(struct super_block *sb, void *lmd2_data,
2236 struct lustre_mount_data *lmd;
2237 struct lustre_sb_info *lsi;
2241 CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
2243 lsi = lustre_init_lsi(sb);
2249 * Disable lockdep during mount, because mount locking patterns are
2255 * LU-639: the OBD cleanup of last mount may not finish yet, wait here.
2257 obd_zombie_barrier();
2259 /* Figure out the lmd from the mount options */
2260 if (lmd_parse(lmd2_data, lmd)) {
2262 GOTO(out, rc = -EINVAL);
2265 if (lmd_is_client(lmd)) {
2267 CERROR("%s: attempting to mount a client with -t lustre_tgt' which is only for server-side mounts: rc = %d\n",
2273 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
2274 rc = server_fill_super(sb);
2276 * server_fill_super calls lustre_start_mgc after the mount
2277 * because we need the MGS NIDs which are stored on disk.
2278 * Plus, we may need to start the MGS first.
2280 * server_fill_super will call server_put_super on failure
2282 * If error happens in fill_super() call, @lsi will be killed there.
2283 * This is why we do not put it here.
2287 CERROR("Unable to mount %s (%d)\n",
2288 s2lsi(sb) ? lmd->lmd_dev : "", rc);
2290 CDEBUG(D_SUPER, "Mount %s complete\n",
2297 /***************** FS registration ******************/
2298 static struct dentry *lustre_tgt_mount(struct file_system_type *fs_type,
2299 int flags, const char *devname,
2302 return mount_nodev(fs_type, flags, data, lustre_tgt_fill_super);
2305 /* Register the "lustre_tgt" fs type.
2307 * Right now this isn't any different than the normal "lustre" filesystem
2308 * type, but it is added so that there is some compatibility to allow
2309 * changing documentation and scripts to start using the "lustre_tgt" type
2310 * at mount time. That will simplify test interop, and in case of upgrades
2311 * that change to the new type and then need to roll back for some reason.
2313 * The long-term goal is to disentangle the client and server mount code.
2315 static struct file_system_type lustre_tgt_fstype = {
2316 .owner = THIS_MODULE,
2317 .name = "lustre_tgt",
2318 .mount = lustre_tgt_mount,
2319 .kill_sb = kill_anon_super,
2320 .fs_flags = FS_REQUIRES_DEV | FS_RENAME_DOES_D_MOVE,
2322 MODULE_ALIAS_FS("lustre_tgt");
2324 int lustre_tgt_register_fs(void)
2326 return register_filesystem(&lustre_tgt_fstype);
2329 void lustre_tgt_unregister_fs(void)
2331 unregister_filesystem(&lustre_tgt_fstype);