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"
43f364ec65SWenchao Xia
bdrv_block_device_info(BlockBackend * blk,BlockDriverState * bs,bool flat,Error ** errp)44c83f9fbaSKevin Wolf BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
45facda544SPeter Krempa BlockDriverState *bs,
46facda544SPeter Krempa bool flat,
47facda544SPeter Krempa Error **errp)
48c13163fbSBenoît Canet {
4946ff64a8SZhao Liu ERRP_GUARD();
50d5a8ee60SAlberto Garcia ImageInfo **p_image_info;
515d881359SHanna Reitz ImageInfo *backing_info;
52c3b29ae6SFabiano Rosas BlockDriverState *backing;
53d470ad42SMax Reitz BlockDeviceInfo *info;
54c13163fbSBenoît Canet
55d470ad42SMax Reitz if (!bs->drv) {
56d470ad42SMax Reitz error_setg(errp, "Block device %s is ejected", bs->node_name);
57d470ad42SMax Reitz return NULL;
58d470ad42SMax Reitz }
59d470ad42SMax Reitz
60f30c66baSMax Reitz bdrv_refresh_filename(bs);
61f30c66baSMax Reitz
62d470ad42SMax Reitz info = g_malloc0(sizeof(*info));
63c13163fbSBenoît Canet info->file = g_strdup(bs->filename);
64307261b2SVladimir Sementsov-Ogievskiy info->ro = bdrv_is_read_only(bs);
65c13163fbSBenoît Canet info->drv = g_strdup(bs->drv->format_name);
66c13163fbSBenoît Canet info->encrypted = bs->encrypted;
67c13163fbSBenoît Canet
689e193c5aSKevin Wolf info->cache = g_new(BlockdevCacheInfo, 1);
699e193c5aSKevin Wolf *info->cache = (BlockdevCacheInfo) {
70c83f9fbaSKevin Wolf .writeback = blk ? blk_enable_write_cache(blk) : true,
719e193c5aSKevin Wolf .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
729e193c5aSKevin Wolf .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
739e193c5aSKevin Wolf };
749e193c5aSKevin Wolf
75c13163fbSBenoît Canet if (bs->node_name[0]) {
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) {
810b877d09SMax Reitz info->backing_file = g_strdup(backing->filename);
82c13163fbSBenoît Canet }
83c13163fbSBenoît Canet
84590a63d5SVladimir Sementsov-Ogievskiy if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
85590a63d5SVladimir Sementsov-Ogievskiy info->has_dirty_bitmaps = true;
86590a63d5SVladimir Sementsov-Ogievskiy info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
87590a63d5SVladimir Sementsov-Ogievskiy }
88590a63d5SVladimir Sementsov-Ogievskiy
89465bee1dSPeter Lieven info->detect_zeroes = bs->detect_zeroes;
90c13163fbSBenoît Canet
91022cdc9fSManos Pitsidianakis if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
92c13163fbSBenoît Canet ThrottleConfig cfg;
93022cdc9fSManos Pitsidianakis BlockBackendPublic *blkp = blk_get_public(blk);
9476f4afb4SAlberto Garcia
95022cdc9fSManos Pitsidianakis throttle_group_get_config(&blkp->throttle_group_member, &cfg);
9676f4afb4SAlberto Garcia
97c13163fbSBenoît Canet info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
98c13163fbSBenoît Canet info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg;
99c13163fbSBenoît Canet info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg;
100c13163fbSBenoît Canet
101c13163fbSBenoît Canet info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
102c13163fbSBenoît Canet info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
103c13163fbSBenoît Canet info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
104c13163fbSBenoît Canet
105c13163fbSBenoît Canet info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
106c13163fbSBenoît Canet info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
107c13163fbSBenoît Canet info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
108c13163fbSBenoît Canet info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
109c13163fbSBenoît Canet info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
110c13163fbSBenoît Canet info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
111c13163fbSBenoît Canet
112c13163fbSBenoît Canet info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
113c13163fbSBenoît Canet info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
114c13163fbSBenoît Canet info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
115c13163fbSBenoît Canet info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
116c13163fbSBenoît Canet info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
117c13163fbSBenoît Canet info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
118c13163fbSBenoît Canet
119398befdfSAlberto Garcia info->has_bps_max_length = info->has_bps_max;
120398befdfSAlberto Garcia info->bps_max_length =
121398befdfSAlberto Garcia cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
122398befdfSAlberto Garcia info->has_bps_rd_max_length = info->has_bps_rd_max;
123398befdfSAlberto Garcia info->bps_rd_max_length =
124398befdfSAlberto Garcia cfg.buckets[THROTTLE_BPS_READ].burst_length;
125398befdfSAlberto Garcia info->has_bps_wr_max_length = info->has_bps_wr_max;
126398befdfSAlberto Garcia info->bps_wr_max_length =
127398befdfSAlberto Garcia cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
128398befdfSAlberto Garcia
129398befdfSAlberto Garcia info->has_iops_max_length = info->has_iops_max;
130398befdfSAlberto Garcia info->iops_max_length =
131398befdfSAlberto Garcia cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
132398befdfSAlberto Garcia info->has_iops_rd_max_length = info->has_iops_rd_max;
133398befdfSAlberto Garcia info->iops_rd_max_length =
134398befdfSAlberto Garcia cfg.buckets[THROTTLE_OPS_READ].burst_length;
135398befdfSAlberto Garcia info->has_iops_wr_max_length = info->has_iops_wr_max;
136398befdfSAlberto Garcia info->iops_wr_max_length =
137398befdfSAlberto Garcia cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
138398befdfSAlberto Garcia
139c13163fbSBenoît Canet info->has_iops_size = cfg.op_size;
140c13163fbSBenoît Canet info->iops_size = cfg.op_size;
141b8fe1694SAlberto Garcia
142022cdc9fSManos Pitsidianakis info->group =
143022cdc9fSManos Pitsidianakis g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
144c13163fbSBenoît Canet }
145c13163fbSBenoît Canet
146e2462113SFrancesco Romani info->write_threshold = bdrv_write_threshold_get(bs);
147e2462113SFrancesco Romani
148d5a8ee60SAlberto Garcia p_image_info = &info->image;
149d3c8c674SKevin Wolf info->backing_file_depth = 0;
1505d881359SHanna Reitz
1515d881359SHanna Reitz /*
1525d881359SHanna Reitz * Skip automatically inserted nodes that the user isn't aware of for
1535d881359SHanna Reitz * query-block (blk != NULL), but not for query-named-block-nodes
1545d881359SHanna Reitz */
155c3b29ae6SFabiano Rosas bdrv_query_image_info(bs, p_image_info, flat, blk != NULL, errp);
1565d881359SHanna Reitz if (*errp) {
157d5a8ee60SAlberto Garcia qapi_free_BlockDeviceInfo(info);
158d5a8ee60SAlberto Garcia return NULL;
159d5a8ee60SAlberto Garcia }
160d3c8c674SKevin Wolf
1615d881359SHanna Reitz backing_info = info->image->backing_image;
1625d881359SHanna Reitz while (backing_info) {
163d3c8c674SKevin Wolf info->backing_file_depth++;
1645d881359SHanna Reitz backing_info = backing_info->backing_image;
165d5a8ee60SAlberto Garcia }
166d5a8ee60SAlberto Garcia
167c13163fbSBenoît Canet return info;
168c13163fbSBenoît Canet }
169c13163fbSBenoît Canet
170fb0ed453SWenchao Xia /*
171fb0ed453SWenchao Xia * Returns 0 on success, with *p_list either set to describe snapshot
172fb0ed453SWenchao Xia * information, or NULL because there are no snapshots. Returns -errno on
173fb0ed453SWenchao Xia * error, with *p_list untouched.
174fb0ed453SWenchao Xia */
bdrv_query_snapshot_info_list(BlockDriverState * bs,SnapshotInfoList ** p_list,Error ** errp)175fb0ed453SWenchao Xia int bdrv_query_snapshot_info_list(BlockDriverState *bs,
176fb0ed453SWenchao Xia SnapshotInfoList **p_list,
177fb0ed453SWenchao Xia Error **errp)
178f364ec65SWenchao Xia {
179f364ec65SWenchao Xia int i, sn_count;
180f364ec65SWenchao Xia QEMUSnapshotInfo *sn_tab = NULL;
18195b3a8c8SEric Blake SnapshotInfoList *head = NULL, **tail = &head;
182fb0ed453SWenchao Xia SnapshotInfo *info;
183fb0ed453SWenchao Xia
184f364ec65SWenchao Xia sn_count = bdrv_snapshot_list(bs, &sn_tab);
185fb0ed453SWenchao Xia if (sn_count < 0) {
186fb0ed453SWenchao Xia const char *dev = bdrv_get_device_name(bs);
187fb0ed453SWenchao Xia switch (sn_count) {
188fb0ed453SWenchao Xia case -ENOMEDIUM:
189fb0ed453SWenchao Xia error_setg(errp, "Device '%s' is not inserted", dev);
190fb0ed453SWenchao Xia break;
191fb0ed453SWenchao Xia case -ENOTSUP:
192fb0ed453SWenchao Xia error_setg(errp,
193fb0ed453SWenchao Xia "Device '%s' does not support internal snapshots",
194fb0ed453SWenchao Xia dev);
195fb0ed453SWenchao Xia break;
196fb0ed453SWenchao Xia default:
197fb0ed453SWenchao Xia error_setg_errno(errp, -sn_count,
198fb0ed453SWenchao Xia "Can't list snapshots of device '%s'", dev);
199fb0ed453SWenchao Xia break;
200fb0ed453SWenchao Xia }
201fb0ed453SWenchao Xia return sn_count;
202fb0ed453SWenchao Xia }
203f364ec65SWenchao Xia
204f364ec65SWenchao Xia for (i = 0; i < sn_count; i++) {
205fb0ed453SWenchao Xia info = g_new0(SnapshotInfo, 1);
206fb0ed453SWenchao Xia info->id = g_strdup(sn_tab[i].id_str);
207fb0ed453SWenchao Xia info->name = g_strdup(sn_tab[i].name);
208fb0ed453SWenchao Xia info->vm_state_size = sn_tab[i].vm_state_size;
209fb0ed453SWenchao Xia info->date_sec = sn_tab[i].date_sec;
210fb0ed453SWenchao Xia info->date_nsec = sn_tab[i].date_nsec;
211fb0ed453SWenchao Xia info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
212fb0ed453SWenchao Xia info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
213b39847a5SPavel Dovgalyuk info->icount = sn_tab[i].icount;
214b39847a5SPavel Dovgalyuk info->has_icount = sn_tab[i].icount != -1ULL;
215f364ec65SWenchao Xia
21695b3a8c8SEric Blake QAPI_LIST_APPEND(tail, info);
217f364ec65SWenchao Xia }
218f364ec65SWenchao Xia
219f364ec65SWenchao Xia g_free(sn_tab);
220fb0ed453SWenchao Xia *p_list = head;
221fb0ed453SWenchao Xia return 0;
222f364ec65SWenchao Xia }
223f364ec65SWenchao Xia
22443526ec8SWenchao Xia /**
225a2085f89SHanna Reitz * Helper function for other query info functions. Store information about @bs
226a2085f89SHanna Reitz * in @info, setting @errp on error.
22743526ec8SWenchao Xia */
228b7cfc7d5SKevin Wolf static void GRAPH_RDLOCK
bdrv_do_query_node_info(BlockDriverState * bs,BlockNodeInfo * info,Error ** errp)229b7cfc7d5SKevin Wolf bdrv_do_query_node_info(BlockDriverState *bs, BlockNodeInfo *info, Error **errp)
230f364ec65SWenchao Xia {
23152bf1e72SMarkus Armbruster int64_t size;
23243526ec8SWenchao Xia const char *backing_filename;
233f364ec65SWenchao Xia BlockDriverInfo bdi;
23443526ec8SWenchao Xia int ret;
23543526ec8SWenchao Xia Error *err = NULL;
236f364ec65SWenchao Xia
23752bf1e72SMarkus Armbruster size = bdrv_getlength(bs);
23852bf1e72SMarkus Armbruster if (size < 0) {
2399adceb02SFam Zheng error_setg_errno(errp, -size, "Can't get image size '%s'",
2409adceb02SFam Zheng bs->exact_filename);
241b49f4755SStefan Hajnoczi return;
24252bf1e72SMarkus Armbruster }
243f364ec65SWenchao Xia
244f30c66baSMax Reitz bdrv_refresh_filename(bs);
245f30c66baSMax Reitz
24643526ec8SWenchao Xia info->filename = g_strdup(bs->filename);
247f364ec65SWenchao Xia info->format = g_strdup(bdrv_get_format_name(bs));
24852bf1e72SMarkus Armbruster info->virtual_size = size;
249f364ec65SWenchao Xia info->actual_size = bdrv_get_allocated_file_size(bs);
250f364ec65SWenchao Xia info->has_actual_size = info->actual_size >= 0;
2518b8277cdSMax Reitz if (bs->encrypted) {
252f364ec65SWenchao Xia info->encrypted = true;
253f364ec65SWenchao Xia info->has_encrypted = true;
254f364ec65SWenchao Xia }
255f364ec65SWenchao Xia if (bdrv_get_info(bs, &bdi) >= 0) {
256f364ec65SWenchao Xia if (bdi.cluster_size != 0) {
257f364ec65SWenchao Xia info->cluster_size = bdi.cluster_size;
258f364ec65SWenchao Xia info->has_cluster_size = true;
259f364ec65SWenchao Xia }
260f364ec65SWenchao Xia info->dirty_flag = bdi.is_dirty;
261f364ec65SWenchao Xia info->has_dirty_flag = true;
262f364ec65SWenchao Xia }
2631bf6e9caSAndrey Shinkevich info->format_specific = bdrv_get_specific_info(bs, &err);
2641bf6e9caSAndrey Shinkevich if (err) {
2651bf6e9caSAndrey Shinkevich error_propagate(errp, err);
266b49f4755SStefan Hajnoczi return;
2671bf6e9caSAndrey Shinkevich }
26843526ec8SWenchao Xia backing_filename = bs->backing_file;
269f364ec65SWenchao Xia if (backing_filename[0] != '\0') {
2706b6833c1SMax Reitz char *backing_filename2;
2710b877d09SMax Reitz
272f364ec65SWenchao Xia info->backing_filename = g_strdup(backing_filename);
2736b6833c1SMax Reitz backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
274f364ec65SWenchao Xia
27512dcb1c0SJohn Snow /* Always report the full_backing_filename if present, even if it's the
27612dcb1c0SJohn Snow * same as backing_filename. That they are same is useful info. */
27712dcb1c0SJohn Snow if (backing_filename2) {
27812dcb1c0SJohn Snow info->full_backing_filename = g_strdup(backing_filename2);
279f364ec65SWenchao Xia }
280f364ec65SWenchao Xia
281f364ec65SWenchao Xia if (bs->backing_format[0]) {
282f364ec65SWenchao Xia info->backing_filename_format = g_strdup(bs->backing_format);
283f364ec65SWenchao Xia }
284564d64bdSJeff Cody g_free(backing_filename2);
285f364ec65SWenchao Xia }
28643526ec8SWenchao Xia
28743526ec8SWenchao Xia ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
28843526ec8SWenchao Xia switch (ret) {
28943526ec8SWenchao Xia case 0:
29043526ec8SWenchao Xia if (info->snapshots) {
29143526ec8SWenchao Xia info->has_snapshots = true;
29243526ec8SWenchao Xia }
29343526ec8SWenchao Xia break;
29443526ec8SWenchao Xia /* recoverable error */
29543526ec8SWenchao Xia case -ENOMEDIUM:
29643526ec8SWenchao Xia case -ENOTSUP:
29743526ec8SWenchao Xia error_free(err);
29843526ec8SWenchao Xia break;
29943526ec8SWenchao Xia default:
30043526ec8SWenchao Xia error_propagate(errp, err);
301b49f4755SStefan Hajnoczi return;
30243526ec8SWenchao Xia }
303f364ec65SWenchao Xia }
304f364ec65SWenchao Xia
305a2085f89SHanna Reitz /**
306a2085f89SHanna Reitz * bdrv_query_image_info:
307a2085f89SHanna Reitz * @bs: block node to examine
308a2085f89SHanna Reitz * @p_info: location to store image information
3095d881359SHanna Reitz * @flat: skip backing node information
3105d881359SHanna Reitz * @skip_implicit_filters: skip implicit filters in the backing chain
311a2085f89SHanna Reitz * @errp: location to store error information
312a2085f89SHanna Reitz *
3135d881359SHanna Reitz * Store image information in @p_info, potentially recursively covering the
3145d881359SHanna Reitz * backing chain.
315a2085f89SHanna Reitz *
3165d881359SHanna Reitz * If @flat is true, do not query backing image information, i.e.
3175d881359SHanna Reitz * (*p_info)->has_backing_image will be set to false and
3185d881359SHanna Reitz * (*p_info)->backing_image to NULL even when the image does in fact have a
3195d881359SHanna Reitz * backing image.
3205d881359SHanna Reitz *
3215d881359SHanna Reitz * If @skip_implicit_filters is true, implicit filter nodes in the backing chain
3225d881359SHanna Reitz * will be skipped when querying backing image information.
3235d881359SHanna Reitz * (@skip_implicit_filters is ignored when @flat is true.)
324a2085f89SHanna Reitz *
325a2085f89SHanna Reitz * @p_info will be set only on success. On error, store error in @errp.
326a2085f89SHanna Reitz */
bdrv_query_image_info(BlockDriverState * bs,ImageInfo ** p_info,bool flat,bool skip_implicit_filters,Error ** errp)327a2085f89SHanna Reitz void bdrv_query_image_info(BlockDriverState *bs,
328a2085f89SHanna Reitz ImageInfo **p_info,
3295d881359SHanna Reitz bool flat,
3305d881359SHanna Reitz bool skip_implicit_filters,
331a2085f89SHanna Reitz Error **errp)
332a2085f89SHanna Reitz {
333a2085f89SHanna Reitz ERRP_GUARD();
33446ff64a8SZhao Liu ImageInfo *info;
335a2085f89SHanna Reitz
336a2085f89SHanna Reitz info = g_new0(ImageInfo, 1);
337a2085f89SHanna Reitz bdrv_do_query_node_info(bs, qapi_ImageInfo_base(info), errp);
338a2085f89SHanna Reitz if (*errp) {
3395d881359SHanna Reitz goto fail;
3405d881359SHanna Reitz }
3415d881359SHanna Reitz
3425d881359SHanna Reitz if (!flat) {
3435d881359SHanna Reitz BlockDriverState *backing;
3445d881359SHanna Reitz
3455d881359SHanna Reitz /*
3465d881359SHanna Reitz * Use any filtered child here (for backwards compatibility to when
3475d881359SHanna Reitz * we always took bs->backing, which might be any filtered child).
3485d881359SHanna Reitz */
3495d881359SHanna Reitz backing = bdrv_filter_or_cow_bs(bs);
3505d881359SHanna Reitz if (skip_implicit_filters) {
3515d881359SHanna Reitz backing = bdrv_skip_implicit_filters(backing);
3525d881359SHanna Reitz }
3535d881359SHanna Reitz
3545d881359SHanna Reitz if (backing) {
3555d881359SHanna Reitz bdrv_query_image_info(backing, &info->backing_image, false,
3565d881359SHanna Reitz skip_implicit_filters, errp);
3575d881359SHanna Reitz if (*errp) {
3585d881359SHanna Reitz goto fail;
3595d881359SHanna Reitz }
3605d881359SHanna Reitz }
361a2085f89SHanna Reitz }
362a2085f89SHanna Reitz
363a2085f89SHanna Reitz *p_info = info;
3645d881359SHanna Reitz return;
3655d881359SHanna Reitz
3665d881359SHanna Reitz fail:
3675d881359SHanna Reitz assert(*errp);
3685d881359SHanna Reitz qapi_free_ImageInfo(info);
369a2085f89SHanna Reitz }
370a2085f89SHanna Reitz
3716cab3399SHanna Reitz /**
3726cab3399SHanna Reitz * bdrv_query_block_graph_info:
3736cab3399SHanna Reitz * @bs: root node to start from
3746cab3399SHanna Reitz * @p_info: location to store image information
3756cab3399SHanna Reitz * @errp: location to store error information
3766cab3399SHanna Reitz *
3776cab3399SHanna Reitz * Store image information about the graph starting from @bs in @p_info.
3786cab3399SHanna Reitz *
3796cab3399SHanna Reitz * @p_info will be set only on success. On error, store error in @errp.
3806cab3399SHanna Reitz */
bdrv_query_block_graph_info(BlockDriverState * bs,BlockGraphInfo ** p_info,Error ** errp)3816cab3399SHanna Reitz void bdrv_query_block_graph_info(BlockDriverState *bs,
3826cab3399SHanna Reitz BlockGraphInfo **p_info,
3836cab3399SHanna Reitz Error **errp)
3846cab3399SHanna Reitz {
38546ff64a8SZhao Liu ERRP_GUARD();
3866cab3399SHanna Reitz BlockGraphInfo *info;
3876cab3399SHanna Reitz BlockChildInfoList **children_list_tail;
3886cab3399SHanna Reitz BdrvChild *c;
3896cab3399SHanna Reitz
3906cab3399SHanna Reitz info = g_new0(BlockGraphInfo, 1);
3916cab3399SHanna Reitz bdrv_do_query_node_info(bs, qapi_BlockGraphInfo_base(info), errp);
3926cab3399SHanna Reitz if (*errp) {
3936cab3399SHanna Reitz goto fail;
3946cab3399SHanna Reitz }
3956cab3399SHanna Reitz
3966cab3399SHanna Reitz children_list_tail = &info->children;
3976cab3399SHanna Reitz
3986cab3399SHanna Reitz QLIST_FOREACH(c, &bs->children, next) {
3996cab3399SHanna Reitz BlockChildInfo *c_info;
4006cab3399SHanna Reitz
4016cab3399SHanna Reitz c_info = g_new0(BlockChildInfo, 1);
4026cab3399SHanna Reitz QAPI_LIST_APPEND(children_list_tail, c_info);
4036cab3399SHanna Reitz
4046cab3399SHanna Reitz c_info->name = g_strdup(c->name);
4056cab3399SHanna Reitz bdrv_query_block_graph_info(c->bs, &c_info->info, errp);
4066cab3399SHanna Reitz if (*errp) {
4076cab3399SHanna Reitz goto fail;
4086cab3399SHanna Reitz }
4096cab3399SHanna Reitz }
4106cab3399SHanna Reitz
4116cab3399SHanna Reitz *p_info = info;
4126cab3399SHanna Reitz return;
4136cab3399SHanna Reitz
4146cab3399SHanna Reitz fail:
4156cab3399SHanna Reitz assert(*errp != NULL);
4166cab3399SHanna Reitz qapi_free_BlockGraphInfo(info);
4176cab3399SHanna Reitz }
4186cab3399SHanna Reitz
419553a7e87SWenchao Xia /* @p_info will be set only on success. */
420b7cfc7d5SKevin Wolf static void GRAPH_RDLOCK
bdrv_query_info(BlockBackend * blk,BlockInfo ** p_info,Error ** errp)421b7cfc7d5SKevin Wolf bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, Error **errp)
422f364ec65SWenchao Xia {
423f364ec65SWenchao Xia BlockInfo *info = g_malloc0(sizeof(*info));
424d829a211SMarkus Armbruster BlockDriverState *bs = blk_bs(blk);
42546eade7bSKevin Wolf char *qdev;
42646eade7bSKevin Wolf
427d3c8c674SKevin Wolf /* Skip automatically inserted nodes that the user isn't aware of */
4280a7585dbSMax Reitz bs = bdrv_skip_implicit_filters(bs);
429d3c8c674SKevin Wolf
430d829a211SMarkus Armbruster info->device = g_strdup(blk_name(blk));
431f364ec65SWenchao Xia info->type = g_strdup("unknown");
432a7f53e26SMarkus Armbruster info->locked = blk_dev_is_medium_locked(blk);
433a7f53e26SMarkus Armbruster info->removable = blk_dev_has_removable_media(blk);
434f364ec65SWenchao Xia
43546eade7bSKevin Wolf qdev = blk_get_attached_dev_id(blk);
43646eade7bSKevin Wolf if (qdev && *qdev) {
43746eade7bSKevin Wolf info->qdev = qdev;
43846eade7bSKevin Wolf } else {
43946eade7bSKevin Wolf g_free(qdev);
44046eade7bSKevin Wolf }
44146eade7bSKevin Wolf
442327032ceSMax Reitz if (blk_dev_has_tray(blk)) {
443f364ec65SWenchao Xia info->has_tray_open = true;
444a7f53e26SMarkus Armbruster info->tray_open = blk_dev_is_tray_open(blk);
445f364ec65SWenchao Xia }
446f364ec65SWenchao Xia
447373340b2SMax Reitz if (blk_iostatus_is_enabled(blk)) {
448f364ec65SWenchao Xia info->has_io_status = true;
449373340b2SMax Reitz info->io_status = blk_iostatus(blk);
450f364ec65SWenchao Xia }
451f364ec65SWenchao Xia
4525433c24fSMax Reitz if (bs && bs->drv) {
453facda544SPeter Krempa info->inserted = bdrv_block_device_info(blk, bs, false, errp);
454d5a8ee60SAlberto Garcia if (info->inserted == NULL) {
455553a7e87SWenchao Xia goto err;
456f364ec65SWenchao Xia }
457553a7e87SWenchao Xia }
458553a7e87SWenchao Xia
459553a7e87SWenchao Xia *p_info = info;
460553a7e87SWenchao Xia return;
461553a7e87SWenchao Xia
462553a7e87SWenchao Xia err:
463553a7e87SWenchao Xia qapi_free_BlockInfo(info);
464f364ec65SWenchao Xia }
465f364ec65SWenchao Xia
uint64_list(uint64_t * list,int size)4667e5c776dSVladimir Sementsov-Ogievskiy static uint64List *uint64_list(uint64_t *list, int size)
4677e5c776dSVladimir Sementsov-Ogievskiy {
4687e5c776dSVladimir Sementsov-Ogievskiy int i;
4697e5c776dSVladimir Sementsov-Ogievskiy uint64List *out_list = NULL;
470c3033fd3SEric Blake uint64List **tail = &out_list;
4717e5c776dSVladimir Sementsov-Ogievskiy
4727e5c776dSVladimir Sementsov-Ogievskiy for (i = 0; i < size; i++) {
473c3033fd3SEric Blake QAPI_LIST_APPEND(tail, list[i]);
4747e5c776dSVladimir Sementsov-Ogievskiy }
4757e5c776dSVladimir Sementsov-Ogievskiy
4767e5c776dSVladimir Sementsov-Ogievskiy return out_list;
4777e5c776dSVladimir Sementsov-Ogievskiy }
4787e5c776dSVladimir Sementsov-Ogievskiy
47954fde4ffSMarkus Armbruster static BlockLatencyHistogramInfo *
bdrv_latency_histogram_stats(BlockLatencyHistogram * hist)48054fde4ffSMarkus Armbruster bdrv_latency_histogram_stats(BlockLatencyHistogram *hist)
4817e5c776dSVladimir Sementsov-Ogievskiy {
48254fde4ffSMarkus Armbruster BlockLatencyHistogramInfo *info;
4837e5c776dSVladimir Sementsov-Ogievskiy
48454fde4ffSMarkus Armbruster if (!hist->bins) {
48554fde4ffSMarkus Armbruster return NULL;
4867e5c776dSVladimir Sementsov-Ogievskiy }
48754fde4ffSMarkus Armbruster
48854fde4ffSMarkus Armbruster info = g_new0(BlockLatencyHistogramInfo, 1);
48954fde4ffSMarkus Armbruster info->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
49054fde4ffSMarkus Armbruster info->bins = uint64_list(hist->bins, hist->nbins);
49154fde4ffSMarkus Armbruster return info;
4927e5c776dSVladimir Sementsov-Ogievskiy }
4937e5c776dSVladimir Sementsov-Ogievskiy
bdrv_query_blk_stats(BlockDeviceStats * ds,BlockBackend * blk)49454302156SMax Reitz static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
495f364ec65SWenchao Xia {
4962b77e60aSKevin Wolf BlockAcctStats *stats = blk_get_stats(blk);
497979e9b03SAlberto Garcia BlockAcctTimedStats *ts = NULL;
49854fde4ffSMarkus Armbruster BlockLatencyHistogram *hgram;
4997f0e9da6SMax Reitz
50054302156SMax Reitz ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
50154302156SMax Reitz ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
50252eb76f4SSam Li ds->zone_append_bytes = stats->nr_bytes[BLOCK_ACCT_ZONE_APPEND];
503159f85ddSAnton Nefedov ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
50454302156SMax Reitz ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
50554302156SMax Reitz ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
50652eb76f4SSam Li ds->zone_append_operations = stats->nr_ops[BLOCK_ACCT_ZONE_APPEND];
507159f85ddSAnton Nefedov ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
5087ee12dafSAlberto Garcia
50954302156SMax Reitz ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
51054302156SMax Reitz ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
51152eb76f4SSam Li ds->failed_zone_append_operations =
51252eb76f4SSam Li stats->failed_ops[BLOCK_ACCT_ZONE_APPEND];
51354302156SMax Reitz ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
514159f85ddSAnton Nefedov ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
5157ee12dafSAlberto Garcia
51654302156SMax Reitz ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
51754302156SMax Reitz ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
51852eb76f4SSam Li ds->invalid_zone_append_operations =
51952eb76f4SSam Li stats->invalid_ops[BLOCK_ACCT_ZONE_APPEND];
52054302156SMax Reitz ds->invalid_flush_operations =
5217ee12dafSAlberto Garcia stats->invalid_ops[BLOCK_ACCT_FLUSH];
522159f85ddSAnton Nefedov ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
5237ee12dafSAlberto Garcia
52454302156SMax Reitz ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
52554302156SMax Reitz ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
52652eb76f4SSam Li ds->zone_append_merged = stats->merged[BLOCK_ACCT_ZONE_APPEND];
527159f85ddSAnton Nefedov ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
52854302156SMax Reitz ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
52954302156SMax Reitz ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
53052eb76f4SSam Li ds->zone_append_total_time_ns =
53152eb76f4SSam Li stats->total_time_ns[BLOCK_ACCT_ZONE_APPEND];
53254302156SMax Reitz ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
53354302156SMax Reitz ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
534159f85ddSAnton Nefedov ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
535cb38fffbSAlberto Garcia
53654302156SMax Reitz ds->has_idle_time_ns = stats->last_access_time_ns > 0;
53754302156SMax Reitz if (ds->has_idle_time_ns) {
53854302156SMax Reitz ds->idle_time_ns = block_acct_idle_time_ns(stats);
539cb38fffbSAlberto Garcia }
540362e9299SAlberto Garcia
54154302156SMax Reitz ds->account_invalid = stats->account_invalid;
54254302156SMax Reitz ds->account_failed = stats->account_failed;
543979e9b03SAlberto Garcia
544979e9b03SAlberto Garcia while ((ts = block_acct_interval_next(stats, ts))) {
545979e9b03SAlberto Garcia BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
546979e9b03SAlberto Garcia
547979e9b03SAlberto Garcia TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
548979e9b03SAlberto Garcia TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
54952eb76f4SSam Li TimedAverage *zap = &ts->latency[BLOCK_ACCT_ZONE_APPEND];
550979e9b03SAlberto Garcia TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
551979e9b03SAlberto Garcia
552979e9b03SAlberto Garcia dev_stats->interval_length = ts->interval_length;
553979e9b03SAlberto Garcia
554979e9b03SAlberto Garcia dev_stats->min_rd_latency_ns = timed_average_min(rd);
555979e9b03SAlberto Garcia dev_stats->max_rd_latency_ns = timed_average_max(rd);
556979e9b03SAlberto Garcia dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
557979e9b03SAlberto Garcia
558979e9b03SAlberto Garcia dev_stats->min_wr_latency_ns = timed_average_min(wr);
559979e9b03SAlberto Garcia dev_stats->max_wr_latency_ns = timed_average_max(wr);
560979e9b03SAlberto Garcia dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
561979e9b03SAlberto Garcia
56252eb76f4SSam Li dev_stats->min_zone_append_latency_ns = timed_average_min(zap);
56352eb76f4SSam Li dev_stats->max_zone_append_latency_ns = timed_average_max(zap);
56452eb76f4SSam Li dev_stats->avg_zone_append_latency_ns = timed_average_avg(zap);
56552eb76f4SSam Li
566979e9b03SAlberto Garcia dev_stats->min_flush_latency_ns = timed_average_min(fl);
567979e9b03SAlberto Garcia dev_stats->max_flush_latency_ns = timed_average_max(fl);
568979e9b03SAlberto Garcia dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
56996e4dedaSAlberto Garcia
57096e4dedaSAlberto Garcia dev_stats->avg_rd_queue_depth =
57196e4dedaSAlberto Garcia block_acct_queue_depth(ts, BLOCK_ACCT_READ);
57296e4dedaSAlberto Garcia dev_stats->avg_wr_queue_depth =
57396e4dedaSAlberto Garcia block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
57452eb76f4SSam Li dev_stats->avg_zone_append_queue_depth =
57552eb76f4SSam Li block_acct_queue_depth(ts, BLOCK_ACCT_ZONE_APPEND);
57654aa3de7SEric Blake
57754aa3de7SEric Blake QAPI_LIST_PREPEND(ds->timed_stats, dev_stats);
578979e9b03SAlberto Garcia }
5797e5c776dSVladimir Sementsov-Ogievskiy
58054fde4ffSMarkus Armbruster hgram = stats->latency_histogram;
58154fde4ffSMarkus Armbruster ds->rd_latency_histogram
58254fde4ffSMarkus Armbruster = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_READ]);
58354fde4ffSMarkus Armbruster ds->wr_latency_histogram
58454fde4ffSMarkus Armbruster = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_WRITE]);
58552eb76f4SSam Li ds->zone_append_latency_histogram
58652eb76f4SSam Li = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_ZONE_APPEND]);
58754fde4ffSMarkus Armbruster ds->flush_latency_histogram
58854fde4ffSMarkus Armbruster = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_FLUSH]);
5897f0e9da6SMax Reitz }
590f364ec65SWenchao Xia
5916ec75a6aSKevin Wolf static BlockStats * GRAPH_RDLOCK
bdrv_query_bds_stats(BlockDriverState * bs,bool blk_level)5926ec75a6aSKevin Wolf bdrv_query_bds_stats(BlockDriverState *bs, bool blk_level)
5932b77e60aSKevin Wolf {
5943f26191cSMax Reitz BdrvChild *parent_child;
5950a7585dbSMax Reitz BlockDriverState *filter_or_cow_bs;
59620a6d768SDou Liyang BlockStats *s = NULL;
59720a6d768SDou Liyang
59820a6d768SDou Liyang s = g_malloc0(sizeof(*s));
59920a6d768SDou Liyang s->stats = g_malloc0(sizeof(*s->stats));
60020a6d768SDou Liyang
60120a6d768SDou Liyang if (!bs) {
60220a6d768SDou Liyang return s;
60320a6d768SDou Liyang }
60420a6d768SDou Liyang
605d3c8c674SKevin Wolf /* Skip automatically inserted nodes that the user isn't aware of in
606d3c8c674SKevin Wolf * a BlockBackend-level command. Stay at the exact node for a node-level
607d3c8c674SKevin Wolf * command. */
6080a7585dbSMax Reitz if (blk_level) {
6090a7585dbSMax Reitz bs = bdrv_skip_implicit_filters(bs);
610d3c8c674SKevin Wolf }
611d3c8c674SKevin Wolf
6122b77e60aSKevin Wolf if (bdrv_get_node_name(bs)[0]) {
6132b77e60aSKevin Wolf s->node_name = g_strdup(bdrv_get_node_name(bs));
6142b77e60aSKevin Wolf }
6152b77e60aSKevin Wolf
616f7946da2SPaolo Bonzini s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
61753d8f9d8SMax Reitz
618d9245599SAnton Nefedov s->driver_specific = bdrv_get_specific_stats(bs);
619d9245599SAnton Nefedov
6203f26191cSMax Reitz parent_child = bdrv_primary_child(bs);
6213f26191cSMax Reitz if (!parent_child ||
6223f26191cSMax Reitz !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED)))
6233f26191cSMax Reitz {
6243f26191cSMax Reitz BdrvChild *c;
6253f26191cSMax Reitz
6263f26191cSMax Reitz /*
6273f26191cSMax Reitz * Look for a unique data-storing child. We do not need to look for
6283f26191cSMax Reitz * filtered children, as there would be only one and it would have been
6293f26191cSMax Reitz * the primary child.
6303f26191cSMax Reitz */
6313f26191cSMax Reitz parent_child = NULL;
6323f26191cSMax Reitz QLIST_FOREACH(c, &bs->children, next) {
6333f26191cSMax Reitz if (c->role & BDRV_CHILD_DATA) {
6343f26191cSMax Reitz if (parent_child) {
6353f26191cSMax Reitz /*
6363f26191cSMax Reitz * There are multiple data-storing children and we cannot
6373f26191cSMax Reitz * choose between them.
6383f26191cSMax Reitz */
6393f26191cSMax Reitz parent_child = NULL;
6403f26191cSMax Reitz break;
6413f26191cSMax Reitz }
6423f26191cSMax Reitz parent_child = c;
6433f26191cSMax Reitz }
6443f26191cSMax Reitz }
6453f26191cSMax Reitz }
6463f26191cSMax Reitz if (parent_child) {
6473f26191cSMax Reitz s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level);
648f364ec65SWenchao Xia }
649f364ec65SWenchao Xia
6500a7585dbSMax Reitz filter_or_cow_bs = bdrv_filter_or_cow_bs(bs);
6510a7585dbSMax Reitz if (blk_level && filter_or_cow_bs) {
6520a7585dbSMax Reitz /*
6530a7585dbSMax Reitz * Put any filtered or COW child here (for backwards
6540a7585dbSMax Reitz * compatibility to when we put bs0->backing here, which might
6550a7585dbSMax Reitz * be either)
6560a7585dbSMax Reitz */
6570a7585dbSMax Reitz s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level);
658c8059b97SFam Zheng }
659c8059b97SFam Zheng
66020a6d768SDou Liyang return s;
661b07363a1SKevin Wolf }
662b07363a1SKevin Wolf
qmp_query_block(Error ** errp)663f364ec65SWenchao Xia BlockInfoList *qmp_query_block(Error **errp)
664f364ec65SWenchao Xia {
665f364ec65SWenchao Xia BlockInfoList *head = NULL, **p_next = &head;
666d829a211SMarkus Armbruster BlockBackend *blk;
667553a7e87SWenchao Xia Error *local_err = NULL;
668f364ec65SWenchao Xia
669b7cfc7d5SKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
670b7cfc7d5SKevin Wolf
671d5b68844SKevin Wolf for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
672d5b68844SKevin Wolf BlockInfoList *info;
673d5b68844SKevin Wolf
674ec18b0a9SKevin Wolf if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
675d5b68844SKevin Wolf continue;
676d5b68844SKevin Wolf }
677d5b68844SKevin Wolf
678d5b68844SKevin Wolf info = g_malloc0(sizeof(*info));
679d829a211SMarkus Armbruster bdrv_query_info(blk, &info->value, &local_err);
68084d18f06SMarkus Armbruster if (local_err) {
681553a7e87SWenchao Xia error_propagate(errp, local_err);
682903c341dSMarkus Armbruster g_free(info);
683903c341dSMarkus Armbruster qapi_free_BlockInfoList(head);
684903c341dSMarkus Armbruster return NULL;
685553a7e87SWenchao Xia }
686f364ec65SWenchao Xia
687f364ec65SWenchao Xia *p_next = info;
688f364ec65SWenchao Xia p_next = &info->next;
689f364ec65SWenchao Xia }
690f364ec65SWenchao Xia
691f364ec65SWenchao Xia return head;
692f364ec65SWenchao Xia }
693f364ec65SWenchao Xia
qmp_query_blockstats(bool has_query_nodes,bool query_nodes,Error ** errp)694f71eaa74SFam Zheng BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
695f71eaa74SFam Zheng bool query_nodes,
696f71eaa74SFam Zheng Error **errp)
697f364ec65SWenchao Xia {
698c3033fd3SEric Blake BlockStatsList *head = NULL, **tail = &head;
699a6baa608SDou Liyang BlockBackend *blk;
700a6baa608SDou Liyang BlockDriverState *bs;
701f364ec65SWenchao Xia
7026ec75a6aSKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
7036ec75a6aSKevin Wolf
704f71eaa74SFam Zheng /* Just to be safe if query_nodes is not always initialized */
705a6baa608SDou Liyang if (has_query_nodes && query_nodes) {
706a6baa608SDou Liyang for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
707c3033fd3SEric Blake QAPI_LIST_APPEND(tail, bdrv_query_bds_stats(bs, false));
708f364ec65SWenchao Xia }
709a6baa608SDou Liyang } else {
710567dcb31SKevin Wolf for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
711a6baa608SDou Liyang BlockStats *s;
7125a9cb5a9SKevin Wolf char *qdev;
713a6baa608SDou Liyang
714567dcb31SKevin Wolf if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
715567dcb31SKevin Wolf continue;
716567dcb31SKevin Wolf }
717567dcb31SKevin Wolf
718a6baa608SDou Liyang s = bdrv_query_bds_stats(blk_bs(blk), true);
719a6baa608SDou Liyang s->device = g_strdup(blk_name(blk));
7205a9cb5a9SKevin Wolf
7215a9cb5a9SKevin Wolf qdev = blk_get_attached_dev_id(blk);
7225a9cb5a9SKevin Wolf if (qdev && *qdev) {
7235a9cb5a9SKevin Wolf s->qdev = qdev;
7245a9cb5a9SKevin Wolf } else {
7255a9cb5a9SKevin Wolf g_free(qdev);
7265a9cb5a9SKevin Wolf }
7275a9cb5a9SKevin Wolf
728a6baa608SDou Liyang bdrv_query_blk_stats(s->stats, blk);
729a6baa608SDou Liyang
730c3033fd3SEric Blake QAPI_LIST_APPEND(tail, s);
731a6baa608SDou Liyang }
732a6baa608SDou Liyang }
733f364ec65SWenchao Xia
734f364ec65SWenchao Xia return head;
735f364ec65SWenchao Xia }
736f364ec65SWenchao Xia
bdrv_snapshot_dump(QEMUSnapshotInfo * sn)737e1ce7d74SMarkus Armbruster void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
738f364ec65SWenchao Xia {
73939683553SDaniel P. Berrangé char clock_buf[128];
740b39847a5SPavel Dovgalyuk char icount_buf[128] = {0};
741f364ec65SWenchao Xia int64_t secs;
742de38b500SEric Blake char *sizing = NULL;
743f364ec65SWenchao Xia
744f364ec65SWenchao Xia if (!sn) {
745*52df1a5bSAbhiram Tilak qemu_printf("%-7s %-16s %8s %19s %15s %10s",
746*52df1a5bSAbhiram Tilak "ID", "TAG", "VM_SIZE", "DATE", "VM_CLOCK", "ICOUNT");
747f364ec65SWenchao Xia } else {
74839683553SDaniel P. Berrangé g_autoptr(GDateTime) date = g_date_time_new_from_unix_local(sn->date_sec);
74939683553SDaniel P. Berrangé g_autofree char *date_buf = g_date_time_format(date, "%Y-%m-%d %H:%M:%S");
75039683553SDaniel P. Berrangé
751f364ec65SWenchao Xia secs = sn->vm_clock_nsec / 1000000000;
752f364ec65SWenchao Xia snprintf(clock_buf, sizeof(clock_buf),
753*52df1a5bSAbhiram Tilak "%04d:%02d:%02d.%03d",
754f364ec65SWenchao Xia (int)(secs / 3600),
755f364ec65SWenchao Xia (int)((secs / 60) % 60),
756f364ec65SWenchao Xia (int)(secs % 60),
757f364ec65SWenchao Xia (int)((sn->vm_clock_nsec / 1000000) % 1000));
758de38b500SEric Blake sizing = size_to_str(sn->vm_state_size);
759b39847a5SPavel Dovgalyuk if (sn->icount != -1ULL) {
760b39847a5SPavel Dovgalyuk snprintf(icount_buf, sizeof(icount_buf),
761b39847a5SPavel Dovgalyuk "%"PRId64, sn->icount);
762*52df1a5bSAbhiram Tilak } else {
763*52df1a5bSAbhiram Tilak snprintf(icount_buf, sizeof(icount_buf), "--");
764b39847a5SPavel Dovgalyuk }
765*52df1a5bSAbhiram Tilak qemu_printf("%-7s %-16s %8s %19s %15s %10s",
766f364ec65SWenchao Xia sn->id_str, sn->name,
767de38b500SEric Blake sizing,
768f364ec65SWenchao Xia date_buf,
769b39847a5SPavel Dovgalyuk clock_buf,
770b39847a5SPavel Dovgalyuk icount_buf);
771f364ec65SWenchao Xia }
772de38b500SEric Blake g_free(sizing);
773f364ec65SWenchao Xia }
774f364ec65SWenchao Xia
775e1ce7d74SMarkus Armbruster static void dump_qdict(int indentation, QDict *dict);
776e1ce7d74SMarkus Armbruster static void dump_qlist(int indentation, QList *list);
777a8d8ecb7SMax Reitz
dump_qobject(int comp_indent,QObject * obj)778e1ce7d74SMarkus Armbruster static void dump_qobject(int comp_indent, QObject *obj)
779a8d8ecb7SMax Reitz {
780a8d8ecb7SMax Reitz switch (qobject_type(obj)) {
78101b2ffceSMarc-André Lureau case QTYPE_QNUM: {
7827dc847ebSMax Reitz QNum *value = qobject_to(QNum, obj);
78301b2ffceSMarc-André Lureau char *tmp = qnum_to_string(value);
784e1ce7d74SMarkus Armbruster qemu_printf("%s", tmp);
78501b2ffceSMarc-André Lureau g_free(tmp);
786a8d8ecb7SMax Reitz break;
787a8d8ecb7SMax Reitz }
788a8d8ecb7SMax Reitz case QTYPE_QSTRING: {
7897dc847ebSMax Reitz QString *value = qobject_to(QString, obj);
790e1ce7d74SMarkus Armbruster qemu_printf("%s", qstring_get_str(value));
791a8d8ecb7SMax Reitz break;
792a8d8ecb7SMax Reitz }
793a8d8ecb7SMax Reitz case QTYPE_QDICT: {
7947dc847ebSMax Reitz QDict *value = qobject_to(QDict, obj);
795e1ce7d74SMarkus Armbruster dump_qdict(comp_indent, value);
796a8d8ecb7SMax Reitz break;
797a8d8ecb7SMax Reitz }
798a8d8ecb7SMax Reitz case QTYPE_QLIST: {
7997dc847ebSMax Reitz QList *value = qobject_to(QList, obj);
800e1ce7d74SMarkus Armbruster dump_qlist(comp_indent, value);
801a8d8ecb7SMax Reitz break;
802a8d8ecb7SMax Reitz }
803a8d8ecb7SMax Reitz case QTYPE_QBOOL: {
8047dc847ebSMax Reitz QBool *value = qobject_to(QBool, obj);
805e1ce7d74SMarkus Armbruster qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
806a8d8ecb7SMax Reitz break;
807a8d8ecb7SMax Reitz }
808a8d8ecb7SMax Reitz default:
809a8d8ecb7SMax Reitz abort();
810a8d8ecb7SMax Reitz }
811a8d8ecb7SMax Reitz }
812a8d8ecb7SMax Reitz
dump_qlist(int indentation,QList * list)813e1ce7d74SMarkus Armbruster static void dump_qlist(int indentation, QList *list)
814a8d8ecb7SMax Reitz {
815a8d8ecb7SMax Reitz const QListEntry *entry;
816a8d8ecb7SMax Reitz int i = 0;
817a8d8ecb7SMax Reitz
818a8d8ecb7SMax Reitz for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
8191310a3d3SEric Blake QType type = qobject_type(entry->value);
820a8d8ecb7SMax Reitz bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
821e1ce7d74SMarkus Armbruster qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
822853ccfedSPeter Xu composite ? '\n' : ' ');
823e1ce7d74SMarkus Armbruster dump_qobject(indentation + 1, entry->value);
824a8d8ecb7SMax Reitz if (!composite) {
825e1ce7d74SMarkus Armbruster qemu_printf("\n");
826a8d8ecb7SMax Reitz }
827a8d8ecb7SMax Reitz }
828a8d8ecb7SMax Reitz }
829a8d8ecb7SMax Reitz
dump_qdict(int indentation,QDict * dict)830e1ce7d74SMarkus Armbruster static void dump_qdict(int indentation, QDict *dict)
831a8d8ecb7SMax Reitz {
832a8d8ecb7SMax Reitz const QDictEntry *entry;
833a8d8ecb7SMax Reitz
834a8d8ecb7SMax Reitz for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
8351310a3d3SEric Blake QType type = qobject_type(entry->value);
836a8d8ecb7SMax Reitz bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
8375eda6227SPeter Xu char *key = g_malloc(strlen(entry->key) + 1);
838a8d8ecb7SMax Reitz int i;
839a8d8ecb7SMax Reitz
840a8d8ecb7SMax Reitz /* replace dashes with spaces in key (variable) names */
841a8d8ecb7SMax Reitz for (i = 0; entry->key[i]; i++) {
842a8d8ecb7SMax Reitz key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
843a8d8ecb7SMax Reitz }
844a8d8ecb7SMax Reitz key[i] = 0;
845e1ce7d74SMarkus Armbruster qemu_printf("%*s%s:%c", indentation * 4, "", key,
846853ccfedSPeter Xu composite ? '\n' : ' ');
847e1ce7d74SMarkus Armbruster dump_qobject(indentation + 1, entry->value);
848a8d8ecb7SMax Reitz if (!composite) {
849e1ce7d74SMarkus Armbruster qemu_printf("\n");
850a8d8ecb7SMax Reitz }
8515eda6227SPeter Xu g_free(key);
852a8d8ecb7SMax Reitz }
853a8d8ecb7SMax Reitz }
854a8d8ecb7SMax Reitz
8553716470bSHanna Reitz /*
8563716470bSHanna Reitz * Return whether dumping the given QObject with dump_qobject() would
8573716470bSHanna Reitz * yield an empty dump, i.e. not print anything.
8583716470bSHanna Reitz */
qobject_is_empty_dump(const QObject * obj)8593716470bSHanna Reitz static bool qobject_is_empty_dump(const QObject *obj)
8603716470bSHanna Reitz {
8613716470bSHanna Reitz switch (qobject_type(obj)) {
8623716470bSHanna Reitz case QTYPE_QNUM:
8633716470bSHanna Reitz case QTYPE_QSTRING:
8643716470bSHanna Reitz case QTYPE_QBOOL:
8653716470bSHanna Reitz return false;
8663716470bSHanna Reitz
8673716470bSHanna Reitz case QTYPE_QDICT:
8683716470bSHanna Reitz return qdict_size(qobject_to(QDict, obj)) == 0;
8693716470bSHanna Reitz
8703716470bSHanna Reitz case QTYPE_QLIST:
8713716470bSHanna Reitz return qlist_empty(qobject_to(QList, obj));
8723716470bSHanna Reitz
8733716470bSHanna Reitz default:
8743716470bSHanna Reitz abort();
8753716470bSHanna Reitz }
8763716470bSHanna Reitz }
8773716470bSHanna Reitz
8783716470bSHanna Reitz /**
8793716470bSHanna Reitz * Dumps the given ImageInfoSpecific object in a human-readable form,
8803716470bSHanna Reitz * prepending an optional prefix if the dump is not empty.
8813716470bSHanna Reitz */
bdrv_image_info_specific_dump(ImageInfoSpecific * info_spec,const char * prefix,int indentation)8823716470bSHanna Reitz void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec,
88376c9e975SHanna Reitz const char *prefix,
88476c9e975SHanna Reitz int indentation)
885a8d8ecb7SMax Reitz {
886a8d8ecb7SMax Reitz QObject *obj, *data;
8877d5e199aSDaniel P. Berrange Visitor *v = qobject_output_visitor_new(&obj);
888a8d8ecb7SMax Reitz
8893b098d56SEric Blake visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
8903b098d56SEric Blake visit_complete(v, &obj);
8917dc847ebSMax Reitz data = qdict_get(qobject_to(QDict, obj), "data");
8923716470bSHanna Reitz if (!qobject_is_empty_dump(data)) {
8933716470bSHanna Reitz if (prefix) {
89476c9e975SHanna Reitz qemu_printf("%*s%s", indentation * 4, "", prefix);
8953716470bSHanna Reitz }
89676c9e975SHanna Reitz dump_qobject(indentation + 1, data);
8973716470bSHanna Reitz }
898cb3e7f08SMarc-André Lureau qobject_unref(obj);
8993b098d56SEric Blake visit_free(v);
900a8d8ecb7SMax Reitz }
901a8d8ecb7SMax Reitz
902d570177bSHanna Reitz /**
903d570177bSHanna Reitz * Print the given @info object in human-readable form. Every field is indented
904d570177bSHanna Reitz * using the given @indentation (four spaces per indentation level).
905d570177bSHanna Reitz *
906d570177bSHanna Reitz * When using this to print a whole block graph, @protocol can be set to true to
907d570177bSHanna Reitz * signify that the given information is associated with a protocol node, i.e.
908d570177bSHanna Reitz * just data storage for an image, such that the data it presents is not really
909d570177bSHanna Reitz * a full VM disk. If so, several fields change name: For example, "virtual
910d570177bSHanna Reitz * size" is printed as "file length".
911d570177bSHanna Reitz * (Consider a qcow2 image, which is represented by a qcow2 node and a file
912d570177bSHanna Reitz * node. Printing a "virtual size" for the file node does not make sense,
913d570177bSHanna Reitz * because without the qcow2 node, it is not really a guest disk, so it does not
914d570177bSHanna Reitz * have a "virtual size". Therefore, we call it "file length" instead.)
915d570177bSHanna Reitz *
916d570177bSHanna Reitz * @protocol is ignored when @indentation is 0, because we take that to mean
917d570177bSHanna Reitz * that the associated node is the root node in the queried block graph, and
918d570177bSHanna Reitz * thus is always to be interpreted as a standalone guest disk.
919d570177bSHanna Reitz */
bdrv_node_info_dump(BlockNodeInfo * info,int indentation,bool protocol)920d570177bSHanna Reitz void bdrv_node_info_dump(BlockNodeInfo *info, int indentation, bool protocol)
921f364ec65SWenchao Xia {
922de38b500SEric Blake char *size_buf, *dsize_buf;
92376c9e975SHanna Reitz g_autofree char *ind_s = g_strdup_printf("%*s", indentation * 4, "");
92476c9e975SHanna Reitz
925d570177bSHanna Reitz if (indentation == 0) {
926d570177bSHanna Reitz /* Top level, consider this a normal image */
927d570177bSHanna Reitz protocol = false;
928d570177bSHanna Reitz }
929d570177bSHanna Reitz
930f364ec65SWenchao Xia if (!info->has_actual_size) {
931de38b500SEric Blake dsize_buf = g_strdup("unavailable");
932f364ec65SWenchao Xia } else {
933de38b500SEric Blake dsize_buf = size_to_str(info->actual_size);
934f364ec65SWenchao Xia }
935de38b500SEric Blake size_buf = size_to_str(info->virtual_size);
936d570177bSHanna Reitz qemu_printf("%s%s: %s\n"
937d570177bSHanna Reitz "%s%s: %s\n"
938d570177bSHanna Reitz "%s%s: %s (%" PRId64 " bytes)\n"
93976c9e975SHanna Reitz "%sdisk size: %s\n",
940d570177bSHanna Reitz ind_s, protocol ? "filename" : "image", info->filename,
941d570177bSHanna Reitz ind_s, protocol ? "protocol type" : "file format",
942d570177bSHanna Reitz info->format,
943d570177bSHanna Reitz ind_s, protocol ? "file length" : "virtual size",
944d570177bSHanna Reitz size_buf, info->virtual_size,
94576c9e975SHanna Reitz ind_s, dsize_buf);
946de38b500SEric Blake g_free(size_buf);
947de38b500SEric Blake g_free(dsize_buf);
948f364ec65SWenchao Xia
949f364ec65SWenchao Xia if (info->has_encrypted && info->encrypted) {
95076c9e975SHanna Reitz qemu_printf("%sencrypted: yes\n", ind_s);
951f364ec65SWenchao Xia }
952f364ec65SWenchao Xia
953f364ec65SWenchao Xia if (info->has_cluster_size) {
95476c9e975SHanna Reitz qemu_printf("%scluster_size: %" PRId64 "\n",
95576c9e975SHanna Reitz ind_s, info->cluster_size);
956f364ec65SWenchao Xia }
957f364ec65SWenchao Xia
958f364ec65SWenchao Xia if (info->has_dirty_flag && info->dirty_flag) {
95976c9e975SHanna Reitz qemu_printf("%scleanly shut down: no\n", ind_s);
960f364ec65SWenchao Xia }
961f364ec65SWenchao Xia
96254fde4ffSMarkus Armbruster if (info->backing_filename) {
96376c9e975SHanna Reitz qemu_printf("%sbacking file: %s", ind_s, info->backing_filename);
96454fde4ffSMarkus Armbruster if (!info->full_backing_filename) {
965e1ce7d74SMarkus Armbruster qemu_printf(" (cannot determine actual path)");
9665c9d9ca5SJohn Snow } else if (strcmp(info->backing_filename,
9675c9d9ca5SJohn Snow info->full_backing_filename) != 0) {
968e1ce7d74SMarkus Armbruster qemu_printf(" (actual path: %s)", info->full_backing_filename);
969f364ec65SWenchao Xia }
970e1ce7d74SMarkus Armbruster qemu_printf("\n");
97154fde4ffSMarkus Armbruster if (info->backing_filename_format) {
97276c9e975SHanna Reitz qemu_printf("%sbacking file format: %s\n",
97376c9e975SHanna Reitz ind_s, info->backing_filename_format);
974f364ec65SWenchao Xia }
975f364ec65SWenchao Xia }
976f364ec65SWenchao Xia
977f364ec65SWenchao Xia if (info->has_snapshots) {
978f364ec65SWenchao Xia SnapshotInfoList *elem;
979f364ec65SWenchao Xia
98076c9e975SHanna Reitz qemu_printf("%sSnapshot list:\n", ind_s);
98176c9e975SHanna Reitz qemu_printf("%s", ind_s);
982e1ce7d74SMarkus Armbruster bdrv_snapshot_dump(NULL);
983e1ce7d74SMarkus Armbruster qemu_printf("\n");
984f364ec65SWenchao Xia
985f364ec65SWenchao Xia /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
986f364ec65SWenchao Xia * we convert to the block layer's native QEMUSnapshotInfo for now.
987f364ec65SWenchao Xia */
988f364ec65SWenchao Xia for (elem = info->snapshots; elem; elem = elem->next) {
989f364ec65SWenchao Xia QEMUSnapshotInfo sn = {
990f364ec65SWenchao Xia .vm_state_size = elem->value->vm_state_size,
991f364ec65SWenchao Xia .date_sec = elem->value->date_sec,
992f364ec65SWenchao Xia .date_nsec = elem->value->date_nsec,
993f364ec65SWenchao Xia .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
994f364ec65SWenchao Xia elem->value->vm_clock_nsec,
995b39847a5SPavel Dovgalyuk .icount = elem->value->has_icount ?
996b39847a5SPavel Dovgalyuk elem->value->icount : -1ULL,
997f364ec65SWenchao Xia };
998f364ec65SWenchao Xia
999f364ec65SWenchao Xia pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
1000f364ec65SWenchao Xia pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
100176c9e975SHanna Reitz qemu_printf("%s", ind_s);
1002e1ce7d74SMarkus Armbruster bdrv_snapshot_dump(&sn);
1003e1ce7d74SMarkus Armbruster qemu_printf("\n");
1004f364ec65SWenchao Xia }
1005f364ec65SWenchao Xia }
1006a8d8ecb7SMax Reitz
100754fde4ffSMarkus Armbruster if (info->format_specific) {
10083716470bSHanna Reitz bdrv_image_info_specific_dump(info->format_specific,
100976c9e975SHanna Reitz "Format specific information:\n",
101076c9e975SHanna Reitz indentation);
1011a8d8ecb7SMax Reitz }
1012f364ec65SWenchao Xia }
1013