2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "config-host.h"
26 /* include native header before sys-queue.h */
27 #include <sys/queue.h>
30 #include "qemu-common.h"
32 #include "block_int.h"
35 #include <sys/types.h>
37 #include <sys/ioctl.h>
48 #define SECTOR_SIZE (1 << SECTOR_BITS)
50 typedef struct BlockDriverAIOCBSync
{
51 BlockDriverAIOCB common
;
54 /* vector translation state */
58 } BlockDriverAIOCBSync
;
60 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
61 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
62 BlockDriverCompletionFunc
*cb
, void *opaque
);
63 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
64 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
65 BlockDriverCompletionFunc
*cb
, void *opaque
);
66 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
67 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
68 uint8_t *buf
, int nb_sectors
);
69 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
70 const uint8_t *buf
, int nb_sectors
);
72 BlockDriverState
*bdrv_first
;
74 static BlockDriver
*first_drv
;
76 int path_is_absolute(const char *path
)
80 /* specific case for names like: "\\.\d:" */
81 if (*path
== '/' || *path
== '\\')
84 p
= strchr(path
, ':');
90 return (*p
== '/' || *p
== '\\');
96 /* if filename is absolute, just copy it to dest. Otherwise, build a
97 path to it by considering it is relative to base_path. URL are
99 void path_combine(char *dest
, int dest_size
,
100 const char *base_path
,
101 const char *filename
)
108 if (path_is_absolute(filename
)) {
109 pstrcpy(dest
, dest_size
, filename
);
111 p
= strchr(base_path
, ':');
116 p1
= strrchr(base_path
, '/');
120 p2
= strrchr(base_path
, '\\');
132 if (len
> dest_size
- 1)
134 memcpy(dest
, base_path
, len
);
136 pstrcat(dest
, dest_size
, filename
);
141 static void bdrv_register(BlockDriver
*bdrv
)
143 if (!bdrv
->bdrv_aio_readv
) {
144 /* add AIO emulation layer */
145 bdrv
->bdrv_aio_readv
= bdrv_aio_readv_em
;
146 bdrv
->bdrv_aio_writev
= bdrv_aio_writev_em
;
147 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
148 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
149 } else if (!bdrv
->bdrv_read
) {
150 /* add synchronous IO emulation layer */
151 bdrv
->bdrv_read
= bdrv_read_em
;
152 bdrv
->bdrv_write
= bdrv_write_em
;
154 aio_pool_init(&bdrv
->aio_pool
, bdrv
->aiocb_size
, bdrv
->bdrv_aio_cancel
);
155 bdrv
->next
= first_drv
;
159 /* create a new block device (by default it is empty) */
160 BlockDriverState
*bdrv_new(const char *device_name
)
162 BlockDriverState
**pbs
, *bs
;
164 bs
= qemu_mallocz(sizeof(BlockDriverState
));
165 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
166 if (device_name
[0] != '\0') {
167 /* insert at the end */
176 BlockDriver
*bdrv_find_format(const char *format_name
)
179 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
180 if (!strcmp(drv1
->format_name
, format_name
))
186 int bdrv_create2(BlockDriver
*drv
,
187 const char *filename
, int64_t size_in_sectors
,
188 const char *backing_file
, const char *backing_format
,
191 if (drv
->bdrv_create2
)
192 return drv
->bdrv_create2(filename
, size_in_sectors
, backing_file
,
193 backing_format
, flags
);
194 if (drv
->bdrv_create
)
195 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
,
200 int bdrv_create(BlockDriver
*drv
,
201 const char *filename
, int64_t size_in_sectors
,
202 const char *backing_file
, int flags
)
204 if (!drv
->bdrv_create
)
206 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
210 void get_tmp_filename(char *filename
, int size
)
212 char temp_dir
[MAX_PATH
];
214 GetTempPath(MAX_PATH
, temp_dir
);
215 GetTempFileName(temp_dir
, "qem", 0, filename
);
218 void get_tmp_filename(char *filename
, int size
)
222 /* XXX: race condition possible */
223 tmpdir
= getenv("TMPDIR");
226 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
227 fd
= mkstemp(filename
);
233 static int is_windows_drive_prefix(const char *filename
)
235 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
236 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
240 static int is_windows_drive(const char *filename
)
242 if (is_windows_drive_prefix(filename
) &&
245 if (strstart(filename
, "\\\\.\\", NULL
) ||
246 strstart(filename
, "//./", NULL
))
252 static BlockDriver
*find_protocol(const char *filename
)
260 if (is_windows_drive(filename
) ||
261 is_windows_drive_prefix(filename
))
264 p
= strchr(filename
, ':');
268 if (len
> sizeof(protocol
) - 1)
269 len
= sizeof(protocol
) - 1;
270 memcpy(protocol
, filename
, len
);
271 protocol
[len
] = '\0';
272 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
273 if (drv1
->protocol_name
&&
274 !strcmp(drv1
->protocol_name
, protocol
))
280 /* XXX: force raw format if block or character device ? It would
281 simplify the BSD case */
282 static BlockDriver
*find_image_format(const char *filename
)
284 int ret
, score
, score_max
;
285 BlockDriver
*drv1
, *drv
;
287 BlockDriverState
*bs
;
289 /* detect host devices. By convention, /dev/cdrom[N] is always
290 recognized as a host CDROM */
291 if (strstart(filename
, "/dev/cdrom", NULL
))
292 return &bdrv_host_device
;
294 if (is_windows_drive(filename
))
295 return &bdrv_host_device
;
299 if (stat(filename
, &st
) >= 0 &&
300 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
301 return &bdrv_host_device
;
306 drv
= find_protocol(filename
);
307 /* no need to test disk image formats for vvfat */
308 if (drv
== &bdrv_vvfat
)
311 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
314 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
321 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
322 if (drv1
->bdrv_probe
) {
323 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
324 if (score
> score_max
) {
333 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
335 BlockDriverState
*bs
;
339 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
349 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
351 return bdrv_open2(bs
, filename
, flags
, NULL
);
354 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
358 char tmp_filename
[PATH_MAX
];
359 char backing_filename
[PATH_MAX
];
362 bs
->is_temporary
= 0;
366 if (flags
& BDRV_O_SNAPSHOT
) {
367 BlockDriverState
*bs1
;
371 /* if snapshot, we create a temporary backing file and open it
372 instead of opening 'filename' directly */
374 /* if there is a backing file, use it */
376 ret
= bdrv_open2(bs1
, filename
, 0, drv
);
381 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
383 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
388 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
390 /* Real path is meaningless for protocols */
392 snprintf(backing_filename
, sizeof(backing_filename
),
395 realpath(filename
, backing_filename
);
397 ret
= bdrv_create2(&bdrv_qcow2
, tmp_filename
,
398 total_size
, backing_filename
,
399 (drv
? drv
->format_name
: NULL
), 0);
403 filename
= tmp_filename
;
405 bs
->is_temporary
= 1;
408 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
409 if (flags
& BDRV_O_FILE
) {
410 drv
= find_protocol(filename
);
412 drv
= find_image_format(filename
);
416 goto unlink_and_fail
;
419 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
420 /* Note: for compatibility, we open disk image files as RDWR, and
421 RDONLY as fallback */
422 if (!(flags
& BDRV_O_FILE
))
423 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_CACHE_MASK
);
425 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
426 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
427 if ((ret
== -EACCES
|| ret
== -EPERM
) && !(flags
& BDRV_O_FILE
)) {
428 ret
= drv
->bdrv_open(bs
, filename
, open_flags
& ~BDRV_O_RDWR
);
432 qemu_free(bs
->opaque
);
436 if (bs
->is_temporary
)
440 if (drv
->bdrv_getlength
) {
441 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
444 if (bs
->is_temporary
) {
448 if (bs
->backing_file
[0] != '\0') {
449 /* if there is a backing file, use it */
450 BlockDriver
*back_drv
= NULL
;
451 bs
->backing_hd
= bdrv_new("");
452 path_combine(backing_filename
, sizeof(backing_filename
),
453 filename
, bs
->backing_file
);
454 if (bs
->backing_format
[0] != '\0')
455 back_drv
= bdrv_find_format(bs
->backing_format
);
456 ret
= bdrv_open2(bs
->backing_hd
, backing_filename
, open_flags
,
464 if (!bdrv_key_required(bs
)) {
465 /* call the change callback */
466 bs
->media_changed
= 1;
468 bs
->change_cb(bs
->change_opaque
);
473 void bdrv_close(BlockDriverState
*bs
)
477 bdrv_delete(bs
->backing_hd
);
478 bs
->drv
->bdrv_close(bs
);
479 qemu_free(bs
->opaque
);
481 if (bs
->is_temporary
) {
482 unlink(bs
->filename
);
488 /* call the change callback */
489 bs
->media_changed
= 1;
491 bs
->change_cb(bs
->change_opaque
);
495 void bdrv_delete(BlockDriverState
*bs
)
497 BlockDriverState
**pbs
;
500 while (*pbs
!= bs
&& *pbs
!= NULL
)
510 * Run consistency checks on an image
512 * Returns the number of errors or -errno when an internal error occurs
514 int bdrv_check(BlockDriverState
*bs
)
516 if (bs
->drv
->bdrv_check
== NULL
) {
520 return bs
->drv
->bdrv_check(bs
);
523 /* commit COW file into the raw image */
524 int bdrv_commit(BlockDriverState
*bs
)
526 BlockDriver
*drv
= bs
->drv
;
527 int64_t i
, total_sectors
;
529 unsigned char sector
[512];
538 if (!bs
->backing_hd
) {
542 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
543 for (i
= 0; i
< total_sectors
;) {
544 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
545 for(j
= 0; j
< n
; j
++) {
546 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
550 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
560 if (drv
->bdrv_make_empty
)
561 return drv
->bdrv_make_empty(bs
);
566 static int bdrv_check_byte_request(BlockDriverState
*bs
, int64_t offset
,
571 if (!bdrv_is_inserted(bs
))
577 len
= bdrv_getlength(bs
);
579 if ((offset
+ size
) > len
)
585 static int bdrv_check_request(BlockDriverState
*bs
, int64_t sector_num
,
588 return bdrv_check_byte_request(bs
, sector_num
* 512, nb_sectors
* 512);
591 /* return < 0 if error. See bdrv_write() for the return codes */
592 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
593 uint8_t *buf
, int nb_sectors
)
595 BlockDriver
*drv
= bs
->drv
;
599 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
602 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
605 /* Return < 0 if error. Important errors are:
606 -EIO generic I/O error (may happen for all errors)
607 -ENOMEDIUM No media inserted.
608 -EINVAL Invalid sector number or nb_sectors
609 -EACCES Trying to write a read-only device
611 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
612 const uint8_t *buf
, int nb_sectors
)
614 BlockDriver
*drv
= bs
->drv
;
619 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
622 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
625 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
626 void *buf
, int count1
)
628 uint8_t tmp_buf
[SECTOR_SIZE
];
629 int len
, nb_sectors
, count
;
633 /* first read 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(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
649 /* read the sectors "in place" */
650 nb_sectors
= count
>> SECTOR_BITS
;
651 if (nb_sectors
> 0) {
652 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
654 sector_num
+= nb_sectors
;
655 len
= nb_sectors
<< SECTOR_BITS
;
660 /* add data from the last sector */
662 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
664 memcpy(buf
, tmp_buf
, count
);
669 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
670 const void *buf
, int count1
)
672 uint8_t tmp_buf
[SECTOR_SIZE
];
673 int len
, nb_sectors
, count
;
677 /* first write to align to sector start */
678 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
681 sector_num
= offset
>> SECTOR_BITS
;
683 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
685 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
686 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
695 /* write the sectors "in place" */
696 nb_sectors
= count
>> SECTOR_BITS
;
697 if (nb_sectors
> 0) {
698 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
700 sector_num
+= nb_sectors
;
701 len
= nb_sectors
<< SECTOR_BITS
;
706 /* add data from the last sector */
708 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
710 memcpy(tmp_buf
, buf
, count
);
711 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
718 * Truncate file to 'offset' bytes (needed only for file protocols)
720 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
722 BlockDriver
*drv
= bs
->drv
;
725 if (!drv
->bdrv_truncate
)
727 return drv
->bdrv_truncate(bs
, offset
);
731 * Length of a file in bytes. Return < 0 if error or unknown.
733 int64_t bdrv_getlength(BlockDriverState
*bs
)
735 BlockDriver
*drv
= bs
->drv
;
738 if (!drv
->bdrv_getlength
) {
740 return bs
->total_sectors
* SECTOR_SIZE
;
742 return drv
->bdrv_getlength(bs
);
745 /* return 0 as number of sectors if no device present or error */
746 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
749 length
= bdrv_getlength(bs
);
753 length
= length
>> SECTOR_BITS
;
754 *nb_sectors_ptr
= length
;
758 uint8_t boot_ind
; /* 0x80 - active */
759 uint8_t head
; /* starting head */
760 uint8_t sector
; /* starting sector */
761 uint8_t cyl
; /* starting cylinder */
762 uint8_t sys_ind
; /* What partition type */
763 uint8_t end_head
; /* end head */
764 uint8_t end_sector
; /* end sector */
765 uint8_t end_cyl
; /* end cylinder */
766 uint32_t start_sect
; /* starting sector counting from 0 */
767 uint32_t nr_sects
; /* nr of sectors in partition */
768 } __attribute__((packed
));
770 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
771 static int guess_disk_lchs(BlockDriverState
*bs
,
772 int *pcylinders
, int *pheads
, int *psectors
)
775 int ret
, i
, heads
, sectors
, cylinders
;
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)
788 for(i
= 0; i
< 4; i
++) {
789 p
= ((struct partition
*)(buf
+ 0x1be)) + i
;
790 nr_sects
= le32_to_cpu(p
->nr_sects
);
791 if (nr_sects
&& p
->end_head
) {
792 /* We make the assumption that the partition terminates on
793 a cylinder boundary */
794 heads
= p
->end_head
+ 1;
795 sectors
= p
->end_sector
& 63;
798 cylinders
= nb_sectors
/ (heads
* sectors
);
799 if (cylinders
< 1 || cylinders
> 16383)
803 *pcylinders
= cylinders
;
805 printf("guessed geometry: LCHS=%d %d %d\n",
806 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_key_required(BlockDriverState
*bs
)
943 BlockDriverState
*backing_hd
= bs
->backing_hd
;
945 if (backing_hd
&& backing_hd
->encrypted
&& !backing_hd
->valid_key
)
947 return (bs
->encrypted
&& !bs
->valid_key
);
950 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
953 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
954 ret
= bdrv_set_key(bs
->backing_hd
, key
);
960 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
962 ret
= bs
->drv
->bdrv_set_key(bs
, key
);
965 } else if (!bs
->valid_key
) {
967 /* call the change callback now, we skipped it on open */
968 bs
->media_changed
= 1;
970 bs
->change_cb(bs
->change_opaque
);
975 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
980 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
984 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
989 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
990 it(opaque
, drv
->format_name
);
994 BlockDriverState
*bdrv_find(const char *name
)
996 BlockDriverState
*bs
;
998 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
999 if (!strcmp(name
, bs
->device_name
))
1005 void bdrv_iterate(void (*it
)(void *opaque
, BlockDriverState
*bs
), void *opaque
)
1007 BlockDriverState
*bs
;
1009 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1014 const char *bdrv_get_device_name(BlockDriverState
*bs
)
1016 return bs
->device_name
;
1019 void bdrv_flush(BlockDriverState
*bs
)
1023 if (bs
->drv
->bdrv_flush
)
1024 bs
->drv
->bdrv_flush(bs
);
1026 bdrv_flush(bs
->backing_hd
);
1029 void bdrv_flush_all(void)
1031 BlockDriverState
*bs
;
1033 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
)
1034 if (bs
->drv
&& !bdrv_is_read_only(bs
) &&
1035 (!bdrv_is_removable(bs
) || bdrv_is_inserted(bs
)))
1040 * Returns true iff the specified sector is present in the disk image. Drivers
1041 * not implementing the functionality are assumed to not support backing files,
1042 * hence all their sectors are reported as allocated.
1044 * 'pnum' is set to the number of sectors (including and immediately following
1045 * the specified sector) that are known to be in the same
1046 * allocated/unallocated state.
1048 * 'nb_sectors' is the max value 'pnum' should be set to.
1050 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
1054 if (!bs
->drv
->bdrv_is_allocated
) {
1055 if (sector_num
>= bs
->total_sectors
) {
1059 n
= bs
->total_sectors
- sector_num
;
1060 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
1063 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
1066 void bdrv_info(Monitor
*mon
)
1068 BlockDriverState
*bs
;
1070 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1071 monitor_printf(mon
, "%s:", bs
->device_name
);
1072 monitor_printf(mon
, " type=");
1075 monitor_printf(mon
, "hd");
1077 case BDRV_TYPE_CDROM
:
1078 monitor_printf(mon
, "cdrom");
1080 case BDRV_TYPE_FLOPPY
:
1081 monitor_printf(mon
, "floppy");
1084 monitor_printf(mon
, " removable=%d", bs
->removable
);
1085 if (bs
->removable
) {
1086 monitor_printf(mon
, " locked=%d", bs
->locked
);
1089 monitor_printf(mon
, " file=");
1090 monitor_print_filename(mon
, bs
->filename
);
1091 if (bs
->backing_file
[0] != '\0') {
1092 monitor_printf(mon
, " backing_file=");
1093 monitor_print_filename(mon
, bs
->backing_file
);
1095 monitor_printf(mon
, " ro=%d", bs
->read_only
);
1096 monitor_printf(mon
, " drv=%s", bs
->drv
->format_name
);
1097 monitor_printf(mon
, " encrypted=%d", bdrv_is_encrypted(bs
));
1099 monitor_printf(mon
, " [not inserted]");
1101 monitor_printf(mon
, "\n");
1105 /* The "info blockstats" command. */
1106 void bdrv_info_stats(Monitor
*mon
)
1108 BlockDriverState
*bs
;
1110 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
1111 monitor_printf(mon
, "%s:"
1112 " rd_bytes=%" PRIu64
1113 " wr_bytes=%" PRIu64
1114 " rd_operations=%" PRIu64
1115 " wr_operations=%" PRIu64
1118 bs
->rd_bytes
, bs
->wr_bytes
,
1119 bs
->rd_ops
, bs
->wr_ops
);
1123 const char *bdrv_get_encrypted_filename(BlockDriverState
*bs
)
1125 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
1126 return bs
->backing_file
;
1127 else if (bs
->encrypted
)
1128 return bs
->filename
;
1133 void bdrv_get_backing_filename(BlockDriverState
*bs
,
1134 char *filename
, int filename_size
)
1136 if (!bs
->backing_hd
) {
1137 pstrcpy(filename
, filename_size
, "");
1139 pstrcpy(filename
, filename_size
, bs
->backing_file
);
1143 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
1144 const uint8_t *buf
, int nb_sectors
)
1146 BlockDriver
*drv
= bs
->drv
;
1149 if (!drv
->bdrv_write_compressed
)
1151 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1154 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1156 BlockDriver
*drv
= bs
->drv
;
1159 if (!drv
->bdrv_get_info
)
1161 memset(bdi
, 0, sizeof(*bdi
));
1162 return drv
->bdrv_get_info(bs
, bdi
);
1165 int bdrv_put_buffer(BlockDriverState
*bs
, const uint8_t *buf
, int64_t pos
, int size
)
1167 BlockDriver
*drv
= bs
->drv
;
1170 if (!drv
->bdrv_put_buffer
)
1172 return drv
->bdrv_put_buffer(bs
, buf
, pos
, size
);
1175 int bdrv_get_buffer(BlockDriverState
*bs
, uint8_t *buf
, int64_t pos
, int size
)
1177 BlockDriver
*drv
= bs
->drv
;
1180 if (!drv
->bdrv_get_buffer
)
1182 return drv
->bdrv_get_buffer(bs
, buf
, pos
, size
);
1185 /**************************************************************/
1186 /* handling of snapshots */
1188 int bdrv_snapshot_create(BlockDriverState
*bs
,
1189 QEMUSnapshotInfo
*sn_info
)
1191 BlockDriver
*drv
= bs
->drv
;
1194 if (!drv
->bdrv_snapshot_create
)
1196 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1199 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1200 const char *snapshot_id
)
1202 BlockDriver
*drv
= bs
->drv
;
1205 if (!drv
->bdrv_snapshot_goto
)
1207 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1210 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1212 BlockDriver
*drv
= bs
->drv
;
1215 if (!drv
->bdrv_snapshot_delete
)
1217 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1220 int bdrv_snapshot_list(BlockDriverState
*bs
,
1221 QEMUSnapshotInfo
**psn_info
)
1223 BlockDriver
*drv
= bs
->drv
;
1226 if (!drv
->bdrv_snapshot_list
)
1228 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1231 #define NB_SUFFIXES 4
1233 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1235 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1240 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1243 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1244 if (size
< (10 * base
)) {
1245 snprintf(buf
, buf_size
, "%0.1f%c",
1246 (double)size
/ base
,
1249 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1250 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1251 ((size
+ (base
>> 1)) / base
),
1261 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1263 char buf1
[128], date_buf
[128], clock_buf
[128];
1273 snprintf(buf
, buf_size
,
1274 "%-10s%-20s%7s%20s%15s",
1275 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1279 ptm
= localtime(&ti
);
1280 strftime(date_buf
, sizeof(date_buf
),
1281 "%Y-%m-%d %H:%M:%S", ptm
);
1283 localtime_r(&ti
, &tm
);
1284 strftime(date_buf
, sizeof(date_buf
),
1285 "%Y-%m-%d %H:%M:%S", &tm
);
1287 secs
= sn
->vm_clock_nsec
/ 1000000000;
1288 snprintf(clock_buf
, sizeof(clock_buf
),
1289 "%02d:%02d:%02d.%03d",
1291 (int)((secs
/ 60) % 60),
1293 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1294 snprintf(buf
, buf_size
,
1295 "%-10s%-20s%7s%20s%15s",
1296 sn
->id_str
, sn
->name
,
1297 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1305 /**************************************************************/
1308 BlockDriverAIOCB
*bdrv_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1309 QEMUIOVector
*qiov
, int nb_sectors
,
1310 BlockDriverCompletionFunc
*cb
, void *opaque
)
1312 BlockDriver
*drv
= bs
->drv
;
1313 BlockDriverAIOCB
*ret
;
1317 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1320 ret
= drv
->bdrv_aio_readv(bs
, sector_num
, qiov
, nb_sectors
,
1324 /* Update stats even though technically transfer has not happened. */
1325 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1332 BlockDriverAIOCB
*bdrv_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1333 QEMUIOVector
*qiov
, int nb_sectors
,
1334 BlockDriverCompletionFunc
*cb
, void *opaque
)
1336 BlockDriver
*drv
= bs
->drv
;
1337 BlockDriverAIOCB
*ret
;
1343 if (bdrv_check_request(bs
, sector_num
, nb_sectors
))
1346 ret
= drv
->bdrv_aio_writev(bs
, sector_num
, qiov
, nb_sectors
,
1350 /* Update stats even though technically transfer has not happened. */
1351 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1358 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1360 acb
->pool
->cancel(acb
);
1364 /**************************************************************/
1365 /* async block device emulation */
1367 static void bdrv_aio_bh_cb(void *opaque
)
1369 BlockDriverAIOCBSync
*acb
= opaque
;
1372 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
1373 qemu_vfree(acb
->bounce
);
1374 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1376 qemu_aio_release(acb
);
1379 static BlockDriverAIOCB
*bdrv_aio_rw_vector(BlockDriverState
*bs
,
1383 BlockDriverCompletionFunc
*cb
,
1388 BlockDriverAIOCBSync
*acb
;
1390 acb
= qemu_aio_get(bs
, cb
, opaque
);
1391 acb
->is_write
= is_write
;
1393 acb
->bounce
= qemu_memalign(512, qiov
->size
);
1396 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1399 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
1400 acb
->ret
= bdrv_write(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1402 acb
->ret
= bdrv_read(bs
, sector_num
, acb
->bounce
, nb_sectors
);
1405 qemu_bh_schedule(acb
->bh
);
1407 return &acb
->common
;
1410 static BlockDriverAIOCB
*bdrv_aio_readv_em(BlockDriverState
*bs
,
1411 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1412 BlockDriverCompletionFunc
*cb
, void *opaque
)
1414 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
1417 static BlockDriverAIOCB
*bdrv_aio_writev_em(BlockDriverState
*bs
,
1418 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1419 BlockDriverCompletionFunc
*cb
, void *opaque
)
1421 return bdrv_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
1425 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1427 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1428 qemu_bh_cancel(acb
->bh
);
1429 qemu_aio_release(acb
);
1432 /**************************************************************/
1433 /* sync block device emulation */
1435 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1437 *(int *)opaque
= ret
;
1440 #define NOT_DONE 0x7fffffff
1442 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1443 uint8_t *buf
, int nb_sectors
)
1446 BlockDriverAIOCB
*acb
;
1450 async_ret
= NOT_DONE
;
1451 iov
.iov_base
= (void *)buf
;
1452 iov
.iov_len
= nb_sectors
* 512;
1453 qemu_iovec_init_external(&qiov
, &iov
, 1);
1454 acb
= bdrv_aio_readv(bs
, sector_num
, &qiov
, nb_sectors
,
1455 bdrv_rw_em_cb
, &async_ret
);
1459 while (async_ret
== NOT_DONE
) {
1466 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1467 const uint8_t *buf
, int nb_sectors
)
1470 BlockDriverAIOCB
*acb
;
1474 async_ret
= NOT_DONE
;
1475 iov
.iov_base
= (void *)buf
;
1476 iov
.iov_len
= nb_sectors
* 512;
1477 qemu_iovec_init_external(&qiov
, &iov
, 1);
1478 acb
= bdrv_aio_writev(bs
, sector_num
, &qiov
, nb_sectors
,
1479 bdrv_rw_em_cb
, &async_ret
);
1482 while (async_ret
== NOT_DONE
) {
1488 void bdrv_init(void)
1490 bdrv_register(&bdrv_raw
);
1491 bdrv_register(&bdrv_host_device
);
1493 bdrv_register(&bdrv_cow
);
1495 bdrv_register(&bdrv_qcow
);
1496 bdrv_register(&bdrv_vmdk
);
1497 bdrv_register(&bdrv_cloop
);
1498 bdrv_register(&bdrv_dmg
);
1499 bdrv_register(&bdrv_bochs
);
1500 bdrv_register(&bdrv_vpc
);
1501 bdrv_register(&bdrv_vvfat
);
1502 bdrv_register(&bdrv_qcow2
);
1503 bdrv_register(&bdrv_parallels
);
1504 bdrv_register(&bdrv_nbd
);
1507 void aio_pool_init(AIOPool
*pool
, int aiocb_size
,
1508 void (*cancel
)(BlockDriverAIOCB
*acb
))
1510 pool
->aiocb_size
= aiocb_size
;
1511 pool
->cancel
= cancel
;
1512 pool
->free_aiocb
= NULL
;
1515 void *qemu_aio_get_pool(AIOPool
*pool
, BlockDriverState
*bs
,
1516 BlockDriverCompletionFunc
*cb
, void *opaque
)
1518 BlockDriverAIOCB
*acb
;
1520 if (pool
->free_aiocb
) {
1521 acb
= pool
->free_aiocb
;
1522 pool
->free_aiocb
= acb
->next
;
1524 acb
= qemu_mallocz(pool
->aiocb_size
);
1529 acb
->opaque
= opaque
;
1533 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1536 return qemu_aio_get_pool(&bs
->drv
->aio_pool
, bs
, cb
, opaque
);
1539 void qemu_aio_release(void *p
)
1541 BlockDriverAIOCB
*acb
= (BlockDriverAIOCB
*)p
;
1542 AIOPool
*pool
= acb
->pool
;
1543 acb
->next
= pool
->free_aiocb
;
1544 pool
->free_aiocb
= acb
;
1547 /**************************************************************/
1548 /* removable device support */
1551 * Return TRUE if the media is present
1553 int bdrv_is_inserted(BlockDriverState
*bs
)
1555 BlockDriver
*drv
= bs
->drv
;
1559 if (!drv
->bdrv_is_inserted
)
1561 ret
= drv
->bdrv_is_inserted(bs
);
1566 * Return TRUE if the media changed since the last call to this
1567 * function. It is currently only used for floppy disks
1569 int bdrv_media_changed(BlockDriverState
*bs
)
1571 BlockDriver
*drv
= bs
->drv
;
1574 if (!drv
|| !drv
->bdrv_media_changed
)
1577 ret
= drv
->bdrv_media_changed(bs
);
1578 if (ret
== -ENOTSUP
)
1579 ret
= bs
->media_changed
;
1580 bs
->media_changed
= 0;
1585 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1587 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1589 BlockDriver
*drv
= bs
->drv
;
1592 if (!drv
|| !drv
->bdrv_eject
) {
1595 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1597 if (ret
== -ENOTSUP
) {
1603 int bdrv_is_locked(BlockDriverState
*bs
)
1609 * Lock or unlock the media (if it is locked, the user won't be able
1610 * to eject it manually).
1612 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1614 BlockDriver
*drv
= bs
->drv
;
1616 bs
->locked
= locked
;
1617 if (drv
&& drv
->bdrv_set_locked
) {
1618 drv
->bdrv_set_locked(bs
, locked
);
1622 /* needed for generic scsi interface */
1624 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1626 BlockDriver
*drv
= bs
->drv
;
1628 if (drv
&& drv
->bdrv_ioctl
)
1629 return drv
->bdrv_ioctl(bs
, req
, buf
);
1633 BlockDriverAIOCB
*bdrv_aio_ioctl(BlockDriverState
*bs
,
1634 unsigned long int req
, void *buf
,
1635 BlockDriverCompletionFunc
*cb
, void *opaque
)
1637 BlockDriver
*drv
= bs
->drv
;
1639 if (drv
&& drv
->bdrv_aio_ioctl
)
1640 return drv
->bdrv_aio_ioctl(bs
, req
, buf
, cb
, opaque
);