[PATCH] block: Wire up 'flat' mode also for 'query-block'

Peter Krempa posted 1 patch 1 month, 4 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/38fa3ec064ed20282fb61f70ffed8e0abb3abbfa.1765445716.git.pkrempa@redhat.com
Maintainers: Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>, Eric Blake <eblake@redhat.com>, Markus Armbruster <armbru@redhat.com>, Peter Maydell <peter.maydell@linaro.org>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>, "Marc-André Lureau" <marcandre.lureau@redhat.com>
There is a newer version of this series
block/monitor/block-hmp-cmds.c | 4 ++--
block/qapi.c                   | 9 +++++----
qapi/block-core.json           | 4 ++++
ui/cocoa.m                     | 2 +-
4 files changed, 12 insertions(+), 7 deletions(-)
[PATCH] block: Wire up 'flat' mode also for 'query-block'
Posted by Peter Krempa 1 month, 4 weeks ago
From: Peter Krempa <pkrempa@redhat.com>

Some time ago (commit facda5443f5a8) I've added 'flat' mode (which
omits 'backing-image' key in reply) to 'query-named-block-nodes' to
minimize the size of the returned JSON for deeper backing chains.

While 'query-block' behaved slightly better it turns out that in libvirt
we do call 'query-block' to figure out some information about the
block device (e.g. throttling info) but we don't look at the backing
chain itself.

Wire up 'flat' for 'query-block' so that libvirt can ask for an
abbreviated output. The implementation is much simpler as the internals
are shared with 'query-named-block-nodes'.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 block/monitor/block-hmp-cmds.c | 4 ++--
 block/qapi.c                   | 9 +++++----
 qapi/block-core.json           | 4 ++++
 ui/cocoa.m                     | 2 +-
 4 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/block/monitor/block-hmp-cmds.c b/block/monitor/block-hmp-cmds.c
index 3640d1f3dc..d301f56a39 100644
--- a/block/monitor/block-hmp-cmds.c
+++ b/block/monitor/block-hmp-cmds.c
@@ -422,7 +422,7 @@ void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
     /* Then try adding all block devices.  If one fails, close all and
      * exit.
      */
-    block_list = qmp_query_block(NULL);
+    block_list = qmp_query_block(false, false, NULL);

     for (info = block_list; info; info = info->next) {
         if (!info->value->inserted) {
@@ -741,7 +741,7 @@ void hmp_info_block(Monitor *mon, const QDict *qdict)

     /* Print BlockBackend information */
     if (!nodes) {
-        block_list = qmp_query_block(NULL);
+        block_list = qmp_query_block(false, false, NULL);
     } else {
         block_list = NULL;
     }
diff --git a/block/qapi.c b/block/qapi.c
index 9f5771e019..2fc38d67a0 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -456,7 +456,7 @@ fail:

 /* @p_info will be set only on success. */
 static void GRAPH_RDLOCK
-bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, Error **errp)
+bdrv_query_info(BlockBackend *blk, bool flat, BlockInfo **p_info, Error **errp)
 {
     BlockInfo *info = g_malloc0(sizeof(*info));
     BlockDriverState *bs = blk_bs(blk);
@@ -488,7 +488,7 @@ bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, Error **errp)
     }

     if (bs && bs->drv) {
-        info->inserted = bdrv_block_device_info(blk, bs, false, errp);
+        info->inserted = bdrv_block_device_info(blk, bs, flat, errp);
         if (info->inserted == NULL) {
             goto err;
         }
@@ -698,11 +698,12 @@ bdrv_query_bds_stats(BlockDriverState *bs, bool blk_level)
     return s;
 }

-BlockInfoList *qmp_query_block(Error **errp)
+BlockInfoList *qmp_query_block(bool has_flat, bool flat, Error **errp)
 {
     BlockInfoList *head = NULL, **p_next = &head;
     BlockBackend *blk;
     Error *local_err = NULL;
+    bool return_flat = has_flat && flat;

     GRAPH_RDLOCK_GUARD_MAINLOOP();

@@ -714,7 +715,7 @@ BlockInfoList *qmp_query_block(Error **errp)
         }

         info = g_malloc0(sizeof(*info));
