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);
372 if (drv->bdrv_getlength) {
373 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
376 if (bs->is_temporary) {
380 if (bs->backing_file[0] != '\0') {
381 /* if there is a backing file, use it */
382 bs->backing_hd = bdrv_new("");
383 if (!bs->backing_hd) {
388 path_combine(backing_filename, sizeof(backing_filename),
389 filename, bs->backing_file);
390 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
394 /* call the change callback */
395 bs->media_changed = 1;
397 bs->change_cb(bs->change_opaque);
402 void bdrv_close(BlockDriverState *bs)
406 bdrv_delete(bs->backing_hd);
407 bs->drv->bdrv_close(bs);
408 qemu_free(bs->opaque);
410 if (bs->is_temporary) {
411 unlink(bs->filename);
417 /* call the change callback */
418 bs->media_changed = 1;
420 bs->change_cb(bs->change_opaque);
424 void bdrv_delete(BlockDriverState *bs)
426 /* XXX: remove the driver list */
431 /* commit COW file into the raw image */
432 int bdrv_commit(BlockDriverState *bs)
434 BlockDriver *drv = bs->drv;
435 int64_t i, total_sectors;
437 unsigned char sector[512];
446 if (!bs->backing_hd) {
450 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
451 for (i = 0; i < total_sectors;) {
452 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
453 for(j = 0; j < n; j++) {
454 if (bdrv_read(bs, i, sector, 1) != 0) {
458 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
468 if (drv->bdrv_make_empty)
469 return drv->bdrv_make_empty(bs);
474 /* return < 0 if error. See bdrv_write() for the return codes */
475 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
476 uint8_t *buf, int nb_sectors)
478 BlockDriver *drv = bs->drv;
483 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
484 memcpy(buf, bs->boot_sector_data, 512);
491 if (drv->bdrv_pread) {
493 len = nb_sectors * 512;
494 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
502 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
506 /* Return < 0 if error. Important errors are:
507 -EIO generic I/O error (may happen for all errors)
508 -ENOMEDIUM No media inserted.
509 -EINVAL Invalid sector number or nb_sectors
510 -EACCES Trying to write a read-only device
512 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
513 const uint8_t *buf, int nb_sectors)
515 BlockDriver *drv = bs->drv;
520 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
521 memcpy(bs->boot_sector_data, buf, 512);
523 if (drv->bdrv_pwrite) {
525 len = nb_sectors * 512;
526 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
534 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
538 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
539 uint8_t *buf, int count1)
541 uint8_t tmp_buf[SECTOR_SIZE];
542 int len, nb_sectors, count;
546 /* first read to align to sector start */
547 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
550 sector_num = offset >> SECTOR_BITS;
552 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
554 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
562 /* read the sectors "in place" */
563 nb_sectors = count >> SECTOR_BITS;
564 if (nb_sectors > 0) {
565 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
567 sector_num += nb_sectors;
568 len = nb_sectors << SECTOR_BITS;
573 /* add data from the last sector */
575 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
577 memcpy(buf, tmp_buf, count);
582 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
583 const uint8_t *buf, int count1)
585 uint8_t tmp_buf[SECTOR_SIZE];
586 int len, nb_sectors, count;
590 /* first write to align to sector start */
591 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
594 sector_num = offset >> SECTOR_BITS;
596 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
598 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
599 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
608 /* write the sectors "in place" */
609 nb_sectors = count >> SECTOR_BITS;
610 if (nb_sectors > 0) {
611 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
613 sector_num += nb_sectors;
614 len = nb_sectors << SECTOR_BITS;
619 /* add data from the last sector */
621 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
623 memcpy(tmp_buf, buf, count);
624 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
631 * Read with byte offsets (needed only for file protocols)
633 int bdrv_pread(BlockDriverState *bs, int64_t offset,
634 void *buf1, int count1)
636 BlockDriver *drv = bs->drv;
640 if (!drv->bdrv_pread)
641 return bdrv_pread_em(bs, offset, buf1, count1);
642 return drv->bdrv_pread(bs, offset, buf1, count1);
646 * Write with byte offsets (needed only for file protocols)
648 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
649 const void *buf1, int count1)
651 BlockDriver *drv = bs->drv;
655 if (!drv->bdrv_pwrite)
656 return bdrv_pwrite_em(bs, offset, buf1, count1);
657 return drv->bdrv_pwrite(bs, offset, buf1, count1);
661 * Truncate file to 'offset' bytes (needed only for file protocols)
663 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
665 BlockDriver *drv = bs->drv;
668 if (!drv->bdrv_truncate)
670 return drv->bdrv_truncate(bs, offset);
674 * Length of a file in bytes. Return < 0 if error or unknown.
676 int64_t bdrv_getlength(BlockDriverState *bs)
678 BlockDriver *drv = bs->drv;
681 if (!drv->bdrv_getlength) {
683 return bs->total_sectors * SECTOR_SIZE;
685 return drv->bdrv_getlength(bs);
688 /* return 0 as number of sectors if no device present or error */
689 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
692 length = bdrv_getlength(bs);
696 length = length >> SECTOR_BITS;
697 *nb_sectors_ptr = length;
700 /* force a given boot sector. */
701 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
703 bs->boot_sector_enabled = 1;
706 memcpy(bs->boot_sector_data, data, size);
707 memset(bs->boot_sector_data + size, 0, 512 - size);
710 void bdrv_set_geometry_hint(BlockDriverState *bs,
711 int cyls, int heads, int secs)
718 void bdrv_set_type_hint(BlockDriverState *bs, int type)
721 bs->removable = ((type == BDRV_TYPE_CDROM ||
722 type == BDRV_TYPE_FLOPPY));
725 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
727 bs->translation = translation;
730 void bdrv_get_geometry_hint(BlockDriverState *bs,
731 int *pcyls, int *pheads, int *psecs)
738 int bdrv_get_type_hint(BlockDriverState *bs)
743 int bdrv_get_translation_hint(BlockDriverState *bs)
745 return bs->translation;
748 int bdrv_is_removable(BlockDriverState *bs)
750 return bs->removable;
753 int bdrv_is_read_only(BlockDriverState *bs)
755 return bs->read_only;
758 /* XXX: no longer used */
759 void bdrv_set_change_cb(BlockDriverState *bs,
760 void (*change_cb)(void *opaque), void *opaque)
762 bs->change_cb = change_cb;
763 bs->change_opaque = opaque;
766 int bdrv_is_encrypted(BlockDriverState *bs)
768 if (bs->backing_hd && bs->backing_hd->encrypted)
770 return bs->encrypted;
773 int bdrv_set_key(BlockDriverState *bs, const char *key)
776 if (bs->backing_hd && bs->backing_hd->encrypted) {
777 ret = bdrv_set_key(bs->backing_hd, key);
783 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
785 return bs->drv->bdrv_set_key(bs, key);
788 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
793 pstrcpy(buf, buf_size, bs->drv->format_name);
797 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
802 for (drv = first_drv; drv != NULL; drv = drv->next) {
803 it(opaque, drv->format_name);
807 BlockDriverState *bdrv_find(const char *name)
809 BlockDriverState *bs;
811 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
812 if (!strcmp(name, bs->device_name))
818 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
820 BlockDriverState *bs;
822 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
823 it(opaque, bs->device_name);
827 const char *bdrv_get_device_name(BlockDriverState *bs)
829 return bs->device_name;
832 void bdrv_flush(BlockDriverState *bs)
834 if (bs->drv->bdrv_flush)
835 bs->drv->bdrv_flush(bs);
837 bdrv_flush(bs->backing_hd);
842 BlockDriverState *bs;
844 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
845 term_printf("%s:", bs->device_name);
846 term_printf(" type=");
851 case BDRV_TYPE_CDROM:
852 term_printf("cdrom");
854 case BDRV_TYPE_FLOPPY:
855 term_printf("floppy");
858 term_printf(" removable=%d", bs->removable);
860 term_printf(" locked=%d", bs->locked);
863 term_printf(" file=%s", bs->filename);
864 if (bs->backing_file[0] != '\0')
865 term_printf(" backing_file=%s", bs->backing_file);
866 term_printf(" ro=%d", bs->read_only);
867 term_printf(" drv=%s", bs->drv->format_name);
869 term_printf(" encrypted");
871 term_printf(" [not inserted]");
877 void bdrv_get_backing_filename(BlockDriverState *bs,
878 char *filename, int filename_size)
880 if (!bs->backing_hd) {
881 pstrcpy(filename, filename_size, "");
883 pstrcpy(filename, filename_size, bs->backing_file);
887 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
888 const uint8_t *buf, int nb_sectors)
890 BlockDriver *drv = bs->drv;
893 if (!drv->bdrv_write_compressed)
895 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
898 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
900 BlockDriver *drv = bs->drv;
903 if (!drv->bdrv_get_info)
905 memset(bdi, 0, sizeof(*bdi));
906 return drv->bdrv_get_info(bs, bdi);
909 /**************************************************************/
910 /* handling of snapshots */
912 int bdrv_snapshot_create(BlockDriverState *bs,
913 QEMUSnapshotInfo *sn_info)
915 BlockDriver *drv = bs->drv;
918 if (!drv->bdrv_snapshot_create)
920 return drv->bdrv_snapshot_create(bs, sn_info);
923 int bdrv_snapshot_goto(BlockDriverState *bs,
924 const char *snapshot_id)
926 BlockDriver *drv = bs->drv;
929 if (!drv->bdrv_snapshot_goto)
931 return drv->bdrv_snapshot_goto(bs, snapshot_id);
934 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
936 BlockDriver *drv = bs->drv;
939 if (!drv->bdrv_snapshot_delete)
941 return drv->bdrv_snapshot_delete(bs, snapshot_id);
944 int bdrv_snapshot_list(BlockDriverState *bs,
945 QEMUSnapshotInfo **psn_info)
947 BlockDriver *drv = bs->drv;
950 if (!drv->bdrv_snapshot_list)
952 return drv->bdrv_snapshot_list(bs, psn_info);
955 #define NB_SUFFIXES 4
957 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
959 static const char suffixes[NB_SUFFIXES] = "KMGT";
964 snprintf(buf, buf_size, "%" PRId64, size);
967 for(i = 0; i < NB_SUFFIXES; i++) {
968 if (size < (10 * base)) {
969 snprintf(buf, buf_size, "%0.1f%c",
973 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
974 snprintf(buf, buf_size, "%" PRId64 "%c",
975 ((size + (base >> 1)) / base),
985 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
987 char buf1[128], date_buf[128], clock_buf[128];
993 snprintf(buf, buf_size,
994 "%-10s%-20s%7s%20s%15s",
995 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
999 localtime_r(&ti, &tm);
1001 strftime(date_buf, sizeof(date_buf),
1002 "%Y-%m-%d %H:%M:%S", &tm);
1003 secs = sn->vm_clock_nsec / 1000000000;
1004 snprintf(clock_buf, sizeof(clock_buf),
1005 "%02d:%02d:%02d.%03d",
1007 (int)((secs / 60) % 60),
1009 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1010 snprintf(buf, buf_size,
1011 "%-10s%-20s%7s%20s%15s",
1012 sn->id_str, sn->name,
1013 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1021 /**************************************************************/
1024 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1025 uint8_t *buf, int nb_sectors,
1026 BlockDriverCompletionFunc *cb, void *opaque)
1028 BlockDriver *drv = bs->drv;
1033 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1034 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1035 memcpy(buf, bs->boot_sector_data, 512);
1041 return drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1044 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1045 const uint8_t *buf, int nb_sectors,
1046 BlockDriverCompletionFunc *cb, void *opaque)
1048 BlockDriver *drv = bs->drv;
1054 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1055 memcpy(bs->boot_sector_data, buf, 512);
1058 return drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1061 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1063 BlockDriver *drv = acb->bs->drv;
1065 drv->bdrv_aio_cancel(acb);
1069 /**************************************************************/
1070 /* async block device emulation */
1073 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1074 int64_t sector_num, uint8_t *buf, int nb_sectors,
1075 BlockDriverCompletionFunc *cb, void *opaque)
1078 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1083 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1084 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1085 BlockDriverCompletionFunc *cb, void *opaque)
1088 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1093 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1097 static void bdrv_aio_bh_cb(void *opaque)
1099 BlockDriverAIOCBSync *acb = opaque;
1100 acb->common.cb(acb->common.opaque, acb->ret);
1101 qemu_aio_release(acb);
1104 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1105 int64_t sector_num, uint8_t *buf, int nb_sectors,
1106 BlockDriverCompletionFunc *cb, void *opaque)
1108 BlockDriverAIOCBSync *acb;
1111 acb = qemu_aio_get(bs, cb, opaque);
1113 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1114 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1116 qemu_bh_schedule(acb->bh);
1117 return &acb->common;
1120 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1121 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1122 BlockDriverCompletionFunc *cb, void *opaque)
1124 BlockDriverAIOCBSync *acb;
1127 acb = qemu_aio_get(bs, cb, opaque);
1129 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1130 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1132 qemu_bh_schedule(acb->bh);
1133 return &acb->common;
1136 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1138 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1139 qemu_bh_cancel(acb->bh);
1140 qemu_aio_release(acb);
1142 #endif /* !QEMU_TOOL */
1144 /**************************************************************/
1145 /* sync block device emulation */
1147 static void bdrv_rw_em_cb(void *opaque, int ret)
1149 *(int *)opaque = ret;
1152 #define NOT_DONE 0x7fffffff
1154 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1155 uint8_t *buf, int nb_sectors)
1158 BlockDriverAIOCB *acb;
1160 async_ret = NOT_DONE;
1161 qemu_aio_wait_start();
1162 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1163 bdrv_rw_em_cb, &async_ret);
1165 qemu_aio_wait_end();
1168 while (async_ret == NOT_DONE) {
1171 qemu_aio_wait_end();
1175 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1176 const uint8_t *buf, int nb_sectors)
1179 BlockDriverAIOCB *acb;
1181 async_ret = NOT_DONE;
1182 qemu_aio_wait_start();
1183 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1184 bdrv_rw_em_cb, &async_ret);
1186 qemu_aio_wait_end();
1189 while (async_ret == NOT_DONE) {
1192 qemu_aio_wait_end();
1196 void bdrv_init(void)
1198 bdrv_register(&bdrv_raw);
1199 bdrv_register(&bdrv_host_device);
1201 bdrv_register(&bdrv_cow);
1203 bdrv_register(&bdrv_qcow);
1204 bdrv_register(&bdrv_vmdk);
1205 bdrv_register(&bdrv_cloop);
1206 bdrv_register(&bdrv_dmg);
1207 bdrv_register(&bdrv_bochs);
1208 bdrv_register(&bdrv_vpc);
1209 bdrv_register(&bdrv_vvfat);
1210 bdrv_register(&bdrv_qcow2);
1213 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1217 BlockDriverAIOCB *acb;
1220 if (drv->free_aiocb) {
1221 acb = drv->free_aiocb;
1222 drv->free_aiocb = acb->next;
1224 acb = qemu_mallocz(drv->aiocb_size);
1230 acb->opaque = opaque;
1234 void qemu_aio_release(void *p)
1236 BlockDriverAIOCB *acb = p;
1237 BlockDriver *drv = acb->bs->drv;
1238 acb->next = drv->free_aiocb;
1239 drv->free_aiocb = acb;
1242 /**************************************************************/
1243 /* removable device support */
1246 * Return TRUE if the media is present
1248 int bdrv_is_inserted(BlockDriverState *bs)
1250 BlockDriver *drv = bs->drv;
1254 if (!drv->bdrv_is_inserted)
1256 ret = drv->bdrv_is_inserted(bs);
1261 * Return TRUE if the media changed since the last call to this
1262 * function. It is currently only used for floppy disks
1264 int bdrv_media_changed(BlockDriverState *bs)
1266 BlockDriver *drv = bs->drv;
1269 if (!drv || !drv->bdrv_media_changed)
1272 ret = drv->bdrv_media_changed(bs);
1273 if (ret == -ENOTSUP)
1274 ret = bs->media_changed;
1275 bs->media_changed = 0;
1280 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1282 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1284 BlockDriver *drv = bs->drv;
1287 if (!drv || !drv->bdrv_eject) {
1290 ret = drv->bdrv_eject(bs, eject_flag);
1292 if (ret == -ENOTSUP) {
1298 int bdrv_is_locked(BlockDriverState *bs)
1304 * Lock or unlock the media (if it is locked, the user won't be able
1305 * to eject it manually).
1307 void bdrv_set_locked(BlockDriverState *bs, int locked)
1309 BlockDriver *drv = bs->drv;
1311 bs->locked = locked;
1312 if (drv && drv->bdrv_set_locked) {
1313 drv->bdrv_set_locked(bs, locked);