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
24 #include "qemu-common.h"
28 #include "block_int.h"
31 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/queue.h>
39 #define SECTOR_SIZE (1 << SECTOR_BITS)
41 typedef struct BlockDriverAIOCBSync {
42 BlockDriverAIOCB common;
45 } BlockDriverAIOCBSync;
47 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
48 int64_t sector_num, uint8_t *buf, int nb_sectors,
49 BlockDriverCompletionFunc *cb, void *opaque);
50 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
51 int64_t sector_num, const uint8_t *buf, int nb_sectors,
52 BlockDriverCompletionFunc *cb, void *opaque);
53 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
54 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
55 uint8_t *buf, int nb_sectors);
56 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
57 const uint8_t *buf, int nb_sectors);
59 BlockDriverState *bdrv_first;
60 static BlockDriver *first_drv;
62 int path_is_absolute(const char *path)
66 /* specific case for names like: "\\.\d:" */
67 if (*path == '/' || *path == '\\')
70 p = strchr(path, ':');
76 return (*p == '/' || *p == '\\');
82 /* if filename is absolute, just copy it to dest. Otherwise, build a
83 path to it by considering it is relative to base_path. URL are
85 void path_combine(char *dest, int dest_size,
86 const char *base_path,
94 if (path_is_absolute(filename)) {
95 pstrcpy(dest, dest_size, filename);
97 p = strchr(base_path, ':');
102 p1 = strrchr(base_path, '/');
106 p2 = strrchr(base_path, '\\');
118 if (len > dest_size - 1)
120 memcpy(dest, base_path, len);
122 pstrcat(dest, dest_size, filename);
126 static int bdrv_rd_badreq_sectors(BlockDriverState *bs,
127 int64_t sector_num, int nb_sectors)
132 nb_sectors > bs->total_sectors ||
133 sector_num > bs->total_sectors - nb_sectors;
136 static int bdrv_rd_badreq_bytes(BlockDriverState *bs,
137 int64_t offset, int count)
139 int64_t size = bs->total_sectors << SECTOR_BITS;
144 offset > size - count;
147 static int bdrv_wr_badreq_sectors(BlockDriverState *bs,
148 int64_t sector_num, int nb_sectors)
150 if (sector_num < 0 ||
154 if (sector_num > bs->total_sectors - nb_sectors) {
156 bs->total_sectors = sector_num + nb_sectors;
163 static int bdrv_wr_badreq_bytes(BlockDriverState *bs,
164 int64_t offset, int count)
166 int64_t size = bs->total_sectors << SECTOR_BITS;
171 if (offset > size - count) {
173 bs->total_sectors = (offset + count + SECTOR_SIZE - 1) >> SECTOR_BITS;
181 static void bdrv_register(BlockDriver *bdrv)
183 if (!bdrv->bdrv_aio_read) {
184 /* add AIO emulation layer */
185 bdrv->bdrv_aio_read = bdrv_aio_read_em;
186 bdrv->bdrv_aio_write = bdrv_aio_write_em;
187 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
188 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
189 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
190 /* add synchronous IO emulation layer */
191 bdrv->bdrv_read = bdrv_read_em;
192 bdrv->bdrv_write = bdrv_write_em;
194 bdrv->next = first_drv;
198 /* create a new block device (by default it is empty) */
199 BlockDriverState *bdrv_new(const char *device_name)
201 BlockDriverState **pbs, *bs;
203 bs = qemu_mallocz(sizeof(BlockDriverState));
206 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
207 if (device_name[0] != '\0') {
208 /* insert at the end */
217 BlockDriver *bdrv_find_format(const char *format_name)
220 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
221 if (!strcmp(drv1->format_name, format_name))
227 int bdrv_create(BlockDriver *drv,
228 const char *filename, int64_t size_in_sectors,
229 const char *backing_file, int flags)
231 if (!drv->bdrv_create)
233 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
237 void get_tmp_filename(char *filename, int size)
239 char temp_dir[MAX_PATH];
241 GetTempPath(MAX_PATH, temp_dir);
242 GetTempFileName(temp_dir, "qem", 0, filename);
245 void get_tmp_filename(char *filename, int size)
249 /* XXX: race condition possible */
250 tmpdir = getenv("TMPDIR");
253 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
254 fd = mkstemp(filename);
260 static int is_windows_drive_prefix(const char *filename)
262 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
263 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
267 static int is_windows_drive(const char *filename)
269 if (is_windows_drive_prefix(filename) &&
272 if (strstart(filename, "\\\\.\\", NULL) ||
273 strstart(filename, "//./", NULL))
279 static BlockDriver *find_protocol(const char *filename)
287 if (is_windows_drive(filename) ||
288 is_windows_drive_prefix(filename))
291 p = strchr(filename, ':');
295 if (len > sizeof(protocol) - 1)
296 len = sizeof(protocol) - 1;
297 memcpy(protocol, filename, len);
298 protocol[len] = '\0';
299 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
300 if (drv1->protocol_name &&
301 !strcmp(drv1->protocol_name, protocol))
307 /* XXX: force raw format if block or character device ? It would
308 simplify the BSD case */
309 static BlockDriver *find_image_format(const char *filename)
311 int ret, score, score_max;
312 BlockDriver *drv1, *drv;
314 BlockDriverState *bs;
316 /* detect host devices. By convention, /dev/cdrom[N] is always
317 recognized as a host CDROM */
318 if (strstart(filename, "/dev/cdrom", NULL))
319 return &bdrv_host_device;
321 if (is_windows_drive(filename))
322 return &bdrv_host_device;
326 if (stat(filename, &st) >= 0 &&
327 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
328 return &bdrv_host_device;
333 drv = find_protocol(filename);
334 /* no need to test disk image formats for vvfat */
335 if (drv == &bdrv_vvfat)
338 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
341 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
348 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
349 if (drv1->bdrv_probe) {
350 score = drv1->bdrv_probe(buf, ret, filename);
351 if (score > score_max) {
360 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
362 BlockDriverState *bs;
368 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
377 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
379 return bdrv_open2(bs, filename, flags, NULL);
382 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
386 char tmp_filename[PATH_MAX];
387 char backing_filename[PATH_MAX];
390 bs->is_temporary = 0;
394 if (flags & BDRV_O_AUTOGROW)
397 if (flags & BDRV_O_SNAPSHOT) {
398 BlockDriverState *bs1;
401 /* if snapshot, we create a temporary backing file and open it
402 instead of opening 'filename' directly */
404 /* if there is a backing file, use it */
409 if (bdrv_open(bs1, filename, 0) < 0) {
413 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
416 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
417 realpath(filename, backing_filename);
418 if (bdrv_create(&bdrv_qcow2, tmp_filename,
419 total_size, backing_filename, 0) < 0) {
422 filename = tmp_filename;
423 bs->is_temporary = 1;
426 pstrcpy(bs->filename, sizeof(bs->filename), filename);
427 if (flags & BDRV_O_FILE) {
428 drv = find_protocol(filename);
433 drv = find_image_format(filename);
439 bs->opaque = qemu_mallocz(drv->instance_size);
440 bs->total_sectors = 0; /* driver will set if it does not do getlength */
441 if (bs->opaque == NULL && drv->instance_size > 0)
443 /* Note: for compatibility, we open disk image files as RDWR, and
444 RDONLY as fallback */
445 if (!(flags & BDRV_O_FILE))
446 open_flags = BDRV_O_RDWR | (flags & BDRV_O_DIRECT);
448 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
449 ret = drv->bdrv_open(bs, filename, open_flags);
450 if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
451 ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
455 qemu_free(bs->opaque);
460 if (drv->bdrv_getlength) {
461 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
464 if (bs->is_temporary) {
468 if (bs->backing_file[0] != '\0') {
469 /* if there is a backing file, use it */
470 bs->backing_hd = bdrv_new("");
471 if (!bs->backing_hd) {
476 path_combine(backing_filename, sizeof(backing_filename),
477 filename, bs->backing_file);
478 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
482 /* call the change callback */
483 bs->media_changed = 1;
485 bs->change_cb(bs->change_opaque);
490 void bdrv_close(BlockDriverState *bs)
494 bdrv_delete(bs->backing_hd);
495 bs->drv->bdrv_close(bs);
496 qemu_free(bs->opaque);
498 if (bs->is_temporary) {
499 unlink(bs->filename);
505 /* call the change callback */
506 bs->total_sectors = 0;
507 bs->media_changed = 1;
509 bs->change_cb(bs->change_opaque);
513 void bdrv_delete(BlockDriverState *bs)
515 /* XXX: remove the driver list */
520 /* commit COW file into the raw image */
521 int bdrv_commit(BlockDriverState *bs)
523 BlockDriver *drv = bs->drv;
524 int64_t i, total_sectors;
526 unsigned char sector[512];
535 if (!bs->backing_hd) {
539 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
540 for (i = 0; i < total_sectors;) {
541 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
542 for(j = 0; j < n; j++) {
543 if (bdrv_read(bs, i, sector, 1) != 0) {
547 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
557 if (drv->bdrv_make_empty)
558 return drv->bdrv_make_empty(bs);
563 /* return < 0 if error. See bdrv_write() for the return codes */
564 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
565 uint8_t *buf, int nb_sectors)
567 BlockDriver *drv = bs->drv;
572 if (bdrv_rd_badreq_sectors(bs, sector_num, nb_sectors))
574 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
575 memcpy(buf, bs->boot_sector_data, 512);
582 if (drv->bdrv_pread) {
584 len = nb_sectors * 512;
585 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
591 bs->rd_bytes += (unsigned) len;
596 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
600 /* Return < 0 if error. Important errors are:
601 -EIO generic I/O error (may happen for all errors)
602 -ENOMEDIUM No media inserted.
603 -EINVAL Invalid sector number or nb_sectors
604 -EACCES Trying to write a read-only device
606 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
607 const uint8_t *buf, int nb_sectors)
609 BlockDriver *drv = bs->drv;
614 if (bdrv_wr_badreq_sectors(bs, sector_num, nb_sectors))
616 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
617 memcpy(bs->boot_sector_data, buf, 512);
619 if (drv->bdrv_pwrite) {
621 len = nb_sectors * 512;
622 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
628 bs->wr_bytes += (unsigned) len;
633 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
637 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
638 uint8_t *buf, int count1)
640 uint8_t tmp_buf[SECTOR_SIZE];
641 int len, nb_sectors, count;
645 /* first read to align to sector start */
646 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
649 sector_num = offset >> SECTOR_BITS;
651 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
653 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
661 /* read the sectors "in place" */
662 nb_sectors = count >> SECTOR_BITS;
663 if (nb_sectors > 0) {
664 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
666 sector_num += nb_sectors;
667 len = nb_sectors << SECTOR_BITS;
672 /* add data from the last sector */
674 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
676 memcpy(buf, tmp_buf, count);
681 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
682 const uint8_t *buf, int count1)
684 uint8_t tmp_buf[SECTOR_SIZE];
685 int len, nb_sectors, count;
689 /* first write to align to sector start */
690 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
693 sector_num = offset >> SECTOR_BITS;
695 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
697 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
698 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
707 /* write the sectors "in place" */
708 nb_sectors = count >> SECTOR_BITS;
709 if (nb_sectors > 0) {
710 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
712 sector_num += nb_sectors;
713 len = nb_sectors << SECTOR_BITS;
718 /* add data from the last sector */
720 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
722 memcpy(tmp_buf, buf, count);
723 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
730 * Read with byte offsets (needed only for file protocols)
732 int bdrv_pread(BlockDriverState *bs, int64_t offset,
733 void *buf1, int count1)
735 BlockDriver *drv = bs->drv;
739 if (!drv->bdrv_pread)
740 return bdrv_pread_em(bs, offset, buf1, count1);
741 if (bdrv_rd_badreq_bytes(bs, offset, count1))
743 return drv->bdrv_pread(bs, offset, buf1, count1);
747 * Write with byte offsets (needed only for file protocols)
749 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
750 const void *buf1, int count1)
752 BlockDriver *drv = bs->drv;
756 if (!drv->bdrv_pwrite)
757 return bdrv_pwrite_em(bs, offset, buf1, count1);
758 if (bdrv_wr_badreq_bytes(bs, offset, count1))
760 return drv->bdrv_pwrite(bs, offset, buf1, count1);
764 * Truncate file to 'offset' bytes (needed only for file protocols)
766 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
768 BlockDriver *drv = bs->drv;
771 if (!drv->bdrv_truncate)
773 return drv->bdrv_truncate(bs, offset);
777 * Length of a file in bytes. Return < 0 if error or unknown.
779 int64_t bdrv_getlength(BlockDriverState *bs)
781 BlockDriver *drv = bs->drv;
784 if (!drv->bdrv_getlength) {
786 return bs->total_sectors * SECTOR_SIZE;
788 return drv->bdrv_getlength(bs);
791 /* return 0 as number of sectors if no device present or error */
792 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
795 length = bdrv_getlength(bs);
799 length = length >> SECTOR_BITS;
800 *nb_sectors_ptr = length;
803 /* force a given boot sector. */
804 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
806 bs->boot_sector_enabled = 1;
809 memcpy(bs->boot_sector_data, data, size);
810 memset(bs->boot_sector_data + size, 0, 512 - size);
813 void bdrv_set_geometry_hint(BlockDriverState *bs,
814 int cyls, int heads, int secs)
821 void bdrv_set_type_hint(BlockDriverState *bs, int type)
824 bs->removable = ((type == BDRV_TYPE_CDROM ||
825 type == BDRV_TYPE_FLOPPY));
828 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
830 bs->translation = translation;
833 void bdrv_get_geometry_hint(BlockDriverState *bs,
834 int *pcyls, int *pheads, int *psecs)
841 int bdrv_get_type_hint(BlockDriverState *bs)
846 int bdrv_get_translation_hint(BlockDriverState *bs)
848 return bs->translation;
851 int bdrv_is_removable(BlockDriverState *bs)
853 return bs->removable;
856 int bdrv_is_read_only(BlockDriverState *bs)
858 return bs->read_only;
861 int bdrv_is_sg(BlockDriverState *bs)
866 /* XXX: no longer used */
867 void bdrv_set_change_cb(BlockDriverState *bs,
868 void (*change_cb)(void *opaque), void *opaque)
870 bs->change_cb = change_cb;
871 bs->change_opaque = opaque;
874 int bdrv_is_encrypted(BlockDriverState *bs)
876 if (bs->backing_hd && bs->backing_hd->encrypted)
878 return bs->encrypted;
881 int bdrv_set_key(BlockDriverState *bs, const char *key)
884 if (bs->backing_hd && bs->backing_hd->encrypted) {
885 ret = bdrv_set_key(bs->backing_hd, key);
891 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
893 return bs->drv->bdrv_set_key(bs, key);
896 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
901 pstrcpy(buf, buf_size, bs->drv->format_name);
905 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
910 for (drv = first_drv; drv != NULL; drv = drv->next) {
911 it(opaque, drv->format_name);
915 BlockDriverState *bdrv_find(const char *name)
917 BlockDriverState *bs;
919 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
920 if (!strcmp(name, bs->device_name))
926 void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
928 BlockDriverState *bs;
930 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
931 it(opaque, bs->device_name);
935 const char *bdrv_get_device_name(BlockDriverState *bs)
937 return bs->device_name;
940 void bdrv_flush(BlockDriverState *bs)
942 if (bs->drv->bdrv_flush)
943 bs->drv->bdrv_flush(bs);
945 bdrv_flush(bs->backing_hd);
951 BlockDriverState *bs;
953 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
954 term_printf("%s:", bs->device_name);
955 term_printf(" type=");
960 case BDRV_TYPE_CDROM:
961 term_printf("cdrom");
963 case BDRV_TYPE_FLOPPY:
964 term_printf("floppy");
967 term_printf(" removable=%d", bs->removable);
969 term_printf(" locked=%d", bs->locked);
972 term_printf(" file=");
973 term_print_filename(bs->filename);
974 if (bs->backing_file[0] != '\0') {
975 term_printf(" backing_file=");
976 term_print_filename(bs->backing_file);
978 term_printf(" ro=%d", bs->read_only);
979 term_printf(" drv=%s", bs->drv->format_name);
981 term_printf(" encrypted");
983 term_printf(" [not inserted]");
989 /* The "info blockstats" command. */
990 void bdrv_info_stats (void)
992 BlockDriverState *bs;
994 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
998 " rd_operations=%" PRIu64
999 " wr_operations=%" PRIu64
1002 bs->rd_bytes, bs->wr_bytes,
1003 bs->rd_ops, bs->wr_ops);
1008 void bdrv_get_backing_filename(BlockDriverState *bs,
1009 char *filename, int filename_size)
1011 if (!bs->backing_hd) {
1012 pstrcpy(filename, filename_size, "");
1014 pstrcpy(filename, filename_size, bs->backing_file);
1018 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1019 const uint8_t *buf, int nb_sectors)
1021 BlockDriver *drv = bs->drv;
1024 if (!drv->bdrv_write_compressed)
1026 if (bdrv_wr_badreq_sectors(bs, sector_num, nb_sectors))
1028 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1031 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1033 BlockDriver *drv = bs->drv;
1036 if (!drv->bdrv_get_info)
1038 memset(bdi, 0, sizeof(*bdi));
1039 return drv->bdrv_get_info(bs, bdi);
1042 /**************************************************************/
1043 /* handling of snapshots */
1045 int bdrv_snapshot_create(BlockDriverState *bs,
1046 QEMUSnapshotInfo *sn_info)
1048 BlockDriver *drv = bs->drv;
1051 if (!drv->bdrv_snapshot_create)
1053 return drv->bdrv_snapshot_create(bs, sn_info);
1056 int bdrv_snapshot_goto(BlockDriverState *bs,
1057 const char *snapshot_id)
1059 BlockDriver *drv = bs->drv;
1062 if (!drv->bdrv_snapshot_goto)
1064 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1067 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1069 BlockDriver *drv = bs->drv;
1072 if (!drv->bdrv_snapshot_delete)
1074 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1077 int bdrv_snapshot_list(BlockDriverState *bs,
1078 QEMUSnapshotInfo **psn_info)
1080 BlockDriver *drv = bs->drv;
1083 if (!drv->bdrv_snapshot_list)
1085 return drv->bdrv_snapshot_list(bs, psn_info);
1088 #define NB_SUFFIXES 4
1090 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1092 static const char suffixes[NB_SUFFIXES] = "KMGT";
1097 snprintf(buf, buf_size, "%" PRId64, size);
1100 for(i = 0; i < NB_SUFFIXES; i++) {
1101 if (size < (10 * base)) {
1102 snprintf(buf, buf_size, "%0.1f%c",
1103 (double)size / base,
1106 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1107 snprintf(buf, buf_size, "%" PRId64 "%c",
1108 ((size + (base >> 1)) / base),
1118 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1120 char buf1[128], date_buf[128], clock_buf[128];
1130 snprintf(buf, buf_size,
1131 "%-10s%-20s%7s%20s%15s",
1132 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1136 ptm = localtime(&ti);
1137 strftime(date_buf, sizeof(date_buf),
1138 "%Y-%m-%d %H:%M:%S", ptm);
1140 localtime_r(&ti, &tm);
1141 strftime(date_buf, sizeof(date_buf),
1142 "%Y-%m-%d %H:%M:%S", &tm);
1144 secs = sn->vm_clock_nsec / 1000000000;
1145 snprintf(clock_buf, sizeof(clock_buf),
1146 "%02d:%02d:%02d.%03d",
1148 (int)((secs / 60) % 60),
1150 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1151 snprintf(buf, buf_size,
1152 "%-10s%-20s%7s%20s%15s",
1153 sn->id_str, sn->name,
1154 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1162 /**************************************************************/
1165 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1166 uint8_t *buf, int nb_sectors,
1167 BlockDriverCompletionFunc *cb, void *opaque)
1169 BlockDriver *drv = bs->drv;
1170 BlockDriverAIOCB *ret;
1174 if (bdrv_rd_badreq_sectors(bs, sector_num, nb_sectors))
1177 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1178 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1179 memcpy(buf, bs->boot_sector_data, 512);
1185 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1188 /* Update stats even though technically transfer has not happened. */
1189 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1196 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1197 const uint8_t *buf, int nb_sectors,
1198 BlockDriverCompletionFunc *cb, void *opaque)
1200 BlockDriver *drv = bs->drv;
1201 BlockDriverAIOCB *ret;
1207 if (bdrv_wr_badreq_sectors(bs, sector_num, nb_sectors))
1209 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1210 memcpy(bs->boot_sector_data, buf, 512);
1213 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1216 /* Update stats even though technically transfer has not happened. */
1217 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1224 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1226 BlockDriver *drv = acb->bs->drv;
1228 drv->bdrv_aio_cancel(acb);
1232 /**************************************************************/
1233 /* async block device emulation */
1236 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1237 int64_t sector_num, uint8_t *buf, int nb_sectors,
1238 BlockDriverCompletionFunc *cb, void *opaque)
1241 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1246 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1247 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1248 BlockDriverCompletionFunc *cb, void *opaque)
1251 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1256 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1260 static void bdrv_aio_bh_cb(void *opaque)
1262 BlockDriverAIOCBSync *acb = opaque;
1263 acb->common.cb(acb->common.opaque, acb->ret);
1264 qemu_aio_release(acb);
1267 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1268 int64_t sector_num, uint8_t *buf, int nb_sectors,
1269 BlockDriverCompletionFunc *cb, void *opaque)
1271 BlockDriverAIOCBSync *acb;
1274 acb = qemu_aio_get(bs, cb, opaque);
1276 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1277 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1279 qemu_bh_schedule(acb->bh);
1280 return &acb->common;
1283 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1284 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1285 BlockDriverCompletionFunc *cb, void *opaque)
1287 BlockDriverAIOCBSync *acb;
1290 acb = qemu_aio_get(bs, cb, opaque);
1292 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1293 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1295 qemu_bh_schedule(acb->bh);
1296 return &acb->common;
1299 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1301 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1302 qemu_bh_cancel(acb->bh);
1303 qemu_aio_release(acb);
1305 #endif /* !QEMU_IMG */
1307 /**************************************************************/
1308 /* sync block device emulation */
1310 static void bdrv_rw_em_cb(void *opaque, int ret)
1312 *(int *)opaque = ret;
1315 #define NOT_DONE 0x7fffffff
1317 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1318 uint8_t *buf, int nb_sectors)
1321 BlockDriverAIOCB *acb;
1323 async_ret = NOT_DONE;
1324 qemu_aio_wait_start();
1325 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1326 bdrv_rw_em_cb, &async_ret);
1328 qemu_aio_wait_end();
1331 while (async_ret == NOT_DONE) {
1334 qemu_aio_wait_end();
1338 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1339 const uint8_t *buf, int nb_sectors)
1342 BlockDriverAIOCB *acb;
1344 async_ret = NOT_DONE;
1345 qemu_aio_wait_start();
1346 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1347 bdrv_rw_em_cb, &async_ret);
1349 qemu_aio_wait_end();
1352 while (async_ret == NOT_DONE) {
1355 qemu_aio_wait_end();
1359 void bdrv_init(void)
1361 bdrv_register(&bdrv_raw);
1362 bdrv_register(&bdrv_host_device);
1364 bdrv_register(&bdrv_cow);
1366 bdrv_register(&bdrv_qcow);
1367 bdrv_register(&bdrv_vmdk);
1368 bdrv_register(&bdrv_cloop);
1369 bdrv_register(&bdrv_dmg);
1370 bdrv_register(&bdrv_bochs);
1371 bdrv_register(&bdrv_vpc);
1372 bdrv_register(&bdrv_vvfat);
1373 bdrv_register(&bdrv_qcow2);
1374 bdrv_register(&bdrv_parallels);
1377 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1381 BlockDriverAIOCB *acb;
1384 if (drv->free_aiocb) {
1385 acb = drv->free_aiocb;
1386 drv->free_aiocb = acb->next;
1388 acb = qemu_mallocz(drv->aiocb_size);
1394 acb->opaque = opaque;
1398 void qemu_aio_release(void *p)
1400 BlockDriverAIOCB *acb = p;
1401 BlockDriver *drv = acb->bs->drv;
1402 acb->next = drv->free_aiocb;
1403 drv->free_aiocb = acb;
1406 /**************************************************************/
1407 /* removable device support */
1410 * Return TRUE if the media is present
1412 int bdrv_is_inserted(BlockDriverState *bs)
1414 BlockDriver *drv = bs->drv;
1418 if (!drv->bdrv_is_inserted)
1420 ret = drv->bdrv_is_inserted(bs);
1425 * Return TRUE if the media changed since the last call to this
1426 * function. It is currently only used for floppy disks
1428 int bdrv_media_changed(BlockDriverState *bs)
1430 BlockDriver *drv = bs->drv;
1433 if (!drv || !drv->bdrv_media_changed)
1436 ret = drv->bdrv_media_changed(bs);
1437 if (ret == -ENOTSUP)
1438 ret = bs->media_changed;
1439 bs->media_changed = 0;
1444 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1446 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1448 BlockDriver *drv = bs->drv;
1451 if (!drv || !drv->bdrv_eject) {
1454 ret = drv->bdrv_eject(bs, eject_flag);
1456 if (ret == -ENOTSUP) {
1462 int bdrv_is_locked(BlockDriverState *bs)
1468 * Lock or unlock the media (if it is locked, the user won't be able
1469 * to eject it manually).
1471 void bdrv_set_locked(BlockDriverState *bs, int locked)
1473 BlockDriver *drv = bs->drv;
1475 bs->locked = locked;
1476 if (drv && drv->bdrv_set_locked) {
1477 drv->bdrv_set_locked(bs, locked);
1481 /* needed for generic scsi interface */
1483 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1485 BlockDriver *drv = bs->drv;
1487 if (drv && drv->bdrv_ioctl)
1488 return drv->bdrv_ioctl(bs, req, buf);