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 seqres=$RESULT_DIR/$seq
14 echo "QA output created by $seq"
17 status=1 # failure is the default!
18 trap "_cleanup; exit \$status" 0 1 2 3 15
23 rm -fr $send_files_dir
27 # get standard environment, filters and checks
32 # real QA test starts here
35 _require_scratch_reflink
37 send_files_dir=$TEST_DIR/btrfs-test-$seq
40 rm -fr $send_files_dir
43 _scratch_mkfs >>$seqres.full 2>&1
46 # Create our test file with a single and large extent (1M) and with different
47 # content for different file ranges that will be reflinked later.
49 -c "pwrite -S 0xab 0 128K" \
50 -c "pwrite -S 0xcd 128K 128K" \
51 -c "pwrite -S 0xef 256K 256K" \
52 -c "pwrite -S 0x1a 512K 512K" \
53 $SCRATCH_MNT/foobar | _filter_xfs_io
55 # Now create the base snapshot, which is going to be the parent snapshot for
56 # a later incremental send.
57 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT \
58 $SCRATCH_MNT/mysnap1 > /dev/null
60 $BTRFS_UTIL_PROG send -f $send_files_dir/1.snap \
61 $SCRATCH_MNT/mysnap1 2>&1 1>/dev/null | _filter_scratch
63 # Now do a series of changes to our file such that we end up with different
64 # parts of the extent reflinked into different file offsets and we overwrite
65 # a large part of the extent too, so no file extent items refer to that part
66 # that was overwritten. This used to confuse the algorithm used by the kernel
67 # to figure out which file ranges to clone, making it attempt to clone from
68 # a source range starting at the current eof of the file, resulting in the
69 # receiver to fail since it is an invalid clone operation.
71 $XFS_IO_PROG -c "reflink $SCRATCH_MNT/foobar 64K 1M 960K" \
72 -c "reflink $SCRATCH_MNT/foobar 0K 512K 256K" \
73 -c "reflink $SCRATCH_MNT/foobar 512K 128K 256K" \
74 -c "pwrite -S 0x73 384K 640K" \
75 $SCRATCH_MNT/foobar | _filter_xfs_io
77 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT \
78 $SCRATCH_MNT/mysnap2 > /dev/null
79 $BTRFS_UTIL_PROG send -p $SCRATCH_MNT/mysnap1 -f $send_files_dir/2.snap \
80 $SCRATCH_MNT/mysnap2 2>&1 1>/dev/null | _filter_scratch
82 echo "File digest in the original filesystem:"
83 _md5_checksum $SCRATCH_MNT/mysnap2/foobar
85 # Now recreate the filesystem by receiving both send streams and verify we get
86 # the same content that the original filesystem had.
88 _scratch_mkfs >>$seqres.full 2>&1
91 $BTRFS_UTIL_PROG receive -f $send_files_dir/1.snap $SCRATCH_MNT > /dev/null
93 # The receive operation below used to fail with the following error:
95 # ERROR: failed to clone extents to foobar: Invalid argument
97 # This is because the send stream included a clone operation to clone from the
98 # current file eof into eof (we can't clone from eof and neither the source
99 # range can overlap with the destination range), resulting in the receiver to
100 # fail with -EINVAL when attempting the clone operation.
102 $BTRFS_UTIL_PROG receive -f $send_files_dir/2.snap $SCRATCH_MNT > /dev/null
104 # Must match what we had in the original filesystem.
105 echo "File digest in the new filesystem:"
106 _md5_checksum $SCRATCH_MNT/mysnap2/foobar