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