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"
30 * Returns 0 on success, with *p_list either set to describe snapshot
31 * information, or NULL because there are no snapshots. Returns -errno on
32 * error, with *p_list untouched.
34 int bdrv_query_snapshot_info_list(BlockDriverState
*bs
,
35 SnapshotInfoList
**p_list
,
39 QEMUSnapshotInfo
*sn_tab
= NULL
;
40 SnapshotInfoList
*info_list
, *cur_item
= NULL
, *head
= NULL
;
43 sn_count
= bdrv_snapshot_list(bs
, &sn_tab
);
45 const char *dev
= bdrv_get_device_name(bs
);
48 error_setg(errp
, "Device '%s' is not inserted", dev
);
52 "Device '%s' does not support internal snapshots",
56 error_setg_errno(errp
, -sn_count
,
57 "Can't list snapshots of device '%s'", dev
);
63 for (i
= 0; i
< sn_count
; i
++) {
64 info
= g_new0(SnapshotInfo
, 1);
65 info
->id
= g_strdup(sn_tab
[i
].id_str
);
66 info
->name
= g_strdup(sn_tab
[i
].name
);
67 info
->vm_state_size
= sn_tab
[i
].vm_state_size
;
68 info
->date_sec
= sn_tab
[i
].date_sec
;
69 info
->date_nsec
= sn_tab
[i
].date_nsec
;
70 info
->vm_clock_sec
= sn_tab
[i
].vm_clock_nsec
/ 1000000000;
71 info
->vm_clock_nsec
= sn_tab
[i
].vm_clock_nsec
% 1000000000;
73 info_list
= g_new0(SnapshotInfoList
, 1);
74 info_list
->value
= info
;
76 /* XXX: waiting for the qapi to support qemu-queue.h types */
78 head
= cur_item
= info_list
;
80 cur_item
->next
= info_list
;
92 * bdrv_query_image_info:
93 * @bs: block device to examine
94 * @p_info: location to store image information
95 * @errp: location to store error information
97 * Store "flat" image information in @p_info.
99 * "Flat" means it does *not* query backing image information,
100 * i.e. (*pinfo)->has_backing_image will be set to false and
101 * (*pinfo)->backing_image to NULL even when the image does in fact have
104 * @p_info will be set only on success. On error, store error in @errp.
106 void bdrv_query_image_info(BlockDriverState
*bs
,
110 uint64_t total_sectors
;
111 const char *backing_filename
;
112 char backing_filename2
[1024];
116 ImageInfo
*info
= g_new0(ImageInfo
, 1);
118 bdrv_get_geometry(bs
, &total_sectors
);
120 info
->filename
= g_strdup(bs
->filename
);
121 info
->format
= g_strdup(bdrv_get_format_name(bs
));
122 info
->virtual_size
= total_sectors
* 512;
123 info
->actual_size
= bdrv_get_allocated_file_size(bs
);
124 info
->has_actual_size
= info
->actual_size
>= 0;
125 if (bdrv_is_encrypted(bs
)) {
126 info
->encrypted
= true;
127 info
->has_encrypted
= true;
129 if (bdrv_get_info(bs
, &bdi
) >= 0) {
130 if (bdi
.cluster_size
!= 0) {
131 info
->cluster_size
= bdi
.cluster_size
;
132 info
->has_cluster_size
= true;
134 info
->dirty_flag
= bdi
.is_dirty
;
135 info
->has_dirty_flag
= true;
137 info
->format_specific
= bdrv_get_specific_info(bs
);
138 info
->has_format_specific
= info
->format_specific
!= NULL
;
140 backing_filename
= bs
->backing_file
;
141 if (backing_filename
[0] != '\0') {
142 info
->backing_filename
= g_strdup(backing_filename
);
143 info
->has_backing_filename
= true;
144 bdrv_get_full_backing_filename(bs
, backing_filename2
,
145 sizeof(backing_filename2
));
147 if (strcmp(backing_filename
, backing_filename2
) != 0) {
148 info
->full_backing_filename
=
149 g_strdup(backing_filename2
);
150 info
->has_full_backing_filename
= true;
153 if (bs
->backing_format
[0]) {
154 info
->backing_filename_format
= g_strdup(bs
->backing_format
);
155 info
->has_backing_filename_format
= true;
159 ret
= bdrv_query_snapshot_info_list(bs
, &info
->snapshots
, &err
);
162 if (info
->snapshots
) {
163 info
->has_snapshots
= true;
166 /* recoverable error */
172 error_propagate(errp
, err
);
173 qapi_free_ImageInfo(info
);
180 /* @p_info will be set only on success. */
181 void bdrv_query_info(BlockDriverState
*bs
,
185 BlockInfo
*info
= g_malloc0(sizeof(*info
));
186 BlockDriverState
*bs0
;
187 ImageInfo
**p_image_info
;
188 Error
*local_err
= NULL
;
189 info
->device
= g_strdup(bs
->device_name
);
190 info
->type
= g_strdup("unknown");
191 info
->locked
= bdrv_dev_is_medium_locked(bs
);
192 info
->removable
= bdrv_dev_has_removable_media(bs
);
194 if (bdrv_dev_has_removable_media(bs
)) {
195 info
->has_tray_open
= true;
196 info
->tray_open
= bdrv_dev_is_tray_open(bs
);
199 if (bdrv_iostatus_is_enabled(bs
)) {
200 info
->has_io_status
= true;
201 info
->io_status
= bs
->iostatus
;
204 if (bs
->dirty_bitmap
) {
205 info
->has_dirty
= true;
206 info
->dirty
= g_malloc0(sizeof(*info
->dirty
));
207 info
->dirty
->count
= bdrv_get_dirty_count(bs
) * BDRV_SECTOR_SIZE
;
208 info
->dirty
->granularity
=
209 ((int64_t) BDRV_SECTOR_SIZE
<< hbitmap_granularity(bs
->dirty_bitmap
));
213 info
->has_inserted
= true;
214 info
->inserted
= g_malloc0(sizeof(*info
->inserted
));
215 info
->inserted
->file
= g_strdup(bs
->filename
);
216 info
->inserted
->ro
= bs
->read_only
;
217 info
->inserted
->drv
= g_strdup(bs
->drv
->format_name
);
218 info
->inserted
->encrypted
= bs
->encrypted
;
219 info
->inserted
->encryption_key_missing
= bdrv_key_required(bs
);
221 if (bs
->backing_file
[0]) {
222 info
->inserted
->has_backing_file
= true;
223 info
->inserted
->backing_file
= g_strdup(bs
->backing_file
);
226 info
->inserted
->backing_file_depth
= bdrv_get_backing_file_depth(bs
);
228 if (bs
->io_limits_enabled
) {
230 throttle_get_config(&bs
->throttle_state
, &cfg
);
231 info
->inserted
->bps
= cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
;
232 info
->inserted
->bps_rd
= cfg
.buckets
[THROTTLE_BPS_READ
].avg
;
233 info
->inserted
->bps_wr
= cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
;
235 info
->inserted
->iops
= cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
;
236 info
->inserted
->iops_rd
= cfg
.buckets
[THROTTLE_OPS_READ
].avg
;
237 info
->inserted
->iops_wr
= cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
;
239 info
->inserted
->has_bps_max
=
240 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
;
241 info
->inserted
->bps_max
=
242 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
;
243 info
->inserted
->has_bps_rd_max
=
244 cfg
.buckets
[THROTTLE_BPS_READ
].max
;
245 info
->inserted
->bps_rd_max
=
246 cfg
.buckets
[THROTTLE_BPS_READ
].max
;
247 info
->inserted
->has_bps_wr_max
=
248 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
;
249 info
->inserted
->bps_wr_max
=
250 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
;
252 info
->inserted
->has_iops_max
=
253 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
;
254 info
->inserted
->iops_max
=
255 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
;
256 info
->inserted
->has_iops_rd_max
=
257 cfg
.buckets
[THROTTLE_OPS_READ
].max
;
258 info
->inserted
->iops_rd_max
=
259 cfg
.buckets
[THROTTLE_OPS_READ
].max
;
260 info
->inserted
->has_iops_wr_max
=
261 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
;
262 info
->inserted
->iops_wr_max
=
263 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
;
265 info
->inserted
->has_iops_size
= cfg
.op_size
;
266 info
->inserted
->iops_size
= cfg
.op_size
;
270 p_image_info
= &info
->inserted
->image
;
272 bdrv_query_image_info(bs0
, p_image_info
, &local_err
);
273 if (error_is_set(&local_err
)) {
274 error_propagate(errp
, local_err
);
277 if (bs0
->drv
&& bs0
->backing_hd
) {
278 bs0
= bs0
->backing_hd
;
279 (*p_image_info
)->has_backing_image
= true;
280 p_image_info
= &((*p_image_info
)->backing_image
);
291 qapi_free_BlockInfo(info
);
294 BlockStats
*bdrv_query_stats(const BlockDriverState
*bs
)
298 s
= g_malloc0(sizeof(*s
));
300 if (bs
->device_name
[0]) {
301 s
->has_device
= true;
302 s
->device
= g_strdup(bs
->device_name
);
305 s
->stats
= g_malloc0(sizeof(*s
->stats
));
306 s
->stats
->rd_bytes
= bs
->nr_bytes
[BDRV_ACCT_READ
];
307 s
->stats
->wr_bytes
= bs
->nr_bytes
[BDRV_ACCT_WRITE
];
308 s
->stats
->rd_operations
= bs
->nr_ops
[BDRV_ACCT_READ
];
309 s
->stats
->wr_operations
= bs
->nr_ops
[BDRV_ACCT_WRITE
];
310 s
->stats
->wr_highest_offset
= bs
->wr_highest_sector
* BDRV_SECTOR_SIZE
;
311 s
->stats
->flush_operations
= bs
->nr_ops
[BDRV_ACCT_FLUSH
];
312 s
->stats
->wr_total_time_ns
= bs
->total_time_ns
[BDRV_ACCT_WRITE
];
313 s
->stats
->rd_total_time_ns
= bs
->total_time_ns
[BDRV_ACCT_READ
];
314 s
->stats
->flush_total_time_ns
= bs
->total_time_ns
[BDRV_ACCT_FLUSH
];
317 s
->has_parent
= true;
318 s
->parent
= bdrv_query_stats(bs
->file
);
324 BlockInfoList
*qmp_query_block(Error
**errp
)
326 BlockInfoList
*head
= NULL
, **p_next
= &head
;
327 BlockDriverState
*bs
= NULL
;
328 Error
*local_err
= NULL
;
330 while ((bs
= bdrv_next(bs
))) {
331 BlockInfoList
*info
= g_malloc0(sizeof(*info
));
332 bdrv_query_info(bs
, &info
->value
, &local_err
);
333 if (error_is_set(&local_err
)) {
334 error_propagate(errp
, local_err
);
339 p_next
= &info
->next
;
345 qapi_free_BlockInfoList(head
);
349 BlockStatsList
*qmp_query_blockstats(Error
**errp
)
351 BlockStatsList
*head
= NULL
, **p_next
= &head
;
352 BlockDriverState
*bs
= NULL
;
354 while ((bs
= bdrv_next(bs
))) {
355 BlockStatsList
*info
= g_malloc0(sizeof(*info
));
356 info
->value
= bdrv_query_stats(bs
);
359 p_next
= &info
->next
;
365 #define NB_SUFFIXES 4
367 static char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
369 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
374 snprintf(buf
, buf_size
, "%" PRId64
, size
);
377 for (i
= 0; i
< NB_SUFFIXES
; i
++) {
378 if (size
< (10 * base
)) {
379 snprintf(buf
, buf_size
, "%0.1f%c",
383 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
384 snprintf(buf
, buf_size
, "%" PRId64
"%c",
385 ((size
+ (base
>> 1)) / base
),
395 void bdrv_snapshot_dump(fprintf_function func_fprintf
, void *f
,
396 QEMUSnapshotInfo
*sn
)
398 char buf1
[128], date_buf
[128], clock_buf
[128];
405 "%-10s%-20s%7s%20s%15s",
406 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
409 localtime_r(&ti
, &tm
);
410 strftime(date_buf
, sizeof(date_buf
),
411 "%Y-%m-%d %H:%M:%S", &tm
);
412 secs
= sn
->vm_clock_nsec
/ 1000000000;
413 snprintf(clock_buf
, sizeof(clock_buf
),
414 "%02d:%02d:%02d.%03d",
416 (int)((secs
/ 60) % 60),
418 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
420 "%-10s%-20s%7s%20s%15s",
421 sn
->id_str
, sn
->name
,
422 get_human_readable_size(buf1
, sizeof(buf1
),
429 void bdrv_image_info_dump(fprintf_function func_fprintf
, void *f
,
432 char size_buf
[128], dsize_buf
[128];
433 if (!info
->has_actual_size
) {
434 snprintf(dsize_buf
, sizeof(dsize_buf
), "unavailable");
436 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
439 get_human_readable_size(size_buf
, sizeof(size_buf
), info
->virtual_size
);
443 "virtual size: %s (%" PRId64
" bytes)\n"
445 info
->filename
, info
->format
, size_buf
,
449 if (info
->has_encrypted
&& info
->encrypted
) {
450 func_fprintf(f
, "encrypted: yes\n");
453 if (info
->has_cluster_size
) {
454 func_fprintf(f
, "cluster_size: %" PRId64
"\n",
458 if (info
->has_dirty_flag
&& info
->dirty_flag
) {
459 func_fprintf(f
, "cleanly shut down: no\n");
462 if (info
->has_backing_filename
) {
463 func_fprintf(f
, "backing file: %s", info
->backing_filename
);
464 if (info
->has_full_backing_filename
) {
465 func_fprintf(f
, " (actual path: %s)", info
->full_backing_filename
);
467 func_fprintf(f
, "\n");
468 if (info
->has_backing_filename_format
) {
469 func_fprintf(f
, "backing file format: %s\n",
470 info
->backing_filename_format
);
474 if (info
->has_snapshots
) {
475 SnapshotInfoList
*elem
;
477 func_fprintf(f
, "Snapshot list:\n");
478 bdrv_snapshot_dump(func_fprintf
, f
, NULL
);
479 func_fprintf(f
, "\n");
481 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
482 * we convert to the block layer's native QEMUSnapshotInfo for now.
484 for (elem
= info
->snapshots
; elem
; elem
= elem
->next
) {
485 QEMUSnapshotInfo sn
= {
486 .vm_state_size
= elem
->value
->vm_state_size
,
487 .date_sec
= elem
->value
->date_sec
,
488 .date_nsec
= elem
->value
->date_nsec
,
489 .vm_clock_nsec
= elem
->value
->vm_clock_sec
* 1000000000ULL +
490 elem
->value
->vm_clock_nsec
,
493 pstrcpy(sn
.id_str
, sizeof(sn
.id_str
), elem
->value
->id
);
494 pstrcpy(sn
.name
, sizeof(sn
.name
), elem
->value
->name
);
495 bdrv_snapshot_dump(func_fprintf
, f
, &sn
);
496 func_fprintf(f
, "\n");