src/locktest: Change command macro names
[xfstests-dev.git] / common / dmdelay
1 ##/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2016 Red Hat, Inc.  All Rights Reserved.
4 #
5 # common functions for setting up and tearing down a dmdelay device
6
7 DELAY_NONE=0
8 DELAY_READ=1
9
10 echo $MOUNT_OPTIONS | grep -q dax
11 if [ $? -eq 0 ]; then
12         _notrun "Cannot run tests with DAX on dmdelay devices"
13 fi
14
15 _init_delay()
16 {
17         local BLK_DEV_SIZE=`blockdev --getsz $SCRATCH_DEV`
18         DELAY_DEV=/dev/mapper/delay-test
19         DELAY_TABLE="0 $BLK_DEV_SIZE delay $SCRATCH_DEV 0 0"
20         DELAY_TABLE_RDELAY="0 $BLK_DEV_SIZE delay $SCRATCH_DEV 0 10000 $SCRATCH_DEV 0 0"
21         _dmsetup_create delay-test --table "$DELAY_TABLE" || \
22                 _fatal "failed to create delay device"
23 }
24
25 _mount_delay()
26 {
27         _scratch_options mount
28         $MOUNT_PROG -t $FSTYP `_common_dev_mount_options` $SCRATCH_OPTIONS \
29                 $DELAY_DEV $SCRATCH_MNT
30 }
31
32 _unmount_delay()
33 {
34         $UMOUNT_PROG $SCRATCH_MNT
35 }
36
37 _cleanup_delay()
38 {
39         # If dmsetup load fails then we need to make sure to do resume here
40         # otherwise the umount will hang
41         $DMSETUP_PROG resume delay-test > /dev/null 2>&1
42         $UMOUNT_PROG $SCRATCH_MNT > /dev/null 2>&1
43         _dmsetup_remove delay-test
44 }
45
46 # _load_delay_table <table> [lockfs]
47 #
48 # This defaults to --nolockfs, which doesn't freeze_fs() before loading the new
49 # table
50 _load_delay_table()
51 {
52         table="$DELAY_TABLE"
53         [ $1 -eq $DELAY_READ ] && table="$DELAY_TABLE_RDELAY"
54
55         suspend_opt="--nolockfs"
56         [ $# -gt 1 ] && [ $2 -eq 1 ] && suspend_opt=""
57
58         # run a suspend/resume cycle to avoid excessive resume delays once a
59         # delay is introduced below
60         $DMSETUP_PROG suspend $suspend_opt delay-test
61         $DMSETUP_PROG resume $suspend_opt delay-test
62
63         $DMSETUP_PROG suspend $suspend_opt delay-test
64         [ $? -ne 0 ] && _fatal "failed to suspend delay-test"
65
66         $DMSETUP_PROG load delay-test --table "$table"
67         [ $? -ne 0 ] && _fatal "failed to load table into delay-test"
68
69         $DMSETUP_PROG resume delay-test
70         [ $? -ne 0 ] && _fatal  "failed to resume delay-test"
71 }