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