From 4d75f22e249843c485784863c64a4c84164b2b36 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Tue, 24 Nov 2026 08:22:47 +0300 Subject: [PATCH 1/2] Skip devices held open after their last session in vitastor-nbd map 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. --- .gitea/workflows/test.yml | 18 ++++++++++++ src/client/nbd_proxy.cpp | 7 +++++ tests/run_tests.sh | 2 ++ tests/test_nbd_map_held_device.sh | 48 +++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100755 tests/test_nbd_map_held_device.sh diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index ad781d0c..40985f42 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -347,6 +347,24 @@ jobs: echo "" done + test_nbd_map_held_device: + runs-on: ubuntu-latest + needs: build + container: ${{env.TEST_IMAGE}}:${{github.sha}} + steps: + - name: Run test + id: test + timeout-minutes: 3 + run: /root/vitastor/tests/test_nbd_map_held_device.sh + - name: Print logs + if: always() && steps.test.outcome == 'failure' + run: | + for i in /root/vitastor/testdata/*.log /root/vitastor/testdata/*.txt; do + echo "-------- $i --------" + cat $i + echo "" + done + test_no_uring: runs-on: ubuntu-latest needs: build diff --git a/src/client/nbd_proxy.cpp b/src/client/nbd_proxy.cpp index 26b6c6f9..633a0aa6 100644 --- a/src/client/nbd_proxy.cpp +++ b/src/client/nbd_proxy.cpp @@ -1020,6 +1020,13 @@ protected: { goto end_close; } + // A device still held open keeps the session of its exited server, NBD_DO_IT would fail + snprintf(path, sizeof(path), "/sys/block/nbd%d/pid", dev_num); + if (access(path, F_OK) == 0) + { + errno = EBUSY; + goto end_unmap; + } r = ioctl(nbd, NBD_SET_BLKSIZE, 4096); if (r < 0) { diff --git a/tests/run_tests.sh b/tests/run_tests.sh index caac41b7..c8cd170e 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -29,6 +29,8 @@ ETCD_SCHEME=https ./test_snapshot.sh ./test_img_online_resize.sh +./test_nbd_map_held_device.sh + ./test_no_uring.sh ./test_interrupted_rebalance.sh diff --git a/tests/test_nbd_map_held_device.sh b/tests/test_nbd_map_held_device.sh new file mode 100755 index 00000000..9737d1ed --- /dev/null +++ b/tests/test_nbd_map_held_device.sh @@ -0,0 +1,48 @@ +#!/bin/bash -ex + +# vitastor-nbd map must not take a device that another process still holds open after its +# previous vitastor-nbd exited. The kernel keeps the old session of such a device: the new +# socket is accepted, but NBD_DO_IT fails, and map printed a device that returns EIO. + +PG_COUNT=1 +. `dirname $0`/run_3osds.sh + +$VITASTOR_CLI create -s 100M testimg1 +$VITASTOR_CLI create -s 100M testimg2 + +if ! (mount | grep '/dev type devtmpfs'); then + sudo bash -c 'mount udev /dev/ -t devtmpfs && mount devpts /dev/pts -t devpts' +fi + +NBD="sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG" +NBD_DEV1=$($NBD map --image testimg1) +trap "touch ./testdata/release; $NBD unmap $NBD_DEV1"' || true; kill -9 $(jobs -p)' EXIT + +# Hold the device open until ./testdata/release appears, at most 60 s +sudo python3 -c ' +import os, sys, time +fd = os.open(sys.argv[1], os.O_RDONLY) +print("ready", flush=True) +for i in range(600): + if os.path.exists(sys.argv[2]): + break + time.sleep(0.1) +' $NBD_DEV1 ./testdata/release > ./testdata/holder.txt & +HOLDER_PID=$! +wait_condition 10 "grep -q ready ./testdata/holder.txt" "device holder" +$NBD unmap $NBD_DEV1 +wait_condition 30 "! pgrep -f '[v]itastor-nbd .* map --image testimg1'" "vitastor-nbd exit" + +NBD_DEV2=$($NBD map --image testimg2) +trap "touch ./testdata/release; $NBD unmap $NBD_DEV2"' || true; kill -9 $(jobs -p)' EXIT + +if [[ "$NBD_DEV2" == "$NBD_DEV1" ]]; then + format_error "vitastor-nbd map took $NBD_DEV1, which is still held open after its vitastor-nbd exited" +fi +sudo dd if=/dev/urandom of=$NBD_DEV2 bs=4k count=1 oflag=direct status=none || \ + format_error "$NBD_DEV2 printed by vitastor-nbd map is not connected" + +touch ./testdata/release +wait $HOLDER_PID + +format_green OK -- 2.47.3 From 542ed24037a2be9fa26aa2a14c33af6553c15ac7 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Tue, 24 Nov 2026 11:41:17 +0300 Subject: [PATCH 2/2] test_nbd_map_held_device: Use check_nbd, log vitastor-nbd, hold the device up to 3 minutes --- tests/test_nbd_map_held_device.sh | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/test_nbd_map_held_device.sh b/tests/test_nbd_map_held_device.sh index 9737d1ed..0f9335c5 100755 --- a/tests/test_nbd_map_held_device.sh +++ b/tests/test_nbd_map_held_device.sh @@ -10,20 +10,18 @@ PG_COUNT=1 $VITASTOR_CLI create -s 100M testimg1 $VITASTOR_CLI create -s 100M testimg2 -if ! (mount | grep '/dev type devtmpfs'); then - sudo bash -c 'mount udev /dev/ -t devtmpfs && mount devpts /dev/pts -t devpts' -fi +check_nbd NBD="sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG" -NBD_DEV1=$($NBD map --image testimg1) +NBD_DEV1=$($NBD map --image testimg1 --logfile $(pwd)/testdata/nbd1.log) trap "touch ./testdata/release; $NBD unmap $NBD_DEV1"' || true; kill -9 $(jobs -p)' EXIT -# Hold the device open until ./testdata/release appears, at most 60 s +# Hold the device open until ./testdata/release appears, at most 180 s sudo python3 -c ' import os, sys, time fd = os.open(sys.argv[1], os.O_RDONLY) print("ready", flush=True) -for i in range(600): +for i in range(1800): if os.path.exists(sys.argv[2]): break time.sleep(0.1) @@ -33,7 +31,7 @@ wait_condition 10 "grep -q ready ./testdata/holder.txt" "device holder" $NBD unmap $NBD_DEV1 wait_condition 30 "! pgrep -f '[v]itastor-nbd .* map --image testimg1'" "vitastor-nbd exit" -NBD_DEV2=$($NBD map --image testimg2) +NBD_DEV2=$($NBD map --image testimg2 --logfile $(pwd)/testdata/nbd2.log) trap "touch ./testdata/release; $NBD unmap $NBD_DEV2"' || true; kill -9 $(jobs -p)' EXIT if [[ "$NBD_DEV2" == "$NBD_DEV1" ]]; then -- 2.47.3