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