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"
25 #include "qemu-common.h"
28 #include "block_int.h"
30 #include "qemu-objects.h"
31 #include "qemu-coroutine.h"
34 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/queue.h>
47 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
48 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
49 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
50 BlockDriverCompletionFunc *cb, void *opaque);
51 static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
52 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
53 BlockDriverCompletionFunc *cb, void *opaque);
54 static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
55 BlockDriverCompletionFunc *cb, void *opaque);
56 static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
57 BlockDriverCompletionFunc *cb, void *opaque);
58 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
59 uint8_t *buf, int nb_sectors);
60 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
61 const uint8_t *buf, int nb_sectors);
62 static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs,
63 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
64 BlockDriverCompletionFunc *cb, void *opaque);
65 static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs,
66 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
67 BlockDriverCompletionFunc *cb, void *opaque);
68 static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
69 int64_t sector_num, int nb_sectors,
71 static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
72 int64_t sector_num, int nb_sectors,
74 static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs);
76 static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
77 QTAILQ_HEAD_INITIALIZER(bdrv_states);
79 static QLIST_HEAD(, BlockDriver) bdrv_drivers =
80 QLIST_HEAD_INITIALIZER(bdrv_drivers);
82 /* The device to use for VM snapshots */
83 static BlockDriverState *bs_snapshots;
85 /* If non-zero, use only whitelisted block drivers */
86 static int use_bdrv_whitelist;
89 static int is_windows_drive_prefix(const char *filename)
91 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
92 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
96 int is_windows_drive(const char *filename)
98 if (is_windows_drive_prefix(filename) &&
101 if (strstart(filename, "\\\\.\\", NULL) ||
102 strstart(filename, "//./", NULL))
108 /* check if the path starts with "<protocol>:" */
109 static int path_has_protocol(const char *path)
112 if (is_windows_drive(path) ||
113 is_windows_drive_prefix(path)) {
118 return strchr(path, ':') != NULL;
121 int path_is_absolute(const char *path)
125 /* specific case for names like: "\\.\d:" */
126 if (*path == '/' || *path == '\\')
129 p = strchr(path, ':');
135 return (*p == '/' || *p == '\\');
141 /* if filename is absolute, just copy it to dest. Otherwise, build a
142 path to it by considering it is relative to base_path. URL are
144 void path_combine(char *dest, int dest_size,
145 const char *base_path,
146 const char *filename)
153 if (path_is_absolute(filename)) {
154 pstrcpy(dest, dest_size, filename);
156 p = strchr(base_path, ':');
161 p1 = strrchr(base_path, '/');
165 p2 = strrchr(base_path, '\\');
177 if (len > dest_size - 1)
179 memcpy(dest, base_path, len);
181 pstrcat(dest, dest_size, filename);
185 void bdrv_register(BlockDriver *bdrv)
187 if (bdrv->bdrv_co_readv) {
188 /* Emulate AIO by coroutines, and sync by AIO */
189 bdrv->bdrv_aio_readv = bdrv_co_aio_readv_em;
190 bdrv->bdrv_aio_writev = bdrv_co_aio_writev_em;
191 bdrv->bdrv_read = bdrv_read_em;
192 bdrv->bdrv_write = bdrv_write_em;
194 bdrv->bdrv_co_readv = bdrv_co_readv_em;
195 bdrv->bdrv_co_writev = bdrv_co_writev_em;
197 if (!bdrv->bdrv_aio_readv) {
198 /* add AIO emulation layer */
199 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
200 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
201 } else if (!bdrv->bdrv_read) {
202 /* add synchronous IO emulation layer */
203 bdrv->bdrv_read = bdrv_read_em;
204 bdrv->bdrv_write = bdrv_write_em;
208 if (!bdrv->bdrv_aio_flush)
209 bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
211 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
214 /* create a new block device (by default it is empty) */
215 BlockDriverState *bdrv_new(const char *device_name)
217 BlockDriverState *bs;
219 bs = g_malloc0(sizeof(BlockDriverState));
220 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
221 if (device_name[0] != '\0') {
222 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
227 BlockDriver *bdrv_find_format(const char *format_name)
230 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
231 if (!strcmp(drv1->format_name, format_name)) {
238 static int bdrv_is_whitelisted(BlockDriver *drv)
240 static const char *whitelist[] = {
241 CONFIG_BDRV_WHITELIST
246 return 1; /* no whitelist, anything goes */
248 for (p = whitelist; *p; p++) {
249 if (!strcmp(drv->format_name, *p)) {
256 BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
258 BlockDriver *drv = bdrv_find_format(format_name);
259 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
262 int bdrv_create(BlockDriver *drv, const char* filename,
263 QEMUOptionParameter *options)
265 if (!drv->bdrv_create)
268 return drv->bdrv_create(filename, options);
271 int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
275 drv = bdrv_find_protocol(filename);
280 return bdrv_create(drv, filename, options);
284 void get_tmp_filename(char *filename, int size)
286 char temp_dir[MAX_PATH];
288 GetTempPath(MAX_PATH, temp_dir);
289 GetTempFileName(temp_dir, "qem", 0, filename);
292 void get_tmp_filename(char *filename, int size)
296 /* XXX: race condition possible */
297 tmpdir = getenv("TMPDIR");
300 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
301 fd = mkstemp(filename);
307 * Detect host devices. By convention, /dev/cdrom[N] is always
308 * recognized as a host CDROM.
310 static BlockDriver *find_hdev_driver(const char *filename)
312 int score_max = 0, score;
313 BlockDriver *drv = NULL, *d;
315 QLIST_FOREACH(d, &bdrv_drivers, list) {
316 if (d->bdrv_probe_device) {
317 score = d->bdrv_probe_device(filename);
318 if (score > score_max) {
328 BlockDriver *bdrv_find_protocol(const char *filename)
335 /* TODO Drivers without bdrv_file_open must be specified explicitly */
338 * XXX(hch): we really should not let host device detection
339 * override an explicit protocol specification, but moving this
340 * later breaks access to device names with colons in them.
341 * Thanks to the brain-dead persistent naming schemes on udev-
342 * based Linux systems those actually are quite common.
344 drv1 = find_hdev_driver(filename);
349 if (!path_has_protocol(filename)) {
350 return bdrv_find_format("file");
352 p = strchr(filename, ':');
355 if (len > sizeof(protocol) - 1)
356 len = sizeof(protocol) - 1;
357 memcpy(protocol, filename, len);
358 protocol[len] = '\0';
359 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
360 if (drv1->protocol_name &&
361 !strcmp(drv1->protocol_name, protocol)) {
368 static int find_image_format(const char *filename, BlockDriver **pdrv)
370 int ret, score, score_max;
371 BlockDriver *drv1, *drv;
373 BlockDriverState *bs;
375 ret = bdrv_file_open(&bs, filename, 0);
381 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
382 if (bs->sg || !bdrv_is_inserted(bs)) {
384 drv = bdrv_find_format("raw");
392 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
401 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
402 if (drv1->bdrv_probe) {
403 score = drv1->bdrv_probe(buf, ret, filename);
404 if (score > score_max) {
418 * Set the current 'total_sectors' value
420 static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
422 BlockDriver *drv = bs->drv;
424 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
428 /* query actual device if possible, otherwise just trust the hint */
429 if (drv->bdrv_getlength) {
430 int64_t length = drv->bdrv_getlength(bs);
434 hint = length >> BDRV_SECTOR_BITS;
437 bs->total_sectors = hint;
442 * Set open flags for a given cache mode
444 * Return 0 on success, -1 if the cache mode was invalid.
446 int bdrv_parse_cache_flags(const char *mode, int *flags)
448 *flags &= ~BDRV_O_CACHE_MASK;
450 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
451 *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
452 } else if (!strcmp(mode, "directsync")) {
453 *flags |= BDRV_O_NOCACHE;
454 } else if (!strcmp(mode, "writeback")) {
455 *flags |= BDRV_O_CACHE_WB;
456 } else if (!strcmp(mode, "unsafe")) {
457 *flags |= BDRV_O_CACHE_WB;
458 *flags |= BDRV_O_NO_FLUSH;
459 } else if (!strcmp(mode, "writethrough")) {
460 /* this is the default */
469 * Common part for opening disk images and files
471 static int bdrv_open_common(BlockDriverState *bs, const char *filename,
472 int flags, BlockDriver *drv)
479 bs->total_sectors = 0;
482 bs->open_flags = flags;
483 bs->buffer_alignment = 512;
485 pstrcpy(bs->filename, sizeof(bs->filename), filename);
487 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
492 bs->opaque = g_malloc0(drv->instance_size);
494 if (flags & BDRV_O_CACHE_WB)
495 bs->enable_write_cache = 1;
498 * Clear flags that are internal to the block layer before opening the
501 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
504 * Snapshots should be writable.
506 if (bs->is_temporary) {
507 open_flags |= BDRV_O_RDWR;
510 /* Open the image, either directly or using a protocol */
511 if (drv->bdrv_file_open) {
512 ret = drv->bdrv_file_open(bs, filename, open_flags);
514 ret = bdrv_file_open(&bs->file, filename, open_flags);
516 ret = drv->bdrv_open(bs, open_flags);
524 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
526 ret = refresh_total_sectors(bs, bs->total_sectors);
532 if (bs->is_temporary) {
540 bdrv_delete(bs->file);
550 * Opens a file using a protocol (file, host_device, nbd, ...)
552 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
554 BlockDriverState *bs;
558 drv = bdrv_find_protocol(filename);
564 ret = bdrv_open_common(bs, filename, flags, drv);
575 * Opens a disk image (raw, qcow2, vmdk, ...)
577 int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
582 if (flags & BDRV_O_SNAPSHOT) {
583 BlockDriverState *bs1;
586 BlockDriver *bdrv_qcow2;
587 QEMUOptionParameter *options;
588 char tmp_filename[PATH_MAX];
589 char backing_filename[PATH_MAX];
591 /* if snapshot, we create a temporary backing file and open it
592 instead of opening 'filename' directly */
594 /* if there is a backing file, use it */
596 ret = bdrv_open(bs1, filename, 0, drv);
601 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
603 if (bs1->drv && bs1->drv->protocol_name)
608 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
610 /* Real path is meaningless for protocols */
612 snprintf(backing_filename, sizeof(backing_filename),
614 else if (!realpath(filename, backing_filename))
617 bdrv_qcow2 = bdrv_find_format("qcow2");
618 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
620 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
621 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
623 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
627 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
628 free_option_parameters(options);
633 filename = tmp_filename;
635 bs->is_temporary = 1;
638 /* Find the right image format driver */
640 ret = find_image_format(filename, &drv);
644 goto unlink_and_fail;
648 ret = bdrv_open_common(bs, filename, flags, drv);
650 goto unlink_and_fail;
653 /* If there is a backing file, use it */
654 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
655 char backing_filename[PATH_MAX];
657 BlockDriver *back_drv = NULL;
659 bs->backing_hd = bdrv_new("");
661 if (path_has_protocol(bs->backing_file)) {
662 pstrcpy(backing_filename, sizeof(backing_filename),
665 path_combine(backing_filename, sizeof(backing_filename),
666 filename, bs->backing_file);
669 if (bs->backing_format[0] != '\0') {
670 back_drv = bdrv_find_format(bs->backing_format);
673 /* backing files always opened read-only */
675 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
677 ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
682 if (bs->is_temporary) {
683 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
685 /* base image inherits from "parent" */
686 bs->backing_hd->keep_read_only = bs->keep_read_only;
690 if (!bdrv_key_required(bs)) {
691 bdrv_dev_change_media_cb(bs, true);
697 if (bs->is_temporary) {
703 void bdrv_close(BlockDriverState *bs)
706 if (bs == bs_snapshots) {
709 if (bs->backing_hd) {
710 bdrv_delete(bs->backing_hd);
711 bs->backing_hd = NULL;
713 bs->drv->bdrv_close(bs);
716 if (bs->is_temporary) {
717 unlink(bs->filename);
723 if (bs->file != NULL) {
724 bdrv_close(bs->file);
727 bdrv_dev_change_media_cb(bs, false);
731 void bdrv_close_all(void)
733 BlockDriverState *bs;
735 QTAILQ_FOREACH(bs, &bdrv_states, list) {
740 /* make a BlockDriverState anonymous by removing from bdrv_state list.
741 Also, NULL terminate the device_name to prevent double remove */
742 void bdrv_make_anon(BlockDriverState *bs)
744 if (bs->device_name[0] != '\0') {
745 QTAILQ_REMOVE(&bdrv_states, bs, list);
747 bs->device_name[0] = '\0';
750 void bdrv_delete(BlockDriverState *bs)
754 /* remove from list, if necessary */
758 if (bs->file != NULL) {
759 bdrv_delete(bs->file);
762 assert(bs != bs_snapshots);
766 int bdrv_attach_dev(BlockDriverState *bs, void *dev)
767 /* TODO change to DeviceState *dev when all users are qdevified */
776 /* TODO qdevified devices don't use this, remove when devices are qdevified */
777 void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev)
779 if (bdrv_attach_dev(bs, dev) < 0) {
784 void bdrv_detach_dev(BlockDriverState *bs, void *dev)
785 /* TODO change to DeviceState *dev when all users are qdevified */
787 assert(bs->dev == dev);
790 bs->dev_opaque = NULL;
791 bs->buffer_alignment = 512;
794 /* TODO change to return DeviceState * when all users are qdevified */
795 void *bdrv_get_attached_dev(BlockDriverState *bs)
800 void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
804 bs->dev_opaque = opaque;
805 if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
810 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load)
812 if (bs->dev_ops && bs->dev_ops->change_media_cb) {
813 bs->dev_ops->change_media_cb(bs->dev_opaque, load);
817 bool bdrv_dev_has_removable_media(BlockDriverState *bs)
819 return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
822 bool bdrv_dev_is_tray_open(BlockDriverState *bs)
824 if (bs->dev_ops && bs->dev_ops->is_tray_open) {
825 return bs->dev_ops->is_tray_open(bs->dev_opaque);
830 static void bdrv_dev_resize_cb(BlockDriverState *bs)
832 if (bs->dev_ops && bs->dev_ops->resize_cb) {
833 bs->dev_ops->resize_cb(bs->dev_opaque);
837 bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
839 if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
840 return bs->dev_ops->is_medium_locked(bs->dev_opaque);
846 * Run consistency checks on an image
848 * Returns 0 if the check could be completed (it doesn't mean that the image is
849 * free of errors) or -errno when an internal error occurred. The results of the
850 * check are stored in res.
852 int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
854 if (bs->drv->bdrv_check == NULL) {
858 memset(res, 0, sizeof(*res));
859 return bs->drv->bdrv_check(bs, res);
862 #define COMMIT_BUF_SECTORS 2048
864 /* commit COW file into the raw image */
865 int bdrv_commit(BlockDriverState *bs)
867 BlockDriver *drv = bs->drv;
868 BlockDriver *backing_drv;
869 int64_t sector, total_sectors;
870 int n, ro, open_flags;
871 int ret = 0, rw_ret = 0;
874 BlockDriverState *bs_rw, *bs_ro;
879 if (!bs->backing_hd) {
883 if (bs->backing_hd->keep_read_only) {
887 backing_drv = bs->backing_hd->drv;
888 ro = bs->backing_hd->read_only;
889 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
890 open_flags = bs->backing_hd->open_flags;
894 bdrv_delete(bs->backing_hd);
895 bs->backing_hd = NULL;
896 bs_rw = bdrv_new("");
897 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR,
901 /* try to re-open read-only */
902 bs_ro = bdrv_new("");
903 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
907 /* drive not functional anymore */
911 bs->backing_hd = bs_ro;
914 bs->backing_hd = bs_rw;
917 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
918 buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
920 for (sector = 0; sector < total_sectors; sector += n) {
921 if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
923 if (bdrv_read(bs, sector, buf, n) != 0) {
928 if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
935 if (drv->bdrv_make_empty) {
936 ret = drv->bdrv_make_empty(bs);
941 * Make sure all data we wrote to the backing device is actually
945 bdrv_flush(bs->backing_hd);
952 bdrv_delete(bs->backing_hd);
953 bs->backing_hd = NULL;
954 bs_ro = bdrv_new("");
955 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
959 /* drive not functional anymore */
963 bs->backing_hd = bs_ro;
964 bs->backing_hd->keep_read_only = 0;
970 void bdrv_commit_all(void)
972 BlockDriverState *bs;
974 QTAILQ_FOREACH(bs, &bdrv_states, list) {
982 * -EINVAL - backing format specified, but no file
983 * -ENOSPC - can't update the backing file because no space is left in the
985 * -ENOTSUP - format driver doesn't support changing the backing file
987 int bdrv_change_backing_file(BlockDriverState *bs,
988 const char *backing_file, const char *backing_fmt)
990 BlockDriver *drv = bs->drv;
992 if (drv->bdrv_change_backing_file != NULL) {
993 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
999 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
1004 if (!bdrv_is_inserted(bs))
1010 len = bdrv_getlength(bs);
1015 if ((offset > len) || (len - offset < size))
1021 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
1024 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
1025 nb_sectors * BDRV_SECTOR_SIZE);
1028 static inline bool bdrv_has_async_rw(BlockDriver *drv)
1030 return drv->bdrv_co_readv != bdrv_co_readv_em
1031 || drv->bdrv_aio_readv != bdrv_aio_readv_em;
1034 static inline bool bdrv_has_async_flush(BlockDriver *drv)
1036 return drv->bdrv_aio_flush != bdrv_aio_flush_em;
1039 /* return < 0 if error. See bdrv_write() for the return codes */
1040 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
1041 uint8_t *buf, int nb_sectors)
1043 BlockDriver *drv = bs->drv;
1048 if (bdrv_has_async_rw(drv) && qemu_in_coroutine()) {
1050 struct iovec iov = {
1051 .iov_base = (void *)buf,
1052 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1055 qemu_iovec_init_external(&qiov, &iov, 1);
1056 return bdrv_co_readv(bs, sector_num, nb_sectors, &qiov);
1059 if (bdrv_check_request(bs, sector_num, nb_sectors))
1062 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
1065 static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
1066 int nb_sectors, int dirty)
1069 unsigned long val, idx, bit;
1071 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
1072 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
1074 for (; start <= end; start++) {
1075 idx = start / (sizeof(unsigned long) * 8);
1076 bit = start % (sizeof(unsigned long) * 8);
1077 val = bs->dirty_bitmap[idx];
1079 if (!(val & (1UL << bit))) {
1084 if (val & (1UL << bit)) {
1086 val &= ~(1UL << bit);
1089 bs->dirty_bitmap[idx] = val;
1093 /* Return < 0 if error. Important errors are:
1094 -EIO generic I/O error (may happen for all errors)
1095 -ENOMEDIUM No media inserted.
1096 -EINVAL Invalid sector number or nb_sectors
1097 -EACCES Trying to write a read-only device
1099 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
1100 const uint8_t *buf, int nb_sectors)
1102 BlockDriver *drv = bs->drv;
1107 if (bdrv_has_async_rw(drv) && qemu_in_coroutine()) {
1109 struct iovec iov = {
1110 .iov_base = (void *)buf,
1111 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1114 qemu_iovec_init_external(&qiov, &iov, 1);
1115 return bdrv_co_writev(bs, sector_num, nb_sectors, &qiov);
1120 if (bdrv_check_request(bs, sector_num, nb_sectors))
1123 if (bs->dirty_bitmap) {
1124 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1127 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1128 bs->wr_highest_sector = sector_num + nb_sectors - 1;
1131 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
1134 int bdrv_pread(BlockDriverState *bs, int64_t offset,
1135 void *buf, int count1)
1137 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
1138 int len, nb_sectors, count;
1143 /* first read to align to sector start */
1144 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
1147 sector_num = offset >> BDRV_SECTOR_BITS;
1149 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1151 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
1159 /* read the sectors "in place" */
1160 nb_sectors = count >> BDRV_SECTOR_BITS;
1161 if (nb_sectors > 0) {
1162 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
1164 sector_num += nb_sectors;
1165 len = nb_sectors << BDRV_SECTOR_BITS;
1170 /* add data from the last sector */
1172 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1174 memcpy(buf, tmp_buf, count);
1179 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1180 const void *buf, int count1)
1182 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
1183 int len, nb_sectors, count;
1188 /* first write to align to sector start */
1189 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
1192 sector_num = offset >> BDRV_SECTOR_BITS;
1194 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1196 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
1197 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1206 /* write the sectors "in place" */
1207 nb_sectors = count >> BDRV_SECTOR_BITS;
1208 if (nb_sectors > 0) {
1209 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
1211 sector_num += nb_sectors;
1212 len = nb_sectors << BDRV_SECTOR_BITS;
1217 /* add data from the last sector */
1219 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1221 memcpy(tmp_buf, buf, count);
1222 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1229 * Writes to the file and ensures that no writes are reordered across this
1230 * request (acts as a barrier)
1232 * Returns 0 on success, -errno in error cases.
1234 int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1235 const void *buf, int count)
1239 ret = bdrv_pwrite(bs, offset, buf, count);
1244 /* No flush needed for cache modes that use O_DSYNC */
1245 if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) {
1252 int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
1253 int nb_sectors, QEMUIOVector *qiov)
1255 BlockDriver *drv = bs->drv;
1257 trace_bdrv_co_readv(bs, sector_num, nb_sectors);
1262 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1266 return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
1269 int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
1270 int nb_sectors, QEMUIOVector *qiov)
1272 BlockDriver *drv = bs->drv;
1274 trace_bdrv_co_writev(bs, sector_num, nb_sectors);
1279 if (bs->read_only) {
1282 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1286 if (bs->dirty_bitmap) {
1287 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1290 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1291 bs->wr_highest_sector = sector_num + nb_sectors - 1;
1294 return drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
1298 * Truncate file to 'offset' bytes (needed only for file protocols)
1300 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1302 BlockDriver *drv = bs->drv;
1306 if (!drv->bdrv_truncate)
1310 if (bdrv_in_use(bs))
1312 ret = drv->bdrv_truncate(bs, offset);
1314 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
1315 bdrv_dev_resize_cb(bs);
1321 * Length of a allocated file in bytes. Sparse files are counted by actual
1322 * allocated space. Return < 0 if error or unknown.
1324 int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
1326 BlockDriver *drv = bs->drv;
1330 if (drv->bdrv_get_allocated_file_size) {
1331 return drv->bdrv_get_allocated_file_size(bs);
1334 return bdrv_get_allocated_file_size(bs->file);
1340 * Length of a file in bytes. Return < 0 if error or unknown.
1342 int64_t bdrv_getlength(BlockDriverState *bs)
1344 BlockDriver *drv = bs->drv;
1348 if (bs->growable || bdrv_dev_has_removable_media(bs)) {
1349 if (drv->bdrv_getlength) {
1350 return drv->bdrv_getlength(bs);
1353 return bs->total_sectors * BDRV_SECTOR_SIZE;
1356 /* return 0 as number of sectors if no device present or error */
1357 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
1360 length = bdrv_getlength(bs);
1364 length = length >> BDRV_SECTOR_BITS;
1365 *nb_sectors_ptr = length;
1369 uint8_t boot_ind; /* 0x80 - active */
1370 uint8_t head; /* starting head */
1371 uint8_t sector; /* starting sector */
1372 uint8_t cyl; /* starting cylinder */
1373 uint8_t sys_ind; /* What partition type */
1374 uint8_t end_head; /* end head */
1375 uint8_t end_sector; /* end sector */
1376 uint8_t end_cyl; /* end cylinder */
1377 uint32_t start_sect; /* starting sector counting from 0 */
1378 uint32_t nr_sects; /* nr of sectors in partition */
1381 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1382 static int guess_disk_lchs(BlockDriverState *bs,
1383 int *pcylinders, int *pheads, int *psectors)
1385 uint8_t buf[BDRV_SECTOR_SIZE];
1386 int ret, i, heads, sectors, cylinders;
1387 struct partition *p;
1389 uint64_t nb_sectors;
1391 bdrv_get_geometry(bs, &nb_sectors);
1393 ret = bdrv_read(bs, 0, buf, 1);
1396 /* test msdos magic */
1397 if (buf[510] != 0x55 || buf[511] != 0xaa)
1399 for(i = 0; i < 4; i++) {
1400 p = ((struct partition *)(buf + 0x1be)) + i;
1401 nr_sects = le32_to_cpu(p->nr_sects);
1402 if (nr_sects && p->end_head) {
1403 /* We make the assumption that the partition terminates on
1404 a cylinder boundary */
1405 heads = p->end_head + 1;
1406 sectors = p->end_sector & 63;
1409 cylinders = nb_sectors / (heads * sectors);
1410 if (cylinders < 1 || cylinders > 16383)
1413 *psectors = sectors;
1414 *pcylinders = cylinders;
1416 printf("guessed geometry: LCHS=%d %d %d\n",
1417 cylinders, heads, sectors);
1425 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1427 int translation, lba_detected = 0;
1428 int cylinders, heads, secs;
1429 uint64_t nb_sectors;
1431 /* if a geometry hint is available, use it */
1432 bdrv_get_geometry(bs, &nb_sectors);
1433 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1434 translation = bdrv_get_translation_hint(bs);
1435 if (cylinders != 0) {
1440 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1442 /* if heads > 16, it means that a BIOS LBA
1443 translation was active, so the default
1444 hardware geometry is OK */
1446 goto default_geometry;
1451 /* disable any translation to be in sync with
1452 the logical geometry */
1453 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1454 bdrv_set_translation_hint(bs,
1455 BIOS_ATA_TRANSLATION_NONE);
1460 /* if no geometry, use a standard physical disk geometry */
1461 cylinders = nb_sectors / (16 * 63);
1463 if (cylinders > 16383)
1465 else if (cylinders < 2)
1470 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1471 if ((*pcyls * *pheads) <= 131072) {
1472 bdrv_set_translation_hint(bs,
1473 BIOS_ATA_TRANSLATION_LARGE);
1475 bdrv_set_translation_hint(bs,
1476 BIOS_ATA_TRANSLATION_LBA);
1480 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1484 void bdrv_set_geometry_hint(BlockDriverState *bs,
1485 int cyls, int heads, int secs)
1492 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1494 bs->translation = translation;
1497 void bdrv_get_geometry_hint(BlockDriverState *bs,
1498 int *pcyls, int *pheads, int *psecs)
1501 *pheads = bs->heads;
1505 /* Recognize floppy formats */
1506 typedef struct FDFormat {
1513 static const FDFormat fd_formats[] = {
1514 /* First entry is default format */
1515 /* 1.44 MB 3"1/2 floppy disks */
1516 { FDRIVE_DRV_144, 18, 80, 1, },
1517 { FDRIVE_DRV_144, 20, 80, 1, },
1518 { FDRIVE_DRV_144, 21, 80, 1, },
1519 { FDRIVE_DRV_144, 21, 82, 1, },
1520 { FDRIVE_DRV_144, 21, 83, 1, },
1521 { FDRIVE_DRV_144, 22, 80, 1, },
1522 { FDRIVE_DRV_144, 23, 80, 1, },
1523 { FDRIVE_DRV_144, 24, 80, 1, },
1524 /* 2.88 MB 3"1/2 floppy disks */
1525 { FDRIVE_DRV_288, 36, 80, 1, },
1526 { FDRIVE_DRV_288, 39, 80, 1, },
1527 { FDRIVE_DRV_288, 40, 80, 1, },
1528 { FDRIVE_DRV_288, 44, 80, 1, },
1529 { FDRIVE_DRV_288, 48, 80, 1, },
1530 /* 720 kB 3"1/2 floppy disks */
1531 { FDRIVE_DRV_144, 9, 80, 1, },
1532 { FDRIVE_DRV_144, 10, 80, 1, },
1533 { FDRIVE_DRV_144, 10, 82, 1, },
1534 { FDRIVE_DRV_144, 10, 83, 1, },
1535 { FDRIVE_DRV_144, 13, 80, 1, },
1536 { FDRIVE_DRV_144, 14, 80, 1, },
1537 /* 1.2 MB 5"1/4 floppy disks */
1538 { FDRIVE_DRV_120, 15, 80, 1, },
1539 { FDRIVE_DRV_120, 18, 80, 1, },
1540 { FDRIVE_DRV_120, 18, 82, 1, },
1541 { FDRIVE_DRV_120, 18, 83, 1, },
1542 { FDRIVE_DRV_120, 20, 80, 1, },
1543 /* 720 kB 5"1/4 floppy disks */
1544 { FDRIVE_DRV_120, 9, 80, 1, },
1545 { FDRIVE_DRV_120, 11, 80, 1, },
1546 /* 360 kB 5"1/4 floppy disks */
1547 { FDRIVE_DRV_120, 9, 40, 1, },
1548 { FDRIVE_DRV_120, 9, 40, 0, },
1549 { FDRIVE_DRV_120, 10, 41, 1, },
1550 { FDRIVE_DRV_120, 10, 42, 1, },
1551 /* 320 kB 5"1/4 floppy disks */
1552 { FDRIVE_DRV_120, 8, 40, 1, },
1553 { FDRIVE_DRV_120, 8, 40, 0, },
1554 /* 360 kB must match 5"1/4 better than 3"1/2... */
1555 { FDRIVE_DRV_144, 9, 80, 0, },
1557 { FDRIVE_DRV_NONE, -1, -1, 0, },
1560 void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads,
1561 int *max_track, int *last_sect,
1562 FDriveType drive_in, FDriveType *drive)
1564 const FDFormat *parse;
1565 uint64_t nb_sectors, size;
1566 int i, first_match, match;
1568 bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect);
1569 if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) {
1570 /* User defined disk */
1572 bdrv_get_geometry(bs, &nb_sectors);
1575 for (i = 0; ; i++) {
1576 parse = &fd_formats[i];
1577 if (parse->drive == FDRIVE_DRV_NONE) {
1580 if (drive_in == parse->drive ||
1581 drive_in == FDRIVE_DRV_NONE) {
1582 size = (parse->max_head + 1) * parse->max_track *
1584 if (nb_sectors == size) {
1588 if (first_match == -1) {
1594 if (first_match == -1) {
1597 match = first_match;
1599 parse = &fd_formats[match];
1601 *nb_heads = parse->max_head + 1;
1602 *max_track = parse->max_track;
1603 *last_sect = parse->last_sect;
1604 *drive = parse->drive;
1608 int bdrv_get_translation_hint(BlockDriverState *bs)
1610 return bs->translation;
1613 void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1614 BlockErrorAction on_write_error)
1616 bs->on_read_error = on_read_error;
1617 bs->on_write_error = on_write_error;
1620 BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1622 return is_read ? bs->on_read_error : bs->on_write_error;
1625 int bdrv_is_read_only(BlockDriverState *bs)
1627 return bs->read_only;
1630 int bdrv_is_sg(BlockDriverState *bs)
1635 int bdrv_enable_write_cache(BlockDriverState *bs)
1637 return bs->enable_write_cache;
1640 int bdrv_is_encrypted(BlockDriverState *bs)
1642 if (bs->backing_hd && bs->backing_hd->encrypted)
1644 return bs->encrypted;
1647 int bdrv_key_required(BlockDriverState *bs)
1649 BlockDriverState *backing_hd = bs->backing_hd;
1651 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1653 return (bs->encrypted && !bs->valid_key);
1656 int bdrv_set_key(BlockDriverState *bs, const char *key)
1659 if (bs->backing_hd && bs->backing_hd->encrypted) {
1660 ret = bdrv_set_key(bs->backing_hd, key);
1666 if (!bs->encrypted) {
1668 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1671 ret = bs->drv->bdrv_set_key(bs, key);
1674 } else if (!bs->valid_key) {
1676 /* call the change callback now, we skipped it on open */
1677 bdrv_dev_change_media_cb(bs, true);
1682 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1687 pstrcpy(buf, buf_size, bs->drv->format_name);
1691 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1696 QLIST_FOREACH(drv, &bdrv_drivers, list) {
1697 it(opaque, drv->format_name);
1701 BlockDriverState *bdrv_find(const char *name)
1703 BlockDriverState *bs;
1705 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1706 if (!strcmp(name, bs->device_name)) {
1713 BlockDriverState *bdrv_next(BlockDriverState *bs)
1716 return QTAILQ_FIRST(&bdrv_states);
1718 return QTAILQ_NEXT(bs, list);
1721 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1723 BlockDriverState *bs;
1725 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1730 const char *bdrv_get_device_name(BlockDriverState *bs)
1732 return bs->device_name;
1735 int bdrv_flush(BlockDriverState *bs)
1737 if (bs->open_flags & BDRV_O_NO_FLUSH) {
1741 if (bs->drv && bdrv_has_async_flush(bs->drv) && qemu_in_coroutine()) {
1742 return bdrv_co_flush_em(bs);
1745 if (bs->drv && bs->drv->bdrv_flush) {
1746 return bs->drv->bdrv_flush(bs);
1750 * Some block drivers always operate in either writethrough or unsafe mode
1751 * and don't support bdrv_flush therefore. Usually qemu doesn't know how
1752 * the server works (because the behaviour is hardcoded or depends on
1753 * server-side configuration), so we can't ensure that everything is safe
1754 * on disk. Returning an error doesn't work because that would break guests
1755 * even if the server operates in writethrough mode.
1757 * Let's hope the user knows what he's doing.
1762 void bdrv_flush_all(void)
1764 BlockDriverState *bs;
1766 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1767 if (!bdrv_is_read_only(bs) && bdrv_is_inserted(bs)) {
1773 int bdrv_has_zero_init(BlockDriverState *bs)
1777 if (bs->drv->bdrv_has_zero_init) {
1778 return bs->drv->bdrv_has_zero_init(bs);
1784 int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
1789 if (!bs->drv->bdrv_discard) {
1792 return bs->drv->bdrv_discard(bs, sector_num, nb_sectors);
1796 * Returns true iff the specified sector is present in the disk image. Drivers
1797 * not implementing the functionality are assumed to not support backing files,
1798 * hence all their sectors are reported as allocated.
1800 * 'pnum' is set to the number of sectors (including and immediately following
1801 * the specified sector) that are known to be in the same
1802 * allocated/unallocated state.
1804 * 'nb_sectors' is the max value 'pnum' should be set to.
1806 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1810 if (!bs->drv->bdrv_is_allocated) {
1811 if (sector_num >= bs->total_sectors) {
1815 n = bs->total_sectors - sector_num;
1816 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1819 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1822 void bdrv_mon_event(const BlockDriverState *bdrv,
1823 BlockMonEventAction action, int is_read)
1826 const char *action_str;
1829 case BDRV_ACTION_REPORT:
1830 action_str = "report";
1832 case BDRV_ACTION_IGNORE:
1833 action_str = "ignore";
1835 case BDRV_ACTION_STOP:
1836 action_str = "stop";
1842 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1845 is_read ? "read" : "write");
1846 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1848 qobject_decref(data);
1851 static void bdrv_print_dict(QObject *obj, void *opaque)
1854 Monitor *mon = opaque;
1856 bs_dict = qobject_to_qdict(obj);
1858 monitor_printf(mon, "%s: removable=%d",
1859 qdict_get_str(bs_dict, "device"),
1860 qdict_get_bool(bs_dict, "removable"));
1862 if (qdict_get_bool(bs_dict, "removable")) {
1863 monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1864 monitor_printf(mon, " tray-open=%d",
1865 qdict_get_bool(bs_dict, "tray-open"));
1867 if (qdict_haskey(bs_dict, "inserted")) {
1868 QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1870 monitor_printf(mon, " file=");
1871 monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1872 if (qdict_haskey(qdict, "backing_file")) {
1873 monitor_printf(mon, " backing_file=");
1874 monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1876 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1877 qdict_get_bool(qdict, "ro"),
1878 qdict_get_str(qdict, "drv"),
1879 qdict_get_bool(qdict, "encrypted"));
1881 monitor_printf(mon, " [not inserted]");
1884 monitor_printf(mon, "\n");
1887 void bdrv_info_print(Monitor *mon, const QObject *data)
1889 qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1892 void bdrv_info(Monitor *mon, QObject **ret_data)
1895 BlockDriverState *bs;
1897 bs_list = qlist_new();
1899 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1903 bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
1904 "'removable': %i, 'locked': %i }",
1906 bdrv_dev_has_removable_media(bs),
1907 bdrv_dev_is_medium_locked(bs));
1908 bs_dict = qobject_to_qdict(bs_obj);
1910 if (bdrv_dev_has_removable_media(bs)) {
1911 qdict_put(bs_dict, "tray-open",
1912 qbool_from_int(bdrv_dev_is_tray_open(bs)));
1917 obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1918 "'encrypted': %i }",
1919 bs->filename, bs->read_only,
1920 bs->drv->format_name,
1921 bdrv_is_encrypted(bs));
1922 if (bs->backing_file[0] != '\0') {
1923 QDict *qdict = qobject_to_qdict(obj);
1924 qdict_put(qdict, "backing_file",
1925 qstring_from_str(bs->backing_file));
1928 qdict_put_obj(bs_dict, "inserted", obj);
1930 qlist_append_obj(bs_list, bs_obj);
1933 *ret_data = QOBJECT(bs_list);
1936 static void bdrv_stats_iter(QObject *data, void *opaque)
1939 Monitor *mon = opaque;
1941 qdict = qobject_to_qdict(data);
1942 monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1944 qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1945 monitor_printf(mon, " rd_bytes=%" PRId64
1946 " wr_bytes=%" PRId64
1947 " rd_operations=%" PRId64
1948 " wr_operations=%" PRId64
1949 " flush_operations=%" PRId64
1950 " wr_total_time_ns=%" PRId64
1951 " rd_total_time_ns=%" PRId64
1952 " flush_total_time_ns=%" PRId64
1954 qdict_get_int(qdict, "rd_bytes"),
1955 qdict_get_int(qdict, "wr_bytes"),
1956 qdict_get_int(qdict, "rd_operations"),
1957 qdict_get_int(qdict, "wr_operations"),
1958 qdict_get_int(qdict, "flush_operations"),
1959 qdict_get_int(qdict, "wr_total_time_ns"),
1960 qdict_get_int(qdict, "rd_total_time_ns"),
1961 qdict_get_int(qdict, "flush_total_time_ns"));
1964 void bdrv_stats_print(Monitor *mon, const QObject *data)
1966 qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1969 static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1974 res = qobject_from_jsonf("{ 'stats': {"
1975 "'rd_bytes': %" PRId64 ","
1976 "'wr_bytes': %" PRId64 ","
1977 "'rd_operations': %" PRId64 ","
1978 "'wr_operations': %" PRId64 ","
1979 "'wr_highest_offset': %" PRId64 ","
1980 "'flush_operations': %" PRId64 ","
1981 "'wr_total_time_ns': %" PRId64 ","
1982 "'rd_total_time_ns': %" PRId64 ","
1983 "'flush_total_time_ns': %" PRId64
1985 bs->nr_bytes[BDRV_ACCT_READ],
1986 bs->nr_bytes[BDRV_ACCT_WRITE],
1987 bs->nr_ops[BDRV_ACCT_READ],
1988 bs->nr_ops[BDRV_ACCT_WRITE],
1989 bs->wr_highest_sector *
1990 (uint64_t)BDRV_SECTOR_SIZE,
1991 bs->nr_ops[BDRV_ACCT_FLUSH],
1992 bs->total_time_ns[BDRV_ACCT_WRITE],
1993 bs->total_time_ns[BDRV_ACCT_READ],
1994 bs->total_time_ns[BDRV_ACCT_FLUSH]);
1995 dict = qobject_to_qdict(res);
1997 if (*bs->device_name) {
1998 qdict_put(dict, "device", qstring_from_str(bs->device_name));
2002 QObject *parent = bdrv_info_stats_bs(bs->file);
2003 qdict_put_obj(dict, "parent", parent);
2009 void bdrv_info_stats(Monitor *mon, QObject **ret_data)
2013 BlockDriverState *bs;
2015 devices = qlist_new();
2017 QTAILQ_FOREACH(bs, &bdrv_states, list) {
2018 obj = bdrv_info_stats_bs(bs);
2019 qlist_append_obj(devices, obj);
2022 *ret_data = QOBJECT(devices);
2025 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
2027 if (bs->backing_hd && bs->backing_hd->encrypted)
2028 return bs->backing_file;
2029 else if (bs->encrypted)
2030 return bs->filename;
2035 void bdrv_get_backing_filename(BlockDriverState *bs,
2036 char *filename, int filename_size)
2038 if (!bs->backing_file) {
2039 pstrcpy(filename, filename_size, "");
2041 pstrcpy(filename, filename_size, bs->backing_file);
2045 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
2046 const uint8_t *buf, int nb_sectors)
2048 BlockDriver *drv = bs->drv;
2051 if (!drv->bdrv_write_compressed)
2053 if (bdrv_check_request(bs, sector_num, nb_sectors))
2056 if (bs->dirty_bitmap) {
2057 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
2060 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
2063 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2065 BlockDriver *drv = bs->drv;
2068 if (!drv->bdrv_get_info)
2070 memset(bdi, 0, sizeof(*bdi));
2071 return drv->bdrv_get_info(bs, bdi);
2074 int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
2075 int64_t pos, int size)
2077 BlockDriver *drv = bs->drv;
2080 if (drv->bdrv_save_vmstate)
2081 return drv->bdrv_save_vmstate(bs, buf, pos, size);
2083 return bdrv_save_vmstate(bs->file, buf, pos, size);
2087 int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2088 int64_t pos, int size)
2090 BlockDriver *drv = bs->drv;
2093 if (drv->bdrv_load_vmstate)
2094 return drv->bdrv_load_vmstate(bs, buf, pos, size);
2096 return bdrv_load_vmstate(bs->file, buf, pos, size);
2100 void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
2102 BlockDriver *drv = bs->drv;
2104 if (!drv || !drv->bdrv_debug_event) {
2108 return drv->bdrv_debug_event(bs, event);
2112 /**************************************************************/
2113 /* handling of snapshots */
2115 int bdrv_can_snapshot(BlockDriverState *bs)
2117 BlockDriver *drv = bs->drv;
2118 if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2122 if (!drv->bdrv_snapshot_create) {
2123 if (bs->file != NULL) {
2124 return bdrv_can_snapshot(bs->file);
2132 int bdrv_is_snapshot(BlockDriverState *bs)
2134 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
2137 BlockDriverState *bdrv_snapshots(void)
2139 BlockDriverState *bs;
2142 return bs_snapshots;
2146 while ((bs = bdrv_next(bs))) {
2147 if (bdrv_can_snapshot(bs)) {
2155 int bdrv_snapshot_create(BlockDriverState *bs,
2156 QEMUSnapshotInfo *sn_info)
2158 BlockDriver *drv = bs->drv;
2161 if (drv->bdrv_snapshot_create)
2162 return drv->bdrv_snapshot_create(bs, sn_info);
2164 return bdrv_snapshot_create(bs->file, sn_info);
2168 int bdrv_snapshot_goto(BlockDriverState *bs,
2169 const char *snapshot_id)
2171 BlockDriver *drv = bs->drv;
2176 if (drv->bdrv_snapshot_goto)
2177 return drv->bdrv_snapshot_goto(bs, snapshot_id);
2180 drv->bdrv_close(bs);
2181 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
2182 open_ret = drv->bdrv_open(bs, bs->open_flags);
2184 bdrv_delete(bs->file);
2194 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
2196 BlockDriver *drv = bs->drv;
2199 if (drv->bdrv_snapshot_delete)
2200 return drv->bdrv_snapshot_delete(bs, snapshot_id);
2202 return bdrv_snapshot_delete(bs->file, snapshot_id);
2206 int bdrv_snapshot_list(BlockDriverState *bs,
2207 QEMUSnapshotInfo **psn_info)
2209 BlockDriver *drv = bs->drv;
2212 if (drv->bdrv_snapshot_list)
2213 return drv->bdrv_snapshot_list(bs, psn_info);
2215 return bdrv_snapshot_list(bs->file, psn_info);
2219 int bdrv_snapshot_load_tmp(BlockDriverState *bs,
2220 const char *snapshot_name)
2222 BlockDriver *drv = bs->drv;
2226 if (!bs->read_only) {
2229 if (drv->bdrv_snapshot_load_tmp) {
2230 return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
2235 #define NB_SUFFIXES 4
2237 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
2239 static const char suffixes[NB_SUFFIXES] = "KMGT";
2244 snprintf(buf, buf_size, "%" PRId64, size);
2247 for(i = 0; i < NB_SUFFIXES; i++) {
2248 if (size < (10 * base)) {
2249 snprintf(buf, buf_size, "%0.1f%c",
2250 (double)size / base,
2253 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
2254 snprintf(buf, buf_size, "%" PRId64 "%c",
2255 ((size + (base >> 1)) / base),
2265 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
2267 char buf1[128], date_buf[128], clock_buf[128];
2277 snprintf(buf, buf_size,
2278 "%-10s%-20s%7s%20s%15s",
2279 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
2283 ptm = localtime(&ti);
2284 strftime(date_buf, sizeof(date_buf),
2285 "%Y-%m-%d %H:%M:%S", ptm);
2287 localtime_r(&ti, &tm);
2288 strftime(date_buf, sizeof(date_buf),
2289 "%Y-%m-%d %H:%M:%S", &tm);
2291 secs = sn->vm_clock_nsec / 1000000000;
2292 snprintf(clock_buf, sizeof(clock_buf),
2293 "%02d:%02d:%02d.%03d",
2295 (int)((secs / 60) % 60),
2297 (int)((sn->vm_clock_nsec / 1000000) % 1000));
2298 snprintf(buf, buf_size,
2299 "%-10s%-20s%7s%20s%15s",
2300 sn->id_str, sn->name,
2301 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
2308 /**************************************************************/
2311 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
2312 QEMUIOVector *qiov, int nb_sectors,
2313 BlockDriverCompletionFunc *cb, void *opaque)
2315 BlockDriver *drv = bs->drv;
2317 trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
2321 if (bdrv_check_request(bs, sector_num, nb_sectors))
2324 return drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
2328 typedef struct BlockCompleteData {
2329 BlockDriverCompletionFunc *cb;
2331 BlockDriverState *bs;
2334 } BlockCompleteData;
2336 static void block_complete_cb(void *opaque, int ret)
2338 BlockCompleteData *b = opaque;
2340 if (b->bs->dirty_bitmap) {
2341 set_dirty_bitmap(b->bs, b->sector_num, b->nb_sectors, 1);
2343 b->cb(b->opaque, ret);
2347 static BlockCompleteData *blk_dirty_cb_alloc(BlockDriverState *bs,
2350 BlockDriverCompletionFunc *cb,
2353 BlockCompleteData *blkdata = g_malloc0(sizeof(BlockCompleteData));
2357 blkdata->opaque = opaque;
2358 blkdata->sector_num = sector_num;
2359 blkdata->nb_sectors = nb_sectors;
2364 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
2365 QEMUIOVector *qiov, int nb_sectors,
2366 BlockDriverCompletionFunc *cb, void *opaque)
2368 BlockDriver *drv = bs->drv;
2369 BlockDriverAIOCB *ret;
2370 BlockCompleteData *blk_cb_data;
2372 trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
2378 if (bdrv_check_request(bs, sector_num, nb_sectors))
2381 if (bs->dirty_bitmap) {
2382 blk_cb_data = blk_dirty_cb_alloc(bs, sector_num, nb_sectors, cb,
2384 cb = &block_complete_cb;
2385 opaque = blk_cb_data;
2388 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
2392 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2393 bs->wr_highest_sector = sector_num + nb_sectors - 1;
2401 typedef struct MultiwriteCB {
2406 BlockDriverCompletionFunc *cb;
2408 QEMUIOVector *free_qiov;
2413 static void multiwrite_user_cb(MultiwriteCB *mcb)
2417 for (i = 0; i < mcb->num_callbacks; i++) {
2418 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
2419 if (mcb->callbacks[i].free_qiov) {
2420 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
2422 g_free(mcb->callbacks[i].free_qiov);
2423 qemu_vfree(mcb->callbacks[i].free_buf);
2427 static void multiwrite_cb(void *opaque, int ret)
2429 MultiwriteCB *mcb = opaque;
2431 trace_multiwrite_cb(mcb, ret);
2433 if (ret < 0 && !mcb->error) {
2437 mcb->num_requests--;
2438 if (mcb->num_requests == 0) {
2439 multiwrite_user_cb(mcb);
2444 static int multiwrite_req_compare(const void *a, const void *b)
2446 const BlockRequest *req1 = a, *req2 = b;
2449 * Note that we can't simply subtract req2->sector from req1->sector
2450 * here as that could overflow the return value.
2452 if (req1->sector > req2->sector) {
2454 } else if (req1->sector < req2->sector) {
2462 * Takes a bunch of requests and tries to merge them. Returns the number of
2463 * requests that remain after merging.
2465 static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
2466 int num_reqs, MultiwriteCB *mcb)
2470 // Sort requests by start sector
2471 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
2473 // Check if adjacent requests touch the same clusters. If so, combine them,
2474 // filling up gaps with zero sectors.
2476 for (i = 1; i < num_reqs; i++) {
2478 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
2480 // This handles the cases that are valid for all block drivers, namely
2481 // exactly sequential writes and overlapping writes.
2482 if (reqs[i].sector <= oldreq_last) {
2486 // The block driver may decide that it makes sense to combine requests
2487 // even if there is a gap of some sectors between them. In this case,
2488 // the gap is filled with zeros (therefore only applicable for yet
2489 // unused space in format like qcow2).
2490 if (!merge && bs->drv->bdrv_merge_requests) {
2491 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
2494 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2500 QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
2501 qemu_iovec_init(qiov,
2502 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2504 // Add the first request to the merged one. If the requests are
2505 // overlapping, drop the last sectors of the first request.
2506 size = (reqs[i].sector - reqs[outidx].sector) << 9;
2507 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2509 // We might need to add some zeros between the two requests
2510 if (reqs[i].sector > oldreq_last) {
2511 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2512 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2513 memset(buf, 0, zero_bytes);
2514 qemu_iovec_add(qiov, buf, zero_bytes);
2515 mcb->callbacks[i].free_buf = buf;
2518 // Add the second request
2519 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2521 reqs[outidx].nb_sectors = qiov->size >> 9;
2522 reqs[outidx].qiov = qiov;
2524 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2527 reqs[outidx].sector = reqs[i].sector;
2528 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2529 reqs[outidx].qiov = reqs[i].qiov;
2537 * Submit multiple AIO write requests at once.
2539 * On success, the function returns 0 and all requests in the reqs array have
2540 * been submitted. In error case this function returns -1, and any of the
2541 * requests may or may not be submitted yet. In particular, this means that the
2542 * callback will be called for some of the requests, for others it won't. The
2543 * caller must check the error field of the BlockRequest to wait for the right
2544 * callbacks (if error != 0, no callback will be called).
2546 * The implementation may modify the contents of the reqs array, e.g. to merge
2547 * requests. However, the fields opaque and error are left unmodified as they
2548 * are used to signal failure for a single request to the caller.
2550 int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2552 BlockDriverAIOCB *acb;
2556 /* don't submit writes if we don't have a medium */
2557 if (bs->drv == NULL) {
2558 for (i = 0; i < num_reqs; i++) {
2559 reqs[i].error = -ENOMEDIUM;
2564 if (num_reqs == 0) {
2568 // Create MultiwriteCB structure
2569 mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
2570 mcb->num_requests = 0;
2571 mcb->num_callbacks = num_reqs;
2573 for (i = 0; i < num_reqs; i++) {
2574 mcb->callbacks[i].cb = reqs[i].cb;
2575 mcb->callbacks[i].opaque = reqs[i].opaque;
2578 // Check for mergable requests
2579 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2581 trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
2584 * Run the aio requests. As soon as one request can't be submitted
2585 * successfully, fail all requests that are not yet submitted (we must
2586 * return failure for all requests anyway)
2588 * num_requests cannot be set to the right value immediately: If
2589 * bdrv_aio_writev fails for some request, num_requests would be too high
2590 * and therefore multiwrite_cb() would never recognize the multiwrite
2591 * request as completed. We also cannot use the loop variable i to set it
2592 * when the first request fails because the callback may already have been
2593 * called for previously submitted requests. Thus, num_requests must be
2594 * incremented for each request that is submitted.
2596 * The problem that callbacks may be called early also means that we need
2597 * to take care that num_requests doesn't become 0 before all requests are
2598 * submitted - multiwrite_cb() would consider the multiwrite request
2599 * completed. A dummy request that is "completed" by a manual call to
2600 * multiwrite_cb() takes care of this.
2602 mcb->num_requests = 1;
2604 // Run the aio requests
2605 for (i = 0; i < num_reqs; i++) {
2606 mcb->num_requests++;
2607 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2608 reqs[i].nb_sectors, multiwrite_cb, mcb);
2611 // We can only fail the whole thing if no request has been
2612 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2613 // complete and report the error in the callback.
2615 trace_bdrv_aio_multiwrite_earlyfail(mcb);
2618 trace_bdrv_aio_multiwrite_latefail(mcb, i);
2619 multiwrite_cb(mcb, -EIO);
2625 /* Complete the dummy request */
2626 multiwrite_cb(mcb, 0);
2631 for (i = 0; i < mcb->num_callbacks; i++) {
2632 reqs[i].error = -EIO;
2638 BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2639 BlockDriverCompletionFunc *cb, void *opaque)
2641 BlockDriver *drv = bs->drv;
2643 trace_bdrv_aio_flush(bs, opaque);
2645 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2646 return bdrv_aio_noop_em(bs, cb, opaque);
2651 return drv->bdrv_aio_flush(bs, cb, opaque);
2654 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
2656 acb->pool->cancel(acb);
2660 /**************************************************************/
2661 /* async block device emulation */
2663 typedef struct BlockDriverAIOCBSync {
2664 BlockDriverAIOCB common;
2667 /* vector translation state */
2671 } BlockDriverAIOCBSync;
2673 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2675 BlockDriverAIOCBSync *acb =
2676 container_of(blockacb, BlockDriverAIOCBSync, common);
2677 qemu_bh_delete(acb->bh);
2679 qemu_aio_release(acb);
2682 static AIOPool bdrv_em_aio_pool = {
2683 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2684 .cancel = bdrv_aio_cancel_em,
2687 static void bdrv_aio_bh_cb(void *opaque)
2689 BlockDriverAIOCBSync *acb = opaque;
2692 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
2693 qemu_vfree(acb->bounce);
2694 acb->common.cb(acb->common.opaque, acb->ret);
2695 qemu_bh_delete(acb->bh);
2697 qemu_aio_release(acb);
2700 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2704 BlockDriverCompletionFunc *cb,
2709 BlockDriverAIOCBSync *acb;
2711 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2712 acb->is_write = is_write;
2714 acb->bounce = qemu_blockalign(bs, qiov->size);
2717 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2720 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2721 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2723 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2726 qemu_bh_schedule(acb->bh);
2728 return &acb->common;
2731 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2732 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2733 BlockDriverCompletionFunc *cb, void *opaque)
2735 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
2738 static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2739 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2740 BlockDriverCompletionFunc *cb, void *opaque)
2742 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2746 typedef struct BlockDriverAIOCBCoroutine {
2747 BlockDriverAIOCB common;
2751 } BlockDriverAIOCBCoroutine;
2753 static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb)
2758 static AIOPool bdrv_em_co_aio_pool = {
2759 .aiocb_size = sizeof(BlockDriverAIOCBCoroutine),
2760 .cancel = bdrv_aio_co_cancel_em,
2763 static void bdrv_co_rw_bh(void *opaque)
2765 BlockDriverAIOCBCoroutine *acb = opaque;
2767 acb->common.cb(acb->common.opaque, acb->req.error);
2768 qemu_bh_delete(acb->bh);
2769 qemu_aio_release(acb);
2772 static void coroutine_fn bdrv_co_rw(void *opaque)
2774 BlockDriverAIOCBCoroutine *acb = opaque;
2775 BlockDriverState *bs = acb->common.bs;
2777 if (!acb->is_write) {
2778 acb->req.error = bs->drv->bdrv_co_readv(bs, acb->req.sector,
2779 acb->req.nb_sectors, acb->req.qiov);
2781 acb->req.error = bs->drv->bdrv_co_writev(bs, acb->req.sector,
2782 acb->req.nb_sectors, acb->req.qiov);
2785 acb->bh = qemu_bh_new(bdrv_co_rw_bh, acb);
2786 qemu_bh_schedule(acb->bh);
2789 static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
2793 BlockDriverCompletionFunc *cb,
2798 BlockDriverAIOCBCoroutine *acb;
2800 acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
2801 acb->req.sector = sector_num;
2802 acb->req.nb_sectors = nb_sectors;
2803 acb->req.qiov = qiov;
2804 acb->is_write = is_write;
2806 co = qemu_coroutine_create(bdrv_co_rw);
2807 qemu_coroutine_enter(co, acb);
2809 return &acb->common;
2812 static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs,
2813 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2814 BlockDriverCompletionFunc *cb, void *opaque)
2816 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque,
2820 static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs,
2821 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2822 BlockDriverCompletionFunc *cb, void *opaque)
2824 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque,
2828 static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2829 BlockDriverCompletionFunc *cb, void *opaque)
2831 BlockDriverAIOCBSync *acb;
2833 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2834 acb->is_write = 1; /* don't bounce in the completion hadler */
2840 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2843 qemu_bh_schedule(acb->bh);
2844 return &acb->common;
2847 static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2848 BlockDriverCompletionFunc *cb, void *opaque)
2850 BlockDriverAIOCBSync *acb;
2852 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2853 acb->is_write = 1; /* don't bounce in the completion handler */
2859 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2862 qemu_bh_schedule(acb->bh);
2863 return &acb->common;
2866 /**************************************************************/
2867 /* sync block device emulation */
2869 static void bdrv_rw_em_cb(void *opaque, int ret)
2871 *(int *)opaque = ret;
2874 #define NOT_DONE 0x7fffffff
2876 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
2877 uint8_t *buf, int nb_sectors)
2880 BlockDriverAIOCB *acb;
2884 async_ret = NOT_DONE;
2885 iov.iov_base = (void *)buf;
2886 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2887 qemu_iovec_init_external(&qiov, &iov, 1);
2888 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2889 bdrv_rw_em_cb, &async_ret);
2895 while (async_ret == NOT_DONE) {
2904 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2905 const uint8_t *buf, int nb_sectors)
2908 BlockDriverAIOCB *acb;
2912 async_ret = NOT_DONE;
2913 iov.iov_base = (void *)buf;
2914 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2915 qemu_iovec_init_external(&qiov, &iov, 1);
2916 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2917 bdrv_rw_em_cb, &async_ret);
2922 while (async_ret == NOT_DONE) {
2930 void bdrv_init(void)
2932 module_call_init(MODULE_INIT_BLOCK);
2935 void bdrv_init_with_whitelist(void)
2937 use_bdrv_whitelist = 1;
2941 void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2942 BlockDriverCompletionFunc *cb, void *opaque)
2944 BlockDriverAIOCB *acb;
2946 if (pool->free_aiocb) {
2947 acb = pool->free_aiocb;
2948 pool->free_aiocb = acb->next;
2950 acb = g_malloc0(pool->aiocb_size);
2955 acb->opaque = opaque;
2959 void qemu_aio_release(void *p)
2961 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2962 AIOPool *pool = acb->pool;
2963 acb->next = pool->free_aiocb;
2964 pool->free_aiocb = acb;
2967 /**************************************************************/
2968 /* Coroutine block device emulation */
2970 typedef struct CoroutineIOCompletion {
2971 Coroutine *coroutine;
2973 } CoroutineIOCompletion;
2975 static void bdrv_co_io_em_complete(void *opaque, int ret)
2977 CoroutineIOCompletion *co = opaque;
2980 qemu_coroutine_enter(co->coroutine, NULL);
2983 static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num,
2984 int nb_sectors, QEMUIOVector *iov,
2987 CoroutineIOCompletion co = {
2988 .coroutine = qemu_coroutine_self(),
2990 BlockDriverAIOCB *acb;
2993 acb = bdrv_aio_writev(bs, sector_num, iov, nb_sectors,
2994 bdrv_co_io_em_complete, &co);
2996 acb = bdrv_aio_readv(bs, sector_num, iov, nb_sectors,
2997 bdrv_co_io_em_complete, &co);
3000 trace_bdrv_co_io(is_write, acb);
3004 qemu_coroutine_yield();
3009 static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
3010 int64_t sector_num, int nb_sectors,
3013 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false);
3016 static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
3017 int64_t sector_num, int nb_sectors,
3020 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true);
3023 static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs)
3025 CoroutineIOCompletion co = {
3026 .coroutine = qemu_coroutine_self(),
3028 BlockDriverAIOCB *acb;
3030 acb = bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
3034 qemu_coroutine_yield();
3038 /**************************************************************/
3039 /* removable device support */
3042 * Return TRUE if the media is present
3044 int bdrv_is_inserted(BlockDriverState *bs)
3046 BlockDriver *drv = bs->drv;
3050 if (!drv->bdrv_is_inserted)
3052 return drv->bdrv_is_inserted(bs);
3056 * Return whether the media changed since the last call to this
3057 * function, or -ENOTSUP if we don't know. Most drivers don't know.
3059 int bdrv_media_changed(BlockDriverState *bs)
3061 BlockDriver *drv = bs->drv;
3063 if (drv && drv->bdrv_media_changed) {
3064 return drv->bdrv_media_changed(bs);
3070 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3072 void bdrv_eject(BlockDriverState *bs, int eject_flag)
3074 BlockDriver *drv = bs->drv;
3076 if (drv && drv->bdrv_eject) {
3077 drv->bdrv_eject(bs, eject_flag);
3082 * Lock or unlock the media (if it is locked, the user won't be able
3083 * to eject it manually).
3085 void bdrv_lock_medium(BlockDriverState *bs, bool locked)
3087 BlockDriver *drv = bs->drv;
3089 trace_bdrv_lock_medium(bs, locked);
3091 if (drv && drv->bdrv_lock_medium) {
3092 drv->bdrv_lock_medium(bs, locked);
3096 /* needed for generic scsi interface */
3098 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
3100 BlockDriver *drv = bs->drv;
3102 if (drv && drv->bdrv_ioctl)
3103 return drv->bdrv_ioctl(bs, req, buf);
3107 BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
3108 unsigned long int req, void *buf,
3109 BlockDriverCompletionFunc *cb, void *opaque)
3111 BlockDriver *drv = bs->drv;
3113 if (drv && drv->bdrv_aio_ioctl)
3114 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
3118 void bdrv_set_buffer_alignment(BlockDriverState *bs, int align)
3120 bs->buffer_alignment = align;
3123 void *qemu_blockalign(BlockDriverState *bs, size_t size)
3125 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
3128 void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
3130 int64_t bitmap_size;
3132 bs->dirty_count = 0;
3134 if (!bs->dirty_bitmap) {
3135 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
3136 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
3137 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
3139 bs->dirty_bitmap = g_malloc0(bitmap_size);
3142 if (bs->dirty_bitmap) {
3143 g_free(bs->dirty_bitmap);
3144 bs->dirty_bitmap = NULL;
3149 int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
3151 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
3153 if (bs->dirty_bitmap &&
3154 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
3155 return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
3156 (1UL << (chunk % (sizeof(unsigned long) * 8))));
3162 void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
3165 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
3168 int64_t bdrv_get_dirty_count(BlockDriverState *bs)
3170 return bs->dirty_count;
3173 void bdrv_set_in_use(BlockDriverState *bs, int in_use)
3175 assert(bs->in_use != in_use);
3176 bs->in_use = in_use;
3179 int bdrv_in_use(BlockDriverState *bs)
3185 bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes,
3186 enum BlockAcctType type)
3188 assert(type < BDRV_MAX_IOTYPE);
3190 cookie->bytes = bytes;
3191 cookie->start_time_ns = get_clock();
3192 cookie->type = type;
3196 bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
3198 assert(cookie->type < BDRV_MAX_IOTYPE);
3200 bs->nr_bytes[cookie->type] += cookie->bytes;
3201 bs->nr_ops[cookie->type]++;
3202 bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
3205 int bdrv_img_create(const char *filename, const char *fmt,
3206 const char *base_filename, const char *base_fmt,
3207 char *options, uint64_t img_size, int flags)
3209 QEMUOptionParameter *param = NULL, *create_options = NULL;
3210 QEMUOptionParameter *backing_fmt, *backing_file, *size;
3211 BlockDriverState *bs = NULL;
3212 BlockDriver *drv, *proto_drv;
3213 BlockDriver *backing_drv = NULL;
3216 /* Find driver and parse its options */
3217 drv = bdrv_find_format(fmt);
3219 error_report("Unknown file format '%s'", fmt);
3224 proto_drv = bdrv_find_protocol(filename);
3226 error_report("Unknown protocol '%s'", filename);
3231 create_options = append_option_parameters(create_options,
3232 drv->create_options);
3233 create_options = append_option_parameters(create_options,
3234 proto_drv->create_options);
3236 /* Create parameter list with default values */
3237 param = parse_option_parameters("", create_options, param);
3239 set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
3241 /* Parse -o options */
3243 param = parse_option_parameters(options, create_options, param);
3244 if (param == NULL) {
3245 error_report("Invalid options for file format '%s'.", fmt);
3251 if (base_filename) {
3252 if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
3254 error_report("Backing file not supported for file format '%s'",
3262 if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
3263 error_report("Backing file format not supported for file "
3264 "format '%s'", fmt);
3270 backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
3271 if (backing_file && backing_file->value.s) {
3272 if (!strcmp(filename, backing_file->value.s)) {
3273 error_report("Error: Trying to create an image with the "
3274 "same filename as the backing file");
3280 backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
3281 if (backing_fmt && backing_fmt->value.s) {
3282 backing_drv = bdrv_find_format(backing_fmt->value.s);
3284 error_report("Unknown backing file format '%s'",
3285 backing_fmt->value.s);
3291 // The size for the image must always be specified, with one exception:
3292 // If we are using a backing file, we can obtain the size from there
3293 size = get_option_parameter(param, BLOCK_OPT_SIZE);
3294 if (size && size->value.n == -1) {
3295 if (backing_file && backing_file->value.s) {
3301 ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv);
3303 error_report("Could not open '%s'", backing_file->value.s);
3306 bdrv_get_geometry(bs, &size);
3309 snprintf(buf, sizeof(buf), "%" PRId64, size);
3310 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
3312 error_report("Image creation needs a size parameter");
3318 printf("Formatting '%s', fmt=%s ", filename, fmt);
3319 print_option_parameters(param);
3322 ret = bdrv_create(drv, filename, param);
3325 if (ret == -ENOTSUP) {
3326 error_report("Formatting or formatting option not supported for "
3327 "file format '%s'", fmt);
3328 } else if (ret == -EFBIG) {
3329 error_report("The image size is too large for file format '%s'",
3332 error_report("%s: error while creating %s: %s", filename, fmt,
3338 free_option_parameters(create_options);
3339 free_option_parameters(param);