1105253cf109ad941e2725f9a62ee64e336293cf
[xfstests-dev.git] / tests / btrfs / 157
1 #! /bin/bash
2 # FS QA Test 157
3 #
4 # The test case is to reproduce a bug in raid6 reconstruction process that
5 # would end up with read failure if there is data corruption on two disks in
6 # the same horizontal stripe, e.g.  due to bitrot.
7 #
8 # The bug happens when
9 # a) all disks are good to read,
10 # b) there is corrupted data on two disks in the same horizontal stripe due to
11 # something like bitrot,
12 # c) when rebuilding data after crc fails, btrfs is not able to tell whether
13 # other copies are good or corrupted because btrfs doesn't have crc for
14 # unallocated blocks.
15 #
16 # The kernel fixes are
17 # Btrfs: do not merge rbios if their fail stripe index are not identical
18 # Btrfs: make raid6 rebuild retry more
19 #
20 #-----------------------------------------------------------------------
21 # Copyright (c) 2017 Oracle.  All Rights Reserved.
22 #
23 # This program is free software; you can redistribute it and/or
24 # modify it under the terms of the GNU General Public License as
25 # published by the Free Software Foundation.
26 #
27 # This program is distributed in the hope that it would be useful,
28 # but WITHOUT ANY WARRANTY; without even the implied warranty of
29 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30 # GNU General Public License for more details.
31 #
32 # You should have received a copy of the GNU General Public License
33 # along with this program; if not, write the Free Software Foundation,
34 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
35 #-----------------------------------------------------------------------
36 #
37
38 seq=`basename $0`
39 seqres=$RESULT_DIR/$seq
40 echo "QA output created by $seq"
41
42 here=`pwd`
43 tmp=/tmp/$$
44 status=1        # failure is the default!
45 trap "_cleanup; exit \$status" 0 1 2 3 15
46
47 _cleanup()
48 {
49         cd /
50         rm -f $tmp.*
51 }
52
53 # get standard environment, filters and checks
54 . ./common/rc
55 . ./common/filter
56
57 # remove previous $seqres.full before test
58 rm -f $seqres.full
59
60 # real QA test starts here
61
62 # Modify as appropriate.
63 _supported_fs btrfs
64 _supported_os Linux
65 _require_scratch_dev_pool 4
66 _require_btrfs_command inspect-internal dump-tree
67
68 get_physical_stripe0()
69 {
70         $BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 $SCRATCH_DEV | \
71         grep " DATA\|RAID6" -A 10 | \
72         $AWK_PROG '($1 ~ /stripe/ && $3 ~ /devid/ && $2 ~ /0/) { print $6 }'
73 }
74
75 get_physical_stripe1()
76 {
77         $BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 $SCRATCH_DEV | \
78         grep " DATA\|RAID6" -A 10 | \
79         $AWK_PROG '($1 ~ /stripe/ && $3 ~ /devid/ && $2 ~ /1/) { print $6 }'
80 }
81
82 _scratch_dev_pool_get 4
83 # step 1: create a raid6 btrfs and create a 4K file
84 echo "step 1......mkfs.btrfs" >>$seqres.full
85
86 mkfs_opts="-d raid6 -b 1G"
87 _scratch_pool_mkfs $mkfs_opts >>$seqres.full 2>&1
88
89 # -o nospace_cache makes sure data is written to the start position of the data
90 # chunk
91 _scratch_mount -o nospace_cache
92
93 # [0,64K) is written to stripe 0 and [64K, 128K) is written to stripe 1
94 $XFS_IO_PROG -f -d -c "pwrite -S 0xaa 0 128K" -c "fsync" \
95         "$SCRATCH_MNT/foobar" | _filter_xfs_io
96
97 _scratch_unmount
98
99 stripe_0=`get_physical_stripe0`
100 stripe_1=`get_physical_stripe1`
101 dev4=`echo $SCRATCH_DEV_POOL | awk '{print $4}'`
102 dev3=`echo $SCRATCH_DEV_POOL | awk '{print $3}'`
103
104 # step 2: corrupt the 1st and 2nd stripe (stripe 0 and 1)
105 echo "step 2......simulate bitrot at offset $stripe_0 of device_4($dev4) and offset $stripe_1 of device_3($dev3)" >>$seqres.full
106
107 $XFS_IO_PROG -f -d -c "pwrite -S 0xbb $stripe_0 64K" $dev4 | _filter_xfs_io
108 $XFS_IO_PROG -f -d -c "pwrite -S 0xbb $stripe_1 64K" $dev3 | _filter_xfs_io
109
110 # step 3: read foobar to repair the bitrot
111 echo "step 3......repair the bitrot" >> $seqres.full
112 _scratch_mount -o nospace_cache
113
114 # read the 2nd stripe, i.e. [64K, 128K), to trigger repair
115 od -x -j 64K $SCRATCH_MNT/foobar
116
117 _scratch_dev_pool_put
118
119 # success, all done
120 status=0
121 exit