xref: /qemu/block/qapi.c (revision 6f061ea1)
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(BlockBackend *blk,
359                                     const BlockDriverState *bs,
360                                     bool query_backing);
361 
362 static void bdrv_query_blk_stats(BlockStats *s, BlockBackend *blk)
363 {
364     BlockAcctStats *stats = blk_get_stats(blk);
365     BlockAcctTimedStats *ts = NULL;
366 
367     s->has_device = true;
368     s->device = g_strdup(blk_name(blk));
369 
370     s->stats->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
371     s->stats->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
372     s->stats->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
373     s->stats->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
374 
375     s->stats->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
376     s->stats->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
377     s->stats->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
378 
379     s->stats->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
380     s->stats->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
381     s->stats->invalid_flush_operations =
382         stats->invalid_ops[BLOCK_ACCT_FLUSH];
383 
384     s->stats->rd_merged = stats->merged[BLOCK_ACCT_READ];
385     s->stats->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
386     s->stats->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
387     s->stats->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
388     s->stats->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
389     s->stats->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
390 
391     s->stats->has_idle_time_ns = stats->last_access_time_ns > 0;
392     if (s->stats->has_idle_time_ns) {
393         s->stats->idle_time_ns = block_acct_idle_time_ns(stats);
394     }
395 
396     s->stats->account_invalid = stats->account_invalid;
397     s->stats->account_failed = stats->account_failed;
398 
399     while ((ts = block_acct_interval_next(stats, ts))) {
400         BlockDeviceTimedStatsList *timed_stats =
401             g_malloc0(sizeof(*timed_stats));
402         BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
403         timed_stats->next = s->stats->timed_stats;
404         timed_stats->value = dev_stats;
405         s->stats->timed_stats = timed_stats;
406 
407         TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
408         TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
409         TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
410 
411         dev_stats->interval_length = ts->interval_length;
412 
413         dev_stats->min_rd_latency_ns = timed_average_min(rd);
414         dev_stats->max_rd_latency_ns = timed_average_max(rd);
415         dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
416 
417         dev_stats->min_wr_latency_ns = timed_average_min(wr);
418         dev_stats->max_wr_latency_ns = timed_average_max(wr);
419         dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
420 
421         dev_stats->min_flush_latency_ns = timed_average_min(fl);
422         dev_stats->max_flush_latency_ns = timed_average_max(fl);
423         dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
424 
425         dev_stats->avg_rd_queue_depth =
426             block_acct_queue_depth(ts, BLOCK_ACCT_READ);
427         dev_stats->avg_wr_queue_depth =
428             block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
429     }
430 }
431 
432 static void bdrv_query_bds_stats(BlockStats *s, const BlockDriverState *bs,
433                                  bool query_backing)
434 {
435     if (bdrv_get_node_name(bs)[0]) {
436         s->has_node_name = true;
437         s->node_name = g_strdup(bdrv_get_node_name(bs));
438     }
439 
440     s->stats->wr_highest_offset = bs->wr_highest_offset;
441 
442     if (bs->file) {
443         s->has_parent = true;
444         s->parent = bdrv_query_stats(NULL, bs->file->bs, query_backing);
445     }
446 
447     if (query_backing && bs->backing) {
448         s->has_backing = true;
449         s->backing = bdrv_query_stats(NULL, bs->backing->bs, query_backing);
450     }
451 
452 }
453 
454 static BlockStats *bdrv_query_stats(BlockBackend *blk,
455                                     const BlockDriverState *bs,
456                                     bool query_backing)
457 {
458     BlockStats *s;
459 
460     s = g_malloc0(sizeof(*s));
461     s->stats = g_malloc0(sizeof(*s->stats));
462 
463     if (blk) {
464         bdrv_query_blk_stats(s, blk);
465     }
466     if (bs) {
467         bdrv_query_bds_stats(s, bs, query_backing);
468     }
469 
470     return s;
471 }
472 
473 BlockInfoList *qmp_query_block(Error **errp)
474 {
475     BlockInfoList *head = NULL, **p_next = &head;
476     BlockBackend *blk;
477     Error *local_err = NULL;
478 
479     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
480         BlockInfoList *info = g_malloc0(sizeof(*info));
481         bdrv_query_info(blk, &info->value, &local_err);
482         if (local_err) {
483             error_propagate(errp, local_err);
484             g_free(info);
485             qapi_free_BlockInfoList(head);
486             return NULL;
487         }
488 
489         *p_next = info;
490         p_next = &info->next;
491     }
492 
493     return head;
494 }
495 
496 static bool next_query_bds(BlockBackend **blk, BlockDriverState **bs,
497                            bool query_nodes)
498 {
499     if (query_nodes) {
500         *bs = bdrv_next_node(*bs);
501         return !!*bs;
502     }
503 
504     *blk = blk_next(*blk);
505     *bs = *blk ? blk_bs(*blk) : NULL;
506 
507     return !!*blk;
508 }
509 
510 BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
511                                      bool query_nodes,
512                                      Error **errp)
513 {
514     BlockStatsList *head = NULL, **p_next = &head;
515     BlockBackend *blk = NULL;
516     BlockDriverState *bs = NULL;
517 
518     /* Just to be safe if query_nodes is not always initialized */
519     query_nodes = has_query_nodes && query_nodes;
520 
521     while (next_query_bds(&blk, &bs, query_nodes)) {
522         BlockStatsList *info = g_malloc0(sizeof(*info));
523         AioContext *ctx = blk ? blk_get_aio_context(blk)
524                               : bdrv_get_aio_context(bs);
525 
526         aio_context_acquire(ctx);
527         info->value = bdrv_query_stats(blk, bs, !query_nodes);
528         aio_context_release(ctx);
529 
530         *p_next = info;
531         p_next = &info->next;
532     }
533 
534     return head;
535 }
536 
537 #define NB_SUFFIXES 4
538 
539 static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
540 {
541     static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'};
542     int64_t base;
543     int i;
544 
545     if (size <= 999) {
546         snprintf(buf, buf_size, "%" PRId64, size);
547     } else {
548         base = 1024;
549         for (i = 0; i < NB_SUFFIXES; i++) {
550             if (size < (10 * base)) {
551                 snprintf(buf, buf_size, "%0.1f%c",
552                          (double)size / base,
553                          suffixes[i]);
554                 break;
555             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
556                 snprintf(buf, buf_size, "%" PRId64 "%c",
557                          ((size + (base >> 1)) / base),
558                          suffixes[i]);
559                 break;
560             }
561             base = base * 1024;
562         }
563     }
564     return buf;
565 }
566 
567 void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
568                         QEMUSnapshotInfo *sn)
569 {
570     char buf1[128], date_buf[128], clock_buf[128];
571     struct tm tm;
572     time_t ti;
573     int64_t secs;
574 
575     if (!sn) {
576         func_fprintf(f,
577                      "%-10s%-20s%7s%20s%15s",
578                      "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
579     } else {
580         ti = sn->date_sec;
581         localtime_r(&ti, &tm);
582         strftime(date_buf, sizeof(date_buf),
583                  "%Y-%m-%d %H:%M:%S", &tm);
584         secs = sn->vm_clock_nsec / 1000000000;
585         snprintf(clock_buf, sizeof(clock_buf),
586                  "%02d:%02d:%02d.%03d",
587                  (int)(secs / 3600),
588                  (int)((secs / 60) % 60),
589                  (int)(secs % 60),
590                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
591         func_fprintf(f,
592                      "%-10s%-20s%7s%20s%15s",
593                      sn->id_str, sn->name,
594                      get_human_readable_size(buf1, sizeof(buf1),
595                                              sn->vm_state_size),
596                      date_buf,
597                      clock_buf);
598     }
599 }
600 
601 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
602                        QDict *dict);
603 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
604                        QList *list);
605 
606 static void dump_qobject(fprintf_function func_fprintf, void *f,
607                          int comp_indent, QObject *obj)
608 {
609     switch (qobject_type(obj)) {
610         case QTYPE_QINT: {
611             QInt *value = qobject_to_qint(obj);
612             func_fprintf(f, "%" PRId64, qint_get_int(value));
613             break;
614         }
615         case QTYPE_QSTRING: {
616             QString *value = qobject_to_qstring(obj);
617             func_fprintf(f, "%s", qstring_get_str(value));
618             break;
619         }
620         case QTYPE_QDICT: {
621             QDict *value = qobject_to_qdict(obj);
622             dump_qdict(func_fprintf, f, comp_indent, value);
623             break;
624         }
625         case QTYPE_QLIST: {
626             QList *value = qobject_to_qlist(obj);
627             dump_qlist(func_fprintf, f, comp_indent, value);
628             break;
629         }
630         case QTYPE_QFLOAT: {
631             QFloat *value = qobject_to_qfloat(obj);
632             func_fprintf(f, "%g", qfloat_get_double(value));
633             break;
634         }
635         case QTYPE_QBOOL: {
636             QBool *value = qobject_to_qbool(obj);
637             func_fprintf(f, "%s", qbool_get_bool(value) ? "true" : "false");
638             break;
639         }
640         default:
641             abort();
642     }
643 }
644 
645 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
646                        QList *list)
647 {
648     const QListEntry *entry;
649     int i = 0;
650 
651     for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
652         QType type = qobject_type(entry->value);
653         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
654         const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: ";
655 
656         func_fprintf(f, format, indentation * 4, "", i);
657         dump_qobject(func_fprintf, f, indentation + 1, entry->value);
658         if (!composite) {
659             func_fprintf(f, "\n");
660         }
661     }
662 }
663 
664 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
665                        QDict *dict)
666 {
667     const QDictEntry *entry;
668 
669     for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
670         QType type = qobject_type(entry->value);
671         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
672         const char *format = composite ? "%*s%s:\n" : "%*s%s: ";
673         char key[strlen(entry->key) + 1];
674         int i;
675 
676         /* replace dashes with spaces in key (variable) names */
677         for (i = 0; entry->key[i]; i++) {
678             key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
679         }
680         key[i] = 0;
681 
682         func_fprintf(f, format, indentation * 4, "", key);
683         dump_qobject(func_fprintf, f, indentation + 1, entry->value);
684         if (!composite) {
685             func_fprintf(f, "\n");
686         }
687     }
688 }
689 
690 void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
691                                    ImageInfoSpecific *info_spec)
692 {
693     QmpOutputVisitor *ov = qmp_output_visitor_new();
694     QObject *obj, *data;
695 
696     visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov), NULL, &info_spec,
697                                  &error_abort);
698     obj = qmp_output_get_qobject(ov);
699     assert(qobject_type(obj) == QTYPE_QDICT);
700     data = qdict_get(qobject_to_qdict(obj), "data");
701     dump_qobject(func_fprintf, f, 1, data);
702     qmp_output_visitor_cleanup(ov);
703 }
704 
705 void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
706                           ImageInfo *info)
707 {
708     char size_buf[128], dsize_buf[128];
709     if (!info->has_actual_size) {
710         snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
711     } else {
712         get_human_readable_size(dsize_buf, sizeof(dsize_buf),
713                                 info->actual_size);
714     }
715     get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
716     func_fprintf(f,
717                  "image: %s\n"
718                  "file format: %s\n"
719                  "virtual size: %s (%" PRId64 " bytes)\n"
720                  "disk size: %s\n",
721                  info->filename, info->format, size_buf,
722                  info->virtual_size,
723                  dsize_buf);
724 
725     if (info->has_encrypted && info->encrypted) {
726         func_fprintf(f, "encrypted: yes\n");
727     }
728 
729     if (info->has_cluster_size) {
730         func_fprintf(f, "cluster_size: %" PRId64 "\n",
731                        info->cluster_size);
732     }
733 
734     if (info->has_dirty_flag && info->dirty_flag) {
735         func_fprintf(f, "cleanly shut down: no\n");
736     }
737 
738     if (info->has_backing_filename) {
739         func_fprintf(f, "backing file: %s", info->backing_filename);
740         if (!info->has_full_backing_filename) {
741             func_fprintf(f, " (cannot determine actual path)");
742         } else if (strcmp(info->backing_filename,
743                           info->full_backing_filename) != 0) {
744             func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
745         }
746         func_fprintf(f, "\n");
747         if (info->has_backing_filename_format) {
748             func_fprintf(f, "backing file format: %s\n",
749                          info->backing_filename_format);
750         }
751     }
752 
753     if (info->has_snapshots) {
754         SnapshotInfoList *elem;
755 
756         func_fprintf(f, "Snapshot list:\n");
757         bdrv_snapshot_dump(func_fprintf, f, NULL);
758         func_fprintf(f, "\n");
759 
760         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
761          * we convert to the block layer's native QEMUSnapshotInfo for now.
762          */
763         for (elem = info->snapshots; elem; elem = elem->next) {
764             QEMUSnapshotInfo sn = {
765                 .vm_state_size = elem->value->vm_state_size,
766                 .date_sec = elem->value->date_sec,
767                 .date_nsec = elem->value->date_nsec,
768                 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
769                                  elem->value->vm_clock_nsec,
770             };
771 
772             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
773             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
774             bdrv_snapshot_dump(func_fprintf, f, &sn);
775             func_fprintf(f, "\n");
776         }
777     }
778 
779     if (info->has_format_specific) {
780         func_fprintf(f, "Format specific information:\n");
781         bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);
782     }
783 }
784