btrfs: Verify falloc on multiple holes won't leak qgroup reserved data space
[xfstests-dev.git] / tests / btrfs / 093
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FS QA Test No. btrfs/093
6 #
7 # Test btrfs file range cloning with the same file as a source and destination.
8 #
9 # This tests a specific scenario where the extent layout of the file confused
10 # the clone ioctl implementation making it return -EEXIST to userspace.
11 # This issue was fixed by the following linux kernel patch:
12 #
13 #    Btrfs: fix range cloning when same inode used as source and destination
14 #
15 seq=`basename $0`
16 seqres=$RESULT_DIR/$seq
17 echo "QA output created by $seq"
18
19 tmp=/tmp/$$
20 status=1        # failure is the default!
21 trap "_cleanup; exit \$status" 0 1 2 3 15
22
23 _cleanup()
24 {
25         rm -f $tmp.*
26 }
27
28 # get standard environment, filters and checks
29 . ./common/rc
30 . ./common/filter
31
32 # real QA test starts here
33 _supported_fs btrfs
34 _supported_os Linux
35 _require_scratch
36 _require_cloner
37
38 rm -f $seqres.full
39
40 # Create a file with an extent layout that confused the btrfs clone ioctl
41 # implementation. The first extent item that is cloned by the second call
42 # to the cloner program will have only a trailing part of it referenced by
43 # a new extent item, since the source offset starts in the middle of that
44 # extent. This confused the clone ioctl because after inserting this new
45 # extent item it would immediately after process it again thinking it
46 # corresponded to an extent that existed before - this made it attempt to
47 # insert a duplicated extent item pointing to the same extent again, which
48 # made it return an -EEXIST error to userspace and turn the filesystem to
49 # readonly mode (since the current transaction got aborted).
50 test_clone()
51 {
52         local bs=$1
53
54         $XFS_IO_PROG -f -c "pwrite -S 0xaa $((2 * $bs)) $((2 * $bs))" \
55                 $SCRATCH_MNT/foo | _filter_xfs_io
56
57         $CLONER_PROG -s $((3 * $bs)) -d $((267 * $bs)) -l 0 $SCRATCH_MNT/foo \
58                 $SCRATCH_MNT/foo
59         $CLONER_PROG -s $((217 * $bs)) -d $((95 * $bs)) -l 0 $SCRATCH_MNT/foo \
60                 $SCRATCH_MNT/foo
61
62         echo "File digest after clone operations using same file as source and destination"
63         md5sum $SCRATCH_MNT/foo | _filter_scratch
64
65         # Test cloning using different source and destination files for the
66         # same exact data - it must produce the exact same result as the case
67         # before.
68         $XFS_IO_PROG -f -c "pwrite -S 0xaa $((2 * $bs)) $((2 * $bs))" \
69                 $SCRATCH_MNT/a | _filter_xfs_io
70         cp $SCRATCH_MNT/a $SCRATCH_MNT/b
71
72         $CLONER_PROG -s $((3 * $bs)) -d $((267 * $bs)) -l 0 $SCRATCH_MNT/a \
73                 $SCRATCH_MNT/b
74
75         cp $SCRATCH_MNT/b $SCRATCH_MNT/foo2
76         $CLONER_PROG -s $((217 * $bs)) -d $((95 * $bs)) -l 0 $SCRATCH_MNT/b \
77                 $SCRATCH_MNT/foo2
78
79         echo "File digest after clone operations using different files as source and destination"
80         md5sum $SCRATCH_MNT/foo2 | _filter_scratch
81
82 }
83
84 # Make sure the test passes offsets and lengths to the btrfs clone ioctl that
85 # are multiples of the fs block size. Currently the block size on btrfs must
86 # be a multiple of the page size, so use a 64Kb fs block size in order to be
87 # able to test on every platform supported by linux.
88 bs=$((64 * 1024))
89
90 echo "Testing without the no-holes feature"
91 _scratch_mkfs "-O ^no-holes -n $bs" >>$seqres.full 2>&1
92 _scratch_mount
93 test_clone $bs
94 _check_scratch_fs
95
96 echo -e "\nTesting with the no-holes feature"
97 _scratch_unmount
98 _scratch_mkfs "-O no-holes -n $bs" >>$seqres.full 2>&1
99 _scratch_mount
100 test_clone $bs
101
102 status=0
103 exit