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