9ae3a8
From f984e0debc17b26f36c8f73027fac11d813826d7 Mon Sep 17 00:00:00 2001
9ae3a8
From: Max Reitz <mreitz@redhat.com>
9ae3a8
Date: Mon, 10 Nov 2014 09:14:05 +0100
9ae3a8
Subject: [PATCH 28/41] qapi: introduce PreallocMode and new PreallocModes full
9ae3a8
 and falloc.
9ae3a8
9ae3a8
Message-id: <1415610847-15383-3-git-send-email-mreitz@redhat.com>
9ae3a8
Patchwork-id: 62238
9ae3a8
O-Subject: [RHEL-7.1 qemu-kvm PATCH v2 2/4] qapi: introduce PreallocMode and new PreallocModes full and falloc.
9ae3a8
Bugzilla: 1087724
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
From: Hu Tao <hutao@cn.fujitsu.com>
9ae3a8
9ae3a8
This patch prepares for the subsequent patches.
9ae3a8
9ae3a8
Signed-off-by: Hu Tao <hutao@cn.fujitsu.com>
9ae3a8
Reviewed-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Reviewed-by: Eric Blake <eblake@redhat.com>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit ffeaac9b4e23a3033e8120cc34bacadc09487f1b)
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	block/qcow2.c
9ae3a8
	qapi/block-core.json
9ae3a8
9ae3a8
Downstream's qapi-schema.json has not (yet) been split into multiple
9ae3a8
files such as qapi/block-core.json.
9ae3a8
9ae3a8
The conflicts in block/qcow2.c result from the fact that
9ae3a8
QEMUOptionParameter has not been replaced with QemuOpts downstream
9ae3a8
(upstream 1bd0e2d1c40ef1dbe717728197071e931abe22a4).
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2.c              | 28 +++++++++++++++++-----------
9ae3a8
 qapi-schema.json           | 18 ++++++++++++++++++
9ae3a8
 tests/qemu-iotests/049.out |  2 +-
9ae3a8
 3 files changed, 36 insertions(+), 12 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/qcow2.c b/block/qcow2.c
9ae3a8
index a679355..99c6b93 100644
9ae3a8
--- a/block/qcow2.c
9ae3a8
+++ b/block/qcow2.c
9ae3a8
@@ -30,6 +30,7 @@
9ae3a8
 #include "qemu/error-report.h"
9ae3a8
 #include "qapi/qmp/qerror.h"
9ae3a8
 #include "qapi/qmp/qbool.h"
9ae3a8
+#include "qapi/util.h"
9ae3a8
 #include "trace.h"
9ae3a8
 
9ae3a8
 /*
9ae3a8
@@ -1555,7 +1556,7 @@ static int preallocate(BlockDriverState *bs)
9ae3a8
 
9ae3a8
 static int qcow2_create2(const char *filename, int64_t total_size,
9ae3a8
                          const char *backing_file, const char *backing_format,
9ae3a8
-                         int flags, size_t cluster_size, int prealloc,
9ae3a8
+                         int flags, size_t cluster_size, PreallocMode prealloc,
9ae3a8
                          QEMUOptionParameter *options, int version,
9ae3a8
                          Error **errp)
9ae3a8
 {
9ae3a8
@@ -1690,7 +1691,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
9ae3a8
     }
9ae3a8
 
9ae3a8
     /* And if we're supposed to preallocate metadata, do that now */
9ae3a8
-    if (prealloc) {
9ae3a8
+    if (prealloc == PREALLOC_MODE_METADATA) {
9ae3a8
         BDRVQcowState *s = bs->opaque;
9ae3a8
         qemu_co_mutex_lock(&s->lock);
9ae3a8
         ret = preallocate(bs);
9ae3a8
@@ -1715,7 +1716,7 @@ static int qcow2_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
     uint64_t sectors = 0;
9ae3a8
     int flags = 0;
9ae3a8
     size_t cluster_size = DEFAULT_CLUSTER_SIZE;
9ae3a8
-    int prealloc = 0;
9ae3a8
+    PreallocMode prealloc = PREALLOC_MODE_OFF;
9ae3a8
     int version = 3;
9ae3a8
     Error *local_err = NULL;
9ae3a8
     int ret;
9ae3a8
@@ -1735,13 +1736,11 @@ static int qcow2_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
                 cluster_size = options->value.n;
9ae3a8
             }
9ae3a8
         } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
