18e5b99d89d62f59e11a456eaef27c44f033ab57
[xfstests-dev.git] / tests / btrfs / 108
1 #! /bin/bash
2 # FS QA Test No. btrfs/108
3 #
4 # Test that a send operation works correctly with reflinked files (cloned
5 # extents which multiple files point to).
6 #
7 #-----------------------------------------------------------------------
8 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
9 # Author: Filipe Manana <fdmanana@suse.com>
10 #
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License as
13 # published by the Free Software Foundation.
14 #
15 # This program is distributed in the hope that it would be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write the Free Software Foundation,
22 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23 #-----------------------------------------------------------------------
24 #
25
26 seq=`basename $0`
27 seqres=$RESULT_DIR/$seq
28 echo "QA output created by $seq"
29
30 tmp=/tmp/$$
31 status=1        # failure is the default!
32 trap "_cleanup; exit \$status" 0 1 2 3 15
33
34 _cleanup()
35 {
36         cd /
37         rm -fr $send_files_dir
38         rm -f $tmp.*
39 }
40
41 # get standard environment, filters and checks
42 . ./common/rc
43 . ./common/filter
44 . ./common/reflink
45
46 # real QA test starts here
47 _supported_fs btrfs
48 _supported_os Linux
49 _require_scratch
50 _need_to_be_root
51 _require_cp_reflink
52 _require_xfs_io_command "fpunch"
53
54 send_files_dir=$TEST_DIR/btrfs-test-$seq
55
56 rm -f $seqres.full
57 rm -fr $send_files_dir
58 mkdir $send_files_dir
59
60 _scratch_mkfs >>$seqres.full 2>&1
61 _scratch_mount
62
63 # Create our test file with a single 100K extent.
64 $XFS_IO_PROG -f -c "pwrite -S 0xaa 0K 100K" $SCRATCH_MNT/foo | _filter_xfs_io
65
66 # Clone our file into a new file named bar.
67 cp --reflink=always $SCRATCH_MNT/foo $SCRATCH_MNT/bar
68
69 # Now overwrite parts of our foo file.
70 $XFS_IO_PROG -c "pwrite -S 0xbb 50K 10K" \
71         -c "pwrite -S 0xcc 90K 10K" \
72         -c "fpunch 70K 10k" \
73         $SCRATCH_MNT/foo | _filter_xfs_io
74
75 _run_btrfs_util_prog subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/snap
76
77 echo "File digests in the original filesystem:"
78 md5sum $SCRATCH_MNT/snap/foo | _filter_scratch
79 md5sum $SCRATCH_MNT/snap/bar | _filter_scratch
80
81 _run_btrfs_util_prog send $SCRATCH_MNT/snap -f $send_files_dir/1.snap
82
83 # Now recreate the filesystem by receiving the send stream and verify we get
84 # the same file contents that the original filesystem had.
85 _scratch_unmount
86 _scratch_mkfs >>$seqres.full 2>&1
87 _scratch_mount
88
89 _run_btrfs_util_prog receive $SCRATCH_MNT -f $send_files_dir/1.snap
90
91 # We expect the destination filesystem to have exactly the same file data as
92 # the original filesystem.
93 # The btrfs send implementation had a bug where it sent a clone operation from
94 # file foo into file bar covering the whole [0, 100K[ range after creating
95 # and writing the file foo. This was incorrect because the file bar now included
96 # the updates done to file foo after we cloned foo to bar, breaking the COW
97 # nature of reflink copies (cloned extents).
98 echo "File digests in the new filesystem:"
99 md5sum $SCRATCH_MNT/snap/foo | _filter_scratch
100 md5sum $SCRATCH_MNT/snap/bar | _filter_scratch
101
102 status=0
103 exit