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"
36 #include <sys/types.h>
38 #include <sys/ioctl.h>
49 #define SECTOR_SIZE (1 << SECTOR_BITS)
51 typedef struct BlockDriverAIOCBSync {
52 BlockDriverAIOCB common;
55 /* vector translation state */
59 } BlockDriverAIOCBSync;
61 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
62 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
63 BlockDriverCompletionFunc *cb, void *opaque);
64 static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
65 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
66 BlockDriverCompletionFunc *cb, void *opaque);
67 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
68 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
69 uint8_t *buf, int nb_sectors);
70 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
71 const uint8_t *buf, int nb_sectors);
73 BlockDriverState *bdrv_first;
75 static BlockDriver *first_drv;
77 int path_is_absolute(const char *path)
81 /* specific case for names like: "\\.\d:" */
82 if (*path == '/' || *path == '\\')
85 p = strchr(path, ':');
91 return (*p == '/' || *p == '\\');
97 /* if filename is absolute, just copy it to dest. Otherwise, build a
98 path to it by considering it is relative to base_path. URL are
100 void path_combine(char *dest, int dest_size,
101 const char *base_path,
102 const char *filename)
109 if (path_is_absolute(filename)) {
110 pstrcpy(dest, dest_size, filename);
112 p = strchr(base_path, ':');
117 p1 = strrchr(base_path, '/');
121 p2 = strrchr(base_path, '\\');
133 if (len > dest_size - 1)
135 memcpy(dest, base_path, len);
137 pstrcat(dest, dest_size, filename);
142 void bdrv_register(BlockDriver *bdrv)
144 if (!bdrv->bdrv_aio_readv) {
145 /* add AIO emulation layer */
146 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
147 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
148 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
149 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
150 } else if (!bdrv->bdrv_read) {
151 /* add synchronous IO emulation layer */
152 bdrv->bdrv_read = bdrv_read_em;
153 bdrv->bdrv_write = bdrv_write_em;
155 aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel);
156 bdrv->next = first_drv;
160 /* create a new block device (by default it is empty) */
161 BlockDriverState *bdrv_new(const char *device_name)
163 BlockDriverState **pbs, *bs;
165 bs = qemu_mallocz(sizeof(BlockDriverState));
166 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
167 if (device_name[0] != '\0') {
168 /* insert at the end */
177 BlockDriver *bdrv_find_format(const char *format_name)
180 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
181 if (!strcmp(drv1->format_name, format_name))
187 int bdrv_create2(BlockDriver *drv,
188 const char *filename, int64_t size_in_sectors,
189 const char *backing_file, const char *backing_format,
192 if (drv->bdrv_create2)
193 return drv->bdrv_create2(filename, size_in_sectors, backing_file,
194 backing_format, flags);
195 if (drv->bdrv_create)
196 return drv->bdrv_create(filename, size_in_sectors, backing_file,
201 int bdrv_create(BlockDriver *drv,
202 const char *filename, int64_t size_in_sectors,
203 const char *backing_file, int flags)
205 if (!drv->bdrv_create)
207 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
211 void get_tmp_filename(char *filename, int size)
213 char temp_dir[MAX_PATH];
215 GetTempPath(MAX_PATH, temp_dir);
216 GetTempFileName(temp_dir, "qem", 0, filename);
219 void get_tmp_filename(char *filename, int size)
223 /* XXX: race condition possible */
224 tmpdir = getenv("TMPDIR");
227 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
228 fd = mkstemp(filename);
234 static int is_windows_drive_prefix(const char *filename)
236 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
237 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
241 static int is_windows_drive(const char *filename)
243 if (is_windows_drive_prefix(filename) &&
246 if (strstart(filename, "\\\\.\\", NULL) ||
247 strstart(filename, "//./", NULL))
253 static BlockDriver *find_protocol(const char *filename)
261 if (is_windows_drive(filename) ||
262 is_windows_drive_prefix(filename))
263 return bdrv_find_format("raw");
265 p = strchr(filename, ':');
267 return bdrv_find_format("raw");
269 if (len > sizeof(protocol) - 1)
270 len = sizeof(protocol) - 1;
271 memcpy(protocol, filename, len);
272 protocol[len] = '\0';
273 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
274 if (drv1->protocol_name &&
275 !strcmp(drv1->protocol_name, protocol))
281 /* XXX: force raw format if block or character device ? It would
282 simplify the BSD case */
283 static BlockDriver *find_image_format(const char *filename)
285 int ret, score, score_max;
286 BlockDriver *drv1, *drv;
288 BlockDriverState *bs;
290 /* detect host devices. By convention, /dev/cdrom[N] is always
291 recognized as a host CDROM */
292 if (strstart(filename, "/dev/cdrom", NULL))
293 return bdrv_find_format("host_device");
295 if (is_windows_drive(filename))
296 return bdrv_find_format("host_device");
300 if (stat(filename, &st) >= 0 &&
301 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
302 return bdrv_find_format("host_device");
307 drv = find_protocol(filename);
308 /* no need to test disk image formats for vvfat */
309 if (drv && strcmp(drv->format_name, "vvfat") == 0)
312 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
315 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
322 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
323 if (drv1->bdrv_probe) {
324 score = drv1->bdrv_probe(buf, ret, filename);
325 if (score > score_max) {
334 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
336 BlockDriverState *bs;
340 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
350 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
352 return bdrv_open2(bs, filename, flags, NULL);
355 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
359 char tmp_filename[PATH_MAX];
360 char backing_filename[PATH_MAX];
363 bs->is_temporary = 0;
366 /* buffer_alignment defaulted to 512, drivers can change this value */
367 bs->buffer_alignment = 512;
369 if (flags & BDRV_O_SNAPSHOT) {
370 BlockDriverState *bs1;
374 /* if snapshot, we create a temporary backing file and open it
375 instead of opening 'filename' directly */
377 /* if there is a backing file, use it */
379 ret = bdrv_open2(bs1, filename, 0, drv);
384 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
386 if (bs1->drv && bs1->drv->protocol_name)
391 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
393 /* Real path is meaningless for protocols */
395 snprintf(backing_filename, sizeof(backing_filename),
398 realpath(filename, backing_filename);
400 ret = bdrv_create2(bdrv_find_format("qcow2"), tmp_filename,
401 total_size, backing_filename,
402 (drv ? drv->format_name : NULL), 0);
406 filename = tmp_filename;
407 drv = bdrv_find_format("qcow2");
408 bs->is_temporary = 1;
411 pstrcpy(bs->filename, sizeof(bs->filename), filename);
412 if (flags & BDRV_O_FILE) {
413 drv = find_protocol(filename);
415 drv = find_image_format(filename);
419 goto unlink_and_fail;
422 bs->opaque = qemu_mallocz(drv->instance_size);
423 /* Note: for compatibility, we open disk image files as RDWR, and
424 RDONLY as fallback */
425 if (!(flags & BDRV_O_FILE))
426 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
428 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
429 ret = drv->bdrv_open(bs, filename, open_flags);
430 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
431 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
435 qemu_free(bs->opaque);
439 if (bs->is_temporary)
443 if (drv->bdrv_getlength) {
444 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
447 if (bs->is_temporary) {
451 if (bs->backing_file[0] != '\0') {
452 /* if there is a backing file, use it */
453 BlockDriver *back_drv = NULL;
454 bs->backing_hd = bdrv_new("");
455 path_combine(backing_filename, sizeof(backing_filename),
456 filename, bs->backing_file);
457 if (bs->backing_format[0] != '\0')
458 back_drv = bdrv_find_format(bs->backing_format);
459 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
467 if (!bdrv_key_required(bs)) {
468 /* call the change callback */
469 bs->media_changed = 1;
471 bs->change_cb(bs->change_opaque);
476 void bdrv_close(BlockDriverState *bs)
480 bdrv_delete(bs->backing_hd);
481 bs->drv->bdrv_close(bs);
482 qemu_free(bs->opaque);
484 if (bs->is_temporary) {
485 unlink(bs->filename);
491 /* call the change callback */
492 bs->media_changed = 1;
494 bs->change_cb(bs->change_opaque);
498 void bdrv_delete(BlockDriverState *bs)
500 BlockDriverState **pbs;
503 while (*pbs != bs && *pbs != NULL)
513 * Run consistency checks on an image
515 * Returns the number of errors or -errno when an internal error occurs
517 int bdrv_check(BlockDriverState *bs)
519 if (bs->drv->bdrv_check == NULL) {
523 return bs->drv->bdrv_check(bs);
526 /* commit COW file into the raw image */
527 int bdrv_commit(BlockDriverState *bs)
529 BlockDriver *drv = bs->drv;
530 int64_t i, total_sectors;
532 unsigned char sector[512];
541 if (!bs->backing_hd) {
545 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
546 for (i = 0; i < total_sectors;) {
547 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
548 for(j = 0; j < n; j++) {
549 if (bdrv_read(bs, i, sector, 1) != 0) {
553 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
563 if (drv->bdrv_make_empty)
564 return drv->bdrv_make_empty(bs);
569 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
574 if (!bdrv_is_inserted(bs))
580 len = bdrv_getlength(bs);
585 if ((offset > len) || (len - offset < size))
591 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
594 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
597 /* return < 0 if error. See bdrv_write() for the return codes */
598 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
599 uint8_t *buf, int nb_sectors)
601 BlockDriver *drv = bs->drv;
605 if (bdrv_check_request(bs, sector_num, nb_sectors))
608 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
611 /* Return < 0 if error. Important errors are:
612 -EIO generic I/O error (may happen for all errors)
613 -ENOMEDIUM No media inserted.
614 -EINVAL Invalid sector number or nb_sectors
615 -EACCES Trying to write a read-only device
617 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
618 const uint8_t *buf, int nb_sectors)
620 BlockDriver *drv = bs->drv;
625 if (bdrv_check_request(bs, sector_num, nb_sectors))
628 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
631 int bdrv_pread(BlockDriverState *bs, int64_t offset,
632 void *buf, int count1)
634 uint8_t tmp_buf[SECTOR_SIZE];
635 int len, nb_sectors, count;
639 /* first read to align to sector start */
640 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
643 sector_num = offset >> SECTOR_BITS;
645 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
647 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
655 /* read the sectors "in place" */
656 nb_sectors = count >> SECTOR_BITS;
657 if (nb_sectors > 0) {
658 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
660 sector_num += nb_sectors;
661 len = nb_sectors << SECTOR_BITS;
666 /* add data from the last sector */
668 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
670 memcpy(buf, tmp_buf, count);
675 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
676 const void *buf, int count1)
678 uint8_t tmp_buf[SECTOR_SIZE];
679 int len, nb_sectors, count;
683 /* first write to align to sector start */
684 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
687 sector_num = offset >> SECTOR_BITS;
689 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
691 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
692 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
701 /* write the sectors "in place" */
702 nb_sectors = count >> SECTOR_BITS;
703 if (nb_sectors > 0) {
704 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
706 sector_num += nb_sectors;
707 len = nb_sectors << SECTOR_BITS;
712 /* add data from the last sector */
714 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
716 memcpy(tmp_buf, buf, count);
717 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
724 * Truncate file to 'offset' bytes (needed only for file protocols)
726 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
728 BlockDriver *drv = bs->drv;
731 if (!drv->bdrv_truncate)
733 return drv->bdrv_truncate(bs, offset);
737 * Length of a file in bytes. Return < 0 if error or unknown.
739 int64_t bdrv_getlength(BlockDriverState *bs)
741 BlockDriver *drv = bs->drv;
744 if (!drv->bdrv_getlength) {
746 return bs->total_sectors * SECTOR_SIZE;
748 return drv->bdrv_getlength(bs);
751 /* return 0 as number of sectors if no device present or error */
752 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
755 length = bdrv_getlength(bs);
759 length = length >> SECTOR_BITS;
760 *nb_sectors_ptr = length;
764 uint8_t boot_ind; /* 0x80 - active */
765 uint8_t head; /* starting head */
766 uint8_t sector; /* starting sector */
767 uint8_t cyl; /* starting cylinder */
768 uint8_t sys_ind; /* What partition type */
769 uint8_t end_head; /* end head */
770 uint8_t end_sector; /* end sector */
771 uint8_t end_cyl; /* end cylinder */
772 uint32_t start_sect; /* starting sector counting from 0 */
773 uint32_t nr_sects; /* nr of sectors in partition */
774 } __attribute__((packed));
776 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
777 static int guess_disk_lchs(BlockDriverState *bs,
778 int *pcylinders, int *pheads, int *psectors)
781 int ret, i, heads, sectors, cylinders;
786 bdrv_get_geometry(bs, &nb_sectors);
788 ret = bdrv_read(bs, 0, buf, 1);
791 /* test msdos magic */
792 if (buf[510] != 0x55 || buf[511] != 0xaa)
794 for(i = 0; i < 4; i++) {
795 p = ((struct partition *)(buf + 0x1be)) + i;
796 nr_sects = le32_to_cpu(p->nr_sects);
797 if (nr_sects && p->end_head) {
798 /* We make the assumption that the partition terminates on
799 a cylinder boundary */
800 heads = p->end_head + 1;
801 sectors = p->end_sector & 63;
804 cylinders = nb_sectors / (heads * sectors);
805 if (cylinders < 1 || cylinders > 16383)
809 *pcylinders = cylinders;
811 printf("guessed geometry: LCHS=%d %d %d\n",
812 cylinders, heads, sectors);
820 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
822 int translation, lba_detected = 0;
823 int cylinders, heads, secs;
826 /* if a geometry hint is available, use it */
827 bdrv_get_geometry(bs, &nb_sectors);
828 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
829 translation = bdrv_get_translation_hint(bs);
830 if (cylinders != 0) {
835 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
837 /* if heads > 16, it means that a BIOS LBA
838 translation was active, so the default
839 hardware geometry is OK */
841 goto default_geometry;
846 /* disable any translation to be in sync with
847 the logical geometry */
848 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
849 bdrv_set_translation_hint(bs,
850 BIOS_ATA_TRANSLATION_NONE);
855 /* if no geometry, use a standard physical disk geometry */
856 cylinders = nb_sectors / (16 * 63);
858 if (cylinders > 16383)
860 else if (cylinders < 2)
865 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
866 if ((*pcyls * *pheads) <= 131072) {
867 bdrv_set_translation_hint(bs,
868 BIOS_ATA_TRANSLATION_LARGE);
870 bdrv_set_translation_hint(bs,
871 BIOS_ATA_TRANSLATION_LBA);
875 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
879 void bdrv_set_geometry_hint(BlockDriverState *bs,
880 int cyls, int heads, int secs)
887 void bdrv_set_type_hint(BlockDriverState *bs, int type)
890 bs->removable = ((type == BDRV_TYPE_CDROM ||
891 type == BDRV_TYPE_FLOPPY));
894 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
896 bs->translation = translation;
899 void bdrv_get_geometry_hint(BlockDriverState *bs,
900 int *pcyls, int *pheads, int *psecs)
907 int bdrv_get_type_hint(BlockDriverState *bs)
912 int bdrv_get_translation_hint(BlockDriverState *bs)
914 return bs->translation;
917 int bdrv_is_removable(BlockDriverState *bs)
919 return bs->removable;
922 int bdrv_is_read_only(BlockDriverState *bs)
924 return bs->read_only;
927 int bdrv_is_sg(BlockDriverState *bs)
932 /* XXX: no longer used */
933 void bdrv_set_change_cb(BlockDriverState *bs,
934 void (*change_cb)(void *opaque), void *opaque)
936 bs->change_cb = change_cb;
937 bs->change_opaque = opaque;
940 int bdrv_is_encrypted(BlockDriverState *bs)
942 if (bs->backing_hd && bs->backing_hd->encrypted)
944 return bs->encrypted;
947 int bdrv_key_required(BlockDriverState *bs)
949 BlockDriverState *backing_hd = bs->backing_hd;
951 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
953 return (bs->encrypted && !bs->valid_key);
956 int bdrv_set_key(BlockDriverState *bs, const char *key)
959 if (bs->backing_hd && bs->backing_hd->encrypted) {
960 ret = bdrv_set_key(bs->backing_hd, key);
966 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
968 ret = bs->drv->bdrv_set_key(bs, key);
971 } else if (!bs->valid_key) {
973 /* call the change callback now, we skipped it on open */
974 bs->media_changed = 1;
976 bs->change_cb(bs->change_opaque);
981 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
986 pstrcpy(buf, buf_size, bs->drv->format_name);
990 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
995 for (drv = first_drv; drv != NULL; drv = drv->next) {
996 it(opaque, drv->format_name);
1000 BlockDriverState *bdrv_find(const char *name)
1002 BlockDriverState *bs;
1004 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1005 if (!strcmp(name, bs->device_name))
1011 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1013 BlockDriverState *bs;
1015 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1020 const char *bdrv_get_device_name(BlockDriverState *bs)
1022 return bs->device_name;
1025 void bdrv_flush(BlockDriverState *bs)
1029 if (bs->drv->bdrv_flush)
1030 bs->drv->bdrv_flush(bs);
1032 bdrv_flush(bs->backing_hd);
1035 void bdrv_flush_all(void)
1037 BlockDriverState *bs;
1039 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1040 if (bs->drv && !bdrv_is_read_only(bs) &&
1041 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1046 * Returns true iff the specified sector is present in the disk image. Drivers
1047 * not implementing the functionality are assumed to not support backing files,
1048 * hence all their sectors are reported as allocated.
1050 * 'pnum' is set to the number of sectors (including and immediately following
1051 * the specified sector) that are known to be in the same
1052 * allocated/unallocated state.
1054 * 'nb_sectors' is the max value 'pnum' should be set to.
1056 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1060 if (!bs->drv->bdrv_is_allocated) {
1061 if (sector_num >= bs->total_sectors) {
1065 n = bs->total_sectors - sector_num;
1066 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1069 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1072 void bdrv_info(Monitor *mon)
1074 BlockDriverState *bs;
1076 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1077 monitor_printf(mon, "%s:", bs->device_name);
1078 monitor_printf(mon, " type=");
1081 monitor_printf(mon, "hd");
1083 case BDRV_TYPE_CDROM:
1084 monitor_printf(mon, "cdrom");
1086 case BDRV_TYPE_FLOPPY:
1087 monitor_printf(mon, "floppy");
1090 monitor_printf(mon, " removable=%d", bs->removable);
1091 if (bs->removable) {
1092 monitor_printf(mon, " locked=%d", bs->locked);
1095 monitor_printf(mon, " file=");
1096 monitor_print_filename(mon, bs->filename);
1097 if (bs->backing_file[0] != '\0') {
1098 monitor_printf(mon, " backing_file=");
1099 monitor_print_filename(mon, bs->backing_file);
1101 monitor_printf(mon, " ro=%d", bs->read_only);
1102 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1103 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1105 monitor_printf(mon, " [not inserted]");
1107 monitor_printf(mon, "\n");
1111 /* The "info blockstats" command. */
1112 void bdrv_info_stats(Monitor *mon)
1114 BlockDriverState *bs;
1116 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1117 monitor_printf(mon, "%s:"
1118 " rd_bytes=%" PRIu64
1119 " wr_bytes=%" PRIu64
1120 " rd_operations=%" PRIu64
1121 " wr_operations=%" PRIu64
1124 bs->rd_bytes, bs->wr_bytes,
1125 bs->rd_ops, bs->wr_ops);
1129 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1131 if (bs->backing_hd && bs->backing_hd->encrypted)
1132 return bs->backing_file;
1133 else if (bs->encrypted)
1134 return bs->filename;
1139 void bdrv_get_backing_filename(BlockDriverState *bs,
1140 char *filename, int filename_size)
1142 if (!bs->backing_hd) {
1143 pstrcpy(filename, filename_size, "");
1145 pstrcpy(filename, filename_size, bs->backing_file);
1149 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1150 const uint8_t *buf, int nb_sectors)
1152 BlockDriver *drv = bs->drv;
1155 if (!drv->bdrv_write_compressed)
1157 if (bdrv_check_request(bs, sector_num, nb_sectors))
1159 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1162 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1164 BlockDriver *drv = bs->drv;
1167 if (!drv->bdrv_get_info)
1169 memset(bdi, 0, sizeof(*bdi));
1170 return drv->bdrv_get_info(bs, bdi);
1173 int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1175 BlockDriver *drv = bs->drv;
1178 if (!drv->bdrv_put_buffer)
1180 return drv->bdrv_put_buffer(bs, buf, pos, size);
1183 int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1185 BlockDriver *drv = bs->drv;
1188 if (!drv->bdrv_get_buffer)
1190 return drv->bdrv_get_buffer(bs, buf, pos, size);
1193 /**************************************************************/
1194 /* handling of snapshots */
1196 int bdrv_snapshot_create(BlockDriverState *bs,
1197 QEMUSnapshotInfo *sn_info)
1199 BlockDriver *drv = bs->drv;
1202 if (!drv->bdrv_snapshot_create)
1204 return drv->bdrv_snapshot_create(bs, sn_info);
1207 int bdrv_snapshot_goto(BlockDriverState *bs,
1208 const char *snapshot_id)
1210 BlockDriver *drv = bs->drv;
1213 if (!drv->bdrv_snapshot_goto)
1215 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1218 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1220 BlockDriver *drv = bs->drv;
1223 if (!drv->bdrv_snapshot_delete)
1225 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1228 int bdrv_snapshot_list(BlockDriverState *bs,
1229 QEMUSnapshotInfo **psn_info)
1231 BlockDriver *drv = bs->drv;
1234 if (!drv->bdrv_snapshot_list)
1236 return drv->bdrv_snapshot_list(bs, psn_info);
1239 #define NB_SUFFIXES 4
1241 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1243 static const char suffixes[NB_SUFFIXES] = "KMGT";
1248 snprintf(buf, buf_size, "%" PRId64, size);
1251 for(i = 0; i < NB_SUFFIXES; i++) {
1252 if (size < (10 * base)) {
1253 snprintf(buf, buf_size, "%0.1f%c",
1254 (double)size / base,
1257 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1258 snprintf(buf, buf_size, "%" PRId64 "%c",
1259 ((size + (base >> 1)) / base),
1269 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1271 char buf1[128], date_buf[128], clock_buf[128];
1281 snprintf(buf, buf_size,
1282 "%-10s%-20s%7s%20s%15s",
1283 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1287 ptm = localtime(&ti);
1288 strftime(date_buf, sizeof(date_buf),
1289 "%Y-%m-%d %H:%M:%S", ptm);
1291 localtime_r(&ti, &tm);
1292 strftime(date_buf, sizeof(date_buf),
1293 "%Y-%m-%d %H:%M:%S", &tm);
1295 secs = sn->vm_clock_nsec / 1000000000;
1296 snprintf(clock_buf, sizeof(clock_buf),
1297 "%02d:%02d:%02d.%03d",
1299 (int)((secs / 60) % 60),
1301 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1302 snprintf(buf, buf_size,
1303 "%-10s%-20s%7s%20s%15s",
1304 sn->id_str, sn->name,
1305 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1313 /**************************************************************/
1316 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1317 QEMUIOVector *qiov, int nb_sectors,
1318 BlockDriverCompletionFunc *cb, void *opaque)
1320 BlockDriver *drv = bs->drv;
1321 BlockDriverAIOCB *ret;
1325 if (bdrv_check_request(bs, sector_num, nb_sectors))
1328 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1332 /* Update stats even though technically transfer has not happened. */
1333 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1340 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1341 QEMUIOVector *qiov, int nb_sectors,
1342 BlockDriverCompletionFunc *cb, void *opaque)
1344 BlockDriver *drv = bs->drv;
1345 BlockDriverAIOCB *ret;
1351 if (bdrv_check_request(bs, sector_num, nb_sectors))
1354 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1358 /* Update stats even though technically transfer has not happened. */
1359 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1366 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1368 acb->pool->cancel(acb);
1372 /**************************************************************/
1373 /* async block device emulation */
1375 static void bdrv_aio_bh_cb(void *opaque)
1377 BlockDriverAIOCBSync *acb = opaque;
1380 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
1381 qemu_vfree(acb->bounce);
1382 acb->common.cb(acb->common.opaque, acb->ret);
1384 qemu_aio_release(acb);
1387 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1391 BlockDriverCompletionFunc *cb,
1396 BlockDriverAIOCBSync *acb;
1398 acb = qemu_aio_get(bs, cb, opaque);
1399 acb->is_write = is_write;
1401 acb->bounce = qemu_blockalign(bs, qiov->size);
1404 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1407 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1408 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1410 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1413 qemu_bh_schedule(acb->bh);
1415 return &acb->common;
1418 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1419 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1420 BlockDriverCompletionFunc *cb, void *opaque)
1422 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
1425 static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1426 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1427 BlockDriverCompletionFunc *cb, void *opaque)
1429 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1433 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1435 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1436 qemu_bh_cancel(acb->bh);
1437 qemu_aio_release(acb);
1440 /**************************************************************/
1441 /* sync block device emulation */
1443 static void bdrv_rw_em_cb(void *opaque, int ret)
1445 *(int *)opaque = ret;
1448 #define NOT_DONE 0x7fffffff
1450 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1451 uint8_t *buf, int nb_sectors)
1454 BlockDriverAIOCB *acb;
1458 async_ret = NOT_DONE;
1459 iov.iov_base = (void *)buf;
1460 iov.iov_len = nb_sectors * 512;
1461 qemu_iovec_init_external(&qiov, &iov, 1);
1462 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
1463 bdrv_rw_em_cb, &async_ret);
1467 while (async_ret == NOT_DONE) {
1474 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1475 const uint8_t *buf, int nb_sectors)
1478 BlockDriverAIOCB *acb;
1482 async_ret = NOT_DONE;
1483 iov.iov_base = (void *)buf;
1484 iov.iov_len = nb_sectors * 512;
1485 qemu_iovec_init_external(&qiov, &iov, 1);
1486 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
1487 bdrv_rw_em_cb, &async_ret);
1490 while (async_ret == NOT_DONE) {
1496 void bdrv_init(void)
1498 module_call_init(MODULE_INIT_BLOCK);
1501 void aio_pool_init(AIOPool *pool, int aiocb_size,
1502 void (*cancel)(BlockDriverAIOCB *acb))
1504 pool->aiocb_size = aiocb_size;
1505 pool->cancel = cancel;
1506 pool->free_aiocb = NULL;
1509 void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1510 BlockDriverCompletionFunc *cb, void *opaque)
1512 BlockDriverAIOCB *acb;
1514 if (pool->free_aiocb) {
1515 acb = pool->free_aiocb;
1516 pool->free_aiocb = acb->next;
1518 acb = qemu_mallocz(pool->aiocb_size);
1523 acb->opaque = opaque;
1527 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1530 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1533 void qemu_aio_release(void *p)
1535 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1536 AIOPool *pool = acb->pool;
1537 acb->next = pool->free_aiocb;
1538 pool->free_aiocb = acb;
1541 /**************************************************************/
1542 /* removable device support */
1545 * Return TRUE if the media is present
1547 int bdrv_is_inserted(BlockDriverState *bs)
1549 BlockDriver *drv = bs->drv;
1553 if (!drv->bdrv_is_inserted)
1555 ret = drv->bdrv_is_inserted(bs);
1560 * Return TRUE if the media changed since the last call to this
1561 * function. It is currently only used for floppy disks
1563 int bdrv_media_changed(BlockDriverState *bs)
1565 BlockDriver *drv = bs->drv;
1568 if (!drv || !drv->bdrv_media_changed)
1571 ret = drv->bdrv_media_changed(bs);
1572 if (ret == -ENOTSUP)
1573 ret = bs->media_changed;
1574 bs->media_changed = 0;
1579 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1581 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1583 BlockDriver *drv = bs->drv;
1586 if (!drv || !drv->bdrv_eject) {
1589 ret = drv->bdrv_eject(bs, eject_flag);
1591 if (ret == -ENOTSUP) {
1597 int bdrv_is_locked(BlockDriverState *bs)
1603 * Lock or unlock the media (if it is locked, the user won't be able
1604 * to eject it manually).
1606 void bdrv_set_locked(BlockDriverState *bs, int locked)
1608 BlockDriver *drv = bs->drv;
1610 bs->locked = locked;
1611 if (drv && drv->bdrv_set_locked) {
1612 drv->bdrv_set_locked(bs, locked);
1616 /* needed for generic scsi interface */
1618 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1620 BlockDriver *drv = bs->drv;
1622 if (drv && drv->bdrv_ioctl)
1623 return drv->bdrv_ioctl(bs, req, buf);
1627 BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1628 unsigned long int req, void *buf,
1629 BlockDriverCompletionFunc *cb, void *opaque)
1631 BlockDriver *drv = bs->drv;
1633 if (drv && drv->bdrv_aio_ioctl)
1634 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1638 void *qemu_blockalign(BlockDriverState *bs, size_t size)
1640 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);