Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[fs/lustre-release.git] / lustre / obdclass / obd_mount.c
index 0c68d2f..c921a43 100644 (file)
@@ -27,7 +27,6 @@
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
  *
  * lustre/obdclass/obd_mount.c
  *
 #define D_MOUNT (D_SUPER|D_CONFIG/*|D_WARNING */)
 #define PRINT_CMD CDEBUG
 
-#include <obd.h>
-#include <obd_class.h>
+#include <linux/types.h>
+#include <linux/parser.h>
 #include <linux/random.h>
-#include <libcfs/linux/linux-uuid.h>
+#include <linux/uuid.h>
 #include <linux/version.h>
+
+#include <obd.h>
+#include <obd_class.h>
+#include <lustre_crypto.h>
 #include <lustre_log.h>
 #include <lustre_disk.h>
 #include <uapi/linux/lustre/lustre_param.h>
-#include <libcfs/crypto/llcrypt.h>
-
-static DEFINE_SPINLOCK(client_lock);
-static struct module *client_mod;
-static int (*client_fill_super)(struct super_block *sb);
-
-static void (*kill_super_cb)(struct super_block *sb);
 
 /**************** config llog ********************/
 
@@ -115,7 +111,6 @@ out:
                                   "%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",
                                   mgc->obd_name, logname, rc);
 
-       /* class_obd_list(); */
        RETURN(rc);
 }
 EXPORT_SYMBOL(lustre_process_log);
@@ -186,6 +181,19 @@ static int do_lcfg(char *cfgname, lnet_nid_t nid, int cmd,
        return rc;
 }
 
+static int do_lcfg_nid(char *cfgname, struct lnet_nid *nid, int cmd,
+                      char *s1)
+{
+       lnet_nid_t nid4 = 0;
+       char *nidstr = NULL;
+
+       if (nid_is_nid4(nid))
+               nid4 = lnet_nid_to_nid4(nid);
+       else
+               nidstr = libcfs_nidstr(nid);
+       return do_lcfg(cfgname, nid4, cmd, s1, nidstr, NULL, NULL);
+}
+
 /**
  * Call class_attach and class_setup.  These methods in turn call
  * OBD type-specific methods.
@@ -209,6 +217,7 @@ int lustre_start_simple(char *obdname, char *type, char *uuid,
        }
        return rc;
 }
+EXPORT_SYMBOL(lustre_start_simple);
 
 static DEFINE_MUTEX(mgc_start_lock);
 
@@ -227,7 +236,7 @@ int lustre_start_mgc(struct super_block *sb)
        struct obd_export *exp;
        struct obd_uuid *uuid = NULL;
        uuid_t uuidc;
-       lnet_nid_t nid;
+       struct lnet_nid nid;
        char nidstr[LNET_NIDSTR_SIZE];
        char *mgcname = NULL, *niduuid = NULL, *mgssec = NULL;
        char *ptr;
@@ -246,10 +255,10 @@ int lustre_start_mgc(struct super_block *sb)
                    (class_parse_nid(lsi->lsi_lmd->lmd_mgs, &nid, &ptr) == 0)) {
                        i++;
                } else if (IS_MGS(lsi)) {
-                       struct lnet_process_id id;
+                       struct lnet_processid id;
 
-                       while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
-                               if (id.nid == LNET_NID_LO_0)
+                       while ((rc = LNetGetId(i++, &id, true)) != -ENOENT) {
+                               if (nid_is_lo0(&id.nid))
                                        continue;
                                nid = id.nid;
                                i++;
@@ -269,7 +278,7 @@ int lustre_start_mgc(struct super_block *sb)
 
        mutex_lock(&mgc_start_lock);
 
-       libcfs_nid2str_r(nid, nidstr, sizeof(nidstr));
+       libcfs_nidstr_r(&nid, nidstr, sizeof(nidstr));
        len = strlen(LUSTRE_MGC_OBDNAME) + strlen(nidstr) + 1;
        OBD_ALLOC(mgcname, len);
        OBD_ALLOC(niduuid, len + 2);
@@ -300,22 +309,25 @@ int lustre_start_mgc(struct super_block *sb)
                if (lmd_is_client(lsi->lsi_lmd)) {
                        int has_ir;
                        int vallen = sizeof(*data);
-                       __u32 *flags = &lsi->lsi_lmd->lmd_flags;
 
                        rc = obd_get_info(NULL, obd->obd_self_export,
                                          strlen(KEY_CONN_DATA), KEY_CONN_DATA,
                                          &vallen, data);
                        LASSERT(rc == 0);
                        has_ir = OCD_HAS_FLAG(data, IMP_RECOV);
-                       if (has_ir ^ !(*flags & LMD_FLG_NOIR)) {
+                       if (has_ir ^ !test_bit(LMD_FLG_NOIR,
+                                              lsi->lsi_lmd->lmd_flags)) {
                                /* LMD_FLG_NOIR is for test purpose only */
                                LCONSOLE_WARN(
                                              "Mounting client with IR setting not compatible with current MGC. Using MGC setting that is IR %s",
                                              has_ir ? "enabled" : "disabled");
-                               if (has_ir)
-                                       *flags &= ~LMD_FLG_NOIR;
-                               else
-                                       *flags |= LMD_FLG_NOIR;
+                               if (has_ir) {
+                                       clear_bit(LMD_FLG_NOIR,
+                                                 lsi->lsi_lmd->lmd_flags);
+                               } else {
+                                       set_bit(LMD_FLG_NOIR,
+                                               lsi->lsi_lmd->lmd_flags);
+                               }
                        }
                }
 
