xref: /qemu/block/qapi.c (revision 1bf6e9ca)
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"
30e688df6bSMarkus Armbruster #include "qapi/error.h"
319af23989SMarkus Armbruster #include "qapi/qapi-commands-block-core.h"
32b3db211fSDaniel P. Berrange #include "qapi/qobject-output-visitor.h"
339af23989SMarkus Armbruster #include "qapi/qapi-visit-block-core.h"
346b673957SMarkus Armbruster #include "qapi/qmp/qbool.h"
35452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
3647e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h"
3715280c36SMarkus Armbruster #include "qapi/qmp/qnum.h"
386b673957SMarkus Armbruster #include "qapi/qmp/qstring.h"
39d829a211SMarkus Armbruster #include "sysemu/block-backend.h"
40f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
41f364ec65SWenchao Xia 
42c83f9fbaSKevin Wolf BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
43c83f9fbaSKevin Wolf                                         BlockDriverState *bs, Error **errp)
44c13163fbSBenoît Canet {
45d5a8ee60SAlberto Garcia     ImageInfo **p_image_info;
46d5a8ee60SAlberto Garcia     BlockDriverState *bs0;
47d470ad42SMax Reitz     BlockDeviceInfo *info;
48c13163fbSBenoît Canet 
49d470ad42SMax Reitz     if (!bs->drv) {
50d470ad42SMax Reitz         error_setg(errp, "Block device %s is ejected", bs->node_name);
51d470ad42SMax Reitz         return NULL;
52d470ad42SMax Reitz     }
53d470ad42SMax Reitz 
54d470ad42SMax Reitz     info = g_malloc0(sizeof(*info));
55c13163fbSBenoît Canet     info->file                   = g_strdup(bs->filename);
56c13163fbSBenoît Canet     info->ro                     = bs->read_only;
57c13163fbSBenoît Canet     info->drv                    = g_strdup(bs->drv->format_name);
58c13163fbSBenoît Canet     info->encrypted              = bs->encrypted;
59c01c214bSDaniel P. Berrange     info->encryption_key_missing = false;
60c13163fbSBenoît Canet 
619e193c5aSKevin Wolf     info->cache = g_new(BlockdevCacheInfo, 1);
629e193c5aSKevin Wolf     *info->cache = (BlockdevCacheInfo) {
63c83f9fbaSKevin Wolf         .writeback      = blk ? blk_enable_write_cache(blk) : true,
649e193c5aSKevin Wolf         .direct         = !!(bs->open_flags & BDRV_O_NOCACHE),
659e193c5aSKevin Wolf         .no_flush       = !!(bs->open_flags & BDRV_O_NO_FLUSH),
669e193c5aSKevin Wolf     };
679e193c5aSKevin Wolf 
68c13163fbSBenoît Canet     if (bs->node_name[0]) {
69c13163fbSBenoît Canet         info->has_node_name = true;
70c13163fbSBenoît Canet         info->node_name = g_strdup(bs->node_name);
71c13163fbSBenoît Canet     }
72c13163fbSBenoît Canet 
73c13163fbSBenoît Canet     if (bs->backing_file[0]) {
74c13163fbSBenoît Canet         info->has_backing_file = true;
75c13163fbSBenoît Canet         info->backing_file = g_strdup(bs->backing_file);
76c13163fbSBenoît Canet     }
77c13163fbSBenoît Canet 
78465bee1dSPeter Lieven     info->detect_zeroes = bs->detect_zeroes;
79c13163fbSBenoît Canet 
80022cdc9fSManos Pitsidianakis     if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
81c13163fbSBenoît Canet         ThrottleConfig cfg;
82022cdc9fSManos Pitsidianakis         BlockBackendPublic *blkp = blk_get_public(blk);
8376f4afb4SAlberto Garcia 
84022cdc9fSManos Pitsidianakis         throttle_group_get_config(&blkp->throttle_group_member, &cfg);
8576f4afb4SAlberto Garcia 
86c13163fbSBenoît Canet         info->bps     = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
87c13163fbSBenoît Canet         info->bps_rd  = cfg.buckets[THROTTLE_BPS_READ].avg;
88c13163fbSBenoît Canet         info->bps_wr  = cfg.buckets[THROTTLE_BPS_WRITE].avg;
89c13163fbSBenoît Canet 
90c13163fbSBenoît Canet         info->iops    = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
91c13163fbSBenoît Canet         info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
92c13163fbSBenoît Canet         info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
93c13163fbSBenoît Canet 
94c13163fbSBenoît Canet         info->has_bps_max     = cfg.buckets[THROTTLE_BPS_TOTAL].max;
95c13163fbSBenoît Canet         info->bps_max         = cfg.buckets[THROTTLE_BPS_TOTAL].max;
96c13163fbSBenoît Canet         info->has_bps_rd_max  = cfg.buckets[THROTTLE_BPS_READ].max;
97c13163fbSBenoît Canet         info->bps_rd_max      = cfg.buckets[THROTTLE_BPS_READ].max;
98c13163fbSBenoît Canet         info->has_bps_wr_max  = cfg.buckets[THROTTLE_BPS_WRITE].max;
99c13163fbSBenoît Canet         info->bps_wr_max      = cfg.buckets[THROTTLE_BPS_WRITE].max;
100c13163fbSBenoît Canet 
101c13163fbSBenoît Canet         info->has_iops_max    = cfg.buckets[THROTTLE_OPS_TOTAL].max;
102c13163fbSBenoît Canet         info->iops_max        = cfg.buckets[THROTTLE_OPS_TOTAL].max;
103c13163fbSBenoît Canet         info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
104c13163fbSBenoît Canet         info->iops_rd_max     = cfg.buckets[THROTTLE_OPS_READ].max;
105c13163fbSBenoît Canet         info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
106c13163fbSBenoît Canet         info->iops_wr_max     = cfg.buckets[THROTTLE_OPS_WRITE].max;
107c13163fbSBenoît Canet 
108398befdfSAlberto Garcia         info->has_bps_max_length     = info->has_bps_max;
109398befdfSAlberto Garcia         info->bps_max_length         =
110398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
111398befdfSAlberto Garcia         info->has_bps_rd_max_length  = info->has_bps_rd_max;
112398befdfSAlberto Garcia         info->bps_rd_max_length      =
113398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_READ].burst_length;
114398befdfSAlberto Garcia         info->has_bps_wr_max_length  = info->has_bps_wr_max;
115398befdfSAlberto Garcia         info->bps_wr_max_length      =
116398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
117398befdfSAlberto Garcia 
118398befdfSAlberto Garcia         info->has_iops_max_length    = info->has_iops_max;
119398befdfSAlberto Garcia         info->iops_max_length        =
120398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
121398befdfSAlberto Garcia         info->has_iops_rd_max_length = info->has_iops_rd_max;
122398befdfSAlberto Garcia         info->iops_rd_max_length     =
123398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_READ].burst_length;
124398befdfSAlberto Garcia         info->has_iops_wr_max_length = info->has_iops_wr_max;
125398befdfSAlberto Garcia         info->iops_wr_max_length     =
126398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
127398befdfSAlberto Garcia 
128c13163fbSBenoît Canet         info->has_iops_size = cfg.op_size;
129c13163fbSBenoît Canet         info->iops_size = cfg.op_size;
130b8fe1694SAlberto Garcia 
131b8fe1694SAlberto Garcia         info->has_group = true;
132022cdc9fSManos Pitsidianakis         info->group =
133022cdc9fSManos Pitsidianakis             g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
134c13163fbSBenoît Canet     }
135c13163fbSBenoît Canet 
136e2462113SFrancesco Romani     info->write_threshold = bdrv_write_threshold_get(bs);
137e2462113SFrancesco Romani 
138d5a8ee60SAlberto Garcia     bs0 = bs;
139d5a8ee60SAlberto Garcia     p_image_info = &info->image;
140d3c8c674SKevin Wolf     info->backing_file_depth = 0;
141d5a8ee60SAlberto Garcia     while (1) {
142d5a8ee60SAlberto Garcia         Error *local_err = NULL;
143d5a8ee60SAlberto Garcia         bdrv_query_image_info(bs0, p_image_info, &local_err);
144d5a8ee60SAlberto Garcia         if (local_err) {
145d5a8ee60SAlberto Garcia             error_propagate(errp, local_err);
146d5a8ee60SAlberto Garcia             qapi_free_BlockDeviceInfo(info);
147d5a8ee60SAlberto Garcia             return NULL;
148d5a8ee60SAlberto Garcia         }
149d3c8c674SKevin Wolf 
150760e0063SKevin Wolf         if (bs0->drv && bs0->backing) {
151d3c8c674SKevin Wolf             info->backing_file_depth++;
152760e0063SKevin Wolf             bs0 = bs0->backing->bs;
153d5a8ee60SAlberto Garcia             (*p_image_info)->has_backing_image = true;
154d5a8ee60SAlberto Garcia             p_image_info = &((*p_image_info)->backing_image);
155d5a8ee60SAlberto Garcia         } else {
156d5a8ee60SAlberto Garcia             break;
157d5a8ee60SAlberto Garcia         }
158d3c8c674SKevin Wolf 
159d3c8c674SKevin Wolf         /* Skip automatically inserted nodes that the user isn't aware of for
160d3c8c674SKevin Wolf          * query-block (blk != NULL), but not for query-named-block-nodes */
1618e8eb0a9SKevin Wolf         while (blk && bs0->drv && bs0->implicit) {
162d3c8c674SKevin Wolf             bs0 = backing_bs(bs0);
1638e8eb0a9SKevin Wolf             assert(bs0);
164d3c8c674SKevin Wolf         }
165d5a8ee60SAlberto Garcia     }
166d5a8ee60SAlberto Garcia 
167c13163fbSBenoît Canet     return info;
168c13163fbSBenoît Canet }
169c13163fbSBenoît Canet 
170fb0ed453SWenchao Xia /*
171fb0ed453SWenchao Xia  * Returns 0 on success, with *p_list either set to describe snapshot
172fb0ed453SWenchao Xia  * information, or NULL because there are no snapshots.  Returns -errno on
173fb0ed453SWenchao Xia  * error, with *p_list untouched.
174fb0ed453SWenchao Xia  */
175fb0ed453SWenchao Xia int bdrv_query_snapshot_info_list(BlockDriverState *bs,
176fb0ed453SWenchao Xia                                   SnapshotInfoList **p_list,
177fb0ed453SWenchao Xia                                   Error **errp)
178f364ec65SWenchao Xia {
179f364ec65SWenchao Xia     int i, sn_count;
180f364ec65SWenchao Xia     QEMUSnapshotInfo *sn_tab = NULL;
181fb0ed453SWenchao Xia     SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
182fb0ed453SWenchao Xia     SnapshotInfo *info;
183fb0ed453SWenchao Xia 
184f364ec65SWenchao Xia     sn_count = bdrv_snapshot_list(bs, &sn_tab);
185fb0ed453SWenchao Xia     if (sn_count < 0) {
186fb0ed453SWenchao Xia         const char *dev = bdrv_get_device_name(bs);
187fb0ed453SWenchao Xia         switch (sn_count) {
188fb0ed453SWenchao Xia         case -ENOMEDIUM:
189fb0ed453SWenchao Xia             error_setg(errp, "Device '%s' is not inserted", dev);
190fb0ed453SWenchao Xia             break;
191fb0ed453SWenchao Xia         case -ENOTSUP:
192fb0ed453SWenchao Xia             error_setg(errp,
193fb0ed453SWenchao Xia                        "Device '%s' does not support internal snapshots",
194fb0ed453SWenchao Xia                        dev);
195fb0ed453SWenchao Xia             break;
196fb0ed453SWenchao Xia         default:
197fb0ed453SWenchao Xia             error_setg_errno(errp, -sn_count,
198fb0ed453SWenchao Xia                              "Can't list snapshots of device '%s'", dev);
199fb0ed453SWenchao Xia             break;
200fb0ed453SWenchao Xia         }
201fb0ed453SWenchao Xia         return sn_count;
202fb0ed453SWenchao Xia     }
203f364ec65SWenchao Xia 
204f364ec65SWenchao Xia     for (i = 0; i < sn_count; i++) {
205fb0ed453SWenchao Xia         info = g_new0(SnapshotInfo, 1);
206fb0ed453SWenchao Xia         info->id            = g_strdup(sn_tab[i].id_str);
207fb0ed453SWenchao Xia         info->name          = g_strdup(sn_tab[i].name);
208fb0ed453SWenchao Xia         info->vm_state_size = sn_tab[i].vm_state_size;
209fb0ed453SWenchao Xia         info->date_sec      = sn_tab[i].date_sec;
210fb0ed453SWenchao Xia         info->date_nsec     = sn_tab[i].date_nsec;
211fb0ed453SWenchao Xia         info->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
212fb0ed453SWenchao Xia         info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
213f364ec65SWenchao Xia 
214fb0ed453SWenchao Xia         info_list = g_new0(SnapshotInfoList, 1);
215fb0ed453SWenchao Xia         info_list->value = info;
216f364ec65SWenchao Xia 
217f364ec65SWenchao Xia         /* XXX: waiting for the qapi to support qemu-queue.h types */
218f364ec65SWenchao Xia         if (!cur_item) {
219fb0ed453SWenchao Xia             head = cur_item = info_list;
220f364ec65SWenchao Xia         } else {
221f364ec65SWenchao Xia             cur_item->next = info_list;
222f364ec65SWenchao Xia             cur_item = info_list;
223f364ec65SWenchao Xia         }
224f364ec65SWenchao Xia 
225f364ec65SWenchao Xia     }
226f364ec65SWenchao Xia 
227f364ec65SWenchao Xia     g_free(sn_tab);
228fb0ed453SWenchao Xia     *p_list = head;
229fb0ed453SWenchao Xia     return 0;
230f364ec65SWenchao Xia }
231f364ec65SWenchao Xia 
23243526ec8SWenchao Xia /**
23343526ec8SWenchao Xia  * bdrv_query_image_info:
23443526ec8SWenchao Xia  * @bs: block device to examine
23543526ec8SWenchao Xia  * @p_info: location to store image information
23643526ec8SWenchao Xia  * @errp: location to store error information
23743526ec8SWenchao Xia  *
238553a7e87SWenchao Xia  * Store "flat" image information in @p_info.
239553a7e87SWenchao Xia  *
240553a7e87SWenchao Xia  * "Flat" means it does *not* query backing image information,
241553a7e87SWenchao Xia  * i.e. (*pinfo)->has_backing_image will be set to false and
242553a7e87SWenchao Xia  * (*pinfo)->backing_image to NULL even when the image does in fact have
243553a7e87SWenchao Xia  * a backing image.
244553a7e87SWenchao Xia  *
24543526ec8SWenchao Xia  * @p_info will be set only on success. On error, store error in @errp.
24643526ec8SWenchao Xia  */
24743526ec8SWenchao Xia void bdrv_query_image_info(BlockDriverState *bs,
24843526ec8SWenchao Xia                            ImageInfo **p_info,
24943526ec8SWenchao Xia                            Error **errp)
250f364ec65SWenchao Xia {
25152bf1e72SMarkus Armbruster     int64_t size;
25243526ec8SWenchao Xia     const char *backing_filename;
253f364ec65SWenchao Xia     BlockDriverInfo bdi;
25443526ec8SWenchao Xia     int ret;
25543526ec8SWenchao Xia     Error *err = NULL;
25652bf1e72SMarkus Armbruster     ImageInfo *info;
257f364ec65SWenchao Xia 
2581963f8d5SPaolo Bonzini     aio_context_acquire(bdrv_get_aio_context(bs));
2591963f8d5SPaolo Bonzini 
26052bf1e72SMarkus Armbruster     size = bdrv_getlength(bs);
26152bf1e72SMarkus Armbruster     if (size < 0) {
2629adceb02SFam Zheng         error_setg_errno(errp, -size, "Can't get image size '%s'",
2639adceb02SFam Zheng                          bs->exact_filename);
2641963f8d5SPaolo Bonzini         goto out;
26552bf1e72SMarkus Armbruster     }
266f364ec65SWenchao Xia 
26752bf1e72SMarkus Armbruster     info = g_new0(ImageInfo, 1);
26843526ec8SWenchao Xia     info->filename        = g_strdup(bs->filename);
269f364ec65SWenchao Xia     info->format          = g_strdup(bdrv_get_format_name(bs));
27052bf1e72SMarkus Armbruster     info->virtual_size    = size;
271f364ec65SWenchao Xia     info->actual_size     = bdrv_get_allocated_file_size(bs);
272f364ec65SWenchao Xia     info->has_actual_size = info->actual_size >= 0;
273f364ec65SWenchao Xia     if (bdrv_is_encrypted(bs)) {
274f364ec65SWenchao Xia         info->encrypted = true;
275f364ec65SWenchao Xia         info->has_encrypted = true;
276f364ec65SWenchao Xia     }
277f364ec65SWenchao Xia     if (bdrv_get_info(bs, &bdi) >= 0) {
278f364ec65SWenchao Xia         if (bdi.cluster_size != 0) {
279f364ec65SWenchao Xia             info->cluster_size = bdi.cluster_size;
280f364ec65SWenchao Xia             info->has_cluster_size = true;
281f364ec65SWenchao Xia         }
282f364ec65SWenchao Xia         info->dirty_flag = bdi.is_dirty;
283f364ec65SWenchao Xia         info->has_dirty_flag = true;
284f364ec65SWenchao Xia     }
285*1bf6e9caSAndrey Shinkevich     info->format_specific = bdrv_get_specific_info(bs, &err);
286*1bf6e9caSAndrey Shinkevich     if (err) {
287*1bf6e9caSAndrey Shinkevich         error_propagate(errp, err);
288*1bf6e9caSAndrey Shinkevich         qapi_free_ImageInfo(info);
289*1bf6e9caSAndrey Shinkevich         goto out;
290*1bf6e9caSAndrey Shinkevich     }
291eae041feSMax Reitz     info->has_format_specific = info->format_specific != NULL;
292eae041feSMax Reitz 
29343526ec8SWenchao Xia     backing_filename = bs->backing_file;
294f364ec65SWenchao Xia     if (backing_filename[0] != '\0') {
2959a29e18fSJeff Cody         char *backing_filename2 = g_malloc0(PATH_MAX);
296f364ec65SWenchao Xia         info->backing_filename = g_strdup(backing_filename);
297f364ec65SWenchao Xia         info->has_backing_filename = true;
2989a29e18fSJeff Cody         bdrv_get_full_backing_filename(bs, backing_filename2, PATH_MAX, &err);
2999f07429eSMax Reitz         if (err) {
300a5002d53SJohn Snow             /* Can't reconstruct the full backing filename, so we must omit
301a5002d53SJohn Snow              * this field and apply a Best Effort to this query. */
302564d64bdSJeff Cody             g_free(backing_filename2);
303a5002d53SJohn Snow             backing_filename2 = NULL;
304a5002d53SJohn Snow             error_free(err);
3050fa296ebSFam Zheng             err = NULL;
3069f07429eSMax Reitz         }
307f364ec65SWenchao Xia 
30812dcb1c0SJohn Snow         /* Always report the full_backing_filename if present, even if it's the
30912dcb1c0SJohn Snow          * same as backing_filename. That they are same is useful info. */
31012dcb1c0SJohn Snow         if (backing_filename2) {
31112dcb1c0SJohn Snow             info->full_backing_filename = g_strdup(backing_filename2);
312f364ec65SWenchao Xia             info->has_full_backing_filename = true;
313f364ec65SWenchao Xia         }
314f364ec65SWenchao Xia 
315f364ec65SWenchao Xia         if (bs->backing_format[0]) {
316f364ec65SWenchao Xia             info->backing_filename_format = g_strdup(bs->backing_format);
317f364ec65SWenchao Xia             info->has_backing_filename_format = true;
318f364ec65SWenchao Xia         }
319564d64bdSJeff Cody         g_free(backing_filename2);
320f364ec65SWenchao Xia     }
32143526ec8SWenchao Xia 
32243526ec8SWenchao Xia     ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
32343526ec8SWenchao Xia     switch (ret) {
32443526ec8SWenchao Xia     case 0:
32543526ec8SWenchao Xia         if (info->snapshots) {
32643526ec8SWenchao Xia             info->has_snapshots = true;
32743526ec8SWenchao Xia         }
32843526ec8SWenchao Xia         break;
32943526ec8SWenchao Xia     /* recoverable error */
33043526ec8SWenchao Xia     case -ENOMEDIUM:
33143526ec8SWenchao Xia     case -ENOTSUP:
33243526ec8SWenchao Xia         error_free(err);
33343526ec8SWenchao Xia         break;
33443526ec8SWenchao Xia     default:
33543526ec8SWenchao Xia         error_propagate(errp, err);
33643526ec8SWenchao Xia         qapi_free_ImageInfo(info);
3371963f8d5SPaolo Bonzini         goto out;
33843526ec8SWenchao Xia     }
33943526ec8SWenchao Xia 
34043526ec8SWenchao Xia     *p_info = info;
3411963f8d5SPaolo Bonzini 
3421963f8d5SPaolo Bonzini out:
3431963f8d5SPaolo Bonzini     aio_context_release(bdrv_get_aio_context(bs));
344f364ec65SWenchao Xia }
345f364ec65SWenchao Xia 
346553a7e87SWenchao Xia /* @p_info will be set only on success. */
347d829a211SMarkus Armbruster static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
348553a7e87SWenchao Xia                             Error **errp)
349f364ec65SWenchao Xia {
350f364ec65SWenchao Xia     BlockInfo *info = g_malloc0(sizeof(*info));
351d829a211SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
35246eade7bSKevin Wolf     char *qdev;
35346eade7bSKevin Wolf 
354d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of */
355d3c8c674SKevin Wolf     while (bs && bs->drv && bs->implicit) {
356d3c8c674SKevin Wolf         bs = backing_bs(bs);
357d3c8c674SKevin Wolf     }
358d3c8c674SKevin Wolf 
359d829a211SMarkus Armbruster     info->device = g_strdup(blk_name(blk));
360f364ec65SWenchao Xia     info->type = g_strdup("unknown");
361a7f53e26SMarkus Armbruster     info->locked = blk_dev_is_medium_locked(blk);
362a7f53e26SMarkus Armbruster     info->removable = blk_dev_has_removable_media(blk);
363f364ec65SWenchao Xia 
36446eade7bSKevin Wolf     qdev = blk_get_attached_dev_id(blk);
36546eade7bSKevin Wolf     if (qdev && *qdev) {
36646eade7bSKevin Wolf         info->has_qdev = true;
36746eade7bSKevin Wolf         info->qdev = qdev;
36846eade7bSKevin Wolf     } else {
36946eade7bSKevin Wolf         g_free(qdev);
37046eade7bSKevin Wolf     }
37146eade7bSKevin Wolf 
372327032ceSMax Reitz     if (blk_dev_has_tray(blk)) {
373f364ec65SWenchao Xia         info->has_tray_open = true;
374a7f53e26SMarkus Armbruster         info->tray_open = blk_dev_is_tray_open(blk);
375f364ec65SWenchao Xia     }
376f364ec65SWenchao Xia 
377373340b2SMax Reitz     if (blk_iostatus_is_enabled(blk)) {
378f364ec65SWenchao Xia         info->has_io_status = true;
379373340b2SMax Reitz         info->io_status = blk_iostatus(blk);
380f364ec65SWenchao Xia     }
381f364ec65SWenchao Xia 
3825433c24fSMax Reitz     if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
38321b56835SFam Zheng         info->has_dirty_bitmaps = true;
38421b56835SFam Zheng         info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
38521b56835SFam Zheng     }
38621b56835SFam Zheng 
3875433c24fSMax Reitz     if (bs && bs->drv) {
388f364ec65SWenchao Xia         info->has_inserted = true;
389c83f9fbaSKevin Wolf         info->inserted = bdrv_block_device_info(blk, bs, errp);
390d5a8ee60SAlberto Garcia         if (info->inserted == NULL) {
391553a7e87SWenchao Xia             goto err;
392f364ec65SWenchao Xia         }
393553a7e87SWenchao Xia     }
394553a7e87SWenchao Xia 
395553a7e87SWenchao Xia     *p_info = info;
396553a7e87SWenchao Xia     return;
397553a7e87SWenchao Xia 
398553a7e87SWenchao Xia  err:
399553a7e87SWenchao Xia     qapi_free_BlockInfo(info);
400f364ec65SWenchao Xia }
401f364ec65SWenchao Xia 
4027e5c776dSVladimir Sementsov-Ogievskiy static uint64List *uint64_list(uint64_t *list, int size)
4037e5c776dSVladimir Sementsov-Ogievskiy {
4047e5c776dSVladimir Sementsov-Ogievskiy     int i;
4057e5c776dSVladimir Sementsov-Ogievskiy     uint64List *out_list = NULL;
4067e5c776dSVladimir Sementsov-Ogievskiy     uint64List **pout_list = &out_list;
4077e5c776dSVladimir Sementsov-Ogievskiy 
4087e5c776dSVladimir Sementsov-Ogievskiy     for (i = 0; i < size; i++) {
4097e5c776dSVladimir Sementsov-Ogievskiy         uint64List *entry = g_new(uint64List, 1);
4107e5c776dSVladimir Sementsov-Ogievskiy         entry->value = list[i];
4117e5c776dSVladimir Sementsov-Ogievskiy         *pout_list = entry;
4127e5c776dSVladimir Sementsov-Ogievskiy         pout_list = &entry->next;
4137e5c776dSVladimir Sementsov-Ogievskiy     }
4147e5c776dSVladimir Sementsov-Ogievskiy 
4157e5c776dSVladimir Sementsov-Ogievskiy     *pout_list = NULL;
4167e5c776dSVladimir Sementsov-Ogievskiy 
4177e5c776dSVladimir Sementsov-Ogievskiy     return out_list;
4187e5c776dSVladimir Sementsov-Ogievskiy }
4197e5c776dSVladimir Sementsov-Ogievskiy 
4207e5c776dSVladimir Sementsov-Ogievskiy static void bdrv_latency_histogram_stats(BlockLatencyHistogram *hist,
4217e5c776dSVladimir Sementsov-Ogievskiy                                          bool *not_null,
4227e5c776dSVladimir Sementsov-Ogievskiy                                          BlockLatencyHistogramInfo **info)
4237e5c776dSVladimir Sementsov-Ogievskiy {
4247e5c776dSVladimir Sementsov-Ogievskiy     *not_null = hist->bins != NULL;
4257e5c776dSVladimir Sementsov-Ogievskiy     if (*not_null) {
4267e5c776dSVladimir Sementsov-Ogievskiy         *info = g_new0(BlockLatencyHistogramInfo, 1);
4277e5c776dSVladimir Sementsov-Ogievskiy 
4287e5c776dSVladimir Sementsov-Ogievskiy         (*info)->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
4297e5c776dSVladimir Sementsov-Ogievskiy         (*info)->bins = uint64_list(hist->bins, hist->nbins);
4307e5c776dSVladimir Sementsov-Ogievskiy     }
4317e5c776dSVladimir Sementsov-Ogievskiy }
4327e5c776dSVladimir Sementsov-Ogievskiy 
43354302156SMax Reitz static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
434f364ec65SWenchao Xia {
4352b77e60aSKevin Wolf     BlockAcctStats *stats = blk_get_stats(blk);
436979e9b03SAlberto Garcia     BlockAcctTimedStats *ts = NULL;
4377f0e9da6SMax Reitz 
43854302156SMax Reitz     ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
43954302156SMax Reitz     ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
44054302156SMax Reitz     ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
44154302156SMax Reitz     ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
4427ee12dafSAlberto Garcia 
44354302156SMax Reitz     ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
44454302156SMax Reitz     ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
44554302156SMax Reitz     ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
4467ee12dafSAlberto Garcia 
44754302156SMax Reitz     ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
44854302156SMax Reitz     ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
44954302156SMax Reitz     ds->invalid_flush_operations =
4507ee12dafSAlberto Garcia         stats->invalid_ops[BLOCK_ACCT_FLUSH];
4517ee12dafSAlberto Garcia 
45254302156SMax Reitz     ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
45354302156SMax Reitz     ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
45454302156SMax Reitz     ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
45554302156SMax Reitz     ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
45654302156SMax Reitz     ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
45754302156SMax Reitz     ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
458cb38fffbSAlberto Garcia 
45954302156SMax Reitz     ds->has_idle_time_ns = stats->last_access_time_ns > 0;
46054302156SMax Reitz     if (ds->has_idle_time_ns) {
46154302156SMax Reitz         ds->idle_time_ns = block_acct_idle_time_ns(stats);
462cb38fffbSAlberto Garcia     }
463362e9299SAlberto Garcia 
46454302156SMax Reitz     ds->account_invalid = stats->account_invalid;
46554302156SMax Reitz     ds->account_failed = stats->account_failed;
466979e9b03SAlberto Garcia 
467979e9b03SAlberto Garcia     while ((ts = block_acct_interval_next(stats, ts))) {
468979e9b03SAlberto Garcia         BlockDeviceTimedStatsList *timed_stats =
469979e9b03SAlberto Garcia             g_malloc0(sizeof(*timed_stats));
470979e9b03SAlberto Garcia         BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
47154302156SMax Reitz         timed_stats->next = ds->timed_stats;
472979e9b03SAlberto Garcia         timed_stats->value = dev_stats;
47354302156SMax Reitz         ds->timed_stats = timed_stats;
474979e9b03SAlberto Garcia 
475979e9b03SAlberto Garcia         TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
476979e9b03SAlberto Garcia         TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
477979e9b03SAlberto Garcia         TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
478979e9b03SAlberto Garcia 
479979e9b03SAlberto Garcia         dev_stats->interval_length = ts->interval_length;
480979e9b03SAlberto Garcia 
481979e9b03SAlberto Garcia         dev_stats->min_rd_latency_ns = timed_average_min(rd);
482979e9b03SAlberto Garcia         dev_stats->max_rd_latency_ns = timed_average_max(rd);
483979e9b03SAlberto Garcia         dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
484979e9b03SAlberto Garcia 
485979e9b03SAlberto Garcia         dev_stats->min_wr_latency_ns = timed_average_min(wr);
486979e9b03SAlberto Garcia         dev_stats->max_wr_latency_ns = timed_average_max(wr);
487979e9b03SAlberto Garcia         dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
488979e9b03SAlberto Garcia 
489979e9b03SAlberto Garcia         dev_stats->min_flush_latency_ns = timed_average_min(fl);
490979e9b03SAlberto Garcia         dev_stats->max_flush_latency_ns = timed_average_max(fl);
491979e9b03SAlberto Garcia         dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
49296e4dedaSAlberto Garcia 
49396e4dedaSAlberto Garcia         dev_stats->avg_rd_queue_depth =
49496e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_READ);
49596e4dedaSAlberto Garcia         dev_stats->avg_wr_queue_depth =
49696e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
497979e9b03SAlberto Garcia     }
4987e5c776dSVladimir Sementsov-Ogievskiy 
4997e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_READ],
5007e5c776dSVladimir Sementsov-Ogievskiy                                  &ds->has_x_rd_latency_histogram,
5017e5c776dSVladimir Sementsov-Ogievskiy                                  &ds->x_rd_latency_histogram);
5027e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_WRITE],
5037e5c776dSVladimir Sementsov-Ogievskiy                                  &ds->has_x_wr_latency_histogram,
5047e5c776dSVladimir Sementsov-Ogievskiy                                  &ds->x_wr_latency_histogram);
5057e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_FLUSH],
5067e5c776dSVladimir Sementsov-Ogievskiy                                  &ds->has_x_flush_latency_histogram,
5077e5c776dSVladimir Sementsov-Ogievskiy                                  &ds->x_flush_latency_histogram);
5087f0e9da6SMax Reitz }
509f364ec65SWenchao Xia 
510d3c8c674SKevin Wolf static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
511d3c8c674SKevin Wolf                                         bool blk_level)
5122b77e60aSKevin Wolf {
51320a6d768SDou Liyang     BlockStats *s = NULL;
51420a6d768SDou Liyang 
51520a6d768SDou Liyang     s = g_malloc0(sizeof(*s));
51620a6d768SDou Liyang     s->stats = g_malloc0(sizeof(*s->stats));
51720a6d768SDou Liyang 
51820a6d768SDou Liyang     if (!bs) {
51920a6d768SDou Liyang         return s;
52020a6d768SDou Liyang     }
52120a6d768SDou Liyang 
522d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of in
523d3c8c674SKevin Wolf      * a BlockBackend-level command. Stay at the exact node for a node-level
524d3c8c674SKevin Wolf      * command. */
525d3c8c674SKevin Wolf     while (blk_level && bs->drv && bs->implicit) {
526d3c8c674SKevin Wolf         bs = backing_bs(bs);
527d3c8c674SKevin Wolf         assert(bs);
528d3c8c674SKevin Wolf     }
529d3c8c674SKevin Wolf 
5302b77e60aSKevin Wolf     if (bdrv_get_node_name(bs)[0]) {
5312b77e60aSKevin Wolf         s->has_node_name = true;
5322b77e60aSKevin Wolf         s->node_name = g_strdup(bdrv_get_node_name(bs));
5332b77e60aSKevin Wolf     }
5342b77e60aSKevin Wolf 
535f7946da2SPaolo Bonzini     s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
53653d8f9d8SMax Reitz 
537f364ec65SWenchao Xia     if (bs->file) {
538f364ec65SWenchao Xia         s->has_parent = true;
539d3c8c674SKevin Wolf         s->parent = bdrv_query_bds_stats(bs->file->bs, blk_level);
540f364ec65SWenchao Xia     }
541f364ec65SWenchao Xia 
542d3c8c674SKevin Wolf     if (blk_level && bs->backing) {
543c8059b97SFam Zheng         s->has_backing = true;
544d3c8c674SKevin Wolf         s->backing = bdrv_query_bds_stats(bs->backing->bs, blk_level);
545c8059b97SFam Zheng     }
546c8059b97SFam Zheng 
54720a6d768SDou Liyang     return s;
548b07363a1SKevin Wolf }
549b07363a1SKevin Wolf 
550f364ec65SWenchao Xia BlockInfoList *qmp_query_block(Error **errp)
551f364ec65SWenchao Xia {
552f364ec65SWenchao Xia     BlockInfoList *head = NULL, **p_next = &head;
553d829a211SMarkus Armbruster     BlockBackend *blk;
554553a7e87SWenchao Xia     Error *local_err = NULL;
555f364ec65SWenchao Xia 
556d5b68844SKevin Wolf     for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
557d5b68844SKevin Wolf         BlockInfoList *info;
558d5b68844SKevin Wolf 
559ec18b0a9SKevin Wolf         if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
560d5b68844SKevin Wolf             continue;
561d5b68844SKevin Wolf         }
562d5b68844SKevin Wolf 
563d5b68844SKevin Wolf         info = g_malloc0(sizeof(*info));
564d829a211SMarkus Armbruster         bdrv_query_info(blk, &info->value, &local_err);
56584d18f06SMarkus Armbruster         if (local_err) {
566553a7e87SWenchao Xia             error_propagate(errp, local_err);
567903c341dSMarkus Armbruster             g_free(info);
568903c341dSMarkus Armbruster             qapi_free_BlockInfoList(head);
569903c341dSMarkus Armbruster             return NULL;
570553a7e87SWenchao Xia         }
571f364ec65SWenchao Xia 
572f364ec65SWenchao Xia         *p_next = info;
573f364ec65SWenchao Xia         p_next = &info->next;
574f364ec65SWenchao Xia     }
575f364ec65SWenchao Xia 
576f364ec65SWenchao Xia     return head;
577f364ec65SWenchao Xia }
578f364ec65SWenchao Xia 
579f71eaa74SFam Zheng BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
580f71eaa74SFam Zheng                                      bool query_nodes,
581f71eaa74SFam Zheng                                      Error **errp)
582f364ec65SWenchao Xia {
583f364ec65SWenchao Xia     BlockStatsList *head = NULL, **p_next = &head;
584a6baa608SDou Liyang     BlockBackend *blk;
585a6baa608SDou Liyang     BlockDriverState *bs;
586f364ec65SWenchao Xia 
587f71eaa74SFam Zheng     /* Just to be safe if query_nodes is not always initialized */
588a6baa608SDou Liyang     if (has_query_nodes && query_nodes) {
589a6baa608SDou Liyang         for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
590f364ec65SWenchao Xia             BlockStatsList *info = g_malloc0(sizeof(*info));
591a6baa608SDou Liyang             AioContext *ctx = bdrv_get_aio_context(bs);
59213344f3aSStefan Hajnoczi 
59313344f3aSStefan Hajnoczi             aio_context_acquire(ctx);
594a6baa608SDou Liyang             info->value = bdrv_query_bds_stats(bs, false);
59513344f3aSStefan Hajnoczi             aio_context_release(ctx);
596f364ec65SWenchao Xia 
597f364ec65SWenchao Xia             *p_next = info;
598f364ec65SWenchao Xia             p_next = &info->next;
599f364ec65SWenchao Xia         }
600a6baa608SDou Liyang     } else {
601567dcb31SKevin Wolf         for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
602f62492bbSKevin Wolf             BlockStatsList *info;
603a6baa608SDou Liyang             AioContext *ctx = blk_get_aio_context(blk);
604a6baa608SDou Liyang             BlockStats *s;
6055a9cb5a9SKevin Wolf             char *qdev;
606a6baa608SDou Liyang 
607567dcb31SKevin Wolf             if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
608567dcb31SKevin Wolf                 continue;
609567dcb31SKevin Wolf             }
610567dcb31SKevin Wolf 
611a6baa608SDou Liyang             aio_context_acquire(ctx);
612a6baa608SDou Liyang             s = bdrv_query_bds_stats(blk_bs(blk), true);
613a6baa608SDou Liyang             s->has_device = true;
614a6baa608SDou Liyang             s->device = g_strdup(blk_name(blk));
6155a9cb5a9SKevin Wolf 
6165a9cb5a9SKevin Wolf             qdev = blk_get_attached_dev_id(blk);
6175a9cb5a9SKevin Wolf             if (qdev && *qdev) {
6185a9cb5a9SKevin Wolf                 s->has_qdev = true;
6195a9cb5a9SKevin Wolf                 s->qdev = qdev;
6205a9cb5a9SKevin Wolf             } else {
6215a9cb5a9SKevin Wolf                 g_free(qdev);
6225a9cb5a9SKevin Wolf             }
6235a9cb5a9SKevin Wolf 
624a6baa608SDou Liyang             bdrv_query_blk_stats(s->stats, blk);
625a6baa608SDou Liyang             aio_context_release(ctx);
626a6baa608SDou Liyang 
627f62492bbSKevin Wolf             info = g_malloc0(sizeof(*info));
628a6baa608SDou Liyang             info->value = s;
629a6baa608SDou Liyang             *p_next = info;
630a6baa608SDou Liyang             p_next = &info->next;
631a6baa608SDou Liyang         }
632a6baa608SDou Liyang     }
633f364ec65SWenchao Xia 
634f364ec65SWenchao Xia     return head;
635f364ec65SWenchao Xia }
636f364ec65SWenchao Xia 
637f364ec65SWenchao Xia #define NB_SUFFIXES 4
638f364ec65SWenchao Xia 
639f364ec65SWenchao Xia static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
640f364ec65SWenchao Xia {
6412c20fa2cSStefan Weil     static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'};
642f364ec65SWenchao Xia     int64_t base;
643f364ec65SWenchao Xia     int i;
644f364ec65SWenchao Xia 
645f364ec65SWenchao Xia     if (size <= 999) {
646f364ec65SWenchao Xia         snprintf(buf, buf_size, "%" PRId64, size);
647f364ec65SWenchao Xia     } else {
648f364ec65SWenchao Xia         base = 1024;
649f364ec65SWenchao Xia         for (i = 0; i < NB_SUFFIXES; i++) {
650f364ec65SWenchao Xia             if (size < (10 * base)) {
651f364ec65SWenchao Xia                 snprintf(buf, buf_size, "%0.1f%c",
652f364ec65SWenchao Xia                          (double)size / base,
653f364ec65SWenchao Xia                          suffixes[i]);
654f364ec65SWenchao Xia                 break;
655f364ec65SWenchao Xia             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
656f364ec65SWenchao Xia                 snprintf(buf, buf_size, "%" PRId64 "%c",
657f364ec65SWenchao Xia                          ((size + (base >> 1)) / base),
658f364ec65SWenchao Xia                          suffixes[i]);
659f364ec65SWenchao Xia                 break;
660f364ec65SWenchao Xia             }
661f364ec65SWenchao Xia             base = base * 1024;
662f364ec65SWenchao Xia         }
663f364ec65SWenchao Xia     }
664f364ec65SWenchao Xia     return buf;
665f364ec65SWenchao Xia }
666f364ec65SWenchao Xia 
6675b917044SWenchao Xia void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
6685b917044SWenchao Xia                         QEMUSnapshotInfo *sn)
669f364ec65SWenchao Xia {
670f364ec65SWenchao Xia     char buf1[128], date_buf[128], clock_buf[128];
671f364ec65SWenchao Xia     struct tm tm;
672f364ec65SWenchao Xia     time_t ti;
673f364ec65SWenchao Xia     int64_t secs;
674f364ec65SWenchao Xia 
675f364ec65SWenchao Xia     if (!sn) {
6765b917044SWenchao Xia         func_fprintf(f,
677f364ec65SWenchao Xia                      "%-10s%-20s%7s%20s%15s",
678f364ec65SWenchao Xia                      "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
679f364ec65SWenchao Xia     } else {
680f364ec65SWenchao Xia         ti = sn->date_sec;
681f364ec65SWenchao Xia         localtime_r(&ti, &tm);
682f364ec65SWenchao Xia         strftime(date_buf, sizeof(date_buf),
683f364ec65SWenchao Xia                  "%Y-%m-%d %H:%M:%S", &tm);
684f364ec65SWenchao Xia         secs = sn->vm_clock_nsec / 1000000000;
685f364ec65SWenchao Xia         snprintf(clock_buf, sizeof(clock_buf),
686f364ec65SWenchao Xia                  "%02d:%02d:%02d.%03d",
687f364ec65SWenchao Xia                  (int)(secs / 3600),
688f364ec65SWenchao Xia                  (int)((secs / 60) % 60),
689f364ec65SWenchao Xia                  (int)(secs % 60),
690f364ec65SWenchao Xia                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
6915b917044SWenchao Xia         func_fprintf(f,
692f364ec65SWenchao Xia                      "%-10s%-20s%7s%20s%15s",
693f364ec65SWenchao Xia                      sn->id_str, sn->name,
6945b917044SWenchao Xia                      get_human_readable_size(buf1, sizeof(buf1),
6955b917044SWenchao Xia                                              sn->vm_state_size),
696f364ec65SWenchao Xia                      date_buf,
697f364ec65SWenchao Xia                      clock_buf);
698f364ec65SWenchao Xia     }
699f364ec65SWenchao Xia }
700f364ec65SWenchao Xia 
701a8d8ecb7SMax Reitz static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
702a8d8ecb7SMax Reitz                        QDict *dict);
703a8d8ecb7SMax Reitz static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
704a8d8ecb7SMax Reitz                        QList *list);
705a8d8ecb7SMax Reitz 
706a8d8ecb7SMax Reitz static void dump_qobject(fprintf_function func_fprintf, void *f,
707a8d8ecb7SMax Reitz                          int comp_indent, QObject *obj)
708a8d8ecb7SMax Reitz {
709a8d8ecb7SMax Reitz     switch (qobject_type(obj)) {
71001b2ffceSMarc-André Lureau         case QTYPE_QNUM: {
7117dc847ebSMax Reitz             QNum *value = qobject_to(QNum, obj);
71201b2ffceSMarc-André Lureau             char *tmp = qnum_to_string(value);
71301b2ffceSMarc-André Lureau             func_fprintf(f, "%s", tmp);
71401b2ffceSMarc-André Lureau             g_free(tmp);
715a8d8ecb7SMax Reitz             break;
716a8d8ecb7SMax Reitz         }
717a8d8ecb7SMax Reitz         case QTYPE_QSTRING: {
7187dc847ebSMax Reitz             QString *value = qobject_to(QString, obj);
719a8d8ecb7SMax Reitz             func_fprintf(f, "%s", qstring_get_str(value));
720a8d8ecb7SMax Reitz             break;
721a8d8ecb7SMax Reitz         }
722a8d8ecb7SMax Reitz         case QTYPE_QDICT: {
7237dc847ebSMax Reitz             QDict *value = qobject_to(QDict, obj);
724a8d8ecb7SMax Reitz             dump_qdict(func_fprintf, f, comp_indent, value);
725a8d8ecb7SMax Reitz             break;
726a8d8ecb7SMax Reitz         }
727a8d8ecb7SMax Reitz         case QTYPE_QLIST: {
7287dc847ebSMax Reitz             QList *value = qobject_to(QList, obj);
729a8d8ecb7SMax Reitz             dump_qlist(func_fprintf, f, comp_indent, value);
730a8d8ecb7SMax Reitz             break;
731a8d8ecb7SMax Reitz         }
732a8d8ecb7SMax Reitz         case QTYPE_QBOOL: {
7337dc847ebSMax Reitz             QBool *value = qobject_to(QBool, obj);
734fc48ffc3SEric Blake             func_fprintf(f, "%s", qbool_get_bool(value) ? "true" : "false");
735a8d8ecb7SMax Reitz             break;
736a8d8ecb7SMax Reitz         }
737a8d8ecb7SMax Reitz         default:
738a8d8ecb7SMax Reitz             abort();
739a8d8ecb7SMax Reitz     }
740a8d8ecb7SMax Reitz }
741a8d8ecb7SMax Reitz 
742a8d8ecb7SMax Reitz static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
743a8d8ecb7SMax Reitz                        QList *list)
744a8d8ecb7SMax Reitz {
745a8d8ecb7SMax Reitz     const QListEntry *entry;
746a8d8ecb7SMax Reitz     int i = 0;
747a8d8ecb7SMax Reitz 
748a8d8ecb7SMax Reitz     for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
7491310a3d3SEric Blake         QType type = qobject_type(entry->value);
750a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
751853ccfedSPeter Xu         func_fprintf(f, "%*s[%i]:%c", indentation * 4, "", i,
752853ccfedSPeter Xu                      composite ? '\n' : ' ');
753a8d8ecb7SMax Reitz         dump_qobject(func_fprintf, f, indentation + 1, entry->value);
754a8d8ecb7SMax Reitz         if (!composite) {
755a8d8ecb7SMax Reitz             func_fprintf(f, "\n");
756a8d8ecb7SMax Reitz         }
757a8d8ecb7SMax Reitz     }
758a8d8ecb7SMax Reitz }
759a8d8ecb7SMax Reitz 
760a8d8ecb7SMax Reitz static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
761a8d8ecb7SMax Reitz                        QDict *dict)
762a8d8ecb7SMax Reitz {
763a8d8ecb7SMax Reitz     const QDictEntry *entry;
764a8d8ecb7SMax Reitz 
765a8d8ecb7SMax Reitz     for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
7661310a3d3SEric Blake         QType type = qobject_type(entry->value);
767a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
7685eda6227SPeter Xu         char *key = g_malloc(strlen(entry->key) + 1);
769a8d8ecb7SMax Reitz         int i;
770a8d8ecb7SMax Reitz 
771a8d8ecb7SMax Reitz         /* replace dashes with spaces in key (variable) names */
772a8d8ecb7SMax Reitz         for (i = 0; entry->key[i]; i++) {
773a8d8ecb7SMax Reitz             key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
774a8d8ecb7SMax Reitz         }
775a8d8ecb7SMax Reitz         key[i] = 0;
776853ccfedSPeter Xu         func_fprintf(f, "%*s%s:%c", indentation * 4, "", key,
777853ccfedSPeter Xu                      composite ? '\n' : ' ');
778a8d8ecb7SMax Reitz         dump_qobject(func_fprintf, f, indentation + 1, entry->value);
779a8d8ecb7SMax Reitz         if (!composite) {
780a8d8ecb7SMax Reitz             func_fprintf(f, "\n");
781a8d8ecb7SMax Reitz         }
7825eda6227SPeter Xu         g_free(key);
783a8d8ecb7SMax Reitz     }
784a8d8ecb7SMax Reitz }
785a8d8ecb7SMax Reitz 
786a8d8ecb7SMax Reitz void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
787a8d8ecb7SMax Reitz                                    ImageInfoSpecific *info_spec)
788a8d8ecb7SMax Reitz {
789a8d8ecb7SMax Reitz     QObject *obj, *data;
7907d5e199aSDaniel P. Berrange     Visitor *v = qobject_output_visitor_new(&obj);
791a8d8ecb7SMax Reitz 
7923b098d56SEric Blake     visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
7933b098d56SEric Blake     visit_complete(v, &obj);
7947dc847ebSMax Reitz     data = qdict_get(qobject_to(QDict, obj), "data");
795a8d8ecb7SMax Reitz     dump_qobject(func_fprintf, f, 1, data);
796cb3e7f08SMarc-André Lureau     qobject_unref(obj);
7973b098d56SEric Blake     visit_free(v);
798a8d8ecb7SMax Reitz }
799a8d8ecb7SMax Reitz 
8005b917044SWenchao Xia void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
8015b917044SWenchao Xia                           ImageInfo *info)
802f364ec65SWenchao Xia {
803f364ec65SWenchao Xia     char size_buf[128], dsize_buf[128];
804f364ec65SWenchao Xia     if (!info->has_actual_size) {
805f364ec65SWenchao Xia         snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
806f364ec65SWenchao Xia     } else {
807f364ec65SWenchao Xia         get_human_readable_size(dsize_buf, sizeof(dsize_buf),
808f364ec65SWenchao Xia                                 info->actual_size);
809f364ec65SWenchao Xia     }
810f364ec65SWenchao Xia     get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
8115b917044SWenchao Xia     func_fprintf(f,
8125b917044SWenchao Xia                  "image: %s\n"
813f364ec65SWenchao Xia                  "file format: %s\n"
814f364ec65SWenchao Xia                  "virtual size: %s (%" PRId64 " bytes)\n"
815f364ec65SWenchao Xia                  "disk size: %s\n",
816f364ec65SWenchao Xia                  info->filename, info->format, size_buf,
817f364ec65SWenchao Xia                  info->virtual_size,
818f364ec65SWenchao Xia                  dsize_buf);
819f364ec65SWenchao Xia 
820f364ec65SWenchao Xia     if (info->has_encrypted && info->encrypted) {
8215b917044SWenchao Xia         func_fprintf(f, "encrypted: yes\n");
822f364ec65SWenchao Xia     }
823f364ec65SWenchao Xia 
824f364ec65SWenchao Xia     if (info->has_cluster_size) {
8255b917044SWenchao Xia         func_fprintf(f, "cluster_size: %" PRId64 "\n",
8265b917044SWenchao Xia                        info->cluster_size);
827f364ec65SWenchao Xia     }
828f364ec65SWenchao Xia 
829f364ec65SWenchao Xia     if (info->has_dirty_flag && info->dirty_flag) {
8305b917044SWenchao Xia         func_fprintf(f, "cleanly shut down: no\n");
831f364ec65SWenchao Xia     }
832f364ec65SWenchao Xia 
833f364ec65SWenchao Xia     if (info->has_backing_filename) {
8345b917044SWenchao Xia         func_fprintf(f, "backing file: %s", info->backing_filename);
8355c9d9ca5SJohn Snow         if (!info->has_full_backing_filename) {
8365c9d9ca5SJohn Snow             func_fprintf(f, " (cannot determine actual path)");
8375c9d9ca5SJohn Snow         } else if (strcmp(info->backing_filename,
8385c9d9ca5SJohn Snow                           info->full_backing_filename) != 0) {
8395b917044SWenchao Xia             func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
840f364ec65SWenchao Xia         }
8415b917044SWenchao Xia         func_fprintf(f, "\n");
842f364ec65SWenchao Xia         if (info->has_backing_filename_format) {
8435b917044SWenchao Xia             func_fprintf(f, "backing file format: %s\n",
8445b917044SWenchao Xia                          info->backing_filename_format);
845f364ec65SWenchao Xia         }
846f364ec65SWenchao Xia     }
847f364ec65SWenchao Xia 
848f364ec65SWenchao Xia     if (info->has_snapshots) {
849f364ec65SWenchao Xia         SnapshotInfoList *elem;
850f364ec65SWenchao Xia 
8515b917044SWenchao Xia         func_fprintf(f, "Snapshot list:\n");
8525b917044SWenchao Xia         bdrv_snapshot_dump(func_fprintf, f, NULL);
8535b917044SWenchao Xia         func_fprintf(f, "\n");
854f364ec65SWenchao Xia 
855f364ec65SWenchao Xia         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
856f364ec65SWenchao Xia          * we convert to the block layer's native QEMUSnapshotInfo for now.
857f364ec65SWenchao Xia          */
858f364ec65SWenchao Xia         for (elem = info->snapshots; elem; elem = elem->next) {
859f364ec65SWenchao Xia             QEMUSnapshotInfo sn = {
860f364ec65SWenchao Xia                 .vm_state_size = elem->value->vm_state_size,
861f364ec65SWenchao Xia                 .date_sec = elem->value->date_sec,
862f364ec65SWenchao Xia                 .date_nsec = elem->value->date_nsec,
863f364ec65SWenchao Xia                 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
864f364ec65SWenchao Xia                                  elem->value->vm_clock_nsec,
865f364ec65SWenchao Xia             };
866f364ec65SWenchao Xia 
867f364ec65SWenchao Xia             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
868f364ec65SWenchao Xia             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
8695b917044SWenchao Xia             bdrv_snapshot_dump(func_fprintf, f, &sn);
8705b917044SWenchao Xia             func_fprintf(f, "\n");
871f364ec65SWenchao Xia         }
872f364ec65SWenchao Xia     }
873a8d8ecb7SMax Reitz 
874a8d8ecb7SMax Reitz     if (info->has_format_specific) {
875a8d8ecb7SMax Reitz         func_fprintf(f, "Format specific information:\n");
876a8d8ecb7SMax Reitz         bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);
877a8d8ecb7SMax Reitz     }
878f364ec65SWenchao Xia }
879