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);
184 static BlockDriver *find_protocol(const char *filename)
190 p = strchr(filename, ':');
194 if (len > sizeof(protocol) - 1)
195 len = sizeof(protocol) - 1;
198 /* specific win32 case for driver letters */
202 memcpy(protocol, filename, len);
203 protocol[len] = '\0';
204 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
205 if (drv1->protocol_name &&
206 !strcmp(drv1->protocol_name, protocol))
212 /* XXX: force raw format if block or character device ? It would
213 simplify the BSD case */
214 static BlockDriver *find_image_format(const char *filename)
216 int ret, score, score_max;
217 BlockDriver *drv1, *drv;
219 BlockDriverState *bs;
221 drv = find_protocol(filename);
222 /* no need to test disk image formats for vvfat or host specific
224 if (drv == &bdrv_vvfat)
226 if (strstart(filename, "/dev/", NULL))
229 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
232 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
239 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
240 if (drv1->bdrv_probe) {
241 score = drv1->bdrv_probe(buf, ret, filename);
242 if (score > score_max) {
251 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
253 BlockDriverState *bs;
259 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
268 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
270 return bdrv_open2(bs, filename, flags, NULL);
273 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
277 char tmp_filename[1024];
278 char backing_filename[1024];
281 bs->is_temporary = 0;
284 if (flags & BDRV_O_SNAPSHOT) {
285 BlockDriverState *bs1;
288 /* if snapshot, we create a temporary backing file and open it
289 instead of opening 'filename' directly */
291 /* if there is a backing file, use it */
296 if (bdrv_open(bs1, filename, 0) < 0) {
300 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
303 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
304 if (bdrv_create(&bdrv_qcow2, tmp_filename,
305 total_size, filename, 0) < 0) {
308 filename = tmp_filename;
309 bs->is_temporary = 1;
312 pstrcpy(bs->filename, sizeof(bs->filename), filename);
313 if (flags & BDRV_O_FILE) {
314 drv = find_protocol(filename);
319 drv = find_image_format(filename);
325 bs->opaque = qemu_mallocz(drv->instance_size);
326 if (bs->opaque == NULL && drv->instance_size > 0)
328 /* Note: for compatibility, we open disk image files as RDWR, and
329 RDONLY as fallback */
330 if (!(flags & BDRV_O_FILE))
331 open_flags = BDRV_O_RDWR;
333 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
334 ret = drv->bdrv_open(bs, filename, open_flags);
335 if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
336 ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
340 qemu_free(bs->opaque);
343 if (drv->bdrv_getlength) {
344 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
347 if (bs->is_temporary) {
351 if (bs->backing_file[0] != '\0') {
352 /* if there is a backing file, use it */
353 bs->backing_hd = bdrv_new("");
354 if (!bs->backing_hd) {
359 path_combine(backing_filename, sizeof(backing_filename),
360 filename, bs->backing_file);
361 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
367 /* call the change callback */
369 bs->change_cb(bs->change_opaque);
374 void bdrv_close(BlockDriverState *bs)
378 bdrv_delete(bs->backing_hd);
379 bs->drv->bdrv_close(bs);
380 qemu_free(bs->opaque);
382 if (bs->is_temporary) {
383 unlink(bs->filename);
390 /* call the change callback */
392 bs->change_cb(bs->change_opaque);
396 void bdrv_delete(BlockDriverState *bs)
398 /* XXX: remove the driver list */
403 /* commit COW file into the raw image */
404 int bdrv_commit(BlockDriverState *bs)
406 int64_t i, total_sectors;
408 unsigned char sector[512];
417 if (!bs->backing_hd) {
421 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
422 for (i = 0; i < total_sectors;) {
423 if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) {
424 for(j = 0; j < n; j++) {
425 if (bdrv_read(bs, i, sector, 1) != 0) {
429 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
439 if (bs->drv->bdrv_make_empty)
440 return bs->drv->bdrv_make_empty(bs);
445 /* return < 0 if error */
446 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
447 uint8_t *buf, int nb_sectors)
449 BlockDriver *drv = bs->drv;
454 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
455 memcpy(buf, bs->boot_sector_data, 512);
462 if (drv->bdrv_pread) {
464 len = nb_sectors * 512;
465 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
473 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
477 /* return < 0 if error */
478 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
479 const uint8_t *buf, int nb_sectors)
481 BlockDriver *drv = bs->drv;
486 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
487 memcpy(bs->boot_sector_data, buf, 512);
489 if (drv->bdrv_pwrite) {
491 len = nb_sectors * 512;
492 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
500 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
504 /* not necessary now */
505 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
506 uint8_t *buf, int count1)
508 uint8_t tmp_buf[SECTOR_SIZE];
509 int len, nb_sectors, count;
513 /* first read to align to sector start */
514 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
517 sector_num = offset >> SECTOR_BITS;
519 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
521 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
529 /* read the sectors "in place" */
530 nb_sectors = count >> SECTOR_BITS;
531 if (nb_sectors > 0) {
532 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
534 sector_num += nb_sectors;
535 len = nb_sectors << SECTOR_BITS;
540 /* add data from the last sector */
542 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
544 memcpy(buf, tmp_buf, count);
549 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
550 const uint8_t *buf, int count1)
552 uint8_t tmp_buf[SECTOR_SIZE];
553 int len, nb_sectors, count;
557 /* first write to align to sector start */
558 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
561 sector_num = offset >> SECTOR_BITS;
563 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
565 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
566 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
575 /* write the sectors "in place" */
576 nb_sectors = count >> SECTOR_BITS;
577 if (nb_sectors > 0) {
578 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
580 sector_num += nb_sectors;
581 len = nb_sectors << SECTOR_BITS;
586 /* add data from the last sector */
588 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
590 memcpy(tmp_buf, buf, count);
591 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
598 * Read with byte offsets (needed only for file protocols)
600 int bdrv_pread(BlockDriverState *bs, int64_t offset,
601 void *buf1, int count1)
603 BlockDriver *drv = bs->drv;
607 if (!drv->bdrv_pread)
608 return bdrv_pread_em(bs, offset, buf1, count1);
609 return drv->bdrv_pread(bs, offset, buf1, count1);
613 * Write with byte offsets (needed only for file protocols)
615 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
616 const void *buf1, int count1)
618 BlockDriver *drv = bs->drv;
622 if (!drv->bdrv_pwrite)
623 return bdrv_pwrite_em(bs, offset, buf1, count1);
624 return drv->bdrv_pwrite(bs, offset, buf1, count1);
628 * Truncate file to 'offset' bytes (needed only for file protocols)
630 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
632 BlockDriver *drv = bs->drv;
635 if (!drv->bdrv_truncate)
637 return drv->bdrv_truncate(bs, offset);
641 * Length of a file in bytes. Return < 0 if error or unknown.
643 int64_t bdrv_getlength(BlockDriverState *bs)
645 BlockDriver *drv = bs->drv;
648 if (!drv->bdrv_getlength) {
650 return bs->total_sectors * SECTOR_SIZE;
652 return drv->bdrv_getlength(bs);
655 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
657 *nb_sectors_ptr = bs->total_sectors;
660 /* force a given boot sector. */
661 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
663 bs->boot_sector_enabled = 1;
666 memcpy(bs->boot_sector_data, data, size);
667 memset(bs->boot_sector_data + size, 0, 512 - size);
670 void bdrv_set_geometry_hint(BlockDriverState *bs,
671 int cyls, int heads, int secs)
678 void bdrv_set_type_hint(BlockDriverState *bs, int type)
681 bs->removable = ((type == BDRV_TYPE_CDROM ||
682 type == BDRV_TYPE_FLOPPY));
685 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
687 bs->translation = translation;
690 void bdrv_get_geometry_hint(BlockDriverState *bs,
691 int *pcyls, int *pheads, int *psecs)
698 int bdrv_get_type_hint(BlockDriverState *bs)
703 int bdrv_get_translation_hint(BlockDriverState *bs)
705 return bs->translation;
708 int bdrv_is_removable(BlockDriverState *bs)
710 return bs->removable;
713 int bdrv_is_read_only(BlockDriverState *bs)
715 return bs->read_only;
718 int bdrv_is_inserted(BlockDriverState *bs)
723 int bdrv_is_locked(BlockDriverState *bs)
728 void bdrv_set_locked(BlockDriverState *bs, int locked)
733 void bdrv_set_change_cb(BlockDriverState *bs,
734 void (*change_cb)(void *opaque), void *opaque)
736 bs->change_cb = change_cb;
737 bs->change_opaque = opaque;
740 int bdrv_is_encrypted(BlockDriverState *bs)
742 if (bs->backing_hd && bs->backing_hd->encrypted)
744 return bs->encrypted;
747 int bdrv_set_key(BlockDriverState *bs, const char *key)
750 if (bs->backing_hd && bs->backing_hd->encrypted) {
751 ret = bdrv_set_key(bs->backing_hd, key);
757 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
759 return bs->drv->bdrv_set_key(bs, key);
762 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
764 if (!bs->inserted || !bs->drv) {
767 pstrcpy(buf, buf_size, bs->drv->format_name);
771 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
776 for (drv = first_drv; drv != NULL; drv = drv->next) {
777 it(opaque, drv->format_name);
781 BlockDriverState *bdrv_find(const char *name)
783 BlockDriverState *bs;
785 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
786 if (!strcmp(name, bs->device_name))
792 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
794 BlockDriverState *bs;
796 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
797 it(opaque, bs->device_name);
801 const char *bdrv_get_device_name(BlockDriverState *bs)
803 return bs->device_name;
806 void bdrv_flush(BlockDriverState *bs)
808 if (bs->drv->bdrv_flush)
809 bs->drv->bdrv_flush(bs);
811 bdrv_flush(bs->backing_hd);
816 BlockDriverState *bs;
818 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
819 term_printf("%s:", bs->device_name);
820 term_printf(" type=");
825 case BDRV_TYPE_CDROM:
826 term_printf("cdrom");
828 case BDRV_TYPE_FLOPPY:
829 term_printf("floppy");
832 term_printf(" removable=%d", bs->removable);
834 term_printf(" locked=%d", bs->locked);
837 term_printf(" file=%s", bs->filename);
838 if (bs->backing_file[0] != '\0')
839 term_printf(" backing_file=%s", bs->backing_file);
840 term_printf(" ro=%d", bs->read_only);
841 term_printf(" drv=%s", bs->drv->format_name);
843 term_printf(" encrypted");
845 term_printf(" [not inserted]");
851 void bdrv_get_backing_filename(BlockDriverState *bs,
852 char *filename, int filename_size)
854 if (!bs->backing_hd) {
855 pstrcpy(filename, filename_size, "");
857 pstrcpy(filename, filename_size, bs->backing_file);
861 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
862 const uint8_t *buf, int nb_sectors)
864 BlockDriver *drv = bs->drv;
867 if (!drv->bdrv_write_compressed)
869 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
872 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
874 BlockDriver *drv = bs->drv;
877 if (!drv->bdrv_get_info)
879 memset(bdi, 0, sizeof(*bdi));
880 return drv->bdrv_get_info(bs, bdi);
883 /**************************************************************/
884 /* handling of snapshots */
886 int bdrv_snapshot_create(BlockDriverState *bs,
887 QEMUSnapshotInfo *sn_info)
889 BlockDriver *drv = bs->drv;
892 if (!drv->bdrv_snapshot_create)
894 return drv->bdrv_snapshot_create(bs, sn_info);
897 int bdrv_snapshot_goto(BlockDriverState *bs,
898 const char *snapshot_id)
900 BlockDriver *drv = bs->drv;
903 if (!drv->bdrv_snapshot_goto)
905 return drv->bdrv_snapshot_goto(bs, snapshot_id);
908 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
910 BlockDriver *drv = bs->drv;
913 if (!drv->bdrv_snapshot_delete)
915 return drv->bdrv_snapshot_delete(bs, snapshot_id);
918 int bdrv_snapshot_list(BlockDriverState *bs,
919 QEMUSnapshotInfo **psn_info)
921 BlockDriver *drv = bs->drv;
924 if (!drv->bdrv_snapshot_list)
926 return drv->bdrv_snapshot_list(bs, psn_info);
929 #define NB_SUFFIXES 4
931 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
933 static const char suffixes[NB_SUFFIXES] = "KMGT";
938 snprintf(buf, buf_size, "%" PRId64, size);
941 for(i = 0; i < NB_SUFFIXES; i++) {
942 if (size < (10 * base)) {
943 snprintf(buf, buf_size, "%0.1f%c",
947 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
948 snprintf(buf, buf_size, "%" PRId64 "%c",
949 ((size + (base >> 1)) / base),
959 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
961 char buf1[128], date_buf[128], clock_buf[128];
967 snprintf(buf, buf_size,
968 "%-10s%-20s%7s%20s%15s",
969 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
973 localtime_r(&ti, &tm);
975 strftime(date_buf, sizeof(date_buf),
976 "%Y-%m-%d %H:%M:%S", &tm);
977 secs = sn->vm_clock_nsec / 1000000000;
978 snprintf(clock_buf, sizeof(clock_buf),
979 "%02d:%02d:%02d.%03d",
981 (int)((secs / 60) % 60),
983 (int)((sn->vm_clock_nsec / 1000000) % 1000));
984 snprintf(buf, buf_size,
985 "%-10s%-20s%7s%20s%15s",
986 sn->id_str, sn->name,
987 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
995 /**************************************************************/
998 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
999 uint8_t *buf, int nb_sectors,
1000 BlockDriverCompletionFunc *cb, void *opaque)
1002 BlockDriver *drv = bs->drv;
1007 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1008 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1009 memcpy(buf, bs->boot_sector_data, 512);
1015 return drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1018 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1019 const uint8_t *buf, int nb_sectors,
1020 BlockDriverCompletionFunc *cb, void *opaque)
1022 BlockDriver *drv = bs->drv;
1028 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1029 memcpy(bs->boot_sector_data, buf, 512);
1032 return drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1035 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1037 BlockDriver *drv = acb->bs->drv;
1039 drv->bdrv_aio_cancel(acb);
1043 /**************************************************************/
1044 /* async block device emulation */
1047 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1048 int64_t sector_num, uint8_t *buf, int nb_sectors,
1049 BlockDriverCompletionFunc *cb, void *opaque)
1052 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1057 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1058 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1059 BlockDriverCompletionFunc *cb, void *opaque)
1062 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1067 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1071 static void bdrv_aio_bh_cb(void *opaque)
1073 BlockDriverAIOCBSync *acb = opaque;
1074 acb->common.cb(acb->common.opaque, acb->ret);
1075 qemu_aio_release(acb);
1078 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1079 int64_t sector_num, uint8_t *buf, int nb_sectors,
1080 BlockDriverCompletionFunc *cb, void *opaque)
1082 BlockDriverAIOCBSync *acb;
1085 acb = qemu_aio_get(bs, cb, opaque);
1087 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1088 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1090 qemu_bh_schedule(acb->bh);
1091 return &acb->common;
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)
1098 BlockDriverAIOCBSync *acb;
1101 acb = qemu_aio_get(bs, cb, opaque);
1103 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1104 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1106 qemu_bh_schedule(acb->bh);
1107 return &acb->common;
1110 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1112 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1113 qemu_bh_cancel(acb->bh);
1114 qemu_aio_release(acb);
1116 #endif /* !QEMU_TOOL */
1118 /**************************************************************/
1119 /* sync block device emulation */
1121 static void bdrv_rw_em_cb(void *opaque, int ret)
1123 *(int *)opaque = ret;
1126 #define NOT_DONE 0x7fffffff
1128 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1129 uint8_t *buf, int nb_sectors)
1132 BlockDriverAIOCB *acb;
1134 async_ret = NOT_DONE;
1135 qemu_aio_wait_start();
1136 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1137 bdrv_rw_em_cb, &async_ret);
1139 qemu_aio_wait_end();
1142 while (async_ret == NOT_DONE) {
1145 qemu_aio_wait_end();
1149 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1150 const uint8_t *buf, int nb_sectors)
1153 BlockDriverAIOCB *acb;
1155 async_ret = NOT_DONE;
1156 qemu_aio_wait_start();
1157 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1158 bdrv_rw_em_cb, &async_ret);
1160 qemu_aio_wait_end();
1163 while (async_ret == NOT_DONE) {
1166 qemu_aio_wait_end();
1170 void bdrv_init(void)
1172 bdrv_register(&bdrv_raw);
1174 bdrv_register(&bdrv_cow);
1176 bdrv_register(&bdrv_qcow);
1177 bdrv_register(&bdrv_vmdk);
1178 bdrv_register(&bdrv_cloop);
1179 bdrv_register(&bdrv_dmg);
1180 bdrv_register(&bdrv_bochs);
1181 bdrv_register(&bdrv_vpc);
1182 bdrv_register(&bdrv_vvfat);
1183 bdrv_register(&bdrv_qcow2);
1186 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1190 BlockDriverAIOCB *acb;
1193 if (drv->free_aiocb) {
1194 acb = drv->free_aiocb;
1195 drv->free_aiocb = acb->next;
1197 acb = qemu_mallocz(drv->aiocb_size);
1203 acb->opaque = opaque;
1207 void qemu_aio_release(void *p)
1209 BlockDriverAIOCB *acb = p;
1210 BlockDriver *drv = acb->bs->drv;
1211 acb->next = drv->free_aiocb;
1212 drv->free_aiocb = acb;