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