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 BlockDriverAIOCB
*bdrv_aio_flush_em(BlockDriverState
*bs
,
58 BlockDriverCompletionFunc
*cb
, void *opaque
);
59 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
60 uint8_t *buf
, int nb_sectors
);
61 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
62 const uint8_t *buf
, int nb_sectors
);
64 BlockDriverState
*bdrv_first
;
66 static BlockDriver
*first_drv
;
68 int path_is_absolute(const char *path
)
72 /* specific case for names like: "\\.\d:" */
73 if (*path
== '/' || *path
== '\\')
76 p
= strchr(path
, ':');
82 return (*p
== '/' || *p
== '\\');
88 /* if filename is absolute, just copy it to dest. Otherwise, build a
89 path to it by considering it is relative to base_path. URL are
91 void path_combine(char *dest
, int dest_size
,
92 const char *base_path
,
100 if (path_is_absolute(filename
)) {
101 pstrcpy(dest
, dest_size
, filename
);
103 p
= strchr(base_path
, ':');
108 p1
= strrchr(base_path
, '/');
112 p2
= strrchr(base_path
, '\\');
124 if (len
> dest_size
- 1)
126 memcpy(dest
, base_path
, len
);
128 pstrcat(dest
, dest_size
, filename
);
132 void bdrv_register(BlockDriver
*bdrv
)
134 if (!bdrv
->bdrv_aio_readv
) {
135 /* add AIO emulation layer */
136 bdrv
->bdrv_aio_readv
= bdrv_aio_readv_em
;
137 bdrv
->bdrv_aio_writev
= bdrv_aio_writev_em
;
138 } else if (!bdrv
->bdrv_read
) {
139 /* add synchronous IO emulation layer */
140 bdrv
->bdrv_read
= bdrv_read_em
;
141 bdrv
->bdrv_write
= bdrv_write_em
;
144 if (!bdrv
->bdrv_aio_flush
)
145 bdrv
->bdrv_aio_flush
= bdrv_aio_flush_em
;
147 bdrv
->next
= first_drv
;
151 /* create a new block device (by default it is empty) */
152 BlockDriverState
*bdrv_new(const char *device_name
)
154 BlockDriverState
**pbs
, *bs
;
156 bs
= qemu_mallocz(sizeof(BlockDriverState
));
157 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
158 if (device_name
[0] != '\0') {
159 /* insert at the end */
168 BlockDriver
*bdrv_find_format(const char *format_name
)
171 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
172 if (!strcmp(drv1
->format_name
, format_name
))
178 int bdrv_create(BlockDriver
*drv
, const char* filename
,
179 QEMUOptionParameter
*options
)
181 if (!drv
->bdrv_create
)
184 return drv
->bdrv_create(filename
, options
);
188 void get_tmp_filename(char *filename
, int size
)
190 char temp_dir
[MAX_PATH
];
192 GetTempPath(MAX_PATH
, temp_dir
);
193 GetTempFileName(temp_dir
, "qem", 0, filename
);
196 void get_tmp_filename(char *filename
, int size
)
200 /* XXX: race condition possible */
201 tmpdir
= getenv("TMPDIR");
204 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
205 fd
= mkstemp(filename
);
211 static int is_windows_drive_prefix(const char *filename
)
213 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
214 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
218 int is_windows_drive(const char *filename
)
220 if (is_windows_drive_prefix(filename
) &&
223 if (strstart(filename
, "\\\\.\\", NULL
) ||
224 strstart(filename
, "//./", NULL
))
230 static BlockDriver
*find_protocol(const char *filename
)
238 if (is_windows_drive(filename
) ||
239 is_windows_drive_prefix(filename
))
240 return bdrv_find_format("raw");
242 p
= strchr(filename
, ':');
244 return bdrv_find_format("raw");
246 if (len
> sizeof(protocol
) - 1)
247 len
= sizeof(protocol
) - 1;
248 memcpy(protocol
, filename
, len
);
249 protocol
[len
] = '\0';
250 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
251 if (drv1
->protocol_name
&&
252 !strcmp(drv1
->protocol_name
, protocol
))
259 * Detect host devices. By convention, /dev/cdrom[N] is always
260 * recognized as a host CDROM.
262 static BlockDriver
*find_hdev_driver(const char *filename
)
264 int score_max
= 0, score
;
265 BlockDriver
*drv
= NULL
, *d
;
267 for (d
= first_drv
; d
; d
= d
->next
) {
268 if (d
->bdrv_probe_device
) {
269 score
= d
->bdrv_probe_device(filename
);
270 if (score
> score_max
) {
280 static BlockDriver
*find_image_format(const char *filename
)
282 int ret
, score
, score_max
;
283 BlockDriver
*drv1
, *drv
;
285 BlockDriverState
*bs
;
287 drv
= find_protocol(filename
);
288 /* no need to test disk image formats for vvfat */
289 if (drv
&& strcmp(drv
->format_name
, "vvfat") == 0)
292 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
295 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
302 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
303 if (drv1
->bdrv_probe
) {
304 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
305 if (score
> score_max
) {
314 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
316 BlockDriverState
*bs
;
320 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
330 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
332 return bdrv_open2(bs
, filename
, flags
, NULL
);
335 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
339 char tmp_filename
[PATH_MAX
];
340 char backing_filename
[PATH_MAX
];
343 bs
->is_temporary
= 0;
346 /* buffer_alignment defaulted to 512, drivers can change this value */
347 bs
->buffer_alignment
= 512;
349 if (flags
& BDRV_O_SNAPSHOT
) {
350 BlockDriverState
*bs1
;
353 BlockDriver
*bdrv_qcow2
;
354 QEMUOptionParameter
*options
;
356 /* if snapshot, we create a temporary backing file and open it
357 instead of opening 'filename' directly */
359 /* if there is a backing file, use it */
361 ret
= bdrv_open2(bs1
, filename
, 0, drv
);
366 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
368 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
373 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
375 /* Real path is meaningless for protocols */
377 snprintf(backing_filename
, sizeof(backing_filename
),
380 realpath(filename
, backing_filename
);
382 bdrv_qcow2
= bdrv_find_format("qcow2");
383 options
= parse_option_parameters("", bdrv_qcow2
->create_options
, NULL
);
385 set_option_parameter_int(options
, BLOCK_OPT_SIZE
, total_size
* 512);
386 set_option_parameter(options
, BLOCK_OPT_BACKING_FILE
, backing_filename
);
388 set_option_parameter(options
, BLOCK_OPT_BACKING_FMT
,
392 ret
= bdrv_create(bdrv_qcow2
, tmp_filename
, options
);
397 filename
= tmp_filename
;
399 bs
->is_temporary
= 1;
402 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
403 if (flags
& BDRV_O_FILE
) {
404 drv
= find_protocol(filename
);
406 drv
= find_hdev_driver(filename
);
408 drv
= find_image_format(filename
);
413 goto unlink_and_fail
;
416 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
419 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
420 * write cache to the guest. We do need the fdatasync to flush
421 * out transactions for block allocations, and we maybe have a
422 * volatile write cache in our backing device to deal with.
424 if (flags
& (BDRV_O_CACHE_WB
|BDRV_O_NOCACHE
))
425 bs
->enable_write_cache
= 1;
427 /* Note: for compatibility, we open disk image files as RDWR, and
428 RDONLY as fallback */
429 if (!(flags
& BDRV_O_FILE
))
430 open_flags
= BDRV_O_RDWR
|
431 (flags
& (BDRV_O_CACHE_MASK
|BDRV_O_NATIVE_AIO
));
433 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
434 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
435 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
436 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
440 qemu_free(bs
->opaque
);
444 if (bs
->is_temporary
)
448 if (drv
->bdrv_getlength
) {
449 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
452 if (bs
->is_temporary
) {
456 if (bs
->backing_file
[0] != '\0') {
457 /* if there is a backing file, use it */
458 BlockDriver
*back_drv
= NULL
;
459 bs
->backing_hd
= bdrv_new("");
460 path_combine(backing_filename
, sizeof(backing_filename
),
461 filename
, bs
->backing_file
);
462 if (bs
->backing_format
[0] != '\0')
463 back_drv
= bdrv_find_format(bs
->backing_format
);
464 ret
= bdrv_open2(bs
->backing_hd
, backing_filename
, open_flags
,
472 if (!bdrv_key_required(bs
)) {
473 /* call the change callback */
474 bs
->media_changed
= 1;
476 bs
->change_cb(bs
->change_opaque
);
481 void bdrv_close(BlockDriverState
*bs
)
485 bdrv_delete(bs
->backing_hd
);
486 bs
->drv
->bdrv_close(bs
);
487 qemu_free(bs
->opaque
);
489 if (bs
->is_temporary
) {
490 unlink(bs
->filename
);
496 /* call the change callback */
497 bs
->media_changed
= 1;
499 bs
->change_cb(bs
->change_opaque
);
503 void bdrv_delete(BlockDriverState
*bs
)
505 BlockDriverState
**pbs
;
508 while (*pbs
!= bs
&& *pbs
!= NULL
)
518 * Run consistency checks on an image
520 * Returns the number of errors or -errno when an internal error occurs
522 int bdrv_check(BlockDriverState
*bs
)
524 if (bs
->drv
->bdrv_check
== NULL
) {
528 return bs
->drv
->bdrv_check(bs
);
531 /* commit COW file into the raw image */
532 int bdrv_commit(BlockDriverState
*bs
)
534 BlockDriver
*drv
= bs
->drv
;
535 int64_t i
, total_sectors
;
537 unsigned char sector
[512];
546 if (!bs
->backing_hd
) {
550 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
551 for (i
= 0; i
< total_sectors
;) {
552 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
553 for(j
= 0; j
< n
; j
++) {
554 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
558 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
568 if (drv
->bdrv_make_empty
)
569 return drv
->bdrv_make_empty(bs
);
574 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
579 if (!bdrv_is_inserted(bs
))
585 len
= bdrv_getlength(bs
);
590 if ((offset
> len
) || (len
- offset
< size
))
596 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
599 return bdrv_check_byte_request(bs
, sector_num
* 512, nb_sectors
* 512);
602 /* return < 0 if error. See bdrv_write() for the return codes */
603 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
604 uint8_t *buf
, int nb_sectors
)
606 BlockDriver
*drv
= bs
->drv
;
610 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
613 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
616 /* Return < 0 if error. Important errors are:
617 -EIO generic I/O error (may happen for all errors)
618 -ENOMEDIUM No media inserted.
619 -EINVAL Invalid sector number or nb_sectors
620 -EACCES Trying to write a read-only device
622 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
623 const uint8_t *buf
, int nb_sectors
)
625 BlockDriver
*drv
= bs
->drv
;
630 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
633 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
636 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
637 void *buf
, int count1
)
639 uint8_t tmp_buf
[SECTOR_SIZE
];
640 int len
, nb_sectors
, count
;
644 /* first read to align to sector start */
645 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
648 sector_num
= offset
>> SECTOR_BITS
;
650 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
652 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
660 /* read the sectors "in place" */
661 nb_sectors
= count
>> SECTOR_BITS
;
662 if (nb_sectors
> 0) {
663 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
665 sector_num
+= nb_sectors
;
666 len
= nb_sectors
<< SECTOR_BITS
;
671 /* add data from the last sector */
673 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
675 memcpy(buf
, tmp_buf
, count
);
680 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
681 const void *buf
, int count1
)
683 uint8_t tmp_buf
[SECTOR_SIZE
];
684 int len
, nb_sectors
, count
;
688 /* first write to align to sector start */
689 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
692 sector_num
= offset
>> SECTOR_BITS
;
694 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
696 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
697 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
706 /* write the sectors "in place" */
707 nb_sectors
= count
>> SECTOR_BITS
;
708 if (nb_sectors
> 0) {
709 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
711 sector_num
+= nb_sectors
;
712 len
= nb_sectors
<< SECTOR_BITS
;
717 /* add data from the last sector */
719 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
721 memcpy(tmp_buf
, buf
, count
);
722 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
729 * Truncate file to 'offset' bytes (needed only for file protocols)
731 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
733 BlockDriver
*drv
= bs
->drv
;
736 if (!drv
->bdrv_truncate
)
738 return drv
->bdrv_truncate(bs
, offset
);
742 * Length of a file in bytes. Return < 0 if error or unknown.
744 int64_t bdrv_getlength(BlockDriverState
*bs
)
746 BlockDriver
*drv
= bs
->drv
;
749 if (!drv
->bdrv_getlength
) {
751 return bs
->total_sectors
* SECTOR_SIZE
;
753 return drv
->bdrv_getlength(bs
);
756 /* return 0 as number of sectors if no device present or error */
757 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
760 length
= bdrv_getlength(bs
);
764 length
= length
>> SECTOR_BITS
;
765 *nb_sectors_ptr
= length
;
769 uint8_t boot_ind
; /* 0x80 - active */
770 uint8_t head
; /* starting head */
771 uint8_t sector
; /* starting sector */
772 uint8_t cyl
; /* starting cylinder */
773 uint8_t sys_ind
; /* What partition type */
774 uint8_t end_head
; /* end head */
775 uint8_t end_sector
; /* end sector */
776 uint8_t end_cyl
; /* end cylinder */
777 uint32_t start_sect
; /* starting sector counting from 0 */
778 uint32_t nr_sects
; /* nr of sectors in partition */
779 } __attribute__((packed
));
781 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
782 static int guess_disk_lchs(BlockDriverState
*bs
,
783 int *pcylinders
, int *pheads
, int *psectors
)
786 int ret
, i
, heads
, sectors
, cylinders
;
791 bdrv_get_geometry(bs
, &nb_sectors
);
793 ret
= bdrv_read(bs
, 0, buf
, 1);
796 /* test msdos magic */
797 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
799 for(i
= 0; i
< 4; i
++) {
800 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
801 nr_sects
= le32_to_cpu(p
->nr_sects
);
802 if (nr_sects
&& p
->end_head
) {
803 /* We make the assumption that the partition terminates on
804 a cylinder boundary */
805 heads
= p
->end_head
+ 1;
806 sectors
= p
->end_sector
& 63;
809 cylinders
= nb_sectors
/ (heads
* sectors
);
810 if (cylinders
< 1 || cylinders
> 16383)
814 *pcylinders
= cylinders
;
816 printf("guessed geometry: LCHS=%d %d %d\n",
817 cylinders
, heads
, sectors
);
825 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
827 int translation
, lba_detected
= 0;
828 int cylinders
, heads
, secs
;
831 /* if a geometry hint is available, use it */
832 bdrv_get_geometry(bs
, &nb_sectors
);
833 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
834 translation
= bdrv_get_translation_hint(bs
);
835 if (cylinders
!= 0) {
840 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
842 /* if heads > 16, it means that a BIOS LBA
843 translation was active, so the default
844 hardware geometry is OK */
846 goto default_geometry
;
851 /* disable any translation to be in sync with
852 the logical geometry */
853 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
854 bdrv_set_translation_hint(bs
,
855 BIOS_ATA_TRANSLATION_NONE
);
860 /* if no geometry, use a standard physical disk geometry */
861 cylinders
= nb_sectors
/ (16 * 63);
863 if (cylinders
> 16383)
865 else if (cylinders
< 2)
870 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
871 if ((*pcyls
* *pheads
) <= 131072) {
872 bdrv_set_translation_hint(bs
,
873 BIOS_ATA_TRANSLATION_LARGE
);
875 bdrv_set_translation_hint(bs
,
876 BIOS_ATA_TRANSLATION_LBA
);
880 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
884 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
885 int cyls
, int heads
, int secs
)
892 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
895 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
896 type
== BDRV_TYPE_FLOPPY
));
899 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
901 bs
->translation
= translation
;
904 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
905 int *pcyls
, int *pheads
, int *psecs
)
912 int bdrv_get_type_hint(BlockDriverState
*bs
)
917 int bdrv_get_translation_hint(BlockDriverState
*bs
)
919 return bs
->translation
;
922 int bdrv_is_removable(BlockDriverState
*bs
)
924 return bs
->removable
;
927 int bdrv_is_read_only(BlockDriverState
*bs
)
929 return bs
->read_only
;
932 int bdrv_is_sg(BlockDriverState
*bs
)
937 int bdrv_enable_write_cache(BlockDriverState
*bs
)
939 return bs
->enable_write_cache
;
942 /* XXX: no longer used */
943 void bdrv_set_change_cb(BlockDriverState
*bs
,
944 void (*change_cb
)(void *opaque
), void *opaque
)
946 bs
->change_cb
= change_cb
;
947 bs
->change_opaque
= opaque
;
950 int bdrv_is_encrypted(BlockDriverState
*bs
)
952 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
954 return bs
->encrypted
;
957 int bdrv_key_required(BlockDriverState
*bs
)
959 BlockDriverState
*backing_hd
= bs
->backing_hd
;
961 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
963 return (bs
->encrypted
&& !bs
->valid_key
);
966 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
969 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
970 ret
= bdrv_set_key(bs
->backing_hd
, key
);
976 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
978 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
981 } else if (!bs
->valid_key
) {
983 /* call the change callback now, we skipped it on open */
984 bs
->media_changed
= 1;
986 bs
->change_cb(bs
->change_opaque
);
991 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
996 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
1000 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
1005 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
1006 it(opaque
, drv
->format_name
);
1010 BlockDriverState
*bdrv_find(const char *name
)
1012 BlockDriverState
*bs
;
1014 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1015 if (!strcmp(name
, bs
->device_name
))
1021 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1023 BlockDriverState
*bs
;
1025 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1030 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1032 return bs
->device_name
;
1035 void bdrv_flush(BlockDriverState
*bs
)
1039 if (bs
->drv
->bdrv_flush
)
1040 bs
->drv
->bdrv_flush(bs
);
1042 bdrv_flush(bs
->backing_hd
);
1045 void bdrv_flush_all(void)
1047 BlockDriverState
*bs
;
1049 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1050 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1051 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1056 * Returns true iff the specified sector is present in the disk image. Drivers
1057 * not implementing the functionality are assumed to not support backing files,
1058 * hence all their sectors are reported as allocated.
1060 * 'pnum' is set to the number of sectors (including and immediately following
1061 * the specified sector) that are known to be in the same
1062 * allocated/unallocated state.
1064 * 'nb_sectors' is the max value 'pnum' should be set to.
1066 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1070 if (!bs
->drv
->bdrv_is_allocated
) {
1071 if (sector_num
>= bs
->total_sectors
) {
1075 n
= bs
->total_sectors
- sector_num
;
1076 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1079 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1082 void bdrv_info(Monitor
*mon
)
1084 BlockDriverState
*bs
;
1086 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1087 monitor_printf(mon
, "%s:", bs
->device_name
);
1088 monitor_printf(mon
, " type=");
1091 monitor_printf(mon
, "hd");
1093 case BDRV_TYPE_CDROM
:
1094 monitor_printf(mon
, "cdrom");
1096 case BDRV_TYPE_FLOPPY
:
1097 monitor_printf(mon
, "floppy");
1100 monitor_printf(mon
, " removable=%d", bs
->removable
);
1101 if (bs
->removable
) {
1102 monitor_printf(mon
, " locked=%d", bs
->locked
);
1105 monitor_printf(mon
, " file=");
1106 monitor_print_filename(mon
, bs
->filename
);
1107 if (bs
->backing_file
[0] != '\0') {
1108 monitor_printf(mon
, " backing_file=");
1109 monitor_print_filename(mon
, bs
->backing_file
);
1111 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1112 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1113 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1115 monitor_printf(mon
, " [not inserted]");
1117 monitor_printf(mon
, "\n");
1121 /* The "info blockstats" command. */
1122 void bdrv_info_stats(Monitor
*mon
)
1124 BlockDriverState
*bs
;
1126 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1127 monitor_printf(mon
, "%s:"
1128 " rd_bytes=%" PRIu64
1129 " wr_bytes=%" PRIu64
1130 " rd_operations=%" PRIu64
1131 " wr_operations=%" PRIu64
1134 bs
->rd_bytes
, bs
->wr_bytes
,
1135 bs
->rd_ops
, bs
->wr_ops
);
1139 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1141 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1142 return bs
->backing_file
;
1143 else if (bs
->encrypted
)
1144 return bs
->filename
;
1149 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1150 char *filename
, int filename_size
)
1152 if (!bs
->backing_hd
) {
1153 pstrcpy(filename
, filename_size
, "");
1155 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1159 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1160 const uint8_t *buf
, int nb_sectors
)
1162 BlockDriver
*drv
= bs
->drv
;
1165 if (!drv
->bdrv_write_compressed
)
1167 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1169 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1172 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1174 BlockDriver
*drv
= bs
->drv
;
1177 if (!drv
->bdrv_get_info
)
1179 memset(bdi
, 0, sizeof(*bdi
));
1180 return drv
->bdrv_get_info(bs
, bdi
);
1183 int bdrv_save_vmstate(BlockDriverState
*bs
, const uint8_t *buf
,
1184 int64_t pos
, int size
)
1186 BlockDriver
*drv
= bs
->drv
;
1189 if (!drv
->bdrv_save_vmstate
)
1191 return drv
->bdrv_save_vmstate(bs
, buf
, pos
, size
);
1194 int bdrv_load_vmstate(BlockDriverState
*bs
, uint8_t *buf
,
1195 int64_t pos
, int size
)
1197 BlockDriver
*drv
= bs
->drv
;
1200 if (!drv
->bdrv_load_vmstate
)
1202 return drv
->bdrv_load_vmstate(bs
, buf
, pos
, size
);
1205 /**************************************************************/
1206 /* handling of snapshots */
1208 int bdrv_snapshot_create(BlockDriverState
*bs
,
1209 QEMUSnapshotInfo
*sn_info
)
1211 BlockDriver
*drv
= bs
->drv
;
1214 if (!drv
->bdrv_snapshot_create
)
1216 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1219 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1220 const char *snapshot_id
)
1222 BlockDriver
*drv
= bs
->drv
;
1225 if (!drv
->bdrv_snapshot_goto
)
1227 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1230 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1232 BlockDriver
*drv
= bs
->drv
;
1235 if (!drv
->bdrv_snapshot_delete
)
1237 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1240 int bdrv_snapshot_list(BlockDriverState
*bs
,
1241 QEMUSnapshotInfo
**psn_info
)
1243 BlockDriver
*drv
= bs
->drv
;
1246 if (!drv
->bdrv_snapshot_list
)
1248 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1251 #define NB_SUFFIXES 4
1253 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1255 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1260 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1263 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1264 if (size
< (10 * base
)) {
1265 snprintf(buf
, buf_size
, "%0.1f%c",
1266 (double)size
/ base
,
1269 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1270 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1271 ((size
+ (base
>> 1)) / base
),
1281 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1283 char buf1
[128], date_buf
[128], clock_buf
[128];
1293 snprintf(buf
, buf_size
,
1294 "%-10s%-20s%7s%20s%15s",
1295 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1299 ptm
= localtime(&ti
);
1300 strftime(date_buf
, sizeof(date_buf
),
1301 "%Y-%m-%d %H:%M:%S", ptm
);
1303 localtime_r(&ti
, &tm
);
1304 strftime(date_buf
, sizeof(date_buf
),
1305 "%Y-%m-%d %H:%M:%S", &tm
);
1307 secs
= sn
->vm_clock_nsec
/ 1000000000;
1308 snprintf(clock_buf
, sizeof(clock_buf
),
1309 "%02d:%02d:%02d.%03d",
1311 (int)((secs
/ 60) % 60),
1313 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1314 snprintf(buf
, buf_size
,
1315 "%-10s%-20s%7s%20s%15s",
1316 sn
->id_str
, sn
->name
,
1317 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1325 /**************************************************************/
1328 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1329 QEMUIOVector
*qiov
, int nb_sectors
,
1330 BlockDriverCompletionFunc
*cb
, void *opaque
)
1332 BlockDriver
*drv
= bs
->drv
;
1333 BlockDriverAIOCB
*ret
;
1337 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1340 ret
= drv
->bdrv_aio_readv(bs
, sector_num
, qiov
, nb_sectors
,
1344 /* Update stats even though technically transfer has not happened. */
1345 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1352 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1353 QEMUIOVector
*qiov
, int nb_sectors
,
1354 BlockDriverCompletionFunc
*cb
, void *opaque
)
1356 BlockDriver
*drv
= bs
->drv
;
1357 BlockDriverAIOCB
*ret
;
1363 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1366 ret
= drv
->bdrv_aio_writev(bs
, sector_num
, qiov
, nb_sectors
,
1370 /* Update stats even though technically transfer has not happened. */
1371 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1379 typedef struct MultiwriteCB
{
1384 BlockDriverCompletionFunc
*cb
;
1386 QEMUIOVector
*free_qiov
;
1391 static void multiwrite_user_cb(MultiwriteCB
*mcb
)
1395 for (i
= 0; i
< mcb
->num_callbacks
; i
++) {
1396 mcb
->callbacks
[i
].cb(mcb
->callbacks
[i
].opaque
, mcb
->error
);
1397 qemu_free(mcb
->callbacks
[i
].free_qiov
);
1398 qemu_free(mcb
->callbacks
[i
].free_buf
);
1402 static void multiwrite_cb(void *opaque
, int ret
)
1404 MultiwriteCB
*mcb
= opaque
;
1408 multiwrite_user_cb(mcb
);
1411 mcb
->num_requests
--;
1412 if (mcb
->num_requests
== 0) {
1413 if (mcb
->error
== 0) {
1414 multiwrite_user_cb(mcb
);
1420 static int multiwrite_req_compare(const void *a
, const void *b
)
1422 return (((BlockRequest
*) a
)->sector
- ((BlockRequest
*) b
)->sector
);
1426 * Takes a bunch of requests and tries to merge them. Returns the number of
1427 * requests that remain after merging.
1429 static int multiwrite_merge(BlockDriverState
*bs
, BlockRequest
*reqs
,
1430 int num_reqs
, MultiwriteCB
*mcb
)
1434 // Sort requests by start sector
1435 qsort(reqs
, num_reqs
, sizeof(*reqs
), &multiwrite_req_compare
);
1437 // Check if adjacent requests touch the same clusters. If so, combine them,
1438 // filling up gaps with zero sectors.
1440 for (i
= 1; i
< num_reqs
; i
++) {
1442 int64_t oldreq_last
= reqs
[outidx
].sector
+ reqs
[outidx
].nb_sectors
;
1444 // This handles the cases that are valid for all block drivers, namely
1445 // exactly sequential writes and overlapping writes.
1446 if (reqs
[i
].sector
<= oldreq_last
) {
1450 // The block driver may decide that it makes sense to combine requests
1451 // even if there is a gap of some sectors between them. In this case,
1452 // the gap is filled with zeros (therefore only applicable for yet
1453 // unused space in format like qcow2).
1454 if (!merge
&& bs
->drv
->bdrv_merge_requests
) {
1455 merge
= bs
->drv
->bdrv_merge_requests(bs
, &reqs
[outidx
], &reqs
[i
]);
1460 QEMUIOVector
*qiov
= qemu_mallocz(sizeof(*qiov
));
1461 qemu_iovec_init(qiov
,
1462 reqs
[outidx
].qiov
->niov
+ reqs
[i
].qiov
->niov
+ 1);
1464 // Add the first request to the merged one. If the requests are
1465 // overlapping, drop the last sectors of the first request.
1466 size
= (reqs
[i
].sector
- reqs
[outidx
].sector
) << 9;
1467 qemu_iovec_concat(qiov
, reqs
[outidx
].qiov
, size
);
1469 // We might need to add some zeros between the two requests
1470 if (reqs
[i
].sector
> oldreq_last
) {
1471 size_t zero_bytes
= (reqs
[i
].sector
- oldreq_last
) << 9;
1472 uint8_t *buf
= qemu_blockalign(bs
, zero_bytes
);
1473 memset(buf
, 0, zero_bytes
);
1474 qemu_iovec_add(qiov
, buf
, zero_bytes
);
1475 mcb
->callbacks
[i
].free_buf
= buf
;
1478 // Add the second request
1479 qemu_iovec_concat(qiov
, reqs
[i
].qiov
, reqs
[i
].qiov
->size
);
1481 reqs
[outidx
].nb_sectors
+= reqs
[i
].nb_sectors
;
1482 reqs
[outidx
].qiov
= qiov
;
1484 mcb
->callbacks
[i
].free_qiov
= reqs
[outidx
].qiov
;
1487 reqs
[outidx
].sector
= reqs
[i
].sector
;
1488 reqs
[outidx
].nb_sectors
= reqs
[i
].nb_sectors
;
1489 reqs
[outidx
].qiov
= reqs
[i
].qiov
;
1497 * Submit multiple AIO write requests at once.
1499 * On success, the function returns 0 and all requests in the reqs array have
1500 * been submitted. In error case this function returns -1, and any of the
1501 * requests may or may not be submitted yet. In particular, this means that the
1502 * callback will be called for some of the requests, for others it won't. The
1503 * caller must check the error field of the BlockRequest to wait for the right
1504 * callbacks (if error != 0, no callback will be called).
1506 * The implementation may modify the contents of the reqs array, e.g. to merge
1507 * requests. However, the fields opaque and error are left unmodified as they
1508 * are used to signal failure for a single request to the caller.
1510 int bdrv_aio_multiwrite(BlockDriverState
*bs
, BlockRequest
*reqs
, int num_reqs
)
1512 BlockDriverAIOCB
*acb
;
1516 if (num_reqs
== 0) {
1520 // Create MultiwriteCB structure
1521 mcb
= qemu_mallocz(sizeof(*mcb
) + num_reqs
* sizeof(*mcb
->callbacks
));
1522 mcb
->num_requests
= 0;
1523 mcb
->num_callbacks
= num_reqs
;
1525 for (i
= 0; i
< num_reqs
; i
++) {
1526 mcb
->callbacks
[i
].cb
= reqs
[i
].cb
;
1527 mcb
->callbacks
[i
].opaque
= reqs
[i
].opaque
;
1530 // Check for mergable requests
1531 num_reqs
= multiwrite_merge(bs
, reqs
, num_reqs
, mcb
);
1533 // Run the aio requests
1534 for (i
= 0; i
< num_reqs
; i
++) {
1535 acb
= bdrv_aio_writev(bs
, reqs
[i
].sector
, reqs
[i
].qiov
,
1536 reqs
[i
].nb_sectors
, multiwrite_cb
, mcb
);
1539 // We can only fail the whole thing if no request has been
1540 // submitted yet. Otherwise we'll wait for the submitted AIOs to
1541 // complete and report the error in the callback.
1542 if (mcb
->num_requests
== 0) {
1543 reqs
[i
].error
= EIO
;
1550 mcb
->num_requests
++;
1561 BlockDriverAIOCB
*bdrv_aio_flush(BlockDriverState
*bs
,
1562 BlockDriverCompletionFunc
*cb
, void *opaque
)
1564 BlockDriver
*drv
= bs
->drv
;
1570 * Note that unlike bdrv_flush the driver is reponsible for flushing a
1571 * backing image if it exists.
1573 return drv
->bdrv_aio_flush(bs
, cb
, opaque
);
1576 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1578 acb
->pool
->cancel(acb
);
1582 /**************************************************************/
1583 /* async block device emulation */
1585 typedef struct BlockDriverAIOCBSync
{
1586 BlockDriverAIOCB common
;
1589 /* vector translation state */
1593 } BlockDriverAIOCBSync
;
1595 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1597 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1598 qemu_bh_delete(acb
->bh
);
1600 qemu_aio_release(acb
);
1603 static AIOPool bdrv_em_aio_pool
= {
1604 .aiocb_size
= sizeof(BlockDriverAIOCBSync
),
1605 .cancel
= bdrv_aio_cancel_em
,
1608 static void bdrv_aio_bh_cb(void *opaque
)
1610 BlockDriverAIOCBSync
*acb
= opaque
;
1613 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
1614 qemu_vfree(acb
->bounce
);
1615 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1616 qemu_bh_delete(acb
->bh
);
1618 qemu_aio_release(acb
);
1621 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1625 BlockDriverCompletionFunc
*cb
,
1630 BlockDriverAIOCBSync
*acb
;
1632 acb
= qemu_aio_get(&bdrv_em_aio_pool
, bs
, cb
, opaque
);
1633 acb
->is_write
= is_write
;
1635 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
1638 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1641 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
1642 acb
->ret
= bdrv_write(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1644 acb
->ret
= bdrv_read(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1647 qemu_bh_schedule(acb
->bh
);
1649 return &acb
->common
;
1652 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
1653 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1654 BlockDriverCompletionFunc
*cb
, void *opaque
)
1656 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
1659 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
1660 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1661 BlockDriverCompletionFunc
*cb
, void *opaque
)
1663 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
1666 static BlockDriverAIOCB
*bdrv_aio_flush_em(BlockDriverState
*bs
,
1667 BlockDriverCompletionFunc
*cb
, void *opaque
)
1669 BlockDriverAIOCBSync
*acb
;
1671 acb
= qemu_aio_get(&bdrv_em_aio_pool
, bs
, cb
, opaque
);
1672 acb
->is_write
= 1; /* don't bounce in the completion hadler */
1678 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1681 qemu_bh_schedule(acb
->bh
);
1682 return &acb
->common
;
1685 /**************************************************************/
1686 /* sync block device emulation */
1688 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1690 *(int *)opaque
= ret
;
1693 #define NOT_DONE 0x7fffffff
1695 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1696 uint8_t *buf
, int nb_sectors
)
1699 BlockDriverAIOCB
*acb
;
1703 async_ret
= NOT_DONE
;
1704 iov
.iov_base
= (void *)buf
;
1705 iov
.iov_len
= nb_sectors
* 512;
1706 qemu_iovec_init_external(&qiov
, &iov
, 1);
1707 acb
= bdrv_aio_readv(bs
, sector_num
, &qiov
, nb_sectors
,
1708 bdrv_rw_em_cb
, &async_ret
);
1712 while (async_ret
== NOT_DONE
) {
1719 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1720 const uint8_t *buf
, int nb_sectors
)
1723 BlockDriverAIOCB
*acb
;
1727 async_ret
= NOT_DONE
;
1728 iov
.iov_base
= (void *)buf
;
1729 iov
.iov_len
= nb_sectors
* 512;
1730 qemu_iovec_init_external(&qiov
, &iov
, 1);
1731 acb
= bdrv_aio_writev(bs
, sector_num
, &qiov
, nb_sectors
,
1732 bdrv_rw_em_cb
, &async_ret
);
1735 while (async_ret
== NOT_DONE
) {
1741 void bdrv_init(void)
1743 module_call_init(MODULE_INIT_BLOCK
);
1746 void *qemu_aio_get(AIOPool
*pool
, BlockDriverState
*bs
,
1747 BlockDriverCompletionFunc
*cb
, void *opaque
)
1749 BlockDriverAIOCB
*acb
;
1751 if (pool
->free_aiocb
) {
1752 acb
= pool
->free_aiocb
;
1753 pool
->free_aiocb
= acb
->next
;
1755 acb
= qemu_mallocz(pool
->aiocb_size
);
1760 acb
->opaque
= opaque
;
1764 void qemu_aio_release(void *p
)
1766 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1767 AIOPool
*pool
= acb
->pool
;
1768 acb
->next
= pool
->free_aiocb
;
1769 pool
->free_aiocb
= acb
;
1772 /**************************************************************/
1773 /* removable device support */
1776 * Return TRUE if the media is present
1778 int bdrv_is_inserted(BlockDriverState
*bs
)
1780 BlockDriver
*drv
= bs
->drv
;
1784 if (!drv
->bdrv_is_inserted
)
1786 ret
= drv
->bdrv_is_inserted(bs
);
1791 * Return TRUE if the media changed since the last call to this
1792 * function. It is currently only used for floppy disks
1794 int bdrv_media_changed(BlockDriverState
*bs
)
1796 BlockDriver
*drv
= bs
->drv
;
1799 if (!drv
|| !drv
->bdrv_media_changed
)
1802 ret
= drv
->bdrv_media_changed(bs
);
1803 if (ret
== -ENOTSUP
)
1804 ret
= bs
->media_changed
;
1805 bs
->media_changed
= 0;
1810 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1812 int bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1814 BlockDriver
*drv
= bs
->drv
;
1821 if (!drv
|| !drv
->bdrv_eject
) {
1824 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1826 if (ret
== -ENOTSUP
) {
1835 int bdrv_is_locked(BlockDriverState
*bs
)
1841 * Lock or unlock the media (if it is locked, the user won't be able
1842 * to eject it manually).
1844 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1846 BlockDriver
*drv
= bs
->drv
;
1848 bs
->locked
= locked
;
1849 if (drv
&& drv
->bdrv_set_locked
) {
1850 drv
->bdrv_set_locked(bs
, locked
);
1854 /* needed for generic scsi interface */
1856 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1858 BlockDriver
*drv
= bs
->drv
;
1860 if (drv
&& drv
->bdrv_ioctl
)
1861 return drv
->bdrv_ioctl(bs
, req
, buf
);
1865 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
1866 unsigned long int req
, void *buf
,
1867 BlockDriverCompletionFunc
*cb
, void *opaque
)
1869 BlockDriver
*drv
= bs
->drv
;
1871 if (drv
&& drv
->bdrv_aio_ioctl
)
1872 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);
1876 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
1878 return qemu_memalign((bs
&& bs
->buffer_alignment
) ? bs
->buffer_alignment
: 512, size
);