Skip to content

Commit

Permalink
block: Add chain helper functions
Browse files Browse the repository at this point in the history
Add some helper functions for skipping filters in a chain of block
nodes.

Signed-off-by: Max Reitz <mreitz@redhat.com>
  • Loading branch information
XanClic authored and kevmw committed Sep 7, 2020
1 parent 9a6fc88 commit d38d7eb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
62 changes: 62 additions & 0 deletions block.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
3 changes: 3 additions & 0 deletions include/block/block_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit d38d7eb

Please sign in to comment.