fstests: Replace all __[u]intNN_t types with standard [u]intNN_t
[xfstests-dev.git] / common / dmflakey
1 ##/bin/bash
2 #
3 # Copyright (c) 2013 Fusion IO, Inc.  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 dmflakey device
20
21 FLAKEY_ALLOW_WRITES=0
22 FLAKEY_DROP_WRITES=1
23 FLAKEY_ERROR_WRITES=2
24
25 echo $MOUNT_OPTIONS | grep -q dax
26 if [ $? -eq 0 ]; then
27         _notrun "Cannot run tests with DAX on dmflakey devices"
28 fi
29
30 _init_flakey()
31 {
32         local BLK_DEV_SIZE=`blockdev --getsz $SCRATCH_DEV`
33         FLAKEY_DEV=/dev/mapper/flakey-test
34         FLAKEY_TABLE="0 $BLK_DEV_SIZE flakey $SCRATCH_DEV 0 180 0"
35         FLAKEY_TABLE_DROP="0 $BLK_DEV_SIZE flakey $SCRATCH_DEV 0 0 180 1 drop_writes"
36         FLAKEY_TABLE_ERROR="0 $BLK_DEV_SIZE flakey $SCRATCH_DEV 0 0 180 1 error_writes"
37         $DMSETUP_PROG create flakey-test --table "$FLAKEY_TABLE" || \
38                 _fatal "failed to create flakey device"
39         $DMSETUP_PROG mknodes > /dev/null 2>&1
40 }
41
42 _mount_flakey()
43 {
44         _scratch_options mount
45         mount -t $FSTYP $SCRATCH_OPTIONS $MOUNT_OPTIONS $FLAKEY_DEV $SCRATCH_MNT
46 }
47
48 _unmount_flakey()
49 {
50         $UMOUNT_PROG $SCRATCH_MNT
51 }
52
53 _cleanup_flakey()
54 {
55         # If dmsetup load fails then we need to make sure to do resume here
56         # otherwise the umount will hang
57         $DMSETUP_PROG resume flakey-test > /dev/null 2>&1
58         $UMOUNT_PROG $SCRATCH_MNT > /dev/null 2>&1
59         # wait for device to be fully settled so that 'dmsetup remove' doesn't
60         # fail due to EBUSY
61         $UDEV_SETTLE_PROG >/dev/null 2>&1
62         $DMSETUP_PROG remove flakey-test > /dev/null 2>&1
63         $DMSETUP_PROG mknodes > /dev/null 2>&1
64 }
65
66 # _load_flakey_table <table> [lockfs]
67 #
68 # This defaults to --nolockfs, which doesn't freeze_fs() before loading the new
69 # table, so it simulates power failure.
70 _load_flakey_table()
71 {
72         table="$FLAKEY_TABLE"
73         [ $1 -eq $FLAKEY_DROP_WRITES ] && table="$FLAKEY_TABLE_DROP"
74         [ $1 -eq $FLAKEY_ERROR_WRITES ] && table="$FLAKEY_TABLE_ERROR"
75
76         suspend_opt="--nolockfs"
77         [ $# -gt 1 ] && [ $2 -eq 1 ] && suspend_opt=""
78
79         $DMSETUP_PROG suspend $suspend_opt flakey-test
80         [ $? -ne 0 ] && _fatal "failed to suspend flakey-test"
81
82         # There may be multiple dm targets in the table, and these dm targets
83         # will be joined by the newline ("\n"). Option --table can not cope with
84         # the multiple-targets case, so get them by reading from standard input.
85         echo -e "$table" | $DMSETUP_PROG load flakey-test
86         [ $? -ne 0 ] && _fatal "failed to load table into flakey-test"
87
88         $DMSETUP_PROG resume flakey-test
89         [ $? -ne 0 ] && _fatal  "failed to resume flakey-test"
90 }
91
92 # Silently drop all writes and unmount/remount to simulate a crash/power
93 # failure.
94 _flakey_drop_and_remount()
95 {
96         _load_flakey_table $FLAKEY_DROP_WRITES
97         _unmount_flakey
98
99         if [ "x$1" = "xyes" ]; then
100                 _check_scratch_fs $FLAKEY_DEV
101         fi
102
103         _load_flakey_table $FLAKEY_ALLOW_WRITES
104         _mount_flakey
105 }
106
107 _require_flakey_with_error_writes()
108 {
109         local SIZE
110         local TABLE
111         local NAME=flakey-test
112
113         _require_dm_target flakey
114
115         SIZE=`blockdev --getsz $SCRATCH_DEV`
116         TABLE="0 $SIZE flakey $SCRATCH_DEV 0 0 180 1 error_writes"
117
118         $DMSETUP_PROG create $NAME --table "$TABLE" >/dev/null 2>&1
119         if [ $? -ne 0 ]; then
120                 _notrun "This test requires error_writes feature in dm-flakey"
121         fi
122         $DMSETUP_PROG mknodes >/dev/null 2>&1
123
124         _cleanup_flakey
125 }