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