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