* [PATCH 1/5] block/nbd: Extract request slot access and reconnection
2026-11-19 14:46 [PATCH 0/5] block/nbd: Fix block resize on idle device Guillaume Vincke
@ 2026-11-19 14:46 ` Guillaume Vincke
2026-11-19 22:16 ` Eric Blake
2026-11-19 14:46 ` [PATCH 2/5] block: Refresh the connection before block_resize Guillaume Vincke
` (4 subsequent siblings)
5 siblings, 1 reply; 15+ messages in thread
From: Guillaume Vincke @ 2026-11-19 14:46 UTC (permalink / raw)
To: qemu-devel
Cc: qemu-block, eblake, vsementsov, kwolf, hreitz, stefanha, fam,
Guillaume Vincke
The number of concurrent requests in the NBD block driver is bounded by
MAX_NBD_REQUESTS. When reaching that level of concurrency, we park
coroutines in a queue and, on completion, we resume the first. That
logic is implemented in nbd_co_send_request().
Coroutines also get queued when the channel is disconnected to make
sure only one request triggers reconnection.
This patch extracts nbd_co_acquire_slot(), nbd_co_release_slot() and
nbd_co_reconnect() out of the request sending. This will help use
nbd_reconnect_attempt() outside of the request sending logic in a later
patch of the series.
No functional change.
Signed-off-by: Guillaume Vincke <guillaume.vincke@ovhcloud.com>
---
block/nbd.c | 56 ++++++++++++++++++++++++++++++++++++++---------------
1 file changed, 40 insertions(+), 16 deletions(-)
diff --git a/block/nbd.c b/block/nbd.c
index 49f585eab5..df72b17ade 100644
--- a/block/nbd.c
+++ b/block/nbd.c
@@ -510,6 +510,41 @@ err:
return ret;
}
+/*
+ * Parks until a slot in the request queue is available or when, being the only
+ * in flight operation running, the caller is ready to run reconnection.
+ * Increments s->in_flight on return.
+ * Called with s->requests_lock held.
+ */
+static void coroutine_fn nbd_co_acquire_slot(BDRVNBDState *s)
+{
+ while (s->in_flight == MAX_NBD_REQUESTS ||
+ (s->state != NBD_CLIENT_CONNECTED && s->in_flight > 0)) {
+ qemu_co_queue_wait(&s->free_sema, &s->requests_lock);
+ }
+ s->in_flight++;
+}
+
+/* Called with s->requests_lock held. */
+static void coroutine_fn nbd_co_release_slot(BDRVNBDState *s)
+{
+ s->in_flight--;
+ qemu_co_queue_next(&s->free_sema);
+}
+
+/* Called with s->requests_lock held. */
+static int coroutine_fn GRAPH_RDLOCK nbd_co_reconnect(BDRVNBDState *s)
+{
+ if (nbd_client_connecting(s)) {
+ nbd_reconnect_attempt(s);
+ qemu_co_queue_restart_all(&s->free_sema);
+ }
+ if (s->state != NBD_CLIENT_CONNECTED) {
+ return -EIO;
+ }
+ return 0;
+}
+
static int coroutine_fn GRAPH_RDLOCK
nbd_co_send_request(BlockDriverState *bs, NBDRequest *request,
QEMUIOVector *qiov)
@@ -518,19 +553,10 @@ nbd_co_send_request(BlockDriverState *bs, NBDRequest *request,
int rc, i = -1;
qemu_mutex_lock(&s->requests_lock);
- while (s->in_flight == MAX_NBD_REQUESTS ||
- (s->state != NBD_CLIENT_CONNECTED && s->in_flight > 0)) {
- qemu_co_queue_wait(&s->free_sema, &s->requests_lock);
- }
-
- s->in_flight++;
+ nbd_co_acquire_slot(s);
if (s->state != NBD_CLIENT_CONNECTED) {
- if (nbd_client_connecting(s)) {
- nbd_reconnect_attempt(s);
- qemu_co_queue_restart_all(&s->free_sema);
- }
- if (s->state != NBD_CLIENT_CONNECTED) {
- rc = -EIO;
+ rc = nbd_co_reconnect(s);
+ if (rc != 0) {
goto err;
}
}
@@ -573,8 +599,7 @@ err:
if (i != -1) {
s->requests[i].coroutine = NULL;
}
- s->in_flight--;
- qemu_co_queue_next(&s->free_sema);
+ nbd_co_release_slot(s);
qemu_mutex_unlock(&s->requests_lock);
}
return rc;
@@ -1085,8 +1110,7 @@ static bool coroutine_fn nbd_reply_chunk_iter_receive(BDRVNBDState *s,
break_loop:
qemu_mutex_lock(&s->requests_lock);
nbd_request_by_cookie(s, cookie, &error_abort)->coroutine = NULL;
- s->in_flight--;
- qemu_co_queue_next(&s->free_sema);
+ nbd_co_release_slot(s);
qemu_mutex_unlock(&s->requests_lock);
return false;
--
2.43.0
^ permalink raw reply related [flat|nested] 15+ messages in thread* Re: [PATCH 1/5] block/nbd: Extract request slot access and reconnection
2026-11-19 14:46 ` [PATCH 1/5] block/nbd: Extract request slot access and reconnection Guillaume Vincke
@ 2026-11-19 22:16 ` Eric Blake
0 siblings, 0 replies; 15+ messages in thread
From: Eric Blake @ 2026-11-19 22:16 UTC (permalink / raw)
To: Guillaume Vincke
Cc: qemu-devel, qemu-block, vsementsov, kwolf, hreitz, stefanha, fam
On Thu, Nov 19, 2026 at 03:46:28PM +0100, Guillaume Vincke wrote:
> The number of concurrent requests in the NBD block driver is bounded by
> MAX_NBD_REQUESTS. When reaching that level of concurrency, we park
> coroutines in a queue and, on completion, we resume the first. That
> logic is implemented in nbd_co_send_request().
> Coroutines also get queued when the channel is disconnected to make
> sure only one request triggers reconnection.
>
> This patch extracts nbd_co_acquire_slot(), nbd_co_release_slot() and
> nbd_co_reconnect() out of the request sending. This will help use
> nbd_reconnect_attempt() outside of the request sending logic in a later
> patch of the series.
>
> No functional change.
>
> Signed-off-by: Guillaume Vincke <guillaume.vincke@ovhcloud.com>
> ---
> block/nbd.c | 56 ++++++++++++++++++++++++++++++++++++++---------------
> 1 file changed, 40 insertions(+), 16 deletions(-)
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization: qemu.org | libguestfs.org
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 2/5] block: Refresh the connection before block_resize
2026-11-19 14:46 [PATCH 0/5] block/nbd: Fix block resize on idle device Guillaume Vincke
2026-11-19 14:46 ` [PATCH 1/5] block/nbd: Extract request slot access and reconnection Guillaume Vincke
@ 2026-11-19 14:46 ` Guillaume Vincke
2026-11-19 22:32 ` Eric Blake
2026-11-19 14:46 ` [PATCH 3/5] block/nbd: Refresh the connection on block_resize Guillaume Vincke
` (3 subsequent siblings)
5 siblings, 1 reply; 15+ messages in thread
From: Guillaume Vincke @ 2026-11-19 14:46 UTC (permalink / raw)
To: qemu-devel
Cc: qemu-block, eblake, vsementsov, kwolf, hreitz, stefanha, fam,
Guillaume Vincke
When a network based device is not connected, there is no way for the
backend to notify a change. For instance, the NBD client relies on lazy
reconnection. Therefore, if an NBD backend changes size, there is
currently no way to update the device size until at least one I/O
operation is sent.
This patch adds the bdrv_co_refresh_connection callback to the
BlockDriver structure so a reconnection can be forced. Considering
nbd_co_truncate() does not perform any I/O, it also modifies the QMP
block_resize implementation to execute the callback, if defined, before
truncating.
No functional change.
Signed-off-by: Guillaume Vincke <guillaume.vincke@ovhcloud.com>
---
block/block-backend.c | 12 ++++++++++++
block/io.c | 24 ++++++++++++++++++++++++
blockdev.c | 9 +++++++++
include/block/block-io.h | 8 ++++++++
include/block/block_int-common.h | 3 +++
include/system/block-backend-io.h | 2 ++
6 files changed, 58 insertions(+)
diff --git a/block/block-backend.c b/block/block-backend.c
index 656c0aed67..b133bb8292 100644
--- a/block/block-backend.c
+++ b/block/block-backend.c
@@ -2600,6 +2600,18 @@ int coroutine_fn blk_co_truncate(BlockBackend *blk, int64_t offset, bool exact,
return bdrv_co_truncate(blk->root, offset, exact, prealloc, flags, errp);
}
+int coroutine_fn blk_co_refresh_connection(BlockBackend *blk, Error **errp)
+{
+ IO_OR_GS_CODE();
+ GRAPH_RDLOCK_GUARD();
+ if (!blk_co_is_available(blk)) {
+ error_setg(errp, "No medium inserted");
+ return -ENOMEDIUM;
+ }
+
+ return bdrv_co_refresh_connection(blk->root, errp);
+}
+
int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
int64_t pos, int size)
{
diff --git a/block/io.c b/block/io.c
index e4568d4d24..3c5c05b9a0 100644
--- a/block/io.c
+++ b/block/io.c
@@ -3274,6 +3274,30 @@ out:
return ret;
}
+int coroutine_fn bdrv_co_refresh_connection(BdrvChild *child, Error **errp)
+{
+ BlockDriverState *bs = child->bs;
+ BlockDriver *drv = bs->drv;
+ int ret;
+ IO_CODE();
+ assert_bdrv_graph_readable();
+
+ if (!drv) {
+ error_setg(errp, "No medium inserted");
+ return -ENOMEDIUM;
+ }
+
+ if (!drv->bdrv_co_refresh_connection) {
+ return 0;
+ }
+
+ bdrv_inc_in_flight(bs);
+ ret = drv->bdrv_co_refresh_connection(bs, errp);
+ bdrv_dec_in_flight(bs);
+
+ return ret;
+}
+
int coroutine_fn bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf)
{
BlockDriver *drv = bs->drv;
diff --git a/blockdev.c b/blockdev.c
index 0f70973326..5d6d618bce 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -2314,6 +2314,7 @@ void coroutine_fn qmp_block_resize(const char *device, const char *node_name,
BlockBackend *blk;
BlockDriverState *bs;
AioContext *old_ctx;
+ int ret;
bs = bdrv_lookup_bs(device, node_name, &local_err);
if (local_err) {
@@ -2338,6 +2339,14 @@ void coroutine_fn qmp_block_resize(const char *device, const char *node_name,
return;
}
+ old_ctx = bdrv_co_enter(bs);
+ ret = blk_co_refresh_connection(blk, errp);
+ bdrv_co_leave(bs, old_ctx);
+ if (ret < 0) {
+ blk_co_unref(blk);
+ return;
+ }
+
bdrv_drained_begin(bs);
old_ctx = bdrv_co_enter(bs);
diff --git a/include/block/block-io.h b/include/block/block-io.h
index f671476f08..5a7328fed5 100644
--- a/include/block/block-io.h
+++ b/include/block/block-io.h
@@ -114,6 +114,14 @@ int coroutine_fn GRAPH_RDLOCK bdrv_co_flush(BlockDriverState *bs);
int coroutine_fn GRAPH_RDLOCK bdrv_co_pdiscard(BdrvChild *child, int64_t offset,
int64_t bytes);
+/*
+ * Ensure the connection is ready to process requests by re-establishing it if
+ * closed.
+ * Returns 0 when the driver does not declare the callback.
+ */
+int coroutine_fn GRAPH_RDLOCK bdrv_co_refresh_connection(BdrvChild *child,
+ Error **errp);
+
/* Report zone information of zone block device. */
int coroutine_fn GRAPH_RDLOCK bdrv_co_zone_report(BlockDriverState *bs,
int64_t offset,
diff --git a/include/block/block_int-common.h b/include/block/block_int-common.h
index 0c7746a544..a0411e2947 100644
--- a/include/block/block_int-common.h
+++ b/include/block/block_int-common.h
@@ -591,6 +591,9 @@ struct BlockDriver {
int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pdiscard)(
BlockDriverState *bs, int64_t offset, int64_t bytes);
+ int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_refresh_connection)(
+ BlockDriverState *bs, Error **errp);
+
/*
* Map [offset, offset + nbytes) range onto a child of @bs to copy from,
* and invoke bdrv_co_copy_range_from(child, ...), or invoke
diff --git a/include/system/block-backend-io.h b/include/system/block-backend-io.h
index 78d410b8df..811264622c 100644
--- a/include/system/block-backend-io.h
+++ b/include/system/block-backend-io.h
@@ -238,4 +238,6 @@ int coroutine_fn blk_co_truncate(BlockBackend *blk, int64_t offset, bool exact,
PreallocMode prealloc, BdrvRequestFlags flags,
Error **errp);
+int coroutine_fn blk_co_refresh_connection(BlockBackend *blk, Error **errp);
+
#endif /* BLOCK_BACKEND_IO_H */
--
2.43.0
^ permalink raw reply related [flat|nested] 15+ messages in thread* Re: [PATCH 2/5] block: Refresh the connection before block_resize
2026-11-19 14:46 ` [PATCH 2/5] block: Refresh the connection before block_resize Guillaume Vincke
@ 2026-11-19 22:32 ` Eric Blake
2026-11-20 10:40 ` guillaume vincke
0 siblings, 1 reply; 15+ messages in thread
From: Eric Blake @ 2026-11-19 22:32 UTC (permalink / raw)
To: Guillaume Vincke
Cc: qemu-devel, qemu-block, vsementsov, kwolf, hreitz, stefanha, fam
On Thu, Nov 19, 2026 at 03:46:29PM +0100, Guillaume Vincke wrote:
> When a network based device is not connected, there is no way for the
> backend to notify a change. For instance, the NBD client relies on lazy
> reconnection. Therefore, if an NBD backend changes size, there is
> currently no way to update the device size until at least one I/O
> operation is sent.
>
> This patch adds the bdrv_co_refresh_connection callback to the
> BlockDriver structure so a reconnection can be forced. Considering
> nbd_co_truncate() does not perform any I/O, it also modifies the QMP
> block_resize implementation to execute the callback, if defined, before
> truncating.
>
> No functional change.
At least not until later patches start implementing the new
callback. But overall the explanation of why this callback is useful
makes sense to me.
>
> Signed-off-by: Guillaume Vincke <guillaume.vincke@ovhcloud.com>
> ---
> block/block-backend.c | 12 ++++++++++++
> block/io.c | 24 ++++++++++++++++++++++++
> blockdev.c | 9 +++++++++
> include/block/block-io.h | 8 ++++++++
> include/block/block_int-common.h | 3 +++
> include/system/block-backend-io.h | 2 ++
> 6 files changed, 58 insertions(+)
>
> diff --git a/block/block-backend.c b/block/block-backend.c
> index 656c0aed67..b133bb8292 100644
> --- a/block/block-backend.c
> +++ b/block/block-backend.c
> @@ -2600,6 +2600,18 @@ int coroutine_fn blk_co_truncate(BlockBackend *blk, int64_t offset, bool exact,
> return bdrv_co_truncate(blk->root, offset, exact, prealloc, flags, errp);
> }
>
> +int coroutine_fn blk_co_refresh_connection(BlockBackend *blk, Error **errp)
> +{
Check out scripts/git.orderfile for a way to list the .h changes (with
the documentation) ahead of the .c changes (with the implementation).
> +++ b/blockdev.c
> @@ -2314,6 +2314,7 @@ void coroutine_fn qmp_block_resize(const char *device, const char *node_name,
> BlockBackend *blk;
> BlockDriverState *bs;
> AioContext *old_ctx;
> + int ret;
>
> bs = bdrv_lookup_bs(device, node_name, &local_err);
> if (local_err) {
> @@ -2338,6 +2339,14 @@ void coroutine_fn qmp_block_resize(const char *device, const char *node_name,
> return;
> }
>
> + old_ctx = bdrv_co_enter(bs);
> + ret = blk_co_refresh_connection(blk, errp);
> + bdrv_co_leave(bs, old_ctx);
> + if (ret < 0) {
> + blk_co_unref(blk);
> + return;
> + }
> +
> bdrv_drained_begin(bs);
>
> old_ctx = bdrv_co_enter(bs);
Am I missing a reason for why you add a separate bdrv_co_enter/leave
critical section prior the bdrv_drained_begin, rather than reusing the
critical section already present during the drained region? Is it
because you want to ensure the (re-)connection is in place before the
drain can trigger IO traffic?
> diff --git a/include/block/block-io.h b/include/block/block-io.h
> index f671476f08..5a7328fed5 100644
> --- a/include/block/block-io.h
> +++ b/include/block/block-io.h
> @@ -114,6 +114,14 @@ int coroutine_fn GRAPH_RDLOCK bdrv_co_flush(BlockDriverState *bs);
> int coroutine_fn GRAPH_RDLOCK bdrv_co_pdiscard(BdrvChild *child, int64_t offset,
> int64_t bytes);
>
> +/*
> + * Ensure the connection is ready to process requests by re-establishing it if
> + * closed.
> + * Returns 0 when the driver does not declare the callback.
> + */
> +int coroutine_fn GRAPH_RDLOCK bdrv_co_refresh_connection(BdrvChild *child,
> + Error **errp);
Maybe word it as "Returns 0 when ready or if driver does not declare
the callback, and -errno on failure to reconnect."
--
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization: qemu.org | libguestfs.org
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH 2/5] block: Refresh the connection before block_resize
2026-11-19 22:32 ` Eric Blake
@ 2026-11-20 10:40 ` guillaume vincke
0 siblings, 0 replies; 15+ messages in thread
From: guillaume vincke @ 2026-11-20 10:40 UTC (permalink / raw)
To: eblake; +Cc: qemu-devel, qemu-block, vsementsov, kwolf, hreitz, stefanha, fam
On 11/19/26 23:32, Eric Blake wrote:
>> +++ b/blockdev.c
>> @@ -2314,6 +2314,7 @@ void coroutine_fn qmp_block_resize(const char *device, const char *node_name,
>> BlockBackend *blk;
>> BlockDriverState *bs;
>> AioContext *old_ctx;
>> + int ret;
>>
>> bs = bdrv_lookup_bs(device, node_name, &local_err);
>> if (local_err) {
>> @@ -2338,6 +2339,14 @@ void coroutine_fn qmp_block_resize(const char *device, const char *node_name,
>> return;
>> }
>>
>> + old_ctx = bdrv_co_enter(bs);
>> + ret = blk_co_refresh_connection(blk, errp);
>> + bdrv_co_leave(bs, old_ctx);
>> + if (ret < 0) {
>> + blk_co_unref(blk);
>> + return;
>> + }
>> +
>> bdrv_drained_begin(bs);
>>
>> old_ctx = bdrv_co_enter(bs);
>
> Am I missing a reason for why you add a separate bdrv_co_enter/leave
> critical section prior the bdrv_drained_begin, rather than reusing the
> critical section already present during the drained region? Is it
> because you want to ensure the (re-)connection is in place before the
> drain can trigger IO traffic?
>
There is no other reason than believing that this option is more
readable and that a purely additive diff is easier to review. After
running the tests, I believe moving it into the same section is a valid
solution. Will change this in v2.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 3/5] block/nbd: Refresh the connection on block_resize
2026-11-19 14:46 [PATCH 0/5] block/nbd: Fix block resize on idle device Guillaume Vincke
2026-11-19 14:46 ` [PATCH 1/5] block/nbd: Extract request slot access and reconnection Guillaume Vincke
2026-11-19 14:46 ` [PATCH 2/5] block: Refresh the connection before block_resize Guillaume Vincke
@ 2026-11-19 14:46 ` Guillaume Vincke
2026-11-19 16:25 ` Eric Blake
2026-11-19 14:46 ` [PATCH 4/5] iotests: Add coverage for NBD resize Guillaume Vincke
` (2 subsequent siblings)
5 siblings, 1 reply; 15+ messages in thread
From: Guillaume Vincke @ 2026-11-19 14:46 UTC (permalink / raw)
To: qemu-devel
Cc: qemu-block, eblake, vsementsov, kwolf, hreitz, stefanha, fam,
Guillaume Vincke
When an NBD device is resized, two steps are needed to propagate the
information. First, as the size is only exchanged at handshake, the
connection needs to be reset. Then, the new size needs to be updated by
calling bdrv_co_refresh_total_sectors().
The QMP block_resize method, by calling bdrv_co_truncate(), takes care
of the second step but, considering the NBD connection is lazy, nothing
deals with reconnection when the device is idle.
This patch uses the new bdrv_co_refresh_connection() to force the
handshake by manually starting the reconnection routine.
It creates a clean flow to resize an NBD-backed block device on the
server side by closing the connection and calling block_resize.
When the node is configured with reconnect-delay=0, refreshing starts
the reconnection but returns immediately with -EIO. The management
application can handle this case by waiting for the connection to be
established and re-running block_resize.
To detect disconnection, we peek the channel. This does not detect dead
peers or disconnected TLS-secured connections. Therefore, this flow
will not work if the server does not disconnect cleanly or if the
communication is encrypted.
Signed-off-by: Guillaume Vincke <guillaume.vincke@ovhcloud.com>
---
block/nbd.c | 39 +++++++++++++++++++++++++++++++++++++++
include/block/nbd.h | 1 +
nbd/client-connection.c | 7 +++++--
3 files changed, 45 insertions(+), 2 deletions(-)
diff --git a/block/nbd.c b/block/nbd.c
index df72b17ade..759a6ef480 100644
--- a/block/nbd.c
+++ b/block/nbd.c
@@ -2081,6 +2081,42 @@ static int coroutine_fn nbd_co_truncate(BlockDriverState *bs, int64_t offset,
return 0;
}
+/*
+ * Start the reconnection routine unless the node is connected and its channel
+ * is still open. When used with a zero reconnect-delay, this function
+ * triggers the reconnection and returns -EIO.
+ */
+static int coroutine_fn GRAPH_RDLOCK
+nbd_co_refresh_connection(BlockDriverState *bs, Error **errp)
+{
+ BDRVNBDState *s = bs->opaque;
+ int ret;
+
+ QEMU_LOCK_GUARD(&s->requests_lock);
+
+ if (s->state == NBD_CLIENT_QUIT) {
+ error_setg(errp, "Cannot refresh a permanently unavailable NBD node");
+ return -ECONNREFUSED;
+ }
+
+ if (s->state == NBD_CLIENT_CONNECTED) {
+ if (!nbd_channel_is_closed(s->ioc)) {
+ return 0;
+ }
+ /* A connection closed by its peer is treated as an error */
+ nbd_channel_error_locked(s, -EIO);
+ }
+
+ nbd_co_acquire_slot(s);
+ ret = nbd_co_reconnect(s);
+ nbd_co_release_slot(s);
+
+ if (ret < 0) {
+ error_setg(errp, "Failed to reconnect NBD node");
+ }
+ return ret;
+}
+
static int64_t coroutine_fn nbd_co_getlength(BlockDriverState *bs)
{
BDRVNBDState *s = bs->opaque;
@@ -2205,6 +2241,7 @@ static BlockDriver bdrv_nbd = {
.bdrv_co_pdiscard = nbd_client_co_pdiscard,
.bdrv_refresh_limits = nbd_refresh_limits,
.bdrv_co_truncate = nbd_co_truncate,
+ .bdrv_co_refresh_connection = nbd_co_refresh_connection,
.bdrv_co_getlength = nbd_co_getlength,
.bdrv_refresh_filename = nbd_refresh_filename,
.bdrv_co_block_status = nbd_client_co_block_status,
@@ -2233,6 +2270,7 @@ static BlockDriver bdrv_nbd_tcp = {
.bdrv_co_pdiscard = nbd_client_co_pdiscard,
.bdrv_refresh_limits = nbd_refresh_limits,
.bdrv_co_truncate = nbd_co_truncate,
+ .bdrv_co_refresh_connection = nbd_co_refresh_connection,
.bdrv_co_getlength = nbd_co_getlength,
.bdrv_refresh_filename = nbd_refresh_filename,
.bdrv_co_block_status = nbd_client_co_block_status,
@@ -2261,6 +2299,7 @@ static BlockDriver bdrv_nbd_unix = {
.bdrv_co_pdiscard = nbd_client_co_pdiscard,
.bdrv_refresh_limits = nbd_refresh_limits,
.bdrv_co_truncate = nbd_co_truncate,
+ .bdrv_co_refresh_connection = nbd_co_refresh_connection,
.bdrv_co_getlength = nbd_co_getlength,
.bdrv_refresh_filename = nbd_refresh_filename,
.bdrv_co_block_status = nbd_client_co_block_status,
diff --git a/include/block/nbd.h b/include/block/nbd.h
index 15ddec91ac..b459788534 100644
--- a/include/block/nbd.h
+++ b/include/block/nbd.h
@@ -508,6 +508,7 @@ NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr,
QCryptoTLSCreds *tlscreds,
const char *tlshostname);
void nbd_client_connection_release(NBDClientConnection *conn);
+bool nbd_channel_is_closed(QIOChannel *ioc);
QIOChannel *coroutine_fn
nbd_co_establish_connection(NBDClientConnection *conn, NBDExportInfo *info,
diff --git a/nbd/client-connection.c b/nbd/client-connection.c
index 78caea3c37..e6e346d9fc 100644
--- a/nbd/client-connection.c
+++ b/nbd/client-connection.c
@@ -296,8 +296,11 @@ void nbd_client_connection_release(NBDClientConnection *conn)
}
}
-/* Only true when the peer closed the channel. Does not detect if dead. */
-static bool nbd_channel_is_closed(QIOChannel *ioc)
+/*
+ * Only true when the peer closed the channel. Always returns false for TLS
+ * channels. Does not detect if dead.
+ */
+bool nbd_channel_is_closed(QIOChannel *ioc)
{
char data;
struct iovec iov = { .iov_base = &data, .iov_len = 1 };
--
2.43.0
^ permalink raw reply related [flat|nested] 15+ messages in thread* Re: [PATCH 3/5] block/nbd: Refresh the connection on block_resize
2026-11-19 14:46 ` [PATCH 3/5] block/nbd: Refresh the connection on block_resize Guillaume Vincke
@ 2026-11-19 16:25 ` Eric Blake
2026-11-20 10:12 ` guillaume vincke
0 siblings, 1 reply; 15+ messages in thread
From: Eric Blake @ 2026-11-19 16:25 UTC (permalink / raw)
To: Guillaume Vincke
Cc: qemu-devel, qemu-block, vsementsov, kwolf, hreitz, stefanha, fam
On Thu, Nov 19, 2026 at 03:46:30PM +0100, Guillaume Vincke wrote:
> When an NBD device is resized, two steps are needed to propagate the
> information. First, as the size is only exchanged at handshake, the
> connection needs to be reset. Then, the new size needs to be updated by
> calling bdrv_co_refresh_total_sectors().
> The QMP block_resize method, by calling bdrv_co_truncate(), takes care
> of the second step but, considering the NBD connection is lazy, nothing
> deals with reconnection when the device is idle.
>
> This patch uses the new bdrv_co_refresh_connection() to force the
> handshake by manually starting the reconnection routine.
> It creates a clean flow to resize an NBD-backed block device on the
> server side by closing the connection and calling block_resize.
> When the node is configured with reconnect-delay=0, refreshing starts
> the reconnection but returns immediately with -EIO. The management
> application can handle this case by waiting for the connection to be
> established and re-running block_resize.
>
> To detect disconnection, we peek the channel. This does not detect dead
> peers or disconnected TLS-secured connections. Therefore, this flow
> will not work if the server does not disconnect cleanly or if the
> communication is encrypted.
This is problematic, since unencrypted traffic is more likely to trip
over denial of service CVE attacks; is there any way we can make
client disconnect easier to detect even for TLS clients?
>
> Signed-off-by: Guillaume Vincke <guillaume.vincke@ovhcloud.com>
> ---
> +++ b/nbd/client-connection.c
> @@ -296,8 +296,11 @@ void nbd_client_connection_release(NBDClientConnection *conn)
> }
> }
>
> -/* Only true when the peer closed the channel. Does not detect if dead. */
> -static bool nbd_channel_is_closed(QIOChannel *ioc)
> +/*
> + * Only true when the peer closed the channel. Always returns false for TLS
> + * channels. Does not detect if dead.
> + */
> +bool nbd_channel_is_closed(QIOChannel *ioc)
> {
> char data;
> struct iovec iov = { .iov_base = &data, .iov_len = 1 };
> --
> 2.43.0
>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization: qemu.org | libguestfs.org
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH 3/5] block/nbd: Refresh the connection on block_resize
2026-11-19 16:25 ` Eric Blake
@ 2026-11-20 10:12 ` guillaume vincke
0 siblings, 0 replies; 15+ messages in thread
From: guillaume vincke @ 2026-11-20 10:12 UTC (permalink / raw)
To: eblake; +Cc: qemu-devel, qemu-block, vsementsov, kwolf, hreitz, stefanha, fam
On 11/19/26 17:25, Eric Blake wrote:
> On Thu, Nov 19, 2026 at 03:46:30PM +0100, Guillaume Vincke wrote:
>> When an NBD device is resized, two steps are needed to propagate the
>> information. First, as the size is only exchanged at handshake, the
>> connection needs to be reset. Then, the new size needs to be updated by
>> calling bdrv_co_refresh_total_sectors().
>> The QMP block_resize method, by calling bdrv_co_truncate(), takes care
>> of the second step but, considering the NBD connection is lazy, nothing
>> deals with reconnection when the device is idle.
>>
>> This patch uses the new bdrv_co_refresh_connection() to force the
>> handshake by manually starting the reconnection routine.
>> It creates a clean flow to resize an NBD-backed block device on the
>> server side by closing the connection and calling block_resize.
>> When the node is configured with reconnect-delay=0, refreshing starts
>> the reconnection but returns immediately with -EIO. The management
>> application can handle this case by waiting for the connection to be
>> established and re-running block_resize.
>>
>> To detect disconnection, we peek the channel. This does not detect dead
>> peers or disconnected TLS-secured connections. Therefore, this flow
>> will not work if the server does not disconnect cleanly or if the
>> communication is encrypted.
>
> This is problematic, since unencrypted traffic is more likely to trip
> over denial of service CVE attacks; is there any way we can make
> client disconnect easier to detect even for TLS clients?
>
I have to admit I focused on my use case but you are right about the
CVE risk. I will suggest an implementation that supports TLS in v2.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 4/5] iotests: Add coverage for NBD resize
2026-11-19 14:46 [PATCH 0/5] block/nbd: Fix block resize on idle device Guillaume Vincke
` (2 preceding siblings ...)
2026-11-19 14:46 ` [PATCH 3/5] block/nbd: Refresh the connection on block_resize Guillaume Vincke
@ 2026-11-19 14:46 ` Guillaume Vincke
2026-11-19 22:51 ` Eric Blake
2026-11-19 14:46 ` [PATCH 5/5] block: Add refresh connection support to raw and luks Guillaume Vincke
2026-11-19 16:20 ` [PATCH 0/5] block/nbd: Fix block resize on idle device Eric Blake
5 siblings, 1 reply; 15+ messages in thread
From: Guillaume Vincke @ 2026-11-19 14:46 UTC (permalink / raw)
To: qemu-devel
Cc: qemu-block, eblake, vsementsov, kwolf, hreitz, stefanha, fam,
Guillaume Vincke
Test the block_resize QMP function on an NBD device. Prior to the
addition of bdrv_co_refresh_connection(), running block_resize on a
node that did not process any requests between the device size
update and the function call failed with "Cannot grow NBD nodes".
Add a test that updates an NBD node size and verifies that the
block_resize behaves as expected. The first part configures a reconnect
delay and asserts that the call succeeds. The second uses the default
reconnect-delay=0 and asserts that, even though the function returns an
error, the reconnection routine gets started and that retrying succeeds
once the node has successfully reconnected.
Signed-off-by: Guillaume Vincke <guillaume.vincke@ovhcloud.com>
---
tests/qemu-iotests/tests/nbd-resize | 81 +++++++++++++++++++++++++
tests/qemu-iotests/tests/nbd-resize.out | 24 ++++++++
2 files changed, 105 insertions(+)
create mode 100755 tests/qemu-iotests/tests/nbd-resize
create mode 100644 tests/qemu-iotests/tests/nbd-resize.out
diff --git a/tests/qemu-iotests/tests/nbd-resize b/tests/qemu-iotests/tests/nbd-resize
new file mode 100755
index 0000000000..4f305eaebf
--- /dev/null
+++ b/tests/qemu-iotests/tests/nbd-resize
@@ -0,0 +1,81 @@
+#!/usr/bin/env python3
+# group: quick
+#
+# Test the NBD resize
+#
+# Copyright (C) 2026 OVHcloud
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+import time
+import iotests
+
+iotests.script_initialize(supported_fmts=['qcow2'])
+nbd_sock = iotests.file_path('nbd-sock', base_dir=iotests.sock_dir)
+disk = iotests.file_path('disk')
+node_name = 'nbd0'
+
+
+def node_size(virtm):
+ for node in virtm.cmd('query-named-block-nodes', flat=True):
+ if node['node-name'] == node_name:
+ return node['image']['virtual-size']
+ return None
+
+
+def wait_for_parked_connection(virtm):
+ with iotests.Timeout(30, "connect-thread did not reach 'parked' state"):
+ while True:
+ client = virtm.qmp('x-debug-query-nbd-client', node_name=node_name)
+ if client['return']['connector']['state'] == 'parked':
+ return
+ time.sleep(0.1)
+
+
+for reconnect_delay in [10, 0]:
+ iotests.log(f"=== Resize with reconnect-delay={reconnect_delay} ===")
+ with iotests.VM() as vm:
+ vm.launch()
+
+ iotests.qemu_img_create('-f', iotests.imgfmt, disk, '1M')
+ with iotests.qemu_nbd_popen('-k',
+ nbd_sock,
+ '-f',
+ iotests.imgfmt,
+ disk):
+ vm.cmd('blockdev-add', {
+ 'driver': 'nbd',
+ 'node-name': node_name,
+ 'server': { 'type': 'unix', 'path': nbd_sock },
+ 'reconnect-delay': reconnect_delay,
+ })
+
+ iotests.log('Size before resize:')
+ iotests.log(node_size(vm))
+
+ iotests.qemu_img('resize', '-f', iotests.imgfmt, disk, '2M')
+ with iotests.qemu_nbd_popen('-k',
+ nbd_sock,
+ '-f',
+ iotests.imgfmt,
+ disk):
+ iotests.log(vm.qmp('block_resize', {
+ 'node-name': node_name,
+ 'size': 2097152,
+ }))
+ iotests.log('Size after resize:')
+ iotests.log(node_size(vm))
+ if reconnect_delay == 0:
+ # With delay=0, block_resize fails while the client reconnects.
+ # Retry once parked.
+ iotests.log('Waiting until the reconnection succeeds')
+ wait_for_parked_connection(vm)
+
+ iotests.log(vm.qmp('block_resize', {
+ 'node-name': node_name,
+ 'size': 2097152,
+ }))
+ iotests.log('Size after retried resize:')
+ iotests.log(node_size(vm))
+
+ vm.shutdown()
diff --git a/tests/qemu-iotests/tests/nbd-resize.out b/tests/qemu-iotests/tests/nbd-resize.out
new file mode 100644
index 0000000000..89eda39740
--- /dev/null
+++ b/tests/qemu-iotests/tests/nbd-resize.out
@@ -0,0 +1,24 @@
+=== Resize with reconnect-delay=10 ===
+Start NBD server
+Size before resize:
+1048576
+Kill NBD server
+Start NBD server
+{"return": {}}
+Size after resize:
+2097152
+Kill NBD server
+=== Resize with reconnect-delay=0 ===
+Start NBD server
+Size before resize:
+1048576
+Kill NBD server
+Start NBD server
+{"error": {"class": "GenericError", "desc": "Failed to reconnect NBD node"}}
+Size after resize:
+1048576
+Waiting until the reconnection succeeds
+{"return": {}}
+Size after retried resize:
+2097152
+Kill NBD server
--
2.43.0
^ permalink raw reply related [flat|nested] 15+ messages in thread* Re: [PATCH 4/5] iotests: Add coverage for NBD resize
2026-11-19 14:46 ` [PATCH 4/5] iotests: Add coverage for NBD resize Guillaume Vincke
@ 2026-11-19 22:51 ` Eric Blake
2026-11-20 10:17 ` guillaume vincke
0 siblings, 1 reply; 15+ messages in thread
From: Eric Blake @ 2026-11-19 22:51 UTC (permalink / raw)
To: Guillaume Vincke
Cc: qemu-devel, qemu-block, vsementsov, kwolf, hreitz, stefanha, fam
On Thu, Nov 19, 2026 at 03:46:31PM +0100, Guillaume Vincke wrote:
> Test the block_resize QMP function on an NBD device. Prior to the
> addition of bdrv_co_refresh_connection(), running block_resize on a
> node that did not process any requests between the device size
> update and the function call failed with "Cannot grow NBD nodes".
>
> Add a test that updates an NBD node size and verifies that the
> block_resize behaves as expected. The first part configures a reconnect
> delay and asserts that the call succeeds. The second uses the default
> reconnect-delay=0 and asserts that, even though the function returns an
> error, the reconnection routine gets started and that retrying succeeds
> once the node has successfully reconnected.
>
> Signed-off-by: Guillaume Vincke <guillaume.vincke@ovhcloud.com>
> ---
> tests/qemu-iotests/tests/nbd-resize | 81 +++++++++++++++++++++++++
> tests/qemu-iotests/tests/nbd-resize.out | 24 ++++++++
> 2 files changed, 105 insertions(+)
> create mode 100755 tests/qemu-iotests/tests/nbd-resize
> create mode 100644 tests/qemu-iotests/tests/nbd-resize.out
>
> diff --git a/tests/qemu-iotests/tests/nbd-resize b/tests/qemu-iotests/tests/nbd-resize
> new file mode 100755
> index 0000000000..4f305eaebf
> --- /dev/null
> +++ b/tests/qemu-iotests/tests/nbd-resize
> @@ -0,0 +1,81 @@
> +#!/usr/bin/env python3
> +# group: quick
Can this also go in the 'auto' group?
> +
> +for reconnect_delay in [10, 0]:
> + iotests.log(f"=== Resize with reconnect-delay={reconnect_delay} ===")
> + with iotests.VM() as vm:
> + vm.launch()
> +
> + iotests.qemu_img_create('-f', iotests.imgfmt, disk, '1M')
> + with iotests.qemu_nbd_popen('-k',
> + nbd_sock,
> + '-f',
> + iotests.imgfmt,
> + disk):
> + vm.cmd('blockdev-add', {
> + 'driver': 'nbd',
> + 'node-name': node_name,
> + 'server': { 'type': 'unix', 'path': nbd_sock },
> + 'reconnect-delay': reconnect_delay,
> + })
> +
> + iotests.log('Size before resize:')
> + iotests.log(node_size(vm))
> +
> + iotests.qemu_img('resize', '-f', iotests.imgfmt, disk, '2M')
> + with iotests.qemu_nbd_popen('-k',
> + nbd_sock,
> + '-f',
> + iotests.imgfmt,
> + disk):
> + iotests.log(vm.qmp('block_resize', {
> + 'node-name': node_name,
> + 'size': 2097152,
> + }))
> + iotests.log('Size after resize:')
> + iotests.log(node_size(vm))
After growing the image from 1M to 2M, is it also worth doing IO to
offsets in the newly-expanded part of the file to prove the resized
region is accessible?
> + if reconnect_delay == 0:
> + # With delay=0, block_resize fails while the client reconnects.
> + # Retry once parked.
> + iotests.log('Waiting until the reconnection succeeds')
> + wait_for_parked_connection(vm)
> +
> + iotests.log(vm.qmp('block_resize', {
> + 'node-name': node_name,
> + 'size': 2097152,
> + }))
> + iotests.log('Size after retried resize:')
> + iotests.log(node_size(vm))
> +
> + vm.shutdown()
> diff --git a/tests/qemu-iotests/tests/nbd-resize.out b/tests/qemu-iotests/tests/nbd-resize.out
> new file mode 100644
> index 0000000000..89eda39740
> --- /dev/null
> +++ b/tests/qemu-iotests/tests/nbd-resize.out
> @@ -0,0 +1,24 @@
> +=== Resize with reconnect-delay=10 ===
> +Start NBD server
> +Size before resize:
> +1048576
> +Kill NBD server
> +Start NBD server
> +{"return": {}}
> +Size after resize:
> +2097152
> +Kill NBD server
> +=== Resize with reconnect-delay=0 ===
> +Start NBD server
> +Size before resize:
> +1048576
> +Kill NBD server
> +Start NBD server
> +{"error": {"class": "GenericError", "desc": "Failed to reconnect NBD node"}}
> +Size after resize:
> +1048576
> +Waiting until the reconnection succeeds
> +{"return": {}}
> +Size after retried resize:
> +2097152
> +Kill NBD server
> --
> 2.43.0
>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization: qemu.org | libguestfs.org
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH 4/5] iotests: Add coverage for NBD resize
2026-11-19 22:51 ` Eric Blake
@ 2026-11-20 10:17 ` guillaume vincke
0 siblings, 0 replies; 15+ messages in thread
From: guillaume vincke @ 2026-11-20 10:17 UTC (permalink / raw)
To: eblake; +Cc: qemu-devel, qemu-block, vsementsov, kwolf, hreitz, stefanha, fam
On 11/19/26 23:51, Eric Blake wrote:
> On Thu, Nov 19, 2026 at 03:46:31PM +0100, Guillaume Vincke wrote:
>> Test the block_resize QMP function on an NBD device. Prior to the
>> addition of bdrv_co_refresh_connection(), running block_resize on a
>> node that did not process any requests between the device size
>> update and the function call failed with "Cannot grow NBD nodes".
>>
>> Add a test that updates an NBD node size and verifies that the
>> block_resize behaves as expected. The first part configures a reconnect
>> delay and asserts that the call succeeds. The second uses the default
>> reconnect-delay=0 and asserts that, even though the function returns an
>> error, the reconnection routine gets started and that retrying succeeds
>> once the node has successfully reconnected.
>>
>> Signed-off-by: Guillaume Vincke <guillaume.vincke@ovhcloud.com>
>> ---
>> tests/qemu-iotests/tests/nbd-resize | 81 +++++++++++++++++++++++++
>> tests/qemu-iotests/tests/nbd-resize.out | 24 ++++++++
>> 2 files changed, 105 insertions(+)
>> create mode 100755 tests/qemu-iotests/tests/nbd-resize
>> create mode 100644 tests/qemu-iotests/tests/nbd-resize.out
>>
>> diff --git a/tests/qemu-iotests/tests/nbd-resize b/tests/qemu-iotests/tests/nbd-resize
>> new file mode 100755
>> index 0000000000..4f305eaebf
>> --- /dev/null
>> +++ b/tests/qemu-iotests/tests/nbd-resize
>> @@ -0,0 +1,81 @@
>> +#!/usr/bin/env python3
>> +# group: quick
>
> Can this also go in the 'auto' group?
>
I believe it can. Will add it in v2.
>> +
>> +for reconnect_delay in [10, 0]:
>> + iotests.log(f"=== Resize with reconnect-delay={reconnect_delay} ===")
>> + with iotests.VM() as vm:
>> + vm.launch()
>> +
>> + iotests.qemu_img_create('-f', iotests.imgfmt, disk, '1M')
>> + with iotests.qemu_nbd_popen('-k',
>> + nbd_sock,
>> + '-f',
>> + iotests.imgfmt,
>> + disk):
>> + vm.cmd('blockdev-add', {
>> + 'driver': 'nbd',
>> + 'node-name': node_name,
>> + 'server': { 'type': 'unix', 'path': nbd_sock },
>> + 'reconnect-delay': reconnect_delay,
>> + })
>> +
>> + iotests.log('Size before resize:')
>> + iotests.log(node_size(vm))
>> +
>> + iotests.qemu_img('resize', '-f', iotests.imgfmt, disk, '2M')
>> + with iotests.qemu_nbd_popen('-k',
>> + nbd_sock,
>> + '-f',
>> + iotests.imgfmt,
>> + disk):
>> + iotests.log(vm.qmp('block_resize', {
>> + 'node-name': node_name,
>> + 'size': 2097152,
>> + }))
>> + iotests.log('Size after resize:')
>> + iotests.log(node_size(vm))
>
> After growing the image from 1M to 2M, is it also worth doing IO to
> offsets in the newly-expanded part of the file to prove the resized
> region is accessible?
>
Doing I/O does not exercise the current patchset but you are right that
we can expect it out of a proper resize test. Will add this in v2.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 5/5] block: Add refresh connection support to raw and luks
2026-11-19 14:46 [PATCH 0/5] block/nbd: Fix block resize on idle device Guillaume Vincke
` (3 preceding siblings ...)
2026-11-19 14:46 ` [PATCH 4/5] iotests: Add coverage for NBD resize Guillaume Vincke
@ 2026-11-19 14:46 ` Guillaume Vincke
2026-11-19 16:20 ` [PATCH 0/5] block/nbd: Fix block resize on idle device Eric Blake
5 siblings, 0 replies; 15+ messages in thread
From: Guillaume Vincke @ 2026-11-19 14:46 UTC (permalink / raw)
To: qemu-devel
Cc: qemu-block, eblake, vsementsov, kwolf, hreitz, stefanha, fam,
Guillaume Vincke
The bdrv_co_refresh_connection callback is used to force an idle NBD
client to reconnect before the node is truncated. However, when a
format node such as luks or raw is on top of the NBD node, the call is
not forwarded.
Implement the bdrv_co_refresh_connection callback in raw and luks to
forward the request to their file child.
Update the nbd-resize test to exercise it with luks and raw format
drivers.
Signed-off-by: Guillaume Vincke <guillaume.vincke@ovhcloud.com>
---
block/crypto.c | 7 ++++
block/raw-format.c | 7 ++++
tests/qemu-iotests/tests/nbd-resize | 51 ++++++++++++++++++++++++-----
3 files changed, 57 insertions(+), 8 deletions(-)
diff --git a/block/crypto.c b/block/crypto.c
index 8a34d92fde..7e13a3a917 100644
--- a/block/crypto.c
+++ b/block/crypto.c
@@ -462,6 +462,12 @@ block_crypto_co_truncate(BlockDriverState *bs, int64_t offset, bool exact,
return bdrv_co_truncate(bs->file, offset, exact, prealloc, 0, errp);
}
+static int coroutine_fn GRAPH_RDLOCK
+block_crypto_co_refresh_connection(BlockDriverState *bs, Error **errp)
+{
+ return bdrv_co_refresh_connection(bs->file, errp);
+}
+
static void block_crypto_close(BlockDriverState *bs)
{
BlockCrypto *crypto = bs->opaque;
@@ -1086,6 +1092,7 @@ static BlockDriver bdrv_crypto_luks = {
.bdrv_co_create = block_crypto_co_create_luks,
.bdrv_co_create_opts = block_crypto_co_create_opts_luks,
.bdrv_co_truncate = block_crypto_co_truncate,
+ .bdrv_co_refresh_connection = block_crypto_co_refresh_connection,
.create_opts = &block_crypto_create_opts_luks,
.amend_opts = &block_crypto_amend_opts_luks,
diff --git a/block/raw-format.c b/block/raw-format.c
index a57c2922d5..389ab16201 100644
--- a/block/raw-format.c
+++ b/block/raw-format.c
@@ -432,6 +432,12 @@ raw_co_truncate(BlockDriverState *bs, int64_t offset, bool exact,
return bdrv_co_truncate(bs->file, offset, exact, prealloc, flags, errp);
}
+static int coroutine_fn GRAPH_RDLOCK
+raw_co_refresh_connection(BlockDriverState *bs, Error **errp)
+{
+ return bdrv_co_refresh_connection(bs->file, errp);
+}
+
static void coroutine_fn GRAPH_RDLOCK
raw_co_eject(BlockDriverState *bs, bool eject_flag)
{
@@ -657,6 +663,7 @@ BlockDriver bdrv_raw = {
.bdrv_co_copy_range_from = &raw_co_copy_range_from,
.bdrv_co_copy_range_to = &raw_co_copy_range_to,
.bdrv_co_truncate = &raw_co_truncate,
+ .bdrv_co_refresh_connection = &raw_co_refresh_connection,
.bdrv_co_getlength = &raw_co_getlength,
.is_format = true,
.bdrv_measure = &raw_measure,
diff --git a/tests/qemu-iotests/tests/nbd-resize b/tests/qemu-iotests/tests/nbd-resize
index 4f305eaebf..ac283c74e5 100755
--- a/tests/qemu-iotests/tests/nbd-resize
+++ b/tests/qemu-iotests/tests/nbd-resize
@@ -10,10 +10,34 @@
import time
import iotests
-iotests.script_initialize(supported_fmts=['qcow2'])
+iotests.script_initialize(supported_fmts=['qcow2', 'raw', 'luks'])
nbd_sock = iotests.file_path('nbd-sock', base_dir=iotests.sock_dir)
disk = iotests.file_path('disk')
-node_name = 'nbd0'
+nbd_node_name = 'nbd0'
+
+qemu_nbd_volume_format = iotests.imgfmt
+node_name = nbd_node_name
+format_driver_extra_opts: dict[str, object] = {}
+
+if iotests.imgfmt == 'raw':
+ node_name = f"raw_{node_name}"
+
+elif iotests.imgfmt == 'luks':
+ qemu_nbd_volume_format = 'raw'
+ node_name = f"luks_{node_name}"
+ format_driver_extra_opts = { 'key-secret': 'keysec0' }
+
+
+def create_img():
+ iotests.qemu_img_create('-f', iotests.imgfmt, disk, '1M')
+
+
+def resize_img():
+ if iotests.imgfmt == 'qcow2':
+ iotests.qemu_img('resize', '-f', iotests.imgfmt, disk, '2M')
+ else:
+ # raw and luks virtual size follows the file size
+ iotests.qemu_img('resize', '-f', 'raw', disk, '+1M')
def node_size(virtm):
@@ -26,7 +50,8 @@ def node_size(virtm):
def wait_for_parked_connection(virtm):
with iotests.Timeout(30, "connect-thread did not reach 'parked' state"):
while True:
- client = virtm.qmp('x-debug-query-nbd-client', node_name=node_name)
+ client = virtm.qmp('x-debug-query-nbd-client',
+ node_name=nbd_node_name)
if client['return']['connector']['state'] == 'parked':
return
time.sleep(0.1)
@@ -35,29 +60,39 @@ def wait_for_parked_connection(virtm):
for reconnect_delay in [10, 0]:
iotests.log(f"=== Resize with reconnect-delay={reconnect_delay} ===")
with iotests.VM() as vm:
+ if iotests.imgfmt == 'luks':
+ vm.add_object(iotests.luks_default_secret_object)
+
vm.launch()
- iotests.qemu_img_create('-f', iotests.imgfmt, disk, '1M')
+ create_img()
with iotests.qemu_nbd_popen('-k',
nbd_sock,
'-f',
- iotests.imgfmt,
+ qemu_nbd_volume_format,
disk):
vm.cmd('blockdev-add', {
'driver': 'nbd',
- 'node-name': node_name,
+ 'node-name': nbd_node_name,
'server': { 'type': 'unix', 'path': nbd_sock },
'reconnect-delay': reconnect_delay,
})
+ if iotests.imgfmt != 'qcow2':
+ vm.cmd('blockdev-add', {
+ 'driver': iotests.imgfmt,
+ 'node-name': node_name,
+ 'file': nbd_node_name,
+ } | format_driver_extra_opts)
+
iotests.log('Size before resize:')
iotests.log(node_size(vm))
- iotests.qemu_img('resize', '-f', iotests.imgfmt, disk, '2M')
+ resize_img()
with iotests.qemu_nbd_popen('-k',
nbd_sock,
'-f',
- iotests.imgfmt,
+ qemu_nbd_volume_format,
disk):
iotests.log(vm.qmp('block_resize', {
'node-name': node_name,
--
2.43.0
^ permalink raw reply related [flat|nested] 15+ messages in thread* Re: [PATCH 0/5] block/nbd: Fix block resize on idle device
2026-11-19 14:46 [PATCH 0/5] block/nbd: Fix block resize on idle device Guillaume Vincke
` (4 preceding siblings ...)
2026-11-19 14:46 ` [PATCH 5/5] block: Add refresh connection support to raw and luks Guillaume Vincke
@ 2026-11-19 16:20 ` Eric Blake
2026-11-20 10:04 ` guillaume vincke
5 siblings, 1 reply; 15+ messages in thread
From: Eric Blake @ 2026-11-19 16:20 UTC (permalink / raw)
To: Guillaume Vincke
Cc: qemu-devel, qemu-block, vsementsov, kwolf, hreitz, stefanha, fam,
nbd
[cc NBD list for a protocol discussion]
On Thu, Nov 19, 2026 at 03:46:27PM +0100, Guillaume Vincke wrote:
> When using an NBD backed device, the size is exchanged during
> handshake. Therefore, when a volume gets resized on the target, it
> closes its clients' connections to force a new handshake. Then,
> management like OpenStack calls the block_resize QMP command to update
> the node size.
> This method works on active devices but, considering the NBD client
> reconnection is lazy, the handshake does not happen on idle ones which
> leads to a "Cannot grow NBD nodes" error when calling block_resize.
>
> This patchset adds a function to manually trigger reconnection on an
> NBD device and calls it in the block_resize QMP command to make sure
> the new size gets exchanged before trying to update the device
> internals. It also implements the refresh callback in the raw and luks
> drivers, which forward it to their file child.
Before I even review this series, I wonder if it is time to consider
reviving discussion on the upstream NBD protocol mailing list about an
extension to allow NBD to optionally support resize messages.
If I recall the last time it was discussed several years ago, there
are competing needs: some scenarios want resize to be driven by the
server (for example, in response to a high-water write event done by
the client, the server detects that storage should grow; now the
server needs to advertise to the client that the exported volume is
larger); and some want it to be driven by the client (the client wants
to specifically ask the server for the export size to grow; the server
may or may not be able to comply). NBD is normally client-driven, so
a client asking the server for a new size is easy to implement by
documenting a new NBD_CMD_RESIZE (in fact, opcode 8 is already
reserved for this in the NBD protocol document).
But the server being able to advertise a new size to the client is an
event-driven task, not client-driven. Here, structured commands and
the fact that the protocol allows out-of-order responses appears to
provide a nice solution - the idea was that a new command initiated by
the client requests an open-ended registration of resize events, and
the server can reply with as many chunks as wanted over a long length
of time reusing the client's cookie, and not using the
NBD_REPLY_FLAG_DONE flag to end the stream of reply chunks until the
client deregisters from the request (either two new NBD_CMD to
register/deregister, or one command with a client flag to request
registration or deregistration).
Ultimately, having NBD able to support live resizes will be even nicer
than forcing client reconnections just to learn a new server size.
That said, your series probably still has merit, because even if we
extend the NBD protocol to allow live resize, it may take a while
before both client and server ends are upgraded to understand the new
protocol extension, while this reconnection should work with older
protocol.
>
> Based-on: <20261105164124.249395-1-guillaume.vincke@ovhcloud.com>
> Needed to detect if the channel was closed by the server.
>
> Based-on: <20261113140852.702959-1-guillaume.vincke@ovhcloud.com>
> Needed to implement a deterministic test.
Based on the dependencies, this is definitely qemu 12.0 material, not
11.2; but that shouldn't stop me from starting review now.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization: qemu.org | libguestfs.org
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH 0/5] block/nbd: Fix block resize on idle device
2026-11-19 16:20 ` [PATCH 0/5] block/nbd: Fix block resize on idle device Eric Blake
@ 2026-11-20 10:04 ` guillaume vincke
0 siblings, 0 replies; 15+ messages in thread
From: guillaume vincke @ 2026-11-20 10:04 UTC (permalink / raw)
To: eblake
Cc: qemu-devel, qemu-block, vsementsov, kwolf, hreitz, stefanha, fam,
nbd
On 11/19/26 17:20, Eric Blake wrote:
> [cc NBD list for a protocol discussion]
>
> On Thu, Nov 19, 2026 at 03:46:27PM +0100, Guillaume Vincke wrote:
>> When using an NBD backed device, the size is exchanged during
>> handshake. Therefore, when a volume gets resized on the target, it
>> closes its clients' connections to force a new handshake. Then,
>> management like OpenStack calls the block_resize QMP command to update
>> the node size.
>> This method works on active devices but, considering the NBD client
>> reconnection is lazy, the handshake does not happen on idle ones which
>> leads to a "Cannot grow NBD nodes" error when calling block_resize.
>>
>> This patchset adds a function to manually trigger reconnection on an
>> NBD device and calls it in the block_resize QMP command to make sure
>> the new size gets exchanged before trying to update the device
>> internals. It also implements the refresh callback in the raw and luks
>> drivers, which forward it to their file child.
>
> Before I even review this series, I wonder if it is time to consider
> reviving discussion on the upstream NBD protocol mailing list about an
> extension to allow NBD to optionally support resize messages.
>
> If I recall the last time it was discussed several years ago, there
> are competing needs: some scenarios want resize to be driven by the
> server (for example, in response to a high-water write event done by
> the client, the server detects that storage should grow; now the
> server needs to advertise to the client that the exported volume is
> larger); and some want it to be driven by the client (the client wants
> to specifically ask the server for the export size to grow; the server
> may or may not be able to comply). NBD is normally client-driven, so
> a client asking the server for a new size is easy to implement by
> documenting a new NBD_CMD_RESIZE (in fact, opcode 8 is already
> reserved for this in the NBD protocol document).
>
> But the server being able to advertise a new size to the client is an
> event-driven task, not client-driven. Here, structured commands and
> the fact that the protocol allows out-of-order responses appears to
> provide a nice solution - the idea was that a new command initiated by
> the client requests an open-ended registration of resize events, and
> the server can reply with as many chunks as wanted over a long length
> of time reusing the client's cookie, and not using the
> NBD_REPLY_FLAG_DONE flag to end the stream of reply chunks until the
> client deregisters from the request (either two new NBD_CMD to
> register/deregister, or one command with a client flag to request
> registration or deregistration).
>
> Ultimately, having NBD able to support live resizes will be even nicer
> than forcing client reconnections just to learn a new server size.
> That said, your series probably still has merit, because even if we
> extend the NBD protocol to allow live resize, it may take a while
> before both client and server ends are upgraded to understand the new
> protocol extension, while this reconnection should work with older
> protocol.
>
I suppose you're talking about the resize extension
https://github.com/NetworkBlockDevice/nbd/blob/extension-resize/doc/proto.md?plain=1#L1366
It would cover my use case nicely. Since management grows the volume
first, sending the NBD_CMD_RESIZE in block_resize would work. The
reason why I decided not to go that route is because getting the
extension accepted looked like a lot more than the current changes.
Moreover, the two approaches can coexist. Handling resizes done on the
server through a reconnection does not prevent the integration of the
client-side solution in the future.
Live resize would indeed be a nicer option. If you back it up, I would
be happy to submit a patch to QEMU with the client-initiated live
resize.
If I get it right, your suggestion for server-side initiated resize has
a lot in common with NVMe's Asynchronous Event Request. That pattern
could help with other events like the server going away or space
running low.
^ permalink raw reply [flat|nested] 15+ messages in thread