loader: check get_image_size() return value
[qemu/ar7.git] / block / qapi.c
blobd2b18ee9df722e08a65a94d83aaa01fd0f76b385
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 "block/qapi.h"
27 #include "block/block_int.h"
28 #include "block/throttle-groups.h"
29 #include "block/write-threshold.h"
30 #include "qmp-commands.h"
31 #include "qapi-visit.h"
32 #include "qapi/qobject-output-visitor.h"
33 #include "qapi/qmp/types.h"
34 #include "sysemu/block-backend.h"
35 #include "qemu/cutils.h"
37 BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
38 BlockDriverState *bs, Error **errp)
40 ImageInfo **p_image_info;
41 BlockDriverState *bs0;
42 BlockDeviceInfo *info = g_malloc0(sizeof(*info));
44 info->file = g_strdup(bs->filename);
45 info->ro = bs->read_only;
46 info->drv = g_strdup(bs->drv->format_name);
47 info->encrypted = bs->encrypted;
48 info->encryption_key_missing = false;
50 info->cache = g_new(BlockdevCacheInfo, 1);
51 *info->cache = (BlockdevCacheInfo) {
52 .writeback = blk ? blk_enable_write_cache(blk) : true,
53 .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
54 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
57 if (bs->node_name[0]) {
58 info->has_node_name = true;
59 info->node_name = g_strdup(bs->node_name);
62 if (bs->backing_file[0]) {
63 info->has_backing_file = true;
64 info->backing_file = g_strdup(bs->backing_file);
67 info->detect_zeroes = bs->detect_zeroes;
69 if (blk && blk_get_public(blk)->throttle_state) {
70 ThrottleConfig cfg;
72 throttle_group_get_config(blk, &cfg);
74 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
75 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg;
76 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg;
78 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
79 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
80 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
82 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
83 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
84 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
85 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
86 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
87 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
89 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
90 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
91 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
92 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
93 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
94 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
96 info->has_bps_max_length = info->has_bps_max;
97 info->bps_max_length =
98 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
99 info->has_bps_rd_max_length = info->has_bps_rd_max;
100 info->bps_rd_max_length =
101 cfg.buckets[THROTTLE_BPS_READ].burst_length;
102 info->has_bps_wr_max_length = info->has_bps_wr_max;
103 info->bps_wr_max_length =
104 cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
106 info->has_iops_max_length = info->has_iops_max;
107 info->iops_max_length =
108 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
109 info->has_iops_rd_max_length = info->has_iops_rd_max;
110 info->iops_rd_max_length =
111 cfg.buckets[THROTTLE_OPS_READ].burst_length;
112 info->has_iops_wr_max_length = info->has_iops_wr_max;
113 info->iops_wr_max_length =
114 cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
116 info->has_iops_size = cfg.op_size;
117 info->iops_size = cfg.op_size;
119 info->has_group = true;
120 info->group = g_strdup(throttle_group_get_name(blk));
123 info->write_threshold = bdrv_write_threshold_get(bs);
125 bs0 = bs;
126 p_image_info = &info->image;
127 info->backing_file_depth = 0;
128 while (1) {
129 Error *local_err = NULL;
130 bdrv_query_image_info(bs0, p_image_info, &local_err);
131 if (local_err) {
132 error_propagate(errp, local_err);
133 qapi_free_BlockDeviceInfo(info);
134 return NULL;
137 if (bs0->drv && bs0->backing) {
138 info->backing_file_depth++;
139 bs0 = bs0->backing->bs;
140 (*p_image_info)->has_backing_image = true;
141 p_image_info = &((*p_image_info)->backing_image);
142 } else {
143 break;
146 /* Skip automatically inserted nodes that the user isn't aware of for
147 * query-block (blk != NULL), but not for query-named-block-nodes */
148 while (blk && bs0 && bs0->drv && bs0->implicit) {
149 bs0 = backing_bs(bs0);
153 return info;
157 * Returns 0 on success, with *p_list either set to describe snapshot
158 * information, or NULL because there are no snapshots. Returns -errno on
159 * error, with *p_list untouched.
161 int bdrv_query_snapshot_info_list(BlockDriverState *bs,
162 SnapshotInfoList **p_list,
163 Error **errp)
165 int i, sn_count;
166 QEMUSnapshotInfo *sn_tab = NULL;
167 SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
168 SnapshotInfo *info;
170 sn_count = bdrv_snapshot_list(bs, &sn_tab);
171 if (sn_count < 0) {
172 const char *dev = bdrv_get_device_name(bs);
173 switch (sn_count) {
174 case -ENOMEDIUM:
175 error_setg(errp, "Device '%s' is not inserted", dev);
176 break;
177 case -ENOTSUP:
178 error_setg(errp,
179 "Device '%s' does not support internal snapshots",
180 dev);
181 break;
182 default:
183 error_setg_errno(errp, -sn_count,
184 "Can't list snapshots of device '%s'", dev);
185 break;
187 return sn_count;
190 for (i = 0; i < sn_count; i++) {
191 info = g_new0(SnapshotInfo, 1);
192 info->id = g_strdup(sn_tab[i].id_str);
193 info->name = g_strdup(sn_tab[i].name);
194 info->vm_state_size = sn_tab[i].vm_state_size;
195 info->date_sec = sn_tab[i].date_sec;
196 info->date_nsec = sn_tab[i].date_nsec;
197 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
198 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
200 info_list = g_new0(SnapshotInfoList, 1);
201 info_list->value = info;
203 /* XXX: waiting for the qapi to support qemu-queue.h types */
204 if (!cur_item) {
205 head = cur_item = info_list;
206 } else {
207 cur_item->next = info_list;
208 cur_item = info_list;
213 g_free(sn_tab);
214 *p_list = head;
215 return 0;
219 * bdrv_query_image_info:
220 * @bs: block device to examine
221 * @p_info: location to store image information
222 * @errp: location to store error information
224 * Store "flat" image information in @p_info.
226 * "Flat" means it does *not* query backing image information,
227 * i.e. (*pinfo)->has_backing_image will be set to false and
228 * (*pinfo)->backing_image to NULL even when the image does in fact have
229 * a backing image.
231 * @p_info will be set only on success. On error, store error in @errp.
233 void bdrv_query_image_info(BlockDriverState *bs,
234 ImageInfo **p_info,
235 Error **errp)
237 int64_t size;
238 const char *backing_filename;
239 BlockDriverInfo bdi;
240 int ret;
241 Error *err = NULL;
242 ImageInfo *info;
244 aio_context_acquire(bdrv_get_aio_context(bs));
246 size = bdrv_getlength(bs);
247 if (size < 0) {
248 error_setg_errno(errp, -size, "Can't get image size '%s'",
249 bs->exact_filename);
250 goto out;
253 info = g_new0(ImageInfo, 1);
254 info->filename = g_strdup(bs->filename);
255 info->format = g_strdup(bdrv_get_format_name(bs));
256 info->virtual_size = size;
257 info->actual_size = bdrv_get_allocated_file_size(bs);
258 info->has_actual_size = info->actual_size >= 0;
259 if (bdrv_is_encrypted(bs)) {
260 info->encrypted = true;
261 info->has_encrypted = true;
263 if (bdrv_get_info(bs, &bdi) >= 0) {
264 if (bdi.cluster_size != 0) {
265 info->cluster_size = bdi.cluster_size;
266 info->has_cluster_size = true;
268 info->dirty_flag = bdi.is_dirty;
269 info->has_dirty_flag = true;
271 info->format_specific = bdrv_get_specific_info(bs);
272 info->has_format_specific = info->format_specific != NULL;
274 backing_filename = bs->backing_file;
275 if (backing_filename[0] != '\0') {
276 char *backing_filename2 = g_malloc0(PATH_MAX);
277 info->backing_filename = g_strdup(backing_filename);
278 info->has_backing_filename = true;
279 bdrv_get_full_backing_filename(bs, backing_filename2, PATH_MAX, &err);
280 if (err) {
281 /* Can't reconstruct the full backing filename, so we must omit
282 * this field and apply a Best Effort to this query. */
283 g_free(backing_filename2);
284 backing_filename2 = NULL;
285 error_free(err);
286 err = NULL;
289 /* Always report the full_backing_filename if present, even if it's the
290 * same as backing_filename. That they are same is useful info. */
291 if (backing_filename2) {
292 info->full_backing_filename = g_strdup(backing_filename2);
293 info->has_full_backing_filename = true;
296 if (bs->backing_format[0]) {
297 info->backing_filename_format = g_strdup(bs->backing_format);
298 info->has_backing_filename_format = true;
300 g_free(backing_filename2);
303 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
304 switch (ret) {
305 case 0:
306 if (info->snapshots) {
307 info->has_snapshots = true;
309 break;
310 /* recoverable error */
311 case -ENOMEDIUM:
312 case -ENOTSUP:
313 error_free(err);
314 break;
315 default:
316 error_propagate(errp, err);
317 qapi_free_ImageInfo(info);
318 goto out;
321 *p_info = info;
323 out:
324 aio_context_release(bdrv_get_aio_context(bs));
327 /* @p_info will be set only on success. */
328 static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
329 Error **errp)
331 BlockInfo *info = g_malloc0(sizeof(*info));
332 BlockDriverState *bs = blk_bs(blk);
333 char *qdev;
335 /* Skip automatically inserted nodes that the user isn't aware of */
336 while (bs && bs->drv && bs->implicit) {
337 bs = backing_bs(bs);
340 info->device = g_strdup(blk_name(blk));
341 info->type = g_strdup("unknown");
342 info->locked = blk_dev_is_medium_locked(blk);
343 info->removable = blk_dev_has_removable_media(blk);
345 qdev = blk_get_attached_dev_id(blk);
346 if (qdev && *qdev) {
347 info->has_qdev = true;
348 info->qdev = qdev;
349 } else {
350 g_free(qdev);
353 if (blk_dev_has_tray(blk)) {
354 info->has_tray_open = true;
355 info->tray_open = blk_dev_is_tray_open(blk);
358 if (blk_iostatus_is_enabled(blk)) {
359 info->has_io_status = true;
360 info->io_status = blk_iostatus(blk);
363 if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
364 info->has_dirty_bitmaps = true;
365 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
368 if (bs && bs->drv) {
369 info->has_inserted = true;
370 info->inserted = bdrv_block_device_info(blk, bs, errp);
371 if (info->inserted == NULL) {
372 goto err;
376 *p_info = info;
377 return;
379 err:
380 qapi_free_BlockInfo(info);
383 static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
385 BlockAcctStats *stats = blk_get_stats(blk);
386 BlockAcctTimedStats *ts = NULL;
388 ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
389 ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
390 ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
391 ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
393 ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
394 ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
395 ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
397 ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
398 ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
399 ds->invalid_flush_operations =
400 stats->invalid_ops[BLOCK_ACCT_FLUSH];
402 ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
403 ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
404 ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
405 ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
406 ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
407 ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
409 ds->has_idle_time_ns = stats->last_access_time_ns > 0;
410 if (ds->has_idle_time_ns) {
411 ds->idle_time_ns = block_acct_idle_time_ns(stats);
414 ds->account_invalid = stats->account_invalid;
415 ds->account_failed = stats->account_failed;
417 while ((ts = block_acct_interval_next(stats, ts))) {
418 BlockDeviceTimedStatsList *timed_stats =
419 g_malloc0(sizeof(*timed_stats));
420 BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
421 timed_stats->next = ds->timed_stats;
422 timed_stats->value = dev_stats;
423 ds->timed_stats = timed_stats;
425 TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
426 TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
427 TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
429 dev_stats->interval_length = ts->interval_length;
431 dev_stats->min_rd_latency_ns = timed_average_min(rd);
432 dev_stats->max_rd_latency_ns = timed_average_max(rd);
433 dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
435 dev_stats->min_wr_latency_ns = timed_average_min(wr);
436 dev_stats->max_wr_latency_ns = timed_average_max(wr);
437 dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
439 dev_stats->min_flush_latency_ns = timed_average_min(fl);
440 dev_stats->max_flush_latency_ns = timed_average_max(fl);
441 dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
443 dev_stats->avg_rd_queue_depth =
444 block_acct_queue_depth(ts, BLOCK_ACCT_READ);
445 dev_stats->avg_wr_queue_depth =
446 block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
450 static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
451 bool blk_level)
453 BlockStats *s = NULL;
455 s = g_malloc0(sizeof(*s));
456 s->stats = g_malloc0(sizeof(*s->stats));
458 if (!bs) {
459 return s;
462 /* Skip automatically inserted nodes that the user isn't aware of in
463 * a BlockBackend-level command. Stay at the exact node for a node-level
464 * command. */
465 while (blk_level && bs->drv && bs->implicit) {
466 bs = backing_bs(bs);
467 assert(bs);
470 if (bdrv_get_node_name(bs)[0]) {
471 s->has_node_name = true;
472 s->node_name = g_strdup(bdrv_get_node_name(bs));
475 s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
477 if (bs->file) {
478 s->has_parent = true;
479 s->parent = bdrv_query_bds_stats(bs->file->bs, blk_level);
482 if (blk_level && bs->backing) {
483 s->has_backing = true;
484 s->backing = bdrv_query_bds_stats(bs->backing->bs, blk_level);
487 return s;
490 BlockInfoList *qmp_query_block(Error **errp)
492 BlockInfoList *head = NULL, **p_next = &head;
493 BlockBackend *blk;
494 Error *local_err = NULL;
496 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
497 BlockInfoList *info;
499 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
500 continue;
503 info = g_malloc0(sizeof(*info));
504 bdrv_query_info(blk, &info->value, &local_err);
505 if (local_err) {
506 error_propagate(errp, local_err);
507 g_free(info);
508 qapi_free_BlockInfoList(head);
509 return NULL;
512 *p_next = info;
513 p_next = &info->next;
516 return head;
519 BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
520 bool query_nodes,
521 Error **errp)
523 BlockStatsList *head = NULL, **p_next = &head;
524 BlockBackend *blk;
525 BlockDriverState *bs;
527 /* Just to be safe if query_nodes is not always initialized */
528 if (has_query_nodes && query_nodes) {
529 for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
530 BlockStatsList *info = g_malloc0(sizeof(*info));
531 AioContext *ctx = bdrv_get_aio_context(bs);
533 aio_context_acquire(ctx);
534 info->value = bdrv_query_bds_stats(bs, false);
535 aio_context_release(ctx);
537 *p_next = info;
538 p_next = &info->next;
540 } else {
541 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
542 BlockStatsList *info = g_malloc0(sizeof(*info));
543 AioContext *ctx = blk_get_aio_context(blk);
544 BlockStats *s;
546 aio_context_acquire(ctx);
547 s = bdrv_query_bds_stats(blk_bs(blk), true);
548 s->has_device = true;
549 s->device = g_strdup(blk_name(blk));
550 bdrv_query_blk_stats(s->stats, blk);
551 aio_context_release(ctx);
553 info->value = s;
554 *p_next = info;
555 p_next = &info->next;
559 return head;
562 #define NB_SUFFIXES 4
564 static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
566 static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'};
567 int64_t base;
568 int i;
570 if (size <= 999) {
571 snprintf(buf, buf_size, "%" PRId64, size);
572 } else {
573 base = 1024;
574 for (i = 0; i < NB_SUFFIXES; i++) {
575 if (size < (10 * base)) {
576 snprintf(buf, buf_size, "%0.1f%c",
577 (double)size / base,
578 suffixes[i]);
579 break;
580 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
581 snprintf(buf, buf_size, "%" PRId64 "%c",
582 ((size + (base >> 1)) / base),
583 suffixes[i]);
584 break;
586 base = base * 1024;
589 return buf;
592 void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
593 QEMUSnapshotInfo *sn)
595 char buf1[128], date_buf[128], clock_buf[128];
596 struct tm tm;
597 time_t ti;
598 int64_t secs;
600 if (!sn) {
601 func_fprintf(f,
602 "%-10s%-20s%7s%20s%15s",
603 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
604 } else {
605 ti = sn->date_sec;
606 localtime_r(&ti, &tm);
607 strftime(date_buf, sizeof(date_buf),
608 "%Y-%m-%d %H:%M:%S", &tm);
609 secs = sn->vm_clock_nsec / 1000000000;
610 snprintf(clock_buf, sizeof(clock_buf),
611 "%02d:%02d:%02d.%03d",
612 (int)(secs / 3600),
613 (int)((secs / 60) % 60),
614 (int)(secs % 60),
615 (int)((sn->vm_clock_nsec / 1000000) % 1000));
616 func_fprintf(f,
617 "%-10s%-20s%7s%20s%15s",
618 sn->id_str, sn->name,
619 get_human_readable_size(buf1, sizeof(buf1),
620 sn->vm_state_size),
621 date_buf,
622 clock_buf);
626 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
627 QDict *dict);
628 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
629 QList *list);
631 static void dump_qobject(fprintf_function func_fprintf, void *f,
632 int comp_indent, QObject *obj)
634 switch (qobject_type(obj)) {
635 case QTYPE_QNUM: {
636 QNum *value = qobject_to_qnum(obj);
637 char *tmp = qnum_to_string(value);
638 func_fprintf(f, "%s", tmp);
639 g_free(tmp);
640 break;
642 case QTYPE_QSTRING: {
643 QString *value = qobject_to_qstring(obj);
644 func_fprintf(f, "%s", qstring_get_str(value));
645 break;
647 case QTYPE_QDICT: {
648 QDict *value = qobject_to_qdict(obj);
649 dump_qdict(func_fprintf, f, comp_indent, value);
650 break;
652 case QTYPE_QLIST: {
653 QList *value = qobject_to_qlist(obj);
654 dump_qlist(func_fprintf, f, comp_indent, value);
655 break;
657 case QTYPE_QBOOL: {
658 QBool *value = qobject_to_qbool(obj);
659 func_fprintf(f, "%s", qbool_get_bool(value) ? "true" : "false");
660 break;
662 default:
663 abort();
667 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
668 QList *list)
670 const QListEntry *entry;
671 int i = 0;
673 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
674 QType type = qobject_type(entry->value);
675 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
676 func_fprintf(f, "%*s[%i]:%c", indentation * 4, "", i,
677 composite ? '\n' : ' ');
678 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
679 if (!composite) {
680 func_fprintf(f, "\n");
685 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
686 QDict *dict)
688 const QDictEntry *entry;
690 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
691 QType type = qobject_type(entry->value);
692 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
693 char *key = g_malloc(strlen(entry->key) + 1);
694 int i;
696 /* replace dashes with spaces in key (variable) names */
697 for (i = 0; entry->key[i]; i++) {
698 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
700 key[i] = 0;
701 func_fprintf(f, "%*s%s:%c", indentation * 4, "", key,
702 composite ? '\n' : ' ');
703 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
704 if (!composite) {
705 func_fprintf(f, "\n");
707 g_free(key);
711 void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
712 ImageInfoSpecific *info_spec)
714 QObject *obj, *data;
715 Visitor *v = qobject_output_visitor_new(&obj);
717 visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
718 visit_complete(v, &obj);
719 data = qdict_get(qobject_to_qdict(obj), "data");
720 dump_qobject(func_fprintf, f, 1, data);
721 qobject_decref(obj);
722 visit_free(v);
725 void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
726 ImageInfo *info)
728 char size_buf[128], dsize_buf[128];
729 if (!info->has_actual_size) {
730 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
731 } else {
732 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
733 info->actual_size);
735 get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
736 func_fprintf(f,
737 "image: %s\n"
738 "file format: %s\n"
739 "virtual size: %s (%" PRId64 " bytes)\n"
740 "disk size: %s\n",
741 info->filename, info->format, size_buf,
742 info->virtual_size,
743 dsize_buf);
745 if (info->has_encrypted && info->encrypted) {
746 func_fprintf(f, "encrypted: yes\n");
749 if (info->has_cluster_size) {
750 func_fprintf(f, "cluster_size: %" PRId64 "\n",
751 info->cluster_size);
754 if (info->has_dirty_flag && info->dirty_flag) {
755 func_fprintf(f, "cleanly shut down: no\n");
758 if (info->has_backing_filename) {
759 func_fprintf(f, "backing file: %s", info->backing_filename);
760 if (!info->has_full_backing_filename) {
761 func_fprintf(f, " (cannot determine actual path)");
762 } else if (strcmp(info->backing_filename,
763 info->full_backing_filename) != 0) {
764 func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
766 func_fprintf(f, "\n");
767 if (info->has_backing_filename_format) {
768 func_fprintf(f, "backing file format: %s\n",
769 info->backing_filename_format);
773 if (info->has_snapshots) {
774 SnapshotInfoList *elem;
776 func_fprintf(f, "Snapshot list:\n");
777 bdrv_snapshot_dump(func_fprintf, f, NULL);
778 func_fprintf(f, "\n");
780 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
781 * we convert to the block layer's native QEMUSnapshotInfo for now.
783 for (elem = info->snapshots; elem; elem = elem->next) {
784 QEMUSnapshotInfo sn = {
785 .vm_state_size = elem->value->vm_state_size,
786 .date_sec = elem->value->date_sec,
787 .date_nsec = elem->value->date_nsec,
788 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
789 elem->value->vm_clock_nsec,
792 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
793 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
794 bdrv_snapshot_dump(func_fprintf, f, &sn);
795 func_fprintf(f, "\n");
799 if (info->has_format_specific) {
800 func_fprintf(f, "Format specific information:\n");
801 bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);