]>
Commit | Line | Data |
---|---|---|
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" |
000c4dff CL |
32 | #ifdef __linux__ |
33 | #include <linux/fs.h> | |
34 | #include <sys/ioctl.h> | |
35 | #ifndef FS_NOCOW_FL | |
36 | #define FS_NOCOW_FL 0x00800000 /* Do not cow file */ | |
37 | #endif | |
38 | #endif | |
f364ec65 | 39 | |
c13163fb BC |
40 | BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs) |
41 | { | |
42 | BlockDeviceInfo *info = g_malloc0(sizeof(*info)); | |
43 | ||
44 | info->file = g_strdup(bs->filename); | |
45 | info->ro = bs->read_only; | |
46 | info->drv = g_strdup(bs->drv->format_name); | |
47 | info->encrypted = bs->encrypted; | |
48 | info->encryption_key_missing = bdrv_key_required(bs); | |
49 | ||
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 | */ | |
100 | int 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 | */ | |
172 | void 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 WX |
178 | char backing_filename2[1024]; |
179 | BlockDriverInfo bdi; | |
43526ec8 WX |
180 | int ret; |
181 | Error *err = NULL; | |
52bf1e72 | 182 | ImageInfo *info; |
000c4dff CL |
183 | #ifdef __linux__ |
184 | int fd, attr; | |
185 | #endif | |
f364ec65 | 186 | |
52bf1e72 MA |
187 | size = bdrv_getlength(bs); |
188 | if (size < 0) { | |
189 | error_setg_errno(errp, -size, "Can't get size of device '%s'", | |
190 | bdrv_get_device_name(bs)); | |
191 | return; | |
192 | } | |
f364ec65 | 193 | |
52bf1e72 | 194 | info = g_new0(ImageInfo, 1); |
43526ec8 | 195 | info->filename = g_strdup(bs->filename); |
f364ec65 | 196 | info->format = g_strdup(bdrv_get_format_name(bs)); |
52bf1e72 | 197 | info->virtual_size = size; |
f364ec65 WX |
198 | info->actual_size = bdrv_get_allocated_file_size(bs); |
199 | info->has_actual_size = info->actual_size >= 0; | |
200 | if (bdrv_is_encrypted(bs)) { | |
201 | info->encrypted = true; | |
202 | info->has_encrypted = true; | |
203 | } | |
204 | if (bdrv_get_info(bs, &bdi) >= 0) { | |
205 | if (bdi.cluster_size != 0) { | |
206 | info->cluster_size = bdi.cluster_size; | |
207 | info->has_cluster_size = true; | |
208 | } | |
209 | info->dirty_flag = bdi.is_dirty; | |
210 | info->has_dirty_flag = true; | |
211 | } | |
eae041fe HR |
212 | info->format_specific = bdrv_get_specific_info(bs); |
213 | info->has_format_specific = info->format_specific != NULL; | |
214 | ||
000c4dff CL |
215 | #ifdef __linux__ |
216 | /* get NOCOW info */ | |
217 | fd = qemu_open(bs->filename, O_RDONLY | O_NONBLOCK); | |
218 | if (fd >= 0) { | |
219 | if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0 && (attr & FS_NOCOW_FL)) { | |
220 | info->has_nocow = true; | |
221 | info->nocow = true; | |
222 | } | |
223 | qemu_close(fd); | |
224 | } | |
225 | #endif | |
226 | ||
43526ec8 | 227 | backing_filename = bs->backing_file; |
f364ec65 WX |
228 | if (backing_filename[0] != '\0') { |
229 | info->backing_filename = g_strdup(backing_filename); | |
230 | info->has_backing_filename = true; | |
231 | bdrv_get_full_backing_filename(bs, backing_filename2, | |
232 | sizeof(backing_filename2)); | |
233 | ||
234 | if (strcmp(backing_filename, backing_filename2) != 0) { | |
235 | info->full_backing_filename = | |
236 | g_strdup(backing_filename2); | |
237 | info->has_full_backing_filename = true; | |
238 | } | |
239 | ||
240 | if (bs->backing_format[0]) { | |
241 | info->backing_filename_format = g_strdup(bs->backing_format); | |
242 | info->has_backing_filename_format = true; | |
243 | } | |
244 | } | |
43526ec8 WX |
245 | |
246 | ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err); | |
247 | switch (ret) { | |
248 | case 0: | |
249 | if (info->snapshots) { | |
250 | info->has_snapshots = true; | |
251 | } | |
252 | break; | |
253 | /* recoverable error */ | |
254 | case -ENOMEDIUM: | |
255 | case -ENOTSUP: | |
256 | error_free(err); | |
257 | break; | |
258 | default: | |
259 | error_propagate(errp, err); | |
260 | qapi_free_ImageInfo(info); | |
261 | return; | |
262 | } | |
263 | ||
264 | *p_info = info; | |
f364ec65 WX |
265 | } |
266 | ||
553a7e87 | 267 | /* @p_info will be set only on success. */ |
d829a211 MA |
268 | static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, |
269 | Error **errp) | |
f364ec65 WX |
270 | { |
271 | BlockInfo *info = g_malloc0(sizeof(*info)); | |
d829a211 | 272 | BlockDriverState *bs = blk_bs(blk); |
553a7e87 WX |
273 | BlockDriverState *bs0; |
274 | ImageInfo **p_image_info; | |
275 | Error *local_err = NULL; | |
d829a211 | 276 | info->device = g_strdup(blk_name(blk)); |
f364ec65 WX |
277 | info->type = g_strdup("unknown"); |
278 | info->locked = bdrv_dev_is_medium_locked(bs); | |
279 | info->removable = bdrv_dev_has_removable_media(bs); | |
280 | ||
281 | if (bdrv_dev_has_removable_media(bs)) { | |
282 | info->has_tray_open = true; | |
283 | info->tray_open = bdrv_dev_is_tray_open(bs); | |
284 | } | |
285 | ||
286 | if (bdrv_iostatus_is_enabled(bs)) { | |
287 | info->has_io_status = true; | |
288 | info->io_status = bs->iostatus; | |
289 | } | |
290 | ||
21b56835 FZ |
291 | if (!QLIST_EMPTY(&bs->dirty_bitmaps)) { |
292 | info->has_dirty_bitmaps = true; | |
293 | info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs); | |
294 | } | |
295 | ||
f364ec65 WX |
296 | if (bs->drv) { |
297 | info->has_inserted = true; | |
c13163fb | 298 | info->inserted = bdrv_block_device_info(bs); |
553a7e87 WX |
299 | |
300 | bs0 = bs; | |
301 | p_image_info = &info->inserted->image; | |
302 | while (1) { | |
303 | bdrv_query_image_info(bs0, p_image_info, &local_err); | |
84d18f06 | 304 | if (local_err) { |
553a7e87 WX |
305 | error_propagate(errp, local_err); |
306 | goto err; | |
307 | } | |
308 | if (bs0->drv && bs0->backing_hd) { | |
309 | bs0 = bs0->backing_hd; | |
310 | (*p_image_info)->has_backing_image = true; | |
311 | p_image_info = &((*p_image_info)->backing_image); | |
312 | } else { | |
313 | break; | |
314 | } | |
315 | } | |
f364ec65 | 316 | } |
553a7e87 WX |
317 | |
318 | *p_info = info; | |
319 | return; | |
320 | ||
321 | err: | |
322 | qapi_free_BlockInfo(info); | |
f364ec65 WX |
323 | } |
324 | ||
ac46821f | 325 | static BlockStats *bdrv_query_stats(const BlockDriverState *bs) |
f364ec65 WX |
326 | { |
327 | BlockStats *s; | |
328 | ||
329 | s = g_malloc0(sizeof(*s)); | |
330 | ||
bfb197e0 | 331 | if (bdrv_get_device_name(bs)[0]) { |
f364ec65 | 332 | s->has_device = true; |
bfb197e0 | 333 | s->device = g_strdup(bdrv_get_device_name(bs)); |
f364ec65 WX |
334 | } |
335 | ||
336 | s->stats = g_malloc0(sizeof(*s->stats)); | |
28298fd3 BC |
337 | s->stats->rd_bytes = bs->stats.nr_bytes[BLOCK_ACCT_READ]; |
338 | s->stats->wr_bytes = bs->stats.nr_bytes[BLOCK_ACCT_WRITE]; | |
339 | s->stats->rd_operations = bs->stats.nr_ops[BLOCK_ACCT_READ]; | |
340 | s->stats->wr_operations = bs->stats.nr_ops[BLOCK_ACCT_WRITE]; | |
0ddd0ad9 BC |
341 | s->stats->wr_highest_offset = |
342 | bs->stats.wr_highest_sector * BDRV_SECTOR_SIZE; | |
28298fd3 BC |
343 | s->stats->flush_operations = bs->stats.nr_ops[BLOCK_ACCT_FLUSH]; |
344 | s->stats->wr_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_WRITE]; | |
345 | s->stats->rd_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_READ]; | |
346 | s->stats->flush_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_FLUSH]; | |
f364ec65 WX |
347 | |
348 | if (bs->file) { | |
349 | s->has_parent = true; | |
350 | s->parent = bdrv_query_stats(bs->file); | |
351 | } | |
352 | ||
c8059b97 FZ |
353 | if (bs->backing_hd) { |
354 | s->has_backing = true; | |
355 | s->backing = bdrv_query_stats(bs->backing_hd); | |
356 | } | |
357 | ||
f364ec65 WX |
358 | return s; |
359 | } | |
360 | ||
361 | BlockInfoList *qmp_query_block(Error **errp) | |
362 | { | |
363 | BlockInfoList *head = NULL, **p_next = &head; | |
d829a211 | 364 | BlockBackend *blk; |
553a7e87 | 365 | Error *local_err = NULL; |
f364ec65 | 366 | |
d829a211 | 367 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
f364ec65 | 368 | BlockInfoList *info = g_malloc0(sizeof(*info)); |
d829a211 | 369 | bdrv_query_info(blk, &info->value, &local_err); |
84d18f06 | 370 | if (local_err) { |
553a7e87 WX |
371 | error_propagate(errp, local_err); |
372 | goto err; | |
373 | } | |
f364ec65 WX |
374 | |
375 | *p_next = info; | |
376 | p_next = &info->next; | |
377 | } | |
378 | ||
379 | return head; | |
553a7e87 WX |
380 | |
381 | err: | |
382 | qapi_free_BlockInfoList(head); | |
383 | return NULL; | |
f364ec65 WX |
384 | } |
385 | ||
386 | BlockStatsList *qmp_query_blockstats(Error **errp) | |
387 | { | |
388 | BlockStatsList *head = NULL, **p_next = &head; | |
389 | BlockDriverState *bs = NULL; | |
390 | ||
391 | while ((bs = bdrv_next(bs))) { | |
392 | BlockStatsList *info = g_malloc0(sizeof(*info)); | |
13344f3a SH |
393 | AioContext *ctx = bdrv_get_aio_context(bs); |
394 | ||
395 | aio_context_acquire(ctx); | |
f364ec65 | 396 | info->value = bdrv_query_stats(bs); |
13344f3a | 397 | aio_context_release(ctx); |
f364ec65 WX |
398 | |
399 | *p_next = info; | |
400 | p_next = &info->next; | |
401 | } | |
402 | ||
403 | return head; | |
404 | } | |
405 | ||
406 | #define NB_SUFFIXES 4 | |
407 | ||
408 | static char *get_human_readable_size(char *buf, int buf_size, int64_t size) | |
409 | { | |
410 | static const char suffixes[NB_SUFFIXES] = "KMGT"; | |
411 | int64_t base; | |
412 | int i; | |
413 | ||
414 | if (size <= 999) { | |
415 | snprintf(buf, buf_size, "%" PRId64, size); | |
416 | } else { | |
417 | base = 1024; | |
418 | for (i = 0; i < NB_SUFFIXES; i++) { | |
419 | if (size < (10 * base)) { | |
420 | snprintf(buf, buf_size, "%0.1f%c", | |
421 | (double)size / base, | |
422 | suffixes[i]); | |
423 | break; | |
424 | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { | |
425 | snprintf(buf, buf_size, "%" PRId64 "%c", | |
426 | ((size + (base >> 1)) / base), | |
427 | suffixes[i]); | |
428 | break; | |
429 | } | |
430 | base = base * 1024; | |
431 | } | |
432 | } | |
433 | return buf; | |
434 | } | |
435 | ||
5b917044 WX |
436 | void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f, |
437 | QEMUSnapshotInfo *sn) | |
f364ec65 WX |
438 | { |
439 | char buf1[128], date_buf[128], clock_buf[128]; | |
440 | struct tm tm; | |
441 | time_t ti; | |
442 | int64_t secs; | |
443 | ||
444 | if (!sn) { | |
5b917044 WX |
445 | func_fprintf(f, |
446 | "%-10s%-20s%7s%20s%15s", | |
447 | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); | |
f364ec65 WX |
448 | } else { |
449 | ti = sn->date_sec; | |
450 | localtime_r(&ti, &tm); | |
451 | strftime(date_buf, sizeof(date_buf), | |
452 | "%Y-%m-%d %H:%M:%S", &tm); | |
453 | secs = sn->vm_clock_nsec / 1000000000; | |
454 | snprintf(clock_buf, sizeof(clock_buf), | |
455 | "%02d:%02d:%02d.%03d", | |
456 | (int)(secs / 3600), | |
457 | (int)((secs / 60) % 60), | |
458 | (int)(secs % 60), | |
459 | (int)((sn->vm_clock_nsec / 1000000) % 1000)); | |
5b917044 WX |
460 | func_fprintf(f, |
461 | "%-10s%-20s%7s%20s%15s", | |
462 | sn->id_str, sn->name, | |
463 | get_human_readable_size(buf1, sizeof(buf1), | |
464 | sn->vm_state_size), | |
465 | date_buf, | |
466 | clock_buf); | |
f364ec65 | 467 | } |
f364ec65 WX |
468 | } |
469 | ||
a8d8ecb7 HR |
470 | static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation, |
471 | QDict *dict); | |
472 | static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation, | |
473 | QList *list); | |
474 | ||
475 | static void dump_qobject(fprintf_function func_fprintf, void *f, | |
476 | int comp_indent, QObject *obj) | |
477 | { | |
478 | switch (qobject_type(obj)) { | |
479 | case QTYPE_QINT: { | |
480 | QInt *value = qobject_to_qint(obj); | |
481 | func_fprintf(f, "%" PRId64, qint_get_int(value)); | |
482 | break; | |
483 | } | |
484 | case QTYPE_QSTRING: { | |
485 | QString *value = qobject_to_qstring(obj); | |
486 | func_fprintf(f, "%s", qstring_get_str(value)); | |
487 | break; | |
488 | } | |
489 | case QTYPE_QDICT: { | |
490 | QDict *value = qobject_to_qdict(obj); | |
491 | dump_qdict(func_fprintf, f, comp_indent, value); | |
492 | break; | |
493 | } | |
494 | case QTYPE_QLIST: { | |
495 | QList *value = qobject_to_qlist(obj); | |
496 | dump_qlist(func_fprintf, f, comp_indent, value); | |
497 | break; | |
498 | } | |
499 | case QTYPE_QFLOAT: { | |
500 | QFloat *value = qobject_to_qfloat(obj); | |
501 | func_fprintf(f, "%g", qfloat_get_double(value)); | |
502 | break; | |
503 | } | |
504 | case QTYPE_QBOOL: { | |
505 | QBool *value = qobject_to_qbool(obj); | |
506 | func_fprintf(f, "%s", qbool_get_int(value) ? "true" : "false"); | |
507 | break; | |
508 | } | |
509 | case QTYPE_QERROR: { | |
510 | QString *value = qerror_human((QError *)obj); | |
511 | func_fprintf(f, "%s", qstring_get_str(value)); | |
f25391c2 | 512 | QDECREF(value); |
a8d8ecb7 HR |
513 | break; |
514 | } | |
515 | case QTYPE_NONE: | |
516 | break; | |
517 | case QTYPE_MAX: | |
518 | default: | |
519 | abort(); | |
520 | } | |
521 | } | |
522 | ||
523 | static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation, | |
524 | QList *list) | |
525 | { | |
526 | const QListEntry *entry; | |
527 | int i = 0; | |
528 | ||
529 | for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) { | |
530 | qtype_code type = qobject_type(entry->value); | |
531 | bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); | |
532 | const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: "; | |
533 | ||
534 | func_fprintf(f, format, indentation * 4, "", i); | |
535 | dump_qobject(func_fprintf, f, indentation + 1, entry->value); | |
536 | if (!composite) { | |
537 | func_fprintf(f, "\n"); | |
538 | } | |
539 | } | |
540 | } | |
541 | ||
542 | static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation, | |
543 | QDict *dict) | |
544 | { | |
545 | const QDictEntry *entry; | |
546 | ||
547 | for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) { | |
548 | qtype_code type = qobject_type(entry->value); | |
549 | bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); | |
550 | const char *format = composite ? "%*s%s:\n" : "%*s%s: "; | |
551 | char key[strlen(entry->key) + 1]; | |
552 | int i; | |
553 | ||
554 | /* replace dashes with spaces in key (variable) names */ | |
555 | for (i = 0; entry->key[i]; i++) { | |
556 | key[i] = entry->key[i] == '-' ? ' ' : entry->key[i]; | |
557 | } | |
558 | key[i] = 0; | |
559 | ||
560 | func_fprintf(f, format, indentation * 4, "", key); | |
561 | dump_qobject(func_fprintf, f, indentation + 1, entry->value); | |
562 | if (!composite) { | |
563 | func_fprintf(f, "\n"); | |
564 | } | |
565 | } | |
566 | } | |
567 | ||
568 | void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f, | |
569 | ImageInfoSpecific *info_spec) | |
570 | { | |
a8d8ecb7 HR |
571 | QmpOutputVisitor *ov = qmp_output_visitor_new(); |
572 | QObject *obj, *data; | |
573 | ||
574 | visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov), &info_spec, NULL, | |
35d0d40a | 575 | &error_abort); |
a8d8ecb7 HR |
576 | obj = qmp_output_get_qobject(ov); |
577 | assert(qobject_type(obj) == QTYPE_QDICT); | |
578 | data = qdict_get(qobject_to_qdict(obj), "data"); | |
579 | dump_qobject(func_fprintf, f, 1, data); | |
580 | qmp_output_visitor_cleanup(ov); | |
581 | } | |
582 | ||
5b917044 WX |
583 | void bdrv_image_info_dump(fprintf_function func_fprintf, void *f, |
584 | ImageInfo *info) | |
f364ec65 WX |
585 | { |
586 | char size_buf[128], dsize_buf[128]; | |
587 | if (!info->has_actual_size) { | |
588 | snprintf(dsize_buf, sizeof(dsize_buf), "unavailable"); | |
589 | } else { | |
590 | get_human_readable_size(dsize_buf, sizeof(dsize_buf), | |
591 | info->actual_size); | |
592 | } | |
593 | get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size); | |
5b917044 WX |
594 | func_fprintf(f, |
595 | "image: %s\n" | |
596 | "file format: %s\n" | |
597 | "virtual size: %s (%" PRId64 " bytes)\n" | |
598 | "disk size: %s\n", | |
599 | info->filename, info->format, size_buf, | |
600 | info->virtual_size, | |
601 | dsize_buf); | |
f364ec65 WX |
602 | |
603 | if (info->has_encrypted && info->encrypted) { | |
5b917044 | 604 | func_fprintf(f, "encrypted: yes\n"); |
f364ec65 WX |
605 | } |
606 | ||
607 | if (info->has_cluster_size) { | |
5b917044 WX |
608 | func_fprintf(f, "cluster_size: %" PRId64 "\n", |
609 | info->cluster_size); | |
f364ec65 WX |
610 | } |
611 | ||
612 | if (info->has_dirty_flag && info->dirty_flag) { | |
5b917044 | 613 | func_fprintf(f, "cleanly shut down: no\n"); |
f364ec65 WX |
614 | } |
615 | ||
616 | if (info->has_backing_filename) { | |
5b917044 | 617 | func_fprintf(f, "backing file: %s", info->backing_filename); |
f364ec65 | 618 | if (info->has_full_backing_filename) { |
5b917044 | 619 | func_fprintf(f, " (actual path: %s)", info->full_backing_filename); |
f364ec65 | 620 | } |
5b917044 | 621 | func_fprintf(f, "\n"); |
f364ec65 | 622 | if (info->has_backing_filename_format) { |
5b917044 WX |
623 | func_fprintf(f, "backing file format: %s\n", |
624 | info->backing_filename_format); | |
f364ec65 WX |
625 | } |
626 | } | |
627 | ||
628 | if (info->has_snapshots) { | |
629 | SnapshotInfoList *elem; | |
f364ec65 | 630 | |
5b917044 WX |
631 | func_fprintf(f, "Snapshot list:\n"); |
632 | bdrv_snapshot_dump(func_fprintf, f, NULL); | |
633 | func_fprintf(f, "\n"); | |
f364ec65 WX |
634 | |
635 | /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but | |
636 | * we convert to the block layer's native QEMUSnapshotInfo for now. | |
637 | */ | |
638 | for (elem = info->snapshots; elem; elem = elem->next) { | |
639 | QEMUSnapshotInfo sn = { | |
640 | .vm_state_size = elem->value->vm_state_size, | |
641 | .date_sec = elem->value->date_sec, | |
642 | .date_nsec = elem->value->date_nsec, | |
643 | .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL + | |
644 | elem->value->vm_clock_nsec, | |
645 | }; | |
646 | ||
647 | pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id); | |
648 | pstrcpy(sn.name, sizeof(sn.name), elem->value->name); | |
5b917044 WX |
649 | bdrv_snapshot_dump(func_fprintf, f, &sn); |
650 | func_fprintf(f, "\n"); | |
f364ec65 WX |
651 | } |
652 | } | |
a8d8ecb7 HR |
653 | |
654 | if (info->has_format_specific) { | |
655 | func_fprintf(f, "Format specific information:\n"); | |
656 | bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific); | |
657 | } | |
000c4dff CL |
658 | |
659 | if (info->has_nocow && info->nocow) { | |
660 | func_fprintf(f, "NOCOW flag: set\n"); | |
661 | } | |
f364ec65 | 662 | } |