]> www.infradead.org Git - users/sagi/blktests.git/commitdiff
common/scsi_debug: make helpers more useful
authorOmar Sandoval <osandov@fb.com>
Wed, 16 Aug 2017 05:36:43 +0000 (22:36 -0700)
committerOmar Sandoval <osandov@fb.com>
Wed, 16 Aug 2017 05:49:07 +0000 (22:49 -0700)
Generalize the helpers to make them more useful: instead of always
setting up one device, it now takes arbitrary module parameters and sets
up as many devices as requested.

Signed-off-by: Omar Sandoval <osandov@fb.com>
common/scsi_debug
tests/block/001
tests/block/002

index 23c9038100c53b990f2d90e036210e542a3d17fa..345d16d7587d8f5f90ee9327759f634287ec2b99 100644 (file)
@@ -21,27 +21,47 @@ _have_scsi_debug() {
        _have_module scsi_debug
 }
 
-_get_scsi_debug_dev() {
-       modprobe -r scsi_debug
-       if ! modprobe scsi_debug; then
+_init_scsi_debug() {
+       if ! modprobe -r scsi_debug || ! modprobe scsi_debug "$@"; then
                return 1
        fi
 
        udevadm settle
 
-       local dev
-       for dev in /sys/block/sd*; do
-               if [[ "$(cat "${dev}/device/model")" =~ ^scsi_debug[[:space:]]*$ ]]; then
-                       SCSI_DEBUG_NAME="${dev#/sys/block}"
-                       SCSI_DEBUG_DEV="/dev/${SCSI_DEBUG_NAME}"
-                       return 0
+       local host_sysfs host target_sysfs target
+       SCSI_DEBUG_HOSTS=()
+       SCSI_DEBUG_TARGETS=()
+       SCSI_DEBUG_DEVICES=()
+       for host_sysfs in /sys/class/scsi_host/*; do
+               if [[ "$(cat "${host_sysfs}/proc_name")" = scsi_debug ]]; then
+                       host="${host_sysfs#/sys/class/scsi_host/host}"
+                       SCSI_DEBUG_HOSTS+=("$host")
+                       for target_sysfs in /sys/class/scsi_device/"$host":*; do
+                               target="${target_sysfs#/sys/class/scsi_device/}"
+                               SCSI_DEBUG_TARGETS+=("$target")
+                               SCSI_DEBUG_DEVICES+=("$(ls "$target_sysfs/device/block")")
+                       done
                fi
        done
 
-       echo "Could not get scsi_debug device" >&2
-       return 1
+       if [[ ${#SCSI_DEBUG_HOSTS[@]} -eq 0 ]]; then
+               echo "Could not find scsi_debug hosts" >&2
+               _exit_scsi_debug
+               return 1
+       fi
+
+       if [[ ${#SCSI_DEBUG_TARGETS[@]} -eq 0 ]]; then
+               echo "Could not find scsi_debug targets" >&2
+               _exit_scsi_debug
+               return 1
+       fi
+
+       return 0
 }
 
-_put_scsi_debug_dev() {
+_exit_scsi_debug() {
+       unset SCSI_DEBUG_HOSTS
+       unset SCSI_DEBUG_TARGETS
+       unset SCSI_DEBUG_DEVICES
        modprobe -r scsi_debug
 }
index 4b11dbbd80163fd1ef68e2b0556dd05349d0126d..7411a0cf04f635a8f259691ae6ea21e0c878a353 100755 (executable)
@@ -29,20 +29,12 @@ requires() {
 }
 
 stress_scsi_debug() {
-       modprobe -r scsi_debug
-       modprobe scsi_debug "$@"
+       if ! _init_scsi_debug "$@"; then
+               return
+       fi
 
-       targets=()
-       for host in /sys/class/scsi_host/*; do
-               if [[ "$(cat "${host}/proc_name")" = scsi_debug ]]; then
-                       host="$(basename "${host}")"
-                       for target in "/sys/class/scsi_device/${host#host}:"*; do
-                               targets+=("$(basename "${target}")")
-                       done
-               fi
-       done
-
-       for target in "${targets[@]}"; do
+       local host target
+       for target in "${SCSI_DEBUG_TARGETS[@]}"; do
                (
                host="${target%%:*}"
                scan="${target#*:}"
@@ -57,7 +49,8 @@ stress_scsi_debug() {
        touch "$TMPDIR/stop"
        wait
        rm -f "$TMPDIR/stop"
-       modprobe -r scsi_debug
+
+       _exit_scsi_debug
 }
 
 test() {
index 310e866a0daf38196434545232b9afb2152d3764..57b4f89840954e9f7ed50cc55f8ce5bea2ad9c0b 100755 (executable)
@@ -30,22 +30,22 @@ requires() {
 test() {
        echo "Running ${TEST_NAME}"
 
-       if ! _get_scsi_debug_dev; then
+       if ! _init_scsi_debug; then
                return 1
        fi
 
-       blktrace -D "$TMP_DIR" "$SCSI_DEBUG_DEV" >"$FULL" 2>&1 &
+       blktrace -D "$TMP_DIR" "/dev/${SCSI_DEBUG_DEVICES[0]}" >"$FULL" 2>&1 &
        sleep 0.5
-       echo 1 > "/sys/block/${SCSI_DEBUG_NAME}/device/delete"
-       if [[ ! -d /sys/kernel/debug/block/${SCSI_DEBUG_NAME} ]]; then
+       echo 1 > "/sys/block/${SCSI_DEBUG_DEVICES[0]}/device/delete"
+       if [[ ! -d /sys/kernel/debug/block/${SCSI_DEBUG_DEVICES[0]} ]]; then
                echo "debugfs directory deleted with blktrace active"
        fi
        { kill $!; wait; } >/dev/null 2>/dev/null
-       if [[ -d /sys/kernel/debug/block/${SCSI_DEBUG_NAME} ]]; then
+       if [[ -d /sys/kernel/debug/block/${SCSI_DEBUG_DEVICES[0]} ]]; then
                echo "debugfs directory leaked"
        fi
 
-       _put_scsi_debug_dev
+       _exit_scsi_debug
 
        echo "Test complete"
 }