common/fuzzy: if the fuzz verb is random, keep fuzzing until we get a new value
[xfstests-dev.git] / common / dmerror
1 ##/bin/bash
2 #
3 # Copyright (c) 2015 Oracle.  All Rights Reserved.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it would be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write the Free Software Foundation,
16 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 #
18 #
19 # common functions for setting up and tearing down a dmerror device
20
21 echo $MOUNT_OPTIONS | grep -q dax
22 if [ $? -eq 0 ]; then
23         _notrun "Cannot run tests with DAX on dmerror devices"
24 fi
25
26 _dmerror_setup()
27 {
28         local dm_backing_dev=$SCRATCH_DEV
29
30         local blk_dev_size=`blockdev --getsz $dm_backing_dev`
31
32         DMERROR_DEV='/dev/mapper/error-test'
33
34         DMLINEAR_TABLE="0 $blk_dev_size linear $dm_backing_dev 0"
35
36         DMERROR_TABLE="0 $blk_dev_size error $dm_backing_dev 0"
37 }
38
39 _dmerror_init()
40 {
41         _dmerror_setup
42         $DMSETUP_PROG remove error-test > /dev/null 2>&1
43         $DMSETUP_PROG create error-test --table "$DMLINEAR_TABLE" || \
44                 _fatal "failed to create dm linear device"
45 }
46
47 _dmerror_mount()
48 {
49         _scratch_options mount
50         $MOUNT_PROG -t $FSTYP `_common_dev_mount_options $*` $SCRATCH_OPTIONS \
51                 $DMERROR_DEV $SCRATCH_MNT
52 }
53
54 _dmerror_unmount()
55 {
56         umount $SCRATCH_MNT
57 }
58
59 _dmerror_cleanup()
60 {
61         $UMOUNT_PROG $SCRATCH_MNT > /dev/null 2>&1
62         # wait for device to be fully settled so that 'dmsetup remove' doesn't
63         # fail due to EBUSY
64         $UDEV_SETTLE_PROG >/dev/null 2>&1
65         $DMSETUP_PROG remove error-test > /dev/null 2>&1
66 }
67
68 _dmerror_load_error_table()
69 {
70         suspend_opt="--nolockfs"
71
72         if [ "$1" = "lockfs" ]; then
73                 suspend_opt=""
74         elif [ -n "$*" ]; then
75                 suspend_opt="$*"
76         fi
77
78         $DMSETUP_PROG suspend $suspend_opt error-test
79         [ $? -ne 0 ] && _fail  "dmsetup suspend failed"
80
81         $DMSETUP_PROG load error-test --table "$DMERROR_TABLE"
82         [ $? -ne 0 ] && _fail "dmsetup failed to load error table"
83
84         $DMSETUP_PROG resume error-test
85         [ $? -ne 0 ] && _fail  "dmsetup resume failed"
86 }
87
88 _dmerror_load_working_table()
89 {
90         suspend_opt="--nolockfs"
91
92         if [ "$1" = "lockfs" ]; then
93                 suspend_opt=""
94         elif [ -n "$*" ]; then
95                 suspend_opt="$*"
96         fi
97
98         $DMSETUP_PROG suspend $suspend_opt error-test
99         [ $? -ne 0 ] && _fail  "dmsetup suspend failed"
100
101         $DMSETUP_PROG load error-test --table "$DMLINEAR_TABLE"
102         [ $? -ne 0 ] && _fail "dmsetup failed to load error table"
103
104         $DMSETUP_PROG resume error-test
105         [ $? -ne 0 ] && _fail  "dmsetup resume failed"
106 }