fstests: move test group info to test files
[xfstests-dev.git] / tests / btrfs / 055
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2014 Filipe Manana.  All Rights Reserved.
4 #
5 # FS QA Test No. btrfs/055
6 #
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).
10 #
11 # This issue is fixed by the following linux kernel btrfs patch:
12 #
13 #    Btrfs: fix clone to deal with holes when NO_HOLES feature is enabled
14 #
15 . ./common/preamble
16 _begin_fstest auto quick clone
17
18 # Override the default cleanup function.
19 _cleanup()
20 {
21     rm -fr $tmp
22 }
23
24 # Import common functions.
25 . ./common/filter
26
27 # real QA test starts here
28 _supported_fs btrfs
29 _require_scratch
30 _require_cloner
31 _require_btrfs_fs_feature "no_holes"
32 _require_btrfs_mkfs_feature "no-holes"
33
34 test_btrfs_clone_with_holes()
35 {
36         _scratch_mkfs "$1" >/dev/null 2>&1
37         _scratch_mount
38
39         BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
40
41         EXTENT_SIZE=$((2 * $BLOCK_SIZE))
42
43         OFFSET=0
44
45         # Create a file with 4 extents and 1 hole, all with 2 blocks each.
46         # The hole is in the block range [4, 5[.
47         $XFS_IO_PROG -s -f -c "pwrite -S 0x01 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" \
48                      $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
49
50         OFFSET=$(($OFFSET + $EXTENT_SIZE))
51         $XFS_IO_PROG -s -f -c "pwrite -S 0x02 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" \
52                      $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
53
54         OFFSET=$(($OFFSET + 2 * $EXTENT_SIZE))
55         $XFS_IO_PROG -s -f -c "pwrite -S 0x04 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" \
56                      $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
57
58         OFFSET=$(($OFFSET + $EXTENT_SIZE))
59         $XFS_IO_PROG -s -f -c "pwrite -S 0x05 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" \
60                      $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
61
62         # Clone destination file, 1 extent of 24 blocks.
63         EXTENT_SIZE=$((24 * $BLOCK_SIZE))
64         $XFS_IO_PROG -s -f -c "pwrite -S 0xff -b $EXTENT_SIZE 0 $EXTENT_SIZE" \
65                 $SCRATCH_MNT/bar | _filter_xfs_io_blocks_modified
66
67         # Clone 2nd extent, 2-blocks sized hole and 3rd extent of foo into bar.
68         $CLONER_PROG -s $((2 * $BLOCK_SIZE)) -d 0 -l $((6 * $BLOCK_SIZE)) \
69                      $SCRATCH_MNT/foo $SCRATCH_MNT/bar
70
71         # Verify both extents and the hole were cloned.
72         echo "1) Check both extents and the hole were cloned"
73         od -t x1 $SCRATCH_MNT/bar | _filter_od
74
75         # Cloning range starts at the middle of a hole.
76         $CLONER_PROG -s $((5 * $BLOCK_SIZE)) -d $((8 * $BLOCK_SIZE)) \
77                      -l $((3 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/bar
78
79         # Verify that half of the hole and the following 2 block extent were cloned.
80         echo "2) Check half hole and the following 2 block extent were cloned"
81         od -t x1 $SCRATCH_MNT/bar | _filter_od
82
83         # Cloning range ends at the middle of a hole.
84         $CLONER_PROG -s 0 -d $((16 * $BLOCK_SIZE)) -l $((5 * $BLOCK_SIZE)) \
85                      $SCRATCH_MNT/foo $SCRATCH_MNT/bar
86
87         # Verify that 2 extents of 2 blocks size and a 1-block hole were cloned.
88         echo "3) Check that 2 extents of 2 blocks each and a hole of 1 block were cloned"
89         od -t x1 $SCRATCH_MNT/bar | _filter_od
90
91         # Create a 6-block hole at the end of the source file (foo).
92         $XFS_IO_PROG -c "truncate $((16 * $BLOCK_SIZE))" $SCRATCH_MNT/foo \
93                 | _filter_xfs_io_blocks_modified
94         sync
95
96         # Now clone a range that overlaps that hole at the end of the foo file.
97         # It should clone the 10th block and the first two blocks of the hole
98         # at the end of foo.
99         $CLONER_PROG -s $((9 * $BLOCK_SIZE)) -d $((21 * $BLOCK_SIZE)) \
100                      -l $((3 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/bar
101
102         # Verify that the 9th block of foo and the first 2 blocks of the
103         # 6-block hole of foo were cloned into bar.
104         echo "4) Check that a block of 1 extent and 2 blocks of a hole were cloned"
105         od -t x1 $SCRATCH_MNT/bar | _filter_od
106
107         # Clone the same range as before, but clone it into a different offset
108         # of the target (bar) such that it increases the size of the target
109         # by 2 blocks.
110         $CLONER_PROG -s $((9 * $BLOCK_SIZE)) -d $((23 * $BLOCK_SIZE)) \
111                      -l $((3 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/bar
112
113         # Verify that the 9th block of foo and the first 2 blocks of the 6-block
114         # hole of foo were cloned into bar at bar's 23rd block and that bar's
115         # size increased by 2 blocks.
116         echo "5) Check that a block of 1 extent and 2 blocks of a hole were" \
117              "cloned and file size increased"
118         od -t x1 $SCRATCH_MNT/bar | _filter_od
119
120         # Create a new completely sparse file (no extents, it's a big hole).
121         $XFS_IO_PROG -f -c "truncate $((25 * $BLOCK_SIZE))" $SCRATCH_MNT/qwerty \
122                 | _filter_xfs_io_blocks_modified
123         sync
124
125         # Test cloning a range from the sparse file to the bar file without
126         # increasing bar's size.
127         $CLONER_PROG -s $((1 * $BLOCK_SIZE)) -d 0 -l $((2 * $BLOCK_SIZE)) \
128                      $SCRATCH_MNT/qwerty $SCRATCH_MNT/bar
129
130         # First 2 blocks of bar should now be zeroes.
131         echo "6) Check that 2 blocks of the hole were cloned"
132         od -t x1 $SCRATCH_MNT/bar | _filter_od
133
134         # Test cloning a range from the sparse file to the end of the bar file.
135         # The bar file currently has 26 blocks.
136         $CLONER_PROG -s 0 -d $((26 * $BLOCK_SIZE)) -l $((8 * $BLOCK_SIZE)) $SCRATCH_MNT/qwerty \
137                 $SCRATCH_MNT/bar
138
139         # Verify bar's size increased to 26 + 8 blocks, and its
140         # last 8 blocks are all zeroes.
141         echo "7) Check that 8 blocks of the hole were cloned and the file size increased"
142         od -t x1 $SCRATCH_MNT/bar | _filter_od
143
144         # Verify that there are no consistency errors.
145         _check_scratch_fs
146 }
147
148 # Regardless of the NO_HOLES feature being enabled or not, the test results
149 # should be exactly the same for both cases.
150
151 echo "Testing without the NO_HOLES feature"
152 # As of btrfs-progs 3.14.x, the no-holes feature isn't enabled by default.
153 # But explicitly disable it at mkfs time as it might be enabled by default
154 # in future versions.
155 test_btrfs_clone_with_holes "-O ^no-holes"
156
157 _scratch_unmount
158
159 echo "Testing with the NO_HOLES feature enabled"
160 test_btrfs_clone_with_holes "-O no-holes"
161
162 status=0
163 exit