xref: /qemu/block/qapi.c (revision 15280c36)
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"
32e688df6bSMarkus Armbruster #include "qapi/error.h"
33b3db211fSDaniel P. Berrange #include "qapi/qobject-output-visitor.h"
346b673957SMarkus Armbruster #include "qapi/qmp/qbool.h"
35*15280c36SMarkus Armbruster #include "qapi/qmp/qnum.h"
366b673957SMarkus Armbruster #include "qapi/qmp/qstring.h"
37d829a211SMarkus Armbruster #include "sysemu/block-backend.h"
38f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
39f364ec65SWenchao Xia 
40c83f9fbaSKevin Wolf BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
41c83f9fbaSKevin Wolf                                         BlockDriverState *bs, Error **errp)
42c13163fbSBenoît Canet {
43d5a8ee60SAlberto Garcia     ImageInfo **p_image_info;
44d5a8ee60SAlberto Garcia     BlockDriverState *bs0;
45d470ad42SMax Reitz     BlockDeviceInfo *info;
46c13163fbSBenoît Canet 
47d470ad42SMax Reitz     if (!bs->drv) {
48d470ad42SMax Reitz         error_setg(errp, "Block device %s is ejected", bs->node_name);
49d470ad42SMax Reitz         return NULL;
50d470ad42SMax Reitz     }
51d470ad42SMax Reitz 
52d470ad42SMax Reitz     info = g_malloc0(sizeof(*info));
53c13163fbSBenoît Canet     info->file                   = g_strdup(bs->filename);
54c13163fbSBenoît Canet     info->ro                     = bs->read_only;
55c13163fbSBenoît Canet     info->drv                    = g_strdup(bs->drv->format_name);
56c13163fbSBenoît Canet     info->encrypted              = bs->encrypted;
57c01c214bSDaniel P. Berrange     info->encryption_key_missing = false;
58c13163fbSBenoît Canet 
599e193c5aSKevin Wolf     info->cache = g_new(BlockdevCacheInfo, 1);
609e193c5aSKevin Wolf     *info->cache = (BlockdevCacheInfo) {
61c83f9fbaSKevin Wolf         .writeback      = blk ? blk_enable_write_cache(blk) : true,
629e193c5aSKevin Wolf         .direct         = !!(bs->open_flags & BDRV_O_NOCACHE),
639e193c5aSKevin Wolf         .no_flush       = !!(bs->open_flags & BDRV_O_NO_FLUSH),
649e193c5aSKevin Wolf     };
659e193c5aSKevin Wolf 
66c13163fbSBenoît Canet     if (bs->node_name[0]) {
67c13163fbSBenoît Canet         info->has_node_name = true;
68c13163fbSBenoît Canet         info->node_name = g_strdup(bs->node_name);
69c13163fbSBenoît Canet     }
70c13163fbSBenoît Canet 
71c13163fbSBenoît Canet     if (bs->backing_file[0]) {
72c13163fbSBenoît Canet         info->has_backing_file = true;
73c13163fbSBenoît Canet         info->backing_file = g_strdup(bs->backing_file);
74c13163fbSBenoît Canet     }
75c13163fbSBenoît Canet 
76465bee1dSPeter Lieven     info->detect_zeroes = bs->detect_zeroes;
77c13163fbSBenoît Canet 
78022cdc9fSManos Pitsidianakis     if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
79c13163fbSBenoît Canet         ThrottleConfig cfg;
80022cdc9fSManos Pitsidianakis         BlockBackendPublic *blkp = blk_get_public(blk);
8176f4afb4SAlberto Garcia 
82022cdc9fSManos Pitsidianakis         throttle_group_get_config(&blkp->throttle_group_member, &cfg);
8376f4afb4SAlberto Garcia 
84c13163fbSBenoît Canet         info->bps     = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
85c13163fbSBenoît Canet         info->bps_rd  = cfg.buckets[THROTTLE_BPS_READ].avg;
86c13163fbSBenoît Canet         info->bps_wr  = cfg.buckets[THROTTLE_BPS_WRITE].avg;
87c13163fbSBenoît Canet 
88c13163fbSBenoît Canet         info->iops    = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
89c13163fbSBenoît Canet         info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
90c13163fbSBenoît Canet         info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
91c13163fbSBenoît Canet 
92c13163fbSBenoît Canet         info->has_bps_max     = cfg.buckets[THROTTLE_BPS_TOTAL].max;
93c13163fbSBenoît Canet         info->bps_max         = cfg.buckets[THROTTLE_BPS_TOTAL].max;
94c13163fbSBenoît Canet         info->has_bps_rd_max  = cfg.buckets[THROTTLE_BPS_READ].max;
95c13163fbSBenoît Canet         info->bps_rd_max      = cfg.buckets[THROTTLE_BPS_READ].max;
96c13163fbSBenoît Canet         info->has_bps_wr_max  = cfg.buckets[THROTTLE_BPS_WRITE].max;
97c13163fbSBenoît Canet         info->bps_wr_max      = cfg.buckets[THROTTLE_BPS_WRITE].max;
98c13163fbSBenoît Canet 
99c13163fbSBenoît Canet         info->has_iops_max    = cfg.buckets[THROTTLE_OPS_TOTAL].max;
100c13163fbSBenoît Canet         info->iops_max        = cfg.buckets[THROTTLE_OPS_TOTAL].max;
101c13163fbSBenoît Canet         info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
102c13163fbSBenoît Canet         info->iops_rd_max     = cfg.buckets[THROTTLE_OPS_READ].max;
103c13163fbSBenoît Canet         info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
104c13163fbSBenoît Canet         info->iops_wr_max     = cfg.buckets[THROTTLE_OPS_WRITE].max;
105c13163fbSBenoît Canet 
106398befdfSAlberto Garcia         info->has_bps_max_length     = info->has_bps_max;
107398befdfSAlberto Garcia         info->bps_max_length         =
108398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
109398befdfSAlberto Garcia         info->has_bps_rd_max_length  = info->has_bps_rd_max;
110398befdfSAlberto Garcia         info->bps_rd_max_length      =
111398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_READ].burst_length;
112398befdfSAlberto Garcia         info->has_bps_wr_max_length  = info->has_bps_wr_max;
113398befdfSAlberto Garcia         info->bps_wr_max_length      =
114398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
115398befdfSAlberto Garcia 
116398befdfSAlberto Garcia         info->has_iops_max_length    = info->has_iops_max;
117398befdfSAlberto Garcia         info->iops_max_length        =
118398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
119398befdfSAlberto Garcia         info->has_iops_rd_max_length = info->has_iops_rd_max;
120398befdfSAlberto Garcia         info->iops_rd_max_length     =
121398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_READ].burst_length;
122398befdfSAlberto Garcia         info->has_iops_wr_max_length = info->has_iops_wr_max;
123398befdfSAlberto Garcia         info->iops_wr_max_length     =
124398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
125398befdfSAlberto Garcia 
126c13163fbSBenoît Canet         info->has_iops_size = cfg.op_size;
127c13163fbSBenoît Canet         info->iops_size = cfg.op_size;
128b8fe1694SAlberto Garcia 
129b8fe1694SAlberto Garcia         info->has_group = true;
130022cdc9fSManos Pitsidianakis         info->group =
131022cdc9fSManos Pitsidianakis             g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
132c13163fbSBenoît Canet     }
133c13163fbSBenoît Canet 
134e2462113SFrancesco Romani     info->write_threshold = bdrv_write_threshold_get(bs);
135e2462113SFrancesco Romani 
136d5a8ee60SAlberto Garcia     bs0 = bs;
137d5a8ee60SAlberto Garcia     p_image_info = &info->image;
138d3c8c674SKevin Wolf     info->backing_file_depth = 0;
139d5a8ee60SAlberto Garcia     while (1) {
140d5a8ee60SAlberto Garcia         Error *local_err = NULL;
141d5a8ee60SAlberto Garcia         bdrv_query_image_info(bs0, p_image_info, &local_err);
142d5a8ee60SAlberto Garcia         if (local_err) {
143d5a8ee60SAlberto Garcia             error_propagate(errp, local_err);
144d5a8ee60SAlberto Garcia             qapi_free_BlockDeviceInfo(info);
145d5a8ee60SAlberto Garcia             return NULL;
146d5a8ee60SAlberto Garcia         }
147d3c8c674SKevin Wolf 
148760e0063SKevin Wolf         if (bs0->drv && bs0->backing) {
149d3c8c674SKevin Wolf             info->backing_file_depth++;
150760e0063SKevin Wolf             bs0 = bs0->backing->bs;
151d5a8ee60SAlberto Garcia             (*p_image_info)->has_backing_image = true;
152d5a8ee60SAlberto Garcia             p_image_info = &((*p_image_info)->backing_image);
153d5a8ee60SAlberto Garcia         } else {
154d5a8ee60SAlberto Garcia             break;
155d5a8ee60SAlberto Garcia         }
156d3c8c674SKevin Wolf 
157d3c8c674SKevin Wolf         /* Skip automatically inserted nodes that the user isn't aware of for
158d3c8c674SKevin Wolf          * query-block (blk != NULL), but not for query-named-block-nodes */
1598e8eb0a9SKevin Wolf         while (blk && bs0->drv && bs0->implicit) {
160d3c8c674SKevin Wolf             bs0 = backing_bs(bs0);
1618e8eb0a9SKevin Wolf             assert(bs0);
162d3c8c674SKevin Wolf         }
163d5a8ee60SAlberto Garcia     }
164d5a8ee60SAlberto Garcia 
165c13163fbSBenoît Canet     return info;
166c13163fbSBenoît Canet }
167c13163fbSBenoît Canet 
168fb0ed453SWenchao Xia /*
169fb0ed453SWenchao Xia  * Returns 0 on success, with *p_list either set to describe snapshot
170fb0ed453SWenchao Xia  * information, or NULL because there are no snapshots.  Returns -errno on
171fb0ed453SWenchao Xia  * error, with *p_list untouched.
172fb0ed453SWenchao Xia  */
173fb0ed453SWenchao Xia int bdrv_query_snapshot_info_list(BlockDriverState *bs,
174fb0ed453SWenchao Xia                                   SnapshotInfoList **p_list,
175fb0ed453SWenchao Xia                                   Error **errp)
176f364ec65SWenchao Xia {
177f364ec65SWenchao Xia     int i, sn_count;
178f364ec65SWenchao Xia     QEMUSnapshotInfo *sn_tab = NULL;
179fb0ed453SWenchao Xia     SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
180fb0ed453SWenchao Xia     SnapshotInfo *info;
181fb0ed453SWenchao Xia 
182f364ec65SWenchao Xia     sn_count = bdrv_snapshot_list(bs, &sn_tab);
183fb0ed453SWenchao Xia     if (sn_count < 0) {
184fb0ed453SWenchao Xia         const char *dev = bdrv_get_device_name(bs);
185fb0ed453SWenchao Xia         switch (sn_count) {
186fb0ed453SWenchao Xia         case -ENOMEDIUM:
187fb0ed453SWenchao Xia             error_setg(errp, "Device '%s' is not inserted", dev);
188fb0ed453SWenchao Xia             break;
189fb0ed453SWenchao Xia         case -ENOTSUP:
190fb0ed453SWenchao Xia             error_setg(errp,
191fb0ed453SWenchao Xia                        "Device '%s' does not support internal snapshots",
192fb0ed453SWenchao Xia                        dev);
193fb0ed453SWenchao Xia             break;
194fb0ed453SWenchao Xia         default:
195fb0ed453SWenchao Xia             error_setg_errno(errp, -sn_count,
196fb0ed453SWenchao Xia                              "Can't list snapshots of device '%s'", dev);
197fb0ed453SWenchao Xia             break;
198fb0ed453SWenchao Xia         }
199fb0ed453SWenchao Xia         return sn_count;
200fb0ed453SWenchao Xia     }
201f364ec65SWenchao Xia 
202f364ec65SWenchao Xia     for (i = 0; i < sn_count; i++) {
203fb0ed453SWenchao Xia         info = g_new0(SnapshotInfo, 1);
204fb0ed453SWenchao Xia         info->id            = g_strdup(sn_tab[i].id_str);
205fb0ed453SWenchao Xia         info->name          = g_strdup(sn_tab[i].name);
206fb0ed453SWenchao Xia         info->vm_state_size = sn_tab[i].vm_state_size;
207fb0ed453SWenchao Xia         info->date_sec      = sn_tab[i].date_sec;
208fb0ed453SWenchao Xia         info->date_nsec     = sn_tab[i].date_nsec;
209fb0ed453SWenchao Xia         info->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
210fb0ed453SWenchao Xia         info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
211f364ec65SWenchao Xia 
212fb0ed453SWenchao Xia         info_list = g_new0(SnapshotInfoList, 1);
213fb0ed453SWenchao Xia         info_list->value = info;
214f364ec65SWenchao Xia 
215f364ec65SWenchao Xia         /* XXX: waiting for the qapi to support qemu-queue.h types */
216f364ec65SWenchao Xia         if (!cur_item) {
217fb0ed453SWenchao Xia             head = cur_item = info_list;
218f364ec65SWenchao Xia         } else {
219f364ec65SWenchao Xia             cur_item->next = info_list;
220f364ec65SWenchao Xia             cur_item = info_list;
221f364ec65SWenchao Xia         }
222f364ec65SWenchao Xia 
223f364ec65SWenchao Xia     }
224f364ec65SWenchao Xia 
225f364ec65SWenchao Xia     g_free(sn_tab);
226fb0ed453SWenchao Xia     *p_list = head;
227fb0ed453SWenchao Xia     return 0;
228f364ec65SWenchao Xia }
229f364ec65SWenchao Xia 
23043526ec8SWenchao Xia /**
23143526ec8SWenchao Xia  * bdrv_query_image_info:
23243526ec8SWenchao Xia  * @bs: block device to examine
23343526ec8SWenchao Xia  * @p_info: location to store image information
23443526ec8SWenchao Xia  * @errp: location to store error information
23543526ec8SWenchao Xia  *
236553a7e87SWenchao Xia  * Store "flat" image information in @p_info.
237553a7e87SWenchao Xia  *
238553a7e87SWenchao Xia  * "Flat" means it does *not* query backing image information,
239553a7e87SWenchao Xia  * i.e. (*pinfo)->has_backing_image will be set to false and
240553a7e87SWenchao Xia  * (*pinfo)->backing_image to NULL even when the image does in fact have
241553a7e87SWenchao Xia  * a backing image.
242553a7e87SWenchao Xia  *
24343526ec8SWenchao Xia  * @p_info will be set only on success. On error, store error in @errp.
24443526ec8SWenchao Xia  */
24543526ec8SWenchao Xia void bdrv_query_image_info(BlockDriverState *bs,
24643526ec8SWenchao Xia                            ImageInfo **p_info,
24743526ec8SWenchao Xia                            Error **errp)
248f364ec65SWenchao Xia {
24952bf1e72SMarkus Armbruster     int64_t size;
25043526ec8SWenchao Xia     const char *backing_filename;
251f364ec65SWenchao Xia     BlockDriverInfo bdi;
25243526ec8SWenchao Xia     int ret;
25343526ec8SWenchao Xia     Error *err = NULL;
25452bf1e72SMarkus Armbruster     ImageInfo *info;
255f364ec65SWenchao Xia 
2561963f8d5SPaolo Bonzini     aio_context_acquire(bdrv_get_aio_context(bs));
2571963f8d5SPaolo Bonzini 
25852bf1e72SMarkus Armbruster     size = bdrv_getlength(bs);
25952bf1e72SMarkus Armbruster     if (size < 0) {
2609adceb02SFam Zheng         error_setg_errno(errp, -size, "Can't get image size '%s'",
2619adceb02SFam Zheng                          bs->exact_filename);
2621963f8d5SPaolo Bonzini         goto out;
26352bf1e72SMarkus Armbruster     }
264f364ec65SWenchao Xia 
26552bf1e72SMarkus Armbruster     info = g_new0(ImageInfo, 1);
26643526ec8SWenchao Xia     info->filename        = g_strdup(bs->filename);
267f364ec65SWenchao Xia     info->format          = g_strdup(bdrv_get_format_name(bs));
26852bf1e72SMarkus Armbruster     info->virtual_size    = size;
269f364ec65SWenchao Xia     info->actual_size     = bdrv_get_allocated_file_size(bs);
270f364ec65SWenchao Xia     info->has_actual_size = info->actual_size >= 0;
271f364ec65SWenchao Xia     if (bdrv_is_encrypted(bs)) {
272f364ec65SWenchao Xia         info->encrypted = true;
273f364ec65SWenchao Xia         info->has_encrypted = true;
274f364ec65SWenchao Xia     }
275f364ec65SWenchao Xia     if (bdrv_get_info(bs, &bdi) >= 0) {
276f364ec65SWenchao Xia         if (bdi.cluster_size != 0) {
277f364ec65SWenchao Xia             info->cluster_size = bdi.cluster_size;
278f364ec65SWenchao Xia             info->has_cluster_size = true;
279f364ec65SWenchao Xia         }
280f364ec65SWenchao Xia         info->dirty_flag = bdi.is_dirty;
281f364ec65SWenchao Xia         info->has_dirty_flag = true;
282f364ec65SWenchao Xia     }
283eae041feSMax Reitz     info->format_specific     = bdrv_get_specific_info(bs);
284eae041feSMax Reitz     info->has_format_specific = info->format_specific != NULL;
285eae041feSMax Reitz 
28643526ec8SWenchao Xia     backing_filename = bs->backing_file;
287f364ec65SWenchao Xia     if (backing_filename[0] != '\0') {
2889a29e18fSJeff Cody         char *backing_filename2 = g_malloc0(PATH_MAX);
289f364ec65SWenchao Xia         info->backing_filename = g_strdup(backing_filename);
290f364ec65SWenchao Xia         info->has_backing_filename = true;
2919a29e18fSJeff Cody         bdrv_get_full_backing_filename(bs, backing_filename2, PATH_MAX, &err);
2929f07429eSMax Reitz         if (err) {
293a5002d53SJohn Snow             /* Can't reconstruct the full backing filename, so we must omit
294a5002d53SJohn Snow              * this field and apply a Best Effort to this query. */
295564d64bdSJeff Cody             g_free(backing_filename2);
296a5002d53SJohn Snow             backing_filename2 = NULL;
297a5002d53SJohn Snow             error_free(err);
2980fa296ebSFam Zheng             err = NULL;
2999f07429eSMax Reitz         }
300f364ec65SWenchao Xia 
30112dcb1c0SJohn Snow         /* Always report the full_backing_filename if present, even if it's the
30212dcb1c0SJohn Snow          * same as backing_filename. That they are same is useful info. */
30312dcb1c0SJohn Snow         if (backing_filename2) {
30412dcb1c0SJohn Snow             info->full_backing_filename = g_strdup(backing_filename2);
305f364ec65SWenchao Xia             info->has_full_backing_filename = true;
306f364ec65SWenchao Xia         }
307f364ec65SWenchao Xia 
308f364ec65SWenchao Xia         if (bs->backing_format[0]) {
309f364ec65SWenchao Xia             info->backing_filename_format = g_strdup(bs->backing_format);
310f364ec65SWenchao Xia             info->has_backing_filename_format = true;
311f364ec65SWenchao Xia         }
312564d64bdSJeff Cody         g_free(backing_filename2);
313f364ec65SWenchao Xia     }
31443526ec8SWenchao Xia 
31543526ec8SWenchao Xia     ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
31643526ec8SWenchao Xia     switch (ret) {
31743526ec8SWenchao Xia     case 0:
31843526ec8SWenchao Xia         if (info->snapshots) {
31943526ec8SWenchao Xia             info->has_snapshots = true;
32043526ec8SWenchao Xia         }
32143526ec8SWenchao Xia         break;
32243526ec8SWenchao Xia     /* recoverable error */
32343526ec8SWenchao Xia     case -ENOMEDIUM:
32443526ec8SWenchao Xia     case -ENOTSUP:
32543526ec8SWenchao Xia         error_free(err);
32643526ec8SWenchao Xia         break;
32743526ec8SWenchao Xia     default:
32843526ec8SWenchao Xia         error_propagate(errp, err);
32943526ec8SWenchao Xia         qapi_free_ImageInfo(info);
3301963f8d5SPaolo Bonzini         goto out;
33143526ec8SWenchao Xia     }
33243526ec8SWenchao Xia 
33343526ec8SWenchao Xia     *p_info = info;
3341963f8d5SPaolo Bonzini 
3351963f8d5SPaolo Bonzini out:
3361963f8d5SPaolo Bonzini     aio_context_release(bdrv_get_aio_context(bs));
337f364ec65SWenchao Xia }
338f364ec65SWenchao Xia 
339553a7e87SWenchao Xia /* @p_info will be set only on success. */
340d829a211SMarkus Armbruster static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
341553a7e87SWenchao Xia                             Error **errp)
342f364ec65SWenchao Xia {
343f364ec65SWenchao Xia     BlockInfo *info = g_malloc0(sizeof(*info));
344d829a211SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
34546eade7bSKevin Wolf     char *qdev;
34646eade7bSKevin Wolf 
347d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of */
348d3c8c674SKevin Wolf     while (bs && bs->drv && bs->implicit) {
349d3c8c674SKevin Wolf         bs = backing_bs(bs);
350d3c8c674SKevin Wolf     }
351d3c8c674SKevin Wolf 
352d829a211SMarkus Armbruster     info->device = g_strdup(blk_name(blk));
353f364ec65SWenchao Xia     info->type = g_strdup("unknown");
354a7f53e26SMarkus Armbruster     info->locked = blk_dev_is_medium_locked(blk);
355a7f53e26SMarkus Armbruster     info->removable = blk_dev_has_removable_media(blk);
356f364ec65SWenchao Xia 
35746eade7bSKevin Wolf     qdev = blk_get_attached_dev_id(blk);
35846eade7bSKevin Wolf     if (qdev && *qdev) {
35946eade7bSKevin Wolf         info->has_qdev = true;
36046eade7bSKevin Wolf         info->qdev = qdev;
36146eade7bSKevin Wolf     } else {
36246eade7bSKevin Wolf         g_free(qdev);
36346eade7bSKevin Wolf     }
36446eade7bSKevin Wolf 
365327032ceSMax Reitz     if (blk_dev_has_tray(blk)) {
366f364ec65SWenchao Xia         info->has_tray_open = true;
367a7f53e26SMarkus Armbruster         info->tray_open = blk_dev_is_tray_open(blk);
368f364ec65SWenchao Xia     }
369f364ec65SWenchao Xia 
370373340b2SMax Reitz     if (blk_iostatus_is_enabled(blk)) {
371f364ec65SWenchao Xia         info->has_io_status = true;
372373340b2SMax Reitz         info->io_status = blk_iostatus(blk);
373f364ec65SWenchao Xia     }
374f364ec65SWenchao Xia 
3755433c24fSMax Reitz     if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
37621b56835SFam Zheng         info->has_dirty_bitmaps = true;
37721b56835SFam Zheng         info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
37821b56835SFam Zheng     }
37921b56835SFam Zheng 
3805433c24fSMax Reitz     if (bs && bs->drv) {
381f364ec65SWenchao Xia         info->has_inserted = true;
382c83f9fbaSKevin Wolf         info->inserted = bdrv_block_device_info(blk, bs, errp);
383d5a8ee60SAlberto Garcia         if (info->inserted == NULL) {
384553a7e87SWenchao Xia             goto err;
385f364ec65SWenchao Xia         }
386553a7e87SWenchao Xia     }
387553a7e87SWenchao Xia 
388553a7e87SWenchao Xia     *p_info = info;
389553a7e87SWenchao Xia     return;
390553a7e87SWenchao Xia 
391553a7e87SWenchao Xia  err:
392553a7e87SWenchao Xia     qapi_free_BlockInfo(info);
393f364ec65SWenchao Xia }
394f364ec65SWenchao Xia 
39554302156SMax Reitz static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
396f364ec65SWenchao Xia {
3972b77e60aSKevin Wolf     BlockAcctStats *stats = blk_get_stats(blk);
398979e9b03SAlberto Garcia     BlockAcctTimedStats *ts = NULL;
3997f0e9da6SMax Reitz 
40054302156SMax Reitz     ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
40154302156SMax Reitz     ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
40254302156SMax Reitz     ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
40354302156SMax Reitz     ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
4047ee12dafSAlberto Garcia 
40554302156SMax Reitz     ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
40654302156SMax Reitz     ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
40754302156SMax Reitz     ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
4087ee12dafSAlberto Garcia 
40954302156SMax Reitz     ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
41054302156SMax Reitz     ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
41154302156SMax Reitz     ds->invalid_flush_operations =
4127ee12dafSAlberto Garcia         stats->invalid_ops[BLOCK_ACCT_FLUSH];
4137ee12dafSAlberto Garcia 
41454302156SMax Reitz     ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
41554302156SMax Reitz     ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
41654302156SMax Reitz     ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
41754302156SMax Reitz     ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
41854302156SMax Reitz     ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
41954302156SMax Reitz     ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
420cb38fffbSAlberto Garcia 
42154302156SMax Reitz     ds->has_idle_time_ns = stats->last_access_time_ns > 0;
42254302156SMax Reitz     if (ds->has_idle_time_ns) {
42354302156SMax Reitz         ds->idle_time_ns = block_acct_idle_time_ns(stats);
424cb38fffbSAlberto Garcia     }
425362e9299SAlberto Garcia 
42654302156SMax Reitz     ds->account_invalid = stats->account_invalid;
42754302156SMax Reitz     ds->account_failed = stats->account_failed;
428979e9b03SAlberto Garcia 
429979e9b03SAlberto Garcia     while ((ts = block_acct_interval_next(stats, ts))) {
430979e9b03SAlberto Garcia         BlockDeviceTimedStatsList *timed_stats =
431979e9b03SAlberto Garcia             g_malloc0(sizeof(*timed_stats));
432979e9b03SAlberto Garcia         BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
43354302156SMax Reitz         timed_stats->next = ds->timed_stats;
434979e9b03SAlberto Garcia         timed_stats->value = dev_stats;
43554302156SMax Reitz         ds->timed_stats = timed_stats;
436979e9b03SAlberto Garcia 
437979e9b03SAlberto Garcia         TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
438979e9b03SAlberto Garcia         TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
439979e9b03SAlberto Garcia         TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
440979e9b03SAlberto Garcia 
441979e9b03SAlberto Garcia         dev_stats->interval_length = ts->interval_length;
442979e9b03SAlberto Garcia 
443979e9b03SAlberto Garcia         dev_stats->min_rd_latency_ns = timed_average_min(rd);
444979e9b03SAlberto Garcia         dev_stats->max_rd_latency_ns = timed_average_max(rd);
445979e9b03SAlberto Garcia         dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
446979e9b03SAlberto Garcia 
447979e9b03SAlberto Garcia         dev_stats->min_wr_latency_ns = timed_average_min(wr);
448979e9b03SAlberto Garcia         dev_stats->max_wr_latency_ns = timed_average_max(wr);
449979e9b03SAlberto Garcia         dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
450979e9b03SAlberto Garcia 
451979e9b03SAlberto Garcia         dev_stats->min_flush_latency_ns = timed_average_min(fl);
452979e9b03SAlberto Garcia         dev_stats->max_flush_latency_ns = timed_average_max(fl);
453979e9b03SAlberto Garcia         dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
45496e4dedaSAlberto Garcia 
45596e4dedaSAlberto Garcia         dev_stats->avg_rd_queue_depth =
45696e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_READ);
45796e4dedaSAlberto Garcia         dev_stats->avg_wr_queue_depth =
45896e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
459979e9b03SAlberto Garcia     }
4607f0e9da6SMax Reitz }
461f364ec65SWenchao Xia 
462d3c8c674SKevin Wolf static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
463d3c8c674SKevin Wolf                                         bool blk_level)
4642b77e60aSKevin Wolf {
46520a6d768SDou Liyang     BlockStats *s = NULL;
46620a6d768SDou Liyang 
46720a6d768SDou Liyang     s = g_malloc0(sizeof(*s));
46820a6d768SDou Liyang     s->stats = g_malloc0(sizeof(*s->stats));
46920a6d768SDou Liyang 
47020a6d768SDou Liyang     if (!bs) {
47120a6d768SDou Liyang         return s;
47220a6d768SDou Liyang     }
47320a6d768SDou Liyang 
474d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of in
475d3c8c674SKevin Wolf      * a BlockBackend-level command. Stay at the exact node for a node-level
476d3c8c674SKevin Wolf      * command. */
477d3c8c674SKevin Wolf     while (blk_level && bs->drv && bs->implicit) {
478d3c8c674SKevin Wolf         bs = backing_bs(bs);
479d3c8c674SKevin Wolf         assert(bs);
480d3c8c674SKevin Wolf     }
481d3c8c674SKevin Wolf 
4822b77e60aSKevin Wolf     if (bdrv_get_node_name(bs)[0]) {
4832b77e60aSKevin Wolf         s->has_node_name = true;
4842b77e60aSKevin Wolf         s->node_name = g_strdup(bdrv_get_node_name(bs));
4852b77e60aSKevin Wolf     }
4862b77e60aSKevin Wolf 
487f7946da2SPaolo Bonzini     s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
48853d8f9d8SMax Reitz 
489f364ec65SWenchao Xia     if (bs->file) {
490f364ec65SWenchao Xia         s->has_parent = true;
491d3c8c674SKevin Wolf         s->parent = bdrv_query_bds_stats(bs->file->bs, blk_level);
492f364ec65SWenchao Xia     }
493f364ec65SWenchao Xia 
494d3c8c674SKevin Wolf     if (blk_level && bs->backing) {
495c8059b97SFam Zheng         s->has_backing = true;
496d3c8c674SKevin Wolf         s->backing = bdrv_query_bds_stats(bs->backing->bs, blk_level);
497c8059b97SFam Zheng     }
498c8059b97SFam Zheng 
49920a6d768SDou Liyang     return s;
500b07363a1SKevin Wolf }
501b07363a1SKevin Wolf 
502f364ec65SWenchao Xia BlockInfoList *qmp_query_block(Error **errp)
503f364ec65SWenchao Xia {
504f364ec65SWenchao Xia     BlockInfoList *head = NULL, **p_next = &head;
505d829a211SMarkus Armbruster     BlockBackend *blk;
506553a7e87SWenchao Xia     Error *local_err = NULL;
507f364ec65SWenchao Xia 
508d5b68844SKevin Wolf     for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
509d5b68844SKevin Wolf         BlockInfoList *info;
510d5b68844SKevin Wolf 
511ec18b0a9SKevin Wolf         if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
512d5b68844SKevin Wolf             continue;
513d5b68844SKevin Wolf         }
514d5b68844SKevin Wolf 
515d5b68844SKevin Wolf         info = g_malloc0(sizeof(*info));
516d829a211SMarkus Armbruster         bdrv_query_info(blk, &info->value, &local_err);
51784d18f06SMarkus Armbruster         if (local_err) {
518553a7e87SWenchao Xia             error_propagate(errp, local_err);
519903c341dSMarkus Armbruster             g_free(info);
520903c341dSMarkus Armbruster             qapi_free_BlockInfoList(head);
521903c341dSMarkus Armbruster             return NULL;
522553a7e87SWenchao Xia         }
523f364ec65SWenchao Xia 
524f364ec65SWenchao Xia         *p_next = info;
525f364ec65SWenchao Xia         p_next = &info->next;
526f364ec65SWenchao Xia     }
527f364ec65SWenchao Xia 
528f364ec65SWenchao Xia     return head;
529f364ec65SWenchao Xia }
530f364ec65SWenchao Xia 
531f71eaa74SFam Zheng BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
532f71eaa74SFam Zheng                                      bool query_nodes,
533f71eaa74SFam Zheng                                      Error **errp)
534f364ec65SWenchao Xia {
535f364ec65SWenchao Xia     BlockStatsList *head = NULL, **p_next = &head;
536a6baa608SDou Liyang     BlockBackend *blk;
537a6baa608SDou Liyang     BlockDriverState *bs;
538f364ec65SWenchao Xia 
539f71eaa74SFam Zheng     /* Just to be safe if query_nodes is not always initialized */
540a6baa608SDou Liyang     if (has_query_nodes && query_nodes) {
541a6baa608SDou Liyang         for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
542f364ec65SWenchao Xia             BlockStatsList *info = g_malloc0(sizeof(*info));
543a6baa608SDou Liyang             AioContext *ctx = bdrv_get_aio_context(bs);
54413344f3aSStefan Hajnoczi 
54513344f3aSStefan Hajnoczi             aio_context_acquire(ctx);
546a6baa608SDou Liyang             info->value = bdrv_query_bds_stats(bs, false);
54713344f3aSStefan Hajnoczi             aio_context_release(ctx);
548f364ec65SWenchao Xia 
549f364ec65SWenchao Xia             *p_next = info;
550f364ec65SWenchao Xia             p_next = &info->next;
551f364ec65SWenchao Xia         }
552a6baa608SDou Liyang     } else {
553a6baa608SDou Liyang         for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
554a6baa608SDou Liyang             BlockStatsList *info = g_malloc0(sizeof(*info));
555a6baa608SDou Liyang             AioContext *ctx = blk_get_aio_context(blk);
556a6baa608SDou Liyang             BlockStats *s;
557a6baa608SDou Liyang 
558a6baa608SDou Liyang             aio_context_acquire(ctx);
559a6baa608SDou Liyang             s = bdrv_query_bds_stats(blk_bs(blk), true);
560a6baa608SDou Liyang             s->has_device = true;
561a6baa608SDou Liyang             s->device = g_strdup(blk_name(blk));
562a6baa608SDou Liyang             bdrv_query_blk_stats(s->stats, blk);
563a6baa608SDou Liyang             aio_context_release(ctx);
564a6baa608SDou Liyang 
565a6baa608SDou Liyang             info->value = s;
566a6baa608SDou Liyang             *p_next = info;
567a6baa608SDou Liyang             p_next = &info->next;
568a6baa608SDou Liyang         }
569a6baa608SDou Liyang     }
570f364ec65SWenchao Xia 
571f364ec65SWenchao Xia     return head;
572f364ec65SWenchao Xia }
573f364ec65SWenchao Xia 
574f364ec65SWenchao Xia #define NB_SUFFIXES 4
575f364ec65SWenchao Xia 
576f364ec65SWenchao Xia static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
577f364ec65SWenchao Xia {
5782c20fa2cSStefan Weil     static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'};
579f364ec65SWenchao Xia     int64_t base;
580f364ec65SWenchao Xia     int i;
581f364ec65SWenchao Xia 
582f364ec65SWenchao Xia     if (size <= 999) {
583f364ec65SWenchao Xia         snprintf(buf, buf_size, "%" PRId64, size);
584f364ec65SWenchao Xia     } else {
585f364ec65SWenchao Xia         base = 1024;
586f364ec65SWenchao Xia         for (i = 0; i < NB_SUFFIXES; i++) {
587f364ec65SWenchao Xia             if (size < (10 * base)) {
588f364ec65SWenchao Xia                 snprintf(buf, buf_size, "%0.1f%c",
589f364ec65SWenchao Xia                          (double)size / base,
590f364ec65SWenchao Xia                          suffixes[i]);
591f364ec65SWenchao Xia                 break;
592f364ec65SWenchao Xia             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
593f364ec65SWenchao Xia                 snprintf(buf, buf_size, "%" PRId64 "%c",
594f364ec65SWenchao Xia                          ((size + (base >> 1)) / base),
595f364ec65SWenchao Xia                          suffixes[i]);
596f364ec65SWenchao Xia                 break;
597f364ec65SWenchao Xia             }
598f364ec65SWenchao Xia             base = base * 1024;
599f364ec65SWenchao Xia         }
600f364ec65SWenchao Xia     }
601f364ec65SWenchao Xia     return buf;
602f364ec65SWenchao Xia }
603f364ec65SWenchao Xia 
6045b917044SWenchao Xia void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
6055b917044SWenchao Xia                         QEMUSnapshotInfo *sn)
606f364ec65SWenchao Xia {
607f364ec65SWenchao Xia     char buf1[128], date_buf[128], clock_buf[128];
608f364ec65SWenchao Xia     struct tm tm;
609f364ec65SWenchao Xia     time_t ti;
610f364ec65SWenchao Xia     int64_t secs;
611f364ec65SWenchao Xia 
612f364ec65SWenchao Xia     if (!sn) {
6135b917044SWenchao Xia         func_fprintf(f,
614f364ec65SWenchao Xia                      "%-10s%-20s%7s%20s%15s",
615f364ec65SWenchao Xia                      "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
616f364ec65SWenchao Xia     } else {
617f364ec65SWenchao Xia         ti = sn->date_sec;
618f364ec65SWenchao Xia         localtime_r(&ti, &tm);
619f364ec65SWenchao Xia         strftime(date_buf, sizeof(date_buf),
620f364ec65SWenchao Xia                  "%Y-%m-%d %H:%M:%S", &tm);
621f364ec65SWenchao Xia         secs = sn->vm_clock_nsec / 1000000000;
622f364ec65SWenchao Xia         snprintf(clock_buf, sizeof(clock_buf),
623f364ec65SWenchao Xia                  "%02d:%02d:%02d.%03d",
624f364ec65SWenchao Xia                  (int)(secs / 3600),
625f364ec65SWenchao Xia                  (int)((secs / 60) % 60),
626f364ec65SWenchao Xia                  (int)(secs % 60),
627f364ec65SWenchao Xia                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
6285b917044SWenchao Xia         func_fprintf(f,
629f364ec65SWenchao Xia                      "%-10s%-20s%7s%20s%15s",
630f364ec65SWenchao Xia                      sn->id_str, sn->name,
6315b917044SWenchao Xia                      get_human_readable_size(buf1, sizeof(buf1),
6325b917044SWenchao Xia                                              sn->vm_state_size),
633f364ec65SWenchao Xia                      date_buf,
634f364ec65SWenchao Xia                      clock_buf);
635f364ec65SWenchao Xia     }
636f364ec65SWenchao Xia }
637f364ec65SWenchao Xia 
638a8d8ecb7SMax Reitz static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
639a8d8ecb7SMax Reitz                        QDict *dict);
640a8d8ecb7SMax Reitz static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
641a8d8ecb7SMax Reitz                        QList *list);
642a8d8ecb7SMax Reitz 
643a8d8ecb7SMax Reitz static void dump_qobject(fprintf_function func_fprintf, void *f,
644a8d8ecb7SMax Reitz                          int comp_indent, QObject *obj)
645a8d8ecb7SMax Reitz {
646a8d8ecb7SMax Reitz     switch (qobject_type(obj)) {
64701b2ffceSMarc-André Lureau         case QTYPE_QNUM: {
64801b2ffceSMarc-André Lureau             QNum *value = qobject_to_qnum(obj);
64901b2ffceSMarc-André Lureau             char *tmp = qnum_to_string(value);
65001b2ffceSMarc-André Lureau             func_fprintf(f, "%s", tmp);
65101b2ffceSMarc-André Lureau             g_free(tmp);
652a8d8ecb7SMax Reitz             break;
653a8d8ecb7SMax Reitz         }
654a8d8ecb7SMax Reitz         case QTYPE_QSTRING: {
655a8d8ecb7SMax Reitz             QString *value = qobject_to_qstring(obj);
656a8d8ecb7SMax Reitz             func_fprintf(f, "%s", qstring_get_str(value));
657a8d8ecb7SMax Reitz             break;
658a8d8ecb7SMax Reitz         }
659a8d8ecb7SMax Reitz         case QTYPE_QDICT: {
660a8d8ecb7SMax Reitz             QDict *value = qobject_to_qdict(obj);
661a8d8ecb7SMax Reitz             dump_qdict(func_fprintf, f, comp_indent, value);
662a8d8ecb7SMax Reitz             break;
663a8d8ecb7SMax Reitz         }
664a8d8ecb7SMax Reitz         case QTYPE_QLIST: {
665a8d8ecb7SMax Reitz             QList *value = qobject_to_qlist(obj);
666a8d8ecb7SMax Reitz             dump_qlist(func_fprintf, f, comp_indent, value);
667a8d8ecb7SMax Reitz             break;
668a8d8ecb7SMax Reitz         }
669a8d8ecb7SMax Reitz         case QTYPE_QBOOL: {
670a8d8ecb7SMax Reitz             QBool *value = qobject_to_qbool(obj);
671fc48ffc3SEric Blake             func_fprintf(f, "%s", qbool_get_bool(value) ? "true" : "false");
672a8d8ecb7SMax Reitz             break;
673a8d8ecb7SMax Reitz         }
674a8d8ecb7SMax Reitz         default:
675a8d8ecb7SMax Reitz             abort();
676a8d8ecb7SMax Reitz     }
677a8d8ecb7SMax Reitz }
678a8d8ecb7SMax Reitz 
679a8d8ecb7SMax Reitz static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
680a8d8ecb7SMax Reitz                        QList *list)
681a8d8ecb7SMax Reitz {
682a8d8ecb7SMax Reitz     const QListEntry *entry;
683a8d8ecb7SMax Reitz     int i = 0;
684a8d8ecb7SMax Reitz 
685a8d8ecb7SMax Reitz     for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
6861310a3d3SEric Blake         QType type = qobject_type(entry->value);
687a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
688853ccfedSPeter Xu         func_fprintf(f, "%*s[%i]:%c", indentation * 4, "", i,
689853ccfedSPeter Xu                      composite ? '\n' : ' ');
690a8d8ecb7SMax Reitz         dump_qobject(func_fprintf, f, indentation + 1, entry->value);
691a8d8ecb7SMax Reitz         if (!composite) {
692a8d8ecb7SMax Reitz             func_fprintf(f, "\n");
693a8d8ecb7SMax Reitz         }
694a8d8ecb7SMax Reitz     }
695a8d8ecb7SMax Reitz }
696a8d8ecb7SMax Reitz 
697a8d8ecb7SMax Reitz static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
698a8d8ecb7SMax Reitz                        QDict *dict)
699a8d8ecb7SMax Reitz {
700a8d8ecb7SMax Reitz     const QDictEntry *entry;
701a8d8ecb7SMax Reitz 
702a8d8ecb7SMax Reitz     for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
7031310a3d3SEric Blake         QType type = qobject_type(entry->value);
704a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
7055eda6227SPeter Xu         char *key = g_malloc(strlen(entry->key) + 1);
706a8d8ecb7SMax Reitz         int i;
707a8d8ecb7SMax Reitz 
708a8d8ecb7SMax Reitz         /* replace dashes with spaces in key (variable) names */
709a8d8ecb7SMax Reitz         for (i = 0; entry->key[i]; i++) {
710a8d8ecb7SMax Reitz             key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
711a8d8ecb7SMax Reitz         }
712a8d8ecb7SMax Reitz         key[i] = 0;
713853ccfedSPeter Xu         func_fprintf(f, "%*s%s:%c", indentation * 4, "", key,
714853ccfedSPeter Xu                      composite ? '\n' : ' ');
715a8d8ecb7SMax Reitz         dump_qobject(func_fprintf, f, indentation + 1, entry->value);
716a8d8ecb7SMax Reitz         if (!composite) {
717a8d8ecb7SMax Reitz             func_fprintf(f, "\n");
718a8d8ecb7SMax Reitz         }
7195eda6227SPeter Xu         g_free(key);
720a8d8ecb7SMax Reitz     }
721a8d8ecb7SMax Reitz }
722a8d8ecb7SMax Reitz 
723a8d8ecb7SMax Reitz void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
724a8d8ecb7SMax Reitz                                    ImageInfoSpecific *info_spec)
725a8d8ecb7SMax Reitz {
726a8d8ecb7SMax Reitz     QObject *obj, *data;
7277d5e199aSDaniel P. Berrange     Visitor *v = qobject_output_visitor_new(&obj);
728a8d8ecb7SMax Reitz 
7293b098d56SEric Blake     visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
7303b098d56SEric Blake     visit_complete(v, &obj);
731a8d8ecb7SMax Reitz     data = qdict_get(qobject_to_qdict(obj), "data");
732a8d8ecb7SMax Reitz     dump_qobject(func_fprintf, f, 1, data);
7333ac2f2f7SPino Toscano     qobject_decref(obj);
7343b098d56SEric Blake     visit_free(v);
735a8d8ecb7SMax Reitz }
736a8d8ecb7SMax Reitz 
7375b917044SWenchao Xia void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
7385b917044SWenchao Xia                           ImageInfo *info)
739f364ec65SWenchao Xia {
740f364ec65SWenchao Xia     char size_buf[128], dsize_buf[128];
741f364ec65SWenchao Xia     if (!info->has_actual_size) {
742f364ec65SWenchao Xia         snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
743f364ec65SWenchao Xia     } else {
744f364ec65SWenchao Xia         get_human_readable_size(dsize_buf, sizeof(dsize_buf),
745f364ec65SWenchao Xia                                 info->actual_size);
746f364ec65SWenchao Xia     }
747f364ec65SWenchao Xia     get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
7485b917044SWenchao Xia     func_fprintf(f,
7495b917044SWenchao Xia                  "image: %s\n"
750f364ec65SWenchao Xia                  "file format: %s\n"
751f364ec65SWenchao Xia                  "virtual size: %s (%" PRId64 " bytes)\n"
752f364ec65SWenchao Xia                  "disk size: %s\n",
753f364ec65SWenchao Xia                  info->filename, info->format, size_buf,
754f364ec65SWenchao Xia                  info->virtual_size,
755f364ec65SWenchao Xia                  dsize_buf);
756f364ec65SWenchao Xia 
757f364ec65SWenchao Xia     if (info->has_encrypted && info->encrypted) {
7585b917044SWenchao Xia         func_fprintf(f, "encrypted: yes\n");
759f364ec65SWenchao Xia     }
760f364ec65SWenchao Xia 
761f364ec65SWenchao Xia     if (info->has_cluster_size) {
7625b917044SWenchao Xia         func_fprintf(f, "cluster_size: %" PRId64 "\n",
7635b917044SWenchao Xia                        info->cluster_size);
764f364ec65SWenchao Xia     }
765f364ec65SWenchao Xia 
766f364ec65SWenchao Xia     if (info->has_dirty_flag && info->dirty_flag) {
7675b917044SWenchao Xia         func_fprintf(f, "cleanly shut down: no\n");
768f364ec65SWenchao Xia     }
769f364ec65SWenchao Xia 
770f364ec65SWenchao Xia     if (info->has_backing_filename) {
7715b917044SWenchao Xia         func_fprintf(f, "backing file: %s", info->backing_filename);
7725c9d9ca5SJohn Snow         if (!info->has_full_backing_filename) {
7735c9d9ca5SJohn Snow             func_fprintf(f, " (cannot determine actual path)");
7745c9d9ca5SJohn Snow         } else if (strcmp(info->backing_filename,
7755c9d9ca5SJohn Snow                           info->full_backing_filename) != 0) {
7765b917044SWenchao Xia             func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
777f364ec65SWenchao Xia         }
7785b917044SWenchao Xia         func_fprintf(f, "\n");
779f364ec65SWenchao Xia         if (info->has_backing_filename_format) {
7805b917044SWenchao Xia             func_fprintf(f, "backing file format: %s\n",
7815b917044SWenchao Xia                          info->backing_filename_format);
782f364ec65SWenchao Xia         }
783f364ec65SWenchao Xia     }
784f364ec65SWenchao Xia 
785f364ec65SWenchao Xia     if (info->has_snapshots) {
786f364ec65SWenchao Xia         SnapshotInfoList *elem;
787f364ec65SWenchao Xia 
7885b917044SWenchao Xia         func_fprintf(f, "Snapshot list:\n");
7895b917044SWenchao Xia         bdrv_snapshot_dump(func_fprintf, f, NULL);
7905b917044SWenchao Xia         func_fprintf(f, "\n");
791f364ec65SWenchao Xia 
792f364ec65SWenchao Xia         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
793f364ec65SWenchao Xia          * we convert to the block layer's native QEMUSnapshotInfo for now.
794f364ec65SWenchao Xia          */
795f364ec65SWenchao Xia         for (elem = info->snapshots; elem; elem = elem->next) {
796f364ec65SWenchao Xia             QEMUSnapshotInfo sn = {
797f364ec65SWenchao Xia                 .vm_state_size = elem->value->vm_state_size,
798f364ec65SWenchao Xia                 .date_sec = elem->value->date_sec,
799f364ec65SWenchao Xia                 .date_nsec = elem->value->date_nsec,
800f364ec65SWenchao Xia                 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
801f364ec65SWenchao Xia                                  elem->value->vm_clock_nsec,
802f364ec65SWenchao Xia             };
803f364ec65SWenchao Xia 
804f364ec65SWenchao Xia             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
805f364ec65SWenchao Xia             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
8065b917044SWenchao Xia             bdrv_snapshot_dump(func_fprintf, f, &sn);
8075b917044SWenchao Xia             func_fprintf(f, "\n");
808f364ec65SWenchao Xia         }
809f364ec65SWenchao Xia     }
810a8d8ecb7SMax Reitz 
811a8d8ecb7SMax Reitz     if (info->has_format_specific) {
812a8d8ecb7SMax Reitz         func_fprintf(f, "Format specific information:\n");
813a8d8ecb7SMax Reitz         bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);
814a8d8ecb7SMax Reitz     }
815f364ec65SWenchao Xia }
816