[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-block] [PULL 07/48] block: Allow references for backing files
From: |
Kevin Wolf |
Subject: |
[Qemu-block] [PULL 07/48] block: Allow references for backing files |
Date: |
Fri, 18 Dec 2015 16:07:13 +0100 |
For bs->file, using references to existing BDSes has been possible for a
while already. This patch enables the same for bs->backing.
Signed-off-by: Kevin Wolf <address@hidden>
Reviewed-by: Max Reitz <address@hidden>
---
block.c | 48 +++++++++++++++++++++++++++++-------------------
block/mirror.c | 2 +-
include/block/block.h | 3 ++-
3 files changed, 32 insertions(+), 21 deletions(-)
diff --git a/block.c b/block.c
index 57bf8fc..84b9bf4 100644
--- a/block.c
+++ b/block.c
@@ -1182,30 +1182,43 @@ out:
/*
* Opens the backing file for a BlockDriverState if not yet open
*
- * options is a QDict of options to pass to the block drivers, or NULL for an
- * empty set of options. The reference to the QDict is transferred to this
- * function (even on failure), so if the caller intends to reuse the
dictionary,
- * it needs to use QINCREF() before calling bdrv_file_open.
+ * bdref_key specifies the key for the image's BlockdevRef in the options
QDict.
+ * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
+ * itself, all options starting with "${bdref_key}." are considered part of the
+ * BlockdevRef.
+ *
+ * TODO Can this be unified with bdrv_open_image()?
*/
-int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)
+int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
+ const char *bdref_key, Error **errp)
{
char *backing_filename = g_malloc0(PATH_MAX);
+ char *bdref_key_dot;
+ const char *reference = NULL;
int ret = 0;
BlockDriverState *backing_hd;
+ QDict *options;
+ QDict *tmp_parent_options = NULL;
Error *local_err = NULL;
if (bs->backing != NULL) {
- QDECREF(options);
goto free_exit;
}
/* NULL means an empty set of options */
- if (options == NULL) {
- options = qdict_new();
+ if (parent_options == NULL) {
+ tmp_parent_options = qdict_new();
+ parent_options = tmp_parent_options;
}
bs->open_flags &= ~BDRV_O_NO_BACKING;
- if (qdict_haskey(options, "file.filename")) {
+
+ bdref_key_dot = g_strdup_printf("%s.", bdref_key);
+ qdict_extract_subqdict(parent_options, &options, bdref_key_dot);
+ g_free(bdref_key_dot);
+
+ reference = qdict_get_try_str(parent_options, bdref_key);
+ if (reference || qdict_haskey(options, "file.filename")) {
backing_filename[0] = '\0';
} else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
QDECREF(options);
@@ -1228,19 +1241,16 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict
*options, Error **errp)
goto free_exit;
}
- backing_hd = bdrv_new();
-
if (bs->backing_format[0] != '\0' && !qdict_haskey(options, "driver")) {
qdict_put(options, "driver", qstring_from_str(bs->backing_format));
}
- assert(bs->backing == NULL);
+ backing_hd = NULL;
ret = bdrv_open_inherit(&backing_hd,
*backing_filename ? backing_filename : NULL,
- NULL, options, 0, bs, &child_backing, &local_err);
+ reference, options, 0, bs, &child_backing,
+ &local_err);
if (ret < 0) {
- bdrv_unref(backing_hd);
- backing_hd = NULL;
bs->open_flags |= BDRV_O_NO_BACKING;
error_setg(errp, "Could not open backing file: %s",
error_get_pretty(local_err));
@@ -1253,8 +1263,11 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict
*options, Error **errp)
bdrv_set_backing_hd(bs, backing_hd);
bdrv_unref(backing_hd);
+ qdict_del(parent_options, bdref_key);
+
free_exit:
g_free(backing_filename);
+ QDECREF(tmp_parent_options);
return ret;
}
@@ -1537,10 +1550,7 @@ static int bdrv_open_inherit(BlockDriverState **pbs,
const char *filename,
/* If there is a backing file, use it */
if ((flags & BDRV_O_NO_BACKING) == 0) {
- QDict *backing_options;
-
- qdict_extract_subqdict(options, &backing_options, "backing.");
- ret = bdrv_open_backing_file(bs, backing_options, &local_err);
+ ret = bdrv_open_backing_file(bs, options, "backing", &local_err);
if (ret < 0) {
goto close_and_fail;
}
diff --git a/block/mirror.c b/block/mirror.c
index 8e3f524..fc34a9c 100644
--- a/block/mirror.c
+++ b/block/mirror.c
@@ -652,7 +652,7 @@ static void mirror_complete(BlockJob *job, Error **errp)
Error *local_err = NULL;
int ret;
- ret = bdrv_open_backing_file(s->target, NULL, &local_err);
+ ret = bdrv_open_backing_file(s->target, NULL, "backing", &local_err);
if (ret < 0) {
error_propagate(errp, local_err);
return;
diff --git a/include/block/block.h b/include/block/block.h
index d048bbf..2e77288 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -210,7 +210,8 @@ BdrvChild *bdrv_open_child(const char *filename,
const BdrvChildRole *child_role,
bool allow_none, Error **errp);
void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd);
-int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp);
+int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,
+ const char *bdref_key, Error **errp);
int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags, Error **errp);
int bdrv_open(BlockDriverState **pbs, const char *filename,
const char *reference, QDict *options, int flags, Error **errp);
--
1.8.3.1
- [Qemu-block] [PULL 13/48] block: reopen: Document option precedence and refactor accordingly, (continued)
- [Qemu-block] [PULL 13/48] block: reopen: Document option precedence and refactor accordingly, Kevin Wolf, 2015/12/18
- [Qemu-block] [PULL 10/48] block: Pass driver-specific options to .bdrv_refresh_filename(), Kevin Wolf, 2015/12/18
- [Qemu-block] [PULL 47/48] qemu-img: abort when full_backing_filename not present, Kevin Wolf, 2015/12/18
- [Qemu-block] [PULL 37/48] block: Remove prototype of bdrv_swap from header, Kevin Wolf, 2015/12/18
- [Qemu-block] [PULL 30/48] qcow2: Use intermediate helper CB for amend, Kevin Wolf, 2015/12/18
- [Qemu-block] [PULL 16/48] block: Introduce bs->explicit_options, Kevin Wolf, 2015/12/18
- [Qemu-block] [PULL 26/48] block: Add opaque value to the amend CB, Kevin Wolf, 2015/12/18
- [Qemu-block] [PULL 32/48] qcow2: Invoke refcount order amendment function, Kevin Wolf, 2015/12/18
- [Qemu-block] [PULL 11/48] block: Keep "driver" in bs->options, Kevin Wolf, 2015/12/18
- [Qemu-block] [PULL 09/48] block: Exclude nested options only for children in append_open_options(), Kevin Wolf, 2015/12/18
- [Qemu-block] [PULL 07/48] block: Allow references for backing files,
Kevin Wolf <=
- Re: [Qemu-block] [Qemu-devel] [PULL 00/48] Block patches, Peter Maydell, 2015/12/18
- [Qemu-block] [PULL 12/48] block: Allow specifying child options in reopen, Kevin Wolf, 2015/12/18
- [Qemu-block] [PULL 18/48] qemu-iotests: Remove cache mode test without medium, Kevin Wolf, 2015/12/18