From: Max Reitz <mreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
qemu-devel@nongnu.org, Max Reitz <mreitz@redhat.com>
Subject: [Qemu-devel] [PATCH 4/7] block: Generic file creation fallback
Date: Fri, 12 Jul 2019 19:35:57 +0200 [thread overview]
Message-ID: <20190712173600.14554-5-mreitz@redhat.com> (raw)
In-Reply-To: <20190712173600.14554-1-mreitz@redhat.com>
If a protocol driver does not support image creation, we can see whether
maybe the file exists already. If so, just truncating it will be
sufficient.
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
block.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 71 insertions(+), 12 deletions(-)
diff --git a/block.c b/block.c
index c139540f2b..5466585501 100644
--- a/block.c
+++ b/block.c
@@ -531,20 +531,63 @@ out:
return ret;
}
-int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
+static int bdrv_create_file_fallback(const char *filename, BlockDriver *drv,
+ QemuOpts *opts, Error **errp)
{
- BlockDriver *drv;
+ BlockBackend *blk;
+ QDict *options = qdict_new();
+ int64_t size = 0;
+ char *buf = NULL;
+ PreallocMode prealloc;
Error *local_err = NULL;
int ret;
+ size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
+ buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
+ prealloc = qapi_enum_parse(&PreallocMode_lookup, buf,
+ PREALLOC_MODE_OFF, &local_err);
+ g_free(buf);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ return -EINVAL;
+ }
+
+ if (prealloc != PREALLOC_MODE_OFF) {
+ error_setg(errp, "Unsupported preallocation mode '%s'",
+ PreallocMode_str(prealloc));
+ return -ENOTSUP;
+ }
+
+ qdict_put_str(options, "driver", drv->format_name);
+
+ blk = blk_new_open(filename, NULL, options,
+ BDRV_O_RDWR | BDRV_O_RESIZE, errp);
+ if (!blk) {
+ error_prepend(errp, "Protocol driver '%s' does not support image "
+ "creation, and opening the image failed: ",
+ drv->format_name);
+ return -EINVAL;
+ }
+
+ ret = blk_truncate_for_formatting(blk, size, errp);
+ blk_unref(blk);
+ return ret;
+}
+
+int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
+{
+ BlockDriver *drv;
+
drv = bdrv_find_protocol(filename, true, errp);
if (drv == NULL) {
return -ENOENT;
}
- ret = bdrv_create(drv, filename, opts, &local_err);
- error_propagate(errp, local_err);
- return ret;
+ if (drv->bdrv_co_create_opts) {
+ return bdrv_create(drv, filename, opts, errp);
+ } else {
+ return bdrv_create_file_fallback(filename, drv, opts, errp);
+ }
}
/**
@@ -1420,6 +1463,24 @@ QemuOptsList bdrv_runtime_opts = {
},
};
+static QemuOptsList fallback_create_opts = {
+ .name = "fallback-create-opts",
+ .head = QTAILQ_HEAD_INITIALIZER(fallback_create_opts.head),
+ .desc = {
+ {
+ .name = BLOCK_OPT_SIZE,
+ .type = QEMU_OPT_SIZE,
+ .help = "Virtual disk size"
+ },
+ {
+ .name = BLOCK_OPT_PREALLOC,
+ .type = QEMU_OPT_STRING,
+ .help = "Preallocation mode (allowed values: off)"
+ },
+ { /* end of list */ }
+ }
+};
+
/*
* Common part for opening disk images and files
*
@@ -5681,14 +5742,12 @@ void bdrv_img_create(const char *filename, const char *fmt,
return;
}
- if (!proto_drv->create_opts) {
- error_setg(errp, "Protocol driver '%s' does not support image creation",
- proto_drv->format_name);
- return;
- }
-
create_opts = qemu_opts_append(create_opts, drv->create_opts);
- create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
+ if (proto_drv->create_opts) {
+ create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
+ } else {
+ create_opts = qemu_opts_append(create_opts, &fallback_create_opts);
+ }
/* Create parameter list with default values */
opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
--
2.21.0
next prev parent reply other threads:[~2019-07-12 17:36 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-07-12 17:35 [Qemu-devel] [PATCH 0/7] block: Generic file creation fallback Max Reitz
2019-07-12 17:35 ` [Qemu-devel] [PATCH 1/7] block/nbd: Fix hang in .bdrv_close() Max Reitz
2019-07-16 13:08 ` [Qemu-devel] [Qemu-block] " Maxim Levitsky
2019-07-12 17:35 ` [Qemu-devel] [PATCH 2/7] block: Add blk_truncate_for_formatting() Max Reitz
2019-07-16 13:08 ` [Qemu-devel] [Qemu-block] " Maxim Levitsky
2019-07-16 15:45 ` Maxim Levitsky
2019-07-16 16:03 ` Max Reitz
2019-07-12 17:35 ` [Qemu-devel] [PATCH 3/7] block: Use blk_truncate_for_formatting() Max Reitz
2019-07-16 13:08 ` [Qemu-devel] [Qemu-block] " Maxim Levitsky
2019-07-12 17:35 ` Max Reitz [this message]
2019-07-16 13:09 ` [Qemu-devel] [Qemu-block] [PATCH 4/7] block: Generic file creation fallback Maxim Levitsky
2019-07-12 17:35 ` [Qemu-devel] [PATCH 5/7] file-posix: Drop hdev_co_create_opts() Max Reitz
2019-07-16 13:09 ` [Qemu-devel] [Qemu-block] " Maxim Levitsky
2019-07-12 17:35 ` [Qemu-devel] [PATCH 6/7] iscsi: Drop iscsi_co_create_opts() Max Reitz
2019-07-16 13:08 ` [Qemu-devel] [Qemu-block] " Maxim Levitsky
2019-07-12 17:36 ` [Qemu-devel] [PATCH 7/7] iotests: Add test for image creation fallback Max Reitz
2019-07-15 9:31 ` Thomas Huth
2019-07-15 9:48 ` Max Reitz
2019-07-16 14:10 ` Eric Blake
2019-09-05 13:30 ` [Qemu-devel] [Qemu-block] [PATCH 0/7] block: Generic file " Maxim Levitsky
2019-09-10 9:16 ` Max Reitz
2019-09-10 10:40 ` Maxim Levitsky
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190712173600.14554-5-mreitz@redhat.com \
--to=mreitz@redhat.com \
--cc=kwolf@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).