generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / tests / btrfs / 229
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2021 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FS QA Test No. btrfs/229
6 #
7 # Test that an incremental send operation correctly issues clone operations for
8 # a file that had different parts of one of its extents cloned into itself, at
9 # different offsets, and a large part of that extent was overwritten, so all the
10 # reflinks only point to subranges of the extent.
11 #
12 . ./common/preamble
13 _begin_fstest auto quick send clone
14
15 # Override the default cleanup function.
16 _cleanup()
17 {
18         cd /
19         rm -fr $send_files_dir
20         rm -f $tmp.*
21 }
22
23 # Import common functions.
24 . ./common/filter
25 . ./common/reflink
26
27 # real QA test starts here
28 _supported_fs btrfs
29 _require_test
30 _require_scratch_reflink
31
32 send_files_dir=$TEST_DIR/btrfs-test-$seq
33
34 rm -fr $send_files_dir
35 mkdir $send_files_dir
36
37 _scratch_mkfs >>$seqres.full 2>&1
38 _scratch_mount
39
40 # Create our test file with a single and large extent (1M) and with different
41 # content for different file ranges that will be reflinked later.
42 $XFS_IO_PROG -f \
43              -c "pwrite -S 0xab 0 128K" \
44              -c "pwrite -S 0xcd 128K 128K" \
45              -c "pwrite -S 0xef 256K 256K" \
46              -c "pwrite -S 0x1a 512K 512K" \
47              $SCRATCH_MNT/foobar | _filter_xfs_io
48
49 # Now create the base snapshot, which is going to be the parent snapshot for
50 # a later incremental send.
51 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT \
52         $SCRATCH_MNT/mysnap1 > /dev/null
53
54 $BTRFS_UTIL_PROG send -f $send_files_dir/1.snap \
55         $SCRATCH_MNT/mysnap1 2>&1 1>/dev/null | _filter_scratch
56
57 # Now do a series of changes to our file such that we end up with different
58 # parts of the extent reflinked into different file offsets and we overwrite
59 # a large part of the extent too, so no file extent items refer to that part
60 # that was overwritten. This used to confuse the algorithm used by the kernel
61 # to figure out which file ranges to clone, making it attempt to clone from
62 # a source range starting at the current eof of the file, resulting in the
63 # receiver to fail since it is an invalid clone operation.
64 #
65 $XFS_IO_PROG -c "reflink $SCRATCH_MNT/foobar 64K 1M 960K" \
66              -c "reflink $SCRATCH_MNT/foobar 0K 512K 256K" \
67              -c "reflink $SCRATCH_MNT/foobar 512K 128K 256K" \
68              -c "pwrite -S 0x73 384K 640K" \
69              $SCRATCH_MNT/foobar | _filter_xfs_io
70
71 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT \
72                  $SCRATCH_MNT/mysnap2 > /dev/null
73 $BTRFS_UTIL_PROG send -p $SCRATCH_MNT/mysnap1 -f $send_files_dir/2.snap \
74                  $SCRATCH_MNT/mysnap2 2>&1 1>/dev/null | _filter_scratch
75
76 echo "File digest in the original filesystem:"
77 _md5_checksum $SCRATCH_MNT/mysnap2/foobar
78
79 # Now recreate the filesystem by receiving both send streams and verify we get
80 # the same content that the original filesystem had.
81 _scratch_unmount
82 _scratch_mkfs >>$seqres.full 2>&1
83 _scratch_mount
84
85 $BTRFS_UTIL_PROG receive -f $send_files_dir/1.snap $SCRATCH_MNT > /dev/null
86
87 # The receive operation below used to fail with the following error:
88 #
89 #    ERROR: failed to clone extents to foobar: Invalid argument
90 #
91 # This is because the send stream included a clone operation to clone from the
92 # current file eof into eof (we can't clone from eof and neither the source
93 # range can overlap with the destination range), resulting in the receiver to
94 # fail with -EINVAL when attempting the clone operation.
95 #
96 $BTRFS_UTIL_PROG receive -f $send_files_dir/2.snap $SCRATCH_MNT > /dev/null
97
98 # Must match what we had in the original filesystem.
99 echo "File digest in the new filesystem:"
100 _md5_checksum $SCRATCH_MNT/mysnap2/foobar
101
102 status=0
103 exit