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 AIOPool vectored_aio_pool
;
53 typedef struct BlockDriverAIOCBSync
{
54 BlockDriverAIOCB common
;
57 } BlockDriverAIOCBSync
;
59 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
60 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
61 BlockDriverCompletionFunc
*cb
, void *opaque
);
62 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
63 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
64 BlockDriverCompletionFunc
*cb
, void *opaque
);
65 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
66 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
67 uint8_t *buf
, int nb_sectors
);
68 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
69 const uint8_t *buf
, int nb_sectors
);
71 BlockDriverState
*bdrv_first
;
73 static BlockDriver
*first_drv
;
75 int path_is_absolute(const char *path
)
79 /* specific case for names like: "\\.\d:" */
80 if (*path
== '/' || *path
== '\\')
83 p
= strchr(path
, ':');
89 return (*p
== '/' || *p
== '\\');
95 /* if filename is absolute, just copy it to dest. Otherwise, build a
96 path to it by considering it is relative to base_path. URL are
98 void path_combine(char *dest
, int dest_size
,
99 const char *base_path
,
100 const char *filename
)
107 if (path_is_absolute(filename
)) {
108 pstrcpy(dest
, dest_size
, filename
);
110 p
= strchr(base_path
, ':');
115 p1
= strrchr(base_path
, '/');
119 p2
= strrchr(base_path
, '\\');
131 if (len
> dest_size
- 1)
133 memcpy(dest
, base_path
, len
);
135 pstrcat(dest
, dest_size
, filename
);
140 static void bdrv_register(BlockDriver
*bdrv
)
142 if (!bdrv
->bdrv_aio_read
) {
143 /* add AIO emulation layer */
144 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
145 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
146 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
147 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
148 } else if (!bdrv
->bdrv_read
) {
149 /* add synchronous IO emulation layer */
150 bdrv
->bdrv_read
= bdrv_read_em
;
151 bdrv
->bdrv_write
= bdrv_write_em
;
153 aio_pool_init(&bdrv
->aio_pool
, bdrv
->aiocb_size
, bdrv
->bdrv_aio_cancel
);
154 bdrv
->next
= first_drv
;
158 /* create a new block device (by default it is empty) */
159 BlockDriverState
*bdrv_new(const char *device_name
)
161 BlockDriverState
**pbs
, *bs
;
163 bs
= qemu_mallocz(sizeof(BlockDriverState
));
164 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
165 if (device_name
[0] != '\0') {
166 /* insert at the end */
175 BlockDriver
*bdrv_find_format(const char *format_name
)
178 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
179 if (!strcmp(drv1
->format_name
, format_name
))
185 int bdrv_create2(BlockDriver
*drv
,
186 const char *filename
, int64_t size_in_sectors
,
187 const char *backing_file
, const char *backing_format
,
190 if (drv
->bdrv_create2
)
191 return drv
->bdrv_create2(filename
, size_in_sectors
, backing_file
,
192 backing_format
, flags
);
193 if (drv
->bdrv_create
)
194 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
,
199 int bdrv_create(BlockDriver
*drv
,
200 const char *filename
, int64_t size_in_sectors
,
201 const char *backing_file
, int flags
)
203 if (!drv
->bdrv_create
)
205 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
209 void get_tmp_filename(char *filename
, int size
)
211 char temp_dir
[MAX_PATH
];
213 GetTempPath(MAX_PATH
, temp_dir
);
214 GetTempFileName(temp_dir
, "qem", 0, filename
);
217 void get_tmp_filename(char *filename
, int size
)
221 /* XXX: race condition possible */
222 tmpdir
= getenv("TMPDIR");
225 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
226 fd
= mkstemp(filename
);
232 static int is_windows_drive_prefix(const char *filename
)
234 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
235 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
239 static int is_windows_drive(const char *filename
)
241 if (is_windows_drive_prefix(filename
) &&
244 if (strstart(filename
, "\\\\.\\", NULL
) ||
245 strstart(filename
, "//./", NULL
))
251 static BlockDriver
*find_protocol(const char *filename
)
259 if (is_windows_drive(filename
) ||
260 is_windows_drive_prefix(filename
))
263 p
= strchr(filename
, ':');
267 if (len
> sizeof(protocol
) - 1)
268 len
= sizeof(protocol
) - 1;
269 memcpy(protocol
, filename
, len
);
270 protocol
[len
] = '\0';
271 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
272 if (drv1
->protocol_name
&&
273 !strcmp(drv1
->protocol_name
, protocol
))
279 /* XXX: force raw format if block or character device ? It would
280 simplify the BSD case */
281 static BlockDriver
*find_image_format(const char *filename
)
283 int ret
, score
, score_max
;
284 BlockDriver
*drv1
, *drv
;
286 BlockDriverState
*bs
;
288 /* detect host devices. By convention, /dev/cdrom[N] is always
289 recognized as a host CDROM */
290 if (strstart(filename
, "/dev/cdrom", NULL
))
291 return &bdrv_host_device
;
293 if (is_windows_drive(filename
))
294 return &bdrv_host_device
;
298 if (stat(filename
, &st
) >= 0 &&
299 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
300 return &bdrv_host_device
;
305 drv
= find_protocol(filename
);
306 /* no need to test disk image formats for vvfat */
307 if (drv
== &bdrv_vvfat
)
310 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
313 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
320 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
321 if (drv1
->bdrv_probe
) {
322 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
323 if (score
> score_max
) {
332 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
334 BlockDriverState
*bs
;
338 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
348 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
350 return bdrv_open2(bs
, filename
, flags
, NULL
);
353 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
357 char tmp_filename
[PATH_MAX
];
358 char backing_filename
[PATH_MAX
];
361 bs
->is_temporary
= 0;
365 if (flags
& BDRV_O_SNAPSHOT
) {
366 BlockDriverState
*bs1
;
370 /* if snapshot, we create a temporary backing file and open it
371 instead of opening 'filename' directly */
373 /* if there is a backing file, use it */
375 ret
= bdrv_open2(bs1
, filename
, 0, drv
);
380 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
382 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
387 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
389 /* Real path is meaningless for protocols */
391 snprintf(backing_filename
, sizeof(backing_filename
),
394 realpath(filename
, backing_filename
);
396 ret
= bdrv_create2(&bdrv_qcow2
, tmp_filename
,
397 total_size
, backing_filename
,
398 (drv
? drv
->format_name
: NULL
), 0);
402 filename
= tmp_filename
;
404 bs
->is_temporary
= 1;
407 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
408 if (flags
& BDRV_O_FILE
) {
409 drv
= find_protocol(filename
);
411 drv
= find_image_format(filename
);
415 goto unlink_and_fail
;
418 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
419 /* Note: for compatibility, we open disk image files as RDWR, and
420 RDONLY as fallback */
421 if (!(flags
& BDRV_O_FILE
))
422 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
424 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
425 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
426 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
427 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
431 qemu_free(bs
->opaque
);
435 if (bs
->is_temporary
)
439 if (drv
->bdrv_getlength
) {
440 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
443 if (bs
->is_temporary
) {
447 if (bs
->backing_file
[0] != '\0') {
448 /* if there is a backing file, use it */
449 BlockDriver
*back_drv
= NULL
;
450 bs
->backing_hd
= bdrv_new("");
451 path_combine(backing_filename
, sizeof(backing_filename
),
452 filename
, bs
->backing_file
);
453 if (bs
->backing_format
[0] != '\0')
454 back_drv
= bdrv_find_format(bs
->backing_format
);
455 ret
= bdrv_open2(bs
->backing_hd
, backing_filename
, open_flags
,
463 if (!bdrv_key_required(bs
)) {
464 /* call the change callback */
465 bs
->media_changed
= 1;
467 bs
->change_cb(bs
->change_opaque
);
472 void bdrv_close(BlockDriverState
*bs
)
476 bdrv_delete(bs
->backing_hd
);
477 bs
->drv
->bdrv_close(bs
);
478 qemu_free(bs
->opaque
);
480 if (bs
->is_temporary
) {
481 unlink(bs
->filename
);
487 /* call the change callback */
488 bs
->media_changed
= 1;
490 bs
->change_cb(bs
->change_opaque
);
494 void bdrv_delete(BlockDriverState
*bs
)
496 BlockDriverState
**pbs
;
499 while (*pbs
!= bs
&& *pbs
!= NULL
)
508 /* commit COW file into the raw image */
509 int bdrv_commit(BlockDriverState
*bs
)
511 BlockDriver
*drv
= bs
->drv
;
512 int64_t i
, total_sectors
;
514 unsigned char sector
[512];
523 if (!bs
->backing_hd
) {
527 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
528 for (i
= 0; i
< total_sectors
;) {
529 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
530 for(j
= 0; j
< n
; j
++) {
531 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
535 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
545 if (drv
->bdrv_make_empty
)
546 return drv
->bdrv_make_empty(bs
);
551 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
556 if (!bdrv_is_inserted(bs
))
562 len
= bdrv_getlength(bs
);
564 if ((offset
+ size
) > len
)
570 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
573 return bdrv_check_byte_request(bs
, sector_num
* 512, nb_sectors
* 512);
576 /* return < 0 if error. See bdrv_write() for the return codes */
577 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
578 uint8_t *buf
, int nb_sectors
)
580 BlockDriver
*drv
= bs
->drv
;
584 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
587 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
590 /* Return < 0 if error. Important errors are:
591 -EIO generic I/O error (may happen for all errors)
592 -ENOMEDIUM No media inserted.
593 -EINVAL Invalid sector number or nb_sectors
594 -EACCES Trying to write a read-only device
596 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
597 const uint8_t *buf
, int nb_sectors
)
599 BlockDriver
*drv
= bs
->drv
;
604 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
607 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
610 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
611 void *buf
, int count1
)
613 uint8_t tmp_buf
[SECTOR_SIZE
];
614 int len
, nb_sectors
, count
;
618 /* first read to align to sector start */
619 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
622 sector_num
= offset
>> SECTOR_BITS
;
624 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
626 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
634 /* read the sectors "in place" */
635 nb_sectors
= count
>> SECTOR_BITS
;
636 if (nb_sectors
> 0) {
637 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
639 sector_num
+= nb_sectors
;
640 len
= nb_sectors
<< SECTOR_BITS
;
645 /* add data from the last sector */
647 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
649 memcpy(buf
, tmp_buf
, count
);
654 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
655 const void *buf
, int count1
)
657 uint8_t tmp_buf
[SECTOR_SIZE
];
658 int len
, nb_sectors
, count
;
662 /* first write to align to sector start */
663 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
666 sector_num
= offset
>> SECTOR_BITS
;
668 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
670 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
671 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
680 /* write the sectors "in place" */
681 nb_sectors
= count
>> SECTOR_BITS
;
682 if (nb_sectors
> 0) {
683 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
685 sector_num
+= nb_sectors
;
686 len
= nb_sectors
<< SECTOR_BITS
;
691 /* add data from the last sector */
693 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
695 memcpy(tmp_buf
, buf
, count
);
696 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
703 * Truncate file to 'offset' bytes (needed only for file protocols)
705 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
707 BlockDriver
*drv
= bs
->drv
;
710 if (!drv
->bdrv_truncate
)
712 return drv
->bdrv_truncate(bs
, offset
);
716 * Length of a file in bytes. Return < 0 if error or unknown.
718 int64_t bdrv_getlength(BlockDriverState
*bs
)
720 BlockDriver
*drv
= bs
->drv
;
723 if (!drv
->bdrv_getlength
) {
725 return bs
->total_sectors
* SECTOR_SIZE
;
727 return drv
->bdrv_getlength(bs
);
730 /* return 0 as number of sectors if no device present or error */
731 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
734 length
= bdrv_getlength(bs
);
738 length
= length
>> SECTOR_BITS
;
739 *nb_sectors_ptr
= length
;
743 uint8_t boot_ind
; /* 0x80 - active */
744 uint8_t head
; /* starting head */
745 uint8_t sector
; /* starting sector */
746 uint8_t cyl
; /* starting cylinder */
747 uint8_t sys_ind
; /* What partition type */
748 uint8_t end_head
; /* end head */
749 uint8_t end_sector
; /* end sector */
750 uint8_t end_cyl
; /* end cylinder */
751 uint32_t start_sect
; /* starting sector counting from 0 */
752 uint32_t nr_sects
; /* nr of sectors in partition */
753 } __attribute__((packed
));
755 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
756 static int guess_disk_lchs(BlockDriverState
*bs
,
757 int *pcylinders
, int *pheads
, int *psectors
)
760 int ret
, i
, heads
, sectors
, cylinders
;
765 buf
= qemu_memalign(512, 512);
769 bdrv_get_geometry(bs
, &nb_sectors
);
771 ret
= bdrv_read(bs
, 0, buf
, 1);
774 /* test msdos magic */
775 if (buf
[510] != 0x55 || buf
[511] != 0xaa) {
779 for(i
= 0; i
< 4; i
++) {
780 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
781 nr_sects
= le32_to_cpu(p
->nr_sects
);
782 if (nr_sects
&& p
->end_head
) {
783 /* We make the assumption that the partition terminates on
784 a cylinder boundary */
785 heads
= p
->end_head
+ 1;
786 sectors
= p
->end_sector
& 63;
789 cylinders
= nb_sectors
/ (heads
* sectors
);
790 if (cylinders
< 1 || cylinders
> 16383)
794 *pcylinders
= cylinders
;
796 printf("guessed geometry: LCHS=%d %d %d\n",
797 cylinders
, heads
, sectors
);
807 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
809 int translation
, lba_detected
= 0;
810 int cylinders
, heads
, secs
;
813 /* if a geometry hint is available, use it */
814 bdrv_get_geometry(bs
, &nb_sectors
);
815 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
816 translation
= bdrv_get_translation_hint(bs
);
817 if (cylinders
!= 0) {
822 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
824 /* if heads > 16, it means that a BIOS LBA
825 translation was active, so the default
826 hardware geometry is OK */
828 goto default_geometry
;
833 /* disable any translation to be in sync with
834 the logical geometry */
835 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
836 bdrv_set_translation_hint(bs
,
837 BIOS_ATA_TRANSLATION_NONE
);
842 /* if no geometry, use a standard physical disk geometry */
843 cylinders
= nb_sectors
/ (16 * 63);
845 if (cylinders
> 16383)
847 else if (cylinders
< 2)
852 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
853 if ((*pcyls
* *pheads
) <= 131072) {
854 bdrv_set_translation_hint(bs
,
855 BIOS_ATA_TRANSLATION_LARGE
);
857 bdrv_set_translation_hint(bs
,
858 BIOS_ATA_TRANSLATION_LBA
);
862 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
866 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
867 int cyls
, int heads
, int secs
)
874 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
877 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
878 type
== BDRV_TYPE_FLOPPY
));
881 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
883 bs
->translation
= translation
;
886 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
887 int *pcyls
, int *pheads
, int *psecs
)
894 int bdrv_get_type_hint(BlockDriverState
*bs
)
899 int bdrv_get_translation_hint(BlockDriverState
*bs
)
901 return bs
->translation
;
904 int bdrv_is_removable(BlockDriverState
*bs
)
906 return bs
->removable
;
909 int bdrv_is_read_only(BlockDriverState
*bs
)
911 return bs
->read_only
;
914 int bdrv_is_sg(BlockDriverState
*bs
)
919 /* XXX: no longer used */
920 void bdrv_set_change_cb(BlockDriverState
*bs
,
921 void (*change_cb
)(void *opaque
), void *opaque
)
923 bs
->change_cb
= change_cb
;
924 bs
->change_opaque
= opaque
;
927 int bdrv_is_encrypted(BlockDriverState
*bs
)
929 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
931 return bs
->encrypted
;
934 int bdrv_key_required(BlockDriverState
*bs
)
936 BlockDriverState
*backing_hd
= bs
->backing_hd
;
938 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
940 return (bs
->encrypted
&& !bs
->valid_key
);
943 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
946 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
947 ret
= bdrv_set_key(bs
->backing_hd
, key
);
953 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
955 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
958 } else if (!bs
->valid_key
) {
960 /* call the change callback now, we skipped it on open */
961 bs
->media_changed
= 1;
963 bs
->change_cb(bs
->change_opaque
);
968 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
973 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
977 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
982 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
983 it(opaque
, drv
->format_name
);
987 BlockDriverState
*bdrv_find(const char *name
)
989 BlockDriverState
*bs
;
991 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
992 if (!strcmp(name
, bs
->device_name
))
998 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1000 BlockDriverState
*bs
;
1002 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1007 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1009 return bs
->device_name
;
1012 void bdrv_flush(BlockDriverState
*bs
)
1016 if (bs
->drv
->bdrv_flush
)
1017 bs
->drv
->bdrv_flush(bs
);
1019 bdrv_flush(bs
->backing_hd
);
1022 void bdrv_flush_all(void)
1024 BlockDriverState
*bs
;
1026 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1027 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1028 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1033 * Returns true iff the specified sector is present in the disk image. Drivers
1034 * not implementing the functionality are assumed to not support backing files,
1035 * hence all their sectors are reported as allocated.
1037 * 'pnum' is set to the number of sectors (including and immediately following
1038 * the specified sector) that are known to be in the same
1039 * allocated/unallocated state.
1041 * 'nb_sectors' is the max value 'pnum' should be set to.
1043 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1047 if (!bs
->drv
->bdrv_is_allocated
) {
1048 if (sector_num
>= bs
->total_sectors
) {
1052 n
= bs
->total_sectors
- sector_num
;
1053 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1056 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1059 void bdrv_info(Monitor
*mon
)
1061 BlockDriverState
*bs
;
1063 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1064 monitor_printf(mon
, "%s:", bs
->device_name
);
1065 monitor_printf(mon
, " type=");
1068 monitor_printf(mon
, "hd");
1070 case BDRV_TYPE_CDROM
:
1071 monitor_printf(mon
, "cdrom");
1073 case BDRV_TYPE_FLOPPY
:
1074 monitor_printf(mon
, "floppy");
1077 monitor_printf(mon
, " removable=%d", bs
->removable
);
1078 if (bs
->removable
) {
1079 monitor_printf(mon
, " locked=%d", bs
->locked
);
1082 monitor_printf(mon
, " file=");
1083 monitor_print_filename(mon
, bs
->filename
);
1084 if (bs
->backing_file
[0] != '\0') {
1085 monitor_printf(mon
, " backing_file=");
1086 monitor_print_filename(mon
, bs
->backing_file
);
1088 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1089 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1090 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1092 monitor_printf(mon
, " [not inserted]");
1094 monitor_printf(mon
, "\n");
1098 /* The "info blockstats" command. */
1099 void bdrv_info_stats(Monitor
*mon
)
1101 BlockDriverState
*bs
;
1103 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1104 monitor_printf(mon
, "%s:"
1105 " rd_bytes=%" PRIu64
1106 " wr_bytes=%" PRIu64
1107 " rd_operations=%" PRIu64
1108 " wr_operations=%" PRIu64
1111 bs
->rd_bytes
, bs
->wr_bytes
,
1112 bs
->rd_ops
, bs
->wr_ops
);
1116 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1118 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1119 return bs
->backing_file
;
1120 else if (bs
->encrypted
)
1121 return bs
->filename
;
1126 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1127 char *filename
, int filename_size
)
1129 if (!bs
->backing_hd
) {
1130 pstrcpy(filename
, filename_size
, "");
1132 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1136 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1137 const uint8_t *buf
, int nb_sectors
)
1139 BlockDriver
*drv
= bs
->drv
;
1142 if (!drv
->bdrv_write_compressed
)
1144 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1147 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1149 BlockDriver
*drv
= bs
->drv
;
1152 if (!drv
->bdrv_get_info
)
1154 memset(bdi
, 0, sizeof(*bdi
));
1155 return drv
->bdrv_get_info(bs
, bdi
);
1158 int bdrv_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
, int64_t pos
, int size
)
1160 BlockDriver
*drv
= bs
->drv
;
1163 if (!drv
->bdrv_put_buffer
)
1165 return drv
->bdrv_put_buffer(bs
, buf
, pos
, size
);
1168 int bdrv_get_buffer(BlockDriverState
*bs
, uint8_t *buf
, int64_t pos
, int size
)
1170 BlockDriver
*drv
= bs
->drv
;
1173 if (!drv
->bdrv_get_buffer
)
1175 return drv
->bdrv_get_buffer(bs
, buf
, pos
, size
);
1178 /**************************************************************/
1179 /* handling of snapshots */
1181 int bdrv_snapshot_create(BlockDriverState
*bs
,
1182 QEMUSnapshotInfo
*sn_info
)
1184 BlockDriver
*drv
= bs
->drv
;
1187 if (!drv
->bdrv_snapshot_create
)
1189 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1192 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1193 const char *snapshot_id
)
1195 BlockDriver
*drv
= bs
->drv
;
1198 if (!drv
->bdrv_snapshot_goto
)
1200 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1203 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1205 BlockDriver
*drv
= bs
->drv
;
1208 if (!drv
->bdrv_snapshot_delete
)
1210 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1213 int bdrv_snapshot_list(BlockDriverState
*bs
,
1214 QEMUSnapshotInfo
**psn_info
)
1216 BlockDriver
*drv
= bs
->drv
;
1219 if (!drv
->bdrv_snapshot_list
)
1221 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1224 #define NB_SUFFIXES 4
1226 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1228 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1233 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1236 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1237 if (size
< (10 * base
)) {
1238 snprintf(buf
, buf_size
, "%0.1f%c",
1239 (double)size
/ base
,
1242 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1243 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1244 ((size
+ (base
>> 1)) / base
),
1254 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1256 char buf1
[128], date_buf
[128], clock_buf
[128];
1266 snprintf(buf
, buf_size
,
1267 "%-10s%-20s%7s%20s%15s",
1268 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1272 ptm
= localtime(&ti
);
1273 strftime(date_buf
, sizeof(date_buf
),
1274 "%Y-%m-%d %H:%M:%S", ptm
);
1276 localtime_r(&ti
, &tm
);
1277 strftime(date_buf
, sizeof(date_buf
),
1278 "%Y-%m-%d %H:%M:%S", &tm
);
1280 secs
= sn
->vm_clock_nsec
/ 1000000000;
1281 snprintf(clock_buf
, sizeof(clock_buf
),
1282 "%02d:%02d:%02d.%03d",
1284 (int)((secs
/ 60) % 60),
1286 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1287 snprintf(buf
, buf_size
,
1288 "%-10s%-20s%7s%20s%15s",
1289 sn
->id_str
, sn
->name
,
1290 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1298 /**************************************************************/
1301 typedef struct VectorTranslationAIOCB
{
1302 BlockDriverAIOCB common
;
1306 BlockDriverAIOCB
*aiocb
;
1307 } VectorTranslationAIOCB
;
1309 static void bdrv_aio_cancel_vector(BlockDriverAIOCB
*_acb
)
1311 VectorTranslationAIOCB
*acb
1312 = container_of(_acb
, VectorTranslationAIOCB
, common
);
1314 bdrv_aio_cancel(acb
->aiocb
);
1317 static void bdrv_aio_rw_vector_cb(void *opaque
, int ret
)
1319 VectorTranslationAIOCB
*s
= (VectorTranslationAIOCB
*)opaque
;
1322 qemu_iovec_from_buffer(s
->iov
, s
->bounce
, s
->iov
->size
);
1324 qemu_vfree(s
->bounce
);
1325 s
->common
.cb(s
->common
.opaque
, ret
);
1326 qemu_aio_release(s
);
1329 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1333 BlockDriverCompletionFunc
*cb
,
1338 VectorTranslationAIOCB
*s
= qemu_aio_get_pool(&vectored_aio_pool
, bs
,
1342 s
->bounce
= qemu_memalign(512, nb_sectors
* 512);
1343 s
->is_write
= is_write
;
1345 qemu_iovec_to_buffer(s
->iov
, s
->bounce
);
1346 s
->aiocb
= bdrv_aio_write(bs
, sector_num
, s
->bounce
, nb_sectors
,
1347 bdrv_aio_rw_vector_cb
, s
);
1349 s
->aiocb
= bdrv_aio_read(bs
, sector_num
, s
->bounce
, nb_sectors
,
1350 bdrv_aio_rw_vector_cb
, s
);
1353 qemu_vfree(s
->bounce
);
1354 qemu_aio_release(s
);
1360 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1361 QEMUIOVector
*iov
, int nb_sectors
,
1362 BlockDriverCompletionFunc
*cb
, void *opaque
)
1364 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1367 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1371 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1372 QEMUIOVector
*iov
, int nb_sectors
,
1373 BlockDriverCompletionFunc
*cb
, void *opaque
)
1375 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1378 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1382 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1383 uint8_t *buf
, int nb_sectors
,
1384 BlockDriverCompletionFunc
*cb
, void *opaque
)
1386 BlockDriver
*drv
= bs
->drv
;
1387 BlockDriverAIOCB
*ret
;
1391 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1394 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1397 /* Update stats even though technically transfer has not happened. */
1398 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1405 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1406 const uint8_t *buf
, int nb_sectors
,
1407 BlockDriverCompletionFunc
*cb
, void *opaque
)
1409 BlockDriver
*drv
= bs
->drv
;
1410 BlockDriverAIOCB
*ret
;
1416 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1419 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1422 /* Update stats even though technically transfer has not happened. */
1423 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1430 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1432 acb
->pool
->cancel(acb
);
1436 /**************************************************************/
1437 /* async block device emulation */
1439 static void bdrv_aio_bh_cb(void *opaque
)
1441 BlockDriverAIOCBSync
*acb
= opaque
;
1442 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1443 qemu_aio_release(acb
);
1446 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1447 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1448 BlockDriverCompletionFunc
*cb
, void *opaque
)
1450 BlockDriverAIOCBSync
*acb
;
1453 acb
= qemu_aio_get(bs
, cb
, opaque
);
1455 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1456 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1458 qemu_bh_schedule(acb
->bh
);
1459 return &acb
->common
;
1462 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1463 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1464 BlockDriverCompletionFunc
*cb
, void *opaque
)
1466 BlockDriverAIOCBSync
*acb
;
1469 acb
= qemu_aio_get(bs
, cb
, opaque
);
1471 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1472 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1474 qemu_bh_schedule(acb
->bh
);
1475 return &acb
->common
;
1478 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1480 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1481 qemu_bh_cancel(acb
->bh
);
1482 qemu_aio_release(acb
);
1485 /**************************************************************/
1486 /* sync block device emulation */
1488 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1490 *(int *)opaque
= ret
;
1493 #define NOT_DONE 0x7fffffff
1495 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1496 uint8_t *buf
, int nb_sectors
)
1499 BlockDriverAIOCB
*acb
;
1501 async_ret
= NOT_DONE
;
1502 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1503 bdrv_rw_em_cb
, &async_ret
);
1507 while (async_ret
== NOT_DONE
) {
1514 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1515 const uint8_t *buf
, int nb_sectors
)
1518 BlockDriverAIOCB
*acb
;
1520 async_ret
= NOT_DONE
;
1521 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1522 bdrv_rw_em_cb
, &async_ret
);
1525 while (async_ret
== NOT_DONE
) {
1531 void bdrv_init(void)
1533 aio_pool_init(&vectored_aio_pool
, sizeof(VectorTranslationAIOCB
),
1534 bdrv_aio_cancel_vector
);
1536 bdrv_register(&bdrv_raw
);
1537 bdrv_register(&bdrv_host_device
);
1539 bdrv_register(&bdrv_cow
);
1541 bdrv_register(&bdrv_qcow
);
1542 bdrv_register(&bdrv_vmdk
);
1543 bdrv_register(&bdrv_cloop
);
1544 bdrv_register(&bdrv_dmg
);
1545 bdrv_register(&bdrv_bochs
);
1546 bdrv_register(&bdrv_vpc
);
1547 bdrv_register(&bdrv_vvfat
);
1548 bdrv_register(&bdrv_qcow2
);
1549 bdrv_register(&bdrv_parallels
);
1550 bdrv_register(&bdrv_nbd
);
1553 void aio_pool_init(AIOPool
*pool
, int aiocb_size
,
1554 void (*cancel
)(BlockDriverAIOCB
*acb
))
1556 pool
->aiocb_size
= aiocb_size
;
1557 pool
->cancel
= cancel
;
1558 pool
->free_aiocb
= NULL
;
1561 void *qemu_aio_get_pool(AIOPool
*pool
, BlockDriverState
*bs
,
1562 BlockDriverCompletionFunc
*cb
, void *opaque
)
1564 BlockDriverAIOCB
*acb
;
1566 if (pool
->free_aiocb
) {
1567 acb
= pool
->free_aiocb
;
1568 pool
->free_aiocb
= acb
->next
;
1570 acb
= qemu_mallocz(pool
->aiocb_size
);
1575 acb
->opaque
= opaque
;
1579 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1582 return qemu_aio_get_pool(&bs
->drv
->aio_pool
, bs
, cb
, opaque
);
1585 void qemu_aio_release(void *p
)
1587 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1588 AIOPool
*pool
= acb
->pool
;
1589 acb
->next
= pool
->free_aiocb
;
1590 pool
->free_aiocb
= acb
;
1593 /**************************************************************/
1594 /* removable device support */
1597 * Return TRUE if the media is present
1599 int bdrv_is_inserted(BlockDriverState
*bs
)
1601 BlockDriver
*drv
= bs
->drv
;
1605 if (!drv
->bdrv_is_inserted
)
1607 ret
= drv
->bdrv_is_inserted(bs
);
1612 * Return TRUE if the media changed since the last call to this
1613 * function. It is currently only used for floppy disks
1615 int bdrv_media_changed(BlockDriverState
*bs
)
1617 BlockDriver
*drv
= bs
->drv
;
1620 if (!drv
|| !drv
->bdrv_media_changed
)
1623 ret
= drv
->bdrv_media_changed(bs
);
1624 if (ret
== -ENOTSUP
)
1625 ret
= bs
->media_changed
;
1626 bs
->media_changed
= 0;
1631 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1633 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1635 BlockDriver
*drv
= bs
->drv
;
1638 if (!drv
|| !drv
->bdrv_eject
) {
1641 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1643 if (ret
== -ENOTSUP
) {
1649 int bdrv_is_locked(BlockDriverState
*bs
)
1655 * Lock or unlock the media (if it is locked, the user won't be able
1656 * to eject it manually).
1658 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1660 BlockDriver
*drv
= bs
->drv
;
1662 bs
->locked
= locked
;
1663 if (drv
&& drv
->bdrv_set_locked
) {
1664 drv
->bdrv_set_locked(bs
, locked
);
1668 /* needed for generic scsi interface */
1670 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1672 BlockDriver
*drv
= bs
->drv
;
1674 if (drv
&& drv
->bdrv_ioctl
)
1675 return drv
->bdrv_ioctl(bs
, req
, buf
);
1679 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
1680 unsigned long int req
, void *buf
,
1681 BlockDriverCompletionFunc
*cb
, void *opaque
)
1683 BlockDriver
*drv
= bs
->drv
;
1685 if (drv
&& drv
->bdrv_aio_ioctl
)
1686 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);