1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 * Copyright Red Hat, Inc.
8 * Stefan Hajnoczi <stefanha@redhat.com>
11 #include "qemu/osdep.h"
13 #include "block/block_int.h"
14 #include "exec/memory.h"
15 #include "exec/cpu-common.h" /* for qemu_ram_get_fd() */
16 #include "qapi/error.h"
17 #include "qemu/error-report.h"
18 #include "qapi/qmp/qdict.h"
19 #include "qemu/module.h"
20 #include "exec/memory.h" /* for ram_block_discard_disable() */
23 * Keep the QEMU BlockDriver names identical to the libblkio driver names.
24 * Using macros instead of typing out the string literals avoids typos.
26 #define DRIVER_IO_URING "io_uring"
27 #define DRIVER_NVME_IO_URING "nvme-io_uring"
28 #define DRIVER_VIRTIO_BLK_VFIO_PCI "virtio-blk-vfio-pci"
29 #define DRIVER_VIRTIO_BLK_VHOST_USER "virtio-blk-vhost-user"
30 #define DRIVER_VIRTIO_BLK_VHOST_VDPA "virtio-blk-vhost-vdpa"
33 * Allocated bounce buffers are kept in a list sorted by buffer address.
35 typedef struct BlkioBounceBuf
{
36 QLIST_ENTRY(BlkioBounceBuf
) next
;
38 /* The bounce buffer */
44 * libblkio is not thread-safe so this lock protects ->blkio and
49 struct blkioq
*blkioq
; /* make this multi-queue in the future... */
53 * Polling fetches the next completion into this field.
55 * No lock is necessary since only one thread calls aio_poll() and invokes
56 * fd and poll handlers.
58 struct blkio_completion poll_completion
;
61 * Protects ->bounce_pool, ->bounce_bufs, ->bounce_available.
63 * Lock ordering: ->bounce_lock before ->blkio_lock.
67 /* Bounce buffer pool */
68 struct blkio_mem_region bounce_pool
;
70 /* Sorted list of allocated bounce buffers */
71 QLIST_HEAD(, BlkioBounceBuf
) bounce_bufs
;
73 /* Queue for coroutines waiting for bounce buffer space */
74 CoQueue bounce_available
;
76 /* The value of the "mem-region-alignment" property */
77 size_t mem_region_alignment
;
79 /* Can we skip adding/deleting blkio_mem_regions? */
80 bool needs_mem_regions
;
82 /* Are file descriptors necessary for blkio_mem_regions? */
83 bool needs_mem_region_fd
;
85 /* Are madvise(MADV_DONTNEED)-style operations unavailable? */
86 bool may_pin_mem_regions
;
89 /* Called with s->bounce_lock held */
90 static int blkio_resize_bounce_pool(BDRVBlkioState
*s
, int64_t bytes
)
92 /* There can be no allocated bounce buffers during resize */
93 assert(QLIST_EMPTY(&s
->bounce_bufs
));
95 /* Pad size to reduce frequency of resize calls */
98 WITH_QEMU_LOCK_GUARD(&s
->blkio_lock
) {
101 if (s
->bounce_pool
.addr
) {
102 blkio_unmap_mem_region(s
->blkio
, &s
->bounce_pool
);
103 blkio_free_mem_region(s
->blkio
, &s
->bounce_pool
);
104 memset(&s
->bounce_pool
, 0, sizeof(s
->bounce_pool
));
107 /* Automatically freed when s->blkio is destroyed */
108 ret
= blkio_alloc_mem_region(s
->blkio
, &s
->bounce_pool
, bytes
);
113 ret
= blkio_map_mem_region(s
->blkio
, &s
->bounce_pool
);
115 blkio_free_mem_region(s
->blkio
, &s
->bounce_pool
);
116 memset(&s
->bounce_pool
, 0, sizeof(s
->bounce_pool
));
124 /* Called with s->bounce_lock held */
126 blkio_do_alloc_bounce_buffer(BDRVBlkioState
*s
, BlkioBounceBuf
*bounce
,
129 void *addr
= s
->bounce_pool
.addr
;
130 BlkioBounceBuf
*cur
= NULL
;
131 BlkioBounceBuf
*prev
= NULL
;
135 * This is just a linear search over the holes between requests. An
136 * efficient allocator would be nice.
138 QLIST_FOREACH(cur
, &s
->bounce_bufs
, next
) {
139 space
= cur
->buf
.iov_base
- addr
;
140 if (bytes
<= space
) {
141 QLIST_INSERT_BEFORE(cur
, bounce
, next
);
142 bounce
->buf
.iov_base
= addr
;
143 bounce
->buf
.iov_len
= bytes
;
147 addr
= cur
->buf
.iov_base
+ cur
->buf
.iov_len
;
151 /* Is there space after the last request? */
152 space
= s
->bounce_pool
.addr
+ s
->bounce_pool
.len
- addr
;
157 QLIST_INSERT_AFTER(prev
, bounce
, next
);
159 QLIST_INSERT_HEAD(&s
->bounce_bufs
, bounce
, next
);
161 bounce
->buf
.iov_base
= addr
;
162 bounce
->buf
.iov_len
= bytes
;
166 static int coroutine_fn
167 blkio_alloc_bounce_buffer(BDRVBlkioState
*s
, BlkioBounceBuf
*bounce
,
171 * Ensure fairness: first time around we join the back of the queue,
172 * subsequently we join the front so we don't lose our place.
174 CoQueueWaitFlags wait_flags
= 0;
176 QEMU_LOCK_GUARD(&s
->bounce_lock
);
178 /* Ensure fairness: don't even try if other requests are already waiting */
179 if (!qemu_co_queue_empty(&s
->bounce_available
)) {
180 qemu_co_queue_wait_flags(&s
->bounce_available
, &s
->bounce_lock
,
182 wait_flags
= CO_QUEUE_WAIT_FRONT
;
186 if (blkio_do_alloc_bounce_buffer(s
, bounce
, bytes
)) {
187 /* Kick the next queued request since there may be space */
188 qemu_co_queue_next(&s
->bounce_available
);
193 * If there are no in-flight requests then the pool was simply too
196 if (QLIST_EMPTY(&s
->bounce_bufs
)) {
200 ret
= blkio_resize_bounce_pool(s
, bytes
);
202 /* Kick the next queued request since that may fail too */
203 qemu_co_queue_next(&s
->bounce_available
);
207 ok
= blkio_do_alloc_bounce_buffer(s
, bounce
, bytes
);
208 assert(ok
); /* must have space this time */
212 qemu_co_queue_wait_flags(&s
->bounce_available
, &s
->bounce_lock
,
214 wait_flags
= CO_QUEUE_WAIT_FRONT
;
218 static void coroutine_fn
blkio_free_bounce_buffer(BDRVBlkioState
*s
,
219 BlkioBounceBuf
*bounce
)
221 QEMU_LOCK_GUARD(&s
->bounce_lock
);
223 QLIST_REMOVE(bounce
, next
);
225 /* Wake up waiting coroutines since space may now be available */
226 qemu_co_queue_next(&s
->bounce_available
);
229 /* For async to .bdrv_co_*() conversion */
231 Coroutine
*coroutine
;
235 static void blkio_completion_fd_read(void *opaque
)
237 BlockDriverState
*bs
= opaque
;
238 BDRVBlkioState
*s
= bs
->opaque
;
242 /* Polling may have already fetched a completion */
243 if (s
->poll_completion
.user_data
!= NULL
) {
244 BlkioCoData
*cod
= s
->poll_completion
.user_data
;
245 cod
->ret
= s
->poll_completion
.ret
;
247 /* Clear it in case aio_co_wake() enters a nested event loop */
248 s
->poll_completion
.user_data
= NULL
;
250 aio_co_wake(cod
->coroutine
);
253 /* Reset completion fd status */
254 ret
= read(s
->completion_fd
, &val
, sizeof(val
));
256 /* Ignore errors, there's nothing we can do */
260 * Reading one completion at a time makes nested event loop re-entrancy
261 * simple. Change this loop to get multiple completions in one go if it
262 * becomes a performance bottleneck.
265 struct blkio_completion completion
;
267 WITH_QEMU_LOCK_GUARD(&s
->blkio_lock
) {
268 ret
= blkioq_do_io(s
->blkioq
, &completion
, 0, 1, NULL
);
274 BlkioCoData
*cod
= completion
.user_data
;
275 cod
->ret
= completion
.ret
;
276 aio_co_wake(cod
->coroutine
);
280 static bool blkio_completion_fd_poll(void *opaque
)
282 BlockDriverState
*bs
= opaque
;
283 BDRVBlkioState
*s
= bs
->opaque
;
286 /* Just in case we already fetched a completion */
287 if (s
->poll_completion
.user_data
!= NULL
) {
291 WITH_QEMU_LOCK_GUARD(&s
->blkio_lock
) {
292 ret
= blkioq_do_io(s
->blkioq
, &s
->poll_completion
, 0, 1, NULL
);
297 static void blkio_completion_fd_poll_ready(void *opaque
)
299 blkio_completion_fd_read(opaque
);
302 static void blkio_attach_aio_context(BlockDriverState
*bs
,
303 AioContext
*new_context
)
305 BDRVBlkioState
*s
= bs
->opaque
;
307 aio_set_fd_handler(new_context
,
310 blkio_completion_fd_read
,
312 blkio_completion_fd_poll
,
313 blkio_completion_fd_poll_ready
,
317 static void blkio_detach_aio_context(BlockDriverState
*bs
)
319 BDRVBlkioState
*s
= bs
->opaque
;
321 aio_set_fd_handler(bdrv_get_aio_context(bs
),
323 false, NULL
, NULL
, NULL
, NULL
, NULL
);
326 /* Call with s->blkio_lock held to submit I/O after enqueuing a new request */
327 static void blkio_submit_io(BlockDriverState
*bs
)
329 if (qatomic_read(&bs
->io_plugged
) == 0) {
330 BDRVBlkioState
*s
= bs
->opaque
;
332 blkioq_do_io(s
->blkioq
, NULL
, 0, 0, NULL
);
336 static int coroutine_fn
337 blkio_co_pdiscard(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
339 BDRVBlkioState
*s
= bs
->opaque
;
341 .coroutine
= qemu_coroutine_self(),
344 WITH_QEMU_LOCK_GUARD(&s
->blkio_lock
) {
345 blkioq_discard(s
->blkioq
, offset
, bytes
, &cod
, 0);
349 qemu_coroutine_yield();
353 static int coroutine_fn
354 blkio_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
355 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
358 .coroutine
= qemu_coroutine_self(),
360 BDRVBlkioState
*s
= bs
->opaque
;
361 bool use_bounce_buffer
=
362 s
->needs_mem_regions
&& !(flags
& BDRV_REQ_REGISTERED_BUF
);
363 BlkioBounceBuf bounce
;
364 struct iovec
*iov
= qiov
->iov
;
365 int iovcnt
= qiov
->niov
;
367 if (use_bounce_buffer
) {
368 int ret
= blkio_alloc_bounce_buffer(s
, &bounce
, bytes
);
377 WITH_QEMU_LOCK_GUARD(&s
->blkio_lock
) {
378 blkioq_readv(s
->blkioq
, offset
, iov
, iovcnt
, &cod
, 0);
382 qemu_coroutine_yield();
384 if (use_bounce_buffer
) {
386 qemu_iovec_from_buf(qiov
, 0,
391 blkio_free_bounce_buffer(s
, &bounce
);
397 static int coroutine_fn
blkio_co_pwritev(BlockDriverState
*bs
, int64_t offset
,
398 int64_t bytes
, QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
400 uint32_t blkio_flags
= (flags
& BDRV_REQ_FUA
) ? BLKIO_REQ_FUA
: 0;
402 .coroutine
= qemu_coroutine_self(),
404 BDRVBlkioState
*s
= bs
->opaque
;
405 bool use_bounce_buffer
=
406 s
->needs_mem_regions
&& !(flags
& BDRV_REQ_REGISTERED_BUF
);
407 BlkioBounceBuf bounce
;
408 struct iovec
*iov
= qiov
->iov
;
409 int iovcnt
= qiov
->niov
;
411 if (use_bounce_buffer
) {
412 int ret
= blkio_alloc_bounce_buffer(s
, &bounce
, bytes
);
417 qemu_iovec_to_buf(qiov
, 0, bounce
.buf
.iov_base
, bytes
);
422 WITH_QEMU_LOCK_GUARD(&s
->blkio_lock
) {
423 blkioq_writev(s
->blkioq
, offset
, iov
, iovcnt
, &cod
, blkio_flags
);
427 qemu_coroutine_yield();
429 if (use_bounce_buffer
) {
430 blkio_free_bounce_buffer(s
, &bounce
);
436 static int coroutine_fn
blkio_co_flush(BlockDriverState
*bs
)
438 BDRVBlkioState
*s
= bs
->opaque
;
440 .coroutine
= qemu_coroutine_self(),
443 WITH_QEMU_LOCK_GUARD(&s
->blkio_lock
) {
444 blkioq_flush(s
->blkioq
, &cod
, 0);
448 qemu_coroutine_yield();
452 static int coroutine_fn
blkio_co_pwrite_zeroes(BlockDriverState
*bs
,
453 int64_t offset
, int64_t bytes
, BdrvRequestFlags flags
)
455 BDRVBlkioState
*s
= bs
->opaque
;
457 .coroutine
= qemu_coroutine_self(),
459 uint32_t blkio_flags
= 0;
461 if (flags
& BDRV_REQ_FUA
) {
462 blkio_flags
|= BLKIO_REQ_FUA
;
464 if (!(flags
& BDRV_REQ_MAY_UNMAP
)) {
465 blkio_flags
|= BLKIO_REQ_NO_UNMAP
;
467 if (flags
& BDRV_REQ_NO_FALLBACK
) {
468 blkio_flags
|= BLKIO_REQ_NO_FALLBACK
;
471 WITH_QEMU_LOCK_GUARD(&s
->blkio_lock
) {
472 blkioq_write_zeroes(s
->blkioq
, offset
, bytes
, &cod
, blkio_flags
);
476 qemu_coroutine_yield();
480 static void blkio_io_unplug(BlockDriverState
*bs
)
482 BDRVBlkioState
*s
= bs
->opaque
;
484 WITH_QEMU_LOCK_GUARD(&s
->blkio_lock
) {
493 } BlkioMemRegionResult
;
496 * Produce a struct blkio_mem_region for a given address and size.
498 * This function produces identical results when called multiple times with the
499 * same arguments. This property is necessary because blkio_unmap_mem_region()
500 * must receive the same struct blkio_mem_region field values that were passed
501 * to blkio_map_mem_region().
503 static BlkioMemRegionResult
504 blkio_mem_region_from_host(BlockDriverState
*bs
,
505 void *host
, size_t size
,
506 struct blkio_mem_region
*region
,
509 BDRVBlkioState
*s
= bs
->opaque
;
511 ram_addr_t fd_offset
= 0;
513 if (((uintptr_t)host
| size
) % s
->mem_region_alignment
) {
514 error_setg(errp
, "unaligned buf %p with size %zu", host
, size
);
518 /* Attempt to find the fd for the underlying memory */
519 if (s
->needs_mem_region_fd
) {
525 * bdrv_register_buf() is called with the BQL held so mr lives at least
526 * until this function returns.
528 ram_block
= qemu_ram_block_from_host(host
, false, &fd_offset
);
530 fd
= qemu_ram_get_fd(ram_block
);
534 * Ideally every RAMBlock would have an fd. pc-bios and other
535 * things don't. Luckily they are usually not I/O buffers and we
536 * can just ignore them.
541 /* Make sure the fd covers the entire range */
542 end_block
= qemu_ram_block_from_host(host
+ size
- 1, false, &offset
);
543 if (ram_block
!= end_block
) {
544 error_setg(errp
, "registered buffer at %p with size %zu extends "
545 "beyond RAMBlock", host
, size
);
550 *region
= (struct blkio_mem_region
){
554 .fd_offset
= fd_offset
,
559 static bool blkio_register_buf(BlockDriverState
*bs
, void *host
, size_t size
,
562 BDRVBlkioState
*s
= bs
->opaque
;
563 struct blkio_mem_region region
;
564 BlkioMemRegionResult region_result
;
568 * Mapping memory regions conflicts with RAM discard (virtio-mem) when
569 * there is pinning, so only do it when necessary.
571 if (!s
->needs_mem_regions
&& s
->may_pin_mem_regions
) {
575 region_result
= blkio_mem_region_from_host(bs
, host
, size
, ®ion
, errp
);
576 if (region_result
== BMRR_SKIP
) {
578 } else if (region_result
!= BMRR_OK
) {
582 WITH_QEMU_LOCK_GUARD(&s
->blkio_lock
) {
583 ret
= blkio_map_mem_region(s
->blkio
, ®ion
);
587 error_setg(errp
, "Failed to add blkio mem region %p with size %zu: %s",
588 host
, size
, blkio_get_error_msg());
594 static void blkio_unregister_buf(BlockDriverState
*bs
, void *host
, size_t size
)
596 BDRVBlkioState
*s
= bs
->opaque
;
597 struct blkio_mem_region region
;
599 /* See blkio_register_buf() */
600 if (!s
->needs_mem_regions
&& s
->may_pin_mem_regions
) {
604 if (blkio_mem_region_from_host(bs
, host
, size
, ®ion
, NULL
) != BMRR_OK
) {
608 WITH_QEMU_LOCK_GUARD(&s
->blkio_lock
) {
609 blkio_unmap_mem_region(s
->blkio
, ®ion
);
613 static int blkio_io_uring_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
616 const char *filename
= qdict_get_str(options
, "filename");
617 BDRVBlkioState
*s
= bs
->opaque
;
620 ret
= blkio_set_str(s
->blkio
, "path", filename
);
621 qdict_del(options
, "filename");
623 error_setg_errno(errp
, -ret
, "failed to set path: %s",
624 blkio_get_error_msg());
628 if (flags
& BDRV_O_NOCACHE
) {
629 ret
= blkio_set_bool(s
->blkio
, "direct", true);
631 error_setg_errno(errp
, -ret
, "failed to set direct: %s",
632 blkio_get_error_msg());
640 static int blkio_nvme_io_uring(BlockDriverState
*bs
, QDict
*options
, int flags
,
643 const char *path
= qdict_get_try_str(options
, "path");
644 BDRVBlkioState
*s
= bs
->opaque
;
648 error_setg(errp
, "missing 'path' option");
652 ret
= blkio_set_str(s
->blkio
, "path", path
);
653 qdict_del(options
, "path");
655 error_setg_errno(errp
, -ret
, "failed to set path: %s",
656 blkio_get_error_msg());
660 if (!(flags
& BDRV_O_NOCACHE
)) {
661 error_setg(errp
, "cache.direct=off is not supported");
668 static int blkio_virtio_blk_common_open(BlockDriverState
*bs
,
669 QDict
*options
, int flags
, Error
**errp
)
671 const char *path
= qdict_get_try_str(options
, "path");
672 BDRVBlkioState
*s
= bs
->opaque
;
676 error_setg(errp
, "missing 'path' option");
680 ret
= blkio_set_str(s
->blkio
, "path", path
);
681 qdict_del(options
, "path");
683 error_setg_errno(errp
, -ret
, "failed to set path: %s",
684 blkio_get_error_msg());
688 if (!(flags
& BDRV_O_NOCACHE
)) {
689 error_setg(errp
, "cache.direct=off is not supported");
695 static int blkio_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
698 const char *blkio_driver
= bs
->drv
->protocol_name
;
699 BDRVBlkioState
*s
= bs
->opaque
;
702 ret
= blkio_create(blkio_driver
, &s
->blkio
);
704 error_setg_errno(errp
, -ret
, "blkio_create failed: %s",
705 blkio_get_error_msg());
709 if (strcmp(blkio_driver
, DRIVER_IO_URING
) == 0) {
710 ret
= blkio_io_uring_open(bs
, options
, flags
, errp
);
711 } else if (strcmp(blkio_driver
, DRIVER_NVME_IO_URING
) == 0) {
712 ret
= blkio_nvme_io_uring(bs
, options
, flags
, errp
);
713 } else if (strcmp(blkio_driver
, DRIVER_VIRTIO_BLK_VFIO_PCI
) == 0) {
714 ret
= blkio_virtio_blk_common_open(bs
, options
, flags
, errp
);
715 } else if (strcmp(blkio_driver
, DRIVER_VIRTIO_BLK_VHOST_USER
) == 0) {
716 ret
= blkio_virtio_blk_common_open(bs
, options
, flags
, errp
);
717 } else if (strcmp(blkio_driver
, DRIVER_VIRTIO_BLK_VHOST_VDPA
) == 0) {
718 ret
= blkio_virtio_blk_common_open(bs
, options
, flags
, errp
);
720 g_assert_not_reached();
723 blkio_destroy(&s
->blkio
);
727 if (!(flags
& BDRV_O_RDWR
)) {
728 ret
= blkio_set_bool(s
->blkio
, "read-only", true);
730 error_setg_errno(errp
, -ret
, "failed to set read-only: %s",
731 blkio_get_error_msg());
732 blkio_destroy(&s
->blkio
);
737 ret
= blkio_connect(s
->blkio
);
739 error_setg_errno(errp
, -ret
, "blkio_connect failed: %s",
740 blkio_get_error_msg());
741 blkio_destroy(&s
->blkio
);
745 ret
= blkio_get_bool(s
->blkio
,
747 &s
->needs_mem_regions
);
749 error_setg_errno(errp
, -ret
,
750 "failed to get needs-mem-regions: %s",
751 blkio_get_error_msg());
752 blkio_destroy(&s
->blkio
);
756 ret
= blkio_get_bool(s
->blkio
,
757 "needs-mem-region-fd",
758 &s
->needs_mem_region_fd
);
760 error_setg_errno(errp
, -ret
,
761 "failed to get needs-mem-region-fd: %s",
762 blkio_get_error_msg());
763 blkio_destroy(&s
->blkio
);
767 ret
= blkio_get_uint64(s
->blkio
,
768 "mem-region-alignment",
769 &s
->mem_region_alignment
);
771 error_setg_errno(errp
, -ret
,
772 "failed to get mem-region-alignment: %s",
773 blkio_get_error_msg());
774 blkio_destroy(&s
->blkio
);
778 ret
= blkio_get_bool(s
->blkio
,
779 "may-pin-mem-regions",
780 &s
->may_pin_mem_regions
);
782 /* Be conservative (assume pinning) if the property is not supported */
783 s
->may_pin_mem_regions
= s
->needs_mem_regions
;
787 * Notify if libblkio drivers pin memory and prevent features like
788 * virtio-mem from working.
790 if (s
->may_pin_mem_regions
) {
791 ret
= ram_block_discard_disable(true);
793 error_setg_errno(errp
, -ret
, "ram_block_discard_disable() failed");
794 blkio_destroy(&s
->blkio
);
799 ret
= blkio_start(s
->blkio
);
801 error_setg_errno(errp
, -ret
, "blkio_start failed: %s",
802 blkio_get_error_msg());
803 blkio_destroy(&s
->blkio
);
804 if (s
->may_pin_mem_regions
) {
805 ram_block_discard_disable(false);
810 bs
->supported_write_flags
= BDRV_REQ_FUA
| BDRV_REQ_REGISTERED_BUF
;
811 bs
->supported_zero_flags
= BDRV_REQ_FUA
| BDRV_REQ_MAY_UNMAP
|
812 BDRV_REQ_NO_FALLBACK
;
814 qemu_mutex_init(&s
->blkio_lock
);
815 qemu_co_mutex_init(&s
->bounce_lock
);
816 qemu_co_queue_init(&s
->bounce_available
);
817 QLIST_INIT(&s
->bounce_bufs
);
818 s
->blkioq
= blkio_get_queue(s
->blkio
, 0);
819 s
->completion_fd
= blkioq_get_completion_fd(s
->blkioq
);
821 blkio_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
825 static void blkio_close(BlockDriverState
*bs
)
827 BDRVBlkioState
*s
= bs
->opaque
;
829 /* There is no destroy() API for s->bounce_lock */
831 qemu_mutex_destroy(&s
->blkio_lock
);
832 blkio_detach_aio_context(bs
);
833 blkio_destroy(&s
->blkio
);
835 if (s
->may_pin_mem_regions
) {
836 ram_block_discard_disable(false);
840 static int64_t blkio_getlength(BlockDriverState
*bs
)
842 BDRVBlkioState
*s
= bs
->opaque
;
846 WITH_QEMU_LOCK_GUARD(&s
->blkio_lock
) {
847 ret
= blkio_get_uint64(s
->blkio
, "capacity", &capacity
);
856 static int coroutine_fn
blkio_truncate(BlockDriverState
*bs
, int64_t offset
,
857 bool exact
, PreallocMode prealloc
,
858 BdrvRequestFlags flags
, Error
**errp
)
860 int64_t current_length
;
862 if (prealloc
!= PREALLOC_MODE_OFF
) {
863 error_setg(errp
, "Unsupported preallocation mode '%s'",
864 PreallocMode_str(prealloc
));
868 current_length
= blkio_getlength(bs
);
870 if (offset
> current_length
) {
871 error_setg(errp
, "Cannot grow device");
873 } else if (exact
&& offset
!= current_length
) {
874 error_setg(errp
, "Cannot resize device");
881 static int blkio_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
886 static void blkio_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
888 BDRVBlkioState
*s
= bs
->opaque
;
889 QEMU_LOCK_GUARD(&s
->blkio_lock
);
893 ret
= blkio_get_int(s
->blkio
, "request-alignment", &value
);
895 error_setg_errno(errp
, -ret
, "failed to get \"request-alignment\": %s",
896 blkio_get_error_msg());
899 bs
->bl
.request_alignment
= value
;
900 if (bs
->bl
.request_alignment
< 1 ||
901 bs
->bl
.request_alignment
>= INT_MAX
||
902 !is_power_of_2(bs
->bl
.request_alignment
)) {
903 error_setg(errp
, "invalid \"request-alignment\" value %" PRIu32
", "
904 "must be a power of 2 less than INT_MAX",
905 bs
->bl
.request_alignment
);
909 ret
= blkio_get_int(s
->blkio
, "optimal-io-size", &value
);
911 error_setg_errno(errp
, -ret
, "failed to get \"optimal-io-size\": %s",
912 blkio_get_error_msg());
915 bs
->bl
.opt_transfer
= value
;
916 if (bs
->bl
.opt_transfer
> INT_MAX
||
917 (bs
->bl
.opt_transfer
% bs
->bl
.request_alignment
)) {
918 error_setg(errp
, "invalid \"optimal-io-size\" value %" PRIu32
", must "
919 "be a multiple of %" PRIu32
, bs
->bl
.opt_transfer
,
920 bs
->bl
.request_alignment
);
924 ret
= blkio_get_int(s
->blkio
, "max-transfer", &value
);
926 error_setg_errno(errp
, -ret
, "failed to get \"max-transfer\": %s",
927 blkio_get_error_msg());
930 bs
->bl
.max_transfer
= value
;
931 if ((bs
->bl
.max_transfer
% bs
->bl
.request_alignment
) ||
932 (bs
->bl
.opt_transfer
&& (bs
->bl
.max_transfer
% bs
->bl
.opt_transfer
))) {
933 error_setg(errp
, "invalid \"max-transfer\" value %" PRIu32
", must be "
934 "a multiple of %" PRIu32
" and %" PRIu32
" (if non-zero)",
935 bs
->bl
.max_transfer
, bs
->bl
.request_alignment
,
936 bs
->bl
.opt_transfer
);
940 ret
= blkio_get_int(s
->blkio
, "buf-alignment", &value
);
942 error_setg_errno(errp
, -ret
, "failed to get \"buf-alignment\": %s",
943 blkio_get_error_msg());
947 error_setg(errp
, "invalid \"buf-alignment\" value %d, must be "
951 bs
->bl
.min_mem_alignment
= value
;
953 ret
= blkio_get_int(s
->blkio
, "optimal-buf-alignment", &value
);
955 error_setg_errno(errp
, -ret
,
956 "failed to get \"optimal-buf-alignment\": %s",
957 blkio_get_error_msg());
961 error_setg(errp
, "invalid \"optimal-buf-alignment\" value %d, "
962 "must be positive", value
);
965 bs
->bl
.opt_mem_alignment
= value
;
967 ret
= blkio_get_int(s
->blkio
, "max-segments", &value
);
969 error_setg_errno(errp
, -ret
, "failed to get \"max-segments\": %s",
970 blkio_get_error_msg());
974 error_setg(errp
, "invalid \"max-segments\" value %d, must be positive",
978 bs
->bl
.max_iov
= value
;
983 * Missing libblkio APIs:
985 * - co_invalidate_cache
992 #define BLKIO_DRIVER(name, ...) \
994 .format_name = name, \
995 .protocol_name = name, \
996 .instance_size = sizeof(BDRVBlkioState), \
997 .bdrv_file_open = blkio_file_open, \
998 .bdrv_close = blkio_close, \
999 .bdrv_getlength = blkio_getlength, \
1000 .bdrv_co_truncate = blkio_truncate, \
1001 .bdrv_get_info = blkio_get_info, \
1002 .bdrv_attach_aio_context = blkio_attach_aio_context, \
1003 .bdrv_detach_aio_context = blkio_detach_aio_context, \
1004 .bdrv_co_pdiscard = blkio_co_pdiscard, \
1005 .bdrv_co_preadv = blkio_co_preadv, \
1006 .bdrv_co_pwritev = blkio_co_pwritev, \
1007 .bdrv_co_flush_to_disk = blkio_co_flush, \
1008 .bdrv_co_pwrite_zeroes = blkio_co_pwrite_zeroes, \
1009 .bdrv_io_unplug = blkio_io_unplug, \
1010 .bdrv_refresh_limits = blkio_refresh_limits, \
1011 .bdrv_register_buf = blkio_register_buf, \
1012 .bdrv_unregister_buf = blkio_unregister_buf, \
1016 static BlockDriver bdrv_io_uring
= BLKIO_DRIVER(
1018 .bdrv_needs_filename
= true,
1021 static BlockDriver bdrv_nvme_io_uring
= BLKIO_DRIVER(
1022 DRIVER_NVME_IO_URING
,
1025 static BlockDriver bdrv_virtio_blk_vfio_pci
= BLKIO_DRIVER(
1026 DRIVER_VIRTIO_BLK_VFIO_PCI
1029 static BlockDriver bdrv_virtio_blk_vhost_user
= BLKIO_DRIVER(
1030 DRIVER_VIRTIO_BLK_VHOST_USER
1033 static BlockDriver bdrv_virtio_blk_vhost_vdpa
= BLKIO_DRIVER(
1034 DRIVER_VIRTIO_BLK_VHOST_VDPA
1037 static void bdrv_blkio_init(void)
1039 bdrv_register(&bdrv_io_uring
);
1040 bdrv_register(&bdrv_nvme_io_uring
);
1041 bdrv_register(&bdrv_virtio_blk_vfio_pci
);
1042 bdrv_register(&bdrv_virtio_blk_vhost_user
);
1043 bdrv_register(&bdrv_virtio_blk_vhost_vdpa
);
1046 block_init(bdrv_blkio_init
);