2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "config-host.h"
26 /* include native header before sys-queue.h */
27 #include <sys/queue.h>
30 #include "qemu-common.h"
32 #include "block_int.h"
35 #include <sys/types.h>
37 #include <sys/ioctl.h>
48 #define SECTOR_SIZE (1 << SECTOR_BITS)
50 static AIOPool vectored_aio_pool
;
52 typedef struct BlockDriverAIOCBSync
{
53 BlockDriverAIOCB common
;
56 } BlockDriverAIOCBSync
;
58 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
59 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
60 BlockDriverCompletionFunc
*cb
, void *opaque
);
61 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
62 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
63 BlockDriverCompletionFunc
*cb
, void *opaque
);
64 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
65 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
66 uint8_t *buf
, int nb_sectors
);
67 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
68 const uint8_t *buf
, int nb_sectors
);
70 BlockDriverState
*bdrv_first
;
72 static BlockDriver
*first_drv
;
74 int path_is_absolute(const char *path
)
78 /* specific case for names like: "\\.\d:" */
79 if (*path
== '/' || *path
== '\\')
82 p
= strchr(path
, ':');
88 return (*p
== '/' || *p
== '\\');
94 /* if filename is absolute, just copy it to dest. Otherwise, build a
95 path to it by considering it is relative to base_path. URL are
97 void path_combine(char *dest
, int dest_size
,
98 const char *base_path
,
106 if (path_is_absolute(filename
)) {
107 pstrcpy(dest
, dest_size
, filename
);
109 p
= strchr(base_path
, ':');
114 p1
= strrchr(base_path
, '/');
118 p2
= strrchr(base_path
, '\\');
130 if (len
> dest_size
- 1)
132 memcpy(dest
, base_path
, len
);
134 pstrcat(dest
, dest_size
, filename
);
139 static void bdrv_register(BlockDriver
*bdrv
)
141 if (!bdrv
->bdrv_aio_read
) {
142 /* add AIO emulation layer */
143 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
144 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
145 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
146 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
147 } else if (!bdrv
->bdrv_read
) {
148 /* add synchronous IO emulation layer */
149 bdrv
->bdrv_read
= bdrv_read_em
;
150 bdrv
->bdrv_write
= bdrv_write_em
;
152 aio_pool_init(&bdrv
->aio_pool
, bdrv
->aiocb_size
, bdrv
->bdrv_aio_cancel
);
153 bdrv
->next
= first_drv
;
157 /* create a new block device (by default it is empty) */
158 BlockDriverState
*bdrv_new(const char *device_name
)
160 BlockDriverState
**pbs
, *bs
;
162 bs
= qemu_mallocz(sizeof(BlockDriverState
));
163 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
164 if (device_name
[0] != '\0') {
165 /* insert at the end */
174 BlockDriver
*bdrv_find_format(const char *format_name
)
177 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
178 if (!strcmp(drv1
->format_name
, format_name
))
184 int bdrv_create2(BlockDriver
*drv
,
185 const char *filename
, int64_t size_in_sectors
,
186 const char *backing_file
, const char *backing_format
,
189 if (drv
->bdrv_create2
)
190 return drv
->bdrv_create2(filename
, size_in_sectors
, backing_file
,
191 backing_format
, flags
);
192 if (drv
->bdrv_create
)
193 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
,
198 int bdrv_create(BlockDriver
*drv
,
199 const char *filename
, int64_t size_in_sectors
,
200 const char *backing_file
, int flags
)
202 if (!drv
->bdrv_create
)
204 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
208 void get_tmp_filename(char *filename
, int size
)
210 char temp_dir
[MAX_PATH
];
212 GetTempPath(MAX_PATH
, temp_dir
);
213 GetTempFileName(temp_dir
, "qem", 0, filename
);
216 void get_tmp_filename(char *filename
, int size
)
220 /* XXX: race condition possible */
221 tmpdir
= getenv("TMPDIR");
224 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
225 fd
= mkstemp(filename
);
231 static int is_windows_drive_prefix(const char *filename
)
233 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
234 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
238 static int is_windows_drive(const char *filename
)
240 if (is_windows_drive_prefix(filename
) &&
243 if (strstart(filename
, "\\\\.\\", NULL
) ||
244 strstart(filename
, "//./", NULL
))
250 static BlockDriver
*find_protocol(const char *filename
)
258 if (is_windows_drive(filename
) ||
259 is_windows_drive_prefix(filename
))
262 p
= strchr(filename
, ':');
266 if (len
> sizeof(protocol
) - 1)
267 len
= sizeof(protocol
) - 1;
268 memcpy(protocol
, filename
, len
);
269 protocol
[len
] = '\0';
270 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
271 if (drv1
->protocol_name
&&
272 !strcmp(drv1
->protocol_name
, protocol
))
278 /* XXX: force raw format if block or character device ? It would
279 simplify the BSD case */
280 static BlockDriver
*find_image_format(const char *filename
)
282 int ret
, score
, score_max
;
283 BlockDriver
*drv1
, *drv
;
285 BlockDriverState
*bs
;
287 /* detect host devices. By convention, /dev/cdrom[N] is always
288 recognized as a host CDROM */
289 if (strstart(filename
, "/dev/cdrom", NULL
))
290 return &bdrv_host_device
;
292 if (is_windows_drive(filename
))
293 return &bdrv_host_device
;
297 if (stat(filename
, &st
) >= 0 &&
298 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
299 return &bdrv_host_device
;
304 drv
= find_protocol(filename
);
305 /* no need to test disk image formats for vvfat */
306 if (drv
== &bdrv_vvfat
)
309 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
312 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
319 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
320 if (drv1
->bdrv_probe
) {
321 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
322 if (score
> score_max
) {
331 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
333 BlockDriverState
*bs
;
337 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
347 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
349 return bdrv_open2(bs
, filename
, flags
, NULL
);
352 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
356 char tmp_filename
[PATH_MAX
];
357 char backing_filename
[PATH_MAX
];
360 bs
->is_temporary
= 0;
364 if (flags
& BDRV_O_SNAPSHOT
) {
365 BlockDriverState
*bs1
;
369 /* if snapshot, we create a temporary backing file and open it
370 instead of opening 'filename' directly */
372 /* if there is a backing file, use it */
374 ret
= bdrv_open2(bs1
, filename
, 0, drv
);
379 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
381 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
386 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
388 /* Real path is meaningless for protocols */
390 snprintf(backing_filename
, sizeof(backing_filename
),
393 realpath(filename
, backing_filename
);
395 ret
= bdrv_create2(&bdrv_qcow2
, tmp_filename
,
396 total_size
, backing_filename
,
397 (drv
? drv
->format_name
: NULL
), 0);
401 filename
= tmp_filename
;
403 bs
->is_temporary
= 1;
406 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
407 if (flags
& BDRV_O_FILE
) {
408 drv
= find_protocol(filename
);
410 drv
= find_image_format(filename
);
414 goto unlink_and_fail
;
417 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
418 /* Note: for compatibility, we open disk image files as RDWR, and
419 RDONLY as fallback */
420 if (!(flags
& BDRV_O_FILE
))
421 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
423 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
424 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
425 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
426 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
430 qemu_free(bs
->opaque
);
434 if (bs
->is_temporary
)
438 if (drv
->bdrv_getlength
) {
439 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
442 if (bs
->is_temporary
) {
446 if (bs
->backing_file
[0] != '\0') {
447 /* if there is a backing file, use it */
448 BlockDriver
*back_drv
= NULL
;
449 bs
->backing_hd
= bdrv_new("");
450 path_combine(backing_filename
, sizeof(backing_filename
),
451 filename
, bs
->backing_file
);
452 if (bs
->backing_format
[0] != '\0')
453 back_drv
= bdrv_find_format(bs
->backing_format
);
454 ret
= bdrv_open2(bs
->backing_hd
, backing_filename
, open_flags
,
462 if (!bdrv_key_required(bs
)) {
463 /* call the change callback */
464 bs
->media_changed
= 1;
466 bs
->change_cb(bs
->change_opaque
);
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
)
507 /* commit COW file into the raw image */
508 int bdrv_commit(BlockDriverState
*bs
)
510 BlockDriver
*drv
= bs
->drv
;
511 int64_t i
, total_sectors
;
513 unsigned char sector
[512];
522 if (!bs
->backing_hd
) {
526 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
527 for (i
= 0; i
< total_sectors
;) {
528 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
529 for(j
= 0; j
< n
; j
++) {
530 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
534 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
544 if (drv
->bdrv_make_empty
)
545 return drv
->bdrv_make_empty(bs
);
550 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
555 if (!bdrv_is_inserted(bs
))
561 len
= bdrv_getlength(bs
);
563 if ((offset
+ size
) > len
)
569 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
574 /* Deal with byte accesses */
576 offset
= -sector_num
;
578 offset
= sector_num
* 512;
580 return bdrv_check_byte_request(bs
, offset
, nb_sectors
* 512);
583 /* return < 0 if error. See bdrv_write() for the return codes */
584 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
585 uint8_t *buf
, int nb_sectors
)
587 BlockDriver
*drv
= bs
->drv
;
591 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
594 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
597 /* Return < 0 if error. Important errors are:
598 -EIO generic I/O error (may happen for all errors)
599 -ENOMEDIUM No media inserted.
600 -EINVAL Invalid sector number or nb_sectors
601 -EACCES Trying to write a read-only device
603 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
604 const uint8_t *buf
, int nb_sectors
)
606 BlockDriver
*drv
= bs
->drv
;
611 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
614 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
617 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
618 void *buf
, int count1
)
620 uint8_t tmp_buf
[SECTOR_SIZE
];
621 int len
, nb_sectors
, count
;
625 /* first read to align to sector start */
626 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
629 sector_num
= offset
>> SECTOR_BITS
;
631 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
633 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
641 /* read the sectors "in place" */
642 nb_sectors
= count
>> SECTOR_BITS
;
643 if (nb_sectors
> 0) {
644 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
646 sector_num
+= nb_sectors
;
647 len
= nb_sectors
<< SECTOR_BITS
;
652 /* add data from the last sector */
654 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
656 memcpy(buf
, tmp_buf
, count
);
661 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
662 const void *buf
, int count1
)
664 uint8_t tmp_buf
[SECTOR_SIZE
];
665 int len
, nb_sectors
, count
;
669 /* first write to align to sector start */
670 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
673 sector_num
= offset
>> SECTOR_BITS
;
675 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
677 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
678 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
687 /* write the sectors "in place" */
688 nb_sectors
= count
>> SECTOR_BITS
;
689 if (nb_sectors
> 0) {
690 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
692 sector_num
+= nb_sectors
;
693 len
= nb_sectors
<< SECTOR_BITS
;
698 /* add data from the last sector */
700 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
702 memcpy(tmp_buf
, buf
, count
);
703 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
710 * Truncate file to 'offset' bytes (needed only for file protocols)
712 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
714 BlockDriver
*drv
= bs
->drv
;
717 if (!drv
->bdrv_truncate
)
719 return drv
->bdrv_truncate(bs
, offset
);
723 * Length of a file in bytes. Return < 0 if error or unknown.
725 int64_t bdrv_getlength(BlockDriverState
*bs
)
727 BlockDriver
*drv
= bs
->drv
;
730 if (!drv
->bdrv_getlength
) {
732 return bs
->total_sectors
* SECTOR_SIZE
;
734 return drv
->bdrv_getlength(bs
);
737 /* return 0 as number of sectors if no device present or error */
738 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
741 length
= bdrv_getlength(bs
);
745 length
= length
>> SECTOR_BITS
;
746 *nb_sectors_ptr
= length
;
750 uint8_t boot_ind
; /* 0x80 - active */
751 uint8_t head
; /* starting head */
752 uint8_t sector
; /* starting sector */
753 uint8_t cyl
; /* starting cylinder */
754 uint8_t sys_ind
; /* What partition type */
755 uint8_t end_head
; /* end head */
756 uint8_t end_sector
; /* end sector */
757 uint8_t end_cyl
; /* end cylinder */
758 uint32_t start_sect
; /* starting sector counting from 0 */
759 uint32_t nr_sects
; /* nr of sectors in partition */
760 } __attribute__((packed
));
762 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
763 static int guess_disk_lchs(BlockDriverState
*bs
,
764 int *pcylinders
, int *pheads
, int *psectors
)
767 int ret
, i
, heads
, sectors
, cylinders
;
772 bdrv_get_geometry(bs
, &nb_sectors
);
774 ret
= bdrv_read(bs
, 0, buf
, 1);
777 /* test msdos magic */
778 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
780 for(i
= 0; i
< 4; i
++) {
781 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
782 nr_sects
= le32_to_cpu(p
->nr_sects
);
783 if (nr_sects
&& p
->end_head
) {
784 /* We make the assumption that the partition terminates on
785 a cylinder boundary */
786 heads
= p
->end_head
+ 1;
787 sectors
= p
->end_sector
& 63;
790 cylinders
= nb_sectors
/ (heads
* sectors
);
791 if (cylinders
< 1 || cylinders
> 16383)
795 *pcylinders
= cylinders
;
797 printf("guessed geometry: LCHS=%d %d %d\n",
798 cylinders
, heads
, sectors
);
806 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
808 int translation
, lba_detected
= 0;
809 int cylinders
, heads
, secs
;
812 /* if a geometry hint is available, use it */
813 bdrv_get_geometry(bs
, &nb_sectors
);
814 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
815 translation
= bdrv_get_translation_hint(bs
);
816 if (cylinders
!= 0) {
821 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
823 /* if heads > 16, it means that a BIOS LBA
824 translation was active, so the default
825 hardware geometry is OK */
827 goto default_geometry
;
832 /* disable any translation to be in sync with
833 the logical geometry */
834 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
835 bdrv_set_translation_hint(bs
,
836 BIOS_ATA_TRANSLATION_NONE
);
841 /* if no geometry, use a standard physical disk geometry */
842 cylinders
= nb_sectors
/ (16 * 63);
844 if (cylinders
> 16383)
846 else if (cylinders
< 2)
851 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
852 if ((*pcyls
* *pheads
) <= 131072) {
853 bdrv_set_translation_hint(bs
,
854 BIOS_ATA_TRANSLATION_LARGE
);
856 bdrv_set_translation_hint(bs
,
857 BIOS_ATA_TRANSLATION_LBA
);
861 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
865 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
866 int cyls
, int heads
, int secs
)
873 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
876 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
877 type
== BDRV_TYPE_FLOPPY
));
880 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
882 bs
->translation
= translation
;
885 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
886 int *pcyls
, int *pheads
, int *psecs
)
893 int bdrv_get_type_hint(BlockDriverState
*bs
)
898 int bdrv_get_translation_hint(BlockDriverState
*bs
)
900 return bs
->translation
;
903 int bdrv_is_removable(BlockDriverState
*bs
)
905 return bs
->removable
;
908 int bdrv_is_read_only(BlockDriverState
*bs
)
910 return bs
->read_only
;
913 int bdrv_is_sg(BlockDriverState
*bs
)
918 /* XXX: no longer used */
919 void bdrv_set_change_cb(BlockDriverState
*bs
,
920 void (*change_cb
)(void *opaque
), void *opaque
)
922 bs
->change_cb
= change_cb
;
923 bs
->change_opaque
= opaque
;
926 int bdrv_is_encrypted(BlockDriverState
*bs
)
928 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
930 return bs
->encrypted
;
933 int bdrv_key_required(BlockDriverState
*bs
)
935 BlockDriverState
*backing_hd
= bs
->backing_hd
;
937 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
939 return (bs
->encrypted
&& !bs
->valid_key
);
942 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
945 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
946 ret
= bdrv_set_key(bs
->backing_hd
, key
);
952 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
954 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
957 } else if (!bs
->valid_key
) {
959 /* call the change callback now, we skipped it on open */
960 bs
->media_changed
= 1;
962 bs
->change_cb(bs
->change_opaque
);
967 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
972 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
976 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
981 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
982 it(opaque
, drv
->format_name
);
986 BlockDriverState
*bdrv_find(const char *name
)
988 BlockDriverState
*bs
;
990 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
991 if (!strcmp(name
, bs
->device_name
))
997 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
999 BlockDriverState
*bs
;
1001 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1006 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1008 return bs
->device_name
;
1011 void bdrv_flush(BlockDriverState
*bs
)
1013 if (bs
->drv
->bdrv_flush
)
1014 bs
->drv
->bdrv_flush(bs
);
1016 bdrv_flush(bs
->backing_hd
);
1019 void bdrv_flush_all(void)
1021 BlockDriverState
*bs
;
1023 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1024 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1025 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1030 * Returns true iff the specified sector is present in the disk image. Drivers
1031 * not implementing the functionality are assumed to not support backing files,
1032 * hence all their sectors are reported as allocated.
1034 * 'pnum' is set to the number of sectors (including and immediately following
1035 * the specified sector) that are known to be in the same
1036 * allocated/unallocated state.
1038 * 'nb_sectors' is the max value 'pnum' should be set to.
1040 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1044 if (!bs
->drv
->bdrv_is_allocated
) {
1045 if (sector_num
>= bs
->total_sectors
) {
1049 n
= bs
->total_sectors
- sector_num
;
1050 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1053 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1056 void bdrv_info(Monitor
*mon
)
1058 BlockDriverState
*bs
;
1060 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1061 monitor_printf(mon
, "%s:", bs
->device_name
);
1062 monitor_printf(mon
, " type=");
1065 monitor_printf(mon
, "hd");
1067 case BDRV_TYPE_CDROM
:
1068 monitor_printf(mon
, "cdrom");
1070 case BDRV_TYPE_FLOPPY
:
1071 monitor_printf(mon
, "floppy");
1074 monitor_printf(mon
, " removable=%d", bs
->removable
);
1075 if (bs
->removable
) {
1076 monitor_printf(mon
, " locked=%d", bs
->locked
);
1079 monitor_printf(mon
, " file=");
1080 monitor_print_filename(mon
, bs
->filename
);
1081 if (bs
->backing_file
[0] != '\0') {
1082 monitor_printf(mon
, " backing_file=");
1083 monitor_print_filename(mon
, bs
->backing_file
);
1085 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1086 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1087 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1089 monitor_printf(mon
, " [not inserted]");
1091 monitor_printf(mon
, "\n");
1095 /* The "info blockstats" command. */
1096 void bdrv_info_stats(Monitor
*mon
)
1098 BlockDriverState
*bs
;
1100 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1101 monitor_printf(mon
, "%s:"
1102 " rd_bytes=%" PRIu64
1103 " wr_bytes=%" PRIu64
1104 " rd_operations=%" PRIu64
1105 " wr_operations=%" PRIu64
1108 bs
->rd_bytes
, bs
->wr_bytes
,
1109 bs
->rd_ops
, bs
->wr_ops
);
1113 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1115 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1116 return bs
->backing_file
;
1117 else if (bs
->encrypted
)
1118 return bs
->filename
;
1123 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1124 char *filename
, int filename_size
)
1126 if (!bs
->backing_hd
) {
1127 pstrcpy(filename
, filename_size
, "");
1129 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1133 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1134 const uint8_t *buf
, int nb_sectors
)
1136 BlockDriver
*drv
= bs
->drv
;
1139 if (!drv
->bdrv_write_compressed
)
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 /**************************************************************/
1156 /* handling of snapshots */
1158 int bdrv_snapshot_create(BlockDriverState
*bs
,
1159 QEMUSnapshotInfo
*sn_info
)
1161 BlockDriver
*drv
= bs
->drv
;
1164 if (!drv
->bdrv_snapshot_create
)
1166 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1169 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1170 const char *snapshot_id
)
1172 BlockDriver
*drv
= bs
->drv
;
1175 if (!drv
->bdrv_snapshot_goto
)
1177 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1180 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1182 BlockDriver
*drv
= bs
->drv
;
1185 if (!drv
->bdrv_snapshot_delete
)
1187 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1190 int bdrv_snapshot_list(BlockDriverState
*bs
,
1191 QEMUSnapshotInfo
**psn_info
)
1193 BlockDriver
*drv
= bs
->drv
;
1196 if (!drv
->bdrv_snapshot_list
)
1198 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1201 #define NB_SUFFIXES 4
1203 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1205 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1210 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1213 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1214 if (size
< (10 * base
)) {
1215 snprintf(buf
, buf_size
, "%0.1f%c",
1216 (double)size
/ base
,
1219 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1220 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1221 ((size
+ (base
>> 1)) / base
),
1231 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1233 char buf1
[128], date_buf
[128], clock_buf
[128];
1243 snprintf(buf
, buf_size
,
1244 "%-10s%-20s%7s%20s%15s",
1245 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1249 ptm
= localtime(&ti
);
1250 strftime(date_buf
, sizeof(date_buf
),
1251 "%Y-%m-%d %H:%M:%S", ptm
);
1253 localtime_r(&ti
, &tm
);
1254 strftime(date_buf
, sizeof(date_buf
),
1255 "%Y-%m-%d %H:%M:%S", &tm
);
1257 secs
= sn
->vm_clock_nsec
/ 1000000000;
1258 snprintf(clock_buf
, sizeof(clock_buf
),
1259 "%02d:%02d:%02d.%03d",
1261 (int)((secs
/ 60) % 60),
1263 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1264 snprintf(buf
, buf_size
,
1265 "%-10s%-20s%7s%20s%15s",
1266 sn
->id_str
, sn
->name
,
1267 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1275 /**************************************************************/
1278 typedef struct VectorTranslationAIOCB
{
1279 BlockDriverAIOCB common
;
1283 BlockDriverAIOCB
*aiocb
;
1284 } VectorTranslationAIOCB
;
1286 static void bdrv_aio_cancel_vector(BlockDriverAIOCB
*_acb
)
1288 VectorTranslationAIOCB
*acb
1289 = container_of(_acb
, VectorTranslationAIOCB
, common
);
1291 bdrv_aio_cancel(acb
->aiocb
);
1294 static void bdrv_aio_rw_vector_cb(void *opaque
, int ret
)
1296 VectorTranslationAIOCB
*s
= (VectorTranslationAIOCB
*)opaque
;
1299 qemu_iovec_from_buffer(s
->iov
, s
->bounce
, s
->iov
->size
);
1301 qemu_vfree(s
->bounce
);
1302 s
->common
.cb(s
->common
.opaque
, ret
);
1303 qemu_aio_release(s
);
1306 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1310 BlockDriverCompletionFunc
*cb
,
1315 VectorTranslationAIOCB
*s
= qemu_aio_get_pool(&vectored_aio_pool
, bs
,
1319 s
->bounce
= qemu_memalign(512, nb_sectors
* 512);
1320 s
->is_write
= is_write
;
1322 qemu_iovec_to_buffer(s
->iov
, s
->bounce
);
1323 s
->aiocb
= bdrv_aio_write(bs
, sector_num
, s
->bounce
, nb_sectors
,
1324 bdrv_aio_rw_vector_cb
, s
);
1326 s
->aiocb
= bdrv_aio_read(bs
, sector_num
, s
->bounce
, nb_sectors
,
1327 bdrv_aio_rw_vector_cb
, s
);
1330 qemu_vfree(s
->bounce
);
1331 qemu_aio_release(s
);
1337 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1338 QEMUIOVector
*iov
, int nb_sectors
,
1339 BlockDriverCompletionFunc
*cb
, void *opaque
)
1341 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1344 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1348 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1349 QEMUIOVector
*iov
, int nb_sectors
,
1350 BlockDriverCompletionFunc
*cb
, void *opaque
)
1352 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1355 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1359 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1360 uint8_t *buf
, int nb_sectors
,
1361 BlockDriverCompletionFunc
*cb
, void *opaque
)
1363 BlockDriver
*drv
= bs
->drv
;
1364 BlockDriverAIOCB
*ret
;
1368 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1371 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1374 /* Update stats even though technically transfer has not happened. */
1375 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1382 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1383 const uint8_t *buf
, int nb_sectors
,
1384 BlockDriverCompletionFunc
*cb
, void *opaque
)
1386 BlockDriver
*drv
= bs
->drv
;
1387 BlockDriverAIOCB
*ret
;
1393 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1396 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1399 /* Update stats even though technically transfer has not happened. */
1400 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1407 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1409 acb
->pool
->cancel(acb
);
1413 /**************************************************************/
1414 /* async block device emulation */
1416 static void bdrv_aio_bh_cb(void *opaque
)
1418 BlockDriverAIOCBSync
*acb
= opaque
;
1419 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1420 qemu_aio_release(acb
);
1423 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1424 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1425 BlockDriverCompletionFunc
*cb
, void *opaque
)
1427 BlockDriverAIOCBSync
*acb
;
1430 acb
= qemu_aio_get(bs
, cb
, opaque
);
1432 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1433 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1435 qemu_bh_schedule(acb
->bh
);
1436 return &acb
->common
;
1439 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1440 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1441 BlockDriverCompletionFunc
*cb
, void *opaque
)
1443 BlockDriverAIOCBSync
*acb
;
1446 acb
= qemu_aio_get(bs
, cb
, opaque
);
1448 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1449 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1451 qemu_bh_schedule(acb
->bh
);
1452 return &acb
->common
;
1455 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1457 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1458 qemu_bh_cancel(acb
->bh
);
1459 qemu_aio_release(acb
);
1462 /**************************************************************/
1463 /* sync block device emulation */
1465 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1467 *(int *)opaque
= ret
;
1470 #define NOT_DONE 0x7fffffff
1472 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1473 uint8_t *buf
, int nb_sectors
)
1476 BlockDriverAIOCB
*acb
;
1478 async_ret
= NOT_DONE
;
1479 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1480 bdrv_rw_em_cb
, &async_ret
);
1484 while (async_ret
== NOT_DONE
) {
1491 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1492 const uint8_t *buf
, int nb_sectors
)
1495 BlockDriverAIOCB
*acb
;
1497 async_ret
= NOT_DONE
;
1498 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1499 bdrv_rw_em_cb
, &async_ret
);
1502 while (async_ret
== NOT_DONE
) {
1508 void bdrv_init(void)
1510 aio_pool_init(&vectored_aio_pool
, sizeof(VectorTranslationAIOCB
),
1511 bdrv_aio_cancel_vector
);
1513 bdrv_register(&bdrv_raw
);
1514 bdrv_register(&bdrv_host_device
);
1516 bdrv_register(&bdrv_cow
);
1518 bdrv_register(&bdrv_qcow
);
1519 bdrv_register(&bdrv_vmdk
);
1520 bdrv_register(&bdrv_cloop
);
1521 bdrv_register(&bdrv_dmg
);
1522 bdrv_register(&bdrv_bochs
);
1523 bdrv_register(&bdrv_vpc
);
1524 bdrv_register(&bdrv_vvfat
);
1525 bdrv_register(&bdrv_qcow2
);
1526 bdrv_register(&bdrv_parallels
);
1527 bdrv_register(&bdrv_nbd
);
1530 void aio_pool_init(AIOPool
*pool
, int aiocb_size
,
1531 void (*cancel
)(BlockDriverAIOCB
*acb
))
1533 pool
->aiocb_size
= aiocb_size
;
1534 pool
->cancel
= cancel
;
1535 pool
->free_aiocb
= NULL
;
1538 void *qemu_aio_get_pool(AIOPool
*pool
, BlockDriverState
*bs
,
1539 BlockDriverCompletionFunc
*cb
, void *opaque
)
1541 BlockDriverAIOCB
*acb
;
1543 if (pool
->free_aiocb
) {
1544 acb
= pool
->free_aiocb
;
1545 pool
->free_aiocb
= acb
->next
;
1547 acb
= qemu_mallocz(pool
->aiocb_size
);
1552 acb
->opaque
= opaque
;
1556 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1559 return qemu_aio_get_pool(&bs
->drv
->aio_pool
, bs
, cb
, opaque
);
1562 void qemu_aio_release(void *p
)
1564 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1565 AIOPool
*pool
= acb
->pool
;
1566 acb
->next
= pool
->free_aiocb
;
1567 pool
->free_aiocb
= acb
;
1570 /**************************************************************/
1571 /* removable device support */
1574 * Return TRUE if the media is present
1576 int bdrv_is_inserted(BlockDriverState
*bs
)
1578 BlockDriver
*drv
= bs
->drv
;
1582 if (!drv
->bdrv_is_inserted
)
1584 ret
= drv
->bdrv_is_inserted(bs
);
1589 * Return TRUE if the media changed since the last call to this
1590 * function. It is currently only used for floppy disks
1592 int bdrv_media_changed(BlockDriverState
*bs
)
1594 BlockDriver
*drv
= bs
->drv
;
1597 if (!drv
|| !drv
->bdrv_media_changed
)
1600 ret
= drv
->bdrv_media_changed(bs
);
1601 if (ret
== -ENOTSUP
)
1602 ret
= bs
->media_changed
;
1603 bs
->media_changed
= 0;
1608 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1610 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1612 BlockDriver
*drv
= bs
->drv
;
1615 if (!drv
|| !drv
->bdrv_eject
) {
1618 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1620 if (ret
== -ENOTSUP
) {
1626 int bdrv_is_locked(BlockDriverState
*bs
)
1632 * Lock or unlock the media (if it is locked, the user won't be able
1633 * to eject it manually).
1635 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1637 BlockDriver
*drv
= bs
->drv
;
1639 bs
->locked
= locked
;
1640 if (drv
&& drv
->bdrv_set_locked
) {
1641 drv
->bdrv_set_locked(bs
, locked
);
1645 /* needed for generic scsi interface */
1647 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1649 BlockDriver
*drv
= bs
->drv
;
1651 if (drv
&& drv
->bdrv_ioctl
)
1652 return drv
->bdrv_ioctl(bs
, req
, buf
);
1656 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
1657 unsigned long int req
, void *buf
,
1658 BlockDriverCompletionFunc
*cb
, void *opaque
)
1660 BlockDriver
*drv
= bs
->drv
;
1662 if (drv
&& drv
->bdrv_aio_ioctl
)
1663 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);