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 if [ -n "$NON_FLAKEY_LOGDEV" ]; then
88 SCRATCH_LOGDEV="$NON_FLAKEY_LOGDEV"
89 unset NON_FLAKEY_LOGDEV
92 if [ -n "$NON_FLAKEY_RTDEV" ]; then
93 SCRATCH_RTDEV="$NON_FLAKEY_RTDEV"
94 unset NON_FLAKEY_RTDEV
98 # _load_flakey_table <table> [lockfs]
100 # This defaults to --nolockfs, which doesn't freeze_fs() before loading the new
101 # table, so it simulates power failure.
105 "$FLAKEY_DROP_WRITES")
106 table="$FLAKEY_TABLE_DROP"
107 logtable="$FLAKEY_LOGTABLE_DROP"
108 rttable="$FLAKEY_RTTABLE_DROP"
110 "$FLAKEY_ERROR_WRITES")
111 table="$FLAKEY_TABLE_ERROR"
112 logtable="$FLAKEY_LOGTABLE_ERROR"
113 rttable="$FLAKEY_RTTABLE_ERROR"
116 table="$FLAKEY_TABLE"
117 logtable="$FLAKEY_LOGTABLE"
118 rttable="$FLAKEY_RTTABLE"
122 suspend_opt="--nolockfs"
123 [ $# -gt 1 ] && [ $2 -eq 1 ] && suspend_opt=""
125 # Suspend the scratch device before the log and realtime devices so
126 # that the kernel can freeze and flush the filesystem if the caller
128 $DMSETUP_PROG suspend $suspend_opt flakey-test
129 [ $? -ne 0 ] && _fatal "failed to suspend flakey-test"
131 if [ -n "$NON_FLAKEY_RTDEV" ]; then
132 $DMSETUP_PROG suspend $suspend_opt flakey-rttest
133 [ $? -ne 0 ] && _fatal "failed to suspend flakey-rttest"
136 if [ -n "$NON_FLAKEY_LOGDEV" ]; then
137 $DMSETUP_PROG suspend $suspend_opt flakey-logtest
138 [ $? -ne 0 ] && _fatal "failed to suspend flakey-logtest"
141 # There may be multiple dm targets in the table, and these dm targets
142 # will be joined by the newline ("\n"). Option --table can not cope with
143 # the multiple-targets case, so get them by reading from standard input.
144 echo -e "$table" | $DMSETUP_PROG load flakey-test
145 [ $? -ne 0 ] && _fatal "failed to load table into flakey-test"
147 if [ -n "$NON_FLAKEY_RTDEV" ]; then
148 echo -e "$rttable" | $DMSETUP_PROG load flakey-rttest
149 [ $? -ne 0 ] && _fatal "failed to load table into flakey-rttest"
152 if [ -n "$NON_FLAKEY_LOGDEV" ]; then
153 echo -e "$logtable" | $DMSETUP_PROG load flakey-logtest
154 [ $? -ne 0 ] && _fatal "failed to load table into flakey-logtest"
157 # Resume devices in the opposite order that we suspended them.
158 if [ -n "$NON_FLAKEY_LOGDEV" ]; then
159 $DMSETUP_PROG resume flakey-logtest
160 [ $? -ne 0 ] && _fatal "failed to resume flakey-logtest"
163 if [ -n "$NON_FLAKEY_RTDEV" ]; then
164 $DMSETUP_PROG resume flakey-rttest
165 [ $? -ne 0 ] && _fatal "failed to resume flakey-rttest"
168 $DMSETUP_PROG resume flakey-test
169 [ $? -ne 0 ] && _fatal "failed to resume flakey-test"
172 # Silently drop all writes and unmount/remount to simulate a crash/power
174 _flakey_drop_and_remount()
176 _load_flakey_table $FLAKEY_DROP_WRITES
179 if [ "x$1" = "xyes" ]; then
180 _check_scratch_fs $FLAKEY_DEV
183 _load_flakey_table $FLAKEY_ALLOW_WRITES
187 _require_flakey_with_error_writes()
191 local NAME=flakey-test
193 _require_dm_target flakey
195 SIZE=`blockdev --getsz $SCRATCH_DEV`
196 TABLE="0 $SIZE flakey $SCRATCH_DEV 0 0 180 1 error_writes"
198 _dmsetup_create $NAME --table "$TABLE" || \
199 _notrun "This test requires error_writes feature in dm-flakey"