]> www.infradead.org Git - users/sagi/blktests.git/commitdiff
loop/006: Add test for oops during backing file verification
authorJan Kara <jack@suse.cz>
Thu, 18 Oct 2018 10:31:47 +0000 (12:31 +0200)
committerOmar Sandoval <osandov@fb.com>
Thu, 25 Oct 2018 21:15:46 +0000 (14:15 -0700)
Add regression test for patch "block/loop: Use global lock for ioctl()
operation." where we can oops while traversing list of loop devices
backing newly created device.

Signed-off-by: Jan Kara <jack@suse.cz>
[Omar: rename to 006, change description]
Signed-off-by: Omar Sandoval <osandov@fb.com>
src/.gitignore
src/Makefile
src/loop_change_fd.c [new file with mode: 0644]
tests/loop/006 [new file with mode: 0755]
tests/loop/006.out [new file with mode: 0644]

index d0400392538b67c12a77425a77f58a4a5d27fc32..8c957859b171fd623e795c6409a04fd91a45194f 100644 (file)
@@ -1,5 +1,6 @@
 /discontiguous-io
 /loblksize
+/loop_change_fd
 /loop_get_status_null
 /openclose
 /nbdsetsize
index f89f61701179c271d777b82bad62a9df61962164..15c10226ccb74d82932c0ef657ed179bae9b68f5 100644 (file)
@@ -4,7 +4,8 @@ C_TARGETS := \
        openclose \
        sg/dxfer-from-dev \
        sg/syzkaller1 \
-       nbdsetsize
+       nbdsetsize \
+       loop_change_fd
 
 CXX_TARGETS := \
        discontiguous-io
diff --git a/src/loop_change_fd.c b/src/loop_change_fd.c
new file mode 100644 (file)
index 0000000..b124d82
--- /dev/null
@@ -0,0 +1,48 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <linux/loop.h>
+
+void usage(const char *progname)
+{
+       fprintf(stderr, "usage: %s LOOPDEV PATH\n", progname);
+       exit(EXIT_FAILURE);
+}
+
+int main(int argc, char **argv)
+{
+       int ret;
+       int fd, filefd;
+
+       if (argc != 3)
+               usage(argv[0]);
+
+       fd = open(argv[1], O_RDWR);
+       if (fd == -1) {
+               perror("open");
+               return EXIT_FAILURE;
+       }
+
+       filefd = open(argv[2], O_RDWR);
+       if (filefd == -1) {
+               perror("open");
+               return EXIT_FAILURE;
+       }
+
+       ret = ioctl(fd, LOOP_CHANGE_FD, filefd);
+       if (ret == -1) {
+               perror("ioctl");
+               close(fd);
+               close(filefd);
+               return EXIT_FAILURE;
+       }
+       close(fd);
+       close(filefd);
+       return EXIT_SUCCESS;
+}
diff --git a/tests/loop/006 b/tests/loop/006
new file mode 100755 (executable)
index 0000000..2ca2d75
--- /dev/null
@@ -0,0 +1,72 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-3.0+
+# Copyright (C) 2018 Jan Kara
+#
+# Regression test for patch "block/loop: Use global lock for ioctl() operation."
+# We swap file (through LOOP_CHANGE_FD) under one loopback device while
+# creating and deleting another loopback device pointing to the first one.
+# This checks for races in validation of backing fd.
+
+. tests/loop/rc
+
+DESCRIPTION="change loop backing file while creating/removing another loop device"
+# Try for some time to trigger the race
+TIMED=1
+
+requires() {
+       _have_src_program loop_change_fd
+}
+
+# Setup and tear down loop device pointing to loop_dev
+run_setter() {
+       loop_dev="$1"
+
+       while top_dev=$(losetup -f --show "$loop_dev"); do
+               losetup -d "$top_dev"
+       done
+}
+
+# Switch backing file of loop_dev continuously
+run_switcher() {
+       loop_dev="$1"
+
+       i=1
+       while src/loop_change_fd "$loop_dev" "$TMPDIR/file$i"; do
+               i=$(((i+1)%2))
+       done
+}
+
+test() {
+       echo "Running ${TEST_NAME}"
+
+       truncate -s 1M "$TMPDIR/file0"
+       truncate -s 1M "$TMPDIR/file1"
+
+       if ! loop_dev="$(losetup -r -f --show "$TMPDIR/file0")"; then
+               return 1
+       fi
+
+       run_switcher "$loop_dev" &
+       switch_pid=$!
+       run_setter "$loop_dev" &
+       set_pid=$!
+
+       sleep ${TIMEOUT:-30}
+
+       # Discard KILLED messages from bash...
+       {
+               kill -9 $switch_pid
+               kill -9 $set_pid
+               wait
+               sleep 1
+       } 2>/dev/null
+
+       # Clean up devices
+       top_dev=$(losetup -j "$loop_dev" | sed -e 's/\([^:]*\): .*/\1/')
+       if [[ -n "$top_dev" ]]; then
+               losetup -d "$top_dev"
+       fi
+       losetup -d "$loop_dev"
+
+       echo "Test complete"
+}
diff --git a/tests/loop/006.out b/tests/loop/006.out
new file mode 100644 (file)
index 0000000..50bf833
--- /dev/null
@@ -0,0 +1,2 @@
+Running loop/006
+Test complete