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
));
157 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
158 if (device_name
[0] != '\0') {
159 /* insert at the end */
168 BlockDriver
*bdrv_find_format(const char *format_name
)
171 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
172 if (!strcmp(drv1
->format_name
, format_name
))
178 int bdrv_create(BlockDriver
*drv
,
179 const char *filename
, int64_t size_in_sectors
,
180 const char *backing_file
, int flags
)
182 if (!drv
->bdrv_create
)
184 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
188 void get_tmp_filename(char *filename
, int size
)
190 char temp_dir
[MAX_PATH
];
192 GetTempPath(MAX_PATH
, temp_dir
);
193 GetTempFileName(temp_dir
, "qem", 0, filename
);
196 void get_tmp_filename(char *filename
, int size
)
200 /* XXX: race condition possible */
201 tmpdir
= getenv("TMPDIR");
204 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
205 fd
= mkstemp(filename
);
211 static int is_windows_drive_prefix(const char *filename
)
213 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
214 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
218 static int is_windows_drive(const char *filename
)
220 if (is_windows_drive_prefix(filename
) &&
223 if (strstart(filename
, "\\\\.\\", NULL
) ||
224 strstart(filename
, "//./", NULL
))
230 static BlockDriver
*find_protocol(const char *filename
)
238 if (is_windows_drive(filename
) ||
239 is_windows_drive_prefix(filename
))
242 p
= strchr(filename
, ':');
246 if (len
> sizeof(protocol
) - 1)
247 len
= sizeof(protocol
) - 1;
248 memcpy(protocol
, filename
, len
);
249 protocol
[len
] = '\0';
250 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
251 if (drv1
->protocol_name
&&
252 !strcmp(drv1
->protocol_name
, protocol
))
258 /* XXX: force raw format if block or character device ? It would
259 simplify the BSD case */
260 static BlockDriver
*find_image_format(const char *filename
)
262 int ret
, score
, score_max
;
263 BlockDriver
*drv1
, *drv
;
265 BlockDriverState
*bs
;
267 /* detect host devices. By convention, /dev/cdrom[N] is always
268 recognized as a host CDROM */
269 if (strstart(filename
, "/dev/cdrom", NULL
))
270 return &bdrv_host_device
;
272 if (is_windows_drive(filename
))
273 return &bdrv_host_device
;
277 if (stat(filename
, &st
) >= 0 &&
278 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
279 return &bdrv_host_device
;
284 drv
= find_protocol(filename
);
285 /* no need to test disk image formats for vvfat */
286 if (drv
== &bdrv_vvfat
)
289 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
292 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
299 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
300 if (drv1
->bdrv_probe
) {
301 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
302 if (score
> score_max
) {
311 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
313 BlockDriverState
*bs
;
319 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
328 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
330 return bdrv_open2(bs
, filename
, flags
, NULL
);
333 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
337 char tmp_filename
[PATH_MAX
];
338 char backing_filename
[PATH_MAX
];
341 bs
->is_temporary
= 0;
344 if (flags
& BDRV_O_SNAPSHOT
) {
345 BlockDriverState
*bs1
;
349 /* if snapshot, we create a temporary backing file and open it
350 instead of opening 'filename' directly */
352 /* if there is a backing file, use it */
357 if (bdrv_open(bs1
, filename
, 0) < 0) {
361 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
363 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
368 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
370 /* Real path is meaningless for protocols */
372 snprintf(backing_filename
, sizeof(backing_filename
),
375 realpath(filename
, backing_filename
);
377 if (bdrv_create(&bdrv_qcow2
, tmp_filename
,
378 total_size
, backing_filename
, 0) < 0) {
381 filename
= tmp_filename
;
382 bs
->is_temporary
= 1;
385 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
386 if (flags
& BDRV_O_FILE
) {
387 drv
= find_protocol(filename
);
392 drv
= find_image_format(filename
);
398 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
399 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
401 /* Note: for compatibility, we open disk image files as RDWR, and
402 RDONLY as fallback */
403 if (!(flags
& BDRV_O_FILE
))
404 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
406 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
407 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
408 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
409 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
413 qemu_free(bs
->opaque
);
418 if (drv
->bdrv_getlength
) {
419 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
422 if (bs
->is_temporary
) {
426 if (bs
->backing_file
[0] != '\0') {
427 /* if there is a backing file, use it */
428 bs
->backing_hd
= bdrv_new("");
429 if (!bs
->backing_hd
) {
434 path_combine(backing_filename
, sizeof(backing_filename
),
435 filename
, bs
->backing_file
);
436 if (bdrv_open(bs
->backing_hd
, backing_filename
, open_flags
) < 0)
440 /* call the change callback */
441 bs
->media_changed
= 1;
443 bs
->change_cb(bs
->change_opaque
);
448 void bdrv_close(BlockDriverState
*bs
)
452 bdrv_delete(bs
->backing_hd
);
453 bs
->drv
->bdrv_close(bs
);
454 qemu_free(bs
->opaque
);
456 if (bs
->is_temporary
) {
457 unlink(bs
->filename
);
463 /* call the change callback */
464 bs
->media_changed
= 1;
466 bs
->change_cb(bs
->change_opaque
);
470 void bdrv_delete(BlockDriverState
*bs
)
472 BlockDriverState
**pbs
;
475 while (*pbs
!= bs
&& *pbs
!= NULL
)
484 /* commit COW file into the raw image */
485 int bdrv_commit(BlockDriverState
*bs
)
487 BlockDriver
*drv
= bs
->drv
;
488 int64_t i
, total_sectors
;
490 unsigned char sector
[512];
499 if (!bs
->backing_hd
) {
503 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
504 for (i
= 0; i
< total_sectors
;) {
505 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
506 for(j
= 0; j
< n
; j
++) {
507 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
511 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
521 if (drv
->bdrv_make_empty
)
522 return drv
->bdrv_make_empty(bs
);
527 /* return < 0 if error. See bdrv_write() for the return codes */
528 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
529 uint8_t *buf
, int nb_sectors
)
531 BlockDriver
*drv
= bs
->drv
;
536 if (drv
->bdrv_pread
) {
538 len
= nb_sectors
* 512;
539 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
545 bs
->rd_bytes
+= (unsigned) len
;
550 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
554 /* Return < 0 if error. Important errors are:
555 -EIO generic I/O error (may happen for all errors)
556 -ENOMEDIUM No media inserted.
557 -EINVAL Invalid sector number or nb_sectors
558 -EACCES Trying to write a read-only device
560 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
561 const uint8_t *buf
, int nb_sectors
)
563 BlockDriver
*drv
= bs
->drv
;
568 if (drv
->bdrv_pwrite
) {
569 int ret
, len
, count
= 0;
570 len
= nb_sectors
* 512;
572 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
- count
);
574 printf("bdrv_write ret=%d\n", ret
);
579 } while (count
!= len
);
580 bs
->wr_bytes
+= (unsigned) len
;
584 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
587 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
588 uint8_t *buf
, int count1
)
590 uint8_t tmp_buf
[SECTOR_SIZE
];
591 int len
, nb_sectors
, count
;
595 /* first read to align to sector start */
596 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
599 sector_num
= offset
>> SECTOR_BITS
;
601 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
603 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
611 /* read the sectors "in place" */
612 nb_sectors
= count
>> SECTOR_BITS
;
613 if (nb_sectors
> 0) {
614 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
616 sector_num
+= nb_sectors
;
617 len
= nb_sectors
<< SECTOR_BITS
;
622 /* add data from the last sector */
624 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
626 memcpy(buf
, tmp_buf
, count
);
631 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
632 const uint8_t *buf
, int count1
)
634 uint8_t tmp_buf
[SECTOR_SIZE
];
635 int len
, nb_sectors
, count
;
639 /* first write to align to sector start */
640 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
643 sector_num
= offset
>> SECTOR_BITS
;
645 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
647 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
648 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
657 /* write the sectors "in place" */
658 nb_sectors
= count
>> SECTOR_BITS
;
659 if (nb_sectors
> 0) {
660 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
662 sector_num
+= nb_sectors
;
663 len
= nb_sectors
<< SECTOR_BITS
;
668 /* add data from the last sector */
670 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
672 memcpy(tmp_buf
, buf
, count
);
673 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
680 * Read with byte offsets (needed only for file protocols)
682 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
683 void *buf1
, int count1
)
685 BlockDriver
*drv
= bs
->drv
;
689 if (!drv
->bdrv_pread
)
690 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
691 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
695 * Write with byte offsets (needed only for file protocols)
697 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
698 const void *buf1
, int count1
)
700 BlockDriver
*drv
= bs
->drv
;
704 if (!drv
->bdrv_pwrite
)
705 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
706 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
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 buf
= qemu_memalign(512, 512);
776 bdrv_get_geometry(bs
, &nb_sectors
);
778 ret
= bdrv_read(bs
, 0, buf
, 1);
781 /* test msdos magic */
782 if (buf
[510] != 0x55 || buf
[511] != 0xaa) {
786 for(i
= 0; i
< 4; i
++) {
787 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
788 nr_sects
= le32_to_cpu(p
->nr_sects
);
789 if (nr_sects
&& p
->end_head
) {
790 /* We make the assumption that the partition terminates on
791 a cylinder boundary */
792 heads
= p
->end_head
+ 1;
793 sectors
= p
->end_sector
& 63;
796 cylinders
= nb_sectors
/ (heads
* sectors
);
797 if (cylinders
< 1 || cylinders
> 16383)
801 *pcylinders
= cylinders
;
803 printf("guessed geometry: LCHS=%d %d %d\n",
804 cylinders
, heads
, sectors
);
814 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
816 int translation
, lba_detected
= 0;
817 int cylinders
, heads
, secs
;
820 /* if a geometry hint is available, use it */
821 bdrv_get_geometry(bs
, &nb_sectors
);
822 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
823 translation
= bdrv_get_translation_hint(bs
);
824 if (cylinders
!= 0) {
829 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
831 /* if heads > 16, it means that a BIOS LBA
832 translation was active, so the default
833 hardware geometry is OK */
835 goto default_geometry
;
840 /* disable any translation to be in sync with
841 the logical geometry */
842 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
843 bdrv_set_translation_hint(bs
,
844 BIOS_ATA_TRANSLATION_NONE
);
849 /* if no geometry, use a standard physical disk geometry */
850 cylinders
= nb_sectors
/ (16 * 63);
852 if (cylinders
> 16383)
854 else if (cylinders
< 2)
859 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
860 if ((*pcyls
* *pheads
) <= 131072) {
861 bdrv_set_translation_hint(bs
,
862 BIOS_ATA_TRANSLATION_LARGE
);
864 bdrv_set_translation_hint(bs
,
865 BIOS_ATA_TRANSLATION_LBA
);
869 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
873 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
874 int cyls
, int heads
, int secs
)
881 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
884 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
885 type
== BDRV_TYPE_FLOPPY
));
888 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
890 bs
->translation
= translation
;
893 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
894 int *pcyls
, int *pheads
, int *psecs
)
901 int bdrv_get_type_hint(BlockDriverState
*bs
)
906 int bdrv_get_translation_hint(BlockDriverState
*bs
)
908 return bs
->translation
;
911 int bdrv_is_removable(BlockDriverState
*bs
)
913 return bs
->removable
;
916 int bdrv_is_read_only(BlockDriverState
*bs
)
918 return bs
->read_only
;
921 int bdrv_is_sg(BlockDriverState
*bs
)
926 /* XXX: no longer used */
927 void bdrv_set_change_cb(BlockDriverState
*bs
,
928 void (*change_cb
)(void *opaque
), void *opaque
)
930 bs
->change_cb
= change_cb
;
931 bs
->change_opaque
= opaque
;
934 int bdrv_is_encrypted(BlockDriverState
*bs
)
936 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
938 return bs
->encrypted
;
941 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
944 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
945 ret
= bdrv_set_key(bs
->backing_hd
, key
);
951 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
953 return bs
->drv
->bdrv_set_key(bs
, key
);
956 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
961 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
965 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
970 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
971 it(opaque
, drv
->format_name
);
975 BlockDriverState
*bdrv_find(const char *name
)
977 BlockDriverState
*bs
;
979 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
980 if (!strcmp(name
, bs
->device_name
))
986 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
988 BlockDriverState
*bs
;
990 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
991 it(opaque
, bs
->device_name
);
995 const char *bdrv_get_device_name(BlockDriverState
*bs
)
997 return bs
->device_name
;
1000 void bdrv_flush(BlockDriverState
*bs
)
1002 if (bs
->drv
->bdrv_flush
)
1003 bs
->drv
->bdrv_flush(bs
);
1005 bdrv_flush(bs
->backing_hd
);
1008 void bdrv_iterate_writeable(void (*it
)(BlockDriverState
*bs
))
1010 BlockDriverState
*bs
;
1012 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1013 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1014 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1018 void bdrv_flush_all(void)
1020 BlockDriverState
*bs
;
1022 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1023 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1024 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1029 * Returns true iff the specified sector is present in the disk image. Drivers
1030 * not implementing the functionality are assumed to not support backing files,
1031 * hence all their sectors are reported as allocated.
1033 * 'pnum' is set to the number of sectors (including and immediately following
1034 * the specified sector) that are known to be in the same
1035 * allocated/unallocated state.
1037 * 'nb_sectors' is the max value 'pnum' should be set to.
1039 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1043 if (!bs
->drv
->bdrv_is_allocated
) {
1044 if (sector_num
>= bs
->total_sectors
) {
1048 n
= bs
->total_sectors
- sector_num
;
1049 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1052 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1055 void bdrv_info(void)
1057 BlockDriverState
*bs
;
1059 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1060 term_printf("%s:", bs
->device_name
);
1061 term_printf(" type=");
1066 case BDRV_TYPE_CDROM
:
1067 term_printf("cdrom");
1069 case BDRV_TYPE_FLOPPY
:
1070 term_printf("floppy");
1073 term_printf(" removable=%d", bs
->removable
);
1074 if (bs
->removable
) {
1075 term_printf(" locked=%d", bs
->locked
);
1078 term_printf(" file=");
1079 term_print_filename(bs
->filename
);
1080 if (bs
->backing_file
[0] != '\0') {
1081 term_printf(" backing_file=");
1082 term_print_filename(bs
->backing_file
);
1084 term_printf(" ro=%d", bs
->read_only
);
1085 term_printf(" drv=%s", bs
->drv
->format_name
);
1087 term_printf(" encrypted");
1089 term_printf(" [not inserted]");
1095 /* The "info blockstats" command. */
1096 void bdrv_info_stats (void)
1098 BlockDriverState
*bs
;
1099 BlockDriverInfo bdi
;
1101 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1103 " rd_bytes=%" PRIu64
1104 " wr_bytes=%" PRIu64
1105 " rd_operations=%" PRIu64
1106 " wr_operations=%" PRIu64
1109 bs
->rd_bytes
, bs
->wr_bytes
,
1110 bs
->rd_ops
, bs
->wr_ops
);
1111 if (bdrv_get_info(bs
, &bdi
) == 0)
1112 term_printf(" high=%" PRId64
1113 " bytes_free=%" PRId64
,
1114 bdi
.highest_alloc
, bdi
.num_free_bytes
);
1119 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1120 char *filename
, int filename_size
)
1122 if (!bs
->backing_hd
) {
1123 pstrcpy(filename
, filename_size
, "");
1125 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1129 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1130 const uint8_t *buf
, int nb_sectors
)
1132 BlockDriver
*drv
= bs
->drv
;
1135 if (!drv
->bdrv_write_compressed
)
1137 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1140 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1142 BlockDriver
*drv
= bs
->drv
;
1145 if (!drv
->bdrv_get_info
)
1147 memset(bdi
, 0, sizeof(*bdi
));
1148 return drv
->bdrv_get_info(bs
, bdi
);
1151 /**************************************************************/
1152 /* handling of snapshots */
1154 int bdrv_snapshot_create(BlockDriverState
*bs
,
1155 QEMUSnapshotInfo
*sn_info
)
1157 BlockDriver
*drv
= bs
->drv
;
1160 if (!drv
->bdrv_snapshot_create
)
1162 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1165 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1166 const char *snapshot_id
)
1168 BlockDriver
*drv
= bs
->drv
;
1171 if (!drv
->bdrv_snapshot_goto
)
1173 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1176 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1178 BlockDriver
*drv
= bs
->drv
;
1181 if (!drv
->bdrv_snapshot_delete
)
1183 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1186 int bdrv_snapshot_list(BlockDriverState
*bs
,
1187 QEMUSnapshotInfo
**psn_info
)
1189 BlockDriver
*drv
= bs
->drv
;
1192 if (!drv
->bdrv_snapshot_list
)
1194 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1197 #define NB_SUFFIXES 4
1199 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1201 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1206 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1209 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1210 if (size
< (10 * base
)) {
1211 snprintf(buf
, buf_size
, "%0.1f%c",
1212 (double)size
/ base
,
1215 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1216 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1217 ((size
+ (base
>> 1)) / base
),
1227 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1229 char buf1
[128], date_buf
[128], clock_buf
[128];
1239 snprintf(buf
, buf_size
,
1240 "%-10s%-20s%7s%20s%15s",
1241 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1245 ptm
= localtime(&ti
);
1246 strftime(date_buf
, sizeof(date_buf
),
1247 "%Y-%m-%d %H:%M:%S", ptm
);
1249 localtime_r(&ti
, &tm
);
1250 strftime(date_buf
, sizeof(date_buf
),
1251 "%Y-%m-%d %H:%M:%S", &tm
);
1253 secs
= sn
->vm_clock_nsec
/ 1000000000;
1254 snprintf(clock_buf
, sizeof(clock_buf
),
1255 "%02d:%02d:%02d.%03d",
1257 (int)((secs
/ 60) % 60),
1259 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1260 snprintf(buf
, buf_size
,
1261 "%-10s%-20s%7s%20s%15s",
1262 sn
->id_str
, sn
->name
,
1263 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1271 /**************************************************************/
1274 typedef struct VectorTranslationState
{
1278 BlockDriverAIOCB
*aiocb
;
1279 BlockDriverAIOCB
*this_aiocb
;
1280 } VectorTranslationState
;
1282 static void bdrv_aio_rw_vector_cb(void *opaque
, int ret
)
1284 VectorTranslationState
*s
= opaque
;
1287 qemu_iovec_from_buffer(s
->iov
, s
->bounce
, s
->iov
->size
);
1289 qemu_free(s
->bounce
);
1290 s
->this_aiocb
->cb(s
->this_aiocb
->opaque
, ret
);
1291 qemu_aio_release(s
->this_aiocb
);
1294 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1298 BlockDriverCompletionFunc
*cb
,
1303 VectorTranslationState
*s
= qemu_mallocz(sizeof(*s
));
1304 BlockDriverAIOCB
*aiocb
= qemu_aio_get(bs
, cb
, opaque
);
1306 s
->this_aiocb
= aiocb
;
1308 s
->bounce
= qemu_memalign(512, nb_sectors
* 512);
1309 s
->is_write
= is_write
;
1311 qemu_iovec_to_buffer(s
->iov
, s
->bounce
);
1312 s
->aiocb
= bdrv_aio_write(bs
, sector_num
, s
->bounce
, nb_sectors
,
1313 bdrv_aio_rw_vector_cb
, s
);
1315 s
->aiocb
= bdrv_aio_read(bs
, sector_num
, s
->bounce
, nb_sectors
,
1316 bdrv_aio_rw_vector_cb
, s
);
1321 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1322 QEMUIOVector
*iov
, int nb_sectors
,
1323 BlockDriverCompletionFunc
*cb
, void *opaque
)
1325 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1329 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1330 QEMUIOVector
*iov
, int nb_sectors
,
1331 BlockDriverCompletionFunc
*cb
, void *opaque
)
1333 return bdrv_aio_rw_vector(bs
, sector_num
, iov
, nb_sectors
,
1337 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1338 uint8_t *buf
, int nb_sectors
,
1339 BlockDriverCompletionFunc
*cb
, void *opaque
)
1341 BlockDriver
*drv
= bs
->drv
;
1342 BlockDriverAIOCB
*ret
;
1347 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1350 /* Update stats even though technically transfer has not happened. */
1351 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1358 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1359 const uint8_t *buf
, int nb_sectors
,
1360 BlockDriverCompletionFunc
*cb
, void *opaque
)
1362 BlockDriver
*drv
= bs
->drv
;
1363 BlockDriverAIOCB
*ret
;
1370 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1373 /* Update stats even though technically transfer has not happened. */
1374 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1381 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1383 BlockDriver
*drv
= acb
->bs
->drv
;
1385 if (acb
->cb
== bdrv_aio_rw_vector_cb
) {
1386 VectorTranslationState
*s
= acb
->opaque
;
1390 drv
->bdrv_aio_cancel(acb
);
1394 /**************************************************************/
1395 /* async block device emulation */
1397 static void bdrv_aio_bh_cb(void *opaque
)
1399 BlockDriverAIOCBSync
*acb
= opaque
;
1400 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1401 qemu_aio_release(acb
);
1404 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1405 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1406 BlockDriverCompletionFunc
*cb
, void *opaque
)
1408 BlockDriverAIOCBSync
*acb
;
1411 acb
= qemu_aio_get(bs
, cb
, opaque
);
1413 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1414 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1416 qemu_bh_schedule(acb
->bh
);
1417 return &acb
->common
;
1420 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1421 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1422 BlockDriverCompletionFunc
*cb
, void *opaque
)
1424 BlockDriverAIOCBSync
*acb
;
1427 acb
= qemu_aio_get(bs
, cb
, opaque
);
1429 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1430 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1432 qemu_bh_schedule(acb
->bh
);
1433 return &acb
->common
;
1436 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1438 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1439 qemu_bh_cancel(acb
->bh
);
1440 qemu_aio_release(acb
);
1443 /**************************************************************/
1444 /* sync block device emulation */
1446 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1448 *(int *)opaque
= ret
;
1451 #define NOT_DONE 0x7fffffff
1453 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1454 uint8_t *buf
, int nb_sectors
)
1457 BlockDriverAIOCB
*acb
;
1459 async_ret
= NOT_DONE
;
1460 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1461 bdrv_rw_em_cb
, &async_ret
);
1465 while (async_ret
== NOT_DONE
) {
1472 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1473 const uint8_t *buf
, int nb_sectors
)
1476 BlockDriverAIOCB
*acb
;
1478 async_ret
= NOT_DONE
;
1479 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1480 bdrv_rw_em_cb
, &async_ret
);
1483 while (async_ret
== NOT_DONE
) {
1489 void bdrv_init(void)
1491 bdrv_register(&bdrv_raw
);
1492 bdrv_register(&bdrv_host_device
);
1494 bdrv_register(&bdrv_cow
);
1496 bdrv_register(&bdrv_qcow
);
1497 bdrv_register(&bdrv_vmdk
);
1498 bdrv_register(&bdrv_cloop
);
1499 bdrv_register(&bdrv_dmg
);
1500 bdrv_register(&bdrv_bochs
);
1501 bdrv_register(&bdrv_vpc
);
1502 bdrv_register(&bdrv_vvfat
);
1503 bdrv_register(&bdrv_qcow2
);
1504 bdrv_register(&bdrv_parallels
);
1505 bdrv_register(&bdrv_nbd
);
1508 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1512 BlockDriverAIOCB
*acb
;
1515 if (drv
->free_aiocb
) {
1516 acb
= drv
->free_aiocb
;
1517 drv
->free_aiocb
= acb
->next
;
1519 acb
= qemu_mallocz(drv
->aiocb_size
);
1525 acb
->opaque
= opaque
;
1529 void qemu_aio_release(void *p
)
1531 BlockDriverAIOCB
*acb
= p
;
1532 BlockDriver
*drv
= acb
->bs
->drv
;
1533 acb
->next
= drv
->free_aiocb
;
1534 drv
->free_aiocb
= acb
;
1537 /**************************************************************/
1538 /* removable device support */
1541 * Return TRUE if the media is present
1543 int bdrv_is_inserted(BlockDriverState
*bs
)
1545 BlockDriver
*drv
= bs
->drv
;
1549 if (!drv
->bdrv_is_inserted
)
1551 ret
= drv
->bdrv_is_inserted(bs
);
1556 * Return TRUE if the media changed since the last call to this
1557 * function. It is currently only used for floppy disks
1559 int bdrv_media_changed(BlockDriverState
*bs
)
1561 BlockDriver
*drv
= bs
->drv
;
1564 if (!drv
|| !drv
->bdrv_media_changed
)
1567 ret
= drv
->bdrv_media_changed(bs
);
1568 if (ret
== -ENOTSUP
)
1569 ret
= bs
->media_changed
;
1570 bs
->media_changed
= 0;
1575 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1577 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1579 BlockDriver
*drv
= bs
->drv
;
1582 if (!drv
|| !drv
->bdrv_eject
) {
1585 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1587 if (ret
== -ENOTSUP
) {
1593 int bdrv_is_locked(BlockDriverState
*bs
)
1599 * Lock or unlock the media (if it is locked, the user won't be able
1600 * to eject it manually).
1602 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1604 BlockDriver
*drv
= bs
->drv
;
1606 bs
->locked
= locked
;
1607 if (drv
&& drv
->bdrv_set_locked
) {
1608 drv
->bdrv_set_locked(bs
, locked
);
1612 /* needed for generic scsi interface */
1614 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1616 BlockDriver
*drv
= bs
->drv
;
1618 if (drv
&& drv
->bdrv_ioctl
)
1619 return drv
->bdrv_ioctl(bs
, req
, buf
);