Whamcloud - gitweb
LU-4460 mount: fix lmd_parse() to handle comma-separated NIDs
[fs/lustre-release.git] / lustre / utils / mount_utils.c
index cdc1ed6..31cec4a 100644 (file)
@@ -46,7 +46,6 @@
 #include <lustre_ver.h>
 #include <sys/stat.h>
 #include <sys/utsname.h>
-#include <linux/loop.h>
 
 extern char *progname;
 extern int verbose;
@@ -103,31 +102,19 @@ int run_command(char *cmd, int cmdsz)
 
 int add_param(char *buf, char *key, char *val)
 {
-       char *sub_val = NULL;
-       int   buflen = strlen(buf);
-       int   end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
-       int   start = 0;
-       int   keylen = 0;
+       int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
+       int start = strlen(buf);
+       int keylen = 0;
 
-       if (key != NULL)
+       if (key)
                keylen = strlen(key);
-
-       start = buflen;
-       while ((sub_val = strsep(&val, ",")) != NULL) {
-               if (*sub_val == 0)
-                       continue;
-
-               if (start + 1 + keylen + strlen(sub_val) >= end) {
-                       fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
-                               progname, buf, key != NULL ? key : "", sub_val);
-                       buf[buflen] = '\0';
-                       return 1;
-               }
-
-               sprintf(buf + start, " %s%s", key != NULL ? key : "", sub_val);
-               start = strlen(buf);
+       if (start + 1 + keylen + strlen(val) >= end) {
+               fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
+                       progname, buf, key ? key : "", val);
+               return 1;
        }
 
+       sprintf(buf + start, " %s%s", key ? key : "", val);
        return 0;
 }
 
@@ -170,58 +157,27 @@ char *strscpy(char *dst, char *src, int buflen)
        return strscat(dst, src, buflen);
 }
 
-static int check_losetup(char *device, char *file_path)
-{
-       struct loop_info64 loop_info;
-       int fd;
-       int rc;
-
-       fd = open(device, O_RDONLY);
-       if (fd < 0)
-               return errno;
-
-       rc = ioctl(fd, LOOP_GET_STATUS64, (void *)&loop_info);
-       close(fd);
-       if (rc < 0)
-               return errno;
-
-       return strcmp(file_path, (char *)(loop_info.lo_file_name));
-}
-
 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
 {
        FILE *fp;
        struct mntent *mnt;
-       char lo_dev[] = "/dev/loop";
-       int lo_dev_len = strlen(lo_dev);
-       int ret = 0;
 
        fp = setmntent(MOUNTED, "r");
        if (fp == NULL)
                return 0;
 
        while ((mnt = getmntent(fp)) != NULL) {
-               char *fsname = mnt->mnt_fsname;
-
-               if ((strcmp(fsname, spec1) == 0 ||
-                    strcmp(fsname, spec2) == 0) &&
+               if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
+                    strcmp(mnt->mnt_fsname, spec2) == 0) &&
                    (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
                    (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
-                       ret = EEXIST;
-                       break;
-               }
-               /* Check if the loop device is already mounted */
-               if (strncmp(fsname, lo_dev, lo_dev_len) != 0)
-                       continue;
-               if (check_losetup(fsname, spec1) == 0 ||
-                   check_losetup(fsname, spec2) == 0) {
-                       ret = EEXIST;
-                       break;
+                       endmntent(fp);
+                       return(EEXIST);
                }
        }
        endmntent(fp);
 
-       return ret;
+       return 0;
 }
 
 #define PROC_DIR       "/proc/"
@@ -400,11 +356,23 @@ int loop_setup(struct mkfs_opts *mop)
 int loop_cleanup(struct mkfs_opts *mop)
 {
        char cmd[150];
-       int ret = 1;
+       int ret = 0;
+
        if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
+               int tries;
+
                sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
-               ret = run_command(cmd, sizeof(cmd));
+               for (tries = 0; tries < 3; tries++) {
+                       ret = run_command(cmd, sizeof(cmd));
+                       if (ret == 0)
+                               break;
+                       sleep(1);
+               }
        }
+
+       if (ret != 0)
+               fprintf(stderr, "cannot cleanup %s: rc = %d\n",
+                       mop->mo_loopdev, ret);
        return ret;
 }
 
@@ -412,7 +380,7 @@ int loop_format(struct mkfs_opts *mop)
 {
        int fd;
 
-       if (mop->mo_device_sz == 0) {
+       if (mop->mo_device_kb == 0) {
                fatal();
                fprintf(stderr, "loop device requires a --device-size= "
                        "param\n");
@@ -427,7 +395,7 @@ int loop_format(struct mkfs_opts *mop)
                return errno;
        }
 
-       if (ftruncate(fd, mop->mo_device_sz * 1024) != 0) {
+       if (ftruncate(fd, mop->mo_device_kb * 1024) != 0) {
                close(fd);
                fatal();
                fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
@@ -776,40 +744,3 @@ int file_create(char *path, __u64 size)
 
        return 0;
 }
-
-/**
- * Try to get the real path to the device, in case it is a
- * symbolic link or dm device for instance
- */
-int get_realpath(char *path, char **device)
-{
-       FILE *f;
-       size_t sz;
-       char *ptr;
-       char name[256];
-       char real_path[PATH_MAX] = {'\0'};
-
-       if (realpath(path, real_path) == NULL)
-               return errno;
-
-       ptr = strrchr(real_path, '/');
-       if (ptr && strncmp(ptr, "/dm-", 4) == 0 && isdigit(*(ptr + 4))) {
-               snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptr+1);
-               f = fopen(path, "r");
-               if (f != NULL) {
-                       /* read "<name>\n" from sysfs */
-                       if (fgets(name, sizeof(name), f) != NULL) {
-                               sz = strlen(name);
-                               if (sz > 1) {
-                                       name[sz - 1] = '\0';
-                                       snprintf(real_path, sizeof(real_path),
-                                                "/dev/mapper/%s", name);
-                               }
-                       }
-                       fclose(f);
-               }
-       }
-       *device = strdup(real_path);
-
-       return 0;
-}