summaryrefslogtreecommitdiff
path: root/tests/btrfs/229
blob: 19026f2d0a41d7085259843517a44148d200836a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2021 SUSE Linux Products GmbH. All Rights Reserved.
#
# FS QA Test No. btrfs/229
#
# Test that an incremental send operation correctly issues clone operations for
# a file that had different parts of one of its extents cloned into itself, at
# different offsets, and a large part of that extent was overwritten, so all the
# reflinks only point to subranges of the extent.
#
seq=`basename $0`
seqres=$RESULT_DIR/$seq
echo "QA output created by $seq"

tmp=/tmp/$$
status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_cleanup()
{
	cd /
	rm -fr $send_files_dir
	rm -f $tmp.*
}

# get standard environment, filters and checks
. ./common/rc
. ./common/filter
. ./common/reflink

# real QA test starts here
_supported_fs btrfs
_require_test
_require_scratch_reflink

send_files_dir=$TEST_DIR/btrfs-test-$seq

rm -f $seqres.full
rm -fr $send_files_dir
mkdir $send_files_dir

_scratch_mkfs >>$seqres.full 2>&1
_scratch_mount

# Create our test file with a single and large extent (1M) and with different
# content for different file ranges that will be reflinked later.
$XFS_IO_PROG -f \
	     -c "pwrite -S 0xab 0 128K" \
	     -c "pwrite -S 0xcd 128K 128K" \
	     -c "pwrite -S 0xef 256K 256K" \
	     -c "pwrite -S 0x1a 512K 512K" \
	     $SCRATCH_MNT/foobar | _filter_xfs_io

# Now create the base snapshot, which is going to be the parent snapshot for
# a later incremental send.
$BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT \
	$SCRATCH_MNT/mysnap1 > /dev/null

$BTRFS_UTIL_PROG send -f $send_files_dir/1.snap \
	$SCRATCH_MNT/mysnap1 2>&1 1>/dev/null | _filter_scratch

# Now do a series of changes to our file such that we end up with different
# parts of the extent reflinked into different file offsets and we overwrite
# a large part of the extent too, so no file extent items refer to that part
# that was overwritten. This used to confuse the algorithm used by the kernel
# to figure out which file ranges to clone, making it attempt to clone from
# a source range starting at the current eof of the file, resulting in the
# receiver to fail since it is an invalid clone operation.
#
$XFS_IO_PROG -c "reflink $SCRATCH_MNT/foobar 64K 1M 960K" \
	     -c "reflink $SCRATCH_MNT/foobar 0K 512K 256K" \
	     -c "reflink $SCRATCH_MNT/foobar 512K 128K 256K" \
	     -c "pwrite -S 0x73 384K 640K" \
	     $SCRATCH_MNT/foobar | _filter_xfs_io

$BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT \
		 $SCRATCH_MNT/mysnap2 > /dev/null
$BTRFS_UTIL_PROG send -p $SCRATCH_MNT/mysnap1 -f $send_files_dir/2.snap \
		 $SCRATCH_MNT/mysnap2 2>&1 1>/dev/null | _filter_scratch

echo "File digest in the original filesystem:"
_md5_checksum $SCRATCH_MNT/mysnap2/foobar

# Now recreate the filesystem by receiving both send streams and verify we get
# the same content that the original filesystem had.
_scratch_unmount
_scratch_mkfs >>$seqres.full 2>&1
_scratch_mount

$BTRFS_UTIL_PROG receive -f $send_files_dir/1.snap $SCRATCH_MNT > /dev/null

# The receive operation below used to fail with the following error:
#
#    ERROR: failed to clone extents to foobar: Invalid argument
#
# This is because the send stream included a clone operation to clone from the
# current file eof into eof (we can't clone from eof and neither the source
# range can overlap with the destination range), resulting in the receiver to
# fail with -EINVAL when attempting the clone operation.
#
$BTRFS_UTIL_PROG receive -f $send_files_dir/2.snap $SCRATCH_MNT > /dev/null

# Must match what we had in the original filesystem.
echo "File digest in the new filesystem:"
_md5_checksum $SCRATCH_MNT/mysnap2/foobar

status=0
exit