xref: /qemu/block/qapi.c (revision c8059b97)
1f364ec65SWenchao Xia /*
2f364ec65SWenchao Xia  * Block layer qmp and info dump related functions
3f364ec65SWenchao Xia  *
4f364ec65SWenchao Xia  * Copyright (c) 2003-2008 Fabrice Bellard
5f364ec65SWenchao Xia  *
6f364ec65SWenchao Xia  * Permission is hereby granted, free of charge, to any person obtaining a copy
7f364ec65SWenchao Xia  * of this software and associated documentation files (the "Software"), to deal
8f364ec65SWenchao Xia  * in the Software without restriction, including without limitation the rights
9f364ec65SWenchao Xia  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10f364ec65SWenchao Xia  * copies of the Software, and to permit persons to whom the Software is
11f364ec65SWenchao Xia  * furnished to do so, subject to the following conditions:
12f364ec65SWenchao Xia  *
13f364ec65SWenchao Xia  * The above copyright notice and this permission notice shall be included in
14f364ec65SWenchao Xia  * all copies or substantial portions of the Software.
15f364ec65SWenchao Xia  *
16f364ec65SWenchao Xia  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17f364ec65SWenchao Xia  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18f364ec65SWenchao Xia  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19f364ec65SWenchao Xia  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20f364ec65SWenchao Xia  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21f364ec65SWenchao Xia  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22f364ec65SWenchao Xia  * THE SOFTWARE.
23f364ec65SWenchao Xia  */
24f364ec65SWenchao Xia 
25f364ec65SWenchao Xia #include "block/qapi.h"
26f364ec65SWenchao Xia #include "block/block_int.h"
27f364ec65SWenchao Xia #include "qmp-commands.h"
28a8d8ecb7SMax Reitz #include "qapi-visit.h"
29a8d8ecb7SMax Reitz #include "qapi/qmp-output-visitor.h"
30a8d8ecb7SMax Reitz #include "qapi/qmp/types.h"
31f364ec65SWenchao Xia 
32fb0ed453SWenchao Xia /*
33fb0ed453SWenchao Xia  * Returns 0 on success, with *p_list either set to describe snapshot
34fb0ed453SWenchao Xia  * information, or NULL because there are no snapshots.  Returns -errno on
35fb0ed453SWenchao Xia  * error, with *p_list untouched.
36fb0ed453SWenchao Xia  */
37fb0ed453SWenchao Xia int bdrv_query_snapshot_info_list(BlockDriverState *bs,
38fb0ed453SWenchao Xia                                   SnapshotInfoList **p_list,
39fb0ed453SWenchao Xia                                   Error **errp)
40f364ec65SWenchao Xia {
41f364ec65SWenchao Xia     int i, sn_count;
42f364ec65SWenchao Xia     QEMUSnapshotInfo *sn_tab = NULL;
43fb0ed453SWenchao Xia     SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
44fb0ed453SWenchao Xia     SnapshotInfo *info;
45fb0ed453SWenchao Xia 
46f364ec65SWenchao Xia     sn_count = bdrv_snapshot_list(bs, &sn_tab);
47fb0ed453SWenchao Xia     if (sn_count < 0) {
48fb0ed453SWenchao Xia         const char *dev = bdrv_get_device_name(bs);
49fb0ed453SWenchao Xia         switch (sn_count) {
50fb0ed453SWenchao Xia         case -ENOMEDIUM:
51fb0ed453SWenchao Xia             error_setg(errp, "Device '%s' is not inserted", dev);
52fb0ed453SWenchao Xia             break;
53fb0ed453SWenchao Xia         case -ENOTSUP:
54fb0ed453SWenchao Xia             error_setg(errp,
55fb0ed453SWenchao Xia                        "Device '%s' does not support internal snapshots",
56fb0ed453SWenchao Xia                        dev);
57fb0ed453SWenchao Xia             break;
58fb0ed453SWenchao Xia         default:
59fb0ed453SWenchao Xia             error_setg_errno(errp, -sn_count,
60fb0ed453SWenchao Xia                              "Can't list snapshots of device '%s'", dev);
61fb0ed453SWenchao Xia             break;
62fb0ed453SWenchao Xia         }
63fb0ed453SWenchao Xia         return sn_count;
64fb0ed453SWenchao Xia     }
65f364ec65SWenchao Xia 
66f364ec65SWenchao Xia     for (i = 0; i < sn_count; i++) {
67fb0ed453SWenchao Xia         info = g_new0(SnapshotInfo, 1);
68fb0ed453SWenchao Xia         info->id            = g_strdup(sn_tab[i].id_str);
69fb0ed453SWenchao Xia         info->name          = g_strdup(sn_tab[i].name);
70fb0ed453SWenchao Xia         info->vm_state_size = sn_tab[i].vm_state_size;
71fb0ed453SWenchao Xia         info->date_sec      = sn_tab[i].date_sec;
72fb0ed453SWenchao Xia         info->date_nsec     = sn_tab[i].date_nsec;
73fb0ed453SWenchao Xia         info->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
74fb0ed453SWenchao Xia         info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
75f364ec65SWenchao Xia 
76fb0ed453SWenchao Xia         info_list = g_new0(SnapshotInfoList, 1);
77fb0ed453SWenchao Xia         info_list->value = info;
78f364ec65SWenchao Xia 
79f364ec65SWenchao Xia         /* XXX: waiting for the qapi to support qemu-queue.h types */
80f364ec65SWenchao Xia         if (!cur_item) {
81fb0ed453SWenchao Xia             head = cur_item = info_list;
82f364ec65SWenchao Xia         } else {
83f364ec65SWenchao Xia             cur_item->next = info_list;
84f364ec65SWenchao Xia             cur_item = info_list;
85f364ec65SWenchao Xia         }
86f364ec65SWenchao Xia 
87f364ec65SWenchao Xia     }
88f364ec65SWenchao Xia 
89f364ec65SWenchao Xia     g_free(sn_tab);
90fb0ed453SWenchao Xia     *p_list = head;
91fb0ed453SWenchao Xia     return 0;
92f364ec65SWenchao Xia }
93f364ec65SWenchao Xia 
9443526ec8SWenchao Xia /**
9543526ec8SWenchao Xia  * bdrv_query_image_info:
9643526ec8SWenchao Xia  * @bs: block device to examine
9743526ec8SWenchao Xia  * @p_info: location to store image information
9843526ec8SWenchao Xia  * @errp: location to store error information
9943526ec8SWenchao Xia  *
100553a7e87SWenchao Xia  * Store "flat" image information in @p_info.
101553a7e87SWenchao Xia  *
102553a7e87SWenchao Xia  * "Flat" means it does *not* query backing image information,
103553a7e87SWenchao Xia  * i.e. (*pinfo)->has_backing_image will be set to false and
104553a7e87SWenchao Xia  * (*pinfo)->backing_image to NULL even when the image does in fact have
105553a7e87SWenchao Xia  * a backing image.
106553a7e87SWenchao Xia  *
10743526ec8SWenchao Xia  * @p_info will be set only on success. On error, store error in @errp.
10843526ec8SWenchao Xia  */
10943526ec8SWenchao Xia void bdrv_query_image_info(BlockDriverState *bs,
11043526ec8SWenchao Xia                            ImageInfo **p_info,
11143526ec8SWenchao Xia                            Error **errp)
112f364ec65SWenchao Xia {
113f364ec65SWenchao Xia     uint64_t total_sectors;
11443526ec8SWenchao Xia     const char *backing_filename;
115f364ec65SWenchao Xia     char backing_filename2[1024];
116f364ec65SWenchao Xia     BlockDriverInfo bdi;
11743526ec8SWenchao Xia     int ret;
11843526ec8SWenchao Xia     Error *err = NULL;
11943526ec8SWenchao Xia     ImageInfo *info = g_new0(ImageInfo, 1);
120f364ec65SWenchao Xia 
121f364ec65SWenchao Xia     bdrv_get_geometry(bs, &total_sectors);
122f364ec65SWenchao Xia 
12343526ec8SWenchao Xia     info->filename        = g_strdup(bs->filename);
124f364ec65SWenchao Xia     info->format          = g_strdup(bdrv_get_format_name(bs));
125f364ec65SWenchao Xia     info->virtual_size    = total_sectors * 512;
126f364ec65SWenchao Xia     info->actual_size     = bdrv_get_allocated_file_size(bs);
127f364ec65SWenchao Xia     info->has_actual_size = info->actual_size >= 0;
128f364ec65SWenchao Xia     if (bdrv_is_encrypted(bs)) {
129f364ec65SWenchao Xia         info->encrypted = true;
130f364ec65SWenchao Xia         info->has_encrypted = true;
131f364ec65SWenchao Xia     }
132f364ec65SWenchao Xia     if (bdrv_get_info(bs, &bdi) >= 0) {
133f364ec65SWenchao Xia         if (bdi.cluster_size != 0) {
134f364ec65SWenchao Xia             info->cluster_size = bdi.cluster_size;
135f364ec65SWenchao Xia             info->has_cluster_size = true;
136f364ec65SWenchao Xia         }
137f364ec65SWenchao Xia         info->dirty_flag = bdi.is_dirty;
138f364ec65SWenchao Xia         info->has_dirty_flag = true;
139f364ec65SWenchao Xia     }
140eae041feSMax Reitz     info->format_specific     = bdrv_get_specific_info(bs);
141eae041feSMax Reitz     info->has_format_specific = info->format_specific != NULL;
142eae041feSMax Reitz 
14343526ec8SWenchao Xia     backing_filename = bs->backing_file;
144f364ec65SWenchao Xia     if (backing_filename[0] != '\0') {
145f364ec65SWenchao Xia         info->backing_filename = g_strdup(backing_filename);
146f364ec65SWenchao Xia         info->has_backing_filename = true;
147f364ec65SWenchao Xia         bdrv_get_full_backing_filename(bs, backing_filename2,
148f364ec65SWenchao Xia                                        sizeof(backing_filename2));
149f364ec65SWenchao Xia 
150f364ec65SWenchao Xia         if (strcmp(backing_filename, backing_filename2) != 0) {
151f364ec65SWenchao Xia             info->full_backing_filename =
152f364ec65SWenchao Xia                         g_strdup(backing_filename2);
153f364ec65SWenchao Xia             info->has_full_backing_filename = true;
154f364ec65SWenchao Xia         }
155f364ec65SWenchao Xia 
156f364ec65SWenchao Xia         if (bs->backing_format[0]) {
157f364ec65SWenchao Xia             info->backing_filename_format = g_strdup(bs->backing_format);
158f364ec65SWenchao Xia             info->has_backing_filename_format = true;
159f364ec65SWenchao Xia         }
160f364ec65SWenchao Xia     }
16143526ec8SWenchao Xia 
16243526ec8SWenchao Xia     ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
16343526ec8SWenchao Xia     switch (ret) {
16443526ec8SWenchao Xia     case 0:
16543526ec8SWenchao Xia         if (info->snapshots) {
16643526ec8SWenchao Xia             info->has_snapshots = true;
16743526ec8SWenchao Xia         }
16843526ec8SWenchao Xia         break;
16943526ec8SWenchao Xia     /* recoverable error */
17043526ec8SWenchao Xia     case -ENOMEDIUM:
17143526ec8SWenchao Xia     case -ENOTSUP:
17243526ec8SWenchao Xia         error_free(err);
17343526ec8SWenchao Xia         break;
17443526ec8SWenchao Xia     default:
17543526ec8SWenchao Xia         error_propagate(errp, err);
17643526ec8SWenchao Xia         qapi_free_ImageInfo(info);
17743526ec8SWenchao Xia         return;
17843526ec8SWenchao Xia     }
17943526ec8SWenchao Xia 
18043526ec8SWenchao Xia     *p_info = info;
181f364ec65SWenchao Xia }
182f364ec65SWenchao Xia 
183553a7e87SWenchao Xia /* @p_info will be set only on success. */
184553a7e87SWenchao Xia void bdrv_query_info(BlockDriverState *bs,
185553a7e87SWenchao Xia                      BlockInfo **p_info,
186553a7e87SWenchao Xia                      Error **errp)
187f364ec65SWenchao Xia {
188f364ec65SWenchao Xia     BlockInfo *info = g_malloc0(sizeof(*info));
189553a7e87SWenchao Xia     BlockDriverState *bs0;
190553a7e87SWenchao Xia     ImageInfo **p_image_info;
191553a7e87SWenchao Xia     Error *local_err = NULL;
192f364ec65SWenchao Xia     info->device = g_strdup(bs->device_name);
193f364ec65SWenchao Xia     info->type = g_strdup("unknown");
194f364ec65SWenchao Xia     info->locked = bdrv_dev_is_medium_locked(bs);
195f364ec65SWenchao Xia     info->removable = bdrv_dev_has_removable_media(bs);
196f364ec65SWenchao Xia 
197f364ec65SWenchao Xia     if (bdrv_dev_has_removable_media(bs)) {
198f364ec65SWenchao Xia         info->has_tray_open = true;
199f364ec65SWenchao Xia         info->tray_open = bdrv_dev_is_tray_open(bs);
200f364ec65SWenchao Xia     }
201f364ec65SWenchao Xia 
202f364ec65SWenchao Xia     if (bdrv_iostatus_is_enabled(bs)) {
203f364ec65SWenchao Xia         info->has_io_status = true;
204f364ec65SWenchao Xia         info->io_status = bs->iostatus;
205f364ec65SWenchao Xia     }
206f364ec65SWenchao Xia 
20721b56835SFam Zheng     if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
20821b56835SFam Zheng         info->has_dirty_bitmaps = true;
20921b56835SFam Zheng         info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
21021b56835SFam Zheng     }
21121b56835SFam Zheng 
212f364ec65SWenchao Xia     if (bs->drv) {
213f364ec65SWenchao Xia         info->has_inserted = true;
214f364ec65SWenchao Xia         info->inserted = g_malloc0(sizeof(*info->inserted));
215f364ec65SWenchao Xia         info->inserted->file = g_strdup(bs->filename);
216f364ec65SWenchao Xia         info->inserted->ro = bs->read_only;
217f364ec65SWenchao Xia         info->inserted->drv = g_strdup(bs->drv->format_name);
218f364ec65SWenchao Xia         info->inserted->encrypted = bs->encrypted;
219f364ec65SWenchao Xia         info->inserted->encryption_key_missing = bdrv_key_required(bs);
220f364ec65SWenchao Xia 
221f364ec65SWenchao Xia         if (bs->backing_file[0]) {
222f364ec65SWenchao Xia             info->inserted->has_backing_file = true;
223f364ec65SWenchao Xia             info->inserted->backing_file = g_strdup(bs->backing_file);
224f364ec65SWenchao Xia         }
225f364ec65SWenchao Xia 
226f364ec65SWenchao Xia         info->inserted->backing_file_depth = bdrv_get_backing_file_depth(bs);
227f364ec65SWenchao Xia 
228f364ec65SWenchao Xia         if (bs->io_limits_enabled) {
229cc0681c4SBenoît Canet             ThrottleConfig cfg;
230cc0681c4SBenoît Canet             throttle_get_config(&bs->throttle_state, &cfg);
231cc0681c4SBenoît Canet             info->inserted->bps     = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
232cc0681c4SBenoît Canet             info->inserted->bps_rd  = cfg.buckets[THROTTLE_BPS_READ].avg;
233cc0681c4SBenoît Canet             info->inserted->bps_wr  = cfg.buckets[THROTTLE_BPS_WRITE].avg;
234cc0681c4SBenoît Canet 
235cc0681c4SBenoît Canet             info->inserted->iops    = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
236cc0681c4SBenoît Canet             info->inserted->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
237cc0681c4SBenoît Canet             info->inserted->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
2383e9fab69SBenoît Canet 
2393e9fab69SBenoît Canet             info->inserted->has_bps_max     =
2403e9fab69SBenoît Canet                 cfg.buckets[THROTTLE_BPS_TOTAL].max;
2413e9fab69SBenoît Canet             info->inserted->bps_max         =
2423e9fab69SBenoît Canet                 cfg.buckets[THROTTLE_BPS_TOTAL].max;
2433e9fab69SBenoît Canet             info->inserted->has_bps_rd_max  =
2443e9fab69SBenoît Canet                 cfg.buckets[THROTTLE_BPS_READ].max;
2453e9fab69SBenoît Canet             info->inserted->bps_rd_max      =
2463e9fab69SBenoît Canet                 cfg.buckets[THROTTLE_BPS_READ].max;
2473e9fab69SBenoît Canet             info->inserted->has_bps_wr_max  =
2483e9fab69SBenoît Canet                 cfg.buckets[THROTTLE_BPS_WRITE].max;
2493e9fab69SBenoît Canet             info->inserted->bps_wr_max      =
2503e9fab69SBenoît Canet                 cfg.buckets[THROTTLE_BPS_WRITE].max;
2513e9fab69SBenoît Canet 
2523e9fab69SBenoît Canet             info->inserted->has_iops_max    =
2533e9fab69SBenoît Canet                 cfg.buckets[THROTTLE_OPS_TOTAL].max;
2543e9fab69SBenoît Canet             info->inserted->iops_max        =
2553e9fab69SBenoît Canet                 cfg.buckets[THROTTLE_OPS_TOTAL].max;
2563e9fab69SBenoît Canet             info->inserted->has_iops_rd_max =
2573e9fab69SBenoît Canet                 cfg.buckets[THROTTLE_OPS_READ].max;
2583e9fab69SBenoît Canet             info->inserted->iops_rd_max     =
2593e9fab69SBenoît Canet                 cfg.buckets[THROTTLE_OPS_READ].max;
2603e9fab69SBenoît Canet             info->inserted->has_iops_wr_max =
2613e9fab69SBenoît Canet                 cfg.buckets[THROTTLE_OPS_WRITE].max;
2623e9fab69SBenoît Canet             info->inserted->iops_wr_max     =
2633e9fab69SBenoît Canet                 cfg.buckets[THROTTLE_OPS_WRITE].max;
2642024c1dfSBenoît Canet 
2652024c1dfSBenoît Canet             info->inserted->has_iops_size = cfg.op_size;
2662024c1dfSBenoît Canet             info->inserted->iops_size = cfg.op_size;
267f364ec65SWenchao Xia         }
268553a7e87SWenchao Xia 
269553a7e87SWenchao Xia         bs0 = bs;
270553a7e87SWenchao Xia         p_image_info = &info->inserted->image;
271553a7e87SWenchao Xia         while (1) {
272553a7e87SWenchao Xia             bdrv_query_image_info(bs0, p_image_info, &local_err);
273553a7e87SWenchao Xia             if (error_is_set(&local_err)) {
274553a7e87SWenchao Xia                 error_propagate(errp, local_err);
275553a7e87SWenchao Xia                 goto err;
276f364ec65SWenchao Xia             }
277553a7e87SWenchao Xia             if (bs0->drv && bs0->backing_hd) {
278553a7e87SWenchao Xia                 bs0 = bs0->backing_hd;
279553a7e87SWenchao Xia                 (*p_image_info)->has_backing_image = true;
280553a7e87SWenchao Xia                 p_image_info = &((*p_image_info)->backing_image);
281553a7e87SWenchao Xia             } else {
282553a7e87SWenchao Xia                 break;
283553a7e87SWenchao Xia             }
284553a7e87SWenchao Xia         }
285553a7e87SWenchao Xia     }
286553a7e87SWenchao Xia 
287553a7e87SWenchao Xia     *p_info = info;
288553a7e87SWenchao Xia     return;
289553a7e87SWenchao Xia 
290553a7e87SWenchao Xia  err:
291553a7e87SWenchao Xia     qapi_free_BlockInfo(info);
292f364ec65SWenchao Xia }
293f364ec65SWenchao Xia 
294f364ec65SWenchao Xia BlockStats *bdrv_query_stats(const BlockDriverState *bs)
295f364ec65SWenchao Xia {
296f364ec65SWenchao Xia     BlockStats *s;
297f364ec65SWenchao Xia 
298f364ec65SWenchao Xia     s = g_malloc0(sizeof(*s));
299f364ec65SWenchao Xia 
300f364ec65SWenchao Xia     if (bs->device_name[0]) {
301f364ec65SWenchao Xia         s->has_device = true;
302f364ec65SWenchao Xia         s->device = g_strdup(bs->device_name);
303f364ec65SWenchao Xia     }
304f364ec65SWenchao Xia 
305f364ec65SWenchao Xia     s->stats = g_malloc0(sizeof(*s->stats));
306f364ec65SWenchao Xia     s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ];
307f364ec65SWenchao Xia     s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE];
308f364ec65SWenchao Xia     s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ];
309f364ec65SWenchao Xia     s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE];
310f364ec65SWenchao Xia     s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE;
311f364ec65SWenchao Xia     s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH];
312f364ec65SWenchao Xia     s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE];
313f364ec65SWenchao Xia     s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ];
314f364ec65SWenchao Xia     s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH];
315f364ec65SWenchao Xia 
316f364ec65SWenchao Xia     if (bs->file) {
317f364ec65SWenchao Xia         s->has_parent = true;
318f364ec65SWenchao Xia         s->parent = bdrv_query_stats(bs->file);
319f364ec65SWenchao Xia     }
320f364ec65SWenchao Xia 
321*c8059b97SFam Zheng     if (bs->backing_hd) {
322*c8059b97SFam Zheng         s->has_backing = true;
323*c8059b97SFam Zheng         s->backing = bdrv_query_stats(bs->backing_hd);
324*c8059b97SFam Zheng     }
325*c8059b97SFam Zheng 
326f364ec65SWenchao Xia     return s;
327f364ec65SWenchao Xia }
328f364ec65SWenchao Xia 
329f364ec65SWenchao Xia BlockInfoList *qmp_query_block(Error **errp)
330f364ec65SWenchao Xia {
331f364ec65SWenchao Xia     BlockInfoList *head = NULL, **p_next = &head;
332f364ec65SWenchao Xia     BlockDriverState *bs = NULL;
333553a7e87SWenchao Xia     Error *local_err = NULL;
334f364ec65SWenchao Xia 
335f364ec65SWenchao Xia      while ((bs = bdrv_next(bs))) {
336f364ec65SWenchao Xia         BlockInfoList *info = g_malloc0(sizeof(*info));
337553a7e87SWenchao Xia         bdrv_query_info(bs, &info->value, &local_err);
338553a7e87SWenchao Xia         if (error_is_set(&local_err)) {
339553a7e87SWenchao Xia             error_propagate(errp, local_err);
340553a7e87SWenchao Xia             goto err;
341553a7e87SWenchao Xia         }
342f364ec65SWenchao Xia 
343f364ec65SWenchao Xia         *p_next = info;
344f364ec65SWenchao Xia         p_next = &info->next;
345f364ec65SWenchao Xia     }
346f364ec65SWenchao Xia 
347f364ec65SWenchao Xia     return head;
348553a7e87SWenchao Xia 
349553a7e87SWenchao Xia  err:
350553a7e87SWenchao Xia     qapi_free_BlockInfoList(head);
351553a7e87SWenchao Xia     return NULL;
352f364ec65SWenchao Xia }
353f364ec65SWenchao Xia 
354f364ec65SWenchao Xia BlockStatsList *qmp_query_blockstats(Error **errp)
355f364ec65SWenchao Xia {
356f364ec65SWenchao Xia     BlockStatsList *head = NULL, **p_next = &head;
357f364ec65SWenchao Xia     BlockDriverState *bs = NULL;
358f364ec65SWenchao Xia 
359f364ec65SWenchao Xia      while ((bs = bdrv_next(bs))) {
360f364ec65SWenchao Xia         BlockStatsList *info = g_malloc0(sizeof(*info));
361f364ec65SWenchao Xia         info->value = bdrv_query_stats(bs);
362f364ec65SWenchao Xia 
363f364ec65SWenchao Xia         *p_next = info;
364f364ec65SWenchao Xia         p_next = &info->next;
365f364ec65SWenchao Xia     }
366f364ec65SWenchao Xia 
367f364ec65SWenchao Xia     return head;
368f364ec65SWenchao Xia }
369f364ec65SWenchao Xia 
370f364ec65SWenchao Xia #define NB_SUFFIXES 4
371f364ec65SWenchao Xia 
372f364ec65SWenchao Xia static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
373f364ec65SWenchao Xia {
374f364ec65SWenchao Xia     static const char suffixes[NB_SUFFIXES] = "KMGT";
375f364ec65SWenchao Xia     int64_t base;
376f364ec65SWenchao Xia     int i;
377f364ec65SWenchao Xia 
378f364ec65SWenchao Xia     if (size <= 999) {
379f364ec65SWenchao Xia         snprintf(buf, buf_size, "%" PRId64, size);
380f364ec65SWenchao Xia     } else {
381f364ec65SWenchao Xia         base = 1024;
382f364ec65SWenchao Xia         for (i = 0; i < NB_SUFFIXES; i++) {
383f364ec65SWenchao Xia             if (size < (10 * base)) {
384f364ec65SWenchao Xia                 snprintf(buf, buf_size, "%0.1f%c",
385f364ec65SWenchao Xia                          (double)size / base,
386f364ec65SWenchao Xia                          suffixes[i]);
387f364ec65SWenchao Xia                 break;
388f364ec65SWenchao Xia             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
389f364ec65SWenchao Xia                 snprintf(buf, buf_size, "%" PRId64 "%c",
390f364ec65SWenchao Xia                          ((size + (base >> 1)) / base),
391f364ec65SWenchao Xia                          suffixes[i]);
392f364ec65SWenchao Xia                 break;
393f364ec65SWenchao Xia             }
394f364ec65SWenchao Xia             base = base * 1024;
395f364ec65SWenchao Xia         }
396f364ec65SWenchao Xia     }
397f364ec65SWenchao Xia     return buf;
398f364ec65SWenchao Xia }
399f364ec65SWenchao Xia 
4005b917044SWenchao Xia void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
4015b917044SWenchao Xia                         QEMUSnapshotInfo *sn)
402f364ec65SWenchao Xia {
403f364ec65SWenchao Xia     char buf1[128], date_buf[128], clock_buf[128];
404f364ec65SWenchao Xia     struct tm tm;
405f364ec65SWenchao Xia     time_t ti;
406f364ec65SWenchao Xia     int64_t secs;
407f364ec65SWenchao Xia 
408f364ec65SWenchao Xia     if (!sn) {
4095b917044SWenchao Xia         func_fprintf(f,
410f364ec65SWenchao Xia                      "%-10s%-20s%7s%20s%15s",
411f364ec65SWenchao Xia                      "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
412f364ec65SWenchao Xia     } else {
413f364ec65SWenchao Xia         ti = sn->date_sec;
414f364ec65SWenchao Xia         localtime_r(&ti, &tm);
415f364ec65SWenchao Xia         strftime(date_buf, sizeof(date_buf),
416f364ec65SWenchao Xia                  "%Y-%m-%d %H:%M:%S", &tm);
417f364ec65SWenchao Xia         secs = sn->vm_clock_nsec / 1000000000;
418f364ec65SWenchao Xia         snprintf(clock_buf, sizeof(clock_buf),
419f364ec65SWenchao Xia                  "%02d:%02d:%02d.%03d",
420f364ec65SWenchao Xia                  (int)(secs / 3600),
421f364ec65SWenchao Xia                  (int)((secs / 60) % 60),
422f364ec65SWenchao Xia                  (int)(secs % 60),
423f364ec65SWenchao Xia                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
4245b917044SWenchao Xia         func_fprintf(f,
425f364ec65SWenchao Xia                      "%-10s%-20s%7s%20s%15s",
426f364ec65SWenchao Xia                      sn->id_str, sn->name,
4275b917044SWenchao Xia                      get_human_readable_size(buf1, sizeof(buf1),
4285b917044SWenchao Xia                                              sn->vm_state_size),
429f364ec65SWenchao Xia                      date_buf,
430f364ec65SWenchao Xia                      clock_buf);
431f364ec65SWenchao Xia     }
432f364ec65SWenchao Xia }
433f364ec65SWenchao Xia 
434a8d8ecb7SMax Reitz static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
435a8d8ecb7SMax Reitz                        QDict *dict);
436a8d8ecb7SMax Reitz static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
437a8d8ecb7SMax Reitz                        QList *list);
438a8d8ecb7SMax Reitz 
439a8d8ecb7SMax Reitz static void dump_qobject(fprintf_function func_fprintf, void *f,
440a8d8ecb7SMax Reitz                          int comp_indent, QObject *obj)
441a8d8ecb7SMax Reitz {
442a8d8ecb7SMax Reitz     switch (qobject_type(obj)) {
443a8d8ecb7SMax Reitz         case QTYPE_QINT: {
444a8d8ecb7SMax Reitz             QInt *value = qobject_to_qint(obj);
445a8d8ecb7SMax Reitz             func_fprintf(f, "%" PRId64, qint_get_int(value));
446a8d8ecb7SMax Reitz             break;
447a8d8ecb7SMax Reitz         }
448a8d8ecb7SMax Reitz         case QTYPE_QSTRING: {
449a8d8ecb7SMax Reitz             QString *value = qobject_to_qstring(obj);
450a8d8ecb7SMax Reitz             func_fprintf(f, "%s", qstring_get_str(value));
451a8d8ecb7SMax Reitz             break;
452a8d8ecb7SMax Reitz         }
453a8d8ecb7SMax Reitz         case QTYPE_QDICT: {
454a8d8ecb7SMax Reitz             QDict *value = qobject_to_qdict(obj);
455a8d8ecb7SMax Reitz             dump_qdict(func_fprintf, f, comp_indent, value);
456a8d8ecb7SMax Reitz             break;
457a8d8ecb7SMax Reitz         }
458a8d8ecb7SMax Reitz         case QTYPE_QLIST: {
459a8d8ecb7SMax Reitz             QList *value = qobject_to_qlist(obj);
460a8d8ecb7SMax Reitz             dump_qlist(func_fprintf, f, comp_indent, value);
461a8d8ecb7SMax Reitz             break;
462a8d8ecb7SMax Reitz         }
463a8d8ecb7SMax Reitz         case QTYPE_QFLOAT: {
464a8d8ecb7SMax Reitz             QFloat *value = qobject_to_qfloat(obj);
465a8d8ecb7SMax Reitz             func_fprintf(f, "%g", qfloat_get_double(value));
466a8d8ecb7SMax Reitz             break;
467a8d8ecb7SMax Reitz         }
468a8d8ecb7SMax Reitz         case QTYPE_QBOOL: {
469a8d8ecb7SMax Reitz             QBool *value = qobject_to_qbool(obj);
470a8d8ecb7SMax Reitz             func_fprintf(f, "%s", qbool_get_int(value) ? "true" : "false");
471a8d8ecb7SMax Reitz             break;
472a8d8ecb7SMax Reitz         }
473a8d8ecb7SMax Reitz         case QTYPE_QERROR: {
474a8d8ecb7SMax Reitz             QString *value = qerror_human((QError *)obj);
475a8d8ecb7SMax Reitz             func_fprintf(f, "%s", qstring_get_str(value));
476a8d8ecb7SMax Reitz             break;
477a8d8ecb7SMax Reitz         }
478a8d8ecb7SMax Reitz         case QTYPE_NONE:
479a8d8ecb7SMax Reitz             break;
480a8d8ecb7SMax Reitz         case QTYPE_MAX:
481a8d8ecb7SMax Reitz         default:
482a8d8ecb7SMax Reitz             abort();
483a8d8ecb7SMax Reitz     }
484a8d8ecb7SMax Reitz }
485a8d8ecb7SMax Reitz 
486a8d8ecb7SMax Reitz static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
487a8d8ecb7SMax Reitz                        QList *list)
488a8d8ecb7SMax Reitz {
489a8d8ecb7SMax Reitz     const QListEntry *entry;
490a8d8ecb7SMax Reitz     int i = 0;
491a8d8ecb7SMax Reitz 
492a8d8ecb7SMax Reitz     for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
493a8d8ecb7SMax Reitz         qtype_code type = qobject_type(entry->value);
494a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
495a8d8ecb7SMax Reitz         const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: ";
496a8d8ecb7SMax Reitz 
497a8d8ecb7SMax Reitz         func_fprintf(f, format, indentation * 4, "", i);
498a8d8ecb7SMax Reitz         dump_qobject(func_fprintf, f, indentation + 1, entry->value);
499a8d8ecb7SMax Reitz         if (!composite) {
500a8d8ecb7SMax Reitz             func_fprintf(f, "\n");
501a8d8ecb7SMax Reitz         }
502a8d8ecb7SMax Reitz     }
503a8d8ecb7SMax Reitz }
504a8d8ecb7SMax Reitz 
505a8d8ecb7SMax Reitz static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
506a8d8ecb7SMax Reitz                        QDict *dict)
507a8d8ecb7SMax Reitz {
508a8d8ecb7SMax Reitz     const QDictEntry *entry;
509a8d8ecb7SMax Reitz 
510a8d8ecb7SMax Reitz     for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
511a8d8ecb7SMax Reitz         qtype_code type = qobject_type(entry->value);
512a8d8ecb7SMax Reitz         bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
513a8d8ecb7SMax Reitz         const char *format = composite ? "%*s%s:\n" : "%*s%s: ";
514a8d8ecb7SMax Reitz         char key[strlen(entry->key) + 1];
515a8d8ecb7SMax Reitz         int i;
516a8d8ecb7SMax Reitz 
517a8d8ecb7SMax Reitz         /* replace dashes with spaces in key (variable) names */
518a8d8ecb7SMax Reitz         for (i = 0; entry->key[i]; i++) {
519a8d8ecb7SMax Reitz             key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
520a8d8ecb7SMax Reitz         }
521a8d8ecb7SMax Reitz         key[i] = 0;
522a8d8ecb7SMax Reitz 
523a8d8ecb7SMax Reitz         func_fprintf(f, format, indentation * 4, "", key);
524a8d8ecb7SMax Reitz         dump_qobject(func_fprintf, f, indentation + 1, entry->value);
525a8d8ecb7SMax Reitz         if (!composite) {
526a8d8ecb7SMax Reitz             func_fprintf(f, "\n");
527a8d8ecb7SMax Reitz         }
528a8d8ecb7SMax Reitz     }
529a8d8ecb7SMax Reitz }
530a8d8ecb7SMax Reitz 
531a8d8ecb7SMax Reitz void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
532a8d8ecb7SMax Reitz                                    ImageInfoSpecific *info_spec)
533a8d8ecb7SMax Reitz {
534a8d8ecb7SMax Reitz     Error *local_err = NULL;
535a8d8ecb7SMax Reitz     QmpOutputVisitor *ov = qmp_output_visitor_new();
536a8d8ecb7SMax Reitz     QObject *obj, *data;
537a8d8ecb7SMax Reitz 
538a8d8ecb7SMax Reitz     visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov), &info_spec, NULL,
539a8d8ecb7SMax Reitz                                  &local_err);
540a8d8ecb7SMax Reitz     obj = qmp_output_get_qobject(ov);
541a8d8ecb7SMax Reitz     assert(qobject_type(obj) == QTYPE_QDICT);
542a8d8ecb7SMax Reitz     data = qdict_get(qobject_to_qdict(obj), "data");
543a8d8ecb7SMax Reitz     dump_qobject(func_fprintf, f, 1, data);
544a8d8ecb7SMax Reitz     qmp_output_visitor_cleanup(ov);
545a8d8ecb7SMax Reitz }
546a8d8ecb7SMax Reitz 
5475b917044SWenchao Xia void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
5485b917044SWenchao Xia                           ImageInfo *info)
549f364ec65SWenchao Xia {
550f364ec65SWenchao Xia     char size_buf[128], dsize_buf[128];
551f364ec65SWenchao Xia     if (!info->has_actual_size) {
552f364ec65SWenchao Xia         snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
553f364ec65SWenchao Xia     } else {
554f364ec65SWenchao Xia         get_human_readable_size(dsize_buf, sizeof(dsize_buf),
555f364ec65SWenchao Xia                                 info->actual_size);
556f364ec65SWenchao Xia     }
557f364ec65SWenchao Xia     get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
5585b917044SWenchao Xia     func_fprintf(f,
5595b917044SWenchao Xia                  "image: %s\n"
560f364ec65SWenchao Xia                  "file format: %s\n"
561f364ec65SWenchao Xia                  "virtual size: %s (%" PRId64 " bytes)\n"
562f364ec65SWenchao Xia                  "disk size: %s\n",
563f364ec65SWenchao Xia                  info->filename, info->format, size_buf,
564f364ec65SWenchao Xia                  info->virtual_size,
565f364ec65SWenchao Xia                  dsize_buf);
566f364ec65SWenchao Xia 
567f364ec65SWenchao Xia     if (info->has_encrypted && info->encrypted) {
5685b917044SWenchao Xia         func_fprintf(f, "encrypted: yes\n");
569f364ec65SWenchao Xia     }
570f364ec65SWenchao Xia 
571f364ec65SWenchao Xia     if (info->has_cluster_size) {
5725b917044SWenchao Xia         func_fprintf(f, "cluster_size: %" PRId64 "\n",
5735b917044SWenchao Xia                        info->cluster_size);
574f364ec65SWenchao Xia     }
575f364ec65SWenchao Xia 
576f364ec65SWenchao Xia     if (info->has_dirty_flag && info->dirty_flag) {
5775b917044SWenchao Xia         func_fprintf(f, "cleanly shut down: no\n");
578f364ec65SWenchao Xia     }
579f364ec65SWenchao Xia 
580f364ec65SWenchao Xia     if (info->has_backing_filename) {
5815b917044SWenchao Xia         func_fprintf(f, "backing file: %s", info->backing_filename);
582f364ec65SWenchao Xia         if (info->has_full_backing_filename) {
5835b917044SWenchao Xia             func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
584f364ec65SWenchao Xia         }
5855b917044SWenchao Xia         func_fprintf(f, "\n");
586f364ec65SWenchao Xia         if (info->has_backing_filename_format) {
5875b917044SWenchao Xia             func_fprintf(f, "backing file format: %s\n",
5885b917044SWenchao Xia                          info->backing_filename_format);
589f364ec65SWenchao Xia         }
590f364ec65SWenchao Xia     }
591f364ec65SWenchao Xia 
592f364ec65SWenchao Xia     if (info->has_snapshots) {
593f364ec65SWenchao Xia         SnapshotInfoList *elem;
594f364ec65SWenchao Xia 
5955b917044SWenchao Xia         func_fprintf(f, "Snapshot list:\n");
5965b917044SWenchao Xia         bdrv_snapshot_dump(func_fprintf, f, NULL);
5975b917044SWenchao Xia         func_fprintf(f, "\n");
598f364ec65SWenchao Xia 
599f364ec65SWenchao Xia         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
600f364ec65SWenchao Xia          * we convert to the block layer's native QEMUSnapshotInfo for now.
601f364ec65SWenchao Xia          */
602f364ec65SWenchao Xia         for (elem = info->snapshots; elem; elem = elem->next) {
603f364ec65SWenchao Xia             QEMUSnapshotInfo sn = {
604f364ec65SWenchao Xia                 .vm_state_size = elem->value->vm_state_size,
605f364ec65SWenchao Xia                 .date_sec = elem->value->date_sec,
606f364ec65SWenchao Xia                 .date_nsec = elem->value->date_nsec,
607f364ec65SWenchao Xia                 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
608f364ec65SWenchao Xia                                  elem->value->vm_clock_nsec,
609f364ec65SWenchao Xia             };
610f364ec65SWenchao Xia 
611f364ec65SWenchao Xia             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
612f364ec65SWenchao Xia             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
6135b917044SWenchao Xia             bdrv_snapshot_dump(func_fprintf, f, &sn);
6145b917044SWenchao Xia             func_fprintf(f, "\n");
615f364ec65SWenchao Xia         }
616f364ec65SWenchao Xia     }
617a8d8ecb7SMax Reitz 
618a8d8ecb7SMax Reitz     if (info->has_format_specific) {
619a8d8ecb7SMax Reitz         func_fprintf(f, "Format specific information:\n");
620a8d8ecb7SMax Reitz         bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);
621a8d8ecb7SMax Reitz     }
622f364ec65SWenchao Xia }
623