block: Leave BDS.backing_{file,format} constant
[qemu.git] / block / qapi.c
blobf423ece98ca2525adeab93d0b235e88e077a9356
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/throttle-groups.h"
30 #include "block/write-threshold.h"
31 #include "qapi/error.h"
32 #include "qapi/qapi-commands-block-core.h"
33 #include "qapi/qobject-output-visitor.h"
34 #include "qapi/qapi-visit-block-core.h"
35 #include "qapi/qmp/qbool.h"
36 #include "qapi/qmp/qdict.h"
37 #include "qapi/qmp/qlist.h"
38 #include "qapi/qmp/qnum.h"
39 #include "qapi/qmp/qstring.h"
40 #include "qemu/qemu-print.h"
41 #include "sysemu/block-backend.h"
42 #include "qemu/cutils.h"
44 BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
45 BlockDriverState *bs,
46 bool flat,
47 Error **errp)
49 ImageInfo **p_image_info;
50 BlockDriverState *bs0, *backing;
51 BlockDeviceInfo *info;
53 if (!bs->drv) {
54 error_setg(errp, "Block device %s is ejected", bs->node_name);
55 return NULL;
58 bdrv_refresh_filename(bs);
60 info = g_malloc0(sizeof(*info));
61 info->file = g_strdup(bs->filename);
62 info->ro = bs->read_only;
63 info->drv = g_strdup(bs->drv->format_name);
64 info->encrypted = bs->encrypted;
65 info->encryption_key_missing = false;
67 info->cache = g_new(BlockdevCacheInfo, 1);
68 *info->cache = (BlockdevCacheInfo) {
69 .writeback = blk ? blk_enable_write_cache(blk) : true,
70 .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
71 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
74 if (bs->node_name[0]) {
75 info->has_node_name = true;
76 info->node_name = g_strdup(bs->node_name);
79 backing = bdrv_cow_bs(bs);
80 if (backing) {
81 info->has_backing_file = true;
82 info->backing_file = g_strdup(backing->filename);
85 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
86 info->has_dirty_bitmaps = true;
87 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
90 info->detect_zeroes = bs->detect_zeroes;
92 if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
93 ThrottleConfig cfg;
94 BlockBackendPublic *blkp = blk_get_public(blk);
96 throttle_group_get_config(&blkp->throttle_group_member, &cfg);
98 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
99 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg;
100 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg;
102 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
103 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
104 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
106 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
107 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
108 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
109 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
110 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
111 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
113 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
114 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
115 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
116 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
117 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
118 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
120 info->has_bps_max_length = info->has_bps_max;
121 info->bps_max_length =
122 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
123 info->has_bps_rd_max_length = info->has_bps_rd_max;
124 info->bps_rd_max_length =
125 cfg.buckets[THROTTLE_BPS_READ].burst_length;
126 info->has_bps_wr_max_length = info->has_bps_wr_max;
127 info->bps_wr_max_length =
128 cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
130 info->has_iops_max_length = info->has_iops_max;
131 info->iops_max_length =
132 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
133 info->has_iops_rd_max_length = info->has_iops_rd_max;
134 info->iops_rd_max_length =
135 cfg.buckets[THROTTLE_OPS_READ].burst_length;
136 info->has_iops_wr_max_length = info->has_iops_wr_max;
137 info->iops_wr_max_length =
138 cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
140 info->has_iops_size = cfg.op_size;
141 info->iops_size = cfg.op_size;
143 info->has_group = true;
144 info->group =
145 g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
148 info->write_threshold = bdrv_write_threshold_get(bs);
150 bs0 = bs;
151 p_image_info = &info->image;
152 info->backing_file_depth = 0;
153 while (1) {
154 Error *local_err = NULL;
155 bdrv_query_image_info(bs0, p_image_info, &local_err);
156 if (local_err) {
157 error_propagate(errp, local_err);
158 qapi_free_BlockDeviceInfo(info);
159 return NULL;
162 /* stop gathering data for flat output */
163 if (flat) {
164 break;
167 if (bs0->drv && bdrv_filter_or_cow_child(bs0)) {
169 * Put any filtered child here (for backwards compatibility to when
170 * we put bs0->backing here, which might be any filtered child).
172 info->backing_file_depth++;
173 bs0 = bdrv_filter_or_cow_bs(bs0);
174 (*p_image_info)->has_backing_image = true;
175 p_image_info = &((*p_image_info)->backing_image);
176 } else {
177 break;
180 /* Skip automatically inserted nodes that the user isn't aware of for
181 * query-block (blk != NULL), but not for query-named-block-nodes */
182 if (blk) {
183 bs0 = bdrv_skip_implicit_filters(bs0);
187 return info;
191 * Returns 0 on success, with *p_list either set to describe snapshot
192 * information, or NULL because there are no snapshots. Returns -errno on
193 * error, with *p_list untouched.
195 int bdrv_query_snapshot_info_list(BlockDriverState *bs,
196 SnapshotInfoList **p_list,
197 Error **errp)
199 int i, sn_count;
200 QEMUSnapshotInfo *sn_tab = NULL;
201 SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
202 SnapshotInfo *info;
204 sn_count = bdrv_snapshot_list(bs, &sn_tab);
205 if (sn_count < 0) {
206 const char *dev = bdrv_get_device_name(bs);
207 switch (sn_count) {
208 case -ENOMEDIUM:
209 error_setg(errp, "Device '%s' is not inserted", dev);
210 break;
211 case -ENOTSUP:
212 error_setg(errp,
213 "Device '%s' does not support internal snapshots",
214 dev);
215 break;
216 default:
217 error_setg_errno(errp, -sn_count,
218 "Can't list snapshots of device '%s'", dev);
219 break;
221 return sn_count;
224 for (i = 0; i < sn_count; i++) {
225 info = g_new0(SnapshotInfo, 1);
226 info->id = g_strdup(sn_tab[i].id_str);
227 info->name = g_strdup(sn_tab[i].name);
228 info->vm_state_size = sn_tab[i].vm_state_size;
229 info->date_sec = sn_tab[i].date_sec;
230 info->date_nsec = sn_tab[i].date_nsec;
231 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
232 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
234 info_list = g_new0(SnapshotInfoList, 1);
235 info_list->value = info;
237 /* XXX: waiting for the qapi to support qemu-queue.h types */
238 if (!cur_item) {
239 head = cur_item = info_list;
240 } else {
241 cur_item->next = info_list;
242 cur_item = info_list;
247 g_free(sn_tab);
248 *p_list = head;
249 return 0;
253 * bdrv_query_image_info:
254 * @bs: block device to examine
255 * @p_info: location to store image information
256 * @errp: location to store error information
258 * Store "flat" image information in @p_info.
260 * "Flat" means it does *not* query backing image information,
261 * i.e. (*pinfo)->has_backing_image will be set to false and
262 * (*pinfo)->backing_image to NULL even when the image does in fact have
263 * a backing image.
265 * @p_info will be set only on success. On error, store error in @errp.
267 void bdrv_query_image_info(BlockDriverState *bs,
268 ImageInfo **p_info,
269 Error **errp)
271 int64_t size;
272 const char *backing_filename;
273 BlockDriverInfo bdi;
274 int ret;
275 Error *err = NULL;
276 ImageInfo *info;
278 aio_context_acquire(bdrv_get_aio_context(bs));
280 size = bdrv_getlength(bs);
281 if (size < 0) {
282 error_setg_errno(errp, -size, "Can't get image size '%s'",
283 bs->exact_filename);
284 goto out;
287 bdrv_refresh_filename(bs);
289 info = g_new0(ImageInfo, 1);
290 info->filename = g_strdup(bs->filename);
291 info->format = g_strdup(bdrv_get_format_name(bs));
292 info->virtual_size = size;
293 info->actual_size = bdrv_get_allocated_file_size(bs);
294 info->has_actual_size = info->actual_size >= 0;
295 if (bs->encrypted) {
296 info->encrypted = true;
297 info->has_encrypted = true;
299 if (bdrv_get_info(bs, &bdi) >= 0) {
300 if (bdi.cluster_size != 0) {
301 info->cluster_size = bdi.cluster_size;
302 info->has_cluster_size = true;
304 info->dirty_flag = bdi.is_dirty;
305 info->has_dirty_flag = true;
307 info->format_specific = bdrv_get_specific_info(bs, &err);
308 if (err) {
309 error_propagate(errp, err);
310 qapi_free_ImageInfo(info);
311 goto out;
313 info->has_format_specific = info->format_specific != NULL;
315 backing_filename = bs->backing_file;
316 if (backing_filename[0] != '\0') {
317 char *backing_filename2;
319 info->backing_filename = g_strdup(backing_filename);
320 info->has_backing_filename = true;
321 backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
323 /* Always report the full_backing_filename if present, even if it's the
324 * same as backing_filename. That they are same is useful info. */
325 if (backing_filename2) {
326 info->full_backing_filename = g_strdup(backing_filename2);
327 info->has_full_backing_filename = true;
330 if (bs->backing_format[0]) {
331 info->backing_filename_format = g_strdup(bs->backing_format);
332 info->has_backing_filename_format = true;
334 g_free(backing_filename2);
337 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
338 switch (ret) {
339 case 0:
340 if (info->snapshots) {
341 info->has_snapshots = true;
343 break;
344 /* recoverable error */
345 case -ENOMEDIUM:
346 case -ENOTSUP:
347 error_free(err);
348 break;
349 default:
350 error_propagate(errp, err);
351 qapi_free_ImageInfo(info);
352 goto out;
355 *p_info = info;
357 out:
358 aio_context_release(bdrv_get_aio_context(bs));
361 /* @p_info will be set only on success. */
362 static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
363 Error **errp)
365 BlockInfo *info = g_malloc0(sizeof(*info));
366 BlockDriverState *bs = blk_bs(blk);
367 char *qdev;
369 /* Skip automatically inserted nodes that the user isn't aware of */
370 bs = bdrv_skip_implicit_filters(bs);
372 info->device = g_strdup(blk_name(blk));
373 info->type = g_strdup("unknown");
374 info->locked = blk_dev_is_medium_locked(blk);
375 info->removable = blk_dev_has_removable_media(blk);
377 qdev = blk_get_attached_dev_id(blk);
378 if (qdev && *qdev) {
379 info->has_qdev = true;
380 info->qdev = qdev;
381 } else {
382 g_free(qdev);
385 if (blk_dev_has_tray(blk)) {
386 info->has_tray_open = true;
387 info->tray_open = blk_dev_is_tray_open(blk);
390 if (blk_iostatus_is_enabled(blk)) {
391 info->has_io_status = true;
392 info->io_status = blk_iostatus(blk);
395 if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
396 info->has_dirty_bitmaps = true;
397 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
400 if (bs && bs->drv) {
401 info->has_inserted = true;
402 info->inserted = bdrv_block_device_info(blk, bs, false, errp);
403 if (info->inserted == NULL) {
404 goto err;
408 *p_info = info;
409 return;
411 err:
412 qapi_free_BlockInfo(info);
415 static uint64List *uint64_list(uint64_t *list, int size)
417 int i;
418 uint64List *out_list = NULL;
419 uint64List **pout_list = &out_list;
421 for (i = 0; i < size; i++) {
422 uint64List *entry = g_new(uint64List, 1);
423 entry->value = list[i];
424 *pout_list = entry;
425 pout_list = &entry->next;
428 *pout_list = NULL;
430 return out_list;
433 static void bdrv_latency_histogram_stats(BlockLatencyHistogram *hist,
434 bool *not_null,
435 BlockLatencyHistogramInfo **info)
437 *not_null = hist->bins != NULL;
438 if (*not_null) {
439 *info = g_new0(BlockLatencyHistogramInfo, 1);
441 (*info)->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
442 (*info)->bins = uint64_list(hist->bins, hist->nbins);
446 static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
448 BlockAcctStats *stats = blk_get_stats(blk);
449 BlockAcctTimedStats *ts = NULL;
451 ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
452 ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
453 ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
454 ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
455 ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
456 ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
458 ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
459 ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
460 ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
461 ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
463 ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
464 ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
465 ds->invalid_flush_operations =
466 stats->invalid_ops[BLOCK_ACCT_FLUSH];
467 ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
469 ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
470 ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
471 ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
472 ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
473 ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
474 ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
475 ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
476 ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
478 ds->has_idle_time_ns = stats->last_access_time_ns > 0;
479 if (ds->has_idle_time_ns) {
480 ds->idle_time_ns = block_acct_idle_time_ns(stats);
483 ds->account_invalid = stats->account_invalid;
484 ds->account_failed = stats->account_failed;
486 while ((ts = block_acct_interval_next(stats, ts))) {
487 BlockDeviceTimedStatsList *timed_stats =
488 g_malloc0(sizeof(*timed_stats));
489 BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
490 timed_stats->next = ds->timed_stats;
491 timed_stats->value = dev_stats;
492 ds->timed_stats = timed_stats;
494 TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
495 TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
496 TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
498 dev_stats->interval_length = ts->interval_length;
500 dev_stats->min_rd_latency_ns = timed_average_min(rd);
501 dev_stats->max_rd_latency_ns = timed_average_max(rd);
502 dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
504 dev_stats->min_wr_latency_ns = timed_average_min(wr);
505 dev_stats->max_wr_latency_ns = timed_average_max(wr);
506 dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
508 dev_stats->min_flush_latency_ns = timed_average_min(fl);
509 dev_stats->max_flush_latency_ns = timed_average_max(fl);
510 dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
512 dev_stats->avg_rd_queue_depth =
513 block_acct_queue_depth(ts, BLOCK_ACCT_READ);
514 dev_stats->avg_wr_queue_depth =
515 block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
518 bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_READ],
519 &ds->has_rd_latency_histogram,
520 &ds->rd_latency_histogram);
521 bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_WRITE],
522 &ds->has_wr_latency_histogram,
523 &ds->wr_latency_histogram);
524 bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_FLUSH],
525 &ds->has_flush_latency_histogram,
526 &ds->flush_latency_histogram);
529 static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
530 bool blk_level)
532 BdrvChild *parent_child;
533 BlockDriverState *filter_or_cow_bs;
534 BlockStats *s = NULL;
536 s = g_malloc0(sizeof(*s));
537 s->stats = g_malloc0(sizeof(*s->stats));
539 if (!bs) {
540 return s;
543 /* Skip automatically inserted nodes that the user isn't aware of in
544 * a BlockBackend-level command. Stay at the exact node for a node-level
545 * command. */
546 if (blk_level) {
547 bs = bdrv_skip_implicit_filters(bs);
550 if (bdrv_get_node_name(bs)[0]) {
551 s->has_node_name = true;
552 s->node_name = g_strdup(bdrv_get_node_name(bs));
555 s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
557 s->driver_specific = bdrv_get_specific_stats(bs);
558 if (s->driver_specific) {
559 s->has_driver_specific = true;
562 parent_child = bdrv_primary_child(bs);
563 if (!parent_child ||
564 !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED)))
566 BdrvChild *c;
569 * Look for a unique data-storing child. We do not need to look for
570 * filtered children, as there would be only one and it would have been
571 * the primary child.
573 parent_child = NULL;
574 QLIST_FOREACH(c, &bs->children, next) {
575 if (c->role & BDRV_CHILD_DATA) {
576 if (parent_child) {
578 * There are multiple data-storing children and we cannot
579 * choose between them.
581 parent_child = NULL;
582 break;
584 parent_child = c;
588 if (parent_child) {
589 s->has_parent = true;
590 s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level);
593 filter_or_cow_bs = bdrv_filter_or_cow_bs(bs);
594 if (blk_level && filter_or_cow_bs) {
596 * Put any filtered or COW child here (for backwards
597 * compatibility to when we put bs0->backing here, which might
598 * be either)
600 s->has_backing = true;
601 s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level);
604 return s;
607 BlockInfoList *qmp_query_block(Error **errp)
609 BlockInfoList *head = NULL, **p_next = &head;
610 BlockBackend *blk;
611 Error *local_err = NULL;
613 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
614 BlockInfoList *info;
616 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
617 continue;
620 info = g_malloc0(sizeof(*info));
621 bdrv_query_info(blk, &info->value, &local_err);
622 if (local_err) {
623 error_propagate(errp, local_err);
624 g_free(info);
625 qapi_free_BlockInfoList(head);
626 return NULL;
629 *p_next = info;
630 p_next = &info->next;
633 return head;
636 BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
637 bool query_nodes,
638 Error **errp)
640 BlockStatsList *head = NULL, **p_next = &head;
641 BlockBackend *blk;
642 BlockDriverState *bs;
644 /* Just to be safe if query_nodes is not always initialized */
645 if (has_query_nodes && query_nodes) {
646 for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
647 BlockStatsList *info = g_malloc0(sizeof(*info));
648 AioContext *ctx = bdrv_get_aio_context(bs);
650 aio_context_acquire(ctx);
651 info->value = bdrv_query_bds_stats(bs, false);
652 aio_context_release(ctx);
654 *p_next = info;
655 p_next = &info->next;
657 } else {
658 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
659 BlockStatsList *info;
660 AioContext *ctx = blk_get_aio_context(blk);
661 BlockStats *s;
662 char *qdev;
664 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
665 continue;
668 aio_context_acquire(ctx);
669 s = bdrv_query_bds_stats(blk_bs(blk), true);
670 s->has_device = true;
671 s->device = g_strdup(blk_name(blk));
673 qdev = blk_get_attached_dev_id(blk);
674 if (qdev && *qdev) {
675 s->has_qdev = true;
676 s->qdev = qdev;
677 } else {
678 g_free(qdev);
681 bdrv_query_blk_stats(s->stats, blk);
682 aio_context_release(ctx);
684 info = g_malloc0(sizeof(*info));
685 info->value = s;
686 *p_next = info;
687 p_next = &info->next;
691 return head;
694 void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
696 char date_buf[128], clock_buf[128];
697 struct tm tm;
698 time_t ti;
699 int64_t secs;
700 char *sizing = NULL;
702 if (!sn) {
703 qemu_printf("%-10s%-20s%11s%20s%15s",
704 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
705 } else {
706 ti = sn->date_sec;
707 localtime_r(&ti, &tm);
708 strftime(date_buf, sizeof(date_buf),
709 "%Y-%m-%d %H:%M:%S", &tm);
710 secs = sn->vm_clock_nsec / 1000000000;
711 snprintf(clock_buf, sizeof(clock_buf),
712 "%02d:%02d:%02d.%03d",
713 (int)(secs / 3600),
714 (int)((secs / 60) % 60),
715 (int)(secs % 60),
716 (int)((sn->vm_clock_nsec / 1000000) % 1000));
717 sizing = size_to_str(sn->vm_state_size);
718 qemu_printf("%-10s%-20s%11s%20s%15s",
719 sn->id_str, sn->name,
720 sizing,
721 date_buf,
722 clock_buf);
724 g_free(sizing);
727 static void dump_qdict(int indentation, QDict *dict);
728 static void dump_qlist(int indentation, QList *list);
730 static void dump_qobject(int comp_indent, QObject *obj)
732 switch (qobject_type(obj)) {
733 case QTYPE_QNUM: {
734 QNum *value = qobject_to(QNum, obj);
735 char *tmp = qnum_to_string(value);
736 qemu_printf("%s", tmp);
737 g_free(tmp);
738 break;
740 case QTYPE_QSTRING: {
741 QString *value = qobject_to(QString, obj);
742 qemu_printf("%s", qstring_get_str(value));
743 break;
745 case QTYPE_QDICT: {
746 QDict *value = qobject_to(QDict, obj);
747 dump_qdict(comp_indent, value);
748 break;
750 case QTYPE_QLIST: {
751 QList *value = qobject_to(QList, obj);
752 dump_qlist(comp_indent, value);
753 break;
755 case QTYPE_QBOOL: {
756 QBool *value = qobject_to(QBool, obj);
757 qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
758 break;
760 default:
761 abort();
765 static void dump_qlist(int indentation, QList *list)
767 const QListEntry *entry;
768 int i = 0;
770 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
771 QType type = qobject_type(entry->value);
772 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
773 qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
774 composite ? '\n' : ' ');
775 dump_qobject(indentation + 1, entry->value);
776 if (!composite) {
777 qemu_printf("\n");
782 static void dump_qdict(int indentation, QDict *dict)
784 const QDictEntry *entry;
786 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
787 QType type = qobject_type(entry->value);
788 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
789 char *key = g_malloc(strlen(entry->key) + 1);
790 int i;
792 /* replace dashes with spaces in key (variable) names */
793 for (i = 0; entry->key[i]; i++) {
794 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
796 key[i] = 0;
797 qemu_printf("%*s%s:%c", indentation * 4, "", key,
798 composite ? '\n' : ' ');
799 dump_qobject(indentation + 1, entry->value);
800 if (!composite) {
801 qemu_printf("\n");
803 g_free(key);
807 void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec)
809 QObject *obj, *data;
810 Visitor *v = qobject_output_visitor_new(&obj);
812 visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
813 visit_complete(v, &obj);
814 data = qdict_get(qobject_to(QDict, obj), "data");
815 dump_qobject(1, data);
816 qobject_unref(obj);
817 visit_free(v);
820 void bdrv_image_info_dump(ImageInfo *info)
822 char *size_buf, *dsize_buf;
823 if (!info->has_actual_size) {
824 dsize_buf = g_strdup("unavailable");
825 } else {
826 dsize_buf = size_to_str(info->actual_size);
828 size_buf = size_to_str(info->virtual_size);
829 qemu_printf("image: %s\n"
830 "file format: %s\n"
831 "virtual size: %s (%" PRId64 " bytes)\n"
832 "disk size: %s\n",
833 info->filename, info->format, size_buf,
834 info->virtual_size,
835 dsize_buf);
836 g_free(size_buf);
837 g_free(dsize_buf);
839 if (info->has_encrypted && info->encrypted) {
840 qemu_printf("encrypted: yes\n");
843 if (info->has_cluster_size) {
844 qemu_printf("cluster_size: %" PRId64 "\n",
845 info->cluster_size);
848 if (info->has_dirty_flag && info->dirty_flag) {
849 qemu_printf("cleanly shut down: no\n");
852 if (info->has_backing_filename) {
853 qemu_printf("backing file: %s", info->backing_filename);
854 if (!info->has_full_backing_filename) {
855 qemu_printf(" (cannot determine actual path)");
856 } else if (strcmp(info->backing_filename,
857 info->full_backing_filename) != 0) {
858 qemu_printf(" (actual path: %s)", info->full_backing_filename);
860 qemu_printf("\n");
861 if (info->has_backing_filename_format) {
862 qemu_printf("backing file format: %s\n",
863 info->backing_filename_format);
867 if (info->has_snapshots) {
868 SnapshotInfoList *elem;
870 qemu_printf("Snapshot list:\n");
871 bdrv_snapshot_dump(NULL);
872 qemu_printf("\n");
874 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
875 * we convert to the block layer's native QEMUSnapshotInfo for now.
877 for (elem = info->snapshots; elem; elem = elem->next) {
878 QEMUSnapshotInfo sn = {
879 .vm_state_size = elem->value->vm_state_size,
880 .date_sec = elem->value->date_sec,
881 .date_nsec = elem->value->date_nsec,
882 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
883 elem->value->vm_clock_nsec,
886 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
887 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
888 bdrv_snapshot_dump(&sn);
889 qemu_printf("\n");
893 if (info->has_format_specific) {
894 qemu_printf("Format specific information:\n");
895 bdrv_image_info_specific_dump(info->format_specific);