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"
26 #include "block_int.h"
29 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <sys/queue.h>
37 #define SECTOR_SIZE (1 << SECTOR_BITS)
39 typedef struct BlockDriverAIOCBSync
{
40 BlockDriverAIOCB common
;
43 } BlockDriverAIOCBSync
;
45 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
46 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
47 BlockDriverCompletionFunc
*cb
, void *opaque
);
48 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
49 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
50 BlockDriverCompletionFunc
*cb
, void *opaque
);
51 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
52 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
53 uint8_t *buf
, int nb_sectors
);
54 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
55 const uint8_t *buf
, int nb_sectors
);
57 static BlockDriver
*first_drv
;
59 int path_is_absolute(const char *path
)
63 /* specific case for names like: "\\.\d:" */
64 if (*path
== '/' || *path
== '\\')
67 p
= strchr(path
, ':');
73 return (*p
== '/' || *p
== '\\');
79 /* if filename is absolute, just copy it to dest. Otherwise, build a
80 path to it by considering it is relative to base_path. URL are
82 void path_combine(char *dest
, int dest_size
,
83 const char *base_path
,
91 if (path_is_absolute(filename
)) {
92 pstrcpy(dest
, dest_size
, filename
);
94 p
= strchr(base_path
, ':');
99 p1
= strrchr(base_path
, '/');
103 p2
= strrchr(base_path
, '\\');
115 if (len
> dest_size
- 1)
117 memcpy(dest
, base_path
, len
);
119 pstrcat(dest
, dest_size
, filename
);
124 static void bdrv_register(BlockDriver
*bdrv
)
126 if (!bdrv
->bdrv_aio_read
) {
127 /* add AIO emulation layer */
128 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
129 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
130 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
131 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
132 } else if (!bdrv
->bdrv_read
&& !bdrv
->bdrv_pread
) {
133 /* add synchronous IO emulation layer */
134 bdrv
->bdrv_read
= bdrv_read_em
;
135 bdrv
->bdrv_write
= bdrv_write_em
;
137 bdrv
->next
= first_drv
;
141 /* create a new block device (by default it is empty) */
142 BlockDriverState
*bdrv_new(const char *device_name
)
144 BlockDriverState
**pbs
, *bs
;
146 bs
= qemu_mallocz(sizeof(BlockDriverState
));
149 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
150 if (device_name
[0] != '\0') {
151 /* insert at the end */
160 BlockDriver
*bdrv_find_format(const char *format_name
)
163 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
164 if (!strcmp(drv1
->format_name
, format_name
))
170 int bdrv_create(BlockDriver
*drv
,
171 const char *filename
, int64_t size_in_sectors
,
172 const char *backing_file
, int flags
)
174 if (!drv
->bdrv_create
)
176 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
180 void get_tmp_filename(char *filename
, int size
)
182 char temp_dir
[MAX_PATH
];
184 GetTempPath(MAX_PATH
, temp_dir
);
185 GetTempFileName(temp_dir
, "qem", 0, filename
);
188 void get_tmp_filename(char *filename
, int size
)
192 /* XXX: race condition possible */
193 tmpdir
= getenv("TMPDIR");
196 snprintf(filename
, size
, "%s/vl.XXXXXX", tmpdir
);
197 fd
= mkstemp(filename
);
203 static int is_windows_drive_prefix(const char *filename
)
205 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
206 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
210 static int is_windows_drive(const char *filename
)
212 if (is_windows_drive_prefix(filename
) &&
215 if (strstart(filename
, "\\\\.\\", NULL
) ||
216 strstart(filename
, "//./", NULL
))
222 static BlockDriver
*find_protocol(const char *filename
)
230 if (is_windows_drive(filename
) ||
231 is_windows_drive_prefix(filename
))
234 p
= strchr(filename
, ':');
238 if (len
> sizeof(protocol
) - 1)
239 len
= sizeof(protocol
) - 1;
240 memcpy(protocol
, filename
, len
);
241 protocol
[len
] = '\0';
242 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
243 if (drv1
->protocol_name
&&
244 !strcmp(drv1
->protocol_name
, protocol
))
250 /* XXX: force raw format if block or character device ? It would
251 simplify the BSD case */
252 static BlockDriver
*find_image_format(const char *filename
)
254 int ret
, score
, score_max
;
255 BlockDriver
*drv1
, *drv
;
257 BlockDriverState
*bs
;
259 /* detect host devices. By convention, /dev/cdrom[N] is always
260 recognized as a host CDROM */
261 if (strstart(filename
, "/dev/cdrom", NULL
))
262 return &bdrv_host_device
;
264 if (is_windows_drive(filename
))
265 return &bdrv_host_device
;
269 if (stat(filename
, &st
) >= 0 &&
270 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
271 return &bdrv_host_device
;
276 drv
= find_protocol(filename
);
277 /* no need to test disk image formats for vvfat */
278 if (drv
== &bdrv_vvfat
)
281 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
284 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
291 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
292 if (drv1
->bdrv_probe
) {
293 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
294 if (score
> score_max
) {
303 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
305 BlockDriverState
*bs
;
311 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
320 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
322 return bdrv_open2(bs
, filename
, flags
, NULL
);
325 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
329 char tmp_filename
[PATH_MAX
];
330 char backing_filename
[PATH_MAX
];
333 bs
->is_temporary
= 0;
336 if (flags
& BDRV_O_SNAPSHOT
) {
337 BlockDriverState
*bs1
;
341 /* if snapshot, we create a temporary backing file and open it
342 instead of opening 'filename' directly */
344 /* if there is a backing file, use it */
349 if (bdrv_open(bs1
, filename
, 0) < 0) {
353 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
355 if (bs1
->drv
&& bs1
->drv
->protocol_name
)
360 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
362 /* Real path is meaningless for protocols */
364 snprintf(backing_filename
, sizeof(backing_filename
),
367 realpath(filename
, backing_filename
);
369 if (bdrv_create(&bdrv_qcow2
, tmp_filename
,
370 total_size
, backing_filename
, 0) < 0) {
373 filename
= tmp_filename
;
374 bs
->is_temporary
= 1;
377 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
378 if (flags
& BDRV_O_FILE
) {
379 drv
= find_protocol(filename
);
384 drv
= find_image_format(filename
);
390 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
391 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
393 /* Note: for compatibility, we open disk image files as RDWR, and
394 RDONLY as fallback */
395 if (!(flags
& BDRV_O_FILE
))
396 open_flags
= BDRV_O_RDWR
| (flags
& BDRV_O_DIRECT
);
398 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
399 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
400 if (ret
== -EACCES
&& !(flags
& BDRV_O_FILE
)) {
401 ret
= drv
->bdrv_open(bs
, filename
, BDRV_O_RDONLY
);
405 qemu_free(bs
->opaque
);
410 if (drv
->bdrv_getlength
) {
411 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
414 if (bs
->is_temporary
) {
418 if (bs
->backing_file
[0] != '\0') {
419 /* if there is a backing file, use it */
420 bs
->backing_hd
= bdrv_new("");
421 if (!bs
->backing_hd
) {
426 path_combine(backing_filename
, sizeof(backing_filename
),
427 filename
, bs
->backing_file
);
428 if (bdrv_open(bs
->backing_hd
, backing_filename
, 0) < 0)
432 /* call the change callback */
433 bs
->media_changed
= 1;
435 bs
->change_cb(bs
->change_opaque
);
440 void bdrv_close(BlockDriverState
*bs
)
444 bdrv_delete(bs
->backing_hd
);
445 bs
->drv
->bdrv_close(bs
);
446 qemu_free(bs
->opaque
);
448 if (bs
->is_temporary
) {
449 unlink(bs
->filename
);
455 /* call the change callback */
456 bs
->media_changed
= 1;
458 bs
->change_cb(bs
->change_opaque
);
462 void bdrv_delete(BlockDriverState
*bs
)
464 BlockDriverState
**pbs
;
467 while (*pbs
!= bs
&& *pbs
!= NULL
)
476 /* commit COW file into the raw image */
477 int bdrv_commit(BlockDriverState
*bs
)
479 BlockDriver
*drv
= bs
->drv
;
480 int64_t i
, total_sectors
;
482 unsigned char sector
[512];
491 if (!bs
->backing_hd
) {
495 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
496 for (i
= 0; i
< total_sectors
;) {
497 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
498 for(j
= 0; j
< n
; j
++) {
499 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
503 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
513 if (drv
->bdrv_make_empty
)
514 return drv
->bdrv_make_empty(bs
);
519 /* return < 0 if error. See bdrv_write() for the return codes */
520 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
521 uint8_t *buf
, int nb_sectors
)
523 BlockDriver
*drv
= bs
->drv
;
528 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
529 memcpy(buf
, bs
->boot_sector_data
, 512);
536 if (drv
->bdrv_pread
) {
538 len
= nb_sectors
* 512;
539 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
545 bs
->rd_bytes
+= (unsigned) len
;
550 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
554 /* Return < 0 if error. Important errors are:
555 -EIO generic I/O error (may happen for all errors)
556 -ENOMEDIUM No media inserted.
557 -EINVAL Invalid sector number or nb_sectors
558 -EACCES Trying to write a read-only device
560 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
561 const uint8_t *buf
, int nb_sectors
)
563 BlockDriver
*drv
= bs
->drv
;
568 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
569 memcpy(bs
->boot_sector_data
, buf
, 512);
571 if (drv
->bdrv_pwrite
) {
573 len
= nb_sectors
* 512;
574 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
);
580 bs
->wr_bytes
+= (unsigned) len
;
585 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
589 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
590 uint8_t *buf
, int count1
)
592 uint8_t tmp_buf
[SECTOR_SIZE
];
593 int len
, nb_sectors
, count
;
597 /* first read to align to sector start */
598 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
601 sector_num
= offset
>> SECTOR_BITS
;
603 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
605 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
613 /* read the sectors "in place" */
614 nb_sectors
= count
>> SECTOR_BITS
;
615 if (nb_sectors
> 0) {
616 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
618 sector_num
+= nb_sectors
;
619 len
= nb_sectors
<< SECTOR_BITS
;
624 /* add data from the last sector */
626 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
628 memcpy(buf
, tmp_buf
, count
);
633 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
634 const uint8_t *buf
, int count1
)
636 uint8_t tmp_buf
[SECTOR_SIZE
];
637 int len
, nb_sectors
, count
;
641 /* first write to align to sector start */
642 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
645 sector_num
= offset
>> SECTOR_BITS
;
647 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
649 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
650 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
659 /* write the sectors "in place" */
660 nb_sectors
= count
>> SECTOR_BITS
;
661 if (nb_sectors
> 0) {
662 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
664 sector_num
+= nb_sectors
;
665 len
= nb_sectors
<< SECTOR_BITS
;
670 /* add data from the last sector */
672 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
674 memcpy(tmp_buf
, buf
, count
);
675 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
682 * Read with byte offsets (needed only for file protocols)
684 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
685 void *buf1
, int count1
)
687 BlockDriver
*drv
= bs
->drv
;
691 if (!drv
->bdrv_pread
)
692 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
693 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
697 * Write with byte offsets (needed only for file protocols)
699 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
700 const void *buf1
, int count1
)
702 BlockDriver
*drv
= bs
->drv
;
706 if (!drv
->bdrv_pwrite
)
707 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
708 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
712 * Truncate file to 'offset' bytes (needed only for file protocols)
714 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
716 BlockDriver
*drv
= bs
->drv
;
719 if (!drv
->bdrv_truncate
)
721 return drv
->bdrv_truncate(bs
, offset
);
725 * Length of a file in bytes. Return < 0 if error or unknown.
727 int64_t bdrv_getlength(BlockDriverState
*bs
)
729 BlockDriver
*drv
= bs
->drv
;
732 if (!drv
->bdrv_getlength
) {
734 return bs
->total_sectors
* SECTOR_SIZE
;
736 return drv
->bdrv_getlength(bs
);
739 /* return 0 as number of sectors if no device present or error */
740 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
743 length
= bdrv_getlength(bs
);
747 length
= length
>> SECTOR_BITS
;
748 *nb_sectors_ptr
= length
;
751 /* force a given boot sector. */
752 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
)
754 bs
->boot_sector_enabled
= 1;
757 memcpy(bs
->boot_sector_data
, data
, size
);
758 memset(bs
->boot_sector_data
+ size
, 0, 512 - size
);
761 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
762 int cyls
, int heads
, int secs
)
769 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
772 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
773 type
== BDRV_TYPE_FLOPPY
));
776 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
778 bs
->translation
= translation
;
781 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
782 int *pcyls
, int *pheads
, int *psecs
)
789 int bdrv_get_type_hint(BlockDriverState
*bs
)
794 int bdrv_get_translation_hint(BlockDriverState
*bs
)
796 return bs
->translation
;
799 int bdrv_is_removable(BlockDriverState
*bs
)
801 return bs
->removable
;
804 int bdrv_is_read_only(BlockDriverState
*bs
)
806 return bs
->read_only
;
809 int bdrv_is_sg(BlockDriverState
*bs
)
814 /* XXX: no longer used */
815 void bdrv_set_change_cb(BlockDriverState
*bs
,
816 void (*change_cb
)(void *opaque
), void *opaque
)
818 bs
->change_cb
= change_cb
;
819 bs
->change_opaque
= opaque
;
822 int bdrv_is_encrypted(BlockDriverState
*bs
)
824 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
826 return bs
->encrypted
;
829 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
832 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
833 ret
= bdrv_set_key(bs
->backing_hd
, key
);
839 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
841 return bs
->drv
->bdrv_set_key(bs
, key
);
844 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
849 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
853 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
858 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
859 it(opaque
, drv
->format_name
);
863 BlockDriverState
*bdrv_find(const char *name
)
865 BlockDriverState
*bs
;
867 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
868 if (!strcmp(name
, bs
->device_name
))
874 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
876 BlockDriverState
*bs
;
878 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
879 it(opaque
, bs
->device_name
);
883 const char *bdrv_get_device_name(BlockDriverState
*bs
)
885 return bs
->device_name
;
888 void bdrv_flush(BlockDriverState
*bs
)
890 if (bs
->drv
->bdrv_flush
)
891 bs
->drv
->bdrv_flush(bs
);
893 bdrv_flush(bs
->backing_hd
);
897 * Returns true iff the specified sector is present in the disk image. Drivers
898 * not implementing the functionality are assumed to not support backing files,
899 * hence all their sectors are reported as allocated.
901 * 'pnum' is set to the number of sectors (including and immediately following
902 * the specified sector) that are known to be in the same
903 * allocated/unallocated state.
905 * 'nb_sectors' is the max value 'pnum' should be set to.
907 int bdrv_is_allocated(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
911 if (!bs
->drv
->bdrv_is_allocated
) {
912 if (sector_num
>= bs
->total_sectors
) {
916 n
= bs
->total_sectors
- sector_num
;
917 *pnum
= (n
< nb_sectors
) ? (n
) : (nb_sectors
);
920 return bs
->drv
->bdrv_is_allocated(bs
, sector_num
, nb_sectors
, pnum
);
925 BlockDriverState
*bs
;
927 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
928 term_printf("%s:", bs
->device_name
);
929 term_printf(" type=");
934 case BDRV_TYPE_CDROM
:
935 term_printf("cdrom");
937 case BDRV_TYPE_FLOPPY
:
938 term_printf("floppy");
941 term_printf(" removable=%d", bs
->removable
);
943 term_printf(" locked=%d", bs
->locked
);
946 term_printf(" file=");
947 term_print_filename(bs
->filename
);
948 if (bs
->backing_file
[0] != '\0') {
949 term_printf(" backing_file=");
950 term_print_filename(bs
->backing_file
);
952 term_printf(" ro=%d", bs
->read_only
);
953 term_printf(" drv=%s", bs
->drv
->format_name
);
955 term_printf(" encrypted");
957 term_printf(" [not inserted]");
963 /* The "info blockstats" command. */
964 void bdrv_info_stats (void)
966 BlockDriverState
*bs
;
968 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
972 " rd_operations=%" PRIu64
973 " wr_operations=%" PRIu64
976 bs
->rd_bytes
, bs
->wr_bytes
,
977 bs
->rd_ops
, bs
->wr_ops
);
981 void bdrv_get_backing_filename(BlockDriverState
*bs
,
982 char *filename
, int filename_size
)
984 if (!bs
->backing_hd
) {
985 pstrcpy(filename
, filename_size
, "");
987 pstrcpy(filename
, filename_size
, bs
->backing_file
);
991 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
992 const uint8_t *buf
, int nb_sectors
)
994 BlockDriver
*drv
= bs
->drv
;
997 if (!drv
->bdrv_write_compressed
)
999 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
1002 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1004 BlockDriver
*drv
= bs
->drv
;
1007 if (!drv
->bdrv_get_info
)
1009 memset(bdi
, 0, sizeof(*bdi
));
1010 return drv
->bdrv_get_info(bs
, bdi
);
1013 /**************************************************************/
1014 /* handling of snapshots */
1016 int bdrv_snapshot_create(BlockDriverState
*bs
,
1017 QEMUSnapshotInfo
*sn_info
)
1019 BlockDriver
*drv
= bs
->drv
;
1022 if (!drv
->bdrv_snapshot_create
)
1024 return drv
->bdrv_snapshot_create(bs
, sn_info
);
1027 int bdrv_snapshot_goto(BlockDriverState
*bs
,
1028 const char *snapshot_id
)
1030 BlockDriver
*drv
= bs
->drv
;
1033 if (!drv
->bdrv_snapshot_goto
)
1035 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
1038 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1040 BlockDriver
*drv
= bs
->drv
;
1043 if (!drv
->bdrv_snapshot_delete
)
1045 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1048 int bdrv_snapshot_list(BlockDriverState
*bs
,
1049 QEMUSnapshotInfo
**psn_info
)
1051 BlockDriver
*drv
= bs
->drv
;
1054 if (!drv
->bdrv_snapshot_list
)
1056 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1059 #define NB_SUFFIXES 4
1061 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1063 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1068 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1071 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1072 if (size
< (10 * base
)) {
1073 snprintf(buf
, buf_size
, "%0.1f%c",
1074 (double)size
/ base
,
1077 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1078 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1079 ((size
+ (base
>> 1)) / base
),
1089 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1091 char buf1
[128], date_buf
[128], clock_buf
[128];
1101 snprintf(buf
, buf_size
,
1102 "%-10s%-20s%7s%20s%15s",
1103 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1107 ptm
= localtime(&ti
);
1108 strftime(date_buf
, sizeof(date_buf
),
1109 "%Y-%m-%d %H:%M:%S", ptm
);
1111 localtime_r(&ti
, &tm
);
1112 strftime(date_buf
, sizeof(date_buf
),
1113 "%Y-%m-%d %H:%M:%S", &tm
);
1115 secs
= sn
->vm_clock_nsec
/ 1000000000;
1116 snprintf(clock_buf
, sizeof(clock_buf
),
1117 "%02d:%02d:%02d.%03d",
1119 (int)((secs
/ 60) % 60),
1121 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1122 snprintf(buf
, buf_size
,
1123 "%-10s%-20s%7s%20s%15s",
1124 sn
->id_str
, sn
->name
,
1125 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1133 /**************************************************************/
1136 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1137 uint8_t *buf
, int nb_sectors
,
1138 BlockDriverCompletionFunc
*cb
, void *opaque
)
1140 BlockDriver
*drv
= bs
->drv
;
1141 BlockDriverAIOCB
*ret
;
1146 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1147 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1148 memcpy(buf
, bs
->boot_sector_data
, 512);
1154 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1157 /* Update stats even though technically transfer has not happened. */
1158 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1165 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1166 const uint8_t *buf
, int nb_sectors
,
1167 BlockDriverCompletionFunc
*cb
, void *opaque
)
1169 BlockDriver
*drv
= bs
->drv
;
1170 BlockDriverAIOCB
*ret
;
1176 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1177 memcpy(bs
->boot_sector_data
, buf
, 512);
1180 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1183 /* Update stats even though technically transfer has not happened. */
1184 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1191 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1193 BlockDriver
*drv
= acb
->bs
->drv
;
1195 drv
->bdrv_aio_cancel(acb
);
1199 /**************************************************************/
1200 /* async block device emulation */
1202 static void bdrv_aio_bh_cb(void *opaque
)
1204 BlockDriverAIOCBSync
*acb
= opaque
;
1205 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1206 qemu_aio_release(acb
);
1209 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1210 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1211 BlockDriverCompletionFunc
*cb
, void *opaque
)
1213 BlockDriverAIOCBSync
*acb
;
1216 acb
= qemu_aio_get(bs
, cb
, opaque
);
1218 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1219 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1221 qemu_bh_schedule(acb
->bh
);
1222 return &acb
->common
;
1225 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1226 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1227 BlockDriverCompletionFunc
*cb
, void *opaque
)
1229 BlockDriverAIOCBSync
*acb
;
1232 acb
= qemu_aio_get(bs
, cb
, opaque
);
1234 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1235 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1237 qemu_bh_schedule(acb
->bh
);
1238 return &acb
->common
;
1241 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1243 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1244 qemu_bh_cancel(acb
->bh
);
1245 qemu_aio_release(acb
);
1248 /**************************************************************/
1249 /* sync block device emulation */
1251 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1253 *(int *)opaque
= ret
;
1256 #define NOT_DONE 0x7fffffff
1258 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1259 uint8_t *buf
, int nb_sectors
)
1262 BlockDriverAIOCB
*acb
;
1264 async_ret
= NOT_DONE
;
1265 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1266 bdrv_rw_em_cb
, &async_ret
);
1270 while (async_ret
== NOT_DONE
) {
1277 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1278 const uint8_t *buf
, int nb_sectors
)
1281 BlockDriverAIOCB
*acb
;
1283 async_ret
= NOT_DONE
;
1284 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1285 bdrv_rw_em_cb
, &async_ret
);
1288 while (async_ret
== NOT_DONE
) {
1294 void bdrv_init(void)
1296 bdrv_register(&bdrv_raw
);
1297 bdrv_register(&bdrv_host_device
);
1299 bdrv_register(&bdrv_cow
);
1301 bdrv_register(&bdrv_qcow
);
1302 bdrv_register(&bdrv_vmdk
);
1303 bdrv_register(&bdrv_cloop
);
1304 bdrv_register(&bdrv_dmg
);
1305 bdrv_register(&bdrv_bochs
);
1306 bdrv_register(&bdrv_vpc
);
1307 bdrv_register(&bdrv_vvfat
);
1308 bdrv_register(&bdrv_qcow2
);
1309 bdrv_register(&bdrv_parallels
);
1310 bdrv_register(&bdrv_nbd
);
1315 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1319 BlockDriverAIOCB
*acb
;
1322 if (drv
->free_aiocb
) {
1323 acb
= drv
->free_aiocb
;
1324 drv
->free_aiocb
= acb
->next
;
1326 acb
= qemu_mallocz(drv
->aiocb_size
);
1332 acb
->opaque
= opaque
;
1336 void qemu_aio_release(void *p
)
1338 BlockDriverAIOCB
*acb
= p
;
1339 BlockDriver
*drv
= acb
->bs
->drv
;
1340 acb
->next
= drv
->free_aiocb
;
1341 drv
->free_aiocb
= acb
;
1344 /**************************************************************/
1345 /* removable device support */
1348 * Return TRUE if the media is present
1350 int bdrv_is_inserted(BlockDriverState
*bs
)
1352 BlockDriver
*drv
= bs
->drv
;
1356 if (!drv
->bdrv_is_inserted
)
1358 ret
= drv
->bdrv_is_inserted(bs
);
1363 * Return TRUE if the media changed since the last call to this
1364 * function. It is currently only used for floppy disks
1366 int bdrv_media_changed(BlockDriverState
*bs
)
1368 BlockDriver
*drv
= bs
->drv
;
1371 if (!drv
|| !drv
->bdrv_media_changed
)
1374 ret
= drv
->bdrv_media_changed(bs
);
1375 if (ret
== -ENOTSUP
)
1376 ret
= bs
->media_changed
;
1377 bs
->media_changed
= 0;
1382 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1384 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1386 BlockDriver
*drv
= bs
->drv
;
1389 if (!drv
|| !drv
->bdrv_eject
) {
1392 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1394 if (ret
== -ENOTSUP
) {
1400 int bdrv_is_locked(BlockDriverState
*bs
)
1406 * Lock or unlock the media (if it is locked, the user won't be able
1407 * to eject it manually).
1409 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1411 BlockDriver
*drv
= bs
->drv
;
1413 bs
->locked
= locked
;
1414 if (drv
&& drv
->bdrv_set_locked
) {
1415 drv
->bdrv_set_locked(bs
, locked
);
1419 /* needed for generic scsi interface */
1421 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1423 BlockDriver
*drv
= bs
->drv
;
1425 if (drv
&& drv
->bdrv_ioctl
)
1426 return drv
->bdrv_ioctl(bs
, req
, buf
);