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 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
39 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
40 BlockDriverCompletionFunc
*cb
, void *opaque
);
41 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
42 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
43 BlockDriverCompletionFunc
*cb
, void *opaque
);
44 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
);
45 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
46 uint8_t *buf
, int nb_sectors
);
47 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
48 const uint8_t *buf
, int nb_sectors
);
50 static BlockDriverState
*bdrv_first
;
51 static BlockDriver
*first_drv
;
59 int path_is_absolute(const char *path
)
62 p
= strchr(path
, ':');
67 return (*p
== PATH_SEP
);
70 /* if filename is absolute, just copy it to dest. Otherwise, build a
71 path to it by considering it is relative to base_path. URL are
73 void path_combine(char *dest
, int dest_size
,
74 const char *base_path
,
82 if (path_is_absolute(filename
)) {
83 pstrcpy(dest
, dest_size
, filename
);
85 p
= strchr(base_path
, ':');
90 p1
= strrchr(base_path
, PATH_SEP
);
98 if (len
> dest_size
- 1)
100 memcpy(dest
, base_path
, len
);
102 pstrcat(dest
, dest_size
, filename
);
107 void bdrv_register(BlockDriver
*bdrv
)
109 if (!bdrv
->bdrv_aio_read
) {
110 /* add AIO emulation layer */
111 bdrv
->bdrv_aio_read
= bdrv_aio_read_em
;
112 bdrv
->bdrv_aio_write
= bdrv_aio_write_em
;
113 bdrv
->bdrv_aio_cancel
= bdrv_aio_cancel_em
;
114 } else if (!bdrv
->bdrv_read
&& !bdrv
->bdrv_pread
) {
115 /* add synchronous IO emulation layer */
116 bdrv
->bdrv_read
= bdrv_read_em
;
117 bdrv
->bdrv_write
= bdrv_write_em
;
119 bdrv
->next
= first_drv
;
123 /* create a new block device (by default it is empty) */
124 BlockDriverState
*bdrv_new(const char *device_name
)
126 BlockDriverState
**pbs
, *bs
;
128 bs
= qemu_mallocz(sizeof(BlockDriverState
));
131 pstrcpy(bs
->device_name
, sizeof(bs
->device_name
), device_name
);
132 if (device_name
[0] != '\0') {
133 /* insert at the end */
142 BlockDriver
*bdrv_find_format(const char *format_name
)
145 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
146 if (!strcmp(drv1
->format_name
, format_name
))
152 int bdrv_create(BlockDriver
*drv
,
153 const char *filename
, int64_t size_in_sectors
,
154 const char *backing_file
, int flags
)
156 if (!drv
->bdrv_create
)
158 return drv
->bdrv_create(filename
, size_in_sectors
, backing_file
, flags
);
162 void get_tmp_filename(char *filename
, int size
)
167 void get_tmp_filename(char *filename
, int size
)
170 /* XXX: race condition possible */
171 pstrcpy(filename
, size
, "/tmp/vl.XXXXXX");
172 fd
= mkstemp(filename
);
177 static BlockDriver
*find_protocol(const char *filename
)
183 p
= strchr(filename
, ':');
187 if (len
> sizeof(protocol
) - 1)
188 len
= sizeof(protocol
) - 1;
191 /* specific win32 case for driver letters */
195 memcpy(protocol
, filename
, len
);
196 protocol
[len
] = '\0';
197 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
198 if (drv1
->protocol_name
&&
199 !strcmp(drv1
->protocol_name
, protocol
))
205 /* XXX: force raw format if block or character device ? It would
206 simplify the BSD case */
207 static BlockDriver
*find_image_format(const char *filename
)
209 int ret
, score
, score_max
;
210 BlockDriver
*drv1
, *drv
;
212 BlockDriverState
*bs
;
214 drv
= find_protocol(filename
);
215 /* no need to test disk image formats for vvfat or host specific
217 if (drv
== &bdrv_vvfat
)
219 if (strstart(filename
, "/dev/", NULL
))
222 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDONLY
);
225 ret
= bdrv_pread(bs
, 0, buf
, sizeof(buf
));
232 for(drv1
= first_drv
; drv1
!= NULL
; drv1
= drv1
->next
) {
233 if (drv1
->bdrv_probe
) {
234 score
= drv1
->bdrv_probe(buf
, ret
, filename
);
235 if (score
> score_max
) {
244 int bdrv_file_open(BlockDriverState
**pbs
, const char *filename
, int flags
)
246 BlockDriverState
*bs
;
252 ret
= bdrv_open2(bs
, filename
, flags
| BDRV_O_FILE
, NULL
);
261 int bdrv_open(BlockDriverState
*bs
, const char *filename
, int flags
)
263 return bdrv_open2(bs
, filename
, flags
, NULL
);
266 int bdrv_open2(BlockDriverState
*bs
, const char *filename
, int flags
,
270 char tmp_filename
[1024];
271 char backing_filename
[1024];
274 bs
->is_temporary
= 0;
277 if (flags
& BDRV_O_SNAPSHOT
) {
278 BlockDriverState
*bs1
;
281 /* if snapshot, we create a temporary backing file and open it
282 instead of opening 'filename' directly */
284 /* if there is a backing file, use it */
289 if (bdrv_open(bs1
, filename
, 0) < 0) {
293 total_size
= bdrv_getlength(bs1
) >> SECTOR_BITS
;
296 get_tmp_filename(tmp_filename
, sizeof(tmp_filename
));
297 if (bdrv_create(&bdrv_qcow2
, tmp_filename
,
298 total_size
, filename
, 0) < 0) {
301 filename
= tmp_filename
;
302 bs
->is_temporary
= 1;
305 pstrcpy(bs
->filename
, sizeof(bs
->filename
), filename
);
306 if (flags
& BDRV_O_FILE
) {
307 drv
= find_protocol(filename
);
312 drv
= find_image_format(filename
);
318 bs
->opaque
= qemu_mallocz(drv
->instance_size
);
319 if (bs
->opaque
== NULL
&& drv
->instance_size
> 0)
321 /* Note: for compatibility, we open disk image files as RDWR, and
322 RDONLY as fallback */
323 if (!(flags
& BDRV_O_FILE
))
324 open_flags
= BDRV_O_RDWR
;
326 open_flags
= flags
& ~(BDRV_O_FILE
| BDRV_O_SNAPSHOT
);
327 ret
= drv
->bdrv_open(bs
, filename
, open_flags
);
328 if (ret
== -EACCES
&& !(flags
& BDRV_O_FILE
)) {
329 ret
= drv
->bdrv_open(bs
, filename
, BDRV_O_RDONLY
);
333 qemu_free(bs
->opaque
);
336 if (drv
->bdrv_getlength
) {
337 bs
->total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
340 if (bs
->is_temporary
) {
344 if (bs
->backing_file
[0] != '\0') {
345 /* if there is a backing file, use it */
346 bs
->backing_hd
= bdrv_new("");
347 if (!bs
->backing_hd
) {
352 path_combine(backing_filename
, sizeof(backing_filename
),
353 filename
, bs
->backing_file
);
354 if (bdrv_open(bs
->backing_hd
, backing_filename
, 0) < 0)
360 /* call the change callback */
362 bs
->change_cb(bs
->change_opaque
);
367 void bdrv_close(BlockDriverState
*bs
)
371 bdrv_delete(bs
->backing_hd
);
372 bs
->drv
->bdrv_close(bs
);
373 qemu_free(bs
->opaque
);
375 if (bs
->is_temporary
) {
376 unlink(bs
->filename
);
383 /* call the change callback */
385 bs
->change_cb(bs
->change_opaque
);
389 void bdrv_delete(BlockDriverState
*bs
)
391 /* XXX: remove the driver list */
396 /* commit COW file into the raw image */
397 int bdrv_commit(BlockDriverState
*bs
)
399 int64_t i
, total_sectors
;
401 unsigned char sector
[512];
410 if (!bs
->backing_hd
) {
414 total_sectors
= bdrv_getlength(bs
) >> SECTOR_BITS
;
415 for (i
= 0; i
< total_sectors
;) {
416 if (bs
->drv
->bdrv_is_allocated(bs
, i
, 65536, &n
)) {
417 for(j
= 0; j
< n
; j
++) {
418 if (bdrv_read(bs
, i
, sector
, 1) != 0) {
422 if (bdrv_write(bs
->backing_hd
, i
, sector
, 1) != 0) {
432 if (bs
->drv
->bdrv_make_empty
)
433 return bs
->drv
->bdrv_make_empty(bs
);
438 /* return < 0 if error */
439 int bdrv_read(BlockDriverState
*bs
, int64_t sector_num
,
440 uint8_t *buf
, int nb_sectors
)
442 BlockDriver
*drv
= bs
->drv
;
447 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
448 memcpy(buf
, bs
->boot_sector_data
, 512);
455 if (drv
->bdrv_pread
) {
457 len
= nb_sectors
* 512;
458 ret
= drv
->bdrv_pread(bs
, sector_num
* 512, buf
, len
);
466 return drv
->bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
470 /* return < 0 if error */
471 int bdrv_write(BlockDriverState
*bs
, int64_t sector_num
,
472 const uint8_t *buf
, int nb_sectors
)
474 BlockDriver
*drv
= bs
->drv
;
479 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
480 memcpy(bs
->boot_sector_data
, buf
, 512);
482 if (drv
->bdrv_pwrite
) {
484 len
= nb_sectors
* 512;
485 ret
= drv
->bdrv_pwrite(bs
, sector_num
* 512, buf
, len
);
493 return drv
->bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
497 /* not necessary now */
498 static int bdrv_pread_em(BlockDriverState
*bs
, int64_t offset
,
499 uint8_t *buf
, int count1
)
501 uint8_t tmp_buf
[SECTOR_SIZE
];
502 int len
, nb_sectors
, count
;
506 /* first read to align to sector start */
507 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
510 sector_num
= offset
>> SECTOR_BITS
;
512 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
514 memcpy(buf
, tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), len
);
522 /* read the sectors "in place" */
523 nb_sectors
= count
>> SECTOR_BITS
;
524 if (nb_sectors
> 0) {
525 if (bdrv_read(bs
, sector_num
, buf
, nb_sectors
) < 0)
527 sector_num
+= nb_sectors
;
528 len
= nb_sectors
<< SECTOR_BITS
;
533 /* add data from the last sector */
535 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
537 memcpy(buf
, tmp_buf
, count
);
542 static int bdrv_pwrite_em(BlockDriverState
*bs
, int64_t offset
,
543 const uint8_t *buf
, int count1
)
545 uint8_t tmp_buf
[SECTOR_SIZE
];
546 int len
, nb_sectors
, count
;
550 /* first write to align to sector start */
551 len
= (SECTOR_SIZE
- offset
) & (SECTOR_SIZE
- 1);
554 sector_num
= offset
>> SECTOR_BITS
;
556 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
558 memcpy(tmp_buf
+ (offset
& (SECTOR_SIZE
- 1)), buf
, len
);
559 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
568 /* write the sectors "in place" */
569 nb_sectors
= count
>> SECTOR_BITS
;
570 if (nb_sectors
> 0) {
571 if (bdrv_write(bs
, sector_num
, buf
, nb_sectors
) < 0)
573 sector_num
+= nb_sectors
;
574 len
= nb_sectors
<< SECTOR_BITS
;
579 /* add data from the last sector */
581 if (bdrv_read(bs
, sector_num
, tmp_buf
, 1) < 0)
583 memcpy(tmp_buf
, buf
, count
);
584 if (bdrv_write(bs
, sector_num
, tmp_buf
, 1) < 0)
591 * Read with byte offsets (needed only for file protocols)
593 int bdrv_pread(BlockDriverState
*bs
, int64_t offset
,
594 void *buf1
, int count1
)
596 BlockDriver
*drv
= bs
->drv
;
600 if (!drv
->bdrv_pread
)
601 return bdrv_pread_em(bs
, offset
, buf1
, count1
);
602 return drv
->bdrv_pread(bs
, offset
, buf1
, count1
);
606 * Write with byte offsets (needed only for file protocols)
608 int bdrv_pwrite(BlockDriverState
*bs
, int64_t offset
,
609 const void *buf1
, int count1
)
611 BlockDriver
*drv
= bs
->drv
;
615 if (!drv
->bdrv_pwrite
)
616 return bdrv_pwrite_em(bs
, offset
, buf1
, count1
);
617 return drv
->bdrv_pwrite(bs
, offset
, buf1
, count1
);
621 * Truncate file to 'offset' bytes (needed only for file protocols)
623 int bdrv_truncate(BlockDriverState
*bs
, int64_t offset
)
625 BlockDriver
*drv
= bs
->drv
;
628 if (!drv
->bdrv_truncate
)
630 return drv
->bdrv_truncate(bs
, offset
);
634 * Length of a file in bytes. Return < 0 if error or unknown.
636 int64_t bdrv_getlength(BlockDriverState
*bs
)
638 BlockDriver
*drv
= bs
->drv
;
641 if (!drv
->bdrv_getlength
) {
643 return bs
->total_sectors
* SECTOR_SIZE
;
645 return drv
->bdrv_getlength(bs
);
648 void bdrv_get_geometry(BlockDriverState
*bs
, int64_t *nb_sectors_ptr
)
650 *nb_sectors_ptr
= bs
->total_sectors
;
653 /* force a given boot sector. */
654 void bdrv_set_boot_sector(BlockDriverState
*bs
, const uint8_t *data
, int size
)
656 bs
->boot_sector_enabled
= 1;
659 memcpy(bs
->boot_sector_data
, data
, size
);
660 memset(bs
->boot_sector_data
+ size
, 0, 512 - size
);
663 void bdrv_set_geometry_hint(BlockDriverState
*bs
,
664 int cyls
, int heads
, int secs
)
671 void bdrv_set_type_hint(BlockDriverState
*bs
, int type
)
674 bs
->removable
= ((type
== BDRV_TYPE_CDROM
||
675 type
== BDRV_TYPE_FLOPPY
));
678 void bdrv_set_translation_hint(BlockDriverState
*bs
, int translation
)
680 bs
->translation
= translation
;
683 void bdrv_get_geometry_hint(BlockDriverState
*bs
,
684 int *pcyls
, int *pheads
, int *psecs
)
691 int bdrv_get_type_hint(BlockDriverState
*bs
)
696 int bdrv_get_translation_hint(BlockDriverState
*bs
)
698 return bs
->translation
;
701 int bdrv_is_removable(BlockDriverState
*bs
)
703 return bs
->removable
;
706 int bdrv_is_read_only(BlockDriverState
*bs
)
708 return bs
->read_only
;
711 int bdrv_is_inserted(BlockDriverState
*bs
)
716 int bdrv_is_locked(BlockDriverState
*bs
)
721 void bdrv_set_locked(BlockDriverState
*bs
, int locked
)
726 void bdrv_set_change_cb(BlockDriverState
*bs
,
727 void (*change_cb
)(void *opaque
), void *opaque
)
729 bs
->change_cb
= change_cb
;
730 bs
->change_opaque
= opaque
;
733 int bdrv_is_encrypted(BlockDriverState
*bs
)
735 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
)
737 return bs
->encrypted
;
740 int bdrv_set_key(BlockDriverState
*bs
, const char *key
)
743 if (bs
->backing_hd
&& bs
->backing_hd
->encrypted
) {
744 ret
= bdrv_set_key(bs
->backing_hd
, key
);
750 if (!bs
->encrypted
|| !bs
->drv
|| !bs
->drv
->bdrv_set_key
)
752 return bs
->drv
->bdrv_set_key(bs
, key
);
755 void bdrv_get_format(BlockDriverState
*bs
, char *buf
, int buf_size
)
757 if (!bs
->inserted
|| !bs
->drv
) {
760 pstrcpy(buf
, buf_size
, bs
->drv
->format_name
);
764 void bdrv_iterate_format(void (*it
)(void *opaque
, const char *name
),
769 for (drv
= first_drv
; drv
!= NULL
; drv
= drv
->next
) {
770 it(opaque
, drv
->format_name
);
774 BlockDriverState
*bdrv_find(const char *name
)
776 BlockDriverState
*bs
;
778 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
779 if (!strcmp(name
, bs
->device_name
))
785 void bdrv_iterate(void (*it
)(void *opaque
, const char *name
), void *opaque
)
787 BlockDriverState
*bs
;
789 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
790 it(opaque
, bs
->device_name
);
794 const char *bdrv_get_device_name(BlockDriverState
*bs
)
796 return bs
->device_name
;
799 void bdrv_flush(BlockDriverState
*bs
)
801 if (bs
->drv
->bdrv_flush
)
802 bs
->drv
->bdrv_flush(bs
);
804 bdrv_flush(bs
->backing_hd
);
809 BlockDriverState
*bs
;
811 for (bs
= bdrv_first
; bs
!= NULL
; bs
= bs
->next
) {
812 term_printf("%s:", bs
->device_name
);
813 term_printf(" type=");
818 case BDRV_TYPE_CDROM
:
819 term_printf("cdrom");
821 case BDRV_TYPE_FLOPPY
:
822 term_printf("floppy");
825 term_printf(" removable=%d", bs
->removable
);
827 term_printf(" locked=%d", bs
->locked
);
830 term_printf(" file=%s", bs
->filename
);
831 if (bs
->backing_file
[0] != '\0')
832 term_printf(" backing_file=%s", bs
->backing_file
);
833 term_printf(" ro=%d", bs
->read_only
);
834 term_printf(" drv=%s", bs
->drv
->format_name
);
836 term_printf(" encrypted");
838 term_printf(" [not inserted]");
844 void bdrv_get_backing_filename(BlockDriverState
*bs
,
845 char *filename
, int filename_size
)
847 if (!bs
->backing_hd
) {
848 pstrcpy(filename
, filename_size
, "");
850 pstrcpy(filename
, filename_size
, bs
->backing_file
);
854 int bdrv_write_compressed(BlockDriverState
*bs
, int64_t sector_num
,
855 const uint8_t *buf
, int nb_sectors
)
857 BlockDriver
*drv
= bs
->drv
;
860 if (!drv
->bdrv_write_compressed
)
862 return drv
->bdrv_write_compressed(bs
, sector_num
, buf
, nb_sectors
);
865 int bdrv_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
867 BlockDriver
*drv
= bs
->drv
;
870 if (!drv
->bdrv_get_info
)
872 memset(bdi
, 0, sizeof(*bdi
));
873 return drv
->bdrv_get_info(bs
, bdi
);
876 /**************************************************************/
877 /* handling of snapshots */
879 int bdrv_snapshot_create(BlockDriverState
*bs
,
880 QEMUSnapshotInfo
*sn_info
)
882 BlockDriver
*drv
= bs
->drv
;
885 if (!drv
->bdrv_snapshot_create
)
887 return drv
->bdrv_snapshot_create(bs
, sn_info
);
890 int bdrv_snapshot_goto(BlockDriverState
*bs
,
891 const char *snapshot_id
)
893 BlockDriver
*drv
= bs
->drv
;
896 if (!drv
->bdrv_snapshot_goto
)
898 return drv
->bdrv_snapshot_goto(bs
, snapshot_id
);
901 int bdrv_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
903 BlockDriver
*drv
= bs
->drv
;
906 if (!drv
->bdrv_snapshot_delete
)
908 return drv
->bdrv_snapshot_delete(bs
, snapshot_id
);
911 int bdrv_snapshot_list(BlockDriverState
*bs
,
912 QEMUSnapshotInfo
**psn_info
)
914 BlockDriver
*drv
= bs
->drv
;
917 if (!drv
->bdrv_snapshot_list
)
919 return drv
->bdrv_snapshot_list(bs
, psn_info
);
922 #define NB_SUFFIXES 4
924 char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
926 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
931 snprintf(buf
, buf_size
, "%" PRId64
, size
);
934 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
935 if (size
< (10 * base
)) {
936 snprintf(buf
, buf_size
, "%0.1f%c",
940 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
941 snprintf(buf
, buf_size
, "%" PRId64
"%c",
942 ((size
+ (base
>> 1)) / base
),
952 char *bdrv_snapshot_dump(char *buf
, int buf_size
, QEMUSnapshotInfo
*sn
)
954 char buf1
[128], date_buf
[128], clock_buf
[128];
960 snprintf(buf
, buf_size
,
961 "%-10s%-20s%7s%20s%15s",
962 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
966 localtime_r(&ti
, &tm
);
968 strftime(date_buf
, sizeof(date_buf
),
969 "%Y-%m-%d %H:%M:%S", &tm
);
970 secs
= sn
->vm_clock_nsec
/ 1000000000;
971 snprintf(clock_buf
, sizeof(clock_buf
),
972 "%02d:%02d:%02d.%03d",
974 (int)((secs
/ 60) % 60),
976 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
977 snprintf(buf
, buf_size
,
978 "%-10s%-20s%7s%20s%15s",
979 sn
->id_str
, sn
->name
,
980 get_human_readable_size(buf1
, sizeof(buf1
), sn
->vm_state_size
),
988 /**************************************************************/
991 BlockDriverAIOCB
*bdrv_aio_read(BlockDriverState
*bs
, int64_t sector_num
,
992 uint8_t *buf
, int nb_sectors
,
993 BlockDriverCompletionFunc
*cb
, void *opaque
)
995 BlockDriver
*drv
= bs
->drv
;
1000 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1001 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1002 memcpy(buf
, bs
->boot_sector_data
, 512);
1008 return drv
->bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1011 BlockDriverAIOCB
*bdrv_aio_write(BlockDriverState
*bs
, int64_t sector_num
,
1012 const uint8_t *buf
, int nb_sectors
,
1013 BlockDriverCompletionFunc
*cb
, void *opaque
)
1015 BlockDriver
*drv
= bs
->drv
;
1021 if (sector_num
== 0 && bs
->boot_sector_enabled
&& nb_sectors
> 0) {
1022 memcpy(bs
->boot_sector_data
, buf
, 512);
1025 return drv
->bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1028 void bdrv_aio_cancel(BlockDriverAIOCB
*acb
)
1030 BlockDriver
*drv
= acb
->bs
->drv
;
1032 drv
->bdrv_aio_cancel(acb
);
1036 /**************************************************************/
1037 /* async block device emulation */
1040 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1041 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1042 BlockDriverCompletionFunc
*cb
, void *opaque
)
1045 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1050 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1051 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1052 BlockDriverCompletionFunc
*cb
, void *opaque
)
1055 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1060 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*acb
)
1064 typedef struct BlockDriverAIOCBSync
{
1065 BlockDriverAIOCB common
;
1068 } BlockDriverAIOCBSync
;
1070 static BlockDriverAIOCBSync
*free_acb
= NULL
;
1072 static void bdrv_aio_bh_cb(void *opaque
)
1074 BlockDriverAIOCBSync
*acb
= opaque
;
1075 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1076 qemu_aio_release(acb
);
1079 static BlockDriverAIOCB
*bdrv_aio_read_em(BlockDriverState
*bs
,
1080 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1081 BlockDriverCompletionFunc
*cb
, void *opaque
)
1083 BlockDriverAIOCBSync
*acb
;
1086 acb
= qemu_aio_get(bs
, cb
, opaque
);
1088 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1089 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
1091 qemu_bh_schedule(acb
->bh
);
1092 return &acb
->common
;
1095 static BlockDriverAIOCB
*bdrv_aio_write_em(BlockDriverState
*bs
,
1096 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
1097 BlockDriverCompletionFunc
*cb
, void *opaque
)
1099 BlockDriverAIOCBSync
*acb
;
1102 acb
= qemu_aio_get(bs
, cb
, opaque
);
1104 acb
->bh
= qemu_bh_new(bdrv_aio_bh_cb
, acb
);
1105 ret
= bdrv_write(bs
, sector_num
, buf
, nb_sectors
);
1107 qemu_bh_schedule(acb
->bh
);
1108 return &acb
->common
;
1111 static void bdrv_aio_cancel_em(BlockDriverAIOCB
*blockacb
)
1113 BlockDriverAIOCBSync
*acb
= (BlockDriverAIOCBSync
*)blockacb
;
1114 qemu_bh_cancel(acb
->bh
);
1115 qemu_aio_release(acb
);
1117 #endif /* !QEMU_TOOL */
1119 /**************************************************************/
1120 /* sync block device emulation */
1122 static void bdrv_rw_em_cb(void *opaque
, int ret
)
1124 *(int *)opaque
= ret
;
1127 #define NOT_DONE 0x7fffffff
1129 static int bdrv_read_em(BlockDriverState
*bs
, int64_t sector_num
,
1130 uint8_t *buf
, int nb_sectors
)
1133 BlockDriverAIOCB
*acb
;
1135 async_ret
= NOT_DONE
;
1136 qemu_aio_wait_start();
1137 acb
= bdrv_aio_read(bs
, sector_num
, buf
, nb_sectors
,
1138 bdrv_rw_em_cb
, &async_ret
);
1140 qemu_aio_wait_end();
1143 while (async_ret
== NOT_DONE
) {
1146 qemu_aio_wait_end();
1150 static int bdrv_write_em(BlockDriverState
*bs
, int64_t sector_num
,
1151 const uint8_t *buf
, int nb_sectors
)
1154 BlockDriverAIOCB
*acb
;
1156 async_ret
= NOT_DONE
;
1157 qemu_aio_wait_start();
1158 acb
= bdrv_aio_write(bs
, sector_num
, buf
, nb_sectors
,
1159 bdrv_rw_em_cb
, &async_ret
);
1161 qemu_aio_wait_end();
1164 while (async_ret
== NOT_DONE
) {
1167 qemu_aio_wait_end();
1171 void bdrv_init(void)
1173 bdrv_register(&bdrv_raw
);
1175 bdrv_register(&bdrv_cow
);
1177 bdrv_register(&bdrv_qcow
);
1178 bdrv_register(&bdrv_vmdk
);
1179 bdrv_register(&bdrv_cloop
);
1180 bdrv_register(&bdrv_dmg
);
1181 bdrv_register(&bdrv_bochs
);
1182 bdrv_register(&bdrv_vpc
);
1183 bdrv_register(&bdrv_vvfat
);
1184 bdrv_register(&bdrv_qcow2
);
1187 void *qemu_aio_get(BlockDriverState
*bs
, BlockDriverCompletionFunc
*cb
,
1191 BlockDriverAIOCB
*acb
;
1194 if (drv
->free_aiocb
) {
1195 acb
= drv
->free_aiocb
;
1196 drv
->free_aiocb
= acb
->next
;
1198 acb
= qemu_mallocz(drv
->aiocb_size
);
1204 acb
->opaque
= opaque
;
1208 void qemu_aio_release(void *p
)
1210 BlockDriverAIOCB
*acb
= p
;
1211 BlockDriver
*drv
= acb
->bs
->drv
;
1212 acb
->next
= drv
->free_aiocb
;
1213 drv
->free_aiocb
= acb
;