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
);
540 if ((offset
> len
) || (len
- offset
< size
))
546 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
551 /* Deal with byte accesses */
553 offset
= -sector_num
;
555 offset
= sector_num
* 512;
557 return bdrv_check_byte_request(bs
, offset
, nb_sectors
* 512);
560 /* return < 0 if error. See bdrv_write() for the return codes */
561 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
562 uint8_t *buf
, int nb_sectors
)
564 BlockDriver
*drv
= bs
->drv
;
568 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
571 if (drv
->bdrv_pread
) {
573 len
= nb_sectors
* 512;
574 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
580 bs
->rd_bytes
+= (unsigned) len
;
585 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
589 /* Return < 0 if error. Important errors are:
590 -EIO generic I/O error (may happen for all errors)
591 -ENOMEDIUM No media inserted.
592 -EINVAL Invalid sector number or nb_sectors
593 -EACCES Trying to write a read-only device
595 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
596 const uint8_t *buf
, int nb_sectors
)
598 BlockDriver
*drv
= bs
->drv
;
603 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
606 if (drv
->bdrv_pwrite
) {
607 int ret
, len
, count
= 0;
608 len
= nb_sectors
* 512;
610 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
- count
);
612 printf("bdrv_write ret=%d\n", ret
);
617 } while (count
!= len
);
618 bs
->wr_bytes
+= (unsigned) len
;
622 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
625 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
626 uint8_t *buf
, int count1
)
628 uint8_t tmp_buf
[SECTOR_SIZE
];
629 int len
, nb_sectors
, count
;
633 /* first read to align to sector start */
634 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
637 sector_num
= offset
>> SECTOR_BITS
;
639 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
641 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
649 /* read the sectors "in place" */
650 nb_sectors
= count
>> SECTOR_BITS
;
651 if (nb_sectors
> 0) {
652 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
654 sector_num
+= nb_sectors
;
655 len
= nb_sectors
<< SECTOR_BITS
;
660 /* add data from the last sector */
662 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
664 memcpy(buf
, tmp_buf
, count
);
669 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
670 const uint8_t *buf
, int count1
)
672 uint8_t tmp_buf
[SECTOR_SIZE
];
673 int len
, nb_sectors
, count
;
677 /* first write to align to sector start */
678 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
681 sector_num
= offset
>> SECTOR_BITS
;
683 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
685 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
686 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
695 /* write the sectors "in place" */
696 nb_sectors
= count
>> SECTOR_BITS
;
697 if (nb_sectors
> 0) {
698 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
700 sector_num
+= nb_sectors
;
701 len
= nb_sectors
<< SECTOR_BITS
;
706 /* add data from the last sector */
708 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
710 memcpy(tmp_buf
, buf
, count
);
711 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
718 * Read with byte offsets (needed only for file protocols)
720 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
721 void *buf1
, int count1
)
723 BlockDriver
*drv
= bs
->drv
;
727 if (bdrv_check_byte_request(bs
, offset
, count1
))
730 if (!drv
->bdrv_pread
)
731 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
732 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
736 * Write with byte offsets (needed only for file protocols)
738 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
739 const void *buf1
, int count1
)
741 BlockDriver
*drv
= bs
->drv
;
745 if (bdrv_check_byte_request(bs
, offset
, count1
))
748 if (!drv
->bdrv_pwrite
)
749 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
750 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
754 * Truncate file to 'offset' bytes (needed only for file protocols)
756 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
758 BlockDriver
*drv
= bs
->drv
;
761 if (!drv
->bdrv_truncate
)
763 return drv
->bdrv_truncate(bs
, offset
);
767 * Length of a file in bytes. Return < 0 if error or unknown.
769 int64_t bdrv_getlength(BlockDriverState
*bs
)
771 BlockDriver
*drv
= bs
->drv
;
774 if (!drv
->bdrv_getlength
) {
776 return bs
->total_sectors
* SECTOR_SIZE
;
778 return drv
->bdrv_getlength(bs
);
781 /* return 0 as number of sectors if no device present or error */
782 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
785 length
= bdrv_getlength(bs
);
789 length
= length
>> SECTOR_BITS
;
790 *nb_sectors_ptr
= length
;
794 uint8_t boot_ind
; /* 0x80 - active */
795 uint8_t head
; /* starting head */
796 uint8_t sector
; /* starting sector */
797 uint8_t cyl
; /* starting cylinder */
798 uint8_t sys_ind
; /* What partition type */
799 uint8_t end_head
; /* end head */
800 uint8_t end_sector
; /* end sector */
801 uint8_t end_cyl
; /* end cylinder */
802 uint32_t start_sect
; /* starting sector counting from 0 */
803 uint32_t nr_sects
; /* nr of sectors in partition */
804 } __attribute__((packed
));
806 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
807 static int guess_disk_lchs(BlockDriverState
*bs
,
808 int *pcylinders
, int *pheads
, int *psectors
)
811 int ret
, i
, heads
, sectors
, cylinders
;
816 buf
= qemu_memalign(512, 512);
820 bdrv_get_geometry(bs
, &nb_sectors
);
822 ret
= bdrv_read(bs
, 0, buf
, 1);
825 /* test msdos magic */
826 if (buf
[510] != 0x55 || buf
[511] != 0xaa) {
830 for(i
= 0; i
< 4; i
++) {
831 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
832 nr_sects
= le32_to_cpu(p
->nr_sects
);
833 if (nr_sects
&& p
->end_head
) {
834 /* We make the assumption that the partition terminates on
835 a cylinder boundary */
836 heads
= p
->end_head
+ 1;
837 sectors
= p
->end_sector
& 63;
840 cylinders
= nb_sectors
/ (heads
* sectors
);
841 if (cylinders
< 1 || cylinders
> 16383)
845 *pcylinders
= cylinders
;
847 printf("guessed geometry: LCHS=%d %d %d\n",
848 cylinders
, heads
, sectors
);
858 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
860 int translation
, lba_detected
= 0;
861 int cylinders
, heads
, secs
;
864 /* if a geometry hint is available, use it */
865 bdrv_get_geometry(bs
, &nb_sectors
);
866 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
867 translation
= bdrv_get_translation_hint(bs
);
868 if (cylinders
!= 0) {
873 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
875 /* if heads > 16, it means that a BIOS LBA
876 translation was active, so the default
877 hardware geometry is OK */
879 goto default_geometry
;
884 /* disable any translation to be in sync with
885 the logical geometry */
886 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
887 bdrv_set_translation_hint(bs
,
888 BIOS_ATA_TRANSLATION_NONE
);
893 /* if no geometry, use a standard physical disk geometry */
894 cylinders
= nb_sectors
/ (16 * 63);
896 if (cylinders
> 16383)
898 else if (cylinders
< 2)
903 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
904 if ((*pcyls
* *pheads
) <= 131072) {
905 bdrv_set_translation_hint(bs
,
906 BIOS_ATA_TRANSLATION_LARGE
);
908 bdrv_set_translation_hint(bs
,
909 BIOS_ATA_TRANSLATION_LBA
);
913 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
917 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
918 int cyls
, int heads
, int secs
)
925 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
928 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
929 type
== BDRV_TYPE_FLOPPY
));
932 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
934 bs
->translation
= translation
;
937 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
938 int *pcyls
, int *pheads
, int *psecs
)
945 int bdrv_get_type_hint(BlockDriverState
*bs
)
950 int bdrv_get_translation_hint(BlockDriverState
*bs
)
952 return bs
->translation
;
955 int bdrv_is_removable(BlockDriverState
*bs
)
957 return bs
->removable
;
960 int bdrv_is_read_only(BlockDriverState
*bs
)
962 return bs
->read_only
;
965 int bdrv_is_sg(BlockDriverState
*bs
)
970 /* XXX: no longer used */
971 void bdrv_set_change_cb(BlockDriverState
*bs
,
972 void (*change_cb
)(void *opaque
), void *opaque
)
974 bs
->change_cb
= change_cb
;
975 bs
->change_opaque
= opaque
;
978 int bdrv_is_encrypted(BlockDriverState
*bs
)
980 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
982 return bs
->encrypted
;
985 int bdrv_key_required(BlockDriverState
*bs
)
987 BlockDriverState
*backing_hd
= bs
->backing_hd
;
989 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
991 return (bs
->encrypted
&& !bs
->valid_key
);
994 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
997 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
998 ret
= bdrv_set_key(bs
->backing_hd
, key
);
1004 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
1006 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
1007 bs
->valid_key
= (ret
== 0);
1011 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
1016 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
1020 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
1025 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
1026 it(opaque
, drv
->format_name
);
1030 BlockDriverState
*bdrv_find(const char *name
)
1032 BlockDriverState
*bs
;
1034 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1035 if (!strcmp(name
, bs
->device_name
))
1041 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1043 BlockDriverState
*bs
;
1045 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1050 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1052 return bs
->device_name
;
1055 void bdrv_flush(BlockDriverState
*bs
)
1057 if (bs
->drv
->bdrv_flush
)
1058 bs
->drv
->bdrv_flush(bs
);
1060 bdrv_flush(bs
->backing_hd
);
1063 void bdrv_flush_all(void)
1065 BlockDriverState
*bs
;
1067 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1068 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1069 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1074 * Returns true iff the specified sector is present in the disk image. Drivers
1075 * not implementing the functionality are assumed to not support backing files,
1076 * hence all their sectors are reported as allocated.
1078 * 'pnum' is set to the number of sectors (including and immediately following
1079 * the specified sector) that are known to be in the same
1080 * allocated/unallocated state.
1082 * 'nb_sectors' is the max value 'pnum' should be set to.
1084 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1088 if (!bs
->drv
->bdrv_is_allocated
) {
1089 if (sector_num
>= bs
->total_sectors
) {
1093 n
= bs
->total_sectors
- sector_num
;
1094 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1097 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1100 void bdrv_info(void)
1102 BlockDriverState
*bs
;
1104 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1105 term_printf("%s:", bs
->device_name
);
1106 term_printf(" type=");
1111 case BDRV_TYPE_CDROM
:
1112 term_printf("cdrom");
1114 case BDRV_TYPE_FLOPPY
:
1115 term_printf("floppy");
1118 term_printf(" removable=%d", bs
->removable
);
1119 if (bs
->removable
) {
1120 term_printf(" locked=%d", bs
->locked
);
1123 term_printf(" file=");
1124 term_print_filename(bs
->filename
);
1125 if (bs
->backing_file
[0] != '\0') {
1126 term_printf(" backing_file=");
1127 term_print_filename(bs
->backing_file
);
1129 term_printf(" ro=%d", bs
->read_only
);
1130 term_printf(" drv=%s", bs
->drv
->format_name
);
1131 term_printf(" encrypted=%d", bdrv_is_encrypted(bs
));
1133 term_printf(" [not inserted]");
1139 /* The "info blockstats" command. */
1140 void bdrv_info_stats (void)
1142 BlockDriverState
*bs
;
1144 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1146 " rd_bytes=%" PRIu64
1147 " wr_bytes=%" PRIu64
1148 " rd_operations=%" PRIu64
1149 " wr_operations=%" PRIu64
1152 bs
->rd_bytes
, bs
->wr_bytes
,
1153 bs
->rd_ops
, bs
->wr_ops
);
1157 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1159 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1160 return bs
->backing_file
;
1161 else if (bs
->encrypted
)
1162 return bs
->filename
;
1167 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1168 char *filename
, int filename_size
)
1170 if (!bs
->backing_hd
) {
1171 pstrcpy(filename
, filename_size
, "");
1173 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1177 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1178 const uint8_t *buf
, int nb_sectors
)
1180 BlockDriver
*drv
= bs
->drv
;
1183 if (!drv
->bdrv_write_compressed
)
1185 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1187 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1190 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1192 BlockDriver
*drv
= bs
->drv
;
1195 if (!drv
->bdrv_get_info
)
1197 memset(bdi
, 0, sizeof(*bdi
));
1198 return drv
->bdrv_get_info(bs
, bdi
);
1201 int bdrv_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
, int64_t pos
, int size
)
1203 BlockDriver
*drv
= bs
->drv
;
1206 if (!drv
->bdrv_put_buffer
)
1208 return drv
->bdrv_put_buffer(bs
, buf
, pos
, size
);
1211 int bdrv_get_buffer(BlockDriverState
*bs
, uint8_t *buf
, int64_t pos
, int size
)
1213 BlockDriver
*drv
= bs
->drv
;
1216 if (!drv
->bdrv_get_buffer
)
1218 return drv
->bdrv_get_buffer(bs
, buf
, pos
, size
);
1221 /**************************************************************/
1222 /* handling of snapshots */
1224 int bdrv_snapshot_create(BlockDriverState
*bs
,
1225 QEMUSnapshotInfo
*sn_info
)
1227 BlockDriver
*drv
= bs
->drv
;
1230 if (!drv
->bdrv_snapshot_create
)
1232 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1235 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1236 const char *snapshot_id
)
1238 BlockDriver
*drv
= bs
->drv
;
1241 if (!drv
->bdrv_snapshot_goto
)
1243 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1246 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1248 BlockDriver
*drv
= bs
->drv
;
1251 if (!drv
->bdrv_snapshot_delete
)
1253 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1256 int bdrv_snapshot_list(BlockDriverState
*bs
,
1257 QEMUSnapshotInfo
**psn_info
)
1259 BlockDriver
*drv
= bs
->drv
;
1262 if (!drv
->bdrv_snapshot_list
)
1264 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1267 #define NB_SUFFIXES 4
1269 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1271 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1276 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1279 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1280 if (size
< (10 * base
)) {
1281 snprintf(buf
, buf_size
, "%0.1f%c",
1282 (double)size
/ base
,
1285 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1286 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1287 ((size
+ (base
>> 1)) / base
),
1297 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1299 char buf1
[128], date_buf
[128], clock_buf
[128];
1309 snprintf(buf
, buf_size
,
1310 "%-10s%-20s%7s%20s%15s",
1311 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1315 ptm
= localtime(&ti
);
1316 strftime(date_buf
, sizeof(date_buf
),
1317 "%Y-%m-%d %H:%M:%S", ptm
);
1319 localtime_r(&ti
, &tm
);
1320 strftime(date_buf
, sizeof(date_buf
),
1321 "%Y-%m-%d %H:%M:%S", &tm
);
1323 secs
= sn
->vm_clock_nsec
/ 1000000000;
1324 snprintf(clock_buf
, sizeof(clock_buf
),
1325 "%02d:%02d:%02d.%03d",
1327 (int)((secs
/ 60) % 60),
1329 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1330 snprintf(buf
, buf_size
,
1331 "%-10s%-20s%7s%20s%15s",
1332 sn
->id_str
, sn
->name
,
1333 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1341 /**************************************************************/
1344 typedef struct VectorTranslationAIOCB
{
1345 BlockDriverAIOCB common
;
1349 BlockDriverAIOCB
*aiocb
;
1350 } VectorTranslationAIOCB
;
1352 static void bdrv_aio_cancel_vector(BlockDriverAIOCB
*_acb
)
1354 VectorTranslationAIOCB
*acb
1355 = container_of(_acb
, VectorTranslationAIOCB
, common
);
1357 bdrv_aio_cancel(acb
->aiocb
);
1360 static void bdrv_aio_rw_vector_cb(void *opaque
, int ret
)
1362 VectorTranslationAIOCB
*s
= (VectorTranslationAIOCB
*)opaque
;
1365 qemu_iovec_from_buffer(s
->iov
, s
->bounce
, s
->iov
->size
);
1367 qemu_vfree(s
->bounce
);
1368 s
->common
.cb(s
->common
.opaque
, ret
);
1369 qemu_aio_release(s
);
1372 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1376 BlockDriverCompletionFunc
*cb
,
1381 VectorTranslationAIOCB
*s
= qemu_aio_get_pool(&vectored_aio_pool
, bs
,
1385 s
->bounce
= qemu_memalign(512, nb_sectors
* 512);
1386 s
->is_write
= is_write
;
1388 qemu_iovec_to_buffer(s
->iov
, s
->bounce
);
1389 s
->aiocb
= bdrv_aio_write(bs
, sector_num
, s
->bounce
, nb_sectors
,
1390 bdrv_aio_rw_vector_cb
, s
);
1392 s
->aiocb
= bdrv_aio_read(bs
, sector_num
, s
->bounce
, nb_sectors
,
1393 bdrv_aio_rw_vector_cb
, s
);
1396 qemu_vfree(s
->bounce
);
1397 qemu_aio_release(s
);
1403 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1404 QEMUIOVector
*iov
, int nb_sectors
,
1405 BlockDriverCompletionFunc
*cb
, void *opaque
)
1407 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1410 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1414 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1415 QEMUIOVector
*iov
, int nb_sectors
,
1416 BlockDriverCompletionFunc
*cb
, void *opaque
)
1418 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1421 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1425 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1426 uint8_t *buf
, int nb_sectors
,
1427 BlockDriverCompletionFunc
*cb
, void *opaque
)
1429 BlockDriver
*drv
= bs
->drv
;
1430 BlockDriverAIOCB
*ret
;
1434 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1437 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1440 /* Update stats even though technically transfer has not happened. */
1441 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1448 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1449 const uint8_t *buf
, int nb_sectors
,
1450 BlockDriverCompletionFunc
*cb
, void *opaque
)
1452 BlockDriver
*drv
= bs
->drv
;
1453 BlockDriverAIOCB
*ret
;
1459 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1462 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1465 /* Update stats even though technically transfer has not happened. */
1466 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1473 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1475 acb
->pool
->cancel(acb
);
1479 /**************************************************************/
1480 /* async block device emulation */
1482 static void bdrv_aio_bh_cb(void *opaque
)
1484 BlockDriverAIOCBSync
*acb
= opaque
;
1485 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1486 qemu_aio_release(acb
);
1489 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1490 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1491 BlockDriverCompletionFunc
*cb
, void *opaque
)
1493 BlockDriverAIOCBSync
*acb
;
1496 acb
= qemu_aio_get(bs
, cb
, opaque
);
1498 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1499 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1501 qemu_bh_schedule(acb
->bh
);
1502 return &acb
->common
;
1505 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1506 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1507 BlockDriverCompletionFunc
*cb
, void *opaque
)
1509 BlockDriverAIOCBSync
*acb
;
1512 acb
= qemu_aio_get(bs
, cb
, opaque
);
1514 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1515 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1517 qemu_bh_schedule(acb
->bh
);
1518 return &acb
->common
;
1521 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1523 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1524 qemu_bh_cancel(acb
->bh
);
1525 qemu_aio_release(acb
);
1528 /**************************************************************/
1529 /* sync block device emulation */
1531 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1533 *(int *)opaque
= ret
;
1536 #define NOT_DONE 0x7fffffff
1538 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1539 uint8_t *buf
, int nb_sectors
)
1542 BlockDriverAIOCB
*acb
;
1544 async_ret
= NOT_DONE
;
1545 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1546 bdrv_rw_em_cb
, &async_ret
);
1550 while (async_ret
== NOT_DONE
) {
1557 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1558 const uint8_t *buf
, int nb_sectors
)
1561 BlockDriverAIOCB
*acb
;
1563 async_ret
= NOT_DONE
;
1564 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1565 bdrv_rw_em_cb
, &async_ret
);
1568 while (async_ret
== NOT_DONE
) {
1574 void bdrv_init(void)
1576 aio_pool_init(&vectored_aio_pool
, sizeof(VectorTranslationAIOCB
),
1577 bdrv_aio_cancel_vector
);
1579 bdrv_register(&bdrv_raw
);
1580 bdrv_register(&bdrv_host_device
);
1582 bdrv_register(&bdrv_cow
);
1584 bdrv_register(&bdrv_qcow
);
1585 bdrv_register(&bdrv_vmdk
);
1586 bdrv_register(&bdrv_cloop
);
1587 bdrv_register(&bdrv_dmg
);
1588 bdrv_register(&bdrv_bochs
);
1589 bdrv_register(&bdrv_vpc
);
1590 bdrv_register(&bdrv_vvfat
);
1591 bdrv_register(&bdrv_qcow2
);
1592 bdrv_register(&bdrv_parallels
);
1593 bdrv_register(&bdrv_nbd
);
1596 void aio_pool_init(AIOPool
*pool
, int aiocb_size
,
1597 void (*cancel
)(BlockDriverAIOCB
*acb
))
1599 pool
->aiocb_size
= aiocb_size
;
1600 pool
->cancel
= cancel
;
1601 pool
->free_aiocb
= NULL
;
1604 void *qemu_aio_get_pool(AIOPool
*pool
, BlockDriverState
*bs
,
1605 BlockDriverCompletionFunc
*cb
, void *opaque
)
1607 BlockDriverAIOCB
*acb
;
1609 if (pool
->free_aiocb
) {
1610 acb
= pool
->free_aiocb
;
1611 pool
->free_aiocb
= acb
->next
;
1613 acb
= qemu_mallocz(pool
->aiocb_size
);
1618 acb
->opaque
= opaque
;
1622 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1625 return qemu_aio_get_pool(&bs
->drv
->aio_pool
, bs
, cb
, opaque
);
1628 void qemu_aio_release(void *p
)
1630 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1631 AIOPool
*pool
= acb
->pool
;
1632 acb
->next
= pool
->free_aiocb
;
1633 pool
->free_aiocb
= acb
;
1636 /**************************************************************/
1637 /* removable device support */
1640 * Return TRUE if the media is present
1642 int bdrv_is_inserted(BlockDriverState
*bs
)
1644 BlockDriver
*drv
= bs
->drv
;
1648 if (!drv
->bdrv_is_inserted
)
1650 ret
= drv
->bdrv_is_inserted(bs
);
1655 * Return TRUE if the media changed since the last call to this
1656 * function. It is currently only used for floppy disks
1658 int bdrv_media_changed(BlockDriverState
*bs
)
1660 BlockDriver
*drv
= bs
->drv
;
1663 if (!drv
|| !drv
->bdrv_media_changed
)
1666 ret
= drv
->bdrv_media_changed(bs
);
1667 if (ret
== -ENOTSUP
)
1668 ret
= bs
->media_changed
;
1669 bs
->media_changed
= 0;
1674 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1676 int bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1678 BlockDriver
*drv
= bs
->drv
;
1685 if (!drv
|| !drv
->bdrv_eject
) {
1688 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1690 if (ret
== -ENOTSUP
) {
1699 int bdrv_is_locked(BlockDriverState
*bs
)
1705 * Lock or unlock the media (if it is locked, the user won't be able
1706 * to eject it manually).
1708 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1710 BlockDriver
*drv
= bs
->drv
;
1712 bs
->locked
= locked
;
1713 if (drv
&& drv
->bdrv_set_locked
) {
1714 drv
->bdrv_set_locked(bs
, locked
);
1718 /* needed for generic scsi interface */
1720 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1722 BlockDriver
*drv
= bs
->drv
;
1724 if (drv
&& drv
->bdrv_ioctl
)
1725 return drv
->bdrv_ioctl(bs
, req
, buf
);