blob: 1cbb0935ff0ece3608ed3cac09881a9672d769f8 [file] [log] [blame]
Wenchao Xiaf364ec62013-05-25 11:09:44 +08001/*
2 * Block layer qmp and info dump related functions
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
Peter Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
Philippe Mathieu-Daudé31e40412019-09-03 14:05:55 +020026#include "qemu/cutils.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080027#include "block/qapi.h"
28#include "block/block_int.h"
Markus Armbrustere2c1c342022-12-21 14:35:49 +010029#include "block/dirty-bitmap.h"
Alberto Garcia76f4afb2015-06-08 18:17:44 +020030#include "block/throttle-groups.h"
Francesco Romanie2462112015-01-12 14:11:13 +010031#include "block/write-threshold.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010032#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010033#include "qapi/qapi-commands-block-core.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010034#include "qapi/qobject-output-visitor.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010035#include "qapi/qapi-visit-block-core.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010036#include "qapi/qmp/qbool.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010037#include "qapi/qmp/qdict.h"
Markus Armbruster47e6b292018-02-01 12:18:38 +010038#include "qapi/qmp/qlist.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010039#include "qapi/qmp/qnum.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010040#include "qapi/qmp/qstring.h"
Markus Armbrustere1ce7d72019-04-17 21:17:55 +020041#include "qemu/qemu-print.h"
Markus Armbrusterd829a212014-10-07 13:59:23 +020042#include "sysemu/block-backend.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080043
Kevin Wolfc83f9fb2016-03-03 11:37:48 +010044BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
Peter Krempafacda542020-01-20 09:50:49 +010045 BlockDriverState *bs,
46 bool flat,
47 Error **errp)
Benoît Canetc13163f2014-01-23 21:31:34 +010048{
Alberto Garciad5a8ee62015-04-17 14:52:43 +030049 ImageInfo **p_image_info;
Hanna Reitz5d881352022-06-20 18:26:58 +020050 ImageInfo *backing_info;
Fabiano Rosasc3b29ae2023-09-01 15:46:05 -030051 BlockDriverState *backing;
Max Reitzd470ad42017-11-10 21:31:09 +010052 BlockDeviceInfo *info;
Hanna Reitz5d881352022-06-20 18:26:58 +020053 ERRP_GUARD();
Benoît Canetc13163f2014-01-23 21:31:34 +010054
Max Reitzd470ad42017-11-10 21:31:09 +010055 if (!bs->drv) {
56 error_setg(errp, "Block device %s is ejected", bs->node_name);
57 return NULL;
58 }
59
Max Reitzf30c66b2019-02-01 20:29:05 +010060 bdrv_refresh_filename(bs);
61
Max Reitzd470ad42017-11-10 21:31:09 +010062 info = g_malloc0(sizeof(*info));
Benoît Canetc13163f2014-01-23 21:31:34 +010063 info->file = g_strdup(bs->filename);
Vladimir Sementsov-Ogievskiy307261b2021-05-27 18:40:54 +030064 info->ro = bdrv_is_read_only(bs);
Benoît Canetc13163f2014-01-23 21:31:34 +010065 info->drv = g_strdup(bs->drv->format_name);
66 info->encrypted = bs->encrypted;
Benoît Canetc13163f2014-01-23 21:31:34 +010067
Kevin Wolf9e193c52014-05-22 13:28:45 +020068 info->cache = g_new(BlockdevCacheInfo, 1);
69 *info->cache = (BlockdevCacheInfo) {
Kevin Wolfc83f9fb2016-03-03 11:37:48 +010070 .writeback = blk ? blk_enable_write_cache(blk) : true,
Kevin Wolf9e193c52014-05-22 13:28:45 +020071 .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
72 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
73 };
74
Benoît Canetc13163f2014-01-23 21:31:34 +010075 if (bs->node_name[0]) {
Benoît Canetc13163f2014-01-23 21:31:34 +010076 info->node_name = g_strdup(bs->node_name);
77 }
78
Max Reitz0b877d02018-08-01 20:34:11 +020079 backing = bdrv_cow_bs(bs);
80 if (backing) {
Max Reitz0b877d02018-08-01 20:34:11 +020081 info->backing_file = g_strdup(backing->filename);
Benoît Canetc13163f2014-01-23 21:31:34 +010082 }
83
Vladimir Sementsov-Ogievskiy590a63d2019-07-29 16:35:56 -040084 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
85 info->has_dirty_bitmaps = true;
86 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
87 }
88
Peter Lieven465bee12014-05-18 00:58:19 +020089 info->detect_zeroes = bs->detect_zeroes;
Benoît Canetc13163f2014-01-23 21:31:34 +010090
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +030091 if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
Benoît Canetc13163f2014-01-23 21:31:34 +010092 ThrottleConfig cfg;
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +030093 BlockBackendPublic *blkp = blk_get_public(blk);
Alberto Garcia76f4afb2015-06-08 18:17:44 +020094
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +030095 throttle_group_get_config(&blkp->throttle_group_member, &cfg);
Alberto Garcia76f4afb2015-06-08 18:17:44 +020096
Benoît Canetc13163f2014-01-23 21:31:34 +010097 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
98 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg;
99 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg;
100
101 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
102 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
103 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
104
105 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
106 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
107 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
108 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
109 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
110 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
111
112 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
113 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
114 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
115 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
116 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
117 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
118
Alberto Garcia398befd2016-02-18 12:27:04 +0200119 info->has_bps_max_length = info->has_bps_max;
120 info->bps_max_length =
121 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
122 info->has_bps_rd_max_length = info->has_bps_rd_max;
123 info->bps_rd_max_length =
124 cfg.buckets[THROTTLE_BPS_READ].burst_length;
125 info->has_bps_wr_max_length = info->has_bps_wr_max;
126 info->bps_wr_max_length =
127 cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
128
129 info->has_iops_max_length = info->has_iops_max;
130 info->iops_max_length =
131 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
132 info->has_iops_rd_max_length = info->has_iops_rd_max;
133 info->iops_rd_max_length =
134 cfg.buckets[THROTTLE_OPS_READ].burst_length;
135 info->has_iops_wr_max_length = info->has_iops_wr_max;
136 info->iops_wr_max_length =
137 cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
138
Benoît Canetc13163f2014-01-23 21:31:34 +0100139 info->has_iops_size = cfg.op_size;
140 info->iops_size = cfg.op_size;
Alberto Garciab8fe1692015-06-08 18:17:46 +0200141
Manos Pitsidianakis022cdc92017-08-25 16:20:23 +0300142 info->group =
143 g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
Benoît Canetc13163f2014-01-23 21:31:34 +0100144 }
145
Francesco Romanie2462112015-01-12 14:11:13 +0100146 info->write_threshold = bdrv_write_threshold_get(bs);
147
Alberto Garciad5a8ee62015-04-17 14:52:43 +0300148 p_image_info = &info->image;
Kevin Wolfd3c8c672017-07-18 17:24:05 +0200149 info->backing_file_depth = 0;
Kevin Wolfd3c8c672017-07-18 17:24:05 +0200150
Hanna Reitz5d881352022-06-20 18:26:58 +0200151 /*
152 * Skip automatically inserted nodes that the user isn't aware of for
153 * query-block (blk != NULL), but not for query-named-block-nodes
154 */
Fabiano Rosasc3b29ae2023-09-01 15:46:05 -0300155 bdrv_query_image_info(bs, p_image_info, flat, blk != NULL, errp);
Hanna Reitz5d881352022-06-20 18:26:58 +0200156 if (*errp) {
157 qapi_free_BlockDeviceInfo(info);
158 return NULL;
159 }
Peter Krempafacda542020-01-20 09:50:49 +0100160
Hanna Reitz5d881352022-06-20 18:26:58 +0200161 backing_info = info->image->backing_image;
162 while (backing_info) {
163 info->backing_file_depth++;
164 backing_info = backing_info->backing_image;
Alberto Garciad5a8ee62015-04-17 14:52:43 +0300165 }
166
Benoît Canetc13163f2014-01-23 21:31:34 +0100167 return info;
168}
169
Wenchao Xiafb0ed452013-06-06 12:27:57 +0800170/*
171 * Returns 0 on success, with *p_list either set to describe snapshot
172 * information, or NULL because there are no snapshots. Returns -errno on
173 * error, with *p_list untouched.
174 */
175int bdrv_query_snapshot_info_list(BlockDriverState *bs,
176 SnapshotInfoList **p_list,
177 Error **errp)
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800178{
179 int i, sn_count;
180 QEMUSnapshotInfo *sn_tab = NULL;
Eric Blake95b3a8c2021-01-13 16:10:13 -0600181 SnapshotInfoList *head = NULL, **tail = &head;
Wenchao Xiafb0ed452013-06-06 12:27:57 +0800182 SnapshotInfo *info;
183
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800184 sn_count = bdrv_snapshot_list(bs, &sn_tab);
Wenchao Xiafb0ed452013-06-06 12:27:57 +0800185 if (sn_count < 0) {
186 const char *dev = bdrv_get_device_name(bs);
187 switch (sn_count) {
188 case -ENOMEDIUM:
189 error_setg(errp, "Device '%s' is not inserted", dev);
190 break;
191 case -ENOTSUP:
192 error_setg(errp,
193 "Device '%s' does not support internal snapshots",
194 dev);
195 break;
196 default:
197 error_setg_errno(errp, -sn_count,
198 "Can't list snapshots of device '%s'", dev);
199 break;
200 }
201 return sn_count;
202 }
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800203
204 for (i = 0; i < sn_count; i++) {
Wenchao Xiafb0ed452013-06-06 12:27:57 +0800205 info = g_new0(SnapshotInfo, 1);
206 info->id = g_strdup(sn_tab[i].id_str);
207 info->name = g_strdup(sn_tab[i].name);
208 info->vm_state_size = sn_tab[i].vm_state_size;
209 info->date_sec = sn_tab[i].date_sec;
210 info->date_nsec = sn_tab[i].date_nsec;
211 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
212 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
Pavel Dovgalyukb39847a2020-10-03 20:13:08 +0300213 info->icount = sn_tab[i].icount;
214 info->has_icount = sn_tab[i].icount != -1ULL;
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800215
Eric Blake95b3a8c2021-01-13 16:10:13 -0600216 QAPI_LIST_APPEND(tail, info);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800217 }
218
219 g_free(sn_tab);
Wenchao Xiafb0ed452013-06-06 12:27:57 +0800220 *p_list = head;
221 return 0;
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800222}
223
Wenchao Xia43526ec2013-06-06 12:27:58 +0800224/**
Hanna Reitza2085f82022-06-20 18:26:56 +0200225 * Helper function for other query info functions. Store information about @bs
226 * in @info, setting @errp on error.
Wenchao Xia43526ec2013-06-06 12:27:58 +0800227 */
Hanna Reitza2085f82022-06-20 18:26:56 +0200228static void bdrv_do_query_node_info(BlockDriverState *bs,
229 BlockNodeInfo *info,
230 Error **errp)
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800231{
Markus Armbruster52bf1e72014-06-26 13:23:25 +0200232 int64_t size;
Wenchao Xia43526ec2013-06-06 12:27:58 +0800233 const char *backing_filename;
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800234 BlockDriverInfo bdi;
Wenchao Xia43526ec2013-06-06 12:27:58 +0800235 int ret;
236 Error *err = NULL;
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800237
Paolo Bonzini1963f8d2015-12-23 11:48:23 +0100238 aio_context_acquire(bdrv_get_aio_context(bs));
239
Markus Armbruster52bf1e72014-06-26 13:23:25 +0200240 size = bdrv_getlength(bs);
241 if (size < 0) {
Fam Zheng9adceb02017-01-19 21:07:58 +0800242 error_setg_errno(errp, -size, "Can't get image size '%s'",
243 bs->exact_filename);
Paolo Bonzini1963f8d2015-12-23 11:48:23 +0100244 goto out;
Markus Armbruster52bf1e72014-06-26 13:23:25 +0200245 }
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800246
Max Reitzf30c66b2019-02-01 20:29:05 +0100247 bdrv_refresh_filename(bs);
248
Wenchao Xia43526ec2013-06-06 12:27:58 +0800249 info->filename = g_strdup(bs->filename);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800250 info->format = g_strdup(bdrv_get_format_name(bs));
Markus Armbruster52bf1e72014-06-26 13:23:25 +0200251 info->virtual_size = size;
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800252 info->actual_size = bdrv_get_allocated_file_size(bs);
253 info->has_actual_size = info->actual_size >= 0;
Max Reitz8b8277c2019-06-12 17:08:20 +0200254 if (bs->encrypted) {
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800255 info->encrypted = true;
256 info->has_encrypted = true;
257 }
258 if (bdrv_get_info(bs, &bdi) >= 0) {
259 if (bdi.cluster_size != 0) {
260 info->cluster_size = bdi.cluster_size;
261 info->has_cluster_size = true;
262 }
263 info->dirty_flag = bdi.is_dirty;
264 info->has_dirty_flag = true;
265 }
Andrey Shinkevich1bf6e9c2019-02-08 18:06:06 +0300266 info->format_specific = bdrv_get_specific_info(bs, &err);
267 if (err) {
268 error_propagate(errp, err);
Andrey Shinkevich1bf6e9c2019-02-08 18:06:06 +0300269 goto out;
270 }
Wenchao Xia43526ec2013-06-06 12:27:58 +0800271 backing_filename = bs->backing_file;
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800272 if (backing_filename[0] != '\0') {
Max Reitz6b6833c2019-02-01 20:29:15 +0100273 char *backing_filename2;
Max Reitz0b877d02018-08-01 20:34:11 +0200274
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800275 info->backing_filename = g_strdup(backing_filename);
Max Reitz6b6833c2019-02-01 20:29:15 +0100276 backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800277
John Snow12dcb1c2015-12-14 14:55:13 -0500278 /* Always report the full_backing_filename if present, even if it's the
279 * same as backing_filename. That they are same is useful info. */
280 if (backing_filename2) {
281 info->full_backing_filename = g_strdup(backing_filename2);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800282 }
283
284 if (bs->backing_format[0]) {
285 info->backing_filename_format = g_strdup(bs->backing_format);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800286 }
Jeff Cody564d64b2015-01-22 08:03:27 -0500287 g_free(backing_filename2);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800288 }
Wenchao Xia43526ec2013-06-06 12:27:58 +0800289
290 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
291 switch (ret) {
292 case 0:
293 if (info->snapshots) {
294 info->has_snapshots = true;
295 }
296 break;
297 /* recoverable error */
298 case -ENOMEDIUM:
299 case -ENOTSUP:
300 error_free(err);
301 break;
302 default:
303 error_propagate(errp, err);
Paolo Bonzini1963f8d2015-12-23 11:48:23 +0100304 goto out;
Wenchao Xia43526ec2013-06-06 12:27:58 +0800305 }
306
Paolo Bonzini1963f8d2015-12-23 11:48:23 +0100307out:
308 aio_context_release(bdrv_get_aio_context(bs));
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800309}
310
Hanna Reitza2085f82022-06-20 18:26:56 +0200311/**
Hanna Reitza2085f82022-06-20 18:26:56 +0200312 * bdrv_query_image_info:
313 * @bs: block node to examine
314 * @p_info: location to store image information
Hanna Reitz5d881352022-06-20 18:26:58 +0200315 * @flat: skip backing node information
316 * @skip_implicit_filters: skip implicit filters in the backing chain
Hanna Reitza2085f82022-06-20 18:26:56 +0200317 * @errp: location to store error information
318 *
Hanna Reitz5d881352022-06-20 18:26:58 +0200319 * Store image information in @p_info, potentially recursively covering the
320 * backing chain.
Hanna Reitza2085f82022-06-20 18:26:56 +0200321 *
Hanna Reitz5d881352022-06-20 18:26:58 +0200322 * If @flat is true, do not query backing image information, i.e.
323 * (*p_info)->has_backing_image will be set to false and
324 * (*p_info)->backing_image to NULL even when the image does in fact have a
325 * backing image.
326 *
327 * If @skip_implicit_filters is true, implicit filter nodes in the backing chain
328 * will be skipped when querying backing image information.
329 * (@skip_implicit_filters is ignored when @flat is true.)
Hanna Reitza2085f82022-06-20 18:26:56 +0200330 *
331 * @p_info will be set only on success. On error, store error in @errp.
332 */
333void bdrv_query_image_info(BlockDriverState *bs,
334 ImageInfo **p_info,
Hanna Reitz5d881352022-06-20 18:26:58 +0200335 bool flat,
336 bool skip_implicit_filters,
Hanna Reitza2085f82022-06-20 18:26:56 +0200337 Error **errp)
338{
339 ImageInfo *info;
340 ERRP_GUARD();
341
342 info = g_new0(ImageInfo, 1);
343 bdrv_do_query_node_info(bs, qapi_ImageInfo_base(info), errp);
344 if (*errp) {
Hanna Reitz5d881352022-06-20 18:26:58 +0200345 goto fail;
346 }
347
348 if (!flat) {
349 BlockDriverState *backing;
350
351 /*
352 * Use any filtered child here (for backwards compatibility to when
353 * we always took bs->backing, which might be any filtered child).
354 */
355 backing = bdrv_filter_or_cow_bs(bs);
356 if (skip_implicit_filters) {
357 backing = bdrv_skip_implicit_filters(backing);
358 }
359
360 if (backing) {
361 bdrv_query_image_info(backing, &info->backing_image, false,
362 skip_implicit_filters, errp);
363 if (*errp) {
364 goto fail;
365 }
366 }
Hanna Reitza2085f82022-06-20 18:26:56 +0200367 }
368
369 *p_info = info;
Hanna Reitz5d881352022-06-20 18:26:58 +0200370 return;
371
372fail:
373 assert(*errp);
374 qapi_free_ImageInfo(info);
Hanna Reitza2085f82022-06-20 18:26:56 +0200375}
376
Hanna Reitz6cab3392022-06-20 18:26:59 +0200377/**
378 * bdrv_query_block_graph_info:
379 * @bs: root node to start from
380 * @p_info: location to store image information
381 * @errp: location to store error information
382 *
383 * Store image information about the graph starting from @bs in @p_info.
384 *
385 * @p_info will be set only on success. On error, store error in @errp.
386 */
387void bdrv_query_block_graph_info(BlockDriverState *bs,
388 BlockGraphInfo **p_info,
389 Error **errp)
390{
391 BlockGraphInfo *info;
392 BlockChildInfoList **children_list_tail;
393 BdrvChild *c;
394 ERRP_GUARD();
395
396 info = g_new0(BlockGraphInfo, 1);
397 bdrv_do_query_node_info(bs, qapi_BlockGraphInfo_base(info), errp);
398 if (*errp) {
399 goto fail;
400 }
401
402 children_list_tail = &info->children;
403
404 QLIST_FOREACH(c, &bs->children, next) {
405 BlockChildInfo *c_info;
406
407 c_info = g_new0(BlockChildInfo, 1);
408 QAPI_LIST_APPEND(children_list_tail, c_info);
409
410 c_info->name = g_strdup(c->name);
411 bdrv_query_block_graph_info(c->bs, &c_info->info, errp);
412 if (*errp) {
413 goto fail;
414 }
415 }
416
417 *p_info = info;
418 return;
419
420fail:
421 assert(*errp != NULL);
422 qapi_free_BlockGraphInfo(info);
423}
424
Wenchao Xia553a7e82013-06-06 12:27:59 +0800425/* @p_info will be set only on success. */
Markus Armbrusterd829a212014-10-07 13:59:23 +0200426static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
427 Error **errp)
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800428{
429 BlockInfo *info = g_malloc0(sizeof(*info));
Markus Armbrusterd829a212014-10-07 13:59:23 +0200430 BlockDriverState *bs = blk_bs(blk);
Kevin Wolf46eade72017-07-11 13:27:38 +0200431 char *qdev;
432
Kevin Wolfd3c8c672017-07-18 17:24:05 +0200433 /* Skip automatically inserted nodes that the user isn't aware of */
Max Reitz0a7585d2019-06-12 18:23:25 +0200434 bs = bdrv_skip_implicit_filters(bs);
Kevin Wolfd3c8c672017-07-18 17:24:05 +0200435
Markus Armbrusterd829a212014-10-07 13:59:23 +0200436 info->device = g_strdup(blk_name(blk));
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800437 info->type = g_strdup("unknown");
Markus Armbrustera7f53e22014-10-07 13:59:25 +0200438 info->locked = blk_dev_is_medium_locked(blk);
439 info->removable = blk_dev_has_removable_media(blk);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800440
Kevin Wolf46eade72017-07-11 13:27:38 +0200441 qdev = blk_get_attached_dev_id(blk);
442 if (qdev && *qdev) {
Kevin Wolf46eade72017-07-11 13:27:38 +0200443 info->qdev = qdev;
444 } else {
445 g_free(qdev);
446 }
447
Max Reitz327032c2016-01-29 20:49:13 +0100448 if (blk_dev_has_tray(blk)) {
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800449 info->has_tray_open = true;
Markus Armbrustera7f53e22014-10-07 13:59:25 +0200450 info->tray_open = blk_dev_is_tray_open(blk);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800451 }
452
Max Reitz373340b2015-10-19 17:53:22 +0200453 if (blk_iostatus_is_enabled(blk)) {
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800454 info->has_io_status = true;
Max Reitz373340b2015-10-19 17:53:22 +0200455 info->io_status = blk_iostatus(blk);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800456 }
457
Max Reitz5433c242015-10-19 17:53:29 +0200458 if (bs && bs->drv) {
Peter Krempafacda542020-01-20 09:50:49 +0100459 info->inserted = bdrv_block_device_info(blk, bs, false, errp);
Alberto Garciad5a8ee62015-04-17 14:52:43 +0300460 if (info->inserted == NULL) {
461 goto err;
Wenchao Xia553a7e82013-06-06 12:27:59 +0800462 }
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800463 }
Wenchao Xia553a7e82013-06-06 12:27:59 +0800464
465 *p_info = info;
466 return;
467
468 err:
469 qapi_free_BlockInfo(info);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800470}
471
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +0300472static uint64List *uint64_list(uint64_t *list, int size)
473{
474 int i;
475 uint64List *out_list = NULL;
Eric Blakec3033fd2021-01-13 16:10:12 -0600476 uint64List **tail = &out_list;
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +0300477
478 for (i = 0; i < size; i++) {
Eric Blakec3033fd2021-01-13 16:10:12 -0600479 QAPI_LIST_APPEND(tail, list[i]);
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +0300480 }
481
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +0300482 return out_list;
483}
484
Markus Armbruster54fde4f2022-11-04 17:06:52 +0100485static BlockLatencyHistogramInfo *
486bdrv_latency_histogram_stats(BlockLatencyHistogram *hist)
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +0300487{
Markus Armbruster54fde4f2022-11-04 17:06:52 +0100488 BlockLatencyHistogramInfo *info;
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +0300489
Markus Armbruster54fde4f2022-11-04 17:06:52 +0100490 if (!hist->bins) {
491 return NULL;
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +0300492 }
Markus Armbruster54fde4f2022-11-04 17:06:52 +0100493
494 info = g_new0(BlockLatencyHistogramInfo, 1);
495 info->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
496 info->bins = uint64_list(hist->bins, hist->nbins);
497 return info;
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +0300498}
499
Max Reitz54302152016-03-02 18:31:09 +0100500static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
Kevin Wolf2b77e602016-02-26 19:02:30 +0100501{
502 BlockAcctStats *stats = blk_get_stats(blk);
503 BlockAcctTimedStats *ts = NULL;
Markus Armbruster54fde4f2022-11-04 17:06:52 +0100504 BlockLatencyHistogram *hgram;
Kevin Wolf2b77e602016-02-26 19:02:30 +0100505
Max Reitz54302152016-03-02 18:31:09 +0100506 ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
507 ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
Sam Li52eb76f2023-05-08 13:19:14 +0800508 ds->zone_append_bytes = stats->nr_bytes[BLOCK_ACCT_ZONE_APPEND];
Anton Nefedov159f85d2019-09-23 15:17:30 +0300509 ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
Max Reitz54302152016-03-02 18:31:09 +0100510 ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
511 ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
Sam Li52eb76f2023-05-08 13:19:14 +0800512 ds->zone_append_operations = stats->nr_ops[BLOCK_ACCT_ZONE_APPEND];
Anton Nefedov159f85d2019-09-23 15:17:30 +0300513 ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
Kevin Wolf2b77e602016-02-26 19:02:30 +0100514
Max Reitz54302152016-03-02 18:31:09 +0100515 ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
516 ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
Sam Li52eb76f2023-05-08 13:19:14 +0800517 ds->failed_zone_append_operations =
518 stats->failed_ops[BLOCK_ACCT_ZONE_APPEND];
Max Reitz54302152016-03-02 18:31:09 +0100519 ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
Anton Nefedov159f85d2019-09-23 15:17:30 +0300520 ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
Kevin Wolf2b77e602016-02-26 19:02:30 +0100521
Max Reitz54302152016-03-02 18:31:09 +0100522 ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
523 ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
Sam Li52eb76f2023-05-08 13:19:14 +0800524 ds->invalid_zone_append_operations =
525 stats->invalid_ops[BLOCK_ACCT_ZONE_APPEND];
Max Reitz54302152016-03-02 18:31:09 +0100526 ds->invalid_flush_operations =
Kevin Wolf2b77e602016-02-26 19:02:30 +0100527 stats->invalid_ops[BLOCK_ACCT_FLUSH];
Anton Nefedov159f85d2019-09-23 15:17:30 +0300528 ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
Kevin Wolf2b77e602016-02-26 19:02:30 +0100529
Max Reitz54302152016-03-02 18:31:09 +0100530 ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
531 ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
Sam Li52eb76f2023-05-08 13:19:14 +0800532 ds->zone_append_merged = stats->merged[BLOCK_ACCT_ZONE_APPEND];
Anton Nefedov159f85d2019-09-23 15:17:30 +0300533 ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
Max Reitz54302152016-03-02 18:31:09 +0100534 ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
535 ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
Sam Li52eb76f2023-05-08 13:19:14 +0800536 ds->zone_append_total_time_ns =
537 stats->total_time_ns[BLOCK_ACCT_ZONE_APPEND];
Max Reitz54302152016-03-02 18:31:09 +0100538 ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
539 ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
Anton Nefedov159f85d2019-09-23 15:17:30 +0300540 ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
Kevin Wolf2b77e602016-02-26 19:02:30 +0100541
Max Reitz54302152016-03-02 18:31:09 +0100542 ds->has_idle_time_ns = stats->last_access_time_ns > 0;
543 if (ds->has_idle_time_ns) {
544 ds->idle_time_ns = block_acct_idle_time_ns(stats);
Kevin Wolf2b77e602016-02-26 19:02:30 +0100545 }
546
Max Reitz54302152016-03-02 18:31:09 +0100547 ds->account_invalid = stats->account_invalid;
548 ds->account_failed = stats->account_failed;
Kevin Wolf2b77e602016-02-26 19:02:30 +0100549
550 while ((ts = block_acct_interval_next(stats, ts))) {
Kevin Wolf2b77e602016-02-26 19:02:30 +0100551 BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
Kevin Wolf2b77e602016-02-26 19:02:30 +0100552
553 TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
554 TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
Sam Li52eb76f2023-05-08 13:19:14 +0800555 TimedAverage *zap = &ts->latency[BLOCK_ACCT_ZONE_APPEND];
Kevin Wolf2b77e602016-02-26 19:02:30 +0100556 TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
557
558 dev_stats->interval_length = ts->interval_length;
559
560 dev_stats->min_rd_latency_ns = timed_average_min(rd);
561 dev_stats->max_rd_latency_ns = timed_average_max(rd);
562 dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
563
564 dev_stats->min_wr_latency_ns = timed_average_min(wr);
565 dev_stats->max_wr_latency_ns = timed_average_max(wr);
566 dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
567
Sam Li52eb76f2023-05-08 13:19:14 +0800568 dev_stats->min_zone_append_latency_ns = timed_average_min(zap);
569 dev_stats->max_zone_append_latency_ns = timed_average_max(zap);
570 dev_stats->avg_zone_append_latency_ns = timed_average_avg(zap);
571
Kevin Wolf2b77e602016-02-26 19:02:30 +0100572 dev_stats->min_flush_latency_ns = timed_average_min(fl);
573 dev_stats->max_flush_latency_ns = timed_average_max(fl);
574 dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
575
576 dev_stats->avg_rd_queue_depth =
577 block_acct_queue_depth(ts, BLOCK_ACCT_READ);
578 dev_stats->avg_wr_queue_depth =
579 block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
Sam Li52eb76f2023-05-08 13:19:14 +0800580 dev_stats->avg_zone_append_queue_depth =
581 block_acct_queue_depth(ts, BLOCK_ACCT_ZONE_APPEND);
Eric Blake54aa3de2020-11-12 19:13:37 -0600582
583 QAPI_LIST_PREPEND(ds->timed_stats, dev_stats);
Kevin Wolf2b77e602016-02-26 19:02:30 +0100584 }
Vladimir Sementsov-Ogievskiy7e5c7762018-03-09 19:52:12 +0300585
Markus Armbruster54fde4f2022-11-04 17:06:52 +0100586 hgram = stats->latency_histogram;
587 ds->rd_latency_histogram
588 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_READ]);
589 ds->wr_latency_histogram
590 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_WRITE]);
Sam Li52eb76f2023-05-08 13:19:14 +0800591 ds->zone_append_latency_histogram
592 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_ZONE_APPEND]);
Markus Armbruster54fde4f2022-11-04 17:06:52 +0100593 ds->flush_latency_histogram
594 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_FLUSH]);
Kevin Wolf2b77e602016-02-26 19:02:30 +0100595}
596
Kevin Wolf6ec75a62023-05-04 13:57:47 +0200597static BlockStats * GRAPH_RDLOCK
598bdrv_query_bds_stats(BlockDriverState *bs, bool blk_level)
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800599{
Max Reitz3f261912019-11-28 09:49:22 +0100600 BdrvChild *parent_child;
Max Reitz0a7585d2019-06-12 18:23:25 +0200601 BlockDriverState *filter_or_cow_bs;
Dou Liyang20a6d762017-01-15 16:01:14 +0800602 BlockStats *s = NULL;
603
604 s = g_malloc0(sizeof(*s));
605 s->stats = g_malloc0(sizeof(*s->stats));
606
607 if (!bs) {
608 return s;
609 }
610
Kevin Wolfd3c8c672017-07-18 17:24:05 +0200611 /* Skip automatically inserted nodes that the user isn't aware of in
612 * a BlockBackend-level command. Stay at the exact node for a node-level
613 * command. */
Max Reitz0a7585d2019-06-12 18:23:25 +0200614 if (blk_level) {
615 bs = bdrv_skip_implicit_filters(bs);
Kevin Wolfd3c8c672017-07-18 17:24:05 +0200616 }
617
Fam Zheng4875a772014-10-31 11:32:56 +0800618 if (bdrv_get_node_name(bs)[0]) {
Fam Zheng4875a772014-10-31 11:32:56 +0800619 s->node_name = g_strdup(bdrv_get_node_name(bs));
620 }
621
Paolo Bonzinif7946da2017-06-05 14:39:00 +0200622 s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
Max Reitz53d8f9d2015-10-19 17:53:20 +0200623
Anton Nefedovd9245592019-09-23 15:17:37 +0300624 s->driver_specific = bdrv_get_specific_stats(bs);
Anton Nefedovd9245592019-09-23 15:17:37 +0300625
Max Reitz3f261912019-11-28 09:49:22 +0100626 parent_child = bdrv_primary_child(bs);
627 if (!parent_child ||
628 !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED)))
629 {
630 BdrvChild *c;
631
632 /*
633 * Look for a unique data-storing child. We do not need to look for
634 * filtered children, as there would be only one and it would have been
635 * the primary child.
636 */
637 parent_child = NULL;
638 QLIST_FOREACH(c, &bs->children, next) {
639 if (c->role & BDRV_CHILD_DATA) {
640 if (parent_child) {
641 /*
642 * There are multiple data-storing children and we cannot
643 * choose between them.
644 */
645 parent_child = NULL;
646 break;
647 }
648 parent_child = c;
649 }
650 }
651 }
652 if (parent_child) {
Max Reitz3f261912019-11-28 09:49:22 +0100653 s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800654 }
655
Max Reitz0a7585d2019-06-12 18:23:25 +0200656 filter_or_cow_bs = bdrv_filter_or_cow_bs(bs);
657 if (blk_level && filter_or_cow_bs) {
658 /*
659 * Put any filtered or COW child here (for backwards
660 * compatibility to when we put bs0->backing here, which might
661 * be either)
662 */
Max Reitz0a7585d2019-06-12 18:23:25 +0200663 s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level);
Fam Zhengc8059b92014-01-23 10:03:26 +0800664 }
665
Dou Liyang20a6d762017-01-15 16:01:14 +0800666 return s;
Kevin Wolfb07363a2016-02-26 21:03:01 +0100667}
668
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800669BlockInfoList *qmp_query_block(Error **errp)
670{
671 BlockInfoList *head = NULL, **p_next = &head;
Markus Armbrusterd829a212014-10-07 13:59:23 +0200672 BlockBackend *blk;
Wenchao Xia553a7e82013-06-06 12:27:59 +0800673 Error *local_err = NULL;
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800674
Kevin Wolfd5b68842017-07-11 13:04:28 +0200675 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
676 BlockInfoList *info;
677
Kevin Wolfec18b0a2017-07-11 14:00:57 +0200678 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
Kevin Wolfd5b68842017-07-11 13:04:28 +0200679 continue;
680 }
681
682 info = g_malloc0(sizeof(*info));
Markus Armbrusterd829a212014-10-07 13:59:23 +0200683 bdrv_query_info(blk, &info->value, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100684 if (local_err) {
Wenchao Xia553a7e82013-06-06 12:27:59 +0800685 error_propagate(errp, local_err);
Markus Armbruster903c3412015-11-20 13:53:35 +0100686 g_free(info);
687 qapi_free_BlockInfoList(head);
688 return NULL;
Wenchao Xia553a7e82013-06-06 12:27:59 +0800689 }
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800690
691 *p_next = info;
692 p_next = &info->next;
693 }
694
695 return head;
696}
697
Fam Zhengf71eaa72014-10-31 11:32:57 +0800698BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
699 bool query_nodes,
700 Error **errp)
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800701{
Eric Blakec3033fd2021-01-13 16:10:12 -0600702 BlockStatsList *head = NULL, **tail = &head;
Dou Liyanga6baa602017-01-15 16:01:15 +0800703 BlockBackend *blk;
704 BlockDriverState *bs;
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800705
Kevin Wolf6ec75a62023-05-04 13:57:47 +0200706 GRAPH_RDLOCK_GUARD_MAINLOOP();
707
Fam Zhengf71eaa72014-10-31 11:32:57 +0800708 /* Just to be safe if query_nodes is not always initialized */
Dou Liyanga6baa602017-01-15 16:01:15 +0800709 if (has_query_nodes && query_nodes) {
710 for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
Dou Liyanga6baa602017-01-15 16:01:15 +0800711 AioContext *ctx = bdrv_get_aio_context(bs);
Fam Zhengf71eaa72014-10-31 11:32:57 +0800712
Dou Liyanga6baa602017-01-15 16:01:15 +0800713 aio_context_acquire(ctx);
Eric Blakec3033fd2021-01-13 16:10:12 -0600714 QAPI_LIST_APPEND(tail, bdrv_query_bds_stats(bs, false));
Dou Liyanga6baa602017-01-15 16:01:15 +0800715 aio_context_release(ctx);
Dou Liyanga6baa602017-01-15 16:01:15 +0800716 }
717 } else {
Kevin Wolf567dcb32018-07-27 16:09:25 +0200718 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
Dou Liyanga6baa602017-01-15 16:01:15 +0800719 AioContext *ctx = blk_get_aio_context(blk);
720 BlockStats *s;
Kevin Wolf5a9cb5a2018-07-27 16:07:07 +0200721 char *qdev;
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800722
Kevin Wolf567dcb32018-07-27 16:09:25 +0200723 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
724 continue;
725 }
726
Dou Liyanga6baa602017-01-15 16:01:15 +0800727 aio_context_acquire(ctx);
728 s = bdrv_query_bds_stats(blk_bs(blk), true);
Dou Liyanga6baa602017-01-15 16:01:15 +0800729 s->device = g_strdup(blk_name(blk));
Kevin Wolf5a9cb5a2018-07-27 16:07:07 +0200730
731 qdev = blk_get_attached_dev_id(blk);
732 if (qdev && *qdev) {
Kevin Wolf5a9cb5a2018-07-27 16:07:07 +0200733 s->qdev = qdev;
734 } else {
735 g_free(qdev);
736 }
737
Dou Liyanga6baa602017-01-15 16:01:15 +0800738 bdrv_query_blk_stats(s->stats, blk);
739 aio_context_release(ctx);
740
Eric Blakec3033fd2021-01-13 16:10:12 -0600741 QAPI_LIST_APPEND(tail, s);
Dou Liyanga6baa602017-01-15 16:01:15 +0800742 }
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800743 }
744
745 return head;
746}
747
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200748void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800749{
Daniel P. Berrangé39683552021-04-30 12:59:06 +0100750 char clock_buf[128];
Pavel Dovgalyukb39847a2020-10-03 20:13:08 +0300751 char icount_buf[128] = {0};
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800752 int64_t secs;
Eric Blakede38b502019-04-17 12:11:01 -0500753 char *sizing = NULL;
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800754
755 if (!sn) {
Kevin Wolf26513a02021-02-02 16:59:11 +0100756 qemu_printf("%-10s%-17s%8s%20s%13s%11s",
Pavel Dovgalyukb39847a2020-10-03 20:13:08 +0300757 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT");
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800758 } else {
Daniel P. Berrangé39683552021-04-30 12:59:06 +0100759 g_autoptr(GDateTime) date = g_date_time_new_from_unix_local(sn->date_sec);
760 g_autofree char *date_buf = g_date_time_format(date, "%Y-%m-%d %H:%M:%S");
761
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800762 secs = sn->vm_clock_nsec / 1000000000;
763 snprintf(clock_buf, sizeof(clock_buf),
764 "%02d:%02d:%02d.%03d",
765 (int)(secs / 3600),
766 (int)((secs / 60) % 60),
767 (int)(secs % 60),
768 (int)((sn->vm_clock_nsec / 1000000) % 1000));
Eric Blakede38b502019-04-17 12:11:01 -0500769 sizing = size_to_str(sn->vm_state_size);
Pavel Dovgalyukb39847a2020-10-03 20:13:08 +0300770 if (sn->icount != -1ULL) {
771 snprintf(icount_buf, sizeof(icount_buf),
772 "%"PRId64, sn->icount);
773 }
Kevin Wolf26513a02021-02-02 16:59:11 +0100774 qemu_printf("%-9s %-16s %8s%20s%13s%11s",
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200775 sn->id_str, sn->name,
Eric Blakede38b502019-04-17 12:11:01 -0500776 sizing,
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200777 date_buf,
Pavel Dovgalyukb39847a2020-10-03 20:13:08 +0300778 clock_buf,
779 icount_buf);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800780 }
Eric Blakede38b502019-04-17 12:11:01 -0500781 g_free(sizing);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800782}
783
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200784static void dump_qdict(int indentation, QDict *dict);
785static void dump_qlist(int indentation, QList *list);
Max Reitza8d8ecb2013-10-09 10:46:17 +0200786
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200787static void dump_qobject(int comp_indent, QObject *obj)
Max Reitza8d8ecb2013-10-09 10:46:17 +0200788{
789 switch (qobject_type(obj)) {
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400790 case QTYPE_QNUM: {
Max Reitz7dc847e2018-02-24 16:40:29 +0100791 QNum *value = qobject_to(QNum, obj);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400792 char *tmp = qnum_to_string(value);
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200793 qemu_printf("%s", tmp);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400794 g_free(tmp);
Max Reitza8d8ecb2013-10-09 10:46:17 +0200795 break;
796 }
797 case QTYPE_QSTRING: {
Max Reitz7dc847e2018-02-24 16:40:29 +0100798 QString *value = qobject_to(QString, obj);
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200799 qemu_printf("%s", qstring_get_str(value));
Max Reitza8d8ecb2013-10-09 10:46:17 +0200800 break;
801 }
802 case QTYPE_QDICT: {
Max Reitz7dc847e2018-02-24 16:40:29 +0100803 QDict *value = qobject_to(QDict, obj);
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200804 dump_qdict(comp_indent, value);
Max Reitza8d8ecb2013-10-09 10:46:17 +0200805 break;
806 }
807 case QTYPE_QLIST: {
Max Reitz7dc847e2018-02-24 16:40:29 +0100808 QList *value = qobject_to(QList, obj);
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200809 dump_qlist(comp_indent, value);
Max Reitza8d8ecb2013-10-09 10:46:17 +0200810 break;
811 }
Max Reitza8d8ecb2013-10-09 10:46:17 +0200812 case QTYPE_QBOOL: {
Max Reitz7dc847e2018-02-24 16:40:29 +0100813 QBool *value = qobject_to(QBool, obj);
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200814 qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
Max Reitza8d8ecb2013-10-09 10:46:17 +0200815 break;
816 }
Max Reitza8d8ecb2013-10-09 10:46:17 +0200817 default:
818 abort();
819 }
820}
821
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200822static void dump_qlist(int indentation, QList *list)
Max Reitza8d8ecb2013-10-09 10:46:17 +0200823{
824 const QListEntry *entry;
825 int i = 0;
826
827 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
Eric Blake1310a3d2015-12-01 22:20:46 -0700828 QType type = qobject_type(entry->value);
Max Reitza8d8ecb2013-10-09 10:46:17 +0200829 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200830 qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
831 composite ? '\n' : ' ');
832 dump_qobject(indentation + 1, entry->value);
Max Reitza8d8ecb2013-10-09 10:46:17 +0200833 if (!composite) {
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200834 qemu_printf("\n");
Max Reitza8d8ecb2013-10-09 10:46:17 +0200835 }
836 }
837}
838
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200839static void dump_qdict(int indentation, QDict *dict)
Max Reitza8d8ecb2013-10-09 10:46:17 +0200840{
841 const QDictEntry *entry;
842
843 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
Eric Blake1310a3d2015-12-01 22:20:46 -0700844 QType type = qobject_type(entry->value);
Max Reitza8d8ecb2013-10-09 10:46:17 +0200845 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
Peter Xu5eda6222016-03-09 13:56:37 +0800846 char *key = g_malloc(strlen(entry->key) + 1);
Max Reitza8d8ecb2013-10-09 10:46:17 +0200847 int i;
848
849 /* replace dashes with spaces in key (variable) names */
850 for (i = 0; entry->key[i]; i++) {
851 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
852 }
853 key[i] = 0;
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200854 qemu_printf("%*s%s:%c", indentation * 4, "", key,
855 composite ? '\n' : ' ');
856 dump_qobject(indentation + 1, entry->value);
Max Reitza8d8ecb2013-10-09 10:46:17 +0200857 if (!composite) {
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200858 qemu_printf("\n");
Max Reitza8d8ecb2013-10-09 10:46:17 +0200859 }
Peter Xu5eda6222016-03-09 13:56:37 +0800860 g_free(key);
Max Reitza8d8ecb2013-10-09 10:46:17 +0200861 }
862}
863
Hanna Reitz37164702022-06-20 18:26:53 +0200864/*
865 * Return whether dumping the given QObject with dump_qobject() would
866 * yield an empty dump, i.e. not print anything.
867 */
868static bool qobject_is_empty_dump(const QObject *obj)
869{
870 switch (qobject_type(obj)) {
871 case QTYPE_QNUM:
872 case QTYPE_QSTRING:
873 case QTYPE_QBOOL:
874 return false;
875
876 case QTYPE_QDICT:
877 return qdict_size(qobject_to(QDict, obj)) == 0;
878
879 case QTYPE_QLIST:
880 return qlist_empty(qobject_to(QList, obj));
881
882 default:
883 abort();
884 }
885}
886
887/**
888 * Dumps the given ImageInfoSpecific object in a human-readable form,
889 * prepending an optional prefix if the dump is not empty.
890 */
891void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec,
Hanna Reitz76c9e972022-06-20 18:27:00 +0200892 const char *prefix,
893 int indentation)
Max Reitza8d8ecb2013-10-09 10:46:17 +0200894{
Max Reitza8d8ecb2013-10-09 10:46:17 +0200895 QObject *obj, *data;
Daniel P. Berrange7d5e1992016-09-30 15:45:28 +0100896 Visitor *v = qobject_output_visitor_new(&obj);
Max Reitza8d8ecb2013-10-09 10:46:17 +0200897
Eric Blake3b098d52016-06-09 10:48:43 -0600898 visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
899 visit_complete(v, &obj);
Max Reitz7dc847e2018-02-24 16:40:29 +0100900 data = qdict_get(qobject_to(QDict, obj), "data");
Hanna Reitz37164702022-06-20 18:26:53 +0200901 if (!qobject_is_empty_dump(data)) {
902 if (prefix) {
Hanna Reitz76c9e972022-06-20 18:27:00 +0200903 qemu_printf("%*s%s", indentation * 4, "", prefix);
Hanna Reitz37164702022-06-20 18:26:53 +0200904 }
Hanna Reitz76c9e972022-06-20 18:27:00 +0200905 dump_qobject(indentation + 1, data);
Hanna Reitz37164702022-06-20 18:26:53 +0200906 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200907 qobject_unref(obj);
Eric Blake3b098d52016-06-09 10:48:43 -0600908 visit_free(v);
Max Reitza8d8ecb2013-10-09 10:46:17 +0200909}
910
Hanna Reitzd5701772022-06-20 18:27:04 +0200911/**
912 * Print the given @info object in human-readable form. Every field is indented
913 * using the given @indentation (four spaces per indentation level).
914 *
915 * When using this to print a whole block graph, @protocol can be set to true to
916 * signify that the given information is associated with a protocol node, i.e.
917 * just data storage for an image, such that the data it presents is not really
918 * a full VM disk. If so, several fields change name: For example, "virtual
919 * size" is printed as "file length".
920 * (Consider a qcow2 image, which is represented by a qcow2 node and a file
921 * node. Printing a "virtual size" for the file node does not make sense,
922 * because without the qcow2 node, it is not really a guest disk, so it does not
923 * have a "virtual size". Therefore, we call it "file length" instead.)
924 *
925 * @protocol is ignored when @indentation is 0, because we take that to mean
926 * that the associated node is the root node in the queried block graph, and
927 * thus is always to be interpreted as a standalone guest disk.
928 */
929void bdrv_node_info_dump(BlockNodeInfo *info, int indentation, bool protocol)
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800930{
Eric Blakede38b502019-04-17 12:11:01 -0500931 char *size_buf, *dsize_buf;
Hanna Reitz76c9e972022-06-20 18:27:00 +0200932 g_autofree char *ind_s = g_strdup_printf("%*s", indentation * 4, "");
933
Hanna Reitzd5701772022-06-20 18:27:04 +0200934 if (indentation == 0) {
935 /* Top level, consider this a normal image */
936 protocol = false;
937 }
938
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800939 if (!info->has_actual_size) {
Eric Blakede38b502019-04-17 12:11:01 -0500940 dsize_buf = g_strdup("unavailable");
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800941 } else {
Eric Blakede38b502019-04-17 12:11:01 -0500942 dsize_buf = size_to_str(info->actual_size);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800943 }
Eric Blakede38b502019-04-17 12:11:01 -0500944 size_buf = size_to_str(info->virtual_size);
Hanna Reitzd5701772022-06-20 18:27:04 +0200945 qemu_printf("%s%s: %s\n"
946 "%s%s: %s\n"
947 "%s%s: %s (%" PRId64 " bytes)\n"
Hanna Reitz76c9e972022-06-20 18:27:00 +0200948 "%sdisk size: %s\n",
Hanna Reitzd5701772022-06-20 18:27:04 +0200949 ind_s, protocol ? "filename" : "image", info->filename,
950 ind_s, protocol ? "protocol type" : "file format",
951 info->format,
952 ind_s, protocol ? "file length" : "virtual size",
953 size_buf, info->virtual_size,
Hanna Reitz76c9e972022-06-20 18:27:00 +0200954 ind_s, dsize_buf);
Eric Blakede38b502019-04-17 12:11:01 -0500955 g_free(size_buf);
956 g_free(dsize_buf);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800957
958 if (info->has_encrypted && info->encrypted) {
Hanna Reitz76c9e972022-06-20 18:27:00 +0200959 qemu_printf("%sencrypted: yes\n", ind_s);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800960 }
961
962 if (info->has_cluster_size) {
Hanna Reitz76c9e972022-06-20 18:27:00 +0200963 qemu_printf("%scluster_size: %" PRId64 "\n",
964 ind_s, info->cluster_size);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800965 }
966
967 if (info->has_dirty_flag && info->dirty_flag) {
Hanna Reitz76c9e972022-06-20 18:27:00 +0200968 qemu_printf("%scleanly shut down: no\n", ind_s);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800969 }
970
Markus Armbruster54fde4f2022-11-04 17:06:52 +0100971 if (info->backing_filename) {
Hanna Reitz76c9e972022-06-20 18:27:00 +0200972 qemu_printf("%sbacking file: %s", ind_s, info->backing_filename);
Markus Armbruster54fde4f2022-11-04 17:06:52 +0100973 if (!info->full_backing_filename) {
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200974 qemu_printf(" (cannot determine actual path)");
John Snow5c9d9ca2015-12-14 14:55:14 -0500975 } else if (strcmp(info->backing_filename,
976 info->full_backing_filename) != 0) {
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200977 qemu_printf(" (actual path: %s)", info->full_backing_filename);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800978 }
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200979 qemu_printf("\n");
Markus Armbruster54fde4f2022-11-04 17:06:52 +0100980 if (info->backing_filename_format) {
Hanna Reitz76c9e972022-06-20 18:27:00 +0200981 qemu_printf("%sbacking file format: %s\n",
982 ind_s, info->backing_filename_format);
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800983 }
984 }
985
986 if (info->has_snapshots) {
987 SnapshotInfoList *elem;
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800988
Hanna Reitz76c9e972022-06-20 18:27:00 +0200989 qemu_printf("%sSnapshot list:\n", ind_s);
990 qemu_printf("%s", ind_s);
Markus Armbrustere1ce7d72019-04-17 21:17:55 +0200991 bdrv_snapshot_dump(NULL);
992 qemu_printf("\n");
Wenchao Xiaf364ec62013-05-25 11:09:44 +0800993
994 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
995 * we convert to the block layer's native QEMUSnapshotInfo for now.
996 */
997 for (elem = info->snapshots; elem; elem = elem->next) {
998 QEMUSnapshotInfo sn = {
999 .vm_state_size = elem->value->vm_state_size,
1000 .date_sec = elem->value->date_sec,
1001 .date_nsec = elem->value->date_nsec,
1002 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
1003 elem->value->vm_clock_nsec,
Pavel Dovgalyukb39847a2020-10-03 20:13:08 +03001004 .icount = elem->value->has_icount ?
1005 elem->value->icount : -1ULL,
Wenchao Xiaf364ec62013-05-25 11:09:44 +08001006 };
1007
1008 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
1009 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
Hanna Reitz76c9e972022-06-20 18:27:00 +02001010 qemu_printf("%s", ind_s);
Markus Armbrustere1ce7d72019-04-17 21:17:55 +02001011 bdrv_snapshot_dump(&sn);
1012 qemu_printf("\n");
Wenchao Xiaf364ec62013-05-25 11:09:44 +08001013 }
1014 }
Max Reitza8d8ecb2013-10-09 10:46:17 +02001015
Markus Armbruster54fde4f2022-11-04 17:06:52 +01001016 if (info->format_specific) {
Hanna Reitz37164702022-06-20 18:26:53 +02001017 bdrv_image_info_specific_dump(info->format_specific,
Hanna Reitz76c9e972022-06-20 18:27:00 +02001018 "Format specific information:\n",
1019 indentation);
Max Reitza8d8ecb2013-10-09 10:46:17 +02001020 }
Wenchao Xiaf364ec62013-05-25 11:09:44 +08001021}