2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 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
24 #include "config-host.h"
25 #include "qemu-common.h"
28 #include "block_int.h"
30 #include "qemu-objects.h"
31 #include "qemu-coroutine.h"
32 #include "qmp-commands.h"
35 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/queue.h>
48 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
50 static void bdrv_dev_change_media_cb(BlockDriverState
*bs
, bool load
);
51 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
52 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
53 BlockDriverCompletionFunc
*cb
, void *opaque
);
54 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
55 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
56 BlockDriverCompletionFunc
*cb
, void *opaque
);
57 static int coroutine_fn
bdrv_co_readv_em(BlockDriverState
*bs
,
58 int64_t sector_num
, int nb_sectors
,
60 static int coroutine_fn
bdrv_co_writev_em(BlockDriverState
*bs
,
61 int64_t sector_num
, int nb_sectors
,
63 static int coroutine_fn
bdrv_co_do_readv(BlockDriverState
*bs
,
64 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
);
65 static int coroutine_fn
bdrv_co_do_writev(BlockDriverState
*bs
,
66 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
);
67 static BlockDriverAIOCB
*bdrv_co_aio_rw_vector(BlockDriverState
*bs
,
71 BlockDriverCompletionFunc
*cb
,
74 static void coroutine_fn
bdrv_co_do_rw(void *opaque
);
76 static QTAILQ_HEAD(, BlockDriverState
) bdrv_states
=
77 QTAILQ_HEAD_INITIALIZER(bdrv_states
);
79 static QLIST_HEAD(, BlockDriver
) bdrv_drivers
=
80 QLIST_HEAD_INITIALIZER(bdrv_drivers
);
82 /* The device to use for VM snapshots */
83 static BlockDriverState
*bs_snapshots
;
85 /* If non-zero, use only whitelisted block drivers */
86 static int use_bdrv_whitelist
;
89 static int is_windows_drive_prefix(const char *filename
)
91 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
92 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
96 int is_windows_drive(const char *filename
)
98 if (is_windows_drive_prefix(filename
) &&
101 if (strstart(filename
, "\\\\.\\", NULL
) ||
102 strstart(filename
, "//./", NULL
))
108 /* check if the path starts with "<protocol>:" */
109 static int path_has_protocol(const char *path
)
112 if (is_windows_drive(path
) ||
113 is_windows_drive_prefix(path
)) {
118 return strchr(path
, ':') != NULL
;
121 int path_is_absolute(const char *path
)
125 /* specific case for names like: "\\.\d:" */
126 if (*path
== '/' || *path
== '\\')
129 p
= strchr(path
, ':');
135 return (*p
== '/' || *p
== '\\');
141 /* if filename is absolute, just copy it to dest. Otherwise, build a
142 path to it by considering it is relative to base_path. URL are
144 void path_combine(char *dest
, int dest_size
,
145 const char *base_path
,
146 const char *filename
)
153 if (path_is_absolute(filename
)) {
154 pstrcpy(dest
, dest_size
, filename
);
156 p
= strchr(base_path
, ':');
161 p1
= strrchr(base_path
, '/');
165 p2
= strrchr(base_path
, '\\');
177 if (len
> dest_size
- 1)
179 memcpy(dest
, base_path
, len
);
181 pstrcat(dest
, dest_size
, filename
);
185 void bdrv_register(BlockDriver
*bdrv
)
187 /* Block drivers without coroutine functions need emulation */
188 if (!bdrv
->bdrv_co_readv
) {
189 bdrv
->bdrv_co_readv
= bdrv_co_readv_em
;
190 bdrv
->bdrv_co_writev
= bdrv_co_writev_em
;
192 /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
193 * the block driver lacks aio we need to emulate that too.
195 if (!bdrv
->bdrv_aio_readv
) {
196 /* add AIO emulation layer */
197 bdrv
->bdrv_aio_readv
= bdrv_aio_readv_em
;
198 bdrv
->bdrv_aio_writev
= bdrv_aio_writev_em
;
202 QLIST_INSERT_HEAD(&bdrv_drivers
, bdrv
, list
);
205 /* create a new block device (by default it is empty) */
206 BlockDriverState
*bdrv_new(const char *device_name
)
208 BlockDriverState
*bs
;
210 bs
= g_malloc0(sizeof(BlockDriverState
));
211 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
212 if (device_name
[0] != '\0') {
213 QTAILQ_INSERT_TAIL(&bdrv_states
, bs
, list
);
215 bdrv_iostatus_disable(bs
);
219 BlockDriver
*bdrv_find_format(const char *format_name
)
222 QLIST_FOREACH(drv1
, &bdrv_drivers
, list
) {
223 if (!strcmp(drv1
->format_name
, format_name
)) {
230 static int bdrv_is_whitelisted(BlockDriver
*drv
)
232 static const char *whitelist
[] = {
233 CONFIG_BDRV_WHITELIST
238 return 1; /* no whitelist, anything goes */
240 for (p
= whitelist
; *p
; p
++) {
241 if (!strcmp(drv
->format_name
, *p
)) {
248 BlockDriver
*bdrv_find_whitelisted_format(const char *format_name
)
250 BlockDriver
*drv
= bdrv_find_format(format_name
);
251 return drv
&& bdrv_is_whitelisted(drv
) ? drv
: NULL
;
254 int bdrv_create(BlockDriver
*drv
, const char* filename
,
255 QEMUOptionParameter
*options
)
257 if (!drv
->bdrv_create
)
260 return drv
->bdrv_create(filename
, options
);
263 int bdrv_create_file(const char* filename
, QEMUOptionParameter
*options
)
267 drv
= bdrv_find_protocol(filename
);
272 return bdrv_create(drv
, filename
, options
);
276 void get_tmp_filename(char *filename
, int size
)
278 char temp_dir
[MAX_PATH
];
280 GetTempPath(MAX_PATH
, temp_dir
);
281 GetTempFileName(temp_dir
, "qem", 0, filename
);
284 void get_tmp_filename(char *filename
, int size
)
288 /* XXX: race condition possible */
289 tmpdir
= getenv("TMPDIR");
292 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
293 fd
= mkstemp(filename
);
299 * Detect host devices. By convention, /dev/cdrom[N] is always
300 * recognized as a host CDROM.
302 static BlockDriver
*find_hdev_driver(const char *filename
)
304 int score_max
= 0, score
;
305 BlockDriver
*drv
= NULL
, *d
;
307 QLIST_FOREACH(d
, &bdrv_drivers
, list
) {
308 if (d
->bdrv_probe_device
) {
309 score
= d
->bdrv_probe_device(filename
);
310 if (score
> score_max
) {
320 BlockDriver
*bdrv_find_protocol(const char *filename
)
327 /* TODO Drivers without bdrv_file_open must be specified explicitly */
330 * XXX(hch): we really should not let host device detection
331 * override an explicit protocol specification, but moving this
332 * later breaks access to device names with colons in them.
333 * Thanks to the brain-dead persistent naming schemes on udev-
334 * based Linux systems those actually are quite common.
336 drv1
= find_hdev_driver(filename
);
341 if (!path_has_protocol(filename
)) {
342 return bdrv_find_format("file");
344 p
= strchr(filename
, ':');
347 if (len
> sizeof(protocol
) - 1)
348 len
= sizeof(protocol
) - 1;
349 memcpy(protocol
, filename
, len
);
350 protocol
[len
] = '\0';
351 QLIST_FOREACH(drv1
, &bdrv_drivers
, list
) {
352 if (drv1
->protocol_name
&&
353 !strcmp(drv1
->protocol_name
, protocol
)) {
360 static int find_image_format(const char *filename
, BlockDriver
**pdrv
)
362 int ret
, score
, score_max
;
363 BlockDriver
*drv1
, *drv
;
365 BlockDriverState
*bs
;
367 ret
= bdrv_file_open(&bs
, filename
, 0);
373 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
374 if (bs
->sg
|| !bdrv_is_inserted(bs
)) {
376 drv
= bdrv_find_format("raw");
384 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
393 QLIST_FOREACH(drv1
, &bdrv_drivers
, list
) {
394 if (drv1
->bdrv_probe
) {
395 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
396 if (score
> score_max
) {
410 * Set the current 'total_sectors' value
412 static int refresh_total_sectors(BlockDriverState
*bs
, int64_t hint
)
414 BlockDriver
*drv
= bs
->drv
;
416 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
420 /* query actual device if possible, otherwise just trust the hint */
421 if (drv
->bdrv_getlength
) {
422 int64_t length
= drv
->bdrv_getlength(bs
);
426 hint
= length
>> BDRV_SECTOR_BITS
;
429 bs
->total_sectors
= hint
;
434 * Set open flags for a given cache mode
436 * Return 0 on success, -1 if the cache mode was invalid.
438 int bdrv_parse_cache_flags(const char *mode
, int *flags
)
440 *flags
&= ~BDRV_O_CACHE_MASK
;
442 if (!strcmp(mode
, "off") || !strcmp(mode
, "none")) {
443 *flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
444 } else if (!strcmp(mode
, "directsync")) {
445 *flags
|= BDRV_O_NOCACHE
;
446 } else if (!strcmp(mode
, "writeback")) {
447 *flags
|= BDRV_O_CACHE_WB
;
448 } else if (!strcmp(mode
, "unsafe")) {
449 *flags
|= BDRV_O_CACHE_WB
;
450 *flags
|= BDRV_O_NO_FLUSH
;
451 } else if (!strcmp(mode
, "writethrough")) {
452 /* this is the default */
461 * Common part for opening disk images and files
463 static int bdrv_open_common(BlockDriverState
*bs
, const char *filename
,
464 int flags
, BlockDriver
*drv
)
470 trace_bdrv_open_common(bs
, filename
, flags
, drv
->format_name
);
473 bs
->total_sectors
= 0;
476 bs
->open_flags
= flags
;
477 bs
->buffer_alignment
= 512;
479 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
481 if (use_bdrv_whitelist
&& !bdrv_is_whitelisted(drv
)) {
486 bs
->opaque
= g_malloc0(drv
->instance_size
);
488 if (flags
& BDRV_O_CACHE_WB
)
489 bs
->enable_write_cache
= 1;
492 * Clear flags that are internal to the block layer before opening the
495 open_flags
= flags
& ~(BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
);
498 * Snapshots should be writable.
500 if (bs
->is_temporary
) {
501 open_flags
|= BDRV_O_RDWR
;
504 /* Open the image, either directly or using a protocol */
505 if (drv
->bdrv_file_open
) {
506 ret
= drv
->bdrv_file_open(bs
, filename
, open_flags
);
508 ret
= bdrv_file_open(&bs
->file
, filename
, open_flags
);
510 ret
= drv
->bdrv_open(bs
, open_flags
);
518 bs
->keep_read_only
= bs
->read_only
= !(open_flags
& BDRV_O_RDWR
);
520 ret
= refresh_total_sectors(bs
, bs
->total_sectors
);
526 if (bs
->is_temporary
) {
534 bdrv_delete(bs
->file
);
544 * Opens a file using a protocol (file, host_device, nbd, ...)
546 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
548 BlockDriverState
*bs
;
552 drv
= bdrv_find_protocol(filename
);
558 ret
= bdrv_open_common(bs
, filename
, flags
, drv
);
569 * Opens a disk image (raw, qcow2, vmdk, ...)
571 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
,
576 if (flags
& BDRV_O_SNAPSHOT
) {
577 BlockDriverState
*bs1
;
580 BlockDriver
*bdrv_qcow2
;
581 QEMUOptionParameter
*options
;
582 char tmp_filename
[PATH_MAX
];
583 char backing_filename
[PATH_MAX
];
585 /* if snapshot, we create a temporary backing file and open it
586 instead of opening 'filename' directly */
588 /* if there is a backing file, use it */
590 ret
= bdrv_open(bs1
, filename
, 0, drv
);
595 total_size
= bdrv_getlength(bs1
) & BDRV_SECTOR_MASK
;
597 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
602 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
604 /* Real path is meaningless for protocols */
606 snprintf(backing_filename
, sizeof(backing_filename
),
608 else if (!realpath(filename
, backing_filename
))
611 bdrv_qcow2
= bdrv_find_format("qcow2");
612 options
= parse_option_parameters("", bdrv_qcow2
->create_options
, NULL
);
614 set_option_parameter_int(options
, BLOCK_OPT_SIZE
, total_size
);
615 set_option_parameter(options
, BLOCK_OPT_BACKING_FILE
, backing_filename
);
617 set_option_parameter(options
, BLOCK_OPT_BACKING_FMT
,
621 ret
= bdrv_create(bdrv_qcow2
, tmp_filename
, options
);
622 free_option_parameters(options
);
627 filename
= tmp_filename
;
629 bs
->is_temporary
= 1;
632 /* Find the right image format driver */
634 ret
= find_image_format(filename
, &drv
);
638 goto unlink_and_fail
;
642 ret
= bdrv_open_common(bs
, filename
, flags
, drv
);
644 goto unlink_and_fail
;
647 /* If there is a backing file, use it */
648 if ((flags
& BDRV_O_NO_BACKING
) == 0 && bs
->backing_file
[0] != '\0') {
649 char backing_filename
[PATH_MAX
];
651 BlockDriver
*back_drv
= NULL
;
653 bs
->backing_hd
= bdrv_new("");
655 if (path_has_protocol(bs
->backing_file
)) {
656 pstrcpy(backing_filename
, sizeof(backing_filename
),
659 path_combine(backing_filename
, sizeof(backing_filename
),
660 filename
, bs
->backing_file
);
663 if (bs
->backing_format
[0] != '\0') {
664 back_drv
= bdrv_find_format(bs
->backing_format
);
667 /* backing files always opened read-only */
669 flags
& ~(BDRV_O_RDWR
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
);
671 ret
= bdrv_open(bs
->backing_hd
, backing_filename
, back_flags
, back_drv
);
676 if (bs
->is_temporary
) {
677 bs
->backing_hd
->keep_read_only
= !(flags
& BDRV_O_RDWR
);
679 /* base image inherits from "parent" */
680 bs
->backing_hd
->keep_read_only
= bs
->keep_read_only
;
684 if (!bdrv_key_required(bs
)) {
685 bdrv_dev_change_media_cb(bs
, true);
691 if (bs
->is_temporary
) {
697 void bdrv_close(BlockDriverState
*bs
)
700 if (bs
== bs_snapshots
) {
703 if (bs
->backing_hd
) {
704 bdrv_delete(bs
->backing_hd
);
705 bs
->backing_hd
= NULL
;
707 bs
->drv
->bdrv_close(bs
);
710 if (bs
->is_temporary
) {
711 unlink(bs
->filename
);
717 if (bs
->file
!= NULL
) {
718 bdrv_close(bs
->file
);
721 bdrv_dev_change_media_cb(bs
, false);
725 void bdrv_close_all(void)
727 BlockDriverState
*bs
;
729 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
734 /* make a BlockDriverState anonymous by removing from bdrv_state list.
735 Also, NULL terminate the device_name to prevent double remove */
736 void bdrv_make_anon(BlockDriverState
*bs
)
738 if (bs
->device_name
[0] != '\0') {
739 QTAILQ_REMOVE(&bdrv_states
, bs
, list
);
741 bs
->device_name
[0] = '\0';
744 void bdrv_delete(BlockDriverState
*bs
)
748 /* remove from list, if necessary */
752 if (bs
->file
!= NULL
) {
753 bdrv_delete(bs
->file
);
756 assert(bs
!= bs_snapshots
);
760 int bdrv_attach_dev(BlockDriverState
*bs
, void *dev
)
761 /* TODO change to DeviceState *dev when all users are qdevified */
767 bdrv_iostatus_reset(bs
);
771 /* TODO qdevified devices don't use this, remove when devices are qdevified */
772 void bdrv_attach_dev_nofail(BlockDriverState
*bs
, void *dev
)
774 if (bdrv_attach_dev(bs
, dev
) < 0) {
779 void bdrv_detach_dev(BlockDriverState
*bs
, void *dev
)
780 /* TODO change to DeviceState *dev when all users are qdevified */
782 assert(bs
->dev
== dev
);
785 bs
->dev_opaque
= NULL
;
786 bs
->buffer_alignment
= 512;
789 /* TODO change to return DeviceState * when all users are qdevified */
790 void *bdrv_get_attached_dev(BlockDriverState
*bs
)
795 void bdrv_set_dev_ops(BlockDriverState
*bs
, const BlockDevOps
*ops
,
799 bs
->dev_opaque
= opaque
;
800 if (bdrv_dev_has_removable_media(bs
) && bs
== bs_snapshots
) {
805 static void bdrv_dev_change_media_cb(BlockDriverState
*bs
, bool load
)
807 if (bs
->dev_ops
&& bs
->dev_ops
->change_media_cb
) {
808 bs
->dev_ops
->change_media_cb(bs
->dev_opaque
, load
);
812 bool bdrv_dev_has_removable_media(BlockDriverState
*bs
)
814 return !bs
->dev
|| (bs
->dev_ops
&& bs
->dev_ops
->change_media_cb
);
817 bool bdrv_dev_is_tray_open(BlockDriverState
*bs
)
819 if (bs
->dev_ops
&& bs
->dev_ops
->is_tray_open
) {
820 return bs
->dev_ops
->is_tray_open(bs
->dev_opaque
);
825 static void bdrv_dev_resize_cb(BlockDriverState
*bs
)
827 if (bs
->dev_ops
&& bs
->dev_ops
->resize_cb
) {
828 bs
->dev_ops
->resize_cb(bs
->dev_opaque
);
832 bool bdrv_dev_is_medium_locked(BlockDriverState
*bs
)
834 if (bs
->dev_ops
&& bs
->dev_ops
->is_medium_locked
) {
835 return bs
->dev_ops
->is_medium_locked(bs
->dev_opaque
);
841 * Run consistency checks on an image
843 * Returns 0 if the check could be completed (it doesn't mean that the image is
844 * free of errors) or -errno when an internal error occurred. The results of the
845 * check are stored in res.
847 int bdrv_check(BlockDriverState
*bs
, BdrvCheckResult
*res
)
849 if (bs
->drv
->bdrv_check
== NULL
) {
853 memset(res
, 0, sizeof(*res
));
854 return bs
->drv
->bdrv_check(bs
, res
);
857 #define COMMIT_BUF_SECTORS 2048
859 /* commit COW file into the raw image */
860 int bdrv_commit(BlockDriverState
*bs
)
862 BlockDriver
*drv
= bs
->drv
;
863 BlockDriver
*backing_drv
;
864 int64_t sector
, total_sectors
;
865 int n
, ro
, open_flags
;
866 int ret
= 0, rw_ret
= 0;
869 BlockDriverState
*bs_rw
, *bs_ro
;
874 if (!bs
->backing_hd
) {
878 if (bs
->backing_hd
->keep_read_only
) {
882 backing_drv
= bs
->backing_hd
->drv
;
883 ro
= bs
->backing_hd
->read_only
;
884 strncpy(filename
, bs
->backing_hd
->filename
, sizeof(filename
));
885 open_flags
= bs
->backing_hd
->open_flags
;
889 bdrv_delete(bs
->backing_hd
);
890 bs
->backing_hd
= NULL
;
891 bs_rw
= bdrv_new("");
892 rw_ret
= bdrv_open(bs_rw
, filename
, open_flags
| BDRV_O_RDWR
,
896 /* try to re-open read-only */
897 bs_ro
= bdrv_new("");
898 ret
= bdrv_open(bs_ro
, filename
, open_flags
& ~BDRV_O_RDWR
,
902 /* drive not functional anymore */
906 bs
->backing_hd
= bs_ro
;
909 bs
->backing_hd
= bs_rw
;
912 total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
913 buf
= g_malloc(COMMIT_BUF_SECTORS
* BDRV_SECTOR_SIZE
);
915 for (sector
= 0; sector
< total_sectors
; sector
+= n
) {
916 if (drv
->bdrv_is_allocated(bs
, sector
, COMMIT_BUF_SECTORS
, &n
)) {
918 if (bdrv_read(bs
, sector
, buf
, n
) != 0) {
923 if (bdrv_write(bs
->backing_hd
, sector
, buf
, n
) != 0) {
930 if (drv
->bdrv_make_empty
) {
931 ret
= drv
->bdrv_make_empty(bs
);
936 * Make sure all data we wrote to the backing device is actually
940 bdrv_flush(bs
->backing_hd
);
947 bdrv_delete(bs
->backing_hd
);
948 bs
->backing_hd
= NULL
;
949 bs_ro
= bdrv_new("");
950 ret
= bdrv_open(bs_ro
, filename
, open_flags
& ~BDRV_O_RDWR
,
954 /* drive not functional anymore */
958 bs
->backing_hd
= bs_ro
;
959 bs
->backing_hd
->keep_read_only
= 0;
965 void bdrv_commit_all(void)
967 BlockDriverState
*bs
;
969 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
977 * -EINVAL - backing format specified, but no file
978 * -ENOSPC - can't update the backing file because no space is left in the
980 * -ENOTSUP - format driver doesn't support changing the backing file
982 int bdrv_change_backing_file(BlockDriverState
*bs
,
983 const char *backing_file
, const char *backing_fmt
)
985 BlockDriver
*drv
= bs
->drv
;
987 if (drv
->bdrv_change_backing_file
!= NULL
) {
988 return drv
->bdrv_change_backing_file(bs
, backing_file
, backing_fmt
);
994 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
999 if (!bdrv_is_inserted(bs
))
1005 len
= bdrv_getlength(bs
);
1010 if ((offset
> len
) || (len
- offset
< size
))
1016 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
1019 return bdrv_check_byte_request(bs
, sector_num
* BDRV_SECTOR_SIZE
,
1020 nb_sectors
* BDRV_SECTOR_SIZE
);
1023 typedef struct RwCo
{
1024 BlockDriverState
*bs
;
1032 static void coroutine_fn
bdrv_rw_co_entry(void *opaque
)
1034 RwCo
*rwco
= opaque
;
1036 if (!rwco
->is_write
) {
1037 rwco
->ret
= bdrv_co_do_readv(rwco
->bs
, rwco
->sector_num
,
1038 rwco
->nb_sectors
, rwco
->qiov
);
1040 rwco
->ret
= bdrv_co_do_writev(rwco
->bs
, rwco
->sector_num
,
1041 rwco
->nb_sectors
, rwco
->qiov
);
1046 * Process a synchronous request using coroutines
1048 static int bdrv_rw_co(BlockDriverState
*bs
, int64_t sector_num
, uint8_t *buf
,
1049 int nb_sectors
, bool is_write
)
1052 struct iovec iov
= {
1053 .iov_base
= (void *)buf
,
1054 .iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
,
1059 .sector_num
= sector_num
,
1060 .nb_sectors
= nb_sectors
,
1062 .is_write
= is_write
,
1066 qemu_iovec_init_external(&qiov
, &iov
, 1);
1068 if (qemu_in_coroutine()) {
1069 /* Fast-path if already in coroutine context */
1070 bdrv_rw_co_entry(&rwco
);
1072 co
= qemu_coroutine_create(bdrv_rw_co_entry
);
1073 qemu_coroutine_enter(co
, &rwco
);
1074 while (rwco
.ret
== NOT_DONE
) {
1081 /* return < 0 if error. See bdrv_write() for the return codes */
1082 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
1083 uint8_t *buf
, int nb_sectors
)
1085 return bdrv_rw_co(bs
, sector_num
, buf
, nb_sectors
, false);
1088 static void set_dirty_bitmap(BlockDriverState
*bs
, int64_t sector_num
,
1089 int nb_sectors
, int dirty
)
1092 unsigned long val
, idx
, bit
;
1094 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
1095 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
1097 for (; start
<= end
; start
++) {
1098 idx
= start
/ (sizeof(unsigned long) * 8);
1099 bit
= start
% (sizeof(unsigned long) * 8);
1100 val
= bs
->dirty_bitmap
[idx
];
1102 if (!(val
& (1UL << bit
))) {
1107 if (val
& (1UL << bit
)) {
1109 val
&= ~(1UL << bit
);
1112 bs
->dirty_bitmap
[idx
] = val
;
1116 /* Return < 0 if error. Important errors are:
1117 -EIO generic I/O error (may happen for all errors)
1118 -ENOMEDIUM No media inserted.
1119 -EINVAL Invalid sector number or nb_sectors
1120 -EACCES Trying to write a read-only device
1122 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
1123 const uint8_t *buf
, int nb_sectors
)
1125 return bdrv_rw_co(bs
, sector_num
, (uint8_t *)buf
, nb_sectors
, true);
1128 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
1129 void *buf
, int count1
)
1131 uint8_t tmp_buf
[BDRV_SECTOR_SIZE
];
1132 int len
, nb_sectors
, count
;
1137 /* first read to align to sector start */
1138 len
= (BDRV_SECTOR_SIZE
- offset
) & (BDRV_SECTOR_SIZE
- 1);
1141 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1143 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1145 memcpy(buf
, tmp_buf
+ (offset
& (BDRV_SECTOR_SIZE
- 1)), len
);
1153 /* read the sectors "in place" */
1154 nb_sectors
= count
>> BDRV_SECTOR_BITS
;
1155 if (nb_sectors
> 0) {
1156 if ((ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
)) < 0)
1158 sector_num
+= nb_sectors
;
1159 len
= nb_sectors
<< BDRV_SECTOR_BITS
;
1164 /* add data from the last sector */
1166 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1168 memcpy(buf
, tmp_buf
, count
);
1173 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
1174 const void *buf
, int count1
)
1176 uint8_t tmp_buf
[BDRV_SECTOR_SIZE
];
1177 int len
, nb_sectors
, count
;
1182 /* first write to align to sector start */
1183 len
= (BDRV_SECTOR_SIZE
- offset
) & (BDRV_SECTOR_SIZE
- 1);
1186 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1188 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1190 memcpy(tmp_buf
+ (offset
& (BDRV_SECTOR_SIZE
- 1)), buf
, len
);
1191 if ((ret
= bdrv_write(bs
, sector_num
, tmp_buf
, 1)) < 0)
1200 /* write the sectors "in place" */
1201 nb_sectors
= count
>> BDRV_SECTOR_BITS
;
1202 if (nb_sectors
> 0) {
1203 if ((ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
)) < 0)
1205 sector_num
+= nb_sectors
;
1206 len
= nb_sectors
<< BDRV_SECTOR_BITS
;
1211 /* add data from the last sector */
1213 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1215 memcpy(tmp_buf
, buf
, count
);
1216 if ((ret
= bdrv_write(bs
, sector_num
, tmp_buf
, 1)) < 0)
1223 * Writes to the file and ensures that no writes are reordered across this
1224 * request (acts as a barrier)
1226 * Returns 0 on success, -errno in error cases.
1228 int bdrv_pwrite_sync(BlockDriverState
*bs
, int64_t offset
,
1229 const void *buf
, int count
)
1233 ret
= bdrv_pwrite(bs
, offset
, buf
, count
);
1238 /* No flush needed for cache modes that use O_DSYNC */
1239 if ((bs
->open_flags
& BDRV_O_CACHE_WB
) != 0) {
1247 * Handle a read request in coroutine context
1249 static int coroutine_fn
bdrv_co_do_readv(BlockDriverState
*bs
,
1250 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
1252 BlockDriver
*drv
= bs
->drv
;
1257 if (bdrv_check_request(bs
, sector_num
, nb_sectors
)) {
1261 return drv
->bdrv_co_readv(bs
, sector_num
, nb_sectors
, qiov
);
1264 int coroutine_fn
bdrv_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
1265 int nb_sectors
, QEMUIOVector
*qiov
)
1267 trace_bdrv_co_readv(bs
, sector_num
, nb_sectors
);
1269 return bdrv_co_do_readv(bs
, sector_num
, nb_sectors
, qiov
);
1273 * Handle a write request in coroutine context
1275 static int coroutine_fn
bdrv_co_do_writev(BlockDriverState
*bs
,
1276 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
1278 BlockDriver
*drv
= bs
->drv
;
1284 if (bs
->read_only
) {
1287 if (bdrv_check_request(bs
, sector_num
, nb_sectors
)) {
1291 ret
= drv
->bdrv_co_writev(bs
, sector_num
, nb_sectors
, qiov
);
1293 if (bs
->dirty_bitmap
) {
1294 set_dirty_bitmap(bs
, sector_num
, nb_sectors
, 1);
1297 if (bs
->wr_highest_sector
< sector_num
+ nb_sectors
- 1) {
1298 bs
->wr_highest_sector
= sector_num
+ nb_sectors
- 1;
1304 int coroutine_fn
bdrv_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
1305 int nb_sectors
, QEMUIOVector
*qiov
)
1307 trace_bdrv_co_writev(bs
, sector_num
, nb_sectors
);
1309 return bdrv_co_do_writev(bs
, sector_num
, nb_sectors
, qiov
);
1313 * Truncate file to 'offset' bytes (needed only for file protocols)
1315 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
1317 BlockDriver
*drv
= bs
->drv
;
1321 if (!drv
->bdrv_truncate
)
1325 if (bdrv_in_use(bs
))
1327 ret
= drv
->bdrv_truncate(bs
, offset
);
1329 ret
= refresh_total_sectors(bs
, offset
>> BDRV_SECTOR_BITS
);
1330 bdrv_dev_resize_cb(bs
);
1336 * Length of a allocated file in bytes. Sparse files are counted by actual
1337 * allocated space. Return < 0 if error or unknown.
1339 int64_t bdrv_get_allocated_file_size(BlockDriverState
*bs
)
1341 BlockDriver
*drv
= bs
->drv
;
1345 if (drv
->bdrv_get_allocated_file_size
) {
1346 return drv
->bdrv_get_allocated_file_size(bs
);
1349 return bdrv_get_allocated_file_size(bs
->file
);
1355 * Length of a file in bytes. Return < 0 if error or unknown.
1357 int64_t bdrv_getlength(BlockDriverState
*bs
)
1359 BlockDriver
*drv
= bs
->drv
;
1363 if (bs
->growable
|| bdrv_dev_has_removable_media(bs
)) {
1364 if (drv
->bdrv_getlength
) {
1365 return drv
->bdrv_getlength(bs
);
1368 return bs
->total_sectors
* BDRV_SECTOR_SIZE
;
1371 /* return 0 as number of sectors if no device present or error */
1372 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
1375 length
= bdrv_getlength(bs
);
1379 length
= length
>> BDRV_SECTOR_BITS
;
1380 *nb_sectors_ptr
= length
;
1384 uint8_t boot_ind
; /* 0x80 - active */
1385 uint8_t head
; /* starting head */
1386 uint8_t sector
; /* starting sector */
1387 uint8_t cyl
; /* starting cylinder */
1388 uint8_t sys_ind
; /* What partition type */
1389 uint8_t end_head
; /* end head */
1390 uint8_t end_sector
; /* end sector */
1391 uint8_t end_cyl
; /* end cylinder */
1392 uint32_t start_sect
; /* starting sector counting from 0 */
1393 uint32_t nr_sects
; /* nr of sectors in partition */
1396 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1397 static int guess_disk_lchs(BlockDriverState
*bs
,
1398 int *pcylinders
, int *pheads
, int *psectors
)
1400 uint8_t buf
[BDRV_SECTOR_SIZE
];
1401 int ret
, i
, heads
, sectors
, cylinders
;
1402 struct partition
*p
;
1404 uint64_t nb_sectors
;
1406 bdrv_get_geometry(bs
, &nb_sectors
);
1408 ret
= bdrv_read(bs
, 0, buf
, 1);
1411 /* test msdos magic */
1412 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
1414 for(i
= 0; i
< 4; i
++) {
1415 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
1416 nr_sects
= le32_to_cpu(p
->nr_sects
);
1417 if (nr_sects
&& p
->end_head
) {
1418 /* We make the assumption that the partition terminates on
1419 a cylinder boundary */
1420 heads
= p
->end_head
+ 1;
1421 sectors
= p
->end_sector
& 63;
1424 cylinders
= nb_sectors
/ (heads
* sectors
);
1425 if (cylinders
< 1 || cylinders
> 16383)
1428 *psectors
= sectors
;
1429 *pcylinders
= cylinders
;
1431 printf("guessed geometry: LCHS=%d %d %d\n",
1432 cylinders
, heads
, sectors
);
1440 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
1442 int translation
, lba_detected
= 0;
1443 int cylinders
, heads
, secs
;
1444 uint64_t nb_sectors
;
1446 /* if a geometry hint is available, use it */
1447 bdrv_get_geometry(bs
, &nb_sectors
);
1448 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
1449 translation
= bdrv_get_translation_hint(bs
);
1450 if (cylinders
!= 0) {
1455 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
1457 /* if heads > 16, it means that a BIOS LBA
1458 translation was active, so the default
1459 hardware geometry is OK */
1461 goto default_geometry
;
1466 /* disable any translation to be in sync with
1467 the logical geometry */
1468 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
1469 bdrv_set_translation_hint(bs
,
1470 BIOS_ATA_TRANSLATION_NONE
);
1475 /* if no geometry, use a standard physical disk geometry */
1476 cylinders
= nb_sectors
/ (16 * 63);
1478 if (cylinders
> 16383)
1480 else if (cylinders
< 2)
1485 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
1486 if ((*pcyls
* *pheads
) <= 131072) {
1487 bdrv_set_translation_hint(bs
,
1488 BIOS_ATA_TRANSLATION_LARGE
);
1490 bdrv_set_translation_hint(bs
,
1491 BIOS_ATA_TRANSLATION_LBA
);
1495 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
1499 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
1500 int cyls
, int heads
, int secs
)
1507 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
1509 bs
->translation
= translation
;
1512 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
1513 int *pcyls
, int *pheads
, int *psecs
)
1516 *pheads
= bs
->heads
;
1520 /* Recognize floppy formats */
1521 typedef struct FDFormat
{
1528 static const FDFormat fd_formats
[] = {
1529 /* First entry is default format */
1530 /* 1.44 MB 3"1/2 floppy disks */
1531 { FDRIVE_DRV_144
, 18, 80, 1, },
1532 { FDRIVE_DRV_144
, 20, 80, 1, },
1533 { FDRIVE_DRV_144
, 21, 80, 1, },
1534 { FDRIVE_DRV_144
, 21, 82, 1, },
1535 { FDRIVE_DRV_144
, 21, 83, 1, },
1536 { FDRIVE_DRV_144
, 22, 80, 1, },
1537 { FDRIVE_DRV_144
, 23, 80, 1, },
1538 { FDRIVE_DRV_144
, 24, 80, 1, },
1539 /* 2.88 MB 3"1/2 floppy disks */
1540 { FDRIVE_DRV_288
, 36, 80, 1, },
1541 { FDRIVE_DRV_288
, 39, 80, 1, },
1542 { FDRIVE_DRV_288
, 40, 80, 1, },
1543 { FDRIVE_DRV_288
, 44, 80, 1, },
1544 { FDRIVE_DRV_288
, 48, 80, 1, },
1545 /* 720 kB 3"1/2 floppy disks */
1546 { FDRIVE_DRV_144
, 9, 80, 1, },
1547 { FDRIVE_DRV_144
, 10, 80, 1, },
1548 { FDRIVE_DRV_144
, 10, 82, 1, },
1549 { FDRIVE_DRV_144
, 10, 83, 1, },
1550 { FDRIVE_DRV_144
, 13, 80, 1, },
1551 { FDRIVE_DRV_144
, 14, 80, 1, },
1552 /* 1.2 MB 5"1/4 floppy disks */
1553 { FDRIVE_DRV_120
, 15, 80, 1, },
1554 { FDRIVE_DRV_120
, 18, 80, 1, },
1555 { FDRIVE_DRV_120
, 18, 82, 1, },
1556 { FDRIVE_DRV_120
, 18, 83, 1, },
1557 { FDRIVE_DRV_120
, 20, 80, 1, },
1558 /* 720 kB 5"1/4 floppy disks */
1559 { FDRIVE_DRV_120
, 9, 80, 1, },
1560 { FDRIVE_DRV_120
, 11, 80, 1, },
1561 /* 360 kB 5"1/4 floppy disks */
1562 { FDRIVE_DRV_120
, 9, 40, 1, },
1563 { FDRIVE_DRV_120
, 9, 40, 0, },
1564 { FDRIVE_DRV_120
, 10, 41, 1, },
1565 { FDRIVE_DRV_120
, 10, 42, 1, },
1566 /* 320 kB 5"1/4 floppy disks */
1567 { FDRIVE_DRV_120
, 8, 40, 1, },
1568 { FDRIVE_DRV_120
, 8, 40, 0, },
1569 /* 360 kB must match 5"1/4 better than 3"1/2... */
1570 { FDRIVE_DRV_144
, 9, 80, 0, },
1572 { FDRIVE_DRV_NONE
, -1, -1, 0, },
1575 void bdrv_get_floppy_geometry_hint(BlockDriverState
*bs
, int *nb_heads
,
1576 int *max_track
, int *last_sect
,
1577 FDriveType drive_in
, FDriveType
*drive
)
1579 const FDFormat
*parse
;
1580 uint64_t nb_sectors
, size
;
1581 int i
, first_match
, match
;
1583 bdrv_get_geometry_hint(bs
, nb_heads
, max_track
, last_sect
);
1584 if (*nb_heads
!= 0 && *max_track
!= 0 && *last_sect
!= 0) {
1585 /* User defined disk */
1587 bdrv_get_geometry(bs
, &nb_sectors
);
1590 for (i
= 0; ; i
++) {
1591 parse
= &fd_formats
[i
];
1592 if (parse
->drive
== FDRIVE_DRV_NONE
) {
1595 if (drive_in
== parse
->drive
||
1596 drive_in
== FDRIVE_DRV_NONE
) {
1597 size
= (parse
->max_head
+ 1) * parse
->max_track
*
1599 if (nb_sectors
== size
) {
1603 if (first_match
== -1) {
1609 if (first_match
== -1) {
1612 match
= first_match
;
1614 parse
= &fd_formats
[match
];
1616 *nb_heads
= parse
->max_head
+ 1;
1617 *max_track
= parse
->max_track
;
1618 *last_sect
= parse
->last_sect
;
1619 *drive
= parse
->drive
;
1623 int bdrv_get_translation_hint(BlockDriverState
*bs
)
1625 return bs
->translation
;
1628 void bdrv_set_on_error(BlockDriverState
*bs
, BlockErrorAction on_read_error
,
1629 BlockErrorAction on_write_error
)
1631 bs
->on_read_error
= on_read_error
;
1632 bs
->on_write_error
= on_write_error
;
1635 BlockErrorAction
bdrv_get_on_error(BlockDriverState
*bs
, int is_read
)
1637 return is_read
? bs
->on_read_error
: bs
->on_write_error
;
1640 int bdrv_is_read_only(BlockDriverState
*bs
)
1642 return bs
->read_only
;
1645 int bdrv_is_sg(BlockDriverState
*bs
)
1650 int bdrv_enable_write_cache(BlockDriverState
*bs
)
1652 return bs
->enable_write_cache
;
1655 int bdrv_is_encrypted(BlockDriverState
*bs
)
1657 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1659 return bs
->encrypted
;
1662 int bdrv_key_required(BlockDriverState
*bs
)
1664 BlockDriverState
*backing_hd
= bs
->backing_hd
;
1666 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
1668 return (bs
->encrypted
&& !bs
->valid_key
);
1671 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
1674 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
1675 ret
= bdrv_set_key(bs
->backing_hd
, key
);
1681 if (!bs
->encrypted
) {
1683 } else if (!bs
->drv
|| !bs
->drv
->bdrv_set_key
) {
1686 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
1689 } else if (!bs
->valid_key
) {
1691 /* call the change callback now, we skipped it on open */
1692 bdrv_dev_change_media_cb(bs
, true);
1697 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
1702 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
1706 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
1711 QLIST_FOREACH(drv
, &bdrv_drivers
, list
) {
1712 it(opaque
, drv
->format_name
);
1716 BlockDriverState
*bdrv_find(const char *name
)
1718 BlockDriverState
*bs
;
1720 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1721 if (!strcmp(name
, bs
->device_name
)) {
1728 BlockDriverState
*bdrv_next(BlockDriverState
*bs
)
1731 return QTAILQ_FIRST(&bdrv_states
);
1733 return QTAILQ_NEXT(bs
, list
);
1736 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1738 BlockDriverState
*bs
;
1740 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1745 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1747 return bs
->device_name
;
1750 void bdrv_flush_all(void)
1752 BlockDriverState
*bs
;
1754 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1755 if (!bdrv_is_read_only(bs
) && bdrv_is_inserted(bs
)) {
1761 int bdrv_has_zero_init(BlockDriverState
*bs
)
1765 if (bs
->drv
->bdrv_has_zero_init
) {
1766 return bs
->drv
->bdrv_has_zero_init(bs
);
1773 * Returns true iff the specified sector is present in the disk image. Drivers
1774 * not implementing the functionality are assumed to not support backing files,
1775 * hence all their sectors are reported as allocated.
1777 * 'pnum' is set to the number of sectors (including and immediately following
1778 * the specified sector) that are known to be in the same
1779 * allocated/unallocated state.
1781 * 'nb_sectors' is the max value 'pnum' should be set to.
1783 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1787 if (!bs
->drv
->bdrv_is_allocated
) {
1788 if (sector_num
>= bs
->total_sectors
) {
1792 n
= bs
->total_sectors
- sector_num
;
1793 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1796 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1799 void bdrv_mon_event(const BlockDriverState
*bdrv
,
1800 BlockMonEventAction action
, int is_read
)
1803 const char *action_str
;
1806 case BDRV_ACTION_REPORT
:
1807 action_str
= "report";
1809 case BDRV_ACTION_IGNORE
:
1810 action_str
= "ignore";
1812 case BDRV_ACTION_STOP
:
1813 action_str
= "stop";
1819 data
= qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1822 is_read
? "read" : "write");
1823 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR
, data
);
1825 qobject_decref(data
);
1828 BlockInfoList
*qmp_query_block(Error
**errp
)
1830 BlockInfoList
*head
= NULL
, *cur_item
= NULL
;
1831 BlockDriverState
*bs
;
1833 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1834 BlockInfoList
*info
= g_malloc0(sizeof(*info
));
1836 info
->value
= g_malloc0(sizeof(*info
->value
));
1837 info
->value
->device
= g_strdup(bs
->device_name
);
1838 info
->value
->type
= g_strdup("unknown");
1839 info
->value
->locked
= bdrv_dev_is_medium_locked(bs
);
1840 info
->value
->removable
= bdrv_dev_has_removable_media(bs
);
1842 if (bdrv_dev_has_removable_media(bs
)) {
1843 info
->value
->has_tray_open
= true;
1844 info
->value
->tray_open
= bdrv_dev_is_tray_open(bs
);
1847 if (bdrv_iostatus_is_enabled(bs
)) {
1848 info
->value
->has_io_status
= true;
1849 info
->value
->io_status
= bs
->iostatus
;
1853 info
->value
->has_inserted
= true;
1854 info
->value
->inserted
= g_malloc0(sizeof(*info
->value
->inserted
));
1855 info
->value
->inserted
->file
= g_strdup(bs
->filename
);
1856 info
->value
->inserted
->ro
= bs
->read_only
;
1857 info
->value
->inserted
->drv
= g_strdup(bs
->drv
->format_name
);
1858 info
->value
->inserted
->encrypted
= bs
->encrypted
;
1859 if (bs
->backing_file
[0]) {
1860 info
->value
->inserted
->has_backing_file
= true;
1861 info
->value
->inserted
->backing_file
= g_strdup(bs
->backing_file
);
1865 /* XXX: waiting for the qapi to support GSList */
1867 head
= cur_item
= info
;
1869 cur_item
->next
= info
;
1877 static void bdrv_stats_iter(QObject
*data
, void *opaque
)
1880 Monitor
*mon
= opaque
;
1882 qdict
= qobject_to_qdict(data
);
1883 monitor_printf(mon
, "%s:", qdict_get_str(qdict
, "device"));
1885 qdict
= qobject_to_qdict(qdict_get(qdict
, "stats"));
1886 monitor_printf(mon
, " rd_bytes=%" PRId64
1887 " wr_bytes=%" PRId64
1888 " rd_operations=%" PRId64
1889 " wr_operations=%" PRId64
1890 " flush_operations=%" PRId64
1891 " wr_total_time_ns=%" PRId64
1892 " rd_total_time_ns=%" PRId64
1893 " flush_total_time_ns=%" PRId64
1895 qdict_get_int(qdict
, "rd_bytes"),
1896 qdict_get_int(qdict
, "wr_bytes"),
1897 qdict_get_int(qdict
, "rd_operations"),
1898 qdict_get_int(qdict
, "wr_operations"),
1899 qdict_get_int(qdict
, "flush_operations"),
1900 qdict_get_int(qdict
, "wr_total_time_ns"),
1901 qdict_get_int(qdict
, "rd_total_time_ns"),
1902 qdict_get_int(qdict
, "flush_total_time_ns"));
1905 void bdrv_stats_print(Monitor
*mon
, const QObject
*data
)
1907 qlist_iter(qobject_to_qlist(data
), bdrv_stats_iter
, mon
);
1910 static QObject
* bdrv_info_stats_bs(BlockDriverState
*bs
)
1915 res
= qobject_from_jsonf("{ 'stats': {"
1916 "'rd_bytes': %" PRId64
","
1917 "'wr_bytes': %" PRId64
","
1918 "'rd_operations': %" PRId64
","
1919 "'wr_operations': %" PRId64
","
1920 "'wr_highest_offset': %" PRId64
","
1921 "'flush_operations': %" PRId64
","
1922 "'wr_total_time_ns': %" PRId64
","
1923 "'rd_total_time_ns': %" PRId64
","
1924 "'flush_total_time_ns': %" PRId64
1926 bs
->nr_bytes
[BDRV_ACCT_READ
],
1927 bs
->nr_bytes
[BDRV_ACCT_WRITE
],
1928 bs
->nr_ops
[BDRV_ACCT_READ
],
1929 bs
->nr_ops
[BDRV_ACCT_WRITE
],
1930 bs
->wr_highest_sector
*
1931 (uint64_t)BDRV_SECTOR_SIZE
,
1932 bs
->nr_ops
[BDRV_ACCT_FLUSH
],
1933 bs
->total_time_ns
[BDRV_ACCT_WRITE
],
1934 bs
->total_time_ns
[BDRV_ACCT_READ
],
1935 bs
->total_time_ns
[BDRV_ACCT_FLUSH
]);
1936 dict
= qobject_to_qdict(res
);
1938 if (*bs
->device_name
) {
1939 qdict_put(dict
, "device", qstring_from_str(bs
->device_name
));
1943 QObject
*parent
= bdrv_info_stats_bs(bs
->file
);
1944 qdict_put_obj(dict
, "parent", parent
);
1950 void bdrv_info_stats(Monitor
*mon
, QObject
**ret_data
)
1954 BlockDriverState
*bs
;
1956 devices
= qlist_new();
1958 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1959 obj
= bdrv_info_stats_bs(bs
);
1960 qlist_append_obj(devices
, obj
);
1963 *ret_data
= QOBJECT(devices
);
1966 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1968 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1969 return bs
->backing_file
;
1970 else if (bs
->encrypted
)
1971 return bs
->filename
;
1976 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1977 char *filename
, int filename_size
)
1979 if (!bs
->backing_file
) {
1980 pstrcpy(filename
, filename_size
, "");
1982 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1986 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1987 const uint8_t *buf
, int nb_sectors
)
1989 BlockDriver
*drv
= bs
->drv
;
1992 if (!drv
->bdrv_write_compressed
)
1994 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1997 if (bs
->dirty_bitmap
) {
1998 set_dirty_bitmap(bs
, sector_num
, nb_sectors
, 1);
2001 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
2004 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2006 BlockDriver
*drv
= bs
->drv
;
2009 if (!drv
->bdrv_get_info
)
2011 memset(bdi
, 0, sizeof(*bdi
));
2012 return drv
->bdrv_get_info(bs
, bdi
);
2015 int bdrv_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
2016 int64_t pos
, int size
)
2018 BlockDriver
*drv
= bs
->drv
;
2021 if (drv
->bdrv_save_vmstate
)
2022 return drv
->bdrv_save_vmstate(bs
, buf
, pos
, size
);
2024 return bdrv_save_vmstate(bs
->file
, buf
, pos
, size
);
2028 int bdrv_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
2029 int64_t pos
, int size
)
2031 BlockDriver
*drv
= bs
->drv
;
2034 if (drv
->bdrv_load_vmstate
)
2035 return drv
->bdrv_load_vmstate(bs
, buf
, pos
, size
);
2037 return bdrv_load_vmstate(bs
->file
, buf
, pos
, size
);
2041 void bdrv_debug_event(BlockDriverState
*bs
, BlkDebugEvent event
)
2043 BlockDriver
*drv
= bs
->drv
;
2045 if (!drv
|| !drv
->bdrv_debug_event
) {
2049 return drv
->bdrv_debug_event(bs
, event
);
2053 /**************************************************************/
2054 /* handling of snapshots */
2056 int bdrv_can_snapshot(BlockDriverState
*bs
)
2058 BlockDriver
*drv
= bs
->drv
;
2059 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2063 if (!drv
->bdrv_snapshot_create
) {
2064 if (bs
->file
!= NULL
) {
2065 return bdrv_can_snapshot(bs
->file
);
2073 int bdrv_is_snapshot(BlockDriverState
*bs
)
2075 return !!(bs
->open_flags
& BDRV_O_SNAPSHOT
);
2078 BlockDriverState
*bdrv_snapshots(void)
2080 BlockDriverState
*bs
;
2083 return bs_snapshots
;
2087 while ((bs
= bdrv_next(bs
))) {
2088 if (bdrv_can_snapshot(bs
)) {
2096 int bdrv_snapshot_create(BlockDriverState
*bs
,
2097 QEMUSnapshotInfo
*sn_info
)
2099 BlockDriver
*drv
= bs
->drv
;
2102 if (drv
->bdrv_snapshot_create
)
2103 return drv
->bdrv_snapshot_create(bs
, sn_info
);
2105 return bdrv_snapshot_create(bs
->file
, sn_info
);
2109 int bdrv_snapshot_goto(BlockDriverState
*bs
,
2110 const char *snapshot_id
)
2112 BlockDriver
*drv
= bs
->drv
;
2117 if (drv
->bdrv_snapshot_goto
)
2118 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
2121 drv
->bdrv_close(bs
);
2122 ret
= bdrv_snapshot_goto(bs
->file
, snapshot_id
);
2123 open_ret
= drv
->bdrv_open(bs
, bs
->open_flags
);
2125 bdrv_delete(bs
->file
);
2135 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
2137 BlockDriver
*drv
= bs
->drv
;
2140 if (drv
->bdrv_snapshot_delete
)
2141 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
2143 return bdrv_snapshot_delete(bs
->file
, snapshot_id
);
2147 int bdrv_snapshot_list(BlockDriverState
*bs
,
2148 QEMUSnapshotInfo
**psn_info
)
2150 BlockDriver
*drv
= bs
->drv
;
2153 if (drv
->bdrv_snapshot_list
)
2154 return drv
->bdrv_snapshot_list(bs
, psn_info
);
2156 return bdrv_snapshot_list(bs
->file
, psn_info
);
2160 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
2161 const char *snapshot_name
)
2163 BlockDriver
*drv
= bs
->drv
;
2167 if (!bs
->read_only
) {
2170 if (drv
->bdrv_snapshot_load_tmp
) {
2171 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_name
);
2176 #define NB_SUFFIXES 4
2178 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
2180 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
2185 snprintf(buf
, buf_size
, "%" PRId64
, size
);
2188 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
2189 if (size
< (10 * base
)) {
2190 snprintf(buf
, buf_size
, "%0.1f%c",
2191 (double)size
/ base
,
2194 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
2195 snprintf(buf
, buf_size
, "%" PRId64
"%c",
2196 ((size
+ (base
>> 1)) / base
),
2206 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
2208 char buf1
[128], date_buf
[128], clock_buf
[128];
2218 snprintf(buf
, buf_size
,
2219 "%-10s%-20s%7s%20s%15s",
2220 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
2224 ptm
= localtime(&ti
);
2225 strftime(date_buf
, sizeof(date_buf
),
2226 "%Y-%m-%d %H:%M:%S", ptm
);
2228 localtime_r(&ti
, &tm
);
2229 strftime(date_buf
, sizeof(date_buf
),
2230 "%Y-%m-%d %H:%M:%S", &tm
);
2232 secs
= sn
->vm_clock_nsec
/ 1000000000;
2233 snprintf(clock_buf
, sizeof(clock_buf
),
2234 "%02d:%02d:%02d.%03d",
2236 (int)((secs
/ 60) % 60),
2238 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
2239 snprintf(buf
, buf_size
,
2240 "%-10s%-20s%7s%20s%15s",
2241 sn
->id_str
, sn
->name
,
2242 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
2249 /**************************************************************/
2252 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
2253 QEMUIOVector
*qiov
, int nb_sectors
,
2254 BlockDriverCompletionFunc
*cb
, void *opaque
)
2256 trace_bdrv_aio_readv(bs
, sector_num
, nb_sectors
, opaque
);
2258 return bdrv_co_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
,
2262 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
2263 QEMUIOVector
*qiov
, int nb_sectors
,
2264 BlockDriverCompletionFunc
*cb
, void *opaque
)
2266 trace_bdrv_aio_writev(bs
, sector_num
, nb_sectors
, opaque
);
2268 return bdrv_co_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
,
2273 typedef struct MultiwriteCB
{
2278 BlockDriverCompletionFunc
*cb
;
2280 QEMUIOVector
*free_qiov
;
2285 static void multiwrite_user_cb(MultiwriteCB
*mcb
)
2289 for (i
= 0; i
< mcb
->num_callbacks
; i
++) {
2290 mcb
->callbacks
[i
].cb(mcb
->callbacks
[i
].opaque
, mcb
->error
);
2291 if (mcb
->callbacks
[i
].free_qiov
) {
2292 qemu_iovec_destroy(mcb
->callbacks
[i
].free_qiov
);
2294 g_free(mcb
->callbacks
[i
].free_qiov
);
2295 qemu_vfree(mcb
->callbacks
[i
].free_buf
);
2299 static void multiwrite_cb(void *opaque
, int ret
)
2301 MultiwriteCB
*mcb
= opaque
;
2303 trace_multiwrite_cb(mcb
, ret
);
2305 if (ret
< 0 && !mcb
->error
) {
2309 mcb
->num_requests
--;
2310 if (mcb
->num_requests
== 0) {
2311 multiwrite_user_cb(mcb
);
2316 static int multiwrite_req_compare(const void *a
, const void *b
)
2318 const BlockRequest
*req1
= a
, *req2
= b
;
2321 * Note that we can't simply subtract req2->sector from req1->sector
2322 * here as that could overflow the return value.
2324 if (req1
->sector
> req2
->sector
) {
2326 } else if (req1
->sector
< req2
->sector
) {
2334 * Takes a bunch of requests and tries to merge them. Returns the number of
2335 * requests that remain after merging.
2337 static int multiwrite_merge(BlockDriverState
*bs
, BlockRequest
*reqs
,
2338 int num_reqs
, MultiwriteCB
*mcb
)
2342 // Sort requests by start sector
2343 qsort(reqs
, num_reqs
, sizeof(*reqs
), &multiwrite_req_compare
);
2345 // Check if adjacent requests touch the same clusters. If so, combine them,
2346 // filling up gaps with zero sectors.
2348 for (i
= 1; i
< num_reqs
; i
++) {
2350 int64_t oldreq_last
= reqs
[outidx
].sector
+ reqs
[outidx
].nb_sectors
;
2352 // This handles the cases that are valid for all block drivers, namely
2353 // exactly sequential writes and overlapping writes.
2354 if (reqs
[i
].sector
<= oldreq_last
) {
2358 // The block driver may decide that it makes sense to combine requests
2359 // even if there is a gap of some sectors between them. In this case,
2360 // the gap is filled with zeros (therefore only applicable for yet
2361 // unused space in format like qcow2).
2362 if (!merge
&& bs
->drv
->bdrv_merge_requests
) {
2363 merge
= bs
->drv
->bdrv_merge_requests(bs
, &reqs
[outidx
], &reqs
[i
]);
2366 if (reqs
[outidx
].qiov
->niov
+ reqs
[i
].qiov
->niov
+ 1 > IOV_MAX
) {
2372 QEMUIOVector
*qiov
= g_malloc0(sizeof(*qiov
));
2373 qemu_iovec_init(qiov
,
2374 reqs
[outidx
].qiov
->niov
+ reqs
[i
].qiov
->niov
+ 1);
2376 // Add the first request to the merged one. If the requests are
2377 // overlapping, drop the last sectors of the first request.
2378 size
= (reqs
[i
].sector
- reqs
[outidx
].sector
) << 9;
2379 qemu_iovec_concat(qiov
, reqs
[outidx
].qiov
, size
);
2381 // We might need to add some zeros between the two requests
2382 if (reqs
[i
].sector
> oldreq_last
) {
2383 size_t zero_bytes
= (reqs
[i
].sector
- oldreq_last
) << 9;
2384 uint8_t *buf
= qemu_blockalign(bs
, zero_bytes
);
2385 memset(buf
, 0, zero_bytes
);
2386 qemu_iovec_add(qiov
, buf
, zero_bytes
);
2387 mcb
->callbacks
[i
].free_buf
= buf
;
2390 // Add the second request
2391 qemu_iovec_concat(qiov
, reqs
[i
].qiov
, reqs
[i
].qiov
->size
);
2393 reqs
[outidx
].nb_sectors
= qiov
->size
>> 9;
2394 reqs
[outidx
].qiov
= qiov
;
2396 mcb
->callbacks
[i
].free_qiov
= reqs
[outidx
].qiov
;
2399 reqs
[outidx
].sector
= reqs
[i
].sector
;
2400 reqs
[outidx
].nb_sectors
= reqs
[i
].nb_sectors
;
2401 reqs
[outidx
].qiov
= reqs
[i
].qiov
;
2409 * Submit multiple AIO write requests at once.
2411 * On success, the function returns 0 and all requests in the reqs array have
2412 * been submitted. In error case this function returns -1, and any of the
2413 * requests may or may not be submitted yet. In particular, this means that the
2414 * callback will be called for some of the requests, for others it won't. The
2415 * caller must check the error field of the BlockRequest to wait for the right
2416 * callbacks (if error != 0, no callback will be called).
2418 * The implementation may modify the contents of the reqs array, e.g. to merge
2419 * requests. However, the fields opaque and error are left unmodified as they
2420 * are used to signal failure for a single request to the caller.
2422 int bdrv_aio_multiwrite(BlockDriverState
*bs
, BlockRequest
*reqs
, int num_reqs
)
2424 BlockDriverAIOCB
*acb
;
2428 /* don't submit writes if we don't have a medium */
2429 if (bs
->drv
== NULL
) {
2430 for (i
= 0; i
< num_reqs
; i
++) {
2431 reqs
[i
].error
= -ENOMEDIUM
;
2436 if (num_reqs
== 0) {
2440 // Create MultiwriteCB structure
2441 mcb
= g_malloc0(sizeof(*mcb
) + num_reqs
* sizeof(*mcb
->callbacks
));
2442 mcb
->num_requests
= 0;
2443 mcb
->num_callbacks
= num_reqs
;
2445 for (i
= 0; i
< num_reqs
; i
++) {
2446 mcb
->callbacks
[i
].cb
= reqs
[i
].cb
;
2447 mcb
->callbacks
[i
].opaque
= reqs
[i
].opaque
;
2450 // Check for mergable requests
2451 num_reqs
= multiwrite_merge(bs
, reqs
, num_reqs
, mcb
);
2453 trace_bdrv_aio_multiwrite(mcb
, mcb
->num_callbacks
, num_reqs
);
2456 * Run the aio requests. As soon as one request can't be submitted
2457 * successfully, fail all requests that are not yet submitted (we must
2458 * return failure for all requests anyway)
2460 * num_requests cannot be set to the right value immediately: If
2461 * bdrv_aio_writev fails for some request, num_requests would be too high
2462 * and therefore multiwrite_cb() would never recognize the multiwrite
2463 * request as completed. We also cannot use the loop variable i to set it
2464 * when the first request fails because the callback may already have been
2465 * called for previously submitted requests. Thus, num_requests must be
2466 * incremented for each request that is submitted.
2468 * The problem that callbacks may be called early also means that we need
2469 * to take care that num_requests doesn't become 0 before all requests are
2470 * submitted - multiwrite_cb() would consider the multiwrite request
2471 * completed. A dummy request that is "completed" by a manual call to
2472 * multiwrite_cb() takes care of this.
2474 mcb
->num_requests
= 1;
2476 // Run the aio requests
2477 for (i
= 0; i
< num_reqs
; i
++) {
2478 mcb
->num_requests
++;
2479 acb
= bdrv_aio_writev(bs
, reqs
[i
].sector
, reqs
[i
].qiov
,
2480 reqs
[i
].nb_sectors
, multiwrite_cb
, mcb
);
2483 // We can only fail the whole thing if no request has been
2484 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2485 // complete and report the error in the callback.
2487 trace_bdrv_aio_multiwrite_earlyfail(mcb
);
2490 trace_bdrv_aio_multiwrite_latefail(mcb
, i
);
2491 multiwrite_cb(mcb
, -EIO
);
2497 /* Complete the dummy request */
2498 multiwrite_cb(mcb
, 0);
2503 for (i
= 0; i
< mcb
->num_callbacks
; i
++) {
2504 reqs
[i
].error
= -EIO
;
2510 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
2512 acb
->pool
->cancel(acb
);
2516 /**************************************************************/
2517 /* async block device emulation */
2519 typedef struct BlockDriverAIOCBSync
{
2520 BlockDriverAIOCB common
;
2523 /* vector translation state */
2527 } BlockDriverAIOCBSync
;
2529 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
2531 BlockDriverAIOCBSync
*acb
=
2532 container_of(blockacb
, BlockDriverAIOCBSync
, common
);
2533 qemu_bh_delete(acb
->bh
);
2535 qemu_aio_release(acb
);
2538 static AIOPool bdrv_em_aio_pool
= {
2539 .aiocb_size
= sizeof(BlockDriverAIOCBSync
),
2540 .cancel
= bdrv_aio_cancel_em
,
2543 static void bdrv_aio_bh_cb(void *opaque
)
2545 BlockDriverAIOCBSync
*acb
= opaque
;
2548 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
2549 qemu_vfree(acb
->bounce
);
2550 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
2551 qemu_bh_delete(acb
->bh
);
2553 qemu_aio_release(acb
);
2556 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
2560 BlockDriverCompletionFunc
*cb
,
2565 BlockDriverAIOCBSync
*acb
;
2567 acb
= qemu_aio_get(&bdrv_em_aio_pool
, bs
, cb
, opaque
);
2568 acb
->is_write
= is_write
;
2570 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
2573 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
2576 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
2577 acb
->ret
= bs
->drv
->bdrv_write(bs
, sector_num
, acb
->bounce
, nb_sectors
);
2579 acb
->ret
= bs
->drv
->bdrv_read(bs
, sector_num
, acb
->bounce
, nb_sectors
);
2582 qemu_bh_schedule(acb
->bh
);
2584 return &acb
->common
;
2587 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
2588 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
2589 BlockDriverCompletionFunc
*cb
, void *opaque
)
2591 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
2594 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
2595 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
2596 BlockDriverCompletionFunc
*cb
, void *opaque
)
2598 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
2602 typedef struct BlockDriverAIOCBCoroutine
{
2603 BlockDriverAIOCB common
;
2607 } BlockDriverAIOCBCoroutine
;
2609 static void bdrv_aio_co_cancel_em(BlockDriverAIOCB
*blockacb
)
2614 static AIOPool bdrv_em_co_aio_pool
= {
2615 .aiocb_size
= sizeof(BlockDriverAIOCBCoroutine
),
2616 .cancel
= bdrv_aio_co_cancel_em
,
2619 static void bdrv_co_em_bh(void *opaque
)
2621 BlockDriverAIOCBCoroutine
*acb
= opaque
;
2623 acb
->common
.cb(acb
->common
.opaque
, acb
->req
.error
);
2624 qemu_bh_delete(acb
->bh
);
2625 qemu_aio_release(acb
);
2628 /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
2629 static void coroutine_fn
bdrv_co_do_rw(void *opaque
)
2631 BlockDriverAIOCBCoroutine
*acb
= opaque
;
2632 BlockDriverState
*bs
= acb
->common
.bs
;
2634 if (!acb
->is_write
) {
2635 acb
->req
.error
= bdrv_co_do_readv(bs
, acb
->req
.sector
,
2636 acb
->req
.nb_sectors
, acb
->req
.qiov
);
2638 acb
->req
.error
= bdrv_co_do_writev(bs
, acb
->req
.sector
,
2639 acb
->req
.nb_sectors
, acb
->req
.qiov
);
2642 acb
->bh
= qemu_bh_new(bdrv_co_em_bh
, acb
);
2643 qemu_bh_schedule(acb
->bh
);
2646 static BlockDriverAIOCB
*bdrv_co_aio_rw_vector(BlockDriverState
*bs
,
2650 BlockDriverCompletionFunc
*cb
,
2655 BlockDriverAIOCBCoroutine
*acb
;
2657 acb
= qemu_aio_get(&bdrv_em_co_aio_pool
, bs
, cb
, opaque
);
2658 acb
->req
.sector
= sector_num
;
2659 acb
->req
.nb_sectors
= nb_sectors
;
2660 acb
->req
.qiov
= qiov
;
2661 acb
->is_write
= is_write
;
2663 co
= qemu_coroutine_create(bdrv_co_do_rw
);
2664 qemu_coroutine_enter(co
, acb
);
2666 return &acb
->common
;
2669 static void coroutine_fn
bdrv_aio_flush_co_entry(void *opaque
)
2671 BlockDriverAIOCBCoroutine
*acb
= opaque
;
2672 BlockDriverState
*bs
= acb
->common
.bs
;
2674 acb
->req
.error
= bdrv_co_flush(bs
);
2675 acb
->bh
= qemu_bh_new(bdrv_co_em_bh
, acb
);
2676 qemu_bh_schedule(acb
->bh
);
2679 BlockDriverAIOCB
*bdrv_aio_flush(BlockDriverState
*bs
,
2680 BlockDriverCompletionFunc
*cb
, void *opaque
)
2682 trace_bdrv_aio_flush(bs
, opaque
);
2685 BlockDriverAIOCBCoroutine
*acb
;
2687 acb
= qemu_aio_get(&bdrv_em_co_aio_pool
, bs
, cb
, opaque
);
2688 co
= qemu_coroutine_create(bdrv_aio_flush_co_entry
);
2689 qemu_coroutine_enter(co
, acb
);
2691 return &acb
->common
;
2694 static void coroutine_fn
bdrv_aio_discard_co_entry(void *opaque
)
2696 BlockDriverAIOCBCoroutine
*acb
= opaque
;
2697 BlockDriverState
*bs
= acb
->common
.bs
;
2699 acb
->req
.error
= bdrv_co_discard(bs
, acb
->req
.sector
, acb
->req
.nb_sectors
);
2700 acb
->bh
= qemu_bh_new(bdrv_co_em_bh
, acb
);
2701 qemu_bh_schedule(acb
->bh
);
2704 BlockDriverAIOCB
*bdrv_aio_discard(BlockDriverState
*bs
,
2705 int64_t sector_num
, int nb_sectors
,
2706 BlockDriverCompletionFunc
*cb
, void *opaque
)
2709 BlockDriverAIOCBCoroutine
*acb
;
2711 trace_bdrv_aio_discard(bs
, sector_num
, nb_sectors
, opaque
);
2713 acb
= qemu_aio_get(&bdrv_em_co_aio_pool
, bs
, cb
, opaque
);
2714 acb
->req
.sector
= sector_num
;
2715 acb
->req
.nb_sectors
= nb_sectors
;
2716 co
= qemu_coroutine_create(bdrv_aio_discard_co_entry
);
2717 qemu_coroutine_enter(co
, acb
);
2719 return &acb
->common
;
2722 void bdrv_init(void)
2724 module_call_init(MODULE_INIT_BLOCK
);
2727 void bdrv_init_with_whitelist(void)
2729 use_bdrv_whitelist
= 1;
2733 void *qemu_aio_get(AIOPool
*pool
, BlockDriverState
*bs
,
2734 BlockDriverCompletionFunc
*cb
, void *opaque
)
2736 BlockDriverAIOCB
*acb
;
2738 if (pool
->free_aiocb
) {
2739 acb
= pool
->free_aiocb
;
2740 pool
->free_aiocb
= acb
->next
;
2742 acb
= g_malloc0(pool
->aiocb_size
);
2747 acb
->opaque
= opaque
;
2751 void qemu_aio_release(void *p
)
2753 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
2754 AIOPool
*pool
= acb
->pool
;
2755 acb
->next
= pool
->free_aiocb
;
2756 pool
->free_aiocb
= acb
;
2759 /**************************************************************/
2760 /* Coroutine block device emulation */
2762 typedef struct CoroutineIOCompletion
{
2763 Coroutine
*coroutine
;
2765 } CoroutineIOCompletion
;
2767 static void bdrv_co_io_em_complete(void *opaque
, int ret
)
2769 CoroutineIOCompletion
*co
= opaque
;
2772 qemu_coroutine_enter(co
->coroutine
, NULL
);
2775 static int coroutine_fn
bdrv_co_io_em(BlockDriverState
*bs
, int64_t sector_num
,
2776 int nb_sectors
, QEMUIOVector
*iov
,
2779 CoroutineIOCompletion co
= {
2780 .coroutine
= qemu_coroutine_self(),
2782 BlockDriverAIOCB
*acb
;
2785 acb
= bs
->drv
->bdrv_aio_writev(bs
, sector_num
, iov
, nb_sectors
,
2786 bdrv_co_io_em_complete
, &co
);
2788 acb
= bs
->drv
->bdrv_aio_readv(bs
, sector_num
, iov
, nb_sectors
,
2789 bdrv_co_io_em_complete
, &co
);
2792 trace_bdrv_co_io_em(bs
, sector_num
, nb_sectors
, is_write
, acb
);
2796 qemu_coroutine_yield();
2801 static int coroutine_fn
bdrv_co_readv_em(BlockDriverState
*bs
,
2802 int64_t sector_num
, int nb_sectors
,
2805 return bdrv_co_io_em(bs
, sector_num
, nb_sectors
, iov
, false);
2808 static int coroutine_fn
bdrv_co_writev_em(BlockDriverState
*bs
,
2809 int64_t sector_num
, int nb_sectors
,
2812 return bdrv_co_io_em(bs
, sector_num
, nb_sectors
, iov
, true);
2815 static void coroutine_fn
bdrv_flush_co_entry(void *opaque
)
2817 RwCo
*rwco
= opaque
;
2819 rwco
->ret
= bdrv_co_flush(rwco
->bs
);
2822 int coroutine_fn
bdrv_co_flush(BlockDriverState
*bs
)
2824 if (bs
->open_flags
& BDRV_O_NO_FLUSH
) {
2826 } else if (!bs
->drv
) {
2828 } else if (bs
->drv
->bdrv_co_flush
) {
2829 return bs
->drv
->bdrv_co_flush(bs
);
2830 } else if (bs
->drv
->bdrv_aio_flush
) {
2831 BlockDriverAIOCB
*acb
;
2832 CoroutineIOCompletion co
= {
2833 .coroutine
= qemu_coroutine_self(),
2836 acb
= bs
->drv
->bdrv_aio_flush(bs
, bdrv_co_io_em_complete
, &co
);
2840 qemu_coroutine_yield();
2845 * Some block drivers always operate in either writethrough or unsafe
2846 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
2847 * know how the server works (because the behaviour is hardcoded or
2848 * depends on server-side configuration), so we can't ensure that
2849 * everything is safe on disk. Returning an error doesn't work because
2850 * that would break guests even if the server operates in writethrough
2853 * Let's hope the user knows what he's doing.
2859 int bdrv_flush(BlockDriverState
*bs
)
2867 if (qemu_in_coroutine()) {
2868 /* Fast-path if already in coroutine context */
2869 bdrv_flush_co_entry(&rwco
);
2871 co
= qemu_coroutine_create(bdrv_flush_co_entry
);
2872 qemu_coroutine_enter(co
, &rwco
);
2873 while (rwco
.ret
== NOT_DONE
) {
2881 static void coroutine_fn
bdrv_discard_co_entry(void *opaque
)
2883 RwCo
*rwco
= opaque
;
2885 rwco
->ret
= bdrv_co_discard(rwco
->bs
, rwco
->sector_num
, rwco
->nb_sectors
);
2888 int coroutine_fn
bdrv_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
2893 } else if (bdrv_check_request(bs
, sector_num
, nb_sectors
)) {
2895 } else if (bs
->read_only
) {
2897 } else if (bs
->drv
->bdrv_co_discard
) {
2898 return bs
->drv
->bdrv_co_discard(bs
, sector_num
, nb_sectors
);
2899 } else if (bs
->drv
->bdrv_aio_discard
) {
2900 BlockDriverAIOCB
*acb
;
2901 CoroutineIOCompletion co
= {
2902 .coroutine
= qemu_coroutine_self(),
2905 acb
= bs
->drv
->bdrv_aio_discard(bs
, sector_num
, nb_sectors
,
2906 bdrv_co_io_em_complete
, &co
);
2910 qemu_coroutine_yield();
2918 int bdrv_discard(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
)
2923 .sector_num
= sector_num
,
2924 .nb_sectors
= nb_sectors
,
2928 if (qemu_in_coroutine()) {
2929 /* Fast-path if already in coroutine context */
2930 bdrv_discard_co_entry(&rwco
);
2932 co
= qemu_coroutine_create(bdrv_discard_co_entry
);
2933 qemu_coroutine_enter(co
, &rwco
);
2934 while (rwco
.ret
== NOT_DONE
) {
2942 /**************************************************************/
2943 /* removable device support */
2946 * Return TRUE if the media is present
2948 int bdrv_is_inserted(BlockDriverState
*bs
)
2950 BlockDriver
*drv
= bs
->drv
;
2954 if (!drv
->bdrv_is_inserted
)
2956 return drv
->bdrv_is_inserted(bs
);
2960 * Return whether the media changed since the last call to this
2961 * function, or -ENOTSUP if we don't know. Most drivers don't know.
2963 int bdrv_media_changed(BlockDriverState
*bs
)
2965 BlockDriver
*drv
= bs
->drv
;
2967 if (drv
&& drv
->bdrv_media_changed
) {
2968 return drv
->bdrv_media_changed(bs
);
2974 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2976 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
2978 BlockDriver
*drv
= bs
->drv
;
2980 if (drv
&& drv
->bdrv_eject
) {
2981 drv
->bdrv_eject(bs
, eject_flag
);
2986 * Lock or unlock the media (if it is locked, the user won't be able
2987 * to eject it manually).
2989 void bdrv_lock_medium(BlockDriverState
*bs
, bool locked
)
2991 BlockDriver
*drv
= bs
->drv
;
2993 trace_bdrv_lock_medium(bs
, locked
);
2995 if (drv
&& drv
->bdrv_lock_medium
) {
2996 drv
->bdrv_lock_medium(bs
, locked
);
3000 /* needed for generic scsi interface */
3002 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
3004 BlockDriver
*drv
= bs
->drv
;
3006 if (drv
&& drv
->bdrv_ioctl
)
3007 return drv
->bdrv_ioctl(bs
, req
, buf
);
3011 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
3012 unsigned long int req
, void *buf
,
3013 BlockDriverCompletionFunc
*cb
, void *opaque
)
3015 BlockDriver
*drv
= bs
->drv
;
3017 if (drv
&& drv
->bdrv_aio_ioctl
)
3018 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);
3022 void bdrv_set_buffer_alignment(BlockDriverState
*bs
, int align
)
3024 bs
->buffer_alignment
= align
;
3027 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
3029 return qemu_memalign((bs
&& bs
->buffer_alignment
) ? bs
->buffer_alignment
: 512, size
);
3032 void bdrv_set_dirty_tracking(BlockDriverState
*bs
, int enable
)
3034 int64_t bitmap_size
;
3036 bs
->dirty_count
= 0;
3038 if (!bs
->dirty_bitmap
) {
3039 bitmap_size
= (bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
) +
3040 BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
3041 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
3043 bs
->dirty_bitmap
= g_malloc0(bitmap_size
);
3046 if (bs
->dirty_bitmap
) {
3047 g_free(bs
->dirty_bitmap
);
3048 bs
->dirty_bitmap
= NULL
;
3053 int bdrv_get_dirty(BlockDriverState
*bs
, int64_t sector
)
3055 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
3057 if (bs
->dirty_bitmap
&&
3058 (sector
<< BDRV_SECTOR_BITS
) < bdrv_getlength(bs
)) {
3059 return !!(bs
->dirty_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
3060 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
3066 void bdrv_reset_dirty(BlockDriverState
*bs
, int64_t cur_sector
,
3069 set_dirty_bitmap(bs
, cur_sector
, nr_sectors
, 0);
3072 int64_t bdrv_get_dirty_count(BlockDriverState
*bs
)
3074 return bs
->dirty_count
;
3077 void bdrv_set_in_use(BlockDriverState
*bs
, int in_use
)
3079 assert(bs
->in_use
!= in_use
);
3080 bs
->in_use
= in_use
;
3083 int bdrv_in_use(BlockDriverState
*bs
)
3088 void bdrv_iostatus_enable(BlockDriverState
*bs
)
3090 bs
->iostatus_enabled
= true;
3091 bs
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
3094 /* The I/O status is only enabled if the drive explicitly
3095 * enables it _and_ the VM is configured to stop on errors */
3096 bool bdrv_iostatus_is_enabled(const BlockDriverState
*bs
)
3098 return (bs
->iostatus_enabled
&&
3099 (bs
->on_write_error
== BLOCK_ERR_STOP_ENOSPC
||
3100 bs
->on_write_error
== BLOCK_ERR_STOP_ANY
||
3101 bs
->on_read_error
== BLOCK_ERR_STOP_ANY
));
3104 void bdrv_iostatus_disable(BlockDriverState
*bs
)
3106 bs
->iostatus_enabled
= false;
3109 void bdrv_iostatus_reset(BlockDriverState
*bs
)
3111 if (bdrv_iostatus_is_enabled(bs
)) {
3112 bs
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
3116 /* XXX: Today this is set by device models because it makes the implementation
3117 quite simple. However, the block layer knows about the error, so it's
3118 possible to implement this without device models being involved */
3119 void bdrv_iostatus_set_err(BlockDriverState
*bs
, int error
)
3121 if (bdrv_iostatus_is_enabled(bs
) &&
3122 bs
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
3124 bs
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
3125 BLOCK_DEVICE_IO_STATUS_FAILED
;
3130 bdrv_acct_start(BlockDriverState
*bs
, BlockAcctCookie
*cookie
, int64_t bytes
,
3131 enum BlockAcctType type
)
3133 assert(type
< BDRV_MAX_IOTYPE
);
3135 cookie
->bytes
= bytes
;
3136 cookie
->start_time_ns
= get_clock();
3137 cookie
->type
= type
;
3141 bdrv_acct_done(BlockDriverState
*bs
, BlockAcctCookie
*cookie
)
3143 assert(cookie
->type
< BDRV_MAX_IOTYPE
);
3145 bs
->nr_bytes
[cookie
->type
] += cookie
->bytes
;
3146 bs
->nr_ops
[cookie
->type
]++;
3147 bs
->total_time_ns
[cookie
->type
] += get_clock() - cookie
->start_time_ns
;
3150 int bdrv_img_create(const char *filename
, const char *fmt
,
3151 const char *base_filename
, const char *base_fmt
,
3152 char *options
, uint64_t img_size
, int flags
)
3154 QEMUOptionParameter
*param
= NULL
, *create_options
= NULL
;
3155 QEMUOptionParameter
*backing_fmt
, *backing_file
, *size
;
3156 BlockDriverState
*bs
= NULL
;
3157 BlockDriver
*drv
, *proto_drv
;
3158 BlockDriver
*backing_drv
= NULL
;
3161 /* Find driver and parse its options */
3162 drv
= bdrv_find_format(fmt
);
3164 error_report("Unknown file format '%s'", fmt
);
3169 proto_drv
= bdrv_find_protocol(filename
);
3171 error_report("Unknown protocol '%s'", filename
);
3176 create_options
= append_option_parameters(create_options
,
3177 drv
->create_options
);
3178 create_options
= append_option_parameters(create_options
,
3179 proto_drv
->create_options
);
3181 /* Create parameter list with default values */
3182 param
= parse_option_parameters("", create_options
, param
);
3184 set_option_parameter_int(param
, BLOCK_OPT_SIZE
, img_size
);
3186 /* Parse -o options */
3188 param
= parse_option_parameters(options
, create_options
, param
);
3189 if (param
== NULL
) {
3190 error_report("Invalid options for file format '%s'.", fmt
);
3196 if (base_filename
) {
3197 if (set_option_parameter(param
, BLOCK_OPT_BACKING_FILE
,
3199 error_report("Backing file not supported for file format '%s'",
3207 if (set_option_parameter(param
, BLOCK_OPT_BACKING_FMT
, base_fmt
)) {
3208 error_report("Backing file format not supported for file "
3209 "format '%s'", fmt
);
3215 backing_file
= get_option_parameter(param
, BLOCK_OPT_BACKING_FILE
);
3216 if (backing_file
&& backing_file
->value
.s
) {
3217 if (!strcmp(filename
, backing_file
->value
.s
)) {
3218 error_report("Error: Trying to create an image with the "
3219 "same filename as the backing file");
3225 backing_fmt
= get_option_parameter(param
, BLOCK_OPT_BACKING_FMT
);
3226 if (backing_fmt
&& backing_fmt
->value
.s
) {
3227 backing_drv
= bdrv_find_format(backing_fmt
->value
.s
);
3229 error_report("Unknown backing file format '%s'",
3230 backing_fmt
->value
.s
);
3236 // The size for the image must always be specified, with one exception:
3237 // If we are using a backing file, we can obtain the size from there
3238 size
= get_option_parameter(param
, BLOCK_OPT_SIZE
);
3239 if (size
&& size
->value
.n
== -1) {
3240 if (backing_file
&& backing_file
->value
.s
) {
3246 ret
= bdrv_open(bs
, backing_file
->value
.s
, flags
, backing_drv
);
3248 error_report("Could not open '%s'", backing_file
->value
.s
);
3251 bdrv_get_geometry(bs
, &size
);
3254 snprintf(buf
, sizeof(buf
), "%" PRId64
, size
);
3255 set_option_parameter(param
, BLOCK_OPT_SIZE
, buf
);
3257 error_report("Image creation needs a size parameter");
3263 printf("Formatting '%s', fmt=%s ", filename
, fmt
);
3264 print_option_parameters(param
);
3267 ret
= bdrv_create(drv
, filename
, param
);
3270 if (ret
== -ENOTSUP
) {
3271 error_report("Formatting or formatting option not supported for "
3272 "file format '%s'", fmt
);
3273 } else if (ret
== -EFBIG
) {
3274 error_report("The image size is too large for file format '%s'",
3277 error_report("%s: error while creating %s: %s", filename
, fmt
,
3283 free_option_parameters(create_options
);
3284 free_option_parameters(param
);