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"
33 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/queue.h>
46 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
47 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
48 BlockDriverCompletionFunc
*cb
, void *opaque
);
49 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
50 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
51 BlockDriverCompletionFunc
*cb
, void *opaque
);
52 static BlockDriverAIOCB
*bdrv_aio_flush_em(BlockDriverState
*bs
,
53 BlockDriverCompletionFunc
*cb
, void *opaque
);
54 static BlockDriverAIOCB
*bdrv_aio_noop_em(BlockDriverState
*bs
,
55 BlockDriverCompletionFunc
*cb
, void *opaque
);
56 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
57 uint8_t *buf
, int nb_sectors
);
58 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
59 const uint8_t *buf
, int nb_sectors
);
61 static QTAILQ_HEAD(, BlockDriverState
) bdrv_states
=
62 QTAILQ_HEAD_INITIALIZER(bdrv_states
);
64 static QLIST_HEAD(, BlockDriver
) bdrv_drivers
=
65 QLIST_HEAD_INITIALIZER(bdrv_drivers
);
67 /* The device to use for VM snapshots */
68 static BlockDriverState
*bs_snapshots
;
70 /* If non-zero, use only whitelisted block drivers */
71 static int use_bdrv_whitelist
;
73 int path_is_absolute(const char *path
)
77 /* specific case for names like: "\\.\d:" */
78 if (*path
== '/' || *path
== '\\')
81 p
= strchr(path
, ':');
87 return (*p
== '/' || *p
== '\\');
93 /* if filename is absolute, just copy it to dest. Otherwise, build a
94 path to it by considering it is relative to base_path. URL are
96 void path_combine(char *dest
, int dest_size
,
97 const char *base_path
,
105 if (path_is_absolute(filename
)) {
106 pstrcpy(dest
, dest_size
, filename
);
108 p
= strchr(base_path
, ':');
113 p1
= strrchr(base_path
, '/');
117 p2
= strrchr(base_path
, '\\');
129 if (len
> dest_size
- 1)
131 memcpy(dest
, base_path
, len
);
133 pstrcat(dest
, dest_size
, filename
);
137 void bdrv_register(BlockDriver
*bdrv
)
139 if (!bdrv
->bdrv_aio_readv
) {
140 /* add AIO emulation layer */
141 bdrv
->bdrv_aio_readv
= bdrv_aio_readv_em
;
142 bdrv
->bdrv_aio_writev
= bdrv_aio_writev_em
;
143 } else if (!bdrv
->bdrv_read
) {
144 /* add synchronous IO emulation layer */
145 bdrv
->bdrv_read
= bdrv_read_em
;
146 bdrv
->bdrv_write
= bdrv_write_em
;
149 if (!bdrv
->bdrv_aio_flush
)
150 bdrv
->bdrv_aio_flush
= bdrv_aio_flush_em
;
152 QLIST_INSERT_HEAD(&bdrv_drivers
, bdrv
, list
);
155 /* create a new block device (by default it is empty) */
156 BlockDriverState
*bdrv_new(const char *device_name
)
158 BlockDriverState
*bs
;
160 bs
= qemu_mallocz(sizeof(BlockDriverState
));
161 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
162 if (device_name
[0] != '\0') {
163 QTAILQ_INSERT_TAIL(&bdrv_states
, bs
, list
);
168 BlockDriver
*bdrv_find_format(const char *format_name
)
171 QLIST_FOREACH(drv1
, &bdrv_drivers
, list
) {
172 if (!strcmp(drv1
->format_name
, format_name
)) {
179 static int bdrv_is_whitelisted(BlockDriver
*drv
)
181 static const char *whitelist
[] = {
182 CONFIG_BDRV_WHITELIST
187 return 1; /* no whitelist, anything goes */
189 for (p
= whitelist
; *p
; p
++) {
190 if (!strcmp(drv
->format_name
, *p
)) {
197 BlockDriver
*bdrv_find_whitelisted_format(const char *format_name
)
199 BlockDriver
*drv
= bdrv_find_format(format_name
);
200 return drv
&& bdrv_is_whitelisted(drv
) ? drv
: NULL
;
203 int bdrv_create(BlockDriver
*drv
, const char* filename
,
204 QEMUOptionParameter
*options
)
206 if (!drv
->bdrv_create
)
209 return drv
->bdrv_create(filename
, options
);
212 int bdrv_create_file(const char* filename
, QEMUOptionParameter
*options
)
216 drv
= bdrv_find_protocol(filename
);
221 return bdrv_create(drv
, filename
, options
);
225 void get_tmp_filename(char *filename
, int size
)
227 char temp_dir
[MAX_PATH
];
229 GetTempPath(MAX_PATH
, temp_dir
);
230 GetTempFileName(temp_dir
, "qem", 0, filename
);
233 void get_tmp_filename(char *filename
, int size
)
237 /* XXX: race condition possible */
238 tmpdir
= getenv("TMPDIR");
241 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
242 fd
= mkstemp(filename
);
248 static int is_windows_drive_prefix(const char *filename
)
250 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
251 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
255 int is_windows_drive(const char *filename
)
257 if (is_windows_drive_prefix(filename
) &&
260 if (strstart(filename
, "\\\\.\\", NULL
) ||
261 strstart(filename
, "//./", NULL
))
268 * Detect host devices. By convention, /dev/cdrom[N] is always
269 * recognized as a host CDROM.
271 static BlockDriver
*find_hdev_driver(const char *filename
)
273 int score_max
= 0, score
;
274 BlockDriver
*drv
= NULL
, *d
;
276 QLIST_FOREACH(d
, &bdrv_drivers
, list
) {
277 if (d
->bdrv_probe_device
) {
278 score
= d
->bdrv_probe_device(filename
);
279 if (score
> score_max
) {
289 BlockDriver
*bdrv_find_protocol(const char *filename
)
296 /* TODO Drivers without bdrv_file_open must be specified explicitly */
299 * XXX(hch): we really should not let host device detection
300 * override an explicit protocol specification, but moving this
301 * later breaks access to device names with colons in them.
302 * Thanks to the brain-dead persistent naming schemes on udev-
303 * based Linux systems those actually are quite common.
305 drv1
= find_hdev_driver(filename
);
311 if (is_windows_drive(filename
) ||
312 is_windows_drive_prefix(filename
))
313 return bdrv_find_format("file");
316 p
= strchr(filename
, ':');
318 return bdrv_find_format("file");
321 if (len
> sizeof(protocol
) - 1)
322 len
= sizeof(protocol
) - 1;
323 memcpy(protocol
, filename
, len
);
324 protocol
[len
] = '\0';
325 QLIST_FOREACH(drv1
, &bdrv_drivers
, list
) {
326 if (drv1
->protocol_name
&&
327 !strcmp(drv1
->protocol_name
, protocol
)) {
334 static int find_image_format(const char *filename
, BlockDriver
**pdrv
)
336 int ret
, score
, score_max
;
337 BlockDriver
*drv1
, *drv
;
339 BlockDriverState
*bs
;
341 ret
= bdrv_file_open(&bs
, filename
, 0);
347 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
348 if (bs
->sg
|| !bdrv_is_inserted(bs
)) {
350 drv
= bdrv_find_format("raw");
358 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
367 QLIST_FOREACH(drv1
, &bdrv_drivers
, list
) {
368 if (drv1
->bdrv_probe
) {
369 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
370 if (score
> score_max
) {
384 * Set the current 'total_sectors' value
386 static int refresh_total_sectors(BlockDriverState
*bs
, int64_t hint
)
388 BlockDriver
*drv
= bs
->drv
;
390 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
394 /* query actual device if possible, otherwise just trust the hint */
395 if (drv
->bdrv_getlength
) {
396 int64_t length
= drv
->bdrv_getlength(bs
);
400 hint
= length
>> BDRV_SECTOR_BITS
;
403 bs
->total_sectors
= hint
;
408 * Common part for opening disk images and files
410 static int bdrv_open_common(BlockDriverState
*bs
, const char *filename
,
411 int flags
, BlockDriver
*drv
)
418 bs
->total_sectors
= 0;
421 bs
->open_flags
= flags
;
422 /* buffer_alignment defaulted to 512, drivers can change this value */
423 bs
->buffer_alignment
= 512;
425 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
427 if (use_bdrv_whitelist
&& !bdrv_is_whitelisted(drv
)) {
432 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
435 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
436 * write cache to the guest. We do need the fdatasync to flush
437 * out transactions for block allocations, and we maybe have a
438 * volatile write cache in our backing device to deal with.
440 if (flags
& (BDRV_O_CACHE_WB
|BDRV_O_NOCACHE
))
441 bs
->enable_write_cache
= 1;
444 * Clear flags that are internal to the block layer before opening the
447 open_flags
= flags
& ~(BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
);
450 * Snapshots should be writeable.
452 if (bs
->is_temporary
) {
453 open_flags
|= BDRV_O_RDWR
;
456 /* Open the image, either directly or using a protocol */
457 if (drv
->bdrv_file_open
) {
458 ret
= drv
->bdrv_file_open(bs
, filename
, open_flags
);
460 ret
= bdrv_file_open(&bs
->file
, filename
, open_flags
);
462 ret
= drv
->bdrv_open(bs
, open_flags
);
470 bs
->keep_read_only
= bs
->read_only
= !(open_flags
& BDRV_O_RDWR
);
472 ret
= refresh_total_sectors(bs
, bs
->total_sectors
);
478 if (bs
->is_temporary
) {
486 bdrv_delete(bs
->file
);
489 qemu_free(bs
->opaque
);
496 * Opens a file using a protocol (file, host_device, nbd, ...)
498 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
500 BlockDriverState
*bs
;
504 drv
= bdrv_find_protocol(filename
);
510 ret
= bdrv_open_common(bs
, filename
, flags
, drv
);
521 * Opens a disk image (raw, qcow2, vmdk, ...)
523 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
,
528 if (flags
& BDRV_O_SNAPSHOT
) {
529 BlockDriverState
*bs1
;
532 BlockDriver
*bdrv_qcow2
;
533 QEMUOptionParameter
*options
;
534 char tmp_filename
[PATH_MAX
];
535 char backing_filename
[PATH_MAX
];
537 /* if snapshot, we create a temporary backing file and open it
538 instead of opening 'filename' directly */
540 /* if there is a backing file, use it */
542 ret
= bdrv_open(bs1
, filename
, 0, drv
);
547 total_size
= bdrv_getlength(bs1
) & BDRV_SECTOR_MASK
;
549 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
554 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
556 /* Real path is meaningless for protocols */
558 snprintf(backing_filename
, sizeof(backing_filename
),
560 else if (!realpath(filename
, backing_filename
))
563 bdrv_qcow2
= bdrv_find_format("qcow2");
564 options
= parse_option_parameters("", bdrv_qcow2
->create_options
, NULL
);
566 set_option_parameter_int(options
, BLOCK_OPT_SIZE
, total_size
);
567 set_option_parameter(options
, BLOCK_OPT_BACKING_FILE
, backing_filename
);
569 set_option_parameter(options
, BLOCK_OPT_BACKING_FMT
,
573 ret
= bdrv_create(bdrv_qcow2
, tmp_filename
, options
);
574 free_option_parameters(options
);
579 filename
= tmp_filename
;
581 bs
->is_temporary
= 1;
584 /* Find the right image format driver */
586 ret
= find_image_format(filename
, &drv
);
590 goto unlink_and_fail
;
594 ret
= bdrv_open_common(bs
, filename
, flags
, drv
);
596 goto unlink_and_fail
;
599 /* If there is a backing file, use it */
600 if ((flags
& BDRV_O_NO_BACKING
) == 0 && bs
->backing_file
[0] != '\0') {
601 char backing_filename
[PATH_MAX
];
603 BlockDriver
*back_drv
= NULL
;
605 bs
->backing_hd
= bdrv_new("");
606 path_combine(backing_filename
, sizeof(backing_filename
),
607 filename
, bs
->backing_file
);
608 if (bs
->backing_format
[0] != '\0')
609 back_drv
= bdrv_find_format(bs
->backing_format
);
611 /* backing files always opened read-only */
613 flags
& ~(BDRV_O_RDWR
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
);
615 ret
= bdrv_open(bs
->backing_hd
, backing_filename
, back_flags
, back_drv
);
620 if (bs
->is_temporary
) {
621 bs
->backing_hd
->keep_read_only
= !(flags
& BDRV_O_RDWR
);
623 /* base image inherits from "parent" */
624 bs
->backing_hd
->keep_read_only
= bs
->keep_read_only
;
628 if (!bdrv_key_required(bs
)) {
629 /* call the change callback */
630 bs
->media_changed
= 1;
632 bs
->change_cb(bs
->change_opaque
);
638 if (bs
->is_temporary
) {
644 void bdrv_close(BlockDriverState
*bs
)
647 if (bs
== bs_snapshots
) {
650 if (bs
->backing_hd
) {
651 bdrv_delete(bs
->backing_hd
);
652 bs
->backing_hd
= NULL
;
654 bs
->drv
->bdrv_close(bs
);
655 qemu_free(bs
->opaque
);
657 if (bs
->is_temporary
) {
658 unlink(bs
->filename
);
664 if (bs
->file
!= NULL
) {
665 bdrv_close(bs
->file
);
668 /* call the change callback */
669 bs
->media_changed
= 1;
671 bs
->change_cb(bs
->change_opaque
);
675 void bdrv_close_all(void)
677 BlockDriverState
*bs
;
679 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
684 void bdrv_delete(BlockDriverState
*bs
)
688 /* remove from list, if necessary */
689 if (bs
->device_name
[0] != '\0') {
690 QTAILQ_REMOVE(&bdrv_states
, bs
, list
);
694 if (bs
->file
!= NULL
) {
695 bdrv_delete(bs
->file
);
698 assert(bs
!= bs_snapshots
);
702 int bdrv_attach(BlockDriverState
*bs
, DeviceState
*qdev
)
711 void bdrv_detach(BlockDriverState
*bs
, DeviceState
*qdev
)
713 assert(bs
->peer
== qdev
);
717 DeviceState
*bdrv_get_attached(BlockDriverState
*bs
)
723 * Run consistency checks on an image
725 * Returns 0 if the check could be completed (it doesn't mean that the image is
726 * free of errors) or -errno when an internal error occured. The results of the
727 * check are stored in res.
729 int bdrv_check(BlockDriverState
*bs
, BdrvCheckResult
*res
)
731 if (bs
->drv
->bdrv_check
== NULL
) {
735 memset(res
, 0, sizeof(*res
));
736 return bs
->drv
->bdrv_check(bs
, res
);
739 #define COMMIT_BUF_SECTORS 2048
741 /* commit COW file into the raw image */
742 int bdrv_commit(BlockDriverState
*bs
)
744 BlockDriver
*drv
= bs
->drv
;
745 BlockDriver
*backing_drv
;
746 int64_t sector
, total_sectors
;
747 int n
, ro
, open_flags
;
748 int ret
= 0, rw_ret
= 0;
751 BlockDriverState
*bs_rw
, *bs_ro
;
756 if (!bs
->backing_hd
) {
760 if (bs
->backing_hd
->keep_read_only
) {
764 backing_drv
= bs
->backing_hd
->drv
;
765 ro
= bs
->backing_hd
->read_only
;
766 strncpy(filename
, bs
->backing_hd
->filename
, sizeof(filename
));
767 open_flags
= bs
->backing_hd
->open_flags
;
771 bdrv_delete(bs
->backing_hd
);
772 bs
->backing_hd
= NULL
;
773 bs_rw
= bdrv_new("");
774 rw_ret
= bdrv_open(bs_rw
, filename
, open_flags
| BDRV_O_RDWR
,
778 /* try to re-open read-only */
779 bs_ro
= bdrv_new("");
780 ret
= bdrv_open(bs_ro
, filename
, open_flags
& ~BDRV_O_RDWR
,
784 /* drive not functional anymore */
788 bs
->backing_hd
= bs_ro
;
791 bs
->backing_hd
= bs_rw
;
794 total_sectors
= bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
;
795 buf
= qemu_malloc(COMMIT_BUF_SECTORS
* BDRV_SECTOR_SIZE
);
797 for (sector
= 0; sector
< total_sectors
; sector
+= n
) {
798 if (drv
->bdrv_is_allocated(bs
, sector
, COMMIT_BUF_SECTORS
, &n
)) {
800 if (bdrv_read(bs
, sector
, buf
, n
) != 0) {
805 if (bdrv_write(bs
->backing_hd
, sector
, buf
, n
) != 0) {
812 if (drv
->bdrv_make_empty
) {
813 ret
= drv
->bdrv_make_empty(bs
);
818 * Make sure all data we wrote to the backing device is actually
822 bdrv_flush(bs
->backing_hd
);
829 bdrv_delete(bs
->backing_hd
);
830 bs
->backing_hd
= NULL
;
831 bs_ro
= bdrv_new("");
832 ret
= bdrv_open(bs_ro
, filename
, open_flags
& ~BDRV_O_RDWR
,
836 /* drive not functional anymore */
840 bs
->backing_hd
= bs_ro
;
841 bs
->backing_hd
->keep_read_only
= 0;
847 void bdrv_commit_all(void)
849 BlockDriverState
*bs
;
851 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
859 * -EINVAL - backing format specified, but no file
860 * -ENOSPC - can't update the backing file because no space is left in the
862 * -ENOTSUP - format driver doesn't support changing the backing file
864 int bdrv_change_backing_file(BlockDriverState
*bs
,
865 const char *backing_file
, const char *backing_fmt
)
867 BlockDriver
*drv
= bs
->drv
;
869 if (drv
->bdrv_change_backing_file
!= NULL
) {
870 return drv
->bdrv_change_backing_file(bs
, backing_file
, backing_fmt
);
876 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
881 if (!bdrv_is_inserted(bs
))
887 len
= bdrv_getlength(bs
);
892 if ((offset
> len
) || (len
- offset
< size
))
898 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
901 return bdrv_check_byte_request(bs
, sector_num
* BDRV_SECTOR_SIZE
,
902 nb_sectors
* BDRV_SECTOR_SIZE
);
905 /* return < 0 if error. See bdrv_write() for the return codes */
906 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
907 uint8_t *buf
, int nb_sectors
)
909 BlockDriver
*drv
= bs
->drv
;
913 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
916 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
919 static void set_dirty_bitmap(BlockDriverState
*bs
, int64_t sector_num
,
920 int nb_sectors
, int dirty
)
923 unsigned long val
, idx
, bit
;
925 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
926 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
928 for (; start
<= end
; start
++) {
929 idx
= start
/ (sizeof(unsigned long) * 8);
930 bit
= start
% (sizeof(unsigned long) * 8);
931 val
= bs
->dirty_bitmap
[idx
];
933 if (!(val
& (1UL << bit
))) {
938 if (val
& (1UL << bit
)) {
940 val
&= ~(1UL << bit
);
943 bs
->dirty_bitmap
[idx
] = val
;
947 /* Return < 0 if error. Important errors are:
948 -EIO generic I/O error (may happen for all errors)
949 -ENOMEDIUM No media inserted.
950 -EINVAL Invalid sector number or nb_sectors
951 -EACCES Trying to write a read-only device
953 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
954 const uint8_t *buf
, int nb_sectors
)
956 BlockDriver
*drv
= bs
->drv
;
961 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
964 if (bs
->dirty_bitmap
) {
965 set_dirty_bitmap(bs
, sector_num
, nb_sectors
, 1);
968 if (bs
->wr_highest_sector
< sector_num
+ nb_sectors
- 1) {
969 bs
->wr_highest_sector
= sector_num
+ nb_sectors
- 1;
972 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
975 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
976 void *buf
, int count1
)
978 uint8_t tmp_buf
[BDRV_SECTOR_SIZE
];
979 int len
, nb_sectors
, count
;
984 /* first read to align to sector start */
985 len
= (BDRV_SECTOR_SIZE
- offset
) & (BDRV_SECTOR_SIZE
- 1);
988 sector_num
= offset
>> BDRV_SECTOR_BITS
;
990 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
992 memcpy(buf
, tmp_buf
+ (offset
& (BDRV_SECTOR_SIZE
- 1)), len
);
1000 /* read the sectors "in place" */
1001 nb_sectors
= count
>> BDRV_SECTOR_BITS
;
1002 if (nb_sectors
> 0) {
1003 if ((ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
)) < 0)
1005 sector_num
+= nb_sectors
;
1006 len
= nb_sectors
<< BDRV_SECTOR_BITS
;
1011 /* add data from the last sector */
1013 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1015 memcpy(buf
, tmp_buf
, count
);
1020 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
1021 const void *buf
, int count1
)
1023 uint8_t tmp_buf
[BDRV_SECTOR_SIZE
];
1024 int len
, nb_sectors
, count
;
1029 /* first write to align to sector start */
1030 len
= (BDRV_SECTOR_SIZE
- offset
) & (BDRV_SECTOR_SIZE
- 1);
1033 sector_num
= offset
>> BDRV_SECTOR_BITS
;
1035 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1037 memcpy(tmp_buf
+ (offset
& (BDRV_SECTOR_SIZE
- 1)), buf
, len
);
1038 if ((ret
= bdrv_write(bs
, sector_num
, tmp_buf
, 1)) < 0)
1047 /* write the sectors "in place" */
1048 nb_sectors
= count
>> BDRV_SECTOR_BITS
;
1049 if (nb_sectors
> 0) {
1050 if ((ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
)) < 0)
1052 sector_num
+= nb_sectors
;
1053 len
= nb_sectors
<< BDRV_SECTOR_BITS
;
1058 /* add data from the last sector */
1060 if ((ret
= bdrv_read(bs
, sector_num
, tmp_buf
, 1)) < 0)
1062 memcpy(tmp_buf
, buf
, count
);
1063 if ((ret
= bdrv_write(bs
, sector_num
, tmp_buf
, 1)) < 0)
1070 * Writes to the file and ensures that no writes are reordered across this
1071 * request (acts as a barrier)
1073 * Returns 0 on success, -errno in error cases.
1075 int bdrv_pwrite_sync(BlockDriverState
*bs
, int64_t offset
,
1076 const void *buf
, int count
)
1080 ret
= bdrv_pwrite(bs
, offset
, buf
, count
);
1085 /* No flush needed for cache=writethrough, it uses O_DSYNC */
1086 if ((bs
->open_flags
& BDRV_O_CACHE_MASK
) != 0) {
1094 * Writes to the file and ensures that no writes are reordered across this
1095 * request (acts as a barrier)
1097 * Returns 0 on success, -errno in error cases.
1099 int bdrv_write_sync(BlockDriverState
*bs
, int64_t sector_num
,
1100 const uint8_t *buf
, int nb_sectors
)
1102 return bdrv_pwrite_sync(bs
, BDRV_SECTOR_SIZE
* sector_num
,
1103 buf
, BDRV_SECTOR_SIZE
* nb_sectors
);
1107 * Truncate file to 'offset' bytes (needed only for file protocols)
1109 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
1111 BlockDriver
*drv
= bs
->drv
;
1115 if (!drv
->bdrv_truncate
)
1119 ret
= drv
->bdrv_truncate(bs
, offset
);
1121 ret
= refresh_total_sectors(bs
, offset
>> BDRV_SECTOR_BITS
);
1127 * Length of a file in bytes. Return < 0 if error or unknown.
1129 int64_t bdrv_getlength(BlockDriverState
*bs
)
1131 BlockDriver
*drv
= bs
->drv
;
1135 /* Fixed size devices use the total_sectors value for speed instead of
1136 issuing a length query (like lseek) on each call. Also, legacy block
1137 drivers don't provide a bdrv_getlength function and must use
1139 if (!bs
->growable
|| !drv
->bdrv_getlength
) {
1140 return bs
->total_sectors
* BDRV_SECTOR_SIZE
;
1142 return drv
->bdrv_getlength(bs
);
1145 /* return 0 as number of sectors if no device present or error */
1146 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
1149 length
= bdrv_getlength(bs
);
1153 length
= length
>> BDRV_SECTOR_BITS
;
1154 *nb_sectors_ptr
= length
;
1158 uint8_t boot_ind
; /* 0x80 - active */
1159 uint8_t head
; /* starting head */
1160 uint8_t sector
; /* starting sector */
1161 uint8_t cyl
; /* starting cylinder */
1162 uint8_t sys_ind
; /* What partition type */
1163 uint8_t end_head
; /* end head */
1164 uint8_t end_sector
; /* end sector */
1165 uint8_t end_cyl
; /* end cylinder */
1166 uint32_t start_sect
; /* starting sector counting from 0 */
1167 uint32_t nr_sects
; /* nr of sectors in partition */
1168 } __attribute__((packed
));
1170 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1171 static int guess_disk_lchs(BlockDriverState
*bs
,
1172 int *pcylinders
, int *pheads
, int *psectors
)
1174 uint8_t buf
[BDRV_SECTOR_SIZE
];
1175 int ret
, i
, heads
, sectors
, cylinders
;
1176 struct partition
*p
;
1178 uint64_t nb_sectors
;
1180 bdrv_get_geometry(bs
, &nb_sectors
);
1182 ret
= bdrv_read(bs
, 0, buf
, 1);
1185 /* test msdos magic */
1186 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
1188 for(i
= 0; i
< 4; i
++) {
1189 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
1190 nr_sects
= le32_to_cpu(p
->nr_sects
);
1191 if (nr_sects
&& p
->end_head
) {
1192 /* We make the assumption that the partition terminates on
1193 a cylinder boundary */
1194 heads
= p
->end_head
+ 1;
1195 sectors
= p
->end_sector
& 63;
1198 cylinders
= nb_sectors
/ (heads
* sectors
);
1199 if (cylinders
< 1 || cylinders
> 16383)
1202 *psectors
= sectors
;
1203 *pcylinders
= cylinders
;
1205 printf("guessed geometry: LCHS=%d %d %d\n",
1206 cylinders
, heads
, sectors
);
1214 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
1216 int translation
, lba_detected
= 0;
1217 int cylinders
, heads
, secs
;
1218 uint64_t nb_sectors
;
1220 /* if a geometry hint is available, use it */
1221 bdrv_get_geometry(bs
, &nb_sectors
);
1222 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
1223 translation
= bdrv_get_translation_hint(bs
);
1224 if (cylinders
!= 0) {
1229 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
1231 /* if heads > 16, it means that a BIOS LBA
1232 translation was active, so the default
1233 hardware geometry is OK */
1235 goto default_geometry
;
1240 /* disable any translation to be in sync with
1241 the logical geometry */
1242 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
1243 bdrv_set_translation_hint(bs
,
1244 BIOS_ATA_TRANSLATION_NONE
);
1249 /* if no geometry, use a standard physical disk geometry */
1250 cylinders
= nb_sectors
/ (16 * 63);
1252 if (cylinders
> 16383)
1254 else if (cylinders
< 2)
1259 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
1260 if ((*pcyls
* *pheads
) <= 131072) {
1261 bdrv_set_translation_hint(bs
,
1262 BIOS_ATA_TRANSLATION_LARGE
);
1264 bdrv_set_translation_hint(bs
,
1265 BIOS_ATA_TRANSLATION_LBA
);
1269 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
1273 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
1274 int cyls
, int heads
, int secs
)
1281 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
1284 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
1285 type
== BDRV_TYPE_FLOPPY
));
1288 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
1290 bs
->translation
= translation
;
1293 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
1294 int *pcyls
, int *pheads
, int *psecs
)
1297 *pheads
= bs
->heads
;
1301 int bdrv_get_type_hint(BlockDriverState
*bs
)
1306 int bdrv_get_translation_hint(BlockDriverState
*bs
)
1308 return bs
->translation
;
1311 void bdrv_set_on_error(BlockDriverState
*bs
, BlockErrorAction on_read_error
,
1312 BlockErrorAction on_write_error
)
1314 bs
->on_read_error
= on_read_error
;
1315 bs
->on_write_error
= on_write_error
;
1318 BlockErrorAction
bdrv_get_on_error(BlockDriverState
*bs
, int is_read
)
1320 return is_read
? bs
->on_read_error
: bs
->on_write_error
;
1323 void bdrv_set_removable(BlockDriverState
*bs
, int removable
)
1325 bs
->removable
= removable
;
1326 if (removable
&& bs
== bs_snapshots
) {
1327 bs_snapshots
= NULL
;
1331 int bdrv_is_removable(BlockDriverState
*bs
)
1333 return bs
->removable
;
1336 int bdrv_is_read_only(BlockDriverState
*bs
)
1338 return bs
->read_only
;
1341 int bdrv_is_sg(BlockDriverState
*bs
)
1346 int bdrv_enable_write_cache(BlockDriverState
*bs
)
1348 return bs
->enable_write_cache
;
1351 /* XXX: no longer used */
1352 void bdrv_set_change_cb(BlockDriverState
*bs
,
1353 void (*change_cb
)(void *opaque
), void *opaque
)
1355 bs
->change_cb
= change_cb
;
1356 bs
->change_opaque
= opaque
;
1359 int bdrv_is_encrypted(BlockDriverState
*bs
)
1361 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1363 return bs
->encrypted
;
1366 int bdrv_key_required(BlockDriverState
*bs
)
1368 BlockDriverState
*backing_hd
= bs
->backing_hd
;
1370 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
1372 return (bs
->encrypted
&& !bs
->valid_key
);
1375 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
1378 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
1379 ret
= bdrv_set_key(bs
->backing_hd
, key
);
1385 if (!bs
->encrypted
) {
1387 } else if (!bs
->drv
|| !bs
->drv
->bdrv_set_key
) {
1390 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
1393 } else if (!bs
->valid_key
) {
1395 /* call the change callback now, we skipped it on open */
1396 bs
->media_changed
= 1;
1398 bs
->change_cb(bs
->change_opaque
);
1403 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
1408 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
1412 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
1417 QLIST_FOREACH(drv
, &bdrv_drivers
, list
) {
1418 it(opaque
, drv
->format_name
);
1422 BlockDriverState
*bdrv_find(const char *name
)
1424 BlockDriverState
*bs
;
1426 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1427 if (!strcmp(name
, bs
->device_name
)) {
1434 BlockDriverState
*bdrv_next(BlockDriverState
*bs
)
1437 return QTAILQ_FIRST(&bdrv_states
);
1439 return QTAILQ_NEXT(bs
, list
);
1442 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1444 BlockDriverState
*bs
;
1446 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1451 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1453 return bs
->device_name
;
1456 int bdrv_flush(BlockDriverState
*bs
)
1458 if (bs
->open_flags
& BDRV_O_NO_FLUSH
) {
1462 if (bs
->drv
&& bs
->drv
->bdrv_flush
) {
1463 return bs
->drv
->bdrv_flush(bs
);
1467 * Some block drivers always operate in either writethrough or unsafe mode
1468 * and don't support bdrv_flush therefore. Usually qemu doesn't know how
1469 * the server works (because the behaviour is hardcoded or depends on
1470 * server-side configuration), so we can't ensure that everything is safe
1471 * on disk. Returning an error doesn't work because that would break guests
1472 * even if the server operates in writethrough mode.
1474 * Let's hope the user knows what he's doing.
1479 void bdrv_flush_all(void)
1481 BlockDriverState
*bs
;
1483 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1484 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1485 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
))) {
1491 int bdrv_has_zero_init(BlockDriverState
*bs
)
1495 if (bs
->drv
->bdrv_has_zero_init
) {
1496 return bs
->drv
->bdrv_has_zero_init(bs
);
1503 * Returns true iff the specified sector is present in the disk image. Drivers
1504 * not implementing the functionality are assumed to not support backing files,
1505 * hence all their sectors are reported as allocated.
1507 * 'pnum' is set to the number of sectors (including and immediately following
1508 * the specified sector) that are known to be in the same
1509 * allocated/unallocated state.
1511 * 'nb_sectors' is the max value 'pnum' should be set to.
1513 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1517 if (!bs
->drv
->bdrv_is_allocated
) {
1518 if (sector_num
>= bs
->total_sectors
) {
1522 n
= bs
->total_sectors
- sector_num
;
1523 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1526 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1529 void bdrv_mon_event(const BlockDriverState
*bdrv
,
1530 BlockMonEventAction action
, int is_read
)
1533 const char *action_str
;
1536 case BDRV_ACTION_REPORT
:
1537 action_str
= "report";
1539 case BDRV_ACTION_IGNORE
:
1540 action_str
= "ignore";
1542 case BDRV_ACTION_STOP
:
1543 action_str
= "stop";
1549 data
= qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1552 is_read
? "read" : "write");
1553 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR
, data
);
1555 qobject_decref(data
);
1558 static void bdrv_print_dict(QObject
*obj
, void *opaque
)
1561 Monitor
*mon
= opaque
;
1563 bs_dict
= qobject_to_qdict(obj
);
1565 monitor_printf(mon
, "%s: type=%s removable=%d",
1566 qdict_get_str(bs_dict
, "device"),
1567 qdict_get_str(bs_dict
, "type"),
1568 qdict_get_bool(bs_dict
, "removable"));
1570 if (qdict_get_bool(bs_dict
, "removable")) {
1571 monitor_printf(mon
, " locked=%d", qdict_get_bool(bs_dict
, "locked"));
1574 if (qdict_haskey(bs_dict
, "inserted")) {
1575 QDict
*qdict
= qobject_to_qdict(qdict_get(bs_dict
, "inserted"));
1577 monitor_printf(mon
, " file=");
1578 monitor_print_filename(mon
, qdict_get_str(qdict
, "file"));
1579 if (qdict_haskey(qdict
, "backing_file")) {
1580 monitor_printf(mon
, " backing_file=");
1581 monitor_print_filename(mon
, qdict_get_str(qdict
, "backing_file"));
1583 monitor_printf(mon
, " ro=%d drv=%s encrypted=%d",
1584 qdict_get_bool(qdict
, "ro"),
1585 qdict_get_str(qdict
, "drv"),
1586 qdict_get_bool(qdict
, "encrypted"));
1588 monitor_printf(mon
, " [not inserted]");
1591 monitor_printf(mon
, "\n");
1594 void bdrv_info_print(Monitor
*mon
, const QObject
*data
)
1596 qlist_iter(qobject_to_qlist(data
), bdrv_print_dict
, mon
);
1599 void bdrv_info(Monitor
*mon
, QObject
**ret_data
)
1602 BlockDriverState
*bs
;
1604 bs_list
= qlist_new();
1606 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1608 const char *type
= "unknown";
1614 case BDRV_TYPE_CDROM
:
1617 case BDRV_TYPE_FLOPPY
:
1622 bs_obj
= qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1623 "'removable': %i, 'locked': %i }",
1624 bs
->device_name
, type
, bs
->removable
,
1629 QDict
*bs_dict
= qobject_to_qdict(bs_obj
);
1631 obj
= qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1632 "'encrypted': %i }",
1633 bs
->filename
, bs
->read_only
,
1634 bs
->drv
->format_name
,
1635 bdrv_is_encrypted(bs
));
1636 if (bs
->backing_file
[0] != '\0') {
1637 QDict
*qdict
= qobject_to_qdict(obj
);
1638 qdict_put(qdict
, "backing_file",
1639 qstring_from_str(bs
->backing_file
));
1642 qdict_put_obj(bs_dict
, "inserted", obj
);
1644 qlist_append_obj(bs_list
, bs_obj
);
1647 *ret_data
= QOBJECT(bs_list
);
1650 static void bdrv_stats_iter(QObject
*data
, void *opaque
)
1653 Monitor
*mon
= opaque
;
1655 qdict
= qobject_to_qdict(data
);
1656 monitor_printf(mon
, "%s:", qdict_get_str(qdict
, "device"));
1658 qdict
= qobject_to_qdict(qdict_get(qdict
, "stats"));
1659 monitor_printf(mon
, " rd_bytes=%" PRId64
1660 " wr_bytes=%" PRId64
1661 " rd_operations=%" PRId64
1662 " wr_operations=%" PRId64
1664 qdict_get_int(qdict
, "rd_bytes"),
1665 qdict_get_int(qdict
, "wr_bytes"),
1666 qdict_get_int(qdict
, "rd_operations"),
1667 qdict_get_int(qdict
, "wr_operations"));
1670 void bdrv_stats_print(Monitor
*mon
, const QObject
*data
)
1672 qlist_iter(qobject_to_qlist(data
), bdrv_stats_iter
, mon
);
1675 static QObject
* bdrv_info_stats_bs(BlockDriverState
*bs
)
1680 res
= qobject_from_jsonf("{ 'stats': {"
1681 "'rd_bytes': %" PRId64
","
1682 "'wr_bytes': %" PRId64
","
1683 "'rd_operations': %" PRId64
","
1684 "'wr_operations': %" PRId64
","
1685 "'wr_highest_offset': %" PRId64
1687 bs
->rd_bytes
, bs
->wr_bytes
,
1688 bs
->rd_ops
, bs
->wr_ops
,
1689 bs
->wr_highest_sector
*
1690 (uint64_t)BDRV_SECTOR_SIZE
);
1691 dict
= qobject_to_qdict(res
);
1693 if (*bs
->device_name
) {
1694 qdict_put(dict
, "device", qstring_from_str(bs
->device_name
));
1698 QObject
*parent
= bdrv_info_stats_bs(bs
->file
);
1699 qdict_put_obj(dict
, "parent", parent
);
1705 void bdrv_info_stats(Monitor
*mon
, QObject
**ret_data
)
1709 BlockDriverState
*bs
;
1711 devices
= qlist_new();
1713 QTAILQ_FOREACH(bs
, &bdrv_states
, list
) {
1714 obj
= bdrv_info_stats_bs(bs
);
1715 qlist_append_obj(devices
, obj
);
1718 *ret_data
= QOBJECT(devices
);
1721 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1723 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1724 return bs
->backing_file
;
1725 else if (bs
->encrypted
)
1726 return bs
->filename
;
1731 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1732 char *filename
, int filename_size
)
1734 if (!bs
->backing_file
) {
1735 pstrcpy(filename
, filename_size
, "");
1737 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1741 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1742 const uint8_t *buf
, int nb_sectors
)
1744 BlockDriver
*drv
= bs
->drv
;
1747 if (!drv
->bdrv_write_compressed
)
1749 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1752 if (bs
->dirty_bitmap
) {
1753 set_dirty_bitmap(bs
, sector_num
, nb_sectors
, 1);
1756 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1759 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1761 BlockDriver
*drv
= bs
->drv
;
1764 if (!drv
->bdrv_get_info
)
1766 memset(bdi
, 0, sizeof(*bdi
));
1767 return drv
->bdrv_get_info(bs
, bdi
);
1770 int bdrv_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
1771 int64_t pos
, int size
)
1773 BlockDriver
*drv
= bs
->drv
;
1776 if (drv
->bdrv_save_vmstate
)
1777 return drv
->bdrv_save_vmstate(bs
, buf
, pos
, size
);
1779 return bdrv_save_vmstate(bs
->file
, buf
, pos
, size
);
1783 int bdrv_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
1784 int64_t pos
, int size
)
1786 BlockDriver
*drv
= bs
->drv
;
1789 if (drv
->bdrv_load_vmstate
)
1790 return drv
->bdrv_load_vmstate(bs
, buf
, pos
, size
);
1792 return bdrv_load_vmstate(bs
->file
, buf
, pos
, size
);
1796 void bdrv_debug_event(BlockDriverState
*bs
, BlkDebugEvent event
)
1798 BlockDriver
*drv
= bs
->drv
;
1800 if (!drv
|| !drv
->bdrv_debug_event
) {
1804 return drv
->bdrv_debug_event(bs
, event
);
1808 /**************************************************************/
1809 /* handling of snapshots */
1811 int bdrv_can_snapshot(BlockDriverState
*bs
)
1813 BlockDriver
*drv
= bs
->drv
;
1814 if (!drv
|| bdrv_is_removable(bs
) || bdrv_is_read_only(bs
)) {
1818 if (!drv
->bdrv_snapshot_create
) {
1819 if (bs
->file
!= NULL
) {
1820 return bdrv_can_snapshot(bs
->file
);
1828 int bdrv_is_snapshot(BlockDriverState
*bs
)
1830 return !!(bs
->open_flags
& BDRV_O_SNAPSHOT
);
1833 BlockDriverState
*bdrv_snapshots(void)
1835 BlockDriverState
*bs
;
1838 return bs_snapshots
;
1842 while ((bs
= bdrv_next(bs
))) {
1843 if (bdrv_can_snapshot(bs
)) {
1851 int bdrv_snapshot_create(BlockDriverState
*bs
,
1852 QEMUSnapshotInfo
*sn_info
)
1854 BlockDriver
*drv
= bs
->drv
;
1857 if (drv
->bdrv_snapshot_create
)
1858 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1860 return bdrv_snapshot_create(bs
->file
, sn_info
);
1864 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1865 const char *snapshot_id
)
1867 BlockDriver
*drv
= bs
->drv
;
1872 if (drv
->bdrv_snapshot_goto
)
1873 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1876 drv
->bdrv_close(bs
);
1877 ret
= bdrv_snapshot_goto(bs
->file
, snapshot_id
);
1878 open_ret
= drv
->bdrv_open(bs
, bs
->open_flags
);
1880 bdrv_delete(bs
->file
);
1890 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1892 BlockDriver
*drv
= bs
->drv
;
1895 if (drv
->bdrv_snapshot_delete
)
1896 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1898 return bdrv_snapshot_delete(bs
->file
, snapshot_id
);
1902 int bdrv_snapshot_list(BlockDriverState
*bs
,
1903 QEMUSnapshotInfo
**psn_info
)
1905 BlockDriver
*drv
= bs
->drv
;
1908 if (drv
->bdrv_snapshot_list
)
1909 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1911 return bdrv_snapshot_list(bs
->file
, psn_info
);
1915 int bdrv_snapshot_load_tmp(BlockDriverState
*bs
,
1916 const char *snapshot_name
)
1918 BlockDriver
*drv
= bs
->drv
;
1922 if (!bs
->read_only
) {
1925 if (drv
->bdrv_snapshot_load_tmp
) {
1926 return drv
->bdrv_snapshot_load_tmp(bs
, snapshot_name
);
1931 #define NB_SUFFIXES 4
1933 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1935 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1940 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1943 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1944 if (size
< (10 * base
)) {
1945 snprintf(buf
, buf_size
, "%0.1f%c",
1946 (double)size
/ base
,
1949 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1950 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1951 ((size
+ (base
>> 1)) / base
),
1961 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1963 char buf1
[128], date_buf
[128], clock_buf
[128];
1973 snprintf(buf
, buf_size
,
1974 "%-10s%-20s%7s%20s%15s",
1975 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1979 ptm
= localtime(&ti
);
1980 strftime(date_buf
, sizeof(date_buf
),
1981 "%Y-%m-%d %H:%M:%S", ptm
);
1983 localtime_r(&ti
, &tm
);
1984 strftime(date_buf
, sizeof(date_buf
),
1985 "%Y-%m-%d %H:%M:%S", &tm
);
1987 secs
= sn
->vm_clock_nsec
/ 1000000000;
1988 snprintf(clock_buf
, sizeof(clock_buf
),
1989 "%02d:%02d:%02d.%03d",
1991 (int)((secs
/ 60) % 60),
1993 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1994 snprintf(buf
, buf_size
,
1995 "%-10s%-20s%7s%20s%15s",
1996 sn
->id_str
, sn
->name
,
1997 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
2005 /**************************************************************/
2008 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
2009 QEMUIOVector
*qiov
, int nb_sectors
,
2010 BlockDriverCompletionFunc
*cb
, void *opaque
)
2012 BlockDriver
*drv
= bs
->drv
;
2013 BlockDriverAIOCB
*ret
;
2015 trace_bdrv_aio_readv(bs
, sector_num
, nb_sectors
, opaque
);
2019 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
2022 ret
= drv
->bdrv_aio_readv(bs
, sector_num
, qiov
, nb_sectors
,
2026 /* Update stats even though technically transfer has not happened. */
2027 bs
->rd_bytes
+= (unsigned) nb_sectors
* BDRV_SECTOR_SIZE
;
2034 typedef struct BlockCompleteData
{
2035 BlockDriverCompletionFunc
*cb
;
2037 BlockDriverState
*bs
;
2040 } BlockCompleteData
;
2042 static void block_complete_cb(void *opaque
, int ret
)
2044 BlockCompleteData
*b
= opaque
;
2046 if (b
->bs
->dirty_bitmap
) {
2047 set_dirty_bitmap(b
->bs
, b
->sector_num
, b
->nb_sectors
, 1);
2049 b
->cb(b
->opaque
, ret
);
2053 static BlockCompleteData
*blk_dirty_cb_alloc(BlockDriverState
*bs
,
2056 BlockDriverCompletionFunc
*cb
,
2059 BlockCompleteData
*blkdata
= qemu_mallocz(sizeof(BlockCompleteData
));
2063 blkdata
->opaque
= opaque
;
2064 blkdata
->sector_num
= sector_num
;
2065 blkdata
->nb_sectors
= nb_sectors
;
2070 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
2071 QEMUIOVector
*qiov
, int nb_sectors
,
2072 BlockDriverCompletionFunc
*cb
, void *opaque
)
2074 BlockDriver
*drv
= bs
->drv
;
2075 BlockDriverAIOCB
*ret
;
2076 BlockCompleteData
*blk_cb_data
;
2078 trace_bdrv_aio_writev(bs
, sector_num
, nb_sectors
, opaque
);
2084 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
2087 if (bs
->dirty_bitmap
) {
2088 blk_cb_data
= blk_dirty_cb_alloc(bs
, sector_num
, nb_sectors
, cb
,
2090 cb
= &block_complete_cb
;
2091 opaque
= blk_cb_data
;
2094 ret
= drv
->bdrv_aio_writev(bs
, sector_num
, qiov
, nb_sectors
,
2098 /* Update stats even though technically transfer has not happened. */
2099 bs
->wr_bytes
+= (unsigned) nb_sectors
* BDRV_SECTOR_SIZE
;
2101 if (bs
->wr_highest_sector
< sector_num
+ nb_sectors
- 1) {
2102 bs
->wr_highest_sector
= sector_num
+ nb_sectors
- 1;
2110 typedef struct MultiwriteCB
{
2115 BlockDriverCompletionFunc
*cb
;
2117 QEMUIOVector
*free_qiov
;
2122 static void multiwrite_user_cb(MultiwriteCB
*mcb
)
2126 for (i
= 0; i
< mcb
->num_callbacks
; i
++) {
2127 mcb
->callbacks
[i
].cb(mcb
->callbacks
[i
].opaque
, mcb
->error
);
2128 if (mcb
->callbacks
[i
].free_qiov
) {
2129 qemu_iovec_destroy(mcb
->callbacks
[i
].free_qiov
);
2131 qemu_free(mcb
->callbacks
[i
].free_qiov
);
2132 qemu_vfree(mcb
->callbacks
[i
].free_buf
);
2136 static void multiwrite_cb(void *opaque
, int ret
)
2138 MultiwriteCB
*mcb
= opaque
;
2140 trace_multiwrite_cb(mcb
, ret
);
2142 if (ret
< 0 && !mcb
->error
) {
2146 mcb
->num_requests
--;
2147 if (mcb
->num_requests
== 0) {
2148 multiwrite_user_cb(mcb
);
2153 static int multiwrite_req_compare(const void *a
, const void *b
)
2155 const BlockRequest
*req1
= a
, *req2
= b
;
2158 * Note that we can't simply subtract req2->sector from req1->sector
2159 * here as that could overflow the return value.
2161 if (req1
->sector
> req2
->sector
) {
2163 } else if (req1
->sector
< req2
->sector
) {
2171 * Takes a bunch of requests and tries to merge them. Returns the number of
2172 * requests that remain after merging.
2174 static int multiwrite_merge(BlockDriverState
*bs
, BlockRequest
*reqs
,
2175 int num_reqs
, MultiwriteCB
*mcb
)
2179 // Sort requests by start sector
2180 qsort(reqs
, num_reqs
, sizeof(*reqs
), &multiwrite_req_compare
);
2182 // Check if adjacent requests touch the same clusters. If so, combine them,
2183 // filling up gaps with zero sectors.
2185 for (i
= 1; i
< num_reqs
; i
++) {
2187 int64_t oldreq_last
= reqs
[outidx
].sector
+ reqs
[outidx
].nb_sectors
;
2189 // This handles the cases that are valid for all block drivers, namely
2190 // exactly sequential writes and overlapping writes.
2191 if (reqs
[i
].sector
<= oldreq_last
) {
2195 // The block driver may decide that it makes sense to combine requests
2196 // even if there is a gap of some sectors between them. In this case,
2197 // the gap is filled with zeros (therefore only applicable for yet
2198 // unused space in format like qcow2).
2199 if (!merge
&& bs
->drv
->bdrv_merge_requests
) {
2200 merge
= bs
->drv
->bdrv_merge_requests(bs
, &reqs
[outidx
], &reqs
[i
]);
2203 if (reqs
[outidx
].qiov
->niov
+ reqs
[i
].qiov
->niov
+ 1 > IOV_MAX
) {
2209 QEMUIOVector
*qiov
= qemu_mallocz(sizeof(*qiov
));
2210 qemu_iovec_init(qiov
,
2211 reqs
[outidx
].qiov
->niov
+ reqs
[i
].qiov
->niov
+ 1);
2213 // Add the first request to the merged one. If the requests are
2214 // overlapping, drop the last sectors of the first request.
2215 size
= (reqs
[i
].sector
- reqs
[outidx
].sector
) << 9;
2216 qemu_iovec_concat(qiov
, reqs
[outidx
].qiov
, size
);
2218 // We might need to add some zeros between the two requests
2219 if (reqs
[i
].sector
> oldreq_last
) {
2220 size_t zero_bytes
= (reqs
[i
].sector
- oldreq_last
) << 9;
2221 uint8_t *buf
= qemu_blockalign(bs
, zero_bytes
);
2222 memset(buf
, 0, zero_bytes
);
2223 qemu_iovec_add(qiov
, buf
, zero_bytes
);
2224 mcb
->callbacks
[i
].free_buf
= buf
;
2227 // Add the second request
2228 qemu_iovec_concat(qiov
, reqs
[i
].qiov
, reqs
[i
].qiov
->size
);
2230 reqs
[outidx
].nb_sectors
= qiov
->size
>> 9;
2231 reqs
[outidx
].qiov
= qiov
;
2233 mcb
->callbacks
[i
].free_qiov
= reqs
[outidx
].qiov
;
2236 reqs
[outidx
].sector
= reqs
[i
].sector
;
2237 reqs
[outidx
].nb_sectors
= reqs
[i
].nb_sectors
;
2238 reqs
[outidx
].qiov
= reqs
[i
].qiov
;
2246 * Submit multiple AIO write requests at once.
2248 * On success, the function returns 0 and all requests in the reqs array have
2249 * been submitted. In error case this function returns -1, and any of the
2250 * requests may or may not be submitted yet. In particular, this means that the
2251 * callback will be called for some of the requests, for others it won't. The
2252 * caller must check the error field of the BlockRequest to wait for the right
2253 * callbacks (if error != 0, no callback will be called).
2255 * The implementation may modify the contents of the reqs array, e.g. to merge
2256 * requests. However, the fields opaque and error are left unmodified as they
2257 * are used to signal failure for a single request to the caller.
2259 int bdrv_aio_multiwrite(BlockDriverState
*bs
, BlockRequest
*reqs
, int num_reqs
)
2261 BlockDriverAIOCB
*acb
;
2265 if (num_reqs
== 0) {
2269 // Create MultiwriteCB structure
2270 mcb
= qemu_mallocz(sizeof(*mcb
) + num_reqs
* sizeof(*mcb
->callbacks
));
2271 mcb
->num_requests
= 0;
2272 mcb
->num_callbacks
= num_reqs
;
2274 for (i
= 0; i
< num_reqs
; i
++) {
2275 mcb
->callbacks
[i
].cb
= reqs
[i
].cb
;
2276 mcb
->callbacks
[i
].opaque
= reqs
[i
].opaque
;
2279 // Check for mergable requests
2280 num_reqs
= multiwrite_merge(bs
, reqs
, num_reqs
, mcb
);
2282 trace_bdrv_aio_multiwrite(mcb
, mcb
->num_callbacks
, num_reqs
);
2285 * Run the aio requests. As soon as one request can't be submitted
2286 * successfully, fail all requests that are not yet submitted (we must
2287 * return failure for all requests anyway)
2289 * num_requests cannot be set to the right value immediately: If
2290 * bdrv_aio_writev fails for some request, num_requests would be too high
2291 * and therefore multiwrite_cb() would never recognize the multiwrite
2292 * request as completed. We also cannot use the loop variable i to set it
2293 * when the first request fails because the callback may already have been
2294 * called for previously submitted requests. Thus, num_requests must be
2295 * incremented for each request that is submitted.
2297 * The problem that callbacks may be called early also means that we need
2298 * to take care that num_requests doesn't become 0 before all requests are
2299 * submitted - multiwrite_cb() would consider the multiwrite request
2300 * completed. A dummy request that is "completed" by a manual call to
2301 * multiwrite_cb() takes care of this.
2303 mcb
->num_requests
= 1;
2305 // Run the aio requests
2306 for (i
= 0; i
< num_reqs
; i
++) {
2307 mcb
->num_requests
++;
2308 acb
= bdrv_aio_writev(bs
, reqs
[i
].sector
, reqs
[i
].qiov
,
2309 reqs
[i
].nb_sectors
, multiwrite_cb
, mcb
);
2312 // We can only fail the whole thing if no request has been
2313 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2314 // complete and report the error in the callback.
2316 trace_bdrv_aio_multiwrite_earlyfail(mcb
);
2319 trace_bdrv_aio_multiwrite_latefail(mcb
, i
);
2320 multiwrite_cb(mcb
, -EIO
);
2326 /* Complete the dummy request */
2327 multiwrite_cb(mcb
, 0);
2332 for (i
= 0; i
< mcb
->num_callbacks
; i
++) {
2333 reqs
[i
].error
= -EIO
;
2339 BlockDriverAIOCB
*bdrv_aio_flush(BlockDriverState
*bs
,
2340 BlockDriverCompletionFunc
*cb
, void *opaque
)
2342 BlockDriver
*drv
= bs
->drv
;
2344 if (bs
->open_flags
& BDRV_O_NO_FLUSH
) {
2345 return bdrv_aio_noop_em(bs
, cb
, opaque
);
2350 return drv
->bdrv_aio_flush(bs
, cb
, opaque
);
2353 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
2355 acb
->pool
->cancel(acb
);
2359 /**************************************************************/
2360 /* async block device emulation */
2362 typedef struct BlockDriverAIOCBSync
{
2363 BlockDriverAIOCB common
;
2366 /* vector translation state */
2370 } BlockDriverAIOCBSync
;
2372 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
2374 BlockDriverAIOCBSync
*acb
=
2375 container_of(blockacb
, BlockDriverAIOCBSync
, common
);
2376 qemu_bh_delete(acb
->bh
);
2378 qemu_aio_release(acb
);
2381 static AIOPool bdrv_em_aio_pool
= {
2382 .aiocb_size
= sizeof(BlockDriverAIOCBSync
),
2383 .cancel
= bdrv_aio_cancel_em
,
2386 static void bdrv_aio_bh_cb(void *opaque
)
2388 BlockDriverAIOCBSync
*acb
= opaque
;
2391 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
2392 qemu_vfree(acb
->bounce
);
2393 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
2394 qemu_bh_delete(acb
->bh
);
2396 qemu_aio_release(acb
);
2399 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
2403 BlockDriverCompletionFunc
*cb
,
2408 BlockDriverAIOCBSync
*acb
;
2410 acb
= qemu_aio_get(&bdrv_em_aio_pool
, bs
, cb
, opaque
);
2411 acb
->is_write
= is_write
;
2413 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
2416 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
2419 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
2420 acb
->ret
= bdrv_write(bs
, sector_num
, acb
->bounce
, nb_sectors
);
2422 acb
->ret
= bdrv_read(bs
, sector_num
, acb
->bounce
, nb_sectors
);
2425 qemu_bh_schedule(acb
->bh
);
2427 return &acb
->common
;
2430 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
2431 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
2432 BlockDriverCompletionFunc
*cb
, void *opaque
)
2434 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
2437 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
2438 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
2439 BlockDriverCompletionFunc
*cb
, void *opaque
)
2441 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
2444 static BlockDriverAIOCB
*bdrv_aio_flush_em(BlockDriverState
*bs
,
2445 BlockDriverCompletionFunc
*cb
, void *opaque
)
2447 BlockDriverAIOCBSync
*acb
;
2449 acb
= qemu_aio_get(&bdrv_em_aio_pool
, bs
, cb
, opaque
);
2450 acb
->is_write
= 1; /* don't bounce in the completion hadler */
2456 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
2459 qemu_bh_schedule(acb
->bh
);
2460 return &acb
->common
;
2463 static BlockDriverAIOCB
*bdrv_aio_noop_em(BlockDriverState
*bs
,
2464 BlockDriverCompletionFunc
*cb
, void *opaque
)
2466 BlockDriverAIOCBSync
*acb
;
2468 acb
= qemu_aio_get(&bdrv_em_aio_pool
, bs
, cb
, opaque
);
2469 acb
->is_write
= 1; /* don't bounce in the completion handler */
2475 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
2478 qemu_bh_schedule(acb
->bh
);
2479 return &acb
->common
;
2482 /**************************************************************/
2483 /* sync block device emulation */
2485 static void bdrv_rw_em_cb(void *opaque
, int ret
)
2487 *(int *)opaque
= ret
;
2490 #define NOT_DONE 0x7fffffff
2492 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
2493 uint8_t *buf
, int nb_sectors
)
2496 BlockDriverAIOCB
*acb
;
2500 async_context_push();
2502 async_ret
= NOT_DONE
;
2503 iov
.iov_base
= (void *)buf
;
2504 iov
.iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
;
2505 qemu_iovec_init_external(&qiov
, &iov
, 1);
2506 acb
= bdrv_aio_readv(bs
, sector_num
, &qiov
, nb_sectors
,
2507 bdrv_rw_em_cb
, &async_ret
);
2513 while (async_ret
== NOT_DONE
) {
2519 async_context_pop();
2523 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
2524 const uint8_t *buf
, int nb_sectors
)
2527 BlockDriverAIOCB
*acb
;
2531 async_context_push();
2533 async_ret
= NOT_DONE
;
2534 iov
.iov_base
= (void *)buf
;
2535 iov
.iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
;
2536 qemu_iovec_init_external(&qiov
, &iov
, 1);
2537 acb
= bdrv_aio_writev(bs
, sector_num
, &qiov
, nb_sectors
,
2538 bdrv_rw_em_cb
, &async_ret
);
2543 while (async_ret
== NOT_DONE
) {
2548 async_context_pop();
2552 void bdrv_init(void)
2554 module_call_init(MODULE_INIT_BLOCK
);
2557 void bdrv_init_with_whitelist(void)
2559 use_bdrv_whitelist
= 1;
2563 void *qemu_aio_get(AIOPool
*pool
, BlockDriverState
*bs
,
2564 BlockDriverCompletionFunc
*cb
, void *opaque
)
2566 BlockDriverAIOCB
*acb
;
2568 if (pool
->free_aiocb
) {
2569 acb
= pool
->free_aiocb
;
2570 pool
->free_aiocb
= acb
->next
;
2572 acb
= qemu_mallocz(pool
->aiocb_size
);
2577 acb
->opaque
= opaque
;
2581 void qemu_aio_release(void *p
)
2583 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
2584 AIOPool
*pool
= acb
->pool
;
2585 acb
->next
= pool
->free_aiocb
;
2586 pool
->free_aiocb
= acb
;
2589 /**************************************************************/
2590 /* removable device support */
2593 * Return TRUE if the media is present
2595 int bdrv_is_inserted(BlockDriverState
*bs
)
2597 BlockDriver
*drv
= bs
->drv
;
2601 if (!drv
->bdrv_is_inserted
)
2602 return !bs
->tray_open
;
2603 ret
= drv
->bdrv_is_inserted(bs
);
2608 * Return TRUE if the media changed since the last call to this
2609 * function. It is currently only used for floppy disks
2611 int bdrv_media_changed(BlockDriverState
*bs
)
2613 BlockDriver
*drv
= bs
->drv
;
2616 if (!drv
|| !drv
->bdrv_media_changed
)
2619 ret
= drv
->bdrv_media_changed(bs
);
2620 if (ret
== -ENOTSUP
)
2621 ret
= bs
->media_changed
;
2622 bs
->media_changed
= 0;
2627 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2629 int bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
2631 BlockDriver
*drv
= bs
->drv
;
2638 if (!drv
|| !drv
->bdrv_eject
) {
2641 ret
= drv
->bdrv_eject(bs
, eject_flag
);
2643 if (ret
== -ENOTSUP
) {
2647 bs
->tray_open
= eject_flag
;
2653 int bdrv_is_locked(BlockDriverState
*bs
)
2659 * Lock or unlock the media (if it is locked, the user won't be able
2660 * to eject it manually).
2662 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
2664 BlockDriver
*drv
= bs
->drv
;
2666 bs
->locked
= locked
;
2667 if (drv
&& drv
->bdrv_set_locked
) {
2668 drv
->bdrv_set_locked(bs
, locked
);
2672 /* needed for generic scsi interface */
2674 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
2676 BlockDriver
*drv
= bs
->drv
;
2678 if (drv
&& drv
->bdrv_ioctl
)
2679 return drv
->bdrv_ioctl(bs
, req
, buf
);
2683 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
2684 unsigned long int req
, void *buf
,
2685 BlockDriverCompletionFunc
*cb
, void *opaque
)
2687 BlockDriver
*drv
= bs
->drv
;
2689 if (drv
&& drv
->bdrv_aio_ioctl
)
2690 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);
2696 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
2698 return qemu_memalign((bs
&& bs
->buffer_alignment
) ? bs
->buffer_alignment
: 512, size
);
2701 void bdrv_set_dirty_tracking(BlockDriverState
*bs
, int enable
)
2703 int64_t bitmap_size
;
2705 bs
->dirty_count
= 0;
2707 if (!bs
->dirty_bitmap
) {
2708 bitmap_size
= (bdrv_getlength(bs
) >> BDRV_SECTOR_BITS
) +
2709 BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
2710 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
2712 bs
->dirty_bitmap
= qemu_mallocz(bitmap_size
);
2715 if (bs
->dirty_bitmap
) {
2716 qemu_free(bs
->dirty_bitmap
);
2717 bs
->dirty_bitmap
= NULL
;
2722 int bdrv_get_dirty(BlockDriverState
*bs
, int64_t sector
)
2724 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
2726 if (bs
->dirty_bitmap
&&
2727 (sector
<< BDRV_SECTOR_BITS
) < bdrv_getlength(bs
)) {
2728 return !!(bs
->dirty_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
2729 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
2735 void bdrv_reset_dirty(BlockDriverState
*bs
, int64_t cur_sector
,
2738 set_dirty_bitmap(bs
, cur_sector
, nr_sectors
, 0);
2741 int64_t bdrv_get_dirty_count(BlockDriverState
*bs
)
2743 return bs
->dirty_count
;