diff mbox

[v5,11/13] block: Add blk_remove_all_bs()

Message ID 1425413591-31413-12-git-send-email-mreitz@redhat.com
State New
Headers show

Commit Message

Max Reitz March 3, 2015, 8:13 p.m. UTC
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.

This patch adds a function for doing that, but does not yet incorporate
it in bdrv_close_all().

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 block/block-backend.c          | 13 +++++++++++++
 include/sysemu/block-backend.h |  1 +
 2 files changed, 14 insertions(+)

Comments

Eric Blake March 19, 2015, 10:16 p.m. UTC | #1
On 03/03/2015 01:13 PM, Max Reitz wrote:
> 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.
> 
> This patch adds a function for doing that, but does not yet incorporate
> it in bdrv_close_all().
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
>  block/block-backend.c          | 13 +++++++++++++
>  include/sysemu/block-backend.h |  1 +
>  2 files changed, 14 insertions(+)

Reviewed-by: Eric Blake <eblake@redhat.com>
diff mbox

Patch

diff --git a/block/block-backend.c b/block/block-backend.c
index 76e6893..8eedc52 100644
--- a/block/block-backend.c
+++ b/block/block-backend.c
@@ -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.
diff --git a/include/sysemu/block-backend.h b/include/sysemu/block-backend.h
index 3725e20..71f5292 100644
--- a/include/sysemu/block-backend.h
+++ b/include/sysemu/block-backend.h
@@ -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);