xref: /qemu/block/qapi.c (revision 159f85dd)
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,
45c83f9fbaSKevin Wolf                                         BlockDriverState *bs, Error **errp)
46c13163fbSBenoît Canet {
47d5a8ee60SAlberto Garcia     ImageInfo **p_image_info;
48d5a8ee60SAlberto Garcia     BlockDriverState *bs0;
49d470ad42SMax Reitz     BlockDeviceInfo *info;
50c13163fbSBenoît Canet 
51d470ad42SMax Reitz     if (!bs->drv) {
52d470ad42SMax Reitz         error_setg(errp, "Block device %s is ejected", bs->node_name);
53d470ad42SMax Reitz         return NULL;
54d470ad42SMax Reitz     }
55d470ad42SMax Reitz 
56f30c66baSMax Reitz     bdrv_refresh_filename(bs);
57f30c66baSMax Reitz 
58d470ad42SMax Reitz     info = g_malloc0(sizeof(*info));
59c13163fbSBenoît Canet     info->file                   = g_strdup(bs->filename);
60c13163fbSBenoît Canet     info->ro                     = bs->read_only;
61c13163fbSBenoît Canet     info->drv                    = g_strdup(bs->drv->format_name);
62c13163fbSBenoît Canet     info->encrypted              = bs->encrypted;
63c01c214bSDaniel P. Berrange     info->encryption_key_missing = false;
64c13163fbSBenoît Canet 
659e193c5aSKevin Wolf     info->cache = g_new(BlockdevCacheInfo, 1);
669e193c5aSKevin Wolf     *info->cache = (BlockdevCacheInfo) {
67c83f9fbaSKevin Wolf         .writeback      = blk ? blk_enable_write_cache(blk) : true,
689e193c5aSKevin Wolf         .direct         = !!(bs->open_flags & BDRV_O_NOCACHE),
699e193c5aSKevin Wolf         .no_flush       = !!(bs->open_flags & BDRV_O_NO_FLUSH),
709e193c5aSKevin Wolf     };
719e193c5aSKevin Wolf 
72c13163fbSBenoît Canet     if (bs->node_name[0]) {
73c13163fbSBenoît Canet         info->has_node_name = true;
74c13163fbSBenoît Canet         info->node_name = g_strdup(bs->node_name);
75c13163fbSBenoît Canet     }
76c13163fbSBenoît Canet 
77c13163fbSBenoît Canet     if (bs->backing_file[0]) {
78c13163fbSBenoît Canet         info->has_backing_file = true;
79c13163fbSBenoît Canet         info->backing_file = g_strdup(bs->backing_file);
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 
140b8fe1694SAlberto Garcia         info->has_group = true;
141022cdc9fSManos Pitsidianakis         info->group =
142022cdc9fSManos Pitsidianakis             g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
143c13163fbSBenoît Canet     }
144c13163fbSBenoît Canet 
145e2462113SFrancesco Romani     info->write_threshold = bdrv_write_threshold_get(bs);
146e2462113SFrancesco Romani 
147d5a8ee60SAlberto Garcia     bs0 = bs;
148d5a8ee60SAlberto Garcia     p_image_info = &info->image;
149d3c8c674SKevin Wolf     info->backing_file_depth = 0;
150d5a8ee60SAlberto Garcia     while (1) {
151d5a8ee60SAlberto Garcia         Error *local_err = NULL;
152d5a8ee60SAlberto Garcia         bdrv_query_image_info(bs0, p_image_info, &local_err);
153d5a8ee60SAlberto Garcia         if (local_err) {
154d5a8ee60SAlberto Garcia             error_propagate(errp, local_err);
155d5a8ee60SAlberto Garcia             qapi_free_BlockDeviceInfo(info);
156d5a8ee60SAlberto Garcia             return NULL;
157d5a8ee60SAlberto Garcia         }
158d3c8c674SKevin Wolf 
159760e0063SKevin Wolf         if (bs0->drv && bs0->backing) {
160d3c8c674SKevin Wolf             info->backing_file_depth++;
161760e0063SKevin Wolf             bs0 = bs0->backing->bs;
162d5a8ee60SAlberto Garcia             (*p_image_info)->has_backing_image = true;
163d5a8ee60SAlberto Garcia             p_image_info = &((*p_image_info)->backing_image);
164d5a8ee60SAlberto Garcia         } else {
165d5a8ee60SAlberto Garcia             break;
166d5a8ee60SAlberto Garcia         }
167d3c8c674SKevin Wolf 
168d3c8c674SKevin Wolf         /* Skip automatically inserted nodes that the user isn't aware of for
169d3c8c674SKevin Wolf          * query-block (blk != NULL), but not for query-named-block-nodes */
1708e8eb0a9SKevin Wolf         while (blk && bs0->drv && bs0->implicit) {
171d3c8c674SKevin Wolf             bs0 = backing_bs(bs0);
1728e8eb0a9SKevin Wolf             assert(bs0);
173d3c8c674SKevin Wolf         }
174d5a8ee60SAlberto Garcia     }
175d5a8ee60SAlberto Garcia 
176c13163fbSBenoît Canet     return info;
177c13163fbSBenoît Canet }
178c13163fbSBenoît Canet 
179fb0ed453SWenchao Xia /*
180fb0ed453SWenchao Xia  * Returns 0 on success, with *p_list either set to describe snapshot
181fb0ed453SWenchao Xia  * information, or NULL because there are no snapshots.  Returns -errno on
182fb0ed453SWenchao Xia  * error, with *p_list untouched.
183fb0ed453SWenchao Xia  */
184fb0ed453SWenchao Xia int bdrv_query_snapshot_info_list(BlockDriverState *bs,
185fb0ed453SWenchao Xia                                   SnapshotInfoList **p_list,
186fb0ed453SWenchao Xia                                   Error **errp)
187f364ec65SWenchao Xia {
188f364ec65SWenchao Xia     int i, sn_count;
189f364ec65SWenchao Xia     QEMUSnapshotInfo *sn_tab = NULL;
190fb0ed453SWenchao Xia     SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
191fb0ed453SWenchao Xia     SnapshotInfo *info;
192fb0ed453SWenchao Xia 
193f364ec65SWenchao Xia     sn_count = bdrv_snapshot_list(bs, &sn_tab);
194fb0ed453SWenchao Xia     if (sn_count < 0) {
195fb0ed453SWenchao Xia         const char *dev = bdrv_get_device_name(bs);
196fb0ed453SWenchao Xia         switch (sn_count) {
197fb0ed453SWenchao Xia         case -ENOMEDIUM:
198fb0ed453SWenchao Xia             error_setg(errp, "Device '%s' is not inserted", dev);
199fb0ed453SWenchao Xia             break;
200fb0ed453SWenchao Xia         case -ENOTSUP:
201fb0ed453SWenchao Xia             error_setg(errp,
202fb0ed453SWenchao Xia                        "Device '%s' does not support internal snapshots",
203fb0ed453SWenchao Xia                        dev);
204fb0ed453SWenchao Xia             break;
205fb0ed453SWenchao Xia         default:
206fb0ed453SWenchao Xia             error_setg_errno(errp, -sn_count,
207fb0ed453SWenchao Xia                              "Can't list snapshots of device '%s'", dev);
208fb0ed453SWenchao Xia             break;
209fb0ed453SWenchao Xia         }
210fb0ed453SWenchao Xia         return sn_count;
211fb0ed453SWenchao Xia     }
212f364ec65SWenchao Xia 
213f364ec65SWenchao Xia     for (i = 0; i < sn_count; i++) {
214fb0ed453SWenchao Xia         info = g_new0(SnapshotInfo, 1);
215fb0ed453SWenchao Xia         info->id            = g_strdup(sn_tab[i].id_str);
216fb0ed453SWenchao Xia         info->name          = g_strdup(sn_tab[i].name);
217fb0ed453SWenchao Xia         info->vm_state_size = sn_tab[i].vm_state_size;
218fb0ed453SWenchao Xia         info->date_sec      = sn_tab[i].date_sec;
219fb0ed453SWenchao Xia         info->date_nsec     = sn_tab[i].date_nsec;
220fb0ed453SWenchao Xia         info->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
221fb0ed453SWenchao Xia         info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
222f364ec65SWenchao Xia 
223fb0ed453SWenchao Xia         info_list = g_new0(SnapshotInfoList, 1);
224fb0ed453SWenchao Xia         info_list->value = info;
225f364ec65SWenchao Xia 
226f364ec65SWenchao Xia         /* XXX: waiting for the qapi to support qemu-queue.h types */
227f364ec65SWenchao Xia         if (!cur_item) {
228fb0ed453SWenchao Xia             head = cur_item = info_list;
229f364ec65SWenchao Xia         } else {
230f364ec65SWenchao Xia             cur_item->next = info_list;
231f364ec65SWenchao Xia             cur_item = info_list;
232f364ec65SWenchao Xia         }
233f364ec65SWenchao Xia 
234f364ec65SWenchao Xia     }
235f364ec65SWenchao Xia 
236f364ec65SWenchao Xia     g_free(sn_tab);
237fb0ed453SWenchao Xia     *p_list = head;
238fb0ed453SWenchao Xia     return 0;
239f364ec65SWenchao Xia }
240f364ec65SWenchao Xia 
24143526ec8SWenchao Xia /**
24243526ec8SWenchao Xia  * bdrv_query_image_info:
24343526ec8SWenchao Xia  * @bs: block device to examine
24443526ec8SWenchao Xia  * @p_info: location to store image information
24543526ec8SWenchao Xia  * @errp: location to store error information
24643526ec8SWenchao Xia  *
247553a7e87SWenchao Xia  * Store "flat" image information in @p_info.
248553a7e87SWenchao Xia  *
249553a7e87SWenchao Xia  * "Flat" means it does *not* query backing image information,
250553a7e87SWenchao Xia  * i.e. (*pinfo)->has_backing_image will be set to false and
251553a7e87SWenchao Xia  * (*pinfo)->backing_image to NULL even when the image does in fact have
252553a7e87SWenchao Xia  * a backing image.
253553a7e87SWenchao Xia  *
25443526ec8SWenchao Xia  * @p_info will be set only on success. On error, store error in @errp.
25543526ec8SWenchao Xia  */
25643526ec8SWenchao Xia void bdrv_query_image_info(BlockDriverState *bs,
25743526ec8SWenchao Xia                            ImageInfo **p_info,
25843526ec8SWenchao Xia                            Error **errp)
259f364ec65SWenchao Xia {
26052bf1e72SMarkus Armbruster     int64_t size;
26143526ec8SWenchao Xia     const char *backing_filename;
262f364ec65SWenchao Xia     BlockDriverInfo bdi;
26343526ec8SWenchao Xia     int ret;
26443526ec8SWenchao Xia     Error *err = NULL;
26552bf1e72SMarkus Armbruster     ImageInfo *info;
266f364ec65SWenchao Xia 
2671963f8d5SPaolo Bonzini     aio_context_acquire(bdrv_get_aio_context(bs));
2681963f8d5SPaolo Bonzini 
26952bf1e72SMarkus Armbruster     size = bdrv_getlength(bs);
27052bf1e72SMarkus Armbruster     if (size < 0) {
2719adceb02SFam Zheng         error_setg_errno(errp, -size, "Can't get image size '%s'",
2729adceb02SFam Zheng                          bs->exact_filename);
2731963f8d5SPaolo Bonzini         goto out;
27452bf1e72SMarkus Armbruster     }
275f364ec65SWenchao Xia 
276f30c66baSMax Reitz     bdrv_refresh_filename(bs);
277f30c66baSMax Reitz 
27852bf1e72SMarkus Armbruster     info = g_new0(ImageInfo, 1);
27943526ec8SWenchao Xia     info->filename        = g_strdup(bs->filename);
280f364ec65SWenchao Xia     info->format          = g_strdup(bdrv_get_format_name(bs));
28152bf1e72SMarkus Armbruster     info->virtual_size    = size;
282f364ec65SWenchao Xia     info->actual_size     = bdrv_get_allocated_file_size(bs);
283f364ec65SWenchao Xia     info->has_actual_size = info->actual_size >= 0;
284f364ec65SWenchao Xia     if (bdrv_is_encrypted(bs)) {
285f364ec65SWenchao Xia         info->encrypted = true;
286f364ec65SWenchao Xia         info->has_encrypted = true;
287f364ec65SWenchao Xia     }
288f364ec65SWenchao Xia     if (bdrv_get_info(bs, &bdi) >= 0) {
289f364ec65SWenchao Xia         if (bdi.cluster_size != 0) {
290f364ec65SWenchao Xia             info->cluster_size = bdi.cluster_size;
291f364ec65SWenchao Xia             info->has_cluster_size = true;
292f364ec65SWenchao Xia         }
293f364ec65SWenchao Xia         info->dirty_flag = bdi.is_dirty;
294f364ec65SWenchao Xia         info->has_dirty_flag = true;
295f364ec65SWenchao Xia     }
2961bf6e9caSAndrey Shinkevich     info->format_specific = bdrv_get_specific_info(bs, &err);
2971bf6e9caSAndrey Shinkevich     if (err) {
2981bf6e9caSAndrey Shinkevich         error_propagate(errp, err);
2991bf6e9caSAndrey Shinkevich         qapi_free_ImageInfo(info);
3001bf6e9caSAndrey Shinkevich         goto out;
3011bf6e9caSAndrey Shinkevich     }
302eae041feSMax Reitz     info->has_format_specific = info->format_specific != NULL;
303eae041feSMax Reitz 
30443526ec8SWenchao Xia     backing_filename = bs->backing_file;
305f364ec65SWenchao Xia     if (backing_filename[0] != '\0') {
3066b6833c1SMax Reitz         char *backing_filename2;
307f364ec65SWenchao Xia         info->backing_filename = g_strdup(backing_filename);
308f364ec65SWenchao Xia         info->has_backing_filename = true;
3096b6833c1SMax Reitz         backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
310f364ec65SWenchao Xia 
31112dcb1c0SJohn Snow         /* Always report the full_backing_filename if present, even if it's the
31212dcb1c0SJohn Snow          * same as backing_filename. That they are same is useful info. */
31312dcb1c0SJohn Snow         if (backing_filename2) {
31412dcb1c0SJohn Snow             info->full_backing_filename = g_strdup(backing_filename2);
315f364ec65SWenchao Xia             info->has_full_backing_filename = true;
316f364ec65SWenchao Xia         }
317f364ec65SWenchao Xia 
318f364ec65SWenchao Xia         if (bs->backing_format[0]) {
319f364ec65SWenchao Xia             info->backing_filename_format = g_strdup(bs->backing_format);
320f364ec65SWenchao Xia             info->has_backing_filename_format = true;
321f364ec65SWenchao Xia         }
322564d64bdSJeff Cody         g_free(backing_filename2);
323f364ec65SWenchao Xia     }
32443526ec8SWenchao Xia 
32543526ec8SWenchao Xia     ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
32643526ec8SWenchao Xia     switch (ret) {
32743526ec8SWenchao Xia     case 0:
32843526ec8SWenchao Xia         if (info->snapshots) {
32943526ec8SWenchao Xia             info->has_snapshots = true;
33043526ec8SWenchao Xia         }
33143526ec8SWenchao Xia         break;
33243526ec8SWenchao Xia     /* recoverable error */
33343526ec8SWenchao Xia     case -ENOMEDIUM:
33443526ec8SWenchao Xia     case -ENOTSUP:
33543526ec8SWenchao Xia         error_free(err);
33643526ec8SWenchao Xia         break;
33743526ec8SWenchao Xia     default:
33843526ec8SWenchao Xia         error_propagate(errp, err);
33943526ec8SWenchao Xia         qapi_free_ImageInfo(info);
3401963f8d5SPaolo Bonzini         goto out;
34143526ec8SWenchao Xia     }
34243526ec8SWenchao Xia 
34343526ec8SWenchao Xia     *p_info = info;
3441963f8d5SPaolo Bonzini 
3451963f8d5SPaolo Bonzini out:
3461963f8d5SPaolo Bonzini     aio_context_release(bdrv_get_aio_context(bs));
347f364ec65SWenchao Xia }
348f364ec65SWenchao Xia 
349553a7e87SWenchao Xia /* @p_info will be set only on success. */
350d829a211SMarkus Armbruster static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
351553a7e87SWenchao Xia                             Error **errp)
352f364ec65SWenchao Xia {
353f364ec65SWenchao Xia     BlockInfo *info = g_malloc0(sizeof(*info));
354d829a211SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
35546eade7bSKevin Wolf     char *qdev;
35646eade7bSKevin Wolf 
357d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of */
358d3c8c674SKevin Wolf     while (bs && bs->drv && bs->implicit) {
359d3c8c674SKevin Wolf         bs = backing_bs(bs);
360d3c8c674SKevin Wolf     }
361d3c8c674SKevin Wolf 
362d829a211SMarkus Armbruster     info->device = g_strdup(blk_name(blk));
363f364ec65SWenchao Xia     info->type = g_strdup("unknown");
364a7f53e26SMarkus Armbruster     info->locked = blk_dev_is_medium_locked(blk);
365a7f53e26SMarkus Armbruster     info->removable = blk_dev_has_removable_media(blk);
366f364ec65SWenchao Xia 
36746eade7bSKevin Wolf     qdev = blk_get_attached_dev_id(blk);
36846eade7bSKevin Wolf     if (qdev && *qdev) {
36946eade7bSKevin Wolf         info->has_qdev = true;
37046eade7bSKevin Wolf         info->qdev = qdev;
37146eade7bSKevin Wolf     } else {
37246eade7bSKevin Wolf         g_free(qdev);
37346eade7bSKevin Wolf     }
37446eade7bSKevin Wolf 
375327032ceSMax Reitz     if (blk_dev_has_tray(blk)) {
376f364ec65SWenchao Xia         info->has_tray_open = true;
377a7f53e26SMarkus Armbruster         info->tray_open = blk_dev_is_tray_open(blk);
378f364ec65SWenchao Xia     }
379f364ec65SWenchao Xia 
380373340b2SMax Reitz     if (blk_iostatus_is_enabled(blk)) {
381f364ec65SWenchao Xia         info->has_io_status = true;
382373340b2SMax Reitz         info->io_status = blk_iostatus(blk);
383f364ec65SWenchao Xia     }
384f364ec65SWenchao Xia 
3855433c24fSMax Reitz     if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
38621b56835SFam Zheng         info->has_dirty_bitmaps = true;
38721b56835SFam Zheng         info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
38821b56835SFam Zheng     }
38921b56835SFam Zheng 
3905433c24fSMax Reitz     if (bs && bs->drv) {
391f364ec65SWenchao Xia         info->has_inserted = true;
392c83f9fbaSKevin Wolf         info->inserted = bdrv_block_device_info(blk, bs, errp);
393d5a8ee60SAlberto Garcia         if (info->inserted == NULL) {
394553a7e87SWenchao Xia             goto err;
395f364ec65SWenchao Xia         }
396553a7e87SWenchao Xia     }
397553a7e87SWenchao Xia 
398553a7e87SWenchao Xia     *p_info = info;
399553a7e87SWenchao Xia     return;
400553a7e87SWenchao Xia 
401553a7e87SWenchao Xia  err:
402553a7e87SWenchao Xia     qapi_free_BlockInfo(info);
403f364ec65SWenchao Xia }
404f364ec65SWenchao Xia 
4057e5c776dSVladimir Sementsov-Ogievskiy static uint64List *uint64_list(uint64_t *list, int size)
4067e5c776dSVladimir Sementsov-Ogievskiy {
4077e5c776dSVladimir Sementsov-Ogievskiy     int i;
4087e5c776dSVladimir Sementsov-Ogievskiy     uint64List *out_list = NULL;
4097e5c776dSVladimir Sementsov-Ogievskiy     uint64List **pout_list = &out_list;
4107e5c776dSVladimir Sementsov-Ogievskiy 
4117e5c776dSVladimir Sementsov-Ogievskiy     for (i = 0; i < size; i++) {
4127e5c776dSVladimir Sementsov-Ogievskiy         uint64List *entry = g_new(uint64List, 1);
4137e5c776dSVladimir Sementsov-Ogievskiy         entry->value = list[i];
4147e5c776dSVladimir Sementsov-Ogievskiy         *pout_list = entry;
4157e5c776dSVladimir Sementsov-Ogievskiy         pout_list = &entry->next;
4167e5c776dSVladimir Sementsov-Ogievskiy     }
4177e5c776dSVladimir Sementsov-Ogievskiy 
4187e5c776dSVladimir Sementsov-Ogievskiy     *pout_list = NULL;
4197e5c776dSVladimir Sementsov-Ogievskiy 
4207e5c776dSVladimir Sementsov-Ogievskiy     return out_list;
4217e5c776dSVladimir Sementsov-Ogievskiy }
4227e5c776dSVladimir Sementsov-Ogievskiy 
4237e5c776dSVladimir Sementsov-Ogievskiy static void bdrv_latency_histogram_stats(BlockLatencyHistogram *hist,
4247e5c776dSVladimir Sementsov-Ogievskiy                                          bool *not_null,
4257e5c776dSVladimir Sementsov-Ogievskiy                                          BlockLatencyHistogramInfo **info)
4267e5c776dSVladimir Sementsov-Ogievskiy {
4277e5c776dSVladimir Sementsov-Ogievskiy     *not_null = hist->bins != NULL;
4287e5c776dSVladimir Sementsov-Ogievskiy     if (*not_null) {
4297e5c776dSVladimir Sementsov-Ogievskiy         *info = g_new0(BlockLatencyHistogramInfo, 1);
4307e5c776dSVladimir Sementsov-Ogievskiy 
4317e5c776dSVladimir Sementsov-Ogievskiy         (*info)->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
4327e5c776dSVladimir Sementsov-Ogievskiy         (*info)->bins = uint64_list(hist->bins, hist->nbins);
4337e5c776dSVladimir Sementsov-Ogievskiy     }
4347e5c776dSVladimir Sementsov-Ogievskiy }
4357e5c776dSVladimir Sementsov-Ogievskiy 
43654302156SMax Reitz static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
437f364ec65SWenchao Xia {
4382b77e60aSKevin Wolf     BlockAcctStats *stats = blk_get_stats(blk);
439979e9b03SAlberto Garcia     BlockAcctTimedStats *ts = NULL;
4407f0e9da6SMax Reitz 
44154302156SMax Reitz     ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
44254302156SMax Reitz     ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
443*159f85ddSAnton Nefedov     ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
44454302156SMax Reitz     ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
44554302156SMax Reitz     ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
446*159f85ddSAnton Nefedov     ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
4477ee12dafSAlberto Garcia 
44854302156SMax Reitz     ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
44954302156SMax Reitz     ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
45054302156SMax Reitz     ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
451*159f85ddSAnton Nefedov     ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
4527ee12dafSAlberto Garcia 
45354302156SMax Reitz     ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
45454302156SMax Reitz     ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
45554302156SMax Reitz     ds->invalid_flush_operations =
4567ee12dafSAlberto Garcia         stats->invalid_ops[BLOCK_ACCT_FLUSH];
457*159f85ddSAnton Nefedov     ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
4587ee12dafSAlberto Garcia 
45954302156SMax Reitz     ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
46054302156SMax Reitz     ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
461*159f85ddSAnton Nefedov     ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
46254302156SMax Reitz     ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
46354302156SMax Reitz     ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
46454302156SMax Reitz     ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
46554302156SMax Reitz     ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
466*159f85ddSAnton Nefedov     ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
467cb38fffbSAlberto Garcia 
46854302156SMax Reitz     ds->has_idle_time_ns = stats->last_access_time_ns > 0;
46954302156SMax Reitz     if (ds->has_idle_time_ns) {
47054302156SMax Reitz         ds->idle_time_ns = block_acct_idle_time_ns(stats);
471cb38fffbSAlberto Garcia     }
472362e9299SAlberto Garcia 
47354302156SMax Reitz     ds->account_invalid = stats->account_invalid;
47454302156SMax Reitz     ds->account_failed = stats->account_failed;
475979e9b03SAlberto Garcia 
476979e9b03SAlberto Garcia     while ((ts = block_acct_interval_next(stats, ts))) {
477979e9b03SAlberto Garcia         BlockDeviceTimedStatsList *timed_stats =
478979e9b03SAlberto Garcia             g_malloc0(sizeof(*timed_stats));
479979e9b03SAlberto Garcia         BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
48054302156SMax Reitz         timed_stats->next = ds->timed_stats;
481979e9b03SAlberto Garcia         timed_stats->value = dev_stats;
48254302156SMax Reitz         ds->timed_stats = timed_stats;
483979e9b03SAlberto Garcia 
484979e9b03SAlberto Garcia         TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
485979e9b03SAlberto Garcia         TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
486979e9b03SAlberto Garcia         TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
487979e9b03SAlberto Garcia 
488979e9b03SAlberto Garcia         dev_stats->interval_length = ts->interval_length;
489979e9b03SAlberto Garcia 
490979e9b03SAlberto Garcia         dev_stats->min_rd_latency_ns = timed_average_min(rd);
491979e9b03SAlberto Garcia         dev_stats->max_rd_latency_ns = timed_average_max(rd);
492979e9b03SAlberto Garcia         dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
493979e9b03SAlberto Garcia 
494979e9b03SAlberto Garcia         dev_stats->min_wr_latency_ns = timed_average_min(wr);
495979e9b03SAlberto Garcia         dev_stats->max_wr_latency_ns = timed_average_max(wr);
496979e9b03SAlberto Garcia         dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
497979e9b03SAlberto Garcia 
498979e9b03SAlberto Garcia         dev_stats->min_flush_latency_ns = timed_average_min(fl);
499979e9b03SAlberto Garcia         dev_stats->max_flush_latency_ns = timed_average_max(fl);
500979e9b03SAlberto Garcia         dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
50196e4dedaSAlberto Garcia 
50296e4dedaSAlberto Garcia         dev_stats->avg_rd_queue_depth =
50396e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_READ);
50496e4dedaSAlberto Garcia         dev_stats->avg_wr_queue_depth =
50596e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
506979e9b03SAlberto Garcia     }
5077e5c776dSVladimir Sementsov-Ogievskiy 
5087e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_READ],
509cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->has_rd_latency_histogram,
510cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->rd_latency_histogram);
5117e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_WRITE],
512cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->has_wr_latency_histogram,
513cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->wr_latency_histogram);
5147e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_FLUSH],
515cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->has_flush_latency_histogram,
516cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->flush_latency_histogram);
5177f0e9da6SMax Reitz }
518f364ec65SWenchao Xia 
519d3c8c674SKevin Wolf static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
520d3c8c674SKevin Wolf                                         bool blk_level)
5212b77e60aSKevin Wolf {
52220a6d768SDou Liyang     BlockStats *s = NULL;
52320a6d768SDou Liyang 
52420a6d768SDou Liyang     s = g_malloc0(sizeof(*s));
52520a6d768SDou Liyang     s->stats = g_malloc0(sizeof(*s->stats));
52620a6d768SDou Liyang 
52720a6d768SDou Liyang     if (!bs) {
52820a6d768SDou Liyang         return s;
52920a6d768SDou Liyang     }
53020a6d768SDou Liyang 
531d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of in
532d3c8c674SKevin Wolf      * a BlockBackend-level command. Stay at the exact node for a node-level
533d3c8c674SKevin Wolf      * command. */
534d3c8c674SKevin Wolf     while (blk_level && bs->drv && bs->implicit) {
535d3c8c674SKevin Wolf         bs = backing_bs(bs);
536d3c8c674SKevin Wolf         assert(bs);
537d3c8c674SKevin Wolf     }
538d3c8c674SKevin Wolf 
5392b77e60aSKevin Wolf     if (bdrv_get_node_name(bs)[0]) {
5402b77e60aSKevin Wolf         s->has_node_name = true;
5412b77e60aSKevin Wolf         s->node_name = g_strdup(bdrv_get_node_name(bs));
5422b77e60aSKevin Wolf     }
5432b77e60aSKevin Wolf 
544f7946da2SPaolo Bonzini     s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
54553d8f9d8SMax Reitz 
546f364ec65SWenchao Xia     if (bs->file) {
547f364ec65SWenchao Xia         s->has_parent = true;
548d3c8c674SKevin Wolf         s->parent = bdrv_query_bds_stats(bs->file->bs, blk_level);
549f364ec65SWenchao Xia     }
550f364ec65SWenchao Xia 
551d3c8c674SKevin Wolf     if (blk_level && bs->backing) {
552c8059b97SFam Zheng         s->has_backing = true;
553d3c8c674SKevin Wolf         s->backing = bdrv_query_bds_stats(bs->backing->bs, blk_level);
554c8059b97SFam Zheng     }
555c8059b97SFam Zheng 
55620a6d768SDou Liyang     return s;
557b07363a1SKevin Wolf }
558b07363a1SKevin Wolf 
559f364ec65SWenchao Xia BlockInfoList *qmp_query_block(Error **errp)
560f364ec65SWenchao Xia {
561f364ec65SWenchao Xia     BlockInfoList *head = NULL, **p_next = &head;
562d829a211SMarkus Armbruster     BlockBackend *blk;
563553a7e87SWenchao Xia     Error *local_err = NULL;
564f364ec65SWenchao Xia 
565d5b68844SKevin Wolf     for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
566d5b68844SKevin Wolf         BlockInfoList *info;
567d5b68844SKevin Wolf 
568ec18b0a9SKevin Wolf         if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
569d5b68844SKevin Wolf             continue;
570d5b68844SKevin Wolf         }
571d5b68844SKevin Wolf 
572d5b68844SKevin Wolf         info = g_malloc0(sizeof(*info));
573d829a211SMarkus Armbruster         bdrv_query_info(blk, &info->value, &local_err);
57484d18f06SMarkus Armbruster         if (local_err) {
575553a7e87SWenchao Xia             error_propagate(errp, local_err);
576903c341dSMarkus Armbruster             g_free(info);
577903c341dSMarkus Armbruster             qapi_free_BlockInfoList(head);
578903c341dSMarkus Armbruster             return NULL;
579553a7e87SWenchao Xia         }
580f364ec65SWenchao Xia 
581f364ec65SWenchao Xia         *p_next = info;
582f364ec65SWenchao Xia         p_next = &info->next;
583f364ec65SWenchao Xia     }
584f364ec65SWenchao Xia 
585f364ec65SWenchao Xia     return head;
586f364ec65SWenchao Xia }
587f364ec65SWenchao Xia 
588f71eaa74SFam Zheng BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
589f71eaa74SFam Zheng                                      bool query_nodes,
590f71eaa74SFam Zheng                                      Error **errp)
591f364ec65SWenchao Xia {
592f364ec65SWenchao Xia     BlockStatsList *head = NULL, **p_next = &head;
593a6baa608SDou Liyang     BlockBackend *blk;
594a6baa608SDou Liyang     BlockDriverState *bs;
595f364ec65SWenchao Xia 
596f71eaa74SFam Zheng     /* Just to be safe if query_nodes is not always initialized */
597a6baa608SDou Liyang     if (has_query_nodes && query_nodes) {
598a6baa608SDou Liyang         for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
599f364ec65SWenchao Xia             BlockStatsList *info = g_malloc0(sizeof(*info));
600a6baa608SDou Liyang             AioContext *ctx = bdrv_get_aio_context(bs);
60113344f3aSStefan Hajnoczi 
60213344f3aSStefan Hajnoczi             aio_context_acquire(ctx);
603a6baa608SDou Liyang             info->value = bdrv_query_bds_stats(bs, false);
60413344f3aSStefan Hajnoczi             aio_context_release(ctx);
605f364ec65SWenchao Xia 
606f364ec65SWenchao Xia             *p_next = info;
607f364ec65SWenchao Xia             p_next = &info->next;
608f364ec65SWenchao Xia         }
609a6baa608SDou Liyang     } else {
610567dcb31SKevin Wolf         for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
611f62492bbSKevin Wolf             BlockStatsList *info;
612a6baa608SDou Liyang             AioContext *ctx = blk_get_aio_context(blk);
613a6baa608SDou Liyang             BlockStats *s;
6145a9cb5a9SKevin Wolf             char *qdev;
615a6baa608SDou Liyang 
616567dcb31SKevin Wolf             if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
617567dcb31SKevin Wolf                 continue;
618567dcb31SKevin Wolf             }
619567dcb31SKevin Wolf 
620a6baa608SDou Liyang             aio_context_acquire(ctx);
621a6baa608SDou Liyang             s = bdrv_query_bds_stats(blk_bs(blk), true);
622a6baa608SDou Liyang             s->has_device = true;
623a6baa608SDou Liyang             s->device = g_strdup(blk_name(blk));
6245a9cb5a9SKevin Wolf 
6255a9cb5a9SKevin Wolf             qdev = blk_get_attached_dev_id(blk);
6265a9cb5a9SKevin Wolf             if (qdev && *qdev) {
6275a9cb5a9SKevin Wolf                 s->has_qdev = true;
6285a9cb5a9SKevin Wolf                 s->qdev = qdev;
6295a9cb5a9SKevin Wolf             } else {
6305a9cb5a9SKevin Wolf                 g_free(qdev);
6315a9cb5a9SKevin Wolf             }
6325a9cb5a9SKevin Wolf 
633a6baa608SDou Liyang             bdrv_query_blk_stats(s->stats, blk);
634a6baa608SDou Liyang             aio_context_release(ctx);
635a6baa608SDou Liyang 
636f62492bbSKevin Wolf             info = g_malloc0(sizeof(*info));
637a6baa608SDou Liyang             info->value = s;
638a6baa608SDou Liyang             *p_next = info;
639a6baa608SDou Liyang             p_next = &info->next;
640a6baa608SDou Liyang         }
641a6baa608SDou Liyang     }
642f364ec65SWenchao Xia 
643f364ec65SWenchao Xia     return head;
644f364ec65SWenchao Xia }
645f364ec65SWenchao Xia 
646e1ce7d74SMarkus Armbruster void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
647f364ec65SWenchao Xia {
648de38b500SEric Blake     char date_buf[128], clock_buf[128];
649f364ec65SWenchao Xia     struct tm tm;
650f364ec65SWenchao Xia     time_t ti;
651f364ec65SWenchao Xia     int64_t secs;
652de38b500SEric Blake     char *sizing = NULL;
653f364ec65SWenchao Xia 
654f364ec65SWenchao Xia     if (!sn) {
655e1ce7d74SMarkus Armbruster         qemu_printf("%-10s%-20s%7s%20s%15s",
656f364ec65SWenchao Xia                     "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
657f364ec65SWenchao Xia     } else {
658f364ec65SWenchao Xia         ti = sn->date_sec;
659f364ec65SWenchao Xia         localtime_r(&ti, &tm);
660f364ec65SWenchao Xia         strftime(date_buf, sizeof(date_buf),
661f364ec65SWenchao Xia                  "%Y-%m-%d %H:%M:%S", &tm);
662f364ec65SWenchao Xia         secs = sn->vm_clock_nsec / 1000000000;
663f364ec65SWenchao Xia         snprintf(clock_buf, sizeof(clock_buf),
664f364ec65SWenchao Xia                  "%02d:%02d:%02d.%03d",
665f364ec65SWenchao Xia                  (int)(secs / 3600),
666f364ec65SWenchao Xia                  (int)((secs / 60) % 60),
667f364ec65SWenchao Xia                  (int)(secs % 60),
668f364ec65SWenchao Xia                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
669de38b500SEric Blake         sizing = size_to_str(sn->vm_state_size);
670e1ce7d74SMarkus Armbruster         qemu_printf("%-10s%-20s%7s%20s%15s",
671f364ec65SWenchao Xia                     sn->id_str, sn->name,
672de38b500SEric Blake                     sizing,
673f364ec65SWenchao Xia                     date_buf,
674f364ec65SWenchao Xia                     clock_buf);
675f364ec65SWenchao Xia     }
676de38b500SEric Blake     g_free(sizing);
677f364ec65SWenchao Xia }
678f364ec65SWenchao Xia 
679e1ce7d74SMarkus Armbruster static void dump_qdict(int indentation, QDict *dict);
680e1ce7d74SMarkus Armbruster static void dump_qlist(int indentation, QList *list);
681a8d8ecb7SMax Reitz 
682e1ce7d74SMarkus Armbruster static void dump_qobject(int comp_indent, QObject *obj)
683a8d8ecb7SMax Reitz {
684a8d8ecb7SMax Reitz     switch (qobject_type(obj)) {
68501b2ffceSMarc-André Lureau         case QTYPE_QNUM: {
6867dc847ebSMax Reitz             QNum *value = qobject_to(QNum, obj);
68701b2ffceSMarc-André Lureau             char *tmp = qnum_to_string(value);
688e1ce7d74SMarkus Armbruster             qemu_printf("%s", tmp);
68901b2ffceSMarc-André Lureau             g_free(tmp);
690a8d8ecb7SMax Reitz             break;
691a8d8ecb7SMax Reitz         }
692a8d8ecb7SMax Reitz         case QTYPE_QSTRING: {
6937dc847ebSMax Reitz             QString *value = qobject_to(QString, obj);
694e1ce7d74SMarkus Armbruster             qemu_printf("%s", qstring_get_str(value));
695a8d8ecb7SMax Reitz             break;
696a8d8ecb7SMax Reitz         }
697a8d8ecb7SMax Reitz         case QTYPE_QDICT: {
6987dc847ebSMax Reitz             QDict *value = qobject_to(QDict, obj);
699e1ce7d74SMarkus Armbruster             dump_qdict(comp_indent, value);
700a8d8ecb7SMax Reitz             break;
701a8d8ecb7SMax Reitz         }
702a8d8ecb7SMax Reitz         case QTYPE_QLIST: {
7037dc847ebSMax Reitz             QList *value = qobject_to(QList, obj);
704e1ce7d74SMarkus Armbruster             dump_qlist(comp_indent, value);
705a8d8ecb7SMax Reitz             break;
706a8d8ecb7SMax Reitz         }
707a8d8ecb7SMax Reitz         case QTYPE_QBOOL: {
7087dc847ebSMax Reitz             QBool *value = qobject_to(QBool, obj);
709e1ce7d74SMarkus Armbruster             qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
710a8d8ecb7SMax Reitz             break;
711a8d8ecb7SMax Reitz         }
712a8d8ecb7SMax Reitz         default:
713a8d8ecb7SMax Reitz             abort();
714a8d8ecb7SMax Reitz     }
715a8d8ecb7SMax Reitz }
716a8d8ecb7SMax Reitz 
717e1ce7d74SMarkus Armbruster static void dump_qlist(int indentation, QList *list)
718a8d8ecb7SMax Reitz {
719a8d8ecb7SMax Reitz     const QListEntry *entry;
720a8d8ecb7SMax Reitz     int i = 0;
721a8d8ecb7SMax Reitz 
722a8d8ecb7SMax Reitz     for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
7231310a3d3SEric Blake         QType type = qobject_type(entry->value);
724a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
725e1ce7d74SMarkus Armbruster         qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
726853ccfedSPeter Xu                     composite ? '\n' : ' ');
727e1ce7d74SMarkus Armbruster         dump_qobject(indentation + 1, entry->value);
728a8d8ecb7SMax Reitz         if (!composite) {
729e1ce7d74SMarkus Armbruster             qemu_printf("\n");
730a8d8ecb7SMax Reitz         }
731a8d8ecb7SMax Reitz     }
732a8d8ecb7SMax Reitz }
733a8d8ecb7SMax Reitz 
734e1ce7d74SMarkus Armbruster static void dump_qdict(int indentation, QDict *dict)
735a8d8ecb7SMax Reitz {
736a8d8ecb7SMax Reitz     const QDictEntry *entry;
737a8d8ecb7SMax Reitz 
738a8d8ecb7SMax Reitz     for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
7391310a3d3SEric Blake         QType type = qobject_type(entry->value);
740a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
7415eda6227SPeter Xu         char *key = g_malloc(strlen(entry->key) + 1);
742a8d8ecb7SMax Reitz         int i;
743a8d8ecb7SMax Reitz 
744a8d8ecb7SMax Reitz         /* replace dashes with spaces in key (variable) names */
745a8d8ecb7SMax Reitz         for (i = 0; entry->key[i]; i++) {
746a8d8ecb7SMax Reitz             key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
747a8d8ecb7SMax Reitz         }
748a8d8ecb7SMax Reitz         key[i] = 0;
749e1ce7d74SMarkus Armbruster         qemu_printf("%*s%s:%c", indentation * 4, "", key,
750853ccfedSPeter Xu                     composite ? '\n' : ' ');
751e1ce7d74SMarkus Armbruster         dump_qobject(indentation + 1, entry->value);
752a8d8ecb7SMax Reitz         if (!composite) {
753e1ce7d74SMarkus Armbruster             qemu_printf("\n");
754a8d8ecb7SMax Reitz         }
7555eda6227SPeter Xu         g_free(key);
756a8d8ecb7SMax Reitz     }
757a8d8ecb7SMax Reitz }
758a8d8ecb7SMax Reitz 
759e1ce7d74SMarkus Armbruster void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec)
760a8d8ecb7SMax Reitz {
761a8d8ecb7SMax Reitz     QObject *obj, *data;
7627d5e199aSDaniel P. Berrange     Visitor *v = qobject_output_visitor_new(&obj);
763a8d8ecb7SMax Reitz 
7643b098d56SEric Blake     visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
7653b098d56SEric Blake     visit_complete(v, &obj);
7667dc847ebSMax Reitz     data = qdict_get(qobject_to(QDict, obj), "data");
767e1ce7d74SMarkus Armbruster     dump_qobject(1, data);
768cb3e7f08SMarc-André Lureau     qobject_unref(obj);
7693b098d56SEric Blake     visit_free(v);
770a8d8ecb7SMax Reitz }
771a8d8ecb7SMax Reitz 
772e1ce7d74SMarkus Armbruster void bdrv_image_info_dump(ImageInfo *info)
773f364ec65SWenchao Xia {
774de38b500SEric Blake     char *size_buf, *dsize_buf;
775f364ec65SWenchao Xia     if (!info->has_actual_size) {
776de38b500SEric Blake         dsize_buf = g_strdup("unavailable");
777f364ec65SWenchao Xia     } else {
778de38b500SEric Blake         dsize_buf = size_to_str(info->actual_size);
779f364ec65SWenchao Xia     }
780de38b500SEric Blake     size_buf = size_to_str(info->virtual_size);
781e1ce7d74SMarkus Armbruster     qemu_printf("image: %s\n"
782f364ec65SWenchao Xia                 "file format: %s\n"
783f364ec65SWenchao Xia                 "virtual size: %s (%" PRId64 " bytes)\n"
784f364ec65SWenchao Xia                 "disk size: %s\n",
785f364ec65SWenchao Xia                 info->filename, info->format, size_buf,
786f364ec65SWenchao Xia                 info->virtual_size,
787f364ec65SWenchao Xia                 dsize_buf);
788de38b500SEric Blake     g_free(size_buf);
789de38b500SEric Blake     g_free(dsize_buf);
790f364ec65SWenchao Xia 
791f364ec65SWenchao Xia     if (info->has_encrypted && info->encrypted) {
792e1ce7d74SMarkus Armbruster         qemu_printf("encrypted: yes\n");
793f364ec65SWenchao Xia     }
794f364ec65SWenchao Xia 
795f364ec65SWenchao Xia     if (info->has_cluster_size) {
796e1ce7d74SMarkus Armbruster         qemu_printf("cluster_size: %" PRId64 "\n",
7975b917044SWenchao Xia                     info->cluster_size);
798f364ec65SWenchao Xia     }
799f364ec65SWenchao Xia 
800f364ec65SWenchao Xia     if (info->has_dirty_flag && info->dirty_flag) {
801e1ce7d74SMarkus Armbruster         qemu_printf("cleanly shut down: no\n");
802f364ec65SWenchao Xia     }
803f364ec65SWenchao Xia 
804f364ec65SWenchao Xia     if (info->has_backing_filename) {
805e1ce7d74SMarkus Armbruster         qemu_printf("backing file: %s", info->backing_filename);
8065c9d9ca5SJohn Snow         if (!info->has_full_backing_filename) {
807e1ce7d74SMarkus Armbruster             qemu_printf(" (cannot determine actual path)");
8085c9d9ca5SJohn Snow         } else if (strcmp(info->backing_filename,
8095c9d9ca5SJohn Snow                           info->full_backing_filename) != 0) {
810e1ce7d74SMarkus Armbruster             qemu_printf(" (actual path: %s)", info->full_backing_filename);
811f364ec65SWenchao Xia         }
812e1ce7d74SMarkus Armbruster         qemu_printf("\n");
813f364ec65SWenchao Xia         if (info->has_backing_filename_format) {
814e1ce7d74SMarkus Armbruster             qemu_printf("backing file format: %s\n",
8155b917044SWenchao Xia                         info->backing_filename_format);
816f364ec65SWenchao Xia         }
817f364ec65SWenchao Xia     }
818f364ec65SWenchao Xia 
819f364ec65SWenchao Xia     if (info->has_snapshots) {
820f364ec65SWenchao Xia         SnapshotInfoList *elem;
821f364ec65SWenchao Xia 
822e1ce7d74SMarkus Armbruster         qemu_printf("Snapshot list:\n");
823e1ce7d74SMarkus Armbruster         bdrv_snapshot_dump(NULL);
824e1ce7d74SMarkus Armbruster         qemu_printf("\n");
825f364ec65SWenchao Xia 
826f364ec65SWenchao Xia         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
827f364ec65SWenchao Xia          * we convert to the block layer's native QEMUSnapshotInfo for now.
828f364ec65SWenchao Xia          */
829f364ec65SWenchao Xia         for (elem = info->snapshots; elem; elem = elem->next) {
830f364ec65SWenchao Xia             QEMUSnapshotInfo sn = {
831f364ec65SWenchao Xia                 .vm_state_size = elem->value->vm_state_size,
832f364ec65SWenchao Xia                 .date_sec = elem->value->date_sec,
833f364ec65SWenchao Xia                 .date_nsec = elem->value->date_nsec,
834f364ec65SWenchao Xia                 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
835f364ec65SWenchao Xia                                  elem->value->vm_clock_nsec,
836f364ec65SWenchao Xia             };
837f364ec65SWenchao Xia 
838f364ec65SWenchao Xia             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
839f364ec65SWenchao Xia             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
840e1ce7d74SMarkus Armbruster             bdrv_snapshot_dump(&sn);
841e1ce7d74SMarkus Armbruster             qemu_printf("\n");
842f364ec65SWenchao Xia         }
843f364ec65SWenchao Xia     }
844a8d8ecb7SMax Reitz 
845a8d8ecb7SMax Reitz     if (info->has_format_specific) {
846e1ce7d74SMarkus Armbruster         qemu_printf("Format specific information:\n");
847e1ce7d74SMarkus Armbruster         bdrv_image_info_specific_dump(info->format_specific);
848a8d8ecb7SMax Reitz     }
849f364ec65SWenchao Xia }
850