2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2014 Filipe Manana. All Rights Reserved.
5 # FS QA Test No. btrfs/037
7 # Test for a btrfs data corruption when using compressed files/extents.
8 # Under certain cases, it was possible for reads to return random data
9 # (content from a previously used page) instead of zeroes. This also
10 # caused partial updates to those regions that were supposed to be filled
11 # with zeroes to save random (and invalid) data into the file extents.
13 # This is fixed by the commit for the linux kernel titled:
15 # Btrfs: fix data corruption when reading/updating compressed extents
18 seqres=$RESULT_DIR/$seq
19 echo "QA output created by $seq"
23 status=1 # failure is the default!
24 trap "_cleanup; exit \$status" 0 1 2 3 15
31 # get standard environment, filters and checks
35 # real QA test starts here
38 _require_xfs_io_command "falloc"
42 _scratch_mkfs >/dev/null 2>&1
43 _scratch_mount "-o compress-force=lzo"
45 $XFS_IO_PROG -f -c "pwrite -S 0x06 -b 18670 266978 18670" \
46 $SCRATCH_MNT/foobar | _filter_xfs_io
47 $XFS_IO_PROG -c "falloc 26450 665194" $SCRATCH_MNT/foobar | _filter_xfs_io
48 $XFS_IO_PROG -c "truncate 542872" $SCRATCH_MNT/foobar | _filter_xfs_io
49 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foobar | _filter_xfs_io
51 # Expected file items in the fs tree are (from btrfs-debug-tree):
53 # item 4 key (257 INODE_ITEM 0) itemoff 15879 itemsize 160
54 # inode generation 6 transid 6 size 542872 block group 0 mode 100600
55 # item 5 key (257 INODE_REF 256) itemoff 15863 itemsize 16
56 # inode ref index 2 namelen 6 name: foobar
57 # item 6 key (257 EXTENT_DATA 0) itemoff 15810 itemsize 53
58 # extent data disk byte 0 nr 0 gen 6
59 # extent data offset 0 nr 24576 ram 266240
60 # extent compression 0
61 # item 7 key (257 EXTENT_DATA 24576) itemoff 15757 itemsize 53
62 # prealloc data disk byte 12849152 nr 241664 gen 6
63 # prealloc data offset 0 nr 241664
64 # item 8 key (257 EXTENT_DATA 266240) itemoff 15704 itemsize 53
65 # extent data disk byte 12845056 nr 4096 gen 6
66 # extent data offset 0 nr 20480 ram 20480
67 # extent compression 2
68 # item 9 key (257 EXTENT_DATA 286720) itemoff 15651 itemsize 53
69 # prealloc data disk byte 13090816 nr 405504 gen 6
70 # prealloc data offset 0 nr 258048
72 # The on disk extent at 266240, contains 5 compressed chunks of file data.
73 # Each of the first 4 chunks compress 4096 bytes of file data, while the last
74 # one compresses only 3024 bytes of file data. Because this extent item is not
75 # the last one in the file, as it followed by a prealloc extent, reads into
76 # the region [285648 ; 286720[ (length = 4096 - 3024) should return zeroes.
79 _check_btrfs_filesystem $SCRATCH_DEV
81 EXPECTED_MD5="b8b0dbb8e02f94123c741c23659a1c0a"
85 _scratch_mount "-o ro"
86 MD5=`md5sum $SCRATCH_MNT/foobar | cut -f 1 -d ' '`
88 if [ "${MD5}x" != "${EXPECTED_MD5}x" ]
90 echo "Unexpected file digest (wanted $EXPECTED_MD5, got $MD5)"