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