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