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