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 QEMUOptionParameter *options;
194 options = parse_option_parameters("", drv->create_options, NULL);
197 if (flags & ~(BLOCK_FLAG_ENCRYPT | BLOCK_FLAG_COMPAT6 | BLOCK_FLAG_COMPRESS)) {
201 if (flags & BLOCK_FLAG_ENCRYPT) {
202 set_option_parameter_int(options, BLOCK_OPT_ENCRYPT, 1);
204 if (flags & BLOCK_FLAG_COMPAT6) {
205 set_option_parameter_int(options, BLOCK_OPT_COMPAT6, 1);
208 // Add size to options
209 set_option_parameter_int(options, BLOCK_OPT_SIZE, size_in_sectors * 512);
212 if ((backing_file != NULL && set_option_parameter(options,
213 BLOCK_OPT_BACKING_FILE, backing_file))
214 || (backing_format != NULL && set_option_parameter(options,
215 BLOCK_OPT_BACKING_FMT, backing_format)))
220 return bdrv_create(drv, filename, options);
223 int bdrv_create(BlockDriver *drv, const char* filename,
224 QEMUOptionParameter *options)
226 if (!drv->bdrv_create)
229 return drv->bdrv_create(filename, options);
233 void get_tmp_filename(char *filename, int size)
235 char temp_dir[MAX_PATH];
237 GetTempPath(MAX_PATH, temp_dir);
238 GetTempFileName(temp_dir, "qem", 0, filename);
241 void get_tmp_filename(char *filename, int size)
245 /* XXX: race condition possible */
246 tmpdir = getenv("TMPDIR");
249 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
250 fd = mkstemp(filename);
256 static int is_windows_drive_prefix(const char *filename)
258 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
259 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
263 static int is_windows_drive(const char *filename)
265 if (is_windows_drive_prefix(filename) &&
268 if (strstart(filename, "\\\\.\\", NULL) ||
269 strstart(filename, "//./", NULL))
275 static BlockDriver *find_protocol(const char *filename)
283 if (is_windows_drive(filename) ||
284 is_windows_drive_prefix(filename))
285 return bdrv_find_format("raw");
287 p = strchr(filename, ':');
289 return bdrv_find_format("raw");
291 if (len > sizeof(protocol) - 1)
292 len = sizeof(protocol) - 1;
293 memcpy(protocol, filename, len);
294 protocol[len] = '\0';
295 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
296 if (drv1->protocol_name &&
297 !strcmp(drv1->protocol_name, protocol))
303 /* XXX: force raw format if block or character device ? It would
304 simplify the BSD case */
305 static BlockDriver *find_image_format(const char *filename)
307 int ret, score, score_max;
308 BlockDriver *drv1, *drv;
310 BlockDriverState *bs;
312 /* detect host devices. By convention, /dev/cdrom[N] is always
313 recognized as a host CDROM */
314 if (strstart(filename, "/dev/cdrom", NULL))
315 return bdrv_find_format("host_device");
317 if (is_windows_drive(filename))
318 return bdrv_find_format("host_device");
322 if (stat(filename, &st) >= 0 &&
323 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
324 return bdrv_find_format("host_device");
329 drv = find_protocol(filename);
330 /* no need to test disk image formats for vvfat */
331 if (drv && strcmp(drv->format_name, "vvfat") == 0)
334 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
337 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
344 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
345 if (drv1->bdrv_probe) {
346 score = drv1->bdrv_probe(buf, ret, filename);
347 if (score > score_max) {
356 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
358 BlockDriverState *bs;
362 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
372 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
374 return bdrv_open2(bs, filename, flags, NULL);
377 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
381 char tmp_filename[PATH_MAX];
382 char backing_filename[PATH_MAX];
385 bs->is_temporary = 0;
388 /* buffer_alignment defaulted to 512, drivers can change this value */
389 bs->buffer_alignment = 512;
391 if (flags & BDRV_O_SNAPSHOT) {
392 BlockDriverState *bs1;
396 /* if snapshot, we create a temporary backing file and open it
397 instead of opening 'filename' directly */
399 /* if there is a backing file, use it */
401 ret = bdrv_open2(bs1, filename, 0, drv);
406 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
408 if (bs1->drv && bs1->drv->protocol_name)
413 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
415 /* Real path is meaningless for protocols */
417 snprintf(backing_filename, sizeof(backing_filename),
420 realpath(filename, backing_filename);
422 ret = bdrv_create2(bdrv_find_format("qcow2"), tmp_filename,
423 total_size, backing_filename,
424 (drv ? drv->format_name : NULL), 0);
428 filename = tmp_filename;
429 drv = bdrv_find_format("qcow2");
430 bs->is_temporary = 1;
433 pstrcpy(bs->filename, sizeof(bs->filename), filename);
434 if (flags & BDRV_O_FILE) {
435 drv = find_protocol(filename);
437 drv = find_image_format(filename);
441 goto unlink_and_fail;
444 bs->opaque = qemu_mallocz(drv->instance_size);
445 /* Note: for compatibility, we open disk image files as RDWR, and
446 RDONLY as fallback */
447 if (!(flags & BDRV_O_FILE))
448 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
450 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
451 ret = drv->bdrv_open(bs, filename, open_flags);
452 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
453 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
457 qemu_free(bs->opaque);
461 if (bs->is_temporary)
465 if (drv->bdrv_getlength) {
466 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
469 if (bs->is_temporary) {
473 if (bs->backing_file[0] != '\0') {
474 /* if there is a backing file, use it */
475 BlockDriver *back_drv = NULL;
476 bs->backing_hd = bdrv_new("");
477 path_combine(backing_filename, sizeof(backing_filename),
478 filename, bs->backing_file);
479 if (bs->backing_format[0] != '\0')
480 back_drv = bdrv_find_format(bs->backing_format);
481 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
489 if (!bdrv_key_required(bs)) {
490 /* call the change callback */
491 bs->media_changed = 1;
493 bs->change_cb(bs->change_opaque);
498 void bdrv_close(BlockDriverState *bs)
502 bdrv_delete(bs->backing_hd);
503 bs->drv->bdrv_close(bs);
504 qemu_free(bs->opaque);
506 if (bs->is_temporary) {
507 unlink(bs->filename);
513 /* call the change callback */
514 bs->media_changed = 1;
516 bs->change_cb(bs->change_opaque);
520 void bdrv_delete(BlockDriverState *bs)
522 BlockDriverState **pbs;
525 while (*pbs != bs && *pbs != NULL)
535 * Run consistency checks on an image
537 * Returns the number of errors or -errno when an internal error occurs
539 int bdrv_check(BlockDriverState *bs)
541 if (bs->drv->bdrv_check == NULL) {
545 return bs->drv->bdrv_check(bs);
548 /* commit COW file into the raw image */
549 int bdrv_commit(BlockDriverState *bs)
551 BlockDriver *drv = bs->drv;
552 int64_t i, total_sectors;
554 unsigned char sector[512];
563 if (!bs->backing_hd) {
567 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
568 for (i = 0; i < total_sectors;) {
569 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
570 for(j = 0; j < n; j++) {
571 if (bdrv_read(bs, i, sector, 1) != 0) {
575 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
585 if (drv->bdrv_make_empty)
586 return drv->bdrv_make_empty(bs);
591 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
596 if (!bdrv_is_inserted(bs))
602 len = bdrv_getlength(bs);
607 if ((offset > len) || (len - offset < size))
613 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
616 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
619 /* return < 0 if error. See bdrv_write() for the return codes */
620 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
621 uint8_t *buf, int nb_sectors)
623 BlockDriver *drv = bs->drv;
627 if (bdrv_check_request(bs, sector_num, nb_sectors))
630 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
633 /* Return < 0 if error. Important errors are:
634 -EIO generic I/O error (may happen for all errors)
635 -ENOMEDIUM No media inserted.
636 -EINVAL Invalid sector number or nb_sectors
637 -EACCES Trying to write a read-only device
639 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
640 const uint8_t *buf, int nb_sectors)
642 BlockDriver *drv = bs->drv;
647 if (bdrv_check_request(bs, sector_num, nb_sectors))
650 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
653 int bdrv_pread(BlockDriverState *bs, int64_t offset,
654 void *buf, int count1)
656 uint8_t tmp_buf[SECTOR_SIZE];
657 int len, nb_sectors, count;
661 /* first read to align to sector start */
662 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
665 sector_num = offset >> SECTOR_BITS;
667 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
669 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
677 /* read the sectors "in place" */
678 nb_sectors = count >> SECTOR_BITS;
679 if (nb_sectors > 0) {
680 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
682 sector_num += nb_sectors;
683 len = nb_sectors << SECTOR_BITS;
688 /* add data from the last sector */
690 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
692 memcpy(buf, tmp_buf, count);
697 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
698 const void *buf, int count1)
700 uint8_t tmp_buf[SECTOR_SIZE];
701 int len, nb_sectors, count;
705 /* first write to align to sector start */
706 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
709 sector_num = offset >> SECTOR_BITS;
711 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
713 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
714 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
723 /* write the sectors "in place" */
724 nb_sectors = count >> SECTOR_BITS;
725 if (nb_sectors > 0) {
726 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
728 sector_num += nb_sectors;
729 len = nb_sectors << SECTOR_BITS;
734 /* add data from the last sector */
736 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
738 memcpy(tmp_buf, buf, count);
739 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
746 * Truncate file to 'offset' bytes (needed only for file protocols)
748 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
750 BlockDriver *drv = bs->drv;
753 if (!drv->bdrv_truncate)
755 return drv->bdrv_truncate(bs, offset);
759 * Length of a file in bytes. Return < 0 if error or unknown.
761 int64_t bdrv_getlength(BlockDriverState *bs)
763 BlockDriver *drv = bs->drv;
766 if (!drv->bdrv_getlength) {
768 return bs->total_sectors * SECTOR_SIZE;
770 return drv->bdrv_getlength(bs);
773 /* return 0 as number of sectors if no device present or error */
774 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
777 length = bdrv_getlength(bs);
781 length = length >> SECTOR_BITS;
782 *nb_sectors_ptr = length;
786 uint8_t boot_ind; /* 0x80 - active */
787 uint8_t head; /* starting head */
788 uint8_t sector; /* starting sector */
789 uint8_t cyl; /* starting cylinder */
790 uint8_t sys_ind; /* What partition type */
791 uint8_t end_head; /* end head */
792 uint8_t end_sector; /* end sector */
793 uint8_t end_cyl; /* end cylinder */
794 uint32_t start_sect; /* starting sector counting from 0 */
795 uint32_t nr_sects; /* nr of sectors in partition */
796 } __attribute__((packed));
798 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
799 static int guess_disk_lchs(BlockDriverState *bs,
800 int *pcylinders, int *pheads, int *psectors)
803 int ret, i, heads, sectors, cylinders;
808 bdrv_get_geometry(bs, &nb_sectors);
810 ret = bdrv_read(bs, 0, buf, 1);
813 /* test msdos magic */
814 if (buf[510] != 0x55 || buf[511] != 0xaa)
816 for(i = 0; i < 4; i++) {
817 p = ((struct partition *)(buf + 0x1be)) + i;
818 nr_sects = le32_to_cpu(p->nr_sects);
819 if (nr_sects && p->end_head) {
820 /* We make the assumption that the partition terminates on
821 a cylinder boundary */
822 heads = p->end_head + 1;
823 sectors = p->end_sector & 63;
826 cylinders = nb_sectors / (heads * sectors);
827 if (cylinders < 1 || cylinders > 16383)
831 *pcylinders = cylinders;
833 printf("guessed geometry: LCHS=%d %d %d\n",
834 cylinders, heads, sectors);
842 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
844 int translation, lba_detected = 0;
845 int cylinders, heads, secs;
848 /* if a geometry hint is available, use it */
849 bdrv_get_geometry(bs, &nb_sectors);
850 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
851 translation = bdrv_get_translation_hint(bs);
852 if (cylinders != 0) {
857 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
859 /* if heads > 16, it means that a BIOS LBA
860 translation was active, so the default
861 hardware geometry is OK */
863 goto default_geometry;
868 /* disable any translation to be in sync with
869 the logical geometry */
870 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
871 bdrv_set_translation_hint(bs,
872 BIOS_ATA_TRANSLATION_NONE);
877 /* if no geometry, use a standard physical disk geometry */
878 cylinders = nb_sectors / (16 * 63);
880 if (cylinders > 16383)
882 else if (cylinders < 2)
887 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
888 if ((*pcyls * *pheads) <= 131072) {
889 bdrv_set_translation_hint(bs,
890 BIOS_ATA_TRANSLATION_LARGE);
892 bdrv_set_translation_hint(bs,
893 BIOS_ATA_TRANSLATION_LBA);
897 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
901 void bdrv_set_geometry_hint(BlockDriverState *bs,
902 int cyls, int heads, int secs)
909 void bdrv_set_type_hint(BlockDriverState *bs, int type)
912 bs->removable = ((type == BDRV_TYPE_CDROM ||
913 type == BDRV_TYPE_FLOPPY));
916 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
918 bs->translation = translation;
921 void bdrv_get_geometry_hint(BlockDriverState *bs,
922 int *pcyls, int *pheads, int *psecs)
929 int bdrv_get_type_hint(BlockDriverState *bs)
934 int bdrv_get_translation_hint(BlockDriverState *bs)
936 return bs->translation;
939 int bdrv_is_removable(BlockDriverState *bs)
941 return bs->removable;
944 int bdrv_is_read_only(BlockDriverState *bs)
946 return bs->read_only;
949 int bdrv_is_sg(BlockDriverState *bs)
954 /* XXX: no longer used */
955 void bdrv_set_change_cb(BlockDriverState *bs,
956 void (*change_cb)(void *opaque), void *opaque)
958 bs->change_cb = change_cb;
959 bs->change_opaque = opaque;
962 int bdrv_is_encrypted(BlockDriverState *bs)
964 if (bs->backing_hd && bs->backing_hd->encrypted)
966 return bs->encrypted;
969 int bdrv_key_required(BlockDriverState *bs)
971 BlockDriverState *backing_hd = bs->backing_hd;
973 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
975 return (bs->encrypted && !bs->valid_key);
978 int bdrv_set_key(BlockDriverState *bs, const char *key)
981 if (bs->backing_hd && bs->backing_hd->encrypted) {
982 ret = bdrv_set_key(bs->backing_hd, key);
988 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
990 ret = bs->drv->bdrv_set_key(bs, key);
993 } else if (!bs->valid_key) {
995 /* call the change callback now, we skipped it on open */
996 bs->media_changed = 1;
998 bs->change_cb(bs->change_opaque);
1003 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1008 pstrcpy(buf, buf_size, bs->drv->format_name);
1012 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1017 for (drv = first_drv; drv != NULL; drv = drv->next) {
1018 it(opaque, drv->format_name);
1022 BlockDriverState *bdrv_find(const char *name)
1024 BlockDriverState *bs;
1026 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1027 if (!strcmp(name, bs->device_name))
1033 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1035 BlockDriverState *bs;
1037 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1042 const char *bdrv_get_device_name(BlockDriverState *bs)
1044 return bs->device_name;
1047 void bdrv_flush(BlockDriverState *bs)
1051 if (bs->drv->bdrv_flush)
1052 bs->drv->bdrv_flush(bs);
1054 bdrv_flush(bs->backing_hd);
1057 void bdrv_flush_all(void)
1059 BlockDriverState *bs;
1061 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1062 if (bs->drv && !bdrv_is_read_only(bs) &&
1063 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1068 * Returns true iff the specified sector is present in the disk image. Drivers
1069 * not implementing the functionality are assumed to not support backing files,
1070 * hence all their sectors are reported as allocated.
1072 * 'pnum' is set to the number of sectors (including and immediately following
1073 * the specified sector) that are known to be in the same
1074 * allocated/unallocated state.
1076 * 'nb_sectors' is the max value 'pnum' should be set to.
1078 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1082 if (!bs->drv->bdrv_is_allocated) {
1083 if (sector_num >= bs->total_sectors) {
1087 n = bs->total_sectors - sector_num;
1088 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1091 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1094 void bdrv_info(Monitor *mon)
1096 BlockDriverState *bs;
1098 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1099 monitor_printf(mon, "%s:", bs->device_name);
1100 monitor_printf(mon, " type=");
1103 monitor_printf(mon, "hd");
1105 case BDRV_TYPE_CDROM:
1106 monitor_printf(mon, "cdrom");
1108 case BDRV_TYPE_FLOPPY:
1109 monitor_printf(mon, "floppy");
1112 monitor_printf(mon, " removable=%d", bs->removable);
1113 if (bs->removable) {
1114 monitor_printf(mon, " locked=%d", bs->locked);
1117 monitor_printf(mon, " file=");
1118 monitor_print_filename(mon, bs->filename);
1119 if (bs->backing_file[0] != '\0') {
1120 monitor_printf(mon, " backing_file=");
1121 monitor_print_filename(mon, bs->backing_file);
1123 monitor_printf(mon, " ro=%d", bs->read_only);
1124 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1125 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1127 monitor_printf(mon, " [not inserted]");
1129 monitor_printf(mon, "\n");
1133 /* The "info blockstats" command. */
1134 void bdrv_info_stats(Monitor *mon)
1136 BlockDriverState *bs;
1138 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1139 monitor_printf(mon, "%s:"
1140 " rd_bytes=%" PRIu64
1141 " wr_bytes=%" PRIu64
1142 " rd_operations=%" PRIu64
1143 " wr_operations=%" PRIu64
1146 bs->rd_bytes, bs->wr_bytes,
1147 bs->rd_ops, bs->wr_ops);
1151 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1153 if (bs->backing_hd && bs->backing_hd->encrypted)
1154 return bs->backing_file;
1155 else if (bs->encrypted)
1156 return bs->filename;
1161 void bdrv_get_backing_filename(BlockDriverState *bs,
1162 char *filename, int filename_size)
1164 if (!bs->backing_hd) {
1165 pstrcpy(filename, filename_size, "");
1167 pstrcpy(filename, filename_size, bs->backing_file);
1171 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1172 const uint8_t *buf, int nb_sectors)
1174 BlockDriver *drv = bs->drv;
1177 if (!drv->bdrv_write_compressed)
1179 if (bdrv_check_request(bs, sector_num, nb_sectors))
1181 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1184 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1186 BlockDriver *drv = bs->drv;
1189 if (!drv->bdrv_get_info)
1191 memset(bdi, 0, sizeof(*bdi));
1192 return drv->bdrv_get_info(bs, bdi);
1195 int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1197 BlockDriver *drv = bs->drv;
1200 if (!drv->bdrv_put_buffer)
1202 return drv->bdrv_put_buffer(bs, buf, pos, size);
1205 int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1207 BlockDriver *drv = bs->drv;
1210 if (!drv->bdrv_get_buffer)
1212 return drv->bdrv_get_buffer(bs, buf, pos, size);
1215 /**************************************************************/
1216 /* handling of snapshots */
1218 int bdrv_snapshot_create(BlockDriverState *bs,
1219 QEMUSnapshotInfo *sn_info)
1221 BlockDriver *drv = bs->drv;
1224 if (!drv->bdrv_snapshot_create)
1226 return drv->bdrv_snapshot_create(bs, sn_info);
1229 int bdrv_snapshot_goto(BlockDriverState *bs,
1230 const char *snapshot_id)
1232 BlockDriver *drv = bs->drv;
1235 if (!drv->bdrv_snapshot_goto)
1237 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1240 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1242 BlockDriver *drv = bs->drv;
1245 if (!drv->bdrv_snapshot_delete)
1247 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1250 int bdrv_snapshot_list(BlockDriverState *bs,
1251 QEMUSnapshotInfo **psn_info)
1253 BlockDriver *drv = bs->drv;
1256 if (!drv->bdrv_snapshot_list)
1258 return drv->bdrv_snapshot_list(bs, psn_info);
1261 #define NB_SUFFIXES 4
1263 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1265 static const char suffixes[NB_SUFFIXES] = "KMGT";
1270 snprintf(buf, buf_size, "%" PRId64, size);
1273 for(i = 0; i < NB_SUFFIXES; i++) {
1274 if (size < (10 * base)) {
1275 snprintf(buf, buf_size, "%0.1f%c",
1276 (double)size / base,
1279 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1280 snprintf(buf, buf_size, "%" PRId64 "%c",
1281 ((size + (base >> 1)) / base),
1291 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1293 char buf1[128], date_buf[128], clock_buf[128];
1303 snprintf(buf, buf_size,
1304 "%-10s%-20s%7s%20s%15s",
1305 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1309 ptm = localtime(&ti);
1310 strftime(date_buf, sizeof(date_buf),
1311 "%Y-%m-%d %H:%M:%S", ptm);
1313 localtime_r(&ti, &tm);
1314 strftime(date_buf, sizeof(date_buf),
1315 "%Y-%m-%d %H:%M:%S", &tm);
1317 secs = sn->vm_clock_nsec / 1000000000;
1318 snprintf(clock_buf, sizeof(clock_buf),
1319 "%02d:%02d:%02d.%03d",
1321 (int)((secs / 60) % 60),
1323 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1324 snprintf(buf, buf_size,
1325 "%-10s%-20s%7s%20s%15s",
1326 sn->id_str, sn->name,
1327 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1335 /**************************************************************/
1338 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1339 QEMUIOVector *qiov, int nb_sectors,
1340 BlockDriverCompletionFunc *cb, void *opaque)
1342 BlockDriver *drv = bs->drv;
1343 BlockDriverAIOCB *ret;
1347 if (bdrv_check_request(bs, sector_num, nb_sectors))
1350 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1354 /* Update stats even though technically transfer has not happened. */
1355 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1362 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1363 QEMUIOVector *qiov, int nb_sectors,
1364 BlockDriverCompletionFunc *cb, void *opaque)
1366 BlockDriver *drv = bs->drv;
1367 BlockDriverAIOCB *ret;
1373 if (bdrv_check_request(bs, sector_num, nb_sectors))
1376 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1380 /* Update stats even though technically transfer has not happened. */
1381 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1388 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1390 acb->pool->cancel(acb);
1394 /**************************************************************/
1395 /* async block device emulation */
1397 static void bdrv_aio_bh_cb(void *opaque)
1399 BlockDriverAIOCBSync *acb = opaque;
1402 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
1403 qemu_vfree(acb->bounce);
1404 acb->common.cb(acb->common.opaque, acb->ret);
1406 qemu_aio_release(acb);
1409 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1413 BlockDriverCompletionFunc *cb,
1418 BlockDriverAIOCBSync *acb;
1420 acb = qemu_aio_get(bs, cb, opaque);
1421 acb->is_write = is_write;
1423 acb->bounce = qemu_blockalign(bs, qiov->size);
1426 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1429 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1430 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1432 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1435 qemu_bh_schedule(acb->bh);
1437 return &acb->common;
1440 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1441 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1442 BlockDriverCompletionFunc *cb, void *opaque)
1444 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
1447 static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1448 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1449 BlockDriverCompletionFunc *cb, void *opaque)
1451 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1455 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1457 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1458 qemu_bh_cancel(acb->bh);
1459 qemu_aio_release(acb);
1462 /**************************************************************/
1463 /* sync block device emulation */
1465 static void bdrv_rw_em_cb(void *opaque, int ret)
1467 *(int *)opaque = ret;
1470 #define NOT_DONE 0x7fffffff
1472 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1473 uint8_t *buf, int nb_sectors)
1476 BlockDriverAIOCB *acb;
1480 async_ret = NOT_DONE;
1481 iov.iov_base = (void *)buf;
1482 iov.iov_len = nb_sectors * 512;
1483 qemu_iovec_init_external(&qiov, &iov, 1);
1484 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
1485 bdrv_rw_em_cb, &async_ret);
1489 while (async_ret == NOT_DONE) {
1496 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1497 const uint8_t *buf, int nb_sectors)
1500 BlockDriverAIOCB *acb;
1504 async_ret = NOT_DONE;
1505 iov.iov_base = (void *)buf;
1506 iov.iov_len = nb_sectors * 512;
1507 qemu_iovec_init_external(&qiov, &iov, 1);
1508 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
1509 bdrv_rw_em_cb, &async_ret);
1512 while (async_ret == NOT_DONE) {
1518 void bdrv_init(void)
1520 module_call_init(MODULE_INIT_BLOCK);
1523 void aio_pool_init(AIOPool *pool, int aiocb_size,
1524 void (*cancel)(BlockDriverAIOCB *acb))
1526 pool->aiocb_size = aiocb_size;
1527 pool->cancel = cancel;
1528 pool->free_aiocb = NULL;
1531 void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1532 BlockDriverCompletionFunc *cb, void *opaque)
1534 BlockDriverAIOCB *acb;
1536 if (pool->free_aiocb) {
1537 acb = pool->free_aiocb;
1538 pool->free_aiocb = acb->next;
1540 acb = qemu_mallocz(pool->aiocb_size);
1545 acb->opaque = opaque;
1549 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1552 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1555 void qemu_aio_release(void *p)
1557 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1558 AIOPool *pool = acb->pool;
1559 acb->next = pool->free_aiocb;
1560 pool->free_aiocb = acb;
1563 /**************************************************************/
1564 /* removable device support */
1567 * Return TRUE if the media is present
1569 int bdrv_is_inserted(BlockDriverState *bs)
1571 BlockDriver *drv = bs->drv;
1575 if (!drv->bdrv_is_inserted)
1577 ret = drv->bdrv_is_inserted(bs);
1582 * Return TRUE if the media changed since the last call to this
1583 * function. It is currently only used for floppy disks
1585 int bdrv_media_changed(BlockDriverState *bs)
1587 BlockDriver *drv = bs->drv;
1590 if (!drv || !drv->bdrv_media_changed)
1593 ret = drv->bdrv_media_changed(bs);
1594 if (ret == -ENOTSUP)
1595 ret = bs->media_changed;
1596 bs->media_changed = 0;
1601 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1603 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1605 BlockDriver *drv = bs->drv;
1608 if (!drv || !drv->bdrv_eject) {
1611 ret = drv->bdrv_eject(bs, eject_flag);
1613 if (ret == -ENOTSUP) {
1619 int bdrv_is_locked(BlockDriverState *bs)
1625 * Lock or unlock the media (if it is locked, the user won't be able
1626 * to eject it manually).
1628 void bdrv_set_locked(BlockDriverState *bs, int locked)
1630 BlockDriver *drv = bs->drv;
1632 bs->locked = locked;
1633 if (drv && drv->bdrv_set_locked) {
1634 drv->bdrv_set_locked(bs, locked);
1638 /* needed for generic scsi interface */
1640 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1642 BlockDriver *drv = bs->drv;
1644 if (drv && drv->bdrv_ioctl)
1645 return drv->bdrv_ioctl(bs, req, buf);
1649 BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1650 unsigned long int req, void *buf,
1651 BlockDriverCompletionFunc *cb, void *opaque)
1653 BlockDriver *drv = bs->drv;
1655 if (drv && drv->bdrv_aio_ioctl)
1656 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1660 void *qemu_blockalign(BlockDriverState *bs, size_t size)
1662 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);