xref: /qemu/block/qapi.c (revision 54fde4ff)
1f364ec65SWenchao Xia /*
2f364ec65SWenchao Xia  * Block layer qmp and info dump related functions
3f364ec65SWenchao Xia  *
4f364ec65SWenchao Xia  * Copyright (c) 2003-2008 Fabrice Bellard
5f364ec65SWenchao Xia  *
6f364ec65SWenchao Xia  * Permission is hereby granted, free of charge, to any person obtaining a copy
7f364ec65SWenchao Xia  * of this software and associated documentation files (the "Software"), to deal
8f364ec65SWenchao Xia  * in the Software without restriction, including without limitation the rights
9f364ec65SWenchao Xia  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10f364ec65SWenchao Xia  * copies of the Software, and to permit persons to whom the Software is
11f364ec65SWenchao Xia  * furnished to do so, subject to the following conditions:
12f364ec65SWenchao Xia  *
13f364ec65SWenchao Xia  * The above copyright notice and this permission notice shall be included in
14f364ec65SWenchao Xia  * all copies or substantial portions of the Software.
15f364ec65SWenchao Xia  *
16f364ec65SWenchao Xia  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17f364ec65SWenchao Xia  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18f364ec65SWenchao Xia  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19f364ec65SWenchao Xia  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20f364ec65SWenchao Xia  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21f364ec65SWenchao Xia  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22f364ec65SWenchao Xia  * THE SOFTWARE.
23f364ec65SWenchao Xia  */
24f364ec65SWenchao Xia 
2580c71a24SPeter Maydell #include "qemu/osdep.h"
2631e40415SPhilippe Mathieu-Daudé #include "qemu/cutils.h"
27f364ec65SWenchao Xia #include "block/qapi.h"
28f364ec65SWenchao Xia #include "block/block_int.h"
2976f4afb4SAlberto Garcia #include "block/throttle-groups.h"
30e2462113SFrancesco Romani #include "block/write-threshold.h"
31e688df6bSMarkus Armbruster #include "qapi/error.h"
329af23989SMarkus Armbruster #include "qapi/qapi-commands-block-core.h"
33b3db211fSDaniel P. Berrange #include "qapi/qobject-output-visitor.h"
349af23989SMarkus Armbruster #include "qapi/qapi-visit-block-core.h"
356b673957SMarkus Armbruster #include "qapi/qmp/qbool.h"
36452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
3747e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h"
3815280c36SMarkus Armbruster #include "qapi/qmp/qnum.h"
396b673957SMarkus Armbruster #include "qapi/qmp/qstring.h"
40e1ce7d74SMarkus Armbruster #include "qemu/qemu-print.h"
41d829a211SMarkus Armbruster #include "sysemu/block-backend.h"
42f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
43f364ec65SWenchao Xia 
44c83f9fbaSKevin Wolf BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
45facda544SPeter Krempa                                         BlockDriverState *bs,
46facda544SPeter Krempa                                         bool flat,
47facda544SPeter Krempa                                         Error **errp)
48c13163fbSBenoît Canet {
49d5a8ee60SAlberto Garcia     ImageInfo **p_image_info;
500b877d09SMax Reitz     BlockDriverState *bs0, *backing;
51d470ad42SMax Reitz     BlockDeviceInfo *info;
52c13163fbSBenoît Canet 
53d470ad42SMax Reitz     if (!bs->drv) {
54d470ad42SMax Reitz         error_setg(errp, "Block device %s is ejected", bs->node_name);
55d470ad42SMax Reitz         return NULL;
56d470ad42SMax Reitz     }
57d470ad42SMax Reitz 
58f30c66baSMax Reitz     bdrv_refresh_filename(bs);
59f30c66baSMax Reitz 
60d470ad42SMax Reitz     info = g_malloc0(sizeof(*info));
61c13163fbSBenoît Canet     info->file                   = g_strdup(bs->filename);
62307261b2SVladimir Sementsov-Ogievskiy     info->ro                     = bdrv_is_read_only(bs);
63c13163fbSBenoît Canet     info->drv                    = g_strdup(bs->drv->format_name);
64c13163fbSBenoît Canet     info->encrypted              = bs->encrypted;
65c13163fbSBenoît Canet 
669e193c5aSKevin Wolf     info->cache = g_new(BlockdevCacheInfo, 1);
679e193c5aSKevin Wolf     *info->cache = (BlockdevCacheInfo) {
68c83f9fbaSKevin Wolf         .writeback      = blk ? blk_enable_write_cache(blk) : true,
699e193c5aSKevin Wolf         .direct         = !!(bs->open_flags & BDRV_O_NOCACHE),
709e193c5aSKevin Wolf         .no_flush       = !!(bs->open_flags & BDRV_O_NO_FLUSH),
719e193c5aSKevin Wolf     };
729e193c5aSKevin Wolf 
73c13163fbSBenoît Canet     if (bs->node_name[0]) {
74c13163fbSBenoît Canet         info->node_name = g_strdup(bs->node_name);
75c13163fbSBenoît Canet     }
76c13163fbSBenoît Canet 
770b877d09SMax Reitz     backing = bdrv_cow_bs(bs);
780b877d09SMax Reitz     if (backing) {
790b877d09SMax Reitz         info->backing_file = g_strdup(backing->filename);
80c13163fbSBenoît Canet     }
81c13163fbSBenoît Canet 
82590a63d5SVladimir Sementsov-Ogievskiy     if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
83590a63d5SVladimir Sementsov-Ogievskiy         info->has_dirty_bitmaps = true;
84590a63d5SVladimir Sementsov-Ogievskiy         info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
85590a63d5SVladimir Sementsov-Ogievskiy     }
86590a63d5SVladimir Sementsov-Ogievskiy 
87465bee1dSPeter Lieven     info->detect_zeroes = bs->detect_zeroes;
88c13163fbSBenoît Canet 
89022cdc9fSManos Pitsidianakis     if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
90c13163fbSBenoît Canet         ThrottleConfig cfg;
91022cdc9fSManos Pitsidianakis         BlockBackendPublic *blkp = blk_get_public(blk);
9276f4afb4SAlberto Garcia 
93022cdc9fSManos Pitsidianakis         throttle_group_get_config(&blkp->throttle_group_member, &cfg);
9476f4afb4SAlberto Garcia 
95c13163fbSBenoît Canet         info->bps     = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
96c13163fbSBenoît Canet         info->bps_rd  = cfg.buckets[THROTTLE_BPS_READ].avg;
97c13163fbSBenoît Canet         info->bps_wr  = cfg.buckets[THROTTLE_BPS_WRITE].avg;
98c13163fbSBenoît Canet 
99c13163fbSBenoît Canet         info->iops    = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
100c13163fbSBenoît Canet         info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
101c13163fbSBenoît Canet         info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
102c13163fbSBenoît Canet 
103c13163fbSBenoît Canet         info->has_bps_max     = cfg.buckets[THROTTLE_BPS_TOTAL].max;
104c13163fbSBenoît Canet         info->bps_max         = cfg.buckets[THROTTLE_BPS_TOTAL].max;
105c13163fbSBenoît Canet         info->has_bps_rd_max  = cfg.buckets[THROTTLE_BPS_READ].max;
106c13163fbSBenoît Canet         info->bps_rd_max      = cfg.buckets[THROTTLE_BPS_READ].max;
107c13163fbSBenoît Canet         info->has_bps_wr_max  = cfg.buckets[THROTTLE_BPS_WRITE].max;
108c13163fbSBenoît Canet         info->bps_wr_max      = cfg.buckets[THROTTLE_BPS_WRITE].max;
109c13163fbSBenoît Canet 
110c13163fbSBenoît Canet         info->has_iops_max    = cfg.buckets[THROTTLE_OPS_TOTAL].max;
111c13163fbSBenoît Canet         info->iops_max        = cfg.buckets[THROTTLE_OPS_TOTAL].max;
112c13163fbSBenoît Canet         info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
113c13163fbSBenoît Canet         info->iops_rd_max     = cfg.buckets[THROTTLE_OPS_READ].max;
114c13163fbSBenoît Canet         info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
115c13163fbSBenoît Canet         info->iops_wr_max     = cfg.buckets[THROTTLE_OPS_WRITE].max;
116c13163fbSBenoît Canet 
117398befdfSAlberto Garcia         info->has_bps_max_length     = info->has_bps_max;
118398befdfSAlberto Garcia         info->bps_max_length         =
119398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
120398befdfSAlberto Garcia         info->has_bps_rd_max_length  = info->has_bps_rd_max;
121398befdfSAlberto Garcia         info->bps_rd_max_length      =
122398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_READ].burst_length;
123398befdfSAlberto Garcia         info->has_bps_wr_max_length  = info->has_bps_wr_max;
124398befdfSAlberto Garcia         info->bps_wr_max_length      =
125398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
126398befdfSAlberto Garcia 
127398befdfSAlberto Garcia         info->has_iops_max_length    = info->has_iops_max;
128398befdfSAlberto Garcia         info->iops_max_length        =
129398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
130398befdfSAlberto Garcia         info->has_iops_rd_max_length = info->has_iops_rd_max;
131398befdfSAlberto Garcia         info->iops_rd_max_length     =
132398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_READ].burst_length;
133398befdfSAlberto Garcia         info->has_iops_wr_max_length = info->has_iops_wr_max;
134398befdfSAlberto Garcia         info->iops_wr_max_length     =
135398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
136398befdfSAlberto Garcia 
137c13163fbSBenoît Canet         info->has_iops_size = cfg.op_size;
138c13163fbSBenoît Canet         info->iops_size = cfg.op_size;
139b8fe1694SAlberto Garcia 
140022cdc9fSManos Pitsidianakis         info->group =
141022cdc9fSManos Pitsidianakis             g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
142c13163fbSBenoît Canet     }
143c13163fbSBenoît Canet 
144e2462113SFrancesco Romani     info->write_threshold = bdrv_write_threshold_get(bs);
145e2462113SFrancesco Romani 
146d5a8ee60SAlberto Garcia     bs0 = bs;
147d5a8ee60SAlberto Garcia     p_image_info = &info->image;
148d3c8c674SKevin Wolf     info->backing_file_depth = 0;
149d5a8ee60SAlberto Garcia     while (1) {
150d5a8ee60SAlberto Garcia         Error *local_err = NULL;
151d5a8ee60SAlberto Garcia         bdrv_query_image_info(bs0, p_image_info, &local_err);
152d5a8ee60SAlberto Garcia         if (local_err) {
153d5a8ee60SAlberto Garcia             error_propagate(errp, local_err);
154d5a8ee60SAlberto Garcia             qapi_free_BlockDeviceInfo(info);
155d5a8ee60SAlberto Garcia             return NULL;
156d5a8ee60SAlberto Garcia         }
157d3c8c674SKevin Wolf 
158facda544SPeter Krempa         /* stop gathering data for flat output */
159facda544SPeter Krempa         if (flat) {
160facda544SPeter Krempa             break;
161facda544SPeter Krempa         }
162facda544SPeter Krempa 
1630a7585dbSMax Reitz         if (bs0->drv && bdrv_filter_or_cow_child(bs0)) {
1640a7585dbSMax Reitz             /*
1650a7585dbSMax Reitz              * Put any filtered child here (for backwards compatibility to when
1660a7585dbSMax Reitz              * we put bs0->backing here, which might be any filtered child).
1670a7585dbSMax Reitz              */
168d3c8c674SKevin Wolf             info->backing_file_depth++;
1690a7585dbSMax Reitz             bs0 = bdrv_filter_or_cow_bs(bs0);
170d5a8ee60SAlberto Garcia             p_image_info = &((*p_image_info)->backing_image);
171d5a8ee60SAlberto Garcia         } else {
172d5a8ee60SAlberto Garcia             break;
173d5a8ee60SAlberto Garcia         }
174d3c8c674SKevin Wolf 
175d3c8c674SKevin Wolf         /* Skip automatically inserted nodes that the user isn't aware of for
176d3c8c674SKevin Wolf          * query-block (blk != NULL), but not for query-named-block-nodes */
1770a7585dbSMax Reitz         if (blk) {
1780a7585dbSMax Reitz             bs0 = bdrv_skip_implicit_filters(bs0);
179d3c8c674SKevin Wolf         }
180d5a8ee60SAlberto Garcia     }
181d5a8ee60SAlberto Garcia 
182c13163fbSBenoît Canet     return info;
183c13163fbSBenoît Canet }
184c13163fbSBenoît Canet 
185fb0ed453SWenchao Xia /*
186fb0ed453SWenchao Xia  * Returns 0 on success, with *p_list either set to describe snapshot
187fb0ed453SWenchao Xia  * information, or NULL because there are no snapshots.  Returns -errno on
188fb0ed453SWenchao Xia  * error, with *p_list untouched.
189fb0ed453SWenchao Xia  */
190fb0ed453SWenchao Xia int bdrv_query_snapshot_info_list(BlockDriverState *bs,
191fb0ed453SWenchao Xia                                   SnapshotInfoList **p_list,
192fb0ed453SWenchao Xia                                   Error **errp)
193f364ec65SWenchao Xia {
194f364ec65SWenchao Xia     int i, sn_count;
195f364ec65SWenchao Xia     QEMUSnapshotInfo *sn_tab = NULL;
19695b3a8c8SEric Blake     SnapshotInfoList *head = NULL, **tail = &head;
197fb0ed453SWenchao Xia     SnapshotInfo *info;
198fb0ed453SWenchao Xia 
199f364ec65SWenchao Xia     sn_count = bdrv_snapshot_list(bs, &sn_tab);
200fb0ed453SWenchao Xia     if (sn_count < 0) {
201fb0ed453SWenchao Xia         const char *dev = bdrv_get_device_name(bs);
202fb0ed453SWenchao Xia         switch (sn_count) {
203fb0ed453SWenchao Xia         case -ENOMEDIUM:
204fb0ed453SWenchao Xia             error_setg(errp, "Device '%s' is not inserted", dev);
205fb0ed453SWenchao Xia             break;
206fb0ed453SWenchao Xia         case -ENOTSUP:
207fb0ed453SWenchao Xia             error_setg(errp,
208fb0ed453SWenchao Xia                        "Device '%s' does not support internal snapshots",
209fb0ed453SWenchao Xia                        dev);
210fb0ed453SWenchao Xia             break;
211fb0ed453SWenchao Xia         default:
212fb0ed453SWenchao Xia             error_setg_errno(errp, -sn_count,
213fb0ed453SWenchao Xia                              "Can't list snapshots of device '%s'", dev);
214fb0ed453SWenchao Xia             break;
215fb0ed453SWenchao Xia         }
216fb0ed453SWenchao Xia         return sn_count;
217fb0ed453SWenchao Xia     }
218f364ec65SWenchao Xia 
219f364ec65SWenchao Xia     for (i = 0; i < sn_count; i++) {
220fb0ed453SWenchao Xia         info = g_new0(SnapshotInfo, 1);
221fb0ed453SWenchao Xia         info->id            = g_strdup(sn_tab[i].id_str);
222fb0ed453SWenchao Xia         info->name          = g_strdup(sn_tab[i].name);
223fb0ed453SWenchao Xia         info->vm_state_size = sn_tab[i].vm_state_size;
224fb0ed453SWenchao Xia         info->date_sec      = sn_tab[i].date_sec;
225fb0ed453SWenchao Xia         info->date_nsec     = sn_tab[i].date_nsec;
226fb0ed453SWenchao Xia         info->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
227fb0ed453SWenchao Xia         info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
228b39847a5SPavel Dovgalyuk         info->icount        = sn_tab[i].icount;
229b39847a5SPavel Dovgalyuk         info->has_icount    = sn_tab[i].icount != -1ULL;
230f364ec65SWenchao Xia 
23195b3a8c8SEric Blake         QAPI_LIST_APPEND(tail, info);
232f364ec65SWenchao Xia     }
233f364ec65SWenchao Xia 
234f364ec65SWenchao Xia     g_free(sn_tab);
235fb0ed453SWenchao Xia     *p_list = head;
236fb0ed453SWenchao Xia     return 0;
237f364ec65SWenchao Xia }
238f364ec65SWenchao Xia 
23943526ec8SWenchao Xia /**
24043526ec8SWenchao Xia  * bdrv_query_image_info:
24143526ec8SWenchao Xia  * @bs: block device to examine
24243526ec8SWenchao Xia  * @p_info: location to store image information
24343526ec8SWenchao Xia  * @errp: location to store error information
24443526ec8SWenchao Xia  *
245553a7e87SWenchao Xia  * Store "flat" image information in @p_info.
246553a7e87SWenchao Xia  *
247553a7e87SWenchao Xia  * "Flat" means it does *not* query backing image information,
248553a7e87SWenchao Xia  * i.e. (*pinfo)->has_backing_image will be set to false and
249553a7e87SWenchao Xia  * (*pinfo)->backing_image to NULL even when the image does in fact have
250553a7e87SWenchao Xia  * a backing image.
251553a7e87SWenchao Xia  *
25243526ec8SWenchao Xia  * @p_info will be set only on success. On error, store error in @errp.
25343526ec8SWenchao Xia  */
25443526ec8SWenchao Xia void bdrv_query_image_info(BlockDriverState *bs,
25543526ec8SWenchao Xia                            ImageInfo **p_info,
25643526ec8SWenchao Xia                            Error **errp)
257f364ec65SWenchao Xia {
25852bf1e72SMarkus Armbruster     int64_t size;
25943526ec8SWenchao Xia     const char *backing_filename;
260f364ec65SWenchao Xia     BlockDriverInfo bdi;
26143526ec8SWenchao Xia     int ret;
26243526ec8SWenchao Xia     Error *err = NULL;
26352bf1e72SMarkus Armbruster     ImageInfo *info;
264f364ec65SWenchao Xia 
2651963f8d5SPaolo Bonzini     aio_context_acquire(bdrv_get_aio_context(bs));
2661963f8d5SPaolo Bonzini 
26752bf1e72SMarkus Armbruster     size = bdrv_getlength(bs);
26852bf1e72SMarkus Armbruster     if (size < 0) {
2699adceb02SFam Zheng         error_setg_errno(errp, -size, "Can't get image size '%s'",
2709adceb02SFam Zheng                          bs->exact_filename);
2711963f8d5SPaolo Bonzini         goto out;
27252bf1e72SMarkus Armbruster     }
273f364ec65SWenchao Xia 
274f30c66baSMax Reitz     bdrv_refresh_filename(bs);
275f30c66baSMax Reitz 
27652bf1e72SMarkus Armbruster     info = g_new0(ImageInfo, 1);
27743526ec8SWenchao Xia     info->filename        = g_strdup(bs->filename);
278f364ec65SWenchao Xia     info->format          = g_strdup(bdrv_get_format_name(bs));
27952bf1e72SMarkus Armbruster     info->virtual_size    = size;
280f364ec65SWenchao Xia     info->actual_size     = bdrv_get_allocated_file_size(bs);
281f364ec65SWenchao Xia     info->has_actual_size = info->actual_size >= 0;
2828b8277cdSMax Reitz     if (bs->encrypted) {
283f364ec65SWenchao Xia         info->encrypted = true;
284f364ec65SWenchao Xia         info->has_encrypted = true;
285f364ec65SWenchao Xia     }
286f364ec65SWenchao Xia     if (bdrv_get_info(bs, &bdi) >= 0) {
287f364ec65SWenchao Xia         if (bdi.cluster_size != 0) {
288f364ec65SWenchao Xia             info->cluster_size = bdi.cluster_size;
289f364ec65SWenchao Xia             info->has_cluster_size = true;
290f364ec65SWenchao Xia         }
291f364ec65SWenchao Xia         info->dirty_flag = bdi.is_dirty;
292f364ec65SWenchao Xia         info->has_dirty_flag = true;
293f364ec65SWenchao Xia     }
2941bf6e9caSAndrey Shinkevich     info->format_specific = bdrv_get_specific_info(bs, &err);
2951bf6e9caSAndrey Shinkevich     if (err) {
2961bf6e9caSAndrey Shinkevich         error_propagate(errp, err);
2971bf6e9caSAndrey Shinkevich         qapi_free_ImageInfo(info);
2981bf6e9caSAndrey Shinkevich         goto out;
2991bf6e9caSAndrey Shinkevich     }
30043526ec8SWenchao Xia     backing_filename = bs->backing_file;
301f364ec65SWenchao Xia     if (backing_filename[0] != '\0') {
3026b6833c1SMax Reitz         char *backing_filename2;
3030b877d09SMax Reitz 
304f364ec65SWenchao Xia         info->backing_filename = g_strdup(backing_filename);
3056b6833c1SMax Reitz         backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
306f364ec65SWenchao Xia 
30712dcb1c0SJohn Snow         /* Always report the full_backing_filename if present, even if it's the
30812dcb1c0SJohn Snow          * same as backing_filename. That they are same is useful info. */
30912dcb1c0SJohn Snow         if (backing_filename2) {
31012dcb1c0SJohn Snow             info->full_backing_filename = g_strdup(backing_filename2);
311f364ec65SWenchao Xia         }
312f364ec65SWenchao Xia 
313f364ec65SWenchao Xia         if (bs->backing_format[0]) {
314f364ec65SWenchao Xia             info->backing_filename_format = g_strdup(bs->backing_format);
315f364ec65SWenchao Xia         }
316564d64bdSJeff Cody         g_free(backing_filename2);
317f364ec65SWenchao Xia     }
31843526ec8SWenchao Xia 
31943526ec8SWenchao Xia     ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
32043526ec8SWenchao Xia     switch (ret) {
32143526ec8SWenchao Xia     case 0:
32243526ec8SWenchao Xia         if (info->snapshots) {
32343526ec8SWenchao Xia             info->has_snapshots = true;
32443526ec8SWenchao Xia         }
32543526ec8SWenchao Xia         break;
32643526ec8SWenchao Xia     /* recoverable error */
32743526ec8SWenchao Xia     case -ENOMEDIUM:
32843526ec8SWenchao Xia     case -ENOTSUP:
32943526ec8SWenchao Xia         error_free(err);
33043526ec8SWenchao Xia         break;
33143526ec8SWenchao Xia     default:
33243526ec8SWenchao Xia         error_propagate(errp, err);
33343526ec8SWenchao Xia         qapi_free_ImageInfo(info);
3341963f8d5SPaolo Bonzini         goto out;
33543526ec8SWenchao Xia     }
33643526ec8SWenchao Xia 
33743526ec8SWenchao Xia     *p_info = info;
3381963f8d5SPaolo Bonzini 
3391963f8d5SPaolo Bonzini out:
3401963f8d5SPaolo Bonzini     aio_context_release(bdrv_get_aio_context(bs));
341f364ec65SWenchao Xia }
342f364ec65SWenchao Xia 
343553a7e87SWenchao Xia /* @p_info will be set only on success. */
344d829a211SMarkus Armbruster static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
345553a7e87SWenchao Xia                             Error **errp)
346f364ec65SWenchao Xia {
347f364ec65SWenchao Xia     BlockInfo *info = g_malloc0(sizeof(*info));
348d829a211SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
34946eade7bSKevin Wolf     char *qdev;
35046eade7bSKevin Wolf 
351d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of */
3520a7585dbSMax Reitz     bs = bdrv_skip_implicit_filters(bs);
353d3c8c674SKevin Wolf 
354d829a211SMarkus Armbruster     info->device = g_strdup(blk_name(blk));
355f364ec65SWenchao Xia     info->type = g_strdup("unknown");
356a7f53e26SMarkus Armbruster     info->locked = blk_dev_is_medium_locked(blk);
357a7f53e26SMarkus Armbruster     info->removable = blk_dev_has_removable_media(blk);
358f364ec65SWenchao Xia 
35946eade7bSKevin Wolf     qdev = blk_get_attached_dev_id(blk);
36046eade7bSKevin Wolf     if (qdev && *qdev) {
36146eade7bSKevin Wolf         info->qdev = qdev;
36246eade7bSKevin Wolf     } else {
36346eade7bSKevin Wolf         g_free(qdev);
36446eade7bSKevin Wolf     }
36546eade7bSKevin Wolf 
366327032ceSMax Reitz     if (blk_dev_has_tray(blk)) {
367f364ec65SWenchao Xia         info->has_tray_open = true;
368a7f53e26SMarkus Armbruster         info->tray_open = blk_dev_is_tray_open(blk);
369f364ec65SWenchao Xia     }
370f364ec65SWenchao Xia 
371373340b2SMax Reitz     if (blk_iostatus_is_enabled(blk)) {
372f364ec65SWenchao Xia         info->has_io_status = true;
373373340b2SMax Reitz         info->io_status = blk_iostatus(blk);
374f364ec65SWenchao Xia     }
375f364ec65SWenchao Xia 
3765433c24fSMax Reitz     if (bs && bs->drv) {
377facda544SPeter Krempa         info->inserted = bdrv_block_device_info(blk, bs, false, errp);
378d5a8ee60SAlberto Garcia         if (info->inserted == NULL) {
379553a7e87SWenchao Xia             goto err;
380f364ec65SWenchao Xia         }
381553a7e87SWenchao Xia     }
382553a7e87SWenchao Xia 
383553a7e87SWenchao Xia     *p_info = info;
384553a7e87SWenchao Xia     return;
385553a7e87SWenchao Xia 
386553a7e87SWenchao Xia  err:
387553a7e87SWenchao Xia     qapi_free_BlockInfo(info);
388f364ec65SWenchao Xia }
389f364ec65SWenchao Xia 
3907e5c776dSVladimir Sementsov-Ogievskiy static uint64List *uint64_list(uint64_t *list, int size)
3917e5c776dSVladimir Sementsov-Ogievskiy {
3927e5c776dSVladimir Sementsov-Ogievskiy     int i;
3937e5c776dSVladimir Sementsov-Ogievskiy     uint64List *out_list = NULL;
394c3033fd3SEric Blake     uint64List **tail = &out_list;
3957e5c776dSVladimir Sementsov-Ogievskiy 
3967e5c776dSVladimir Sementsov-Ogievskiy     for (i = 0; i < size; i++) {
397c3033fd3SEric Blake         QAPI_LIST_APPEND(tail, list[i]);
3987e5c776dSVladimir Sementsov-Ogievskiy     }
3997e5c776dSVladimir Sementsov-Ogievskiy 
4007e5c776dSVladimir Sementsov-Ogievskiy     return out_list;
4017e5c776dSVladimir Sementsov-Ogievskiy }
4027e5c776dSVladimir Sementsov-Ogievskiy 
403*54fde4ffSMarkus Armbruster static BlockLatencyHistogramInfo *
404*54fde4ffSMarkus Armbruster bdrv_latency_histogram_stats(BlockLatencyHistogram *hist)
4057e5c776dSVladimir Sementsov-Ogievskiy {
406*54fde4ffSMarkus Armbruster     BlockLatencyHistogramInfo *info;
4077e5c776dSVladimir Sementsov-Ogievskiy 
408*54fde4ffSMarkus Armbruster     if (!hist->bins) {
409*54fde4ffSMarkus Armbruster         return NULL;
4107e5c776dSVladimir Sementsov-Ogievskiy     }
411*54fde4ffSMarkus Armbruster 
412*54fde4ffSMarkus Armbruster     info = g_new0(BlockLatencyHistogramInfo, 1);
413*54fde4ffSMarkus Armbruster     info->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
414*54fde4ffSMarkus Armbruster     info->bins = uint64_list(hist->bins, hist->nbins);
415*54fde4ffSMarkus Armbruster     return info;
4167e5c776dSVladimir Sementsov-Ogievskiy }
4177e5c776dSVladimir Sementsov-Ogievskiy 
41854302156SMax Reitz static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
419f364ec65SWenchao Xia {
4202b77e60aSKevin Wolf     BlockAcctStats *stats = blk_get_stats(blk);
421979e9b03SAlberto Garcia     BlockAcctTimedStats *ts = NULL;
422*54fde4ffSMarkus Armbruster     BlockLatencyHistogram *hgram;
4237f0e9da6SMax Reitz 
42454302156SMax Reitz     ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
42554302156SMax Reitz     ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
426159f85ddSAnton Nefedov     ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
42754302156SMax Reitz     ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
42854302156SMax Reitz     ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
429159f85ddSAnton Nefedov     ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
4307ee12dafSAlberto Garcia 
43154302156SMax Reitz     ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
43254302156SMax Reitz     ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
43354302156SMax Reitz     ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
434159f85ddSAnton Nefedov     ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
4357ee12dafSAlberto Garcia 
43654302156SMax Reitz     ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
43754302156SMax Reitz     ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
43854302156SMax Reitz     ds->invalid_flush_operations =
4397ee12dafSAlberto Garcia         stats->invalid_ops[BLOCK_ACCT_FLUSH];
440159f85ddSAnton Nefedov     ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
4417ee12dafSAlberto Garcia 
44254302156SMax Reitz     ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
44354302156SMax Reitz     ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
444159f85ddSAnton Nefedov     ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
44554302156SMax Reitz     ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
44654302156SMax Reitz     ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
44754302156SMax Reitz     ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
44854302156SMax Reitz     ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
449159f85ddSAnton Nefedov     ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
450cb38fffbSAlberto Garcia 
45154302156SMax Reitz     ds->has_idle_time_ns = stats->last_access_time_ns > 0;
45254302156SMax Reitz     if (ds->has_idle_time_ns) {
45354302156SMax Reitz         ds->idle_time_ns = block_acct_idle_time_ns(stats);
454cb38fffbSAlberto Garcia     }
455362e9299SAlberto Garcia 
45654302156SMax Reitz     ds->account_invalid = stats->account_invalid;
45754302156SMax Reitz     ds->account_failed = stats->account_failed;
458979e9b03SAlberto Garcia 
459979e9b03SAlberto Garcia     while ((ts = block_acct_interval_next(stats, ts))) {
460979e9b03SAlberto Garcia         BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
461979e9b03SAlberto Garcia 
462979e9b03SAlberto Garcia         TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
463979e9b03SAlberto Garcia         TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
464979e9b03SAlberto Garcia         TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
465979e9b03SAlberto Garcia 
466979e9b03SAlberto Garcia         dev_stats->interval_length = ts->interval_length;
467979e9b03SAlberto Garcia 
468979e9b03SAlberto Garcia         dev_stats->min_rd_latency_ns = timed_average_min(rd);
469979e9b03SAlberto Garcia         dev_stats->max_rd_latency_ns = timed_average_max(rd);
470979e9b03SAlberto Garcia         dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
471979e9b03SAlberto Garcia 
472979e9b03SAlberto Garcia         dev_stats->min_wr_latency_ns = timed_average_min(wr);
473979e9b03SAlberto Garcia         dev_stats->max_wr_latency_ns = timed_average_max(wr);
474979e9b03SAlberto Garcia         dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
475979e9b03SAlberto Garcia 
476979e9b03SAlberto Garcia         dev_stats->min_flush_latency_ns = timed_average_min(fl);
477979e9b03SAlberto Garcia         dev_stats->max_flush_latency_ns = timed_average_max(fl);
478979e9b03SAlberto Garcia         dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
47996e4dedaSAlberto Garcia 
48096e4dedaSAlberto Garcia         dev_stats->avg_rd_queue_depth =
48196e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_READ);
48296e4dedaSAlberto Garcia         dev_stats->avg_wr_queue_depth =
48396e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
48454aa3de7SEric Blake 
48554aa3de7SEric Blake         QAPI_LIST_PREPEND(ds->timed_stats, dev_stats);
486979e9b03SAlberto Garcia     }
4877e5c776dSVladimir Sementsov-Ogievskiy 
488*54fde4ffSMarkus Armbruster     hgram = stats->latency_histogram;
489*54fde4ffSMarkus Armbruster     ds->rd_latency_histogram
490*54fde4ffSMarkus Armbruster         = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_READ]);
491*54fde4ffSMarkus Armbruster     ds->wr_latency_histogram
492*54fde4ffSMarkus Armbruster         = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_WRITE]);
493*54fde4ffSMarkus Armbruster     ds->flush_latency_histogram
494*54fde4ffSMarkus Armbruster         = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_FLUSH]);
4957f0e9da6SMax Reitz }
496f364ec65SWenchao Xia 
497d3c8c674SKevin Wolf static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
498d3c8c674SKevin Wolf                                         bool blk_level)
4992b77e60aSKevin Wolf {
5003f26191cSMax Reitz     BdrvChild *parent_child;
5010a7585dbSMax Reitz     BlockDriverState *filter_or_cow_bs;
50220a6d768SDou Liyang     BlockStats *s = NULL;
50320a6d768SDou Liyang 
50420a6d768SDou Liyang     s = g_malloc0(sizeof(*s));
50520a6d768SDou Liyang     s->stats = g_malloc0(sizeof(*s->stats));
50620a6d768SDou Liyang 
50720a6d768SDou Liyang     if (!bs) {
50820a6d768SDou Liyang         return s;
50920a6d768SDou Liyang     }
51020a6d768SDou Liyang 
511d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of in
512d3c8c674SKevin Wolf      * a BlockBackend-level command. Stay at the exact node for a node-level
513d3c8c674SKevin Wolf      * command. */
5140a7585dbSMax Reitz     if (blk_level) {
5150a7585dbSMax Reitz         bs = bdrv_skip_implicit_filters(bs);
516d3c8c674SKevin Wolf     }
517d3c8c674SKevin Wolf 
5182b77e60aSKevin Wolf     if (bdrv_get_node_name(bs)[0]) {
5192b77e60aSKevin Wolf         s->node_name = g_strdup(bdrv_get_node_name(bs));
5202b77e60aSKevin Wolf     }
5212b77e60aSKevin Wolf 
522f7946da2SPaolo Bonzini     s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
52353d8f9d8SMax Reitz 
524d9245599SAnton Nefedov     s->driver_specific = bdrv_get_specific_stats(bs);
525d9245599SAnton Nefedov 
5263f26191cSMax Reitz     parent_child = bdrv_primary_child(bs);
5273f26191cSMax Reitz     if (!parent_child ||
5283f26191cSMax Reitz         !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED)))
5293f26191cSMax Reitz     {
5303f26191cSMax Reitz         BdrvChild *c;
5313f26191cSMax Reitz 
5323f26191cSMax Reitz         /*
5333f26191cSMax Reitz          * Look for a unique data-storing child.  We do not need to look for
5343f26191cSMax Reitz          * filtered children, as there would be only one and it would have been
5353f26191cSMax Reitz          * the primary child.
5363f26191cSMax Reitz          */
5373f26191cSMax Reitz         parent_child = NULL;
5383f26191cSMax Reitz         QLIST_FOREACH(c, &bs->children, next) {
5393f26191cSMax Reitz             if (c->role & BDRV_CHILD_DATA) {
5403f26191cSMax Reitz                 if (parent_child) {
5413f26191cSMax Reitz                     /*
5423f26191cSMax Reitz                      * There are multiple data-storing children and we cannot
5433f26191cSMax Reitz                      * choose between them.
5443f26191cSMax Reitz                      */
5453f26191cSMax Reitz                     parent_child = NULL;
5463f26191cSMax Reitz                     break;
5473f26191cSMax Reitz                 }
5483f26191cSMax Reitz                 parent_child = c;
5493f26191cSMax Reitz             }
5503f26191cSMax Reitz         }
5513f26191cSMax Reitz     }
5523f26191cSMax Reitz     if (parent_child) {
5533f26191cSMax Reitz         s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level);
554f364ec65SWenchao Xia     }
555f364ec65SWenchao Xia 
5560a7585dbSMax Reitz     filter_or_cow_bs = bdrv_filter_or_cow_bs(bs);
5570a7585dbSMax Reitz     if (blk_level && filter_or_cow_bs) {
5580a7585dbSMax Reitz         /*
5590a7585dbSMax Reitz          * Put any filtered or COW child here (for backwards
5600a7585dbSMax Reitz          * compatibility to when we put bs0->backing here, which might
5610a7585dbSMax Reitz          * be either)
5620a7585dbSMax Reitz          */
5630a7585dbSMax Reitz         s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level);
564c8059b97SFam Zheng     }
565c8059b97SFam Zheng 
56620a6d768SDou Liyang     return s;
567b07363a1SKevin Wolf }
568b07363a1SKevin Wolf 
569f364ec65SWenchao Xia BlockInfoList *qmp_query_block(Error **errp)
570f364ec65SWenchao Xia {
571f364ec65SWenchao Xia     BlockInfoList *head = NULL, **p_next = &head;
572d829a211SMarkus Armbruster     BlockBackend *blk;
573553a7e87SWenchao Xia     Error *local_err = NULL;
574f364ec65SWenchao Xia 
575d5b68844SKevin Wolf     for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
576d5b68844SKevin Wolf         BlockInfoList *info;
577d5b68844SKevin Wolf 
578ec18b0a9SKevin Wolf         if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
579d5b68844SKevin Wolf             continue;
580d5b68844SKevin Wolf         }
581d5b68844SKevin Wolf 
582d5b68844SKevin Wolf         info = g_malloc0(sizeof(*info));
583d829a211SMarkus Armbruster         bdrv_query_info(blk, &info->value, &local_err);
58484d18f06SMarkus Armbruster         if (local_err) {
585553a7e87SWenchao Xia             error_propagate(errp, local_err);
586903c341dSMarkus Armbruster             g_free(info);
587903c341dSMarkus Armbruster             qapi_free_BlockInfoList(head);
588903c341dSMarkus Armbruster             return NULL;
589553a7e87SWenchao Xia         }
590f364ec65SWenchao Xia 
591f364ec65SWenchao Xia         *p_next = info;
592f364ec65SWenchao Xia         p_next = &info->next;
593f364ec65SWenchao Xia     }
594f364ec65SWenchao Xia 
595f364ec65SWenchao Xia     return head;
596f364ec65SWenchao Xia }
597f364ec65SWenchao Xia 
598f71eaa74SFam Zheng BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
599f71eaa74SFam Zheng                                      bool query_nodes,
600f71eaa74SFam Zheng                                      Error **errp)
601f364ec65SWenchao Xia {
602c3033fd3SEric Blake     BlockStatsList *head = NULL, **tail = &head;
603a6baa608SDou Liyang     BlockBackend *blk;
604a6baa608SDou Liyang     BlockDriverState *bs;
605f364ec65SWenchao Xia 
606f71eaa74SFam Zheng     /* Just to be safe if query_nodes is not always initialized */
607a6baa608SDou Liyang     if (has_query_nodes && query_nodes) {
608a6baa608SDou Liyang         for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
609a6baa608SDou Liyang             AioContext *ctx = bdrv_get_aio_context(bs);
61013344f3aSStefan Hajnoczi 
61113344f3aSStefan Hajnoczi             aio_context_acquire(ctx);
612c3033fd3SEric Blake             QAPI_LIST_APPEND(tail, bdrv_query_bds_stats(bs, false));
61313344f3aSStefan Hajnoczi             aio_context_release(ctx);
614f364ec65SWenchao Xia         }
615a6baa608SDou Liyang     } else {
616567dcb31SKevin Wolf         for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
617a6baa608SDou Liyang             AioContext *ctx = blk_get_aio_context(blk);
618a6baa608SDou Liyang             BlockStats *s;
6195a9cb5a9SKevin Wolf             char *qdev;
620a6baa608SDou Liyang 
621567dcb31SKevin Wolf             if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
622567dcb31SKevin Wolf                 continue;
623567dcb31SKevin Wolf             }
624567dcb31SKevin Wolf 
625a6baa608SDou Liyang             aio_context_acquire(ctx);
626a6baa608SDou Liyang             s = bdrv_query_bds_stats(blk_bs(blk), true);
627a6baa608SDou Liyang             s->device = g_strdup(blk_name(blk));
6285a9cb5a9SKevin Wolf 
6295a9cb5a9SKevin Wolf             qdev = blk_get_attached_dev_id(blk);
6305a9cb5a9SKevin Wolf             if (qdev && *qdev) {
6315a9cb5a9SKevin Wolf                 s->qdev = qdev;
6325a9cb5a9SKevin Wolf             } else {
6335a9cb5a9SKevin Wolf                 g_free(qdev);
6345a9cb5a9SKevin Wolf             }
6355a9cb5a9SKevin Wolf 
636a6baa608SDou Liyang             bdrv_query_blk_stats(s->stats, blk);
637a6baa608SDou Liyang             aio_context_release(ctx);
638a6baa608SDou Liyang 
639c3033fd3SEric Blake             QAPI_LIST_APPEND(tail, s);
640a6baa608SDou Liyang         }
641a6baa608SDou Liyang     }
642f364ec65SWenchao Xia 
643f364ec65SWenchao Xia     return head;
644f364ec65SWenchao Xia }
645f364ec65SWenchao Xia 
646e1ce7d74SMarkus Armbruster void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
647f364ec65SWenchao Xia {
64839683553SDaniel P. Berrangé     char clock_buf[128];
649b39847a5SPavel Dovgalyuk     char icount_buf[128] = {0};
650f364ec65SWenchao Xia     int64_t secs;
651de38b500SEric Blake     char *sizing = NULL;
652f364ec65SWenchao Xia 
653f364ec65SWenchao Xia     if (!sn) {
65426513a01SKevin Wolf         qemu_printf("%-10s%-17s%8s%20s%13s%11s",
655b39847a5SPavel Dovgalyuk                     "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT");
656f364ec65SWenchao Xia     } else {
65739683553SDaniel P. Berrangé         g_autoptr(GDateTime) date = g_date_time_new_from_unix_local(sn->date_sec);
65839683553SDaniel P. Berrangé         g_autofree char *date_buf = g_date_time_format(date, "%Y-%m-%d %H:%M:%S");
65939683553SDaniel P. Berrangé 
660f364ec65SWenchao Xia         secs = sn->vm_clock_nsec / 1000000000;
661f364ec65SWenchao Xia         snprintf(clock_buf, sizeof(clock_buf),
662f364ec65SWenchao Xia                  "%02d:%02d:%02d.%03d",
663f364ec65SWenchao Xia                  (int)(secs / 3600),
664f364ec65SWenchao Xia                  (int)((secs / 60) % 60),
665f364ec65SWenchao Xia                  (int)(secs % 60),
666f364ec65SWenchao Xia                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
667de38b500SEric Blake         sizing = size_to_str(sn->vm_state_size);
668b39847a5SPavel Dovgalyuk         if (sn->icount != -1ULL) {
669b39847a5SPavel Dovgalyuk             snprintf(icount_buf, sizeof(icount_buf),
670b39847a5SPavel Dovgalyuk                 "%"PRId64, sn->icount);
671b39847a5SPavel Dovgalyuk         }
67226513a01SKevin Wolf         qemu_printf("%-9s %-16s %8s%20s%13s%11s",
673f364ec65SWenchao Xia                     sn->id_str, sn->name,
674de38b500SEric Blake                     sizing,
675f364ec65SWenchao Xia                     date_buf,
676b39847a5SPavel Dovgalyuk                     clock_buf,
677b39847a5SPavel Dovgalyuk                     icount_buf);
678f364ec65SWenchao Xia     }
679de38b500SEric Blake     g_free(sizing);
680f364ec65SWenchao Xia }
681f364ec65SWenchao Xia 
682e1ce7d74SMarkus Armbruster static void dump_qdict(int indentation, QDict *dict);
683e1ce7d74SMarkus Armbruster static void dump_qlist(int indentation, QList *list);
684a8d8ecb7SMax Reitz 
685e1ce7d74SMarkus Armbruster static void dump_qobject(int comp_indent, QObject *obj)
686a8d8ecb7SMax Reitz {
687a8d8ecb7SMax Reitz     switch (qobject_type(obj)) {
68801b2ffceSMarc-André Lureau         case QTYPE_QNUM: {
6897dc847ebSMax Reitz             QNum *value = qobject_to(QNum, obj);
69001b2ffceSMarc-André Lureau             char *tmp = qnum_to_string(value);
691e1ce7d74SMarkus Armbruster             qemu_printf("%s", tmp);
69201b2ffceSMarc-André Lureau             g_free(tmp);
693a8d8ecb7SMax Reitz             break;
694a8d8ecb7SMax Reitz         }
695a8d8ecb7SMax Reitz         case QTYPE_QSTRING: {
6967dc847ebSMax Reitz             QString *value = qobject_to(QString, obj);
697e1ce7d74SMarkus Armbruster             qemu_printf("%s", qstring_get_str(value));
698a8d8ecb7SMax Reitz             break;
699a8d8ecb7SMax Reitz         }
700a8d8ecb7SMax Reitz         case QTYPE_QDICT: {
7017dc847ebSMax Reitz             QDict *value = qobject_to(QDict, obj);
702e1ce7d74SMarkus Armbruster             dump_qdict(comp_indent, value);
703a8d8ecb7SMax Reitz             break;
704a8d8ecb7SMax Reitz         }
705a8d8ecb7SMax Reitz         case QTYPE_QLIST: {
7067dc847ebSMax Reitz             QList *value = qobject_to(QList, obj);
707e1ce7d74SMarkus Armbruster             dump_qlist(comp_indent, value);
708a8d8ecb7SMax Reitz             break;
709a8d8ecb7SMax Reitz         }
710a8d8ecb7SMax Reitz         case QTYPE_QBOOL: {
7117dc847ebSMax Reitz             QBool *value = qobject_to(QBool, obj);
712e1ce7d74SMarkus Armbruster             qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
713a8d8ecb7SMax Reitz             break;
714a8d8ecb7SMax Reitz         }
715a8d8ecb7SMax Reitz         default:
716a8d8ecb7SMax Reitz             abort();
717a8d8ecb7SMax Reitz     }
718a8d8ecb7SMax Reitz }
719a8d8ecb7SMax Reitz 
720e1ce7d74SMarkus Armbruster static void dump_qlist(int indentation, QList *list)
721a8d8ecb7SMax Reitz {
722a8d8ecb7SMax Reitz     const QListEntry *entry;
723a8d8ecb7SMax Reitz     int i = 0;
724a8d8ecb7SMax Reitz 
725a8d8ecb7SMax Reitz     for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
7261310a3d3SEric Blake         QType type = qobject_type(entry->value);
727a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
728e1ce7d74SMarkus Armbruster         qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
729853ccfedSPeter Xu                     composite ? '\n' : ' ');
730e1ce7d74SMarkus Armbruster         dump_qobject(indentation + 1, entry->value);
731a8d8ecb7SMax Reitz         if (!composite) {
732e1ce7d74SMarkus Armbruster             qemu_printf("\n");
733a8d8ecb7SMax Reitz         }
734a8d8ecb7SMax Reitz     }
735a8d8ecb7SMax Reitz }
736a8d8ecb7SMax Reitz 
737e1ce7d74SMarkus Armbruster static void dump_qdict(int indentation, QDict *dict)
738a8d8ecb7SMax Reitz {
739a8d8ecb7SMax Reitz     const QDictEntry *entry;
740a8d8ecb7SMax Reitz 
741a8d8ecb7SMax Reitz     for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
7421310a3d3SEric Blake         QType type = qobject_type(entry->value);
743a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
7445eda6227SPeter Xu         char *key = g_malloc(strlen(entry->key) + 1);
745a8d8ecb7SMax Reitz         int i;
746a8d8ecb7SMax Reitz 
747a8d8ecb7SMax Reitz         /* replace dashes with spaces in key (variable) names */
748a8d8ecb7SMax Reitz         for (i = 0; entry->key[i]; i++) {
749a8d8ecb7SMax Reitz             key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
750a8d8ecb7SMax Reitz         }
751a8d8ecb7SMax Reitz         key[i] = 0;
752e1ce7d74SMarkus Armbruster         qemu_printf("%*s%s:%c", indentation * 4, "", key,
753853ccfedSPeter Xu                     composite ? '\n' : ' ');
754e1ce7d74SMarkus Armbruster         dump_qobject(indentation + 1, entry->value);
755a8d8ecb7SMax Reitz         if (!composite) {
756e1ce7d74SMarkus Armbruster             qemu_printf("\n");
757a8d8ecb7SMax Reitz         }
7585eda6227SPeter Xu         g_free(key);
759a8d8ecb7SMax Reitz     }
760a8d8ecb7SMax Reitz }
761a8d8ecb7SMax Reitz 
762e1ce7d74SMarkus Armbruster void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec)
763a8d8ecb7SMax Reitz {
764a8d8ecb7SMax Reitz     QObject *obj, *data;
7657d5e199aSDaniel P. Berrange     Visitor *v = qobject_output_visitor_new(&obj);
766a8d8ecb7SMax Reitz 
7673b098d56SEric Blake     visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
7683b098d56SEric Blake     visit_complete(v, &obj);
7697dc847ebSMax Reitz     data = qdict_get(qobject_to(QDict, obj), "data");
770e1ce7d74SMarkus Armbruster     dump_qobject(1, data);
771cb3e7f08SMarc-André Lureau     qobject_unref(obj);
7723b098d56SEric Blake     visit_free(v);
773a8d8ecb7SMax Reitz }
774a8d8ecb7SMax Reitz 
775e1ce7d74SMarkus Armbruster void bdrv_image_info_dump(ImageInfo *info)
776f364ec65SWenchao Xia {
777de38b500SEric Blake     char *size_buf, *dsize_buf;
778f364ec65SWenchao Xia     if (!info->has_actual_size) {
779de38b500SEric Blake         dsize_buf = g_strdup("unavailable");
780f364ec65SWenchao Xia     } else {
781de38b500SEric Blake         dsize_buf = size_to_str(info->actual_size);
782f364ec65SWenchao Xia     }
783de38b500SEric Blake     size_buf = size_to_str(info->virtual_size);
784e1ce7d74SMarkus Armbruster     qemu_printf("image: %s\n"
785f364ec65SWenchao Xia                 "file format: %s\n"
786f364ec65SWenchao Xia                 "virtual size: %s (%" PRId64 " bytes)\n"
787f364ec65SWenchao Xia                 "disk size: %s\n",
788f364ec65SWenchao Xia                 info->filename, info->format, size_buf,
789f364ec65SWenchao Xia                 info->virtual_size,
790f364ec65SWenchao Xia                 dsize_buf);
791de38b500SEric Blake     g_free(size_buf);
792de38b500SEric Blake     g_free(dsize_buf);
793f364ec65SWenchao Xia 
794f364ec65SWenchao Xia     if (info->has_encrypted && info->encrypted) {
795e1ce7d74SMarkus Armbruster         qemu_printf("encrypted: yes\n");
796f364ec65SWenchao Xia     }
797f364ec65SWenchao Xia 
798f364ec65SWenchao Xia     if (info->has_cluster_size) {
799e1ce7d74SMarkus Armbruster         qemu_printf("cluster_size: %" PRId64 "\n",
8005b917044SWenchao Xia                     info->cluster_size);
801f364ec65SWenchao Xia     }
802f364ec65SWenchao Xia 
803f364ec65SWenchao Xia     if (info->has_dirty_flag && info->dirty_flag) {
804e1ce7d74SMarkus Armbruster         qemu_printf("cleanly shut down: no\n");
805f364ec65SWenchao Xia     }
806f364ec65SWenchao Xia 
807*54fde4ffSMarkus Armbruster     if (info->backing_filename) {
808e1ce7d74SMarkus Armbruster         qemu_printf("backing file: %s", info->backing_filename);
809*54fde4ffSMarkus Armbruster         if (!info->full_backing_filename) {
810e1ce7d74SMarkus Armbruster             qemu_printf(" (cannot determine actual path)");
8115c9d9ca5SJohn Snow         } else if (strcmp(info->backing_filename,
8125c9d9ca5SJohn Snow                           info->full_backing_filename) != 0) {
813e1ce7d74SMarkus Armbruster             qemu_printf(" (actual path: %s)", info->full_backing_filename);
814f364ec65SWenchao Xia         }
815e1ce7d74SMarkus Armbruster         qemu_printf("\n");
816*54fde4ffSMarkus Armbruster         if (info->backing_filename_format) {
817e1ce7d74SMarkus Armbruster             qemu_printf("backing file format: %s\n",
8185b917044SWenchao Xia                         info->backing_filename_format);
819f364ec65SWenchao Xia         }
820f364ec65SWenchao Xia     }
821f364ec65SWenchao Xia 
822f364ec65SWenchao Xia     if (info->has_snapshots) {
823f364ec65SWenchao Xia         SnapshotInfoList *elem;
824f364ec65SWenchao Xia 
825e1ce7d74SMarkus Armbruster         qemu_printf("Snapshot list:\n");
826e1ce7d74SMarkus Armbruster         bdrv_snapshot_dump(NULL);
827e1ce7d74SMarkus Armbruster         qemu_printf("\n");
828f364ec65SWenchao Xia 
829f364ec65SWenchao Xia         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
830f364ec65SWenchao Xia          * we convert to the block layer's native QEMUSnapshotInfo for now.
831f364ec65SWenchao Xia          */
832f364ec65SWenchao Xia         for (elem = info->snapshots; elem; elem = elem->next) {
833f364ec65SWenchao Xia             QEMUSnapshotInfo sn = {
834f364ec65SWenchao Xia                 .vm_state_size = elem->value->vm_state_size,
835f364ec65SWenchao Xia                 .date_sec = elem->value->date_sec,
836f364ec65SWenchao Xia                 .date_nsec = elem->value->date_nsec,
837f364ec65SWenchao Xia                 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
838f364ec65SWenchao Xia                                  elem->value->vm_clock_nsec,
839b39847a5SPavel Dovgalyuk                 .icount = elem->value->has_icount ?
840b39847a5SPavel Dovgalyuk                           elem->value->icount : -1ULL,
841f364ec65SWenchao Xia             };
842f364ec65SWenchao Xia 
843f364ec65SWenchao Xia             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
844f364ec65SWenchao Xia             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
845e1ce7d74SMarkus Armbruster             bdrv_snapshot_dump(&sn);
846e1ce7d74SMarkus Armbruster             qemu_printf("\n");
847f364ec65SWenchao Xia         }
848f364ec65SWenchao Xia     }
849a8d8ecb7SMax Reitz 
850*54fde4ffSMarkus Armbruster     if (info->format_specific) {
851e1ce7d74SMarkus Armbruster         qemu_printf("Format specific information:\n");
852e1ce7d74SMarkus Armbruster         bdrv_image_info_specific_dump(info->format_specific);
853a8d8ecb7SMax Reitz     }
854f364ec65SWenchao Xia }
855