2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2014 Filipe Manana. All Rights Reserved.
5 # FS QA Test No. btrfs/055
7 # Regression test for the btrfs ioctl clone operation when the source range
8 # contains hole(s) and the FS has the NO_HOLES feature enabled (file holes
9 # don't need file extent items in the btree to represent them).
11 # This issue is fixed by the following linux kernel btrfs patch:
13 # Btrfs: fix clone to deal with holes when NO_HOLES feature is enabled
16 seqres=$RESULT_DIR/$seq
17 echo "QA output created by $seq"
20 status=1 # failure is the default!
21 trap "_cleanup; exit \$status" 0 1 2 3 15
28 # get standard environment, filters and checks
32 # real QA test starts here
37 _require_btrfs_fs_feature "no_holes"
38 _require_btrfs_mkfs_feature "no-holes"
42 test_btrfs_clone_with_holes()
44 _scratch_mkfs "$1" >/dev/null 2>&1
47 BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
49 EXTENT_SIZE=$((2 * $BLOCK_SIZE))
53 # Create a file with 4 extents and 1 hole, all with 2 blocks each.
54 # The hole is in the block range [4, 5[.
55 $XFS_IO_PROG -s -f -c "pwrite -S 0x01 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" \
56 $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
58 OFFSET=$(($OFFSET + $EXTENT_SIZE))
59 $XFS_IO_PROG -s -f -c "pwrite -S 0x02 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" \
60 $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
62 OFFSET=$(($OFFSET + 2 * $EXTENT_SIZE))
63 $XFS_IO_PROG -s -f -c "pwrite -S 0x04 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" \
64 $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
66 OFFSET=$(($OFFSET + $EXTENT_SIZE))
67 $XFS_IO_PROG -s -f -c "pwrite -S 0x05 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" \
68 $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
70 # Clone destination file, 1 extent of 24 blocks.
71 EXTENT_SIZE=$((24 * $BLOCK_SIZE))
72 $XFS_IO_PROG -s -f -c "pwrite -S 0xff -b $EXTENT_SIZE 0 $EXTENT_SIZE" \
73 $SCRATCH_MNT/bar | _filter_xfs_io_blocks_modified
75 # Clone 2nd extent, 2-blocks sized hole and 3rd extent of foo into bar.
76 $CLONER_PROG -s $((2 * $BLOCK_SIZE)) -d 0 -l $((6 * $BLOCK_SIZE)) \
77 $SCRATCH_MNT/foo $SCRATCH_MNT/bar
79 # Verify both extents and the hole were cloned.
80 echo "1) Check both extents and the hole were cloned"
81 od -t x1 $SCRATCH_MNT/bar | _filter_od
83 # Cloning range starts at the middle of a hole.
84 $CLONER_PROG -s $((5 * $BLOCK_SIZE)) -d $((8 * $BLOCK_SIZE)) \
85 -l $((3 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/bar
87 # Verify that half of the hole and the following 2 block extent were cloned.
88 echo "2) Check half hole and the following 2 block extent were cloned"
89 od -t x1 $SCRATCH_MNT/bar | _filter_od
91 # Cloning range ends at the middle of a hole.
92 $CLONER_PROG -s 0 -d $((16 * $BLOCK_SIZE)) -l $((5 * $BLOCK_SIZE)) \
93 $SCRATCH_MNT/foo $SCRATCH_MNT/bar
95 # Verify that 2 extents of 2 blocks size and a 1-block hole were cloned.
96 echo "3) Check that 2 extents of 2 blocks each and a hole of 1 block were cloned"
97 od -t x1 $SCRATCH_MNT/bar | _filter_od
99 # Create a 6-block hole at the end of the source file (foo).
100 $XFS_IO_PROG -c "truncate $((16 * $BLOCK_SIZE))" $SCRATCH_MNT/foo \
101 | _filter_xfs_io_blocks_modified
104 # Now clone a range that overlaps that hole at the end of the foo file.
105 # It should clone the 10th block and the first two blocks of the hole
107 $CLONER_PROG -s $((9 * $BLOCK_SIZE)) -d $((21 * $BLOCK_SIZE)) \
108 -l $((3 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/bar
110 # Verify that the 9th block of foo and the first 2 blocks of the
111 # 6-block hole of foo were cloned into bar.
112 echo "4) Check that a block of 1 extent and 2 blocks of a hole were cloned"
113 od -t x1 $SCRATCH_MNT/bar | _filter_od
115 # Clone the same range as before, but clone it into a different offset
116 # of the target (bar) such that it increases the size of the target
118 $CLONER_PROG -s $((9 * $BLOCK_SIZE)) -d $((23 * $BLOCK_SIZE)) \
119 -l $((3 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/bar
121 # Verify that the 9th block of foo and the first 2 blocks of the 6-block
122 # hole of foo were cloned into bar at bar's 23rd block and that bar's
123 # size increased by 2 blocks.
124 echo "5) Check that a block of 1 extent and 2 blocks of a hole were" \
125 "cloned and file size increased"
126 od -t x1 $SCRATCH_MNT/bar | _filter_od
128 # Create a new completely sparse file (no extents, it's a big hole).
129 $XFS_IO_PROG -f -c "truncate $((25 * $BLOCK_SIZE))" $SCRATCH_MNT/qwerty \
130 | _filter_xfs_io_blocks_modified
133 # Test cloning a range from the sparse file to the bar file without
134 # increasing bar's size.
135 $CLONER_PROG -s $((1 * $BLOCK_SIZE)) -d 0 -l $((2 * $BLOCK_SIZE)) \
136 $SCRATCH_MNT/qwerty $SCRATCH_MNT/bar
138 # First 2 blocks of bar should now be zeroes.
139 echo "6) Check that 2 blocks of the hole were cloned"
140 od -t x1 $SCRATCH_MNT/bar | _filter_od
142 # Test cloning a range from the sparse file to the end of the bar file.
143 # The bar file currently has 26 blocks.
144 $CLONER_PROG -s 0 -d $((26 * $BLOCK_SIZE)) -l $((8 * $BLOCK_SIZE)) $SCRATCH_MNT/qwerty \
147 # Verify bar's size increased to 26 + 8 blocks, and its
148 # last 8 blocks are all zeroes.
149 echo "7) Check that 8 blocks of the hole were cloned and the file size increased"
150 od -t x1 $SCRATCH_MNT/bar | _filter_od
152 # Verify that there are no consistency errors.
156 # Regardless of the NO_HOLES feature being enabled or not, the test results
157 # should be exactly the same for both cases.
159 echo "Testing without the NO_HOLES feature"
160 # As of btrfs-progs 3.14.x, the no-holes feature isn't enabled by default.
161 # But explicitly disable it at mkfs time as it might be enabled by default
162 # in future versions.
163 test_btrfs_clone_with_holes "-O ^no-holes"
167 echo "Testing with the NO_HOLES feature enabled"
168 test_btrfs_clone_with_holes "-O no-holes"