a0177f671ea3807874c0e7104c2baf9132d0b8a8
[xfstests-dev.git] / tests / btrfs / 112
1 #! /bin/bash
2 # FSQA Test No. 112
3 #
4 # Test several cases of cloning inline extents that used to lead to file
5 # corruption or data loss.
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         cd /
37         rm -f $tmp.*
38 }
39
40 # get standard environment, filters and checks
41 . ./common/rc
42 . ./common/filter
43
44 # real QA test starts here
45 _supported_fs btrfs
46 _supported_os Linux
47 _require_scratch
48 _require_cloner
49 _require_btrfs_fs_feature "no_holes"
50 _require_btrfs_mkfs_feature "no-holes"
51 _require_xfs_io_command "falloc"
52
53 rm -f $seqres.full
54
55 test_cloning_inline_extents()
56 {
57         local mkfs_opts=$1
58         local mount_opts=$2
59
60         _scratch_mkfs $mkfs_opts >>$seqres.full 2>&1
61         _scratch_mount $mount_opts
62
63         # File bar, the source for all the following clone operations, consists
64         # of a single inline extent (50 bytes).
65         $XFS_IO_PROG -f -c "pwrite -S 0xbb 0 50" $SCRATCH_MNT/bar \
66                 | _filter_xfs_io
67
68         # Test cloning into a file with an extent (non-inlined) where the
69         # destination offset overlaps that extent. It should not be possible to
70         # clone the inline extent from file bar into this file.
71         $XFS_IO_PROG -f -c "pwrite -S 0xaa 0K 16K" $SCRATCH_MNT/foo \
72                 | _filter_xfs_io
73         $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo
74
75         # Doing IO against any range in the first 4K of the file should work.
76         # Due to a past clone ioctl bug which allowed cloning the inline extent,
77         # these operations resulted in EIO errors.
78         echo "File foo data after clone operation:"
79         # All bytes should have the value 0xaa (clone operation failed and did
80         # not modify our file).
81         od -t x1 $SCRATCH_MNT/foo
82         $XFS_IO_PROG -c "pwrite -S 0xcc 0 100" $SCRATCH_MNT/foo | _filter_xfs_io
83
84         # Test cloning the inline extent against a file which has a hole in its
85         # first 4K followed by a non-inlined extent. It should not be possible
86         # as well to clone the inline extent from file bar into this file.
87         $XFS_IO_PROG -f -c "pwrite -S 0xdd 4K 12K" $SCRATCH_MNT/foo2 \
88                 | _filter_xfs_io
89         $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo2
90
91         # Doing IO against any range in the first 4K of the file should work.
92         # Due to a past clone ioctl bug which allowed cloning the inline extent,
93         # these operations resulted in EIO errors.
94         echo "File foo2 data after clone operation:"
95         # All bytes should have the value 0x00 (clone operation failed and did
96         # not modify our file).
97         od -t x1 $SCRATCH_MNT/foo2
98         $XFS_IO_PROG -c "pwrite -S 0xee 0 90" $SCRATCH_MNT/foo2 | _filter_xfs_io
99
100         # Test cloning the inline extent against a file which has a size of zero
101         # but has a prealloc extent. It should not be possible as well to clone
102         # the inline extent from file bar into this file.
103         $XFS_IO_PROG -f -c "falloc -k 0 1M" $SCRATCH_MNT/foo3 | _filter_xfs_io
104         $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo3
105
106         # Doing IO against any range in the first 4K of the file should work.
107         # Due to a past clone ioctl bug which allowed cloning the inline extent,
108         # these operations resulted in EIO errors.
109         echo "First 50 bytes of foo3 after clone operation:"
110         # Should not be able to read any bytes, file has 0 bytes i_size (the
111         # clone operation failed and did not modify our file).
112         od -t x1 $SCRATCH_MNT/foo3
113         $XFS_IO_PROG -c "pwrite -S 0xff 0 90" $SCRATCH_MNT/foo3 | _filter_xfs_io
114
115         # Test cloning the inline extent against a file which consists of a
116         # single inline extent that has a size not greater than the size of
117         # bar's inline extent (40 < 50).
118         # It should be possible to do the extent cloning from bar to this file.
119         $XFS_IO_PROG -f -c "pwrite -S 0x01 0 40" $SCRATCH_MNT/foo4 \
120                 | _filter_xfs_io
121         $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo4
122
123         # Doing IO against any range in the first 4K of the file should work.
124         echo "File foo4 data after clone operation:"
125         # Must match file bar's content.
126         od -t x1 $SCRATCH_MNT/foo4
127         $XFS_IO_PROG -c "pwrite -S 0x02 0 90" $SCRATCH_MNT/foo4 | _filter_xfs_io
128
129         # Test cloning the inline extent against a file which consists of a
130         # single inline extent that has a size greater than the size of bar's
131         # inline extent (60 > 50).
132         # It should not be possible to clone the inline extent from file bar
133         # into this file.
134         $XFS_IO_PROG -f -c "pwrite -S 0x03 0 60" $SCRATCH_MNT/foo5 \
135                 | _filter_xfs_io
136         $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo5
137
138         # Reading the file should not fail.
139         echo "File foo5 data after clone operation:"
140         # Must have a size of 60 bytes, with all bytes having a value of 0x03
141         # (the clone operation failed and did not modify our file).
142         od -t x1 $SCRATCH_MNT/foo5
143
144         # Test cloning the inline extent against a file which has no extents but
145         # has a size greater than bar's inline extent (16K > 50).
146         # It should not be possible to clone the inline extent from file bar
147         # into this file.
148         $XFS_IO_PROG -f -c "truncate 16K" $SCRATCH_MNT/foo6 | _filter_xfs_io
149         $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo6
150
151         # Reading the file should not fail.
152         echo "File foo6 data after clone operation:"
153         # Must have a size of 16K, with all bytes having a value of 0x00 (the
154         # clone operation failed and did not modify our file).
155         od -t x1 $SCRATCH_MNT/foo6
156
157         # Test cloning the inline extent against a file which has no extents but
158         # has a size not greater than bar's inline extent (30 < 50).
159         # It should be possible to clone the inline extent from file bar into
160         # this file.
161         $XFS_IO_PROG -f -c "truncate 30" $SCRATCH_MNT/foo7 | _filter_xfs_io
162         $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo7
163
164         # Reading the file should not fail.
165         echo "File foo7 data after clone operation:"
166         # Must have a size of 50 bytes, with all bytes having a value of 0xbb.
167         od -t x1 $SCRATCH_MNT/foo7
168
169         # Test cloning the inline extent against a file which has a size not
170         # greater than the size of bar's inline extent (20 < 50) but has
171         # a prealloc extent that goes beyond the file's size. It should not be
172         # possible to clone the inline extent from bar into this file.
173         $XFS_IO_PROG -f -c "falloc -k 0 1M" \
174                         -c "pwrite -S 0x88 0 20" \
175                         $SCRATCH_MNT/foo8 | _filter_xfs_io
176         $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo8
177
178         echo "File foo8 data after clone operation:"
179         # Must have a size of 20 bytes, with all bytes having a value of 0x88
180         # (the clone operation did not modify our file).
181         od -t x1 $SCRATCH_MNT/foo8
182
183         _scratch_unmount
184 }
185
186 echo -e "\nTesting without compression and without the no-holes feature...\n"
187 test_cloning_inline_extents
188
189 echo -e "\nTesting with compression and without the no-holes feature...\n"
190 test_cloning_inline_extents "" "-o compress"
191
192 echo -e "\nTesting without compression and with the no-holes feature...\n"
193 test_cloning_inline_extents "-O no-holes" ""
194
195 echo -e "\nTesting with compression and with the no-holes feature...\n"
196 test_cloning_inline_extents "-O no-holes" "-o compress"
197
198 status=0
199 exit