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>
42 #define SECTOR_SIZE (1 << SECTOR_BITS)
44 typedef struct BlockDriverAIOCBSync
{
45 BlockDriverAIOCB common
;
48 } BlockDriverAIOCBSync
;
50 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
51 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
52 BlockDriverCompletionFunc
*cb
, void *opaque
);
53 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
54 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
55 BlockDriverCompletionFunc
*cb
, void *opaque
);
56 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
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
);
131 static void bdrv_register(BlockDriver
*bdrv
)
133 if (!bdrv
->bdrv_aio_read
) {
134 /* add AIO emulation layer */
135 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
136 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
137 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
138 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
139 } else if (!bdrv
->bdrv_read
&& !bdrv
->bdrv_pread
) {
140 /* add synchronous IO emulation layer */
141 bdrv
->bdrv_read
= bdrv_read_em
;
142 bdrv
->bdrv_write
= bdrv_write_em
;
144 bdrv
->next
= first_drv
;
148 /* create a new block device (by default it is empty) */
149 BlockDriverState
*bdrv_new(const char *device_name
)
151 BlockDriverState
**pbs
, *bs
;
153 bs
= qemu_mallocz(sizeof(BlockDriverState
));
154 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
155 if (device_name
[0] != '\0') {
156 /* insert at the end */
165 BlockDriver
*bdrv_find_format(const char *format_name
)
168 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
169 if (!strcmp(drv1
->format_name
, format_name
))
175 int bdrv_create(BlockDriver
*drv
,
176 const char *filename
, int64_t size_in_sectors
,
177 const char *backing_file
, int flags
)
179 if (!drv
->bdrv_create
)
181 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
185 void get_tmp_filename(char *filename
, int size
)
187 char temp_dir
[MAX_PATH
];
189 GetTempPath(MAX_PATH
, temp_dir
);
190 GetTempFileName(temp_dir
, "qem", 0, filename
);
193 void get_tmp_filename(char *filename
, int size
)
197 /* XXX: race condition possible */
198 tmpdir
= getenv("TMPDIR");
201 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
202 fd
= mkstemp(filename
);
208 static int is_windows_drive_prefix(const char *filename
)
210 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
211 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
215 static int is_windows_drive(const char *filename
)
217 if (is_windows_drive_prefix(filename
) &&
220 if (strstart(filename
, "\\\\.\\", NULL
) ||
221 strstart(filename
, "//./", NULL
))
227 static BlockDriver
*find_protocol(const char *filename
)
235 if (is_windows_drive(filename
) ||
236 is_windows_drive_prefix(filename
))
239 p
= strchr(filename
, ':');
243 if (len
> sizeof(protocol
) - 1)
244 len
= sizeof(protocol
) - 1;
245 memcpy(protocol
, filename
, len
);
246 protocol
[len
] = '\0';
247 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
248 if (drv1
->protocol_name
&&
249 !strcmp(drv1
->protocol_name
, protocol
))
255 /* XXX: force raw format if block or character device ? It would
256 simplify the BSD case */
257 static BlockDriver
*find_image_format(const char *filename
)
259 int ret
, score
, score_max
;
260 BlockDriver
*drv1
, *drv
;
262 BlockDriverState
*bs
;
264 /* detect host devices. By convention, /dev/cdrom[N] is always
265 recognized as a host CDROM */
266 if (strstart(filename
, "/dev/cdrom", NULL
))
267 return &bdrv_host_device
;
269 if (is_windows_drive(filename
))
270 return &bdrv_host_device
;
274 if (stat(filename
, &st
) >= 0 &&
275 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
276 return &bdrv_host_device
;
281 drv
= find_protocol(filename
);
282 /* no need to test disk image formats for vvfat */
283 if (drv
== &bdrv_vvfat
)
286 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
289 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
296 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
297 if (drv1
->bdrv_probe
) {
298 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
299 if (score
> score_max
) {
308 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
310 BlockDriverState
*bs
;
316 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 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
528 if (!bdrv_is_inserted(bs
))
534 len
= bdrv_getlength(bs
);
536 if ((offset
+ size
) > len
)
542 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
547 /* Deal with byte accesses */
549 offset
= -sector_num
;
551 offset
= sector_num
* 512;
553 return bdrv_check_byte_request(bs
, offset
, nb_sectors
* 512);
556 /* return < 0 if error. See bdrv_write() for the return codes */
557 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
558 uint8_t *buf
, int nb_sectors
)
560 BlockDriver
*drv
= bs
->drv
;
564 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
567 if (drv
->bdrv_pread
) {
569 len
= nb_sectors
* 512;
570 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
576 bs
->rd_bytes
+= (unsigned) len
;
581 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
585 /* Return < 0 if error. Important errors are:
586 -EIO generic I/O error (may happen for all errors)
587 -ENOMEDIUM No media inserted.
588 -EINVAL Invalid sector number or nb_sectors
589 -EACCES Trying to write a read-only device
591 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
592 const uint8_t *buf
, int nb_sectors
)
594 BlockDriver
*drv
= bs
->drv
;
599 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
602 if (drv
->bdrv_pwrite
) {
603 int ret
, len
, count
= 0;
604 len
= nb_sectors
* 512;
606 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
- count
);
608 printf("bdrv_write ret=%d\n", ret
);
613 } while (count
!= len
);
614 bs
->wr_bytes
+= (unsigned) len
;
618 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
621 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
622 uint8_t *buf
, int count1
)
624 uint8_t tmp_buf
[SECTOR_SIZE
];
625 int len
, nb_sectors
, count
;
629 /* first read to align to sector start */
630 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
633 sector_num
= offset
>> SECTOR_BITS
;
635 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
637 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
645 /* read the sectors "in place" */
646 nb_sectors
= count
>> SECTOR_BITS
;
647 if (nb_sectors
> 0) {
648 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
650 sector_num
+= nb_sectors
;
651 len
= nb_sectors
<< SECTOR_BITS
;
656 /* add data from the last sector */
658 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
660 memcpy(buf
, tmp_buf
, count
);
665 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
666 const uint8_t *buf
, int count1
)
668 uint8_t tmp_buf
[SECTOR_SIZE
];
669 int len
, nb_sectors
, count
;
673 /* first write to align to sector start */
674 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
677 sector_num
= offset
>> SECTOR_BITS
;
679 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
681 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
682 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
691 /* write the sectors "in place" */
692 nb_sectors
= count
>> SECTOR_BITS
;
693 if (nb_sectors
> 0) {
694 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
696 sector_num
+= nb_sectors
;
697 len
= nb_sectors
<< SECTOR_BITS
;
702 /* add data from the last sector */
704 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
706 memcpy(tmp_buf
, buf
, count
);
707 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
714 * Read with byte offsets (needed only for file protocols)
716 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
717 void *buf1
, int count1
)
719 BlockDriver
*drv
= bs
->drv
;
723 if (bdrv_check_byte_request(bs
, offset
, count1
))
726 if (!drv
->bdrv_pread
)
727 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
728 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
732 * Write with byte offsets (needed only for file protocols)
734 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
735 const void *buf1
, int count1
)
737 BlockDriver
*drv
= bs
->drv
;
741 if (bdrv_check_byte_request(bs
, offset
, count1
))
744 if (!drv
->bdrv_pwrite
)
745 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
746 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
750 * Truncate file to 'offset' bytes (needed only for file protocols)
752 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
754 BlockDriver
*drv
= bs
->drv
;
757 if (!drv
->bdrv_truncate
)
759 return drv
->bdrv_truncate(bs
, offset
);
763 * Length of a file in bytes. Return < 0 if error or unknown.
765 int64_t bdrv_getlength(BlockDriverState
*bs
)
767 BlockDriver
*drv
= bs
->drv
;
770 if (!drv
->bdrv_getlength
) {
772 return bs
->total_sectors
* SECTOR_SIZE
;
774 return drv
->bdrv_getlength(bs
);
777 /* return 0 as number of sectors if no device present or error */
778 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
781 length
= bdrv_getlength(bs
);
785 length
= length
>> SECTOR_BITS
;
786 *nb_sectors_ptr
= length
;
790 uint8_t boot_ind
; /* 0x80 - active */
791 uint8_t head
; /* starting head */
792 uint8_t sector
; /* starting sector */
793 uint8_t cyl
; /* starting cylinder */
794 uint8_t sys_ind
; /* What partition type */
795 uint8_t end_head
; /* end head */
796 uint8_t end_sector
; /* end sector */
797 uint8_t end_cyl
; /* end cylinder */
798 uint32_t start_sect
; /* starting sector counting from 0 */
799 uint32_t nr_sects
; /* nr of sectors in partition */
800 } __attribute__((packed
));
802 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
803 static int guess_disk_lchs(BlockDriverState
*bs
,
804 int *pcylinders
, int *pheads
, int *psectors
)
807 int ret
, i
, heads
, sectors
, cylinders
;
812 bdrv_get_geometry(bs
, &nb_sectors
);
814 ret
= bdrv_read(bs
, 0, buf
, 1);
817 /* test msdos magic */
818 if (buf
[510] != 0x55 || buf
[511] != 0xaa)
820 for(i
= 0; i
< 4; i
++) {
821 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
822 nr_sects
= le32_to_cpu(p
->nr_sects
);
823 if (nr_sects
&& p
->end_head
) {
824 /* We make the assumption that the partition terminates on
825 a cylinder boundary */
826 heads
= p
->end_head
+ 1;
827 sectors
= p
->end_sector
& 63;
830 cylinders
= nb_sectors
/ (heads
* sectors
);
831 if (cylinders
< 1 || cylinders
> 16383)
835 *pcylinders
= cylinders
;
837 printf("guessed geometry: LCHS=%d %d %d\n",
838 cylinders
, heads
, sectors
);
846 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
848 int translation
, lba_detected
= 0;
849 int cylinders
, heads
, secs
;
852 /* if a geometry hint is available, use it */
853 bdrv_get_geometry(bs
, &nb_sectors
);
854 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
855 translation
= bdrv_get_translation_hint(bs
);
856 if (cylinders
!= 0) {
861 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
863 /* if heads > 16, it means that a BIOS LBA
864 translation was active, so the default
865 hardware geometry is OK */
867 goto default_geometry
;
872 /* disable any translation to be in sync with
873 the logical geometry */
874 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
875 bdrv_set_translation_hint(bs
,
876 BIOS_ATA_TRANSLATION_NONE
);
881 /* if no geometry, use a standard physical disk geometry */
882 cylinders
= nb_sectors
/ (16 * 63);
884 if (cylinders
> 16383)
886 else if (cylinders
< 2)
891 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
892 if ((*pcyls
* *pheads
) <= 131072) {
893 bdrv_set_translation_hint(bs
,
894 BIOS_ATA_TRANSLATION_LARGE
);
896 bdrv_set_translation_hint(bs
,
897 BIOS_ATA_TRANSLATION_LBA
);
901 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
905 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
906 int cyls
, int heads
, int secs
)
913 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
916 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
917 type
== BDRV_TYPE_FLOPPY
));
920 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
922 bs
->translation
= translation
;
925 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
926 int *pcyls
, int *pheads
, int *psecs
)
933 int bdrv_get_type_hint(BlockDriverState
*bs
)
938 int bdrv_get_translation_hint(BlockDriverState
*bs
)
940 return bs
->translation
;
943 int bdrv_is_removable(BlockDriverState
*bs
)
945 return bs
->removable
;
948 int bdrv_is_read_only(BlockDriverState
*bs
)
950 return bs
->read_only
;
953 int bdrv_is_sg(BlockDriverState
*bs
)
958 /* XXX: no longer used */
959 void bdrv_set_change_cb(BlockDriverState
*bs
,
960 void (*change_cb
)(void *opaque
), void *opaque
)
962 bs
->change_cb
= change_cb
;
963 bs
->change_opaque
= opaque
;
966 int bdrv_is_encrypted(BlockDriverState
*bs
)
968 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
970 return bs
->encrypted
;
973 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
976 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
977 ret
= bdrv_set_key(bs
->backing_hd
, key
);
983 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
985 return bs
->drv
->bdrv_set_key(bs
, key
);
988 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
993 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
997 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
1002 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
1003 it(opaque
, drv
->format_name
);
1007 BlockDriverState
*bdrv_find(const char *name
)
1009 BlockDriverState
*bs
;
1011 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1012 if (!strcmp(name
, bs
->device_name
))
1018 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
1020 BlockDriverState
*bs
;
1022 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1023 it(opaque
, bs
->device_name
);
1027 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1029 return bs
->device_name
;
1032 void bdrv_flush(BlockDriverState
*bs
)
1034 if (bs
->drv
->bdrv_flush
)
1035 bs
->drv
->bdrv_flush(bs
);
1037 bdrv_flush(bs
->backing_hd
);
1040 void bdrv_flush_all(void)
1042 BlockDriverState
*bs
;
1044 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1045 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1046 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1051 * Returns true iff the specified sector is present in the disk image. Drivers
1052 * not implementing the functionality are assumed to not support backing files,
1053 * hence all their sectors are reported as allocated.
1055 * 'pnum' is set to the number of sectors (including and immediately following
1056 * the specified sector) that are known to be in the same
1057 * allocated/unallocated state.
1059 * 'nb_sectors' is the max value 'pnum' should be set to.
1061 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1065 if (!bs
->drv
->bdrv_is_allocated
) {
1066 if (sector_num
>= bs
->total_sectors
) {
1070 n
= bs
->total_sectors
- sector_num
;
1071 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1074 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1077 void bdrv_info(void)
1079 BlockDriverState
*bs
;
1081 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1082 term_printf("%s:", bs
->device_name
);
1083 term_printf(" type=");
1088 case BDRV_TYPE_CDROM
:
1089 term_printf("cdrom");
1091 case BDRV_TYPE_FLOPPY
:
1092 term_printf("floppy");
1095 term_printf(" removable=%d", bs
->removable
);
1096 if (bs
->removable
) {
1097 term_printf(" locked=%d", bs
->locked
);
1100 term_printf(" file=");
1101 term_print_filename(bs
->filename
);
1102 if (bs
->backing_file
[0] != '\0') {
1103 term_printf(" backing_file=");
1104 term_print_filename(bs
->backing_file
);
1106 term_printf(" ro=%d", bs
->read_only
);
1107 term_printf(" drv=%s", bs
->drv
->format_name
);
1109 term_printf(" encrypted");
1111 term_printf(" [not inserted]");
1117 /* The "info blockstats" command. */
1118 void bdrv_info_stats (void)
1120 BlockDriverState
*bs
;
1121 BlockDriverInfo bdi
;
1123 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1125 " rd_bytes=%" PRIu64
1126 " wr_bytes=%" PRIu64
1127 " rd_operations=%" PRIu64
1128 " wr_operations=%" PRIu64
1131 bs
->rd_bytes
, bs
->wr_bytes
,
1132 bs
->rd_ops
, bs
->wr_ops
);
1133 if (bdrv_get_info(bs
, &bdi
) == 0)
1134 term_printf(" high=%" PRId64
1135 " bytes_free=%" PRId64
,
1136 bdi
.highest_alloc
, bdi
.num_free_bytes
);
1141 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1142 char *filename
, int filename_size
)
1144 if (!bs
->backing_hd
) {
1145 pstrcpy(filename
, filename_size
, "");
1147 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1151 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1152 const uint8_t *buf
, int nb_sectors
)
1154 BlockDriver
*drv
= bs
->drv
;
1157 if (!drv
->bdrv_write_compressed
)
1159 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1162 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1164 BlockDriver
*drv
= bs
->drv
;
1167 if (!drv
->bdrv_get_info
)
1169 memset(bdi
, 0, sizeof(*bdi
));
1170 return drv
->bdrv_get_info(bs
, bdi
);
1173 /**************************************************************/
1174 /* handling of snapshots */
1176 int bdrv_snapshot_create(BlockDriverState
*bs
,
1177 QEMUSnapshotInfo
*sn_info
)
1179 BlockDriver
*drv
= bs
->drv
;
1182 if (!drv
->bdrv_snapshot_create
)
1184 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1187 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1188 const char *snapshot_id
)
1190 BlockDriver
*drv
= bs
->drv
;
1193 if (!drv
->bdrv_snapshot_goto
)
1195 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1198 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1200 BlockDriver
*drv
= bs
->drv
;
1203 if (!drv
->bdrv_snapshot_delete
)
1205 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1208 int bdrv_snapshot_list(BlockDriverState
*bs
,
1209 QEMUSnapshotInfo
**psn_info
)
1211 BlockDriver
*drv
= bs
->drv
;
1214 if (!drv
->bdrv_snapshot_list
)
1216 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1219 #define NB_SUFFIXES 4
1221 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1223 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1228 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1231 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1232 if (size
< (10 * base
)) {
1233 snprintf(buf
, buf_size
, "%0.1f%c",
1234 (double)size
/ base
,
1237 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1238 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1239 ((size
+ (base
>> 1)) / base
),
1249 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1251 char buf1
[128], date_buf
[128], clock_buf
[128];
1261 snprintf(buf
, buf_size
,
1262 "%-10s%-20s%7s%20s%15s",
1263 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1267 ptm
= localtime(&ti
);
1268 strftime(date_buf
, sizeof(date_buf
),
1269 "%Y-%m-%d %H:%M:%S", ptm
);
1271 localtime_r(&ti
, &tm
);
1272 strftime(date_buf
, sizeof(date_buf
),
1273 "%Y-%m-%d %H:%M:%S", &tm
);
1275 secs
= sn
->vm_clock_nsec
/ 1000000000;
1276 snprintf(clock_buf
, sizeof(clock_buf
),
1277 "%02d:%02d:%02d.%03d",
1279 (int)((secs
/ 60) % 60),
1281 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1282 snprintf(buf
, buf_size
,
1283 "%-10s%-20s%7s%20s%15s",
1284 sn
->id_str
, sn
->name
,
1285 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1293 /**************************************************************/
1296 typedef struct VectorTranslationState
{
1300 BlockDriverAIOCB
*aiocb
;
1301 BlockDriverAIOCB
*this_aiocb
;
1302 } VectorTranslationState
;
1304 static void bdrv_aio_rw_vector_cb(void *opaque
, int ret
)
1306 VectorTranslationState
*s
= opaque
;
1309 qemu_iovec_from_buffer(s
->iov
, s
->bounce
, s
->iov
->size
);
1311 qemu_vfree(s
->bounce
);
1312 s
->this_aiocb
->cb(s
->this_aiocb
->opaque
, ret
);
1313 qemu_aio_release(s
->this_aiocb
);
1316 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1320 BlockDriverCompletionFunc
*cb
,
1325 VectorTranslationState
*s
= qemu_mallocz(sizeof(*s
));
1326 BlockDriverAIOCB
*aiocb
= qemu_aio_get(bs
, cb
, opaque
);
1328 s
->this_aiocb
= aiocb
;
1330 s
->bounce
= qemu_memalign(512, nb_sectors
* 512);
1331 s
->is_write
= is_write
;
1333 qemu_iovec_to_buffer(s
->iov
, s
->bounce
);
1334 s
->aiocb
= bdrv_aio_write(bs
, sector_num
, s
->bounce
, nb_sectors
,
1335 bdrv_aio_rw_vector_cb
, s
);
1337 s
->aiocb
= bdrv_aio_read(bs
, sector_num
, s
->bounce
, nb_sectors
,
1338 bdrv_aio_rw_vector_cb
, s
);
1343 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1344 QEMUIOVector
*iov
, int nb_sectors
,
1345 BlockDriverCompletionFunc
*cb
, void *opaque
)
1347 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1350 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1354 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1355 QEMUIOVector
*iov
, int nb_sectors
,
1356 BlockDriverCompletionFunc
*cb
, void *opaque
)
1358 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1361 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1365 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1366 uint8_t *buf
, int nb_sectors
,
1367 BlockDriverCompletionFunc
*cb
, void *opaque
)
1369 BlockDriver
*drv
= bs
->drv
;
1370 BlockDriverAIOCB
*ret
;
1374 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1377 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1380 /* Update stats even though technically transfer has not happened. */
1381 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1388 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1389 const uint8_t *buf
, int nb_sectors
,
1390 BlockDriverCompletionFunc
*cb
, void *opaque
)
1392 BlockDriver
*drv
= bs
->drv
;
1393 BlockDriverAIOCB
*ret
;
1399 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1402 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1405 /* Update stats even though technically transfer has not happened. */
1406 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1413 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1415 BlockDriver
*drv
= acb
->bs
->drv
;
1417 if (acb
->cb
== bdrv_aio_rw_vector_cb
) {
1418 VectorTranslationState
*s
= acb
->opaque
;
1422 drv
->bdrv_aio_cancel(acb
);
1426 /**************************************************************/
1427 /* async block device emulation */
1429 static void bdrv_aio_bh_cb(void *opaque
)
1431 BlockDriverAIOCBSync
*acb
= opaque
;
1432 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1433 qemu_aio_release(acb
);
1436 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1437 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1438 BlockDriverCompletionFunc
*cb
, void *opaque
)
1440 BlockDriverAIOCBSync
*acb
;
1443 acb
= qemu_aio_get(bs
, cb
, opaque
);
1445 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1446 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1448 qemu_bh_schedule(acb
->bh
);
1449 return &acb
->common
;
1452 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1453 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1454 BlockDriverCompletionFunc
*cb
, void *opaque
)
1456 BlockDriverAIOCBSync
*acb
;
1459 acb
= qemu_aio_get(bs
, cb
, opaque
);
1461 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1462 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1464 qemu_bh_schedule(acb
->bh
);
1465 return &acb
->common
;
1468 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1470 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1471 qemu_bh_cancel(acb
->bh
);
1472 qemu_aio_release(acb
);
1475 /**************************************************************/
1476 /* sync block device emulation */
1478 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1480 *(int *)opaque
= ret
;
1483 #define NOT_DONE 0x7fffffff
1485 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1486 uint8_t *buf
, int nb_sectors
)
1489 BlockDriverAIOCB
*acb
;
1491 async_ret
= NOT_DONE
;
1492 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1493 bdrv_rw_em_cb
, &async_ret
);
1497 while (async_ret
== NOT_DONE
) {
1504 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1505 const uint8_t *buf
, int nb_sectors
)
1508 BlockDriverAIOCB
*acb
;
1510 async_ret
= NOT_DONE
;
1511 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1512 bdrv_rw_em_cb
, &async_ret
);
1515 while (async_ret
== NOT_DONE
) {
1521 void bdrv_init(void)
1523 bdrv_register(&bdrv_raw
);
1524 bdrv_register(&bdrv_host_device
);
1526 bdrv_register(&bdrv_cow
);
1528 bdrv_register(&bdrv_qcow
);
1529 bdrv_register(&bdrv_vmdk
);
1530 bdrv_register(&bdrv_cloop
);
1531 bdrv_register(&bdrv_dmg
);
1532 bdrv_register(&bdrv_bochs
);
1533 bdrv_register(&bdrv_vpc
);
1534 bdrv_register(&bdrv_vvfat
);
1535 bdrv_register(&bdrv_qcow2
);
1536 bdrv_register(&bdrv_parallels
);
1537 bdrv_register(&bdrv_nbd
);
1540 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1544 BlockDriverAIOCB
*acb
;
1547 if (drv
->free_aiocb
) {
1548 acb
= drv
->free_aiocb
;
1549 drv
->free_aiocb
= acb
->next
;
1551 acb
= qemu_mallocz(drv
->aiocb_size
);
1555 acb
->opaque
= opaque
;
1559 void qemu_aio_release(void *p
)
1561 BlockDriverAIOCB
*acb
= p
;
1562 BlockDriver
*drv
= acb
->bs
->drv
;
1563 acb
->next
= drv
->free_aiocb
;
1564 drv
->free_aiocb
= acb
;
1567 /**************************************************************/
1568 /* removable device support */
1571 * Return TRUE if the media is present
1573 int bdrv_is_inserted(BlockDriverState
*bs
)
1575 BlockDriver
*drv
= bs
->drv
;
1579 if (!drv
->bdrv_is_inserted
)
1581 ret
= drv
->bdrv_is_inserted(bs
);
1586 * Return TRUE if the media changed since the last call to this
1587 * function. It is currently only used for floppy disks
1589 int bdrv_media_changed(BlockDriverState
*bs
)
1591 BlockDriver
*drv
= bs
->drv
;
1594 if (!drv
|| !drv
->bdrv_media_changed
)
1597 ret
= drv
->bdrv_media_changed(bs
);
1598 if (ret
== -ENOTSUP
)
1599 ret
= bs
->media_changed
;
1600 bs
->media_changed
= 0;
1605 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1607 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1609 BlockDriver
*drv
= bs
->drv
;
1612 if (!drv
|| !drv
->bdrv_eject
) {
1615 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1617 if (ret
== -ENOTSUP
) {
1623 int bdrv_is_locked(BlockDriverState
*bs
)
1629 * Lock or unlock the media (if it is locked, the user won't be able
1630 * to eject it manually).
1632 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1634 BlockDriver
*drv
= bs
->drv
;
1636 bs
->locked
= locked
;
1637 if (drv
&& drv
->bdrv_set_locked
) {
1638 drv
->bdrv_set_locked(bs
, locked
);
1642 /* needed for generic scsi interface */
1644 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1646 BlockDriver
*drv
= bs
->drv
;
1648 if (drv
&& drv
->bdrv_ioctl
)
1649 return drv
->bdrv_ioctl(bs
, req
, buf
);