[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-block] [PATCH v2 for-2.7 2/8] block: Let bdrv_open_inherit() retur
From: |
Max Reitz |
Subject: |
[Qemu-block] [PATCH v2 for-2.7 2/8] block: Let bdrv_open_inherit() return the snapshot |
Date: |
Wed, 6 Apr 2016 19:57:08 +0200 |
If bdrv_open_inherit() creates a snapshot BDS and *pbs is NULL, that
snapshot BDS should be returned instead of the BDS under it.
To this end, bdrv_append_temp_snapshot() now returns the snapshot BDS
instead of just appending it on top of the snapshotted BDS. Also, it
calls bdrv_ref() before bdrv_append() (which bdrv_open_inherit() has to
undo if not returning the overlay).
Signed-off-by: Max Reitz <address@hidden>
---
block.c | 33 ++++++++++++++++++++++++++-------
1 file changed, 26 insertions(+), 7 deletions(-)
diff --git a/block.c b/block.c
index b6a452a..d222227 100644
--- a/block.c
+++ b/block.c
@@ -1424,8 +1424,10 @@ done:
return c;
}
-static int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags,
- QDict *snapshot_options, Error **errp)
+static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,
+ int flags,
+ QDict *snapshot_options,
+ Error **errp)
{
/* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
char *tmp_filename = g_malloc0(PATH_MAX + 1);
@@ -1441,7 +1443,6 @@ static int bdrv_append_temp_snapshot(BlockDriverState
*bs, int flags,
/* Get the required size from the image */
total_size = bdrv_getlength(bs);
if (total_size < 0) {
- ret = total_size;
error_setg_errno(errp, -total_size, "Could not get image size");
goto out;
}
@@ -1481,12 +1482,16 @@ static int bdrv_append_temp_snapshot(BlockDriverState
*bs, int flags,
goto out;
}
+ bdrv_ref(bs_snapshot);
bdrv_append(bs_snapshot, bs);
+ g_free(tmp_filename);
+ return bs_snapshot;
+
out:
QDECREF(snapshot_options);
g_free(tmp_filename);
- return ret;
+ return NULL;
}
/*
@@ -1705,17 +1710,31 @@ static int bdrv_open_inherit(BlockDriverState **pbs,
const char *filename,
}
QDECREF(options);
- *pbs = bs;
/* For snapshot=on, create a temporary qcow2 overlay. bs points to the
* temporary snapshot afterwards. */
if (snapshot_flags) {
- ret = bdrv_append_temp_snapshot(bs, snapshot_flags, snapshot_options,
- &local_err);
+ BlockDriverState *snapshot_bs;
+ snapshot_bs = bdrv_append_temp_snapshot(bs, snapshot_flags,
+ snapshot_options, &local_err);
snapshot_options = NULL;
if (local_err) {
+ ret = -EINVAL;
goto close_and_fail;
}
+ if (!*pbs) {
+ /* The reference is now held by the overlay BDS */
+ bdrv_unref(bs);
+ bs = snapshot_bs;
+ } else {
+ /* It is still referenced in the same way that *pbs was referenced,
+ * however that may be */
+ bdrv_unref(snapshot_bs);
+ }
+ }
+
+ if (!*pbs) {
+ *pbs = bs;
}
return 0;
--
2.8.0
- [Qemu-block] [PATCH v2 for-2.7 0/8] blockdev: (Nearly) free clean-up work, Max Reitz, 2016/04/06
- [Qemu-block] [PATCH v2 for-2.7 3/8] tests: Drop BDS from test-throttle.c, Max Reitz, 2016/04/06
- [Qemu-block] [PATCH v2 for-2.7 4/8] block: Drop blk_new_with_bs(), Max Reitz, 2016/04/06
- [Qemu-block] [PATCH v2 for-2.7 5/8] block: Drop bdrv_new_root(), Max Reitz, 2016/04/06
- [Qemu-block] [PATCH v2 for-2.7 8/8] block: Drop bdrv_parent_cb_...() from bdrv_close(), Max Reitz, 2016/04/06