xfstests: remove _need_to_be_root
[xfstests-dev.git] / tests / btrfs / 113
1 #! /bin/bash
2 # FSQA Test No. 113
3 #
4 # Test that truncating a file that consists of a compressed and inlined extent
5 # to a smaller size and then cloning it into another file is not possible and
6 # does not result in leaking stale data (data past the truncation offset) nor
7 # losing data in the clone operation's destination file.
8 #
9 #-----------------------------------------------------------------------
10 #
11 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
12 # Author: Filipe Manana <fdmanana@suse.com>
13 #
14 # This program is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License as
16 # published by the Free Software Foundation.
17 #
18 # This program is distributed in the hope that it would be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write the Free Software Foundation,
25 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26 #-----------------------------------------------------------------------
27 #
28
29 seq=`basename $0`
30 seqres=$RESULT_DIR/$seq
31 echo "QA output created by $seq"
32 tmp=/tmp/$$
33 status=1        # failure is the default!
34 trap "_cleanup; exit \$status" 0 1 2 3 15
35
36 _cleanup()
37 {
38         cd /
39         rm -f $tmp.*
40 }
41
42 # get standard environment, filters and checks
43 . ./common/rc
44 . ./common/filter
45
46 # real QA test starts here
47 _supported_fs btrfs
48 _supported_os Linux
49 _require_scratch
50 _require_cloner
51
52 rm -f $seqres.full
53
54 _scratch_mkfs >>$seqres.full 2>&1
55 _scratch_mount "-o compress"
56
57 # Create our test files. File foo is going to be the source of a clone operation
58 # and consists of a single inline extent with an uncompressed size of 512 bytes,
59 # while file bar consists of a single inline extent with an uncompressed size of
60 # 256 bytes. For our test's purpose, it's important that file bar has an inline
61 # extent with a size smaller than foo's inline extent.
62 $XFS_IO_PROG -f -c "pwrite -S 0xa1 0 128"   \
63                 -c "pwrite -S 0x2a 128 384" \
64                 $SCRATCH_MNT/foo | _filter_xfs_io
65 $XFS_IO_PROG -f -c "pwrite -S 0xbb 0 256" $SCRATCH_MNT/bar | _filter_xfs_io
66
67 # Now durably persist all metadata and data. We do this to make sure that we get
68 # on disk an inline extent with a size of 512 bytes for file foo.
69 sync
70
71 # Now truncate our file foo to a smaller size. Because it consists of a
72 # compressed and inline extent, btrfs did not shrink the inline extent to the
73 # new size (if the extent was not compressed, btrfs would shrink it to 128
74 # bytes), it only updates the inode's i_size to 128 bytes.
75 $XFS_IO_PROG -c "truncate 128" $SCRATCH_MNT/foo
76
77 # Now clone foo's inline extent into bar.
78 # This clone operation should fail with errno EOPNOTSUPP because the source
79 # file consists only of an inline extent and the file's size is smaller than
80 # the inline extent of the destination (128 bytes < 256 bytes). However the
81 # clone ioctl was not prepared to deal with a file that has a size smaller
82 # than the size of its inline extent (something that happens only for compressed
83 # inline extents), resulting in copying the full inline extent from the source
84 # file into the destination file.
85 #
86 # Note that btrfs' clone operation for inline extents consists of removing the
87 # inline extent from the destination inode and copy the inline extent from the
88 # source inode into the destination inode, meaning that if the destination
89 # inode's inline extent is larger (N bytes) than the source inode's inline
90 # extent (M bytes), some bytes (N - M bytes) will be lost from the destination
91 # file. Btrfs could copy the source inline extent's data into the destination's
92 # inline extent so that we would not lose any data, but that's currently not
93 # done due to the complexity that would be needed to deal with such cases
94 # (specially when one or both extents are compressed), returning EOPNOTSUPP, as
95 # it's normally not a very common case to clone very small files (only case
96 # where we get inline extents) and copying inline extents does not save any
97 # space (unlike for normal, non-inlined extents).
98 $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/foo $SCRATCH_MNT/bar
99
100 # Now because the above clone operation used to succeed, and due to foo's inline
101 # extent not being shinked by the truncate operation, our file bar got the whole
102 # inline extent copied from foo, making us lose the last 128 bytes from bar
103 # which got replaced by the bytes in range [128, 256[ from foo before foo was
104 # truncated - in other words, data loss from bar and being able to read old and
105 # stale data from foo that should not be possible to read anymore through normal
106 # filesystem operations. Contrast with the case where we truncate a file from a
107 # size N to a smaller size M, truncate it back to size N and then read the range
108 # [M, N[, we should always get the value 0x00 for all the bytes in that range.
109
110 # We expected the clone operation to fail with errno EOPNOTSUPP and therefore
111 # not modify our file's bar data/metadata. So its content should be 256 bytes
112 # long with all bytes having the value 0xbb.
113 #
114 # Without the btrfs bug fix, the clone operation succeeded and resulted in
115 # leaking truncated data from foo, the bytes that belonged to its range
116 # [128, 256[, and losing data from bar in that same range. So reading the
117 # file gave us the following content:
118 #
119 # 0000000 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 a1
120 # *
121 # 0000200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a
122 # *
123 # 0000400
124 echo "File bar's content after the clone operation:"
125 od -t x1 $SCRATCH_MNT/bar
126
127 # Also because the foo's inline extent was not shrunk by the truncate
128 # operation, btrfs' fsck, which is run by the fstests framework everytime a
129 # test completes, failed reporting the following error:
130 #
131 #  root 5 inode 257 errors 400, nbytes wrong
132
133 status=0
134 exit