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"
31 #include "qemu-coroutine.h"
32 #include "qmp-commands.h"
33 #include "qemu-timer.h"
36 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/queue.h>
49 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
51 static void bdrv_dev_change_media_cb(BlockDriverState
*bs
, bool load
);
52 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
53 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
54 BlockDriverCompletionFunc
*cb
, void *opaque
);
55 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
56 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
57 BlockDriverCompletionFunc
*cb
, void *opaque
);
58 static int coroutine_fn
bdrv_co_readv_em(BlockDriverState
*bs
,
59 int64_t sector_num
, int nb_sectors
,
61 static int coroutine_fn
bdrv_co_writev_em(BlockDriverState
*bs
,
62 int64_t sector_num
, int nb_sectors
,
64 static int coroutine_fn
bdrv_co_do_readv(BlockDriverState
*bs
,
65 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
);
66 static int coroutine_fn
bdrv_co_do_writev(BlockDriverState
*bs
,
67 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
);
68 static BlockDriverAIOCB
*bdrv_co_aio_rw_vector(BlockDriverState
*bs
,
72 BlockDriverCompletionFunc
*cb
,
75 static void coroutine_fn
bdrv_co_do_rw(void *opaque
);
77 static bool bdrv_exceed_bps_limits(BlockDriverState
*bs
, int nb_sectors
,
78 bool is_write
, double elapsed_time
, uint64_t *wait
);
79 static bool bdrv_exceed_iops_limits(BlockDriverState
*bs
, bool is_write
,
80 double elapsed_time
, uint64_t *wait
);
81 static bool bdrv_exceed_io_limits(BlockDriverState
*bs
, int nb_sectors
,
82 bool is_write
, int64_t *wait
);
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 /* throttling disk I/O limits */
117 void bdrv_io_limits_disable(BlockDriverState
*bs
)
119 bs
->io_limits_enabled
= false;
121 while (qemu_co_queue_next(&bs
->throttled_reqs
));
123 if (bs
->block_timer
) {
124 qemu_del_timer(bs
->block_timer
);
125 qemu_free_timer(bs
->block_timer
);
126 bs
->block_timer
= NULL
;
132 memset(&bs
->io_base
, 0, sizeof(bs
->io_base
));
135 static void bdrv_block_timer(void *opaque
)
137 BlockDriverState
*bs
= opaque
;
139 qemu_co_queue_next(&bs
->throttled_reqs
);
142 void bdrv_io_limits_enable(BlockDriverState
*bs
)
144 qemu_co_queue_init(&bs
->throttled_reqs
);
145 bs
->block_timer
= qemu_new_timer_ns(vm_clock
, bdrv_block_timer
, bs
);
146 bs
->slice_time
= 5 * BLOCK_IO_SLICE_TIME
;
147 bs
->slice_start
= qemu_get_clock_ns(vm_clock
);
148 bs
->slice_end
= bs
->slice_start
+ bs
->slice_time
;
149 memset(&bs
->io_base
, 0, sizeof(bs
->io_base
));
150 bs
->io_limits_enabled
= true;
153 bool bdrv_io_limits_enabled(BlockDriverState
*bs
)
155 BlockIOLimit
*io_limits
= &bs
->io_limits
;
156 return io_limits
->bps
[BLOCK_IO_LIMIT_READ
]
157 || io_limits
->bps
[BLOCK_IO_LIMIT_WRITE
]
158 || io_limits
->bps
[BLOCK_IO_LIMIT_TOTAL
]
159 || io_limits
->iops
[BLOCK_IO_LIMIT_READ
]
160 || io_limits
->iops
[BLOCK_IO_LIMIT_WRITE
]
161 || io_limits
->iops
[BLOCK_IO_LIMIT_TOTAL
];
164 static void bdrv_io_limits_intercept(BlockDriverState
*bs
,
165 bool is_write
, int nb_sectors
)
167 int64_t wait_time
= -1;
169 if (!qemu_co_queue_empty(&bs
->throttled_reqs
)) {
170 qemu_co_queue_wait(&bs
->throttled_reqs
);
173 /* In fact, we hope to keep each request's timing, in FIFO mode. The next
174 * throttled requests will not be dequeued until the current request is
175 * allowed to be serviced. So if the current request still exceeds the
176 * limits, it will be inserted to the head. All requests followed it will
177 * be still in throttled_reqs queue.
180 while (bdrv_exceed_io_limits(bs
, nb_sectors
, is_write
, &wait_time
)) {
181 qemu_mod_timer(bs
->block_timer
,
182 wait_time
+ qemu_get_clock_ns(vm_clock
));
183 qemu_co_queue_wait_insert_head(&bs
->throttled_reqs
);
186 qemu_co_queue_next(&bs
->throttled_reqs
);
189 /* check if the path starts with "<protocol>:" */
190 static int path_has_protocol(const char *path
)
193 if (is_windows_drive(path
) ||
194 is_windows_drive_prefix(path
)) {
199 return strchr(path
, ':') != NULL
;
202 int path_is_absolute(const char *path
)
206 /* specific case for names like: "\\.\d:" */
207 if (*path
== '/' || *path
== '\\')
210 p
= strchr(path
, ':');
216 return (*p
== '/' || *p
== '\\');
222 /* if filename is absolute, just copy it to dest. Otherwise, build a
223 path to it by considering it is relative to base_path. URL are
225 void path_combine(char *dest
, int dest_size
,
226 const char *base_path
,
227 const char *filename
)
234 if (path_is_absolute(filename
)) {
235 pstrcpy(dest
, dest_size
, filename
);
237 p
= strchr(base_path
, ':');
242 p1
= strrchr(base_path
, '/');
246 p2
= strrchr(base_path
, '\\');
258 if (len
> dest_size
- 1)
260 memcpy(dest
, base_path
, len
);
262 pstrcat(dest
, dest_size
, filename
);
266 void bdrv_register(BlockDriver
*bdrv
)
268 /* Block drivers without coroutine functions need emulation */
269 if (!bdrv
->bdrv_co_readv
) {
270 bdrv
->bdrv_co_readv
= bdrv_co_readv_em
;
271 bdrv
->bdrv_co_writev
= bdrv_co_writev_em
;
273 /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
274 * the block driver lacks aio we need to emulate that too.
276 if (!bdrv
->bdrv_aio_readv
) {
277 /* add AIO emulation layer */
278 bdrv
->bdrv_aio_readv
= bdrv_aio_readv_em
;
279 bdrv
->bdrv_aio_writev
= bdrv_aio_writev_em
;
283 QLIST_INSERT_HEAD(&bdrv_drivers
, bdrv
, list
);
286 /* create a new block device (by default it is empty) */
287 BlockDriverState
*bdrv_new(const char *device_name
)
289 BlockDriverState
*bs
;
291 bs
= g_malloc0(sizeof(BlockDriverState
));
292 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
293 if (device_name
[0] != '\0') {
294 QTAILQ_INSERT_TAIL(&bdrv_states
, bs
, list
);
296 bdrv_iostatus_disable(bs
);
300 BlockDriver
*bdrv_find_format(const char *format_name
)
303 QLIST_FOREACH(drv1
, &bdrv_drivers
, list
) {
304 if (!strcmp(drv1
->format_name
, format_name
)) {
311 static int bdrv_is_whitelisted(BlockDriver
*drv
)
313 static const char *whitelist
[] = {
314 CONFIG_BDRV_WHITELIST
319 return 1; /* no whitelist, anything goes */
321 for (p
= whitelist
; *p
; p
++) {
322 if (!strcmp(drv
->format_name
, *p
)) {
329 BlockDriver
*bdrv_find_whitelisted_format(const char *format_name
)
331 BlockDriver
*drv
= bdrv_find_format(format_name
);
332 return drv
&& bdrv_is_whitelisted(drv
) ? drv
: NULL
;
335 int bdrv_create(BlockDriver
*drv
, const char* filename
,
336 QEMUOptionParameter
*options
)
338 if (!drv
->bdrv_create
)
341 return drv
->bdrv_create(filename
, options
);
344 int bdrv_create_file(const char* filename
, QEMUOptionParameter
*options
)
348 drv
= bdrv_find_protocol(filename
);
353 return bdrv_create(drv
, filename
, options
);
357 void get_tmp_filename(char *filename
, int size
)
359 char temp_dir
[MAX_PATH
];
361 GetTempPath(MAX_PATH
, temp_dir
);
362 GetTempFileName(temp_dir
, "qem", 0, filename
);
365 void get_tmp_filename(char *filename
, int size
)
369 /* XXX: race condition possible */
370 tmpdir
= getenv("TMPDIR");
373 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
374 fd
= mkstemp(filename
);
380 * Detect host devices. By convention, /dev/cdrom[N] is always
381 * recognized as a host CDROM.
383 static BlockDriver
*find_hdev_driver(const char *filename
)
385 int score_max
= 0, score
;
386 BlockDriver
*drv
= NULL
, *d
;
388 QLIST_FOREACH(d
, &bdrv_drivers
, list
) {
389 if (d
->bdrv_probe_device
) {
390 score
= d
->bdrv_probe_device(filename
);
391 if (score
> score_max
) {
401 BlockDriver
*bdrv_find_protocol(const char *filename
)
408 /* TODO Drivers without bdrv_file_open must be specified explicitly */
411 * XXX(hch): we really should not let host device detection
412 * override an explicit protocol specification, but moving this
413 * later breaks access to device names with colons in them.
414 * Thanks to the brain-dead persistent naming schemes on udev-
415 * based Linux systems those actually are quite common.
417 drv1
= find_hdev_driver(filename
);
422 if (!path_has_protocol(filename
)) {
423 return bdrv_find_format("file");
425 p
= strchr(filename
, ':');
428 if (len
> sizeof(protocol
) - 1)
429 len
= sizeof(protocol
) - 1;
430 memcpy(protocol
, filename
, len
);
431 protocol
[len
] = '\0';
432 QLIST_FOREACH(drv1
, &bdrv_drivers
, list
) {
433 if (drv1
->protocol_name
&&
434 !strcmp(drv1
->protocol_name
, protocol
)) {
441 static int find_image_format(const char *filename
, BlockDriver
**pdrv
)
443 int ret
, score
, score_max
;
444 BlockDriver
*drv1
, *drv
;
446 BlockDriverState
*bs
;
448 ret
= bdrv_file_open(&bs
, filename
, 0);
454 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
455 if (bs
->sg
|| !bdrv_is_inserted(bs
)) {
457 drv
= bdrv_find_format("raw");
465 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
474 QLIST_FOREACH(drv1
, &bdrv_drivers
, list
) {
475 if (drv1
->bdrv_probe
) {
476 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
477 if (score
> score_max
) {
491 * Set the current 'total_sectors' value
493 static int refresh_total_sectors(BlockDriverState
*bs
, int64_t hint
)
495 BlockDriver
*drv
= bs
->drv
;
497 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
501 /* query actual device if possible, otherwise just trust the hint */
502 if (drv
->bdrv_getlength
) {
503 int64_t length
= drv
->bdrv_getlength(bs
);
507 hint
= length
>> BDRV_SECTOR_BITS
;
510 bs
->total_sectors
= hint
;
515 * Set open flags for a given cache mode
517 * Return 0 on success, -1 if the cache mode was invalid.
519 int bdrv_parse_cache_flags(const char *mode
, int *flags
)
521 *flags
&= ~BDRV_O_CACHE_MASK
;
523 if (!strcmp(mode
, "off") || !strcmp(mode
, "none")) {
524 *flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
525 } else if (!strcmp(mode
, "directsync")) {
526 *flags
|= BDRV_O_NOCACHE
;
527 } else if (!strcmp(mode
, "writeback")) {
528 *flags
|= BDRV_O_CACHE_WB
;
529 } else if (!strcmp(mode
, "unsafe")) {
530 *flags
|= BDRV_O_CACHE_WB
;
531 *flags
|= BDRV_O_NO_FLUSH
;
532 } else if (!strcmp(mode
, "writethrough")) {
533 /* this is the default */
542 * Common part for opening disk images and files
544 static int bdrv_open_common(BlockDriverState
*bs
, const char *filename
,
545 int flags
, BlockDriver
*drv
)
551 trace_bdrv_open_common(bs
, filename
, flags
, drv
->format_name
);
554 bs
->total_sectors
= 0;
558 bs
->open_flags
= flags
;
560 bs
->buffer_alignment
= 512;
562 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
563 bs
->backing_file
[0] = '\0';
565 if (use_bdrv_whitelist
&& !bdrv_is_whitelisted(drv
)) {
570 bs
->opaque
= g_malloc0(drv
->instance_size
);
572 bs
->enable_write_cache
= !!(flags
& BDRV_O_CACHE_WB
);
575 * Clear flags that are internal to the block layer before opening the
578 open_flags
= flags
& ~(BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
);
581 * Snapshots should be writable.
583 if (bs
->is_temporary
) {
584 open_flags
|= BDRV_O_RDWR
;
587 bs
->keep_read_only
= bs
->read_only
= !(open_flags
& BDRV_O_RDWR
);
589 /* Open the image, either directly or using a protocol */
590 if (drv
->bdrv_file_open
) {
591 ret
= drv
->bdrv_file_open(bs
, filename
, open_flags
);
593 ret
= bdrv_file_open(&bs
->file
, filename
, open_flags
);
595 ret
= drv
->bdrv_open(bs
, open_flags
);
603 ret
= refresh_total_sectors(bs
, bs
->total_sectors
);
609 if (bs
->is_temporary
) {
617 bdrv_delete(bs
->file
);
627 * Opens a file using a protocol (file, host_device, nbd, ...)
629 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
631 BlockDriverState
*bs
;
635 drv
= bdrv_find_protocol(filename
);
641 ret
= bdrv_open_common(bs
, filename
, flags
, drv
);
652 * Opens a disk image (raw, qcow2, vmdk, ...)
654 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
,
658 char tmp_filename
[PATH_MAX
];
660 if (flags
& BDRV_O_SNAPSHOT
) {
661 BlockDriverState
*bs1
;
664 BlockDriver
*bdrv_qcow2
;
665 QEMUOptionParameter
*options
;
666 char backing_filename
[PATH_MAX
];
668 /* if snapshot, we create a temporary backing file and open it
669 instead of opening 'filename' directly */
671 /* if there is a backing file, use it */
673 ret
= bdrv_open(bs1
, filename
, 0, drv
);
678 total_size
= bdrv_getlength(bs1
) & BDRV_SECTOR_MASK
;
680 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
685 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
687 /* Real path is meaningless for protocols */
689 snprintf(backing_filename
, sizeof(backing_filename
),
691 else if (!realpath(filename
, backing_filename
))
694 bdrv_qcow2
= bdrv_find_format("qcow2");
695 options
= parse_option_parameters("", bdrv_qcow2
->create_options
, NULL
);
697 set_option_parameter_int(options
, BLOCK_OPT_SIZE
, total_size
);
698 set_option_parameter(options
, BLOCK_OPT_BACKING_FILE
, backing_filename
);
700 set_option_parameter(options
, BLOCK_OPT_BACKING_FMT
,
704 ret
= bdrv_create(bdrv_qcow2
, tmp_filename
, options
);
705 free_option_parameters(options
);
710 filename
= tmp_filename
;
712 bs
->is_temporary
= 1;
715 /* Find the right image format driver */
717 ret
= find_image_format(filename
, &drv
);
721 goto unlink_and_fail
;
725 ret
= bdrv_open_common(bs
, filename
, flags
, drv
);
727 goto unlink_and_fail
;
730 /* If there is a backing file, use it */
731 if ((flags
& BDRV_O_NO_BACKING
) == 0 && bs
->backing_file
[0] != '\0') {
732 char backing_filename
[PATH_MAX
];
734 BlockDriver
*back_drv
= NULL
;
736 bs
->backing_hd
= bdrv_new("");
738 if (path_has_protocol(bs
->backing_file
)) {
739 pstrcpy(backing_filename
, sizeof(backing_filename
),
742 path_combine(backing_filename
, sizeof(backing_filename
),
743 filename
, bs
->backing_file
);
746 if (bs
->backing_format
[0] != '\0') {
747 back_drv
= bdrv_find_format(bs
->backing_format
);
750 /* backing files always opened read-only */
752 flags
& ~(BDRV_O_RDWR
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
);
754 ret
= bdrv_open(bs
->backing_hd
, backing_filename
, back_flags
, back_drv
);
759 if (bs
->is_temporary
) {
760 bs
->backing_hd
->keep_read_only
= !(flags
& BDRV_O_RDWR
);
762 /* base image inherits from "parent" */
763 bs
->backing_hd
->keep_read_only
= bs
->keep_read_only
;
767 if (!bdrv_key_required(bs
)) {
768 bdrv_dev_change_media_cb(bs
, true);
771 /* throttling disk I/O limits */
772 if (bs
->io_limits_enabled
) {
773 bdrv_io_limits_enable(bs
);
779 if (bs
->is_temporary
) {
785 void bdrv_close(BlockDriverState
*bs
)
788 if (bs
== bs_snapshots
) {
791 if (bs
->backing_hd
) {
792 bdrv_delete(bs
->backing_hd
);
793 bs
->backing_hd
= NULL
;
795 bs
->drv
->bdrv_close(bs
);
798 if (bs
->is_temporary
) {
799 unlink(bs
->filename
);
805 if (bs
->file
!= NULL
) {
806 bdrv_close(bs
->file
);
809 bdrv_dev_change_media_cb(bs
, false);
812 /*throttling disk I/O limits*/
813 if (bs
->io_limits_enabled
) {
814 bdrv_io_limits_disable(bs
);
818 void bdrv_close_all(void)
820 BlockDriverState
*bs
;
822 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
827 /* make a BlockDriverState anonymous by removing from bdrv_state list.
828 Also, NULL terminate the device_name to prevent double remove */
829 void bdrv_make_anon(BlockDriverState
*bs
)
831 if (bs
->device_name
[0] != '\0') {
832 QTAILQ_REMOVE(&bdrv_states
, bs
, list
);
834 bs
->device_name
[0] = '\0';
837 void bdrv_delete(BlockDriverState
*bs
)
841 /* remove from list, if necessary */
845 if (bs
->file
!= NULL
) {
846 bdrv_delete(bs
->file
);
849 assert(bs
!= bs_snapshots
);
853 int bdrv_attach_dev(BlockDriverState
*bs
, void *dev
)
854 /* TODO change to DeviceState *dev when all users are qdevified */
860 bdrv_iostatus_reset(bs
);
864 /* TODO qdevified devices don't use this, remove when devices are qdevified */
865 void bdrv_attach_dev_nofail(BlockDriverState
*bs
, void *dev
)
867 if (bdrv_attach_dev(bs
, dev
) < 0) {
872 void bdrv_detach_dev(BlockDriverState
*bs
, void *dev
)
873 /* TODO change to DeviceState *dev when all users are qdevified */
875 assert(bs
->dev
== dev
);
878 bs
->dev_opaque
= NULL
;
879 bs
->buffer_alignment
= 512;
882 /* TODO change to return DeviceState * when all users are qdevified */
883 void *bdrv_get_attached_dev(BlockDriverState
*bs
)
888 void bdrv_set_dev_ops(BlockDriverState
*bs
, const BlockDevOps
*ops
,
892 bs
->dev_opaque
= opaque
;
893 if (bdrv_dev_has_removable_media(bs
) && bs
== bs_snapshots
) {
898 static void bdrv_dev_change_media_cb(BlockDriverState
*bs
, bool load
)
900 if (bs
->dev_ops
&& bs
->dev_ops
->change_media_cb
) {
901 bs
->dev_ops
->change_media_cb(bs
->dev_opaque
, load
);
905 bool bdrv_dev_has_removable_media(BlockDriverState
*bs
)
907 return !bs
->dev
|| (bs
->dev_ops
&& bs
->dev_ops
->change_media_cb
);
910 void bdrv_dev_eject_request(BlockDriverState
*bs
, bool force
)
912 if (bs
->dev_ops
&& bs
->dev_ops
->eject_request_cb
) {
913 bs
->dev_ops
->eject_request_cb(bs
->dev_opaque
, force
);
917 bool bdrv_dev_is_tray_open(BlockDriverState
*bs
)
919 if (bs
->dev_ops
&& bs
->dev_ops
->is_tray_open
) {
920 return bs
->dev_ops
->is_tray_open(bs
->dev_opaque
);
925 static void bdrv_dev_resize_cb(BlockDriverState
*bs
)
927 if (bs
->dev_ops
&& bs
->dev_ops
->resize_cb
) {
928 bs
->dev_ops
->resize_cb(bs
->dev_opaque
);
932 bool bdrv_dev_is_medium_locked(BlockDriverState
*bs
)
934 if (bs
->dev_ops
&& bs
->dev_ops
->is_medium_locked
) {
935 return bs
->dev_ops
->is_medium_locked(bs
->dev_opaque
);
941 * Run consistency checks on an image
943 * Returns 0 if the check could be completed (it doesn't mean that the image is
944 * free of errors) or -errno when an internal error occurred. The results of the
945 * check are stored in res.
947 int bdrv_check(BlockDriverState
*bs
, BdrvCheckResult
*res
)
949 if (bs
->drv
->bdrv_check
== NULL
) {
953 memset(res
, 0, sizeof(*res
));
954 return bs
->drv
->bdrv_check(bs
, res
);
957 #define COMMIT_BUF_SECTORS 2048
959 /* commit COW file into the raw image */
960 int bdrv_commit(BlockDriverState
*bs
)
962 BlockDriver
*drv
= bs
->drv
;
963 BlockDriver
*backing_drv
;
964 int64_t sector
, total_sectors
;
965 int n
, ro
, open_flags
;
966 int ret
= 0, rw_ret
= 0;
969 BlockDriverState
*bs_rw
, *bs_ro
;
974 if (!bs
->backing_hd
) {
978 if (bs
->backing_hd
->keep_read_only
) {
982 backing_drv
= bs
->backing_hd
->drv
;
983 ro
= bs
->backing_hd
->read_only
;
984 strncpy(filename
, bs
->backing_hd
->filename
, sizeof(filename
));
985 open_flags
= bs
->backing_hd
->open_flags
;
989 bdrv_delete(bs
->backing_hd
);
990 bs
->backing_hd
= NULL
;
991 bs_rw
= bdrv_new("");
992 rw_ret
= bdrv_open(bs_rw
, filename
, open_flags
| BDRV_O_RDWR
,
996 /* try to re-open read-only */
997 bs_ro
= bdrv_new("");
998 ret
= bdrv_open(bs_ro
, filename
, open_flags
& ~BDRV_O_RDWR
,
1002 /* drive not functional anymore */
1006 bs
->backing_hd
= bs_ro
;
1009 bs
->backing_hd
= bs_rw
;
1012 total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
1013 buf
= g_malloc(COMMIT_BUF_SECTORS
* BDRV_SECTOR_SIZE
);
1015 for (sector
= 0; sector
< total_sectors
; sector
+= n
) {
1016 if (bdrv_is_allocated(bs
, sector
, COMMIT_BUF_SECTORS
, &n
)) {
1018 if (bdrv_read(bs
, sector
, buf
, n
) != 0) {
1023 if (bdrv_write(bs
->backing_hd
, sector
, buf
, n
) != 0) {
1030 if (drv
->bdrv_make_empty
) {
1031 ret
= drv
->bdrv_make_empty(bs
);
1036 * Make sure all data we wrote to the backing device is actually
1040 bdrv_flush(bs
->backing_hd
);
1047 bdrv_delete(bs
->backing_hd
);
1048 bs
->backing_hd
= NULL
;
1049 bs_ro
= bdrv_new("");
1050 ret
= bdrv_open(bs_ro
, filename
, open_flags
& ~BDRV_O_RDWR
,
1054 /* drive not functional anymore */
1058 bs
->backing_hd
= bs_ro
;
1059 bs
->backing_hd
->keep_read_only
= 0;
1065 void bdrv_commit_all(void)
1067 BlockDriverState
*bs
;
1069 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1077 * -EINVAL - backing format specified, but no file
1078 * -ENOSPC - can't update the backing file because no space is left in the
1080 * -ENOTSUP - format driver doesn't support changing the backing file
1082 int bdrv_change_backing_file(BlockDriverState
*bs
,
1083 const char *backing_file
, const char *backing_fmt
)
1085 BlockDriver
*drv
= bs
->drv
;
1087 if (drv
->bdrv_change_backing_file
!= NULL
) {
1088 return drv
->bdrv_change_backing_file(bs
, backing_file
, backing_fmt
);
1094 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
1099 if (!bdrv_is_inserted(bs
))
1105 len
= bdrv_getlength(bs
);
1110 if ((offset
> len
) || (len
- offset
< size
))
1116 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
1119 return bdrv_check_byte_request(bs
, sector_num
* BDRV_SECTOR_SIZE
,
1120 nb_sectors
* BDRV_SECTOR_SIZE
);
1123 typedef struct RwCo
{
1124 BlockDriverState
*bs
;
1132 static void coroutine_fn
bdrv_rw_co_entry(void *opaque
)
1134 RwCo
*rwco
= opaque
;
1136 if (!rwco
->is_write
) {
1137 rwco
->ret
= bdrv_co_do_readv(rwco
->bs
, rwco
->sector_num
,
1138 rwco
->nb_sectors
, rwco
->qiov
);
1140 rwco
->ret
= bdrv_co_do_writev(rwco
->bs
, rwco
->sector_num
,
1141 rwco
->nb_sectors
, rwco
->qiov
);
1146 * Process a synchronous request using coroutines
1148 static int bdrv_rw_co(BlockDriverState
*bs
, int64_t sector_num
, uint8_t *buf
,
1149 int nb_sectors
, bool is_write
)
1152 struct iovec iov
= {
1153 .iov_base
= (void *)buf
,
1154 .iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
,
1159 .sector_num
= sector_num
,
1160 .nb_sectors
= nb_sectors
,
1162 .is_write
= is_write
,
1166 qemu_iovec_init_external(&qiov
, &iov
, 1);
1168 if (qemu_in_coroutine()) {
1169 /* Fast-path if already in coroutine context */
1170 bdrv_rw_co_entry(&rwco
);
1172 co
= qemu_coroutine_create(bdrv_rw_co_entry
);
1173 qemu_coroutine_enter(co
, &rwco
);
1174 while (rwco
.ret
== NOT_DONE
) {
1181 /* return < 0 if error. See bdrv_write() for the return codes */
1182 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
1183 uint8_t *buf
, int nb_sectors
)
1185 return bdrv_rw_co(bs
, sector_num
, buf
, nb_sectors
, false);
1188 static void set_dirty_bitmap(BlockDriverState
*bs
, int64_t sector_num
,
1189 int nb_sectors
, int dirty
)
1192 unsigned long val
, idx
, bit
;
1194 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
1195 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
1197 for (; start
<= end
; start
++) {
1198 idx
= start
/ (sizeof(unsigned long) * 8);
1199 bit
= start
% (sizeof(unsigned long) * 8);
1200 val
= bs
->dirty_bitmap
[idx
];
1202 if (!(val
& (1UL << bit
))) {
1207 if (val
& (1UL << bit
)) {
1209 val
&= ~(1UL << bit
);
1212 bs
->dirty_bitmap
[idx
] = val
;
1216 /* Return < 0 if error. Important errors are:
1217 -EIO generic I/O error (may happen for all errors)
1218 -ENOMEDIUM No media inserted.
1219 -EINVAL Invalid sector number or nb_sectors
1220 -EACCES Trying to write a read-only device
1222 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
1223 const uint8_t *buf
, int nb_sectors
)
1225 return bdrv_rw_co(bs
, sector_num
, (uint8_t *)buf
, nb_sectors
, true);
1228 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
1229 void *buf
, int count1
)
1231 uint8_t tmp_buf
[BDRV_SECTOR_SIZE
];
1232 int len
, nb_sectors
, count
;
1237 /* first read to align to sector start */
1238 len
= (BDRV_SECTOR_SIZE
- offset
) & (BDRV_SECTOR_SIZE
- 1);
1241 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1243 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1245 memcpy(buf
, tmp_buf
+ (offset
& (BDRV_SECTOR_SIZE
- 1)), len
);
1253 /* read the sectors "in place" */
1254 nb_sectors
= count
>> BDRV_SECTOR_BITS
;
1255 if (nb_sectors
> 0) {
1256 if ((ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
)) < 0)
1258 sector_num
+= nb_sectors
;
1259 len
= nb_sectors
<< BDRV_SECTOR_BITS
;
1264 /* add data from the last sector */
1266 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1268 memcpy(buf
, tmp_buf
, count
);
1273 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
1274 const void *buf
, int count1
)
1276 uint8_t tmp_buf
[BDRV_SECTOR_SIZE
];
1277 int len
, nb_sectors
, count
;
1282 /* first write to align to sector start */
1283 len
= (BDRV_SECTOR_SIZE
- offset
) & (BDRV_SECTOR_SIZE
- 1);
1286 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1288 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1290 memcpy(tmp_buf
+ (offset
& (BDRV_SECTOR_SIZE
- 1)), buf
, len
);
1291 if ((ret
= bdrv_write(bs
, sector_num
, tmp_buf
, 1)) < 0)
1300 /* write the sectors "in place" */
1301 nb_sectors
= count
>> BDRV_SECTOR_BITS
;
1302 if (nb_sectors
> 0) {
1303 if ((ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
)) < 0)
1305 sector_num
+= nb_sectors
;
1306 len
= nb_sectors
<< BDRV_SECTOR_BITS
;
1311 /* add data from the last sector */
1313 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1315 memcpy(tmp_buf
, buf
, count
);
1316 if ((ret
= bdrv_write(bs
, sector_num
, tmp_buf
, 1)) < 0)
1323 * Writes to the file and ensures that no writes are reordered across this
1324 * request (acts as a barrier)
1326 * Returns 0 on success, -errno in error cases.
1328 int bdrv_pwrite_sync(BlockDriverState
*bs
, int64_t offset
,
1329 const void *buf
, int count
)
1333 ret
= bdrv_pwrite(bs
, offset
, buf
, count
);
1338 /* No flush needed for cache modes that use O_DSYNC */
1339 if ((bs
->open_flags
& BDRV_O_CACHE_WB
) != 0) {
1347 * Handle a read request in coroutine context
1349 static int coroutine_fn
bdrv_co_do_readv(BlockDriverState
*bs
,
1350 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
1352 BlockDriver
*drv
= bs
->drv
;
1357 if (bdrv_check_request(bs
, sector_num
, nb_sectors
)) {
1361 /* throttling disk read I/O */
1362 if (bs
->io_limits_enabled
) {
1363 bdrv_io_limits_intercept(bs
, false, nb_sectors
);
1366 return drv
->bdrv_co_readv(bs
, sector_num
, nb_sectors
, qiov
);
1369 int coroutine_fn
bdrv_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
1370 int nb_sectors
, QEMUIOVector
*qiov
)
1372 trace_bdrv_co_readv(bs
, sector_num
, nb_sectors
);
1374 return bdrv_co_do_readv(bs
, sector_num
, nb_sectors
, qiov
);
1378 * Handle a write request in coroutine context
1380 static int coroutine_fn
bdrv_co_do_writev(BlockDriverState
*bs
,
1381 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
1383 BlockDriver
*drv
= bs
->drv
;
1389 if (bs
->read_only
) {
1392 if (bdrv_check_request(bs
, sector_num
, nb_sectors
)) {
1396 /* throttling disk write I/O */
1397 if (bs
->io_limits_enabled
) {
1398 bdrv_io_limits_intercept(bs
, true, nb_sectors
);
1401 ret
= drv
->bdrv_co_writev(bs
, sector_num
, nb_sectors
, qiov
);
1403 if (bs
->dirty_bitmap
) {
1404 set_dirty_bitmap(bs
, sector_num
, nb_sectors
, 1);
1407 if (bs
->wr_highest_sector
< sector_num
+ nb_sectors
- 1) {
1408 bs
->wr_highest_sector
= sector_num
+ nb_sectors
- 1;
1414 int coroutine_fn
bdrv_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
1415 int nb_sectors
, QEMUIOVector
*qiov
)
1417 trace_bdrv_co_writev(bs
, sector_num
, nb_sectors
);
1419 return bdrv_co_do_writev(bs
, sector_num
, nb_sectors
, qiov
);
1423 * Truncate file to 'offset' bytes (needed only for file protocols)
1425 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
1427 BlockDriver
*drv
= bs
->drv
;
1431 if (!drv
->bdrv_truncate
)
1435 if (bdrv_in_use(bs
))
1437 ret
= drv
->bdrv_truncate(bs
, offset
);
1439 ret
= refresh_total_sectors(bs
, offset
>> BDRV_SECTOR_BITS
);
1440 bdrv_dev_resize_cb(bs
);
1446 * Length of a allocated file in bytes. Sparse files are counted by actual
1447 * allocated space. Return < 0 if error or unknown.
1449 int64_t bdrv_get_allocated_file_size(BlockDriverState
*bs
)
1451 BlockDriver
*drv
= bs
->drv
;
1455 if (drv
->bdrv_get_allocated_file_size
) {
1456 return drv
->bdrv_get_allocated_file_size(bs
);
1459 return bdrv_get_allocated_file_size(bs
->file
);
1465 * Length of a file in bytes. Return < 0 if error or unknown.
1467 int64_t bdrv_getlength(BlockDriverState
*bs
)
1469 BlockDriver
*drv
= bs
->drv
;
1473 if (bs
->growable
|| bdrv_dev_has_removable_media(bs
)) {
1474 if (drv
->bdrv_getlength
) {
1475 return drv
->bdrv_getlength(bs
);
1478 return bs
->total_sectors
* BDRV_SECTOR_SIZE
;
1481 /* return 0 as number of sectors if no device present or error */
1482 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
1485 length
= bdrv_getlength(bs
);
1489 length
= length
>> BDRV_SECTOR_BITS
;
1490 *nb_sectors_ptr
= length
;
1494 uint8_t boot_ind
; /* 0x80 - active */
1495 uint8_t head
; /* starting head */
1496 uint8_t sector
; /* starting sector */
1497 uint8_t cyl
; /* starting cylinder */
1498 uint8_t sys_ind
; /* What partition type */
1499 uint8_t end_head
; /* end head */
1500 uint8_t end_sector
; /* end sector */
1501 uint8_t end_cyl
; /* end cylinder */
1502 uint32_t start_sect
; /* starting sector counting from 0 */
1503 uint32_t nr_sects
; /* nr of sectors in partition */
1506 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1507 static int guess_disk_lchs(BlockDriverState
*bs
,
1508 int *pcylinders
, int *pheads
, int *psectors
)
1510 uint8_t buf
[BDRV_SECTOR_SIZE
];
1511 int ret
, i
, heads
, sectors
, cylinders
;
1512 struct partition
*p
;
1514 uint64_t nb_sectors
;
1516 bdrv_get_geometry(bs
, &nb_sectors
);
1518 ret
= bdrv_read(bs
, 0, buf
, 1);
1521 /* test msdos magic */
1522 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
1524 for(i
= 0; i
< 4; i
++) {
1525 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
1526 nr_sects
= le32_to_cpu(p
->nr_sects
);
1527 if (nr_sects
&& p
->end_head
) {
1528 /* We make the assumption that the partition terminates on
1529 a cylinder boundary */
1530 heads
= p
->end_head
+ 1;
1531 sectors
= p
->end_sector
& 63;
1534 cylinders
= nb_sectors
/ (heads
* sectors
);
1535 if (cylinders
< 1 || cylinders
> 16383)
1538 *psectors
= sectors
;
1539 *pcylinders
= cylinders
;
1541 printf("guessed geometry: LCHS=%d %d %d\n",
1542 cylinders
, heads
, sectors
);
1550 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
1552 int translation
, lba_detected
= 0;
1553 int cylinders
, heads
, secs
;
1554 uint64_t nb_sectors
;
1556 /* if a geometry hint is available, use it */
1557 bdrv_get_geometry(bs
, &nb_sectors
);
1558 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
1559 translation
= bdrv_get_translation_hint(bs
);
1560 if (cylinders
!= 0) {
1565 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
1567 /* if heads > 16, it means that a BIOS LBA
1568 translation was active, so the default
1569 hardware geometry is OK */
1571 goto default_geometry
;
1576 /* disable any translation to be in sync with
1577 the logical geometry */
1578 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
1579 bdrv_set_translation_hint(bs
,
1580 BIOS_ATA_TRANSLATION_NONE
);
1585 /* if no geometry, use a standard physical disk geometry */
1586 cylinders
= nb_sectors
/ (16 * 63);
1588 if (cylinders
> 16383)
1590 else if (cylinders
< 2)
1595 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
1596 if ((*pcyls
* *pheads
) <= 131072) {
1597 bdrv_set_translation_hint(bs
,
1598 BIOS_ATA_TRANSLATION_LARGE
);
1600 bdrv_set_translation_hint(bs
,
1601 BIOS_ATA_TRANSLATION_LBA
);
1605 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
1609 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
1610 int cyls
, int heads
, int secs
)
1617 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
1619 bs
->translation
= translation
;
1622 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
1623 int *pcyls
, int *pheads
, int *psecs
)
1626 *pheads
= bs
->heads
;
1630 /* throttling disk io limits */
1631 void bdrv_set_io_limits(BlockDriverState
*bs
,
1632 BlockIOLimit
*io_limits
)
1634 bs
->io_limits
= *io_limits
;
1635 bs
->io_limits_enabled
= bdrv_io_limits_enabled(bs
);
1638 /* Recognize floppy formats */
1639 typedef struct FDFormat
{
1646 static const FDFormat fd_formats
[] = {
1647 /* First entry is default format */
1648 /* 1.44 MB 3"1/2 floppy disks */
1649 { FDRIVE_DRV_144
, 18, 80, 1, },
1650 { FDRIVE_DRV_144
, 20, 80, 1, },
1651 { FDRIVE_DRV_144
, 21, 80, 1, },
1652 { FDRIVE_DRV_144
, 21, 82, 1, },
1653 { FDRIVE_DRV_144
, 21, 83, 1, },
1654 { FDRIVE_DRV_144
, 22, 80, 1, },
1655 { FDRIVE_DRV_144
, 23, 80, 1, },
1656 { FDRIVE_DRV_144
, 24, 80, 1, },
1657 /* 2.88 MB 3"1/2 floppy disks */
1658 { FDRIVE_DRV_288
, 36, 80, 1, },
1659 { FDRIVE_DRV_288
, 39, 80, 1, },
1660 { FDRIVE_DRV_288
, 40, 80, 1, },
1661 { FDRIVE_DRV_288
, 44, 80, 1, },
1662 { FDRIVE_DRV_288
, 48, 80, 1, },
1663 /* 720 kB 3"1/2 floppy disks */
1664 { FDRIVE_DRV_144
, 9, 80, 1, },
1665 { FDRIVE_DRV_144
, 10, 80, 1, },
1666 { FDRIVE_DRV_144
, 10, 82, 1, },
1667 { FDRIVE_DRV_144
, 10, 83, 1, },
1668 { FDRIVE_DRV_144
, 13, 80, 1, },
1669 { FDRIVE_DRV_144
, 14, 80, 1, },
1670 /* 1.2 MB 5"1/4 floppy disks */
1671 { FDRIVE_DRV_120
, 15, 80, 1, },
1672 { FDRIVE_DRV_120
, 18, 80, 1, },
1673 { FDRIVE_DRV_120
, 18, 82, 1, },
1674 { FDRIVE_DRV_120
, 18, 83, 1, },
1675 { FDRIVE_DRV_120
, 20, 80, 1, },
1676 /* 720 kB 5"1/4 floppy disks */
1677 { FDRIVE_DRV_120
, 9, 80, 1, },
1678 { FDRIVE_DRV_120
, 11, 80, 1, },
1679 /* 360 kB 5"1/4 floppy disks */
1680 { FDRIVE_DRV_120
, 9, 40, 1, },
1681 { FDRIVE_DRV_120
, 9, 40, 0, },
1682 { FDRIVE_DRV_120
, 10, 41, 1, },
1683 { FDRIVE_DRV_120
, 10, 42, 1, },
1684 /* 320 kB 5"1/4 floppy disks */
1685 { FDRIVE_DRV_120
, 8, 40, 1, },
1686 { FDRIVE_DRV_120
, 8, 40, 0, },
1687 /* 360 kB must match 5"1/4 better than 3"1/2... */
1688 { FDRIVE_DRV_144
, 9, 80, 0, },
1690 { FDRIVE_DRV_NONE
, -1, -1, 0, },
1693 void bdrv_get_floppy_geometry_hint(BlockDriverState
*bs
, int *nb_heads
,
1694 int *max_track
, int *last_sect
,
1695 FDriveType drive_in
, FDriveType
*drive
)
1697 const FDFormat
*parse
;
1698 uint64_t nb_sectors
, size
;
1699 int i
, first_match
, match
;
1701 bdrv_get_geometry_hint(bs
, nb_heads
, max_track
, last_sect
);
1702 if (*nb_heads
!= 0 && *max_track
!= 0 && *last_sect
!= 0) {
1703 /* User defined disk */
1705 bdrv_get_geometry(bs
, &nb_sectors
);
1708 for (i
= 0; ; i
++) {
1709 parse
= &fd_formats
[i
];
1710 if (parse
->drive
== FDRIVE_DRV_NONE
) {
1713 if (drive_in
== parse
->drive
||
1714 drive_in
== FDRIVE_DRV_NONE
) {
1715 size
= (parse
->max_head
+ 1) * parse
->max_track
*
1717 if (nb_sectors
== size
) {
1721 if (first_match
== -1) {
1727 if (first_match
== -1) {
1730 match
= first_match
;
1732 parse
= &fd_formats
[match
];
1734 *nb_heads
= parse
->max_head
+ 1;
1735 *max_track
= parse
->max_track
;
1736 *last_sect
= parse
->last_sect
;
1737 *drive
= parse
->drive
;
1741 int bdrv_get_translation_hint(BlockDriverState
*bs
)
1743 return bs
->translation
;
1746 void bdrv_set_on_error(BlockDriverState
*bs
, BlockErrorAction on_read_error
,
1747 BlockErrorAction on_write_error
)
1749 bs
->on_read_error
= on_read_error
;
1750 bs
->on_write_error
= on_write_error
;
1753 BlockErrorAction
bdrv_get_on_error(BlockDriverState
*bs
, int is_read
)
1755 return is_read
? bs
->on_read_error
: bs
->on_write_error
;
1758 int bdrv_is_read_only(BlockDriverState
*bs
)
1760 return bs
->read_only
;
1763 int bdrv_is_sg(BlockDriverState
*bs
)
1768 int bdrv_enable_write_cache(BlockDriverState
*bs
)
1770 return bs
->enable_write_cache
;
1773 int bdrv_is_encrypted(BlockDriverState
*bs
)
1775 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1777 return bs
->encrypted
;
1780 int bdrv_key_required(BlockDriverState
*bs
)
1782 BlockDriverState
*backing_hd
= bs
->backing_hd
;
1784 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
1786 return (bs
->encrypted
&& !bs
->valid_key
);
1789 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
1792 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
1793 ret
= bdrv_set_key(bs
->backing_hd
, key
);
1799 if (!bs
->encrypted
) {
1801 } else if (!bs
->drv
|| !bs
->drv
->bdrv_set_key
) {
1804 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
1807 } else if (!bs
->valid_key
) {
1809 /* call the change callback now, we skipped it on open */
1810 bdrv_dev_change_media_cb(bs
, true);
1815 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
1820 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
1824 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
1829 QLIST_FOREACH(drv
, &bdrv_drivers
, list
) {
1830 it(opaque
, drv
->format_name
);
1834 BlockDriverState
*bdrv_find(const char *name
)
1836 BlockDriverState
*bs
;
1838 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1839 if (!strcmp(name
, bs
->device_name
)) {
1846 BlockDriverState
*bdrv_next(BlockDriverState
*bs
)
1849 return QTAILQ_FIRST(&bdrv_states
);
1851 return QTAILQ_NEXT(bs
, list
);
1854 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1856 BlockDriverState
*bs
;
1858 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1863 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1865 return bs
->device_name
;
1868 void bdrv_flush_all(void)
1870 BlockDriverState
*bs
;
1872 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1873 if (!bdrv_is_read_only(bs
) && bdrv_is_inserted(bs
)) {
1879 int bdrv_has_zero_init(BlockDriverState
*bs
)
1883 if (bs
->drv
->bdrv_has_zero_init
) {
1884 return bs
->drv
->bdrv_has_zero_init(bs
);
1890 typedef struct BdrvCoIsAllocatedData
{
1891 BlockDriverState
*bs
;
1897 } BdrvCoIsAllocatedData
;
1900 * Returns true iff the specified sector is present in the disk image. Drivers
1901 * not implementing the functionality are assumed to not support backing files,
1902 * hence all their sectors are reported as allocated.
1904 * 'pnum' is set to the number of sectors (including and immediately following
1905 * the specified sector) that are known to be in the same
1906 * allocated/unallocated state.
1908 * 'nb_sectors' is the max value 'pnum' should be set to.
1910 int coroutine_fn
bdrv_co_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1911 int nb_sectors
, int *pnum
)
1913 if (!bs
->drv
->bdrv_co_is_allocated
) {
1915 if (sector_num
>= bs
->total_sectors
) {
1919 n
= bs
->total_sectors
- sector_num
;
1920 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1924 return bs
->drv
->bdrv_co_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1927 /* Coroutine wrapper for bdrv_is_allocated() */
1928 static void coroutine_fn
bdrv_is_allocated_co_entry(void *opaque
)
1930 BdrvCoIsAllocatedData
*data
= opaque
;
1931 BlockDriverState
*bs
= data
->bs
;
1933 data
->ret
= bdrv_co_is_allocated(bs
, data
->sector_num
, data
->nb_sectors
,
1939 * Synchronous wrapper around bdrv_co_is_allocated().
1941 * See bdrv_co_is_allocated() for details.
1943 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1947 BdrvCoIsAllocatedData data
= {
1949 .sector_num
= sector_num
,
1950 .nb_sectors
= nb_sectors
,
1955 co
= qemu_coroutine_create(bdrv_is_allocated_co_entry
);
1956 qemu_coroutine_enter(co
, &data
);
1957 while (!data
.done
) {
1963 void bdrv_mon_event(const BlockDriverState
*bdrv
,
1964 BlockMonEventAction action
, int is_read
)
1967 const char *action_str
;
1970 case BDRV_ACTION_REPORT
:
1971 action_str
= "report";
1973 case BDRV_ACTION_IGNORE
:
1974 action_str
= "ignore";
1976 case BDRV_ACTION_STOP
:
1977 action_str
= "stop";
1983 data
= qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1986 is_read
? "read" : "write");
1987 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR
, data
);
1989 qobject_decref(data
);
1992 BlockInfoList
*qmp_query_block(Error
**errp
)
1994 BlockInfoList
*head
= NULL
, *cur_item
= NULL
;
1995 BlockDriverState
*bs
;
1997 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1998 BlockInfoList
*info
= g_malloc0(sizeof(*info
));
2000 info
->value
= g_malloc0(sizeof(*info
->value
));
2001 info
->value
->device
= g_strdup(bs
->device_name
);
2002 info
->value
->type
= g_strdup("unknown");
2003 info
->value
->locked
= bdrv_dev_is_medium_locked(bs
);
2004 info
->value
->removable
= bdrv_dev_has_removable_media(bs
);
2006 if (bdrv_dev_has_removable_media(bs
)) {
2007 info
->value
->has_tray_open
= true;
2008 info
->value
->tray_open
= bdrv_dev_is_tray_open(bs
);
2011 if (bdrv_iostatus_is_enabled(bs
)) {
2012 info
->value
->has_io_status
= true;
2013 info
->value
->io_status
= bs
->iostatus
;
2017 info
->value
->has_inserted
= true;
2018 info
->value
->inserted
= g_malloc0(sizeof(*info
->value
->inserted
));
2019 info
->value
->inserted
->file
= g_strdup(bs
->filename
);
2020 info
->value
->inserted
->ro
= bs
->read_only
;
2021 info
->value
->inserted
->drv
= g_strdup(bs
->drv
->format_name
);
2022 info
->value
->inserted
->encrypted
= bs
->encrypted
;
2023 if (bs
->backing_file
[0]) {
2024 info
->value
->inserted
->has_backing_file
= true;
2025 info
->value
->inserted
->backing_file
= g_strdup(bs
->backing_file
);
2028 if (bs
->io_limits_enabled
) {
2029 info
->value
->inserted
->bps
=
2030 bs
->io_limits
.bps
[BLOCK_IO_LIMIT_TOTAL
];
2031 info
->value
->inserted
->bps_rd
=
2032 bs
->io_limits
.bps
[BLOCK_IO_LIMIT_READ
];
2033 info
->value
->inserted
->bps_wr
=
2034 bs
->io_limits
.bps
[BLOCK_IO_LIMIT_WRITE
];
2035 info
->value
->inserted
->iops
=
2036 bs
->io_limits
.iops
[BLOCK_IO_LIMIT_TOTAL
];
2037 info
->value
->inserted
->iops_rd
=
2038 bs
->io_limits
.iops
[BLOCK_IO_LIMIT_READ
];
2039 info
->value
->inserted
->iops_wr
=
2040 bs
->io_limits
.iops
[BLOCK_IO_LIMIT_WRITE
];
2044 /* XXX: waiting for the qapi to support GSList */
2046 head
= cur_item
= info
;
2048 cur_item
->next
= info
;
2056 /* Consider exposing this as a full fledged QMP command */
2057 static BlockStats
*qmp_query_blockstat(const BlockDriverState
*bs
, Error
**errp
)
2061 s
= g_malloc0(sizeof(*s
));
2063 if (bs
->device_name
[0]) {
2064 s
->has_device
= true;
2065 s
->device
= g_strdup(bs
->device_name
);
2068 s
->stats
= g_malloc0(sizeof(*s
->stats
));
2069 s
->stats
->rd_bytes
= bs
->nr_bytes
[BDRV_ACCT_READ
];
2070 s
->stats
->wr_bytes
= bs
->nr_bytes
[BDRV_ACCT_WRITE
];
2071 s
->stats
->rd_operations
= bs
->nr_ops
[BDRV_ACCT_READ
];
2072 s
->stats
->wr_operations
= bs
->nr_ops
[BDRV_ACCT_WRITE
];
2073 s
->stats
->wr_highest_offset
= bs
->wr_highest_sector
* BDRV_SECTOR_SIZE
;
2074 s
->stats
->flush_operations
= bs
->nr_ops
[BDRV_ACCT_FLUSH
];
2075 s
->stats
->wr_total_time_ns
= bs
->total_time_ns
[BDRV_ACCT_WRITE
];
2076 s
->stats
->rd_total_time_ns
= bs
->total_time_ns
[BDRV_ACCT_READ
];
2077 s
->stats
->flush_total_time_ns
= bs
->total_time_ns
[BDRV_ACCT_FLUSH
];
2080 s
->has_parent
= true;
2081 s
->parent
= qmp_query_blockstat(bs
->file
, NULL
);
2087 BlockStatsList
*qmp_query_blockstats(Error
**errp
)
2089 BlockStatsList
*head
= NULL
, *cur_item
= NULL
;
2090 BlockDriverState
*bs
;
2092 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
2093 BlockStatsList
*info
= g_malloc0(sizeof(*info
));
2094 info
->value
= qmp_query_blockstat(bs
, NULL
);
2096 /* XXX: waiting for the qapi to support GSList */
2098 head
= cur_item
= info
;
2100 cur_item
->next
= info
;
2108 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
2110 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
2111 return bs
->backing_file
;
2112 else if (bs
->encrypted
)
2113 return bs
->filename
;
2118 void bdrv_get_backing_filename(BlockDriverState
*bs
,
2119 char *filename
, int filename_size
)
2121 pstrcpy(filename
, filename_size
, bs
->backing_file
);
2124 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
2125 const uint8_t *buf
, int nb_sectors
)
2127 BlockDriver
*drv
= bs
->drv
;
2130 if (!drv
->bdrv_write_compressed
)
2132 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
2135 if (bs
->dirty_bitmap
) {
2136 set_dirty_bitmap(bs
, sector_num
, nb_sectors
, 1);
2139 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
2142 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2144 BlockDriver
*drv
= bs
->drv
;
2147 if (!drv
->bdrv_get_info
)
2149 memset(bdi
, 0, sizeof(*bdi
));
2150 return drv
->bdrv_get_info(bs
, bdi
);
2153 int bdrv_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
2154 int64_t pos
, int size
)
2156 BlockDriver
*drv
= bs
->drv
;
2159 if (drv
->bdrv_save_vmstate
)
2160 return drv
->bdrv_save_vmstate(bs
, buf
, pos
, size
);
2162 return bdrv_save_vmstate(bs
->file
, buf
, pos
, size
);
2166 int bdrv_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
2167 int64_t pos
, int size
)
2169 BlockDriver
*drv
= bs
->drv
;
2172 if (drv
->bdrv_load_vmstate
)
2173 return drv
->bdrv_load_vmstate(bs
, buf
, pos
, size
);
2175 return bdrv_load_vmstate(bs
->file
, buf
, pos
, size
);
2179 void bdrv_debug_event(BlockDriverState
*bs
, BlkDebugEvent event
)
2181 BlockDriver
*drv
= bs
->drv
;
2183 if (!drv
|| !drv
->bdrv_debug_event
) {
2187 return drv
->bdrv_debug_event(bs
, event
);
2191 /**************************************************************/
2192 /* handling of snapshots */
2194 int bdrv_can_snapshot(BlockDriverState
*bs
)
2196 BlockDriver
*drv
= bs
->drv
;
2197 if (!drv
|| !bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2201 if (!drv
->bdrv_snapshot_create
) {
2202 if (bs
->file
!= NULL
) {
2203 return bdrv_can_snapshot(bs
->file
);
2211 int bdrv_is_snapshot(BlockDriverState
*bs
)
2213 return !!(bs
->open_flags
& BDRV_O_SNAPSHOT
);
2216 BlockDriverState
*bdrv_snapshots(void)
2218 BlockDriverState
*bs
;
2221 return bs_snapshots
;
2225 while ((bs
= bdrv_next(bs
))) {
2226 if (bdrv_can_snapshot(bs
)) {
2234 int bdrv_snapshot_create(BlockDriverState
*bs
,
2235 QEMUSnapshotInfo
*sn_info
)
2237 BlockDriver
*drv
= bs
->drv
;
2240 if (drv
->bdrv_snapshot_create
)
2241 return drv
->bdrv_snapshot_create(bs
, sn_info
);
2243 return bdrv_snapshot_create(bs
->file
, sn_info
);
2247 int bdrv_snapshot_goto(BlockDriverState
*bs
,
2248 const char *snapshot_id
)
2250 BlockDriver
*drv
= bs
->drv
;
2255 if (drv
->bdrv_snapshot_goto
)
2256 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
2259 drv
->bdrv_close(bs
);
2260 ret
= bdrv_snapshot_goto(bs
->file
, snapshot_id
);
2261 open_ret
= drv
->bdrv_open(bs
, bs
->open_flags
);
2263 bdrv_delete(bs
->file
);
2273 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
2275 BlockDriver
*drv
= bs
->drv
;
2278 if (drv
->bdrv_snapshot_delete
)
2279 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
2281 return bdrv_snapshot_delete(bs
->file
, snapshot_id
);
2285 int bdrv_snapshot_list(BlockDriverState
*bs
,
2286 QEMUSnapshotInfo
**psn_info
)
2288 BlockDriver
*drv
= bs
->drv
;
2291 if (drv
->bdrv_snapshot_list
)
2292 return drv
->bdrv_snapshot_list(bs
, psn_info
);
2294 return bdrv_snapshot_list(bs
->file
, psn_info
);
2298 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
2299 const char *snapshot_name
)
2301 BlockDriver
*drv
= bs
->drv
;
2305 if (!bs
->read_only
) {
2308 if (drv
->bdrv_snapshot_load_tmp
) {
2309 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_name
);
2314 #define NB_SUFFIXES 4
2316 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
2318 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
2323 snprintf(buf
, buf_size
, "%" PRId64
, size
);
2326 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
2327 if (size
< (10 * base
)) {
2328 snprintf(buf
, buf_size
, "%0.1f%c",
2329 (double)size
/ base
,
2332 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
2333 snprintf(buf
, buf_size
, "%" PRId64
"%c",
2334 ((size
+ (base
>> 1)) / base
),
2344 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
2346 char buf1
[128], date_buf
[128], clock_buf
[128];
2356 snprintf(buf
, buf_size
,
2357 "%-10s%-20s%7s%20s%15s",
2358 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
2362 ptm
= localtime(&ti
);
2363 strftime(date_buf
, sizeof(date_buf
),
2364 "%Y-%m-%d %H:%M:%S", ptm
);
2366 localtime_r(&ti
, &tm
);
2367 strftime(date_buf
, sizeof(date_buf
),
2368 "%Y-%m-%d %H:%M:%S", &tm
);
2370 secs
= sn
->vm_clock_nsec
/ 1000000000;
2371 snprintf(clock_buf
, sizeof(clock_buf
),
2372 "%02d:%02d:%02d.%03d",
2374 (int)((secs
/ 60) % 60),
2376 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
2377 snprintf(buf
, buf_size
,
2378 "%-10s%-20s%7s%20s%15s",
2379 sn
->id_str
, sn
->name
,
2380 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
2387 /**************************************************************/
2390 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
2391 QEMUIOVector
*qiov
, int nb_sectors
,
2392 BlockDriverCompletionFunc
*cb
, void *opaque
)
2394 trace_bdrv_aio_readv(bs
, sector_num
, nb_sectors
, opaque
);
2396 return bdrv_co_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
,
2400 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
2401 QEMUIOVector
*qiov
, int nb_sectors
,
2402 BlockDriverCompletionFunc
*cb
, void *opaque
)
2404 trace_bdrv_aio_writev(bs
, sector_num
, nb_sectors
, opaque
);
2406 return bdrv_co_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
,
2411 typedef struct MultiwriteCB
{
2416 BlockDriverCompletionFunc
*cb
;
2418 QEMUIOVector
*free_qiov
;
2423 static void multiwrite_user_cb(MultiwriteCB
*mcb
)
2427 for (i
= 0; i
< mcb
->num_callbacks
; i
++) {
2428 mcb
->callbacks
[i
].cb(mcb
->callbacks
[i
].opaque
, mcb
->error
);
2429 if (mcb
->callbacks
[i
].free_qiov
) {
2430 qemu_iovec_destroy(mcb
->callbacks
[i
].free_qiov
);
2432 g_free(mcb
->callbacks
[i
].free_qiov
);
2433 qemu_vfree(mcb
->callbacks
[i
].free_buf
);
2437 static void multiwrite_cb(void *opaque
, int ret
)
2439 MultiwriteCB
*mcb
= opaque
;
2441 trace_multiwrite_cb(mcb
, ret
);
2443 if (ret
< 0 && !mcb
->error
) {
2447 mcb
->num_requests
--;
2448 if (mcb
->num_requests
== 0) {
2449 multiwrite_user_cb(mcb
);
2454 static int multiwrite_req_compare(const void *a
, const void *b
)
2456 const BlockRequest
*req1
= a
, *req2
= b
;
2459 * Note that we can't simply subtract req2->sector from req1->sector
2460 * here as that could overflow the return value.
2462 if (req1
->sector
> req2
->sector
) {
2464 } else if (req1
->sector
< req2
->sector
) {
2472 * Takes a bunch of requests and tries to merge them. Returns the number of
2473 * requests that remain after merging.
2475 static int multiwrite_merge(BlockDriverState
*bs
, BlockRequest
*reqs
,
2476 int num_reqs
, MultiwriteCB
*mcb
)
2480 // Sort requests by start sector
2481 qsort(reqs
, num_reqs
, sizeof(*reqs
), &multiwrite_req_compare
);
2483 // Check if adjacent requests touch the same clusters. If so, combine them,
2484 // filling up gaps with zero sectors.
2486 for (i
= 1; i
< num_reqs
; i
++) {
2488 int64_t oldreq_last
= reqs
[outidx
].sector
+ reqs
[outidx
].nb_sectors
;
2490 // This handles the cases that are valid for all block drivers, namely
2491 // exactly sequential writes and overlapping writes.
2492 if (reqs
[i
].sector
<= oldreq_last
) {
2496 // The block driver may decide that it makes sense to combine requests
2497 // even if there is a gap of some sectors between them. In this case,
2498 // the gap is filled with zeros (therefore only applicable for yet
2499 // unused space in format like qcow2).
2500 if (!merge
&& bs
->drv
->bdrv_merge_requests
) {
2501 merge
= bs
->drv
->bdrv_merge_requests(bs
, &reqs
[outidx
], &reqs
[i
]);
2504 if (reqs
[outidx
].qiov
->niov
+ reqs
[i
].qiov
->niov
+ 1 > IOV_MAX
) {
2510 QEMUIOVector
*qiov
= g_malloc0(sizeof(*qiov
));
2511 qemu_iovec_init(qiov
,
2512 reqs
[outidx
].qiov
->niov
+ reqs
[i
].qiov
->niov
+ 1);
2514 // Add the first request to the merged one. If the requests are
2515 // overlapping, drop the last sectors of the first request.
2516 size
= (reqs
[i
].sector
- reqs
[outidx
].sector
) << 9;
2517 qemu_iovec_concat(qiov
, reqs
[outidx
].qiov
, size
);
2519 // We might need to add some zeros between the two requests
2520 if (reqs
[i
].sector
> oldreq_last
) {
2521 size_t zero_bytes
= (reqs
[i
].sector
- oldreq_last
) << 9;
2522 uint8_t *buf
= qemu_blockalign(bs
, zero_bytes
);
2523 memset(buf
, 0, zero_bytes
);
2524 qemu_iovec_add(qiov
, buf
, zero_bytes
);
2525 mcb
->callbacks
[i
].free_buf
= buf
;
2528 // Add the second request
2529 qemu_iovec_concat(qiov
, reqs
[i
].qiov
, reqs
[i
].qiov
->size
);
2531 reqs
[outidx
].nb_sectors
= qiov
->size
>> 9;
2532 reqs
[outidx
].qiov
= qiov
;
2534 mcb
->callbacks
[i
].free_qiov
= reqs
[outidx
].qiov
;
2537 reqs
[outidx
].sector
= reqs
[i
].sector
;
2538 reqs
[outidx
].nb_sectors
= reqs
[i
].nb_sectors
;
2539 reqs
[outidx
].qiov
= reqs
[i
].qiov
;
2547 * Submit multiple AIO write requests at once.
2549 * On success, the function returns 0 and all requests in the reqs array have
2550 * been submitted. In error case this function returns -1, and any of the
2551 * requests may or may not be submitted yet. In particular, this means that the
2552 * callback will be called for some of the requests, for others it won't. The
2553 * caller must check the error field of the BlockRequest to wait for the right
2554 * callbacks (if error != 0, no callback will be called).
2556 * The implementation may modify the contents of the reqs array, e.g. to merge
2557 * requests. However, the fields opaque and error are left unmodified as they
2558 * are used to signal failure for a single request to the caller.
2560 int bdrv_aio_multiwrite(BlockDriverState
*bs
, BlockRequest
*reqs
, int num_reqs
)
2562 BlockDriverAIOCB
*acb
;
2566 /* don't submit writes if we don't have a medium */
2567 if (bs
->drv
== NULL
) {
2568 for (i
= 0; i
< num_reqs
; i
++) {
2569 reqs
[i
].error
= -ENOMEDIUM
;
2574 if (num_reqs
== 0) {
2578 // Create MultiwriteCB structure
2579 mcb
= g_malloc0(sizeof(*mcb
) + num_reqs
* sizeof(*mcb
->callbacks
));
2580 mcb
->num_requests
= 0;
2581 mcb
->num_callbacks
= num_reqs
;
2583 for (i
= 0; i
< num_reqs
; i
++) {
2584 mcb
->callbacks
[i
].cb
= reqs
[i
].cb
;
2585 mcb
->callbacks
[i
].opaque
= reqs
[i
].opaque
;
2588 // Check for mergable requests
2589 num_reqs
= multiwrite_merge(bs
, reqs
, num_reqs
, mcb
);
2591 trace_bdrv_aio_multiwrite(mcb
, mcb
->num_callbacks
, num_reqs
);
2594 * Run the aio requests. As soon as one request can't be submitted
2595 * successfully, fail all requests that are not yet submitted (we must
2596 * return failure for all requests anyway)
2598 * num_requests cannot be set to the right value immediately: If
2599 * bdrv_aio_writev fails for some request, num_requests would be too high
2600 * and therefore multiwrite_cb() would never recognize the multiwrite
2601 * request as completed. We also cannot use the loop variable i to set it
2602 * when the first request fails because the callback may already have been
2603 * called for previously submitted requests. Thus, num_requests must be
2604 * incremented for each request that is submitted.
2606 * The problem that callbacks may be called early also means that we need
2607 * to take care that num_requests doesn't become 0 before all requests are
2608 * submitted - multiwrite_cb() would consider the multiwrite request
2609 * completed. A dummy request that is "completed" by a manual call to
2610 * multiwrite_cb() takes care of this.
2612 mcb
->num_requests
= 1;
2614 // Run the aio requests
2615 for (i
= 0; i
< num_reqs
; i
++) {
2616 mcb
->num_requests
++;
2617 acb
= bdrv_aio_writev(bs
, reqs
[i
].sector
, reqs
[i
].qiov
,
2618 reqs
[i
].nb_sectors
, multiwrite_cb
, mcb
);
2621 // We can only fail the whole thing if no request has been
2622 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2623 // complete and report the error in the callback.
2625 trace_bdrv_aio_multiwrite_earlyfail(mcb
);
2628 trace_bdrv_aio_multiwrite_latefail(mcb
, i
);
2629 multiwrite_cb(mcb
, -EIO
);
2635 /* Complete the dummy request */
2636 multiwrite_cb(mcb
, 0);
2641 for (i
= 0; i
< mcb
->num_callbacks
; i
++) {
2642 reqs
[i
].error
= -EIO
;
2648 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
2650 acb
->pool
->cancel(acb
);
2653 /* block I/O throttling */
2654 static bool bdrv_exceed_bps_limits(BlockDriverState
*bs
, int nb_sectors
,
2655 bool is_write
, double elapsed_time
, uint64_t *wait
)
2657 uint64_t bps_limit
= 0;
2658 double bytes_limit
, bytes_base
, bytes_res
;
2659 double slice_time
, wait_time
;
2661 if (bs
->io_limits
.bps
[BLOCK_IO_LIMIT_TOTAL
]) {
2662 bps_limit
= bs
->io_limits
.bps
[BLOCK_IO_LIMIT_TOTAL
];
2663 } else if (bs
->io_limits
.bps
[is_write
]) {
2664 bps_limit
= bs
->io_limits
.bps
[is_write
];
2673 slice_time
= bs
->slice_end
- bs
->slice_start
;
2674 slice_time
/= (NANOSECONDS_PER_SECOND
);
2675 bytes_limit
= bps_limit
* slice_time
;
2676 bytes_base
= bs
->nr_bytes
[is_write
] - bs
->io_base
.bytes
[is_write
];
2677 if (bs
->io_limits
.bps
[BLOCK_IO_LIMIT_TOTAL
]) {
2678 bytes_base
+= bs
->nr_bytes
[!is_write
] - bs
->io_base
.bytes
[!is_write
];
2681 /* bytes_base: the bytes of data which have been read/written; and
2682 * it is obtained from the history statistic info.
2683 * bytes_res: the remaining bytes of data which need to be read/written.
2684 * (bytes_base + bytes_res) / bps_limit: used to calcuate
2685 * the total time for completing reading/writting all data.
2687 bytes_res
= (unsigned) nb_sectors
* BDRV_SECTOR_SIZE
;
2689 if (bytes_base
+ bytes_res
<= bytes_limit
) {
2697 /* Calc approx time to dispatch */
2698 wait_time
= (bytes_base
+ bytes_res
) / bps_limit
- elapsed_time
;
2700 /* When the I/O rate at runtime exceeds the limits,
2701 * bs->slice_end need to be extended in order that the current statistic
2702 * info can be kept until the timer fire, so it is increased and tuned
2703 * based on the result of experiment.
2705 bs
->slice_time
= wait_time
* BLOCK_IO_SLICE_TIME
* 10;
2706 bs
->slice_end
+= bs
->slice_time
- 3 * BLOCK_IO_SLICE_TIME
;
2708 *wait
= wait_time
* BLOCK_IO_SLICE_TIME
* 10;
2714 static bool bdrv_exceed_iops_limits(BlockDriverState
*bs
, bool is_write
,
2715 double elapsed_time
, uint64_t *wait
)
2717 uint64_t iops_limit
= 0;
2718 double ios_limit
, ios_base
;
2719 double slice_time
, wait_time
;
2721 if (bs
->io_limits
.iops
[BLOCK_IO_LIMIT_TOTAL
]) {
2722 iops_limit
= bs
->io_limits
.iops
[BLOCK_IO_LIMIT_TOTAL
];
2723 } else if (bs
->io_limits
.iops
[is_write
]) {
2724 iops_limit
= bs
->io_limits
.iops
[is_write
];
2733 slice_time
= bs
->slice_end
- bs
->slice_start
;
2734 slice_time
/= (NANOSECONDS_PER_SECOND
);
2735 ios_limit
= iops_limit
* slice_time
;
2736 ios_base
= bs
->nr_ops
[is_write
] - bs
->io_base
.ios
[is_write
];
2737 if (bs
->io_limits
.iops
[BLOCK_IO_LIMIT_TOTAL
]) {
2738 ios_base
+= bs
->nr_ops
[!is_write
] - bs
->io_base
.ios
[!is_write
];
2741 if (ios_base
+ 1 <= ios_limit
) {
2749 /* Calc approx time to dispatch */
2750 wait_time
= (ios_base
+ 1) / iops_limit
;
2751 if (wait_time
> elapsed_time
) {
2752 wait_time
= wait_time
- elapsed_time
;
2757 bs
->slice_time
= wait_time
* BLOCK_IO_SLICE_TIME
* 10;
2758 bs
->slice_end
+= bs
->slice_time
- 3 * BLOCK_IO_SLICE_TIME
;
2760 *wait
= wait_time
* BLOCK_IO_SLICE_TIME
* 10;
2766 static bool bdrv_exceed_io_limits(BlockDriverState
*bs
, int nb_sectors
,
2767 bool is_write
, int64_t *wait
)
2769 int64_t now
, max_wait
;
2770 uint64_t bps_wait
= 0, iops_wait
= 0;
2771 double elapsed_time
;
2772 int bps_ret
, iops_ret
;
2774 now
= qemu_get_clock_ns(vm_clock
);
2775 if ((bs
->slice_start
< now
)
2776 && (bs
->slice_end
> now
)) {
2777 bs
->slice_end
= now
+ bs
->slice_time
;
2779 bs
->slice_time
= 5 * BLOCK_IO_SLICE_TIME
;
2780 bs
->slice_start
= now
;
2781 bs
->slice_end
= now
+ bs
->slice_time
;
2783 bs
->io_base
.bytes
[is_write
] = bs
->nr_bytes
[is_write
];
2784 bs
->io_base
.bytes
[!is_write
] = bs
->nr_bytes
[!is_write
];
2786 bs
->io_base
.ios
[is_write
] = bs
->nr_ops
[is_write
];
2787 bs
->io_base
.ios
[!is_write
] = bs
->nr_ops
[!is_write
];
2790 elapsed_time
= now
- bs
->slice_start
;
2791 elapsed_time
/= (NANOSECONDS_PER_SECOND
);
2793 bps_ret
= bdrv_exceed_bps_limits(bs
, nb_sectors
,
2794 is_write
, elapsed_time
, &bps_wait
);
2795 iops_ret
= bdrv_exceed_iops_limits(bs
, is_write
,
2796 elapsed_time
, &iops_wait
);
2797 if (bps_ret
|| iops_ret
) {
2798 max_wait
= bps_wait
> iops_wait
? bps_wait
: iops_wait
;
2803 now
= qemu_get_clock_ns(vm_clock
);
2804 if (bs
->slice_end
< now
+ max_wait
) {
2805 bs
->slice_end
= now
+ max_wait
;
2818 /**************************************************************/
2819 /* async block device emulation */
2821 typedef struct BlockDriverAIOCBSync
{
2822 BlockDriverAIOCB common
;
2825 /* vector translation state */
2829 } BlockDriverAIOCBSync
;
2831 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
2833 BlockDriverAIOCBSync
*acb
=
2834 container_of(blockacb
, BlockDriverAIOCBSync
, common
);
2835 qemu_bh_delete(acb
->bh
);
2837 qemu_aio_release(acb
);
2840 static AIOPool bdrv_em_aio_pool
= {
2841 .aiocb_size
= sizeof(BlockDriverAIOCBSync
),
2842 .cancel
= bdrv_aio_cancel_em
,
2845 static void bdrv_aio_bh_cb(void *opaque
)
2847 BlockDriverAIOCBSync
*acb
= opaque
;
2850 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
2851 qemu_vfree(acb
->bounce
);
2852 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
2853 qemu_bh_delete(acb
->bh
);
2855 qemu_aio_release(acb
);
2858 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
2862 BlockDriverCompletionFunc
*cb
,
2867 BlockDriverAIOCBSync
*acb
;
2869 acb
= qemu_aio_get(&bdrv_em_aio_pool
, bs
, cb
, opaque
);
2870 acb
->is_write
= is_write
;
2872 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
2875 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
2878 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
2879 acb
->ret
= bs
->drv
->bdrv_write(bs
, sector_num
, acb
->bounce
, nb_sectors
);
2881 acb
->ret
= bs
->drv
->bdrv_read(bs
, sector_num
, acb
->bounce
, nb_sectors
);
2884 qemu_bh_schedule(acb
->bh
);
2886 return &acb
->common
;
2889 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
2890 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
2891 BlockDriverCompletionFunc
*cb
, void *opaque
)
2893 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
2896 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
2897 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
2898 BlockDriverCompletionFunc
*cb
, void *opaque
)
2900 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
2904 typedef struct BlockDriverAIOCBCoroutine
{
2905 BlockDriverAIOCB common
;
2909 } BlockDriverAIOCBCoroutine
;
2911 static void bdrv_aio_co_cancel_em(BlockDriverAIOCB
*blockacb
)
2916 static AIOPool bdrv_em_co_aio_pool
= {
2917 .aiocb_size
= sizeof(BlockDriverAIOCBCoroutine
),
2918 .cancel
= bdrv_aio_co_cancel_em
,
2921 static void bdrv_co_em_bh(void *opaque
)
2923 BlockDriverAIOCBCoroutine
*acb
= opaque
;
2925 acb
->common
.cb(acb
->common
.opaque
, acb
->req
.error
);
2926 qemu_bh_delete(acb
->bh
);
2927 qemu_aio_release(acb
);
2930 /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
2931 static void coroutine_fn
bdrv_co_do_rw(void *opaque
)
2933 BlockDriverAIOCBCoroutine
*acb
= opaque
;
2934 BlockDriverState
*bs
= acb
->common
.bs
;
2936 if (!acb
->is_write
) {
2937 acb
->req
.error
= bdrv_co_do_readv(bs
, acb
->req
.sector
,
2938 acb
->req
.nb_sectors
, acb
->req
.qiov
);
2940 acb
->req
.error
= bdrv_co_do_writev(bs
, acb
->req
.sector
,
2941 acb
->req
.nb_sectors
, acb
->req
.qiov
);
2944 acb
->bh
= qemu_bh_new(bdrv_co_em_bh
, acb
);
2945 qemu_bh_schedule(acb
->bh
);
2948 static BlockDriverAIOCB
*bdrv_co_aio_rw_vector(BlockDriverState
*bs
,
2952 BlockDriverCompletionFunc
*cb
,
2957 BlockDriverAIOCBCoroutine
*acb
;
2959 acb
= qemu_aio_get(&bdrv_em_co_aio_pool
, bs
, cb
, opaque
);
2960 acb
->req
.sector
= sector_num
;
2961 acb
->req
.nb_sectors
= nb_sectors
;
2962 acb
->req
.qiov
= qiov
;
2963 acb
->is_write
= is_write
;
2965 co
= qemu_coroutine_create(bdrv_co_do_rw
);
2966 qemu_coroutine_enter(co
, acb
);
2968 return &acb
->common
;
2971 static void coroutine_fn
bdrv_aio_flush_co_entry(void *opaque
)
2973 BlockDriverAIOCBCoroutine
*acb
= opaque
;
2974 BlockDriverState
*bs
= acb
->common
.bs
;
2976 acb
->req
.error
= bdrv_co_flush(bs
);
2977 acb
->bh
= qemu_bh_new(bdrv_co_em_bh
, acb
);
2978 qemu_bh_schedule(acb
->bh
);
2981 BlockDriverAIOCB
*bdrv_aio_flush(BlockDriverState
*bs
,
2982 BlockDriverCompletionFunc
*cb
, void *opaque
)
2984 trace_bdrv_aio_flush(bs
, opaque
);
2987 BlockDriverAIOCBCoroutine
*acb
;
2989 acb
= qemu_aio_get(&bdrv_em_co_aio_pool
, bs
, cb
, opaque
);
2990 co
= qemu_coroutine_create(bdrv_aio_flush_co_entry
);
2991 qemu_coroutine_enter(co
, acb
);
2993 return &acb
->common
;
2996 static void coroutine_fn
bdrv_aio_discard_co_entry(void *opaque
)
2998 BlockDriverAIOCBCoroutine
*acb
= opaque
;
2999 BlockDriverState
*bs
= acb
->common
.bs
;
3001 acb
->req
.error
= bdrv_co_discard(bs
, acb
->req
.sector
, acb
->req
.nb_sectors
);
3002 acb
->bh
= qemu_bh_new(bdrv_co_em_bh
, acb
);
3003 qemu_bh_schedule(acb
->bh
);
3006 BlockDriverAIOCB
*bdrv_aio_discard(BlockDriverState
*bs
,
3007 int64_t sector_num
, int nb_sectors
,
3008 BlockDriverCompletionFunc
*cb
, void *opaque
)
3011 BlockDriverAIOCBCoroutine
*acb
;
3013 trace_bdrv_aio_discard(bs
, sector_num
, nb_sectors
, opaque
);
3015 acb
= qemu_aio_get(&bdrv_em_co_aio_pool
, bs
, cb
, opaque
);
3016 acb
->req
.sector
= sector_num
;
3017 acb
->req
.nb_sectors
= nb_sectors
;
3018 co
= qemu_coroutine_create(bdrv_aio_discard_co_entry
);
3019 qemu_coroutine_enter(co
, acb
);
3021 return &acb
->common
;
3024 void bdrv_init(void)
3026 module_call_init(MODULE_INIT_BLOCK
);
3029 void bdrv_init_with_whitelist(void)
3031 use_bdrv_whitelist
= 1;
3035 void *qemu_aio_get(AIOPool
*pool
, BlockDriverState
*bs
,
3036 BlockDriverCompletionFunc
*cb
, void *opaque
)
3038 BlockDriverAIOCB
*acb
;
3040 if (pool
->free_aiocb
) {
3041 acb
= pool
->free_aiocb
;
3042 pool
->free_aiocb
= acb
->next
;
3044 acb
= g_malloc0(pool
->aiocb_size
);
3049 acb
->opaque
= opaque
;
3053 void qemu_aio_release(void *p
)
3055 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
3056 AIOPool
*pool
= acb
->pool
;
3057 acb
->next
= pool
->free_aiocb
;
3058 pool
->free_aiocb
= acb
;
3061 /**************************************************************/
3062 /* Coroutine block device emulation */
3064 typedef struct CoroutineIOCompletion
{
3065 Coroutine
*coroutine
;
3067 } CoroutineIOCompletion
;
3069 static void bdrv_co_io_em_complete(void *opaque
, int ret
)
3071 CoroutineIOCompletion
*co
= opaque
;
3074 qemu_coroutine_enter(co
->coroutine
, NULL
);
3077 static int coroutine_fn
bdrv_co_io_em(BlockDriverState
*bs
, int64_t sector_num
,
3078 int nb_sectors
, QEMUIOVector
*iov
,
3081 CoroutineIOCompletion co
= {
3082 .coroutine
= qemu_coroutine_self(),
3084 BlockDriverAIOCB
*acb
;
3087 acb
= bs
->drv
->bdrv_aio_writev(bs
, sector_num
, iov
, nb_sectors
,
3088 bdrv_co_io_em_complete
, &co
);
3090 acb
= bs
->drv
->bdrv_aio_readv(bs
, sector_num
, iov
, nb_sectors
,
3091 bdrv_co_io_em_complete
, &co
);
3094 trace_bdrv_co_io_em(bs
, sector_num
, nb_sectors
, is_write
, acb
);
3098 qemu_coroutine_yield();
3103 static int coroutine_fn
bdrv_co_readv_em(BlockDriverState
*bs
,
3104 int64_t sector_num
, int nb_sectors
,
3107 return bdrv_co_io_em(bs
, sector_num
, nb_sectors
, iov
, false);
3110 static int coroutine_fn
bdrv_co_writev_em(BlockDriverState
*bs
,
3111 int64_t sector_num
, int nb_sectors
,
3114 return bdrv_co_io_em(bs
, sector_num
, nb_sectors
, iov
, true);
3117 static void coroutine_fn
bdrv_flush_co_entry(void *opaque
)
3119 RwCo
*rwco
= opaque
;
3121 rwco
->ret
= bdrv_co_flush(rwco
->bs
);
3124 int coroutine_fn
bdrv_co_flush(BlockDriverState
*bs
)
3132 /* Write back cached data to the OS even with cache=unsafe */
3133 if (bs
->drv
->bdrv_co_flush_to_os
) {
3134 ret
= bs
->drv
->bdrv_co_flush_to_os(bs
);
3140 /* But don't actually force it to the disk with cache=unsafe */
3141 if (bs
->open_flags
& BDRV_O_NO_FLUSH
) {
3145 if (bs
->drv
->bdrv_co_flush_to_disk
) {
3146 return bs
->drv
->bdrv_co_flush_to_disk(bs
);
3147 } else if (bs
->drv
->bdrv_aio_flush
) {
3148 BlockDriverAIOCB
*acb
;
3149 CoroutineIOCompletion co
= {
3150 .coroutine
= qemu_coroutine_self(),
3153 acb
= bs
->drv
->bdrv_aio_flush(bs
, bdrv_co_io_em_complete
, &co
);
3157 qemu_coroutine_yield();
3162 * Some block drivers always operate in either writethrough or unsafe
3163 * mode and don't support bdrv_flush therefore. Usually qemu doesn't
3164 * know how the server works (because the behaviour is hardcoded or
3165 * depends on server-side configuration), so we can't ensure that
3166 * everything is safe on disk. Returning an error doesn't work because
3167 * that would break guests even if the server operates in writethrough
3170 * Let's hope the user knows what he's doing.
3176 void bdrv_invalidate_cache(BlockDriverState
*bs
)
3178 if (bs
->drv
&& bs
->drv
->bdrv_invalidate_cache
) {
3179 bs
->drv
->bdrv_invalidate_cache(bs
);
3183 void bdrv_invalidate_cache_all(void)
3185 BlockDriverState
*bs
;
3187 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
3188 bdrv_invalidate_cache(bs
);
3192 int bdrv_flush(BlockDriverState
*bs
)
3200 if (qemu_in_coroutine()) {
3201 /* Fast-path if already in coroutine context */
3202 bdrv_flush_co_entry(&rwco
);
3204 co
= qemu_coroutine_create(bdrv_flush_co_entry
);
3205 qemu_coroutine_enter(co
, &rwco
);
3206 while (rwco
.ret
== NOT_DONE
) {
3214 static void coroutine_fn
bdrv_discard_co_entry(void *opaque
)
3216 RwCo
*rwco
= opaque
;
3218 rwco
->ret
= bdrv_co_discard(rwco
->bs
, rwco
->sector_num
, rwco
->nb_sectors
);
3221 int coroutine_fn
bdrv_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
3226 } else if (bdrv_check_request(bs
, sector_num
, nb_sectors
)) {
3228 } else if (bs
->read_only
) {
3230 } else if (bs
->drv
->bdrv_co_discard
) {
3231 return bs
->drv
->bdrv_co_discard(bs
, sector_num
, nb_sectors
);
3232 } else if (bs
->drv
->bdrv_aio_discard
) {
3233 BlockDriverAIOCB
*acb
;
3234 CoroutineIOCompletion co
= {
3235 .coroutine
= qemu_coroutine_self(),
3238 acb
= bs
->drv
->bdrv_aio_discard(bs
, sector_num
, nb_sectors
,
3239 bdrv_co_io_em_complete
, &co
);
3243 qemu_coroutine_yield();
3251 int bdrv_discard(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
)
3256 .sector_num
= sector_num
,
3257 .nb_sectors
= nb_sectors
,
3261 if (qemu_in_coroutine()) {
3262 /* Fast-path if already in coroutine context */
3263 bdrv_discard_co_entry(&rwco
);
3265 co
= qemu_coroutine_create(bdrv_discard_co_entry
);
3266 qemu_coroutine_enter(co
, &rwco
);
3267 while (rwco
.ret
== NOT_DONE
) {
3275 /**************************************************************/
3276 /* removable device support */
3279 * Return TRUE if the media is present
3281 int bdrv_is_inserted(BlockDriverState
*bs
)
3283 BlockDriver
*drv
= bs
->drv
;
3287 if (!drv
->bdrv_is_inserted
)
3289 return drv
->bdrv_is_inserted(bs
);
3293 * Return whether the media changed since the last call to this
3294 * function, or -ENOTSUP if we don't know. Most drivers don't know.
3296 int bdrv_media_changed(BlockDriverState
*bs
)
3298 BlockDriver
*drv
= bs
->drv
;
3300 if (drv
&& drv
->bdrv_media_changed
) {
3301 return drv
->bdrv_media_changed(bs
);
3307 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3309 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
3311 BlockDriver
*drv
= bs
->drv
;
3313 if (drv
&& drv
->bdrv_eject
) {
3314 drv
->bdrv_eject(bs
, eject_flag
);
3319 * Lock or unlock the media (if it is locked, the user won't be able
3320 * to eject it manually).
3322 void bdrv_lock_medium(BlockDriverState
*bs
, bool locked
)
3324 BlockDriver
*drv
= bs
->drv
;
3326 trace_bdrv_lock_medium(bs
, locked
);
3328 if (drv
&& drv
->bdrv_lock_medium
) {
3329 drv
->bdrv_lock_medium(bs
, locked
);
3333 /* needed for generic scsi interface */
3335 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
3337 BlockDriver
*drv
= bs
->drv
;
3339 if (drv
&& drv
->bdrv_ioctl
)
3340 return drv
->bdrv_ioctl(bs
, req
, buf
);
3344 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
3345 unsigned long int req
, void *buf
,
3346 BlockDriverCompletionFunc
*cb
, void *opaque
)
3348 BlockDriver
*drv
= bs
->drv
;
3350 if (drv
&& drv
->bdrv_aio_ioctl
)
3351 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);
3355 void bdrv_set_buffer_alignment(BlockDriverState
*bs
, int align
)
3357 bs
->buffer_alignment
= align
;
3360 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
3362 return qemu_memalign((bs
&& bs
->buffer_alignment
) ? bs
->buffer_alignment
: 512, size
);
3365 void bdrv_set_dirty_tracking(BlockDriverState
*bs
, int enable
)
3367 int64_t bitmap_size
;
3369 bs
->dirty_count
= 0;
3371 if (!bs
->dirty_bitmap
) {
3372 bitmap_size
= (bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
) +
3373 BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
3374 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
3376 bs
->dirty_bitmap
= g_malloc0(bitmap_size
);
3379 if (bs
->dirty_bitmap
) {
3380 g_free(bs
->dirty_bitmap
);
3381 bs
->dirty_bitmap
= NULL
;
3386 int bdrv_get_dirty(BlockDriverState
*bs
, int64_t sector
)
3388 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
3390 if (bs
->dirty_bitmap
&&
3391 (sector
<< BDRV_SECTOR_BITS
) < bdrv_getlength(bs
)) {
3392 return !!(bs
->dirty_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
3393 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
3399 void bdrv_reset_dirty(BlockDriverState
*bs
, int64_t cur_sector
,
3402 set_dirty_bitmap(bs
, cur_sector
, nr_sectors
, 0);
3405 int64_t bdrv_get_dirty_count(BlockDriverState
*bs
)
3407 return bs
->dirty_count
;
3410 void bdrv_set_in_use(BlockDriverState
*bs
, int in_use
)
3412 assert(bs
->in_use
!= in_use
);
3413 bs
->in_use
= in_use
;
3416 int bdrv_in_use(BlockDriverState
*bs
)
3421 void bdrv_iostatus_enable(BlockDriverState
*bs
)
3423 bs
->iostatus_enabled
= true;
3424 bs
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
3427 /* The I/O status is only enabled if the drive explicitly
3428 * enables it _and_ the VM is configured to stop on errors */
3429 bool bdrv_iostatus_is_enabled(const BlockDriverState
*bs
)
3431 return (bs
->iostatus_enabled
&&
3432 (bs
->on_write_error
== BLOCK_ERR_STOP_ENOSPC
||
3433 bs
->on_write_error
== BLOCK_ERR_STOP_ANY
||
3434 bs
->on_read_error
== BLOCK_ERR_STOP_ANY
));
3437 void bdrv_iostatus_disable(BlockDriverState
*bs
)
3439 bs
->iostatus_enabled
= false;
3442 void bdrv_iostatus_reset(BlockDriverState
*bs
)
3444 if (bdrv_iostatus_is_enabled(bs
)) {
3445 bs
->iostatus
= BLOCK_DEVICE_IO_STATUS_OK
;
3449 /* XXX: Today this is set by device models because it makes the implementation
3450 quite simple. However, the block layer knows about the error, so it's
3451 possible to implement this without device models being involved */
3452 void bdrv_iostatus_set_err(BlockDriverState
*bs
, int error
)
3454 if (bdrv_iostatus_is_enabled(bs
) &&
3455 bs
->iostatus
== BLOCK_DEVICE_IO_STATUS_OK
) {
3457 bs
->iostatus
= error
== ENOSPC
? BLOCK_DEVICE_IO_STATUS_NOSPACE
:
3458 BLOCK_DEVICE_IO_STATUS_FAILED
;
3463 bdrv_acct_start(BlockDriverState
*bs
, BlockAcctCookie
*cookie
, int64_t bytes
,
3464 enum BlockAcctType type
)
3466 assert(type
< BDRV_MAX_IOTYPE
);
3468 cookie
->bytes
= bytes
;
3469 cookie
->start_time_ns
= get_clock();
3470 cookie
->type
= type
;
3474 bdrv_acct_done(BlockDriverState
*bs
, BlockAcctCookie
*cookie
)
3476 assert(cookie
->type
< BDRV_MAX_IOTYPE
);
3478 bs
->nr_bytes
[cookie
->type
] += cookie
->bytes
;
3479 bs
->nr_ops
[cookie
->type
]++;
3480 bs
->total_time_ns
[cookie
->type
] += get_clock() - cookie
->start_time_ns
;
3483 int bdrv_img_create(const char *filename
, const char *fmt
,
3484 const char *base_filename
, const char *base_fmt
,
3485 char *options
, uint64_t img_size
, int flags
)
3487 QEMUOptionParameter
*param
= NULL
, *create_options
= NULL
;
3488 QEMUOptionParameter
*backing_fmt
, *backing_file
, *size
;
3489 BlockDriverState
*bs
= NULL
;
3490 BlockDriver
*drv
, *proto_drv
;
3491 BlockDriver
*backing_drv
= NULL
;
3494 /* Find driver and parse its options */
3495 drv
= bdrv_find_format(fmt
);
3497 error_report("Unknown file format '%s'", fmt
);
3502 proto_drv
= bdrv_find_protocol(filename
);
3504 error_report("Unknown protocol '%s'", filename
);
3509 create_options
= append_option_parameters(create_options
,
3510 drv
->create_options
);
3511 create_options
= append_option_parameters(create_options
,
3512 proto_drv
->create_options
);
3514 /* Create parameter list with default values */
3515 param
= parse_option_parameters("", create_options
, param
);
3517 set_option_parameter_int(param
, BLOCK_OPT_SIZE
, img_size
);
3519 /* Parse -o options */
3521 param
= parse_option_parameters(options
, create_options
, param
);
3522 if (param
== NULL
) {
3523 error_report("Invalid options for file format '%s'.", fmt
);
3529 if (base_filename
) {
3530 if (set_option_parameter(param
, BLOCK_OPT_BACKING_FILE
,
3532 error_report("Backing file not supported for file format '%s'",
3540 if (set_option_parameter(param
, BLOCK_OPT_BACKING_FMT
, base_fmt
)) {
3541 error_report("Backing file format not supported for file "
3542 "format '%s'", fmt
);
3548 backing_file
= get_option_parameter(param
, BLOCK_OPT_BACKING_FILE
);
3549 if (backing_file
&& backing_file
->value
.s
) {
3550 if (!strcmp(filename
, backing_file
->value
.s
)) {
3551 error_report("Error: Trying to create an image with the "
3552 "same filename as the backing file");
3558 backing_fmt
= get_option_parameter(param
, BLOCK_OPT_BACKING_FMT
);
3559 if (backing_fmt
&& backing_fmt
->value
.s
) {
3560 backing_drv
= bdrv_find_format(backing_fmt
->value
.s
);
3562 error_report("Unknown backing file format '%s'",
3563 backing_fmt
->value
.s
);
3569 // The size for the image must always be specified, with one exception:
3570 // If we are using a backing file, we can obtain the size from there
3571 size
= get_option_parameter(param
, BLOCK_OPT_SIZE
);
3572 if (size
&& size
->value
.n
== -1) {
3573 if (backing_file
&& backing_file
->value
.s
) {
3579 ret
= bdrv_open(bs
, backing_file
->value
.s
, flags
, backing_drv
);
3581 error_report("Could not open '%s'", backing_file
->value
.s
);
3584 bdrv_get_geometry(bs
, &size
);
3587 snprintf(buf
, sizeof(buf
), "%" PRId64
, size
);
3588 set_option_parameter(param
, BLOCK_OPT_SIZE
, buf
);
3590 error_report("Image creation needs a size parameter");
3596 printf("Formatting '%s', fmt=%s ", filename
, fmt
);
3597 print_option_parameters(param
);
3600 ret
= bdrv_create(drv
, filename
, param
);
3603 if (ret
== -ENOTSUP
) {
3604 error_report("Formatting or formatting option not supported for "
3605 "file format '%s'", fmt
);
3606 } else if (ret
== -EFBIG
) {
3607 error_report("The image size is too large for file format '%s'",
3610 error_report("%s: error while creating %s: %s", filename
, fmt
,
3616 free_option_parameters(create_options
);
3617 free_option_parameters(param
);