fa622568adfd977ac19a64004a044c5f04346f7d
[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 . ./common/preamble
12 _begin_fstest auto quick
13
14 # Import common functions.
15 . ./common/filter
16
17 # real QA test starts here
18 get_physical()
19 {
20         $BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 $SCRATCH_DEV | \
21                 grep $1 -A2 | \
22                 $AWK_PROG "(\$1 ~ /stripe/ && \$3 ~ /devid/ && \$2 ~ /0/) { print \$6 }"
23 }
24
25 # Modify as appropriate.
26 _supported_fs btrfs
27 # Overwriting data is forbidden on a zoned block device
28 _require_non_zoned_device $SCRATCH_DEV
29
30 _scratch_mkfs > /dev/null
31 # disable freespace inode to ensure file is the first thing in the data
32 # blobk group
33 _scratch_mount -o nospace_cache
34
35 pagesize=$(get_page_size)
36 blocksize=$(_get_block_size $SCRATCH_MNT)
37
38 # For subpage case, since we still do read in full page size, if have 8 corrupted
39 # sectors in one page, then even we just try to read one sector of that page,
40 # all 8 corruption will be reported.
41 # So here we chose the filesize using page size.
42 filesize=$((8*$pagesize))
43 if [ $blocksize -le $pagesize ]; then
44         sectors_per_page=$(($pagesize / $blocksize))
45 else
46         # We don't support multi-page sectorsize yet
47         _notrun "this test doesn't support sectorsize $blocksize with page size $pagesize yet"
48 fi
49
50 uuid=$(findmnt -n -o UUID "$SCRATCH_MNT")
51
52 if [ ! -e /sys/fs/btrfs/$uuid/bdi ]; then
53         _notrun "bdi link not found"
54 fi
55
56 #create an 8 block file
57 $XFS_IO_PROG -d -f -c "pwrite -S 0xbb -b $filesize 0 $filesize" "$SCRATCH_MNT/foobar" > /dev/null
58
59 logical_extent=$($FILEFRAG_PROG -v "$SCRATCH_MNT/foobar" | _filter_filefrag | cut -d '#' -f 1)
60 physical_extent=$(get_physical $logical_extent)
61
62 echo "logical = $logical_extent physical=$physical_extent" >> $seqres.full
63
64 # corrupt first 4 blocks of file
65 _scratch_unmount
66 $XFS_IO_PROG -d -c "pwrite -S 0xaa -b $pagesize $physical_extent $((4 * $pagesize))" $SCRATCH_DEV > /dev/null
67 _scratch_mount
68
69 # disable readahead to avoid skewing the counter
70 echo 0 > /sys/fs/btrfs/$uuid/bdi/read_ahead_kb
71
72 # buffered reads whould result in 2 * sectors_per_page errors since readahead code always submits
73 # at least 1 page worth of IO and it will be counted as error(s) as well
74 $XFS_IO_PROG -c "pread -b $filesize 0 $filesize" "$SCRATCH_MNT/foobar" > /dev/null 2>&1
75 errs=$($BTRFS_UTIL_PROG device stats $SCRATCH_DEV | awk '/corruption_errs/ { print $2 }')
76 if [ $errs -ne $((2 * $sectors_per_page)) ]; then
77         _fail "Errors: $errs expected: 2"
78 fi
79
80 # DIO does check every sector
81 $XFS_IO_PROG -d -c "pread -b $filesize 0 $filesize" "$SCRATCH_MNT/foobar" > /dev/null 2>&1
82 errs=$($BTRFS_UTIL_PROG device stats $SCRATCH_DEV | awk '/corruption_errs/ { print $2 }')
83 if [ $errs -ne $((6 * $sectors_per_page)) ]; then
84         _fail "Errors: $errs expected: 6"
85 fi
86
87 # success, all done
88 echo "Silence is golden"
89 status=0
90 exit