2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2019 SUSE Linux Products GmbH. All Rights Reserved.
7 # Check that send operations (full and incremental) are able to issue clone
8 # operations for extents that are shared between the same file.
11 _begin_fstest auto quick send clone
13 # Override the default cleanup function.
18 rm -fr $send_files_dir
21 # Import common functions.
26 # real QA test starts here
30 _require_scratch_reflink
31 _require_xfs_io_command "fiemap"
33 send_files_dir=$TEST_DIR/btrfs-test-$seq
35 rm -fr $send_files_dir
38 _scratch_mkfs >>$seqres.full 2>&1
41 # Create our first test file, which has an extent that is shared only with
42 # itself and no other files. We want to verify a full send operation will
44 $XFS_IO_PROG -f -c "pwrite -S 0xb1 -b 64K 0 64K" $SCRATCH_MNT/foo \
46 $XFS_IO_PROG -c "reflink $SCRATCH_MNT/foo 0 64K 64K" $SCRATCH_MNT/foo \
49 # Create out second test file which initially, for the first send operation,
50 # only has a single extent that is not shared.
51 $XFS_IO_PROG -f -c "pwrite -S 0xc7 -b 64K 0 64K" $SCRATCH_MNT/bar \
54 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/base 2>&1 \
57 $BTRFS_UTIL_PROG send -f $send_files_dir/1.snap $SCRATCH_MNT/base 2>&1 \
60 # Now clone the existing extent in file bar to itself at a different offset.
61 # We want to verify the incremental send operation below will issue a clone
62 # operation instead of a write operation.
63 $XFS_IO_PROG -c "reflink $SCRATCH_MNT/bar 0 64K 64K" $SCRATCH_MNT/bar \
66 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/incr 2>&1 \
69 $BTRFS_UTIL_PROG send -p $SCRATCH_MNT/base -f $send_files_dir/2.snap \
70 $SCRATCH_MNT/incr 2>&1 | _filter_scratch
72 # Compute digests of the snapshot trees so that later we can compare against
73 # digests of the trees in the new filesystem, to see if they match (no data or
74 # metadata corruption happened).
75 $FSSUM_PROG -A -f -w $send_files_dir/base.fssum $SCRATCH_MNT/base
76 $FSSUM_PROG -A -f -w $send_files_dir/incr.fssum \
77 -x $SCRATCH_MNT/incr/base $SCRATCH_MNT/incr
79 # Now recreate the filesystem by receiving both send streams and verify we get
80 # the same file contents that the original filesystem had and that files foo
81 # and bar have shared extents.
83 _scratch_mkfs >>$seqres.full 2>&1
86 $BTRFS_UTIL_PROG receive -f $send_files_dir/1.snap $SCRATCH_MNT
87 $BTRFS_UTIL_PROG receive -f $send_files_dir/2.snap $SCRATCH_MNT
89 # Compute digests of the snapshot trees in the new filesystem and compare them
90 # to the ones in the original filesystem, they must match.
91 $FSSUM_PROG -r $send_files_dir/base.fssum $SCRATCH_MNT/base
92 $FSSUM_PROG -r $send_files_dir/incr.fssum $SCRATCH_MNT/incr
94 num_extents=$(_count_extents $SCRATCH_MNT/base/foo)
95 num_exclusive_extents=$(_count_exclusive_extents $SCRATCH_MNT/base/foo)
96 if [ $num_extents -ne 2 ] || [ $num_exclusive_extents -ne 1 ]; then
97 echo "File foo does not have 2 shared extents in the base snapshot"
98 $XFS_IO_PROG -r -c "fiemap" $SCRATCH_MNT/base/foo
101 num_extents=$(_count_extents $SCRATCH_MNT/incr/foo)
102 num_exclusive_extents=$(_count_exclusive_extents $SCRATCH_MNT/incr/foo)
103 if [ $num_extents -ne 2 ] || [ $num_exclusive_extents -ne 1 ]; then
104 echo "File foo does not have 2 shared extents in the incr snapshot"
105 $XFS_IO_PROG -r -c "fiemap" $SCRATCH_MNT/incr/foo
108 num_extents=$(_count_extents $SCRATCH_MNT/incr/bar)
109 num_exclusive_extents=$(_count_exclusive_extents $SCRATCH_MNT/incr/bar)
110 if [ $num_extents -ne 2 ] || [ $num_exclusive_extents -ne 1 ]; then
111 echo "File bar does not have 2 shared extents in the incr snapshot"
112 $XFS_IO_PROG -r -c "fiemap" $SCRATCH_MNT/incr/bar