Whamcloud - gitweb
LU-4416 mem: truncate_pagecache oldsize removed
[fs/lustre-release.git] / lustre / utils / mount_utils.c
index b8e97c4..1fca4c7 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/
@@ -102,19 +102,31 @@ int run_command(char *cmd, int cmdsz)
 
 int add_param(char *buf, char *key, char *val)
 {
-       int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
-       int start = strlen(buf);
-       int keylen = 0;
+       char *sub_val = NULL;
+       int   buflen = strlen(buf);
+       int   end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
+       int   start = 0;
+       int   keylen = 0;
 
-       if (key)
+       if (key != NULL)
                keylen = strlen(key);
-       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;
+
+       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);
        }
 
-       sprintf(buf + start, " %s%s", key ? key : "", val);
        return 0;
 }
 
@@ -339,7 +351,8 @@ int loop_setup(struct mkfs_opts *mop)
                                continue;
                        if (ret) {
                                fprintf(stderr, "%s: error %d on losetup: %s\n",
-                                       progname, ret, strerror(ret));
+                                       progname, ret,
+                                       ret >= 0 ? strerror(ret) : "");
                                return ret;
                        }
                        strscpy(mop->mo_loopdev, l_device,
@@ -355,11 +368,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;
 }
 
@@ -367,7 +392,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");
@@ -382,7 +407,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",
@@ -688,11 +713,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);