@@ -1941,17 +1941,8 @@ static void bdrv_close(BlockDriverState *bs)
void bdrv_close_all(void)
{
- BlockDriverState *bs;
-
blockdev_close_all_bdrv_states();
-
- QTAILQ_FOREACH(bs, &bdrv_states, device_list) {
- AioContext *aio_context = bdrv_get_aio_context(bs);
-
- aio_context_acquire(aio_context);
- bdrv_close(bs);
- aio_context_release(aio_context);
- }
+ blk_remove_all_bs();
}
/* Check if any requests are in-flight (including throttled requests) */
@@ -207,6 +207,19 @@ void blk_unref(BlockBackend *blk)
}
}
+void blk_remove_all_bs(void)
+{
+ BlockBackend *blk;
+
+ QTAILQ_FOREACH(blk, &blk_backends, link) {
+ AioContext *ctx = blk_get_aio_context(blk);
+
+ aio_context_acquire(ctx);
+ blk_remove_bs(blk);
+ aio_context_release(ctx);
+ }
+}
+
/*
* Return the BlockBackend after @blk.
* If @blk is null, return the first one.
@@ -67,6 +67,7 @@ BlockBackend *blk_new_open(const char *name, const char *filename,
Error **errp);
void blk_ref(BlockBackend *blk);
void blk_unref(BlockBackend *blk);
+void blk_remove_all_bs(void);
const char *blk_name(BlockBackend *blk);
BlockBackend *blk_by_name(const char *name);
BlockBackend *blk_next(BlockBackend *blk);
When bdrv_close_all() is called, instead of force-closing all root BlockDriverStates, it is better to just drop the reference from all BlockBackends and let them be closed automatically. This prevents BDS from getting closed that are still referenced by other BDS, which may result in loss of cached data. Signed-off-by: Max Reitz <mreitz@redhat.com> --- block.c | 11 +---------- block/block-backend.c | 13 +++++++++++++ include/sysemu/block-backend.h | 1 + 3 files changed, 15 insertions(+), 10 deletions(-)