block: remove 'encryption_key_missing' flag from QAPI
[qemu/armbru.git] / block / qapi.c
blob3acc118c445b6bbf77fa88b4044aa434f96bdd1c
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;
66 info->cache = g_new(BlockdevCacheInfo, 1);
67 *info->cache = (BlockdevCacheInfo) {
68 .writeback = blk ? blk_enable_write_cache(blk) : true,
69 .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
70 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
73 if (bs->node_name[0]) {
74 info->has_node_name = true;
75 info->node_name = g_strdup(bs->node_name);
78 backing = bdrv_cow_bs(bs);
79 if (backing) {
80 info->has_backing_file = true;
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->has_group = true;
143 info->group =
144 g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
147 info->write_threshold = bdrv_write_threshold_get(bs);
149 bs0 = bs;
150 p_image_info = &info->image;
151 info->backing_file_depth = 0;
152 while (1) {
153 Error *local_err = NULL;
154 bdrv_query_image_info(bs0, p_image_info, &local_err);
155 if (local_err) {
156 error_propagate(errp, local_err);
157 qapi_free_BlockDeviceInfo(info);
158 return NULL;
161 /* stop gathering data for flat output */
162 if (flat) {
163 break;
166 if (bs0->drv && bdrv_filter_or_cow_child(bs0)) {
168 * Put any filtered child here (for backwards compatibility to when
169 * we put bs0->backing here, which might be any filtered child).
171 info->backing_file_depth++;
172 bs0 = bdrv_filter_or_cow_bs(bs0);
173 (*p_image_info)->has_backing_image = true;
174 p_image_info = &((*p_image_info)->backing_image);
175 } else {
176 break;
179 /* Skip automatically inserted nodes that the user isn't aware of for
180 * query-block (blk != NULL), but not for query-named-block-nodes */
181 if (blk) {
182 bs0 = bdrv_skip_implicit_filters(bs0);
186 return info;
190 * Returns 0 on success, with *p_list either set to describe snapshot
191 * information, or NULL because there are no snapshots. Returns -errno on
192 * error, with *p_list untouched.
194 int bdrv_query_snapshot_info_list(BlockDriverState *bs,
195 SnapshotInfoList **p_list,
196 Error **errp)
198 int i, sn_count;
199 QEMUSnapshotInfo *sn_tab = NULL;
200 SnapshotInfoList *head = NULL, **tail = &head;
201 SnapshotInfo *info;
203 sn_count = bdrv_snapshot_list(bs, &sn_tab);
204 if (sn_count < 0) {
205 const char *dev = bdrv_get_device_name(bs);
206 switch (sn_count) {
207 case -ENOMEDIUM:
208 error_setg(errp, "Device '%s' is not inserted", dev);
209 break;
210 case -ENOTSUP:
211 error_setg(errp,
212 "Device '%s' does not support internal snapshots",
213 dev);
214 break;
215 default:
216 error_setg_errno(errp, -sn_count,
217 "Can't list snapshots of device '%s'", dev);
218 break;
220 return sn_count;
223 for (i = 0; i < sn_count; i++) {
224 info = g_new0(SnapshotInfo, 1);
225 info->id = g_strdup(sn_tab[i].id_str);
226 info->name = g_strdup(sn_tab[i].name);
227 info->vm_state_size = sn_tab[i].vm_state_size;
228 info->date_sec = sn_tab[i].date_sec;
229 info->date_nsec = sn_tab[i].date_nsec;
230 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
231 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
232 info->icount = sn_tab[i].icount;
233 info->has_icount = sn_tab[i].icount != -1ULL;
235 QAPI_LIST_APPEND(tail, info);
238 g_free(sn_tab);
239 *p_list = head;
240 return 0;
244 * bdrv_query_image_info:
245 * @bs: block device to examine
246 * @p_info: location to store image information
247 * @errp: location to store error information
249 * Store "flat" image information in @p_info.
251 * "Flat" means it does *not* query backing image information,
252 * i.e. (*pinfo)->has_backing_image will be set to false and
253 * (*pinfo)->backing_image to NULL even when the image does in fact have
254 * a backing image.
256 * @p_info will be set only on success. On error, store error in @errp.
258 void bdrv_query_image_info(BlockDriverState *bs,
259 ImageInfo **p_info,
260 Error **errp)
262 int64_t size;
263 const char *backing_filename;
264 BlockDriverInfo bdi;
265 int ret;
266 Error *err = NULL;
267 ImageInfo *info;
269 aio_context_acquire(bdrv_get_aio_context(bs));
271 size = bdrv_getlength(bs);
272 if (size < 0) {
273 error_setg_errno(errp, -size, "Can't get image size '%s'",
274 bs->exact_filename);
275 goto out;
278 bdrv_refresh_filename(bs);
280 info = g_new0(ImageInfo, 1);
281 info->filename = g_strdup(bs->filename);
282 info->format = g_strdup(bdrv_get_format_name(bs));
283 info->virtual_size = size;
284 info->actual_size = bdrv_get_allocated_file_size(bs);
285 info->has_actual_size = info->actual_size >= 0;
286 if (bs->encrypted) {
287 info->encrypted = true;
288 info->has_encrypted = true;
290 if (bdrv_get_info(bs, &bdi) >= 0) {
291 if (bdi.cluster_size != 0) {
292 info->cluster_size = bdi.cluster_size;
293 info->has_cluster_size = true;
295 info->dirty_flag = bdi.is_dirty;
296 info->has_dirty_flag = true;
298 info->format_specific = bdrv_get_specific_info(bs, &err);
299 if (err) {
300 error_propagate(errp, err);
301 qapi_free_ImageInfo(info);
302 goto out;
304 info->has_format_specific = info->format_specific != NULL;
306 backing_filename = bs->backing_file;
307 if (backing_filename[0] != '\0') {
308 char *backing_filename2;
310 info->backing_filename = g_strdup(backing_filename);
311 info->has_backing_filename = true;
312 backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
314 /* Always report the full_backing_filename if present, even if it's the
315 * same as backing_filename. That they are same is useful info. */
316 if (backing_filename2) {
317 info->full_backing_filename = g_strdup(backing_filename2);
318 info->has_full_backing_filename = true;
321 if (bs->backing_format[0]) {
322 info->backing_filename_format = g_strdup(bs->backing_format);
323 info->has_backing_filename_format = true;
325 g_free(backing_filename2);
328 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
329 switch (ret) {
330 case 0:
331 if (info->snapshots) {
332 info->has_snapshots = true;
334 break;
335 /* recoverable error */
336 case -ENOMEDIUM:
337 case -ENOTSUP:
338 error_free(err);
339 break;
340 default:
341 error_propagate(errp, err);
342 qapi_free_ImageInfo(info);
343 goto out;
346 *p_info = info;
348 out:
349 aio_context_release(bdrv_get_aio_context(bs));
352 /* @p_info will be set only on success. */
353 static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
354 Error **errp)
356 BlockInfo *info = g_malloc0(sizeof(*info));
357 BlockDriverState *bs = blk_bs(blk);
358 char *qdev;
360 /* Skip automatically inserted nodes that the user isn't aware of */
361 bs = bdrv_skip_implicit_filters(bs);
363 info->device = g_strdup(blk_name(blk));
364 info->type = g_strdup("unknown");
365 info->locked = blk_dev_is_medium_locked(blk);
366 info->removable = blk_dev_has_removable_media(blk);
368 qdev = blk_get_attached_dev_id(blk);
369 if (qdev && *qdev) {
370 info->has_qdev = true;
371 info->qdev = qdev;
372 } else {
373 g_free(qdev);
376 if (blk_dev_has_tray(blk)) {
377 info->has_tray_open = true;
378 info->tray_open = blk_dev_is_tray_open(blk);
381 if (blk_iostatus_is_enabled(blk)) {
382 info->has_io_status = true;
383 info->io_status = blk_iostatus(blk);
386 if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
387 info->has_dirty_bitmaps = true;
388 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
391 if (bs && bs->drv) {
392 info->has_inserted = true;
393 info->inserted = bdrv_block_device_info(blk, bs, false, errp);
394 if (info->inserted == NULL) {
395 goto err;
399 *p_info = info;
400 return;
402 err:
403 qapi_free_BlockInfo(info);
406 static uint64List *uint64_list(uint64_t *list, int size)
408 int i;
409 uint64List *out_list = NULL;
410 uint64List **tail = &out_list;
412 for (i = 0; i < size; i++) {
413 QAPI_LIST_APPEND(tail, list[i]);
416 return out_list;
419 static void bdrv_latency_histogram_stats(BlockLatencyHistogram *hist,
420 bool *not_null,
421 BlockLatencyHistogramInfo **info)
423 *not_null = hist->bins != NULL;
424 if (*not_null) {
425 *info = g_new0(BlockLatencyHistogramInfo, 1);
427 (*info)->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
428 (*info)->bins = uint64_list(hist->bins, hist->nbins);
432 static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
434 BlockAcctStats *stats = blk_get_stats(blk);
435 BlockAcctTimedStats *ts = NULL;
437 ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
438 ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
439 ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
440 ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
441 ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
442 ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
444 ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
445 ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
446 ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
447 ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
449 ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
450 ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
451 ds->invalid_flush_operations =
452 stats->invalid_ops[BLOCK_ACCT_FLUSH];
453 ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
455 ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
456 ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
457 ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
458 ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
459 ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
460 ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
461 ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
462 ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
464 ds->has_idle_time_ns = stats->last_access_time_ns > 0;
465 if (ds->has_idle_time_ns) {
466 ds->idle_time_ns = block_acct_idle_time_ns(stats);
469 ds->account_invalid = stats->account_invalid;
470 ds->account_failed = stats->account_failed;
472 while ((ts = block_acct_interval_next(stats, ts))) {
473 BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
475 TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
476 TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
477 TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
479 dev_stats->interval_length = ts->interval_length;
481 dev_stats->min_rd_latency_ns = timed_average_min(rd);
482 dev_stats->max_rd_latency_ns = timed_average_max(rd);
483 dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
485 dev_stats->min_wr_latency_ns = timed_average_min(wr);
486 dev_stats->max_wr_latency_ns = timed_average_max(wr);
487 dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
489 dev_stats->min_flush_latency_ns = timed_average_min(fl);
490 dev_stats->max_flush_latency_ns = timed_average_max(fl);
491 dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
493 dev_stats->avg_rd_queue_depth =
494 block_acct_queue_depth(ts, BLOCK_ACCT_READ);
495 dev_stats->avg_wr_queue_depth =
496 block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
498 QAPI_LIST_PREPEND(ds->timed_stats, dev_stats);
501 bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_READ],
502 &ds->has_rd_latency_histogram,
503 &ds->rd_latency_histogram);
504 bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_WRITE],
505 &ds->has_wr_latency_histogram,
506 &ds->wr_latency_histogram);
507 bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_FLUSH],
508 &ds->has_flush_latency_histogram,
509 &ds->flush_latency_histogram);
512 static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
513 bool blk_level)
515 BdrvChild *parent_child;
516 BlockDriverState *filter_or_cow_bs;
517 BlockStats *s = NULL;
519 s = g_malloc0(sizeof(*s));
520 s->stats = g_malloc0(sizeof(*s->stats));
522 if (!bs) {
523 return s;
526 /* Skip automatically inserted nodes that the user isn't aware of in
527 * a BlockBackend-level command. Stay at the exact node for a node-level
528 * command. */
529 if (blk_level) {
530 bs = bdrv_skip_implicit_filters(bs);
533 if (bdrv_get_node_name(bs)[0]) {
534 s->has_node_name = true;
535 s->node_name = g_strdup(bdrv_get_node_name(bs));
538 s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
540 s->driver_specific = bdrv_get_specific_stats(bs);
541 if (s->driver_specific) {
542 s->has_driver_specific = true;
545 parent_child = bdrv_primary_child(bs);
546 if (!parent_child ||
547 !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED)))
549 BdrvChild *c;
552 * Look for a unique data-storing child. We do not need to look for
553 * filtered children, as there would be only one and it would have been
554 * the primary child.
556 parent_child = NULL;
557 QLIST_FOREACH(c, &bs->children, next) {
558 if (c->role & BDRV_CHILD_DATA) {
559 if (parent_child) {
561 * There are multiple data-storing children and we cannot
562 * choose between them.
564 parent_child = NULL;
565 break;
567 parent_child = c;
571 if (parent_child) {
572 s->has_parent = true;
573 s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level);
576 filter_or_cow_bs = bdrv_filter_or_cow_bs(bs);
577 if (blk_level && filter_or_cow_bs) {
579 * Put any filtered or COW child here (for backwards
580 * compatibility to when we put bs0->backing here, which might
581 * be either)
583 s->has_backing = true;
584 s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level);
587 return s;
590 BlockInfoList *qmp_query_block(Error **errp)
592 BlockInfoList *head = NULL, **p_next = &head;
593 BlockBackend *blk;
594 Error *local_err = NULL;
596 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
597 BlockInfoList *info;
599 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
600 continue;
603 info = g_malloc0(sizeof(*info));
604 bdrv_query_info(blk, &info->value, &local_err);
605 if (local_err) {
606 error_propagate(errp, local_err);
607 g_free(info);
608 qapi_free_BlockInfoList(head);
609 return NULL;
612 *p_next = info;
613 p_next = &info->next;
616 return head;
619 BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
620 bool query_nodes,
621 Error **errp)
623 BlockStatsList *head = NULL, **tail = &head;
624 BlockBackend *blk;
625 BlockDriverState *bs;
627 /* Just to be safe if query_nodes is not always initialized */
628 if (has_query_nodes && query_nodes) {
629 for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
630 AioContext *ctx = bdrv_get_aio_context(bs);
632 aio_context_acquire(ctx);
633 QAPI_LIST_APPEND(tail, bdrv_query_bds_stats(bs, false));
634 aio_context_release(ctx);
636 } else {
637 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
638 AioContext *ctx = blk_get_aio_context(blk);
639 BlockStats *s;
640 char *qdev;
642 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
643 continue;
646 aio_context_acquire(ctx);
647 s = bdrv_query_bds_stats(blk_bs(blk), true);
648 s->has_device = true;
649 s->device = g_strdup(blk_name(blk));
651 qdev = blk_get_attached_dev_id(blk);
652 if (qdev && *qdev) {
653 s->has_qdev = true;
654 s->qdev = qdev;
655 } else {
656 g_free(qdev);
659 bdrv_query_blk_stats(s->stats, blk);
660 aio_context_release(ctx);
662 QAPI_LIST_APPEND(tail, s);
666 return head;
669 void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
671 char date_buf[128], clock_buf[128];
672 char icount_buf[128] = {0};
673 struct tm tm;
674 time_t ti;
675 int64_t secs;
676 char *sizing = NULL;
678 if (!sn) {
679 qemu_printf("%-10s%-17s%8s%20s%13s%11s",
680 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT");
681 } else {
682 ti = sn->date_sec;
683 localtime_r(&ti, &tm);
684 strftime(date_buf, sizeof(date_buf),
685 "%Y-%m-%d %H:%M:%S", &tm);
686 secs = sn->vm_clock_nsec / 1000000000;
687 snprintf(clock_buf, sizeof(clock_buf),
688 "%02d:%02d:%02d.%03d",
689 (int)(secs / 3600),
690 (int)((secs / 60) % 60),
691 (int)(secs % 60),
692 (int)((sn->vm_clock_nsec / 1000000) % 1000));
693 sizing = size_to_str(sn->vm_state_size);
694 if (sn->icount != -1ULL) {
695 snprintf(icount_buf, sizeof(icount_buf),
696 "%"PRId64, sn->icount);
698 qemu_printf("%-9s %-16s %8s%20s%13s%11s",
699 sn->id_str, sn->name,
700 sizing,
701 date_buf,
702 clock_buf,
703 icount_buf);
705 g_free(sizing);
708 static void dump_qdict(int indentation, QDict *dict);
709 static void dump_qlist(int indentation, QList *list);
711 static void dump_qobject(int comp_indent, QObject *obj)
713 switch (qobject_type(obj)) {
714 case QTYPE_QNUM: {
715 QNum *value = qobject_to(QNum, obj);
716 char *tmp = qnum_to_string(value);
717 qemu_printf("%s", tmp);
718 g_free(tmp);
719 break;
721 case QTYPE_QSTRING: {
722 QString *value = qobject_to(QString, obj);
723 qemu_printf("%s", qstring_get_str(value));
724 break;
726 case QTYPE_QDICT: {
727 QDict *value = qobject_to(QDict, obj);
728 dump_qdict(comp_indent, value);
729 break;
731 case QTYPE_QLIST: {
732 QList *value = qobject_to(QList, obj);
733 dump_qlist(comp_indent, value);
734 break;
736 case QTYPE_QBOOL: {
737 QBool *value = qobject_to(QBool, obj);
738 qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
739 break;
741 default:
742 abort();
746 static void dump_qlist(int indentation, QList *list)
748 const QListEntry *entry;
749 int i = 0;
751 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
752 QType type = qobject_type(entry->value);
753 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
754 qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
755 composite ? '\n' : ' ');
756 dump_qobject(indentation + 1, entry->value);
757 if (!composite) {
758 qemu_printf("\n");
763 static void dump_qdict(int indentation, QDict *dict)
765 const QDictEntry *entry;
767 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
768 QType type = qobject_type(entry->value);
769 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
770 char *key = g_malloc(strlen(entry->key) + 1);
771 int i;
773 /* replace dashes with spaces in key (variable) names */
774 for (i = 0; entry->key[i]; i++) {
775 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
777 key[i] = 0;
778 qemu_printf("%*s%s:%c", indentation * 4, "", key,
779 composite ? '\n' : ' ');
780 dump_qobject(indentation + 1, entry->value);
781 if (!composite) {
782 qemu_printf("\n");
784 g_free(key);
788 void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec)
790 QObject *obj, *data;
791 Visitor *v = qobject_output_visitor_new(&obj);
793 visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
794 visit_complete(v, &obj);
795 data = qdict_get(qobject_to(QDict, obj), "data");
796 dump_qobject(1, data);
797 qobject_unref(obj);
798 visit_free(v);
801 void bdrv_image_info_dump(ImageInfo *info)
803 char *size_buf, *dsize_buf;
804 if (!info->has_actual_size) {
805 dsize_buf = g_strdup("unavailable");
806 } else {
807 dsize_buf = size_to_str(info->actual_size);
809 size_buf = size_to_str(info->virtual_size);
810 qemu_printf("image: %s\n"
811 "file format: %s\n"
812 "virtual size: %s (%" PRId64 " bytes)\n"
813 "disk size: %s\n",
814 info->filename, info->format, size_buf,
815 info->virtual_size,
816 dsize_buf);
817 g_free(size_buf);
818 g_free(dsize_buf);
820 if (info->has_encrypted && info->encrypted) {
821 qemu_printf("encrypted: yes\n");
824 if (info->has_cluster_size) {
825 qemu_printf("cluster_size: %" PRId64 "\n",
826 info->cluster_size);
829 if (info->has_dirty_flag && info->dirty_flag) {
830 qemu_printf("cleanly shut down: no\n");
833 if (info->has_backing_filename) {
834 qemu_printf("backing file: %s", info->backing_filename);
835 if (!info->has_full_backing_filename) {
836 qemu_printf(" (cannot determine actual path)");
837 } else if (strcmp(info->backing_filename,
838 info->full_backing_filename) != 0) {
839 qemu_printf(" (actual path: %s)", info->full_backing_filename);
841 qemu_printf("\n");
842 if (info->has_backing_filename_format) {
843 qemu_printf("backing file format: %s\n",
844 info->backing_filename_format);
848 if (info->has_snapshots) {
849 SnapshotInfoList *elem;
851 qemu_printf("Snapshot list:\n");
852 bdrv_snapshot_dump(NULL);
853 qemu_printf("\n");
855 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
856 * we convert to the block layer's native QEMUSnapshotInfo for now.
858 for (elem = info->snapshots; elem; elem = elem->next) {
859 QEMUSnapshotInfo sn = {
860 .vm_state_size = elem->value->vm_state_size,
861 .date_sec = elem->value->date_sec,
862 .date_nsec = elem->value->date_nsec,
863 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
864 elem->value->vm_clock_nsec,
865 .icount = elem->value->has_icount ?
866 elem->value->icount : -1ULL,
869 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
870 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
871 bdrv_snapshot_dump(&sn);
872 qemu_printf("\n");
876 if (info->has_format_specific) {
877 qemu_printf("Format specific information:\n");
878 bdrv_image_info_specific_dump(info->format_specific);