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)
38 typedef struct BlockDriverAIOCBSync
{
39 BlockDriverAIOCB common
;
42 } BlockDriverAIOCBSync
;
44 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
45 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
46 BlockDriverCompletionFunc
*cb
, void *opaque
);
47 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
48 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
49 BlockDriverCompletionFunc
*cb
, void *opaque
);
50 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
51 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
52 uint8_t *buf
, int nb_sectors
);
53 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
54 const uint8_t *buf
, int nb_sectors
);
56 static BlockDriverState
*bdrv_first
;
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 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
)
191 /* XXX: race condition possible */
192 pstrcpy(filename
, size
, "/tmp/vl.XXXXXX");
193 fd
= mkstemp(filename
);
199 static int is_windows_drive_prefix(const char *filename
)
201 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
202 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
206 static int is_windows_drive(const char *filename
)
208 if (is_windows_drive_prefix(filename
) &&
211 if (strstart(filename
, "\\\\.\\", NULL
) ||
212 strstart(filename
, "//./", NULL
))
218 static BlockDriver
*find_protocol(const char *filename
)
226 if (is_windows_drive(filename
) ||
227 is_windows_drive_prefix(filename
))
230 p
= strchr(filename
, ':');
234 if (len
> sizeof(protocol
) - 1)
235 len
= sizeof(protocol
) - 1;
236 memcpy(protocol
, filename
, len
);
237 protocol
[len
] = '\0';
238 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
239 if (drv1
->protocol_name
&&
240 !strcmp(drv1
->protocol_name
, protocol
))
246 /* XXX: force raw format if block or character device ? It would
247 simplify the BSD case */
248 static BlockDriver
*find_image_format(const char *filename
)
250 int ret
, score
, score_max
;
251 BlockDriver
*drv1
, *drv
;
253 BlockDriverState
*bs
;
255 /* detect host devices. By convention, /dev/cdrom[N] is always
256 recognized as a host CDROM */
257 if (strstart(filename
, "/dev/cdrom", NULL
))
258 return &bdrv_host_device
;
260 if (is_windows_drive(filename
))
261 return &bdrv_host_device
;
265 if (stat(filename
, &st
) >= 0 &&
266 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
267 return &bdrv_host_device
;
272 drv
= find_protocol(filename
);
273 /* no need to test disk image formats for vvfat */
274 if (drv
== &bdrv_vvfat
)
277 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
280 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
287 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
288 if (drv1
->bdrv_probe
) {
289 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
290 if (score
> score_max
) {
299 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
301 BlockDriverState
*bs
;
307 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
316 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
318 return bdrv_open2(bs
, filename
, flags
, NULL
);
321 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
325 char tmp_filename
[PATH_MAX
];
326 char backing_filename
[PATH_MAX
];
329 bs
->is_temporary
= 0;
332 if (flags
& BDRV_O_SNAPSHOT
) {
333 BlockDriverState
*bs1
;
336 /* if snapshot, we create a temporary backing file and open it
337 instead of opening 'filename' directly */
339 /* if there is a backing file, use it */
344 if (bdrv_open(bs1
, filename
, 0) < 0) {
348 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
351 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
352 realpath(filename
, backing_filename
);
353 if (bdrv_create(&bdrv_qcow2
, tmp_filename
,
354 total_size
, backing_filename
, 0) < 0) {
357 filename
= tmp_filename
;
358 bs
->is_temporary
= 1;
361 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
362 if (flags
& BDRV_O_FILE
) {
363 drv
= find_protocol(filename
);
368 drv
= find_image_format(filename
);
374 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
375 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
377 /* Note: for compatibility, we open disk image files as RDWR, and
378 RDONLY as fallback */
379 if (!(flags
& BDRV_O_FILE
))
380 open_flags
= BDRV_O_RDWR
;
382 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
383 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
384 if (ret
== -EACCES
&& !(flags
& BDRV_O_FILE
)) {
385 ret
= drv
->bdrv_open(bs
, filename
, BDRV_O_RDONLY
);
389 qemu_free(bs
->opaque
);
394 if (drv
->bdrv_getlength
) {
395 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
398 if (bs
->is_temporary
) {
402 if (bs
->backing_file
[0] != '\0') {
403 /* if there is a backing file, use it */
404 bs
->backing_hd
= bdrv_new("");
405 if (!bs
->backing_hd
) {
410 path_combine(backing_filename
, sizeof(backing_filename
),
411 filename
, bs
->backing_file
);
412 if (bdrv_open(bs
->backing_hd
, backing_filename
, 0) < 0)
416 /* call the change callback */
417 bs
->media_changed
= 1;
419 bs
->change_cb(bs
->change_opaque
);
424 void bdrv_close(BlockDriverState
*bs
)
428 bdrv_delete(bs
->backing_hd
);
429 bs
->drv
->bdrv_close(bs
);
430 qemu_free(bs
->opaque
);
432 if (bs
->is_temporary
) {
433 unlink(bs
->filename
);
439 /* call the change callback */
440 bs
->media_changed
= 1;
442 bs
->change_cb(bs
->change_opaque
);
446 void bdrv_delete(BlockDriverState
*bs
)
448 /* XXX: remove the driver list */
453 /* commit COW file into the raw image */
454 int bdrv_commit(BlockDriverState
*bs
)
456 BlockDriver
*drv
= bs
->drv
;
457 int64_t i
, total_sectors
;
459 unsigned char sector
[512];
468 if (!bs
->backing_hd
) {
472 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
473 for (i
= 0; i
< total_sectors
;) {
474 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
475 for(j
= 0; j
< n
; j
++) {
476 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
480 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
490 if (drv
->bdrv_make_empty
)
491 return drv
->bdrv_make_empty(bs
);
496 /* return < 0 if error. See bdrv_write() for the return codes */
497 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
498 uint8_t *buf
, int nb_sectors
)
500 BlockDriver
*drv
= bs
->drv
;
505 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
506 memcpy(buf
, bs
->boot_sector_data
, 512);
513 if (drv
->bdrv_pread
) {
515 len
= nb_sectors
* 512;
516 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
524 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
528 /* Return < 0 if error. Important errors are:
529 -EIO generic I/O error (may happen for all errors)
530 -ENOMEDIUM No media inserted.
531 -EINVAL Invalid sector number or nb_sectors
532 -EACCES Trying to write a read-only device
534 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
535 const uint8_t *buf
, int nb_sectors
)
537 BlockDriver
*drv
= bs
->drv
;
542 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
543 memcpy(bs
->boot_sector_data
, buf
, 512);
545 if (drv
->bdrv_pwrite
) {
547 len
= nb_sectors
* 512;
548 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
);
556 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
560 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
561 uint8_t *buf
, int count1
)
563 uint8_t tmp_buf
[SECTOR_SIZE
];
564 int len
, nb_sectors
, count
;
568 /* first read to align to sector start */
569 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
572 sector_num
= offset
>> SECTOR_BITS
;
574 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
576 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
584 /* read the sectors "in place" */
585 nb_sectors
= count
>> SECTOR_BITS
;
586 if (nb_sectors
> 0) {
587 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
589 sector_num
+= nb_sectors
;
590 len
= nb_sectors
<< SECTOR_BITS
;
595 /* add data from the last sector */
597 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
599 memcpy(buf
, tmp_buf
, count
);
604 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
605 const uint8_t *buf
, int count1
)
607 uint8_t tmp_buf
[SECTOR_SIZE
];
608 int len
, nb_sectors
, count
;
612 /* first write to align to sector start */
613 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
616 sector_num
= offset
>> SECTOR_BITS
;
618 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
620 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
621 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
630 /* write the sectors "in place" */
631 nb_sectors
= count
>> SECTOR_BITS
;
632 if (nb_sectors
> 0) {
633 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
635 sector_num
+= nb_sectors
;
636 len
= nb_sectors
<< SECTOR_BITS
;
641 /* add data from the last sector */
643 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
645 memcpy(tmp_buf
, buf
, count
);
646 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
653 * Read with byte offsets (needed only for file protocols)
655 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
656 void *buf1
, int count1
)
658 BlockDriver
*drv
= bs
->drv
;
662 if (!drv
->bdrv_pread
)
663 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
664 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
668 * Write with byte offsets (needed only for file protocols)
670 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
671 const void *buf1
, int count1
)
673 BlockDriver
*drv
= bs
->drv
;
677 if (!drv
->bdrv_pwrite
)
678 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
679 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
683 * Truncate file to 'offset' bytes (needed only for file protocols)
685 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
687 BlockDriver
*drv
= bs
->drv
;
690 if (!drv
->bdrv_truncate
)
692 return drv
->bdrv_truncate(bs
, offset
);
696 * Length of a file in bytes. Return < 0 if error or unknown.
698 int64_t bdrv_getlength(BlockDriverState
*bs
)
700 BlockDriver
*drv
= bs
->drv
;
703 if (!drv
->bdrv_getlength
) {
705 return bs
->total_sectors
* SECTOR_SIZE
;
707 return drv
->bdrv_getlength(bs
);
710 /* return 0 as number of sectors if no device present or error */
711 void bdrv_get_geometry(BlockDriverState
*bs
, int64_t *nb_sectors_ptr
)
714 length
= bdrv_getlength(bs
);
718 length
= length
>> SECTOR_BITS
;
719 *nb_sectors_ptr
= length
;
722 /* force a given boot sector. */
723 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
)
725 bs
->boot_sector_enabled
= 1;
728 memcpy(bs
->boot_sector_data
, data
, size
);
729 memset(bs
->boot_sector_data
+ size
, 0, 512 - size
);
732 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
733 int cyls
, int heads
, int secs
)
740 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
743 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
744 type
== BDRV_TYPE_FLOPPY
));
747 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
749 bs
->translation
= translation
;
752 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
753 int *pcyls
, int *pheads
, int *psecs
)
760 int bdrv_get_type_hint(BlockDriverState
*bs
)
765 int bdrv_get_translation_hint(BlockDriverState
*bs
)
767 return bs
->translation
;
770 int bdrv_is_removable(BlockDriverState
*bs
)
772 return bs
->removable
;
775 int bdrv_is_read_only(BlockDriverState
*bs
)
777 return bs
->read_only
;
780 /* XXX: no longer used */
781 void bdrv_set_change_cb(BlockDriverState
*bs
,
782 void (*change_cb
)(void *opaque
), void *opaque
)
784 bs
->change_cb
= change_cb
;
785 bs
->change_opaque
= opaque
;
788 int bdrv_is_encrypted(BlockDriverState
*bs
)
790 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
792 return bs
->encrypted
;
795 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
798 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
799 ret
= bdrv_set_key(bs
->backing_hd
, key
);
805 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
807 return bs
->drv
->bdrv_set_key(bs
, key
);
810 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
815 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
819 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
824 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
825 it(opaque
, drv
->format_name
);
829 BlockDriverState
*bdrv_find(const char *name
)
831 BlockDriverState
*bs
;
833 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
834 if (!strcmp(name
, bs
->device_name
))
840 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
842 BlockDriverState
*bs
;
844 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
845 it(opaque
, bs
->device_name
);
849 const char *bdrv_get_device_name(BlockDriverState
*bs
)
851 return bs
->device_name
;
854 void bdrv_flush(BlockDriverState
*bs
)
856 if (bs
->drv
->bdrv_flush
)
857 bs
->drv
->bdrv_flush(bs
);
859 bdrv_flush(bs
->backing_hd
);
864 BlockDriverState
*bs
;
866 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
867 term_printf("%s:", bs
->device_name
);
868 term_printf(" type=");
873 case BDRV_TYPE_CDROM
:
874 term_printf("cdrom");
876 case BDRV_TYPE_FLOPPY
:
877 term_printf("floppy");
880 term_printf(" removable=%d", bs
->removable
);
882 term_printf(" locked=%d", bs
->locked
);
885 term_printf(" file=");
886 term_print_filename(bs
->filename
);
887 if (bs
->backing_file
[0] != '\0') {
888 term_printf(" backing_file=");
889 term_print_filename(bs
->backing_file
);
891 term_printf(" ro=%d", bs
->read_only
);
892 term_printf(" drv=%s", bs
->drv
->format_name
);
894 term_printf(" encrypted");
896 term_printf(" [not inserted]");
902 void bdrv_get_backing_filename(BlockDriverState
*bs
,
903 char *filename
, int filename_size
)
905 if (!bs
->backing_hd
) {
906 pstrcpy(filename
, filename_size
, "");
908 pstrcpy(filename
, filename_size
, bs
->backing_file
);
912 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
913 const uint8_t *buf
, int nb_sectors
)
915 BlockDriver
*drv
= bs
->drv
;
918 if (!drv
->bdrv_write_compressed
)
920 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
923 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
925 BlockDriver
*drv
= bs
->drv
;
928 if (!drv
->bdrv_get_info
)
930 memset(bdi
, 0, sizeof(*bdi
));
931 return drv
->bdrv_get_info(bs
, bdi
);
934 /**************************************************************/
935 /* handling of snapshots */
937 int bdrv_snapshot_create(BlockDriverState
*bs
,
938 QEMUSnapshotInfo
*sn_info
)
940 BlockDriver
*drv
= bs
->drv
;
943 if (!drv
->bdrv_snapshot_create
)
945 return drv
->bdrv_snapshot_create(bs
, sn_info
);
948 int bdrv_snapshot_goto(BlockDriverState
*bs
,
949 const char *snapshot_id
)
951 BlockDriver
*drv
= bs
->drv
;
954 if (!drv
->bdrv_snapshot_goto
)
956 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
959 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
961 BlockDriver
*drv
= bs
->drv
;
964 if (!drv
->bdrv_snapshot_delete
)
966 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
969 int bdrv_snapshot_list(BlockDriverState
*bs
,
970 QEMUSnapshotInfo
**psn_info
)
972 BlockDriver
*drv
= bs
->drv
;
975 if (!drv
->bdrv_snapshot_list
)
977 return drv
->bdrv_snapshot_list(bs
, psn_info
);
980 #define NB_SUFFIXES 4
982 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
984 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
989 snprintf(buf
, buf_size
, "%" PRId64
, size
);
992 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
993 if (size
< (10 * base
)) {
994 snprintf(buf
, buf_size
, "%0.1f%c",
998 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
999 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1000 ((size
+ (base
>> 1)) / base
),
1010 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1012 char buf1
[128], date_buf
[128], clock_buf
[128];
1022 snprintf(buf
, buf_size
,
1023 "%-10s%-20s%7s%20s%15s",
1024 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1028 ptm
= localtime(&ti
);
1029 strftime(date_buf
, sizeof(date_buf
),
1030 "%Y-%m-%d %H:%M:%S", ptm
);
1032 localtime_r(&ti
, &tm
);
1033 strftime(date_buf
, sizeof(date_buf
),
1034 "%Y-%m-%d %H:%M:%S", &tm
);
1036 secs
= sn
->vm_clock_nsec
/ 1000000000;
1037 snprintf(clock_buf
, sizeof(clock_buf
),
1038 "%02d:%02d:%02d.%03d",
1040 (int)((secs
/ 60) % 60),
1042 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1043 snprintf(buf
, buf_size
,
1044 "%-10s%-20s%7s%20s%15s",
1045 sn
->id_str
, sn
->name
,
1046 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1054 /**************************************************************/
1057 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1058 uint8_t *buf
, int nb_sectors
,
1059 BlockDriverCompletionFunc
*cb
, void *opaque
)
1061 BlockDriver
*drv
= bs
->drv
;
1066 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1067 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1068 memcpy(buf
, bs
->boot_sector_data
, 512);
1074 return drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1077 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1078 const uint8_t *buf
, int nb_sectors
,
1079 BlockDriverCompletionFunc
*cb
, void *opaque
)
1081 BlockDriver
*drv
= bs
->drv
;
1087 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1088 memcpy(bs
->boot_sector_data
, buf
, 512);
1091 return drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1094 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1096 BlockDriver
*drv
= acb
->bs
->drv
;
1098 drv
->bdrv_aio_cancel(acb
);
1102 /**************************************************************/
1103 /* async block device emulation */
1106 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1107 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1108 BlockDriverCompletionFunc
*cb
, void *opaque
)
1111 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1116 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1117 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1118 BlockDriverCompletionFunc
*cb
, void *opaque
)
1121 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1126 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
)
1130 static void bdrv_aio_bh_cb(void *opaque
)
1132 BlockDriverAIOCBSync
*acb
= opaque
;
1133 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1134 qemu_aio_release(acb
);
1137 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1138 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1139 BlockDriverCompletionFunc
*cb
, void *opaque
)
1141 BlockDriverAIOCBSync
*acb
;
1144 acb
= qemu_aio_get(bs
, cb
, opaque
);
1146 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1147 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1149 qemu_bh_schedule(acb
->bh
);
1150 return &acb
->common
;
1153 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1154 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1155 BlockDriverCompletionFunc
*cb
, void *opaque
)
1157 BlockDriverAIOCBSync
*acb
;
1160 acb
= qemu_aio_get(bs
, cb
, opaque
);
1162 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1163 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1165 qemu_bh_schedule(acb
->bh
);
1166 return &acb
->common
;
1169 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1171 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1172 qemu_bh_cancel(acb
->bh
);
1173 qemu_aio_release(acb
);
1175 #endif /* !QEMU_TOOL */
1177 /**************************************************************/
1178 /* sync block device emulation */
1180 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1182 *(int *)opaque
= ret
;
1185 #define NOT_DONE 0x7fffffff
1187 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1188 uint8_t *buf
, int nb_sectors
)
1191 BlockDriverAIOCB
*acb
;
1193 async_ret
= NOT_DONE
;
1194 qemu_aio_wait_start();
1195 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1196 bdrv_rw_em_cb
, &async_ret
);
1198 qemu_aio_wait_end();
1201 while (async_ret
== NOT_DONE
) {
1204 qemu_aio_wait_end();
1208 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1209 const uint8_t *buf
, int nb_sectors
)
1212 BlockDriverAIOCB
*acb
;
1214 async_ret
= NOT_DONE
;
1215 qemu_aio_wait_start();
1216 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1217 bdrv_rw_em_cb
, &async_ret
);
1219 qemu_aio_wait_end();
1222 while (async_ret
== NOT_DONE
) {
1225 qemu_aio_wait_end();
1229 void bdrv_init(void)
1231 bdrv_register(&bdrv_raw
);
1232 bdrv_register(&bdrv_host_device
);
1234 bdrv_register(&bdrv_cow
);
1236 bdrv_register(&bdrv_qcow
);
1237 bdrv_register(&bdrv_vmdk
);
1238 bdrv_register(&bdrv_cloop
);
1239 bdrv_register(&bdrv_dmg
);
1240 bdrv_register(&bdrv_bochs
);
1241 bdrv_register(&bdrv_vpc
);
1242 bdrv_register(&bdrv_vvfat
);
1243 bdrv_register(&bdrv_qcow2
);
1244 bdrv_register(&bdrv_parallels
);
1247 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1251 BlockDriverAIOCB
*acb
;
1254 if (drv
->free_aiocb
) {
1255 acb
= drv
->free_aiocb
;
1256 drv
->free_aiocb
= acb
->next
;
1258 acb
= qemu_mallocz(drv
->aiocb_size
);
1264 acb
->opaque
= opaque
;
1268 void qemu_aio_release(void *p
)
1270 BlockDriverAIOCB
*acb
= p
;
1271 BlockDriver
*drv
= acb
->bs
->drv
;
1272 acb
->next
= drv
->free_aiocb
;
1273 drv
->free_aiocb
= acb
;
1276 /**************************************************************/
1277 /* removable device support */
1280 * Return TRUE if the media is present
1282 int bdrv_is_inserted(BlockDriverState
*bs
)
1284 BlockDriver
*drv
= bs
->drv
;
1288 if (!drv
->bdrv_is_inserted
)
1290 ret
= drv
->bdrv_is_inserted(bs
);
1295 * Return TRUE if the media changed since the last call to this
1296 * function. It is currently only used for floppy disks
1298 int bdrv_media_changed(BlockDriverState
*bs
)
1300 BlockDriver
*drv
= bs
->drv
;
1303 if (!drv
|| !drv
->bdrv_media_changed
)
1306 ret
= drv
->bdrv_media_changed(bs
);
1307 if (ret
== -ENOTSUP
)
1308 ret
= bs
->media_changed
;
1309 bs
->media_changed
= 0;
1314 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1316 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1318 BlockDriver
*drv
= bs
->drv
;
1321 if (!drv
|| !drv
->bdrv_eject
) {
1324 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1326 if (ret
== -ENOTSUP
) {
1332 int bdrv_is_locked(BlockDriverState
*bs
)
1338 * Lock or unlock the media (if it is locked, the user won't be able
1339 * to eject it manually).
1341 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1343 BlockDriver
*drv
= bs
->drv
;
1345 bs
->locked
= locked
;
1346 if (drv
&& drv
->bdrv_set_locked
) {
1347 drv
->bdrv_set_locked(bs
, locked
);