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