generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / tests / btrfs / 141
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 Liu Bo.  All Rights Reserved.
4 #
5 # FS QA Test 141
6 #
7 # Regression test for btrfs buffered read's repair during read.
8 #
9 # Commit 20a7db8ab3f2 ("btrfs: add dummy callback for readpage_io_failed
10 # and drop checks") introduced the regression.
11 #
12 # The upstream fix is
13 #       Commit 9d0d1c8b1c9d ("Btrfs: bring back repair during read")
14 #
15 . ./common/preamble
16 _begin_fstest auto quick
17
18 # Import common functions.
19 . ./common/filter
20
21 # real QA test starts here
22
23 # Modify as appropriate.
24 _supported_fs btrfs
25 _require_scratch_dev_pool 2
26
27 _require_btrfs_command inspect-internal dump-tree
28 _require_command "$FILEFRAG_PROG" filefrag
29
30 get_physical()
31 {
32         local logical=$1
33         local stripe=$2
34         $BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 $SCRATCH_DEV | \
35                 grep $logical -A 6 | \
36                 $AWK_PROG "(\$1 ~ /stripe/ && \$3 ~ /devid/ && \$2 ~ /$stripe/) { print \$6 }"
37 }
38
39 get_devid()
40 {
41         local logical=$1
42         local stripe=$2
43         $BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 $SCRATCH_DEV | \
44                 grep $logical -A 6 | \
45                 $AWK_PROG "(\$1 ~ /stripe/ && \$3 ~ /devid/ && \$2 ~ /$stripe/) { print \$4 }"
46 }
47
48 get_device_path()
49 {
50         local devid=$1
51         echo "$SCRATCH_DEV_POOL" | $AWK_PROG "{print \$$devid}"
52 }
53
54 _scratch_dev_pool_get 2
55 # step 1, create a raid1 btrfs which contains one 128k file.
56 echo "step 1......mkfs.btrfs" >>$seqres.full
57
58 _check_minimal_fs_size $(( 1024 * 1024 * 1024 ))
59 mkfs_opts="-d raid1 -b 1G"
60 _scratch_pool_mkfs $mkfs_opts >>$seqres.full 2>&1
61
62 # make sure data is written to the start position of the data chunk
63 _scratch_mount $(_btrfs_no_v1_cache_opt)
64
65 $XFS_IO_PROG -f -d -c "pwrite -S 0xaa -b 128K 0 128K" "$SCRATCH_MNT/foobar" |\
66         _filter_xfs_io_offset
67
68 # step 2, corrupt the first 64k of one copy (on SCRATCH_DEV which is the first
69 # one in $SCRATCH_DEV_POOL
70 echo "step 2......corrupt file extent" >>$seqres.full
71
72 ${FILEFRAG_PROG} -v $SCRATCH_MNT/foobar >> $seqres.full
73 logical_in_btrfs=`${FILEFRAG_PROG} -v $SCRATCH_MNT/foobar | _filter_filefrag | cut -d '#' -f 1`
74 physical=$(get_physical ${logical_in_btrfs} 1)
75 devid=$(get_devid ${logical_in_btrfs} 1)
76 devpath=$(get_device_path ${devid})
77
78 _scratch_unmount
79 echo " corrupt stripe #1, devid $devid devpath $devpath physical $physical" \
80         >> $seqres.full
81 $XFS_IO_PROG -d -c "pwrite -S 0xbb -b 64K $physical 64K" $devpath > /dev/null
82
83 _scratch_mount
84
85 # step 3, 128k buffered read (this read can repair bad copy)
86 echo "step 3......repair the bad copy" >>$seqres.full
87
88 # since raid1 consists of two copies, and the bad copy was put on stripe #1
89 # while the good copy lies on stripe #0, the bad copy only gets access when the
90 # reader's pid % 2 == 1 is true
91 while true; do
92         echo 3 > /proc/sys/vm/drop_caches
93         $XFS_IO_PROG -c "pread -b 128K 0 128K" "$SCRATCH_MNT/foobar" > /dev/null &
94         pid=$!
95         wait
96         [ $((pid % 2)) == 1 ] && break
97 done
98
99 _scratch_unmount
100
101 # check if the repair works
102 $XFS_IO_PROG -c "pread -v -b 512 $physical 512" $devpath |\
103         _filter_xfs_io_offset
104
105 _scratch_dev_pool_put
106 # success, all done
107 status=0
108 exit