-        bdrv_query_info(blk, &info->value, &local_err);
+        bdrv_query_info(blk, return_flat, &info->value, &local_err);
         if (local_err) {
             error_propagate(errp, local_err);
             g_free(info);
diff --git a/qapi/block-core.json b/qapi/block-core.json
index b82af74256..e89d878544 100644
--- a/qapi/block-core.json
+++ b/qapi/block-core.json
@@ -855,6 +855,9 @@
 #
 # Get a list of `BlockInfo` for all virtual block devices.
 #
+# @flat: Omit the nested data about backing image ("backing-image"
+#     key) if true.  Default is false (Since 11.0)
+#
 # Returns: a list describing each virtual block device.  Filter nodes
 #     that were created implicitly are skipped over.
 #
@@ -945,6 +948,7 @@
 #        }
 ##
 { 'command': 'query-block', 'returns': ['BlockInfo'],
+  'data': { '*flat': 'bool' },
   'allow-preconfig': true }

 ##
diff --git a/ui/cocoa.m b/ui/cocoa.m
index 23b7a736d7..5b21fe3aea 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -1849,7 +1849,7 @@ static void addRemovableDevicesMenuItems(void)
     BlockInfoList *currentDevice, *pointerToFree;
     NSString *deviceName;

-    currentDevice = qmp_query_block(NULL);
+    currentDevice = qmp_query_block(false, false, NULL);
     pointerToFree = currentDevice;

     menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu];
-- 
2.52.0
Re: [PATCH] block: Wire up 'flat' mode also for 'query-block'
Posted by Markus Armbruster 3 weeks, 6 days ago
Peter Krempa <pkrempa@redhat.com> writes:

> From: Peter Krempa <pkrempa@redhat.com>
>
> Some time ago (commit facda5443f5a8) I've added 'flat' mode (which
> omits 'backing-image' key in reply) to 'query-named-block-nodes' to
> minimize the size of the returned JSON for deeper backing chains.

Almost six years.  Time flies :)

> While 'query-block' behaved slightly better it turns out that in libvirt
> we do call 'query-block' to figure out some information about the
> block device (e.g. throttling info) but we don't look at the backing
> chain itself.
>
> Wire up 'flat' for 'query-block' so that libvirt can ask for an
> abbreviated output. The implementation is much simpler as the internals
> are shared with 'query-named-block-nodes'.
>
> Signed-off-by: Peter Krempa <pkrempa@redhat.com>
> ---
>  block/monitor/block-hmp-cmds.c | 4 ++--
>  block/qapi.c                   | 9 +++++----
>  qapi/block-core.json           | 4 ++++
>  ui/cocoa.m                     | 2 +-
>  4 files changed, 12 insertions(+), 7 deletions(-)
>
> diff --git a/block/monitor/block-hmp-cmds.c b/block/monitor/block-hmp-cmds.c
> index 3640d1f3dc..d301f56a39 100644
> --- a/block/monitor/block-hmp-cmds.c
> +++ b/block/monitor/block-hmp-cmds.c
> @@ -422,7 +422,7 @@ void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
>      /* Then try adding all block devices.  If one fails, close all and
>       * exit.
>       */
> -    block_list = qmp_query_block(NULL);
> +    block_list = qmp_query_block(false, false, NULL);

Is info->value->inserted->image->backing_image used here?

If not, we can pass true and save allocations.  Thought, not a demand.

Same for the other calls.

