+static int parse_ldd(char *source, struct mount_opts *mop,
+ char *options, size_t options_len)
+{
+ struct lustre_disk_data *ldd = &mop->mo_ldd;
+ char *cur, *start;
+ int rc = 0;
+
+ rc = osd_is_lustre(source, &ldd->ldd_mount_type);
+ if (rc == 0) {
+ fprintf(stderr, "%s: %s has not been formatted with mkfs.lustre"
+ " or the backend filesystem type is not supported by "
+ "this tool\n", progname, source);
+ return ENODEV;
+ }
+
+ rc = osd_read_ldd(source, ldd);
+ if (rc) {
+ fprintf(stderr, "%s: %s failed to read permanent mount"
+ " data: %s\n", progname, source,
+ rc >= 0 ? strerror(rc) : "");
+ return rc;
+ }
+
+ if ((IS_MDT(ldd) || IS_OST(ldd)) &&
+ (ldd->ldd_flags & LDD_F_NEED_INDEX)) {
+ fprintf(stderr, "%s: %s has no index assigned "
+ "(probably formatted with old mkfs)\n",
+ progname, source);
+ return EINVAL;
+ }
+
+ if (ldd->ldd_flags & LDD_F_UPGRADE14) {
+ fprintf(stderr, "%s: we cannot upgrade %s from this (very old) "
+ "Lustre version\n", progname, source);
+ return EINVAL;
+ }
+
+ if (ldd->ldd_flags & LDD_F_UPDATE)
+ clear_update_ondisk(source, ldd);
+
+ /* Since we never rewrite ldd, ignore temp flags */
+ ldd->ldd_flags &= ~(LDD_F_VIRGIN | LDD_F_WRITECONF);
+
+ /* svname of the form lustre:OST1234 means never registered */
+ rc = strlen(ldd->ldd_svname);
+ if (strcmp(ldd->ldd_svname, "MGS") != 0) {
+ if (rc < 8) {
+ fprintf(stderr, "%s: invalid name '%s'\n",
+ progname, ldd->ldd_svname);
+ return EINVAL;
+ } else if (ldd->ldd_svname[rc - 8] == ':') {
+ ldd->ldd_svname[rc - 8] = '-';
+ ldd->ldd_flags |= LDD_F_VIRGIN;
+ } else if (ldd->ldd_svname[rc - 8] == '=') {
+ ldd->ldd_svname[rc - 8] = '-';
+ ldd->ldd_flags |= LDD_F_WRITECONF;
+ }
+ }
+ /* backend osd type */
+ rc = append_option(options, options_len, "osd=",
+ mt_type(ldd->ldd_mount_type));
+ if (rc != 0)
+ return rc;
+
+ rc = append_option(options, options_len, ldd->ldd_mount_opts, NULL);
+ if (rc != 0)
+ return rc;
+
+ if (!mop->mo_have_mgsnid) {
+ /* Only use disk data if mount -o mgsnode=nid wasn't
+ * specified */
+ if (ldd->ldd_flags & LDD_F_SV_TYPE_MGS) {
+ rc = append_option(options, options_len, "mgs", NULL);
+ if (rc != 0)
+ return rc;
+ mop->mo_have_mgsnid++;
+ } else {
+ if (add_mgsnids(mop, options, ldd->ldd_params,
+ options_len))
+ return E2BIG;
+ }
+ }
+ /* Better have an mgsnid by now */
+ if (!mop->mo_have_mgsnid) {
+ fprintf(stderr, "%s: missing option mgsnode=<nid>\n",
+ progname);
+ return EINVAL;
+ }
+
+ if (ldd->ldd_flags & LDD_F_VIRGIN) {
+ rc = append_option(options, options_len, "virgin", NULL);
+ if (rc != 0)
+ return rc;
+ }
+ if (ldd->ldd_flags & LDD_F_UPDATE) {
+ rc = append_option(options, options_len, "update", NULL);
+ if (rc != 0)
+ return rc;
+ }
+ if (ldd->ldd_flags & LDD_F_WRITECONF) {
+ rc = append_option(options, options_len, "writeconf", NULL);
+ if (rc != 0)
+ return rc;
+ }
+ if (ldd->ldd_flags & LDD_F_NO_PRIMNODE) {
+ rc = append_option(options, options_len, "noprimnode", NULL);
+ if (rc != 0)
+ return rc;
+ }
+
+ /* prefix every lustre parameter with param= so that in-kernel
+ * mount can recognize them properly and send to MGS at registration */
+ start = ldd->ldd_params;
+ while (start && *start != '\0') {
+ while (*start == ' ') start++;
+ if (*start == '\0')
+ break;
+ cur = start;
+ start = strchr(cur, ' ');
+ if (start) {
+ *start = '\0';
+ start++;
+ }
+ rc = append_option(options, options_len, "param=", cur);
+ if (rc != 0)
+ return rc;
+ }
+
+ /* svname must be last option */
+ rc = append_option(options, options_len, "svname=", ldd->ldd_svname);
+
+ return rc;
+}