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 } BlockDriverAIOCBSync
;
57 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
58 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
59 BlockDriverCompletionFunc
*cb
, void *opaque
);
60 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
61 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
62 BlockDriverCompletionFunc
*cb
, void *opaque
);
63 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
64 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
65 uint8_t *buf
, int nb_sectors
);
66 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
67 const uint8_t *buf
, int nb_sectors
);
69 BlockDriverState
*bdrv_first
;
71 static BlockDriver
*first_drv
;
73 int path_is_absolute(const char *path
)
77 /* specific case for names like: "\\.\d:" */
78 if (*path
== '/' || *path
== '\\')
81 p
= strchr(path
, ':');
87 return (*p
== '/' || *p
== '\\');
93 /* if filename is absolute, just copy it to dest. Otherwise, build a
94 path to it by considering it is relative to base_path. URL are
96 void path_combine(char *dest
, int dest_size
,
97 const char *base_path
,
105 if (path_is_absolute(filename
)) {
106 pstrcpy(dest
, dest_size
, filename
);
108 p
= strchr(base_path
, ':');
113 p1
= strrchr(base_path
, '/');
117 p2
= strrchr(base_path
, '\\');
129 if (len
> dest_size
- 1)
131 memcpy(dest
, base_path
, len
);
133 pstrcat(dest
, dest_size
, filename
);
138 static void bdrv_register(BlockDriver
*bdrv
)
140 if (!bdrv
->bdrv_aio_read
) {
141 /* add AIO emulation layer */
142 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
143 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
144 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
145 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
146 } else if (!bdrv
->bdrv_read
) {
147 /* add synchronous IO emulation layer */
148 bdrv
->bdrv_read
= bdrv_read_em
;
149 bdrv
->bdrv_write
= bdrv_write_em
;
151 bdrv
->next
= first_drv
;
155 /* create a new block device (by default it is empty) */
156 BlockDriverState
*bdrv_new(const char *device_name
)
158 BlockDriverState
**pbs
, *bs
;
160 bs
= qemu_mallocz(sizeof(BlockDriverState
));
161 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
162 if (device_name
[0] != '\0') {
163 /* insert at the end */
172 BlockDriver
*bdrv_find_format(const char *format_name
)
175 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
176 if (!strcmp(drv1
->format_name
, format_name
))
182 int bdrv_create(BlockDriver
*drv
,
183 const char *filename
, int64_t size_in_sectors
,
184 const char *backing_file
, int flags
)
186 if (!drv
->bdrv_create
)
188 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
192 void get_tmp_filename(char *filename
, int size
)
194 char temp_dir
[MAX_PATH
];
196 GetTempPath(MAX_PATH
, temp_dir
);
197 GetTempFileName(temp_dir
, "qem", 0, filename
);
200 void get_tmp_filename(char *filename
, int size
)
204 /* XXX: race condition possible */
205 tmpdir
= getenv("TMPDIR");
208 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
209 fd
= mkstemp(filename
);
215 static int is_windows_drive_prefix(const char *filename
)
217 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
218 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
222 static int is_windows_drive(const char *filename
)
224 if (is_windows_drive_prefix(filename
) &&
227 if (strstart(filename
, "\\\\.\\", NULL
) ||
228 strstart(filename
, "//./", NULL
))
234 static BlockDriver
*find_protocol(const char *filename
)
242 if (is_windows_drive(filename
) ||
243 is_windows_drive_prefix(filename
))
246 p
= strchr(filename
, ':');
250 if (len
> sizeof(protocol
) - 1)
251 len
= sizeof(protocol
) - 1;
252 memcpy(protocol
, filename
, len
);
253 protocol
[len
] = '\0';
254 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
255 if (drv1
->protocol_name
&&
256 !strcmp(drv1
->protocol_name
, protocol
))
262 /* XXX: force raw format if block or character device ? It would
263 simplify the BSD case */
264 static BlockDriver
*find_image_format(const char *filename
)
266 int ret
, score
, score_max
;
267 BlockDriver
*drv1
, *drv
;
269 BlockDriverState
*bs
;
271 /* detect host devices. By convention, /dev/cdrom[N] is always
272 recognized as a host CDROM */
273 if (strstart(filename
, "/dev/cdrom", NULL
))
274 return &bdrv_host_device
;
276 if (is_windows_drive(filename
))
277 return &bdrv_host_device
;
281 if (stat(filename
, &st
) >= 0 &&
282 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
283 return &bdrv_host_device
;
288 drv
= find_protocol(filename
);
289 /* no need to test disk image formats for vvfat */
290 if (drv
== &bdrv_vvfat
)
293 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
296 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
303 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
304 if (drv1
->bdrv_probe
) {
305 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
306 if (score
> score_max
) {
315 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
317 BlockDriverState
*bs
;
321 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
331 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
333 return bdrv_open2(bs
, filename
, flags
, NULL
);
336 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
340 char tmp_filename
[PATH_MAX
];
341 char backing_filename
[PATH_MAX
];
344 bs
->is_temporary
= 0;
348 if (flags
& BDRV_O_SNAPSHOT
) {
349 BlockDriverState
*bs1
;
353 /* if snapshot, we create a temporary backing file and open it
354 instead of opening 'filename' directly */
356 /* if there is a backing file, use it */
358 ret
= bdrv_open(bs1
, filename
, 0);
363 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
365 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
370 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
372 /* Real path is meaningless for protocols */
374 snprintf(backing_filename
, sizeof(backing_filename
),
377 realpath(filename
, backing_filename
);
379 ret
= bdrv_create(&bdrv_qcow2
, tmp_filename
,
380 total_size
, backing_filename
, 0);
384 filename
= tmp_filename
;
385 bs
->is_temporary
= 1;
388 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
389 if (flags
& BDRV_O_FILE
) {
390 drv
= find_protocol(filename
);
392 drv
= find_image_format(filename
);
396 goto unlink_and_fail
;
399 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
400 /* Note: for compatibility, we open disk image files as RDWR, and
401 RDONLY as fallback */
402 if (!(flags
& BDRV_O_FILE
))
403 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
405 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
406 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
407 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
408 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
412 qemu_free(bs
->opaque
);
416 if (bs
->is_temporary
)
420 if (drv
->bdrv_getlength
) {
421 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
424 if (bs
->is_temporary
) {
428 if (bs
->backing_file
[0] != '\0') {
429 /* if there is a backing file, use it */
430 bs
->backing_hd
= bdrv_new("");
431 path_combine(backing_filename
, sizeof(backing_filename
),
432 filename
, bs
->backing_file
);
433 ret
= bdrv_open(bs
->backing_hd
, backing_filename
, open_flags
);
440 if (!bdrv_key_required(bs
)) {
441 /* call the change callback */
442 bs
->media_changed
= 1;
444 bs
->change_cb(bs
->change_opaque
);
449 void bdrv_close(BlockDriverState
*bs
)
453 bdrv_delete(bs
->backing_hd
);
454 bs
->drv
->bdrv_close(bs
);
455 qemu_free(bs
->opaque
);
457 if (bs
->is_temporary
) {
458 unlink(bs
->filename
);
464 /* call the change callback */
465 bs
->media_changed
= 1;
467 bs
->change_cb(bs
->change_opaque
);
471 void bdrv_delete(BlockDriverState
*bs
)
473 BlockDriverState
**pbs
;
476 while (*pbs
!= bs
&& *pbs
!= NULL
)
485 /* commit COW file into the raw image */
486 int bdrv_commit(BlockDriverState
*bs
)
488 BlockDriver
*drv
= bs
->drv
;
489 int64_t i
, total_sectors
;
491 unsigned char sector
[512];
500 if (!bs
->backing_hd
) {
504 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
505 for (i
= 0; i
< total_sectors
;) {
506 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
507 for(j
= 0; j
< n
; j
++) {
508 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
512 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
522 if (drv
->bdrv_make_empty
)
523 return drv
->bdrv_make_empty(bs
);
528 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
533 if (!bdrv_is_inserted(bs
))
539 len
= bdrv_getlength(bs
);
541 if ((offset
+ size
) > len
)
547 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
552 /* Deal with byte accesses */
554 offset
= -sector_num
;
556 offset
= sector_num
* 512;
558 return bdrv_check_byte_request(bs
, offset
, nb_sectors
* 512);
561 /* return < 0 if error. See bdrv_write() for the return codes */
562 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
563 uint8_t *buf
, int nb_sectors
)
565 BlockDriver
*drv
= bs
->drv
;
569 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
572 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
575 /* Return < 0 if error. Important errors are:
576 -EIO generic I/O error (may happen for all errors)
577 -ENOMEDIUM No media inserted.
578 -EINVAL Invalid sector number or nb_sectors
579 -EACCES Trying to write a read-only device
581 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
582 const uint8_t *buf
, int nb_sectors
)
584 BlockDriver
*drv
= bs
->drv
;
589 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
592 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
595 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
596 void *buf
, int count1
)
598 uint8_t tmp_buf
[SECTOR_SIZE
];
599 int len
, nb_sectors
, count
;
603 /* first read to align to sector start */
604 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
607 sector_num
= offset
>> SECTOR_BITS
;
609 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
611 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
619 /* read the sectors "in place" */
620 nb_sectors
= count
>> SECTOR_BITS
;
621 if (nb_sectors
> 0) {
622 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
624 sector_num
+= nb_sectors
;
625 len
= nb_sectors
<< SECTOR_BITS
;
630 /* add data from the last sector */
632 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
634 memcpy(buf
, tmp_buf
, count
);
639 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
640 const void *buf
, int count1
)
642 uint8_t tmp_buf
[SECTOR_SIZE
];
643 int len
, nb_sectors
, count
;
647 /* first write to align to sector start */
648 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
651 sector_num
= offset
>> SECTOR_BITS
;
653 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
655 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
656 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
665 /* write the sectors "in place" */
666 nb_sectors
= count
>> SECTOR_BITS
;
667 if (nb_sectors
> 0) {
668 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
670 sector_num
+= nb_sectors
;
671 len
= nb_sectors
<< SECTOR_BITS
;
676 /* add data from the last sector */
678 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
680 memcpy(tmp_buf
, buf
, count
);
681 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
688 * Truncate file to 'offset' bytes (needed only for file protocols)
690 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
692 BlockDriver
*drv
= bs
->drv
;
695 if (!drv
->bdrv_truncate
)
697 return drv
->bdrv_truncate(bs
, offset
);
701 * Length of a file in bytes. Return < 0 if error or unknown.
703 int64_t bdrv_getlength(BlockDriverState
*bs
)
705 BlockDriver
*drv
= bs
->drv
;
708 if (!drv
->bdrv_getlength
) {
710 return bs
->total_sectors
* SECTOR_SIZE
;
712 return drv
->bdrv_getlength(bs
);
715 /* return 0 as number of sectors if no device present or error */
716 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
719 length
= bdrv_getlength(bs
);
723 length
= length
>> SECTOR_BITS
;
724 *nb_sectors_ptr
= length
;
728 uint8_t boot_ind
; /* 0x80 - active */
729 uint8_t head
; /* starting head */
730 uint8_t sector
; /* starting sector */
731 uint8_t cyl
; /* starting cylinder */
732 uint8_t sys_ind
; /* What partition type */
733 uint8_t end_head
; /* end head */
734 uint8_t end_sector
; /* end sector */
735 uint8_t end_cyl
; /* end cylinder */
736 uint32_t start_sect
; /* starting sector counting from 0 */
737 uint32_t nr_sects
; /* nr of sectors in partition */
738 } __attribute__((packed
));
740 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
741 static int guess_disk_lchs(BlockDriverState
*bs
,
742 int *pcylinders
, int *pheads
, int *psectors
)
745 int ret
, i
, heads
, sectors
, cylinders
;
750 buf
= qemu_memalign(512, 512);
754 bdrv_get_geometry(bs
, &nb_sectors
);
756 ret
= bdrv_read(bs
, 0, buf
, 1);
759 /* test msdos magic */
760 if (buf
[510] != 0x55 || buf
[511] != 0xaa) {
764 for(i
= 0; i
< 4; i
++) {
765 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
766 nr_sects
= le32_to_cpu(p
->nr_sects
);
767 if (nr_sects
&& p
->end_head
) {
768 /* We make the assumption that the partition terminates on
769 a cylinder boundary */
770 heads
= p
->end_head
+ 1;
771 sectors
= p
->end_sector
& 63;
774 cylinders
= nb_sectors
/ (heads
* sectors
);
775 if (cylinders
< 1 || cylinders
> 16383)
779 *pcylinders
= cylinders
;
781 printf("guessed geometry: LCHS=%d %d %d\n",
782 cylinders
, heads
, sectors
);
792 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
794 int translation
, lba_detected
= 0;
795 int cylinders
, heads
, secs
;
798 /* if a geometry hint is available, use it */
799 bdrv_get_geometry(bs
, &nb_sectors
);
800 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
801 translation
= bdrv_get_translation_hint(bs
);
802 if (cylinders
!= 0) {
807 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
809 /* if heads > 16, it means that a BIOS LBA
810 translation was active, so the default
811 hardware geometry is OK */
813 goto default_geometry
;
818 /* disable any translation to be in sync with
819 the logical geometry */
820 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
821 bdrv_set_translation_hint(bs
,
822 BIOS_ATA_TRANSLATION_NONE
);
827 /* if no geometry, use a standard physical disk geometry */
828 cylinders
= nb_sectors
/ (16 * 63);
830 if (cylinders
> 16383)
832 else if (cylinders
< 2)
837 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
838 if ((*pcyls
* *pheads
) <= 131072) {
839 bdrv_set_translation_hint(bs
,
840 BIOS_ATA_TRANSLATION_LARGE
);
842 bdrv_set_translation_hint(bs
,
843 BIOS_ATA_TRANSLATION_LBA
);
847 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
851 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
852 int cyls
, int heads
, int secs
)
859 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
862 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
863 type
== BDRV_TYPE_FLOPPY
));
866 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
868 bs
->translation
= translation
;
871 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
872 int *pcyls
, int *pheads
, int *psecs
)
879 int bdrv_get_type_hint(BlockDriverState
*bs
)
884 int bdrv_get_translation_hint(BlockDriverState
*bs
)
886 return bs
->translation
;
889 int bdrv_is_removable(BlockDriverState
*bs
)
891 return bs
->removable
;
894 int bdrv_is_read_only(BlockDriverState
*bs
)
896 return bs
->read_only
;
899 int bdrv_is_sg(BlockDriverState
*bs
)
904 /* XXX: no longer used */
905 void bdrv_set_change_cb(BlockDriverState
*bs
,
906 void (*change_cb
)(void *opaque
), void *opaque
)
908 bs
->change_cb
= change_cb
;
909 bs
->change_opaque
= opaque
;
912 int bdrv_is_encrypted(BlockDriverState
*bs
)
914 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
916 return bs
->encrypted
;
919 int bdrv_key_required(BlockDriverState
*bs
)
921 BlockDriverState
*backing_hd
= bs
->backing_hd
;
923 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
925 return (bs
->encrypted
&& !bs
->valid_key
);
928 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
931 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
932 ret
= bdrv_set_key(bs
->backing_hd
, key
);
938 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
940 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
943 } else if (!bs
->valid_key
) {
945 /* call the change callback now, we skipped it on open */
946 bs
->media_changed
= 1;
948 bs
->change_cb(bs
->change_opaque
);
953 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
958 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
962 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
967 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
968 it(opaque
, drv
->format_name
);
972 BlockDriverState
*bdrv_find(const char *name
)
974 BlockDriverState
*bs
;
976 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
977 if (!strcmp(name
, bs
->device_name
))
983 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
985 BlockDriverState
*bs
;
987 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
992 const char *bdrv_get_device_name(BlockDriverState
*bs
)
994 return bs
->device_name
;
997 void bdrv_flush(BlockDriverState
*bs
)
999 if (bs
->drv
->bdrv_flush
)
1000 bs
->drv
->bdrv_flush(bs
);
1002 bdrv_flush(bs
->backing_hd
);
1005 void bdrv_flush_all(void)
1007 BlockDriverState
*bs
;
1009 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1010 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1011 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1016 * Returns true iff the specified sector is present in the disk image. Drivers
1017 * not implementing the functionality are assumed to not support backing files,
1018 * hence all their sectors are reported as allocated.
1020 * 'pnum' is set to the number of sectors (including and immediately following
1021 * the specified sector) that are known to be in the same
1022 * allocated/unallocated state.
1024 * 'nb_sectors' is the max value 'pnum' should be set to.
1026 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1030 if (!bs
->drv
->bdrv_is_allocated
) {
1031 if (sector_num
>= bs
->total_sectors
) {
1035 n
= bs
->total_sectors
- sector_num
;
1036 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1039 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1042 void bdrv_info(Monitor
*mon
)
1044 BlockDriverState
*bs
;
1046 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1047 monitor_printf(mon
, "%s:", bs
->device_name
);
1048 monitor_printf(mon
, " type=");
1051 monitor_printf(mon
, "hd");
1053 case BDRV_TYPE_CDROM
:
1054 monitor_printf(mon
, "cdrom");
1056 case BDRV_TYPE_FLOPPY
:
1057 monitor_printf(mon
, "floppy");
1060 monitor_printf(mon
, " removable=%d", bs
->removable
);
1061 if (bs
->removable
) {
1062 monitor_printf(mon
, " locked=%d", bs
->locked
);
1065 monitor_printf(mon
, " file=");
1066 monitor_print_filename(mon
, bs
->filename
);
1067 if (bs
->backing_file
[0] != '\0') {
1068 monitor_printf(mon
, " backing_file=");
1069 monitor_print_filename(mon
, bs
->backing_file
);
1071 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1072 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1073 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1075 monitor_printf(mon
, " [not inserted]");
1077 monitor_printf(mon
, "\n");
1081 /* The "info blockstats" command. */
1082 void bdrv_info_stats(Monitor
*mon
)
1084 BlockDriverState
*bs
;
1086 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1087 monitor_printf(mon
, "%s:"
1088 " rd_bytes=%" PRIu64
1089 " wr_bytes=%" PRIu64
1090 " rd_operations=%" PRIu64
1091 " wr_operations=%" PRIu64
1094 bs
->rd_bytes
, bs
->wr_bytes
,
1095 bs
->rd_ops
, bs
->wr_ops
);
1099 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1101 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1102 return bs
->backing_file
;
1103 else if (bs
->encrypted
)
1104 return bs
->filename
;
1109 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1110 char *filename
, int filename_size
)
1112 if (!bs
->backing_hd
) {
1113 pstrcpy(filename
, filename_size
, "");
1115 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1119 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1120 const uint8_t *buf
, int nb_sectors
)
1122 BlockDriver
*drv
= bs
->drv
;
1125 if (!drv
->bdrv_write_compressed
)
1127 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1130 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1132 BlockDriver
*drv
= bs
->drv
;
1135 if (!drv
->bdrv_get_info
)
1137 memset(bdi
, 0, sizeof(*bdi
));
1138 return drv
->bdrv_get_info(bs
, bdi
);
1141 /**************************************************************/
1142 /* handling of snapshots */
1144 int bdrv_snapshot_create(BlockDriverState
*bs
,
1145 QEMUSnapshotInfo
*sn_info
)
1147 BlockDriver
*drv
= bs
->drv
;
1150 if (!drv
->bdrv_snapshot_create
)
1152 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1155 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1156 const char *snapshot_id
)
1158 BlockDriver
*drv
= bs
->drv
;
1161 if (!drv
->bdrv_snapshot_goto
)
1163 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1166 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1168 BlockDriver
*drv
= bs
->drv
;
1171 if (!drv
->bdrv_snapshot_delete
)
1173 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1176 int bdrv_snapshot_list(BlockDriverState
*bs
,
1177 QEMUSnapshotInfo
**psn_info
)
1179 BlockDriver
*drv
= bs
->drv
;
1182 if (!drv
->bdrv_snapshot_list
)
1184 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1187 #define NB_SUFFIXES 4
1189 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1191 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1196 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1199 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1200 if (size
< (10 * base
)) {
1201 snprintf(buf
, buf_size
, "%0.1f%c",
1202 (double)size
/ base
,
1205 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1206 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1207 ((size
+ (base
>> 1)) / base
),
1217 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1219 char buf1
[128], date_buf
[128], clock_buf
[128];
1229 snprintf(buf
, buf_size
,
1230 "%-10s%-20s%7s%20s%15s",
1231 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1235 ptm
= localtime(&ti
);
1236 strftime(date_buf
, sizeof(date_buf
),
1237 "%Y-%m-%d %H:%M:%S", ptm
);
1239 localtime_r(&ti
, &tm
);
1240 strftime(date_buf
, sizeof(date_buf
),
1241 "%Y-%m-%d %H:%M:%S", &tm
);
1243 secs
= sn
->vm_clock_nsec
/ 1000000000;
1244 snprintf(clock_buf
, sizeof(clock_buf
),
1245 "%02d:%02d:%02d.%03d",
1247 (int)((secs
/ 60) % 60),
1249 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1250 snprintf(buf
, buf_size
,
1251 "%-10s%-20s%7s%20s%15s",
1252 sn
->id_str
, sn
->name
,
1253 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1261 /**************************************************************/
1264 typedef struct VectorTranslationState
{
1268 BlockDriverAIOCB
*aiocb
;
1269 BlockDriverAIOCB
*this_aiocb
;
1270 } VectorTranslationState
;
1272 static void bdrv_aio_rw_vector_cb(void *opaque
, int ret
)
1274 VectorTranslationState
*s
= opaque
;
1277 qemu_iovec_from_buffer(s
->iov
, s
->bounce
, s
->iov
->size
);
1279 qemu_vfree(s
->bounce
);
1280 s
->this_aiocb
->cb(s
->this_aiocb
->opaque
, ret
);
1281 qemu_aio_release(s
->this_aiocb
);
1284 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1288 BlockDriverCompletionFunc
*cb
,
1293 VectorTranslationState
*s
= qemu_mallocz(sizeof(*s
));
1294 BlockDriverAIOCB
*aiocb
= qemu_aio_get(bs
, cb
, opaque
);
1296 s
->this_aiocb
= aiocb
;
1298 s
->bounce
= qemu_memalign(512, nb_sectors
* 512);
1299 s
->is_write
= is_write
;
1301 qemu_iovec_to_buffer(s
->iov
, s
->bounce
);
1302 s
->aiocb
= bdrv_aio_write(bs
, sector_num
, s
->bounce
, nb_sectors
,
1303 bdrv_aio_rw_vector_cb
, s
);
1305 s
->aiocb
= bdrv_aio_read(bs
, sector_num
, s
->bounce
, nb_sectors
,
1306 bdrv_aio_rw_vector_cb
, s
);
1311 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1312 QEMUIOVector
*iov
, int nb_sectors
,
1313 BlockDriverCompletionFunc
*cb
, void *opaque
)
1315 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1318 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1322 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1323 QEMUIOVector
*iov
, int nb_sectors
,
1324 BlockDriverCompletionFunc
*cb
, void *opaque
)
1326 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1329 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1333 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1334 uint8_t *buf
, int nb_sectors
,
1335 BlockDriverCompletionFunc
*cb
, void *opaque
)
1337 BlockDriver
*drv
= bs
->drv
;
1338 BlockDriverAIOCB
*ret
;
1342 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1345 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1348 /* Update stats even though technically transfer has not happened. */
1349 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1356 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1357 const uint8_t *buf
, int nb_sectors
,
1358 BlockDriverCompletionFunc
*cb
, void *opaque
)
1360 BlockDriver
*drv
= bs
->drv
;
1361 BlockDriverAIOCB
*ret
;
1367 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1370 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1373 /* Update stats even though technically transfer has not happened. */
1374 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1381 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1383 BlockDriver
*drv
= acb
->bs
->drv
;
1385 if (acb
->cb
== bdrv_aio_rw_vector_cb
) {
1386 VectorTranslationState
*s
= acb
->opaque
;
1390 drv
->bdrv_aio_cancel(acb
);
1394 /**************************************************************/
1395 /* async block device emulation */
1397 static void bdrv_aio_bh_cb(void *opaque
)
1399 BlockDriverAIOCBSync
*acb
= opaque
;
1400 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1401 qemu_aio_release(acb
);
1404 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1405 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1406 BlockDriverCompletionFunc
*cb
, void *opaque
)
1408 BlockDriverAIOCBSync
*acb
;
1411 acb
= qemu_aio_get(bs
, cb
, opaque
);
1413 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1414 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1416 qemu_bh_schedule(acb
->bh
);
1417 return &acb
->common
;
1420 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1421 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1422 BlockDriverCompletionFunc
*cb
, void *opaque
)
1424 BlockDriverAIOCBSync
*acb
;
1427 acb
= qemu_aio_get(bs
, cb
, opaque
);
1429 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1430 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1432 qemu_bh_schedule(acb
->bh
);
1433 return &acb
->common
;
1436 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1438 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1439 qemu_bh_cancel(acb
->bh
);
1440 qemu_aio_release(acb
);
1443 /**************************************************************/
1444 /* sync block device emulation */
1446 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1448 *(int *)opaque
= ret
;
1451 #define NOT_DONE 0x7fffffff
1453 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1454 uint8_t *buf
, int nb_sectors
)
1457 BlockDriverAIOCB
*acb
;
1459 async_ret
= NOT_DONE
;
1460 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1461 bdrv_rw_em_cb
, &async_ret
);
1465 while (async_ret
== NOT_DONE
) {
1472 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1473 const uint8_t *buf
, int nb_sectors
)
1476 BlockDriverAIOCB
*acb
;
1478 async_ret
= NOT_DONE
;
1479 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1480 bdrv_rw_em_cb
, &async_ret
);
1483 while (async_ret
== NOT_DONE
) {
1489 void bdrv_init(void)
1491 bdrv_register(&bdrv_raw
);
1492 bdrv_register(&bdrv_host_device
);
1494 bdrv_register(&bdrv_cow
);
1496 bdrv_register(&bdrv_qcow
);
1497 bdrv_register(&bdrv_vmdk
);
1498 bdrv_register(&bdrv_cloop
);
1499 bdrv_register(&bdrv_dmg
);
1500 bdrv_register(&bdrv_bochs
);
1501 bdrv_register(&bdrv_vpc
);
1502 bdrv_register(&bdrv_vvfat
);
1503 bdrv_register(&bdrv_qcow2
);
1504 bdrv_register(&bdrv_parallels
);
1505 bdrv_register(&bdrv_nbd
);
1508 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1512 BlockDriverAIOCB
*acb
;
1515 if (drv
->free_aiocb
) {
1516 acb
= drv
->free_aiocb
;
1517 drv
->free_aiocb
= acb
->next
;
1519 acb
= qemu_mallocz(drv
->aiocb_size
);
1523 acb
->opaque
= opaque
;
1527 void qemu_aio_release(void *p
)
1529 BlockDriverAIOCB
*acb
= p
;
1530 BlockDriver
*drv
= acb
->bs
->drv
;
1531 acb
->next
= drv
->free_aiocb
;
1532 drv
->free_aiocb
= acb
;
1535 /**************************************************************/
1536 /* removable device support */
1539 * Return TRUE if the media is present
1541 int bdrv_is_inserted(BlockDriverState
*bs
)
1543 BlockDriver
*drv
= bs
->drv
;
1547 if (!drv
->bdrv_is_inserted
)
1549 ret
= drv
->bdrv_is_inserted(bs
);
1554 * Return TRUE if the media changed since the last call to this
1555 * function. It is currently only used for floppy disks
1557 int bdrv_media_changed(BlockDriverState
*bs
)
1559 BlockDriver
*drv
= bs
->drv
;
1562 if (!drv
|| !drv
->bdrv_media_changed
)
1565 ret
= drv
->bdrv_media_changed(bs
);
1566 if (ret
== -ENOTSUP
)
1567 ret
= bs
->media_changed
;
1568 bs
->media_changed
= 0;
1573 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1575 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1577 BlockDriver
*drv
= bs
->drv
;
1580 if (!drv
|| !drv
->bdrv_eject
) {
1583 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1585 if (ret
== -ENOTSUP
) {
1591 int bdrv_is_locked(BlockDriverState
*bs
)
1597 * Lock or unlock the media (if it is locked, the user won't be able
1598 * to eject it manually).
1600 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1602 BlockDriver
*drv
= bs
->drv
;
1604 bs
->locked
= locked
;
1605 if (drv
&& drv
->bdrv_set_locked
) {
1606 drv
->bdrv_set_locked(bs
, locked
);
1610 /* needed for generic scsi interface */
1612 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1614 BlockDriver
*drv
= bs
->drv
;
1616 if (drv
&& drv
->bdrv_ioctl
)
1617 return drv
->bdrv_ioctl(bs
, req
, buf
);
1621 int bdrv_sg_send_command(BlockDriverState
*bs
, void *buf
, int count
)
1623 return bs
->drv
->bdrv_sg_send_command(bs
, buf
, count
);
1626 int bdrv_sg_recv_response(BlockDriverState
*bs
, void *buf
, int count
)
1628 return bs
->drv
->bdrv_sg_recv_response(bs
, buf
, count
);
1631 BlockDriverAIOCB
*bdrv_sg_aio_read(BlockDriverState
*bs
, void *buf
, int count
,
1632 BlockDriverCompletionFunc
*cb
, void *opaque
)
1634 return bs
->drv
->bdrv_sg_aio_read(bs
, buf
, count
, cb
, opaque
);
1637 BlockDriverAIOCB
*bdrv_sg_aio_write(BlockDriverState
*bs
, void *buf
, int count
,
1638 BlockDriverCompletionFunc
*cb
, void *opaque
)
1640 return bs
->drv
->bdrv_sg_aio_write(bs
, buf
, count
, cb
, opaque
);