btrfs/012: check free size of scratch device before copying files
[xfstests-dev.git] / tests / btrfs / 189
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2019 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FS QA Test No. 189
6 #
7 # Test that an incremental send receive does not issue clone operations that
8 # attempt to clone the last block of a file, with a size not aligned to the
9 # filesystem's sector size, into the middle of some other file. Such clone
10 # request causes the receiver to fail (with EINVAL), for kernels that include
11 # commit ac765f83f1397646 ("Btrfs: fix data corruption due to cloning of eof
12 # block"), or cause silent data corruption for older kernels.
13 #
14 seq=`basename $0`
15 seqres=$RESULT_DIR/$seq
16 echo "QA output created by $seq"
17 tmp=/tmp/$$
18 status=1        # failure is the default!
19 trap "_cleanup; exit \$status" 0 1 2 3 15
20
21 _cleanup()
22 {
23         cd /
24         rm -f $tmp.*
25         rm -fr $send_files_dir
26 }
27
28 # get standard environment, filters and checks
29 . ./common/rc
30 . ./common/filter
31 . ./common/reflink
32
33 # real QA test starts here
34 _supported_fs btrfs
35 _require_fssum
36 _require_test
37 _require_scratch_reflink
38
39 send_files_dir=$TEST_DIR/btrfs-test-$seq
40
41 rm -f $seqres.full
42 rm -fr $send_files_dir
43 mkdir $send_files_dir
44
45 _scratch_mkfs >>$seqres.full 2>&1
46 _scratch_mount
47
48 $XFS_IO_PROG -f -c "pwrite -S 0xb1 0 2M" $SCRATCH_MNT/foo | _filter_xfs_io
49 $XFS_IO_PROG -f -c "pwrite -S 0xc7 0 2M" $SCRATCH_MNT/bar | _filter_xfs_io
50 $XFS_IO_PROG -f -c "pwrite -S 0x4d 0 2M" $SCRATCH_MNT/baz | _filter_xfs_io
51 $XFS_IO_PROG -f -c "pwrite -S 0xe2 0 2M" $SCRATCH_MNT/zoo | _filter_xfs_io
52
53 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/base 2>&1 \
54         | _filter_scratch
55
56 $BTRFS_UTIL_PROG send -f $send_files_dir/1.snap $SCRATCH_MNT/base 2>&1 \
57         | _filter_scratch
58
59 # Clone part of the extent from a higher offset to a lower offset of the same
60 # file.
61 $XFS_IO_PROG -c "reflink $SCRATCH_MNT/bar 1600K 640K 128K" $SCRATCH_MNT/bar \
62         | _filter_xfs_io
63
64 # Now clone from the previous file, same range, into the middle of another file,
65 # such that the end offset at the destination is smaller than the destination's
66 # file size.
67 $XFS_IO_PROG -c "reflink $SCRATCH_MNT/bar 1600K 0 128K" $SCRATCH_MNT/zoo \
68         | _filter_xfs_io
69
70 # Truncate the source file of the previous clone operation to a smaller size,
71 # which ends up in the middle of the range of previous clone operation from file
72 # bar to file bar. We want to check this doesn't confuse send to issue invalid
73 # clone operations. This smaller size must not be aligned to the sector size of
74 # the filesystem - the unaligned size is what can cause those invalid clone
75 # operations.
76 $XFS_IO_PROG -c "truncate 710K" $SCRATCH_MNT/bar
77
78 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/incr 2>&1 \
79         | _filter_scratch
80
81 $BTRFS_UTIL_PROG send -p $SCRATCH_MNT/base -f $send_files_dir/2.snap \
82         $SCRATCH_MNT/incr 2>&1 | _filter_scratch
83
84 # Compute digests of the snapshot trees so that later we can compare against
85 # digests of the trees in the new filesystem, to see if they match (no data or
86 # metadata corruption happened).
87 $FSSUM_PROG -A -f -w $send_files_dir/base.fssum $SCRATCH_MNT/base
88 $FSSUM_PROG -A -f -w $send_files_dir/incr.fssum \
89         -x $SCRATCH_MNT/incr/base $SCRATCH_MNT/incr
90
91 # Now recreate the filesystem by receiving both send streams and verify we get
92 # the same file contents that the original filesystem had.
93 _scratch_unmount
94 _scratch_mkfs >>$seqres.full 2>&1
95 _scratch_mount
96
97 $BTRFS_UTIL_PROG receive -f $send_files_dir/1.snap $SCRATCH_MNT
98 $BTRFS_UTIL_PROG receive -f $send_files_dir/2.snap $SCRATCH_MNT
99
100 # Compute digests of the snapshot trees in the new filesystem and compare them
101 # to the ones in the original filesystem, they must match.
102 $FSSUM_PROG -r $send_files_dir/base.fssum $SCRATCH_MNT/base
103 $FSSUM_PROG -r $send_files_dir/incr.fssum $SCRATCH_MNT/incr
104
105 status=0
106 exit