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"
35 #include <sys/types.h>
37 #include <sys/ioctl.h>
48 #define SECTOR_SIZE (1 << SECTOR_BITS)
50 static AIOPool vectored_aio_pool
;
52 typedef struct BlockDriverAIOCBSync
{
53 BlockDriverAIOCB common
;
56 } BlockDriverAIOCBSync
;
58 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
59 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
60 BlockDriverCompletionFunc
*cb
, void *opaque
);
61 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
62 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
63 BlockDriverCompletionFunc
*cb
, void *opaque
);
64 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
65 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
66 uint8_t *buf
, int nb_sectors
);
67 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
68 const uint8_t *buf
, int nb_sectors
);
70 BlockDriverState
*bdrv_first
;
72 static BlockDriver
*first_drv
;
74 int path_is_absolute(const char *path
)
78 /* specific case for names like: "\\.\d:" */
79 if (*path
== '/' || *path
== '\\')
82 p
= strchr(path
, ':');
88 return (*p
== '/' || *p
== '\\');
94 /* if filename is absolute, just copy it to dest. Otherwise, build a
95 path to it by considering it is relative to base_path. URL are
97 void path_combine(char *dest
, int dest_size
,
98 const char *base_path
,
106 if (path_is_absolute(filename
)) {
107 pstrcpy(dest
, dest_size
, filename
);
109 p
= strchr(base_path
, ':');
114 p1
= strrchr(base_path
, '/');
118 p2
= strrchr(base_path
, '\\');
130 if (len
> dest_size
- 1)
132 memcpy(dest
, base_path
, len
);
134 pstrcat(dest
, dest_size
, filename
);
139 static void bdrv_register(BlockDriver
*bdrv
)
141 if (!bdrv
->bdrv_aio_read
) {
142 /* add AIO emulation layer */
143 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
144 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
145 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
146 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
147 } else if (!bdrv
->bdrv_read
) {
148 /* add synchronous IO emulation layer */
149 bdrv
->bdrv_read
= bdrv_read_em
;
150 bdrv
->bdrv_write
= bdrv_write_em
;
152 aio_pool_init(&bdrv
->aio_pool
, bdrv
->aiocb_size
, bdrv
->bdrv_aio_cancel
);
153 bdrv
->next
= first_drv
;
157 /* create a new block device (by default it is empty) */
158 BlockDriverState
*bdrv_new(const char *device_name
)
160 BlockDriverState
**pbs
, *bs
;
162 bs
= qemu_mallocz(sizeof(BlockDriverState
));
163 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
164 if (device_name
[0] != '\0') {
165 /* insert at the end */
174 BlockDriver
*bdrv_find_format(const char *format_name
)
177 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
178 if (!strcmp(drv1
->format_name
, format_name
))
184 int bdrv_create(BlockDriver
*drv
,
185 const char *filename
, int64_t size_in_sectors
,
186 const char *backing_file
, int flags
)
188 if (!drv
->bdrv_create
)
190 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
194 void get_tmp_filename(char *filename
, int size
)
196 char temp_dir
[MAX_PATH
];
198 GetTempPath(MAX_PATH
, temp_dir
);
199 GetTempFileName(temp_dir
, "qem", 0, filename
);
202 void get_tmp_filename(char *filename
, int size
)
206 /* XXX: race condition possible */
207 tmpdir
= getenv("TMPDIR");
210 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
211 fd
= mkstemp(filename
);
217 static int is_windows_drive_prefix(const char *filename
)
219 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
220 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
224 static int is_windows_drive(const char *filename
)
226 if (is_windows_drive_prefix(filename
) &&
229 if (strstart(filename
, "\\\\.\\", NULL
) ||
230 strstart(filename
, "//./", NULL
))
236 static BlockDriver
*find_protocol(const char *filename
)
244 if (is_windows_drive(filename
) ||
245 is_windows_drive_prefix(filename
))
248 p
= strchr(filename
, ':');
252 if (len
> sizeof(protocol
) - 1)
253 len
= sizeof(protocol
) - 1;
254 memcpy(protocol
, filename
, len
);
255 protocol
[len
] = '\0';
256 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
257 if (drv1
->protocol_name
&&
258 !strcmp(drv1
->protocol_name
, protocol
))
264 /* XXX: force raw format if block or character device ? It would
265 simplify the BSD case */
266 static BlockDriver
*find_image_format(const char *filename
)
268 int ret
, score
, score_max
;
269 BlockDriver
*drv1
, *drv
;
271 BlockDriverState
*bs
;
273 /* detect host devices. By convention, /dev/cdrom[N] is always
274 recognized as a host CDROM */
275 if (strstart(filename
, "/dev/cdrom", NULL
))
276 return &bdrv_host_device
;
278 if (is_windows_drive(filename
))
279 return &bdrv_host_device
;
283 if (stat(filename
, &st
) >= 0 &&
284 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
285 return &bdrv_host_device
;
290 drv
= find_protocol(filename
);
291 /* no need to test disk image formats for vvfat */
292 if (drv
== &bdrv_vvfat
)
295 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
298 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
305 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
306 if (drv1
->bdrv_probe
) {
307 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
308 if (score
> score_max
) {
317 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
319 BlockDriverState
*bs
;
323 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
333 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
335 return bdrv_open2(bs
, filename
, flags
, NULL
);
338 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
342 char tmp_filename
[PATH_MAX
];
343 char backing_filename
[PATH_MAX
];
346 bs
->is_temporary
= 0;
350 if (flags
& BDRV_O_SNAPSHOT
) {
351 BlockDriverState
*bs1
;
355 /* if snapshot, we create a temporary backing file and open it
356 instead of opening 'filename' directly */
358 /* if there is a backing file, use it */
360 ret
= bdrv_open(bs1
, filename
, 0);
365 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
367 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
372 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
374 /* Real path is meaningless for protocols */
376 snprintf(backing_filename
, sizeof(backing_filename
),
379 realpath(filename
, backing_filename
);
381 ret
= bdrv_create(&bdrv_qcow2
, tmp_filename
,
382 total_size
, backing_filename
, 0);
386 filename
= tmp_filename
;
387 bs
->is_temporary
= 1;
390 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
391 if (flags
& BDRV_O_FILE
) {
392 drv
= find_protocol(filename
);
394 drv
= find_image_format(filename
);
398 goto unlink_and_fail
;
401 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
402 /* Note: for compatibility, we open disk image files as RDWR, and
403 RDONLY as fallback */
404 if (!(flags
& BDRV_O_FILE
))
405 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
407 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
408 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
409 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
410 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
414 qemu_free(bs
->opaque
);
418 if (bs
->is_temporary
)
422 if (drv
->bdrv_getlength
) {
423 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
426 if (bs
->is_temporary
) {
430 if (bs
->backing_file
[0] != '\0') {
431 /* if there is a backing file, use it */
432 bs
->backing_hd
= bdrv_new("");
433 path_combine(backing_filename
, sizeof(backing_filename
),
434 filename
, bs
->backing_file
);
435 ret
= bdrv_open(bs
->backing_hd
, backing_filename
, open_flags
);
442 if (!bdrv_key_required(bs
)) {
443 /* call the change callback */
444 bs
->media_changed
= 1;
446 bs
->change_cb(bs
->change_opaque
);
451 void bdrv_close(BlockDriverState
*bs
)
455 bdrv_delete(bs
->backing_hd
);
456 bs
->drv
->bdrv_close(bs
);
457 qemu_free(bs
->opaque
);
459 if (bs
->is_temporary
) {
460 unlink(bs
->filename
);
466 /* call the change callback */
467 bs
->media_changed
= 1;
469 bs
->change_cb(bs
->change_opaque
);
473 void bdrv_delete(BlockDriverState
*bs
)
475 BlockDriverState
**pbs
;
478 while (*pbs
!= bs
&& *pbs
!= NULL
)
487 /* commit COW file into the raw image */
488 int bdrv_commit(BlockDriverState
*bs
)
490 BlockDriver
*drv
= bs
->drv
;
491 int64_t i
, total_sectors
;
493 unsigned char sector
[512];
502 if (!bs
->backing_hd
) {
506 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
507 for (i
= 0; i
< total_sectors
;) {
508 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
509 for(j
= 0; j
< n
; j
++) {
510 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
514 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
524 if (drv
->bdrv_make_empty
)
525 return drv
->bdrv_make_empty(bs
);
530 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
535 if (!bdrv_is_inserted(bs
))
541 len
= bdrv_getlength(bs
);
543 if ((offset
+ size
) > len
)
549 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
554 /* Deal with byte accesses */
556 offset
= -sector_num
;
558 offset
= sector_num
* 512;
560 return bdrv_check_byte_request(bs
, offset
, nb_sectors
* 512);
563 /* return < 0 if error. See bdrv_write() for the return codes */
564 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
565 uint8_t *buf
, int nb_sectors
)
567 BlockDriver
*drv
= bs
->drv
;
571 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
574 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
577 /* Return < 0 if error. Important errors are:
578 -EIO generic I/O error (may happen for all errors)
579 -ENOMEDIUM No media inserted.
580 -EINVAL Invalid sector number or nb_sectors
581 -EACCES Trying to write a read-only device
583 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
584 const uint8_t *buf
, int nb_sectors
)
586 BlockDriver
*drv
= bs
->drv
;
591 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
594 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
597 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
598 void *buf
, int count1
)
600 uint8_t tmp_buf
[SECTOR_SIZE
];
601 int len
, nb_sectors
, count
;
605 /* first read to align to sector start */
606 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
609 sector_num
= offset
>> SECTOR_BITS
;
611 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
613 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
621 /* read the sectors "in place" */
622 nb_sectors
= count
>> SECTOR_BITS
;
623 if (nb_sectors
> 0) {
624 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
626 sector_num
+= nb_sectors
;
627 len
= nb_sectors
<< SECTOR_BITS
;
632 /* add data from the last sector */
634 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
636 memcpy(buf
, tmp_buf
, count
);
641 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
642 const void *buf
, int count1
)
644 uint8_t tmp_buf
[SECTOR_SIZE
];
645 int len
, nb_sectors
, count
;
649 /* first write to align to sector start */
650 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
653 sector_num
= offset
>> SECTOR_BITS
;
655 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
657 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
658 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
667 /* write the sectors "in place" */
668 nb_sectors
= count
>> SECTOR_BITS
;
669 if (nb_sectors
> 0) {
670 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
672 sector_num
+= nb_sectors
;
673 len
= nb_sectors
<< SECTOR_BITS
;
678 /* add data from the last sector */
680 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
682 memcpy(tmp_buf
, buf
, count
);
683 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
690 * Truncate file to 'offset' bytes (needed only for file protocols)
692 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
694 BlockDriver
*drv
= bs
->drv
;
697 if (!drv
->bdrv_truncate
)
699 return drv
->bdrv_truncate(bs
, offset
);
703 * Length of a file in bytes. Return < 0 if error or unknown.
705 int64_t bdrv_getlength(BlockDriverState
*bs
)
707 BlockDriver
*drv
= bs
->drv
;
710 if (!drv
->bdrv_getlength
) {
712 return bs
->total_sectors
* SECTOR_SIZE
;
714 return drv
->bdrv_getlength(bs
);
717 /* return 0 as number of sectors if no device present or error */
718 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
721 length
= bdrv_getlength(bs
);
725 length
= length
>> SECTOR_BITS
;
726 *nb_sectors_ptr
= length
;
730 uint8_t boot_ind
; /* 0x80 - active */
731 uint8_t head
; /* starting head */
732 uint8_t sector
; /* starting sector */
733 uint8_t cyl
; /* starting cylinder */
734 uint8_t sys_ind
; /* What partition type */
735 uint8_t end_head
; /* end head */
736 uint8_t end_sector
; /* end sector */
737 uint8_t end_cyl
; /* end cylinder */
738 uint32_t start_sect
; /* starting sector counting from 0 */
739 uint32_t nr_sects
; /* nr of sectors in partition */
740 } __attribute__((packed
));
742 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
743 static int guess_disk_lchs(BlockDriverState
*bs
,
744 int *pcylinders
, int *pheads
, int *psectors
)
747 int ret
, i
, heads
, sectors
, cylinders
;
752 bdrv_get_geometry(bs
, &nb_sectors
);
754 ret
= bdrv_read(bs
, 0, buf
, 1);
757 /* test msdos magic */
758 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
760 for(i
= 0; i
< 4; i
++) {
761 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
762 nr_sects
= le32_to_cpu(p
->nr_sects
);
763 if (nr_sects
&& p
->end_head
) {
764 /* We make the assumption that the partition terminates on
765 a cylinder boundary */
766 heads
= p
->end_head
+ 1;
767 sectors
= p
->end_sector
& 63;
770 cylinders
= nb_sectors
/ (heads
* sectors
);
771 if (cylinders
< 1 || cylinders
> 16383)
775 *pcylinders
= cylinders
;
777 printf("guessed geometry: LCHS=%d %d %d\n",
778 cylinders
, heads
, sectors
);
786 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
788 int translation
, lba_detected
= 0;
789 int cylinders
, heads
, secs
;
792 /* if a geometry hint is available, use it */
793 bdrv_get_geometry(bs
, &nb_sectors
);
794 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
795 translation
= bdrv_get_translation_hint(bs
);
796 if (cylinders
!= 0) {
801 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
803 /* if heads > 16, it means that a BIOS LBA
804 translation was active, so the default
805 hardware geometry is OK */
807 goto default_geometry
;
812 /* disable any translation to be in sync with
813 the logical geometry */
814 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
815 bdrv_set_translation_hint(bs
,
816 BIOS_ATA_TRANSLATION_NONE
);
821 /* if no geometry, use a standard physical disk geometry */
822 cylinders
= nb_sectors
/ (16 * 63);
824 if (cylinders
> 16383)
826 else if (cylinders
< 2)
831 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
832 if ((*pcyls
* *pheads
) <= 131072) {
833 bdrv_set_translation_hint(bs
,
834 BIOS_ATA_TRANSLATION_LARGE
);
836 bdrv_set_translation_hint(bs
,
837 BIOS_ATA_TRANSLATION_LBA
);
841 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
845 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
846 int cyls
, int heads
, int secs
)
853 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
856 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
857 type
== BDRV_TYPE_FLOPPY
));
860 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
862 bs
->translation
= translation
;
865 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
866 int *pcyls
, int *pheads
, int *psecs
)
873 int bdrv_get_type_hint(BlockDriverState
*bs
)
878 int bdrv_get_translation_hint(BlockDriverState
*bs
)
880 return bs
->translation
;
883 int bdrv_is_removable(BlockDriverState
*bs
)
885 return bs
->removable
;
888 int bdrv_is_read_only(BlockDriverState
*bs
)
890 return bs
->read_only
;
893 int bdrv_is_sg(BlockDriverState
*bs
)
898 /* XXX: no longer used */
899 void bdrv_set_change_cb(BlockDriverState
*bs
,
900 void (*change_cb
)(void *opaque
), void *opaque
)
902 bs
->change_cb
= change_cb
;
903 bs
->change_opaque
= opaque
;
906 int bdrv_is_encrypted(BlockDriverState
*bs
)
908 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
910 return bs
->encrypted
;
913 int bdrv_key_required(BlockDriverState
*bs
)
915 BlockDriverState
*backing_hd
= bs
->backing_hd
;
917 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
919 return (bs
->encrypted
&& !bs
->valid_key
);
922 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
925 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
926 ret
= bdrv_set_key(bs
->backing_hd
, key
);
932 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
934 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
937 } else if (!bs
->valid_key
) {
939 /* call the change callback now, we skipped it on open */
940 bs
->media_changed
= 1;
942 bs
->change_cb(bs
->change_opaque
);
947 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
952 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
956 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
961 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
962 it(opaque
, drv
->format_name
);
966 BlockDriverState
*bdrv_find(const char *name
)
968 BlockDriverState
*bs
;
970 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
971 if (!strcmp(name
, bs
->device_name
))
977 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
979 BlockDriverState
*bs
;
981 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
986 const char *bdrv_get_device_name(BlockDriverState
*bs
)
988 return bs
->device_name
;
991 void bdrv_flush(BlockDriverState
*bs
)
993 if (bs
->drv
->bdrv_flush
)
994 bs
->drv
->bdrv_flush(bs
);
996 bdrv_flush(bs
->backing_hd
);
999 void bdrv_flush_all(void)
1001 BlockDriverState
*bs
;
1003 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1004 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1005 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1010 * Returns true iff the specified sector is present in the disk image. Drivers
1011 * not implementing the functionality are assumed to not support backing files,
1012 * hence all their sectors are reported as allocated.
1014 * 'pnum' is set to the number of sectors (including and immediately following
1015 * the specified sector) that are known to be in the same
1016 * allocated/unallocated state.
1018 * 'nb_sectors' is the max value 'pnum' should be set to.
1020 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1024 if (!bs
->drv
->bdrv_is_allocated
) {
1025 if (sector_num
>= bs
->total_sectors
) {
1029 n
= bs
->total_sectors
- sector_num
;
1030 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1033 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1036 void bdrv_info(Monitor
*mon
)
1038 BlockDriverState
*bs
;
1040 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1041 monitor_printf(mon
, "%s:", bs
->device_name
);
1042 monitor_printf(mon
, " type=");
1045 monitor_printf(mon
, "hd");
1047 case BDRV_TYPE_CDROM
:
1048 monitor_printf(mon
, "cdrom");
1050 case BDRV_TYPE_FLOPPY
:
1051 monitor_printf(mon
, "floppy");
1054 monitor_printf(mon
, " removable=%d", bs
->removable
);
1055 if (bs
->removable
) {
1056 monitor_printf(mon
, " locked=%d", bs
->locked
);
1059 monitor_printf(mon
, " file=");
1060 monitor_print_filename(mon
, bs
->filename
);
1061 if (bs
->backing_file
[0] != '\0') {
1062 monitor_printf(mon
, " backing_file=");
1063 monitor_print_filename(mon
, bs
->backing_file
);
1065 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1066 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1067 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1069 monitor_printf(mon
, " [not inserted]");
1071 monitor_printf(mon
, "\n");
1075 /* The "info blockstats" command. */
1076 void bdrv_info_stats(Monitor
*mon
)
1078 BlockDriverState
*bs
;
1080 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1081 monitor_printf(mon
, "%s:"
1082 " rd_bytes=%" PRIu64
1083 " wr_bytes=%" PRIu64
1084 " rd_operations=%" PRIu64
1085 " wr_operations=%" PRIu64
1088 bs
->rd_bytes
, bs
->wr_bytes
,
1089 bs
->rd_ops
, bs
->wr_ops
);
1093 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1095 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1096 return bs
->backing_file
;
1097 else if (bs
->encrypted
)
1098 return bs
->filename
;
1103 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1104 char *filename
, int filename_size
)
1106 if (!bs
->backing_hd
) {
1107 pstrcpy(filename
, filename_size
, "");
1109 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1113 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1114 const uint8_t *buf
, int nb_sectors
)
1116 BlockDriver
*drv
= bs
->drv
;
1119 if (!drv
->bdrv_write_compressed
)
1121 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1124 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1126 BlockDriver
*drv
= bs
->drv
;
1129 if (!drv
->bdrv_get_info
)
1131 memset(bdi
, 0, sizeof(*bdi
));
1132 return drv
->bdrv_get_info(bs
, bdi
);
1135 /**************************************************************/
1136 /* handling of snapshots */
1138 int bdrv_snapshot_create(BlockDriverState
*bs
,
1139 QEMUSnapshotInfo
*sn_info
)
1141 BlockDriver
*drv
= bs
->drv
;
1144 if (!drv
->bdrv_snapshot_create
)
1146 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1149 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1150 const char *snapshot_id
)
1152 BlockDriver
*drv
= bs
->drv
;
1155 if (!drv
->bdrv_snapshot_goto
)
1157 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1160 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1162 BlockDriver
*drv
= bs
->drv
;
1165 if (!drv
->bdrv_snapshot_delete
)
1167 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1170 int bdrv_snapshot_list(BlockDriverState
*bs
,
1171 QEMUSnapshotInfo
**psn_info
)
1173 BlockDriver
*drv
= bs
->drv
;
1176 if (!drv
->bdrv_snapshot_list
)
1178 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1181 #define NB_SUFFIXES 4
1183 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1185 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1190 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1193 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1194 if (size
< (10 * base
)) {
1195 snprintf(buf
, buf_size
, "%0.1f%c",
1196 (double)size
/ base
,
1199 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1200 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1201 ((size
+ (base
>> 1)) / base
),
1211 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1213 char buf1
[128], date_buf
[128], clock_buf
[128];
1223 snprintf(buf
, buf_size
,
1224 "%-10s%-20s%7s%20s%15s",
1225 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1229 ptm
= localtime(&ti
);
1230 strftime(date_buf
, sizeof(date_buf
),
1231 "%Y-%m-%d %H:%M:%S", ptm
);
1233 localtime_r(&ti
, &tm
);
1234 strftime(date_buf
, sizeof(date_buf
),
1235 "%Y-%m-%d %H:%M:%S", &tm
);
1237 secs
= sn
->vm_clock_nsec
/ 1000000000;
1238 snprintf(clock_buf
, sizeof(clock_buf
),
1239 "%02d:%02d:%02d.%03d",
1241 (int)((secs
/ 60) % 60),
1243 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1244 snprintf(buf
, buf_size
,
1245 "%-10s%-20s%7s%20s%15s",
1246 sn
->id_str
, sn
->name
,
1247 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1255 /**************************************************************/
1258 typedef struct VectorTranslationAIOCB
{
1259 BlockDriverAIOCB common
;
1263 BlockDriverAIOCB
*aiocb
;
1264 } VectorTranslationAIOCB
;
1266 static void bdrv_aio_cancel_vector(BlockDriverAIOCB
*_acb
)
1268 VectorTranslationAIOCB
*acb
1269 = container_of(_acb
, VectorTranslationAIOCB
, common
);
1271 bdrv_aio_cancel(acb
->aiocb
);
1274 static void bdrv_aio_rw_vector_cb(void *opaque
, int ret
)
1276 VectorTranslationAIOCB
*s
= (VectorTranslationAIOCB
*)opaque
;
1279 qemu_iovec_from_buffer(s
->iov
, s
->bounce
, s
->iov
->size
);
1281 qemu_vfree(s
->bounce
);
1282 s
->common
.cb(s
->common
.opaque
, ret
);
1283 qemu_aio_release(s
);
1286 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1290 BlockDriverCompletionFunc
*cb
,
1295 VectorTranslationAIOCB
*s
= qemu_aio_get_pool(&vectored_aio_pool
, bs
,
1299 s
->bounce
= qemu_memalign(512, nb_sectors
* 512);
1300 s
->is_write
= is_write
;
1302 qemu_iovec_to_buffer(s
->iov
, s
->bounce
);
1303 s
->aiocb
= bdrv_aio_write(bs
, sector_num
, s
->bounce
, nb_sectors
,
1304 bdrv_aio_rw_vector_cb
, s
);
1306 s
->aiocb
= bdrv_aio_read(bs
, sector_num
, s
->bounce
, nb_sectors
,
1307 bdrv_aio_rw_vector_cb
, s
);
1312 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1313 QEMUIOVector
*iov
, int nb_sectors
,
1314 BlockDriverCompletionFunc
*cb
, void *opaque
)
1316 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1319 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1323 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1324 QEMUIOVector
*iov
, int nb_sectors
,
1325 BlockDriverCompletionFunc
*cb
, void *opaque
)
1327 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1330 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1334 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1335 uint8_t *buf
, int nb_sectors
,
1336 BlockDriverCompletionFunc
*cb
, void *opaque
)
1338 BlockDriver
*drv
= bs
->drv
;
1339 BlockDriverAIOCB
*ret
;
1343 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1346 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1349 /* Update stats even though technically transfer has not happened. */
1350 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1357 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1358 const uint8_t *buf
, int nb_sectors
,
1359 BlockDriverCompletionFunc
*cb
, void *opaque
)
1361 BlockDriver
*drv
= bs
->drv
;
1362 BlockDriverAIOCB
*ret
;
1368 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1371 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1374 /* Update stats even though technically transfer has not happened. */
1375 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1382 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1384 acb
->pool
->cancel(acb
);
1388 /**************************************************************/
1389 /* async block device emulation */
1391 static void bdrv_aio_bh_cb(void *opaque
)
1393 BlockDriverAIOCBSync
*acb
= opaque
;
1394 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1395 qemu_aio_release(acb
);
1398 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1399 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1400 BlockDriverCompletionFunc
*cb
, void *opaque
)
1402 BlockDriverAIOCBSync
*acb
;
1405 acb
= qemu_aio_get(bs
, cb
, opaque
);
1407 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1408 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1410 qemu_bh_schedule(acb
->bh
);
1411 return &acb
->common
;
1414 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1415 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1416 BlockDriverCompletionFunc
*cb
, void *opaque
)
1418 BlockDriverAIOCBSync
*acb
;
1421 acb
= qemu_aio_get(bs
, cb
, opaque
);
1423 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1424 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1426 qemu_bh_schedule(acb
->bh
);
1427 return &acb
->common
;
1430 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1432 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1433 qemu_bh_cancel(acb
->bh
);
1434 qemu_aio_release(acb
);
1437 /**************************************************************/
1438 /* sync block device emulation */
1440 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1442 *(int *)opaque
= ret
;
1445 #define NOT_DONE 0x7fffffff
1447 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1448 uint8_t *buf
, int nb_sectors
)
1451 BlockDriverAIOCB
*acb
;
1453 async_ret
= NOT_DONE
;
1454 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1455 bdrv_rw_em_cb
, &async_ret
);
1459 while (async_ret
== NOT_DONE
) {
1466 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1467 const uint8_t *buf
, int nb_sectors
)
1470 BlockDriverAIOCB
*acb
;
1472 async_ret
= NOT_DONE
;
1473 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1474 bdrv_rw_em_cb
, &async_ret
);
1477 while (async_ret
== NOT_DONE
) {
1483 void bdrv_init(void)
1485 aio_pool_init(&vectored_aio_pool
, sizeof(VectorTranslationAIOCB
),
1486 bdrv_aio_cancel_vector
);
1488 bdrv_register(&bdrv_raw
);
1489 bdrv_register(&bdrv_host_device
);
1491 bdrv_register(&bdrv_cow
);
1493 bdrv_register(&bdrv_qcow
);
1494 bdrv_register(&bdrv_vmdk
);
1495 bdrv_register(&bdrv_cloop
);
1496 bdrv_register(&bdrv_dmg
);
1497 bdrv_register(&bdrv_bochs
);
1498 bdrv_register(&bdrv_vpc
);
1499 bdrv_register(&bdrv_vvfat
);
1500 bdrv_register(&bdrv_qcow2
);
1501 bdrv_register(&bdrv_parallels
);
1502 bdrv_register(&bdrv_nbd
);
1505 void aio_pool_init(AIOPool
*pool
, int aiocb_size
,
1506 void (*cancel
)(BlockDriverAIOCB
*acb
))
1508 pool
->aiocb_size
= aiocb_size
;
1509 pool
->cancel
= cancel
;
1510 pool
->free_aiocb
= NULL
;
1513 void *qemu_aio_get_pool(AIOPool
*pool
, BlockDriverState
*bs
,
1514 BlockDriverCompletionFunc
*cb
, void *opaque
)
1516 BlockDriverAIOCB
*acb
;
1518 if (pool
->free_aiocb
) {
1519 acb
= pool
->free_aiocb
;
1520 pool
->free_aiocb
= acb
->next
;
1522 acb
= qemu_mallocz(pool
->aiocb_size
);
1527 acb
->opaque
= opaque
;
1531 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1534 return qemu_aio_get_pool(&bs
->drv
->aio_pool
, bs
, cb
, opaque
);
1537 void qemu_aio_release(void *p
)
1539 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1540 AIOPool
*pool
= acb
->pool
;
1541 acb
->next
= pool
->free_aiocb
;
1542 pool
->free_aiocb
= acb
;
1545 /**************************************************************/
1546 /* removable device support */
1549 * Return TRUE if the media is present
1551 int bdrv_is_inserted(BlockDriverState
*bs
)
1553 BlockDriver
*drv
= bs
->drv
;
1557 if (!drv
->bdrv_is_inserted
)
1559 ret
= drv
->bdrv_is_inserted(bs
);
1564 * Return TRUE if the media changed since the last call to this
1565 * function. It is currently only used for floppy disks
1567 int bdrv_media_changed(BlockDriverState
*bs
)
1569 BlockDriver
*drv
= bs
->drv
;
1572 if (!drv
|| !drv
->bdrv_media_changed
)
1575 ret
= drv
->bdrv_media_changed(bs
);
1576 if (ret
== -ENOTSUP
)
1577 ret
= bs
->media_changed
;
1578 bs
->media_changed
= 0;
1583 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1585 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1587 BlockDriver
*drv
= bs
->drv
;
1590 if (!drv
|| !drv
->bdrv_eject
) {
1593 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1595 if (ret
== -ENOTSUP
) {
1601 int bdrv_is_locked(BlockDriverState
*bs
)
1607 * Lock or unlock the media (if it is locked, the user won't be able
1608 * to eject it manually).
1610 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1612 BlockDriver
*drv
= bs
->drv
;
1614 bs
->locked
= locked
;
1615 if (drv
&& drv
->bdrv_set_locked
) {
1616 drv
->bdrv_set_locked(bs
, locked
);
1620 /* needed for generic scsi interface */
1622 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1624 BlockDriver
*drv
= bs
->drv
;
1626 if (drv
&& drv
->bdrv_ioctl
)
1627 return drv
->bdrv_ioctl(bs
, req
, buf
);
1631 int bdrv_sg_send_command(BlockDriverState
*bs
, void *buf
, int count
)
1633 return bs
->drv
->bdrv_sg_send_command(bs
, buf
, count
);
1636 int bdrv_sg_recv_response(BlockDriverState
*bs
, void *buf
, int count
)
1638 return bs
->drv
->bdrv_sg_recv_response(bs
, buf
, count
);
1641 BlockDriverAIOCB
*bdrv_sg_aio_read(BlockDriverState
*bs
, void *buf
, int count
,
1642 BlockDriverCompletionFunc
*cb
, void *opaque
)
1644 return bs
->drv
->bdrv_sg_aio_read(bs
, buf
, count
, cb
, opaque
);
1647 BlockDriverAIOCB
*bdrv_sg_aio_write(BlockDriverState
*bs
, void *buf
, int count
,
1648 BlockDriverCompletionFunc
*cb
, void *opaque
)
1650 return bs
->drv
->bdrv_sg_aio_write(bs
, buf
, count
, cb
, opaque
);