All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hu Tao <hutao@cn.fujitsu.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, Peter Lieven <pl@kamp.de>,
	Markus Armbruster <armbru@redhat.com>,
	Max Reitz <mreitz@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	y-goto@jp.fujitsu.com
Subject: [Qemu-devel] [PATCH v10 5/6] raw-posix: Add full image preallocation option
Date: Thu, 12 Jun 2014 11:54:26 +0800	[thread overview]
Message-ID: <bfc6628a9900a649425fd47d9a0d374fba17b5c2.1402544518.git.hutao@cn.fujitsu.com> (raw)
In-Reply-To: <cover.1402544517.git.hutao@cn.fujitsu.com>

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

Signed-off-by: Hu Tao <hutao@cn.fujitsu.com>
---
 block/raw-posix.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 52 insertions(+), 7 deletions(-)

diff --git a/block/raw-posix.c b/block/raw-posix.c
index 35057f0..73b10cd 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -30,6 +30,7 @@
 #include "block/thread-pool.h"
 #include "qemu/iov.h"
 #include "raw-aio.h"
+#include "qapi/util.h"
 
 #if defined(__APPLE__) && (__MACH__)
 #include <paths.h>
@@ -1279,6 +1280,8 @@ static int raw_create(const char *filename, QEMUOptionParameter *options,
     int fd;
     int result = 0;
     int64_t total_size = 0;
+    PreallocMode prealloc = PREALLOC_MODE_OFF;
+    Error *error = NULL;
 
     strstart(filename, "file:", &filename);
 
@@ -1286,6 +1289,14 @@ static int raw_create(const char *filename, QEMUOptionParameter *options,
     while (options && options->name) {
         if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
             total_size = ROUND_UP(options->value.n, BDRV_SECTOR_SIZE);
+        } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
+            prealloc = qapi_enum_parse(PreallocMode_lookup, options->value.s,
+                                       PREALLOC_MODE_MAX, PREALLOC_MODE_OFF,
+                                       &error);
+            if (error) {
+                error_propagate(errp, error);
+                return -EINVAL;
+            }
         }
         options++;
     }
@@ -1295,16 +1306,45 @@ static int raw_create(const char *filename, QEMUOptionParameter *options,
     if (fd < 0) {
         result = -errno;
         error_setg_errno(errp, -result, "Could not create file");
-    } else {
-        if (ftruncate(fd, total_size) != 0) {
-            result = -errno;
-            error_setg_errno(errp, -result, "Could not resize file");
+        goto out;
+    }
+    if (ftruncate(fd, total_size) != 0) {
+        result = -errno;
+        error_setg_errno(errp, -result, "Could not resize file");
+        goto out_close;
+    }
+    if (prealloc == PREALLOC_MODE_METADATA) {
+        /* posix_fallocate() doesn't set errno. */
+        result = -posix_fallocate(fd, 0, total_size);
+        if (result != 0) {
+            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");
+    } else if (prealloc == PREALLOC_MODE_FULL) {
+        char *buf = g_malloc0(65536);
+        int64_t num = 0, left = total_size;
+
+        while (left > 0) {
+            num = MIN(left, 65536);
+            result = write(fd, buf, num);
+            if (result < 0) {
+                result = -errno;
+                error_setg_errno(errp, -result,
+                                 "Could not write to the new file");
+                g_free(buf);
+                goto out_close;
+            }
+            left -= num;
         }
+        fsync(fd);
+        g_free(buf);
     }
+out_close:
+    if (qemu_close(fd) != 0 && result == 0) {
+        result = -errno;
+        error_setg_errno(errp, -result, "Could not close the new file");
+    }
+out:
     return result;
 }
 
@@ -1479,6 +1519,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, metadata, full)"
+    },
     { NULL }
 };
 
-- 
1.9.3

  parent reply	other threads:[~2014-06-12  3:56 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-12  3:54 [Qemu-devel] [PATCH v10 0/6] qemu-img: add preallocation=full Hu Tao
2014-06-12  3:54 ` [Qemu-devel] [PATCH v10 1/6] block: round up file size to nearest sector Hu Tao
2014-06-14 18:51   ` Max Reitz
2014-06-12  3:54 ` [Qemu-devel] [PATCH v10 2/6] raw, qcow2: don't convert file size to sector size Hu Tao
2014-06-14 19:00   ` Max Reitz
2014-06-12  3:54 ` [Qemu-devel] [PATCH v10 3/6] rename parse_enum_option to qapi_enum_parse and make it public Hu Tao
2014-06-14 19:07   ` Max Reitz
2014-06-17  2:36     ` Hu Tao
2014-06-12  3:54 ` [Qemu-devel] [PATCH v10 4/6] qapi: introduce PreallocMode and a new PreallocMode full Hu Tao
2014-06-14 19:17   ` Max Reitz
2014-06-25  5:46     ` Hu Tao
2014-06-12  3:54 ` Hu Tao [this message]
2014-06-14 19:38   ` [Qemu-devel] [PATCH v10 5/6] raw-posix: Add full image preallocation option Max Reitz
2014-06-25  6:04     ` Hu Tao
2014-06-12  3:54 ` [Qemu-devel] [PATCH v10 6/6] qcow2: " Hu Tao
2014-06-14 20:37   ` Max Reitz
2014-06-20  8:25     ` Hu Tao
2014-06-20 18:37       ` Max Reitz
2014-06-25  5:41         ` Hu Tao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bfc6628a9900a649425fd47d9a0d374fba17b5c2.1402544518.git.hutao@cn.fujitsu.com \
    --to=hutao@cn.fujitsu.com \
    --cc=armbru@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=pl@kamp.de \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=y-goto@jp.fujitsu.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.