4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/obd_mount.c
38 * Client mount routines
40 * Author: Nathan Rutman <nathan@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_CLASS
45 #define D_MOUNT (D_SUPER|D_CONFIG/*|D_WARNING */)
46 #define PRINT_CMD CDEBUG
49 #include <obd_class.h>
50 #include <lustre/lustre_user.h>
51 #include <linux/version.h>
52 #include <lustre_log.h>
53 #include <lustre_disk.h>
54 #include <lustre_param.h>
56 static int (*client_fill_super)(struct super_block *sb,
57 struct vfsmount *mnt);
59 static void (*kill_super_cb)(struct super_block *sb);
61 /**************** config llog ********************/
63 /** Get a config log from the MGS and process it.
64 * This func is called for both clients and servers.
65 * Continue to process new statements appended to the logs
66 * (whenever the config lock is revoked) until lustre_end_log
68 * @param sb The superblock is used by the MGC to write to the local copy of
70 * @param logname The name of the llog to replicate from the MGS
71 * @param cfg Since the same mgc may be used to follow multiple config logs
72 * (e.g. ost1, ost2, client), the config_llog_instance keeps the state for
73 * this log, and is added to the mgc's list of logs to follow.
75 int lustre_process_log(struct super_block *sb, char *logname,
76 struct config_llog_instance *cfg)
78 struct lustre_cfg *lcfg;
79 struct lustre_cfg_bufs *bufs;
80 struct lustre_sb_info *lsi = s2lsi(sb);
81 struct obd_device *mgc = lsi->lsi_mgc;
92 /* mgc_process_config */
93 lustre_cfg_bufs_reset(bufs, mgc->obd_name);
94 lustre_cfg_bufs_set_string(bufs, 1, logname);
95 lustre_cfg_bufs_set(bufs, 2, cfg, sizeof(*cfg));
96 lustre_cfg_bufs_set(bufs, 3, &sb, sizeof(sb));
97 lcfg = lustre_cfg_new(LCFG_LOG_START, bufs);
99 GOTO(out, rc = -ENOMEM);
100 rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
101 lustre_cfg_free(lcfg);
106 LCONSOLE_ERROR_MSG(0x15b, "%s: The configuration from log '%s'"
107 "failed from the MGS (%d). Make sure this "
108 "client and the MGS are running compatible "
109 "versions of Lustre.\n",
110 mgc->obd_name, logname, rc);
112 LCONSOLE_ERROR_MSG(0x15c, "%s: The configuration from log '%s' "
113 "failed (%d). This may be the result of "
114 "communication errors between this node and "
115 "the MGS, a bad configuration, or other "
116 "errors. See the syslog for more "
117 "information.\n", mgc->obd_name, logname,
120 /* class_obd_list(); */
123 EXPORT_SYMBOL(lustre_process_log);
125 /* Stop watching this config log for updates */
126 int lustre_end_log(struct super_block *sb, char *logname,
127 struct config_llog_instance *cfg)
129 struct lustre_cfg *lcfg;
130 struct lustre_cfg_bufs bufs;
131 struct lustre_sb_info *lsi = s2lsi(sb);
132 struct obd_device *mgc = lsi->lsi_mgc;
139 /* mgc_process_config */
140 lustre_cfg_bufs_reset(&bufs, mgc->obd_name);
141 lustre_cfg_bufs_set_string(&bufs, 1, logname);
143 lustre_cfg_bufs_set(&bufs, 2, cfg, sizeof(*cfg));
144 lcfg = lustre_cfg_new(LCFG_LOG_END, &bufs);
147 rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
148 lustre_cfg_free(lcfg);
151 EXPORT_SYMBOL(lustre_end_log);
153 /**************** obd start *******************/
155 /** lustre_cfg_bufs are a holdover from 1.4; we can still set these up from
156 * lctl (and do for echo cli/srv.
158 int do_lcfg(char *cfgname, lnet_nid_t nid, int cmd,
159 char *s1, char *s2, char *s3, char *s4)
161 struct lustre_cfg_bufs bufs;
162 struct lustre_cfg * lcfg = NULL;
165 CDEBUG(D_TRACE, "lcfg %s %#x %s %s %s %s\n", cfgname,
166 cmd, s1, s2, s3, s4);
168 lustre_cfg_bufs_reset(&bufs, cfgname);
170 lustre_cfg_bufs_set_string(&bufs, 1, s1);
172 lustre_cfg_bufs_set_string(&bufs, 2, s2);
174 lustre_cfg_bufs_set_string(&bufs, 3, s3);
176 lustre_cfg_bufs_set_string(&bufs, 4, s4);
178 lcfg = lustre_cfg_new(cmd, &bufs);
181 lcfg->lcfg_nid = nid;
182 rc = class_process_config(lcfg);
183 lustre_cfg_free(lcfg);
187 /** Call class_attach and class_setup. These methods in turn call
188 * obd type-specific methods.
190 int lustre_start_simple(char *obdname, char *type, char *uuid,
191 char *s1, char *s2, char *s3, char *s4)
194 CDEBUG(D_MOUNT, "Starting obd %s (typ=%s)\n", obdname, type);
196 rc = do_lcfg(obdname, 0, LCFG_ATTACH, type, uuid, NULL, NULL);
198 CERROR("%s attach error %d\n", obdname, rc);
201 rc = do_lcfg(obdname, 0, LCFG_SETUP, s1, s2, s3, s4);
203 CERROR("%s setup error %d\n", obdname, rc);
204 do_lcfg(obdname, 0, LCFG_DETACH, NULL, NULL, NULL, NULL);
209 static DEFINE_MUTEX(mgc_start_lock);
211 /** Set up a mgc obd to process startup logs
213 * \param sb [in] super block of the mgc obd
215 * \retval 0 success, otherwise error code
217 int lustre_start_mgc(struct super_block *sb)
219 struct obd_connect_data *data = NULL;
220 struct lustre_sb_info *lsi = s2lsi(sb);
221 struct obd_device *obd;
222 struct obd_export *exp;
223 struct obd_uuid *uuid;
226 char nidstr[LNET_NIDSTR_SIZE];
227 char *mgcname = NULL, *niduuid = NULL, *mgssec = NULL;
229 int rc = 0, i = 0, j;
233 LASSERT(lsi->lsi_lmd);
235 /* Find the first non-lo MGS nid for our MGC name */
236 if (IS_SERVER(lsi)) {
237 /* mount -o mgsnode=nid */
238 ptr = lsi->lsi_lmd->lmd_mgs;
239 if (lsi->lsi_lmd->lmd_mgs &&
240 (class_parse_nid(lsi->lsi_lmd->lmd_mgs, &nid, &ptr) == 0)) {
242 } else if (IS_MGS(lsi)) {
243 lnet_process_id_t id;
244 while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
245 if (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND)
252 } else { /* client */
253 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
254 ptr = lsi->lsi_lmd->lmd_dev;
255 if (class_parse_nid(ptr, &nid, &ptr) == 0)
259 CERROR("No valid MGS nids found.\n");
263 mutex_lock(&mgc_start_lock);
265 libcfs_nid2str_r(nid, nidstr, sizeof(nidstr));
266 len = strlen(LUSTRE_MGC_OBDNAME) + strlen(nidstr) + 1;
267 OBD_ALLOC(mgcname, len);
268 OBD_ALLOC(niduuid, len + 2);
269 if (mgcname == NULL || niduuid == NULL)
270 GOTO(out_free, rc = -ENOMEM);
271 snprintf(mgcname, len, "%s%s", LUSTRE_MGC_OBDNAME, nidstr);
273 mgssec = lsi->lsi_lmd->lmd_mgssec ? lsi->lsi_lmd->lmd_mgssec : "";
277 GOTO(out_free, rc = -ENOMEM);
279 obd = class_name2obd(mgcname);
280 if (obd && !obd->obd_stopping) {
283 rc = obd_set_info_async(NULL, obd->obd_self_export,
284 strlen(KEY_MGSSEC), KEY_MGSSEC,
285 strlen(mgssec), mgssec, NULL);
289 /* Re-using an existing MGC */
290 atomic_inc(&obd->u.cli.cl_mgc_refcount);
292 /* IR compatibility check, only for clients */
293 if (lmd_is_client(lsi->lsi_lmd)) {
295 int vallen = sizeof(*data);
296 __u32 *flags = &lsi->lsi_lmd->lmd_flags;
298 rc = obd_get_info(NULL, obd->obd_self_export,
299 strlen(KEY_CONN_DATA), KEY_CONN_DATA,
302 has_ir = OCD_HAS_FLAG(data, IMP_RECOV);
303 if (has_ir ^ !(*flags & LMD_FLG_NOIR)) {
304 /* LMD_FLG_NOIR is for test purpose only */
306 "Trying to mount a client with IR setting "
307 "not compatible with current mgc. "
308 "Force to use current mgc setting that is "
310 has_ir ? "enabled" : "disabled");
312 *flags &= ~LMD_FLG_NOIR;
314 *flags |= LMD_FLG_NOIR;
319 /* If we are restarting the MGS, don't try to keep the MGC's
320 old connection, or registration will fail. */
322 CDEBUG(D_MOUNT, "New MGS with live MGC\n");
326 /* Try all connections, but only once (again).
327 We don't want to block another target from starting
328 (using its local copy of the log), but we do want to connect
329 if at all possible. */
331 CDEBUG(D_MOUNT, "%s: Set MGC reconnect %d\n", mgcname,recov_bk);
332 rc = obd_set_info_async(NULL, obd->obd_self_export,
333 sizeof(KEY_INIT_RECOV_BACKUP),
334 KEY_INIT_RECOV_BACKUP,
335 sizeof(recov_bk), &recov_bk, NULL);
339 CDEBUG(D_MOUNT, "Start MGC '%s'\n", mgcname);
341 /* Add the primary nids for the MGS */
343 snprintf(niduuid, len + 2, "%s_%x", mgcname, i);
344 if (IS_SERVER(lsi)) {
345 ptr = lsi->lsi_lmd->lmd_mgs;
346 CDEBUG(D_MOUNT, "mgs nids %s.\n", ptr);
348 /* Use local nids (including LO) */
349 lnet_process_id_t id;
350 while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
351 rc = do_lcfg(mgcname, id.nid, LCFG_ADD_UUID,
352 niduuid, NULL, NULL, NULL);
355 /* Use mgsnode= nids */
356 /* mount -o mgsnode=nid */
357 if (lsi->lsi_lmd->lmd_mgs) {
358 ptr = lsi->lsi_lmd->lmd_mgs;
359 } else if (class_find_param(ptr, PARAM_MGSNODE,
361 CERROR("No MGS nids given.\n");
362 GOTO(out_free, rc = -EINVAL);
366 * Here we only take the first mgsnid as its primary
367 * serving mgs node, the rest mgsnid will be taken as
368 * failover mgs node, otherwise they would be takens
369 * as multiple nids of a single mgs node.
371 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
372 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
373 niduuid, NULL, NULL, NULL);
380 } else { /* client */
381 /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
382 ptr = lsi->lsi_lmd->lmd_dev;
383 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
384 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
385 niduuid, NULL, NULL, NULL);
388 /* Stop at the first failover nid */
394 CERROR("No valid MGS nids found.\n");
395 GOTO(out_free, rc = -EINVAL);
397 lsi->lsi_lmd->lmd_mgs_failnodes = 1;
399 /* Random uuid for MGC allows easier reconnects */
402 GOTO(out_free, rc = -ENOMEM);
404 ll_generate_random_uuid(uuidc);
405 class_uuid_unparse(uuidc, uuid);
408 rc = lustre_start_simple(mgcname, LUSTRE_MGC_NAME,
409 (char *)uuid->uuid, LUSTRE_MGS_OBDNAME,
410 niduuid, NULL, NULL);
415 /* Add any failover MGS nids */
417 while (ptr && ((*ptr == ':' ||
418 class_find_param(ptr, PARAM_MGSNODE, &ptr) == 0))) {
419 /* New failover node */
420 sprintf(niduuid, "%s_%x", mgcname, i);
422 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
423 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
424 niduuid, NULL, NULL, NULL);
431 rc = do_lcfg(mgcname, 0, LCFG_ADD_CONN,
432 niduuid, NULL, NULL, NULL);
440 lsi->lsi_lmd->lmd_mgs_failnodes = i;
442 obd = class_name2obd(mgcname);
444 CERROR("Can't find mgcobd %s\n", mgcname);
445 GOTO(out_free, rc = -ENOTCONN);
448 rc = obd_set_info_async(NULL, obd->obd_self_export,
449 strlen(KEY_MGSSEC), KEY_MGSSEC,
450 strlen(mgssec), mgssec, NULL);
454 /* Keep a refcount of servers/clients who started with "mount",
455 so we know when we can get rid of the mgc. */
456 atomic_set(&obd->u.cli.cl_mgc_refcount, 1);
458 /* We connect to the MGS at setup, and don't disconnect until cleanup */
459 data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
460 OBD_CONNECT_FULL20 | OBD_CONNECT_IMP_RECOV |
461 OBD_CONNECT_LVB_TYPE | OBD_CONNECT_BULK_MBITS;
463 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
464 data->ocd_connect_flags |= OBD_CONNECT_MNE_SWAB;
467 if (lmd_is_client(lsi->lsi_lmd) &&
468 lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)
469 data->ocd_connect_flags &= ~OBD_CONNECT_IMP_RECOV;
470 data->ocd_version = LUSTRE_VERSION_CODE;
471 rc = obd_connect(NULL, &exp, obd, &(obd->obd_uuid), data, NULL);
473 CERROR("connect failed %d\n", rc);
477 obd->u.cli.cl_mgc_mgsexp = exp;
480 /* Keep the mgc info in the sb. Note that many lsi's can point
484 mutex_unlock(&mgc_start_lock);
489 OBD_FREE(mgcname, len);
491 OBD_FREE(niduuid, len + 2);
495 static int lustre_stop_mgc(struct super_block *sb)
497 struct lustre_sb_info *lsi = s2lsi(sb);
498 struct obd_device *obd;
499 char *niduuid = NULL, *ptr = NULL;
500 int i, rc = 0, len = 0;
510 mutex_lock(&mgc_start_lock);
511 LASSERT(atomic_read(&obd->u.cli.cl_mgc_refcount) > 0);
512 if (!atomic_dec_and_test(&obd->u.cli.cl_mgc_refcount)) {
513 /* This is not fatal, every client that stops
514 will call in here. */
515 CDEBUG(D_MOUNT, "mgc still has %d references.\n",
516 atomic_read(&obd->u.cli.cl_mgc_refcount));
517 GOTO(out, rc = -EBUSY);
520 /* The MGC has no recoverable data in any case.
521 * force shotdown set in umount_begin */
522 obd->obd_no_recov = 1;
524 if (obd->u.cli.cl_mgc_mgsexp) {
525 /* An error is not fatal, if we are unable to send the
526 disconnect mgs ping evictor cleans up the export */
527 rc = obd_disconnect(obd->u.cli.cl_mgc_mgsexp);
529 CDEBUG(D_MOUNT, "disconnect failed %d\n", rc);
532 /* Save the obdname for cleaning the nid uuids, which are
534 len = strlen(obd->obd_name) + 6;
535 OBD_ALLOC(niduuid, len);
537 strcpy(niduuid, obd->obd_name);
538 ptr = niduuid + strlen(niduuid);
541 rc = class_manual_cleanup(obd);
545 /* Clean the nid uuids */
547 GOTO(out, rc = -ENOMEM);
549 for (i = 0; i < lsi->lsi_lmd->lmd_mgs_failnodes; i++) {
550 sprintf(ptr, "_%x", i);
551 rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_DEL_UUID,
552 niduuid, NULL, NULL, NULL);
554 CERROR("del MDC UUID %s failed: rc = %d\n",
559 OBD_FREE(niduuid, len);
561 /* class_import_put will get rid of the additional connections */
562 mutex_unlock(&mgc_start_lock);
566 /***************** lustre superblock **************/
568 static struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
570 struct lustre_sb_info *lsi;
576 OBD_ALLOC_PTR(lsi->lsi_lmd);
582 lsi->lsi_lmd->lmd_exclude_count = 0;
583 lsi->lsi_lmd->lmd_recovery_time_soft = 0;
584 lsi->lsi_lmd->lmd_recovery_time_hard = 0;
585 s2lsi_nocast(sb) = lsi;
586 /* we take 1 extra ref for our setup */
587 atomic_set(&lsi->lsi_mounts, 1);
589 /* Default umount style */
590 lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
591 INIT_LIST_HEAD(&lsi->lsi_lwp_list);
592 spin_lock_init(&lsi->lsi_lwp_lock);
597 static int lustre_free_lsi(struct super_block *sb)
599 struct lustre_sb_info *lsi = s2lsi(sb);
602 LASSERT(lsi != NULL);
603 CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
605 /* someone didn't call server_put_mount. */
606 LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
608 if (lsi->lsi_lmd != NULL) {
609 if (lsi->lsi_lmd->lmd_dev != NULL)
610 OBD_FREE(lsi->lsi_lmd->lmd_dev,
611 strlen(lsi->lsi_lmd->lmd_dev) + 1);
612 if (lsi->lsi_lmd->lmd_profile != NULL)
613 OBD_FREE(lsi->lsi_lmd->lmd_profile,
614 strlen(lsi->lsi_lmd->lmd_profile) + 1);
615 if (lsi->lsi_lmd->lmd_fileset != NULL)
616 OBD_FREE(lsi->lsi_lmd->lmd_fileset,
617 strlen(lsi->lsi_lmd->lmd_fileset) + 1);
618 if (lsi->lsi_lmd->lmd_mgssec != NULL)
619 OBD_FREE(lsi->lsi_lmd->lmd_mgssec,
620 strlen(lsi->lsi_lmd->lmd_mgssec) + 1);
621 if (lsi->lsi_lmd->lmd_opts != NULL)
622 OBD_FREE(lsi->lsi_lmd->lmd_opts,
623 strlen(lsi->lsi_lmd->lmd_opts) + 1);
624 if (lsi->lsi_lmd->lmd_exclude_count)
625 OBD_FREE(lsi->lsi_lmd->lmd_exclude,
626 sizeof(lsi->lsi_lmd->lmd_exclude[0]) *
627 lsi->lsi_lmd->lmd_exclude_count);
628 if (lsi->lsi_lmd->lmd_mgs != NULL)
629 OBD_FREE(lsi->lsi_lmd->lmd_mgs,
630 strlen(lsi->lsi_lmd->lmd_mgs) + 1);
631 if (lsi->lsi_lmd->lmd_osd_type != NULL)
632 OBD_FREE(lsi->lsi_lmd->lmd_osd_type,
633 strlen(lsi->lsi_lmd->lmd_osd_type) + 1);
634 if (lsi->lsi_lmd->lmd_params != NULL)
635 OBD_FREE(lsi->lsi_lmd->lmd_params, 4096);
637 OBD_FREE_PTR(lsi->lsi_lmd);
640 LASSERT(lsi->lsi_llsbi == NULL);
642 s2lsi_nocast(sb) = NULL;
647 /* The lsi has one reference for every server that is using the disk -
648 e.g. MDT, MGS, and potentially MGC */
649 int lustre_put_lsi(struct super_block *sb)
651 struct lustre_sb_info *lsi = s2lsi(sb);
654 LASSERT(lsi != NULL);
656 CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
657 if (atomic_dec_and_test(&lsi->lsi_mounts)) {
658 if (IS_SERVER(lsi) && lsi->lsi_osd_exp) {
659 lu_device_put(&lsi->lsi_dt_dev->dd_lu_dev);
660 lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt = NULL;
661 lsi->lsi_dt_dev = NULL;
662 obd_disconnect(lsi->lsi_osd_exp);
663 /* wait till OSD is gone */
664 obd_zombie_barrier();
673 * <FSNAME><SEPARATOR><TYPE><INDEX>
674 * FSNAME is between 1 and 8 characters (inclusive).
675 * Excluded characters are '/' and ':'
676 * SEPARATOR is either ':' or '-'
677 * TYPE: "OST", "MDT", etc.
678 * INDEX: Hex representation of the index
681 /** Get the fsname ("lustre") from the server name ("lustre-OST003F").
682 * @param [in] svname server name including type and index
683 * @param [out] fsname Buffer to copy filesystem name prefix into.
684 * Must have at least 'strlen(fsname) + 1' chars.
685 * @param [out] endptr if endptr isn't NULL it is set to end of fsname
688 int server_name2fsname(const char *svname, char *fsname, const char **endptr)
692 dash = svname + strnlen(svname, 8); /* max fsname length is 8 */
693 for (; dash > svname && *dash != '-' && *dash != ':'; dash--)
698 if (fsname != NULL) {
699 strncpy(fsname, svname, dash - svname);
700 fsname[dash - svname] = '\0';
708 EXPORT_SYMBOL(server_name2fsname);
711 * Get service name (svname) from string
713 * if endptr isn't NULL it is set to end of fsname *
715 int server_name2svname(const char *label, char *svname, const char **endptr,
721 /* We use server_name2fsname() just for parsing */
722 rc = server_name2fsname(label, NULL, &dash);
729 if (strlcpy(svname, dash + 1, svsize) >= svsize)
734 EXPORT_SYMBOL(server_name2svname);
737 * check server name is OST.
739 int server_name_is_ost(const char *svname)
744 /* We use server_name2fsname() just for parsing */
745 rc = server_name2fsname(svname, NULL, &dash);
751 if (strncmp(dash, "OST", 3) == 0)
755 EXPORT_SYMBOL(server_name_is_ost);
758 * Get the index from the target name MDTXXXX/OSTXXXX
759 * rc = server type, or rc < 0 on error
761 int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
763 const char *dash = tgtname;
767 if (strncmp(dash, "MDT", 3) == 0)
768 rc = LDD_F_SV_TYPE_MDT;
769 else if (strncmp(dash, "OST", 3) == 0)
770 rc = LDD_F_SV_TYPE_OST;
776 if (strncmp(dash, "all", 3) == 0) {
779 return rc | LDD_F_SV_ALL;
782 index = simple_strtoul(dash, (char **)endptr, 16);
787 EXPORT_SYMBOL(target_name2index);
789 /* Get the index from the obd name.
792 if endptr isn't NULL it is set to end of name */
793 int server_name2index(const char *svname, __u32 *idx, const char **endptr)
798 /* We use server_name2fsname() just for parsing */
799 rc = server_name2fsname(svname, NULL, &dash);
804 rc = target_name2index(dash, idx, endptr);
808 /* Account for -mdc after index that is possible when specifying mdt */
809 if (endptr != NULL && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
810 sizeof(LUSTRE_MDC_NAME)-1) == 0)
811 *endptr += sizeof(LUSTRE_MDC_NAME);
815 EXPORT_SYMBOL(server_name2index);
817 /*************** mount common betweeen server and client ***************/
820 int lustre_common_put_super(struct super_block *sb)
825 CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
827 /* Drop a ref to the MGC */
828 rc = lustre_stop_mgc(sb);
829 if (rc && (rc != -ENOENT)) {
831 CERROR("Can't stop MGC: %d\n", rc);
834 /* BUSY just means that there's some other obd that
835 needs the mgc. Let him clean it up. */
836 CDEBUG(D_MOUNT, "MGC still in use\n");
838 /* Drop a ref to the mounted disk */
843 EXPORT_SYMBOL(lustre_common_put_super);
845 static void lmd_print(struct lustre_mount_data *lmd)
849 PRINT_CMD(D_MOUNT, " mount data:\n");
850 if (lmd_is_client(lmd))
851 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
852 PRINT_CMD(D_MOUNT, "device: %s\n", lmd->lmd_dev);
853 PRINT_CMD(D_MOUNT, "flags: %x\n", lmd->lmd_flags);
856 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
858 if (lmd->lmd_recovery_time_soft)
859 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
860 lmd->lmd_recovery_time_soft);
862 if (lmd->lmd_recovery_time_hard)
863 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
864 lmd->lmd_recovery_time_hard);
866 for (i = 0; i < lmd->lmd_exclude_count; i++) {
867 PRINT_CMD(D_MOUNT, "exclude %d: OST%04x\n", i,
868 lmd->lmd_exclude[i]);
872 /* Is this server on the exclusion list */
873 int lustre_check_exclusion(struct super_block *sb, char *svname)
875 struct lustre_sb_info *lsi = s2lsi(sb);
876 struct lustre_mount_data *lmd = lsi->lsi_lmd;
881 rc = server_name2index(svname, &index, NULL);
882 if (rc != LDD_F_SV_TYPE_OST)
883 /* Only exclude OSTs */
886 CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
887 index, lmd->lmd_exclude_count, lmd->lmd_dev);
889 for(i = 0; i < lmd->lmd_exclude_count; i++) {
890 if (index == lmd->lmd_exclude[i]) {
891 CWARN("Excluding %s (on exclusion list)\n", svname);
898 /* mount -v -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
899 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
901 const char *s1 = ptr, *s2;
907 /* The shortest an ost name can be is 8 chars: -OST0000.
908 We don't actually know the fsname at this time, so in fact
909 a user could specify any fsname. */
910 devmax = strlen(ptr) / 8 + 1;
912 /* temp storage until we figure out how many we have */
913 OBD_ALLOC(exclude_list, sizeof(index) * devmax);
917 /* we enter this fn pointing at the '=' */
918 while (*s1 && *s1 != ' ' && *s1 != ',') {
920 rc = server_name2index(s1, &index, &s2);
922 CERROR("Can't parse server name '%s': rc = %d\n",
926 if (rc == LDD_F_SV_TYPE_OST)
927 exclude_list[lmd->lmd_exclude_count++] = index;
929 CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
930 (uint)(s2-s1), s1, rc);
932 /* now we are pointing at ':' (next exclude)
933 or ',' (end of excludes) */
934 if (lmd->lmd_exclude_count >= devmax)
937 if (rc >= 0) /* non-err */
940 if (lmd->lmd_exclude_count) {
941 /* permanent, freed in lustre_free_lsi */
942 OBD_ALLOC(lmd->lmd_exclude, sizeof(index) *
943 lmd->lmd_exclude_count);
944 if (lmd->lmd_exclude) {
945 memcpy(lmd->lmd_exclude, exclude_list,
946 sizeof(index) * lmd->lmd_exclude_count);
949 lmd->lmd_exclude_count = 0;
952 OBD_FREE(exclude_list, sizeof(index) * devmax);
956 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
961 if (lmd->lmd_mgssec != NULL) {
962 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
963 lmd->lmd_mgssec = NULL;
966 tail = strchr(ptr, ',');
968 length = strlen(ptr);
972 OBD_ALLOC(lmd->lmd_mgssec, length + 1);
973 if (lmd->lmd_mgssec == NULL)
976 memcpy(lmd->lmd_mgssec, ptr, length);
977 lmd->lmd_mgssec[length] = '\0';
981 static int lmd_parse_string(char **handle, char *ptr)
986 if ((handle == NULL) || (ptr == NULL))
989 if (*handle != NULL) {
990 OBD_FREE(*handle, strlen(*handle) + 1);
994 tail = strchr(ptr, ',');
996 length = strlen(ptr);
1000 OBD_ALLOC(*handle, length + 1);
1001 if (*handle == NULL)
1004 memcpy(*handle, ptr, length);
1005 (*handle)[length] = '\0';
1010 /* Collect multiple values for mgsnid specifiers */
1011 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
1019 /* Find end of nidlist */
1020 while (class_parse_nid_quiet(tail, &nid, &tail) == 0) {}
1021 length = tail - *ptr;
1023 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
1027 if (lmd->lmd_mgs != NULL)
1028 oldlen = strlen(lmd->lmd_mgs) + 1;
1030 OBD_ALLOC(mgsnid, oldlen + length + 1);
1034 if (lmd->lmd_mgs != NULL) {
1035 /* Multiple mgsnid= are taken to mean failover locations */
1036 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
1037 mgsnid[oldlen - 1] = ':';
1038 OBD_FREE(lmd->lmd_mgs, oldlen);
1040 memcpy(mgsnid + oldlen, *ptr, length);
1041 mgsnid[oldlen + length] = '\0';
1042 lmd->lmd_mgs = mgsnid;
1049 * Find the first delimiter (comma or colon) from the specified \a buf and
1050 * make \a *endh point to the string starting with the delimiter. The commas
1051 * in expression list [...] will be skipped.
1053 * \param[in] buf a delimiter-separated string
1054 * \param[in] endh a pointer to a pointer that will point to the string
1055 * starting with the delimiter
1057 * \retval 0 if delimiter is found
1058 * \retval 1 if delimiter is not found
1060 static int lmd_find_delimiter(char *buf, char **endh)
1068 while (*c != '\0') {
1074 if ((*c == ',' || *c == ':') && skip == 0) {
1087 * Find the first valid string delimited by comma or colon from the specified
1088 * \a buf and parse it to see whether it's a valid nid list. If yes, \a *endh
1089 * will point to the next string starting with the delimiter.
1091 * \param[in] buf a delimiter-separated string
1092 * \param[in] endh a pointer to a pointer that will point to the string
1093 * starting with the delimiter
1095 * \retval 0 if the string is a valid nid list
1096 * \retval 1 if the string is not a valid nid list
1098 static int lmd_parse_nidlist(char *buf, char **endh)
1100 struct list_head nidlist;
1107 while (*buf == ',' || *buf == ':')
1109 if (*buf == ' ' || *buf == '/' || *buf == '\0')
1112 if (lmd_find_delimiter(buf, &endp) != 0)
1113 endp = buf + strlen(buf);
1118 INIT_LIST_HEAD(&nidlist);
1119 if (cfs_parse_nidlist(buf, strlen(buf), &nidlist) <= 0)
1121 cfs_free_nidlist(&nidlist);
1131 /** Parse mount line options
1132 * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
1133 * dev is passed as device=uml1:/lustre by mount.lustre
1135 static int lmd_parse(char *options, struct lustre_mount_data *lmd)
1137 char *s1, *s2, *s3, *devname = NULL;
1138 struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1144 LCONSOLE_ERROR_MSG(0x162, "Missing mount data: check that "
1145 "/sbin/mount.lustre is installed.\n");
1149 /* Options should be a string - try to detect old lmd data */
1150 if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1151 LCONSOLE_ERROR_MSG(0x163, "You're using an old version of "
1152 "/sbin/mount.lustre. Please install "
1153 "version %s\n", LUSTRE_VERSION_STRING);
1156 lmd->lmd_magic = LMD_MAGIC;
1158 OBD_ALLOC(lmd->lmd_params, LMD_PARAMS_MAXLEN);
1159 if (lmd->lmd_params == NULL)
1161 lmd->lmd_params[0] = '\0';
1163 /* Set default flags here */
1168 int time_min = OBD_RECOVERY_TIME_MIN;
1170 /* Skip whitespace and extra commas */
1171 while (*s1 == ' ' || *s1 == ',')
1175 /* Client options are parsed in ll_options: eg. flock,
1178 /* Parse non-ldiskfs options here. Rather than modifying
1179 ldiskfs, we just zero these out here */
1180 if (strncmp(s1, "abort_recov", 11) == 0) {
1181 lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
1183 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
1184 lmd->lmd_recovery_time_soft = max_t(int,
1185 simple_strtoul(s1 + 19, NULL, 10), time_min);
1187 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
1188 lmd->lmd_recovery_time_hard = max_t(int,
1189 simple_strtoul(s1 + 19, NULL, 10), time_min);
1191 } else if (strncmp(s1, "noir", 4) == 0) {
1192 lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
1194 } else if (strncmp(s1, "nosvc", 5) == 0) {
1195 lmd->lmd_flags |= LMD_FLG_NOSVC;
1197 } else if (strncmp(s1, "nomgs", 5) == 0) {
1198 lmd->lmd_flags |= LMD_FLG_NOMGS;
1200 } else if (strncmp(s1, "noscrub", 7) == 0) {
1201 lmd->lmd_flags |= LMD_FLG_NOSCRUB;
1203 } else if (strncmp(s1, "skip_lfsck", 10) == 0) {
1204 lmd->lmd_flags |= LMD_FLG_SKIP_LFSCK;
1206 } else if (strncmp(s1, PARAM_MGSNODE,
1207 sizeof(PARAM_MGSNODE) - 1) == 0) {
1208 s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
1209 /* Assume the next mount opt is the first
1210 invalid nid we get to. */
1211 rc = lmd_parse_mgs(lmd, &s2);
1216 } else if (strncmp(s1, "writeconf", 9) == 0) {
1217 lmd->lmd_flags |= LMD_FLG_WRITECONF;
1219 } else if (strncmp(s1, "update", 6) == 0) {
1220 lmd->lmd_flags |= LMD_FLG_UPDATE;
1222 } else if (strncmp(s1, "virgin", 6) == 0) {
1223 lmd->lmd_flags |= LMD_FLG_VIRGIN;
1225 } else if (strncmp(s1, "noprimnode", 10) == 0) {
1226 lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
1228 } else if (strncmp(s1, "mgssec=", 7) == 0) {
1229 rc = lmd_parse_mgssec(lmd, s1 + 7);
1233 /* ost exclusion list */
1234 } else if (strncmp(s1, "exclude=", 8) == 0) {
1235 rc = lmd_make_exclusion(lmd, s1 + 7);
1239 } else if (strncmp(s1, "mgs", 3) == 0) {
1241 lmd->lmd_flags |= LMD_FLG_MGS;
1243 } else if (strncmp(s1, "svname=", 7) == 0) {
1244 rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
1248 } else if (strncmp(s1, "param=", 6) == 0) {
1249 size_t length, params_length;
1251 if (lmd_find_delimiter(s1 + 6, &tail) != 0)
1252 length = strlen(s1);
1254 char *param_str = tail + 1;
1255 int supplementary = 1;
1256 while (lmd_parse_nidlist(param_str,
1260 length = param_str - s1 - supplementary;
1263 params_length = strlen(lmd->lmd_params);
1264 if (params_length + length + 1 >= LMD_PARAMS_MAXLEN)
1266 strncat(lmd->lmd_params, s1 + 6, length);
1267 lmd->lmd_params[params_length + length] = '\0';
1268 strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
1269 s3 = s1 + 6 + length;
1271 } else if (strncmp(s1, "osd=", 4) == 0) {
1272 rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
1277 /* Linux 2.4 doesn't pass the device, so we stuck it at the
1278 end of the options. */
1279 else if (strncmp(s1, "device=", 7) == 0) {
1281 /* terminate options right before device. device
1282 must be the last one. */
1288 s2 = strchr(s3, ',');
1296 memmove(s1, s2, strlen(s2) + 1);
1302 LCONSOLE_ERROR_MSG(0x164, "Can't find the device name "
1303 "(need mount option 'device=...')\n");
1307 s1 = strstr(devname, ":/");
1310 lmd->lmd_flags |= LMD_FLG_CLIENT;
1311 /* Remove leading /s from fsname */
1312 while (*++s1 == '/')
1315 while (*s2 != '/' && *s2 != '\0')
1317 /* Freed in lustre_free_lsi */
1318 OBD_ALLOC(lmd->lmd_profile, s2 - s1 + 8);
1319 if (!lmd->lmd_profile)
1322 strncat(lmd->lmd_profile, s1, s2 - s1);
1323 strncat(lmd->lmd_profile, "-client", 7);
1326 s2 = s1 + strlen(s1) - 1;
1327 /* Remove padding /s from fileset */
1331 OBD_ALLOC(lmd->lmd_fileset, s2 - s1 + 2);
1332 if (lmd->lmd_fileset == NULL) {
1333 OBD_FREE(lmd->lmd_profile,
1334 strlen(lmd->lmd_profile) + 1);
1337 strncat(lmd->lmd_fileset, s1, s2 - s1 + 1);
1341 /* Freed in lustre_free_lsi */
1342 OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
1345 strcpy(lmd->lmd_dev, devname);
1347 /* Save mount options */
1348 s1 = options + strlen(options) - 1;
1349 while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1351 if (*options != 0) {
1352 /* Freed in lustre_free_lsi */
1353 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1356 strcpy(lmd->lmd_opts, options);
1360 lmd->lmd_magic = LMD_MAGIC;
1365 CERROR("Bad mount options %s\n", options);
1369 struct lustre_mount_data2 {
1371 struct vfsmount *lmd2_mnt;
1374 /** This is the entry point for the mount call into Lustre.
1375 * This is called when a server or client is mounted,
1376 * and this is where we start setting things up.
1377 * @param data Mount options (e.g. -o flock,abort_recov)
1379 static int lustre_fill_super(struct super_block *sb, void *data, int silent)
1381 struct lustre_mount_data *lmd;
1382 struct lustre_mount_data2 *lmd2 = data;
1383 struct lustre_sb_info *lsi;
1387 CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
1389 lsi = lustre_init_lsi(sb);
1395 * Disable lockdep during mount, because mount locking patterns are
1401 * LU-639: the obd cleanup of last mount may not finish yet, wait here.
1403 obd_zombie_barrier();
1405 /* Figure out the lmd from the mount options */
1406 if (lmd_parse((char *)(lmd2->lmd2_data), lmd)) {
1408 GOTO(out, rc = -EINVAL);
1411 if (lmd_is_client(lmd)) {
1412 CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
1413 if (client_fill_super == NULL)
1414 request_module("lustre");
1415 if (client_fill_super == NULL) {
1416 LCONSOLE_ERROR_MSG(0x165, "Nothing registered for "
1417 "client mount! Is the 'lustre' "
1418 "module loaded?\n");
1422 rc = lustre_start_mgc(sb);
1424 lustre_common_put_super(sb);
1427 /* Connect and start */
1428 /* (should always be ll_fill_super) */
1429 rc = (*client_fill_super)(sb, lmd2->lmd2_mnt);
1430 /* c_f_s will call lustre_common_put_super on failure */
1433 #ifdef HAVE_SERVER_SUPPORT
1434 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
1435 rc = server_fill_super(sb);
1436 /* s_f_s calls lustre_start_mgc after the mount because we need
1437 the MGS nids which are stored on disk. Plus, we may
1438 need to start the MGS first. */
1439 /* s_f_s will call server_put_super on failure */
1441 CERROR("This is client-side-only module, "
1442 "cannot handle server mount.\n");
1447 /* If error happens in fill_super() call, @lsi will be killed there.
1448 * This is why we do not put it here. */
1452 CERROR("Unable to mount %s (%d)\n",
1453 s2lsi(sb) ? lmd->lmd_dev : "", rc);
1455 CDEBUG(D_SUPER, "Mount %s complete\n",
1463 /* We can't call ll_fill_super by name because it lives in a module that
1464 must be loaded after this one. */
1465 void lustre_register_client_fill_super(int (*cfs)(struct super_block *sb,
1466 struct vfsmount *mnt))
1468 client_fill_super = cfs;
1470 EXPORT_SYMBOL(lustre_register_client_fill_super);
1472 void lustre_register_kill_super_cb(void (*cfs)(struct super_block *sb))
1474 kill_super_cb = cfs;
1476 EXPORT_SYMBOL(lustre_register_kill_super_cb);
1478 /***************** FS registration ******************/
1479 #ifdef HAVE_FSTYPE_MOUNT
1480 static struct dentry *lustre_mount(struct file_system_type *fs_type, int flags,
1481 const char *devname, void *data)
1483 struct lustre_mount_data2 lmd2 = { data, NULL };
1485 return mount_nodev(fs_type, flags, &lmd2, lustre_fill_super);
1488 static int lustre_get_sb(struct file_system_type *fs_type, int flags,
1489 const char *devname, void *data, struct vfsmount *mnt)
1491 struct lustre_mount_data2 lmd2 = { data, mnt };
1493 return get_sb_nodev(fs_type, flags, &lmd2, lustre_fill_super, mnt);
1497 static void lustre_kill_super(struct super_block *sb)
1499 struct lustre_sb_info *lsi = s2lsi(sb);
1501 if (kill_super_cb && lsi && !IS_SERVER(lsi))
1502 (*kill_super_cb)(sb);
1504 kill_anon_super(sb);
1507 /** Register the "lustre" fs type
1509 static struct file_system_type lustre_fs_type = {
1510 .owner = THIS_MODULE,
1512 #ifdef HAVE_FSTYPE_MOUNT
1513 .mount = lustre_mount,
1515 .get_sb = lustre_get_sb,
1517 .kill_sb = lustre_kill_super,
1518 .fs_flags = FS_REQUIRES_DEV | FS_HAS_FIEMAP | FS_RENAME_DOES_D_MOVE,
1520 MODULE_ALIAS_FS("lustre");
1522 int lustre_register_fs(void)
1524 return register_filesystem(&lustre_fs_type);
1527 int lustre_unregister_fs(void)
1529 return unregister_filesystem(&lustre_fs_type);