caa979a497cb0bf2a1d5433c261cfe320515d2ee
[xfstests-dev.git] / tests / btrfs / 103
1 #! /bin/bash
2 # FSQA Test No. 103
3 #
4 # Regression test for file read corruption when using compressed extents that
5 # are shared by multiple consecutive ranges of the same file.
6 #
7 #-----------------------------------------------------------------------
8 #
9 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
10 # Author: Filipe Manana <fdmanana@suse.com>
11 #
12 # This program is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License as
14 # published by the Free Software Foundation.
15 #
16 # This program is distributed in the hope that it would be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write the Free Software Foundation,
23 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24 #-----------------------------------------------------------------------
25 #
26
27 seq=`basename $0`
28 seqres=$RESULT_DIR/$seq
29 echo "QA output created by $seq"
30 tmp=/tmp/$$
31 status=1        # failure is the default!
32 trap "_cleanup; exit \$status" 0 1 2 3 15
33
34 _cleanup()
35 {
36         rm -f $tmp.*
37 }
38
39 # get standard environment, filters and checks
40 . ./common/rc
41 . ./common/filter
42
43 # real QA test starts here
44 _supported_fs btrfs
45 _supported_os Linux
46 _require_scratch
47 _require_cloner
48
49 rm -f $seqres.full
50
51 test_clone_and_read_compressed_extent()
52 {
53         local mount_opts=$1
54
55         _scratch_mkfs >>$seqres.full 2>&1
56         _scratch_mount $mount_opts
57
58         BLOCK_SIZE=$(get_block_size $SCRATCH_MNT)
59
60         # Create a test file with a single extent that is compressed (the
61         # data we write into it is highly compressible no matter which
62         # compression algorithm is used, zlib or lzo).
63         $XFS_IO_PROG -f -c "pwrite -S 0xaa 0K $((1 * $BLOCK_SIZE))" \
64                 -c "pwrite -S 0xbb $((1 * $BLOCK_SIZE)) $((2 * $BLOCK_SIZE))" \
65                 -c "pwrite -S 0xcc $((3 * $BLOCK_SIZE)) $((1 * $BLOCK_SIZE))" \
66                 $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
67
68
69         # Now clone our extent into an adjacent offset.
70         $CLONER_PROG -s $((1 * $BLOCK_SIZE)) -d $((4 * $BLOCK_SIZE)) \
71                      -l $((2 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/foo
72
73         # Same as before but for this file we clone the extent into a lower
74         # file offset.
75         $XFS_IO_PROG -f \
76                 -c "pwrite -S 0xaa $((2 * $BLOCK_SIZE)) $((1 * $BLOCK_SIZE))" \
77                 -c "pwrite -S 0xbb $((3 * $BLOCK_SIZE)) $((2 * $BLOCK_SIZE))" \
78                 -c "pwrite -S 0xcc $((5 * $BLOCK_SIZE)) $((1 * $BLOCK_SIZE))" \
79                 $SCRATCH_MNT/bar | _filter_xfs_io_blocks_modified
80
81         $CLONER_PROG -s $((3 * $BLOCK_SIZE)) -d 0 -l $((2 * $BLOCK_SIZE)) \
82                 $SCRATCH_MNT/bar $SCRATCH_MNT/bar
83
84         echo "File contents before unmounting filesystem:"
85         echo "foo:"
86         od -t x1 $SCRATCH_MNT/foo | _filter_od
87         echo "bar:"
88         od -t x1 $SCRATCH_MNT/bar | _filter_od
89
90         # Evicting the inode or clearing the page cache before reading again
91         # the file would also trigger the bug - reads were returning all bytes
92         # in the range corresponding to the second reference to the extent with
93         # a value of 0, but the correct data was persisted (it was a bug
94         # exclusively in the read path). The issue happened only if the same
95         # readpages() call targeted pages belonging to the first and second
96         # ranges that point to the same compressed extent.
97         _scratch_cycle_mount
98
99         echo "File contents after mounting filesystem again:"
100         # Must match the same contents we got before.
101         echo "foo:"
102         od -t x1 $SCRATCH_MNT/foo | _filter_od
103         echo "bar:"
104         od -t x1 $SCRATCH_MNT/bar | _filter_od
105 }
106
107 echo -e "\nTesting with zlib compression..."
108 test_clone_and_read_compressed_extent "-o compress=zlib"
109
110 _scratch_unmount
111
112 echo -e "\nTesting with lzo compression..."
113 test_clone_and_read_compressed_extent "-o compress=lzo"
114
115 status=0
116 exit