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
25 #include "block_int.h"
28 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/queue.h>
36 #define SECTOR_SIZE (1 << SECTOR_BITS)
39 #define ENOMEDIUM ENODEV
42 typedef struct BlockDriverAIOCBSync
{
43 BlockDriverAIOCB common
;
46 } BlockDriverAIOCBSync
;
48 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
49 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
50 BlockDriverCompletionFunc
*cb
, void *opaque
);
51 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
52 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
53 BlockDriverCompletionFunc
*cb
, void *opaque
);
54 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
55 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
56 uint8_t *buf
, int nb_sectors
);
57 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
58 const uint8_t *buf
, int nb_sectors
);
60 static BlockDriverState
*bdrv_first
;
61 static BlockDriver
*first_drv
;
63 int path_is_absolute(const char *path
)
67 /* specific case for names like: "\\.\d:" */
68 if (*path
== '/' || *path
== '\\')
71 p
= strchr(path
, ':');
77 return (*p
== '/' || *p
== '\\');
83 /* if filename is absolute, just copy it to dest. Otherwise, build a
84 path to it by considering it is relative to base_path. URL are
86 void path_combine(char *dest
, int dest_size
,
87 const char *base_path
,
95 if (path_is_absolute(filename
)) {
96 pstrcpy(dest
, dest_size
, filename
);
98 p
= strchr(base_path
, ':');
103 p1
= strrchr(base_path
, '/');
107 p2
= strrchr(base_path
, '\\');
119 if (len
> dest_size
- 1)
121 memcpy(dest
, base_path
, len
);
123 pstrcat(dest
, dest_size
, filename
);
128 void bdrv_register(BlockDriver
*bdrv
)
130 if (!bdrv
->bdrv_aio_read
) {
131 /* add AIO emulation layer */
132 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
133 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
134 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
135 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
136 } else if (!bdrv
->bdrv_read
&& !bdrv
->bdrv_pread
) {
137 /* add synchronous IO emulation layer */
138 bdrv
->bdrv_read
= bdrv_read_em
;
139 bdrv
->bdrv_write
= bdrv_write_em
;
141 bdrv
->next
= first_drv
;
145 /* create a new block device (by default it is empty) */
146 BlockDriverState
*bdrv_new(const char *device_name
)
148 BlockDriverState
**pbs
, *bs
;
150 bs
= qemu_mallocz(sizeof(BlockDriverState
));
153 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
154 if (device_name
[0] != '\0') {
155 /* insert at the end */
164 BlockDriver
*bdrv_find_format(const char *format_name
)
167 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
168 if (!strcmp(drv1
->format_name
, format_name
))
174 int bdrv_create(BlockDriver
*drv
,
175 const char *filename
, int64_t size_in_sectors
,
176 const char *backing_file
, int flags
)
178 if (!drv
->bdrv_create
)
180 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
184 void get_tmp_filename(char *filename
, int size
)
186 char temp_dir
[MAX_PATH
];
188 GetTempPath(MAX_PATH
, temp_dir
);
189 GetTempFileName(temp_dir
, "qem", 0, filename
);
192 void get_tmp_filename(char *filename
, int size
)
195 /* XXX: race condition possible */
196 pstrcpy(filename
, size
, "/tmp/vl.XXXXXX");
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
[1024];
330 char backing_filename
[1024];
333 bs
->is_temporary
= 0;
336 if (flags
& BDRV_O_SNAPSHOT
) {
337 BlockDriverState
*bs1
;
340 /* if snapshot, we create a temporary backing file and open it
341 instead of opening 'filename' directly */
343 /* if there is a backing file, use it */
348 if (bdrv_open(bs1
, filename
, 0) < 0) {
352 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
355 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
356 realpath(filename
, backing_filename
);
357 if (bdrv_create(&bdrv_qcow2
, tmp_filename
,
358 total_size
, backing_filename
, 0) < 0) {
361 filename
= tmp_filename
;
362 bs
->is_temporary
= 1;
365 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
366 if (flags
& BDRV_O_FILE
) {
367 drv
= find_protocol(filename
);
372 drv
= find_image_format(filename
);
378 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
379 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
381 /* Note: for compatibility, we open disk image files as RDWR, and
382 RDONLY as fallback */
383 if (!(flags
& BDRV_O_FILE
))
384 open_flags
= BDRV_O_RDWR
;
386 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
387 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
388 if (ret
== -EACCES
&& !(flags
& BDRV_O_FILE
)) {
389 ret
= drv
->bdrv_open(bs
, filename
, BDRV_O_RDONLY
);
393 qemu_free(bs
->opaque
);
398 if (drv
->bdrv_getlength
) {
399 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
402 if (bs
->is_temporary
) {
406 if (bs
->backing_file
[0] != '\0') {
407 /* if there is a backing file, use it */
408 bs
->backing_hd
= bdrv_new("");
409 if (!bs
->backing_hd
) {
414 path_combine(backing_filename
, sizeof(backing_filename
),
415 filename
, bs
->backing_file
);
416 if (bdrv_open(bs
->backing_hd
, backing_filename
, 0) < 0)
420 /* call the change callback */
421 bs
->media_changed
= 1;
423 bs
->change_cb(bs
->change_opaque
);
428 void bdrv_close(BlockDriverState
*bs
)
432 bdrv_delete(bs
->backing_hd
);
433 bs
->drv
->bdrv_close(bs
);
434 qemu_free(bs
->opaque
);
436 if (bs
->is_temporary
) {
437 unlink(bs
->filename
);
443 /* call the change callback */
444 bs
->media_changed
= 1;
446 bs
->change_cb(bs
->change_opaque
);
450 void bdrv_delete(BlockDriverState
*bs
)
452 /* XXX: remove the driver list */
457 /* commit COW file into the raw image */
458 int bdrv_commit(BlockDriverState
*bs
)
460 BlockDriver
*drv
= bs
->drv
;
461 int64_t i
, total_sectors
;
463 unsigned char sector
[512];
472 if (!bs
->backing_hd
) {
476 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
477 for (i
= 0; i
< total_sectors
;) {
478 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
479 for(j
= 0; j
< n
; j
++) {
480 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
484 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
494 if (drv
->bdrv_make_empty
)
495 return drv
->bdrv_make_empty(bs
);
500 /* return < 0 if error. See bdrv_write() for the return codes */
501 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
502 uint8_t *buf
, int nb_sectors
)
504 BlockDriver
*drv
= bs
->drv
;
509 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
510 memcpy(buf
, bs
->boot_sector_data
, 512);
517 if (drv
->bdrv_pread
) {
519 len
= nb_sectors
* 512;
520 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
528 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
532 /* Return < 0 if error. Important errors are:
533 -EIO generic I/O error (may happen for all errors)
534 -ENOMEDIUM No media inserted.
535 -EINVAL Invalid sector number or nb_sectors
536 -EACCES Trying to write a read-only device
538 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
539 const uint8_t *buf
, int nb_sectors
)
541 BlockDriver
*drv
= bs
->drv
;
546 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
547 memcpy(bs
->boot_sector_data
, buf
, 512);
549 if (drv
->bdrv_pwrite
) {
551 len
= nb_sectors
* 512;
552 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
);
560 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
564 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
565 uint8_t *buf
, int count1
)
567 uint8_t tmp_buf
[SECTOR_SIZE
];
568 int len
, nb_sectors
, count
;
572 /* first read to align to sector start */
573 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
576 sector_num
= offset
>> SECTOR_BITS
;
578 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
580 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
588 /* read the sectors "in place" */
589 nb_sectors
= count
>> SECTOR_BITS
;
590 if (nb_sectors
> 0) {
591 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
593 sector_num
+= nb_sectors
;
594 len
= nb_sectors
<< SECTOR_BITS
;
599 /* add data from the last sector */
601 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
603 memcpy(buf
, tmp_buf
, count
);
608 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
609 const uint8_t *buf
, int count1
)
611 uint8_t tmp_buf
[SECTOR_SIZE
];
612 int len
, nb_sectors
, count
;
616 /* first write to align to sector start */
617 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
620 sector_num
= offset
>> SECTOR_BITS
;
622 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
624 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
625 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
634 /* write the sectors "in place" */
635 nb_sectors
= count
>> SECTOR_BITS
;
636 if (nb_sectors
> 0) {
637 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
639 sector_num
+= nb_sectors
;
640 len
= nb_sectors
<< SECTOR_BITS
;
645 /* add data from the last sector */
647 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
649 memcpy(tmp_buf
, buf
, count
);
650 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
657 * Read with byte offsets (needed only for file protocols)
659 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
660 void *buf1
, int count1
)
662 BlockDriver
*drv
= bs
->drv
;
666 if (!drv
->bdrv_pread
)
667 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
668 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
672 * Write with byte offsets (needed only for file protocols)
674 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
675 const void *buf1
, int count1
)
677 BlockDriver
*drv
= bs
->drv
;
681 if (!drv
->bdrv_pwrite
)
682 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
683 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
687 * Truncate file to 'offset' bytes (needed only for file protocols)
689 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
691 BlockDriver
*drv
= bs
->drv
;
694 if (!drv
->bdrv_truncate
)
696 return drv
->bdrv_truncate(bs
, offset
);
700 * Length of a file in bytes. Return < 0 if error or unknown.
702 int64_t bdrv_getlength(BlockDriverState
*bs
)
704 BlockDriver
*drv
= bs
->drv
;
707 if (!drv
->bdrv_getlength
) {
709 return bs
->total_sectors
* SECTOR_SIZE
;
711 return drv
->bdrv_getlength(bs
);
714 /* return 0 as number of sectors if no device present or error */
715 void bdrv_get_geometry(BlockDriverState
*bs
, int64_t *nb_sectors_ptr
)
718 length
= bdrv_getlength(bs
);
722 length
= length
>> SECTOR_BITS
;
723 *nb_sectors_ptr
= length
;
726 /* force a given boot sector. */
727 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
)
729 bs
->boot_sector_enabled
= 1;
732 memcpy(bs
->boot_sector_data
, data
, size
);
733 memset(bs
->boot_sector_data
+ size
, 0, 512 - size
);
736 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
737 int cyls
, int heads
, int secs
)
744 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
747 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
748 type
== BDRV_TYPE_FLOPPY
));
751 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
753 bs
->translation
= translation
;
756 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
757 int *pcyls
, int *pheads
, int *psecs
)
764 int bdrv_get_type_hint(BlockDriverState
*bs
)
769 int bdrv_get_translation_hint(BlockDriverState
*bs
)
771 return bs
->translation
;
774 int bdrv_is_removable(BlockDriverState
*bs
)
776 return bs
->removable
;
779 int bdrv_is_read_only(BlockDriverState
*bs
)
781 return bs
->read_only
;
784 /* XXX: no longer used */
785 void bdrv_set_change_cb(BlockDriverState
*bs
,
786 void (*change_cb
)(void *opaque
), void *opaque
)
788 bs
->change_cb
= change_cb
;
789 bs
->change_opaque
= opaque
;
792 int bdrv_is_encrypted(BlockDriverState
*bs
)
794 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
796 return bs
->encrypted
;
799 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
802 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
803 ret
= bdrv_set_key(bs
->backing_hd
, key
);
809 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
811 return bs
->drv
->bdrv_set_key(bs
, key
);
814 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
819 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
823 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
828 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
829 it(opaque
, drv
->format_name
);
833 BlockDriverState
*bdrv_find(const char *name
)
835 BlockDriverState
*bs
;
837 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
838 if (!strcmp(name
, bs
->device_name
))
844 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
846 BlockDriverState
*bs
;
848 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
849 it(opaque
, bs
->device_name
);
853 const char *bdrv_get_device_name(BlockDriverState
*bs
)
855 return bs
->device_name
;
858 void bdrv_flush(BlockDriverState
*bs
)
860 if (bs
->drv
->bdrv_flush
)
861 bs
->drv
->bdrv_flush(bs
);
863 bdrv_flush(bs
->backing_hd
);
868 BlockDriverState
*bs
;
870 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
871 term_printf("%s:", bs
->device_name
);
872 term_printf(" type=");
877 case BDRV_TYPE_CDROM
:
878 term_printf("cdrom");
880 case BDRV_TYPE_FLOPPY
:
881 term_printf("floppy");
884 term_printf(" removable=%d", bs
->removable
);
886 term_printf(" locked=%d", bs
->locked
);
889 term_printf(" file=");
890 term_print_filename(bs
->filename
);
891 if (bs
->backing_file
[0] != '\0') {
892 term_printf(" backing_file=");
893 term_print_filename(bs
->backing_file
);
895 term_printf(" ro=%d", bs
->read_only
);
896 term_printf(" drv=%s", bs
->drv
->format_name
);
898 term_printf(" encrypted");
900 term_printf(" [not inserted]");
906 void bdrv_get_backing_filename(BlockDriverState
*bs
,
907 char *filename
, int filename_size
)
909 if (!bs
->backing_hd
) {
910 pstrcpy(filename
, filename_size
, "");
912 pstrcpy(filename
, filename_size
, bs
->backing_file
);
916 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
917 const uint8_t *buf
, int nb_sectors
)
919 BlockDriver
*drv
= bs
->drv
;
922 if (!drv
->bdrv_write_compressed
)
924 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
927 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
929 BlockDriver
*drv
= bs
->drv
;
932 if (!drv
->bdrv_get_info
)
934 memset(bdi
, 0, sizeof(*bdi
));
935 return drv
->bdrv_get_info(bs
, bdi
);
938 /**************************************************************/
939 /* handling of snapshots */
941 int bdrv_snapshot_create(BlockDriverState
*bs
,
942 QEMUSnapshotInfo
*sn_info
)
944 BlockDriver
*drv
= bs
->drv
;
947 if (!drv
->bdrv_snapshot_create
)
949 return drv
->bdrv_snapshot_create(bs
, sn_info
);
952 int bdrv_snapshot_goto(BlockDriverState
*bs
,
953 const char *snapshot_id
)
955 BlockDriver
*drv
= bs
->drv
;
958 if (!drv
->bdrv_snapshot_goto
)
960 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
963 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
965 BlockDriver
*drv
= bs
->drv
;
968 if (!drv
->bdrv_snapshot_delete
)
970 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
973 int bdrv_snapshot_list(BlockDriverState
*bs
,
974 QEMUSnapshotInfo
**psn_info
)
976 BlockDriver
*drv
= bs
->drv
;
979 if (!drv
->bdrv_snapshot_list
)
981 return drv
->bdrv_snapshot_list(bs
, psn_info
);
984 #define NB_SUFFIXES 4
986 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
988 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
993 snprintf(buf
, buf_size
, "%" PRId64
, size
);
996 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
997 if (size
< (10 * base
)) {
998 snprintf(buf
, buf_size
, "%0.1f%c",
1002 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1003 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1004 ((size
+ (base
>> 1)) / base
),
1014 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1016 char buf1
[128], date_buf
[128], clock_buf
[128];
1026 snprintf(buf
, buf_size
,
1027 "%-10s%-20s%7s%20s%15s",
1028 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1032 ptm
= localtime(&ti
);
1033 strftime(date_buf
, sizeof(date_buf
),
1034 "%Y-%m-%d %H:%M:%S", ptm
);
1036 localtime_r(&ti
, &tm
);
1037 strftime(date_buf
, sizeof(date_buf
),
1038 "%Y-%m-%d %H:%M:%S", &tm
);
1040 secs
= sn
->vm_clock_nsec
/ 1000000000;
1041 snprintf(clock_buf
, sizeof(clock_buf
),
1042 "%02d:%02d:%02d.%03d",
1044 (int)((secs
/ 60) % 60),
1046 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1047 snprintf(buf
, buf_size
,
1048 "%-10s%-20s%7s%20s%15s",
1049 sn
->id_str
, sn
->name
,
1050 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1058 /**************************************************************/
1061 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1062 uint8_t *buf
, int nb_sectors
,
1063 BlockDriverCompletionFunc
*cb
, void *opaque
)
1065 BlockDriver
*drv
= bs
->drv
;
1070 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1071 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1072 memcpy(buf
, bs
->boot_sector_data
, 512);
1078 return drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1081 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1082 const uint8_t *buf
, int nb_sectors
,
1083 BlockDriverCompletionFunc
*cb
, void *opaque
)
1085 BlockDriver
*drv
= bs
->drv
;
1091 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1092 memcpy(bs
->boot_sector_data
, buf
, 512);
1095 return drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1098 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1100 BlockDriver
*drv
= acb
->bs
->drv
;
1102 drv
->bdrv_aio_cancel(acb
);
1106 /**************************************************************/
1107 /* async block device emulation */
1110 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1111 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1112 BlockDriverCompletionFunc
*cb
, void *opaque
)
1115 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1120 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1121 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1122 BlockDriverCompletionFunc
*cb
, void *opaque
)
1125 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1130 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
)
1134 static void bdrv_aio_bh_cb(void *opaque
)
1136 BlockDriverAIOCBSync
*acb
= opaque
;
1137 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1138 qemu_aio_release(acb
);
1141 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1142 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1143 BlockDriverCompletionFunc
*cb
, void *opaque
)
1145 BlockDriverAIOCBSync
*acb
;
1148 acb
= qemu_aio_get(bs
, cb
, opaque
);
1150 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1151 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1153 qemu_bh_schedule(acb
->bh
);
1154 return &acb
->common
;
1157 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1158 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1159 BlockDriverCompletionFunc
*cb
, void *opaque
)
1161 BlockDriverAIOCBSync
*acb
;
1164 acb
= qemu_aio_get(bs
, cb
, opaque
);
1166 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1167 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1169 qemu_bh_schedule(acb
->bh
);
1170 return &acb
->common
;
1173 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1175 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1176 qemu_bh_cancel(acb
->bh
);
1177 qemu_aio_release(acb
);
1179 #endif /* !QEMU_TOOL */
1181 /**************************************************************/
1182 /* sync block device emulation */
1184 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1186 *(int *)opaque
= ret
;
1189 #define NOT_DONE 0x7fffffff
1191 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1192 uint8_t *buf
, int nb_sectors
)
1195 BlockDriverAIOCB
*acb
;
1197 async_ret
= NOT_DONE
;
1198 qemu_aio_wait_start();
1199 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1200 bdrv_rw_em_cb
, &async_ret
);
1202 qemu_aio_wait_end();
1205 while (async_ret
== NOT_DONE
) {
1208 qemu_aio_wait_end();
1212 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1213 const uint8_t *buf
, int nb_sectors
)
1216 BlockDriverAIOCB
*acb
;
1218 async_ret
= NOT_DONE
;
1219 qemu_aio_wait_start();
1220 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1221 bdrv_rw_em_cb
, &async_ret
);
1223 qemu_aio_wait_end();
1226 while (async_ret
== NOT_DONE
) {
1229 qemu_aio_wait_end();
1233 void bdrv_init(void)
1235 bdrv_register(&bdrv_raw
);
1236 bdrv_register(&bdrv_host_device
);
1238 bdrv_register(&bdrv_cow
);
1240 bdrv_register(&bdrv_qcow
);
1241 bdrv_register(&bdrv_vmdk
);
1242 bdrv_register(&bdrv_cloop
);
1243 bdrv_register(&bdrv_dmg
);
1244 bdrv_register(&bdrv_bochs
);
1245 bdrv_register(&bdrv_vpc
);
1246 bdrv_register(&bdrv_vvfat
);
1247 bdrv_register(&bdrv_qcow2
);
1250 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1254 BlockDriverAIOCB
*acb
;
1257 if (drv
->free_aiocb
) {
1258 acb
= drv
->free_aiocb
;
1259 drv
->free_aiocb
= acb
->next
;
1261 acb
= qemu_mallocz(drv
->aiocb_size
);
1267 acb
->opaque
= opaque
;
1271 void qemu_aio_release(void *p
)
1273 BlockDriverAIOCB
*acb
= p
;
1274 BlockDriver
*drv
= acb
->bs
->drv
;
1275 acb
->next
= drv
->free_aiocb
;
1276 drv
->free_aiocb
= acb
;
1279 /**************************************************************/
1280 /* removable device support */
1283 * Return TRUE if the media is present
1285 int bdrv_is_inserted(BlockDriverState
*bs
)
1287 BlockDriver
*drv
= bs
->drv
;
1291 if (!drv
->bdrv_is_inserted
)
1293 ret
= drv
->bdrv_is_inserted(bs
);
1298 * Return TRUE if the media changed since the last call to this
1299 * function. It is currently only used for floppy disks
1301 int bdrv_media_changed(BlockDriverState
*bs
)
1303 BlockDriver
*drv
= bs
->drv
;
1306 if (!drv
|| !drv
->bdrv_media_changed
)
1309 ret
= drv
->bdrv_media_changed(bs
);
1310 if (ret
== -ENOTSUP
)
1311 ret
= bs
->media_changed
;
1312 bs
->media_changed
= 0;
1317 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1319 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1321 BlockDriver
*drv
= bs
->drv
;
1324 if (!drv
|| !drv
->bdrv_eject
) {
1327 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1329 if (ret
== -ENOTSUP
) {
1335 int bdrv_is_locked(BlockDriverState
*bs
)
1341 * Lock or unlock the media (if it is locked, the user won't be able
1342 * to eject it manually).
1344 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1346 BlockDriver
*drv
= bs
->drv
;
1348 bs
->locked
= locked
;
1349 if (drv
&& drv
->bdrv_set_locked
) {
1350 drv
->bdrv_set_locked(bs
, locked
);