2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2020 SUSE Linux Products GmbH. All Rights Reserved.
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")
12 seqres=$RESULT_DIR/$seq
13 echo "QA output created by $seq"
17 status=1 # failure is the default!
18 trap "_cleanup; exit \$status" 0 1 2 3 15
26 # get standard environment, filters and checks
30 # remove previous $seqres.full before test
33 # real QA test starts here
36 $BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 $SCRATCH_DEV | \
38 $AWK_PROG "(\$1 ~ /stripe/ && \$3 ~ /devid/ && \$2 ~ /0/) { print \$6 }"
41 # Modify as appropriate.
44 _scratch_mkfs > /dev/null
45 # disable freespace inode to ensure file is the first thing in the data
47 _scratch_mount -o nospace_cache
49 blocksize=$(_get_block_size $SCRATCH_MNT)
50 filesize=$((8*$blocksize))
51 uuid=$(findmnt -n -o UUID "$SCRATCH_MNT")
53 if [ ! -e /sys/fs/btrfs/$uuid/bdi ]; then
54 _notrun "bdi link not found"
57 #create an 8 block file
58 $XFS_IO_PROG -d -f -c "pwrite -S 0xbb -b $filesize 0 $filesize" "$SCRATCH_MNT/foobar" > /dev/null
60 logical_extent=$($FILEFRAG_PROG -v "$SCRATCH_MNT/foobar" | _filter_filefrag | cut -d '#' -f 1)
61 physical_extent=$(get_physical $logical_extent)
63 echo "logical = $logical_extent physical=$physical_extent" >> $seqres.full
65 # corrupt first 4 blocks of file
67 $XFS_IO_PROG -d -c "pwrite -S 0xaa -b $blocksize $physical_extent $((4*$blocksize))" $SCRATCH_DEV > /dev/null
70 # disable readahead to avoid skewing the counter
71 echo 0 > /sys/fs/btrfs/$uuid/bdi/read_ahead_kb
73 # buffered reads whould result in 2 errors since readahead code always submits
74 # at least 1 page worth of IO and it will be counted as an error as well
75 $XFS_IO_PROG -c "pread -b $filesize 0 $filesize" "$SCRATCH_MNT/foobar" > /dev/null 2>&1
76 errs=$($BTRFS_UTIL_PROG device stats $SCRATCH_DEV | awk '/corruption_errs/ { print $2 }')
77 if [ $errs -ne 2 ]; then
78 _fail "Errors: $errs expected: 2"
81 # DIO does check every sector
82 $XFS_IO_PROG -d -c "pread -b $filesize 0 $filesize" "$SCRATCH_MNT/foobar" > /dev/null 2>&1
83 errs=$($BTRFS_UTIL_PROG device stats $SCRATCH_DEV | awk '/corruption_errs/ { print $2 }')
84 if [ $errs -ne 6 ]; then
85 _fail "Errors: $errs expected: 6"
89 echo "Silence is golden"