2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2013 Fusion IO, Inc. All Rights Reserved.
5 # common functions for setting up and tearing down a dmflakey device
14 local BLK_DEV_SIZE=`blockdev --getsz $SCRATCH_DEV`
15 FLAKEY_DEV=/dev/mapper/flakey-test
16 FLAKEY_TABLE="0 $BLK_DEV_SIZE flakey $SCRATCH_DEV 0 180 0"
17 FLAKEY_TABLE_DROP="0 $BLK_DEV_SIZE flakey $SCRATCH_DEV 0 0 180 1 drop_writes"
18 FLAKEY_TABLE_ERROR="0 $BLK_DEV_SIZE flakey $SCRATCH_DEV 0 0 180 1 error_writes"
19 _dmsetup_create flakey-test --table "$FLAKEY_TABLE" || \
20 _fatal "failed to create flakey device"
23 if [ -n "$SCRATCH_RTDEV" ]; then
24 if [ -z "$NON_FLAKEY_RTDEV" ]; then
25 # Set up the device switch
26 local backing_dev="$SCRATCH_RTDEV"
27 export NON_FLAKEY_RTDEV="$SCRATCH_RTDEV"
28 SCRATCH_RTDEV=/dev/mapper/flakey-rttest
30 # Already set up; recreate tables
31 local backing_dev="$NON_FLAKEY_RTDEV"
33 local BLK_DEV_SIZE=`blockdev --getsz $backing_dev`
34 FLAKEY_RTTABLE="0 $BLK_DEV_SIZE flakey $backing_dev 0 180 0"
35 FLAKEY_RTTABLE_DROP="0 $BLK_DEV_SIZE flakey $backing_dev 0 0 180 1 drop_writes"
36 FLAKEY_RTTABLE_ERROR="0 $BLK_DEV_SIZE flakey $backing_dev 0 0 180 1 error_writes"
37 _dmsetup_create flakey-rttest --table "$FLAKEY_RTTABLE" || \
38 _fatal "failed to create flakey rt device"
42 if [ -n "$SCRATCH_LOGDEV" ]; then
43 if [ -z "$NON_FLAKEY_LOGDEV" ]; then
44 # Set up the device switch
45 local backing_dev="$SCRATCH_LOGDEV"
46 export NON_FLAKEY_LOGDEV="$SCRATCH_LOGDEV"
47 SCRATCH_LOGDEV=/dev/mapper/flakey-logtest
49 # Already set up; recreate tables
50 local backing_dev="$NON_FLAKEY_LOGDEV"
52 local BLK_DEV_SIZE=`blockdev --getsz $backing_dev`
53 FLAKEY_LOGTABLE="0 $BLK_DEV_SIZE flakey $backing_dev 0 180 0"
54 FLAKEY_LOGTABLE_DROP="0 $BLK_DEV_SIZE flakey $backing_dev 0 0 180 1 drop_writes"
55 FLAKEY_LOGTABLE_ERROR="0 $BLK_DEV_SIZE flakey $backing_dev 0 0 180 1 error_writes"
56 _dmsetup_create flakey-logtest --table "$FLAKEY_LOGTABLE" || \
57 _fatal "failed to create flakey log device"
63 _scratch_options mount
65 mount -t $FSTYP $SCRATCH_OPTIONS $MOUNT_OPTIONS $FLAKEY_DEV $SCRATCH_MNT
70 $UMOUNT_PROG $SCRATCH_MNT
75 # If dmsetup load fails then we need to make sure to do resume here
76 # otherwise the umount will hang
77 test -n "$NON_FLAKEY_LOGDEV" && $DMSETUP_PROG resume flakey-logtest &> /dev/null
78 test -n "$NON_FLAKEY_RTDEV" && $DMSETUP_PROG resume flakey-rttest &> /dev/null
79 $DMSETUP_PROG resume flakey-test > /dev/null 2>&1
81 $UMOUNT_PROG $SCRATCH_MNT > /dev/null 2>&1
83 _dmsetup_remove flakey-test
84 test -n "$NON_FLAKEY_LOGDEV" && _dmsetup_remove flakey-logtest
85 test -n "$NON_FLAKEY_RTDEV" && _dmsetup_remove flakey-rttest
87 SCRATCH_LOGDEV="$NON_FLAKEY_LOGDEV"
88 unset NON_FLAKEY_LOGDEV
90 SCRATCH_RTDEV="$NON_FLAKEY_RTDEV"
91 unset NON_FLAKEY_RTDEV
94 # _load_flakey_table <table> [lockfs]
96 # This defaults to --nolockfs, which doesn't freeze_fs() before loading the new
97 # table, so it simulates power failure.
101 "$FLAKEY_DROP_WRITES")
102 table="$FLAKEY_TABLE_DROP"
103 logtable="$FLAKEY_LOGTABLE_DROP"
104 rttable="$FLAKEY_RTTABLE_DROP"
106 "$FLAKEY_ERROR_WRITES")
107 table="$FLAKEY_TABLE_ERROR"
108 logtable="$FLAKEY_LOGTABLE_ERROR"
109 rttable="$FLAKEY_RTTABLE_ERROR"
112 table="$FLAKEY_TABLE"
113 logtable="$FLAKEY_LOGTABLE"
114 rttable="$FLAKEY_RTTABLE"
118 suspend_opt="--nolockfs"
119 [ $# -gt 1 ] && [ $2 -eq 1 ] && suspend_opt=""
121 # Suspend the scratch device before the log and realtime devices so
122 # that the kernel can freeze and flush the filesystem if the caller
124 $DMSETUP_PROG suspend $suspend_opt flakey-test
125 [ $? -ne 0 ] && _fatal "failed to suspend flakey-test"
127 if [ -n "$NON_FLAKEY_RTDEV" ]; then
128 $DMSETUP_PROG suspend $suspend_opt flakey-rttest
129 [ $? -ne 0 ] && _fatal "failed to suspend flakey-rttest"
132 if [ -n "$NON_FLAKEY_LOGDEV" ]; then
133 $DMSETUP_PROG suspend $suspend_opt flakey-logtest
134 [ $? -ne 0 ] && _fatal "failed to suspend flakey-logtest"
137 # There may be multiple dm targets in the table, and these dm targets
138 # will be joined by the newline ("\n"). Option --table can not cope with
139 # the multiple-targets case, so get them by reading from standard input.
140 echo -e "$table" | $DMSETUP_PROG load flakey-test
141 [ $? -ne 0 ] && _fatal "failed to load table into flakey-test"
143 if [ -n "$NON_FLAKEY_RTDEV" ]; then
144 echo -e "$rttable" | $DMSETUP_PROG load flakey-rttest
145 [ $? -ne 0 ] && _fatal "failed to load table into flakey-rttest"
148 if [ -n "$NON_FLAKEY_LOGDEV" ]; then
149 echo -e "$logtable" | $DMSETUP_PROG load flakey-logtest
150 [ $? -ne 0 ] && _fatal "failed to load table into flakey-logtest"
153 # Resume devices in the opposite order that we suspended them.
154 if [ -n "$NON_FLAKEY_LOGDEV" ]; then
155 $DMSETUP_PROG resume flakey-logtest
156 [ $? -ne 0 ] && _fatal "failed to resume flakey-logtest"
159 if [ -n "$NON_FLAKEY_RTDEV" ]; then
160 $DMSETUP_PROG resume flakey-rttest
161 [ $? -ne 0 ] && _fatal "failed to resume flakey-rttest"
164 $DMSETUP_PROG resume flakey-test
165 [ $? -ne 0 ] && _fatal "failed to resume flakey-test"
168 # Silently drop all writes and unmount/remount to simulate a crash/power
170 _flakey_drop_and_remount()
172 _load_flakey_table $FLAKEY_DROP_WRITES
175 if [ "x$1" = "xyes" ]; then
176 _check_scratch_fs $FLAKEY_DEV
179 _load_flakey_table $FLAKEY_ALLOW_WRITES
183 _require_flakey_with_error_writes()
187 local NAME=flakey-test
189 _require_dm_target flakey
191 SIZE=`blockdev --getsz $SCRATCH_DEV`
192 TABLE="0 $SIZE flakey $SCRATCH_DEV 0 0 180 1 error_writes"
194 _dmsetup_create $NAME --table "$TABLE" || \
195 _notrun "This test requires error_writes feature in dm-flakey"