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>
42 #define SECTOR_SIZE (1 << SECTOR_BITS)
44 static AIOPool vectored_aio_pool
;
46 typedef struct BlockDriverAIOCBSync
{
47 BlockDriverAIOCB common
;
50 } BlockDriverAIOCBSync
;
52 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
53 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
54 BlockDriverCompletionFunc
*cb
, void *opaque
);
55 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
56 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
57 BlockDriverCompletionFunc
*cb
, void *opaque
);
58 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
59 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
60 uint8_t *buf
, int nb_sectors
);
61 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
62 const uint8_t *buf
, int nb_sectors
);
64 BlockDriverState
*bdrv_first
;
66 static BlockDriver
*first_drv
;
68 int path_is_absolute(const char *path
)
72 /* specific case for names like: "\\.\d:" */
73 if (*path
== '/' || *path
== '\\')
76 p
= strchr(path
, ':');
82 return (*p
== '/' || *p
== '\\');
88 /* if filename is absolute, just copy it to dest. Otherwise, build a
89 path to it by considering it is relative to base_path. URL are
91 void path_combine(char *dest
, int dest_size
,
92 const char *base_path
,
100 if (path_is_absolute(filename
)) {
101 pstrcpy(dest
, dest_size
, filename
);
103 p
= strchr(base_path
, ':');
108 p1
= strrchr(base_path
, '/');
112 p2
= strrchr(base_path
, '\\');
124 if (len
> dest_size
- 1)
126 memcpy(dest
, base_path
, len
);
128 pstrcat(dest
, dest_size
, filename
);
133 static void bdrv_register(BlockDriver
*bdrv
)
135 if (!bdrv
->bdrv_aio_read
) {
136 /* add AIO emulation layer */
137 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
138 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
139 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
140 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
141 } else if (!bdrv
->bdrv_read
&& !bdrv
->bdrv_pread
) {
142 /* add synchronous IO emulation layer */
143 bdrv
->bdrv_read
= bdrv_read_em
;
144 bdrv
->bdrv_write
= bdrv_write_em
;
146 aio_pool_init(&bdrv
->aio_pool
, bdrv
->aiocb_size
, bdrv
->bdrv_aio_cancel
);
147 bdrv
->next
= first_drv
;
151 /* create a new block device (by default it is empty) */
152 BlockDriverState
*bdrv_new(const char *device_name
)
154 BlockDriverState
**pbs
, *bs
;
156 bs
= qemu_mallocz(sizeof(BlockDriverState
));
157 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
158 if (device_name
[0] != '\0') {
159 /* insert at the end */
168 BlockDriver
*bdrv_find_format(const char *format_name
)
171 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
172 if (!strcmp(drv1
->format_name
, format_name
))
178 int bdrv_create(BlockDriver
*drv
,
179 const char *filename
, int64_t size_in_sectors
,
180 const char *backing_file
, int flags
)
182 if (!drv
->bdrv_create
)
184 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
188 void get_tmp_filename(char *filename
, int size
)
190 char temp_dir
[MAX_PATH
];
192 GetTempPath(MAX_PATH
, temp_dir
);
193 GetTempFileName(temp_dir
, "qem", 0, filename
);
196 void get_tmp_filename(char *filename
, int size
)
200 /* XXX: race condition possible */
201 tmpdir
= getenv("TMPDIR");
204 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
205 fd
= mkstemp(filename
);
211 static int is_windows_drive_prefix(const char *filename
)
213 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
214 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
218 static int is_windows_drive(const char *filename
)
220 if (is_windows_drive_prefix(filename
) &&
223 if (strstart(filename
, "\\\\.\\", NULL
) ||
224 strstart(filename
, "//./", NULL
))
230 static BlockDriver
*find_protocol(const char *filename
)
238 if (is_windows_drive(filename
) ||
239 is_windows_drive_prefix(filename
))
242 p
= strchr(filename
, ':');
246 if (len
> sizeof(protocol
) - 1)
247 len
= sizeof(protocol
) - 1;
248 memcpy(protocol
, filename
, len
);
249 protocol
[len
] = '\0';
250 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
251 if (drv1
->protocol_name
&&
252 !strcmp(drv1
->protocol_name
, protocol
))
258 /* XXX: force raw format if block or character device ? It would
259 simplify the BSD case */
260 static BlockDriver
*find_image_format(const char *filename
)
262 int ret
, score
, score_max
;
263 BlockDriver
*drv1
, *drv
;
265 BlockDriverState
*bs
;
267 /* detect host devices. By convention, /dev/cdrom[N] is always
268 recognized as a host CDROM */
269 if (strstart(filename
, "/dev/cdrom", NULL
))
270 return &bdrv_host_device
;
272 if (is_windows_drive(filename
))
273 return &bdrv_host_device
;
277 if (stat(filename
, &st
) >= 0 &&
278 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
279 return &bdrv_host_device
;
284 drv
= find_protocol(filename
);
285 /* no need to test disk image formats for vvfat */
286 if (drv
== &bdrv_vvfat
)
289 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
292 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
299 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
300 if (drv1
->bdrv_probe
) {
301 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
302 if (score
> score_max
) {
311 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
313 BlockDriverState
*bs
;
317 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
327 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
329 return bdrv_open2(bs
, filename
, flags
, NULL
);
332 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
336 char tmp_filename
[PATH_MAX
];
337 char backing_filename
[PATH_MAX
];
340 bs
->is_temporary
= 0;
344 if (flags
& BDRV_O_SNAPSHOT
) {
345 BlockDriverState
*bs1
;
349 /* if snapshot, we create a temporary backing file and open it
350 instead of opening 'filename' directly */
352 /* if there is a backing file, use it */
354 ret
= bdrv_open(bs1
, filename
, 0);
359 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
361 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
366 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
368 /* Real path is meaningless for protocols */
370 snprintf(backing_filename
, sizeof(backing_filename
),
373 realpath(filename
, backing_filename
);
375 ret
= bdrv_create(&bdrv_qcow2
, tmp_filename
,
376 total_size
, backing_filename
, 0);
380 filename
= tmp_filename
;
381 bs
->is_temporary
= 1;
384 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
385 if (flags
& BDRV_O_FILE
) {
386 drv
= find_protocol(filename
);
388 drv
= find_image_format(filename
);
392 goto unlink_and_fail
;
395 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
396 /* Note: for compatibility, we open disk image files as RDWR, and
397 RDONLY as fallback */
398 if (!(flags
& BDRV_O_FILE
))
399 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
401 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
402 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
403 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
404 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
408 qemu_free(bs
->opaque
);
412 if (bs
->is_temporary
)
416 if (drv
->bdrv_getlength
) {
417 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
420 if (bs
->is_temporary
) {
424 if (bs
->backing_file
[0] != '\0') {
425 /* if there is a backing file, use it */
426 bs
->backing_hd
= bdrv_new("");
427 path_combine(backing_filename
, sizeof(backing_filename
),
428 filename
, bs
->backing_file
);
429 ret
= bdrv_open(bs
->backing_hd
, backing_filename
, open_flags
);
436 /* call the change callback */
437 bs
->media_changed
= 1;
439 bs
->change_cb(bs
->change_opaque
);
444 void bdrv_close(BlockDriverState
*bs
)
448 bdrv_delete(bs
->backing_hd
);
449 bs
->drv
->bdrv_close(bs
);
450 qemu_free(bs
->opaque
);
452 if (bs
->is_temporary
) {
453 unlink(bs
->filename
);
459 /* call the change callback */
460 bs
->media_changed
= 1;
462 bs
->change_cb(bs
->change_opaque
);
466 void bdrv_delete(BlockDriverState
*bs
)
468 BlockDriverState
**pbs
;
471 while (*pbs
!= bs
&& *pbs
!= NULL
)
480 /* commit COW file into the raw image */
481 int bdrv_commit(BlockDriverState
*bs
)
483 BlockDriver
*drv
= bs
->drv
;
484 int64_t i
, total_sectors
;
486 unsigned char sector
[512];
495 if (!bs
->backing_hd
) {
499 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
500 for (i
= 0; i
< total_sectors
;) {
501 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
502 for(j
= 0; j
< n
; j
++) {
503 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
507 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
517 if (drv
->bdrv_make_empty
)
518 return drv
->bdrv_make_empty(bs
);
523 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
528 if (!bdrv_is_inserted(bs
))
534 len
= bdrv_getlength(bs
);
539 if ((offset
> len
) || (len
- offset
< size
))
545 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
550 /* Deal with byte accesses */
552 offset
= -sector_num
;
554 offset
= sector_num
* 512;
556 return bdrv_check_byte_request(bs
, offset
, nb_sectors
* 512);
559 /* return < 0 if error. See bdrv_write() for the return codes */
560 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
561 uint8_t *buf
, int nb_sectors
)
563 BlockDriver
*drv
= bs
->drv
;
567 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
570 if (drv
->bdrv_pread
) {
572 len
= nb_sectors
* 512;
573 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
579 bs
->rd_bytes
+= (unsigned) len
;
584 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
588 /* Return < 0 if error. Important errors are:
589 -EIO generic I/O error (may happen for all errors)
590 -ENOMEDIUM No media inserted.
591 -EINVAL Invalid sector number or nb_sectors
592 -EACCES Trying to write a read-only device
594 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
595 const uint8_t *buf
, int nb_sectors
)
597 BlockDriver
*drv
= bs
->drv
;
602 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
605 if (drv
->bdrv_pwrite
) {
606 int ret
, len
, count
= 0;
607 len
= nb_sectors
* 512;
609 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
- count
);
611 printf("bdrv_write ret=%d\n", ret
);
616 } while (count
!= len
);
617 bs
->wr_bytes
+= (unsigned) len
;
621 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
624 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
625 uint8_t *buf
, int count1
)
627 uint8_t tmp_buf
[SECTOR_SIZE
];
628 int len
, nb_sectors
, count
;
632 /* first read to align to sector start */
633 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
636 sector_num
= offset
>> SECTOR_BITS
;
638 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
640 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
648 /* read the sectors "in place" */
649 nb_sectors
= count
>> SECTOR_BITS
;
650 if (nb_sectors
> 0) {
651 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
653 sector_num
+= nb_sectors
;
654 len
= nb_sectors
<< SECTOR_BITS
;
659 /* add data from the last sector */
661 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
663 memcpy(buf
, tmp_buf
, count
);
668 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
669 const uint8_t *buf
, int count1
)
671 uint8_t tmp_buf
[SECTOR_SIZE
];
672 int len
, nb_sectors
, count
;
676 /* first write to align to sector start */
677 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
680 sector_num
= offset
>> SECTOR_BITS
;
682 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
684 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
685 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
694 /* write the sectors "in place" */
695 nb_sectors
= count
>> SECTOR_BITS
;
696 if (nb_sectors
> 0) {
697 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
699 sector_num
+= nb_sectors
;
700 len
= nb_sectors
<< SECTOR_BITS
;
705 /* add data from the last sector */
707 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
709 memcpy(tmp_buf
, buf
, count
);
710 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
717 * Read with byte offsets (needed only for file protocols)
719 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
720 void *buf1
, int count1
)
722 BlockDriver
*drv
= bs
->drv
;
726 if (bdrv_check_byte_request(bs
, offset
, count1
))
729 if (!drv
->bdrv_pread
)
730 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
731 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
735 * Write with byte offsets (needed only for file protocols)
737 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
738 const void *buf1
, int count1
)
740 BlockDriver
*drv
= bs
->drv
;
744 if (bdrv_check_byte_request(bs
, offset
, count1
))
747 if (!drv
->bdrv_pwrite
)
748 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
749 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
753 * Truncate file to 'offset' bytes (needed only for file protocols)
755 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
757 BlockDriver
*drv
= bs
->drv
;
760 if (!drv
->bdrv_truncate
)
762 return drv
->bdrv_truncate(bs
, offset
);
766 * Length of a file in bytes. Return < 0 if error or unknown.
768 int64_t bdrv_getlength(BlockDriverState
*bs
)
770 BlockDriver
*drv
= bs
->drv
;
773 if (!drv
->bdrv_getlength
) {
775 return bs
->total_sectors
* SECTOR_SIZE
;
777 return drv
->bdrv_getlength(bs
);
780 /* return 0 as number of sectors if no device present or error */
781 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
784 length
= bdrv_getlength(bs
);
788 length
= length
>> SECTOR_BITS
;
789 *nb_sectors_ptr
= length
;
793 uint8_t boot_ind
; /* 0x80 - active */
794 uint8_t head
; /* starting head */
795 uint8_t sector
; /* starting sector */
796 uint8_t cyl
; /* starting cylinder */
797 uint8_t sys_ind
; /* What partition type */
798 uint8_t end_head
; /* end head */
799 uint8_t end_sector
; /* end sector */
800 uint8_t end_cyl
; /* end cylinder */
801 uint32_t start_sect
; /* starting sector counting from 0 */
802 uint32_t nr_sects
; /* nr of sectors in partition */
803 } __attribute__((packed
));
805 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
806 static int guess_disk_lchs(BlockDriverState
*bs
,
807 int *pcylinders
, int *pheads
, int *psectors
)
810 int ret
, i
, heads
, sectors
, cylinders
;
815 bdrv_get_geometry(bs
, &nb_sectors
);
817 ret
= bdrv_read(bs
, 0, buf
, 1);
820 /* test msdos magic */
821 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
823 for(i
= 0; i
< 4; i
++) {
824 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
825 nr_sects
= le32_to_cpu(p
->nr_sects
);
826 if (nr_sects
&& p
->end_head
) {
827 /* We make the assumption that the partition terminates on
828 a cylinder boundary */
829 heads
= p
->end_head
+ 1;
830 sectors
= p
->end_sector
& 63;
833 cylinders
= nb_sectors
/ (heads
* sectors
);
834 if (cylinders
< 1 || cylinders
> 16383)
838 *pcylinders
= cylinders
;
840 printf("guessed geometry: LCHS=%d %d %d\n",
841 cylinders
, heads
, sectors
);
849 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
851 int translation
, lba_detected
= 0;
852 int cylinders
, heads
, secs
;
855 /* if a geometry hint is available, use it */
856 bdrv_get_geometry(bs
, &nb_sectors
);
857 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
858 translation
= bdrv_get_translation_hint(bs
);
859 if (cylinders
!= 0) {
864 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
866 /* if heads > 16, it means that a BIOS LBA
867 translation was active, so the default
868 hardware geometry is OK */
870 goto default_geometry
;
875 /* disable any translation to be in sync with
876 the logical geometry */
877 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
878 bdrv_set_translation_hint(bs
,
879 BIOS_ATA_TRANSLATION_NONE
);
884 /* if no geometry, use a standard physical disk geometry */
885 cylinders
= nb_sectors
/ (16 * 63);
887 if (cylinders
> 16383)
889 else if (cylinders
< 2)
894 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
895 if ((*pcyls
* *pheads
) <= 131072) {
896 bdrv_set_translation_hint(bs
,
897 BIOS_ATA_TRANSLATION_LARGE
);
899 bdrv_set_translation_hint(bs
,
900 BIOS_ATA_TRANSLATION_LBA
);
904 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
908 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
909 int cyls
, int heads
, int secs
)
916 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
919 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
920 type
== BDRV_TYPE_FLOPPY
));
923 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
925 bs
->translation
= translation
;
928 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
929 int *pcyls
, int *pheads
, int *psecs
)
936 int bdrv_get_type_hint(BlockDriverState
*bs
)
941 int bdrv_get_translation_hint(BlockDriverState
*bs
)
943 return bs
->translation
;
946 int bdrv_is_removable(BlockDriverState
*bs
)
948 return bs
->removable
;
951 int bdrv_is_read_only(BlockDriverState
*bs
)
953 return bs
->read_only
;
956 int bdrv_is_sg(BlockDriverState
*bs
)
961 /* XXX: no longer used */
962 void bdrv_set_change_cb(BlockDriverState
*bs
,
963 void (*change_cb
)(void *opaque
), void *opaque
)
965 bs
->change_cb
= change_cb
;
966 bs
->change_opaque
= opaque
;
969 int bdrv_is_encrypted(BlockDriverState
*bs
)
971 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
973 return bs
->encrypted
;
976 int bdrv_key_required(BlockDriverState
*bs
)
978 BlockDriverState
*backing_hd
= bs
->backing_hd
;
980 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
982 return (bs
->encrypted
&& !bs
->valid_key
);
985 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
988 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
989 ret
= bdrv_set_key(bs
->backing_hd
, key
);
995 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
997 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
998 bs
->valid_key
= (ret
== 0);
1002 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
1007 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
1011 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
1016 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
1017 it(opaque
, drv
->format_name
);
1021 BlockDriverState
*bdrv_find(const char *name
)
1023 BlockDriverState
*bs
;
1025 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1026 if (!strcmp(name
, bs
->device_name
))
1032 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1034 BlockDriverState
*bs
;
1036 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1041 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1043 return bs
->device_name
;
1046 void bdrv_flush(BlockDriverState
*bs
)
1048 if (bs
->drv
->bdrv_flush
)
1049 bs
->drv
->bdrv_flush(bs
);
1051 bdrv_flush(bs
->backing_hd
);
1054 void bdrv_flush_all(void)
1056 BlockDriverState
*bs
;
1058 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1059 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1060 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1065 * Returns true iff the specified sector is present in the disk image. Drivers
1066 * not implementing the functionality are assumed to not support backing files,
1067 * hence all their sectors are reported as allocated.
1069 * 'pnum' is set to the number of sectors (including and immediately following
1070 * the specified sector) that are known to be in the same
1071 * allocated/unallocated state.
1073 * 'nb_sectors' is the max value 'pnum' should be set to.
1075 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1079 if (!bs
->drv
->bdrv_is_allocated
) {
1080 if (sector_num
>= bs
->total_sectors
) {
1084 n
= bs
->total_sectors
- sector_num
;
1085 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1088 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1091 void bdrv_info(void)
1093 BlockDriverState
*bs
;
1095 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1096 term_printf("%s:", bs
->device_name
);
1097 term_printf(" type=");
1102 case BDRV_TYPE_CDROM
:
1103 term_printf("cdrom");
1105 case BDRV_TYPE_FLOPPY
:
1106 term_printf("floppy");
1109 term_printf(" removable=%d", bs
->removable
);
1110 if (bs
->removable
) {
1111 term_printf(" locked=%d", bs
->locked
);
1114 term_printf(" file=");
1115 term_print_filename(bs
->filename
);
1116 if (bs
->backing_file
[0] != '\0') {
1117 term_printf(" backing_file=");
1118 term_print_filename(bs
->backing_file
);
1120 term_printf(" ro=%d", bs
->read_only
);
1121 term_printf(" drv=%s", bs
->drv
->format_name
);
1122 term_printf(" encrypted=%d", bdrv_is_encrypted(bs
));
1124 term_printf(" [not inserted]");
1130 /* The "info blockstats" command. */
1131 void bdrv_info_stats (void)
1133 BlockDriverState
*bs
;
1135 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1137 " rd_bytes=%" PRIu64
1138 " wr_bytes=%" PRIu64
1139 " rd_operations=%" PRIu64
1140 " wr_operations=%" PRIu64
1143 bs
->rd_bytes
, bs
->wr_bytes
,
1144 bs
->rd_ops
, bs
->wr_ops
);
1148 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1150 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1151 return bs
->backing_file
;
1152 else if (bs
->encrypted
)
1153 return bs
->filename
;
1158 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1159 char *filename
, int filename_size
)
1161 if (!bs
->backing_hd
) {
1162 pstrcpy(filename
, filename_size
, "");
1164 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1168 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1169 const uint8_t *buf
, int nb_sectors
)
1171 BlockDriver
*drv
= bs
->drv
;
1174 if (!drv
->bdrv_write_compressed
)
1176 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1178 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1181 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1183 BlockDriver
*drv
= bs
->drv
;
1186 if (!drv
->bdrv_get_info
)
1188 memset(bdi
, 0, sizeof(*bdi
));
1189 return drv
->bdrv_get_info(bs
, bdi
);
1192 int bdrv_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
, int64_t pos
, int size
)
1194 BlockDriver
*drv
= bs
->drv
;
1197 if (!drv
->bdrv_put_buffer
)
1199 return drv
->bdrv_put_buffer(bs
, buf
, pos
, size
);
1202 int bdrv_get_buffer(BlockDriverState
*bs
, uint8_t *buf
, int64_t pos
, int size
)
1204 BlockDriver
*drv
= bs
->drv
;
1207 if (!drv
->bdrv_get_buffer
)
1209 return drv
->bdrv_get_buffer(bs
, buf
, pos
, size
);
1212 /**************************************************************/
1213 /* handling of snapshots */
1215 int bdrv_snapshot_create(BlockDriverState
*bs
,
1216 QEMUSnapshotInfo
*sn_info
)
1218 BlockDriver
*drv
= bs
->drv
;
1221 if (!drv
->bdrv_snapshot_create
)
1223 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1226 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1227 const char *snapshot_id
)
1229 BlockDriver
*drv
= bs
->drv
;
1232 if (!drv
->bdrv_snapshot_goto
)
1234 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1237 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1239 BlockDriver
*drv
= bs
->drv
;
1242 if (!drv
->bdrv_snapshot_delete
)
1244 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1247 int bdrv_snapshot_list(BlockDriverState
*bs
,
1248 QEMUSnapshotInfo
**psn_info
)
1250 BlockDriver
*drv
= bs
->drv
;
1253 if (!drv
->bdrv_snapshot_list
)
1255 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1258 #define NB_SUFFIXES 4
1260 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1262 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1267 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1270 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1271 if (size
< (10 * base
)) {
1272 snprintf(buf
, buf_size
, "%0.1f%c",
1273 (double)size
/ base
,
1276 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1277 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1278 ((size
+ (base
>> 1)) / base
),
1288 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1290 char buf1
[128], date_buf
[128], clock_buf
[128];
1300 snprintf(buf
, buf_size
,
1301 "%-10s%-20s%7s%20s%15s",
1302 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1306 ptm
= localtime(&ti
);
1307 strftime(date_buf
, sizeof(date_buf
),
1308 "%Y-%m-%d %H:%M:%S", ptm
);
1310 localtime_r(&ti
, &tm
);
1311 strftime(date_buf
, sizeof(date_buf
),
1312 "%Y-%m-%d %H:%M:%S", &tm
);
1314 secs
= sn
->vm_clock_nsec
/ 1000000000;
1315 snprintf(clock_buf
, sizeof(clock_buf
),
1316 "%02d:%02d:%02d.%03d",
1318 (int)((secs
/ 60) % 60),
1320 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1321 snprintf(buf
, buf_size
,
1322 "%-10s%-20s%7s%20s%15s",
1323 sn
->id_str
, sn
->name
,
1324 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1332 /**************************************************************/
1335 typedef struct VectorTranslationState
{
1339 BlockDriverAIOCB
*aiocb
;
1340 BlockDriverAIOCB
*this_aiocb
;
1341 } VectorTranslationState
;
1343 static void bdrv_aio_cancel_vector(BlockDriverAIOCB
*acb
)
1345 VectorTranslationState
*s
= acb
->opaque
;
1347 bdrv_aio_cancel(s
->aiocb
);
1350 static void bdrv_aio_rw_vector_cb(void *opaque
, int ret
)
1352 VectorTranslationState
*s
= opaque
;
1355 qemu_iovec_from_buffer(s
->iov
, s
->bounce
, s
->iov
->size
);
1357 qemu_vfree(s
->bounce
);
1358 s
->this_aiocb
->cb(s
->this_aiocb
->opaque
, ret
);
1359 qemu_aio_release(s
->this_aiocb
);
1362 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1366 BlockDriverCompletionFunc
*cb
,
1371 VectorTranslationState
*s
= qemu_mallocz(sizeof(*s
));
1372 BlockDriverAIOCB
*aiocb
= qemu_aio_get_pool(&vectored_aio_pool
, bs
,
1375 s
->this_aiocb
= aiocb
;
1377 s
->bounce
= qemu_memalign(512, nb_sectors
* 512);
1378 s
->is_write
= is_write
;
1380 qemu_iovec_to_buffer(s
->iov
, s
->bounce
);
1381 s
->aiocb
= bdrv_aio_write(bs
, sector_num
, s
->bounce
, nb_sectors
,
1382 bdrv_aio_rw_vector_cb
, s
);
1384 s
->aiocb
= bdrv_aio_read(bs
, sector_num
, s
->bounce
, nb_sectors
,
1385 bdrv_aio_rw_vector_cb
, s
);
1390 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1391 QEMUIOVector
*iov
, int nb_sectors
,
1392 BlockDriverCompletionFunc
*cb
, void *opaque
)
1394 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1397 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1401 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1402 QEMUIOVector
*iov
, int nb_sectors
,
1403 BlockDriverCompletionFunc
*cb
, void *opaque
)
1405 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1408 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1412 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1413 uint8_t *buf
, int nb_sectors
,
1414 BlockDriverCompletionFunc
*cb
, void *opaque
)
1416 BlockDriver
*drv
= bs
->drv
;
1417 BlockDriverAIOCB
*ret
;
1421 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1424 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1427 /* Update stats even though technically transfer has not happened. */
1428 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1435 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1436 const uint8_t *buf
, int nb_sectors
,
1437 BlockDriverCompletionFunc
*cb
, void *opaque
)
1439 BlockDriver
*drv
= bs
->drv
;
1440 BlockDriverAIOCB
*ret
;
1446 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1449 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1452 /* Update stats even though technically transfer has not happened. */
1453 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1460 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1462 acb
->pool
->cancel(acb
);
1466 /**************************************************************/
1467 /* async block device emulation */
1469 static void bdrv_aio_bh_cb(void *opaque
)
1471 BlockDriverAIOCBSync
*acb
= opaque
;
1472 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1473 qemu_aio_release(acb
);
1476 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1477 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1478 BlockDriverCompletionFunc
*cb
, void *opaque
)
1480 BlockDriverAIOCBSync
*acb
;
1483 acb
= qemu_aio_get(bs
, cb
, opaque
);
1485 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1486 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1488 qemu_bh_schedule(acb
->bh
);
1489 return &acb
->common
;
1492 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1493 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1494 BlockDriverCompletionFunc
*cb
, void *opaque
)
1496 BlockDriverAIOCBSync
*acb
;
1499 acb
= qemu_aio_get(bs
, cb
, opaque
);
1501 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1502 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1504 qemu_bh_schedule(acb
->bh
);
1505 return &acb
->common
;
1508 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1510 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1511 qemu_bh_cancel(acb
->bh
);
1512 qemu_aio_release(acb
);
1515 /**************************************************************/
1516 /* sync block device emulation */
1518 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1520 *(int *)opaque
= ret
;
1523 #define NOT_DONE 0x7fffffff
1525 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1526 uint8_t *buf
, int nb_sectors
)
1529 BlockDriverAIOCB
*acb
;
1531 async_ret
= NOT_DONE
;
1532 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1533 bdrv_rw_em_cb
, &async_ret
);
1537 while (async_ret
== NOT_DONE
) {
1544 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1545 const uint8_t *buf
, int nb_sectors
)
1548 BlockDriverAIOCB
*acb
;
1550 async_ret
= NOT_DONE
;
1551 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1552 bdrv_rw_em_cb
, &async_ret
);
1555 while (async_ret
== NOT_DONE
) {
1561 void bdrv_init(void)
1563 aio_pool_init(&vectored_aio_pool
, sizeof(BlockDriverAIOCB
),
1564 bdrv_aio_cancel_vector
);
1566 bdrv_register(&bdrv_raw
);
1567 bdrv_register(&bdrv_host_device
);
1569 bdrv_register(&bdrv_cow
);
1571 bdrv_register(&bdrv_qcow
);
1572 bdrv_register(&bdrv_vmdk
);
1573 bdrv_register(&bdrv_cloop
);
1574 bdrv_register(&bdrv_dmg
);
1575 bdrv_register(&bdrv_bochs
);
1576 bdrv_register(&bdrv_vpc
);
1577 bdrv_register(&bdrv_vvfat
);
1578 bdrv_register(&bdrv_qcow2
);
1579 bdrv_register(&bdrv_parallels
);
1580 bdrv_register(&bdrv_nbd
);
1583 void aio_pool_init(AIOPool
*pool
, int aiocb_size
,
1584 void (*cancel
)(BlockDriverAIOCB
*acb
))
1586 pool
->aiocb_size
= aiocb_size
;
1587 pool
->cancel
= cancel
;
1588 pool
->free_aiocb
= NULL
;
1591 void *qemu_aio_get_pool(AIOPool
*pool
, BlockDriverState
*bs
,
1592 BlockDriverCompletionFunc
*cb
, void *opaque
)
1594 BlockDriverAIOCB
*acb
;
1596 if (pool
->free_aiocb
) {
1597 acb
= pool
->free_aiocb
;
1598 pool
->free_aiocb
= acb
->next
;
1600 acb
= qemu_mallocz(pool
->aiocb_size
);
1605 acb
->opaque
= opaque
;
1609 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1612 return qemu_aio_get_pool(&bs
->drv
->aio_pool
, bs
, cb
, opaque
);
1615 void qemu_aio_release(void *p
)
1617 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1618 AIOPool
*pool
= acb
->pool
;
1619 acb
->next
= pool
->free_aiocb
;
1620 pool
->free_aiocb
= acb
;
1623 /**************************************************************/
1624 /* removable device support */
1627 * Return TRUE if the media is present
1629 int bdrv_is_inserted(BlockDriverState
*bs
)
1631 BlockDriver
*drv
= bs
->drv
;
1635 if (!drv
->bdrv_is_inserted
)
1637 ret
= drv
->bdrv_is_inserted(bs
);
1642 * Return TRUE if the media changed since the last call to this
1643 * function. It is currently only used for floppy disks
1645 int bdrv_media_changed(BlockDriverState
*bs
)
1647 BlockDriver
*drv
= bs
->drv
;
1650 if (!drv
|| !drv
->bdrv_media_changed
)
1653 ret
= drv
->bdrv_media_changed(bs
);
1654 if (ret
== -ENOTSUP
)
1655 ret
= bs
->media_changed
;
1656 bs
->media_changed
= 0;
1661 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1663 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1665 BlockDriver
*drv
= bs
->drv
;
1668 if (!drv
|| !drv
->bdrv_eject
) {
1671 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1673 if (ret
== -ENOTSUP
) {
1679 int bdrv_is_locked(BlockDriverState
*bs
)
1685 * Lock or unlock the media (if it is locked, the user won't be able
1686 * to eject it manually).
1688 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1690 BlockDriver
*drv
= bs
->drv
;
1692 bs
->locked
= locked
;
1693 if (drv
&& drv
->bdrv_set_locked
) {
1694 drv
->bdrv_set_locked(bs
, locked
);
1698 /* needed for generic scsi interface */
1700 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1702 BlockDriver
*drv
= bs
->drv
;
1704 if (drv
&& drv
->bdrv_ioctl
)
1705 return drv
->bdrv_ioctl(bs
, req
, buf
);