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
)
194 /* XXX: race condition possible */
195 pstrcpy(filename
, size
, "/tmp/vl.XXXXXX");
196 fd
= mkstemp(filename
);
202 static int is_windows_drive_prefix(const char *filename
)
204 return (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
205 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
209 static int is_windows_drive(const char *filename
)
211 if (is_windows_drive_prefix(filename
) &&
214 if (strstart(filename
, "\\\\.\\", NULL
) ||
215 strstart(filename
, "//./", NULL
))
221 static BlockDriver
*find_protocol(const char *filename
)
229 if (is_windows_drive(filename
) ||
230 is_windows_drive_prefix(filename
))
233 p
= strchr(filename
, ':');
237 if (len
> sizeof(protocol
) - 1)
238 len
= sizeof(protocol
) - 1;
239 memcpy(protocol
, filename
, len
);
240 protocol
[len
] = '\0';
241 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
242 if (drv1
->protocol_name
&&
243 !strcmp(drv1
->protocol_name
, protocol
))
249 /* XXX: force raw format if block or character device ? It would
250 simplify the BSD case */
251 static BlockDriver
*find_image_format(const char *filename
)
253 int ret
, score
, score_max
;
254 BlockDriver
*drv1
, *drv
;
256 BlockDriverState
*bs
;
258 /* detect host devices. By convention, /dev/cdrom[N] is always
259 recognized as a host CDROM */
260 if (strstart(filename
, "/dev/cdrom", NULL
))
261 return &bdrv_host_device
;
263 if (is_windows_drive(filename
))
264 return &bdrv_host_device
;
268 if (stat(filename
, &st
) >= 0 &&
269 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
270 return &bdrv_host_device
;
275 drv
= find_protocol(filename
);
276 /* no need to test disk image formats for vvfat */
277 if (drv
== &bdrv_vvfat
)
280 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
283 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
290 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
291 if (drv1
->bdrv_probe
) {
292 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
293 if (score
> score_max
) {
302 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
304 BlockDriverState
*bs
;
310 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
319 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
321 return bdrv_open2(bs
, filename
, flags
, NULL
);
324 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
328 char tmp_filename
[PATH_MAX
];
329 char backing_filename
[PATH_MAX
];
332 bs
->is_temporary
= 0;
335 if (flags
& BDRV_O_SNAPSHOT
) {
336 BlockDriverState
*bs1
;
339 /* if snapshot, we create a temporary backing file and open it
340 instead of opening 'filename' directly */
342 /* if there is a backing file, use it */
347 if (bdrv_open(bs1
, filename
, 0) < 0) {
351 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
354 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
355 realpath(filename
, backing_filename
);
356 if (bdrv_create(&bdrv_qcow2
, tmp_filename
,
357 total_size
, backing_filename
, 0) < 0) {
360 filename
= tmp_filename
;
361 bs
->is_temporary
= 1;
364 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
365 if (flags
& BDRV_O_FILE
) {
366 drv
= find_protocol(filename
);
371 drv
= find_image_format(filename
);
377 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
378 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
380 /* Note: for compatibility, we open disk image files as RDWR, and
381 RDONLY as fallback */
382 if (!(flags
& BDRV_O_FILE
))
383 open_flags
= BDRV_O_RDWR
;
385 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
386 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
387 if (ret
== -EACCES
&& !(flags
& BDRV_O_FILE
)) {
388 ret
= drv
->bdrv_open(bs
, filename
, BDRV_O_RDONLY
);
392 qemu_free(bs
->opaque
);
397 if (drv
->bdrv_getlength
) {
398 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
401 if (bs
->is_temporary
) {
405 if (bs
->backing_file
[0] != '\0') {
406 /* if there is a backing file, use it */
407 bs
->backing_hd
= bdrv_new("");
408 if (!bs
->backing_hd
) {
413 path_combine(backing_filename
, sizeof(backing_filename
),
414 filename
, bs
->backing_file
);
415 if (bdrv_open(bs
->backing_hd
, backing_filename
, 0) < 0)
419 /* call the change callback */
420 bs
->media_changed
= 1;
422 bs
->change_cb(bs
->change_opaque
);
427 void bdrv_close(BlockDriverState
*bs
)
431 bdrv_delete(bs
->backing_hd
);
432 bs
->drv
->bdrv_close(bs
);
433 qemu_free(bs
->opaque
);
435 if (bs
->is_temporary
) {
436 unlink(bs
->filename
);
442 /* call the change callback */
443 bs
->media_changed
= 1;
445 bs
->change_cb(bs
->change_opaque
);
449 void bdrv_delete(BlockDriverState
*bs
)
451 /* XXX: remove the driver list */
456 /* commit COW file into the raw image */
457 int bdrv_commit(BlockDriverState
*bs
)
459 BlockDriver
*drv
= bs
->drv
;
460 int64_t i
, total_sectors
;
462 unsigned char sector
[512];
471 if (!bs
->backing_hd
) {
475 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
476 for (i
= 0; i
< total_sectors
;) {
477 if (drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
478 for(j
= 0; j
< n
; j
++) {
479 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
483 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
493 if (drv
->bdrv_make_empty
)
494 return drv
->bdrv_make_empty(bs
);
499 /* return < 0 if error. See bdrv_write() for the return codes */
500 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
501 uint8_t *buf
, int nb_sectors
)
503 BlockDriver
*drv
= bs
->drv
;
508 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
509 memcpy(buf
, bs
->boot_sector_data
, 512);
516 if (drv
->bdrv_pread
) {
518 len
= nb_sectors
* 512;
519 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
525 bs
->rd_bytes
+= (unsigned) len
;
530 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
534 /* Return < 0 if error. Important errors are:
535 -EIO generic I/O error (may happen for all errors)
536 -ENOMEDIUM No media inserted.
537 -EINVAL Invalid sector number or nb_sectors
538 -EACCES Trying to write a read-only device
540 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
541 const uint8_t *buf
, int nb_sectors
)
543 BlockDriver
*drv
= bs
->drv
;
548 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
549 memcpy(bs
->boot_sector_data
, buf
, 512);
551 if (drv
->bdrv_pwrite
) {
553 len
= nb_sectors
* 512;
554 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
);
560 bs
->wr_bytes
+= (unsigned) len
;
565 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
569 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
570 uint8_t *buf
, int count1
)
572 uint8_t tmp_buf
[SECTOR_SIZE
];
573 int len
, nb_sectors
, count
;
577 /* first read to align to sector start */
578 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
581 sector_num
= offset
>> SECTOR_BITS
;
583 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
585 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
593 /* read the sectors "in place" */
594 nb_sectors
= count
>> SECTOR_BITS
;
595 if (nb_sectors
> 0) {
596 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
598 sector_num
+= nb_sectors
;
599 len
= nb_sectors
<< SECTOR_BITS
;
604 /* add data from the last sector */
606 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
608 memcpy(buf
, tmp_buf
, count
);
613 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
614 const uint8_t *buf
, int count1
)
616 uint8_t tmp_buf
[SECTOR_SIZE
];
617 int len
, nb_sectors
, count
;
621 /* first write to align to sector start */
622 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
625 sector_num
= offset
>> SECTOR_BITS
;
627 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
629 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
630 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
639 /* write the sectors "in place" */
640 nb_sectors
= count
>> SECTOR_BITS
;
641 if (nb_sectors
> 0) {
642 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
644 sector_num
+= nb_sectors
;
645 len
= nb_sectors
<< SECTOR_BITS
;
650 /* add data from the last sector */
652 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
654 memcpy(tmp_buf
, buf
, count
);
655 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
662 * Read with byte offsets (needed only for file protocols)
664 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
665 void *buf1
, int count1
)
667 BlockDriver
*drv
= bs
->drv
;
671 if (!drv
->bdrv_pread
)
672 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
673 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
677 * Write with byte offsets (needed only for file protocols)
679 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
680 const void *buf1
, int count1
)
682 BlockDriver
*drv
= bs
->drv
;
686 if (!drv
->bdrv_pwrite
)
687 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
688 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
692 * Truncate file to 'offset' bytes (needed only for file protocols)
694 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
696 BlockDriver
*drv
= bs
->drv
;
699 if (!drv
->bdrv_truncate
)
701 return drv
->bdrv_truncate(bs
, offset
);
705 * Length of a file in bytes. Return < 0 if error or unknown.
707 int64_t bdrv_getlength(BlockDriverState
*bs
)
709 BlockDriver
*drv
= bs
->drv
;
712 if (!drv
->bdrv_getlength
) {
714 return bs
->total_sectors
* SECTOR_SIZE
;
716 return drv
->bdrv_getlength(bs
);
719 /* return 0 as number of sectors if no device present or error */
720 void bdrv_get_geometry(BlockDriverState
*bs
, int64_t *nb_sectors_ptr
)
723 length
= bdrv_getlength(bs
);
727 length
= length
>> SECTOR_BITS
;
728 *nb_sectors_ptr
= length
;
731 /* force a given boot sector. */
732 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
)
734 bs
->boot_sector_enabled
= 1;
737 memcpy(bs
->boot_sector_data
, data
, size
);
738 memset(bs
->boot_sector_data
+ size
, 0, 512 - size
);
741 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
742 int cyls
, int heads
, int secs
)
749 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
752 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
753 type
== BDRV_TYPE_FLOPPY
));
756 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
758 bs
->translation
= translation
;
761 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
762 int *pcyls
, int *pheads
, int *psecs
)
769 int bdrv_get_type_hint(BlockDriverState
*bs
)
774 int bdrv_get_translation_hint(BlockDriverState
*bs
)
776 return bs
->translation
;
779 int bdrv_is_removable(BlockDriverState
*bs
)
781 return bs
->removable
;
784 int bdrv_is_read_only(BlockDriverState
*bs
)
786 return bs
->read_only
;
789 /* XXX: no longer used */
790 void bdrv_set_change_cb(BlockDriverState
*bs
,
791 void (*change_cb
)(void *opaque
), void *opaque
)
793 bs
->change_cb
= change_cb
;
794 bs
->change_opaque
= opaque
;
797 int bdrv_is_encrypted(BlockDriverState
*bs
)
799 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
801 return bs
->encrypted
;
804 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
807 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
808 ret
= bdrv_set_key(bs
->backing_hd
, key
);
814 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
816 return bs
->drv
->bdrv_set_key(bs
, key
);
819 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
824 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
828 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
833 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
834 it(opaque
, drv
->format_name
);
838 BlockDriverState
*bdrv_find(const char *name
)
840 BlockDriverState
*bs
;
842 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
843 if (!strcmp(name
, bs
->device_name
))
849 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
851 BlockDriverState
*bs
;
853 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
854 it(opaque
, bs
->device_name
);
858 const char *bdrv_get_device_name(BlockDriverState
*bs
)
860 return bs
->device_name
;
863 void bdrv_flush(BlockDriverState
*bs
)
865 if (bs
->drv
->bdrv_flush
)
866 bs
->drv
->bdrv_flush(bs
);
868 bdrv_flush(bs
->backing_hd
);
874 BlockDriverState
*bs
;
876 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
877 term_printf("%s:", bs
->device_name
);
878 term_printf(" type=");
883 case BDRV_TYPE_CDROM
:
884 term_printf("cdrom");
886 case BDRV_TYPE_FLOPPY
:
887 term_printf("floppy");
890 term_printf(" removable=%d", bs
->removable
);
892 term_printf(" locked=%d", bs
->locked
);
895 term_printf(" file=");
896 term_print_filename(bs
->filename
);
897 if (bs
->backing_file
[0] != '\0') {
898 term_printf(" backing_file=");
899 term_print_filename(bs
->backing_file
);
901 term_printf(" ro=%d", bs
->read_only
);
902 term_printf(" drv=%s", bs
->drv
->format_name
);
904 term_printf(" encrypted");
906 term_printf(" [not inserted]");
912 /* The "info blockstats" command. */
913 void bdrv_info_stats (void)
915 BlockDriverState
*bs
;
917 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
921 " rd_operations=%" PRIu64
922 " wr_operations=%" PRIu64
925 bs
->rd_bytes
, bs
->wr_bytes
,
926 bs
->rd_ops
, bs
->wr_ops
);
931 void bdrv_get_backing_filename(BlockDriverState
*bs
,
932 char *filename
, int filename_size
)
934 if (!bs
->backing_hd
) {
935 pstrcpy(filename
, filename_size
, "");
937 pstrcpy(filename
, filename_size
, bs
->backing_file
);
941 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
942 const uint8_t *buf
, int nb_sectors
)
944 BlockDriver
*drv
= bs
->drv
;
947 if (!drv
->bdrv_write_compressed
)
949 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
952 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
954 BlockDriver
*drv
= bs
->drv
;
957 if (!drv
->bdrv_get_info
)
959 memset(bdi
, 0, sizeof(*bdi
));
960 return drv
->bdrv_get_info(bs
, bdi
);
963 /**************************************************************/
964 /* handling of snapshots */
966 int bdrv_snapshot_create(BlockDriverState
*bs
,
967 QEMUSnapshotInfo
*sn_info
)
969 BlockDriver
*drv
= bs
->drv
;
972 if (!drv
->bdrv_snapshot_create
)
974 return drv
->bdrv_snapshot_create(bs
, sn_info
);
977 int bdrv_snapshot_goto(BlockDriverState
*bs
,
978 const char *snapshot_id
)
980 BlockDriver
*drv
= bs
->drv
;
983 if (!drv
->bdrv_snapshot_goto
)
985 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
988 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
990 BlockDriver
*drv
= bs
->drv
;
993 if (!drv
->bdrv_snapshot_delete
)
995 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
998 int bdrv_snapshot_list(BlockDriverState
*bs
,
999 QEMUSnapshotInfo
**psn_info
)
1001 BlockDriver
*drv
= bs
->drv
;
1004 if (!drv
->bdrv_snapshot_list
)
1006 return drv
->bdrv_snapshot_list(bs
, psn_info
);
1009 #define NB_SUFFIXES 4
1011 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
1013 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
1018 snprintf(buf
, buf_size
, "%" PRId64
, size
);
1021 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
1022 if (size
< (10 * base
)) {
1023 snprintf(buf
, buf_size
, "%0.1f%c",
1024 (double)size
/ base
,
1027 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
1028 snprintf(buf
, buf_size
, "%" PRId64
"%c",
1029 ((size
+ (base
>> 1)) / base
),
1039 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
1041 char buf1
[128], date_buf
[128], clock_buf
[128];
1051 snprintf(buf
, buf_size
,
1052 "%-10s%-20s%7s%20s%15s",
1053 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1057 ptm
= localtime(&ti
);
1058 strftime(date_buf
, sizeof(date_buf
),
1059 "%Y-%m-%d %H:%M:%S", ptm
);
1061 localtime_r(&ti
, &tm
);
1062 strftime(date_buf
, sizeof(date_buf
),
1063 "%Y-%m-%d %H:%M:%S", &tm
);
1065 secs
= sn
->vm_clock_nsec
/ 1000000000;
1066 snprintf(clock_buf
, sizeof(clock_buf
),
1067 "%02d:%02d:%02d.%03d",
1069 (int)((secs
/ 60) % 60),
1071 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
1072 snprintf(buf
, buf_size
,
1073 "%-10s%-20s%7s%20s%15s",
1074 sn
->id_str
, sn
->name
,
1075 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
1083 /**************************************************************/
1086 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
1087 uint8_t *buf
, int nb_sectors
,
1088 BlockDriverCompletionFunc
*cb
, void *opaque
)
1090 BlockDriver
*drv
= bs
->drv
;
1091 BlockDriverAIOCB
*ret
;
1096 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1097 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1098 memcpy(buf
, bs
->boot_sector_data
, 512);
1104 ret
= drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1107 /* Update stats even though technically transfer has not happened. */
1108 bs
->rd_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1115 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1116 const uint8_t *buf
, int nb_sectors
,
1117 BlockDriverCompletionFunc
*cb
, void *opaque
)
1119 BlockDriver
*drv
= bs
->drv
;
1120 BlockDriverAIOCB
*ret
;
1126 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1127 memcpy(bs
->boot_sector_data
, buf
, 512);
1130 ret
= drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1133 /* Update stats even though technically transfer has not happened. */
1134 bs
->wr_bytes
+= (unsigned) nb_sectors
* SECTOR_SIZE
;
1141 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1143 BlockDriver
*drv
= acb
->bs
->drv
;
1145 drv
->bdrv_aio_cancel(acb
);
1149 /**************************************************************/
1150 /* async block device emulation */
1153 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1154 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1155 BlockDriverCompletionFunc
*cb
, void *opaque
)
1158 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1163 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1164 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1165 BlockDriverCompletionFunc
*cb
, void *opaque
)
1168 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1173 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
)
1177 static void bdrv_aio_bh_cb(void *opaque
)
1179 BlockDriverAIOCBSync
*acb
= opaque
;
1180 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1181 qemu_aio_release(acb
);
1184 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1185 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1186 BlockDriverCompletionFunc
*cb
, void *opaque
)
1188 BlockDriverAIOCBSync
*acb
;
1191 acb
= qemu_aio_get(bs
, cb
, opaque
);
1193 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1194 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1196 qemu_bh_schedule(acb
->bh
);
1197 return &acb
->common
;
1200 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1201 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1202 BlockDriverCompletionFunc
*cb
, void *opaque
)
1204 BlockDriverAIOCBSync
*acb
;
1207 acb
= qemu_aio_get(bs
, cb
, opaque
);
1209 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1210 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1212 qemu_bh_schedule(acb
->bh
);
1213 return &acb
->common
;
1216 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1218 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1219 qemu_bh_cancel(acb
->bh
);
1220 qemu_aio_release(acb
);
1222 #endif /* !QEMU_IMG */
1224 /**************************************************************/
1225 /* sync block device emulation */
1227 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1229 *(int *)opaque
= ret
;
1232 #define NOT_DONE 0x7fffffff
1234 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1235 uint8_t *buf
, int nb_sectors
)
1238 BlockDriverAIOCB
*acb
;
1240 async_ret
= NOT_DONE
;
1241 qemu_aio_wait_start();
1242 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1243 bdrv_rw_em_cb
, &async_ret
);
1245 qemu_aio_wait_end();
1248 while (async_ret
== NOT_DONE
) {
1251 qemu_aio_wait_end();
1255 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1256 const uint8_t *buf
, int nb_sectors
)
1259 BlockDriverAIOCB
*acb
;
1261 async_ret
= NOT_DONE
;
1262 qemu_aio_wait_start();
1263 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1264 bdrv_rw_em_cb
, &async_ret
);
1266 qemu_aio_wait_end();
1269 while (async_ret
== NOT_DONE
) {
1272 qemu_aio_wait_end();
1276 void bdrv_init(void)
1278 bdrv_register(&bdrv_raw
);
1279 bdrv_register(&bdrv_host_device
);
1281 bdrv_register(&bdrv_cow
);
1283 bdrv_register(&bdrv_qcow
);
1284 bdrv_register(&bdrv_vmdk
);
1285 bdrv_register(&bdrv_cloop
);
1286 bdrv_register(&bdrv_dmg
);
1287 bdrv_register(&bdrv_bochs
);
1288 bdrv_register(&bdrv_vpc
);
1289 bdrv_register(&bdrv_vvfat
);
1290 bdrv_register(&bdrv_qcow2
);
1291 bdrv_register(&bdrv_parallels
);
1294 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1298 BlockDriverAIOCB
*acb
;
1301 if (drv
->free_aiocb
) {
1302 acb
= drv
->free_aiocb
;
1303 drv
->free_aiocb
= acb
->next
;
1305 acb
= qemu_mallocz(drv
->aiocb_size
);
1311 acb
->opaque
= opaque
;
1315 void qemu_aio_release(void *p
)
1317 BlockDriverAIOCB
*acb
= p
;
1318 BlockDriver
*drv
= acb
->bs
->drv
;
1319 acb
->next
= drv
->free_aiocb
;
1320 drv
->free_aiocb
= acb
;
1323 /**************************************************************/
1324 /* removable device support */
1327 * Return TRUE if the media is present
1329 int bdrv_is_inserted(BlockDriverState
*bs
)
1331 BlockDriver
*drv
= bs
->drv
;
1335 if (!drv
->bdrv_is_inserted
)
1337 ret
= drv
->bdrv_is_inserted(bs
);
1342 * Return TRUE if the media changed since the last call to this
1343 * function. It is currently only used for floppy disks
1345 int bdrv_media_changed(BlockDriverState
*bs
)
1347 BlockDriver
*drv
= bs
->drv
;
1350 if (!drv
|| !drv
->bdrv_media_changed
)
1353 ret
= drv
->bdrv_media_changed(bs
);
1354 if (ret
== -ENOTSUP
)
1355 ret
= bs
->media_changed
;
1356 bs
->media_changed
= 0;
1361 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1363 void bdrv_eject(BlockDriverState
*bs
, int eject_flag
)
1365 BlockDriver
*drv
= bs
->drv
;
1368 if (!drv
|| !drv
->bdrv_eject
) {
1371 ret
= drv
->bdrv_eject(bs
, eject_flag
);
1373 if (ret
== -ENOTSUP
) {
1379 int bdrv_is_locked(BlockDriverState
*bs
)
1385 * Lock or unlock the media (if it is locked, the user won't be able
1386 * to eject it manually).
1388 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
1390 BlockDriver
*drv
= bs
->drv
;
1392 bs
->locked
= locked
;
1393 if (drv
&& drv
->bdrv_set_locked
) {
1394 drv
->bdrv_set_locked(bs
, locked
);