btrfs: check qgroup doesn't crash when beyond limit
[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 . ./common/dmdust
41
42 # remove previous $seqres.full before test
43 rm -f $seqres.full
44
45 # real QA test starts here
46
47 # Modify as appropriate.
48 _supported_fs btrfs
49 _require_scratch_dev_pool 2
50 _require_dm_target dust
51
52 _require_btrfs_command inspect-internal dump-tree
53 _require_command "$FILEFRAG_PROG" filefrag
54
55 _scratch_dev_pool_get 2
56 # step 1, create a raid1 btrfs which contains one 128k file.
57 echo "step 1......mkfs.btrfs" >>$seqres.full
58
59 mkfs_opts="-d raid1 -b 1G"
60 _scratch_pool_mkfs $mkfs_opts >>$seqres.full 2>&1
61
62 # -o nospace_cache makes sure data is written to the start position of the data
63 # chunk
64 _scratch_mount -o nospace_cache,nodatasum
65
66 $XFS_IO_PROG -f -d -c "pwrite -S 0xaa -b 128K 0 128K" "$SCRATCH_MNT/foobar" |\
67         _filter_xfs_io_offset
68
69 # step 2, corrupt the first 64k of stripe #1
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 echo "Logical offset is $logical_in_btrfs" >>$seqres.full
75 _scratch_unmount
76
77 read -r stripe physical < <(
78 $BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 "$SCRATCH_DEV" |
79         grep "$logical_in_btrfs" -A 6 |
80         $AWK_PROG '$1 == "stripe" && $3 == "devid" && $4 == 1 { print $2 " " $6; exit }')
81 echo "Physical offset of stripe $stripe is $physical on devid 1" >> $seqres.full
82
83 $XFS_IO_PROG -d -c "pwrite -S 0xbb -b 64K $physical 64K" "$SCRATCH_DEV" > /dev/null
84
85 _init_dust
86 _mount_dust
87
88 # step 3, 128k buffered read (this read can repair bad copy)
89 echo "step 3......repair the bad copy" >>$seqres.full
90
91 $DMSETUP_PROG message dust-test 0 addbadblock $((physical / 512))
92 $DMSETUP_PROG message dust-test 0 enable
93 while [[ -z $( (( BASHPID % 2 == stripe )) &&
94                exec $XFS_IO_PROG -c "pread -b 128K 0 128K" "$SCRATCH_MNT/foobar") ]]; do
95         :
96 done
97
98 _cleanup_dust
99
100 # check if the repair works
101 $XFS_IO_PROG -c "pread -v -b 512 $physical 512" "$SCRATCH_DEV" |
102         _filter_xfs_io_offset
103
104 _scratch_dev_pool_put
105 # success, all done
106 status=0
107 exit