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 static BlockDriver
*first_drv
;
61 int path_is_absolute(const char *path
)
65 /* specific case for names like: "\\.\d:" */
66 if (*path
== '/' || *path
== '\\')
69 p
= strchr(path
, ':');
75 return (*p
== '/' || *p
== '\\');
81 /* if filename is absolute, just copy it to dest. Otherwise, build a
82 path to it by considering it is relative to base_path. URL are
84 void path_combine(char *dest
, int dest_size
,
85 const char *base_path
,
93 if (path_is_absolute(filename
)) {
94 pstrcpy(dest
, dest_size
, filename
);
96 p
= strchr(base_path
, ':');
101 p1
= strrchr(base_path
, '/');
105 p2
= strrchr(base_path
, '\\');
117 if (len
> dest_size
- 1)
119 memcpy(dest
, base_path
, len
);
121 pstrcat(dest
, dest_size
, filename
);
126 static void bdrv_register(BlockDriver
*bdrv
)
128 if (!bdrv
->bdrv_aio_read
) {
129 /* add AIO emulation layer */
130 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
131 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
132 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
133 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
134 } else if (!bdrv
->bdrv_read
&& !bdrv
->bdrv_pread
) {
135 /* add synchronous IO emulation layer */
136 bdrv
->bdrv_read
= bdrv_read_em
;
137 bdrv
->bdrv_write
= bdrv_write_em
;
139 bdrv
->next
= first_drv
;
143 /* create a new block device (by default it is empty) */
144 BlockDriverState
*bdrv_new(const char *device_name
)
146 BlockDriverState
**pbs
, *bs
;
148 bs
= qemu_mallocz(sizeof(BlockDriverState
));
151 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
152 if (device_name
[0] != '\0') {
153 /* insert at the end */
162 BlockDriver
*bdrv_find_format(const char *format_name
)
165 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
166 if (!strcmp(drv1
->format_name
, format_name
))
172 int bdrv_create(BlockDriver
*drv
,
173 const char *filename
, int64_t size_in_sectors
,
174 const char *backing_file
, int flags
)
176 if (!drv
->bdrv_create
)
178 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
182 void get_tmp_filename(char *filename
, int size
)
184 char temp_dir
[MAX_PATH
];
186 GetTempPath(MAX_PATH
, temp_dir
);
187 GetTempFileName(temp_dir
, "qem", 0, filename
);
190 void get_tmp_filename(char *filename
, int size
)
194 /* XXX: race condition possible */
195 tmpdir
= getenv("TMPDIR");
198 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
199 fd
= mkstemp(filename
);
205 static int is_windows_drive_prefix(const char *filename
)
207 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
208 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
212 static int is_windows_drive(const char *filename
)
214 if (is_windows_drive_prefix(filename
) &&
217 if (strstart(filename
, "\\\\.\\", NULL
) ||
218 strstart(filename
, "//./", NULL
))
224 static BlockDriver
*find_protocol(const char *filename
)
232 if (is_windows_drive(filename
) ||
233 is_windows_drive_prefix(filename
))
236 p
= strchr(filename
, ':');
240 if (len
> sizeof(protocol
) - 1)
241 len
= sizeof(protocol
) - 1;
242 memcpy(protocol
, filename
, len
);
243 protocol
[len
] = '\0';
244 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
245 if (drv1
->protocol_name
&&
246 !strcmp(drv1
->protocol_name
, protocol
))
252 /* XXX: force raw format if block or character device ? It would
253 simplify the BSD case */
254 static BlockDriver
*find_image_format(const char *filename
)
256 int ret
, score
, score_max
;
257 BlockDriver
*drv1
, *drv
;
259 BlockDriverState
*bs
;
261 /* detect host devices. By convention, /dev/cdrom[N] is always
262 recognized as a host CDROM */
263 if (strstart(filename
, "/dev/cdrom", NULL
))
264 return &bdrv_host_device
;
266 if (is_windows_drive(filename
))
267 return &bdrv_host_device
;
271 if (stat(filename
, &st
) >= 0 &&
272 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
273 return &bdrv_host_device
;
278 drv
= find_protocol(filename
);
279 /* no need to test disk image formats for vvfat */
280 if (drv
== &bdrv_vvfat
)
283 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
286 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
293 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
294 if (drv1
->bdrv_probe
) {
295 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
296 if (score
> score_max
) {
305 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
307 BlockDriverState
*bs
;
313 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
322 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
324 return bdrv_open2(bs
, filename
, flags
, NULL
);
327 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
331 char tmp_filename
[PATH_MAX
];
332 char backing_filename
[PATH_MAX
];
335 bs
->is_temporary
= 0;
338 if (flags
& BDRV_O_SNAPSHOT
) {
339 BlockDriverState
*bs1
;
342 /* if snapshot, we create a temporary backing file and open it
343 instead of opening 'filename' directly */
345 /* if there is a backing file, use it */
350 if (bdrv_open(bs1
, filename
, 0) < 0) {
354 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
357 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
358 realpath(filename
, backing_filename
);
359 if (bdrv_create(&bdrv_qcow2
, tmp_filename
,
360 total_size
, backing_filename
, 0) < 0) {
363 filename
= tmp_filename
;
364 bs
->is_temporary
= 1;
367 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
368 if (flags
& BDRV_O_FILE
) {
369 drv
= find_protocol(filename
);
374 drv
= find_image_format(filename
);
380 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
381 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
383 /* Note: for compatibility, we open disk image files as RDWR, and
384 RDONLY as fallback */
385 if (!(flags
& BDRV_O_FILE
))
386 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_DIRECT
);
388 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
389 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
390 if (ret
== -EACCES
&& !(flags
& BDRV_O_FILE
)) {
391 ret
= drv
->bdrv_open(bs
, filename
, BDRV_O_RDONLY
);
395 qemu_free(bs
->opaque
);
400 if (drv
->bdrv_getlength
) {
401 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
404 if (bs
->is_temporary
) {
408 if (bs
->backing_file
[0] != '\0') {
409 /* if there is a backing file, use it */
410 bs
->backing_hd
= bdrv_new("");
411 if (!bs
->backing_hd
) {
416 path_combine(backing_filename
, sizeof(backing_filename
),
417 filename
, bs
->backing_file
);
418 if (bdrv_open(bs
->backing_hd
, backing_filename
, 0) < 0)
422 /* call the change callback */
423 bs
->media_changed
= 1;
425 bs
->change_cb(bs
->change_opaque
);
430 void bdrv_close(BlockDriverState
*bs
)
434 bdrv_delete(bs
->backing_hd
);
435 bs
->drv
->bdrv_close(bs
);
436 qemu_free(bs
->opaque
);
438 if (bs
->is_temporary
) {
439 unlink(bs
->filename
);
445 /* call the change callback */
446 bs
->media_changed
= 1;
448 bs
->change_cb(bs
->change_opaque
);
452 void bdrv_delete(BlockDriverState
*bs
)
454 BlockDriverState
**pbs
;
457 while (*pbs
!= bs
&& *pbs
!= NULL
)
466 /* commit COW file into the raw image */
467 int bdrv_commit(BlockDriverState
*bs
)
469 BlockDriver
*drv
= bs
->drv
;
470 int64_t i
, total_sectors
;
472 unsigned char sector
[512];
481 if (!bs
->backing_hd
) {
485 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
486 for (i
= 0; i
< total_sectors
;) {
487 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
488 for(j
= 0; j
< n
; j
++) {
489 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
493 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
503 if (drv
->bdrv_make_empty
)
504 return drv
->bdrv_make_empty(bs
);
509 /* return < 0 if error. See bdrv_write() for the return codes */
510 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
511 uint8_t *buf
, int nb_sectors
)
513 BlockDriver
*drv
= bs
->drv
;
518 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
519 memcpy(buf
, bs
->boot_sector_data
, 512);
526 if (drv
->bdrv_pread
) {
528 len
= nb_sectors
* 512;
529 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
535 bs
->rd_bytes
+= (unsigned) len
;
540 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
544 /* Return < 0 if error. Important errors are:
545 -EIO generic I/O error (may happen for all errors)
546 -ENOMEDIUM No media inserted.
547 -EINVAL Invalid sector number or nb_sectors
548 -EACCES Trying to write a read-only device
550 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
551 const uint8_t *buf
, int nb_sectors
)
553 BlockDriver
*drv
= bs
->drv
;
558 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
559 memcpy(bs
->boot_sector_data
, buf
, 512);
561 if (drv
->bdrv_pwrite
) {
563 len
= nb_sectors
* 512;
564 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
);
570 bs
->wr_bytes
+= (unsigned) len
;
575 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
579 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
580 uint8_t *buf
, int count1
)
582 uint8_t tmp_buf
[SECTOR_SIZE
];
583 int len
, nb_sectors
, count
;
587 /* first read to align to sector start */
588 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
591 sector_num
= offset
>> SECTOR_BITS
;
593 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
595 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
603 /* read the sectors "in place" */
604 nb_sectors
= count
>> SECTOR_BITS
;
605 if (nb_sectors
> 0) {
606 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
608 sector_num
+= nb_sectors
;
609 len
= nb_sectors
<< SECTOR_BITS
;
614 /* add data from the last sector */
616 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
618 memcpy(buf
, tmp_buf
, count
);
623 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
624 const uint8_t *buf
, int count1
)
626 uint8_t tmp_buf
[SECTOR_SIZE
];
627 int len
, nb_sectors
, count
;
631 /* first write to align to sector start */
632 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
635 sector_num
= offset
>> SECTOR_BITS
;
637 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
639 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
640 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
649 /* write the sectors "in place" */
650 nb_sectors
= count
>> SECTOR_BITS
;
651 if (nb_sectors
> 0) {
652 if (bdrv_write(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(tmp_buf
, buf
, count
);
665 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
672 * Read with byte offsets (needed only for file protocols)
674 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
675 void *buf1
, int count1
)
677 BlockDriver
*drv
= bs
->drv
;
681 if (!drv
->bdrv_pread
)
682 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
683 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
687 * Write with byte offsets (needed only for file protocols)
689 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
690 const void *buf1
, int count1
)
692 BlockDriver
*drv
= bs
->drv
;
696 if (!drv
->bdrv_pwrite
)
697 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
698 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
702 * Truncate file to 'offset' bytes (needed only for file protocols)
704 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
706 BlockDriver
*drv
= bs
->drv
;
709 if (!drv
->bdrv_truncate
)
711 return drv
->bdrv_truncate(bs
, offset
);
715 * Length of a file in bytes. Return < 0 if error or unknown.
717 int64_t bdrv_getlength(BlockDriverState
*bs
)
719 BlockDriver
*drv
= bs
->drv
;
722 if (!drv
->bdrv_getlength
) {
724 return bs
->total_sectors
* SECTOR_SIZE
;
726 return drv
->bdrv_getlength(bs
);
729 /* return 0 as number of sectors if no device present or error */
730 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
733 length
= bdrv_getlength(bs
);
737 length
= length
>> SECTOR_BITS
;
738 *nb_sectors_ptr
= length
;
741 /* force a given boot sector. */
742 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
)
744 bs
->boot_sector_enabled
= 1;
747 memcpy(bs
->boot_sector_data
, data
, size
);
748 memset(bs
->boot_sector_data
+ size
, 0, 512 - size
);
751 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
752 int cyls
, int heads
, int secs
)
759 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
762 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
763 type
== BDRV_TYPE_FLOPPY
));
766 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
768 bs
->translation
= translation
;
771 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
772 int *pcyls
, int *pheads
, int *psecs
)
779 int bdrv_get_type_hint(BlockDriverState
*bs
)
784 int bdrv_get_translation_hint(BlockDriverState
*bs
)
786 return bs
->translation
;
789 int bdrv_is_removable(BlockDriverState
*bs
)
791 return bs
->removable
;
794 int bdrv_is_read_only(BlockDriverState
*bs
)
796 return bs
->read_only
;
799 int bdrv_is_sg(BlockDriverState
*bs
)
804 /* XXX: no longer used */
805 void bdrv_set_change_cb(BlockDriverState
*bs
,
806 void (*change_cb
)(void *opaque
), void *opaque
)
808 bs
->change_cb
= change_cb
;
809 bs
->change_opaque
= opaque
;
812 int bdrv_is_encrypted(BlockDriverState
*bs
)
814 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
816 return bs
->encrypted
;
819 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
822 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
823 ret
= bdrv_set_key(bs
->backing_hd
, key
);
829 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
831 return bs
->drv
->bdrv_set_key(bs
, key
);
834 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
839 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
843 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
848 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
849 it(opaque
, drv
->format_name
);
853 BlockDriverState
*bdrv_find(const char *name
)
855 BlockDriverState
*bs
;
857 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
858 if (!strcmp(name
, bs
->device_name
))
864 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
866 BlockDriverState
*bs
;
868 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
869 it(opaque
, bs
->device_name
);
873 const char *bdrv_get_device_name(BlockDriverState
*bs
)
875 return bs
->device_name
;
878 void bdrv_flush(BlockDriverState
*bs
)
880 if (bs
->drv
->bdrv_flush
)
881 bs
->drv
->bdrv_flush(bs
);
883 bdrv_flush(bs
->backing_hd
);
887 * Returns true iff the specified sector is present in the disk image. Drivers
888 * not implementing the functionality are assumed to not support backing files,
889 * hence all their sectors are reported as allocated.
891 * 'pnum' is set to the number of sectors (including and immediately following
892 * the specified sector) that are known to be in the same
893 * allocated/unallocated state.
895 * 'nb_sectors' is the max value 'pnum' should be set to.
897 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
901 if (!bs
->drv
->bdrv_is_allocated
) {
902 if (sector_num
>= bs
->total_sectors
) {
906 n
= bs
->total_sectors
- sector_num
;
907 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
910 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
916 BlockDriverState
*bs
;
918 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
919 term_printf("%s:", bs
->device_name
);
920 term_printf(" type=");
925 case BDRV_TYPE_CDROM
:
926 term_printf("cdrom");
928 case BDRV_TYPE_FLOPPY
:
929 term_printf("floppy");
932 term_printf(" removable=%d", bs
->removable
);
934 term_printf(" locked=%d", bs
->locked
);
937 term_printf(" file=");
938 term_print_filename(bs
->filename
);
939 if (bs
->backing_file
[0] != '\0') {
940 term_printf(" backing_file=");
941 term_print_filename(bs
->backing_file
);
943 term_printf(" ro=%d", bs
->read_only
);
944 term_printf(" drv=%s", bs
->drv
->format_name
);
946 term_printf(" encrypted");
948 term_printf(" [not inserted]");
954 /* The "info blockstats" command. */
955 void bdrv_info_stats (void)
957 BlockDriverState
*bs
;
959 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
963 " rd_operations=%" PRIu64
964 " wr_operations=%" PRIu64
967 bs
->rd_bytes
, bs
->wr_bytes
,
968 bs
->rd_ops
, bs
->wr_ops
);
973 void bdrv_get_backing_filename(BlockDriverState
*bs
,
974 char *filename
, int filename_size
)
976 if (!bs
->backing_hd
) {
977 pstrcpy(filename
, filename_size
, "");
979 pstrcpy(filename
, filename_size
, bs
->backing_file
);
983 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
984 const uint8_t *buf
, int nb_sectors
)
986 BlockDriver
*drv
= bs
->drv
;
989 if (!drv
->bdrv_write_compressed
)
991 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
994 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
996 BlockDriver
*drv
= bs
->drv
;
999 if (!drv
->bdrv_get_info
)
1001 memset(bdi
, 0, sizeof(*bdi
));
1002 return drv
->bdrv_get_info(bs
, bdi
);
1005 /**************************************************************/
1006 /* handling of snapshots */
1008 int bdrv_snapshot_create(BlockDriverState
*bs
,
1009 QEMUSnapshotInfo
*sn_info
)
1011 BlockDriver
*drv
= bs
->drv
;
1014 if (!drv
->bdrv_snapshot_create
)
1016 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1019 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1020 const char *snapshot_id
)
1022 BlockDriver
*drv
= bs
->drv
;
1025 if (!drv
->bdrv_snapshot_goto
)
1027 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1030 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1032 BlockDriver
*drv
= bs
->drv
;
1035 if (!drv
->bdrv_snapshot_delete
)
1037 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1040 int bdrv_snapshot_list(BlockDriverState
*bs
,
1041 QEMUSnapshotInfo
**psn_info
)
1043 BlockDriver
*drv
= bs
->drv
;
1046 if (!drv
->bdrv_snapshot_list
)
1048 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1051 #define NB_SUFFIXES 4
1053 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1055 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1060 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1063 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1064 if (size
< (10 * base
)) {
1065 snprintf(buf
, buf_size
, "%0.1f%c",
1066 (double)size
/ base
,
1069 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1070 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1071 ((size
+ (base
>> 1)) / base
),
1081 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1083 char buf1
[128], date_buf
[128], clock_buf
[128];
1093 snprintf(buf
, buf_size
,
1094 "%-10s%-20s%7s%20s%15s",
1095 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1099 ptm
= localtime(&ti
);
1100 strftime(date_buf
, sizeof(date_buf
),
1101 "%Y-%m-%d %H:%M:%S", ptm
);
1103 localtime_r(&ti
, &tm
);
1104 strftime(date_buf
, sizeof(date_buf
),
1105 "%Y-%m-%d %H:%M:%S", &tm
);
1107 secs
= sn
->vm_clock_nsec
/ 1000000000;
1108 snprintf(clock_buf
, sizeof(clock_buf
),
1109 "%02d:%02d:%02d.%03d",
1111 (int)((secs
/ 60) % 60),
1113 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1114 snprintf(buf
, buf_size
,
1115 "%-10s%-20s%7s%20s%15s",
1116 sn
->id_str
, sn
->name
,
1117 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1125 /**************************************************************/
1128 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1129 uint8_t *buf
, int nb_sectors
,
1130 BlockDriverCompletionFunc
*cb
, void *opaque
)
1132 BlockDriver
*drv
= bs
->drv
;
1133 BlockDriverAIOCB
*ret
;
1138 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1139 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1140 memcpy(buf
, bs
->boot_sector_data
, 512);
1146 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1149 /* Update stats even though technically transfer has not happened. */
1150 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1157 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1158 const uint8_t *buf
, int nb_sectors
,
1159 BlockDriverCompletionFunc
*cb
, void *opaque
)
1161 BlockDriver
*drv
= bs
->drv
;
1162 BlockDriverAIOCB
*ret
;
1168 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1169 memcpy(bs
->boot_sector_data
, buf
, 512);
1172 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1175 /* Update stats even though technically transfer has not happened. */
1176 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1183 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1185 BlockDriver
*drv
= acb
->bs
->drv
;
1187 drv
->bdrv_aio_cancel(acb
);
1191 /**************************************************************/
1192 /* async block device emulation */
1195 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1196 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1197 BlockDriverCompletionFunc
*cb
, void *opaque
)
1200 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1205 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1206 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1207 BlockDriverCompletionFunc
*cb
, void *opaque
)
1210 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1215 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
)
1219 static void bdrv_aio_bh_cb(void *opaque
)
1221 BlockDriverAIOCBSync
*acb
= opaque
;
1222 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1223 qemu_aio_release(acb
);
1226 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1227 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1228 BlockDriverCompletionFunc
*cb
, void *opaque
)
1230 BlockDriverAIOCBSync
*acb
;
1233 acb
= qemu_aio_get(bs
, cb
, opaque
);
1235 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1236 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1238 qemu_bh_schedule(acb
->bh
);
1239 return &acb
->common
;
1242 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1243 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1244 BlockDriverCompletionFunc
*cb
, void *opaque
)
1246 BlockDriverAIOCBSync
*acb
;
1249 acb
= qemu_aio_get(bs
, cb
, opaque
);
1251 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1252 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1254 qemu_bh_schedule(acb
->bh
);
1255 return &acb
->common
;
1258 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1260 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1261 qemu_bh_cancel(acb
->bh
);
1262 qemu_aio_release(acb
);
1264 #endif /* !QEMU_IMG */
1266 /**************************************************************/
1267 /* sync block device emulation */
1269 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1271 *(int *)opaque
= ret
;
1274 #define NOT_DONE 0x7fffffff
1276 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1277 uint8_t *buf
, int nb_sectors
)
1280 BlockDriverAIOCB
*acb
;
1282 async_ret
= NOT_DONE
;
1283 qemu_aio_wait_start();
1284 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1285 bdrv_rw_em_cb
, &async_ret
);
1287 qemu_aio_wait_end();
1290 while (async_ret
== NOT_DONE
) {
1293 qemu_aio_wait_end();
1297 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1298 const uint8_t *buf
, int nb_sectors
)
1301 BlockDriverAIOCB
*acb
;
1303 async_ret
= NOT_DONE
;
1304 qemu_aio_wait_start();
1305 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1306 bdrv_rw_em_cb
, &async_ret
);
1308 qemu_aio_wait_end();
1311 while (async_ret
== NOT_DONE
) {
1314 qemu_aio_wait_end();
1318 void bdrv_init(void)
1320 bdrv_register(&bdrv_raw
);
1321 bdrv_register(&bdrv_host_device
);
1323 bdrv_register(&bdrv_cow
);
1325 bdrv_register(&bdrv_qcow
);
1326 bdrv_register(&bdrv_vmdk
);
1327 bdrv_register(&bdrv_cloop
);
1328 bdrv_register(&bdrv_dmg
);
1329 bdrv_register(&bdrv_bochs
);
1330 bdrv_register(&bdrv_vpc
);
1331 bdrv_register(&bdrv_vvfat
);
1332 bdrv_register(&bdrv_qcow2
);
1333 bdrv_register(&bdrv_parallels
);
1335 bdrv_register(&bdrv_nbd
);
1339 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1343 BlockDriverAIOCB
*acb
;
1346 if (drv
->free_aiocb
) {
1347 acb
= drv
->free_aiocb
;
1348 drv
->free_aiocb
= acb
->next
;
1350 acb
= qemu_mallocz(drv
->aiocb_size
);
1356 acb
->opaque
= opaque
;
1360 void qemu_aio_release(void *p
)
1362 BlockDriverAIOCB
*acb
= p
;
1363 BlockDriver
*drv
= acb
->bs
->drv
;
1364 acb
->next
= drv
->free_aiocb
;
1365 drv
->free_aiocb
= acb
;
1368 /**************************************************************/
1369 /* removable device support */
1372 * Return TRUE if the media is present
1374 int bdrv_is_inserted(BlockDriverState
*bs
)
1376 BlockDriver
*drv
= bs
->drv
;
1380 if (!drv
->bdrv_is_inserted
)
1382 ret
= drv
->bdrv_is_inserted(bs
);
1387 * Return TRUE if the media changed since the last call to this
1388 * function. It is currently only used for floppy disks
1390 int bdrv_media_changed(BlockDriverState
*bs
)
1392 BlockDriver
*drv
= bs
->drv
;
1395 if (!drv
|| !drv
->bdrv_media_changed
)
1398 ret
= drv
->bdrv_media_changed(bs
);
1399 if (ret
== -ENOTSUP
)
1400 ret
= bs
->media_changed
;
1401 bs
->media_changed
= 0;
1406 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1408 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1410 BlockDriver
*drv
= bs
->drv
;
1413 if (!drv
|| !drv
->bdrv_eject
) {
1416 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1418 if (ret
== -ENOTSUP
) {
1424 int bdrv_is_locked(BlockDriverState
*bs
)
1430 * Lock or unlock the media (if it is locked, the user won't be able
1431 * to eject it manually).
1433 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1435 BlockDriver
*drv
= bs
->drv
;
1437 bs
->locked
= locked
;
1438 if (drv
&& drv
->bdrv_set_locked
) {
1439 drv
->bdrv_set_locked(bs
, locked
);
1443 /* needed for generic scsi interface */
1445 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1447 BlockDriver
*drv
= bs
->drv
;
1449 if (drv
&& drv
->bdrv_ioctl
)
1450 return drv
->bdrv_ioctl(bs
, req
, buf
);