Skip devices held open after their last session in vitastor-nbd map #172

Open
koolkhel wants to merge 2 commits from koolkhel/vitastor:nbd-map-held-device into master
Contributor

Description

vitastor-nbd map could print a device that was not connected.

  • A device that another process still holds open after its vitastor-nbd exited keeps the old kernel session. The holder can be udev probing it after a writer's close, a writer, or a mount.
  • list_mapped() skips the pid file of a process that is gone, so the device counts as free.
  • The old NBD_CLEAR_SOCK reset the setup owner, so NBD_SET_SOCK accepts the new socket.
  • The child reports success to the parent before NBD_DO_IT, so map prints the device.
  • NBD_DO_IT then fails with EBUSY: nbd_start_device() refuses while nbd->pid of the old session is set. Reads and writes return EIO, and the map process stays without a device.

The fix: after NBD_SET_SOCK, an existing /sys/block/nbdN/pid is treated as busy through the existing error path. Automatic device choice then moves to the next device, and a map of a named device fails.

  • The check is race-free. After a successful NBD_SET_SOCK this process owns the device setup and holds a reference to the session, so the pid cannot appear or go away.
  • A pid file at that point can only belong to an ended session: a live ioctl session owns the setup, and a live netlink one is bound, so both make NBD_SET_SOCK fail.
  • netlink-map is not affected: nbd_genl_connect() refuses a device that is held open.

Tested: new tests/test_nbd_map_held_device.sh.

  • It holds the device open during unmap, then checks that the next map takes another device and that a write to it succeeds.
  • Results in a VM (Ubuntu 24.04, kernel 6.8.0-142): it fails on master 4ee30fa7, 3 runs of 3 ("map took /dev/nbd0"), and passes with the fix, 3 of 3.
  • It is added to tests/run_tests.sh and the workflow next to test_img_online_resize.sh.
  • The same session logic was checked in kernel 6.1's nbd.c.

Not checked: kernels older than 6.1.

Contributor Name

Yury Luneff (MIND Software LLC, https://mindsw.io)

CLA

### Description `vitastor-nbd map` could print a device that was not connected. - A device that another process still holds open after its vitastor-nbd exited keeps the old kernel session. The holder can be udev probing it after a writer's close, a writer, or a mount. - `list_mapped()` skips the pid file of a process that is gone, so the device counts as free. - The old `NBD_CLEAR_SOCK` reset the setup owner, so `NBD_SET_SOCK` accepts the new socket. - The child reports success to the parent before `NBD_DO_IT`, so map prints the device. - `NBD_DO_IT` then fails with EBUSY: `nbd_start_device()` refuses while `nbd->pid` of the old session is set. Reads and writes return EIO, and the map process stays without a device. The fix: after `NBD_SET_SOCK`, an existing `/sys/block/nbdN/pid` is treated as busy through the existing error path. Automatic device choice then moves to the next device, and a map of a named device fails. - The check is race-free. After a successful `NBD_SET_SOCK` this process owns the device setup and holds a reference to the session, so the pid cannot appear or go away. - A pid file at that point can only belong to an ended session: a live ioctl session owns the setup, and a live netlink one is bound, so both make `NBD_SET_SOCK` fail. - netlink-map is not affected: `nbd_genl_connect()` refuses a device that is held open. Tested: new `tests/test_nbd_map_held_device.sh`. - It holds the device open during `unmap`, then checks that the next map takes another device and that a write to it succeeds. - Results in a VM (Ubuntu 24.04, kernel 6.8.0-142): it fails on master 4ee30fa7, 3 runs of 3 ("map took /dev/nbd0"), and passes with the fix, 3 of 3. - It is added to `tests/run_tests.sh` and the workflow next to `test_img_online_resize.sh`. - The same session logic was checked in kernel 6.1's `nbd.c`. Not checked: kernels older than 6.1. ### Contributor Name Yury Luneff (MIND Software LLC, https://mindsw.io) ### CLA - [x] I accept Vitastor CLA agreement: https://git.yourcmc.ru/vitalif/vitastor/src/branch/master/CLA-en.md
koolkhel added 1 commit 2026-11-24 08:22:58 +03:00
A device that another process (udev, a writer, a mount) still holds
open after its vitastor-nbd exited keeps the old kernel session.
NBD_SET_SOCK accepts the new socket, but NBD_DO_IT then fails with EBUSY
because the old session's pid is still set. map had already reported
success and printed the device, which returned EIO.

After NBD_SET_SOCK, treat an existing /sys/block/nbdN/pid as busy, so
map tries the next device, or fails for an explicitly named one.
koolkhel added 1 commit 2026-11-24 11:42:41 +03:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u nbd-map-held-device:koolkhel-nbd-map-held-device
git checkout koolkhel-nbd-map-held-device
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: vitalif/vitastor#172
No description provided.