2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2021 SUSE Linux Products GmbH. All Rights Reserved.
5 # FS QA Test No. btrfs/229
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.
13 _begin_fstest auto quick send clone
15 # Override the default cleanup function.
19 rm -fr $send_files_dir
23 # Import common functions.
27 # real QA test starts here
30 _require_scratch_reflink
32 send_files_dir=$TEST_DIR/btrfs-test-$seq
34 rm -fr $send_files_dir
37 _scratch_mkfs >>$seqres.full 2>&1
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.
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
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
54 $BTRFS_UTIL_PROG send -f $send_files_dir/1.snap \
55 $SCRATCH_MNT/mysnap1 2>&1 1>/dev/null | _filter_scratch
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.
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
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
76 echo "File digest in the original filesystem:"
77 _md5_checksum $SCRATCH_MNT/mysnap2/foobar
79 # Now recreate the filesystem by receiving both send streams and verify we get
80 # the same content that the original filesystem had.
82 _scratch_mkfs >>$seqres.full 2>&1
85 $BTRFS_UTIL_PROG receive -f $send_files_dir/1.snap $SCRATCH_MNT > /dev/null
87 # The receive operation below used to fail with the following error:
89 # ERROR: failed to clone extents to foobar: Invalid argument
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.
96 $BTRFS_UTIL_PROG receive -f $send_files_dir/2.snap $SCRATCH_MNT > /dev/null
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