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.
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.
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.