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 static int is_windows_drive(const char *filename
)
214 if (is_windows_drive_prefix(filename
) &&
217 if (strstart(filename
, "\\\\.\\", NULL
) ||
218 strstart(filename
, "//./", NULL
))
224 static BlockDriver
*find_protocol(const char *filename
)
232 if (is_windows_drive(filename
) ||
233 is_windows_drive_prefix(filename
))
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
))
252 /* XXX: force raw format if block or character device ? It would
253 simplify the BSD case */
254 static BlockDriver
*find_image_format(const char *filename
)
256 int ret
, score
, score_max
;
257 BlockDriver
*drv1
, *drv
;
259 BlockDriverState
*bs
;
261 /* detect host devices. By convention, /dev/cdrom[N] is always
262 recognized as a host CDROM */
263 if (strstart(filename
, "/dev/cdrom", NULL
))
264 return bdrv_find_format("host_device");
266 if (is_windows_drive(filename
))
267 return bdrv_find_format("host_device");
271 if (stat(filename
, &st
) >= 0 &&
272 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
273 return bdrv_find_format("host_device");
278 drv
= find_protocol(filename
);
279 /* no need to test disk image formats for vvfat */
280 if (drv
&& strcmp(drv
->format_name
, "vvfat") == 0)
283 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
286 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
293 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
294 if (drv1
->bdrv_probe
) {
295 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
296 if (score
> score_max
) {
305 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
307 BlockDriverState
*bs
;
311 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
321 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
323 return bdrv_open2(bs
, filename
, flags
, NULL
);
326 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
330 char tmp_filename
[PATH_MAX
];
331 char backing_filename
[PATH_MAX
];
334 bs
->is_temporary
= 0;
337 /* buffer_alignment defaulted to 512, drivers can change this value */
338 bs
->buffer_alignment
= 512;
340 if (flags
& BDRV_O_SNAPSHOT
) {
341 BlockDriverState
*bs1
;
344 BlockDriver
*bdrv_qcow2
;
345 QEMUOptionParameter
*options
;
347 /* if snapshot, we create a temporary backing file and open it
348 instead of opening 'filename' directly */
350 /* if there is a backing file, use it */
352 ret
= bdrv_open2(bs1
, filename
, 0, drv
);
357 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
359 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
364 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
366 /* Real path is meaningless for protocols */
368 snprintf(backing_filename
, sizeof(backing_filename
),
371 realpath(filename
, backing_filename
);
373 bdrv_qcow2
= bdrv_find_format("qcow2");
374 options
= parse_option_parameters("", bdrv_qcow2
->create_options
, NULL
);
376 set_option_parameter_int(options
, BLOCK_OPT_SIZE
, total_size
* 512);
377 set_option_parameter(options
, BLOCK_OPT_BACKING_FILE
, backing_filename
);
379 set_option_parameter(options
, BLOCK_OPT_BACKING_FMT
,
383 ret
= bdrv_create(bdrv_qcow2
, tmp_filename
, options
);
388 filename
= tmp_filename
;
390 bs
->is_temporary
= 1;
393 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
394 if (flags
& BDRV_O_FILE
) {
395 drv
= find_protocol(filename
);
397 drv
= find_image_format(filename
);
401 goto unlink_and_fail
;
404 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
405 /* Note: for compatibility, we open disk image files as RDWR, and
406 RDONLY as fallback */
407 if (!(flags
& BDRV_O_FILE
))
408 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
410 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
411 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
412 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
413 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
417 qemu_free(bs
->opaque
);
421 if (bs
->is_temporary
)
425 if (drv
->bdrv_getlength
) {
426 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
429 if (bs
->is_temporary
) {
433 if (bs
->backing_file
[0] != '\0') {
434 /* if there is a backing file, use it */
435 BlockDriver
*back_drv
= NULL
;
436 bs
->backing_hd
= bdrv_new("");
437 path_combine(backing_filename
, sizeof(backing_filename
),
438 filename
, bs
->backing_file
);
439 if (bs
->backing_format
[0] != '\0')
440 back_drv
= bdrv_find_format(bs
->backing_format
);
441 ret
= bdrv_open2(bs
->backing_hd
, backing_filename
, open_flags
,
449 if (!bdrv_key_required(bs
)) {
450 /* call the change callback */
451 bs
->media_changed
= 1;
453 bs
->change_cb(bs
->change_opaque
);
458 void bdrv_close(BlockDriverState
*bs
)
462 bdrv_delete(bs
->backing_hd
);
463 bs
->drv
->bdrv_close(bs
);
464 qemu_free(bs
->opaque
);
466 if (bs
->is_temporary
) {
467 unlink(bs
->filename
);
473 /* call the change callback */
474 bs
->media_changed
= 1;
476 bs
->change_cb(bs
->change_opaque
);
480 void bdrv_delete(BlockDriverState
*bs
)
482 BlockDriverState
**pbs
;
485 while (*pbs
!= bs
&& *pbs
!= NULL
)
495 * Run consistency checks on an image
497 * Returns the number of errors or -errno when an internal error occurs
499 int bdrv_check(BlockDriverState
*bs
)
501 if (bs
->drv
->bdrv_check
== NULL
) {
505 return bs
->drv
->bdrv_check(bs
);
508 /* commit COW file into the raw image */
509 int bdrv_commit(BlockDriverState
*bs
)
511 BlockDriver
*drv
= bs
->drv
;
512 int64_t i
, total_sectors
;
514 unsigned char sector
[512];
523 if (!bs
->backing_hd
) {
527 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
528 for (i
= 0; i
< total_sectors
;) {
529 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
530 for(j
= 0; j
< n
; j
++) {
531 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
535 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
545 if (drv
->bdrv_make_empty
)
546 return drv
->bdrv_make_empty(bs
);
551 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
556 if (!bdrv_is_inserted(bs
))
562 len
= bdrv_getlength(bs
);
567 if ((offset
> len
) || (len
- offset
< size
))
573 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
576 return bdrv_check_byte_request(bs
, sector_num
* 512, nb_sectors
* 512);
579 /* return < 0 if error. See bdrv_write() for the return codes */
580 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
581 uint8_t *buf
, int nb_sectors
)
583 BlockDriver
*drv
= bs
->drv
;
587 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
590 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
593 /* Return < 0 if error. Important errors are:
594 -EIO generic I/O error (may happen for all errors)
595 -ENOMEDIUM No media inserted.
596 -EINVAL Invalid sector number or nb_sectors
597 -EACCES Trying to write a read-only device
599 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
600 const uint8_t *buf
, int nb_sectors
)
602 BlockDriver
*drv
= bs
->drv
;
607 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
610 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
613 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
614 void *buf
, int count1
)
616 uint8_t tmp_buf
[SECTOR_SIZE
];
617 int len
, nb_sectors
, count
;
621 /* first read to align to sector start */
622 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
625 sector_num
= offset
>> SECTOR_BITS
;
627 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
629 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
637 /* read the sectors "in place" */
638 nb_sectors
= count
>> SECTOR_BITS
;
639 if (nb_sectors
> 0) {
640 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
642 sector_num
+= nb_sectors
;
643 len
= nb_sectors
<< SECTOR_BITS
;
648 /* add data from the last sector */
650 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
652 memcpy(buf
, tmp_buf
, count
);
657 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
658 const void *buf
, int count1
)
660 uint8_t tmp_buf
[SECTOR_SIZE
];
661 int len
, nb_sectors
, count
;
665 /* first write to align to sector start */
666 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
669 sector_num
= offset
>> SECTOR_BITS
;
671 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
673 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
674 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
683 /* write the sectors "in place" */
684 nb_sectors
= count
>> SECTOR_BITS
;
685 if (nb_sectors
> 0) {
686 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
688 sector_num
+= nb_sectors
;
689 len
= nb_sectors
<< SECTOR_BITS
;
694 /* add data from the last sector */
696 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
698 memcpy(tmp_buf
, buf
, count
);
699 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
706 * Truncate file to 'offset' bytes (needed only for file protocols)
708 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
710 BlockDriver
*drv
= bs
->drv
;
713 if (!drv
->bdrv_truncate
)
715 return drv
->bdrv_truncate(bs
, offset
);
719 * Length of a file in bytes. Return < 0 if error or unknown.
721 int64_t bdrv_getlength(BlockDriverState
*bs
)
723 BlockDriver
*drv
= bs
->drv
;
726 if (!drv
->bdrv_getlength
) {
728 return bs
->total_sectors
* SECTOR_SIZE
;
730 return drv
->bdrv_getlength(bs
);
733 /* return 0 as number of sectors if no device present or error */
734 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
737 length
= bdrv_getlength(bs
);
741 length
= length
>> SECTOR_BITS
;
742 *nb_sectors_ptr
= length
;
746 uint8_t boot_ind
; /* 0x80 - active */
747 uint8_t head
; /* starting head */
748 uint8_t sector
; /* starting sector */
749 uint8_t cyl
; /* starting cylinder */
750 uint8_t sys_ind
; /* What partition type */
751 uint8_t end_head
; /* end head */
752 uint8_t end_sector
; /* end sector */
753 uint8_t end_cyl
; /* end cylinder */
754 uint32_t start_sect
; /* starting sector counting from 0 */
755 uint32_t nr_sects
; /* nr of sectors in partition */
756 } __attribute__((packed
));
758 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
759 static int guess_disk_lchs(BlockDriverState
*bs
,
760 int *pcylinders
, int *pheads
, int *psectors
)
763 int ret
, i
, heads
, sectors
, cylinders
;
768 bdrv_get_geometry(bs
, &nb_sectors
);
770 ret
= bdrv_read(bs
, 0, buf
, 1);
773 /* test msdos magic */
774 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
776 for(i
= 0; i
< 4; i
++) {
777 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
778 nr_sects
= le32_to_cpu(p
->nr_sects
);
779 if (nr_sects
&& p
->end_head
) {
780 /* We make the assumption that the partition terminates on
781 a cylinder boundary */
782 heads
= p
->end_head
+ 1;
783 sectors
= p
->end_sector
& 63;
786 cylinders
= nb_sectors
/ (heads
* sectors
);
787 if (cylinders
< 1 || cylinders
> 16383)
791 *pcylinders
= cylinders
;
793 printf("guessed geometry: LCHS=%d %d %d\n",
794 cylinders
, heads
, sectors
);
802 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
804 int translation
, lba_detected
= 0;
805 int cylinders
, heads
, secs
;
808 /* if a geometry hint is available, use it */
809 bdrv_get_geometry(bs
, &nb_sectors
);
810 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
811 translation
= bdrv_get_translation_hint(bs
);
812 if (cylinders
!= 0) {
817 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
819 /* if heads > 16, it means that a BIOS LBA
820 translation was active, so the default
821 hardware geometry is OK */
823 goto default_geometry
;
828 /* disable any translation to be in sync with
829 the logical geometry */
830 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
831 bdrv_set_translation_hint(bs
,
832 BIOS_ATA_TRANSLATION_NONE
);
837 /* if no geometry, use a standard physical disk geometry */
838 cylinders
= nb_sectors
/ (16 * 63);
840 if (cylinders
> 16383)
842 else if (cylinders
< 2)
847 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
848 if ((*pcyls
* *pheads
) <= 131072) {
849 bdrv_set_translation_hint(bs
,
850 BIOS_ATA_TRANSLATION_LARGE
);
852 bdrv_set_translation_hint(bs
,
853 BIOS_ATA_TRANSLATION_LBA
);
857 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
861 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
862 int cyls
, int heads
, int secs
)
869 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
872 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
873 type
== BDRV_TYPE_FLOPPY
));
876 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
878 bs
->translation
= translation
;
881 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
882 int *pcyls
, int *pheads
, int *psecs
)
889 int bdrv_get_type_hint(BlockDriverState
*bs
)
894 int bdrv_get_translation_hint(BlockDriverState
*bs
)
896 return bs
->translation
;
899 int bdrv_is_removable(BlockDriverState
*bs
)
901 return bs
->removable
;
904 int bdrv_is_read_only(BlockDriverState
*bs
)
906 return bs
->read_only
;
909 int bdrv_is_sg(BlockDriverState
*bs
)
914 /* XXX: no longer used */
915 void bdrv_set_change_cb(BlockDriverState
*bs
,
916 void (*change_cb
)(void *opaque
), void *opaque
)
918 bs
->change_cb
= change_cb
;
919 bs
->change_opaque
= opaque
;
922 int bdrv_is_encrypted(BlockDriverState
*bs
)
924 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
926 return bs
->encrypted
;
929 int bdrv_key_required(BlockDriverState
*bs
)
931 BlockDriverState
*backing_hd
= bs
->backing_hd
;
933 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
935 return (bs
->encrypted
&& !bs
->valid_key
);
938 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
941 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
942 ret
= bdrv_set_key(bs
->backing_hd
, key
);
948 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
950 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
953 } else if (!bs
->valid_key
) {
955 /* call the change callback now, we skipped it on open */
956 bs
->media_changed
= 1;
958 bs
->change_cb(bs
->change_opaque
);
963 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
968 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
972 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
977 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
978 it(opaque
, drv
->format_name
);
982 BlockDriverState
*bdrv_find(const char *name
)
984 BlockDriverState
*bs
;
986 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
987 if (!strcmp(name
, bs
->device_name
))
993 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
995 BlockDriverState
*bs
;
997 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1002 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1004 return bs
->device_name
;
1007 void bdrv_flush(BlockDriverState
*bs
)
1011 if (bs
->drv
->bdrv_flush
)
1012 bs
->drv
->bdrv_flush(bs
);
1014 bdrv_flush(bs
->backing_hd
);
1017 void bdrv_flush_all(void)
1019 BlockDriverState
*bs
;
1021 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1022 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1023 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1028 * Returns true iff the specified sector is present in the disk image. Drivers
1029 * not implementing the functionality are assumed to not support backing files,
1030 * hence all their sectors are reported as allocated.
1032 * 'pnum' is set to the number of sectors (including and immediately following
1033 * the specified sector) that are known to be in the same
1034 * allocated/unallocated state.
1036 * 'nb_sectors' is the max value 'pnum' should be set to.
1038 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1042 if (!bs
->drv
->bdrv_is_allocated
) {
1043 if (sector_num
>= bs
->total_sectors
) {
1047 n
= bs
->total_sectors
- sector_num
;
1048 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1051 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1054 void bdrv_info(Monitor
*mon
)
1056 BlockDriverState
*bs
;
1058 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1059 monitor_printf(mon
, "%s:", bs
->device_name
);
1060 monitor_printf(mon
, " type=");
1063 monitor_printf(mon
, "hd");
1065 case BDRV_TYPE_CDROM
:
1066 monitor_printf(mon
, "cdrom");
1068 case BDRV_TYPE_FLOPPY
:
1069 monitor_printf(mon
, "floppy");
1072 monitor_printf(mon
, " removable=%d", bs
->removable
);
1073 if (bs
->removable
) {
1074 monitor_printf(mon
, " locked=%d", bs
->locked
);
1077 monitor_printf(mon
, " file=");
1078 monitor_print_filename(mon
, bs
->filename
);
1079 if (bs
->backing_file
[0] != '\0') {
1080 monitor_printf(mon
, " backing_file=");
1081 monitor_print_filename(mon
, bs
->backing_file
);
1083 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1084 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1085 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1087 monitor_printf(mon
, " [not inserted]");
1089 monitor_printf(mon
, "\n");
1093 /* The "info blockstats" command. */
1094 void bdrv_info_stats(Monitor
*mon
)
1096 BlockDriverState
*bs
;
1098 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1099 monitor_printf(mon
, "%s:"
1100 " rd_bytes=%" PRIu64
1101 " wr_bytes=%" PRIu64
1102 " rd_operations=%" PRIu64
1103 " wr_operations=%" PRIu64
1106 bs
->rd_bytes
, bs
->wr_bytes
,
1107 bs
->rd_ops
, bs
->wr_ops
);
1111 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1113 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1114 return bs
->backing_file
;
1115 else if (bs
->encrypted
)
1116 return bs
->filename
;
1121 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1122 char *filename
, int filename_size
)
1124 if (!bs
->backing_hd
) {
1125 pstrcpy(filename
, filename_size
, "");
1127 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1131 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1132 const uint8_t *buf
, int nb_sectors
)
1134 BlockDriver
*drv
= bs
->drv
;
1137 if (!drv
->bdrv_write_compressed
)
1139 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1141 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1144 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1146 BlockDriver
*drv
= bs
->drv
;
1149 if (!drv
->bdrv_get_info
)
1151 memset(bdi
, 0, sizeof(*bdi
));
1152 return drv
->bdrv_get_info(bs
, bdi
);
1155 int bdrv_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
, int64_t pos
, int size
)
1157 BlockDriver
*drv
= bs
->drv
;
1160 if (!drv
->bdrv_put_buffer
)
1162 return drv
->bdrv_put_buffer(bs
, buf
, pos
, size
);
1165 int bdrv_get_buffer(BlockDriverState
*bs
, uint8_t *buf
, int64_t pos
, int size
)
1167 BlockDriver
*drv
= bs
->drv
;
1170 if (!drv
->bdrv_get_buffer
)
1172 return drv
->bdrv_get_buffer(bs
, buf
, pos
, size
);
1175 /**************************************************************/
1176 /* handling of snapshots */
1178 int bdrv_snapshot_create(BlockDriverState
*bs
,
1179 QEMUSnapshotInfo
*sn_info
)
1181 BlockDriver
*drv
= bs
->drv
;
1184 if (!drv
->bdrv_snapshot_create
)
1186 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1189 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1190 const char *snapshot_id
)
1192 BlockDriver
*drv
= bs
->drv
;
1195 if (!drv
->bdrv_snapshot_goto
)
1197 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1200 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1202 BlockDriver
*drv
= bs
->drv
;
1205 if (!drv
->bdrv_snapshot_delete
)
1207 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1210 int bdrv_snapshot_list(BlockDriverState
*bs
,
1211 QEMUSnapshotInfo
**psn_info
)
1213 BlockDriver
*drv
= bs
->drv
;
1216 if (!drv
->bdrv_snapshot_list
)
1218 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1221 #define NB_SUFFIXES 4
1223 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1225 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1230 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1233 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1234 if (size
< (10 * base
)) {
1235 snprintf(buf
, buf_size
, "%0.1f%c",
1236 (double)size
/ base
,
1239 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1240 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1241 ((size
+ (base
>> 1)) / base
),
1251 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1253 char buf1
[128], date_buf
[128], clock_buf
[128];
1263 snprintf(buf
, buf_size
,
1264 "%-10s%-20s%7s%20s%15s",
1265 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1269 ptm
= localtime(&ti
);
1270 strftime(date_buf
, sizeof(date_buf
),
1271 "%Y-%m-%d %H:%M:%S", ptm
);
1273 localtime_r(&ti
, &tm
);
1274 strftime(date_buf
, sizeof(date_buf
),
1275 "%Y-%m-%d %H:%M:%S", &tm
);
1277 secs
= sn
->vm_clock_nsec
/ 1000000000;
1278 snprintf(clock_buf
, sizeof(clock_buf
),
1279 "%02d:%02d:%02d.%03d",
1281 (int)((secs
/ 60) % 60),
1283 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1284 snprintf(buf
, buf_size
,
1285 "%-10s%-20s%7s%20s%15s",
1286 sn
->id_str
, sn
->name
,
1287 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1295 /**************************************************************/
1298 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1299 QEMUIOVector
*qiov
, int nb_sectors
,
1300 BlockDriverCompletionFunc
*cb
, void *opaque
)
1302 BlockDriver
*drv
= bs
->drv
;
1303 BlockDriverAIOCB
*ret
;
1307 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1310 ret
= drv
->bdrv_aio_readv(bs
, sector_num
, qiov
, nb_sectors
,
1314 /* Update stats even though technically transfer has not happened. */
1315 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1322 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1323 QEMUIOVector
*qiov
, int nb_sectors
,
1324 BlockDriverCompletionFunc
*cb
, void *opaque
)
1326 BlockDriver
*drv
= bs
->drv
;
1327 BlockDriverAIOCB
*ret
;
1333 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1336 ret
= drv
->bdrv_aio_writev(bs
, sector_num
, qiov
, nb_sectors
,
1340 /* Update stats even though technically transfer has not happened. */
1341 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1348 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1350 acb
->pool
->cancel(acb
);
1354 /**************************************************************/
1355 /* async block device emulation */
1357 typedef struct BlockDriverAIOCBSync
{
1358 BlockDriverAIOCB common
;
1361 /* vector translation state */
1365 } BlockDriverAIOCBSync
;
1367 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1369 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1370 qemu_bh_cancel(acb
->bh
);
1371 qemu_aio_release(acb
);
1374 static AIOPool bdrv_em_aio_pool
= {
1375 .aiocb_size
= sizeof(BlockDriverAIOCBSync
),
1376 .cancel
= bdrv_aio_cancel_em
,
1379 static void bdrv_aio_bh_cb(void *opaque
)
1381 BlockDriverAIOCBSync
*acb
= opaque
;
1384 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
1385 qemu_vfree(acb
->bounce
);
1386 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1388 qemu_aio_release(acb
);
1391 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1395 BlockDriverCompletionFunc
*cb
,
1400 BlockDriverAIOCBSync
*acb
;
1402 acb
= qemu_aio_get(&bdrv_em_aio_pool
, bs
, cb
, opaque
);
1403 acb
->is_write
= is_write
;
1405 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
1408 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1411 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
1412 acb
->ret
= bdrv_write(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1414 acb
->ret
= bdrv_read(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1417 qemu_bh_schedule(acb
->bh
);
1419 return &acb
->common
;
1422 static BlockDriverAIOCB
*bdrv_aio_readv_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
, 0);
1429 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
1430 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1431 BlockDriverCompletionFunc
*cb
, void *opaque
)
1433 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
1436 /**************************************************************/
1437 /* sync block device emulation */
1439 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1441 *(int *)opaque
= ret
;
1444 #define NOT_DONE 0x7fffffff
1446 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1447 uint8_t *buf
, int nb_sectors
)
1450 BlockDriverAIOCB
*acb
;
1454 async_ret
= NOT_DONE
;
1455 iov
.iov_base
= (void *)buf
;
1456 iov
.iov_len
= nb_sectors
* 512;
1457 qemu_iovec_init_external(&qiov
, &iov
, 1);
1458 acb
= bdrv_aio_readv(bs
, sector_num
, &qiov
, nb_sectors
,
1459 bdrv_rw_em_cb
, &async_ret
);
1463 while (async_ret
== NOT_DONE
) {
1470 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1471 const uint8_t *buf
, int nb_sectors
)
1474 BlockDriverAIOCB
*acb
;
1478 async_ret
= NOT_DONE
;
1479 iov
.iov_base
= (void *)buf
;
1480 iov
.iov_len
= nb_sectors
* 512;
1481 qemu_iovec_init_external(&qiov
, &iov
, 1);
1482 acb
= bdrv_aio_writev(bs
, sector_num
, &qiov
, nb_sectors
,
1483 bdrv_rw_em_cb
, &async_ret
);
1486 while (async_ret
== NOT_DONE
) {
1492 void bdrv_init(void)
1494 module_call_init(MODULE_INIT_BLOCK
);
1497 void *qemu_aio_get(AIOPool
*pool
, BlockDriverState
*bs
,
1498 BlockDriverCompletionFunc
*cb
, void *opaque
)
1500 BlockDriverAIOCB
*acb
;
1502 if (pool
->free_aiocb
) {
1503 acb
= pool
->free_aiocb
;
1504 pool
->free_aiocb
= acb
->next
;
1506 acb
= qemu_mallocz(pool
->aiocb_size
);
1511 acb
->opaque
= opaque
;
1515 void qemu_aio_release(void *p
)
1517 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1518 AIOPool
*pool
= acb
->pool
;
1519 acb
->next
= pool
->free_aiocb
;
1520 pool
->free_aiocb
= acb
;
1523 /**************************************************************/
1524 /* removable device support */
1527 * Return TRUE if the media is present
1529 int bdrv_is_inserted(BlockDriverState
*bs
)
1531 BlockDriver
*drv
= bs
->drv
;
1535 if (!drv
->bdrv_is_inserted
)
1537 ret
= drv
->bdrv_is_inserted(bs
);
1542 * Return TRUE if the media changed since the last call to this
1543 * function. It is currently only used for floppy disks
1545 int bdrv_media_changed(BlockDriverState
*bs
)
1547 BlockDriver
*drv
= bs
->drv
;
1550 if (!drv
|| !drv
->bdrv_media_changed
)
1553 ret
= drv
->bdrv_media_changed(bs
);
1554 if (ret
== -ENOTSUP
)
1555 ret
= bs
->media_changed
;
1556 bs
->media_changed
= 0;
1561 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1563 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1565 BlockDriver
*drv
= bs
->drv
;
1568 if (!drv
|| !drv
->bdrv_eject
) {
1571 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1573 if (ret
== -ENOTSUP
) {
1579 int bdrv_is_locked(BlockDriverState
*bs
)
1585 * Lock or unlock the media (if it is locked, the user won't be able
1586 * to eject it manually).
1588 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1590 BlockDriver
*drv
= bs
->drv
;
1592 bs
->locked
= locked
;
1593 if (drv
&& drv
->bdrv_set_locked
) {
1594 drv
->bdrv_set_locked(bs
, locked
);
1598 /* needed for generic scsi interface */
1600 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1602 BlockDriver
*drv
= bs
->drv
;
1604 if (drv
&& drv
->bdrv_ioctl
)
1605 return drv
->bdrv_ioctl(bs
, req
, buf
);
1609 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
1610 unsigned long int req
, void *buf
,
1611 BlockDriverCompletionFunc
*cb
, void *opaque
)
1613 BlockDriver
*drv
= bs
->drv
;
1615 if (drv
&& drv
->bdrv_aio_ioctl
)
1616 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);
1620 void *qemu_blockalign(BlockDriverState
*bs
, size_t size
)
1622 return qemu_memalign((bs
&& bs
->buffer_alignment
) ? bs
->buffer_alignment
: 512, size
);