block: switch bdrv_read()/bdrv_write() to coroutines
[qemu/ar7.git] / block.c
blobae8fc80ffb9fe017d937f6b06964ea1ad09a9a18
1 /*
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
22 * THE SOFTWARE.
24 #include "config-host.h"
25 #include "qemu-common.h"
26 #include "trace.h"
27 #include "monitor.h"
28 #include "block_int.h"
29 #include "module.h"
30 #include "qemu-objects.h"
31 #include "qemu-coroutine.h"
33 #ifdef CONFIG_BSD
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/ioctl.h>
37 #include <sys/queue.h>
38 #ifndef __DragonFly__
39 #include <sys/disk.h>
40 #endif
41 #endif
43 #ifdef _WIN32
44 #include <windows.h>
45 #endif
47 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
49 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
50 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
51 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
52 BlockDriverCompletionFunc *cb, void *opaque);
53 static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
54 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
55 BlockDriverCompletionFunc *cb, void *opaque);
56 static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
57 BlockDriverCompletionFunc *cb, void *opaque);
58 static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
59 BlockDriverCompletionFunc *cb, void *opaque);
60 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
61 uint8_t *buf, int nb_sectors);
62 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
63 const uint8_t *buf, int nb_sectors);
64 static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs,
65 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
66 BlockDriverCompletionFunc *cb, void *opaque);
67 static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs,
68 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
69 BlockDriverCompletionFunc *cb, void *opaque);
70 static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
71 int64_t sector_num, int nb_sectors,
72 QEMUIOVector *iov);
73 static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
74 int64_t sector_num, int nb_sectors,
75 QEMUIOVector *iov);
76 static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs);
77 static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
78 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
79 static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
80 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
82 static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
83 QTAILQ_HEAD_INITIALIZER(bdrv_states);
85 static QLIST_HEAD(, BlockDriver) bdrv_drivers =
86 QLIST_HEAD_INITIALIZER(bdrv_drivers);
88 /* The device to use for VM snapshots */
89 static BlockDriverState *bs_snapshots;
91 /* If non-zero, use only whitelisted block drivers */
92 static int use_bdrv_whitelist;
94 #ifdef _WIN32
95 static int is_windows_drive_prefix(const char *filename)
97 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
98 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
99 filename[1] == ':');
102 int is_windows_drive(const char *filename)
104 if (is_windows_drive_prefix(filename) &&
105 filename[2] == '\0')
106 return 1;
107 if (strstart(filename, "\\\\.\\", NULL) ||
108 strstart(filename, "//./", NULL))
109 return 1;
110 return 0;
112 #endif
114 /* check if the path starts with "<protocol>:" */
115 static int path_has_protocol(const char *path)
117 #ifdef _WIN32
118 if (is_windows_drive(path) ||
119 is_windows_drive_prefix(path)) {
120 return 0;
122 #endif
124 return strchr(path, ':') != NULL;
127 int path_is_absolute(const char *path)
129 const char *p;
130 #ifdef _WIN32
131 /* specific case for names like: "\\.\d:" */
132 if (*path == '/' || *path == '\\')
133 return 1;
134 #endif
135 p = strchr(path, ':');
136 if (p)
137 p++;
138 else
139 p = path;
140 #ifdef _WIN32
141 return (*p == '/' || *p == '\\');
142 #else
143 return (*p == '/');
144 #endif
147 /* if filename is absolute, just copy it to dest. Otherwise, build a
148 path to it by considering it is relative to base_path. URL are
149 supported. */
150 void path_combine(char *dest, int dest_size,
151 const char *base_path,
152 const char *filename)
154 const char *p, *p1;
155 int len;
157 if (dest_size <= 0)
158 return;
159 if (path_is_absolute(filename)) {
160 pstrcpy(dest, dest_size, filename);
161 } else {
162 p = strchr(base_path, ':');
163 if (p)
164 p++;
165 else
166 p = base_path;
167 p1 = strrchr(base_path, '/');
168 #ifdef _WIN32
170 const char *p2;
171 p2 = strrchr(base_path, '\\');
172 if (!p1 || p2 > p1)
173 p1 = p2;
175 #endif
176 if (p1)
177 p1++;
178 else
179 p1 = base_path;
180 if (p1 > p)
181 p = p1;
182 len = p - base_path;
183 if (len > dest_size - 1)
184 len = dest_size - 1;
185 memcpy(dest, base_path, len);
186 dest[len] = '\0';
187 pstrcat(dest, dest_size, filename);
191 void bdrv_register(BlockDriver *bdrv)
193 if (bdrv->bdrv_co_readv) {
194 /* Emulate AIO by coroutines, and sync by AIO */
195 bdrv->bdrv_aio_readv = bdrv_co_aio_readv_em;
196 bdrv->bdrv_aio_writev = bdrv_co_aio_writev_em;
197 bdrv->bdrv_read = bdrv_read_em;
198 bdrv->bdrv_write = bdrv_write_em;
199 } else {
200 bdrv->bdrv_co_readv = bdrv_co_readv_em;
201 bdrv->bdrv_co_writev = bdrv_co_writev_em;
203 if (!bdrv->bdrv_aio_readv) {
204 /* add AIO emulation layer */
205 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
206 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
207 } else if (!bdrv->bdrv_read) {
208 /* add synchronous IO emulation layer */
209 bdrv->bdrv_read = bdrv_read_em;
210 bdrv->bdrv_write = bdrv_write_em;
214 if (!bdrv->bdrv_aio_flush)
215 bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
217 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
220 /* create a new block device (by default it is empty) */
221 BlockDriverState *bdrv_new(const char *device_name)
223 BlockDriverState *bs;
225 bs = g_malloc0(sizeof(BlockDriverState));
226 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
227 if (device_name[0] != '\0') {
228 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
230 bdrv_iostatus_disable(bs);
231 return bs;
234 BlockDriver *bdrv_find_format(const char *format_name)
236 BlockDriver *drv1;
237 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
238 if (!strcmp(drv1->format_name, format_name)) {
239 return drv1;
242 return NULL;
245 static int bdrv_is_whitelisted(BlockDriver *drv)
247 static const char *whitelist[] = {
248 CONFIG_BDRV_WHITELIST
250 const char **p;
252 if (!whitelist[0])
253 return 1; /* no whitelist, anything goes */
255 for (p = whitelist; *p; p++) {
256 if (!strcmp(drv->format_name, *p)) {
257 return 1;
260 return 0;
263 BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
265 BlockDriver *drv = bdrv_find_format(format_name);
266 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
269 int bdrv_create(BlockDriver *drv, const char* filename,
270 QEMUOptionParameter *options)
272 if (!drv->bdrv_create)
273 return -ENOTSUP;
275 return drv->bdrv_create(filename, options);
278 int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
280 BlockDriver *drv;
282 drv = bdrv_find_protocol(filename);
283 if (drv == NULL) {
284 return -ENOENT;
287 return bdrv_create(drv, filename, options);
290 #ifdef _WIN32
291 void get_tmp_filename(char *filename, int size)
293 char temp_dir[MAX_PATH];
295 GetTempPath(MAX_PATH, temp_dir);
296 GetTempFileName(temp_dir, "qem", 0, filename);
298 #else
299 void get_tmp_filename(char *filename, int size)
301 int fd;
302 const char *tmpdir;
303 /* XXX: race condition possible */
304 tmpdir = getenv("TMPDIR");
305 if (!tmpdir)
306 tmpdir = "/tmp";
307 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
308 fd = mkstemp(filename);
309 close(fd);
311 #endif
314 * Detect host devices. By convention, /dev/cdrom[N] is always
315 * recognized as a host CDROM.
317 static BlockDriver *find_hdev_driver(const char *filename)
319 int score_max = 0, score;
320 BlockDriver *drv = NULL, *d;
322 QLIST_FOREACH(d, &bdrv_drivers, list) {
323 if (d->bdrv_probe_device) {
324 score = d->bdrv_probe_device(filename);
325 if (score > score_max) {
326 score_max = score;
327 drv = d;
332 return drv;
335 BlockDriver *bdrv_find_protocol(const char *filename)
337 BlockDriver *drv1;
338 char protocol[128];
339 int len;
340 const char *p;
342 /* TODO Drivers without bdrv_file_open must be specified explicitly */
345 * XXX(hch): we really should not let host device detection
346 * override an explicit protocol specification, but moving this
347 * later breaks access to device names with colons in them.
348 * Thanks to the brain-dead persistent naming schemes on udev-
349 * based Linux systems those actually are quite common.
351 drv1 = find_hdev_driver(filename);
352 if (drv1) {
353 return drv1;
356 if (!path_has_protocol(filename)) {
357 return bdrv_find_format("file");
359 p = strchr(filename, ':');
360 assert(p != NULL);
361 len = p - filename;
362 if (len > sizeof(protocol) - 1)
363 len = sizeof(protocol) - 1;
364 memcpy(protocol, filename, len);
365 protocol[len] = '\0';
366 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
367 if (drv1->protocol_name &&
368 !strcmp(drv1->protocol_name, protocol)) {
369 return drv1;
372 return NULL;
375 static int find_image_format(const char *filename, BlockDriver **pdrv)
377 int ret, score, score_max;
378 BlockDriver *drv1, *drv;
379 uint8_t buf[2048];
380 BlockDriverState *bs;
382 ret = bdrv_file_open(&bs, filename, 0);
383 if (ret < 0) {
384 *pdrv = NULL;
385 return ret;
388 /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
389 if (bs->sg || !bdrv_is_inserted(bs)) {
390 bdrv_delete(bs);
391 drv = bdrv_find_format("raw");
392 if (!drv) {
393 ret = -ENOENT;
395 *pdrv = drv;
396 return ret;
399 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
400 bdrv_delete(bs);
401 if (ret < 0) {
402 *pdrv = NULL;
403 return ret;
406 score_max = 0;
407 drv = NULL;
408 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
409 if (drv1->bdrv_probe) {
410 score = drv1->bdrv_probe(buf, ret, filename);
411 if (score > score_max) {
412 score_max = score;
413 drv = drv1;
417 if (!drv) {
418 ret = -ENOENT;
420 *pdrv = drv;
421 return ret;
425 * Set the current 'total_sectors' value
427 static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
429 BlockDriver *drv = bs->drv;
431 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
432 if (bs->sg)
433 return 0;
435 /* query actual device if possible, otherwise just trust the hint */
436 if (drv->bdrv_getlength) {
437 int64_t length = drv->bdrv_getlength(bs);
438 if (length < 0) {
439 return length;
441 hint = length >> BDRV_SECTOR_BITS;
444 bs->total_sectors = hint;
445 return 0;
449 * Set open flags for a given cache mode
451 * Return 0 on success, -1 if the cache mode was invalid.
453 int bdrv_parse_cache_flags(const char *mode, int *flags)
455 *flags &= ~BDRV_O_CACHE_MASK;
457 if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
458 *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
459 } else if (!strcmp(mode, "directsync")) {
460 *flags |= BDRV_O_NOCACHE;
461 } else if (!strcmp(mode, "writeback")) {
462 *flags |= BDRV_O_CACHE_WB;
463 } else if (!strcmp(mode, "unsafe")) {
464 *flags |= BDRV_O_CACHE_WB;
465 *flags |= BDRV_O_NO_FLUSH;
466 } else if (!strcmp(mode, "writethrough")) {
467 /* this is the default */
468 } else {
469 return -1;
472 return 0;
476 * Common part for opening disk images and files
478 static int bdrv_open_common(BlockDriverState *bs, const char *filename,
479 int flags, BlockDriver *drv)
481 int ret, open_flags;
483 assert(drv != NULL);
485 trace_bdrv_open_common(bs, filename, flags, drv->format_name);
487 bs->file = NULL;
488 bs->total_sectors = 0;
489 bs->encrypted = 0;
490 bs->valid_key = 0;
491 bs->open_flags = flags;
492 bs->buffer_alignment = 512;
494 pstrcpy(bs->filename, sizeof(bs->filename), filename);
496 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
497 return -ENOTSUP;
500 bs->drv = drv;
501 bs->opaque = g_malloc0(drv->instance_size);
503 if (flags & BDRV_O_CACHE_WB)
504 bs->enable_write_cache = 1;
507 * Clear flags that are internal to the block layer before opening the
508 * image.
510 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
513 * Snapshots should be writable.
515 if (bs->is_temporary) {
516 open_flags |= BDRV_O_RDWR;
519 /* Open the image, either directly or using a protocol */
520 if (drv->bdrv_file_open) {
521 ret = drv->bdrv_file_open(bs, filename, open_flags);
522 } else {
523 ret = bdrv_file_open(&bs->file, filename, open_flags);
524 if (ret >= 0) {
525 ret = drv->bdrv_open(bs, open_flags);
529 if (ret < 0) {
530 goto free_and_fail;
533 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
535 ret = refresh_total_sectors(bs, bs->total_sectors);
536 if (ret < 0) {
537 goto free_and_fail;
540 #ifndef _WIN32
541 if (bs->is_temporary) {
542 unlink(filename);
544 #endif
545 return 0;
547 free_and_fail:
548 if (bs->file) {
549 bdrv_delete(bs->file);
550 bs->file = NULL;
552 g_free(bs->opaque);
553 bs->opaque = NULL;
554 bs->drv = NULL;
555 return ret;
559 * Opens a file using a protocol (file, host_device, nbd, ...)
561 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
563 BlockDriverState *bs;
564 BlockDriver *drv;
565 int ret;
567 drv = bdrv_find_protocol(filename);
568 if (!drv) {
569 return -ENOENT;
572 bs = bdrv_new("");
573 ret = bdrv_open_common(bs, filename, flags, drv);
574 if (ret < 0) {
575 bdrv_delete(bs);
576 return ret;
578 bs->growable = 1;
579 *pbs = bs;
580 return 0;
584 * Opens a disk image (raw, qcow2, vmdk, ...)
586 int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
587 BlockDriver *drv)
589 int ret;
591 if (flags & BDRV_O_SNAPSHOT) {
592 BlockDriverState *bs1;
593 int64_t total_size;
594 int is_protocol = 0;
595 BlockDriver *bdrv_qcow2;
596 QEMUOptionParameter *options;
597 char tmp_filename[PATH_MAX];
598 char backing_filename[PATH_MAX];
600 /* if snapshot, we create a temporary backing file and open it
601 instead of opening 'filename' directly */
603 /* if there is a backing file, use it */
604 bs1 = bdrv_new("");
605 ret = bdrv_open(bs1, filename, 0, drv);
606 if (ret < 0) {
607 bdrv_delete(bs1);
608 return ret;
610 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
612 if (bs1->drv && bs1->drv->protocol_name)
613 is_protocol = 1;
615 bdrv_delete(bs1);
617 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
619 /* Real path is meaningless for protocols */
620 if (is_protocol)
621 snprintf(backing_filename, sizeof(backing_filename),
622 "%s", filename);
623 else if (!realpath(filename, backing_filename))
624 return -errno;
626 bdrv_qcow2 = bdrv_find_format("qcow2");
627 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
629 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
630 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
631 if (drv) {
632 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
633 drv->format_name);
636 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
637 free_option_parameters(options);
638 if (ret < 0) {
639 return ret;
642 filename = tmp_filename;
643 drv = bdrv_qcow2;
644 bs->is_temporary = 1;
647 /* Find the right image format driver */
648 if (!drv) {
649 ret = find_image_format(filename, &drv);
652 if (!drv) {
653 goto unlink_and_fail;
656 /* Open the image */
657 ret = bdrv_open_common(bs, filename, flags, drv);
658 if (ret < 0) {
659 goto unlink_and_fail;
662 /* If there is a backing file, use it */
663 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
664 char backing_filename[PATH_MAX];
665 int back_flags;
666 BlockDriver *back_drv = NULL;
668 bs->backing_hd = bdrv_new("");
670 if (path_has_protocol(bs->backing_file)) {
671 pstrcpy(backing_filename, sizeof(backing_filename),
672 bs->backing_file);
673 } else {
674 path_combine(backing_filename, sizeof(backing_filename),
675 filename, bs->backing_file);
678 if (bs->backing_format[0] != '\0') {
679 back_drv = bdrv_find_format(bs->backing_format);
682 /* backing files always opened read-only */
683 back_flags =
684 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
686 ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
687 if (ret < 0) {
688 bdrv_close(bs);
689 return ret;
691 if (bs->is_temporary) {
692 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
693 } else {
694 /* base image inherits from "parent" */
695 bs->backing_hd->keep_read_only = bs->keep_read_only;
699 if (!bdrv_key_required(bs)) {
700 bdrv_dev_change_media_cb(bs, true);
703 return 0;
705 unlink_and_fail:
706 if (bs->is_temporary) {
707 unlink(filename);
709 return ret;
712 void bdrv_close(BlockDriverState *bs)
714 if (bs->drv) {
715 if (bs == bs_snapshots) {
716 bs_snapshots = NULL;
718 if (bs->backing_hd) {
719 bdrv_delete(bs->backing_hd);
720 bs->backing_hd = NULL;
722 bs->drv->bdrv_close(bs);
723 g_free(bs->opaque);
724 #ifdef _WIN32
725 if (bs->is_temporary) {
726 unlink(bs->filename);
728 #endif
729 bs->opaque = NULL;
730 bs->drv = NULL;
732 if (bs->file != NULL) {
733 bdrv_close(bs->file);
736 bdrv_dev_change_media_cb(bs, false);
740 void bdrv_close_all(void)
742 BlockDriverState *bs;
744 QTAILQ_FOREACH(bs, &bdrv_states, list) {
745 bdrv_close(bs);
749 /* make a BlockDriverState anonymous by removing from bdrv_state list.
750 Also, NULL terminate the device_name to prevent double remove */
751 void bdrv_make_anon(BlockDriverState *bs)
753 if (bs->device_name[0] != '\0') {
754 QTAILQ_REMOVE(&bdrv_states, bs, list);
756 bs->device_name[0] = '\0';
759 void bdrv_delete(BlockDriverState *bs)
761 assert(!bs->dev);
763 /* remove from list, if necessary */
764 bdrv_make_anon(bs);
766 bdrv_close(bs);
767 if (bs->file != NULL) {
768 bdrv_delete(bs->file);
771 assert(bs != bs_snapshots);
772 g_free(bs);
775 int bdrv_attach_dev(BlockDriverState *bs, void *dev)
776 /* TODO change to DeviceState *dev when all users are qdevified */
778 if (bs->dev) {
779 return -EBUSY;
781 bs->dev = dev;
782 bdrv_iostatus_reset(bs);
783 return 0;
786 /* TODO qdevified devices don't use this, remove when devices are qdevified */
787 void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev)
789 if (bdrv_attach_dev(bs, dev) < 0) {
790 abort();
794 void bdrv_detach_dev(BlockDriverState *bs, void *dev)
795 /* TODO change to DeviceState *dev when all users are qdevified */
797 assert(bs->dev == dev);
798 bs->dev = NULL;
799 bs->dev_ops = NULL;
800 bs->dev_opaque = NULL;
801 bs->buffer_alignment = 512;
804 /* TODO change to return DeviceState * when all users are qdevified */
805 void *bdrv_get_attached_dev(BlockDriverState *bs)
807 return bs->dev;
810 void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
811 void *opaque)
813 bs->dev_ops = ops;
814 bs->dev_opaque = opaque;
815 if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
816 bs_snapshots = NULL;
820 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load)
822 if (bs->dev_ops && bs->dev_ops->change_media_cb) {
823 bs->dev_ops->change_media_cb(bs->dev_opaque, load);
827 bool bdrv_dev_has_removable_media(BlockDriverState *bs)
829 return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
832 bool bdrv_dev_is_tray_open(BlockDriverState *bs)
834 if (bs->dev_ops && bs->dev_ops->is_tray_open) {
835 return bs->dev_ops->is_tray_open(bs->dev_opaque);
837 return false;
840 static void bdrv_dev_resize_cb(BlockDriverState *bs)
842 if (bs->dev_ops && bs->dev_ops->resize_cb) {
843 bs->dev_ops->resize_cb(bs->dev_opaque);
847 bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
849 if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
850 return bs->dev_ops->is_medium_locked(bs->dev_opaque);
852 return false;
856 * Run consistency checks on an image
858 * Returns 0 if the check could be completed (it doesn't mean that the image is
859 * free of errors) or -errno when an internal error occurred. The results of the
860 * check are stored in res.
862 int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
864 if (bs->drv->bdrv_check == NULL) {
865 return -ENOTSUP;
868 memset(res, 0, sizeof(*res));
869 return bs->drv->bdrv_check(bs, res);
872 #define COMMIT_BUF_SECTORS 2048
874 /* commit COW file into the raw image */
875 int bdrv_commit(BlockDriverState *bs)
877 BlockDriver *drv = bs->drv;
878 BlockDriver *backing_drv;
879 int64_t sector, total_sectors;
880 int n, ro, open_flags;
881 int ret = 0, rw_ret = 0;
882 uint8_t *buf;
883 char filename[1024];
884 BlockDriverState *bs_rw, *bs_ro;
886 if (!drv)
887 return -ENOMEDIUM;
889 if (!bs->backing_hd) {
890 return -ENOTSUP;
893 if (bs->backing_hd->keep_read_only) {
894 return -EACCES;
897 backing_drv = bs->backing_hd->drv;
898 ro = bs->backing_hd->read_only;
899 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
900 open_flags = bs->backing_hd->open_flags;
902 if (ro) {
903 /* re-open as RW */
904 bdrv_delete(bs->backing_hd);
905 bs->backing_hd = NULL;
906 bs_rw = bdrv_new("");
907 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR,
908 backing_drv);
909 if (rw_ret < 0) {
910 bdrv_delete(bs_rw);
911 /* try to re-open read-only */
912 bs_ro = bdrv_new("");
913 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
914 backing_drv);
915 if (ret < 0) {
916 bdrv_delete(bs_ro);
917 /* drive not functional anymore */
918 bs->drv = NULL;
919 return ret;
921 bs->backing_hd = bs_ro;
922 return rw_ret;
924 bs->backing_hd = bs_rw;
927 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
928 buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
930 for (sector = 0; sector < total_sectors; sector += n) {
931 if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
933 if (bdrv_read(bs, sector, buf, n) != 0) {
934 ret = -EIO;
935 goto ro_cleanup;
938 if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
939 ret = -EIO;
940 goto ro_cleanup;
945 if (drv->bdrv_make_empty) {
946 ret = drv->bdrv_make_empty(bs);
947 bdrv_flush(bs);
951 * Make sure all data we wrote to the backing device is actually
952 * stable on disk.
954 if (bs->backing_hd)
955 bdrv_flush(bs->backing_hd);
957 ro_cleanup:
958 g_free(buf);
960 if (ro) {
961 /* re-open as RO */
962 bdrv_delete(bs->backing_hd);
963 bs->backing_hd = NULL;
964 bs_ro = bdrv_new("");
965 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
966 backing_drv);
967 if (ret < 0) {
968 bdrv_delete(bs_ro);
969 /* drive not functional anymore */
970 bs->drv = NULL;
971 return ret;
973 bs->backing_hd = bs_ro;
974 bs->backing_hd->keep_read_only = 0;
977 return ret;
980 void bdrv_commit_all(void)
982 BlockDriverState *bs;
984 QTAILQ_FOREACH(bs, &bdrv_states, list) {
985 bdrv_commit(bs);
990 * Return values:
991 * 0 - success
992 * -EINVAL - backing format specified, but no file
993 * -ENOSPC - can't update the backing file because no space is left in the
994 * image file header
995 * -ENOTSUP - format driver doesn't support changing the backing file
997 int bdrv_change_backing_file(BlockDriverState *bs,
998 const char *backing_file, const char *backing_fmt)
1000 BlockDriver *drv = bs->drv;
1002 if (drv->bdrv_change_backing_file != NULL) {
1003 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
1004 } else {
1005 return -ENOTSUP;
1009 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
1010 size_t size)
1012 int64_t len;
1014 if (!bdrv_is_inserted(bs))
1015 return -ENOMEDIUM;
1017 if (bs->growable)
1018 return 0;
1020 len = bdrv_getlength(bs);
1022 if (offset < 0)
1023 return -EIO;
1025 if ((offset > len) || (len - offset < size))
1026 return -EIO;
1028 return 0;
1031 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
1032 int nb_sectors)
1034 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
1035 nb_sectors * BDRV_SECTOR_SIZE);
1038 static inline bool bdrv_has_async_rw(BlockDriver *drv)
1040 return drv->bdrv_co_readv != bdrv_co_readv_em
1041 || drv->bdrv_aio_readv != bdrv_aio_readv_em;
1044 static inline bool bdrv_has_async_flush(BlockDriver *drv)
1046 return drv->bdrv_aio_flush != bdrv_aio_flush_em;
1049 typedef struct RwCo {
1050 BlockDriverState *bs;
1051 int64_t sector_num;
1052 int nb_sectors;
1053 QEMUIOVector *qiov;
1054 bool is_write;
1055 int ret;
1056 } RwCo;
1058 static void coroutine_fn bdrv_rw_co_entry(void *opaque)
1060 RwCo *rwco = opaque;
1062 if (!rwco->is_write) {
1063 rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num,
1064 rwco->nb_sectors, rwco->qiov);
1065 } else {
1066 rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num,
1067 rwco->nb_sectors, rwco->qiov);
1072 * Process a synchronous request using coroutines
1074 static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf,
1075 int nb_sectors, bool is_write)
1077 QEMUIOVector qiov;
1078 struct iovec iov = {
1079 .iov_base = (void *)buf,
1080 .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1082 Coroutine *co;
1083 RwCo rwco = {
1084 .bs = bs,
1085 .sector_num = sector_num,
1086 .nb_sectors = nb_sectors,
1087 .qiov = &qiov,
1088 .is_write = is_write,
1089 .ret = NOT_DONE,
1092 qemu_iovec_init_external(&qiov, &iov, 1);
1094 if (qemu_in_coroutine()) {
1095 /* Fast-path if already in coroutine context */
1096 bdrv_rw_co_entry(&rwco);
1097 } else {
1098 co = qemu_coroutine_create(bdrv_rw_co_entry);
1099 qemu_coroutine_enter(co, &rwco);
1100 while (rwco.ret == NOT_DONE) {
1101 qemu_aio_wait();
1104 return rwco.ret;
1107 /* return < 0 if error. See bdrv_write() for the return codes */
1108 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
1109 uint8_t *buf, int nb_sectors)
1111 return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false);
1114 static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
1115 int nb_sectors, int dirty)
1117 int64_t start, end;
1118 unsigned long val, idx, bit;
1120 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
1121 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
1123 for (; start <= end; start++) {
1124 idx = start / (sizeof(unsigned long) * 8);
1125 bit = start % (sizeof(unsigned long) * 8);
1126 val = bs->dirty_bitmap[idx];
1127 if (dirty) {
1128 if (!(val & (1UL << bit))) {
1129 bs->dirty_count++;
1130 val |= 1UL << bit;
1132 } else {
1133 if (val & (1UL << bit)) {
1134 bs->dirty_count--;
1135 val &= ~(1UL << bit);
1138 bs->dirty_bitmap[idx] = val;
1142 /* Return < 0 if error. Important errors are:
1143 -EIO generic I/O error (may happen for all errors)
1144 -ENOMEDIUM No media inserted.
1145 -EINVAL Invalid sector number or nb_sectors
1146 -EACCES Trying to write a read-only device
1148 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
1149 const uint8_t *buf, int nb_sectors)
1151 return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true);
1154 int bdrv_pread(BlockDriverState *bs, int64_t offset,
1155 void *buf, int count1)
1157 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
1158 int len, nb_sectors, count;
1159 int64_t sector_num;
1160 int ret;
1162 count = count1;
1163 /* first read to align to sector start */
1164 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
1165 if (len > count)
1166 len = count;
1167 sector_num = offset >> BDRV_SECTOR_BITS;
1168 if (len > 0) {
1169 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1170 return ret;
1171 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
1172 count -= len;
1173 if (count == 0)
1174 return count1;
1175 sector_num++;
1176 buf += len;
1179 /* read the sectors "in place" */
1180 nb_sectors = count >> BDRV_SECTOR_BITS;
1181 if (nb_sectors > 0) {
1182 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
1183 return ret;
1184 sector_num += nb_sectors;
1185 len = nb_sectors << BDRV_SECTOR_BITS;
1186 buf += len;
1187 count -= len;
1190 /* add data from the last sector */
1191 if (count > 0) {
1192 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1193 return ret;
1194 memcpy(buf, tmp_buf, count);
1196 return count1;
1199 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1200 const void *buf, int count1)
1202 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
1203 int len, nb_sectors, count;
1204 int64_t sector_num;
1205 int ret;
1207 count = count1;
1208 /* first write to align to sector start */
1209 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
1210 if (len > count)
1211 len = count;
1212 sector_num = offset >> BDRV_SECTOR_BITS;
1213 if (len > 0) {
1214 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1215 return ret;
1216 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
1217 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1218 return ret;
1219 count -= len;
1220 if (count == 0)
1221 return count1;
1222 sector_num++;
1223 buf += len;
1226 /* write the sectors "in place" */
1227 nb_sectors = count >> BDRV_SECTOR_BITS;
1228 if (nb_sectors > 0) {
1229 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
1230 return ret;
1231 sector_num += nb_sectors;
1232 len = nb_sectors << BDRV_SECTOR_BITS;
1233 buf += len;
1234 count -= len;
1237 /* add data from the last sector */
1238 if (count > 0) {
1239 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1240 return ret;
1241 memcpy(tmp_buf, buf, count);
1242 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1243 return ret;
1245 return count1;
1249 * Writes to the file and ensures that no writes are reordered across this
1250 * request (acts as a barrier)
1252 * Returns 0 on success, -errno in error cases.
1254 int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1255 const void *buf, int count)
1257 int ret;
1259 ret = bdrv_pwrite(bs, offset, buf, count);
1260 if (ret < 0) {
1261 return ret;
1264 /* No flush needed for cache modes that use O_DSYNC */
1265 if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) {
1266 bdrv_flush(bs);
1269 return 0;
1273 * Handle a read request in coroutine context
1275 static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
1276 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
1278 BlockDriver *drv = bs->drv;
1280 if (!drv) {
1281 return -ENOMEDIUM;
1283 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1284 return -EIO;
1287 return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
1290 int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
1291 int nb_sectors, QEMUIOVector *qiov)
1293 trace_bdrv_co_readv(bs, sector_num, nb_sectors);
1295 return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov);
1299 * Handle a write request in coroutine context
1301 static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
1302 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
1304 BlockDriver *drv = bs->drv;
1306 if (!bs->drv) {
1307 return -ENOMEDIUM;
1309 if (bs->read_only) {
1310 return -EACCES;
1312 if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1313 return -EIO;
1316 if (bs->dirty_bitmap) {
1317 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1320 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1321 bs->wr_highest_sector = sector_num + nb_sectors - 1;
1324 return drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
1327 int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
1328 int nb_sectors, QEMUIOVector *qiov)
1330 trace_bdrv_co_writev(bs, sector_num, nb_sectors);
1332 return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov);
1336 * Truncate file to 'offset' bytes (needed only for file protocols)
1338 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1340 BlockDriver *drv = bs->drv;
1341 int ret;
1342 if (!drv)
1343 return -ENOMEDIUM;
1344 if (!drv->bdrv_truncate)
1345 return -ENOTSUP;
1346 if (bs->read_only)
1347 return -EACCES;
1348 if (bdrv_in_use(bs))
1349 return -EBUSY;
1350 ret = drv->bdrv_truncate(bs, offset);
1351 if (ret == 0) {
1352 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
1353 bdrv_dev_resize_cb(bs);
1355 return ret;
1359 * Length of a allocated file in bytes. Sparse files are counted by actual
1360 * allocated space. Return < 0 if error or unknown.
1362 int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
1364 BlockDriver *drv = bs->drv;
1365 if (!drv) {
1366 return -ENOMEDIUM;
1368 if (drv->bdrv_get_allocated_file_size) {
1369 return drv->bdrv_get_allocated_file_size(bs);
1371 if (bs->file) {
1372 return bdrv_get_allocated_file_size(bs->file);
1374 return -ENOTSUP;
1378 * Length of a file in bytes. Return < 0 if error or unknown.
1380 int64_t bdrv_getlength(BlockDriverState *bs)
1382 BlockDriver *drv = bs->drv;
1383 if (!drv)
1384 return -ENOMEDIUM;
1386 if (bs->growable || bdrv_dev_has_removable_media(bs)) {
1387 if (drv->bdrv_getlength) {
1388 return drv->bdrv_getlength(bs);
1391 return bs->total_sectors * BDRV_SECTOR_SIZE;
1394 /* return 0 as number of sectors if no device present or error */
1395 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
1397 int64_t length;
1398 length = bdrv_getlength(bs);
1399 if (length < 0)
1400 length = 0;
1401 else
1402 length = length >> BDRV_SECTOR_BITS;
1403 *nb_sectors_ptr = length;
1406 struct partition {
1407 uint8_t boot_ind; /* 0x80 - active */
1408 uint8_t head; /* starting head */
1409 uint8_t sector; /* starting sector */
1410 uint8_t cyl; /* starting cylinder */
1411 uint8_t sys_ind; /* What partition type */
1412 uint8_t end_head; /* end head */
1413 uint8_t end_sector; /* end sector */
1414 uint8_t end_cyl; /* end cylinder */
1415 uint32_t start_sect; /* starting sector counting from 0 */
1416 uint32_t nr_sects; /* nr of sectors in partition */
1417 } QEMU_PACKED;
1419 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1420 static int guess_disk_lchs(BlockDriverState *bs,
1421 int *pcylinders, int *pheads, int *psectors)
1423 uint8_t buf[BDRV_SECTOR_SIZE];
1424 int ret, i, heads, sectors, cylinders;
1425 struct partition *p;
1426 uint32_t nr_sects;
1427 uint64_t nb_sectors;
1429 bdrv_get_geometry(bs, &nb_sectors);
1431 ret = bdrv_read(bs, 0, buf, 1);
1432 if (ret < 0)
1433 return -1;
1434 /* test msdos magic */
1435 if (buf[510] != 0x55 || buf[511] != 0xaa)
1436 return -1;
1437 for(i = 0; i < 4; i++) {
1438 p = ((struct partition *)(buf + 0x1be)) + i;
1439 nr_sects = le32_to_cpu(p->nr_sects);
1440 if (nr_sects && p->end_head) {
1441 /* We make the assumption that the partition terminates on
1442 a cylinder boundary */
1443 heads = p->end_head + 1;
1444 sectors = p->end_sector & 63;
1445 if (sectors == 0)
1446 continue;
1447 cylinders = nb_sectors / (heads * sectors);
1448 if (cylinders < 1 || cylinders > 16383)
1449 continue;
1450 *pheads = heads;
1451 *psectors = sectors;
1452 *pcylinders = cylinders;
1453 #if 0
1454 printf("guessed geometry: LCHS=%d %d %d\n",
1455 cylinders, heads, sectors);
1456 #endif
1457 return 0;
1460 return -1;
1463 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1465 int translation, lba_detected = 0;
1466 int cylinders, heads, secs;
1467 uint64_t nb_sectors;
1469 /* if a geometry hint is available, use it */
1470 bdrv_get_geometry(bs, &nb_sectors);
1471 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1472 translation = bdrv_get_translation_hint(bs);
1473 if (cylinders != 0) {
1474 *pcyls = cylinders;
1475 *pheads = heads;
1476 *psecs = secs;
1477 } else {
1478 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1479 if (heads > 16) {
1480 /* if heads > 16, it means that a BIOS LBA
1481 translation was active, so the default
1482 hardware geometry is OK */
1483 lba_detected = 1;
1484 goto default_geometry;
1485 } else {
1486 *pcyls = cylinders;
1487 *pheads = heads;
1488 *psecs = secs;
1489 /* disable any translation to be in sync with
1490 the logical geometry */
1491 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1492 bdrv_set_translation_hint(bs,
1493 BIOS_ATA_TRANSLATION_NONE);
1496 } else {
1497 default_geometry:
1498 /* if no geometry, use a standard physical disk geometry */
1499 cylinders = nb_sectors / (16 * 63);
1501 if (cylinders > 16383)
1502 cylinders = 16383;
1503 else if (cylinders < 2)
1504 cylinders = 2;
1505 *pcyls = cylinders;
1506 *pheads = 16;
1507 *psecs = 63;
1508 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1509 if ((*pcyls * *pheads) <= 131072) {
1510 bdrv_set_translation_hint(bs,
1511 BIOS_ATA_TRANSLATION_LARGE);
1512 } else {
1513 bdrv_set_translation_hint(bs,
1514 BIOS_ATA_TRANSLATION_LBA);
1518 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1522 void bdrv_set_geometry_hint(BlockDriverState *bs,
1523 int cyls, int heads, int secs)
1525 bs->cyls = cyls;
1526 bs->heads = heads;
1527 bs->secs = secs;
1530 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1532 bs->translation = translation;
1535 void bdrv_get_geometry_hint(BlockDriverState *bs,
1536 int *pcyls, int *pheads, int *psecs)
1538 *pcyls = bs->cyls;
1539 *pheads = bs->heads;
1540 *psecs = bs->secs;
1543 /* Recognize floppy formats */
1544 typedef struct FDFormat {
1545 FDriveType drive;
1546 uint8_t last_sect;
1547 uint8_t max_track;
1548 uint8_t max_head;
1549 } FDFormat;
1551 static const FDFormat fd_formats[] = {
1552 /* First entry is default format */
1553 /* 1.44 MB 3"1/2 floppy disks */
1554 { FDRIVE_DRV_144, 18, 80, 1, },
1555 { FDRIVE_DRV_144, 20, 80, 1, },
1556 { FDRIVE_DRV_144, 21, 80, 1, },
1557 { FDRIVE_DRV_144, 21, 82, 1, },
1558 { FDRIVE_DRV_144, 21, 83, 1, },
1559 { FDRIVE_DRV_144, 22, 80, 1, },
1560 { FDRIVE_DRV_144, 23, 80, 1, },
1561 { FDRIVE_DRV_144, 24, 80, 1, },
1562 /* 2.88 MB 3"1/2 floppy disks */
1563 { FDRIVE_DRV_288, 36, 80, 1, },
1564 { FDRIVE_DRV_288, 39, 80, 1, },
1565 { FDRIVE_DRV_288, 40, 80, 1, },
1566 { FDRIVE_DRV_288, 44, 80, 1, },
1567 { FDRIVE_DRV_288, 48, 80, 1, },
1568 /* 720 kB 3"1/2 floppy disks */
1569 { FDRIVE_DRV_144, 9, 80, 1, },
1570 { FDRIVE_DRV_144, 10, 80, 1, },
1571 { FDRIVE_DRV_144, 10, 82, 1, },
1572 { FDRIVE_DRV_144, 10, 83, 1, },
1573 { FDRIVE_DRV_144, 13, 80, 1, },
1574 { FDRIVE_DRV_144, 14, 80, 1, },
1575 /* 1.2 MB 5"1/4 floppy disks */
1576 { FDRIVE_DRV_120, 15, 80, 1, },
1577 { FDRIVE_DRV_120, 18, 80, 1, },
1578 { FDRIVE_DRV_120, 18, 82, 1, },
1579 { FDRIVE_DRV_120, 18, 83, 1, },
1580 { FDRIVE_DRV_120, 20, 80, 1, },
1581 /* 720 kB 5"1/4 floppy disks */
1582 { FDRIVE_DRV_120, 9, 80, 1, },
1583 { FDRIVE_DRV_120, 11, 80, 1, },
1584 /* 360 kB 5"1/4 floppy disks */
1585 { FDRIVE_DRV_120, 9, 40, 1, },
1586 { FDRIVE_DRV_120, 9, 40, 0, },
1587 { FDRIVE_DRV_120, 10, 41, 1, },
1588 { FDRIVE_DRV_120, 10, 42, 1, },
1589 /* 320 kB 5"1/4 floppy disks */
1590 { FDRIVE_DRV_120, 8, 40, 1, },
1591 { FDRIVE_DRV_120, 8, 40, 0, },
1592 /* 360 kB must match 5"1/4 better than 3"1/2... */
1593 { FDRIVE_DRV_144, 9, 80, 0, },
1594 /* end */
1595 { FDRIVE_DRV_NONE, -1, -1, 0, },
1598 void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads,
1599 int *max_track, int *last_sect,
1600 FDriveType drive_in, FDriveType *drive)
1602 const FDFormat *parse;
1603 uint64_t nb_sectors, size;
1604 int i, first_match, match;
1606 bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect);
1607 if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) {
1608 /* User defined disk */
1609 } else {
1610 bdrv_get_geometry(bs, &nb_sectors);
1611 match = -1;
1612 first_match = -1;
1613 for (i = 0; ; i++) {
1614 parse = &fd_formats[i];
1615 if (parse->drive == FDRIVE_DRV_NONE) {
1616 break;
1618 if (drive_in == parse->drive ||
1619 drive_in == FDRIVE_DRV_NONE) {
1620 size = (parse->max_head + 1) * parse->max_track *
1621 parse->last_sect;
1622 if (nb_sectors == size) {
1623 match = i;
1624 break;
1626 if (first_match == -1) {
1627 first_match = i;
1631 if (match == -1) {
1632 if (first_match == -1) {
1633 match = 1;
1634 } else {
1635 match = first_match;
1637 parse = &fd_formats[match];
1639 *nb_heads = parse->max_head + 1;
1640 *max_track = parse->max_track;
1641 *last_sect = parse->last_sect;
1642 *drive = parse->drive;
1646 int bdrv_get_translation_hint(BlockDriverState *bs)
1648 return bs->translation;
1651 void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1652 BlockErrorAction on_write_error)
1654 bs->on_read_error = on_read_error;
1655 bs->on_write_error = on_write_error;
1658 BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1660 return is_read ? bs->on_read_error : bs->on_write_error;
1663 int bdrv_is_read_only(BlockDriverState *bs)
1665 return bs->read_only;
1668 int bdrv_is_sg(BlockDriverState *bs)
1670 return bs->sg;
1673 int bdrv_enable_write_cache(BlockDriverState *bs)
1675 return bs->enable_write_cache;
1678 int bdrv_is_encrypted(BlockDriverState *bs)
1680 if (bs->backing_hd && bs->backing_hd->encrypted)
1681 return 1;
1682 return bs->encrypted;
1685 int bdrv_key_required(BlockDriverState *bs)
1687 BlockDriverState *backing_hd = bs->backing_hd;
1689 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1690 return 1;
1691 return (bs->encrypted && !bs->valid_key);
1694 int bdrv_set_key(BlockDriverState *bs, const char *key)
1696 int ret;
1697 if (bs->backing_hd && bs->backing_hd->encrypted) {
1698 ret = bdrv_set_key(bs->backing_hd, key);
1699 if (ret < 0)
1700 return ret;
1701 if (!bs->encrypted)
1702 return 0;
1704 if (!bs->encrypted) {
1705 return -EINVAL;
1706 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1707 return -ENOMEDIUM;
1709 ret = bs->drv->bdrv_set_key(bs, key);
1710 if (ret < 0) {
1711 bs->valid_key = 0;
1712 } else if (!bs->valid_key) {
1713 bs->valid_key = 1;
1714 /* call the change callback now, we skipped it on open */
1715 bdrv_dev_change_media_cb(bs, true);
1717 return ret;
1720 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1722 if (!bs->drv) {
1723 buf[0] = '\0';
1724 } else {
1725 pstrcpy(buf, buf_size, bs->drv->format_name);
1729 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1730 void *opaque)
1732 BlockDriver *drv;
1734 QLIST_FOREACH(drv, &bdrv_drivers, list) {
1735 it(opaque, drv->format_name);
1739 BlockDriverState *bdrv_find(const char *name)
1741 BlockDriverState *bs;
1743 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1744 if (!strcmp(name, bs->device_name)) {
1745 return bs;
1748 return NULL;
1751 BlockDriverState *bdrv_next(BlockDriverState *bs)
1753 if (!bs) {
1754 return QTAILQ_FIRST(&bdrv_states);
1756 return QTAILQ_NEXT(bs, list);
1759 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1761 BlockDriverState *bs;
1763 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1764 it(opaque, bs);
1768 const char *bdrv_get_device_name(BlockDriverState *bs)
1770 return bs->device_name;
1773 int bdrv_flush(BlockDriverState *bs)
1775 if (bs->open_flags & BDRV_O_NO_FLUSH) {
1776 return 0;
1779 if (bs->drv && bdrv_has_async_flush(bs->drv) && qemu_in_coroutine()) {
1780 return bdrv_co_flush_em(bs);
1783 if (bs->drv && bs->drv->bdrv_flush) {
1784 return bs->drv->bdrv_flush(bs);
1788 * Some block drivers always operate in either writethrough or unsafe mode
1789 * and don't support bdrv_flush therefore. Usually qemu doesn't know how
1790 * the server works (because the behaviour is hardcoded or depends on
1791 * server-side configuration), so we can't ensure that everything is safe
1792 * on disk. Returning an error doesn't work because that would break guests
1793 * even if the server operates in writethrough mode.
1795 * Let's hope the user knows what he's doing.
1797 return 0;
1800 void bdrv_flush_all(void)
1802 BlockDriverState *bs;
1804 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1805 if (!bdrv_is_read_only(bs) && bdrv_is_inserted(bs)) {
1806 bdrv_flush(bs);
1811 int bdrv_has_zero_init(BlockDriverState *bs)
1813 assert(bs->drv);
1815 if (bs->drv->bdrv_has_zero_init) {
1816 return bs->drv->bdrv_has_zero_init(bs);
1819 return 1;
1822 int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
1824 if (!bs->drv) {
1825 return -ENOMEDIUM;
1827 if (!bs->drv->bdrv_discard) {
1828 return 0;
1830 return bs->drv->bdrv_discard(bs, sector_num, nb_sectors);
1834 * Returns true iff the specified sector is present in the disk image. Drivers
1835 * not implementing the functionality are assumed to not support backing files,
1836 * hence all their sectors are reported as allocated.
1838 * 'pnum' is set to the number of sectors (including and immediately following
1839 * the specified sector) that are known to be in the same
1840 * allocated/unallocated state.
1842 * 'nb_sectors' is the max value 'pnum' should be set to.
1844 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1845 int *pnum)
1847 int64_t n;
1848 if (!bs->drv->bdrv_is_allocated) {
1849 if (sector_num >= bs->total_sectors) {
1850 *pnum = 0;
1851 return 0;
1853 n = bs->total_sectors - sector_num;
1854 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1855 return 1;
1857 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1860 void bdrv_mon_event(const BlockDriverState *bdrv,
1861 BlockMonEventAction action, int is_read)
1863 QObject *data;
1864 const char *action_str;
1866 switch (action) {
1867 case BDRV_ACTION_REPORT:
1868 action_str = "report";
1869 break;
1870 case BDRV_ACTION_IGNORE:
1871 action_str = "ignore";
1872 break;
1873 case BDRV_ACTION_STOP:
1874 action_str = "stop";
1875 break;
1876 default:
1877 abort();
1880 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1881 bdrv->device_name,
1882 action_str,
1883 is_read ? "read" : "write");
1884 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1886 qobject_decref(data);
1889 static void bdrv_print_dict(QObject *obj, void *opaque)
1891 QDict *bs_dict;
1892 Monitor *mon = opaque;
1894 bs_dict = qobject_to_qdict(obj);
1896 monitor_printf(mon, "%s: removable=%d",
1897 qdict_get_str(bs_dict, "device"),
1898 qdict_get_bool(bs_dict, "removable"));
1900 if (qdict_get_bool(bs_dict, "removable")) {
1901 monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1902 monitor_printf(mon, " tray-open=%d",
1903 qdict_get_bool(bs_dict, "tray-open"));
1906 if (qdict_haskey(bs_dict, "io-status")) {
1907 monitor_printf(mon, " io-status=%s", qdict_get_str(bs_dict, "io-status"));
1910 if (qdict_haskey(bs_dict, "inserted")) {
1911 QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1913 monitor_printf(mon, " file=");
1914 monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1915 if (qdict_haskey(qdict, "backing_file")) {
1916 monitor_printf(mon, " backing_file=");
1917 monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1919 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1920 qdict_get_bool(qdict, "ro"),
1921 qdict_get_str(qdict, "drv"),
1922 qdict_get_bool(qdict, "encrypted"));
1923 } else {
1924 monitor_printf(mon, " [not inserted]");
1927 monitor_printf(mon, "\n");
1930 void bdrv_info_print(Monitor *mon, const QObject *data)
1932 qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1935 static const char *const io_status_name[BDRV_IOS_MAX] = {
1936 [BDRV_IOS_OK] = "ok",
1937 [BDRV_IOS_FAILED] = "failed",
1938 [BDRV_IOS_ENOSPC] = "nospace",
1941 void bdrv_info(Monitor *mon, QObject **ret_data)
1943 QList *bs_list;
1944 BlockDriverState *bs;
1946 bs_list = qlist_new();
1948 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1949 QObject *bs_obj;
1950 QDict *bs_dict;
1952 bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
1953 "'removable': %i, 'locked': %i }",
1954 bs->device_name,
1955 bdrv_dev_has_removable_media(bs),
1956 bdrv_dev_is_medium_locked(bs));
1957 bs_dict = qobject_to_qdict(bs_obj);
1959 if (bdrv_dev_has_removable_media(bs)) {
1960 qdict_put(bs_dict, "tray-open",
1961 qbool_from_int(bdrv_dev_is_tray_open(bs)));
1964 if (bdrv_iostatus_is_enabled(bs)) {
1965 qdict_put(bs_dict, "io-status",
1966 qstring_from_str(io_status_name[bs->iostatus]));
1969 if (bs->drv) {
1970 QObject *obj;
1972 obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1973 "'encrypted': %i }",
1974 bs->filename, bs->read_only,
1975 bs->drv->format_name,
1976 bdrv_is_encrypted(bs));
1977 if (bs->backing_file[0] != '\0') {
1978 QDict *qdict = qobject_to_qdict(obj);
1979 qdict_put(qdict, "backing_file",
1980 qstring_from_str(bs->backing_file));
1983 qdict_put_obj(bs_dict, "inserted", obj);
1985 qlist_append_obj(bs_list, bs_obj);
1988 *ret_data = QOBJECT(bs_list);
1991 static void bdrv_stats_iter(QObject *data, void *opaque)
1993 QDict *qdict;
1994 Monitor *mon = opaque;
1996 qdict = qobject_to_qdict(data);
1997 monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1999 qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
2000 monitor_printf(mon, " rd_bytes=%" PRId64
2001 " wr_bytes=%" PRId64
2002 " rd_operations=%" PRId64
2003 " wr_operations=%" PRId64
2004 " flush_operations=%" PRId64
2005 " wr_total_time_ns=%" PRId64
2006 " rd_total_time_ns=%" PRId64
2007 " flush_total_time_ns=%" PRId64
2008 "\n",
2009 qdict_get_int(qdict, "rd_bytes"),
2010 qdict_get_int(qdict, "wr_bytes"),
2011 qdict_get_int(qdict, "rd_operations"),
2012 qdict_get_int(qdict, "wr_operations"),
2013 qdict_get_int(qdict, "flush_operations"),
2014 qdict_get_int(qdict, "wr_total_time_ns"),
2015 qdict_get_int(qdict, "rd_total_time_ns"),
2016 qdict_get_int(qdict, "flush_total_time_ns"));
2019 void bdrv_stats_print(Monitor *mon, const QObject *data)
2021 qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
2024 static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
2026 QObject *res;
2027 QDict *dict;
2029 res = qobject_from_jsonf("{ 'stats': {"
2030 "'rd_bytes': %" PRId64 ","
2031 "'wr_bytes': %" PRId64 ","
2032 "'rd_operations': %" PRId64 ","
2033 "'wr_operations': %" PRId64 ","
2034 "'wr_highest_offset': %" PRId64 ","
2035 "'flush_operations': %" PRId64 ","
2036 "'wr_total_time_ns': %" PRId64 ","
2037 "'rd_total_time_ns': %" PRId64 ","
2038 "'flush_total_time_ns': %" PRId64
2039 "} }",
2040 bs->nr_bytes[BDRV_ACCT_READ],
2041 bs->nr_bytes[BDRV_ACCT_WRITE],
2042 bs->nr_ops[BDRV_ACCT_READ],
2043 bs->nr_ops[BDRV_ACCT_WRITE],
2044 bs->wr_highest_sector *
2045 (uint64_t)BDRV_SECTOR_SIZE,
2046 bs->nr_ops[BDRV_ACCT_FLUSH],
2047 bs->total_time_ns[BDRV_ACCT_WRITE],
2048 bs->total_time_ns[BDRV_ACCT_READ],
2049 bs->total_time_ns[BDRV_ACCT_FLUSH]);
2050 dict = qobject_to_qdict(res);
2052 if (*bs->device_name) {
2053 qdict_put(dict, "device", qstring_from_str(bs->device_name));
2056 if (bs->file) {
2057 QObject *parent = bdrv_info_stats_bs(bs->file);
2058 qdict_put_obj(dict, "parent", parent);
2061 return res;
2064 void bdrv_info_stats(Monitor *mon, QObject **ret_data)
2066 QObject *obj;
2067 QList *devices;
2068 BlockDriverState *bs;
2070 devices = qlist_new();
2072 QTAILQ_FOREACH(bs, &bdrv_states, list) {
2073 obj = bdrv_info_stats_bs(bs);
2074 qlist_append_obj(devices, obj);
2077 *ret_data = QOBJECT(devices);
2080 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
2082 if (bs->backing_hd && bs->backing_hd->encrypted)
2083 return bs->backing_file;
2084 else if (bs->encrypted)
2085 return bs->filename;
2086 else
2087 return NULL;
2090 void bdrv_get_backing_filename(BlockDriverState *bs,
2091 char *filename, int filename_size)
2093 if (!bs->backing_file) {
2094 pstrcpy(filename, filename_size, "");
2095 } else {
2096 pstrcpy(filename, filename_size, bs->backing_file);
2100 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
2101 const uint8_t *buf, int nb_sectors)
2103 BlockDriver *drv = bs->drv;
2104 if (!drv)
2105 return -ENOMEDIUM;
2106 if (!drv->bdrv_write_compressed)
2107 return -ENOTSUP;
2108 if (bdrv_check_request(bs, sector_num, nb_sectors))
2109 return -EIO;
2111 if (bs->dirty_bitmap) {
2112 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
2115 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
2118 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2120 BlockDriver *drv = bs->drv;
2121 if (!drv)
2122 return -ENOMEDIUM;
2123 if (!drv->bdrv_get_info)
2124 return -ENOTSUP;
2125 memset(bdi, 0, sizeof(*bdi));
2126 return drv->bdrv_get_info(bs, bdi);
2129 int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
2130 int64_t pos, int size)
2132 BlockDriver *drv = bs->drv;
2133 if (!drv)
2134 return -ENOMEDIUM;
2135 if (drv->bdrv_save_vmstate)
2136 return drv->bdrv_save_vmstate(bs, buf, pos, size);
2137 if (bs->file)
2138 return bdrv_save_vmstate(bs->file, buf, pos, size);
2139 return -ENOTSUP;
2142 int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2143 int64_t pos, int size)
2145 BlockDriver *drv = bs->drv;
2146 if (!drv)
2147 return -ENOMEDIUM;
2148 if (drv->bdrv_load_vmstate)
2149 return drv->bdrv_load_vmstate(bs, buf, pos, size);
2150 if (bs->file)
2151 return bdrv_load_vmstate(bs->file, buf, pos, size);
2152 return -ENOTSUP;
2155 void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
2157 BlockDriver *drv = bs->drv;
2159 if (!drv || !drv->bdrv_debug_event) {
2160 return;
2163 return drv->bdrv_debug_event(bs, event);
2167 /**************************************************************/
2168 /* handling of snapshots */
2170 int bdrv_can_snapshot(BlockDriverState *bs)
2172 BlockDriver *drv = bs->drv;
2173 if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2174 return 0;
2177 if (!drv->bdrv_snapshot_create) {
2178 if (bs->file != NULL) {
2179 return bdrv_can_snapshot(bs->file);
2181 return 0;
2184 return 1;
2187 int bdrv_is_snapshot(BlockDriverState *bs)
2189 return !!(bs->open_flags & BDRV_O_SNAPSHOT);
2192 BlockDriverState *bdrv_snapshots(void)
2194 BlockDriverState *bs;
2196 if (bs_snapshots) {
2197 return bs_snapshots;
2200 bs = NULL;
2201 while ((bs = bdrv_next(bs))) {
2202 if (bdrv_can_snapshot(bs)) {
2203 bs_snapshots = bs;
2204 return bs;
2207 return NULL;
2210 int bdrv_snapshot_create(BlockDriverState *bs,
2211 QEMUSnapshotInfo *sn_info)
2213 BlockDriver *drv = bs->drv;
2214 if (!drv)
2215 return -ENOMEDIUM;
2216 if (drv->bdrv_snapshot_create)
2217 return drv->bdrv_snapshot_create(bs, sn_info);
2218 if (bs->file)
2219 return bdrv_snapshot_create(bs->file, sn_info);
2220 return -ENOTSUP;
2223 int bdrv_snapshot_goto(BlockDriverState *bs,
2224 const char *snapshot_id)
2226 BlockDriver *drv = bs->drv;
2227 int ret, open_ret;
2229 if (!drv)
2230 return -ENOMEDIUM;
2231 if (drv->bdrv_snapshot_goto)
2232 return drv->bdrv_snapshot_goto(bs, snapshot_id);
2234 if (bs->file) {
2235 drv->bdrv_close(bs);
2236 ret = bdrv_snapshot_goto(bs->file, snapshot_id);
2237 open_ret = drv->bdrv_open(bs, bs->open_flags);
2238 if (open_ret < 0) {
2239 bdrv_delete(bs->file);
2240 bs->drv = NULL;
2241 return open_ret;
2243 return ret;
2246 return -ENOTSUP;
2249 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
2251 BlockDriver *drv = bs->drv;
2252 if (!drv)
2253 return -ENOMEDIUM;
2254 if (drv->bdrv_snapshot_delete)
2255 return drv->bdrv_snapshot_delete(bs, snapshot_id);
2256 if (bs->file)
2257 return bdrv_snapshot_delete(bs->file, snapshot_id);
2258 return -ENOTSUP;
2261 int bdrv_snapshot_list(BlockDriverState *bs,
2262 QEMUSnapshotInfo **psn_info)
2264 BlockDriver *drv = bs->drv;
2265 if (!drv)
2266 return -ENOMEDIUM;
2267 if (drv->bdrv_snapshot_list)
2268 return drv->bdrv_snapshot_list(bs, psn_info);
2269 if (bs->file)
2270 return bdrv_snapshot_list(bs->file, psn_info);
2271 return -ENOTSUP;
2274 int bdrv_snapshot_load_tmp(BlockDriverState *bs,
2275 const char *snapshot_name)
2277 BlockDriver *drv = bs->drv;
2278 if (!drv) {
2279 return -ENOMEDIUM;
2281 if (!bs->read_only) {
2282 return -EINVAL;
2284 if (drv->bdrv_snapshot_load_tmp) {
2285 return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
2287 return -ENOTSUP;
2290 #define NB_SUFFIXES 4
2292 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
2294 static const char suffixes[NB_SUFFIXES] = "KMGT";
2295 int64_t base;
2296 int i;
2298 if (size <= 999) {
2299 snprintf(buf, buf_size, "%" PRId64, size);
2300 } else {
2301 base = 1024;
2302 for(i = 0; i < NB_SUFFIXES; i++) {
2303 if (size < (10 * base)) {
2304 snprintf(buf, buf_size, "%0.1f%c",
2305 (double)size / base,
2306 suffixes[i]);
2307 break;
2308 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
2309 snprintf(buf, buf_size, "%" PRId64 "%c",
2310 ((size + (base >> 1)) / base),
2311 suffixes[i]);
2312 break;
2314 base = base * 1024;
2317 return buf;
2320 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
2322 char buf1[128], date_buf[128], clock_buf[128];
2323 #ifdef _WIN32
2324 struct tm *ptm;
2325 #else
2326 struct tm tm;
2327 #endif
2328 time_t ti;
2329 int64_t secs;
2331 if (!sn) {
2332 snprintf(buf, buf_size,
2333 "%-10s%-20s%7s%20s%15s",
2334 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
2335 } else {
2336 ti = sn->date_sec;
2337 #ifdef _WIN32
2338 ptm = localtime(&ti);
2339 strftime(date_buf, sizeof(date_buf),
2340 "%Y-%m-%d %H:%M:%S", ptm);
2341 #else
2342 localtime_r(&ti, &tm);
2343 strftime(date_buf, sizeof(date_buf),
2344 "%Y-%m-%d %H:%M:%S", &tm);
2345 #endif
2346 secs = sn->vm_clock_nsec / 1000000000;
2347 snprintf(clock_buf, sizeof(clock_buf),
2348 "%02d:%02d:%02d.%03d",
2349 (int)(secs / 3600),
2350 (int)((secs / 60) % 60),
2351 (int)(secs % 60),
2352 (int)((sn->vm_clock_nsec / 1000000) % 1000));
2353 snprintf(buf, buf_size,
2354 "%-10s%-20s%7s%20s%15s",
2355 sn->id_str, sn->name,
2356 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
2357 date_buf,
2358 clock_buf);
2360 return buf;
2363 /**************************************************************/
2364 /* async I/Os */
2366 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
2367 QEMUIOVector *qiov, int nb_sectors,
2368 BlockDriverCompletionFunc *cb, void *opaque)
2370 BlockDriver *drv = bs->drv;
2372 trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
2374 if (!drv)
2375 return NULL;
2376 if (bdrv_check_request(bs, sector_num, nb_sectors))
2377 return NULL;
2379 return drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
2380 cb, opaque);
2383 typedef struct BlockCompleteData {
2384 BlockDriverCompletionFunc *cb;
2385 void *opaque;
2386 BlockDriverState *bs;
2387 int64_t sector_num;
2388 int nb_sectors;
2389 } BlockCompleteData;
2391 static void block_complete_cb(void *opaque, int ret)
2393 BlockCompleteData *b = opaque;
2395 if (b->bs->dirty_bitmap) {
2396 set_dirty_bitmap(b->bs, b->sector_num, b->nb_sectors, 1);
2398 b->cb(b->opaque, ret);
2399 g_free(b);
2402 static BlockCompleteData *blk_dirty_cb_alloc(BlockDriverState *bs,
2403 int64_t sector_num,
2404 int nb_sectors,
2405 BlockDriverCompletionFunc *cb,
2406 void *opaque)
2408 BlockCompleteData *blkdata = g_malloc0(sizeof(BlockCompleteData));
2410 blkdata->bs = bs;
2411 blkdata->cb = cb;
2412 blkdata->opaque = opaque;
2413 blkdata->sector_num = sector_num;
2414 blkdata->nb_sectors = nb_sectors;
2416 return blkdata;
2419 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
2420 QEMUIOVector *qiov, int nb_sectors,
2421 BlockDriverCompletionFunc *cb, void *opaque)
2423 BlockDriver *drv = bs->drv;
2424 BlockDriverAIOCB *ret;
2425 BlockCompleteData *blk_cb_data;
2427 trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
2429 if (!drv)
2430 return NULL;
2431 if (bs->read_only)
2432 return NULL;
2433 if (bdrv_check_request(bs, sector_num, nb_sectors))
2434 return NULL;
2436 if (bs->dirty_bitmap) {
2437 blk_cb_data = blk_dirty_cb_alloc(bs, sector_num, nb_sectors, cb,
2438 opaque);
2439 cb = &block_complete_cb;
2440 opaque = blk_cb_data;
2443 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
2444 cb, opaque);
2446 if (ret) {
2447 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2448 bs->wr_highest_sector = sector_num + nb_sectors - 1;
2452 return ret;
2456 typedef struct MultiwriteCB {
2457 int error;
2458 int num_requests;
2459 int num_callbacks;
2460 struct {
2461 BlockDriverCompletionFunc *cb;
2462 void *opaque;
2463 QEMUIOVector *free_qiov;
2464 void *free_buf;
2465 } callbacks[];
2466 } MultiwriteCB;
2468 static void multiwrite_user_cb(MultiwriteCB *mcb)
2470 int i;
2472 for (i = 0; i < mcb->num_callbacks; i++) {
2473 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
2474 if (mcb->callbacks[i].free_qiov) {
2475 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
2477 g_free(mcb->callbacks[i].free_qiov);
2478 qemu_vfree(mcb->callbacks[i].free_buf);
2482 static void multiwrite_cb(void *opaque, int ret)
2484 MultiwriteCB *mcb = opaque;
2486 trace_multiwrite_cb(mcb, ret);
2488 if (ret < 0 && !mcb->error) {
2489 mcb->error = ret;
2492 mcb->num_requests--;
2493 if (mcb->num_requests == 0) {
2494 multiwrite_user_cb(mcb);
2495 g_free(mcb);
2499 static int multiwrite_req_compare(const void *a, const void *b)
2501 const BlockRequest *req1 = a, *req2 = b;
2504 * Note that we can't simply subtract req2->sector from req1->sector
2505 * here as that could overflow the return value.
2507 if (req1->sector > req2->sector) {
2508 return 1;
2509 } else if (req1->sector < req2->sector) {
2510 return -1;
2511 } else {
2512 return 0;
2517 * Takes a bunch of requests and tries to merge them. Returns the number of
2518 * requests that remain after merging.
2520 static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
2521 int num_reqs, MultiwriteCB *mcb)
2523 int i, outidx;
2525 // Sort requests by start sector
2526 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
2528 // Check if adjacent requests touch the same clusters. If so, combine them,
2529 // filling up gaps with zero sectors.
2530 outidx = 0;
2531 for (i = 1; i < num_reqs; i++) {
2532 int merge = 0;
2533 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
2535 // This handles the cases that are valid for all block drivers, namely
2536 // exactly sequential writes and overlapping writes.
2537 if (reqs[i].sector <= oldreq_last) {
2538 merge = 1;
2541 // The block driver may decide that it makes sense to combine requests
2542 // even if there is a gap of some sectors between them. In this case,
2543 // the gap is filled with zeros (therefore only applicable for yet
2544 // unused space in format like qcow2).
2545 if (!merge && bs->drv->bdrv_merge_requests) {
2546 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
2549 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2550 merge = 0;
2553 if (merge) {
2554 size_t size;
2555 QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
2556 qemu_iovec_init(qiov,
2557 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2559 // Add the first request to the merged one. If the requests are
2560 // overlapping, drop the last sectors of the first request.
2561 size = (reqs[i].sector - reqs[outidx].sector) << 9;
2562 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2564 // We might need to add some zeros between the two requests
2565 if (reqs[i].sector > oldreq_last) {
2566 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2567 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2568 memset(buf, 0, zero_bytes);
2569 qemu_iovec_add(qiov, buf, zero_bytes);
2570 mcb->callbacks[i].free_buf = buf;
2573 // Add the second request
2574 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2576 reqs[outidx].nb_sectors = qiov->size >> 9;
2577 reqs[outidx].qiov = qiov;
2579 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2580 } else {
2581 outidx++;
2582 reqs[outidx].sector = reqs[i].sector;
2583 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2584 reqs[outidx].qiov = reqs[i].qiov;
2588 return outidx + 1;
2592 * Submit multiple AIO write requests at once.
2594 * On success, the function returns 0 and all requests in the reqs array have
2595 * been submitted. In error case this function returns -1, and any of the
2596 * requests may or may not be submitted yet. In particular, this means that the
2597 * callback will be called for some of the requests, for others it won't. The
2598 * caller must check the error field of the BlockRequest to wait for the right
2599 * callbacks (if error != 0, no callback will be called).
2601 * The implementation may modify the contents of the reqs array, e.g. to merge
2602 * requests. However, the fields opaque and error are left unmodified as they
2603 * are used to signal failure for a single request to the caller.
2605 int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2607 BlockDriverAIOCB *acb;
2608 MultiwriteCB *mcb;
2609 int i;
2611 /* don't submit writes if we don't have a medium */
2612 if (bs->drv == NULL) {
2613 for (i = 0; i < num_reqs; i++) {
2614 reqs[i].error = -ENOMEDIUM;
2616 return -1;
2619 if (num_reqs == 0) {
2620 return 0;
2623 // Create MultiwriteCB structure
2624 mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
2625 mcb->num_requests = 0;
2626 mcb->num_callbacks = num_reqs;
2628 for (i = 0; i < num_reqs; i++) {
2629 mcb->callbacks[i].cb = reqs[i].cb;
2630 mcb->callbacks[i].opaque = reqs[i].opaque;
2633 // Check for mergable requests
2634 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2636 trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
2639 * Run the aio requests. As soon as one request can't be submitted
2640 * successfully, fail all requests that are not yet submitted (we must
2641 * return failure for all requests anyway)
2643 * num_requests cannot be set to the right value immediately: If
2644 * bdrv_aio_writev fails for some request, num_requests would be too high
2645 * and therefore multiwrite_cb() would never recognize the multiwrite
2646 * request as completed. We also cannot use the loop variable i to set it
2647 * when the first request fails because the callback may already have been
2648 * called for previously submitted requests. Thus, num_requests must be
2649 * incremented for each request that is submitted.
2651 * The problem that callbacks may be called early also means that we need
2652 * to take care that num_requests doesn't become 0 before all requests are
2653 * submitted - multiwrite_cb() would consider the multiwrite request
2654 * completed. A dummy request that is "completed" by a manual call to
2655 * multiwrite_cb() takes care of this.
2657 mcb->num_requests = 1;
2659 // Run the aio requests
2660 for (i = 0; i < num_reqs; i++) {
2661 mcb->num_requests++;
2662 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2663 reqs[i].nb_sectors, multiwrite_cb, mcb);
2665 if (acb == NULL) {
2666 // We can only fail the whole thing if no request has been
2667 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2668 // complete and report the error in the callback.
2669 if (i == 0) {
2670 trace_bdrv_aio_multiwrite_earlyfail(mcb);
2671 goto fail;
2672 } else {
2673 trace_bdrv_aio_multiwrite_latefail(mcb, i);
2674 multiwrite_cb(mcb, -EIO);
2675 break;
2680 /* Complete the dummy request */
2681 multiwrite_cb(mcb, 0);
2683 return 0;
2685 fail:
2686 for (i = 0; i < mcb->num_callbacks; i++) {
2687 reqs[i].error = -EIO;
2689 g_free(mcb);
2690 return -1;
2693 BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2694 BlockDriverCompletionFunc *cb, void *opaque)
2696 BlockDriver *drv = bs->drv;
2698 trace_bdrv_aio_flush(bs, opaque);
2700 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2701 return bdrv_aio_noop_em(bs, cb, opaque);
2704 if (!drv)
2705 return NULL;
2706 return drv->bdrv_aio_flush(bs, cb, opaque);
2709 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
2711 acb->pool->cancel(acb);
2715 /**************************************************************/
2716 /* async block device emulation */
2718 typedef struct BlockDriverAIOCBSync {
2719 BlockDriverAIOCB common;
2720 QEMUBH *bh;
2721 int ret;
2722 /* vector translation state */
2723 QEMUIOVector *qiov;
2724 uint8_t *bounce;
2725 int is_write;
2726 } BlockDriverAIOCBSync;
2728 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2730 BlockDriverAIOCBSync *acb =
2731 container_of(blockacb, BlockDriverAIOCBSync, common);
2732 qemu_bh_delete(acb->bh);
2733 acb->bh = NULL;
2734 qemu_aio_release(acb);
2737 static AIOPool bdrv_em_aio_pool = {
2738 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2739 .cancel = bdrv_aio_cancel_em,
2742 static void bdrv_aio_bh_cb(void *opaque)
2744 BlockDriverAIOCBSync *acb = opaque;
2746 if (!acb->is_write)
2747 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
2748 qemu_vfree(acb->bounce);
2749 acb->common.cb(acb->common.opaque, acb->ret);
2750 qemu_bh_delete(acb->bh);
2751 acb->bh = NULL;
2752 qemu_aio_release(acb);
2755 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2756 int64_t sector_num,
2757 QEMUIOVector *qiov,
2758 int nb_sectors,
2759 BlockDriverCompletionFunc *cb,
2760 void *opaque,
2761 int is_write)
2764 BlockDriverAIOCBSync *acb;
2766 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2767 acb->is_write = is_write;
2768 acb->qiov = qiov;
2769 acb->bounce = qemu_blockalign(bs, qiov->size);
2771 if (!acb->bh)
2772 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2774 if (is_write) {
2775 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2776 acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2777 } else {
2778 acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2781 qemu_bh_schedule(acb->bh);
2783 return &acb->common;
2786 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2787 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2788 BlockDriverCompletionFunc *cb, void *opaque)
2790 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
2793 static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2794 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2795 BlockDriverCompletionFunc *cb, void *opaque)
2797 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2801 typedef struct BlockDriverAIOCBCoroutine {
2802 BlockDriverAIOCB common;
2803 BlockRequest req;
2804 bool is_write;
2805 QEMUBH* bh;
2806 } BlockDriverAIOCBCoroutine;
2808 static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb)
2810 qemu_aio_flush();
2813 static AIOPool bdrv_em_co_aio_pool = {
2814 .aiocb_size = sizeof(BlockDriverAIOCBCoroutine),
2815 .cancel = bdrv_aio_co_cancel_em,
2818 static void bdrv_co_rw_bh(void *opaque)
2820 BlockDriverAIOCBCoroutine *acb = opaque;
2822 acb->common.cb(acb->common.opaque, acb->req.error);
2823 qemu_bh_delete(acb->bh);
2824 qemu_aio_release(acb);
2827 static void coroutine_fn bdrv_co_rw(void *opaque)
2829 BlockDriverAIOCBCoroutine *acb = opaque;
2830 BlockDriverState *bs = acb->common.bs;
2832 if (!acb->is_write) {
2833 acb->req.error = bs->drv->bdrv_co_readv(bs, acb->req.sector,
2834 acb->req.nb_sectors, acb->req.qiov);
2835 } else {
2836 acb->req.error = bs->drv->bdrv_co_writev(bs, acb->req.sector,
2837 acb->req.nb_sectors, acb->req.qiov);
2840 acb->bh = qemu_bh_new(bdrv_co_rw_bh, acb);
2841 qemu_bh_schedule(acb->bh);
2844 static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
2845 int64_t sector_num,
2846 QEMUIOVector *qiov,
2847 int nb_sectors,
2848 BlockDriverCompletionFunc *cb,
2849 void *opaque,
2850 bool is_write)
2852 Coroutine *co;
2853 BlockDriverAIOCBCoroutine *acb;
2855 acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
2856 acb->req.sector = sector_num;
2857 acb->req.nb_sectors = nb_sectors;
2858 acb->req.qiov = qiov;
2859 acb->is_write = is_write;
2861 co = qemu_coroutine_create(bdrv_co_rw);
2862 qemu_coroutine_enter(co, acb);
2864 return &acb->common;
2867 static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs,
2868 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2869 BlockDriverCompletionFunc *cb, void *opaque)
2871 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque,
2872 false);
2875 static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs,
2876 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2877 BlockDriverCompletionFunc *cb, void *opaque)
2879 return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque,
2880 true);
2883 static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2884 BlockDriverCompletionFunc *cb, void *opaque)
2886 BlockDriverAIOCBSync *acb;
2888 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2889 acb->is_write = 1; /* don't bounce in the completion hadler */
2890 acb->qiov = NULL;
2891 acb->bounce = NULL;
2892 acb->ret = 0;
2894 if (!acb->bh)
2895 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2897 bdrv_flush(bs);
2898 qemu_bh_schedule(acb->bh);
2899 return &acb->common;
2902 static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2903 BlockDriverCompletionFunc *cb, void *opaque)
2905 BlockDriverAIOCBSync *acb;
2907 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2908 acb->is_write = 1; /* don't bounce in the completion handler */
2909 acb->qiov = NULL;
2910 acb->bounce = NULL;
2911 acb->ret = 0;
2913 if (!acb->bh) {
2914 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2917 qemu_bh_schedule(acb->bh);
2918 return &acb->common;
2921 /**************************************************************/
2922 /* sync block device emulation */
2924 static void bdrv_rw_em_cb(void *opaque, int ret)
2926 *(int *)opaque = ret;
2929 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
2930 uint8_t *buf, int nb_sectors)
2932 int async_ret;
2933 BlockDriverAIOCB *acb;
2934 struct iovec iov;
2935 QEMUIOVector qiov;
2937 async_ret = NOT_DONE;
2938 iov.iov_base = (void *)buf;
2939 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2940 qemu_iovec_init_external(&qiov, &iov, 1);
2942 acb = bs->drv->bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2943 bdrv_rw_em_cb, &async_ret);
2944 if (acb == NULL) {
2945 async_ret = -1;
2946 goto fail;
2949 while (async_ret == NOT_DONE) {
2950 qemu_aio_wait();
2954 fail:
2955 return async_ret;
2958 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2959 const uint8_t *buf, int nb_sectors)
2961 int async_ret;
2962 BlockDriverAIOCB *acb;
2963 struct iovec iov;
2964 QEMUIOVector qiov;
2966 async_ret = NOT_DONE;
2967 iov.iov_base = (void *)buf;
2968 iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2969 qemu_iovec_init_external(&qiov, &iov, 1);
2971 acb = bs->drv->bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2972 bdrv_rw_em_cb, &async_ret);
2973 if (acb == NULL) {
2974 async_ret = -1;
2975 goto fail;
2977 while (async_ret == NOT_DONE) {
2978 qemu_aio_wait();
2981 fail:
2982 return async_ret;
2985 void bdrv_init(void)
2987 module_call_init(MODULE_INIT_BLOCK);
2990 void bdrv_init_with_whitelist(void)
2992 use_bdrv_whitelist = 1;
2993 bdrv_init();
2996 void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2997 BlockDriverCompletionFunc *cb, void *opaque)
2999 BlockDriverAIOCB *acb;
3001 if (pool->free_aiocb) {
3002 acb = pool->free_aiocb;
3003 pool->free_aiocb = acb->next;
3004 } else {
3005 acb = g_malloc0(pool->aiocb_size);
3006 acb->pool = pool;
3008 acb->bs = bs;
3009 acb->cb = cb;
3010 acb->opaque = opaque;
3011 return acb;
3014 void qemu_aio_release(void *p)
3016 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
3017 AIOPool *pool = acb->pool;
3018 acb->next = pool->free_aiocb;
3019 pool->free_aiocb = acb;
3022 /**************************************************************/
3023 /* Coroutine block device emulation */
3025 typedef struct CoroutineIOCompletion {
3026 Coroutine *coroutine;
3027 int ret;
3028 } CoroutineIOCompletion;
3030 static void bdrv_co_io_em_complete(void *opaque, int ret)
3032 CoroutineIOCompletion *co = opaque;
3034 co->ret = ret;
3035 qemu_coroutine_enter(co->coroutine, NULL);
3038 static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num,
3039 int nb_sectors, QEMUIOVector *iov,
3040 bool is_write)
3042 CoroutineIOCompletion co = {
3043 .coroutine = qemu_coroutine_self(),
3045 BlockDriverAIOCB *acb;
3047 if (is_write) {
3048 acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors,
3049 bdrv_co_io_em_complete, &co);
3050 } else {
3051 acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors,
3052 bdrv_co_io_em_complete, &co);
3055 trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb);
3056 if (!acb) {
3057 return -EIO;
3059 qemu_coroutine_yield();
3061 return co.ret;
3064 static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
3065 int64_t sector_num, int nb_sectors,
3066 QEMUIOVector *iov)
3068 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false);
3071 static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
3072 int64_t sector_num, int nb_sectors,
3073 QEMUIOVector *iov)
3075 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true);
3078 static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs)
3080 CoroutineIOCompletion co = {
3081 .coroutine = qemu_coroutine_self(),
3083 BlockDriverAIOCB *acb;
3085 acb = bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
3086 if (!acb) {
3087 return -EIO;
3089 qemu_coroutine_yield();
3090 return co.ret;
3093 /**************************************************************/
3094 /* removable device support */
3097 * Return TRUE if the media is present
3099 int bdrv_is_inserted(BlockDriverState *bs)
3101 BlockDriver *drv = bs->drv;
3103 if (!drv)
3104 return 0;
3105 if (!drv->bdrv_is_inserted)
3106 return 1;
3107 return drv->bdrv_is_inserted(bs);
3111 * Return whether the media changed since the last call to this
3112 * function, or -ENOTSUP if we don't know. Most drivers don't know.
3114 int bdrv_media_changed(BlockDriverState *bs)
3116 BlockDriver *drv = bs->drv;
3118 if (drv && drv->bdrv_media_changed) {
3119 return drv->bdrv_media_changed(bs);
3121 return -ENOTSUP;
3125 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3127 void bdrv_eject(BlockDriverState *bs, int eject_flag)
3129 BlockDriver *drv = bs->drv;
3131 if (drv && drv->bdrv_eject) {
3132 drv->bdrv_eject(bs, eject_flag);
3137 * Lock or unlock the media (if it is locked, the user won't be able
3138 * to eject it manually).
3140 void bdrv_lock_medium(BlockDriverState *bs, bool locked)
3142 BlockDriver *drv = bs->drv;
3144 trace_bdrv_lock_medium(bs, locked);
3146 if (drv && drv->bdrv_lock_medium) {
3147 drv->bdrv_lock_medium(bs, locked);
3151 /* needed for generic scsi interface */
3153 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
3155 BlockDriver *drv = bs->drv;
3157 if (drv && drv->bdrv_ioctl)
3158 return drv->bdrv_ioctl(bs, req, buf);
3159 return -ENOTSUP;
3162 BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
3163 unsigned long int req, void *buf,
3164 BlockDriverCompletionFunc *cb, void *opaque)
3166 BlockDriver *drv = bs->drv;
3168 if (drv && drv->bdrv_aio_ioctl)
3169 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
3170 return NULL;
3173 void bdrv_set_buffer_alignment(BlockDriverState *bs, int align)
3175 bs->buffer_alignment = align;
3178 void *qemu_blockalign(BlockDriverState *bs, size_t size)
3180 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
3183 void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
3185 int64_t bitmap_size;
3187 bs->dirty_count = 0;
3188 if (enable) {
3189 if (!bs->dirty_bitmap) {
3190 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
3191 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
3192 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
3194 bs->dirty_bitmap = g_malloc0(bitmap_size);
3196 } else {
3197 if (bs->dirty_bitmap) {
3198 g_free(bs->dirty_bitmap);
3199 bs->dirty_bitmap = NULL;
3204 int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
3206 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
3208 if (bs->dirty_bitmap &&
3209 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
3210 return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
3211 (1UL << (chunk % (sizeof(unsigned long) * 8))));
3212 } else {
3213 return 0;
3217 void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
3218 int nr_sectors)
3220 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
3223 int64_t bdrv_get_dirty_count(BlockDriverState *bs)
3225 return bs->dirty_count;
3228 void bdrv_set_in_use(BlockDriverState *bs, int in_use)
3230 assert(bs->in_use != in_use);
3231 bs->in_use = in_use;
3234 int bdrv_in_use(BlockDriverState *bs)
3236 return bs->in_use;
3239 void bdrv_iostatus_enable(BlockDriverState *bs)
3241 bs->iostatus = BDRV_IOS_OK;
3244 /* The I/O status is only enabled if the drive explicitly
3245 * enables it _and_ the VM is configured to stop on errors */
3246 bool bdrv_iostatus_is_enabled(const BlockDriverState *bs)
3248 return (bs->iostatus != BDRV_IOS_INVAL &&
3249 (bs->on_write_error == BLOCK_ERR_STOP_ENOSPC ||
3250 bs->on_write_error == BLOCK_ERR_STOP_ANY ||
3251 bs->on_read_error == BLOCK_ERR_STOP_ANY));
3254 void bdrv_iostatus_disable(BlockDriverState *bs)
3256 bs->iostatus = BDRV_IOS_INVAL;
3259 void bdrv_iostatus_reset(BlockDriverState *bs)
3261 if (bdrv_iostatus_is_enabled(bs)) {
3262 bs->iostatus = BDRV_IOS_OK;
3266 /* XXX: Today this is set by device models because it makes the implementation
3267 quite simple. However, the block layer knows about the error, so it's
3268 possible to implement this without device models being involved */
3269 void bdrv_iostatus_set_err(BlockDriverState *bs, int error)
3271 if (bdrv_iostatus_is_enabled(bs) && bs->iostatus == BDRV_IOS_OK) {
3272 assert(error >= 0);
3273 bs->iostatus = error == ENOSPC ? BDRV_IOS_ENOSPC : BDRV_IOS_FAILED;
3277 void
3278 bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes,
3279 enum BlockAcctType type)
3281 assert(type < BDRV_MAX_IOTYPE);
3283 cookie->bytes = bytes;
3284 cookie->start_time_ns = get_clock();
3285 cookie->type = type;
3288 void
3289 bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
3291 assert(cookie->type < BDRV_MAX_IOTYPE);
3293 bs->nr_bytes[cookie->type] += cookie->bytes;
3294 bs->nr_ops[cookie->type]++;
3295 bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
3298 int bdrv_img_create(const char *filename, const char *fmt,
3299 const char *base_filename, const char *base_fmt,
3300 char *options, uint64_t img_size, int flags)
3302 QEMUOptionParameter *param = NULL, *create_options = NULL;
3303 QEMUOptionParameter *backing_fmt, *backing_file, *size;
3304 BlockDriverState *bs = NULL;
3305 BlockDriver *drv, *proto_drv;
3306 BlockDriver *backing_drv = NULL;
3307 int ret = 0;
3309 /* Find driver and parse its options */
3310 drv = bdrv_find_format(fmt);
3311 if (!drv) {
3312 error_report("Unknown file format '%s'", fmt);
3313 ret = -EINVAL;
3314 goto out;
3317 proto_drv = bdrv_find_protocol(filename);
3318 if (!proto_drv) {
3319 error_report("Unknown protocol '%s'", filename);
3320 ret = -EINVAL;
3321 goto out;
3324 create_options = append_option_parameters(create_options,
3325 drv->create_options);
3326 create_options = append_option_parameters(create_options,
3327 proto_drv->create_options);
3329 /* Create parameter list with default values */
3330 param = parse_option_parameters("", create_options, param);
3332 set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
3334 /* Parse -o options */
3335 if (options) {
3336 param = parse_option_parameters(options, create_options, param);
3337 if (param == NULL) {
3338 error_report("Invalid options for file format '%s'.", fmt);
3339 ret = -EINVAL;
3340 goto out;
3344 if (base_filename) {
3345 if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
3346 base_filename)) {
3347 error_report("Backing file not supported for file format '%s'",
3348 fmt);
3349 ret = -EINVAL;
3350 goto out;
3354 if (base_fmt) {
3355 if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
3356 error_report("Backing file format not supported for file "
3357 "format '%s'", fmt);
3358 ret = -EINVAL;
3359 goto out;
3363 backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
3364 if (backing_file && backing_file->value.s) {
3365 if (!strcmp(filename, backing_file->value.s)) {
3366 error_report("Error: Trying to create an image with the "
3367 "same filename as the backing file");
3368 ret = -EINVAL;
3369 goto out;
3373 backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
3374 if (backing_fmt && backing_fmt->value.s) {
3375 backing_drv = bdrv_find_format(backing_fmt->value.s);
3376 if (!backing_drv) {
3377 error_report("Unknown backing file format '%s'",
3378 backing_fmt->value.s);
3379 ret = -EINVAL;
3380 goto out;
3384 // The size for the image must always be specified, with one exception:
3385 // If we are using a backing file, we can obtain the size from there
3386 size = get_option_parameter(param, BLOCK_OPT_SIZE);
3387 if (size && size->value.n == -1) {
3388 if (backing_file && backing_file->value.s) {
3389 uint64_t size;
3390 char buf[32];
3392 bs = bdrv_new("");
3394 ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv);
3395 if (ret < 0) {
3396 error_report("Could not open '%s'", backing_file->value.s);
3397 goto out;
3399 bdrv_get_geometry(bs, &size);
3400 size *= 512;
3402 snprintf(buf, sizeof(buf), "%" PRId64, size);
3403 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
3404 } else {
3405 error_report("Image creation needs a size parameter");
3406 ret = -EINVAL;
3407 goto out;
3411 printf("Formatting '%s', fmt=%s ", filename, fmt);
3412 print_option_parameters(param);
3413 puts("");
3415 ret = bdrv_create(drv, filename, param);
3417 if (ret < 0) {
3418 if (ret == -ENOTSUP) {
3419 error_report("Formatting or formatting option not supported for "
3420 "file format '%s'", fmt);
3421 } else if (ret == -EFBIG) {
3422 error_report("The image size is too large for file format '%s'",
3423 fmt);
3424 } else {
3425 error_report("%s: error while creating %s: %s", filename, fmt,
3426 strerror(-ret));
3430 out:
3431 free_option_parameters(create_options);
3432 free_option_parameters(param);
3434 if (bs) {
3435 bdrv_delete(bs);
3438 return ret;