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