]> www.infradead.org Git - users/hch/blktests.git/commitdiff
nbd/003:add mount and clear_sock test for nbd
authorSun Ke <sunke32@huawei.com>
Mon, 23 Dec 2019 03:18:29 +0000 (11:18 +0800)
committerOmar Sandoval <osandov@fb.com>
Tue, 11 Feb 2020 22:22:21 +0000 (14:22 -0800)
Add the test case to check nbd device. This test case catches regressions
fixed by commit 92b5c8f0063e4 "nbd: replace kill_bdev() with
__invalidate_device() again".

Establish the nbd connection. Run two processes. The first one do mount
and umount, and the other one do clear_sock ioctl.

Signed-off-by: Sun Ke <sunke32@huawei.com>
[Omar: simplify]
Signed-off-by: Omar Sandoval <osandov@fb.com>
src/.gitignore
src/Makefile
src/mount_clear_sock.c [new file with mode: 0644]
tests/nbd/003 [new file with mode: 0644]
tests/nbd/003.out [new file with mode: 0644]

index 2108f56595d89ca892f8664e70e4443bb6d44111..355bed3c1b1e04fd7db5d636688c26010f02fb76 100644 (file)
@@ -2,8 +2,9 @@
 /loblksize
 /loop_change_fd
 /loop_get_status_null
-/openclose
+/mount_clear_sock
 /nbdsetsize
+/openclose
 /sg/dxfer-from-dev
 /sg/syzkaller1
 /zbdioctl
index 917d6f4d2b7f1c5267533343b0c4278f5ff2dfcf..3b587f6b62e37f07c1cf0edb414b97f16648c82f 100644 (file)
@@ -4,12 +4,13 @@ HAVE_C_HEADER = $(shell if echo "\#include <$(1)>" |          \
 
 C_TARGETS := \
        loblksize \
+       loop_change_fd \
        loop_get_status_null \
+       mount_clear_sock \
+       nbdsetsize \
        openclose \
        sg/dxfer-from-dev \
        sg/syzkaller1 \
-       nbdsetsize \
-       loop_change_fd \
        zbdioctl
 
 CXX_TARGETS := \
diff --git a/src/mount_clear_sock.c b/src/mount_clear_sock.c
new file mode 100644 (file)
index 0000000..4030a4a
--- /dev/null
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: GPL-3.0+
+// Copyright (C) 2019 Sun Ke
+
+#include <assert.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <linux/fs.h>
+#include <linux/nbd.h>
+
+int main(int argc, char **argv)
+{
+       const char *mountpoint, *dev, *fstype;
+       int loops, fd;
+
+       if (argc != 5) {
+               fprintf(stderr, "usage: %s DEV MOUNTPOINT FSTYPE LOOPS", argv[0]);
+               return EXIT_FAILURE;
+       }
+
+       dev = argv[1];
+       mountpoint = argv[2];
+       fstype = argv[3];
+       loops = atoi(argv[4]);
+
+       fd = open(dev, O_RDWR);
+       if (fd == -1) {
+               perror("open");
+               return EXIT_FAILURE;
+       }
+
+       for (int i = 0; i < loops; i++) {
+               pid_t mount_pid, clear_sock_pid;
+               int wstatus;
+
+               mount_pid = fork();
+               if (mount_pid == -1) {
+                       perror("fork");
+                       return EXIT_FAILURE;
+               }
+               if (mount_pid == 0) {
+                       mount(dev, mountpoint, fstype,
+                             MS_NOSUID | MS_SYNCHRONOUS, 0);
+                       umount(mountpoint);
+                       exit(EXIT_SUCCESS);
+               }
+
+               clear_sock_pid = fork();
+               if (clear_sock_pid == -1) {
+                       perror("fork");
+                       return EXIT_FAILURE;
+               }
+               if (clear_sock_pid == 0) {
+                       if (ioctl(fd, NBD_CLEAR_SOCK, 0) == -1) {
+                               perror("ioctl");
+                               exit(EXIT_FAILURE);
+                       }
+                       exit(EXIT_SUCCESS);
+               }
+
+               if (waitpid(mount_pid, &wstatus, 0) == -1) {
+                       perror("waitpid");
+                       return EXIT_FAILURE;
+               }
+               if (!WIFEXITED(wstatus) ||
+                   WEXITSTATUS(wstatus) != EXIT_SUCCESS) {
+                       fprintf(stderr, "mount process failed");
+                       return EXIT_FAILURE;
+               }
+
+               if (waitpid(clear_sock_pid, &wstatus, 0) == -1) {
+                       perror("waitpid");
+                       return EXIT_FAILURE;
+               }
+               if (!WIFEXITED(wstatus) ||
+                   WEXITSTATUS(wstatus) != EXIT_SUCCESS) {
+                       fprintf(stderr, "NBD_CLEAR_SOCK process failed");
+                       return EXIT_FAILURE;
+               }
+       }
+
+       close(fd);
+       return EXIT_SUCCESS;
+}
diff --git a/tests/nbd/003 b/tests/nbd/003
new file mode 100644 (file)
index 0000000..57fb63a
--- /dev/null
@@ -0,0 +1,30 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-3.0+
+# Copyright (C) 2019 Sun Ke
+#
+# Regression test for commit 2b5c8f0063e4 ("nbd: replace kill_bdev() with
+# __invalidate_device() again").
+
+. tests/nbd/rc
+
+DESCRIPTION="mount/unmount concurrently with NBD_CLEAR_SOCK"
+QUICK=1
+
+requires() {
+       _have_nbd && _have_src_program mount_clear_sock
+}
+
+test() {
+       echo "Running ${TEST_NAME}"
+
+       _start_nbd_server
+       nbd-client -L -N export localhost /dev/nbd0 >> "$FULL" 2>&1
+       mkfs.ext4 /dev/nbd0 >> "$FULL" 2>&1
+
+       mkdir -p "${TMPDIR}/mnt"
+       src/mount_clear_sock /dev/nbd0 "${TMPDIR}/mnt" ext4 5000
+       umount "${TMPDIR}/mnt" > /dev/null 2>&1
+
+       nbd-client -d /dev/nbd0 >> "$FULL" 2>&1
+       _stop_nbd_server
+}
diff --git a/tests/nbd/003.out b/tests/nbd/003.out
new file mode 100644 (file)
index 0000000..aa340db
--- /dev/null
@@ -0,0 +1 @@
+Running nbd/003