From nobody Tue Feb 10 05:45:09 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1648034497282927.357861019002; Wed, 23 Mar 2022 04:21:37 -0700 (PDT) Received: from localhost ([::1]:37606 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nWz3Y-0006IH-23 for importer@patchew.org; Wed, 23 Mar 2022 07:21:36 -0400 Received: from eggs.gnu.org ([209.51.188.92]:36952) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nWyzk-0000xz-Uo for qemu-devel@nongnu.org; Wed, 23 Mar 2022 07:17:41 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:55674) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nWyzg-0001yK-3F for qemu-devel@nongnu.org; Wed, 23 Mar 2022 07:17:40 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-403-oN4q2bhaPSW-bELnyMPQHA-1; Wed, 23 Mar 2022 07:17:31 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C1F75185A7BA; Wed, 23 Mar 2022 11:17:30 +0000 (UTC) Received: from localhost (unknown [10.39.194.72]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4B2EB41136E1; Wed, 23 Mar 2022 11:17:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1648034255; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WLnq4mun/H8h5jGXpNruvkOQAG3FzNTj3QZG2VFydpE=; b=PC9UC11GGqwIdx/nNWkEoQWZtoi/GDaXVcu7FkMSuWtvshjfYVIhjW1lEPeHAdEhZ25Ev9 ofiM2cn7/tSlngYW8EBq4kp/Bm0v2DcUg9gFb6YVL3CCMzxX9g8Xqj3P1zujDqMXolZnmY EfkF+T0QsSyxM9tql171umYhp/k5ikk= X-MC-Unique: oN4q2bhaPSW-bELnyMPQHA-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [RFC 1/8] blkio: add io_uring block driver using libblkio Date: Wed, 23 Mar 2022 11:17:20 +0000 Message-Id: <20220323111727.1100209-2-stefanha@redhat.com> In-Reply-To: <20220323111727.1100209-1-stefanha@redhat.com> References: <20220323111727.1100209-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laurent Vivier , Kevin Wolf , Thomas Huth , Vladimir Sementsov-Ogievskiy , qemu-block@nongnu.org, "Michael S. Tsirkin" , John Snow , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Alberto Faria , Markus Armbruster , Yanan Wang , Eduardo Habkost , Hanna Reitz , Stefan Hajnoczi , Paolo Bonzini , Fam Zheng , Eric Blake , sgarzare@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1648034498567100001 Content-Type: text/plain; charset="utf-8" libblkio (https://gitlab.com/libblkio/libblkio/) is a library for high-performance disk I/O. It currently supports io_uring with additional drivers planned. One of the reasons for developing libblkio is that other applications besides QEMU can use it. This will be particularly useful for vhost-user-blk which applications may wish to use for connecting to qemu-storage-daemon. libblkio also gives us an opportunity to develop in Rust behind a C API that is easy to consume from QEMU. This commit adds an io_uring BlockDriver to QEMU using libblkio. For now I/O buffers are copied through bounce buffers if the libblkio driver requires it. Later commits add an optimization for pre-registering guest RAM to avoid bounce buffers. It will be easy to add other libblkio drivers since they will share the majority of code. Signed-off-by: Stefan Hajnoczi --- MAINTAINERS | 6 + meson_options.txt | 2 + qapi/block-core.json | 18 +- meson.build | 9 + block/blkio.c | 460 ++++++++++++++++++++++++++++++++++ tests/qtest/modules-test.c | 3 + block/meson.build | 1 + scripts/meson-buildoptions.sh | 3 + 8 files changed, 501 insertions(+), 1 deletion(-) create mode 100644 block/blkio.c diff --git a/MAINTAINERS b/MAINTAINERS index cc364afef7..0fb08dd4f7 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3349,6 +3349,12 @@ L: qemu-block@nongnu.org S: Maintained F: block/vdi.c =20 +blkio +M: Stefan Hajnoczi +L: qemu-block@nongnu.org +S: Maintained +F: block/blkio.c + iSCSI M: Ronnie Sahlberg M: Paolo Bonzini diff --git a/meson_options.txt b/meson_options.txt index 52b11cead4..1e82e770e7 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -101,6 +101,8 @@ option('bzip2', type : 'feature', value : 'auto', description: 'bzip2 support for DMG images') option('cap_ng', type : 'feature', value : 'auto', description: 'cap_ng support') +option('blkio', type : 'feature', value : 'auto', + description: 'libblkio block device driver') option('bpf', type : 'feature', value : 'auto', description: 'eBPF support') option('cocoa', type : 'feature', value : 'auto', diff --git a/qapi/block-core.json b/qapi/block-core.json index e89f2dfb5b..d9bb283108 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -2924,7 +2924,9 @@ 'file', 'snapshot-access', 'ftp', 'ftps', 'gluster', {'name': 'host_cdrom', 'if': 'HAVE_HOST_BLOCK_DEVICE' }, {'name': 'host_device', 'if': 'HAVE_HOST_BLOCK_DEVICE' }, - 'http', 'https', 'iscsi', + 'http', 'https', + { 'name': 'io_uring', 'if': 'CONFIG_BLKIO' }, + 'iscsi', 'luks', 'nbd', 'nfs', 'null-aio', 'null-co', 'nvme', 'parallel= s', 'preallocate', 'qcow', 'qcow2', 'qed', 'quorum', 'raw', 'rbd', { 'name': 'replication', 'if': 'CONFIG_REPLICATION' }, @@ -3656,6 +3658,18 @@ '*debug': 'int', '*logfile': 'str' } } =20 +## +# @BlockdevOptionsIoUring: +# +# Driver specific block device options for the io_uring backend. +# +# @filename: path to the image file +# +# Since: 6.3 +## +{ 'struct': 'BlockdevOptionsIoUring', + 'data': { 'filename': 'str' } } + ## # @IscsiTransport: # @@ -4254,6 +4268,8 @@ 'if': 'HAVE_HOST_BLOCK_DEVICE' }, 'http': 'BlockdevOptionsCurlHttp', 'https': 'BlockdevOptionsCurlHttps', + 'io_uring': { 'type': 'BlockdevOptionsIoUring', + 'if': 'CONFIG_BLKIO' }, 'iscsi': 'BlockdevOptionsIscsi', 'luks': 'BlockdevOptionsLUKS', 'nbd': 'BlockdevOptionsNbd', diff --git a/meson.build b/meson.build index aef724ad3c..d52f542b2e 100644 --- a/meson.build +++ b/meson.build @@ -636,6 +636,13 @@ if not get_option('virglrenderer').auto() or have_syst= em or have_vhost_user_gpu required: get_option('virglrenderer'), kwargs: static_kwargs) endif +blkio =3D not_found +if not get_option('blkio').auto() or have_block + blkio =3D dependency('blkio', + method: 'pkg-config', + required: get_option('blkio'), + kwargs: static_kwargs) +endif curl =3D not_found if not get_option('curl').auto() or have_block curl =3D dependency('libcurl', version: '>=3D7.29.0', @@ -1519,6 +1526,7 @@ config_host_data.set('CONFIG_LIBUDEV', libudev.found(= )) config_host_data.set('CONFIG_LZO', lzo.found()) config_host_data.set('CONFIG_MPATH', mpathpersist.found()) config_host_data.set('CONFIG_MPATH_NEW_API', mpathpersist_new_api) +config_host_data.set('CONFIG_BLKIO', blkio.found()) config_host_data.set('CONFIG_CURL', curl.found()) config_host_data.set('CONFIG_CURSES', curses.found()) config_host_data.set('CONFIG_GBM', gbm.found()) @@ -3672,6 +3680,7 @@ summary_info +=3D {'PAM': pam} summary_info +=3D {'iconv support': iconv} summary_info +=3D {'curses support': curses} summary_info +=3D {'virgl support': virgl} +summary_info +=3D {'blkio support': blkio} summary_info +=3D {'curl support': curl} summary_info +=3D {'Multipath support': mpathpersist} summary_info +=3D {'VNC support': vnc} diff --git a/block/blkio.c b/block/blkio.c new file mode 100644 index 0000000000..dd2308b967 --- /dev/null +++ b/block/blkio.c @@ -0,0 +1,460 @@ +#include "qemu/osdep.h" +#include +#include "block/block_int.h" +#include "qapi/error.h" +#include "qapi/qmp/qdict.h" +#include "qemu/module.h" + +typedef struct BlkAIOCB { + BlockAIOCB common; + struct blkio_mem_region mem_region; + QEMUIOVector qiov; + struct iovec bounce_iov; +} BlkioAIOCB; + +typedef struct { + /* Protects ->blkio and request submission on ->blkioq */ + QemuMutex lock; + + struct blkio *blkio; + struct blkioq *blkioq; /* this could be multi-queue in the future */ + int completion_fd; + + /* The value of the "mem-region-alignment" property */ + size_t mem_region_alignment; + + /* Can we skip adding/deleting blkio_mem_regions? */ + bool needs_mem_regions; + + /* + * blkio_completion_fd_poll() stashes the next completion for + * blkio_completion_fd_poll_ready(). + */ + struct blkio_completion pending_completion; +} BDRVBlkioState; + +static void blkio_aiocb_complete(BlkioAIOCB *acb, int ret) +{ + /* Copy bounce buffer back to qiov */ + if (acb->qiov.niov > 0) { + qemu_iovec_from_buf(&acb->qiov, 0, + acb->bounce_iov.iov_base, + acb->bounce_iov.iov_len); + qemu_iovec_destroy(&acb->qiov); + } + + acb->common.cb(acb->common.opaque, ret); + + if (acb->mem_region.len > 0) { + BDRVBlkioState *s =3D acb->common.bs->opaque; + + WITH_QEMU_LOCK_GUARD(&s->lock) { + blkio_free_mem_region(s->blkio, &acb->mem_region); + } + } + + qemu_aio_unref(&acb->common); +} + +/* + * Only the thread that calls aio_poll() invokes fd and poll handlers. + * Therefore locks are not necessary except when accessing s->blkio. + * + * No locking is performed around blkioq_get_completions() although other + * threads may submit I/O requests on s->blkioq. We're assuming there is no + * inteference between blkioq_get_completions() and other s->blkioq APIs. + */ + +static void blkio_completion_fd_read(void *opaque) +{ + BlockDriverState *bs =3D opaque; + BDRVBlkioState *s =3D bs->opaque; + struct blkio_completion completion; + uint64_t val; + ssize_t ret __attribute__((unused)); + + /* Reset completion fd status */ + ret =3D read(s->completion_fd, &val, sizeof(val)); + + /* + * Reading one completion at a time makes nested event loop re-entrancy + * simple. Change this loop to get multiple completions in one go if it + * becomes a performance bottleneck. + */ + while (blkioq_get_completions(s->blkioq, &completion, 1) =3D=3D 1) { + blkio_aiocb_complete(completion.user_data, completion.ret); + } +} + +static bool blkio_completion_fd_poll(void *opaque) +{ + BlockDriverState *bs =3D opaque; + BDRVBlkioState *s =3D bs->opaque; + + return blkioq_get_completions(s->blkioq, &s->pending_completion, 1) = =3D=3D 1; +} + +static void blkio_completion_fd_poll_ready(void *opaque) +{ + BlockDriverState *bs =3D opaque; + BDRVBlkioState *s =3D bs->opaque; + BlkioAIOCB *acb =3D s->pending_completion.user_data; + + blkio_aiocb_complete(acb, s->pending_completion.ret); + + /* Process any remaining completions */ + blkio_completion_fd_read(opaque); +} + +static void blkio_attach_aio_context(BlockDriverState *bs, + AioContext *new_context) +{ + BDRVBlkioState *s =3D bs->opaque; + + aio_set_fd_handler(new_context, + s->completion_fd, + false, + blkio_completion_fd_read, + NULL, + blkio_completion_fd_poll, + blkio_completion_fd_poll_ready, + bs); +} + +static void blkio_detach_aio_context(BlockDriverState *bs) +{ + BDRVBlkioState *s =3D bs->opaque; + + aio_set_fd_handler(bdrv_get_aio_context(bs), + s->completion_fd, + false, NULL, NULL, NULL, NULL, NULL); +} + +static const AIOCBInfo blkio_aiocb_info =3D { + .aiocb_size =3D sizeof(BlkioAIOCB), +}; + +/* Create a BlkioAIOCB */ +static BlkioAIOCB *blkio_aiocb_get(BlockDriverState *bs, + BlockCompletionFunc *cb, + void *opaque) +{ + BlkioAIOCB *acb =3D qemu_aio_get(&blkio_aiocb_info, bs, cb, opaque); + + /* A few fields need to be initialized, leave the rest... */ + acb->qiov.niov =3D 0; + acb->mem_region.len =3D 0; + return acb; +} + +/* s->lock must be held */ +static int blkio_aiocb_init_mem_region_locked(BlkioAIOCB *acb, size_t len) +{ + BDRVBlkioState *s =3D acb->common.bs->opaque; + size_t mem_region_len =3D QEMU_ALIGN_UP(len, s->mem_region_alignment); + int ret; + + ret =3D blkio_alloc_mem_region(s->blkio, &acb->mem_region, mem_region_= len, + 0, NULL); + if (ret < 0) { + return ret; + } + + acb->bounce_iov.iov_base =3D acb->mem_region.addr; + acb->bounce_iov.iov_len =3D len; + return 0; +} + +static BlockAIOCB *blkio_aio_preadv(BlockDriverState *bs, int64_t offset, + int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags, + BlockCompletionFunc *cb, void *opaque) +{ + BDRVBlkioState *s =3D bs->opaque; + struct iovec *iov =3D qiov->iov; + int iovcnt =3D qiov->niov; + BlkioAIOCB *acb; + int ret; + + QEMU_LOCK_GUARD(&s->lock); + + acb =3D blkio_aiocb_get(bs, cb, opaque); + + if (s->needs_mem_regions) { + if (blkio_aiocb_init_mem_region_locked(acb, bytes) < 0) { + qemu_aio_unref(&acb->common); + return NULL; + } + + /* Copy qiov because we'll call qemu_iovec_from_buf() on completio= n */ + qemu_iovec_init_slice(&acb->qiov, qiov, 0, qiov->size); + + iov =3D &acb->bounce_iov; + iovcnt =3D 1; + } + + ret =3D blkioq_readv(s->blkioq, offset, iov, iovcnt, acb, 0); + if (ret < 0) { + if (s->needs_mem_regions) { + blkio_free_mem_region(s->blkio, &acb->mem_region); + qemu_iovec_destroy(&acb->qiov); + } + qemu_aio_unref(&acb->common); + return NULL; + } + + if (qatomic_read(&bs->io_plugged) =3D=3D 0) { + blkioq_submit_and_wait(s->blkioq, 0, NULL, NULL); + } + + return &acb->common; +} + +static BlockAIOCB *blkio_aio_pwritev(BlockDriverState *bs, int64_t offset, + int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags, + BlockCompletionFunc *cb, void *opaque) +{ + uint32_t blkio_flags =3D (flags & BDRV_REQ_FUA) ? BLKIO_REQ_FUA : 0; + BDRVBlkioState *s =3D bs->opaque; + struct iovec *iov =3D qiov->iov; + int iovcnt =3D qiov->niov; + BlkioAIOCB *acb; + int ret; + + QEMU_LOCK_GUARD(&s->lock); + + acb =3D blkio_aiocb_get(bs, cb, opaque); + + if (s->needs_mem_regions) { + if (blkio_aiocb_init_mem_region_locked(acb, bytes) < 0) { + qemu_aio_unref(&acb->common); + return NULL; + } + + qemu_iovec_to_buf(qiov, 0, acb->bounce_iov.iov_base, bytes); + + iov =3D &acb->bounce_iov; + iovcnt =3D 1; + } + + ret =3D blkioq_writev(s->blkioq, offset, iov, iovcnt, acb, blkio_flags= ); + if (ret < 0) { + if (s->needs_mem_regions) { + blkio_free_mem_region(s->blkio, &acb->mem_region); + } + qemu_aio_unref(&acb->common); + return NULL; + } + + if (qatomic_read(&bs->io_plugged) =3D=3D 0) { + blkioq_submit_and_wait(s->blkioq, 0, NULL, NULL); + } + + return &acb->common; +} + +static BlockAIOCB *blkio_aio_flush(BlockDriverState *bs, + BlockCompletionFunc *cb, + void *opaque) +{ + BDRVBlkioState *s =3D bs->opaque; + BlkioAIOCB *acb; + int ret; + + QEMU_LOCK_GUARD(&s->lock); + + acb =3D blkio_aiocb_get(bs, cb, opaque); + + ret =3D blkioq_flush(s->blkioq, acb, 0); + if (ret < 0) { + qemu_aio_unref(&acb->common); + return NULL; + } + + if (qatomic_read(&bs->io_plugged) =3D=3D 0) { + blkioq_submit_and_wait(s->blkioq, 0, NULL, NULL); + } + + return &acb->common; +} + +static void blkio_io_unplug(BlockDriverState *bs) +{ + BDRVBlkioState *s =3D bs->opaque; + + WITH_QEMU_LOCK_GUARD(&s->lock) { + blkioq_submit_and_wait(s->blkioq, 0, NULL, NULL); + } +} + +static void blkio_parse_filename_io_uring(const char *filename, QDict *opt= ions, + Error **errp) +{ + bdrv_parse_filename_strip_prefix(filename, "io_uring:", options); +} + +static int blkio_file_open(BlockDriverState *bs, QDict *options, int flags, + Error **errp) +{ + const char *filename =3D qdict_get_try_str(options, "filename"); + const char *blkio_driver =3D bs->drv->protocol_name; + BDRVBlkioState *s =3D bs->opaque; + char *errmsg; + int ret; + + ret =3D blkio_create(blkio_driver, &s->blkio, &errmsg); + if (ret < 0) { + error_setg_errno(errp, -ret, "blkio_create failed: %s", errmsg); + free(errmsg); + return ret; + } + + ret =3D blkio_set_str(s->blkio, "path", filename, &errmsg); + if (ret < 0) { + error_setg_errno(errp, -ret, "failed to set path: %s", errmsg); + free(errmsg); + blkio_destroy(&s->blkio); + return ret; + } + + if (flags & BDRV_O_NOCACHE) { + ret =3D blkio_set_bool(s->blkio, "direct", true, &errmsg); + if (ret < 0) { + error_setg_errno(errp, -ret, "failed to set direct: %s", errms= g); + free(errmsg); + blkio_destroy(&s->blkio); + return ret; + } + } + + if (!(flags & BDRV_O_RDWR)) { + ret =3D blkio_set_bool(s->blkio, "readonly", true, &errmsg); + if (ret < 0) { + error_setg_errno(errp, -ret, "failed to set readonly: %s", err= msg); + free(errmsg); + blkio_destroy(&s->blkio); + return ret; + } + } + + ret =3D blkio_connect(s->blkio, &errmsg); + if (ret < 0) { + error_setg_errno(errp, -ret, "blkio_connect failed: %s", errmsg); + free(errmsg); + blkio_destroy(&s->blkio); + return ret; + } + + ret =3D blkio_get_bool(s->blkio, + "needs-mem-regions", + &s->needs_mem_regions, + &errmsg); + if (ret < 0) { + error_setg_errno(errp, -ret, + "failed to get needs-mem-regions: %s", errmsg); + free(errmsg); + blkio_destroy(&s->blkio); + return ret; + } + + ret =3D blkio_get_uint64(s->blkio, + "mem-region-alignment", + &s->mem_region_alignment, + &errmsg); + if (ret < 0) { + error_setg_errno(errp, -ret, + "failed to get mem-region-alignment: %s", errmsg); + free(errmsg); + blkio_destroy(&s->blkio); + return ret; + } + + ret =3D blkio_start(s->blkio, &errmsg); + if (ret < 0) { + error_setg_errno(errp, -ret, "blkio_start failed: %s", errmsg); + free(errmsg); + blkio_destroy(&s->blkio); + return ret; + } + + bs->supported_write_flags =3D BDRV_REQ_FUA; + + qemu_mutex_init(&s->lock); + s->blkioq =3D blkio_get_queue(s->blkio, 0); + s->completion_fd =3D blkioq_get_completion_fd(s->blkioq); + + blkio_attach_aio_context(bs, bdrv_get_aio_context(bs)); + + qdict_del(options, "filename"); + return 0; +} + +static void blkio_close(BlockDriverState *bs) +{ + BDRVBlkioState *s =3D bs->opaque; + + qemu_mutex_destroy(&s->lock); + blkio_destroy(&s->blkio); +} + +static int64_t blkio_getlength(BlockDriverState *bs) +{ + BDRVBlkioState *s =3D bs->opaque; + char *errmsg; + uint64_t capacity; + int ret; + + WITH_QEMU_LOCK_GUARD(&s->lock) { + ret =3D blkio_get_uint64(s->blkio, "capacity", &capacity, &errmsg); + } + if (ret < 0) { + free(errmsg); + return -ret; + } + + return capacity; +} + +static int blkio_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) +{ + return 0; +} + +static BlockDriver bdrv_io_uring =3D { + .format_name =3D "io_uring", + .protocol_name =3D "io_uring", + .instance_size =3D sizeof(BDRVBlkioState), + .bdrv_needs_filename =3D true, + .bdrv_parse_filename =3D blkio_parse_filename_io_uring, + .bdrv_file_open =3D blkio_file_open, + .bdrv_close =3D blkio_close, + .bdrv_getlength =3D blkio_getlength, + .has_variable_length =3D true, + .bdrv_get_info =3D blkio_get_info, + .bdrv_attach_aio_context =3D blkio_attach_aio_context, + .bdrv_detach_aio_context =3D blkio_detach_aio_context, + .bdrv_aio_preadv =3D blkio_aio_preadv, + .bdrv_aio_pwritev =3D blkio_aio_pwritev, + .bdrv_aio_flush =3D blkio_aio_flush, + .bdrv_io_unplug =3D blkio_io_unplug, + + /* + * TODO + * Missing libblkio APIs: + * - bdrv_refresh_limits + * - write zeroes + * - discard + * - block_status + * - co_invalidate_cache + * + * Out of scope? + * - create + * - truncate + */ +}; + +static void bdrv_blkio_init(void) +{ + bdrv_register(&bdrv_io_uring); +} + +block_init(bdrv_blkio_init); diff --git a/tests/qtest/modules-test.c b/tests/qtest/modules-test.c index c238b3f422..2e0f73fdc6 100644 --- a/tests/qtest/modules-test.c +++ b/tests/qtest/modules-test.c @@ -16,6 +16,9 @@ static void test_modules_load(const void *data) int main(int argc, char *argv[]) { const char *modules[] =3D { +#ifdef CONFIG_BLKIO + "block-", "blkio", +#endif #ifdef CONFIG_CURL "block-", "curl", #endif diff --git a/block/meson.build b/block/meson.build index 0b2a60c99b..787667384a 100644 --- a/block/meson.build +++ b/block/meson.build @@ -92,6 +92,7 @@ block_modules =3D {} =20 modsrc =3D [] foreach m : [ + [blkio, 'blkio', files('blkio.c')], [curl, 'curl', files('curl.c')], [glusterfs, 'gluster', files('gluster.c')], [libiscsi, 'iscsi', [files('iscsi.c'), libm]], diff --git a/scripts/meson-buildoptions.sh b/scripts/meson-buildoptions.sh index 1e26f4571e..845a29b9b7 100644 --- a/scripts/meson-buildoptions.sh +++ b/scripts/meson-buildoptions.sh @@ -40,6 +40,7 @@ meson_options_help() { printf "%s\n" ' auth-pam PAM access control' printf "%s\n" ' avx2 AVX2 optimizations' printf "%s\n" ' avx512f AVX512F optimizations' + printf "%s\n" ' blkio libblkio block device driver' printf "%s\n" ' bochs bochs image format support' printf "%s\n" ' bpf eBPF support' printf "%s\n" ' brlapi brlapi character device driver' @@ -146,6 +147,8 @@ _meson_option_parse() { --disable-avx2) printf "%s" -Davx2=3Ddisabled ;; --enable-avx512f) printf "%s" -Davx512f=3Denabled ;; --disable-avx512f) printf "%s" -Davx512f=3Ddisabled ;; + --enable-blkio) printf "%s" -Dblkio=3Denabled ;; + --disable-blkio) printf "%s" -Dblkio=3Ddisabled ;; --enable-block-drv-whitelist-in-tools) printf "%s" -Dblock_drv_whiteli= st_in_tools=3Dtrue ;; --disable-block-drv-whitelist-in-tools) printf "%s" -Dblock_drv_whitel= ist_in_tools=3Dfalse ;; --enable-bochs) printf "%s" -Dbochs=3Denabled ;; --=20 2.35.1