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, 2015, 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 lnet_process_id_t id;
240 while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
241 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
248 } else { /* client */
249 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
250 ptr = lsi->lsi_lmd->lmd_dev;
251 if (class_parse_nid(ptr, &nid, &ptr) == 0)
255 CERROR("No valid MGS nids found.\n");
259 mutex_lock(&mgc_start_lock);
261 libcfs_nid2str_r(nid, nidstr, sizeof(nidstr));
262 len = strlen(LUSTRE_MGC_OBDNAME) + strlen(nidstr) + 1;
263 OBD_ALLOC(mgcname, len);
264 OBD_ALLOC(niduuid, len + 2);
265 if (mgcname == NULL || niduuid == NULL)
266 GOTO(out_free, rc = -ENOMEM);
267 snprintf(mgcname, len, "%s%s", LUSTRE_MGC_OBDNAME, nidstr);
269 mgssec = lsi->lsi_lmd->lmd_mgssec ? lsi->lsi_lmd->lmd_mgssec : "";
273 GOTO(out_free, rc = -ENOMEM);
275 obd = class_name2obd(mgcname);
276 if (obd && !obd->obd_stopping) {
279 rc = obd_set_info_async(NULL, obd->obd_self_export,
280 strlen(KEY_MGSSEC), KEY_MGSSEC,
281 strlen(mgssec), mgssec, NULL);
285 /* Re-using an existing MGC */
286 atomic_inc(&obd->u.cli.cl_mgc_refcount);
288 /* IR compatibility check, only for clients */
289 if (lmd_is_client(lsi->lsi_lmd)) {
291 int vallen = sizeof(*data);
292 __u32 *flags = &lsi->lsi_lmd->lmd_flags;
294 rc = obd_get_info(NULL, obd->obd_self_export,
295 strlen(KEY_CONN_DATA), KEY_CONN_DATA,
298 has_ir = OCD_HAS_FLAG(data, IMP_RECOV);
299 if (has_ir ^ !(*flags & LMD_FLG_NOIR)) {
300 /* LMD_FLG_NOIR is for test purpose only */
302 "Trying to mount a client with IR setting "
303 "not compatible with current mgc. "
304 "Force to use current mgc setting that is "
306 has_ir ? "enabled" : "disabled");
308 *flags &= ~LMD_FLG_NOIR;
310 *flags |= LMD_FLG_NOIR;
315 /* If we are restarting the MGS, don't try to keep the MGC's
316 old connection, or registration will fail. */
318 CDEBUG(D_MOUNT, "New MGS with live MGC\n");
322 /* Try all connections, but only once (again).
323 We don't want to block another target from starting
324 (using its local copy of the log), but we do want to connect
325 if at all possible. */
327 CDEBUG(D_MOUNT, "%s: Set MGC reconnect %d\n", mgcname,recov_bk);
328 rc = obd_set_info_async(NULL, obd->obd_self_export,
329 sizeof(KEY_INIT_RECOV_BACKUP),
330 KEY_INIT_RECOV_BACKUP,
331 sizeof(recov_bk), &recov_bk, NULL);
335 CDEBUG(D_MOUNT, "Start MGC '%s'\n", mgcname);
337 /* Add the primary nids for the MGS */
339 snprintf(niduuid, len + 2, "%s_%x", mgcname, i);
340 if (IS_SERVER(lsi)) {
341 ptr = lsi->lsi_lmd->lmd_mgs;
342 CDEBUG(D_MOUNT, "mgs nids %s.\n", ptr);
344 /* Use local nids (including LO) */
345 lnet_process_id_t id;
346 while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
347 rc = do_lcfg(mgcname, id.nid, LCFG_ADD_UUID,
348 niduuid, NULL, NULL, NULL);
351 /* Use mgsnode= nids */
352 /* mount -o mgsnode=nid */
353 if (lsi->lsi_lmd->lmd_mgs) {
354 ptr = lsi->lsi_lmd->lmd_mgs;
355 } else if (class_find_param(ptr, PARAM_MGSNODE,
357 CERROR("No MGS nids given.\n");
358 GOTO(out_free, rc = -EINVAL);
362 * Here we only take the first mgsnid as its primary
363 * serving mgs node, the rest mgsnid will be taken as
364 * failover mgs node, otherwise they would be takens
365 * as multiple nids of a single mgs node.
367 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
368 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
369 niduuid, NULL, NULL, NULL);
376 } else { /* client */
377 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
378 ptr = lsi->lsi_lmd->lmd_dev;
379 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
380 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
381 niduuid, NULL, NULL, NULL);
384 /* Stop at the first failover nid */
390 CERROR("No valid MGS nids found.\n");
391 GOTO(out_free, rc = -EINVAL);
393 lsi->lsi_lmd->lmd_mgs_failnodes = 1;
395 /* Random uuid for MGC allows easier reconnects */
398 GOTO(out_free, rc = -ENOMEM);
400 ll_generate_random_uuid(uuidc);
401 class_uuid_unparse(uuidc, uuid);
404 rc = lustre_start_simple(mgcname, LUSTRE_MGC_NAME,
405 (char *)uuid->uuid, LUSTRE_MGS_OBDNAME,
406 niduuid, NULL, NULL);
411 /* Add any failover MGS nids */
413 while (ptr && ((*ptr == ':' ||
414 class_find_param(ptr, PARAM_MGSNODE, &ptr) == 0))) {
415 /* New failover node */
416 sprintf(niduuid, "%s_%x", mgcname, i);
418 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
419 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
420 niduuid, NULL, NULL, NULL);
427 rc = do_lcfg(mgcname, 0, LCFG_ADD_CONN,
428 niduuid, NULL, NULL, NULL);
436 lsi->lsi_lmd->lmd_mgs_failnodes = i;
438 obd = class_name2obd(mgcname);
440 CERROR("Can't find mgcobd %s\n", mgcname);
441 GOTO(out_free, rc = -ENOTCONN);
444 rc = obd_set_info_async(NULL, obd->obd_self_export,
445 strlen(KEY_MGSSEC), KEY_MGSSEC,
446 strlen(mgssec), mgssec, NULL);
450 /* Keep a refcount of servers/clients who started with "mount",
451 so we know when we can get rid of the mgc. */
452 atomic_set(&obd->u.cli.cl_mgc_refcount, 1);
454 /* We connect to the MGS at setup, and don't disconnect until cleanup */
455 data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
456 OBD_CONNECT_FULL20 | OBD_CONNECT_IMP_RECOV |
457 OBD_CONNECT_LVB_TYPE | OBD_CONNECT_BULK_MBITS;
459 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
460 data->ocd_connect_flags |= OBD_CONNECT_MNE_SWAB;
463 if (lmd_is_client(lsi->lsi_lmd) &&
464 lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)
465 data->ocd_connect_flags &= ~OBD_CONNECT_IMP_RECOV;
466 data->ocd_version = LUSTRE_VERSION_CODE;
467 rc = obd_connect(NULL, &exp, obd, &(obd->obd_uuid), data, NULL);
469 CERROR("connect failed %d\n", rc);
473 obd->u.cli.cl_mgc_mgsexp = exp;
476 /* Keep the mgc info in the sb. Note that many lsi's can point
480 mutex_unlock(&mgc_start_lock);
485 OBD_FREE(mgcname, len);
487 OBD_FREE(niduuid, len + 2);
491 static int lustre_stop_mgc(struct super_block *sb)
493 struct lustre_sb_info *lsi = s2lsi(sb);
494 struct obd_device *obd;
495 char *niduuid = NULL, *ptr = NULL;
496 int i, rc = 0, len = 0;
506 mutex_lock(&mgc_start_lock);
507 LASSERT(atomic_read(&obd->u.cli.cl_mgc_refcount) > 0);
508 if (!atomic_dec_and_test(&obd->u.cli.cl_mgc_refcount)) {
509 /* This is not fatal, every client that stops
510 will call in here. */
511 CDEBUG(D_MOUNT, "mgc still has %d references.\n",
512 atomic_read(&obd->u.cli.cl_mgc_refcount));
513 GOTO(out, rc = -EBUSY);
516 /* The MGC has no recoverable data in any case.
517 * force shotdown set in umount_begin */
518 obd->obd_no_recov = 1;
520 if (obd->u.cli.cl_mgc_mgsexp) {
521 /* An error is not fatal, if we are unable to send the
522 disconnect mgs ping evictor cleans up the export */
523 rc = obd_disconnect(obd->u.cli.cl_mgc_mgsexp);
525 CDEBUG(D_MOUNT, "disconnect failed %d\n", rc);
528 /* Save the obdname for cleaning the nid uuids, which are
530 len = strlen(obd->obd_name) + 6;
531 OBD_ALLOC(niduuid, len);
533 strcpy(niduuid, obd->obd_name);
534 ptr = niduuid + strlen(niduuid);
537 rc = class_manual_cleanup(obd);
541 /* Clean the nid uuids */
543 GOTO(out, rc = -ENOMEM);
545 for (i = 0; i < lsi->lsi_lmd->lmd_mgs_failnodes; i++) {
546 sprintf(ptr, "_%x", i);
547 rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_DEL_UUID,
548 niduuid, NULL, NULL, NULL);
550 CERROR("del MDC UUID %s failed: rc = %d\n",
555 OBD_FREE(niduuid, len);
557 /* class_import_put will get rid of the additional connections */
558 mutex_unlock(&mgc_start_lock);
562 /***************** lustre superblock **************/
564 static struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
566 struct lustre_sb_info *lsi;
572 OBD_ALLOC_PTR(lsi->lsi_lmd);
578 lsi->lsi_lmd->lmd_exclude_count = 0;
579 lsi->lsi_lmd->lmd_recovery_time_soft = 0;
580 lsi->lsi_lmd->lmd_recovery_time_hard = 0;
581 s2lsi_nocast(sb) = lsi;
582 /* we take 1 extra ref for our setup */
583 atomic_set(&lsi->lsi_mounts, 1);
585 /* Default umount style */
586 lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
587 INIT_LIST_HEAD(&lsi->lsi_lwp_list);
588 spin_lock_init(&lsi->lsi_lwp_lock);
593 static int lustre_free_lsi(struct super_block *sb)
595 struct lustre_sb_info *lsi = s2lsi(sb);
598 LASSERT(lsi != NULL);
599 CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
601 /* someone didn't call server_put_mount. */
602 LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
604 if (lsi->lsi_lmd != NULL) {
605 if (lsi->lsi_lmd->lmd_dev != NULL)
606 OBD_FREE(lsi->lsi_lmd->lmd_dev,
607 strlen(lsi->lsi_lmd->lmd_dev) + 1);
608 if (lsi->lsi_lmd->lmd_profile != NULL)
609 OBD_FREE(lsi->lsi_lmd->lmd_profile,
610 strlen(lsi->lsi_lmd->lmd_profile) + 1);
611 if (lsi->lsi_lmd->lmd_fileset != NULL)
612 OBD_FREE(lsi->lsi_lmd->lmd_fileset,
613 strlen(lsi->lsi_lmd->lmd_fileset) + 1);
614 if (lsi->lsi_lmd->lmd_mgssec != NULL)
615 OBD_FREE(lsi->lsi_lmd->lmd_mgssec,
616 strlen(lsi->lsi_lmd->lmd_mgssec) + 1);
617 if (lsi->lsi_lmd->lmd_opts != NULL)
618 OBD_FREE(lsi->lsi_lmd->lmd_opts,
619 strlen(lsi->lsi_lmd->lmd_opts) + 1);
620 if (lsi->lsi_lmd->lmd_exclude_count)
621 OBD_FREE(lsi->lsi_lmd->lmd_exclude,
622 sizeof(lsi->lsi_lmd->lmd_exclude[0]) *
623 lsi->lsi_lmd->lmd_exclude_count);
624 if (lsi->lsi_lmd->lmd_mgs != NULL)
625 OBD_FREE(lsi->lsi_lmd->lmd_mgs,
626 strlen(lsi->lsi_lmd->lmd_mgs) + 1);
627 if (lsi->lsi_lmd->lmd_osd_type != NULL)
628 OBD_FREE(lsi->lsi_lmd->lmd_osd_type,
629 strlen(lsi->lsi_lmd->lmd_osd_type) + 1);
630 if (lsi->lsi_lmd->lmd_params != NULL)
631 OBD_FREE(lsi->lsi_lmd->lmd_params, 4096);
633 OBD_FREE_PTR(lsi->lsi_lmd);
636 LASSERT(lsi->lsi_llsbi == NULL);
638 s2lsi_nocast(sb) = NULL;
643 /* The lsi has one reference for every server that is using the disk -
644 e.g. MDT, MGS, and potentially MGC */
645 int lustre_put_lsi(struct super_block *sb)
647 struct lustre_sb_info *lsi = s2lsi(sb);
650 LASSERT(lsi != NULL);
652 CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
653 if (atomic_dec_and_test(&lsi->lsi_mounts)) {
654 if (IS_SERVER(lsi) && lsi->lsi_osd_exp) {
655 lu_device_put(&lsi->lsi_dt_dev->dd_lu_dev);
656 lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt = NULL;
657 lsi->lsi_dt_dev = NULL;
658 obd_disconnect(lsi->lsi_osd_exp);
659 /* wait till OSD is gone */
660 obd_zombie_barrier();
669 * <FSNAME><SEPARATOR><TYPE><INDEX>
670 * FSNAME is between 1 and 8 characters (inclusive).
671 * Excluded characters are '/' and ':'
672 * SEPARATOR is either ':' or '-'
673 * TYPE: "OST", "MDT", etc.
674 * INDEX: Hex representation of the index
677 /** Get the fsname ("lustre") from the server name ("lustre-OST003F").
678 * @param [in] svname server name including type and index
679 * @param [out] fsname Buffer to copy filesystem name prefix into.
680 * Must have at least 'strlen(fsname) + 1' chars.
681 * @param [out] endptr if endptr isn't NULL it is set to end of fsname
684 int server_name2fsname(const char *svname, char *fsname, const char **endptr)
688 dash = svname + strnlen(svname, 8); /* max fsname length is 8 */
689 for (; dash > svname && *dash != '-' && *dash != ':'; dash--)
694 if (fsname != NULL) {
695 strncpy(fsname, svname, dash - svname);
696 fsname[dash - svname] = '\0';
704 EXPORT_SYMBOL(server_name2fsname);
707 * Get service name (svname) from string
709 * if endptr isn't NULL it is set to end of fsname *
711 int server_name2svname(const char *label, char *svname, const char **endptr,
717 /* We use server_name2fsname() just for parsing */
718 rc = server_name2fsname(label, NULL, &dash);
725 if (strlcpy(svname, dash + 1, svsize) >= svsize)
730 EXPORT_SYMBOL(server_name2svname);
733 * check server name is OST.
735 int server_name_is_ost(const char *svname)
740 /* We use server_name2fsname() just for parsing */
741 rc = server_name2fsname(svname, NULL, &dash);
747 if (strncmp(dash, "OST", 3) == 0)
751 EXPORT_SYMBOL(server_name_is_ost);
754 * Get the index from the target name MDTXXXX/OSTXXXX
755 * rc = server type, or rc < 0 on error
757 int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
759 const char *dash = tgtname;
763 if (strncmp(dash, "MDT", 3) == 0)
764 rc = LDD_F_SV_TYPE_MDT;
765 else if (strncmp(dash, "OST", 3) == 0)
766 rc = LDD_F_SV_TYPE_OST;
772 if (strncmp(dash, "all", 3) == 0) {
775 return rc | LDD_F_SV_ALL;
778 index = simple_strtoul(dash, (char **)endptr, 16);
783 EXPORT_SYMBOL(target_name2index);
785 /* Get the index from the obd name.
788 if endptr isn't NULL it is set to end of name */
789 int server_name2index(const char *svname, __u32 *idx, const char **endptr)
794 /* We use server_name2fsname() just for parsing */
795 rc = server_name2fsname(svname, NULL, &dash);
800 rc = target_name2index(dash, idx, endptr);
804 /* Account for -mdc after index that is possible when specifying mdt */
805 if (endptr != NULL && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
806 sizeof(LUSTRE_MDC_NAME)-1) == 0)
807 *endptr += sizeof(LUSTRE_MDC_NAME);
811 EXPORT_SYMBOL(server_name2index);
813 /*************** mount common betweeen server and client ***************/
816 int lustre_common_put_super(struct super_block *sb)
821 CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
823 /* Drop a ref to the MGC */
824 rc = lustre_stop_mgc(sb);
825 if (rc && (rc != -ENOENT)) {
827 CERROR("Can't stop MGC: %d\n", rc);
830 /* BUSY just means that there's some other obd that
831 needs the mgc. Let him clean it up. */
832 CDEBUG(D_MOUNT, "MGC still in use\n");
834 /* Drop a ref to the mounted disk */
839 EXPORT_SYMBOL(lustre_common_put_super);
841 static void lmd_print(struct lustre_mount_data *lmd)
845 PRINT_CMD(D_MOUNT, " mount data:\n");
846 if (lmd_is_client(lmd))
847 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
848 PRINT_CMD(D_MOUNT, "device: %s\n", lmd->lmd_dev);
849 PRINT_CMD(D_MOUNT, "flags: %x\n", lmd->lmd_flags);
852 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
854 if (lmd->lmd_recovery_time_soft)
855 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
856 lmd->lmd_recovery_time_soft);
858 if (lmd->lmd_recovery_time_hard)
859 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
860 lmd->lmd_recovery_time_hard);
862 for (i = 0; i < lmd->lmd_exclude_count; i++) {
863 PRINT_CMD(D_MOUNT, "exclude %d: OST%04x\n", i,
864 lmd->lmd_exclude[i]);
868 /* Is this server on the exclusion list */
869 int lustre_check_exclusion(struct super_block *sb, char *svname)
871 struct lustre_sb_info *lsi = s2lsi(sb);
872 struct lustre_mount_data *lmd = lsi->lsi_lmd;
877 rc = server_name2index(svname, &index, NULL);
878 if (rc != LDD_F_SV_TYPE_OST)
879 /* Only exclude OSTs */
882 CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
883 index, lmd->lmd_exclude_count, lmd->lmd_dev);
885 for(i = 0; i < lmd->lmd_exclude_count; i++) {
886 if (index == lmd->lmd_exclude[i]) {
887 CWARN("Excluding %s (on exclusion list)\n", svname);
894 /* mount -v -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
895 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
897 const char *s1 = ptr, *s2;
903 /* The shortest an ost name can be is 8 chars: -OST0000.
904 We don't actually know the fsname at this time, so in fact
905 a user could specify any fsname. */
906 devmax = strlen(ptr) / 8 + 1;
908 /* temp storage until we figure out how many we have */
909 OBD_ALLOC(exclude_list, sizeof(index) * devmax);
913 /* we enter this fn pointing at the '=' */
914 while (*s1 && *s1 != ' ' && *s1 != ',') {
916 rc = server_name2index(s1, &index, &s2);
918 CERROR("Can't parse server name '%s': rc = %d\n",
922 if (rc == LDD_F_SV_TYPE_OST)
923 exclude_list[lmd->lmd_exclude_count++] = index;
925 CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
926 (uint)(s2-s1), s1, rc);
928 /* now we are pointing at ':' (next exclude)
929 or ',' (end of excludes) */
930 if (lmd->lmd_exclude_count >= devmax)
933 if (rc >= 0) /* non-err */
936 if (lmd->lmd_exclude_count) {
937 /* permanent, freed in lustre_free_lsi */
938 OBD_ALLOC(lmd->lmd_exclude, sizeof(index) *
939 lmd->lmd_exclude_count);
940 if (lmd->lmd_exclude) {
941 memcpy(lmd->lmd_exclude, exclude_list,
942 sizeof(index) * lmd->lmd_exclude_count);
945 lmd->lmd_exclude_count = 0;
948 OBD_FREE(exclude_list, sizeof(index) * devmax);
952 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
957 if (lmd->lmd_mgssec != NULL) {
958 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
959 lmd->lmd_mgssec = NULL;
962 tail = strchr(ptr, ',');
964 length = strlen(ptr);
968 OBD_ALLOC(lmd->lmd_mgssec, length + 1);
969 if (lmd->lmd_mgssec == NULL)
972 memcpy(lmd->lmd_mgssec, ptr, length);
973 lmd->lmd_mgssec[length] = '\0';
977 static int lmd_parse_string(char **handle, char *ptr)
982 if ((handle == NULL) || (ptr == NULL))
985 if (*handle != NULL) {
986 OBD_FREE(*handle, strlen(*handle) + 1);
990 tail = strchr(ptr, ',');
992 length = strlen(ptr);
996 OBD_ALLOC(*handle, length + 1);
1000 memcpy(*handle, ptr, length);
1001 (*handle)[length] = '\0';
1006 /* Collect multiple values for mgsnid specifiers */
1007 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
1015 /* Find end of nidlist */
1016 while (class_parse_nid_quiet(tail, &nid, &tail) == 0) {}
1017 length = tail - *ptr;
1019 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
1023 if (lmd->lmd_mgs != NULL)
1024 oldlen = strlen(lmd->lmd_mgs) + 1;
1026 OBD_ALLOC(mgsnid, oldlen + length + 1);
1030 if (lmd->lmd_mgs != NULL) {
1031 /* Multiple mgsnid= are taken to mean failover locations */
1032 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
1033 mgsnid[oldlen - 1] = ':';
1034 OBD_FREE(lmd->lmd_mgs, oldlen);
1036 memcpy(mgsnid + oldlen, *ptr, length);
1037 mgsnid[oldlen + length] = '\0';
1038 lmd->lmd_mgs = mgsnid;
1045 * Find the first delimiter (comma or colon) from the specified \a buf and
1046 * make \a *endh point to the string starting with the delimiter. The commas
1047 * in expression list [...] will be skipped.
1049 * \param[in] buf a delimiter-separated string
1050 * \param[in] endh a pointer to a pointer that will point to the string
1051 * starting with the delimiter
1053 * \retval 0 if delimiter is found
1054 * \retval 1 if delimiter is not found
1056 static int lmd_find_delimiter(char *buf, char **endh)
1064 while (*c != '\0') {
1070 if ((*c == ',' || *c == ':') && skip == 0) {
1083 * Find the first valid string delimited by comma or colon from the specified
1084 * \a buf and parse it to see whether it's a valid nid list. If yes, \a *endh
1085 * will point to the next string starting with the delimiter.
1087 * \param[in] buf a delimiter-separated string
1088 * \param[in] endh a pointer to a pointer that will point to the string
1089 * starting with the delimiter
1091 * \retval 0 if the string is a valid nid list
1092 * \retval 1 if the string is not a valid nid list
1094 static int lmd_parse_nidlist(char *buf, char **endh)
1096 struct list_head nidlist;
1103 while (*buf == ',' || *buf == ':')
1105 if (*buf == ' ' || *buf == '/' || *buf == '\0')
1108 if (lmd_find_delimiter(buf, &endp) != 0)
1109 endp = buf + strlen(buf);
1114 INIT_LIST_HEAD(&nidlist);
1115 if (cfs_parse_nidlist(buf, strlen(buf), &nidlist) <= 0)
1117 cfs_free_nidlist(&nidlist);
1127 /** Parse mount line options
1128 * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
1129 * dev is passed as device=uml1:/lustre by mount.lustre
1131 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
1133 char *s1, *s2, *s3, *devname = NULL;
1134 struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1140 LCONSOLE_ERROR_MSG(0x162, "Missing mount data: check that "
1141 "/sbin/mount.lustre is installed.\n");
1145 /* Options should be a string - try to detect old lmd data */
1146 if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1147 LCONSOLE_ERROR_MSG(0x163, "You're using an old version of "
1148 "/sbin/mount.lustre. Please install "
1149 "version %s\n", LUSTRE_VERSION_STRING);
1152 lmd->lmd_magic = LMD_MAGIC;
1154 OBD_ALLOC(lmd->lmd_params, LMD_PARAMS_MAXLEN);
1155 if (lmd->lmd_params == NULL)
1157 lmd->lmd_params[0] = '\0';
1159 /* Set default flags here */
1164 int time_min = OBD_RECOVERY_TIME_MIN;
1166 /* Skip whitespace and extra commas */
1167 while (*s1 == ' ' || *s1 == ',')
1171 /* Client options are parsed in ll_options: eg. flock,
1174 /* Parse non-ldiskfs options here. Rather than modifying
1175 ldiskfs, we just zero these out here */
1176 if (strncmp(s1, "abort_recov", 11) == 0) {
1177 lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
1179 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
1180 lmd->lmd_recovery_time_soft = max_t(int,
1181 simple_strtoul(s1 + 19, NULL, 10), time_min);
1183 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
1184 lmd->lmd_recovery_time_hard = max_t(int,
1185 simple_strtoul(s1 + 19, NULL, 10), time_min);
1187 } else if (strncmp(s1, "noir", 4) == 0) {
1188 lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
1190 } else if (strncmp(s1, "nosvc", 5) == 0) {
1191 lmd->lmd_flags |= LMD_FLG_NOSVC;
1193 } else if (strncmp(s1, "nomgs", 5) == 0) {
1194 lmd->lmd_flags |= LMD_FLG_NOMGS;
1196 } else if (strncmp(s1, "noscrub", 7) == 0) {
1197 lmd->lmd_flags |= LMD_FLG_NOSCRUB;
1199 } else if (strncmp(s1, "skip_lfsck", 10) == 0) {
1200 lmd->lmd_flags |= LMD_FLG_SKIP_LFSCK;
1202 } else if (strncmp(s1, PARAM_MGSNODE,
1203 sizeof(PARAM_MGSNODE) - 1) == 0) {
1204 s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
1205 /* Assume the next mount opt is the first
1206 invalid nid we get to. */
1207 rc = lmd_parse_mgs(lmd, &s2);
1212 } else if (strncmp(s1, "writeconf", 9) == 0) {
1213 lmd->lmd_flags |= LMD_FLG_WRITECONF;
1215 } else if (strncmp(s1, "update", 6) == 0) {
1216 lmd->lmd_flags |= LMD_FLG_UPDATE;
1218 } else if (strncmp(s1, "virgin", 6) == 0) {
1219 lmd->lmd_flags |= LMD_FLG_VIRGIN;
1221 } else if (strncmp(s1, "noprimnode", 10) == 0) {
1222 lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
1224 } else if (strncmp(s1, "mgssec=", 7) == 0) {
1225 rc = lmd_parse_mgssec(lmd, s1 + 7);
1229 /* ost exclusion list */
1230 } else if (strncmp(s1, "exclude=", 8) == 0) {
1231 rc = lmd_make_exclusion(lmd, s1 + 7);
1235 } else if (strncmp(s1, "mgs", 3) == 0) {
1237 lmd->lmd_flags |= LMD_FLG_MGS;
1239 } else if (strncmp(s1, "svname=", 7) == 0) {
1240 rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
1244 } else if (strncmp(s1, "param=", 6) == 0) {
1245 size_t length, params_length;
1247 if (lmd_find_delimiter(s1 + 6, &tail) != 0)
1248 length = strlen(s1);
1250 char *param_str = tail + 1;
1251 int supplementary = 1;
1252 while (lmd_parse_nidlist(param_str,
1256 length = param_str - s1 - supplementary;
1259 params_length = strlen(lmd->lmd_params);
1260 if (params_length + length + 1 >= LMD_PARAMS_MAXLEN)
1262 strncat(lmd->lmd_params, s1 + 6, length);
1263 lmd->lmd_params[params_length + length] = '\0';
1264 strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
1265 s3 = s1 + 6 + length;
1267 } else if (strncmp(s1, "osd=", 4) == 0) {
1268 rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
1273 /* Linux 2.4 doesn't pass the device, so we stuck it at the
1274 end of the options. */
1275 else if (strncmp(s1, "device=", 7) == 0) {
1277 /* terminate options right before device. device
1278 must be the last one. */
1284 s2 = strchr(s3, ',');
1292 memmove(s1, s2, strlen(s2) + 1);
1298 LCONSOLE_ERROR_MSG(0x164, "Can't find the device name "
1299 "(need mount option 'device=...')\n");
1303 s1 = strstr(devname, ":/");
1306 lmd->lmd_flags |= LMD_FLG_CLIENT;
1307 /* Remove leading /s from fsname */
1308 while (*++s1 == '/')
1311 while (*s2 != '/' && *s2 != '\0')
1313 /* Freed in lustre_free_lsi */
1314 OBD_ALLOC(lmd->lmd_profile, s2 - s1 + 8);
1315 if (!lmd->lmd_profile)
1318 strncat(lmd->lmd_profile, s1, s2 - s1);
1319 strncat(lmd->lmd_profile, "-client", 7);
1322 s2 = s1 + strlen(s1) - 1;
1323 /* Remove padding /s from fileset */
1327 OBD_ALLOC(lmd->lmd_fileset, s2 - s1 + 2);
1328 if (lmd->lmd_fileset == NULL) {
1329 OBD_FREE(lmd->lmd_profile,
1330 strlen(lmd->lmd_profile) + 1);
1333 strncat(lmd->lmd_fileset, s1, s2 - s1 + 1);
1337 /* Freed in lustre_free_lsi */
1338 OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
1341 strcpy(lmd->lmd_dev, devname);
1343 /* Save mount options */
1344 s1 = options + strlen(options) - 1;
1345 while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1347 if (*options != 0) {
1348 /* Freed in lustre_free_lsi */
1349 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1352 strcpy(lmd->lmd_opts, options);
1356 lmd->lmd_magic = LMD_MAGIC;
1361 CERROR("Bad mount options %s\n", options);
1365 struct lustre_mount_data2 {
1367 struct vfsmount *lmd2_mnt;
1370 /** This is the entry point for the mount call into Lustre.
1371 * This is called when a server or client is mounted,
1372 * and this is where we start setting things up.
1373 * @param data Mount options (e.g. -o flock,abort_recov)
1375 static int lustre_fill_super(struct super_block *sb, void *data, int silent)
1377 struct lustre_mount_data *lmd;
1378 struct lustre_mount_data2 *lmd2 = data;
1379 struct lustre_sb_info *lsi;
1383 CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
1385 lsi = lustre_init_lsi(sb);
1391 * Disable lockdep during mount, because mount locking patterns are
1397 * LU-639: the obd cleanup of last mount may not finish yet, wait here.
1399 obd_zombie_barrier();
1401 /* Figure out the lmd from the mount options */
1402 if (lmd_parse((char *)(lmd2->lmd2_data), lmd)) {
1404 GOTO(out, rc = -EINVAL);
1407 if (lmd_is_client(lmd)) {
1408 CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
1409 if (client_fill_super == NULL)
1410 request_module("lustre");
1411 if (client_fill_super == NULL) {
1412 LCONSOLE_ERROR_MSG(0x165, "Nothing registered for "
1413 "client mount! Is the 'lustre' "
1414 "module loaded?\n");
1418 rc = lustre_start_mgc(sb);
1420 lustre_common_put_super(sb);
1423 /* Connect and start */
1424 /* (should always be ll_fill_super) */
1425 rc = (*client_fill_super)(sb, lmd2->lmd2_mnt);
1426 /* c_f_s will call lustre_common_put_super on failure */
1429 #ifdef HAVE_SERVER_SUPPORT
1430 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
1431 rc = server_fill_super(sb);
1432 /* s_f_s calls lustre_start_mgc after the mount because we need
1433 the MGS nids which are stored on disk. Plus, we may
1434 need to start the MGS first. */
1435 /* s_f_s will call server_put_super on failure */
1437 CERROR("This is client-side-only module, "
1438 "cannot handle server mount.\n");
1443 /* If error happens in fill_super() call, @lsi will be killed there.
1444 * This is why we do not put it here. */
1448 CERROR("Unable to mount %s (%d)\n",
1449 s2lsi(sb) ? lmd->lmd_dev : "", rc);
1451 CDEBUG(D_SUPER, "Mount %s complete\n",
1459 /* We can't call ll_fill_super by name because it lives in a module that
1460 must be loaded after this one. */
1461 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb,
1462 struct vfsmount *mnt))
1464 client_fill_super = cfs;
1466 EXPORT_SYMBOL(lustre_register_client_fill_super);
1468 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
1470 kill_super_cb = cfs;
1472 EXPORT_SYMBOL(lustre_register_kill_super_cb);
1474 /***************** FS registration ******************/
1475 #ifdef HAVE_FSTYPE_MOUNT
1476 static struct dentry *lustre_mount(struct file_system_type *fs_type, int flags,
1477 const char *devname, void *data)
1479 struct lustre_mount_data2 lmd2 = { data, NULL };
1481 return mount_nodev(fs_type, flags, &lmd2, lustre_fill_super);
1484 static int lustre_get_sb(struct file_system_type *fs_type, int flags,
1485 const char *devname, void *data, struct vfsmount *mnt)
1487 struct lustre_mount_data2 lmd2 = { data, mnt };
1489 return get_sb_nodev(fs_type, flags, &lmd2, lustre_fill_super, mnt);
1493 static void lustre_kill_super(struct super_block *sb)
1495 struct lustre_sb_info *lsi = s2lsi(sb);
1497 if (kill_super_cb && lsi && !IS_SERVER(lsi))
1498 (*kill_super_cb)(sb);
1500 kill_anon_super(sb);
1503 /** Register the "lustre" fs type
1505 static struct file_system_type lustre_fs_type = {
1506 .owner = THIS_MODULE,
1508 #ifdef HAVE_FSTYPE_MOUNT
1509 .mount = lustre_mount,
1511 .get_sb = lustre_get_sb,
1513 .kill_sb = lustre_kill_super,
1514 .fs_flags = FS_REQUIRES_DEV | FS_HAS_FIEMAP | FS_RENAME_DOES_D_MOVE,
1516 MODULE_ALIAS_FS("lustre");
1518 int lustre_register_fs(void)
1520 return register_filesystem(&lustre_fs_type);
1523 int lustre_unregister_fs(void)
1525 return unregister_filesystem(&lustre_fs_type);