xref: /openbmc/qemu/block/qapi.c (revision c964b660)
1 /*
2  * Block layer qmp and info dump related functions
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
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/qmp-output-visitor.h"
33 #include "qapi/qmp/types.h"
34 #include "sysemu/block-backend.h"
35 
36 BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs, Error **errp)
37 {
38     ImageInfo **p_image_info;
39     BlockDriverState *bs0;
40     BlockDeviceInfo *info = g_malloc0(sizeof(*info));
41 
42     info->file                   = g_strdup(bs->filename);
43     info->ro                     = bs->read_only;
44     info->drv                    = g_strdup(bs->drv->format_name);
45     info->encrypted              = bs->encrypted;
46     info->encryption_key_missing = bdrv_key_required(bs);
47 
48     info->cache = g_new(BlockdevCacheInfo, 1);
49     *info->cache = (BlockdevCacheInfo) {
50         .writeback      = bdrv_enable_write_cache(bs),
51         .direct         = !!(bs->open_flags & BDRV_O_NOCACHE),
52         .no_flush       = !!(bs->open_flags & BDRV_O_NO_FLUSH),
53     };
54 
55     if (bs->node_name[0]) {
56         info->has_node_name = true;
57         info->node_name = g_strdup(bs->node_name);
58     }
59 
60     if (bs->backing_file[0]) {
61         info->has_backing_file = true;
62         info->backing_file = g_strdup(bs->backing_file);
63     }
64 
65     info->backing_file_depth = bdrv_get_backing_file_depth(bs);
66     info->detect_zeroes = bs->detect_zeroes;
67 
68     if (bs->throttle_state) {
69         ThrottleConfig cfg;
70 
71         throttle_group_get_config(bs, &cfg);
72 
73         info->bps     = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
74         info->bps_rd  = cfg.buckets[THROTTLE_BPS_READ].avg;
75         info->bps_wr  = cfg.buckets[THROTTLE_BPS_WRITE].avg;
76 
77         info->iops    = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
78         info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
79         info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
80 
81         info->has_bps_max     = cfg.buckets[THROTTLE_BPS_TOTAL].max;
82         info->bps_max         = cfg.buckets[THROTTLE_BPS_TOTAL].max;
83         info->has_bps_rd_max  = cfg.buckets[THROTTLE_BPS_READ].max;
84         info->bps_rd_max      = cfg.buckets[THROTTLE_BPS_READ].max;
85         info->has_bps_wr_max  = cfg.buckets[THROTTLE_BPS_WRITE].max;
86         info->bps_wr_max      = cfg.buckets[THROTTLE_BPS_WRITE].max;
87 
88         info->has_iops_max    = cfg.buckets[THROTTLE_OPS_TOTAL].max;
89         info->iops_max        = cfg.buckets[THROTTLE_OPS_TOTAL].max;
90         info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
91         info->iops_rd_max     = cfg.buckets[THROTTLE_OPS_READ].max;
92         info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
93         info->iops_wr_max     = cfg.buckets[THROTTLE_OPS_WRITE].max;
94 
95         info->has_iops_size = cfg.op_size;
96         info->iops_size = cfg.op_size;
97 
98         info->has_group = true;
99         info->group = g_strdup(throttle_group_get_name(bs));
100     }
101 
102     info->write_threshold = bdrv_write_threshold_get(bs);
103 
104     bs0 = bs;
105     p_image_info = &info->image;
106     while (1) {
107         Error *local_err = NULL;
108         bdrv_query_image_info(bs0, p_image_info, &local_err);
109         if (local_err) {
110             error_propagate(errp, local_err);
111             qapi_free_BlockDeviceInfo(info);
112             return NULL;
113         }
114         if (bs0->drv && bs0->backing) {
115             bs0 = bs0->backing->bs;
116             (*p_image_info)->has_backing_image = true;
117             p_image_info = &((*p_image_info)->backing_image);
118         } else {
119             break;
120         }
121     }
122 
123     return info;
124 }
125 
126 /*
127  * Returns 0 on success, with *p_list either set to describe snapshot
128  * information, or NULL because there are no snapshots.  Returns -errno on
129  * error, with *p_list untouched.
130  */
131 int bdrv_query_snapshot_info_list(BlockDriverState *bs,
132                                   SnapshotInfoList **p_list,
133                                   Error **errp)
134 {
135     int i, sn_count;
136     QEMUSnapshotInfo *sn_tab = NULL;
137     SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
138     SnapshotInfo *info;
139 
140     sn_count = bdrv_snapshot_list(bs, &sn_tab);
141     if (sn_count < 0) {
142         const char *dev = bdrv_get_device_name(bs);
143         switch (sn_count) {
144         case -ENOMEDIUM:
145             error_setg(errp, "Device '%s' is not inserted", dev);
146             break;
147         case -ENOTSUP:
148             error_setg(errp,
149                        "Device '%s' does not support internal snapshots",
150                        dev);
151             break;
152         default:
153             error_setg_errno(errp, -sn_count,
154                              "Can't list snapshots of device '%s'", dev);
155             break;
156         }
157         return sn_count;
158     }
159 
160     for (i = 0; i < sn_count; i++) {
161         info = g_new0(SnapshotInfo, 1);
162         info->id            = g_strdup(sn_tab[i].id_str);
163         info->name          = g_strdup(sn_tab[i].name);
164         info->vm_state_size = sn_tab[i].vm_state_size;
165         info->date_sec      = sn_tab[i].date_sec;
166         info->date_nsec     = sn_tab[i].date_nsec;
167         info->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
168         info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
169 
170         info_list = g_new0(SnapshotInfoList, 1);
171         info_list->value = info;
172 
173         /* XXX: waiting for the qapi to support qemu-queue.h types */
174         if (!cur_item) {
175             head = cur_item = info_list;
176         } else {
177             cur_item->next = info_list;
178             cur_item = info_list;
179         }
180 
181     }
182 
183     g_free(sn_tab);
184     *p_list = head;
185     return 0;
186 }
187 
188 /**
189  * bdrv_query_image_info:
190  * @bs: block device to examine
191  * @p_info: location to store image information
192  * @errp: location to store error information
193  *
194  * Store "flat" image information in @p_info.
195  *
196  * "Flat" means it does *not* query backing image information,
197  * i.e. (*pinfo)->has_backing_image will be set to false and
198  * (*pinfo)->backing_image to NULL even when the image does in fact have
199  * a backing image.
200  *
201  * @p_info will be set only on success. On error, store error in @errp.
202  */
203 void bdrv_query_image_info(BlockDriverState *bs,
204                            ImageInfo **p_info,
205                            Error **errp)
206 {
207     int64_t size;
208     const char *backing_filename;
209     BlockDriverInfo bdi;
210     int ret;
211     Error *err = NULL;
212     ImageInfo *info;
213 
214     aio_context_acquire(bdrv_get_aio_context(bs));
215 
216     size = bdrv_getlength(bs);
217     if (size < 0) {
218         error_setg_errno(errp, -size, "Can't get size of device '%s'",
219                          bdrv_get_device_name(bs));
220         goto out;
221     }
222 
223     info = g_new0(ImageInfo, 1);
224     info->filename        = g_strdup(bs->filename);
225     info->format          = g_strdup(bdrv_get_format_name(bs));
226     info->virtual_size    = size;
227     info->actual_size     = bdrv_get_allocated_file_size(bs);
228     info->has_actual_size = info->actual_size >= 0;
229     if (bdrv_is_encrypted(bs)) {
230         info->encrypted = true;
231         info->has_encrypted = true;
232     }
233     if (bdrv_get_info(bs, &bdi) >= 0) {
234         if (bdi.cluster_size != 0) {
235             info->cluster_size = bdi.cluster_size;
236             info->has_cluster_size = true;
237         }
238         info->dirty_flag = bdi.is_dirty;
239         info->has_dirty_flag = true;
240     }
241     info->format_specific     = bdrv_get_specific_info(bs);
242     info->has_format_specific = info->format_specific != NULL;
243 
244     backing_filename = bs->backing_file;
245     if (backing_filename[0] != '\0') {
246         char *backing_filename2 = g_malloc0(PATH_MAX);
247         info->backing_filename = g_strdup(backing_filename);
248         info->has_backing_filename = true;
249         bdrv_get_full_backing_filename(bs, backing_filename2, PATH_MAX, &err);
250         if (err) {
251             /* Can't reconstruct the full backing filename, so we must omit
252              * this field and apply a Best Effort to this query. */
253             g_free(backing_filename2);
254             backing_filename2 = NULL;
255             error_free(err);
256             err = NULL;
257         }
258 
259         /* Always report the full_backing_filename if present, even if it's the
260          * same as backing_filename. That they are same is useful info. */
261         if (backing_filename2) {
262             info->full_backing_filename = g_strdup(backing_filename2);
263             info->has_full_backing_filename = true;
264         }
265 
266         if (bs->backing_format[0]) {
267             info->backing_filename_format = g_strdup(bs->backing_format);
268             info->has_backing_filename_format = true;
269         }
270         g_free(backing_filename2);
271     }
272 
273     ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
274     switch (ret) {
275     case 0:
276         if (info->snapshots) {
277             info->has_snapshots = true;
278         }
279         break;
280     /* recoverable error */
281     case -ENOMEDIUM:
282     case -ENOTSUP:
283         error_free(err);
284         break;
285     default:
286         error_propagate(errp, err);
287         qapi_free_ImageInfo(info);
288         goto out;
289     }
290 
291     *p_info = info;
292 
293 out:
294     aio_context_release(bdrv_get_aio_context(bs));
295 }
296 
297 /* @p_info will be set only on success. */
298 static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
299                             Error **errp)
300 {
301     BlockInfo *info = g_malloc0(sizeof(*info));
302     BlockDriverState *bs = blk_bs(blk);
303     info->device = g_strdup(blk_name(blk));
304     info->type = g_strdup("unknown");
305     info->locked = blk_dev_is_medium_locked(blk);
306     info->removable = blk_dev_has_removable_media(blk);
307 
308     if (blk_dev_has_tray(blk)) {
309         info->has_tray_open = true;
310         info->tray_open = blk_dev_is_tray_open(blk);
311     }
312 
313     if (blk_iostatus_is_enabled(blk)) {
314         info->has_io_status = true;
315         info->io_status = blk_iostatus(blk);
316     }
317 
318     if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
319         info->has_dirty_bitmaps = true;
320         info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
321     }
322 
323     if (bs && bs->drv) {
324         info->has_inserted = true;
325         info->inserted = bdrv_block_device_info(bs, errp);
326         if (info->inserted == NULL) {
327             goto err;
328         }
329     }
330 
331     *p_info = info;
332     return;
333 
334  err:
335     qapi_free_BlockInfo(info);
336 }
337 
338 static BlockStats *bdrv_query_stats(const BlockDriverState *bs,
339                                     bool query_backing)
340 {
341     BlockStats *s;
342 
343     s = g_malloc0(sizeof(*s));
344 
345     if (bdrv_get_device_name(bs)[0]) {
346         s->has_device = true;
347         s->device = g_strdup(bdrv_get_device_name(bs));
348     }
349 
350     if (bdrv_get_node_name(bs)[0]) {
351         s->has_node_name = true;
352         s->node_name = g_strdup(bdrv_get_node_name(bs));
353     }
354 
355     s->stats = g_malloc0(sizeof(*s->stats));
356     if (bs->blk) {
357         BlockAcctStats *stats = blk_get_stats(bs->blk);
358         BlockAcctTimedStats *ts = NULL;
359 
360         s->stats->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
361         s->stats->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
362         s->stats->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
363         s->stats->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
364 
365         s->stats->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
366         s->stats->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
367         s->stats->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
368 
369         s->stats->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
370         s->stats->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
371         s->stats->invalid_flush_operations =
372             stats->invalid_ops[BLOCK_ACCT_FLUSH];
373 
374         s->stats->rd_merged = stats->merged[BLOCK_ACCT_READ];
375         s->stats->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
376         s->stats->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
377         s->stats->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
378         s->stats->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
379         s->stats->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
380 
381         s->stats->has_idle_time_ns = stats->last_access_time_ns > 0;
382         if (s->stats->has_idle_time_ns) {
383             s->stats->idle_time_ns = block_acct_idle_time_ns(stats);
384         }
385 
386         s->stats->account_invalid = stats->account_invalid;
387         s->stats->account_failed = stats->account_failed;
388 
389         while ((ts = block_acct_interval_next(stats, ts))) {
390             BlockDeviceTimedStatsList *timed_stats =
391                 g_malloc0(sizeof(*timed_stats));
392             BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
393             timed_stats->next = s->stats->timed_stats;
394             timed_stats->value = dev_stats;
395             s->stats->timed_stats = timed_stats;
396 
397             TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
398             TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
399             TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
400 
401             dev_stats->interval_length = ts->interval_length;
402 
403             dev_stats->min_rd_latency_ns = timed_average_min(rd);
404             dev_stats->max_rd_latency_ns = timed_average_max(rd);
405             dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
406 
407             dev_stats->min_wr_latency_ns = timed_average_min(wr);
408             dev_stats->max_wr_latency_ns = timed_average_max(wr);
409             dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
410 
411             dev_stats->min_flush_latency_ns = timed_average_min(fl);
412             dev_stats->max_flush_latency_ns = timed_average_max(fl);
413             dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
414 
415             dev_stats->avg_rd_queue_depth =
416                 block_acct_queue_depth(ts, BLOCK_ACCT_READ);
417             dev_stats->avg_wr_queue_depth =
418                 block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
419         }
420     }
421 
422     s->stats->wr_highest_offset = bs->wr_highest_offset;
423 
424     if (bs->file) {
425         s->has_parent = true;
426         s->parent = bdrv_query_stats(bs->file->bs, query_backing);
427     }
428 
429     if (query_backing && bs->backing) {
430         s->has_backing = true;
431         s->backing = bdrv_query_stats(bs->backing->bs, query_backing);
432     }
433 
434     return s;
435 }
436 
437 BlockInfoList *qmp_query_block(Error **errp)
438 {
439     BlockInfoList *head = NULL, **p_next = &head;
440     BlockBackend *blk;
441     Error *local_err = NULL;
442 
443     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
444         BlockInfoList *info = g_malloc0(sizeof(*info));
445         bdrv_query_info(blk, &info->value, &local_err);
446         if (local_err) {
447             error_propagate(errp, local_err);
448             g_free(info);
449             qapi_free_BlockInfoList(head);
450             return NULL;
451         }
452 
453         *p_next = info;
454         p_next = &info->next;
455     }
456 
457     return head;
458 }
459 
460 BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
461                                      bool query_nodes,
462                                      Error **errp)
463 {
464     BlockStatsList *head = NULL, **p_next = &head;
465     BlockDriverState *bs = NULL;
466 
467     /* Just to be safe if query_nodes is not always initialized */
468     query_nodes = has_query_nodes && query_nodes;
469 
470     while ((bs = query_nodes ? bdrv_next_node(bs) : bdrv_next(bs))) {
471         BlockStatsList *info = g_malloc0(sizeof(*info));
472         AioContext *ctx = bdrv_get_aio_context(bs);
473 
474         aio_context_acquire(ctx);
475         info->value = bdrv_query_stats(bs, !query_nodes);
476         aio_context_release(ctx);
477 
478         *p_next = info;
479         p_next = &info->next;
480     }
481 
482     return head;
483 }
484 
485 #define NB_SUFFIXES 4
486 
487 static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
488 {
489     static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'};
490     int64_t base;
491     int i;
492 
493     if (size <= 999) {
494         snprintf(buf, buf_size, "%" PRId64, size);
495     } else {
496         base = 1024;
497         for (i = 0; i < NB_SUFFIXES; i++) {
498             if (size < (10 * base)) {
499                 snprintf(buf, buf_size, "%0.1f%c",
500                          (double)size / base,
501                          suffixes[i]);
502                 break;
503             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
504                 snprintf(buf, buf_size, "%" PRId64 "%c",
505                          ((size + (base >> 1)) / base),
506                          suffixes[i]);
507                 break;
508             }
509             base = base * 1024;
510         }
511     }
512     return buf;
513 }
514 
515 void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
516                         QEMUSnapshotInfo *sn)
517 {
518     char buf1[128], date_buf[128], clock_buf[128];
519     struct tm tm;
520     time_t ti;
521     int64_t secs;
522 
523     if (!sn) {
524         func_fprintf(f,
525                      "%-10s%-20s%7s%20s%15s",
526                      "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
527     } else {
528         ti = sn->date_sec;
529         localtime_r(&ti, &tm);
530         strftime(date_buf, sizeof(date_buf),
531                  "%Y-%m-%d %H:%M:%S", &tm);
532         secs = sn->vm_clock_nsec / 1000000000;
533         snprintf(clock_buf, sizeof(clock_buf),
534                  "%02d:%02d:%02d.%03d",
535                  (int)(secs / 3600),
536                  (int)((secs / 60) % 60),
537                  (int)(secs % 60),
538                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
539         func_fprintf(f,
540                      "%-10s%-20s%7s%20s%15s",
541                      sn->id_str, sn->name,
542                      get_human_readable_size(buf1, sizeof(buf1),
543                                              sn->vm_state_size),
544                      date_buf,
545                      clock_buf);
546     }
547 }
548 
549 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
550                        QDict *dict);
551 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
552                        QList *list);
553 
554 static void dump_qobject(fprintf_function func_fprintf, void *f,
555                          int comp_indent, QObject *obj)
556 {
557     switch (qobject_type(obj)) {
558         case QTYPE_QINT: {
559             QInt *value = qobject_to_qint(obj);
560             func_fprintf(f, "%" PRId64, qint_get_int(value));
561             break;
562         }
563         case QTYPE_QSTRING: {
564             QString *value = qobject_to_qstring(obj);
565             func_fprintf(f, "%s", qstring_get_str(value));
566             break;
567         }
568         case QTYPE_QDICT: {
569             QDict *value = qobject_to_qdict(obj);
570             dump_qdict(func_fprintf, f, comp_indent, value);
571             break;
572         }
573         case QTYPE_QLIST: {
574             QList *value = qobject_to_qlist(obj);
575             dump_qlist(func_fprintf, f, comp_indent, value);
576             break;
577         }
578         case QTYPE_QFLOAT: {
579             QFloat *value = qobject_to_qfloat(obj);
580             func_fprintf(f, "%g", qfloat_get_double(value));
581             break;
582         }
583         case QTYPE_QBOOL: {
584             QBool *value = qobject_to_qbool(obj);
585             func_fprintf(f, "%s", qbool_get_bool(value) ? "true" : "false");
586             break;
587         }
588         default:
589             abort();
590     }
591 }
592 
593 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
594                        QList *list)
595 {
596     const QListEntry *entry;
597     int i = 0;
598 
599     for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
600         QType type = qobject_type(entry->value);
601         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
602         const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: ";
603 
604         func_fprintf(f, format, indentation * 4, "", i);
605         dump_qobject(func_fprintf, f, indentation + 1, entry->value);
606         if (!composite) {
607             func_fprintf(f, "\n");
608         }
609     }
610 }
611 
612 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
613                        QDict *dict)
614 {
615     const QDictEntry *entry;
616 
617     for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
618         QType type = qobject_type(entry->value);
619         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
620         const char *format = composite ? "%*s%s:\n" : "%*s%s: ";
621         char key[strlen(entry->key) + 1];
622         int i;
623 
624         /* replace dashes with spaces in key (variable) names */
625         for (i = 0; entry->key[i]; i++) {
626             key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
627         }
628         key[i] = 0;
629 
630         func_fprintf(f, format, indentation * 4, "", key);
631         dump_qobject(func_fprintf, f, indentation + 1, entry->value);
632         if (!composite) {
633             func_fprintf(f, "\n");
634         }
635     }
636 }
637 
638 void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
639                                    ImageInfoSpecific *info_spec)
640 {
641     QmpOutputVisitor *ov = qmp_output_visitor_new();
642     QObject *obj, *data;
643 
644     visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov), NULL, &info_spec,
645                                  &error_abort);
646     obj = qmp_output_get_qobject(ov);
647     assert(qobject_type(obj) == QTYPE_QDICT);
648     data = qdict_get(qobject_to_qdict(obj), "data");
649     dump_qobject(func_fprintf, f, 1, data);
650     qmp_output_visitor_cleanup(ov);
651 }
652 
653 void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
654                           ImageInfo *info)
655 {
656     char size_buf[128], dsize_buf[128];
657     if (!info->has_actual_size) {
658         snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
659     } else {
660         get_human_readable_size(dsize_buf, sizeof(dsize_buf),
661                                 info->actual_size);
662     }
663     get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
664     func_fprintf(f,
665                  "image: %s\n"
666                  "file format: %s\n"
667                  "virtual size: %s (%" PRId64 " bytes)\n"
668                  "disk size: %s\n",
669                  info->filename, info->format, size_buf,
670                  info->virtual_size,
671                  dsize_buf);
672 
673     if (info->has_encrypted && info->encrypted) {
674         func_fprintf(f, "encrypted: yes\n");
675     }
676 
677     if (info->has_cluster_size) {
678         func_fprintf(f, "cluster_size: %" PRId64 "\n",
679                        info->cluster_size);
680     }
681 
682     if (info->has_dirty_flag && info->dirty_flag) {
683         func_fprintf(f, "cleanly shut down: no\n");
684     }
685 
686     if (info->has_backing_filename) {
687         func_fprintf(f, "backing file: %s", info->backing_filename);
688         if (!info->has_full_backing_filename) {
689             func_fprintf(f, " (cannot determine actual path)");
690         } else if (strcmp(info->backing_filename,
691                           info->full_backing_filename) != 0) {
692             func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
693         }
694         func_fprintf(f, "\n");
695         if (info->has_backing_filename_format) {
696             func_fprintf(f, "backing file format: %s\n",
697                          info->backing_filename_format);
698         }
699     }
700 
701     if (info->has_snapshots) {
702         SnapshotInfoList *elem;
703 
704         func_fprintf(f, "Snapshot list:\n");
705         bdrv_snapshot_dump(func_fprintf, f, NULL);
706         func_fprintf(f, "\n");
707 
708         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
709          * we convert to the block layer's native QEMUSnapshotInfo for now.
710          */
711         for (elem = info->snapshots; elem; elem = elem->next) {
712             QEMUSnapshotInfo sn = {
713                 .vm_state_size = elem->value->vm_state_size,
714                 .date_sec = elem->value->date_sec,
715                 .date_nsec = elem->value->date_nsec,
716                 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
717                                  elem->value->vm_clock_nsec,
718             };
719 
720             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
721             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
722             bdrv_snapshot_dump(func_fprintf, f, &sn);
723             func_fprintf(f, "\n");
724         }
725     }
726 
727     if (info->has_format_specific) {
728         func_fprintf(f, "Format specific information:\n");
729         bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);
730     }
731 }
732