@@ -354,11 +366,12 @@ int lustre_start_mgc(struct super_block *sb)
                CDEBUG(D_MOUNT, "mgs NIDs %s.\n", ptr);
                if (IS_MGS(lsi)) {
                        /* Use local NIDs (including LO) */
-                       struct lnet_process_id id;
+                       struct lnet_processid id;
 
-                       while ((rc = LNetGetId(i++, &id)) != -ENOENT) {
-                               rc = do_lcfg(mgcname, id.nid, LCFG_ADD_UUID,
-                                            niduuid, NULL, NULL, NULL);
+                       while ((rc = LNetGetId(i++, &id, true)) != -ENOENT) {
+                               rc = do_lcfg_nid(mgcname, &id.nid,
+                                                LCFG_ADD_UUID,
+                                                niduuid);
                        }
                } else {
                        /* Use mgsnode= nids */
@@ -376,8 +389,9 @@ int lustre_start_mgc(struct super_block *sb)
                         * by commas.
                         */
                        while (class_parse_nid(ptr, &nid, &ptr) == 0) {
-                               rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
-                                            niduuid, NULL, NULL, NULL);
+                               rc = do_lcfg_nid(mgcname, &nid,
+                                                LCFG_ADD_UUID,
+                                                niduuid);
                                if (rc == 0)
                                        ++i;
                                /* Stop at the first failover NID */
@@ -389,8 +403,8 @@ int lustre_start_mgc(struct super_block *sb)
                /* Use NIDs from mount line: uml1,1@elan:uml2,2@elan:/lustre */
                ptr = lsi->lsi_lmd->lmd_dev;
                while (class_parse_nid(ptr, &nid, &ptr) == 0) {
-                       rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
-                                    niduuid, NULL, NULL, NULL);
+                       rc = do_lcfg_nid(mgcname, &nid, LCFG_ADD_UUID,
+                                        niduuid);
                        if (rc == 0)
                                ++i;
                        /* Stop at the first failover NID */
@@ -410,7 +424,7 @@ int lustre_start_mgc(struct super_block *sb)
                GOTO(out_free, rc = -ENOMEM);
 
        generate_random_uuid(uuidc.b);
-       snprintf(uuid->uuid, UUID_SIZE, "%pU", uuidc.b);
+       snprintf(uuid->uuid, sizeof(*uuid), "%pU", uuidc.b);
 
        /* Start the MGC */
        rc = lustre_start_simple(mgcname, LUSTRE_MGC_NAME,
@@ -427,8 +441,8 @@ int lustre_start_mgc(struct super_block *sb)
                sprintf(niduuid, "%s_%x", mgcname, i);
                j = 0;
                while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
-                       rc = do_lcfg(mgcname, nid, LCFG_ADD_UUID,
-                                    niduuid, NULL, NULL, NULL);
+                       rc = do_lcfg_nid(mgcname, &nid, LCFG_ADD_UUID,
+                                        niduuid);
                        if (rc == 0)
                                ++j;
                        if (*ptr == ':')
@@ -468,10 +482,13 @@ int lustre_start_mgc(struct super_block *sb)
        data->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
                                  OBD_CONNECT_FULL20 | OBD_CONNECT_IMP_RECOV |
                                  OBD_CONNECT_LVB_TYPE |
-                                 OBD_CONNECT_BULK_MBITS | OBD_CONNECT_BARRIER;
+                                 OBD_CONNECT_BULK_MBITS | OBD_CONNECT_BARRIER |
+                                 OBD_CONNECT_FLAGS2;
+       data->ocd_connect_flags2 = OBD_CONNECT2_REP_MBITS |
+                                  OBD_CONNECT2_LARGE_NID;
 
        if (lmd_is_client(lsi->lsi_lmd) &&
-           lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)
+           test_bit(LMD_FLG_NOIR, lsi->lsi_lmd->lmd_flags))
                data->ocd_connect_flags &= ~OBD_CONNECT_IMP_RECOV;
        data->ocd_version = LUSTRE_VERSION_CODE;
        rc = obd_connect(NULL, &exp, obd, uuid, data, NULL);
@@ -501,13 +518,14 @@ out_free:
                OBD_FREE(niduuid, len + 2);
        RETURN(rc);
 }
+EXPORT_SYMBOL(lustre_start_mgc);
 
-static int lustre_stop_mgc(struct super_block *sb)
+int lustre_stop_mgc(struct super_block *sb)
 {
        struct lustre_sb_info *lsi = s2lsi(sb);
        struct obd_device *obd;
-       char *niduuid = NULL, *ptr = NULL;
-       int i, rc = 0, len = 0;
+       char niduuid[MAX_OBD_NAME + 6], *ptr = NULL;
+       int i, rc = 0;
 
        ENTRY;
 
@@ -547,24 +565,16 @@ static int lustre_stop_mgc(struct super_block *sb)
        }
 
        /*
-        * Save the obdname for cleaning the nid uuids, which are
-        * obdname_XX
+        * Cache the obdname for cleaning the nid uuids, which are
+        * obdname_XX before calling class_manual_cleanup
         */
-       len = strlen(obd->obd_name) + 6;
-       OBD_ALLOC(niduuid, len);
-       if (niduuid) {
-               strcpy(niduuid, obd->obd_name);
-               ptr = niduuid + strlen(niduuid);
-       }
+       strcpy(niduuid, obd->obd_name);
+       ptr = niduuid + strlen(niduuid);
 
        rc = class_manual_cleanup(obd);
        if (rc)
                GOTO(out, rc);
 
-       /* Clean the nid uuids */
-       if (!niduuid)
-               GOTO(out, rc = -ENOMEM);
-
        for (i = 0; i < lsi->lsi_lmd->lmd_mgs_failnodes; i++) {
                sprintf(ptr, "_%x", i);
                rc = do_lcfg(LUSTRE_MGC_OBDNAME, 0, LCFG_DEL_UUID,
@@ -574,17 +584,15 @@ static int lustre_stop_mgc(struct super_block *sb)
                               niduuid, rc);
        }
 out:
-       if (niduuid)
-               OBD_FREE(niduuid, len);
-
        /* class_import_put will get rid of the additional connections */
        mutex_unlock(&mgc_start_lock);
        RETURN(rc);
 }
+EXPORT_SYMBOL(lustre_stop_mgc);
 
 /***************** lustre superblock **************/
 
-static struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
+struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
 {
        struct lustre_sb_info *lsi;
 
@@ -599,12 +607,9 @@ static struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
                RETURN(NULL);
        }
 
-       lsi->lsi_lmd->lmd_exclude_count = 0;
-       lsi->lsi_lmd->lmd_recovery_time_soft = 0;
-       lsi->lsi_lmd->lmd_recovery_time_hard = 0;
        s2lsi_nocast(sb) = lsi;
        /* we take 1 extra ref for our setup */
-       atomic_set(&lsi->lsi_mounts, 1);
+       kref_init(&lsi->lsi_mounts);
 
        /* Default umount style */
        lsi->lsi_flags = LSI_UMOUNT_FAILOVER;
@@ -613,20 +618,19 @@ static struct lustre_sb_info *lustre_init_lsi(struct super_block *sb)
 
        RETURN(lsi);
 }
+EXPORT_SYMBOL(lustre_init_lsi);
 
-static int lustre_free_lsi(struct super_block *sb)
+static int lustre_free_lsi(struct lustre_sb_info *lsi)
 {
-       struct lustre_sb_info *lsi = s2lsi(sb);
-
        ENTRY;
 
        LASSERT(lsi != NULL);
        CDEBUG(D_MOUNT, "Freeing lsi %p\n", lsi);
 
        /* someone didn't call server_put_mount. */
-       LASSERT(atomic_read(&lsi->lsi_mounts) == 0);
+       LASSERT(kref_read(&lsi->lsi_mounts) == 0);
 
-       llcrypt_sb_free(sb);
+       llcrypt_sb_free(lsi);
        if (lsi->lsi_lmd != NULL) {
                if (lsi->lsi_lmd->lmd_dev != NULL)
                        OBD_FREE(lsi->lsi_lmd->lmd_dev,
@@ -664,11 +668,26 @@ static int lustre_free_lsi(struct super_block *sb)
 
        LASSERT(lsi->lsi_llsbi == NULL);
        OBD_FREE_PTR(lsi);
-       s2lsi_nocast(sb) = NULL;
 
        RETURN(0);
 }
 
+static void lustre_put_lsi_free(struct kref *kref)
+{
+       struct lustre_sb_info *lsi = container_of(kref, struct lustre_sb_info,
+                                                 lsi_mounts);
+
+       if (IS_SERVER(lsi) && lsi->lsi_osd_exp) {
+               lu_device_put(&lsi->lsi_dt_dev->dd_lu_dev);
+               lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt = NULL;
+               lsi->lsi_dt_dev = NULL;
+               obd_disconnect(lsi->lsi_osd_exp);
+               /* wait till OSD is gone */
+               obd_zombie_barrier();
+       }
+       lustre_free_lsi(lsi);
+}
+
 /*
  * The lsi has one reference for every server that is using the disk -
  * e.g. MDT, MGS, and potentially MGC
@@ -681,21 +700,14 @@ int lustre_put_lsi(struct super_block *sb)
 
        LASSERT(lsi != NULL);
 
-       CDEBUG(D_MOUNT, "put %p %d\n", sb, atomic_read(&lsi->lsi_mounts));
-       if (atomic_dec_and_test(&lsi->lsi_mounts)) {
-               if (IS_SERVER(lsi) && lsi->lsi_osd_exp) {
-                       lu_device_put(&lsi->lsi_dt_dev->dd_lu_dev);
-                       lsi->lsi_osd_exp->exp_obd->obd_lvfs_ctxt.dt = NULL;
-                       lsi->lsi_dt_dev = NULL;
-                       obd_disconnect(lsi->lsi_osd_exp);
-                       /* wait till OSD is gone */
-                       obd_zombie_barrier();
-               }
-               lustre_free_lsi(sb);
+       CDEBUG(D_MOUNT, "put %p %d\n", sb, kref_read(&lsi->lsi_mounts));
+       if (kref_put(&lsi->lsi_mounts, lustre_put_lsi_free)) {
+               s2lsi_nocast(sb) = NULL;
                RETURN(1);
        }
        RETURN(0);
 }
