generic: test MADV_POPULATE_READ with IO errors
[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 _init_delay()
11 {
12         local BLK_DEV_SIZE=`blockdev --getsz $SCRATCH_DEV`
13         DELAY_DEV=/dev/mapper/delay-test
14         DELAY_TABLE="0 $BLK_DEV_SIZE delay $SCRATCH_DEV 0 0"
15         DELAY_TABLE_RDELAY="0 $BLK_DEV_SIZE delay $SCRATCH_DEV 0 10000 $SCRATCH_DEV 0 0"
16         _dmsetup_create delay-test --table "$DELAY_TABLE" || \
17                 _fatal "failed to create delay device"
18 }
19
20 _mount_delay()
21 {
22         _scratch_options mount
23         $MOUNT_PROG -t $FSTYP `_common_dev_mount_options` $SCRATCH_OPTIONS \
24                 $DELAY_DEV $SCRATCH_MNT
25 }
26
27 _unmount_delay()
28 {
29         $UMOUNT_PROG $SCRATCH_MNT
30 }
31
32 _cleanup_delay()
33 {
34         # If dmsetup load fails then we need to make sure to do resume here
35         # otherwise the umount will hang
36         $DMSETUP_PROG resume delay-test > /dev/null 2>&1
37         $UMOUNT_PROG $SCRATCH_MNT > /dev/null 2>&1
38         _dmsetup_remove delay-test
39 }
40
41 # _load_delay_table <table> [lockfs]
42 #
43 # This defaults to --nolockfs, which doesn't freeze_fs() before loading the new
44 # table
45 _load_delay_table()
46 {
47         table="$DELAY_TABLE"
48         [ $1 -eq $DELAY_READ ] && table="$DELAY_TABLE_RDELAY"
49
50         suspend_opt="--nolockfs"
51         [ $# -gt 1 ] && [ $2 -eq 1 ] && suspend_opt=""
52
53         # run a suspend/resume cycle to avoid excessive resume delays once a
54         # delay is introduced below
55         $DMSETUP_PROG suspend $suspend_opt delay-test
56         $DMSETUP_PROG resume $suspend_opt delay-test
57
58         $DMSETUP_PROG suspend $suspend_opt delay-test
59         [ $? -ne 0 ] && _fatal "failed to suspend delay-test"
60
61         $DMSETUP_PROG load delay-test --table "$table"
62         [ $? -ne 0 ] && _fatal "failed to load table into delay-test"
63
64         $DMSETUP_PROG resume delay-test
65         [ $? -ne 0 ] && _fatal  "failed to resume delay-test"
66 }