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