generic/192: fix instability on exFAT
[xfstests-dev.git] / common / dmflakey
1 ##/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2013 Fusion IO, Inc.  All Rights Reserved.
4 #
5 # common functions for setting up and tearing down a dmflakey device
6
7 FLAKEY_ALLOW_WRITES=0
8 FLAKEY_DROP_WRITES=1
9 FLAKEY_ERROR_WRITES=2
10
11 _init_flakey()
12 {
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"
20 }
21
22 _mount_flakey()
23 {
24         _scratch_options mount
25         mount -t $FSTYP $SCRATCH_OPTIONS $MOUNT_OPTIONS $FLAKEY_DEV $SCRATCH_MNT
26 }
27
28 _unmount_flakey()
29 {
30         $UMOUNT_PROG $SCRATCH_MNT
31 }
32
33 _cleanup_flakey()
34 {
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
40 }
41
42 # _load_flakey_table <table> [lockfs]
43 #
44 # This defaults to --nolockfs, which doesn't freeze_fs() before loading the new
45 # table, so it simulates power failure.
46 _load_flakey_table()
47 {
48         table="$FLAKEY_TABLE"
49         [ $1 -eq $FLAKEY_DROP_WRITES ] && table="$FLAKEY_TABLE_DROP"
50         [ $1 -eq $FLAKEY_ERROR_WRITES ] && table="$FLAKEY_TABLE_ERROR"
51
52         suspend_opt="--nolockfs"
53         [ $# -gt 1 ] && [ $2 -eq 1 ] && suspend_opt=""
54
55         $DMSETUP_PROG suspend $suspend_opt flakey-test
56         [ $? -ne 0 ] && _fatal "failed to suspend flakey-test"
57
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"
63
64         $DMSETUP_PROG resume flakey-test
65         [ $? -ne 0 ] && _fatal  "failed to resume flakey-test"
66 }
67
68 # Silently drop all writes and unmount/remount to simulate a crash/power
69 # failure.
70 _flakey_drop_and_remount()
71 {
72         _load_flakey_table $FLAKEY_DROP_WRITES
73         _unmount_flakey
74
75         if [ "x$1" = "xyes" ]; then
76                 _check_scratch_fs $FLAKEY_DEV
77         fi
78
79         _load_flakey_table $FLAKEY_ALLOW_WRITES
80         _mount_flakey
81 }
82
83 _require_flakey_with_error_writes()
84 {
85         local SIZE
86         local TABLE
87         local NAME=flakey-test
88
89         _require_dm_target flakey
90
91         SIZE=`blockdev --getsz $SCRATCH_DEV`
92         TABLE="0 $SIZE flakey $SCRATCH_DEV 0 0 180 1 error_writes"
93
94         _dmsetup_create $NAME --table "$TABLE" || \
95                 _notrun "This test requires error_writes feature in dm-flakey"
96
97         _cleanup_flakey
98 }