+EXPORT_SYMBOL(lustre_put_lsi);
 
 /*
  * The goal of this function is to extract the file system name
@@ -816,6 +828,7 @@ int server_name2fsname(const char *svname, char *fsname, const char **endptr)
 }
 EXPORT_SYMBOL(server_name2fsname);
 
+#ifdef HAVE_SERVER_SUPPORT
 /**
  * Get service name (svname) from string
  * rc < 0 on error
@@ -835,12 +848,14 @@ int server_name2svname(const char *label, char *svname, const char **endptr,
        if (endptr != NULL)
                *endptr = dash;
 
-       if (strlcpy(svname, dash + 1, svsize) >= svsize)
-               return -E2BIG;
+       rc = strscpy(svname, dash + 1, svsize);
+       if (rc < 0)
+               return rc;
 
        return 0;
 }
 EXPORT_SYMBOL(server_name2svname);
+#endif /* HAVE_SERVER_SUPPORT */
 
 /**
  * check server name is OST.
@@ -867,16 +882,16 @@ EXPORT_SYMBOL(server_name_is_ost);
  * Get the index from the target name MDTXXXX/OSTXXXX
  * rc = server type, or rc < 0  on error
  **/
-int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
+int target_name2index(const char *tgtname, u32 *idx, const char **endptr)
 {
        const char *dash = tgtname;
-       unsigned long index;
-       int rc;
+       int type, len, rc;
+       u16 index;
 
        if (strncmp(dash, "MDT", 3) == 0)
-               rc = LDD_F_SV_TYPE_MDT;
+               type = LDD_F_SV_TYPE_MDT;
        else if (strncmp(dash, "OST", 3) == 0)
-               rc = LDD_F_SV_TYPE_OST;
+               type = LDD_F_SV_TYPE_OST;
        else
                return -EINVAL;
 
@@ -885,17 +900,34 @@ int target_name2index(const char *tgtname, __u32 *idx, const char **endptr)
        if (strncmp(dash, "all", 3) == 0) {
                if (endptr != NULL)
                        *endptr = dash + 3;
-               return rc | LDD_F_SV_ALL;
+               return type | LDD_F_SV_ALL;
        }
 
-       index = simple_strtoul(dash, (char **)endptr, 16);
-       if (idx != NULL)
+       len = strspn(dash, "0123456789ABCDEFabcdef");
+       if (len > 4)
+               return -ERANGE;
+
+       if (strlen(dash) != len) {
+               char num[5];
+
+               num[4] = '\0';
+               memcpy(num, dash, sizeof(num) - 1);
+               rc = kstrtou16(num, 16, &index);
+               if (rc < 0)
+                       return rc;
+       } else {
+               rc = kstrtou16(dash, 16, &index);
+               if (rc < 0)
+                       return rc;
+       }
+
+       if (idx)
                *idx = index;
 
-       if (index > 0xffff)
-               return -ERANGE;
+       if (endptr)
+               *endptr = dash  + len;
 
-       return rc;
+       return type;
 }
 EXPORT_SYMBOL(target_name2index);
 
@@ -968,7 +1000,6 @@ static void lmd_print(struct lustre_mount_data *lmd)
        if (lmd_is_client(lmd))
                PRINT_CMD(D_MOUNT, "profile: %s\n", lmd->lmd_profile);
        PRINT_CMD(D_MOUNT, "device:  %s\n", lmd->lmd_dev);
-       PRINT_CMD(D_MOUNT, "flags:   %x\n", lmd->lmd_flags);
 
        if (lmd->lmd_opts)
                PRINT_CMD(D_MOUNT, "options: %s\n", lmd->lmd_opts);
@@ -1032,7 +1063,7 @@ static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
        devmax = strlen(ptr) / 8 + 1;
 
        /* temp storage until we figure out how many we have */
-       OBD_ALLOC(exclude_list, sizeof(index) * devmax);
+       OBD_ALLOC_PTR_ARRAY(exclude_list, devmax);
        if (!exclude_list)
                RETURN(-ENOMEM);
 
@@ -1063,8 +1094,8 @@ static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
 
        if (lmd->lmd_exclude_count) {
                /* permanent, freed in lustre_free_lsi */
-               OBD_ALLOC(lmd->lmd_exclude, sizeof(index) *
-                         lmd->lmd_exclude_count);
+               OBD_ALLOC_PTR_ARRAY(lmd->lmd_exclude,
+                                   lmd->lmd_exclude_count);
                if (lmd->lmd_exclude) {
                        memcpy(lmd->lmd_exclude, exclude_list,
                               sizeof(index) * lmd->lmd_exclude_count);
@@ -1073,26 +1104,19 @@ static int lmd_make_exclusion(struct lustre_mount_data *lmd, const char *ptr)
                        lmd->lmd_exclude_count = 0;
                }
        }
