thebeanogamer / rpms / qemu-kvm

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