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) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/obd_mount.c
34 * Client mount routines
36 * Author: Nathan Rutman <nathan@clusterfs.com>
40 #define DEBUG_SUBSYSTEM S_CLASS
41 #define D_MOUNT (D_SUPER|D_CONFIG/*|D_WARNING */)
42 #define PRINT_CMD CDEBUG
45 #include <obd_class.h>
46 #include <lustre/lustre_user.h>
47 #include <linux/version.h>
48 #include <lustre_log.h>
49 #include <lustre_disk.h>
50 #include <lustre_param.h>
52 static int (*client_fill_super)(struct super_block *sb,
53 struct vfsmount *mnt);
55 static void (*kill_super_cb)(struct super_block *sb);
57 /**************** config llog ********************/
59 /** Get a config log from the MGS and process it.
60 * This func is called for both clients and servers.
61 * Continue to process new statements appended to the logs
62 * (whenever the config lock is revoked) until lustre_end_log
64 * @param sb The superblock is used by the MGC to write to the local copy of
66 * @param logname The name of the llog to replicate from the MGS
67 * @param cfg Since the same mgc may be used to follow multiple config logs
68 * (e.g. ost1, ost2, client), the config_llog_instance keeps the state for
69 * this log, and is added to the mgc's list of logs to follow.
71 int lustre_process_log(struct super_block *sb, char *logname,
72 struct config_llog_instance *cfg)
74 struct lustre_cfg *lcfg;
75 struct lustre_cfg_bufs *bufs;
76 struct lustre_sb_info *lsi = s2lsi(sb);
77 struct obd_device *mgc = lsi->lsi_mgc;
88 /* mgc_process_config */
89 lustre_cfg_bufs_reset(bufs, mgc->obd_name);
90 lustre_cfg_bufs_set_string(bufs, 1, logname);
91 lustre_cfg_bufs_set(bufs, 2, cfg, sizeof(*cfg));
92 lustre_cfg_bufs_set(bufs, 3, &sb, sizeof(sb));
93 lcfg = lustre_cfg_new(LCFG_LOG_START, bufs);
95 GOTO(out, rc = -ENOMEM);
96 rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
97 lustre_cfg_free(lcfg);
102 LCONSOLE_ERROR_MSG(0x15b, "%s: The configuration from log '%s'"
103 "failed from the MGS (%d). Make sure this "
104 "client and the MGS are running compatible "
105 "versions of Lustre.\n",
106 mgc->obd_name, logname, rc);
108 LCONSOLE_ERROR_MSG(0x15c, "%s: The configuration from log '%s' "
109 "failed (%d). This may be the result of "
110 "communication errors between this node and "
111 "the MGS, a bad configuration, or other "
112 "errors. See the syslog for more "
113 "information.\n", mgc->obd_name, logname,
116 /* class_obd_list(); */
119 EXPORT_SYMBOL(lustre_process_log);
121 /* Stop watching this config log for updates */
122 int lustre_end_log(struct super_block *sb, char *logname,
123 struct config_llog_instance *cfg)
125 struct lustre_cfg *lcfg;
126 struct lustre_cfg_bufs bufs;
127 struct lustre_sb_info *lsi = s2lsi(sb);
128 struct obd_device *mgc = lsi->lsi_mgc;
135 /* mgc_process_config */
136 lustre_cfg_bufs_reset(&bufs, mgc->obd_name);
137 lustre_cfg_bufs_set_string(&bufs, 1, logname);
139 lustre_cfg_bufs_set(&bufs, 2, cfg, sizeof(*cfg));
140 lcfg = lustre_cfg_new(LCFG_LOG_END, &bufs);
143 rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
144 lustre_cfg_free(lcfg);
147 EXPORT_SYMBOL(lustre_end_log);
149 /**************** obd start *******************/
151 /** lustre_cfg_bufs are a holdover from 1.4; we can still set these up from
152 * lctl (and do for echo cli/srv.
154 int do_lcfg(char *cfgname, lnet_nid_t nid, int cmd,
155 char *s1, char *s2, char *s3, char *s4)
157 struct lustre_cfg_bufs bufs;
158 struct lustre_cfg * lcfg = NULL;
161 CDEBUG(D_TRACE, "lcfg %s %#x %s %s %s %s\n", cfgname,
162 cmd, s1, s2, s3, s4);
164 lustre_cfg_bufs_reset(&bufs, cfgname);
166 lustre_cfg_bufs_set_string(&bufs, 1, s1);
168 lustre_cfg_bufs_set_string(&bufs, 2, s2);
170 lustre_cfg_bufs_set_string(&bufs, 3, s3);
172 lustre_cfg_bufs_set_string(&bufs, 4, s4);
174 lcfg = lustre_cfg_new(cmd, &bufs);
177 lcfg->lcfg_nid = nid;
178 rc = class_process_config(lcfg);
179 lustre_cfg_free(lcfg);
183 /** Call class_attach and class_setup. These methods in turn call
184 * obd type-specific methods.
186 int lustre_start_simple(char *obdname, char *type, char *uuid,
187 char *s1, char *s2, char *s3, char *s4)
190 CDEBUG(D_MOUNT, "Starting obd %s (typ=%s)\n", obdname, type);
192 rc = do_lcfg(obdname, 0, LCFG_ATTACH, type, uuid, NULL, NULL);
194 CERROR("%s attach error %d\n", obdname, rc);
197 rc = do_lcfg(obdname, 0, LCFG_SETUP, s1, s2, s3, s4);
199 CERROR("%s setup error %d\n", obdname, rc);
200 do_lcfg(obdname, 0, LCFG_DETACH, NULL, NULL, NULL, NULL);
205 static DEFINE_MUTEX(mgc_start_lock);
207 /** Set up a mgc obd to process startup logs
209 * \param sb [in] super block of the mgc obd
211 * \retval 0 success, otherwise error code
213 int lustre_start_mgc(struct super_block *sb)
215 struct obd_connect_data *data = NULL;
216 struct lustre_sb_info *lsi = s2lsi(sb);
217 struct obd_device *obd;
218 struct obd_export *exp;
219 struct obd_uuid *uuid;
222 char nidstr[LNET_NIDSTR_SIZE];
223 char *mgcname = NULL, *niduuid = NULL, *mgssec = NULL;
225 int rc = 0, i = 0, j;
229 LASSERT(lsi->lsi_lmd);
231 /* Find the first non-lo MGS nid for our MGC name */
232 if (IS_SERVER(lsi)) {
233 /* mount -o mgsnode=nid */
234 ptr = lsi->lsi_lmd->lmd_mgs;
235 if (lsi->lsi_lmd->lmd_mgs &&
236 (class_parse_nid(lsi->lsi_lmd->lmd_mgs, &nid, &ptr) == 0)) {
238 } else if (IS_MGS(lsi)) {
239 struct lnet_process_id id;
241 while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
242 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
249 } else { /* client */
250 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
251 ptr = lsi->lsi_lmd->lmd_dev;
252 if (class_parse_nid(ptr, &nid, &ptr) == 0)
256 CERROR("No valid MGS nids found.\n");
260 mutex_lock(&mgc_start_lock);
262 libcfs_nid2str_r(nid, nidstr, sizeof(nidstr));
263 len = strlen(LUSTRE_MGC_OBDNAME) + strlen(nidstr) + 1;
264 OBD_ALLOC(mgcname, len);
265 OBD_ALLOC(niduuid, len + 2);
266 if (mgcname == NULL || niduuid == NULL)
267 GOTO(out_free, rc = -ENOMEM);
268 snprintf(mgcname, len, "%s%s", LUSTRE_MGC_OBDNAME, nidstr);
270 mgssec = lsi->lsi_lmd->lmd_mgssec ? lsi->lsi_lmd->lmd_mgssec : "";
274 GOTO(out_free, rc = -ENOMEM);
276 obd = class_name2obd(mgcname);
277 if (obd && !obd->obd_stopping) {
280 rc = obd_set_info_async(NULL, obd->obd_self_export,
281 strlen(KEY_MGSSEC), KEY_MGSSEC,
282 strlen(mgssec), mgssec, NULL);
286 /* Re-using an existing MGC */
287 atomic_inc(&obd->u.cli.cl_mgc_refcount);
289 /* IR compatibility check, only for clients */
290 if (lmd_is_client(lsi->lsi_lmd)) {
292 int vallen = sizeof(*data);
293 __u32 *flags = &lsi->lsi_lmd->lmd_flags;
295 rc = obd_get_info(NULL, obd->obd_self_export,
296 strlen(KEY_CONN_DATA), KEY_CONN_DATA,
299 has_ir = OCD_HAS_FLAG(data, IMP_RECOV);
300 if (has_ir ^ !(*flags & LMD_FLG_NOIR)) {
301 /* LMD_FLG_NOIR is for test purpose only */
303 "Trying to mount a client with IR setting "
304 "not compatible with current mgc. "
305 "Force to use current mgc setting that is "
307 has_ir ? "enabled" : "disabled");
309 *flags &= ~LMD_FLG_NOIR;
311 *flags |= LMD_FLG_NOIR;
316 /* If we are restarting the MGS, don't try to keep the MGC's
317 old connection, or registration will fail. */
319 CDEBUG(D_MOUNT, "New MGS with live MGC\n");
323 /* Try all connections, but only once (again).
324 We don't want to block another target from starting
325 (using its local copy of the log), but we do want to connect
326 if at all possible. */
328 CDEBUG(D_MOUNT, "%s: Set MGC reconnect %d\n", mgcname,recov_bk);
329 rc = obd_set_info_async(NULL, obd->obd_self_export,
330 sizeof(KEY_INIT_RECOV_BACKUP),
331 KEY_INIT_RECOV_BACKUP,
332 sizeof(recov_bk), &recov_bk, NULL);
336 CDEBUG(D_MOUNT, "Start MGC '%s'\n", mgcname);
338 /* Add the primary nids for the MGS */
340 snprintf(niduuid, len + 2, "%s_%x", mgcname, i);
341 if (IS_SERVER(lsi)) {
342 ptr = lsi->lsi_lmd->lmd_mgs;
343 CDEBUG(D_MOUNT, "mgs nids %s.\n", ptr);
345 /* Use local nids (including LO) */
346 struct lnet_process_id id;
348 while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
349 rc = do_lcfg(mgcname, id.nid, LCFG_ADD_UUID,
350 niduuid, NULL, NULL, NULL);
353 /* Use mgsnode= nids */
354 /* mount -o mgsnode=nid */
355 if (lsi->lsi_lmd->lmd_mgs) {
356 ptr = lsi->lsi_lmd->lmd_mgs;
357 } else if (class_find_param(ptr, PARAM_MGSNODE,
359 CERROR("No MGS nids given.\n");
360 GOTO(out_free, rc = -EINVAL);
364 * Here we only take the first mgsnid as its primary
365 * serving mgs node, the rest mgsnid will be taken as
366 * failover mgs node, otherwise they would be takens
367 * as multiple nids of a single mgs node.
369 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
370 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
371 niduuid, NULL, NULL, NULL);
378 } else { /* client */
379 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
380 ptr = lsi->lsi_lmd->lmd_dev;
381 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
382 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
383 niduuid, NULL, NULL, NULL);
386 /* Stop at the first failover nid */
392 CERROR("No valid MGS nids found.\n");
393 GOTO(out_free, rc = -EINVAL);
395 lsi->lsi_lmd->lmd_mgs_failnodes = 1;
397 /* Random uuid for MGC allows easier reconnects */
400 GOTO(out_free, rc = -ENOMEM);
402 ll_generate_random_uuid(uuidc);
403 class_uuid_unparse(uuidc, uuid);
406 rc = lustre_start_simple(mgcname, LUSTRE_MGC_NAME,
407 (char *)uuid->uuid, LUSTRE_MGS_OBDNAME,
408 niduuid, NULL, NULL);
413 /* Add any failover MGS nids */
415 while (ptr && ((*ptr == ':' ||
416 class_find_param(ptr, PARAM_MGSNODE, &ptr) == 0))) {
417 /* New failover node */
418 sprintf(niduuid, "%s_%x", mgcname, i);
420 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
421 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
422 niduuid, NULL, NULL, NULL);
429 rc = do_lcfg(mgcname, 0, LCFG_ADD_CONN,
430 niduuid, NULL, NULL, NULL);
438 lsi->lsi_lmd->lmd_mgs_failnodes = i;
440 obd = class_name2obd(mgcname);
442 CERROR("Can't find mgcobd %s\n", mgcname);
443 GOTO(out_free, rc = -ENOTCONN);
446 rc = obd_set_info_async(NULL, obd->obd_self_export,
447 strlen(KEY_MGSSEC), KEY_MGSSEC,
448 strlen(mgssec), mgssec, NULL);
452 /* Keep a refcount of servers/clients who started with "mount",
453 so we know when we can get rid of the mgc. */
454 atomic_set(&obd->u.cli.cl_mgc_refcount, 1);
456 /* We connect to the MGS at setup, and don't disconnect until cleanup */
457 data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
458 OBD_CONNECT_FULL20 | OBD_CONNECT_IMP_RECOV |
459 OBD_CONNECT_LVB_TYPE |
460 OBD_CONNECT_BULK_MBITS | OBD_CONNECT_BARRIER;
462 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
463 data->ocd_connect_flags |= OBD_CONNECT_MNE_SWAB;
466 if (lmd_is_client(lsi->lsi_lmd) &&
467 lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)
468 data->ocd_connect_flags &= ~OBD_CONNECT_IMP_RECOV;
469 data->ocd_version = LUSTRE_VERSION_CODE;
470 rc = obd_connect(NULL, &exp, obd, &(obd->obd_uuid), data, NULL);
472 CERROR("connect failed %d\n", rc);
476 obd->u.cli.cl_mgc_mgsexp = exp;
479 /* Keep the mgc info in the sb. Note that many lsi's can point
483 mutex_unlock(&mgc_start_lock);
488 OBD_FREE(mgcname, len);
490 OBD_FREE(niduuid, len + 2);
494 static int lustre_stop_mgc(struct super_block *sb)
496 struct lustre_sb_info *lsi = s2lsi(sb);
497 struct obd_device *obd;
498 char *niduuid = NULL, *ptr = NULL;
499 int i, rc = 0, len = 0;
509 mutex_lock(&mgc_start_lock);
510 LASSERT(atomic_read(&obd->u.cli.cl_mgc_refcount) > 0);
511 if (!atomic_dec_and_test(&obd->u.cli.cl_mgc_refcount)) {
512 /* This is not fatal, every client that stops
513 will call in here. */
514 CDEBUG(D_MOUNT, "mgc still has %d references.\n",
515 atomic_read(&obd->u.cli.cl_mgc_refcount));
516 GOTO(out, rc = -EBUSY);
519 /* The MGC has no recoverable data in any case.
520 * force shotdown set in umount_begin */
521 obd->obd_no_recov = 1;
523 if (obd->u.cli.cl_mgc_mgsexp) {
524 /* An error is not fatal, if we are unable to send the
525 disconnect mgs ping evictor cleans up the export */
526 rc = obd_disconnect(obd->u.cli.cl_mgc_mgsexp);
528 CDEBUG(D_MOUNT, "disconnect failed %d\n", rc);
531 /* Save the obdname for cleaning the nid uuids, which are
533 len = strlen(obd->obd_name) + 6;
534 OBD_ALLOC(niduuid, len);
536 strcpy(niduuid, obd->obd_name);
537 ptr = niduuid + strlen(niduuid);
540 rc = class_manual_cleanup(obd);
544 /* Clean the nid uuids */
546 GOTO(out, rc = -ENOMEM);
548 for (i = 0; i < lsi->lsi_lmd->lmd_mgs_failnodes; i++) {
549 sprintf(ptr, "_%x", i);
550 rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_DEL_UUID,
551 niduuid, NULL, NULL, NULL);
553 CERROR("del MDC UUID %s failed: rc = %d\n",
558 OBD_FREE(niduuid, len);
560 /* class_import_put will get rid of the additional connections */
561 mutex_unlock(&mgc_start_lock);
565 /***************** lustre superblock **************/
567 static struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
569 struct lustre_sb_info *lsi;
575 OBD_ALLOC_PTR(lsi->lsi_lmd);
581 lsi->lsi_lmd->lmd_exclude_count = 0;
582 lsi->lsi_lmd->lmd_recovery_time_soft = 0;
583 lsi->lsi_lmd->lmd_recovery_time_hard = 0;
584 s2lsi_nocast(sb) = lsi;
585 /* we take 1 extra ref for our setup */
586 atomic_set(&lsi->lsi_mounts, 1);
588 /* Default umount style */
589 lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
590 INIT_LIST_HEAD(&lsi->lsi_lwp_list);
591 spin_lock_init(&lsi->lsi_lwp_lock);
596 static int lustre_free_lsi(struct super_block *sb)
598 struct lustre_sb_info *lsi = s2lsi(sb);
601 LASSERT(lsi != NULL);
602 CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
604 /* someone didn't call server_put_mount. */
605 LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
607 if (lsi->lsi_lmd != NULL) {
608 if (lsi->lsi_lmd->lmd_dev != NULL)
609 OBD_FREE(lsi->lsi_lmd->lmd_dev,
610 strlen(lsi->lsi_lmd->lmd_dev) + 1);
611 if (lsi->lsi_lmd->lmd_profile != NULL)
612 OBD_FREE(lsi->lsi_lmd->lmd_profile,
613 strlen(lsi->lsi_lmd->lmd_profile) + 1);
614 if (lsi->lsi_lmd->lmd_fileset != NULL)
615 OBD_FREE(lsi->lsi_lmd->lmd_fileset,
616 strlen(lsi->lsi_lmd->lmd_fileset) + 1);
617 if (lsi->lsi_lmd->lmd_mgssec != NULL)
618 OBD_FREE(lsi->lsi_lmd->lmd_mgssec,
619 strlen(lsi->lsi_lmd->lmd_mgssec) + 1);
620 if (lsi->lsi_lmd->lmd_opts != NULL)
621 OBD_FREE(lsi->lsi_lmd->lmd_opts,
622 strlen(lsi->lsi_lmd->lmd_opts) + 1);
623 if (lsi->lsi_lmd->lmd_exclude_count)
624 OBD_FREE(lsi->lsi_lmd->lmd_exclude,
625 sizeof(lsi->lsi_lmd->lmd_exclude[0]) *
626 lsi->lsi_lmd->lmd_exclude_count);
627 if (lsi->lsi_lmd->lmd_mgs != NULL)
628 OBD_FREE(lsi->lsi_lmd->lmd_mgs,
629 strlen(lsi->lsi_lmd->lmd_mgs) + 1);
630 if (lsi->lsi_lmd->lmd_osd_type != NULL)
631 OBD_FREE(lsi->lsi_lmd->lmd_osd_type,
632 strlen(lsi->lsi_lmd->lmd_osd_type) + 1);
633 if (lsi->lsi_lmd->lmd_params != NULL)
634 OBD_FREE(lsi->lsi_lmd->lmd_params, 4096);
635 if (lsi->lsi_lmd->lmd_nidnet != NULL)
636 OBD_FREE(lsi->lsi_lmd->lmd_nidnet,
637 strlen(lsi->lsi_lmd->lmd_nidnet) + 1);
639 OBD_FREE_PTR(lsi->lsi_lmd);
642 LASSERT(lsi->lsi_llsbi == NULL);
644 s2lsi_nocast(sb) = NULL;
649 /* The lsi has one reference for every server that is using the disk -
650 e.g. MDT, MGS, and potentially MGC */
651 int lustre_put_lsi(struct super_block *sb)
653 struct lustre_sb_info *lsi = s2lsi(sb);
656 LASSERT(lsi != NULL);
658 CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
659 if (atomic_dec_and_test(&lsi->lsi_mounts)) {
660 if (IS_SERVER(lsi) && lsi->lsi_osd_exp) {
661 lu_device_put(&lsi->lsi_dt_dev->dd_lu_dev);
662 lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt = NULL;
663 lsi->lsi_dt_dev = NULL;
664 obd_disconnect(lsi->lsi_osd_exp);
665 /* wait till OSD is gone */
666 obd_zombie_barrier();
675 * <FSNAME><SEPARATOR><TYPE><INDEX>
676 * FSNAME is between 1 and 8 characters (inclusive).
677 * Excluded characters are '/' and ':'
678 * SEPARATOR is either ':' or '-'
679 * TYPE: "OST", "MDT", etc.
680 * INDEX: Hex representation of the index
683 /** Get the fsname ("lustre") from the server name ("lustre-OST003F").
684 * @param [in] svname server name including type and index
685 * @param [out] fsname Buffer to copy filesystem name prefix into.
686 * Must have at least 'strlen(fsname) + 1' chars.
687 * @param [out] endptr if endptr isn't NULL it is set to end of fsname
690 int server_name2fsname(const char *svname, char *fsname, const char **endptr)
694 dash = svname + strnlen(svname, 8); /* max fsname length is 8 */
695 for (; dash > svname && *dash != '-' && *dash != ':'; dash--)
700 if (fsname != NULL) {
701 strncpy(fsname, svname, dash - svname);
702 fsname[dash - svname] = '\0';
710 EXPORT_SYMBOL(server_name2fsname);
713 * Get service name (svname) from string
715 * if endptr isn't NULL it is set to end of fsname *
717 int server_name2svname(const char *label, char *svname, const char **endptr,
723 /* We use server_name2fsname() just for parsing */
724 rc = server_name2fsname(label, NULL, &dash);
731 if (strlcpy(svname, dash + 1, svsize) >= svsize)
736 EXPORT_SYMBOL(server_name2svname);
739 * check server name is OST.
741 int server_name_is_ost(const char *svname)
746 /* We use server_name2fsname() just for parsing */
747 rc = server_name2fsname(svname, NULL, &dash);
753 if (strncmp(dash, "OST", 3) == 0)
757 EXPORT_SYMBOL(server_name_is_ost);
760 * Get the index from the target name MDTXXXX/OSTXXXX
761 * rc = server type, or rc < 0 on error
763 int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
765 const char *dash = tgtname;
769 if (strncmp(dash, "MDT", 3) == 0)
770 rc = LDD_F_SV_TYPE_MDT;
771 else if (strncmp(dash, "OST", 3) == 0)
772 rc = LDD_F_SV_TYPE_OST;
778 if (strncmp(dash, "all", 3) == 0) {
781 return rc | LDD_F_SV_ALL;
784 index = simple_strtoul(dash, (char **)endptr, 16);
789 EXPORT_SYMBOL(target_name2index);
791 /* Get the index from the obd name.
794 if endptr isn't NULL it is set to end of name */
795 int server_name2index(const char *svname, __u32 *idx, const char **endptr)
800 /* We use server_name2fsname() just for parsing */
801 rc = server_name2fsname(svname, NULL, &dash);
806 rc = target_name2index(dash, idx, endptr);
810 /* Account for -mdc after index that is possible when specifying mdt */
811 if (endptr != NULL && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
812 sizeof(LUSTRE_MDC_NAME)-1) == 0)
813 *endptr += sizeof(LUSTRE_MDC_NAME);
817 EXPORT_SYMBOL(server_name2index);
819 /*************** mount common betweeen server and client ***************/
822 int lustre_common_put_super(struct super_block *sb)
827 CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
829 /* Drop a ref to the MGC */
830 rc = lustre_stop_mgc(sb);
831 if (rc && (rc != -ENOENT)) {
833 CERROR("Can't stop MGC: %d\n", rc);
836 /* BUSY just means that there's some other obd that
837 needs the mgc. Let him clean it up. */
838 CDEBUG(D_MOUNT, "MGC still in use\n");
840 /* Drop a ref to the mounted disk */
845 EXPORT_SYMBOL(lustre_common_put_super);
847 static void lmd_print(struct lustre_mount_data *lmd)
851 PRINT_CMD(D_MOUNT, " mount data:\n");
852 if (lmd_is_client(lmd))
853 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
854 PRINT_CMD(D_MOUNT, "device: %s\n", lmd->lmd_dev);
855 PRINT_CMD(D_MOUNT, "flags: %x\n", lmd->lmd_flags);
858 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
860 if (lmd->lmd_recovery_time_soft)
861 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
862 lmd->lmd_recovery_time_soft);
864 if (lmd->lmd_recovery_time_hard)
865 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
866 lmd->lmd_recovery_time_hard);
868 for (i = 0; i < lmd->lmd_exclude_count; i++) {
869 PRINT_CMD(D_MOUNT, "exclude %d: OST%04x\n", i,
870 lmd->lmd_exclude[i]);
874 /* Is this server on the exclusion list */
875 int lustre_check_exclusion(struct super_block *sb, char *svname)
877 struct lustre_sb_info *lsi = s2lsi(sb);
878 struct lustre_mount_data *lmd = lsi->lsi_lmd;
883 rc = server_name2index(svname, &index, NULL);
884 if (rc != LDD_F_SV_TYPE_OST)
885 /* Only exclude OSTs */
888 CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
889 index, lmd->lmd_exclude_count, lmd->lmd_dev);
891 for(i = 0; i < lmd->lmd_exclude_count; i++) {
892 if (index == lmd->lmd_exclude[i]) {
893 CWARN("Excluding %s (on exclusion list)\n", svname);
900 /* mount -v -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
901 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
903 const char *s1 = ptr, *s2;
909 /* The shortest an ost name can be is 8 chars: -OST0000.
910 We don't actually know the fsname at this time, so in fact
911 a user could specify any fsname. */
912 devmax = strlen(ptr) / 8 + 1;
914 /* temp storage until we figure out how many we have */
915 OBD_ALLOC(exclude_list, sizeof(index) * devmax);
919 /* we enter this fn pointing at the '=' */
920 while (*s1 && *s1 != ' ' && *s1 != ',') {
922 rc = server_name2index(s1, &index, &s2);
924 CERROR("Can't parse server name '%s': rc = %d\n",
928 if (rc == LDD_F_SV_TYPE_OST)
929 exclude_list[lmd->lmd_exclude_count++] = index;
931 CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
932 (uint)(s2-s1), s1, rc);
934 /* now we are pointing at ':' (next exclude)
935 or ',' (end of excludes) */
936 if (lmd->lmd_exclude_count >= devmax)
939 if (rc >= 0) /* non-err */
942 if (lmd->lmd_exclude_count) {
943 /* permanent, freed in lustre_free_lsi */
944 OBD_ALLOC(lmd->lmd_exclude, sizeof(index) *
945 lmd->lmd_exclude_count);
946 if (lmd->lmd_exclude) {
947 memcpy(lmd->lmd_exclude, exclude_list,
948 sizeof(index) * lmd->lmd_exclude_count);
951 lmd->lmd_exclude_count = 0;
954 OBD_FREE(exclude_list, sizeof(index) * devmax);
958 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
963 if (lmd->lmd_mgssec != NULL) {
964 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
965 lmd->lmd_mgssec = NULL;
968 tail = strchr(ptr, ',');
970 length = strlen(ptr);
974 OBD_ALLOC(lmd->lmd_mgssec, length + 1);
975 if (lmd->lmd_mgssec == NULL)
978 memcpy(lmd->lmd_mgssec, ptr, length);
979 lmd->lmd_mgssec[length] = '\0';
983 static int lmd_parse_network(struct lustre_mount_data *lmd, char *ptr)
988 if (lmd->lmd_nidnet != NULL) {
989 OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
990 lmd->lmd_nidnet = NULL;
993 tail = strchr(ptr, ',');
995 length = strlen(ptr);
999 OBD_ALLOC(lmd->lmd_nidnet, length + 1);
1000 if (lmd->lmd_nidnet == NULL)
1003 memcpy(lmd->lmd_nidnet, ptr, length);
1004 lmd->lmd_nidnet[length] = '\0';
1008 static int lmd_parse_string(char **handle, char *ptr)
1013 if ((handle == NULL) || (ptr == NULL))
1016 if (*handle != NULL) {
1017 OBD_FREE(*handle, strlen(*handle) + 1);
1021 tail = strchr(ptr, ',');
1023 length = strlen(ptr);
1025 length = tail - ptr;
1027 OBD_ALLOC(*handle, length + 1);
1028 if (*handle == NULL)
1031 memcpy(*handle, ptr, length);
1032 (*handle)[length] = '\0';
1037 /* Collect multiple values for mgsnid specifiers */
1038 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
1046 /* Find end of nidlist */
1047 while (class_parse_nid_quiet(tail, &nid, &tail) == 0) {}
1048 length = tail - *ptr;
1050 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
1054 if (lmd->lmd_mgs != NULL)
1055 oldlen = strlen(lmd->lmd_mgs) + 1;
1057 OBD_ALLOC(mgsnid, oldlen + length + 1);
1061 if (lmd->lmd_mgs != NULL) {
1062 /* Multiple mgsnid= are taken to mean failover locations */
1063 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
1064 mgsnid[oldlen - 1] = ':';
1065 OBD_FREE(lmd->lmd_mgs, oldlen);
1067 memcpy(mgsnid + oldlen, *ptr, length);
1068 mgsnid[oldlen + length] = '\0';
1069 lmd->lmd_mgs = mgsnid;
1076 * Find the first delimiter (comma or colon) from the specified \a buf and
1077 * make \a *endh point to the string starting with the delimiter. The commas
1078 * in expression list [...] will be skipped.
1080 * \param[in] buf a delimiter-separated string
1081 * \param[in] endh a pointer to a pointer that will point to the string
1082 * starting with the delimiter
1084 * \retval 0 if delimiter is found
1085 * \retval 1 if delimiter is not found
1087 static int lmd_find_delimiter(char *buf, char **endh)
1095 while (*c != '\0') {
1101 if ((*c == ',' || *c == ':') && skip == 0) {
1114 * Find the first valid string delimited by comma or colon from the specified
1115 * \a buf and parse it to see whether it's a valid nid list. If yes, \a *endh
1116 * will point to the next string starting with the delimiter.
1118 * \param[in] buf a delimiter-separated string
1119 * \param[in] endh a pointer to a pointer that will point to the string
1120 * starting with the delimiter
1122 * \retval 0 if the string is a valid nid list
1123 * \retval 1 if the string is not a valid nid list
1125 static int lmd_parse_nidlist(char *buf, char **endh)
1127 struct list_head nidlist;
1134 while (*buf == ',' || *buf == ':')
1136 if (*buf == ' ' || *buf == '/' || *buf == '\0')
1139 if (lmd_find_delimiter(buf, &endp) != 0)
1140 endp = buf + strlen(buf);
1145 INIT_LIST_HEAD(&nidlist);
1146 if (cfs_parse_nidlist(buf, strlen(buf), &nidlist) <= 0)
1148 cfs_free_nidlist(&nidlist);
1158 /** Parse mount line options
1159 * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
1160 * dev is passed as device=uml1:/lustre by mount.lustre
1162 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
1164 char *s1, *s2, *devname = NULL;
1165 struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1171 LCONSOLE_ERROR_MSG(0x162, "Missing mount data: check that "
1172 "/sbin/mount.lustre is installed.\n");
1176 /* Options should be a string - try to detect old lmd data */
1177 if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1178 LCONSOLE_ERROR_MSG(0x163, "You're using an old version of "
1179 "/sbin/mount.lustre. Please install "
1180 "version %s\n", LUSTRE_VERSION_STRING);
1183 lmd->lmd_magic = LMD_MAGIC;
1185 OBD_ALLOC(lmd->lmd_params, LMD_PARAMS_MAXLEN);
1186 if (lmd->lmd_params == NULL)
1188 lmd->lmd_params[0] = '\0';
1190 /* Set default flags here */
1195 int time_min = OBD_RECOVERY_TIME_MIN;
1198 /* Skip whitespace and extra commas */
1199 while (*s1 == ' ' || *s1 == ',')
1203 /* Client options are parsed in ll_options: eg. flock,
1206 /* Parse non-ldiskfs options here. Rather than modifying
1207 ldiskfs, we just zero these out here */
1208 if (strncmp(s1, "abort_recov", 11) == 0) {
1209 lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
1211 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
1212 lmd->lmd_recovery_time_soft =
1213 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1216 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
1217 lmd->lmd_recovery_time_hard =
1218 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1221 } else if (strncmp(s1, "noir", 4) == 0) {
1222 lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
1224 } else if (strncmp(s1, "nosvc", 5) == 0) {
1225 lmd->lmd_flags |= LMD_FLG_NOSVC;
1227 } else if (strncmp(s1, "nomgs", 5) == 0) {
1228 lmd->lmd_flags |= LMD_FLG_NOMGS;
1230 } else if (strncmp(s1, "noscrub", 7) == 0) {
1231 lmd->lmd_flags |= LMD_FLG_NOSCRUB;
1233 } else if (strncmp(s1, "skip_lfsck", 10) == 0) {
1234 lmd->lmd_flags |= LMD_FLG_SKIP_LFSCK;
1236 } else if (strncmp(s1, PARAM_MGSNODE,
1237 sizeof(PARAM_MGSNODE) - 1) == 0) {
1238 s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
1239 /* Assume the next mount opt is the first
1240 invalid nid we get to. */
1241 rc = lmd_parse_mgs(lmd, &s2);
1246 } else if (strncmp(s1, "writeconf", 9) == 0) {
1247 lmd->lmd_flags |= LMD_FLG_WRITECONF;
1249 } else if (strncmp(s1, "update", 6) == 0) {
1250 lmd->lmd_flags |= LMD_FLG_UPDATE;
1252 } else if (strncmp(s1, "virgin", 6) == 0) {
1253 lmd->lmd_flags |= LMD_FLG_VIRGIN;
1255 } else if (strncmp(s1, "noprimnode", 10) == 0) {
1256 lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
1258 } else if (strncmp(s1, "mgssec=", 7) == 0) {
1259 rc = lmd_parse_mgssec(lmd, s1 + 7);
1263 /* ost exclusion list */
1264 } else if (strncmp(s1, "exclude=", 8) == 0) {
1265 rc = lmd_make_exclusion(lmd, s1 + 7);
1269 } else if (strncmp(s1, "mgs", 3) == 0) {
1271 lmd->lmd_flags |= LMD_FLG_MGS;
1273 } else if (strncmp(s1, "svname=", 7) == 0) {
1274 rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
1278 } else if (strncmp(s1, "param=", 6) == 0) {
1279 size_t length, params_length;
1281 if (lmd_find_delimiter(s1 + 6, &tail) != 0)
1282 length = strlen(s1);
1284 char *param_str = tail + 1;
1285 int supplementary = 1;
1286 while (lmd_parse_nidlist(param_str,
1290 length = param_str - s1 - supplementary;
1293 params_length = strlen(lmd->lmd_params);
1294 if (params_length + length + 1 >= LMD_PARAMS_MAXLEN)
1296 strncat(lmd->lmd_params, s1 + 6, length);
1297 lmd->lmd_params[params_length + length] = '\0';
1298 strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
1299 s3 = s1 + 6 + length;
1301 } else if (strncmp(s1, "osd=", 4) == 0) {
1302 rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
1307 /* Linux 2.4 doesn't pass the device, so we stuck it at the
1308 end of the options. */
1309 else if (strncmp(s1, "device=", 7) == 0) {
1311 /* terminate options right before device. device
1312 must be the last one. */
1315 } else if (strncmp(s1, "network=", 8) == 0) {
1316 rc = lmd_parse_network(lmd, s1 + 8);
1323 s2 = strchr(s3, ',');
1331 memmove(s1, s2, strlen(s2) + 1);
1337 LCONSOLE_ERROR_MSG(0x164, "Can't find the device name "
1338 "(need mount option 'device=...')\n");
1342 s1 = strstr(devname, ":/");
1345 lmd->lmd_flags |= LMD_FLG_CLIENT;
1346 /* Remove leading /s from fsname */
1347 while (*++s1 == '/')
1350 while (*s2 != '/' && *s2 != '\0')
1352 /* Freed in lustre_free_lsi */
1353 OBD_ALLOC(lmd->lmd_profile, s2 - s1 + 8);
1354 if (!lmd->lmd_profile)
1357 strncat(lmd->lmd_profile, s1, s2 - s1);
1358 strncat(lmd->lmd_profile, "-client", 7);
1361 s2 = s1 + strlen(s1) - 1;
1362 /* Remove padding /s from fileset */
1366 OBD_ALLOC(lmd->lmd_fileset, s2 - s1 + 2);
1367 if (lmd->lmd_fileset == NULL) {
1368 OBD_FREE(lmd->lmd_profile,
1369 strlen(lmd->lmd_profile) + 1);
1372 strncat(lmd->lmd_fileset, s1, s2 - s1 + 1);
1376 if (lmd->lmd_nidnet != NULL) {
1377 /* 'network=' mount option forbidden for server */
1378 OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1379 lmd->lmd_nidnet = NULL;
1381 CERROR("%s: option 'network=' not allowed for Lustre "
1382 "servers: rc = %d\n", devname, rc);
1387 /* Freed in lustre_free_lsi */
1388 OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
1391 strncpy(lmd->lmd_dev, devname, strlen(devname)+1);
1393 /* Save mount options */
1394 s1 = options + strlen(options) - 1;
1395 while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1397 if (*options != 0) {
1398 /* Freed in lustre_free_lsi */
1399 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1402 strncpy(lmd->lmd_opts, options, strlen(options)+1);
1406 lmd->lmd_magic = LMD_MAGIC;
1411 CERROR("Bad mount options %s\n", options);
1415 struct lustre_mount_data2 {
1417 struct vfsmount *lmd2_mnt;
1420 /** This is the entry point for the mount call into Lustre.
1421 * This is called when a server or client is mounted,
1422 * and this is where we start setting things up.
1423 * @param data Mount options (e.g. -o flock,abort_recov)
1425 static int lustre_fill_super(struct super_block *sb, void *data, int silent)
1427 struct lustre_mount_data *lmd;
1428 struct lustre_mount_data2 *lmd2 = data;
1429 struct lustre_sb_info *lsi;
1433 CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
1435 lsi = lustre_init_lsi(sb);
1441 * Disable lockdep during mount, because mount locking patterns are
1447 * LU-639: the obd cleanup of last mount may not finish yet, wait here.
1449 obd_zombie_barrier();
1451 /* Figure out the lmd from the mount options */
1452 if (lmd_parse((char *)(lmd2->lmd2_data), lmd)) {
1454 GOTO(out, rc = -EINVAL);
1457 if (lmd_is_client(lmd)) {
1458 CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
1459 if (client_fill_super == NULL)
1460 request_module("lustre");
1461 if (client_fill_super == NULL) {
1462 LCONSOLE_ERROR_MSG(0x165, "Nothing registered for "
1463 "client mount! Is the 'lustre' "
1464 "module loaded?\n");
1468 rc = lustre_start_mgc(sb);
1470 lustre_common_put_super(sb);
1473 /* Connect and start */
1474 /* (should always be ll_fill_super) */
1475 rc = (*client_fill_super)(sb, lmd2->lmd2_mnt);
1476 /* c_f_s will call lustre_common_put_super on failure */
1479 #ifdef HAVE_SERVER_SUPPORT
1480 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
1481 rc = server_fill_super(sb);
1482 /* s_f_s calls lustre_start_mgc after the mount because we need
1483 the MGS nids which are stored on disk. Plus, we may
1484 need to start the MGS first. */
1485 /* s_f_s will call server_put_super on failure */
1487 CERROR("This is client-side-only module, "
1488 "cannot handle server mount.\n");
1493 /* If error happens in fill_super() call, @lsi will be killed there.
1494 * This is why we do not put it here. */
1498 CERROR("Unable to mount %s (%d)\n",
1499 s2lsi(sb) ? lmd->lmd_dev : "", rc);
1501 CDEBUG(D_SUPER, "Mount %s complete\n",
1509 /* We can't call ll_fill_super by name because it lives in a module that
1510 must be loaded after this one. */
1511 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb,
1512 struct vfsmount *mnt))
1514 client_fill_super = cfs;
1516 EXPORT_SYMBOL(lustre_register_client_fill_super);
1518 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
1520 kill_super_cb = cfs;
1522 EXPORT_SYMBOL(lustre_register_kill_super_cb);
1524 /***************** FS registration ******************/
1525 #ifdef HAVE_FSTYPE_MOUNT
1526 static struct dentry *lustre_mount(struct file_system_type *fs_type, int flags,
1527 const char *devname, void *data)
1529 struct lustre_mount_data2 lmd2 = {
1533 return mount_nodev(fs_type, flags, &lmd2, lustre_fill_super);
1536 static int lustre_get_sb(struct file_system_type *fs_type, int flags,
1537 const char *devname, void *data, struct vfsmount *mnt)
1539 struct lustre_mount_data2 lmd2 = {
1544 return get_sb_nodev(fs_type, flags, &lmd2, lustre_fill_super, mnt);
1548 static void lustre_kill_super(struct super_block *sb)
1550 struct lustre_sb_info *lsi = s2lsi(sb);
1552 if (kill_super_cb && lsi && !IS_SERVER(lsi))
1553 (*kill_super_cb)(sb);
1555 kill_anon_super(sb);
1558 /** Register the "lustre" fs type
1560 static struct file_system_type lustre_fs_type = {
1561 .owner = THIS_MODULE,
1563 #ifdef HAVE_FSTYPE_MOUNT
1564 .mount = lustre_mount,
1566 .get_sb = lustre_get_sb,
1568 .kill_sb = lustre_kill_super,
1569 .fs_flags = FS_REQUIRES_DEV | FS_HAS_FIEMAP | FS_RENAME_DOES_D_MOVE,
1571 MODULE_ALIAS_FS("lustre");
1573 int lustre_register_fs(void)
1575 return register_filesystem(&lustre_fs_type);
1578 int lustre_unregister_fs(void)
1580 return unregister_filesystem(&lustre_fs_type);