btrfs/057: Fix false alerts due to orphan files
[xfstests-dev.git] / tests / btrfs / 143
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 Liu Bo.  All Rights Reserved.
4 #
5 # FS QA Test 143
6 #
7 # Regression test for btrfs buffered read's repair during read without checksum.
8 #
9 # This is to test whether buffered read retry-repair code is able to work in
10 # raid1 case as expected.
11 #
12 # Please note that without checksum, btrfs doesn't know if the data used to
13 # repair is correct, so repair is more of resync which makes sure that both
14 # of the copy has the same content.
15 #
16 # Commit 20a7db8ab3f2 ("btrfs: add dummy callback for readpage_io_failed and drop
17 # checks") introduced the regression.
18 #
19 # The upstream fix is
20 #       commit 9d0d1c8b1c9d ("Btrfs: bring back repair during read")
21 #
22 seq=`basename $0`
23 seqres=$RESULT_DIR/$seq
24 echo "QA output created by $seq"
25
26 here=`pwd`
27 tmp=/tmp/$$
28 status=1        # failure is the default!
29 trap "_cleanup; exit \$status" 0 1 2 3 15
30
31 _cleanup()
32 {
33         cd /
34         rm -f $tmp.*
35 }
36
37 # get standard environment, filters and checks
38 . ./common/rc
39 . ./common/filter
40
41 # remove previous $seqres.full before test
42 rm -f $seqres.full
43
44 # real QA test starts here
45
46 # Modify as appropriate.
47 _supported_fs btrfs
48 _supported_os Linux
49 _require_fail_make_request
50 _require_scratch_dev_pool 2
51
52 _require_btrfs_command inspect-internal dump-tree
53 _require_command "$FILEFRAG_PROG" filefrag
54
55 get_physical()
56 {
57         # $1 is logical address
58         # print chunk tree and find devid 2 which is $SCRATCH_DEV
59         $BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 $SCRATCH_DEV | \
60         grep $1 -A 6 | awk '($1 ~ /stripe/ && $3 ~ /devid/ && $4 ~ /1/) { print $6 }'
61 }
62
63 SYSFS_BDEV=`_sysfs_dev $SCRATCH_DEV`
64
65 start_fail()
66 {
67         echo 100 > $DEBUGFS_MNT/fail_make_request/probability
68         # the 1st one fails the first bio which is reading 4k (or more due to
69         # readahead), and the 2nd one fails the retry of validation so that it
70         # triggers read-repair
71         echo 2 > $DEBUGFS_MNT/fail_make_request/times
72         echo 0 > $DEBUGFS_MNT/fail_make_request/verbose
73         echo 1 > $SYSFS_BDEV/make-it-fail
74 }
75
76 stop_fail()
77 {
78         echo 0 > $DEBUGFS_MNT/fail_make_request/probability
79         echo 0 > $DEBUGFS_MNT/fail_make_request/times
80         echo 0 > $SYSFS_BDEV/make-it-fail
81 }
82
83 _scratch_dev_pool_get 2
84 # step 1, create a raid1 btrfs which contains one 128k file.
85 echo "step 1......mkfs.btrfs" >>$seqres.full
86
87 mkfs_opts="-d raid1 -b 1G"
88 _scratch_pool_mkfs $mkfs_opts >>$seqres.full 2>&1
89
90 # -o nospace_cache makes sure data is written to the start position of the data
91 # chunk
92 _scratch_mount -o nospace_cache,nodatasum
93
94 $XFS_IO_PROG -f -d -c "pwrite -S 0xaa -b 128K 0 128K" "$SCRATCH_MNT/foobar" |\
95         _filter_xfs_io_offset
96
97 # step 2, corrupt the first 64k of one copy (on SCRATCH_DEV which is the first
98 # one in $SCRATCH_DEV_POOL
99 echo "step 2......corrupt file extent" >>$seqres.full
100
101 ${FILEFRAG_PROG} -v $SCRATCH_MNT/foobar >> $seqres.full
102 logical_in_btrfs=`${FILEFRAG_PROG} -v $SCRATCH_MNT/foobar | _filter_filefrag | cut -d '#' -f 1`
103 physical_on_scratch=`get_physical ${logical_in_btrfs}`
104
105 _scratch_unmount
106 $XFS_IO_PROG -d -c "pwrite -S 0xbb -b 64K $physical_on_scratch 64K" $SCRATCH_DEV |\
107         _filter_xfs_io_offset
108
109 _scratch_mount -o nospace_cache
110
111 # step 3, 128k buffered read (this read can repair bad copy)
112 echo "step 3......repair the bad copy" >>$seqres.full
113
114 # since raid1 consists of two copies, and the bad copy was put on stripe #1
115 # while the good copy lies on stripe #0, the bad copy only gets access when the
116 # reader's pid % 2 == 1 is true
117 while [[ -z ${result} ]]; do
118     # invalidate the page cache.
119     _scratch_cycle_mount
120
121     start_fail
122     result=$(bash -c "
123         if [[ \$((\$\$ % 2)) -eq 1 ]]; then
124                 exec $XFS_IO_PROG -c \"pread 0 4K\" \"$SCRATCH_MNT/foobar\"
125         fi");
126     stop_fail
127 done
128
129 _scratch_unmount
130
131 # check if the repair works
132 $XFS_IO_PROG -c "pread -v -b 512 $physical_on_scratch 512" $SCRATCH_DEV |\
133         _filter_xfs_io_offset
134
135 _scratch_dev_pool_put
136 # success, all done
137 status=0
138 exit