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 typedef struct BlockDriverAIOCBSync
{
52 BlockDriverAIOCB common
;
55 /* vector translation state */
59 } BlockDriverAIOCBSync
;
61 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
62 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
63 BlockDriverCompletionFunc
*cb
, void *opaque
);
64 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
65 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
66 BlockDriverCompletionFunc
*cb
, void *opaque
);
67 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
68 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
69 uint8_t *buf
, int nb_sectors
);
70 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
71 const uint8_t *buf
, int nb_sectors
);
73 BlockDriverState
*bdrv_first
;
75 static BlockDriver
*first_drv
;
77 int path_is_absolute(const char *path
)
81 /* specific case for names like: "\\.\d:" */
82 if (*path
== '/' || *path
== '\\')
85 p
= strchr(path
, ':');
91 return (*p
== '/' || *p
== '\\');
97 /* if filename is absolute, just copy it to dest. Otherwise, build a
98 path to it by considering it is relative to base_path. URL are
100 void path_combine(char *dest
, int dest_size
,
101 const char *base_path
,
102 const char *filename
)
109 if (path_is_absolute(filename
)) {
110 pstrcpy(dest
, dest_size
, filename
);
112 p
= strchr(base_path
, ':');
117 p1
= strrchr(base_path
, '/');
121 p2
= strrchr(base_path
, '\\');
133 if (len
> dest_size
- 1)
135 memcpy(dest
, base_path
, len
);
137 pstrcat(dest
, dest_size
, filename
);
142 static void bdrv_register(BlockDriver
*bdrv
)
144 if (!bdrv
->bdrv_aio_readv
) {
145 /* add AIO emulation layer */
146 bdrv
->bdrv_aio_readv
= bdrv_aio_readv_em
;
147 bdrv
->bdrv_aio_writev
= bdrv_aio_writev_em
;
148 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
149 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
150 } else if (!bdrv
->bdrv_read
) {
151 /* add synchronous IO emulation layer */
152 bdrv
->bdrv_read
= bdrv_read_em
;
153 bdrv
->bdrv_write
= bdrv_write_em
;
155 aio_pool_init(&bdrv
->aio_pool
, bdrv
->aiocb_size
, bdrv
->bdrv_aio_cancel
);
156 bdrv
->next
= first_drv
;
160 /* create a new block device (by default it is empty) */
161 BlockDriverState
*bdrv_new(const char *device_name
)
163 BlockDriverState
**pbs
, *bs
;
165 bs
= qemu_mallocz(sizeof(BlockDriverState
));
166 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
167 if (device_name
[0] != '\0') {
168 /* insert at the end */
177 BlockDriver
*bdrv_find_format(const char *format_name
)
180 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
181 if (!strcmp(drv1
->format_name
, format_name
))
187 int bdrv_create2(BlockDriver
*drv
,
188 const char *filename
, int64_t size_in_sectors
,
189 const char *backing_file
, const char *backing_format
,
192 if (drv
->bdrv_create2
)
193 return drv
->bdrv_create2(filename
, size_in_sectors
, backing_file
,
194 backing_format
, flags
);
195 if (drv
->bdrv_create
)
196 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
,
201 int bdrv_create(BlockDriver
*drv
,
202 const char *filename
, int64_t size_in_sectors
,
203 const char *backing_file
, int flags
)
205 if (!drv
->bdrv_create
)
207 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
211 void get_tmp_filename(char *filename
, int size
)
213 char temp_dir
[MAX_PATH
];
215 GetTempPath(MAX_PATH
, temp_dir
);
216 GetTempFileName(temp_dir
, "qem", 0, filename
);
219 void get_tmp_filename(char *filename
, int size
)
223 /* XXX: race condition possible */
224 tmpdir
= getenv("TMPDIR");
227 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
228 fd
= mkstemp(filename
);
234 static int is_windows_drive_prefix(const char *filename
)
236 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
237 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
241 static int is_windows_drive(const char *filename
)
243 if (is_windows_drive_prefix(filename
) &&
246 if (strstart(filename
, "\\\\.\\", NULL
) ||
247 strstart(filename
, "//./", NULL
))
253 static BlockDriver
*find_protocol(const char *filename
)
261 if (is_windows_drive(filename
) ||
262 is_windows_drive_prefix(filename
))
265 p
= strchr(filename
, ':');
269 if (len
> sizeof(protocol
) - 1)
270 len
= sizeof(protocol
) - 1;
271 memcpy(protocol
, filename
, len
);
272 protocol
[len
] = '\0';
273 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
274 if (drv1
->protocol_name
&&
275 !strcmp(drv1
->protocol_name
, protocol
))
281 /* XXX: force raw format if block or character device ? It would
282 simplify the BSD case */
283 static BlockDriver
*find_image_format(const char *filename
)
285 int ret
, score
, score_max
;
286 BlockDriver
*drv1
, *drv
;
288 BlockDriverState
*bs
;
290 /* detect host devices. By convention, /dev/cdrom[N] is always
291 recognized as a host CDROM */
292 if (strstart(filename
, "/dev/cdrom", NULL
))
293 return &bdrv_host_device
;
295 if (is_windows_drive(filename
))
296 return &bdrv_host_device
;
300 if (stat(filename
, &st
) >= 0 &&
301 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
302 return &bdrv_host_device
;
307 drv
= find_protocol(filename
);
308 /* no need to test disk image formats for vvfat */
309 if (drv
== &bdrv_vvfat
)
312 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
315 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
322 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
323 if (drv1
->bdrv_probe
) {
324 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
325 if (score
> score_max
) {
334 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
336 BlockDriverState
*bs
;
340 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
350 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
352 return bdrv_open2(bs
, filename
, flags
, NULL
);
355 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
359 char tmp_filename
[PATH_MAX
];
360 char backing_filename
[PATH_MAX
];
363 bs
->is_temporary
= 0;
367 if (flags
& BDRV_O_SNAPSHOT
) {
368 BlockDriverState
*bs1
;
372 /* if snapshot, we create a temporary backing file and open it
373 instead of opening 'filename' directly */
375 /* if there is a backing file, use it */
377 ret
= bdrv_open2(bs1
, filename
, 0, drv
);
382 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
384 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
389 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
391 /* Real path is meaningless for protocols */
393 snprintf(backing_filename
, sizeof(backing_filename
),
396 realpath(filename
, backing_filename
);
398 ret
= bdrv_create2(&bdrv_qcow2
, tmp_filename
,
399 total_size
, backing_filename
,
400 (drv
? drv
->format_name
: NULL
), 0);
404 filename
= tmp_filename
;
406 bs
->is_temporary
= 1;
409 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
410 if (flags
& BDRV_O_FILE
) {
411 drv
= find_protocol(filename
);
413 drv
= find_image_format(filename
);
417 goto unlink_and_fail
;
420 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
421 /* Note: for compatibility, we open disk image files as RDWR, and
422 RDONLY as fallback */
423 if (!(flags
& BDRV_O_FILE
))
424 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
426 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
427 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
428 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
429 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
433 qemu_free(bs
->opaque
);
437 if (bs
->is_temporary
)
441 if (drv
->bdrv_getlength
) {
442 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
445 if (bs
->is_temporary
) {
449 if (bs
->backing_file
[0] != '\0') {
450 /* if there is a backing file, use it */
451 BlockDriver
*back_drv
= NULL
;
452 bs
->backing_hd
= bdrv_new("");
453 path_combine(backing_filename
, sizeof(backing_filename
),
454 filename
, bs
->backing_file
);
455 if (bs
->backing_format
[0] != '\0')
456 back_drv
= bdrv_find_format(bs
->backing_format
);
457 ret
= bdrv_open2(bs
->backing_hd
, backing_filename
, open_flags
,
465 if (!bdrv_key_required(bs
)) {
466 /* call the change callback */
467 bs
->media_changed
= 1;
469 bs
->change_cb(bs
->change_opaque
);
474 void bdrv_close(BlockDriverState
*bs
)
478 bdrv_delete(bs
->backing_hd
);
479 bs
->drv
->bdrv_close(bs
);
480 qemu_free(bs
->opaque
);
482 if (bs
->is_temporary
) {
483 unlink(bs
->filename
);
489 /* call the change callback */
490 bs
->media_changed
= 1;
492 bs
->change_cb(bs
->change_opaque
);
496 void bdrv_delete(BlockDriverState
*bs
)
498 BlockDriverState
**pbs
;
501 while (*pbs
!= bs
&& *pbs
!= NULL
)
510 /* commit COW file into the raw image */
511 int bdrv_commit(BlockDriverState
*bs
)
513 BlockDriver
*drv
= bs
->drv
;
514 int64_t i
, total_sectors
;
516 unsigned char sector
[512];
525 if (!bs
->backing_hd
) {
529 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
530 for (i
= 0; i
< total_sectors
;) {
531 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
532 for(j
= 0; j
< n
; j
++) {
533 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
537 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
547 if (drv
->bdrv_make_empty
)
548 return drv
->bdrv_make_empty(bs
);
553 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
558 if (!bdrv_is_inserted(bs
))
564 len
= bdrv_getlength(bs
);
566 if ((offset
+ size
) > len
)
572 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
575 return bdrv_check_byte_request(bs
, sector_num
* 512, nb_sectors
* 512);
578 /* return < 0 if error. See bdrv_write() for the return codes */
579 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
580 uint8_t *buf
, int nb_sectors
)
582 BlockDriver
*drv
= bs
->drv
;
586 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
589 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
592 /* Return < 0 if error. Important errors are:
593 -EIO generic I/O error (may happen for all errors)
594 -ENOMEDIUM No media inserted.
595 -EINVAL Invalid sector number or nb_sectors
596 -EACCES Trying to write a read-only device
598 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
599 const uint8_t *buf
, int nb_sectors
)
601 BlockDriver
*drv
= bs
->drv
;
606 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
609 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
612 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
613 void *buf
, int count1
)
615 uint8_t tmp_buf
[SECTOR_SIZE
];
616 int len
, nb_sectors
, count
;
620 /* first read to align to sector start */
621 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
624 sector_num
= offset
>> SECTOR_BITS
;
626 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
628 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
636 /* read the sectors "in place" */
637 nb_sectors
= count
>> SECTOR_BITS
;
638 if (nb_sectors
> 0) {
639 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
641 sector_num
+= nb_sectors
;
642 len
= nb_sectors
<< SECTOR_BITS
;
647 /* add data from the last sector */
649 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
651 memcpy(buf
, tmp_buf
, count
);
656 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
657 const void *buf
, int count1
)
659 uint8_t tmp_buf
[SECTOR_SIZE
];
660 int len
, nb_sectors
, count
;
664 /* first write to align to sector start */
665 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
668 sector_num
= offset
>> SECTOR_BITS
;
670 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
672 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
673 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
682 /* write the sectors "in place" */
683 nb_sectors
= count
>> SECTOR_BITS
;
684 if (nb_sectors
> 0) {
685 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
687 sector_num
+= nb_sectors
;
688 len
= nb_sectors
<< SECTOR_BITS
;
693 /* add data from the last sector */
695 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
697 memcpy(tmp_buf
, buf
, count
);
698 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
705 * Truncate file to 'offset' bytes (needed only for file protocols)
707 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
709 BlockDriver
*drv
= bs
->drv
;
712 if (!drv
->bdrv_truncate
)
714 return drv
->bdrv_truncate(bs
, offset
);
718 * Length of a file in bytes. Return < 0 if error or unknown.
720 int64_t bdrv_getlength(BlockDriverState
*bs
)
722 BlockDriver
*drv
= bs
->drv
;
725 if (!drv
->bdrv_getlength
) {
727 return bs
->total_sectors
* SECTOR_SIZE
;
729 return drv
->bdrv_getlength(bs
);
732 /* return 0 as number of sectors if no device present or error */
733 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
736 length
= bdrv_getlength(bs
);
740 length
= length
>> SECTOR_BITS
;
741 *nb_sectors_ptr
= length
;
745 uint8_t boot_ind
; /* 0x80 - active */
746 uint8_t head
; /* starting head */
747 uint8_t sector
; /* starting sector */
748 uint8_t cyl
; /* starting cylinder */
749 uint8_t sys_ind
; /* What partition type */
750 uint8_t end_head
; /* end head */
751 uint8_t end_sector
; /* end sector */
752 uint8_t end_cyl
; /* end cylinder */
753 uint32_t start_sect
; /* starting sector counting from 0 */
754 uint32_t nr_sects
; /* nr of sectors in partition */
755 } __attribute__((packed
));
757 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
758 static int guess_disk_lchs(BlockDriverState
*bs
,
759 int *pcylinders
, int *pheads
, int *psectors
)
762 int ret
, i
, heads
, sectors
, cylinders
;
767 buf
= qemu_memalign(512, 512);
771 bdrv_get_geometry(bs
, &nb_sectors
);
773 ret
= bdrv_read(bs
, 0, buf
, 1);
776 /* test msdos magic */
777 if (buf
[510] != 0x55 || buf
[511] != 0xaa) {
781 for(i
= 0; i
< 4; i
++) {
782 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
783 nr_sects
= le32_to_cpu(p
->nr_sects
);
784 if (nr_sects
&& p
->end_head
) {
785 /* We make the assumption that the partition terminates on
786 a cylinder boundary */
787 heads
= p
->end_head
+ 1;
788 sectors
= p
->end_sector
& 63;
791 cylinders
= nb_sectors
/ (heads
* sectors
);
792 if (cylinders
< 1 || cylinders
> 16383)
796 *pcylinders
= cylinders
;
798 printf("guessed geometry: LCHS=%d %d %d\n",
799 cylinders
, heads
, sectors
);
809 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
811 int translation
, lba_detected
= 0;
812 int cylinders
, heads
, secs
;
815 /* if a geometry hint is available, use it */
816 bdrv_get_geometry(bs
, &nb_sectors
);
817 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
818 translation
= bdrv_get_translation_hint(bs
);
819 if (cylinders
!= 0) {
824 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
826 /* if heads > 16, it means that a BIOS LBA
827 translation was active, so the default
828 hardware geometry is OK */
830 goto default_geometry
;
835 /* disable any translation to be in sync with
836 the logical geometry */
837 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
838 bdrv_set_translation_hint(bs
,
839 BIOS_ATA_TRANSLATION_NONE
);
844 /* if no geometry, use a standard physical disk geometry */
845 cylinders
= nb_sectors
/ (16 * 63);
847 if (cylinders
> 16383)
849 else if (cylinders
< 2)
854 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
855 if ((*pcyls
* *pheads
) <= 131072) {
856 bdrv_set_translation_hint(bs
,
857 BIOS_ATA_TRANSLATION_LARGE
);
859 bdrv_set_translation_hint(bs
,
860 BIOS_ATA_TRANSLATION_LBA
);
864 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
868 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
869 int cyls
, int heads
, int secs
)
876 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
879 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
880 type
== BDRV_TYPE_FLOPPY
));
883 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
885 bs
->translation
= translation
;
888 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
889 int *pcyls
, int *pheads
, int *psecs
)
896 int bdrv_get_type_hint(BlockDriverState
*bs
)
901 int bdrv_get_translation_hint(BlockDriverState
*bs
)
903 return bs
->translation
;
906 int bdrv_is_removable(BlockDriverState
*bs
)
908 return bs
->removable
;
911 int bdrv_is_read_only(BlockDriverState
*bs
)
913 return bs
->read_only
;
916 int bdrv_is_sg(BlockDriverState
*bs
)
921 /* XXX: no longer used */
922 void bdrv_set_change_cb(BlockDriverState
*bs
,
923 void (*change_cb
)(void *opaque
), void *opaque
)
925 bs
->change_cb
= change_cb
;
926 bs
->change_opaque
= opaque
;
929 int bdrv_is_encrypted(BlockDriverState
*bs
)
931 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
933 return bs
->encrypted
;
936 int bdrv_key_required(BlockDriverState
*bs
)
938 BlockDriverState
*backing_hd
= bs
->backing_hd
;
940 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
942 return (bs
->encrypted
&& !bs
->valid_key
);
945 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
948 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
949 ret
= bdrv_set_key(bs
->backing_hd
, key
);
955 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
957 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
960 } else if (!bs
->valid_key
) {
962 /* call the change callback now, we skipped it on open */
963 bs
->media_changed
= 1;
965 bs
->change_cb(bs
->change_opaque
);
970 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
975 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
979 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
984 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
985 it(opaque
, drv
->format_name
);
989 BlockDriverState
*bdrv_find(const char *name
)
991 BlockDriverState
*bs
;
993 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
994 if (!strcmp(name
, bs
->device_name
))
1000 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1002 BlockDriverState
*bs
;
1004 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1009 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1011 return bs
->device_name
;
1014 void bdrv_flush(BlockDriverState
*bs
)
1018 if (bs
->drv
->bdrv_flush
)
1019 bs
->drv
->bdrv_flush(bs
);
1021 bdrv_flush(bs
->backing_hd
);
1024 void bdrv_flush_all(void)
1026 BlockDriverState
*bs
;
1028 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1029 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1030 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1035 * Returns true iff the specified sector is present in the disk image. Drivers
1036 * not implementing the functionality are assumed to not support backing files,
1037 * hence all their sectors are reported as allocated.
1039 * 'pnum' is set to the number of sectors (including and immediately following
1040 * the specified sector) that are known to be in the same
1041 * allocated/unallocated state.
1043 * 'nb_sectors' is the max value 'pnum' should be set to.
1045 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1049 if (!bs
->drv
->bdrv_is_allocated
) {
1050 if (sector_num
>= bs
->total_sectors
) {
1054 n
= bs
->total_sectors
- sector_num
;
1055 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1058 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1061 void bdrv_info(Monitor
*mon
)
1063 BlockDriverState
*bs
;
1065 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1066 monitor_printf(mon
, "%s:", bs
->device_name
);
1067 monitor_printf(mon
, " type=");
1070 monitor_printf(mon
, "hd");
1072 case BDRV_TYPE_CDROM
:
1073 monitor_printf(mon
, "cdrom");
1075 case BDRV_TYPE_FLOPPY
:
1076 monitor_printf(mon
, "floppy");
1079 monitor_printf(mon
, " removable=%d", bs
->removable
);
1080 if (bs
->removable
) {
1081 monitor_printf(mon
, " locked=%d", bs
->locked
);
1084 monitor_printf(mon
, " file=");
1085 monitor_print_filename(mon
, bs
->filename
);
1086 if (bs
->backing_file
[0] != '\0') {
1087 monitor_printf(mon
, " backing_file=");
1088 monitor_print_filename(mon
, bs
->backing_file
);
1090 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1091 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1092 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1094 monitor_printf(mon
, " [not inserted]");
1096 monitor_printf(mon
, "\n");
1100 /* The "info blockstats" command. */
1101 void bdrv_info_stats(Monitor
*mon
)
1103 BlockDriverState
*bs
;
1105 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1106 monitor_printf(mon
, "%s:"
1107 " rd_bytes=%" PRIu64
1108 " wr_bytes=%" PRIu64
1109 " rd_operations=%" PRIu64
1110 " wr_operations=%" PRIu64
1113 bs
->rd_bytes
, bs
->wr_bytes
,
1114 bs
->rd_ops
, bs
->wr_ops
);
1118 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1120 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1121 return bs
->backing_file
;
1122 else if (bs
->encrypted
)
1123 return bs
->filename
;
1128 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1129 char *filename
, int filename_size
)
1131 if (!bs
->backing_hd
) {
1132 pstrcpy(filename
, filename_size
, "");
1134 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1138 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1139 const uint8_t *buf
, int nb_sectors
)
1141 BlockDriver
*drv
= bs
->drv
;
1144 if (!drv
->bdrv_write_compressed
)
1146 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1149 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1151 BlockDriver
*drv
= bs
->drv
;
1154 if (!drv
->bdrv_get_info
)
1156 memset(bdi
, 0, sizeof(*bdi
));
1157 return drv
->bdrv_get_info(bs
, bdi
);
1160 int bdrv_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
, int64_t pos
, int size
)
1162 BlockDriver
*drv
= bs
->drv
;
1165 if (!drv
->bdrv_put_buffer
)
1167 return drv
->bdrv_put_buffer(bs
, buf
, pos
, size
);
1170 int bdrv_get_buffer(BlockDriverState
*bs
, uint8_t *buf
, int64_t pos
, int size
)
1172 BlockDriver
*drv
= bs
->drv
;
1175 if (!drv
->bdrv_get_buffer
)
1177 return drv
->bdrv_get_buffer(bs
, buf
, pos
, size
);
1180 /**************************************************************/
1181 /* handling of snapshots */
1183 int bdrv_snapshot_create(BlockDriverState
*bs
,
1184 QEMUSnapshotInfo
*sn_info
)
1186 BlockDriver
*drv
= bs
->drv
;
1189 if (!drv
->bdrv_snapshot_create
)
1191 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1194 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1195 const char *snapshot_id
)
1197 BlockDriver
*drv
= bs
->drv
;
1200 if (!drv
->bdrv_snapshot_goto
)
1202 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1205 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1207 BlockDriver
*drv
= bs
->drv
;
1210 if (!drv
->bdrv_snapshot_delete
)
1212 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1215 int bdrv_snapshot_list(BlockDriverState
*bs
,
1216 QEMUSnapshotInfo
**psn_info
)
1218 BlockDriver
*drv
= bs
->drv
;
1221 if (!drv
->bdrv_snapshot_list
)
1223 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1226 #define NB_SUFFIXES 4
1228 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1230 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1235 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1238 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1239 if (size
< (10 * base
)) {
1240 snprintf(buf
, buf_size
, "%0.1f%c",
1241 (double)size
/ base
,
1244 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1245 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1246 ((size
+ (base
>> 1)) / base
),
1256 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1258 char buf1
[128], date_buf
[128], clock_buf
[128];
1268 snprintf(buf
, buf_size
,
1269 "%-10s%-20s%7s%20s%15s",
1270 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1274 ptm
= localtime(&ti
);
1275 strftime(date_buf
, sizeof(date_buf
),
1276 "%Y-%m-%d %H:%M:%S", ptm
);
1278 localtime_r(&ti
, &tm
);
1279 strftime(date_buf
, sizeof(date_buf
),
1280 "%Y-%m-%d %H:%M:%S", &tm
);
1282 secs
= sn
->vm_clock_nsec
/ 1000000000;
1283 snprintf(clock_buf
, sizeof(clock_buf
),
1284 "%02d:%02d:%02d.%03d",
1286 (int)((secs
/ 60) % 60),
1288 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1289 snprintf(buf
, buf_size
,
1290 "%-10s%-20s%7s%20s%15s",
1291 sn
->id_str
, sn
->name
,
1292 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1300 /**************************************************************/
1303 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1304 QEMUIOVector
*qiov
, int nb_sectors
,
1305 BlockDriverCompletionFunc
*cb
, void *opaque
)
1307 BlockDriver
*drv
= bs
->drv
;
1308 BlockDriverAIOCB
*ret
;
1312 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1315 ret
= drv
->bdrv_aio_readv(bs
, sector_num
, qiov
, nb_sectors
,
1319 /* Update stats even though technically transfer has not happened. */
1320 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1327 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1328 QEMUIOVector
*qiov
, int nb_sectors
,
1329 BlockDriverCompletionFunc
*cb
, void *opaque
)
1331 BlockDriver
*drv
= bs
->drv
;
1332 BlockDriverAIOCB
*ret
;
1338 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1341 ret
= drv
->bdrv_aio_writev(bs
, sector_num
, qiov
, nb_sectors
,
1345 /* Update stats even though technically transfer has not happened. */
1346 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1353 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1355 acb
->pool
->cancel(acb
);
1359 /**************************************************************/
1360 /* async block device emulation */
1362 static void bdrv_aio_bh_cb(void *opaque
)
1364 BlockDriverAIOCBSync
*acb
= opaque
;
1367 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
1368 qemu_vfree(acb
->bounce
);
1369 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1371 qemu_aio_release(acb
);
1374 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1378 BlockDriverCompletionFunc
*cb
,
1383 BlockDriverAIOCBSync
*acb
;
1385 acb
= qemu_aio_get(bs
, cb
, opaque
);
1386 acb
->is_write
= is_write
;
1388 acb
->bounce
= qemu_memalign(512, qiov
->size
);
1391 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1394 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
1395 acb
->ret
= bdrv_write(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1397 acb
->ret
= bdrv_read(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1400 qemu_bh_schedule(acb
->bh
);
1402 return &acb
->common
;
1405 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
1406 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1407 BlockDriverCompletionFunc
*cb
, void *opaque
)
1409 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
1412 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
1413 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1414 BlockDriverCompletionFunc
*cb
, void *opaque
)
1416 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
1420 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1422 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1423 qemu_bh_cancel(acb
->bh
);
1424 qemu_aio_release(acb
);
1427 /**************************************************************/
1428 /* sync block device emulation */
1430 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1432 *(int *)opaque
= ret
;
1435 #define NOT_DONE 0x7fffffff
1437 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1438 uint8_t *buf
, int nb_sectors
)
1441 BlockDriverAIOCB
*acb
;
1445 async_ret
= NOT_DONE
;
1447 iov
.iov_len
= nb_sectors
* 512;
1448 qemu_iovec_init_external(&qiov
, &iov
, 1);
1449 acb
= bdrv_aio_readv(bs
, sector_num
, &qiov
, nb_sectors
,
1450 bdrv_rw_em_cb
, &async_ret
);
1454 while (async_ret
== NOT_DONE
) {
1461 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1462 const uint8_t *buf
, int nb_sectors
)
1465 BlockDriverAIOCB
*acb
;
1469 async_ret
= NOT_DONE
;
1470 iov
.iov_base
= (void *)buf
;
1471 iov
.iov_len
= nb_sectors
* 512;
1472 qemu_iovec_init_external(&qiov
, &iov
, 1);
1473 acb
= bdrv_aio_writev(bs
, sector_num
, &qiov
, nb_sectors
,
1474 bdrv_rw_em_cb
, &async_ret
);
1477 while (async_ret
== NOT_DONE
) {
1483 void bdrv_init(void)
1485 bdrv_register(&bdrv_raw
);
1486 bdrv_register(&bdrv_host_device
);
1488 bdrv_register(&bdrv_cow
);
1490 bdrv_register(&bdrv_qcow
);
1491 bdrv_register(&bdrv_vmdk
);
1492 bdrv_register(&bdrv_cloop
);
1493 bdrv_register(&bdrv_dmg
);
1494 bdrv_register(&bdrv_bochs
);
1495 bdrv_register(&bdrv_vpc
);
1496 bdrv_register(&bdrv_vvfat
);
1497 bdrv_register(&bdrv_qcow2
);
1498 bdrv_register(&bdrv_parallels
);
1499 bdrv_register(&bdrv_nbd
);
1502 void aio_pool_init(AIOPool
*pool
, int aiocb_size
,
1503 void (*cancel
)(BlockDriverAIOCB
*acb
))
1505 pool
->aiocb_size
= aiocb_size
;
1506 pool
->cancel
= cancel
;
1507 pool
->free_aiocb
= NULL
;
1510 void *qemu_aio_get_pool(AIOPool
*pool
, BlockDriverState
*bs
,
1511 BlockDriverCompletionFunc
*cb
, void *opaque
)
1513 BlockDriverAIOCB
*acb
;
1515 if (pool
->free_aiocb
) {
1516 acb
= pool
->free_aiocb
;
1517 pool
->free_aiocb
= acb
->next
;
1519 acb
= qemu_mallocz(pool
->aiocb_size
);
1524 acb
->opaque
= opaque
;
1528 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1531 return qemu_aio_get_pool(&bs
->drv
->aio_pool
, bs
, cb
, opaque
);
1534 void qemu_aio_release(void *p
)
1536 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1537 AIOPool
*pool
= acb
->pool
;
1538 acb
->next
= pool
->free_aiocb
;
1539 pool
->free_aiocb
= acb
;
1542 /**************************************************************/
1543 /* removable device support */
1546 * Return TRUE if the media is present
1548 int bdrv_is_inserted(BlockDriverState
*bs
)
1550 BlockDriver
*drv
= bs
->drv
;
1554 if (!drv
->bdrv_is_inserted
)
1556 ret
= drv
->bdrv_is_inserted(bs
);
1561 * Return TRUE if the media changed since the last call to this
1562 * function. It is currently only used for floppy disks
1564 int bdrv_media_changed(BlockDriverState
*bs
)
1566 BlockDriver
*drv
= bs
->drv
;
1569 if (!drv
|| !drv
->bdrv_media_changed
)
1572 ret
= drv
->bdrv_media_changed(bs
);
1573 if (ret
== -ENOTSUP
)
1574 ret
= bs
->media_changed
;
1575 bs
->media_changed
= 0;
1580 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1582 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1584 BlockDriver
*drv
= bs
->drv
;
1587 if (!drv
|| !drv
->bdrv_eject
) {
1590 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1592 if (ret
== -ENOTSUP
) {
1598 int bdrv_is_locked(BlockDriverState
*bs
)
1604 * Lock or unlock the media (if it is locked, the user won't be able
1605 * to eject it manually).
1607 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1609 BlockDriver
*drv
= bs
->drv
;
1611 bs
->locked
= locked
;
1612 if (drv
&& drv
->bdrv_set_locked
) {
1613 drv
->bdrv_set_locked(bs
, locked
);
1617 /* needed for generic scsi interface */
1619 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1621 BlockDriver
*drv
= bs
->drv
;
1623 if (drv
&& drv
->bdrv_ioctl
)
1624 return drv
->bdrv_ioctl(bs
, req
, buf
);
1628 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
1629 unsigned long int req
, void *buf
,
1630 BlockDriverCompletionFunc
*cb
, void *opaque
)
1632 BlockDriver
*drv
= bs
->drv
;
1634 if (drv
&& drv
->bdrv_aio_ioctl
)
1635 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);