From 8ac1eea221b5a9c51c454ebc4ec40c6b191a07b8 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Mon, 23 Nov 2026 04:42:07 +0300 Subject: [PATCH 1/3] Ignore image resize events after NBD disconnect in vitastor-nbd vitastor-nbd kept its image watch during the final flush, after the NBD device was already disconnected. A resize of the image in that time made it call NBD_SET_SIZE on the device, which by then could be mapped again to another image and so got the wrong size. Close the watch when the main loop ends with the device disconnected. After SIGINT or SIGTERM the device is still connected and the flush still reads requests, which use the watch, so it stays open there. --- src/client/nbd_proxy.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/client/nbd_proxy.cpp b/src/client/nbd_proxy.cpp index 26b6c6f9..2880f709 100644 --- a/src/client/nbd_proxy.cpp +++ b/src/client/nbd_proxy.cpp @@ -759,6 +759,12 @@ help: ringloop->loop(); ringloop->wait(); } + if (watch && nbd_fd < 0) + { + // The device is disconnected and may already be mapped by someone else + cli->st_cli->close_watch(watch); + watch = NULL; + } cli->flush(); } -- 2.47.3 From dec1295ed54f050a4d3e919cbdd288d75072e775 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Tue, 24 Nov 2026 22:59:15 +0300 Subject: [PATCH 2/3] Send UPDATE_SIZE once per resize and not after stop in vitastor-ublk try_resize() set size_changed to true instead of false, so after the first online resize every ring loop iteration sent another UBLK_U_CMD_UPDATE_SIZE: about 100k commands per second at full CPU, and a log line for each on kernels without UPDATE_SIZE. The commands also went on in the final flush after unmap had stopped the device. UPDATE_SIZE on a stopped device crashes 6.16-6.19 kernels without 25966fc09769 ("ublk: fix NULL pointer dereference in ublk_ctrl_set_size()"). Don't resize once the server is stopping, and submit the command at once rather than with some later submit. --- src/client/ublk_server.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/client/ublk_server.cpp b/src/client/ublk_server.cpp index dabf2702..b2db55d2 100644 --- a/src/client/ublk_server.cpp +++ b/src/client/ublk_server.cpp @@ -361,7 +361,8 @@ help: } consumer.loop = [this]() { - if (size_changed) + // The device may be stopped already: UPDATE_SIZE on it crashes unpatched 6.16-6.19 kernels + if (size_changed && !stop) try_resize(); submit_postponed(); }; @@ -700,7 +701,7 @@ protected: { return; } - size_changed = true; + size_changed = false; ring_data_t* data = ((ring_data_t*)sqe->user_data); data->callback = [this, size = device_size](ring_data_t *data) { @@ -709,6 +710,7 @@ protected: fprintf(stderr, "Failed to update size of /dev/ublkb%d to %ju bytes: %s (code %d)\n", ublk_dev.dev_id, size, strerror(-data->res), data->res); } }; + ringloop->submit(); } void map_ublk_queue() -- 2.47.3 From b411d71ead7736aed648937e0db836040d48869c Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 25 Nov 2026 11:12:26 +0300 Subject: [PATCH 3/3] Send UPDATE_SIZE only after the device is started in vitastor-ublk The ring consumer is registered before START_DEV, so that the server answers the I/O the kernel sends while the disk is added, and the synchronous control commands run the ring loop. A resize event that arrived before the device was started therefore sent UPDATE_SIZE to a device without a disk: kernels 6.16-6.19 without 25966fc09769 dereference NULL there, and patched kernels refuse it, so the device kept the size given in SET_PARAMS. Keep the resize pending until the device is started. --- src/client/ublk_server.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/client/ublk_server.cpp b/src/client/ublk_server.cpp index b2db55d2..ec53cc75 100644 --- a/src/client/ublk_server.cpp +++ b/src/client/ublk_server.cpp @@ -87,6 +87,7 @@ protected: cluster_client_t *cli = NULL; inode_watch_t *watch = NULL; bool size_changed = false; + bool started = false; std::string logfile = "/dev/null"; std::string pidfile; @@ -361,13 +362,14 @@ help: } consumer.loop = [this]() { - // The device may be stopped already: UPDATE_SIZE on it crashes unpatched 6.16-6.19 kernels - if (size_changed && !stop) + // Not before START_DEV or after stop: UPDATE_SIZE on a device without a disk crashes unpatched 6.16-6.19 kernels + if (size_changed && started && !stop) try_resize(); submit_postponed(); }; ringloop->register_consumer(&consumer); start_device(recover); + started = true; if (pidfile != "") write_pid(); if (bg) -- 2.47.3