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 #include "tgt_internal.h"
62 /*********** mount lookup *********/
64 static DEFINE_MUTEX(lustre_mount_info_lock);
65 static LIST_HEAD(server_mount_info_list);
67 static struct lustre_mount_info *server_find_mount(const char *name)
69 struct list_head *tmp;
70 struct lustre_mount_info *lmi;
73 list_for_each(tmp, &server_mount_info_list) {
74 lmi = list_entry(tmp, struct lustre_mount_info,
76 if (strcmp(name, lmi->lmi_name) == 0)
82 /* we must register an obd for a mount before we call the setup routine.
83 *_setup will call lustre_get_mount to get the mnt struct
84 * by obd_name, since we can't pass the pointer to setup.
86 static int server_register_mount(const char *name, struct super_block *sb)
88 struct lustre_mount_info *lmi;
94 OBD_ALLOC(lmi, sizeof(*lmi));
97 OBD_ALLOC(name_cp, strlen(name) + 1);
99 OBD_FREE(lmi, sizeof(*lmi));
102 strcpy(name_cp, name);
104 mutex_lock(&lustre_mount_info_lock);
106 if (server_find_mount(name)) {
107 mutex_unlock(&lustre_mount_info_lock);
108 OBD_FREE(lmi, sizeof(*lmi));
109 OBD_FREE(name_cp, strlen(name) + 1);
110 CERROR("Already registered %s\n", name);
113 lmi->lmi_name = name_cp;
115 list_add(&lmi->lmi_list_chain, &server_mount_info_list);
117 mutex_unlock(&lustre_mount_info_lock);
119 CDEBUG(D_MOUNT, "register mount %p from %s\n", sb, name);
124 /* when an obd no longer needs a mount */
125 static int server_deregister_mount(const char *name)
127 struct lustre_mount_info *lmi;
130 mutex_lock(&lustre_mount_info_lock);
131 lmi = server_find_mount(name);
133 mutex_unlock(&lustre_mount_info_lock);
134 CERROR("%s not registered\n", name);
138 CDEBUG(D_MOUNT, "deregister mount %p from %s\n", lmi->lmi_sb, name);
140 OBD_FREE(lmi->lmi_name, strlen(lmi->lmi_name) + 1);
141 list_del(&lmi->lmi_list_chain);
142 OBD_FREE(lmi, sizeof(*lmi));
143 mutex_unlock(&lustre_mount_info_lock);
145 CFS_RACE(OBD_FAIL_MDS_LLOG_UMOUNT_RACE);
149 /* obd's look up a registered mount using their obdname. This is just
150 * for initial obd setup to find the mount struct. It should not be
151 * called every time you want to mntget.
153 struct lustre_mount_info *server_get_mount(const char *name)
155 struct lustre_mount_info *lmi;
156 struct lustre_sb_info *lsi;
159 mutex_lock(&lustre_mount_info_lock);
160 lmi = server_find_mount(name);
161 mutex_unlock(&lustre_mount_info_lock);
163 CERROR("Can't find mount for %s\n", name);
166 lsi = s2lsi(lmi->lmi_sb);
168 atomic_inc(&lsi->lsi_mounts);
170 CDEBUG(D_MOUNT, "get mount %p from %s, refs=%d\n", lmi->lmi_sb,
171 name, atomic_read(&lsi->lsi_mounts));
175 EXPORT_SYMBOL(server_get_mount);
178 * server_put_mount: to be called from obd_cleanup methods
180 * @dereg_mnt: 0 or 1 depending on whether the mount is to be deregistered or
183 * The caller decides whether server_deregister_mount() needs to be called or
184 * not. Calling of server_deregister_mount() does not depend on refcounting on
185 * lsi because we could have say the mgs and mds on the same node and we
186 * unmount the mds, then the ref on the lsi would still be non-zero but we
187 * would still want to deregister the mds mount.
189 int server_put_mount(const char *name, bool dereg_mnt)
191 struct lustre_mount_info *lmi;
192 struct lustre_sb_info *lsi;
195 mutex_lock(&lustre_mount_info_lock);
196 lmi = server_find_mount(name);
197 mutex_unlock(&lustre_mount_info_lock);
199 CERROR("Can't find mount for %s\n", name);
202 lsi = s2lsi(lmi->lmi_sb);
204 CDEBUG(D_MOUNT, "put mount %p from %s, refs=%d\n",
205 lmi->lmi_sb, name, atomic_read(&lsi->lsi_mounts));
207 if (lustre_put_lsi(lmi->lmi_sb))
208 CDEBUG(D_MOUNT, "Last put of mount %p from %s\n",
212 /* this obd should never need the mount again */
213 server_deregister_mount(name);
217 EXPORT_SYMBOL(server_put_mount);
219 /* Set up a MGS to serve startup logs */
220 static int server_start_mgs(struct super_block *sb)
222 struct lustre_sb_info *lsi = s2lsi(sb);
223 struct lustre_mount_info *lmi;
227 /* It is impossible to have more than 1 MGS per node, since
228 * MGC wouldn't know which to connect to
230 lmi = server_find_mount(LUSTRE_MGS_OBDNAME);
232 lsi = s2lsi(lmi->lmi_sb);
233 LCONSOLE_ERROR_MSG(0x15d,
234 "The MGS service was already started from server\n");
238 CDEBUG(D_CONFIG, "Start MGS service %s\n", LUSTRE_MGS_OBDNAME);
240 rc = server_register_mount(LUSTRE_MGS_OBDNAME, sb);
242 GOTO(report_err, rc);
244 rc = lustre_start_simple(LUSTRE_MGS_OBDNAME, LUSTRE_MGS_NAME,
245 LUSTRE_MGS_OBDNAME, NULL, NULL,
246 lsi->lsi_osd_obdname, NULL);
247 /* server_deregister_mount() is not called previously, for lsi
248 * and other stuff can't be freed cleanly when mgs calls
249 * server_put_mount() in error handling case (see b=17758),
250 * this problem is caused by a bug in mgs_init0, which forgot
251 * calling server_put_mount in error case.
254 server_deregister_mount(LUSTRE_MGS_OBDNAME);
256 LCONSOLE_ERROR_MSG(0x15e,
257 "Failed to start MGS '%s' (%d). Is the 'mgs' module loaded?\n",
258 LUSTRE_MGS_OBDNAME, rc);
263 static int server_stop_mgs(struct super_block *sb)
265 struct obd_device *obd;
267 struct lustre_mount_info *lmi;
270 /* Do not stop MGS if this device is not the running MGT */
271 lmi = server_find_mount(LUSTRE_MGS_OBDNAME);
272 if (lmi && lmi->lmi_sb != sb)
275 CDEBUG(D_MOUNT, "Stop MGS service %s\n", LUSTRE_MGS_OBDNAME);
277 /* There better be only one MGS */
278 obd = class_name2obd(LUSTRE_MGS_OBDNAME);
280 CDEBUG(D_CONFIG, "mgs %s not running\n", LUSTRE_MGS_OBDNAME);
284 /* The MGS should always stop when we say so */
286 rc = class_manual_cleanup(obd);
290 /* Since there's only one mgc per node, we have to change it's fs to get
291 * access to the right disk.
293 static int server_mgc_set_fs(const struct lu_env *env,
294 struct obd_device *mgc, struct super_block *sb)
296 struct lustre_sb_info *lsi = s2lsi(sb);
300 CDEBUG(D_MOUNT, "Set mgc disk for %s\n", lsi->lsi_lmd->lmd_dev);
302 /* cl_mgc_sem in mgc insures we sleep if the mgc_fs is busy */
303 rc = obd_set_info_async(env, mgc->obd_self_export,
304 sizeof(KEY_SET_FS), KEY_SET_FS,
305 sizeof(*sb), sb, NULL);
307 CERROR("can't set_fs %d\n", rc);
312 static int server_mgc_clear_fs(const struct lu_env *env,
313 struct obd_device *mgc)
318 CDEBUG(D_MOUNT, "Unassign mgc disk\n");
319 rc = obd_set_info_async(env, mgc->obd_self_export,
320 sizeof(KEY_CLEAR_FS), KEY_CLEAR_FS,
325 static inline bool is_mdc_device(const char *devname)
329 ptr = strrchr(devname, '-');
330 return ptr && strcmp(ptr, "-mdc") == 0;
333 static inline bool tgt_is_mdt(const char *tgtname, u32 *idx)
337 type = server_name2index(tgtname, idx, NULL);
339 return type == LDD_F_SV_TYPE_MDT;
343 * Convert OST/MDT name(fsname-{MDT,OST}xxxx) to a lwp name with the @idx:yyyy
344 * (fsname-MDTyyyy-lwp-{MDT,OST}xxxx)
346 int tgt_name2lwp_name(const char *tgt_name, char *lwp_name, int len, u32 idx)
353 OBD_ALLOC(fsname, MTI_NAME_MAXLEN);
357 rc = server_name2fsname(tgt_name, fsname, &tgt);
359 CERROR("%s: failed to get fsname from tgt_name: rc = %d\n",
364 if (*tgt != '-' && *tgt != ':') {
365 CERROR("%s: invalid tgt_name name!\n", tgt_name);
366 GOTO(cleanup, rc = -EINVAL);
370 if (strncmp(tgt, "OST", 3) != 0 && strncmp(tgt, "MDT", 3) != 0) {
371 CERROR("%s is not an OST or MDT target!\n", tgt_name);
372 GOTO(cleanup, rc = -EINVAL);
374 snprintf(lwp_name, len, "%s-MDT%04x-%s-%s",
375 fsname, idx, LUSTRE_LWP_NAME, tgt);
377 GOTO(cleanup, rc = 0);
381 OBD_FREE(fsname, MTI_NAME_MAXLEN);
385 EXPORT_SYMBOL(tgt_name2lwp_name);
387 static LIST_HEAD(lwp_register_list);
388 static DEFINE_SPINLOCK(lwp_register_list_lock);
390 static void lustre_put_lwp_item(struct lwp_register_item *lri)
392 if (atomic_dec_and_test(&lri->lri_ref)) {
393 LASSERT(list_empty(&lri->lri_list));
396 class_export_put(*lri->lri_exp);
401 int lustre_register_lwp_item(const char *lwpname, struct obd_export **exp,
402 register_lwp_cb cb_func, void *cb_data)
404 struct obd_device *lwp;
405 struct lwp_register_item *lri;
409 LASSERTF(strlen(lwpname) < MTI_NAME_MAXLEN, "lwpname is too long %s\n",
411 LASSERT(exp && !*exp);
417 lwp = class_name2obd(lwpname);
418 if (lwp && lwp->obd_set_up == 1) {
419 struct obd_uuid *uuid;
426 memcpy(uuid->uuid, lwpname, strlen(lwpname));
427 *exp = obd_uuid_lookup(lwp, uuid);
431 memcpy(lri->lri_name, lwpname, strlen(lwpname));
433 lri->lri_cb_func = cb_func;
434 lri->lri_cb_data = cb_data;
435 INIT_LIST_HEAD(&lri->lri_list);
437 * Initialize the lri_ref at 2, one will be released before
438 * current function returned via lustre_put_lwp_item(), the
439 * other will be released in lustre_deregister_lwp_item().
441 atomic_set(&lri->lri_ref, 2);
443 spin_lock(&lwp_register_list_lock);
444 list_add(&lri->lri_list, &lwp_register_list);
447 spin_unlock(&lwp_register_list_lock);
451 lustre_put_lwp_item(lri);
455 EXPORT_SYMBOL(lustre_register_lwp_item);
457 void lustre_deregister_lwp_item(struct obd_export **exp)
459 struct lwp_register_item *lri;
460 bool removed = false;
463 spin_lock(&lwp_register_list_lock);
464 list_for_each_entry(lri, &lwp_register_list, lri_list) {
465 if (exp == lri->lri_exp) {
466 list_del_init(&lri->lri_list);
471 spin_unlock(&lwp_register_list_lock);
476 /* See lustre_notify_lwp_list(), in some extreme race conditions,
477 * the notify callback could be still on the fly, we need to wait
478 * for the callback done before moving on to free the data used
481 while (atomic_read(&lri->lri_ref) > 1) {
482 CDEBUG(D_MOUNT, "lri reference count %u, repeat: %d\n",
483 atomic_read(&lri->lri_ref), repeat);
485 schedule_timeout_interruptible(cfs_time_seconds(1));
487 lustre_put_lwp_item(lri);
489 EXPORT_SYMBOL(lustre_deregister_lwp_item);
491 struct obd_export *lustre_find_lwp_by_index(const char *dev, u32 idx)
493 struct lustre_mount_info *lmi;
494 struct lustre_sb_info *lsi;
495 struct obd_device *lwp;
496 struct obd_export *exp = NULL;
501 lmi = server_get_mount(dev);
505 lsi = s2lsi(lmi->lmi_sb);
506 rc = server_name2fsname(lsi->lsi_svname, fsname, NULL);
508 CERROR("%s: failed to get fsname: rc = %d\n",
509 lsi->lsi_svname, rc);
513 snprintf(lwp_name, sizeof(lwp_name), "%s-MDT%04x", fsname, idx);
514 mutex_lock(&lsi->lsi_lwp_mutex);
515 list_for_each_entry(lwp, &lsi->lsi_lwp_list, obd_lwp_list) {
516 char *ptr = strstr(lwp->obd_name, lwp_name);
518 if (ptr && lwp->obd_lwp_export) {
519 exp = class_export_get(lwp->obd_lwp_export);
523 mutex_unlock(&lsi->lsi_lwp_mutex);
526 server_put_mount(dev, false);
530 EXPORT_SYMBOL(lustre_find_lwp_by_index);
532 void lustre_notify_lwp_list(struct obd_export *exp)
534 struct lwp_register_item *lri;
538 spin_lock(&lwp_register_list_lock);
539 list_for_each_entry(lri, &lwp_register_list, lri_list) {
540 if (strcmp(exp->exp_obd->obd_name, lri->lri_name))
544 *lri->lri_exp = class_export_get(exp);
545 if (!lri->lri_cb_func)
547 atomic_inc(&lri->lri_ref);
548 spin_unlock(&lwp_register_list_lock);
550 lri->lri_cb_func(lri->lri_cb_data);
551 lustre_put_lwp_item(lri);
553 /* Others may have changed the list after we unlock, we have
554 * to rescan the list from the beginning. Usually, the list
555 * 'lwp_register_list' is very short, and there is 'guard'
556 * lri::lri_exp that will prevent the callback to be done
557 * repeatedly. So rescanning the list has no problem.
561 spin_unlock(&lwp_register_list_lock);
563 EXPORT_SYMBOL(lustre_notify_lwp_list);
565 static int lustre_lwp_connect(struct obd_device *lwp, bool is_mdt)
568 struct lu_context session_ctx;
569 struct obd_export *exp;
570 struct obd_uuid *uuid = NULL;
571 struct obd_connect_data *data = NULL;
575 /* log has been fully processed, let clients connect */
576 rc = lu_env_init(&env, lwp->obd_lu_dev->ld_type->ldt_ctx_tags);
580 lu_context_init(&session_ctx, LCT_SERVER_SESSION);
581 session_ctx.lc_thread = NULL;
582 lu_context_enter(&session_ctx);
583 env.le_ses = &session_ctx;
587 GOTO(out, rc = -ENOMEM);
589 data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_INDEX;
590 data->ocd_version = LUSTRE_VERSION_CODE;
591 data->ocd_connect_flags |= OBD_CONNECT_FID | OBD_CONNECT_AT |
592 OBD_CONNECT_LRU_RESIZE | OBD_CONNECT_FULL20 |
593 OBD_CONNECT_LVB_TYPE | OBD_CONNECT_LIGHTWEIGHT |
594 OBD_CONNECT_LFSCK | OBD_CONNECT_BULK_MBITS;
597 data->ocd_connect_flags |= OBD_CONNECT_MDS_MDS;
601 GOTO(out, rc = -ENOMEM);
603 if (strlen(lwp->obd_name) > sizeof(uuid->uuid)) {
604 CERROR("%s: Too long lwp name %s, max_size is %d\n",
605 lwp->obd_name, lwp->obd_name, (int)sizeof(uuid->uuid));
606 GOTO(out, rc = -EINVAL);
609 /* Use lwp name as the uuid, so we find the export by lwp name later */
610 memcpy(uuid->uuid, lwp->obd_name, strlen(lwp->obd_name));
611 rc = obd_connect(&env, &exp, lwp, uuid, data, NULL);
613 CERROR("%s: connect failed: rc = %d\n", lwp->obd_name, rc);
615 if (unlikely(lwp->obd_lwp_export))
616 class_export_put(lwp->obd_lwp_export);
617 lwp->obd_lwp_export = class_export_get(exp);
629 lu_context_exit(&session_ctx);
630 lu_context_fini(&session_ctx);
636 * lwp is used by slaves (Non-MDT0 targets) to manage the connection to MDT0,
637 * or from the OSTx to MDTy.
639 static int lustre_lwp_setup(struct lustre_cfg *lcfg, struct lustre_sb_info *lsi,
642 struct obd_device *obd;
643 char *lwpname = NULL;
644 char *lwpuuid = NULL;
650 lnet_nid4_to_nid(lcfg->lcfg_nid, &nid);
652 rc = libcfs_strnid(&nid, lustre_cfg_string(lcfg, 2));
656 rc = class_add_uuid(lustre_cfg_string(lcfg, 1), &nid);
658 CERROR("%s: Can't add uuid: rc =%d\n", lsi->lsi_svname, rc);
662 OBD_ALLOC(lwpname, MTI_NAME_MAXLEN);
664 GOTO(out, rc = -ENOMEM);
666 rc = tgt_name2lwp_name(lsi->lsi_svname, lwpname, MTI_NAME_MAXLEN, idx);
668 CERROR("%s: failed to generate lwp name: rc = %d\n",
669 lsi->lsi_svname, rc);
673 OBD_ALLOC(lwpuuid, MTI_NAME_MAXLEN);
675 GOTO(out, rc = -ENOMEM);
677 sprintf(lwpuuid, "%s_UUID", lwpname);
678 rc = lustre_start_simple(lwpname, LUSTRE_LWP_NAME,
679 lwpuuid, lustre_cfg_string(lcfg, 1),
682 CERROR("%s: setup up failed: rc %d\n", lwpname, rc);
686 obd = class_name2obd(lwpname);
689 rc = lustre_lwp_connect(obd, strstr(lsi->lsi_svname, "-MDT") != NULL);
691 CERROR("%s: connect failed: rc = %d\n", lwpname, rc);
695 obd->u.cli.cl_max_mds_easize = MAX_MD_SIZE;
696 mutex_lock(&lsi->lsi_lwp_mutex);
697 list_add_tail(&obd->obd_lwp_list, &lsi->lsi_lwp_list);
698 mutex_unlock(&lsi->lsi_lwp_mutex);
701 OBD_FREE(lwpname, MTI_NAME_MAXLEN);
703 OBD_FREE(lwpuuid, MTI_NAME_MAXLEN);
708 /* the caller is responsible for memory free */
709 static struct obd_device *lustre_find_lwp(struct lustre_sb_info *lsi,
710 char **lwpname, u32 idx)
712 struct obd_device *lwp;
717 LASSERT(IS_OST(lsi) || IS_MDT(lsi));
719 OBD_ALLOC(*lwpname, MTI_NAME_MAXLEN);
720 if (*lwpname == NULL)
721 RETURN(ERR_PTR(-ENOMEM));
723 rc = tgt_name2lwp_name(lsi->lsi_svname, *lwpname, MTI_NAME_MAXLEN, idx);
725 CERROR("%s: failed to generate lwp name: rc = %d\n",
726 lsi->lsi_svname, rc);
727 GOTO(out, rc = -EINVAL);
730 lwp = class_name2obd(*lwpname);
734 if (*lwpname != NULL) {
735 OBD_FREE(*lwpname, MTI_NAME_MAXLEN);
741 RETURN(lwp ? lwp : ERR_PTR(-ENOENT));
744 static int lustre_lwp_add_conn(struct lustre_cfg *cfg,
745 struct lustre_sb_info *lsi, u32 idx)
747 struct lustre_cfg_bufs *bufs = NULL;
748 struct lustre_cfg *lcfg = NULL;
749 char *lwpname = NULL;
750 struct obd_device *lwp;
754 lwp = lustre_find_lwp(lsi, &lwpname, idx);
756 CERROR("%s: can't find lwp device.\n", lsi->lsi_svname);
757 GOTO(out, rc = PTR_ERR(lwp));
763 GOTO(out, rc = -ENOMEM);
765 lustre_cfg_bufs_reset(bufs, lwpname);
766 lustre_cfg_bufs_set_string(bufs, 1,
767 lustre_cfg_string(cfg, 1));
769 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
771 GOTO(out_cfg, rc = -ENOMEM);
772 lustre_cfg_init(lcfg, LCFG_ADD_CONN, bufs);
774 rc = class_add_conn(lwp, lcfg);
776 CERROR("%s: can't add conn: rc = %d\n", lwpname, rc);
779 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
780 lcfg->lcfg_buflens));
786 OBD_FREE(lwpname, MTI_NAME_MAXLEN);
791 * Retrieve MDT nids from the client log, then start the lwp device.
792 * there are only two scenarios which would include mdt nid.
794 * marker 5 (flags=0x01, v2.1.54.0) lustre-MDTyyyy 'add mdc' xxx-
795 * add_uuid nid=192.168.122.162@tcp(0x20000c0a87aa2) 0: 1:192.168.122.162@tcp
796 * attach 0:lustre-MDTyyyy-mdc 1:mdc 2:lustre-clilmv_UUID
797 * setup 0:lustre-MDTyyyy-mdc 1:lustre-MDTyyyy_UUID 2:192.168.122.162@tcp
798 * add_uuid nid=192.168.172.1@tcp(0x20000c0a8ac01) 0: 1:192.168.172.1@tcp
799 * add_conn 0:lustre-MDTyyyy-mdc 1:192.168.172.1@tcp
800 * modify_mdc_tgts add 0:lustre-clilmv 1:lustre-MDTyyyy_UUID xxxx
801 * marker 5 (flags=0x02, v2.1.54.0) lustre-MDTyyyy 'add mdc' xxxx-
803 * marker 7 (flags=0x01, v2.1.54.0) lustre-MDTyyyy 'add failnid' xxxx-
804 * add_uuid nid=192.168.122.2@tcp(0x20000c0a87a02) 0: 1:192.168.122.2@tcp
805 * add_conn 0:lustre-MDTyyyy-mdc 1:192.168.122.2@tcp
806 * marker 7 (flags=0x02, v2.1.54.0) lustre-MDTyyyy 'add failnid' xxxx-
808 static int client_lwp_config_process(const struct lu_env *env,
809 struct llog_handle *handle,
810 struct llog_rec_hdr *rec, void *data)
812 struct config_llog_instance *cfg = data;
813 int cfg_len = rec->lrh_len;
814 char *cfg_buf = (char *) (rec + 1);
815 struct lustre_cfg *lcfg = NULL;
816 struct lustre_sb_info *lsi;
817 int rc = 0, swab = 0;
820 if (rec->lrh_type != OBD_CFG_REC) {
821 CERROR("Unknown llog record type %#x encountered\n",
827 GOTO(out, rc = -EINVAL);
828 lsi = s2lsi(cfg->cfg_sb);
830 lcfg = (struct lustre_cfg *)cfg_buf;
831 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
832 lustre_swab_lustre_cfg(lcfg);
836 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
840 switch (lcfg->lcfg_command) {
842 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
844 lustre_swab_cfg_marker(marker, swab,
845 LUSTRE_CFG_BUFLEN(lcfg, 1));
846 if (marker->cm_flags & CM_SKIP ||
847 marker->cm_flags & CM_EXCLUDE)
850 if (!tgt_is_mdt(marker->cm_tgtname, &cfg->cfg_lwp_idx))
853 if (IS_MDT(lsi) && cfg->cfg_lwp_idx != 0)
856 if (!strncmp(marker->cm_comment, "add mdc", 7) ||
857 !strncmp(marker->cm_comment, "add failnid", 11)) {
858 if (marker->cm_flags & CM_START) {
859 cfg->cfg_flags = CFG_F_MARKER;
860 /* This hack is to differentiate the
861 * ADD_UUID is come from "add mdc" record
862 * or from "add failnid" record.
864 if (!strncmp(marker->cm_comment,
866 cfg->cfg_flags |= CFG_F_SKIP;
867 } else if (marker->cm_flags & CM_END) {
873 case LCFG_ADD_UUID: {
874 if (cfg->cfg_flags == CFG_F_MARKER) {
875 rc = lustre_lwp_setup(lcfg, lsi, cfg->cfg_lwp_idx);
876 /* XXX: process only the first nid as
877 * we don't need another instance of lwp
879 cfg->cfg_flags |= CFG_F_SKIP;
880 } else if (cfg->cfg_flags == (CFG_F_MARKER | CFG_F_SKIP)) {
885 lnet_nid4_to_nid(lcfg->lcfg_nid, &nid);
887 rc = libcfs_strnid(&nid,
888 lustre_cfg_string(lcfg, 2));
890 rc = class_add_uuid(lustre_cfg_string(lcfg, 1),
893 CERROR("%s: Fail to add uuid, rc:%d\n",
894 lsi->lsi_svname, rc);
898 case LCFG_ADD_CONN: {
899 char *devname = lustre_cfg_string(lcfg, 0);
903 if (!is_mdc_device(devname))
906 if (!(cfg->cfg_flags & CFG_F_MARKER)) {
907 CDEBUG(D_CONFIG, "Skipping add_conn for %s, rec %d\n",
908 devname, rec->lrh_index);
912 /* add_conn should follow by add_uuid. This
913 * guarantee lwp device was created
915 if (!(cfg->cfg_flags & CFG_F_SKIP)) {
916 CWARN("Error at config for %s rec %d, add_conn should follow by add_uuid\n",
917 devname, rec->lrh_index);
920 ptr = strrchr(devname, '-');
925 if (!tgt_is_mdt(devname, &idx)) {
931 if (IS_MDT(lsi) && idx != 0)
934 rc = lustre_lwp_add_conn(lcfg, lsi, idx);
944 static int lustre_disconnect_lwp(struct super_block *sb)
946 struct lustre_sb_info *lsi = s2lsi(sb);
947 struct obd_device *lwp;
948 char *logname = NULL;
949 struct lustre_cfg_bufs *bufs = NULL;
950 struct config_llog_instance *cfg = NULL;
955 if (likely(lsi->lsi_lwp_started)) {
956 OBD_ALLOC(logname, MTI_NAME_MAXLEN);
960 rc = server_name2fsname(lsi->lsi_svname, logname, NULL);
962 CERROR("%s: failed to get fsname from svname: rc = %d\n",
963 lsi->lsi_svname, rc);
964 GOTO(out, rc = -EINVAL);
967 strcat(logname, "-client");
970 GOTO(out, rc = -ENOMEM);
973 cfg->cfg_instance = ll_get_cfg_instance(sb);
974 rc = lustre_end_log(sb, logname, cfg);
975 if (rc != 0 && rc != -ENOENT)
978 lsi->lsi_lwp_started = 0;
983 GOTO(out, rc = -ENOMEM);
985 mutex_lock(&lsi->lsi_lwp_mutex);
986 list_for_each_entry(lwp, &lsi->lsi_lwp_list, obd_lwp_list) {
987 struct lustre_cfg *lcfg;
989 if (likely(lwp->obd_lwp_export)) {
990 class_export_put(lwp->obd_lwp_export);
991 lwp->obd_lwp_export = NULL;
994 lustre_cfg_bufs_reset(bufs, lwp->obd_name);
995 lustre_cfg_bufs_set_string(bufs, 1, NULL);
996 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount,
1002 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
1004 /* Disconnect import first. NULL is passed for the '@env',
1005 * since it will not be used.
1007 rc = lwp->obd_lu_dev->ld_ops->ldo_process_config(NULL,
1008 lwp->obd_lu_dev, lcfg);
1009 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
1010 lcfg->lcfg_buflens));
1011 if (rc != 0 && rc != -ETIMEDOUT && rc != -ENODEV &&
1012 rc != -ENOTCONN && rc != -ESHUTDOWN) {
1013 CERROR("%s: fail to disconnect LWP: rc = %d\n",
1018 mutex_unlock(&lsi->lsi_lwp_mutex);
1028 OBD_FREE(logname, MTI_NAME_MAXLEN);
1030 return rc1 != 0 ? rc1 : rc;
1034 * Stop the lwp for an OST/MDT target.
1036 static int lustre_stop_lwp(struct super_block *sb)
1038 struct lustre_sb_info *lsi = s2lsi(sb);
1039 struct obd_device *lwp;
1044 mutex_lock(&lsi->lsi_lwp_mutex);
1045 while (!list_empty(&lsi->lsi_lwp_list)) {
1046 lwp = list_entry(lsi->lsi_lwp_list.next, struct obd_device,
1048 list_del_init(&lwp->obd_lwp_list);
1050 mutex_unlock(&lsi->lsi_lwp_mutex);
1052 rc = class_manual_cleanup(lwp);
1054 CERROR("%s: fail to stop LWP: rc = %d\n",
1058 mutex_lock(&lsi->lsi_lwp_mutex);
1060 mutex_unlock(&lsi->lsi_lwp_mutex);
1062 RETURN(rc1 != 0 ? rc1 : rc);
1066 * Start the lwp(fsname-MDTyyyy-lwp-{MDT,OST}xxxx) for a MDT/OST or MDT target.
1068 static int lustre_start_lwp(struct super_block *sb)
1070 struct lustre_sb_info *lsi = s2lsi(sb);
1071 struct config_llog_instance *cfg = NULL;
1076 if (unlikely(lsi->lsi_lwp_started))
1079 OBD_ALLOC(logname, MTI_NAME_MAXLEN);
1080 if (logname == NULL)
1083 rc = server_name2fsname(lsi->lsi_svname, logname, NULL);
1085 CERROR("%s: failed to get fsname from svname: rc = %d\n",
1086 lsi->lsi_svname, rc);
1087 GOTO(out, rc = -EINVAL);
1090 strcat(logname, "-client");
1093 GOTO(out, rc = -ENOMEM);
1095 cfg->cfg_callback = client_lwp_config_process;
1096 cfg->cfg_instance = ll_get_cfg_instance(sb);
1097 rc = lustre_process_log(sb, logname, cfg);
1098 /* need to remove config llog from mgc */
1099 lsi->lsi_lwp_started = 1;
1104 OBD_FREE(logname, MTI_NAME_MAXLEN);
1111 static DEFINE_MUTEX(server_start_lock);
1113 /* Stop MDS/OSS if nobody is using them */
1114 static int server_stop_servers(int lsiflags)
1116 struct obd_device *obd = NULL;
1117 struct obd_type *type = NULL;
1122 mutex_lock(&server_start_lock);
1124 /* Either an MDT or an OST or neither */
1125 /* if this was an MDT, and there are no more MDT's, clean up the MDS */
1126 if (lsiflags & LDD_F_SV_TYPE_MDT) {
1127 obd = class_name2obd(LUSTRE_MDS_OBDNAME);
1128 type = class_search_type(LUSTRE_MDT_NAME);
1129 } else if (lsiflags & LDD_F_SV_TYPE_OST) {
1130 /* if this was an OST, and there are no more OST's, clean up the OSS */
1131 obd = class_name2obd(LUSTRE_OSS_OBDNAME);
1132 type = class_search_type(LUSTRE_OST_NAME);
1135 /* server_stop_servers is a pair of server_start_targets
1136 * Here we put type which was taken at server_start_targets.
1137 * If type is NULL then there is a wrong logic around type or
1140 LASSERTF(type, "Server flags %d, obd %s\n", lsiflags,
1141 obd ? obd->obd_name : "NULL");
1143 type_last = (atomic_read(&type->typ_refcnt) == 1);
1145 class_put_type(type);
1146 if (obd && type_last) {
1148 /* obd_fail doesn't mean much on a server obd */
1149 rc = class_manual_cleanup(obd);
1152 /* put reference taken by class_search_type */
1153 kobject_put(&type->typ_kobj);
1155 mutex_unlock(&server_start_lock);
1160 int server_mti_print(const char *title, struct mgs_target_info *mti)
1162 PRINT_CMD(PRINT_MASK, "mti %s\n", title);
1163 PRINT_CMD(PRINT_MASK, "server: %s\n", mti->mti_svname);
1164 PRINT_CMD(PRINT_MASK, "fs: %s\n", mti->mti_fsname);
1165 PRINT_CMD(PRINT_MASK, "uuid: %s\n", mti->mti_uuid);
1166 PRINT_CMD(PRINT_MASK, "ver: %d flags: %#x\n",
1167 mti->mti_config_ver, mti->mti_flags);
1171 /* Generate data for registration */
1172 static int server_lsi2mti(struct lustre_sb_info *lsi,
1173 struct mgs_target_info *mti)
1175 struct lnet_processid id;
1180 if (!IS_SERVER(lsi))
1183 if (strlcpy(mti->mti_svname, lsi->lsi_svname, sizeof(mti->mti_svname))
1184 >= sizeof(mti->mti_svname))
1187 mti->mti_nid_count = 0;
1188 while (LNetGetId(i++, &id) != -ENOENT) {
1189 if (nid_is_lo0(&id.nid))
1192 /* server use --servicenode param, only allow specified
1193 * nids be registered
1195 if (test_bit(LMD_FLG_NO_PRIMNODE, lsi->lsi_lmd->lmd_flags) &&
1196 class_match_nid(lsi->lsi_lmd->lmd_params,
1197 PARAM_FAILNODE, &id.nid) < 1)
1200 if (!class_find_param(lsi->lsi_lmd->lmd_params,
1201 PARAM_NETWORK, NULL)) {
1202 if (LNetGetPeerDiscoveryStatus()) {
1203 CERROR("LNet Dynamic Peer Discovery is enabled"
1204 " on this node. 'network' option used in"
1205 " mkfs.lustre cannot be taken into"
1211 /* match specified network */
1212 if (!class_match_net(lsi->lsi_lmd->lmd_params,
1213 PARAM_NETWORK, LNET_NID_NET(&id.nid)))
1216 mti->mti_nids[mti->mti_nid_count] = lnet_nid_to_nid4(&id.nid);
1217 mti->mti_nid_count++;
1218 if (mti->mti_nid_count >= MTI_NIDS_MAX) {
1219 CWARN("Only using first %d nids for %s\n",
1220 mti->mti_nid_count, mti->mti_svname);
1225 if (mti->mti_nid_count == 0) {
1226 CERROR("Failed to get NID for server %s, please check whether the target is specifed with improper --servicenode or --network options.\n",
1231 mti->mti_lustre_ver = LUSTRE_VERSION_CODE;
1232 mti->mti_config_ver = 0;
1234 rc = server_name2fsname(lsi->lsi_svname, mti->mti_fsname, NULL);
1238 rc = server_name2index(lsi->lsi_svname, &mti->mti_stripe_index, NULL);
1241 /* Orion requires index to be set */
1242 LASSERT(!(rc & LDD_F_NEED_INDEX));
1243 /* keep only LDD flags */
1244 mti->mti_flags = lsi->lsi_flags & LDD_F_MASK;
1245 if (mti->mti_flags & (LDD_F_WRITECONF | LDD_F_VIRGIN))
1246 mti->mti_flags |= LDD_F_UPDATE;
1247 cplen = strlcpy(mti->mti_params, lsi->lsi_lmd->lmd_params,
1248 sizeof(mti->mti_params));
1249 if (cplen >= sizeof(mti->mti_params))
1254 /* Register an old or new target with the MGS. If needed MGS will construct
1255 * startup logs and assign index
1257 static int server_register_target(struct lustre_sb_info *lsi)
1259 struct obd_device *mgc = lsi->lsi_mgc;
1260 struct mgs_target_info *mti = NULL;
1268 if (!IS_SERVER(lsi))
1275 rc = server_lsi2mti(lsi, mti);
1279 CDEBUG(D_MOUNT, "Registration %s, fs=%s, %s, index=%04x, flags=%#x\n",
1280 mti->mti_svname, mti->mti_fsname,
1281 libcfs_nid2str(mti->mti_nids[0]), mti->mti_stripe_index,
1284 /* we cannot ignore registration failure if MGS logs must be updated. */
1285 must_succeed = !!(lsi->lsi_flags &
1286 (LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_WRITECONF |
1288 mti->mti_flags |= LDD_F_OPC_REG;
1291 /* Register the target */
1292 /* FIXME use mgc_process_config instead */
1293 rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1294 sizeof(KEY_REGISTER_TARGET),
1295 KEY_REGISTER_TARGET,
1296 sizeof(*mti), mti, NULL);
1298 if (mti->mti_flags & LDD_F_ERROR) {
1299 LCONSOLE_ERROR_MSG(0x160,
1300 "%s: the MGS refuses to allow this server to start: rc = %d. Please see messages on the MGS.\n",
1301 lsi->lsi_svname, rc);
1302 } else if (must_succeed) {
1303 if ((rc == -ESHUTDOWN || rc == -EIO) && ++tried < 5) {
1304 /* The connection with MGS is not established.
1305 * Try again after 2 seconds. Interruptable.
1307 schedule_timeout_interruptible(cfs_time_seconds(2));
1308 if (!signal_pending(current))
1312 LCONSOLE_ERROR_MSG(0x15f,
1313 "%s: cannot register this server with the MGS: rc = %d. Is the MGS running?\n",
1314 lsi->lsi_svname, rc);
1317 "%s: error registering with the MGS: rc = %d (not fatal)\n",
1318 lsi->lsi_svname, rc);
1319 /* reset the error code for non-fatal error. */
1332 * Notify the MGS that this target is ready.
1333 * Used by IR - if the MGS receives this message, it will notify clients.
1335 static int server_notify_target(struct super_block *sb, struct obd_device *obd)
1337 struct lustre_sb_info *lsi = s2lsi(sb);
1338 struct obd_device *mgc = lsi->lsi_mgc;
1339 struct mgs_target_info *mti = NULL;
1345 if (!(IS_SERVER(lsi)))
1351 rc = server_lsi2mti(lsi, mti);
1355 mti->mti_instance = obd2obt(obd)->obt_instance;
1356 mti->mti_flags |= LDD_F_OPC_READY;
1358 /* FIXME use mgc_process_config instead */
1359 rc = obd_set_info_async(NULL, mgc->u.cli.cl_mgc_mgsexp,
1360 sizeof(KEY_REGISTER_TARGET),
1361 KEY_REGISTER_TARGET,
1362 sizeof(*mti), mti, NULL);
1364 /* Imperative recovery: if the mgs informs us to use IR? */
1365 if (!rc && !(mti->mti_flags & LDD_F_ERROR) &&
1366 (mti->mti_flags & LDD_F_IR_CAPABLE))
1367 lsi->lsi_flags |= LDD_F_IR_CAPABLE;
1376 /* Start server targets: MDTs and OSTs */
1377 static int server_start_targets(struct super_block *sb)
1379 struct obd_device *obd;
1380 struct lustre_sb_info *lsi = s2lsi(sb);
1381 struct config_llog_instance cfg;
1382 struct lu_env mgc_env;
1383 struct lu_device *dev;
1384 char *name_service, *obd_name_service = NULL;
1385 struct obd_type *type = NULL;
1389 CDEBUG(D_MOUNT, "starting target %s\n", lsi->lsi_svname);
1391 LASSERTF(IS_MDT(lsi) || IS_OST(lsi), "designed for MDT or OST only\n");
1394 obd_name_service = LUSTRE_MDS_OBDNAME;
1395 name_service = LUSTRE_MDS_NAME;
1397 obd_name_service = LUSTRE_OSS_OBDNAME;
1398 name_service = LUSTRE_OSS_NAME;
1401 /* make sure MDS/OSS is started */
1402 mutex_lock(&server_start_lock);
1403 obd = class_name2obd(obd_name_service);
1405 rc = lustre_start_simple(obd_name_service, name_service,
1407 LUSTRE_MDS_OBDNAME"_uuid" :
1408 LUSTRE_OSS_OBDNAME"_uuid"),
1409 NULL, NULL, NULL, NULL);
1411 mutex_unlock(&server_start_lock);
1412 CERROR("failed to start %s: %d\n",
1413 obd_name_service, rc);
1417 /* hold a type reference and put it at server_stop_servers */
1418 type = class_get_type(IS_MDT(lsi) ?
1419 LUSTRE_MDT_NAME : LUSTRE_OST_NAME);
1421 mutex_unlock(&server_start_lock);
1422 GOTO(out_stop_service, rc = -ENODEV);
1424 lsi->lsi_server_started = 1;
1425 mutex_unlock(&server_start_lock);
1426 if (CFS_FAIL_PRECHECK(OBD_FAIL_OBD_STOP_MDS_RACE) &&
1428 CFS_RACE(OBD_FAIL_OBD_STOP_MDS_RACE);
1429 msleep(2 * MSEC_PER_SEC);
1432 rc = lu_env_init(&mgc_env, LCT_MG_THREAD);
1434 GOTO(out_stop_service, rc);
1436 /* Set the mgc fs to our server disk. This allows the MGC to
1437 * read and write configs locally, in case it can't talk to the MGS.
1439 rc = server_mgc_set_fs(&mgc_env, lsi->lsi_mgc, sb);
1443 /* Register with MGS */
1444 rc = server_register_target(lsi);
1448 /* Let the target look up the mount using the target's name
1449 * (we can't pass the sb or mnt through class_process_config.)
1451 rc = server_register_mount(lsi->lsi_svname, sb);
1455 /* Start targets using the llog named for the target */
1456 memset(&cfg, 0, sizeof(cfg));
1457 cfg.cfg_callback = class_config_llog_handler;
1458 cfg.cfg_sub_clds = CONFIG_SUB_SERVER;
1459 rc = lustre_process_log(sb, lsi->lsi_svname, &cfg);
1461 CERROR("failed to start server %s: %d\n",
1462 lsi->lsi_svname, rc);
1463 /* Do NOT call server_deregister_mount() here. This makes it
1464 * impossible to find mount later in cleanup time and leaves
1465 * @lsi and othder stuff leaked. -umka
1470 obd = class_name2obd(lsi->lsi_svname);
1472 CERROR("no server named %s was started\n", lsi->lsi_svname);
1473 GOTO(out_mgc, rc = -ENXIO);
1476 if (IS_OST(lsi) || IS_MDT(lsi)) {
1477 rc = lustre_start_lwp(sb);
1479 CERROR("%s: failed to start LWP: %d\n",
1480 lsi->lsi_svname, rc);
1485 server_notify_target(sb, obd);
1487 /* calculate recovery timeout, do it after lustre_process_log */
1488 server_calc_timeout(lsi, obd);
1490 /* log has been fully processed, let clients connect */
1491 dev = obd->obd_lu_dev;
1492 if (dev && dev->ld_ops->ldo_prepare) {
1495 rc = lu_env_init(&env, dev->ld_type->ldt_ctx_tags);
1497 struct lu_context session_ctx;
1499 lu_context_init(&session_ctx, LCT_SERVER_SESSION);
1500 session_ctx.lc_thread = NULL;
1501 lu_context_enter(&session_ctx);
1502 env.le_ses = &session_ctx;
1504 rc = dev->ld_ops->ldo_prepare(&env, NULL, dev);
1507 lu_context_exit(&session_ctx);
1508 lu_context_fini(&session_ctx);
1512 /* abort recovery only on the complete stack:
1513 * many devices can be involved
1515 if ((test_bit(LMD_FLG_ABORT_RECOV, lsi->lsi_lmd->lmd_flags) ||
1516 (test_bit(LMD_FLG_ABORT_RECOV_MDT, lsi->lsi_lmd->lmd_flags))) &&
1517 (OBP(obd, iocontrol))) {
1518 struct obd_ioctl_data karg;
1520 if (test_bit(LMD_FLG_ABORT_RECOV, lsi->lsi_lmd->lmd_flags))
1521 karg.ioc_type = OBD_FLG_ABORT_RECOV_OST;
1523 karg.ioc_type = OBD_FLG_ABORT_RECOV_MDT;
1525 obd_iocontrol(OBD_IOC_ABORT_RECOVERY, obd->obd_self_export, 0,
1530 /* Release the mgc fs for others to use */
1531 server_mgc_clear_fs(&mgc_env, lsi->lsi_mgc);
1533 lu_env_fini(&mgc_env);
1535 /* in case of error upper function call
1536 * server_put_super->server_stop_servers()
1542 static int lsi_prepare(struct lustre_sb_info *lsi)
1544 const char *osd_type;
1551 LASSERT(lsi->lsi_lmd);
1553 /* The server name is given as a mount line option */
1554 if (!lsi->lsi_lmd->lmd_profile) {
1555 LCONSOLE_ERROR("Can't determine server name\n");
1559 /* Determine osd type */
1560 if (!lsi->lsi_lmd->lmd_osd_type) {
1561 osd_type = LUSTRE_OSD_LDISKFS_NAME;
1564 osd_type = lsi->lsi_lmd->lmd_osd_type;
1565 fstype = lsi->lsi_lmd->lmd_osd_type;
1568 if (strlen(lsi->lsi_lmd->lmd_profile) >= sizeof(lsi->lsi_svname) ||
1569 strlen(osd_type) >= sizeof(lsi->lsi_osd_type) ||
1570 strlen(fstype) >= sizeof(lsi->lsi_fstype))
1571 RETURN(-ENAMETOOLONG);
1573 strlcpy(lsi->lsi_svname, lsi->lsi_lmd->lmd_profile,
1574 sizeof(lsi->lsi_svname));
1575 strlcpy(lsi->lsi_osd_type, osd_type, sizeof(lsi->lsi_osd_type));
1576 /* XXX: a temp. solution for components using ldiskfs
1577 * to be removed in one of the subsequent patches
1579 strlcpy(lsi->lsi_fstype, fstype, sizeof(lsi->lsi_fstype));
1581 /* Determine server type */
1582 rc = server_name2index(lsi->lsi_svname, &index, NULL);
1584 if (test_bit(LMD_FLG_MGS, lsi->lsi_lmd->lmd_flags)) {
1585 /* Assume we're a bare MGS */
1587 set_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags);
1589 LCONSOLE_ERROR("Can't determine server type of '%s'\n",
1594 lsi->lsi_flags |= rc;
1596 /* Add mount line flags that used to be in ldd:
1597 * writeconf, mgs, anything else?
1599 lsi->lsi_flags |= test_bit(LMD_FLG_WRITECONF, lsi->lsi_lmd->lmd_flags) ?
1600 LDD_F_WRITECONF : 0;
1601 lsi->lsi_flags |= test_bit(LMD_FLG_NO_LOCAL_LOGS, lsi->lsi_lmd->lmd_flags) ?
1602 LDD_F_NO_LOCAL_LOGS : 0;
1603 lsi->lsi_flags |= test_bit(LMD_FLG_VIRGIN, lsi->lsi_lmd->lmd_flags) ?
1605 lsi->lsi_flags |= test_bit(LMD_FLG_UPDATE, lsi->lsi_lmd->lmd_flags) ?
1607 lsi->lsi_flags |= test_bit(LMD_FLG_MGS, lsi->lsi_lmd->lmd_flags) ?
1608 LDD_F_SV_TYPE_MGS : 0;
1609 lsi->lsi_flags |= test_bit(LMD_FLG_NO_PRIMNODE, lsi->lsi_lmd->lmd_flags) ?
1610 LDD_F_NO_PRIMNODE : 0;
1615 /*************** server mount ******************/
1617 /** Start the shutdown of servers at umount.
1619 static void server_put_super(struct super_block *sb)
1621 struct lustre_sb_info *lsi = s2lsi(sb);
1622 struct obd_device *obd;
1623 char *tmpname, *extraname = NULL;
1625 int lsiflags = lsi->lsi_flags;
1626 bool stop_servers = lsi->lsi_server_started;
1629 LASSERT(IS_SERVER(lsi));
1631 tmpname_sz = strlen(lsi->lsi_svname) + 1;
1632 OBD_ALLOC(tmpname, tmpname_sz);
1633 memcpy(tmpname, lsi->lsi_svname, tmpname_sz);
1634 CDEBUG(D_MOUNT, "server put_super %s\n", tmpname);
1635 if (IS_MDT(lsi) && test_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags))
1636 snprintf(tmpname, tmpname_sz, "MGS");
1638 /* disconnect the lwp first to drain off the inflight request */
1639 if (IS_OST(lsi) || IS_MDT(lsi)) {
1642 rc = lustre_disconnect_lwp(sb);
1643 if (rc != 0 && rc != -ETIMEDOUT && rc != -ENODEV &&
1644 rc != -ENOTCONN && rc != -ESHUTDOWN)
1645 CWARN("%s: failed to disconnect lwp: rc= %d\n",
1649 /* Stop the target */
1650 if (!test_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags) &&
1651 (IS_MDT(lsi) || IS_OST(lsi))) {
1652 struct lustre_profile *lprof = NULL;
1654 /* tell the mgc to drop the config log */
1655 lustre_end_log(sb, lsi->lsi_svname, NULL);
1657 /* COMPAT_146 - profile may get deleted in mgc_cleanup.
1658 * If there are any setup/cleanup errors, save the lov
1659 * name for safety cleanup later.
1661 lprof = class_get_profile(lsi->lsi_svname);
1664 OBD_ALLOC(extraname, strlen(lprof->lp_dt) + 1);
1665 strncpy(extraname, lprof->lp_dt,
1666 strlen(lprof->lp_dt) + 1);
1668 class_put_profile(lprof);
1671 obd = class_name2obd(lsi->lsi_svname);
1673 CDEBUG(D_MOUNT, "stopping %s\n", obd->obd_name);
1674 if (lsiflags & LSI_UMOUNT_FAILOVER)
1676 /* We can't seem to give an error return code
1677 * to .put_super, so we better make sure we clean up!
1680 class_manual_cleanup(obd);
1681 if (CFS_FAIL_PRECHECK(OBD_FAIL_OBD_STOP_MDS_RACE)) {
1684 server_name2index(lsi->lsi_svname, &idx, NULL);
1685 /* sleeping for MDT0001 */
1687 CFS_RACE(OBD_FAIL_OBD_STOP_MDS_RACE);
1690 CERROR("no obd %s\n", lsi->lsi_svname);
1691 server_deregister_mount(lsi->lsi_svname);
1695 /* If they wanted the mgs to stop separately from the mdt, they
1696 * should have put it on a different device.
1698 lustre_stop_mgc(sb);
1700 /* if MDS start with --nomgs, don't stop MGS then */
1701 if (!test_bit(LMD_FLG_NOMGS, lsi->lsi_lmd->lmd_flags))
1702 server_stop_mgs(sb);
1705 if (IS_OST(lsi) || IS_MDT(lsi)) {
1706 if (lustre_stop_lwp(sb) < 0)
1707 CERROR("%s: failed to stop lwp!\n", tmpname);
1710 /* Drop a ref to the mounted disk */
1713 /* wait till all in-progress cleanups are done
1714 * specifically we're interested in ofd cleanup
1717 obd_zombie_barrier();
1719 /* Stop the servers (MDS, OSS) if no longer needed. We must wait
1720 * until the target is really gone so that our type refcount check
1724 server_stop_servers(lsiflags);
1726 /* In case of startup or cleanup err, stop related obds */
1728 obd = class_name2obd(extraname);
1730 CWARN("Cleaning orphaned obd %s\n", extraname);
1732 class_manual_cleanup(obd);
1734 OBD_FREE(extraname, strlen(extraname) + 1);
1737 LCONSOLE(D_WARNING, "server umount %s complete\n", tmpname);
1738 OBD_FREE(tmpname, tmpname_sz);
1742 /* Called only for 'umount -f' */
1743 static void server_umount_begin(struct super_block *sb)
1745 struct lustre_sb_info *lsi = s2lsi(sb);
1748 CDEBUG(D_MOUNT, "umount -f\n");
1749 /* umount = failover
1751 * no third way to do non-force, non-failover
1753 lsi->lsi_flags &= ~LSI_UMOUNT_FAILOVER;
1757 static int server_statfs(struct dentry *dentry, struct kstatfs *buf)
1759 struct super_block *sb = dentry->d_sb;
1760 struct lustre_sb_info *lsi = s2lsi(sb);
1761 struct obd_statfs statfs;
1765 if (lsi->lsi_dt_dev) {
1766 rc = dt_statfs(NULL, lsi->lsi_dt_dev, &statfs);
1768 statfs_unpack(buf, &statfs);
1769 buf->f_type = sb->s_magic;
1775 buf->f_type = sb->s_magic;
1776 buf->f_bsize = sb->s_blocksize;
1782 buf->f_namelen = NAME_MAX;
1786 static int server_show_options(struct seq_file *seq, struct dentry *dentry)
1788 struct lustre_sb_info *lsi;
1789 struct lustre_mount_data *lmd;
1790 struct obd_statfs osfs;
1791 struct super_block *sb;
1794 LASSERT(seq && dentry);
1795 lsi = s2lsi(dentry->d_sb);
1799 if (lsi->lsi_dt_dev) {
1800 rc = dt_statfs(NULL, lsi->lsi_dt_dev, &osfs);
1802 /* Check FS State for OS_STATFS_READONLY
1803 * (Read only) flag. If it is not set then
1804 * toggle back the s_flag's SB_RDONLY bit.
1805 * The SB_RDONLY bit is always set for OST/MDT
1806 * during server prep (server_fill_super_common())
1809 * Also, if server is mounted with "rdonly_dev"
1810 * (LMD_FLG_DEV_RDONLY) then force flag to be 'ro'
1812 if (!test_bit(LMD_FLG_DEV_RDONLY, lmd->lmd_flags) &&
1813 !(osfs.os_state & OS_STATFS_READONLY))
1814 sb->s_flags &= ~SB_RDONLY;
1818 seq_printf(seq, ",svname=%s", lmd->lmd_profile);
1820 if (test_bit(LMD_FLG_ABORT_RECOV, lmd->lmd_flags))
1821 seq_puts(seq, ",abort_recov");
1823 if (test_bit(LMD_FLG_NOIR, lmd->lmd_flags))
1824 seq_puts(seq, ",noir");
1826 if (test_bit(LMD_FLG_NOSVC, lmd->lmd_flags))
1827 seq_puts(seq, ",nosvc");
1829 if (test_bit(LMD_FLG_NOMGS, lmd->lmd_flags))
1830 seq_puts(seq, ",nomgs");
1832 if (test_bit(LMD_FLG_NOSCRUB, lmd->lmd_flags))
1833 seq_puts(seq, ",noscrub");
1835 if (test_bit(LMD_FLG_SKIP_LFSCK, lmd->lmd_flags))
1836 seq_puts(seq, ",skip_lfsck");
1838 if (test_bit(LMD_FLG_DEV_RDONLY, lmd->lmd_flags))
1839 seq_puts(seq, ",rdonly_dev");
1841 if (test_bit(LMD_FLG_MGS, lmd->lmd_flags))
1842 seq_puts(seq, ",mgs");
1845 seq_printf(seq, ",mgsnode=%s", lmd->lmd_mgs);
1847 if (lmd->lmd_osd_type)
1848 seq_printf(seq, ",osd=%s", lmd->lmd_osd_type);
1850 if (lmd->lmd_opts) {
1852 seq_puts(seq, lmd->lmd_opts);
1858 /** The operations we support directly on the superblock:
1859 * mount, umount, and df.
1861 static const struct super_operations server_ops = {
1862 .put_super = server_put_super,
1863 .umount_begin = server_umount_begin, /* umount -f */
1864 .statfs = server_statfs,
1865 .show_options = server_show_options,
1868 #if defined(HAVE_USER_NAMESPACE_ARG)
1869 # define USERNS_ARG mnt_userns,
1872 # ifdef HAVE_INODEOPS_ENHANCED_GETATTR
1873 # define server_getattr(ns, path, st, rq, fl) server_getattr(path, st, rq, fl)
1878 * inode operations for Lustre server mountpoints
1880 #if defined(HAVE_USER_NAMESPACE_ARG) || defined(HAVE_INODEOPS_ENHANCED_GETATTR)
1881 static int server_getattr(struct user_namespace *mnt_userns,
1882 const struct path *path, struct kstat *stat,
1883 u32 request_mask, unsigned int flags)
1885 struct inode *inode = d_inode(path->dentry);
1887 static int server_getattr(struct vfsmount *mnt, struct dentry *de,
1890 struct inode *inode = de->d_inode;
1892 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
1893 struct vfsmount *root_mnt;
1894 struct inode *root_inode;
1896 root_mnt = dt_mnt_get(lsi->lsi_dt_dev);
1897 if (IS_ERR(root_mnt))
1898 root_inode = igrab(inode);
1900 root_inode = igrab(root_mnt->mnt_sb->s_root->d_inode);
1904 CDEBUG(D_SUPER, "%s: root_inode from %s ino=%lu, dev=%x\n",
1905 lsi->lsi_svname, root_inode == inode ? "lsi" : "vfsmnt",
1906 root_inode->i_ino, root_inode->i_rdev);
1907 generic_fillattr(USERNS_ARG root_inode, stat);
1913 #ifdef HAVE_IOP_XATTR
1914 static ssize_t server_getxattr(struct dentry *dentry, const char *name,
1915 void *buffer, size_t size)
1917 if (!selinux_is_enabled())
1922 static int server_setxattr(struct dentry *dentry, const char *name,
1923 const void *value, size_t size, int flags)
1929 static ssize_t server_listxattr(struct dentry *d_entry, char *name,
1935 static bool is_cmd_supported(unsigned int cmd)
1937 CDEBUG(D_SUPER, "ioctl cmd=%x\n", cmd);
1942 case LL_IOC_RESIZE_FS:
1951 static long server_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1953 struct lustre_sb_info *lsi = s2lsi(file_inode(filp)->i_sb);
1954 struct vfsmount *root_mnt;
1955 struct file *root_filp;
1956 struct inode *root_inode;
1959 if (!is_cmd_supported(cmd))
1962 root_mnt = dt_mnt_get(lsi->lsi_dt_dev);
1963 if (IS_ERR(root_mnt))
1966 root_inode = igrab(root_mnt->mnt_root->d_inode);
1970 root_filp = alloc_file_pseudo(root_inode, root_mnt, "/",
1971 O_RDWR | O_NOATIME, root_inode->i_fop);
1972 if (root_inode->i_fop && root_inode->i_fop->unlocked_ioctl)
1973 err = root_inode->i_fop->unlocked_ioctl(root_filp, cmd, arg);
1979 static const struct inode_operations server_inode_operations = {
1980 .getattr = server_getattr,
1981 #ifdef HAVE_IOP_XATTR
1982 .setxattr = server_setxattr,
1983 .getxattr = server_getxattr,
1985 .listxattr = server_listxattr,
1988 static const struct file_operations server_file_operations = {
1989 .unlocked_ioctl = server_ioctl,
1992 #define log2(n) ffz(~(n))
1993 #define LUSTRE_SUPER_MAGIC 0x0BD00BD1
1995 static int server_fill_super_common(struct super_block *sb)
1997 struct inode *root = NULL;
2000 CDEBUG(D_MOUNT, "Server sb, dev=%d\n", (int)sb->s_dev);
2002 sb->s_blocksize = 4096;
2003 sb->s_blocksize_bits = log2(sb->s_blocksize);
2004 sb->s_magic = LUSTRE_SUPER_MAGIC;
2005 sb->s_maxbytes = 0; /* we don't allow file IO on server mountpoints */
2006 sb->s_flags |= SB_RDONLY;
2007 sb->s_op = &server_ops;
2009 root = new_inode(sb);
2011 CERROR("Can't make root inode\n");
2015 /* returns -EIO for every operation */
2016 /* make_bad_inode(root); -- badness - can't umount */
2017 /* apparently we need to be a directory for the mount to finish */
2018 root->i_mode = S_IFDIR;
2019 root->i_op = &server_inode_operations;
2020 root->i_fop = &server_file_operations;
2021 sb->s_root = d_make_root(root);
2023 CERROR("%s: can't make root dentry\n", sb->s_id);
2030 static int osd_start(struct lustre_sb_info *lsi, unsigned long mflags)
2032 struct lustre_mount_data *lmd = lsi->lsi_lmd;
2033 struct obd_device *obd;
2034 struct dt_device_param p;
2035 char flagstr[20 + 1 + 10 + 1];
2041 "Attempting to start %s, type=%s, lsifl=%x, mountfl=%lx\n",
2042 lsi->lsi_svname, lsi->lsi_osd_type, lsi->lsi_flags, mflags);
2044 sprintf(lsi->lsi_osd_obdname, "%s-osd", lsi->lsi_svname);
2045 strcpy(lsi->lsi_osd_uuid, lsi->lsi_osd_obdname);
2046 strcat(lsi->lsi_osd_uuid, "_UUID");
2047 bitmap_to_arr32(&lmd_flags, lmd->lmd_flags, LMD_FLG_NUM_FLAGS);
2048 snprintf(flagstr, sizeof(flagstr), "%lu:%u", mflags, lmd_flags);
2050 obd = class_name2obd(lsi->lsi_osd_obdname);
2052 rc = lustre_start_simple(lsi->lsi_osd_obdname,
2054 lsi->lsi_osd_uuid, lmd->lmd_dev,
2055 flagstr, lsi->lsi_lmd->lmd_opts,
2059 obd = class_name2obd(lsi->lsi_osd_obdname);
2062 CDEBUG(D_MOUNT, "%s already started\n", lsi->lsi_osd_obdname);
2063 /* but continue setup to allow special case of MDT and internal
2064 * MGT being started separately.
2066 if (!((IS_MGS(lsi) &&
2067 test_bit(LMD_FLG_NOMGS, lsi->lsi_lmd->lmd_flags)) ||
2069 test_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags))))
2073 rc = obd_connect(NULL, &lsi->lsi_osd_exp,
2074 obd, &obd->obd_uuid, NULL, NULL);
2078 class_manual_cleanup(obd);
2079 lsi->lsi_dt_dev = NULL;
2083 LASSERT(obd->obd_lu_dev);
2084 lu_device_get(obd->obd_lu_dev);
2085 lsi->lsi_dt_dev = lu2dt_dev(obd->obd_lu_dev);
2086 LASSERT(lsi->lsi_dt_dev);
2088 /* set disk context for llog usage */
2089 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
2090 obd->obd_lvfs_ctxt.dt = lsi->lsi_dt_dev;
2092 dt_conf_get(NULL, lsi->lsi_dt_dev, &p);
2097 /** Fill in the superblock info for a Lustre server.
2098 * Mount the device with the correct options.
2099 * Read the on-disk config file.
2100 * Start the services.
2102 int server_fill_super(struct super_block *sb)
2104 struct lustre_sb_info *lsi = s2lsi(sb);
2108 /* to simulate target mount race */
2109 CFS_RACE(OBD_FAIL_TGT_MOUNT_RACE);
2111 rc = lsi_prepare(lsi);
2117 /* Start low level OSD */
2118 rc = osd_start(lsi, sb->s_flags);
2120 CERROR("Unable to start osd on %s: %d\n",
2121 lsi->lsi_lmd->lmd_dev, rc);
2126 CDEBUG(D_MOUNT, "Found service %s on device %s\n",
2127 lsi->lsi_svname, lsi->lsi_lmd->lmd_dev);
2129 if (class_name2obd(lsi->lsi_svname)) {
2130 LCONSOLE_ERROR_MSG(0x161,
2131 "The target named %s is already running. Double-mount may have compromised the disk journal.\n",
2137 /* Start MGS before MGC */
2138 if (IS_MGS(lsi) && !test_bit(LMD_FLG_NOMGS, lsi->lsi_lmd->lmd_flags)) {
2139 rc = server_start_mgs(sb);
2144 /* Start MGC before servers */
2145 rc = lustre_start_mgc(sb);
2149 /* Set up all obd devices for service */
2150 if (!test_bit(LMD_FLG_NOSVC, lsi->lsi_lmd->lmd_flags) &&
2151 (IS_OST(lsi) || IS_MDT(lsi))) {
2152 rc = server_start_targets(sb);
2154 CERROR("Unable to start targets: %d\n", rc);
2157 /* FIXME overmount client here, or can we just start a
2158 * client log and client_fill_super on this sb? We
2159 * need to make sure server_put_super gets called too
2160 * - ll_put_super calls lustre_common_put_super; check
2161 * there for LSI_SERVER flag, call s_p_s if so.
2163 * Probably should start client from new thread so we
2164 * can return. Client will not finish until all
2165 * servers are connected. Note - MGS-only server does
2166 * NOT get a client, since there is no lustre fs
2167 * associated - the MGS is for all lustre fs's
2171 rc = server_fill_super_common(sb);
2177 /* We jump here in case of failure while starting targets or MGS.
2178 * In this case we can't just put @mnt and have to do real cleanup
2179 * with stoping targets, etc.
2181 server_put_super(sb);
2184 EXPORT_SYMBOL(server_fill_super);
2187 * Calculate timeout value for a target.
2189 void server_calc_timeout(struct lustre_sb_info *lsi, struct obd_device *obd)
2191 struct lustre_mount_data *lmd;
2195 bool has_ir = !!(lsi->lsi_flags & LDD_F_IR_CAPABLE);
2196 int min = OBD_RECOVERY_TIME_MIN;
2198 LASSERT(IS_SERVER(lsi));
2202 soft = lmd->lmd_recovery_time_soft;
2203 hard = lmd->lmd_recovery_time_hard;
2204 has_ir = has_ir && !test_bit(LMD_FLG_NOIR, lmd->lmd_flags);
2205 obd->obd_no_ir = !has_ir;
2209 soft = OBD_RECOVERY_TIME_SOFT;
2211 hard = OBD_RECOVERY_TIME_HARD;
2213 /* target may have ir_factor configured. */
2214 factor = OBD_IR_FACTOR_DEFAULT;
2215 if (obd->obd_recovery_ir_factor)
2216 factor = obd->obd_recovery_ir_factor;
2219 int new_soft = soft;
2221 /* adjust timeout value by imperative recovery */
2222 new_soft = (soft * factor) / OBD_IR_FACTOR_MAX;
2223 /* make sure the timeout is not too short */
2224 new_soft = max(min, new_soft);
2226 LCONSOLE_INFO("%s: Imperative Recovery enabled, recovery window shrunk from %d-%d down to %d-%d\n",
2227 obd->obd_name, soft, hard, new_soft, hard);
2231 LCONSOLE_INFO("%s: Imperative Recovery not enabled, recovery window %d-%d\n",
2232 obd->obd_name, soft, hard);
2236 obd->obd_recovery_timeout = max_t(time64_t, obd->obd_recovery_timeout,
2238 obd->obd_recovery_time_hard = hard;
2239 obd->obd_recovery_ir_factor = factor;
2243 * This is the entry point for the mount call into Lustre.
2244 * This is called when a server target is mounted,
2245 * and this is where we start setting things up.
2246 * @param data Mount options (e.g. -o flock,abort_recov)
2248 static int lustre_tgt_fill_super(struct super_block *sb, void *lmd2_data,
2251 struct lustre_mount_data *lmd;
2252 struct lustre_sb_info *lsi;
2256 CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
2258 lsi = lustre_init_lsi(sb);
2264 * Disable lockdep during mount, because mount locking patterns are
2270 * LU-639: the OBD cleanup of last mount may not finish yet, wait here.
2272 obd_zombie_barrier();
2274 /* Figure out the lmd from the mount options */
2275 if (lmd_parse(lmd2_data, lmd)) {
2277 GOTO(out, rc = -EINVAL);
2280 if (lmd_is_client(lmd)) {
2282 CERROR("%s: attempting to mount a client with -t lustre_tgt' which is only for server-side mounts: rc = %d\n",
2288 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
2289 rc = server_fill_super(sb);
2291 * server_fill_super calls lustre_start_mgc after the mount
2292 * because we need the MGS NIDs which are stored on disk.
2293 * Plus, we may need to start the MGS first.
2295 * server_fill_super will call server_put_super on failure
2297 * If error happens in fill_super() call, @lsi will be killed there.
2298 * This is why we do not put it here.
2302 CERROR("Unable to mount %s (%d)\n",
2303 s2lsi(sb) ? lmd->lmd_dev : "", rc);
2305 CDEBUG(D_SUPER, "Mount %s complete\n",
2312 /***************** FS registration ******************/
2313 static struct dentry *lustre_tgt_mount(struct file_system_type *fs_type,
2314 int flags, const char *devname,
2317 return mount_nodev(fs_type, flags, data, lustre_tgt_fill_super);
2320 /* Register the "lustre_tgt" fs type.
2322 * Right now this isn't any different than the normal "lustre" filesystem
2323 * type, but it is added so that there is some compatibility to allow
2324 * changing documentation and scripts to start using the "lustre_tgt" type
2325 * at mount time. That will simplify test interop, and in case of upgrades
2326 * that change to the new type and then need to roll back for some reason.
2328 * The long-term goal is to disentangle the client and server mount code.
2330 static struct file_system_type lustre_tgt_fstype = {
2331 .owner = THIS_MODULE,
2332 .name = "lustre_tgt",
2333 .mount = lustre_tgt_mount,
2334 .kill_sb = kill_anon_super,
2335 .fs_flags = FS_REQUIRES_DEV | FS_RENAME_DOES_D_MOVE,
2337 MODULE_ALIAS_FS("lustre_tgt");
2339 int lustre_tgt_register_fs(void)
2341 return register_filesystem(&lustre_tgt_fstype);
2344 void lustre_tgt_unregister_fs(void)
2346 unregister_filesystem(&lustre_tgt_fstype);