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 "qemu-common.h"
28 #include "block_int.h"
32 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/queue.h>
40 #define SECTOR_SIZE (1 << SECTOR_BITS)
42 typedef struct BlockDriverAIOCBSync
{
43 BlockDriverAIOCB common
;
46 } BlockDriverAIOCBSync
;
48 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
49 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
50 BlockDriverCompletionFunc
*cb
, void *opaque
);
51 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
52 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
53 BlockDriverCompletionFunc
*cb
, void *opaque
);
54 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
55 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
56 uint8_t *buf
, int nb_sectors
);
57 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
58 const uint8_t *buf
, int nb_sectors
);
60 BlockDriverState
*bdrv_first
;
61 static BlockDriver
*first_drv
;
63 int path_is_absolute(const char *path
)
67 /* specific case for names like: "\\.\d:" */
68 if (*path
== '/' || *path
== '\\')
71 p
= strchr(path
, ':');
77 return (*p
== '/' || *p
== '\\');
83 /* if filename is absolute, just copy it to dest. Otherwise, build a
84 path to it by considering it is relative to base_path. URL are
86 void path_combine(char *dest
, int dest_size
,
87 const char *base_path
,
95 if (path_is_absolute(filename
)) {
96 pstrcpy(dest
, dest_size
, filename
);
98 p
= strchr(base_path
, ':');
103 p1
= strrchr(base_path
, '/');
107 p2
= strrchr(base_path
, '\\');
119 if (len
> dest_size
- 1)
121 memcpy(dest
, base_path
, len
);
123 pstrcat(dest
, dest_size
, filename
);
128 static void bdrv_register(BlockDriver
*bdrv
)
130 if (!bdrv
->bdrv_aio_read
) {
131 /* add AIO emulation layer */
132 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
133 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
134 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
135 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
136 } else if (!bdrv
->bdrv_read
&& !bdrv
->bdrv_pread
) {
137 /* add synchronous IO emulation layer */
138 bdrv
->bdrv_read
= bdrv_read_em
;
139 bdrv
->bdrv_write
= bdrv_write_em
;
141 bdrv
->next
= first_drv
;
145 /* create a new block device (by default it is empty) */
146 BlockDriverState
*bdrv_new(const char *device_name
)
148 BlockDriverState
**pbs
, *bs
;
150 bs
= qemu_mallocz(sizeof(BlockDriverState
));
153 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
154 if (device_name
[0] != '\0') {
155 /* insert at the end */
164 BlockDriver
*bdrv_find_format(const char *format_name
)
167 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
168 if (!strcmp(drv1
->format_name
, format_name
))
174 int bdrv_create(BlockDriver
*drv
,
175 const char *filename
, int64_t size_in_sectors
,
176 const char *backing_file
, int flags
)
178 if (!drv
->bdrv_create
)
180 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
184 void get_tmp_filename(char *filename
, int size
)
186 char temp_dir
[MAX_PATH
];
188 GetTempPath(MAX_PATH
, temp_dir
);
189 GetTempFileName(temp_dir
, "qem", 0, filename
);
192 void get_tmp_filename(char *filename
, int size
)
196 /* XXX: race condition possible */
197 tmpdir
= getenv("TMPDIR");
200 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
201 fd
= mkstemp(filename
);
207 static int is_windows_drive_prefix(const char *filename
)
209 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
210 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
214 static int is_windows_drive(const char *filename
)
216 if (is_windows_drive_prefix(filename
) &&
219 if (strstart(filename
, "\\\\.\\", NULL
) ||
220 strstart(filename
, "//./", NULL
))
226 static BlockDriver
*find_protocol(const char *filename
)
234 if (is_windows_drive(filename
) ||
235 is_windows_drive_prefix(filename
))
238 p
= strchr(filename
, ':');
242 if (len
> sizeof(protocol
) - 1)
243 len
= sizeof(protocol
) - 1;
244 memcpy(protocol
, filename
, len
);
245 protocol
[len
] = '\0';
246 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
247 if (drv1
->protocol_name
&&
248 !strcmp(drv1
->protocol_name
, protocol
))
254 /* XXX: force raw format if block or character device ? It would
255 simplify the BSD case */
256 static BlockDriver
*find_image_format(const char *filename
)
258 int ret
, score
, score_max
;
259 BlockDriver
*drv1
, *drv
;
261 BlockDriverState
*bs
;
263 /* detect host devices. By convention, /dev/cdrom[N] is always
264 recognized as a host CDROM */
265 if (strstart(filename
, "/dev/cdrom", NULL
))
266 return &bdrv_host_device
;
268 if (is_windows_drive(filename
))
269 return &bdrv_host_device
;
273 if (stat(filename
, &st
) >= 0 &&
274 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
275 return &bdrv_host_device
;
280 drv
= find_protocol(filename
);
281 /* no need to test disk image formats for vvfat */
282 if (drv
== &bdrv_vvfat
)
285 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
288 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
295 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
296 if (drv1
->bdrv_probe
) {
297 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
298 if (score
> score_max
) {
307 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
309 BlockDriverState
*bs
;
315 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
324 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
326 return bdrv_open2(bs
, filename
, flags
, NULL
);
329 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
333 char tmp_filename
[PATH_MAX
];
334 char backing_filename
[PATH_MAX
];
337 bs
->is_temporary
= 0;
340 if (flags
& BDRV_O_SNAPSHOT
) {
341 BlockDriverState
*bs1
;
344 /* if snapshot, we create a temporary backing file and open it
345 instead of opening 'filename' directly */
347 /* if there is a backing file, use it */
352 if (bdrv_open(bs1
, filename
, 0) < 0) {
356 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
359 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
360 realpath(filename
, backing_filename
);
361 if (bdrv_create(&bdrv_qcow2
, tmp_filename
,
362 total_size
, backing_filename
, 0) < 0) {
365 filename
= tmp_filename
;
366 bs
->is_temporary
= 1;
369 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
370 if (flags
& BDRV_O_FILE
) {
371 drv
= find_protocol(filename
);
376 drv
= find_image_format(filename
);
382 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
383 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
385 /* Note: for compatibility, we open disk image files as RDWR, and
386 RDONLY as fallback */
387 if (!(flags
& BDRV_O_FILE
))
388 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_DIRECT
);
390 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
391 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
392 if (ret
== -EACCES
&& !(flags
& BDRV_O_FILE
)) {
393 ret
= drv
->bdrv_open(bs
, filename
, BDRV_O_RDONLY
);
397 qemu_free(bs
->opaque
);
402 if (drv
->bdrv_getlength
) {
403 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
406 if (bs
->is_temporary
) {
410 if (bs
->backing_file
[0] != '\0') {
411 /* if there is a backing file, use it */
412 bs
->backing_hd
= bdrv_new("");
413 if (!bs
->backing_hd
) {
418 path_combine(backing_filename
, sizeof(backing_filename
),
419 filename
, bs
->backing_file
);
420 if (bdrv_open(bs
->backing_hd
, backing_filename
, 0) < 0)
424 /* call the change callback */
425 bs
->media_changed
= 1;
427 bs
->change_cb(bs
->change_opaque
);
432 void bdrv_close(BlockDriverState
*bs
)
436 bdrv_delete(bs
->backing_hd
);
437 bs
->drv
->bdrv_close(bs
);
438 qemu_free(bs
->opaque
);
440 if (bs
->is_temporary
) {
441 unlink(bs
->filename
);
447 /* call the change callback */
448 bs
->media_changed
= 1;
450 bs
->change_cb(bs
->change_opaque
);
454 void bdrv_delete(BlockDriverState
*bs
)
456 BlockDriverState
**pbs
;
459 while (*pbs
!= bs
&& *pbs
!= NULL
)
468 /* commit COW file into the raw image */
469 int bdrv_commit(BlockDriverState
*bs
)
471 BlockDriver
*drv
= bs
->drv
;
472 int64_t i
, total_sectors
;
474 unsigned char sector
[512];
483 if (!bs
->backing_hd
) {
487 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
488 for (i
= 0; i
< total_sectors
;) {
489 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
490 for(j
= 0; j
< n
; j
++) {
491 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
495 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
505 if (drv
->bdrv_make_empty
)
506 return drv
->bdrv_make_empty(bs
);
511 /* return < 0 if error. See bdrv_write() for the return codes */
512 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
513 uint8_t *buf
, int nb_sectors
)
515 BlockDriver
*drv
= bs
->drv
;
520 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
521 memcpy(buf
, bs
->boot_sector_data
, 512);
528 if (drv
->bdrv_pread
) {
530 len
= nb_sectors
* 512;
531 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
537 bs
->rd_bytes
+= (unsigned) len
;
542 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
546 /* Return < 0 if error. Important errors are:
547 -EIO generic I/O error (may happen for all errors)
548 -ENOMEDIUM No media inserted.
549 -EINVAL Invalid sector number or nb_sectors
550 -EACCES Trying to write a read-only device
552 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
553 const uint8_t *buf
, int nb_sectors
)
555 BlockDriver
*drv
= bs
->drv
;
560 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
561 memcpy(bs
->boot_sector_data
, buf
, 512);
563 if (drv
->bdrv_pwrite
) {
565 len
= nb_sectors
* 512;
566 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
);
572 bs
->wr_bytes
+= (unsigned) len
;
577 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
581 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
582 uint8_t *buf
, int count1
)
584 uint8_t tmp_buf
[SECTOR_SIZE
];
585 int len
, nb_sectors
, count
;
589 /* first read to align to sector start */
590 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
593 sector_num
= offset
>> SECTOR_BITS
;
595 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
597 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
605 /* read the sectors "in place" */
606 nb_sectors
= count
>> SECTOR_BITS
;
607 if (nb_sectors
> 0) {
608 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
610 sector_num
+= nb_sectors
;
611 len
= nb_sectors
<< SECTOR_BITS
;
616 /* add data from the last sector */
618 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
620 memcpy(buf
, tmp_buf
, count
);
625 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
626 const uint8_t *buf
, int count1
)
628 uint8_t tmp_buf
[SECTOR_SIZE
];
629 int len
, nb_sectors
, count
;
633 /* first write to align to sector start */
634 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
637 sector_num
= offset
>> SECTOR_BITS
;
639 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
641 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
642 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
651 /* write the sectors "in place" */
652 nb_sectors
= count
>> SECTOR_BITS
;
653 if (nb_sectors
> 0) {
654 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
656 sector_num
+= nb_sectors
;
657 len
= nb_sectors
<< SECTOR_BITS
;
662 /* add data from the last sector */
664 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
666 memcpy(tmp_buf
, buf
, count
);
667 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
674 * Read with byte offsets (needed only for file protocols)
676 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
677 void *buf1
, int count1
)
679 BlockDriver
*drv
= bs
->drv
;
683 if (!drv
->bdrv_pread
)
684 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
685 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
689 * Write with byte offsets (needed only for file protocols)
691 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
692 const void *buf1
, int count1
)
694 BlockDriver
*drv
= bs
->drv
;
698 if (!drv
->bdrv_pwrite
)
699 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
700 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
704 * Truncate file to 'offset' bytes (needed only for file protocols)
706 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
708 BlockDriver
*drv
= bs
->drv
;
711 if (!drv
->bdrv_truncate
)
713 return drv
->bdrv_truncate(bs
, offset
);
717 * Length of a file in bytes. Return < 0 if error or unknown.
719 int64_t bdrv_getlength(BlockDriverState
*bs
)
721 BlockDriver
*drv
= bs
->drv
;
724 if (!drv
->bdrv_getlength
) {
726 return bs
->total_sectors
* SECTOR_SIZE
;
728 return drv
->bdrv_getlength(bs
);
731 /* return 0 as number of sectors if no device present or error */
732 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
735 length
= bdrv_getlength(bs
);
739 length
= length
>> SECTOR_BITS
;
740 *nb_sectors_ptr
= length
;
743 /* force a given boot sector. */
744 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
)
746 bs
->boot_sector_enabled
= 1;
749 memcpy(bs
->boot_sector_data
, data
, size
);
750 memset(bs
->boot_sector_data
+ size
, 0, 512 - size
);
754 uint8_t boot_ind
; /* 0x80 - active */
755 uint8_t head
; /* starting head */
756 uint8_t sector
; /* starting sector */
757 uint8_t cyl
; /* starting cylinder */
758 uint8_t sys_ind
; /* What partition type */
759 uint8_t end_head
; /* end head */
760 uint8_t end_sector
; /* end sector */
761 uint8_t end_cyl
; /* end cylinder */
762 uint32_t start_sect
; /* starting sector counting from 0 */
763 uint32_t nr_sects
; /* nr of sectors in partition */
764 } __attribute__((packed
));
766 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
767 static int guess_disk_lchs(BlockDriverState
*bs
,
768 int *pcylinders
, int *pheads
, int *psectors
)
771 int ret
, i
, heads
, sectors
, cylinders
;
776 buf
= qemu_memalign(512, 512);
780 bdrv_get_geometry(bs
, &nb_sectors
);
782 ret
= bdrv_read(bs
, 0, buf
, 1);
785 /* test msdos magic */
786 if (buf
[510] != 0x55 || buf
[511] != 0xaa) {
790 for(i
= 0; i
< 4; i
++) {
791 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
792 nr_sects
= le32_to_cpu(p
->nr_sects
);
793 if (nr_sects
&& p
->end_head
) {
794 /* We make the assumption that the partition terminates on
795 a cylinder boundary */
796 heads
= p
->end_head
+ 1;
797 sectors
= p
->end_sector
& 63;
800 cylinders
= nb_sectors
/ (heads
* sectors
);
801 if (cylinders
< 1 || cylinders
> 16383)
805 *pcylinders
= cylinders
;
807 printf("guessed geometry: LCHS=%d %d %d\n",
808 cylinders
, heads
, sectors
);
818 void bdrv_guess_geometry(BlockDriverState
*bs
, int *pcyls
, int *pheads
, int *psecs
)
820 int translation
, lba_detected
= 0;
821 int cylinders
, heads
, secs
;
824 /* if a geometry hint is available, use it */
825 bdrv_get_geometry(bs
, &nb_sectors
);
826 bdrv_get_geometry_hint(bs
, &cylinders
, &heads
, &secs
);
827 translation
= bdrv_get_translation_hint(bs
);
828 if (cylinders
!= 0) {
833 if (guess_disk_lchs(bs
, &cylinders
, &heads
, &secs
) == 0) {
835 /* if heads > 16, it means that a BIOS LBA
836 translation was active, so the default
837 hardware geometry is OK */
839 goto default_geometry
;
844 /* disable any translation to be in sync with
845 the logical geometry */
846 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
847 bdrv_set_translation_hint(bs
,
848 BIOS_ATA_TRANSLATION_NONE
);
853 /* if no geometry, use a standard physical disk geometry */
854 cylinders
= nb_sectors
/ (16 * 63);
856 if (cylinders
> 16383)
858 else if (cylinders
< 2)
863 if ((lba_detected
== 1) && (translation
== BIOS_ATA_TRANSLATION_AUTO
)) {
864 if ((*pcyls
* *pheads
) <= 131072) {
865 bdrv_set_translation_hint(bs
,
866 BIOS_ATA_TRANSLATION_LARGE
);
868 bdrv_set_translation_hint(bs
,
869 BIOS_ATA_TRANSLATION_LBA
);
873 bdrv_set_geometry_hint(bs
, *pcyls
, *pheads
, *psecs
);
877 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
878 int cyls
, int heads
, int secs
)
885 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
888 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
889 type
== BDRV_TYPE_FLOPPY
));
892 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
894 bs
->translation
= translation
;
897 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
898 int *pcyls
, int *pheads
, int *psecs
)
905 int bdrv_get_type_hint(BlockDriverState
*bs
)
910 int bdrv_get_translation_hint(BlockDriverState
*bs
)
912 return bs
->translation
;
915 int bdrv_is_removable(BlockDriverState
*bs
)
917 return bs
->removable
;
920 int bdrv_is_read_only(BlockDriverState
*bs
)
922 return bs
->read_only
;
925 int bdrv_is_sg(BlockDriverState
*bs
)
930 /* XXX: no longer used */
931 void bdrv_set_change_cb(BlockDriverState
*bs
,
932 void (*change_cb
)(void *opaque
), void *opaque
)
934 bs
->change_cb
= change_cb
;
935 bs
->change_opaque
= opaque
;
938 int bdrv_is_encrypted(BlockDriverState
*bs
)
940 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
942 return bs
->encrypted
;
945 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
948 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
949 ret
= bdrv_set_key(bs
->backing_hd
, key
);
955 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
957 return bs
->drv
->bdrv_set_key(bs
, key
);
960 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
965 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
969 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
974 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
975 it(opaque
, drv
->format_name
);
979 BlockDriverState
*bdrv_find(const char *name
)
981 BlockDriverState
*bs
;
983 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
984 if (!strcmp(name
, bs
->device_name
))
990 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
992 BlockDriverState
*bs
;
994 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
995 it(opaque
, bs
->device_name
);
999 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1001 return bs
->device_name
;
1004 void bdrv_flush(BlockDriverState
*bs
)
1006 if (bs
->drv
->bdrv_flush
)
1007 bs
->drv
->bdrv_flush(bs
);
1009 bdrv_flush(bs
->backing_hd
);
1012 void bdrv_iterate_writeable(void (*it
)(BlockDriverState
*bs
))
1014 BlockDriverState
*bs
;
1016 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1017 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1018 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1022 void bdrv_flush_all(void)
1024 bdrv_iterate_writeable(bdrv_flush
);
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
);
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
;
1100 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1102 " rd_bytes=%" PRIu64
1103 " wr_bytes=%" PRIu64
1104 " rd_operations=%" PRIu64
1105 " wr_operations=%" PRIu64
1108 bs
->rd_bytes
, bs
->wr_bytes
,
1109 bs
->rd_ops
, bs
->wr_ops
);
1114 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1115 char *filename
, int filename_size
)
1117 if (!bs
->backing_hd
) {
1118 pstrcpy(filename
, filename_size
, "");
1120 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1124 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1125 const uint8_t *buf
, int nb_sectors
)
1127 BlockDriver
*drv
= bs
->drv
;
1130 if (!drv
->bdrv_write_compressed
)
1132 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1135 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1137 BlockDriver
*drv
= bs
->drv
;
1140 if (!drv
->bdrv_get_info
)
1142 memset(bdi
, 0, sizeof(*bdi
));
1143 return drv
->bdrv_get_info(bs
, bdi
);
1146 /**************************************************************/
1147 /* handling of snapshots */
1149 int bdrv_snapshot_create(BlockDriverState
*bs
,
1150 QEMUSnapshotInfo
*sn_info
)
1152 BlockDriver
*drv
= bs
->drv
;
1155 if (!drv
->bdrv_snapshot_create
)
1157 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1160 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1161 const char *snapshot_id
)
1163 BlockDriver
*drv
= bs
->drv
;
1166 if (!drv
->bdrv_snapshot_goto
)
1168 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1171 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1173 BlockDriver
*drv
= bs
->drv
;
1176 if (!drv
->bdrv_snapshot_delete
)
1178 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1181 int bdrv_snapshot_list(BlockDriverState
*bs
,
1182 QEMUSnapshotInfo
**psn_info
)
1184 BlockDriver
*drv
= bs
->drv
;
1187 if (!drv
->bdrv_snapshot_list
)
1189 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1192 #define NB_SUFFIXES 4
1194 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1196 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1201 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1204 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1205 if (size
< (10 * base
)) {
1206 snprintf(buf
, buf_size
, "%0.1f%c",
1207 (double)size
/ base
,
1210 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1211 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1212 ((size
+ (base
>> 1)) / base
),
1222 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1224 char buf1
[128], date_buf
[128], clock_buf
[128];
1234 snprintf(buf
, buf_size
,
1235 "%-10s%-20s%7s%20s%15s",
1236 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1240 ptm
= localtime(&ti
);
1241 strftime(date_buf
, sizeof(date_buf
),
1242 "%Y-%m-%d %H:%M:%S", ptm
);
1244 localtime_r(&ti
, &tm
);
1245 strftime(date_buf
, sizeof(date_buf
),
1246 "%Y-%m-%d %H:%M:%S", &tm
);
1248 secs
= sn
->vm_clock_nsec
/ 1000000000;
1249 snprintf(clock_buf
, sizeof(clock_buf
),
1250 "%02d:%02d:%02d.%03d",
1252 (int)((secs
/ 60) % 60),
1254 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1255 snprintf(buf
, buf_size
,
1256 "%-10s%-20s%7s%20s%15s",
1257 sn
->id_str
, sn
->name
,
1258 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1266 /**************************************************************/
1269 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1270 uint8_t *buf
, int nb_sectors
,
1271 BlockDriverCompletionFunc
*cb
, void *opaque
)
1273 BlockDriver
*drv
= bs
->drv
;
1274 BlockDriverAIOCB
*ret
;
1279 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1280 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1281 memcpy(buf
, bs
->boot_sector_data
, 512);
1287 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1290 /* Update stats even though technically transfer has not happened. */
1291 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1298 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1299 const uint8_t *buf
, int nb_sectors
,
1300 BlockDriverCompletionFunc
*cb
, void *opaque
)
1302 BlockDriver
*drv
= bs
->drv
;
1303 BlockDriverAIOCB
*ret
;
1309 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1310 memcpy(bs
->boot_sector_data
, buf
, 512);
1313 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1316 /* Update stats even though technically transfer has not happened. */
1317 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1324 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1326 BlockDriver
*drv
= acb
->bs
->drv
;
1328 drv
->bdrv_aio_cancel(acb
);
1332 /**************************************************************/
1333 /* async block device emulation */
1336 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1337 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1338 BlockDriverCompletionFunc
*cb
, void *opaque
)
1341 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1346 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1347 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1348 BlockDriverCompletionFunc
*cb
, void *opaque
)
1351 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1356 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
)
1360 static void bdrv_aio_bh_cb(void *opaque
)
1362 BlockDriverAIOCBSync
*acb
= opaque
;
1363 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1364 qemu_aio_release(acb
);
1367 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1368 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1369 BlockDriverCompletionFunc
*cb
, void *opaque
)
1371 BlockDriverAIOCBSync
*acb
;
1374 acb
= qemu_aio_get(bs
, cb
, opaque
);
1376 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1377 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1379 qemu_bh_schedule(acb
->bh
);
1380 return &acb
->common
;
1383 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1384 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1385 BlockDriverCompletionFunc
*cb
, void *opaque
)
1387 BlockDriverAIOCBSync
*acb
;
1390 acb
= qemu_aio_get(bs
, cb
, opaque
);
1392 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1393 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1395 qemu_bh_schedule(acb
->bh
);
1396 return &acb
->common
;
1399 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1401 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1402 qemu_bh_cancel(acb
->bh
);
1403 qemu_aio_release(acb
);
1405 #endif /* !QEMU_IMG */
1407 /**************************************************************/
1408 /* sync block device emulation */
1410 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1412 *(int *)opaque
= ret
;
1415 #define NOT_DONE 0x7fffffff
1417 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1418 uint8_t *buf
, int nb_sectors
)
1421 BlockDriverAIOCB
*acb
;
1423 async_ret
= NOT_DONE
;
1424 qemu_aio_wait_start();
1425 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1426 bdrv_rw_em_cb
, &async_ret
);
1428 qemu_aio_wait_end();
1431 while (async_ret
== NOT_DONE
) {
1434 qemu_aio_wait_end();
1438 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1439 const uint8_t *buf
, int nb_sectors
)
1442 BlockDriverAIOCB
*acb
;
1444 async_ret
= NOT_DONE
;
1445 qemu_aio_wait_start();
1446 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1447 bdrv_rw_em_cb
, &async_ret
);
1449 qemu_aio_wait_end();
1452 while (async_ret
== NOT_DONE
) {
1455 qemu_aio_wait_end();
1459 void bdrv_init(void)
1461 bdrv_register(&bdrv_raw
);
1462 bdrv_register(&bdrv_host_device
);
1464 bdrv_register(&bdrv_cow
);
1466 bdrv_register(&bdrv_qcow
);
1467 bdrv_register(&bdrv_vmdk
);
1468 bdrv_register(&bdrv_cloop
);
1469 bdrv_register(&bdrv_dmg
);
1470 bdrv_register(&bdrv_bochs
);
1471 bdrv_register(&bdrv_vpc
);
1472 bdrv_register(&bdrv_vvfat
);
1473 bdrv_register(&bdrv_qcow2
);
1474 bdrv_register(&bdrv_parallels
);
1475 bdrv_register(&bdrv_nbd
);
1478 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1482 BlockDriverAIOCB
*acb
;
1485 if (drv
->free_aiocb
) {
1486 acb
= drv
->free_aiocb
;
1487 drv
->free_aiocb
= acb
->next
;
1489 acb
= qemu_mallocz(drv
->aiocb_size
);
1495 acb
->opaque
= opaque
;
1499 void qemu_aio_release(void *p
)
1501 BlockDriverAIOCB
*acb
= p
;
1502 BlockDriver
*drv
= acb
->bs
->drv
;
1503 acb
->next
= drv
->free_aiocb
;
1504 drv
->free_aiocb
= acb
;
1507 /**************************************************************/
1508 /* removable device support */
1511 * Return TRUE if the media is present
1513 int bdrv_is_inserted(BlockDriverState
*bs
)
1515 BlockDriver
*drv
= bs
->drv
;
1519 if (!drv
->bdrv_is_inserted
)
1521 ret
= drv
->bdrv_is_inserted(bs
);
1526 * Return TRUE if the media changed since the last call to this
1527 * function. It is currently only used for floppy disks
1529 int bdrv_media_changed(BlockDriverState
*bs
)
1531 BlockDriver
*drv
= bs
->drv
;
1534 if (!drv
|| !drv
->bdrv_media_changed
)
1537 ret
= drv
->bdrv_media_changed(bs
);
1538 if (ret
== -ENOTSUP
)
1539 ret
= bs
->media_changed
;
1540 bs
->media_changed
= 0;
1545 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1547 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1549 BlockDriver
*drv
= bs
->drv
;
1552 if (!drv
|| !drv
->bdrv_eject
) {
1555 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1557 if (ret
== -ENOTSUP
) {
1563 int bdrv_is_locked(BlockDriverState
*bs
)
1569 * Lock or unlock the media (if it is locked, the user won't be able
1570 * to eject it manually).
1572 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1574 BlockDriver
*drv
= bs
->drv
;
1576 bs
->locked
= locked
;
1577 if (drv
&& drv
->bdrv_set_locked
) {
1578 drv
->bdrv_set_locked(bs
, locked
);
1582 /* needed for generic scsi interface */
1584 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1586 BlockDriver
*drv
= bs
->drv
;
1588 if (drv
&& drv
->bdrv_ioctl
)
1589 return drv
->bdrv_ioctl(bs
, req
, buf
);