]> git.proxmox.com Git - mirror_qemu.git/blame - block/qapi.c
block: remove AioContext locking
[mirror_qemu.git] / block / qapi.c
CommitLineData
f364ec65
WX
1/*
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
80c71a24 25#include "qemu/osdep.h"
31e40415 26#include "qemu/cutils.h"
f364ec65
WX
27#include "block/qapi.h"
28#include "block/block_int.h"
e2c1c34f 29#include "block/dirty-bitmap.h"
76f4afb4 30#include "block/throttle-groups.h"
e2462113 31#include "block/write-threshold.h"
e688df6b 32#include "qapi/error.h"
9af23989 33#include "qapi/qapi-commands-block-core.h"
b3db211f 34#include "qapi/qobject-output-visitor.h"
9af23989 35#include "qapi/qapi-visit-block-core.h"
6b673957 36#include "qapi/qmp/qbool.h"
452fcdbc 37#include "qapi/qmp/qdict.h"
47e6b297 38#include "qapi/qmp/qlist.h"
15280c36 39#include "qapi/qmp/qnum.h"
6b673957 40#include "qapi/qmp/qstring.h"
e1ce7d74 41#include "qemu/qemu-print.h"
d829a211 42#include "sysemu/block-backend.h"
f364ec65 43
c83f9fba 44BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
facda544
PK
45 BlockDriverState *bs,
46 bool flat,
47 Error **errp)
c13163fb 48{
d5a8ee60 49 ImageInfo **p_image_info;
5d881359 50 ImageInfo *backing_info;
c3b29ae6 51 BlockDriverState *backing;
d470ad42 52 BlockDeviceInfo *info;
5d881359 53 ERRP_GUARD();
c13163fb 54
d470ad42
HR
55 if (!bs->drv) {
56 error_setg(errp, "Block device %s is ejected", bs->node_name);
57 return NULL;
58 }
59
f30c66ba
HR
60 bdrv_refresh_filename(bs);
61
d470ad42 62 info = g_malloc0(sizeof(*info));
c13163fb 63 info->file = g_strdup(bs->filename);
307261b2 64 info->ro = bdrv_is_read_only(bs);
c13163fb
BC
65 info->drv = g_strdup(bs->drv->format_name);
66 info->encrypted = bs->encrypted;
c13163fb 67
9e193c5a
KW
68 info->cache = g_new(BlockdevCacheInfo, 1);
69 *info->cache = (BlockdevCacheInfo) {
c83f9fba 70 .writeback = blk ? blk_enable_write_cache(blk) : true,
9e193c5a
KW
71 .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
72 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
73 };
74
c13163fb 75 if (bs->node_name[0]) {
c13163fb
BC
76 info->node_name = g_strdup(bs->node_name);
77 }
78
0b877d09
HR
79 backing = bdrv_cow_bs(bs);
80 if (backing) {
0b877d09 81 info->backing_file = g_strdup(backing->filename);
c13163fb
BC
82 }
83
590a63d5
VSO
84 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
85 info->has_dirty_bitmaps = true;
86 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
87 }
88
465bee1d 89 info->detect_zeroes = bs->detect_zeroes;
c13163fb 90
022cdc9f 91 if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
c13163fb 92 ThrottleConfig cfg;
022cdc9f 93 BlockBackendPublic *blkp = blk_get_public(blk);
76f4afb4 94
022cdc9f 95 throttle_group_get_config(&blkp->throttle_group_member, &cfg);
76f4afb4 96
c13163fb
BC
97 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
398befdf
AG
119 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
c13163fb
BC
139 info->has_iops_size = cfg.op_size;
140 info->iops_size = cfg.op_size;
b8fe1694 141
022cdc9f
MP
142 info->group =
143 g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
c13163fb
BC
144 }
145
e2462113
FR
146 info->write_threshold = bdrv_write_threshold_get(bs);
147
d5a8ee60 148 p_image_info = &info->image;
d3c8c674 149 info->backing_file_depth = 0;
facda544 150
5d881359
HR
151 /*
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 */
c3b29ae6 155 bdrv_query_image_info(bs, p_image_info, flat, blk != NULL, errp);
5d881359
HR
156 if (*errp) {
157 qapi_free_BlockDeviceInfo(info);
158 return NULL;
159 }
d3c8c674 160
5d881359
HR
161 backing_info = info->image->backing_image;
162 while (backing_info) {
163 info->backing_file_depth++;
164 backing_info = backing_info->backing_image;
d5a8ee60
AG
165 }
166
c13163fb
BC
167 return info;
168}
169
fb0ed453
WX
170/*
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)
f364ec65
WX
178{
179 int i, sn_count;
180 QEMUSnapshotInfo *sn_tab = NULL;
95b3a8c8 181 SnapshotInfoList *head = NULL, **tail = &head;
fb0ed453
WX
182 SnapshotInfo *info;
183
f364ec65 184 sn_count = bdrv_snapshot_list(bs, &sn_tab);
fb0ed453
WX
185 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 }
f364ec65
WX
203
204 for (i = 0; i < sn_count; i++) {
fb0ed453
WX
205 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;
b39847a5
PD
213 info->icount = sn_tab[i].icount;
214 info->has_icount = sn_tab[i].icount != -1ULL;
f364ec65 215
95b3a8c8 216 QAPI_LIST_APPEND(tail, info);
f364ec65
WX
217 }
218
219 g_free(sn_tab);
fb0ed453
WX
220 *p_list = head;
221 return 0;
f364ec65
WX
222}
223
43526ec8 224/**
a2085f89
HR
225 * Helper function for other query info functions. Store information about @bs
226 * in @info, setting @errp on error.
43526ec8 227 */
b7cfc7d5
KW
228static void GRAPH_RDLOCK
229bdrv_do_query_node_info(BlockDriverState *bs, BlockNodeInfo *info, Error **errp)
f364ec65 230{
52bf1e72 231 int64_t size;
43526ec8 232 const char *backing_filename;
f364ec65 233 BlockDriverInfo bdi;
43526ec8
WX
234 int ret;
235 Error *err = NULL;
f364ec65 236
52bf1e72
MA
237 size = bdrv_getlength(bs);
238 if (size < 0) {
9adceb02
FZ
239 error_setg_errno(errp, -size, "Can't get image size '%s'",
240 bs->exact_filename);
b49f4755 241 return;
52bf1e72 242 }
f364ec65 243
f30c66ba
HR
244 bdrv_refresh_filename(bs);
245
43526ec8 246 info->filename = g_strdup(bs->filename);
f364ec65 247 info->format = g_strdup(bdrv_get_format_name(bs));
52bf1e72 248 info->virtual_size = size;
f364ec65
WX
249 info->actual_size = bdrv_get_allocated_file_size(bs);
250 info->has_actual_size = info->actual_size >= 0;
8b8277cd 251 if (bs->encrypted) {
f364ec65
WX
252 info->encrypted = true;
253 info->has_encrypted = true;
254 }
255 if (bdrv_get_info(bs, &bdi) >= 0) {
256 if (bdi.cluster_size != 0) {
257 info->cluster_size = bdi.cluster_size;
258 info->has_cluster_size = true;
259 }
260 info->dirty_flag = bdi.is_dirty;
261 info->has_dirty_flag = true;
262 }
1bf6e9ca
AS
263 info->format_specific = bdrv_get_specific_info(bs, &err);
264 if (err) {
265 error_propagate(errp, err);
b49f4755 266 return;
1bf6e9ca 267 }
43526ec8 268 backing_filename = bs->backing_file;
f364ec65 269 if (backing_filename[0] != '\0') {
6b6833c1 270 char *backing_filename2;
0b877d09 271
f364ec65 272 info->backing_filename = g_strdup(backing_filename);
6b6833c1 273 backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
f364ec65 274
12dcb1c0
JS
275 /* Always report the full_backing_filename if present, even if it's the
276 * same as backing_filename. That they are same is useful info. */
277 if (backing_filename2) {
278 info->full_backing_filename = g_strdup(backing_filename2);
f364ec65
WX
279 }
280
281 if (bs->backing_format[0]) {
282 info->backing_filename_format = g_strdup(bs->backing_format);
f364ec65 283 }
564d64bd 284 g_free(backing_filename2);
f364ec65 285 }
43526ec8
WX
286
287 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
288 switch (ret) {
289 case 0:
290 if (info->snapshots) {
291 info->has_snapshots = true;
292 }
293 break;
294 /* recoverable error */
295 case -ENOMEDIUM:
296 case -ENOTSUP:
297 error_free(err);
298 break;
299 default:
300 error_propagate(errp, err);
b49f4755 301 return;
43526ec8 302 }
f364ec65
WX
303}
304
a2085f89
HR
305/**
306 * bdrv_query_image_info:
307 * @bs: block node to examine
308 * @p_info: location to store image information
5d881359
HR
309 * @flat: skip backing node information
310 * @skip_implicit_filters: skip implicit filters in the backing chain
a2085f89
HR
311 * @errp: location to store error information
312 *
5d881359
HR
313 * Store image information in @p_info, potentially recursively covering the
314 * backing chain.
315 *
316 * If @flat is true, do not query backing image information, i.e.
317 * (*p_info)->has_backing_image will be set to false and
318 * (*p_info)->backing_image to NULL even when the image does in fact have a
319 * backing image.
a2085f89 320 *
5d881359
HR
321 * If @skip_implicit_filters is true, implicit filter nodes in the backing chain
322 * will be skipped when querying backing image information.
323 * (@skip_implicit_filters is ignored when @flat is true.)
a2085f89
HR
324 *
325 * @p_info will be set only on success. On error, store error in @errp.
326 */
327void bdrv_query_image_info(BlockDriverState *bs,
328 ImageInfo **p_info,
5d881359
HR
329 bool flat,
330 bool skip_implicit_filters,
a2085f89
HR
331 Error **errp)
332{
333 ImageInfo *info;
334 ERRP_GUARD();
335
336 info = g_new0(ImageInfo, 1);
337 bdrv_do_query_node_info(bs, qapi_ImageInfo_base(info), errp);
338 if (*errp) {
5d881359
HR
339 goto fail;
340 }
341
342 if (!flat) {
343 BlockDriverState *backing;
344
345 /*
346 * Use any filtered child here (for backwards compatibility to when
347 * we always took bs->backing, which might be any filtered child).
348 */
349 backing = bdrv_filter_or_cow_bs(bs);
350 if (skip_implicit_filters) {
351 backing = bdrv_skip_implicit_filters(backing);
352 }
353
354 if (backing) {
355 bdrv_query_image_info(backing, &info->backing_image, false,
356 skip_implicit_filters, errp);
357 if (*errp) {
358 goto fail;
359 }
360 }
a2085f89
HR
361 }
362
363 *p_info = info;
5d881359
HR
364 return;
365
366fail:
367 assert(*errp);
368 qapi_free_ImageInfo(info);
a2085f89
HR
369}
370
6cab3399
HR
371/**
372 * bdrv_query_block_graph_info:
373 * @bs: root node to start from
374 * @p_info: location to store image information
375 * @errp: location to store error information
376 *
377 * Store image information about the graph starting from @bs in @p_info.
378 *
379 * @p_info will be set only on success. On error, store error in @errp.
380 */
381void bdrv_query_block_graph_info(BlockDriverState *bs,
382 BlockGraphInfo **p_info,
383 Error **errp)
384{
385 BlockGraphInfo *info;
386 BlockChildInfoList **children_list_tail;
387 BdrvChild *c;
388 ERRP_GUARD();
389
390 info = g_new0(BlockGraphInfo, 1);
391 bdrv_do_query_node_info(bs, qapi_BlockGraphInfo_base(info), errp);
392 if (*errp) {
393 goto fail;
394 }
395
396 children_list_tail = &info->children;
397
398 QLIST_FOREACH(c, &bs->children, next) {
399 BlockChildInfo *c_info;
400
401 c_info = g_new0(BlockChildInfo, 1);
402 QAPI_LIST_APPEND(children_list_tail, c_info);
403
404 c_info->name = g_strdup(c->name);
405 bdrv_query_block_graph_info(c->bs, &c_info->info, errp);
406 if (*errp) {
407 goto fail;
408 }
409 }
410
411 *p_info = info;
412 return;
413
414fail:
415 assert(*errp != NULL);
416 qapi_free_BlockGraphInfo(info);
417}
418
553a7e87 419/* @p_info will be set only on success. */
b7cfc7d5
KW
420static void GRAPH_RDLOCK
421bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, Error **errp)
f364ec65
WX
422{
423 BlockInfo *info = g_malloc0(sizeof(*info));
d829a211 424 BlockDriverState *bs = blk_bs(blk);
46eade7b
KW
425 char *qdev;
426
d3c8c674 427 /* Skip automatically inserted nodes that the user isn't aware of */
0a7585db 428 bs = bdrv_skip_implicit_filters(bs);
d3c8c674 429
d829a211 430 info->device = g_strdup(blk_name(blk));
f364ec65 431 info->type = g_strdup("unknown");
a7f53e26
MA
432 info->locked = blk_dev_is_medium_locked(blk);
433 info->removable = blk_dev_has_removable_media(blk);
f364ec65 434
46eade7b
KW
435 qdev = blk_get_attached_dev_id(blk);
436 if (qdev && *qdev) {
46eade7b
KW
437 info->qdev = qdev;
438 } else {
439 g_free(qdev);
440 }
441
327032ce 442 if (blk_dev_has_tray(blk)) {
f364ec65 443 info->has_tray_open = true;
a7f53e26 444 info->tray_open = blk_dev_is_tray_open(blk);
f364ec65
WX
445 }
446
373340b2 447 if (blk_iostatus_is_enabled(blk)) {
f364ec65 448 info->has_io_status = true;
373340b2 449 info->io_status = blk_iostatus(blk);
f364ec65
WX
450 }
451
5433c24f 452 if (bs && bs->drv) {
facda544 453 info->inserted = bdrv_block_device_info(blk, bs, false, errp);
d5a8ee60
AG
454 if (info->inserted == NULL) {
455 goto err;
553a7e87 456 }
f364ec65 457 }
553a7e87
WX
458
459 *p_info = info;
460 return;
461
462 err:
463 qapi_free_BlockInfo(info);
f364ec65
WX
464}
465
7e5c776d
VSO
466static uint64List *uint64_list(uint64_t *list, int size)
467{
468 int i;
469 uint64List *out_list = NULL;
c3033fd3 470 uint64List **tail = &out_list;
7e5c776d
VSO
471
472 for (i = 0; i < size; i++) {
c3033fd3 473 QAPI_LIST_APPEND(tail, list[i]);
7e5c776d
VSO
474 }
475
7e5c776d
VSO
476 return out_list;
477}
478
54fde4ff
MA
479static BlockLatencyHistogramInfo *
480bdrv_latency_histogram_stats(BlockLatencyHistogram *hist)
7e5c776d 481{
54fde4ff 482 BlockLatencyHistogramInfo *info;
7e5c776d 483
54fde4ff
MA
484 if (!hist->bins) {
485 return NULL;
7e5c776d 486 }
54fde4ff
MA
487
488 info = g_new0(BlockLatencyHistogramInfo, 1);
489 info->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
490 info->bins = uint64_list(hist->bins, hist->nbins);
491 return info;
7e5c776d
VSO
492}
493
54302156 494static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
2b77e60a
KW
495{
496 BlockAcctStats *stats = blk_get_stats(blk);
497 BlockAcctTimedStats *ts = NULL;
54fde4ff 498 BlockLatencyHistogram *hgram;
2b77e60a 499
54302156
HR
500 ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
501 ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
52eb76f4 502 ds->zone_append_bytes = stats->nr_bytes[BLOCK_ACCT_ZONE_APPEND];
159f85dd 503 ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
54302156
HR
504 ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
505 ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
52eb76f4 506 ds->zone_append_operations = stats->nr_ops[BLOCK_ACCT_ZONE_APPEND];
159f85dd 507 ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
2b77e60a 508
54302156
HR
509 ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
510 ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
52eb76f4
SL
511 ds->failed_zone_append_operations =
512 stats->failed_ops[BLOCK_ACCT_ZONE_APPEND];
54302156 513 ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
159f85dd 514 ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
2b77e60a 515
54302156
HR
516 ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
517 ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
52eb76f4
SL
518 ds->invalid_zone_append_operations =
519 stats->invalid_ops[BLOCK_ACCT_ZONE_APPEND];
54302156 520 ds->invalid_flush_operations =
2b77e60a 521 stats->invalid_ops[BLOCK_ACCT_FLUSH];
159f85dd 522 ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
2b77e60a 523
54302156
HR
524 ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
525 ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
52eb76f4 526 ds->zone_append_merged = stats->merged[BLOCK_ACCT_ZONE_APPEND];
159f85dd 527 ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
54302156
HR
528 ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
529 ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
52eb76f4
SL
530 ds->zone_append_total_time_ns =
531 stats->total_time_ns[BLOCK_ACCT_ZONE_APPEND];
54302156
HR
532 ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
533 ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
159f85dd 534 ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
2b77e60a 535
54302156
HR
536 ds->has_idle_time_ns = stats->last_access_time_ns > 0;
537 if (ds->has_idle_time_ns) {
538 ds->idle_time_ns = block_acct_idle_time_ns(stats);
2b77e60a
KW
539 }
540
54302156
HR
541 ds->account_invalid = stats->account_invalid;
542 ds->account_failed = stats->account_failed;
2b77e60a
KW
543
544 while ((ts = block_acct_interval_next(stats, ts))) {
2b77e60a 545 BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
2b77e60a
KW
546
547 TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
548 TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
52eb76f4 549 TimedAverage *zap = &ts->latency[BLOCK_ACCT_ZONE_APPEND];
2b77e60a
KW
550 TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
551
552 dev_stats->interval_length = ts->interval_length;
553
554 dev_stats->min_rd_latency_ns = timed_average_min(rd);
555 dev_stats->max_rd_latency_ns = timed_average_max(rd);
556 dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
557
558 dev_stats->min_wr_latency_ns = timed_average_min(wr);
559 dev_stats->max_wr_latency_ns = timed_average_max(wr);
560 dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
561
52eb76f4
SL
562 dev_stats->min_zone_append_latency_ns = timed_average_min(zap);
563 dev_stats->max_zone_append_latency_ns = timed_average_max(zap);
564 dev_stats->avg_zone_append_latency_ns = timed_average_avg(zap);
565
2b77e60a
KW
566 dev_stats->min_flush_latency_ns = timed_average_min(fl);
567 dev_stats->max_flush_latency_ns = timed_average_max(fl);
568 dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
569
570 dev_stats->avg_rd_queue_depth =
571 block_acct_queue_depth(ts, BLOCK_ACCT_READ);
572 dev_stats->avg_wr_queue_depth =
573 block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
52eb76f4
SL
574 dev_stats->avg_zone_append_queue_depth =
575 block_acct_queue_depth(ts, BLOCK_ACCT_ZONE_APPEND);
54aa3de7
EB
576
577 QAPI_LIST_PREPEND(ds->timed_stats, dev_stats);
2b77e60a 578 }
7e5c776d 579
54fde4ff
MA
580 hgram = stats->latency_histogram;
581 ds->rd_latency_histogram
582 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_READ]);
583 ds->wr_latency_histogram
584 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_WRITE]);
52eb76f4
SL
585 ds->zone_append_latency_histogram
586 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_ZONE_APPEND]);
54fde4ff
MA
587 ds->flush_latency_histogram
588 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_FLUSH]);
2b77e60a
KW
589}
590
6ec75a6a
KW
591static BlockStats * GRAPH_RDLOCK
592bdrv_query_bds_stats(BlockDriverState *bs, bool blk_level)
f364ec65 593{
3f26191c 594 BdrvChild *parent_child;
0a7585db 595 BlockDriverState *filter_or_cow_bs;
20a6d768
DL
596 BlockStats *s = NULL;
597
598 s = g_malloc0(sizeof(*s));
599 s->stats = g_malloc0(sizeof(*s->stats));
600
601 if (!bs) {
602 return s;
603 }
604
d3c8c674
KW
605 /* Skip automatically inserted nodes that the user isn't aware of in
606 * a BlockBackend-level command. Stay at the exact node for a node-level
607 * command. */
0a7585db
HR
608 if (blk_level) {
609 bs = bdrv_skip_implicit_filters(bs);
d3c8c674
KW
610 }
611
4875a779 612 if (bdrv_get_node_name(bs)[0]) {
4875a779
FZ
613 s->node_name = g_strdup(bdrv_get_node_name(bs));
614 }
615
f7946da2 616 s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
53d8f9d8 617
d9245599 618 s->driver_specific = bdrv_get_specific_stats(bs);
d9245599 619
3f26191c
HR
620 parent_child = bdrv_primary_child(bs);
621 if (!parent_child ||
622 !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED)))
623 {
624 BdrvChild *c;
625
626 /*
627 * Look for a unique data-storing child. We do not need to look for
628 * filtered children, as there would be only one and it would have been
629 * the primary child.
630 */
631 parent_child = NULL;
632 QLIST_FOREACH(c, &bs->children, next) {
633 if (c->role & BDRV_CHILD_DATA) {
634 if (parent_child) {
635 /*
636 * There are multiple data-storing children and we cannot
637 * choose between them.
638 */
639 parent_child = NULL;
640 break;
641 }
642 parent_child = c;
643 }
644 }
645 }
646 if (parent_child) {
3f26191c 647 s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level);
f364ec65
WX
648 }
649
0a7585db
HR
650 filter_or_cow_bs = bdrv_filter_or_cow_bs(bs);
651 if (blk_level && filter_or_cow_bs) {
652 /*
653 * Put any filtered or COW child here (for backwards
654 * compatibility to when we put bs0->backing here, which might
655 * be either)
656 */
0a7585db 657 s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level);
c8059b97
FZ
658 }
659
20a6d768 660 return s;
b07363a1
KW
661}
662
f364ec65
WX
663BlockInfoList *qmp_query_block(Error **errp)
664{
665 BlockInfoList *head = NULL, **p_next = &head;
d829a211 666 BlockBackend *blk;
553a7e87 667 Error *local_err = NULL;
f364ec65 668
b7cfc7d5
KW
669 GRAPH_RDLOCK_GUARD_MAINLOOP();
670
d5b68844
KW
671 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
672 BlockInfoList *info;
673
ec18b0a9 674 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
d5b68844
KW
675 continue;
676 }
677
678 info = g_malloc0(sizeof(*info));
d829a211 679 bdrv_query_info(blk, &info->value, &local_err);
84d18f06 680 if (local_err) {
553a7e87 681 error_propagate(errp, local_err);
903c341d
MA
682 g_free(info);
683 qapi_free_BlockInfoList(head);
684 return NULL;
553a7e87 685 }
f364ec65
WX
686
687 *p_next = info;
688 p_next = &info->next;
689 }
690
691 return head;
692}
693
f71eaa74
FZ
694BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
695 bool query_nodes,
696 Error **errp)
f364ec65 697{
c3033fd3 698 BlockStatsList *head = NULL, **tail = &head;
a6baa608
DL
699 BlockBackend *blk;
700 BlockDriverState *bs;
f364ec65 701
6ec75a6a
KW
702 GRAPH_RDLOCK_GUARD_MAINLOOP();
703
f71eaa74 704 /* Just to be safe if query_nodes is not always initialized */
a6baa608
DL
705 if (has_query_nodes && query_nodes) {
706 for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
c3033fd3 707 QAPI_LIST_APPEND(tail, bdrv_query_bds_stats(bs, false));
a6baa608
DL
708 }
709 } else {
567dcb31 710 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
a6baa608 711 BlockStats *s;
5a9cb5a9 712 char *qdev;
a6baa608 713
567dcb31
KW
714 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
715 continue;
716 }
717
a6baa608 718 s = bdrv_query_bds_stats(blk_bs(blk), true);
a6baa608 719 s->device = g_strdup(blk_name(blk));
5a9cb5a9
KW
720
721 qdev = blk_get_attached_dev_id(blk);
722 if (qdev && *qdev) {
5a9cb5a9
KW
723 s->qdev = qdev;
724 } else {
725 g_free(qdev);
726 }
727
a6baa608 728 bdrv_query_blk_stats(s->stats, blk);
a6baa608 729
c3033fd3 730 QAPI_LIST_APPEND(tail, s);
a6baa608 731 }
f364ec65
WX
732 }
733
734 return head;
735}
736
e1ce7d74 737void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
f364ec65 738{
39683553 739 char clock_buf[128];
b39847a5 740 char icount_buf[128] = {0};
f364ec65 741 int64_t secs;
de38b500 742 char *sizing = NULL;
f364ec65
WX
743
744 if (!sn) {
26513a01 745 qemu_printf("%-10s%-17s%8s%20s%13s%11s",
b39847a5 746 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT");
f364ec65 747 } else {
39683553
DB
748 g_autoptr(GDateTime) date = g_date_time_new_from_unix_local(sn->date_sec);
749 g_autofree char *date_buf = g_date_time_format(date, "%Y-%m-%d %H:%M:%S");
750
f364ec65
WX
751 secs = sn->vm_clock_nsec / 1000000000;
752 snprintf(clock_buf, sizeof(clock_buf),
753 "%02d:%02d:%02d.%03d",
754 (int)(secs / 3600),
755 (int)((secs / 60) % 60),
756 (int)(secs % 60),
757 (int)((sn->vm_clock_nsec / 1000000) % 1000));
de38b500 758 sizing = size_to_str(sn->vm_state_size);
b39847a5
PD
759 if (sn->icount != -1ULL) {
760 snprintf(icount_buf, sizeof(icount_buf),
761 "%"PRId64, sn->icount);
762 }
26513a01 763 qemu_printf("%-9s %-16s %8s%20s%13s%11s",
e1ce7d74 764 sn->id_str, sn->name,
de38b500 765 sizing,
e1ce7d74 766 date_buf,
b39847a5
PD
767 clock_buf,
768 icount_buf);
f364ec65 769 }
de38b500 770 g_free(sizing);
f364ec65
WX
771}
772
e1ce7d74
MA
773static void dump_qdict(int indentation, QDict *dict);
774static void dump_qlist(int indentation, QList *list);
a8d8ecb7 775
e1ce7d74 776static void dump_qobject(int comp_indent, QObject *obj)
a8d8ecb7
HR
777{
778 switch (qobject_type(obj)) {
01b2ffce 779 case QTYPE_QNUM: {
7dc847eb 780 QNum *value = qobject_to(QNum, obj);
01b2ffce 781 char *tmp = qnum_to_string(value);
e1ce7d74 782 qemu_printf("%s", tmp);
01b2ffce 783 g_free(tmp);
a8d8ecb7
HR
784 break;
785 }
786 case QTYPE_QSTRING: {
7dc847eb 787 QString *value = qobject_to(QString, obj);
e1ce7d74 788 qemu_printf("%s", qstring_get_str(value));
a8d8ecb7
HR
789 break;
790 }
791 case QTYPE_QDICT: {
7dc847eb 792 QDict *value = qobject_to(QDict, obj);
e1ce7d74 793 dump_qdict(comp_indent, value);
a8d8ecb7
HR
794 break;
795 }
796 case QTYPE_QLIST: {
7dc847eb 797 QList *value = qobject_to(QList, obj);
e1ce7d74 798 dump_qlist(comp_indent, value);
a8d8ecb7
HR
799 break;
800 }
a8d8ecb7 801 case QTYPE_QBOOL: {
7dc847eb 802 QBool *value = qobject_to(QBool, obj);
e1ce7d74 803 qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
a8d8ecb7
HR
804 break;
805 }
a8d8ecb7
HR
806 default:
807 abort();
808 }
809}
810
e1ce7d74 811static void dump_qlist(int indentation, QList *list)
a8d8ecb7
HR
812{
813 const QListEntry *entry;
814 int i = 0;
815
816 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
1310a3d3 817 QType type = qobject_type(entry->value);
a8d8ecb7 818 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
e1ce7d74
MA
819 qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
820 composite ? '\n' : ' ');
821 dump_qobject(indentation + 1, entry->value);
a8d8ecb7 822 if (!composite) {
e1ce7d74 823 qemu_printf("\n");
a8d8ecb7
HR
824 }
825 }
826}
827
e1ce7d74 828static void dump_qdict(int indentation, QDict *dict)
a8d8ecb7
HR
829{
830 const QDictEntry *entry;
831
832 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
1310a3d3 833 QType type = qobject_type(entry->value);
a8d8ecb7 834 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
5eda6227 835 char *key = g_malloc(strlen(entry->key) + 1);
a8d8ecb7
HR
836 int i;
837
838 /* replace dashes with spaces in key (variable) names */
839 for (i = 0; entry->key[i]; i++) {
840 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
841 }
842 key[i] = 0;
e1ce7d74
MA
843 qemu_printf("%*s%s:%c", indentation * 4, "", key,
844 composite ? '\n' : ' ');
845 dump_qobject(indentation + 1, entry->value);
a8d8ecb7 846 if (!composite) {
e1ce7d74 847 qemu_printf("\n");
a8d8ecb7 848 }
5eda6227 849 g_free(key);
a8d8ecb7
HR
850 }
851}
852
3716470b
HR
853/*
854 * Return whether dumping the given QObject with dump_qobject() would
855 * yield an empty dump, i.e. not print anything.
856 */
857static bool qobject_is_empty_dump(const QObject *obj)
858{
859 switch (qobject_type(obj)) {
860 case QTYPE_QNUM:
861 case QTYPE_QSTRING:
862 case QTYPE_QBOOL:
863 return false;
864
865 case QTYPE_QDICT:
866 return qdict_size(qobject_to(QDict, obj)) == 0;
867
868 case QTYPE_QLIST:
869 return qlist_empty(qobject_to(QList, obj));
870
871 default:
872 abort();
873 }
874}
875
876/**
877 * Dumps the given ImageInfoSpecific object in a human-readable form,
878 * prepending an optional prefix if the dump is not empty.
879 */
880void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec,
76c9e975
HR
881 const char *prefix,
882 int indentation)
a8d8ecb7 883{
a8d8ecb7 884 QObject *obj, *data;
7d5e199a 885 Visitor *v = qobject_output_visitor_new(&obj);
a8d8ecb7 886
3b098d56
EB
887 visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
888 visit_complete(v, &obj);
7dc847eb 889 data = qdict_get(qobject_to(QDict, obj), "data");
3716470b
HR
890 if (!qobject_is_empty_dump(data)) {
891 if (prefix) {
76c9e975 892 qemu_printf("%*s%s", indentation * 4, "", prefix);
3716470b 893 }
76c9e975 894 dump_qobject(indentation + 1, data);
3716470b 895 }
cb3e7f08 896 qobject_unref(obj);
3b098d56 897 visit_free(v);
a8d8ecb7
HR
898}
899
d570177b
HR
900/**
901 * Print the given @info object in human-readable form. Every field is indented
902 * using the given @indentation (four spaces per indentation level).
903 *
904 * When using this to print a whole block graph, @protocol can be set to true to
905 * signify that the given information is associated with a protocol node, i.e.
906 * just data storage for an image, such that the data it presents is not really
907 * a full VM disk. If so, several fields change name: For example, "virtual
908 * size" is printed as "file length".
909 * (Consider a qcow2 image, which is represented by a qcow2 node and a file
910 * node. Printing a "virtual size" for the file node does not make sense,
911 * because without the qcow2 node, it is not really a guest disk, so it does not
912 * have a "virtual size". Therefore, we call it "file length" instead.)
913 *
914 * @protocol is ignored when @indentation is 0, because we take that to mean
915 * that the associated node is the root node in the queried block graph, and
916 * thus is always to be interpreted as a standalone guest disk.
917 */
918void bdrv_node_info_dump(BlockNodeInfo *info, int indentation, bool protocol)
f364ec65 919{
de38b500 920 char *size_buf, *dsize_buf;
76c9e975
HR
921 g_autofree char *ind_s = g_strdup_printf("%*s", indentation * 4, "");
922
d570177b
HR
923 if (indentation == 0) {
924 /* Top level, consider this a normal image */
925 protocol = false;
926 }
927
f364ec65 928 if (!info->has_actual_size) {
de38b500 929 dsize_buf = g_strdup("unavailable");
f364ec65 930 } else {
de38b500 931 dsize_buf = size_to_str(info->actual_size);
f364ec65 932 }
de38b500 933 size_buf = size_to_str(info->virtual_size);
d570177b
HR
934 qemu_printf("%s%s: %s\n"
935 "%s%s: %s\n"
936 "%s%s: %s (%" PRId64 " bytes)\n"
76c9e975 937 "%sdisk size: %s\n",
d570177b
HR
938 ind_s, protocol ? "filename" : "image", info->filename,
939 ind_s, protocol ? "protocol type" : "file format",
940 info->format,
941 ind_s, protocol ? "file length" : "virtual size",
942 size_buf, info->virtual_size,
76c9e975 943 ind_s, dsize_buf);
de38b500
EB
944 g_free(size_buf);
945 g_free(dsize_buf);
f364ec65
WX
946
947 if (info->has_encrypted && info->encrypted) {
76c9e975 948 qemu_printf("%sencrypted: yes\n", ind_s);
f364ec65
WX
949 }
950
951 if (info->has_cluster_size) {
76c9e975
HR
952 qemu_printf("%scluster_size: %" PRId64 "\n",
953 ind_s, info->cluster_size);
f364ec65
WX
954 }
955
956 if (info->has_dirty_flag && info->dirty_flag) {
76c9e975 957 qemu_printf("%scleanly shut down: no\n", ind_s);
f364ec65
WX
958 }
959
54fde4ff 960 if (info->backing_filename) {
76c9e975 961 qemu_printf("%sbacking file: %s", ind_s, info->backing_filename);
54fde4ff 962 if (!info->full_backing_filename) {
e1ce7d74 963 qemu_printf(" (cannot determine actual path)");
5c9d9ca5
JS
964 } else if (strcmp(info->backing_filename,
965 info->full_backing_filename) != 0) {
e1ce7d74 966 qemu_printf(" (actual path: %s)", info->full_backing_filename);
f364ec65 967 }
e1ce7d74 968 qemu_printf("\n");
54fde4ff 969 if (info->backing_filename_format) {
76c9e975
HR
970 qemu_printf("%sbacking file format: %s\n",
971 ind_s, info->backing_filename_format);
f364ec65
WX
972 }
973 }
974
975 if (info->has_snapshots) {
976 SnapshotInfoList *elem;
f364ec65 977
76c9e975
HR
978 qemu_printf("%sSnapshot list:\n", ind_s);
979 qemu_printf("%s", ind_s);
e1ce7d74
MA
980 bdrv_snapshot_dump(NULL);
981 qemu_printf("\n");
f364ec65
WX
982
983 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
984 * we convert to the block layer's native QEMUSnapshotInfo for now.
985 */
986 for (elem = info->snapshots; elem; elem = elem->next) {
987 QEMUSnapshotInfo sn = {
988 .vm_state_size = elem->value->vm_state_size,
989 .date_sec = elem->value->date_sec,
990 .date_nsec = elem->value->date_nsec,
991 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
992 elem->value->vm_clock_nsec,
b39847a5
PD
993 .icount = elem->value->has_icount ?
994 elem->value->icount : -1ULL,
f364ec65
WX
995 };
996
997 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
998 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
76c9e975 999 qemu_printf("%s", ind_s);
e1ce7d74
MA
1000 bdrv_snapshot_dump(&sn);
1001 qemu_printf("\n");
f364ec65
WX
1002 }
1003 }
a8d8ecb7 1004
54fde4ff 1005 if (info->format_specific) {
3716470b 1006 bdrv_image_info_specific_dump(info->format_specific,
76c9e975
HR
1007 "Format specific information:\n",
1008 indentation);
a8d8ecb7 1009 }
f364ec65 1010}