xref: /qemu/block/qapi.c (revision 3f26191c)
1f364ec65SWenchao Xia /*
2f364ec65SWenchao Xia  * Block layer qmp and info dump related functions
3f364ec65SWenchao Xia  *
4f364ec65SWenchao Xia  * Copyright (c) 2003-2008 Fabrice Bellard
5f364ec65SWenchao Xia  *
6f364ec65SWenchao Xia  * Permission is hereby granted, free of charge, to any person obtaining a copy
7f364ec65SWenchao Xia  * of this software and associated documentation files (the "Software"), to deal
8f364ec65SWenchao Xia  * in the Software without restriction, including without limitation the rights
9f364ec65SWenchao Xia  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10f364ec65SWenchao Xia  * copies of the Software, and to permit persons to whom the Software is
11f364ec65SWenchao Xia  * furnished to do so, subject to the following conditions:
12f364ec65SWenchao Xia  *
13f364ec65SWenchao Xia  * The above copyright notice and this permission notice shall be included in
14f364ec65SWenchao Xia  * all copies or substantial portions of the Software.
15f364ec65SWenchao Xia  *
16f364ec65SWenchao Xia  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17f364ec65SWenchao Xia  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18f364ec65SWenchao Xia  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19f364ec65SWenchao Xia  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20f364ec65SWenchao Xia  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21f364ec65SWenchao Xia  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22f364ec65SWenchao Xia  * THE SOFTWARE.
23f364ec65SWenchao Xia  */
24f364ec65SWenchao Xia 
2580c71a24SPeter Maydell #include "qemu/osdep.h"
2631e40415SPhilippe Mathieu-Daudé #include "qemu/cutils.h"
27f364ec65SWenchao Xia #include "block/qapi.h"
28f364ec65SWenchao Xia #include "block/block_int.h"
2976f4afb4SAlberto Garcia #include "block/throttle-groups.h"
30e2462113SFrancesco Romani #include "block/write-threshold.h"
31e688df6bSMarkus Armbruster #include "qapi/error.h"
329af23989SMarkus Armbruster #include "qapi/qapi-commands-block-core.h"
33b3db211fSDaniel P. Berrange #include "qapi/qobject-output-visitor.h"
349af23989SMarkus Armbruster #include "qapi/qapi-visit-block-core.h"
356b673957SMarkus Armbruster #include "qapi/qmp/qbool.h"
36452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
3747e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h"
3815280c36SMarkus Armbruster #include "qapi/qmp/qnum.h"
396b673957SMarkus Armbruster #include "qapi/qmp/qstring.h"
40e1ce7d74SMarkus Armbruster #include "qemu/qemu-print.h"
41d829a211SMarkus Armbruster #include "sysemu/block-backend.h"
42f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
43f364ec65SWenchao Xia 
44c83f9fbaSKevin Wolf BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
45facda544SPeter Krempa                                         BlockDriverState *bs,
46facda544SPeter Krempa                                         bool flat,
47facda544SPeter Krempa                                         Error **errp)
48c13163fbSBenoît Canet {
49d5a8ee60SAlberto Garcia     ImageInfo **p_image_info;
50d5a8ee60SAlberto Garcia     BlockDriverState *bs0;
51d470ad42SMax Reitz     BlockDeviceInfo *info;
52c13163fbSBenoît Canet 
53d470ad42SMax Reitz     if (!bs->drv) {
54d470ad42SMax Reitz         error_setg(errp, "Block device %s is ejected", bs->node_name);
55d470ad42SMax Reitz         return NULL;
56d470ad42SMax Reitz     }
57d470ad42SMax Reitz 
58f30c66baSMax Reitz     bdrv_refresh_filename(bs);
59f30c66baSMax Reitz 
60d470ad42SMax Reitz     info = g_malloc0(sizeof(*info));
61c13163fbSBenoît Canet     info->file                   = g_strdup(bs->filename);
62c13163fbSBenoît Canet     info->ro                     = bs->read_only;
63c13163fbSBenoît Canet     info->drv                    = g_strdup(bs->drv->format_name);
64c13163fbSBenoît Canet     info->encrypted              = bs->encrypted;
65c01c214bSDaniel P. Berrange     info->encryption_key_missing = false;
66c13163fbSBenoît Canet 
679e193c5aSKevin Wolf     info->cache = g_new(BlockdevCacheInfo, 1);
689e193c5aSKevin Wolf     *info->cache = (BlockdevCacheInfo) {
69c83f9fbaSKevin Wolf         .writeback      = blk ? blk_enable_write_cache(blk) : true,
709e193c5aSKevin Wolf         .direct         = !!(bs->open_flags & BDRV_O_NOCACHE),
719e193c5aSKevin Wolf         .no_flush       = !!(bs->open_flags & BDRV_O_NO_FLUSH),
729e193c5aSKevin Wolf     };
739e193c5aSKevin Wolf 
74c13163fbSBenoît Canet     if (bs->node_name[0]) {
75c13163fbSBenoît Canet         info->has_node_name = true;
76c13163fbSBenoît Canet         info->node_name = g_strdup(bs->node_name);
77c13163fbSBenoît Canet     }
78c13163fbSBenoît Canet 
79c13163fbSBenoît Canet     if (bs->backing_file[0]) {
80c13163fbSBenoît Canet         info->has_backing_file = true;
81c13163fbSBenoît Canet         info->backing_file = g_strdup(bs->backing_file);
82c13163fbSBenoît Canet     }
83c13163fbSBenoît Canet 
84590a63d5SVladimir Sementsov-Ogievskiy     if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
85590a63d5SVladimir Sementsov-Ogievskiy         info->has_dirty_bitmaps = true;
86590a63d5SVladimir Sementsov-Ogievskiy         info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
87590a63d5SVladimir Sementsov-Ogievskiy     }
88590a63d5SVladimir Sementsov-Ogievskiy 
89465bee1dSPeter Lieven     info->detect_zeroes = bs->detect_zeroes;
90c13163fbSBenoît Canet 
91022cdc9fSManos Pitsidianakis     if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
92c13163fbSBenoît Canet         ThrottleConfig cfg;
93022cdc9fSManos Pitsidianakis         BlockBackendPublic *blkp = blk_get_public(blk);
9476f4afb4SAlberto Garcia 
95022cdc9fSManos Pitsidianakis         throttle_group_get_config(&blkp->throttle_group_member, &cfg);
9676f4afb4SAlberto Garcia 
97c13163fbSBenoît Canet         info->bps     = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
98c13163fbSBenoît Canet         info->bps_rd  = cfg.buckets[THROTTLE_BPS_READ].avg;
99c13163fbSBenoît Canet         info->bps_wr  = cfg.buckets[THROTTLE_BPS_WRITE].avg;
100c13163fbSBenoît Canet 
101c13163fbSBenoît Canet         info->iops    = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
102c13163fbSBenoît Canet         info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
103c13163fbSBenoît Canet         info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
104c13163fbSBenoît Canet 
105c13163fbSBenoît Canet         info->has_bps_max     = cfg.buckets[THROTTLE_BPS_TOTAL].max;
106c13163fbSBenoît Canet         info->bps_max         = cfg.buckets[THROTTLE_BPS_TOTAL].max;
107c13163fbSBenoît Canet         info->has_bps_rd_max  = cfg.buckets[THROTTLE_BPS_READ].max;
108c13163fbSBenoît Canet         info->bps_rd_max      = cfg.buckets[THROTTLE_BPS_READ].max;
109c13163fbSBenoît Canet         info->has_bps_wr_max  = cfg.buckets[THROTTLE_BPS_WRITE].max;
110c13163fbSBenoît Canet         info->bps_wr_max      = cfg.buckets[THROTTLE_BPS_WRITE].max;
111c13163fbSBenoît Canet 
112c13163fbSBenoît Canet         info->has_iops_max    = cfg.buckets[THROTTLE_OPS_TOTAL].max;
113c13163fbSBenoît Canet         info->iops_max        = cfg.buckets[THROTTLE_OPS_TOTAL].max;
114c13163fbSBenoît Canet         info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
115c13163fbSBenoît Canet         info->iops_rd_max     = cfg.buckets[THROTTLE_OPS_READ].max;
116c13163fbSBenoît Canet         info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
117c13163fbSBenoît Canet         info->iops_wr_max     = cfg.buckets[THROTTLE_OPS_WRITE].max;
118c13163fbSBenoît Canet 
119398befdfSAlberto Garcia         info->has_bps_max_length     = info->has_bps_max;
120398befdfSAlberto Garcia         info->bps_max_length         =
121398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
122398befdfSAlberto Garcia         info->has_bps_rd_max_length  = info->has_bps_rd_max;
123398befdfSAlberto Garcia         info->bps_rd_max_length      =
124398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_READ].burst_length;
125398befdfSAlberto Garcia         info->has_bps_wr_max_length  = info->has_bps_wr_max;
126398befdfSAlberto Garcia         info->bps_wr_max_length      =
127398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
128398befdfSAlberto Garcia 
129398befdfSAlberto Garcia         info->has_iops_max_length    = info->has_iops_max;
130398befdfSAlberto Garcia         info->iops_max_length        =
131398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
132398befdfSAlberto Garcia         info->has_iops_rd_max_length = info->has_iops_rd_max;
133398befdfSAlberto Garcia         info->iops_rd_max_length     =
134398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_READ].burst_length;
135398befdfSAlberto Garcia         info->has_iops_wr_max_length = info->has_iops_wr_max;
136398befdfSAlberto Garcia         info->iops_wr_max_length     =
137398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
138398befdfSAlberto Garcia 
139c13163fbSBenoît Canet         info->has_iops_size = cfg.op_size;
140c13163fbSBenoît Canet         info->iops_size = cfg.op_size;
141b8fe1694SAlberto Garcia 
142b8fe1694SAlberto Garcia         info->has_group = true;
143022cdc9fSManos Pitsidianakis         info->group =
144022cdc9fSManos Pitsidianakis             g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
145c13163fbSBenoît Canet     }
146c13163fbSBenoît Canet 
147e2462113SFrancesco Romani     info->write_threshold = bdrv_write_threshold_get(bs);
148e2462113SFrancesco Romani 
149d5a8ee60SAlberto Garcia     bs0 = bs;
150d5a8ee60SAlberto Garcia     p_image_info = &info->image;
151d3c8c674SKevin Wolf     info->backing_file_depth = 0;
152d5a8ee60SAlberto Garcia     while (1) {
153d5a8ee60SAlberto Garcia         Error *local_err = NULL;
154d5a8ee60SAlberto Garcia         bdrv_query_image_info(bs0, p_image_info, &local_err);
155d5a8ee60SAlberto Garcia         if (local_err) {
156d5a8ee60SAlberto Garcia             error_propagate(errp, local_err);
157d5a8ee60SAlberto Garcia             qapi_free_BlockDeviceInfo(info);
158d5a8ee60SAlberto Garcia             return NULL;
159d5a8ee60SAlberto Garcia         }
160d3c8c674SKevin Wolf 
161facda544SPeter Krempa         /* stop gathering data for flat output */
162facda544SPeter Krempa         if (flat) {
163facda544SPeter Krempa             break;
164facda544SPeter Krempa         }
165facda544SPeter Krempa 
166760e0063SKevin Wolf         if (bs0->drv && bs0->backing) {
167d3c8c674SKevin Wolf             info->backing_file_depth++;
168760e0063SKevin Wolf             bs0 = bs0->backing->bs;
169d5a8ee60SAlberto Garcia             (*p_image_info)->has_backing_image = true;
170d5a8ee60SAlberto Garcia             p_image_info = &((*p_image_info)->backing_image);
171d5a8ee60SAlberto Garcia         } else {
172d5a8ee60SAlberto Garcia             break;
173d5a8ee60SAlberto Garcia         }
174d3c8c674SKevin Wolf 
175d3c8c674SKevin Wolf         /* Skip automatically inserted nodes that the user isn't aware of for
176d3c8c674SKevin Wolf          * query-block (blk != NULL), but not for query-named-block-nodes */
1778e8eb0a9SKevin Wolf         while (blk && bs0->drv && bs0->implicit) {
178d3c8c674SKevin Wolf             bs0 = backing_bs(bs0);
1798e8eb0a9SKevin Wolf             assert(bs0);
180d3c8c674SKevin Wolf         }
181d5a8ee60SAlberto Garcia     }
182d5a8ee60SAlberto Garcia 
183c13163fbSBenoît Canet     return info;
184c13163fbSBenoît Canet }
185c13163fbSBenoît Canet 
186fb0ed453SWenchao Xia /*
187fb0ed453SWenchao Xia  * Returns 0 on success, with *p_list either set to describe snapshot
188fb0ed453SWenchao Xia  * information, or NULL because there are no snapshots.  Returns -errno on
189fb0ed453SWenchao Xia  * error, with *p_list untouched.
190fb0ed453SWenchao Xia  */
191fb0ed453SWenchao Xia int bdrv_query_snapshot_info_list(BlockDriverState *bs,
192fb0ed453SWenchao Xia                                   SnapshotInfoList **p_list,
193fb0ed453SWenchao Xia                                   Error **errp)
194f364ec65SWenchao Xia {
195f364ec65SWenchao Xia     int i, sn_count;
196f364ec65SWenchao Xia     QEMUSnapshotInfo *sn_tab = NULL;
197fb0ed453SWenchao Xia     SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
198fb0ed453SWenchao Xia     SnapshotInfo *info;
199fb0ed453SWenchao Xia 
200f364ec65SWenchao Xia     sn_count = bdrv_snapshot_list(bs, &sn_tab);
201fb0ed453SWenchao Xia     if (sn_count < 0) {
202fb0ed453SWenchao Xia         const char *dev = bdrv_get_device_name(bs);
203fb0ed453SWenchao Xia         switch (sn_count) {
204fb0ed453SWenchao Xia         case -ENOMEDIUM:
205fb0ed453SWenchao Xia             error_setg(errp, "Device '%s' is not inserted", dev);
206fb0ed453SWenchao Xia             break;
207fb0ed453SWenchao Xia         case -ENOTSUP:
208fb0ed453SWenchao Xia             error_setg(errp,
209fb0ed453SWenchao Xia                        "Device '%s' does not support internal snapshots",
210fb0ed453SWenchao Xia                        dev);
211fb0ed453SWenchao Xia             break;
212fb0ed453SWenchao Xia         default:
213fb0ed453SWenchao Xia             error_setg_errno(errp, -sn_count,
214fb0ed453SWenchao Xia                              "Can't list snapshots of device '%s'", dev);
215fb0ed453SWenchao Xia             break;
216fb0ed453SWenchao Xia         }
217fb0ed453SWenchao Xia         return sn_count;
218fb0ed453SWenchao Xia     }
219f364ec65SWenchao Xia 
220f364ec65SWenchao Xia     for (i = 0; i < sn_count; i++) {
221fb0ed453SWenchao Xia         info = g_new0(SnapshotInfo, 1);
222fb0ed453SWenchao Xia         info->id            = g_strdup(sn_tab[i].id_str);
223fb0ed453SWenchao Xia         info->name          = g_strdup(sn_tab[i].name);
224fb0ed453SWenchao Xia         info->vm_state_size = sn_tab[i].vm_state_size;
225fb0ed453SWenchao Xia         info->date_sec      = sn_tab[i].date_sec;
226fb0ed453SWenchao Xia         info->date_nsec     = sn_tab[i].date_nsec;
227fb0ed453SWenchao Xia         info->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
228fb0ed453SWenchao Xia         info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
229f364ec65SWenchao Xia 
230fb0ed453SWenchao Xia         info_list = g_new0(SnapshotInfoList, 1);
231fb0ed453SWenchao Xia         info_list->value = info;
232f364ec65SWenchao Xia 
233f364ec65SWenchao Xia         /* XXX: waiting for the qapi to support qemu-queue.h types */
234f364ec65SWenchao Xia         if (!cur_item) {
235fb0ed453SWenchao Xia             head = cur_item = info_list;
236f364ec65SWenchao Xia         } else {
237f364ec65SWenchao Xia             cur_item->next = info_list;
238f364ec65SWenchao Xia             cur_item = info_list;
239f364ec65SWenchao Xia         }
240f364ec65SWenchao Xia 
241f364ec65SWenchao Xia     }
242f364ec65SWenchao Xia 
243f364ec65SWenchao Xia     g_free(sn_tab);
244fb0ed453SWenchao Xia     *p_list = head;
245fb0ed453SWenchao Xia     return 0;
246f364ec65SWenchao Xia }
247f364ec65SWenchao Xia 
24843526ec8SWenchao Xia /**
24943526ec8SWenchao Xia  * bdrv_query_image_info:
25043526ec8SWenchao Xia  * @bs: block device to examine
25143526ec8SWenchao Xia  * @p_info: location to store image information
25243526ec8SWenchao Xia  * @errp: location to store error information
25343526ec8SWenchao Xia  *
254553a7e87SWenchao Xia  * Store "flat" image information in @p_info.
255553a7e87SWenchao Xia  *
256553a7e87SWenchao Xia  * "Flat" means it does *not* query backing image information,
257553a7e87SWenchao Xia  * i.e. (*pinfo)->has_backing_image will be set to false and
258553a7e87SWenchao Xia  * (*pinfo)->backing_image to NULL even when the image does in fact have
259553a7e87SWenchao Xia  * a backing image.
260553a7e87SWenchao Xia  *
26143526ec8SWenchao Xia  * @p_info will be set only on success. On error, store error in @errp.
26243526ec8SWenchao Xia  */
26343526ec8SWenchao Xia void bdrv_query_image_info(BlockDriverState *bs,
26443526ec8SWenchao Xia                            ImageInfo **p_info,
26543526ec8SWenchao Xia                            Error **errp)
266f364ec65SWenchao Xia {
26752bf1e72SMarkus Armbruster     int64_t size;
26843526ec8SWenchao Xia     const char *backing_filename;
269f364ec65SWenchao Xia     BlockDriverInfo bdi;
27043526ec8SWenchao Xia     int ret;
27143526ec8SWenchao Xia     Error *err = NULL;
27252bf1e72SMarkus Armbruster     ImageInfo *info;
273f364ec65SWenchao Xia 
2741963f8d5SPaolo Bonzini     aio_context_acquire(bdrv_get_aio_context(bs));
2751963f8d5SPaolo Bonzini 
27652bf1e72SMarkus Armbruster     size = bdrv_getlength(bs);
27752bf1e72SMarkus Armbruster     if (size < 0) {
2789adceb02SFam Zheng         error_setg_errno(errp, -size, "Can't get image size '%s'",
2799adceb02SFam Zheng                          bs->exact_filename);
2801963f8d5SPaolo Bonzini         goto out;
28152bf1e72SMarkus Armbruster     }
282f364ec65SWenchao Xia 
283f30c66baSMax Reitz     bdrv_refresh_filename(bs);
284f30c66baSMax Reitz 
28552bf1e72SMarkus Armbruster     info = g_new0(ImageInfo, 1);
28643526ec8SWenchao Xia     info->filename        = g_strdup(bs->filename);
287f364ec65SWenchao Xia     info->format          = g_strdup(bdrv_get_format_name(bs));
28852bf1e72SMarkus Armbruster     info->virtual_size    = size;
289f364ec65SWenchao Xia     info->actual_size     = bdrv_get_allocated_file_size(bs);
290f364ec65SWenchao Xia     info->has_actual_size = info->actual_size >= 0;
2918b8277cdSMax Reitz     if (bs->encrypted) {
292f364ec65SWenchao Xia         info->encrypted = true;
293f364ec65SWenchao Xia         info->has_encrypted = true;
294f364ec65SWenchao Xia     }
295f364ec65SWenchao Xia     if (bdrv_get_info(bs, &bdi) >= 0) {
296f364ec65SWenchao Xia         if (bdi.cluster_size != 0) {
297f364ec65SWenchao Xia             info->cluster_size = bdi.cluster_size;
298f364ec65SWenchao Xia             info->has_cluster_size = true;
299f364ec65SWenchao Xia         }
300f364ec65SWenchao Xia         info->dirty_flag = bdi.is_dirty;
301f364ec65SWenchao Xia         info->has_dirty_flag = true;
302f364ec65SWenchao Xia     }
3031bf6e9caSAndrey Shinkevich     info->format_specific = bdrv_get_specific_info(bs, &err);
3041bf6e9caSAndrey Shinkevich     if (err) {
3051bf6e9caSAndrey Shinkevich         error_propagate(errp, err);
3061bf6e9caSAndrey Shinkevich         qapi_free_ImageInfo(info);
3071bf6e9caSAndrey Shinkevich         goto out;
3081bf6e9caSAndrey Shinkevich     }
309eae041feSMax Reitz     info->has_format_specific = info->format_specific != NULL;
310eae041feSMax Reitz 
31143526ec8SWenchao Xia     backing_filename = bs->backing_file;
312f364ec65SWenchao Xia     if (backing_filename[0] != '\0') {
3136b6833c1SMax Reitz         char *backing_filename2;
314f364ec65SWenchao Xia         info->backing_filename = g_strdup(backing_filename);
315f364ec65SWenchao Xia         info->has_backing_filename = true;
3166b6833c1SMax Reitz         backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
317f364ec65SWenchao Xia 
31812dcb1c0SJohn Snow         /* Always report the full_backing_filename if present, even if it's the
31912dcb1c0SJohn Snow          * same as backing_filename. That they are same is useful info. */
32012dcb1c0SJohn Snow         if (backing_filename2) {
32112dcb1c0SJohn Snow             info->full_backing_filename = g_strdup(backing_filename2);
322f364ec65SWenchao Xia             info->has_full_backing_filename = true;
323f364ec65SWenchao Xia         }
324f364ec65SWenchao Xia 
325f364ec65SWenchao Xia         if (bs->backing_format[0]) {
326f364ec65SWenchao Xia             info->backing_filename_format = g_strdup(bs->backing_format);
327f364ec65SWenchao Xia             info->has_backing_filename_format = true;
328f364ec65SWenchao Xia         }
329564d64bdSJeff Cody         g_free(backing_filename2);
330f364ec65SWenchao Xia     }
33143526ec8SWenchao Xia 
33243526ec8SWenchao Xia     ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
33343526ec8SWenchao Xia     switch (ret) {
33443526ec8SWenchao Xia     case 0:
33543526ec8SWenchao Xia         if (info->snapshots) {
33643526ec8SWenchao Xia             info->has_snapshots = true;
33743526ec8SWenchao Xia         }
33843526ec8SWenchao Xia         break;
33943526ec8SWenchao Xia     /* recoverable error */
34043526ec8SWenchao Xia     case -ENOMEDIUM:
34143526ec8SWenchao Xia     case -ENOTSUP:
34243526ec8SWenchao Xia         error_free(err);
34343526ec8SWenchao Xia         break;
34443526ec8SWenchao Xia     default:
34543526ec8SWenchao Xia         error_propagate(errp, err);
34643526ec8SWenchao Xia         qapi_free_ImageInfo(info);
3471963f8d5SPaolo Bonzini         goto out;
34843526ec8SWenchao Xia     }
34943526ec8SWenchao Xia 
35043526ec8SWenchao Xia     *p_info = info;
3511963f8d5SPaolo Bonzini 
3521963f8d5SPaolo Bonzini out:
3531963f8d5SPaolo Bonzini     aio_context_release(bdrv_get_aio_context(bs));
354f364ec65SWenchao Xia }
355f364ec65SWenchao Xia 
356553a7e87SWenchao Xia /* @p_info will be set only on success. */
357d829a211SMarkus Armbruster static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
358553a7e87SWenchao Xia                             Error **errp)
359f364ec65SWenchao Xia {
360f364ec65SWenchao Xia     BlockInfo *info = g_malloc0(sizeof(*info));
361d829a211SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
36246eade7bSKevin Wolf     char *qdev;
36346eade7bSKevin Wolf 
364d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of */
365d3c8c674SKevin Wolf     while (bs && bs->drv && bs->implicit) {
366d3c8c674SKevin Wolf         bs = backing_bs(bs);
367d3c8c674SKevin Wolf     }
368d3c8c674SKevin Wolf 
369d829a211SMarkus Armbruster     info->device = g_strdup(blk_name(blk));
370f364ec65SWenchao Xia     info->type = g_strdup("unknown");
371a7f53e26SMarkus Armbruster     info->locked = blk_dev_is_medium_locked(blk);
372a7f53e26SMarkus Armbruster     info->removable = blk_dev_has_removable_media(blk);
373f364ec65SWenchao Xia 
37446eade7bSKevin Wolf     qdev = blk_get_attached_dev_id(blk);
37546eade7bSKevin Wolf     if (qdev && *qdev) {
37646eade7bSKevin Wolf         info->has_qdev = true;
37746eade7bSKevin Wolf         info->qdev = qdev;
37846eade7bSKevin Wolf     } else {
37946eade7bSKevin Wolf         g_free(qdev);
38046eade7bSKevin Wolf     }
38146eade7bSKevin Wolf 
382327032ceSMax Reitz     if (blk_dev_has_tray(blk)) {
383f364ec65SWenchao Xia         info->has_tray_open = true;
384a7f53e26SMarkus Armbruster         info->tray_open = blk_dev_is_tray_open(blk);
385f364ec65SWenchao Xia     }
386f364ec65SWenchao Xia 
387373340b2SMax Reitz     if (blk_iostatus_is_enabled(blk)) {
388f364ec65SWenchao Xia         info->has_io_status = true;
389373340b2SMax Reitz         info->io_status = blk_iostatus(blk);
390f364ec65SWenchao Xia     }
391f364ec65SWenchao Xia 
3925433c24fSMax Reitz     if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
39321b56835SFam Zheng         info->has_dirty_bitmaps = true;
39421b56835SFam Zheng         info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
39521b56835SFam Zheng     }
39621b56835SFam Zheng 
3975433c24fSMax Reitz     if (bs && bs->drv) {
398f364ec65SWenchao Xia         info->has_inserted = true;
399facda544SPeter Krempa         info->inserted = bdrv_block_device_info(blk, bs, false, errp);
400d5a8ee60SAlberto Garcia         if (info->inserted == NULL) {
401553a7e87SWenchao Xia             goto err;
402f364ec65SWenchao Xia         }
403553a7e87SWenchao Xia     }
404553a7e87SWenchao Xia 
405553a7e87SWenchao Xia     *p_info = info;
406553a7e87SWenchao Xia     return;
407553a7e87SWenchao Xia 
408553a7e87SWenchao Xia  err:
409553a7e87SWenchao Xia     qapi_free_BlockInfo(info);
410f364ec65SWenchao Xia }
411f364ec65SWenchao Xia 
4127e5c776dSVladimir Sementsov-Ogievskiy static uint64List *uint64_list(uint64_t *list, int size)
4137e5c776dSVladimir Sementsov-Ogievskiy {
4147e5c776dSVladimir Sementsov-Ogievskiy     int i;
4157e5c776dSVladimir Sementsov-Ogievskiy     uint64List *out_list = NULL;
4167e5c776dSVladimir Sementsov-Ogievskiy     uint64List **pout_list = &out_list;
4177e5c776dSVladimir Sementsov-Ogievskiy 
4187e5c776dSVladimir Sementsov-Ogievskiy     for (i = 0; i < size; i++) {
4197e5c776dSVladimir Sementsov-Ogievskiy         uint64List *entry = g_new(uint64List, 1);
4207e5c776dSVladimir Sementsov-Ogievskiy         entry->value = list[i];
4217e5c776dSVladimir Sementsov-Ogievskiy         *pout_list = entry;
4227e5c776dSVladimir Sementsov-Ogievskiy         pout_list = &entry->next;
4237e5c776dSVladimir Sementsov-Ogievskiy     }
4247e5c776dSVladimir Sementsov-Ogievskiy 
4257e5c776dSVladimir Sementsov-Ogievskiy     *pout_list = NULL;
4267e5c776dSVladimir Sementsov-Ogievskiy 
4277e5c776dSVladimir Sementsov-Ogievskiy     return out_list;
4287e5c776dSVladimir Sementsov-Ogievskiy }
4297e5c776dSVladimir Sementsov-Ogievskiy 
4307e5c776dSVladimir Sementsov-Ogievskiy static void bdrv_latency_histogram_stats(BlockLatencyHistogram *hist,
4317e5c776dSVladimir Sementsov-Ogievskiy                                          bool *not_null,
4327e5c776dSVladimir Sementsov-Ogievskiy                                          BlockLatencyHistogramInfo **info)
4337e5c776dSVladimir Sementsov-Ogievskiy {
4347e5c776dSVladimir Sementsov-Ogievskiy     *not_null = hist->bins != NULL;
4357e5c776dSVladimir Sementsov-Ogievskiy     if (*not_null) {
4367e5c776dSVladimir Sementsov-Ogievskiy         *info = g_new0(BlockLatencyHistogramInfo, 1);
4377e5c776dSVladimir Sementsov-Ogievskiy 
4387e5c776dSVladimir Sementsov-Ogievskiy         (*info)->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
4397e5c776dSVladimir Sementsov-Ogievskiy         (*info)->bins = uint64_list(hist->bins, hist->nbins);
4407e5c776dSVladimir Sementsov-Ogievskiy     }
4417e5c776dSVladimir Sementsov-Ogievskiy }
4427e5c776dSVladimir Sementsov-Ogievskiy 
44354302156SMax Reitz static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
444f364ec65SWenchao Xia {
4452b77e60aSKevin Wolf     BlockAcctStats *stats = blk_get_stats(blk);
446979e9b03SAlberto Garcia     BlockAcctTimedStats *ts = NULL;
4477f0e9da6SMax Reitz 
44854302156SMax Reitz     ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
44954302156SMax Reitz     ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
450159f85ddSAnton Nefedov     ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
45154302156SMax Reitz     ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
45254302156SMax Reitz     ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
453159f85ddSAnton Nefedov     ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
4547ee12dafSAlberto Garcia 
45554302156SMax Reitz     ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
45654302156SMax Reitz     ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
45754302156SMax Reitz     ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
458159f85ddSAnton Nefedov     ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
4597ee12dafSAlberto Garcia 
46054302156SMax Reitz     ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
46154302156SMax Reitz     ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
46254302156SMax Reitz     ds->invalid_flush_operations =
4637ee12dafSAlberto Garcia         stats->invalid_ops[BLOCK_ACCT_FLUSH];
464159f85ddSAnton Nefedov     ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
4657ee12dafSAlberto Garcia 
46654302156SMax Reitz     ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
46754302156SMax Reitz     ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
468159f85ddSAnton Nefedov     ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
46954302156SMax Reitz     ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
47054302156SMax Reitz     ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
47154302156SMax Reitz     ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
47254302156SMax Reitz     ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
473159f85ddSAnton Nefedov     ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
474cb38fffbSAlberto Garcia 
47554302156SMax Reitz     ds->has_idle_time_ns = stats->last_access_time_ns > 0;
47654302156SMax Reitz     if (ds->has_idle_time_ns) {
47754302156SMax Reitz         ds->idle_time_ns = block_acct_idle_time_ns(stats);
478cb38fffbSAlberto Garcia     }
479362e9299SAlberto Garcia 
48054302156SMax Reitz     ds->account_invalid = stats->account_invalid;
48154302156SMax Reitz     ds->account_failed = stats->account_failed;
482979e9b03SAlberto Garcia 
483979e9b03SAlberto Garcia     while ((ts = block_acct_interval_next(stats, ts))) {
484979e9b03SAlberto Garcia         BlockDeviceTimedStatsList *timed_stats =
485979e9b03SAlberto Garcia             g_malloc0(sizeof(*timed_stats));
486979e9b03SAlberto Garcia         BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
48754302156SMax Reitz         timed_stats->next = ds->timed_stats;
488979e9b03SAlberto Garcia         timed_stats->value = dev_stats;
48954302156SMax Reitz         ds->timed_stats = timed_stats;
490979e9b03SAlberto Garcia 
491979e9b03SAlberto Garcia         TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
492979e9b03SAlberto Garcia         TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
493979e9b03SAlberto Garcia         TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
494979e9b03SAlberto Garcia 
495979e9b03SAlberto Garcia         dev_stats->interval_length = ts->interval_length;
496979e9b03SAlberto Garcia 
497979e9b03SAlberto Garcia         dev_stats->min_rd_latency_ns = timed_average_min(rd);
498979e9b03SAlberto Garcia         dev_stats->max_rd_latency_ns = timed_average_max(rd);
499979e9b03SAlberto Garcia         dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
500979e9b03SAlberto Garcia 
501979e9b03SAlberto Garcia         dev_stats->min_wr_latency_ns = timed_average_min(wr);
502979e9b03SAlberto Garcia         dev_stats->max_wr_latency_ns = timed_average_max(wr);
503979e9b03SAlberto Garcia         dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
504979e9b03SAlberto Garcia 
505979e9b03SAlberto Garcia         dev_stats->min_flush_latency_ns = timed_average_min(fl);
506979e9b03SAlberto Garcia         dev_stats->max_flush_latency_ns = timed_average_max(fl);
507979e9b03SAlberto Garcia         dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
50896e4dedaSAlberto Garcia 
50996e4dedaSAlberto Garcia         dev_stats->avg_rd_queue_depth =
51096e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_READ);
51196e4dedaSAlberto Garcia         dev_stats->avg_wr_queue_depth =
51296e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
513979e9b03SAlberto Garcia     }
5147e5c776dSVladimir Sementsov-Ogievskiy 
5157e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_READ],
516cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->has_rd_latency_histogram,
517cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->rd_latency_histogram);
5187e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_WRITE],
519cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->has_wr_latency_histogram,
520cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->wr_latency_histogram);
5217e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_FLUSH],
522cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->has_flush_latency_histogram,
523cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->flush_latency_histogram);
5247f0e9da6SMax Reitz }
525f364ec65SWenchao Xia 
526d3c8c674SKevin Wolf static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
527d3c8c674SKevin Wolf                                         bool blk_level)
5282b77e60aSKevin Wolf {
529*3f26191cSMax Reitz     BdrvChild *parent_child;
53020a6d768SDou Liyang     BlockStats *s = NULL;
53120a6d768SDou Liyang 
53220a6d768SDou Liyang     s = g_malloc0(sizeof(*s));
53320a6d768SDou Liyang     s->stats = g_malloc0(sizeof(*s->stats));
53420a6d768SDou Liyang 
53520a6d768SDou Liyang     if (!bs) {
53620a6d768SDou Liyang         return s;
53720a6d768SDou Liyang     }
53820a6d768SDou Liyang 
539d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of in
540d3c8c674SKevin Wolf      * a BlockBackend-level command. Stay at the exact node for a node-level
541d3c8c674SKevin Wolf      * command. */
542d3c8c674SKevin Wolf     while (blk_level && bs->drv && bs->implicit) {
543d3c8c674SKevin Wolf         bs = backing_bs(bs);
544d3c8c674SKevin Wolf         assert(bs);
545d3c8c674SKevin Wolf     }
546d3c8c674SKevin Wolf 
5472b77e60aSKevin Wolf     if (bdrv_get_node_name(bs)[0]) {
5482b77e60aSKevin Wolf         s->has_node_name = true;
5492b77e60aSKevin Wolf         s->node_name = g_strdup(bdrv_get_node_name(bs));
5502b77e60aSKevin Wolf     }
5512b77e60aSKevin Wolf 
552f7946da2SPaolo Bonzini     s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
55353d8f9d8SMax Reitz 
554d9245599SAnton Nefedov     s->driver_specific = bdrv_get_specific_stats(bs);
555d9245599SAnton Nefedov     if (s->driver_specific) {
556d9245599SAnton Nefedov         s->has_driver_specific = true;
557d9245599SAnton Nefedov     }
558d9245599SAnton Nefedov 
559*3f26191cSMax Reitz     parent_child = bdrv_primary_child(bs);
560*3f26191cSMax Reitz     if (!parent_child ||
561*3f26191cSMax Reitz         !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED)))
562*3f26191cSMax Reitz     {
563*3f26191cSMax Reitz         BdrvChild *c;
564*3f26191cSMax Reitz 
565*3f26191cSMax Reitz         /*
566*3f26191cSMax Reitz          * Look for a unique data-storing child.  We do not need to look for
567*3f26191cSMax Reitz          * filtered children, as there would be only one and it would have been
568*3f26191cSMax Reitz          * the primary child.
569*3f26191cSMax Reitz          */
570*3f26191cSMax Reitz         parent_child = NULL;
571*3f26191cSMax Reitz         QLIST_FOREACH(c, &bs->children, next) {
572*3f26191cSMax Reitz             if (c->role & BDRV_CHILD_DATA) {
573*3f26191cSMax Reitz                 if (parent_child) {
574*3f26191cSMax Reitz                     /*
575*3f26191cSMax Reitz                      * There are multiple data-storing children and we cannot
576*3f26191cSMax Reitz                      * choose between them.
577*3f26191cSMax Reitz                      */
578*3f26191cSMax Reitz                     parent_child = NULL;
579*3f26191cSMax Reitz                     break;
580*3f26191cSMax Reitz                 }
581*3f26191cSMax Reitz                 parent_child = c;
582*3f26191cSMax Reitz             }
583*3f26191cSMax Reitz         }
584*3f26191cSMax Reitz     }
585*3f26191cSMax Reitz     if (parent_child) {
586f364ec65SWenchao Xia         s->has_parent = true;
587*3f26191cSMax Reitz         s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level);
588f364ec65SWenchao Xia     }
589f364ec65SWenchao Xia 
590d3c8c674SKevin Wolf     if (blk_level && bs->backing) {
591c8059b97SFam Zheng         s->has_backing = true;
592d3c8c674SKevin Wolf         s->backing = bdrv_query_bds_stats(bs->backing->bs, blk_level);
593c8059b97SFam Zheng     }
594c8059b97SFam Zheng 
59520a6d768SDou Liyang     return s;
596b07363a1SKevin Wolf }
597b07363a1SKevin Wolf 
598f364ec65SWenchao Xia BlockInfoList *qmp_query_block(Error **errp)
599f364ec65SWenchao Xia {
600f364ec65SWenchao Xia     BlockInfoList *head = NULL, **p_next = &head;
601d829a211SMarkus Armbruster     BlockBackend *blk;
602553a7e87SWenchao Xia     Error *local_err = NULL;
603f364ec65SWenchao Xia 
604d5b68844SKevin Wolf     for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
605d5b68844SKevin Wolf         BlockInfoList *info;
606d5b68844SKevin Wolf 
607ec18b0a9SKevin Wolf         if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
608d5b68844SKevin Wolf             continue;
609d5b68844SKevin Wolf         }
610d5b68844SKevin Wolf 
611d5b68844SKevin Wolf         info = g_malloc0(sizeof(*info));
612d829a211SMarkus Armbruster         bdrv_query_info(blk, &info->value, &local_err);
61384d18f06SMarkus Armbruster         if (local_err) {
614553a7e87SWenchao Xia             error_propagate(errp, local_err);
615903c341dSMarkus Armbruster             g_free(info);
616903c341dSMarkus Armbruster             qapi_free_BlockInfoList(head);
617903c341dSMarkus Armbruster             return NULL;
618553a7e87SWenchao Xia         }
619f364ec65SWenchao Xia 
620f364ec65SWenchao Xia         *p_next = info;
621f364ec65SWenchao Xia         p_next = &info->next;
622f364ec65SWenchao Xia     }
623f364ec65SWenchao Xia 
624f364ec65SWenchao Xia     return head;
625f364ec65SWenchao Xia }
626f364ec65SWenchao Xia 
627f71eaa74SFam Zheng BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
628f71eaa74SFam Zheng                                      bool query_nodes,
629f71eaa74SFam Zheng                                      Error **errp)
630f364ec65SWenchao Xia {
631f364ec65SWenchao Xia     BlockStatsList *head = NULL, **p_next = &head;
632a6baa608SDou Liyang     BlockBackend *blk;
633a6baa608SDou Liyang     BlockDriverState *bs;
634f364ec65SWenchao Xia 
635f71eaa74SFam Zheng     /* Just to be safe if query_nodes is not always initialized */
636a6baa608SDou Liyang     if (has_query_nodes && query_nodes) {
637a6baa608SDou Liyang         for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
638f364ec65SWenchao Xia             BlockStatsList *info = g_malloc0(sizeof(*info));
639a6baa608SDou Liyang             AioContext *ctx = bdrv_get_aio_context(bs);
64013344f3aSStefan Hajnoczi 
64113344f3aSStefan Hajnoczi             aio_context_acquire(ctx);
642a6baa608SDou Liyang             info->value = bdrv_query_bds_stats(bs, false);
64313344f3aSStefan Hajnoczi             aio_context_release(ctx);
644f364ec65SWenchao Xia 
645f364ec65SWenchao Xia             *p_next = info;
646f364ec65SWenchao Xia             p_next = &info->next;
647f364ec65SWenchao Xia         }
648a6baa608SDou Liyang     } else {
649567dcb31SKevin Wolf         for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
650f62492bbSKevin Wolf             BlockStatsList *info;
651a6baa608SDou Liyang             AioContext *ctx = blk_get_aio_context(blk);
652a6baa608SDou Liyang             BlockStats *s;
6535a9cb5a9SKevin Wolf             char *qdev;
654a6baa608SDou Liyang 
655567dcb31SKevin Wolf             if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
656567dcb31SKevin Wolf                 continue;
657567dcb31SKevin Wolf             }
658567dcb31SKevin Wolf 
659a6baa608SDou Liyang             aio_context_acquire(ctx);
660a6baa608SDou Liyang             s = bdrv_query_bds_stats(blk_bs(blk), true);
661a6baa608SDou Liyang             s->has_device = true;
662a6baa608SDou Liyang             s->device = g_strdup(blk_name(blk));
6635a9cb5a9SKevin Wolf 
6645a9cb5a9SKevin Wolf             qdev = blk_get_attached_dev_id(blk);
6655a9cb5a9SKevin Wolf             if (qdev && *qdev) {
6665a9cb5a9SKevin Wolf                 s->has_qdev = true;
6675a9cb5a9SKevin Wolf                 s->qdev = qdev;
6685a9cb5a9SKevin Wolf             } else {
6695a9cb5a9SKevin Wolf                 g_free(qdev);
6705a9cb5a9SKevin Wolf             }
6715a9cb5a9SKevin Wolf 
672a6baa608SDou Liyang             bdrv_query_blk_stats(s->stats, blk);
673a6baa608SDou Liyang             aio_context_release(ctx);
674a6baa608SDou Liyang 
675f62492bbSKevin Wolf             info = g_malloc0(sizeof(*info));
676a6baa608SDou Liyang             info->value = s;
677a6baa608SDou Liyang             *p_next = info;
678a6baa608SDou Liyang             p_next = &info->next;
679a6baa608SDou Liyang         }
680a6baa608SDou Liyang     }
681f364ec65SWenchao Xia 
682f364ec65SWenchao Xia     return head;
683f364ec65SWenchao Xia }
684f364ec65SWenchao Xia 
685e1ce7d74SMarkus Armbruster void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
686f364ec65SWenchao Xia {
687de38b500SEric Blake     char date_buf[128], clock_buf[128];
688f364ec65SWenchao Xia     struct tm tm;
689f364ec65SWenchao Xia     time_t ti;
690f364ec65SWenchao Xia     int64_t secs;
691de38b500SEric Blake     char *sizing = NULL;
692f364ec65SWenchao Xia 
693f364ec65SWenchao Xia     if (!sn) {
694804359b8SMax Reitz         qemu_printf("%-10s%-20s%11s%20s%15s",
695f364ec65SWenchao Xia                     "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
696f364ec65SWenchao Xia     } else {
697f364ec65SWenchao Xia         ti = sn->date_sec;
698f364ec65SWenchao Xia         localtime_r(&ti, &tm);
699f364ec65SWenchao Xia         strftime(date_buf, sizeof(date_buf),
700f364ec65SWenchao Xia                  "%Y-%m-%d %H:%M:%S", &tm);
701f364ec65SWenchao Xia         secs = sn->vm_clock_nsec / 1000000000;
702f364ec65SWenchao Xia         snprintf(clock_buf, sizeof(clock_buf),
703f364ec65SWenchao Xia                  "%02d:%02d:%02d.%03d",
704f364ec65SWenchao Xia                  (int)(secs / 3600),
705f364ec65SWenchao Xia                  (int)((secs / 60) % 60),
706f364ec65SWenchao Xia                  (int)(secs % 60),
707f364ec65SWenchao Xia                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
708de38b500SEric Blake         sizing = size_to_str(sn->vm_state_size);
709804359b8SMax Reitz         qemu_printf("%-10s%-20s%11s%20s%15s",
710f364ec65SWenchao Xia                     sn->id_str, sn->name,
711de38b500SEric Blake                     sizing,
712f364ec65SWenchao Xia                     date_buf,
713f364ec65SWenchao Xia                     clock_buf);
714f364ec65SWenchao Xia     }
715de38b500SEric Blake     g_free(sizing);
716f364ec65SWenchao Xia }
717f364ec65SWenchao Xia 
718e1ce7d74SMarkus Armbruster static void dump_qdict(int indentation, QDict *dict);
719e1ce7d74SMarkus Armbruster static void dump_qlist(int indentation, QList *list);
720a8d8ecb7SMax Reitz 
721e1ce7d74SMarkus Armbruster static void dump_qobject(int comp_indent, QObject *obj)
722a8d8ecb7SMax Reitz {
723a8d8ecb7SMax Reitz     switch (qobject_type(obj)) {
72401b2ffceSMarc-André Lureau         case QTYPE_QNUM: {
7257dc847ebSMax Reitz             QNum *value = qobject_to(QNum, obj);
72601b2ffceSMarc-André Lureau             char *tmp = qnum_to_string(value);
727e1ce7d74SMarkus Armbruster             qemu_printf("%s", tmp);
72801b2ffceSMarc-André Lureau             g_free(tmp);
729a8d8ecb7SMax Reitz             break;
730a8d8ecb7SMax Reitz         }
731a8d8ecb7SMax Reitz         case QTYPE_QSTRING: {
7327dc847ebSMax Reitz             QString *value = qobject_to(QString, obj);
733e1ce7d74SMarkus Armbruster             qemu_printf("%s", qstring_get_str(value));
734a8d8ecb7SMax Reitz             break;
735a8d8ecb7SMax Reitz         }
736a8d8ecb7SMax Reitz         case QTYPE_QDICT: {
7377dc847ebSMax Reitz             QDict *value = qobject_to(QDict, obj);
738e1ce7d74SMarkus Armbruster             dump_qdict(comp_indent, value);
739a8d8ecb7SMax Reitz             break;
740a8d8ecb7SMax Reitz         }
741a8d8ecb7SMax Reitz         case QTYPE_QLIST: {
7427dc847ebSMax Reitz             QList *value = qobject_to(QList, obj);
743e1ce7d74SMarkus Armbruster             dump_qlist(comp_indent, value);
744a8d8ecb7SMax Reitz             break;
745a8d8ecb7SMax Reitz         }
746a8d8ecb7SMax Reitz         case QTYPE_QBOOL: {
7477dc847ebSMax Reitz             QBool *value = qobject_to(QBool, obj);
748e1ce7d74SMarkus Armbruster             qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
749a8d8ecb7SMax Reitz             break;
750a8d8ecb7SMax Reitz         }
751a8d8ecb7SMax Reitz         default:
752a8d8ecb7SMax Reitz             abort();
753a8d8ecb7SMax Reitz     }
754a8d8ecb7SMax Reitz }
755a8d8ecb7SMax Reitz 
756e1ce7d74SMarkus Armbruster static void dump_qlist(int indentation, QList *list)
757a8d8ecb7SMax Reitz {
758a8d8ecb7SMax Reitz     const QListEntry *entry;
759a8d8ecb7SMax Reitz     int i = 0;
760a8d8ecb7SMax Reitz 
761a8d8ecb7SMax Reitz     for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
7621310a3d3SEric Blake         QType type = qobject_type(entry->value);
763a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
764e1ce7d74SMarkus Armbruster         qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
765853ccfedSPeter Xu                     composite ? '\n' : ' ');
766e1ce7d74SMarkus Armbruster         dump_qobject(indentation + 1, entry->value);
767a8d8ecb7SMax Reitz         if (!composite) {
768e1ce7d74SMarkus Armbruster             qemu_printf("\n");
769a8d8ecb7SMax Reitz         }
770a8d8ecb7SMax Reitz     }
771a8d8ecb7SMax Reitz }
772a8d8ecb7SMax Reitz 
773e1ce7d74SMarkus Armbruster static void dump_qdict(int indentation, QDict *dict)
774a8d8ecb7SMax Reitz {
775a8d8ecb7SMax Reitz     const QDictEntry *entry;
776a8d8ecb7SMax Reitz 
777a8d8ecb7SMax Reitz     for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
7781310a3d3SEric Blake         QType type = qobject_type(entry->value);
779a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
7805eda6227SPeter Xu         char *key = g_malloc(strlen(entry->key) + 1);
781a8d8ecb7SMax Reitz         int i;
782a8d8ecb7SMax Reitz 
783a8d8ecb7SMax Reitz         /* replace dashes with spaces in key (variable) names */
784a8d8ecb7SMax Reitz         for (i = 0; entry->key[i]; i++) {
785a8d8ecb7SMax Reitz             key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
786a8d8ecb7SMax Reitz         }
787a8d8ecb7SMax Reitz         key[i] = 0;
788e1ce7d74SMarkus Armbruster         qemu_printf("%*s%s:%c", indentation * 4, "", key,
789853ccfedSPeter Xu                     composite ? '\n' : ' ');
790e1ce7d74SMarkus Armbruster         dump_qobject(indentation + 1, entry->value);
791a8d8ecb7SMax Reitz         if (!composite) {
792e1ce7d74SMarkus Armbruster             qemu_printf("\n");
793a8d8ecb7SMax Reitz         }
7945eda6227SPeter Xu         g_free(key);
795a8d8ecb7SMax Reitz     }
796a8d8ecb7SMax Reitz }
797a8d8ecb7SMax Reitz 
798e1ce7d74SMarkus Armbruster void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec)
799a8d8ecb7SMax Reitz {
800a8d8ecb7SMax Reitz     QObject *obj, *data;
8017d5e199aSDaniel P. Berrange     Visitor *v = qobject_output_visitor_new(&obj);
802a8d8ecb7SMax Reitz 
8033b098d56SEric Blake     visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
8043b098d56SEric Blake     visit_complete(v, &obj);
8057dc847ebSMax Reitz     data = qdict_get(qobject_to(QDict, obj), "data");
806e1ce7d74SMarkus Armbruster     dump_qobject(1, data);
807cb3e7f08SMarc-André Lureau     qobject_unref(obj);
8083b098d56SEric Blake     visit_free(v);
809a8d8ecb7SMax Reitz }
810a8d8ecb7SMax Reitz 
811e1ce7d74SMarkus Armbruster void bdrv_image_info_dump(ImageInfo *info)
812f364ec65SWenchao Xia {
813de38b500SEric Blake     char *size_buf, *dsize_buf;
814f364ec65SWenchao Xia     if (!info->has_actual_size) {
815de38b500SEric Blake         dsize_buf = g_strdup("unavailable");
816f364ec65SWenchao Xia     } else {
817de38b500SEric Blake         dsize_buf = size_to_str(info->actual_size);
818f364ec65SWenchao Xia     }
819de38b500SEric Blake     size_buf = size_to_str(info->virtual_size);
820e1ce7d74SMarkus Armbruster     qemu_printf("image: %s\n"
821f364ec65SWenchao Xia                 "file format: %s\n"
822f364ec65SWenchao Xia                 "virtual size: %s (%" PRId64 " bytes)\n"
823f364ec65SWenchao Xia                 "disk size: %s\n",
824f364ec65SWenchao Xia                 info->filename, info->format, size_buf,
825f364ec65SWenchao Xia                 info->virtual_size,
826f364ec65SWenchao Xia                 dsize_buf);
827de38b500SEric Blake     g_free(size_buf);
828de38b500SEric Blake     g_free(dsize_buf);
829f364ec65SWenchao Xia 
830f364ec65SWenchao Xia     if (info->has_encrypted && info->encrypted) {
831e1ce7d74SMarkus Armbruster         qemu_printf("encrypted: yes\n");
832f364ec65SWenchao Xia     }
833f364ec65SWenchao Xia 
834f364ec65SWenchao Xia     if (info->has_cluster_size) {
835e1ce7d74SMarkus Armbruster         qemu_printf("cluster_size: %" PRId64 "\n",
8365b917044SWenchao Xia                     info->cluster_size);
837f364ec65SWenchao Xia     }
838f364ec65SWenchao Xia 
839f364ec65SWenchao Xia     if (info->has_dirty_flag && info->dirty_flag) {
840e1ce7d74SMarkus Armbruster         qemu_printf("cleanly shut down: no\n");
841f364ec65SWenchao Xia     }
842f364ec65SWenchao Xia 
843f364ec65SWenchao Xia     if (info->has_backing_filename) {
844e1ce7d74SMarkus Armbruster         qemu_printf("backing file: %s", info->backing_filename);
8455c9d9ca5SJohn Snow         if (!info->has_full_backing_filename) {
846e1ce7d74SMarkus Armbruster             qemu_printf(" (cannot determine actual path)");
8475c9d9ca5SJohn Snow         } else if (strcmp(info->backing_filename,
8485c9d9ca5SJohn Snow                           info->full_backing_filename) != 0) {
849e1ce7d74SMarkus Armbruster             qemu_printf(" (actual path: %s)", info->full_backing_filename);
850f364ec65SWenchao Xia         }
851e1ce7d74SMarkus Armbruster         qemu_printf("\n");
852f364ec65SWenchao Xia         if (info->has_backing_filename_format) {
853e1ce7d74SMarkus Armbruster             qemu_printf("backing file format: %s\n",
8545b917044SWenchao Xia                         info->backing_filename_format);
855f364ec65SWenchao Xia         }
856f364ec65SWenchao Xia     }
857f364ec65SWenchao Xia 
858f364ec65SWenchao Xia     if (info->has_snapshots) {
859f364ec65SWenchao Xia         SnapshotInfoList *elem;
860f364ec65SWenchao Xia 
861e1ce7d74SMarkus Armbruster         qemu_printf("Snapshot list:\n");
862e1ce7d74SMarkus Armbruster         bdrv_snapshot_dump(NULL);
863e1ce7d74SMarkus Armbruster         qemu_printf("\n");
864f364ec65SWenchao Xia 
865f364ec65SWenchao Xia         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
866f364ec65SWenchao Xia          * we convert to the block layer's native QEMUSnapshotInfo for now.
867f364ec65SWenchao Xia          */
868f364ec65SWenchao Xia         for (elem = info->snapshots; elem; elem = elem->next) {
869f364ec65SWenchao Xia             QEMUSnapshotInfo sn = {
870f364ec65SWenchao Xia                 .vm_state_size = elem->value->vm_state_size,
871f364ec65SWenchao Xia                 .date_sec = elem->value->date_sec,
872f364ec65SWenchao Xia                 .date_nsec = elem->value->date_nsec,
873f364ec65SWenchao Xia                 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
874f364ec65SWenchao Xia                                  elem->value->vm_clock_nsec,
875f364ec65SWenchao Xia             };
876f364ec65SWenchao Xia 
877f364ec65SWenchao Xia             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
878f364ec65SWenchao Xia             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
879e1ce7d74SMarkus Armbruster             bdrv_snapshot_dump(&sn);
880e1ce7d74SMarkus Armbruster             qemu_printf("\n");
881f364ec65SWenchao Xia         }
882f364ec65SWenchao Xia     }
883a8d8ecb7SMax Reitz 
884a8d8ecb7SMax Reitz     if (info->has_format_specific) {
885e1ce7d74SMarkus Armbruster         qemu_printf("Format specific information:\n");
886e1ce7d74SMarkus Armbruster         bdrv_image_info_specific_dump(info->format_specific);
887a8d8ecb7SMax Reitz     }
888f364ec65SWenchao Xia }
889