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