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
) {
570 len
= nb_sectors
* 512;
571 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
);
577 bs
->wr_bytes
+= (unsigned) len
;
582 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
586 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
587 uint8_t *buf
, int count1
)
589 uint8_t tmp_buf
[SECTOR_SIZE
];
590 int len
, nb_sectors
, count
;
594 /* first read to align to sector start */
595 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
598 sector_num
= offset
>> SECTOR_BITS
;
600 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
602 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
610 /* read the sectors "in place" */
611 nb_sectors
= count
>> SECTOR_BITS
;
612 if (nb_sectors
> 0) {
613 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
615 sector_num
+= nb_sectors
;
616 len
= nb_sectors
<< SECTOR_BITS
;
621 /* add data from the last sector */
623 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
625 memcpy(buf
, tmp_buf
, count
);
630 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
631 const uint8_t *buf
, int count1
)
633 uint8_t tmp_buf
[SECTOR_SIZE
];
634 int len
, nb_sectors
, count
;
638 /* first write to align to sector start */
639 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
642 sector_num
= offset
>> SECTOR_BITS
;
644 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
646 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
647 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
656 /* write the sectors "in place" */
657 nb_sectors
= count
>> SECTOR_BITS
;
658 if (nb_sectors
> 0) {
659 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
661 sector_num
+= nb_sectors
;
662 len
= nb_sectors
<< SECTOR_BITS
;
667 /* add data from the last sector */
669 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
671 memcpy(tmp_buf
, buf
, count
);
672 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
679 * Read with byte offsets (needed only for file protocols)
681 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
682 void *buf1
, int count1
)
684 BlockDriver
*drv
= bs
->drv
;
688 if (!drv
->bdrv_pread
)
689 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
690 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
694 * Write with byte offsets (needed only for file protocols)
696 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
697 const void *buf1
, int count1
)
699 BlockDriver
*drv
= bs
->drv
;
703 if (!drv
->bdrv_pwrite
)
704 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
705 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
709 * Truncate file to 'offset' bytes (needed only for file protocols)
711 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
713 BlockDriver
*drv
= bs
->drv
;
716 if (!drv
->bdrv_truncate
)
718 return drv
->bdrv_truncate(bs
, offset
);
722 * Length of a file in bytes. Return < 0 if error or unknown.
724 int64_t bdrv_getlength(BlockDriverState
*bs
)
726 BlockDriver
*drv
= bs
->drv
;
729 if (!drv
->bdrv_getlength
) {
731 return bs
->total_sectors
* SECTOR_SIZE
;
733 return drv
->bdrv_getlength(bs
);
736 /* return 0 as number of sectors if no device present or error */
737 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
740 length
= bdrv_getlength(bs
);
744 length
= length
>> SECTOR_BITS
;
745 *nb_sectors_ptr
= length
;
749 uint8_t boot_ind
; /* 0x80 - active */
750 uint8_t head
; /* starting head */
751 uint8_t sector
; /* starting sector */
752 uint8_t cyl
; /* starting cylinder */
753 uint8_t sys_ind
; /* What partition type */
754 uint8_t end_head
; /* end head */
755 uint8_t end_sector
; /* end sector */
756 uint8_t end_cyl
; /* end cylinder */
757 uint32_t start_sect
; /* starting sector counting from 0 */
758 uint32_t nr_sects
; /* nr of sectors in partition */
759 } __attribute__((packed
));
761 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
762 static int guess_disk_lchs(BlockDriverState
*bs
,
763 int *pcylinders
, int *pheads
, int *psectors
)
766 int ret
, i
, heads
, sectors
, cylinders
;
771 buf
= qemu_memalign(512, 512);
775 bdrv_get_geometry(bs
, &nb_sectors
);
777 ret
= bdrv_read(bs
, 0, buf
, 1);
780 /* test msdos magic */
781 if (buf
[510] != 0x55 || buf
[511] != 0xaa) {
785 for(i
= 0; i
< 4; i
++) {
786 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
787 nr_sects
= le32_to_cpu(p
->nr_sects
);
788 if (nr_sects
&& p
->end_head
) {
789 /* We make the assumption that the partition terminates on
790 a cylinder boundary */
791 heads
= p
->end_head
+ 1;
792 sectors
= p
->end_sector
& 63;
795 cylinders
= nb_sectors
/ (heads
* sectors
);
796 if (cylinders
< 1 || cylinders
> 16383)
800 *pcylinders
= cylinders
;
802 printf("guessed geometry: LCHS=%d %d %d\n",
803 cylinders
, heads
, sectors
);
813 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
815 int translation
, lba_detected
= 0;
816 int cylinders
, heads
, secs
;
819 /* if a geometry hint is available, use it */
820 bdrv_get_geometry(bs
, &nb_sectors
);
821 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
822 translation
= bdrv_get_translation_hint(bs
);
823 if (cylinders
!= 0) {
828 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
830 /* if heads > 16, it means that a BIOS LBA
831 translation was active, so the default
832 hardware geometry is OK */
834 goto default_geometry
;
839 /* disable any translation to be in sync with
840 the logical geometry */
841 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
842 bdrv_set_translation_hint(bs
,
843 BIOS_ATA_TRANSLATION_NONE
);
848 /* if no geometry, use a standard physical disk geometry */
849 cylinders
= nb_sectors
/ (16 * 63);
851 if (cylinders
> 16383)
853 else if (cylinders
< 2)
858 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
859 if ((*pcyls
* *pheads
) <= 131072) {
860 bdrv_set_translation_hint(bs
,
861 BIOS_ATA_TRANSLATION_LARGE
);
863 bdrv_set_translation_hint(bs
,
864 BIOS_ATA_TRANSLATION_LBA
);
868 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
872 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
873 int cyls
, int heads
, int secs
)
880 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
883 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
884 type
== BDRV_TYPE_FLOPPY
));
887 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
889 bs
->translation
= translation
;
892 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
893 int *pcyls
, int *pheads
, int *psecs
)
900 int bdrv_get_type_hint(BlockDriverState
*bs
)
905 int bdrv_get_translation_hint(BlockDriverState
*bs
)
907 return bs
->translation
;
910 int bdrv_is_removable(BlockDriverState
*bs
)
912 return bs
->removable
;
915 int bdrv_is_read_only(BlockDriverState
*bs
)
917 return bs
->read_only
;
920 int bdrv_is_sg(BlockDriverState
*bs
)
925 /* XXX: no longer used */
926 void bdrv_set_change_cb(BlockDriverState
*bs
,
927 void (*change_cb
)(void *opaque
), void *opaque
)
929 bs
->change_cb
= change_cb
;
930 bs
->change_opaque
= opaque
;
933 int bdrv_is_encrypted(BlockDriverState
*bs
)
935 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
937 return bs
->encrypted
;
940 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
943 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
944 ret
= bdrv_set_key(bs
->backing_hd
, key
);
950 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
952 return bs
->drv
->bdrv_set_key(bs
, key
);
955 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
960 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
964 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
969 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
970 it(opaque
, drv
->format_name
);
974 BlockDriverState
*bdrv_find(const char *name
)
976 BlockDriverState
*bs
;
978 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
979 if (!strcmp(name
, bs
->device_name
))
985 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
987 BlockDriverState
*bs
;
989 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
990 it(opaque
, bs
->device_name
);
994 const char *bdrv_get_device_name(BlockDriverState
*bs
)
996 return bs
->device_name
;
999 void bdrv_flush(BlockDriverState
*bs
)
1001 if (bs
->drv
->bdrv_flush
)
1002 bs
->drv
->bdrv_flush(bs
);
1004 bdrv_flush(bs
->backing_hd
);
1007 void bdrv_iterate_writeable(void (*it
)(BlockDriverState
*bs
))
1009 BlockDriverState
*bs
;
1011 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1012 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1013 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1017 void bdrv_flush_all(void)
1019 BlockDriverState
*bs
;
1021 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1022 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1023 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1028 * Returns true iff the specified sector is present in the disk image. Drivers
1029 * not implementing the functionality are assumed to not support backing files,
1030 * hence all their sectors are reported as allocated.
1032 * 'pnum' is set to the number of sectors (including and immediately following
1033 * the specified sector) that are known to be in the same
1034 * allocated/unallocated state.
1036 * 'nb_sectors' is the max value 'pnum' should be set to.
1038 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1042 if (!bs
->drv
->bdrv_is_allocated
) {
1043 if (sector_num
>= bs
->total_sectors
) {
1047 n
= bs
->total_sectors
- sector_num
;
1048 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1051 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1054 void bdrv_info(void)
1056 BlockDriverState
*bs
;
1058 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1059 term_printf("%s:", bs
->device_name
);
1060 term_printf(" type=");
1065 case BDRV_TYPE_CDROM
:
1066 term_printf("cdrom");
1068 case BDRV_TYPE_FLOPPY
:
1069 term_printf("floppy");
1072 term_printf(" removable=%d", bs
->removable
);
1073 if (bs
->removable
) {
1074 term_printf(" locked=%d", bs
->locked
);
1077 term_printf(" file=");
1078 term_print_filename(bs
->filename
);
1079 if (bs
->backing_file
[0] != '\0') {
1080 term_printf(" backing_file=");
1081 term_print_filename(bs
->backing_file
);
1083 term_printf(" ro=%d", bs
->read_only
);
1084 term_printf(" drv=%s", bs
->drv
->format_name
);
1086 term_printf(" encrypted");
1088 term_printf(" [not inserted]");
1094 /* The "info blockstats" command. */
1095 void bdrv_info_stats (void)
1097 BlockDriverState
*bs
;
1099 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1101 " rd_bytes=%" PRIu64
1102 " wr_bytes=%" PRIu64
1103 " rd_operations=%" PRIu64
1104 " wr_operations=%" PRIu64
1107 bs
->rd_bytes
, bs
->wr_bytes
,
1108 bs
->rd_ops
, bs
->wr_ops
);
1112 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1113 char *filename
, int filename_size
)
1115 if (!bs
->backing_hd
) {
1116 pstrcpy(filename
, filename_size
, "");
1118 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1122 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1123 const uint8_t *buf
, int nb_sectors
)
1125 BlockDriver
*drv
= bs
->drv
;
1128 if (!drv
->bdrv_write_compressed
)
1130 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1133 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1135 BlockDriver
*drv
= bs
->drv
;
1138 if (!drv
->bdrv_get_info
)
1140 memset(bdi
, 0, sizeof(*bdi
));
1141 return drv
->bdrv_get_info(bs
, bdi
);
1144 /**************************************************************/
1145 /* handling of snapshots */
1147 int bdrv_snapshot_create(BlockDriverState
*bs
,
1148 QEMUSnapshotInfo
*sn_info
)
1150 BlockDriver
*drv
= bs
->drv
;
1153 if (!drv
->bdrv_snapshot_create
)
1155 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1158 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1159 const char *snapshot_id
)
1161 BlockDriver
*drv
= bs
->drv
;
1164 if (!drv
->bdrv_snapshot_goto
)
1166 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1169 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1171 BlockDriver
*drv
= bs
->drv
;
1174 if (!drv
->bdrv_snapshot_delete
)
1176 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1179 int bdrv_snapshot_list(BlockDriverState
*bs
,
1180 QEMUSnapshotInfo
**psn_info
)
1182 BlockDriver
*drv
= bs
->drv
;
1185 if (!drv
->bdrv_snapshot_list
)
1187 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1190 #define NB_SUFFIXES 4
1192 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1194 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1199 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1202 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1203 if (size
< (10 * base
)) {
1204 snprintf(buf
, buf_size
, "%0.1f%c",
1205 (double)size
/ base
,
1208 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1209 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1210 ((size
+ (base
>> 1)) / base
),
1220 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1222 char buf1
[128], date_buf
[128], clock_buf
[128];
1232 snprintf(buf
, buf_size
,
1233 "%-10s%-20s%7s%20s%15s",
1234 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1238 ptm
= localtime(&ti
);
1239 strftime(date_buf
, sizeof(date_buf
),
1240 "%Y-%m-%d %H:%M:%S", ptm
);
1242 localtime_r(&ti
, &tm
);
1243 strftime(date_buf
, sizeof(date_buf
),
1244 "%Y-%m-%d %H:%M:%S", &tm
);
1246 secs
= sn
->vm_clock_nsec
/ 1000000000;
1247 snprintf(clock_buf
, sizeof(clock_buf
),
1248 "%02d:%02d:%02d.%03d",
1250 (int)((secs
/ 60) % 60),
1252 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1253 snprintf(buf
, buf_size
,
1254 "%-10s%-20s%7s%20s%15s",
1255 sn
->id_str
, sn
->name
,
1256 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1264 /**************************************************************/
1267 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1268 uint8_t *buf
, int nb_sectors
,
1269 BlockDriverCompletionFunc
*cb
, void *opaque
)
1271 BlockDriver
*drv
= bs
->drv
;
1272 BlockDriverAIOCB
*ret
;
1277 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1280 /* Update stats even though technically transfer has not happened. */
1281 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1288 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1289 const uint8_t *buf
, int nb_sectors
,
1290 BlockDriverCompletionFunc
*cb
, void *opaque
)
1292 BlockDriver
*drv
= bs
->drv
;
1293 BlockDriverAIOCB
*ret
;
1300 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1303 /* Update stats even though technically transfer has not happened. */
1304 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1311 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1313 BlockDriver
*drv
= acb
->bs
->drv
;
1315 drv
->bdrv_aio_cancel(acb
);
1319 /**************************************************************/
1320 /* async block device emulation */
1322 static void bdrv_aio_bh_cb(void *opaque
)
1324 BlockDriverAIOCBSync
*acb
= opaque
;
1325 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1326 qemu_aio_release(acb
);
1329 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1330 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1331 BlockDriverCompletionFunc
*cb
, void *opaque
)
1333 BlockDriverAIOCBSync
*acb
;
1336 acb
= qemu_aio_get(bs
, cb
, opaque
);
1338 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1339 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1341 qemu_bh_schedule(acb
->bh
);
1342 return &acb
->common
;
1345 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1346 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1347 BlockDriverCompletionFunc
*cb
, void *opaque
)
1349 BlockDriverAIOCBSync
*acb
;
1352 acb
= qemu_aio_get(bs
, cb
, opaque
);
1354 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1355 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1357 qemu_bh_schedule(acb
->bh
);
1358 return &acb
->common
;
1361 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1363 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1364 qemu_bh_cancel(acb
->bh
);
1365 qemu_aio_release(acb
);
1368 /**************************************************************/
1369 /* sync block device emulation */
1371 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1373 *(int *)opaque
= ret
;
1376 #define NOT_DONE 0x7fffffff
1378 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1379 uint8_t *buf
, int nb_sectors
)
1382 BlockDriverAIOCB
*acb
;
1384 async_ret
= NOT_DONE
;
1385 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1386 bdrv_rw_em_cb
, &async_ret
);
1390 while (async_ret
== NOT_DONE
) {
1397 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1398 const uint8_t *buf
, int nb_sectors
)
1401 BlockDriverAIOCB
*acb
;
1403 async_ret
= NOT_DONE
;
1404 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1405 bdrv_rw_em_cb
, &async_ret
);
1408 while (async_ret
== NOT_DONE
) {
1414 void bdrv_init(void)
1416 bdrv_register(&bdrv_raw
);
1417 bdrv_register(&bdrv_host_device
);
1419 bdrv_register(&bdrv_cow
);
1421 bdrv_register(&bdrv_qcow
);
1422 bdrv_register(&bdrv_vmdk
);
1423 bdrv_register(&bdrv_cloop
);
1424 bdrv_register(&bdrv_dmg
);
1425 bdrv_register(&bdrv_bochs
);
1426 bdrv_register(&bdrv_vpc
);
1427 bdrv_register(&bdrv_vvfat
);
1428 bdrv_register(&bdrv_qcow2
);
1429 bdrv_register(&bdrv_parallels
);
1430 bdrv_register(&bdrv_nbd
);
1433 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1437 BlockDriverAIOCB
*acb
;
1440 if (drv
->free_aiocb
) {
1441 acb
= drv
->free_aiocb
;
1442 drv
->free_aiocb
= acb
->next
;
1444 acb
= qemu_mallocz(drv
->aiocb_size
);
1450 acb
->opaque
= opaque
;
1454 void qemu_aio_release(void *p
)
1456 BlockDriverAIOCB
*acb
= p
;
1457 BlockDriver
*drv
= acb
->bs
->drv
;
1458 acb
->next
= drv
->free_aiocb
;
1459 drv
->free_aiocb
= acb
;
1462 /**************************************************************/
1463 /* removable device support */
1466 * Return TRUE if the media is present
1468 int bdrv_is_inserted(BlockDriverState
*bs
)
1470 BlockDriver
*drv
= bs
->drv
;
1474 if (!drv
->bdrv_is_inserted
)
1476 ret
= drv
->bdrv_is_inserted(bs
);
1481 * Return TRUE if the media changed since the last call to this
1482 * function. It is currently only used for floppy disks
1484 int bdrv_media_changed(BlockDriverState
*bs
)
1486 BlockDriver
*drv
= bs
->drv
;
1489 if (!drv
|| !drv
->bdrv_media_changed
)
1492 ret
= drv
->bdrv_media_changed(bs
);
1493 if (ret
== -ENOTSUP
)
1494 ret
= bs
->media_changed
;
1495 bs
->media_changed
= 0;
1500 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1502 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1504 BlockDriver
*drv
= bs
->drv
;
1507 if (!drv
|| !drv
->bdrv_eject
) {
1510 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1512 if (ret
== -ENOTSUP
) {
1518 int bdrv_is_locked(BlockDriverState
*bs
)
1524 * Lock or unlock the media (if it is locked, the user won't be able
1525 * to eject it manually).
1527 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1529 BlockDriver
*drv
= bs
->drv
;
1531 bs
->locked
= locked
;
1532 if (drv
&& drv
->bdrv_set_locked
) {
1533 drv
->bdrv_set_locked(bs
, locked
);
1537 /* needed for generic scsi interface */
1539 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1541 BlockDriver
*drv
= bs
->drv
;
1543 if (drv
&& drv
->bdrv_ioctl
)
1544 return drv
->bdrv_ioctl(bs
, req
, buf
);