- glob_t glob_info;
- struct stat stat_buf;
- char *chk_major, *chk_minor;
- char *savept, *dev, *s2 = 0;
- char buf[PATH_MAX], path[PATH_MAX];
- int i, rc = 0;
- int major, minor;
-
- if (!source)
- return -EINVAL;
-
- if (strncmp(source, "/dev/loop", 9) == 0)
- return 0;
-
- if ((*source != '/') && ((s2 = strpbrk(source, ",:")) != NULL))
- return 0;
-
- dev = source + src_len - 1;
- while (dev > source && (*dev != '/')) {
- if (isdigit(*dev))
- *dev = 0;
- dev--;
- }
- snprintf(path, sizeof(path), "/sys/block%s/%s", dev,
- MAX_HW_SECTORS_KB_PATH);
- rc = read_file(path, buf, sizeof(buf));
- if (!rc && (strlen(buf)-1)) {
- snprintf(path, sizeof(path), "/sys/block%s/%s", dev,
- MAX_SECTORS_KB_PATH);
- rc = write_file(path, buf);
- if (rc) {
- fprintf(stderr, "warning: opening %s: %s\n",
- path, strerror(errno));
- return rc;
- }
- } else if (rc == ENOENT) {
- /* The name of the device say 'X' specified in /dev/X may not match
- * any entry under /sys/block/. In that case we need to match
- * the major/minor number to find the entry under sys/block
- * corresponding to /dev/X */
-
- dev = source + src_len - 1;
- while (dev > source) {
- if (isdigit(*dev))
- *dev = 0;
- dev--;
- }
-
- rc = stat(dev, &stat_buf);
- if (rc) {
- fprintf(stderr, "warning: %s, Stat failed for device %s\n",
- strerror(errno), dev);
- return rc;
- }
- major = major(stat_buf.st_rdev);
- minor = minor(stat_buf.st_rdev);
- rc = glob("/sys/block/*", GLOB_NOSORT, NULL, &glob_info);
- if (rc) {
- fprintf(stderr, "warning: failed to read entries under /sys/block\n");
- return rc;
- }
-
- for (i = 0; i < glob_info.gl_pathc; i++){
- snprintf(path, sizeof(path), "%s/dev", glob_info.gl_pathv[i]);
-
- rc = read_file(path, buf, sizeof(buf));
- if (rc)
- continue;
-
- if (buf[strlen(buf)-1] == '\n')
- buf[strlen(buf)-1] = '\0';
-
- chk_major = strtok_r(buf, ":", &savept);
- chk_minor = savept;
- if (major == atoi(chk_major) && minor == atoi(chk_minor))
- break;
- }
+ struct lustre_disk_data *ldd = &mop->mo_ldd;
+ char *cur, *start;
+ int rc;
+
+ 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 (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 */
+ append_option(options, "osd=");
+ strcat(options, mt_type(ldd->ldd_mount_type));
+
+ append_option(options, ldd->ldd_mount_opts);
+
+ 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) {
+ append_option(options, "mgs");
+ mop->mo_have_mgsnid++;
+ } else {
+ add_mgsnids(mop, options, ldd->ldd_params);
+ }
+ }
+ /* 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)
+ append_option(options, "virgin");
+ if (ldd->ldd_flags & LDD_F_UPDATE)
+ append_option(options, "update");
+ if (ldd->ldd_flags & LDD_F_WRITECONF)
+ append_option(options, "writeconf");
+ if (ldd->ldd_flags & LDD_F_NO_PRIMNODE)
+ append_option(options, "noprimnode");
+
+ /* 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++;
+ }
+ append_option(options, "param=");
+ strcat(options, cur);
+ }
+
+ /* svname must be last option */
+ append_option(options, "svname=");
+ strcat(options, ldd->ldd_svname);
+
+ return 0;
+}