From: Alberto Faria <afaria@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Coiby Xu" <Coiby.Xu@gmail.com>,
"Laurent Vivier" <lvivier@redhat.com>,
"Fabiano Rosas" <farosas@suse.de>,
"Raphael Norwitz" <raphael@enfabrica.net>,
"Eduardo Habkost" <eduardo@habkost.net>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Stefano Garzarella" <sgarzare@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Stefan Hajnoczi" <stefanha@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Cornelia Huck" <cohuck@redhat.com>,
"Kevin Wolf" <kwolf@redhat.com>,
qemu-block@nongnu.org, "Zhao Liu" <zhao1.liu@intel.com>,
"Hanna Reitz" <hreitz@redhat.com>,
"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
"Xie Yongji" <xieyongji@bytedance.com>,
"Yanan Wang" <wangyanan55@huawei.com>,
"Alberto Faria" <afaria@redhat.com>
Subject: [RFC 2/4] virtio-blk: Add VIRTIO_BLK_T_OUT_FUA command support
Date: Thu, 8 May 2025 01:24:38 +0100 [thread overview]
Message-ID: <20250508002440.423776-3-afaria@redhat.com> (raw)
In-Reply-To: <20250508002440.423776-1-afaria@redhat.com>
Signed-off-by: Alberto Faria <afaria@redhat.com>
---
block/export/virtio-blk-handler.c | 7 ++--
hw/block/virtio-blk.c | 2 ++
hw/core/machine.c | 4 ++-
hw/virtio/virtio-qmp.c | 2 ++
tests/qtest/virtio-blk-test.c | 56 +++++++++++++++++++++++++++++++
5 files changed, 68 insertions(+), 3 deletions(-)
diff --git a/block/export/virtio-blk-handler.c b/block/export/virtio-blk-handler.c
index bc1cec67570..db40529fa68 100644
--- a/block/export/virtio-blk-handler.c
+++ b/block/export/virtio-blk-handler.c
@@ -169,11 +169,13 @@ int coroutine_fn virtio_blk_process_req(VirtioBlkHandler *handler,
type = le32_to_cpu(out.type);
switch (type & ~VIRTIO_BLK_T_BARRIER) {
case VIRTIO_BLK_T_IN:
- case VIRTIO_BLK_T_OUT: {
+ case VIRTIO_BLK_T_OUT:
+ case VIRTIO_BLK_T_OUT_FUA: {
QEMUIOVector qiov;
int64_t offset;
ssize_t ret = 0;
bool is_write = type & VIRTIO_BLK_T_OUT;
+ bool is_fua = type == VIRTIO_BLK_T_OUT_FUA;
int64_t sector_num = le64_to_cpu(out.sector);
if (is_write && !handler->writable) {
@@ -197,7 +199,8 @@ int coroutine_fn virtio_blk_process_req(VirtioBlkHandler *handler,
offset = sector_num << VIRTIO_BLK_SECTOR_BITS;
if (is_write) {
- ret = blk_co_pwritev(blk, offset, qiov.size, &qiov, 0);
+ ret = blk_co_pwritev(blk, offset, qiov.size, &qiov,
+ is_fua ? BDRV_REQ_FUA : 0);
} else {
ret = blk_co_preadv(blk, offset, qiov.size, &qiov, 0);
}
diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c
index b54d01d3a24..611de0fa860 100644
--- a/hw/block/virtio-blk.c
+++ b/hw/block/virtio-blk.c
@@ -1882,6 +1882,8 @@ static const Property virtio_blk_properties[] = {
conf.max_discard_sectors, BDRV_REQUEST_MAX_SECTORS),
DEFINE_PROP_UINT32("max-write-zeroes-sectors", VirtIOBlock,
conf.max_write_zeroes_sectors, BDRV_REQUEST_MAX_SECTORS),
+ DEFINE_PROP_BIT64("fua-write", VirtIOBlock, host_features,
+ VIRTIO_BLK_F_OUT_FUA, true),
DEFINE_PROP_BOOL("x-enable-wce-if-config-wce", VirtIOBlock,
conf.x_enable_wce_if_config_wce, true),
};
diff --git a/hw/core/machine.c b/hw/core/machine.c
index ed01798d37c..8439b094904 100644
--- a/hw/core/machine.c
+++ b/hw/core/machine.c
@@ -37,7 +37,9 @@
#include "hw/virtio/virtio-iommu.h"
#include "audio/audio.h"
-GlobalProperty hw_compat_10_0[] = {};
+GlobalProperty hw_compat_10_0[] = {
+ { "virtio-blk-device", "fua-write", "false" },
+};
const size_t hw_compat_10_0_len = G_N_ELEMENTS(hw_compat_10_0);
GlobalProperty hw_compat_9_2[] = {
diff --git a/hw/virtio/virtio-qmp.c b/hw/virtio/virtio-qmp.c
index 3b6377cf0d2..9cbdd543623 100644
--- a/hw/virtio/virtio-qmp.c
+++ b/hw/virtio/virtio-qmp.c
@@ -173,6 +173,8 @@ static const qmp_virtio_feature_map_t virtio_blk_feature_map[] = {
"VIRTIO_BLK_F_SECURE_ERASE: Secure erase supported"),
FEATURE_ENTRY(VIRTIO_BLK_F_ZONED, \
"VIRTIO_BLK_F_ZONED: Zoned block devices"),
+ FEATURE_ENTRY(VIRTIO_BLK_F_OUT_FUA, \
+ "VIRTIO_BLK_F_OUT_FUA: FUA write command supported"),
#ifndef VIRTIO_BLK_NO_LEGACY
FEATURE_ENTRY(VIRTIO_BLK_F_BARRIER, \
"VIRTIO_BLK_F_BARRIER: Request barriers supported"),
diff --git a/tests/qtest/virtio-blk-test.c b/tests/qtest/virtio-blk-test.c
index 98c906ebb4a..a90838b8912 100644
--- a/tests/qtest/virtio-blk-test.c
+++ b/tests/qtest/virtio-blk-test.c
@@ -88,6 +88,7 @@ static uint64_t virtio_blk_request(QGuestAllocator *alloc, QVirtioDevice *d,
switch (req->type) {
case VIRTIO_BLK_T_IN:
case VIRTIO_BLK_T_OUT:
+ case VIRTIO_BLK_T_OUT_FUA:
g_assert_cmpuint(data_size % 512, ==, 0);
break;
case VIRTIO_BLK_T_DISCARD:
@@ -280,6 +281,61 @@ static QVirtQueue *test_basic(QVirtioDevice *dev, QGuestAllocator *alloc)
guest_free(alloc, req_addr);
}
+ if (features & (1u << VIRTIO_BLK_F_OUT_FUA)) {
+ /* FUA write and read with 3 descriptor layout */
+ /* FUA write request */
+ req.type = VIRTIO_BLK_T_OUT_FUA;
+ req.ioprio = 1;
+ req.sector = 0;
+ req.data = g_malloc0(512);
+ strcpy(req.data, "test");
+
+ req_addr = virtio_blk_request(alloc, dev, &req, 512);
+
+ g_free(req.data);
+
+ free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
+ qvirtqueue_add(qts, vq, req_addr + 16, 512, false, true);
+ qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
+
+ qvirtqueue_kick(qts, dev, vq, free_head);
+
+ qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
+ QVIRTIO_BLK_TIMEOUT_US);
+ status = readb(req_addr + 528);
+ g_assert_cmpint(status, ==, 0);
+
+ guest_free(alloc, req_addr);
+
+ /* Read request */
+ req.type = VIRTIO_BLK_T_IN;
+ req.ioprio = 1;
+ req.sector = 0;
+ req.data = g_malloc0(512);
+
+ req_addr = virtio_blk_request(alloc, dev, &req, 512);
+
+ g_free(req.data);
+
+ free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
+ qvirtqueue_add(qts, vq, req_addr + 16, 512, true, true);
+ qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
+
+ qvirtqueue_kick(qts, dev, vq, free_head);
+
+ qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
+ QVIRTIO_BLK_TIMEOUT_US);
+ status = readb(req_addr + 528);
+ g_assert_cmpint(status, ==, 0);
+
+ data = g_malloc0(512);
+ memread(req_addr + 16, data, 512);
+ g_assert_cmpstr(data, ==, "test");
+ g_free(data);
+
+ guest_free(alloc, req_addr);
+ }
+
if (features & (1u << VIRTIO_F_ANY_LAYOUT)) {
/* Write and read with 2 descriptor layout */
/* Write request */
--
2.49.0
next prev parent reply other threads:[~2025-05-08 0:25 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-08 0:24 [RFC 0/4] virtio-blk: Add support for FUA write requests Alberto Faria
2025-05-08 0:24 ` [RFC 1/4] include: Add tentative virtio-blk FUA write definitions Alberto Faria
2025-05-08 0:24 ` Alberto Faria [this message]
2025-05-08 20:37 ` [RFC 2/4] virtio-blk: Add VIRTIO_BLK_T_OUT_FUA command support Stefan Hajnoczi
2025-05-08 21:28 ` Alberto Faria
2025-05-08 0:24 ` [RFC 3/4] vhost-user-blk: " Alberto Faria
2025-05-08 0:24 ` [RFC 4/4] vduse-blk: " Alberto Faria
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250508002440.423776-3-afaria@redhat.com \
--to=afaria@redhat.com \
--cc=Coiby.Xu@gmail.com \
--cc=cohuck@redhat.com \
--cc=eduardo@habkost.net \
--cc=farosas@suse.de \
--cc=hreitz@redhat.com \
--cc=kwolf@redhat.com \
--cc=lvivier@redhat.com \
--cc=marcel.apfelbaum@gmail.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=raphael@enfabrica.net \
--cc=sgarzare@redhat.com \
--cc=stefanha@redhat.com \
--cc=wangyanan55@huawei.com \
--cc=xieyongji@bytedance.com \
--cc=zhao1.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).