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/
31 * lustre/obdclass/obd_mount.c
33 * Client mount routines
35 * Author: Nathan Rutman <nathan@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_CLASS
40 #define D_MOUNT (D_SUPER|D_CONFIG/*|D_WARNING */)
41 #define PRINT_CMD CDEBUG
44 #include <obd_class.h>
45 #include <linux/random.h>
46 #include <libcfs/linux/linux-uuid.h>
47 #include <linux/version.h>
48 #include <lustre_log.h>
49 #include <lustre_disk.h>
50 #include <uapi/linux/lustre/lustre_param.h>
51 #include <libcfs/crypto/llcrypt.h>
53 /**************** config llog ********************/
56 * Get a config log from the MGS and process it.
57 * This func is called for both clients and servers.
58 * Continue to process new statements appended to the logs
59 * (whenever the config lock is revoked) until lustre_end_log
62 * @param sb The superblock is used by the MGC to write to the local copy of
64 * @param logname The name of the llog to replicate from the MGS
65 * @param cfg Since the same MGC may be used to follow multiple config logs
66 * (e.g. ost1, ost2, client), the config_llog_instance keeps the state for
67 * this log, and is added to the mgc's list of logs to follow.
69 int lustre_process_log(struct super_block *sb, char *logname,
70 struct config_llog_instance *cfg)
72 struct lustre_cfg *lcfg;
73 struct lustre_cfg_bufs *bufs;
74 struct lustre_sb_info *lsi = s2lsi(sb);
75 struct obd_device *mgc = lsi->lsi_mgc;
87 /* mgc_process_config */
88 lustre_cfg_bufs_reset(bufs, mgc->obd_name);
89 lustre_cfg_bufs_set_string(bufs, 1, logname);
90 lustre_cfg_bufs_set(bufs, 2, cfg, sizeof(*cfg));
91 lustre_cfg_bufs_set(bufs, 3, &sb, sizeof(sb));
92 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
94 GOTO(out, rc = -ENOMEM);
95 lustre_cfg_init(lcfg, LCFG_LOG_START, bufs);
97 rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
98 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
103 LCONSOLE_ERROR_MSG(0x15b,
104 "%s: Configuration from log %s failed from MGS %d. Check client and MGS are on compatible version.\n",
105 mgc->obd_name, logname, rc);
107 LCONSOLE_ERROR_MSG(0x15c,
108 "%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",
109 mgc->obd_name, logname, rc);
111 /* class_obd_list(); */
114 EXPORT_SYMBOL(lustre_process_log);
116 /* Stop watching this config log for updates */
117 int lustre_end_log(struct super_block *sb, char *logname,
118 struct config_llog_instance *cfg)
120 struct lustre_cfg *lcfg;
121 struct lustre_cfg_bufs bufs;
122 struct lustre_sb_info *lsi = s2lsi(sb);
123 struct obd_device *mgc = lsi->lsi_mgc;
131 /* mgc_process_config */
132 lustre_cfg_bufs_reset(&bufs, mgc->obd_name);
133 lustre_cfg_bufs_set_string(&bufs, 1, logname);
135 lustre_cfg_bufs_set(&bufs, 2, cfg, sizeof(*cfg));
136 OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
139 lustre_cfg_init(lcfg, LCFG_LOG_END, &bufs);
140 rc = obd_process_config(mgc, sizeof(*lcfg), lcfg);
141 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
144 EXPORT_SYMBOL(lustre_end_log);
146 /**************** OBD start *******************/
149 * lustre_cfg_bufs are a holdover from 1.4; we can still set these up from
150 * lctl (and do for echo cli/srv.
152 static int do_lcfg(char *cfgname, lnet_nid_t nid, int cmd,
153 char *s1, char *s2, char *s3, char *s4)
155 struct lustre_cfg_bufs bufs;
156 struct lustre_cfg *lcfg = NULL;
159 CDEBUG(D_TRACE, "lcfg %s %#x %s %s %s %s\n", cfgname,
160 cmd, s1, s2, s3, s4);
162 lustre_cfg_bufs_reset(&bufs, cfgname);
164 lustre_cfg_bufs_set_string(&bufs, 1, s1);
166 lustre_cfg_bufs_set_string(&bufs, 2, s2);
168 lustre_cfg_bufs_set_string(&bufs, 3, s3);
170 lustre_cfg_bufs_set_string(&bufs, 4, s4);
172 OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
175 lustre_cfg_init(lcfg, cmd, &bufs);
176 lcfg->lcfg_nid = nid;
177 rc = class_process_config(lcfg);
178 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
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)
191 CDEBUG(D_MOUNT, "Starting OBD %s (typ=%s)\n", obdname, type);
193 rc = do_lcfg(obdname, 0, LCFG_ATTACH, type, uuid, NULL, NULL);
195 CERROR("%s attach error %d\n", obdname, rc);
198 rc = do_lcfg(obdname, 0, LCFG_SETUP, s1, s2, s3, s4);
200 CERROR("%s setup error %d\n", obdname, rc);
201 do_lcfg(obdname, 0, LCFG_DETACH, NULL, NULL, NULL, NULL);
206 static DEFINE_MUTEX(mgc_start_lock);
209 * Set up a MGC OBD to process startup logs
211 * \param sb [in] super block of the MGC OBD
213 * \retval 0 success, otherwise error code
215 int lustre_start_mgc(struct super_block *sb)
217 struct obd_connect_data *data = NULL;
218 struct lustre_sb_info *lsi = s2lsi(sb);
219 struct obd_device *obd;
220 struct obd_export *exp;
221 struct obd_uuid *uuid = NULL;
224 char nidstr[LNET_NIDSTR_SIZE];
225 char *mgcname = NULL, *niduuid = NULL, *mgssec = NULL;
227 int rc = 0, i = 0, j;
232 LASSERT(lsi->lsi_lmd);
234 /* Find the first non-lo MGS NID for our MGC name */
235 if (IS_SERVER(lsi)) {
236 /* mount -o mgsnode=nid */
237 ptr = lsi->lsi_lmd->lmd_mgs;
238 if (lsi->lsi_lmd->lmd_mgs &&
239 (class_parse_nid(lsi->lsi_lmd->lmd_mgs, &nid, &ptr) == 0)) {
241 } else if (IS_MGS(lsi)) {
242 struct lnet_process_id id;
244 while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
245 if (id.nid == LNET_NID_LO_0)
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 "Mounting client with IR setting not compatible with current MGC. Using MGC setting that is IR %s",
307 has_ir ? "enabled" : "disabled");
309 *flags &= ~LMD_FLG_NOIR;
311 *flags |= LMD_FLG_NOIR;
317 * If we are restarting the MGS, don't try to keep the MGC's
318 * old connection, or registration will fail.
321 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.
332 CDEBUG(D_MOUNT, "%s:Set MGC reconnect %d\n", mgcname, recov_bk);
333 rc = obd_set_info_async(NULL, obd->obd_self_export,
334 sizeof(KEY_INIT_RECOV_BACKUP),
335 KEY_INIT_RECOV_BACKUP,
336 sizeof(recov_bk), &recov_bk, NULL);
340 CDEBUG(D_MOUNT, "Start MGC '%s'\n", mgcname);
342 /* Add the primary NIDs for the MGS */
344 snprintf(niduuid, len + 2, "%s_%x", mgcname, i);
345 if (IS_SERVER(lsi)) {
346 ptr = lsi->lsi_lmd->lmd_mgs;
347 CDEBUG(D_MOUNT, "mgs NIDs %s.\n", ptr);
349 /* Use local NIDs (including LO) */
350 struct lnet_process_id id;
352 while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
353 rc = do_lcfg(mgcname, id.nid, LCFG_ADD_UUID,
354 niduuid, NULL, NULL, NULL);
357 /* Use mgsnode= nids */
358 /* mount -o mgsnode=nid */
359 if (lsi->lsi_lmd->lmd_mgs) {
360 ptr = lsi->lsi_lmd->lmd_mgs;
361 } else if (class_find_param(ptr, PARAM_MGSNODE,
363 CERROR("No MGS NIDs given.\n");
364 GOTO(out_free, rc = -EINVAL);
367 * Add primary MGS NID(s).
368 * Multiple NIDs on one MGS node are separated
371 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
372 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
373 niduuid, NULL, NULL, NULL);
376 /* Stop at the first failover NID */
381 } else { /* client */
382 /* Use NIDs from mount line: uml1,1@elan:uml2,2@elan:/lustre */
383 ptr = lsi->lsi_lmd->lmd_dev;
384 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
385 rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
386 niduuid, NULL, NULL, NULL);
389 /* Stop at the first failover NID */
395 CERROR("No valid MGS NIDs found.\n");
396 GOTO(out_free, rc = -EINVAL);
398 lsi->lsi_lmd->lmd_mgs_failnodes = 1;
400 /* Random uuid for MGC allows easier reconnects */
403 GOTO(out_free, rc = -ENOMEM);
405 generate_random_uuid(uuidc.b);
406 snprintf(uuid->uuid, sizeof(*uuid), "%pU", uuidc.b);
409 rc = lustre_start_simple(mgcname, LUSTRE_MGC_NAME,
410 (char *)uuid->uuid, LUSTRE_MGS_OBDNAME,
411 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);
455 * Keep a refcount of servers/clients who started with "mount",
456 * so we know when we can get rid of the mgc.
458 atomic_set(&obd->u.cli.cl_mgc_refcount, 1);
460 /* We connect to the MGS at setup, and don't disconnect until cleanup */
461 data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
462 OBD_CONNECT_FULL20 | OBD_CONNECT_IMP_RECOV |
463 OBD_CONNECT_LVB_TYPE |
464 OBD_CONNECT_BULK_MBITS | OBD_CONNECT_BARRIER;
466 if (lmd_is_client(lsi->lsi_lmd) &&
467 lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)
468 data->ocd_connect_flags &= ~OBD_CONNECT_IMP_RECOV;
469 data->ocd_version = LUSTRE_VERSION_CODE;
470 rc = obd_connect(NULL, &exp, obd, uuid, data, NULL);
472 CERROR("connect failed %d\n", rc);
476 obd->u.cli.cl_mgc_mgsexp = exp;
480 * Keep the MGC info in the sb. Note that many lsi's can point
485 mutex_unlock(&mgc_start_lock);
492 OBD_FREE(mgcname, len);
494 OBD_FREE(niduuid, len + 2);
497 EXPORT_SYMBOL(lustre_start_mgc);
499 static int lustre_stop_mgc(struct super_block *sb)
501 struct lustre_sb_info *lsi = s2lsi(sb);
502 struct obd_device *obd;
503 char *niduuid = NULL, *ptr = NULL;
504 int i, rc = 0, len = 0;
515 mutex_lock(&mgc_start_lock);
516 LASSERT(atomic_read(&obd->u.cli.cl_mgc_refcount) > 0);
517 if (!atomic_dec_and_test(&obd->u.cli.cl_mgc_refcount)) {
519 * This is not fatal, every client that stops
522 CDEBUG(D_MOUNT, "MGC still has %d references.\n",
523 atomic_read(&obd->u.cli.cl_mgc_refcount));
524 GOTO(out, rc = -EBUSY);
528 * The MGC has no recoverable data in any case.
529 * force shotdown set in umount_begin
531 obd->obd_no_recov = 1;
533 if (obd->u.cli.cl_mgc_mgsexp) {
535 * An error is not fatal, if we are unable to send the
536 * disconnect mgs ping evictor cleans up the export
538 rc = obd_disconnect(obd->u.cli.cl_mgc_mgsexp);
540 CDEBUG(D_MOUNT, "disconnect failed %d\n", rc);
544 * Save the obdname for cleaning the nid uuids, which are
547 len = strlen(obd->obd_name) + 6;
548 OBD_ALLOC(niduuid, len);
550 strcpy(niduuid, obd->obd_name);
551 ptr = niduuid + strlen(niduuid);
554 rc = class_manual_cleanup(obd);
558 /* Clean the nid uuids */
560 GOTO(out, rc = -ENOMEM);
562 for (i = 0; i < lsi->lsi_lmd->lmd_mgs_failnodes; i++) {
563 sprintf(ptr, "_%x", i);
564 rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_DEL_UUID,
565 niduuid, NULL, NULL, NULL);
567 CERROR("del MDC UUID %s failed: rc = %d\n",
572 OBD_FREE(niduuid, len);
574 /* class_import_put will get rid of the additional connections */
575 mutex_unlock(&mgc_start_lock);
579 /***************** lustre superblock **************/
581 struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
583 struct lustre_sb_info *lsi;
590 OBD_ALLOC_PTR(lsi->lsi_lmd);
596 s2lsi_nocast(sb) = lsi;
597 /* we take 1 extra ref for our setup */
598 atomic_set(&lsi->lsi_mounts, 1);
600 /* Default umount style */
601 lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
602 INIT_LIST_HEAD(&lsi->lsi_lwp_list);
603 mutex_init(&lsi->lsi_lwp_mutex);
607 EXPORT_SYMBOL(lustre_init_lsi);
609 static int lustre_free_lsi(struct super_block *sb)
611 struct lustre_sb_info *lsi = s2lsi(sb);
615 LASSERT(lsi != NULL);
616 CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
618 /* someone didn't call server_put_mount. */
619 LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
622 if (lsi->lsi_lmd != NULL) {
623 if (lsi->lsi_lmd->lmd_dev != NULL)
624 OBD_FREE(lsi->lsi_lmd->lmd_dev,
625 strlen(lsi->lsi_lmd->lmd_dev) + 1);
626 if (lsi->lsi_lmd->lmd_profile != NULL)
627 OBD_FREE(lsi->lsi_lmd->lmd_profile,
628 strlen(lsi->lsi_lmd->lmd_profile) + 1);
629 if (lsi->lsi_lmd->lmd_fileset != NULL)
630 OBD_FREE(lsi->lsi_lmd->lmd_fileset,
631 strlen(lsi->lsi_lmd->lmd_fileset) + 1);
632 if (lsi->lsi_lmd->lmd_mgssec != NULL)
633 OBD_FREE(lsi->lsi_lmd->lmd_mgssec,
634 strlen(lsi->lsi_lmd->lmd_mgssec) + 1);
635 if (lsi->lsi_lmd->lmd_opts != NULL)
636 OBD_FREE(lsi->lsi_lmd->lmd_opts,
637 strlen(lsi->lsi_lmd->lmd_opts) + 1);
638 if (lsi->lsi_lmd->lmd_exclude_count)
639 OBD_FREE(lsi->lsi_lmd->lmd_exclude,
640 sizeof(lsi->lsi_lmd->lmd_exclude[0]) *
641 lsi->lsi_lmd->lmd_exclude_count);
642 if (lsi->lsi_lmd->lmd_mgs != NULL)
643 OBD_FREE(lsi->lsi_lmd->lmd_mgs,
644 strlen(lsi->lsi_lmd->lmd_mgs) + 1);
645 if (lsi->lsi_lmd->lmd_osd_type != NULL)
646 OBD_FREE(lsi->lsi_lmd->lmd_osd_type,
647 strlen(lsi->lsi_lmd->lmd_osd_type) + 1);
648 if (lsi->lsi_lmd->lmd_params != NULL)
649 OBD_FREE(lsi->lsi_lmd->lmd_params, 4096);
650 if (lsi->lsi_lmd->lmd_nidnet != NULL)
651 OBD_FREE(lsi->lsi_lmd->lmd_nidnet,
652 strlen(lsi->lsi_lmd->lmd_nidnet) + 1);
654 OBD_FREE_PTR(lsi->lsi_lmd);
657 LASSERT(lsi->lsi_llsbi == NULL);
659 s2lsi_nocast(sb) = NULL;
665 * The lsi has one reference for every server that is using the disk -
666 * e.g. MDT, MGS, and potentially MGC
668 int lustre_put_lsi(struct super_block *sb)
670 struct lustre_sb_info *lsi = s2lsi(sb);
674 LASSERT(lsi != NULL);
676 CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
677 if (atomic_dec_and_test(&lsi->lsi_mounts)) {
678 if (IS_SERVER(lsi) && lsi->lsi_osd_exp) {
679 lu_device_put(&lsi->lsi_dt_dev->dd_lu_dev);
680 lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt = NULL;
681 lsi->lsi_dt_dev = NULL;
682 obd_disconnect(lsi->lsi_osd_exp);
683 /* wait till OSD is gone */
684 obd_zombie_barrier();
691 EXPORT_SYMBOL(lustre_put_lsi);
694 * The goal of this function is to extract the file system name
695 * from the OBD name. This can come in two flavors. One is
696 * fsname-MDTXXXX or fsname-XXXXXXX were X is a hexadecimal
697 * number. In both cases we should return fsname. If it is
698 * not a valid OBD name it is assumed to be the file system
701 void obdname2fsname(const char *tgt, char *fsname, size_t buflen)
708 * First we have to see if the @tgt has '-' at all. It is
709 * valid for the user to request something like
710 * lctl set_param -P llite.lustre*.xattr_cache=0
712 ptr = strrchr(tgt, '-');
714 /* No '-' means it could end in '*' */
715 ptr = strchr(tgt, '*');
717 /* No '*' either. Assume tgt = fsname */
725 /* tgt format fsname-MDT0000-* */
726 if ((!strncmp(ptr, "-MDT", 4) ||
727 !strncmp(ptr, "-OST", 4)) &&
728 (isxdigit(ptr[4]) && isxdigit(ptr[5]) &&
729 isxdigit(ptr[6]) && isxdigit(ptr[7]))) {
735 * tgt_format fsname-cli'dev'-'uuid' except for the llite case
736 * which are named fsname-'uuid'. Examples:
738 * lustre-clilov-ffff88104db5b800
739 * lustre-ffff88104db5b800 (for llite device)
741 * The length of the OBD uuid can vary on different platforms.
742 * This test if any invalid characters are in string. Allow
743 * wildcards with '*' character.
746 if (!strspn(ptr, "0123456789abcdefABCDEF*")) {
752 * Now that we validated the device name lets extract the
753 * file system name. Most of the names in this class will
754 * have '-cli' in its name which needs to be dropped. If
755 * it doesn't have '-cli' then its a llite device which
756 * ptr already points to the start of the uuid string.
758 tmp = strstr(tgt, "-cli");
765 len = min_t(size_t, len, LUSTRE_MAXFSNAME);
766 snprintf(fsname, buflen, "%.*s", (int)len, tgt);
770 EXPORT_SYMBOL(obdname2fsname);
774 * <FSNAME><SEPARATOR><TYPE><INDEX>
775 * FSNAME is between 1 and 8 characters (inclusive).
776 * Excluded characters are '/' and ':'
777 * SEPARATOR is either ':' or '-'
778 * TYPE: "OST", "MDT", etc.
779 * INDEX: Hex representation of the index
783 * Get the fsname ("lustre") from the server name ("lustre-OST003F").
784 * @param [in] svname server name including type and index
785 * @param [out] fsname Buffer to copy filesystem name prefix into.
786 * Must have at least 'strlen(fsname) + 1' chars.
787 * @param [out] endptr if endptr isn't NULL it is set to end of fsname
790 int server_name2fsname(const char *svname, char *fsname, const char **endptr)
794 dash = svname + strnlen(svname, LUSTRE_MAXFSNAME);
795 for (; dash > svname && *dash != '-' && *dash != ':'; dash--)
800 if (fsname != NULL) {
801 strncpy(fsname, svname, dash - svname);
802 fsname[dash - svname] = '\0';
810 EXPORT_SYMBOL(server_name2fsname);
813 * Get service name (svname) from string
815 * if endptr isn't NULL it is set to end of fsname *
817 int server_name2svname(const char *label, char *svname, const char **endptr,
823 /* We use server_name2fsname() just for parsing */
824 rc = server_name2fsname(label, NULL, &dash);
831 if (strlcpy(svname, dash + 1, svsize) >= svsize)
836 EXPORT_SYMBOL(server_name2svname);
839 * check server name is OST.
841 int server_name_is_ost(const char *svname)
846 /* We use server_name2fsname() just for parsing */
847 rc = server_name2fsname(svname, NULL, &dash);
853 if (strncmp(dash, "OST", 3) == 0)
857 EXPORT_SYMBOL(server_name_is_ost);
860 * Get the index from the target name MDTXXXX/OSTXXXX
861 * rc = server type, or rc < 0 on error
863 int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
865 const char *dash = tgtname;
869 if (strncmp(dash, "MDT", 3) == 0)
870 rc = LDD_F_SV_TYPE_MDT;
871 else if (strncmp(dash, "OST", 3) == 0)
872 rc = LDD_F_SV_TYPE_OST;
878 if (strncmp(dash, "all", 3) == 0) {
881 return rc | LDD_F_SV_ALL;
884 index = simple_strtoul(dash, (char **)endptr, 16);
893 EXPORT_SYMBOL(target_name2index);
896 * Get the index from the OBD name.
897 * rc = server type, or
899 * if endptr isn't NULL it is set to end of name
901 int server_name2index(const char *svname, __u32 *idx, const char **endptr)
906 /* We use server_name2fsname() just for parsing */
907 rc = server_name2fsname(svname, NULL, &dash);
912 rc = target_name2index(dash, idx, endptr);
916 /* Account for -mdc after index that is possible when specifying mdt */
917 if (endptr != NULL && strncmp(LUSTRE_MDC_NAME, *endptr + 1,
918 sizeof(LUSTRE_MDC_NAME)-1) == 0)
919 *endptr += sizeof(LUSTRE_MDC_NAME);
923 EXPORT_SYMBOL(server_name2index);
925 /*************** mount common betweeen server and client ***************/
928 int lustre_common_put_super(struct super_block *sb)
934 CDEBUG(D_MOUNT, "dropping sb %p\n", sb);
936 /* Drop a ref to the MGC */
937 rc = lustre_stop_mgc(sb);
938 if (rc && (rc != -ENOENT)) {
940 CERROR("Can't stop MGC: %d\n", rc);
944 * BUSY just means that there's some other OBD that
945 * needs the mgc. Let him clean it up.
947 CDEBUG(D_MOUNT, "MGC still in use\n");
949 /* Drop a ref to the mounted disk */
954 EXPORT_SYMBOL(lustre_common_put_super);
956 static void lmd_print(struct lustre_mount_data *lmd)
960 PRINT_CMD(D_MOUNT, " mount data:\n");
961 if (lmd_is_client(lmd))
962 PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
963 PRINT_CMD(D_MOUNT, "device: %s\n", lmd->lmd_dev);
964 PRINT_CMD(D_MOUNT, "flags: %x\n", lmd->lmd_flags);
967 PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
969 if (lmd->lmd_recovery_time_soft)
970 PRINT_CMD(D_MOUNT, "recovery time soft: %d\n",
971 lmd->lmd_recovery_time_soft);
973 if (lmd->lmd_recovery_time_hard)
974 PRINT_CMD(D_MOUNT, "recovery time hard: %d\n",
975 lmd->lmd_recovery_time_hard);
977 for (i = 0; i < lmd->lmd_exclude_count; i++) {
978 PRINT_CMD(D_MOUNT, "exclude %d: OST%04x\n", i,
979 lmd->lmd_exclude[i]);
983 /* Is this server on the exclusion list */
984 int lustre_check_exclusion(struct super_block *sb, char *svname)
986 struct lustre_sb_info *lsi = s2lsi(sb);
987 struct lustre_mount_data *lmd = lsi->lsi_lmd;
993 rc = server_name2index(svname, &index, NULL);
994 if (rc != LDD_F_SV_TYPE_OST)
995 /* Only exclude OSTs */
998 CDEBUG(D_MOUNT, "Check exclusion %s (%d) in %d of %s\n", svname,
999 index, lmd->lmd_exclude_count, lmd->lmd_dev);
1001 for (i = 0; i < lmd->lmd_exclude_count; i++) {
1002 if (index == lmd->lmd_exclude[i]) {
1003 CWARN("Excluding %s (on exclusion list)\n", svname);
1010 /* mount -v -o exclude=lustre-OST0001:lustre-OST0002 -t lustre ... */
1011 static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
1013 const char *s1 = ptr, *s2;
1014 __u32 *exclude_list;
1021 * The shortest an ost name can be is 8 chars: -OST0000.
1022 * We don't actually know the fsname at this time, so in fact
1023 * a user could specify any fsname.
1025 devmax = strlen(ptr) / 8 + 1;
1027 /* temp storage until we figure out how many we have */
1028 OBD_ALLOC_PTR_ARRAY(exclude_list, devmax);
1032 /* we enter this fn pointing at the '=' */
1033 while (*s1 && *s1 != ' ' && *s1 != ',') {
1035 rc = server_name2index(s1, &index, &s2);
1037 CERROR("Can't parse server name '%s': rc = %d\n",
1041 if (rc == LDD_F_SV_TYPE_OST)
1042 exclude_list[lmd->lmd_exclude_count++] = index;
1044 CDEBUG(D_MOUNT, "ignoring exclude %.*s: type = %#x\n",
1045 (uint)(s2-s1), s1, rc);
1048 * now we are pointing at ':' (next exclude)
1049 * or ',' (end of excludes)
1051 if (lmd->lmd_exclude_count >= devmax)
1054 if (rc >= 0) /* non-err */
1057 if (lmd->lmd_exclude_count) {
1058 /* permanent, freed in lustre_free_lsi */
1059 OBD_ALLOC_PTR_ARRAY(lmd->lmd_exclude,
1060 lmd->lmd_exclude_count);
1061 if (lmd->lmd_exclude) {
1062 memcpy(lmd->lmd_exclude, exclude_list,
1063 sizeof(index) * lmd->lmd_exclude_count);
1066 lmd->lmd_exclude_count = 0;
1069 OBD_FREE_PTR_ARRAY(exclude_list, devmax);
1073 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
1078 if (lmd->lmd_mgssec != NULL) {
1079 OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
1080 lmd->lmd_mgssec = NULL;
1083 tail = strchr(ptr, ',');
1085 length = strlen(ptr);
1087 length = tail - ptr;
1089 OBD_ALLOC(lmd->lmd_mgssec, length + 1);
1090 if (lmd->lmd_mgssec == NULL)
1093 memcpy(lmd->lmd_mgssec, ptr, length);
1094 lmd->lmd_mgssec[length] = '\0';
1098 static int lmd_parse_network(struct lustre_mount_data *lmd, char *ptr)
1103 if (lmd->lmd_nidnet != NULL) {
1104 OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1105 lmd->lmd_nidnet = NULL;
1108 tail = strchr(ptr, ',');
1110 length = strlen(ptr);
1112 length = tail - ptr;
1114 OBD_ALLOC(lmd->lmd_nidnet, length + 1);
1115 if (lmd->lmd_nidnet == NULL)
1118 memcpy(lmd->lmd_nidnet, ptr, length);
1119 lmd->lmd_nidnet[length] = '\0';
1123 static int lmd_parse_string(char **handle, char *ptr)
1128 if ((handle == NULL) || (ptr == NULL))
1131 if (*handle != NULL) {
1132 OBD_FREE(*handle, strlen(*handle) + 1);
1136 tail = strchr(ptr, ',');
1138 length = strlen(ptr);
1140 length = tail - ptr;
1142 OBD_ALLOC(*handle, length + 1);
1143 if (*handle == NULL)
1146 memcpy(*handle, ptr, length);
1147 (*handle)[length] = '\0';
1152 /* Collect multiple values for mgsnid specifiers */
1153 static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
1161 /* Find end of NID-list */
1162 while (class_parse_nid_quiet(tail, &nid, &tail) == 0)
1165 length = tail - *ptr;
1167 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
1171 if (lmd->lmd_mgs != NULL)
1172 oldlen = strlen(lmd->lmd_mgs) + 1;
1174 OBD_ALLOC(mgsnid, oldlen + length + 1);
1178 if (lmd->lmd_mgs != NULL) {
1179 /* Multiple mgsnid= are taken to mean failover locations */
1180 memcpy(mgsnid, lmd->lmd_mgs, oldlen);
1181 mgsnid[oldlen - 1] = ':';
1182 OBD_FREE(lmd->lmd_mgs, oldlen);
1184 memcpy(mgsnid + oldlen, *ptr, length);
1185 mgsnid[oldlen + length] = '\0';
1186 lmd->lmd_mgs = mgsnid;
1193 * Find the first delimiter (comma or colon) from the specified \a buf and
1194 * make \a *endh point to the string starting with the delimiter. The commas
1195 * in expression list [...] will be skipped.
1197 * @buf a delimiter-separated string
1198 * @endh a pointer to a pointer that will point to the string
1199 * starting with the delimiter
1201 * RETURNS true if delimiter is found, false if delimiter is not found
1203 static bool lmd_find_delimiter(char *buf, char **endh)
1212 if (*c == ',' || *c == ':')
1215 pos = strcspn(c, "[:,]");
1219 /* Not a valid mount string */
1221 CWARN("invalid mount string format\n");
1229 /* invalid mount string */
1231 CWARN("invalid mount string format\n");
1246 * Find the first valid string delimited by comma or colon from the specified
1247 * \a buf and parse it to see whether it's a valid NID list. If yes, \a *endh
1248 * will point to the next string starting with the delimiter.
1250 * \param[in] buf a delimiter-separated string
1251 * \param[in] endh a pointer to a pointer that will point to the string
1252 * starting with the delimiter
1254 * \retval 0 if the string is a valid NID list
1255 * \retval 1 if the string is not a valid NID list
1257 static int lmd_parse_nidlist(char *buf, char **endh)
1266 while (*buf == ',' || *buf == ':')
1268 if (*buf == ' ' || *buf == '/' || *buf == '\0')
1271 if (!lmd_find_delimiter(buf, &endp))
1272 endp = buf + strlen(buf);
1277 if (cfs_parse_nidlist(buf, strlen(buf), &nidlist) <= 0)
1279 cfs_free_nidlist(&nidlist);
1290 * Parse mount line options
1291 * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
1292 * dev is passed as device=uml1:/lustre by mount.lustre_tgt
1294 int lmd_parse(char *options, struct lustre_mount_data *lmd)
1296 char *s1, *s2, *devname = NULL;
1297 struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
1304 LCONSOLE_ERROR_MSG(0x162,
1305 "Missing mount data: check /sbin/mount.lustre_tgt is installed.\n");
1309 /* Options should be a string - try to detect old lmd data */
1310 if ((raw->lmd_magic & 0xffffff00) == (LMD_MAGIC & 0xffffff00)) {
1311 LCONSOLE_ERROR_MSG(0x163,
1312 "Using an old version of /sbin/mount.lustre. Please install version %s\n",
1313 LUSTRE_VERSION_STRING);
1316 lmd->lmd_magic = LMD_MAGIC;
1318 OBD_ALLOC(lmd->lmd_params, LMD_PARAMS_MAXLEN);
1319 if (lmd->lmd_params == NULL)
1321 lmd->lmd_params[0] = '\0';
1323 /* Set default flags here */
1328 int time_min = OBD_RECOVERY_TIME_MIN;
1331 /* Skip whitespace and extra commas */
1332 while (*s1 == ' ' || *s1 == ',')
1337 * Client options are parsed in ll_options: eg. flock,
1342 * Parse non-ldiskfs options here. Rather than modifying
1343 * ldiskfs, we just zero these out here
1345 if (strncmp(s1, "abort_recov", 11) == 0) {
1346 lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
1348 } else if (strncmp(s1, "abort_recov_mdt", 15) == 0) {
1349 lmd->lmd_flags |= LMD_FLG_ABORT_RECOV_MDT;
1351 } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
1352 lmd->lmd_recovery_time_soft =
1353 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1356 } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
1357 lmd->lmd_recovery_time_hard =
1358 max_t(int, simple_strtoul(s1 + 19, NULL, 10),
1361 } else if (strncmp(s1, "no_precreate", 12) == 0) {
1362 lmd->lmd_flags |= LMD_FLG_NO_PRECREATE;
1364 } else if (strncmp(s1, "noir", 4) == 0) {
1365 lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
1367 } else if (strncmp(s1, "nosvc", 5) == 0) {
1368 lmd->lmd_flags |= LMD_FLG_NOSVC;
1370 } else if (strncmp(s1, "nomgs", 5) == 0) {
1371 lmd->lmd_flags |= LMD_FLG_NOMGS;
1373 } else if (strncmp(s1, "noscrub", 7) == 0) {
1374 lmd->lmd_flags |= LMD_FLG_NOSCRUB;
1376 } else if (strncmp(s1, "skip_lfsck", 10) == 0) {
1377 lmd->lmd_flags |= LMD_FLG_SKIP_LFSCK;
1379 } else if (strncmp(s1, "rdonly_dev", 10) == 0) {
1380 lmd->lmd_flags |= LMD_FLG_DEV_RDONLY;
1382 } else if (strncmp(s1, PARAM_MGSNODE,
1383 sizeof(PARAM_MGSNODE) - 1) == 0) {
1384 s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
1386 * Assume the next mount opt is the first
1387 * invalid NID we get to.
1389 rc = lmd_parse_mgs(lmd, &s2);
1394 } else if (strncmp(s1, "writeconf", 9) == 0) {
1395 lmd->lmd_flags |= LMD_FLG_WRITECONF;
1397 } else if (strncmp(s1, "nolocallogs", 11) == 0) {
1398 lmd->lmd_flags |= LMD_FLG_NO_LOCAL_LOGS;
1400 } else if (strncmp(s1, "update", 6) == 0) {
1401 lmd->lmd_flags |= LMD_FLG_UPDATE;
1403 } else if (strncmp(s1, "virgin", 6) == 0) {
1404 lmd->lmd_flags |= LMD_FLG_VIRGIN;
1406 } else if (strncmp(s1, "noprimnode", 10) == 0) {
1407 lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
1409 } else if (strncmp(s1, "mgssec=", 7) == 0) {
1410 rc = lmd_parse_mgssec(lmd, s1 + 7);
1414 /* ost exclusion list */
1415 } else if (strncmp(s1, "exclude=", 8) == 0) {
1416 rc = lmd_make_exclusion(lmd, s1 + 7);
1420 } else if (strncmp(s1, "mgs", 3) == 0) {
1422 lmd->lmd_flags |= LMD_FLG_MGS;
1424 } else if (strncmp(s1, "svname=", 7) == 0) {
1425 rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
1429 } else if (strncmp(s1, "param=", 6) == 0) {
1430 size_t length, params_length;
1433 if (lmd_find_delimiter(s1 + 6, &tail)) {
1434 char *param_str = tail + 1;
1435 int supplementary = 1;
1437 while (lmd_parse_nidlist(param_str,
1441 length = param_str - s1 - supplementary;
1443 length = strlen(s1);
1446 params_length = strlen(lmd->lmd_params);
1447 if (params_length + length + 1 >= LMD_PARAMS_MAXLEN)
1449 strncat(lmd->lmd_params, s1 + 6, length);
1450 lmd->lmd_params[params_length + length] = '\0';
1451 strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
1452 s3 = s1 + 6 + length;
1454 } else if (strncmp(s1, "localrecov", 10) == 0) {
1455 lmd->lmd_flags |= LMD_FLG_LOCAL_RECOV;
1457 } else if (strncmp(s1, "osd=", 4) == 0) {
1458 rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
1464 * Linux 2.4 doesn't pass the device, so we stuck it at
1465 * the end of the options.
1467 else if (strncmp(s1, "device=", 7) == 0) {
1470 * terminate options right before device. device
1471 * must be the last one.
1475 } else if (strncmp(s1, "network=", 8) == 0) {
1476 rc = lmd_parse_network(lmd, s1 + 8);
1480 /* check if LNet dynamic peer discovery is activated */
1481 if (LNetGetPeerDiscoveryStatus()) {
1482 CERROR("LNet Dynamic Peer Discovery is enabled "
1483 "on this node. 'network' mount option "
1484 "cannot be taken into account.\n");
1492 s2 = strchr(s3, ',');
1500 memmove(s1, s2, strlen(s2) + 1);
1506 LCONSOLE_ERROR_MSG(0x164,
1507 "Can't find device name (need mount option 'device=...')\n");
1511 s1 = strstr(devname, ":/");
1514 lmd->lmd_flags |= LMD_FLG_CLIENT;
1515 /* Remove leading /s from fsname */
1516 while (*++s1 == '/')
1519 while (*s2 != '/' && *s2 != '\0')
1521 /* Freed in lustre_free_lsi */
1522 OBD_ALLOC(lmd->lmd_profile, s2 - s1 + 8);
1523 if (!lmd->lmd_profile)
1526 strncat(lmd->lmd_profile, s1, s2 - s1);
1527 strncat(lmd->lmd_profile, "-client", 7);
1530 s2 = s1 + strlen(s1) - 1;
1531 /* Remove padding /s from fileset */
1535 OBD_ALLOC(lmd->lmd_fileset, s2 - s1 + 2);
1536 if (lmd->lmd_fileset == NULL) {
1537 OBD_FREE(lmd->lmd_profile,
1538 strlen(lmd->lmd_profile) + 1);
1541 strncat(lmd->lmd_fileset, s1, s2 - s1 + 1);
1545 if (lmd->lmd_nidnet != NULL) {
1546 /* 'network=' mount option forbidden for server */
1547 OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
1548 lmd->lmd_nidnet = NULL;
1551 "%s: option 'network=' not allowed for Lustre servers: rc = %d\n",
1557 /* Freed in lustre_free_lsi */
1558 OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
1561 strncpy(lmd->lmd_dev, devname, strlen(devname)+1);
1563 /* Save mount options */
1564 s1 = options + strlen(options) - 1;
1565 while (s1 >= options && (*s1 == ',' || *s1 == ' '))
1567 while (*options && (*options == ',' || *options == ' '))
1569 if (*options != 0) {
1570 /* Freed in lustre_free_lsi */
1571 OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
1574 strncpy(lmd->lmd_opts, options, strlen(options)+1);
1578 lmd->lmd_magic = LMD_MAGIC;
1583 CERROR("Bad mount options %s\n", options);
1586 EXPORT_SYMBOL(lmd_parse);
1588 #ifdef HAVE_SERVER_SUPPORT
1590 * This is the entry point for the mount call into Lustre.
1591 * This is called when a server target is mounted,
1592 * and this is where we start setting things up.
1593 * @param data Mount options (e.g. -o flock,abort_recov)
1595 static int lustre_tgt_fill_super(struct super_block *sb, void *lmd2_data,
1598 struct lustre_mount_data *lmd;
1599 struct lustre_sb_info *lsi;
1604 CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
1606 lsi = lustre_init_lsi(sb);
1612 * Disable lockdep during mount, because mount locking patterns are
1618 * LU-639: the OBD cleanup of last mount may not finish yet, wait here.
1620 obd_zombie_barrier();
1622 /* Figure out the lmd from the mount options */
1623 if (lmd_parse(lmd2_data, lmd)) {
1625 GOTO(out, rc = -EINVAL);
1628 if (lmd_is_client(lmd)) {
1630 CERROR("%s: attempting to mount a client with -t lustre_tgt' which is only for server-side mounts: rc = %d\n",
1636 CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
1637 rc = server_fill_super(sb);
1639 * server_fill_super calls lustre_start_mgc after the mount
1640 * because we need the MGS NIDs which are stored on disk.
1641 * Plus, we may need to start the MGS first.
1643 * server_fill_super will call server_put_super on failure
1645 * If error happens in fill_super() call, @lsi will be killed there.
1646 * This is why we do not put it here.
1650 CERROR("Unable to mount %s (%d)\n",
1651 s2lsi(sb) ? lmd->lmd_dev : "", rc);
1653 CDEBUG(D_SUPER, "Mount %s complete\n",
1660 /***************** FS registration ******************/
1661 static struct dentry *lustre_tgt_mount(struct file_system_type *fs_type,
1662 int flags, const char *devname,
1665 return mount_nodev(fs_type, flags, data, lustre_tgt_fill_super);
1668 /* Register the "lustre_tgt" fs type.
1670 * Right now this isn't any different than the normal "lustre" filesystem
1671 * type, but it is added so that there is some compatibility to allow
1672 * changing documentation and scripts to start using the "lustre_tgt" type
1673 * at mount time. That will simplify test interop, and in case of upgrades
1674 * that change to the new type and then need to roll back for some reason.
1676 * The long-term goal is to disentangle the client and server mount code.
1678 static struct file_system_type lustre_tgt_fstype = {
1679 .owner = THIS_MODULE,
1680 .name = "lustre_tgt",
1681 .mount = lustre_tgt_mount,
1682 .kill_sb = kill_anon_super,
1683 .fs_flags = FS_REQUIRES_DEV | FS_RENAME_DOES_D_MOVE,
1685 MODULE_ALIAS_FS("lustre_tgt");
1687 int lustre_tgt_register_fs(void)
1689 return register_filesystem(&lustre_tgt_fstype);
1692 void lustre_tgt_unregister_fs(void)
1694 unregister_filesystem(&lustre_tgt_fstype);
1697 #endif /* HAVE_SERVER_SUPPORT */