-       OBD_FREE(exclude_list, sizeof(index) * devmax);
+       OBD_FREE_PTR_ARRAY(exclude_list, devmax);
        RETURN(rc);
 }
 
 static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
 {
-       char *tail;
-       int length;
+       int length = strlen(ptr);
 
        if (lmd->lmd_mgssec != NULL) {
                OBD_FREE(lmd->lmd_mgssec, strlen(lmd->lmd_mgssec) + 1);
                lmd->lmd_mgssec = NULL;
        }
 
-       tail = strchr(ptr, ',');
-       if (tail == NULL)
-               length = strlen(ptr);
-       else
-               length = tail - ptr;
-
        OBD_ALLOC(lmd->lmd_mgssec, length + 1);
        if (lmd->lmd_mgssec == NULL)
                return -ENOMEM;
@@ -1104,20 +1128,13 @@ static int lmd_parse_mgssec(struct lustre_mount_data *lmd, char *ptr)
 
 static int lmd_parse_network(struct lustre_mount_data *lmd, char *ptr)
 {
-       char *tail;
-       int length;
+       int length = strlen(ptr);
 
        if (lmd->lmd_nidnet != NULL) {
                OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
                lmd->lmd_nidnet = NULL;
        }
 
-       tail = strchr(ptr, ',');
-       if (tail == NULL)
-               length = strlen(ptr);
-       else
-               length = tail - ptr;
-
        OBD_ALLOC(lmd->lmd_nidnet, length + 1);
        if (lmd->lmd_nidnet == NULL)
                return -ENOMEM;
@@ -1129,49 +1146,38 @@ static int lmd_parse_network(struct lustre_mount_data *lmd, char *ptr)
 
 static int lmd_parse_string(char **handle, char *ptr)
 {
-       char *tail;
-       int length;
-
-       if ((handle == NULL) || (ptr == NULL))
+       if (!handle || !ptr)
                return -EINVAL;
 
-       if (*handle != NULL) {
-               OBD_FREE(*handle, strlen(*handle) + 1);
-               *handle = NULL;
-       }
-
-       tail = strchr(ptr, ',');
-       if (tail == NULL)
-               length = strlen(ptr);
-       else
-               length = tail - ptr;
+       OBD_FREE(*handle, strlen(*handle) + 1);
+       *handle = NULL;
 
-       OBD_ALLOC(*handle, length + 1);
-       if (*handle == NULL)
+       *handle = kstrdup(ptr, GFP_NOFS);
+       if (!*handle)
                return -ENOMEM;
 
-       memcpy(*handle, ptr, length);
-       (*handle)[length] = '\0';
+       OBD_ALLOC_POST(*handle, strlen(ptr) + 1, "kmalloced");
 
        return 0;
 }
 
 /* Collect multiple values for mgsnid specifiers */
-static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
+static int lmd_parse_mgs(struct lustre_mount_data *lmd, char *ptr, char **tail)
 {
-       lnet_nid_t nid;
-       char *tail = *ptr;
+       int length = strlen(ptr);
+       struct lnet_nid nid;
+       char *next = *tail;
        char *mgsnid;
-       int length;
        int oldlen = 0;
 
        /* Find end of NID-list */
-       while (class_parse_nid_quiet(tail, &nid, &tail) == 0)
+       while (class_parse_nid_quiet(*tail, &nid, tail) == 0)
                ; /* do nothing */
 
-       length = tail - *ptr;
+       if (next && next != *tail)
+               length += *tail - next + 1;
        if (length == 0) {
-               LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", *ptr);
+               LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", ptr);
                return -EINVAL;
        }
 
@@ -1188,109 +1194,222 @@ static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
                mgsnid[oldlen - 1] = ':';
                OBD_FREE(lmd->lmd_mgs, oldlen);
        }
-       memcpy(mgsnid + oldlen, *ptr, length);
-       mgsnid[oldlen + length] = '\0';
+
+       if (next && next != *tail)
+               snprintf(mgsnid + oldlen, length + 1, "%s,%.*s", ptr,
+                        (int)(*tail - next + 1), next);
+       else
+               snprintf(mgsnid + oldlen, length + 1, "%s", ptr);
        lmd->lmd_mgs = mgsnid;
-       *ptr = tail;
 
        return 0;
 }
 
+enum lmd_mnt_flags {
+       LMD_OPT_RECOVERY_TIME_SOFT      = LMD_FLG_NUM_FLAGS + 1,
+       LMD_OPT_RECOVERY_TIME_HARD,
+       LMD_OPT_MGSNODE,
+       LMD_OPT_MGSSEC,
+       LMD_OPT_EXCLUDE,
+       LMD_OPT_SVNAME,
+       LMD_OPT_PARAM,
+       LMD_OPT_OSD,
+       LMD_OPT_NETWORK,
+       LMD_OPT_DEVICE,
+       LMD_NUM_MOUNT_OPT
+};
+
+static const match_table_t lmd_flags_table = {
+       {LMD_FLG_SKIP_LFSCK,            "skip_lfsck"},
+       {LMD_FLG_ABORT_RECOV,           "abort_recov"},
+       {LMD_FLG_ABORT_RECOV,           "abort_recovery"},
+       {LMD_FLG_NOSVC,                 "nosvc"},
+       {LMD_FLG_MGS,                   "mgs"},
+       {LMD_FLG_NOMGS,                 "nomgs"},
+       {LMD_FLG_WRITECONF,             "writeconf"},
+       {LMD_FLG_NOIR,                  "noir"},
+       {LMD_FLG_NOSCRUB,               "noscrub"},
+       {LMD_FLG_NO_PRIMNODE,           "noprimnode"},
+       {LMD_FLG_VIRGIN,                "virgin"},
+       {LMD_FLG_UPDATE,                "update"},
+       {LMD_FLG_DEV_RDONLY,            "rdonly_dev"},
+       {LMD_FLG_NO_CREATE,             "no_create"},
+       {LMD_FLG_NO_CREATE,             "no_precreate"},
+       {LMD_FLG_LOCAL_RECOV,           "localrecov"},
+       {LMD_FLG_ABORT_RECOV_MDT,       "abort_recov_mdt"},
+       {LMD_FLG_ABORT_RECOV_MDT,       "abort_recovery_mdt"},
+       {LMD_FLG_NO_LOCAL_LOGS,         "nolocallogs"},
+
+       {LMD_OPT_RECOVERY_TIME_SOFT,    "recovery_time_soft=%u"},
+       {LMD_OPT_RECOVERY_TIME_HARD,    "recovery_time_hard=%u"},
+       {LMD_OPT_MGSNODE,               "mgsnode=%s"},
+       {LMD_OPT_MGSSEC,                "mgssec=%s"},
+       {LMD_OPT_EXCLUDE,               "exclude=%s"},
+       {LMD_OPT_SVNAME,                "svname=%s"},
+       {LMD_OPT_PARAM,                 "param=%s"},
+       {LMD_OPT_OSD,                   "osd=%s"},
+       {LMD_OPT_NETWORK,               "network=%s"},
+       {LMD_OPT_DEVICE,                "device=%s"}, /* should be last */
+       {LMD_NUM_MOUNT_OPT,             NULL}
+};
+
 /**
- * Find the first delimiter (comma or colon) from the specified \a buf and
- * make \a *endh point to the string starting with the delimiter. The commas
- * in expression list [...] will be skipped.
+ * Find the first delimiter; comma; from the specified \a buf and
+ * make \a *endh point to the string starting with the delimiter.
+ * The character ':' is also a delimiter for Lustre but not match_table
+ * so the string is not split on it. Making it safe to ignore.
  *
  * @buf                a delimiter-separated string
  * @endh       a pointer to a pointer that will point to the string
  *             starting with the delimiter
  *
- * RETURNS     true if delimiter is found, false if delimiter is not found
+ * Returns:    true if delimiter is found, false if delimiter is not found
  */
 static bool lmd_find_delimiter(char *buf, char **endh)
 {
+       substring_t args[LMD_NUM_MOUNT_OPT];
+       char *end, *tmp;
+       size_t len;
+       int token;
+
+       if (!buf)
+               return false;
+
+       /* No more options so we are done */
+       end = strchr(buf, ',');
+       if (!end)
+               return false;
+
+       len = end - buf;
+       tmp = kstrndup(buf, len, GFP_KERNEL);
+       if (!tmp)
+               return false;
+
+       args[0].to = NULL;
+       args[0].from = NULL;
+       token = match_token(tmp, lmd_flags_table, args);
+       kfree(tmp);
+       if (token != LMD_NUM_MOUNT_OPT)
+               return false;
+
+       if (endh)
+               *endh = end;
+
+       return true;
+}
+
+/**
+ * Make sure the string in \a buf is of a valid formt.
+ *
+ * @buf                a delimiter-separated string
+ *
+ * Returns:    true if string valid, false if string contains errors
+ */
+static bool lmd_validate_param(char *buf)
+{
        char *c = buf;
        size_t pos;
-       bool found;
 
        if (!buf)
                return false;
 try_again:
-       if (*c == ',' || *c == ':')
-               return true;
-
-       pos = strcspn(c, "[:,]");
+       pos = strcspn(c, "[]");
        if (!pos)
-               return false;
+               return true;
 
+       c += pos;
        /* Not a valid mount string */
        if (*c == ']') {
                CWARN("invalid mount string format\n");
                return false;
        }
 
-       c += pos;
        if (*c == '[') {
-               c = strchr(c, ']');
+               char *right = strchr(c, ']'), *tmp;
 
                /* invalid mount string */
-               if (!c) {
+               if (!right) {
                        CWARN("invalid mount string format\n");
                        return false;
                }
                c++;
+
+               /* Test for [ .. [ .. ] */
+               tmp = strchr(c, '[');
+               if (tmp && tmp < right) {
+                       CWARN("invalid mount string format\n");
+                       return false;
+               }
+
+               /* Test for [ .. @ .. ] which means brackets
+                * span more than one NID string.
+                */
+               tmp = strchr(c, '@');
+               if (tmp && tmp < right) {
+                       CWARN("invalid mount string format\n");
+                       return false;
+               }
+
+               c = right++;
                goto try_again;
        }
 
-       found = *c != '\0';
-       if (found && endh)
-               *endh = c;
-
-       return found;
+       return true;
 }
 
 /**
  * Find the first valid string delimited by comma or colon from the specified
- * \a buf and parse it to see whether it's a valid NID list. If yes, \a *endh
+ * @buf and parse it to see whether it's a valid nid list. If yes, @*endh
  * will point to the next string starting with the delimiter.
  *
- * \param[in] buf      a delimiter-separated string
- * \param[in] endh     a pointer to a pointer that will point to the string
- *                     starting with the delimiter
+ * @buf:       a delimiter-separated string
  *
- * \retval 0           if the string is a valid NID list
- * \retval 1           if the string is not a valid NID list
+ * Returns:    false   if the string is a valid nid list
+ *              true   if the string is not a valid nid list
  */
-static int lmd_parse_nidlist(char *buf, char **endh)
+static bool lmd_parse_nidlist(char *buf)
 {
        LIST_HEAD(nidlist);
-       char *endp = buf;
-       char tmp;
-       int rc = 0;
+       bool invalid;
+       char *end;
 
-       if (buf == NULL)
-               return 1;
-       while (*buf == ',' || *buf == ':')
-               buf++;
-       if (*buf == ' ' || *buf == '/' || *buf == '\0')
-               return 1;
+       if (!buf)
+               return true;
 
-       if (!lmd_find_delimiter(buf, &endp))
-               endp = buf + strlen(buf);
+       end = strchr(buf, '=');
+       if (end)
+               buf = end + 1;
 
-       tmp = *endp;
-       *endp = '\0';
+       while ((end = strchr(buf, '@')) != NULL) {
+               size_t pos = strcspn(end, ":,");
+               char c;
 
-       if (cfs_parse_nidlist(buf, strlen(buf), &nidlist) <= 0)
-               rc = 1;
-       cfs_free_nidlist(&nidlist);
+               end += pos;
+               c = end[0];
+               end[0] = '\0';
+               /* FIXME !!! Add IPv6 support to cfs_parse_nidlist */
+               if (strchr(buf, ':')) {
+                       struct lnet_nid nid;
 
-       *endp = tmp;
-       if (rc != 0)
-               return rc;
-       if (endh != NULL)
-               *endh = endp;
-       return 0;
+                       if (libcfs_strnid(&nid, buf) < 0) {
+                               invalid = true;
+                               goto failed;
+                       }
+               } else {
+                       if (cfs_parse_nidlist(buf, &nidlist) < 0) {
+                               invalid = true;
+                               goto failed;
+                       } else {
+                               cfs_free_nidlist(&nidlist);
+                       }
+               }
+               end[0] = c;
+               end++;
+               buf = end;
+       }
+       invalid = false;
+failed:
+       return invalid;
 }
 
 /**
@@ -1298,14 +1417,13 @@ static int lmd_parse_nidlist(char *buf, char **endh)
  * e.g. mount -v -t lustre -o abort_recov uml1:uml2:/lustre-client /mnt/lustre
  * dev is passed as device=uml1:/lustre by mount.lustre_tgt
  */
-static int lmd_parse(char *options, struct lustre_mount_data *lmd)
+int lmd_parse(char *options, struct lustre_mount_data *lmd)
 {
-       char *s1, *s2, *devname = NULL;
+       char *s1, *s2, *opts, *orig_opts, *devname = NULL;
        struct lustre_mount_data *raw = (struct lustre_mount_data *)options;
        int rc = 0;
 
        ENTRY;
-
        LASSERT(lmd);
        if (!options) {
                LCONSOLE_ERROR_MSG(0x162,
@@ -1322,23 +1440,57 @@ static int lmd_parse(char *options, struct lustre_mount_data *lmd)
        }
        lmd->lmd_magic = LMD_MAGIC;
 
-       OBD_ALLOC(lmd->lmd_params, LMD_PARAMS_MAXLEN);
-       if (lmd->lmd_params == NULL)
+       /* Don't stomp on lmd_opts */
+       opts = kstrdup(options, GFP_KERNEL);
+       if (!opts)
                RETURN(-ENOMEM);
+       orig_opts = opts;
+       s1 = opts;
+
+       OBD_ALLOC(lmd->lmd_params, LMD_PARAMS_MAXLEN);
+       if (!lmd->lmd_params)
+               GOTO(invalid, rc = -ENOMEM);
        lmd->lmd_params[0] = '\0';
 
        /* Set default flags here */
+       while ((s1 = strsep(&opts, ",")) != NULL) {
+               int time_min = OBD_RECOVERY_TIME_MIN, tmp;
+               substring_t args[LMD_NUM_MOUNT_OPT];
+               int token;
 
-       s1 = options;
-       while (*s1) {
-               int clear = 0;
-               int time_min = OBD_RECOVERY_TIME_MIN;
-               char *s3;
+               if (!*s1)
+                       continue;
+               /*
+                * Initialize args struct so we know whether arg was
+                * found; some options take optional arguments.
+                */
+               args[0].to = NULL;
+               args[0].from = NULL;
+               token = match_token(s1, lmd_flags_table, args);
+               if (token == LMD_NUM_MOUNT_OPT) {
+                       if (match_wildcard("iam", s1) ||
+                           match_wildcard("hsm", s1))
+                               continue;
+
+                       /* Normally we would error but client and
+                        * server mounting is intertwine. So pass
+                        * off unknown args to ll_options instead.
+                        */
+                       continue;
+               } else {
+                       /* We found a known server option. Filter out
+                        * the result out of the options string. The
+                        * reset will be stored in lmd_opts.
+                        */
+                       char *tmp = strstr(options, s1);
 
-               /* Skip whitespace and extra commas */
-               while (*s1 == ' ' || *s1 == ',')
-                       s1++;
-               s3 = s1;
+                       if (strcmp(tmp, s1) != 0) {
+                               s2 = tmp + strlen(s1) + 1;
+                               memmove(tmp, s2, strlen(s2) + 1);
+                       } else {
+                               *tmp = 0;
+                       }
+               }
 
                /*
                 * Client options are parsed in ll_options: eg. flock,
@@ -1349,170 +1501,199 @@ static int lmd_parse(char *options, struct lustre_mount_data *lmd)
                 * Parse non-ldiskfs options here. Rather than modifying
                 * ldiskfs, we just zero these out here
                 */
-               if (strncmp(s1, "abort_recov", 11) == 0) {
-                       lmd->lmd_flags |= LMD_FLG_ABORT_RECOV;
-                       clear++;
-               } else if (strncmp(s1, "recovery_time_soft=", 19) == 0) {
-                       lmd->lmd_recovery_time_soft =
-                               max_t(int, simple_strtoul(s1 + 19, NULL, 10),
-                                     time_min);
-                       clear++;
-               } else if (strncmp(s1, "recovery_time_hard=", 19) == 0) {
-                       lmd->lmd_recovery_time_hard =
-                               max_t(int, simple_strtoul(s1 + 19, NULL, 10),
-                                     time_min);
-                       clear++;
-               } else if (strncmp(s1, "no_precreate", 12) == 0) {
-                       lmd->lmd_flags |= LMD_FLG_NO_PRECREATE;
-                       clear++;
-               } else if (strncmp(s1, "noir", 4) == 0) {
-                       lmd->lmd_flags |= LMD_FLG_NOIR; /* test purpose only. */
-                       clear++;
-               } else if (strncmp(s1, "nosvc", 5) == 0) {
-                       lmd->lmd_flags |= LMD_FLG_NOSVC;
-                       clear++;
-               } else if (strncmp(s1, "nomgs", 5) == 0) {
-                       lmd->lmd_flags |= LMD_FLG_NOMGS;
-                       clear++;
-               } else if (strncmp(s1, "noscrub", 7) == 0) {
-                       lmd->lmd_flags |= LMD_FLG_NOSCRUB;
-                       clear++;
-               } else if (strncmp(s1, "skip_lfsck", 10) == 0) {
-                       lmd->lmd_flags |= LMD_FLG_SKIP_LFSCK;
-                       clear++;
-               } else if (strncmp(s1, "rdonly_dev", 10) == 0) {
-                       lmd->lmd_flags |= LMD_FLG_DEV_RDONLY;
-                       clear++;
-               } else if (strncmp(s1, PARAM_MGSNODE,
-                                  sizeof(PARAM_MGSNODE) - 1) == 0) {
-                       s2 = s1 + sizeof(PARAM_MGSNODE) - 1;
-                       /*
-                        * Assume the next mount opt is the first
+               switch (token) {
+               case LMD_FLG_ABORT_RECOV_MDT:
+               case LMD_FLG_ABORT_RECOV:
+               case LMD_FLG_NO_CREATE:
+               case LMD_FLG_NOIR: /* test purpose only. */
+               case LMD_FLG_NOSVC:
+               case LMD_FLG_NOMGS:
+               case LMD_FLG_NOSCRUB:
+               case LMD_FLG_SKIP_LFSCK:
+               case LMD_FLG_DEV_RDONLY:
+               case LMD_FLG_WRITECONF:
+               case LMD_FLG_NO_LOCAL_LOGS:
+               case LMD_FLG_UPDATE:
+               case LMD_FLG_VIRGIN:
+               case LMD_FLG_NO_PRIMNODE:
+               case LMD_FLG_MGS: /* We are an MGS */
+               case LMD_FLG_LOCAL_RECOV:
+                       set_bit(token, lmd->lmd_flags);
+                       break;
+               case LMD_OPT_RECOVERY_TIME_SOFT:
+                       rc = match_int(args, &tmp);
+                       if (rc == 0)
+                               lmd->lmd_recovery_time_soft = max_t(int, tmp,
+                                                                   time_min);
+                       break;
+               case LMD_OPT_RECOVERY_TIME_HARD:
+                       rc = match_int(args, &tmp);
+                       if (rc == 0)
+                               lmd->lmd_recovery_time_hard = max_t(int, tmp,
+                                                                   time_min);
+                       break;
+               case LMD_OPT_MGSNODE:
+                       /* Assume the next mount opt is the first
                         * invalid NID we get to.
                         */
-                       rc = lmd_parse_mgs(lmd, &s2);
-                       if (rc)
-                               goto invalid;
-                       s3 = s2;
-                       clear++;
-               } else if (strncmp(s1, "writeconf", 9) == 0) {
-                       lmd->lmd_flags |= LMD_FLG_WRITECONF;
-                       clear++;
-               } else if (strncmp(s1, "update", 6) == 0) {
-                       lmd->lmd_flags |= LMD_FLG_UPDATE;
-                       clear++;
-               } else if (strncmp(s1, "virgin", 6) == 0) {
-                       lmd->lmd_flags |= LMD_FLG_VIRGIN;
-                       clear++;
-               } else if (strncmp(s1, "noprimnode", 10) == 0) {
-                       lmd->lmd_flags |= LMD_FLG_NO_PRIMNODE;
-                       clear++;
-               } else if (strncmp(s1, "mgssec=", 7) == 0) {
-                       rc = lmd_parse_mgssec(lmd, s1 + 7);
-                       if (rc)
-                               goto invalid;
-                       clear++;
+                       rc = lmd_parse_mgs(lmd, args->from, &opts);
+                       if (rc < 0)
+                               GOTO(invalid, rc);
+
+                       if (strcmp(options, opts) != 0) {
+                               s2 = strstr(options, opts);
+                               if (s2)
+                                       options = s2;
+                       }
+                       break;
+               case LMD_OPT_MGSSEC:
+                       rc = lmd_parse_mgssec(lmd, args->from);
+                       break;
+               case LMD_OPT_EXCLUDE:
                        /* ost exclusion list */
-               } else if (strncmp(s1, "exclude=", 8) == 0) {
-                       rc = lmd_make_exclusion(lmd, s1 + 7);
-                       if (rc)
-                               goto invalid;
-                       clear++;
-               } else if (strncmp(s1, "mgs", 3) == 0) {
-                       /* We are an MGS */
-                       lmd->lmd_flags |= LMD_FLG_MGS;
-                       clear++;
-               } else if (strncmp(s1, "svname=", 7) == 0) {
-                       rc = lmd_parse_string(&lmd->lmd_profile, s1 + 7);
-                       if (rc)
-                               goto invalid;
-                       clear++;
-               } else if (strncmp(s1, "param=", 6) == 0) {
-                       size_t length, params_length;
-                       char  *tail = s1;
-
-                       if (lmd_find_delimiter(s1 + 6, &tail)) {
-                               char *param_str = tail + 1;
-                               int   supplementary = 1;
-
-                               while (lmd_parse_nidlist(param_str,
-                                                        &param_str) == 0) {
-                                       supplementary = 0;
+                       rc = lmd_make_exclusion(lmd, args->from);
+                       break;
+               case LMD_OPT_SVNAME:
+                       rc = lmd_parse_string(&lmd->lmd_profile, args->from);
+                       break;
+               case LMD_OPT_PARAM: {
+                       size_t length = strlen(args->from), params_length;
+                       char *tail = NULL, *entry;
+
+                       params_length = strlen(lmd->lmd_params);
+                       if (params_length + length + 1 >= LMD_PARAMS_MAXLEN) {
+                               rc = -E2BIG;
+                               goto bad_string;
+                       }
+                       entry = lmd->lmd_params + params_length;
+                       strncat(lmd->lmd_params, args->from, length);
+
+                       /* Find end of param string */
+                       while (lmd_find_delimiter(opts, &tail)) {
+                               params_length = strlen(lmd->lmd_params);
+                               /* match_table splits by ',' so fill it in */
+                               lmd->lmd_params[params_length++] = ',';
+
+                               length = tail - opts + 1;
+                               if (!length)
+                                       break;
+                               if (params_length + length + 1 >=
+                                   LMD_PARAMS_MAXLEN) {
+                                       rc = -E2BIG;
+                                       goto bad_string;
                                }
-                               length = param_str - s1 - supplementary;
-                       } else {
-                               length = strlen(s1);
+
+                               strscpy(lmd->lmd_params + params_length,
+                                       opts, length);
+                               opts = tail + 1;
                        }
-                       length -= 6;
-                       params_length = strlen(lmd->lmd_params);
-                       if (params_length + length + 1 >= LMD_PARAMS_MAXLEN)
-                               RETURN(-E2BIG);
-                       strncat(lmd->lmd_params, s1 + 6, length);
+
                        lmd->lmd_params[params_length + length] = '\0';
+
+                       if (!lmd_validate_param(entry)) {
+                               rc = -EINVAL;
+                               goto bad_string;
+                       }
+
+                       /* param contains NIDs */
+                       if (strchr(entry, '@') && lmd_parse_nidlist(entry)) {
+                               rc = -EINVAL;
+                               goto bad_string;
+                       }
+
+                       /* remove params from opts string from options string */
+                       if (strlen(args->from) != strlen(entry)) {
+                               char *tmp = entry + strlen(args->from) + 1;
+
+                               s2 = strstr(options, tmp);
+                               if (s2) {
+                                       size_t len = strlen(s2) - strlen(tmp);
+
+                                       memmove(s2, s2 + strlen(tmp) + 1, len);
+                               }
+                       }
+
                        strlcat(lmd->lmd_params, " ", LMD_PARAMS_MAXLEN);
-                       s3 = s1 + 6 + length;
-                       clear++;
-               } else if (strncmp(s1, "localrecov", 10) == 0) {
-                       lmd->lmd_flags |= LMD_FLG_LOCAL_RECOV;
-                       clear++;
-               } else if (strncmp(s1, "osd=", 4) == 0) {
-                       rc = lmd_parse_string(&lmd->lmd_osd_type, s1 + 4);
-                       if (rc)
-                               goto invalid;
-                       clear++;
+                       if (tail)
+                               opts = tail + 1;
+bad_string:
+                       break;
                }
-               /*
-                * Linux 2.4 doesn't pass the device, so we stuck it at
-                * the end of the options.
-                */
-               else if (strncmp(s1, "device=", 7) == 0) {
-                       devname = s1 + 7;
-                       /*
-                        * terminate options right before device.  device
-                        * must be the last one.
-                        */
-                       *s1 = '\0';
+               case LMD_OPT_OSD:
+                       rc = lmd_parse_string(&lmd->lmd_osd_type, args->from);
                        break;
-               } else if (strncmp(s1, "network=", 8) == 0) {
-                       rc = lmd_parse_network(lmd, s1 + 8);
-                       if (rc)
-                               goto invalid;
+               case LMD_OPT_DEVICE: {
+                       size_t len = 0;
 
+                       /* match_table splits strings at ',' so we need to
+                        * piece things back together.
+                        */
+                       if (opts) {
+                               len = strlen(opts) + 1;
+
+                               /* Move to last part of device string */
+                               s2 = strchr(opts, '/');
+                               if (!s2)
+                                       GOTO(invalid, rc = -EINVAL);
+
+                               /* See if more options exist */
+                               s2 = strchr(s2, ',');
+                               if (s2)
+                                       len = s2 - opts;
+                       }
+                       len += strlen(args->from) + 1;
+
+                       /* Freed in lustre_free_lsi */
+                       OBD_ALLOC(lmd->lmd_dev, len);
+                       if (!lmd->lmd_dev)
+                               GOTO(invalid, rc = -ENOMEM);
+
+                       if (opts)
+                               snprintf(lmd->lmd_dev, len, "%s,%s",
+                                        args->from, opts);
+                       else
+                               strscpy(lmd->lmd_dev, args->from, len);
+
+                       devname = lmd->lmd_dev;
+
+                       /* remove the split string 'opts' from options */
+                       if (opts) {
+                               s1 = strstr(options, opts);
+                               if (s1) {
+                                       /* opts start after args->from so
+                                        * reduce len.
+                                        */
+                                       len -= strlen(args->from) + 2;
+                                       s2 = s1 + len;
+                                       memmove(options, s2, strlen(s2) + 1);
+                                       opts += len;
+                               }
+                       }
+                       break;
+               }
+               case LMD_OPT_NETWORK:
+                       rc = lmd_parse_network(lmd, args->from);
                        /* check if LNet dynamic peer discovery is activated */
                        if (LNetGetPeerDiscoveryStatus()) {
-                               CERROR("LNet Dynamic Peer Discovery is enabled "
-                                      "on this node. 'network' mount option "
-                                      "cannot be taken into account.\n");
-                               goto invalid;
+                               CERROR("LNet Dynamic Peer Discovery is enabled on this node. 'network' mount option cannot be taken into account.\n");
+                               rc = -EINVAL;
                        }
-
-                       clear++;
-               }
-
-               /* Find next opt */
-               s2 = strchr(s3, ',');
-               if (s2 == NULL) {
-                       if (clear)
-                               *s1 = '\0';
                        break;
                }
-               s2++;
-               if (clear)
-                       memmove(s1, s2, strlen(s2) + 1);
-               else
-                       s1 = s2;
        }
+       if (rc < 0)
+               GOTO(invalid, rc);
 
        if (!devname) {
                LCONSOLE_ERROR_MSG(0x164,
                                   "Can't find device name (need mount option 'device=...')\n");
-               goto invalid;
+               GOTO(invalid, rc = -ENODEV);
        }
 
        s1 = strstr(devname, ":/");
        if (s1) {
                ++s1;
-               lmd->lmd_flags |= LMD_FLG_CLIENT;
+               set_bit(LMD_FLG_CLIENT, lmd->lmd_flags);
                /* Remove leading /s from fsname */
                while (*++s1 == '/')
                        ;
@@ -1522,7 +1703,7 @@ static int lmd_parse(char *options, struct lustre_mount_data *lmd)
                /* Freed in lustre_free_lsi */
                OBD_ALLOC(lmd->lmd_profile, s2 - s1 + 8);
                if (!lmd->lmd_profile)
-                       RETURN(-ENOMEM);
+                       GOTO(invalid, rc = -ENOMEM);
 
                strncat(lmd->lmd_profile, s1, s2 - s1);
                strncat(lmd->lmd_profile, "-client", 7);
@@ -1534,11 +1715,8 @@ static int lmd_parse(char *options, struct lustre_mount_data *lmd)
                        s2--;
                if (s2 > s1) {
                        OBD_ALLOC(lmd->lmd_fileset, s2 - s1 + 2);
-                       if (lmd->lmd_fileset == NULL) {
-                               OBD_FREE(lmd->lmd_profile,
-                                        strlen(lmd->lmd_profile) + 1);
-                               RETURN(-ENOMEM);
-                       }
+                       if (!lmd->lmd_fileset)
+                               GOTO(invalid, rc = -ENOMEM);
                        strncat(lmd->lmd_fileset, s1, s2 - s1 + 1);
                }
        } else {
@@ -1548,19 +1726,12 @@ static int lmd_parse(char *options, struct lustre_mount_data *lmd)
                        OBD_FREE(lmd->lmd_nidnet, strlen(lmd->lmd_nidnet) + 1);
                        lmd->lmd_nidnet = NULL;
                        rc = -EINVAL;
-                       CERROR(
-                              "%s: option 'network=' not allowed for Lustre servers: rc = %d\n",
+                       CERROR("%s: option 'network=' not allowed for Lustre servers: rc = %d\n",
                               devname, rc);
-                       RETURN(rc);
+                       GOTO(invalid, rc);
                }
        }
 
-       /* Freed in lustre_free_lsi */
-       OBD_ALLOC(lmd->lmd_dev, strlen(devname) + 1);
-       if (!lmd->lmd_dev)
-               RETURN(-ENOMEM);
-       strncpy(lmd->lmd_dev, devname, strlen(devname)+1);
-
        /* Save mount options */
        s1 = options + strlen(options) - 1;
        while (s1 >= options && (*s1 == ',' || *s1 == ' '))
@@ -1571,212 +1742,16 @@ static int lmd_parse(char *options, struct lustre_mount_data *lmd)
                /* Freed in lustre_free_lsi */
                OBD_ALLOC(lmd->lmd_opts, strlen(options) + 1);
                if (!lmd->lmd_opts)
-                       RETURN(-ENOMEM);
-               strncpy(lmd->lmd_opts, options, strlen(options)+1);
+                       GOTO(invalid, rc = -ENOMEM);
+               strncpy(lmd->lmd_opts, options, strlen(options));
        }
 
        lmd_print(lmd);
-       lmd->lmd_magic = LMD_MAGIC;
-
-       RETURN(rc);
-
 invalid:
-       CERROR("Bad mount options %s\n", options);
-       RETURN(-EINVAL);
-}
-
-/**
- * This is the entry point for the mount call into Lustre.
- * This is called when a server or client is mounted,
- * and this is where we start setting things up.
- * @param data Mount options (e.g. -o flock,abort_recov)
- */
-static int lustre_fill_super(struct super_block *sb, void *lmd2_data,
-                            int silent)
-{
-       struct lustre_mount_data *lmd;
-       struct lustre_sb_info *lsi;
-       int rc;
-
-       ENTRY;
-
-       CDEBUG(D_MOUNT|D_VFSTRACE, "VFS Op: sb %p\n", sb);
-
-       lsi = lustre_init_lsi(sb);
-       if (!lsi)
-               RETURN(-ENOMEM);
-       lmd = lsi->lsi_lmd;
-
-       /*
-        * Disable lockdep during mount, because mount locking patterns are
-        * 'special'.
-        */
-       lockdep_off();
-
-       /*
-        * LU-639: the OBD cleanup of last mount may not finish yet, wait here.
-        */
-       obd_zombie_barrier();
-
-       /* Figure out the lmd from the mount options */
-       if (lmd_parse(lmd2_data, lmd)) {
-               lustre_put_lsi(sb);
-               GOTO(out, rc = -EINVAL);
-       }
-
-       if (lmd_is_client(lmd)) {
-               bool have_client = false;
-
-               CDEBUG(D_MOUNT, "Mounting client %s\n", lmd->lmd_profile);
-               if (!client_fill_super)
-                       request_module("lustre");
-               spin_lock(&client_lock);
-               if (client_fill_super && try_module_get(client_mod))
-                       have_client = true;
-               spin_unlock(&client_lock);
-               if (!have_client) {
-                       LCONSOLE_ERROR_MSG(0x165,
-                                          "Nothing registered for client mount! Is the 'lustre' module loaded?\n");
-                       lustre_put_lsi(sb);
-                       rc = -ENODEV;
-               } else {
-                       rc = lustre_start_mgc(sb);
-                       if (rc) {
-                               lustre_common_put_super(sb);
-                               GOTO(out, rc);
-                       }
-                       /* Connect and start */
-                       /* (should always be ll_fill_super) */
-                       rc = (*client_fill_super)(sb);
-                       /* c_f_s will call lustre_common_put_super on failure,
-                        * which takes care of the module reference.
-                        */
-               }
-       } else {
-#ifdef HAVE_SERVER_SUPPORT
-               CDEBUG(D_MOUNT, "Mounting server from %s\n", lmd->lmd_dev);
-               rc = server_fill_super(sb);
-               /*
-                * s_f_s calls lustre_start_mgc after the mount because we need
-                * the MGS NIDs which are stored on disk.  Plus, we may
-                * need to start the MGS first.
-                */
-               /* s_f_s will call server_put_super on failure */
-#else
-               CERROR("client-side-only module, cannot handle server mount\n");
-               rc = -EINVAL;
-#endif
-       }
-
-       /*
-        * If error happens in fill_super() call, @lsi will be killed there.
-        * This is why we do not put it here.
-        */
-       GOTO(out, rc);
-out:
-       if (rc) {
-               CERROR("Unable to mount %s (%d)\n",
-                      s2lsi(sb) ? lmd->lmd_dev : "", rc);
-       } else {
-               CDEBUG(D_SUPER, "Mount %s complete\n",
-                      lmd->lmd_dev);
-       }
-       lockdep_on();
-       return rc;
-}
-
-
-/*
- * We can't call ll_fill_super by name because it lives in a module that
- * must be loaded after this one.
- */
-void lustre_register_super_ops(struct module *mod,
-                              int (*cfs)(struct super_block *sb),
-                              void (*ksc)(struct super_block *sb))
-{
-       spin_lock(&client_lock);
-       client_mod = mod;
-       client_fill_super = cfs;
-       kill_super_cb = ksc;
-       spin_unlock(&client_lock);
-}
-EXPORT_SYMBOL(lustre_register_super_ops);
-
-/***************** FS registration ******************/
-static struct dentry *lustre_mount(struct file_system_type *fs_type, int flags,
-                                  const char *devname, void *data)
-{
-       return mount_nodev(fs_type, flags, data, lustre_fill_super);
-}
-
-static void lustre_kill_super(struct super_block *sb)
-{
-       struct lustre_sb_info *lsi = s2lsi(sb);
-
-       if (kill_super_cb && lsi && !IS_SERVER(lsi))
-               (*kill_super_cb)(sb);
-
-       kill_anon_super(sb);
-}
-
-#ifdef HAVE_SERVER_SUPPORT
-/* Register the "lustre_tgt" fs type.
- *
- * Right now this isn't any different than the normal "lustre" filesystem
- * type, but it is added so that there is some compatibility to allow
- * changing documentation and scripts to start using the "lustre_tgt" type
- * at mount time. That will simplify test interop, and in case of upgrades
- * that change to the new type and then need to roll back for some reason.
- *
- * The long-term goal is to disentangle the client and server mount code.
- */
-static struct file_system_type lustre_fs_type_tgt = {
-       .owner          = THIS_MODULE,
-       .name           = "lustre_tgt",
-       .mount          = lustre_mount,
-       .kill_sb        = lustre_kill_super,
-       .fs_flags       = FS_REQUIRES_DEV | FS_RENAME_DOES_D_MOVE,
-};
-MODULE_ALIAS_FS("lustre_tgt");
-
-#define register_filesystem_tgt(fstype)                                            \
-do {                                                                       \
-       int _rc;                                                            \
-                                                                           \
-       _rc = register_filesystem(fstype);                                  \
-       if (_rc && _rc != -EBUSY) {                                         \
-               /* Don't fail if server code also registers "lustre_tgt" */ \
-               CERROR("obdclass: register fstype '%s' failed: rc = %d\n",  \
-                      (fstype)->name, _rc);                                \
-               return _rc;                                                 \
-       }                                                                   \
-} while (0)
-#define unregister_filesystem_tgt(fstype) unregister_filesystem(fstype)
-#else
-#define register_filesystem_tgt(fstype)   do {} while (0)
-#define unregister_filesystem_tgt(fstype) do {} while (0)
-#endif
-
-/* Register the "lustre" fs type */
-static struct file_system_type lustre_fs_type = {
-       .owner          = THIS_MODULE,
-       .name           = "lustre",
-       .mount          = lustre_mount,
-       .kill_sb        = lustre_kill_super,
-       .fs_flags       = FS_RENAME_DOES_D_MOVE,
-};
-MODULE_ALIAS_FS("lustre");
-
-int lustre_register_fs(void)
-{
-       register_filesystem_tgt(&lustre_fs_type_tgt);
-
-       return register_filesystem(&lustre_fs_type);
-}
-
-int lustre_unregister_fs(void)
-{
-       unregister_filesystem_tgt(&lustre_fs_type_tgt);
+       if (rc < 0)
+               CERROR("Bad mount options %s\n", options);
+       kfree(orig_opts);
 
-       return unregister_filesystem(&lustre_fs_type);
+       RETURN(rc);
 }
+EXPORT_SYMBOL(lmd_parse);