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
;
65 int path_is_absolute(const char *path
)
68 p
= strchr(path
, ':');
73 return (*p
== PATH_SEP
);
76 /* if filename is absolute, just copy it to dest. Otherwise, build a
77 path to it by considering it is relative to base_path. URL are
79 void path_combine(char *dest
, int dest_size
,
80 const char *base_path
,
88 if (path_is_absolute(filename
)) {
89 pstrcpy(dest
, dest_size
, filename
);
91 p
= strchr(base_path
, ':');
96 p1
= strrchr(base_path
, PATH_SEP
);
104 if (len
> dest_size
- 1)
106 memcpy(dest
, base_path
, len
);
108 pstrcat(dest
, dest_size
, filename
);
113 void bdrv_register(BlockDriver
*bdrv
)
115 if (!bdrv
->bdrv_aio_read
) {
116 /* add AIO emulation layer */
117 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
118 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
119 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
120 bdrv
->aiocb_size
= sizeof(BlockDriverAIOCBSync
);
121 } else if (!bdrv
->bdrv_read
&& !bdrv
->bdrv_pread
) {
122 /* add synchronous IO emulation layer */
123 bdrv
->bdrv_read
= bdrv_read_em
;
124 bdrv
->bdrv_write
= bdrv_write_em
;
126 bdrv
->next
= first_drv
;
130 /* create a new block device (by default it is empty) */
131 BlockDriverState
*bdrv_new(const char *device_name
)
133 BlockDriverState
**pbs
, *bs
;
135 bs
= qemu_mallocz(sizeof(BlockDriverState
));
138 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
139 if (device_name
[0] != '\0') {
140 /* insert at the end */
149 BlockDriver
*bdrv_find_format(const char *format_name
)
152 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
153 if (!strcmp(drv1
->format_name
, format_name
))
159 int bdrv_create(BlockDriver
*drv
,
160 const char *filename
, int64_t size_in_sectors
,
161 const char *backing_file
, int flags
)
163 if (!drv
->bdrv_create
)
165 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
169 void get_tmp_filename(char *filename
, int size
)
174 void get_tmp_filename(char *filename
, int size
)
177 /* XXX: race condition possible */
178 pstrcpy(filename
, size
, "/tmp/vl.XXXXXX");
179 fd
= mkstemp(filename
);
185 static int is_windows_drive_prefix(const char *filename
)
187 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
188 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
192 static int is_windows_drive(const char *filename
)
194 if (is_windows_drive_prefix(filename
) &&
197 if (strstart(filename
, "\\\\.\\", NULL
) ||
198 strstart(filename
, "//./", NULL
))
204 static BlockDriver
*find_protocol(const char *filename
)
212 if (is_windows_drive(filename
) ||
213 is_windows_drive_prefix(filename
))
216 p
= strchr(filename
, ':');
220 if (len
> sizeof(protocol
) - 1)
221 len
= sizeof(protocol
) - 1;
222 memcpy(protocol
, filename
, len
);
223 protocol
[len
] = '\0';
224 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
225 if (drv1
->protocol_name
&&
226 !strcmp(drv1
->protocol_name
, protocol
))
232 /* XXX: force raw format if block or character device ? It would
233 simplify the BSD case */
234 static BlockDriver
*find_image_format(const char *filename
)
236 int ret
, score
, score_max
;
237 BlockDriver
*drv1
, *drv
;
239 BlockDriverState
*bs
;
241 /* detect host devices. By convention, /dev/cdrom[N] is always
242 recognized as a host CDROM */
243 if (strstart(filename
, "/dev/cdrom", NULL
))
244 return &bdrv_host_device
;
246 if (is_windows_drive(filename
))
247 return &bdrv_host_device
;
251 if (stat(filename
, &st
) >= 0 &&
252 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
253 return &bdrv_host_device
;
258 drv
= find_protocol(filename
);
259 /* no need to test disk image formats for vvfat */
260 if (drv
== &bdrv_vvfat
)
263 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
266 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
273 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
274 if (drv1
->bdrv_probe
) {
275 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
276 if (score
> score_max
) {
285 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
287 BlockDriverState
*bs
;
293 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
302 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
304 return bdrv_open2(bs
, filename
, flags
, NULL
);
307 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
311 char tmp_filename
[1024];
312 char backing_filename
[1024];
315 bs
->is_temporary
= 0;
318 if (flags
& BDRV_O_SNAPSHOT
) {
319 BlockDriverState
*bs1
;
322 /* if snapshot, we create a temporary backing file and open it
323 instead of opening 'filename' directly */
325 /* if there is a backing file, use it */
330 if (bdrv_open(bs1
, filename
, 0) < 0) {
334 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
337 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
338 realpath(filename
, backing_filename
);
339 if (bdrv_create(&bdrv_qcow2
, tmp_filename
,
340 total_size
, backing_filename
, 0) < 0) {
343 filename
= tmp_filename
;
344 bs
->is_temporary
= 1;
347 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
348 if (flags
& BDRV_O_FILE
) {
349 drv
= find_protocol(filename
);
354 drv
= find_image_format(filename
);
360 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
361 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
363 /* Note: for compatibility, we open disk image files as RDWR, and
364 RDONLY as fallback */
365 if (!(flags
& BDRV_O_FILE
))
366 open_flags
= BDRV_O_RDWR
;
368 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
369 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
370 if (ret
== -EACCES
&& !(flags
& BDRV_O_FILE
)) {
371 ret
= drv
->bdrv_open(bs
, filename
, BDRV_O_RDONLY
);
375 qemu_free(bs
->opaque
);
380 if (drv
->bdrv_getlength
) {
381 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
384 if (bs
->is_temporary
) {
388 if (bs
->backing_file
[0] != '\0') {
389 /* if there is a backing file, use it */
390 bs
->backing_hd
= bdrv_new("");
391 if (!bs
->backing_hd
) {
396 path_combine(backing_filename
, sizeof(backing_filename
),
397 filename
, bs
->backing_file
);
398 if (bdrv_open(bs
->backing_hd
, backing_filename
, 0) < 0)
402 /* call the change callback */
403 bs
->media_changed
= 1;
405 bs
->change_cb(bs
->change_opaque
);
410 void bdrv_close(BlockDriverState
*bs
)
414 bdrv_delete(bs
->backing_hd
);
415 bs
->drv
->bdrv_close(bs
);
416 qemu_free(bs
->opaque
);
418 if (bs
->is_temporary
) {
419 unlink(bs
->filename
);
425 /* call the change callback */
426 bs
->media_changed
= 1;
428 bs
->change_cb(bs
->change_opaque
);
432 void bdrv_delete(BlockDriverState
*bs
)
434 /* XXX: remove the driver list */
439 /* commit COW file into the raw image */
440 int bdrv_commit(BlockDriverState
*bs
)
442 BlockDriver
*drv
= bs
->drv
;
443 int64_t i
, total_sectors
;
445 unsigned char sector
[512];
454 if (!bs
->backing_hd
) {
458 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
459 for (i
= 0; i
< total_sectors
;) {
460 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
461 for(j
= 0; j
< n
; j
++) {
462 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
466 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
476 if (drv
->bdrv_make_empty
)
477 return drv
->bdrv_make_empty(bs
);
482 /* return < 0 if error. See bdrv_write() for the return codes */
483 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
484 uint8_t *buf
, int nb_sectors
)
486 BlockDriver
*drv
= bs
->drv
;
491 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
492 memcpy(buf
, bs
->boot_sector_data
, 512);
499 if (drv
->bdrv_pread
) {
501 len
= nb_sectors
* 512;
502 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
510 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
514 /* Return < 0 if error. Important errors are:
515 -EIO generic I/O error (may happen for all errors)
516 -ENOMEDIUM No media inserted.
517 -EINVAL Invalid sector number or nb_sectors
518 -EACCES Trying to write a read-only device
520 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
521 const 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(bs
->boot_sector_data
, buf
, 512);
531 if (drv
->bdrv_pwrite
) {
533 len
= nb_sectors
* 512;
534 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
);
542 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
546 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
547 uint8_t *buf
, int count1
)
549 uint8_t tmp_buf
[SECTOR_SIZE
];
550 int len
, nb_sectors
, count
;
554 /* first read to align to sector start */
555 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
558 sector_num
= offset
>> SECTOR_BITS
;
560 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
562 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
570 /* read the sectors "in place" */
571 nb_sectors
= count
>> SECTOR_BITS
;
572 if (nb_sectors
> 0) {
573 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
575 sector_num
+= nb_sectors
;
576 len
= nb_sectors
<< SECTOR_BITS
;
581 /* add data from the last sector */
583 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
585 memcpy(buf
, tmp_buf
, count
);
590 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
591 const uint8_t *buf
, int count1
)
593 uint8_t tmp_buf
[SECTOR_SIZE
];
594 int len
, nb_sectors
, count
;
598 /* first write to align to sector start */
599 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
602 sector_num
= offset
>> SECTOR_BITS
;
604 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
606 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
607 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
616 /* write the sectors "in place" */
617 nb_sectors
= count
>> SECTOR_BITS
;
618 if (nb_sectors
> 0) {
619 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
621 sector_num
+= nb_sectors
;
622 len
= nb_sectors
<< SECTOR_BITS
;
627 /* add data from the last sector */
629 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
631 memcpy(tmp_buf
, buf
, count
);
632 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
639 * Read with byte offsets (needed only for file protocols)
641 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
642 void *buf1
, int count1
)
644 BlockDriver
*drv
= bs
->drv
;
648 if (!drv
->bdrv_pread
)
649 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
650 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
654 * Write with byte offsets (needed only for file protocols)
656 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
657 const void *buf1
, int count1
)
659 BlockDriver
*drv
= bs
->drv
;
663 if (!drv
->bdrv_pwrite
)
664 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
665 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
669 * Truncate file to 'offset' bytes (needed only for file protocols)
671 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
673 BlockDriver
*drv
= bs
->drv
;
676 if (!drv
->bdrv_truncate
)
678 return drv
->bdrv_truncate(bs
, offset
);
682 * Length of a file in bytes. Return < 0 if error or unknown.
684 int64_t bdrv_getlength(BlockDriverState
*bs
)
686 BlockDriver
*drv
= bs
->drv
;
689 if (!drv
->bdrv_getlength
) {
691 return bs
->total_sectors
* SECTOR_SIZE
;
693 return drv
->bdrv_getlength(bs
);
696 /* return 0 as number of sectors if no device present or error */
697 void bdrv_get_geometry(BlockDriverState
*bs
, int64_t *nb_sectors_ptr
)
700 length
= bdrv_getlength(bs
);
704 length
= length
>> SECTOR_BITS
;
705 *nb_sectors_ptr
= length
;
708 /* force a given boot sector. */
709 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
)
711 bs
->boot_sector_enabled
= 1;
714 memcpy(bs
->boot_sector_data
, data
, size
);
715 memset(bs
->boot_sector_data
+ size
, 0, 512 - size
);
718 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
719 int cyls
, int heads
, int secs
)
726 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
729 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
730 type
== BDRV_TYPE_FLOPPY
));
733 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
735 bs
->translation
= translation
;
738 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
739 int *pcyls
, int *pheads
, int *psecs
)
746 int bdrv_get_type_hint(BlockDriverState
*bs
)
751 int bdrv_get_translation_hint(BlockDriverState
*bs
)
753 return bs
->translation
;
756 int bdrv_is_removable(BlockDriverState
*bs
)
758 return bs
->removable
;
761 int bdrv_is_read_only(BlockDriverState
*bs
)
763 return bs
->read_only
;
766 /* XXX: no longer used */
767 void bdrv_set_change_cb(BlockDriverState
*bs
,
768 void (*change_cb
)(void *opaque
), void *opaque
)
770 bs
->change_cb
= change_cb
;
771 bs
->change_opaque
= opaque
;
774 int bdrv_is_encrypted(BlockDriverState
*bs
)
776 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
778 return bs
->encrypted
;
781 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
784 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
785 ret
= bdrv_set_key(bs
->backing_hd
, key
);
791 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
793 return bs
->drv
->bdrv_set_key(bs
, key
);
796 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
801 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
805 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
810 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
811 it(opaque
, drv
->format_name
);
815 BlockDriverState
*bdrv_find(const char *name
)
817 BlockDriverState
*bs
;
819 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
820 if (!strcmp(name
, bs
->device_name
))
826 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
828 BlockDriverState
*bs
;
830 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
831 it(opaque
, bs
->device_name
);
835 const char *bdrv_get_device_name(BlockDriverState
*bs
)
837 return bs
->device_name
;
840 void bdrv_flush(BlockDriverState
*bs
)
842 if (bs
->drv
->bdrv_flush
)
843 bs
->drv
->bdrv_flush(bs
);
845 bdrv_flush(bs
->backing_hd
);
850 BlockDriverState
*bs
;
852 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
853 term_printf("%s:", bs
->device_name
);
854 term_printf(" type=");
859 case BDRV_TYPE_CDROM
:
860 term_printf("cdrom");
862 case BDRV_TYPE_FLOPPY
:
863 term_printf("floppy");
866 term_printf(" removable=%d", bs
->removable
);
868 term_printf(" locked=%d", bs
->locked
);
871 term_printf(" file=");
872 term_print_filename(bs
->filename
);
873 if (bs
->backing_file
[0] != '\0') {
874 term_printf(" backing_file=");
875 term_print_filename(bs
->backing_file
);
877 term_printf(" ro=%d", bs
->read_only
);
878 term_printf(" drv=%s", bs
->drv
->format_name
);
880 term_printf(" encrypted");
882 term_printf(" [not inserted]");
888 void bdrv_get_backing_filename(BlockDriverState
*bs
,
889 char *filename
, int filename_size
)
891 if (!bs
->backing_hd
) {
892 pstrcpy(filename
, filename_size
, "");
894 pstrcpy(filename
, filename_size
, bs
->backing_file
);
898 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
899 const uint8_t *buf
, int nb_sectors
)
901 BlockDriver
*drv
= bs
->drv
;
904 if (!drv
->bdrv_write_compressed
)
906 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
909 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
911 BlockDriver
*drv
= bs
->drv
;
914 if (!drv
->bdrv_get_info
)
916 memset(bdi
, 0, sizeof(*bdi
));
917 return drv
->bdrv_get_info(bs
, bdi
);
920 /**************************************************************/
921 /* handling of snapshots */
923 int bdrv_snapshot_create(BlockDriverState
*bs
,
924 QEMUSnapshotInfo
*sn_info
)
926 BlockDriver
*drv
= bs
->drv
;
929 if (!drv
->bdrv_snapshot_create
)
931 return drv
->bdrv_snapshot_create(bs
, sn_info
);
934 int bdrv_snapshot_goto(BlockDriverState
*bs
,
935 const char *snapshot_id
)
937 BlockDriver
*drv
= bs
->drv
;
940 if (!drv
->bdrv_snapshot_goto
)
942 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
945 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
947 BlockDriver
*drv
= bs
->drv
;
950 if (!drv
->bdrv_snapshot_delete
)
952 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
955 int bdrv_snapshot_list(BlockDriverState
*bs
,
956 QEMUSnapshotInfo
**psn_info
)
958 BlockDriver
*drv
= bs
->drv
;
961 if (!drv
->bdrv_snapshot_list
)
963 return drv
->bdrv_snapshot_list(bs
, psn_info
);
966 #define NB_SUFFIXES 4
968 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
970 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
975 snprintf(buf
, buf_size
, "%" PRId64
, size
);
978 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
979 if (size
< (10 * base
)) {
980 snprintf(buf
, buf_size
, "%0.1f%c",
984 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
985 snprintf(buf
, buf_size
, "%" PRId64
"%c",
986 ((size
+ (base
>> 1)) / base
),
996 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
998 char buf1
[128], date_buf
[128], clock_buf
[128];
1004 snprintf(buf
, buf_size
,
1005 "%-10s%-20s%7s%20s%15s",
1006 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1010 localtime_r(&ti
, &tm
);
1012 strftime(date_buf
, sizeof(date_buf
),
1013 "%Y-%m-%d %H:%M:%S", &tm
);
1014 secs
= sn
->vm_clock_nsec
/ 1000000000;
1015 snprintf(clock_buf
, sizeof(clock_buf
),
1016 "%02d:%02d:%02d.%03d",
1018 (int)((secs
/ 60) % 60),
1020 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1021 snprintf(buf
, buf_size
,
1022 "%-10s%-20s%7s%20s%15s",
1023 sn
->id_str
, sn
->name
,
1024 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1032 /**************************************************************/
1035 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1036 uint8_t *buf
, int nb_sectors
,
1037 BlockDriverCompletionFunc
*cb
, void *opaque
)
1039 BlockDriver
*drv
= bs
->drv
;
1044 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1045 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1046 memcpy(buf
, bs
->boot_sector_data
, 512);
1052 return drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1055 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1056 const uint8_t *buf
, int nb_sectors
,
1057 BlockDriverCompletionFunc
*cb
, void *opaque
)
1059 BlockDriver
*drv
= bs
->drv
;
1065 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1066 memcpy(bs
->boot_sector_data
, buf
, 512);
1069 return drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1072 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1074 BlockDriver
*drv
= acb
->bs
->drv
;
1076 drv
->bdrv_aio_cancel(acb
);
1080 /**************************************************************/
1081 /* async block device emulation */
1084 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1085 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1086 BlockDriverCompletionFunc
*cb
, void *opaque
)
1089 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1094 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1095 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1096 BlockDriverCompletionFunc
*cb
, void *opaque
)
1099 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1104 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
)
1108 static void bdrv_aio_bh_cb(void *opaque
)
1110 BlockDriverAIOCBSync
*acb
= opaque
;
1111 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1112 qemu_aio_release(acb
);
1115 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1116 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1117 BlockDriverCompletionFunc
*cb
, void *opaque
)
1119 BlockDriverAIOCBSync
*acb
;
1122 acb
= qemu_aio_get(bs
, cb
, opaque
);
1124 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1125 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1127 qemu_bh_schedule(acb
->bh
);
1128 return &acb
->common
;
1131 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1132 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1133 BlockDriverCompletionFunc
*cb
, void *opaque
)
1135 BlockDriverAIOCBSync
*acb
;
1138 acb
= qemu_aio_get(bs
, cb
, opaque
);
1140 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1141 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1143 qemu_bh_schedule(acb
->bh
);
1144 return &acb
->common
;
1147 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1149 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1150 qemu_bh_cancel(acb
->bh
);
1151 qemu_aio_release(acb
);
1153 #endif /* !QEMU_TOOL */
1155 /**************************************************************/
1156 /* sync block device emulation */
1158 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1160 *(int *)opaque
= ret
;
1163 #define NOT_DONE 0x7fffffff
1165 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1166 uint8_t *buf
, int nb_sectors
)
1169 BlockDriverAIOCB
*acb
;
1171 async_ret
= NOT_DONE
;
1172 qemu_aio_wait_start();
1173 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1174 bdrv_rw_em_cb
, &async_ret
);
1176 qemu_aio_wait_end();
1179 while (async_ret
== NOT_DONE
) {
1182 qemu_aio_wait_end();
1186 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1187 const uint8_t *buf
, int nb_sectors
)
1190 BlockDriverAIOCB
*acb
;
1192 async_ret
= NOT_DONE
;
1193 qemu_aio_wait_start();
1194 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1195 bdrv_rw_em_cb
, &async_ret
);
1197 qemu_aio_wait_end();
1200 while (async_ret
== NOT_DONE
) {
1203 qemu_aio_wait_end();
1207 void bdrv_init(void)
1209 bdrv_register(&bdrv_raw
);
1210 bdrv_register(&bdrv_host_device
);
1212 bdrv_register(&bdrv_cow
);
1214 bdrv_register(&bdrv_qcow
);
1215 bdrv_register(&bdrv_vmdk
);
1216 bdrv_register(&bdrv_cloop
);
1217 bdrv_register(&bdrv_dmg
);
1218 bdrv_register(&bdrv_bochs
);
1219 bdrv_register(&bdrv_vpc
);
1220 bdrv_register(&bdrv_vvfat
);
1221 bdrv_register(&bdrv_qcow2
);
1224 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1228 BlockDriverAIOCB
*acb
;
1231 if (drv
->free_aiocb
) {
1232 acb
= drv
->free_aiocb
;
1233 drv
->free_aiocb
= acb
->next
;
1235 acb
= qemu_mallocz(drv
->aiocb_size
);
1241 acb
->opaque
= opaque
;
1245 void qemu_aio_release(void *p
)
1247 BlockDriverAIOCB
*acb
= p
;
1248 BlockDriver
*drv
= acb
->bs
->drv
;
1249 acb
->next
= drv
->free_aiocb
;
1250 drv
->free_aiocb
= acb
;
1253 /**************************************************************/
1254 /* removable device support */
1257 * Return TRUE if the media is present
1259 int bdrv_is_inserted(BlockDriverState
*bs
)
1261 BlockDriver
*drv
= bs
->drv
;
1265 if (!drv
->bdrv_is_inserted
)
1267 ret
= drv
->bdrv_is_inserted(bs
);
1272 * Return TRUE if the media changed since the last call to this
1273 * function. It is currently only used for floppy disks
1275 int bdrv_media_changed(BlockDriverState
*bs
)
1277 BlockDriver
*drv
= bs
->drv
;
1280 if (!drv
|| !drv
->bdrv_media_changed
)
1283 ret
= drv
->bdrv_media_changed(bs
);
1284 if (ret
== -ENOTSUP
)
1285 ret
= bs
->media_changed
;
1286 bs
->media_changed
= 0;
1291 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1293 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1295 BlockDriver
*drv
= bs
->drv
;
1298 if (!drv
|| !drv
->bdrv_eject
) {
1301 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1303 if (ret
== -ENOTSUP
) {
1309 int bdrv_is_locked(BlockDriverState
*bs
)
1315 * Lock or unlock the media (if it is locked, the user won't be able
1316 * to eject it manually).
1318 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1320 BlockDriver
*drv
= bs
->drv
;
1322 bs
->locked
= locked
;
1323 if (drv
&& drv
->bdrv_set_locked
) {
1324 drv
->bdrv_set_locked(bs
, locked
);