xref: /qemu/block/qapi.c (revision d570177b)
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"
29e2c1c34fSMarkus Armbruster #include "block/dirty-bitmap.h"
3076f4afb4SAlberto Garcia #include "block/throttle-groups.h"
31e2462113SFrancesco Romani #include "block/write-threshold.h"
32e688df6bSMarkus Armbruster #include "qapi/error.h"
339af23989SMarkus Armbruster #include "qapi/qapi-commands-block-core.h"
34b3db211fSDaniel P. Berrange #include "qapi/qobject-output-visitor.h"
359af23989SMarkus Armbruster #include "qapi/qapi-visit-block-core.h"
366b673957SMarkus Armbruster #include "qapi/qmp/qbool.h"
37452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
3847e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h"
3915280c36SMarkus Armbruster #include "qapi/qmp/qnum.h"
406b673957SMarkus Armbruster #include "qapi/qmp/qstring.h"
41e1ce7d74SMarkus Armbruster #include "qemu/qemu-print.h"
42d829a211SMarkus Armbruster #include "sysemu/block-backend.h"
43f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
44f364ec65SWenchao Xia 
45c83f9fbaSKevin Wolf BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
46facda544SPeter Krempa                                         BlockDriverState *bs,
47facda544SPeter Krempa                                         bool flat,
48facda544SPeter Krempa                                         Error **errp)
49c13163fbSBenoît Canet {
50d5a8ee60SAlberto Garcia     ImageInfo **p_image_info;
515d881359SHanna Reitz     ImageInfo *backing_info;
520b877d09SMax Reitz     BlockDriverState *bs0, *backing;
53d470ad42SMax Reitz     BlockDeviceInfo *info;
545d881359SHanna Reitz     ERRP_GUARD();
55c13163fbSBenoît Canet 
56d470ad42SMax Reitz     if (!bs->drv) {
57d470ad42SMax Reitz         error_setg(errp, "Block device %s is ejected", bs->node_name);
58d470ad42SMax Reitz         return NULL;
59d470ad42SMax Reitz     }
60d470ad42SMax Reitz 
61f30c66baSMax Reitz     bdrv_refresh_filename(bs);
62f30c66baSMax Reitz 
63d470ad42SMax Reitz     info = g_malloc0(sizeof(*info));
64c13163fbSBenoît Canet     info->file                   = g_strdup(bs->filename);
65307261b2SVladimir Sementsov-Ogievskiy     info->ro                     = bdrv_is_read_only(bs);
66c13163fbSBenoît Canet     info->drv                    = g_strdup(bs->drv->format_name);
67c13163fbSBenoît Canet     info->encrypted              = bs->encrypted;
68c13163fbSBenoît Canet 
699e193c5aSKevin Wolf     info->cache = g_new(BlockdevCacheInfo, 1);
709e193c5aSKevin Wolf     *info->cache = (BlockdevCacheInfo) {
71c83f9fbaSKevin Wolf         .writeback      = blk ? blk_enable_write_cache(blk) : true,
729e193c5aSKevin Wolf         .direct         = !!(bs->open_flags & BDRV_O_NOCACHE),
739e193c5aSKevin Wolf         .no_flush       = !!(bs->open_flags & BDRV_O_NO_FLUSH),
749e193c5aSKevin Wolf     };
759e193c5aSKevin Wolf 
76c13163fbSBenoît Canet     if (bs->node_name[0]) {
77c13163fbSBenoît Canet         info->node_name = g_strdup(bs->node_name);
78c13163fbSBenoît Canet     }
79c13163fbSBenoît Canet 
800b877d09SMax Reitz     backing = bdrv_cow_bs(bs);
810b877d09SMax Reitz     if (backing) {
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 
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;
1525d881359SHanna Reitz 
1535d881359SHanna Reitz     /*
1545d881359SHanna Reitz      * Skip automatically inserted nodes that the user isn't aware of for
1555d881359SHanna Reitz      * query-block (blk != NULL), but not for query-named-block-nodes
1565d881359SHanna Reitz      */
1575d881359SHanna Reitz     bdrv_query_image_info(bs0, p_image_info, flat, blk != NULL, errp);
1585d881359SHanna Reitz     if (*errp) {
159d5a8ee60SAlberto Garcia         qapi_free_BlockDeviceInfo(info);
160d5a8ee60SAlberto Garcia         return NULL;
161d5a8ee60SAlberto Garcia     }
162d3c8c674SKevin Wolf 
1635d881359SHanna Reitz     backing_info = info->image->backing_image;
1645d881359SHanna Reitz     while (backing_info) {
165d3c8c674SKevin Wolf         info->backing_file_depth++;
1665d881359SHanna Reitz         backing_info = backing_info->backing_image;
167d5a8ee60SAlberto Garcia     }
168d5a8ee60SAlberto Garcia 
169c13163fbSBenoît Canet     return info;
170c13163fbSBenoît Canet }
171c13163fbSBenoît Canet 
172fb0ed453SWenchao Xia /*
173fb0ed453SWenchao Xia  * Returns 0 on success, with *p_list either set to describe snapshot
174fb0ed453SWenchao Xia  * information, or NULL because there are no snapshots.  Returns -errno on
175fb0ed453SWenchao Xia  * error, with *p_list untouched.
176fb0ed453SWenchao Xia  */
177fb0ed453SWenchao Xia int bdrv_query_snapshot_info_list(BlockDriverState *bs,
178fb0ed453SWenchao Xia                                   SnapshotInfoList **p_list,
179fb0ed453SWenchao Xia                                   Error **errp)
180f364ec65SWenchao Xia {
181f364ec65SWenchao Xia     int i, sn_count;
182f364ec65SWenchao Xia     QEMUSnapshotInfo *sn_tab = NULL;
18395b3a8c8SEric Blake     SnapshotInfoList *head = NULL, **tail = &head;
184fb0ed453SWenchao Xia     SnapshotInfo *info;
185fb0ed453SWenchao Xia 
186f364ec65SWenchao Xia     sn_count = bdrv_snapshot_list(bs, &sn_tab);
187fb0ed453SWenchao Xia     if (sn_count < 0) {
188fb0ed453SWenchao Xia         const char *dev = bdrv_get_device_name(bs);
189fb0ed453SWenchao Xia         switch (sn_count) {
190fb0ed453SWenchao Xia         case -ENOMEDIUM:
191fb0ed453SWenchao Xia             error_setg(errp, "Device '%s' is not inserted", dev);
192fb0ed453SWenchao Xia             break;
193fb0ed453SWenchao Xia         case -ENOTSUP:
194fb0ed453SWenchao Xia             error_setg(errp,
195fb0ed453SWenchao Xia                        "Device '%s' does not support internal snapshots",
196fb0ed453SWenchao Xia                        dev);
197fb0ed453SWenchao Xia             break;
198fb0ed453SWenchao Xia         default:
199fb0ed453SWenchao Xia             error_setg_errno(errp, -sn_count,
200fb0ed453SWenchao Xia                              "Can't list snapshots of device '%s'", dev);
201fb0ed453SWenchao Xia             break;
202fb0ed453SWenchao Xia         }
203fb0ed453SWenchao Xia         return sn_count;
204fb0ed453SWenchao Xia     }
205f364ec65SWenchao Xia 
206f364ec65SWenchao Xia     for (i = 0; i < sn_count; i++) {
207fb0ed453SWenchao Xia         info = g_new0(SnapshotInfo, 1);
208fb0ed453SWenchao Xia         info->id            = g_strdup(sn_tab[i].id_str);
209fb0ed453SWenchao Xia         info->name          = g_strdup(sn_tab[i].name);
210fb0ed453SWenchao Xia         info->vm_state_size = sn_tab[i].vm_state_size;
211fb0ed453SWenchao Xia         info->date_sec      = sn_tab[i].date_sec;
212fb0ed453SWenchao Xia         info->date_nsec     = sn_tab[i].date_nsec;
213fb0ed453SWenchao Xia         info->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
214fb0ed453SWenchao Xia         info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
215b39847a5SPavel Dovgalyuk         info->icount        = sn_tab[i].icount;
216b39847a5SPavel Dovgalyuk         info->has_icount    = sn_tab[i].icount != -1ULL;
217f364ec65SWenchao Xia 
21895b3a8c8SEric Blake         QAPI_LIST_APPEND(tail, info);
219f364ec65SWenchao Xia     }
220f364ec65SWenchao Xia 
221f364ec65SWenchao Xia     g_free(sn_tab);
222fb0ed453SWenchao Xia     *p_list = head;
223fb0ed453SWenchao Xia     return 0;
224f364ec65SWenchao Xia }
225f364ec65SWenchao Xia 
22643526ec8SWenchao Xia /**
227a2085f89SHanna Reitz  * Helper function for other query info functions.  Store information about @bs
228a2085f89SHanna Reitz  * in @info, setting @errp on error.
22943526ec8SWenchao Xia  */
230a2085f89SHanna Reitz static void bdrv_do_query_node_info(BlockDriverState *bs,
231a2085f89SHanna Reitz                                     BlockNodeInfo *info,
23243526ec8SWenchao Xia                                     Error **errp)
233f364ec65SWenchao Xia {
23452bf1e72SMarkus Armbruster     int64_t size;
23543526ec8SWenchao Xia     const char *backing_filename;
236f364ec65SWenchao Xia     BlockDriverInfo bdi;
23743526ec8SWenchao Xia     int ret;
23843526ec8SWenchao Xia     Error *err = NULL;
239f364ec65SWenchao Xia 
2401963f8d5SPaolo Bonzini     aio_context_acquire(bdrv_get_aio_context(bs));
2411963f8d5SPaolo Bonzini 
24252bf1e72SMarkus Armbruster     size = bdrv_getlength(bs);
24352bf1e72SMarkus Armbruster     if (size < 0) {
2449adceb02SFam Zheng         error_setg_errno(errp, -size, "Can't get image size '%s'",
2459adceb02SFam Zheng                          bs->exact_filename);
2461963f8d5SPaolo Bonzini         goto out;
24752bf1e72SMarkus Armbruster     }
248f364ec65SWenchao Xia 
249f30c66baSMax Reitz     bdrv_refresh_filename(bs);
250f30c66baSMax Reitz 
25143526ec8SWenchao Xia     info->filename        = g_strdup(bs->filename);
252f364ec65SWenchao Xia     info->format          = g_strdup(bdrv_get_format_name(bs));
25352bf1e72SMarkus Armbruster     info->virtual_size    = size;
254f364ec65SWenchao Xia     info->actual_size     = bdrv_get_allocated_file_size(bs);
255f364ec65SWenchao Xia     info->has_actual_size = info->actual_size >= 0;
2568b8277cdSMax Reitz     if (bs->encrypted) {
257f364ec65SWenchao Xia         info->encrypted = true;
258f364ec65SWenchao Xia         info->has_encrypted = true;
259f364ec65SWenchao Xia     }
260f364ec65SWenchao Xia     if (bdrv_get_info(bs, &bdi) >= 0) {
261f364ec65SWenchao Xia         if (bdi.cluster_size != 0) {
262f364ec65SWenchao Xia             info->cluster_size = bdi.cluster_size;
263f364ec65SWenchao Xia             info->has_cluster_size = true;
264f364ec65SWenchao Xia         }
265f364ec65SWenchao Xia         info->dirty_flag = bdi.is_dirty;
266f364ec65SWenchao Xia         info->has_dirty_flag = true;
267f364ec65SWenchao Xia     }
2681bf6e9caSAndrey Shinkevich     info->format_specific = bdrv_get_specific_info(bs, &err);
2691bf6e9caSAndrey Shinkevich     if (err) {
2701bf6e9caSAndrey Shinkevich         error_propagate(errp, err);
2711bf6e9caSAndrey Shinkevich         goto out;
2721bf6e9caSAndrey Shinkevich     }
27343526ec8SWenchao Xia     backing_filename = bs->backing_file;
274f364ec65SWenchao Xia     if (backing_filename[0] != '\0') {
2756b6833c1SMax Reitz         char *backing_filename2;
2760b877d09SMax Reitz 
277f364ec65SWenchao Xia         info->backing_filename = g_strdup(backing_filename);
2786b6833c1SMax Reitz         backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
279f364ec65SWenchao Xia 
28012dcb1c0SJohn Snow         /* Always report the full_backing_filename if present, even if it's the
28112dcb1c0SJohn Snow          * same as backing_filename. That they are same is useful info. */
28212dcb1c0SJohn Snow         if (backing_filename2) {
28312dcb1c0SJohn Snow             info->full_backing_filename = g_strdup(backing_filename2);
284f364ec65SWenchao Xia         }
285f364ec65SWenchao Xia 
286f364ec65SWenchao Xia         if (bs->backing_format[0]) {
287f364ec65SWenchao Xia             info->backing_filename_format = g_strdup(bs->backing_format);
288f364ec65SWenchao Xia         }
289564d64bdSJeff Cody         g_free(backing_filename2);
290f364ec65SWenchao Xia     }
29143526ec8SWenchao Xia 
29243526ec8SWenchao Xia     ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
29343526ec8SWenchao Xia     switch (ret) {
29443526ec8SWenchao Xia     case 0:
29543526ec8SWenchao Xia         if (info->snapshots) {
29643526ec8SWenchao Xia             info->has_snapshots = true;
29743526ec8SWenchao Xia         }
29843526ec8SWenchao Xia         break;
29943526ec8SWenchao Xia     /* recoverable error */
30043526ec8SWenchao Xia     case -ENOMEDIUM:
30143526ec8SWenchao Xia     case -ENOTSUP:
30243526ec8SWenchao Xia         error_free(err);
30343526ec8SWenchao Xia         break;
30443526ec8SWenchao Xia     default:
30543526ec8SWenchao Xia         error_propagate(errp, err);
3061963f8d5SPaolo Bonzini         goto out;
30743526ec8SWenchao Xia     }
30843526ec8SWenchao Xia 
3091963f8d5SPaolo Bonzini out:
3101963f8d5SPaolo Bonzini     aio_context_release(bdrv_get_aio_context(bs));
311f364ec65SWenchao Xia }
312f364ec65SWenchao Xia 
313a2085f89SHanna Reitz /**
314a2085f89SHanna Reitz  * bdrv_query_block_node_info:
315a2085f89SHanna Reitz  * @bs: block node to examine
316a2085f89SHanna Reitz  * @p_info: location to store node information
317a2085f89SHanna Reitz  * @errp: location to store error information
318a2085f89SHanna Reitz  *
319a2085f89SHanna Reitz  * Store image information about @bs in @p_info.
320a2085f89SHanna Reitz  *
321a2085f89SHanna Reitz  * @p_info will be set only on success. On error, store error in @errp.
322a2085f89SHanna Reitz  */
323a2085f89SHanna Reitz void bdrv_query_block_node_info(BlockDriverState *bs,
324a2085f89SHanna Reitz                                 BlockNodeInfo **p_info,
325a2085f89SHanna Reitz                                 Error **errp)
326a2085f89SHanna Reitz {
327a2085f89SHanna Reitz     BlockNodeInfo *info;
328a2085f89SHanna Reitz     ERRP_GUARD();
329a2085f89SHanna Reitz 
330a2085f89SHanna Reitz     info = g_new0(BlockNodeInfo, 1);
331a2085f89SHanna Reitz     bdrv_do_query_node_info(bs, info, errp);
332a2085f89SHanna Reitz     if (*errp) {
333a2085f89SHanna Reitz         qapi_free_BlockNodeInfo(info);
334a2085f89SHanna Reitz         return;
335a2085f89SHanna Reitz     }
336a2085f89SHanna Reitz 
337a2085f89SHanna Reitz     *p_info = info;
338a2085f89SHanna Reitz }
339a2085f89SHanna Reitz 
340a2085f89SHanna Reitz /**
341a2085f89SHanna Reitz  * bdrv_query_image_info:
342a2085f89SHanna Reitz  * @bs: block node to examine
343a2085f89SHanna Reitz  * @p_info: location to store image information
3445d881359SHanna Reitz  * @flat: skip backing node information
3455d881359SHanna Reitz  * @skip_implicit_filters: skip implicit filters in the backing chain
346a2085f89SHanna Reitz  * @errp: location to store error information
347a2085f89SHanna Reitz  *
3485d881359SHanna Reitz  * Store image information in @p_info, potentially recursively covering the
3495d881359SHanna Reitz  * backing chain.
350a2085f89SHanna Reitz  *
3515d881359SHanna Reitz  * If @flat is true, do not query backing image information, i.e.
3525d881359SHanna Reitz  * (*p_info)->has_backing_image will be set to false and
3535d881359SHanna Reitz  * (*p_info)->backing_image to NULL even when the image does in fact have a
3545d881359SHanna Reitz  * backing image.
3555d881359SHanna Reitz  *
3565d881359SHanna Reitz  * If @skip_implicit_filters is true, implicit filter nodes in the backing chain
3575d881359SHanna Reitz  * will be skipped when querying backing image information.
3585d881359SHanna Reitz  * (@skip_implicit_filters is ignored when @flat is true.)
359a2085f89SHanna Reitz  *
360a2085f89SHanna Reitz  * @p_info will be set only on success. On error, store error in @errp.
361a2085f89SHanna Reitz  */
362a2085f89SHanna Reitz void bdrv_query_image_info(BlockDriverState *bs,
363a2085f89SHanna Reitz                            ImageInfo **p_info,
3645d881359SHanna Reitz                            bool flat,
3655d881359SHanna Reitz                            bool skip_implicit_filters,
366a2085f89SHanna Reitz                            Error **errp)
367a2085f89SHanna Reitz {
368a2085f89SHanna Reitz     ImageInfo *info;
369a2085f89SHanna Reitz     ERRP_GUARD();
370a2085f89SHanna Reitz 
371a2085f89SHanna Reitz     info = g_new0(ImageInfo, 1);
372a2085f89SHanna Reitz     bdrv_do_query_node_info(bs, qapi_ImageInfo_base(info), errp);
373a2085f89SHanna Reitz     if (*errp) {
3745d881359SHanna Reitz         goto fail;
3755d881359SHanna Reitz     }
3765d881359SHanna Reitz 
3775d881359SHanna Reitz     if (!flat) {
3785d881359SHanna Reitz         BlockDriverState *backing;
3795d881359SHanna Reitz 
3805d881359SHanna Reitz         /*
3815d881359SHanna Reitz          * Use any filtered child here (for backwards compatibility to when
3825d881359SHanna Reitz          * we always took bs->backing, which might be any filtered child).
3835d881359SHanna Reitz          */
3845d881359SHanna Reitz         backing = bdrv_filter_or_cow_bs(bs);
3855d881359SHanna Reitz         if (skip_implicit_filters) {
3865d881359SHanna Reitz             backing = bdrv_skip_implicit_filters(backing);
3875d881359SHanna Reitz         }
3885d881359SHanna Reitz 
3895d881359SHanna Reitz         if (backing) {
3905d881359SHanna Reitz             bdrv_query_image_info(backing, &info->backing_image, false,
3915d881359SHanna Reitz                                   skip_implicit_filters, errp);
3925d881359SHanna Reitz             if (*errp) {
3935d881359SHanna Reitz                 goto fail;
3945d881359SHanna Reitz             }
3955d881359SHanna Reitz         }
396a2085f89SHanna Reitz     }
397a2085f89SHanna Reitz 
398a2085f89SHanna Reitz     *p_info = info;
3995d881359SHanna Reitz     return;
4005d881359SHanna Reitz 
4015d881359SHanna Reitz fail:
4025d881359SHanna Reitz     assert(*errp);
4035d881359SHanna Reitz     qapi_free_ImageInfo(info);
404a2085f89SHanna Reitz }
405a2085f89SHanna Reitz 
4066cab3399SHanna Reitz /**
4076cab3399SHanna Reitz  * bdrv_query_block_graph_info:
4086cab3399SHanna Reitz  * @bs: root node to start from
4096cab3399SHanna Reitz  * @p_info: location to store image information
4106cab3399SHanna Reitz  * @errp: location to store error information
4116cab3399SHanna Reitz  *
4126cab3399SHanna Reitz  * Store image information about the graph starting from @bs in @p_info.
4136cab3399SHanna Reitz  *
4146cab3399SHanna Reitz  * @p_info will be set only on success. On error, store error in @errp.
4156cab3399SHanna Reitz  */
4166cab3399SHanna Reitz void bdrv_query_block_graph_info(BlockDriverState *bs,
4176cab3399SHanna Reitz                                  BlockGraphInfo **p_info,
4186cab3399SHanna Reitz                                  Error **errp)
4196cab3399SHanna Reitz {
4206cab3399SHanna Reitz     BlockGraphInfo *info;
4216cab3399SHanna Reitz     BlockChildInfoList **children_list_tail;
4226cab3399SHanna Reitz     BdrvChild *c;
4236cab3399SHanna Reitz     ERRP_GUARD();
4246cab3399SHanna Reitz 
4256cab3399SHanna Reitz     info = g_new0(BlockGraphInfo, 1);
4266cab3399SHanna Reitz     bdrv_do_query_node_info(bs, qapi_BlockGraphInfo_base(info), errp);
4276cab3399SHanna Reitz     if (*errp) {
4286cab3399SHanna Reitz         goto fail;
4296cab3399SHanna Reitz     }
4306cab3399SHanna Reitz 
4316cab3399SHanna Reitz     children_list_tail = &info->children;
4326cab3399SHanna Reitz 
4336cab3399SHanna Reitz     QLIST_FOREACH(c, &bs->children, next) {
4346cab3399SHanna Reitz         BlockChildInfo *c_info;
4356cab3399SHanna Reitz 
4366cab3399SHanna Reitz         c_info = g_new0(BlockChildInfo, 1);
4376cab3399SHanna Reitz         QAPI_LIST_APPEND(children_list_tail, c_info);
4386cab3399SHanna Reitz 
4396cab3399SHanna Reitz         c_info->name = g_strdup(c->name);
4406cab3399SHanna Reitz         bdrv_query_block_graph_info(c->bs, &c_info->info, errp);
4416cab3399SHanna Reitz         if (*errp) {
4426cab3399SHanna Reitz             goto fail;
4436cab3399SHanna Reitz         }
4446cab3399SHanna Reitz     }
4456cab3399SHanna Reitz 
4466cab3399SHanna Reitz     *p_info = info;
4476cab3399SHanna Reitz     return;
4486cab3399SHanna Reitz 
4496cab3399SHanna Reitz fail:
4506cab3399SHanna Reitz     assert(*errp != NULL);
4516cab3399SHanna Reitz     qapi_free_BlockGraphInfo(info);
4526cab3399SHanna Reitz }
4536cab3399SHanna Reitz 
454553a7e87SWenchao Xia /* @p_info will be set only on success. */
455d829a211SMarkus Armbruster static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
456553a7e87SWenchao Xia                             Error **errp)
457f364ec65SWenchao Xia {
458f364ec65SWenchao Xia     BlockInfo *info = g_malloc0(sizeof(*info));
459d829a211SMarkus Armbruster     BlockDriverState *bs = blk_bs(blk);
46046eade7bSKevin Wolf     char *qdev;
46146eade7bSKevin Wolf 
462d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of */
4630a7585dbSMax Reitz     bs = bdrv_skip_implicit_filters(bs);
464d3c8c674SKevin Wolf 
465d829a211SMarkus Armbruster     info->device = g_strdup(blk_name(blk));
466f364ec65SWenchao Xia     info->type = g_strdup("unknown");
467a7f53e26SMarkus Armbruster     info->locked = blk_dev_is_medium_locked(blk);
468a7f53e26SMarkus Armbruster     info->removable = blk_dev_has_removable_media(blk);
469f364ec65SWenchao Xia 
47046eade7bSKevin Wolf     qdev = blk_get_attached_dev_id(blk);
47146eade7bSKevin Wolf     if (qdev && *qdev) {
47246eade7bSKevin Wolf         info->qdev = qdev;
47346eade7bSKevin Wolf     } else {
47446eade7bSKevin Wolf         g_free(qdev);
47546eade7bSKevin Wolf     }
47646eade7bSKevin Wolf 
477327032ceSMax Reitz     if (blk_dev_has_tray(blk)) {
478f364ec65SWenchao Xia         info->has_tray_open = true;
479a7f53e26SMarkus Armbruster         info->tray_open = blk_dev_is_tray_open(blk);
480f364ec65SWenchao Xia     }
481f364ec65SWenchao Xia 
482373340b2SMax Reitz     if (blk_iostatus_is_enabled(blk)) {
483f364ec65SWenchao Xia         info->has_io_status = true;
484373340b2SMax Reitz         info->io_status = blk_iostatus(blk);
485f364ec65SWenchao Xia     }
486f364ec65SWenchao Xia 
4875433c24fSMax Reitz     if (bs && bs->drv) {
488facda544SPeter Krempa         info->inserted = bdrv_block_device_info(blk, bs, false, errp);
489d5a8ee60SAlberto Garcia         if (info->inserted == NULL) {
490553a7e87SWenchao Xia             goto err;
491f364ec65SWenchao Xia         }
492553a7e87SWenchao Xia     }
493553a7e87SWenchao Xia 
494553a7e87SWenchao Xia     *p_info = info;
495553a7e87SWenchao Xia     return;
496553a7e87SWenchao Xia 
497553a7e87SWenchao Xia  err:
498553a7e87SWenchao Xia     qapi_free_BlockInfo(info);
499f364ec65SWenchao Xia }
500f364ec65SWenchao Xia 
5017e5c776dSVladimir Sementsov-Ogievskiy static uint64List *uint64_list(uint64_t *list, int size)
5027e5c776dSVladimir Sementsov-Ogievskiy {
5037e5c776dSVladimir Sementsov-Ogievskiy     int i;
5047e5c776dSVladimir Sementsov-Ogievskiy     uint64List *out_list = NULL;
505c3033fd3SEric Blake     uint64List **tail = &out_list;
5067e5c776dSVladimir Sementsov-Ogievskiy 
5077e5c776dSVladimir Sementsov-Ogievskiy     for (i = 0; i < size; i++) {
508c3033fd3SEric Blake         QAPI_LIST_APPEND(tail, list[i]);
5097e5c776dSVladimir Sementsov-Ogievskiy     }
5107e5c776dSVladimir Sementsov-Ogievskiy 
5117e5c776dSVladimir Sementsov-Ogievskiy     return out_list;
5127e5c776dSVladimir Sementsov-Ogievskiy }
5137e5c776dSVladimir Sementsov-Ogievskiy 
51454fde4ffSMarkus Armbruster static BlockLatencyHistogramInfo *
51554fde4ffSMarkus Armbruster bdrv_latency_histogram_stats(BlockLatencyHistogram *hist)
5167e5c776dSVladimir Sementsov-Ogievskiy {
51754fde4ffSMarkus Armbruster     BlockLatencyHistogramInfo *info;
5187e5c776dSVladimir Sementsov-Ogievskiy 
51954fde4ffSMarkus Armbruster     if (!hist->bins) {
52054fde4ffSMarkus Armbruster         return NULL;
5217e5c776dSVladimir Sementsov-Ogievskiy     }
52254fde4ffSMarkus Armbruster 
52354fde4ffSMarkus Armbruster     info = g_new0(BlockLatencyHistogramInfo, 1);
52454fde4ffSMarkus Armbruster     info->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
52554fde4ffSMarkus Armbruster     info->bins = uint64_list(hist->bins, hist->nbins);
52654fde4ffSMarkus Armbruster     return info;
5277e5c776dSVladimir Sementsov-Ogievskiy }
5287e5c776dSVladimir Sementsov-Ogievskiy 
52954302156SMax Reitz static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
530f364ec65SWenchao Xia {
5312b77e60aSKevin Wolf     BlockAcctStats *stats = blk_get_stats(blk);
532979e9b03SAlberto Garcia     BlockAcctTimedStats *ts = NULL;
53354fde4ffSMarkus Armbruster     BlockLatencyHistogram *hgram;
5347f0e9da6SMax Reitz 
53554302156SMax Reitz     ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
53654302156SMax Reitz     ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
537159f85ddSAnton Nefedov     ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
53854302156SMax Reitz     ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
53954302156SMax Reitz     ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
540159f85ddSAnton Nefedov     ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
5417ee12dafSAlberto Garcia 
54254302156SMax Reitz     ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
54354302156SMax Reitz     ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
54454302156SMax Reitz     ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
545159f85ddSAnton Nefedov     ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
5467ee12dafSAlberto Garcia 
54754302156SMax Reitz     ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
54854302156SMax Reitz     ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
54954302156SMax Reitz     ds->invalid_flush_operations =
5507ee12dafSAlberto Garcia         stats->invalid_ops[BLOCK_ACCT_FLUSH];
551159f85ddSAnton Nefedov     ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
5527ee12dafSAlberto Garcia 
55354302156SMax Reitz     ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
55454302156SMax Reitz     ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
555159f85ddSAnton Nefedov     ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
55654302156SMax Reitz     ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
55754302156SMax Reitz     ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
55854302156SMax Reitz     ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
55954302156SMax Reitz     ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
560159f85ddSAnton Nefedov     ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
561cb38fffbSAlberto Garcia 
56254302156SMax Reitz     ds->has_idle_time_ns = stats->last_access_time_ns > 0;
56354302156SMax Reitz     if (ds->has_idle_time_ns) {
56454302156SMax Reitz         ds->idle_time_ns = block_acct_idle_time_ns(stats);
565cb38fffbSAlberto Garcia     }
566362e9299SAlberto Garcia 
56754302156SMax Reitz     ds->account_invalid = stats->account_invalid;
56854302156SMax Reitz     ds->account_failed = stats->account_failed;
569979e9b03SAlberto Garcia 
570979e9b03SAlberto Garcia     while ((ts = block_acct_interval_next(stats, ts))) {
571979e9b03SAlberto Garcia         BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
572979e9b03SAlberto Garcia 
573979e9b03SAlberto Garcia         TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
574979e9b03SAlberto Garcia         TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
575979e9b03SAlberto Garcia         TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
576979e9b03SAlberto Garcia 
577979e9b03SAlberto Garcia         dev_stats->interval_length = ts->interval_length;
578979e9b03SAlberto Garcia 
579979e9b03SAlberto Garcia         dev_stats->min_rd_latency_ns = timed_average_min(rd);
580979e9b03SAlberto Garcia         dev_stats->max_rd_latency_ns = timed_average_max(rd);
581979e9b03SAlberto Garcia         dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
582979e9b03SAlberto Garcia 
583979e9b03SAlberto Garcia         dev_stats->min_wr_latency_ns = timed_average_min(wr);
584979e9b03SAlberto Garcia         dev_stats->max_wr_latency_ns = timed_average_max(wr);
585979e9b03SAlberto Garcia         dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
586979e9b03SAlberto Garcia 
587979e9b03SAlberto Garcia         dev_stats->min_flush_latency_ns = timed_average_min(fl);
588979e9b03SAlberto Garcia         dev_stats->max_flush_latency_ns = timed_average_max(fl);
589979e9b03SAlberto Garcia         dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
59096e4dedaSAlberto Garcia 
59196e4dedaSAlberto Garcia         dev_stats->avg_rd_queue_depth =
59296e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_READ);
59396e4dedaSAlberto Garcia         dev_stats->avg_wr_queue_depth =
59496e4dedaSAlberto Garcia             block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
59554aa3de7SEric Blake 
59654aa3de7SEric Blake         QAPI_LIST_PREPEND(ds->timed_stats, dev_stats);
597979e9b03SAlberto Garcia     }
5987e5c776dSVladimir Sementsov-Ogievskiy 
59954fde4ffSMarkus Armbruster     hgram = stats->latency_histogram;
60054fde4ffSMarkus Armbruster     ds->rd_latency_histogram
60154fde4ffSMarkus Armbruster         = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_READ]);
60254fde4ffSMarkus Armbruster     ds->wr_latency_histogram
60354fde4ffSMarkus Armbruster         = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_WRITE]);
60454fde4ffSMarkus Armbruster     ds->flush_latency_histogram
60554fde4ffSMarkus Armbruster         = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_FLUSH]);
6067f0e9da6SMax Reitz }
607f364ec65SWenchao Xia 
608d3c8c674SKevin Wolf static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
609d3c8c674SKevin Wolf                                         bool blk_level)
6102b77e60aSKevin Wolf {
6113f26191cSMax Reitz     BdrvChild *parent_child;
6120a7585dbSMax Reitz     BlockDriverState *filter_or_cow_bs;
61320a6d768SDou Liyang     BlockStats *s = NULL;
61420a6d768SDou Liyang 
61520a6d768SDou Liyang     s = g_malloc0(sizeof(*s));
61620a6d768SDou Liyang     s->stats = g_malloc0(sizeof(*s->stats));
61720a6d768SDou Liyang 
61820a6d768SDou Liyang     if (!bs) {
61920a6d768SDou Liyang         return s;
62020a6d768SDou Liyang     }
62120a6d768SDou Liyang 
622d3c8c674SKevin Wolf     /* Skip automatically inserted nodes that the user isn't aware of in
623d3c8c674SKevin Wolf      * a BlockBackend-level command. Stay at the exact node for a node-level
624d3c8c674SKevin Wolf      * command. */
6250a7585dbSMax Reitz     if (blk_level) {
6260a7585dbSMax Reitz         bs = bdrv_skip_implicit_filters(bs);
627d3c8c674SKevin Wolf     }
628d3c8c674SKevin Wolf 
6292b77e60aSKevin Wolf     if (bdrv_get_node_name(bs)[0]) {
6302b77e60aSKevin Wolf         s->node_name = g_strdup(bdrv_get_node_name(bs));
6312b77e60aSKevin Wolf     }
6322b77e60aSKevin Wolf 
633f7946da2SPaolo Bonzini     s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
63453d8f9d8SMax Reitz 
635d9245599SAnton Nefedov     s->driver_specific = bdrv_get_specific_stats(bs);
636d9245599SAnton Nefedov 
6373f26191cSMax Reitz     parent_child = bdrv_primary_child(bs);
6383f26191cSMax Reitz     if (!parent_child ||
6393f26191cSMax Reitz         !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED)))
6403f26191cSMax Reitz     {
6413f26191cSMax Reitz         BdrvChild *c;
6423f26191cSMax Reitz 
6433f26191cSMax Reitz         /*
6443f26191cSMax Reitz          * Look for a unique data-storing child.  We do not need to look for
6453f26191cSMax Reitz          * filtered children, as there would be only one and it would have been
6463f26191cSMax Reitz          * the primary child.
6473f26191cSMax Reitz          */
6483f26191cSMax Reitz         parent_child = NULL;
6493f26191cSMax Reitz         QLIST_FOREACH(c, &bs->children, next) {
6503f26191cSMax Reitz             if (c->role & BDRV_CHILD_DATA) {
6513f26191cSMax Reitz                 if (parent_child) {
6523f26191cSMax Reitz                     /*
6533f26191cSMax Reitz                      * There are multiple data-storing children and we cannot
6543f26191cSMax Reitz                      * choose between them.
6553f26191cSMax Reitz                      */
6563f26191cSMax Reitz                     parent_child = NULL;
6573f26191cSMax Reitz                     break;
6583f26191cSMax Reitz                 }
6593f26191cSMax Reitz                 parent_child = c;
6603f26191cSMax Reitz             }
6613f26191cSMax Reitz         }
6623f26191cSMax Reitz     }
6633f26191cSMax Reitz     if (parent_child) {
6643f26191cSMax Reitz         s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level);
665f364ec65SWenchao Xia     }
666f364ec65SWenchao Xia 
6670a7585dbSMax Reitz     filter_or_cow_bs = bdrv_filter_or_cow_bs(bs);
6680a7585dbSMax Reitz     if (blk_level && filter_or_cow_bs) {
6690a7585dbSMax Reitz         /*
6700a7585dbSMax Reitz          * Put any filtered or COW child here (for backwards
6710a7585dbSMax Reitz          * compatibility to when we put bs0->backing here, which might
6720a7585dbSMax Reitz          * be either)
6730a7585dbSMax Reitz          */
6740a7585dbSMax Reitz         s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level);
675c8059b97SFam Zheng     }
676c8059b97SFam Zheng 
67720a6d768SDou Liyang     return s;
678b07363a1SKevin Wolf }
679b07363a1SKevin Wolf 
680f364ec65SWenchao Xia BlockInfoList *qmp_query_block(Error **errp)
681f364ec65SWenchao Xia {
682f364ec65SWenchao Xia     BlockInfoList *head = NULL, **p_next = &head;
683d829a211SMarkus Armbruster     BlockBackend *blk;
684553a7e87SWenchao Xia     Error *local_err = NULL;
685f364ec65SWenchao Xia 
686d5b68844SKevin Wolf     for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
687d5b68844SKevin Wolf         BlockInfoList *info;
688d5b68844SKevin Wolf 
689ec18b0a9SKevin Wolf         if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
690d5b68844SKevin Wolf             continue;
691d5b68844SKevin Wolf         }
692d5b68844SKevin Wolf 
693d5b68844SKevin Wolf         info = g_malloc0(sizeof(*info));
694d829a211SMarkus Armbruster         bdrv_query_info(blk, &info->value, &local_err);
69584d18f06SMarkus Armbruster         if (local_err) {
696553a7e87SWenchao Xia             error_propagate(errp, local_err);
697903c341dSMarkus Armbruster             g_free(info);
698903c341dSMarkus Armbruster             qapi_free_BlockInfoList(head);
699903c341dSMarkus Armbruster             return NULL;
700553a7e87SWenchao Xia         }
701f364ec65SWenchao Xia 
702f364ec65SWenchao Xia         *p_next = info;
703f364ec65SWenchao Xia         p_next = &info->next;
704f364ec65SWenchao Xia     }
705f364ec65SWenchao Xia 
706f364ec65SWenchao Xia     return head;
707f364ec65SWenchao Xia }
708f364ec65SWenchao Xia 
709f71eaa74SFam Zheng BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
710f71eaa74SFam Zheng                                      bool query_nodes,
711f71eaa74SFam Zheng                                      Error **errp)
712f364ec65SWenchao Xia {
713c3033fd3SEric Blake     BlockStatsList *head = NULL, **tail = &head;
714a6baa608SDou Liyang     BlockBackend *blk;
715a6baa608SDou Liyang     BlockDriverState *bs;
716f364ec65SWenchao Xia 
717f71eaa74SFam Zheng     /* Just to be safe if query_nodes is not always initialized */
718a6baa608SDou Liyang     if (has_query_nodes && query_nodes) {
719a6baa608SDou Liyang         for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
720a6baa608SDou Liyang             AioContext *ctx = bdrv_get_aio_context(bs);
72113344f3aSStefan Hajnoczi 
72213344f3aSStefan Hajnoczi             aio_context_acquire(ctx);
723c3033fd3SEric Blake             QAPI_LIST_APPEND(tail, bdrv_query_bds_stats(bs, false));
72413344f3aSStefan Hajnoczi             aio_context_release(ctx);
725f364ec65SWenchao Xia         }
726a6baa608SDou Liyang     } else {
727567dcb31SKevin Wolf         for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
728a6baa608SDou Liyang             AioContext *ctx = blk_get_aio_context(blk);
729a6baa608SDou Liyang             BlockStats *s;
7305a9cb5a9SKevin Wolf             char *qdev;
731a6baa608SDou Liyang 
732567dcb31SKevin Wolf             if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
733567dcb31SKevin Wolf                 continue;
734567dcb31SKevin Wolf             }
735567dcb31SKevin Wolf 
736a6baa608SDou Liyang             aio_context_acquire(ctx);
737a6baa608SDou Liyang             s = bdrv_query_bds_stats(blk_bs(blk), true);
738a6baa608SDou Liyang             s->device = g_strdup(blk_name(blk));
7395a9cb5a9SKevin Wolf 
7405a9cb5a9SKevin Wolf             qdev = blk_get_attached_dev_id(blk);
7415a9cb5a9SKevin Wolf             if (qdev && *qdev) {
7425a9cb5a9SKevin Wolf                 s->qdev = qdev;
7435a9cb5a9SKevin Wolf             } else {
7445a9cb5a9SKevin Wolf                 g_free(qdev);
7455a9cb5a9SKevin Wolf             }
7465a9cb5a9SKevin Wolf 
747a6baa608SDou Liyang             bdrv_query_blk_stats(s->stats, blk);
748a6baa608SDou Liyang             aio_context_release(ctx);
749a6baa608SDou Liyang 
750c3033fd3SEric Blake             QAPI_LIST_APPEND(tail, s);
751a6baa608SDou Liyang         }
752a6baa608SDou Liyang     }
753f364ec65SWenchao Xia 
754f364ec65SWenchao Xia     return head;
755f364ec65SWenchao Xia }
756f364ec65SWenchao Xia 
757e1ce7d74SMarkus Armbruster void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
758f364ec65SWenchao Xia {
75939683553SDaniel P. Berrangé     char clock_buf[128];
760b39847a5SPavel Dovgalyuk     char icount_buf[128] = {0};
761f364ec65SWenchao Xia     int64_t secs;
762de38b500SEric Blake     char *sizing = NULL;
763f364ec65SWenchao Xia 
764f364ec65SWenchao Xia     if (!sn) {
76526513a01SKevin Wolf         qemu_printf("%-10s%-17s%8s%20s%13s%11s",
766b39847a5SPavel Dovgalyuk                     "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT");
767f364ec65SWenchao Xia     } else {
76839683553SDaniel P. Berrangé         g_autoptr(GDateTime) date = g_date_time_new_from_unix_local(sn->date_sec);
76939683553SDaniel P. Berrangé         g_autofree char *date_buf = g_date_time_format(date, "%Y-%m-%d %H:%M:%S");
77039683553SDaniel P. Berrangé 
771f364ec65SWenchao Xia         secs = sn->vm_clock_nsec / 1000000000;
772f364ec65SWenchao Xia         snprintf(clock_buf, sizeof(clock_buf),
773f364ec65SWenchao Xia                  "%02d:%02d:%02d.%03d",
774f364ec65SWenchao Xia                  (int)(secs / 3600),
775f364ec65SWenchao Xia                  (int)((secs / 60) % 60),
776f364ec65SWenchao Xia                  (int)(secs % 60),
777f364ec65SWenchao Xia                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
778de38b500SEric Blake         sizing = size_to_str(sn->vm_state_size);
779b39847a5SPavel Dovgalyuk         if (sn->icount != -1ULL) {
780b39847a5SPavel Dovgalyuk             snprintf(icount_buf, sizeof(icount_buf),
781b39847a5SPavel Dovgalyuk                 "%"PRId64, sn->icount);
782b39847a5SPavel Dovgalyuk         }
78326513a01SKevin Wolf         qemu_printf("%-9s %-16s %8s%20s%13s%11s",
784f364ec65SWenchao Xia                     sn->id_str, sn->name,
785de38b500SEric Blake                     sizing,
786f364ec65SWenchao Xia                     date_buf,
787b39847a5SPavel Dovgalyuk                     clock_buf,
788b39847a5SPavel Dovgalyuk                     icount_buf);
789f364ec65SWenchao Xia     }
790de38b500SEric Blake     g_free(sizing);
791f364ec65SWenchao Xia }
792f364ec65SWenchao Xia 
793e1ce7d74SMarkus Armbruster static void dump_qdict(int indentation, QDict *dict);
794e1ce7d74SMarkus Armbruster static void dump_qlist(int indentation, QList *list);
795a8d8ecb7SMax Reitz 
796e1ce7d74SMarkus Armbruster static void dump_qobject(int comp_indent, QObject *obj)
797a8d8ecb7SMax Reitz {
798a8d8ecb7SMax Reitz     switch (qobject_type(obj)) {
79901b2ffceSMarc-André Lureau         case QTYPE_QNUM: {
8007dc847ebSMax Reitz             QNum *value = qobject_to(QNum, obj);
80101b2ffceSMarc-André Lureau             char *tmp = qnum_to_string(value);
802e1ce7d74SMarkus Armbruster             qemu_printf("%s", tmp);
80301b2ffceSMarc-André Lureau             g_free(tmp);
804a8d8ecb7SMax Reitz             break;
805a8d8ecb7SMax Reitz         }
806a8d8ecb7SMax Reitz         case QTYPE_QSTRING: {
8077dc847ebSMax Reitz             QString *value = qobject_to(QString, obj);
808e1ce7d74SMarkus Armbruster             qemu_printf("%s", qstring_get_str(value));
809a8d8ecb7SMax Reitz             break;
810a8d8ecb7SMax Reitz         }
811a8d8ecb7SMax Reitz         case QTYPE_QDICT: {
8127dc847ebSMax Reitz             QDict *value = qobject_to(QDict, obj);
813e1ce7d74SMarkus Armbruster             dump_qdict(comp_indent, value);
814a8d8ecb7SMax Reitz             break;
815a8d8ecb7SMax Reitz         }
816a8d8ecb7SMax Reitz         case QTYPE_QLIST: {
8177dc847ebSMax Reitz             QList *value = qobject_to(QList, obj);
818e1ce7d74SMarkus Armbruster             dump_qlist(comp_indent, value);
819a8d8ecb7SMax Reitz             break;
820a8d8ecb7SMax Reitz         }
821a8d8ecb7SMax Reitz         case QTYPE_QBOOL: {
8227dc847ebSMax Reitz             QBool *value = qobject_to(QBool, obj);
823e1ce7d74SMarkus Armbruster             qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
824a8d8ecb7SMax Reitz             break;
825a8d8ecb7SMax Reitz         }
826a8d8ecb7SMax Reitz         default:
827a8d8ecb7SMax Reitz             abort();
828a8d8ecb7SMax Reitz     }
829a8d8ecb7SMax Reitz }
830a8d8ecb7SMax Reitz 
831e1ce7d74SMarkus Armbruster static void dump_qlist(int indentation, QList *list)
832a8d8ecb7SMax Reitz {
833a8d8ecb7SMax Reitz     const QListEntry *entry;
834a8d8ecb7SMax Reitz     int i = 0;
835a8d8ecb7SMax Reitz 
836a8d8ecb7SMax Reitz     for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
8371310a3d3SEric Blake         QType type = qobject_type(entry->value);
838a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
839e1ce7d74SMarkus Armbruster         qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
840853ccfedSPeter Xu                     composite ? '\n' : ' ');
841e1ce7d74SMarkus Armbruster         dump_qobject(indentation + 1, entry->value);
842a8d8ecb7SMax Reitz         if (!composite) {
843e1ce7d74SMarkus Armbruster             qemu_printf("\n");
844a8d8ecb7SMax Reitz         }
845a8d8ecb7SMax Reitz     }
846a8d8ecb7SMax Reitz }
847a8d8ecb7SMax Reitz 
848e1ce7d74SMarkus Armbruster static void dump_qdict(int indentation, QDict *dict)
849a8d8ecb7SMax Reitz {
850a8d8ecb7SMax Reitz     const QDictEntry *entry;
851a8d8ecb7SMax Reitz 
852a8d8ecb7SMax Reitz     for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
8531310a3d3SEric Blake         QType type = qobject_type(entry->value);
854a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
8555eda6227SPeter Xu         char *key = g_malloc(strlen(entry->key) + 1);
856a8d8ecb7SMax Reitz         int i;
857a8d8ecb7SMax Reitz 
858a8d8ecb7SMax Reitz         /* replace dashes with spaces in key (variable) names */
859a8d8ecb7SMax Reitz         for (i = 0; entry->key[i]; i++) {
860a8d8ecb7SMax Reitz             key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
861a8d8ecb7SMax Reitz         }
862a8d8ecb7SMax Reitz         key[i] = 0;
863e1ce7d74SMarkus Armbruster         qemu_printf("%*s%s:%c", indentation * 4, "", key,
864853ccfedSPeter Xu                     composite ? '\n' : ' ');
865e1ce7d74SMarkus Armbruster         dump_qobject(indentation + 1, entry->value);
866a8d8ecb7SMax Reitz         if (!composite) {
867e1ce7d74SMarkus Armbruster             qemu_printf("\n");
868a8d8ecb7SMax Reitz         }
8695eda6227SPeter Xu         g_free(key);
870a8d8ecb7SMax Reitz     }
871a8d8ecb7SMax Reitz }
872a8d8ecb7SMax Reitz 
8733716470bSHanna Reitz /*
8743716470bSHanna Reitz  * Return whether dumping the given QObject with dump_qobject() would
8753716470bSHanna Reitz  * yield an empty dump, i.e. not print anything.
8763716470bSHanna Reitz  */
8773716470bSHanna Reitz static bool qobject_is_empty_dump(const QObject *obj)
8783716470bSHanna Reitz {
8793716470bSHanna Reitz     switch (qobject_type(obj)) {
8803716470bSHanna Reitz     case QTYPE_QNUM:
8813716470bSHanna Reitz     case QTYPE_QSTRING:
8823716470bSHanna Reitz     case QTYPE_QBOOL:
8833716470bSHanna Reitz         return false;
8843716470bSHanna Reitz 
8853716470bSHanna Reitz     case QTYPE_QDICT:
8863716470bSHanna Reitz         return qdict_size(qobject_to(QDict, obj)) == 0;
8873716470bSHanna Reitz 
8883716470bSHanna Reitz     case QTYPE_QLIST:
8893716470bSHanna Reitz         return qlist_empty(qobject_to(QList, obj));
8903716470bSHanna Reitz 
8913716470bSHanna Reitz     default:
8923716470bSHanna Reitz         abort();
8933716470bSHanna Reitz     }
8943716470bSHanna Reitz }
8953716470bSHanna Reitz 
8963716470bSHanna Reitz /**
8973716470bSHanna Reitz  * Dumps the given ImageInfoSpecific object in a human-readable form,
8983716470bSHanna Reitz  * prepending an optional prefix if the dump is not empty.
8993716470bSHanna Reitz  */
9003716470bSHanna Reitz void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec,
90176c9e975SHanna Reitz                                    const char *prefix,
90276c9e975SHanna Reitz                                    int indentation)
903a8d8ecb7SMax Reitz {
904a8d8ecb7SMax Reitz     QObject *obj, *data;
9057d5e199aSDaniel P. Berrange     Visitor *v = qobject_output_visitor_new(&obj);
906a8d8ecb7SMax Reitz 
9073b098d56SEric Blake     visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
9083b098d56SEric Blake     visit_complete(v, &obj);
9097dc847ebSMax Reitz     data = qdict_get(qobject_to(QDict, obj), "data");
9103716470bSHanna Reitz     if (!qobject_is_empty_dump(data)) {
9113716470bSHanna Reitz         if (prefix) {
91276c9e975SHanna Reitz             qemu_printf("%*s%s", indentation * 4, "", prefix);
9133716470bSHanna Reitz         }
91476c9e975SHanna Reitz         dump_qobject(indentation + 1, data);
9153716470bSHanna Reitz     }
916cb3e7f08SMarc-André Lureau     qobject_unref(obj);
9173b098d56SEric Blake     visit_free(v);
918a8d8ecb7SMax Reitz }
919a8d8ecb7SMax Reitz 
920d570177bSHanna Reitz /**
921d570177bSHanna Reitz  * Print the given @info object in human-readable form.  Every field is indented
922d570177bSHanna Reitz  * using the given @indentation (four spaces per indentation level).
923d570177bSHanna Reitz  *
924d570177bSHanna Reitz  * When using this to print a whole block graph, @protocol can be set to true to
925d570177bSHanna Reitz  * signify that the given information is associated with a protocol node, i.e.
926d570177bSHanna Reitz  * just data storage for an image, such that the data it presents is not really
927d570177bSHanna Reitz  * a full VM disk.  If so, several fields change name: For example, "virtual
928d570177bSHanna Reitz  * size" is printed as "file length".
929d570177bSHanna Reitz  * (Consider a qcow2 image, which is represented by a qcow2 node and a file
930d570177bSHanna Reitz  * node.  Printing a "virtual size" for the file node does not make sense,
931d570177bSHanna Reitz  * because without the qcow2 node, it is not really a guest disk, so it does not
932d570177bSHanna Reitz  * have a "virtual size".  Therefore, we call it "file length" instead.)
933d570177bSHanna Reitz  *
934d570177bSHanna Reitz  * @protocol is ignored when @indentation is 0, because we take that to mean
935d570177bSHanna Reitz  * that the associated node is the root node in the queried block graph, and
936d570177bSHanna Reitz  * thus is always to be interpreted as a standalone guest disk.
937d570177bSHanna Reitz  */
938d570177bSHanna Reitz void bdrv_node_info_dump(BlockNodeInfo *info, int indentation, bool protocol)
939f364ec65SWenchao Xia {
940de38b500SEric Blake     char *size_buf, *dsize_buf;
94176c9e975SHanna Reitz     g_autofree char *ind_s = g_strdup_printf("%*s", indentation * 4, "");
94276c9e975SHanna Reitz 
943d570177bSHanna Reitz     if (indentation == 0) {
944d570177bSHanna Reitz         /* Top level, consider this a normal image */
945d570177bSHanna Reitz         protocol = false;
946d570177bSHanna Reitz     }
947d570177bSHanna Reitz 
948f364ec65SWenchao Xia     if (!info->has_actual_size) {
949de38b500SEric Blake         dsize_buf = g_strdup("unavailable");
950f364ec65SWenchao Xia     } else {
951de38b500SEric Blake         dsize_buf = size_to_str(info->actual_size);
952f364ec65SWenchao Xia     }
953de38b500SEric Blake     size_buf = size_to_str(info->virtual_size);
954d570177bSHanna Reitz     qemu_printf("%s%s: %s\n"
955d570177bSHanna Reitz                 "%s%s: %s\n"
956d570177bSHanna Reitz                 "%s%s: %s (%" PRId64 " bytes)\n"
95776c9e975SHanna Reitz                 "%sdisk size: %s\n",
958d570177bSHanna Reitz                 ind_s, protocol ? "filename" : "image", info->filename,
959d570177bSHanna Reitz                 ind_s, protocol ? "protocol type" : "file format",
960d570177bSHanna Reitz                 info->format,
961d570177bSHanna Reitz                 ind_s, protocol ? "file length" : "virtual size",
962d570177bSHanna Reitz                 size_buf, info->virtual_size,
96376c9e975SHanna Reitz                 ind_s, dsize_buf);
964de38b500SEric Blake     g_free(size_buf);
965de38b500SEric Blake     g_free(dsize_buf);
966f364ec65SWenchao Xia 
967f364ec65SWenchao Xia     if (info->has_encrypted && info->encrypted) {
96876c9e975SHanna Reitz         qemu_printf("%sencrypted: yes\n", ind_s);
969f364ec65SWenchao Xia     }
970f364ec65SWenchao Xia 
971f364ec65SWenchao Xia     if (info->has_cluster_size) {
97276c9e975SHanna Reitz         qemu_printf("%scluster_size: %" PRId64 "\n",
97376c9e975SHanna Reitz                     ind_s, info->cluster_size);
974f364ec65SWenchao Xia     }
975f364ec65SWenchao Xia 
976f364ec65SWenchao Xia     if (info->has_dirty_flag && info->dirty_flag) {
97776c9e975SHanna Reitz         qemu_printf("%scleanly shut down: no\n", ind_s);
978f364ec65SWenchao Xia     }
979f364ec65SWenchao Xia 
98054fde4ffSMarkus Armbruster     if (info->backing_filename) {
98176c9e975SHanna Reitz         qemu_printf("%sbacking file: %s", ind_s, info->backing_filename);
98254fde4ffSMarkus Armbruster         if (!info->full_backing_filename) {
983e1ce7d74SMarkus Armbruster             qemu_printf(" (cannot determine actual path)");
9845c9d9ca5SJohn Snow         } else if (strcmp(info->backing_filename,
9855c9d9ca5SJohn Snow                           info->full_backing_filename) != 0) {
986e1ce7d74SMarkus Armbruster             qemu_printf(" (actual path: %s)", info->full_backing_filename);
987f364ec65SWenchao Xia         }
988e1ce7d74SMarkus Armbruster         qemu_printf("\n");
98954fde4ffSMarkus Armbruster         if (info->backing_filename_format) {
99076c9e975SHanna Reitz             qemu_printf("%sbacking file format: %s\n",
99176c9e975SHanna Reitz                         ind_s, info->backing_filename_format);
992f364ec65SWenchao Xia         }
993f364ec65SWenchao Xia     }
994f364ec65SWenchao Xia 
995f364ec65SWenchao Xia     if (info->has_snapshots) {
996f364ec65SWenchao Xia         SnapshotInfoList *elem;
997f364ec65SWenchao Xia 
99876c9e975SHanna Reitz         qemu_printf("%sSnapshot list:\n", ind_s);
99976c9e975SHanna Reitz         qemu_printf("%s", ind_s);
1000e1ce7d74SMarkus Armbruster         bdrv_snapshot_dump(NULL);
1001e1ce7d74SMarkus Armbruster         qemu_printf("\n");
1002f364ec65SWenchao Xia 
1003f364ec65SWenchao Xia         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
1004f364ec65SWenchao Xia          * we convert to the block layer's native QEMUSnapshotInfo for now.
1005f364ec65SWenchao Xia          */
1006f364ec65SWenchao Xia         for (elem = info->snapshots; elem; elem = elem->next) {
1007f364ec65SWenchao Xia             QEMUSnapshotInfo sn = {
1008f364ec65SWenchao Xia                 .vm_state_size = elem->value->vm_state_size,
1009f364ec65SWenchao Xia                 .date_sec = elem->value->date_sec,
1010f364ec65SWenchao Xia                 .date_nsec = elem->value->date_nsec,
1011f364ec65SWenchao Xia                 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
1012f364ec65SWenchao Xia                                  elem->value->vm_clock_nsec,
1013b39847a5SPavel Dovgalyuk                 .icount = elem->value->has_icount ?
1014b39847a5SPavel Dovgalyuk                           elem->value->icount : -1ULL,
1015f364ec65SWenchao Xia             };
1016f364ec65SWenchao Xia 
1017f364ec65SWenchao Xia             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
1018f364ec65SWenchao Xia             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
101976c9e975SHanna Reitz             qemu_printf("%s", ind_s);
1020e1ce7d74SMarkus Armbruster             bdrv_snapshot_dump(&sn);
1021e1ce7d74SMarkus Armbruster             qemu_printf("\n");
1022f364ec65SWenchao Xia         }
1023f364ec65SWenchao Xia     }
1024a8d8ecb7SMax Reitz 
102554fde4ffSMarkus Armbruster     if (info->format_specific) {
10263716470bSHanna Reitz         bdrv_image_info_specific_dump(info->format_specific,
102776c9e975SHanna Reitz                                       "Format specific information:\n",
102876c9e975SHanna Reitz                                       indentation);
1029a8d8ecb7SMax Reitz     }
1030f364ec65SWenchao Xia }
1031