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"
31 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/queue.h>
39 #define SECTOR_SIZE (1 << SECTOR_BITS)
41 typedef struct BlockDriverAIOCBSync
{
42 BlockDriverAIOCB common
;
45 } BlockDriverAIOCBSync
;
47 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
48 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
49 BlockDriverCompletionFunc
*cb
, void *opaque
);
50 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
51 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
52 BlockDriverCompletionFunc
*cb
, void *opaque
);
53 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
54 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
55 uint8_t *buf
, int nb_sectors
);
56 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
57 const uint8_t *buf
, int nb_sectors
);
59 BlockDriverState
*bdrv_first
;
60 static BlockDriver
*first_drv
;
62 int path_is_absolute(const char *path
)
66 /* specific case for names like: "\\.\d:" */
67 if (*path
== '/' || *path
== '\\')
70 p
= strchr(path
, ':');
76 return (*p
== '/' || *p
== '\\');
82 /* if filename is absolute, just copy it to dest. Otherwise, build a
83 path to it by considering it is relative to base_path. URL are
85 void path_combine(char *dest
, int dest_size
,
86 const char *base_path
,
94 if (path_is_absolute(filename
)) {
95 pstrcpy(dest
, dest_size
, filename
);
97 p
= strchr(base_path
, ':');
102 p1
= strrchr(base_path
, '/');
106 p2
= strrchr(base_path
, '\\');
118 if (len
> dest_size
- 1)
120 memcpy(dest
, base_path
, len
);
122 pstrcat(dest
, dest_size
, filename
);
127 static void bdrv_register(BlockDriver
*bdrv
)
129 if (!bdrv
->bdrv_aio_read
) {
130 /* add AIO emulation layer */
131 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
132 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
133 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
134 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
135 } else if (!bdrv
->bdrv_read
&& !bdrv
->bdrv_pread
) {
136 /* add synchronous IO emulation layer */
137 bdrv
->bdrv_read
= bdrv_read_em
;
138 bdrv
->bdrv_write
= bdrv_write_em
;
140 bdrv
->next
= first_drv
;
144 /* create a new block device (by default it is empty) */
145 BlockDriverState
*bdrv_new(const char *device_name
)
147 BlockDriverState
**pbs
, *bs
;
149 bs
= qemu_mallocz(sizeof(BlockDriverState
));
152 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
153 if (device_name
[0] != '\0') {
154 /* insert at the end */
163 BlockDriver
*bdrv_find_format(const char *format_name
)
166 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
167 if (!strcmp(drv1
->format_name
, format_name
))
173 int bdrv_create(BlockDriver
*drv
,
174 const char *filename
, int64_t size_in_sectors
,
175 const char *backing_file
, int flags
)
177 if (!drv
->bdrv_create
)
179 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
183 void get_tmp_filename(char *filename
, int size
)
185 char temp_dir
[MAX_PATH
];
187 GetTempPath(MAX_PATH
, temp_dir
);
188 GetTempFileName(temp_dir
, "qem", 0, filename
);
191 void get_tmp_filename(char *filename
, int size
)
195 /* XXX: race condition possible */
196 tmpdir
= getenv("TMPDIR");
199 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
200 fd
= mkstemp(filename
);
206 static int is_windows_drive_prefix(const char *filename
)
208 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
209 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
213 static int is_windows_drive(const char *filename
)
215 if (is_windows_drive_prefix(filename
) &&
218 if (strstart(filename
, "\\\\.\\", NULL
) ||
219 strstart(filename
, "//./", NULL
))
225 static BlockDriver
*find_protocol(const char *filename
)
233 if (is_windows_drive(filename
) ||
234 is_windows_drive_prefix(filename
))
237 p
= strchr(filename
, ':');
241 if (len
> sizeof(protocol
) - 1)
242 len
= sizeof(protocol
) - 1;
243 memcpy(protocol
, filename
, len
);
244 protocol
[len
] = '\0';
245 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
246 if (drv1
->protocol_name
&&
247 !strcmp(drv1
->protocol_name
, protocol
))
253 /* XXX: force raw format if block or character device ? It would
254 simplify the BSD case */
255 static BlockDriver
*find_image_format(const char *filename
)
257 int ret
, score
, score_max
;
258 BlockDriver
*drv1
, *drv
;
260 BlockDriverState
*bs
;
262 /* detect host devices. By convention, /dev/cdrom[N] is always
263 recognized as a host CDROM */
264 if (strstart(filename
, "/dev/cdrom", NULL
))
265 return &bdrv_host_device
;
267 if (is_windows_drive(filename
))
268 return &bdrv_host_device
;
272 if (stat(filename
, &st
) >= 0 &&
273 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
274 return &bdrv_host_device
;
279 drv
= find_protocol(filename
);
280 /* no need to test disk image formats for vvfat */
281 if (drv
== &bdrv_vvfat
)
284 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
287 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
294 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
295 if (drv1
->bdrv_probe
) {
296 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
297 if (score
> score_max
) {
306 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
308 BlockDriverState
*bs
;
314 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
323 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
325 return bdrv_open2(bs
, filename
, flags
, NULL
);
328 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
332 char tmp_filename
[PATH_MAX
];
333 char backing_filename
[PATH_MAX
];
336 bs
->is_temporary
= 0;
339 if (flags
& BDRV_O_SNAPSHOT
) {
340 BlockDriverState
*bs1
;
343 /* if snapshot, we create a temporary backing file and open it
344 instead of opening 'filename' directly */
346 /* if there is a backing file, use it */
351 if (bdrv_open(bs1
, filename
, 0) < 0) {
355 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
358 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
359 realpath(filename
, backing_filename
);
360 if (bdrv_create(&bdrv_qcow2
, tmp_filename
,
361 total_size
, backing_filename
, 0) < 0) {
364 filename
= tmp_filename
;
365 bs
->is_temporary
= 1;
368 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
369 if (flags
& BDRV_O_FILE
) {
370 drv
= find_protocol(filename
);
375 drv
= find_image_format(filename
);
381 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
382 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
384 /* Note: for compatibility, we open disk image files as RDWR, and
385 RDONLY as fallback */
386 if (!(flags
& BDRV_O_FILE
))
387 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_DIRECT
);
389 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
390 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
391 if (ret
== -EACCES
&& !(flags
& BDRV_O_FILE
)) {
392 ret
= drv
->bdrv_open(bs
, filename
, BDRV_O_RDONLY
);
396 qemu_free(bs
->opaque
);
401 if (drv
->bdrv_getlength
) {
402 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
405 if (bs
->is_temporary
) {
409 if (bs
->backing_file
[0] != '\0') {
410 /* if there is a backing file, use it */
411 bs
->backing_hd
= bdrv_new("");
412 if (!bs
->backing_hd
) {
417 path_combine(backing_filename
, sizeof(backing_filename
),
418 filename
, bs
->backing_file
);
419 if (bdrv_open(bs
->backing_hd
, backing_filename
, 0) < 0)
423 /* call the change callback */
424 bs
->media_changed
= 1;
426 bs
->change_cb(bs
->change_opaque
);
431 void bdrv_close(BlockDriverState
*bs
)
435 bdrv_delete(bs
->backing_hd
);
436 bs
->drv
->bdrv_close(bs
);
437 qemu_free(bs
->opaque
);
439 if (bs
->is_temporary
) {
440 unlink(bs
->filename
);
446 /* call the change callback */
447 bs
->media_changed
= 1;
449 bs
->change_cb(bs
->change_opaque
);
453 void bdrv_delete(BlockDriverState
*bs
)
455 BlockDriverState
**pbs
;
458 while (*pbs
!= bs
&& *pbs
!= NULL
)
467 /* commit COW file into the raw image */
468 int bdrv_commit(BlockDriverState
*bs
)
470 BlockDriver
*drv
= bs
->drv
;
471 int64_t i
, total_sectors
;
473 unsigned char sector
[512];
482 if (!bs
->backing_hd
) {
486 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
487 for (i
= 0; i
< total_sectors
;) {
488 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
489 for(j
= 0; j
< n
; j
++) {
490 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
494 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
504 if (drv
->bdrv_make_empty
)
505 return drv
->bdrv_make_empty(bs
);
510 /* return < 0 if error. See bdrv_write() for the return codes */
511 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
512 uint8_t *buf
, int nb_sectors
)
514 BlockDriver
*drv
= bs
->drv
;
519 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
520 memcpy(buf
, bs
->boot_sector_data
, 512);
527 if (drv
->bdrv_pread
) {
529 len
= nb_sectors
* 512;
530 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
536 bs
->rd_bytes
+= (unsigned) len
;
541 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
545 /* Return < 0 if error. Important errors are:
546 -EIO generic I/O error (may happen for all errors)
547 -ENOMEDIUM No media inserted.
548 -EINVAL Invalid sector number or nb_sectors
549 -EACCES Trying to write a read-only device
551 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
552 const uint8_t *buf
, int nb_sectors
)
554 BlockDriver
*drv
= bs
->drv
;
559 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
560 memcpy(bs
->boot_sector_data
, buf
, 512);
562 if (drv
->bdrv_pwrite
) {
564 len
= nb_sectors
* 512;
565 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
);
571 bs
->wr_bytes
+= (unsigned) len
;
576 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
580 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
581 uint8_t *buf
, int count1
)
583 uint8_t tmp_buf
[SECTOR_SIZE
];
584 int len
, nb_sectors
, count
;
588 /* first read to align to sector start */
589 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
592 sector_num
= offset
>> SECTOR_BITS
;
594 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
596 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
604 /* read the sectors "in place" */
605 nb_sectors
= count
>> SECTOR_BITS
;
606 if (nb_sectors
> 0) {
607 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
609 sector_num
+= nb_sectors
;
610 len
= nb_sectors
<< SECTOR_BITS
;
615 /* add data from the last sector */
617 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
619 memcpy(buf
, tmp_buf
, count
);
624 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
625 const uint8_t *buf
, int count1
)
627 uint8_t tmp_buf
[SECTOR_SIZE
];
628 int len
, nb_sectors
, count
;
632 /* first write to align to sector start */
633 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
636 sector_num
= offset
>> SECTOR_BITS
;
638 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
640 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
641 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
650 /* write the sectors "in place" */
651 nb_sectors
= count
>> SECTOR_BITS
;
652 if (nb_sectors
> 0) {
653 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
655 sector_num
+= nb_sectors
;
656 len
= nb_sectors
<< SECTOR_BITS
;
661 /* add data from the last sector */
663 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
665 memcpy(tmp_buf
, buf
, count
);
666 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
673 * Read with byte offsets (needed only for file protocols)
675 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
676 void *buf1
, int count1
)
678 BlockDriver
*drv
= bs
->drv
;
682 if (!drv
->bdrv_pread
)
683 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
684 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
688 * Write with byte offsets (needed only for file protocols)
690 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
691 const void *buf1
, int count1
)
693 BlockDriver
*drv
= bs
->drv
;
697 if (!drv
->bdrv_pwrite
)
698 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
699 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
703 * Truncate file to 'offset' bytes (needed only for file protocols)
705 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
707 BlockDriver
*drv
= bs
->drv
;
710 if (!drv
->bdrv_truncate
)
712 return drv
->bdrv_truncate(bs
, offset
);
716 * Length of a file in bytes. Return < 0 if error or unknown.
718 int64_t bdrv_getlength(BlockDriverState
*bs
)
720 BlockDriver
*drv
= bs
->drv
;
723 if (!drv
->bdrv_getlength
) {
725 return bs
->total_sectors
* SECTOR_SIZE
;
727 return drv
->bdrv_getlength(bs
);
730 /* return 0 as number of sectors if no device present or error */
731 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
734 length
= bdrv_getlength(bs
);
738 length
= length
>> SECTOR_BITS
;
739 *nb_sectors_ptr
= length
;
742 /* force a given boot sector. */
743 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
)
745 bs
->boot_sector_enabled
= 1;
748 memcpy(bs
->boot_sector_data
, data
, size
);
749 memset(bs
->boot_sector_data
+ size
, 0, 512 - size
);
752 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
753 int cyls
, int heads
, int secs
)
760 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
763 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
764 type
== BDRV_TYPE_FLOPPY
));
767 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
769 bs
->translation
= translation
;
772 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
773 int *pcyls
, int *pheads
, int *psecs
)
780 int bdrv_get_type_hint(BlockDriverState
*bs
)
785 int bdrv_get_translation_hint(BlockDriverState
*bs
)
787 return bs
->translation
;
790 int bdrv_is_removable(BlockDriverState
*bs
)
792 return bs
->removable
;
795 int bdrv_is_read_only(BlockDriverState
*bs
)
797 return bs
->read_only
;
800 int bdrv_is_sg(BlockDriverState
*bs
)
805 /* XXX: no longer used */
806 void bdrv_set_change_cb(BlockDriverState
*bs
,
807 void (*change_cb
)(void *opaque
), void *opaque
)
809 bs
->change_cb
= change_cb
;
810 bs
->change_opaque
= opaque
;
813 int bdrv_is_encrypted(BlockDriverState
*bs
)
815 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
817 return bs
->encrypted
;
820 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
823 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
824 ret
= bdrv_set_key(bs
->backing_hd
, key
);
830 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
832 return bs
->drv
->bdrv_set_key(bs
, key
);
835 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
840 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
844 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
849 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
850 it(opaque
, drv
->format_name
);
854 BlockDriverState
*bdrv_find(const char *name
)
856 BlockDriverState
*bs
;
858 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
859 if (!strcmp(name
, bs
->device_name
))
865 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
867 BlockDriverState
*bs
;
869 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
870 it(opaque
, bs
->device_name
);
874 const char *bdrv_get_device_name(BlockDriverState
*bs
)
876 return bs
->device_name
;
879 void bdrv_flush(BlockDriverState
*bs
)
881 if (bs
->drv
->bdrv_flush
)
882 bs
->drv
->bdrv_flush(bs
);
884 bdrv_flush(bs
->backing_hd
);
888 * Returns true iff the specified sector is present in the disk image. Drivers
889 * not implementing the functionality are assumed to not support backing files,
890 * hence all their sectors are reported as allocated.
892 * 'pnum' is set to the number of sectors (including and immediately following
893 * the specified sector) that are known to be in the same
894 * allocated/unallocated state.
896 * 'nb_sectors' is the max value 'pnum' should be set to.
898 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
902 if (!bs
->drv
->bdrv_is_allocated
) {
903 if (sector_num
>= bs
->total_sectors
) {
907 n
= bs
->total_sectors
- sector_num
;
908 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
911 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
917 BlockDriverState
*bs
;
919 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
920 term_printf("%s:", bs
->device_name
);
921 term_printf(" type=");
926 case BDRV_TYPE_CDROM
:
927 term_printf("cdrom");
929 case BDRV_TYPE_FLOPPY
:
930 term_printf("floppy");
933 term_printf(" removable=%d", bs
->removable
);
935 term_printf(" locked=%d", bs
->locked
);
938 term_printf(" file=");
939 term_print_filename(bs
->filename
);
940 if (bs
->backing_file
[0] != '\0') {
941 term_printf(" backing_file=");
942 term_print_filename(bs
->backing_file
);
944 term_printf(" ro=%d", bs
->read_only
);
945 term_printf(" drv=%s", bs
->drv
->format_name
);
947 term_printf(" encrypted");
949 term_printf(" [not inserted]");
955 /* The "info blockstats" command. */
956 void bdrv_info_stats (void)
958 BlockDriverState
*bs
;
960 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
964 " rd_operations=%" PRIu64
965 " wr_operations=%" PRIu64
968 bs
->rd_bytes
, bs
->wr_bytes
,
969 bs
->rd_ops
, bs
->wr_ops
);
974 void bdrv_get_backing_filename(BlockDriverState
*bs
,
975 char *filename
, int filename_size
)
977 if (!bs
->backing_hd
) {
978 pstrcpy(filename
, filename_size
, "");
980 pstrcpy(filename
, filename_size
, bs
->backing_file
);
984 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
985 const uint8_t *buf
, int nb_sectors
)
987 BlockDriver
*drv
= bs
->drv
;
990 if (!drv
->bdrv_write_compressed
)
992 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
995 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
997 BlockDriver
*drv
= bs
->drv
;
1000 if (!drv
->bdrv_get_info
)
1002 memset(bdi
, 0, sizeof(*bdi
));
1003 return drv
->bdrv_get_info(bs
, bdi
);
1006 /**************************************************************/
1007 /* handling of snapshots */
1009 int bdrv_snapshot_create(BlockDriverState
*bs
,
1010 QEMUSnapshotInfo
*sn_info
)
1012 BlockDriver
*drv
= bs
->drv
;
1015 if (!drv
->bdrv_snapshot_create
)
1017 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1020 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1021 const char *snapshot_id
)
1023 BlockDriver
*drv
= bs
->drv
;
1026 if (!drv
->bdrv_snapshot_goto
)
1028 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1031 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1033 BlockDriver
*drv
= bs
->drv
;
1036 if (!drv
->bdrv_snapshot_delete
)
1038 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1041 int bdrv_snapshot_list(BlockDriverState
*bs
,
1042 QEMUSnapshotInfo
**psn_info
)
1044 BlockDriver
*drv
= bs
->drv
;
1047 if (!drv
->bdrv_snapshot_list
)
1049 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1052 #define NB_SUFFIXES 4
1054 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1056 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1061 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1064 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1065 if (size
< (10 * base
)) {
1066 snprintf(buf
, buf_size
, "%0.1f%c",
1067 (double)size
/ base
,
1070 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1071 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1072 ((size
+ (base
>> 1)) / base
),
1082 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1084 char buf1
[128], date_buf
[128], clock_buf
[128];
1094 snprintf(buf
, buf_size
,
1095 "%-10s%-20s%7s%20s%15s",
1096 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1100 ptm
= localtime(&ti
);
1101 strftime(date_buf
, sizeof(date_buf
),
1102 "%Y-%m-%d %H:%M:%S", ptm
);
1104 localtime_r(&ti
, &tm
);
1105 strftime(date_buf
, sizeof(date_buf
),
1106 "%Y-%m-%d %H:%M:%S", &tm
);
1108 secs
= sn
->vm_clock_nsec
/ 1000000000;
1109 snprintf(clock_buf
, sizeof(clock_buf
),
1110 "%02d:%02d:%02d.%03d",
1112 (int)((secs
/ 60) % 60),
1114 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1115 snprintf(buf
, buf_size
,
1116 "%-10s%-20s%7s%20s%15s",
1117 sn
->id_str
, sn
->name
,
1118 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1126 /**************************************************************/
1129 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1130 uint8_t *buf
, int nb_sectors
,
1131 BlockDriverCompletionFunc
*cb
, void *opaque
)
1133 BlockDriver
*drv
= bs
->drv
;
1134 BlockDriverAIOCB
*ret
;
1139 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1140 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1141 memcpy(buf
, bs
->boot_sector_data
, 512);
1147 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1150 /* Update stats even though technically transfer has not happened. */
1151 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1158 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1159 const uint8_t *buf
, int nb_sectors
,
1160 BlockDriverCompletionFunc
*cb
, void *opaque
)
1162 BlockDriver
*drv
= bs
->drv
;
1163 BlockDriverAIOCB
*ret
;
1169 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1170 memcpy(bs
->boot_sector_data
, buf
, 512);
1173 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1176 /* Update stats even though technically transfer has not happened. */
1177 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1184 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1186 BlockDriver
*drv
= acb
->bs
->drv
;
1188 drv
->bdrv_aio_cancel(acb
);
1192 /**************************************************************/
1193 /* async block device emulation */
1196 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1197 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1198 BlockDriverCompletionFunc
*cb
, void *opaque
)
1201 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1206 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1207 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1208 BlockDriverCompletionFunc
*cb
, void *opaque
)
1211 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1216 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
)
1220 static void bdrv_aio_bh_cb(void *opaque
)
1222 BlockDriverAIOCBSync
*acb
= opaque
;
1223 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1224 qemu_aio_release(acb
);
1227 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1228 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1229 BlockDriverCompletionFunc
*cb
, void *opaque
)
1231 BlockDriverAIOCBSync
*acb
;
1234 acb
= qemu_aio_get(bs
, cb
, opaque
);
1236 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1237 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1239 qemu_bh_schedule(acb
->bh
);
1240 return &acb
->common
;
1243 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1244 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1245 BlockDriverCompletionFunc
*cb
, void *opaque
)
1247 BlockDriverAIOCBSync
*acb
;
1250 acb
= qemu_aio_get(bs
, cb
, opaque
);
1252 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1253 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1255 qemu_bh_schedule(acb
->bh
);
1256 return &acb
->common
;
1259 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1261 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1262 qemu_bh_cancel(acb
->bh
);
1263 qemu_aio_release(acb
);
1265 #endif /* !QEMU_IMG */
1267 /**************************************************************/
1268 /* sync block device emulation */
1270 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1272 *(int *)opaque
= ret
;
1275 #define NOT_DONE 0x7fffffff
1277 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1278 uint8_t *buf
, int nb_sectors
)
1281 BlockDriverAIOCB
*acb
;
1283 async_ret
= NOT_DONE
;
1284 qemu_aio_wait_start();
1285 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1286 bdrv_rw_em_cb
, &async_ret
);
1288 qemu_aio_wait_end();
1291 while (async_ret
== NOT_DONE
) {
1294 qemu_aio_wait_end();
1298 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1299 const uint8_t *buf
, int nb_sectors
)
1302 BlockDriverAIOCB
*acb
;
1304 async_ret
= NOT_DONE
;
1305 qemu_aio_wait_start();
1306 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1307 bdrv_rw_em_cb
, &async_ret
);
1309 qemu_aio_wait_end();
1312 while (async_ret
== NOT_DONE
) {
1315 qemu_aio_wait_end();
1319 void bdrv_init(void)
1321 bdrv_register(&bdrv_raw
);
1322 bdrv_register(&bdrv_host_device
);
1324 bdrv_register(&bdrv_cow
);
1326 bdrv_register(&bdrv_qcow
);
1327 bdrv_register(&bdrv_vmdk
);
1328 bdrv_register(&bdrv_cloop
);
1329 bdrv_register(&bdrv_dmg
);
1330 bdrv_register(&bdrv_bochs
);
1331 bdrv_register(&bdrv_vpc
);
1332 bdrv_register(&bdrv_vvfat
);
1333 bdrv_register(&bdrv_qcow2
);
1334 bdrv_register(&bdrv_parallels
);
1337 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1341 BlockDriverAIOCB
*acb
;
1344 if (drv
->free_aiocb
) {
1345 acb
= drv
->free_aiocb
;
1346 drv
->free_aiocb
= acb
->next
;
1348 acb
= qemu_mallocz(drv
->aiocb_size
);
1354 acb
->opaque
= opaque
;
1358 void qemu_aio_release(void *p
)
1360 BlockDriverAIOCB
*acb
= p
;
1361 BlockDriver
*drv
= acb
->bs
->drv
;
1362 acb
->next
= drv
->free_aiocb
;
1363 drv
->free_aiocb
= acb
;
1366 /**************************************************************/
1367 /* removable device support */
1370 * Return TRUE if the media is present
1372 int bdrv_is_inserted(BlockDriverState
*bs
)
1374 BlockDriver
*drv
= bs
->drv
;
1378 if (!drv
->bdrv_is_inserted
)
1380 ret
= drv
->bdrv_is_inserted(bs
);
1385 * Return TRUE if the media changed since the last call to this
1386 * function. It is currently only used for floppy disks
1388 int bdrv_media_changed(BlockDriverState
*bs
)
1390 BlockDriver
*drv
= bs
->drv
;
1393 if (!drv
|| !drv
->bdrv_media_changed
)
1396 ret
= drv
->bdrv_media_changed(bs
);
1397 if (ret
== -ENOTSUP
)
1398 ret
= bs
->media_changed
;
1399 bs
->media_changed
= 0;
1404 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1406 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1408 BlockDriver
*drv
= bs
->drv
;
1411 if (!drv
|| !drv
->bdrv_eject
) {
1414 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1416 if (ret
== -ENOTSUP
) {
1422 int bdrv_is_locked(BlockDriverState
*bs
)
1428 * Lock or unlock the media (if it is locked, the user won't be able
1429 * to eject it manually).
1431 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1433 BlockDriver
*drv
= bs
->drv
;
1435 bs
->locked
= locked
;
1436 if (drv
&& drv
->bdrv_set_locked
) {
1437 drv
->bdrv_set_locked(bs
, locked
);
1441 /* needed for generic scsi interface */
1443 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1445 BlockDriver
*drv
= bs
->drv
;
1447 if (drv
&& drv
->bdrv_ioctl
)
1448 return drv
->bdrv_ioctl(bs
, req
, buf
);