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 "config-host.h"
26 /* include native header before sys-queue.h */
27 #include <sys/queue.h>
30 #include "qemu-common.h"
32 #include "block_int.h"
35 #include <sys/types.h>
37 #include <sys/ioctl.h>
48 #define SECTOR_SIZE (1 << SECTOR_BITS)
50 static AIOPool vectored_aio_pool;
52 typedef struct BlockDriverAIOCBSync {
53 BlockDriverAIOCB common;
56 } BlockDriverAIOCBSync;
58 static BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs,
59 int64_t sector_num, uint8_t *buf, int nb_sectors,
60 BlockDriverCompletionFunc *cb, void *opaque);
61 static BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs,
62 int64_t sector_num, const uint8_t *buf, int nb_sectors,
63 BlockDriverCompletionFunc *cb, void *opaque);
64 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
65 int64_t sector_num, uint8_t *buf, int nb_sectors,
66 BlockDriverCompletionFunc *cb, void *opaque);
67 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
68 int64_t sector_num, const uint8_t *buf, int nb_sectors,
69 BlockDriverCompletionFunc *cb, void *opaque);
70 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
71 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
72 uint8_t *buf, int nb_sectors);
73 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
74 const uint8_t *buf, int nb_sectors);
76 BlockDriverState *bdrv_first;
78 static BlockDriver *first_drv;
80 int path_is_absolute(const char *path)
84 /* specific case for names like: "\\.\d:" */
85 if (*path == '/' || *path == '\\')
88 p = strchr(path, ':');
94 return (*p == '/' || *p == '\\');
100 /* if filename is absolute, just copy it to dest. Otherwise, build a
101 path to it by considering it is relative to base_path. URL are
103 void path_combine(char *dest, int dest_size,
104 const char *base_path,
105 const char *filename)
112 if (path_is_absolute(filename)) {
113 pstrcpy(dest, dest_size, filename);
115 p = strchr(base_path, ':');
120 p1 = strrchr(base_path, '/');
124 p2 = strrchr(base_path, '\\');
136 if (len > dest_size - 1)
138 memcpy(dest, base_path, len);
140 pstrcat(dest, dest_size, filename);
145 static void bdrv_register(BlockDriver *bdrv)
147 if (!bdrv->bdrv_aio_read) {
148 /* add AIO emulation layer */
149 bdrv->bdrv_aio_read = bdrv_aio_read_em;
150 bdrv->bdrv_aio_write = bdrv_aio_write_em;
151 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
152 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
153 } else if (!bdrv->bdrv_read) {
154 /* add synchronous IO emulation layer */
155 bdrv->bdrv_read = bdrv_read_em;
156 bdrv->bdrv_write = bdrv_write_em;
158 aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel);
159 bdrv->next = first_drv;
163 /* create a new block device (by default it is empty) */
164 BlockDriverState *bdrv_new(const char *device_name)
166 BlockDriverState **pbs, *bs;
168 bs = qemu_mallocz(sizeof(BlockDriverState));
169 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
170 if (device_name[0] != '\0') {
171 /* insert at the end */
180 BlockDriver *bdrv_find_format(const char *format_name)
183 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
184 if (!strcmp(drv1->format_name, format_name))
190 int bdrv_create2(BlockDriver *drv,
191 const char *filename, int64_t size_in_sectors,
192 const char *backing_file, const char *backing_format,
195 if (drv->bdrv_create2)
196 return drv->bdrv_create2(filename, size_in_sectors, backing_file,
197 backing_format, flags);
198 if (drv->bdrv_create)
199 return drv->bdrv_create(filename, size_in_sectors, backing_file,
204 int bdrv_create(BlockDriver *drv,
205 const char *filename, int64_t size_in_sectors,
206 const char *backing_file, int flags)
208 if (!drv->bdrv_create)
210 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
214 void get_tmp_filename(char *filename, int size)
216 char temp_dir[MAX_PATH];
218 GetTempPath(MAX_PATH, temp_dir);
219 GetTempFileName(temp_dir, "qem", 0, filename);
222 void get_tmp_filename(char *filename, int size)
226 /* XXX: race condition possible */
227 tmpdir = getenv("TMPDIR");
230 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
231 fd = mkstemp(filename);
237 static int is_windows_drive_prefix(const char *filename)
239 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
240 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
244 static int is_windows_drive(const char *filename)
246 if (is_windows_drive_prefix(filename) &&
249 if (strstart(filename, "\\\\.\\", NULL) ||
250 strstart(filename, "//./", NULL))
256 static BlockDriver *find_protocol(const char *filename)
264 if (is_windows_drive(filename) ||
265 is_windows_drive_prefix(filename))
268 p = strchr(filename, ':');
272 if (len > sizeof(protocol) - 1)
273 len = sizeof(protocol) - 1;
274 memcpy(protocol, filename, len);
275 protocol[len] = '\0';
276 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
277 if (drv1->protocol_name &&
278 !strcmp(drv1->protocol_name, protocol))
284 /* XXX: force raw format if block or character device ? It would
285 simplify the BSD case */
286 static BlockDriver *find_image_format(const char *filename)
288 int ret, score, score_max;
289 BlockDriver *drv1, *drv;
291 BlockDriverState *bs;
293 /* detect host devices. By convention, /dev/cdrom[N] is always
294 recognized as a host CDROM */
295 if (strstart(filename, "/dev/cdrom", NULL))
296 return &bdrv_host_device;
298 if (is_windows_drive(filename))
299 return &bdrv_host_device;
303 if (stat(filename, &st) >= 0 &&
304 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
305 return &bdrv_host_device;
310 drv = find_protocol(filename);
311 /* no need to test disk image formats for vvfat */
312 if (drv == &bdrv_vvfat)
315 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
318 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
325 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
326 if (drv1->bdrv_probe) {
327 score = drv1->bdrv_probe(buf, ret, filename);
328 if (score > score_max) {
337 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
339 BlockDriverState *bs;
343 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
353 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
355 return bdrv_open2(bs, filename, flags, NULL);
358 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
362 char tmp_filename[PATH_MAX];
363 char backing_filename[PATH_MAX];
366 bs->is_temporary = 0;
370 if (flags & BDRV_O_SNAPSHOT) {
371 BlockDriverState *bs1;
375 /* if snapshot, we create a temporary backing file and open it
376 instead of opening 'filename' directly */
378 /* if there is a backing file, use it */
380 ret = bdrv_open2(bs1, filename, 0, drv);
385 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
387 if (bs1->drv && bs1->drv->protocol_name)
392 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
394 /* Real path is meaningless for protocols */
396 snprintf(backing_filename, sizeof(backing_filename),
399 realpath(filename, backing_filename);
401 ret = bdrv_create2(&bdrv_qcow2, tmp_filename,
402 total_size, backing_filename,
403 (drv ? drv->format_name : NULL), 0);
407 filename = tmp_filename;
409 bs->is_temporary = 1;
412 pstrcpy(bs->filename, sizeof(bs->filename), filename);
413 if (flags & BDRV_O_FILE) {
414 drv = find_protocol(filename);
416 drv = find_image_format(filename);
420 goto unlink_and_fail;
423 bs->opaque = qemu_mallocz(drv->instance_size);
424 /* Note: for compatibility, we open disk image files as RDWR, and
425 RDONLY as fallback */
426 if (!(flags & BDRV_O_FILE))
427 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
429 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
430 ret = drv->bdrv_open(bs, filename, open_flags);
431 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
432 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
436 qemu_free(bs->opaque);
440 if (bs->is_temporary)
444 if (drv->bdrv_getlength) {
445 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
448 if (bs->is_temporary) {
452 if (bs->backing_file[0] != '\0') {
453 /* if there is a backing file, use it */
454 BlockDriver *back_drv = NULL;
455 bs->backing_hd = bdrv_new("");
456 path_combine(backing_filename, sizeof(backing_filename),
457 filename, bs->backing_file);
458 if (bs->backing_format[0] != '\0')
459 back_drv = bdrv_find_format(bs->backing_format);
460 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
468 if (!bdrv_key_required(bs)) {
469 /* call the change callback */
470 bs->media_changed = 1;
472 bs->change_cb(bs->change_opaque);
477 void bdrv_close(BlockDriverState *bs)
481 bdrv_delete(bs->backing_hd);
482 bs->drv->bdrv_close(bs);
483 qemu_free(bs->opaque);
485 if (bs->is_temporary) {
486 unlink(bs->filename);
492 /* call the change callback */
493 bs->media_changed = 1;
495 bs->change_cb(bs->change_opaque);
499 void bdrv_delete(BlockDriverState *bs)
501 BlockDriverState **pbs;
504 while (*pbs != bs && *pbs != NULL)
513 /* commit COW file into the raw image */
514 int bdrv_commit(BlockDriverState *bs)
516 BlockDriver *drv = bs->drv;
517 int64_t i, total_sectors;
519 unsigned char sector[512];
528 if (!bs->backing_hd) {
532 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
533 for (i = 0; i < total_sectors;) {
534 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
535 for(j = 0; j < n; j++) {
536 if (bdrv_read(bs, i, sector, 1) != 0) {
540 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
550 if (drv->bdrv_make_empty)
551 return drv->bdrv_make_empty(bs);
556 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
561 if (!bdrv_is_inserted(bs))
567 len = bdrv_getlength(bs);
569 if ((offset + size) > len)
575 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
578 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
581 /* return < 0 if error. See bdrv_write() for the return codes */
582 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
583 uint8_t *buf, int nb_sectors)
585 BlockDriver *drv = bs->drv;
589 if (bdrv_check_request(bs, sector_num, nb_sectors))
592 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
595 /* Return < 0 if error. Important errors are:
596 -EIO generic I/O error (may happen for all errors)
597 -ENOMEDIUM No media inserted.
598 -EINVAL Invalid sector number or nb_sectors
599 -EACCES Trying to write a read-only device
601 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
602 const uint8_t *buf, int nb_sectors)
604 BlockDriver *drv = bs->drv;
609 if (bdrv_check_request(bs, sector_num, nb_sectors))
612 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
615 int bdrv_pread(BlockDriverState *bs, int64_t offset,
616 void *buf, int count1)
618 uint8_t tmp_buf[SECTOR_SIZE];
619 int len, nb_sectors, count;
623 /* first read to align to sector start */
624 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
627 sector_num = offset >> SECTOR_BITS;
629 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
631 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
639 /* read the sectors "in place" */
640 nb_sectors = count >> SECTOR_BITS;
641 if (nb_sectors > 0) {
642 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
644 sector_num += nb_sectors;
645 len = nb_sectors << SECTOR_BITS;
650 /* add data from the last sector */
652 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
654 memcpy(buf, tmp_buf, count);
659 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
660 const void *buf, int count1)
662 uint8_t tmp_buf[SECTOR_SIZE];
663 int len, nb_sectors, count;
667 /* first write to align to sector start */
668 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
671 sector_num = offset >> SECTOR_BITS;
673 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
675 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
676 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
685 /* write the sectors "in place" */
686 nb_sectors = count >> SECTOR_BITS;
687 if (nb_sectors > 0) {
688 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
690 sector_num += nb_sectors;
691 len = nb_sectors << SECTOR_BITS;
696 /* add data from the last sector */
698 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
700 memcpy(tmp_buf, buf, count);
701 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
708 * Truncate file to 'offset' bytes (needed only for file protocols)
710 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
712 BlockDriver *drv = bs->drv;
715 if (!drv->bdrv_truncate)
717 return drv->bdrv_truncate(bs, offset);
721 * Length of a file in bytes. Return < 0 if error or unknown.
723 int64_t bdrv_getlength(BlockDriverState *bs)
725 BlockDriver *drv = bs->drv;
728 if (!drv->bdrv_getlength) {
730 return bs->total_sectors * SECTOR_SIZE;
732 return drv->bdrv_getlength(bs);
735 /* return 0 as number of sectors if no device present or error */
736 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
739 length = bdrv_getlength(bs);
743 length = length >> SECTOR_BITS;
744 *nb_sectors_ptr = length;
748 uint8_t boot_ind; /* 0x80 - active */
749 uint8_t head; /* starting head */
750 uint8_t sector; /* starting sector */
751 uint8_t cyl; /* starting cylinder */
752 uint8_t sys_ind; /* What partition type */
753 uint8_t end_head; /* end head */
754 uint8_t end_sector; /* end sector */
755 uint8_t end_cyl; /* end cylinder */
756 uint32_t start_sect; /* starting sector counting from 0 */
757 uint32_t nr_sects; /* nr of sectors in partition */
758 } __attribute__((packed));
760 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
761 static int guess_disk_lchs(BlockDriverState *bs,
762 int *pcylinders, int *pheads, int *psectors)
765 int ret, i, heads, sectors, cylinders;
770 bdrv_get_geometry(bs, &nb_sectors);
772 ret = bdrv_read(bs, 0, buf, 1);
775 /* test msdos magic */
776 if (buf[510] != 0x55 || buf[511] != 0xaa)
778 for(i = 0; i < 4; i++) {
779 p = ((struct partition *)(buf + 0x1be)) + i;
780 nr_sects = le32_to_cpu(p->nr_sects);
781 if (nr_sects && p->end_head) {
782 /* We make the assumption that the partition terminates on
783 a cylinder boundary */
784 heads = p->end_head + 1;
785 sectors = p->end_sector & 63;
788 cylinders = nb_sectors / (heads * sectors);
789 if (cylinders < 1 || cylinders > 16383)
793 *pcylinders = cylinders;
795 printf("guessed geometry: LCHS=%d %d %d\n",
796 cylinders, heads, sectors);
804 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
806 int translation, lba_detected = 0;
807 int cylinders, heads, secs;
810 /* if a geometry hint is available, use it */
811 bdrv_get_geometry(bs, &nb_sectors);
812 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
813 translation = bdrv_get_translation_hint(bs);
814 if (cylinders != 0) {
819 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
821 /* if heads > 16, it means that a BIOS LBA
822 translation was active, so the default
823 hardware geometry is OK */
825 goto default_geometry;
830 /* disable any translation to be in sync with
831 the logical geometry */
832 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
833 bdrv_set_translation_hint(bs,
834 BIOS_ATA_TRANSLATION_NONE);
839 /* if no geometry, use a standard physical disk geometry */
840 cylinders = nb_sectors / (16 * 63);
842 if (cylinders > 16383)
844 else if (cylinders < 2)
849 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
850 if ((*pcyls * *pheads) <= 131072) {
851 bdrv_set_translation_hint(bs,
852 BIOS_ATA_TRANSLATION_LARGE);
854 bdrv_set_translation_hint(bs,
855 BIOS_ATA_TRANSLATION_LBA);
859 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
863 void bdrv_set_geometry_hint(BlockDriverState *bs,
864 int cyls, int heads, int secs)
871 void bdrv_set_type_hint(BlockDriverState *bs, int type)
874 bs->removable = ((type == BDRV_TYPE_CDROM ||
875 type == BDRV_TYPE_FLOPPY));
878 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
880 bs->translation = translation;
883 void bdrv_get_geometry_hint(BlockDriverState *bs,
884 int *pcyls, int *pheads, int *psecs)
891 int bdrv_get_type_hint(BlockDriverState *bs)
896 int bdrv_get_translation_hint(BlockDriverState *bs)
898 return bs->translation;
901 int bdrv_is_removable(BlockDriverState *bs)
903 return bs->removable;
906 int bdrv_is_read_only(BlockDriverState *bs)
908 return bs->read_only;
911 int bdrv_is_sg(BlockDriverState *bs)
916 /* XXX: no longer used */
917 void bdrv_set_change_cb(BlockDriverState *bs,
918 void (*change_cb)(void *opaque), void *opaque)
920 bs->change_cb = change_cb;
921 bs->change_opaque = opaque;
924 int bdrv_is_encrypted(BlockDriverState *bs)
926 if (bs->backing_hd && bs->backing_hd->encrypted)
928 return bs->encrypted;
931 int bdrv_key_required(BlockDriverState *bs)
933 BlockDriverState *backing_hd = bs->backing_hd;
935 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
937 return (bs->encrypted && !bs->valid_key);
940 int bdrv_set_key(BlockDriverState *bs, const char *key)
943 if (bs->backing_hd && bs->backing_hd->encrypted) {
944 ret = bdrv_set_key(bs->backing_hd, key);
950 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
952 ret = bs->drv->bdrv_set_key(bs, key);
955 } else if (!bs->valid_key) {
957 /* call the change callback now, we skipped it on open */
958 bs->media_changed = 1;
960 bs->change_cb(bs->change_opaque);
965 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
970 pstrcpy(buf, buf_size, bs->drv->format_name);
974 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
979 for (drv = first_drv; drv != NULL; drv = drv->next) {
980 it(opaque, drv->format_name);
984 BlockDriverState *bdrv_find(const char *name)
986 BlockDriverState *bs;
988 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
989 if (!strcmp(name, bs->device_name))
995 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
997 BlockDriverState *bs;
999 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1004 const char *bdrv_get_device_name(BlockDriverState *bs)
1006 return bs->device_name;
1009 void bdrv_flush(BlockDriverState *bs)
1013 if (bs->drv->bdrv_flush)
1014 bs->drv->bdrv_flush(bs);
1016 bdrv_flush(bs->backing_hd);
1019 void bdrv_flush_all(void)
1021 BlockDriverState *bs;
1023 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1024 if (bs->drv && !bdrv_is_read_only(bs) &&
1025 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1030 * Returns true iff the specified sector is present in the disk image. Drivers
1031 * not implementing the functionality are assumed to not support backing files,
1032 * hence all their sectors are reported as allocated.
1034 * 'pnum' is set to the number of sectors (including and immediately following
1035 * the specified sector) that are known to be in the same
1036 * allocated/unallocated state.
1038 * 'nb_sectors' is the max value 'pnum' should be set to.
1040 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1044 if (!bs->drv->bdrv_is_allocated) {
1045 if (sector_num >= bs->total_sectors) {
1049 n = bs->total_sectors - sector_num;
1050 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1053 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1056 void bdrv_info(Monitor *mon)
1058 BlockDriverState *bs;
1060 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1061 monitor_printf(mon, "%s:", bs->device_name);
1062 monitor_printf(mon, " type=");
1065 monitor_printf(mon, "hd");
1067 case BDRV_TYPE_CDROM:
1068 monitor_printf(mon, "cdrom");
1070 case BDRV_TYPE_FLOPPY:
1071 monitor_printf(mon, "floppy");
1074 monitor_printf(mon, " removable=%d", bs->removable);
1075 if (bs->removable) {
1076 monitor_printf(mon, " locked=%d", bs->locked);
1079 monitor_printf(mon, " file=");
1080 monitor_print_filename(mon, bs->filename);
1081 if (bs->backing_file[0] != '\0') {
1082 monitor_printf(mon, " backing_file=");
1083 monitor_print_filename(mon, bs->backing_file);
1085 monitor_printf(mon, " ro=%d", bs->read_only);
1086 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1087 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1089 monitor_printf(mon, " [not inserted]");
1091 monitor_printf(mon, "\n");
1095 /* The "info blockstats" command. */
1096 void bdrv_info_stats(Monitor *mon)
1098 BlockDriverState *bs;
1100 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1101 monitor_printf(mon, "%s:"
1102 " rd_bytes=%" PRIu64
1103 " wr_bytes=%" PRIu64
1104 " rd_operations=%" PRIu64
1105 " wr_operations=%" PRIu64
1108 bs->rd_bytes, bs->wr_bytes,
1109 bs->rd_ops, bs->wr_ops);
1113 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1115 if (bs->backing_hd && bs->backing_hd->encrypted)
1116 return bs->backing_file;
1117 else if (bs->encrypted)
1118 return bs->filename;
1123 void bdrv_get_backing_filename(BlockDriverState *bs,
1124 char *filename, int filename_size)
1126 if (!bs->backing_hd) {
1127 pstrcpy(filename, filename_size, "");
1129 pstrcpy(filename, filename_size, bs->backing_file);
1133 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1134 const uint8_t *buf, int nb_sectors)
1136 BlockDriver *drv = bs->drv;
1139 if (!drv->bdrv_write_compressed)
1141 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1144 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1146 BlockDriver *drv = bs->drv;
1149 if (!drv->bdrv_get_info)
1151 memset(bdi, 0, sizeof(*bdi));
1152 return drv->bdrv_get_info(bs, bdi);
1155 int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1157 BlockDriver *drv = bs->drv;
1160 if (!drv->bdrv_put_buffer)
1162 return drv->bdrv_put_buffer(bs, buf, pos, size);
1165 int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1167 BlockDriver *drv = bs->drv;
1170 if (!drv->bdrv_get_buffer)
1172 return drv->bdrv_get_buffer(bs, buf, pos, size);
1175 /**************************************************************/
1176 /* handling of snapshots */
1178 int bdrv_snapshot_create(BlockDriverState *bs,
1179 QEMUSnapshotInfo *sn_info)
1181 BlockDriver *drv = bs->drv;
1184 if (!drv->bdrv_snapshot_create)
1186 return drv->bdrv_snapshot_create(bs, sn_info);
1189 int bdrv_snapshot_goto(BlockDriverState *bs,
1190 const char *snapshot_id)
1192 BlockDriver *drv = bs->drv;
1195 if (!drv->bdrv_snapshot_goto)
1197 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1200 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1202 BlockDriver *drv = bs->drv;
1205 if (!drv->bdrv_snapshot_delete)
1207 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1210 int bdrv_snapshot_list(BlockDriverState *bs,
1211 QEMUSnapshotInfo **psn_info)
1213 BlockDriver *drv = bs->drv;
1216 if (!drv->bdrv_snapshot_list)
1218 return drv->bdrv_snapshot_list(bs, psn_info);
1221 #define NB_SUFFIXES 4
1223 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1225 static const char suffixes[NB_SUFFIXES] = "KMGT";
1230 snprintf(buf, buf_size, "%" PRId64, size);
1233 for(i = 0; i < NB_SUFFIXES; i++) {
1234 if (size < (10 * base)) {
1235 snprintf(buf, buf_size, "%0.1f%c",
1236 (double)size / base,
1239 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1240 snprintf(buf, buf_size, "%" PRId64 "%c",
1241 ((size + (base >> 1)) / base),
1251 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1253 char buf1[128], date_buf[128], clock_buf[128];
1263 snprintf(buf, buf_size,
1264 "%-10s%-20s%7s%20s%15s",
1265 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1269 ptm = localtime(&ti);
1270 strftime(date_buf, sizeof(date_buf),
1271 "%Y-%m-%d %H:%M:%S", ptm);
1273 localtime_r(&ti, &tm);
1274 strftime(date_buf, sizeof(date_buf),
1275 "%Y-%m-%d %H:%M:%S", &tm);
1277 secs = sn->vm_clock_nsec / 1000000000;
1278 snprintf(clock_buf, sizeof(clock_buf),
1279 "%02d:%02d:%02d.%03d",
1281 (int)((secs / 60) % 60),
1283 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1284 snprintf(buf, buf_size,
1285 "%-10s%-20s%7s%20s%15s",
1286 sn->id_str, sn->name,
1287 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1295 /**************************************************************/
1298 typedef struct VectorTranslationAIOCB {
1299 BlockDriverAIOCB common;
1303 BlockDriverAIOCB *aiocb;
1304 } VectorTranslationAIOCB;
1306 static void bdrv_aio_cancel_vector(BlockDriverAIOCB *_acb)
1308 VectorTranslationAIOCB *acb
1309 = container_of(_acb, VectorTranslationAIOCB, common);
1311 bdrv_aio_cancel(acb->aiocb);
1314 static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1316 VectorTranslationAIOCB *s = (VectorTranslationAIOCB *)opaque;
1319 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
1321 qemu_vfree(s->bounce);
1322 s->common.cb(s->common.opaque, ret);
1323 qemu_aio_release(s);
1326 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1330 BlockDriverCompletionFunc *cb,
1335 VectorTranslationAIOCB *s = qemu_aio_get_pool(&vectored_aio_pool, bs,
1339 s->bounce = qemu_memalign(512, nb_sectors * 512);
1340 s->is_write = is_write;
1342 qemu_iovec_to_buffer(s->iov, s->bounce);
1343 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1344 bdrv_aio_rw_vector_cb, s);
1346 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1347 bdrv_aio_rw_vector_cb, s);
1350 qemu_vfree(s->bounce);
1351 qemu_aio_release(s);
1357 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1358 QEMUIOVector *iov, int nb_sectors,
1359 BlockDriverCompletionFunc *cb, void *opaque)
1361 if (bdrv_check_request(bs, sector_num, nb_sectors))
1364 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1368 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1369 QEMUIOVector *iov, int nb_sectors,
1370 BlockDriverCompletionFunc *cb, void *opaque)
1372 if (bdrv_check_request(bs, sector_num, nb_sectors))
1375 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1379 static BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1380 uint8_t *buf, int nb_sectors,
1381 BlockDriverCompletionFunc *cb, void *opaque)
1383 BlockDriver *drv = bs->drv;
1384 BlockDriverAIOCB *ret;
1388 if (bdrv_check_request(bs, sector_num, nb_sectors))
1391 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1394 /* Update stats even though technically transfer has not happened. */
1395 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1402 static BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1403 const uint8_t *buf, int nb_sectors,
1404 BlockDriverCompletionFunc *cb, void *opaque)
1406 BlockDriver *drv = bs->drv;
1407 BlockDriverAIOCB *ret;
1413 if (bdrv_check_request(bs, sector_num, nb_sectors))
1416 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1419 /* Update stats even though technically transfer has not happened. */
1420 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1427 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1429 acb->pool->cancel(acb);
1433 /**************************************************************/
1434 /* async block device emulation */
1436 static void bdrv_aio_bh_cb(void *opaque)
1438 BlockDriverAIOCBSync *acb = opaque;
1439 acb->common.cb(acb->common.opaque, acb->ret);
1440 qemu_aio_release(acb);
1443 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1444 int64_t sector_num, uint8_t *buf, int nb_sectors,
1445 BlockDriverCompletionFunc *cb, void *opaque)
1447 BlockDriverAIOCBSync *acb;
1450 acb = qemu_aio_get(bs, cb, opaque);
1452 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1453 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1455 qemu_bh_schedule(acb->bh);
1456 return &acb->common;
1459 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1460 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1461 BlockDriverCompletionFunc *cb, void *opaque)
1463 BlockDriverAIOCBSync *acb;
1466 acb = qemu_aio_get(bs, cb, opaque);
1468 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1469 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1471 qemu_bh_schedule(acb->bh);
1472 return &acb->common;
1475 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1477 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1478 qemu_bh_cancel(acb->bh);
1479 qemu_aio_release(acb);
1482 /**************************************************************/
1483 /* sync block device emulation */
1485 static void bdrv_rw_em_cb(void *opaque, int ret)
1487 *(int *)opaque = ret;
1490 #define NOT_DONE 0x7fffffff
1492 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1493 uint8_t *buf, int nb_sectors)
1496 BlockDriverAIOCB *acb;
1498 async_ret = NOT_DONE;
1499 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1500 bdrv_rw_em_cb, &async_ret);
1504 while (async_ret == NOT_DONE) {
1511 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1512 const uint8_t *buf, int nb_sectors)
1515 BlockDriverAIOCB *acb;
1517 async_ret = NOT_DONE;
1518 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1519 bdrv_rw_em_cb, &async_ret);
1522 while (async_ret == NOT_DONE) {
1528 void bdrv_init(void)
1530 aio_pool_init(&vectored_aio_pool, sizeof(VectorTranslationAIOCB),
1531 bdrv_aio_cancel_vector);
1533 bdrv_register(&bdrv_raw);
1534 bdrv_register(&bdrv_host_device);
1536 bdrv_register(&bdrv_cow);
1538 bdrv_register(&bdrv_qcow);
1539 bdrv_register(&bdrv_vmdk);
1540 bdrv_register(&bdrv_cloop);
1541 bdrv_register(&bdrv_dmg);
1542 bdrv_register(&bdrv_bochs);
1543 bdrv_register(&bdrv_vpc);
1544 bdrv_register(&bdrv_vvfat);
1545 bdrv_register(&bdrv_qcow2);
1546 bdrv_register(&bdrv_parallels);
1547 bdrv_register(&bdrv_nbd);
1550 void aio_pool_init(AIOPool *pool, int aiocb_size,
1551 void (*cancel)(BlockDriverAIOCB *acb))
1553 pool->aiocb_size = aiocb_size;
1554 pool->cancel = cancel;
1555 pool->free_aiocb = NULL;
1558 void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1559 BlockDriverCompletionFunc *cb, void *opaque)
1561 BlockDriverAIOCB *acb;
1563 if (pool->free_aiocb) {
1564 acb = pool->free_aiocb;
1565 pool->free_aiocb = acb->next;
1567 acb = qemu_mallocz(pool->aiocb_size);
1572 acb->opaque = opaque;
1576 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1579 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1582 void qemu_aio_release(void *p)
1584 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1585 AIOPool *pool = acb->pool;
1586 acb->next = pool->free_aiocb;
1587 pool->free_aiocb = acb;
1590 /**************************************************************/
1591 /* removable device support */
1594 * Return TRUE if the media is present
1596 int bdrv_is_inserted(BlockDriverState *bs)
1598 BlockDriver *drv = bs->drv;
1602 if (!drv->bdrv_is_inserted)
1604 ret = drv->bdrv_is_inserted(bs);
1609 * Return TRUE if the media changed since the last call to this
1610 * function. It is currently only used for floppy disks
1612 int bdrv_media_changed(BlockDriverState *bs)
1614 BlockDriver *drv = bs->drv;
1617 if (!drv || !drv->bdrv_media_changed)
1620 ret = drv->bdrv_media_changed(bs);
1621 if (ret == -ENOTSUP)
1622 ret = bs->media_changed;
1623 bs->media_changed = 0;
1628 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1630 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1632 BlockDriver *drv = bs->drv;
1635 if (!drv || !drv->bdrv_eject) {
1638 ret = drv->bdrv_eject(bs, eject_flag);
1640 if (ret == -ENOTSUP) {
1646 int bdrv_is_locked(BlockDriverState *bs)
1652 * Lock or unlock the media (if it is locked, the user won't be able
1653 * to eject it manually).
1655 void bdrv_set_locked(BlockDriverState *bs, int locked)
1657 BlockDriver *drv = bs->drv;
1659 bs->locked = locked;
1660 if (drv && drv->bdrv_set_locked) {
1661 drv->bdrv_set_locked(bs, locked);
1665 /* needed for generic scsi interface */
1667 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1669 BlockDriver *drv = bs->drv;
1671 if (drv && drv->bdrv_ioctl)
1672 return drv->bdrv_ioctl(bs, req, buf);
1676 BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1677 unsigned long int req, void *buf,
1678 BlockDriverCompletionFunc *cb, void *opaque)
1680 BlockDriver *drv = bs->drv;
1682 if (drv && drv->bdrv_aio_ioctl)
1683 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);