misc: move exit status into trap handler
[xfstests-dev.git] / tests / btrfs / 215
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2020 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FS QA Test 215
6 #
7 # Test that reading corrupted files would correctly increment device status
8 # counters. This is fixed by the following linux kernel commit:
9 # 814723e0a55a ("btrfs: increment device corruption error in case of checksum error")
10 #
11 seq=`basename $0`
12 seqres=$RESULT_DIR/$seq
13 echo "QA output created by $seq"
14
15 here=`pwd`
16 tmp=/tmp/$$
17 status=1        # failure is the default!
18 trap "_cleanup; exit \$status" 0 1 2 3 15
19
20 _cleanup()
21 {
22         cd /
23         rm -f $tmp.*
24 }
25
26 # get standard environment, filters and checks
27 . ./common/rc
28 . ./common/filter
29
30 # remove previous $seqres.full before test
31 rm -f $seqres.full
32
33 # real QA test starts here
34 get_physical()
35 {
36         $BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 $SCRATCH_DEV | \
37                 grep $1 -A2 | \
38                 $AWK_PROG "(\$1 ~ /stripe/ && \$3 ~ /devid/ && \$2 ~ /0/) { print \$6 }"
39 }
40
41 # Modify as appropriate.
42 _supported_fs btrfs
43
44 _scratch_mkfs > /dev/null
45 # disable freespace inode to ensure file is the first thing in the data
46 # blobk group
47 _scratch_mount -o nospace_cache
48
49 pagesize=$(get_page_size)
50 blocksize=$(_get_block_size $SCRATCH_MNT)
51
52 # For subpage case, since we still do read in full page size, if have 8 corrupted
53 # sectors in one page, then even we just try to read one sector of that page,
54 # all 8 corruption will be reported.
55 # So here we chose the filesize using page size.
56 filesize=$((8*$pagesize))
57 if [ $blocksize -le $pagesize ]; then
58         sectors_per_page=$(($pagesize / $blocksize))
59 else
60         # We don't support multi-page sectorsize yet
61         _notrun "this test doesn't support sectorsize $blocksize with page size $pagesize yet"
62 fi
63
64 uuid=$(findmnt -n -o UUID "$SCRATCH_MNT")
65
66 if [ ! -e /sys/fs/btrfs/$uuid/bdi ]; then
67         _notrun "bdi link not found"
68 fi
69
70 #create an 8 block file
71 $XFS_IO_PROG -d -f -c "pwrite -S 0xbb -b $filesize 0 $filesize" "$SCRATCH_MNT/foobar" > /dev/null
72
73 logical_extent=$($FILEFRAG_PROG -v "$SCRATCH_MNT/foobar" | _filter_filefrag | cut -d '#' -f 1)
74 physical_extent=$(get_physical $logical_extent)
75
76 echo "logical = $logical_extent physical=$physical_extent" >> $seqres.full
77
78 # corrupt first 4 blocks of file
79 _scratch_unmount
80 $XFS_IO_PROG -d -c "pwrite -S 0xaa -b $pagesize $physical_extent $((4 * $pagesize))" $SCRATCH_DEV > /dev/null
81 _scratch_mount
82
83 # disable readahead to avoid skewing the counter
84 echo 0 > /sys/fs/btrfs/$uuid/bdi/read_ahead_kb
85
86 # buffered reads whould result in 2 * sectors_per_page errors since readahead code always submits
87 # at least 1 page worth of IO and it will be counted as error(s) as well
88 $XFS_IO_PROG -c "pread -b $filesize 0 $filesize" "$SCRATCH_MNT/foobar" > /dev/null 2>&1
89 errs=$($BTRFS_UTIL_PROG device stats $SCRATCH_DEV | awk '/corruption_errs/ { print $2 }')
90 if [ $errs -ne $((2 * $sectors_per_page)) ]; then
91         _fail "Errors: $errs expected: 2"
92 fi
93
94 # DIO does check every sector
95 $XFS_IO_PROG -d -c "pread -b $filesize 0 $filesize" "$SCRATCH_MNT/foobar" > /dev/null 2>&1
96 errs=$($BTRFS_UTIL_PROG device stats $SCRATCH_DEV | awk '/corruption_errs/ { print $2 }')
97 if [ $errs -ne $((6 * $sectors_per_page)) ]; then
98         _fail "Errors: $errs expected: 6"
99 fi
100
101 # success, all done
102 echo "Silence is golden"
103 status=0
104 exit