xref: /qemu/block/qapi.c (revision facda544)
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,
45*facda544SPeter Krempa                                         BlockDriverState *bs,
46*facda544SPeter Krempa                                         bool flat,
47*facda544SPeter Krempa                                         Error **errp)
48c13163fbSBenoît Canet {
49d5a8ee60SAlberto Garcia     ImageInfo **p_image_info;
50d5a8ee60SAlberto Garcia     BlockDriverState *bs0;
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);
62c13163fbSBenoît Canet     info->ro                     = bs->read_only;
63c13163fbSBenoît Canet     info->drv                    = g_strdup(bs->drv->format_name);
64c13163fbSBenoît Canet     info->encrypted              = bs->encrypted;
65c01c214bSDaniel P. Berrange     info->encryption_key_missing = false;
66c13163fbSBenoît Canet 
679e193c5aSKevin Wolf     info->cache = g_new(BlockdevCacheInfo, 1);
689e193c5aSKevin Wolf     *info->cache = (BlockdevCacheInfo) {
69c83f9fbaSKevin Wolf         .writeback      = blk ? blk_enable_write_cache(blk) : true,
709e193c5aSKevin Wolf         .direct         = !!(bs->open_flags & BDRV_O_NOCACHE),
719e193c5aSKevin Wolf         .no_flush       = !!(bs->open_flags & BDRV_O_NO_FLUSH),
729e193c5aSKevin Wolf     };
739e193c5aSKevin Wolf 
74c13163fbSBenoît Canet     if (bs->node_name[0]) {
75c13163fbSBenoît Canet         info->has_node_name = true;
76c13163fbSBenoît Canet         info->node_name = g_strdup(bs->node_name);
77c13163fbSBenoît Canet     }
78c13163fbSBenoît Canet 
79c13163fbSBenoît Canet     if (bs->backing_file[0]) {
80c13163fbSBenoît Canet         info->has_backing_file = true;
81c13163fbSBenoît Canet         info->backing_file = g_strdup(bs->backing_file);
82c13163fbSBenoît Canet     }
83c13163fbSBenoît Canet 
84590a63d5SVladimir Sementsov-Ogievskiy     if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
85590a63d5SVladimir Sementsov-Ogievskiy         info->has_dirty_bitmaps = true;
86590a63d5SVladimir Sementsov-Ogievskiy         info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
87590a63d5SVladimir Sementsov-Ogievskiy     }
88590a63d5SVladimir Sementsov-Ogievskiy 
89465bee1dSPeter Lieven     info->detect_zeroes = bs->detect_zeroes;
90c13163fbSBenoît Canet 
91022cdc9fSManos Pitsidianakis     if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
92c13163fbSBenoît Canet         ThrottleConfig cfg;
93022cdc9fSManos Pitsidianakis         BlockBackendPublic *blkp = blk_get_public(blk);
9476f4afb4SAlberto Garcia 
95022cdc9fSManos Pitsidianakis         throttle_group_get_config(&blkp->throttle_group_member, &cfg);
9676f4afb4SAlberto Garcia 
97c13163fbSBenoît Canet         info->bps     = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
98c13163fbSBenoît Canet         info->bps_rd  = cfg.buckets[THROTTLE_BPS_READ].avg;
99c13163fbSBenoît Canet         info->bps_wr  = cfg.buckets[THROTTLE_BPS_WRITE].avg;
100c13163fbSBenoît Canet 
101c13163fbSBenoît Canet         info->iops    = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
102c13163fbSBenoît Canet         info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
103c13163fbSBenoît Canet         info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
104c13163fbSBenoît Canet 
105c13163fbSBenoît Canet         info->has_bps_max     = cfg.buckets[THROTTLE_BPS_TOTAL].max;
106c13163fbSBenoît Canet         info->bps_max         = cfg.buckets[THROTTLE_BPS_TOTAL].max;
107c13163fbSBenoît Canet         info->has_bps_rd_max  = cfg.buckets[THROTTLE_BPS_READ].max;
108c13163fbSBenoît Canet         info->bps_rd_max      = cfg.buckets[THROTTLE_BPS_READ].max;
109c13163fbSBenoît Canet         info->has_bps_wr_max  = cfg.buckets[THROTTLE_BPS_WRITE].max;
110c13163fbSBenoît Canet         info->bps_wr_max      = cfg.buckets[THROTTLE_BPS_WRITE].max;
111c13163fbSBenoît Canet 
112c13163fbSBenoît Canet         info->has_iops_max    = cfg.buckets[THROTTLE_OPS_TOTAL].max;
113c13163fbSBenoît Canet         info->iops_max        = cfg.buckets[THROTTLE_OPS_TOTAL].max;
114c13163fbSBenoît Canet         info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
115c13163fbSBenoît Canet         info->iops_rd_max     = cfg.buckets[THROTTLE_OPS_READ].max;
116c13163fbSBenoît Canet         info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
117c13163fbSBenoît Canet         info->iops_wr_max     = cfg.buckets[THROTTLE_OPS_WRITE].max;
118c13163fbSBenoît Canet 
119398befdfSAlberto Garcia         info->has_bps_max_length     = info->has_bps_max;
120398befdfSAlberto Garcia         info->bps_max_length         =
121398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
122398befdfSAlberto Garcia         info->has_bps_rd_max_length  = info->has_bps_rd_max;
123398befdfSAlberto Garcia         info->bps_rd_max_length      =
124398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_READ].burst_length;
125398befdfSAlberto Garcia         info->has_bps_wr_max_length  = info->has_bps_wr_max;
126398befdfSAlberto Garcia         info->bps_wr_max_length      =
127398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
128398befdfSAlberto Garcia 
129398befdfSAlberto Garcia         info->has_iops_max_length    = info->has_iops_max;
130398befdfSAlberto Garcia         info->iops_max_length        =
131398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
132398befdfSAlberto Garcia         info->has_iops_rd_max_length = info->has_iops_rd_max;
133398befdfSAlberto Garcia         info->iops_rd_max_length     =
134398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_READ].burst_length;
135398befdfSAlberto Garcia         info->has_iops_wr_max_length = info->has_iops_wr_max;
136398befdfSAlberto Garcia         info->iops_wr_max_length     =
137398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
138398befdfSAlberto Garcia 
139c13163fbSBenoît Canet         info->has_iops_size = cfg.op_size;
140c13163fbSBenoît Canet         info->iops_size = cfg.op_size;
141b8fe1694SAlberto Garcia 
142b8fe1694SAlberto Garcia         info->has_group = true;
143022cdc9fSManos Pitsidianakis         info->group =
144022cdc9fSManos Pitsidianakis             g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
145c13163fbSBenoît Canet     }
146c13163fbSBenoît Canet 
147e2462113SFrancesco Romani     info->write_threshold = bdrv_write_threshold_get(bs);
148e2462113SFrancesco Romani 
149d5a8ee60SAlberto Garcia     bs0 = bs;
150d5a8ee60SAlberto Garcia     p_image_info = &info->image;
151d3c8c674SKevin Wolf     info->backing_file_depth = 0;
152d5a8ee60SAlberto Garcia     while (1) {
153d5a8ee60SAlberto Garcia         Error *local_err = NULL;
154d5a8ee60SAlberto Garcia         bdrv_query_image_info(bs0, p_image_info, &local_err);
155d5a8ee60SAlberto Garcia         if (local_err) {
156d5a8ee60SAlberto Garcia             error_propagate(errp, local_err);
157d5a8ee60SAlberto Garcia             qapi_free_BlockDeviceInfo(info);
158d5a8ee60SAlberto Garcia             return NULL;
159d5a8ee60SAlberto Garcia         }
160d3c8c674SKevin Wolf 
161*facda544SPeter Krempa         /* stop gathering data for flat output */
162*facda544SPeter Krempa         if (flat) {
163*facda544SPeter Krempa             break;
164*facda544SPeter Krempa         }
165*facda544SPeter Krempa 
166760e0063SKevin Wolf         if (bs0->drv && bs0->backing) {
167d3c8c674SKevin Wolf             info->backing_file_depth++;
168760e0063SKevin Wolf             bs0 = bs0->backing->bs;
169d5a8ee60SAlberto Garcia             (*p_image_info)->has_backing_image = true;
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 */
1778e8eb0a9SKevin Wolf         while (blk && bs0->drv && bs0->implicit) {
178d3c8c674SKevin Wolf             bs0 = backing_bs(bs0);
1798e8eb0a9SKevin Wolf             assert(bs0);
180d3c8c674SKevin Wolf         }
181d5a8ee60SAlberto Garcia     }
182d5a8ee60SAlberto Garcia 
183c13163fbSBenoît Canet     return info;
184c13163fbSBenoît Canet }
185c13163fbSBenoît Canet 
186fb0ed453SWenchao Xia /*
187fb0ed453SWenchao Xia  * Returns 0 on success, with *p_list either set to describe snapshot
188fb0ed453SWenchao Xia  * information, or NULL because there are no snapshots.  Returns -errno on
189fb0ed453SWenchao Xia  * error, with *p_list untouched.
190fb0ed453SWenchao Xia  */
191fb0ed453SWenchao Xia int bdrv_query_snapshot_info_list(BlockDriverState *bs,
192fb0ed453SWenchao Xia                                   SnapshotInfoList **p_list,
193fb0ed453SWenchao Xia                                   Error **errp)
194f364ec65SWenchao Xia {
195f364ec65SWenchao Xia     int i, sn_count;
196f364ec65SWenchao Xia     QEMUSnapshotInfo *sn_tab = NULL;
197fb0ed453SWenchao Xia     SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
198fb0ed453SWenchao Xia     SnapshotInfo *info;
199fb0ed453SWenchao Xia 
200f364ec65SWenchao Xia     sn_count = bdrv_snapshot_list(bs, &sn_tab);
201fb0ed453SWenchao Xia     if (sn_count < 0) {
202fb0ed453SWenchao Xia         const char *dev = bdrv_get_device_name(bs);
203fb0ed453SWenchao Xia         switch (sn_count) {
204fb0ed453SWenchao Xia         case -ENOMEDIUM:
205fb0ed453SWenchao Xia             error_setg(errp, "Device '%s' is not inserted", dev);
206fb0ed453SWenchao Xia             break;
207fb0ed453SWenchao Xia         case -ENOTSUP:
208fb0ed453SWenchao Xia             error_setg(errp,
209fb0ed453SWenchao Xia                        "Device '%s' does not support internal snapshots",
210fb0ed453SWenchao Xia                        dev);
211fb0ed453SWenchao Xia             break;
212fb0ed453SWenchao Xia         default:
213fb0ed453SWenchao Xia             error_setg_errno(errp, -sn_count,
214fb0ed453SWenchao Xia                              "Can't list snapshots of device '%s'", dev);
215fb0ed453SWenchao Xia             break;
216fb0ed453SWenchao Xia         }
217fb0ed453SWenchao Xia         return sn_count;
218fb0ed453SWenchao Xia     }
219f364ec65SWenchao Xia 
220f364ec65SWenchao Xia     for (i = 0; i < sn_count; i++) {
221fb0ed453SWenchao Xia         info = g_new0(SnapshotInfo, 1);
222fb0ed453SWenchao Xia         info->id            = g_strdup(sn_tab[i].id_str);
223fb0ed453SWenchao Xia         info->name          = g_strdup(sn_tab[i].name);
224fb0ed453SWenchao Xia         info->vm_state_size = sn_tab[i].vm_state_size;
225fb0ed453SWenchao Xia         info->date_sec      = sn_tab[i].date_sec;
226fb0ed453SWenchao Xia         info->date_nsec     = sn_tab[i].date_nsec;
227fb0ed453SWenchao Xia         info->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
228fb0ed453SWenchao Xia         info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
229f364ec65SWenchao Xia 
230fb0ed453SWenchao Xia         info_list = g_new0(SnapshotInfoList, 1);
231fb0ed453SWenchao Xia         info_list->value = info;
232f364ec65SWenchao Xia 
233f364ec65SWenchao Xia         /* XXX: waiting for the qapi to support qemu-queue.h types */
234f364ec65SWenchao Xia         if (!cur_item) {
235fb0ed453SWenchao Xia             head = cur_item = info_list;
236f364ec65SWenchao Xia         } else {
237f364ec65SWenchao Xia             cur_item->next = info_list;
238f364ec65SWenchao Xia             cur_item = info_list;
239f364ec65SWenchao Xia         }
240f364ec65SWenchao Xia 
241f364ec65SWenchao Xia     }
242f364ec65SWenchao Xia 
243f364ec65SWenchao Xia     g_free(sn_tab);
244fb0ed453SWenchao Xia     *p_list = head;
245fb0ed453SWenchao Xia     return 0;
246f364ec65SWenchao Xia }
247f364ec65SWenchao Xia 
24843526ec8SWenchao Xia /**
24943526ec8SWenchao Xia  * bdrv_query_image_info:
25043526ec8SWenchao Xia  * @bs: block device to examine
25143526ec8SWenchao Xia  * @p_info: location to store image information
25243526ec8SWenchao Xia  * @errp: location to store error information
25343526ec8SWenchao Xia  *
254553a7e87SWenchao Xia  * Store "flat" image information in @p_info.
255553a7e87SWenchao Xia  *
256553a7e87SWenchao Xia  * "Flat" means it does *not* query backing image information,
257553a7e87SWenchao Xia  * i.e. (*pinfo)->has_backing_image will be set to false and
258553a7e87SWenchao Xia  * (*pinfo)->backing_image to NULL even when the image does in fact have
259553a7e87SWenchao Xia  * a backing image.
260553a7e87SWenchao Xia  *
26143526ec8SWenchao Xia  * @p_info will be set only on success. On error, store error in @errp.
26243526ec8SWenchao Xia  */
26343526ec8SWenchao Xia void bdrv_query_image_info(BlockDriverState *bs,
26443526ec8SWenchao Xia                            ImageInfo **p_info,
26543526ec8SWenchao Xia                            Error **errp)
266f364ec65SWenchao Xia {
26752bf1e72SMarkus Armbruster     int64_t size;
26843526ec8SWenchao Xia     const char *backing_filename;
269f364ec65SWenchao Xia     BlockDriverInfo bdi;
27043526ec8SWenchao Xia     int ret;
27143526ec8SWenchao Xia     Error *err = NULL;
27252bf1e72SMarkus Armbruster     ImageInfo *info;
273f364ec65SWenchao Xia 
2741963f8d5SPaolo Bonzini     aio_context_acquire(bdrv_get_aio_context(bs));
2751963f8d5SPaolo Bonzini 
27652bf1e72SMarkus Armbruster     size = bdrv_getlength(bs);
27752bf1e72SMarkus Armbruster     if (size < 0) {
2789adceb02SFam Zheng         error_setg_errno(errp, -size, "Can't get image size '%s'",
2799adceb02SFam Zheng                          bs->exact_filename);
2801963f8d5SPaolo Bonzini         goto out;
28152bf1e72SMarkus Armbruster     }
282f364ec65SWenchao Xia 
283f30c66baSMax Reitz     bdrv_refresh_filename(bs);
284f30c66baSMax Reitz 
28552bf1e72SMarkus Armbruster     info = g_new0(ImageInfo, 1);
28643526ec8SWenchao Xia     info->filename        = g_strdup(bs->filename);
287f364ec65SWenchao Xia     info->format          = g_strdup(bdrv_get_format_name(bs));
28852bf1e72SMarkus Armbruster     info->virtual_size    = size;
289f364ec65SWenchao Xia     info->actual_size     = bdrv_get_allocated_file_size(bs);
290f364ec65SWenchao Xia     info->has_actual_size = info->actual_size >= 0;
291f364ec65SWenchao Xia     if (bdrv_is_encrypted(bs)) {
292f364ec65SWenchao Xia         info->encrypted = true;
293f364ec65SWenchao Xia         info->has_encrypted = true;
294f364ec65SWenchao Xia     }
295f364ec65SWenchao Xia     if (bdrv_get_info(bs, &bdi) >= 0) {
296f364ec65SWenchao Xia         if (bdi.cluster_size != 0) {
297f364ec65SWenchao Xia             info->cluster_size = bdi.cluster_size;
298f364ec65SWenchao Xia             info->has_cluster_size = true;
299f364ec65SWenchao Xia         }
300f364ec65SWenchao Xia         info->dirty_flag = bdi.is_dirty;
301f364ec65SWenchao Xia         info->has_dirty_flag = true;
302f364ec65SWenchao Xia     }
3031bf6e9caSAndrey Shinkevich     info->format_specific = bdrv_get_specific_info(bs, &err);
3041bf6e9caSAndrey Shinkevich     if (err) {
3051bf6e9caSAndrey Shinkevich         error_propagate(errp, err);
3061bf6e9caSAndrey Shinkevich         qapi_free_ImageInfo(info);
3071bf6e9caSAndrey Shinkevich         goto out;
3081bf6e9caSAndrey Shinkevich     }
309eae041feSMax Reitz     info->has_format_specific = info->format_specific != NULL;
310eae041feSMax Reitz 
31143526ec8SWenchao Xia     backing_filename = bs->backing_file;
312f364ec65SWenchao Xia     if (backing_filename[0] != '\0') {
3136b6833c1SMax Reitz         char *backing_filename2;
314f364ec65SWenchao Xia         info->backing_filename = g_strdup(backing_filename);
315f364ec65SWenchao Xia         info->has_backing_filename = true;
3166b6833c1SMax Reitz         backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
317f364ec65SWenchao Xia 
31812dcb1c0SJohn Snow         /* Always report the full_backing_filename if present, even if it's the
31912dcb1c0SJohn Snow          * same as backing_filename. That they are same is useful info. */
32012dcb1c0SJohn Snow         if (backing_filename2) {
32112dcb1c0SJohn Snow             info->full_backing_filename = g_strdup(backing_filename2);
322f364ec65SWenchao Xia             info->has_full_backing_filename = true;
323f364ec65SWenchao Xia         }
324f364ec65SWenchao Xia 
325f364ec65SWenchao Xia         if (bs->backing_format[0]) {
326f364ec65SWenchao Xia             info->backing_filename_format = g_strdup(bs->backing_format);
327f364ec65SWenchao Xia             info->has_backing_filename_format = true;
328f364ec65SWenchao Xia         }
329564d64bdSJeff Cody         g_free(backing_filename2);
330f364ec65SWenchao Xia     }
33143526ec8SWenchao Xia 
33243526ec8SWenchao Xia     ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
33343526ec8SWenchao Xia     switch (ret) {
33443526ec8SWenchao Xia     case 0:
33543526ec8SWenchao Xia         if (info->snapshots) {
33643526ec8SWenchao Xia             info->has_snapshots = true;
33743526ec8SWenchao Xia         }
33843526ec8SWenchao Xia         break;
33943526ec8SWenchao Xia     /* recoverable error */
34043526ec8SWenchao Xia     case -ENOMEDIUM:
34143526ec8SWenchao Xia     case -ENOTSUP:
34243526ec8SWenchao Xia         error_free(err);
34343526ec8SWenchao Xia         break;
34443526ec8SWenchao Xia     default:
34543526ec8SWenchao Xia         error_propagate(errp, err);
34643526ec8SWenchao Xia         qapi_free_ImageInfo(info);
3471963f8d5SPaolo Bonzini         goto out;
34843526ec8SWenchao Xia     }
34943526ec8SWenchao Xia 
35043526ec8SWenchao Xia     *p_info = info;
3511963f8d5SPaolo Bonzini 
3521963f8d5SPaolo Bonzini out:
3531963f8d5SPaolo Bonzini     aio_context_release(bdrv_get_aio_context(bs));
354f364ec65SWenchao Xia }
355f364ec65SWenchao Xia 
356553a7e87SWenchao Xia /* @p_info will be set only on success. */
357d829a211SMarkus Armbruster static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
358553a7e87SWenchao Xia                             Error **errp)
359f364ec65SWenchao Xia {
360f364ec65SWenchao Xia     BlockInfo *info = g_malloc0(sizeof(*info));
361d829a211SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
36246eade7bSKevin Wolf     char *qdev;
36346eade7bSKevin Wolf 
364d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of */
365d3c8c674SKevin Wolf     while (bs && bs->drv && bs->implicit) {
366d3c8c674SKevin Wolf         bs = backing_bs(bs);
367d3c8c674SKevin Wolf     }
368d3c8c674SKevin Wolf 
369d829a211SMarkus Armbruster     info->device = g_strdup(blk_name(blk));
370f364ec65SWenchao Xia     info->type = g_strdup("unknown");
371a7f53e26SMarkus Armbruster     info->locked = blk_dev_is_medium_locked(blk);
372a7f53e26SMarkus Armbruster     info->removable = blk_dev_has_removable_media(blk);
373f364ec65SWenchao Xia 
37446eade7bSKevin Wolf     qdev = blk_get_attached_dev_id(blk);
37546eade7bSKevin Wolf     if (qdev && *qdev) {
37646eade7bSKevin Wolf         info->has_qdev = true;
37746eade7bSKevin Wolf         info->qdev = qdev;
37846eade7bSKevin Wolf     } else {
37946eade7bSKevin Wolf         g_free(qdev);
38046eade7bSKevin Wolf     }
38146eade7bSKevin Wolf 
382327032ceSMax Reitz     if (blk_dev_has_tray(blk)) {
383f364ec65SWenchao Xia         info->has_tray_open = true;
384a7f53e26SMarkus Armbruster         info->tray_open = blk_dev_is_tray_open(blk);
385f364ec65SWenchao Xia     }
386f364ec65SWenchao Xia 
387373340b2SMax Reitz     if (blk_iostatus_is_enabled(blk)) {
388f364ec65SWenchao Xia         info->has_io_status = true;
389373340b2SMax Reitz         info->io_status = blk_iostatus(blk);
390f364ec65SWenchao Xia     }
391f364ec65SWenchao Xia 
3925433c24fSMax Reitz     if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
39321b56835SFam Zheng         info->has_dirty_bitmaps = true;
39421b56835SFam Zheng         info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
39521b56835SFam Zheng     }
39621b56835SFam Zheng 
3975433c24fSMax Reitz     if (bs && bs->drv) {
398f364ec65SWenchao Xia         info->has_inserted = true;
399*facda544SPeter Krempa         info->inserted = bdrv_block_device_info(blk, bs, false, errp);
400d5a8ee60SAlberto Garcia         if (info->inserted == NULL) {
401553a7e87SWenchao Xia             goto err;
402f364ec65SWenchao Xia         }
403553a7e87SWenchao Xia     }
404553a7e87SWenchao Xia 
405553a7e87SWenchao Xia     *p_info = info;
406553a7e87SWenchao Xia     return;
407553a7e87SWenchao Xia 
408553a7e87SWenchao Xia  err:
409553a7e87SWenchao Xia     qapi_free_BlockInfo(info);
410f364ec65SWenchao Xia }
411f364ec65SWenchao Xia 
4127e5c776dSVladimir Sementsov-Ogievskiy static uint64List *uint64_list(uint64_t *list, int size)
4137e5c776dSVladimir Sementsov-Ogievskiy {
4147e5c776dSVladimir Sementsov-Ogievskiy     int i;
4157e5c776dSVladimir Sementsov-Ogievskiy     uint64List *out_list = NULL;
4167e5c776dSVladimir Sementsov-Ogievskiy     uint64List **pout_list = &out_list;
4177e5c776dSVladimir Sementsov-Ogievskiy 
4187e5c776dSVladimir Sementsov-Ogievskiy     for (i = 0; i < size; i++) {
4197e5c776dSVladimir Sementsov-Ogievskiy         uint64List *entry = g_new(uint64List, 1);
4207e5c776dSVladimir Sementsov-Ogievskiy         entry->value = list[i];
4217e5c776dSVladimir Sementsov-Ogievskiy         *pout_list = entry;
4227e5c776dSVladimir Sementsov-Ogievskiy         pout_list = &entry->next;
4237e5c776dSVladimir Sementsov-Ogievskiy     }
4247e5c776dSVladimir Sementsov-Ogievskiy 
4257e5c776dSVladimir Sementsov-Ogievskiy     *pout_list = NULL;
4267e5c776dSVladimir Sementsov-Ogievskiy 
4277e5c776dSVladimir Sementsov-Ogievskiy     return out_list;
4287e5c776dSVladimir Sementsov-Ogievskiy }
4297e5c776dSVladimir Sementsov-Ogievskiy 
4307e5c776dSVladimir Sementsov-Ogievskiy static void bdrv_latency_histogram_stats(BlockLatencyHistogram *hist,
4317e5c776dSVladimir Sementsov-Ogievskiy                                          bool *not_null,
4327e5c776dSVladimir Sementsov-Ogievskiy                                          BlockLatencyHistogramInfo **info)
4337e5c776dSVladimir Sementsov-Ogievskiy {
4347e5c776dSVladimir Sementsov-Ogievskiy     *not_null = hist->bins != NULL;
4357e5c776dSVladimir Sementsov-Ogievskiy     if (*not_null) {
4367e5c776dSVladimir Sementsov-Ogievskiy         *info = g_new0(BlockLatencyHistogramInfo, 1);
4377e5c776dSVladimir Sementsov-Ogievskiy 
4387e5c776dSVladimir Sementsov-Ogievskiy         (*info)->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
4397e5c776dSVladimir Sementsov-Ogievskiy         (*info)->bins = uint64_list(hist->bins, hist->nbins);
4407e5c776dSVladimir Sementsov-Ogievskiy     }
4417e5c776dSVladimir Sementsov-Ogievskiy }
4427e5c776dSVladimir Sementsov-Ogievskiy 
44354302156SMax Reitz static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
444f364ec65SWenchao Xia {
4452b77e60aSKevin Wolf     BlockAcctStats *stats = blk_get_stats(blk);
446979e9b03SAlberto Garcia     BlockAcctTimedStats *ts = NULL;
4477f0e9da6SMax Reitz 
44854302156SMax Reitz     ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
44954302156SMax Reitz     ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
450159f85ddSAnton Nefedov     ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
45154302156SMax Reitz     ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
45254302156SMax Reitz     ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
453159f85ddSAnton Nefedov     ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
4547ee12dafSAlberto Garcia 
45554302156SMax Reitz     ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
45654302156SMax Reitz     ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
45754302156SMax Reitz     ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
458159f85ddSAnton Nefedov     ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
4597ee12dafSAlberto Garcia 
46054302156SMax Reitz     ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
46154302156SMax Reitz     ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
46254302156SMax Reitz     ds->invalid_flush_operations =
4637ee12dafSAlberto Garcia         stats->invalid_ops[BLOCK_ACCT_FLUSH];
464159f85ddSAnton Nefedov     ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
4657ee12dafSAlberto Garcia 
46654302156SMax Reitz     ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
46754302156SMax Reitz     ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
468159f85ddSAnton Nefedov     ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
46954302156SMax Reitz     ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
47054302156SMax Reitz     ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
47154302156SMax Reitz     ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
47254302156SMax Reitz     ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
473159f85ddSAnton Nefedov     ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
474cb38fffbSAlberto Garcia 
47554302156SMax Reitz     ds->has_idle_time_ns = stats->last_access_time_ns > 0;
47654302156SMax Reitz     if (ds->has_idle_time_ns) {
47754302156SMax Reitz         ds->idle_time_ns = block_acct_idle_time_ns(stats);
478cb38fffbSAlberto Garcia     }
479362e9299SAlberto Garcia 
48054302156SMax Reitz     ds->account_invalid = stats->account_invalid;
48154302156SMax Reitz     ds->account_failed = stats->account_failed;
482979e9b03SAlberto Garcia 
483979e9b03SAlberto Garcia     while ((ts = block_acct_interval_next(stats, ts))) {
484979e9b03SAlberto Garcia         BlockDeviceTimedStatsList *timed_stats =
485979e9b03SAlberto Garcia             g_malloc0(sizeof(*timed_stats));
486979e9b03SAlberto Garcia         BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
48754302156SMax Reitz         timed_stats->next = ds->timed_stats;
488979e9b03SAlberto Garcia         timed_stats->value = dev_stats;
48954302156SMax Reitz         ds->timed_stats = timed_stats;
490979e9b03SAlberto Garcia 
491979e9b03SAlberto Garcia         TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
492979e9b03SAlberto Garcia         TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
493979e9b03SAlberto Garcia         TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
494979e9b03SAlberto Garcia 
495979e9b03SAlberto Garcia         dev_stats->interval_length = ts->interval_length;
496979e9b03SAlberto Garcia 
497979e9b03SAlberto Garcia         dev_stats->min_rd_latency_ns = timed_average_min(rd);
498979e9b03SAlberto Garcia         dev_stats->max_rd_latency_ns = timed_average_max(rd);
499979e9b03SAlberto Garcia         dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
500979e9b03SAlberto Garcia 
501979e9b03SAlberto Garcia         dev_stats->min_wr_latency_ns = timed_average_min(wr);
502979e9b03SAlberto Garcia         dev_stats->max_wr_latency_ns = timed_average_max(wr);
503979e9b03SAlberto Garcia         dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
504979e9b03SAlberto Garcia 
505979e9b03SAlberto Garcia         dev_stats->min_flush_latency_ns = timed_average_min(fl);
506979e9b03SAlberto Garcia         dev_stats->max_flush_latency_ns = timed_average_max(fl);
507979e9b03SAlberto Garcia         dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
50896e4dedaSAlberto Garcia 
50996e4dedaSAlberto Garcia         dev_stats->avg_rd_queue_depth =
51096e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_READ);
51196e4dedaSAlberto Garcia         dev_stats->avg_wr_queue_depth =
51296e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
513979e9b03SAlberto Garcia     }
5147e5c776dSVladimir Sementsov-Ogievskiy 
5157e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_READ],
516cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->has_rd_latency_histogram,
517cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->rd_latency_histogram);
5187e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_WRITE],
519cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->has_wr_latency_histogram,
520cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->wr_latency_histogram);
5217e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_FLUSH],
522cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->has_flush_latency_histogram,
523cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->flush_latency_histogram);
5247f0e9da6SMax Reitz }
525f364ec65SWenchao Xia 
526d3c8c674SKevin Wolf static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
527d3c8c674SKevin Wolf                                         bool blk_level)
5282b77e60aSKevin Wolf {
52920a6d768SDou Liyang     BlockStats *s = NULL;
53020a6d768SDou Liyang 
53120a6d768SDou Liyang     s = g_malloc0(sizeof(*s));
53220a6d768SDou Liyang     s->stats = g_malloc0(sizeof(*s->stats));
53320a6d768SDou Liyang 
53420a6d768SDou Liyang     if (!bs) {
53520a6d768SDou Liyang         return s;
53620a6d768SDou Liyang     }
53720a6d768SDou Liyang 
538d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of in
539d3c8c674SKevin Wolf      * a BlockBackend-level command. Stay at the exact node for a node-level
540d3c8c674SKevin Wolf      * command. */
541d3c8c674SKevin Wolf     while (blk_level && bs->drv && bs->implicit) {
542d3c8c674SKevin Wolf         bs = backing_bs(bs);
543d3c8c674SKevin Wolf         assert(bs);
544d3c8c674SKevin Wolf     }
545d3c8c674SKevin Wolf 
5462b77e60aSKevin Wolf     if (bdrv_get_node_name(bs)[0]) {
5472b77e60aSKevin Wolf         s->has_node_name = true;
5482b77e60aSKevin Wolf         s->node_name = g_strdup(bdrv_get_node_name(bs));
5492b77e60aSKevin Wolf     }
5502b77e60aSKevin Wolf 
551f7946da2SPaolo Bonzini     s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
55253d8f9d8SMax Reitz 
553d9245599SAnton Nefedov     s->driver_specific = bdrv_get_specific_stats(bs);
554d9245599SAnton Nefedov     if (s->driver_specific) {
555d9245599SAnton Nefedov         s->has_driver_specific = true;
556d9245599SAnton Nefedov     }
557d9245599SAnton Nefedov 
558f364ec65SWenchao Xia     if (bs->file) {
559f364ec65SWenchao Xia         s->has_parent = true;
560d3c8c674SKevin Wolf         s->parent = bdrv_query_bds_stats(bs->file->bs, blk_level);
561f364ec65SWenchao Xia     }
562f364ec65SWenchao Xia 
563d3c8c674SKevin Wolf     if (blk_level && bs->backing) {
564c8059b97SFam Zheng         s->has_backing = true;
565d3c8c674SKevin Wolf         s->backing = bdrv_query_bds_stats(bs->backing->bs, blk_level);
566c8059b97SFam Zheng     }
567c8059b97SFam Zheng 
56820a6d768SDou Liyang     return s;
569b07363a1SKevin Wolf }
570b07363a1SKevin Wolf 
571f364ec65SWenchao Xia BlockInfoList *qmp_query_block(Error **errp)
572f364ec65SWenchao Xia {
573f364ec65SWenchao Xia     BlockInfoList *head = NULL, **p_next = &head;
574d829a211SMarkus Armbruster     BlockBackend *blk;
575553a7e87SWenchao Xia     Error *local_err = NULL;
576f364ec65SWenchao Xia 
577d5b68844SKevin Wolf     for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
578d5b68844SKevin Wolf         BlockInfoList *info;
579d5b68844SKevin Wolf 
580ec18b0a9SKevin Wolf         if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
581d5b68844SKevin Wolf             continue;
582d5b68844SKevin Wolf         }
583d5b68844SKevin Wolf 
584d5b68844SKevin Wolf         info = g_malloc0(sizeof(*info));
585d829a211SMarkus Armbruster         bdrv_query_info(blk, &info->value, &local_err);
58684d18f06SMarkus Armbruster         if (local_err) {
587553a7e87SWenchao Xia             error_propagate(errp, local_err);
588903c341dSMarkus Armbruster             g_free(info);
589903c341dSMarkus Armbruster             qapi_free_BlockInfoList(head);
590903c341dSMarkus Armbruster             return NULL;
591553a7e87SWenchao Xia         }
592f364ec65SWenchao Xia 
593f364ec65SWenchao Xia         *p_next = info;
594f364ec65SWenchao Xia         p_next = &info->next;
595f364ec65SWenchao Xia     }
596f364ec65SWenchao Xia 
597f364ec65SWenchao Xia     return head;
598f364ec65SWenchao Xia }
599f364ec65SWenchao Xia 
600f71eaa74SFam Zheng BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
601f71eaa74SFam Zheng                                      bool query_nodes,
602f71eaa74SFam Zheng                                      Error **errp)
603f364ec65SWenchao Xia {
604f364ec65SWenchao Xia     BlockStatsList *head = NULL, **p_next = &head;
605a6baa608SDou Liyang     BlockBackend *blk;
606a6baa608SDou Liyang     BlockDriverState *bs;
607f364ec65SWenchao Xia 
608f71eaa74SFam Zheng     /* Just to be safe if query_nodes is not always initialized */
609a6baa608SDou Liyang     if (has_query_nodes && query_nodes) {
610a6baa608SDou Liyang         for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
611f364ec65SWenchao Xia             BlockStatsList *info = g_malloc0(sizeof(*info));
612a6baa608SDou Liyang             AioContext *ctx = bdrv_get_aio_context(bs);
61313344f3aSStefan Hajnoczi 
61413344f3aSStefan Hajnoczi             aio_context_acquire(ctx);
615a6baa608SDou Liyang             info->value = bdrv_query_bds_stats(bs, false);
61613344f3aSStefan Hajnoczi             aio_context_release(ctx);
617f364ec65SWenchao Xia 
618f364ec65SWenchao Xia             *p_next = info;
619f364ec65SWenchao Xia             p_next = &info->next;
620f364ec65SWenchao Xia         }
621a6baa608SDou Liyang     } else {
622567dcb31SKevin Wolf         for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
623f62492bbSKevin Wolf             BlockStatsList *info;
624a6baa608SDou Liyang             AioContext *ctx = blk_get_aio_context(blk);
625a6baa608SDou Liyang             BlockStats *s;
6265a9cb5a9SKevin Wolf             char *qdev;
627a6baa608SDou Liyang 
628567dcb31SKevin Wolf             if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
629567dcb31SKevin Wolf                 continue;
630567dcb31SKevin Wolf             }
631567dcb31SKevin Wolf 
632a6baa608SDou Liyang             aio_context_acquire(ctx);
633a6baa608SDou Liyang             s = bdrv_query_bds_stats(blk_bs(blk), true);
634a6baa608SDou Liyang             s->has_device = true;
635a6baa608SDou Liyang             s->device = g_strdup(blk_name(blk));
6365a9cb5a9SKevin Wolf 
6375a9cb5a9SKevin Wolf             qdev = blk_get_attached_dev_id(blk);
6385a9cb5a9SKevin Wolf             if (qdev && *qdev) {
6395a9cb5a9SKevin Wolf                 s->has_qdev = true;
6405a9cb5a9SKevin Wolf                 s->qdev = qdev;
6415a9cb5a9SKevin Wolf             } else {
6425a9cb5a9SKevin Wolf                 g_free(qdev);
6435a9cb5a9SKevin Wolf             }
6445a9cb5a9SKevin Wolf 
645a6baa608SDou Liyang             bdrv_query_blk_stats(s->stats, blk);
646a6baa608SDou Liyang             aio_context_release(ctx);
647a6baa608SDou Liyang 
648f62492bbSKevin Wolf             info = g_malloc0(sizeof(*info));
649a6baa608SDou Liyang             info->value = s;
650a6baa608SDou Liyang             *p_next = info;
651a6baa608SDou Liyang             p_next = &info->next;
652a6baa608SDou Liyang         }
653a6baa608SDou Liyang     }
654f364ec65SWenchao Xia 
655f364ec65SWenchao Xia     return head;
656f364ec65SWenchao Xia }
657f364ec65SWenchao Xia 
658e1ce7d74SMarkus Armbruster void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
659f364ec65SWenchao Xia {
660de38b500SEric Blake     char date_buf[128], clock_buf[128];
661f364ec65SWenchao Xia     struct tm tm;
662f364ec65SWenchao Xia     time_t ti;
663f364ec65SWenchao Xia     int64_t secs;
664de38b500SEric Blake     char *sizing = NULL;
665f364ec65SWenchao Xia 
666f364ec65SWenchao Xia     if (!sn) {
667e1ce7d74SMarkus Armbruster         qemu_printf("%-10s%-20s%7s%20s%15s",
668f364ec65SWenchao Xia                     "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
669f364ec65SWenchao Xia     } else {
670f364ec65SWenchao Xia         ti = sn->date_sec;
671f364ec65SWenchao Xia         localtime_r(&ti, &tm);
672f364ec65SWenchao Xia         strftime(date_buf, sizeof(date_buf),
673f364ec65SWenchao Xia                  "%Y-%m-%d %H:%M:%S", &tm);
674f364ec65SWenchao Xia         secs = sn->vm_clock_nsec / 1000000000;
675f364ec65SWenchao Xia         snprintf(clock_buf, sizeof(clock_buf),
676f364ec65SWenchao Xia                  "%02d:%02d:%02d.%03d",
677f364ec65SWenchao Xia                  (int)(secs / 3600),
678f364ec65SWenchao Xia                  (int)((secs / 60) % 60),
679f364ec65SWenchao Xia                  (int)(secs % 60),
680f364ec65SWenchao Xia                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
681de38b500SEric Blake         sizing = size_to_str(sn->vm_state_size);
682e1ce7d74SMarkus Armbruster         qemu_printf("%-10s%-20s%7s%20s%15s",
683f364ec65SWenchao Xia                     sn->id_str, sn->name,
684de38b500SEric Blake                     sizing,
685f364ec65SWenchao Xia                     date_buf,
686f364ec65SWenchao Xia                     clock_buf);
687f364ec65SWenchao Xia     }
688de38b500SEric Blake     g_free(sizing);
689f364ec65SWenchao Xia }
690f364ec65SWenchao Xia 
691e1ce7d74SMarkus Armbruster static void dump_qdict(int indentation, QDict *dict);
692e1ce7d74SMarkus Armbruster static void dump_qlist(int indentation, QList *list);
693a8d8ecb7SMax Reitz 
694e1ce7d74SMarkus Armbruster static void dump_qobject(int comp_indent, QObject *obj)
695a8d8ecb7SMax Reitz {
696a8d8ecb7SMax Reitz     switch (qobject_type(obj)) {
69701b2ffceSMarc-André Lureau         case QTYPE_QNUM: {
6987dc847ebSMax Reitz             QNum *value = qobject_to(QNum, obj);
69901b2ffceSMarc-André Lureau             char *tmp = qnum_to_string(value);
700e1ce7d74SMarkus Armbruster             qemu_printf("%s", tmp);
70101b2ffceSMarc-André Lureau             g_free(tmp);
702a8d8ecb7SMax Reitz             break;
703a8d8ecb7SMax Reitz         }
704a8d8ecb7SMax Reitz         case QTYPE_QSTRING: {
7057dc847ebSMax Reitz             QString *value = qobject_to(QString, obj);
706e1ce7d74SMarkus Armbruster             qemu_printf("%s", qstring_get_str(value));
707a8d8ecb7SMax Reitz             break;
708a8d8ecb7SMax Reitz         }
709a8d8ecb7SMax Reitz         case QTYPE_QDICT: {
7107dc847ebSMax Reitz             QDict *value = qobject_to(QDict, obj);
711e1ce7d74SMarkus Armbruster             dump_qdict(comp_indent, value);
712a8d8ecb7SMax Reitz             break;
713a8d8ecb7SMax Reitz         }
714a8d8ecb7SMax Reitz         case QTYPE_QLIST: {
7157dc847ebSMax Reitz             QList *value = qobject_to(QList, obj);
716e1ce7d74SMarkus Armbruster             dump_qlist(comp_indent, value);
717a8d8ecb7SMax Reitz             break;
718a8d8ecb7SMax Reitz         }
719a8d8ecb7SMax Reitz         case QTYPE_QBOOL: {
7207dc847ebSMax Reitz             QBool *value = qobject_to(QBool, obj);
721e1ce7d74SMarkus Armbruster             qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
722a8d8ecb7SMax Reitz             break;
723a8d8ecb7SMax Reitz         }
724a8d8ecb7SMax Reitz         default:
725a8d8ecb7SMax Reitz             abort();
726a8d8ecb7SMax Reitz     }
727a8d8ecb7SMax Reitz }
728a8d8ecb7SMax Reitz 
729e1ce7d74SMarkus Armbruster static void dump_qlist(int indentation, QList *list)
730a8d8ecb7SMax Reitz {
731a8d8ecb7SMax Reitz     const QListEntry *entry;
732a8d8ecb7SMax Reitz     int i = 0;
733a8d8ecb7SMax Reitz 
734a8d8ecb7SMax Reitz     for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
7351310a3d3SEric Blake         QType type = qobject_type(entry->value);
736a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
737e1ce7d74SMarkus Armbruster         qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
738853ccfedSPeter Xu                     composite ? '\n' : ' ');
739e1ce7d74SMarkus Armbruster         dump_qobject(indentation + 1, entry->value);
740a8d8ecb7SMax Reitz         if (!composite) {
741e1ce7d74SMarkus Armbruster             qemu_printf("\n");
742a8d8ecb7SMax Reitz         }
743a8d8ecb7SMax Reitz     }
744a8d8ecb7SMax Reitz }
745a8d8ecb7SMax Reitz 
746e1ce7d74SMarkus Armbruster static void dump_qdict(int indentation, QDict *dict)
747a8d8ecb7SMax Reitz {
748a8d8ecb7SMax Reitz     const QDictEntry *entry;
749a8d8ecb7SMax Reitz 
750a8d8ecb7SMax Reitz     for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
7511310a3d3SEric Blake         QType type = qobject_type(entry->value);
752a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
7535eda6227SPeter Xu         char *key = g_malloc(strlen(entry->key) + 1);
754a8d8ecb7SMax Reitz         int i;
755a8d8ecb7SMax Reitz 
756a8d8ecb7SMax Reitz         /* replace dashes with spaces in key (variable) names */
757a8d8ecb7SMax Reitz         for (i = 0; entry->key[i]; i++) {
758a8d8ecb7SMax Reitz             key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
759a8d8ecb7SMax Reitz         }
760a8d8ecb7SMax Reitz         key[i] = 0;
761e1ce7d74SMarkus Armbruster         qemu_printf("%*s%s:%c", indentation * 4, "", key,
762853ccfedSPeter Xu                     composite ? '\n' : ' ');
763e1ce7d74SMarkus Armbruster         dump_qobject(indentation + 1, entry->value);
764a8d8ecb7SMax Reitz         if (!composite) {
765e1ce7d74SMarkus Armbruster             qemu_printf("\n");
766a8d8ecb7SMax Reitz         }
7675eda6227SPeter Xu         g_free(key);
768a8d8ecb7SMax Reitz     }
769a8d8ecb7SMax Reitz }
770a8d8ecb7SMax Reitz 
771e1ce7d74SMarkus Armbruster void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec)
772a8d8ecb7SMax Reitz {
773a8d8ecb7SMax Reitz     QObject *obj, *data;
7747d5e199aSDaniel P. Berrange     Visitor *v = qobject_output_visitor_new(&obj);
775a8d8ecb7SMax Reitz 
7763b098d56SEric Blake     visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
7773b098d56SEric Blake     visit_complete(v, &obj);
7787dc847ebSMax Reitz     data = qdict_get(qobject_to(QDict, obj), "data");
779e1ce7d74SMarkus Armbruster     dump_qobject(1, data);
780cb3e7f08SMarc-André Lureau     qobject_unref(obj);
7813b098d56SEric Blake     visit_free(v);
782a8d8ecb7SMax Reitz }
783a8d8ecb7SMax Reitz 
784e1ce7d74SMarkus Armbruster void bdrv_image_info_dump(ImageInfo *info)
785f364ec65SWenchao Xia {
786de38b500SEric Blake     char *size_buf, *dsize_buf;
787f364ec65SWenchao Xia     if (!info->has_actual_size) {
788de38b500SEric Blake         dsize_buf = g_strdup("unavailable");
789f364ec65SWenchao Xia     } else {
790de38b500SEric Blake         dsize_buf = size_to_str(info->actual_size);
791f364ec65SWenchao Xia     }
792de38b500SEric Blake     size_buf = size_to_str(info->virtual_size);
793e1ce7d74SMarkus Armbruster     qemu_printf("image: %s\n"
794f364ec65SWenchao Xia                 "file format: %s\n"
795f364ec65SWenchao Xia                 "virtual size: %s (%" PRId64 " bytes)\n"
796f364ec65SWenchao Xia                 "disk size: %s\n",
797f364ec65SWenchao Xia                 info->filename, info->format, size_buf,
798f364ec65SWenchao Xia                 info->virtual_size,
799f364ec65SWenchao Xia                 dsize_buf);
800de38b500SEric Blake     g_free(size_buf);
801de38b500SEric Blake     g_free(dsize_buf);
802f364ec65SWenchao Xia 
803f364ec65SWenchao Xia     if (info->has_encrypted && info->encrypted) {
804e1ce7d74SMarkus Armbruster         qemu_printf("encrypted: yes\n");
805f364ec65SWenchao Xia     }
806f364ec65SWenchao Xia 
807f364ec65SWenchao Xia     if (info->has_cluster_size) {
808e1ce7d74SMarkus Armbruster         qemu_printf("cluster_size: %" PRId64 "\n",
8095b917044SWenchao Xia                     info->cluster_size);
810f364ec65SWenchao Xia     }
811f364ec65SWenchao Xia 
812f364ec65SWenchao Xia     if (info->has_dirty_flag && info->dirty_flag) {
813e1ce7d74SMarkus Armbruster         qemu_printf("cleanly shut down: no\n");
814f364ec65SWenchao Xia     }
815f364ec65SWenchao Xia 
816f364ec65SWenchao Xia     if (info->has_backing_filename) {
817e1ce7d74SMarkus Armbruster         qemu_printf("backing file: %s", info->backing_filename);
8185c9d9ca5SJohn Snow         if (!info->has_full_backing_filename) {
819e1ce7d74SMarkus Armbruster             qemu_printf(" (cannot determine actual path)");
8205c9d9ca5SJohn Snow         } else if (strcmp(info->backing_filename,
8215c9d9ca5SJohn Snow                           info->full_backing_filename) != 0) {
822e1ce7d74SMarkus Armbruster             qemu_printf(" (actual path: %s)", info->full_backing_filename);
823f364ec65SWenchao Xia         }
824e1ce7d74SMarkus Armbruster         qemu_printf("\n");
825f364ec65SWenchao Xia         if (info->has_backing_filename_format) {
826e1ce7d74SMarkus Armbruster             qemu_printf("backing file format: %s\n",
8275b917044SWenchao Xia                         info->backing_filename_format);
828f364ec65SWenchao Xia         }
829f364ec65SWenchao Xia     }
830f364ec65SWenchao Xia 
831f364ec65SWenchao Xia     if (info->has_snapshots) {
832f364ec65SWenchao Xia         SnapshotInfoList *elem;
833f364ec65SWenchao Xia 
834e1ce7d74SMarkus Armbruster         qemu_printf("Snapshot list:\n");
835e1ce7d74SMarkus Armbruster         bdrv_snapshot_dump(NULL);
836e1ce7d74SMarkus Armbruster         qemu_printf("\n");
837f364ec65SWenchao Xia 
838f364ec65SWenchao Xia         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
839f364ec65SWenchao Xia          * we convert to the block layer's native QEMUSnapshotInfo for now.
840f364ec65SWenchao Xia          */
841f364ec65SWenchao Xia         for (elem = info->snapshots; elem; elem = elem->next) {
842f364ec65SWenchao Xia             QEMUSnapshotInfo sn = {
843f364ec65SWenchao Xia                 .vm_state_size = elem->value->vm_state_size,
844f364ec65SWenchao Xia                 .date_sec = elem->value->date_sec,
845f364ec65SWenchao Xia                 .date_nsec = elem->value->date_nsec,
846f364ec65SWenchao Xia                 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
847f364ec65SWenchao Xia                                  elem->value->vm_clock_nsec,
848f364ec65SWenchao Xia             };
849f364ec65SWenchao Xia 
850f364ec65SWenchao Xia             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
851f364ec65SWenchao Xia             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
852e1ce7d74SMarkus Armbruster             bdrv_snapshot_dump(&sn);
853e1ce7d74SMarkus Armbruster             qemu_printf("\n");
854f364ec65SWenchao Xia         }
855f364ec65SWenchao Xia     }
856a8d8ecb7SMax Reitz 
857a8d8ecb7SMax Reitz     if (info->has_format_specific) {
858e1ce7d74SMarkus Armbruster         qemu_printf("Format specific information:\n");
859e1ce7d74SMarkus Armbruster         bdrv_image_info_specific_dump(info->format_specific);
860a8d8ecb7SMax Reitz     }
861f364ec65SWenchao Xia }
862