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