xref: /qemu/block/qapi.c (revision b39847a5)
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;
500b877d09SMax Reitz     BlockDriverState *bs0, *backing;
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 
790b877d09SMax Reitz     backing = bdrv_cow_bs(bs);
800b877d09SMax Reitz     if (backing) {
81c13163fbSBenoît Canet         info->has_backing_file = true;
820b877d09SMax Reitz         info->backing_file = g_strdup(backing->filename);
83c13163fbSBenoît Canet     }
84c13163fbSBenoît Canet 
85590a63d5SVladimir Sementsov-Ogievskiy     if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
86590a63d5SVladimir Sementsov-Ogievskiy         info->has_dirty_bitmaps = true;
87590a63d5SVladimir Sementsov-Ogievskiy         info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
88590a63d5SVladimir Sementsov-Ogievskiy     }
89590a63d5SVladimir Sementsov-Ogievskiy 
90465bee1dSPeter Lieven     info->detect_zeroes = bs->detect_zeroes;
91c13163fbSBenoît Canet 
92022cdc9fSManos Pitsidianakis     if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
93c13163fbSBenoît Canet         ThrottleConfig cfg;
94022cdc9fSManos Pitsidianakis         BlockBackendPublic *blkp = blk_get_public(blk);
9576f4afb4SAlberto Garcia 
96022cdc9fSManos Pitsidianakis         throttle_group_get_config(&blkp->throttle_group_member, &cfg);
9776f4afb4SAlberto Garcia 
98c13163fbSBenoît Canet         info->bps     = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
99c13163fbSBenoît Canet         info->bps_rd  = cfg.buckets[THROTTLE_BPS_READ].avg;
100c13163fbSBenoît Canet         info->bps_wr  = cfg.buckets[THROTTLE_BPS_WRITE].avg;
101c13163fbSBenoît Canet 
102c13163fbSBenoît Canet         info->iops    = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
103c13163fbSBenoît Canet         info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
104c13163fbSBenoît Canet         info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
105c13163fbSBenoît Canet 
106c13163fbSBenoît Canet         info->has_bps_max     = cfg.buckets[THROTTLE_BPS_TOTAL].max;
107c13163fbSBenoît Canet         info->bps_max         = cfg.buckets[THROTTLE_BPS_TOTAL].max;
108c13163fbSBenoît Canet         info->has_bps_rd_max  = cfg.buckets[THROTTLE_BPS_READ].max;
109c13163fbSBenoît Canet         info->bps_rd_max      = cfg.buckets[THROTTLE_BPS_READ].max;
110c13163fbSBenoît Canet         info->has_bps_wr_max  = cfg.buckets[THROTTLE_BPS_WRITE].max;
111c13163fbSBenoît Canet         info->bps_wr_max      = cfg.buckets[THROTTLE_BPS_WRITE].max;
112c13163fbSBenoît Canet 
113c13163fbSBenoît Canet         info->has_iops_max    = cfg.buckets[THROTTLE_OPS_TOTAL].max;
114c13163fbSBenoît Canet         info->iops_max        = cfg.buckets[THROTTLE_OPS_TOTAL].max;
115c13163fbSBenoît Canet         info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
116c13163fbSBenoît Canet         info->iops_rd_max     = cfg.buckets[THROTTLE_OPS_READ].max;
117c13163fbSBenoît Canet         info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
118c13163fbSBenoît Canet         info->iops_wr_max     = cfg.buckets[THROTTLE_OPS_WRITE].max;
119c13163fbSBenoît Canet 
120398befdfSAlberto Garcia         info->has_bps_max_length     = info->has_bps_max;
121398befdfSAlberto Garcia         info->bps_max_length         =
122398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
123398befdfSAlberto Garcia         info->has_bps_rd_max_length  = info->has_bps_rd_max;
124398befdfSAlberto Garcia         info->bps_rd_max_length      =
125398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_READ].burst_length;
126398befdfSAlberto Garcia         info->has_bps_wr_max_length  = info->has_bps_wr_max;
127398befdfSAlberto Garcia         info->bps_wr_max_length      =
128398befdfSAlberto Garcia             cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
129398befdfSAlberto Garcia 
130398befdfSAlberto Garcia         info->has_iops_max_length    = info->has_iops_max;
131398befdfSAlberto Garcia         info->iops_max_length        =
132398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
133398befdfSAlberto Garcia         info->has_iops_rd_max_length = info->has_iops_rd_max;
134398befdfSAlberto Garcia         info->iops_rd_max_length     =
135398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_READ].burst_length;
136398befdfSAlberto Garcia         info->has_iops_wr_max_length = info->has_iops_wr_max;
137398befdfSAlberto Garcia         info->iops_wr_max_length     =
138398befdfSAlberto Garcia             cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
139398befdfSAlberto Garcia 
140c13163fbSBenoît Canet         info->has_iops_size = cfg.op_size;
141c13163fbSBenoît Canet         info->iops_size = cfg.op_size;
142b8fe1694SAlberto Garcia 
143b8fe1694SAlberto Garcia         info->has_group = true;
144022cdc9fSManos Pitsidianakis         info->group =
145022cdc9fSManos Pitsidianakis             g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
146c13163fbSBenoît Canet     }
147c13163fbSBenoît Canet 
148e2462113SFrancesco Romani     info->write_threshold = bdrv_write_threshold_get(bs);
149e2462113SFrancesco Romani 
150d5a8ee60SAlberto Garcia     bs0 = bs;
151d5a8ee60SAlberto Garcia     p_image_info = &info->image;
152d3c8c674SKevin Wolf     info->backing_file_depth = 0;
153d5a8ee60SAlberto Garcia     while (1) {
154d5a8ee60SAlberto Garcia         Error *local_err = NULL;
155d5a8ee60SAlberto Garcia         bdrv_query_image_info(bs0, p_image_info, &local_err);
156d5a8ee60SAlberto Garcia         if (local_err) {
157d5a8ee60SAlberto Garcia             error_propagate(errp, local_err);
158d5a8ee60SAlberto Garcia             qapi_free_BlockDeviceInfo(info);
159d5a8ee60SAlberto Garcia             return NULL;
160d5a8ee60SAlberto Garcia         }
161d3c8c674SKevin Wolf 
162facda544SPeter Krempa         /* stop gathering data for flat output */
163facda544SPeter Krempa         if (flat) {
164facda544SPeter Krempa             break;
165facda544SPeter Krempa         }
166facda544SPeter Krempa 
1670a7585dbSMax Reitz         if (bs0->drv && bdrv_filter_or_cow_child(bs0)) {
1680a7585dbSMax Reitz             /*
1690a7585dbSMax Reitz              * Put any filtered child here (for backwards compatibility to when
1700a7585dbSMax Reitz              * we put bs0->backing here, which might be any filtered child).
1710a7585dbSMax Reitz              */
172d3c8c674SKevin Wolf             info->backing_file_depth++;
1730a7585dbSMax Reitz             bs0 = bdrv_filter_or_cow_bs(bs0);
174d5a8ee60SAlberto Garcia             (*p_image_info)->has_backing_image = true;
175d5a8ee60SAlberto Garcia             p_image_info = &((*p_image_info)->backing_image);
176d5a8ee60SAlberto Garcia         } else {
177d5a8ee60SAlberto Garcia             break;
178d5a8ee60SAlberto Garcia         }
179d3c8c674SKevin Wolf 
180d3c8c674SKevin Wolf         /* Skip automatically inserted nodes that the user isn't aware of for
181d3c8c674SKevin Wolf          * query-block (blk != NULL), but not for query-named-block-nodes */
1820a7585dbSMax Reitz         if (blk) {
1830a7585dbSMax Reitz             bs0 = bdrv_skip_implicit_filters(bs0);
184d3c8c674SKevin Wolf         }
185d5a8ee60SAlberto Garcia     }
186d5a8ee60SAlberto Garcia 
187c13163fbSBenoît Canet     return info;
188c13163fbSBenoît Canet }
189c13163fbSBenoît Canet 
190fb0ed453SWenchao Xia /*
191fb0ed453SWenchao Xia  * Returns 0 on success, with *p_list either set to describe snapshot
192fb0ed453SWenchao Xia  * information, or NULL because there are no snapshots.  Returns -errno on
193fb0ed453SWenchao Xia  * error, with *p_list untouched.
194fb0ed453SWenchao Xia  */
195fb0ed453SWenchao Xia int bdrv_query_snapshot_info_list(BlockDriverState *bs,
196fb0ed453SWenchao Xia                                   SnapshotInfoList **p_list,
197fb0ed453SWenchao Xia                                   Error **errp)
198f364ec65SWenchao Xia {
199f364ec65SWenchao Xia     int i, sn_count;
200f364ec65SWenchao Xia     QEMUSnapshotInfo *sn_tab = NULL;
201fb0ed453SWenchao Xia     SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
202fb0ed453SWenchao Xia     SnapshotInfo *info;
203fb0ed453SWenchao Xia 
204f364ec65SWenchao Xia     sn_count = bdrv_snapshot_list(bs, &sn_tab);
205fb0ed453SWenchao Xia     if (sn_count < 0) {
206fb0ed453SWenchao Xia         const char *dev = bdrv_get_device_name(bs);
207fb0ed453SWenchao Xia         switch (sn_count) {
208fb0ed453SWenchao Xia         case -ENOMEDIUM:
209fb0ed453SWenchao Xia             error_setg(errp, "Device '%s' is not inserted", dev);
210fb0ed453SWenchao Xia             break;
211fb0ed453SWenchao Xia         case -ENOTSUP:
212fb0ed453SWenchao Xia             error_setg(errp,
213fb0ed453SWenchao Xia                        "Device '%s' does not support internal snapshots",
214fb0ed453SWenchao Xia                        dev);
215fb0ed453SWenchao Xia             break;
216fb0ed453SWenchao Xia         default:
217fb0ed453SWenchao Xia             error_setg_errno(errp, -sn_count,
218fb0ed453SWenchao Xia                              "Can't list snapshots of device '%s'", dev);
219fb0ed453SWenchao Xia             break;
220fb0ed453SWenchao Xia         }
221fb0ed453SWenchao Xia         return sn_count;
222fb0ed453SWenchao Xia     }
223f364ec65SWenchao Xia 
224f364ec65SWenchao Xia     for (i = 0; i < sn_count; i++) {
225fb0ed453SWenchao Xia         info = g_new0(SnapshotInfo, 1);
226fb0ed453SWenchao Xia         info->id            = g_strdup(sn_tab[i].id_str);
227fb0ed453SWenchao Xia         info->name          = g_strdup(sn_tab[i].name);
228fb0ed453SWenchao Xia         info->vm_state_size = sn_tab[i].vm_state_size;
229fb0ed453SWenchao Xia         info->date_sec      = sn_tab[i].date_sec;
230fb0ed453SWenchao Xia         info->date_nsec     = sn_tab[i].date_nsec;
231fb0ed453SWenchao Xia         info->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
232fb0ed453SWenchao Xia         info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
233*b39847a5SPavel Dovgalyuk         info->icount        = sn_tab[i].icount;
234*b39847a5SPavel Dovgalyuk         info->has_icount    = sn_tab[i].icount != -1ULL;
235f364ec65SWenchao Xia 
236fb0ed453SWenchao Xia         info_list = g_new0(SnapshotInfoList, 1);
237fb0ed453SWenchao Xia         info_list->value = info;
238f364ec65SWenchao Xia 
239f364ec65SWenchao Xia         /* XXX: waiting for the qapi to support qemu-queue.h types */
240f364ec65SWenchao Xia         if (!cur_item) {
241fb0ed453SWenchao Xia             head = cur_item = info_list;
242f364ec65SWenchao Xia         } else {
243f364ec65SWenchao Xia             cur_item->next = info_list;
244f364ec65SWenchao Xia             cur_item = info_list;
245f364ec65SWenchao Xia         }
246f364ec65SWenchao Xia 
247f364ec65SWenchao Xia     }
248f364ec65SWenchao Xia 
249f364ec65SWenchao Xia     g_free(sn_tab);
250fb0ed453SWenchao Xia     *p_list = head;
251fb0ed453SWenchao Xia     return 0;
252f364ec65SWenchao Xia }
253f364ec65SWenchao Xia 
25443526ec8SWenchao Xia /**
25543526ec8SWenchao Xia  * bdrv_query_image_info:
25643526ec8SWenchao Xia  * @bs: block device to examine
25743526ec8SWenchao Xia  * @p_info: location to store image information
25843526ec8SWenchao Xia  * @errp: location to store error information
25943526ec8SWenchao Xia  *
260553a7e87SWenchao Xia  * Store "flat" image information in @p_info.
261553a7e87SWenchao Xia  *
262553a7e87SWenchao Xia  * "Flat" means it does *not* query backing image information,
263553a7e87SWenchao Xia  * i.e. (*pinfo)->has_backing_image will be set to false and
264553a7e87SWenchao Xia  * (*pinfo)->backing_image to NULL even when the image does in fact have
265553a7e87SWenchao Xia  * a backing image.
266553a7e87SWenchao Xia  *
26743526ec8SWenchao Xia  * @p_info will be set only on success. On error, store error in @errp.
26843526ec8SWenchao Xia  */
26943526ec8SWenchao Xia void bdrv_query_image_info(BlockDriverState *bs,
27043526ec8SWenchao Xia                            ImageInfo **p_info,
27143526ec8SWenchao Xia                            Error **errp)
272f364ec65SWenchao Xia {
27352bf1e72SMarkus Armbruster     int64_t size;
27443526ec8SWenchao Xia     const char *backing_filename;
275f364ec65SWenchao Xia     BlockDriverInfo bdi;
27643526ec8SWenchao Xia     int ret;
27743526ec8SWenchao Xia     Error *err = NULL;
27852bf1e72SMarkus Armbruster     ImageInfo *info;
279f364ec65SWenchao Xia 
2801963f8d5SPaolo Bonzini     aio_context_acquire(bdrv_get_aio_context(bs));
2811963f8d5SPaolo Bonzini 
28252bf1e72SMarkus Armbruster     size = bdrv_getlength(bs);
28352bf1e72SMarkus Armbruster     if (size < 0) {
2849adceb02SFam Zheng         error_setg_errno(errp, -size, "Can't get image size '%s'",
2859adceb02SFam Zheng                          bs->exact_filename);
2861963f8d5SPaolo Bonzini         goto out;
28752bf1e72SMarkus Armbruster     }
288f364ec65SWenchao Xia 
289f30c66baSMax Reitz     bdrv_refresh_filename(bs);
290f30c66baSMax Reitz 
29152bf1e72SMarkus Armbruster     info = g_new0(ImageInfo, 1);
29243526ec8SWenchao Xia     info->filename        = g_strdup(bs->filename);
293f364ec65SWenchao Xia     info->format          = g_strdup(bdrv_get_format_name(bs));
29452bf1e72SMarkus Armbruster     info->virtual_size    = size;
295f364ec65SWenchao Xia     info->actual_size     = bdrv_get_allocated_file_size(bs);
296f364ec65SWenchao Xia     info->has_actual_size = info->actual_size >= 0;
2978b8277cdSMax Reitz     if (bs->encrypted) {
298f364ec65SWenchao Xia         info->encrypted = true;
299f364ec65SWenchao Xia         info->has_encrypted = true;
300f364ec65SWenchao Xia     }
301f364ec65SWenchao Xia     if (bdrv_get_info(bs, &bdi) >= 0) {
302f364ec65SWenchao Xia         if (bdi.cluster_size != 0) {
303f364ec65SWenchao Xia             info->cluster_size = bdi.cluster_size;
304f364ec65SWenchao Xia             info->has_cluster_size = true;
305f364ec65SWenchao Xia         }
306f364ec65SWenchao Xia         info->dirty_flag = bdi.is_dirty;
307f364ec65SWenchao Xia         info->has_dirty_flag = true;
308f364ec65SWenchao Xia     }
3091bf6e9caSAndrey Shinkevich     info->format_specific = bdrv_get_specific_info(bs, &err);
3101bf6e9caSAndrey Shinkevich     if (err) {
3111bf6e9caSAndrey Shinkevich         error_propagate(errp, err);
3121bf6e9caSAndrey Shinkevich         qapi_free_ImageInfo(info);
3131bf6e9caSAndrey Shinkevich         goto out;
3141bf6e9caSAndrey Shinkevich     }
315eae041feSMax Reitz     info->has_format_specific = info->format_specific != NULL;
316eae041feSMax Reitz 
31743526ec8SWenchao Xia     backing_filename = bs->backing_file;
318f364ec65SWenchao Xia     if (backing_filename[0] != '\0') {
3196b6833c1SMax Reitz         char *backing_filename2;
3200b877d09SMax Reitz 
321f364ec65SWenchao Xia         info->backing_filename = g_strdup(backing_filename);
322f364ec65SWenchao Xia         info->has_backing_filename = true;
3236b6833c1SMax Reitz         backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
324f364ec65SWenchao Xia 
32512dcb1c0SJohn Snow         /* Always report the full_backing_filename if present, even if it's the
32612dcb1c0SJohn Snow          * same as backing_filename. That they are same is useful info. */
32712dcb1c0SJohn Snow         if (backing_filename2) {
32812dcb1c0SJohn Snow             info->full_backing_filename = g_strdup(backing_filename2);
329f364ec65SWenchao Xia             info->has_full_backing_filename = true;
330f364ec65SWenchao Xia         }
331f364ec65SWenchao Xia 
332f364ec65SWenchao Xia         if (bs->backing_format[0]) {
333f364ec65SWenchao Xia             info->backing_filename_format = g_strdup(bs->backing_format);
334f364ec65SWenchao Xia             info->has_backing_filename_format = true;
335f364ec65SWenchao Xia         }
336564d64bdSJeff Cody         g_free(backing_filename2);
337f364ec65SWenchao Xia     }
33843526ec8SWenchao Xia 
33943526ec8SWenchao Xia     ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
34043526ec8SWenchao Xia     switch (ret) {
34143526ec8SWenchao Xia     case 0:
34243526ec8SWenchao Xia         if (info->snapshots) {
34343526ec8SWenchao Xia             info->has_snapshots = true;
34443526ec8SWenchao Xia         }
34543526ec8SWenchao Xia         break;
34643526ec8SWenchao Xia     /* recoverable error */
34743526ec8SWenchao Xia     case -ENOMEDIUM:
34843526ec8SWenchao Xia     case -ENOTSUP:
34943526ec8SWenchao Xia         error_free(err);
35043526ec8SWenchao Xia         break;
35143526ec8SWenchao Xia     default:
35243526ec8SWenchao Xia         error_propagate(errp, err);
35343526ec8SWenchao Xia         qapi_free_ImageInfo(info);
3541963f8d5SPaolo Bonzini         goto out;
35543526ec8SWenchao Xia     }
35643526ec8SWenchao Xia 
35743526ec8SWenchao Xia     *p_info = info;
3581963f8d5SPaolo Bonzini 
3591963f8d5SPaolo Bonzini out:
3601963f8d5SPaolo Bonzini     aio_context_release(bdrv_get_aio_context(bs));
361f364ec65SWenchao Xia }
362f364ec65SWenchao Xia 
363553a7e87SWenchao Xia /* @p_info will be set only on success. */
364d829a211SMarkus Armbruster static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
365553a7e87SWenchao Xia                             Error **errp)
366f364ec65SWenchao Xia {
367f364ec65SWenchao Xia     BlockInfo *info = g_malloc0(sizeof(*info));
368d829a211SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
36946eade7bSKevin Wolf     char *qdev;
37046eade7bSKevin Wolf 
371d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of */
3720a7585dbSMax Reitz     bs = bdrv_skip_implicit_filters(bs);
373d3c8c674SKevin Wolf 
374d829a211SMarkus Armbruster     info->device = g_strdup(blk_name(blk));
375f364ec65SWenchao Xia     info->type = g_strdup("unknown");
376a7f53e26SMarkus Armbruster     info->locked = blk_dev_is_medium_locked(blk);
377a7f53e26SMarkus Armbruster     info->removable = blk_dev_has_removable_media(blk);
378f364ec65SWenchao Xia 
37946eade7bSKevin Wolf     qdev = blk_get_attached_dev_id(blk);
38046eade7bSKevin Wolf     if (qdev && *qdev) {
38146eade7bSKevin Wolf         info->has_qdev = true;
38246eade7bSKevin Wolf         info->qdev = qdev;
38346eade7bSKevin Wolf     } else {
38446eade7bSKevin Wolf         g_free(qdev);
38546eade7bSKevin Wolf     }
38646eade7bSKevin Wolf 
387327032ceSMax Reitz     if (blk_dev_has_tray(blk)) {
388f364ec65SWenchao Xia         info->has_tray_open = true;
389a7f53e26SMarkus Armbruster         info->tray_open = blk_dev_is_tray_open(blk);
390f364ec65SWenchao Xia     }
391f364ec65SWenchao Xia 
392373340b2SMax Reitz     if (blk_iostatus_is_enabled(blk)) {
393f364ec65SWenchao Xia         info->has_io_status = true;
394373340b2SMax Reitz         info->io_status = blk_iostatus(blk);
395f364ec65SWenchao Xia     }
396f364ec65SWenchao Xia 
3975433c24fSMax Reitz     if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
39821b56835SFam Zheng         info->has_dirty_bitmaps = true;
39921b56835SFam Zheng         info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
40021b56835SFam Zheng     }
40121b56835SFam Zheng 
4025433c24fSMax Reitz     if (bs && bs->drv) {
403f364ec65SWenchao Xia         info->has_inserted = true;
404facda544SPeter Krempa         info->inserted = bdrv_block_device_info(blk, bs, false, errp);
405d5a8ee60SAlberto Garcia         if (info->inserted == NULL) {
406553a7e87SWenchao Xia             goto err;
407f364ec65SWenchao Xia         }
408553a7e87SWenchao Xia     }
409553a7e87SWenchao Xia 
410553a7e87SWenchao Xia     *p_info = info;
411553a7e87SWenchao Xia     return;
412553a7e87SWenchao Xia 
413553a7e87SWenchao Xia  err:
414553a7e87SWenchao Xia     qapi_free_BlockInfo(info);
415f364ec65SWenchao Xia }
416f364ec65SWenchao Xia 
4177e5c776dSVladimir Sementsov-Ogievskiy static uint64List *uint64_list(uint64_t *list, int size)
4187e5c776dSVladimir Sementsov-Ogievskiy {
4197e5c776dSVladimir Sementsov-Ogievskiy     int i;
4207e5c776dSVladimir Sementsov-Ogievskiy     uint64List *out_list = NULL;
4217e5c776dSVladimir Sementsov-Ogievskiy     uint64List **pout_list = &out_list;
4227e5c776dSVladimir Sementsov-Ogievskiy 
4237e5c776dSVladimir Sementsov-Ogievskiy     for (i = 0; i < size; i++) {
4247e5c776dSVladimir Sementsov-Ogievskiy         uint64List *entry = g_new(uint64List, 1);
4257e5c776dSVladimir Sementsov-Ogievskiy         entry->value = list[i];
4267e5c776dSVladimir Sementsov-Ogievskiy         *pout_list = entry;
4277e5c776dSVladimir Sementsov-Ogievskiy         pout_list = &entry->next;
4287e5c776dSVladimir Sementsov-Ogievskiy     }
4297e5c776dSVladimir Sementsov-Ogievskiy 
4307e5c776dSVladimir Sementsov-Ogievskiy     *pout_list = NULL;
4317e5c776dSVladimir Sementsov-Ogievskiy 
4327e5c776dSVladimir Sementsov-Ogievskiy     return out_list;
4337e5c776dSVladimir Sementsov-Ogievskiy }
4347e5c776dSVladimir Sementsov-Ogievskiy 
4357e5c776dSVladimir Sementsov-Ogievskiy static void bdrv_latency_histogram_stats(BlockLatencyHistogram *hist,
4367e5c776dSVladimir Sementsov-Ogievskiy                                          bool *not_null,
4377e5c776dSVladimir Sementsov-Ogievskiy                                          BlockLatencyHistogramInfo **info)
4387e5c776dSVladimir Sementsov-Ogievskiy {
4397e5c776dSVladimir Sementsov-Ogievskiy     *not_null = hist->bins != NULL;
4407e5c776dSVladimir Sementsov-Ogievskiy     if (*not_null) {
4417e5c776dSVladimir Sementsov-Ogievskiy         *info = g_new0(BlockLatencyHistogramInfo, 1);
4427e5c776dSVladimir Sementsov-Ogievskiy 
4437e5c776dSVladimir Sementsov-Ogievskiy         (*info)->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
4447e5c776dSVladimir Sementsov-Ogievskiy         (*info)->bins = uint64_list(hist->bins, hist->nbins);
4457e5c776dSVladimir Sementsov-Ogievskiy     }
4467e5c776dSVladimir Sementsov-Ogievskiy }
4477e5c776dSVladimir Sementsov-Ogievskiy 
44854302156SMax Reitz static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
449f364ec65SWenchao Xia {
4502b77e60aSKevin Wolf     BlockAcctStats *stats = blk_get_stats(blk);
451979e9b03SAlberto Garcia     BlockAcctTimedStats *ts = NULL;
4527f0e9da6SMax Reitz 
45354302156SMax Reitz     ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
45454302156SMax Reitz     ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
455159f85ddSAnton Nefedov     ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
45654302156SMax Reitz     ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
45754302156SMax Reitz     ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
458159f85ddSAnton Nefedov     ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
4597ee12dafSAlberto Garcia 
46054302156SMax Reitz     ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
46154302156SMax Reitz     ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
46254302156SMax Reitz     ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
463159f85ddSAnton Nefedov     ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
4647ee12dafSAlberto Garcia 
46554302156SMax Reitz     ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
46654302156SMax Reitz     ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
46754302156SMax Reitz     ds->invalid_flush_operations =
4687ee12dafSAlberto Garcia         stats->invalid_ops[BLOCK_ACCT_FLUSH];
469159f85ddSAnton Nefedov     ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
4707ee12dafSAlberto Garcia 
47154302156SMax Reitz     ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
47254302156SMax Reitz     ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
473159f85ddSAnton Nefedov     ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
47454302156SMax Reitz     ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
47554302156SMax Reitz     ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
47654302156SMax Reitz     ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
47754302156SMax Reitz     ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
478159f85ddSAnton Nefedov     ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
479cb38fffbSAlberto Garcia 
48054302156SMax Reitz     ds->has_idle_time_ns = stats->last_access_time_ns > 0;
48154302156SMax Reitz     if (ds->has_idle_time_ns) {
48254302156SMax Reitz         ds->idle_time_ns = block_acct_idle_time_ns(stats);
483cb38fffbSAlberto Garcia     }
484362e9299SAlberto Garcia 
48554302156SMax Reitz     ds->account_invalid = stats->account_invalid;
48654302156SMax Reitz     ds->account_failed = stats->account_failed;
487979e9b03SAlberto Garcia 
488979e9b03SAlberto Garcia     while ((ts = block_acct_interval_next(stats, ts))) {
489979e9b03SAlberto Garcia         BlockDeviceTimedStatsList *timed_stats =
490979e9b03SAlberto Garcia             g_malloc0(sizeof(*timed_stats));
491979e9b03SAlberto Garcia         BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
49254302156SMax Reitz         timed_stats->next = ds->timed_stats;
493979e9b03SAlberto Garcia         timed_stats->value = dev_stats;
49454302156SMax Reitz         ds->timed_stats = timed_stats;
495979e9b03SAlberto Garcia 
496979e9b03SAlberto Garcia         TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
497979e9b03SAlberto Garcia         TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
498979e9b03SAlberto Garcia         TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
499979e9b03SAlberto Garcia 
500979e9b03SAlberto Garcia         dev_stats->interval_length = ts->interval_length;
501979e9b03SAlberto Garcia 
502979e9b03SAlberto Garcia         dev_stats->min_rd_latency_ns = timed_average_min(rd);
503979e9b03SAlberto Garcia         dev_stats->max_rd_latency_ns = timed_average_max(rd);
504979e9b03SAlberto Garcia         dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
505979e9b03SAlberto Garcia 
506979e9b03SAlberto Garcia         dev_stats->min_wr_latency_ns = timed_average_min(wr);
507979e9b03SAlberto Garcia         dev_stats->max_wr_latency_ns = timed_average_max(wr);
508979e9b03SAlberto Garcia         dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
509979e9b03SAlberto Garcia 
510979e9b03SAlberto Garcia         dev_stats->min_flush_latency_ns = timed_average_min(fl);
511979e9b03SAlberto Garcia         dev_stats->max_flush_latency_ns = timed_average_max(fl);
512979e9b03SAlberto Garcia         dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
51396e4dedaSAlberto Garcia 
51496e4dedaSAlberto Garcia         dev_stats->avg_rd_queue_depth =
51596e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_READ);
51696e4dedaSAlberto Garcia         dev_stats->avg_wr_queue_depth =
51796e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
518979e9b03SAlberto Garcia     }
5197e5c776dSVladimir Sementsov-Ogievskiy 
5207e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_READ],
521cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->has_rd_latency_histogram,
522cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->rd_latency_histogram);
5237e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_WRITE],
524cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->has_wr_latency_histogram,
525cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->wr_latency_histogram);
5267e5c776dSVladimir Sementsov-Ogievskiy     bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_FLUSH],
527cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->has_flush_latency_histogram,
528cb8aac37SVladimir Sementsov-Ogievskiy                                  &ds->flush_latency_histogram);
5297f0e9da6SMax Reitz }
530f364ec65SWenchao Xia 
531d3c8c674SKevin Wolf static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
532d3c8c674SKevin Wolf                                         bool blk_level)
5332b77e60aSKevin Wolf {
5343f26191cSMax Reitz     BdrvChild *parent_child;
5350a7585dbSMax Reitz     BlockDriverState *filter_or_cow_bs;
53620a6d768SDou Liyang     BlockStats *s = NULL;
53720a6d768SDou Liyang 
53820a6d768SDou Liyang     s = g_malloc0(sizeof(*s));
53920a6d768SDou Liyang     s->stats = g_malloc0(sizeof(*s->stats));
54020a6d768SDou Liyang 
54120a6d768SDou Liyang     if (!bs) {
54220a6d768SDou Liyang         return s;
54320a6d768SDou Liyang     }
54420a6d768SDou Liyang 
545d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of in
546d3c8c674SKevin Wolf      * a BlockBackend-level command. Stay at the exact node for a node-level
547d3c8c674SKevin Wolf      * command. */
5480a7585dbSMax Reitz     if (blk_level) {
5490a7585dbSMax Reitz         bs = bdrv_skip_implicit_filters(bs);
550d3c8c674SKevin Wolf     }
551d3c8c674SKevin Wolf 
5522b77e60aSKevin Wolf     if (bdrv_get_node_name(bs)[0]) {
5532b77e60aSKevin Wolf         s->has_node_name = true;
5542b77e60aSKevin Wolf         s->node_name = g_strdup(bdrv_get_node_name(bs));
5552b77e60aSKevin Wolf     }
5562b77e60aSKevin Wolf 
557f7946da2SPaolo Bonzini     s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
55853d8f9d8SMax Reitz 
559d9245599SAnton Nefedov     s->driver_specific = bdrv_get_specific_stats(bs);
560d9245599SAnton Nefedov     if (s->driver_specific) {
561d9245599SAnton Nefedov         s->has_driver_specific = true;
562d9245599SAnton Nefedov     }
563d9245599SAnton Nefedov 
5643f26191cSMax Reitz     parent_child = bdrv_primary_child(bs);
5653f26191cSMax Reitz     if (!parent_child ||
5663f26191cSMax Reitz         !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED)))
5673f26191cSMax Reitz     {
5683f26191cSMax Reitz         BdrvChild *c;
5693f26191cSMax Reitz 
5703f26191cSMax Reitz         /*
5713f26191cSMax Reitz          * Look for a unique data-storing child.  We do not need to look for
5723f26191cSMax Reitz          * filtered children, as there would be only one and it would have been
5733f26191cSMax Reitz          * the primary child.
5743f26191cSMax Reitz          */
5753f26191cSMax Reitz         parent_child = NULL;
5763f26191cSMax Reitz         QLIST_FOREACH(c, &bs->children, next) {
5773f26191cSMax Reitz             if (c->role & BDRV_CHILD_DATA) {
5783f26191cSMax Reitz                 if (parent_child) {
5793f26191cSMax Reitz                     /*
5803f26191cSMax Reitz                      * There are multiple data-storing children and we cannot
5813f26191cSMax Reitz                      * choose between them.
5823f26191cSMax Reitz                      */
5833f26191cSMax Reitz                     parent_child = NULL;
5843f26191cSMax Reitz                     break;
5853f26191cSMax Reitz                 }
5863f26191cSMax Reitz                 parent_child = c;
5873f26191cSMax Reitz             }
5883f26191cSMax Reitz         }
5893f26191cSMax Reitz     }
5903f26191cSMax Reitz     if (parent_child) {
591f364ec65SWenchao Xia         s->has_parent = true;
5923f26191cSMax Reitz         s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level);
593f364ec65SWenchao Xia     }
594f364ec65SWenchao Xia 
5950a7585dbSMax Reitz     filter_or_cow_bs = bdrv_filter_or_cow_bs(bs);
5960a7585dbSMax Reitz     if (blk_level && filter_or_cow_bs) {
5970a7585dbSMax Reitz         /*
5980a7585dbSMax Reitz          * Put any filtered or COW child here (for backwards
5990a7585dbSMax Reitz          * compatibility to when we put bs0->backing here, which might
6000a7585dbSMax Reitz          * be either)
6010a7585dbSMax Reitz          */
602c8059b97SFam Zheng         s->has_backing = true;
6030a7585dbSMax Reitz         s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level);
604c8059b97SFam Zheng     }
605c8059b97SFam Zheng 
60620a6d768SDou Liyang     return s;
607b07363a1SKevin Wolf }
608b07363a1SKevin Wolf 
609f364ec65SWenchao Xia BlockInfoList *qmp_query_block(Error **errp)
610f364ec65SWenchao Xia {
611f364ec65SWenchao Xia     BlockInfoList *head = NULL, **p_next = &head;
612d829a211SMarkus Armbruster     BlockBackend *blk;
613553a7e87SWenchao Xia     Error *local_err = NULL;
614f364ec65SWenchao Xia 
615d5b68844SKevin Wolf     for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
616d5b68844SKevin Wolf         BlockInfoList *info;
617d5b68844SKevin Wolf 
618ec18b0a9SKevin Wolf         if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
619d5b68844SKevin Wolf             continue;
620d5b68844SKevin Wolf         }
621d5b68844SKevin Wolf 
622d5b68844SKevin Wolf         info = g_malloc0(sizeof(*info));
623d829a211SMarkus Armbruster         bdrv_query_info(blk, &info->value, &local_err);
62484d18f06SMarkus Armbruster         if (local_err) {
625553a7e87SWenchao Xia             error_propagate(errp, local_err);
626903c341dSMarkus Armbruster             g_free(info);
627903c341dSMarkus Armbruster             qapi_free_BlockInfoList(head);
628903c341dSMarkus Armbruster             return NULL;
629553a7e87SWenchao Xia         }
630f364ec65SWenchao Xia 
631f364ec65SWenchao Xia         *p_next = info;
632f364ec65SWenchao Xia         p_next = &info->next;
633f364ec65SWenchao Xia     }
634f364ec65SWenchao Xia 
635f364ec65SWenchao Xia     return head;
636f364ec65SWenchao Xia }
637f364ec65SWenchao Xia 
638f71eaa74SFam Zheng BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
639f71eaa74SFam Zheng                                      bool query_nodes,
640f71eaa74SFam Zheng                                      Error **errp)
641f364ec65SWenchao Xia {
642f364ec65SWenchao Xia     BlockStatsList *head = NULL, **p_next = &head;
643a6baa608SDou Liyang     BlockBackend *blk;
644a6baa608SDou Liyang     BlockDriverState *bs;
645f364ec65SWenchao Xia 
646f71eaa74SFam Zheng     /* Just to be safe if query_nodes is not always initialized */
647a6baa608SDou Liyang     if (has_query_nodes && query_nodes) {
648a6baa608SDou Liyang         for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
649f364ec65SWenchao Xia             BlockStatsList *info = g_malloc0(sizeof(*info));
650a6baa608SDou Liyang             AioContext *ctx = bdrv_get_aio_context(bs);
65113344f3aSStefan Hajnoczi 
65213344f3aSStefan Hajnoczi             aio_context_acquire(ctx);
653a6baa608SDou Liyang             info->value = bdrv_query_bds_stats(bs, false);
65413344f3aSStefan Hajnoczi             aio_context_release(ctx);
655f364ec65SWenchao Xia 
656f364ec65SWenchao Xia             *p_next = info;
657f364ec65SWenchao Xia             p_next = &info->next;
658f364ec65SWenchao Xia         }
659a6baa608SDou Liyang     } else {
660567dcb31SKevin Wolf         for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
661f62492bbSKevin Wolf             BlockStatsList *info;
662a6baa608SDou Liyang             AioContext *ctx = blk_get_aio_context(blk);
663a6baa608SDou Liyang             BlockStats *s;
6645a9cb5a9SKevin Wolf             char *qdev;
665a6baa608SDou Liyang 
666567dcb31SKevin Wolf             if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
667567dcb31SKevin Wolf                 continue;
668567dcb31SKevin Wolf             }
669567dcb31SKevin Wolf 
670a6baa608SDou Liyang             aio_context_acquire(ctx);
671a6baa608SDou Liyang             s = bdrv_query_bds_stats(blk_bs(blk), true);
672a6baa608SDou Liyang             s->has_device = true;
673a6baa608SDou Liyang             s->device = g_strdup(blk_name(blk));
6745a9cb5a9SKevin Wolf 
6755a9cb5a9SKevin Wolf             qdev = blk_get_attached_dev_id(blk);
6765a9cb5a9SKevin Wolf             if (qdev && *qdev) {
6775a9cb5a9SKevin Wolf                 s->has_qdev = true;
6785a9cb5a9SKevin Wolf                 s->qdev = qdev;
6795a9cb5a9SKevin Wolf             } else {
6805a9cb5a9SKevin Wolf                 g_free(qdev);
6815a9cb5a9SKevin Wolf             }
6825a9cb5a9SKevin Wolf 
683a6baa608SDou Liyang             bdrv_query_blk_stats(s->stats, blk);
684a6baa608SDou Liyang             aio_context_release(ctx);
685a6baa608SDou Liyang 
686f62492bbSKevin Wolf             info = g_malloc0(sizeof(*info));
687a6baa608SDou Liyang             info->value = s;
688a6baa608SDou Liyang             *p_next = info;
689a6baa608SDou Liyang             p_next = &info->next;
690a6baa608SDou Liyang         }
691a6baa608SDou Liyang     }
692f364ec65SWenchao Xia 
693f364ec65SWenchao Xia     return head;
694f364ec65SWenchao Xia }
695f364ec65SWenchao Xia 
696e1ce7d74SMarkus Armbruster void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
697f364ec65SWenchao Xia {
698de38b500SEric Blake     char date_buf[128], clock_buf[128];
699*b39847a5SPavel Dovgalyuk     char icount_buf[128] = {0};
700f364ec65SWenchao Xia     struct tm tm;
701f364ec65SWenchao Xia     time_t ti;
702f364ec65SWenchao Xia     int64_t secs;
703de38b500SEric Blake     char *sizing = NULL;
704f364ec65SWenchao Xia 
705f364ec65SWenchao Xia     if (!sn) {
706*b39847a5SPavel Dovgalyuk         qemu_printf("%-10s%-18s%7s%20s%13s%11s",
707*b39847a5SPavel Dovgalyuk                     "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT");
708f364ec65SWenchao Xia     } else {
709f364ec65SWenchao Xia         ti = sn->date_sec;
710f364ec65SWenchao Xia         localtime_r(&ti, &tm);
711f364ec65SWenchao Xia         strftime(date_buf, sizeof(date_buf),
712f364ec65SWenchao Xia                  "%Y-%m-%d %H:%M:%S", &tm);
713f364ec65SWenchao Xia         secs = sn->vm_clock_nsec / 1000000000;
714f364ec65SWenchao Xia         snprintf(clock_buf, sizeof(clock_buf),
715f364ec65SWenchao Xia                  "%02d:%02d:%02d.%03d",
716f364ec65SWenchao Xia                  (int)(secs / 3600),
717f364ec65SWenchao Xia                  (int)((secs / 60) % 60),
718f364ec65SWenchao Xia                  (int)(secs % 60),
719f364ec65SWenchao Xia                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
720de38b500SEric Blake         sizing = size_to_str(sn->vm_state_size);
721*b39847a5SPavel Dovgalyuk         if (sn->icount != -1ULL) {
722*b39847a5SPavel Dovgalyuk             snprintf(icount_buf, sizeof(icount_buf),
723*b39847a5SPavel Dovgalyuk                 "%"PRId64, sn->icount);
724*b39847a5SPavel Dovgalyuk         }
725*b39847a5SPavel Dovgalyuk         qemu_printf("%-9s %-17s %7s%20s%13s%11s",
726f364ec65SWenchao Xia                     sn->id_str, sn->name,
727de38b500SEric Blake                     sizing,
728f364ec65SWenchao Xia                     date_buf,
729*b39847a5SPavel Dovgalyuk                     clock_buf,
730*b39847a5SPavel Dovgalyuk                     icount_buf);
731f364ec65SWenchao Xia     }
732de38b500SEric Blake     g_free(sizing);
733f364ec65SWenchao Xia }
734f364ec65SWenchao Xia 
735e1ce7d74SMarkus Armbruster static void dump_qdict(int indentation, QDict *dict);
736e1ce7d74SMarkus Armbruster static void dump_qlist(int indentation, QList *list);
737a8d8ecb7SMax Reitz 
738e1ce7d74SMarkus Armbruster static void dump_qobject(int comp_indent, QObject *obj)
739a8d8ecb7SMax Reitz {
740a8d8ecb7SMax Reitz     switch (qobject_type(obj)) {
74101b2ffceSMarc-André Lureau         case QTYPE_QNUM: {
7427dc847ebSMax Reitz             QNum *value = qobject_to(QNum, obj);
74301b2ffceSMarc-André Lureau             char *tmp = qnum_to_string(value);
744e1ce7d74SMarkus Armbruster             qemu_printf("%s", tmp);
74501b2ffceSMarc-André Lureau             g_free(tmp);
746a8d8ecb7SMax Reitz             break;
747a8d8ecb7SMax Reitz         }
748a8d8ecb7SMax Reitz         case QTYPE_QSTRING: {
7497dc847ebSMax Reitz             QString *value = qobject_to(QString, obj);
750e1ce7d74SMarkus Armbruster             qemu_printf("%s", qstring_get_str(value));
751a8d8ecb7SMax Reitz             break;
752a8d8ecb7SMax Reitz         }
753a8d8ecb7SMax Reitz         case QTYPE_QDICT: {
7547dc847ebSMax Reitz             QDict *value = qobject_to(QDict, obj);
755e1ce7d74SMarkus Armbruster             dump_qdict(comp_indent, value);
756a8d8ecb7SMax Reitz             break;
757a8d8ecb7SMax Reitz         }
758a8d8ecb7SMax Reitz         case QTYPE_QLIST: {
7597dc847ebSMax Reitz             QList *value = qobject_to(QList, obj);
760e1ce7d74SMarkus Armbruster             dump_qlist(comp_indent, value);
761a8d8ecb7SMax Reitz             break;
762a8d8ecb7SMax Reitz         }
763a8d8ecb7SMax Reitz         case QTYPE_QBOOL: {
7647dc847ebSMax Reitz             QBool *value = qobject_to(QBool, obj);
765e1ce7d74SMarkus Armbruster             qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
766a8d8ecb7SMax Reitz             break;
767a8d8ecb7SMax Reitz         }
768a8d8ecb7SMax Reitz         default:
769a8d8ecb7SMax Reitz             abort();
770a8d8ecb7SMax Reitz     }
771a8d8ecb7SMax Reitz }
772a8d8ecb7SMax Reitz 
773e1ce7d74SMarkus Armbruster static void dump_qlist(int indentation, QList *list)
774a8d8ecb7SMax Reitz {
775a8d8ecb7SMax Reitz     const QListEntry *entry;
776a8d8ecb7SMax Reitz     int i = 0;
777a8d8ecb7SMax Reitz 
778a8d8ecb7SMax Reitz     for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
7791310a3d3SEric Blake         QType type = qobject_type(entry->value);
780a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
781e1ce7d74SMarkus Armbruster         qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
782853ccfedSPeter Xu                     composite ? '\n' : ' ');
783e1ce7d74SMarkus Armbruster         dump_qobject(indentation + 1, entry->value);
784a8d8ecb7SMax Reitz         if (!composite) {
785e1ce7d74SMarkus Armbruster             qemu_printf("\n");
786a8d8ecb7SMax Reitz         }
787a8d8ecb7SMax Reitz     }
788a8d8ecb7SMax Reitz }
789a8d8ecb7SMax Reitz 
790e1ce7d74SMarkus Armbruster static void dump_qdict(int indentation, QDict *dict)
791a8d8ecb7SMax Reitz {
792a8d8ecb7SMax Reitz     const QDictEntry *entry;
793a8d8ecb7SMax Reitz 
794a8d8ecb7SMax Reitz     for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
7951310a3d3SEric Blake         QType type = qobject_type(entry->value);
796a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
7975eda6227SPeter Xu         char *key = g_malloc(strlen(entry->key) + 1);
798a8d8ecb7SMax Reitz         int i;
799a8d8ecb7SMax Reitz 
800a8d8ecb7SMax Reitz         /* replace dashes with spaces in key (variable) names */
801a8d8ecb7SMax Reitz         for (i = 0; entry->key[i]; i++) {
802a8d8ecb7SMax Reitz             key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
803a8d8ecb7SMax Reitz         }
804a8d8ecb7SMax Reitz         key[i] = 0;
805e1ce7d74SMarkus Armbruster         qemu_printf("%*s%s:%c", indentation * 4, "", key,
806853ccfedSPeter Xu                     composite ? '\n' : ' ');
807e1ce7d74SMarkus Armbruster         dump_qobject(indentation + 1, entry->value);
808a8d8ecb7SMax Reitz         if (!composite) {
809e1ce7d74SMarkus Armbruster             qemu_printf("\n");
810a8d8ecb7SMax Reitz         }
8115eda6227SPeter Xu         g_free(key);
812a8d8ecb7SMax Reitz     }
813a8d8ecb7SMax Reitz }
814a8d8ecb7SMax Reitz 
815e1ce7d74SMarkus Armbruster void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec)
816a8d8ecb7SMax Reitz {
817a8d8ecb7SMax Reitz     QObject *obj, *data;
8187d5e199aSDaniel P. Berrange     Visitor *v = qobject_output_visitor_new(&obj);
819a8d8ecb7SMax Reitz 
8203b098d56SEric Blake     visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
8213b098d56SEric Blake     visit_complete(v, &obj);
8227dc847ebSMax Reitz     data = qdict_get(qobject_to(QDict, obj), "data");
823e1ce7d74SMarkus Armbruster     dump_qobject(1, data);
824cb3e7f08SMarc-André Lureau     qobject_unref(obj);
8253b098d56SEric Blake     visit_free(v);
826a8d8ecb7SMax Reitz }
827a8d8ecb7SMax Reitz 
828e1ce7d74SMarkus Armbruster void bdrv_image_info_dump(ImageInfo *info)
829f364ec65SWenchao Xia {
830de38b500SEric Blake     char *size_buf, *dsize_buf;
831f364ec65SWenchao Xia     if (!info->has_actual_size) {
832de38b500SEric Blake         dsize_buf = g_strdup("unavailable");
833f364ec65SWenchao Xia     } else {
834de38b500SEric Blake         dsize_buf = size_to_str(info->actual_size);
835f364ec65SWenchao Xia     }
836de38b500SEric Blake     size_buf = size_to_str(info->virtual_size);
837e1ce7d74SMarkus Armbruster     qemu_printf("image: %s\n"
838f364ec65SWenchao Xia                 "file format: %s\n"
839f364ec65SWenchao Xia                 "virtual size: %s (%" PRId64 " bytes)\n"
840f364ec65SWenchao Xia                 "disk size: %s\n",
841f364ec65SWenchao Xia                 info->filename, info->format, size_buf,
842f364ec65SWenchao Xia                 info->virtual_size,
843f364ec65SWenchao Xia                 dsize_buf);
844de38b500SEric Blake     g_free(size_buf);
845de38b500SEric Blake     g_free(dsize_buf);
846f364ec65SWenchao Xia 
847f364ec65SWenchao Xia     if (info->has_encrypted && info->encrypted) {
848e1ce7d74SMarkus Armbruster         qemu_printf("encrypted: yes\n");
849f364ec65SWenchao Xia     }
850f364ec65SWenchao Xia 
851f364ec65SWenchao Xia     if (info->has_cluster_size) {
852e1ce7d74SMarkus Armbruster         qemu_printf("cluster_size: %" PRId64 "\n",
8535b917044SWenchao Xia                     info->cluster_size);
854f364ec65SWenchao Xia     }
855f364ec65SWenchao Xia 
856f364ec65SWenchao Xia     if (info->has_dirty_flag && info->dirty_flag) {
857e1ce7d74SMarkus Armbruster         qemu_printf("cleanly shut down: no\n");
858f364ec65SWenchao Xia     }
859f364ec65SWenchao Xia 
860f364ec65SWenchao Xia     if (info->has_backing_filename) {
861e1ce7d74SMarkus Armbruster         qemu_printf("backing file: %s", info->backing_filename);
8625c9d9ca5SJohn Snow         if (!info->has_full_backing_filename) {
863e1ce7d74SMarkus Armbruster             qemu_printf(" (cannot determine actual path)");
8645c9d9ca5SJohn Snow         } else if (strcmp(info->backing_filename,
8655c9d9ca5SJohn Snow                           info->full_backing_filename) != 0) {
866e1ce7d74SMarkus Armbruster             qemu_printf(" (actual path: %s)", info->full_backing_filename);
867f364ec65SWenchao Xia         }
868e1ce7d74SMarkus Armbruster         qemu_printf("\n");
869f364ec65SWenchao Xia         if (info->has_backing_filename_format) {
870e1ce7d74SMarkus Armbruster             qemu_printf("backing file format: %s\n",
8715b917044SWenchao Xia                         info->backing_filename_format);
872f364ec65SWenchao Xia         }
873f364ec65SWenchao Xia     }
874f364ec65SWenchao Xia 
875f364ec65SWenchao Xia     if (info->has_snapshots) {
876f364ec65SWenchao Xia         SnapshotInfoList *elem;
877f364ec65SWenchao Xia 
878e1ce7d74SMarkus Armbruster         qemu_printf("Snapshot list:\n");
879e1ce7d74SMarkus Armbruster         bdrv_snapshot_dump(NULL);
880e1ce7d74SMarkus Armbruster         qemu_printf("\n");
881f364ec65SWenchao Xia 
882f364ec65SWenchao Xia         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
883f364ec65SWenchao Xia          * we convert to the block layer's native QEMUSnapshotInfo for now.
884f364ec65SWenchao Xia          */
885f364ec65SWenchao Xia         for (elem = info->snapshots; elem; elem = elem->next) {
886f364ec65SWenchao Xia             QEMUSnapshotInfo sn = {
887f364ec65SWenchao Xia                 .vm_state_size = elem->value->vm_state_size,
888f364ec65SWenchao Xia                 .date_sec = elem->value->date_sec,
889f364ec65SWenchao Xia                 .date_nsec = elem->value->date_nsec,
890f364ec65SWenchao Xia                 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
891f364ec65SWenchao Xia                                  elem->value->vm_clock_nsec,
892*b39847a5SPavel Dovgalyuk                 .icount = elem->value->has_icount ?
893*b39847a5SPavel Dovgalyuk                           elem->value->icount : -1ULL,
894f364ec65SWenchao Xia             };
895f364ec65SWenchao Xia 
896f364ec65SWenchao Xia             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
897f364ec65SWenchao Xia             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
898e1ce7d74SMarkus Armbruster             bdrv_snapshot_dump(&sn);
899e1ce7d74SMarkus Armbruster             qemu_printf("\n");
900f364ec65SWenchao Xia         }
901f364ec65SWenchao Xia     }
902a8d8ecb7SMax Reitz 
903a8d8ecb7SMax Reitz     if (info->has_format_specific) {
904e1ce7d74SMarkus Armbruster         qemu_printf("Format specific information:\n");
905e1ce7d74SMarkus Armbruster         bdrv_image_info_specific_dump(info->format_specific);
906a8d8ecb7SMax Reitz     }
907f364ec65SWenchao Xia }
908