Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / block / qapi.c
blob2b5793f1d9b9da25f0ad1832079a6ecaa05ed87c
1 /*
2 * Block layer qmp and info dump related functions
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "qemu/cutils.h"
27 #include "block/qapi.h"
28 #include "block/block_int.h"
29 #include "block/dirty-bitmap.h"
30 #include "block/throttle-groups.h"
31 #include "block/write-threshold.h"
32 #include "qapi/error.h"
33 #include "qapi/qapi-commands-block-core.h"
34 #include "qapi/qobject-output-visitor.h"
35 #include "qapi/qapi-visit-block-core.h"
36 #include "qapi/qmp/qbool.h"
37 #include "qapi/qmp/qdict.h"
38 #include "qapi/qmp/qlist.h"
39 #include "qapi/qmp/qnum.h"
40 #include "qapi/qmp/qstring.h"
41 #include "qemu/qemu-print.h"
42 #include "sysemu/block-backend.h"
44 BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
45 BlockDriverState *bs,
46 bool flat,
47 Error **errp)
49 ERRP_GUARD();
50 ImageInfo **p_image_info;
51 ImageInfo *backing_info;
52 BlockDriverState *backing;
53 BlockDeviceInfo *info;
55 if (!bs->drv) {
56 error_setg(errp, "Block device %s is ejected", bs->node_name);
57 return NULL;
60 bdrv_refresh_filename(bs);
62 info = g_malloc0(sizeof(*info));
63 info->file = g_strdup(bs->filename);
64 info->ro = bdrv_is_read_only(bs);
65 info->drv = g_strdup(bs->drv->format_name);
66 info->encrypted = bs->encrypted;
68 info->cache = g_new(BlockdevCacheInfo, 1);
69 *info->cache = (BlockdevCacheInfo) {
70 .writeback = blk ? blk_enable_write_cache(blk) : true,
71 .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
72 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
75 if (bs->node_name[0]) {
76 info->node_name = g_strdup(bs->node_name);
79 backing = bdrv_cow_bs(bs);
80 if (backing) {
81 info->backing_file = g_strdup(backing->filename);
84 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
85 info->has_dirty_bitmaps = true;
86 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
89 info->detect_zeroes = bs->detect_zeroes;
91 if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
92 ThrottleConfig cfg;
93 BlockBackendPublic *blkp = blk_get_public(blk);
95 throttle_group_get_config(&blkp->throttle_group_member, &cfg);
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;
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;
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;
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;
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;
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;
139 info->has_iops_size = cfg.op_size;
140 info->iops_size = cfg.op_size;
142 info->group =
143 g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
146 info->write_threshold = bdrv_write_threshold_get(bs);
148 p_image_info = &info->image;
149 info->backing_file_depth = 0;
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
155 bdrv_query_image_info(bs, p_image_info, flat, blk != NULL, errp);
156 if (*errp) {
157 qapi_free_BlockDeviceInfo(info);
158 return NULL;
161 backing_info = info->image->backing_image;
162 while (backing_info) {
163 info->backing_file_depth++;
164 backing_info = backing_info->backing_image;
167 return info;
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.
175 int bdrv_query_snapshot_info_list(BlockDriverState *bs,
176 SnapshotInfoList **p_list,
177 Error **errp)
179 int i, sn_count;
180 QEMUSnapshotInfo *sn_tab = NULL;
181 SnapshotInfoList *head = NULL, **tail = &head;
182 SnapshotInfo *info;
184 sn_count = bdrv_snapshot_list(bs, &sn_tab);
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;
201 return sn_count;
204 for (i = 0; i < sn_count; i++) {
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;
213 info->icount = sn_tab[i].icount;
214 info->has_icount = sn_tab[i].icount != -1ULL;
216 QAPI_LIST_APPEND(tail, info);
219 g_free(sn_tab);
220 *p_list = head;
221 return 0;
225 * Helper function for other query info functions. Store information about @bs
226 * in @info, setting @errp on error.
228 static void GRAPH_RDLOCK
229 bdrv_do_query_node_info(BlockDriverState *bs, BlockNodeInfo *info, Error **errp)
231 int64_t size;
232 const char *backing_filename;
233 BlockDriverInfo bdi;
234 int ret;
235 Error *err = NULL;
237 size = bdrv_getlength(bs);
238 if (size < 0) {
239 error_setg_errno(errp, -size, "Can't get image size '%s'",
240 bs->exact_filename);
241 return;
244 bdrv_refresh_filename(bs);
246 info->filename = g_strdup(bs->filename);
247 info->format = g_strdup(bdrv_get_format_name(bs));
248 info->virtual_size = size;
249 info->actual_size = bdrv_get_allocated_file_size(bs);
250 info->has_actual_size = info->actual_size >= 0;
251 if (bs->encrypted) {
252 info->encrypted = true;
253 info->has_encrypted = true;
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;
260 info->dirty_flag = bdi.is_dirty;
261 info->has_dirty_flag = true;
263 info->format_specific = bdrv_get_specific_info(bs, &err);
264 if (err) {
265 error_propagate(errp, err);
266 return;
268 backing_filename = bs->backing_file;
269 if (backing_filename[0] != '\0') {
270 char *backing_filename2;
272 info->backing_filename = g_strdup(backing_filename);
273 backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
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);
281 if (bs->backing_format[0]) {
282 info->backing_filename_format = g_strdup(bs->backing_format);
284 g_free(backing_filename2);
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;
293 break;
294 /* recoverable error */
295 case -ENOMEDIUM:
296 case -ENOTSUP:
297 error_free(err);
298 break;
299 default:
300 error_propagate(errp, err);
301 return;
306 * bdrv_query_image_info:
307 * @bs: block node to examine
308 * @p_info: location to store image information
309 * @flat: skip backing node information
310 * @skip_implicit_filters: skip implicit filters in the backing chain
311 * @errp: location to store error information
313 * Store image information in @p_info, potentially recursively covering the
314 * backing chain.
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.
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.)
325 * @p_info will be set only on success. On error, store error in @errp.
327 void bdrv_query_image_info(BlockDriverState *bs,
328 ImageInfo **p_info,
329 bool flat,
330 bool skip_implicit_filters,
331 Error **errp)
333 ERRP_GUARD();
334 ImageInfo *info;
336 info = g_new0(ImageInfo, 1);
337 bdrv_do_query_node_info(bs, qapi_ImageInfo_base(info), errp);
338 if (*errp) {
339 goto fail;
342 if (!flat) {
343 BlockDriverState *backing;
346 * Use any filtered child here (for backwards compatibility to when
347 * we always took bs->backing, which might be any filtered child).
349 backing = bdrv_filter_or_cow_bs(bs);
350 if (skip_implicit_filters) {
351 backing = bdrv_skip_implicit_filters(backing);
354 if (backing) {
355 bdrv_query_image_info(backing, &info->backing_image, false,
356 skip_implicit_filters, errp);
357 if (*errp) {
358 goto fail;
363 *p_info = info;
364 return;
366 fail:
367 assert(*errp);
368 qapi_free_ImageInfo(info);
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
377 * Store image information about the graph starting from @bs in @p_info.
379 * @p_info will be set only on success. On error, store error in @errp.
381 void bdrv_query_block_graph_info(BlockDriverState *bs,
382 BlockGraphInfo **p_info,
383 Error **errp)
385 ERRP_GUARD();
386 BlockGraphInfo *info;
387 BlockChildInfoList **children_list_tail;
388 BdrvChild *c;
390 info = g_new0(BlockGraphInfo, 1);
391 bdrv_do_query_node_info(bs, qapi_BlockGraphInfo_base(info), errp);
392 if (*errp) {
393 goto fail;
396 children_list_tail = &info->children;
398 QLIST_FOREACH(c, &bs->children, next) {
399 BlockChildInfo *c_info;
401 c_info = g_new0(BlockChildInfo, 1);
402 QAPI_LIST_APPEND(children_list_tail, c_info);
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;
411 *p_info = info;
412 return;
414 fail:
415 assert(*errp != NULL);
416 qapi_free_BlockGraphInfo(info);
419 /* @p_info will be set only on success. */
420 static void GRAPH_RDLOCK
421 bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, Error **errp)
423 BlockInfo *info = g_malloc0(sizeof(*info));
424 BlockDriverState *bs = blk_bs(blk);
425 char *qdev;
427 /* Skip automatically inserted nodes that the user isn't aware of */
428 bs = bdrv_skip_implicit_filters(bs);
430 info->device = g_strdup(blk_name(blk));
431 info->type = g_strdup("unknown");
432 info->locked = blk_dev_is_medium_locked(blk);
433 info->removable = blk_dev_has_removable_media(blk);
435 qdev = blk_get_attached_dev_id(blk);
436 if (qdev && *qdev) {
437 info->qdev = qdev;
438 } else {
439 g_free(qdev);
442 if (blk_dev_has_tray(blk)) {
443 info->has_tray_open = true;
444 info->tray_open = blk_dev_is_tray_open(blk);
447 if (blk_iostatus_is_enabled(blk)) {
448 info->has_io_status = true;
449 info->io_status = blk_iostatus(blk);
452 if (bs && bs->drv) {
453 info->inserted = bdrv_block_device_info(blk, bs, false, errp);
454 if (info->inserted == NULL) {
455 goto err;
459 *p_info = info;
460 return;
462 err:
463 qapi_free_BlockInfo(info);
466 static uint64List *uint64_list(uint64_t *list, int size)
468 int i;
469 uint64List *out_list = NULL;
470 uint64List **tail = &out_list;
472 for (i = 0; i < size; i++) {
473 QAPI_LIST_APPEND(tail, list[i]);
476 return out_list;
479 static BlockLatencyHistogramInfo *
480 bdrv_latency_histogram_stats(BlockLatencyHistogram *hist)
482 BlockLatencyHistogramInfo *info;
484 if (!hist->bins) {
485 return NULL;
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;
494 static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
496 BlockAcctStats *stats = blk_get_stats(blk);
497 BlockAcctTimedStats *ts = NULL;
498 BlockLatencyHistogram *hgram;
500 ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
501 ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
502 ds->zone_append_bytes = stats->nr_bytes[BLOCK_ACCT_ZONE_APPEND];
503 ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
504 ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
505 ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
506 ds->zone_append_operations = stats->nr_ops[BLOCK_ACCT_ZONE_APPEND];
507 ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
509 ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
510 ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
511 ds->failed_zone_append_operations =
512 stats->failed_ops[BLOCK_ACCT_ZONE_APPEND];
513 ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
514 ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
516 ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
517 ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
518 ds->invalid_zone_append_operations =
519 stats->invalid_ops[BLOCK_ACCT_ZONE_APPEND];
520 ds->invalid_flush_operations =
521 stats->invalid_ops[BLOCK_ACCT_FLUSH];
522 ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
524 ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
525 ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
526 ds->zone_append_merged = stats->merged[BLOCK_ACCT_ZONE_APPEND];
527 ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
528 ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
529 ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
530 ds->zone_append_total_time_ns =
531 stats->total_time_ns[BLOCK_ACCT_ZONE_APPEND];
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];
534 ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
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);
541 ds->account_invalid = stats->account_invalid;
542 ds->account_failed = stats->account_failed;
544 while ((ts = block_acct_interval_next(stats, ts))) {
545 BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
547 TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
548 TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
549 TimedAverage *zap = &ts->latency[BLOCK_ACCT_ZONE_APPEND];
550 TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
552 dev_stats->interval_length = ts->interval_length;
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);
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);
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);
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);
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);
574 dev_stats->avg_zone_append_queue_depth =
575 block_acct_queue_depth(ts, BLOCK_ACCT_ZONE_APPEND);
577 QAPI_LIST_PREPEND(ds->timed_stats, dev_stats);
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]);
585 ds->zone_append_latency_histogram
586 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_ZONE_APPEND]);
587 ds->flush_latency_histogram
588 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_FLUSH]);
591 static BlockStats * GRAPH_RDLOCK
592 bdrv_query_bds_stats(BlockDriverState *bs, bool blk_level)
594 BdrvChild *parent_child;
595 BlockDriverState *filter_or_cow_bs;
596 BlockStats *s = NULL;
598 s = g_malloc0(sizeof(*s));
599 s->stats = g_malloc0(sizeof(*s->stats));
601 if (!bs) {
602 return s;
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. */
608 if (blk_level) {
609 bs = bdrv_skip_implicit_filters(bs);
612 if (bdrv_get_node_name(bs)[0]) {
613 s->node_name = g_strdup(bdrv_get_node_name(bs));
616 s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
618 s->driver_specific = bdrv_get_specific_stats(bs);
620 parent_child = bdrv_primary_child(bs);
621 if (!parent_child ||
622 !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED)))
624 BdrvChild *c;
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.
631 parent_child = NULL;
632 QLIST_FOREACH(c, &bs->children, next) {
633 if (c->role & BDRV_CHILD_DATA) {
634 if (parent_child) {
636 * There are multiple data-storing children and we cannot
637 * choose between them.
639 parent_child = NULL;
640 break;
642 parent_child = c;
646 if (parent_child) {
647 s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level);
650 filter_or_cow_bs = bdrv_filter_or_cow_bs(bs);
651 if (blk_level && filter_or_cow_bs) {
653 * Put any filtered or COW child here (for backwards
654 * compatibility to when we put bs0->backing here, which might
655 * be either)
657 s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level);
660 return s;
663 BlockInfoList *qmp_query_block(Error **errp)
665 BlockInfoList *head = NULL, **p_next = &head;
666 BlockBackend *blk;
667 Error *local_err = NULL;
669 GRAPH_RDLOCK_GUARD_MAINLOOP();
671 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
672 BlockInfoList *info;
674 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
675 continue;
678 info = g_malloc0(sizeof(*info));
679 bdrv_query_info(blk, &info->value, &local_err);
680 if (local_err) {
681 error_propagate(errp, local_err);
682 g_free(info);
683 qapi_free_BlockInfoList(head);
684 return NULL;
687 *p_next = info;
688 p_next = &info->next;
691 return head;
694 BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
695 bool query_nodes,
696 Error **errp)
698 BlockStatsList *head = NULL, **tail = &head;
699 BlockBackend *blk;
700 BlockDriverState *bs;
702 GRAPH_RDLOCK_GUARD_MAINLOOP();
704 /* Just to be safe if query_nodes is not always initialized */
705 if (has_query_nodes && query_nodes) {
706 for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
707 QAPI_LIST_APPEND(tail, bdrv_query_bds_stats(bs, false));
709 } else {
710 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
711 BlockStats *s;
712 char *qdev;
714 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
715 continue;
718 s = bdrv_query_bds_stats(blk_bs(blk), true);
719 s->device = g_strdup(blk_name(blk));
721 qdev = blk_get_attached_dev_id(blk);
722 if (qdev && *qdev) {
723 s->qdev = qdev;
724 } else {
725 g_free(qdev);
728 bdrv_query_blk_stats(s->stats, blk);
730 QAPI_LIST_APPEND(tail, s);
734 return head;
737 void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
739 char clock_buf[128];
740 char icount_buf[128] = {0};
741 int64_t secs;
742 char *sizing = NULL;
744 if (!sn) {
745 qemu_printf("%-7s %-16s %8s %19s %15s %10s",
746 "ID", "TAG", "VM_SIZE", "DATE", "VM_CLOCK", "ICOUNT");
747 } else {
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");
751 secs = sn->vm_clock_nsec / 1000000000;
752 snprintf(clock_buf, sizeof(clock_buf),
753 "%04d:%02d:%02d.%03d",
754 (int)(secs / 3600),
755 (int)((secs / 60) % 60),
756 (int)(secs % 60),
757 (int)((sn->vm_clock_nsec / 1000000) % 1000));
758 sizing = size_to_str(sn->vm_state_size);
759 if (sn->icount != -1ULL) {
760 snprintf(icount_buf, sizeof(icount_buf),
761 "%"PRId64, sn->icount);
762 } else {
763 snprintf(icount_buf, sizeof(icount_buf), "--");
765 qemu_printf("%-7s %-16s %8s %19s %15s %10s",
766 sn->id_str, sn->name,
767 sizing,
768 date_buf,
769 clock_buf,
770 icount_buf);
772 g_free(sizing);
775 static void dump_qdict(int indentation, QDict *dict);
776 static void dump_qlist(int indentation, QList *list);
778 static void dump_qobject(int comp_indent, QObject *obj)
780 switch (qobject_type(obj)) {
781 case QTYPE_QNUM: {
782 QNum *value = qobject_to(QNum, obj);
783 char *tmp = qnum_to_string(value);
784 qemu_printf("%s", tmp);
785 g_free(tmp);
786 break;
788 case QTYPE_QSTRING: {
789 QString *value = qobject_to(QString, obj);
790 qemu_printf("%s", qstring_get_str(value));
791 break;
793 case QTYPE_QDICT: {
794 QDict *value = qobject_to(QDict, obj);
795 dump_qdict(comp_indent, value);
796 break;
798 case QTYPE_QLIST: {
799 QList *value = qobject_to(QList, obj);
800 dump_qlist(comp_indent, value);
801 break;
803 case QTYPE_QBOOL: {
804 QBool *value = qobject_to(QBool, obj);
805 qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
806 break;
808 default:
809 abort();
813 static void dump_qlist(int indentation, QList *list)
815 const QListEntry *entry;
816 int i = 0;
818 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
819 QType type = qobject_type(entry->value);
820 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
821 qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
822 composite ? '\n' : ' ');
823 dump_qobject(indentation + 1, entry->value);
824 if (!composite) {
825 qemu_printf("\n");
830 static void dump_qdict(int indentation, QDict *dict)
832 const QDictEntry *entry;
834 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
835 QType type = qobject_type(entry->value);
836 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
837 char *key = g_malloc(strlen(entry->key) + 1);
838 int i;
840 /* replace dashes with spaces in key (variable) names */
841 for (i = 0; entry->key[i]; i++) {
842 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
844 key[i] = 0;
845 qemu_printf("%*s%s:%c", indentation * 4, "", key,
846 composite ? '\n' : ' ');
847 dump_qobject(indentation + 1, entry->value);
848 if (!composite) {
849 qemu_printf("\n");
851 g_free(key);
856 * Return whether dumping the given QObject with dump_qobject() would
857 * yield an empty dump, i.e. not print anything.
859 static bool qobject_is_empty_dump(const QObject *obj)
861 switch (qobject_type(obj)) {
862 case QTYPE_QNUM:
863 case QTYPE_QSTRING:
864 case QTYPE_QBOOL:
865 return false;
867 case QTYPE_QDICT:
868 return qdict_size(qobject_to(QDict, obj)) == 0;
870 case QTYPE_QLIST:
871 return qlist_empty(qobject_to(QList, obj));
873 default:
874 abort();
879 * Dumps the given ImageInfoSpecific object in a human-readable form,
880 * prepending an optional prefix if the dump is not empty.
882 void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec,
883 const char *prefix,
884 int indentation)
886 QObject *obj, *data;
887 Visitor *v = qobject_output_visitor_new(&obj);
889 visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
890 visit_complete(v, &obj);
891 data = qdict_get(qobject_to(QDict, obj), "data");
892 if (!qobject_is_empty_dump(data)) {
893 if (prefix) {
894 qemu_printf("%*s%s", indentation * 4, "", prefix);
896 dump_qobject(indentation + 1, data);
898 qobject_unref(obj);
899 visit_free(v);
903 * Print the given @info object in human-readable form. Every field is indented
904 * using the given @indentation (four spaces per indentation level).
906 * When using this to print a whole block graph, @protocol can be set to true to
907 * signify that the given information is associated with a protocol node, i.e.
908 * just data storage for an image, such that the data it presents is not really
909 * a full VM disk. If so, several fields change name: For example, "virtual
910 * size" is printed as "file length".
911 * (Consider a qcow2 image, which is represented by a qcow2 node and a file
912 * node. Printing a "virtual size" for the file node does not make sense,
913 * because without the qcow2 node, it is not really a guest disk, so it does not
914 * have a "virtual size". Therefore, we call it "file length" instead.)
916 * @protocol is ignored when @indentation is 0, because we take that to mean
917 * that the associated node is the root node in the queried block graph, and
918 * thus is always to be interpreted as a standalone guest disk.
920 void bdrv_node_info_dump(BlockNodeInfo *info, int indentation, bool protocol)
922 char *size_buf, *dsize_buf;
923 g_autofree char *ind_s = g_strdup_printf("%*s", indentation * 4, "");
925 if (indentation == 0) {
926 /* Top level, consider this a normal image */
927 protocol = false;
930 if (!info->has_actual_size) {
931 dsize_buf = g_strdup("unavailable");
932 } else {
933 dsize_buf = size_to_str(info->actual_size);
935 size_buf = size_to_str(info->virtual_size);
936 qemu_printf("%s%s: %s\n"
937 "%s%s: %s\n"
938 "%s%s: %s (%" PRId64 " bytes)\n"
939 "%sdisk size: %s\n",
940 ind_s, protocol ? "filename" : "image", info->filename,
941 ind_s, protocol ? "protocol type" : "file format",
942 info->format,
943 ind_s, protocol ? "file length" : "virtual size",
944 size_buf, info->virtual_size,
945 ind_s, dsize_buf);
946 g_free(size_buf);
947 g_free(dsize_buf);
949 if (info->has_encrypted && info->encrypted) {
950 qemu_printf("%sencrypted: yes\n", ind_s);
953 if (info->has_cluster_size) {
954 qemu_printf("%scluster_size: %" PRId64 "\n",
955 ind_s, info->cluster_size);
958 if (info->has_dirty_flag && info->dirty_flag) {
959 qemu_printf("%scleanly shut down: no\n", ind_s);
962 if (info->backing_filename) {
963 qemu_printf("%sbacking file: %s", ind_s, info->backing_filename);
964 if (!info->full_backing_filename) {
965 qemu_printf(" (cannot determine actual path)");
966 } else if (strcmp(info->backing_filename,
967 info->full_backing_filename) != 0) {
968 qemu_printf(" (actual path: %s)", info->full_backing_filename);
970 qemu_printf("\n");
971 if (info->backing_filename_format) {
972 qemu_printf("%sbacking file format: %s\n",
973 ind_s, info->backing_filename_format);
977 if (info->has_snapshots) {
978 SnapshotInfoList *elem;
980 qemu_printf("%sSnapshot list:\n", ind_s);
981 qemu_printf("%s", ind_s);
982 bdrv_snapshot_dump(NULL);
983 qemu_printf("\n");
985 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
986 * we convert to the block layer's native QEMUSnapshotInfo for now.
988 for (elem = info->snapshots; elem; elem = elem->next) {
989 QEMUSnapshotInfo sn = {
990 .vm_state_size = elem->value->vm_state_size,
991 .date_sec = elem->value->date_sec,
992 .date_nsec = elem->value->date_nsec,
993 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
994 elem->value->vm_clock_nsec,
995 .icount = elem->value->has_icount ?
996 elem->value->icount : -1ULL,
999 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
1000 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
1001 qemu_printf("%s", ind_s);
1002 bdrv_snapshot_dump(&sn);
1003 qemu_printf("\n");
1007 if (info->format_specific) {
1008 bdrv_image_info_specific_dump(info->format_specific,
1009 "Format specific information:\n",
1010 indentation);