@@ -932,6 +932,7 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options)
bs->open_flags |= BDRV_O_NO_BACKING;
return ret;
}
+ bdrv_ref(bs->backing_hd, false);
return 0;
}
@@ -1075,9 +1076,11 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
if (bs->file != file) {
bdrv_delete(file);
- file = NULL;
}
+ file = NULL;
+ bdrv_ref(bs->file, false);
+
/* If there is a backing file, use it */
if ((flags & BDRV_O_NO_BACKING) == 0) {
QDict *backing_options;
@@ -1375,7 +1378,7 @@ void bdrv_close(BlockDriverState *bs)
if (bs->drv) {
if (bs->backing_hd) {
- bdrv_delete(bs->backing_hd);
+ bdrv_unref(bs->backing_hd, false);
bs->backing_hd = NULL;
}
bs->drv->bdrv_close(bs);
@@ -1399,7 +1402,7 @@ void bdrv_close(BlockDriverState *bs)
bs->options = NULL;
if (bs->file != NULL) {
- bdrv_delete(bs->file);
+ bdrv_unref(bs->file, false);
bs->file = NULL;
}
}
@@ -1587,7 +1590,11 @@ void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
/* The contents of 'tmp' will become bs_top, as we are
* swapping bs_new and bs_top contents. */
+ if (bs_top->backing_hd) {
+ bdrv_unref(bs_top->backing_hd, false);
+ }
bs_top->backing_hd = bs_new;
+ bdrv_ref(bs_new, false);
bs_top->open_flags &= ~BDRV_O_NO_BACKING;
pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file),
bs_new->filename);
@@ -2108,8 +2115,11 @@ int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
if (ret) {
goto exit;
}
+ if (new_top_bs->backing_hd) {
+ bdrv_unref(new_top_bs->backing_hd, false);
+ }
new_top_bs->backing_hd = base_bs;
-
+ bdrv_ref(base_bs, false);
QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) {
/* so that bdrv_close() does not recursively close the chain */
@@ -389,6 +389,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags)
if (ret < 0) {
goto fail;
}
+ bdrv_ref(bs->file, false);
ret = 0;
fail:
@@ -144,6 +144,7 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags)
if (ret < 0) {
goto fail;
}
+ bdrv_ref(bs->file, false);
/* Open the test file */
filename = qemu_opt_get(opts, "x-image");
@@ -99,7 +99,7 @@ int bdrv_snapshot_goto(BlockDriverState *bs,
ret = bdrv_snapshot_goto(bs->file, snapshot_id);
open_ret = drv->bdrv_open(bs, NULL, bs->open_flags);
if (open_ret < 0) {
- bdrv_delete(bs->file);
+ bdrv_unref(bs->file, false);
bs->drv = NULL;
return open_ret;
}
@@ -68,7 +68,7 @@ static void close_unused_images(BlockDriverState *top, BlockDriverState *base,
unused = intermediate;
intermediate = intermediate->backing_hd;
unused->backing_hd = NULL;
- bdrv_delete(unused);
+ bdrv_unref(unused, false);
}
top->backing_hd = base;
}
@@ -2949,6 +2949,7 @@ static int enable_write_target(BDRVVVFATState *s)
s->bs->backing_hd->drv = &vvfat_write_target;
s->bs->backing_hd->opaque = g_malloc(sizeof(void*));
*(void**)s->bs->backing_hd->opaque = s;
+ bdrv_ref(s->bs->backing_hd, false);
return 0;
}
Signed-off-by: Fam Zheng <famz@redhat.com> --- block.c | 18 ++++++++++++++---- block/blkdebug.c | 1 + block/blkverify.c | 1 + block/snapshot.c | 2 +- block/stream.c | 2 +- block/vvfat.c | 1 + 6 files changed, 19 insertions(+), 6 deletions(-)