2 * Block layer qmp and info dump related functions
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "block/qapi.h"
26 #include "block/block_int.h"
27 #include "qmp-commands.h"
28 #include "qapi-visit.h"
29 #include "qapi/qmp-output-visitor.h"
30 #include "qapi/qmp/types.h"
33 #include <sys/ioctl.h>
35 #define FS_NOCOW_FL 0x00800000 /* Do not cow file */
39 BlockDeviceInfo
*bdrv_block_device_info(BlockDriverState
*bs
)
41 BlockDeviceInfo
*info
= g_malloc0(sizeof(*info
));
43 info
->file
= g_strdup(bs
->filename
);
44 info
->ro
= bs
->read_only
;
45 info
->drv
= g_strdup(bs
->drv
->format_name
);
46 info
->encrypted
= bs
->encrypted
;
47 info
->encryption_key_missing
= bdrv_key_required(bs
);
49 if (bs
->node_name
[0]) {
50 info
->has_node_name
= true;
51 info
->node_name
= g_strdup(bs
->node_name
);
54 if (bs
->backing_file
[0]) {
55 info
->has_backing_file
= true;
56 info
->backing_file
= g_strdup(bs
->backing_file
);
59 info
->backing_file_depth
= bdrv_get_backing_file_depth(bs
);
60 info
->detect_zeroes
= bs
->detect_zeroes
;
62 if (bs
->io_limits_enabled
) {
64 throttle_get_config(&bs
->throttle_state
, &cfg
);
65 info
->bps
= cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
;
66 info
->bps_rd
= cfg
.buckets
[THROTTLE_BPS_READ
].avg
;
67 info
->bps_wr
= cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
;
69 info
->iops
= cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
;
70 info
->iops_rd
= cfg
.buckets
[THROTTLE_OPS_READ
].avg
;
71 info
->iops_wr
= cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
;
73 info
->has_bps_max
= cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
;
74 info
->bps_max
= cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
;
75 info
->has_bps_rd_max
= cfg
.buckets
[THROTTLE_BPS_READ
].max
;
76 info
->bps_rd_max
= cfg
.buckets
[THROTTLE_BPS_READ
].max
;
77 info
->has_bps_wr_max
= cfg
.buckets
[THROTTLE_BPS_WRITE
].max
;
78 info
->bps_wr_max
= cfg
.buckets
[THROTTLE_BPS_WRITE
].max
;
80 info
->has_iops_max
= cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
;
81 info
->iops_max
= cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
;
82 info
->has_iops_rd_max
= cfg
.buckets
[THROTTLE_OPS_READ
].max
;
83 info
->iops_rd_max
= cfg
.buckets
[THROTTLE_OPS_READ
].max
;
84 info
->has_iops_wr_max
= cfg
.buckets
[THROTTLE_OPS_WRITE
].max
;
85 info
->iops_wr_max
= cfg
.buckets
[THROTTLE_OPS_WRITE
].max
;
87 info
->has_iops_size
= cfg
.op_size
;
88 info
->iops_size
= cfg
.op_size
;
95 * Returns 0 on success, with *p_list either set to describe snapshot
96 * information, or NULL because there are no snapshots. Returns -errno on
97 * error, with *p_list untouched.
99 int bdrv_query_snapshot_info_list(BlockDriverState
*bs
,
100 SnapshotInfoList
**p_list
,
104 QEMUSnapshotInfo
*sn_tab
= NULL
;
105 SnapshotInfoList
*info_list
, *cur_item
= NULL
, *head
= NULL
;
108 sn_count
= bdrv_snapshot_list(bs
, &sn_tab
);
110 const char *dev
= bdrv_get_device_name(bs
);
113 error_setg(errp
, "Device '%s' is not inserted", dev
);
117 "Device '%s' does not support internal snapshots",
121 error_setg_errno(errp
, -sn_count
,
122 "Can't list snapshots of device '%s'", dev
);
128 for (i
= 0; i
< sn_count
; i
++) {
129 info
= g_new0(SnapshotInfo
, 1);
130 info
->id
= g_strdup(sn_tab
[i
].id_str
);
131 info
->name
= g_strdup(sn_tab
[i
].name
);
132 info
->vm_state_size
= sn_tab
[i
].vm_state_size
;
133 info
->date_sec
= sn_tab
[i
].date_sec
;
134 info
->date_nsec
= sn_tab
[i
].date_nsec
;
135 info
->vm_clock_sec
= sn_tab
[i
].vm_clock_nsec
/ 1000000000;
136 info
->vm_clock_nsec
= sn_tab
[i
].vm_clock_nsec
% 1000000000;
138 info_list
= g_new0(SnapshotInfoList
, 1);
139 info_list
->value
= info
;
141 /* XXX: waiting for the qapi to support qemu-queue.h types */
143 head
= cur_item
= info_list
;
145 cur_item
->next
= info_list
;
146 cur_item
= info_list
;
157 * bdrv_query_image_info:
158 * @bs: block device to examine
159 * @p_info: location to store image information
160 * @errp: location to store error information
162 * Store "flat" image information in @p_info.
164 * "Flat" means it does *not* query backing image information,
165 * i.e. (*pinfo)->has_backing_image will be set to false and
166 * (*pinfo)->backing_image to NULL even when the image does in fact have
169 * @p_info will be set only on success. On error, store error in @errp.
171 void bdrv_query_image_info(BlockDriverState
*bs
,
176 const char *backing_filename
;
177 char backing_filename2
[1024];
186 size
= bdrv_getlength(bs
);
188 error_setg_errno(errp
, -size
, "Can't get size of device '%s'",
189 bdrv_get_device_name(bs
));
193 info
= g_new0(ImageInfo
, 1);
194 info
->filename
= g_strdup(bs
->filename
);
195 info
->format
= g_strdup(bdrv_get_format_name(bs
));
196 info
->virtual_size
= size
;
197 info
->actual_size
= bdrv_get_allocated_file_size(bs
);
198 info
->has_actual_size
= info
->actual_size
>= 0;
199 if (bdrv_is_encrypted(bs
)) {
200 info
->encrypted
= true;
201 info
->has_encrypted
= true;
203 if (bdrv_get_info(bs
, &bdi
) >= 0) {
204 if (bdi
.cluster_size
!= 0) {
205 info
->cluster_size
= bdi
.cluster_size
;
206 info
->has_cluster_size
= true;
208 info
->dirty_flag
= bdi
.is_dirty
;
209 info
->has_dirty_flag
= true;
211 info
->format_specific
= bdrv_get_specific_info(bs
);
212 info
->has_format_specific
= info
->format_specific
!= NULL
;
216 fd
= qemu_open(bs
->filename
, O_RDONLY
| O_NONBLOCK
);
218 if (ioctl(fd
, FS_IOC_GETFLAGS
, &attr
) == 0 && (attr
& FS_NOCOW_FL
)) {
219 info
->has_nocow
= true;
226 backing_filename
= bs
->backing_file
;
227 if (backing_filename
[0] != '\0') {
228 info
->backing_filename
= g_strdup(backing_filename
);
229 info
->has_backing_filename
= true;
230 bdrv_get_full_backing_filename(bs
, backing_filename2
,
231 sizeof(backing_filename2
));
233 if (strcmp(backing_filename
, backing_filename2
) != 0) {
234 info
->full_backing_filename
=
235 g_strdup(backing_filename2
);
236 info
->has_full_backing_filename
= true;
239 if (bs
->backing_format
[0]) {
240 info
->backing_filename_format
= g_strdup(bs
->backing_format
);
241 info
->has_backing_filename_format
= true;
245 ret
= bdrv_query_snapshot_info_list(bs
, &info
->snapshots
, &err
);
248 if (info
->snapshots
) {
249 info
->has_snapshots
= true;
252 /* recoverable error */
258 error_propagate(errp
, err
);
259 qapi_free_ImageInfo(info
);
266 /* @p_info will be set only on success. */
267 void bdrv_query_info(BlockDriverState
*bs
,
271 BlockInfo
*info
= g_malloc0(sizeof(*info
));
272 BlockDriverState
*bs0
;
273 ImageInfo
**p_image_info
;
274 Error
*local_err
= NULL
;
275 info
->device
= g_strdup(bs
->device_name
);
276 info
->type
= g_strdup("unknown");
277 info
->locked
= bdrv_dev_is_medium_locked(bs
);
278 info
->removable
= bdrv_dev_has_removable_media(bs
);
280 if (bdrv_dev_has_removable_media(bs
)) {
281 info
->has_tray_open
= true;
282 info
->tray_open
= bdrv_dev_is_tray_open(bs
);
285 if (bdrv_iostatus_is_enabled(bs
)) {
286 info
->has_io_status
= true;
287 info
->io_status
= bs
->iostatus
;
290 if (!QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
291 info
->has_dirty_bitmaps
= true;
292 info
->dirty_bitmaps
= bdrv_query_dirty_bitmaps(bs
);
296 info
->has_inserted
= true;
297 info
->inserted
= bdrv_block_device_info(bs
);
300 p_image_info
= &info
->inserted
->image
;
302 bdrv_query_image_info(bs0
, p_image_info
, &local_err
);
304 error_propagate(errp
, local_err
);
307 if (bs0
->drv
&& bs0
->backing_hd
) {
308 bs0
= bs0
->backing_hd
;
309 (*p_image_info
)->has_backing_image
= true;
310 p_image_info
= &((*p_image_info
)->backing_image
);
321 qapi_free_BlockInfo(info
);
324 static BlockStats
*bdrv_query_stats(const BlockDriverState
*bs
)
328 s
= g_malloc0(sizeof(*s
));
330 if (bs
->device_name
[0]) {
331 s
->has_device
= true;
332 s
->device
= g_strdup(bs
->device_name
);
335 s
->stats
= g_malloc0(sizeof(*s
->stats
));
336 s
->stats
->rd_bytes
= bs
->stats
.nr_bytes
[BLOCK_ACCT_READ
];
337 s
->stats
->wr_bytes
= bs
->stats
.nr_bytes
[BLOCK_ACCT_WRITE
];
338 s
->stats
->rd_operations
= bs
->stats
.nr_ops
[BLOCK_ACCT_READ
];
339 s
->stats
->wr_operations
= bs
->stats
.nr_ops
[BLOCK_ACCT_WRITE
];
340 s
->stats
->wr_highest_offset
=
341 bs
->stats
.wr_highest_sector
* BDRV_SECTOR_SIZE
;
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
];
348 s
->has_parent
= true;
349 s
->parent
= bdrv_query_stats(bs
->file
);
352 if (bs
->backing_hd
) {
353 s
->has_backing
= true;
354 s
->backing
= bdrv_query_stats(bs
->backing_hd
);
360 BlockInfoList
*qmp_query_block(Error
**errp
)
362 BlockInfoList
*head
= NULL
, **p_next
= &head
;
363 BlockDriverState
*bs
= NULL
;
364 Error
*local_err
= NULL
;
366 while ((bs
= bdrv_next(bs
))) {
367 BlockInfoList
*info
= g_malloc0(sizeof(*info
));
368 bdrv_query_info(bs
, &info
->value
, &local_err
);
370 error_propagate(errp
, local_err
);
375 p_next
= &info
->next
;
381 qapi_free_BlockInfoList(head
);
385 BlockStatsList
*qmp_query_blockstats(Error
**errp
)
387 BlockStatsList
*head
= NULL
, **p_next
= &head
;
388 BlockDriverState
*bs
= NULL
;
390 while ((bs
= bdrv_next(bs
))) {
391 BlockStatsList
*info
= g_malloc0(sizeof(*info
));
392 AioContext
*ctx
= bdrv_get_aio_context(bs
);
394 aio_context_acquire(ctx
);
395 info
->value
= bdrv_query_stats(bs
);
396 aio_context_release(ctx
);
399 p_next
= &info
->next
;
405 #define NB_SUFFIXES 4
407 static char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
409 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
414 snprintf(buf
, buf_size
, "%" PRId64
, size
);
417 for (i
= 0; i
< NB_SUFFIXES
; i
++) {
418 if (size
< (10 * base
)) {
419 snprintf(buf
, buf_size
, "%0.1f%c",
423 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
424 snprintf(buf
, buf_size
, "%" PRId64
"%c",
425 ((size
+ (base
>> 1)) / base
),
435 void bdrv_snapshot_dump(fprintf_function func_fprintf
, void *f
,
436 QEMUSnapshotInfo
*sn
)
438 char buf1
[128], date_buf
[128], clock_buf
[128];
445 "%-10s%-20s%7s%20s%15s",
446 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
449 localtime_r(&ti
, &tm
);
450 strftime(date_buf
, sizeof(date_buf
),
451 "%Y-%m-%d %H:%M:%S", &tm
);
452 secs
= sn
->vm_clock_nsec
/ 1000000000;
453 snprintf(clock_buf
, sizeof(clock_buf
),
454 "%02d:%02d:%02d.%03d",
456 (int)((secs
/ 60) % 60),
458 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
460 "%-10s%-20s%7s%20s%15s",
461 sn
->id_str
, sn
->name
,
462 get_human_readable_size(buf1
, sizeof(buf1
),
469 static void dump_qdict(fprintf_function func_fprintf
, void *f
, int indentation
,
471 static void dump_qlist(fprintf_function func_fprintf
, void *f
, int indentation
,
474 static void dump_qobject(fprintf_function func_fprintf
, void *f
,
475 int comp_indent
, QObject
*obj
)
477 switch (qobject_type(obj
)) {
479 QInt
*value
= qobject_to_qint(obj
);
480 func_fprintf(f
, "%" PRId64
, qint_get_int(value
));
483 case QTYPE_QSTRING
: {
484 QString
*value
= qobject_to_qstring(obj
);
485 func_fprintf(f
, "%s", qstring_get_str(value
));
489 QDict
*value
= qobject_to_qdict(obj
);
490 dump_qdict(func_fprintf
, f
, comp_indent
, value
);
494 QList
*value
= qobject_to_qlist(obj
);
495 dump_qlist(func_fprintf
, f
, comp_indent
, value
);
499 QFloat
*value
= qobject_to_qfloat(obj
);
500 func_fprintf(f
, "%g", qfloat_get_double(value
));
504 QBool
*value
= qobject_to_qbool(obj
);
505 func_fprintf(f
, "%s", qbool_get_int(value
) ? "true" : "false");
509 QString
*value
= qerror_human((QError
*)obj
);
510 func_fprintf(f
, "%s", qstring_get_str(value
));
522 static void dump_qlist(fprintf_function func_fprintf
, void *f
, int indentation
,
525 const QListEntry
*entry
;
528 for (entry
= qlist_first(list
); entry
; entry
= qlist_next(entry
), i
++) {
529 qtype_code type
= qobject_type(entry
->value
);
530 bool composite
= (type
== QTYPE_QDICT
|| type
== QTYPE_QLIST
);
531 const char *format
= composite
? "%*s[%i]:\n" : "%*s[%i]: ";
533 func_fprintf(f
, format
, indentation
* 4, "", i
);
534 dump_qobject(func_fprintf
, f
, indentation
+ 1, entry
->value
);
536 func_fprintf(f
, "\n");
541 static void dump_qdict(fprintf_function func_fprintf
, void *f
, int indentation
,
544 const QDictEntry
*entry
;
546 for (entry
= qdict_first(dict
); entry
; entry
= qdict_next(dict
, entry
)) {
547 qtype_code type
= qobject_type(entry
->value
);
548 bool composite
= (type
== QTYPE_QDICT
|| type
== QTYPE_QLIST
);
549 const char *format
= composite
? "%*s%s:\n" : "%*s%s: ";
550 char key
[strlen(entry
->key
) + 1];
553 /* replace dashes with spaces in key (variable) names */
554 for (i
= 0; entry
->key
[i
]; i
++) {
555 key
[i
] = entry
->key
[i
] == '-' ? ' ' : entry
->key
[i
];
559 func_fprintf(f
, format
, indentation
* 4, "", key
);
560 dump_qobject(func_fprintf
, f
, indentation
+ 1, entry
->value
);
562 func_fprintf(f
, "\n");
567 void bdrv_image_info_specific_dump(fprintf_function func_fprintf
, void *f
,
568 ImageInfoSpecific
*info_spec
)
570 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
573 visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov
), &info_spec
, NULL
,
575 obj
= qmp_output_get_qobject(ov
);
576 assert(qobject_type(obj
) == QTYPE_QDICT
);
577 data
= qdict_get(qobject_to_qdict(obj
), "data");
578 dump_qobject(func_fprintf
, f
, 1, data
);
579 qmp_output_visitor_cleanup(ov
);
582 void bdrv_image_info_dump(fprintf_function func_fprintf
, void *f
,
585 char size_buf
[128], dsize_buf
[128];
586 if (!info
->has_actual_size
) {
587 snprintf(dsize_buf
, sizeof(dsize_buf
), "unavailable");
589 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
592 get_human_readable_size(size_buf
, sizeof(size_buf
), info
->virtual_size
);
596 "virtual size: %s (%" PRId64
" bytes)\n"
598 info
->filename
, info
->format
, size_buf
,
602 if (info
->has_encrypted
&& info
->encrypted
) {
603 func_fprintf(f
, "encrypted: yes\n");
606 if (info
->has_cluster_size
) {
607 func_fprintf(f
, "cluster_size: %" PRId64
"\n",
611 if (info
->has_dirty_flag
&& info
->dirty_flag
) {
612 func_fprintf(f
, "cleanly shut down: no\n");
615 if (info
->has_backing_filename
) {
616 func_fprintf(f
, "backing file: %s", info
->backing_filename
);
617 if (info
->has_full_backing_filename
) {
618 func_fprintf(f
, " (actual path: %s)", info
->full_backing_filename
);
620 func_fprintf(f
, "\n");
621 if (info
->has_backing_filename_format
) {
622 func_fprintf(f
, "backing file format: %s\n",
623 info
->backing_filename_format
);
627 if (info
->has_snapshots
) {
628 SnapshotInfoList
*elem
;
630 func_fprintf(f
, "Snapshot list:\n");
631 bdrv_snapshot_dump(func_fprintf
, f
, NULL
);
632 func_fprintf(f
, "\n");
634 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
635 * we convert to the block layer's native QEMUSnapshotInfo for now.
637 for (elem
= info
->snapshots
; elem
; elem
= elem
->next
) {
638 QEMUSnapshotInfo sn
= {
639 .vm_state_size
= elem
->value
->vm_state_size
,
640 .date_sec
= elem
->value
->date_sec
,
641 .date_nsec
= elem
->value
->date_nsec
,
642 .vm_clock_nsec
= elem
->value
->vm_clock_sec
* 1000000000ULL +
643 elem
->value
->vm_clock_nsec
,
646 pstrcpy(sn
.id_str
, sizeof(sn
.id_str
), elem
->value
->id
);
647 pstrcpy(sn
.name
, sizeof(sn
.name
), elem
->value
->name
);
648 bdrv_snapshot_dump(func_fprintf
, f
, &sn
);
649 func_fprintf(f
, "\n");
653 if (info
->has_format_specific
) {
654 func_fprintf(f
, "Format specific information:\n");
655 bdrv_image_info_specific_dump(func_fprintf
, f
, info
->format_specific
);
658 if (info
->has_nocow
&& info
->nocow
) {
659 func_fprintf(f
, "NOCOW flag: set\n");