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