>
>      for (info = block_list; info; info = info->next) {
>          if (!info->value->inserted) {
> @@ -741,7 +741,7 @@ void hmp_info_block(Monitor *mon, const QDict *qdict)
>
>      /* Print BlockBackend information */
>      if (!nodes) {
> -        block_list = qmp_query_block(NULL);
> +        block_list = qmp_query_block(false, false, NULL);
>      } else {
>          block_list = NULL;
>      }
> diff --git a/block/qapi.c b/block/qapi.c
> index 9f5771e019..2fc38d67a0 100644
> --- a/block/qapi.c
> +++ b/block/qapi.c
> @@ -456,7 +456,7 @@ fail:
>
>  /* @p_info will be set only on success. */
>  static void GRAPH_RDLOCK
> -bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, Error **errp)
> +bdrv_query_info(BlockBackend *blk, bool flat, BlockInfo **p_info, Error **errp)
>  {
>      BlockInfo *info = g_malloc0(sizeof(*info));
>      BlockDriverState *bs = blk_bs(blk);
> @@ -488,7 +488,7 @@ bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, Error **errp)
>      }
>
>      if (bs && bs->drv) {
> -        info->inserted = bdrv_block_device_info(blk, bs, false, errp);
> +        info->inserted = bdrv_block_device_info(blk, bs, flat, errp);
>          if (info->inserted == NULL) {
>              goto err;
>          }
> @@ -698,11 +698,12 @@ bdrv_query_bds_stats(BlockDriverState *bs, bool blk_level)
>      return s;
>  }
>
> -BlockInfoList *qmp_query_block(Error **errp)
> +BlockInfoList *qmp_query_block(bool has_flat, bool flat, Error **errp)
>  {
>      BlockInfoList *head = NULL, **p_next = &head;
>      BlockBackend *blk;
>      Error *local_err = NULL;
> +    bool return_flat = has_flat && flat;
>
>      GRAPH_RDLOCK_GUARD_MAINLOOP();
>
> @@ -714,7 +715,7 @@ BlockInfoList *qmp_query_block(Error **errp)
>          }
>
>          info = g_malloc0(sizeof(*info));
> -        bdrv_query_info(blk, &info->value, &local_err);
> +        bdrv_query_info(blk, return_flat, &info->value, &local_err);
>          if (local_err) {
>              error_propagate(errp, local_err);
>              g_free(info);
> diff --git a/qapi/block-core.json b/qapi/block-core.json
> index b82af74256..e89d878544 100644
> --- a/qapi/block-core.json
> +++ b/qapi/block-core.json
> @@ -855,6 +855,9 @@
>  #
>  # Get a list of `BlockInfo` for all virtual block devices.
>  #
> +# @flat: Omit the nested data about backing image ("backing-image"
> +#     key) if true.  Default is false (Since 11.0)
> +#

The "backing-image" key is actually BlockInfo member inserted member
image member backing-image.  This is even more deeply nested than for
query-named-block-nodes.  Doc text good enough?

>  # Returns: a list describing each virtual block device.  Filter nodes
>  #     that were created implicitly are skipped over.
>  #
> @@ -945,6 +948,7 @@
>  #        }
>  ##
>  { 'command': 'query-block', 'returns': ['BlockInfo'],
> +  'data': { '*flat': 'bool' },
>    'allow-preconfig': true }
>
>  ##

QAPI schema
Acked-by: Markus Armbruster <armbru@redhat.com>

> diff --git a/ui/cocoa.m b/ui/cocoa.m
> index 23b7a736d7..5b21fe3aea 100644
> --- a/ui/cocoa.m
> +++ b/ui/cocoa.m
> @@ -1849,7 +1849,7 @@ static void addRemovableDevicesMenuItems(void)
>      BlockInfoList *currentDevice, *pointerToFree;
>      NSString *deviceName;
>
> -    currentDevice = qmp_query_block(NULL);
> +    currentDevice = qmp_query_block(false, false, NULL);
>      pointerToFree = currentDevice;
>
>      menu = [[[NSApp mainMenu] itemWithTitle:@"Machine"] submenu];
Re: [PATCH] block: Wire up 'flat' mode also for 'query-block'
Posted by Peter Krempa 3 weeks, 5 days ago
On Mon, Jan 12, 2026 at 11:29:24 +0100, Markus Armbruster wrote:
> Peter Krempa <pkrempa@redhat.com> writes:
> 
> > From: Peter Krempa <pkrempa@redhat.com>
> >
> > Some time ago (commit facda5443f5a8) I've added 'flat' mode (which
> > omits 'backing-image' key in reply) to 'query-named-block-nodes' to
> > minimize the size of the returned JSON for deeper backing chains.
> 
> Almost six years.  Time flies :)

Indeed! Actually last year (already) I've deleted the corresponding
capability since libvirt now only supports QEMUs which do support this
:D

