Whamcloud - gitweb
LU-1330 obdclass: splits server-side procfs handling
[fs/lustre-release.git] / lustre / utils / mount_utils.c
index e0502fe..d754d49 100644 (file)
@@ -46,6 +46,7 @@
 #include <lustre_ver.h>
 #include <sys/stat.h>
 #include <sys/utsname.h>
+#include <linux/loop.h>
 
 extern char *progname;
 extern int verbose;
@@ -102,31 +103,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;
 }
 
@@ -319,7 +308,8 @@ int loop_setup(struct mkfs_opts *mop)
        int i, ret = 0;
 
        /* Figure out the loop device names */
-       if (!access("/dev/loop0", F_OK | R_OK)) {
+       if (!access("/dev/loop0", F_OK | R_OK) ||
+           !access("/dev/loop-control", F_OK | R_OK)) {
                strcpy(loop_base, "/dev/loop\0");
        } else if (!access("/dev/loop/0", F_OK | R_OK)) {
                strcpy(loop_base, "/dev/loop/\0");
@@ -333,9 +323,24 @@ int loop_setup(struct mkfs_opts *mop)
                char cmd[PATH_MAX];
                int cmdsz = sizeof(cmd);
 
+#ifdef LOOP_CTL_GET_FREE
+               ret = open("/dev/loop-control", O_RDWR);
+               if (ret < 0) {
+                       fprintf(stderr, "%s: can't access loop control\n", progname);
+                       return EACCES;
+               }
+               /* find or allocate a free loop device to use */
+               i = ioctl(ret, LOOP_CTL_GET_FREE);
+               if (i < 0) {
+                       fprintf(stderr, "%s: access loop control error\n", progname);
+                       return EACCES;
+               }
+               sprintf(l_device, "%s%d", loop_base, i);
+#else
                sprintf(l_device, "%s%d", loop_base, i);
                if (access(l_device, F_OK | R_OK))
                        break;
+#endif
                snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
                ret = system(cmd);
 
@@ -368,11 +373,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;
 }
 
@@ -380,7 +397,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");
@@ -395,7 +412,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",