generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / common / scsi_debug
1 ##/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2012 Red Hat, Inc. All Rights Reserved.
4 #
5 # Functions useful for tests on unique block devices
6
7 . common/module
8
9 _require_scsi_debug()
10 {
11         local mod_present=0
12
13         # make sure we have the module
14         modinfo scsi_debug 2>&1 > /dev/null || _notrun "scsi_debug module not found"
15
16         lsmod | grep -wq scsi_debug
17         if [[ $? -eq 0 ]]; then
18                 mod_present=1
19         fi
20
21         if [[ $mod_present -eq 1 ]]; then
22                 # We try to remove the module only once if MODPROBE_PATIENT_RM_TIMEOUT_SECONDS
23                 # is set to forever because fstests does not leave modules
24                 # lingering around. If you do have a module lingering around
25                 # and its being used, it wasn't us who started it, so you
26                 # likely would not want to wait forever for it really.
27                 if [[ "$MODPROBE_PATIENT_RM_TIMEOUT_SECONDS" == "forever" ]]; then
28                         rmmod scsi_debug || _notrun "scsi_debug module in use and MODPROBE_PATIENT_RM_TIMEOUT_SECONDS set to forever, removing once failed"
29                 else
30                         _patient_rmmod scsi_debug || _notrun "scsi_debug module in use"
31                 fi
32         fi
33         # make sure it has the features we need
34         # logical/physical sectors plus unmap support all went in together
35         modinfo scsi_debug | grep -wq sector_size || _notrun "scsi_debug too old"
36 }
37
38 # Args: [physical sector size, [logical sector size, [unaligned(0|1), [size in megs]]]]
39 _get_scsi_debug_dev()
40 {
41         # Defaults to phys 512, logical 512, aligned
42         physical=${1-512}
43         logical=${2-512}
44         unaligned=${3-0}
45         size=${4-128}
46         test -n "$4" && shift
47         test -n "$3" && shift
48         test -n "$2" && shift
49         test -n "$1" && shift
50
51         phys_exp=0
52         while [ $logical -lt $physical ]; do
53                 let physical=physical/2
54                 let phys_exp=phys_exp+1
55         done
56         opts="sector_size=$logical physblk_exp=$phys_exp lowest_aligned=$unaligned dev_size_mb=$size $@"
57         echo "scsi_debug options $opts" >> $seqres.full
58         modprobe scsi_debug $opts
59         [ $? -eq 0 ] || _fail "scsi_debug modprobe failed"
60         $UDEV_SETTLE_PROG
61         device=`grep -wl scsi_debug /sys/block/sd*/device/model | awk -F / '{print $4}'`
62         echo "/dev/$device"
63 }
64
65 _put_scsi_debug_dev()
66 {
67         lsmod | grep -wq scsi_debug || return
68         $UDEV_SETTLE_PROG
69         _patient_rmmod scsi_debug || _fail "Could not remove scsi_debug module"
70 }