diff --git a/block/snapshot.c b/block/snapshot.c index bf5c2ca5e1..06b1185d27 100644 --- a/block/snapshot.c +++ b/block/snapshot.c @@ -27,6 +27,7 @@ #include "block/block_int.h" #include "qapi/error.h" #include "qapi/qmp/qerror.h" +#include "qapi/qmp/qstring.h" QemuOptsList internal_snapshot_opts = { .name = "snapshot", @@ -189,14 +190,33 @@ int bdrv_snapshot_goto(BlockDriverState *bs, } if (bs->file) { + BlockDriverState *file; + QDict *options = qdict_clone_shallow(bs->options); + QDict *file_options; + + file = bs->file->bs; + /* Prevent it from getting deleted when detached from bs */ + bdrv_ref(file); + + qdict_extract_subqdict(options, &file_options, "file."); + QDECREF(file_options); + qdict_put(options, "file", qstring_from_str(bdrv_get_node_name(file))); + drv->bdrv_close(bs); - ret = bdrv_snapshot_goto(bs->file->bs, snapshot_id); - open_ret = drv->bdrv_open(bs, NULL, bs->open_flags, NULL); + bdrv_unref_child(bs, bs->file); + bs->file = NULL; + + ret = bdrv_snapshot_goto(file, snapshot_id); + open_ret = drv->bdrv_open(bs, options, bs->open_flags, NULL); + QDECREF(options); if (open_ret < 0) { - bdrv_unref(bs->file->bs); + bdrv_unref(file); bs->drv = NULL; return open_ret; } + + assert(bs->file->bs == file); + bdrv_unref(file); return ret; }