Whamcloud - gitweb
LU-1330 obdclass: splits server-side procfs handling
[fs/lustre-release.git] / lustre / utils / mount_utils.c
index 00e5d5c..d754d49 100644 (file)
@@ -26,7 +26,7 @@
 /*
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
- * Copyright (c) 2011, 2012, Intel Corporation.
+ * Copyright (c) 2012, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -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;
@@ -307,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");
@@ -321,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);
 
@@ -356,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;
 }
 
@@ -368,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");
@@ -383,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",
@@ -689,11 +718,23 @@ __u64 get_device_size(char* device)
        return size >> 10;
 }
 
-int file_create(char *path, int size)
+int file_create(char *path, __u64 size)
 {
+       __u64 size_max;
        int ret;
        int fd;
 
+       /*
+        * Since "size" is in KB, the file offset it represents could overflow
+        * off_t.
+        */
+       size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
+       if (size >= size_max) {
+               fprintf(stderr, "%s: "LPU64" KB: Backing store size must be "
+                       "smaller than "LPU64" KB\n", progname, size, size_max);
+               return EFBIG;
+       }
+
        ret = access(path, F_OK);
        if (ret == 0) {
                ret = unlink(path);