]> git.proxmox.com Git - mirror_qemu.git/blame - block/qapi.c
i386/kvm: hv-evmcs requires hv-vapic
[mirror_qemu.git] / block / qapi.c
CommitLineData
f364ec65
WX
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
80c71a24 25#include "qemu/osdep.h"
a8d25326 26#include "qemu-common.h"
f364ec65
WX
27#include "block/qapi.h"
28#include "block/block_int.h"
76f4afb4 29#include "block/throttle-groups.h"
e2462113 30#include "block/write-threshold.h"
e688df6b 31#include "qapi/error.h"
9af23989 32#include "qapi/qapi-commands-block-core.h"
b3db211f 33#include "qapi/qobject-output-visitor.h"
9af23989 34#include "qapi/qapi-visit-block-core.h"
6b673957 35#include "qapi/qmp/qbool.h"
452fcdbc 36#include "qapi/qmp/qdict.h"
47e6b297 37#include "qapi/qmp/qlist.h"
15280c36 38#include "qapi/qmp/qnum.h"
6b673957 39#include "qapi/qmp/qstring.h"
e1ce7d74 40#include "qemu/qemu-print.h"
d829a211 41#include "sysemu/block-backend.h"
f348b6d1 42#include "qemu/cutils.h"
f364ec65 43
c83f9fba
KW
44BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
45 BlockDriverState *bs, Error **errp)
c13163fb 46{
d5a8ee60
AG
47 ImageInfo **p_image_info;
48 BlockDriverState *bs0;
d470ad42 49 BlockDeviceInfo *info;
c13163fb 50
d470ad42
HR
51 if (!bs->drv) {
52 error_setg(errp, "Block device %s is ejected", bs->node_name);
53 return NULL;
54 }
55
f30c66ba
HR
56 bdrv_refresh_filename(bs);
57
d470ad42 58 info = g_malloc0(sizeof(*info));
c13163fb
BC
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;
c01c214b 63 info->encryption_key_missing = false;
c13163fb 64
9e193c5a
KW
65 info->cache = g_new(BlockdevCacheInfo, 1);
66 *info->cache = (BlockdevCacheInfo) {
c83f9fba 67 .writeback = blk ? blk_enable_write_cache(blk) : true,
9e193c5a
KW
68 .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
69 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
70 };
71
c13163fb
BC
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
465bee1d 82 info->detect_zeroes = bs->detect_zeroes;
c13163fb 83
022cdc9f 84 if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
c13163fb 85 ThrottleConfig cfg;
022cdc9f 86 BlockBackendPublic *blkp = blk_get_public(blk);
76f4afb4 87
022cdc9f 88 throttle_group_get_config(&blkp->throttle_group_member, &cfg);
76f4afb4 89
c13163fb
BC
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
398befdf
AG
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
c13163fb
BC
132 info->has_iops_size = cfg.op_size;
133 info->iops_size = cfg.op_size;
b8fe1694
AG
134
135 info->has_group = true;
022cdc9f
MP
136 info->group =
137 g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
c13163fb
BC
138 }
139
e2462113
FR
140 info->write_threshold = bdrv_write_threshold_get(bs);
141
d5a8ee60
AG
142 bs0 = bs;
143 p_image_info = &info->image;
d3c8c674 144 info->backing_file_depth = 0;
d5a8ee60
AG
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 }
d3c8c674 153
760e0063 154 if (bs0->drv && bs0->backing) {
d3c8c674 155 info->backing_file_depth++;
760e0063 156 bs0 = bs0->backing->bs;
d5a8ee60
AG
157 (*p_image_info)->has_backing_image = true;
158 p_image_info = &((*p_image_info)->backing_image);
159 } else {
160 break;
161 }
d3c8c674
KW
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 */
8e8eb0a9 165 while (blk && bs0->drv && bs0->implicit) {
d3c8c674 166 bs0 = backing_bs(bs0);
8e8eb0a9 167 assert(bs0);
d3c8c674 168 }
d5a8ee60
AG
169 }
170
c13163fb
BC
171 return info;
172}
173
fb0ed453
WX
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 */
179int bdrv_query_snapshot_info_list(BlockDriverState *bs,
180 SnapshotInfoList **p_list,
181 Error **errp)
f364ec65
WX
182{
183 int i, sn_count;
184 QEMUSnapshotInfo *sn_tab = NULL;
fb0ed453
WX
185 SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
186 SnapshotInfo *info;
187
f364ec65 188 sn_count = bdrv_snapshot_list(bs, &sn_tab);
fb0ed453
WX
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 }
f364ec65
WX
207
208 for (i = 0; i < sn_count; i++) {
fb0ed453
WX
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;
f364ec65 217
fb0ed453
WX
218 info_list = g_new0(SnapshotInfoList, 1);
219 info_list->value = info;
f364ec65
WX
220
221 /* XXX: waiting for the qapi to support qemu-queue.h types */
222 if (!cur_item) {
fb0ed453 223 head = cur_item = info_list;
f364ec65
WX
224 } else {
225 cur_item->next = info_list;
226 cur_item = info_list;
227 }
228
229 }
230
231 g_free(sn_tab);
fb0ed453
WX
232 *p_list = head;
233 return 0;
f364ec65
WX
234}
235
43526ec8
WX
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 *
553a7e87
WX
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 *
43526ec8
WX
249 * @p_info will be set only on success. On error, store error in @errp.
250 */
251void bdrv_query_image_info(BlockDriverState *bs,
252 ImageInfo **p_info,
253 Error **errp)
f364ec65 254{
52bf1e72 255 int64_t size;
43526ec8 256 const char *backing_filename;
f364ec65 257 BlockDriverInfo bdi;
43526ec8
WX
258 int ret;
259 Error *err = NULL;
52bf1e72 260 ImageInfo *info;
f364ec65 261
1963f8d5
PB
262 aio_context_acquire(bdrv_get_aio_context(bs));
263
52bf1e72
MA
264 size = bdrv_getlength(bs);
265 if (size < 0) {
9adceb02
FZ
266 error_setg_errno(errp, -size, "Can't get image size '%s'",
267 bs->exact_filename);
1963f8d5 268 goto out;
52bf1e72 269 }
f364ec65 270
f30c66ba
HR
271 bdrv_refresh_filename(bs);
272
52bf1e72 273 info = g_new0(ImageInfo, 1);
43526ec8 274 info->filename = g_strdup(bs->filename);
f364ec65 275 info->format = g_strdup(bdrv_get_format_name(bs));
52bf1e72 276 info->virtual_size = size;
f364ec65
WX
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 }
1bf6e9ca
AS
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 }
eae041fe
HR
297 info->has_format_specific = info->format_specific != NULL;
298
43526ec8 299 backing_filename = bs->backing_file;
f364ec65 300 if (backing_filename[0] != '\0') {
6b6833c1 301 char *backing_filename2;
f364ec65
WX
302 info->backing_filename = g_strdup(backing_filename);
303 info->has_backing_filename = true;
6b6833c1 304 backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
f364ec65 305
12dcb1c0
JS
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);
f364ec65
WX
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 }
564d64bd 317 g_free(backing_filename2);
f364ec65 318 }
43526ec8
WX
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);
1963f8d5 335 goto out;
43526ec8
WX
336 }
337
338 *p_info = info;
1963f8d5
PB
339
340out:
341 aio_context_release(bdrv_get_aio_context(bs));
f364ec65
WX
342}
343
553a7e87 344/* @p_info will be set only on success. */
d829a211
MA
345static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
346 Error **errp)
f364ec65
WX
347{
348 BlockInfo *info = g_malloc0(sizeof(*info));
d829a211 349 BlockDriverState *bs = blk_bs(blk);
46eade7b
KW
350 char *qdev;
351
d3c8c674
KW
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
d829a211 357 info->device = g_strdup(blk_name(blk));
f364ec65 358 info->type = g_strdup("unknown");
a7f53e26
MA
359 info->locked = blk_dev_is_medium_locked(blk);
360 info->removable = blk_dev_has_removable_media(blk);
f364ec65 361
46eade7b
KW
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
327032ce 370 if (blk_dev_has_tray(blk)) {
f364ec65 371 info->has_tray_open = true;
a7f53e26 372 info->tray_open = blk_dev_is_tray_open(blk);
f364ec65
WX
373 }
374
373340b2 375 if (blk_iostatus_is_enabled(blk)) {
f364ec65 376 info->has_io_status = true;
373340b2 377 info->io_status = blk_iostatus(blk);
f364ec65
WX
378 }
379
5433c24f 380 if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
21b56835
FZ
381 info->has_dirty_bitmaps = true;
382 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
383 }
384
5433c24f 385 if (bs && bs->drv) {
f364ec65 386 info->has_inserted = true;
c83f9fba 387 info->inserted = bdrv_block_device_info(blk, bs, errp);
d5a8ee60
AG
388 if (info->inserted == NULL) {
389 goto err;
553a7e87 390 }
f364ec65 391 }
553a7e87
WX
392
393 *p_info = info;
394 return;
395
396 err:
397 qapi_free_BlockInfo(info);
f364ec65
WX
398}
399
7e5c776d
VSO
400static 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
418static 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
54302156 431static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
2b77e60a
KW
432{
433 BlockAcctStats *stats = blk_get_stats(blk);
434 BlockAcctTimedStats *ts = NULL;
435
54302156
HR
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];
2b77e60a 440
54302156
HR
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];
2b77e60a 444
54302156
HR
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 =
2b77e60a
KW
448 stats->invalid_ops[BLOCK_ACCT_FLUSH];
449
54302156
HR
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];
2b77e60a 456
54302156
HR
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);
2b77e60a
KW
460 }
461
54302156
HR
462 ds->account_invalid = stats->account_invalid;
463 ds->account_failed = stats->account_failed;
2b77e60a
KW
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));
54302156 469 timed_stats->next = ds->timed_stats;
2b77e60a 470 timed_stats->value = dev_stats;
54302156 471 ds->timed_stats = timed_stats;
2b77e60a
KW
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 }
7e5c776d
VSO
496
497 bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_READ],
cb8aac37
VSO
498 &ds->has_rd_latency_histogram,
499 &ds->rd_latency_histogram);
7e5c776d 500 bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_WRITE],
cb8aac37
VSO
501 &ds->has_wr_latency_histogram,
502 &ds->wr_latency_histogram);
7e5c776d 503 bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_FLUSH],
cb8aac37
VSO
504 &ds->has_flush_latency_histogram,
505 &ds->flush_latency_histogram);
2b77e60a
KW
506}
507
d3c8c674
KW
508static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
509 bool blk_level)
f364ec65 510{
20a6d768
DL
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
d3c8c674
KW
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
4875a779
FZ
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
f7946da2 533 s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
53d8f9d8 534
f364ec65
WX
535 if (bs->file) {
536 s->has_parent = true;
d3c8c674 537 s->parent = bdrv_query_bds_stats(bs->file->bs, blk_level);
f364ec65
WX
538 }
539
d3c8c674 540 if (blk_level && bs->backing) {
c8059b97 541 s->has_backing = true;
d3c8c674 542 s->backing = bdrv_query_bds_stats(bs->backing->bs, blk_level);
c8059b97
FZ
543 }
544
20a6d768 545 return s;
b07363a1
KW
546}
547
f364ec65
WX
548BlockInfoList *qmp_query_block(Error **errp)
549{
550 BlockInfoList *head = NULL, **p_next = &head;
d829a211 551 BlockBackend *blk;
553a7e87 552 Error *local_err = NULL;
f364ec65 553
d5b68844
KW
554 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
555 BlockInfoList *info;
556
ec18b0a9 557 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
d5b68844
KW
558 continue;
559 }
560
561 info = g_malloc0(sizeof(*info));
d829a211 562 bdrv_query_info(blk, &info->value, &local_err);
84d18f06 563 if (local_err) {
553a7e87 564 error_propagate(errp, local_err);
903c341d
MA
565 g_free(info);
566 qapi_free_BlockInfoList(head);
567 return NULL;
553a7e87 568 }
f364ec65
WX
569
570 *p_next = info;
571 p_next = &info->next;
572 }
573
574 return head;
575}
576
f71eaa74
FZ
577BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
578 bool query_nodes,
579 Error **errp)
f364ec65
WX
580{
581 BlockStatsList *head = NULL, **p_next = &head;
a6baa608
DL
582 BlockBackend *blk;
583 BlockDriverState *bs;
f364ec65 584
f71eaa74 585 /* Just to be safe if query_nodes is not always initialized */
a6baa608
DL
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);
13344f3a 590
a6baa608
DL
591 aio_context_acquire(ctx);
592 info->value = bdrv_query_bds_stats(bs, false);
593 aio_context_release(ctx);
f364ec65 594
a6baa608
DL
595 *p_next = info;
596 p_next = &info->next;
597 }
598 } else {
567dcb31 599 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
f62492bb 600 BlockStatsList *info;
a6baa608
DL
601 AioContext *ctx = blk_get_aio_context(blk);
602 BlockStats *s;
5a9cb5a9 603 char *qdev;
a6baa608 604
567dcb31
KW
605 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
606 continue;
607 }
608
a6baa608
DL
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));
5a9cb5a9
KW
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
a6baa608
DL
622 bdrv_query_blk_stats(s->stats, blk);
623 aio_context_release(ctx);
624
f62492bb 625 info = g_malloc0(sizeof(*info));
a6baa608
DL
626 info->value = s;
627 *p_next = info;
628 p_next = &info->next;
629 }
f364ec65
WX
630 }
631
632 return head;
633}
634
e1ce7d74 635void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
f364ec65 636{
de38b500 637 char date_buf[128], clock_buf[128];
f364ec65
WX
638 struct tm tm;
639 time_t ti;
640 int64_t secs;
de38b500 641 char *sizing = NULL;
f364ec65
WX
642
643 if (!sn) {
e1ce7d74
MA
644 qemu_printf("%-10s%-20s%7s%20s%15s",
645 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
f364ec65
WX
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));
de38b500 658 sizing = size_to_str(sn->vm_state_size);
e1ce7d74
MA
659 qemu_printf("%-10s%-20s%7s%20s%15s",
660 sn->id_str, sn->name,
de38b500 661 sizing,
e1ce7d74
MA
662 date_buf,
663 clock_buf);
f364ec65 664 }
de38b500 665 g_free(sizing);
f364ec65
WX
666}
667
e1ce7d74
MA
668static void dump_qdict(int indentation, QDict *dict);
669static void dump_qlist(int indentation, QList *list);
a8d8ecb7 670
e1ce7d74 671static void dump_qobject(int comp_indent, QObject *obj)
a8d8ecb7
HR
672{
673 switch (qobject_type(obj)) {
01b2ffce 674 case QTYPE_QNUM: {
7dc847eb 675 QNum *value = qobject_to(QNum, obj);
01b2ffce 676 char *tmp = qnum_to_string(value);
e1ce7d74 677 qemu_printf("%s", tmp);
01b2ffce 678 g_free(tmp);
a8d8ecb7
HR
679 break;
680 }
681 case QTYPE_QSTRING: {
7dc847eb 682 QString *value = qobject_to(QString, obj);
e1ce7d74 683 qemu_printf("%s", qstring_get_str(value));
a8d8ecb7
HR
684 break;
685 }
686 case QTYPE_QDICT: {
7dc847eb 687 QDict *value = qobject_to(QDict, obj);
e1ce7d74 688 dump_qdict(comp_indent, value);
a8d8ecb7
HR
689 break;
690 }
691 case QTYPE_QLIST: {
7dc847eb 692 QList *value = qobject_to(QList, obj);
e1ce7d74 693 dump_qlist(comp_indent, value);
a8d8ecb7
HR
694 break;
695 }
a8d8ecb7 696 case QTYPE_QBOOL: {
7dc847eb 697 QBool *value = qobject_to(QBool, obj);
e1ce7d74 698 qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
a8d8ecb7
HR
699 break;
700 }
a8d8ecb7
HR
701 default:
702 abort();
703 }
704}
705
e1ce7d74 706static void dump_qlist(int indentation, QList *list)
a8d8ecb7
HR
707{
708 const QListEntry *entry;
709 int i = 0;
710
711 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
1310a3d3 712 QType type = qobject_type(entry->value);
a8d8ecb7 713 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
e1ce7d74
MA
714 qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
715 composite ? '\n' : ' ');
716 dump_qobject(indentation + 1, entry->value);
a8d8ecb7 717 if (!composite) {
e1ce7d74 718 qemu_printf("\n");
a8d8ecb7
HR
719 }
720 }
721}
722
e1ce7d74 723static void dump_qdict(int indentation, QDict *dict)
a8d8ecb7
HR
724{
725 const QDictEntry *entry;
726
727 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
1310a3d3 728 QType type = qobject_type(entry->value);
a8d8ecb7 729 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
5eda6227 730 char *key = g_malloc(strlen(entry->key) + 1);
a8d8ecb7
HR
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;
e1ce7d74
MA
738 qemu_printf("%*s%s:%c", indentation * 4, "", key,
739 composite ? '\n' : ' ');
740 dump_qobject(indentation + 1, entry->value);
a8d8ecb7 741 if (!composite) {
e1ce7d74 742 qemu_printf("\n");
a8d8ecb7 743 }
5eda6227 744 g_free(key);
a8d8ecb7
HR
745 }
746}
747
e1ce7d74 748void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec)
a8d8ecb7 749{
a8d8ecb7 750 QObject *obj, *data;
7d5e199a 751 Visitor *v = qobject_output_visitor_new(&obj);
a8d8ecb7 752
3b098d56
EB
753 visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
754 visit_complete(v, &obj);
7dc847eb 755 data = qdict_get(qobject_to(QDict, obj), "data");
e1ce7d74 756 dump_qobject(1, data);
cb3e7f08 757 qobject_unref(obj);
3b098d56 758 visit_free(v);
a8d8ecb7
HR
759}
760
e1ce7d74 761void bdrv_image_info_dump(ImageInfo *info)
f364ec65 762{
de38b500 763 char *size_buf, *dsize_buf;
f364ec65 764 if (!info->has_actual_size) {
de38b500 765 dsize_buf = g_strdup("unavailable");
f364ec65 766 } else {
de38b500 767 dsize_buf = size_to_str(info->actual_size);
f364ec65 768 }
de38b500 769 size_buf = size_to_str(info->virtual_size);
e1ce7d74
MA
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);
de38b500
EB
777 g_free(size_buf);
778 g_free(dsize_buf);
f364ec65
WX
779
780 if (info->has_encrypted && info->encrypted) {
e1ce7d74 781 qemu_printf("encrypted: yes\n");
f364ec65
WX
782 }
783
784 if (info->has_cluster_size) {
e1ce7d74
MA
785 qemu_printf("cluster_size: %" PRId64 "\n",
786 info->cluster_size);
f364ec65
WX
787 }
788
789 if (info->has_dirty_flag && info->dirty_flag) {
e1ce7d74 790 qemu_printf("cleanly shut down: no\n");
f364ec65
WX
791 }
792
793 if (info->has_backing_filename) {
e1ce7d74 794 qemu_printf("backing file: %s", info->backing_filename);
5c9d9ca5 795 if (!info->has_full_backing_filename) {
e1ce7d74 796 qemu_printf(" (cannot determine actual path)");
5c9d9ca5
JS
797 } else if (strcmp(info->backing_filename,
798 info->full_backing_filename) != 0) {
e1ce7d74 799 qemu_printf(" (actual path: %s)", info->full_backing_filename);
f364ec65 800 }
e1ce7d74 801 qemu_printf("\n");
f364ec65 802 if (info->has_backing_filename_format) {
e1ce7d74
MA
803 qemu_printf("backing file format: %s\n",
804 info->backing_filename_format);
f364ec65
WX
805 }
806 }
807
808 if (info->has_snapshots) {
809 SnapshotInfoList *elem;
f364ec65 810
e1ce7d74
MA
811 qemu_printf("Snapshot list:\n");
812 bdrv_snapshot_dump(NULL);
813 qemu_printf("\n");
f364ec65
WX
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);
e1ce7d74
MA
829 bdrv_snapshot_dump(&sn);
830 qemu_printf("\n");
f364ec65
WX
831 }
832 }
a8d8ecb7
HR
833
834 if (info->has_format_specific) {
e1ce7d74
MA
835 qemu_printf("Format specific information:\n");
836 bdrv_image_info_specific_dump(info->format_specific);
a8d8ecb7 837 }
f364ec65 838}