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_DIRECT
);
401 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
402 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
403 if (ret
== -EACCES
&& !(flags
& BDRV_O_FILE
)) {
404 ret
= drv
->bdrv_open(bs
, filename
, BDRV_O_RDONLY
);
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
, 0) < 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 (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
532 memcpy(buf
, bs
->boot_sector_data
, 512);
539 if (drv
->bdrv_pread
) {
541 len
= nb_sectors
* 512;
542 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
548 bs
->rd_bytes
+= (unsigned) len
;
553 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
557 /* Return < 0 if error. Important errors are:
558 -EIO generic I/O error (may happen for all errors)
559 -ENOMEDIUM No media inserted.
560 -EINVAL Invalid sector number or nb_sectors
561 -EACCES Trying to write a read-only device
563 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
564 const uint8_t *buf
, int nb_sectors
)
566 BlockDriver
*drv
= bs
->drv
;
571 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
572 memcpy(bs
->boot_sector_data
, buf
, 512);
574 if (drv
->bdrv_pwrite
) {
576 len
= nb_sectors
* 512;
577 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
);
583 bs
->wr_bytes
+= (unsigned) len
;
588 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
592 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
593 uint8_t *buf
, int count1
)
595 uint8_t tmp_buf
[SECTOR_SIZE
];
596 int len
, nb_sectors
, count
;
600 /* first read to align to sector start */
601 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
604 sector_num
= offset
>> SECTOR_BITS
;
606 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
608 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
616 /* read the sectors "in place" */
617 nb_sectors
= count
>> SECTOR_BITS
;
618 if (nb_sectors
> 0) {
619 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
621 sector_num
+= nb_sectors
;
622 len
= nb_sectors
<< SECTOR_BITS
;
627 /* add data from the last sector */
629 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
631 memcpy(buf
, tmp_buf
, count
);
636 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
637 const uint8_t *buf
, int count1
)
639 uint8_t tmp_buf
[SECTOR_SIZE
];
640 int len
, nb_sectors
, count
;
644 /* first write to align to sector start */
645 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
648 sector_num
= offset
>> SECTOR_BITS
;
650 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
652 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
653 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
662 /* write the sectors "in place" */
663 nb_sectors
= count
>> SECTOR_BITS
;
664 if (nb_sectors
> 0) {
665 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
667 sector_num
+= nb_sectors
;
668 len
= nb_sectors
<< SECTOR_BITS
;
673 /* add data from the last sector */
675 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
677 memcpy(tmp_buf
, buf
, count
);
678 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
685 * Read with byte offsets (needed only for file protocols)
687 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
688 void *buf1
, int count1
)
690 BlockDriver
*drv
= bs
->drv
;
694 if (!drv
->bdrv_pread
)
695 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
696 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
700 * Write with byte offsets (needed only for file protocols)
702 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
703 const void *buf1
, int count1
)
705 BlockDriver
*drv
= bs
->drv
;
709 if (!drv
->bdrv_pwrite
)
710 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
711 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
715 * Truncate file to 'offset' bytes (needed only for file protocols)
717 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
719 BlockDriver
*drv
= bs
->drv
;
722 if (!drv
->bdrv_truncate
)
724 return drv
->bdrv_truncate(bs
, offset
);
728 * Length of a file in bytes. Return < 0 if error or unknown.
730 int64_t bdrv_getlength(BlockDriverState
*bs
)
732 BlockDriver
*drv
= bs
->drv
;
735 if (!drv
->bdrv_getlength
) {
737 return bs
->total_sectors
* SECTOR_SIZE
;
739 return drv
->bdrv_getlength(bs
);
742 /* return 0 as number of sectors if no device present or error */
743 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
746 length
= bdrv_getlength(bs
);
750 length
= length
>> SECTOR_BITS
;
751 *nb_sectors_ptr
= length
;
754 /* force a given boot sector. */
755 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
)
757 bs
->boot_sector_enabled
= 1;
760 memcpy(bs
->boot_sector_data
, data
, size
);
761 memset(bs
->boot_sector_data
+ size
, 0, 512 - size
);
765 uint8_t boot_ind
; /* 0x80 - active */
766 uint8_t head
; /* starting head */
767 uint8_t sector
; /* starting sector */
768 uint8_t cyl
; /* starting cylinder */
769 uint8_t sys_ind
; /* What partition type */
770 uint8_t end_head
; /* end head */
771 uint8_t end_sector
; /* end sector */
772 uint8_t end_cyl
; /* end cylinder */
773 uint32_t start_sect
; /* starting sector counting from 0 */
774 uint32_t nr_sects
; /* nr of sectors in partition */
775 } __attribute__((packed
));
777 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
778 static int guess_disk_lchs(BlockDriverState
*bs
,
779 int *pcylinders
, int *pheads
, int *psectors
)
782 int ret
, i
, heads
, sectors
, cylinders
;
787 buf
= qemu_memalign(512, 512);
791 bdrv_get_geometry(bs
, &nb_sectors
);
793 ret
= bdrv_read(bs
, 0, buf
, 1);
796 /* test msdos magic */
797 if (buf
[510] != 0x55 || buf
[511] != 0xaa) {
801 for(i
= 0; i
< 4; i
++) {
802 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
803 nr_sects
= le32_to_cpu(p
->nr_sects
);
804 if (nr_sects
&& p
->end_head
) {
805 /* We make the assumption that the partition terminates on
806 a cylinder boundary */
807 heads
= p
->end_head
+ 1;
808 sectors
= p
->end_sector
& 63;
811 cylinders
= nb_sectors
/ (heads
* sectors
);
812 if (cylinders
< 1 || cylinders
> 16383)
816 *pcylinders
= cylinders
;
818 printf("guessed geometry: LCHS=%d %d %d\n",
819 cylinders
, heads
, sectors
);
829 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
831 int translation
, lba_detected
= 0;
832 int cylinders
, heads
, secs
;
835 /* if a geometry hint is available, use it */
836 bdrv_get_geometry(bs
, &nb_sectors
);
837 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
838 translation
= bdrv_get_translation_hint(bs
);
839 if (cylinders
!= 0) {
844 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
846 /* if heads > 16, it means that a BIOS LBA
847 translation was active, so the default
848 hardware geometry is OK */
850 goto default_geometry
;
855 /* disable any translation to be in sync with
856 the logical geometry */
857 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
858 bdrv_set_translation_hint(bs
,
859 BIOS_ATA_TRANSLATION_NONE
);
864 /* if no geometry, use a standard physical disk geometry */
865 cylinders
= nb_sectors
/ (16 * 63);
867 if (cylinders
> 16383)
869 else if (cylinders
< 2)
874 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
875 if ((*pcyls
* *pheads
) <= 131072) {
876 bdrv_set_translation_hint(bs
,
877 BIOS_ATA_TRANSLATION_LARGE
);
879 bdrv_set_translation_hint(bs
,
880 BIOS_ATA_TRANSLATION_LBA
);
884 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
888 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
889 int cyls
, int heads
, int secs
)
896 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
899 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
900 type
== BDRV_TYPE_FLOPPY
));
903 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
905 bs
->translation
= translation
;
908 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
909 int *pcyls
, int *pheads
, int *psecs
)
916 int bdrv_get_type_hint(BlockDriverState
*bs
)
921 int bdrv_get_translation_hint(BlockDriverState
*bs
)
923 return bs
->translation
;
926 int bdrv_is_removable(BlockDriverState
*bs
)
928 return bs
->removable
;
931 int bdrv_is_read_only(BlockDriverState
*bs
)
933 return bs
->read_only
;
936 int bdrv_is_sg(BlockDriverState
*bs
)
941 /* XXX: no longer used */
942 void bdrv_set_change_cb(BlockDriverState
*bs
,
943 void (*change_cb
)(void *opaque
), void *opaque
)
945 bs
->change_cb
= change_cb
;
946 bs
->change_opaque
= opaque
;
949 int bdrv_is_encrypted(BlockDriverState
*bs
)
951 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
953 return bs
->encrypted
;
956 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
959 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
960 ret
= bdrv_set_key(bs
->backing_hd
, key
);
966 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
968 return bs
->drv
->bdrv_set_key(bs
, key
);
971 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
976 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
980 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
985 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
986 it(opaque
, drv
->format_name
);
990 BlockDriverState
*bdrv_find(const char *name
)
992 BlockDriverState
*bs
;
994 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
995 if (!strcmp(name
, bs
->device_name
))
1001 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
1003 BlockDriverState
*bs
;
1005 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1006 it(opaque
, bs
->device_name
);
1010 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1012 return bs
->device_name
;
1015 void bdrv_flush(BlockDriverState
*bs
)
1017 if (bs
->drv
->bdrv_flush
)
1018 bs
->drv
->bdrv_flush(bs
);
1020 bdrv_flush(bs
->backing_hd
);
1023 void bdrv_iterate_writeable(void (*it
)(BlockDriverState
*bs
))
1025 BlockDriverState
*bs
;
1027 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1028 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1029 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1033 void bdrv_flush_all(void)
1035 bdrv_iterate_writeable(bdrv_flush
);
1039 * Returns true iff the specified sector is present in the disk image. Drivers
1040 * not implementing the functionality are assumed to not support backing files,
1041 * hence all their sectors are reported as allocated.
1043 * 'pnum' is set to the number of sectors (including and immediately following
1044 * the specified sector) that are known to be in the same
1045 * allocated/unallocated state.
1047 * 'nb_sectors' is the max value 'pnum' should be set to.
1049 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1053 if (!bs
->drv
->bdrv_is_allocated
) {
1054 if (sector_num
>= bs
->total_sectors
) {
1058 n
= bs
->total_sectors
- sector_num
;
1059 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1062 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1065 void bdrv_info(void)
1067 BlockDriverState
*bs
;
1069 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1070 term_printf("%s:", bs
->device_name
);
1071 term_printf(" type=");
1076 case BDRV_TYPE_CDROM
:
1077 term_printf("cdrom");
1079 case BDRV_TYPE_FLOPPY
:
1080 term_printf("floppy");
1083 term_printf(" removable=%d", bs
->removable
);
1084 if (bs
->removable
) {
1085 term_printf(" locked=%d", bs
->locked
);
1088 term_printf(" file=");
1089 term_print_filename(bs
->filename
);
1090 if (bs
->backing_file
[0] != '\0') {
1091 term_printf(" backing_file=");
1092 term_print_filename(bs
->backing_file
);
1094 term_printf(" ro=%d", bs
->read_only
);
1095 term_printf(" drv=%s", bs
->drv
->format_name
);
1097 term_printf(" encrypted");
1099 term_printf(" [not inserted]");
1105 /* The "info blockstats" command. */
1106 void bdrv_info_stats (void)
1108 BlockDriverState
*bs
;
1110 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1112 " rd_bytes=%" PRIu64
1113 " wr_bytes=%" PRIu64
1114 " rd_operations=%" PRIu64
1115 " wr_operations=%" PRIu64
1118 bs
->rd_bytes
, bs
->wr_bytes
,
1119 bs
->rd_ops
, bs
->wr_ops
);
1123 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1124 char *filename
, int filename_size
)
1126 if (!bs
->backing_hd
) {
1127 pstrcpy(filename
, filename_size
, "");
1129 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1133 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1134 const uint8_t *buf
, int nb_sectors
)
1136 BlockDriver
*drv
= bs
->drv
;
1139 if (!drv
->bdrv_write_compressed
)
1141 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1144 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1146 BlockDriver
*drv
= bs
->drv
;
1149 if (!drv
->bdrv_get_info
)
1151 memset(bdi
, 0, sizeof(*bdi
));
1152 return drv
->bdrv_get_info(bs
, bdi
);
1155 /**************************************************************/
1156 /* handling of snapshots */
1158 int bdrv_snapshot_create(BlockDriverState
*bs
,
1159 QEMUSnapshotInfo
*sn_info
)
1161 BlockDriver
*drv
= bs
->drv
;
1164 if (!drv
->bdrv_snapshot_create
)
1166 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1169 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1170 const char *snapshot_id
)
1172 BlockDriver
*drv
= bs
->drv
;
1175 if (!drv
->bdrv_snapshot_goto
)
1177 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1180 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1182 BlockDriver
*drv
= bs
->drv
;
1185 if (!drv
->bdrv_snapshot_delete
)
1187 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1190 int bdrv_snapshot_list(BlockDriverState
*bs
,
1191 QEMUSnapshotInfo
**psn_info
)
1193 BlockDriver
*drv
= bs
->drv
;
1196 if (!drv
->bdrv_snapshot_list
)
1198 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1201 #define NB_SUFFIXES 4
1203 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1205 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1210 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1213 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1214 if (size
< (10 * base
)) {
1215 snprintf(buf
, buf_size
, "%0.1f%c",
1216 (double)size
/ base
,
1219 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1220 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1221 ((size
+ (base
>> 1)) / base
),
1231 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1233 char buf1
[128], date_buf
[128], clock_buf
[128];
1243 snprintf(buf
, buf_size
,
1244 "%-10s%-20s%7s%20s%15s",
1245 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1249 ptm
= localtime(&ti
);
1250 strftime(date_buf
, sizeof(date_buf
),
1251 "%Y-%m-%d %H:%M:%S", ptm
);
1253 localtime_r(&ti
, &tm
);
1254 strftime(date_buf
, sizeof(date_buf
),
1255 "%Y-%m-%d %H:%M:%S", &tm
);
1257 secs
= sn
->vm_clock_nsec
/ 1000000000;
1258 snprintf(clock_buf
, sizeof(clock_buf
),
1259 "%02d:%02d:%02d.%03d",
1261 (int)((secs
/ 60) % 60),
1263 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1264 snprintf(buf
, buf_size
,
1265 "%-10s%-20s%7s%20s%15s",
1266 sn
->id_str
, sn
->name
,
1267 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1275 /**************************************************************/
1278 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1279 uint8_t *buf
, int nb_sectors
,
1280 BlockDriverCompletionFunc
*cb
, void *opaque
)
1282 BlockDriver
*drv
= bs
->drv
;
1283 BlockDriverAIOCB
*ret
;
1288 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1289 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1290 memcpy(buf
, bs
->boot_sector_data
, 512);
1296 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1299 /* Update stats even though technically transfer has not happened. */
1300 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1307 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1308 const uint8_t *buf
, int nb_sectors
,
1309 BlockDriverCompletionFunc
*cb
, void *opaque
)
1311 BlockDriver
*drv
= bs
->drv
;
1312 BlockDriverAIOCB
*ret
;
1318 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1319 memcpy(bs
->boot_sector_data
, buf
, 512);
1322 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1325 /* Update stats even though technically transfer has not happened. */
1326 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1333 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1335 BlockDriver
*drv
= acb
->bs
->drv
;
1337 drv
->bdrv_aio_cancel(acb
);
1341 /**************************************************************/
1342 /* async block device emulation */
1344 static void bdrv_aio_bh_cb(void *opaque
)
1346 BlockDriverAIOCBSync
*acb
= opaque
;
1347 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1348 qemu_aio_release(acb
);
1351 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1352 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1353 BlockDriverCompletionFunc
*cb
, void *opaque
)
1355 BlockDriverAIOCBSync
*acb
;
1358 acb
= qemu_aio_get(bs
, cb
, opaque
);
1360 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1361 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1363 qemu_bh_schedule(acb
->bh
);
1364 return &acb
->common
;
1367 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1368 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1369 BlockDriverCompletionFunc
*cb
, void *opaque
)
1371 BlockDriverAIOCBSync
*acb
;
1374 acb
= qemu_aio_get(bs
, cb
, opaque
);
1376 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1377 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1379 qemu_bh_schedule(acb
->bh
);
1380 return &acb
->common
;
1383 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1385 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1386 qemu_bh_cancel(acb
->bh
);
1387 qemu_aio_release(acb
);
1390 /**************************************************************/
1391 /* sync block device emulation */
1393 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1395 *(int *)opaque
= ret
;
1398 #define NOT_DONE 0x7fffffff
1400 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1401 uint8_t *buf
, int nb_sectors
)
1404 BlockDriverAIOCB
*acb
;
1406 async_ret
= NOT_DONE
;
1407 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1408 bdrv_rw_em_cb
, &async_ret
);
1412 while (async_ret
== NOT_DONE
) {
1419 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1420 const uint8_t *buf
, int nb_sectors
)
1423 BlockDriverAIOCB
*acb
;
1425 async_ret
= NOT_DONE
;
1426 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1427 bdrv_rw_em_cb
, &async_ret
);
1430 while (async_ret
== NOT_DONE
) {
1436 void bdrv_init(void)
1438 bdrv_register(&bdrv_raw
);
1439 bdrv_register(&bdrv_host_device
);
1441 bdrv_register(&bdrv_cow
);
1443 bdrv_register(&bdrv_qcow
);
1444 bdrv_register(&bdrv_vmdk
);
1445 bdrv_register(&bdrv_cloop
);
1446 bdrv_register(&bdrv_dmg
);
1447 bdrv_register(&bdrv_bochs
);
1448 bdrv_register(&bdrv_vpc
);
1449 bdrv_register(&bdrv_vvfat
);
1450 bdrv_register(&bdrv_qcow2
);
1451 bdrv_register(&bdrv_parallels
);
1452 bdrv_register(&bdrv_nbd
);
1455 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1459 BlockDriverAIOCB
*acb
;
1462 if (drv
->free_aiocb
) {
1463 acb
= drv
->free_aiocb
;
1464 drv
->free_aiocb
= acb
->next
;
1466 acb
= qemu_mallocz(drv
->aiocb_size
);
1472 acb
->opaque
= opaque
;
1476 void qemu_aio_release(void *p
)
1478 BlockDriverAIOCB
*acb
= p
;
1479 BlockDriver
*drv
= acb
->bs
->drv
;
1480 acb
->next
= drv
->free_aiocb
;
1481 drv
->free_aiocb
= acb
;
1484 /**************************************************************/
1485 /* removable device support */
1488 * Return TRUE if the media is present
1490 int bdrv_is_inserted(BlockDriverState
*bs
)
1492 BlockDriver
*drv
= bs
->drv
;
1496 if (!drv
->bdrv_is_inserted
)
1498 ret
= drv
->bdrv_is_inserted(bs
);
1503 * Return TRUE if the media changed since the last call to this
1504 * function. It is currently only used for floppy disks
1506 int bdrv_media_changed(BlockDriverState
*bs
)
1508 BlockDriver
*drv
= bs
->drv
;
1511 if (!drv
|| !drv
->bdrv_media_changed
)
1514 ret
= drv
->bdrv_media_changed(bs
);
1515 if (ret
== -ENOTSUP
)
1516 ret
= bs
->media_changed
;
1517 bs
->media_changed
= 0;
1522 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1524 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1526 BlockDriver
*drv
= bs
->drv
;
1529 if (!drv
|| !drv
->bdrv_eject
) {
1532 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1534 if (ret
== -ENOTSUP
) {
1540 int bdrv_is_locked(BlockDriverState
*bs
)
1546 * Lock or unlock the media (if it is locked, the user won't be able
1547 * to eject it manually).
1549 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1551 BlockDriver
*drv
= bs
->drv
;
1553 bs
->locked
= locked
;
1554 if (drv
&& drv
->bdrv_set_locked
) {
1555 drv
->bdrv_set_locked(bs
, locked
);
1559 /* needed for generic scsi interface */
1561 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1563 BlockDriver
*drv
= bs
->drv
;
1565 if (drv
&& drv
->bdrv_ioctl
)
1566 return drv
->bdrv_ioctl(bs
, req
, buf
);