]> git.proxmox.com Git - mirror_qemu.git/blame - block/qapi.c
throttle: acquire the ThrottleGroup lock in bdrv_swap()
[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
25#include "block/qapi.h"
26#include "block/block_int.h"
76f4afb4 27#include "block/throttle-groups.h"
e2462113 28#include "block/write-threshold.h"
f364ec65 29#include "qmp-commands.h"
a8d8ecb7
HR
30#include "qapi-visit.h"
31#include "qapi/qmp-output-visitor.h"
32#include "qapi/qmp/types.h"
d829a211 33#include "sysemu/block-backend.h"
f364ec65 34
d5a8ee60 35BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs, Error **errp)
c13163fb 36{
d5a8ee60
AG
37 ImageInfo **p_image_info;
38 BlockDriverState *bs0;
c13163fb
BC
39 BlockDeviceInfo *info = g_malloc0(sizeof(*info));
40
41 info->file = g_strdup(bs->filename);
42 info->ro = bs->read_only;
43 info->drv = g_strdup(bs->drv->format_name);
44 info->encrypted = bs->encrypted;
45 info->encryption_key_missing = bdrv_key_required(bs);
46
9e193c5a
KW
47 info->cache = g_new(BlockdevCacheInfo, 1);
48 *info->cache = (BlockdevCacheInfo) {
49 .writeback = bdrv_enable_write_cache(bs),
50 .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
51 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
52 };
53
c13163fb
BC
54 if (bs->node_name[0]) {
55 info->has_node_name = true;
56 info->node_name = g_strdup(bs->node_name);
57 }
58
59 if (bs->backing_file[0]) {
60 info->has_backing_file = true;
61 info->backing_file = g_strdup(bs->backing_file);
62 }
63
64 info->backing_file_depth = bdrv_get_backing_file_depth(bs);
465bee1d 65 info->detect_zeroes = bs->detect_zeroes;
c13163fb
BC
66
67 if (bs->io_limits_enabled) {
68 ThrottleConfig cfg;
76f4afb4
AG
69
70 throttle_group_get_config(bs, &cfg);
71
c13163fb
BC
72 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
73 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg;
74 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg;
75
76 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
77 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
78 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
79
80 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
81 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
82 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
83 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
84 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
85 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
86
87 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
88 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
89 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
90 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
91 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
92 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
93
94 info->has_iops_size = cfg.op_size;
95 info->iops_size = cfg.op_size;
96 }
97
e2462113
FR
98 info->write_threshold = bdrv_write_threshold_get(bs);
99
d5a8ee60
AG
100 bs0 = bs;
101 p_image_info = &info->image;
102 while (1) {
103 Error *local_err = NULL;
104 bdrv_query_image_info(bs0, p_image_info, &local_err);
105 if (local_err) {
106 error_propagate(errp, local_err);
107 qapi_free_BlockDeviceInfo(info);
108 return NULL;
109 }
110 if (bs0->drv && bs0->backing_hd) {
111 bs0 = bs0->backing_hd;
112 (*p_image_info)->has_backing_image = true;
113 p_image_info = &((*p_image_info)->backing_image);
114 } else {
115 break;
116 }
117 }
118
c13163fb
BC
119 return info;
120}
121
fb0ed453
WX
122/*
123 * Returns 0 on success, with *p_list either set to describe snapshot
124 * information, or NULL because there are no snapshots. Returns -errno on
125 * error, with *p_list untouched.
126 */
127int bdrv_query_snapshot_info_list(BlockDriverState *bs,
128 SnapshotInfoList **p_list,
129 Error **errp)
f364ec65
WX
130{
131 int i, sn_count;
132 QEMUSnapshotInfo *sn_tab = NULL;
fb0ed453
WX
133 SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
134 SnapshotInfo *info;
135
f364ec65 136 sn_count = bdrv_snapshot_list(bs, &sn_tab);
fb0ed453
WX
137 if (sn_count < 0) {
138 const char *dev = bdrv_get_device_name(bs);
139 switch (sn_count) {
140 case -ENOMEDIUM:
141 error_setg(errp, "Device '%s' is not inserted", dev);
142 break;
143 case -ENOTSUP:
144 error_setg(errp,
145 "Device '%s' does not support internal snapshots",
146 dev);
147 break;
148 default:
149 error_setg_errno(errp, -sn_count,
150 "Can't list snapshots of device '%s'", dev);
151 break;
152 }
153 return sn_count;
154 }
f364ec65
WX
155
156 for (i = 0; i < sn_count; i++) {
fb0ed453
WX
157 info = g_new0(SnapshotInfo, 1);
158 info->id = g_strdup(sn_tab[i].id_str);
159 info->name = g_strdup(sn_tab[i].name);
160 info->vm_state_size = sn_tab[i].vm_state_size;
161 info->date_sec = sn_tab[i].date_sec;
162 info->date_nsec = sn_tab[i].date_nsec;
163 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
164 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
f364ec65 165
fb0ed453
WX
166 info_list = g_new0(SnapshotInfoList, 1);
167 info_list->value = info;
f364ec65
WX
168
169 /* XXX: waiting for the qapi to support qemu-queue.h types */
170 if (!cur_item) {
fb0ed453 171 head = cur_item = info_list;
f364ec65
WX
172 } else {
173 cur_item->next = info_list;
174 cur_item = info_list;
175 }
176
177 }
178
179 g_free(sn_tab);
fb0ed453
WX
180 *p_list = head;
181 return 0;
f364ec65
WX
182}
183
43526ec8
WX
184/**
185 * bdrv_query_image_info:
186 * @bs: block device to examine
187 * @p_info: location to store image information
188 * @errp: location to store error information
189 *
553a7e87
WX
190 * Store "flat" image information in @p_info.
191 *
192 * "Flat" means it does *not* query backing image information,
193 * i.e. (*pinfo)->has_backing_image will be set to false and
194 * (*pinfo)->backing_image to NULL even when the image does in fact have
195 * a backing image.
196 *
43526ec8
WX
197 * @p_info will be set only on success. On error, store error in @errp.
198 */
199void bdrv_query_image_info(BlockDriverState *bs,
200 ImageInfo **p_info,
201 Error **errp)
f364ec65 202{
52bf1e72 203 int64_t size;
43526ec8 204 const char *backing_filename;
f364ec65 205 BlockDriverInfo bdi;
43526ec8
WX
206 int ret;
207 Error *err = NULL;
52bf1e72 208 ImageInfo *info;
f364ec65 209
52bf1e72
MA
210 size = bdrv_getlength(bs);
211 if (size < 0) {
212 error_setg_errno(errp, -size, "Can't get size of device '%s'",
213 bdrv_get_device_name(bs));
214 return;
215 }
f364ec65 216
52bf1e72 217 info = g_new0(ImageInfo, 1);
43526ec8 218 info->filename = g_strdup(bs->filename);
f364ec65 219 info->format = g_strdup(bdrv_get_format_name(bs));
52bf1e72 220 info->virtual_size = size;
f364ec65
WX
221 info->actual_size = bdrv_get_allocated_file_size(bs);
222 info->has_actual_size = info->actual_size >= 0;
223 if (bdrv_is_encrypted(bs)) {
224 info->encrypted = true;
225 info->has_encrypted = true;
226 }
227 if (bdrv_get_info(bs, &bdi) >= 0) {
228 if (bdi.cluster_size != 0) {
229 info->cluster_size = bdi.cluster_size;
230 info->has_cluster_size = true;
231 }
232 info->dirty_flag = bdi.is_dirty;
233 info->has_dirty_flag = true;
234 }
eae041fe
HR
235 info->format_specific = bdrv_get_specific_info(bs);
236 info->has_format_specific = info->format_specific != NULL;
237
43526ec8 238 backing_filename = bs->backing_file;
f364ec65 239 if (backing_filename[0] != '\0') {
9a29e18f 240 char *backing_filename2 = g_malloc0(PATH_MAX);
f364ec65
WX
241 info->backing_filename = g_strdup(backing_filename);
242 info->has_backing_filename = true;
9a29e18f 243 bdrv_get_full_backing_filename(bs, backing_filename2, PATH_MAX, &err);
9f07429e
HR
244 if (err) {
245 error_propagate(errp, err);
246 qapi_free_ImageInfo(info);
564d64bd 247 g_free(backing_filename2);
9f07429e
HR
248 return;
249 }
f364ec65
WX
250
251 if (strcmp(backing_filename, backing_filename2) != 0) {
252 info->full_backing_filename =
253 g_strdup(backing_filename2);
254 info->has_full_backing_filename = true;
255 }
256
257 if (bs->backing_format[0]) {
258 info->backing_filename_format = g_strdup(bs->backing_format);
259 info->has_backing_filename_format = true;
260 }
564d64bd 261 g_free(backing_filename2);
f364ec65 262 }
43526ec8
WX
263
264 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
265 switch (ret) {
266 case 0:
267 if (info->snapshots) {
268 info->has_snapshots = true;
269 }
270 break;
271 /* recoverable error */
272 case -ENOMEDIUM:
273 case -ENOTSUP:
274 error_free(err);
275 break;
276 default:
277 error_propagate(errp, err);
278 qapi_free_ImageInfo(info);
279 return;
280 }
281
282 *p_info = info;
f364ec65
WX
283}
284
553a7e87 285/* @p_info will be set only on success. */
d829a211
MA
286static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
287 Error **errp)
f364ec65
WX
288{
289 BlockInfo *info = g_malloc0(sizeof(*info));
d829a211 290 BlockDriverState *bs = blk_bs(blk);
d829a211 291 info->device = g_strdup(blk_name(blk));
f364ec65 292 info->type = g_strdup("unknown");
a7f53e26
MA
293 info->locked = blk_dev_is_medium_locked(blk);
294 info->removable = blk_dev_has_removable_media(blk);
f364ec65 295
a7f53e26 296 if (blk_dev_has_removable_media(blk)) {
f364ec65 297 info->has_tray_open = true;
a7f53e26 298 info->tray_open = blk_dev_is_tray_open(blk);
f364ec65
WX
299 }
300
301 if (bdrv_iostatus_is_enabled(bs)) {
302 info->has_io_status = true;
303 info->io_status = bs->iostatus;
304 }
305
21b56835
FZ
306 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
307 info->has_dirty_bitmaps = true;
308 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
309 }
310
f364ec65
WX
311 if (bs->drv) {
312 info->has_inserted = true;
d5a8ee60
AG
313 info->inserted = bdrv_block_device_info(bs, errp);
314 if (info->inserted == NULL) {
315 goto err;
553a7e87 316 }
f364ec65 317 }
553a7e87
WX
318
319 *p_info = info;
320 return;
321
322 err:
323 qapi_free_BlockInfo(info);
f364ec65
WX
324}
325
f71eaa74
FZ
326static BlockStats *bdrv_query_stats(const BlockDriverState *bs,
327 bool query_backing)
f364ec65
WX
328{
329 BlockStats *s;
330
331 s = g_malloc0(sizeof(*s));
332
bfb197e0 333 if (bdrv_get_device_name(bs)[0]) {
f364ec65 334 s->has_device = true;
bfb197e0 335 s->device = g_strdup(bdrv_get_device_name(bs));
f364ec65
WX
336 }
337
4875a779
FZ
338 if (bdrv_get_node_name(bs)[0]) {
339 s->has_node_name = true;
340 s->node_name = g_strdup(bdrv_get_node_name(bs));
341 }
342
f364ec65 343 s->stats = g_malloc0(sizeof(*s->stats));
28298fd3
BC
344 s->stats->rd_bytes = bs->stats.nr_bytes[BLOCK_ACCT_READ];
345 s->stats->wr_bytes = bs->stats.nr_bytes[BLOCK_ACCT_WRITE];
346 s->stats->rd_operations = bs->stats.nr_ops[BLOCK_ACCT_READ];
347 s->stats->wr_operations = bs->stats.nr_ops[BLOCK_ACCT_WRITE];
f4564d53
PL
348 s->stats->rd_merged = bs->stats.merged[BLOCK_ACCT_READ];
349 s->stats->wr_merged = bs->stats.merged[BLOCK_ACCT_WRITE];
0ddd0ad9
BC
350 s->stats->wr_highest_offset =
351 bs->stats.wr_highest_sector * BDRV_SECTOR_SIZE;
28298fd3
BC
352 s->stats->flush_operations = bs->stats.nr_ops[BLOCK_ACCT_FLUSH];
353 s->stats->wr_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_WRITE];
354 s->stats->rd_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_READ];
355 s->stats->flush_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_FLUSH];
f364ec65
WX
356
357 if (bs->file) {
358 s->has_parent = true;
f71eaa74 359 s->parent = bdrv_query_stats(bs->file, query_backing);
f364ec65
WX
360 }
361
f71eaa74 362 if (query_backing && bs->backing_hd) {
c8059b97 363 s->has_backing = true;
f71eaa74 364 s->backing = bdrv_query_stats(bs->backing_hd, query_backing);
c8059b97
FZ
365 }
366
f364ec65
WX
367 return s;
368}
369
370BlockInfoList *qmp_query_block(Error **errp)
371{
372 BlockInfoList *head = NULL, **p_next = &head;
d829a211 373 BlockBackend *blk;
553a7e87 374 Error *local_err = NULL;
f364ec65 375
d829a211 376 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
f364ec65 377 BlockInfoList *info = g_malloc0(sizeof(*info));
d829a211 378 bdrv_query_info(blk, &info->value, &local_err);
84d18f06 379 if (local_err) {
553a7e87
WX
380 error_propagate(errp, local_err);
381 goto err;
382 }
f364ec65
WX
383
384 *p_next = info;
385 p_next = &info->next;
386 }
387
388 return head;
553a7e87
WX
389
390 err:
391 qapi_free_BlockInfoList(head);
392 return NULL;
f364ec65
WX
393}
394
f71eaa74
FZ
395BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
396 bool query_nodes,
397 Error **errp)
f364ec65
WX
398{
399 BlockStatsList *head = NULL, **p_next = &head;
400 BlockDriverState *bs = NULL;
401
f71eaa74
FZ
402 /* Just to be safe if query_nodes is not always initialized */
403 query_nodes = has_query_nodes && query_nodes;
404
405 while ((bs = query_nodes ? bdrv_next_node(bs) : bdrv_next(bs))) {
f364ec65 406 BlockStatsList *info = g_malloc0(sizeof(*info));
13344f3a
SH
407 AioContext *ctx = bdrv_get_aio_context(bs);
408
409 aio_context_acquire(ctx);
f71eaa74 410 info->value = bdrv_query_stats(bs, !query_nodes);
13344f3a 411 aio_context_release(ctx);
f364ec65
WX
412
413 *p_next = info;
414 p_next = &info->next;
415 }
416
417 return head;
418}
419
420#define NB_SUFFIXES 4
421
422static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
423{
2c20fa2c 424 static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'};
f364ec65
WX
425 int64_t base;
426 int i;
427
428 if (size <= 999) {
429 snprintf(buf, buf_size, "%" PRId64, size);
430 } else {
431 base = 1024;
432 for (i = 0; i < NB_SUFFIXES; i++) {
433 if (size < (10 * base)) {
434 snprintf(buf, buf_size, "%0.1f%c",
435 (double)size / base,
436 suffixes[i]);
437 break;
438 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
439 snprintf(buf, buf_size, "%" PRId64 "%c",
440 ((size + (base >> 1)) / base),
441 suffixes[i]);
442 break;
443 }
444 base = base * 1024;
445 }
446 }
447 return buf;
448}
449
5b917044
WX
450void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
451 QEMUSnapshotInfo *sn)
f364ec65
WX
452{
453 char buf1[128], date_buf[128], clock_buf[128];
454 struct tm tm;
455 time_t ti;
456 int64_t secs;
457
458 if (!sn) {
5b917044
WX
459 func_fprintf(f,
460 "%-10s%-20s%7s%20s%15s",
461 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
f364ec65
WX
462 } else {
463 ti = sn->date_sec;
464 localtime_r(&ti, &tm);
465 strftime(date_buf, sizeof(date_buf),
466 "%Y-%m-%d %H:%M:%S", &tm);
467 secs = sn->vm_clock_nsec / 1000000000;
468 snprintf(clock_buf, sizeof(clock_buf),
469 "%02d:%02d:%02d.%03d",
470 (int)(secs / 3600),
471 (int)((secs / 60) % 60),
472 (int)(secs % 60),
473 (int)((sn->vm_clock_nsec / 1000000) % 1000));
5b917044
WX
474 func_fprintf(f,
475 "%-10s%-20s%7s%20s%15s",
476 sn->id_str, sn->name,
477 get_human_readable_size(buf1, sizeof(buf1),
478 sn->vm_state_size),
479 date_buf,
480 clock_buf);
f364ec65 481 }
f364ec65
WX
482}
483
a8d8ecb7
HR
484static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
485 QDict *dict);
486static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
487 QList *list);
488
489static void dump_qobject(fprintf_function func_fprintf, void *f,
490 int comp_indent, QObject *obj)
491{
492 switch (qobject_type(obj)) {
493 case QTYPE_QINT: {
494 QInt *value = qobject_to_qint(obj);
495 func_fprintf(f, "%" PRId64, qint_get_int(value));
496 break;
497 }
498 case QTYPE_QSTRING: {
499 QString *value = qobject_to_qstring(obj);
500 func_fprintf(f, "%s", qstring_get_str(value));
501 break;
502 }
503 case QTYPE_QDICT: {
504 QDict *value = qobject_to_qdict(obj);
505 dump_qdict(func_fprintf, f, comp_indent, value);
506 break;
507 }
508 case QTYPE_QLIST: {
509 QList *value = qobject_to_qlist(obj);
510 dump_qlist(func_fprintf, f, comp_indent, value);
511 break;
512 }
513 case QTYPE_QFLOAT: {
514 QFloat *value = qobject_to_qfloat(obj);
515 func_fprintf(f, "%g", qfloat_get_double(value));
516 break;
517 }
518 case QTYPE_QBOOL: {
519 QBool *value = qobject_to_qbool(obj);
520 func_fprintf(f, "%s", qbool_get_int(value) ? "true" : "false");
521 break;
522 }
523 case QTYPE_QERROR: {
524 QString *value = qerror_human((QError *)obj);
525 func_fprintf(f, "%s", qstring_get_str(value));
f25391c2 526 QDECREF(value);
a8d8ecb7
HR
527 break;
528 }
a8d8ecb7
HR
529 default:
530 abort();
531 }
532}
533
534static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
535 QList *list)
536{
537 const QListEntry *entry;
538 int i = 0;
539
540 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
541 qtype_code type = qobject_type(entry->value);
542 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
543 const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: ";
544
545 func_fprintf(f, format, indentation * 4, "", i);
546 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
547 if (!composite) {
548 func_fprintf(f, "\n");
549 }
550 }
551}
552
553static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
554 QDict *dict)
555{
556 const QDictEntry *entry;
557
558 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
559 qtype_code type = qobject_type(entry->value);
560 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
561 const char *format = composite ? "%*s%s:\n" : "%*s%s: ";
562 char key[strlen(entry->key) + 1];
563 int i;
564
565 /* replace dashes with spaces in key (variable) names */
566 for (i = 0; entry->key[i]; i++) {
567 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
568 }
569 key[i] = 0;
570
571 func_fprintf(f, format, indentation * 4, "", key);
572 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
573 if (!composite) {
574 func_fprintf(f, "\n");
575 }
576 }
577}
578
579void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
580 ImageInfoSpecific *info_spec)
581{
a8d8ecb7
HR
582 QmpOutputVisitor *ov = qmp_output_visitor_new();
583 QObject *obj, *data;
584
585 visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov), &info_spec, NULL,
35d0d40a 586 &error_abort);
a8d8ecb7
HR
587 obj = qmp_output_get_qobject(ov);
588 assert(qobject_type(obj) == QTYPE_QDICT);
589 data = qdict_get(qobject_to_qdict(obj), "data");
590 dump_qobject(func_fprintf, f, 1, data);
591 qmp_output_visitor_cleanup(ov);
592}
593
5b917044
WX
594void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
595 ImageInfo *info)
f364ec65
WX
596{
597 char size_buf[128], dsize_buf[128];
598 if (!info->has_actual_size) {
599 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
600 } else {
601 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
602 info->actual_size);
603 }
604 get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
5b917044
WX
605 func_fprintf(f,
606 "image: %s\n"
607 "file format: %s\n"
608 "virtual size: %s (%" PRId64 " bytes)\n"
609 "disk size: %s\n",
610 info->filename, info->format, size_buf,
611 info->virtual_size,
612 dsize_buf);
f364ec65
WX
613
614 if (info->has_encrypted && info->encrypted) {
5b917044 615 func_fprintf(f, "encrypted: yes\n");
f364ec65
WX
616 }
617
618 if (info->has_cluster_size) {
5b917044
WX
619 func_fprintf(f, "cluster_size: %" PRId64 "\n",
620 info->cluster_size);
f364ec65
WX
621 }
622
623 if (info->has_dirty_flag && info->dirty_flag) {
5b917044 624 func_fprintf(f, "cleanly shut down: no\n");
f364ec65
WX
625 }
626
627 if (info->has_backing_filename) {
5b917044 628 func_fprintf(f, "backing file: %s", info->backing_filename);
f364ec65 629 if (info->has_full_backing_filename) {
5b917044 630 func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
f364ec65 631 }
5b917044 632 func_fprintf(f, "\n");
f364ec65 633 if (info->has_backing_filename_format) {
5b917044
WX
634 func_fprintf(f, "backing file format: %s\n",
635 info->backing_filename_format);
f364ec65
WX
636 }
637 }
638
639 if (info->has_snapshots) {
640 SnapshotInfoList *elem;
f364ec65 641
5b917044
WX
642 func_fprintf(f, "Snapshot list:\n");
643 bdrv_snapshot_dump(func_fprintf, f, NULL);
644 func_fprintf(f, "\n");
f364ec65
WX
645
646 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
647 * we convert to the block layer's native QEMUSnapshotInfo for now.
648 */
649 for (elem = info->snapshots; elem; elem = elem->next) {
650 QEMUSnapshotInfo sn = {
651 .vm_state_size = elem->value->vm_state_size,
652 .date_sec = elem->value->date_sec,
653 .date_nsec = elem->value->date_nsec,
654 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
655 elem->value->vm_clock_nsec,
656 };
657
658 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
659 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
5b917044
WX
660 bdrv_snapshot_dump(func_fprintf, f, &sn);
661 func_fprintf(f, "\n");
f364ec65
WX
662 }
663 }
a8d8ecb7
HR
664
665 if (info->has_format_specific) {
666 func_fprintf(f, "Format specific information:\n");
667 bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);
668 }
f364ec65 669}