2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
7 # Test several cases of cloning inline extents that used to lead to file
8 # corruption or data loss.
11 seqres=$RESULT_DIR/$seq
12 echo "QA output created by $seq"
14 status=1 # failure is the default!
15 trap "_cleanup; exit \$status" 0 1 2 3 15
23 # get standard environment, filters and checks
26 . ./common/filter.btrfs
28 # real QA test starts here
33 _require_btrfs_fs_feature "no_holes"
34 _require_btrfs_mkfs_feature "no-holes"
35 _require_xfs_io_command "falloc"
39 test_cloning_inline_extents()
44 _scratch_mkfs $mkfs_opts >>$seqres.full 2>&1
45 _scratch_mount $mount_opts
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 \
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 \
57 $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo \
58 | _filter_btrfs_cloner_error
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
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 \
74 $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo2 \
75 | _filter_btrfs_cloner_error
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
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
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
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 \
108 $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo4 \
109 | _filter_btrfs_cloner_error
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
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
122 $XFS_IO_PROG -f -c "pwrite -S 0x03 0 60" $SCRATCH_MNT/foo5 \
124 $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo5 \
125 | _filter_btrfs_cloner_error
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
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
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
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
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
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
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
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
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
178 echo -e "\nTesting without compression and without the no-holes feature...\n"
179 test_cloning_inline_extents
181 echo -e "\nTesting with compression and without the no-holes feature...\n"
182 test_cloning_inline_extents "" "-o compress"
184 echo -e "\nTesting without compression and with the no-holes feature...\n"
185 test_cloning_inline_extents "-O no-holes" ""
187 echo -e "\nTesting with compression and with the no-holes feature...\n"
188 test_cloning_inline_extents "-O no-holes" "-o compress"