902636
From 882d09226b7f45b72c5b7763c4c4aba182e0f8a1 Mon Sep 17 00:00:00 2001
902636
From: Maxim Levitsky <mlevitsk@redhat.com>
902636
Date: Wed, 11 Mar 2020 10:51:43 +0000
902636
Subject: [PATCH 02/20] block: Generic file creation fallback
902636
902636
RH-Author: Maxim Levitsky <mlevitsk@redhat.com>
902636
Message-id: <20200311105147.13208-3-mlevitsk@redhat.com>
902636
Patchwork-id: 94227
902636
O-Subject: [RHEL-AV-8.2.0 qemu-kvm PATCH v2 2/6] block: Generic file creation fallback
902636
Bugzilla: 1640894
902636
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
902636
RH-Acked-by: John Snow <jsnow@redhat.com>
902636
RH-Acked-by: Max Reitz <mreitz@redhat.com>
902636
902636
From: Max Reitz <mreitz@redhat.com>
902636
902636
If a protocol driver does not support image creation, we can see whether
902636
maybe the file exists already.  If so, just truncating it will be
902636
sufficient.
902636
902636
Signed-off-by: Max Reitz <mreitz@redhat.com>
902636
Message-Id: <20200122164532.178040-3-mreitz@redhat.com>
902636
Signed-off-by: Max Reitz <mreitz@redhat.com>
902636
(cherry picked from commit fd17146cd93d1704cd96d7c2757b325fc7aac6fd)
902636
Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
902636
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
902636
---
902636
 block.c | 159 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
902636
 1 file changed, 147 insertions(+), 12 deletions(-)
902636
902636
diff --git a/block.c b/block.c
902636
index 2e5e8b6..3beec7f 100644
902636
--- a/block.c
902636
+++ b/block.c
902636
@@ -532,20 +532,139 @@ out:
902636
     return ret;
902636
 }
902636
 
