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"
26 /* include native header before sys-queue.h */
27 #include <sys/queue.h>
30 #include "qemu-common.h"
32 #include "block_int.h"
36 #include <sys/types.h>
38 #include <sys/ioctl.h>
49 #define SECTOR_SIZE (1 << SECTOR_BITS)
51 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
52 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
53 BlockDriverCompletionFunc
*cb
, void *opaque
);
54 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
55 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
56 BlockDriverCompletionFunc
*cb
, void *opaque
);
57 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
58 uint8_t *buf
, int nb_sectors
);
59 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
60 const uint8_t *buf
, int nb_sectors
);
62 BlockDriverState
*bdrv_first
;
64 static BlockDriver
*first_drv
;
66 int path_is_absolute(const char *path
)
70 /* specific case for names like: "\\.\d:" */
71 if (*path
== '/' || *path
== '\\')
74 p
= strchr(path
, ':');
80 return (*p
== '/' || *p
== '\\');
86 /* if filename is absolute, just copy it to dest. Otherwise, build a
87 path to it by considering it is relative to base_path. URL are
89 void path_combine(char *dest
, int dest_size
,
90 const char *base_path
,
98 if (path_is_absolute(filename
)) {
99 pstrcpy(dest
, dest_size
, filename
);
101 p
= strchr(base_path
, ':');
106 p1
= strrchr(base_path
, '/');
110 p2
= strrchr(base_path
, '\\');
122 if (len
> dest_size
- 1)
124 memcpy(dest
, base_path
, len
);
126 pstrcat(dest
, dest_size
, filename
);
130 void bdrv_register(BlockDriver
*bdrv
)
132 if (!bdrv
->bdrv_aio_readv
) {
133 /* add AIO emulation layer */
134 bdrv
->bdrv_aio_readv
= bdrv_aio_readv_em
;
135 bdrv
->bdrv_aio_writev
= bdrv_aio_writev_em
;
136 } else if (!bdrv
->bdrv_read
) {
137 /* add synchronous IO emulation layer */
138 bdrv
->bdrv_read
= bdrv_read_em
;
139 bdrv
->bdrv_write
= bdrv_write_em
;
141 bdrv
->next
= first_drv
;
145 /* create a new block device (by default it is empty) */
146 BlockDriverState
*bdrv_new(const char *device_name
)
148 BlockDriverState
**pbs
, *bs
;
150 bs
= qemu_mallocz(sizeof(BlockDriverState
));
151 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
152 if (device_name
[0] != '\0') {
153 /* insert at the end */
162 BlockDriver
*bdrv_find_format(const char *format_name
)
165 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
166 if (!strcmp(drv1
->format_name
, format_name
))
172 int bdrv_create(BlockDriver
*drv
, const char* filename
,
173 QEMUOptionParameter
*options
)
175 if (!drv
->bdrv_create
)
178 return drv
->bdrv_create(filename
, options
);
182 void get_tmp_filename(char *filename
, int size
)
184 char temp_dir
[MAX_PATH
];
186 GetTempPath(MAX_PATH
, temp_dir
);
187 GetTempFileName(temp_dir
, "qem", 0, filename
);
190 void get_tmp_filename(char *filename
, int size
)
194 /* XXX: race condition possible */
195 tmpdir
= getenv("TMPDIR");
198 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
199 fd
= mkstemp(filename
);
205 static int is_windows_drive_prefix(const char *filename
)
207 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
208 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
212 int is_windows_drive(const char *filename
)
214 if (is_windows_drive_prefix(filename
) &&
217 if (strstart(filename
, "\\\\.\\", NULL
) ||
218 strstart(filename
, "//./", NULL
))
224 static BlockDriver
*find_protocol(const char *filename
)
232 if (is_windows_drive(filename
) ||
233 is_windows_drive_prefix(filename
))
234 return bdrv_find_format("raw");
236 p
= strchr(filename
, ':');
238 return bdrv_find_format("raw");
240 if (len
> sizeof(protocol
) - 1)
241 len
= sizeof(protocol
) - 1;
242 memcpy(protocol
, filename
, len
);
243 protocol
[len
] = '\0';
244 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
245 if (drv1
->protocol_name
&&
246 !strcmp(drv1
->protocol_name
, protocol
))
253 * Detect host devices. By convention, /dev/cdrom[N] is always
254 * recognized as a host CDROM.
256 static BlockDriver
*find_hdev_driver(const char *filename
)
258 int score_max
= 0, score
;
259 BlockDriver
*drv
= NULL
, *d
;
261 for (d
= first_drv
; d
; d
= d
->next
) {
262 if (d
->bdrv_probe_device
) {
263 score
= d
->bdrv_probe_device(filename
);
264 if (score
> score_max
) {
274 static BlockDriver
*find_image_format(const char *filename
)
276 int ret
, score
, score_max
;
277 BlockDriver
*drv1
, *drv
;
279 BlockDriverState
*bs
;
281 drv
= find_protocol(filename
);
282 /* no need to test disk image formats for vvfat */
283 if (drv
&& strcmp(drv
->format_name
, "vvfat") == 0)
286 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
289 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
296 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
297 if (drv1
->bdrv_probe
) {
298 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
299 if (score
> score_max
) {
308 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
310 BlockDriverState
*bs
;
314 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
324 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
326 return bdrv_open2(bs
, filename
, flags
, NULL
);
329 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
333 char tmp_filename
[PATH_MAX
];
334 char backing_filename
[PATH_MAX
];
337 bs
->is_temporary
= 0;
340 /* buffer_alignment defaulted to 512, drivers can change this value */
341 bs
->buffer_alignment
= 512;
343 if (flags
& BDRV_O_SNAPSHOT
) {
344 BlockDriverState
*bs1
;
347 BlockDriver
*bdrv_qcow2
;
348 QEMUOptionParameter
*options
;
350 /* if snapshot, we create a temporary backing file and open it
351 instead of opening 'filename' directly */
353 /* if there is a backing file, use it */
355 ret
= bdrv_open2(bs1
, filename
, 0, drv
);
360 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
362 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
367 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
369 /* Real path is meaningless for protocols */
371 snprintf(backing_filename
, sizeof(backing_filename
),
374 realpath(filename
, backing_filename
);
376 bdrv_qcow2
= bdrv_find_format("qcow2");
377 options
= parse_option_parameters("", bdrv_qcow2
->create_options
, NULL
);
379 set_option_parameter_int(options
, BLOCK_OPT_SIZE
, total_size
* 512);
380 set_option_parameter(options
, BLOCK_OPT_BACKING_FILE
, backing_filename
);
382 set_option_parameter(options
, BLOCK_OPT_BACKING_FMT
,
386 ret
= bdrv_create(bdrv_qcow2
, tmp_filename
, options
);
391 filename
= tmp_filename
;
393 bs
->is_temporary
= 1;
396 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
397 if (flags
& BDRV_O_FILE
) {
398 drv
= find_protocol(filename
);
400 drv
= find_hdev_driver(filename
);
402 drv
= find_image_format(filename
);
407 goto unlink_and_fail
;
410 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
413 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
414 * write cache to the guest. We do need the fdatasync to flush
415 * out transactions for block allocations, and we maybe have a
416 * volatile write cache in our backing device to deal with.
418 if (flags
& (BDRV_O_CACHE_WB
|BDRV_O_NOCACHE
))
419 bs
->enable_write_cache
= 1;
421 /* Note: for compatibility, we open disk image files as RDWR, and
422 RDONLY as fallback */
423 if (!(flags
& BDRV_O_FILE
))
424 open_flags
= BDRV_O_RDWR
|
425 (flags
& (BDRV_O_CACHE_MASK
|BDRV_O_NATIVE_AIO
));
427 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
428 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
429 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
430 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
434 qemu_free(bs
->opaque
);
438 if (bs
->is_temporary
)
442 if (drv
->bdrv_getlength
) {
443 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
446 if (bs
->is_temporary
) {
450 if (bs
->backing_file
[0] != '\0') {
451 /* if there is a backing file, use it */
452 BlockDriver
*back_drv
= NULL
;
453 bs
->backing_hd
= bdrv_new("");
454 path_combine(backing_filename
, sizeof(backing_filename
),
455 filename
, bs
->backing_file
);
456 if (bs
->backing_format
[0] != '\0')
457 back_drv
= bdrv_find_format(bs
->backing_format
);
458 ret
= bdrv_open2(bs
->backing_hd
, backing_filename
, open_flags
,
466 if (!bdrv_key_required(bs
)) {
467 /* call the change callback */
468 bs
->media_changed
= 1;
470 bs
->change_cb(bs
->change_opaque
);
475 void bdrv_close(BlockDriverState
*bs
)
479 bdrv_delete(bs
->backing_hd
);
480 bs
->drv
->bdrv_close(bs
);
481 qemu_free(bs
->opaque
);
483 if (bs
->is_temporary
) {
484 unlink(bs
->filename
);
490 /* call the change callback */
491 bs
->media_changed
= 1;
493 bs
->change_cb(bs
->change_opaque
);
497 void bdrv_delete(BlockDriverState
*bs
)
499 BlockDriverState
**pbs
;
502 while (*pbs
!= bs
&& *pbs
!= NULL
)
512 * Run consistency checks on an image
514 * Returns the number of errors or -errno when an internal error occurs
516 int bdrv_check(BlockDriverState
*bs
)
518 if (bs
->drv
->bdrv_check
== NULL
) {
522 return bs
->drv
->bdrv_check(bs
);
525 /* commit COW file into the raw image */
526 int bdrv_commit(BlockDriverState
*bs
)
528 BlockDriver
*drv
= bs
->drv
;
529 int64_t i
, total_sectors
;
531 unsigned char sector
[512];
540 if (!bs
->backing_hd
) {
544 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
545 for (i
= 0; i
< total_sectors
;) {
546 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
547 for(j
= 0; j
< n
; j
++) {
548 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
552 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
562 if (drv
->bdrv_make_empty
)
563 return drv
->bdrv_make_empty(bs
);
568 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
573 if (!bdrv_is_inserted(bs
))
579 len
= bdrv_getlength(bs
);
584 if ((offset
> len
) || (len
- offset
< size
))
590 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
593 return bdrv_check_byte_request(bs
, sector_num
* 512, nb_sectors
* 512);
596 /* return < 0 if error. See bdrv_write() for the return codes */
597 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
598 uint8_t *buf
, int nb_sectors
)
600 BlockDriver
*drv
= bs
->drv
;
604 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
607 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
610 /* Return < 0 if error. Important errors are:
611 -EIO generic I/O error (may happen for all errors)
612 -ENOMEDIUM No media inserted.
613 -EINVAL Invalid sector number or nb_sectors
614 -EACCES Trying to write a read-only device
616 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
617 const uint8_t *buf
, int nb_sectors
)
619 BlockDriver
*drv
= bs
->drv
;
624 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
627 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
630 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
631 void *buf
, int count1
)
633 uint8_t tmp_buf
[SECTOR_SIZE
];
634 int len
, nb_sectors
, count
;
638 /* first read to align to sector start */
639 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
642 sector_num
= offset
>> SECTOR_BITS
;
644 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
646 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
654 /* read the sectors "in place" */
655 nb_sectors
= count
>> SECTOR_BITS
;
656 if (nb_sectors
> 0) {
657 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
659 sector_num
+= nb_sectors
;
660 len
= nb_sectors
<< SECTOR_BITS
;
665 /* add data from the last sector */
667 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
669 memcpy(buf
, tmp_buf
, count
);
674 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
675 const void *buf
, int count1
)
677 uint8_t tmp_buf
[SECTOR_SIZE
];
678 int len
, nb_sectors
, count
;
682 /* first write to align to sector start */
683 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
686 sector_num
= offset
>> SECTOR_BITS
;
688 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
690 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
691 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
700 /* write the sectors "in place" */
701 nb_sectors
= count
>> SECTOR_BITS
;
702 if (nb_sectors
> 0) {
703 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
705 sector_num
+= nb_sectors
;
706 len
= nb_sectors
<< SECTOR_BITS
;
711 /* add data from the last sector */
713 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
715 memcpy(tmp_buf
, buf
, count
);
716 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
723 * Truncate file to 'offset' bytes (needed only for file protocols)
725 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
727 BlockDriver
*drv
= bs
->drv
;
730 if (!drv
->bdrv_truncate
)
732 return drv
->bdrv_truncate(bs
, offset
);
736 * Length of a file in bytes. Return < 0 if error or unknown.
738 int64_t bdrv_getlength(BlockDriverState
*bs
)
740 BlockDriver
*drv
= bs
->drv
;
743 if (!drv
->bdrv_getlength
) {
745 return bs
->total_sectors
* SECTOR_SIZE
;
747 return drv
->bdrv_getlength(bs
);
750 /* return 0 as number of sectors if no device present or error */
751 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
754 length
= bdrv_getlength(bs
);
758 length
= length
>> SECTOR_BITS
;
759 *nb_sectors_ptr
= length
;
763 uint8_t boot_ind
; /* 0x80 - active */
764 uint8_t head
; /* starting head */
765 uint8_t sector
; /* starting sector */
766 uint8_t cyl
; /* starting cylinder */
767 uint8_t sys_ind
; /* What partition type */
768 uint8_t end_head
; /* end head */
769 uint8_t end_sector
; /* end sector */
770 uint8_t end_cyl
; /* end cylinder */
771 uint32_t start_sect
; /* starting sector counting from 0 */
772 uint32_t nr_sects
; /* nr of sectors in partition */
773 } __attribute__((packed
));
775 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
776 static int guess_disk_lchs(BlockDriverState
*bs
,
777 int *pcylinders
, int *pheads
, int *psectors
)
780 int ret
, i
, heads
, sectors
, cylinders
;
785 bdrv_get_geometry(bs
, &nb_sectors
);
787 ret
= bdrv_read(bs
, 0, buf
, 1);
790 /* test msdos magic */
791 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
793 for(i
= 0; i
< 4; i
++) {
794 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
795 nr_sects
= le32_to_cpu(p
->nr_sects
);
796 if (nr_sects
&& p
->end_head
) {
797 /* We make the assumption that the partition terminates on
798 a cylinder boundary */
799 heads
= p
->end_head
+ 1;
800 sectors
= p
->end_sector
& 63;
803 cylinders
= nb_sectors
/ (heads
* sectors
);
804 if (cylinders
< 1 || cylinders
> 16383)
808 *pcylinders
= cylinders
;
810 printf("guessed geometry: LCHS=%d %d %d\n",
811 cylinders
, heads
, sectors
);
819 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
821 int translation
, lba_detected
= 0;
822 int cylinders
, heads
, secs
;
825 /* if a geometry hint is available, use it */
826 bdrv_get_geometry(bs
, &nb_sectors
);
827 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
828 translation
= bdrv_get_translation_hint(bs
);
829 if (cylinders
!= 0) {
834 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
836 /* if heads > 16, it means that a BIOS LBA
837 translation was active, so the default
838 hardware geometry is OK */
840 goto default_geometry
;
845 /* disable any translation to be in sync with
846 the logical geometry */
847 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
848 bdrv_set_translation_hint(bs
,
849 BIOS_ATA_TRANSLATION_NONE
);
854 /* if no geometry, use a standard physical disk geometry */
855 cylinders
= nb_sectors
/ (16 * 63);
857 if (cylinders
> 16383)
859 else if (cylinders
< 2)
864 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
865 if ((*pcyls
* *pheads
) <= 131072) {
866 bdrv_set_translation_hint(bs
,
867 BIOS_ATA_TRANSLATION_LARGE
);
869 bdrv_set_translation_hint(bs
,
870 BIOS_ATA_TRANSLATION_LBA
);
874 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
878 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
879 int cyls
, int heads
, int secs
)
886 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
889 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
890 type
== BDRV_TYPE_FLOPPY
));
893 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
895 bs
->translation
= translation
;
898 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
899 int *pcyls
, int *pheads
, int *psecs
)
906 int bdrv_get_type_hint(BlockDriverState
*bs
)
911 int bdrv_get_translation_hint(BlockDriverState
*bs
)
913 return bs
->translation
;
916 int bdrv_is_removable(BlockDriverState
*bs
)
918 return bs
->removable
;
921 int bdrv_is_read_only(BlockDriverState
*bs
)
923 return bs
->read_only
;
926 int bdrv_is_sg(BlockDriverState
*bs
)
931 int bdrv_enable_write_cache(BlockDriverState
*bs
)
933 return bs
->enable_write_cache
;
936 /* XXX: no longer used */
937 void bdrv_set_change_cb(BlockDriverState
*bs
,
938 void (*change_cb
)(void *opaque
), void *opaque
)
940 bs
->change_cb
= change_cb
;
941 bs
->change_opaque
= opaque
;
944 int bdrv_is_encrypted(BlockDriverState
*bs
)
946 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
948 return bs
->encrypted
;
951 int bdrv_key_required(BlockDriverState
*bs
)
953 BlockDriverState
*backing_hd
= bs
->backing_hd
;
955 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
957 return (bs
->encrypted
&& !bs
->valid_key
);
960 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
963 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
964 ret
= bdrv_set_key(bs
->backing_hd
, key
);
970 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
972 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
975 } else if (!bs
->valid_key
) {
977 /* call the change callback now, we skipped it on open */
978 bs
->media_changed
= 1;
980 bs
->change_cb(bs
->change_opaque
);
985 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
990 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
994 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
999 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
1000 it(opaque
, drv
->format_name
);
1004 BlockDriverState
*bdrv_find(const char *name
)
1006 BlockDriverState
*bs
;
1008 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1009 if (!strcmp(name
, bs
->device_name
))
1015 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1017 BlockDriverState
*bs
;
1019 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1024 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1026 return bs
->device_name
;
1029 void bdrv_flush(BlockDriverState
*bs
)
1033 if (bs
->drv
->bdrv_flush
)
1034 bs
->drv
->bdrv_flush(bs
);
1036 bdrv_flush(bs
->backing_hd
);
1039 void bdrv_flush_all(void)
1041 BlockDriverState
*bs
;
1043 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1044 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1045 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1050 * Returns true iff the specified sector is present in the disk image. Drivers
1051 * not implementing the functionality are assumed to not support backing files,
1052 * hence all their sectors are reported as allocated.
1054 * 'pnum' is set to the number of sectors (including and immediately following
1055 * the specified sector) that are known to be in the same
1056 * allocated/unallocated state.
1058 * 'nb_sectors' is the max value 'pnum' should be set to.
1060 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1064 if (!bs
->drv
->bdrv_is_allocated
) {
1065 if (sector_num
>= bs
->total_sectors
) {
1069 n
= bs
->total_sectors
- sector_num
;
1070 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1073 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1076 void bdrv_info(Monitor
*mon
)
1078 BlockDriverState
*bs
;
1080 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1081 monitor_printf(mon
, "%s:", bs
->device_name
);
1082 monitor_printf(mon
, " type=");
1085 monitor_printf(mon
, "hd");
1087 case BDRV_TYPE_CDROM
:
1088 monitor_printf(mon
, "cdrom");
1090 case BDRV_TYPE_FLOPPY
:
1091 monitor_printf(mon
, "floppy");
1094 monitor_printf(mon
, " removable=%d", bs
->removable
);
1095 if (bs
->removable
) {
1096 monitor_printf(mon
, " locked=%d", bs
->locked
);
1099 monitor_printf(mon
, " file=");
1100 monitor_print_filename(mon
, bs
->filename
);
1101 if (bs
->backing_file
[0] != '\0') {
1102 monitor_printf(mon
, " backing_file=");
1103 monitor_print_filename(mon
, bs
->backing_file
);
1105 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1106 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1107 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1109 monitor_printf(mon
, " [not inserted]");
1111 monitor_printf(mon
, "\n");
1115 /* The "info blockstats" command. */
1116 void bdrv_info_stats(Monitor
*mon
)
1118 BlockDriverState
*bs
;
1120 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1121 monitor_printf(mon
, "%s:"
1122 " rd_bytes=%" PRIu64
1123 " wr_bytes=%" PRIu64
1124 " rd_operations=%" PRIu64
1125 " wr_operations=%" PRIu64
1128 bs
->rd_bytes
, bs
->wr_bytes
,
1129 bs
->rd_ops
, bs
->wr_ops
);
1133 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1135 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1136 return bs
->backing_file
;
1137 else if (bs
->encrypted
)
1138 return bs
->filename
;
1143 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1144 char *filename
, int filename_size
)
1146 if (!bs
->backing_hd
) {
1147 pstrcpy(filename
, filename_size
, "");
1149 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1153 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1154 const uint8_t *buf
, int nb_sectors
)
1156 BlockDriver
*drv
= bs
->drv
;
1159 if (!drv
->bdrv_write_compressed
)
1161 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1163 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1166 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1168 BlockDriver
*drv
= bs
->drv
;
1171 if (!drv
->bdrv_get_info
)
1173 memset(bdi
, 0, sizeof(*bdi
));
1174 return drv
->bdrv_get_info(bs
, bdi
);
1177 int bdrv_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
1178 int64_t pos
, int size
)
1180 BlockDriver
*drv
= bs
->drv
;
1183 if (!drv
->bdrv_save_vmstate
)
1185 return drv
->bdrv_save_vmstate(bs
, buf
, pos
, size
);
1188 int bdrv_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
1189 int64_t pos
, int size
)
1191 BlockDriver
*drv
= bs
->drv
;
1194 if (!drv
->bdrv_load_vmstate
)
1196 return drv
->bdrv_load_vmstate(bs
, buf
, pos
, size
);
1199 /**************************************************************/
1200 /* handling of snapshots */
1202 int bdrv_snapshot_create(BlockDriverState
*bs
,
1203 QEMUSnapshotInfo
*sn_info
)
1205 BlockDriver
*drv
= bs
->drv
;
1208 if (!drv
->bdrv_snapshot_create
)
1210 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1213 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1214 const char *snapshot_id
)
1216 BlockDriver
*drv
= bs
->drv
;
1219 if (!drv
->bdrv_snapshot_goto
)
1221 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1224 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1226 BlockDriver
*drv
= bs
->drv
;
1229 if (!drv
->bdrv_snapshot_delete
)
1231 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1234 int bdrv_snapshot_list(BlockDriverState
*bs
,
1235 QEMUSnapshotInfo
**psn_info
)
1237 BlockDriver
*drv
= bs
->drv
;
1240 if (!drv
->bdrv_snapshot_list
)
1242 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1245 #define NB_SUFFIXES 4
1247 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1249 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1254 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1257 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1258 if (size
< (10 * base
)) {
1259 snprintf(buf
, buf_size
, "%0.1f%c",
1260 (double)size
/ base
,
1263 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1264 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1265 ((size
+ (base
>> 1)) / base
),
1275 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1277 char buf1
[128], date_buf
[128], clock_buf
[128];
1287 snprintf(buf
, buf_size
,
1288 "%-10s%-20s%7s%20s%15s",
1289 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1293 ptm
= localtime(&ti
);
1294 strftime(date_buf
, sizeof(date_buf
),
1295 "%Y-%m-%d %H:%M:%S", ptm
);
1297 localtime_r(&ti
, &tm
);
1298 strftime(date_buf
, sizeof(date_buf
),
1299 "%Y-%m-%d %H:%M:%S", &tm
);
1301 secs
= sn
->vm_clock_nsec
/ 1000000000;
1302 snprintf(clock_buf
, sizeof(clock_buf
),
1303 "%02d:%02d:%02d.%03d",
1305 (int)((secs
/ 60) % 60),
1307 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1308 snprintf(buf
, buf_size
,
1309 "%-10s%-20s%7s%20s%15s",
1310 sn
->id_str
, sn
->name
,
1311 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1319 /**************************************************************/
1322 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1323 QEMUIOVector
*qiov
, int nb_sectors
,
1324 BlockDriverCompletionFunc
*cb
, void *opaque
)
1326 BlockDriver
*drv
= bs
->drv
;
1327 BlockDriverAIOCB
*ret
;
1331 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1334 ret
= drv
->bdrv_aio_readv(bs
, sector_num
, qiov
, nb_sectors
,
1338 /* Update stats even though technically transfer has not happened. */
1339 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1346 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1347 QEMUIOVector
*qiov
, int nb_sectors
,
1348 BlockDriverCompletionFunc
*cb
, void *opaque
)
1350 BlockDriver
*drv
= bs
->drv
;
1351 BlockDriverAIOCB
*ret
;
1357 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1360 ret
= drv
->bdrv_aio_writev(bs
, sector_num
, qiov
, nb_sectors
,
1364 /* Update stats even though technically transfer has not happened. */
1365 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1373 typedef struct MultiwriteCB
{
1378 BlockDriverCompletionFunc
*cb
;
1380 QEMUIOVector
*free_qiov
;
1385 static void multiwrite_user_cb(MultiwriteCB
*mcb
)
1389 for (i
= 0; i
< mcb
->num_callbacks
; i
++) {
1390 mcb
->callbacks
[i
].cb(mcb
->callbacks
[i
].opaque
, mcb
->error
);
1391 qemu_free(mcb
->callbacks
[i
].free_qiov
);
1392 qemu_free(mcb
->callbacks
[i
].free_buf
);
1396 static void multiwrite_cb(void *opaque
, int ret
)
1398 MultiwriteCB
*mcb
= opaque
;
1402 multiwrite_user_cb(mcb
);
1405 mcb
->num_requests
--;
1406 if (mcb
->num_requests
== 0) {
1407 if (mcb
->error
== 0) {
1408 multiwrite_user_cb(mcb
);
1414 static int multiwrite_req_compare(const void *a
, const void *b
)
1416 return (((BlockRequest
*) a
)->sector
- ((BlockRequest
*) b
)->sector
);
1420 * Takes a bunch of requests and tries to merge them. Returns the number of
1421 * requests that remain after merging.
1423 static int multiwrite_merge(BlockDriverState
*bs
, BlockRequest
*reqs
,
1424 int num_reqs
, MultiwriteCB
*mcb
)
1428 // Sort requests by start sector
1429 qsort(reqs
, num_reqs
, sizeof(*reqs
), &multiwrite_req_compare
);
1431 // Check if adjacent requests touch the same clusters. If so, combine them,
1432 // filling up gaps with zero sectors.
1434 for (i
= 1; i
< num_reqs
; i
++) {
1436 int64_t oldreq_last
= reqs
[outidx
].sector
+ reqs
[outidx
].nb_sectors
;
1438 // This handles the cases that are valid for all block drivers, namely
1439 // exactly sequential writes and overlapping writes.
1440 if (reqs
[i
].sector
<= oldreq_last
) {
1444 // The block driver may decide that it makes sense to combine requests
1445 // even if there is a gap of some sectors between them. In this case,
1446 // the gap is filled with zeros (therefore only applicable for yet
1447 // unused space in format like qcow2).
1448 if (!merge
&& bs
->drv
->bdrv_merge_requests
) {
1449 merge
= bs
->drv
->bdrv_merge_requests(bs
, &reqs
[outidx
], &reqs
[i
]);
1454 QEMUIOVector
*qiov
= qemu_mallocz(sizeof(*qiov
));
1455 qemu_iovec_init(qiov
,
1456 reqs
[outidx
].qiov
->niov
+ reqs
[i
].qiov
->niov
+ 1);
1458 // Add the first request to the merged one. If the requests are
1459 // overlapping, drop the last sectors of the first request.
1460 size
= (reqs
[i
].sector
- reqs
[outidx
].sector
) << 9;
1461 qemu_iovec_concat(qiov
, reqs
[outidx
].qiov
, size
);
1463 // We might need to add some zeros between the two requests
1464 if (reqs
[i
].sector
> oldreq_last
) {
1465 size_t zero_bytes
= (reqs
[i
].sector
- oldreq_last
) << 9;
1466 uint8_t *buf
= qemu_blockalign(bs
, zero_bytes
);
1467 memset(buf
, 0, zero_bytes
);
1468 qemu_iovec_add(qiov
, buf
, zero_bytes
);
1469 mcb
->callbacks
[i
].free_buf
= buf
;
1472 // Add the second request
1473 qemu_iovec_concat(qiov
, reqs
[i
].qiov
, reqs
[i
].qiov
->size
);
1475 reqs
[outidx
].nb_sectors
+= reqs
[i
].nb_sectors
;
1476 reqs
[outidx
].qiov
= qiov
;
1478 mcb
->callbacks
[i
].free_qiov
= reqs
[outidx
].qiov
;
1481 reqs
[outidx
].sector
= reqs
[i
].sector
;
1482 reqs
[outidx
].nb_sectors
= reqs
[i
].nb_sectors
;
1483 reqs
[outidx
].qiov
= reqs
[i
].qiov
;
1491 * Submit multiple AIO write requests at once.
1493 * On success, the function returns 0 and all requests in the reqs array have
1494 * been submitted. In error case this function returns -1, and any of the
1495 * requests may or may not be submitted yet. In particular, this means that the
1496 * callback will be called for some of the requests, for others it won't. The
1497 * caller must check the error field of the BlockRequest to wait for the right
1498 * callbacks (if error != 0, no callback will be called).
1500 * The implementation may modify the contents of the reqs array, e.g. to merge
1501 * requests. However, the fields opaque and error are left unmodified as they
1502 * are used to signal failure for a single request to the caller.
1504 int bdrv_aio_multiwrite(BlockDriverState
*bs
, BlockRequest
*reqs
, int num_reqs
)
1506 BlockDriverAIOCB
*acb
;
1510 if (num_reqs
== 0) {
1514 // Create MultiwriteCB structure
1515 mcb
= qemu_mallocz(sizeof(*mcb
) + num_reqs
* sizeof(*mcb
->callbacks
));
1516 mcb
->num_requests
= 0;
1517 mcb
->num_callbacks
= num_reqs
;
1519 for (i
= 0; i
< num_reqs
; i
++) {
1520 mcb
->callbacks
[i
].cb
= reqs
[i
].cb
;
1521 mcb
->callbacks
[i
].opaque
= reqs
[i
].opaque
;
1524 // Check for mergable requests
1525 num_reqs
= multiwrite_merge(bs
, reqs
, num_reqs
, mcb
);
1527 // Run the aio requests
1528 for (i
= 0; i
< num_reqs
; i
++) {
1529 acb
= bdrv_aio_writev(bs
, reqs
[i
].sector
, reqs
[i
].qiov
,
1530 reqs
[i
].nb_sectors
, multiwrite_cb
, mcb
);
1533 // We can only fail the whole thing if no request has been
1534 // submitted yet. Otherwise we'll wait for the submitted AIOs to
1535 // complete and report the error in the callback.
1536 if (mcb
->num_requests
== 0) {
1537 reqs
[i
].error
= EIO
;
1544 mcb
->num_requests
++;
1555 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1557 acb
->pool
->cancel(acb
);
1561 /**************************************************************/
1562 /* async block device emulation */
1564 typedef struct BlockDriverAIOCBSync
{
1565 BlockDriverAIOCB common
;
1568 /* vector translation state */
1572 } BlockDriverAIOCBSync
;
1574 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1576 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1577 qemu_bh_delete(acb
->bh
);
1579 qemu_aio_release(acb
);
1582 static AIOPool bdrv_em_aio_pool
= {
1583 .aiocb_size
= sizeof(BlockDriverAIOCBSync
),
1584 .cancel
= bdrv_aio_cancel_em
,
1587 static void bdrv_aio_bh_cb(void *opaque
)
1589 BlockDriverAIOCBSync
*acb
= opaque
;
1592 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
1593 qemu_vfree(acb
->bounce
);
1594 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1595 qemu_bh_delete(acb
->bh
);
1597 qemu_aio_release(acb
);
1600 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1604 BlockDriverCompletionFunc
*cb
,
1609 BlockDriverAIOCBSync
*acb
;
1611 acb
= qemu_aio_get(&bdrv_em_aio_pool
, bs
, cb
, opaque
);
1612 acb
->is_write
= is_write
;
1614 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
1617 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1620 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
1621 acb
->ret
= bdrv_write(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1623 acb
->ret
= bdrv_read(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1626 qemu_bh_schedule(acb
->bh
);
1628 return &acb
->common
;
1631 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
1632 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1633 BlockDriverCompletionFunc
*cb
, void *opaque
)
1635 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
1638 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
1639 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1640 BlockDriverCompletionFunc
*cb
, void *opaque
)
1642 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
1645 /**************************************************************/
1646 /* sync block device emulation */
1648 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1650 *(int *)opaque
= ret
;
1653 #define NOT_DONE 0x7fffffff
1655 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1656 uint8_t *buf
, int nb_sectors
)
1659 BlockDriverAIOCB
*acb
;
1663 async_ret
= NOT_DONE
;
1664 iov
.iov_base
= (void *)buf
;
1665 iov
.iov_len
= nb_sectors
* 512;
1666 qemu_iovec_init_external(&qiov
, &iov
, 1);
1667 acb
= bdrv_aio_readv(bs
, sector_num
, &qiov
, nb_sectors
,
1668 bdrv_rw_em_cb
, &async_ret
);
1672 while (async_ret
== NOT_DONE
) {
1679 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1680 const uint8_t *buf
, int nb_sectors
)
1683 BlockDriverAIOCB
*acb
;
1687 async_ret
= NOT_DONE
;
1688 iov
.iov_base
= (void *)buf
;
1689 iov
.iov_len
= nb_sectors
* 512;
1690 qemu_iovec_init_external(&qiov
, &iov
, 1);
1691 acb
= bdrv_aio_writev(bs
, sector_num
, &qiov
, nb_sectors
,
1692 bdrv_rw_em_cb
, &async_ret
);
1695 while (async_ret
== NOT_DONE
) {
1701 void bdrv_init(void)
1703 module_call_init(MODULE_INIT_BLOCK
);
1706 void *qemu_aio_get(AIOPool
*pool
, BlockDriverState
*bs
,
1707 BlockDriverCompletionFunc
*cb
, void *opaque
)
1709 BlockDriverAIOCB
*acb
;
1711 if (pool
->free_aiocb
) {
1712 acb
= pool
->free_aiocb
;
1713 pool
->free_aiocb
= acb
->next
;
1715 acb
= qemu_mallocz(pool
->aiocb_size
);
1720 acb
->opaque
= opaque
;
1724 void qemu_aio_release(void *p
)
1726 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1727 AIOPool
*pool
= acb
->pool
;
1728 acb
->next
= pool
->free_aiocb
;
1729 pool
->free_aiocb
= acb
;
1732 /**************************************************************/
1733 /* removable device support */
1736 * Return TRUE if the media is present
1738 int bdrv_is_inserted(BlockDriverState
*bs
)
1740 BlockDriver
*drv
= bs
->drv
;
1744 if (!drv
->bdrv_is_inserted
)
1746 ret
= drv
->bdrv_is_inserted(bs
);
1751 * Return TRUE if the media changed since the last call to this
1752 * function. It is currently only used for floppy disks
1754 int bdrv_media_changed(BlockDriverState
*bs
)
1756 BlockDriver
*drv
= bs
->drv
;
1759 if (!drv
|| !drv
->bdrv_media_changed
)
1762 ret
= drv
->bdrv_media_changed(bs
);
1763 if (ret
== -ENOTSUP
)
1764 ret
= bs
->media_changed
;
1765 bs
->media_changed
= 0;
1770 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1772 int bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1774 BlockDriver
*drv
= bs
->drv
;
1781 if (!drv
|| !drv
->bdrv_eject
) {
1784 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1786 if (ret
== -ENOTSUP
) {
1795 int bdrv_is_locked(BlockDriverState
*bs
)
1801 * Lock or unlock the media (if it is locked, the user won't be able
1802 * to eject it manually).
1804 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1806 BlockDriver
*drv
= bs
->drv
;
1808 bs
->locked
= locked
;
1809 if (drv
&& drv
->bdrv_set_locked
) {
1810 drv
->bdrv_set_locked(bs
, locked
);
1814 /* needed for generic scsi interface */
1816 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1818 BlockDriver
*drv
= bs
->drv
;
1820 if (drv
&& drv
->bdrv_ioctl
)
1821 return drv
->bdrv_ioctl(bs
, req
, buf
);
1825 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
1826 unsigned long int req
, void *buf
,
1827 BlockDriverCompletionFunc
*cb
, void *opaque
)
1829 BlockDriver
*drv
= bs
->drv
;
1831 if (drv
&& drv
->bdrv_aio_ioctl
)
1832 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);
1836 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
1838 return qemu_memalign((bs
&& bs
->buffer_alignment
) ? bs
->buffer_alignment
: 512, size
);