2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "config-host.h"
26 /* include native header before sys-queue.h */
27 #include <sys/queue.h>
30 #include "qemu-common.h"
32 #include "block_int.h"
35 #include <sys/types.h>
37 #include <sys/ioctl.h>
48 #define SECTOR_SIZE (1 << SECTOR_BITS)
50 typedef struct BlockDriverAIOCBSync
{
51 BlockDriverAIOCB common
;
54 /* vector translation state */
58 } BlockDriverAIOCBSync
;
60 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
61 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
62 BlockDriverCompletionFunc
*cb
, void *opaque
);
63 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
64 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
65 BlockDriverCompletionFunc
*cb
, void *opaque
);
66 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
67 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
68 uint8_t *buf
, int nb_sectors
);
69 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
70 const uint8_t *buf
, int nb_sectors
);
72 BlockDriverState
*bdrv_first
;
74 static BlockDriver
*first_drv
;
76 int path_is_absolute(const char *path
)
80 /* specific case for names like: "\\.\d:" */
81 if (*path
== '/' || *path
== '\\')
84 p
= strchr(path
, ':');
90 return (*p
== '/' || *p
== '\\');
96 /* if filename is absolute, just copy it to dest. Otherwise, build a
97 path to it by considering it is relative to base_path. URL are
99 void path_combine(char *dest
, int dest_size
,
100 const char *base_path
,
101 const char *filename
)
108 if (path_is_absolute(filename
)) {
109 pstrcpy(dest
, dest_size
, filename
);
111 p
= strchr(base_path
, ':');
116 p1
= strrchr(base_path
, '/');
120 p2
= strrchr(base_path
, '\\');
132 if (len
> dest_size
- 1)
134 memcpy(dest
, base_path
, len
);
136 pstrcat(dest
, dest_size
, filename
);
141 static void bdrv_register(BlockDriver
*bdrv
)
143 if (!bdrv
->bdrv_aio_readv
) {
144 /* add AIO emulation layer */
145 bdrv
->bdrv_aio_readv
= bdrv_aio_readv_em
;
146 bdrv
->bdrv_aio_writev
= bdrv_aio_writev_em
;
147 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
148 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
149 } else if (!bdrv
->bdrv_read
) {
150 /* add synchronous IO emulation layer */
151 bdrv
->bdrv_read
= bdrv_read_em
;
152 bdrv
->bdrv_write
= bdrv_write_em
;
154 aio_pool_init(&bdrv
->aio_pool
, bdrv
->aiocb_size
, bdrv
->bdrv_aio_cancel
);
155 bdrv
->next
= first_drv
;
159 /* create a new block device (by default it is empty) */
160 BlockDriverState
*bdrv_new(const char *device_name
)
162 BlockDriverState
**pbs
, *bs
;
164 bs
= qemu_mallocz(sizeof(BlockDriverState
));
165 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
166 if (device_name
[0] != '\0') {
167 /* insert at the end */
176 BlockDriver
*bdrv_find_format(const char *format_name
)
179 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
180 if (!strcmp(drv1
->format_name
, format_name
))
186 int bdrv_create2(BlockDriver
*drv
,
187 const char *filename
, int64_t size_in_sectors
,
188 const char *backing_file
, const char *backing_format
,
191 if (drv
->bdrv_create2
)
192 return drv
->bdrv_create2(filename
, size_in_sectors
, backing_file
,
193 backing_format
, flags
);
194 if (drv
->bdrv_create
)
195 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
,
200 int bdrv_create(BlockDriver
*drv
,
201 const char *filename
, int64_t size_in_sectors
,
202 const char *backing_file
, int flags
)
204 if (!drv
->bdrv_create
)
206 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
210 void get_tmp_filename(char *filename
, int size
)
212 char temp_dir
[MAX_PATH
];
214 GetTempPath(MAX_PATH
, temp_dir
);
215 GetTempFileName(temp_dir
, "qem", 0, filename
);
218 void get_tmp_filename(char *filename
, int size
)
222 /* XXX: race condition possible */
223 tmpdir
= getenv("TMPDIR");
226 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
227 fd
= mkstemp(filename
);
233 static int is_windows_drive_prefix(const char *filename
)
235 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
236 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
240 static int is_windows_drive(const char *filename
)
242 if (is_windows_drive_prefix(filename
) &&
245 if (strstart(filename
, "\\\\.\\", NULL
) ||
246 strstart(filename
, "//./", NULL
))
252 static BlockDriver
*find_protocol(const char *filename
)
260 if (is_windows_drive(filename
) ||
261 is_windows_drive_prefix(filename
))
264 p
= strchr(filename
, ':');
268 if (len
> sizeof(protocol
) - 1)
269 len
= sizeof(protocol
) - 1;
270 memcpy(protocol
, filename
, len
);
271 protocol
[len
] = '\0';
272 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
273 if (drv1
->protocol_name
&&
274 !strcmp(drv1
->protocol_name
, protocol
))
280 /* XXX: force raw format if block or character device ? It would
281 simplify the BSD case */
282 static BlockDriver
*find_image_format(const char *filename
)
284 int ret
, score
, score_max
;
285 BlockDriver
*drv1
, *drv
;
287 BlockDriverState
*bs
;
289 /* detect host devices. By convention, /dev/cdrom[N] is always
290 recognized as a host CDROM */
291 if (strstart(filename
, "/dev/cdrom", NULL
))
292 return &bdrv_host_device
;
294 if (is_windows_drive(filename
))
295 return &bdrv_host_device
;
299 if (stat(filename
, &st
) >= 0 &&
300 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
301 return &bdrv_host_device
;
306 drv
= find_protocol(filename
);
307 /* no need to test disk image formats for vvfat */
308 if (drv
== &bdrv_vvfat
)
311 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
314 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
321 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
322 if (drv1
->bdrv_probe
) {
323 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
324 if (score
> score_max
) {
333 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
335 BlockDriverState
*bs
;
339 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
349 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
351 return bdrv_open2(bs
, filename
, flags
, NULL
);
354 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
358 char tmp_filename
[PATH_MAX
];
359 char backing_filename
[PATH_MAX
];
362 bs
->is_temporary
= 0;
365 /* buffer_alignment defaulted to 512, drivers can change this value */
366 bs
->buffer_alignment
= 512;
368 if (flags
& BDRV_O_SNAPSHOT
) {
369 BlockDriverState
*bs1
;
373 /* if snapshot, we create a temporary backing file and open it
374 instead of opening 'filename' directly */
376 /* if there is a backing file, use it */
378 ret
= bdrv_open2(bs1
, filename
, 0, drv
);
383 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
385 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
390 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
392 /* Real path is meaningless for protocols */
394 snprintf(backing_filename
, sizeof(backing_filename
),
397 realpath(filename
, backing_filename
);
399 ret
= bdrv_create2(&bdrv_qcow2
, tmp_filename
,
400 total_size
, backing_filename
,
401 (drv
? drv
->format_name
: NULL
), 0);
405 filename
= tmp_filename
;
407 bs
->is_temporary
= 1;
410 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
411 if (flags
& BDRV_O_FILE
) {
412 drv
= find_protocol(filename
);
414 drv
= find_image_format(filename
);
418 goto unlink_and_fail
;
421 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
422 /* Note: for compatibility, we open disk image files as RDWR, and
423 RDONLY as fallback */
424 if (!(flags
& BDRV_O_FILE
))
425 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
427 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
428 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
429 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
430 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
434 qemu_free(bs
->opaque
);
438 if (bs
->is_temporary
)
442 if (drv
->bdrv_getlength
) {
443 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
446 if (bs
->is_temporary
) {
450 if (bs
->backing_file
[0] != '\0') {
451 /* if there is a backing file, use it */
452 BlockDriver
*back_drv
= NULL
;
453 bs
->backing_hd
= bdrv_new("");
454 path_combine(backing_filename
, sizeof(backing_filename
),
455 filename
, bs
->backing_file
);
456 if (bs
->backing_format
[0] != '\0')
457 back_drv
= bdrv_find_format(bs
->backing_format
);
458 ret
= bdrv_open2(bs
->backing_hd
, backing_filename
, open_flags
,
466 if (!bdrv_key_required(bs
)) {
467 /* call the change callback */
468 bs
->media_changed
= 1;
470 bs
->change_cb(bs
->change_opaque
);
475 void bdrv_close(BlockDriverState
*bs
)
479 bdrv_delete(bs
->backing_hd
);
480 bs
->drv
->bdrv_close(bs
);
481 qemu_free(bs
->opaque
);
483 if (bs
->is_temporary
) {
484 unlink(bs
->filename
);
490 /* call the change callback */
491 bs
->media_changed
= 1;
493 bs
->change_cb(bs
->change_opaque
);
497 void bdrv_delete(BlockDriverState
*bs
)
499 BlockDriverState
**pbs
;
502 while (*pbs
!= bs
&& *pbs
!= NULL
)
512 * Run consistency checks on an image
514 * Returns the number of errors or -errno when an internal error occurs
516 int bdrv_check(BlockDriverState
*bs
)
518 if (bs
->drv
->bdrv_check
== NULL
) {
522 return bs
->drv
->bdrv_check(bs
);
525 /* commit COW file into the raw image */
526 int bdrv_commit(BlockDriverState
*bs
)
528 BlockDriver
*drv
= bs
->drv
;
529 int64_t i
, total_sectors
;
531 unsigned char sector
[512];
540 if (!bs
->backing_hd
) {
544 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
545 for (i
= 0; i
< total_sectors
;) {
546 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
547 for(j
= 0; j
< n
; j
++) {
548 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
552 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
562 if (drv
->bdrv_make_empty
)
563 return drv
->bdrv_make_empty(bs
);
568 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
573 if (!bdrv_is_inserted(bs
))
579 len
= bdrv_getlength(bs
);
581 if ((offset
+ size
) > len
)
587 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
590 return bdrv_check_byte_request(bs
, sector_num
* 512, nb_sectors
* 512);
593 /* return < 0 if error. See bdrv_write() for the return codes */
594 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
595 uint8_t *buf
, int nb_sectors
)
597 BlockDriver
*drv
= bs
->drv
;
601 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
604 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
607 /* Return < 0 if error. Important errors are:
608 -EIO generic I/O error (may happen for all errors)
609 -ENOMEDIUM No media inserted.
610 -EINVAL Invalid sector number or nb_sectors
611 -EACCES Trying to write a read-only device
613 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
614 const uint8_t *buf
, int nb_sectors
)
616 BlockDriver
*drv
= bs
->drv
;
621 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
624 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
627 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
628 void *buf
, int count1
)
630 uint8_t tmp_buf
[SECTOR_SIZE
];
631 int len
, nb_sectors
, count
;
635 /* first read to align to sector start */
636 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
639 sector_num
= offset
>> SECTOR_BITS
;
641 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
643 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
651 /* read the sectors "in place" */
652 nb_sectors
= count
>> SECTOR_BITS
;
653 if (nb_sectors
> 0) {
654 if (bdrv_read(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(buf
, tmp_buf
, count
);
671 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
672 const void *buf
, int count1
)
674 uint8_t tmp_buf
[SECTOR_SIZE
];
675 int len
, nb_sectors
, count
;
679 /* first write to align to sector start */
680 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
683 sector_num
= offset
>> SECTOR_BITS
;
685 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
687 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
688 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
697 /* write the sectors "in place" */
698 nb_sectors
= count
>> SECTOR_BITS
;
699 if (nb_sectors
> 0) {
700 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
702 sector_num
+= nb_sectors
;
703 len
= nb_sectors
<< SECTOR_BITS
;
708 /* add data from the last sector */
710 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
712 memcpy(tmp_buf
, buf
, count
);
713 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
720 * Truncate file to 'offset' bytes (needed only for file protocols)
722 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
724 BlockDriver
*drv
= bs
->drv
;
727 if (!drv
->bdrv_truncate
)
729 return drv
->bdrv_truncate(bs
, offset
);
733 * Length of a file in bytes. Return < 0 if error or unknown.
735 int64_t bdrv_getlength(BlockDriverState
*bs
)
737 BlockDriver
*drv
= bs
->drv
;
740 if (!drv
->bdrv_getlength
) {
742 return bs
->total_sectors
* SECTOR_SIZE
;
744 return drv
->bdrv_getlength(bs
);
747 /* return 0 as number of sectors if no device present or error */
748 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
751 length
= bdrv_getlength(bs
);
755 length
= length
>> SECTOR_BITS
;
756 *nb_sectors_ptr
= length
;
760 uint8_t boot_ind
; /* 0x80 - active */
761 uint8_t head
; /* starting head */
762 uint8_t sector
; /* starting sector */
763 uint8_t cyl
; /* starting cylinder */
764 uint8_t sys_ind
; /* What partition type */
765 uint8_t end_head
; /* end head */
766 uint8_t end_sector
; /* end sector */
767 uint8_t end_cyl
; /* end cylinder */
768 uint32_t start_sect
; /* starting sector counting from 0 */
769 uint32_t nr_sects
; /* nr of sectors in partition */
770 } __attribute__((packed
));
772 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
773 static int guess_disk_lchs(BlockDriverState
*bs
,
774 int *pcylinders
, int *pheads
, int *psectors
)
777 int ret
, i
, heads
, sectors
, cylinders
;
782 bdrv_get_geometry(bs
, &nb_sectors
);
784 ret
= bdrv_read(bs
, 0, buf
, 1);
787 /* test msdos magic */
788 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
790 for(i
= 0; i
< 4; i
++) {
791 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
792 nr_sects
= le32_to_cpu(p
->nr_sects
);
793 if (nr_sects
&& p
->end_head
) {
794 /* We make the assumption that the partition terminates on
795 a cylinder boundary */
796 heads
= p
->end_head
+ 1;
797 sectors
= p
->end_sector
& 63;
800 cylinders
= nb_sectors
/ (heads
* sectors
);
801 if (cylinders
< 1 || cylinders
> 16383)
805 *pcylinders
= cylinders
;
807 printf("guessed geometry: LCHS=%d %d %d\n",
808 cylinders
, heads
, sectors
);
816 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
818 int translation
, lba_detected
= 0;
819 int cylinders
, heads
, secs
;
822 /* if a geometry hint is available, use it */
823 bdrv_get_geometry(bs
, &nb_sectors
);
824 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
825 translation
= bdrv_get_translation_hint(bs
);
826 if (cylinders
!= 0) {
831 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
833 /* if heads > 16, it means that a BIOS LBA
834 translation was active, so the default
835 hardware geometry is OK */
837 goto default_geometry
;
842 /* disable any translation to be in sync with
843 the logical geometry */
844 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
845 bdrv_set_translation_hint(bs
,
846 BIOS_ATA_TRANSLATION_NONE
);
851 /* if no geometry, use a standard physical disk geometry */
852 cylinders
= nb_sectors
/ (16 * 63);
854 if (cylinders
> 16383)
856 else if (cylinders
< 2)
861 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
862 if ((*pcyls
* *pheads
) <= 131072) {
863 bdrv_set_translation_hint(bs
,
864 BIOS_ATA_TRANSLATION_LARGE
);
866 bdrv_set_translation_hint(bs
,
867 BIOS_ATA_TRANSLATION_LBA
);
871 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
875 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
876 int cyls
, int heads
, int secs
)
883 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
886 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
887 type
== BDRV_TYPE_FLOPPY
));
890 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
892 bs
->translation
= translation
;
895 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
896 int *pcyls
, int *pheads
, int *psecs
)
903 int bdrv_get_type_hint(BlockDriverState
*bs
)
908 int bdrv_get_translation_hint(BlockDriverState
*bs
)
910 return bs
->translation
;
913 int bdrv_is_removable(BlockDriverState
*bs
)
915 return bs
->removable
;
918 int bdrv_is_read_only(BlockDriverState
*bs
)
920 return bs
->read_only
;
923 int bdrv_is_sg(BlockDriverState
*bs
)
928 /* XXX: no longer used */
929 void bdrv_set_change_cb(BlockDriverState
*bs
,
930 void (*change_cb
)(void *opaque
), void *opaque
)
932 bs
->change_cb
= change_cb
;
933 bs
->change_opaque
= opaque
;
936 int bdrv_is_encrypted(BlockDriverState
*bs
)
938 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
940 return bs
->encrypted
;
943 int bdrv_key_required(BlockDriverState
*bs
)
945 BlockDriverState
*backing_hd
= bs
->backing_hd
;
947 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
949 return (bs
->encrypted
&& !bs
->valid_key
);
952 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
955 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
956 ret
= bdrv_set_key(bs
->backing_hd
, key
);
962 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
964 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
967 } else if (!bs
->valid_key
) {
969 /* call the change callback now, we skipped it on open */
970 bs
->media_changed
= 1;
972 bs
->change_cb(bs
->change_opaque
);
977 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
982 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
986 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
991 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
992 it(opaque
, drv
->format_name
);
996 BlockDriverState
*bdrv_find(const char *name
)
998 BlockDriverState
*bs
;
1000 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1001 if (!strcmp(name
, bs
->device_name
))
1007 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1009 BlockDriverState
*bs
;
1011 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1016 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1018 return bs
->device_name
;
1021 void bdrv_flush(BlockDriverState
*bs
)
1025 if (bs
->drv
->bdrv_flush
)
1026 bs
->drv
->bdrv_flush(bs
);
1028 bdrv_flush(bs
->backing_hd
);
1031 void bdrv_flush_all(void)
1033 BlockDriverState
*bs
;
1035 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1036 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1037 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1042 * Returns true iff the specified sector is present in the disk image. Drivers
1043 * not implementing the functionality are assumed to not support backing files,
1044 * hence all their sectors are reported as allocated.
1046 * 'pnum' is set to the number of sectors (including and immediately following
1047 * the specified sector) that are known to be in the same
1048 * allocated/unallocated state.
1050 * 'nb_sectors' is the max value 'pnum' should be set to.
1052 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1056 if (!bs
->drv
->bdrv_is_allocated
) {
1057 if (sector_num
>= bs
->total_sectors
) {
1061 n
= bs
->total_sectors
- sector_num
;
1062 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1065 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1068 void bdrv_info(Monitor
*mon
)
1070 BlockDriverState
*bs
;
1072 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1073 monitor_printf(mon
, "%s:", bs
->device_name
);
1074 monitor_printf(mon
, " type=");
1077 monitor_printf(mon
, "hd");
1079 case BDRV_TYPE_CDROM
:
1080 monitor_printf(mon
, "cdrom");
1082 case BDRV_TYPE_FLOPPY
:
1083 monitor_printf(mon
, "floppy");
1086 monitor_printf(mon
, " removable=%d", bs
->removable
);
1087 if (bs
->removable
) {
1088 monitor_printf(mon
, " locked=%d", bs
->locked
);
1091 monitor_printf(mon
, " file=");
1092 monitor_print_filename(mon
, bs
->filename
);
1093 if (bs
->backing_file
[0] != '\0') {
1094 monitor_printf(mon
, " backing_file=");
1095 monitor_print_filename(mon
, bs
->backing_file
);
1097 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1098 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1099 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1101 monitor_printf(mon
, " [not inserted]");
1103 monitor_printf(mon
, "\n");
1107 /* The "info blockstats" command. */
1108 void bdrv_info_stats(Monitor
*mon
)
1110 BlockDriverState
*bs
;
1112 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1113 monitor_printf(mon
, "%s:"
1114 " rd_bytes=%" PRIu64
1115 " wr_bytes=%" PRIu64
1116 " rd_operations=%" PRIu64
1117 " wr_operations=%" PRIu64
1120 bs
->rd_bytes
, bs
->wr_bytes
,
1121 bs
->rd_ops
, bs
->wr_ops
);
1125 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1127 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1128 return bs
->backing_file
;
1129 else if (bs
->encrypted
)
1130 return bs
->filename
;
1135 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1136 char *filename
, int filename_size
)
1138 if (!bs
->backing_hd
) {
1139 pstrcpy(filename
, filename_size
, "");
1141 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1145 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1146 const uint8_t *buf
, int nb_sectors
)
1148 BlockDriver
*drv
= bs
->drv
;
1151 if (!drv
->bdrv_write_compressed
)
1153 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1156 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1158 BlockDriver
*drv
= bs
->drv
;
1161 if (!drv
->bdrv_get_info
)
1163 memset(bdi
, 0, sizeof(*bdi
));
1164 return drv
->bdrv_get_info(bs
, bdi
);
1167 int bdrv_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
, int64_t pos
, int size
)
1169 BlockDriver
*drv
= bs
->drv
;
1172 if (!drv
->bdrv_put_buffer
)
1174 return drv
->bdrv_put_buffer(bs
, buf
, pos
, size
);
1177 int bdrv_get_buffer(BlockDriverState
*bs
, uint8_t *buf
, int64_t pos
, int size
)
1179 BlockDriver
*drv
= bs
->drv
;
1182 if (!drv
->bdrv_get_buffer
)
1184 return drv
->bdrv_get_buffer(bs
, buf
, pos
, size
);
1187 /**************************************************************/
1188 /* handling of snapshots */
1190 int bdrv_snapshot_create(BlockDriverState
*bs
,
1191 QEMUSnapshotInfo
*sn_info
)
1193 BlockDriver
*drv
= bs
->drv
;
1196 if (!drv
->bdrv_snapshot_create
)
1198 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1201 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1202 const char *snapshot_id
)
1204 BlockDriver
*drv
= bs
->drv
;
1207 if (!drv
->bdrv_snapshot_goto
)
1209 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1212 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1214 BlockDriver
*drv
= bs
->drv
;
1217 if (!drv
->bdrv_snapshot_delete
)
1219 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1222 int bdrv_snapshot_list(BlockDriverState
*bs
,
1223 QEMUSnapshotInfo
**psn_info
)
1225 BlockDriver
*drv
= bs
->drv
;
1228 if (!drv
->bdrv_snapshot_list
)
1230 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1233 #define NB_SUFFIXES 4
1235 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1237 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1242 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1245 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1246 if (size
< (10 * base
)) {
1247 snprintf(buf
, buf_size
, "%0.1f%c",
1248 (double)size
/ base
,
1251 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1252 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1253 ((size
+ (base
>> 1)) / base
),
1263 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1265 char buf1
[128], date_buf
[128], clock_buf
[128];
1275 snprintf(buf
, buf_size
,
1276 "%-10s%-20s%7s%20s%15s",
1277 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1281 ptm
= localtime(&ti
);
1282 strftime(date_buf
, sizeof(date_buf
),
1283 "%Y-%m-%d %H:%M:%S", ptm
);
1285 localtime_r(&ti
, &tm
);
1286 strftime(date_buf
, sizeof(date_buf
),
1287 "%Y-%m-%d %H:%M:%S", &tm
);
1289 secs
= sn
->vm_clock_nsec
/ 1000000000;
1290 snprintf(clock_buf
, sizeof(clock_buf
),
1291 "%02d:%02d:%02d.%03d",
1293 (int)((secs
/ 60) % 60),
1295 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1296 snprintf(buf
, buf_size
,
1297 "%-10s%-20s%7s%20s%15s",
1298 sn
->id_str
, sn
->name
,
1299 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1307 /**************************************************************/
1310 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1311 QEMUIOVector
*qiov
, int nb_sectors
,
1312 BlockDriverCompletionFunc
*cb
, void *opaque
)
1314 BlockDriver
*drv
= bs
->drv
;
1315 BlockDriverAIOCB
*ret
;
1319 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1322 ret
= drv
->bdrv_aio_readv(bs
, sector_num
, qiov
, nb_sectors
,
1326 /* Update stats even though technically transfer has not happened. */
1327 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1334 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1335 QEMUIOVector
*qiov
, int nb_sectors
,
1336 BlockDriverCompletionFunc
*cb
, void *opaque
)
1338 BlockDriver
*drv
= bs
->drv
;
1339 BlockDriverAIOCB
*ret
;
1345 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1348 ret
= drv
->bdrv_aio_writev(bs
, sector_num
, qiov
, nb_sectors
,
1352 /* Update stats even though technically transfer has not happened. */
1353 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1360 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1362 acb
->pool
->cancel(acb
);
1366 /**************************************************************/
1367 /* async block device emulation */
1369 static void bdrv_aio_bh_cb(void *opaque
)
1371 BlockDriverAIOCBSync
*acb
= opaque
;
1374 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
1375 qemu_vfree(acb
->bounce
);
1376 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1378 qemu_aio_release(acb
);
1381 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1385 BlockDriverCompletionFunc
*cb
,
1390 BlockDriverAIOCBSync
*acb
;
1392 acb
= qemu_aio_get(bs
, cb
, opaque
);
1393 acb
->is_write
= is_write
;
1395 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
1398 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1401 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
1402 acb
->ret
= bdrv_write(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1404 acb
->ret
= bdrv_read(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1407 qemu_bh_schedule(acb
->bh
);
1409 return &acb
->common
;
1412 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
1413 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1414 BlockDriverCompletionFunc
*cb
, void *opaque
)
1416 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
1419 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
1420 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1421 BlockDriverCompletionFunc
*cb
, void *opaque
)
1423 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
1427 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1429 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1430 qemu_bh_cancel(acb
->bh
);
1431 qemu_aio_release(acb
);
1434 /**************************************************************/
1435 /* sync block device emulation */
1437 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1439 *(int *)opaque
= ret
;
1442 #define NOT_DONE 0x7fffffff
1444 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1445 uint8_t *buf
, int nb_sectors
)
1448 BlockDriverAIOCB
*acb
;
1452 async_ret
= NOT_DONE
;
1453 iov
.iov_base
= (void *)buf
;
1454 iov
.iov_len
= nb_sectors
* 512;
1455 qemu_iovec_init_external(&qiov
, &iov
, 1);
1456 acb
= bdrv_aio_readv(bs
, sector_num
, &qiov
, nb_sectors
,
1457 bdrv_rw_em_cb
, &async_ret
);
1461 while (async_ret
== NOT_DONE
) {
1468 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1469 const uint8_t *buf
, int nb_sectors
)
1472 BlockDriverAIOCB
*acb
;
1476 async_ret
= NOT_DONE
;
1477 iov
.iov_base
= (void *)buf
;
1478 iov
.iov_len
= nb_sectors
* 512;
1479 qemu_iovec_init_external(&qiov
, &iov
, 1);
1480 acb
= bdrv_aio_writev(bs
, sector_num
, &qiov
, nb_sectors
,
1481 bdrv_rw_em_cb
, &async_ret
);
1484 while (async_ret
== NOT_DONE
) {
1490 void bdrv_init(void)
1492 bdrv_register(&bdrv_raw
);
1493 bdrv_register(&bdrv_host_device
);
1495 bdrv_register(&bdrv_cow
);
1497 bdrv_register(&bdrv_qcow
);
1498 bdrv_register(&bdrv_vmdk
);
1499 bdrv_register(&bdrv_cloop
);
1500 bdrv_register(&bdrv_dmg
);
1501 bdrv_register(&bdrv_bochs
);
1502 bdrv_register(&bdrv_vpc
);
1503 bdrv_register(&bdrv_vvfat
);
1504 bdrv_register(&bdrv_qcow2
);
1505 bdrv_register(&bdrv_parallels
);
1506 bdrv_register(&bdrv_nbd
);
1509 void aio_pool_init(AIOPool
*pool
, int aiocb_size
,
1510 void (*cancel
)(BlockDriverAIOCB
*acb
))
1512 pool
->aiocb_size
= aiocb_size
;
1513 pool
->cancel
= cancel
;
1514 pool
->free_aiocb
= NULL
;
1517 void *qemu_aio_get_pool(AIOPool
*pool
, BlockDriverState
*bs
,
1518 BlockDriverCompletionFunc
*cb
, void *opaque
)
1520 BlockDriverAIOCB
*acb
;
1522 if (pool
->free_aiocb
) {
1523 acb
= pool
->free_aiocb
;
1524 pool
->free_aiocb
= acb
->next
;
1526 acb
= qemu_mallocz(pool
->aiocb_size
);
1531 acb
->opaque
= opaque
;
1535 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1538 return qemu_aio_get_pool(&bs
->drv
->aio_pool
, bs
, cb
, opaque
);
1541 void qemu_aio_release(void *p
)
1543 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1544 AIOPool
*pool
= acb
->pool
;
1545 acb
->next
= pool
->free_aiocb
;
1546 pool
->free_aiocb
= acb
;
1549 /**************************************************************/
1550 /* removable device support */
1553 * Return TRUE if the media is present
1555 int bdrv_is_inserted(BlockDriverState
*bs
)
1557 BlockDriver
*drv
= bs
->drv
;
1561 if (!drv
->bdrv_is_inserted
)
1563 ret
= drv
->bdrv_is_inserted(bs
);
1568 * Return TRUE if the media changed since the last call to this
1569 * function. It is currently only used for floppy disks
1571 int bdrv_media_changed(BlockDriverState
*bs
)
1573 BlockDriver
*drv
= bs
->drv
;
1576 if (!drv
|| !drv
->bdrv_media_changed
)
1579 ret
= drv
->bdrv_media_changed(bs
);
1580 if (ret
== -ENOTSUP
)
1581 ret
= bs
->media_changed
;
1582 bs
->media_changed
= 0;
1587 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1589 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1591 BlockDriver
*drv
= bs
->drv
;
1594 if (!drv
|| !drv
->bdrv_eject
) {
1597 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1599 if (ret
== -ENOTSUP
) {
1605 int bdrv_is_locked(BlockDriverState
*bs
)
1611 * Lock or unlock the media (if it is locked, the user won't be able
1612 * to eject it manually).
1614 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1616 BlockDriver
*drv
= bs
->drv
;
1618 bs
->locked
= locked
;
1619 if (drv
&& drv
->bdrv_set_locked
) {
1620 drv
->bdrv_set_locked(bs
, locked
);
1624 /* needed for generic scsi interface */
1626 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1628 BlockDriver
*drv
= bs
->drv
;
1630 if (drv
&& drv
->bdrv_ioctl
)
1631 return drv
->bdrv_ioctl(bs
, req
, buf
);
1635 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
1636 unsigned long int req
, void *buf
,
1637 BlockDriverCompletionFunc
*cb
, void *opaque
)
1639 BlockDriver
*drv
= bs
->drv
;
1641 if (drv
&& drv
->bdrv_aio_ioctl
)
1642 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);
1646 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
1648 return qemu_memalign((bs
&& bs
->buffer_alignment
) ? bs
->buffer_alignment
: 512, size
);