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;
59 int path_is_absolute(const char *path)
62 p = strchr(path, ':');
68 return (*p == '/' || *p == '\\');
74 /* if filename is absolute, just copy it to dest. Otherwise, build a
75 path to it by considering it is relative to base_path. URL are
77 void path_combine(char *dest, int dest_size,
78 const char *base_path,
86 if (path_is_absolute(filename)) {
87 pstrcpy(dest, dest_size, filename);
89 p = strchr(base_path, ':');
94 p1 = strrchr(base_path, '/');
98 p2 = strrchr(base_path, '\\');
110 if (len > dest_size - 1)
112 memcpy(dest, base_path, len);
114 pstrcat(dest, dest_size, filename);
119 void bdrv_register(BlockDriver *bdrv)
121 if (!bdrv->bdrv_aio_read) {
122 /* add AIO emulation layer */
123 bdrv->bdrv_aio_read = bdrv_aio_read_em;
124 bdrv->bdrv_aio_write = bdrv_aio_write_em;
125 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
126 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
127 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
128 /* add synchronous IO emulation layer */
129 bdrv->bdrv_read = bdrv_read_em;
130 bdrv->bdrv_write = bdrv_write_em;
132 bdrv->next = first_drv;
136 /* create a new block device (by default it is empty) */
137 BlockDriverState *bdrv_new(const char *device_name)
139 BlockDriverState **pbs, *bs;
141 bs = qemu_mallocz(sizeof(BlockDriverState));
144 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
145 if (device_name[0] != '\0') {
146 /* insert at the end */
155 BlockDriver *bdrv_find_format(const char *format_name)
158 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
159 if (!strcmp(drv1->format_name, format_name))
165 int bdrv_create(BlockDriver *drv,
166 const char *filename, int64_t size_in_sectors,
167 const char *backing_file, int flags)
169 if (!drv->bdrv_create)
171 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
175 void get_tmp_filename(char *filename, int size)
177 char temp_dir[MAX_PATH];
179 GetTempPath(MAX_PATH, temp_dir);
180 GetTempFileName(temp_dir, "qem", 0, filename);
183 void get_tmp_filename(char *filename, int size)
186 /* XXX: race condition possible */
187 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
188 fd = mkstemp(filename);
194 static int is_windows_drive_prefix(const char *filename)
196 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
197 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
201 static int is_windows_drive(const char *filename)
203 if (is_windows_drive_prefix(filename) &&
206 if (strstart(filename, "\\\\.\\", NULL) ||
207 strstart(filename, "//./", NULL))
213 static BlockDriver *find_protocol(const char *filename)
221 if (is_windows_drive(filename) ||
222 is_windows_drive_prefix(filename))
225 p = strchr(filename, ':');
229 if (len > sizeof(protocol) - 1)
230 len = sizeof(protocol) - 1;
231 memcpy(protocol, filename, len);
232 protocol[len] = '\0';
233 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
234 if (drv1->protocol_name &&
235 !strcmp(drv1->protocol_name, protocol))
241 /* XXX: force raw format if block or character device ? It would
242 simplify the BSD case */
243 static BlockDriver *find_image_format(const char *filename)
245 int ret, score, score_max;
246 BlockDriver *drv1, *drv;
248 BlockDriverState *bs;
250 /* detect host devices. By convention, /dev/cdrom[N] is always
251 recognized as a host CDROM */
252 if (strstart(filename, "/dev/cdrom", NULL))
253 return &bdrv_host_device;
255 if (is_windows_drive(filename))
256 return &bdrv_host_device;
260 if (stat(filename, &st) >= 0 &&
261 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
262 return &bdrv_host_device;
267 drv = find_protocol(filename);
268 /* no need to test disk image formats for vvfat */
269 if (drv == &bdrv_vvfat)
272 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
275 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
282 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
283 if (drv1->bdrv_probe) {
284 score = drv1->bdrv_probe(buf, ret, filename);
285 if (score > score_max) {
294 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
296 BlockDriverState *bs;
302 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
311 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
313 return bdrv_open2(bs, filename, flags, NULL);
316 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
320 char tmp_filename[1024];
321 char backing_filename[1024];
324 bs->is_temporary = 0;
327 if (flags & BDRV_O_SNAPSHOT) {
328 BlockDriverState *bs1;
331 /* if snapshot, we create a temporary backing file and open it
332 instead of opening 'filename' directly */
334 /* if there is a backing file, use it */
339 if (bdrv_open(bs1, filename, 0) < 0) {
343 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
346 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
347 realpath(filename, backing_filename);
348 if (bdrv_create(&bdrv_qcow2, tmp_filename,
349 total_size, backing_filename, 0) < 0) {
352 filename = tmp_filename;
353 bs->is_temporary = 1;
356 pstrcpy(bs->filename, sizeof(bs->filename), filename);
357 if (flags & BDRV_O_FILE) {
358 drv = find_protocol(filename);
363 drv = find_image_format(filename);
369 bs->opaque = qemu_mallocz(drv->instance_size);
370 if (bs->opaque == NULL && drv->instance_size > 0)
372 /* Note: for compatibility, we open disk image files as RDWR, and
373 RDONLY as fallback */
374 if (!(flags & BDRV_O_FILE))
375 open_flags = BDRV_O_RDWR;
377 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
378 ret = drv->bdrv_open(bs, filename, open_flags);
379 if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
380 ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
384 qemu_free(bs->opaque);
389 if (drv->bdrv_getlength) {
390 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
393 if (bs->is_temporary) {
397 if (bs->backing_file[0] != '\0') {
398 /* if there is a backing file, use it */
399 bs->backing_hd = bdrv_new("");
400 if (!bs->backing_hd) {
405 path_combine(backing_filename, sizeof(backing_filename),
406 filename, bs->backing_file);
407 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
411 /* call the change callback */
412 bs->media_changed = 1;
414 bs->change_cb(bs->change_opaque);
419 void bdrv_close(BlockDriverState *bs)
423 bdrv_delete(bs->backing_hd);
424 bs->drv->bdrv_close(bs);
425 qemu_free(bs->opaque);
427 if (bs->is_temporary) {
428 unlink(bs->filename);
434 /* call the change callback */
435 bs->media_changed = 1;
437 bs->change_cb(bs->change_opaque);
441 void bdrv_delete(BlockDriverState *bs)
443 /* XXX: remove the driver list */
448 /* commit COW file into the raw image */
449 int bdrv_commit(BlockDriverState *bs)
451 BlockDriver *drv = bs->drv;
452 int64_t i, total_sectors;
454 unsigned char sector[512];
463 if (!bs->backing_hd) {
467 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
468 for (i = 0; i < total_sectors;) {
469 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
470 for(j = 0; j < n; j++) {
471 if (bdrv_read(bs, i, sector, 1) != 0) {
475 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
485 if (drv->bdrv_make_empty)
486 return drv->bdrv_make_empty(bs);
491 /* return < 0 if error. See bdrv_write() for the return codes */
492 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
493 uint8_t *buf, int nb_sectors)
495 BlockDriver *drv = bs->drv;
500 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
501 memcpy(buf, bs->boot_sector_data, 512);
508 if (drv->bdrv_pread) {
510 len = nb_sectors * 512;
511 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
519 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
523 /* Return < 0 if error. Important errors are:
524 -EIO generic I/O error (may happen for all errors)
525 -ENOMEDIUM No media inserted.
526 -EINVAL Invalid sector number or nb_sectors
527 -EACCES Trying to write a read-only device
529 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
530 const uint8_t *buf, int nb_sectors)
532 BlockDriver *drv = bs->drv;
537 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
538 memcpy(bs->boot_sector_data, buf, 512);
540 if (drv->bdrv_pwrite) {
542 len = nb_sectors * 512;
543 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
551 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
555 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
556 uint8_t *buf, int count1)
558 uint8_t tmp_buf[SECTOR_SIZE];
559 int len, nb_sectors, count;
563 /* first read to align to sector start */
564 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
567 sector_num = offset >> SECTOR_BITS;
569 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
571 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
579 /* read the sectors "in place" */
580 nb_sectors = count >> SECTOR_BITS;
581 if (nb_sectors > 0) {
582 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
584 sector_num += nb_sectors;
585 len = nb_sectors << SECTOR_BITS;
590 /* add data from the last sector */
592 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
594 memcpy(buf, tmp_buf, count);
599 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
600 const uint8_t *buf, int count1)
602 uint8_t tmp_buf[SECTOR_SIZE];
603 int len, nb_sectors, count;
607 /* first write to align to sector start */
608 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
611 sector_num = offset >> SECTOR_BITS;
613 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
615 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
616 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
625 /* write the sectors "in place" */
626 nb_sectors = count >> SECTOR_BITS;
627 if (nb_sectors > 0) {
628 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
630 sector_num += nb_sectors;
631 len = nb_sectors << SECTOR_BITS;
636 /* add data from the last sector */
638 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
640 memcpy(tmp_buf, buf, count);
641 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
648 * Read with byte offsets (needed only for file protocols)
650 int bdrv_pread(BlockDriverState *bs, int64_t offset,
651 void *buf1, int count1)
653 BlockDriver *drv = bs->drv;
657 if (!drv->bdrv_pread)
658 return bdrv_pread_em(bs, offset, buf1, count1);
659 return drv->bdrv_pread(bs, offset, buf1, count1);
663 * Write with byte offsets (needed only for file protocols)
665 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
666 const void *buf1, int count1)
668 BlockDriver *drv = bs->drv;
672 if (!drv->bdrv_pwrite)
673 return bdrv_pwrite_em(bs, offset, buf1, count1);
674 return drv->bdrv_pwrite(bs, offset, buf1, count1);
678 * Truncate file to 'offset' bytes (needed only for file protocols)
680 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
682 BlockDriver *drv = bs->drv;
685 if (!drv->bdrv_truncate)
687 return drv->bdrv_truncate(bs, offset);
691 * Length of a file in bytes. Return < 0 if error or unknown.
693 int64_t bdrv_getlength(BlockDriverState *bs)
695 BlockDriver *drv = bs->drv;
698 if (!drv->bdrv_getlength) {
700 return bs->total_sectors * SECTOR_SIZE;
702 return drv->bdrv_getlength(bs);
705 /* return 0 as number of sectors if no device present or error */
706 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
709 length = bdrv_getlength(bs);
713 length = length >> SECTOR_BITS;
714 *nb_sectors_ptr = length;
717 /* force a given boot sector. */
718 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
720 bs->boot_sector_enabled = 1;
723 memcpy(bs->boot_sector_data, data, size);
724 memset(bs->boot_sector_data + size, 0, 512 - size);
727 void bdrv_set_geometry_hint(BlockDriverState *bs,
728 int cyls, int heads, int secs)
735 void bdrv_set_type_hint(BlockDriverState *bs, int type)
738 bs->removable = ((type == BDRV_TYPE_CDROM ||
739 type == BDRV_TYPE_FLOPPY));
742 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
744 bs->translation = translation;
747 void bdrv_get_geometry_hint(BlockDriverState *bs,
748 int *pcyls, int *pheads, int *psecs)
755 int bdrv_get_type_hint(BlockDriverState *bs)
760 int bdrv_get_translation_hint(BlockDriverState *bs)
762 return bs->translation;
765 int bdrv_is_removable(BlockDriverState *bs)
767 return bs->removable;
770 int bdrv_is_read_only(BlockDriverState *bs)
772 return bs->read_only;
775 /* XXX: no longer used */
776 void bdrv_set_change_cb(BlockDriverState *bs,
777 void (*change_cb)(void *opaque), void *opaque)
779 bs->change_cb = change_cb;
780 bs->change_opaque = opaque;
783 int bdrv_is_encrypted(BlockDriverState *bs)
785 if (bs->backing_hd && bs->backing_hd->encrypted)
787 return bs->encrypted;
790 int bdrv_set_key(BlockDriverState *bs, const char *key)
793 if (bs->backing_hd && bs->backing_hd->encrypted) {
794 ret = bdrv_set_key(bs->backing_hd, key);
800 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
802 return bs->drv->bdrv_set_key(bs, key);
805 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
810 pstrcpy(buf, buf_size, bs->drv->format_name);
814 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
819 for (drv = first_drv; drv != NULL; drv = drv->next) {
820 it(opaque, drv->format_name);
824 BlockDriverState *bdrv_find(const char *name)
826 BlockDriverState *bs;
828 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
829 if (!strcmp(name, bs->device_name))
835 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
837 BlockDriverState *bs;
839 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
840 it(opaque, bs->device_name);
844 const char *bdrv_get_device_name(BlockDriverState *bs)
846 return bs->device_name;
849 void bdrv_flush(BlockDriverState *bs)
851 if (bs->drv->bdrv_flush)
852 bs->drv->bdrv_flush(bs);
854 bdrv_flush(bs->backing_hd);
859 BlockDriverState *bs;
861 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
862 term_printf("%s:", bs->device_name);
863 term_printf(" type=");
868 case BDRV_TYPE_CDROM:
869 term_printf("cdrom");
871 case BDRV_TYPE_FLOPPY:
872 term_printf("floppy");
875 term_printf(" removable=%d", bs->removable);
877 term_printf(" locked=%d", bs->locked);
880 term_printf(" file=");
881 term_print_filename(bs->filename);
882 if (bs->backing_file[0] != '\0') {
883 term_printf(" backing_file=");
884 term_print_filename(bs->backing_file);
886 term_printf(" ro=%d", bs->read_only);
887 term_printf(" drv=%s", bs->drv->format_name);
889 term_printf(" encrypted");
891 term_printf(" [not inserted]");
897 void bdrv_get_backing_filename(BlockDriverState *bs,
898 char *filename, int filename_size)
900 if (!bs->backing_hd) {
901 pstrcpy(filename, filename_size, "");
903 pstrcpy(filename, filename_size, bs->backing_file);
907 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
908 const uint8_t *buf, int nb_sectors)
910 BlockDriver *drv = bs->drv;
913 if (!drv->bdrv_write_compressed)
915 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
918 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
920 BlockDriver *drv = bs->drv;
923 if (!drv->bdrv_get_info)
925 memset(bdi, 0, sizeof(*bdi));
926 return drv->bdrv_get_info(bs, bdi);
929 /**************************************************************/
930 /* handling of snapshots */
932 int bdrv_snapshot_create(BlockDriverState *bs,
933 QEMUSnapshotInfo *sn_info)
935 BlockDriver *drv = bs->drv;
938 if (!drv->bdrv_snapshot_create)
940 return drv->bdrv_snapshot_create(bs, sn_info);
943 int bdrv_snapshot_goto(BlockDriverState *bs,
944 const char *snapshot_id)
946 BlockDriver *drv = bs->drv;
949 if (!drv->bdrv_snapshot_goto)
951 return drv->bdrv_snapshot_goto(bs, snapshot_id);
954 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
956 BlockDriver *drv = bs->drv;
959 if (!drv->bdrv_snapshot_delete)
961 return drv->bdrv_snapshot_delete(bs, snapshot_id);
964 int bdrv_snapshot_list(BlockDriverState *bs,
965 QEMUSnapshotInfo **psn_info)
967 BlockDriver *drv = bs->drv;
970 if (!drv->bdrv_snapshot_list)
972 return drv->bdrv_snapshot_list(bs, psn_info);
975 #define NB_SUFFIXES 4
977 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
979 static const char suffixes[NB_SUFFIXES] = "KMGT";
984 snprintf(buf, buf_size, "%" PRId64, size);
987 for(i = 0; i < NB_SUFFIXES; i++) {
988 if (size < (10 * base)) {
989 snprintf(buf, buf_size, "%0.1f%c",
993 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
994 snprintf(buf, buf_size, "%" PRId64 "%c",
995 ((size + (base >> 1)) / base),
1005 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1007 char buf1[128], date_buf[128], clock_buf[128];
1017 snprintf(buf, buf_size,
1018 "%-10s%-20s%7s%20s%15s",
1019 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1023 ptm = localtime(&ti);
1024 strftime(date_buf, sizeof(date_buf),
1025 "%Y-%m-%d %H:%M:%S", ptm);
1027 localtime_r(&ti, &tm);
1028 strftime(date_buf, sizeof(date_buf),
1029 "%Y-%m-%d %H:%M:%S", &tm);
1031 secs = sn->vm_clock_nsec / 1000000000;
1032 snprintf(clock_buf, sizeof(clock_buf),
1033 "%02d:%02d:%02d.%03d",
1035 (int)((secs / 60) % 60),
1037 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1038 snprintf(buf, buf_size,
1039 "%-10s%-20s%7s%20s%15s",
1040 sn->id_str, sn->name,
1041 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1049 /**************************************************************/
1052 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1053 uint8_t *buf, int nb_sectors,
1054 BlockDriverCompletionFunc *cb, void *opaque)
1056 BlockDriver *drv = bs->drv;
1061 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1062 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1063 memcpy(buf, bs->boot_sector_data, 512);
1069 return drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1072 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1073 const uint8_t *buf, int nb_sectors,
1074 BlockDriverCompletionFunc *cb, void *opaque)
1076 BlockDriver *drv = bs->drv;
1082 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1083 memcpy(bs->boot_sector_data, buf, 512);
1086 return drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1089 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1091 BlockDriver *drv = acb->bs->drv;
1093 drv->bdrv_aio_cancel(acb);
1097 /**************************************************************/
1098 /* async block device emulation */
1101 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1102 int64_t sector_num, uint8_t *buf, int nb_sectors,
1103 BlockDriverCompletionFunc *cb, void *opaque)
1106 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1111 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1112 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1113 BlockDriverCompletionFunc *cb, void *opaque)
1116 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1121 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1125 static void bdrv_aio_bh_cb(void *opaque)
1127 BlockDriverAIOCBSync *acb = opaque;
1128 acb->common.cb(acb->common.opaque, acb->ret);
1129 qemu_aio_release(acb);
1132 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1133 int64_t sector_num, uint8_t *buf, int nb_sectors,
1134 BlockDriverCompletionFunc *cb, void *opaque)
1136 BlockDriverAIOCBSync *acb;
1139 acb = qemu_aio_get(bs, cb, opaque);
1141 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1142 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1144 qemu_bh_schedule(acb->bh);
1145 return &acb->common;
1148 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1149 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1150 BlockDriverCompletionFunc *cb, void *opaque)
1152 BlockDriverAIOCBSync *acb;
1155 acb = qemu_aio_get(bs, cb, opaque);
1157 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1158 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1160 qemu_bh_schedule(acb->bh);
1161 return &acb->common;
1164 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1166 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1167 qemu_bh_cancel(acb->bh);
1168 qemu_aio_release(acb);
1170 #endif /* !QEMU_TOOL */
1172 /**************************************************************/
1173 /* sync block device emulation */
1175 static void bdrv_rw_em_cb(void *opaque, int ret)
1177 *(int *)opaque = ret;
1180 #define NOT_DONE 0x7fffffff
1182 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1183 uint8_t *buf, int nb_sectors)
1186 BlockDriverAIOCB *acb;
1188 async_ret = NOT_DONE;
1189 qemu_aio_wait_start();
1190 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1191 bdrv_rw_em_cb, &async_ret);
1193 qemu_aio_wait_end();
1196 while (async_ret == NOT_DONE) {
1199 qemu_aio_wait_end();
1203 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1204 const uint8_t *buf, int nb_sectors)
1207 BlockDriverAIOCB *acb;
1209 async_ret = NOT_DONE;
1210 qemu_aio_wait_start();
1211 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1212 bdrv_rw_em_cb, &async_ret);
1214 qemu_aio_wait_end();
1217 while (async_ret == NOT_DONE) {
1220 qemu_aio_wait_end();
1224 void bdrv_init(void)
1226 bdrv_register(&bdrv_raw);
1227 bdrv_register(&bdrv_host_device);
1229 bdrv_register(&bdrv_cow);
1231 bdrv_register(&bdrv_qcow);
1232 bdrv_register(&bdrv_vmdk);
1233 bdrv_register(&bdrv_cloop);
1234 bdrv_register(&bdrv_dmg);
1235 bdrv_register(&bdrv_bochs);
1236 bdrv_register(&bdrv_vpc);
1237 bdrv_register(&bdrv_vvfat);
1238 bdrv_register(&bdrv_qcow2);
1241 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1245 BlockDriverAIOCB *acb;
1248 if (drv->free_aiocb) {
1249 acb = drv->free_aiocb;
1250 drv->free_aiocb = acb->next;
1252 acb = qemu_mallocz(drv->aiocb_size);
1258 acb->opaque = opaque;
1262 void qemu_aio_release(void *p)
1264 BlockDriverAIOCB *acb = p;
1265 BlockDriver *drv = acb->bs->drv;
1266 acb->next = drv->free_aiocb;
1267 drv->free_aiocb = acb;
1270 /**************************************************************/
1271 /* removable device support */
1274 * Return TRUE if the media is present
1276 int bdrv_is_inserted(BlockDriverState *bs)
1278 BlockDriver *drv = bs->drv;
1282 if (!drv->bdrv_is_inserted)
1284 ret = drv->bdrv_is_inserted(bs);
1289 * Return TRUE if the media changed since the last call to this
1290 * function. It is currently only used for floppy disks
1292 int bdrv_media_changed(BlockDriverState *bs)
1294 BlockDriver *drv = bs->drv;
1297 if (!drv || !drv->bdrv_media_changed)
1300 ret = drv->bdrv_media_changed(bs);
1301 if (ret == -ENOTSUP)
1302 ret = bs->media_changed;
1303 bs->media_changed = 0;
1308 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1310 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1312 BlockDriver *drv = bs->drv;
1315 if (!drv || !drv->bdrv_eject) {
1318 ret = drv->bdrv_eject(bs, eject_flag);
1320 if (ret == -ENOTSUP) {
1326 int bdrv_is_locked(BlockDriverState *bs)
1332 * Lock or unlock the media (if it is locked, the user won't be able
1333 * to eject it manually).
1335 void bdrv_set_locked(BlockDriverState *bs, int locked)
1337 BlockDriver *drv = bs->drv;
1339 bs->locked = locked;
1340 if (drv && drv->bdrv_set_locked) {
1341 drv->bdrv_set_locked(bs, locked);