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