diff --git a/block.c b/block.c index 209ba39d31..58d12c029d 100644 --- a/block.c +++ b/block.c @@ -1409,6 +1409,7 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename, } if (child_role) { + bs->inherits_from = parent; flags = child_role->inherit_flags(parent->open_flags); } @@ -1837,6 +1838,9 @@ void bdrv_close(BlockDriverState *bs) BdrvChild *child, *next; QLIST_FOREACH_SAFE(child, &bs->children, next, next) { + if (child->bs->inherits_from == bs) { + child->bs->inherits_from = NULL; + } QLIST_REMOVE(child, next); g_free(child); } @@ -1985,6 +1989,7 @@ static void bdrv_move_feature_fields(BlockDriverState *bs_dest, void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old) { BlockDriverState tmp; + BdrvChild *child; bdrv_drain(bs_new); bdrv_drain(bs_old); @@ -2044,6 +2049,18 @@ void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old) QLIST_FIX_HEAD_PTR(&bs_new->children, next); QLIST_FIX_HEAD_PTR(&bs_old->children, next); + /* Update references in bs->opaque and children */ + QLIST_FOREACH(child, &bs_old->children, next) { + if (child->bs->inherits_from == bs_new) { + child->bs->inherits_from = bs_old; + } + } + QLIST_FOREACH(child, &bs_new->children, next) { + if (child->bs->inherits_from == bs_old) { + child->bs->inherits_from = bs_new; + } + } + bdrv_rebind(bs_new); bdrv_rebind(bs_old); } diff --git a/include/block/block_int.h b/include/block/block_int.h index 4ae58606a6..2732ccdaae 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -437,6 +437,10 @@ struct BlockDriverState { /* long-running background operation */ BlockJob *job; + /* The node that this node inherited default options from (and a reopen on + * which can affect this node by changing these defaults). This is always a + * parent node of this node. */ + BlockDriverState *inherits_from; QLIST_HEAD(, BdrvChild) children; QDict *options;