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