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, 2017, 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 <linux/random.h>
47 #include <libcfs/linux/linux-uuid.h>
48 #include <linux/version.h>
49 #include <lustre_log.h>
50 #include <lustre_disk.h>
51 #include <uapi/linux/lustre/lustre_param.h>
52 #include <libcfs/crypto/llcrypt.h>
54 /**************** config llog ********************/
57 * Get a config log from the MGS and process it.
58 * This func is called for both clients and servers.
59 * Continue to process new statements appended to the logs
60 * (whenever the config lock is revoked) until lustre_end_log
63 * @param sb The superblock is used by the MGC to write to the local copy of
65 * @param logname The name of the llog to replicate from the MGS
66 * @param cfg Since the same MGC may be used to follow multiple config logs
67 * (e.g. ost1, ost2, client), the config_llog_instance keeps the state for
68 * this log, and is added to the mgc's list of logs to follow.
70 int lustre_process_log(struct super_block *sb, char *logname,
71 struct config_llog_instance *cfg)
73 struct lustre_cfg *lcfg;
74 struct lustre_cfg_bufs *bufs;
75 struct lustre_sb_info *lsi = s2lsi(sb);
76 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 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
95 GOTO(out, rc = -ENOMEM);
96 lustre_cfg_init(lcfg, LCFG_LOG_START, bufs);
98 rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
99 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
104 LCONSOLE_ERROR_MSG(0x15b,
105 "%s: Configuration from log %s failed from MGS %d. Check client and MGS are on compatible version.\n",
106 mgc->obd_name, logname, rc);
108 LCONSOLE_ERROR_MSG(0x15c,
109 "%s: Confguration from log %s failed from MGS %d. Communication error between node & MGS, a bad configuration, or other errors. See syslog for more info\n",
110 mgc->obd_name, logname, rc);
112 /* class_obd_list(); */
115 EXPORT_SYMBOL(lustre_process_log);
117 /* Stop watching this config log for updates */
118 int lustre_end_log(struct super_block *sb, char *logname,
119 struct config_llog_instance *cfg)
121 struct lustre_cfg *lcfg;
122 struct lustre_cfg_bufs bufs;
123 struct lustre_sb_info *lsi = s2lsi(sb);
124 struct obd_device *mgc = lsi->lsi_mgc;
132 /* mgc_process_config */
133 lustre_cfg_bufs_reset(&bufs, mgc->obd_name);
134 lustre_cfg_bufs_set_string(&bufs, 1, logname);
136 lustre_cfg_bufs_set(&bufs, 2, cfg, sizeof(*cfg));
137 OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
140 lustre_cfg_init(lcfg, LCFG_LOG_END, &bufs);
141 rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
142 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
145 EXPORT_SYMBOL(lustre_end_log);
147 /**************** OBD start *******************/
150 * lustre_cfg_bufs are a holdover from 1.4; we can still set these up from
151 * lctl (and do for echo cli/srv.
153 static int do_lcfg(char *cfgname, lnet_nid_t nid, int cmd,
154 char *s1, char *s2, char *s3, char *s4)
156 struct lustre_cfg_bufs bufs;
157 struct lustre_cfg *lcfg = NULL;
160 CDEBUG(D_TRACE, "lcfg %s %#x %s %s %s %s\n", cfgname,
161 cmd, s1, s2, s3, s4);
163 lustre_cfg_bufs_reset(&bufs, cfgname);
165 lustre_cfg_bufs_set_string(&bufs, 1, s1);
167 lustre_cfg_bufs_set_string(&bufs, 2, s2);
169 lustre_cfg_bufs_set_string(&bufs, 3, s3);
171 lustre_cfg_bufs_set_string(&bufs, 4, s4);
173 OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
176 lustre_cfg_init(lcfg, cmd, &bufs);
177 lcfg->lcfg_nid = nid;
178 rc = class_process_config(lcfg);
179 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
184 * Call class_attach and class_setup. These methods in turn call
185 * OBD type-specific methods.
187 int lustre_start_simple(char *obdname, char *type, char *uuid,
188 char *s1, char *s2, char *s3, char *s4)
192 CDEBUG(D_MOUNT, "Starting OBD %s (typ=%s)\n", obdname, type);
194 rc = do_lcfg(obdname, 0, LCFG_ATTACH, type, uuid, NULL, NULL);
196 CERROR("%s attach error %d\n", obdname, rc);
199 rc = do_lcfg(obdname, 0, LCFG_SETUP, s1, s2, s3, s4);
201 CERROR("%s setup error %d\n", obdname, rc);
202 do_lcfg(obdname, 0, LCFG_DETACH, NULL, NULL, NULL, NULL);
207 static DEFINE_MUTEX(mgc_start_lock);
210 * Set up a MGC OBD to process startup logs
212 * \param sb [in] super block of the MGC OBD
214 * \retval 0 success, otherwise error code
216 int lustre_start_mgc(struct super_block *sb)
218 struct obd_connect_data *data = NULL;
219 struct lustre_sb_info *lsi = s2lsi(sb);
220 struct obd_device *obd;
221 struct obd_export *exp;
222 struct obd_uuid *uuid = NULL;
225 char nidstr[LNET_NIDSTR_SIZE];
226 char *mgcname = NULL, *niduuid = NULL, *mgssec = NULL;
228 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 struct lnet_process_id id;
245 while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
246 if (id.nid == LNET_NID_LO_0)
253 } else { /* client */
254 /* Use NIDs from mount line: uml1,1@elan:uml2,2@elan:/lustre */
255 ptr = lsi->lsi_lmd->lmd_dev;
256 if (class_parse_nid(ptr, &nid, &ptr) == 0)
260 CERROR("No valid MGS NIDs found.\n");
264 mutex_lock(&mgc_start_lock);
266 libcfs_nid2str_r(nid, nidstr, sizeof(nidstr));
267 len = strlen(LUSTRE_MGC_OBDNAME) + strlen(nidstr) + 1;
268 OBD_ALLOC(mgcname, len);
269 OBD_ALLOC(niduuid, len + 2);
270 if (mgcname == NULL || niduuid == NULL)
271 GOTO(out_free, rc = -ENOMEM);
272 snprintf(mgcname, len, "%s%s", LUSTRE_MGC_OBDNAME, nidstr);
274 mgssec = lsi->lsi_lmd->lmd_mgssec ? lsi->lsi_lmd->lmd_mgssec : "";
278 GOTO(out_free, rc = -ENOMEM);
280 obd = class_name2obd(mgcname);
281 if (obd && !obd->obd_stopping) {
284 rc = obd_set_info_async(NULL, obd->obd_self_export,
285 strlen(KEY_MGSSEC), KEY_MGSSEC,
286 strlen(mgssec), mgssec, NULL);
290 /* Re-using an existing MGC */
291 atomic_inc(&obd->u.cli.cl_mgc_refcount);
293 /* IR compatibility check, only for clients */
294 if (lmd_is_client(lsi->lsi_lmd)) {
296 int vallen = sizeof(*data);
297 __u32 *flags = &lsi->lsi_lmd->lmd_flags;
299 rc = obd_get_info(NULL, obd->obd_self_export,
300 strlen(KEY_CONN_DATA), KEY_CONN_DATA,
303 has_ir = OCD_HAS_FLAG(data, IMP_RECOV);
304 if (has_ir ^ !(*flags & LMD_FLG_NOIR)) {
305 /* LMD_FLG_NOIR is for test purpose only */
307 "Mounting client with IR setting not compatible with current MGC. Using MGC setting that is IR %s",
308 has_ir ? "enabled" : "disabled");
310 *flags &= ~LMD_FLG_NOIR;
312 *flags |= LMD_FLG_NOIR;
318 * If we are restarting the MGS, don't try to keep the MGC's
319 * old connection, or registration will fail.
322 CDEBUG(D_MOUNT, "New MGS with live MGC\n");
327 * Try all connections, but only once (again).
328 * We don't want to block another target from starting
329 * (using its local copy of the log), but we do want to connect
330 * if at all possible.
333 CDEBUG(D_MOUNT, "%s:Set MGC reconnect %d\n", mgcname, recov_bk);
334 rc = obd_set_info_async(NULL, obd->obd_self_export,
335 sizeof(KEY_INIT_RECOV_BACKUP),
336 KEY_INIT_RECOV_BACKUP,
337 sizeof(recov_bk), &recov_bk, NULL);
341 CDEBUG(D_MOUNT, "Start MGC '%s'\n", mgcname);
343 /* Add the primary NIDs for the MGS */
345 snprintf(niduuid, len + 2, "%s_%x", mgcname, i);
346 if (IS_SERVER(lsi)) {
347 ptr = lsi->lsi_lmd->lmd_mgs;
348 CDEBUG(D_MOUNT, "mgs NIDs %s.\n", ptr);
350 /* Use local NIDs (including LO) */
351 struct lnet_process_id id;
353 while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
354 rc = do_lcfg(mgcname, id.nid, LCFG_ADD_UUID,
355 niduuid, NULL, NULL, NULL);
358 /* Use mgsnode= nids */
359 /* mount -o mgsnode=nid */
360 if (lsi->lsi_lmd->lmd_mgs) {
361 ptr = lsi->lsi_lmd->lmd_mgs;
362 } else if (class_find_param(ptr, PARAM_MGSNODE,
364 CERROR("No MGS NIDs given.\n");
365 GOTO(out_free, rc = -EINVAL);
368 * Add primary MGS NID(s).
369 * Multiple NIDs on one MGS node are separated
372 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
373 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
374 niduuid, NULL, NULL, NULL);
377 /* Stop at the first failover NID */
382 } else { /* client */
383 /* Use NIDs from mount line: uml1,1@elan:uml2,2@elan:/lustre */
384 ptr = lsi->lsi_lmd->lmd_dev;
385 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
386 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
387 niduuid, NULL, NULL, NULL);
390 /* Stop at the first failover NID */
396 CERROR("No valid MGS NIDs found.\n");
397 GOTO(out_free, rc = -EINVAL);
399 lsi->lsi_lmd->lmd_mgs_failnodes = 1;
401 /* Random uuid for MGC allows easier reconnects */
404 GOTO(out_free, rc = -ENOMEM);
406 generate_random_uuid(uuidc.b);
407 snprintf(uuid->uuid, sizeof(*uuid), "%pU", uuidc.b);
410 rc = lustre_start_simple(mgcname, LUSTRE_MGC_NAME,
411 (char *)uuid->uuid, LUSTRE_MGS_OBDNAME,
412 niduuid, NULL, NULL);
416 /* Add any failover MGS NIDs */
418 while (ptr && ((*ptr == ':' ||
419 class_find_param(ptr, PARAM_MGSNODE, &ptr) == 0))) {
420 /* New failover node */
421 sprintf(niduuid, "%s_%x", mgcname, i);
423 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
424 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
425 niduuid, NULL, NULL, NULL);
432 rc = do_lcfg(mgcname, 0, LCFG_ADD_CONN,
433 niduuid, NULL, NULL, NULL);
441 lsi->lsi_lmd->lmd_mgs_failnodes = i;
443 obd = class_name2obd(mgcname);
445 CERROR("Can't find mgcobd %s\n", mgcname);
446 GOTO(out_free, rc = -ENOTCONN);
449 rc = obd_set_info_async(NULL, obd->obd_self_export,
450 strlen(KEY_MGSSEC), KEY_MGSSEC,
451 strlen(mgssec), mgssec, NULL);
456 * Keep a refcount of servers/clients who started with "mount",
457 * so we know when we can get rid of the mgc.
459 atomic_set(&obd->u.cli.cl_mgc_refcount, 1);
461 /* We connect to the MGS at setup, and don't disconnect until cleanup */
462 data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
463 OBD_CONNECT_FULL20 | OBD_CONNECT_IMP_RECOV |
464 OBD_CONNECT_LVB_TYPE |
465 OBD_CONNECT_BULK_MBITS | OBD_CONNECT_BARRIER;
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, uuid, data, NULL);
473 CERROR("connect failed %d\n", rc);
477 obd->u.cli.cl_mgc_mgsexp = exp;
481 * Keep the MGC info in the sb. Note that many lsi's can point
486 mutex_unlock(&mgc_start_lock);
493 OBD_FREE(mgcname, len);
495 OBD_FREE(niduuid, len + 2);
498 EXPORT_SYMBOL(lustre_start_mgc);
500 static int lustre_stop_mgc(struct super_block *sb)
502 struct lustre_sb_info *lsi = s2lsi(sb);
503 struct obd_device *obd;
504 char *niduuid = NULL, *ptr = NULL;
505 int i, rc = 0, len = 0;
516 mutex_lock(&mgc_start_lock);
517 LASSERT(atomic_read(&obd->u.cli.cl_mgc_refcount) > 0);
518 if (!atomic_dec_and_test(&obd->u.cli.cl_mgc_refcount)) {
520 * This is not fatal, every client that stops
523 CDEBUG(D_MOUNT, "MGC still has %d references.\n",
524 atomic_read(&obd->u.cli.cl_mgc_refcount));
525 GOTO(out, rc = -EBUSY);
529 * The MGC has no recoverable data in any case.
530 * force shotdown set in umount_begin
532 obd->obd_no_recov = 1;
534 if (obd->u.cli.cl_mgc_mgsexp) {
536 * An error is not fatal, if we are unable to send the
537 * disconnect mgs ping evictor cleans up the export
539 rc = obd_disconnect(obd->u.cli.cl_mgc_mgsexp);
541 CDEBUG(D_MOUNT, "disconnect failed %d\n", rc);
545 * Save the obdname for cleaning the nid uuids, which are
548 len = strlen(obd->obd_name) + 6;
549 OBD_ALLOC(niduuid, len);
551 strcpy(niduuid, obd->obd_name);
552 ptr = niduuid + strlen(niduuid);
555 rc = class_manual_cleanup(obd);
559 /* Clean the nid uuids */
561 GOTO(out, rc = -ENOMEM);
563 for (i = 0; i < lsi->lsi_lmd->lmd_mgs_failnodes; i++) {
564 sprintf(ptr, "_%x", i);
565 rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_DEL_UUID,
566 niduuid, NULL, NULL, NULL);
568 CERROR("del MDC UUID %s failed: rc = %d\n",
573 OBD_FREE(niduuid, len);
575 /* class_import_put will get rid of the additional connections */
576 mutex_unlock(&mgc_start_lock);
580 /***************** lustre superblock **************/
582 struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
584 struct lustre_sb_info *lsi;
591 OBD_ALLOC_PTR(lsi->lsi_lmd);
597 s2lsi_nocast(sb) = lsi;
598 /* we take 1 extra ref for our setup */
599 atomic_set(&lsi->lsi_mounts, 1);
601 /* Default umount style */
602 lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
603 INIT_LIST_HEAD(&lsi->lsi_lwp_list);
604 mutex_init(&lsi->lsi_lwp_mutex);
608 EXPORT_SYMBOL(lustre_init_lsi);
610 static int lustre_free_lsi(struct super_block *sb)
612 struct lustre_sb_info *lsi = s2lsi(sb);
616 LASSERT(lsi != NULL);
617 CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
619 /* someone didn't call server_put_mount. */
620 LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
623 if (lsi->lsi_lmd != NULL) {
624 if (lsi->lsi_lmd->lmd_dev != NULL)
625 OBD_FREE(lsi->lsi_lmd->lmd_dev,
626 strlen(lsi->lsi_lmd->lmd_dev) + 1);
627 if (lsi->lsi_lmd->lmd_profile != NULL)
628 OBD_FREE(lsi->lsi_lmd->lmd_profile,
629 strlen(lsi->lsi_lmd->lmd_profile) + 1);
630 if (lsi->lsi_lmd->lmd_fileset != NULL)
631 OBD_FREE(lsi->lsi_lmd->lmd_fileset,
632 strlen(lsi->lsi_lmd->lmd_fileset) + 1);
633 if (lsi->lsi_lmd->lmd_mgssec != NULL)
634 OBD_FREE(lsi->lsi_lmd->lmd_mgssec,
635 strlen(lsi->lsi_lmd->lmd_mgssec) + 1);
636 if (lsi->lsi_lmd->lmd_opts != NULL)
637 OBD_FREE(lsi->lsi_lmd->lmd_opts,
638 strlen(lsi->lsi_lmd->lmd_opts) + 1);
639 if (lsi->lsi_lmd->lmd_exclude_count)
640 OBD_FREE(lsi->lsi_lmd->lmd_exclude,
641 sizeof(lsi->lsi_lmd->lmd_exclude[0]) *
642 lsi->lsi_lmd->lmd_exclude_count);
643 if (lsi->lsi_lmd->lmd_mgs != NULL)
644 OBD_FREE(lsi->lsi_lmd->lmd_mgs,
645 strlen(lsi->lsi_lmd->lmd_mgs) + 1);
646 if (lsi->lsi_lmd->lmd_osd_type != NULL)
647 OBD_FREE(lsi->lsi_lmd->lmd_osd_type,
648 strlen(lsi->lsi_lmd->lmd_osd_type) + 1);
649 if (lsi->lsi_lmd->lmd_params != NULL)
650 OBD_FREE(lsi->lsi_lmd->lmd_params, 4096);
651 if (lsi->lsi_lmd->lmd_nidnet != NULL)
652 OBD_FREE(lsi->lsi_lmd->lmd_nidnet,
653 strlen(lsi->lsi_lmd->lmd_nidnet) + 1);
655 OBD_FREE_PTR(lsi->lsi_lmd);
658 LASSERT(lsi->lsi_llsbi == NULL);
660 s2lsi_nocast(sb) = NULL;
666 * The lsi has one reference for every server that is using the disk -
667 * e.g. MDT, MGS, and potentially MGC
669 int lustre_put_lsi(struct super_block *sb)
671 struct lustre_sb_info *lsi = s2lsi(sb);
675 LASSERT(lsi != NULL);
677 CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
678 if (atomic_dec_and_test(&lsi->lsi_mounts)) {
679 if (IS_SERVER(lsi) && lsi->lsi_osd_exp) {
680 lu_device_put(&lsi->lsi_dt_dev->dd_lu_dev);
681 lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt = NULL;
682 lsi->lsi_dt_dev = NULL;
683 obd_disconnect(lsi->lsi_osd_exp);
684 /* wait till OSD is gone */
685 obd_zombie_barrier();
692 EXPORT_SYMBOL(lustre_put_lsi);
695 * The goal of this function is to extract the file system name
696 * from the OBD name. This can come in two flavors. One is
697 * fsname-MDTXXXX or fsname-XXXXXXX were X is a hexadecimal
698 * number. In both cases we should return fsname. If it is
699 * not a valid OBD name it is assumed to be the file system
702 void obdname2fsname(const char *tgt, char *fsname, size_t buflen)
709 * First we have to see if the @tgt has '-' at all. It is
710 * valid for the user to request something like
711 * lctl set_param -P llite.lustre*.xattr_cache=0
713 ptr = strrchr(tgt, '-');
715 /* No '-' means it could end in '*' */
716 ptr = strchr(tgt, '*');
718 /* No '*' either. Assume tgt = fsname */
726 /* tgt format fsname-MDT0000-* */
727 if ((!strncmp(ptr, "-MDT", 4) ||
728 !strncmp(ptr, "-OST", 4)) &&
729 (isxdigit(ptr[4]) && isxdigit(ptr[5]) &&
730 isxdigit(ptr[6]) && isxdigit(ptr[7]))) {
736 * tgt_format fsname-cli'dev'-'uuid' except for the llite case
737 * which are named fsname-'uuid'. Examples:
739 * lustre-clilov-ffff88104db5b800
740 * lustre-ffff88104db5b800 (for llite device)
742 * The length of the OBD uuid can vary on different platforms.
743 * This test if any invalid characters are in string. Allow
744 * wildcards with '*' character.
747 if (!strspn(ptr, "0123456789abcdefABCDEF*")) {
753 * Now that we validated the device name lets extract the
754 * file system name. Most of the names in this class will
755 * have '-cli' in its name which needs to be dropped. If
756 * it doesn't have '-cli' then its a llite device which
757 * ptr already points to the start of the uuid string.
759 tmp = strstr(tgt, "-cli");
766 len = min_t(size_t, len, LUSTRE_MAXFSNAME);
767 snprintf(fsname, buflen, "%.*s", (int)len, tgt);
771 EXPORT_SYMBOL(obdname2fsname);
775 * <FSNAME><SEPARATOR><TYPE><INDEX>
776 * FSNAME is between 1 and 8 characters (inclusive).
777 * Excluded characters are '/' and ':'
778 * SEPARATOR is either ':' or '-'
779 * TYPE: "OST", "MDT", etc.
780 * INDEX: Hex representation of the index
784 * Get the fsname ("lustre") from the server name ("lustre-OST003F").
785 * @param [in] svname server name including type and index
786 * @param [out] fsname Buffer to copy filesystem name prefix into.
787 * Must have at least 'strlen(fsname) + 1' chars.
788 * @param [out] endptr if endptr isn't NULL it is set to end of fsname
791 int server_name2fsname(const char *svname, char *fsname, const char **endptr)
795 dash = svname + strnlen(svname, LUSTRE_MAXFSNAME);
796 for (; dash > svname && *dash != '-' && *dash != ':'; dash--)
801 if (fsname != NULL) {
802 strncpy(fsname, svname, dash - svname);
803 fsname[dash - svname] = '\0';
811 EXPORT_SYMBOL(server_name2fsname);
814 * Get service name (svname) from string
816 * if endptr isn't NULL it is set to end of fsname *
818 int server_name2svname(const char *label, char *svname, const char **endptr,
824 /* We use server_name2fsname() just for parsing */
825 rc = server_name2fsname(label, NULL, &dash);
832 if (strlcpy(svname, dash + 1, svsize) >= svsize)
837 EXPORT_SYMBOL(server_name2svname);
840 * check server name is OST.
842 int server_name_is_ost(const char *svname)
847 /* We use server_name2fsname() just for parsing */
848 rc = server_name2fsname(svname, NULL, &dash);
854 if (strncmp(dash, "OST", 3) == 0)
858 EXPORT_SYMBOL(server_name_is_ost);
861 * Get the index from the target name MDTXXXX/OSTXXXX
862 * rc = server type, or rc < 0 on error
864 int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
866 const char *dash = tgtname;
870 if (strncmp(dash, "MDT", 3) == 0)
871 rc = LDD_F_SV_TYPE_MDT;
872 else if (strncmp(dash, "OST", 3) == 0)
873 rc = LDD_F_SV_TYPE_OST;
879 if (strncmp(dash, "all", 3) == 0) {
882 return rc | LDD_F_SV_ALL;
885 index = simple_strtoul(dash, (char **)endptr, 16);
894 EXPORT_SYMBOL(target_name2index);
897 * Get the index from the OBD name.
898 * rc = server type, or
900 * if endptr isn't NULL it is set to end of name
902 int server_name2index(const char *svname, __u32 *idx, const char **endptr)
907 /* We use server_name2fsname() just for parsing */
908 rc = server_name2fsname(svname, NULL, &dash);
913 rc = target_name2index(dash, idx, endptr);
917 /* Account for -mdc after index that is possible when specifying mdt */
918 if (endptr != NULL && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
919 sizeof(LUSTRE_MDC_NAME)-1) == 0)
920 *endptr += sizeof(LUSTRE_MDC_NAME);
924 EXPORT_SYMBOL(server_name2index);
926 /*************** mount common betweeen server and client ***************/
929 int lustre_common_put_super(struct super_block *sb)
935 CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
937 /* Drop a ref to the MGC */
938 rc = lustre_stop_mgc(sb);
939 if (rc && (rc != -ENOENT)) {
941 CERROR("Can't stop MGC: %d\n", rc);
945 * BUSY just means that there's some other OBD that
946 * needs the mgc. Let him clean it up.
948 CDEBUG(D_MOUNT, "MGC still in use\n");
950 /* Drop a ref to the mounted disk */
955 EXPORT_SYMBOL(lustre_common_put_super);
957 static void lmd_print(struct lustre_mount_data *lmd)
961 PRINT_CMD(D_MOUNT, " mount data:\n");
962 if (lmd_is_client(lmd))
963 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
964 PRINT_CMD(D_MOUNT, "device: %s\n", lmd->lmd_dev);
965 PRINT_CMD(D_MOUNT, "flags: %x\n", lmd->lmd_flags);
968 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
970 if (lmd->lmd_recovery_time_soft)
971 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
972 lmd->lmd_recovery_time_soft);
974 if (lmd->lmd_recovery_time_hard)
975 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
976 lmd->lmd_recovery_time_hard);
978 for (i = 0; i < lmd->lmd_exclude_count; i++) {
979 PRINT_CMD(D_MOUNT, "exclude %d: OST%04x\n", i,
980 lmd->lmd_exclude[i]);
984 /* Is this server on the exclusion list */
985 int lustre_check_exclusion(struct super_block *sb, char *svname)
987 struct lustre_sb_info *lsi = s2lsi(sb);
988 struct lustre_mount_data *lmd = lsi->lsi_lmd;
994 rc = server_name2index(svname, &index, NULL);
995 if (rc != LDD_F_SV_TYPE_OST)
996 /* Only exclude OSTs */
999 CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
1000 index, lmd->lmd_exclude_count, lmd->lmd_dev);
1002 for (i = 0; i < lmd->lmd_exclude_count; i++) {
1003 if (index == lmd->lmd_exclude[i]) {
1004 CWARN("Excluding %s (on exclusion list)\n", svname);
1011 /* mount -v -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
1012 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
1014 const char *s1 = ptr, *s2;
1015 __u32 *exclude_list;
1022 * The shortest an ost name can be is 8 chars: -OST0000.
1023 * We don't actually know the fsname at this time, so in fact
1024 * a user could specify any fsname.
1026 devmax = strlen(ptr) / 8 + 1;
1028 /* temp storage until we figure out how many we have */
1029 OBD_ALLOC_PTR_ARRAY(exclude_list, devmax);
1033 /* we enter this fn pointing at the '=' */
1034 while (*s1 && *s1 != ' ' && *s1 != ',') {
1036 rc = server_name2index(s1, &index, &s2);
1038 CERROR("Can't parse server name '%s': rc = %d\n",
1042 if (rc == LDD_F_SV_TYPE_OST)
1043 exclude_list[lmd->lmd_exclude_count++] = index;
1045 CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
1046 (uint)(s2-s1), s1, rc);
1049 * now we are pointing at ':' (next exclude)
1050 * or ',' (end of excludes)
1052 if (lmd->lmd_exclude_count >= devmax)
1055 if (rc >= 0) /* non-err */
1058 if (lmd->lmd_exclude_count) {
1059 /* permanent, freed in lustre_free_lsi */
1060 OBD_ALLOC_PTR_ARRAY(lmd->lmd_exclude,
1061 lmd->lmd_exclude_count);
1062 if (lmd->lmd_exclude) {
1063 memcpy(lmd->lmd_exclude, exclude_list,
1064 sizeof(index) * lmd->lmd_exclude_count);
1067 lmd->lmd_exclude_count = 0;
1070 OBD_FREE_PTR_ARRAY(exclude_list, devmax);
1074 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
1079 if (lmd->lmd_mgssec != NULL) {
1080 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
1081 lmd->lmd_mgssec = NULL;
1084 tail = strchr(ptr, ',');
1086 length = strlen(ptr);
1088 length = tail - ptr;
1090 OBD_ALLOC(lmd->lmd_mgssec, length + 1);
1091 if (lmd->lmd_mgssec == NULL)
1094 memcpy(lmd->lmd_mgssec, ptr, length);
1095 lmd->lmd_mgssec[length] = '\0';
1099 static int lmd_parse_network(struct lustre_mount_data *lmd, char *ptr)
1104 if (lmd->lmd_nidnet != NULL) {
1105 OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1106 lmd->lmd_nidnet = NULL;
1109 tail = strchr(ptr, ',');
1111 length = strlen(ptr);
1113 length = tail - ptr;
1115 OBD_ALLOC(lmd->lmd_nidnet, length + 1);
1116 if (lmd->lmd_nidnet == NULL)
1119 memcpy(lmd->lmd_nidnet, ptr, length);
1120 lmd->lmd_nidnet[length] = '\0';
1124 static int lmd_parse_string(char **handle, char *ptr)
1129 if ((handle == NULL) || (ptr == NULL))
1132 if (*handle != NULL) {
1133 OBD_FREE(*handle, strlen(*handle) + 1);
1137 tail = strchr(ptr, ',');
1139 length = strlen(ptr);
1141 length = tail - ptr;
1143 OBD_ALLOC(*handle, length + 1);
1144 if (*handle == NULL)
1147 memcpy(*handle, ptr, length);
1148 (*handle)[length] = '\0';
1153 /* Collect multiple values for mgsnid specifiers */
1154 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
1162 /* Find end of NID-list */
1163 while (class_parse_nid_quiet(tail, &nid, &tail) == 0)
1166 length = tail - *ptr;
1168 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
1172 if (lmd->lmd_mgs != NULL)
1173 oldlen = strlen(lmd->lmd_mgs) + 1;
1175 OBD_ALLOC(mgsnid, oldlen + length + 1);
1179 if (lmd->lmd_mgs != NULL) {
1180 /* Multiple mgsnid= are taken to mean failover locations */
1181 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
1182 mgsnid[oldlen - 1] = ':';
1183 OBD_FREE(lmd->lmd_mgs, oldlen);
1185 memcpy(mgsnid + oldlen, *ptr, length);
1186 mgsnid[oldlen + length] = '\0';
1187 lmd->lmd_mgs = mgsnid;
1194 * Find the first delimiter (comma or colon) from the specified \a buf and
1195 * make \a *endh point to the string starting with the delimiter. The commas
1196 * in expression list [...] will be skipped.
1198 * @buf a delimiter-separated string
1199 * @endh a pointer to a pointer that will point to the string
1200 * starting with the delimiter
1202 * RETURNS true if delimiter is found, false if delimiter is not found
1204 static bool lmd_find_delimiter(char *buf, char **endh)
1213 if (*c == ',' || *c == ':')
1216 pos = strcspn(c, "[:,]");
1220 /* Not a valid mount string */
1222 CWARN("invalid mount string format\n");
1230 /* invalid mount string */
1232 CWARN("invalid mount string format\n");
1247 * Find the first valid string delimited by comma or colon from the specified
1248 * \a buf and parse it to see whether it's a valid NID list. If yes, \a *endh
1249 * will point to the next string starting with the delimiter.
1251 * \param[in] buf a delimiter-separated string
1252 * \param[in] endh a pointer to a pointer that will point to the string
1253 * starting with the delimiter
1255 * \retval 0 if the string is a valid NID list
1256 * \retval 1 if the string is not a valid NID list
1258 static int lmd_parse_nidlist(char *buf, char **endh)
1267 while (*buf == ',' || *buf == ':')
1269 if (*buf == ' ' || *buf == '/' || *buf == '\0')
1272 if (!lmd_find_delimiter(buf, &endp))
1273 endp = buf + strlen(buf);
1278 if (cfs_parse_nidlist(buf, strlen(buf), &nidlist) <= 0)
1280 cfs_free_nidlist(&nidlist);
1291 * Parse mount line options
1292 * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
1293 * dev is passed as device=uml1:/lustre by mount.lustre_tgt
1295 int lmd_parse(char *options, struct lustre_mount_data *lmd)
1297 char *s1, *s2, *devname = NULL;
1298 struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1305 LCONSOLE_ERROR_MSG(0x162,
1306 "Missing mount data: check /sbin/mount.lustre_tgt is installed.\n");
1310 /* Options should be a string - try to detect old lmd data */
1311 if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1312 LCONSOLE_ERROR_MSG(0x163,
1313 "Using an old version of /sbin/mount.lustre. Please install version %s\n",
1314 LUSTRE_VERSION_STRING);
1317 lmd->lmd_magic = LMD_MAGIC;
1319 OBD_ALLOC(lmd->lmd_params, LMD_PARAMS_MAXLEN);
1320 if (lmd->lmd_params == NULL)
1322 lmd->lmd_params[0] = '\0';
1324 /* Set default flags here */
1329 int time_min = OBD_RECOVERY_TIME_MIN;
1332 /* Skip whitespace and extra commas */
1333 while (*s1 == ' ' || *s1 == ',')
1338 * Client options are parsed in ll_options: eg. flock,
1343 * Parse non-ldiskfs options here. Rather than modifying
1344 * ldiskfs, we just zero these out here
1346 if (strncmp(s1, "abort_recov", 11) == 0) {
1347 lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
1349 } else if (strncmp(s1, "abort_recov_mdt", 15) == 0) {
1350 lmd->lmd_flags |= LMD_FLG_ABORT_RECOV_MDT;
1352 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
1353 lmd->lmd_recovery_time_soft =
1354 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1357 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
1358 lmd->lmd_recovery_time_hard =
1359 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1362 } else if (strncmp(s1, "no_precreate", 12) == 0) {
1363 lmd->lmd_flags |= LMD_FLG_NO_PRECREATE;
1365 } else if (strncmp(s1, "noir", 4) == 0) {
1366 lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
1368 } else if (strncmp(s1, "nosvc", 5) == 0) {
1369 lmd->lmd_flags |= LMD_FLG_NOSVC;
1371 } else if (strncmp(s1, "nomgs", 5) == 0) {
1372 lmd->lmd_flags |= LMD_FLG_NOMGS;
1374 } else if (strncmp(s1, "noscrub", 7) == 0) {
1375 lmd->lmd_flags |= LMD_FLG_NOSCRUB;
1377 } else if (strncmp(s1, "skip_lfsck", 10) == 0) {
1378 lmd->lmd_flags |= LMD_FLG_SKIP_LFSCK;
1380 } else if (strncmp(s1, "rdonly_dev", 10) == 0) {
1381 lmd->lmd_flags |= LMD_FLG_DEV_RDONLY;
1383 } else if (strncmp(s1, PARAM_MGSNODE,
1384 sizeof(PARAM_MGSNODE) - 1) == 0) {
1385 s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
1387 * Assume the next mount opt is the first
1388 * invalid NID we get to.
1390 rc = lmd_parse_mgs(lmd, &s2);
1395 } else if (strncmp(s1, "writeconf", 9) == 0) {
1396 lmd->lmd_flags |= LMD_FLG_WRITECONF;
1398 } else if (strncmp(s1, "update", 6) == 0) {
1399 lmd->lmd_flags |= LMD_FLG_UPDATE;
1401 } else if (strncmp(s1, "virgin", 6) == 0) {
1402 lmd->lmd_flags |= LMD_FLG_VIRGIN;
1404 } else if (strncmp(s1, "noprimnode", 10) == 0) {
1405 lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
1407 } else if (strncmp(s1, "mgssec=", 7) == 0) {
1408 rc = lmd_parse_mgssec(lmd, s1 + 7);
1412 /* ost exclusion list */
1413 } else if (strncmp(s1, "exclude=", 8) == 0) {
1414 rc = lmd_make_exclusion(lmd, s1 + 7);
1418 } else if (strncmp(s1, "mgs", 3) == 0) {
1420 lmd->lmd_flags |= LMD_FLG_MGS;
1422 } else if (strncmp(s1, "svname=", 7) == 0) {
1423 rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
1427 } else if (strncmp(s1, "param=", 6) == 0) {
1428 size_t length, params_length;
1431 if (lmd_find_delimiter(s1 + 6, &tail)) {
1432 char *param_str = tail + 1;
1433 int supplementary = 1;
1435 while (lmd_parse_nidlist(param_str,
1439 length = param_str - s1 - supplementary;
1441 length = strlen(s1);
1444 params_length = strlen(lmd->lmd_params);
1445 if (params_length + length + 1 >= LMD_PARAMS_MAXLEN)
1447 strncat(lmd->lmd_params, s1 + 6, length);
1448 lmd->lmd_params[params_length + length] = '\0';
1449 strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
1450 s3 = s1 + 6 + length;
1452 } else if (strncmp(s1, "localrecov", 10) == 0) {
1453 lmd->lmd_flags |= LMD_FLG_LOCAL_RECOV;
1455 } else if (strncmp(s1, "osd=", 4) == 0) {
1456 rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
1462 * Linux 2.4 doesn't pass the device, so we stuck it at
1463 * the end of the options.
1465 else if (strncmp(s1, "device=", 7) == 0) {
1468 * terminate options right before device. device
1469 * must be the last one.
1473 } else if (strncmp(s1, "network=", 8) == 0) {
1474 rc = lmd_parse_network(lmd, s1 + 8);
1478 /* check if LNet dynamic peer discovery is activated */
1479 if (LNetGetPeerDiscoveryStatus()) {
1480 CERROR("LNet Dynamic Peer Discovery is enabled "
1481 "on this node. 'network' mount option "
1482 "cannot be taken into account.\n");
1490 s2 = strchr(s3, ',');
1498 memmove(s1, s2, strlen(s2) + 1);
1504 LCONSOLE_ERROR_MSG(0x164,
1505 "Can't find device name (need mount option 'device=...')\n");
1509 s1 = strstr(devname, ":/");
1512 lmd->lmd_flags |= LMD_FLG_CLIENT;
1513 /* Remove leading /s from fsname */
1514 while (*++s1 == '/')
1517 while (*s2 != '/' && *s2 != '\0')
1519 /* Freed in lustre_free_lsi */
1520 OBD_ALLOC(lmd->lmd_profile, s2 - s1 + 8);
1521 if (!lmd->lmd_profile)
1524 strncat(lmd->lmd_profile, s1, s2 - s1);
1525 strncat(lmd->lmd_profile, "-client", 7);
1528 s2 = s1 + strlen(s1) - 1;
1529 /* Remove padding /s from fileset */
1533 OBD_ALLOC(lmd->lmd_fileset, s2 - s1 + 2);
1534 if (lmd->lmd_fileset == NULL) {
1535 OBD_FREE(lmd->lmd_profile,
1536 strlen(lmd->lmd_profile) + 1);
1539 strncat(lmd->lmd_fileset, s1, s2 - s1 + 1);
1543 if (lmd->lmd_nidnet != NULL) {
1544 /* 'network=' mount option forbidden for server */
1545 OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1546 lmd->lmd_nidnet = NULL;
1549 "%s: option 'network=' not allowed for Lustre servers: rc = %d\n",
1555 /* Freed in lustre_free_lsi */
1556 OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
1559 strncpy(lmd->lmd_dev, devname, strlen(devname)+1);
1561 /* Save mount options */
1562 s1 = options + strlen(options) - 1;
1563 while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1565 while (*options && (*options == ',' || *options == ' '))
1567 if (*options != 0) {
1568 /* Freed in lustre_free_lsi */
1569 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1572 strncpy(lmd->lmd_opts, options, strlen(options)+1);
1576 lmd->lmd_magic = LMD_MAGIC;
1581 CERROR("Bad mount options %s\n", options);
1584 EXPORT_SYMBOL(lmd_parse);
1586 #ifdef HAVE_SERVER_SUPPORT
1588 * This is the entry point for the mount call into Lustre.
1589 * This is called when a server target is mounted,
1590 * and this is where we start setting things up.
1591 * @param data Mount options (e.g. -o flock,abort_recov)
1593 static int lustre_tgt_fill_super(struct super_block *sb, void *lmd2_data,
1596 struct lustre_mount_data *lmd;
1597 struct lustre_sb_info *lsi;
1602 CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
1604 lsi = lustre_init_lsi(sb);
1610 * Disable lockdep during mount, because mount locking patterns are
1616 * LU-639: the OBD cleanup of last mount may not finish yet, wait here.
1618 obd_zombie_barrier();
1620 /* Figure out the lmd from the mount options */
1621 if (lmd_parse(lmd2_data, lmd)) {
1623 GOTO(out, rc = -EINVAL);
1626 if (lmd_is_client(lmd)) {
1628 CERROR("%s: attempting to mount a client with -t lustre_tgt' which is only for server-side mounts: rc = %d\n",
1634 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
1635 rc = server_fill_super(sb);
1637 * server_fill_super calls lustre_start_mgc after the mount
1638 * because we need the MGS NIDs which are stored on disk.
1639 * Plus, we may need to start the MGS first.
1641 * server_fill_super will call server_put_super on failure
1643 * If error happens in fill_super() call, @lsi will be killed there.
1644 * This is why we do not put it here.
1648 CERROR("Unable to mount %s (%d)\n",
1649 s2lsi(sb) ? lmd->lmd_dev : "", rc);
1651 CDEBUG(D_SUPER, "Mount %s complete\n",
1658 /***************** FS registration ******************/
1659 static struct dentry *lustre_tgt_mount(struct file_system_type *fs_type,
1660 int flags, const char *devname,
1663 return mount_nodev(fs_type, flags, data, lustre_tgt_fill_super);
1666 /* Register the "lustre_tgt" fs type.
1668 * Right now this isn't any different than the normal "lustre" filesystem
1669 * type, but it is added so that there is some compatibility to allow
1670 * changing documentation and scripts to start using the "lustre_tgt" type
1671 * at mount time. That will simplify test interop, and in case of upgrades
1672 * that change to the new type and then need to roll back for some reason.
1674 * The long-term goal is to disentangle the client and server mount code.
1676 static struct file_system_type lustre_tgt_fstype = {
1677 .owner = THIS_MODULE,
1678 .name = "lustre_tgt",
1679 .mount = lustre_tgt_mount,
1680 .kill_sb = kill_anon_super,
1681 .fs_flags = FS_REQUIRES_DEV | FS_RENAME_DOES_D_MOVE,
1683 MODULE_ALIAS_FS("lustre_tgt");
1685 int lustre_tgt_register_fs(void)
1687 return register_filesystem(&lustre_tgt_fstype);
1690 void lustre_tgt_unregister_fs(void)
1692 unregister_filesystem(&lustre_tgt_fstype);
1695 #endif /* HAVE_SERVER_SUPPORT */