From: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
To: svt-core@virtuozzo.com
Cc: den@openvz.org, andrey.drobyshev@virtuozzo.com
Subject: [QEMU HCI-8.0 PATCH v3 3/5] vhost-blk: add read-only flag
Date: Tue, 8 Sep 2026 04:04:53 +0300 [thread overview]
Message-ID: <20260908010455.249679-4-andrey.zhadchenko@virtuozzo.com> (raw)
In-Reply-To: <20260908010455.249679-1-andrey.zhadchenko@virtuozzo.com>
Also compare BLKROGET with the selected mode and reject r/w if
needed.
https://virtuozzo.atlassian.net/browse/VSTOR-143437
Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
---
v2:
- check BLKROGET only if rw was requested like posix_file
hw/block/vhost-blk.c | 25 ++++++++++++++++++++++++-
include/hw/virtio/vhost-blk.h | 1 +
2 files changed, 25 insertions(+), 1 deletion(-)
diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index d6681235e5..797743b2c1 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -232,8 +232,9 @@ static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
ERRP_GUARD();
BlockConf *conf = &s->conf.conf;
struct stat st;
+ int open_flags = s->conf.readonly ? O_RDONLY : O_RDWR;
- s->backend_fd = qemu_open(s->conf.devpath, O_RDWR, errp);
+ s->backend_fd = qemu_open(s->conf.devpath, open_flags, errp);
if (s->backend_fd < 0) {
error_prepend(errp, "vhost-blk: unable to open backend: ");
return false;
@@ -251,6 +252,23 @@ static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
goto fail;
}
+ if (!s->conf.readonly) {
+ int readonly;
+
+ if (ioctl(s->backend_fd, BLKROGET, &readonly) < 0) {
+ error_setg_errno(errp, errno,
+ "vhost-blk: unable to get read-only status of "
+ "'%s'", s->conf.devpath);
+ goto fail;
+ }
+
+ if (readonly) {
+ error_setg_errno(errp, EROFS, "vhost-blk: '%s' is not writable",
+ s->conf.devpath);
+ goto fail;
+ }
+ }
+
vhost_blk_update_size(s, errp);
if (*errp) {
goto fail;
@@ -409,6 +427,10 @@ static uint64_t vhost_blk_get_features(VirtIODevice *vdev,
virtio_add_feature(&features, VIRTIO_F_VERSION_1);
+ if (s->conf.readonly) {
+ virtio_add_feature(&features, VIRTIO_BLK_F_RO);
+ }
+
if (s->conf.num_queues > 1) {
virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
}
@@ -456,6 +478,7 @@ static void vhost_blk_update_config(VirtIODevice *vdev, uint8_t *config)
static const Property vhost_blk_properties[] = {
DEFINE_PROP_STRING("devpath", VHostBlk, conf.devpath),
+ DEFINE_PROP_BOOL("read-only", VHostBlk, conf.readonly, false),
DEFINE_PROP_UINT16("num-queues", VHostBlk, conf.num_queues,
VHOST_BLK_AUTO_NUM_QUEUES),
DEFINE_PROP_UINT16("queue-size", VHostBlk, conf.queue_size, 256),
diff --git a/include/hw/virtio/vhost-blk.h b/include/hw/virtio/vhost-blk.h
index c194b421d9..c6646f5845 100644
--- a/include/hw/virtio/vhost-blk.h
+++ b/include/hw/virtio/vhost-blk.h
@@ -25,6 +25,7 @@
typedef struct VhostBlkConf {
BlockConf conf;
char *devpath;
+ bool readonly;
uint16_t num_queues;
uint16_t queue_size;
uint16_t num_threads;
--
2.43.5
next prev parent reply other threads:[~2026-09-08 1:07 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-08 1:04 [QEMU HCI-8.0 PATCH v3 0/5] vhost-blk change backend setup Andrey Zhadchenko
2026-09-08 1:04 ` [QEMU HCI-8.0 PATCH v3 1/5] vhost-blk: improve vhost_blk_device_realize cleanup Andrey Zhadchenko
2026-09-08 11:11 ` Andrey Drobyshev
2026-09-08 1:04 ` [QEMU HCI-8.0 PATCH v3 2/5] vhost-blk: change backend setup Andrey Zhadchenko
2026-09-08 11:11 ` Andrey Drobyshev
2026-09-08 1:04 ` Andrey Zhadchenko [this message]
2026-09-08 11:11 ` [QEMU HCI-8.0 PATCH v3 3/5] vhost-blk: add read-only flag Andrey Drobyshev
2026-09-08 1:04 ` [QEMU HCI-8.0 PATCH v3 4/5] vhost-blk: watch the device for resize events Andrey Zhadchenko
2026-09-08 11:11 ` Andrey Drobyshev
2026-09-08 1:04 ` [QEMU HCI-8.0 PATCH v3 5/5] vhost-blk: preserve the uevent socket across cpr-exec Andrey Zhadchenko
2026-09-08 11:11 ` Andrey Drobyshev
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=20260908010455.249679-4-andrey.zhadchenko@virtuozzo.com \
--to=andrey.zhadchenko@virtuozzo.com \
--cc=andrey.drobyshev@virtuozzo.com \
--cc=den@openvz.org \
--cc=svt-core@virtuozzo.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.