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