thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
ed5979
From ae2c3df00d673d436fe4d8ec9103a3b76d7e6233 Mon Sep 17 00:00:00 2001
ed5979
From: Hanna Reitz <hreitz@redhat.com>
ed5979
Date: Mon, 20 Jun 2022 18:26:58 +0200
ed5979
Subject: [PATCH 11/20] block/qapi: Let bdrv_query_image_info() recurse
ed5979
ed5979
RH-Author: Hanna Czenczek <hreitz@redhat.com>
ed5979
RH-MergeRequest: 145: Show protocol-level information in qemu-img info
ed5979
RH-Bugzilla: 1860292
ed5979
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
ed5979
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ed5979
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
ed5979
RH-Commit: [6/12] 451a83fd682cd6dd6026c22974d18c2f12ee06e3 (hreitz/qemu-kvm-c-9-s)
ed5979
ed5979
There is no real reason why bdrv_query_image_info() should generally not
ed5979
recurse.  The ImageInfo struct has a pointer to the backing image, so it
ed5979
should generally be filled, unless the caller explicitly opts out.
ed5979
ed5979
This moves the recursing code from bdrv_block_device_info() into
ed5979
bdrv_query_image_info().
ed5979
ed5979
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
ed5979
Message-Id: <20220620162704.80987-7-hreitz@redhat.com>
ed5979
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
ed5979
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ed5979
(cherry picked from commit 5d8813593f3f673fc96eed199beb35690cc46f58)
ed5979
ed5979
Conflicts:
ed5979
    block/qapi.c: Conflicts with
ed5979
    54fde4ff0621c22b15cbaaa3c74301cc0dbd1c9e ("qapi block: Elide
ed5979
    redundant has_FOO in generated C"), which dropped
ed5979
    `has_backing_image`.  Without that commit (and 44ea9d9be before it),
ed5979
    we still need to set `has_backing_image` in
ed5979
    `bdrv_query_image_info()`.
ed5979
ed5979
Signed-off-by: Hanna Czenczek <hreitz@redhat.com>
ed5979
---
ed5979
 block/qapi.c         | 94 +++++++++++++++++++++++++++-----------------
ed5979
 include/block/qapi.h |  2 +
ed5979
 2 files changed, 59 insertions(+), 37 deletions(-)
ed5979
ed5979
diff --git a/block/qapi.c b/block/qapi.c
ed5979
index ad88bf9b38..5d0a8d2ce3 100644
ed5979
--- a/block/qapi.c
ed5979
+++ b/block/qapi.c
ed5979
@@ -47,8 +47,10 @@ BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
ed5979
                                         Error **errp)
