@@ -2904,7 +2904,7 @@ BlockDriverState *bdrv_next_node(BlockDriverState *bs)
* the monitor or attached to a BlockBackend */
BlockDriverState *bdrv_next(BlockDriverState *bs)
{
- if (!bs || bs->blk) {
+ if (!bs || bdrv_has_blk(bs)) {
bs = blk_next_root_bs(bs);
if (bs) {
return bs;
@@ -2915,7 +2915,7 @@ BlockDriverState *bdrv_next(BlockDriverState *bs)
* handled by the above block already */
do {
bs = bdrv_next_monitor_owned(bs);
- } while (bs && bs->blk);
+ } while (bs && bdrv_has_blk(bs));
return bs;
}
@@ -434,6 +434,23 @@ BlockDriverState *blk_bs(BlockBackend *blk)
}
/*
+ * Returns true if @bs has an associated BlockBackend.
+ */
+bool bdrv_has_blk(BlockDriverState *bs)
+{
+ BdrvChild *child;
+ QLIST_FOREACH(child, &bs->parents, next_parent) {
+ if (child->role == &child_root) {
+ assert(bs->blk);
+ return true;
+ }
+ }
+
+ assert(!bs->blk);
+ return false;
+}
+
+/*
* Return @blk's DriveInfo if any, else null.
*/
DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
@@ -99,6 +99,7 @@ BlockBackend *blk_by_public(BlockBackendPublic *public);
BlockDriverState *blk_bs(BlockBackend *blk);
void blk_remove_bs(BlockBackend *blk);
void blk_insert_bs(BlockBackend *blk, BlockDriverState *bs);
+bool bdrv_has_blk(BlockDriverState *bs);
void blk_set_allow_write_beyond_eof(BlockBackend *blk, bool allow);
void blk_iostatus_enable(BlockBackend *blk);
We just want to know whether a BDS has at least one BB attached in order to avoid enumerating it twice. This doesn't depend on the exact BB that is attached and is still a valid question when more than one BB can be attached, so just answer it by checking the parents list. Signed-off-by: Kevin Wolf <kwolf@redhat.com> --- block.c | 4 ++-- block/block-backend.c | 17 +++++++++++++++++ include/sysemu/block-backend.h | 1 + 3 files changed, 20 insertions(+), 2 deletions(-)