> 
> > While 'query-block' behaved slightly better it turns out that in libvirt
> > we do call 'query-block' to figure out some information about the
> > block device (e.g. throttling info) but we don't look at the backing
> > chain itself.
> >
> > Wire up 'flat' for 'query-block' so that libvirt can ask for an
> > abbreviated output. The implementation is much simpler as the internals
> > are shared with 'query-named-block-nodes'.
> >
> > Signed-off-by: Peter Krempa <pkrempa@redhat.com>
> > ---
> >  block/monitor/block-hmp-cmds.c | 4 ++--
> >  block/qapi.c                   | 9 +++++----
> >  qapi/block-core.json           | 4 ++++
> >  ui/cocoa.m                     | 2 +-
> >  4 files changed, 12 insertions(+), 7 deletions(-)
> >
> > diff --git a/block/monitor/block-hmp-cmds.c b/block/monitor/block-hmp-cmds.c
> > index 3640d1f3dc..d301f56a39 100644
> > --- a/block/monitor/block-hmp-cmds.c
> > +++ b/block/monitor/block-hmp-cmds.c
> > @@ -422,7 +422,7 @@ void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
> >      /* Then try adding all block devices.  If one fails, close all and
> >       * exit.
> >       */
> > -    block_list = qmp_query_block(NULL);
> > +    block_list = qmp_query_block(false, false, NULL);
> 
> Is info->value->inserted->image->backing_image used here?
> 
> If not, we can pass true and save allocations.  Thought, not a demand.

It's not used in hmp_nbd_server_start. I'll do a separate patch for it
as it's simple enough, but IMO not related to this patch which should
keep behaviour of unrelated code.

> Same for the other calls.
> 
> >
> >      for (info = block_list; info; info = info->next) {
> >          if (!info->value->inserted) {
> > @@ -741,7 +741,7 @@ void hmp_info_block(Monitor *mon, const QDict *qdict)

... but this one does use it (although for questionably useful
information: first backing image name and the depth of the backing chain).


[...]


> > diff --git a/qapi/block-core.json b/qapi/block-core.json
> > index b82af74256..e89d878544 100644
> > --- a/qapi/block-core.json
> > +++ b/qapi/block-core.json
> > @@ -855,6 +855,9 @@
> >  #
> >  # Get a list of `BlockInfo` for all virtual block devices.
> >  #
> > +# @flat: Omit the nested data about backing image ("backing-image"
> > +#     key) if true.  Default is false (Since 11.0)
> > +#
> 
> The "backing-image" key is actually BlockInfo member inserted member
> image member backing-image.  This is even more deeply nested than for
> query-named-block-nodes.  Doc text good enough?

Hmm, how about:

+# @flat: Omit the nested data about backing image (Omitted are contents of
+#        'backing-image' key of the 'ImageInfo' struct which is returned
+#        as 'image' key of 'BlockDeviceInfo' struct returned as 'inserted' key
+#        in  'BlockInfo' struct returned by this command).

or 

+# @flat: Omit the nested data about backing image (Omitted are contents
+#        of 'BlockInfo'->inserted->image->backing-image).

?
Re: [PATCH] block: Wire up 'flat' mode also for 'query-block'
Posted by Peter Krempa 1 month ago
On Thu, Dec 11, 2025 at 10:37:53 +0100, Peter Krempa wrote:
> From: Peter Krempa <pkrempa@redhat.com>
> 
> Some time ago (commit facda5443f5a8) I've added 'flat' mode (which
> omits 'backing-image' key in reply) to 'query-named-block-nodes' to
> minimize the size of the returned JSON for deeper backing chains.
> 
> While 'query-block' behaved slightly better it turns out that in libvirt
> we do call 'query-block' to figure out some information about the
> block device (e.g. throttling info) but we don't look at the backing
> chain itself.
> 
> Wire up 'flat' for 'query-block' so that libvirt can ask for an
> abbreviated output. The implementation is much simpler as the internals
> are shared with 'query-named-block-nodes'.
> 
> Signed-off-by: Peter Krempa <pkrempa@redhat.com>
> ---
>  block/monitor/block-hmp-cmds.c | 4 ++--
>  block/qapi.c                   | 9 +++++----
>  qapi/block-core.json           | 4 ++++
>  ui/cocoa.m                     | 2 +-
>  4 files changed, 12 insertions(+), 7 deletions(-)

Ping