BlockBackend name is empty if it is added with '-blockdev' and '-device' options or hotplugged with QMP but callers still expect backend to be accesible by name for operations like commit or statistics access. Intoduce blk_lookup function to search both by name and BDS-root node_name.
Signed-off-by: Anton Kuchin <antonkuc...@yandex-team.ru> --- block/block-backend.c | 29 +++++++++++++++++++++++++++++ include/sysemu/block-backend.h | 7 +++++++ 2 files changed, 36 insertions(+) diff --git a/block/block-backend.c b/block/block-backend.c index 60d37a0c3d..86a492853c 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -684,6 +684,35 @@ static BlockBackend *bdrv_first_blk(BlockDriverState *bs) return NULL; } +/* + * Return the BlockBackend that has attached BDS-tree root with + * node_name @node_name if it exists, else null. + * @node_name must not be null. + */ +static BlockBackend *blk_by_root_name(const char *node_name) +{ + BlockBackend *blk = NULL; + + assert(node_name); + while ((blk = blk_all_next(blk)) != NULL) { + BlockDriverState *bs = blk_bs(blk); + if (bs && !strcmp(node_name, bs->node_name)) { + return blk; + } + } + return NULL; +} + +BlockBackend *blk_lookup(const char *name) +{ + assert(name); + BlockBackend *blk = blk_by_name(name); + if (!blk) { + blk = blk_by_root_name(name); + } + return blk; +} + /* * Returns true if @bs has an associated BlockBackend. */ diff --git a/include/sysemu/block-backend.h b/include/sysemu/block-backend.h index c96bcdee14..290b8f8fc9 100644 --- a/include/sysemu/block-backend.h +++ b/include/sysemu/block-backend.h @@ -85,6 +85,13 @@ void blk_unref(BlockBackend *blk); void blk_remove_all_bs(void); const char *blk_name(const BlockBackend *blk); BlockBackend *blk_by_name(const char *name); + +/* + * Search BlockBackend by name or root BlockDriverSate node_name. + * Hotplug BlockBackends have no name so need to also check BDS-tree roots + * @name must not be null. + */ +BlockBackend *blk_lookup(const char *name); BlockBackend *blk_next(BlockBackend *blk); BlockBackend *blk_all_next(BlockBackend *blk); bool monitor_add_blk(BlockBackend *blk, const char *name, Error **errp); -- 2.19.1