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 int bdrv_aio_new_em(BlockDriverAIOCB *acb);
39 static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num,
40 uint8_t *buf, int nb_sectors);
41 static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num,
42 const uint8_t *buf, int nb_sectors);
43 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
44 static void bdrv_aio_delete_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_new) {
110 /* add AIO emulation layer */
111 bdrv->bdrv_aio_new = bdrv_aio_new_em;
112 bdrv->bdrv_aio_read = bdrv_aio_read_em;
113 bdrv->bdrv_aio_write = bdrv_aio_write_em;
114 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
115 bdrv->bdrv_aio_delete = bdrv_aio_delete_em;
116 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
117 /* add synchronous IO emulation layer */
118 bdrv->bdrv_read = bdrv_read_em;
119 bdrv->bdrv_write = bdrv_write_em;
121 bdrv->next = first_drv;
125 /* create a new block device (by default it is empty) */
126 BlockDriverState *bdrv_new(const char *device_name)
128 BlockDriverState **pbs, *bs;
130 bs = qemu_mallocz(sizeof(BlockDriverState));
133 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
134 if (device_name[0] != '\0') {
135 /* insert at the end */
144 BlockDriver *bdrv_find_format(const char *format_name)
147 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
148 if (!strcmp(drv1->format_name, format_name))
154 int bdrv_create(BlockDriver *drv,
155 const char *filename, int64_t size_in_sectors,
156 const char *backing_file, int flags)
158 if (!drv->bdrv_create)
160 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
164 void get_tmp_filename(char *filename, int size)
169 void get_tmp_filename(char *filename, int size)
172 /* XXX: race condition possible */
173 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
174 fd = mkstemp(filename);
179 static BlockDriver *find_protocol(const char *filename)
185 p = strchr(filename, ':');
189 if (len > sizeof(protocol) - 1)
190 len = sizeof(protocol) - 1;
193 /* specific win32 case for driver letters */
197 memcpy(protocol, filename, len);
198 protocol[len] = '\0';
199 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
200 if (drv1->protocol_name &&
201 !strcmp(drv1->protocol_name, protocol))
207 /* XXX: force raw format if block or character device ? It would
208 simplify the BSD case */
209 static BlockDriver *find_image_format(const char *filename)
211 int ret, score, score_max;
212 BlockDriver *drv1, *drv;
214 BlockDriverState *bs;
216 drv = find_protocol(filename);
217 /* no need to test disk image formats for vvfat or host specific
219 if (drv == &bdrv_vvfat)
221 if (strstart(filename, "/dev/", NULL))
224 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
227 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
234 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
235 if (drv1->bdrv_probe) {
236 score = drv1->bdrv_probe(buf, ret, filename);
237 if (score > score_max) {
246 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
248 BlockDriverState *bs;
254 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
263 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
265 return bdrv_open2(bs, filename, flags, NULL);
268 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
272 char tmp_filename[1024];
273 char backing_filename[1024];
276 bs->is_temporary = 0;
279 if (flags & BDRV_O_SNAPSHOT) {
280 BlockDriverState *bs1;
283 /* if snapshot, we create a temporary backing file and open it
284 instead of opening 'filename' directly */
286 /* if there is a backing file, use it */
291 if (bdrv_open(bs1, filename, 0) < 0) {
295 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
298 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
299 if (bdrv_create(&bdrv_qcow2, tmp_filename,
300 total_size, filename, 0) < 0) {
303 filename = tmp_filename;
304 bs->is_temporary = 1;
307 pstrcpy(bs->filename, sizeof(bs->filename), filename);
308 if (flags & BDRV_O_FILE) {
309 drv = find_protocol(filename);
314 drv = find_image_format(filename);
320 bs->opaque = qemu_mallocz(drv->instance_size);
321 if (bs->opaque == NULL && drv->instance_size > 0)
323 /* Note: for compatibility, we open disk image files as RDWR, and
324 RDONLY as fallback */
325 if (!(flags & BDRV_O_FILE))
326 open_flags = BDRV_O_RDWR;
328 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
329 ret = drv->bdrv_open(bs, filename, open_flags);
330 if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
331 ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
335 qemu_free(bs->opaque);
338 if (drv->bdrv_getlength) {
339 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
342 if (bs->is_temporary) {
346 if (bs->backing_file[0] != '\0') {
347 /* if there is a backing file, use it */
348 bs->backing_hd = bdrv_new("");
349 if (!bs->backing_hd) {
354 path_combine(backing_filename, sizeof(backing_filename),
355 filename, bs->backing_file);
356 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
362 /* call the change callback */
364 bs->change_cb(bs->change_opaque);
369 void bdrv_close(BlockDriverState *bs)
373 bdrv_delete(bs->backing_hd);
374 bs->drv->bdrv_close(bs);
375 qemu_free(bs->opaque);
377 if (bs->is_temporary) {
378 unlink(bs->filename);
385 /* call the change callback */
387 bs->change_cb(bs->change_opaque);
391 void bdrv_delete(BlockDriverState *bs)
393 /* XXX: remove the driver list */
398 /* commit COW file into the raw image */
399 int bdrv_commit(BlockDriverState *bs)
401 int64_t i, total_sectors;
403 unsigned char sector[512];
412 if (!bs->backing_hd) {
416 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
417 for (i = 0; i < total_sectors;) {
418 if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) {
419 for(j = 0; j < n; j++) {
420 if (bdrv_read(bs, i, sector, 1) != 0) {
424 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
434 if (bs->drv->bdrv_make_empty)
435 return bs->drv->bdrv_make_empty(bs);
440 /* return < 0 if error */
441 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
442 uint8_t *buf, int nb_sectors)
444 BlockDriver *drv = bs->drv;
449 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
450 memcpy(buf, bs->boot_sector_data, 512);
457 if (drv->bdrv_pread) {
459 len = nb_sectors * 512;
460 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
468 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
472 /* return < 0 if error */
473 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
474 const uint8_t *buf, int nb_sectors)
476 BlockDriver *drv = bs->drv;
481 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
482 memcpy(bs->boot_sector_data, buf, 512);
484 if (drv->bdrv_pwrite) {
486 len = nb_sectors * 512;
487 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
495 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
499 /* not necessary now */
500 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
501 uint8_t *buf, int count1)
503 uint8_t tmp_buf[SECTOR_SIZE];
504 int len, nb_sectors, count;
508 /* first read to align to sector start */
509 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
512 sector_num = offset >> SECTOR_BITS;
514 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
516 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
524 /* read the sectors "in place" */
525 nb_sectors = count >> SECTOR_BITS;
526 if (nb_sectors > 0) {
527 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
529 sector_num += nb_sectors;
530 len = nb_sectors << SECTOR_BITS;
535 /* add data from the last sector */
537 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
539 memcpy(buf, tmp_buf, count);
544 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
545 const uint8_t *buf, int count1)
547 uint8_t tmp_buf[SECTOR_SIZE];
548 int len, nb_sectors, count;
552 /* first write to align to sector start */
553 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
556 sector_num = offset >> SECTOR_BITS;
558 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
560 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
561 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
570 /* write the sectors "in place" */
571 nb_sectors = count >> SECTOR_BITS;
572 if (nb_sectors > 0) {
573 if (bdrv_write(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(tmp_buf, buf, count);
586 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
593 * Read with byte offsets (needed only for file protocols)
595 int bdrv_pread(BlockDriverState *bs, int64_t offset,
596 void *buf1, int count1)
598 BlockDriver *drv = bs->drv;
602 if (!drv->bdrv_pread)
603 return bdrv_pread_em(bs, offset, buf1, count1);
604 return drv->bdrv_pread(bs, offset, buf1, count1);
608 * Write with byte offsets (needed only for file protocols)
610 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
611 const void *buf1, int count1)
613 BlockDriver *drv = bs->drv;
617 if (!drv->bdrv_pwrite)
618 return bdrv_pwrite_em(bs, offset, buf1, count1);
619 return drv->bdrv_pwrite(bs, offset, buf1, count1);
623 * Truncate file to 'offset' bytes (needed only for file protocols)
625 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
627 BlockDriver *drv = bs->drv;
630 if (!drv->bdrv_truncate)
632 return drv->bdrv_truncate(bs, offset);
636 * Length of a file in bytes. Return < 0 if error or unknown.
638 int64_t bdrv_getlength(BlockDriverState *bs)
640 BlockDriver *drv = bs->drv;
643 if (!drv->bdrv_getlength) {
645 return bs->total_sectors * SECTOR_SIZE;
647 return drv->bdrv_getlength(bs);
650 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
652 *nb_sectors_ptr = bs->total_sectors;
655 /* force a given boot sector. */
656 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
658 bs->boot_sector_enabled = 1;
661 memcpy(bs->boot_sector_data, data, size);
662 memset(bs->boot_sector_data + size, 0, 512 - size);
665 void bdrv_set_geometry_hint(BlockDriverState *bs,
666 int cyls, int heads, int secs)
673 void bdrv_set_type_hint(BlockDriverState *bs, int type)
676 bs->removable = ((type == BDRV_TYPE_CDROM ||
677 type == BDRV_TYPE_FLOPPY));
680 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
682 bs->translation = translation;
685 void bdrv_get_geometry_hint(BlockDriverState *bs,
686 int *pcyls, int *pheads, int *psecs)
693 int bdrv_get_type_hint(BlockDriverState *bs)
698 int bdrv_get_translation_hint(BlockDriverState *bs)
700 return bs->translation;
703 int bdrv_is_removable(BlockDriverState *bs)
705 return bs->removable;
708 int bdrv_is_read_only(BlockDriverState *bs)
710 return bs->read_only;
713 int bdrv_is_inserted(BlockDriverState *bs)
718 int bdrv_is_locked(BlockDriverState *bs)
723 void bdrv_set_locked(BlockDriverState *bs, int locked)
728 void bdrv_set_change_cb(BlockDriverState *bs,
729 void (*change_cb)(void *opaque), void *opaque)
731 bs->change_cb = change_cb;
732 bs->change_opaque = opaque;
735 int bdrv_is_encrypted(BlockDriverState *bs)
737 if (bs->backing_hd && bs->backing_hd->encrypted)
739 return bs->encrypted;
742 int bdrv_set_key(BlockDriverState *bs, const char *key)
745 if (bs->backing_hd && bs->backing_hd->encrypted) {
746 ret = bdrv_set_key(bs->backing_hd, key);
752 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
754 return bs->drv->bdrv_set_key(bs, key);
757 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
759 if (!bs->inserted || !bs->drv) {
762 pstrcpy(buf, buf_size, bs->drv->format_name);
766 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
771 for (drv = first_drv; drv != NULL; drv = drv->next) {
772 it(opaque, drv->format_name);
776 BlockDriverState *bdrv_find(const char *name)
778 BlockDriverState *bs;
780 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
781 if (!strcmp(name, bs->device_name))
787 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
789 BlockDriverState *bs;
791 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
792 it(opaque, bs->device_name);
796 const char *bdrv_get_device_name(BlockDriverState *bs)
798 return bs->device_name;
801 void bdrv_flush(BlockDriverState *bs)
803 if (bs->drv->bdrv_flush)
804 bs->drv->bdrv_flush(bs);
806 bdrv_flush(bs->backing_hd);
811 BlockDriverState *bs;
813 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
814 term_printf("%s:", bs->device_name);
815 term_printf(" type=");
820 case BDRV_TYPE_CDROM:
821 term_printf("cdrom");
823 case BDRV_TYPE_FLOPPY:
824 term_printf("floppy");
827 term_printf(" removable=%d", bs->removable);
829 term_printf(" locked=%d", bs->locked);
832 term_printf(" file=%s", bs->filename);
833 if (bs->backing_file[0] != '\0')
834 term_printf(" backing_file=%s", bs->backing_file);
835 term_printf(" ro=%d", bs->read_only);
836 term_printf(" drv=%s", bs->drv->format_name);
838 term_printf(" encrypted");
840 term_printf(" [not inserted]");
846 void bdrv_get_backing_filename(BlockDriverState *bs,
847 char *filename, int filename_size)
849 if (!bs->backing_hd) {
850 pstrcpy(filename, filename_size, "");
852 pstrcpy(filename, filename_size, bs->backing_file);
856 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
857 const uint8_t *buf, int nb_sectors)
859 BlockDriver *drv = bs->drv;
862 if (!drv->bdrv_write_compressed)
864 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
867 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
869 BlockDriver *drv = bs->drv;
872 if (!drv->bdrv_get_info)
874 memset(bdi, 0, sizeof(*bdi));
875 return drv->bdrv_get_info(bs, bdi);
878 /**************************************************************/
879 /* handling of snapshots */
881 int bdrv_snapshot_create(BlockDriverState *bs,
882 QEMUSnapshotInfo *sn_info)
884 BlockDriver *drv = bs->drv;
887 if (!drv->bdrv_snapshot_create)
889 return drv->bdrv_snapshot_create(bs, sn_info);
892 int bdrv_snapshot_goto(BlockDriverState *bs,
893 const char *snapshot_id)
895 BlockDriver *drv = bs->drv;
898 if (!drv->bdrv_snapshot_goto)
900 return drv->bdrv_snapshot_goto(bs, snapshot_id);
903 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
905 BlockDriver *drv = bs->drv;
908 if (!drv->bdrv_snapshot_delete)
910 return drv->bdrv_snapshot_delete(bs, snapshot_id);
913 int bdrv_snapshot_list(BlockDriverState *bs,
914 QEMUSnapshotInfo **psn_info)
916 BlockDriver *drv = bs->drv;
919 if (!drv->bdrv_snapshot_list)
921 return drv->bdrv_snapshot_list(bs, psn_info);
924 #define NB_SUFFIXES 4
926 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
928 static const char suffixes[NB_SUFFIXES] = "KMGT";
933 snprintf(buf, buf_size, "%" PRId64, size);
936 for(i = 0; i < NB_SUFFIXES; i++) {
937 if (size < (10 * base)) {
938 snprintf(buf, buf_size, "%0.1f%c",
942 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
943 snprintf(buf, buf_size, "%" PRId64 "%c",
944 ((size + (base >> 1)) / base),
954 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
956 char buf1[128], date_buf[128], clock_buf[128];
962 snprintf(buf, buf_size,
963 "%-10s%-20s%7s%20s%15s",
964 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
967 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_new(BlockDriverState *bs)
993 BlockDriver *drv = bs->drv;
994 BlockDriverAIOCB *acb;
995 acb = qemu_mallocz(sizeof(BlockDriverAIOCB));
1000 if (drv->bdrv_aio_new(acb) < 0) {
1007 int bdrv_aio_read(BlockDriverAIOCB *acb, int64_t sector_num,
1008 uint8_t *buf, int nb_sectors,
1009 BlockDriverCompletionFunc *cb, void *opaque)
1011 BlockDriverState *bs = acb->bs;
1012 BlockDriver *drv = bs->drv;
1017 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1018 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1019 memcpy(buf, bs->boot_sector_data, 512);
1026 acb->cb_opaque = opaque;
1027 return drv->bdrv_aio_read(acb, sector_num, buf, nb_sectors);
1030 int bdrv_aio_write(BlockDriverAIOCB *acb, int64_t sector_num,
1031 const uint8_t *buf, int nb_sectors,
1032 BlockDriverCompletionFunc *cb, void *opaque)
1034 BlockDriverState *bs = acb->bs;
1035 BlockDriver *drv = bs->drv;
1041 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1042 memcpy(bs->boot_sector_data, buf, 512);
1046 acb->cb_opaque = opaque;
1047 return drv->bdrv_aio_write(acb, sector_num, buf, nb_sectors);
1050 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1052 BlockDriverState *bs = acb->bs;
1053 BlockDriver *drv = bs->drv;
1055 drv->bdrv_aio_cancel(acb);
1058 void bdrv_aio_delete(BlockDriverAIOCB *acb)
1060 BlockDriverState *bs = acb->bs;
1061 BlockDriver *drv = bs->drv;
1063 drv->bdrv_aio_delete(acb);
1067 /**************************************************************/
1068 /* async block device emulation */
1071 static int bdrv_aio_new_em(BlockDriverAIOCB *acb)
1076 static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num,
1077 uint8_t *buf, int nb_sectors)
1080 ret = bdrv_read(acb->bs, sector_num, buf, nb_sectors);
1081 acb->cb(acb->cb_opaque, ret);
1085 static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num,
1086 const uint8_t *buf, int nb_sectors)
1089 ret = bdrv_write(acb->bs, sector_num, buf, nb_sectors);
1090 acb->cb(acb->cb_opaque, ret);
1094 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1098 static void bdrv_aio_delete_em(BlockDriverAIOCB *acb)
1102 typedef struct BlockDriverAIOCBSync {
1105 } BlockDriverAIOCBSync;
1107 static void bdrv_aio_bh_cb(void *opaque)
1109 BlockDriverAIOCB *acb = opaque;
1110 BlockDriverAIOCBSync *acb1 = acb->opaque;
1111 acb->cb(acb->cb_opaque, acb1->ret);
1114 static int bdrv_aio_new_em(BlockDriverAIOCB *acb)
1116 BlockDriverAIOCBSync *acb1;
1118 acb1 = qemu_mallocz(sizeof(BlockDriverAIOCBSync));
1122 acb1->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1126 static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num,
1127 uint8_t *buf, int nb_sectors)
1129 BlockDriverAIOCBSync *acb1 = acb->opaque;
1132 ret = bdrv_read(acb->bs, sector_num, buf, nb_sectors);
1134 qemu_bh_schedule(acb1->bh);
1138 static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num,
1139 const uint8_t *buf, int nb_sectors)
1141 BlockDriverAIOCBSync *acb1 = acb->opaque;
1144 ret = bdrv_write(acb->bs, sector_num, buf, nb_sectors);
1146 qemu_bh_schedule(acb1->bh);
1150 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1152 BlockDriverAIOCBSync *acb1 = acb->opaque;
1153 qemu_bh_cancel(acb1->bh);
1156 static void bdrv_aio_delete_em(BlockDriverAIOCB *acb)
1158 BlockDriverAIOCBSync *acb1 = acb->opaque;
1159 qemu_bh_delete(acb1->bh);
1161 #endif /* !QEMU_TOOL */
1163 /**************************************************************/
1164 /* sync block device emulation */
1166 static void bdrv_rw_em_cb(void *opaque, int ret)
1168 *(int *)opaque = ret;
1171 #define NOT_DONE 0x7fffffff
1173 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1174 uint8_t *buf, int nb_sectors)
1178 if (!bs->sync_aiocb) {
1179 bs->sync_aiocb = bdrv_aio_new(bs);
1180 if (!bs->sync_aiocb)
1183 async_ret = NOT_DONE;
1184 qemu_aio_wait_start();
1185 ret = bdrv_aio_read(bs->sync_aiocb, sector_num, buf, nb_sectors,
1186 bdrv_rw_em_cb, &async_ret);
1188 qemu_aio_wait_end();
1191 while (async_ret == NOT_DONE) {
1194 qemu_aio_wait_end();
1198 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1199 const uint8_t *buf, int nb_sectors)
1203 if (!bs->sync_aiocb) {
1204 bs->sync_aiocb = bdrv_aio_new(bs);
1205 if (!bs->sync_aiocb)
1208 async_ret = NOT_DONE;
1209 qemu_aio_wait_start();
1210 ret = bdrv_aio_write(bs->sync_aiocb, sector_num, buf, nb_sectors,
1211 bdrv_rw_em_cb, &async_ret);
1213 qemu_aio_wait_end();
1216 while (async_ret == NOT_DONE) {
1219 qemu_aio_wait_end();
1223 void bdrv_init(void)
1225 bdrv_register(&bdrv_raw);
1227 bdrv_register(&bdrv_cow);
1229 bdrv_register(&bdrv_qcow);
1230 bdrv_register(&bdrv_vmdk);
1231 bdrv_register(&bdrv_cloop);
1232 bdrv_register(&bdrv_dmg);
1233 bdrv_register(&bdrv_bochs);
1234 bdrv_register(&bdrv_vpc);
1235 bdrv_register(&bdrv_vvfat);
1236 bdrv_register(&bdrv_qcow2);