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
13 local BLK_DEV_SIZE=`blockdev --getsz $SCRATCH_DEV`
14 FLAKEY_DEV=/dev/mapper/flakey-test
15 FLAKEY_TABLE="0 $BLK_DEV_SIZE flakey $SCRATCH_DEV 0 180 0"
16 FLAKEY_TABLE_DROP="0 $BLK_DEV_SIZE flakey $SCRATCH_DEV 0 0 180 1 drop_writes"
17 FLAKEY_TABLE_ERROR="0 $BLK_DEV_SIZE flakey $SCRATCH_DEV 0 0 180 1 error_writes"
18 _dmsetup_create flakey-test --table "$FLAKEY_TABLE" || \
19 _fatal "failed to create flakey device"
24 _scratch_options mount
25 mount -t $FSTYP $SCRATCH_OPTIONS $MOUNT_OPTIONS $FLAKEY_DEV $SCRATCH_MNT
30 $UMOUNT_PROG $SCRATCH_MNT
35 # If dmsetup load fails then we need to make sure to do resume here
36 # otherwise the umount will hang
37 $DMSETUP_PROG resume flakey-test > /dev/null 2>&1
38 $UMOUNT_PROG $SCRATCH_MNT > /dev/null 2>&1
39 _dmsetup_remove flakey-test
42 # _load_flakey_table <table> [lockfs]
44 # This defaults to --nolockfs, which doesn't freeze_fs() before loading the new
45 # table, so it simulates power failure.
49 [ $1 -eq $FLAKEY_DROP_WRITES ] && table="$FLAKEY_TABLE_DROP"
50 [ $1 -eq $FLAKEY_ERROR_WRITES ] && table="$FLAKEY_TABLE_ERROR"
52 suspend_opt="--nolockfs"
53 [ $# -gt 1 ] && [ $2 -eq 1 ] && suspend_opt=""
55 $DMSETUP_PROG suspend $suspend_opt flakey-test
56 [ $? -ne 0 ] && _fatal "failed to suspend flakey-test"
58 # There may be multiple dm targets in the table, and these dm targets
59 # will be joined by the newline ("\n"). Option --table can not cope with
60 # the multiple-targets case, so get them by reading from standard input.
61 echo -e "$table" | $DMSETUP_PROG load flakey-test
62 [ $? -ne 0 ] && _fatal "failed to load table into flakey-test"
64 $DMSETUP_PROG resume flakey-test
65 [ $? -ne 0 ] && _fatal "failed to resume flakey-test"
68 # Silently drop all writes and unmount/remount to simulate a crash/power
70 _flakey_drop_and_remount()
72 _load_flakey_table $FLAKEY_DROP_WRITES
75 if [ "x$1" = "xyes" ]; then
76 _check_scratch_fs $FLAKEY_DEV
79 _load_flakey_table $FLAKEY_ALLOW_WRITES
83 _require_flakey_with_error_writes()
87 local NAME=flakey-test
89 _require_dm_target flakey
91 SIZE=`blockdev --getsz $SCRATCH_DEV`
92 TABLE="0 $SIZE flakey $SCRATCH_DEV 0 0 180 1 error_writes"
94 _dmsetup_create $NAME --table "$TABLE" || \
95 _notrun "This test requires error_writes feature in dm-flakey"