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
,
575 /* Deal with byte accesses */
577 offset
= -sector_num
;
579 offset
= sector_num
* 512;
581 return bdrv_check_byte_request(bs
, offset
, nb_sectors
* 512);
584 /* return < 0 if error. See bdrv_write() for the return codes */
585 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
586 uint8_t *buf
, int nb_sectors
)
588 BlockDriver
*drv
= bs
->drv
;
592 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
595 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
598 /* Return < 0 if error. Important errors are:
599 -EIO generic I/O error (may happen for all errors)
600 -ENOMEDIUM No media inserted.
601 -EINVAL Invalid sector number or nb_sectors
602 -EACCES Trying to write a read-only device
604 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
605 const uint8_t *buf
, int nb_sectors
)
607 BlockDriver
*drv
= bs
->drv
;
612 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
615 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
618 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
619 void *buf
, int count1
)
621 uint8_t tmp_buf
[SECTOR_SIZE
];
622 int len
, nb_sectors
, count
;
626 /* first read to align to sector start */
627 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
630 sector_num
= offset
>> SECTOR_BITS
;
632 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
634 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
642 /* read the sectors "in place" */
643 nb_sectors
= count
>> SECTOR_BITS
;
644 if (nb_sectors
> 0) {
645 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
647 sector_num
+= nb_sectors
;
648 len
= nb_sectors
<< SECTOR_BITS
;
653 /* add data from the last sector */
655 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
657 memcpy(buf
, tmp_buf
, count
);
662 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
663 const void *buf
, int count1
)
665 uint8_t tmp_buf
[SECTOR_SIZE
];
666 int len
, nb_sectors
, count
;
670 /* first write to align to sector start */
671 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
674 sector_num
= offset
>> SECTOR_BITS
;
676 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
678 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
679 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
688 /* write the sectors "in place" */
689 nb_sectors
= count
>> SECTOR_BITS
;
690 if (nb_sectors
> 0) {
691 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
693 sector_num
+= nb_sectors
;
694 len
= nb_sectors
<< SECTOR_BITS
;
699 /* add data from the last sector */
701 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
703 memcpy(tmp_buf
, buf
, count
);
704 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
711 * Truncate file to 'offset' bytes (needed only for file protocols)
713 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
715 BlockDriver
*drv
= bs
->drv
;
718 if (!drv
->bdrv_truncate
)
720 return drv
->bdrv_truncate(bs
, offset
);
724 * Length of a file in bytes. Return < 0 if error or unknown.
726 int64_t bdrv_getlength(BlockDriverState
*bs
)
728 BlockDriver
*drv
= bs
->drv
;
731 if (!drv
->bdrv_getlength
) {
733 return bs
->total_sectors
* SECTOR_SIZE
;
735 return drv
->bdrv_getlength(bs
);
738 /* return 0 as number of sectors if no device present or error */
739 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
742 length
= bdrv_getlength(bs
);
746 length
= length
>> SECTOR_BITS
;
747 *nb_sectors_ptr
= length
;
751 uint8_t boot_ind
; /* 0x80 - active */
752 uint8_t head
; /* starting head */
753 uint8_t sector
; /* starting sector */
754 uint8_t cyl
; /* starting cylinder */
755 uint8_t sys_ind
; /* What partition type */
756 uint8_t end_head
; /* end head */
757 uint8_t end_sector
; /* end sector */
758 uint8_t end_cyl
; /* end cylinder */
759 uint32_t start_sect
; /* starting sector counting from 0 */
760 uint32_t nr_sects
; /* nr of sectors in partition */
761 } __attribute__((packed
));
763 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
764 static int guess_disk_lchs(BlockDriverState
*bs
,
765 int *pcylinders
, int *pheads
, int *psectors
)
768 int ret
, i
, heads
, sectors
, cylinders
;
773 buf
= qemu_memalign(512, 512);
777 bdrv_get_geometry(bs
, &nb_sectors
);
779 ret
= bdrv_read(bs
, 0, buf
, 1);
782 /* test msdos magic */
783 if (buf
[510] != 0x55 || buf
[511] != 0xaa) {
787 for(i
= 0; i
< 4; i
++) {
788 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
789 nr_sects
= le32_to_cpu(p
->nr_sects
);
790 if (nr_sects
&& p
->end_head
) {
791 /* We make the assumption that the partition terminates on
792 a cylinder boundary */
793 heads
= p
->end_head
+ 1;
794 sectors
= p
->end_sector
& 63;
797 cylinders
= nb_sectors
/ (heads
* sectors
);
798 if (cylinders
< 1 || cylinders
> 16383)
802 *pcylinders
= cylinders
;
804 printf("guessed geometry: LCHS=%d %d %d\n",
805 cylinders
, heads
, sectors
);
815 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
817 int translation
, lba_detected
= 0;
818 int cylinders
, heads
, secs
;
821 /* if a geometry hint is available, use it */
822 bdrv_get_geometry(bs
, &nb_sectors
);
823 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
824 translation
= bdrv_get_translation_hint(bs
);
825 if (cylinders
!= 0) {
830 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
832 /* if heads > 16, it means that a BIOS LBA
833 translation was active, so the default
834 hardware geometry is OK */
836 goto default_geometry
;
841 /* disable any translation to be in sync with
842 the logical geometry */
843 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
844 bdrv_set_translation_hint(bs
,
845 BIOS_ATA_TRANSLATION_NONE
);
850 /* if no geometry, use a standard physical disk geometry */
851 cylinders
= nb_sectors
/ (16 * 63);
853 if (cylinders
> 16383)
855 else if (cylinders
< 2)
860 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
861 if ((*pcyls
* *pheads
) <= 131072) {
862 bdrv_set_translation_hint(bs
,
863 BIOS_ATA_TRANSLATION_LARGE
);
865 bdrv_set_translation_hint(bs
,
866 BIOS_ATA_TRANSLATION_LBA
);
870 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
874 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
875 int cyls
, int heads
, int secs
)
882 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
885 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
886 type
== BDRV_TYPE_FLOPPY
));
889 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
891 bs
->translation
= translation
;
894 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
895 int *pcyls
, int *pheads
, int *psecs
)
902 int bdrv_get_type_hint(BlockDriverState
*bs
)
907 int bdrv_get_translation_hint(BlockDriverState
*bs
)
909 return bs
->translation
;
912 int bdrv_is_removable(BlockDriverState
*bs
)
914 return bs
->removable
;
917 int bdrv_is_read_only(BlockDriverState
*bs
)
919 return bs
->read_only
;
922 int bdrv_is_sg(BlockDriverState
*bs
)
927 /* XXX: no longer used */
928 void bdrv_set_change_cb(BlockDriverState
*bs
,
929 void (*change_cb
)(void *opaque
), void *opaque
)
931 bs
->change_cb
= change_cb
;
932 bs
->change_opaque
= opaque
;
935 int bdrv_is_encrypted(BlockDriverState
*bs
)
937 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
939 return bs
->encrypted
;
942 int bdrv_key_required(BlockDriverState
*bs
)
944 BlockDriverState
*backing_hd
= bs
->backing_hd
;
946 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
948 return (bs
->encrypted
&& !bs
->valid_key
);
951 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
954 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
955 ret
= bdrv_set_key(bs
->backing_hd
, key
);
961 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
963 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
966 } else if (!bs
->valid_key
) {
968 /* call the change callback now, we skipped it on open */
969 bs
->media_changed
= 1;
971 bs
->change_cb(bs
->change_opaque
);
976 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
981 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
985 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
990 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
991 it(opaque
, drv
->format_name
);
995 BlockDriverState
*bdrv_find(const char *name
)
997 BlockDriverState
*bs
;
999 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1000 if (!strcmp(name
, bs
->device_name
))
1006 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1008 BlockDriverState
*bs
;
1010 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1015 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1017 return bs
->device_name
;
1020 void bdrv_flush(BlockDriverState
*bs
)
1022 if (bs
->drv
->bdrv_flush
)
1023 bs
->drv
->bdrv_flush(bs
);
1025 bdrv_flush(bs
->backing_hd
);
1028 void bdrv_flush_all(void)
1030 BlockDriverState
*bs
;
1032 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1033 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1034 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1039 * Returns true iff the specified sector is present in the disk image. Drivers
1040 * not implementing the functionality are assumed to not support backing files,
1041 * hence all their sectors are reported as allocated.
1043 * 'pnum' is set to the number of sectors (including and immediately following
1044 * the specified sector) that are known to be in the same
1045 * allocated/unallocated state.
1047 * 'nb_sectors' is the max value 'pnum' should be set to.
1049 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1053 if (!bs
->drv
->bdrv_is_allocated
) {
1054 if (sector_num
>= bs
->total_sectors
) {
1058 n
= bs
->total_sectors
- sector_num
;
1059 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1062 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1065 void bdrv_info(Monitor
*mon
)
1067 BlockDriverState
*bs
;
1069 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1070 monitor_printf(mon
, "%s:", bs
->device_name
);
1071 monitor_printf(mon
, " type=");
1074 monitor_printf(mon
, "hd");
1076 case BDRV_TYPE_CDROM
:
1077 monitor_printf(mon
, "cdrom");
1079 case BDRV_TYPE_FLOPPY
:
1080 monitor_printf(mon
, "floppy");
1083 monitor_printf(mon
, " removable=%d", bs
->removable
);
1084 if (bs
->removable
) {
1085 monitor_printf(mon
, " locked=%d", bs
->locked
);
1088 monitor_printf(mon
, " file=");
1089 monitor_print_filename(mon
, bs
->filename
);
1090 if (bs
->backing_file
[0] != '\0') {
1091 monitor_printf(mon
, " backing_file=");
1092 monitor_print_filename(mon
, bs
->backing_file
);
1094 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1095 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1096 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1098 monitor_printf(mon
, " [not inserted]");
1100 monitor_printf(mon
, "\n");
1104 /* The "info blockstats" command. */
1105 void bdrv_info_stats(Monitor
*mon
)
1107 BlockDriverState
*bs
;
1109 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1110 monitor_printf(mon
, "%s:"
1111 " rd_bytes=%" PRIu64
1112 " wr_bytes=%" PRIu64
1113 " rd_operations=%" PRIu64
1114 " wr_operations=%" PRIu64
1117 bs
->rd_bytes
, bs
->wr_bytes
,
1118 bs
->rd_ops
, bs
->wr_ops
);
1122 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1124 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1125 return bs
->backing_file
;
1126 else if (bs
->encrypted
)
1127 return bs
->filename
;
1132 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1133 char *filename
, int filename_size
)
1135 if (!bs
->backing_hd
) {
1136 pstrcpy(filename
, filename_size
, "");
1138 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1142 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1143 const uint8_t *buf
, int nb_sectors
)
1145 BlockDriver
*drv
= bs
->drv
;
1148 if (!drv
->bdrv_write_compressed
)
1150 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1153 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1155 BlockDriver
*drv
= bs
->drv
;
1158 if (!drv
->bdrv_get_info
)
1160 memset(bdi
, 0, sizeof(*bdi
));
1161 return drv
->bdrv_get_info(bs
, bdi
);
1164 /**************************************************************/
1165 /* handling of snapshots */
1167 int bdrv_snapshot_create(BlockDriverState
*bs
,
1168 QEMUSnapshotInfo
*sn_info
)
1170 BlockDriver
*drv
= bs
->drv
;
1173 if (!drv
->bdrv_snapshot_create
)
1175 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1178 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1179 const char *snapshot_id
)
1181 BlockDriver
*drv
= bs
->drv
;
1184 if (!drv
->bdrv_snapshot_goto
)
1186 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1189 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1191 BlockDriver
*drv
= bs
->drv
;
1194 if (!drv
->bdrv_snapshot_delete
)
1196 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1199 int bdrv_snapshot_list(BlockDriverState
*bs
,
1200 QEMUSnapshotInfo
**psn_info
)
1202 BlockDriver
*drv
= bs
->drv
;
1205 if (!drv
->bdrv_snapshot_list
)
1207 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1210 #define NB_SUFFIXES 4
1212 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1214 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1219 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1222 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1223 if (size
< (10 * base
)) {
1224 snprintf(buf
, buf_size
, "%0.1f%c",
1225 (double)size
/ base
,
1228 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1229 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1230 ((size
+ (base
>> 1)) / base
),
1240 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1242 char buf1
[128], date_buf
[128], clock_buf
[128];
1252 snprintf(buf
, buf_size
,
1253 "%-10s%-20s%7s%20s%15s",
1254 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1258 ptm
= localtime(&ti
);
1259 strftime(date_buf
, sizeof(date_buf
),
1260 "%Y-%m-%d %H:%M:%S", ptm
);
1262 localtime_r(&ti
, &tm
);
1263 strftime(date_buf
, sizeof(date_buf
),
1264 "%Y-%m-%d %H:%M:%S", &tm
);
1266 secs
= sn
->vm_clock_nsec
/ 1000000000;
1267 snprintf(clock_buf
, sizeof(clock_buf
),
1268 "%02d:%02d:%02d.%03d",
1270 (int)((secs
/ 60) % 60),
1272 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1273 snprintf(buf
, buf_size
,
1274 "%-10s%-20s%7s%20s%15s",
1275 sn
->id_str
, sn
->name
,
1276 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1284 /**************************************************************/
1287 typedef struct VectorTranslationAIOCB
{
1288 BlockDriverAIOCB common
;
1292 BlockDriverAIOCB
*aiocb
;
1293 } VectorTranslationAIOCB
;
1295 static void bdrv_aio_cancel_vector(BlockDriverAIOCB
*_acb
)
1297 VectorTranslationAIOCB
*acb
1298 = container_of(_acb
, VectorTranslationAIOCB
, common
);
1300 bdrv_aio_cancel(acb
->aiocb
);
1303 static void bdrv_aio_rw_vector_cb(void *opaque
, int ret
)
1305 VectorTranslationAIOCB
*s
= (VectorTranslationAIOCB
*)opaque
;
1308 qemu_iovec_from_buffer(s
->iov
, s
->bounce
, s
->iov
->size
);
1310 qemu_vfree(s
->bounce
);
1311 s
->common
.cb(s
->common
.opaque
, ret
);
1312 qemu_aio_release(s
);
1315 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1319 BlockDriverCompletionFunc
*cb
,
1324 VectorTranslationAIOCB
*s
= qemu_aio_get_pool(&vectored_aio_pool
, bs
,
1328 s
->bounce
= qemu_memalign(512, nb_sectors
* 512);
1329 s
->is_write
= is_write
;
1331 qemu_iovec_to_buffer(s
->iov
, s
->bounce
);
1332 s
->aiocb
= bdrv_aio_write(bs
, sector_num
, s
->bounce
, nb_sectors
,
1333 bdrv_aio_rw_vector_cb
, s
);
1335 s
->aiocb
= bdrv_aio_read(bs
, sector_num
, s
->bounce
, nb_sectors
,
1336 bdrv_aio_rw_vector_cb
, s
);
1339 qemu_vfree(s
->bounce
);
1340 qemu_aio_release(s
);
1346 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1347 QEMUIOVector
*iov
, int nb_sectors
,
1348 BlockDriverCompletionFunc
*cb
, void *opaque
)
1350 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1353 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1357 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1358 QEMUIOVector
*iov
, int nb_sectors
,
1359 BlockDriverCompletionFunc
*cb
, void *opaque
)
1361 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1364 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1368 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1369 uint8_t *buf
, int nb_sectors
,
1370 BlockDriverCompletionFunc
*cb
, void *opaque
)
1372 BlockDriver
*drv
= bs
->drv
;
1373 BlockDriverAIOCB
*ret
;
1377 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1380 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1383 /* Update stats even though technically transfer has not happened. */
1384 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1391 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1392 const uint8_t *buf
, int nb_sectors
,
1393 BlockDriverCompletionFunc
*cb
, void *opaque
)
1395 BlockDriver
*drv
= bs
->drv
;
1396 BlockDriverAIOCB
*ret
;
1402 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1405 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1408 /* Update stats even though technically transfer has not happened. */
1409 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1416 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1418 acb
->pool
->cancel(acb
);
1422 /**************************************************************/
1423 /* async block device emulation */
1425 static void bdrv_aio_bh_cb(void *opaque
)
1427 BlockDriverAIOCBSync
*acb
= opaque
;
1428 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1429 qemu_aio_release(acb
);
1432 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1433 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1434 BlockDriverCompletionFunc
*cb
, void *opaque
)
1436 BlockDriverAIOCBSync
*acb
;
1439 acb
= qemu_aio_get(bs
, cb
, opaque
);
1441 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1442 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1444 qemu_bh_schedule(acb
->bh
);
1445 return &acb
->common
;
1448 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1449 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1450 BlockDriverCompletionFunc
*cb
, void *opaque
)
1452 BlockDriverAIOCBSync
*acb
;
1455 acb
= qemu_aio_get(bs
, cb
, opaque
);
1457 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1458 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1460 qemu_bh_schedule(acb
->bh
);
1461 return &acb
->common
;
1464 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1466 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1467 qemu_bh_cancel(acb
->bh
);
1468 qemu_aio_release(acb
);
1471 /**************************************************************/
1472 /* sync block device emulation */
1474 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1476 *(int *)opaque
= ret
;
1479 #define NOT_DONE 0x7fffffff
1481 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1482 uint8_t *buf
, int nb_sectors
)
1485 BlockDriverAIOCB
*acb
;
1487 async_ret
= NOT_DONE
;
1488 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1489 bdrv_rw_em_cb
, &async_ret
);
1493 while (async_ret
== NOT_DONE
) {
1500 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1501 const uint8_t *buf
, int nb_sectors
)
1504 BlockDriverAIOCB
*acb
;
1506 async_ret
= NOT_DONE
;
1507 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1508 bdrv_rw_em_cb
, &async_ret
);
1511 while (async_ret
== NOT_DONE
) {
1517 void bdrv_init(void)
1519 aio_pool_init(&vectored_aio_pool
, sizeof(VectorTranslationAIOCB
),
1520 bdrv_aio_cancel_vector
);
1522 bdrv_register(&bdrv_raw
);
1523 bdrv_register(&bdrv_host_device
);
1525 bdrv_register(&bdrv_cow
);
1527 bdrv_register(&bdrv_qcow
);
1528 bdrv_register(&bdrv_vmdk
);
1529 bdrv_register(&bdrv_cloop
);
1530 bdrv_register(&bdrv_dmg
);
1531 bdrv_register(&bdrv_bochs
);
1532 bdrv_register(&bdrv_vpc
);
1533 bdrv_register(&bdrv_vvfat
);
1534 bdrv_register(&bdrv_qcow2
);
1535 bdrv_register(&bdrv_parallels
);
1536 bdrv_register(&bdrv_nbd
);
1539 void aio_pool_init(AIOPool
*pool
, int aiocb_size
,
1540 void (*cancel
)(BlockDriverAIOCB
*acb
))
1542 pool
->aiocb_size
= aiocb_size
;
1543 pool
->cancel
= cancel
;
1544 pool
->free_aiocb
= NULL
;
1547 void *qemu_aio_get_pool(AIOPool
*pool
, BlockDriverState
*bs
,
1548 BlockDriverCompletionFunc
*cb
, void *opaque
)
1550 BlockDriverAIOCB
*acb
;
1552 if (pool
->free_aiocb
) {
1553 acb
= pool
->free_aiocb
;
1554 pool
->free_aiocb
= acb
->next
;
1556 acb
= qemu_mallocz(pool
->aiocb_size
);
1561 acb
->opaque
= opaque
;
1565 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1568 return qemu_aio_get_pool(&bs
->drv
->aio_pool
, bs
, cb
, opaque
);
1571 void qemu_aio_release(void *p
)
1573 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1574 AIOPool
*pool
= acb
->pool
;
1575 acb
->next
= pool
->free_aiocb
;
1576 pool
->free_aiocb
= acb
;
1579 /**************************************************************/
1580 /* removable device support */
1583 * Return TRUE if the media is present
1585 int bdrv_is_inserted(BlockDriverState
*bs
)
1587 BlockDriver
*drv
= bs
->drv
;
1591 if (!drv
->bdrv_is_inserted
)
1593 ret
= drv
->bdrv_is_inserted(bs
);
1598 * Return TRUE if the media changed since the last call to this
1599 * function. It is currently only used for floppy disks
1601 int bdrv_media_changed(BlockDriverState
*bs
)
1603 BlockDriver
*drv
= bs
->drv
;
1606 if (!drv
|| !drv
->bdrv_media_changed
)
1609 ret
= drv
->bdrv_media_changed(bs
);
1610 if (ret
== -ENOTSUP
)
1611 ret
= bs
->media_changed
;
1612 bs
->media_changed
= 0;
1617 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1619 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1621 BlockDriver
*drv
= bs
->drv
;
1624 if (!drv
|| !drv
->bdrv_eject
) {
1627 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1629 if (ret
== -ENOTSUP
) {
1635 int bdrv_is_locked(BlockDriverState
*bs
)
1641 * Lock or unlock the media (if it is locked, the user won't be able
1642 * to eject it manually).
1644 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1646 BlockDriver
*drv
= bs
->drv
;
1648 bs
->locked
= locked
;
1649 if (drv
&& drv
->bdrv_set_locked
) {
1650 drv
->bdrv_set_locked(bs
, locked
);
1654 /* needed for generic scsi interface */
1656 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1658 BlockDriver
*drv
= bs
->drv
;
1660 if (drv
&& drv
->bdrv_ioctl
)
1661 return drv
->bdrv_ioctl(bs
, req
, buf
);
1665 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
1666 unsigned long int req
, void *buf
,
1667 BlockDriverCompletionFunc
*cb
, void *opaque
)
1669 BlockDriver
*drv
= bs
->drv
;
1671 if (drv
&& drv
->bdrv_aio_ioctl
)
1672 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);