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