2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "block_int.h"
28 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/queue.h>
36 #define SECTOR_SIZE (1 << SECTOR_BITS)
38 typedef struct BlockDriverAIOCBSync {
39 BlockDriverAIOCB common;
42 } BlockDriverAIOCBSync;
44 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
45 int64_t sector_num, uint8_t *buf, int nb_sectors,
46 BlockDriverCompletionFunc *cb, void *opaque);
47 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
48 int64_t sector_num, const uint8_t *buf, int nb_sectors,
49 BlockDriverCompletionFunc *cb, void *opaque);
50 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
51 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
52 uint8_t *buf, int nb_sectors);
53 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
54 const uint8_t *buf, int nb_sectors);
56 static BlockDriverState *bdrv_first;
57 static BlockDriver *first_drv;
65 int path_is_absolute(const char *path)
68 p = strchr(path, ':');
73 return (*p == PATH_SEP);
76 /* if filename is absolute, just copy it to dest. Otherwise, build a
77 path to it by considering it is relative to base_path. URL are
79 void path_combine(char *dest, int dest_size,
80 const char *base_path,
88 if (path_is_absolute(filename)) {
89 pstrcpy(dest, dest_size, filename);
91 p = strchr(base_path, ':');
96 p1 = strrchr(base_path, PATH_SEP);
104 if (len > dest_size - 1)
106 memcpy(dest, base_path, len);
108 pstrcat(dest, dest_size, filename);
113 void bdrv_register(BlockDriver *bdrv)
115 if (!bdrv->bdrv_aio_read) {
116 /* add AIO emulation layer */
117 bdrv->bdrv_aio_read = bdrv_aio_read_em;
118 bdrv->bdrv_aio_write = bdrv_aio_write_em;
119 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
120 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
121 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
122 /* add synchronous IO emulation layer */
123 bdrv->bdrv_read = bdrv_read_em;
124 bdrv->bdrv_write = bdrv_write_em;
126 bdrv->next = first_drv;
130 /* create a new block device (by default it is empty) */
131 BlockDriverState *bdrv_new(const char *device_name)
133 BlockDriverState **pbs, *bs;
135 bs = qemu_mallocz(sizeof(BlockDriverState));
138 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
139 if (device_name[0] != '\0') {
140 /* insert at the end */
149 BlockDriver *bdrv_find_format(const char *format_name)
152 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
153 if (!strcmp(drv1->format_name, format_name))
159 int bdrv_create(BlockDriver *drv,
160 const char *filename, int64_t size_in_sectors,
161 const char *backing_file, int flags)
163 if (!drv->bdrv_create)
165 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
169 void get_tmp_filename(char *filename, int size)
174 void get_tmp_filename(char *filename, int size)
177 /* XXX: race condition possible */
178 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
179 fd = mkstemp(filename);
185 static int is_windows_drive(const char *filename)
187 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
188 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
189 filename[1] == ':' && filename[2] == '\0')
191 if (strstart(filename, "\\\\.\\", NULL) ||
192 strstart(filename, "//./", NULL))
198 static BlockDriver *find_protocol(const char *filename)
206 if (is_windows_drive(filename))
209 p = strchr(filename, ':');
213 if (len > sizeof(protocol) - 1)
214 len = sizeof(protocol) - 1;
215 memcpy(protocol, filename, len);
216 protocol[len] = '\0';
217 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
218 if (drv1->protocol_name &&
219 !strcmp(drv1->protocol_name, protocol))
225 /* XXX: force raw format if block or character device ? It would
226 simplify the BSD case */
227 static BlockDriver *find_image_format(const char *filename)
229 int ret, score, score_max;
230 BlockDriver *drv1, *drv;
232 BlockDriverState *bs;
234 /* detect host devices. By convention, /dev/cdrom[N] is always
235 recognized as a host CDROM */
236 if (strstart(filename, "/dev/cdrom", NULL))
237 return &bdrv_host_device;
239 if (is_windows_drive(filename))
240 return &bdrv_host_device;
244 if (stat(filename, &st) >= 0 &&
245 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
246 return &bdrv_host_device;
251 drv = find_protocol(filename);
252 /* no need to test disk image formats for vvfat */
253 if (drv == &bdrv_vvfat)
256 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
259 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
266 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
267 if (drv1->bdrv_probe) {
268 score = drv1->bdrv_probe(buf, ret, filename);
269 if (score > score_max) {
278 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
280 BlockDriverState *bs;
286 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
295 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
297 return bdrv_open2(bs, filename, flags, NULL);
300 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
304 char tmp_filename[1024];
305 char backing_filename[1024];
308 bs->is_temporary = 0;
311 if (flags & BDRV_O_SNAPSHOT) {
312 BlockDriverState *bs1;
315 /* if snapshot, we create a temporary backing file and open it
316 instead of opening 'filename' directly */
318 /* if there is a backing file, use it */
323 if (bdrv_open(bs1, filename, 0) < 0) {
327 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
330 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
331 if (bdrv_create(&bdrv_qcow2, tmp_filename,
332 total_size, filename, 0) < 0) {
335 filename = tmp_filename;
336 bs->is_temporary = 1;
339 pstrcpy(bs->filename, sizeof(bs->filename), filename);
340 if (flags & BDRV_O_FILE) {
341 drv = find_protocol(filename);
346 drv = find_image_format(filename);
352 bs->opaque = qemu_mallocz(drv->instance_size);
353 if (bs->opaque == NULL && drv->instance_size > 0)
355 /* Note: for compatibility, we open disk image files as RDWR, and
356 RDONLY as fallback */
357 if (!(flags & BDRV_O_FILE))
358 open_flags = BDRV_O_RDWR;
360 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
361 ret = drv->bdrv_open(bs, filename, open_flags);
362 if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
363 ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
367 qemu_free(bs->opaque);
370 if (drv->bdrv_getlength) {
371 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
374 if (bs->is_temporary) {
378 if (bs->backing_file[0] != '\0') {
379 /* if there is a backing file, use it */
380 bs->backing_hd = bdrv_new("");
381 if (!bs->backing_hd) {
386 path_combine(backing_filename, sizeof(backing_filename),
387 filename, bs->backing_file);
388 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
392 /* call the change callback */
393 bs->media_changed = 1;
395 bs->change_cb(bs->change_opaque);
400 void bdrv_close(BlockDriverState *bs)
404 bdrv_delete(bs->backing_hd);
405 bs->drv->bdrv_close(bs);
406 qemu_free(bs->opaque);
408 if (bs->is_temporary) {
409 unlink(bs->filename);
415 /* call the change callback */
416 bs->media_changed = 1;
418 bs->change_cb(bs->change_opaque);
422 void bdrv_delete(BlockDriverState *bs)
424 /* XXX: remove the driver list */
429 /* commit COW file into the raw image */
430 int bdrv_commit(BlockDriverState *bs)
432 BlockDriver *drv = bs->drv;
433 int64_t i, total_sectors;
435 unsigned char sector[512];
444 if (!bs->backing_hd) {
448 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
449 for (i = 0; i < total_sectors;) {
450 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
451 for(j = 0; j < n; j++) {
452 if (bdrv_read(bs, i, sector, 1) != 0) {
456 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
466 if (drv->bdrv_make_empty)
467 return drv->bdrv_make_empty(bs);
472 /* return < 0 if error. See bdrv_write() for the return codes */
473 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
474 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(buf, bs->boot_sector_data, 512);
489 if (drv->bdrv_pread) {
491 len = nb_sectors * 512;
492 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
500 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
504 /* Return < 0 if error. Important errors are:
505 -EIO generic I/O error (may happen for all errors)
506 -ENOMEDIUM No media inserted.
507 -EINVAL Invalid sector number or nb_sectors
508 -EACCES Trying to write a read-only device
510 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
511 const uint8_t *buf, int nb_sectors)
513 BlockDriver *drv = bs->drv;
518 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
519 memcpy(bs->boot_sector_data, buf, 512);
521 if (drv->bdrv_pwrite) {
523 len = nb_sectors * 512;
524 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
532 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
536 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
537 uint8_t *buf, int count1)
539 uint8_t tmp_buf[SECTOR_SIZE];
540 int len, nb_sectors, count;
544 /* first read to align to sector start */
545 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
548 sector_num = offset >> SECTOR_BITS;
550 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
552 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
560 /* read the sectors "in place" */
561 nb_sectors = count >> SECTOR_BITS;
562 if (nb_sectors > 0) {
563 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
565 sector_num += nb_sectors;
566 len = nb_sectors << SECTOR_BITS;
571 /* add data from the last sector */
573 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
575 memcpy(buf, tmp_buf, count);
580 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
581 const uint8_t *buf, int count1)
583 uint8_t tmp_buf[SECTOR_SIZE];
584 int len, nb_sectors, count;
588 /* first write to align to sector start */
589 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
592 sector_num = offset >> SECTOR_BITS;
594 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
596 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
597 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
606 /* write the sectors "in place" */
607 nb_sectors = count >> SECTOR_BITS;
608 if (nb_sectors > 0) {
609 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
611 sector_num += nb_sectors;
612 len = nb_sectors << SECTOR_BITS;
617 /* add data from the last sector */
619 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
621 memcpy(tmp_buf, buf, count);
622 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
629 * Read with byte offsets (needed only for file protocols)
631 int bdrv_pread(BlockDriverState *bs, int64_t offset,
632 void *buf1, int count1)
634 BlockDriver *drv = bs->drv;
638 if (!drv->bdrv_pread)
639 return bdrv_pread_em(bs, offset, buf1, count1);
640 return drv->bdrv_pread(bs, offset, buf1, count1);
644 * Write with byte offsets (needed only for file protocols)
646 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
647 const void *buf1, int count1)
649 BlockDriver *drv = bs->drv;
653 if (!drv->bdrv_pwrite)
654 return bdrv_pwrite_em(bs, offset, buf1, count1);
655 return drv->bdrv_pwrite(bs, offset, buf1, count1);
659 * Truncate file to 'offset' bytes (needed only for file protocols)
661 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
663 BlockDriver *drv = bs->drv;
666 if (!drv->bdrv_truncate)
668 return drv->bdrv_truncate(bs, offset);
672 * Length of a file in bytes. Return < 0 if error or unknown.
674 int64_t bdrv_getlength(BlockDriverState *bs)
676 BlockDriver *drv = bs->drv;
679 if (!drv->bdrv_getlength) {
681 return bs->total_sectors * SECTOR_SIZE;
683 return drv->bdrv_getlength(bs);
686 /* return 0 as number of sectors if no device present or error */
687 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
690 length = bdrv_getlength(bs);
694 length = length >> SECTOR_BITS;
695 *nb_sectors_ptr = length;
698 /* force a given boot sector. */
699 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
701 bs->boot_sector_enabled = 1;
704 memcpy(bs->boot_sector_data, data, size);
705 memset(bs->boot_sector_data + size, 0, 512 - size);
708 void bdrv_set_geometry_hint(BlockDriverState *bs,
709 int cyls, int heads, int secs)
716 void bdrv_set_type_hint(BlockDriverState *bs, int type)
719 bs->removable = ((type == BDRV_TYPE_CDROM ||
720 type == BDRV_TYPE_FLOPPY));
723 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
725 bs->translation = translation;
728 void bdrv_get_geometry_hint(BlockDriverState *bs,
729 int *pcyls, int *pheads, int *psecs)
736 int bdrv_get_type_hint(BlockDriverState *bs)
741 int bdrv_get_translation_hint(BlockDriverState *bs)
743 return bs->translation;
746 int bdrv_is_removable(BlockDriverState *bs)
748 return bs->removable;
751 int bdrv_is_read_only(BlockDriverState *bs)
753 return bs->read_only;
756 /* XXX: no longer used */
757 void bdrv_set_change_cb(BlockDriverState *bs,
758 void (*change_cb)(void *opaque), void *opaque)
760 bs->change_cb = change_cb;
761 bs->change_opaque = opaque;
764 int bdrv_is_encrypted(BlockDriverState *bs)
766 if (bs->backing_hd && bs->backing_hd->encrypted)
768 return bs->encrypted;
771 int bdrv_set_key(BlockDriverState *bs, const char *key)
774 if (bs->backing_hd && bs->backing_hd->encrypted) {
775 ret = bdrv_set_key(bs->backing_hd, key);
781 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
783 return bs->drv->bdrv_set_key(bs, key);
786 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
791 pstrcpy(buf, buf_size, bs->drv->format_name);
795 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
800 for (drv = first_drv; drv != NULL; drv = drv->next) {
801 it(opaque, drv->format_name);
805 BlockDriverState *bdrv_find(const char *name)
807 BlockDriverState *bs;
809 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
810 if (!strcmp(name, bs->device_name))
816 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
818 BlockDriverState *bs;
820 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
821 it(opaque, bs->device_name);
825 const char *bdrv_get_device_name(BlockDriverState *bs)
827 return bs->device_name;
830 void bdrv_flush(BlockDriverState *bs)
832 if (bs->drv->bdrv_flush)
833 bs->drv->bdrv_flush(bs);
835 bdrv_flush(bs->backing_hd);
840 BlockDriverState *bs;
842 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
843 term_printf("%s:", bs->device_name);
844 term_printf(" type=");
849 case BDRV_TYPE_CDROM:
850 term_printf("cdrom");
852 case BDRV_TYPE_FLOPPY:
853 term_printf("floppy");
856 term_printf(" removable=%d", bs->removable);
858 term_printf(" locked=%d", bs->locked);
861 term_printf(" file=%s", bs->filename);
862 if (bs->backing_file[0] != '\0')
863 term_printf(" backing_file=%s", bs->backing_file);
864 term_printf(" ro=%d", bs->read_only);
865 term_printf(" drv=%s", bs->drv->format_name);
867 term_printf(" encrypted");
869 term_printf(" [not inserted]");
875 void bdrv_get_backing_filename(BlockDriverState *bs,
876 char *filename, int filename_size)
878 if (!bs->backing_hd) {
879 pstrcpy(filename, filename_size, "");
881 pstrcpy(filename, filename_size, bs->backing_file);
885 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
886 const uint8_t *buf, int nb_sectors)
888 BlockDriver *drv = bs->drv;
891 if (!drv->bdrv_write_compressed)
893 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
896 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
898 BlockDriver *drv = bs->drv;
901 if (!drv->bdrv_get_info)
903 memset(bdi, 0, sizeof(*bdi));
904 return drv->bdrv_get_info(bs, bdi);
907 /**************************************************************/
908 /* handling of snapshots */
910 int bdrv_snapshot_create(BlockDriverState *bs,
911 QEMUSnapshotInfo *sn_info)
913 BlockDriver *drv = bs->drv;
916 if (!drv->bdrv_snapshot_create)
918 return drv->bdrv_snapshot_create(bs, sn_info);
921 int bdrv_snapshot_goto(BlockDriverState *bs,
922 const char *snapshot_id)
924 BlockDriver *drv = bs->drv;
927 if (!drv->bdrv_snapshot_goto)
929 return drv->bdrv_snapshot_goto(bs, snapshot_id);
932 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
934 BlockDriver *drv = bs->drv;
937 if (!drv->bdrv_snapshot_delete)
939 return drv->bdrv_snapshot_delete(bs, snapshot_id);
942 int bdrv_snapshot_list(BlockDriverState *bs,
943 QEMUSnapshotInfo **psn_info)
945 BlockDriver *drv = bs->drv;
948 if (!drv->bdrv_snapshot_list)
950 return drv->bdrv_snapshot_list(bs, psn_info);
953 #define NB_SUFFIXES 4
955 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
957 static const char suffixes[NB_SUFFIXES] = "KMGT";
962 snprintf(buf, buf_size, "%" PRId64, size);
965 for(i = 0; i < NB_SUFFIXES; i++) {
966 if (size < (10 * base)) {
967 snprintf(buf, buf_size, "%0.1f%c",
971 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
972 snprintf(buf, buf_size, "%" PRId64 "%c",
973 ((size + (base >> 1)) / base),
983 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
985 char buf1[128], date_buf[128], clock_buf[128];
991 snprintf(buf, buf_size,
992 "%-10s%-20s%7s%20s%15s",
993 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
997 localtime_r(&ti, &tm);
999 strftime(date_buf, sizeof(date_buf),
1000 "%Y-%m-%d %H:%M:%S", &tm);
1001 secs = sn->vm_clock_nsec / 1000000000;
1002 snprintf(clock_buf, sizeof(clock_buf),
1003 "%02d:%02d:%02d.%03d",
1005 (int)((secs / 60) % 60),
1007 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1008 snprintf(buf, buf_size,
1009 "%-10s%-20s%7s%20s%15s",
1010 sn->id_str, sn->name,
1011 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1019 /**************************************************************/
1022 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1023 uint8_t *buf, int nb_sectors,
1024 BlockDriverCompletionFunc *cb, void *opaque)
1026 BlockDriver *drv = bs->drv;
1031 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1032 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1033 memcpy(buf, bs->boot_sector_data, 512);
1039 return drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1042 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1043 const uint8_t *buf, int nb_sectors,
1044 BlockDriverCompletionFunc *cb, void *opaque)
1046 BlockDriver *drv = bs->drv;
1052 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1053 memcpy(bs->boot_sector_data, buf, 512);
1056 return drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1059 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1061 BlockDriver *drv = acb->bs->drv;
1063 drv->bdrv_aio_cancel(acb);
1067 /**************************************************************/
1068 /* async block device emulation */
1071 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1072 int64_t sector_num, uint8_t *buf, int nb_sectors,
1073 BlockDriverCompletionFunc *cb, void *opaque)
1076 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1081 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1082 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1083 BlockDriverCompletionFunc *cb, void *opaque)
1086 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1091 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1095 static void bdrv_aio_bh_cb(void *opaque)
1097 BlockDriverAIOCBSync *acb = opaque;
1098 acb->common.cb(acb->common.opaque, acb->ret);
1099 qemu_aio_release(acb);
1102 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1103 int64_t sector_num, uint8_t *buf, int nb_sectors,
1104 BlockDriverCompletionFunc *cb, void *opaque)
1106 BlockDriverAIOCBSync *acb;
1109 acb = qemu_aio_get(bs, cb, opaque);
1111 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1112 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1114 qemu_bh_schedule(acb->bh);
1115 return &acb->common;
1118 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1119 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1120 BlockDriverCompletionFunc *cb, void *opaque)
1122 BlockDriverAIOCBSync *acb;
1125 acb = qemu_aio_get(bs, cb, opaque);
1127 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1128 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1130 qemu_bh_schedule(acb->bh);
1131 return &acb->common;
1134 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1136 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1137 qemu_bh_cancel(acb->bh);
1138 qemu_aio_release(acb);
1140 #endif /* !QEMU_TOOL */
1142 /**************************************************************/
1143 /* sync block device emulation */
1145 static void bdrv_rw_em_cb(void *opaque, int ret)
1147 *(int *)opaque = ret;
1150 #define NOT_DONE 0x7fffffff
1152 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1153 uint8_t *buf, int nb_sectors)
1156 BlockDriverAIOCB *acb;
1158 async_ret = NOT_DONE;
1159 qemu_aio_wait_start();
1160 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1161 bdrv_rw_em_cb, &async_ret);
1163 qemu_aio_wait_end();
1166 while (async_ret == NOT_DONE) {
1169 qemu_aio_wait_end();
1173 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1174 const uint8_t *buf, int nb_sectors)
1177 BlockDriverAIOCB *acb;
1179 async_ret = NOT_DONE;
1180 qemu_aio_wait_start();
1181 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1182 bdrv_rw_em_cb, &async_ret);
1184 qemu_aio_wait_end();
1187 while (async_ret == NOT_DONE) {
1190 qemu_aio_wait_end();
1194 void bdrv_init(void)
1196 bdrv_register(&bdrv_raw);
1197 bdrv_register(&bdrv_host_device);
1199 bdrv_register(&bdrv_cow);
1201 bdrv_register(&bdrv_qcow);
1202 bdrv_register(&bdrv_vmdk);
1203 bdrv_register(&bdrv_cloop);
1204 bdrv_register(&bdrv_dmg);
1205 bdrv_register(&bdrv_bochs);
1206 bdrv_register(&bdrv_vpc);
1207 bdrv_register(&bdrv_vvfat);
1208 bdrv_register(&bdrv_qcow2);
1211 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1215 BlockDriverAIOCB *acb;
1218 if (drv->free_aiocb) {
1219 acb = drv->free_aiocb;
1220 drv->free_aiocb = acb->next;
1222 acb = qemu_mallocz(drv->aiocb_size);
1228 acb->opaque = opaque;
1232 void qemu_aio_release(void *p)
1234 BlockDriverAIOCB *acb = p;
1235 BlockDriver *drv = acb->bs->drv;
1236 acb->next = drv->free_aiocb;
1237 drv->free_aiocb = acb;
1240 /**************************************************************/
1241 /* removable device support */
1244 * Return TRUE if the media is present
1246 int bdrv_is_inserted(BlockDriverState *bs)
1248 BlockDriver *drv = bs->drv;
1252 if (!drv->bdrv_is_inserted)
1254 ret = drv->bdrv_is_inserted(bs);
1259 * Return TRUE if the media changed since the last call to this
1260 * function. It is currently only used for floppy disks
1262 int bdrv_media_changed(BlockDriverState *bs)
1264 BlockDriver *drv = bs->drv;
1267 if (!drv || !drv->bdrv_media_changed)
1270 ret = drv->bdrv_media_changed(bs);
1271 if (ret == -ENOTSUP)
1272 ret = bs->media_changed;
1273 bs->media_changed = 0;
1278 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1280 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1282 BlockDriver *drv = bs->drv;
1285 if (!drv || !drv->bdrv_eject) {
1288 ret = drv->bdrv_eject(bs, eject_flag);
1290 if (ret == -ENOTSUP) {
1296 int bdrv_is_locked(BlockDriverState *bs)
1302 * Lock or unlock the media (if it is locked, the user won't be able
1303 * to eject it manually).
1305 void bdrv_set_locked(BlockDriverState *bs, int locked)
1307 BlockDriver *drv = bs->drv;
1309 bs->locked = locked;
1310 if (drv && drv->bdrv_set_locked) {
1311 drv->bdrv_set_locked(bs, locked);