9ae3a8
-            if (!options->value.s || !strcmp(options->value.s, "off")) {
9ae3a8
-                prealloc = 0;
9ae3a8
-            } else if (!strcmp(options->value.s, "metadata")) {
9ae3a8
-                prealloc = 1;
9ae3a8
-            } else {
9ae3a8
-                error_setg(errp, "Invalid preallocation mode: '%s'",
9ae3a8
-                           options->value.s);
9ae3a8
+            prealloc = qapi_enum_parse(PreallocMode_lookup, options->value.s,
9ae3a8
+                                       PREALLOC_MODE_MAX, PREALLOC_MODE_OFF,
9ae3a8
+                                       &local_err);
9ae3a8
+            if (local_err) {
9ae3a8
+                error_propagate(errp, local_err);
9ae3a8
                 return -EINVAL;
9ae3a8
             }
9ae3a8
         } else if (!strcmp(options->name, BLOCK_OPT_COMPAT_LEVEL)) {
9ae3a8
@@ -1762,7 +1761,14 @@ static int qcow2_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
         options++;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    if (backing_file && prealloc) {
9ae3a8
+    if (prealloc != PREALLOC_MODE_OFF &&
9ae3a8
+        prealloc != PREALLOC_MODE_METADATA) {
9ae3a8
+        error_setg(errp, "Unsupported preallocate mode: %s",
9ae3a8
+                   PreallocMode_lookup[prealloc]);
9ae3a8
+        return -EINVAL;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    if (backing_file && prealloc != PREALLOC_MODE_OFF) {
9ae3a8
         error_setg(errp, "Backing file and preallocation cannot be used at "
9ae3a8
                    "the same time");
9ae3a8
         return -EINVAL;
9ae3a8
diff --git a/qapi-schema.json b/qapi-schema.json
9ae3a8
index 31ac5c5..2af2643 100644
9ae3a8
--- a/qapi-schema.json
9ae3a8
+++ b/qapi-schema.json
9ae3a8
@@ -4118,6 +4118,24 @@
9ae3a8
 { 'command': 'blockdev-add', 'data': { 'options': 'BlockdevOptions' } }
9ae3a8
 
9ae3a8
 ##
9ae3a8
+# @PreallocMode
9ae3a8
+#
9ae3a8
+# Preallocation mode of QEMU image file
9ae3a8
+#
9ae3a8
+# @off: no preallocation
9ae3a8
+# @metadata: preallocate only for metadata
9ae3a8
+# @falloc: like @full preallocation but allocate disk space by
9ae3a8
+#          posix_fallocate() rather than writing zeros.
9ae3a8
+# @full: preallocate all data by writing zeros to device to ensure disk
9ae3a8
+#        space is really available. @full preallocation also sets up
9ae3a8
+#        metadata correctly.
9ae3a8
+#
9ae3a8
+# Since 2.2
9ae3a8
+##
9ae3a8
+{ 'enum': 'PreallocMode',
9ae3a8
+  'data': [ 'off', 'metadata', 'falloc', 'full' ] }
9ae3a8
+
9ae3a8
+##
9ae3a8
 # @RxState:
9ae3a8
 #
9ae3a8
 # Packets receiving state
9ae3a8
diff --git a/tests/qemu-iotests/049.out b/tests/qemu-iotests/049.out
9ae3a8
index 96e83a4..b2fcf0b 100644
9ae3a8
--- a/tests/qemu-iotests/049.out
9ae3a8
+++ b/tests/qemu-iotests/049.out
9ae3a8
@@ -179,7 +179,7 @@ qemu-img create -f qcow2 -o preallocation=metadata TEST_DIR/t.qcow2 64M
9ae3a8
 Formatting 'TEST_DIR/t.qcow2', fmt=qcow2 size=67108864 encryption=off cluster_size=65536 preallocation='metadata' lazy_refcounts=off 
9ae3a8
 
9ae3a8
 qemu-img create -f qcow2 -o preallocation=1234 TEST_DIR/t.qcow2 64M
9ae3a8
-qemu-img: TEST_DIR/t.qcow2: Invalid preallocation mode: '1234'
9ae3a8
+qemu-img: TEST_DIR/t.qcow2: invalid parameter value: 1234
9ae3a8
 Formatting 'TEST_DIR/t.qcow2', fmt=qcow2 size=67108864 encryption=off cluster_size=65536 preallocation='1234' lazy_refcounts=off 
9ae3a8
 
9ae3a8
 == Check encryption option ==
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8