common: add helper function _flakey_drop_and_remount
[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
24 _init_flakey()
25 {
26         local BLK_DEV_SIZE=`blockdev --getsz $SCRATCH_DEV`
27         FLAKEY_DEV=/dev/mapper/flakey-test
28         FLAKEY_TABLE="0 $BLK_DEV_SIZE flakey $SCRATCH_DEV 0 180 0"
29         FLAKEY_TABLE_DROP="0 $BLK_DEV_SIZE flakey $SCRATCH_DEV 0 0 180 1 drop_writes"
30         $DMSETUP_PROG create flakey-test --table "$FLAKEY_TABLE" || \
31                 _fatal "failed to create flakey device"
32         $DMSETUP_PROG mknodes > /dev/null 2>&1
33 }
34
35 _mount_flakey()
36 {
37         mount -t $FSTYP $MOUNT_OPTIONS $FLAKEY_DEV $SCRATCH_MNT
38 }
39
40 _unmount_flakey()
41 {
42         $UMOUNT_PROG $SCRATCH_MNT
43 }
44
45 _cleanup_flakey()
46 {
47         # If dmsetup load fails then we need to make sure to do resume here
48         # otherwise the umount will hang
49         $DMSETUP_PROG resume flakey-test > /dev/null 2>&1
50         $UMOUNT_PROG $SCRATCH_MNT > /dev/null 2>&1
51         $DMSETUP_PROG remove flakey-test > /dev/null 2>&1
52         $DMSETUP_PROG mknodes > /dev/null 2>&1
53 }
54
55 # _load_flakey_table <table> [lockfs]
56 #
57 # This defaults to --nolockfs, which doesn't freeze_fs() before loading the new
58 # table, so it simulates power failure.
59 _load_flakey_table()
60 {
61
62         table="$FLAKEY_TABLE"
63         [ $1 -eq $FLAKEY_DROP_WRITES ] && table="$FLAKEY_TABLE_DROP"
64
65         suspend_opt="--nolockfs"
66         [ $# -gt 1 ] && [ $2 -eq 1 ] && suspend_opt=""
67
68         $DMSETUP_PROG suspend $suspend_opt flakey-test
69         [ $? -ne 0 ] && _fatal "failed to suspend flakey-test"
70
71         $DMSETUP_PROG load flakey-test --table "$table"
72         [ $? -ne 0 ] && _fatal "failed to load table into flakey-test"
73
74         $DMSETUP_PROG resume flakey-test
75         [ $? -ne 0 ] && _fatal  "failed to resumeflakey-test"
76 }
77
78 # Silently drop all writes and unmount/remount to simulate a crash/power
79 # failure.
80 _flakey_drop_and_remount()
81 {
82         _load_flakey_table $FLAKEY_DROP_WRITES
83         _unmount_flakey
84
85         if [ "x$1" = "xyes" ]; then
86                 _check_scratch_fs $FLAKEY_DEV
87         fi
88
89         _load_flakey_table $FLAKEY_ALLOW_WRITES
90         _mount_flakey
91 }