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>
43 #define SECTOR_SIZE (1 << SECTOR_BITS)
45 typedef struct BlockDriverAIOCBSync
{
46 BlockDriverAIOCB common
;
49 } BlockDriverAIOCBSync
;
51 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
52 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
53 BlockDriverCompletionFunc
*cb
, void *opaque
);
54 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
55 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
56 BlockDriverCompletionFunc
*cb
, void *opaque
);
57 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
58 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
59 uint8_t *buf
, int nb_sectors
);
60 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
61 const uint8_t *buf
, int nb_sectors
);
63 BlockDriverState
*bdrv_first
;
65 static BlockDriver
*first_drv
;
67 int path_is_absolute(const char *path
)
71 /* specific case for names like: "\\.\d:" */
72 if (*path
== '/' || *path
== '\\')
75 p
= strchr(path
, ':');
81 return (*p
== '/' || *p
== '\\');
87 /* if filename is absolute, just copy it to dest. Otherwise, build a
88 path to it by considering it is relative to base_path. URL are
90 void path_combine(char *dest
, int dest_size
,
91 const char *base_path
,
99 if (path_is_absolute(filename
)) {
100 pstrcpy(dest
, dest_size
, filename
);
102 p
= strchr(base_path
, ':');
107 p1
= strrchr(base_path
, '/');
111 p2
= strrchr(base_path
, '\\');
123 if (len
> dest_size
- 1)
125 memcpy(dest
, base_path
, len
);
127 pstrcat(dest
, dest_size
, filename
);
132 static void bdrv_register(BlockDriver
*bdrv
)
134 if (!bdrv
->bdrv_aio_read
) {
135 /* add AIO emulation layer */
136 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
137 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
138 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
139 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
140 } else if (!bdrv
->bdrv_read
&& !bdrv
->bdrv_pread
) {
141 /* add synchronous IO emulation layer */
142 bdrv
->bdrv_read
= bdrv_read_em
;
143 bdrv
->bdrv_write
= bdrv_write_em
;
145 bdrv
->next
= first_drv
;
149 /* create a new block device (by default it is empty) */
150 BlockDriverState
*bdrv_new(const char *device_name
)
152 BlockDriverState
**pbs
, *bs
;
154 bs
= qemu_mallocz(sizeof(BlockDriverState
));
155 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
156 if (device_name
[0] != '\0') {
157 /* insert at the end */
166 BlockDriver
*bdrv_find_format(const char *format_name
)
169 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
170 if (!strcmp(drv1
->format_name
, format_name
))
176 int bdrv_create(BlockDriver
*drv
,
177 const char *filename
, int64_t size_in_sectors
,
178 const char *backing_file
, int flags
)
180 if (!drv
->bdrv_create
)
182 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
186 void get_tmp_filename(char *filename
, int size
)
188 char temp_dir
[MAX_PATH
];
190 GetTempPath(MAX_PATH
, temp_dir
);
191 GetTempFileName(temp_dir
, "qem", 0, filename
);
194 void get_tmp_filename(char *filename
, int size
)
198 /* XXX: race condition possible */
199 tmpdir
= getenv("TMPDIR");
202 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
203 fd
= mkstemp(filename
);
209 static int is_windows_drive_prefix(const char *filename
)
211 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
212 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
216 static int is_windows_drive(const char *filename
)
218 if (is_windows_drive_prefix(filename
) &&
221 if (strstart(filename
, "\\\\.\\", NULL
) ||
222 strstart(filename
, "//./", NULL
))
228 static BlockDriver
*find_protocol(const char *filename
)
236 if (is_windows_drive(filename
) ||
237 is_windows_drive_prefix(filename
))
240 p
= strchr(filename
, ':');
244 if (len
> sizeof(protocol
) - 1)
245 len
= sizeof(protocol
) - 1;
246 memcpy(protocol
, filename
, len
);
247 protocol
[len
] = '\0';
248 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
249 if (drv1
->protocol_name
&&
250 !strcmp(drv1
->protocol_name
, protocol
))
256 /* XXX: force raw format if block or character device ? It would
257 simplify the BSD case */
258 static BlockDriver
*find_image_format(const char *filename
)
260 int ret
, score
, score_max
;
261 BlockDriver
*drv1
, *drv
;
263 BlockDriverState
*bs
;
265 /* detect host devices. By convention, /dev/cdrom[N] is always
266 recognized as a host CDROM */
267 if (strstart(filename
, "/dev/cdrom", NULL
))
268 return &bdrv_host_device
;
270 if (is_windows_drive(filename
))
271 return &bdrv_host_device
;
275 if (stat(filename
, &st
) >= 0 &&
276 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
277 return &bdrv_host_device
;
282 drv
= find_protocol(filename
);
283 /* no need to test disk image formats for vvfat */
284 if (drv
== &bdrv_vvfat
)
287 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
290 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
297 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
298 if (drv1
->bdrv_probe
) {
299 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
300 if (score
> score_max
) {
309 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
311 BlockDriverState
*bs
;
317 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
326 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
328 return bdrv_open2(bs
, filename
, flags
, NULL
);
331 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
335 char tmp_filename
[PATH_MAX
];
336 char backing_filename
[PATH_MAX
];
339 bs
->is_temporary
= 0;
342 if (flags
& BDRV_O_SNAPSHOT
) {
343 BlockDriverState
*bs1
;
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 */
355 if (bdrv_open(bs1
, filename
, 0) < 0) {
359 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
361 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
366 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
368 /* Real path is meaningless for protocols */
370 snprintf(backing_filename
, sizeof(backing_filename
),
373 realpath(filename
, backing_filename
);
375 if (bdrv_create(&bdrv_qcow2
, tmp_filename
,
376 total_size
, backing_filename
, 0) < 0) {
379 filename
= tmp_filename
;
380 bs
->is_temporary
= 1;
383 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
384 if (flags
& BDRV_O_FILE
) {
385 drv
= find_protocol(filename
);
390 drv
= find_image_format(filename
);
396 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
397 /* Note: for compatibility, we open disk image files as RDWR, and
398 RDONLY as fallback */
399 if (!(flags
& BDRV_O_FILE
))
400 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
402 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
403 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
404 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
405 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
409 qemu_free(bs
->opaque
);
414 if (drv
->bdrv_getlength
) {
415 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
418 if (bs
->is_temporary
) {
422 if (bs
->backing_file
[0] != '\0') {
423 /* if there is a backing file, use it */
424 bs
->backing_hd
= bdrv_new("");
425 if (!bs
->backing_hd
) {
430 path_combine(backing_filename
, sizeof(backing_filename
),
431 filename
, bs
->backing_file
);
432 if (bdrv_open(bs
->backing_hd
, backing_filename
, open_flags
) < 0)
436 /* call the change callback */
437 bs
->media_changed
= 1;
439 bs
->change_cb(bs
->change_opaque
);
444 void bdrv_close(BlockDriverState
*bs
)
448 bdrv_delete(bs
->backing_hd
);
449 bs
->drv
->bdrv_close(bs
);
450 qemu_free(bs
->opaque
);
452 if (bs
->is_temporary
) {
453 unlink(bs
->filename
);
459 /* call the change callback */
460 bs
->media_changed
= 1;
462 bs
->change_cb(bs
->change_opaque
);
466 void bdrv_delete(BlockDriverState
*bs
)
468 BlockDriverState
**pbs
;
471 while (*pbs
!= bs
&& *pbs
!= NULL
)
480 /* commit COW file into the raw image */
481 int bdrv_commit(BlockDriverState
*bs
)
483 BlockDriver
*drv
= bs
->drv
;
484 int64_t i
, total_sectors
;
486 unsigned char sector
[512];
495 if (!bs
->backing_hd
) {
499 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
500 for (i
= 0; i
< total_sectors
;) {
501 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
502 for(j
= 0; j
< n
; j
++) {
503 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
507 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
517 if (drv
->bdrv_make_empty
)
518 return drv
->bdrv_make_empty(bs
);
523 /* return < 0 if error. See bdrv_write() for the return codes */
524 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
525 uint8_t *buf
, int nb_sectors
)
527 BlockDriver
*drv
= bs
->drv
;
532 if (drv
->bdrv_pread
) {
534 len
= nb_sectors
* 512;
535 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
541 bs
->rd_bytes
+= (unsigned) len
;
546 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
550 /* Return < 0 if error. Important errors are:
551 -EIO generic I/O error (may happen for all errors)
552 -ENOMEDIUM No media inserted.
553 -EINVAL Invalid sector number or nb_sectors
554 -EACCES Trying to write a read-only device
556 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
557 const uint8_t *buf
, int nb_sectors
)
559 BlockDriver
*drv
= bs
->drv
;
564 if (drv
->bdrv_pwrite
) {
565 int ret
, len
, count
= 0;
566 len
= nb_sectors
* 512;
568 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
- count
);
570 printf("bdrv_write ret=%d\n", ret
);
575 } while (count
!= len
);
576 bs
->wr_bytes
+= (unsigned) len
;
580 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
583 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
584 uint8_t *buf
, int count1
)
586 uint8_t tmp_buf
[SECTOR_SIZE
];
587 int len
, nb_sectors
, count
;
591 /* first read to align to sector start */
592 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
595 sector_num
= offset
>> SECTOR_BITS
;
597 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
599 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
607 /* read the sectors "in place" */
608 nb_sectors
= count
>> SECTOR_BITS
;
609 if (nb_sectors
> 0) {
610 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
612 sector_num
+= nb_sectors
;
613 len
= nb_sectors
<< SECTOR_BITS
;
618 /* add data from the last sector */
620 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
622 memcpy(buf
, tmp_buf
, count
);
627 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
628 const uint8_t *buf
, int count1
)
630 uint8_t tmp_buf
[SECTOR_SIZE
];
631 int len
, nb_sectors
, count
;
635 /* first write to align to sector start */
636 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
639 sector_num
= offset
>> SECTOR_BITS
;
641 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
643 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
644 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
653 /* write the sectors "in place" */
654 nb_sectors
= count
>> SECTOR_BITS
;
655 if (nb_sectors
> 0) {
656 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
658 sector_num
+= nb_sectors
;
659 len
= nb_sectors
<< SECTOR_BITS
;
664 /* add data from the last sector */
666 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
668 memcpy(tmp_buf
, buf
, count
);
669 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
676 * Read with byte offsets (needed only for file protocols)
678 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
679 void *buf1
, int count1
)
681 BlockDriver
*drv
= bs
->drv
;
685 if (!drv
->bdrv_pread
)
686 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
687 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
691 * Write with byte offsets (needed only for file protocols)
693 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
694 const void *buf1
, int count1
)
696 BlockDriver
*drv
= bs
->drv
;
700 if (!drv
->bdrv_pwrite
)
701 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
702 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
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 buf
= qemu_memalign(512, 512);
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) {
782 for(i
= 0; i
< 4; i
++) {
783 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
784 nr_sects
= le32_to_cpu(p
->nr_sects
);
785 if (nr_sects
&& p
->end_head
) {
786 /* We make the assumption that the partition terminates on
787 a cylinder boundary */
788 heads
= p
->end_head
+ 1;
789 sectors
= p
->end_sector
& 63;
792 cylinders
= nb_sectors
/ (heads
* sectors
);
793 if (cylinders
< 1 || cylinders
> 16383)
797 *pcylinders
= cylinders
;
799 printf("guessed geometry: LCHS=%d %d %d\n",
800 cylinders
, heads
, sectors
);
810 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
812 int translation
, lba_detected
= 0;
813 int cylinders
, heads
, secs
;
816 /* if a geometry hint is available, use it */
817 bdrv_get_geometry(bs
, &nb_sectors
);
818 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
819 translation
= bdrv_get_translation_hint(bs
);
820 if (cylinders
!= 0) {
825 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
827 /* if heads > 16, it means that a BIOS LBA
828 translation was active, so the default
829 hardware geometry is OK */
831 goto default_geometry
;
836 /* disable any translation to be in sync with
837 the logical geometry */
838 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
839 bdrv_set_translation_hint(bs
,
840 BIOS_ATA_TRANSLATION_NONE
);
845 /* if no geometry, use a standard physical disk geometry */
846 cylinders
= nb_sectors
/ (16 * 63);
848 if (cylinders
> 16383)
850 else if (cylinders
< 2)
855 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
856 if ((*pcyls
* *pheads
) <= 131072) {
857 bdrv_set_translation_hint(bs
,
858 BIOS_ATA_TRANSLATION_LARGE
);
860 bdrv_set_translation_hint(bs
,
861 BIOS_ATA_TRANSLATION_LBA
);
865 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
869 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
870 int cyls
, int heads
, int secs
)
877 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
880 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
881 type
== BDRV_TYPE_FLOPPY
));
884 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
886 bs
->translation
= translation
;
889 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
890 int *pcyls
, int *pheads
, int *psecs
)
897 int bdrv_get_type_hint(BlockDriverState
*bs
)
902 int bdrv_get_translation_hint(BlockDriverState
*bs
)
904 return bs
->translation
;
907 int bdrv_is_removable(BlockDriverState
*bs
)
909 return bs
->removable
;
912 int bdrv_is_read_only(BlockDriverState
*bs
)
914 return bs
->read_only
;
917 int bdrv_is_sg(BlockDriverState
*bs
)
922 /* XXX: no longer used */
923 void bdrv_set_change_cb(BlockDriverState
*bs
,
924 void (*change_cb
)(void *opaque
), void *opaque
)
926 bs
->change_cb
= change_cb
;
927 bs
->change_opaque
= opaque
;
930 int bdrv_is_encrypted(BlockDriverState
*bs
)
932 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
934 return bs
->encrypted
;
937 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
940 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
941 ret
= bdrv_set_key(bs
->backing_hd
, key
);
947 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
949 return bs
->drv
->bdrv_set_key(bs
, key
);
952 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
957 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
961 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
966 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
967 it(opaque
, drv
->format_name
);
971 BlockDriverState
*bdrv_find(const char *name
)
973 BlockDriverState
*bs
;
975 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
976 if (!strcmp(name
, bs
->device_name
))
982 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
984 BlockDriverState
*bs
;
986 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
987 it(opaque
, bs
->device_name
);
991 const char *bdrv_get_device_name(BlockDriverState
*bs
)
993 return bs
->device_name
;
996 void bdrv_flush(BlockDriverState
*bs
)
998 if (bs
->drv
->bdrv_flush
)
999 bs
->drv
->bdrv_flush(bs
);
1001 bdrv_flush(bs
->backing_hd
);
1004 void bdrv_flush_all(void)
1006 BlockDriverState
*bs
;
1008 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1009 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1010 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1015 * Returns true iff the specified sector is present in the disk image. Drivers
1016 * not implementing the functionality are assumed to not support backing files,
1017 * hence all their sectors are reported as allocated.
1019 * 'pnum' is set to the number of sectors (including and immediately following
1020 * the specified sector) that are known to be in the same
1021 * allocated/unallocated state.
1023 * 'nb_sectors' is the max value 'pnum' should be set to.
1025 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1029 if (!bs
->drv
->bdrv_is_allocated
) {
1030 if (sector_num
>= bs
->total_sectors
) {
1034 n
= bs
->total_sectors
- sector_num
;
1035 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1038 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1041 void bdrv_info(void)
1043 BlockDriverState
*bs
;
1045 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1046 term_printf("%s:", bs
->device_name
);
1047 term_printf(" type=");
1052 case BDRV_TYPE_CDROM
:
1053 term_printf("cdrom");
1055 case BDRV_TYPE_FLOPPY
:
1056 term_printf("floppy");
1059 term_printf(" removable=%d", bs
->removable
);
1060 if (bs
->removable
) {
1061 term_printf(" locked=%d", bs
->locked
);
1064 term_printf(" file=");
1065 term_print_filename(bs
->filename
);
1066 if (bs
->backing_file
[0] != '\0') {
1067 term_printf(" backing_file=");
1068 term_print_filename(bs
->backing_file
);
1070 term_printf(" ro=%d", bs
->read_only
);
1071 term_printf(" drv=%s", bs
->drv
->format_name
);
1073 term_printf(" encrypted");
1075 term_printf(" [not inserted]");
1081 /* The "info blockstats" command. */
1082 void bdrv_info_stats (void)
1084 BlockDriverState
*bs
;
1085 BlockDriverInfo bdi
;
1087 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1089 " rd_bytes=%" PRIu64
1090 " wr_bytes=%" PRIu64
1091 " rd_operations=%" PRIu64
1092 " wr_operations=%" PRIu64
1095 bs
->rd_bytes
, bs
->wr_bytes
,
1096 bs
->rd_ops
, bs
->wr_ops
);
1097 if (bdrv_get_info(bs
, &bdi
) == 0)
1098 term_printf(" high=%" PRId64
1099 " bytes_free=%" PRId64
,
1100 bdi
.highest_alloc
, bdi
.num_free_bytes
);
1105 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1106 char *filename
, int filename_size
)
1108 if (!bs
->backing_hd
) {
1109 pstrcpy(filename
, filename_size
, "");
1111 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1115 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1116 const uint8_t *buf
, int nb_sectors
)
1118 BlockDriver
*drv
= bs
->drv
;
1121 if (!drv
->bdrv_write_compressed
)
1123 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1126 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1128 BlockDriver
*drv
= bs
->drv
;
1131 if (!drv
->bdrv_get_info
)
1133 memset(bdi
, 0, sizeof(*bdi
));
1134 return drv
->bdrv_get_info(bs
, bdi
);
1137 /**************************************************************/
1138 /* handling of snapshots */
1140 int bdrv_snapshot_create(BlockDriverState
*bs
,
1141 QEMUSnapshotInfo
*sn_info
)
1143 BlockDriver
*drv
= bs
->drv
;
1146 if (!drv
->bdrv_snapshot_create
)
1148 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1151 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1152 const char *snapshot_id
)
1154 BlockDriver
*drv
= bs
->drv
;
1157 if (!drv
->bdrv_snapshot_goto
)
1159 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1162 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1164 BlockDriver
*drv
= bs
->drv
;
1167 if (!drv
->bdrv_snapshot_delete
)
1169 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1172 int bdrv_snapshot_list(BlockDriverState
*bs
,
1173 QEMUSnapshotInfo
**psn_info
)
1175 BlockDriver
*drv
= bs
->drv
;
1178 if (!drv
->bdrv_snapshot_list
)
1180 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1183 #define NB_SUFFIXES 4
1185 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1187 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1192 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1195 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1196 if (size
< (10 * base
)) {
1197 snprintf(buf
, buf_size
, "%0.1f%c",
1198 (double)size
/ base
,
1201 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1202 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1203 ((size
+ (base
>> 1)) / base
),
1213 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1215 char buf1
[128], date_buf
[128], clock_buf
[128];
1225 snprintf(buf
, buf_size
,
1226 "%-10s%-20s%7s%20s%15s",
1227 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1231 ptm
= localtime(&ti
);
1232 strftime(date_buf
, sizeof(date_buf
),
1233 "%Y-%m-%d %H:%M:%S", ptm
);
1235 localtime_r(&ti
, &tm
);
1236 strftime(date_buf
, sizeof(date_buf
),
1237 "%Y-%m-%d %H:%M:%S", &tm
);
1239 secs
= sn
->vm_clock_nsec
/ 1000000000;
1240 snprintf(clock_buf
, sizeof(clock_buf
),
1241 "%02d:%02d:%02d.%03d",
1243 (int)((secs
/ 60) % 60),
1245 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1246 snprintf(buf
, buf_size
,
1247 "%-10s%-20s%7s%20s%15s",
1248 sn
->id_str
, sn
->name
,
1249 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1257 /**************************************************************/
1260 typedef struct VectorTranslationState
{
1264 BlockDriverAIOCB
*aiocb
;
1265 BlockDriverAIOCB
*this_aiocb
;
1266 } VectorTranslationState
;
1268 static void bdrv_aio_rw_vector_cb(void *opaque
, int ret
)
1270 VectorTranslationState
*s
= opaque
;
1273 qemu_iovec_from_buffer(s
->iov
, s
->bounce
, s
->iov
->size
);
1275 qemu_free(s
->bounce
);
1276 s
->this_aiocb
->cb(s
->this_aiocb
->opaque
, ret
);
1277 qemu_aio_release(s
->this_aiocb
);
1280 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1284 BlockDriverCompletionFunc
*cb
,
1289 VectorTranslationState
*s
= qemu_mallocz(sizeof(*s
));
1290 BlockDriverAIOCB
*aiocb
= qemu_aio_get(bs
, cb
, opaque
);
1292 s
->this_aiocb
= aiocb
;
1294 s
->bounce
= qemu_memalign(512, nb_sectors
* 512);
1295 s
->is_write
= is_write
;
1297 qemu_iovec_to_buffer(s
->iov
, s
->bounce
);
1298 s
->aiocb
= bdrv_aio_write(bs
, sector_num
, s
->bounce
, nb_sectors
,
1299 bdrv_aio_rw_vector_cb
, s
);
1301 s
->aiocb
= bdrv_aio_read(bs
, sector_num
, s
->bounce
, nb_sectors
,
1302 bdrv_aio_rw_vector_cb
, s
);
1307 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1308 QEMUIOVector
*iov
, int nb_sectors
,
1309 BlockDriverCompletionFunc
*cb
, void *opaque
)
1311 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1315 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1316 QEMUIOVector
*iov
, int nb_sectors
,
1317 BlockDriverCompletionFunc
*cb
, void *opaque
)
1319 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1323 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1324 uint8_t *buf
, int nb_sectors
,
1325 BlockDriverCompletionFunc
*cb
, void *opaque
)
1327 BlockDriver
*drv
= bs
->drv
;
1328 BlockDriverAIOCB
*ret
;
1333 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1336 /* Update stats even though technically transfer has not happened. */
1337 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1344 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1345 const uint8_t *buf
, int nb_sectors
,
1346 BlockDriverCompletionFunc
*cb
, void *opaque
)
1348 BlockDriver
*drv
= bs
->drv
;
1349 BlockDriverAIOCB
*ret
;
1356 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1359 /* Update stats even though technically transfer has not happened. */
1360 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1367 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1369 BlockDriver
*drv
= acb
->bs
->drv
;
1371 if (acb
->cb
== bdrv_aio_rw_vector_cb
) {
1372 VectorTranslationState
*s
= acb
->opaque
;
1376 drv
->bdrv_aio_cancel(acb
);
1380 /**************************************************************/
1381 /* async block device emulation */
1383 static void bdrv_aio_bh_cb(void *opaque
)
1385 BlockDriverAIOCBSync
*acb
= opaque
;
1386 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1387 qemu_aio_release(acb
);
1390 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1391 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1392 BlockDriverCompletionFunc
*cb
, void *opaque
)
1394 BlockDriverAIOCBSync
*acb
;
1397 acb
= qemu_aio_get(bs
, cb
, opaque
);
1399 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1400 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1402 qemu_bh_schedule(acb
->bh
);
1403 return &acb
->common
;
1406 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1407 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1408 BlockDriverCompletionFunc
*cb
, void *opaque
)
1410 BlockDriverAIOCBSync
*acb
;
1413 acb
= qemu_aio_get(bs
, cb
, opaque
);
1415 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1416 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1418 qemu_bh_schedule(acb
->bh
);
1419 return &acb
->common
;
1422 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1424 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1425 qemu_bh_cancel(acb
->bh
);
1426 qemu_aio_release(acb
);
1429 /**************************************************************/
1430 /* sync block device emulation */
1432 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1434 *(int *)opaque
= ret
;
1437 #define NOT_DONE 0x7fffffff
1439 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1440 uint8_t *buf
, int nb_sectors
)
1443 BlockDriverAIOCB
*acb
;
1445 async_ret
= NOT_DONE
;
1446 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1447 bdrv_rw_em_cb
, &async_ret
);
1451 while (async_ret
== NOT_DONE
) {
1458 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1459 const uint8_t *buf
, int nb_sectors
)
1462 BlockDriverAIOCB
*acb
;
1464 async_ret
= NOT_DONE
;
1465 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1466 bdrv_rw_em_cb
, &async_ret
);
1469 while (async_ret
== NOT_DONE
) {
1475 void bdrv_init(void)
1477 bdrv_register(&bdrv_raw
);
1478 bdrv_register(&bdrv_host_device
);
1480 bdrv_register(&bdrv_cow
);
1482 bdrv_register(&bdrv_qcow
);
1483 bdrv_register(&bdrv_vmdk
);
1484 bdrv_register(&bdrv_cloop
);
1485 bdrv_register(&bdrv_dmg
);
1486 bdrv_register(&bdrv_bochs
);
1487 bdrv_register(&bdrv_vpc
);
1488 bdrv_register(&bdrv_vvfat
);
1489 bdrv_register(&bdrv_qcow2
);
1490 bdrv_register(&bdrv_parallels
);
1491 bdrv_register(&bdrv_nbd
);
1494 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1498 BlockDriverAIOCB
*acb
;
1501 if (drv
->free_aiocb
) {
1502 acb
= drv
->free_aiocb
;
1503 drv
->free_aiocb
= acb
->next
;
1505 acb
= qemu_mallocz(drv
->aiocb_size
);
1509 acb
->opaque
= opaque
;
1513 void qemu_aio_release(void *p
)
1515 BlockDriverAIOCB
*acb
= p
;
1516 BlockDriver
*drv
= acb
->bs
->drv
;
1517 acb
->next
= drv
->free_aiocb
;
1518 drv
->free_aiocb
= acb
;
1521 /**************************************************************/
1522 /* removable device support */
1525 * Return TRUE if the media is present
1527 int bdrv_is_inserted(BlockDriverState
*bs
)
1529 BlockDriver
*drv
= bs
->drv
;
1533 if (!drv
->bdrv_is_inserted
)
1535 ret
= drv
->bdrv_is_inserted(bs
);
1540 * Return TRUE if the media changed since the last call to this
1541 * function. It is currently only used for floppy disks
1543 int bdrv_media_changed(BlockDriverState
*bs
)
1545 BlockDriver
*drv
= bs
->drv
;
1548 if (!drv
|| !drv
->bdrv_media_changed
)
1551 ret
= drv
->bdrv_media_changed(bs
);
1552 if (ret
== -ENOTSUP
)
1553 ret
= bs
->media_changed
;
1554 bs
->media_changed
= 0;
1559 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1561 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1563 BlockDriver
*drv
= bs
->drv
;
1566 if (!drv
|| !drv
->bdrv_eject
) {
1569 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1571 if (ret
== -ENOTSUP
) {
1577 int bdrv_is_locked(BlockDriverState
*bs
)
1583 * Lock or unlock the media (if it is locked, the user won't be able
1584 * to eject it manually).
1586 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1588 BlockDriver
*drv
= bs
->drv
;
1590 bs
->locked
= locked
;
1591 if (drv
&& drv
->bdrv_set_locked
) {
1592 drv
->bdrv_set_locked(bs
, locked
);
1596 /* needed for generic scsi interface */
1598 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1600 BlockDriver
*drv
= bs
->drv
;
1602 if (drv
&& drv
->bdrv_ioctl
)
1603 return drv
->bdrv_ioctl(bs
, req
, buf
);