902636
-int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
902636
+/**
902636
+ * Helper function for bdrv_create_file_fallback(): Resize @blk to at
902636
+ * least the given @minimum_size.
902636
+ *
902636
+ * On success, return @blk's actual length.
902636
+ * Otherwise, return -errno.
902636
+ */
902636
+static int64_t create_file_fallback_truncate(BlockBackend *blk,
902636
+                                             int64_t minimum_size, Error **errp)
902636
 {
902636
-    BlockDriver *drv;
902636
+    Error *local_err = NULL;
902636
+    int64_t size;
902636
+    int ret;
902636
+
902636
+    ret = blk_truncate(blk, minimum_size, false, PREALLOC_MODE_OFF, &local_err);
902636
+    if (ret < 0 && ret != -ENOTSUP) {
902636
+        error_propagate(errp, local_err);
902636
+        return ret;
902636
+    }
902636
+
902636
+    size = blk_getlength(blk);
902636
+    if (size < 0) {
902636
+        error_free(local_err);
902636
+        error_setg_errno(errp, -size,
902636
+                         "Failed to inquire the new image file's length");
902636
+        return size;
902636
+    }
902636
+
902636
+    if (size < minimum_size) {
902636
+        /* Need to grow the image, but we failed to do that */
902636
+        error_propagate(errp, local_err);
902636
+        return -ENOTSUP;
902636
+    }
902636
+
902636
+    error_free(local_err);
902636
+    local_err = NULL;
902636
+
902636
+    return size;
902636
+}
902636
+
902636
+/**
902636
+ * Helper function for bdrv_create_file_fallback(): Zero the first
902636
+ * sector to remove any potentially pre-existing image header.
902636
+ */
902636
+static int create_file_fallback_zero_first_sector(BlockBackend *blk,
902636
+                                                  int64_t current_size,
902636
+                                                  Error **errp)
902636
+{
902636
+    int64_t bytes_to_clear;
902636
+    int ret;
902636
+
902636
+    bytes_to_clear = MIN(current_size, BDRV_SECTOR_SIZE);
902636
+    if (bytes_to_clear) {
902636
+        ret = blk_pwrite_zeroes(blk, 0, bytes_to_clear, BDRV_REQ_MAY_UNMAP);
902636
+        if (ret < 0) {
902636
+            error_setg_errno(errp, -ret,
902636
+                             "Failed to clear the new image's first sector");
902636
+            return ret;
902636
+        }
902636
+    }
902636
+
902636
+    return 0;
902636
+}
902636
+
902636
+static int bdrv_create_file_fallback(const char *filename, BlockDriver *drv,
902636
+                                     QemuOpts *opts, Error **errp)
902636
+{
902636
+    BlockBackend *blk;
902636
+    QDict *options = qdict_new();
902636
+    int64_t size = 0;
902636
+    char *buf = NULL;
902636
+    PreallocMode prealloc;
902636
     Error *local_err = NULL;
902636
     int ret;
902636
 
902636
+    size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
902636
+    buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
902636
+    prealloc = qapi_enum_parse(&PreallocMode_lookup, buf,
902636
+                               PREALLOC_MODE_OFF, &local_err);
902636
+    g_free(buf);
902636
+    if (local_err) {
902636
+        error_propagate(errp, local_err);
902636
+        return -EINVAL;
902636
+    }
902636
+
902636
+    if (prealloc != PREALLOC_MODE_OFF) {
902636
+        error_setg(errp, "Unsupported preallocation mode '%s'",
902636
+                   PreallocMode_str(prealloc));
902636
+        return -ENOTSUP;
902636
+    }
902636
+
902636
+    qdict_put_str(options, "driver", drv->format_name);
902636
+
902636
+    blk = blk_new_open(filename, NULL, options,
902636
+                       BDRV_O_RDWR | BDRV_O_RESIZE, errp);
902636
+    if (!blk) {
902636
+        error_prepend(errp, "Protocol driver '%s' does not support image "
902636
+                      "creation, and opening the image failed: ",
902636
+                      drv->format_name);
902636
+        return -EINVAL;
902636
+    }
902636
+
902636
+    size = create_file_fallback_truncate(blk, size, errp);
902636
+    if (size < 0) {
902636
+        ret = size;
902636
+        goto out;
902636
+    }
902636
+
902636
+    ret = create_file_fallback_zero_first_sector(blk, size, errp);
902636
+    if (ret < 0) {
902636
+        goto out;
902636
+    }
902636
+
902636
+    ret = 0;
902636
+out:
902636
+    blk_unref(blk);
902636
+    return ret;
902636
+}
902636
+
902636
+int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
902636
+{
902636
+    BlockDriver *drv;
902636
+
902636
     drv = bdrv_find_protocol(filename, true, errp);
902636
     if (drv == NULL) {
902636
         return -ENOENT;
902636
     }
902636
 
902636
-    ret = bdrv_create(drv, filename, opts, &local_err);
902636
-    error_propagate(errp, local_err);
902636
-    return ret;
902636
+    if (drv->bdrv_co_create_opts) {
902636
+        return bdrv_create(drv, filename, opts, errp);
902636
+    } else {
902636
+        return bdrv_create_file_fallback(filename, drv, opts, errp);
902636
+    }
902636
 }
902636
 
902636
 /**
902636
@@ -1422,6 +1541,24 @@ QemuOptsList bdrv_runtime_opts = {
902636
     },
902636
 };
902636
 
902636
+static QemuOptsList fallback_create_opts = {
902636
+    .name = "fallback-create-opts",
902636
+    .head = QTAILQ_HEAD_INITIALIZER(fallback_create_opts.head),
902636
+    .desc = {
902636
+        {
902636
+            .name = BLOCK_OPT_SIZE,
902636
+            .type = QEMU_OPT_SIZE,
902636
+            .help = "Virtual disk size"
902636
+        },
902636
+        {
902636
+            .name = BLOCK_OPT_PREALLOC,
902636
+            .type = QEMU_OPT_STRING,
902636
+            .help = "Preallocation mode (allowed values: off)"
902636
+        },
902636
+        { /* end of list */ }
902636
+    }
902636
+};
902636
+
902636
 /*
902636
  * Common part for opening disk images and files
902636
  *
902636
@@ -5743,14 +5880,12 @@ void bdrv_img_create(const char *filename, const char *fmt,
902636
         return;
902636
     }
902636
 
902636
-    if (!proto_drv->create_opts) {
902636
-        error_setg(errp, "Protocol driver '%s' does not support image creation",
902636
-                   proto_drv->format_name);
902636
-        return;
902636
-    }
902636
-
902636
     create_opts = qemu_opts_append(create_opts, drv->create_opts);
902636
-    create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
902636
+    if (proto_drv->create_opts) {
902636
+        create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
902636
+    } else {
902636
+        create_opts = qemu_opts_append(create_opts, &fallback_create_opts);
902636
+    }
902636
 
902636
     /* Create parameter list with default values */
902636
     opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
902636
-- 
902636
1.8.3.1
902636