xref: /qemu/block/qapi.c (revision 853ccfed)
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"
26f364ec65SWenchao Xia #include "block/qapi.h"
27f364ec65SWenchao Xia #include "block/block_int.h"
2876f4afb4SAlberto Garcia #include "block/throttle-groups.h"
29e2462113SFrancesco Romani #include "block/write-threshold.h"
30f364ec65SWenchao Xia #include "qmp-commands.h"
31a8d8ecb7SMax Reitz #include "qapi-visit.h"
32a8d8ecb7SMax Reitz #include "qapi/qmp-output-visitor.h"
33a8d8ecb7SMax Reitz #include "qapi/qmp/types.h"
34d829a211SMarkus Armbruster #include "sysemu/block-backend.h"
35f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
36f364ec65SWenchao Xia 
37d5a8ee60SAlberto Garcia BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs, Error **errp)
38c13163fbSBenoît Canet {
39d5a8ee60SAlberto Garcia     ImageInfo **p_image_info;
40d5a8ee60SAlberto Garcia     BlockDriverState *bs0;
41c13163fbSBenoît Canet     BlockDeviceInfo *info = g_malloc0(sizeof(*info));
42c13163fbSBenoît Canet 
43c13163fbSBenoît Canet     info->file                   = g_strdup(bs->filename);
44c13163fbSBenoît Canet     info->ro                     = bs->read_only;
45c13163fbSBenoît Canet     info->drv                    = g_strdup(bs->drv->format_name);
46c13163fbSBenoît Canet     info->encrypted              = bs->encrypted;
47c13163fbSBenoît Canet     info->encryption_key_missing = bdrv_key_required(bs);
48c13163fbSBenoît Canet 
499e193c5aSKevin Wolf     info->cache = g_new(BlockdevCacheInfo, 1);
509e193c5aSKevin Wolf     *info->cache = (BlockdevCacheInfo) {
519e193c5aSKevin Wolf         .writeback      = bdrv_enable_write_cache(bs),
529e193c5aSKevin Wolf         .direct         = !!(bs->open_flags & BDRV_O_NOCACHE),
539e193c5aSKevin Wolf         .no_flush       = !!(bs->open_flags & BDRV_O_NO_FLUSH),
549e193c5aSKevin Wolf     };
559e193c5aSKevin Wolf 
56c13163fbSBenoît Canet     if (bs->node_name[0]) {
57c13163fbSBenoît Canet         info->has_node_name = true;
58c13163fbSBenoît Canet         info->node_name = g_strdup(bs->node_name);
59c13163fbSBenoît Canet     }
60c13163fbSBenoît Canet 
61c13163fbSBenoît Canet     if (bs->backing_file[0]) {
62c13163fbSBenoît Canet         info->has_backing_file = true;
63c13163fbSBenoît Canet         info->backing_file = g_strdup(bs->backing_file);
64c13163fbSBenoît Canet     }
65c13163fbSBenoît Canet 
66c13163fbSBenoît Canet     info->backing_file_depth = bdrv_get_backing_file_depth(bs);
67465bee1dSPeter Lieven     info->detect_zeroes = bs->detect_zeroes;
68c13163fbSBenoît Canet 
69a0d64a61SAlberto Garcia     if (bs->throttle_state) {
70c13163fbSBenoît Canet         ThrottleConfig cfg;
7176f4afb4SAlberto Garcia 
7276f4afb4SAlberto Garcia         throttle_group_get_config(bs, &cfg);
7376f4afb4SAlberto Garcia 
74c13163fbSBenoît Canet         info->bps     = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
75c13163fbSBenoît Canet         info->bps_rd  = cfg.buckets[THROTTLE_BPS_READ].avg;
76c13163fbSBenoît Canet         info->bps_wr  = cfg.buckets[THROTTLE_BPS_WRITE].avg;
77c13163fbSBenoît Canet 
78c13163fbSBenoît Canet         info->iops    = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
79c13163fbSBenoît Canet         info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
80c13163fbSBenoît Canet         info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
81c13163fbSBenoît Canet 
82c13163fbSBenoît Canet         info->has_bps_max     = cfg.buckets[THROTTLE_BPS_TOTAL].max;
83c13163fbSBenoît Canet         info->bps_max         = cfg.buckets[THROTTLE_BPS_TOTAL].max;
84c13163fbSBenoît Canet         info->has_bps_rd_max  = cfg.buckets[THROTTLE_BPS_READ].max;
85c13163fbSBenoît Canet         info->bps_rd_max      = cfg.buckets[THROTTLE_BPS_READ].max;
86c13163fbSBenoît Canet         info->has_bps_wr_max  = cfg.buckets[THROTTLE_BPS_WRITE].max;
87c13163fbSBenoît Canet         info->bps_wr_max      = cfg.buckets[THROTTLE_BPS_WRITE].max;
88c13163fbSBenoît Canet 
89c13163fbSBenoît Canet         info->has_iops_max    = cfg.buckets[THROTTLE_OPS_TOTAL].max;
90c13163fbSBenoît Canet         info->iops_max        = cfg.buckets[THROTTLE_OPS_TOTAL].max;
91c13163fbSBenoît Canet         info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
92c13163fbSBenoît Canet         info->iops_rd_max     = cfg.buckets[THROTTLE_OPS_READ].max;
93c13163fbSBenoît Canet         info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
94c13163fbSBenoît Canet         info->iops_wr_max     = cfg.buckets[THROTTLE_OPS_WRITE].max;
95c13163fbSBenoît Canet 
96398befdfSAlberto Garcia         info->has_bps_max_length     = info->has_bps_max;
97398befdfSAlberto Garcia         info->bps_max_length         =
98398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
99398befdfSAlberto Garcia         info->has_bps_rd_max_length  = info->has_bps_rd_max;
100398befdfSAlberto Garcia         info->bps_rd_max_length      =
101398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_READ].burst_length;
102398befdfSAlberto Garcia         info->has_bps_wr_max_length  = info->has_bps_wr_max;
103398befdfSAlberto Garcia         info->bps_wr_max_length      =
104398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
105398befdfSAlberto Garcia 
106398befdfSAlberto Garcia         info->has_iops_max_length    = info->has_iops_max;
107398befdfSAlberto Garcia         info->iops_max_length        =
108398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
109398befdfSAlberto Garcia         info->has_iops_rd_max_length = info->has_iops_rd_max;
110398befdfSAlberto Garcia         info->iops_rd_max_length     =
111398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_READ].burst_length;
112398befdfSAlberto Garcia         info->has_iops_wr_max_length = info->has_iops_wr_max;
113398befdfSAlberto Garcia         info->iops_wr_max_length     =
114398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
115398befdfSAlberto Garcia 
116c13163fbSBenoît Canet         info->has_iops_size = cfg.op_size;
117c13163fbSBenoît Canet         info->iops_size = cfg.op_size;
118b8fe1694SAlberto Garcia 
119b8fe1694SAlberto Garcia         info->has_group = true;
120b8fe1694SAlberto Garcia         info->group = g_strdup(throttle_group_get_name(bs));
121c13163fbSBenoît Canet     }
122c13163fbSBenoît Canet 
123e2462113SFrancesco Romani     info->write_threshold = bdrv_write_threshold_get(bs);
124e2462113SFrancesco Romani 
125d5a8ee60SAlberto Garcia     bs0 = bs;
126d5a8ee60SAlberto Garcia     p_image_info = &info->image;
127d5a8ee60SAlberto Garcia     while (1) {
128d5a8ee60SAlberto Garcia         Error *local_err = NULL;
129d5a8ee60SAlberto Garcia         bdrv_query_image_info(bs0, p_image_info, &local_err);
130d5a8ee60SAlberto Garcia         if (local_err) {
131d5a8ee60SAlberto Garcia             error_propagate(errp, local_err);
132d5a8ee60SAlberto Garcia             qapi_free_BlockDeviceInfo(info);
133d5a8ee60SAlberto Garcia             return NULL;
134d5a8ee60SAlberto Garcia         }
135760e0063SKevin Wolf         if (bs0->drv && bs0->backing) {
136760e0063SKevin Wolf             bs0 = bs0->backing->bs;
137d5a8ee60SAlberto Garcia             (*p_image_info)->has_backing_image = true;
138d5a8ee60SAlberto Garcia             p_image_info = &((*p_image_info)->backing_image);
139d5a8ee60SAlberto Garcia         } else {
140d5a8ee60SAlberto Garcia             break;
141d5a8ee60SAlberto Garcia         }
142d5a8ee60SAlberto Garcia     }
143d5a8ee60SAlberto Garcia 
144c13163fbSBenoît Canet     return info;
145c13163fbSBenoît Canet }
146c13163fbSBenoît Canet 
147fb0ed453SWenchao Xia /*
148fb0ed453SWenchao Xia  * Returns 0 on success, with *p_list either set to describe snapshot
149fb0ed453SWenchao Xia  * information, or NULL because there are no snapshots.  Returns -errno on
150fb0ed453SWenchao Xia  * error, with *p_list untouched.
151fb0ed453SWenchao Xia  */
152fb0ed453SWenchao Xia int bdrv_query_snapshot_info_list(BlockDriverState *bs,
153fb0ed453SWenchao Xia                                   SnapshotInfoList **p_list,
154fb0ed453SWenchao Xia                                   Error **errp)
155f364ec65SWenchao Xia {
156f364ec65SWenchao Xia     int i, sn_count;
157f364ec65SWenchao Xia     QEMUSnapshotInfo *sn_tab = NULL;
158fb0ed453SWenchao Xia     SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
159fb0ed453SWenchao Xia     SnapshotInfo *info;
160fb0ed453SWenchao Xia 
161f364ec65SWenchao Xia     sn_count = bdrv_snapshot_list(bs, &sn_tab);
162fb0ed453SWenchao Xia     if (sn_count < 0) {
163fb0ed453SWenchao Xia         const char *dev = bdrv_get_device_name(bs);
164fb0ed453SWenchao Xia         switch (sn_count) {
165fb0ed453SWenchao Xia         case -ENOMEDIUM:
166fb0ed453SWenchao Xia             error_setg(errp, "Device '%s' is not inserted", dev);
167fb0ed453SWenchao Xia             break;
168fb0ed453SWenchao Xia         case -ENOTSUP:
169fb0ed453SWenchao Xia             error_setg(errp,
170fb0ed453SWenchao Xia                        "Device '%s' does not support internal snapshots",
171fb0ed453SWenchao Xia                        dev);
172fb0ed453SWenchao Xia             break;
173fb0ed453SWenchao Xia         default:
174fb0ed453SWenchao Xia             error_setg_errno(errp, -sn_count,
175fb0ed453SWenchao Xia                              "Can't list snapshots of device '%s'", dev);
176fb0ed453SWenchao Xia             break;
177fb0ed453SWenchao Xia         }
178fb0ed453SWenchao Xia         return sn_count;
179fb0ed453SWenchao Xia     }
180f364ec65SWenchao Xia 
181f364ec65SWenchao Xia     for (i = 0; i < sn_count; i++) {
182fb0ed453SWenchao Xia         info = g_new0(SnapshotInfo, 1);
183fb0ed453SWenchao Xia         info->id            = g_strdup(sn_tab[i].id_str);
184fb0ed453SWenchao Xia         info->name          = g_strdup(sn_tab[i].name);
185fb0ed453SWenchao Xia         info->vm_state_size = sn_tab[i].vm_state_size;
186fb0ed453SWenchao Xia         info->date_sec      = sn_tab[i].date_sec;
187fb0ed453SWenchao Xia         info->date_nsec     = sn_tab[i].date_nsec;
188fb0ed453SWenchao Xia         info->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
189fb0ed453SWenchao Xia         info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
190f364ec65SWenchao Xia 
191fb0ed453SWenchao Xia         info_list = g_new0(SnapshotInfoList, 1);
192fb0ed453SWenchao Xia         info_list->value = info;
193f364ec65SWenchao Xia 
194f364ec65SWenchao Xia         /* XXX: waiting for the qapi to support qemu-queue.h types */
195f364ec65SWenchao Xia         if (!cur_item) {
196fb0ed453SWenchao Xia             head = cur_item = info_list;
197f364ec65SWenchao Xia         } else {
198f364ec65SWenchao Xia             cur_item->next = info_list;
199f364ec65SWenchao Xia             cur_item = info_list;
200f364ec65SWenchao Xia         }
201f364ec65SWenchao Xia 
202f364ec65SWenchao Xia     }
203f364ec65SWenchao Xia 
204f364ec65SWenchao Xia     g_free(sn_tab);
205fb0ed453SWenchao Xia     *p_list = head;
206fb0ed453SWenchao Xia     return 0;
207f364ec65SWenchao Xia }
208f364ec65SWenchao Xia 
20943526ec8SWenchao Xia /**
21043526ec8SWenchao Xia  * bdrv_query_image_info:
21143526ec8SWenchao Xia  * @bs: block device to examine
21243526ec8SWenchao Xia  * @p_info: location to store image information
21343526ec8SWenchao Xia  * @errp: location to store error information
21443526ec8SWenchao Xia  *
215553a7e87SWenchao Xia  * Store "flat" image information in @p_info.
216553a7e87SWenchao Xia  *
217553a7e87SWenchao Xia  * "Flat" means it does *not* query backing image information,
218553a7e87SWenchao Xia  * i.e. (*pinfo)->has_backing_image will be set to false and
219553a7e87SWenchao Xia  * (*pinfo)->backing_image to NULL even when the image does in fact have
220553a7e87SWenchao Xia  * a backing image.
221553a7e87SWenchao Xia  *
22243526ec8SWenchao Xia  * @p_info will be set only on success. On error, store error in @errp.
22343526ec8SWenchao Xia  */
22443526ec8SWenchao Xia void bdrv_query_image_info(BlockDriverState *bs,
22543526ec8SWenchao Xia                            ImageInfo **p_info,
22643526ec8SWenchao Xia                            Error **errp)
227f364ec65SWenchao Xia {
22852bf1e72SMarkus Armbruster     int64_t size;
22943526ec8SWenchao Xia     const char *backing_filename;
230f364ec65SWenchao Xia     BlockDriverInfo bdi;
23143526ec8SWenchao Xia     int ret;
23243526ec8SWenchao Xia     Error *err = NULL;
23352bf1e72SMarkus Armbruster     ImageInfo *info;
234f364ec65SWenchao Xia 
2351963f8d5SPaolo Bonzini     aio_context_acquire(bdrv_get_aio_context(bs));
2361963f8d5SPaolo Bonzini 
23752bf1e72SMarkus Armbruster     size = bdrv_getlength(bs);
23852bf1e72SMarkus Armbruster     if (size < 0) {
23952bf1e72SMarkus Armbruster         error_setg_errno(errp, -size, "Can't get size of device '%s'",
24052bf1e72SMarkus Armbruster                          bdrv_get_device_name(bs));
2411963f8d5SPaolo Bonzini         goto out;
24252bf1e72SMarkus Armbruster     }
243f364ec65SWenchao Xia 
24452bf1e72SMarkus Armbruster     info = g_new0(ImageInfo, 1);
24543526ec8SWenchao Xia     info->filename        = g_strdup(bs->filename);
246f364ec65SWenchao Xia     info->format          = g_strdup(bdrv_get_format_name(bs));
24752bf1e72SMarkus Armbruster     info->virtual_size    = size;
248f364ec65SWenchao Xia     info->actual_size     = bdrv_get_allocated_file_size(bs);
249f364ec65SWenchao Xia     info->has_actual_size = info->actual_size >= 0;
250f364ec65SWenchao Xia     if (bdrv_is_encrypted(bs)) {
251f364ec65SWenchao Xia         info->encrypted = true;
252f364ec65SWenchao Xia         info->has_encrypted = true;
253f364ec65SWenchao Xia     }
254f364ec65SWenchao Xia     if (bdrv_get_info(bs, &bdi) >= 0) {
255f364ec65SWenchao Xia         if (bdi.cluster_size != 0) {
256f364ec65SWenchao Xia             info->cluster_size = bdi.cluster_size;
257f364ec65SWenchao Xia             info->has_cluster_size = true;
258f364ec65SWenchao Xia         }
259f364ec65SWenchao Xia         info->dirty_flag = bdi.is_dirty;
260f364ec65SWenchao Xia         info->has_dirty_flag = true;
261f364ec65SWenchao Xia     }
262eae041feSMax Reitz     info->format_specific     = bdrv_get_specific_info(bs);
263eae041feSMax Reitz     info->has_format_specific = info->format_specific != NULL;
264eae041feSMax Reitz 
26543526ec8SWenchao Xia     backing_filename = bs->backing_file;
266f364ec65SWenchao Xia     if (backing_filename[0] != '\0') {
2679a29e18fSJeff Cody         char *backing_filename2 = g_malloc0(PATH_MAX);
268f364ec65SWenchao Xia         info->backing_filename = g_strdup(backing_filename);
269f364ec65SWenchao Xia         info->has_backing_filename = true;
2709a29e18fSJeff Cody         bdrv_get_full_backing_filename(bs, backing_filename2, PATH_MAX, &err);
2719f07429eSMax Reitz         if (err) {
272a5002d53SJohn Snow             /* Can't reconstruct the full backing filename, so we must omit
273a5002d53SJohn Snow              * this field and apply a Best Effort to this query. */
274564d64bdSJeff Cody             g_free(backing_filename2);
275a5002d53SJohn Snow             backing_filename2 = NULL;
276a5002d53SJohn Snow             error_free(err);
2770fa296ebSFam Zheng             err = NULL;
2789f07429eSMax Reitz         }
279f364ec65SWenchao Xia 
28012dcb1c0SJohn Snow         /* Always report the full_backing_filename if present, even if it's the
28112dcb1c0SJohn Snow          * same as backing_filename. That they are same is useful info. */
28212dcb1c0SJohn Snow         if (backing_filename2) {
28312dcb1c0SJohn Snow             info->full_backing_filename = g_strdup(backing_filename2);
284f364ec65SWenchao Xia             info->has_full_backing_filename = true;
285f364ec65SWenchao Xia         }
286f364ec65SWenchao Xia 
287f364ec65SWenchao Xia         if (bs->backing_format[0]) {
288f364ec65SWenchao Xia             info->backing_filename_format = g_strdup(bs->backing_format);
289f364ec65SWenchao Xia             info->has_backing_filename_format = true;
290f364ec65SWenchao Xia         }
291564d64bdSJeff Cody         g_free(backing_filename2);
292f364ec65SWenchao Xia     }
29343526ec8SWenchao Xia 
29443526ec8SWenchao Xia     ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
29543526ec8SWenchao Xia     switch (ret) {
29643526ec8SWenchao Xia     case 0:
29743526ec8SWenchao Xia         if (info->snapshots) {
29843526ec8SWenchao Xia             info->has_snapshots = true;
29943526ec8SWenchao Xia         }
30043526ec8SWenchao Xia         break;
30143526ec8SWenchao Xia     /* recoverable error */
30243526ec8SWenchao Xia     case -ENOMEDIUM:
30343526ec8SWenchao Xia     case -ENOTSUP:
30443526ec8SWenchao Xia         error_free(err);
30543526ec8SWenchao Xia         break;
30643526ec8SWenchao Xia     default:
30743526ec8SWenchao Xia         error_propagate(errp, err);
30843526ec8SWenchao Xia         qapi_free_ImageInfo(info);
3091963f8d5SPaolo Bonzini         goto out;
31043526ec8SWenchao Xia     }
31143526ec8SWenchao Xia 
31243526ec8SWenchao Xia     *p_info = info;
3131963f8d5SPaolo Bonzini 
3141963f8d5SPaolo Bonzini out:
3151963f8d5SPaolo Bonzini     aio_context_release(bdrv_get_aio_context(bs));
316f364ec65SWenchao Xia }
317f364ec65SWenchao Xia 
318553a7e87SWenchao Xia /* @p_info will be set only on success. */
319d829a211SMarkus Armbruster static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
320553a7e87SWenchao Xia                             Error **errp)
321f364ec65SWenchao Xia {
322f364ec65SWenchao Xia     BlockInfo *info = g_malloc0(sizeof(*info));
323d829a211SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
324d829a211SMarkus Armbruster     info->device = g_strdup(blk_name(blk));
325f364ec65SWenchao Xia     info->type = g_strdup("unknown");
326a7f53e26SMarkus Armbruster     info->locked = blk_dev_is_medium_locked(blk);
327a7f53e26SMarkus Armbruster     info->removable = blk_dev_has_removable_media(blk);
328f364ec65SWenchao Xia 
329327032ceSMax Reitz     if (blk_dev_has_tray(blk)) {
330f364ec65SWenchao Xia         info->has_tray_open = true;
331a7f53e26SMarkus Armbruster         info->tray_open = blk_dev_is_tray_open(blk);
332f364ec65SWenchao Xia     }
333f364ec65SWenchao Xia 
334373340b2SMax Reitz     if (blk_iostatus_is_enabled(blk)) {
335f364ec65SWenchao Xia         info->has_io_status = true;
336373340b2SMax Reitz         info->io_status = blk_iostatus(blk);
337f364ec65SWenchao Xia     }
338f364ec65SWenchao Xia 
3395433c24fSMax Reitz     if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
34021b56835SFam Zheng         info->has_dirty_bitmaps = true;
34121b56835SFam Zheng         info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
34221b56835SFam Zheng     }
34321b56835SFam Zheng 
3445433c24fSMax Reitz     if (bs && bs->drv) {
345f364ec65SWenchao Xia         info->has_inserted = true;
346d5a8ee60SAlberto Garcia         info->inserted = bdrv_block_device_info(bs, errp);
347d5a8ee60SAlberto Garcia         if (info->inserted == NULL) {
348553a7e87SWenchao Xia             goto err;
349f364ec65SWenchao Xia         }
350553a7e87SWenchao Xia     }
351553a7e87SWenchao Xia 
352553a7e87SWenchao Xia     *p_info = info;
353553a7e87SWenchao Xia     return;
354553a7e87SWenchao Xia 
355553a7e87SWenchao Xia  err:
356553a7e87SWenchao Xia     qapi_free_BlockInfo(info);
357f364ec65SWenchao Xia }
358f364ec65SWenchao Xia 
359c21cc6caSKevin Wolf static BlockStats *bdrv_query_stats(BlockBackend *blk,
360c21cc6caSKevin Wolf                                     const BlockDriverState *bs,
361b07363a1SKevin Wolf                                     bool query_backing);
362b07363a1SKevin Wolf 
3632b77e60aSKevin Wolf static void bdrv_query_blk_stats(BlockStats *s, BlockBackend *blk)
364f364ec65SWenchao Xia {
3652b77e60aSKevin Wolf     BlockAcctStats *stats = blk_get_stats(blk);
366979e9b03SAlberto Garcia     BlockAcctTimedStats *ts = NULL;
3677f0e9da6SMax Reitz 
368c21cc6caSKevin Wolf     s->has_device = true;
369c21cc6caSKevin Wolf     s->device = g_strdup(blk_name(blk));
370c21cc6caSKevin Wolf 
3717f0e9da6SMax Reitz     s->stats->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
3727f0e9da6SMax Reitz     s->stats->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
3737f0e9da6SMax Reitz     s->stats->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
3747f0e9da6SMax Reitz     s->stats->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
3757ee12dafSAlberto Garcia 
3767ee12dafSAlberto Garcia     s->stats->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
3777ee12dafSAlberto Garcia     s->stats->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
3787ee12dafSAlberto Garcia     s->stats->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
3797ee12dafSAlberto Garcia 
3807ee12dafSAlberto Garcia     s->stats->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
3817ee12dafSAlberto Garcia     s->stats->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
3827ee12dafSAlberto Garcia     s->stats->invalid_flush_operations =
3837ee12dafSAlberto Garcia         stats->invalid_ops[BLOCK_ACCT_FLUSH];
3847ee12dafSAlberto Garcia 
3857f0e9da6SMax Reitz     s->stats->rd_merged = stats->merged[BLOCK_ACCT_READ];
3867f0e9da6SMax Reitz     s->stats->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
3877f0e9da6SMax Reitz     s->stats->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
3887f0e9da6SMax Reitz     s->stats->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
3897f0e9da6SMax Reitz     s->stats->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
3907f0e9da6SMax Reitz     s->stats->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
391cb38fffbSAlberto Garcia 
392cb38fffbSAlberto Garcia     s->stats->has_idle_time_ns = stats->last_access_time_ns > 0;
393cb38fffbSAlberto Garcia     if (s->stats->has_idle_time_ns) {
394cb38fffbSAlberto Garcia         s->stats->idle_time_ns = block_acct_idle_time_ns(stats);
395cb38fffbSAlberto Garcia     }
396362e9299SAlberto Garcia 
397362e9299SAlberto Garcia     s->stats->account_invalid = stats->account_invalid;
398362e9299SAlberto Garcia     s->stats->account_failed = stats->account_failed;
399979e9b03SAlberto Garcia 
400979e9b03SAlberto Garcia     while ((ts = block_acct_interval_next(stats, ts))) {
401979e9b03SAlberto Garcia         BlockDeviceTimedStatsList *timed_stats =
402979e9b03SAlberto Garcia             g_malloc0(sizeof(*timed_stats));
403979e9b03SAlberto Garcia         BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
404979e9b03SAlberto Garcia         timed_stats->next = s->stats->timed_stats;
405979e9b03SAlberto Garcia         timed_stats->value = dev_stats;
406979e9b03SAlberto Garcia         s->stats->timed_stats = timed_stats;
407979e9b03SAlberto Garcia 
408979e9b03SAlberto Garcia         TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
409979e9b03SAlberto Garcia         TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
410979e9b03SAlberto Garcia         TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
411979e9b03SAlberto Garcia 
412979e9b03SAlberto Garcia         dev_stats->interval_length = ts->interval_length;
413979e9b03SAlberto Garcia 
414979e9b03SAlberto Garcia         dev_stats->min_rd_latency_ns = timed_average_min(rd);
415979e9b03SAlberto Garcia         dev_stats->max_rd_latency_ns = timed_average_max(rd);
416979e9b03SAlberto Garcia         dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
417979e9b03SAlberto Garcia 
418979e9b03SAlberto Garcia         dev_stats->min_wr_latency_ns = timed_average_min(wr);
419979e9b03SAlberto Garcia         dev_stats->max_wr_latency_ns = timed_average_max(wr);
420979e9b03SAlberto Garcia         dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
421979e9b03SAlberto Garcia 
422979e9b03SAlberto Garcia         dev_stats->min_flush_latency_ns = timed_average_min(fl);
423979e9b03SAlberto Garcia         dev_stats->max_flush_latency_ns = timed_average_max(fl);
424979e9b03SAlberto Garcia         dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
42596e4dedaSAlberto Garcia 
42696e4dedaSAlberto Garcia         dev_stats->avg_rd_queue_depth =
42796e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_READ);
42896e4dedaSAlberto Garcia         dev_stats->avg_wr_queue_depth =
42996e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
430979e9b03SAlberto Garcia     }
4317f0e9da6SMax Reitz }
432f364ec65SWenchao Xia 
433b07363a1SKevin Wolf static void bdrv_query_bds_stats(BlockStats *s, const BlockDriverState *bs,
4342b77e60aSKevin Wolf                                  bool query_backing)
4352b77e60aSKevin Wolf {
4362b77e60aSKevin Wolf     if (bdrv_get_node_name(bs)[0]) {
4372b77e60aSKevin Wolf         s->has_node_name = true;
4382b77e60aSKevin Wolf         s->node_name = g_strdup(bdrv_get_node_name(bs));
4392b77e60aSKevin Wolf     }
4402b77e60aSKevin Wolf 
44153d8f9d8SMax Reitz     s->stats->wr_highest_offset = bs->wr_highest_offset;
44253d8f9d8SMax Reitz 
443f364ec65SWenchao Xia     if (bs->file) {
444f364ec65SWenchao Xia         s->has_parent = true;
445c21cc6caSKevin Wolf         s->parent = bdrv_query_stats(NULL, bs->file->bs, query_backing);
446f364ec65SWenchao Xia     }
447f364ec65SWenchao Xia 
448760e0063SKevin Wolf     if (query_backing && bs->backing) {
449c8059b97SFam Zheng         s->has_backing = true;
450c21cc6caSKevin Wolf         s->backing = bdrv_query_stats(NULL, bs->backing->bs, query_backing);
451c8059b97SFam Zheng     }
452c8059b97SFam Zheng 
453b07363a1SKevin Wolf }
454b07363a1SKevin Wolf 
455c21cc6caSKevin Wolf static BlockStats *bdrv_query_stats(BlockBackend *blk,
456c21cc6caSKevin Wolf                                     const BlockDriverState *bs,
457b07363a1SKevin Wolf                                     bool query_backing)
458b07363a1SKevin Wolf {
459b07363a1SKevin Wolf     BlockStats *s;
460b07363a1SKevin Wolf 
461b07363a1SKevin Wolf     s = g_malloc0(sizeof(*s));
462b07363a1SKevin Wolf     s->stats = g_malloc0(sizeof(*s->stats));
463b07363a1SKevin Wolf 
464c21cc6caSKevin Wolf     if (blk) {
465c21cc6caSKevin Wolf         bdrv_query_blk_stats(s, blk);
466b07363a1SKevin Wolf     }
467c21cc6caSKevin Wolf     if (bs) {
468b07363a1SKevin Wolf         bdrv_query_bds_stats(s, bs, query_backing);
469c21cc6caSKevin Wolf     }
470b07363a1SKevin Wolf 
471f364ec65SWenchao Xia     return s;
472f364ec65SWenchao Xia }
473f364ec65SWenchao Xia 
474f364ec65SWenchao Xia BlockInfoList *qmp_query_block(Error **errp)
475f364ec65SWenchao Xia {
476f364ec65SWenchao Xia     BlockInfoList *head = NULL, **p_next = &head;
477d829a211SMarkus Armbruster     BlockBackend *blk;
478553a7e87SWenchao Xia     Error *local_err = NULL;
479f364ec65SWenchao Xia 
480d829a211SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
481f364ec65SWenchao Xia         BlockInfoList *info = g_malloc0(sizeof(*info));
482d829a211SMarkus Armbruster         bdrv_query_info(blk, &info->value, &local_err);
48384d18f06SMarkus Armbruster         if (local_err) {
484553a7e87SWenchao Xia             error_propagate(errp, local_err);
485903c341dSMarkus Armbruster             g_free(info);
486903c341dSMarkus Armbruster             qapi_free_BlockInfoList(head);
487903c341dSMarkus Armbruster             return NULL;
488553a7e87SWenchao Xia         }
489f364ec65SWenchao Xia 
490f364ec65SWenchao Xia         *p_next = info;
491f364ec65SWenchao Xia         p_next = &info->next;
492f364ec65SWenchao Xia     }
493f364ec65SWenchao Xia 
494f364ec65SWenchao Xia     return head;
495f364ec65SWenchao Xia }
496f364ec65SWenchao Xia 
497c21cc6caSKevin Wolf static bool next_query_bds(BlockBackend **blk, BlockDriverState **bs,
498c21cc6caSKevin Wolf                            bool query_nodes)
499c21cc6caSKevin Wolf {
500c21cc6caSKevin Wolf     if (query_nodes) {
501c21cc6caSKevin Wolf         *bs = bdrv_next_node(*bs);
502c21cc6caSKevin Wolf         return !!*bs;
503c21cc6caSKevin Wolf     }
504c21cc6caSKevin Wolf 
505c21cc6caSKevin Wolf     *blk = blk_next(*blk);
506c21cc6caSKevin Wolf     *bs = *blk ? blk_bs(*blk) : NULL;
507c21cc6caSKevin Wolf 
508c21cc6caSKevin Wolf     return !!*blk;
509c21cc6caSKevin Wolf }
510c21cc6caSKevin Wolf 
511f71eaa74SFam Zheng BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
512f71eaa74SFam Zheng                                      bool query_nodes,
513f71eaa74SFam Zheng                                      Error **errp)
514f364ec65SWenchao Xia {
515f364ec65SWenchao Xia     BlockStatsList *head = NULL, **p_next = &head;
516c21cc6caSKevin Wolf     BlockBackend *blk = NULL;
517f364ec65SWenchao Xia     BlockDriverState *bs = NULL;
518f364ec65SWenchao Xia 
519f71eaa74SFam Zheng     /* Just to be safe if query_nodes is not always initialized */
520f71eaa74SFam Zheng     query_nodes = has_query_nodes && query_nodes;
521f71eaa74SFam Zheng 
522c21cc6caSKevin Wolf     while (next_query_bds(&blk, &bs, query_nodes)) {
523f364ec65SWenchao Xia         BlockStatsList *info = g_malloc0(sizeof(*info));
524c21cc6caSKevin Wolf         AioContext *ctx = blk ? blk_get_aio_context(blk)
525c21cc6caSKevin Wolf                               : bdrv_get_aio_context(bs);
52613344f3aSStefan Hajnoczi 
52713344f3aSStefan Hajnoczi         aio_context_acquire(ctx);
528c21cc6caSKevin Wolf         info->value = bdrv_query_stats(blk, bs, !query_nodes);
52913344f3aSStefan Hajnoczi         aio_context_release(ctx);
530f364ec65SWenchao Xia 
531f364ec65SWenchao Xia         *p_next = info;
532f364ec65SWenchao Xia         p_next = &info->next;
533f364ec65SWenchao Xia     }
534f364ec65SWenchao Xia 
535f364ec65SWenchao Xia     return head;
536f364ec65SWenchao Xia }
537f364ec65SWenchao Xia 
538f364ec65SWenchao Xia #define NB_SUFFIXES 4
539f364ec65SWenchao Xia 
540f364ec65SWenchao Xia static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
541f364ec65SWenchao Xia {
5422c20fa2cSStefan Weil     static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'};
543f364ec65SWenchao Xia     int64_t base;
544f364ec65SWenchao Xia     int i;
545f364ec65SWenchao Xia 
546f364ec65SWenchao Xia     if (size <= 999) {
547f364ec65SWenchao Xia         snprintf(buf, buf_size, "%" PRId64, size);
548f364ec65SWenchao Xia     } else {
549f364ec65SWenchao Xia         base = 1024;
550f364ec65SWenchao Xia         for (i = 0; i < NB_SUFFIXES; i++) {
551f364ec65SWenchao Xia             if (size < (10 * base)) {
552f364ec65SWenchao Xia                 snprintf(buf, buf_size, "%0.1f%c",
553f364ec65SWenchao Xia                          (double)size / base,
554f364ec65SWenchao Xia                          suffixes[i]);
555f364ec65SWenchao Xia                 break;
556f364ec65SWenchao Xia             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
557f364ec65SWenchao Xia                 snprintf(buf, buf_size, "%" PRId64 "%c",
558f364ec65SWenchao Xia                          ((size + (base >> 1)) / base),
559f364ec65SWenchao Xia                          suffixes[i]);
560f364ec65SWenchao Xia                 break;
561f364ec65SWenchao Xia             }
562f364ec65SWenchao Xia             base = base * 1024;
563f364ec65SWenchao Xia         }
564f364ec65SWenchao Xia     }
565f364ec65SWenchao Xia     return buf;
566f364ec65SWenchao Xia }
567f364ec65SWenchao Xia 
5685b917044SWenchao Xia void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
5695b917044SWenchao Xia                         QEMUSnapshotInfo *sn)
570f364ec65SWenchao Xia {
571f364ec65SWenchao Xia     char buf1[128], date_buf[128], clock_buf[128];
572f364ec65SWenchao Xia     struct tm tm;
573f364ec65SWenchao Xia     time_t ti;
574f364ec65SWenchao Xia     int64_t secs;
575f364ec65SWenchao Xia 
576f364ec65SWenchao Xia     if (!sn) {
5775b917044SWenchao Xia         func_fprintf(f,
578f364ec65SWenchao Xia                      "%-10s%-20s%7s%20s%15s",
579f364ec65SWenchao Xia                      "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
580f364ec65SWenchao Xia     } else {
581f364ec65SWenchao Xia         ti = sn->date_sec;
582f364ec65SWenchao Xia         localtime_r(&ti, &tm);
583f364ec65SWenchao Xia         strftime(date_buf, sizeof(date_buf),
584f364ec65SWenchao Xia                  "%Y-%m-%d %H:%M:%S", &tm);
585f364ec65SWenchao Xia         secs = sn->vm_clock_nsec / 1000000000;
586f364ec65SWenchao Xia         snprintf(clock_buf, sizeof(clock_buf),
587f364ec65SWenchao Xia                  "%02d:%02d:%02d.%03d",
588f364ec65SWenchao Xia                  (int)(secs / 3600),
589f364ec65SWenchao Xia                  (int)((secs / 60) % 60),
590f364ec65SWenchao Xia                  (int)(secs % 60),
591f364ec65SWenchao Xia                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
5925b917044SWenchao Xia         func_fprintf(f,
593f364ec65SWenchao Xia                      "%-10s%-20s%7s%20s%15s",
594f364ec65SWenchao Xia                      sn->id_str, sn->name,
5955b917044SWenchao Xia                      get_human_readable_size(buf1, sizeof(buf1),
5965b917044SWenchao Xia                                              sn->vm_state_size),
597f364ec65SWenchao Xia                      date_buf,
598f364ec65SWenchao Xia                      clock_buf);
599f364ec65SWenchao Xia     }
600f364ec65SWenchao Xia }
601f364ec65SWenchao Xia 
602a8d8ecb7SMax Reitz static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
603a8d8ecb7SMax Reitz                        QDict *dict);
604a8d8ecb7SMax Reitz static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
605a8d8ecb7SMax Reitz                        QList *list);
606a8d8ecb7SMax Reitz 
607a8d8ecb7SMax Reitz static void dump_qobject(fprintf_function func_fprintf, void *f,
608a8d8ecb7SMax Reitz                          int comp_indent, QObject *obj)
609a8d8ecb7SMax Reitz {
610a8d8ecb7SMax Reitz     switch (qobject_type(obj)) {
611a8d8ecb7SMax Reitz         case QTYPE_QINT: {
612a8d8ecb7SMax Reitz             QInt *value = qobject_to_qint(obj);
613a8d8ecb7SMax Reitz             func_fprintf(f, "%" PRId64, qint_get_int(value));
614a8d8ecb7SMax Reitz             break;
615a8d8ecb7SMax Reitz         }
616a8d8ecb7SMax Reitz         case QTYPE_QSTRING: {
617a8d8ecb7SMax Reitz             QString *value = qobject_to_qstring(obj);
618a8d8ecb7SMax Reitz             func_fprintf(f, "%s", qstring_get_str(value));
619a8d8ecb7SMax Reitz             break;
620a8d8ecb7SMax Reitz         }
621a8d8ecb7SMax Reitz         case QTYPE_QDICT: {
622a8d8ecb7SMax Reitz             QDict *value = qobject_to_qdict(obj);
623a8d8ecb7SMax Reitz             dump_qdict(func_fprintf, f, comp_indent, value);
624a8d8ecb7SMax Reitz             break;
625a8d8ecb7SMax Reitz         }
626a8d8ecb7SMax Reitz         case QTYPE_QLIST: {
627a8d8ecb7SMax Reitz             QList *value = qobject_to_qlist(obj);
628a8d8ecb7SMax Reitz             dump_qlist(func_fprintf, f, comp_indent, value);
629a8d8ecb7SMax Reitz             break;
630a8d8ecb7SMax Reitz         }
631a8d8ecb7SMax Reitz         case QTYPE_QFLOAT: {
632a8d8ecb7SMax Reitz             QFloat *value = qobject_to_qfloat(obj);
633a8d8ecb7SMax Reitz             func_fprintf(f, "%g", qfloat_get_double(value));
634a8d8ecb7SMax Reitz             break;
635a8d8ecb7SMax Reitz         }
636a8d8ecb7SMax Reitz         case QTYPE_QBOOL: {
637a8d8ecb7SMax Reitz             QBool *value = qobject_to_qbool(obj);
638fc48ffc3SEric Blake             func_fprintf(f, "%s", qbool_get_bool(value) ? "true" : "false");
639a8d8ecb7SMax Reitz             break;
640a8d8ecb7SMax Reitz         }
641a8d8ecb7SMax Reitz         default:
642a8d8ecb7SMax Reitz             abort();
643a8d8ecb7SMax Reitz     }
644a8d8ecb7SMax Reitz }
645a8d8ecb7SMax Reitz 
646a8d8ecb7SMax Reitz static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
647a8d8ecb7SMax Reitz                        QList *list)
648a8d8ecb7SMax Reitz {
649a8d8ecb7SMax Reitz     const QListEntry *entry;
650a8d8ecb7SMax Reitz     int i = 0;
651a8d8ecb7SMax Reitz 
652a8d8ecb7SMax Reitz     for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
6531310a3d3SEric Blake         QType type = qobject_type(entry->value);
654a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
655*853ccfedSPeter Xu         func_fprintf(f, "%*s[%i]:%c", indentation * 4, "", i,
656*853ccfedSPeter Xu                      composite ? '\n' : ' ');
657a8d8ecb7SMax Reitz         dump_qobject(func_fprintf, f, indentation + 1, entry->value);
658a8d8ecb7SMax Reitz         if (!composite) {
659a8d8ecb7SMax Reitz             func_fprintf(f, "\n");
660a8d8ecb7SMax Reitz         }
661a8d8ecb7SMax Reitz     }
662a8d8ecb7SMax Reitz }
663a8d8ecb7SMax Reitz 
664a8d8ecb7SMax Reitz static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
665a8d8ecb7SMax Reitz                        QDict *dict)
666a8d8ecb7SMax Reitz {
667a8d8ecb7SMax Reitz     const QDictEntry *entry;
668a8d8ecb7SMax Reitz 
669a8d8ecb7SMax Reitz     for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
6701310a3d3SEric Blake         QType type = qobject_type(entry->value);
671a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
672a8d8ecb7SMax Reitz         char key[strlen(entry->key) + 1];
673a8d8ecb7SMax Reitz         int i;
674a8d8ecb7SMax Reitz 
675a8d8ecb7SMax Reitz         /* replace dashes with spaces in key (variable) names */
676a8d8ecb7SMax Reitz         for (i = 0; entry->key[i]; i++) {
677a8d8ecb7SMax Reitz             key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
678a8d8ecb7SMax Reitz         }
679a8d8ecb7SMax Reitz         key[i] = 0;
680*853ccfedSPeter Xu         func_fprintf(f, "%*s%s:%c", indentation * 4, "", key,
681*853ccfedSPeter Xu                      composite ? '\n' : ' ');
682a8d8ecb7SMax Reitz         dump_qobject(func_fprintf, f, indentation + 1, entry->value);
683a8d8ecb7SMax Reitz         if (!composite) {
684a8d8ecb7SMax Reitz             func_fprintf(f, "\n");
685a8d8ecb7SMax Reitz         }
686a8d8ecb7SMax Reitz     }
687a8d8ecb7SMax Reitz }
688a8d8ecb7SMax Reitz 
689a8d8ecb7SMax Reitz void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
690a8d8ecb7SMax Reitz                                    ImageInfoSpecific *info_spec)
691a8d8ecb7SMax Reitz {
692a8d8ecb7SMax Reitz     QmpOutputVisitor *ov = qmp_output_visitor_new();
693a8d8ecb7SMax Reitz     QObject *obj, *data;
694a8d8ecb7SMax Reitz 
69551e72bc1SEric Blake     visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov), NULL, &info_spec,
69635d0d40aSMax Reitz                                  &error_abort);
697a8d8ecb7SMax Reitz     obj = qmp_output_get_qobject(ov);
698a8d8ecb7SMax Reitz     assert(qobject_type(obj) == QTYPE_QDICT);
699a8d8ecb7SMax Reitz     data = qdict_get(qobject_to_qdict(obj), "data");
700a8d8ecb7SMax Reitz     dump_qobject(func_fprintf, f, 1, data);
701a8d8ecb7SMax Reitz     qmp_output_visitor_cleanup(ov);
702a8d8ecb7SMax Reitz }
703a8d8ecb7SMax Reitz 
7045b917044SWenchao Xia void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
7055b917044SWenchao Xia                           ImageInfo *info)
706f364ec65SWenchao Xia {
707f364ec65SWenchao Xia     char size_buf[128], dsize_buf[128];
708f364ec65SWenchao Xia     if (!info->has_actual_size) {
709f364ec65SWenchao Xia         snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
710f364ec65SWenchao Xia     } else {
711f364ec65SWenchao Xia         get_human_readable_size(dsize_buf, sizeof(dsize_buf),
712f364ec65SWenchao Xia                                 info->actual_size);
713f364ec65SWenchao Xia     }
714f364ec65SWenchao Xia     get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
7155b917044SWenchao Xia     func_fprintf(f,
7165b917044SWenchao Xia                  "image: %s\n"
717f364ec65SWenchao Xia                  "file format: %s\n"
718f364ec65SWenchao Xia                  "virtual size: %s (%" PRId64 " bytes)\n"
719f364ec65SWenchao Xia                  "disk size: %s\n",
720f364ec65SWenchao Xia                  info->filename, info->format, size_buf,
721f364ec65SWenchao Xia                  info->virtual_size,
722f364ec65SWenchao Xia                  dsize_buf);
723f364ec65SWenchao Xia 
724f364ec65SWenchao Xia     if (info->has_encrypted && info->encrypted) {
7255b917044SWenchao Xia         func_fprintf(f, "encrypted: yes\n");
726f364ec65SWenchao Xia     }
727f364ec65SWenchao Xia 
728f364ec65SWenchao Xia     if (info->has_cluster_size) {
7295b917044SWenchao Xia         func_fprintf(f, "cluster_size: %" PRId64 "\n",
7305b917044SWenchao Xia                        info->cluster_size);
731f364ec65SWenchao Xia     }
732f364ec65SWenchao Xia 
733f364ec65SWenchao Xia     if (info->has_dirty_flag && info->dirty_flag) {
7345b917044SWenchao Xia         func_fprintf(f, "cleanly shut down: no\n");
735f364ec65SWenchao Xia     }
736f364ec65SWenchao Xia 
737f364ec65SWenchao Xia     if (info->has_backing_filename) {
7385b917044SWenchao Xia         func_fprintf(f, "backing file: %s", info->backing_filename);
7395c9d9ca5SJohn Snow         if (!info->has_full_backing_filename) {
7405c9d9ca5SJohn Snow             func_fprintf(f, " (cannot determine actual path)");
7415c9d9ca5SJohn Snow         } else if (strcmp(info->backing_filename,
7425c9d9ca5SJohn Snow                           info->full_backing_filename) != 0) {
7435b917044SWenchao Xia             func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
744f364ec65SWenchao Xia         }
7455b917044SWenchao Xia         func_fprintf(f, "\n");
746f364ec65SWenchao Xia         if (info->has_backing_filename_format) {
7475b917044SWenchao Xia             func_fprintf(f, "backing file format: %s\n",
7485b917044SWenchao Xia                          info->backing_filename_format);
749f364ec65SWenchao Xia         }
750f364ec65SWenchao Xia     }
751f364ec65SWenchao Xia 
752f364ec65SWenchao Xia     if (info->has_snapshots) {
753f364ec65SWenchao Xia         SnapshotInfoList *elem;
754f364ec65SWenchao Xia 
7555b917044SWenchao Xia         func_fprintf(f, "Snapshot list:\n");
7565b917044SWenchao Xia         bdrv_snapshot_dump(func_fprintf, f, NULL);
7575b917044SWenchao Xia         func_fprintf(f, "\n");
758f364ec65SWenchao Xia 
759f364ec65SWenchao Xia         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
760f364ec65SWenchao Xia          * we convert to the block layer's native QEMUSnapshotInfo for now.
761f364ec65SWenchao Xia          */
762f364ec65SWenchao Xia         for (elem = info->snapshots; elem; elem = elem->next) {
763f364ec65SWenchao Xia             QEMUSnapshotInfo sn = {
764f364ec65SWenchao Xia                 .vm_state_size = elem->value->vm_state_size,
765f364ec65SWenchao Xia                 .date_sec = elem->value->date_sec,
766f364ec65SWenchao Xia                 .date_nsec = elem->value->date_nsec,
767f364ec65SWenchao Xia                 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
768f364ec65SWenchao Xia                                  elem->value->vm_clock_nsec,
769f364ec65SWenchao Xia             };
770f364ec65SWenchao Xia 
771f364ec65SWenchao Xia             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
772f364ec65SWenchao Xia             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
7735b917044SWenchao Xia             bdrv_snapshot_dump(func_fprintf, f, &sn);
7745b917044SWenchao Xia             func_fprintf(f, "\n");
775f364ec65SWenchao Xia         }
776f364ec65SWenchao Xia     }
777a8d8ecb7SMax Reitz 
778a8d8ecb7SMax Reitz     if (info->has_format_specific) {
779a8d8ecb7SMax Reitz         func_fprintf(f, "Format specific information:\n");
780a8d8ecb7SMax Reitz         bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);
781a8d8ecb7SMax Reitz     }
782f364ec65SWenchao Xia }
783