[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v8 02/43] block: Add chain helper functions
From: |
Max Reitz |
Subject: |
[PATCH v8 02/43] block: Add chain helper functions |
Date: |
Tue, 1 Sep 2020 16:33:43 +0200 |
Add some helper functions for skipping filters in a chain of block
nodes.
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
include/block/block_int.h | 3 ++
block.c | 62 +++++++++++++++++++++++++++++++++++++++
2 files changed, 65 insertions(+)
diff --git a/include/block/block_int.h b/include/block/block_int.h
index f280a95b26..8205ccaa62 100644
--- a/include/block/block_int.h
+++ b/include/block/block_int.h
@@ -1394,6 +1394,9 @@ BdrvChild *bdrv_cow_child(BlockDriverState *bs);
BdrvChild *bdrv_filter_child(BlockDriverState *bs);
BdrvChild *bdrv_filter_or_cow_child(BlockDriverState *bs);
BdrvChild *bdrv_primary_child(BlockDriverState *bs);
+BlockDriverState *bdrv_skip_implicit_filters(BlockDriverState *bs);
+BlockDriverState *bdrv_skip_filters(BlockDriverState *bs);
+BlockDriverState *bdrv_backing_chain_next(BlockDriverState *bs);
static inline BlockDriverState *child_bs(BdrvChild *child)
{
diff --git a/block.c b/block.c
index 9f866e8676..b162142609 100644
--- a/block.c
+++ b/block.c
@@ -7034,3 +7034,65 @@ BdrvChild *bdrv_primary_child(BlockDriverState *bs)
return found;
}
+
+static BlockDriverState *bdrv_do_skip_filters(BlockDriverState *bs,
+ bool stop_on_explicit_filter)
+{
+ BdrvChild *c;
+
+ if (!bs) {
+ return NULL;
+ }
+
+ while (!(stop_on_explicit_filter && !bs->implicit)) {
+ c = bdrv_filter_child(bs);
+ if (!c) {
+ /*
+ * A filter that is embedded in a working block graph must
+ * have a child. Assert this here so this function does
+ * not return a filter node that is not expected by the
+ * caller.
+ */
+ assert(!bs->drv || !bs->drv->is_filter);
+ break;
+ }
+ bs = c->bs;
+ }
+ /*
+ * Note that this treats nodes with bs->drv == NULL as not being
+ * filters (bs->drv == NULL should be replaced by something else
+ * anyway).
+ * The advantage of this behavior is that this function will thus
+ * always return a non-NULL value (given a non-NULL @bs).
+ */
+
+ return bs;
+}
+
+/*
+ * Return the first BDS that has not been added implicitly or that
+ * does not have a filtered child down the chain starting from @bs
+ * (including @bs itself).
+ */
+BlockDriverState *bdrv_skip_implicit_filters(BlockDriverState *bs)
+{
+ return bdrv_do_skip_filters(bs, true);
+}
+
+/*
+ * Return the first BDS that does not have a filtered child down the
+ * chain starting from @bs (including @bs itself).
+ */
+BlockDriverState *bdrv_skip_filters(BlockDriverState *bs)
+{
+ return bdrv_do_skip_filters(bs, false);
+}
+
+/*
+ * For a backing chain, return the first non-filter backing image of
+ * the first non-filter image.
+ */
+BlockDriverState *bdrv_backing_chain_next(BlockDriverState *bs)
+{
+ return bdrv_skip_filters(bdrv_cow_bs(bdrv_skip_filters(bs)));
+}
--
2.26.2
- [PATCH v8 00/43] block: Deal with filters, Max Reitz, 2020/09/01
- [PATCH v8 01/43] block: Add child access functions, Max Reitz, 2020/09/01
- [PATCH v8 03/43] block: bdrv_cow_child() for bdrv_has_zero_init(), Max Reitz, 2020/09/01
- [PATCH v8 02/43] block: Add chain helper functions,
Max Reitz <=
- [PATCH v8 04/43] block: bdrv_set_backing_hd() is about bs->backing, Max Reitz, 2020/09/01
- [PATCH v8 06/43] block: Drop bdrv_is_encrypted(), Max Reitz, 2020/09/01
- [PATCH v8 05/43] block: Include filters when freezing backing chain, Max Reitz, 2020/09/01
- [PATCH v8 07/43] block: Add bdrv_supports_compressed_writes(), Max Reitz, 2020/09/01
- [PATCH v8 08/43] throttle: Support compressed writes, Max Reitz, 2020/09/01
- [PATCH v8 09/43] copy-on-read: Support compressed writes, Max Reitz, 2020/09/01
- [PATCH v8 10/43] block: Use bdrv_filter_(bs|child) where obvious, Max Reitz, 2020/09/01
- [PATCH v8 11/43] block: Use CAFs in block status functions, Max Reitz, 2020/09/01
- [PATCH v8 12/43] stream: Deal with filters, Max Reitz, 2020/09/01
- [PATCH v8 13/43] block: Use CAFs when working with backing chains, Max Reitz, 2020/09/01