]> git.proxmox.com Git - mirror_qemu.git/blob - block/qapi.c
block/qapi: Introduce BlockGraphInfo
[mirror_qemu.git] / block / qapi.c
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
25 #include "qemu/osdep.h"
26 #include "qemu/cutils.h"
27 #include "block/qapi.h"
28 #include "block/block_int.h"
29 #include "block/dirty-bitmap.h"
30 #include "block/throttle-groups.h"
31 #include "block/write-threshold.h"
32 #include "qapi/error.h"
33 #include "qapi/qapi-commands-block-core.h"
34 #include "qapi/qobject-output-visitor.h"
35 #include "qapi/qapi-visit-block-core.h"
36 #include "qapi/qmp/qbool.h"
37 #include "qapi/qmp/qdict.h"
38 #include "qapi/qmp/qlist.h"
39 #include "qapi/qmp/qnum.h"
40 #include "qapi/qmp/qstring.h"
41 #include "qemu/qemu-print.h"
42 #include "sysemu/block-backend.h"
43 #include "qemu/cutils.h"
44
45 BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
46 BlockDriverState *bs,
47 bool flat,
48 Error **errp)
49 {
50 ImageInfo **p_image_info;
51 ImageInfo *backing_info;
52 BlockDriverState *bs0, *backing;
53 BlockDeviceInfo *info;
54 ERRP_GUARD();
55
56 if (!bs->drv) {
57 error_setg(errp, "Block device %s is ejected", bs->node_name);
58 return NULL;
59 }
60
61 bdrv_refresh_filename(bs);
62
63 info = g_malloc0(sizeof(*info));
64 info->file = g_strdup(bs->filename);
65 info->ro = bdrv_is_read_only(bs);
66 info->drv = g_strdup(bs->drv->format_name);
67 info->encrypted = bs->encrypted;
68
69 info->cache = g_new(BlockdevCacheInfo, 1);
70 *info->cache = (BlockdevCacheInfo) {
71 .writeback = blk ? blk_enable_write_cache(blk) : true,
72 .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
73 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
74 };
75
76 if (bs->node_name[0]) {
77 info->node_name = g_strdup(bs->node_name);
78 }
79
80 backing = bdrv_cow_bs(bs);
81 if (backing) {
82 info->backing_file = g_strdup(backing->filename);
83 }
84
85 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
86 info->has_dirty_bitmaps = true;
87 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
88 }
89
90 info->detect_zeroes = bs->detect_zeroes;
91
92 if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
93 ThrottleConfig cfg;
94 BlockBackendPublic *blkp = blk_get_public(blk);
95
96 throttle_group_get_config(&blkp->throttle_group_member, &cfg);
97
98 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
99 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg;
100 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg;
101
102 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
103 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
104 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
105
106 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
107 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
108 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
109 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
110 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
111 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
112
113 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
114 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
115 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
116 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
117 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
118 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
119
120 info->has_bps_max_length = info->has_bps_max;
121 info->bps_max_length =
122 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
123 info->has_bps_rd_max_length = info->has_bps_rd_max;
124 info->bps_rd_max_length =
125 cfg.buckets[THROTTLE_BPS_READ].burst_length;
126 info->has_bps_wr_max_length = info->has_bps_wr_max;
127 info->bps_wr_max_length =
128 cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
129
130 info->has_iops_max_length = info->has_iops_max;
131 info->iops_max_length =
132 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
133 info->has_iops_rd_max_length = info->has_iops_rd_max;
134 info->iops_rd_max_length =
135 cfg.buckets[THROTTLE_OPS_READ].burst_length;
136 info->has_iops_wr_max_length = info->has_iops_wr_max;
137 info->iops_wr_max_length =
138 cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
139
140 info->has_iops_size = cfg.op_size;
141 info->iops_size = cfg.op_size;
142
143 info->group =
144 g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
145 }
146
147 info->write_threshold = bdrv_write_threshold_get(bs);
148
149 bs0 = bs;
150 p_image_info = &info->image;
151 info->backing_file_depth = 0;
152
153 /*
154 * Skip automatically inserted nodes that the user isn't aware of for
155 * query-block (blk != NULL), but not for query-named-block-nodes
156 */
157 bdrv_query_image_info(bs0, p_image_info, flat, blk != NULL, errp);
158 if (*errp) {
159 qapi_free_BlockDeviceInfo(info);
160 return NULL;
161 }
162
163 backing_info = info->image->backing_image;
164 while (backing_info) {
165 info->backing_file_depth++;
166 backing_info = backing_info->backing_image;
167 }
168
169 return info;
170 }
171
172 /*
173 * Returns 0 on success, with *p_list either set to describe snapshot
174 * information, or NULL because there are no snapshots. Returns -errno on
175 * error, with *p_list untouched.
176 */
177 int bdrv_query_snapshot_info_list(BlockDriverState *bs,
178 SnapshotInfoList **p_list,
179 Error **errp)
180 {
181 int i, sn_count;
182 QEMUSnapshotInfo *sn_tab = NULL;
183 SnapshotInfoList *head = NULL, **tail = &head;
184 SnapshotInfo *info;
185
186 sn_count = bdrv_snapshot_list(bs, &sn_tab);
187 if (sn_count < 0) {
188 const char *dev = bdrv_get_device_name(bs);
189 switch (sn_count) {
190 case -ENOMEDIUM:
191 error_setg(errp, "Device '%s' is not inserted", dev);
192 break;
193 case -ENOTSUP:
194 error_setg(errp,
195 "Device '%s' does not support internal snapshots",
196 dev);
197 break;
198 default:
199 error_setg_errno(errp, -sn_count,
200 "Can't list snapshots of device '%s'", dev);
201 break;
202 }
203 return sn_count;
204 }
205
206 for (i = 0; i < sn_count; i++) {
207 info = g_new0(SnapshotInfo, 1);
208 info->id = g_strdup(sn_tab[i].id_str);
209 info->name = g_strdup(sn_tab[i].name);
210 info->vm_state_size = sn_tab[i].vm_state_size;
211 info->date_sec = sn_tab[i].date_sec;
212 info->date_nsec = sn_tab[i].date_nsec;
213 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
214 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
215 info->icount = sn_tab[i].icount;
216 info->has_icount = sn_tab[i].icount != -1ULL;
217
218 QAPI_LIST_APPEND(tail, info);
219 }
220
221 g_free(sn_tab);
222 *p_list = head;
223 return 0;
224 }
225
226 /**
227 * Helper function for other query info functions. Store information about @bs
228 * in @info, setting @errp on error.
229 */
230 static void bdrv_do_query_node_info(BlockDriverState *bs,
231 BlockNodeInfo *info,
232 Error **errp)
233 {
234 int64_t size;
235 const char *backing_filename;
236 BlockDriverInfo bdi;
237 int ret;
238 Error *err = NULL;
239
240 aio_context_acquire(bdrv_get_aio_context(bs));
241
242 size = bdrv_getlength(bs);
243 if (size < 0) {
244 error_setg_errno(errp, -size, "Can't get image size '%s'",
245 bs->exact_filename);
246 goto out;
247 }
248
249 bdrv_refresh_filename(bs);
250
251 info->filename = g_strdup(bs->filename);
252 info->format = g_strdup(bdrv_get_format_name(bs));
253 info->virtual_size = size;
254 info->actual_size = bdrv_get_allocated_file_size(bs);
255 info->has_actual_size = info->actual_size >= 0;
256 if (bs->encrypted) {
257 info->encrypted = true;
258 info->has_encrypted = true;
259 }
260 if (bdrv_get_info(bs, &bdi) >= 0) {
261 if (bdi.cluster_size != 0) {
262 info->cluster_size = bdi.cluster_size;
263 info->has_cluster_size = true;
264 }
265 info->dirty_flag = bdi.is_dirty;
266 info->has_dirty_flag = true;
267 }
268 info->format_specific = bdrv_get_specific_info(bs, &err);
269 if (err) {
270 error_propagate(errp, err);
271 goto out;
272 }
273 backing_filename = bs->backing_file;
274 if (backing_filename[0] != '\0') {
275 char *backing_filename2;
276
277 info->backing_filename = g_strdup(backing_filename);
278 backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
279
280 /* Always report the full_backing_filename if present, even if it's the
281 * same as backing_filename. That they are same is useful info. */
282 if (backing_filename2) {
283 info->full_backing_filename = g_strdup(backing_filename2);
284 }
285
286 if (bs->backing_format[0]) {
287 info->backing_filename_format = g_strdup(bs->backing_format);
288 }
289 g_free(backing_filename2);
290 }
291
292 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
293 switch (ret) {
294 case 0:
295 if (info->snapshots) {
296 info->has_snapshots = true;
297 }
298 break;
299 /* recoverable error */
300 case -ENOMEDIUM:
301 case -ENOTSUP:
302 error_free(err);
303 break;
304 default:
305 error_propagate(errp, err);
306 goto out;
307 }
308
309 out:
310 aio_context_release(bdrv_get_aio_context(bs));
311 }
312
313 /**
314 * bdrv_query_block_node_info:
315 * @bs: block node to examine
316 * @p_info: location to store node information
317 * @errp: location to store error information
318 *
319 * Store image information about @bs in @p_info.
320 *
321 * @p_info will be set only on success. On error, store error in @errp.
322 */
323 void bdrv_query_block_node_info(BlockDriverState *bs,
324 BlockNodeInfo **p_info,
325 Error **errp)
326 {
327 BlockNodeInfo *info;
328 ERRP_GUARD();
329
330 info = g_new0(BlockNodeInfo, 1);
331 bdrv_do_query_node_info(bs, info, errp);
332 if (*errp) {
333 qapi_free_BlockNodeInfo(info);
334 return;
335 }
336
337 *p_info = info;
338 }
339
340 /**
341 * bdrv_query_image_info:
342 * @bs: block node to examine
343 * @p_info: location to store image information
344 * @flat: skip backing node information
345 * @skip_implicit_filters: skip implicit filters in the backing chain
346 * @errp: location to store error information
347 *
348 * Store image information in @p_info, potentially recursively covering the
349 * backing chain.
350 *
351 * If @flat is true, do not query backing image information, i.e.
352 * (*p_info)->has_backing_image will be set to false and
353 * (*p_info)->backing_image to NULL even when the image does in fact have a
354 * backing image.
355 *
356 * If @skip_implicit_filters is true, implicit filter nodes in the backing chain
357 * will be skipped when querying backing image information.
358 * (@skip_implicit_filters is ignored when @flat is true.)
359 *
360 * @p_info will be set only on success. On error, store error in @errp.
361 */
362 void bdrv_query_image_info(BlockDriverState *bs,
363 ImageInfo **p_info,
364 bool flat,
365 bool skip_implicit_filters,
366 Error **errp)
367 {
368 ImageInfo *info;
369 ERRP_GUARD();
370
371 info = g_new0(ImageInfo, 1);
372 bdrv_do_query_node_info(bs, qapi_ImageInfo_base(info), errp);
373 if (*errp) {
374 goto fail;
375 }
376
377 if (!flat) {
378 BlockDriverState *backing;
379
380 /*
381 * Use any filtered child here (for backwards compatibility to when
382 * we always took bs->backing, which might be any filtered child).
383 */
384 backing = bdrv_filter_or_cow_bs(bs);
385 if (skip_implicit_filters) {
386 backing = bdrv_skip_implicit_filters(backing);
387 }
388
389 if (backing) {
390 bdrv_query_image_info(backing, &info->backing_image, false,
391 skip_implicit_filters, errp);
392 if (*errp) {
393 goto fail;
394 }
395 }
396 }
397
398 *p_info = info;
399 return;
400
401 fail:
402 assert(*errp);
403 qapi_free_ImageInfo(info);
404 }
405
406 /**
407 * bdrv_query_block_graph_info:
408 * @bs: root node to start from
409 * @p_info: location to store image information
410 * @errp: location to store error information
411 *
412 * Store image information about the graph starting from @bs in @p_info.
413 *
414 * @p_info will be set only on success. On error, store error in @errp.
415 */
416 void bdrv_query_block_graph_info(BlockDriverState *bs,
417 BlockGraphInfo **p_info,
418 Error **errp)
419 {
420 BlockGraphInfo *info;
421 BlockChildInfoList **children_list_tail;
422 BdrvChild *c;
423 ERRP_GUARD();
424
425 info = g_new0(BlockGraphInfo, 1);
426 bdrv_do_query_node_info(bs, qapi_BlockGraphInfo_base(info), errp);
427 if (*errp) {
428 goto fail;
429 }
430
431 children_list_tail = &info->children;
432
433 QLIST_FOREACH(c, &bs->children, next) {
434 BlockChildInfo *c_info;
435
436 c_info = g_new0(BlockChildInfo, 1);
437 QAPI_LIST_APPEND(children_list_tail, c_info);
438
439 c_info->name = g_strdup(c->name);
440 bdrv_query_block_graph_info(c->bs, &c_info->info, errp);
441 if (*errp) {
442 goto fail;
443 }
444 }
445
446 *p_info = info;
447 return;
448
449 fail:
450 assert(*errp != NULL);
451 qapi_free_BlockGraphInfo(info);
452 }
453
454 /* @p_info will be set only on success. */
455 static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
456 Error **errp)
457 {
458 BlockInfo *info = g_malloc0(sizeof(*info));
459 BlockDriverState *bs = blk_bs(blk);
460 char *qdev;
461
462 /* Skip automatically inserted nodes that the user isn't aware of */
463 bs = bdrv_skip_implicit_filters(bs);
464
465 info->device = g_strdup(blk_name(blk));
466 info->type = g_strdup("unknown");
467 info->locked = blk_dev_is_medium_locked(blk);
468 info->removable = blk_dev_has_removable_media(blk);
469
470 qdev = blk_get_attached_dev_id(blk);
471 if (qdev && *qdev) {
472 info->qdev = qdev;
473 } else {
474 g_free(qdev);
475 }
476
477 if (blk_dev_has_tray(blk)) {
478 info->has_tray_open = true;
479 info->tray_open = blk_dev_is_tray_open(blk);
480 }
481
482 if (blk_iostatus_is_enabled(blk)) {
483 info->has_io_status = true;
484 info->io_status = blk_iostatus(blk);
485 }
486
487 if (bs && bs->drv) {
488 info->inserted = bdrv_block_device_info(blk, bs, false, errp);
489 if (info->inserted == NULL) {
490 goto err;
491 }
492 }
493
494 *p_info = info;
495 return;
496
497 err:
498 qapi_free_BlockInfo(info);
499 }
500
501 static uint64List *uint64_list(uint64_t *list, int size)
502 {
503 int i;
504 uint64List *out_list = NULL;
505 uint64List **tail = &out_list;
506
507 for (i = 0; i < size; i++) {
508 QAPI_LIST_APPEND(tail, list[i]);
509 }
510
511 return out_list;
512 }
513
514 static BlockLatencyHistogramInfo *
515 bdrv_latency_histogram_stats(BlockLatencyHistogram *hist)
516 {
517 BlockLatencyHistogramInfo *info;
518
519 if (!hist->bins) {
520 return NULL;
521 }
522
523 info = g_new0(BlockLatencyHistogramInfo, 1);
524 info->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
525 info->bins = uint64_list(hist->bins, hist->nbins);
526 return info;
527 }
528
529 static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
530 {
531 BlockAcctStats *stats = blk_get_stats(blk);
532 BlockAcctTimedStats *ts = NULL;
533 BlockLatencyHistogram *hgram;
534
535 ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
536 ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
537 ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
538 ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
539 ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
540 ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
541
542 ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
543 ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
544 ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
545 ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
546
547 ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
548 ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
549 ds->invalid_flush_operations =
550 stats->invalid_ops[BLOCK_ACCT_FLUSH];
551 ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
552
553 ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
554 ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
555 ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
556 ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
557 ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
558 ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
559 ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
560 ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
561
562 ds->has_idle_time_ns = stats->last_access_time_ns > 0;
563 if (ds->has_idle_time_ns) {
564 ds->idle_time_ns = block_acct_idle_time_ns(stats);
565 }
566
567 ds->account_invalid = stats->account_invalid;
568 ds->account_failed = stats->account_failed;
569
570 while ((ts = block_acct_interval_next(stats, ts))) {
571 BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
572
573 TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
574 TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
575 TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
576
577 dev_stats->interval_length = ts->interval_length;
578
579 dev_stats->min_rd_latency_ns = timed_average_min(rd);
580 dev_stats->max_rd_latency_ns = timed_average_max(rd);
581 dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
582
583 dev_stats->min_wr_latency_ns = timed_average_min(wr);
584 dev_stats->max_wr_latency_ns = timed_average_max(wr);
585 dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
586
587 dev_stats->min_flush_latency_ns = timed_average_min(fl);
588 dev_stats->max_flush_latency_ns = timed_average_max(fl);
589 dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
590
591 dev_stats->avg_rd_queue_depth =
592 block_acct_queue_depth(ts, BLOCK_ACCT_READ);
593 dev_stats->avg_wr_queue_depth =
594 block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
595
596 QAPI_LIST_PREPEND(ds->timed_stats, dev_stats);
597 }
598
599 hgram = stats->latency_histogram;
600 ds->rd_latency_histogram
601 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_READ]);
602 ds->wr_latency_histogram
603 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_WRITE]);
604 ds->flush_latency_histogram
605 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_FLUSH]);
606 }
607
608 static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
609 bool blk_level)
610 {
611 BdrvChild *parent_child;
612 BlockDriverState *filter_or_cow_bs;
613 BlockStats *s = NULL;
614
615 s = g_malloc0(sizeof(*s));
616 s->stats = g_malloc0(sizeof(*s->stats));
617
618 if (!bs) {
619 return s;
620 }
621
622 /* Skip automatically inserted nodes that the user isn't aware of in
623 * a BlockBackend-level command. Stay at the exact node for a node-level
624 * command. */
625 if (blk_level) {
626 bs = bdrv_skip_implicit_filters(bs);
627 }
628
629 if (bdrv_get_node_name(bs)[0]) {
630 s->node_name = g_strdup(bdrv_get_node_name(bs));
631 }
632
633 s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
634
635 s->driver_specific = bdrv_get_specific_stats(bs);
636
637 parent_child = bdrv_primary_child(bs);
638 if (!parent_child ||
639 !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED)))
640 {
641 BdrvChild *c;
642
643 /*
644 * Look for a unique data-storing child. We do not need to look for
645 * filtered children, as there would be only one and it would have been
646 * the primary child.
647 */
648 parent_child = NULL;
649 QLIST_FOREACH(c, &bs->children, next) {
650 if (c->role & BDRV_CHILD_DATA) {
651 if (parent_child) {
652 /*
653 * There are multiple data-storing children and we cannot
654 * choose between them.
655 */
656 parent_child = NULL;
657 break;
658 }
659 parent_child = c;
660 }
661 }
662 }
663 if (parent_child) {
664 s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level);
665 }
666
667 filter_or_cow_bs = bdrv_filter_or_cow_bs(bs);
668 if (blk_level && filter_or_cow_bs) {
669 /*
670 * Put any filtered or COW child here (for backwards
671 * compatibility to when we put bs0->backing here, which might
672 * be either)
673 */
674 s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level);
675 }
676
677 return s;
678 }
679
680 BlockInfoList *qmp_query_block(Error **errp)
681 {
682 BlockInfoList *head = NULL, **p_next = &head;
683 BlockBackend *blk;
684 Error *local_err = NULL;
685
686 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
687 BlockInfoList *info;
688
689 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
690 continue;
691 }
692
693 info = g_malloc0(sizeof(*info));
694 bdrv_query_info(blk, &info->value, &local_err);
695 if (local_err) {
696 error_propagate(errp, local_err);
697 g_free(info);
698 qapi_free_BlockInfoList(head);
699 return NULL;
700 }
701
702 *p_next = info;
703 p_next = &info->next;
704 }
705
706 return head;
707 }
708
709 BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
710 bool query_nodes,
711 Error **errp)
712 {
713 BlockStatsList *head = NULL, **tail = &head;
714 BlockBackend *blk;
715 BlockDriverState *bs;
716
717 /* Just to be safe if query_nodes is not always initialized */
718 if (has_query_nodes && query_nodes) {
719 for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
720 AioContext *ctx = bdrv_get_aio_context(bs);
721
722 aio_context_acquire(ctx);
723 QAPI_LIST_APPEND(tail, bdrv_query_bds_stats(bs, false));
724 aio_context_release(ctx);
725 }
726 } else {
727 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
728 AioContext *ctx = blk_get_aio_context(blk);
729 BlockStats *s;
730 char *qdev;
731
732 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
733 continue;
734 }
735
736 aio_context_acquire(ctx);
737 s = bdrv_query_bds_stats(blk_bs(blk), true);
738 s->device = g_strdup(blk_name(blk));
739
740 qdev = blk_get_attached_dev_id(blk);
741 if (qdev && *qdev) {
742 s->qdev = qdev;
743 } else {
744 g_free(qdev);
745 }
746
747 bdrv_query_blk_stats(s->stats, blk);
748 aio_context_release(ctx);
749
750 QAPI_LIST_APPEND(tail, s);
751 }
752 }
753
754 return head;
755 }
756
757 void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
758 {
759 char clock_buf[128];
760 char icount_buf[128] = {0};
761 int64_t secs;
762 char *sizing = NULL;
763
764 if (!sn) {
765 qemu_printf("%-10s%-17s%8s%20s%13s%11s",
766 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT");
767 } else {
768 g_autoptr(GDateTime) date = g_date_time_new_from_unix_local(sn->date_sec);
769 g_autofree char *date_buf = g_date_time_format(date, "%Y-%m-%d %H:%M:%S");
770
771 secs = sn->vm_clock_nsec / 1000000000;
772 snprintf(clock_buf, sizeof(clock_buf),
773 "%02d:%02d:%02d.%03d",
774 (int)(secs / 3600),
775 (int)((secs / 60) % 60),
776 (int)(secs % 60),
777 (int)((sn->vm_clock_nsec / 1000000) % 1000));
778 sizing = size_to_str(sn->vm_state_size);
779 if (sn->icount != -1ULL) {
780 snprintf(icount_buf, sizeof(icount_buf),
781 "%"PRId64, sn->icount);
782 }
783 qemu_printf("%-9s %-16s %8s%20s%13s%11s",
784 sn->id_str, sn->name,
785 sizing,
786 date_buf,
787 clock_buf,
788 icount_buf);
789 }
790 g_free(sizing);
791 }
792
793 static void dump_qdict(int indentation, QDict *dict);
794 static void dump_qlist(int indentation, QList *list);
795
796 static void dump_qobject(int comp_indent, QObject *obj)
797 {
798 switch (qobject_type(obj)) {
799 case QTYPE_QNUM: {
800 QNum *value = qobject_to(QNum, obj);
801 char *tmp = qnum_to_string(value);
802 qemu_printf("%s", tmp);
803 g_free(tmp);
804 break;
805 }
806 case QTYPE_QSTRING: {
807 QString *value = qobject_to(QString, obj);
808 qemu_printf("%s", qstring_get_str(value));
809 break;
810 }
811 case QTYPE_QDICT: {
812 QDict *value = qobject_to(QDict, obj);
813 dump_qdict(comp_indent, value);
814 break;
815 }
816 case QTYPE_QLIST: {
817 QList *value = qobject_to(QList, obj);
818 dump_qlist(comp_indent, value);
819 break;
820 }
821 case QTYPE_QBOOL: {
822 QBool *value = qobject_to(QBool, obj);
823 qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
824 break;
825 }
826 default:
827 abort();
828 }
829 }
830
831 static void dump_qlist(int indentation, QList *list)
832 {
833 const QListEntry *entry;
834 int i = 0;
835
836 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
837 QType type = qobject_type(entry->value);
838 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
839 qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
840 composite ? '\n' : ' ');
841 dump_qobject(indentation + 1, entry->value);
842 if (!composite) {
843 qemu_printf("\n");
844 }
845 }
846 }
847
848 static void dump_qdict(int indentation, QDict *dict)
849 {
850 const QDictEntry *entry;
851
852 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
853 QType type = qobject_type(entry->value);
854 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
855 char *key = g_malloc(strlen(entry->key) + 1);
856 int i;
857
858 /* replace dashes with spaces in key (variable) names */
859 for (i = 0; entry->key[i]; i++) {
860 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
861 }
862 key[i] = 0;
863 qemu_printf("%*s%s:%c", indentation * 4, "", key,
864 composite ? '\n' : ' ');
865 dump_qobject(indentation + 1, entry->value);
866 if (!composite) {
867 qemu_printf("\n");
868 }
869 g_free(key);
870 }
871 }
872
873 /*
874 * Return whether dumping the given QObject with dump_qobject() would
875 * yield an empty dump, i.e. not print anything.
876 */
877 static bool qobject_is_empty_dump(const QObject *obj)
878 {
879 switch (qobject_type(obj)) {
880 case QTYPE_QNUM:
881 case QTYPE_QSTRING:
882 case QTYPE_QBOOL:
883 return false;
884
885 case QTYPE_QDICT:
886 return qdict_size(qobject_to(QDict, obj)) == 0;
887
888 case QTYPE_QLIST:
889 return qlist_empty(qobject_to(QList, obj));
890
891 default:
892 abort();
893 }
894 }
895
896 /**
897 * Dumps the given ImageInfoSpecific object in a human-readable form,
898 * prepending an optional prefix if the dump is not empty.
899 */
900 void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec,
901 const char *prefix)
902 {
903 QObject *obj, *data;
904 Visitor *v = qobject_output_visitor_new(&obj);
905
906 visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
907 visit_complete(v, &obj);
908 data = qdict_get(qobject_to(QDict, obj), "data");
909 if (!qobject_is_empty_dump(data)) {
910 if (prefix) {
911 qemu_printf("%s", prefix);
912 }
913 dump_qobject(1, data);
914 }
915 qobject_unref(obj);
916 visit_free(v);
917 }
918
919 void bdrv_node_info_dump(BlockNodeInfo *info)
920 {
921 char *size_buf, *dsize_buf;
922 if (!info->has_actual_size) {
923 dsize_buf = g_strdup("unavailable");
924 } else {
925 dsize_buf = size_to_str(info->actual_size);
926 }
927 size_buf = size_to_str(info->virtual_size);
928 qemu_printf("image: %s\n"
929 "file format: %s\n"
930 "virtual size: %s (%" PRId64 " bytes)\n"
931 "disk size: %s\n",
932 info->filename, info->format, size_buf,
933 info->virtual_size,
934 dsize_buf);
935 g_free(size_buf);
936 g_free(dsize_buf);
937
938 if (info->has_encrypted && info->encrypted) {
939 qemu_printf("encrypted: yes\n");
940 }
941
942 if (info->has_cluster_size) {
943 qemu_printf("cluster_size: %" PRId64 "\n",
944 info->cluster_size);
945 }
946
947 if (info->has_dirty_flag && info->dirty_flag) {
948 qemu_printf("cleanly shut down: no\n");
949 }
950
951 if (info->backing_filename) {
952 qemu_printf("backing file: %s", info->backing_filename);
953 if (!info->full_backing_filename) {
954 qemu_printf(" (cannot determine actual path)");
955 } else if (strcmp(info->backing_filename,
956 info->full_backing_filename) != 0) {
957 qemu_printf(" (actual path: %s)", info->full_backing_filename);
958 }
959 qemu_printf("\n");
960 if (info->backing_filename_format) {
961 qemu_printf("backing file format: %s\n",
962 info->backing_filename_format);
963 }
964 }
965
966 if (info->has_snapshots) {
967 SnapshotInfoList *elem;
968
969 qemu_printf("Snapshot list:\n");
970 bdrv_snapshot_dump(NULL);
971 qemu_printf("\n");
972
973 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
974 * we convert to the block layer's native QEMUSnapshotInfo for now.
975 */
976 for (elem = info->snapshots; elem; elem = elem->next) {
977 QEMUSnapshotInfo sn = {
978 .vm_state_size = elem->value->vm_state_size,
979 .date_sec = elem->value->date_sec,
980 .date_nsec = elem->value->date_nsec,
981 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
982 elem->value->vm_clock_nsec,
983 .icount = elem->value->has_icount ?
984 elem->value->icount : -1ULL,
985 };
986
987 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
988 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
989 bdrv_snapshot_dump(&sn);
990 qemu_printf("\n");
991 }
992 }
993
994 if (info->format_specific) {
995 bdrv_image_info_specific_dump(info->format_specific,
996 "Format specific information:\n");
997 }
998 }