...and release the reference on bdrv_close_all(). Signed-off-by: Max Reitz <mre...@redhat.com> --- block.c | 2 ++ blockdev.c | 24 ++++++++++++++++++++++++ include/block/block_int.h | 2 ++ 3 files changed, 28 insertions(+)
diff --git a/block.c b/block.c index 64d1524..f096577 100644 --- a/block.c +++ b/block.c @@ -2015,6 +2015,8 @@ static void bdrv_move_feature_fields(BlockDriverState *bs_dest, /* job */ bs_dest->job = bs_src->job; + /* keep the same entry in the list of monitor-owned BDS */ + bs_dest->monitor_list = bs_src->monitor_list; bs_dest->blk = bs_src->blk; memcpy(bs_dest->op_blockers, bs_src->op_blockers, diff --git a/blockdev.c b/blockdev.c index 9db66fd..2801aea 100644 --- a/blockdev.c +++ b/blockdev.c @@ -56,6 +56,11 @@ typedef struct BlockdevCloseAllNotifier { static QTAILQ_HEAD(, BlockdevCloseAllNotifier) close_all_notifiers = QTAILQ_HEAD_INITIALIZER(close_all_notifiers); +static Notifier *close_all_bdrv_states_notifier; + +static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states = + QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states); + static const char *const if_name[IF_COUNT] = { [IF_NONE] = "none", [IF_IDE] = "ide", @@ -129,6 +134,17 @@ static void blockdev_close_all_notifier(Notifier *n, void *data) monitor_blk_unref_with_can(can->blk, can); } +static void blockdev_close_all_bdrv_states(Notifier *n, void *data) +{ + BlockDriverState *bds, *next_bds; + + QTAILQ_FOREACH_SAFE(bds, &monitor_bdrv_states, monitor_list, next_bds) { + bdrv_unref(bds); + } + + g_free(n); +} + /** * Boards may call this to offer board-by-board overrides * of the default, global values. @@ -3231,6 +3247,14 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp) if (!bs) { goto fail; } + + QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); + if (!close_all_bdrv_states_notifier) { + close_all_bdrv_states_notifier = g_new0(Notifier, 1); + close_all_bdrv_states_notifier->notify = + blockdev_close_all_bdrv_states; + bdrv_add_close_all_notifier(close_all_bdrv_states_notifier); + } } if (bs && bdrv_key_required(bs)) { diff --git a/include/block/block_int.h b/include/block/block_int.h index 9005938..61c848a 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -380,6 +380,8 @@ struct BlockDriverState { char node_name[32]; /* element of the list of named nodes building the graph */ QTAILQ_ENTRY(BlockDriverState) node_list; + /* element of the list of monitor-owned BDS */ + QTAILQ_ENTRY(BlockDriverState) monitor_list; QLIST_HEAD(, BdrvDirtyBitmap) dirty_bitmaps; int refcnt; -- 2.1.0