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"
36 #include <sys/types.h>
38 #include <sys/ioctl.h>
49 #define SECTOR_SIZE (1 << SECTOR_BITS)
51 typedef struct BlockDriverAIOCBSync
{
52 BlockDriverAIOCB common
;
55 /* vector translation state */
59 } BlockDriverAIOCBSync
;
61 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
62 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
63 BlockDriverCompletionFunc
*cb
, void *opaque
);
64 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
65 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
66 BlockDriverCompletionFunc
*cb
, void *opaque
);
67 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
68 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
69 uint8_t *buf
, int nb_sectors
);
70 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
71 const uint8_t *buf
, int nb_sectors
);
73 BlockDriverState
*bdrv_first
;
75 static BlockDriver
*first_drv
;
77 int path_is_absolute(const char *path
)
81 /* specific case for names like: "\\.\d:" */
82 if (*path
== '/' || *path
== '\\')
85 p
= strchr(path
, ':');
91 return (*p
== '/' || *p
== '\\');
97 /* if filename is absolute, just copy it to dest. Otherwise, build a
98 path to it by considering it is relative to base_path. URL are
100 void path_combine(char *dest
, int dest_size
,
101 const char *base_path
,
102 const char *filename
)
109 if (path_is_absolute(filename
)) {
110 pstrcpy(dest
, dest_size
, filename
);
112 p
= strchr(base_path
, ':');
117 p1
= strrchr(base_path
, '/');
121 p2
= strrchr(base_path
, '\\');
133 if (len
> dest_size
- 1)
135 memcpy(dest
, base_path
, len
);
137 pstrcat(dest
, dest_size
, filename
);
142 void bdrv_register(BlockDriver
*bdrv
)
144 if (!bdrv
->bdrv_aio_readv
) {
145 /* add AIO emulation layer */
146 bdrv
->bdrv_aio_readv
= bdrv_aio_readv_em
;
147 bdrv
->bdrv_aio_writev
= bdrv_aio_writev_em
;
148 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
149 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
150 } else if (!bdrv
->bdrv_read
) {
151 /* add synchronous IO emulation layer */
152 bdrv
->bdrv_read
= bdrv_read_em
;
153 bdrv
->bdrv_write
= bdrv_write_em
;
155 aio_pool_init(&bdrv
->aio_pool
, bdrv
->aiocb_size
, bdrv
->bdrv_aio_cancel
);
156 bdrv
->next
= first_drv
;
160 /* create a new block device (by default it is empty) */
161 BlockDriverState
*bdrv_new(const char *device_name
)
163 BlockDriverState
**pbs
, *bs
;
165 bs
= qemu_mallocz(sizeof(BlockDriverState
));
166 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
167 if (device_name
[0] != '\0') {
168 /* insert at the end */
177 BlockDriver
*bdrv_find_format(const char *format_name
)
180 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
181 if (!strcmp(drv1
->format_name
, format_name
))
187 int bdrv_create(BlockDriver
*drv
, const char* filename
,
188 QEMUOptionParameter
*options
)
190 if (!drv
->bdrv_create
)
193 return drv
->bdrv_create(filename
, options
);
197 void get_tmp_filename(char *filename
, int size
)
199 char temp_dir
[MAX_PATH
];
201 GetTempPath(MAX_PATH
, temp_dir
);
202 GetTempFileName(temp_dir
, "qem", 0, filename
);
205 void get_tmp_filename(char *filename
, int size
)
209 /* XXX: race condition possible */
210 tmpdir
= getenv("TMPDIR");
213 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
214 fd
= mkstemp(filename
);
220 static int is_windows_drive_prefix(const char *filename
)
222 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
223 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
227 static int is_windows_drive(const char *filename
)
229 if (is_windows_drive_prefix(filename
) &&
232 if (strstart(filename
, "\\\\.\\", NULL
) ||
233 strstart(filename
, "//./", NULL
))
239 static BlockDriver
*find_protocol(const char *filename
)
247 if (is_windows_drive(filename
) ||
248 is_windows_drive_prefix(filename
))
249 return bdrv_find_format("raw");
251 p
= strchr(filename
, ':');
253 return bdrv_find_format("raw");
255 if (len
> sizeof(protocol
) - 1)
256 len
= sizeof(protocol
) - 1;
257 memcpy(protocol
, filename
, len
);
258 protocol
[len
] = '\0';
259 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
260 if (drv1
->protocol_name
&&
261 !strcmp(drv1
->protocol_name
, protocol
))
267 /* XXX: force raw format if block or character device ? It would
268 simplify the BSD case */
269 static BlockDriver
*find_image_format(const char *filename
)
271 int ret
, score
, score_max
;
272 BlockDriver
*drv1
, *drv
;
274 BlockDriverState
*bs
;
276 /* detect host devices. By convention, /dev/cdrom[N] is always
277 recognized as a host CDROM */
278 if (strstart(filename
, "/dev/cdrom", NULL
))
279 return bdrv_find_format("host_device");
281 if (is_windows_drive(filename
))
282 return bdrv_find_format("host_device");
286 if (stat(filename
, &st
) >= 0 &&
287 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
288 return bdrv_find_format("host_device");
293 drv
= find_protocol(filename
);
294 /* no need to test disk image formats for vvfat */
295 if (drv
&& strcmp(drv
->format_name
, "vvfat") == 0)
298 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
301 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
308 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
309 if (drv1
->bdrv_probe
) {
310 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
311 if (score
> score_max
) {
320 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
322 BlockDriverState
*bs
;
326 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
336 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
338 return bdrv_open2(bs
, filename
, flags
, NULL
);
341 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
345 char tmp_filename
[PATH_MAX
];
346 char backing_filename
[PATH_MAX
];
349 bs
->is_temporary
= 0;
352 /* buffer_alignment defaulted to 512, drivers can change this value */
353 bs
->buffer_alignment
= 512;
355 if (flags
& BDRV_O_SNAPSHOT
) {
356 BlockDriverState
*bs1
;
359 BlockDriver
*bdrv_qcow2
;
360 QEMUOptionParameter
*options
;
362 /* if snapshot, we create a temporary backing file and open it
363 instead of opening 'filename' directly */
365 /* if there is a backing file, use it */
367 ret
= bdrv_open2(bs1
, filename
, 0, drv
);
372 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
374 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
379 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
381 /* Real path is meaningless for protocols */
383 snprintf(backing_filename
, sizeof(backing_filename
),
386 realpath(filename
, backing_filename
);
388 bdrv_qcow2
= bdrv_find_format("qcow2");
389 options
= parse_option_parameters("", bdrv_qcow2
->create_options
, NULL
);
391 set_option_parameter_int(options
, BLOCK_OPT_SIZE
, total_size
* 512);
392 set_option_parameter(options
, BLOCK_OPT_BACKING_FILE
, backing_filename
);
394 set_option_parameter(options
, BLOCK_OPT_BACKING_FMT
,
398 ret
= bdrv_create(bdrv_qcow2
, tmp_filename
, options
);
403 filename
= tmp_filename
;
405 bs
->is_temporary
= 1;
408 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
409 if (flags
& BDRV_O_FILE
) {
410 drv
= find_protocol(filename
);
412 drv
= find_image_format(filename
);
416 goto unlink_and_fail
;
419 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
420 /* Note: for compatibility, we open disk image files as RDWR, and
421 RDONLY as fallback */
422 if (!(flags
& BDRV_O_FILE
))
423 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
425 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
426 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
427 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
428 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
432 qemu_free(bs
->opaque
);
436 if (bs
->is_temporary
)
440 if (drv
->bdrv_getlength
) {
441 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
444 if (bs
->is_temporary
) {
448 if (bs
->backing_file
[0] != '\0') {
449 /* if there is a backing file, use it */
450 BlockDriver
*back_drv
= NULL
;
451 bs
->backing_hd
= bdrv_new("");
452 path_combine(backing_filename
, sizeof(backing_filename
),
453 filename
, bs
->backing_file
);
454 if (bs
->backing_format
[0] != '\0')
455 back_drv
= bdrv_find_format(bs
->backing_format
);
456 ret
= bdrv_open2(bs
->backing_hd
, backing_filename
, open_flags
,
464 if (!bdrv_key_required(bs
)) {
465 /* call the change callback */
466 bs
->media_changed
= 1;
468 bs
->change_cb(bs
->change_opaque
);
473 void bdrv_close(BlockDriverState
*bs
)
477 bdrv_delete(bs
->backing_hd
);
478 bs
->drv
->bdrv_close(bs
);
479 qemu_free(bs
->opaque
);
481 if (bs
->is_temporary
) {
482 unlink(bs
->filename
);
488 /* call the change callback */
489 bs
->media_changed
= 1;
491 bs
->change_cb(bs
->change_opaque
);
495 void bdrv_delete(BlockDriverState
*bs
)
497 BlockDriverState
**pbs
;
500 while (*pbs
!= bs
&& *pbs
!= NULL
)
510 * Run consistency checks on an image
512 * Returns the number of errors or -errno when an internal error occurs
514 int bdrv_check(BlockDriverState
*bs
)
516 if (bs
->drv
->bdrv_check
== NULL
) {
520 return bs
->drv
->bdrv_check(bs
);
523 /* commit COW file into the raw image */
524 int bdrv_commit(BlockDriverState
*bs
)
526 BlockDriver
*drv
= bs
->drv
;
527 int64_t i
, total_sectors
;
529 unsigned char sector
[512];
538 if (!bs
->backing_hd
) {
542 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
543 for (i
= 0; i
< total_sectors
;) {
544 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
545 for(j
= 0; j
< n
; j
++) {
546 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
550 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
560 if (drv
->bdrv_make_empty
)
561 return drv
->bdrv_make_empty(bs
);
566 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
571 if (!bdrv_is_inserted(bs
))
577 len
= bdrv_getlength(bs
);
582 if ((offset
> len
) || (len
- offset
< size
))
588 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
591 return bdrv_check_byte_request(bs
, sector_num
* 512, nb_sectors
* 512);
594 /* return < 0 if error. See bdrv_write() for the return codes */
595 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
596 uint8_t *buf
, int nb_sectors
)
598 BlockDriver
*drv
= bs
->drv
;
602 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
605 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
608 /* Return < 0 if error. Important errors are:
609 -EIO generic I/O error (may happen for all errors)
610 -ENOMEDIUM No media inserted.
611 -EINVAL Invalid sector number or nb_sectors
612 -EACCES Trying to write a read-only device
614 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
615 const uint8_t *buf
, int nb_sectors
)
617 BlockDriver
*drv
= bs
->drv
;
622 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
625 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
628 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
629 void *buf
, int count1
)
631 uint8_t tmp_buf
[SECTOR_SIZE
];
632 int len
, nb_sectors
, count
;
636 /* first read to align to sector start */
637 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
640 sector_num
= offset
>> SECTOR_BITS
;
642 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
644 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
652 /* read the sectors "in place" */
653 nb_sectors
= count
>> SECTOR_BITS
;
654 if (nb_sectors
> 0) {
655 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
657 sector_num
+= nb_sectors
;
658 len
= nb_sectors
<< SECTOR_BITS
;
663 /* add data from the last sector */
665 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
667 memcpy(buf
, tmp_buf
, count
);
672 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
673 const void *buf
, int count1
)
675 uint8_t tmp_buf
[SECTOR_SIZE
];
676 int len
, nb_sectors
, count
;
680 /* first write to align to sector start */
681 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
684 sector_num
= offset
>> SECTOR_BITS
;
686 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
688 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
689 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
698 /* write the sectors "in place" */
699 nb_sectors
= count
>> SECTOR_BITS
;
700 if (nb_sectors
> 0) {
701 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
703 sector_num
+= nb_sectors
;
704 len
= nb_sectors
<< SECTOR_BITS
;
709 /* add data from the last sector */
711 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
713 memcpy(tmp_buf
, buf
, count
);
714 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
721 * Truncate file to 'offset' bytes (needed only for file protocols)
723 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
725 BlockDriver
*drv
= bs
->drv
;
728 if (!drv
->bdrv_truncate
)
730 return drv
->bdrv_truncate(bs
, offset
);
734 * Length of a file in bytes. Return < 0 if error or unknown.
736 int64_t bdrv_getlength(BlockDriverState
*bs
)
738 BlockDriver
*drv
= bs
->drv
;
741 if (!drv
->bdrv_getlength
) {
743 return bs
->total_sectors
* SECTOR_SIZE
;
745 return drv
->bdrv_getlength(bs
);
748 /* return 0 as number of sectors if no device present or error */
749 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
752 length
= bdrv_getlength(bs
);
756 length
= length
>> SECTOR_BITS
;
757 *nb_sectors_ptr
= length
;
761 uint8_t boot_ind
; /* 0x80 - active */
762 uint8_t head
; /* starting head */
763 uint8_t sector
; /* starting sector */
764 uint8_t cyl
; /* starting cylinder */
765 uint8_t sys_ind
; /* What partition type */
766 uint8_t end_head
; /* end head */
767 uint8_t end_sector
; /* end sector */
768 uint8_t end_cyl
; /* end cylinder */
769 uint32_t start_sect
; /* starting sector counting from 0 */
770 uint32_t nr_sects
; /* nr of sectors in partition */
771 } __attribute__((packed
));
773 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
774 static int guess_disk_lchs(BlockDriverState
*bs
,
775 int *pcylinders
, int *pheads
, int *psectors
)
778 int ret
, i
, heads
, sectors
, cylinders
;
783 bdrv_get_geometry(bs
, &nb_sectors
);
785 ret
= bdrv_read(bs
, 0, buf
, 1);
788 /* test msdos magic */
789 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
791 for(i
= 0; i
< 4; i
++) {
792 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
793 nr_sects
= le32_to_cpu(p
->nr_sects
);
794 if (nr_sects
&& p
->end_head
) {
795 /* We make the assumption that the partition terminates on
796 a cylinder boundary */
797 heads
= p
->end_head
+ 1;
798 sectors
= p
->end_sector
& 63;
801 cylinders
= nb_sectors
/ (heads
* sectors
);
802 if (cylinders
< 1 || cylinders
> 16383)
806 *pcylinders
= cylinders
;
808 printf("guessed geometry: LCHS=%d %d %d\n",
809 cylinders
, heads
, sectors
);
817 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
819 int translation
, lba_detected
= 0;
820 int cylinders
, heads
, secs
;
823 /* if a geometry hint is available, use it */
824 bdrv_get_geometry(bs
, &nb_sectors
);
825 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
826 translation
= bdrv_get_translation_hint(bs
);
827 if (cylinders
!= 0) {
832 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
834 /* if heads > 16, it means that a BIOS LBA
835 translation was active, so the default
836 hardware geometry is OK */
838 goto default_geometry
;
843 /* disable any translation to be in sync with
844 the logical geometry */
845 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
846 bdrv_set_translation_hint(bs
,
847 BIOS_ATA_TRANSLATION_NONE
);
852 /* if no geometry, use a standard physical disk geometry */
853 cylinders
= nb_sectors
/ (16 * 63);
855 if (cylinders
> 16383)
857 else if (cylinders
< 2)
862 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
863 if ((*pcyls
* *pheads
) <= 131072) {
864 bdrv_set_translation_hint(bs
,
865 BIOS_ATA_TRANSLATION_LARGE
);
867 bdrv_set_translation_hint(bs
,
868 BIOS_ATA_TRANSLATION_LBA
);
872 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
876 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
877 int cyls
, int heads
, int secs
)
884 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
887 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
888 type
== BDRV_TYPE_FLOPPY
));
891 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
893 bs
->translation
= translation
;
896 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
897 int *pcyls
, int *pheads
, int *psecs
)
904 int bdrv_get_type_hint(BlockDriverState
*bs
)
909 int bdrv_get_translation_hint(BlockDriverState
*bs
)
911 return bs
->translation
;
914 int bdrv_is_removable(BlockDriverState
*bs
)
916 return bs
->removable
;
919 int bdrv_is_read_only(BlockDriverState
*bs
)
921 return bs
->read_only
;
924 int bdrv_is_sg(BlockDriverState
*bs
)
929 /* XXX: no longer used */
930 void bdrv_set_change_cb(BlockDriverState
*bs
,
931 void (*change_cb
)(void *opaque
), void *opaque
)
933 bs
->change_cb
= change_cb
;
934 bs
->change_opaque
= opaque
;
937 int bdrv_is_encrypted(BlockDriverState
*bs
)
939 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
941 return bs
->encrypted
;
944 int bdrv_key_required(BlockDriverState
*bs
)
946 BlockDriverState
*backing_hd
= bs
->backing_hd
;
948 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
950 return (bs
->encrypted
&& !bs
->valid_key
);
953 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
956 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
957 ret
= bdrv_set_key(bs
->backing_hd
, key
);
963 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
965 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
968 } else if (!bs
->valid_key
) {
970 /* call the change callback now, we skipped it on open */
971 bs
->media_changed
= 1;
973 bs
->change_cb(bs
->change_opaque
);
978 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
983 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
987 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
992 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
993 it(opaque
, drv
->format_name
);
997 BlockDriverState
*bdrv_find(const char *name
)
999 BlockDriverState
*bs
;
1001 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1002 if (!strcmp(name
, bs
->device_name
))
1008 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1010 BlockDriverState
*bs
;
1012 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1017 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1019 return bs
->device_name
;
1022 void bdrv_flush(BlockDriverState
*bs
)
1026 if (bs
->drv
->bdrv_flush
)
1027 bs
->drv
->bdrv_flush(bs
);
1029 bdrv_flush(bs
->backing_hd
);
1032 void bdrv_flush_all(void)
1034 BlockDriverState
*bs
;
1036 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1037 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1038 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1043 * Returns true iff the specified sector is present in the disk image. Drivers
1044 * not implementing the functionality are assumed to not support backing files,
1045 * hence all their sectors are reported as allocated.
1047 * 'pnum' is set to the number of sectors (including and immediately following
1048 * the specified sector) that are known to be in the same
1049 * allocated/unallocated state.
1051 * 'nb_sectors' is the max value 'pnum' should be set to.
1053 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1057 if (!bs
->drv
->bdrv_is_allocated
) {
1058 if (sector_num
>= bs
->total_sectors
) {
1062 n
= bs
->total_sectors
- sector_num
;
1063 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1066 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1069 void bdrv_info(Monitor
*mon
)
1071 BlockDriverState
*bs
;
1073 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1074 monitor_printf(mon
, "%s:", bs
->device_name
);
1075 monitor_printf(mon
, " type=");
1078 monitor_printf(mon
, "hd");
1080 case BDRV_TYPE_CDROM
:
1081 monitor_printf(mon
, "cdrom");
1083 case BDRV_TYPE_FLOPPY
:
1084 monitor_printf(mon
, "floppy");
1087 monitor_printf(mon
, " removable=%d", bs
->removable
);
1088 if (bs
->removable
) {
1089 monitor_printf(mon
, " locked=%d", bs
->locked
);
1092 monitor_printf(mon
, " file=");
1093 monitor_print_filename(mon
, bs
->filename
);
1094 if (bs
->backing_file
[0] != '\0') {
1095 monitor_printf(mon
, " backing_file=");
1096 monitor_print_filename(mon
, bs
->backing_file
);
1098 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1099 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1100 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1102 monitor_printf(mon
, " [not inserted]");
1104 monitor_printf(mon
, "\n");
1108 /* The "info blockstats" command. */
1109 void bdrv_info_stats(Monitor
*mon
)
1111 BlockDriverState
*bs
;
1113 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1114 monitor_printf(mon
, "%s:"
1115 " rd_bytes=%" PRIu64
1116 " wr_bytes=%" PRIu64
1117 " rd_operations=%" PRIu64
1118 " wr_operations=%" PRIu64
1121 bs
->rd_bytes
, bs
->wr_bytes
,
1122 bs
->rd_ops
, bs
->wr_ops
);
1126 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1128 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1129 return bs
->backing_file
;
1130 else if (bs
->encrypted
)
1131 return bs
->filename
;
1136 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1137 char *filename
, int filename_size
)
1139 if (!bs
->backing_hd
) {
1140 pstrcpy(filename
, filename_size
, "");
1142 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1146 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1147 const uint8_t *buf
, int nb_sectors
)
1149 BlockDriver
*drv
= bs
->drv
;
1152 if (!drv
->bdrv_write_compressed
)
1154 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1156 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1159 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1161 BlockDriver
*drv
= bs
->drv
;
1164 if (!drv
->bdrv_get_info
)
1166 memset(bdi
, 0, sizeof(*bdi
));
1167 return drv
->bdrv_get_info(bs
, bdi
);
1170 int bdrv_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
, int64_t pos
, int size
)
1172 BlockDriver
*drv
= bs
->drv
;
1175 if (!drv
->bdrv_put_buffer
)
1177 return drv
->bdrv_put_buffer(bs
, buf
, pos
, size
);
1180 int bdrv_get_buffer(BlockDriverState
*bs
, uint8_t *buf
, int64_t pos
, int size
)
1182 BlockDriver
*drv
= bs
->drv
;
1185 if (!drv
->bdrv_get_buffer
)
1187 return drv
->bdrv_get_buffer(bs
, buf
, pos
, size
);
1190 /**************************************************************/
1191 /* handling of snapshots */
1193 int bdrv_snapshot_create(BlockDriverState
*bs
,
1194 QEMUSnapshotInfo
*sn_info
)
1196 BlockDriver
*drv
= bs
->drv
;
1199 if (!drv
->bdrv_snapshot_create
)
1201 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1204 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1205 const char *snapshot_id
)
1207 BlockDriver
*drv
= bs
->drv
;
1210 if (!drv
->bdrv_snapshot_goto
)
1212 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1215 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1217 BlockDriver
*drv
= bs
->drv
;
1220 if (!drv
->bdrv_snapshot_delete
)
1222 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1225 int bdrv_snapshot_list(BlockDriverState
*bs
,
1226 QEMUSnapshotInfo
**psn_info
)
1228 BlockDriver
*drv
= bs
->drv
;
1231 if (!drv
->bdrv_snapshot_list
)
1233 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1236 #define NB_SUFFIXES 4
1238 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1240 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1245 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1248 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1249 if (size
< (10 * base
)) {
1250 snprintf(buf
, buf_size
, "%0.1f%c",
1251 (double)size
/ base
,
1254 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1255 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1256 ((size
+ (base
>> 1)) / base
),
1266 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1268 char buf1
[128], date_buf
[128], clock_buf
[128];
1278 snprintf(buf
, buf_size
,
1279 "%-10s%-20s%7s%20s%15s",
1280 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1284 ptm
= localtime(&ti
);
1285 strftime(date_buf
, sizeof(date_buf
),
1286 "%Y-%m-%d %H:%M:%S", ptm
);
1288 localtime_r(&ti
, &tm
);
1289 strftime(date_buf
, sizeof(date_buf
),
1290 "%Y-%m-%d %H:%M:%S", &tm
);
1292 secs
= sn
->vm_clock_nsec
/ 1000000000;
1293 snprintf(clock_buf
, sizeof(clock_buf
),
1294 "%02d:%02d:%02d.%03d",
1296 (int)((secs
/ 60) % 60),
1298 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1299 snprintf(buf
, buf_size
,
1300 "%-10s%-20s%7s%20s%15s",
1301 sn
->id_str
, sn
->name
,
1302 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1310 /**************************************************************/
1313 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1314 QEMUIOVector
*qiov
, int nb_sectors
,
1315 BlockDriverCompletionFunc
*cb
, void *opaque
)
1317 BlockDriver
*drv
= bs
->drv
;
1318 BlockDriverAIOCB
*ret
;
1322 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1325 ret
= drv
->bdrv_aio_readv(bs
, sector_num
, qiov
, nb_sectors
,
1329 /* Update stats even though technically transfer has not happened. */
1330 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1337 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1338 QEMUIOVector
*qiov
, int nb_sectors
,
1339 BlockDriverCompletionFunc
*cb
, void *opaque
)
1341 BlockDriver
*drv
= bs
->drv
;
1342 BlockDriverAIOCB
*ret
;
1348 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1351 ret
= drv
->bdrv_aio_writev(bs
, sector_num
, qiov
, nb_sectors
,
1355 /* Update stats even though technically transfer has not happened. */
1356 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1363 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1365 acb
->pool
->cancel(acb
);
1369 /**************************************************************/
1370 /* async block device emulation */
1372 static void bdrv_aio_bh_cb(void *opaque
)
1374 BlockDriverAIOCBSync
*acb
= opaque
;
1377 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
1378 qemu_vfree(acb
->bounce
);
1379 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1381 qemu_aio_release(acb
);
1384 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1388 BlockDriverCompletionFunc
*cb
,
1393 BlockDriverAIOCBSync
*acb
;
1395 acb
= qemu_aio_get(bs
, cb
, opaque
);
1396 acb
->is_write
= is_write
;
1398 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
1401 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1404 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
1405 acb
->ret
= bdrv_write(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1407 acb
->ret
= bdrv_read(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1410 qemu_bh_schedule(acb
->bh
);
1412 return &acb
->common
;
1415 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
1416 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1417 BlockDriverCompletionFunc
*cb
, void *opaque
)
1419 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
1422 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
1423 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1424 BlockDriverCompletionFunc
*cb
, void *opaque
)
1426 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
1430 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1432 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1433 qemu_bh_cancel(acb
->bh
);
1434 qemu_aio_release(acb
);
1437 /**************************************************************/
1438 /* sync block device emulation */
1440 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1442 *(int *)opaque
= ret
;
1445 #define NOT_DONE 0x7fffffff
1447 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1448 uint8_t *buf
, int nb_sectors
)
1451 BlockDriverAIOCB
*acb
;
1455 async_ret
= NOT_DONE
;
1456 iov
.iov_base
= (void *)buf
;
1457 iov
.iov_len
= nb_sectors
* 512;
1458 qemu_iovec_init_external(&qiov
, &iov
, 1);
1459 acb
= bdrv_aio_readv(bs
, sector_num
, &qiov
, nb_sectors
,
1460 bdrv_rw_em_cb
, &async_ret
);
1464 while (async_ret
== NOT_DONE
) {
1471 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1472 const uint8_t *buf
, int nb_sectors
)
1475 BlockDriverAIOCB
*acb
;
1479 async_ret
= NOT_DONE
;
1480 iov
.iov_base
= (void *)buf
;
1481 iov
.iov_len
= nb_sectors
* 512;
1482 qemu_iovec_init_external(&qiov
, &iov
, 1);
1483 acb
= bdrv_aio_writev(bs
, sector_num
, &qiov
, nb_sectors
,
1484 bdrv_rw_em_cb
, &async_ret
);
1487 while (async_ret
== NOT_DONE
) {
1493 void bdrv_init(void)
1495 module_call_init(MODULE_INIT_BLOCK
);
1498 void aio_pool_init(AIOPool
*pool
, int aiocb_size
,
1499 void (*cancel
)(BlockDriverAIOCB
*acb
))
1501 pool
->aiocb_size
= aiocb_size
;
1502 pool
->cancel
= cancel
;
1503 pool
->free_aiocb
= NULL
;
1506 void *qemu_aio_get_pool(AIOPool
*pool
, BlockDriverState
*bs
,
1507 BlockDriverCompletionFunc
*cb
, void *opaque
)
1509 BlockDriverAIOCB
*acb
;
1511 if (pool
->free_aiocb
) {
1512 acb
= pool
->free_aiocb
;
1513 pool
->free_aiocb
= acb
->next
;
1515 acb
= qemu_mallocz(pool
->aiocb_size
);
1520 acb
->opaque
= opaque
;
1524 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1527 return qemu_aio_get_pool(&bs
->drv
->aio_pool
, bs
, cb
, opaque
);
1530 void qemu_aio_release(void *p
)
1532 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1533 AIOPool
*pool
= acb
->pool
;
1534 acb
->next
= pool
->free_aiocb
;
1535 pool
->free_aiocb
= acb
;
1538 /**************************************************************/
1539 /* removable device support */
1542 * Return TRUE if the media is present
1544 int bdrv_is_inserted(BlockDriverState
*bs
)
1546 BlockDriver
*drv
= bs
->drv
;
1550 if (!drv
->bdrv_is_inserted
)
1552 ret
= drv
->bdrv_is_inserted(bs
);
1557 * Return TRUE if the media changed since the last call to this
1558 * function. It is currently only used for floppy disks
1560 int bdrv_media_changed(BlockDriverState
*bs
)
1562 BlockDriver
*drv
= bs
->drv
;
1565 if (!drv
|| !drv
->bdrv_media_changed
)
1568 ret
= drv
->bdrv_media_changed(bs
);
1569 if (ret
== -ENOTSUP
)
1570 ret
= bs
->media_changed
;
1571 bs
->media_changed
= 0;
1576 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1578 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1580 BlockDriver
*drv
= bs
->drv
;
1583 if (!drv
|| !drv
->bdrv_eject
) {
1586 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1588 if (ret
== -ENOTSUP
) {
1594 int bdrv_is_locked(BlockDriverState
*bs
)
1600 * Lock or unlock the media (if it is locked, the user won't be able
1601 * to eject it manually).
1603 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1605 BlockDriver
*drv
= bs
->drv
;
1607 bs
->locked
= locked
;
1608 if (drv
&& drv
->bdrv_set_locked
) {
1609 drv
->bdrv_set_locked(bs
, locked
);
1613 /* needed for generic scsi interface */
1615 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1617 BlockDriver
*drv
= bs
->drv
;
1619 if (drv
&& drv
->bdrv_ioctl
)
1620 return drv
->bdrv_ioctl(bs
, req
, buf
);
1624 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
1625 unsigned long int req
, void *buf
,
1626 BlockDriverCompletionFunc
*cb
, void *opaque
)
1628 BlockDriver
*drv
= bs
->drv
;
1630 if (drv
&& drv
->bdrv_aio_ioctl
)
1631 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);
1635 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
1637 return qemu_memalign((bs
&& bs
->buffer_alignment
) ? bs
->buffer_alignment
: 512, size
);