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"
29 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <sys/queue.h>
37 #define SECTOR_SIZE (1 << SECTOR_BITS)
39 typedef struct BlockDriverAIOCBSync
{
40 BlockDriverAIOCB common
;
43 } BlockDriverAIOCBSync
;
45 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
46 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
47 BlockDriverCompletionFunc
*cb
, void *opaque
);
48 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
49 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
50 BlockDriverCompletionFunc
*cb
, void *opaque
);
51 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
52 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
53 uint8_t *buf
, int nb_sectors
);
54 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
55 const uint8_t *buf
, int nb_sectors
);
57 BlockDriverState
*bdrv_first
;
59 static BlockDriver
*first_drv
;
61 int path_is_absolute(const char *path
)
65 /* specific case for names like: "\\.\d:" */
66 if (*path
== '/' || *path
== '\\')
69 p
= strchr(path
, ':');
75 return (*p
== '/' || *p
== '\\');
81 /* if filename is absolute, just copy it to dest. Otherwise, build a
82 path to it by considering it is relative to base_path. URL are
84 void path_combine(char *dest
, int dest_size
,
85 const char *base_path
,
93 if (path_is_absolute(filename
)) {
94 pstrcpy(dest
, dest_size
, filename
);
96 p
= strchr(base_path
, ':');
101 p1
= strrchr(base_path
, '/');
105 p2
= strrchr(base_path
, '\\');
117 if (len
> dest_size
- 1)
119 memcpy(dest
, base_path
, len
);
121 pstrcat(dest
, dest_size
, filename
);
126 static void bdrv_register(BlockDriver
*bdrv
)
128 if (!bdrv
->bdrv_aio_read
) {
129 /* add AIO emulation layer */
130 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
131 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
132 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
133 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
134 } else if (!bdrv
->bdrv_read
&& !bdrv
->bdrv_pread
) {
135 /* add synchronous IO emulation layer */
136 bdrv
->bdrv_read
= bdrv_read_em
;
137 bdrv
->bdrv_write
= bdrv_write_em
;
139 bdrv
->next
= first_drv
;
143 /* create a new block device (by default it is empty) */
144 BlockDriverState
*bdrv_new(const char *device_name
)
146 BlockDriverState
**pbs
, *bs
;
148 bs
= qemu_mallocz(sizeof(BlockDriverState
));
151 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
152 if (device_name
[0] != '\0') {
153 /* insert at the end */
162 BlockDriver
*bdrv_find_format(const char *format_name
)
165 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
166 if (!strcmp(drv1
->format_name
, format_name
))
172 int bdrv_create(BlockDriver
*drv
,
173 const char *filename
, int64_t size_in_sectors
,
174 const char *backing_file
, int flags
)
176 if (!drv
->bdrv_create
)
178 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
182 void get_tmp_filename(char *filename
, int size
)
184 char temp_dir
[MAX_PATH
];
186 GetTempPath(MAX_PATH
, temp_dir
);
187 GetTempFileName(temp_dir
, "qem", 0, filename
);
190 void get_tmp_filename(char *filename
, int size
)
194 /* XXX: race condition possible */
195 tmpdir
= getenv("TMPDIR");
198 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
199 fd
= mkstemp(filename
);
205 static int is_windows_drive_prefix(const char *filename
)
207 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
208 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
212 static int is_windows_drive(const char *filename
)
214 if (is_windows_drive_prefix(filename
) &&
217 if (strstart(filename
, "\\\\.\\", NULL
) ||
218 strstart(filename
, "//./", NULL
))
224 static BlockDriver
*find_protocol(const char *filename
)
232 if (is_windows_drive(filename
) ||
233 is_windows_drive_prefix(filename
))
236 p
= strchr(filename
, ':');
240 if (len
> sizeof(protocol
) - 1)
241 len
= sizeof(protocol
) - 1;
242 memcpy(protocol
, filename
, len
);
243 protocol
[len
] = '\0';
244 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
245 if (drv1
->protocol_name
&&
246 !strcmp(drv1
->protocol_name
, protocol
))
252 /* XXX: force raw format if block or character device ? It would
253 simplify the BSD case */
254 static BlockDriver
*find_image_format(const char *filename
)
256 int ret
, score
, score_max
;
257 BlockDriver
*drv1
, *drv
;
259 BlockDriverState
*bs
;
261 /* detect host devices. By convention, /dev/cdrom[N] is always
262 recognized as a host CDROM */
263 if (strstart(filename
, "/dev/cdrom", NULL
))
264 return &bdrv_host_device
;
266 if (is_windows_drive(filename
))
267 return &bdrv_host_device
;
271 if (stat(filename
, &st
) >= 0 &&
272 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
273 return &bdrv_host_device
;
278 drv
= find_protocol(filename
);
279 /* no need to test disk image formats for vvfat */
280 if (drv
== &bdrv_vvfat
)
283 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
286 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
293 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
294 if (drv1
->bdrv_probe
) {
295 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
296 if (score
> score_max
) {
305 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
307 BlockDriverState
*bs
;
313 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
322 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
324 return bdrv_open2(bs
, filename
, flags
, NULL
);
327 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
331 char tmp_filename
[PATH_MAX
];
332 char backing_filename
[PATH_MAX
];
335 bs
->is_temporary
= 0;
338 if (flags
& BDRV_O_SNAPSHOT
) {
339 BlockDriverState
*bs1
;
343 /* if snapshot, we create a temporary backing file and open it
344 instead of opening 'filename' directly */
346 /* if there is a backing file, use it */
351 if (bdrv_open(bs1
, filename
, 0) < 0) {
355 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
357 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
362 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
364 /* Real path is meaningless for protocols */
366 snprintf(backing_filename
, sizeof(backing_filename
),
369 realpath(filename
, backing_filename
);
371 if (bdrv_create(&bdrv_qcow2
, tmp_filename
,
372 total_size
, backing_filename
, 0) < 0) {
375 filename
= tmp_filename
;
376 bs
->is_temporary
= 1;
379 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
380 if (flags
& BDRV_O_FILE
) {
381 drv
= find_protocol(filename
);
386 drv
= find_image_format(filename
);
392 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
393 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
395 /* Note: for compatibility, we open disk image files as RDWR, and
396 RDONLY as fallback */
397 if (!(flags
& BDRV_O_FILE
))
398 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
400 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
401 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
402 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
403 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
407 qemu_free(bs
->opaque
);
412 if (drv
->bdrv_getlength
) {
413 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
416 if (bs
->is_temporary
) {
420 if (bs
->backing_file
[0] != '\0') {
421 /* if there is a backing file, use it */
422 bs
->backing_hd
= bdrv_new("");
423 if (!bs
->backing_hd
) {
428 path_combine(backing_filename
, sizeof(backing_filename
),
429 filename
, bs
->backing_file
);
430 if (bdrv_open(bs
->backing_hd
, backing_filename
, open_flags
) < 0)
434 /* call the change callback */
435 bs
->media_changed
= 1;
437 bs
->change_cb(bs
->change_opaque
);
442 void bdrv_close(BlockDriverState
*bs
)
446 bdrv_delete(bs
->backing_hd
);
447 bs
->drv
->bdrv_close(bs
);
448 qemu_free(bs
->opaque
);
450 if (bs
->is_temporary
) {
451 unlink(bs
->filename
);
457 /* call the change callback */
458 bs
->media_changed
= 1;
460 bs
->change_cb(bs
->change_opaque
);
464 void bdrv_delete(BlockDriverState
*bs
)
466 BlockDriverState
**pbs
;
469 while (*pbs
!= bs
&& *pbs
!= NULL
)
478 /* commit COW file into the raw image */
479 int bdrv_commit(BlockDriverState
*bs
)
481 BlockDriver
*drv
= bs
->drv
;
482 int64_t i
, total_sectors
;
484 unsigned char sector
[512];
493 if (!bs
->backing_hd
) {
497 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
498 for (i
= 0; i
< total_sectors
;) {
499 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
500 for(j
= 0; j
< n
; j
++) {
501 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
505 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
515 if (drv
->bdrv_make_empty
)
516 return drv
->bdrv_make_empty(bs
);
521 /* return < 0 if error. See bdrv_write() for the return codes */
522 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
523 uint8_t *buf
, int nb_sectors
)
525 BlockDriver
*drv
= bs
->drv
;
530 if (drv
->bdrv_pread
) {
532 len
= nb_sectors
* 512;
533 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
539 bs
->rd_bytes
+= (unsigned) len
;
544 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
548 /* Return < 0 if error. Important errors are:
549 -EIO generic I/O error (may happen for all errors)
550 -ENOMEDIUM No media inserted.
551 -EINVAL Invalid sector number or nb_sectors
552 -EACCES Trying to write a read-only device
554 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
555 const uint8_t *buf
, int nb_sectors
)
557 BlockDriver
*drv
= bs
->drv
;
562 if (drv
->bdrv_pwrite
) {
564 len
= nb_sectors
* 512;
565 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
);
571 bs
->wr_bytes
+= (unsigned) len
;
576 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
580 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
581 uint8_t *buf
, int count1
)
583 uint8_t tmp_buf
[SECTOR_SIZE
];
584 int len
, nb_sectors
, count
;
588 /* first read to align to sector start */
589 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
592 sector_num
= offset
>> SECTOR_BITS
;
594 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
596 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
604 /* read the sectors "in place" */
605 nb_sectors
= count
>> SECTOR_BITS
;
606 if (nb_sectors
> 0) {
607 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
609 sector_num
+= nb_sectors
;
610 len
= nb_sectors
<< SECTOR_BITS
;
615 /* add data from the last sector */
617 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
619 memcpy(buf
, tmp_buf
, count
);
624 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
625 const uint8_t *buf
, int count1
)
627 uint8_t tmp_buf
[SECTOR_SIZE
];
628 int len
, nb_sectors
, count
;
632 /* first write 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(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
641 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
650 /* write the sectors "in place" */
651 nb_sectors
= count
>> SECTOR_BITS
;
652 if (nb_sectors
> 0) {
653 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
655 sector_num
+= nb_sectors
;
656 len
= nb_sectors
<< SECTOR_BITS
;
661 /* add data from the last sector */
663 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
665 memcpy(tmp_buf
, buf
, count
);
666 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
673 * Read with byte offsets (needed only for file protocols)
675 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
676 void *buf1
, int count1
)
678 BlockDriver
*drv
= bs
->drv
;
682 if (!drv
->bdrv_pread
)
683 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
684 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
688 * Write with byte offsets (needed only for file protocols)
690 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
691 const void *buf1
, int count1
)
693 BlockDriver
*drv
= bs
->drv
;
697 if (!drv
->bdrv_pwrite
)
698 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
699 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
703 * Truncate file to 'offset' bytes (needed only for file protocols)
705 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
707 BlockDriver
*drv
= bs
->drv
;
710 if (!drv
->bdrv_truncate
)
712 return drv
->bdrv_truncate(bs
, offset
);
716 * Length of a file in bytes. Return < 0 if error or unknown.
718 int64_t bdrv_getlength(BlockDriverState
*bs
)
720 BlockDriver
*drv
= bs
->drv
;
723 if (!drv
->bdrv_getlength
) {
725 return bs
->total_sectors
* SECTOR_SIZE
;
727 return drv
->bdrv_getlength(bs
);
730 /* return 0 as number of sectors if no device present or error */
731 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
734 length
= bdrv_getlength(bs
);
738 length
= length
>> SECTOR_BITS
;
739 *nb_sectors_ptr
= length
;
743 uint8_t boot_ind
; /* 0x80 - active */
744 uint8_t head
; /* starting head */
745 uint8_t sector
; /* starting sector */
746 uint8_t cyl
; /* starting cylinder */
747 uint8_t sys_ind
; /* What partition type */
748 uint8_t end_head
; /* end head */
749 uint8_t end_sector
; /* end sector */
750 uint8_t end_cyl
; /* end cylinder */
751 uint32_t start_sect
; /* starting sector counting from 0 */
752 uint32_t nr_sects
; /* nr of sectors in partition */
753 } __attribute__((packed
));
755 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
756 static int guess_disk_lchs(BlockDriverState
*bs
,
757 int *pcylinders
, int *pheads
, int *psectors
)
760 int ret
, i
, heads
, sectors
, cylinders
;
765 bdrv_get_geometry(bs
, &nb_sectors
);
767 ret
= bdrv_read(bs
, 0, buf
, 1);
770 /* test msdos magic */
771 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
773 for(i
= 0; i
< 4; i
++) {
774 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
775 nr_sects
= le32_to_cpu(p
->nr_sects
);
776 if (nr_sects
&& p
->end_head
) {
777 /* We make the assumption that the partition terminates on
778 a cylinder boundary */
779 heads
= p
->end_head
+ 1;
780 sectors
= p
->end_sector
& 63;
783 cylinders
= nb_sectors
/ (heads
* sectors
);
784 if (cylinders
< 1 || cylinders
> 16383)
788 *pcylinders
= cylinders
;
790 printf("guessed geometry: LCHS=%d %d %d\n",
791 cylinders
, heads
, sectors
);
799 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
801 int translation
, lba_detected
= 0;
802 int cylinders
, heads
, secs
;
805 /* if a geometry hint is available, use it */
806 bdrv_get_geometry(bs
, &nb_sectors
);
807 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
808 translation
= bdrv_get_translation_hint(bs
);
809 if (cylinders
!= 0) {
814 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
816 /* if heads > 16, it means that a BIOS LBA
817 translation was active, so the default
818 hardware geometry is OK */
820 goto default_geometry
;
825 /* disable any translation to be in sync with
826 the logical geometry */
827 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
828 bdrv_set_translation_hint(bs
,
829 BIOS_ATA_TRANSLATION_NONE
);
834 /* if no geometry, use a standard physical disk geometry */
835 cylinders
= nb_sectors
/ (16 * 63);
837 if (cylinders
> 16383)
839 else if (cylinders
< 2)
844 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
845 if ((*pcyls
* *pheads
) <= 131072) {
846 bdrv_set_translation_hint(bs
,
847 BIOS_ATA_TRANSLATION_LARGE
);
849 bdrv_set_translation_hint(bs
,
850 BIOS_ATA_TRANSLATION_LBA
);
854 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
858 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
859 int cyls
, int heads
, int secs
)
866 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
869 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
870 type
== BDRV_TYPE_FLOPPY
));
873 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
875 bs
->translation
= translation
;
878 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
879 int *pcyls
, int *pheads
, int *psecs
)
886 int bdrv_get_type_hint(BlockDriverState
*bs
)
891 int bdrv_get_translation_hint(BlockDriverState
*bs
)
893 return bs
->translation
;
896 int bdrv_is_removable(BlockDriverState
*bs
)
898 return bs
->removable
;
901 int bdrv_is_read_only(BlockDriverState
*bs
)
903 return bs
->read_only
;
906 int bdrv_is_sg(BlockDriverState
*bs
)
911 /* XXX: no longer used */
912 void bdrv_set_change_cb(BlockDriverState
*bs
,
913 void (*change_cb
)(void *opaque
), void *opaque
)
915 bs
->change_cb
= change_cb
;
916 bs
->change_opaque
= opaque
;
919 int bdrv_is_encrypted(BlockDriverState
*bs
)
921 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
923 return bs
->encrypted
;
926 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
929 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
930 ret
= bdrv_set_key(bs
->backing_hd
, key
);
936 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
938 return bs
->drv
->bdrv_set_key(bs
, key
);
941 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
946 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
950 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
955 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
956 it(opaque
, drv
->format_name
);
960 BlockDriverState
*bdrv_find(const char *name
)
962 BlockDriverState
*bs
;
964 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
965 if (!strcmp(name
, bs
->device_name
))
971 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
973 BlockDriverState
*bs
;
975 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
976 it(opaque
, bs
->device_name
);
980 const char *bdrv_get_device_name(BlockDriverState
*bs
)
982 return bs
->device_name
;
985 void bdrv_flush(BlockDriverState
*bs
)
987 if (bs
->drv
->bdrv_flush
)
988 bs
->drv
->bdrv_flush(bs
);
990 bdrv_flush(bs
->backing_hd
);
993 void bdrv_flush_all(void)
995 BlockDriverState
*bs
;
997 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
998 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
999 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1004 * Returns true iff the specified sector is present in the disk image. Drivers
1005 * not implementing the functionality are assumed to not support backing files,
1006 * hence all their sectors are reported as allocated.
1008 * 'pnum' is set to the number of sectors (including and immediately following
1009 * the specified sector) that are known to be in the same
1010 * allocated/unallocated state.
1012 * 'nb_sectors' is the max value 'pnum' should be set to.
1014 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1018 if (!bs
->drv
->bdrv_is_allocated
) {
1019 if (sector_num
>= bs
->total_sectors
) {
1023 n
= bs
->total_sectors
- sector_num
;
1024 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1027 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1030 void bdrv_info(void)
1032 BlockDriverState
*bs
;
1034 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1035 term_printf("%s:", bs
->device_name
);
1036 term_printf(" type=");
1041 case BDRV_TYPE_CDROM
:
1042 term_printf("cdrom");
1044 case BDRV_TYPE_FLOPPY
:
1045 term_printf("floppy");
1048 term_printf(" removable=%d", bs
->removable
);
1049 if (bs
->removable
) {
1050 term_printf(" locked=%d", bs
->locked
);
1053 term_printf(" file=");
1054 term_print_filename(bs
->filename
);
1055 if (bs
->backing_file
[0] != '\0') {
1056 term_printf(" backing_file=");
1057 term_print_filename(bs
->backing_file
);
1059 term_printf(" ro=%d", bs
->read_only
);
1060 term_printf(" drv=%s", bs
->drv
->format_name
);
1062 term_printf(" encrypted");
1064 term_printf(" [not inserted]");
1070 /* The "info blockstats" command. */
1071 void bdrv_info_stats (void)
1073 BlockDriverState
*bs
;
1075 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1077 " rd_bytes=%" PRIu64
1078 " wr_bytes=%" PRIu64
1079 " rd_operations=%" PRIu64
1080 " wr_operations=%" PRIu64
1083 bs
->rd_bytes
, bs
->wr_bytes
,
1084 bs
->rd_ops
, bs
->wr_ops
);
1088 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1089 char *filename
, int filename_size
)
1091 if (!bs
->backing_hd
) {
1092 pstrcpy(filename
, filename_size
, "");
1094 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1098 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1099 const uint8_t *buf
, int nb_sectors
)
1101 BlockDriver
*drv
= bs
->drv
;
1104 if (!drv
->bdrv_write_compressed
)
1106 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1109 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1111 BlockDriver
*drv
= bs
->drv
;
1114 if (!drv
->bdrv_get_info
)
1116 memset(bdi
, 0, sizeof(*bdi
));
1117 return drv
->bdrv_get_info(bs
, bdi
);
1120 /**************************************************************/
1121 /* handling of snapshots */
1123 int bdrv_snapshot_create(BlockDriverState
*bs
,
1124 QEMUSnapshotInfo
*sn_info
)
1126 BlockDriver
*drv
= bs
->drv
;
1129 if (!drv
->bdrv_snapshot_create
)
1131 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1134 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1135 const char *snapshot_id
)
1137 BlockDriver
*drv
= bs
->drv
;
1140 if (!drv
->bdrv_snapshot_goto
)
1142 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1145 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1147 BlockDriver
*drv
= bs
->drv
;
1150 if (!drv
->bdrv_snapshot_delete
)
1152 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1155 int bdrv_snapshot_list(BlockDriverState
*bs
,
1156 QEMUSnapshotInfo
**psn_info
)
1158 BlockDriver
*drv
= bs
->drv
;
1161 if (!drv
->bdrv_snapshot_list
)
1163 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1166 #define NB_SUFFIXES 4
1168 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1170 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1175 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1178 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1179 if (size
< (10 * base
)) {
1180 snprintf(buf
, buf_size
, "%0.1f%c",
1181 (double)size
/ base
,
1184 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1185 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1186 ((size
+ (base
>> 1)) / base
),
1196 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1198 char buf1
[128], date_buf
[128], clock_buf
[128];
1208 snprintf(buf
, buf_size
,
1209 "%-10s%-20s%7s%20s%15s",
1210 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1214 ptm
= localtime(&ti
);
1215 strftime(date_buf
, sizeof(date_buf
),
1216 "%Y-%m-%d %H:%M:%S", ptm
);
1218 localtime_r(&ti
, &tm
);
1219 strftime(date_buf
, sizeof(date_buf
),
1220 "%Y-%m-%d %H:%M:%S", &tm
);
1222 secs
= sn
->vm_clock_nsec
/ 1000000000;
1223 snprintf(clock_buf
, sizeof(clock_buf
),
1224 "%02d:%02d:%02d.%03d",
1226 (int)((secs
/ 60) % 60),
1228 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1229 snprintf(buf
, buf_size
,
1230 "%-10s%-20s%7s%20s%15s",
1231 sn
->id_str
, sn
->name
,
1232 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1240 /**************************************************************/
1243 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1244 uint8_t *buf
, int nb_sectors
,
1245 BlockDriverCompletionFunc
*cb
, void *opaque
)
1247 BlockDriver
*drv
= bs
->drv
;
1248 BlockDriverAIOCB
*ret
;
1253 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1256 /* Update stats even though technically transfer has not happened. */
1257 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1264 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1265 const uint8_t *buf
, int nb_sectors
,
1266 BlockDriverCompletionFunc
*cb
, void *opaque
)
1268 BlockDriver
*drv
= bs
->drv
;
1269 BlockDriverAIOCB
*ret
;
1276 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1279 /* Update stats even though technically transfer has not happened. */
1280 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1287 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1289 BlockDriver
*drv
= acb
->bs
->drv
;
1291 drv
->bdrv_aio_cancel(acb
);
1295 /**************************************************************/
1296 /* async block device emulation */
1298 static void bdrv_aio_bh_cb(void *opaque
)
1300 BlockDriverAIOCBSync
*acb
= opaque
;
1301 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1302 qemu_aio_release(acb
);
1305 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1306 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1307 BlockDriverCompletionFunc
*cb
, void *opaque
)
1309 BlockDriverAIOCBSync
*acb
;
1312 acb
= qemu_aio_get(bs
, cb
, opaque
);
1314 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1315 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1317 qemu_bh_schedule(acb
->bh
);
1318 return &acb
->common
;
1321 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1322 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1323 BlockDriverCompletionFunc
*cb
, void *opaque
)
1325 BlockDriverAIOCBSync
*acb
;
1328 acb
= qemu_aio_get(bs
, cb
, opaque
);
1330 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1331 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1333 qemu_bh_schedule(acb
->bh
);
1334 return &acb
->common
;
1337 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1339 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1340 qemu_bh_cancel(acb
->bh
);
1341 qemu_aio_release(acb
);
1344 /**************************************************************/
1345 /* sync block device emulation */
1347 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1349 *(int *)opaque
= ret
;
1352 #define NOT_DONE 0x7fffffff
1354 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1355 uint8_t *buf
, int nb_sectors
)
1358 BlockDriverAIOCB
*acb
;
1360 async_ret
= NOT_DONE
;
1361 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1362 bdrv_rw_em_cb
, &async_ret
);
1366 while (async_ret
== NOT_DONE
) {
1373 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1374 const uint8_t *buf
, int nb_sectors
)
1377 BlockDriverAIOCB
*acb
;
1379 async_ret
= NOT_DONE
;
1380 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1381 bdrv_rw_em_cb
, &async_ret
);
1384 while (async_ret
== NOT_DONE
) {
1390 void bdrv_init(void)
1392 bdrv_register(&bdrv_raw
);
1393 bdrv_register(&bdrv_host_device
);
1395 bdrv_register(&bdrv_cow
);
1397 bdrv_register(&bdrv_qcow
);
1398 bdrv_register(&bdrv_vmdk
);
1399 bdrv_register(&bdrv_cloop
);
1400 bdrv_register(&bdrv_dmg
);
1401 bdrv_register(&bdrv_bochs
);
1402 bdrv_register(&bdrv_vpc
);
1403 bdrv_register(&bdrv_vvfat
);
1404 bdrv_register(&bdrv_qcow2
);
1405 bdrv_register(&bdrv_parallels
);
1406 bdrv_register(&bdrv_nbd
);
1409 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1413 BlockDriverAIOCB
*acb
;
1416 if (drv
->free_aiocb
) {
1417 acb
= drv
->free_aiocb
;
1418 drv
->free_aiocb
= acb
->next
;
1420 acb
= qemu_mallocz(drv
->aiocb_size
);
1426 acb
->opaque
= opaque
;
1430 void qemu_aio_release(void *p
)
1432 BlockDriverAIOCB
*acb
= p
;
1433 BlockDriver
*drv
= acb
->bs
->drv
;
1434 acb
->next
= drv
->free_aiocb
;
1435 drv
->free_aiocb
= acb
;
1438 /**************************************************************/
1439 /* removable device support */
1442 * Return TRUE if the media is present
1444 int bdrv_is_inserted(BlockDriverState
*bs
)
1446 BlockDriver
*drv
= bs
->drv
;
1450 if (!drv
->bdrv_is_inserted
)
1452 ret
= drv
->bdrv_is_inserted(bs
);
1457 * Return TRUE if the media changed since the last call to this
1458 * function. It is currently only used for floppy disks
1460 int bdrv_media_changed(BlockDriverState
*bs
)
1462 BlockDriver
*drv
= bs
->drv
;
1465 if (!drv
|| !drv
->bdrv_media_changed
)
1468 ret
= drv
->bdrv_media_changed(bs
);
1469 if (ret
== -ENOTSUP
)
1470 ret
= bs
->media_changed
;
1471 bs
->media_changed
= 0;
1476 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1478 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1480 BlockDriver
*drv
= bs
->drv
;
1483 if (!drv
|| !drv
->bdrv_eject
) {
1486 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1488 if (ret
== -ENOTSUP
) {
1494 int bdrv_is_locked(BlockDriverState
*bs
)
1500 * Lock or unlock the media (if it is locked, the user won't be able
1501 * to eject it manually).
1503 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1505 BlockDriver
*drv
= bs
->drv
;
1507 bs
->locked
= locked
;
1508 if (drv
&& drv
->bdrv_set_locked
) {
1509 drv
->bdrv_set_locked(bs
, locked
);
1513 /* needed for generic scsi interface */
1515 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1517 BlockDriver
*drv
= bs
->drv
;
1519 if (drv
&& drv
->bdrv_ioctl
)
1520 return drv
->bdrv_ioctl(bs
, req
, buf
);