xref: /qemu/block/qapi.c (revision f7946da2)
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"
32b3db211fSDaniel P. Berrange #include "qapi/qobject-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 
37c83f9fbaSKevin Wolf BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
38c83f9fbaSKevin Wolf                                         BlockDriverState *bs, Error **errp)
39c13163fbSBenoît Canet {
40d5a8ee60SAlberto Garcia     ImageInfo **p_image_info;
41d5a8ee60SAlberto Garcia     BlockDriverState *bs0;
42c13163fbSBenoît Canet     BlockDeviceInfo *info = g_malloc0(sizeof(*info));
43c13163fbSBenoît Canet 
44c13163fbSBenoît Canet     info->file                   = g_strdup(bs->filename);
45c13163fbSBenoît Canet     info->ro                     = bs->read_only;
46c13163fbSBenoît Canet     info->drv                    = g_strdup(bs->drv->format_name);
47c13163fbSBenoît Canet     info->encrypted              = bs->encrypted;
48c13163fbSBenoît Canet     info->encryption_key_missing = bdrv_key_required(bs);
49c13163fbSBenoît Canet 
509e193c5aSKevin Wolf     info->cache = g_new(BlockdevCacheInfo, 1);
519e193c5aSKevin Wolf     *info->cache = (BlockdevCacheInfo) {
52c83f9fbaSKevin Wolf         .writeback      = blk ? blk_enable_write_cache(blk) : true,
539e193c5aSKevin Wolf         .direct         = !!(bs->open_flags & BDRV_O_NOCACHE),
549e193c5aSKevin Wolf         .no_flush       = !!(bs->open_flags & BDRV_O_NO_FLUSH),
559e193c5aSKevin Wolf     };
569e193c5aSKevin Wolf 
57c13163fbSBenoît Canet     if (bs->node_name[0]) {
58c13163fbSBenoît Canet         info->has_node_name = true;
59c13163fbSBenoît Canet         info->node_name = g_strdup(bs->node_name);
60c13163fbSBenoît Canet     }
61c13163fbSBenoît Canet 
62c13163fbSBenoît Canet     if (bs->backing_file[0]) {
63c13163fbSBenoît Canet         info->has_backing_file = true;
64c13163fbSBenoît Canet         info->backing_file = g_strdup(bs->backing_file);
65c13163fbSBenoît Canet     }
66c13163fbSBenoît Canet 
67c13163fbSBenoît Canet     info->backing_file_depth = bdrv_get_backing_file_depth(bs);
68465bee1dSPeter Lieven     info->detect_zeroes = bs->detect_zeroes;
69c13163fbSBenoît Canet 
7079c719b7SKevin Wolf     if (blk && blk_get_public(blk)->throttle_state) {
71c13163fbSBenoît Canet         ThrottleConfig cfg;
7276f4afb4SAlberto Garcia 
7379c719b7SKevin Wolf         throttle_group_get_config(blk, &cfg);
7476f4afb4SAlberto Garcia 
75c13163fbSBenoît Canet         info->bps     = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
76c13163fbSBenoît Canet         info->bps_rd  = cfg.buckets[THROTTLE_BPS_READ].avg;
77c13163fbSBenoît Canet         info->bps_wr  = cfg.buckets[THROTTLE_BPS_WRITE].avg;
78c13163fbSBenoît Canet 
79c13163fbSBenoît Canet         info->iops    = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
80c13163fbSBenoît Canet         info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
81c13163fbSBenoît Canet         info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
82c13163fbSBenoît Canet 
83c13163fbSBenoît Canet         info->has_bps_max     = cfg.buckets[THROTTLE_BPS_TOTAL].max;
84c13163fbSBenoît Canet         info->bps_max         = cfg.buckets[THROTTLE_BPS_TOTAL].max;
85c13163fbSBenoît Canet         info->has_bps_rd_max  = cfg.buckets[THROTTLE_BPS_READ].max;
86c13163fbSBenoît Canet         info->bps_rd_max      = cfg.buckets[THROTTLE_BPS_READ].max;
87c13163fbSBenoît Canet         info->has_bps_wr_max  = cfg.buckets[THROTTLE_BPS_WRITE].max;
88c13163fbSBenoît Canet         info->bps_wr_max      = cfg.buckets[THROTTLE_BPS_WRITE].max;
89c13163fbSBenoît Canet 
90c13163fbSBenoît Canet         info->has_iops_max    = cfg.buckets[THROTTLE_OPS_TOTAL].max;
91c13163fbSBenoît Canet         info->iops_max        = cfg.buckets[THROTTLE_OPS_TOTAL].max;
92c13163fbSBenoît Canet         info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
93c13163fbSBenoît Canet         info->iops_rd_max     = cfg.buckets[THROTTLE_OPS_READ].max;
94c13163fbSBenoît Canet         info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
95c13163fbSBenoît Canet         info->iops_wr_max     = cfg.buckets[THROTTLE_OPS_WRITE].max;
96c13163fbSBenoît Canet 
97398befdfSAlberto Garcia         info->has_bps_max_length     = info->has_bps_max;
98398befdfSAlberto Garcia         info->bps_max_length         =
99398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
100398befdfSAlberto Garcia         info->has_bps_rd_max_length  = info->has_bps_rd_max;
101398befdfSAlberto Garcia         info->bps_rd_max_length      =
102398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_READ].burst_length;
103398befdfSAlberto Garcia         info->has_bps_wr_max_length  = info->has_bps_wr_max;
104398befdfSAlberto Garcia         info->bps_wr_max_length      =
105398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
106398befdfSAlberto Garcia 
107398befdfSAlberto Garcia         info->has_iops_max_length    = info->has_iops_max;
108398befdfSAlberto Garcia         info->iops_max_length        =
109398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
110398befdfSAlberto Garcia         info->has_iops_rd_max_length = info->has_iops_rd_max;
111398befdfSAlberto Garcia         info->iops_rd_max_length     =
112398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_READ].burst_length;
113398befdfSAlberto Garcia         info->has_iops_wr_max_length = info->has_iops_wr_max;
114398befdfSAlberto Garcia         info->iops_wr_max_length     =
115398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
116398befdfSAlberto Garcia 
117c13163fbSBenoît Canet         info->has_iops_size = cfg.op_size;
118c13163fbSBenoît Canet         info->iops_size = cfg.op_size;
119b8fe1694SAlberto Garcia 
120b8fe1694SAlberto Garcia         info->has_group = true;
12179c719b7SKevin Wolf         info->group = g_strdup(throttle_group_get_name(blk));
122c13163fbSBenoît Canet     }
123c13163fbSBenoît Canet 
124e2462113SFrancesco Romani     info->write_threshold = bdrv_write_threshold_get(bs);
125e2462113SFrancesco Romani 
126d5a8ee60SAlberto Garcia     bs0 = bs;
127d5a8ee60SAlberto Garcia     p_image_info = &info->image;
128d5a8ee60SAlberto Garcia     while (1) {
129d5a8ee60SAlberto Garcia         Error *local_err = NULL;
130d5a8ee60SAlberto Garcia         bdrv_query_image_info(bs0, p_image_info, &local_err);
131d5a8ee60SAlberto Garcia         if (local_err) {
132d5a8ee60SAlberto Garcia             error_propagate(errp, local_err);
133d5a8ee60SAlberto Garcia             qapi_free_BlockDeviceInfo(info);
134d5a8ee60SAlberto Garcia             return NULL;
135d5a8ee60SAlberto Garcia         }
136760e0063SKevin Wolf         if (bs0->drv && bs0->backing) {
137760e0063SKevin Wolf             bs0 = bs0->backing->bs;
138d5a8ee60SAlberto Garcia             (*p_image_info)->has_backing_image = true;
139d5a8ee60SAlberto Garcia             p_image_info = &((*p_image_info)->backing_image);
140d5a8ee60SAlberto Garcia         } else {
141d5a8ee60SAlberto Garcia             break;
142d5a8ee60SAlberto Garcia         }
143d5a8ee60SAlberto Garcia     }
144d5a8ee60SAlberto Garcia 
145c13163fbSBenoît Canet     return info;
146c13163fbSBenoît Canet }
147c13163fbSBenoît Canet 
148fb0ed453SWenchao Xia /*
149fb0ed453SWenchao Xia  * Returns 0 on success, with *p_list either set to describe snapshot
150fb0ed453SWenchao Xia  * information, or NULL because there are no snapshots.  Returns -errno on
151fb0ed453SWenchao Xia  * error, with *p_list untouched.
152fb0ed453SWenchao Xia  */
153fb0ed453SWenchao Xia int bdrv_query_snapshot_info_list(BlockDriverState *bs,
154fb0ed453SWenchao Xia                                   SnapshotInfoList **p_list,
155fb0ed453SWenchao Xia                                   Error **errp)
156f364ec65SWenchao Xia {
157f364ec65SWenchao Xia     int i, sn_count;
158f364ec65SWenchao Xia     QEMUSnapshotInfo *sn_tab = NULL;
159fb0ed453SWenchao Xia     SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
160fb0ed453SWenchao Xia     SnapshotInfo *info;
161fb0ed453SWenchao Xia 
162f364ec65SWenchao Xia     sn_count = bdrv_snapshot_list(bs, &sn_tab);
163fb0ed453SWenchao Xia     if (sn_count < 0) {
164fb0ed453SWenchao Xia         const char *dev = bdrv_get_device_name(bs);
165fb0ed453SWenchao Xia         switch (sn_count) {
166fb0ed453SWenchao Xia         case -ENOMEDIUM:
167fb0ed453SWenchao Xia             error_setg(errp, "Device '%s' is not inserted", dev);
168fb0ed453SWenchao Xia             break;
169fb0ed453SWenchao Xia         case -ENOTSUP:
170fb0ed453SWenchao Xia             error_setg(errp,
171fb0ed453SWenchao Xia                        "Device '%s' does not support internal snapshots",
172fb0ed453SWenchao Xia                        dev);
173fb0ed453SWenchao Xia             break;
174fb0ed453SWenchao Xia         default:
175fb0ed453SWenchao Xia             error_setg_errno(errp, -sn_count,
176fb0ed453SWenchao Xia                              "Can't list snapshots of device '%s'", dev);
177fb0ed453SWenchao Xia             break;
178fb0ed453SWenchao Xia         }
179fb0ed453SWenchao Xia         return sn_count;
180fb0ed453SWenchao Xia     }
181f364ec65SWenchao Xia 
182f364ec65SWenchao Xia     for (i = 0; i < sn_count; i++) {
183fb0ed453SWenchao Xia         info = g_new0(SnapshotInfo, 1);
184fb0ed453SWenchao Xia         info->id            = g_strdup(sn_tab[i].id_str);
185fb0ed453SWenchao Xia         info->name          = g_strdup(sn_tab[i].name);
186fb0ed453SWenchao Xia         info->vm_state_size = sn_tab[i].vm_state_size;
187fb0ed453SWenchao Xia         info->date_sec      = sn_tab[i].date_sec;
188fb0ed453SWenchao Xia         info->date_nsec     = sn_tab[i].date_nsec;
189fb0ed453SWenchao Xia         info->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
190fb0ed453SWenchao Xia         info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
191f364ec65SWenchao Xia 
192fb0ed453SWenchao Xia         info_list = g_new0(SnapshotInfoList, 1);
193fb0ed453SWenchao Xia         info_list->value = info;
194f364ec65SWenchao Xia 
195f364ec65SWenchao Xia         /* XXX: waiting for the qapi to support qemu-queue.h types */
196f364ec65SWenchao Xia         if (!cur_item) {
197fb0ed453SWenchao Xia             head = cur_item = info_list;
198f364ec65SWenchao Xia         } else {
199f364ec65SWenchao Xia             cur_item->next = info_list;
200f364ec65SWenchao Xia             cur_item = info_list;
201f364ec65SWenchao Xia         }
202f364ec65SWenchao Xia 
203f364ec65SWenchao Xia     }
204f364ec65SWenchao Xia 
205f364ec65SWenchao Xia     g_free(sn_tab);
206fb0ed453SWenchao Xia     *p_list = head;
207fb0ed453SWenchao Xia     return 0;
208f364ec65SWenchao Xia }
209f364ec65SWenchao Xia 
21043526ec8SWenchao Xia /**
21143526ec8SWenchao Xia  * bdrv_query_image_info:
21243526ec8SWenchao Xia  * @bs: block device to examine
21343526ec8SWenchao Xia  * @p_info: location to store image information
21443526ec8SWenchao Xia  * @errp: location to store error information
21543526ec8SWenchao Xia  *
216553a7e87SWenchao Xia  * Store "flat" image information in @p_info.
217553a7e87SWenchao Xia  *
218553a7e87SWenchao Xia  * "Flat" means it does *not* query backing image information,
219553a7e87SWenchao Xia  * i.e. (*pinfo)->has_backing_image will be set to false and
220553a7e87SWenchao Xia  * (*pinfo)->backing_image to NULL even when the image does in fact have
221553a7e87SWenchao Xia  * a backing image.
222553a7e87SWenchao Xia  *
22343526ec8SWenchao Xia  * @p_info will be set only on success. On error, store error in @errp.
22443526ec8SWenchao Xia  */
22543526ec8SWenchao Xia void bdrv_query_image_info(BlockDriverState *bs,
22643526ec8SWenchao Xia                            ImageInfo **p_info,
22743526ec8SWenchao Xia                            Error **errp)
228f364ec65SWenchao Xia {
22952bf1e72SMarkus Armbruster     int64_t size;
23043526ec8SWenchao Xia     const char *backing_filename;
231f364ec65SWenchao Xia     BlockDriverInfo bdi;
23243526ec8SWenchao Xia     int ret;
23343526ec8SWenchao Xia     Error *err = NULL;
23452bf1e72SMarkus Armbruster     ImageInfo *info;
235f364ec65SWenchao Xia 
2361963f8d5SPaolo Bonzini     aio_context_acquire(bdrv_get_aio_context(bs));
2371963f8d5SPaolo Bonzini 
23852bf1e72SMarkus Armbruster     size = bdrv_getlength(bs);
23952bf1e72SMarkus Armbruster     if (size < 0) {
2409adceb02SFam Zheng         error_setg_errno(errp, -size, "Can't get image size '%s'",
2419adceb02SFam Zheng                          bs->exact_filename);
2421963f8d5SPaolo Bonzini         goto out;
24352bf1e72SMarkus Armbruster     }
244f364ec65SWenchao Xia 
24552bf1e72SMarkus Armbruster     info = g_new0(ImageInfo, 1);
24643526ec8SWenchao Xia     info->filename        = g_strdup(bs->filename);
247f364ec65SWenchao Xia     info->format          = g_strdup(bdrv_get_format_name(bs));
24852bf1e72SMarkus Armbruster     info->virtual_size    = size;
249f364ec65SWenchao Xia     info->actual_size     = bdrv_get_allocated_file_size(bs);
250f364ec65SWenchao Xia     info->has_actual_size = info->actual_size >= 0;
251f364ec65SWenchao Xia     if (bdrv_is_encrypted(bs)) {
252f364ec65SWenchao Xia         info->encrypted = true;
253f364ec65SWenchao Xia         info->has_encrypted = true;
254f364ec65SWenchao Xia     }
255f364ec65SWenchao Xia     if (bdrv_get_info(bs, &bdi) >= 0) {
256f364ec65SWenchao Xia         if (bdi.cluster_size != 0) {
257f364ec65SWenchao Xia             info->cluster_size = bdi.cluster_size;
258f364ec65SWenchao Xia             info->has_cluster_size = true;
259f364ec65SWenchao Xia         }
260f364ec65SWenchao Xia         info->dirty_flag = bdi.is_dirty;
261f364ec65SWenchao Xia         info->has_dirty_flag = true;
262f364ec65SWenchao Xia     }
263eae041feSMax Reitz     info->format_specific     = bdrv_get_specific_info(bs);
264eae041feSMax Reitz     info->has_format_specific = info->format_specific != NULL;
265eae041feSMax Reitz 
26643526ec8SWenchao Xia     backing_filename = bs->backing_file;
267f364ec65SWenchao Xia     if (backing_filename[0] != '\0') {
2689a29e18fSJeff Cody         char *backing_filename2 = g_malloc0(PATH_MAX);
269f364ec65SWenchao Xia         info->backing_filename = g_strdup(backing_filename);
270f364ec65SWenchao Xia         info->has_backing_filename = true;
2719a29e18fSJeff Cody         bdrv_get_full_backing_filename(bs, backing_filename2, PATH_MAX, &err);
2729f07429eSMax Reitz         if (err) {
273a5002d53SJohn Snow             /* Can't reconstruct the full backing filename, so we must omit
274a5002d53SJohn Snow              * this field and apply a Best Effort to this query. */
275564d64bdSJeff Cody             g_free(backing_filename2);
276a5002d53SJohn Snow             backing_filename2 = NULL;
277a5002d53SJohn Snow             error_free(err);
2780fa296ebSFam Zheng             err = NULL;
2799f07429eSMax Reitz         }
280f364ec65SWenchao Xia 
28112dcb1c0SJohn Snow         /* Always report the full_backing_filename if present, even if it's the
28212dcb1c0SJohn Snow          * same as backing_filename. That they are same is useful info. */
28312dcb1c0SJohn Snow         if (backing_filename2) {
28412dcb1c0SJohn Snow             info->full_backing_filename = g_strdup(backing_filename2);
285f364ec65SWenchao Xia             info->has_full_backing_filename = true;
286f364ec65SWenchao Xia         }
287f364ec65SWenchao Xia 
288f364ec65SWenchao Xia         if (bs->backing_format[0]) {
289f364ec65SWenchao Xia             info->backing_filename_format = g_strdup(bs->backing_format);
290f364ec65SWenchao Xia             info->has_backing_filename_format = true;
291f364ec65SWenchao Xia         }
292564d64bdSJeff Cody         g_free(backing_filename2);
293f364ec65SWenchao Xia     }
29443526ec8SWenchao Xia 
29543526ec8SWenchao Xia     ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
29643526ec8SWenchao Xia     switch (ret) {
29743526ec8SWenchao Xia     case 0:
29843526ec8SWenchao Xia         if (info->snapshots) {
29943526ec8SWenchao Xia             info->has_snapshots = true;
30043526ec8SWenchao Xia         }
30143526ec8SWenchao Xia         break;
30243526ec8SWenchao Xia     /* recoverable error */
30343526ec8SWenchao Xia     case -ENOMEDIUM:
30443526ec8SWenchao Xia     case -ENOTSUP:
30543526ec8SWenchao Xia         error_free(err);
30643526ec8SWenchao Xia         break;
30743526ec8SWenchao Xia     default:
30843526ec8SWenchao Xia         error_propagate(errp, err);
30943526ec8SWenchao Xia         qapi_free_ImageInfo(info);
3101963f8d5SPaolo Bonzini         goto out;
31143526ec8SWenchao Xia     }
31243526ec8SWenchao Xia 
31343526ec8SWenchao Xia     *p_info = info;
3141963f8d5SPaolo Bonzini 
3151963f8d5SPaolo Bonzini out:
3161963f8d5SPaolo Bonzini     aio_context_release(bdrv_get_aio_context(bs));
317f364ec65SWenchao Xia }
318f364ec65SWenchao Xia 
319553a7e87SWenchao Xia /* @p_info will be set only on success. */
320d829a211SMarkus Armbruster static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
321553a7e87SWenchao Xia                             Error **errp)
322f364ec65SWenchao Xia {
323f364ec65SWenchao Xia     BlockInfo *info = g_malloc0(sizeof(*info));
324d829a211SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
325d829a211SMarkus Armbruster     info->device = g_strdup(blk_name(blk));
326f364ec65SWenchao Xia     info->type = g_strdup("unknown");
327a7f53e26SMarkus Armbruster     info->locked = blk_dev_is_medium_locked(blk);
328a7f53e26SMarkus Armbruster     info->removable = blk_dev_has_removable_media(blk);
329f364ec65SWenchao Xia 
330327032ceSMax Reitz     if (blk_dev_has_tray(blk)) {
331f364ec65SWenchao Xia         info->has_tray_open = true;
332a7f53e26SMarkus Armbruster         info->tray_open = blk_dev_is_tray_open(blk);
333f364ec65SWenchao Xia     }
334f364ec65SWenchao Xia 
335373340b2SMax Reitz     if (blk_iostatus_is_enabled(blk)) {
336f364ec65SWenchao Xia         info->has_io_status = true;
337373340b2SMax Reitz         info->io_status = blk_iostatus(blk);
338f364ec65SWenchao Xia     }
339f364ec65SWenchao Xia 
3405433c24fSMax Reitz     if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
34121b56835SFam Zheng         info->has_dirty_bitmaps = true;
34221b56835SFam Zheng         info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
34321b56835SFam Zheng     }
34421b56835SFam Zheng 
3455433c24fSMax Reitz     if (bs && bs->drv) {
346f364ec65SWenchao Xia         info->has_inserted = true;
347c83f9fbaSKevin Wolf         info->inserted = bdrv_block_device_info(blk, bs, errp);
348d5a8ee60SAlberto Garcia         if (info->inserted == NULL) {
349553a7e87SWenchao Xia             goto err;
350f364ec65SWenchao Xia         }
351553a7e87SWenchao Xia     }
352553a7e87SWenchao Xia 
353553a7e87SWenchao Xia     *p_info = info;
354553a7e87SWenchao Xia     return;
355553a7e87SWenchao Xia 
356553a7e87SWenchao Xia  err:
357553a7e87SWenchao Xia     qapi_free_BlockInfo(info);
358f364ec65SWenchao Xia }
359f364ec65SWenchao Xia 
36054302156SMax Reitz static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
361f364ec65SWenchao Xia {
3622b77e60aSKevin Wolf     BlockAcctStats *stats = blk_get_stats(blk);
363979e9b03SAlberto Garcia     BlockAcctTimedStats *ts = NULL;
3647f0e9da6SMax Reitz 
36554302156SMax Reitz     ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
36654302156SMax Reitz     ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
36754302156SMax Reitz     ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
36854302156SMax Reitz     ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
3697ee12dafSAlberto Garcia 
37054302156SMax Reitz     ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
37154302156SMax Reitz     ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
37254302156SMax Reitz     ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
3737ee12dafSAlberto Garcia 
37454302156SMax Reitz     ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
37554302156SMax Reitz     ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
37654302156SMax Reitz     ds->invalid_flush_operations =
3777ee12dafSAlberto Garcia         stats->invalid_ops[BLOCK_ACCT_FLUSH];
3787ee12dafSAlberto Garcia 
37954302156SMax Reitz     ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
38054302156SMax Reitz     ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
38154302156SMax Reitz     ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
38254302156SMax Reitz     ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
38354302156SMax Reitz     ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
38454302156SMax Reitz     ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
385cb38fffbSAlberto Garcia 
38654302156SMax Reitz     ds->has_idle_time_ns = stats->last_access_time_ns > 0;
38754302156SMax Reitz     if (ds->has_idle_time_ns) {
38854302156SMax Reitz         ds->idle_time_ns = block_acct_idle_time_ns(stats);
389cb38fffbSAlberto Garcia     }
390362e9299SAlberto Garcia 
39154302156SMax Reitz     ds->account_invalid = stats->account_invalid;
39254302156SMax Reitz     ds->account_failed = stats->account_failed;
393979e9b03SAlberto Garcia 
394979e9b03SAlberto Garcia     while ((ts = block_acct_interval_next(stats, ts))) {
395979e9b03SAlberto Garcia         BlockDeviceTimedStatsList *timed_stats =
396979e9b03SAlberto Garcia             g_malloc0(sizeof(*timed_stats));
397979e9b03SAlberto Garcia         BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
39854302156SMax Reitz         timed_stats->next = ds->timed_stats;
399979e9b03SAlberto Garcia         timed_stats->value = dev_stats;
40054302156SMax Reitz         ds->timed_stats = timed_stats;
401979e9b03SAlberto Garcia 
402979e9b03SAlberto Garcia         TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
403979e9b03SAlberto Garcia         TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
404979e9b03SAlberto Garcia         TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
405979e9b03SAlberto Garcia 
406979e9b03SAlberto Garcia         dev_stats->interval_length = ts->interval_length;
407979e9b03SAlberto Garcia 
408979e9b03SAlberto Garcia         dev_stats->min_rd_latency_ns = timed_average_min(rd);
409979e9b03SAlberto Garcia         dev_stats->max_rd_latency_ns = timed_average_max(rd);
410979e9b03SAlberto Garcia         dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
411979e9b03SAlberto Garcia 
412979e9b03SAlberto Garcia         dev_stats->min_wr_latency_ns = timed_average_min(wr);
413979e9b03SAlberto Garcia         dev_stats->max_wr_latency_ns = timed_average_max(wr);
414979e9b03SAlberto Garcia         dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
415979e9b03SAlberto Garcia 
416979e9b03SAlberto Garcia         dev_stats->min_flush_latency_ns = timed_average_min(fl);
417979e9b03SAlberto Garcia         dev_stats->max_flush_latency_ns = timed_average_max(fl);
418979e9b03SAlberto Garcia         dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
41996e4dedaSAlberto Garcia 
42096e4dedaSAlberto Garcia         dev_stats->avg_rd_queue_depth =
42196e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_READ);
42296e4dedaSAlberto Garcia         dev_stats->avg_wr_queue_depth =
42396e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
424979e9b03SAlberto Garcia     }
4257f0e9da6SMax Reitz }
426f364ec65SWenchao Xia 
42720a6d768SDou Liyang static BlockStats *bdrv_query_bds_stats(const BlockDriverState *bs,
4282b77e60aSKevin Wolf                                  bool query_backing)
4292b77e60aSKevin Wolf {
43020a6d768SDou Liyang     BlockStats *s = NULL;
43120a6d768SDou Liyang 
43220a6d768SDou Liyang     s = g_malloc0(sizeof(*s));
43320a6d768SDou Liyang     s->stats = g_malloc0(sizeof(*s->stats));
43420a6d768SDou Liyang 
43520a6d768SDou Liyang     if (!bs) {
43620a6d768SDou Liyang         return s;
43720a6d768SDou Liyang     }
43820a6d768SDou Liyang 
4392b77e60aSKevin Wolf     if (bdrv_get_node_name(bs)[0]) {
4402b77e60aSKevin Wolf         s->has_node_name = true;
4412b77e60aSKevin Wolf         s->node_name = g_strdup(bdrv_get_node_name(bs));
4422b77e60aSKevin Wolf     }
4432b77e60aSKevin Wolf 
444*f7946da2SPaolo Bonzini     s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
44553d8f9d8SMax Reitz 
446f364ec65SWenchao Xia     if (bs->file) {
447f364ec65SWenchao Xia         s->has_parent = true;
44820a6d768SDou Liyang         s->parent = bdrv_query_bds_stats(bs->file->bs, query_backing);
449f364ec65SWenchao Xia     }
450f364ec65SWenchao Xia 
451760e0063SKevin Wolf     if (query_backing && bs->backing) {
452c8059b97SFam Zheng         s->has_backing = true;
45320a6d768SDou Liyang         s->backing = bdrv_query_bds_stats(bs->backing->bs, query_backing);
454c8059b97SFam Zheng     }
455c8059b97SFam Zheng 
45620a6d768SDou Liyang     return s;
457b07363a1SKevin Wolf }
458b07363a1SKevin Wolf 
459f364ec65SWenchao Xia BlockInfoList *qmp_query_block(Error **errp)
460f364ec65SWenchao Xia {
461f364ec65SWenchao Xia     BlockInfoList *head = NULL, **p_next = &head;
462d829a211SMarkus Armbruster     BlockBackend *blk;
463553a7e87SWenchao Xia     Error *local_err = NULL;
464f364ec65SWenchao Xia 
465d829a211SMarkus Armbruster     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
466f364ec65SWenchao Xia         BlockInfoList *info = g_malloc0(sizeof(*info));
467d829a211SMarkus Armbruster         bdrv_query_info(blk, &info->value, &local_err);
46884d18f06SMarkus Armbruster         if (local_err) {
469553a7e87SWenchao Xia             error_propagate(errp, local_err);
470903c341dSMarkus Armbruster             g_free(info);
471903c341dSMarkus Armbruster             qapi_free_BlockInfoList(head);
472903c341dSMarkus Armbruster             return NULL;
473553a7e87SWenchao Xia         }
474f364ec65SWenchao Xia 
475f364ec65SWenchao Xia         *p_next = info;
476f364ec65SWenchao Xia         p_next = &info->next;
477f364ec65SWenchao Xia     }
478f364ec65SWenchao Xia 
479f364ec65SWenchao Xia     return head;
480f364ec65SWenchao Xia }
481f364ec65SWenchao Xia 
482f71eaa74SFam Zheng BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
483f71eaa74SFam Zheng                                      bool query_nodes,
484f71eaa74SFam Zheng                                      Error **errp)
485f364ec65SWenchao Xia {
486f364ec65SWenchao Xia     BlockStatsList *head = NULL, **p_next = &head;
487a6baa608SDou Liyang     BlockBackend *blk;
488a6baa608SDou Liyang     BlockDriverState *bs;
489f364ec65SWenchao Xia 
490f71eaa74SFam Zheng     /* Just to be safe if query_nodes is not always initialized */
491a6baa608SDou Liyang     if (has_query_nodes && query_nodes) {
492a6baa608SDou Liyang         for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
493f364ec65SWenchao Xia             BlockStatsList *info = g_malloc0(sizeof(*info));
494a6baa608SDou Liyang             AioContext *ctx = bdrv_get_aio_context(bs);
49513344f3aSStefan Hajnoczi 
49613344f3aSStefan Hajnoczi             aio_context_acquire(ctx);
497a6baa608SDou Liyang             info->value = bdrv_query_bds_stats(bs, false);
49813344f3aSStefan Hajnoczi             aio_context_release(ctx);
499f364ec65SWenchao Xia 
500f364ec65SWenchao Xia             *p_next = info;
501f364ec65SWenchao Xia             p_next = &info->next;
502f364ec65SWenchao Xia         }
503a6baa608SDou Liyang     } else {
504a6baa608SDou Liyang         for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
505a6baa608SDou Liyang             BlockStatsList *info = g_malloc0(sizeof(*info));
506a6baa608SDou Liyang             AioContext *ctx = blk_get_aio_context(blk);
507a6baa608SDou Liyang             BlockStats *s;
508a6baa608SDou Liyang 
509a6baa608SDou Liyang             aio_context_acquire(ctx);
510a6baa608SDou Liyang             s = bdrv_query_bds_stats(blk_bs(blk), true);
511a6baa608SDou Liyang             s->has_device = true;
512a6baa608SDou Liyang             s->device = g_strdup(blk_name(blk));
513a6baa608SDou Liyang             bdrv_query_blk_stats(s->stats, blk);
514a6baa608SDou Liyang             aio_context_release(ctx);
515a6baa608SDou Liyang 
516a6baa608SDou Liyang             info->value = s;
517a6baa608SDou Liyang             *p_next = info;
518a6baa608SDou Liyang             p_next = &info->next;
519a6baa608SDou Liyang         }
520a6baa608SDou Liyang     }
521f364ec65SWenchao Xia 
522f364ec65SWenchao Xia     return head;
523f364ec65SWenchao Xia }
524f364ec65SWenchao Xia 
525f364ec65SWenchao Xia #define NB_SUFFIXES 4
526f364ec65SWenchao Xia 
527f364ec65SWenchao Xia static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
528f364ec65SWenchao Xia {
5292c20fa2cSStefan Weil     static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'};
530f364ec65SWenchao Xia     int64_t base;
531f364ec65SWenchao Xia     int i;
532f364ec65SWenchao Xia 
533f364ec65SWenchao Xia     if (size <= 999) {
534f364ec65SWenchao Xia         snprintf(buf, buf_size, "%" PRId64, size);
535f364ec65SWenchao Xia     } else {
536f364ec65SWenchao Xia         base = 1024;
537f364ec65SWenchao Xia         for (i = 0; i < NB_SUFFIXES; i++) {
538f364ec65SWenchao Xia             if (size < (10 * base)) {
539f364ec65SWenchao Xia                 snprintf(buf, buf_size, "%0.1f%c",
540f364ec65SWenchao Xia                          (double)size / base,
541f364ec65SWenchao Xia                          suffixes[i]);
542f364ec65SWenchao Xia                 break;
543f364ec65SWenchao Xia             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
544f364ec65SWenchao Xia                 snprintf(buf, buf_size, "%" PRId64 "%c",
545f364ec65SWenchao Xia                          ((size + (base >> 1)) / base),
546f364ec65SWenchao Xia                          suffixes[i]);
547f364ec65SWenchao Xia                 break;
548f364ec65SWenchao Xia             }
549f364ec65SWenchao Xia             base = base * 1024;
550f364ec65SWenchao Xia         }
551f364ec65SWenchao Xia     }
552f364ec65SWenchao Xia     return buf;
553f364ec65SWenchao Xia }
554f364ec65SWenchao Xia 
5555b917044SWenchao Xia void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
5565b917044SWenchao Xia                         QEMUSnapshotInfo *sn)
557f364ec65SWenchao Xia {
558f364ec65SWenchao Xia     char buf1[128], date_buf[128], clock_buf[128];
559f364ec65SWenchao Xia     struct tm tm;
560f364ec65SWenchao Xia     time_t ti;
561f364ec65SWenchao Xia     int64_t secs;
562f364ec65SWenchao Xia 
563f364ec65SWenchao Xia     if (!sn) {
5645b917044SWenchao Xia         func_fprintf(f,
565f364ec65SWenchao Xia                      "%-10s%-20s%7s%20s%15s",
566f364ec65SWenchao Xia                      "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
567f364ec65SWenchao Xia     } else {
568f364ec65SWenchao Xia         ti = sn->date_sec;
569f364ec65SWenchao Xia         localtime_r(&ti, &tm);
570f364ec65SWenchao Xia         strftime(date_buf, sizeof(date_buf),
571f364ec65SWenchao Xia                  "%Y-%m-%d %H:%M:%S", &tm);
572f364ec65SWenchao Xia         secs = sn->vm_clock_nsec / 1000000000;
573f364ec65SWenchao Xia         snprintf(clock_buf, sizeof(clock_buf),
574f364ec65SWenchao Xia                  "%02d:%02d:%02d.%03d",
575f364ec65SWenchao Xia                  (int)(secs / 3600),
576f364ec65SWenchao Xia                  (int)((secs / 60) % 60),
577f364ec65SWenchao Xia                  (int)(secs % 60),
578f364ec65SWenchao Xia                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
5795b917044SWenchao Xia         func_fprintf(f,
580f364ec65SWenchao Xia                      "%-10s%-20s%7s%20s%15s",
581f364ec65SWenchao Xia                      sn->id_str, sn->name,
5825b917044SWenchao Xia                      get_human_readable_size(buf1, sizeof(buf1),
5835b917044SWenchao Xia                                              sn->vm_state_size),
584f364ec65SWenchao Xia                      date_buf,
585f364ec65SWenchao Xia                      clock_buf);
586f364ec65SWenchao Xia     }
587f364ec65SWenchao Xia }
588f364ec65SWenchao Xia 
589a8d8ecb7SMax Reitz static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
590a8d8ecb7SMax Reitz                        QDict *dict);
591a8d8ecb7SMax Reitz static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
592a8d8ecb7SMax Reitz                        QList *list);
593a8d8ecb7SMax Reitz 
594a8d8ecb7SMax Reitz static void dump_qobject(fprintf_function func_fprintf, void *f,
595a8d8ecb7SMax Reitz                          int comp_indent, QObject *obj)
596a8d8ecb7SMax Reitz {
597a8d8ecb7SMax Reitz     switch (qobject_type(obj)) {
598a8d8ecb7SMax Reitz         case QTYPE_QINT: {
599a8d8ecb7SMax Reitz             QInt *value = qobject_to_qint(obj);
600a8d8ecb7SMax Reitz             func_fprintf(f, "%" PRId64, qint_get_int(value));
601a8d8ecb7SMax Reitz             break;
602a8d8ecb7SMax Reitz         }
603a8d8ecb7SMax Reitz         case QTYPE_QSTRING: {
604a8d8ecb7SMax Reitz             QString *value = qobject_to_qstring(obj);
605a8d8ecb7SMax Reitz             func_fprintf(f, "%s", qstring_get_str(value));
606a8d8ecb7SMax Reitz             break;
607a8d8ecb7SMax Reitz         }
608a8d8ecb7SMax Reitz         case QTYPE_QDICT: {
609a8d8ecb7SMax Reitz             QDict *value = qobject_to_qdict(obj);
610a8d8ecb7SMax Reitz             dump_qdict(func_fprintf, f, comp_indent, value);
611a8d8ecb7SMax Reitz             break;
612a8d8ecb7SMax Reitz         }
613a8d8ecb7SMax Reitz         case QTYPE_QLIST: {
614a8d8ecb7SMax Reitz             QList *value = qobject_to_qlist(obj);
615a8d8ecb7SMax Reitz             dump_qlist(func_fprintf, f, comp_indent, value);
616a8d8ecb7SMax Reitz             break;
617a8d8ecb7SMax Reitz         }
618a8d8ecb7SMax Reitz         case QTYPE_QFLOAT: {
619a8d8ecb7SMax Reitz             QFloat *value = qobject_to_qfloat(obj);
620a8d8ecb7SMax Reitz             func_fprintf(f, "%g", qfloat_get_double(value));
621a8d8ecb7SMax Reitz             break;
622a8d8ecb7SMax Reitz         }
623a8d8ecb7SMax Reitz         case QTYPE_QBOOL: {
624a8d8ecb7SMax Reitz             QBool *value = qobject_to_qbool(obj);
625fc48ffc3SEric Blake             func_fprintf(f, "%s", qbool_get_bool(value) ? "true" : "false");
626a8d8ecb7SMax Reitz             break;
627a8d8ecb7SMax Reitz         }
628a8d8ecb7SMax Reitz         default:
629a8d8ecb7SMax Reitz             abort();
630a8d8ecb7SMax Reitz     }
631a8d8ecb7SMax Reitz }
632a8d8ecb7SMax Reitz 
633a8d8ecb7SMax Reitz static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
634a8d8ecb7SMax Reitz                        QList *list)
635a8d8ecb7SMax Reitz {
636a8d8ecb7SMax Reitz     const QListEntry *entry;
637a8d8ecb7SMax Reitz     int i = 0;
638a8d8ecb7SMax Reitz 
639a8d8ecb7SMax Reitz     for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
6401310a3d3SEric Blake         QType type = qobject_type(entry->value);
641a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
642853ccfedSPeter Xu         func_fprintf(f, "%*s[%i]:%c", indentation * 4, "", i,
643853ccfedSPeter Xu                      composite ? '\n' : ' ');
644a8d8ecb7SMax Reitz         dump_qobject(func_fprintf, f, indentation + 1, entry->value);
645a8d8ecb7SMax Reitz         if (!composite) {
646a8d8ecb7SMax Reitz             func_fprintf(f, "\n");
647a8d8ecb7SMax Reitz         }
648a8d8ecb7SMax Reitz     }
649a8d8ecb7SMax Reitz }
650a8d8ecb7SMax Reitz 
651a8d8ecb7SMax Reitz static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
652a8d8ecb7SMax Reitz                        QDict *dict)
653a8d8ecb7SMax Reitz {
654a8d8ecb7SMax Reitz     const QDictEntry *entry;
655a8d8ecb7SMax Reitz 
656a8d8ecb7SMax Reitz     for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
6571310a3d3SEric Blake         QType type = qobject_type(entry->value);
658a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
6595eda6227SPeter Xu         char *key = g_malloc(strlen(entry->key) + 1);
660a8d8ecb7SMax Reitz         int i;
661a8d8ecb7SMax Reitz 
662a8d8ecb7SMax Reitz         /* replace dashes with spaces in key (variable) names */
663a8d8ecb7SMax Reitz         for (i = 0; entry->key[i]; i++) {
664a8d8ecb7SMax Reitz             key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
665a8d8ecb7SMax Reitz         }
666a8d8ecb7SMax Reitz         key[i] = 0;
667853ccfedSPeter Xu         func_fprintf(f, "%*s%s:%c", indentation * 4, "", key,
668853ccfedSPeter Xu                      composite ? '\n' : ' ');
669a8d8ecb7SMax Reitz         dump_qobject(func_fprintf, f, indentation + 1, entry->value);
670a8d8ecb7SMax Reitz         if (!composite) {
671a8d8ecb7SMax Reitz             func_fprintf(f, "\n");
672a8d8ecb7SMax Reitz         }
6735eda6227SPeter Xu         g_free(key);
674a8d8ecb7SMax Reitz     }
675a8d8ecb7SMax Reitz }
676a8d8ecb7SMax Reitz 
677a8d8ecb7SMax Reitz void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
678a8d8ecb7SMax Reitz                                    ImageInfoSpecific *info_spec)
679a8d8ecb7SMax Reitz {
680a8d8ecb7SMax Reitz     QObject *obj, *data;
6817d5e199aSDaniel P. Berrange     Visitor *v = qobject_output_visitor_new(&obj);
682a8d8ecb7SMax Reitz 
6833b098d56SEric Blake     visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
6843b098d56SEric Blake     visit_complete(v, &obj);
685a8d8ecb7SMax Reitz     data = qdict_get(qobject_to_qdict(obj), "data");
686a8d8ecb7SMax Reitz     dump_qobject(func_fprintf, f, 1, data);
6873ac2f2f7SPino Toscano     qobject_decref(obj);
6883b098d56SEric Blake     visit_free(v);
689a8d8ecb7SMax Reitz }
690a8d8ecb7SMax Reitz 
6915b917044SWenchao Xia void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
6925b917044SWenchao Xia                           ImageInfo *info)
693f364ec65SWenchao Xia {
694f364ec65SWenchao Xia     char size_buf[128], dsize_buf[128];
695f364ec65SWenchao Xia     if (!info->has_actual_size) {
696f364ec65SWenchao Xia         snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
697f364ec65SWenchao Xia     } else {
698f364ec65SWenchao Xia         get_human_readable_size(dsize_buf, sizeof(dsize_buf),
699f364ec65SWenchao Xia                                 info->actual_size);
700f364ec65SWenchao Xia     }
701f364ec65SWenchao Xia     get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
7025b917044SWenchao Xia     func_fprintf(f,
7035b917044SWenchao Xia                  "image: %s\n"
704f364ec65SWenchao Xia                  "file format: %s\n"
705f364ec65SWenchao Xia                  "virtual size: %s (%" PRId64 " bytes)\n"
706f364ec65SWenchao Xia                  "disk size: %s\n",
707f364ec65SWenchao Xia                  info->filename, info->format, size_buf,
708f364ec65SWenchao Xia                  info->virtual_size,
709f364ec65SWenchao Xia                  dsize_buf);
710f364ec65SWenchao Xia 
711f364ec65SWenchao Xia     if (info->has_encrypted && info->encrypted) {
7125b917044SWenchao Xia         func_fprintf(f, "encrypted: yes\n");
713f364ec65SWenchao Xia     }
714f364ec65SWenchao Xia 
715f364ec65SWenchao Xia     if (info->has_cluster_size) {
7165b917044SWenchao Xia         func_fprintf(f, "cluster_size: %" PRId64 "\n",
7175b917044SWenchao Xia                        info->cluster_size);
718f364ec65SWenchao Xia     }
719f364ec65SWenchao Xia 
720f364ec65SWenchao Xia     if (info->has_dirty_flag && info->dirty_flag) {
7215b917044SWenchao Xia         func_fprintf(f, "cleanly shut down: no\n");
722f364ec65SWenchao Xia     }
723f364ec65SWenchao Xia 
724f364ec65SWenchao Xia     if (info->has_backing_filename) {
7255b917044SWenchao Xia         func_fprintf(f, "backing file: %s", info->backing_filename);
7265c9d9ca5SJohn Snow         if (!info->has_full_backing_filename) {
7275c9d9ca5SJohn Snow             func_fprintf(f, " (cannot determine actual path)");
7285c9d9ca5SJohn Snow         } else if (strcmp(info->backing_filename,
7295c9d9ca5SJohn Snow                           info->full_backing_filename) != 0) {
7305b917044SWenchao Xia             func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
731f364ec65SWenchao Xia         }
7325b917044SWenchao Xia         func_fprintf(f, "\n");
733f364ec65SWenchao Xia         if (info->has_backing_filename_format) {
7345b917044SWenchao Xia             func_fprintf(f, "backing file format: %s\n",
7355b917044SWenchao Xia                          info->backing_filename_format);
736f364ec65SWenchao Xia         }
737f364ec65SWenchao Xia     }
738f364ec65SWenchao Xia 
739f364ec65SWenchao Xia     if (info->has_snapshots) {
740f364ec65SWenchao Xia         SnapshotInfoList *elem;
741f364ec65SWenchao Xia 
7425b917044SWenchao Xia         func_fprintf(f, "Snapshot list:\n");
7435b917044SWenchao Xia         bdrv_snapshot_dump(func_fprintf, f, NULL);
7445b917044SWenchao Xia         func_fprintf(f, "\n");
745f364ec65SWenchao Xia 
746f364ec65SWenchao Xia         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
747f364ec65SWenchao Xia          * we convert to the block layer's native QEMUSnapshotInfo for now.
748f364ec65SWenchao Xia          */
749f364ec65SWenchao Xia         for (elem = info->snapshots; elem; elem = elem->next) {
750f364ec65SWenchao Xia             QEMUSnapshotInfo sn = {
751f364ec65SWenchao Xia                 .vm_state_size = elem->value->vm_state_size,
752f364ec65SWenchao Xia                 .date_sec = elem->value->date_sec,
753f364ec65SWenchao Xia                 .date_nsec = elem->value->date_nsec,
754f364ec65SWenchao Xia                 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
755f364ec65SWenchao Xia                                  elem->value->vm_clock_nsec,
756f364ec65SWenchao Xia             };
757f364ec65SWenchao Xia 
758f364ec65SWenchao Xia             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
759f364ec65SWenchao Xia             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
7605b917044SWenchao Xia             bdrv_snapshot_dump(func_fprintf, f, &sn);
7615b917044SWenchao Xia             func_fprintf(f, "\n");
762f364ec65SWenchao Xia         }
763f364ec65SWenchao Xia     }
764a8d8ecb7SMax Reitz 
765a8d8ecb7SMax Reitz     if (info->has_format_specific) {
766a8d8ecb7SMax Reitz         func_fprintf(f, "Format specific information:\n");
767a8d8ecb7SMax Reitz         bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);
768a8d8ecb7SMax Reitz     }
769f364ec65SWenchao Xia }
770