ed5979
 {
ed5979
     ImageInfo **p_image_info;
ed5979
+    ImageInfo *backing_info;
ed5979
     BlockDriverState *bs0, *backing;
ed5979
     BlockDeviceInfo *info;
ed5979
+    ERRP_GUARD();
ed5979
 
ed5979
     if (!bs->drv) {
ed5979
         error_setg(errp, "Block device %s is ejected", bs->node_name);
ed5979
@@ -149,38 +151,21 @@ BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
ed5979
     bs0 = bs;
ed5979
     p_image_info = &info->image;
ed5979
     info->backing_file_depth = 0;
ed5979
-    while (1) {
ed5979
-        Error *local_err = NULL;
ed5979
-        bdrv_query_image_info(bs0, p_image_info, &local_err);
ed5979
-        if (local_err) {
ed5979
-            error_propagate(errp, local_err);
ed5979
-            qapi_free_BlockDeviceInfo(info);
ed5979
-            return NULL;
ed5979
-        }
ed5979
-
ed5979
-        /* stop gathering data for flat output */
ed5979
-        if (flat) {
ed5979
-            break;
ed5979
-        }
ed5979
 
ed5979
-        if (bs0->drv && bdrv_filter_or_cow_child(bs0)) {
ed5979
-            /*
ed5979
-             * Put any filtered child here (for backwards compatibility to when
ed5979
-             * we put bs0->backing here, which might be any filtered child).
ed5979
-             */
ed5979
-            info->backing_file_depth++;
ed5979
-            bs0 = bdrv_filter_or_cow_bs(bs0);
ed5979
-            (*p_image_info)->has_backing_image = true;
ed5979
-            p_image_info = &((*p_image_info)->backing_image);
ed5979
-        } else {
ed5979
-            break;
ed5979
-        }
ed5979
+    /*
ed5979
+     * Skip automatically inserted nodes that the user isn't aware of for
ed5979
+     * query-block (blk != NULL), but not for query-named-block-nodes
ed5979
+     */
ed5979
+    bdrv_query_image_info(bs0, p_image_info, flat, blk != NULL, errp);
ed5979
+    if (*errp) {
ed5979
+        qapi_free_BlockDeviceInfo(info);
ed5979
+        return NULL;
ed5979
+    }
ed5979
 
ed5979
-        /* Skip automatically inserted nodes that the user isn't aware of for
ed5979
-         * query-block (blk != NULL), but not for query-named-block-nodes */
ed5979
-        if (blk) {
ed5979
-            bs0 = bdrv_skip_implicit_filters(bs0);
ed5979
-        }
ed5979
+    backing_info = info->image->backing_image;
ed5979
+    while (backing_info) {
ed5979
+        info->backing_file_depth++;
ed5979
+        backing_info = backing_info->backing_image;
ed5979
     }
ed5979
 
ed5979
     return info;
ed5979
@@ -363,19 +348,28 @@ void bdrv_query_block_node_info(BlockDriverState *bs,
ed5979
  * bdrv_query_image_info:
ed5979
  * @bs: block node to examine
ed5979
  * @p_info: location to store image information
ed5979
+ * @flat: skip backing node information
ed5979
+ * @skip_implicit_filters: skip implicit filters in the backing chain
ed5979
  * @errp: location to store error information
ed5979
  *
ed5979
- * Store "flat" image information in @p_info.
ed5979
+ * Store image information in @p_info, potentially recursively covering the
ed5979
+ * backing chain.
ed5979
  *
ed5979
- * "Flat" means it does *not* query backing image information,
ed5979
- * i.e. (*pinfo)->has_backing_image will be set to false and
ed5979
- * (*pinfo)->backing_image to NULL even when the image does in fact have
ed5979
- * a backing image.
ed5979
+ * If @flat is true, do not query backing image information, i.e.
ed5979
+ * (*p_info)->has_backing_image will be set to false and
ed5979
+ * (*p_info)->backing_image to NULL even when the image does in fact have a
ed5979
+ * backing image.
ed5979
+ *
ed5979
+ * If @skip_implicit_filters is true, implicit filter nodes in the backing chain
ed5979
+ * will be skipped when querying backing image information.
ed5979
+ * (@skip_implicit_filters is ignored when @flat is true.)
ed5979
  *
ed5979
  * @p_info will be set only on success. On error, store error in @errp.
ed5979
  */
ed5979
 void bdrv_query_image_info(BlockDriverState *bs,
ed5979
                            ImageInfo **p_info,
ed5979
+                           bool flat,
ed5979
+                           bool skip_implicit_filters,
ed5979
                            Error **errp)
ed5979
 {
ed5979
     ImageInfo *info;
ed5979
@@ -384,11 +378,37 @@ void bdrv_query_image_info(BlockDriverState *bs,
ed5979
     info = g_new0(ImageInfo, 1);
ed5979
     bdrv_do_query_node_info(bs, qapi_ImageInfo_base(info), errp);
ed5979
     if (*errp) {
ed5979
-        qapi_free_ImageInfo(info);
ed5979
-        return;
ed5979
+        goto fail;
ed5979
+    }
ed5979
+
ed5979
+    if (!flat) {
ed5979
+        BlockDriverState *backing;
ed5979
+
ed5979
+        /*
ed5979
+         * Use any filtered child here (for backwards compatibility to when
ed5979
+         * we always took bs->backing, which might be any filtered child).
ed5979
+         */
ed5979
+        backing = bdrv_filter_or_cow_bs(bs);
ed5979
+        if (skip_implicit_filters) {
ed5979
+            backing = bdrv_skip_implicit_filters(backing);
ed5979
+        }
ed5979
+
ed5979
+        if (backing) {
ed5979
+            bdrv_query_image_info(backing, &info->backing_image, false,
ed5979
+                                  skip_implicit_filters, errp);
ed5979
+            if (*errp) {
ed5979
+                goto fail;
ed5979
+            }
ed5979
+            info->has_backing_image = true;
ed5979
+        }
ed5979
     }
ed5979
 
ed5979
     *p_info = info;
ed5979
+    return;
ed5979
+
ed5979
+fail:
ed5979
+    assert(*errp);
ed5979
+    qapi_free_ImageInfo(info);
ed5979
 }
ed5979
 
ed5979
 /* @p_info will be set only on success. */
ed5979
diff --git a/include/block/qapi.h b/include/block/qapi.h
ed5979
index 22198dcd0c..2174bf8fa2 100644
ed5979
--- a/include/block/qapi.h
ed5979
+++ b/include/block/qapi.h
ed5979
@@ -40,6 +40,8 @@ void bdrv_query_block_node_info(BlockDriverState *bs,
ed5979
                                 Error **errp);
ed5979
 void bdrv_query_image_info(BlockDriverState *bs,
ed5979
                            ImageInfo **p_info,
ed5979
+                           bool flat,
ed5979
+                           bool skip_implicit_filters,
ed5979
                            Error **errp);
ed5979
 
ed5979
 void bdrv_snapshot_dump(QEMUSnapshotInfo *sn);
ed5979
-- 
ed5979
2.31.1
ed5979