xfs: Check for extent overflow when trivally adding a new extent
[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 _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                 | _filter_btrfs_cloner_error
58
59         # Doing IO against any range in the first 4K of the file should work.
60         # Due to a past clone ioctl bug which allowed cloning the inline extent,
61         # these operations resulted in EIO errors.
62         echo "File foo data after clone operation:"
63         # All bytes should have the value 0xaa (clone operation failed and did
64         # not modify our file).
65         od -t x1 $SCRATCH_MNT/foo
66         $XFS_IO_PROG -c "pwrite -S 0xcc 0 100" $SCRATCH_MNT/foo | _filter_xfs_io
67
68         # Test cloning the inline extent against a file which has a hole in its
69         # first 4K followed by a non-inlined extent. It should not be possible
70         # as well to clone the inline extent from file bar into this file.
71         $XFS_IO_PROG -f -c "pwrite -S 0xdd 4K 12K" $SCRATCH_MNT/foo2 \
72                 | _filter_xfs_io
73         $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo2 \
74                 | _filter_btrfs_cloner_error
75
76         # Doing IO against any range in the first 4K of the file should work.
77         # Due to a past clone ioctl bug which allowed cloning the inline extent,
78         # these operations resulted in EIO errors.
79         echo "File foo2 data after clone operation:"
80         # All bytes should have the value 0x00 (clone operation failed and did
81         # not modify our file).
82         od -t x1 $SCRATCH_MNT/foo2
83         $XFS_IO_PROG -c "pwrite -S 0xee 0 90" $SCRATCH_MNT/foo2 | _filter_xfs_io
84
85         # Test cloning the inline extent against a file which consists of a
86         # single inline extent that has a size not greater than the size of
87         # bar's inline extent (40 < 50).
88         # It should be possible to do the extent cloning from bar to this file.
89         $XFS_IO_PROG -f -c "pwrite -S 0x01 0 40" $SCRATCH_MNT/foo4 \
90                 | _filter_xfs_io
91         $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo4 \
92                 | _filter_btrfs_cloner_error
93
94         # Doing IO against any range in the first 4K of the file should work.
95         echo "File foo4 data after clone operation:"
96         # Must match file bar's content.
97         od -t x1 $SCRATCH_MNT/foo4
98         $XFS_IO_PROG -c "pwrite -S 0x02 0 90" $SCRATCH_MNT/foo4 | _filter_xfs_io
99
100         # Test cloning the inline extent against a file which consists of a
101         # single inline extent that has a size greater than the size of bar's
102         # inline extent (60 > 50).
103         # It should not be possible to clone the inline extent from file bar
104         # into this file.
105         $XFS_IO_PROG -f -c "pwrite -S 0x03 0 60" $SCRATCH_MNT/foo5 \
106                 | _filter_xfs_io
107         $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo5 \
108                 | _filter_btrfs_cloner_error
109
110         # Reading the file should not fail.
111         echo "File foo5 data after clone operation:"
112         # Must have a size of 60 bytes, with all bytes having a value of 0x03
113         # (the clone operation failed and did not modify our file).
114         od -t x1 $SCRATCH_MNT/foo5
115
116         # Test cloning the inline extent against a file which has no extents but
117         # has a size greater than bar's inline extent (16K > 50).
118         # It should not be possible to clone the inline extent from file bar
119         # into this file.
120         $XFS_IO_PROG -f -c "truncate 16K" $SCRATCH_MNT/foo6 | _filter_xfs_io
121         $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo6 \
122                 | _filter_btrfs_cloner_error
123
124         # Reading the file should not fail.
125         echo "File foo6 data after clone operation:"
126         # Must have a size of 16K, with all bytes having a value of 0x00 (the
127         # clone operation failed and did not modify our file).
128         od -t x1 $SCRATCH_MNT/foo6
129
130         # Test cloning the inline extent against a file which has no extents but
131         # has a size not greater than bar's inline extent (30 < 50).
132         # It should be possible to clone the inline extent from file bar into
133         # this file.
134         $XFS_IO_PROG -f -c "truncate 30" $SCRATCH_MNT/foo7 | _filter_xfs_io
135         $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo7 \
136                 | _filter_btrfs_cloner_error
137
138         # Reading the file should not fail.
139         echo "File foo7 data after clone operation:"
140         # Must have a size of 50 bytes, with all bytes having a value of 0xbb.
141         od -t x1 $SCRATCH_MNT/foo7
142
143         _scratch_unmount
144 }
145
146 echo -e "\nTesting without compression and without the no-holes feature...\n"
147 test_cloning_inline_extents
148
149 echo -e "\nTesting with compression and without the no-holes feature...\n"
150 test_cloning_inline_extents "" "-o compress"
151
152 echo -e "\nTesting without compression and with the no-holes feature...\n"
153 test_cloning_inline_extents "-O no-holes" ""
154
155 echo -e "\nTesting with compression and with the no-holes feature...\n"
156 test_cloning_inline_extents "-O no-holes" "-o compress"
157
158 status=0
159 exit