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
)
228 int len
= strnlen(filename
, 127)+1;
232 if (is_windows_drive(filename
) ||
233 is_windows_drive_prefix(filename
))
234 return bdrv_find_format("raw");
236 p
= fill_token(protocol
, len
, filename
, ':');
238 return bdrv_find_format("raw");
239 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
240 if (drv1
->protocol_name
&&
241 !strcmp(drv1
->protocol_name
, protocol
))
248 * Detect host devices. By convention, /dev/cdrom[N] is always
249 * recognized as a host CDROM.
251 static BlockDriver
*find_hdev_driver(const char *filename
)
253 int score_max
= 0, score
;
254 BlockDriver
*drv
= NULL
, *d
;
256 for (d
= first_drv
; d
; d
= d
->next
) {
257 if (d
->bdrv_probe_device
) {
258 score
= d
->bdrv_probe_device(filename
);
259 if (score
> score_max
) {
269 static BlockDriver
*find_image_format(const char *filename
)
271 int ret
, score
, score_max
;
272 BlockDriver
*drv1
, *drv
;
274 BlockDriverState
*bs
;
276 drv
= find_protocol(filename
);
277 /* no need to test disk image formats for vvfat */
278 if (drv
&& strcmp(drv
->format_name
, "vvfat") == 0)
281 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
284 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
291 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
292 if (drv1
->bdrv_probe
) {
293 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
294 if (score
> score_max
) {
303 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
305 BlockDriverState
*bs
;
309 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
319 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
321 return bdrv_open2(bs
, filename
, flags
, NULL
);
324 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
328 char tmp_filename
[PATH_MAX
];
329 char backing_filename
[PATH_MAX
];
332 bs
->is_temporary
= 0;
335 /* buffer_alignment defaulted to 512, drivers can change this value */
336 bs
->buffer_alignment
= 512;
338 if (flags
& BDRV_O_SNAPSHOT
) {
339 BlockDriverState
*bs1
;
342 BlockDriver
*bdrv_qcow2
;
343 QEMUOptionParameter
*options
;
345 /* if snapshot, we create a temporary backing file and open it
346 instead of opening 'filename' directly */
348 /* if there is a backing file, use it */
350 ret
= bdrv_open2(bs1
, filename
, 0, drv
);
355 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
357 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
362 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
364 /* Real path is meaningless for protocols */
366 snprintf(backing_filename
, sizeof(backing_filename
),
369 realpath(filename
, backing_filename
);
371 bdrv_qcow2
= bdrv_find_format("qcow2");
372 options
= parse_option_parameters("", bdrv_qcow2
->create_options
, NULL
);
374 set_option_parameter_int(options
, BLOCK_OPT_SIZE
, total_size
* 512);
375 set_option_parameter(options
, BLOCK_OPT_BACKING_FILE
, backing_filename
);
377 set_option_parameter(options
, BLOCK_OPT_BACKING_FMT
,
381 ret
= bdrv_create(bdrv_qcow2
, tmp_filename
, options
);
386 filename
= tmp_filename
;
388 bs
->is_temporary
= 1;
391 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
392 if (flags
& BDRV_O_FILE
) {
393 drv
= find_protocol(filename
);
395 drv
= find_hdev_driver(filename
);
397 drv
= find_image_format(filename
);
402 goto unlink_and_fail
;
405 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
406 /* Note: for compatibility, we open disk image files as RDWR, and
407 RDONLY as fallback */
408 if (!(flags
& BDRV_O_FILE
))
409 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
411 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
412 ret
= bdrv_open3(bs
, filename
, open_flags
, drv
);
413 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
414 ret
= bdrv_open3(bs
, filename
, open_flags
& ~BDRV_O_RDWR
, drv
);
418 qemu_free(bs
->opaque
);
422 if (bs
->is_temporary
)
426 if (drv
->bdrv_getlength
) {
427 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
430 if (bs
->is_temporary
) {
434 if (bs
->backing_file
[0] != '\0') {
435 /* if there is a backing file, use it */
436 BlockDriver
*back_drv
= NULL
;
437 bs
->backing_hd
= bdrv_new("");
438 path_combine(backing_filename
, sizeof(backing_filename
),
439 filename
, bs
->backing_file
);
440 if (bs
->backing_format
[0] != '\0')
441 back_drv
= bdrv_find_format(bs
->backing_format
);
442 ret
= bdrv_open2(bs
->backing_hd
, backing_filename
, open_flags
,
450 if (!bdrv_key_required(bs
)) {
451 /* call the change callback */
452 bs
->media_changed
= 1;
454 bs
->change_cb(bs
->change_opaque
);
459 int bdrv_open3(BlockDriverState
*bs
, const char *filename
, int flags
, BlockDriver
*drv
)
461 char myfile
[PATH_MAX
];
464 if (!strstart(filename
, "file:", &f
)) {
465 fill_token(myfile
, PATH_MAX
, filename
, '\0');
466 return drv
->bdrv_open(bs
,myfile
,flags
);
468 return drv
->bdrv_open(bs
,f
,flags
);
471 void bdrv_close(BlockDriverState
*bs
)
475 bdrv_delete(bs
->backing_hd
);
476 bs
->drv
->bdrv_close(bs
);
477 qemu_free(bs
->opaque
);
479 if (bs
->is_temporary
) {
480 unlink(bs
->filename
);
486 /* call the change callback */
487 bs
->media_changed
= 1;
489 bs
->change_cb(bs
->change_opaque
);
493 void bdrv_delete(BlockDriverState
*bs
)
495 BlockDriverState
**pbs
;
498 while (*pbs
!= bs
&& *pbs
!= NULL
)
508 * Run consistency checks on an image
510 * Returns the number of errors or -errno when an internal error occurs
512 int bdrv_check(BlockDriverState
*bs
)
514 if (bs
->drv
->bdrv_check
== NULL
) {
518 return bs
->drv
->bdrv_check(bs
);
521 /* commit COW file into the raw image */
522 int bdrv_commit(BlockDriverState
*bs
)
524 BlockDriver
*drv
= bs
->drv
;
525 int64_t i
, total_sectors
;
527 unsigned char sector
[512];
536 if (!bs
->backing_hd
) {
540 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
541 for (i
= 0; i
< total_sectors
;) {
542 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
543 for(j
= 0; j
< n
; j
++) {
544 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
548 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
558 if (drv
->bdrv_make_empty
)
559 return drv
->bdrv_make_empty(bs
);
564 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
569 if (!bdrv_is_inserted(bs
))
575 len
= bdrv_getlength(bs
);
580 if ((offset
> len
) || (len
- offset
< size
))
586 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
589 return bdrv_check_byte_request(bs
, sector_num
* 512, nb_sectors
* 512);
592 /* return < 0 if error. See bdrv_write() for the return codes */
593 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
594 uint8_t *buf
, int nb_sectors
)
596 BlockDriver
*drv
= bs
->drv
;
600 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
603 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
606 /* Return < 0 if error. Important errors are:
607 -EIO generic I/O error (may happen for all errors)
608 -ENOMEDIUM No media inserted.
609 -EINVAL Invalid sector number or nb_sectors
610 -EACCES Trying to write a read-only device
612 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
613 const uint8_t *buf
, int nb_sectors
)
615 BlockDriver
*drv
= bs
->drv
;
620 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
623 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
626 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
627 void *buf
, int count1
)
629 uint8_t tmp_buf
[SECTOR_SIZE
];
630 int len
, nb_sectors
, count
;
634 /* first read to align to sector start */
635 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
638 sector_num
= offset
>> SECTOR_BITS
;
640 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
642 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
650 /* read the sectors "in place" */
651 nb_sectors
= count
>> SECTOR_BITS
;
652 if (nb_sectors
> 0) {
653 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
655 sector_num
+= nb_sectors
;
656 len
= nb_sectors
<< SECTOR_BITS
;
661 /* add data from the last sector */
663 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
665 memcpy(buf
, tmp_buf
, count
);
670 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
671 const void *buf
, int count1
)
673 uint8_t tmp_buf
[SECTOR_SIZE
];
674 int len
, nb_sectors
, count
;
678 /* first write to align to sector start */
679 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
682 sector_num
= offset
>> SECTOR_BITS
;
684 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
686 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
687 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
696 /* write the sectors "in place" */
697 nb_sectors
= count
>> SECTOR_BITS
;
698 if (nb_sectors
> 0) {
699 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
701 sector_num
+= nb_sectors
;
702 len
= nb_sectors
<< SECTOR_BITS
;
707 /* add data from the last sector */
709 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
711 memcpy(tmp_buf
, buf
, count
);
712 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
719 * Truncate file to 'offset' bytes (needed only for file protocols)
721 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
723 BlockDriver
*drv
= bs
->drv
;
726 if (!drv
->bdrv_truncate
)
728 return drv
->bdrv_truncate(bs
, offset
);
732 * Length of a file in bytes. Return < 0 if error or unknown.
734 int64_t bdrv_getlength(BlockDriverState
*bs
)
736 BlockDriver
*drv
= bs
->drv
;
739 if (!drv
->bdrv_getlength
) {
741 return bs
->total_sectors
* SECTOR_SIZE
;
743 return drv
->bdrv_getlength(bs
);
746 /* return 0 as number of sectors if no device present or error */
747 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
750 length
= bdrv_getlength(bs
);
754 length
= length
>> SECTOR_BITS
;
755 *nb_sectors_ptr
= length
;
759 uint8_t boot_ind
; /* 0x80 - active */
760 uint8_t head
; /* starting head */
761 uint8_t sector
; /* starting sector */
762 uint8_t cyl
; /* starting cylinder */
763 uint8_t sys_ind
; /* What partition type */
764 uint8_t end_head
; /* end head */
765 uint8_t end_sector
; /* end sector */
766 uint8_t end_cyl
; /* end cylinder */
767 uint32_t start_sect
; /* starting sector counting from 0 */
768 uint32_t nr_sects
; /* nr of sectors in partition */
769 } __attribute__((packed
));
771 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
772 static int guess_disk_lchs(BlockDriverState
*bs
,
773 int *pcylinders
, int *pheads
, int *psectors
)
776 int ret
, i
, heads
, sectors
, cylinders
;
781 bdrv_get_geometry(bs
, &nb_sectors
);
783 ret
= bdrv_read(bs
, 0, buf
, 1);
786 /* test msdos magic */
787 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
789 for(i
= 0; i
< 4; i
++) {
790 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
791 nr_sects
= le32_to_cpu(p
->nr_sects
);
792 if (nr_sects
&& p
->end_head
) {
793 /* We make the assumption that the partition terminates on
794 a cylinder boundary */
795 heads
= p
->end_head
+ 1;
796 sectors
= p
->end_sector
& 63;
799 cylinders
= nb_sectors
/ (heads
* sectors
);
800 if (cylinders
< 1 || cylinders
> 16383)
804 *pcylinders
= cylinders
;
806 printf("guessed geometry: LCHS=%d %d %d\n",
807 cylinders
, heads
, sectors
);
815 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
817 int translation
, lba_detected
= 0;
818 int cylinders
, heads
, secs
;
821 /* if a geometry hint is available, use it */
822 bdrv_get_geometry(bs
, &nb_sectors
);
823 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
824 translation
= bdrv_get_translation_hint(bs
);
825 if (cylinders
!= 0) {
830 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
832 /* if heads > 16, it means that a BIOS LBA
833 translation was active, so the default
834 hardware geometry is OK */
836 goto default_geometry
;
841 /* disable any translation to be in sync with
842 the logical geometry */
843 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
844 bdrv_set_translation_hint(bs
,
845 BIOS_ATA_TRANSLATION_NONE
);
850 /* if no geometry, use a standard physical disk geometry */
851 cylinders
= nb_sectors
/ (16 * 63);
853 if (cylinders
> 16383)
855 else if (cylinders
< 2)
860 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
861 if ((*pcyls
* *pheads
) <= 131072) {
862 bdrv_set_translation_hint(bs
,
863 BIOS_ATA_TRANSLATION_LARGE
);
865 bdrv_set_translation_hint(bs
,
866 BIOS_ATA_TRANSLATION_LBA
);
870 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
874 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
875 int cyls
, int heads
, int secs
)
882 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
885 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
886 type
== BDRV_TYPE_FLOPPY
));
889 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
891 bs
->translation
= translation
;
894 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
895 int *pcyls
, int *pheads
, int *psecs
)
902 int bdrv_get_type_hint(BlockDriverState
*bs
)
907 int bdrv_get_translation_hint(BlockDriverState
*bs
)
909 return bs
->translation
;
912 int bdrv_is_removable(BlockDriverState
*bs
)
914 return bs
->removable
;
917 int bdrv_is_read_only(BlockDriverState
*bs
)
919 return bs
->read_only
;
922 int bdrv_is_sg(BlockDriverState
*bs
)
927 /* XXX: no longer used */
928 void bdrv_set_change_cb(BlockDriverState
*bs
,
929 void (*change_cb
)(void *opaque
), void *opaque
)
931 bs
->change_cb
= change_cb
;
932 bs
->change_opaque
= opaque
;
935 int bdrv_is_encrypted(BlockDriverState
*bs
)
937 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
939 return bs
->encrypted
;
942 int bdrv_key_required(BlockDriverState
*bs
)
944 BlockDriverState
*backing_hd
= bs
->backing_hd
;
946 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
948 return (bs
->encrypted
&& !bs
->valid_key
);
951 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
954 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
955 ret
= bdrv_set_key(bs
->backing_hd
, key
);
961 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
963 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
966 } else if (!bs
->valid_key
) {
968 /* call the change callback now, we skipped it on open */
969 bs
->media_changed
= 1;
971 bs
->change_cb(bs
->change_opaque
);
976 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
981 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
985 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
990 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
991 it(opaque
, drv
->format_name
);
995 BlockDriverState
*bdrv_find(const char *name
)
997 BlockDriverState
*bs
;
999 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1000 if (!strcmp(name
, bs
->device_name
))
1006 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1008 BlockDriverState
*bs
;
1010 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1015 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1017 return bs
->device_name
;
1020 void bdrv_flush(BlockDriverState
*bs
)
1024 if (bs
->drv
->bdrv_flush
)
1025 bs
->drv
->bdrv_flush(bs
);
1027 bdrv_flush(bs
->backing_hd
);
1030 void bdrv_flush_all(void)
1032 BlockDriverState
*bs
;
1034 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1035 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1036 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1041 * Returns true iff the specified sector is present in the disk image. Drivers
1042 * not implementing the functionality are assumed to not support backing files,
1043 * hence all their sectors are reported as allocated.
1045 * 'pnum' is set to the number of sectors (including and immediately following
1046 * the specified sector) that are known to be in the same
1047 * allocated/unallocated state.
1049 * 'nb_sectors' is the max value 'pnum' should be set to.
1051 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1055 if (!bs
->drv
->bdrv_is_allocated
) {
1056 if (sector_num
>= bs
->total_sectors
) {
1060 n
= bs
->total_sectors
- sector_num
;
1061 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1064 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1067 void bdrv_info(Monitor
*mon
)
1069 BlockDriverState
*bs
;
1071 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1072 monitor_printf(mon
, "%s:", bs
->device_name
);
1073 monitor_printf(mon
, " type=");
1076 monitor_printf(mon
, "hd");
1078 case BDRV_TYPE_CDROM
:
1079 monitor_printf(mon
, "cdrom");
1081 case BDRV_TYPE_FLOPPY
:
1082 monitor_printf(mon
, "floppy");
1085 monitor_printf(mon
, " removable=%d", bs
->removable
);
1086 if (bs
->removable
) {
1087 monitor_printf(mon
, " locked=%d", bs
->locked
);
1090 monitor_printf(mon
, " file=");
1091 monitor_print_filename(mon
, bs
->filename
);
1092 if (bs
->backing_file
[0] != '\0') {
1093 monitor_printf(mon
, " backing_file=");
1094 monitor_print_filename(mon
, bs
->backing_file
);
1096 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1097 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1098 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1100 monitor_printf(mon
, " [not inserted]");
1102 monitor_printf(mon
, "\n");
1106 /* The "info blockstats" command. */
1107 void bdrv_info_stats(Monitor
*mon
)
1109 BlockDriverState
*bs
;
1111 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1112 monitor_printf(mon
, "%s:"
1113 " rd_bytes=%" PRIu64
1114 " wr_bytes=%" PRIu64
1115 " rd_operations=%" PRIu64
1116 " wr_operations=%" PRIu64
1119 bs
->rd_bytes
, bs
->wr_bytes
,
1120 bs
->rd_ops
, bs
->wr_ops
);
1124 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1126 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1127 return bs
->backing_file
;
1128 else if (bs
->encrypted
)
1129 return bs
->filename
;
1134 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1135 char *filename
, int filename_size
)
1137 if (!bs
->backing_hd
) {
1138 pstrcpy(filename
, filename_size
, "");
1140 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1144 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1145 const uint8_t *buf
, int nb_sectors
)
1147 BlockDriver
*drv
= bs
->drv
;
1150 if (!drv
->bdrv_write_compressed
)
1152 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1154 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1157 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1159 BlockDriver
*drv
= bs
->drv
;
1162 if (!drv
->bdrv_get_info
)
1164 memset(bdi
, 0, sizeof(*bdi
));
1165 return drv
->bdrv_get_info(bs
, bdi
);
1168 int bdrv_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
, int64_t pos
, int size
)
1170 BlockDriver
*drv
= bs
->drv
;
1173 if (!drv
->bdrv_put_buffer
)
1175 return drv
->bdrv_put_buffer(bs
, buf
, pos
, size
);
1178 int bdrv_get_buffer(BlockDriverState
*bs
, uint8_t *buf
, int64_t pos
, int size
)
1180 BlockDriver
*drv
= bs
->drv
;
1183 if (!drv
->bdrv_get_buffer
)
1185 return drv
->bdrv_get_buffer(bs
, buf
, pos
, size
);
1188 /**************************************************************/
1189 /* handling of snapshots */
1191 int bdrv_snapshot_create(BlockDriverState
*bs
,
1192 QEMUSnapshotInfo
*sn_info
)
1194 BlockDriver
*drv
= bs
->drv
;
1197 if (!drv
->bdrv_snapshot_create
)
1199 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1202 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1203 const char *snapshot_id
)
1205 BlockDriver
*drv
= bs
->drv
;
1208 if (!drv
->bdrv_snapshot_goto
)
1210 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1213 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1215 BlockDriver
*drv
= bs
->drv
;
1218 if (!drv
->bdrv_snapshot_delete
)
1220 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1223 int bdrv_snapshot_list(BlockDriverState
*bs
,
1224 QEMUSnapshotInfo
**psn_info
)
1226 BlockDriver
*drv
= bs
->drv
;
1229 if (!drv
->bdrv_snapshot_list
)
1231 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1234 #define NB_SUFFIXES 4
1236 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1238 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1243 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1246 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1247 if (size
< (10 * base
)) {
1248 snprintf(buf
, buf_size
, "%0.1f%c",
1249 (double)size
/ base
,
1252 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1253 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1254 ((size
+ (base
>> 1)) / base
),
1264 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1266 char buf1
[128], date_buf
[128], clock_buf
[128];
1276 snprintf(buf
, buf_size
,
1277 "%-10s%-20s%7s%20s%15s",
1278 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1282 ptm
= localtime(&ti
);
1283 strftime(date_buf
, sizeof(date_buf
),
1284 "%Y-%m-%d %H:%M:%S", ptm
);
1286 localtime_r(&ti
, &tm
);
1287 strftime(date_buf
, sizeof(date_buf
),
1288 "%Y-%m-%d %H:%M:%S", &tm
);
1290 secs
= sn
->vm_clock_nsec
/ 1000000000;
1291 snprintf(clock_buf
, sizeof(clock_buf
),
1292 "%02d:%02d:%02d.%03d",
1294 (int)((secs
/ 60) % 60),
1296 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1297 snprintf(buf
, buf_size
,
1298 "%-10s%-20s%7s%20s%15s",
1299 sn
->id_str
, sn
->name
,
1300 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1308 /**************************************************************/
1311 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1312 QEMUIOVector
*qiov
, int nb_sectors
,
1313 BlockDriverCompletionFunc
*cb
, void *opaque
)
1315 BlockDriver
*drv
= bs
->drv
;
1316 BlockDriverAIOCB
*ret
;
1320 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1323 ret
= drv
->bdrv_aio_readv(bs
, sector_num
, qiov
, nb_sectors
,
1327 /* Update stats even though technically transfer has not happened. */
1328 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1335 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1336 QEMUIOVector
*qiov
, int nb_sectors
,
1337 BlockDriverCompletionFunc
*cb
, void *opaque
)
1339 BlockDriver
*drv
= bs
->drv
;
1340 BlockDriverAIOCB
*ret
;
1346 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1349 ret
= drv
->bdrv_aio_writev(bs
, sector_num
, qiov
, nb_sectors
,
1353 /* Update stats even though technically transfer has not happened. */
1354 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1361 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1363 acb
->pool
->cancel(acb
);
1367 /**************************************************************/
1368 /* async block device emulation */
1370 typedef struct BlockDriverAIOCBSync
{
1371 BlockDriverAIOCB common
;
1374 /* vector translation state */
1378 } BlockDriverAIOCBSync
;
1380 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1382 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1383 qemu_bh_delete(acb
->bh
);
1384 qemu_aio_release(acb
);
1387 static AIOPool bdrv_em_aio_pool
= {
1388 .aiocb_size
= sizeof(BlockDriverAIOCBSync
),
1389 .cancel
= bdrv_aio_cancel_em
,
1392 static void bdrv_aio_bh_cb(void *opaque
)
1394 BlockDriverAIOCBSync
*acb
= opaque
;
1397 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
1398 qemu_vfree(acb
->bounce
);
1399 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1400 qemu_bh_delete(acb
->bh
);
1401 qemu_aio_release(acb
);
1404 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1408 BlockDriverCompletionFunc
*cb
,
1413 BlockDriverAIOCBSync
*acb
;
1415 acb
= qemu_aio_get(&bdrv_em_aio_pool
, bs
, cb
, opaque
);
1416 acb
->is_write
= is_write
;
1418 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
1421 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1424 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
1425 acb
->ret
= bdrv_write(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1427 acb
->ret
= bdrv_read(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1430 qemu_bh_schedule(acb
->bh
);
1432 return &acb
->common
;
1435 static BlockDriverAIOCB
*bdrv_aio_readv_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
, 0);
1442 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
1443 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1444 BlockDriverCompletionFunc
*cb
, void *opaque
)
1446 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
1449 /**************************************************************/
1450 /* sync block device emulation */
1452 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1454 *(int *)opaque
= ret
;
1457 #define NOT_DONE 0x7fffffff
1459 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1460 uint8_t *buf
, int nb_sectors
)
1463 BlockDriverAIOCB
*acb
;
1467 async_ret
= NOT_DONE
;
1468 iov
.iov_base
= (void *)buf
;
1469 iov
.iov_len
= nb_sectors
* 512;
1470 qemu_iovec_init_external(&qiov
, &iov
, 1);
1471 acb
= bdrv_aio_readv(bs
, sector_num
, &qiov
, nb_sectors
,
1472 bdrv_rw_em_cb
, &async_ret
);
1476 while (async_ret
== NOT_DONE
) {
1483 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1484 const uint8_t *buf
, int nb_sectors
)
1487 BlockDriverAIOCB
*acb
;
1491 async_ret
= NOT_DONE
;
1492 iov
.iov_base
= (void *)buf
;
1493 iov
.iov_len
= nb_sectors
* 512;
1494 qemu_iovec_init_external(&qiov
, &iov
, 1);
1495 acb
= bdrv_aio_writev(bs
, sector_num
, &qiov
, nb_sectors
,
1496 bdrv_rw_em_cb
, &async_ret
);
1499 while (async_ret
== NOT_DONE
) {
1505 void bdrv_init(void)
1507 module_call_init(MODULE_INIT_BLOCK
);
1510 void *qemu_aio_get(AIOPool
*pool
, BlockDriverState
*bs
,
1511 BlockDriverCompletionFunc
*cb
, void *opaque
)
1513 BlockDriverAIOCB
*acb
;
1515 if (pool
->free_aiocb
) {
1516 acb
= pool
->free_aiocb
;
1517 pool
->free_aiocb
= acb
->next
;
1519 acb
= qemu_mallocz(pool
->aiocb_size
);
1524 acb
->opaque
= opaque
;
1528 void qemu_aio_release(void *p
)
1530 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1531 AIOPool
*pool
= acb
->pool
;
1532 acb
->next
= pool
->free_aiocb
;
1533 pool
->free_aiocb
= acb
;
1536 /**************************************************************/
1537 /* removable device support */
1540 * Return TRUE if the media is present
1542 int bdrv_is_inserted(BlockDriverState
*bs
)
1544 BlockDriver
*drv
= bs
->drv
;
1548 if (!drv
->bdrv_is_inserted
)
1550 ret
= drv
->bdrv_is_inserted(bs
);
1555 * Return TRUE if the media changed since the last call to this
1556 * function. It is currently only used for floppy disks
1558 int bdrv_media_changed(BlockDriverState
*bs
)
1560 BlockDriver
*drv
= bs
->drv
;
1563 if (!drv
|| !drv
->bdrv_media_changed
)
1566 ret
= drv
->bdrv_media_changed(bs
);
1567 if (ret
== -ENOTSUP
)
1568 ret
= bs
->media_changed
;
1569 bs
->media_changed
= 0;
1574 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1576 int bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1578 BlockDriver
*drv
= bs
->drv
;
1585 if (!drv
|| !drv
->bdrv_eject
) {
1588 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1590 if (ret
== -ENOTSUP
) {
1599 int bdrv_is_locked(BlockDriverState
*bs
)
1605 * Lock or unlock the media (if it is locked, the user won't be able
1606 * to eject it manually).
1608 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1610 BlockDriver
*drv
= bs
->drv
;
1612 bs
->locked
= locked
;
1613 if (drv
&& drv
->bdrv_set_locked
) {
1614 drv
->bdrv_set_locked(bs
, locked
);
1618 /* needed for generic scsi interface */
1620 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1622 BlockDriver
*drv
= bs
->drv
;
1624 if (drv
&& drv
->bdrv_ioctl
)
1625 return drv
->bdrv_ioctl(bs
, req
, buf
);
1629 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
1630 unsigned long int req
, void *buf
,
1631 BlockDriverCompletionFunc
*cb
, void *opaque
)
1633 BlockDriver
*drv
= bs
->drv
;
1635 if (drv
&& drv
->bdrv_aio_ioctl
)
1636 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);
1640 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
1642 return qemu_memalign((bs
&& bs
->buffer_alignment
) ? bs
->buffer_alignment
: 512, size
);