common/config: implement set_prog_path() using 'type -P'
[xfstests-dev.git] / common / dmdelay
1 ##/bin/bash
2 #
3 # Copyright (c) 2016 Red Hat, 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 dmdelay device
20
21 DELAY_NONE=0
22 DELAY_READ=1
23
24 echo $MOUNT_OPTIONS | grep -q dax
25 if [ $? -eq 0 ]; then
26         _notrun "Cannot run tests with DAX on dmdelay devices"
27 fi
28
29 _init_delay()
30 {
31         local BLK_DEV_SIZE=`blockdev --getsz $SCRATCH_DEV`
32         DELAY_DEV=/dev/mapper/delay-test
33         DELAY_TABLE="0 $BLK_DEV_SIZE delay $SCRATCH_DEV 0 0"
34         DELAY_TABLE_RDELAY="0 $BLK_DEV_SIZE delay $SCRATCH_DEV 0 10000 $SCRATCH_DEV 0 0"
35         $DMSETUP_PROG create delay-test --table "$DELAY_TABLE" || \
36                 _fatal "failed to create delay device"
37         $DMSETUP_PROG mknodes > /dev/null 2>&1
38 }
39
40 _mount_delay()
41 {
42         _scratch_options mount
43         $MOUNT_PROG -t $FSTYP `_common_dev_mount_options` $SCRATCH_OPTIONS \
44                 $DELAY_DEV $SCRATCH_MNT
45 }
46
47 _unmount_delay()
48 {
49         $UMOUNT_PROG $SCRATCH_MNT
50 }
51
52 _cleanup_delay()
53 {
54         # If dmsetup load fails then we need to make sure to do resume here
55         # otherwise the umount will hang
56         $DMSETUP_PROG resume delay-test > /dev/null 2>&1
57         $UMOUNT_PROG $SCRATCH_MNT > /dev/null 2>&1
58         # wait for device to be fully settled so that 'dmsetup remove' doesn't
59         # fail due to EBUSY
60         $UDEV_SETTLE_PROG >/dev/null 2>&1
61         $DMSETUP_PROG remove delay-test > /dev/null 2>&1
62         $DMSETUP_PROG mknodes > /dev/null 2>&1
63 }
64
65 # _load_delay_table <table> [lockfs]
66 #
67 # This defaults to --nolockfs, which doesn't freeze_fs() before loading the new
68 # table
69 _load_delay_table()
70 {
71         table="$DELAY_TABLE"
72         [ $1 -eq $DELAY_READ ] && table="$DELAY_TABLE_RDELAY"
73
74         suspend_opt="--nolockfs"
75         [ $# -gt 1 ] && [ $2 -eq 1 ] && suspend_opt=""
76
77         # run a suspend/resume cycle to avoid excessive resume delays once a
78         # delay is introduced below
79         $DMSETUP_PROG suspend $suspend_opt delay-test
80         $DMSETUP_PROG resume $suspend_opt delay-test
81
82         $DMSETUP_PROG suspend $suspend_opt delay-test
83         [ $? -ne 0 ] && _fatal "failed to suspend delay-test"
84
85         $DMSETUP_PROG load delay-test --table "$table"
86         [ $? -ne 0 ] && _fatal "failed to load table into delay-test"
87
88         $DMSETUP_PROG resume delay-test
89         [ $? -ne 0 ] && _fatal  "failed to resume delay-test"
90 }