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 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
52 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
53 BlockDriverCompletionFunc
*cb
, void *opaque
);
54 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
55 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
56 BlockDriverCompletionFunc
*cb
, void *opaque
);
57 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
58 uint8_t *buf
, int nb_sectors
);
59 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
60 const uint8_t *buf
, int nb_sectors
);
62 BlockDriverState
*bdrv_first
;
64 static BlockDriver
*first_drv
;
66 int path_is_absolute(const char *path
)
70 /* specific case for names like: "\\.\d:" */
71 if (*path
== '/' || *path
== '\\')
74 p
= strchr(path
, ':');
80 return (*p
== '/' || *p
== '\\');
86 /* if filename is absolute, just copy it to dest. Otherwise, build a
87 path to it by considering it is relative to base_path. URL are
89 void path_combine(char *dest
, int dest_size
,
90 const char *base_path
,
98 if (path_is_absolute(filename
)) {
99 pstrcpy(dest
, dest_size
, filename
);
101 p
= strchr(base_path
, ':');
106 p1
= strrchr(base_path
, '/');
110 p2
= strrchr(base_path
, '\\');
122 if (len
> dest_size
- 1)
124 memcpy(dest
, base_path
, len
);
126 pstrcat(dest
, dest_size
, filename
);
130 void bdrv_register(BlockDriver
*bdrv
)
132 if (!bdrv
->bdrv_aio_readv
) {
133 /* add AIO emulation layer */
134 bdrv
->bdrv_aio_readv
= bdrv_aio_readv_em
;
135 bdrv
->bdrv_aio_writev
= bdrv_aio_writev_em
;
136 } else if (!bdrv
->bdrv_read
) {
137 /* add synchronous IO emulation layer */
138 bdrv
->bdrv_read
= bdrv_read_em
;
139 bdrv
->bdrv_write
= bdrv_write_em
;
141 bdrv
->next
= first_drv
;
145 /* create a new block device (by default it is empty) */
146 BlockDriverState
*bdrv_new(const char *device_name
)
148 BlockDriverState
**pbs
, *bs
;
150 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
, const char* filename
,
173 QEMUOptionParameter
*options
)
175 if (!drv
->bdrv_create
)
178 return drv
->bdrv_create(filename
, options
);
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 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
))
234 return bdrv_find_format("raw");
236 p
= strchr(filename
, ':');
238 return bdrv_find_format("raw");
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
))
253 * Detect host devices. By convention, /dev/cdrom[N] is always
254 * recognized as a host CDROM.
256 static BlockDriver
*find_hdev_driver(const char *filename
)
258 int score_max
= 0, score
;
259 BlockDriver
*drv
= NULL
, *d
;
261 for (d
= first_drv
; d
; d
= d
->next
) {
262 if (d
->bdrv_probe_device
) {
263 score
= d
->bdrv_probe_device(filename
);
264 if (score
> score_max
) {
274 static BlockDriver
*find_image_format(const char *filename
)
276 int ret
, score
, score_max
;
277 BlockDriver
*drv1
, *drv
;
279 BlockDriverState
*bs
;
281 drv
= find_protocol(filename
);
282 /* no need to test disk image formats for vvfat */
283 if (drv
&& strcmp(drv
->format_name
, "vvfat") == 0)
286 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
289 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
296 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
297 if (drv1
->bdrv_probe
) {
298 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
299 if (score
> score_max
) {
308 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
310 BlockDriverState
*bs
;
314 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
324 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
326 return bdrv_open2(bs
, filename
, flags
, NULL
);
329 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
333 char tmp_filename
[PATH_MAX
];
334 char backing_filename
[PATH_MAX
];
337 bs
->is_temporary
= 0;
340 /* buffer_alignment defaulted to 512, drivers can change this value */
341 bs
->buffer_alignment
= 512;
343 if (flags
& BDRV_O_SNAPSHOT
) {
344 BlockDriverState
*bs1
;
347 BlockDriver
*bdrv_qcow2
;
348 QEMUOptionParameter
*options
;
350 /* if snapshot, we create a temporary backing file and open it
351 instead of opening 'filename' directly */
353 /* if there is a backing file, use it */
355 ret
= bdrv_open2(bs1
, filename
, 0, drv
);
360 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
362 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
367 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
369 /* Real path is meaningless for protocols */
371 snprintf(backing_filename
, sizeof(backing_filename
),
374 realpath(filename
, backing_filename
);
376 bdrv_qcow2
= bdrv_find_format("qcow2");
377 options
= parse_option_parameters("", bdrv_qcow2
->create_options
, NULL
);
379 set_option_parameter_int(options
, BLOCK_OPT_SIZE
, total_size
* 512);
380 set_option_parameter(options
, BLOCK_OPT_BACKING_FILE
, backing_filename
);
382 set_option_parameter(options
, BLOCK_OPT_BACKING_FMT
,
386 ret
= bdrv_create(bdrv_qcow2
, tmp_filename
, options
);
391 filename
= tmp_filename
;
393 bs
->is_temporary
= 1;
396 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
397 if (flags
& BDRV_O_FILE
) {
398 drv
= find_protocol(filename
);
400 drv
= find_hdev_driver(filename
);
402 drv
= find_image_format(filename
);
407 goto unlink_and_fail
;
410 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
411 /* Note: for compatibility, we open disk image files as RDWR, and
412 RDONLY as fallback */
413 if (!(flags
& BDRV_O_FILE
))
414 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
416 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
417 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
418 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
419 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
423 qemu_free(bs
->opaque
);
427 if (bs
->is_temporary
)
431 if (drv
->bdrv_getlength
) {
432 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
435 if (bs
->is_temporary
) {
439 if (bs
->backing_file
[0] != '\0') {
440 /* if there is a backing file, use it */
441 BlockDriver
*back_drv
= NULL
;
442 bs
->backing_hd
= bdrv_new("");
443 path_combine(backing_filename
, sizeof(backing_filename
),
444 filename
, bs
->backing_file
);
445 if (bs
->backing_format
[0] != '\0')
446 back_drv
= bdrv_find_format(bs
->backing_format
);
447 ret
= bdrv_open2(bs
->backing_hd
, backing_filename
, open_flags
,
455 if (!bdrv_key_required(bs
)) {
456 /* call the change callback */
457 bs
->media_changed
= 1;
459 bs
->change_cb(bs
->change_opaque
);
464 void bdrv_close(BlockDriverState
*bs
)
468 bdrv_delete(bs
->backing_hd
);
469 bs
->drv
->bdrv_close(bs
);
470 qemu_free(bs
->opaque
);
472 if (bs
->is_temporary
) {
473 unlink(bs
->filename
);
479 /* call the change callback */
480 bs
->media_changed
= 1;
482 bs
->change_cb(bs
->change_opaque
);
486 void bdrv_delete(BlockDriverState
*bs
)
488 BlockDriverState
**pbs
;
491 while (*pbs
!= bs
&& *pbs
!= NULL
)
501 * Run consistency checks on an image
503 * Returns the number of errors or -errno when an internal error occurs
505 int bdrv_check(BlockDriverState
*bs
)
507 if (bs
->drv
->bdrv_check
== NULL
) {
511 return bs
->drv
->bdrv_check(bs
);
514 /* commit COW file into the raw image */
515 int bdrv_commit(BlockDriverState
*bs
)
517 BlockDriver
*drv
= bs
->drv
;
518 int64_t i
, total_sectors
;
520 unsigned char sector
[512];
529 if (!bs
->backing_hd
) {
533 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
534 for (i
= 0; i
< total_sectors
;) {
535 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
536 for(j
= 0; j
< n
; j
++) {
537 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
541 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
551 if (drv
->bdrv_make_empty
)
552 return drv
->bdrv_make_empty(bs
);
557 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
562 if (!bdrv_is_inserted(bs
))
568 len
= bdrv_getlength(bs
);
573 if ((offset
> len
) || (len
- offset
< size
))
579 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
582 return bdrv_check_byte_request(bs
, sector_num
* 512, nb_sectors
* 512);
585 /* return < 0 if error. See bdrv_write() for the return codes */
586 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
587 uint8_t *buf
, int nb_sectors
)
589 BlockDriver
*drv
= bs
->drv
;
593 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
596 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
599 /* Return < 0 if error. Important errors are:
600 -EIO generic I/O error (may happen for all errors)
601 -ENOMEDIUM No media inserted.
602 -EINVAL Invalid sector number or nb_sectors
603 -EACCES Trying to write a read-only device
605 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
606 const uint8_t *buf
, int nb_sectors
)
608 BlockDriver
*drv
= bs
->drv
;
613 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
616 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
619 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
620 void *buf
, int count1
)
622 uint8_t tmp_buf
[SECTOR_SIZE
];
623 int len
, nb_sectors
, count
;
627 /* first read to align to sector start */
628 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
631 sector_num
= offset
>> SECTOR_BITS
;
633 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
635 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
643 /* read the sectors "in place" */
644 nb_sectors
= count
>> SECTOR_BITS
;
645 if (nb_sectors
> 0) {
646 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
648 sector_num
+= nb_sectors
;
649 len
= nb_sectors
<< SECTOR_BITS
;
654 /* add data from the last sector */
656 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
658 memcpy(buf
, tmp_buf
, count
);
663 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
664 const void *buf
, int count1
)
666 uint8_t tmp_buf
[SECTOR_SIZE
];
667 int len
, nb_sectors
, count
;
671 /* first write to align to sector start */
672 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
675 sector_num
= offset
>> SECTOR_BITS
;
677 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
679 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
680 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
689 /* write the sectors "in place" */
690 nb_sectors
= count
>> SECTOR_BITS
;
691 if (nb_sectors
> 0) {
692 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
694 sector_num
+= nb_sectors
;
695 len
= nb_sectors
<< SECTOR_BITS
;
700 /* add data from the last sector */
702 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
704 memcpy(tmp_buf
, buf
, count
);
705 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
712 * Truncate file to 'offset' bytes (needed only for file protocols)
714 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
716 BlockDriver
*drv
= bs
->drv
;
719 if (!drv
->bdrv_truncate
)
721 return drv
->bdrv_truncate(bs
, offset
);
725 * Length of a file in bytes. Return < 0 if error or unknown.
727 int64_t bdrv_getlength(BlockDriverState
*bs
)
729 BlockDriver
*drv
= bs
->drv
;
732 if (!drv
->bdrv_getlength
) {
734 return bs
->total_sectors
* SECTOR_SIZE
;
736 return drv
->bdrv_getlength(bs
);
739 /* return 0 as number of sectors if no device present or error */
740 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
743 length
= bdrv_getlength(bs
);
747 length
= length
>> SECTOR_BITS
;
748 *nb_sectors_ptr
= length
;
752 uint8_t boot_ind
; /* 0x80 - active */
753 uint8_t head
; /* starting head */
754 uint8_t sector
; /* starting sector */
755 uint8_t cyl
; /* starting cylinder */
756 uint8_t sys_ind
; /* What partition type */
757 uint8_t end_head
; /* end head */
758 uint8_t end_sector
; /* end sector */
759 uint8_t end_cyl
; /* end cylinder */
760 uint32_t start_sect
; /* starting sector counting from 0 */
761 uint32_t nr_sects
; /* nr of sectors in partition */
762 } __attribute__((packed
));
764 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
765 static int guess_disk_lchs(BlockDriverState
*bs
,
766 int *pcylinders
, int *pheads
, int *psectors
)
769 int ret
, i
, heads
, sectors
, cylinders
;
774 bdrv_get_geometry(bs
, &nb_sectors
);
776 ret
= bdrv_read(bs
, 0, buf
, 1);
779 /* test msdos magic */
780 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
782 for(i
= 0; i
< 4; i
++) {
783 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
784 nr_sects
= le32_to_cpu(p
->nr_sects
);
785 if (nr_sects
&& p
->end_head
) {
786 /* We make the assumption that the partition terminates on
787 a cylinder boundary */
788 heads
= p
->end_head
+ 1;
789 sectors
= p
->end_sector
& 63;
792 cylinders
= nb_sectors
/ (heads
* sectors
);
793 if (cylinders
< 1 || cylinders
> 16383)
797 *pcylinders
= cylinders
;
799 printf("guessed geometry: LCHS=%d %d %d\n",
800 cylinders
, heads
, sectors
);
808 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
810 int translation
, lba_detected
= 0;
811 int cylinders
, heads
, secs
;
814 /* if a geometry hint is available, use it */
815 bdrv_get_geometry(bs
, &nb_sectors
);
816 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
817 translation
= bdrv_get_translation_hint(bs
);
818 if (cylinders
!= 0) {
823 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
825 /* if heads > 16, it means that a BIOS LBA
826 translation was active, so the default
827 hardware geometry is OK */
829 goto default_geometry
;
834 /* disable any translation to be in sync with
835 the logical geometry */
836 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
837 bdrv_set_translation_hint(bs
,
838 BIOS_ATA_TRANSLATION_NONE
);
843 /* if no geometry, use a standard physical disk geometry */
844 cylinders
= nb_sectors
/ (16 * 63);
846 if (cylinders
> 16383)
848 else if (cylinders
< 2)
853 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
854 if ((*pcyls
* *pheads
) <= 131072) {
855 bdrv_set_translation_hint(bs
,
856 BIOS_ATA_TRANSLATION_LARGE
);
858 bdrv_set_translation_hint(bs
,
859 BIOS_ATA_TRANSLATION_LBA
);
863 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
867 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
868 int cyls
, int heads
, int secs
)
875 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
878 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
879 type
== BDRV_TYPE_FLOPPY
));
882 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
884 bs
->translation
= translation
;
887 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
888 int *pcyls
, int *pheads
, int *psecs
)
895 int bdrv_get_type_hint(BlockDriverState
*bs
)
900 int bdrv_get_translation_hint(BlockDriverState
*bs
)
902 return bs
->translation
;
905 int bdrv_is_removable(BlockDriverState
*bs
)
907 return bs
->removable
;
910 int bdrv_is_read_only(BlockDriverState
*bs
)
912 return bs
->read_only
;
915 int bdrv_is_sg(BlockDriverState
*bs
)
920 /* XXX: no longer used */
921 void bdrv_set_change_cb(BlockDriverState
*bs
,
922 void (*change_cb
)(void *opaque
), void *opaque
)
924 bs
->change_cb
= change_cb
;
925 bs
->change_opaque
= opaque
;
928 int bdrv_is_encrypted(BlockDriverState
*bs
)
930 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
932 return bs
->encrypted
;
935 int bdrv_key_required(BlockDriverState
*bs
)
937 BlockDriverState
*backing_hd
= bs
->backing_hd
;
939 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
941 return (bs
->encrypted
&& !bs
->valid_key
);
944 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
947 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
948 ret
= bdrv_set_key(bs
->backing_hd
, key
);
954 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
956 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
959 } else if (!bs
->valid_key
) {
961 /* call the change callback now, we skipped it on open */
962 bs
->media_changed
= 1;
964 bs
->change_cb(bs
->change_opaque
);
969 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
974 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
978 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
983 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
984 it(opaque
, drv
->format_name
);
988 BlockDriverState
*bdrv_find(const char *name
)
990 BlockDriverState
*bs
;
992 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
993 if (!strcmp(name
, bs
->device_name
))
999 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1001 BlockDriverState
*bs
;
1003 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1008 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1010 return bs
->device_name
;
1013 void bdrv_flush(BlockDriverState
*bs
)
1017 if (bs
->drv
->bdrv_flush
)
1018 bs
->drv
->bdrv_flush(bs
);
1020 bdrv_flush(bs
->backing_hd
);
1023 void bdrv_flush_all(void)
1025 BlockDriverState
*bs
;
1027 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1028 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1029 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1034 * Returns true iff the specified sector is present in the disk image. Drivers
1035 * not implementing the functionality are assumed to not support backing files,
1036 * hence all their sectors are reported as allocated.
1038 * 'pnum' is set to the number of sectors (including and immediately following
1039 * the specified sector) that are known to be in the same
1040 * allocated/unallocated state.
1042 * 'nb_sectors' is the max value 'pnum' should be set to.
1044 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1048 if (!bs
->drv
->bdrv_is_allocated
) {
1049 if (sector_num
>= bs
->total_sectors
) {
1053 n
= bs
->total_sectors
- sector_num
;
1054 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1057 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1060 void bdrv_info(Monitor
*mon
)
1062 BlockDriverState
*bs
;
1064 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1065 monitor_printf(mon
, "%s:", bs
->device_name
);
1066 monitor_printf(mon
, " type=");
1069 monitor_printf(mon
, "hd");
1071 case BDRV_TYPE_CDROM
:
1072 monitor_printf(mon
, "cdrom");
1074 case BDRV_TYPE_FLOPPY
:
1075 monitor_printf(mon
, "floppy");
1078 monitor_printf(mon
, " removable=%d", bs
->removable
);
1079 if (bs
->removable
) {
1080 monitor_printf(mon
, " locked=%d", bs
->locked
);
1083 monitor_printf(mon
, " file=");
1084 monitor_print_filename(mon
, bs
->filename
);
1085 if (bs
->backing_file
[0] != '\0') {
1086 monitor_printf(mon
, " backing_file=");
1087 monitor_print_filename(mon
, bs
->backing_file
);
1089 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1090 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1091 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1093 monitor_printf(mon
, " [not inserted]");
1095 monitor_printf(mon
, "\n");
1099 /* The "info blockstats" command. */
1100 void bdrv_info_stats(Monitor
*mon
)
1102 BlockDriverState
*bs
;
1104 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1105 monitor_printf(mon
, "%s:"
1106 " rd_bytes=%" PRIu64
1107 " wr_bytes=%" PRIu64
1108 " rd_operations=%" PRIu64
1109 " wr_operations=%" PRIu64
1112 bs
->rd_bytes
, bs
->wr_bytes
,
1113 bs
->rd_ops
, bs
->wr_ops
);
1117 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1119 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1120 return bs
->backing_file
;
1121 else if (bs
->encrypted
)
1122 return bs
->filename
;
1127 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1128 char *filename
, int filename_size
)
1130 if (!bs
->backing_hd
) {
1131 pstrcpy(filename
, filename_size
, "");
1133 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1137 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1138 const uint8_t *buf
, int nb_sectors
)
1140 BlockDriver
*drv
= bs
->drv
;
1143 if (!drv
->bdrv_write_compressed
)
1145 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1147 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1150 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1152 BlockDriver
*drv
= bs
->drv
;
1155 if (!drv
->bdrv_get_info
)
1157 memset(bdi
, 0, sizeof(*bdi
));
1158 return drv
->bdrv_get_info(bs
, bdi
);
1161 int bdrv_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
, int64_t pos
, int size
)
1163 BlockDriver
*drv
= bs
->drv
;
1166 if (!drv
->bdrv_put_buffer
)
1168 return drv
->bdrv_put_buffer(bs
, buf
, pos
, size
);
1171 int bdrv_get_buffer(BlockDriverState
*bs
, uint8_t *buf
, int64_t pos
, int size
)
1173 BlockDriver
*drv
= bs
->drv
;
1176 if (!drv
->bdrv_get_buffer
)
1178 return drv
->bdrv_get_buffer(bs
, buf
, pos
, size
);
1181 /**************************************************************/
1182 /* handling of snapshots */
1184 int bdrv_snapshot_create(BlockDriverState
*bs
,
1185 QEMUSnapshotInfo
*sn_info
)
1187 BlockDriver
*drv
= bs
->drv
;
1190 if (!drv
->bdrv_snapshot_create
)
1192 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1195 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1196 const char *snapshot_id
)
1198 BlockDriver
*drv
= bs
->drv
;
1201 if (!drv
->bdrv_snapshot_goto
)
1203 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1206 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1208 BlockDriver
*drv
= bs
->drv
;
1211 if (!drv
->bdrv_snapshot_delete
)
1213 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1216 int bdrv_snapshot_list(BlockDriverState
*bs
,
1217 QEMUSnapshotInfo
**psn_info
)
1219 BlockDriver
*drv
= bs
->drv
;
1222 if (!drv
->bdrv_snapshot_list
)
1224 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1227 #define NB_SUFFIXES 4
1229 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1231 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1236 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1239 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1240 if (size
< (10 * base
)) {
1241 snprintf(buf
, buf_size
, "%0.1f%c",
1242 (double)size
/ base
,
1245 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1246 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1247 ((size
+ (base
>> 1)) / base
),
1257 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1259 char buf1
[128], date_buf
[128], clock_buf
[128];
1269 snprintf(buf
, buf_size
,
1270 "%-10s%-20s%7s%20s%15s",
1271 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1275 ptm
= localtime(&ti
);
1276 strftime(date_buf
, sizeof(date_buf
),
1277 "%Y-%m-%d %H:%M:%S", ptm
);
1279 localtime_r(&ti
, &tm
);
1280 strftime(date_buf
, sizeof(date_buf
),
1281 "%Y-%m-%d %H:%M:%S", &tm
);
1283 secs
= sn
->vm_clock_nsec
/ 1000000000;
1284 snprintf(clock_buf
, sizeof(clock_buf
),
1285 "%02d:%02d:%02d.%03d",
1287 (int)((secs
/ 60) % 60),
1289 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1290 snprintf(buf
, buf_size
,
1291 "%-10s%-20s%7s%20s%15s",
1292 sn
->id_str
, sn
->name
,
1293 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1301 /**************************************************************/
1304 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1305 QEMUIOVector
*qiov
, int nb_sectors
,
1306 BlockDriverCompletionFunc
*cb
, void *opaque
)
1308 BlockDriver
*drv
= bs
->drv
;
1309 BlockDriverAIOCB
*ret
;
1313 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1316 ret
= drv
->bdrv_aio_readv(bs
, sector_num
, qiov
, nb_sectors
,
1320 /* Update stats even though technically transfer has not happened. */
1321 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1328 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1329 QEMUIOVector
*qiov
, int nb_sectors
,
1330 BlockDriverCompletionFunc
*cb
, void *opaque
)
1332 BlockDriver
*drv
= bs
->drv
;
1333 BlockDriverAIOCB
*ret
;
1339 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1342 ret
= drv
->bdrv_aio_writev(bs
, sector_num
, qiov
, nb_sectors
,
1346 /* Update stats even though technically transfer has not happened. */
1347 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1354 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1356 acb
->pool
->cancel(acb
);
1360 /**************************************************************/
1361 /* async block device emulation */
1363 typedef struct BlockDriverAIOCBSync
{
1364 BlockDriverAIOCB common
;
1367 /* vector translation state */
1371 } BlockDriverAIOCBSync
;
1373 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1375 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1376 qemu_bh_delete(acb
->bh
);
1377 qemu_aio_release(acb
);
1380 static AIOPool bdrv_em_aio_pool
= {
1381 .aiocb_size
= sizeof(BlockDriverAIOCBSync
),
1382 .cancel
= bdrv_aio_cancel_em
,
1385 static void bdrv_aio_bh_cb(void *opaque
)
1387 BlockDriverAIOCBSync
*acb
= opaque
;
1390 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
1391 qemu_vfree(acb
->bounce
);
1392 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1393 qemu_bh_delete(acb
->bh
);
1394 qemu_aio_release(acb
);
1397 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1401 BlockDriverCompletionFunc
*cb
,
1406 BlockDriverAIOCBSync
*acb
;
1408 acb
= qemu_aio_get(&bdrv_em_aio_pool
, bs
, cb
, opaque
);
1409 acb
->is_write
= is_write
;
1411 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
1414 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1417 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
1418 acb
->ret
= bdrv_write(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1420 acb
->ret
= bdrv_read(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1423 qemu_bh_schedule(acb
->bh
);
1425 return &acb
->common
;
1428 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
1429 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1430 BlockDriverCompletionFunc
*cb
, void *opaque
)
1432 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
1435 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
1436 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1437 BlockDriverCompletionFunc
*cb
, void *opaque
)
1439 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
1442 /**************************************************************/
1443 /* sync block device emulation */
1445 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1447 *(int *)opaque
= ret
;
1450 #define NOT_DONE 0x7fffffff
1452 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1453 uint8_t *buf
, int nb_sectors
)
1456 BlockDriverAIOCB
*acb
;
1460 async_ret
= NOT_DONE
;
1461 iov
.iov_base
= (void *)buf
;
1462 iov
.iov_len
= nb_sectors
* 512;
1463 qemu_iovec_init_external(&qiov
, &iov
, 1);
1464 acb
= bdrv_aio_readv(bs
, sector_num
, &qiov
, nb_sectors
,
1465 bdrv_rw_em_cb
, &async_ret
);
1469 while (async_ret
== NOT_DONE
) {
1476 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1477 const uint8_t *buf
, int nb_sectors
)
1480 BlockDriverAIOCB
*acb
;
1484 async_ret
= NOT_DONE
;
1485 iov
.iov_base
= (void *)buf
;
1486 iov
.iov_len
= nb_sectors
* 512;
1487 qemu_iovec_init_external(&qiov
, &iov
, 1);
1488 acb
= bdrv_aio_writev(bs
, sector_num
, &qiov
, nb_sectors
,
1489 bdrv_rw_em_cb
, &async_ret
);
1492 while (async_ret
== NOT_DONE
) {
1498 void bdrv_init(void)
1500 module_call_init(MODULE_INIT_BLOCK
);
1503 void *qemu_aio_get(AIOPool
*pool
, BlockDriverState
*bs
,
1504 BlockDriverCompletionFunc
*cb
, void *opaque
)
1506 BlockDriverAIOCB
*acb
;
1508 if (pool
->free_aiocb
) {
1509 acb
= pool
->free_aiocb
;
1510 pool
->free_aiocb
= acb
->next
;
1512 acb
= qemu_mallocz(pool
->aiocb_size
);
1517 acb
->opaque
= opaque
;
1521 void qemu_aio_release(void *p
)
1523 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1524 AIOPool
*pool
= acb
->pool
;
1525 acb
->next
= pool
->free_aiocb
;
1526 pool
->free_aiocb
= acb
;
1529 /**************************************************************/
1530 /* removable device support */
1533 * Return TRUE if the media is present
1535 int bdrv_is_inserted(BlockDriverState
*bs
)
1537 BlockDriver
*drv
= bs
->drv
;
1541 if (!drv
->bdrv_is_inserted
)
1543 ret
= drv
->bdrv_is_inserted(bs
);
1548 * Return TRUE if the media changed since the last call to this
1549 * function. It is currently only used for floppy disks
1551 int bdrv_media_changed(BlockDriverState
*bs
)
1553 BlockDriver
*drv
= bs
->drv
;
1556 if (!drv
|| !drv
->bdrv_media_changed
)
1559 ret
= drv
->bdrv_media_changed(bs
);
1560 if (ret
== -ENOTSUP
)
1561 ret
= bs
->media_changed
;
1562 bs
->media_changed
= 0;
1567 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1569 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1571 BlockDriver
*drv
= bs
->drv
;
1574 if (!drv
|| !drv
->bdrv_eject
) {
1577 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1579 if (ret
== -ENOTSUP
) {
1585 int bdrv_is_locked(BlockDriverState
*bs
)
1591 * Lock or unlock the media (if it is locked, the user won't be able
1592 * to eject it manually).
1594 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1596 BlockDriver
*drv
= bs
->drv
;
1598 bs
->locked
= locked
;
1599 if (drv
&& drv
->bdrv_set_locked
) {
1600 drv
->bdrv_set_locked(bs
, locked
);
1604 /* needed for generic scsi interface */
1606 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1608 BlockDriver
*drv
= bs
->drv
;
1610 if (drv
&& drv
->bdrv_ioctl
)
1611 return drv
->bdrv_ioctl(bs
, req
, buf
);
1615 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
1616 unsigned long int req
, void *buf
,
1617 BlockDriverCompletionFunc
*cb
, void *opaque
)
1619 BlockDriver
*drv
= bs
->drv
;
1621 if (drv
&& drv
->bdrv_aio_ioctl
)
1622 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);
1626 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
1628 return qemu_memalign((bs
&& bs
->buffer_alignment
) ? bs
->buffer_alignment
: 512, size
);