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..0f9335c5 --- /dev/null +++ b/tests/test_nbd_map_held_device.sh @@ -0,0 +1,46 @@ +#!/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 + +check_nbd + +NBD="sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG" +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 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(1800): + 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 --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 + 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