Rename _scratch_mount to _scratch_cycle_mount
[xfstests-dev.git] / tests / btrfs / 052
1 #! /bin/bash
2 # FS QA Test No. btrfs/052
3 #
4 # Verify that the btrfs ioctl clone operation can operate on the same
5 # file as a source and target. That is, clone extents within the same
6 # file.
7 #
8 #-----------------------------------------------------------------------
9 # Copyright (c) 2014 Filipe Manana.  All Rights Reserved.
10 #
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License as
13 # published by the Free Software Foundation.
14 #
15 # This program is distributed in the hope that it would be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write the Free Software Foundation,
22 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23 #-----------------------------------------------------------------------
24 #
25
26 seq=`basename $0`
27 seqres=$RESULT_DIR/$seq
28 echo "QA output created by $seq"
29
30 tmp=/tmp/$$
31 status=1        # failure is the default!
32 trap "_cleanup; exit \$status" 0 1 2 3 15
33
34 _cleanup()
35 {
36     rm -fr $tmp
37 }
38
39 # get standard environment, filters and checks
40 . ./common/rc
41 . ./common/filter
42
43 # real QA test starts here
44 _supported_fs btrfs
45 _supported_os Linux
46 _require_scratch
47 _require_cloner
48 _need_to_be_root
49
50 rm -f $seqres.full
51
52 test_btrfs_clone_same_file()
53 {
54         if [ -z $1 ]; then
55                 MOUNT_OPTIONS=""
56         else
57                 MOUNT_OPTIONS="-O $1"
58         fi
59         _scratch_mkfs >/dev/null 2>&1
60         _scratch_mount $MOUNT_OPTIONS
61
62         BLOCK_SIZE=$(get_block_size $SCRATCH_MNT)
63
64         EXTENT_SIZE=$((2 * $BLOCK_SIZE))
65
66         # Create a file with 5 extents, 4 extents of 2 blocks each and 1 extent
67         # of 16 blocks.
68         OFFSET=0
69         $XFS_IO_PROG -f -c "pwrite -S 0x01 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
70                 | _filter_xfs_io_blocks_modified
71         sync
72
73         OFFSET=$(($OFFSET + $EXTENT_SIZE))
74         $XFS_IO_PROG -c "pwrite -S 0x02 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
75                 | _filter_xfs_io_blocks_modified
76         sync
77
78         OFFSET=$(($OFFSET + $EXTENT_SIZE))
79         $XFS_IO_PROG -c "pwrite -S 0x03 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
80                 | _filter_xfs_io_blocks_modified
81         sync
82
83         OFFSET=$(($OFFSET + $EXTENT_SIZE))
84         $XFS_IO_PROG -c "pwrite -S 0x04 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
85                 | _filter_xfs_io_blocks_modified
86         sync
87
88         OFFSET=$(($OFFSET + $EXTENT_SIZE))
89         EXTENT_SIZE=$((16 * $BLOCK_SIZE))
90         $XFS_IO_PROG -c "pwrite -S 0x05 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
91                 | _filter_xfs_io_blocks_modified
92         sync
93
94         # Initial file content.
95         od -t x1 $SCRATCH_MNT/foo | _filter_od
96
97         # Same source and target ranges - must fail.
98         $CLONER_PROG -s $((2 * $BLOCK_SIZE)) -d $((2 * $BLOCK_SIZE)) \
99                      -l $((2 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/foo
100         # Check file content didn't change.
101         od -t x1 $SCRATCH_MNT/foo | _filter_od
102
103         # Intersection between source and target ranges - must fail too.
104         # $CLONER_PROG -s 4096 -d 8192 -l 8192 $SCRATCH_MNT/foo $SCRATCH_MNT/foo
105         $CLONER_PROG -s $((1 * $BLOCK_SIZE)) -d $((2 * $BLOCK_SIZE)) \
106                      -l $((2 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/foo
107         # Check file content didn't change.
108         od -t x1 $SCRATCH_MNT/foo | _filter_od
109
110         # Clone an entire extent from a higher range to a lower range.
111         $CLONER_PROG -s $((6 * $BLOCK_SIZE)) -d 0 -l $((2 * $BLOCK_SIZE)) \
112                      $SCRATCH_MNT/foo $SCRATCH_MNT/foo
113         # Check entire file, 0th and 1st blocks now have the same content
114         # as the 6th and 7th blocks.
115         od -t x1 $SCRATCH_MNT/foo | _filter_od
116
117         # Clone an entire extent from a lower range to a higher range.
118         $CLONER_PROG -s $((2 * $BLOCK_SIZE)) -d $((4 * $BLOCK_SIZE)) \
119                      -l $((2 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/foo
120         # Check entire file, 0th and 1st blocks now have the same content
121         # as the 6th and 7th block, and 4th and 5th blocks now has the same
122         # content as the 2nd and 3rd blocks.
123         od -t x1 $SCRATCH_MNT/foo | _filter_od
124
125         # Now clone 1 extent and an half into the file range starting
126         # at 16th block So we get the second half of the extent
127         # starting at 4th block and the whole extent starting at 6th
128         # block cloned into the middle of the 16 blocks extent that
129         # starts at 8th block. This makes the clone ioctl process more
130         # extent items from the b+tree and forces a split of the large
131         # 16-block extent at the end of the file.
132         $CLONER_PROG -s $((5 * $BLOCK_SIZE)) -d $((16 * $BLOCK_SIZE)) \
133                      -l $((3 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/foo
134
135         # Check entire file. Besides the previous changes, we now
136         # should have 1 block with the value 0x02 at 16th block, and 2
137         # blocks with value 0x04 starting at the 17th block . The
138         # block ranges [8, 16[ and [19, 24[ should remain with all
139         # bytes having the value 0x05.
140         od -t x1 $SCRATCH_MNT/foo | _filter_od
141
142         # Now update 2 blocks of data at offset 0. The extent at this
143         # position is a clone of the extent at 6th block. Check that
144         # writing to this offset doesn't change data at 6th block.
145         $XFS_IO_PROG -c "pwrite -S 0xff -b $((2 * $BLOCK_SIZE)) 0 $((2 * $BLOCK_SIZE))" \
146                      $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
147         od -t x1 $SCRATCH_MNT/foo | _filter_od
148
149         # Check that after defragmenting the file and re-mounting, the file
150         # content remains exactly the same as before.
151         _run_btrfs_util_prog filesystem defragment $SCRATCH_MNT/foo
152         _scratch_cycle_mount
153         od -t x1 $SCRATCH_MNT/foo | _filter_od
154
155         # Verify that there are no consistency errors.
156         _check_scratch_fs
157 }
158
159 # For any of the tests below, regardless of cow/nodatacow/compression, the
160 # results as observed by an application/user should be exactly the same.
161
162 echo "Testing with a cow file (default)"
163 test_btrfs_clone_same_file
164
165 _scratch_unmount
166
167 echo "Testing with a nocow file (-O nodatacow)"
168 test_btrfs_clone_same_file "nodatacow"
169
170 _scratch_unmount
171
172 echo "Testing with a cow file and lzo compression"
173 test_btrfs_clone_same_file "compress-force=lzo"
174
175 _scratch_unmount
176
177 echo "Testing with a cow file and zlib compression"
178 test_btrfs_clone_same_file "compress-force=zlib"
179
180 _scratch_unmount
181
182 echo "Testing with a nocow file and lzo compression"
183 test_btrfs_clone_same_file "nodatacow,compress-force=lzo"
184
185 _scratch_unmount
186
187 echo "Testing with a nocow file and zlib compression"
188 test_btrfs_clone_same_file "nodatacow,compress-force=zlib"
189
190 status=0
191 exit