xfs: Check for extent overflow when trivally adding a new extent
[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 _require_scratch
35 _require_cloner
36
37 rm -f $seqres.full
38
39 # Create a file with an extent layout that confused the btrfs clone ioctl
40 # implementation. The first extent item that is cloned by the second call
41 # to the cloner program will have only a trailing part of it referenced by
42 # a new extent item, since the source offset starts in the middle of that
43 # extent. This confused the clone ioctl because after inserting this new
44 # extent item it would immediately after process it again thinking it
45 # corresponded to an extent that existed before - this made it attempt to
46 # insert a duplicated extent item pointing to the same extent again, which
47 # made it return an -EEXIST error to userspace and turn the filesystem to
48 # readonly mode (since the current transaction got aborted).
49 test_clone()
50 {
51         local bs=$1
52
53         $XFS_IO_PROG -f -c "pwrite -S 0xaa $((2 * $bs)) $((2 * $bs))" \
54                 $SCRATCH_MNT/foo | _filter_xfs_io
55
56         $CLONER_PROG -s $((3 * $bs)) -d $((267 * $bs)) -l 0 $SCRATCH_MNT/foo \
57                 $SCRATCH_MNT/foo
58         $CLONER_PROG -s $((217 * $bs)) -d $((95 * $bs)) -l 0 $SCRATCH_MNT/foo \
59                 $SCRATCH_MNT/foo
60
61         echo "File digest after clone operations using same file as source and destination"
62         md5sum $SCRATCH_MNT/foo | _filter_scratch
63
64         # Test cloning using different source and destination files for the
65         # same exact data - it must produce the exact same result as the case
66         # before.
67         $XFS_IO_PROG -f -c "pwrite -S 0xaa $((2 * $bs)) $((2 * $bs))" \
68                 $SCRATCH_MNT/a | _filter_xfs_io
69         cp $SCRATCH_MNT/a $SCRATCH_MNT/b
70
71         $CLONER_PROG -s $((3 * $bs)) -d $((267 * $bs)) -l 0 $SCRATCH_MNT/a \
72                 $SCRATCH_MNT/b
73
74         cp $SCRATCH_MNT/b $SCRATCH_MNT/foo2
75         $CLONER_PROG -s $((217 * $bs)) -d $((95 * $bs)) -l 0 $SCRATCH_MNT/b \
76                 $SCRATCH_MNT/foo2
77
78         echo "File digest after clone operations using different files as source and destination"
79         md5sum $SCRATCH_MNT/foo2 | _filter_scratch
80
81 }
82
83 # Make sure the test passes offsets and lengths to the btrfs clone ioctl that
84 # are multiples of the fs block size. Currently the block size on btrfs must
85 # be a multiple of the page size, so use a 64Kb fs block size in order to be
86 # able to test on every platform supported by linux.
87 bs=$((64 * 1024))
88
89 echo "Testing without the no-holes feature"
90 _scratch_mkfs "-O ^no-holes -n $bs" >>$seqres.full 2>&1
91 _scratch_mount
92 test_clone $bs
93 _check_scratch_fs
94
95 echo -e "\nTesting with the no-holes feature"
96 _scratch_unmount
97 _scratch_mkfs "-O no-holes -n $bs" >>$seqres.full 2>&1
98 _scratch_mount
99 test_clone $bs
100
101 status=0
102 exit