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"
34 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/queue.h>
47 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
49 static void bdrv_dev_change_media_cb(BlockDriverState
*bs
, bool load
);
50 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
51 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
52 BlockDriverCompletionFunc
*cb
, void *opaque
);
53 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
54 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
55 BlockDriverCompletionFunc
*cb
, void *opaque
);
56 static BlockDriverAIOCB
*bdrv_aio_flush_em(BlockDriverState
*bs
,
57 BlockDriverCompletionFunc
*cb
, void *opaque
);
58 static BlockDriverAIOCB
*bdrv_aio_noop_em(BlockDriverState
*bs
,
59 BlockDriverCompletionFunc
*cb
, void *opaque
);
60 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
61 uint8_t *buf
, int nb_sectors
);
62 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
63 const uint8_t *buf
, int nb_sectors
);
64 static int coroutine_fn
bdrv_co_readv_em(BlockDriverState
*bs
,
65 int64_t sector_num
, int nb_sectors
,
67 static int coroutine_fn
bdrv_co_writev_em(BlockDriverState
*bs
,
68 int64_t sector_num
, int nb_sectors
,
70 static int coroutine_fn
bdrv_co_flush_em(BlockDriverState
*bs
);
71 static int coroutine_fn
bdrv_co_do_readv(BlockDriverState
*bs
,
72 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
);
73 static int coroutine_fn
bdrv_co_do_writev(BlockDriverState
*bs
,
74 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
);
75 static BlockDriverAIOCB
*bdrv_co_aio_rw_vector(BlockDriverState
*bs
,
79 BlockDriverCompletionFunc
*cb
,
82 static void coroutine_fn
bdrv_co_do_rw(void *opaque
);
84 static QTAILQ_HEAD(, BlockDriverState
) bdrv_states
=
85 QTAILQ_HEAD_INITIALIZER(bdrv_states
);
87 static QLIST_HEAD(, BlockDriver
) bdrv_drivers
=
88 QLIST_HEAD_INITIALIZER(bdrv_drivers
);
90 /* The device to use for VM snapshots */
91 static BlockDriverState
*bs_snapshots
;
93 /* If non-zero, use only whitelisted block drivers */
94 static int use_bdrv_whitelist
;
97 static int is_windows_drive_prefix(const char *filename
)
99 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
100 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
104 int is_windows_drive(const char *filename
)
106 if (is_windows_drive_prefix(filename
) &&
109 if (strstart(filename
, "\\\\.\\", NULL
) ||
110 strstart(filename
, "//./", NULL
))
116 /* check if the path starts with "<protocol>:" */
117 static int path_has_protocol(const char *path
)
120 if (is_windows_drive(path
) ||
121 is_windows_drive_prefix(path
)) {
126 return strchr(path
, ':') != NULL
;
129 int path_is_absolute(const char *path
)
133 /* specific case for names like: "\\.\d:" */
134 if (*path
== '/' || *path
== '\\')
137 p
= strchr(path
, ':');
143 return (*p
== '/' || *p
== '\\');
149 /* if filename is absolute, just copy it to dest. Otherwise, build a
150 path to it by considering it is relative to base_path. URL are
152 void path_combine(char *dest
, int dest_size
,
153 const char *base_path
,
154 const char *filename
)
161 if (path_is_absolute(filename
)) {
162 pstrcpy(dest
, dest_size
, filename
);
164 p
= strchr(base_path
, ':');
169 p1
= strrchr(base_path
, '/');
173 p2
= strrchr(base_path
, '\\');
185 if (len
> dest_size
- 1)
187 memcpy(dest
, base_path
, len
);
189 pstrcat(dest
, dest_size
, filename
);
193 void bdrv_register(BlockDriver
*bdrv
)
195 /* Block drivers without coroutine functions need emulation */
196 if (!bdrv
->bdrv_co_readv
) {
197 bdrv
->bdrv_co_readv
= bdrv_co_readv_em
;
198 bdrv
->bdrv_co_writev
= bdrv_co_writev_em
;
200 if (!bdrv
->bdrv_aio_readv
) {
201 /* add AIO emulation layer */
202 bdrv
->bdrv_aio_readv
= bdrv_aio_readv_em
;
203 bdrv
->bdrv_aio_writev
= bdrv_aio_writev_em
;
204 } else if (!bdrv
->bdrv_read
) {
205 /* add synchronous IO emulation layer */
206 bdrv
->bdrv_read
= bdrv_read_em
;
207 bdrv
->bdrv_write
= bdrv_write_em
;
211 if (!bdrv
->bdrv_aio_flush
)
212 bdrv
->bdrv_aio_flush
= bdrv_aio_flush_em
;
214 QLIST_INSERT_HEAD(&bdrv_drivers
, bdrv
, list
);
217 /* create a new block device (by default it is empty) */
218 BlockDriverState
*bdrv_new(const char *device_name
)
220 BlockDriverState
*bs
;
222 bs
= g_malloc0(sizeof(BlockDriverState
));
223 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
224 if (device_name
[0] != '\0') {
225 QTAILQ_INSERT_TAIL(&bdrv_states
, bs
, list
);
227 bdrv_iostatus_disable(bs
);
231 BlockDriver
*bdrv_find_format(const char *format_name
)
234 QLIST_FOREACH(drv1
, &bdrv_drivers
, list
) {
235 if (!strcmp(drv1
->format_name
, format_name
)) {
242 static int bdrv_is_whitelisted(BlockDriver
*drv
)
244 static const char *whitelist
[] = {
245 CONFIG_BDRV_WHITELIST
250 return 1; /* no whitelist, anything goes */
252 for (p
= whitelist
; *p
; p
++) {
253 if (!strcmp(drv
->format_name
, *p
)) {
260 BlockDriver
*bdrv_find_whitelisted_format(const char *format_name
)
262 BlockDriver
*drv
= bdrv_find_format(format_name
);
263 return drv
&& bdrv_is_whitelisted(drv
) ? drv
: NULL
;
266 int bdrv_create(BlockDriver
*drv
, const char* filename
,
267 QEMUOptionParameter
*options
)
269 if (!drv
->bdrv_create
)
272 return drv
->bdrv_create(filename
, options
);
275 int bdrv_create_file(const char* filename
, QEMUOptionParameter
*options
)
279 drv
= bdrv_find_protocol(filename
);
284 return bdrv_create(drv
, filename
, options
);
288 void get_tmp_filename(char *filename
, int size
)
290 char temp_dir
[MAX_PATH
];
292 GetTempPath(MAX_PATH
, temp_dir
);
293 GetTempFileName(temp_dir
, "qem", 0, filename
);
296 void get_tmp_filename(char *filename
, int size
)
300 /* XXX: race condition possible */
301 tmpdir
= getenv("TMPDIR");
304 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
305 fd
= mkstemp(filename
);
311 * Detect host devices. By convention, /dev/cdrom[N] is always
312 * recognized as a host CDROM.
314 static BlockDriver
*find_hdev_driver(const char *filename
)
316 int score_max
= 0, score
;
317 BlockDriver
*drv
= NULL
, *d
;
319 QLIST_FOREACH(d
, &bdrv_drivers
, list
) {
320 if (d
->bdrv_probe_device
) {
321 score
= d
->bdrv_probe_device(filename
);
322 if (score
> score_max
) {
332 BlockDriver
*bdrv_find_protocol(const char *filename
)
339 /* TODO Drivers without bdrv_file_open must be specified explicitly */
342 * XXX(hch): we really should not let host device detection
343 * override an explicit protocol specification, but moving this
344 * later breaks access to device names with colons in them.
345 * Thanks to the brain-dead persistent naming schemes on udev-
346 * based Linux systems those actually are quite common.
348 drv1
= find_hdev_driver(filename
);
353 if (!path_has_protocol(filename
)) {
354 return bdrv_find_format("file");
356 p
= strchr(filename
, ':');
359 if (len
> sizeof(protocol
) - 1)
360 len
= sizeof(protocol
) - 1;
361 memcpy(protocol
, filename
, len
);
362 protocol
[len
] = '\0';
363 QLIST_FOREACH(drv1
, &bdrv_drivers
, list
) {
364 if (drv1
->protocol_name
&&
365 !strcmp(drv1
->protocol_name
, protocol
)) {
372 static int find_image_format(const char *filename
, BlockDriver
**pdrv
)
374 int ret
, score
, score_max
;
375 BlockDriver
*drv1
, *drv
;
377 BlockDriverState
*bs
;
379 ret
= bdrv_file_open(&bs
, filename
, 0);
385 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
386 if (bs
->sg
|| !bdrv_is_inserted(bs
)) {
388 drv
= bdrv_find_format("raw");
396 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
405 QLIST_FOREACH(drv1
, &bdrv_drivers
, list
) {
406 if (drv1
->bdrv_probe
) {
407 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
408 if (score
> score_max
) {
422 * Set the current 'total_sectors' value
424 static int refresh_total_sectors(BlockDriverState
*bs
, int64_t hint
)
426 BlockDriver
*drv
= bs
->drv
;
428 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
432 /* query actual device if possible, otherwise just trust the hint */
433 if (drv
->bdrv_getlength
) {
434 int64_t length
= drv
->bdrv_getlength(bs
);
438 hint
= length
>> BDRV_SECTOR_BITS
;
441 bs
->total_sectors
= hint
;
446 * Set open flags for a given cache mode
448 * Return 0 on success, -1 if the cache mode was invalid.
450 int bdrv_parse_cache_flags(const char *mode
, int *flags
)
452 *flags
&= ~BDRV_O_CACHE_MASK
;
454 if (!strcmp(mode
, "off") || !strcmp(mode
, "none")) {
455 *flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
456 } else if (!strcmp(mode
, "directsync")) {
457 *flags
|= BDRV_O_NOCACHE
;
458 } else if (!strcmp(mode
, "writeback")) {
459 *flags
|= BDRV_O_CACHE_WB
;
460 } else if (!strcmp(mode
, "unsafe")) {
461 *flags
|= BDRV_O_CACHE_WB
;
462 *flags
|= BDRV_O_NO_FLUSH
;
463 } else if (!strcmp(mode
, "writethrough")) {
464 /* this is the default */
473 * Common part for opening disk images and files
475 static int bdrv_open_common(BlockDriverState
*bs
, const char *filename
,
476 int flags
, BlockDriver
*drv
)
482 trace_bdrv_open_common(bs
, filename
, flags
, drv
->format_name
);
485 bs
->total_sectors
= 0;
488 bs
->open_flags
= flags
;
489 bs
->buffer_alignment
= 512;
491 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
493 if (use_bdrv_whitelist
&& !bdrv_is_whitelisted(drv
)) {
498 bs
->opaque
= g_malloc0(drv
->instance_size
);
500 if (flags
& BDRV_O_CACHE_WB
)
501 bs
->enable_write_cache
= 1;
504 * Clear flags that are internal to the block layer before opening the
507 open_flags
= flags
& ~(BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
);
510 * Snapshots should be writable.
512 if (bs
->is_temporary
) {
513 open_flags
|= BDRV_O_RDWR
;
516 /* Open the image, either directly or using a protocol */
517 if (drv
->bdrv_file_open
) {
518 ret
= drv
->bdrv_file_open(bs
, filename
, open_flags
);
520 ret
= bdrv_file_open(&bs
->file
, filename
, open_flags
);
522 ret
= drv
->bdrv_open(bs
, open_flags
);
530 bs
->keep_read_only
= bs
->read_only
= !(open_flags
& BDRV_O_RDWR
);
532 ret
= refresh_total_sectors(bs
, bs
->total_sectors
);
538 if (bs
->is_temporary
) {
546 bdrv_delete(bs
->file
);
556 * Opens a file using a protocol (file, host_device, nbd, ...)
558 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
560 BlockDriverState
*bs
;
564 drv
= bdrv_find_protocol(filename
);
570 ret
= bdrv_open_common(bs
, filename
, flags
, drv
);
581 * Opens a disk image (raw, qcow2, vmdk, ...)
583 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
,
588 if (flags
& BDRV_O_SNAPSHOT
) {
589 BlockDriverState
*bs1
;
592 BlockDriver
*bdrv_qcow2
;
593 QEMUOptionParameter
*options
;
594 char tmp_filename
[PATH_MAX
];
595 char backing_filename
[PATH_MAX
];
597 /* if snapshot, we create a temporary backing file and open it
598 instead of opening 'filename' directly */
600 /* if there is a backing file, use it */
602 ret
= bdrv_open(bs1
, filename
, 0, drv
);
607 total_size
= bdrv_getlength(bs1
) & BDRV_SECTOR_MASK
;
609 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
614 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
616 /* Real path is meaningless for protocols */
618 snprintf(backing_filename
, sizeof(backing_filename
),
620 else if (!realpath(filename
, backing_filename
))
623 bdrv_qcow2
= bdrv_find_format("qcow2");
624 options
= parse_option_parameters("", bdrv_qcow2
->create_options
, NULL
);
626 set_option_parameter_int(options
, BLOCK_OPT_SIZE
, total_size
);
627 set_option_parameter(options
, BLOCK_OPT_BACKING_FILE
, backing_filename
);
629 set_option_parameter(options
, BLOCK_OPT_BACKING_FMT
,
633 ret
= bdrv_create(bdrv_qcow2
, tmp_filename
, options
);
634 free_option_parameters(options
);
639 filename
= tmp_filename
;
641 bs
->is_temporary
= 1;
644 /* Find the right image format driver */
646 ret
= find_image_format(filename
, &drv
);
650 goto unlink_and_fail
;
654 ret
= bdrv_open_common(bs
, filename
, flags
, drv
);
656 goto unlink_and_fail
;
659 /* If there is a backing file, use it */
660 if ((flags
& BDRV_O_NO_BACKING
) == 0 && bs
->backing_file
[0] != '\0') {
661 char backing_filename
[PATH_MAX
];
663 BlockDriver
*back_drv
= NULL
;
665 bs
->backing_hd
= bdrv_new("");
667 if (path_has_protocol(bs
->backing_file
)) {
668 pstrcpy(backing_filename
, sizeof(backing_filename
),
671 path_combine(backing_filename
, sizeof(backing_filename
),
672 filename
, bs
->backing_file
);
675 if (bs
->backing_format
[0] != '\0') {
676 back_drv
= bdrv_find_format(bs
->backing_format
);
679 /* backing files always opened read-only */
681 flags
& ~(BDRV_O_RDWR
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
);
683 ret
= bdrv_open(bs
->backing_hd
, backing_filename
, back_flags
, back_drv
);
688 if (bs
->is_temporary
) {
689 bs
->backing_hd
->keep_read_only
= !(flags
& BDRV_O_RDWR
);
691 /* base image inherits from "parent" */
692 bs
->backing_hd
->keep_read_only
= bs
->keep_read_only
;
696 if (!bdrv_key_required(bs
)) {
697 bdrv_dev_change_media_cb(bs
, true);
703 if (bs
->is_temporary
) {
709 void bdrv_close(BlockDriverState
*bs
)
712 if (bs
== bs_snapshots
) {
715 if (bs
->backing_hd
) {
716 bdrv_delete(bs
->backing_hd
);
717 bs
->backing_hd
= NULL
;
719 bs
->drv
->bdrv_close(bs
);
722 if (bs
->is_temporary
) {
723 unlink(bs
->filename
);
729 if (bs
->file
!= NULL
) {
730 bdrv_close(bs
->file
);
733 bdrv_dev_change_media_cb(bs
, false);
737 void bdrv_close_all(void)
739 BlockDriverState
*bs
;
741 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
746 /* make a BlockDriverState anonymous by removing from bdrv_state list.
747 Also, NULL terminate the device_name to prevent double remove */
748 void bdrv_make_anon(BlockDriverState
*bs
)
750 if (bs
->device_name
[0] != '\0') {
751 QTAILQ_REMOVE(&bdrv_states
, bs
, list
);
753 bs
->device_name
[0] = '\0';
756 void bdrv_delete(BlockDriverState
*bs
)
760 /* remove from list, if necessary */
764 if (bs
->file
!= NULL
) {
765 bdrv_delete(bs
->file
);
768 assert(bs
!= bs_snapshots
);
772 int bdrv_attach_dev(BlockDriverState
*bs
, void *dev
)
773 /* TODO change to DeviceState *dev when all users are qdevified */
779 bdrv_iostatus_reset(bs
);
783 /* TODO qdevified devices don't use this, remove when devices are qdevified */
784 void bdrv_attach_dev_nofail(BlockDriverState
*bs
, void *dev
)
786 if (bdrv_attach_dev(bs
, dev
) < 0) {
791 void bdrv_detach_dev(BlockDriverState
*bs
, void *dev
)
792 /* TODO change to DeviceState *dev when all users are qdevified */
794 assert(bs
->dev
== dev
);
797 bs
->dev_opaque
= NULL
;
798 bs
->buffer_alignment
= 512;
801 /* TODO change to return DeviceState * when all users are qdevified */
802 void *bdrv_get_attached_dev(BlockDriverState
*bs
)
807 void bdrv_set_dev_ops(BlockDriverState
*bs
, const BlockDevOps
*ops
,
811 bs
->dev_opaque
= opaque
;
812 if (bdrv_dev_has_removable_media(bs
) && bs
== bs_snapshots
) {
817 static void bdrv_dev_change_media_cb(BlockDriverState
*bs
, bool load
)
819 if (bs
->dev_ops
&& bs
->dev_ops
->change_media_cb
) {
820 bs
->dev_ops
->change_media_cb(bs
->dev_opaque
, load
);
824 bool bdrv_dev_has_removable_media(BlockDriverState
*bs
)
826 return !bs
->dev
|| (bs
->dev_ops
&& bs
->dev_ops
->change_media_cb
);
829 bool bdrv_dev_is_tray_open(BlockDriverState
*bs
)
831 if (bs
->dev_ops
&& bs
->dev_ops
->is_tray_open
) {
832 return bs
->dev_ops
->is_tray_open(bs
->dev_opaque
);
837 static void bdrv_dev_resize_cb(BlockDriverState
*bs
)
839 if (bs
->dev_ops
&& bs
->dev_ops
->resize_cb
) {
840 bs
->dev_ops
->resize_cb(bs
->dev_opaque
);
844 bool bdrv_dev_is_medium_locked(BlockDriverState
*bs
)
846 if (bs
->dev_ops
&& bs
->dev_ops
->is_medium_locked
) {
847 return bs
->dev_ops
->is_medium_locked(bs
->dev_opaque
);
853 * Run consistency checks on an image
855 * Returns 0 if the check could be completed (it doesn't mean that the image is
856 * free of errors) or -errno when an internal error occurred. The results of the
857 * check are stored in res.
859 int bdrv_check(BlockDriverState
*bs
, BdrvCheckResult
*res
)
861 if (bs
->drv
->bdrv_check
== NULL
) {
865 memset(res
, 0, sizeof(*res
));
866 return bs
->drv
->bdrv_check(bs
, res
);
869 #define COMMIT_BUF_SECTORS 2048
871 /* commit COW file into the raw image */
872 int bdrv_commit(BlockDriverState
*bs
)
874 BlockDriver
*drv
= bs
->drv
;
875 BlockDriver
*backing_drv
;
876 int64_t sector
, total_sectors
;
877 int n
, ro
, open_flags
;
878 int ret
= 0, rw_ret
= 0;
881 BlockDriverState
*bs_rw
, *bs_ro
;
886 if (!bs
->backing_hd
) {
890 if (bs
->backing_hd
->keep_read_only
) {
894 backing_drv
= bs
->backing_hd
->drv
;
895 ro
= bs
->backing_hd
->read_only
;
896 strncpy(filename
, bs
->backing_hd
->filename
, sizeof(filename
));
897 open_flags
= bs
->backing_hd
->open_flags
;
901 bdrv_delete(bs
->backing_hd
);
902 bs
->backing_hd
= NULL
;
903 bs_rw
= bdrv_new("");
904 rw_ret
= bdrv_open(bs_rw
, filename
, open_flags
| BDRV_O_RDWR
,
908 /* try to re-open read-only */
909 bs_ro
= bdrv_new("");
910 ret
= bdrv_open(bs_ro
, filename
, open_flags
& ~BDRV_O_RDWR
,
914 /* drive not functional anymore */
918 bs
->backing_hd
= bs_ro
;
921 bs
->backing_hd
= bs_rw
;
924 total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
925 buf
= g_malloc(COMMIT_BUF_SECTORS
* BDRV_SECTOR_SIZE
);
927 for (sector
= 0; sector
< total_sectors
; sector
+= n
) {
928 if (drv
->bdrv_is_allocated(bs
, sector
, COMMIT_BUF_SECTORS
, &n
)) {
930 if (bdrv_read(bs
, sector
, buf
, n
) != 0) {
935 if (bdrv_write(bs
->backing_hd
, sector
, buf
, n
) != 0) {
942 if (drv
->bdrv_make_empty
) {
943 ret
= drv
->bdrv_make_empty(bs
);
948 * Make sure all data we wrote to the backing device is actually
952 bdrv_flush(bs
->backing_hd
);
959 bdrv_delete(bs
->backing_hd
);
960 bs
->backing_hd
= NULL
;
961 bs_ro
= bdrv_new("");
962 ret
= bdrv_open(bs_ro
, filename
, open_flags
& ~BDRV_O_RDWR
,
966 /* drive not functional anymore */
970 bs
->backing_hd
= bs_ro
;
971 bs
->backing_hd
->keep_read_only
= 0;
977 void bdrv_commit_all(void)
979 BlockDriverState
*bs
;
981 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
989 * -EINVAL - backing format specified, but no file
990 * -ENOSPC - can't update the backing file because no space is left in the
992 * -ENOTSUP - format driver doesn't support changing the backing file
994 int bdrv_change_backing_file(BlockDriverState
*bs
,
995 const char *backing_file
, const char *backing_fmt
)
997 BlockDriver
*drv
= bs
->drv
;
999 if (drv
->bdrv_change_backing_file
!= NULL
) {
1000 return drv
->bdrv_change_backing_file(bs
, backing_file
, backing_fmt
);
1006 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
1011 if (!bdrv_is_inserted(bs
))
1017 len
= bdrv_getlength(bs
);
1022 if ((offset
> len
) || (len
- offset
< size
))
1028 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
1031 return bdrv_check_byte_request(bs
, sector_num
* BDRV_SECTOR_SIZE
,
1032 nb_sectors
* BDRV_SECTOR_SIZE
);
1035 static inline bool bdrv_has_async_rw(BlockDriver
*drv
)
1037 return drv
->bdrv_co_readv
!= bdrv_co_readv_em
1038 || drv
->bdrv_aio_readv
!= bdrv_aio_readv_em
;
1041 static inline bool bdrv_has_async_flush(BlockDriver
*drv
)
1043 return drv
->bdrv_aio_flush
!= bdrv_aio_flush_em
;
1046 typedef struct RwCo
{
1047 BlockDriverState
*bs
;
1055 static void coroutine_fn
bdrv_rw_co_entry(void *opaque
)
1057 RwCo
*rwco
= opaque
;
1059 if (!rwco
->is_write
) {
1060 rwco
->ret
= bdrv_co_do_readv(rwco
->bs
, rwco
->sector_num
,
1061 rwco
->nb_sectors
, rwco
->qiov
);
1063 rwco
->ret
= bdrv_co_do_writev(rwco
->bs
, rwco
->sector_num
,
1064 rwco
->nb_sectors
, rwco
->qiov
);
1069 * Process a synchronous request using coroutines
1071 static int bdrv_rw_co(BlockDriverState
*bs
, int64_t sector_num
, uint8_t *buf
,
1072 int nb_sectors
, bool is_write
)
1075 struct iovec iov
= {
1076 .iov_base
= (void *)buf
,
1077 .iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
,
1082 .sector_num
= sector_num
,
1083 .nb_sectors
= nb_sectors
,
1085 .is_write
= is_write
,
1089 qemu_iovec_init_external(&qiov
, &iov
, 1);
1091 if (qemu_in_coroutine()) {
1092 /* Fast-path if already in coroutine context */
1093 bdrv_rw_co_entry(&rwco
);
1095 co
= qemu_coroutine_create(bdrv_rw_co_entry
);
1096 qemu_coroutine_enter(co
, &rwco
);
1097 while (rwco
.ret
== NOT_DONE
) {
1104 /* return < 0 if error. See bdrv_write() for the return codes */
1105 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
1106 uint8_t *buf
, int nb_sectors
)
1108 return bdrv_rw_co(bs
, sector_num
, buf
, nb_sectors
, false);
1111 static void set_dirty_bitmap(BlockDriverState
*bs
, int64_t sector_num
,
1112 int nb_sectors
, int dirty
)
1115 unsigned long val
, idx
, bit
;
1117 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
1118 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
1120 for (; start
<= end
; start
++) {
1121 idx
= start
/ (sizeof(unsigned long) * 8);
1122 bit
= start
% (sizeof(unsigned long) * 8);
1123 val
= bs
->dirty_bitmap
[idx
];
1125 if (!(val
& (1UL << bit
))) {
1130 if (val
& (1UL << bit
)) {
1132 val
&= ~(1UL << bit
);
1135 bs
->dirty_bitmap
[idx
] = val
;
1139 /* Return < 0 if error. Important errors are:
1140 -EIO generic I/O error (may happen for all errors)
1141 -ENOMEDIUM No media inserted.
1142 -EINVAL Invalid sector number or nb_sectors
1143 -EACCES Trying to write a read-only device
1145 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
1146 const uint8_t *buf
, int nb_sectors
)
1148 return bdrv_rw_co(bs
, sector_num
, (uint8_t *)buf
, nb_sectors
, true);
1151 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
1152 void *buf
, int count1
)
1154 uint8_t tmp_buf
[BDRV_SECTOR_SIZE
];
1155 int len
, nb_sectors
, count
;
1160 /* first read to align to sector start */
1161 len
= (BDRV_SECTOR_SIZE
- offset
) & (BDRV_SECTOR_SIZE
- 1);
1164 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1166 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1168 memcpy(buf
, tmp_buf
+ (offset
& (BDRV_SECTOR_SIZE
- 1)), len
);
1176 /* read the sectors "in place" */
1177 nb_sectors
= count
>> BDRV_SECTOR_BITS
;
1178 if (nb_sectors
> 0) {
1179 if ((ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
)) < 0)
1181 sector_num
+= nb_sectors
;
1182 len
= nb_sectors
<< BDRV_SECTOR_BITS
;
1187 /* add data from the last sector */
1189 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1191 memcpy(buf
, tmp_buf
, count
);
1196 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
1197 const void *buf
, int count1
)
1199 uint8_t tmp_buf
[BDRV_SECTOR_SIZE
];
1200 int len
, nb_sectors
, count
;
1205 /* first write to align to sector start */
1206 len
= (BDRV_SECTOR_SIZE
- offset
) & (BDRV_SECTOR_SIZE
- 1);
1209 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1211 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1213 memcpy(tmp_buf
+ (offset
& (BDRV_SECTOR_SIZE
- 1)), buf
, len
);
1214 if ((ret
= bdrv_write(bs
, sector_num
, tmp_buf
, 1)) < 0)
1223 /* write the sectors "in place" */
1224 nb_sectors
= count
>> BDRV_SECTOR_BITS
;
1225 if (nb_sectors
> 0) {
1226 if ((ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
)) < 0)
1228 sector_num
+= nb_sectors
;
1229 len
= nb_sectors
<< BDRV_SECTOR_BITS
;
1234 /* add data from the last sector */
1236 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1238 memcpy(tmp_buf
, buf
, count
);
1239 if ((ret
= bdrv_write(bs
, sector_num
, tmp_buf
, 1)) < 0)
1246 * Writes to the file and ensures that no writes are reordered across this
1247 * request (acts as a barrier)
1249 * Returns 0 on success, -errno in error cases.
1251 int bdrv_pwrite_sync(BlockDriverState
*bs
, int64_t offset
,
1252 const void *buf
, int count
)
1256 ret
= bdrv_pwrite(bs
, offset
, buf
, count
);
1261 /* No flush needed for cache modes that use O_DSYNC */
1262 if ((bs
->open_flags
& BDRV_O_CACHE_WB
) != 0) {
1270 * Handle a read request in coroutine context
1272 static int coroutine_fn
bdrv_co_do_readv(BlockDriverState
*bs
,
1273 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
1275 BlockDriver
*drv
= bs
->drv
;
1280 if (bdrv_check_request(bs
, sector_num
, nb_sectors
)) {
1284 return drv
->bdrv_co_readv(bs
, sector_num
, nb_sectors
, qiov
);
1287 int coroutine_fn
bdrv_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
1288 int nb_sectors
, QEMUIOVector
*qiov
)
1290 trace_bdrv_co_readv(bs
, sector_num
, nb_sectors
);
1292 return bdrv_co_do_readv(bs
, sector_num
, nb_sectors
, qiov
);
1296 * Handle a write request in coroutine context
1298 static int coroutine_fn
bdrv_co_do_writev(BlockDriverState
*bs
,
1299 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
1301 BlockDriver
*drv
= bs
->drv
;
1307 if (bs
->read_only
) {
1310 if (bdrv_check_request(bs
, sector_num
, nb_sectors
)) {
1314 ret
= drv
->bdrv_co_writev(bs
, sector_num
, nb_sectors
, qiov
);
1316 if (bs
->dirty_bitmap
) {
1317 set_dirty_bitmap(bs
, sector_num
, nb_sectors
, 1);
1320 if (bs
->wr_highest_sector
< sector_num
+ nb_sectors
- 1) {
1321 bs
->wr_highest_sector
= sector_num
+ nb_sectors
- 1;
1327 int coroutine_fn
bdrv_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
1328 int nb_sectors
, QEMUIOVector
*qiov
)
1330 trace_bdrv_co_writev(bs
, sector_num
, nb_sectors
);
1332 return bdrv_co_do_writev(bs
, sector_num
, nb_sectors
, qiov
);
1336 * Truncate file to 'offset' bytes (needed only for file protocols)
1338 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
1340 BlockDriver
*drv
= bs
->drv
;
1344 if (!drv
->bdrv_truncate
)
1348 if (bdrv_in_use(bs
))
1350 ret
= drv
->bdrv_truncate(bs
, offset
);
1352 ret
= refresh_total_sectors(bs
, offset
>> BDRV_SECTOR_BITS
);
1353 bdrv_dev_resize_cb(bs
);
1359 * Length of a allocated file in bytes. Sparse files are counted by actual
1360 * allocated space. Return < 0 if error or unknown.
1362 int64_t bdrv_get_allocated_file_size(BlockDriverState
*bs
)
1364 BlockDriver
*drv
= bs
->drv
;
1368 if (drv
->bdrv_get_allocated_file_size
) {
1369 return drv
->bdrv_get_allocated_file_size(bs
);
1372 return bdrv_get_allocated_file_size(bs
->file
);
1378 * Length of a file in bytes. Return < 0 if error or unknown.
1380 int64_t bdrv_getlength(BlockDriverState
*bs
)
1382 BlockDriver
*drv
= bs
->drv
;
1386 if (bs
->growable
|| bdrv_dev_has_removable_media(bs
)) {
1387 if (drv
->bdrv_getlength
) {
1388 return drv
->bdrv_getlength(bs
);
1391 return bs
->total_sectors
* BDRV_SECTOR_SIZE
;
1394 /* return 0 as number of sectors if no device present or error */
1395 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
1398 length
= bdrv_getlength(bs
);
1402 length
= length
>> BDRV_SECTOR_BITS
;
1403 *nb_sectors_ptr
= length
;
1407 uint8_t boot_ind
; /* 0x80 - active */
1408 uint8_t head
; /* starting head */
1409 uint8_t sector
; /* starting sector */
1410 uint8_t cyl
; /* starting cylinder */
1411 uint8_t sys_ind
; /* What partition type */
1412 uint8_t end_head
; /* end head */
1413 uint8_t end_sector
; /* end sector */
1414 uint8_t end_cyl
; /* end cylinder */
1415 uint32_t start_sect
; /* starting sector counting from 0 */
1416 uint32_t nr_sects
; /* nr of sectors in partition */
1419 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1420 static int guess_disk_lchs(BlockDriverState
*bs
,
1421 int *pcylinders
, int *pheads
, int *psectors
)
1423 uint8_t buf
[BDRV_SECTOR_SIZE
];
1424 int ret
, i
, heads
, sectors
, cylinders
;
1425 struct partition
*p
;
1427 uint64_t nb_sectors
;
1429 bdrv_get_geometry(bs
, &nb_sectors
);
1431 ret
= bdrv_read(bs
, 0, buf
, 1);
1434 /* test msdos magic */
1435 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
1437 for(i
= 0; i
< 4; i
++) {
1438 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
1439 nr_sects
= le32_to_cpu(p
->nr_sects
);
1440 if (nr_sects
&& p
->end_head
) {
1441 /* We make the assumption that the partition terminates on
1442 a cylinder boundary */
1443 heads
= p
->end_head
+ 1;
1444 sectors
= p
->end_sector
& 63;
1447 cylinders
= nb_sectors
/ (heads
* sectors
);
1448 if (cylinders
< 1 || cylinders
> 16383)
1451 *psectors
= sectors
;
1452 *pcylinders
= cylinders
;
1454 printf("guessed geometry: LCHS=%d %d %d\n",
1455 cylinders
, heads
, sectors
);
1463 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
1465 int translation
, lba_detected
= 0;
1466 int cylinders
, heads
, secs
;
1467 uint64_t nb_sectors
;
1469 /* if a geometry hint is available, use it */
1470 bdrv_get_geometry(bs
, &nb_sectors
);
1471 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
1472 translation
= bdrv_get_translation_hint(bs
);
1473 if (cylinders
!= 0) {
1478 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
1480 /* if heads > 16, it means that a BIOS LBA
1481 translation was active, so the default
1482 hardware geometry is OK */
1484 goto default_geometry
;
1489 /* disable any translation to be in sync with
1490 the logical geometry */
1491 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
1492 bdrv_set_translation_hint(bs
,
1493 BIOS_ATA_TRANSLATION_NONE
);
1498 /* if no geometry, use a standard physical disk geometry */
1499 cylinders
= nb_sectors
/ (16 * 63);
1501 if (cylinders
> 16383)
1503 else if (cylinders
< 2)
1508 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
1509 if ((*pcyls
* *pheads
) <= 131072) {
1510 bdrv_set_translation_hint(bs
,
1511 BIOS_ATA_TRANSLATION_LARGE
);
1513 bdrv_set_translation_hint(bs
,
1514 BIOS_ATA_TRANSLATION_LBA
);
1518 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
1522 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
1523 int cyls
, int heads
, int secs
)
1530 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
1532 bs
->translation
= translation
;
1535 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
1536 int *pcyls
, int *pheads
, int *psecs
)
1539 *pheads
= bs
->heads
;
1543 /* Recognize floppy formats */
1544 typedef struct FDFormat
{
1551 static const FDFormat fd_formats
[] = {
1552 /* First entry is default format */
1553 /* 1.44 MB 3"1/2 floppy disks */
1554 { FDRIVE_DRV_144
, 18, 80, 1, },
1555 { FDRIVE_DRV_144
, 20, 80, 1, },
1556 { FDRIVE_DRV_144
, 21, 80, 1, },
1557 { FDRIVE_DRV_144
, 21, 82, 1, },
1558 { FDRIVE_DRV_144
, 21, 83, 1, },
1559 { FDRIVE_DRV_144
, 22, 80, 1, },
1560 { FDRIVE_DRV_144
, 23, 80, 1, },
1561 { FDRIVE_DRV_144
, 24, 80, 1, },
1562 /* 2.88 MB 3"1/2 floppy disks */
1563 { FDRIVE_DRV_288
, 36, 80, 1, },
1564 { FDRIVE_DRV_288
, 39, 80, 1, },
1565 { FDRIVE_DRV_288
, 40, 80, 1, },
1566 { FDRIVE_DRV_288
, 44, 80, 1, },
1567 { FDRIVE_DRV_288
, 48, 80, 1, },
1568 /* 720 kB 3"1/2 floppy disks */
1569 { FDRIVE_DRV_144
, 9, 80, 1, },
1570 { FDRIVE_DRV_144
, 10, 80, 1, },
1571 { FDRIVE_DRV_144
, 10, 82, 1, },
1572 { FDRIVE_DRV_144
, 10, 83, 1, },
1573 { FDRIVE_DRV_144
, 13, 80, 1, },
1574 { FDRIVE_DRV_144
, 14, 80, 1, },
1575 /* 1.2 MB 5"1/4 floppy disks */
1576 { FDRIVE_DRV_120
, 15, 80, 1, },
1577 { FDRIVE_DRV_120
, 18, 80, 1, },
1578 { FDRIVE_DRV_120
, 18, 82, 1, },
1579 { FDRIVE_DRV_120
, 18, 83, 1, },
1580 { FDRIVE_DRV_120
, 20, 80, 1, },
1581 /* 720 kB 5"1/4 floppy disks */
1582 { FDRIVE_DRV_120
, 9, 80, 1, },
1583 { FDRIVE_DRV_120
, 11, 80, 1, },
1584 /* 360 kB 5"1/4 floppy disks */
1585 { FDRIVE_DRV_120
, 9, 40, 1, },
1586 { FDRIVE_DRV_120
, 9, 40, 0, },
1587 { FDRIVE_DRV_120
, 10, 41, 1, },
1588 { FDRIVE_DRV_120
, 10, 42, 1, },
1589 /* 320 kB 5"1/4 floppy disks */
1590 { FDRIVE_DRV_120
, 8, 40, 1, },
1591 { FDRIVE_DRV_120
, 8, 40, 0, },
1592 /* 360 kB must match 5"1/4 better than 3"1/2... */
1593 { FDRIVE_DRV_144
, 9, 80, 0, },
1595 { FDRIVE_DRV_NONE
, -1, -1, 0, },
1598 void bdrv_get_floppy_geometry_hint(BlockDriverState
*bs
, int *nb_heads
,
1599 int *max_track
, int *last_sect
,
1600 FDriveType drive_in
, FDriveType
*drive
)
1602 const FDFormat
*parse
;
1603 uint64_t nb_sectors
, size
;
1604 int i
, first_match
, match
;
1606 bdrv_get_geometry_hint(bs
, nb_heads
, max_track
, last_sect
);
1607 if (*nb_heads
!= 0 && *max_track
!= 0 && *last_sect
!= 0) {
1608 /* User defined disk */
1610 bdrv_get_geometry(bs
, &nb_sectors
);
1613 for (i
= 0; ; i
++) {
1614 parse
= &fd_formats
[i
];
1615 if (parse
->drive
== FDRIVE_DRV_NONE
) {
1618 if (drive_in
== parse
->drive
||
1619 drive_in
== FDRIVE_DRV_NONE
) {
1620 size
= (parse
->max_head
+ 1) * parse
->max_track
*
1622 if (nb_sectors
== size
) {
1626 if (first_match
== -1) {
1632 if (first_match
== -1) {
1635 match
= first_match
;
1637 parse
= &fd_formats
[match
];
1639 *nb_heads
= parse
->max_head
+ 1;
1640 *max_track
= parse
->max_track
;
1641 *last_sect
= parse
->last_sect
;
1642 *drive
= parse
->drive
;
1646 int bdrv_get_translation_hint(BlockDriverState
*bs
)
1648 return bs
->translation
;
1651 void bdrv_set_on_error(BlockDriverState
*bs
, BlockErrorAction on_read_error
,
1652 BlockErrorAction on_write_error
)
1654 bs
->on_read_error
= on_read_error
;
1655 bs
->on_write_error
= on_write_error
;
1658 BlockErrorAction
bdrv_get_on_error(BlockDriverState
*bs
, int is_read
)
1660 return is_read
? bs
->on_read_error
: bs
->on_write_error
;
1663 int bdrv_is_read_only(BlockDriverState
*bs
)
1665 return bs
->read_only
;
1668 int bdrv_is_sg(BlockDriverState
*bs
)
1673 int bdrv_enable_write_cache(BlockDriverState
*bs
)
1675 return bs
->enable_write_cache
;
1678 int bdrv_is_encrypted(BlockDriverState
*bs
)
1680 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1682 return bs
->encrypted
;
1685 int bdrv_key_required(BlockDriverState
*bs
)
1687 BlockDriverState
*backing_hd
= bs
->backing_hd
;
1689 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
1691 return (bs
->encrypted
&& !bs
->valid_key
);
1694 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
1697 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
1698 ret
= bdrv_set_key(bs
->backing_hd
, key
);
1704 if (!bs
->encrypted
) {
1706 } else if (!bs
->drv
|| !bs
->drv
->bdrv_set_key
) {
1709 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
1712 } else if (!bs
->valid_key
) {
1714 /* call the change callback now, we skipped it on open */
1715 bdrv_dev_change_media_cb(bs
, true);
1720 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
1725 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
1729 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
1734 QLIST_FOREACH(drv
, &bdrv_drivers
, list
) {
1735 it(opaque
, drv
->format_name
);
1739 BlockDriverState
*bdrv_find(const char *name
)
1741 BlockDriverState
*bs
;
1743 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1744 if (!strcmp(name
, bs
->device_name
)) {
1751 BlockDriverState
*bdrv_next(BlockDriverState
*bs
)
1754 return QTAILQ_FIRST(&bdrv_states
);
1756 return QTAILQ_NEXT(bs
, list
);
1759 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1761 BlockDriverState
*bs
;
1763 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1768 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1770 return bs
->device_name
;
1773 int bdrv_flush(BlockDriverState
*bs
)
1775 if (bs
->open_flags
& BDRV_O_NO_FLUSH
) {
1779 if (bs
->drv
&& bdrv_has_async_flush(bs
->drv
) && qemu_in_coroutine()) {
1780 return bdrv_co_flush_em(bs
);
1783 if (bs
->drv
&& bs
->drv
->bdrv_flush
) {
1784 return bs
->drv
->bdrv_flush(bs
);
1788 * Some block drivers always operate in either writethrough or unsafe mode
1789 * and don't support bdrv_flush therefore. Usually qemu doesn't know how
1790 * the server works (because the behaviour is hardcoded or depends on
1791 * server-side configuration), so we can't ensure that everything is safe
1792 * on disk. Returning an error doesn't work because that would break guests
1793 * even if the server operates in writethrough mode.
1795 * Let's hope the user knows what he's doing.
1800 void bdrv_flush_all(void)
1802 BlockDriverState
*bs
;
1804 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1805 if (!bdrv_is_read_only(bs
) && bdrv_is_inserted(bs
)) {
1811 int bdrv_has_zero_init(BlockDriverState
*bs
)
1815 if (bs
->drv
->bdrv_has_zero_init
) {
1816 return bs
->drv
->bdrv_has_zero_init(bs
);
1822 int bdrv_discard(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
)
1827 if (!bs
->drv
->bdrv_discard
) {
1830 return bs
->drv
->bdrv_discard(bs
, sector_num
, nb_sectors
);
1834 * Returns true iff the specified sector is present in the disk image. Drivers
1835 * not implementing the functionality are assumed to not support backing files,
1836 * hence all their sectors are reported as allocated.
1838 * 'pnum' is set to the number of sectors (including and immediately following
1839 * the specified sector) that are known to be in the same
1840 * allocated/unallocated state.
1842 * 'nb_sectors' is the max value 'pnum' should be set to.
1844 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1848 if (!bs
->drv
->bdrv_is_allocated
) {
1849 if (sector_num
>= bs
->total_sectors
) {
1853 n
= bs
->total_sectors
- sector_num
;
1854 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1857 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1860 void bdrv_mon_event(const BlockDriverState
*bdrv
,
1861 BlockMonEventAction action
, int is_read
)
1864 const char *action_str
;
1867 case BDRV_ACTION_REPORT
:
1868 action_str
= "report";
1870 case BDRV_ACTION_IGNORE
:
1871 action_str
= "ignore";
1873 case BDRV_ACTION_STOP
:
1874 action_str
= "stop";
1880 data
= qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1883 is_read
? "read" : "write");
1884 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR
, data
);
1886 qobject_decref(data
);
1889 static void bdrv_print_dict(QObject
*obj
, void *opaque
)
1892 Monitor
*mon
= opaque
;
1894 bs_dict
= qobject_to_qdict(obj
);
1896 monitor_printf(mon
, "%s: removable=%d",
1897 qdict_get_str(bs_dict
, "device"),
1898 qdict_get_bool(bs_dict
, "removable"));
1900 if (qdict_get_bool(bs_dict
, "removable")) {
1901 monitor_printf(mon
, " locked=%d", qdict_get_bool(bs_dict
, "locked"));
1902 monitor_printf(mon
, " tray-open=%d",
1903 qdict_get_bool(bs_dict
, "tray-open"));
1906 if (qdict_haskey(bs_dict
, "io-status")) {
1907 monitor_printf(mon
, " io-status=%s", qdict_get_str(bs_dict
, "io-status"));
1910 if (qdict_haskey(bs_dict
, "inserted")) {
1911 QDict
*qdict
= qobject_to_qdict(qdict_get(bs_dict
, "inserted"));
1913 monitor_printf(mon
, " file=");
1914 monitor_print_filename(mon
, qdict_get_str(qdict
, "file"));
1915 if (qdict_haskey(qdict
, "backing_file")) {
1916 monitor_printf(mon
, " backing_file=");
1917 monitor_print_filename(mon
, qdict_get_str(qdict
, "backing_file"));
1919 monitor_printf(mon
, " ro=%d drv=%s encrypted=%d",
1920 qdict_get_bool(qdict
, "ro"),
1921 qdict_get_str(qdict
, "drv"),
1922 qdict_get_bool(qdict
, "encrypted"));
1924 monitor_printf(mon
, " [not inserted]");
1927 monitor_printf(mon
, "\n");
1930 void bdrv_info_print(Monitor
*mon
, const QObject
*data
)
1932 qlist_iter(qobject_to_qlist(data
), bdrv_print_dict
, mon
);
1935 static const char *const io_status_name
[BDRV_IOS_MAX
] = {
1936 [BDRV_IOS_OK
] = "ok",
1937 [BDRV_IOS_FAILED
] = "failed",
1938 [BDRV_IOS_ENOSPC
] = "nospace",
1941 void bdrv_info(Monitor
*mon
, QObject
**ret_data
)
1944 BlockDriverState
*bs
;
1946 bs_list
= qlist_new();
1948 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1952 bs_obj
= qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
1953 "'removable': %i, 'locked': %i }",
1955 bdrv_dev_has_removable_media(bs
),
1956 bdrv_dev_is_medium_locked(bs
));
1957 bs_dict
= qobject_to_qdict(bs_obj
);
1959 if (bdrv_dev_has_removable_media(bs
)) {
1960 qdict_put(bs_dict
, "tray-open",
1961 qbool_from_int(bdrv_dev_is_tray_open(bs
)));
1964 if (bdrv_iostatus_is_enabled(bs
)) {
1965 qdict_put(bs_dict
, "io-status",
1966 qstring_from_str(io_status_name
[bs
->iostatus
]));
1972 obj
= qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1973 "'encrypted': %i }",
1974 bs
->filename
, bs
->read_only
,
1975 bs
->drv
->format_name
,
1976 bdrv_is_encrypted(bs
));
1977 if (bs
->backing_file
[0] != '\0') {
1978 QDict
*qdict
= qobject_to_qdict(obj
);
1979 qdict_put(qdict
, "backing_file",
1980 qstring_from_str(bs
->backing_file
));
1983 qdict_put_obj(bs_dict
, "inserted", obj
);
1985 qlist_append_obj(bs_list
, bs_obj
);
1988 *ret_data
= QOBJECT(bs_list
);
1991 static void bdrv_stats_iter(QObject
*data
, void *opaque
)
1994 Monitor
*mon
= opaque
;
1996 qdict
= qobject_to_qdict(data
);
1997 monitor_printf(mon
, "%s:", qdict_get_str(qdict
, "device"));
1999 qdict
= qobject_to_qdict(qdict_get(qdict
, "stats"));
2000 monitor_printf(mon
, " rd_bytes=%" PRId64
2001 " wr_bytes=%" PRId64
2002 " rd_operations=%" PRId64
2003 " wr_operations=%" PRId64
2004 " flush_operations=%" PRId64
2005 " wr_total_time_ns=%" PRId64
2006 " rd_total_time_ns=%" PRId64
2007 " flush_total_time_ns=%" PRId64
2009 qdict_get_int(qdict
, "rd_bytes"),
2010 qdict_get_int(qdict
, "wr_bytes"),
2011 qdict_get_int(qdict
, "rd_operations"),
2012 qdict_get_int(qdict
, "wr_operations"),
2013 qdict_get_int(qdict
, "flush_operations"),
2014 qdict_get_int(qdict
, "wr_total_time_ns"),
2015 qdict_get_int(qdict
, "rd_total_time_ns"),
2016 qdict_get_int(qdict
, "flush_total_time_ns"));
2019 void bdrv_stats_print(Monitor
*mon
, const QObject
*data
)
2021 qlist_iter(qobject_to_qlist(data
), bdrv_stats_iter
, mon
);
2024 static QObject
* bdrv_info_stats_bs(BlockDriverState
*bs
)
2029 res
= qobject_from_jsonf("{ 'stats': {"
2030 "'rd_bytes': %" PRId64
","
2031 "'wr_bytes': %" PRId64
","
2032 "'rd_operations': %" PRId64
","
2033 "'wr_operations': %" PRId64
","
2034 "'wr_highest_offset': %" PRId64
","
2035 "'flush_operations': %" PRId64
","
2036 "'wr_total_time_ns': %" PRId64
","
2037 "'rd_total_time_ns': %" PRId64
","
2038 "'flush_total_time_ns': %" PRId64
2040 bs
->nr_bytes
[BDRV_ACCT_READ
],
2041 bs
->nr_bytes
[BDRV_ACCT_WRITE
],
2042 bs
->nr_ops
[BDRV_ACCT_READ
],
2043 bs
->nr_ops
[BDRV_ACCT_WRITE
],
2044 bs
->wr_highest_sector
*
2045 (uint64_t)BDRV_SECTOR_SIZE
,
2046 bs
->nr_ops
[BDRV_ACCT_FLUSH
],
2047 bs
->total_time_ns
[BDRV_ACCT_WRITE
],
2048 bs
->total_time_ns
[BDRV_ACCT_READ
],
2049 bs
->total_time_ns
[BDRV_ACCT_FLUSH
]);
2050 dict
= qobject_to_qdict(res
);
2052 if (*bs
->device_name
) {
2053 qdict_put(dict
, "device", qstring_from_str(bs
->device_name
));
2057 QObject
*parent
= bdrv_info_stats_bs(bs
->file
);
2058 qdict_put_obj(dict
, "parent", parent
);
2064 void bdrv_info_stats(Monitor
*mon
, QObject
**ret_data
)
2068 BlockDriverState
*bs
;
2070 devices
= qlist_new();
2072 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
2073 obj
= bdrv_info_stats_bs(bs
);
2074 qlist_append_obj(devices
, obj
);
2077 *ret_data
= QOBJECT(devices
);
2080 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
2082 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
2083 return bs
->backing_file
;
2084 else if (bs
->encrypted
)
2085 return bs
->filename
;
2090 void bdrv_get_backing_filename(BlockDriverState
*bs
,
2091 char *filename
, int filename_size
)
2093 if (!bs
->backing_file
) {
2094 pstrcpy(filename
, filename_size
, "");
2096 pstrcpy(filename
, filename_size
, bs
->backing_file
);
2100 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
2101 const uint8_t *buf
, int nb_sectors
)
2103 BlockDriver
*drv
= bs
->drv
;
2106 if (!drv
->bdrv_write_compressed
)
2108 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
2111 if (bs
->dirty_bitmap
) {
2112 set_dirty_bitmap(bs
, sector_num
, nb_sectors
, 1);
2115 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
2118 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2120 BlockDriver
*drv
= bs
->drv
;
2123 if (!drv
->bdrv_get_info
)
2125 memset(bdi
, 0, sizeof(*bdi
));
2126 return drv
->bdrv_get_info(bs
, bdi
);
2129 int bdrv_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
2130 int64_t pos
, int size
)
2132 BlockDriver
*drv
= bs
->drv
;
2135 if (drv
->bdrv_save_vmstate
)
2136 return drv
->bdrv_save_vmstate(bs
, buf
, pos
, size
);
2138 return bdrv_save_vmstate(bs
->file
, buf
, pos
, size
);
2142 int bdrv_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
2143 int64_t pos
, int size
)
2145 BlockDriver
*drv
= bs
->drv
;
2148 if (drv
->bdrv_load_vmstate
)
2149 return drv
->bdrv_load_vmstate(bs
, buf
, pos
, size
);
2151 return bdrv_load_vmstate(bs
->file
, buf
, pos
, size
);
2155 void bdrv_debug_event(BlockDriverState
*bs
, BlkDebugEvent event
)
2157 BlockDriver
*drv
= bs
->drv
;
2159 if (!drv
|| !drv
->bdrv_debug_event
) {
2163 return drv
->bdrv_debug_event(bs
, event
);
2167 /**************************************************************/
2168 /* handling of snapshots */
2170 int bdrv_can_snapshot(BlockDriverState
*bs
)
2172 BlockDriver
*drv
= bs
->drv
;
2173 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2177 if (!drv
->bdrv_snapshot_create
) {
2178 if (bs
->file
!= NULL
) {
2179 return bdrv_can_snapshot(bs
->file
);
2187 int bdrv_is_snapshot(BlockDriverState
*bs
)
2189 return !!(bs
->open_flags
& BDRV_O_SNAPSHOT
);
2192 BlockDriverState
*bdrv_snapshots(void)
2194 BlockDriverState
*bs
;
2197 return bs_snapshots
;
2201 while ((bs
= bdrv_next(bs
))) {
2202 if (bdrv_can_snapshot(bs
)) {
2210 int bdrv_snapshot_create(BlockDriverState
*bs
,
2211 QEMUSnapshotInfo
*sn_info
)
2213 BlockDriver
*drv
= bs
->drv
;
2216 if (drv
->bdrv_snapshot_create
)
2217 return drv
->bdrv_snapshot_create(bs
, sn_info
);
2219 return bdrv_snapshot_create(bs
->file
, sn_info
);
2223 int bdrv_snapshot_goto(BlockDriverState
*bs
,
2224 const char *snapshot_id
)
2226 BlockDriver
*drv
= bs
->drv
;
2231 if (drv
->bdrv_snapshot_goto
)
2232 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
2235 drv
->bdrv_close(bs
);
2236 ret
= bdrv_snapshot_goto(bs
->file
, snapshot_id
);
2237 open_ret
= drv
->bdrv_open(bs
, bs
->open_flags
);
2239 bdrv_delete(bs
->file
);
2249 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
2251 BlockDriver
*drv
= bs
->drv
;
2254 if (drv
->bdrv_snapshot_delete
)
2255 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
2257 return bdrv_snapshot_delete(bs
->file
, snapshot_id
);
2261 int bdrv_snapshot_list(BlockDriverState
*bs
,
2262 QEMUSnapshotInfo
**psn_info
)
2264 BlockDriver
*drv
= bs
->drv
;
2267 if (drv
->bdrv_snapshot_list
)
2268 return drv
->bdrv_snapshot_list(bs
, psn_info
);
2270 return bdrv_snapshot_list(bs
->file
, psn_info
);
2274 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
2275 const char *snapshot_name
)
2277 BlockDriver
*drv
= bs
->drv
;
2281 if (!bs
->read_only
) {
2284 if (drv
->bdrv_snapshot_load_tmp
) {
2285 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_name
);
2290 #define NB_SUFFIXES 4
2292 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
2294 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
2299 snprintf(buf
, buf_size
, "%" PRId64
, size
);
2302 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
2303 if (size
< (10 * base
)) {
2304 snprintf(buf
, buf_size
, "%0.1f%c",
2305 (double)size
/ base
,
2308 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
2309 snprintf(buf
, buf_size
, "%" PRId64
"%c",
2310 ((size
+ (base
>> 1)) / base
),
2320 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
2322 char buf1
[128], date_buf
[128], clock_buf
[128];
2332 snprintf(buf
, buf_size
,
2333 "%-10s%-20s%7s%20s%15s",
2334 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
2338 ptm
= localtime(&ti
);
2339 strftime(date_buf
, sizeof(date_buf
),
2340 "%Y-%m-%d %H:%M:%S", ptm
);
2342 localtime_r(&ti
, &tm
);
2343 strftime(date_buf
, sizeof(date_buf
),
2344 "%Y-%m-%d %H:%M:%S", &tm
);
2346 secs
= sn
->vm_clock_nsec
/ 1000000000;
2347 snprintf(clock_buf
, sizeof(clock_buf
),
2348 "%02d:%02d:%02d.%03d",
2350 (int)((secs
/ 60) % 60),
2352 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
2353 snprintf(buf
, buf_size
,
2354 "%-10s%-20s%7s%20s%15s",
2355 sn
->id_str
, sn
->name
,
2356 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
2363 /**************************************************************/
2366 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
2367 QEMUIOVector
*qiov
, int nb_sectors
,
2368 BlockDriverCompletionFunc
*cb
, void *opaque
)
2370 trace_bdrv_aio_readv(bs
, sector_num
, nb_sectors
, opaque
);
2372 return bdrv_co_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
,
2376 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
2377 QEMUIOVector
*qiov
, int nb_sectors
,
2378 BlockDriverCompletionFunc
*cb
, void *opaque
)
2380 trace_bdrv_aio_writev(bs
, sector_num
, nb_sectors
, opaque
);
2382 return bdrv_co_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
,
2387 typedef struct MultiwriteCB
{
2392 BlockDriverCompletionFunc
*cb
;
2394 QEMUIOVector
*free_qiov
;
2399 static void multiwrite_user_cb(MultiwriteCB
*mcb
)
2403 for (i
= 0; i
< mcb
->num_callbacks
; i
++) {
2404 mcb
->callbacks
[i
].cb(mcb
->callbacks
[i
].opaque
, mcb
->error
);
2405 if (mcb
->callbacks
[i
].free_qiov
) {
2406 qemu_iovec_destroy(mcb
->callbacks
[i
].free_qiov
);
2408 g_free(mcb
->callbacks
[i
].free_qiov
);
2409 qemu_vfree(mcb
->callbacks
[i
].free_buf
);
2413 static void multiwrite_cb(void *opaque
, int ret
)
2415 MultiwriteCB
*mcb
= opaque
;
2417 trace_multiwrite_cb(mcb
, ret
);
2419 if (ret
< 0 && !mcb
->error
) {
2423 mcb
->num_requests
--;
2424 if (mcb
->num_requests
== 0) {
2425 multiwrite_user_cb(mcb
);
2430 static int multiwrite_req_compare(const void *a
, const void *b
)
2432 const BlockRequest
*req1
= a
, *req2
= b
;
2435 * Note that we can't simply subtract req2->sector from req1->sector
2436 * here as that could overflow the return value.
2438 if (req1
->sector
> req2
->sector
) {
2440 } else if (req1
->sector
< req2
->sector
) {
2448 * Takes a bunch of requests and tries to merge them. Returns the number of
2449 * requests that remain after merging.
2451 static int multiwrite_merge(BlockDriverState
*bs
, BlockRequest
*reqs
,
2452 int num_reqs
, MultiwriteCB
*mcb
)
2456 // Sort requests by start sector
2457 qsort(reqs
, num_reqs
, sizeof(*reqs
), &multiwrite_req_compare
);
2459 // Check if adjacent requests touch the same clusters. If so, combine them,
2460 // filling up gaps with zero sectors.
2462 for (i
= 1; i
< num_reqs
; i
++) {
2464 int64_t oldreq_last
= reqs
[outidx
].sector
+ reqs
[outidx
].nb_sectors
;
2466 // This handles the cases that are valid for all block drivers, namely
2467 // exactly sequential writes and overlapping writes.
2468 if (reqs
[i
].sector
<= oldreq_last
) {
2472 // The block driver may decide that it makes sense to combine requests
2473 // even if there is a gap of some sectors between them. In this case,
2474 // the gap is filled with zeros (therefore only applicable for yet
2475 // unused space in format like qcow2).
2476 if (!merge
&& bs
->drv
->bdrv_merge_requests
) {
2477 merge
= bs
->drv
->bdrv_merge_requests(bs
, &reqs
[outidx
], &reqs
[i
]);
2480 if (reqs
[outidx
].qiov
->niov
+ reqs
[i
].qiov
->niov
+ 1 > IOV_MAX
) {
2486 QEMUIOVector
*qiov
= g_malloc0(sizeof(*qiov
));
2487 qemu_iovec_init(qiov
,
2488 reqs
[outidx
].qiov
->niov
+ reqs
[i
].qiov
->niov
+ 1);
2490 // Add the first request to the merged one. If the requests are
2491 // overlapping, drop the last sectors of the first request.
2492 size
= (reqs
[i
].sector
- reqs
[outidx
].sector
) << 9;
2493 qemu_iovec_concat(qiov
, reqs
[outidx
].qiov
, size
);
2495 // We might need to add some zeros between the two requests
2496 if (reqs
[i
].sector
> oldreq_last
) {
2497 size_t zero_bytes
= (reqs
[i
].sector
- oldreq_last
) << 9;
2498 uint8_t *buf
= qemu_blockalign(bs
, zero_bytes
);
2499 memset(buf
, 0, zero_bytes
);
2500 qemu_iovec_add(qiov
, buf
, zero_bytes
);
2501 mcb
->callbacks
[i
].free_buf
= buf
;
2504 // Add the second request
2505 qemu_iovec_concat(qiov
, reqs
[i
].qiov
, reqs
[i
].qiov
->size
);
2507 reqs
[outidx
].nb_sectors
= qiov
->size
>> 9;
2508 reqs
[outidx
].qiov
= qiov
;
2510 mcb
->callbacks
[i
].free_qiov
= reqs
[outidx
].qiov
;
2513 reqs
[outidx
].sector
= reqs
[i
].sector
;
2514 reqs
[outidx
].nb_sectors
= reqs
[i
].nb_sectors
;
2515 reqs
[outidx
].qiov
= reqs
[i
].qiov
;
2523 * Submit multiple AIO write requests at once.
2525 * On success, the function returns 0 and all requests in the reqs array have
2526 * been submitted. In error case this function returns -1, and any of the
2527 * requests may or may not be submitted yet. In particular, this means that the
2528 * callback will be called for some of the requests, for others it won't. The
2529 * caller must check the error field of the BlockRequest to wait for the right
2530 * callbacks (if error != 0, no callback will be called).
2532 * The implementation may modify the contents of the reqs array, e.g. to merge
2533 * requests. However, the fields opaque and error are left unmodified as they
2534 * are used to signal failure for a single request to the caller.
2536 int bdrv_aio_multiwrite(BlockDriverState
*bs
, BlockRequest
*reqs
, int num_reqs
)
2538 BlockDriverAIOCB
*acb
;
2542 /* don't submit writes if we don't have a medium */
2543 if (bs
->drv
== NULL
) {
2544 for (i
= 0; i
< num_reqs
; i
++) {
2545 reqs
[i
].error
= -ENOMEDIUM
;
2550 if (num_reqs
== 0) {
2554 // Create MultiwriteCB structure
2555 mcb
= g_malloc0(sizeof(*mcb
) + num_reqs
* sizeof(*mcb
->callbacks
));
2556 mcb
->num_requests
= 0;
2557 mcb
->num_callbacks
= num_reqs
;
2559 for (i
= 0; i
< num_reqs
; i
++) {
2560 mcb
->callbacks
[i
].cb
= reqs
[i
].cb
;
2561 mcb
->callbacks
[i
].opaque
= reqs
[i
].opaque
;
2564 // Check for mergable requests
2565 num_reqs
= multiwrite_merge(bs
, reqs
, num_reqs
, mcb
);
2567 trace_bdrv_aio_multiwrite(mcb
, mcb
->num_callbacks
, num_reqs
);
2570 * Run the aio requests. As soon as one request can't be submitted
2571 * successfully, fail all requests that are not yet submitted (we must
2572 * return failure for all requests anyway)
2574 * num_requests cannot be set to the right value immediately: If
2575 * bdrv_aio_writev fails for some request, num_requests would be too high
2576 * and therefore multiwrite_cb() would never recognize the multiwrite
2577 * request as completed. We also cannot use the loop variable i to set it
2578 * when the first request fails because the callback may already have been
2579 * called for previously submitted requests. Thus, num_requests must be
2580 * incremented for each request that is submitted.
2582 * The problem that callbacks may be called early also means that we need
2583 * to take care that num_requests doesn't become 0 before all requests are
2584 * submitted - multiwrite_cb() would consider the multiwrite request
2585 * completed. A dummy request that is "completed" by a manual call to
2586 * multiwrite_cb() takes care of this.
2588 mcb
->num_requests
= 1;
2590 // Run the aio requests
2591 for (i
= 0; i
< num_reqs
; i
++) {
2592 mcb
->num_requests
++;
2593 acb
= bdrv_aio_writev(bs
, reqs
[i
].sector
, reqs
[i
].qiov
,
2594 reqs
[i
].nb_sectors
, multiwrite_cb
, mcb
);
2597 // We can only fail the whole thing if no request has been
2598 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2599 // complete and report the error in the callback.
2601 trace_bdrv_aio_multiwrite_earlyfail(mcb
);
2604 trace_bdrv_aio_multiwrite_latefail(mcb
, i
);
2605 multiwrite_cb(mcb
, -EIO
);
2611 /* Complete the dummy request */
2612 multiwrite_cb(mcb
, 0);
2617 for (i
= 0; i
< mcb
->num_callbacks
; i
++) {
2618 reqs
[i
].error
= -EIO
;
2624 BlockDriverAIOCB
*bdrv_aio_flush(BlockDriverState
*bs
,
2625 BlockDriverCompletionFunc
*cb
, void *opaque
)
2627 BlockDriver
*drv
= bs
->drv
;
2629 trace_bdrv_aio_flush(bs
, opaque
);
2631 if (bs
->open_flags
& BDRV_O_NO_FLUSH
) {
2632 return bdrv_aio_noop_em(bs
, cb
, opaque
);
2637 return drv
->bdrv_aio_flush(bs
, cb
, opaque
);
2640 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
2642 acb
->pool
->cancel(acb
);
2646 /**************************************************************/
2647 /* async block device emulation */
2649 typedef struct BlockDriverAIOCBSync
{
2650 BlockDriverAIOCB common
;
2653 /* vector translation state */
2657 } BlockDriverAIOCBSync
;
2659 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
2661 BlockDriverAIOCBSync
*acb
=
2662 container_of(blockacb
, BlockDriverAIOCBSync
, common
);
2663 qemu_bh_delete(acb
->bh
);
2665 qemu_aio_release(acb
);
2668 static AIOPool bdrv_em_aio_pool
= {
2669 .aiocb_size
= sizeof(BlockDriverAIOCBSync
),
2670 .cancel
= bdrv_aio_cancel_em
,
2673 static void bdrv_aio_bh_cb(void *opaque
)
2675 BlockDriverAIOCBSync
*acb
= opaque
;
2678 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
2679 qemu_vfree(acb
->bounce
);
2680 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
2681 qemu_bh_delete(acb
->bh
);
2683 qemu_aio_release(acb
);
2686 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
2690 BlockDriverCompletionFunc
*cb
,
2695 BlockDriverAIOCBSync
*acb
;
2697 acb
= qemu_aio_get(&bdrv_em_aio_pool
, bs
, cb
, opaque
);
2698 acb
->is_write
= is_write
;
2700 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
2703 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
2706 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
2707 acb
->ret
= bs
->drv
->bdrv_write(bs
, sector_num
, acb
->bounce
, nb_sectors
);
2709 acb
->ret
= bs
->drv
->bdrv_read(bs
, sector_num
, acb
->bounce
, nb_sectors
);
2712 qemu_bh_schedule(acb
->bh
);
2714 return &acb
->common
;
2717 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
2718 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
2719 BlockDriverCompletionFunc
*cb
, void *opaque
)
2721 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
2724 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
2725 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
2726 BlockDriverCompletionFunc
*cb
, void *opaque
)
2728 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
2732 typedef struct BlockDriverAIOCBCoroutine
{
2733 BlockDriverAIOCB common
;
2737 } BlockDriverAIOCBCoroutine
;
2739 static void bdrv_aio_co_cancel_em(BlockDriverAIOCB
*blockacb
)
2744 static AIOPool bdrv_em_co_aio_pool
= {
2745 .aiocb_size
= sizeof(BlockDriverAIOCBCoroutine
),
2746 .cancel
= bdrv_aio_co_cancel_em
,
2749 static void bdrv_co_rw_bh(void *opaque
)
2751 BlockDriverAIOCBCoroutine
*acb
= opaque
;
2753 acb
->common
.cb(acb
->common
.opaque
, acb
->req
.error
);
2754 qemu_bh_delete(acb
->bh
);
2755 qemu_aio_release(acb
);
2758 /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
2759 static void coroutine_fn
bdrv_co_do_rw(void *opaque
)
2761 BlockDriverAIOCBCoroutine
*acb
= opaque
;
2762 BlockDriverState
*bs
= acb
->common
.bs
;
2764 if (!acb
->is_write
) {
2765 acb
->req
.error
= bdrv_co_do_readv(bs
, acb
->req
.sector
,
2766 acb
->req
.nb_sectors
, acb
->req
.qiov
);
2768 acb
->req
.error
= bdrv_co_do_writev(bs
, acb
->req
.sector
,
2769 acb
->req
.nb_sectors
, acb
->req
.qiov
);
2772 acb
->bh
= qemu_bh_new(bdrv_co_rw_bh
, acb
);
2773 qemu_bh_schedule(acb
->bh
);
2776 static BlockDriverAIOCB
*bdrv_co_aio_rw_vector(BlockDriverState
*bs
,
2780 BlockDriverCompletionFunc
*cb
,
2785 BlockDriverAIOCBCoroutine
*acb
;
2787 acb
= qemu_aio_get(&bdrv_em_co_aio_pool
, bs
, cb
, opaque
);
2788 acb
->req
.sector
= sector_num
;
2789 acb
->req
.nb_sectors
= nb_sectors
;
2790 acb
->req
.qiov
= qiov
;
2791 acb
->is_write
= is_write
;
2793 co
= qemu_coroutine_create(bdrv_co_do_rw
);
2794 qemu_coroutine_enter(co
, acb
);
2796 return &acb
->common
;
2799 static BlockDriverAIOCB
*bdrv_aio_flush_em(BlockDriverState
*bs
,
2800 BlockDriverCompletionFunc
*cb
, void *opaque
)
2802 BlockDriverAIOCBSync
*acb
;
2804 acb
= qemu_aio_get(&bdrv_em_aio_pool
, bs
, cb
, opaque
);
2805 acb
->is_write
= 1; /* don't bounce in the completion hadler */
2811 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
2814 qemu_bh_schedule(acb
->bh
);
2815 return &acb
->common
;
2818 static BlockDriverAIOCB
*bdrv_aio_noop_em(BlockDriverState
*bs
,
2819 BlockDriverCompletionFunc
*cb
, void *opaque
)
2821 BlockDriverAIOCBSync
*acb
;
2823 acb
= qemu_aio_get(&bdrv_em_aio_pool
, bs
, cb
, opaque
);
2824 acb
->is_write
= 1; /* don't bounce in the completion handler */
2830 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
2833 qemu_bh_schedule(acb
->bh
);
2834 return &acb
->common
;
2837 /**************************************************************/
2838 /* sync block device emulation */
2840 static void bdrv_rw_em_cb(void *opaque
, int ret
)
2842 *(int *)opaque
= ret
;
2845 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
2846 uint8_t *buf
, int nb_sectors
)
2849 BlockDriverAIOCB
*acb
;
2853 async_ret
= NOT_DONE
;
2854 iov
.iov_base
= (void *)buf
;
2855 iov
.iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
;
2856 qemu_iovec_init_external(&qiov
, &iov
, 1);
2858 acb
= bs
->drv
->bdrv_aio_readv(bs
, sector_num
, &qiov
, nb_sectors
,
2859 bdrv_rw_em_cb
, &async_ret
);
2865 while (async_ret
== NOT_DONE
) {
2874 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
2875 const uint8_t *buf
, int nb_sectors
)
2878 BlockDriverAIOCB
*acb
;
2882 async_ret
= NOT_DONE
;
2883 iov
.iov_base
= (void *)buf
;
2884 iov
.iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
;
2885 qemu_iovec_init_external(&qiov
, &iov
, 1);
2887 acb
= bs
->drv
->bdrv_aio_writev(bs
, sector_num
, &qiov
, nb_sectors
,
2888 bdrv_rw_em_cb
, &async_ret
);
2893 while (async_ret
== NOT_DONE
) {
2901 void bdrv_init(void)
2903 module_call_init(MODULE_INIT_BLOCK
);
2906 void bdrv_init_with_whitelist(void)
2908 use_bdrv_whitelist
= 1;
2912 void *qemu_aio_get(AIOPool
*pool
, BlockDriverState
*bs
,
2913 BlockDriverCompletionFunc
*cb
, void *opaque
)
2915 BlockDriverAIOCB
*acb
;
2917 if (pool
->free_aiocb
) {
2918 acb
= pool
->free_aiocb
;
2919 pool
->free_aiocb
= acb
->next
;
2921 acb
= g_malloc0(pool
->aiocb_size
);
2926 acb
->opaque
= opaque
;
2930 void qemu_aio_release(void *p
)
2932 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
2933 AIOPool
*pool
= acb
->pool
;
2934 acb
->next
= pool
->free_aiocb
;
2935 pool
->free_aiocb
= acb
;
2938 /**************************************************************/
2939 /* Coroutine block device emulation */
2941 typedef struct CoroutineIOCompletion
{
2942 Coroutine
*coroutine
;
2944 } CoroutineIOCompletion
;
2946 static void bdrv_co_io_em_complete(void *opaque
, int ret
)
2948 CoroutineIOCompletion
*co
= opaque
;
2951 qemu_coroutine_enter(co
->coroutine
, NULL
);
2954 static int coroutine_fn
bdrv_co_io_em(BlockDriverState
*bs
, int64_t sector_num
,
2955 int nb_sectors
, QEMUIOVector
*iov
,
2958 CoroutineIOCompletion co
= {
2959 .coroutine
= qemu_coroutine_self(),
2961 BlockDriverAIOCB
*acb
;
2964 acb
= bs
->drv
->bdrv_aio_writev(bs
, sector_num
, iov
, nb_sectors
,
2965 bdrv_co_io_em_complete
, &co
);
2967 acb
= bs
->drv
->bdrv_aio_readv(bs
, sector_num
, iov
, nb_sectors
,
2968 bdrv_co_io_em_complete
, &co
);
2971 trace_bdrv_co_io_em(bs
, sector_num
, nb_sectors
, is_write
, acb
);
2975 qemu_coroutine_yield();
2980 static int coroutine_fn
bdrv_co_readv_em(BlockDriverState
*bs
,
2981 int64_t sector_num
, int nb_sectors
,
2984 return bdrv_co_io_em(bs
, sector_num
, nb_sectors
, iov
, false);
2987 static int coroutine_fn
bdrv_co_writev_em(BlockDriverState
*bs
,
2988 int64_t sector_num
, int nb_sectors
,
2991 return bdrv_co_io_em(bs
, sector_num
, nb_sectors
, iov
, true);
2994 static int coroutine_fn
bdrv_co_flush_em(BlockDriverState
*bs
)
2996 CoroutineIOCompletion co
= {
2997 .coroutine
= qemu_coroutine_self(),
2999 BlockDriverAIOCB
*acb
;
3001 acb
= bdrv_aio_flush(bs
, bdrv_co_io_em_complete
, &co
);
3005 qemu_coroutine_yield();
3009 /**************************************************************/
3010 /* removable device support */
3013 * Return TRUE if the media is present
3015 int bdrv_is_inserted(BlockDriverState
*bs
)
3017 BlockDriver
*drv
= bs
->drv
;
3021 if (!drv
->bdrv_is_inserted
)
3023 return drv
->bdrv_is_inserted(bs
);
3027 * Return whether the media changed since the last call to this
3028 * function, or -ENOTSUP if we don't know. Most drivers don't know.
3030 int bdrv_media_changed(BlockDriverState
*bs
)
3032 BlockDriver
*drv
= bs
->drv
;
3034 if (drv
&& drv
->bdrv_media_changed
) {
3035 return drv
->bdrv_media_changed(bs
);
3041 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3043 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
3045 BlockDriver
*drv
= bs
->drv
;
3047 if (drv
&& drv
->bdrv_eject
) {
3048 drv
->bdrv_eject(bs
, eject_flag
);
3053 * Lock or unlock the media (if it is locked, the user won't be able
3054 * to eject it manually).
3056 void bdrv_lock_medium(BlockDriverState
*bs
, bool locked
)
3058 BlockDriver
*drv
= bs
->drv
;
3060 trace_bdrv_lock_medium(bs
, locked
);
3062 if (drv
&& drv
->bdrv_lock_medium
) {
3063 drv
->bdrv_lock_medium(bs
, locked
);
3067 /* needed for generic scsi interface */
3069 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
3071 BlockDriver
*drv
= bs
->drv
;
3073 if (drv
&& drv
->bdrv_ioctl
)
3074 return drv
->bdrv_ioctl(bs
, req
, buf
);
3078 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
3079 unsigned long int req
, void *buf
,
3080 BlockDriverCompletionFunc
*cb
, void *opaque
)
3082 BlockDriver
*drv
= bs
->drv
;
3084 if (drv
&& drv
->bdrv_aio_ioctl
)
3085 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);
3089 void bdrv_set_buffer_alignment(BlockDriverState
*bs
, int align
)
3091 bs
->buffer_alignment
= align
;
3094 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
3096 return qemu_memalign((bs
&& bs
->buffer_alignment
) ? bs
->buffer_alignment
: 512, size
);
3099 void bdrv_set_dirty_tracking(BlockDriverState
*bs
, int enable
)
3101 int64_t bitmap_size
;
3103 bs
->dirty_count
= 0;
3105 if (!bs
->dirty_bitmap
) {
3106 bitmap_size
= (bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
) +
3107 BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
3108 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
3110 bs
->dirty_bitmap
= g_malloc0(bitmap_size
);
3113 if (bs
->dirty_bitmap
) {
3114 g_free(bs
->dirty_bitmap
);
3115 bs
->dirty_bitmap
= NULL
;
3120 int bdrv_get_dirty(BlockDriverState
*bs
, int64_t sector
)
3122 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
3124 if (bs
->dirty_bitmap
&&
3125 (sector
<< BDRV_SECTOR_BITS
) < bdrv_getlength(bs
)) {
3126 return !!(bs
->dirty_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
3127 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
3133 void bdrv_reset_dirty(BlockDriverState
*bs
, int64_t cur_sector
,
3136 set_dirty_bitmap(bs
, cur_sector
, nr_sectors
, 0);
3139 int64_t bdrv_get_dirty_count(BlockDriverState
*bs
)
3141 return bs
->dirty_count
;
3144 void bdrv_set_in_use(BlockDriverState
*bs
, int in_use
)
3146 assert(bs
->in_use
!= in_use
);
3147 bs
->in_use
= in_use
;
3150 int bdrv_in_use(BlockDriverState
*bs
)
3155 void bdrv_iostatus_enable(BlockDriverState
*bs
)
3157 bs
->iostatus
= BDRV_IOS_OK
;
3160 /* The I/O status is only enabled if the drive explicitly
3161 * enables it _and_ the VM is configured to stop on errors */
3162 bool bdrv_iostatus_is_enabled(const BlockDriverState
*bs
)
3164 return (bs
->iostatus
!= BDRV_IOS_INVAL
&&
3165 (bs
->on_write_error
== BLOCK_ERR_STOP_ENOSPC
||
3166 bs
->on_write_error
== BLOCK_ERR_STOP_ANY
||
3167 bs
->on_read_error
== BLOCK_ERR_STOP_ANY
));
3170 void bdrv_iostatus_disable(BlockDriverState
*bs
)
3172 bs
->iostatus
= BDRV_IOS_INVAL
;
3175 void bdrv_iostatus_reset(BlockDriverState
*bs
)
3177 if (bdrv_iostatus_is_enabled(bs
)) {
3178 bs
->iostatus
= BDRV_IOS_OK
;
3182 /* XXX: Today this is set by device models because it makes the implementation
3183 quite simple. However, the block layer knows about the error, so it's
3184 possible to implement this without device models being involved */
3185 void bdrv_iostatus_set_err(BlockDriverState
*bs
, int error
)
3187 if (bdrv_iostatus_is_enabled(bs
) && bs
->iostatus
== BDRV_IOS_OK
) {
3189 bs
->iostatus
= error
== ENOSPC
? BDRV_IOS_ENOSPC
: BDRV_IOS_FAILED
;
3194 bdrv_acct_start(BlockDriverState
*bs
, BlockAcctCookie
*cookie
, int64_t bytes
,
3195 enum BlockAcctType type
)
3197 assert(type
< BDRV_MAX_IOTYPE
);
3199 cookie
->bytes
= bytes
;
3200 cookie
->start_time_ns
= get_clock();
3201 cookie
->type
= type
;
3205 bdrv_acct_done(BlockDriverState
*bs
, BlockAcctCookie
*cookie
)
3207 assert(cookie
->type
< BDRV_MAX_IOTYPE
);
3209 bs
->nr_bytes
[cookie
->type
] += cookie
->bytes
;
3210 bs
->nr_ops
[cookie
->type
]++;
3211 bs
->total_time_ns
[cookie
->type
] += get_clock() - cookie
->start_time_ns
;
3214 int bdrv_img_create(const char *filename
, const char *fmt
,
3215 const char *base_filename
, const char *base_fmt
,
3216 char *options
, uint64_t img_size
, int flags
)
3218 QEMUOptionParameter
*param
= NULL
, *create_options
= NULL
;
3219 QEMUOptionParameter
*backing_fmt
, *backing_file
, *size
;
3220 BlockDriverState
*bs
= NULL
;
3221 BlockDriver
*drv
, *proto_drv
;
3222 BlockDriver
*backing_drv
= NULL
;
3225 /* Find driver and parse its options */
3226 drv
= bdrv_find_format(fmt
);
3228 error_report("Unknown file format '%s'", fmt
);
3233 proto_drv
= bdrv_find_protocol(filename
);
3235 error_report("Unknown protocol '%s'", filename
);
3240 create_options
= append_option_parameters(create_options
,
3241 drv
->create_options
);
3242 create_options
= append_option_parameters(create_options
,
3243 proto_drv
->create_options
);
3245 /* Create parameter list with default values */
3246 param
= parse_option_parameters("", create_options
, param
);
3248 set_option_parameter_int(param
, BLOCK_OPT_SIZE
, img_size
);
3250 /* Parse -o options */
3252 param
= parse_option_parameters(options
, create_options
, param
);
3253 if (param
== NULL
) {
3254 error_report("Invalid options for file format '%s'.", fmt
);
3260 if (base_filename
) {
3261 if (set_option_parameter(param
, BLOCK_OPT_BACKING_FILE
,
3263 error_report("Backing file not supported for file format '%s'",
3271 if (set_option_parameter(param
, BLOCK_OPT_BACKING_FMT
, base_fmt
)) {
3272 error_report("Backing file format not supported for file "
3273 "format '%s'", fmt
);
3279 backing_file
= get_option_parameter(param
, BLOCK_OPT_BACKING_FILE
);
3280 if (backing_file
&& backing_file
->value
.s
) {
3281 if (!strcmp(filename
, backing_file
->value
.s
)) {
3282 error_report("Error: Trying to create an image with the "
3283 "same filename as the backing file");
3289 backing_fmt
= get_option_parameter(param
, BLOCK_OPT_BACKING_FMT
);
3290 if (backing_fmt
&& backing_fmt
->value
.s
) {
3291 backing_drv
= bdrv_find_format(backing_fmt
->value
.s
);
3293 error_report("Unknown backing file format '%s'",
3294 backing_fmt
->value
.s
);
3300 // The size for the image must always be specified, with one exception:
3301 // If we are using a backing file, we can obtain the size from there
3302 size
= get_option_parameter(param
, BLOCK_OPT_SIZE
);
3303 if (size
&& size
->value
.n
== -1) {
3304 if (backing_file
&& backing_file
->value
.s
) {
3310 ret
= bdrv_open(bs
, backing_file
->value
.s
, flags
, backing_drv
);
3312 error_report("Could not open '%s'", backing_file
->value
.s
);
3315 bdrv_get_geometry(bs
, &size
);
3318 snprintf(buf
, sizeof(buf
), "%" PRId64
, size
);
3319 set_option_parameter(param
, BLOCK_OPT_SIZE
, buf
);
3321 error_report("Image creation needs a size parameter");
3327 printf("Formatting '%s', fmt=%s ", filename
, fmt
);
3328 print_option_parameters(param
);
3331 ret
= bdrv_create(drv
, filename
, param
);
3334 if (ret
== -ENOTSUP
) {
3335 error_report("Formatting or formatting option not supported for "
3336 "file format '%s'", fmt
);
3337 } else if (ret
== -EFBIG
) {
3338 error_report("The image size is too large for file format '%s'",
3341 error_report("%s: error while creating %s: %s", filename
, fmt
,
3347 free_option_parameters(create_options
);
3348 free_option_parameters(param
);