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>
43 #define SECTOR_SIZE (1 << SECTOR_BITS)
45 static AIOPool vectored_aio_pool
;
47 typedef struct BlockDriverAIOCBSync
{
48 BlockDriverAIOCB common
;
51 } BlockDriverAIOCBSync
;
53 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
54 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
55 BlockDriverCompletionFunc
*cb
, void *opaque
);
56 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
57 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
58 BlockDriverCompletionFunc
*cb
, void *opaque
);
59 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
60 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
61 uint8_t *buf
, int nb_sectors
);
62 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
63 const uint8_t *buf
, int nb_sectors
);
65 BlockDriverState
*bdrv_first
;
67 static BlockDriver
*first_drv
;
69 int path_is_absolute(const char *path
)
73 /* specific case for names like: "\\.\d:" */
74 if (*path
== '/' || *path
== '\\')
77 p
= strchr(path
, ':');
83 return (*p
== '/' || *p
== '\\');
89 /* if filename is absolute, just copy it to dest. Otherwise, build a
90 path to it by considering it is relative to base_path. URL are
92 void path_combine(char *dest
, int dest_size
,
93 const char *base_path
,
101 if (path_is_absolute(filename
)) {
102 pstrcpy(dest
, dest_size
, filename
);
104 p
= strchr(base_path
, ':');
109 p1
= strrchr(base_path
, '/');
113 p2
= strrchr(base_path
, '\\');
125 if (len
> dest_size
- 1)
127 memcpy(dest
, base_path
, len
);
129 pstrcat(dest
, dest_size
, filename
);
134 static void bdrv_register(BlockDriver
*bdrv
)
136 if (!bdrv
->bdrv_aio_read
) {
137 /* add AIO emulation layer */
138 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
139 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
140 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
141 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
142 } else if (!bdrv
->bdrv_read
&& !bdrv
->bdrv_pread
) {
143 /* add synchronous IO emulation layer */
144 bdrv
->bdrv_read
= bdrv_read_em
;
145 bdrv
->bdrv_write
= bdrv_write_em
;
147 aio_pool_init(&bdrv
->aio_pool
, bdrv
->aiocb_size
, bdrv
->bdrv_aio_cancel
);
148 bdrv
->next
= first_drv
;
152 /* create a new block device (by default it is empty) */
153 BlockDriverState
*bdrv_new(const char *device_name
)
155 BlockDriverState
**pbs
, *bs
;
157 bs
= qemu_mallocz(sizeof(BlockDriverState
));
158 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
159 if (device_name
[0] != '\0') {
160 /* insert at the end */
169 BlockDriver
*bdrv_find_format(const char *format_name
)
172 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
173 if (!strcmp(drv1
->format_name
, format_name
))
179 int bdrv_create(BlockDriver
*drv
,
180 const char *filename
, int64_t size_in_sectors
,
181 const char *backing_file
, int flags
)
183 if (!drv
->bdrv_create
)
185 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
189 void get_tmp_filename(char *filename
, int size
)
191 char temp_dir
[MAX_PATH
];
193 GetTempPath(MAX_PATH
, temp_dir
);
194 GetTempFileName(temp_dir
, "qem", 0, filename
);
197 void get_tmp_filename(char *filename
, int size
)
201 /* XXX: race condition possible */
202 tmpdir
= getenv("TMPDIR");
205 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
206 fd
= mkstemp(filename
);
212 static int is_windows_drive_prefix(const char *filename
)
214 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
215 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
219 static int is_windows_drive(const char *filename
)
221 if (is_windows_drive_prefix(filename
) &&
224 if (strstart(filename
, "\\\\.\\", NULL
) ||
225 strstart(filename
, "//./", NULL
))
231 static BlockDriver
*find_protocol(const char *filename
)
239 if (is_windows_drive(filename
) ||
240 is_windows_drive_prefix(filename
))
243 p
= strchr(filename
, ':');
247 if (len
> sizeof(protocol
) - 1)
248 len
= sizeof(protocol
) - 1;
249 memcpy(protocol
, filename
, len
);
250 protocol
[len
] = '\0';
251 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
252 if (drv1
->protocol_name
&&
253 !strcmp(drv1
->protocol_name
, protocol
))
259 /* XXX: force raw format if block or character device ? It would
260 simplify the BSD case */
261 static BlockDriver
*find_image_format(const char *filename
)
263 int ret
, score
, score_max
;
264 BlockDriver
*drv1
, *drv
;
266 BlockDriverState
*bs
;
268 /* detect host devices. By convention, /dev/cdrom[N] is always
269 recognized as a host CDROM */
270 if (strstart(filename
, "/dev/cdrom", NULL
))
271 return &bdrv_host_device
;
273 if (is_windows_drive(filename
))
274 return &bdrv_host_device
;
278 if (stat(filename
, &st
) >= 0 &&
279 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
280 return &bdrv_host_device
;
285 drv
= find_protocol(filename
);
286 /* no need to test disk image formats for vvfat */
287 if (drv
== &bdrv_vvfat
)
290 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
293 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
300 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
301 if (drv1
->bdrv_probe
) {
302 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
303 if (score
> score_max
) {
312 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
314 BlockDriverState
*bs
;
318 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
328 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
330 return bdrv_open2(bs
, filename
, flags
, NULL
);
333 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
337 char tmp_filename
[PATH_MAX
];
338 char backing_filename
[PATH_MAX
];
341 bs
->is_temporary
= 0;
345 if (flags
& BDRV_O_SNAPSHOT
) {
346 BlockDriverState
*bs1
;
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_open(bs1
, filename
, 0);
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 ret
= bdrv_create(&bdrv_qcow2
, tmp_filename
,
377 total_size
, backing_filename
, 0);
381 filename
= tmp_filename
;
382 bs
->is_temporary
= 1;
385 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
386 if (flags
& BDRV_O_FILE
) {
387 drv
= find_protocol(filename
);
389 drv
= find_image_format(filename
);
393 goto unlink_and_fail
;
396 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
397 /* Note: for compatibility, we open disk image files as RDWR, and
398 RDONLY as fallback */
399 if (!(flags
& BDRV_O_FILE
))
400 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
402 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
403 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
404 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
405 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
409 qemu_free(bs
->opaque
);
413 if (bs
->is_temporary
)
417 if (drv
->bdrv_getlength
) {
418 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
421 if (bs
->is_temporary
) {
425 if (bs
->backing_file
[0] != '\0') {
426 /* if there is a backing file, use it */
427 bs
->backing_hd
= bdrv_new("");
428 path_combine(backing_filename
, sizeof(backing_filename
),
429 filename
, bs
->backing_file
);
430 ret
= bdrv_open(bs
->backing_hd
, backing_filename
, open_flags
);
437 /* call the change callback */
438 bs
->media_changed
= 1;
440 bs
->change_cb(bs
->change_opaque
);
445 void bdrv_close(BlockDriverState
*bs
)
449 bdrv_delete(bs
->backing_hd
);
450 bs
->drv
->bdrv_close(bs
);
451 qemu_free(bs
->opaque
);
453 if (bs
->is_temporary
) {
454 unlink(bs
->filename
);
460 /* call the change callback */
461 bs
->media_changed
= 1;
463 bs
->change_cb(bs
->change_opaque
);
467 void bdrv_delete(BlockDriverState
*bs
)
469 BlockDriverState
**pbs
;
472 while (*pbs
!= bs
&& *pbs
!= NULL
)
481 /* commit COW file into the raw image */
482 int bdrv_commit(BlockDriverState
*bs
)
484 BlockDriver
*drv
= bs
->drv
;
485 int64_t i
, total_sectors
;
487 unsigned char sector
[512];
496 if (!bs
->backing_hd
) {
500 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
501 for (i
= 0; i
< total_sectors
;) {
502 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
503 for(j
= 0; j
< n
; j
++) {
504 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
508 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
518 if (drv
->bdrv_make_empty
)
519 return drv
->bdrv_make_empty(bs
);
524 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
529 if (!bdrv_is_inserted(bs
))
535 len
= bdrv_getlength(bs
);
537 if ((offset
+ size
) > len
)
543 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
548 /* Deal with byte accesses */
550 offset
= -sector_num
;
552 offset
= sector_num
* 512;
554 return bdrv_check_byte_request(bs
, offset
, nb_sectors
* 512);
557 /* return < 0 if error. See bdrv_write() for the return codes */
558 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
559 uint8_t *buf
, int nb_sectors
)
561 BlockDriver
*drv
= bs
->drv
;
565 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
568 if (drv
->bdrv_pread
) {
570 len
= nb_sectors
* 512;
571 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
577 bs
->rd_bytes
+= (unsigned) len
;
582 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
586 /* Return < 0 if error. Important errors are:
587 -EIO generic I/O error (may happen for all errors)
588 -ENOMEDIUM No media inserted.
589 -EINVAL Invalid sector number or nb_sectors
590 -EACCES Trying to write a read-only device
592 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
593 const uint8_t *buf
, int nb_sectors
)
595 BlockDriver
*drv
= bs
->drv
;
600 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
603 if (drv
->bdrv_pwrite
) {
604 int ret
, len
, count
= 0;
605 len
= nb_sectors
* 512;
607 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
- count
);
609 printf("bdrv_write ret=%d\n", ret
);
614 } while (count
!= len
);
615 bs
->wr_bytes
+= (unsigned) len
;
619 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
622 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
623 uint8_t *buf
, int count1
)
625 uint8_t tmp_buf
[SECTOR_SIZE
];
626 int len
, nb_sectors
, count
;
630 /* first read to align to sector start */
631 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
634 sector_num
= offset
>> SECTOR_BITS
;
636 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
638 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
646 /* read the sectors "in place" */
647 nb_sectors
= count
>> SECTOR_BITS
;
648 if (nb_sectors
> 0) {
649 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
651 sector_num
+= nb_sectors
;
652 len
= nb_sectors
<< SECTOR_BITS
;
657 /* add data from the last sector */
659 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
661 memcpy(buf
, tmp_buf
, count
);
666 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
667 const uint8_t *buf
, int count1
)
669 uint8_t tmp_buf
[SECTOR_SIZE
];
670 int len
, nb_sectors
, count
;
674 /* first write to align to sector start */
675 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
678 sector_num
= offset
>> SECTOR_BITS
;
680 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
682 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
683 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
692 /* write the sectors "in place" */
693 nb_sectors
= count
>> SECTOR_BITS
;
694 if (nb_sectors
> 0) {
695 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
697 sector_num
+= nb_sectors
;
698 len
= nb_sectors
<< SECTOR_BITS
;
703 /* add data from the last sector */
705 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
707 memcpy(tmp_buf
, buf
, count
);
708 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
715 * Read with byte offsets (needed only for file protocols)
717 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
718 void *buf1
, int count1
)
720 BlockDriver
*drv
= bs
->drv
;
724 if (bdrv_check_byte_request(bs
, offset
, count1
))
727 if (!drv
->bdrv_pread
)
728 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
729 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
733 * Write with byte offsets (needed only for file protocols)
735 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
736 const void *buf1
, int count1
)
738 BlockDriver
*drv
= bs
->drv
;
742 if (bdrv_check_byte_request(bs
, offset
, count1
))
745 if (!drv
->bdrv_pwrite
)
746 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
747 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
751 * Truncate file to 'offset' bytes (needed only for file protocols)
753 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
755 BlockDriver
*drv
= bs
->drv
;
758 if (!drv
->bdrv_truncate
)
760 return drv
->bdrv_truncate(bs
, offset
);
764 * Length of a file in bytes. Return < 0 if error or unknown.
766 int64_t bdrv_getlength(BlockDriverState
*bs
)
768 BlockDriver
*drv
= bs
->drv
;
771 if (!drv
->bdrv_getlength
) {
773 return bs
->total_sectors
* SECTOR_SIZE
;
775 return drv
->bdrv_getlength(bs
);
778 /* return 0 as number of sectors if no device present or error */
779 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
782 length
= bdrv_getlength(bs
);
786 length
= length
>> SECTOR_BITS
;
787 *nb_sectors_ptr
= length
;
791 uint8_t boot_ind
; /* 0x80 - active */
792 uint8_t head
; /* starting head */
793 uint8_t sector
; /* starting sector */
794 uint8_t cyl
; /* starting cylinder */
795 uint8_t sys_ind
; /* What partition type */
796 uint8_t end_head
; /* end head */
797 uint8_t end_sector
; /* end sector */
798 uint8_t end_cyl
; /* end cylinder */
799 uint32_t start_sect
; /* starting sector counting from 0 */
800 uint32_t nr_sects
; /* nr of sectors in partition */
801 } __attribute__((packed
));
803 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
804 static int guess_disk_lchs(BlockDriverState
*bs
,
805 int *pcylinders
, int *pheads
, int *psectors
)
808 int ret
, i
, heads
, sectors
, cylinders
;
813 buf
= qemu_memalign(512, 512);
817 bdrv_get_geometry(bs
, &nb_sectors
);
819 ret
= bdrv_read(bs
, 0, buf
, 1);
822 /* test msdos magic */
823 if (buf
[510] != 0x55 || buf
[511] != 0xaa) {
827 for(i
= 0; i
< 4; i
++) {
828 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
829 nr_sects
= le32_to_cpu(p
->nr_sects
);
830 if (nr_sects
&& p
->end_head
) {
831 /* We make the assumption that the partition terminates on
832 a cylinder boundary */
833 heads
= p
->end_head
+ 1;
834 sectors
= p
->end_sector
& 63;
837 cylinders
= nb_sectors
/ (heads
* sectors
);
838 if (cylinders
< 1 || cylinders
> 16383)
842 *pcylinders
= cylinders
;
844 printf("guessed geometry: LCHS=%d %d %d\n",
845 cylinders
, heads
, sectors
);
855 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
857 int translation
, lba_detected
= 0;
858 int cylinders
, heads
, secs
;
861 /* if a geometry hint is available, use it */
862 bdrv_get_geometry(bs
, &nb_sectors
);
863 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
864 translation
= bdrv_get_translation_hint(bs
);
865 if (cylinders
!= 0) {
870 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
872 /* if heads > 16, it means that a BIOS LBA
873 translation was active, so the default
874 hardware geometry is OK */
876 goto default_geometry
;
881 /* disable any translation to be in sync with
882 the logical geometry */
883 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
884 bdrv_set_translation_hint(bs
,
885 BIOS_ATA_TRANSLATION_NONE
);
890 /* if no geometry, use a standard physical disk geometry */
891 cylinders
= nb_sectors
/ (16 * 63);
893 if (cylinders
> 16383)
895 else if (cylinders
< 2)
900 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
901 if ((*pcyls
* *pheads
) <= 131072) {
902 bdrv_set_translation_hint(bs
,
903 BIOS_ATA_TRANSLATION_LARGE
);
905 bdrv_set_translation_hint(bs
,
906 BIOS_ATA_TRANSLATION_LBA
);
910 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
914 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
915 int cyls
, int heads
, int secs
)
922 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
925 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
926 type
== BDRV_TYPE_FLOPPY
));
929 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
931 bs
->translation
= translation
;
934 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
935 int *pcyls
, int *pheads
, int *psecs
)
942 int bdrv_get_type_hint(BlockDriverState
*bs
)
947 int bdrv_get_translation_hint(BlockDriverState
*bs
)
949 return bs
->translation
;
952 int bdrv_is_removable(BlockDriverState
*bs
)
954 return bs
->removable
;
957 int bdrv_is_read_only(BlockDriverState
*bs
)
959 return bs
->read_only
;
962 int bdrv_is_sg(BlockDriverState
*bs
)
967 /* XXX: no longer used */
968 void bdrv_set_change_cb(BlockDriverState
*bs
,
969 void (*change_cb
)(void *opaque
), void *opaque
)
971 bs
->change_cb
= change_cb
;
972 bs
->change_opaque
= opaque
;
975 int bdrv_is_encrypted(BlockDriverState
*bs
)
977 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
979 return bs
->encrypted
;
982 int bdrv_key_required(BlockDriverState
*bs
)
984 BlockDriverState
*backing_hd
= bs
->backing_hd
;
986 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
988 return (bs
->encrypted
&& !bs
->valid_key
);
991 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
994 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
995 ret
= bdrv_set_key(bs
->backing_hd
, key
);
1001 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
1003 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
1004 bs
->valid_key
= (ret
== 0);
1008 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
1013 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
1017 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
1022 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
1023 it(opaque
, drv
->format_name
);
1027 BlockDriverState
*bdrv_find(const char *name
)
1029 BlockDriverState
*bs
;
1031 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1032 if (!strcmp(name
, bs
->device_name
))
1038 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1040 BlockDriverState
*bs
;
1042 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1047 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1049 return bs
->device_name
;
1052 void bdrv_flush(BlockDriverState
*bs
)
1054 if (bs
->drv
->bdrv_flush
)
1055 bs
->drv
->bdrv_flush(bs
);
1057 bdrv_flush(bs
->backing_hd
);
1060 void bdrv_flush_all(void)
1062 BlockDriverState
*bs
;
1064 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1065 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1066 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1071 * Returns true iff the specified sector is present in the disk image. Drivers
1072 * not implementing the functionality are assumed to not support backing files,
1073 * hence all their sectors are reported as allocated.
1075 * 'pnum' is set to the number of sectors (including and immediately following
1076 * the specified sector) that are known to be in the same
1077 * allocated/unallocated state.
1079 * 'nb_sectors' is the max value 'pnum' should be set to.
1081 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1085 if (!bs
->drv
->bdrv_is_allocated
) {
1086 if (sector_num
>= bs
->total_sectors
) {
1090 n
= bs
->total_sectors
- sector_num
;
1091 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1094 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1097 void bdrv_info(void)
1099 BlockDriverState
*bs
;
1101 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1102 term_printf("%s:", bs
->device_name
);
1103 term_printf(" type=");
1108 case BDRV_TYPE_CDROM
:
1109 term_printf("cdrom");
1111 case BDRV_TYPE_FLOPPY
:
1112 term_printf("floppy");
1115 term_printf(" removable=%d", bs
->removable
);
1116 if (bs
->removable
) {
1117 term_printf(" locked=%d", bs
->locked
);
1120 term_printf(" file=");
1121 term_print_filename(bs
->filename
);
1122 if (bs
->backing_file
[0] != '\0') {
1123 term_printf(" backing_file=");
1124 term_print_filename(bs
->backing_file
);
1126 term_printf(" ro=%d", bs
->read_only
);
1127 term_printf(" drv=%s", bs
->drv
->format_name
);
1128 term_printf(" encrypted=%d", bdrv_is_encrypted(bs
));
1130 term_printf(" [not inserted]");
1136 /* The "info blockstats" command. */
1137 void bdrv_info_stats (void)
1139 BlockDriverState
*bs
;
1141 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1143 " rd_bytes=%" PRIu64
1144 " wr_bytes=%" PRIu64
1145 " rd_operations=%" PRIu64
1146 " wr_operations=%" PRIu64
1149 bs
->rd_bytes
, bs
->wr_bytes
,
1150 bs
->rd_ops
, bs
->wr_ops
);
1154 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1156 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1157 return bs
->backing_file
;
1158 else if (bs
->encrypted
)
1159 return bs
->filename
;
1164 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1165 char *filename
, int filename_size
)
1167 if (!bs
->backing_hd
) {
1168 pstrcpy(filename
, filename_size
, "");
1170 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1174 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1175 const uint8_t *buf
, int nb_sectors
)
1177 BlockDriver
*drv
= bs
->drv
;
1180 if (!drv
->bdrv_write_compressed
)
1182 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1185 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1187 BlockDriver
*drv
= bs
->drv
;
1190 if (!drv
->bdrv_get_info
)
1192 memset(bdi
, 0, sizeof(*bdi
));
1193 return drv
->bdrv_get_info(bs
, bdi
);
1196 int bdrv_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
, int64_t pos
, int size
)
1198 BlockDriver
*drv
= bs
->drv
;
1201 if (!drv
->bdrv_put_buffer
)
1203 return drv
->bdrv_put_buffer(bs
, buf
, pos
, size
);
1206 int bdrv_get_buffer(BlockDriverState
*bs
, uint8_t *buf
, int64_t pos
, int size
)
1208 BlockDriver
*drv
= bs
->drv
;
1211 if (!drv
->bdrv_get_buffer
)
1213 return drv
->bdrv_get_buffer(bs
, buf
, pos
, size
);
1216 /**************************************************************/
1217 /* handling of snapshots */
1219 int bdrv_snapshot_create(BlockDriverState
*bs
,
1220 QEMUSnapshotInfo
*sn_info
)
1222 BlockDriver
*drv
= bs
->drv
;
1225 if (!drv
->bdrv_snapshot_create
)
1227 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1230 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1231 const char *snapshot_id
)
1233 BlockDriver
*drv
= bs
->drv
;
1236 if (!drv
->bdrv_snapshot_goto
)
1238 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1241 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1243 BlockDriver
*drv
= bs
->drv
;
1246 if (!drv
->bdrv_snapshot_delete
)
1248 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1251 int bdrv_snapshot_list(BlockDriverState
*bs
,
1252 QEMUSnapshotInfo
**psn_info
)
1254 BlockDriver
*drv
= bs
->drv
;
1257 if (!drv
->bdrv_snapshot_list
)
1259 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1262 #define NB_SUFFIXES 4
1264 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1266 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1271 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1274 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1275 if (size
< (10 * base
)) {
1276 snprintf(buf
, buf_size
, "%0.1f%c",
1277 (double)size
/ base
,
1280 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1281 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1282 ((size
+ (base
>> 1)) / base
),
1292 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1294 char buf1
[128], date_buf
[128], clock_buf
[128];
1304 snprintf(buf
, buf_size
,
1305 "%-10s%-20s%7s%20s%15s",
1306 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1310 ptm
= localtime(&ti
);
1311 strftime(date_buf
, sizeof(date_buf
),
1312 "%Y-%m-%d %H:%M:%S", ptm
);
1314 localtime_r(&ti
, &tm
);
1315 strftime(date_buf
, sizeof(date_buf
),
1316 "%Y-%m-%d %H:%M:%S", &tm
);
1318 secs
= sn
->vm_clock_nsec
/ 1000000000;
1319 snprintf(clock_buf
, sizeof(clock_buf
),
1320 "%02d:%02d:%02d.%03d",
1322 (int)((secs
/ 60) % 60),
1324 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1325 snprintf(buf
, buf_size
,
1326 "%-10s%-20s%7s%20s%15s",
1327 sn
->id_str
, sn
->name
,
1328 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1336 /**************************************************************/
1339 typedef struct VectorTranslationState
{
1343 BlockDriverAIOCB
*aiocb
;
1344 BlockDriverAIOCB
*this_aiocb
;
1345 } VectorTranslationState
;
1347 static void bdrv_aio_cancel_vector(BlockDriverAIOCB
*acb
)
1349 VectorTranslationState
*s
= acb
->opaque
;
1351 bdrv_aio_cancel(s
->aiocb
);
1354 static void bdrv_aio_rw_vector_cb(void *opaque
, int ret
)
1356 VectorTranslationState
*s
= opaque
;
1359 qemu_iovec_from_buffer(s
->iov
, s
->bounce
, s
->iov
->size
);
1361 qemu_vfree(s
->bounce
);
1362 s
->this_aiocb
->cb(s
->this_aiocb
->opaque
, ret
);
1363 qemu_aio_release(s
->this_aiocb
);
1366 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1370 BlockDriverCompletionFunc
*cb
,
1375 VectorTranslationState
*s
= qemu_mallocz(sizeof(*s
));
1376 BlockDriverAIOCB
*aiocb
= qemu_aio_get_pool(&vectored_aio_pool
, bs
,
1379 s
->this_aiocb
= aiocb
;
1381 s
->bounce
= qemu_memalign(512, nb_sectors
* 512);
1382 s
->is_write
= is_write
;
1384 qemu_iovec_to_buffer(s
->iov
, s
->bounce
);
1385 s
->aiocb
= bdrv_aio_write(bs
, sector_num
, s
->bounce
, nb_sectors
,
1386 bdrv_aio_rw_vector_cb
, s
);
1388 s
->aiocb
= bdrv_aio_read(bs
, sector_num
, s
->bounce
, nb_sectors
,
1389 bdrv_aio_rw_vector_cb
, s
);
1394 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1395 QEMUIOVector
*iov
, int nb_sectors
,
1396 BlockDriverCompletionFunc
*cb
, void *opaque
)
1398 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1401 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1405 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1406 QEMUIOVector
*iov
, int nb_sectors
,
1407 BlockDriverCompletionFunc
*cb
, void *opaque
)
1409 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1412 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1416 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1417 uint8_t *buf
, int nb_sectors
,
1418 BlockDriverCompletionFunc
*cb
, void *opaque
)
1420 BlockDriver
*drv
= bs
->drv
;
1421 BlockDriverAIOCB
*ret
;
1425 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1428 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1431 /* Update stats even though technically transfer has not happened. */
1432 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1439 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1440 const uint8_t *buf
, int nb_sectors
,
1441 BlockDriverCompletionFunc
*cb
, void *opaque
)
1443 BlockDriver
*drv
= bs
->drv
;
1444 BlockDriverAIOCB
*ret
;
1450 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1453 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1456 /* Update stats even though technically transfer has not happened. */
1457 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1464 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1466 acb
->pool
->cancel(acb
);
1470 /**************************************************************/
1471 /* async block device emulation */
1473 static void bdrv_aio_bh_cb(void *opaque
)
1475 BlockDriverAIOCBSync
*acb
= opaque
;
1476 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1477 qemu_aio_release(acb
);
1480 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1481 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1482 BlockDriverCompletionFunc
*cb
, void *opaque
)
1484 BlockDriverAIOCBSync
*acb
;
1487 acb
= qemu_aio_get(bs
, cb
, opaque
);
1489 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1490 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1492 qemu_bh_schedule(acb
->bh
);
1493 return &acb
->common
;
1496 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1497 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1498 BlockDriverCompletionFunc
*cb
, void *opaque
)
1500 BlockDriverAIOCBSync
*acb
;
1503 acb
= qemu_aio_get(bs
, cb
, opaque
);
1505 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1506 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1508 qemu_bh_schedule(acb
->bh
);
1509 return &acb
->common
;
1512 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1514 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1515 qemu_bh_cancel(acb
->bh
);
1516 qemu_aio_release(acb
);
1519 /**************************************************************/
1520 /* sync block device emulation */
1522 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1524 *(int *)opaque
= ret
;
1527 #define NOT_DONE 0x7fffffff
1529 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1530 uint8_t *buf
, int nb_sectors
)
1533 BlockDriverAIOCB
*acb
;
1535 async_ret
= NOT_DONE
;
1536 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1537 bdrv_rw_em_cb
, &async_ret
);
1541 while (async_ret
== NOT_DONE
) {
1548 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1549 const uint8_t *buf
, int nb_sectors
)
1552 BlockDriverAIOCB
*acb
;
1554 async_ret
= NOT_DONE
;
1555 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1556 bdrv_rw_em_cb
, &async_ret
);
1559 while (async_ret
== NOT_DONE
) {
1565 void bdrv_init(void)
1567 aio_pool_init(&vectored_aio_pool
, sizeof(BlockDriverAIOCB
),
1568 bdrv_aio_cancel_vector
);
1570 bdrv_register(&bdrv_raw
);
1571 bdrv_register(&bdrv_host_device
);
1573 bdrv_register(&bdrv_cow
);
1575 bdrv_register(&bdrv_qcow
);
1576 bdrv_register(&bdrv_vmdk
);
1577 bdrv_register(&bdrv_cloop
);
1578 bdrv_register(&bdrv_dmg
);
1579 bdrv_register(&bdrv_bochs
);
1580 bdrv_register(&bdrv_vpc
);
1581 bdrv_register(&bdrv_vvfat
);
1582 bdrv_register(&bdrv_qcow2
);
1583 bdrv_register(&bdrv_parallels
);
1584 bdrv_register(&bdrv_nbd
);
1587 void aio_pool_init(AIOPool
*pool
, int aiocb_size
,
1588 void (*cancel
)(BlockDriverAIOCB
*acb
))
1590 pool
->aiocb_size
= aiocb_size
;
1591 pool
->cancel
= cancel
;
1592 pool
->free_aiocb
= NULL
;
1595 void *qemu_aio_get_pool(AIOPool
*pool
, BlockDriverState
*bs
,
1596 BlockDriverCompletionFunc
*cb
, void *opaque
)
1598 BlockDriverAIOCB
*acb
;
1600 if (pool
->free_aiocb
) {
1601 acb
= pool
->free_aiocb
;
1602 pool
->free_aiocb
= acb
->next
;
1604 acb
= qemu_mallocz(pool
->aiocb_size
);
1609 acb
->opaque
= opaque
;
1613 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1616 return qemu_aio_get_pool(&bs
->drv
->aio_pool
, bs
, cb
, opaque
);
1619 void qemu_aio_release(void *p
)
1621 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1622 AIOPool
*pool
= acb
->pool
;
1623 acb
->next
= pool
->free_aiocb
;
1624 pool
->free_aiocb
= acb
;
1627 /**************************************************************/
1628 /* removable device support */
1631 * Return TRUE if the media is present
1633 int bdrv_is_inserted(BlockDriverState
*bs
)
1635 BlockDriver
*drv
= bs
->drv
;
1639 if (!drv
->bdrv_is_inserted
)
1641 ret
= drv
->bdrv_is_inserted(bs
);
1646 * Return TRUE if the media changed since the last call to this
1647 * function. It is currently only used for floppy disks
1649 int bdrv_media_changed(BlockDriverState
*bs
)
1651 BlockDriver
*drv
= bs
->drv
;
1654 if (!drv
|| !drv
->bdrv_media_changed
)
1657 ret
= drv
->bdrv_media_changed(bs
);
1658 if (ret
== -ENOTSUP
)
1659 ret
= bs
->media_changed
;
1660 bs
->media_changed
= 0;
1665 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1667 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1669 BlockDriver
*drv
= bs
->drv
;
1672 if (!drv
|| !drv
->bdrv_eject
) {
1675 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1677 if (ret
== -ENOTSUP
) {
1683 int bdrv_is_locked(BlockDriverState
*bs
)
1689 * Lock or unlock the media (if it is locked, the user won't be able
1690 * to eject it manually).
1692 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1694 BlockDriver
*drv
= bs
->drv
;
1696 bs
->locked
= locked
;
1697 if (drv
&& drv
->bdrv_set_locked
) {
1698 drv
->bdrv_set_locked(bs
, locked
);
1702 /* needed for generic scsi interface */
1704 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1706 BlockDriver
*drv
= bs
->drv
;
1708 if (drv
&& drv
->bdrv_ioctl
)
1709 return drv
->bdrv_ioctl(bs
, req
, buf
);