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 typedef struct BlockDriverAIOCBSync
{
51 BlockDriverAIOCB common
;
54 } BlockDriverAIOCBSync
;
56 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
57 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
58 BlockDriverCompletionFunc
*cb
, void *opaque
);
59 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
60 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
61 BlockDriverCompletionFunc
*cb
, void *opaque
);
62 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
63 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
64 uint8_t *buf
, int nb_sectors
);
65 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
66 const uint8_t *buf
, int nb_sectors
);
68 BlockDriverState
*bdrv_first
;
70 static BlockDriver
*first_drv
;
72 int path_is_absolute(const char *path
)
76 /* specific case for names like: "\\.\d:" */
77 if (*path
== '/' || *path
== '\\')
80 p
= strchr(path
, ':');
86 return (*p
== '/' || *p
== '\\');
92 /* if filename is absolute, just copy it to dest. Otherwise, build a
93 path to it by considering it is relative to base_path. URL are
95 void path_combine(char *dest
, int dest_size
,
96 const char *base_path
,
104 if (path_is_absolute(filename
)) {
105 pstrcpy(dest
, dest_size
, filename
);
107 p
= strchr(base_path
, ':');
112 p1
= strrchr(base_path
, '/');
116 p2
= strrchr(base_path
, '\\');
128 if (len
> dest_size
- 1)
130 memcpy(dest
, base_path
, len
);
132 pstrcat(dest
, dest_size
, filename
);
137 static void bdrv_register(BlockDriver
*bdrv
)
139 if (!bdrv
->bdrv_aio_read
) {
140 /* add AIO emulation layer */
141 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
142 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
143 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
144 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
145 } else if (!bdrv
->bdrv_read
) {
146 /* add synchronous IO emulation layer */
147 bdrv
->bdrv_read
= bdrv_read_em
;
148 bdrv
->bdrv_write
= bdrv_write_em
;
150 aio_pool_init(&bdrv
->aio_pool
, bdrv
->aiocb_size
, bdrv
->bdrv_aio_cancel
);
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 bdrv_get_geometry(bs
, &nb_sectors
);
752 ret
= bdrv_read(bs
, 0, buf
, 1);
755 /* test msdos magic */
756 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
758 for(i
= 0; i
< 4; i
++) {
759 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
760 nr_sects
= le32_to_cpu(p
->nr_sects
);
761 if (nr_sects
&& p
->end_head
) {
762 /* We make the assumption that the partition terminates on
763 a cylinder boundary */
764 heads
= p
->end_head
+ 1;
765 sectors
= p
->end_sector
& 63;
768 cylinders
= nb_sectors
/ (heads
* sectors
);
769 if (cylinders
< 1 || cylinders
> 16383)
773 *pcylinders
= cylinders
;
775 printf("guessed geometry: LCHS=%d %d %d\n",
776 cylinders
, heads
, sectors
);
784 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
786 int translation
, lba_detected
= 0;
787 int cylinders
, heads
, secs
;
790 /* if a geometry hint is available, use it */
791 bdrv_get_geometry(bs
, &nb_sectors
);
792 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
793 translation
= bdrv_get_translation_hint(bs
);
794 if (cylinders
!= 0) {
799 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
801 /* if heads > 16, it means that a BIOS LBA
802 translation was active, so the default
803 hardware geometry is OK */
805 goto default_geometry
;
810 /* disable any translation to be in sync with
811 the logical geometry */
812 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
813 bdrv_set_translation_hint(bs
,
814 BIOS_ATA_TRANSLATION_NONE
);
819 /* if no geometry, use a standard physical disk geometry */
820 cylinders
= nb_sectors
/ (16 * 63);
822 if (cylinders
> 16383)
824 else if (cylinders
< 2)
829 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
830 if ((*pcyls
* *pheads
) <= 131072) {
831 bdrv_set_translation_hint(bs
,
832 BIOS_ATA_TRANSLATION_LARGE
);
834 bdrv_set_translation_hint(bs
,
835 BIOS_ATA_TRANSLATION_LBA
);
839 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
843 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
844 int cyls
, int heads
, int secs
)
851 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
854 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
855 type
== BDRV_TYPE_FLOPPY
));
858 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
860 bs
->translation
= translation
;
863 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
864 int *pcyls
, int *pheads
, int *psecs
)
871 int bdrv_get_type_hint(BlockDriverState
*bs
)
876 int bdrv_get_translation_hint(BlockDriverState
*bs
)
878 return bs
->translation
;
881 int bdrv_is_removable(BlockDriverState
*bs
)
883 return bs
->removable
;
886 int bdrv_is_read_only(BlockDriverState
*bs
)
888 return bs
->read_only
;
891 int bdrv_is_sg(BlockDriverState
*bs
)
896 /* XXX: no longer used */
897 void bdrv_set_change_cb(BlockDriverState
*bs
,
898 void (*change_cb
)(void *opaque
), void *opaque
)
900 bs
->change_cb
= change_cb
;
901 bs
->change_opaque
= opaque
;
904 int bdrv_is_encrypted(BlockDriverState
*bs
)
906 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
908 return bs
->encrypted
;
911 int bdrv_key_required(BlockDriverState
*bs
)
913 BlockDriverState
*backing_hd
= bs
->backing_hd
;
915 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
917 return (bs
->encrypted
&& !bs
->valid_key
);
920 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
923 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
924 ret
= bdrv_set_key(bs
->backing_hd
, key
);
930 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
932 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
935 } else if (!bs
->valid_key
) {
937 /* call the change callback now, we skipped it on open */
938 bs
->media_changed
= 1;
940 bs
->change_cb(bs
->change_opaque
);
945 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
950 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
954 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
959 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
960 it(opaque
, drv
->format_name
);
964 BlockDriverState
*bdrv_find(const char *name
)
966 BlockDriverState
*bs
;
968 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
969 if (!strcmp(name
, bs
->device_name
))
975 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
977 BlockDriverState
*bs
;
979 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
984 const char *bdrv_get_device_name(BlockDriverState
*bs
)
986 return bs
->device_name
;
989 void bdrv_flush(BlockDriverState
*bs
)
991 if (bs
->drv
->bdrv_flush
)
992 bs
->drv
->bdrv_flush(bs
);
994 bdrv_flush(bs
->backing_hd
);
997 void bdrv_flush_all(void)
999 BlockDriverState
*bs
;
1001 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1002 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1003 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1008 * Returns true iff the specified sector is present in the disk image. Drivers
1009 * not implementing the functionality are assumed to not support backing files,
1010 * hence all their sectors are reported as allocated.
1012 * 'pnum' is set to the number of sectors (including and immediately following
1013 * the specified sector) that are known to be in the same
1014 * allocated/unallocated state.
1016 * 'nb_sectors' is the max value 'pnum' should be set to.
1018 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1022 if (!bs
->drv
->bdrv_is_allocated
) {
1023 if (sector_num
>= bs
->total_sectors
) {
1027 n
= bs
->total_sectors
- sector_num
;
1028 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1031 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1034 void bdrv_info(Monitor
*mon
)
1036 BlockDriverState
*bs
;
1038 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1039 monitor_printf(mon
, "%s:", bs
->device_name
);
1040 monitor_printf(mon
, " type=");
1043 monitor_printf(mon
, "hd");
1045 case BDRV_TYPE_CDROM
:
1046 monitor_printf(mon
, "cdrom");
1048 case BDRV_TYPE_FLOPPY
:
1049 monitor_printf(mon
, "floppy");
1052 monitor_printf(mon
, " removable=%d", bs
->removable
);
1053 if (bs
->removable
) {
1054 monitor_printf(mon
, " locked=%d", bs
->locked
);
1057 monitor_printf(mon
, " file=");
1058 monitor_print_filename(mon
, bs
->filename
);
1059 if (bs
->backing_file
[0] != '\0') {
1060 monitor_printf(mon
, " backing_file=");
1061 monitor_print_filename(mon
, bs
->backing_file
);
1063 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1064 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1065 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1067 monitor_printf(mon
, " [not inserted]");
1069 monitor_printf(mon
, "\n");
1073 /* The "info blockstats" command. */
1074 void bdrv_info_stats(Monitor
*mon
)
1076 BlockDriverState
*bs
;
1078 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1079 monitor_printf(mon
, "%s:"
1080 " rd_bytes=%" PRIu64
1081 " wr_bytes=%" PRIu64
1082 " rd_operations=%" PRIu64
1083 " wr_operations=%" PRIu64
1086 bs
->rd_bytes
, bs
->wr_bytes
,
1087 bs
->rd_ops
, bs
->wr_ops
);
1091 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1093 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1094 return bs
->backing_file
;
1095 else if (bs
->encrypted
)
1096 return bs
->filename
;
1101 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1102 char *filename
, int filename_size
)
1104 if (!bs
->backing_hd
) {
1105 pstrcpy(filename
, filename_size
, "");
1107 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1111 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1112 const uint8_t *buf
, int nb_sectors
)
1114 BlockDriver
*drv
= bs
->drv
;
1117 if (!drv
->bdrv_write_compressed
)
1119 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1122 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1124 BlockDriver
*drv
= bs
->drv
;
1127 if (!drv
->bdrv_get_info
)
1129 memset(bdi
, 0, sizeof(*bdi
));
1130 return drv
->bdrv_get_info(bs
, bdi
);
1133 /**************************************************************/
1134 /* handling of snapshots */
1136 int bdrv_snapshot_create(BlockDriverState
*bs
,
1137 QEMUSnapshotInfo
*sn_info
)
1139 BlockDriver
*drv
= bs
->drv
;
1142 if (!drv
->bdrv_snapshot_create
)
1144 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1147 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1148 const char *snapshot_id
)
1150 BlockDriver
*drv
= bs
->drv
;
1153 if (!drv
->bdrv_snapshot_goto
)
1155 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1158 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1160 BlockDriver
*drv
= bs
->drv
;
1163 if (!drv
->bdrv_snapshot_delete
)
1165 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1168 int bdrv_snapshot_list(BlockDriverState
*bs
,
1169 QEMUSnapshotInfo
**psn_info
)
1171 BlockDriver
*drv
= bs
->drv
;
1174 if (!drv
->bdrv_snapshot_list
)
1176 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1179 #define NB_SUFFIXES 4
1181 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1183 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1188 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1191 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1192 if (size
< (10 * base
)) {
1193 snprintf(buf
, buf_size
, "%0.1f%c",
1194 (double)size
/ base
,
1197 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1198 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1199 ((size
+ (base
>> 1)) / base
),
1209 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1211 char buf1
[128], date_buf
[128], clock_buf
[128];
1221 snprintf(buf
, buf_size
,
1222 "%-10s%-20s%7s%20s%15s",
1223 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1227 ptm
= localtime(&ti
);
1228 strftime(date_buf
, sizeof(date_buf
),
1229 "%Y-%m-%d %H:%M:%S", ptm
);
1231 localtime_r(&ti
, &tm
);
1232 strftime(date_buf
, sizeof(date_buf
),
1233 "%Y-%m-%d %H:%M:%S", &tm
);
1235 secs
= sn
->vm_clock_nsec
/ 1000000000;
1236 snprintf(clock_buf
, sizeof(clock_buf
),
1237 "%02d:%02d:%02d.%03d",
1239 (int)((secs
/ 60) % 60),
1241 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1242 snprintf(buf
, buf_size
,
1243 "%-10s%-20s%7s%20s%15s",
1244 sn
->id_str
, sn
->name
,
1245 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1253 /**************************************************************/
1256 typedef struct VectorTranslationState
{
1260 BlockDriverAIOCB
*aiocb
;
1261 BlockDriverAIOCB
*this_aiocb
;
1262 } VectorTranslationState
;
1264 static void bdrv_aio_rw_vector_cb(void *opaque
, int ret
)
1266 VectorTranslationState
*s
= opaque
;
1269 qemu_iovec_from_buffer(s
->iov
, s
->bounce
, s
->iov
->size
);
1271 qemu_vfree(s
->bounce
);
1272 s
->this_aiocb
->cb(s
->this_aiocb
->opaque
, ret
);
1273 qemu_aio_release(s
->this_aiocb
);
1276 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1280 BlockDriverCompletionFunc
*cb
,
1285 VectorTranslationState
*s
= qemu_mallocz(sizeof(*s
));
1286 BlockDriverAIOCB
*aiocb
= qemu_aio_get(bs
, cb
, opaque
);
1288 s
->this_aiocb
= aiocb
;
1290 s
->bounce
= qemu_memalign(512, nb_sectors
* 512);
1291 s
->is_write
= is_write
;
1293 qemu_iovec_to_buffer(s
->iov
, s
->bounce
);
1294 s
->aiocb
= bdrv_aio_write(bs
, sector_num
, s
->bounce
, nb_sectors
,
1295 bdrv_aio_rw_vector_cb
, s
);
1297 s
->aiocb
= bdrv_aio_read(bs
, sector_num
, s
->bounce
, nb_sectors
,
1298 bdrv_aio_rw_vector_cb
, s
);
1303 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1304 QEMUIOVector
*iov
, int nb_sectors
,
1305 BlockDriverCompletionFunc
*cb
, void *opaque
)
1307 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1310 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1314 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1315 QEMUIOVector
*iov
, int nb_sectors
,
1316 BlockDriverCompletionFunc
*cb
, void *opaque
)
1318 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1321 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1325 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1326 uint8_t *buf
, int nb_sectors
,
1327 BlockDriverCompletionFunc
*cb
, void *opaque
)
1329 BlockDriver
*drv
= bs
->drv
;
1330 BlockDriverAIOCB
*ret
;
1334 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1337 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1340 /* Update stats even though technically transfer has not happened. */
1341 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1348 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1349 const uint8_t *buf
, int nb_sectors
,
1350 BlockDriverCompletionFunc
*cb
, void *opaque
)
1352 BlockDriver
*drv
= bs
->drv
;
1353 BlockDriverAIOCB
*ret
;
1359 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1362 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1365 /* Update stats even though technically transfer has not happened. */
1366 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1373 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1375 if (acb
->cb
== bdrv_aio_rw_vector_cb
) {
1376 VectorTranslationState
*s
= acb
->opaque
;
1380 acb
->pool
->cancel(acb
);
1384 /**************************************************************/
1385 /* async block device emulation */
1387 static void bdrv_aio_bh_cb(void *opaque
)
1389 BlockDriverAIOCBSync
*acb
= opaque
;
1390 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1391 qemu_aio_release(acb
);
1394 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1395 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1396 BlockDriverCompletionFunc
*cb
, void *opaque
)
1398 BlockDriverAIOCBSync
*acb
;
1401 acb
= qemu_aio_get(bs
, cb
, opaque
);
1403 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1404 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1406 qemu_bh_schedule(acb
->bh
);
1407 return &acb
->common
;
1410 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1411 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1412 BlockDriverCompletionFunc
*cb
, void *opaque
)
1414 BlockDriverAIOCBSync
*acb
;
1417 acb
= qemu_aio_get(bs
, cb
, opaque
);
1419 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1420 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1422 qemu_bh_schedule(acb
->bh
);
1423 return &acb
->common
;
1426 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1428 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1429 qemu_bh_cancel(acb
->bh
);
1430 qemu_aio_release(acb
);
1433 /**************************************************************/
1434 /* sync block device emulation */
1436 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1438 *(int *)opaque
= ret
;
1441 #define NOT_DONE 0x7fffffff
1443 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1444 uint8_t *buf
, int nb_sectors
)
1447 BlockDriverAIOCB
*acb
;
1449 async_ret
= NOT_DONE
;
1450 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1451 bdrv_rw_em_cb
, &async_ret
);
1455 while (async_ret
== NOT_DONE
) {
1462 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1463 const uint8_t *buf
, int nb_sectors
)
1466 BlockDriverAIOCB
*acb
;
1468 async_ret
= NOT_DONE
;
1469 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1470 bdrv_rw_em_cb
, &async_ret
);
1473 while (async_ret
== NOT_DONE
) {
1479 void bdrv_init(void)
1481 bdrv_register(&bdrv_raw
);
1482 bdrv_register(&bdrv_host_device
);
1484 bdrv_register(&bdrv_cow
);
1486 bdrv_register(&bdrv_qcow
);
1487 bdrv_register(&bdrv_vmdk
);
1488 bdrv_register(&bdrv_cloop
);
1489 bdrv_register(&bdrv_dmg
);
1490 bdrv_register(&bdrv_bochs
);
1491 bdrv_register(&bdrv_vpc
);
1492 bdrv_register(&bdrv_vvfat
);
1493 bdrv_register(&bdrv_qcow2
);
1494 bdrv_register(&bdrv_parallels
);
1495 bdrv_register(&bdrv_nbd
);
1498 void aio_pool_init(AIOPool
*pool
, int aiocb_size
,
1499 void (*cancel
)(BlockDriverAIOCB
*acb
))
1501 pool
->aiocb_size
= aiocb_size
;
1502 pool
->cancel
= cancel
;
1503 pool
->free_aiocb
= NULL
;
1506 void *qemu_aio_get_pool(AIOPool
*pool
, BlockDriverState
*bs
,
1507 BlockDriverCompletionFunc
*cb
, void *opaque
)
1509 BlockDriverAIOCB
*acb
;
1511 if (pool
->free_aiocb
) {
1512 acb
= pool
->free_aiocb
;
1513 pool
->free_aiocb
= acb
->next
;
1515 acb
= qemu_mallocz(pool
->aiocb_size
);
1520 acb
->opaque
= opaque
;
1524 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1527 return qemu_aio_get_pool(&bs
->drv
->aio_pool
, bs
, cb
, opaque
);
1530 void qemu_aio_release(void *p
)
1532 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1533 AIOPool
*pool
= acb
->pool
;
1534 acb
->next
= pool
->free_aiocb
;
1535 pool
->free_aiocb
= acb
;
1538 /**************************************************************/
1539 /* removable device support */
1542 * Return TRUE if the media is present
1544 int bdrv_is_inserted(BlockDriverState
*bs
)
1546 BlockDriver
*drv
= bs
->drv
;
1550 if (!drv
->bdrv_is_inserted
)
1552 ret
= drv
->bdrv_is_inserted(bs
);
1557 * Return TRUE if the media changed since the last call to this
1558 * function. It is currently only used for floppy disks
1560 int bdrv_media_changed(BlockDriverState
*bs
)
1562 BlockDriver
*drv
= bs
->drv
;
1565 if (!drv
|| !drv
->bdrv_media_changed
)
1568 ret
= drv
->bdrv_media_changed(bs
);
1569 if (ret
== -ENOTSUP
)
1570 ret
= bs
->media_changed
;
1571 bs
->media_changed
= 0;
1576 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1578 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1580 BlockDriver
*drv
= bs
->drv
;
1583 if (!drv
|| !drv
->bdrv_eject
) {
1586 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1588 if (ret
== -ENOTSUP
) {
1594 int bdrv_is_locked(BlockDriverState
*bs
)
1600 * Lock or unlock the media (if it is locked, the user won't be able
1601 * to eject it manually).
1603 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1605 BlockDriver
*drv
= bs
->drv
;
1607 bs
->locked
= locked
;
1608 if (drv
&& drv
->bdrv_set_locked
) {
1609 drv
->bdrv_set_locked(bs
, locked
);
1613 /* needed for generic scsi interface */
1615 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1617 BlockDriver
*drv
= bs
->drv
;
1619 if (drv
&& drv
->bdrv_ioctl
)
1620 return drv
->bdrv_ioctl(bs
, req
, buf
);
1624 int bdrv_sg_send_command(BlockDriverState
*bs
, void *buf
, int count
)
1626 return bs
->drv
->bdrv_sg_send_command(bs
, buf
, count
);
1629 int bdrv_sg_recv_response(BlockDriverState
*bs
, void *buf
, int count
)
1631 return bs
->drv
->bdrv_sg_recv_response(bs
, buf
, count
);
1634 BlockDriverAIOCB
*bdrv_sg_aio_read(BlockDriverState
*bs
, void *buf
, int count
,
1635 BlockDriverCompletionFunc
*cb
, void *opaque
)
1637 return bs
->drv
->bdrv_sg_aio_read(bs
, buf
, count
, cb
, opaque
);
1640 BlockDriverAIOCB
*bdrv_sg_aio_write(BlockDriverState
*bs
, void *buf
, int count
,
1641 BlockDriverCompletionFunc
*cb
, void *opaque
)
1643 return bs
->drv
->bdrv_sg_aio_write(bs
, buf
, count
, cb
, opaque
);