qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [RFC PATCH v3 4/6] raw-posix: Add full image preallocation


From: Hu Tao
Subject: [Qemu-devel] [RFC PATCH v3 4/6] raw-posix: Add full image preallocation option
Date: Thu, 19 Dec 2013 10:27:39 +0800

This patch adds a new option preallocation for raw format, and implements
full preallocation.

Signed-off-by: Hu Tao <address@hidden>
---
 block/raw-posix.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/block/raw-posix.c b/block/raw-posix.c
index 19181f2..e09e170 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -1199,11 +1199,22 @@ static int raw_create(const char *filename, 
QEMUOptionParameter *options,
     int fd;
     int result = 0;
     int64_t total_size = 0;
+    int prealloc = PREALLOC_OFF;
 
     /* Read out options */
     while (options && options->name) {
         if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
             total_size = options->value.n / BDRV_SECTOR_SIZE;
+        } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
+            if (!options->value.s || !strcmp(options->value.s, "off")) {
+                prealloc = PREALLOC_OFF;
+            } else if (!strcmp(options->value.s, "full")) {
+                prealloc = PREALLOC_FULL;
+            } else {
+                error_setg(errp, "Invalid preallocation mode: '%s'",
+                           options->value.s);
+                return -EINVAL;
+            }
         }
         options++;
     }
@@ -1218,6 +1229,12 @@ static int raw_create(const char *filename, 
QEMUOptionParameter *options,
             result = -errno;
             error_setg_errno(errp, -result, "Could not resize file");
         }
+        if (prealloc == PREALLOC_FULL &&
+            raw_preallocate2(fd, 0, total_size * BDRV_SECTOR_SIZE) != 0) {
+            result = -errno;
+            error_setg_errno(errp, -result,
+                             "Could not preallocate data for the new file");
+        }
         if (qemu_close(fd) != 0) {
             result = -errno;
             error_setg_errno(errp, -result, "Could not close the new file");
@@ -1373,6 +1390,11 @@ static QEMUOptionParameter raw_create_options[] = {
         .type = OPT_SIZE,
         .help = "Virtual disk size"
     },
+    {
+        .name = BLOCK_OPT_PREALLOC,
+        .type = OPT_STRING,
+        .help = "Preallocation mode (allowed values: off, full)"
+    },
     { NULL }
 };
 
-- 
1.7.11.7




reply via email to

[Prev in Thread] Current Thread [Next in Thread]