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 /* XXX: remove the driver list */
460 /* commit COW file into the raw image */
461 int bdrv_commit(BlockDriverState
*bs
)
463 BlockDriver
*drv
= bs
->drv
;
464 int64_t i
, total_sectors
;
466 unsigned char sector
[512];
475 if (!bs
->backing_hd
) {
479 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
480 for (i
= 0; i
< total_sectors
;) {
481 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
482 for(j
= 0; j
< n
; j
++) {
483 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
487 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
497 if (drv
->bdrv_make_empty
)
498 return drv
->bdrv_make_empty(bs
);
503 /* return < 0 if error. See bdrv_write() for the return codes */
504 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
505 uint8_t *buf
, int nb_sectors
)
507 BlockDriver
*drv
= bs
->drv
;
512 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
513 memcpy(buf
, bs
->boot_sector_data
, 512);
520 if (drv
->bdrv_pread
) {
522 len
= nb_sectors
* 512;
523 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
529 bs
->rd_bytes
+= (unsigned) len
;
534 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
538 /* Return < 0 if error. Important errors are:
539 -EIO generic I/O error (may happen for all errors)
540 -ENOMEDIUM No media inserted.
541 -EINVAL Invalid sector number or nb_sectors
542 -EACCES Trying to write a read-only device
544 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
545 const uint8_t *buf
, int nb_sectors
)
547 BlockDriver
*drv
= bs
->drv
;
552 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
553 memcpy(bs
->boot_sector_data
, buf
, 512);
555 if (drv
->bdrv_pwrite
) {
557 len
= nb_sectors
* 512;
558 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
);
564 bs
->wr_bytes
+= (unsigned) len
;
569 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
573 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
574 uint8_t *buf
, int count1
)
576 uint8_t tmp_buf
[SECTOR_SIZE
];
577 int len
, nb_sectors
, count
;
581 /* first read to align to sector start */
582 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
585 sector_num
= offset
>> SECTOR_BITS
;
587 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
589 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
597 /* read the sectors "in place" */
598 nb_sectors
= count
>> SECTOR_BITS
;
599 if (nb_sectors
> 0) {
600 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
602 sector_num
+= nb_sectors
;
603 len
= nb_sectors
<< SECTOR_BITS
;
608 /* add data from the last sector */
610 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
612 memcpy(buf
, tmp_buf
, count
);
617 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
618 const uint8_t *buf
, int count1
)
620 uint8_t tmp_buf
[SECTOR_SIZE
];
621 int len
, nb_sectors
, count
;
625 /* first write to align to sector start */
626 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
629 sector_num
= offset
>> SECTOR_BITS
;
631 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
633 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
634 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
643 /* write the sectors "in place" */
644 nb_sectors
= count
>> SECTOR_BITS
;
645 if (nb_sectors
> 0) {
646 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
648 sector_num
+= nb_sectors
;
649 len
= nb_sectors
<< SECTOR_BITS
;
654 /* add data from the last sector */
656 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
658 memcpy(tmp_buf
, buf
, count
);
659 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
666 * Read with byte offsets (needed only for file protocols)
668 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
669 void *buf1
, int count1
)
671 BlockDriver
*drv
= bs
->drv
;
675 if (!drv
->bdrv_pread
)
676 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
677 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
681 * Write with byte offsets (needed only for file protocols)
683 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
684 const void *buf1
, int count1
)
686 BlockDriver
*drv
= bs
->drv
;
690 if (!drv
->bdrv_pwrite
)
691 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
692 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
696 * Truncate file to 'offset' bytes (needed only for file protocols)
698 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
700 BlockDriver
*drv
= bs
->drv
;
703 if (!drv
->bdrv_truncate
)
705 return drv
->bdrv_truncate(bs
, offset
);
709 * Length of a file in bytes. Return < 0 if error or unknown.
711 int64_t bdrv_getlength(BlockDriverState
*bs
)
713 BlockDriver
*drv
= bs
->drv
;
716 if (!drv
->bdrv_getlength
) {
718 return bs
->total_sectors
* SECTOR_SIZE
;
720 return drv
->bdrv_getlength(bs
);
723 /* return 0 as number of sectors if no device present or error */
724 void bdrv_get_geometry(BlockDriverState
*bs
, uint64_t *nb_sectors_ptr
)
727 length
= bdrv_getlength(bs
);
731 length
= length
>> SECTOR_BITS
;
732 *nb_sectors_ptr
= length
;
735 /* force a given boot sector. */
736 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
)
738 bs
->boot_sector_enabled
= 1;
741 memcpy(bs
->boot_sector_data
, data
, size
);
742 memset(bs
->boot_sector_data
+ size
, 0, 512 - size
);
745 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
746 int cyls
, int heads
, int secs
)
753 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
756 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
757 type
== BDRV_TYPE_FLOPPY
));
760 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
762 bs
->translation
= translation
;
765 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
766 int *pcyls
, int *pheads
, int *psecs
)
773 int bdrv_get_type_hint(BlockDriverState
*bs
)
778 int bdrv_get_translation_hint(BlockDriverState
*bs
)
780 return bs
->translation
;
783 int bdrv_is_removable(BlockDriverState
*bs
)
785 return bs
->removable
;
788 int bdrv_is_read_only(BlockDriverState
*bs
)
790 return bs
->read_only
;
793 int bdrv_is_sg(BlockDriverState
*bs
)
798 /* XXX: no longer used */
799 void bdrv_set_change_cb(BlockDriverState
*bs
,
800 void (*change_cb
)(void *opaque
), void *opaque
)
802 bs
->change_cb
= change_cb
;
803 bs
->change_opaque
= opaque
;
806 int bdrv_is_encrypted(BlockDriverState
*bs
)
808 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
810 return bs
->encrypted
;
813 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
816 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
817 ret
= bdrv_set_key(bs
->backing_hd
, key
);
823 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
825 return bs
->drv
->bdrv_set_key(bs
, key
);
828 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
833 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
837 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
842 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
843 it(opaque
, drv
->format_name
);
847 BlockDriverState
*bdrv_find(const char *name
)
849 BlockDriverState
*bs
;
851 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
852 if (!strcmp(name
, bs
->device_name
))
858 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
860 BlockDriverState
*bs
;
862 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
863 it(opaque
, bs
->device_name
);
867 const char *bdrv_get_device_name(BlockDriverState
*bs
)
869 return bs
->device_name
;
872 void bdrv_flush(BlockDriverState
*bs
)
874 if (bs
->drv
->bdrv_flush
)
875 bs
->drv
->bdrv_flush(bs
);
877 bdrv_flush(bs
->backing_hd
);
883 BlockDriverState
*bs
;
885 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
886 term_printf("%s:", bs
->device_name
);
887 term_printf(" type=");
892 case BDRV_TYPE_CDROM
:
893 term_printf("cdrom");
895 case BDRV_TYPE_FLOPPY
:
896 term_printf("floppy");
899 term_printf(" removable=%d", bs
->removable
);
901 term_printf(" locked=%d", bs
->locked
);
904 term_printf(" file=");
905 term_print_filename(bs
->filename
);
906 if (bs
->backing_file
[0] != '\0') {
907 term_printf(" backing_file=");
908 term_print_filename(bs
->backing_file
);
910 term_printf(" ro=%d", bs
->read_only
);
911 term_printf(" drv=%s", bs
->drv
->format_name
);
913 term_printf(" encrypted");
915 term_printf(" [not inserted]");
921 /* The "info blockstats" command. */
922 void bdrv_info_stats (void)
924 BlockDriverState
*bs
;
926 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
930 " rd_operations=%" PRIu64
931 " wr_operations=%" PRIu64
934 bs
->rd_bytes
, bs
->wr_bytes
,
935 bs
->rd_ops
, bs
->wr_ops
);
940 void bdrv_get_backing_filename(BlockDriverState
*bs
,
941 char *filename
, int filename_size
)
943 if (!bs
->backing_hd
) {
944 pstrcpy(filename
, filename_size
, "");
946 pstrcpy(filename
, filename_size
, bs
->backing_file
);
950 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
951 const uint8_t *buf
, int nb_sectors
)
953 BlockDriver
*drv
= bs
->drv
;
956 if (!drv
->bdrv_write_compressed
)
958 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
961 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
963 BlockDriver
*drv
= bs
->drv
;
966 if (!drv
->bdrv_get_info
)
968 memset(bdi
, 0, sizeof(*bdi
));
969 return drv
->bdrv_get_info(bs
, bdi
);
972 /**************************************************************/
973 /* handling of snapshots */
975 int bdrv_snapshot_create(BlockDriverState
*bs
,
976 QEMUSnapshotInfo
*sn_info
)
978 BlockDriver
*drv
= bs
->drv
;
981 if (!drv
->bdrv_snapshot_create
)
983 return drv
->bdrv_snapshot_create(bs
, sn_info
);
986 int bdrv_snapshot_goto(BlockDriverState
*bs
,
987 const char *snapshot_id
)
989 BlockDriver
*drv
= bs
->drv
;
992 if (!drv
->bdrv_snapshot_goto
)
994 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
997 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
999 BlockDriver
*drv
= bs
->drv
;
1002 if (!drv
->bdrv_snapshot_delete
)
1004 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
1007 int bdrv_snapshot_list(BlockDriverState
*bs
,
1008 QEMUSnapshotInfo
**psn_info
)
1010 BlockDriver
*drv
= bs
->drv
;
1013 if (!drv
->bdrv_snapshot_list
)
1015 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1018 #define NB_SUFFIXES 4
1020 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1022 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1027 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1030 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1031 if (size
< (10 * base
)) {
1032 snprintf(buf
, buf_size
, "%0.1f%c",
1033 (double)size
/ base
,
1036 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1037 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1038 ((size
+ (base
>> 1)) / base
),
1048 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1050 char buf1
[128], date_buf
[128], clock_buf
[128];
1060 snprintf(buf
, buf_size
,
1061 "%-10s%-20s%7s%20s%15s",
1062 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1066 ptm
= localtime(&ti
);
1067 strftime(date_buf
, sizeof(date_buf
),
1068 "%Y-%m-%d %H:%M:%S", ptm
);
1070 localtime_r(&ti
, &tm
);
1071 strftime(date_buf
, sizeof(date_buf
),
1072 "%Y-%m-%d %H:%M:%S", &tm
);
1074 secs
= sn
->vm_clock_nsec
/ 1000000000;
1075 snprintf(clock_buf
, sizeof(clock_buf
),
1076 "%02d:%02d:%02d.%03d",
1078 (int)((secs
/ 60) % 60),
1080 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1081 snprintf(buf
, buf_size
,
1082 "%-10s%-20s%7s%20s%15s",
1083 sn
->id_str
, sn
->name
,
1084 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1092 /**************************************************************/
1095 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1096 uint8_t *buf
, int nb_sectors
,
1097 BlockDriverCompletionFunc
*cb
, void *opaque
)
1099 BlockDriver
*drv
= bs
->drv
;
1100 BlockDriverAIOCB
*ret
;
1105 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1106 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1107 memcpy(buf
, bs
->boot_sector_data
, 512);
1113 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1116 /* Update stats even though technically transfer has not happened. */
1117 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1124 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1125 const uint8_t *buf
, int nb_sectors
,
1126 BlockDriverCompletionFunc
*cb
, void *opaque
)
1128 BlockDriver
*drv
= bs
->drv
;
1129 BlockDriverAIOCB
*ret
;
1135 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1136 memcpy(bs
->boot_sector_data
, buf
, 512);
1139 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1142 /* Update stats even though technically transfer has not happened. */
1143 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1150 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1152 BlockDriver
*drv
= acb
->bs
->drv
;
1154 drv
->bdrv_aio_cancel(acb
);
1158 /**************************************************************/
1159 /* async block device emulation */
1162 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1163 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1164 BlockDriverCompletionFunc
*cb
, void *opaque
)
1167 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1172 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1173 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1174 BlockDriverCompletionFunc
*cb
, void *opaque
)
1177 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1182 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
)
1186 static void bdrv_aio_bh_cb(void *opaque
)
1188 BlockDriverAIOCBSync
*acb
= opaque
;
1189 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1190 qemu_aio_release(acb
);
1193 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1194 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1195 BlockDriverCompletionFunc
*cb
, void *opaque
)
1197 BlockDriverAIOCBSync
*acb
;
1200 acb
= qemu_aio_get(bs
, cb
, opaque
);
1202 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1203 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1205 qemu_bh_schedule(acb
->bh
);
1206 return &acb
->common
;
1209 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1210 int64_t sector_num
, const 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_write(bs
, sector_num
, buf
, nb_sectors
);
1221 qemu_bh_schedule(acb
->bh
);
1222 return &acb
->common
;
1225 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1227 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1228 qemu_bh_cancel(acb
->bh
);
1229 qemu_aio_release(acb
);
1231 #endif /* !QEMU_IMG */
1233 /**************************************************************/
1234 /* sync block device emulation */
1236 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1238 *(int *)opaque
= ret
;
1241 #define NOT_DONE 0x7fffffff
1243 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1244 uint8_t *buf
, int nb_sectors
)
1247 BlockDriverAIOCB
*acb
;
1249 async_ret
= NOT_DONE
;
1250 qemu_aio_wait_start();
1251 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1252 bdrv_rw_em_cb
, &async_ret
);
1254 qemu_aio_wait_end();
1257 while (async_ret
== NOT_DONE
) {
1260 qemu_aio_wait_end();
1264 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1265 const uint8_t *buf
, int nb_sectors
)
1268 BlockDriverAIOCB
*acb
;
1270 async_ret
= NOT_DONE
;
1271 qemu_aio_wait_start();
1272 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1273 bdrv_rw_em_cb
, &async_ret
);
1275 qemu_aio_wait_end();
1278 while (async_ret
== NOT_DONE
) {
1281 qemu_aio_wait_end();
1285 void bdrv_init(void)
1287 bdrv_register(&bdrv_raw
);
1288 bdrv_register(&bdrv_host_device
);
1290 bdrv_register(&bdrv_cow
);
1292 bdrv_register(&bdrv_qcow
);
1293 bdrv_register(&bdrv_vmdk
);
1294 bdrv_register(&bdrv_cloop
);
1295 bdrv_register(&bdrv_dmg
);
1296 bdrv_register(&bdrv_bochs
);
1297 bdrv_register(&bdrv_vpc
);
1298 bdrv_register(&bdrv_vvfat
);
1299 bdrv_register(&bdrv_qcow2
);
1300 bdrv_register(&bdrv_parallels
);
1303 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1307 BlockDriverAIOCB
*acb
;
1310 if (drv
->free_aiocb
) {
1311 acb
= drv
->free_aiocb
;
1312 drv
->free_aiocb
= acb
->next
;
1314 acb
= qemu_mallocz(drv
->aiocb_size
);
1320 acb
->opaque
= opaque
;
1324 void qemu_aio_release(void *p
)
1326 BlockDriverAIOCB
*acb
= p
;
1327 BlockDriver
*drv
= acb
->bs
->drv
;
1328 acb
->next
= drv
->free_aiocb
;
1329 drv
->free_aiocb
= acb
;
1332 /**************************************************************/
1333 /* removable device support */
1336 * Return TRUE if the media is present
1338 int bdrv_is_inserted(BlockDriverState
*bs
)
1340 BlockDriver
*drv
= bs
->drv
;
1344 if (!drv
->bdrv_is_inserted
)
1346 ret
= drv
->bdrv_is_inserted(bs
);
1351 * Return TRUE if the media changed since the last call to this
1352 * function. It is currently only used for floppy disks
1354 int bdrv_media_changed(BlockDriverState
*bs
)
1356 BlockDriver
*drv
= bs
->drv
;
1359 if (!drv
|| !drv
->bdrv_media_changed
)
1362 ret
= drv
->bdrv_media_changed(bs
);
1363 if (ret
== -ENOTSUP
)
1364 ret
= bs
->media_changed
;
1365 bs
->media_changed
= 0;
1370 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1372 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1374 BlockDriver
*drv
= bs
->drv
;
1377 if (!drv
|| !drv
->bdrv_eject
) {
1380 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1382 if (ret
== -ENOTSUP
) {
1388 int bdrv_is_locked(BlockDriverState
*bs
)
1394 * Lock or unlock the media (if it is locked, the user won't be able
1395 * to eject it manually).
1397 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1399 BlockDriver
*drv
= bs
->drv
;
1401 bs
->locked
= locked
;
1402 if (drv
&& drv
->bdrv_set_locked
) {
1403 drv
->bdrv_set_locked(bs
, locked
);
1407 /* needed for generic scsi interface */
1409 int bdrv_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1411 BlockDriver
*drv
= bs
->drv
;
1413 if (drv
&& drv
->bdrv_ioctl
)
1414 return drv
->bdrv_ioctl(bs
, req
, buf
);