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 "qemu-common.h"
26 #include "block_int.h"
30 #include <sys/types.h>
32 #include <sys/ioctl.h>
33 #include <sys/queue.h>
38 #define SECTOR_SIZE (1 << SECTOR_BITS)
40 typedef struct BlockDriverAIOCBSync
{
41 BlockDriverAIOCB common
;
44 } BlockDriverAIOCBSync
;
46 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
47 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
48 BlockDriverCompletionFunc
*cb
, void *opaque
);
49 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
50 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
51 BlockDriverCompletionFunc
*cb
, void *opaque
);
52 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
53 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
54 uint8_t *buf
, int nb_sectors
);
55 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
56 const uint8_t *buf
, int nb_sectors
);
58 BlockDriverState
*bdrv_first
;
60 static BlockDriver
*first_drv
;
62 int path_is_absolute(const char *path
)
66 /* specific case for names like: "\\.\d:" */
67 if (*path
== '/' || *path
== '\\')
70 p
= strchr(path
, ':');
76 return (*p
== '/' || *p
== '\\');
82 /* if filename is absolute, just copy it to dest. Otherwise, build a
83 path to it by considering it is relative to base_path. URL are
85 void path_combine(char *dest
, int dest_size
,
86 const char *base_path
,
94 if (path_is_absolute(filename
)) {
95 pstrcpy(dest
, dest_size
, filename
);
97 p
= strchr(base_path
, ':');
102 p1
= strrchr(base_path
, '/');
106 p2
= strrchr(base_path
, '\\');
118 if (len
> dest_size
- 1)
120 memcpy(dest
, base_path
, len
);
122 pstrcat(dest
, dest_size
, filename
);
127 static void bdrv_register(BlockDriver
*bdrv
)
129 if (!bdrv
->bdrv_aio_read
) {
130 /* add AIO emulation layer */
131 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
132 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
133 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
134 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
135 } else if (!bdrv
->bdrv_read
&& !bdrv
->bdrv_pread
) {
136 /* add synchronous IO emulation layer */
137 bdrv
->bdrv_read
= bdrv_read_em
;
138 bdrv
->bdrv_write
= bdrv_write_em
;
140 bdrv
->next
= first_drv
;
144 /* create a new block device (by default it is empty) */
145 BlockDriverState
*bdrv_new(const char *device_name
)
147 BlockDriverState
**pbs
, *bs
;
149 bs
= qemu_mallocz(sizeof(BlockDriverState
));
152 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
153 if (device_name
[0] != '\0') {
154 /* insert at the end */
163 BlockDriver
*bdrv_find_format(const char *format_name
)
166 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
167 if (!strcmp(drv1
->format_name
, format_name
))
173 int bdrv_create(BlockDriver
*drv
,
174 const char *filename
, int64_t size_in_sectors
,
175 const char *backing_file
, int flags
)
177 if (!drv
->bdrv_create
)
179 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
183 void get_tmp_filename(char *filename
, int size
)
185 char temp_dir
[MAX_PATH
];
187 GetTempPath(MAX_PATH
, temp_dir
);
188 GetTempFileName(temp_dir
, "qem", 0, filename
);
191 void get_tmp_filename(char *filename
, int size
)
195 /* XXX: race condition possible */
196 tmpdir
= getenv("TMPDIR");
199 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
200 fd
= mkstemp(filename
);
206 static int is_windows_drive_prefix(const char *filename
)
208 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
209 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
213 static int is_windows_drive(const char *filename
)
215 if (is_windows_drive_prefix(filename
) &&
218 if (strstart(filename
, "\\\\.\\", NULL
) ||
219 strstart(filename
, "//./", NULL
))
225 static BlockDriver
*find_protocol(const char *filename
)
233 if (is_windows_drive(filename
) ||
234 is_windows_drive_prefix(filename
))
237 p
= strchr(filename
, ':');
241 if (len
> sizeof(protocol
) - 1)
242 len
= sizeof(protocol
) - 1;
243 memcpy(protocol
, filename
, len
);
244 protocol
[len
] = '\0';
245 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
246 if (drv1
->protocol_name
&&
247 !strcmp(drv1
->protocol_name
, protocol
))
253 /* XXX: force raw format if block or character device ? It would
254 simplify the BSD case */
255 static BlockDriver
*find_image_format(const char *filename
)
257 int ret
, score
, score_max
;
258 BlockDriver
*drv1
, *drv
;
260 BlockDriverState
*bs
;
262 /* detect host devices. By convention, /dev/cdrom[N] is always
263 recognized as a host CDROM */
264 if (strstart(filename
, "/dev/cdrom", NULL
))
265 return &bdrv_host_device
;
267 if (is_windows_drive(filename
))
268 return &bdrv_host_device
;
272 if (stat(filename
, &st
) >= 0 &&
273 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
274 return &bdrv_host_device
;
279 drv
= find_protocol(filename
);
280 /* no need to test disk image formats for vvfat */
281 if (drv
== &bdrv_vvfat
)
284 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
287 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
294 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
295 if (drv1
->bdrv_probe
) {
296 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
297 if (score
> score_max
) {
306 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
308 BlockDriverState
*bs
;
314 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
323 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
325 return bdrv_open2(bs
, filename
, flags
, NULL
);
328 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
332 char tmp_filename
[PATH_MAX
];
333 char backing_filename
[PATH_MAX
];
336 bs
->is_temporary
= 0;
339 if (flags
& BDRV_O_SNAPSHOT
) {
340 BlockDriverState
*bs1
;
344 /* if snapshot, we create a temporary backing file and open it
345 instead of opening 'filename' directly */
347 /* if there is a backing file, use it */
352 if (bdrv_open(bs1
, filename
, 0) < 0) {
356 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
358 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
363 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
365 /* Real path is meaningless for protocols */
367 snprintf(backing_filename
, sizeof(backing_filename
),
370 realpath(filename
, backing_filename
);
372 if (bdrv_create(&bdrv_qcow2
, tmp_filename
,
373 total_size
, backing_filename
, 0) < 0) {
376 filename
= tmp_filename
;
377 bs
->is_temporary
= 1;
380 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
381 if (flags
& BDRV_O_FILE
) {
382 drv
= find_protocol(filename
);
387 drv
= find_image_format(filename
);
393 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
394 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
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
);
413 if (drv
->bdrv_getlength
) {
414 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
417 if (bs
->is_temporary
) {
421 if (bs
->backing_file
[0] != '\0') {
422 /* if there is a backing file, use it */
423 bs
->backing_hd
= bdrv_new("");
424 if (!bs
->backing_hd
) {
429 path_combine(backing_filename
, sizeof(backing_filename
),
430 filename
, bs
->backing_file
);
431 if (bdrv_open(bs
->backing_hd
, backing_filename
, open_flags
) < 0)
435 /* call the change callback */
436 bs
->media_changed
= 1;
438 bs
->change_cb(bs
->change_opaque
);
443 void bdrv_close(BlockDriverState
*bs
)
447 bdrv_delete(bs
->backing_hd
);
448 bs
->drv
->bdrv_close(bs
);
449 qemu_free(bs
->opaque
);
451 if (bs
->is_temporary
) {
452 unlink(bs
->filename
);
458 /* call the change callback */
459 bs
->media_changed
= 1;
461 bs
->change_cb(bs
->change_opaque
);
465 void bdrv_delete(BlockDriverState
*bs
)
467 BlockDriverState
**pbs
;
470 while (*pbs
!= bs
&& *pbs
!= NULL
)
479 /* commit COW file into the raw image */
480 int bdrv_commit(BlockDriverState
*bs
)
482 BlockDriver
*drv
= bs
->drv
;
483 int64_t i
, total_sectors
;
485 unsigned char sector
[512];
494 if (!bs
->backing_hd
) {
498 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
499 for (i
= 0; i
< total_sectors
;) {
500 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
501 for(j
= 0; j
< n
; j
++) {
502 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
506 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
516 if (drv
->bdrv_make_empty
)
517 return drv
->bdrv_make_empty(bs
);
522 /* return < 0 if error. See bdrv_write() for the return codes */
523 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
524 uint8_t *buf
, int nb_sectors
)
526 BlockDriver
*drv
= bs
->drv
;
531 if (drv
->bdrv_pread
) {
533 len
= nb_sectors
* 512;
534 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
540 bs
->rd_bytes
+= (unsigned) len
;
545 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
549 /* Return < 0 if error. Important errors are:
550 -EIO generic I/O error (may happen for all errors)
551 -ENOMEDIUM No media inserted.
552 -EINVAL Invalid sector number or nb_sectors
553 -EACCES Trying to write a read-only device
555 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
556 const uint8_t *buf
, int nb_sectors
)
558 BlockDriver
*drv
= bs
->drv
;
563 if (drv
->bdrv_pwrite
) {
565 len
= nb_sectors
* 512;
566 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
);
572 bs
->wr_bytes
+= (unsigned) len
;
577 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
581 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
582 uint8_t *buf
, int count1
)
584 uint8_t tmp_buf
[SECTOR_SIZE
];
585 int len
, nb_sectors
, count
;
589 /* first read to align to sector start */
590 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
593 sector_num
= offset
>> SECTOR_BITS
;
595 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
597 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
605 /* read the sectors "in place" */
606 nb_sectors
= count
>> SECTOR_BITS
;
607 if (nb_sectors
> 0) {
608 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
610 sector_num
+= nb_sectors
;
611 len
= nb_sectors
<< SECTOR_BITS
;
616 /* add data from the last sector */
618 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
620 memcpy(buf
, tmp_buf
, count
);
625 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
626 const uint8_t *buf
, int count1
)
628 uint8_t tmp_buf
[SECTOR_SIZE
];
629 int len
, nb_sectors
, count
;
633 /* first write to align to sector start */
634 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
637 sector_num
= offset
>> SECTOR_BITS
;
639 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
641 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
642 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
651 /* write the sectors "in place" */
652 nb_sectors
= count
>> SECTOR_BITS
;
653 if (nb_sectors
> 0) {
654 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
656 sector_num
+= nb_sectors
;
657 len
= nb_sectors
<< SECTOR_BITS
;
662 /* add data from the last sector */
664 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
666 memcpy(tmp_buf
, buf
, count
);
667 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
674 * Read with byte offsets (needed only for file protocols)
676 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
677 void *buf1
, int count1
)
679 BlockDriver
*drv
= bs
->drv
;
683 if (!drv
->bdrv_pread
)
684 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
685 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
689 * Write with byte offsets (needed only for file protocols)
691 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
692 const void *buf1
, int count1
)
694 BlockDriver
*drv
= bs
->drv
;
698 if (!drv
->bdrv_pwrite
)
699 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
700 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
704 * Truncate file to 'offset' bytes (needed only for file protocols)
706 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
708 BlockDriver
*drv
= bs
->drv
;
711 if (!drv
->bdrv_truncate
)
713 return drv
->bdrv_truncate(bs
, offset
);
717 * Length of a file in bytes. Return < 0 if error or unknown.
719 int64_t bdrv_getlength(BlockDriverState
*bs
)
721 BlockDriver
*drv
= bs
->drv
;
724 if (!drv
->bdrv_getlength
) {
726 return bs
->total_sectors
* SECTOR_SIZE
;
728 return drv
->bdrv_getlength(bs
);
731 /* return 0 as number of sectors if no device present or error */
732 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
735 length
= bdrv_getlength(bs
);
739 length
= length
>> SECTOR_BITS
;
740 *nb_sectors_ptr
= length
;
744 uint8_t boot_ind
; /* 0x80 - active */
745 uint8_t head
; /* starting head */
746 uint8_t sector
; /* starting sector */
747 uint8_t cyl
; /* starting cylinder */
748 uint8_t sys_ind
; /* What partition type */
749 uint8_t end_head
; /* end head */
750 uint8_t end_sector
; /* end sector */
751 uint8_t end_cyl
; /* end cylinder */
752 uint32_t start_sect
; /* starting sector counting from 0 */
753 uint32_t nr_sects
; /* nr of sectors in partition */
754 } __attribute__((packed
));
756 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
757 static int guess_disk_lchs(BlockDriverState
*bs
,
758 int *pcylinders
, int *pheads
, int *psectors
)
761 int ret
, i
, heads
, sectors
, cylinders
;
766 buf
= qemu_memalign(512, 512);
770 bdrv_get_geometry(bs
, &nb_sectors
);
772 ret
= bdrv_read(bs
, 0, buf
, 1);
775 /* test msdos magic */
776 if (buf
[510] != 0x55 || buf
[511] != 0xaa) {
780 for(i
= 0; i
< 4; i
++) {
781 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
782 nr_sects
= le32_to_cpu(p
->nr_sects
);
783 if (nr_sects
&& p
->end_head
) {
784 /* We make the assumption that the partition terminates on
785 a cylinder boundary */
786 heads
= p
->end_head
+ 1;
787 sectors
= p
->end_sector
& 63;
790 cylinders
= nb_sectors
/ (heads
* sectors
);
791 if (cylinders
< 1 || cylinders
> 16383)
795 *pcylinders
= cylinders
;
797 printf("guessed geometry: LCHS=%d %d %d\n",
798 cylinders
, heads
, sectors
);
808 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
810 int translation
, lba_detected
= 0;
811 int cylinders
, heads
, secs
;
814 /* if a geometry hint is available, use it */
815 bdrv_get_geometry(bs
, &nb_sectors
);
816 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
817 translation
= bdrv_get_translation_hint(bs
);
818 if (cylinders
!= 0) {
823 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
825 /* if heads > 16, it means that a BIOS LBA
826 translation was active, so the default
827 hardware geometry is OK */
829 goto default_geometry
;
834 /* disable any translation to be in sync with
835 the logical geometry */
836 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
837 bdrv_set_translation_hint(bs
,
838 BIOS_ATA_TRANSLATION_NONE
);
843 /* if no geometry, use a standard physical disk geometry */
844 cylinders
= nb_sectors
/ (16 * 63);
846 if (cylinders
> 16383)
848 else if (cylinders
< 2)
853 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
854 if ((*pcyls
* *pheads
) <= 131072) {
855 bdrv_set_translation_hint(bs
,
856 BIOS_ATA_TRANSLATION_LARGE
);
858 bdrv_set_translation_hint(bs
,
859 BIOS_ATA_TRANSLATION_LBA
);
863 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
867 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
868 int cyls
, int heads
, int secs
)
875 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
878 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
879 type
== BDRV_TYPE_FLOPPY
));
882 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
884 bs
->translation
= translation
;
887 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
888 int *pcyls
, int *pheads
, int *psecs
)
895 int bdrv_get_type_hint(BlockDriverState
*bs
)
900 int bdrv_get_translation_hint(BlockDriverState
*bs
)
902 return bs
->translation
;
905 int bdrv_is_removable(BlockDriverState
*bs
)
907 return bs
->removable
;
910 int bdrv_is_read_only(BlockDriverState
*bs
)
912 return bs
->read_only
;
915 int bdrv_is_sg(BlockDriverState
*bs
)
920 /* XXX: no longer used */
921 void bdrv_set_change_cb(BlockDriverState
*bs
,
922 void (*change_cb
)(void *opaque
), void *opaque
)
924 bs
->change_cb
= change_cb
;
925 bs
->change_opaque
= opaque
;
928 int bdrv_is_encrypted(BlockDriverState
*bs
)
930 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
932 return bs
->encrypted
;
935 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
938 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
939 ret
= bdrv_set_key(bs
->backing_hd
, key
);
945 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
947 return bs
->drv
->bdrv_set_key(bs
, key
);
950 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
955 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
959 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
964 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
965 it(opaque
, drv
->format_name
);
969 BlockDriverState
*bdrv_find(const char *name
)
971 BlockDriverState
*bs
;
973 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
974 if (!strcmp(name
, bs
->device_name
))
980 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
982 BlockDriverState
*bs
;
984 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
985 it(opaque
, bs
->device_name
);
989 const char *bdrv_get_device_name(BlockDriverState
*bs
)
991 return bs
->device_name
;
994 void bdrv_flush(BlockDriverState
*bs
)
996 if (bs
->drv
->bdrv_flush
)
997 bs
->drv
->bdrv_flush(bs
);
999 bdrv_flush(bs
->backing_hd
);
1002 void bdrv_iterate_writeable(void (*it
)(BlockDriverState
*bs
))
1004 BlockDriverState
*bs
;
1006 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1007 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1008 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1012 void bdrv_flush_all(void)
1014 BlockDriverState
*bs
;
1016 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1017 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1018 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1023 * Returns true iff the specified sector is present in the disk image. Drivers
1024 * not implementing the functionality are assumed to not support backing files,
1025 * hence all their sectors are reported as allocated.
1027 * 'pnum' is set to the number of sectors (including and immediately following
1028 * the specified sector) that are known to be in the same
1029 * allocated/unallocated state.
1031 * 'nb_sectors' is the max value 'pnum' should be set to.
1033 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1037 if (!bs
->drv
->bdrv_is_allocated
) {
1038 if (sector_num
>= bs
->total_sectors
) {
1042 n
= bs
->total_sectors
- sector_num
;
1043 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1046 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1049 void bdrv_info(void)
1051 BlockDriverState
*bs
;
1053 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1054 term_printf("%s:", bs
->device_name
);
1055 term_printf(" type=");
1060 case BDRV_TYPE_CDROM
:
1061 term_printf("cdrom");
1063 case BDRV_TYPE_FLOPPY
:
1064 term_printf("floppy");
1067 term_printf(" removable=%d", bs
->removable
);
1068 if (bs
->removable
) {
1069 term_printf(" locked=%d", bs
->locked
);
1072 term_printf(" file=");
1073 term_print_filename(bs
->filename
);
1074 if (bs
->backing_file
[0] != '\0') {
1075 term_printf(" backing_file=");
1076 term_print_filename(bs
->backing_file
);
1078 term_printf(" ro=%d", bs
->read_only
);
1079 term_printf(" drv=%s", bs
->drv
->format_name
);
1081 term_printf(" encrypted");
1083 term_printf(" [not inserted]");
1089 /* The "info blockstats" command. */
1090 void bdrv_info_stats (void)
1092 BlockDriverState
*bs
;
1094 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1096 " rd_bytes=%" PRIu64
1097 " wr_bytes=%" PRIu64
1098 " rd_operations=%" PRIu64
1099 " wr_operations=%" PRIu64
1102 bs
->rd_bytes
, bs
->wr_bytes
,
1103 bs
->rd_ops
, bs
->wr_ops
);
1107 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1108 char *filename
, int filename_size
)
1110 if (!bs
->backing_hd
) {
1111 pstrcpy(filename
, filename_size
, "");
1113 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1117 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1118 const uint8_t *buf
, int nb_sectors
)
1120 BlockDriver
*drv
= bs
->drv
;
1123 if (!drv
->bdrv_write_compressed
)
1125 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1128 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1130 BlockDriver
*drv
= bs
->drv
;
1133 if (!drv
->bdrv_get_info
)
1135 memset(bdi
, 0, sizeof(*bdi
));
1136 return drv
->bdrv_get_info(bs
, bdi
);
1139 /**************************************************************/
1140 /* handling of snapshots */
1142 int bdrv_snapshot_create(BlockDriverState
*bs
,
1143 QEMUSnapshotInfo
*sn_info
)
1145 BlockDriver
*drv
= bs
->drv
;
1148 if (!drv
->bdrv_snapshot_create
)
1150 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1153 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1154 const char *snapshot_id
)
1156 BlockDriver
*drv
= bs
->drv
;
1159 if (!drv
->bdrv_snapshot_goto
)
1161 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1164 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1166 BlockDriver
*drv
= bs
->drv
;
1169 if (!drv
->bdrv_snapshot_delete
)
1171 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1174 int bdrv_snapshot_list(BlockDriverState
*bs
,
1175 QEMUSnapshotInfo
**psn_info
)
1177 BlockDriver
*drv
= bs
->drv
;
1180 if (!drv
->bdrv_snapshot_list
)
1182 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1185 #define NB_SUFFIXES 4
1187 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1189 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1194 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1197 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1198 if (size
< (10 * base
)) {
1199 snprintf(buf
, buf_size
, "%0.1f%c",
1200 (double)size
/ base
,
1203 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1204 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1205 ((size
+ (base
>> 1)) / base
),
1215 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1217 char buf1
[128], date_buf
[128], clock_buf
[128];
1227 snprintf(buf
, buf_size
,
1228 "%-10s%-20s%7s%20s%15s",
1229 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1233 ptm
= localtime(&ti
);
1234 strftime(date_buf
, sizeof(date_buf
),
1235 "%Y-%m-%d %H:%M:%S", ptm
);
1237 localtime_r(&ti
, &tm
);
1238 strftime(date_buf
, sizeof(date_buf
),
1239 "%Y-%m-%d %H:%M:%S", &tm
);
1241 secs
= sn
->vm_clock_nsec
/ 1000000000;
1242 snprintf(clock_buf
, sizeof(clock_buf
),
1243 "%02d:%02d:%02d.%03d",
1245 (int)((secs
/ 60) % 60),
1247 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1248 snprintf(buf
, buf_size
,
1249 "%-10s%-20s%7s%20s%15s",
1250 sn
->id_str
, sn
->name
,
1251 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1259 /**************************************************************/
1262 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1263 uint8_t *buf
, int nb_sectors
,
1264 BlockDriverCompletionFunc
*cb
, void *opaque
)
1266 BlockDriver
*drv
= bs
->drv
;
1267 BlockDriverAIOCB
*ret
;
1272 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1275 /* Update stats even though technically transfer has not happened. */
1276 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1283 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1284 const uint8_t *buf
, int nb_sectors
,
1285 BlockDriverCompletionFunc
*cb
, void *opaque
)
1287 BlockDriver
*drv
= bs
->drv
;
1288 BlockDriverAIOCB
*ret
;
1295 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1298 /* Update stats even though technically transfer has not happened. */
1299 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1306 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1308 BlockDriver
*drv
= acb
->bs
->drv
;
1310 drv
->bdrv_aio_cancel(acb
);
1314 /**************************************************************/
1315 /* async block device emulation */
1317 static void bdrv_aio_bh_cb(void *opaque
)
1319 BlockDriverAIOCBSync
*acb
= opaque
;
1320 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1321 qemu_aio_release(acb
);
1324 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1325 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1326 BlockDriverCompletionFunc
*cb
, void *opaque
)
1328 BlockDriverAIOCBSync
*acb
;
1331 acb
= qemu_aio_get(bs
, cb
, opaque
);
1333 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1334 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1336 qemu_bh_schedule(acb
->bh
);
1337 return &acb
->common
;
1340 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1341 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1342 BlockDriverCompletionFunc
*cb
, void *opaque
)
1344 BlockDriverAIOCBSync
*acb
;
1347 acb
= qemu_aio_get(bs
, cb
, opaque
);
1349 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1350 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1352 qemu_bh_schedule(acb
->bh
);
1353 return &acb
->common
;
1356 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1358 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1359 qemu_bh_cancel(acb
->bh
);
1360 qemu_aio_release(acb
);
1363 /**************************************************************/
1364 /* sync block device emulation */
1366 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1368 *(int *)opaque
= ret
;
1371 #define NOT_DONE 0x7fffffff
1373 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1374 uint8_t *buf
, int nb_sectors
)
1377 BlockDriverAIOCB
*acb
;
1379 async_ret
= NOT_DONE
;
1380 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1381 bdrv_rw_em_cb
, &async_ret
);
1385 while (async_ret
== NOT_DONE
) {
1392 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1393 const uint8_t *buf
, int nb_sectors
)
1396 BlockDriverAIOCB
*acb
;
1398 async_ret
= NOT_DONE
;
1399 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1400 bdrv_rw_em_cb
, &async_ret
);
1403 while (async_ret
== NOT_DONE
) {
1409 void bdrv_init(void)
1411 bdrv_register(&bdrv_raw
);
1412 bdrv_register(&bdrv_host_device
);
1414 bdrv_register(&bdrv_cow
);
1416 bdrv_register(&bdrv_qcow
);
1417 bdrv_register(&bdrv_vmdk
);
1418 bdrv_register(&bdrv_cloop
);
1419 bdrv_register(&bdrv_dmg
);
1420 bdrv_register(&bdrv_bochs
);
1421 bdrv_register(&bdrv_vpc
);
1422 bdrv_register(&bdrv_vvfat
);
1423 bdrv_register(&bdrv_qcow2
);
1424 bdrv_register(&bdrv_parallels
);
1425 bdrv_register(&bdrv_nbd
);
1428 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1432 BlockDriverAIOCB
*acb
;
1435 if (drv
->free_aiocb
) {
1436 acb
= drv
->free_aiocb
;
1437 drv
->free_aiocb
= acb
->next
;
1439 acb
= qemu_mallocz(drv
->aiocb_size
);
1445 acb
->opaque
= opaque
;
1449 void qemu_aio_release(void *p
)
1451 BlockDriverAIOCB
*acb
= p
;
1452 BlockDriver
*drv
= acb
->bs
->drv
;
1453 acb
->next
= drv
->free_aiocb
;
1454 drv
->free_aiocb
= acb
;
1457 /**************************************************************/
1458 /* removable device support */
1461 * Return TRUE if the media is present
1463 int bdrv_is_inserted(BlockDriverState
*bs
)
1465 BlockDriver
*drv
= bs
->drv
;
1469 if (!drv
->bdrv_is_inserted
)
1471 ret
= drv
->bdrv_is_inserted(bs
);
1476 * Return TRUE if the media changed since the last call to this
1477 * function. It is currently only used for floppy disks
1479 int bdrv_media_changed(BlockDriverState
*bs
)
1481 BlockDriver
*drv
= bs
->drv
;
1484 if (!drv
|| !drv
->bdrv_media_changed
)
1487 ret
= drv
->bdrv_media_changed(bs
);
1488 if (ret
== -ENOTSUP
)
1489 ret
= bs
->media_changed
;
1490 bs
->media_changed
= 0;
1495 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1497 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1499 BlockDriver
*drv
= bs
->drv
;
1502 if (!drv
|| !drv
->bdrv_eject
) {
1505 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1507 if (ret
== -ENOTSUP
) {
1513 int bdrv_is_locked(BlockDriverState
*bs
)
1519 * Lock or unlock the media (if it is locked, the user won't be able
1520 * to eject it manually).
1522 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1524 BlockDriver
*drv
= bs
->drv
;
1526 bs
->locked
= locked
;
1527 if (drv
&& drv
->bdrv_set_locked
) {
1528 drv
->bdrv_set_locked(bs
, locked
);
1532 /* needed for generic scsi interface */
1534 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1536 BlockDriver
*drv
= bs
->drv
;
1538 if (drv
&& drv
->bdrv_ioctl
)
1539 return drv
->bdrv_ioctl(bs
, req
, buf
);