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