c75193dd0654890fa8bc4a919cc913fc7751b0a3
[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         # Create a file with 5 extents, 4 of 8Kb each and 1 of 64Kb.
63         $XFS_IO_PROG -f -c "pwrite -S 0x01 -b 8192 0 8192" $SCRATCH_MNT/foo \
64                 | _filter_xfs_io
65         sync
66         $XFS_IO_PROG -c "pwrite -S 0x02 -b 8192 8192 8192" $SCRATCH_MNT/foo \
67                 | _filter_xfs_io
68         sync
69         $XFS_IO_PROG -c "pwrite -S 0x03 -b 8192 16384 8192" $SCRATCH_MNT/foo \
70                 | _filter_xfs_io
71         sync
72         $XFS_IO_PROG -c "pwrite -S 0x04 -b 8192 24576 8192" $SCRATCH_MNT/foo \
73                 | _filter_xfs_io
74         sync
75         $XFS_IO_PROG -c "pwrite -S 0x05 -b 65536 32768 65536" $SCRATCH_MNT/foo \
76                 | _filter_xfs_io
77         sync
78
79         # Digest of initial content.
80         md5sum $SCRATCH_MNT/foo | _filter_scratch
81
82         # Same source and target ranges - must fail.
83         $CLONER_PROG -s 8192 -d 8192 -l 8192 $SCRATCH_MNT/foo $SCRATCH_MNT/foo
84         # Check file content didn't change.
85         md5sum $SCRATCH_MNT/foo | _filter_scratch
86
87         # Intersection between source and target ranges - must fail too.
88         $CLONER_PROG -s 4096 -d 8192 -l 8192 $SCRATCH_MNT/foo $SCRATCH_MNT/foo
89         # Check file content didn't change.
90         md5sum $SCRATCH_MNT/foo | _filter_scratch
91
92         # Clone an entire extent from a higher range to a lower range.
93         $CLONER_PROG -s 24576 -d 0 -l 8192 $SCRATCH_MNT/foo $SCRATCH_MNT/foo
94
95         # Check entire file, the 8Kb block at offset 0 now has the same content
96         # as the 8Kb block at offset 24576.
97         od -t x1 $SCRATCH_MNT/foo
98
99         # Clone an entire extent from a lower range to a higher range.
100         $CLONER_PROG -s 8192 -d 16384 -l 8192 $SCRATCH_MNT/foo $SCRATCH_MNT/foo
101
102         # Check entire file, the 8Kb block at offset 0 now has the same content
103         # as the 8Kb block at offset 24576, and the 8Kb block at offset 16384
104         # now has the same content as the 8Kb block at offset 8192.
105         od -t x1 $SCRATCH_MNT/foo
106
107         # Now clone 1 extent and an half into the file range starting at offset
108         # 65536. So we get the second half of the extent at offset 16384 and the
109         # whole extent at 24576 cloned into the middle of the 64Kb extent that
110         # starts at file offset 32768. This makes the clone ioctl process more
111         # extent items from the b+tree and forces a split of the large 64Kb
112         # extent at the end of the file.
113         $CLONER_PROG -s 20480 -d 65536 -l 12288 $SCRATCH_MNT/foo \
114                 $SCRATCH_MNT/foo
115
116         # Check entire file. Besides the previous changes, we now should have
117         # 4096 bytes with the value 0x02 at file offset 65536, and 8192 bytes
118         # with value 0x04 at the file offset 69632. The ranges [32768, 65536[
119         # and [77824, 98304[ should remain with all bytes having the value 0x05.
120         od -t x1 $SCRATCH_MNT/foo
121
122         # Now update 8Kb of data at offset 0. The extent at this position is a
123         # clone of the extent at offset 24576. Check that writing to this offset
124         # doesn't change data at offset 24576.
125         $XFS_IO_PROG -c "pwrite -S 0xff -b 8192 0 8192" $SCRATCH_MNT/foo \
126                 | _filter_xfs_io
127         od -t x1 $SCRATCH_MNT/foo
128
129         # Check that after defragmenting the file and re-mounting, the file
130         # content remains exactly the same as before.
131         _run_btrfs_util_prog filesystem defragment $SCRATCH_MNT/foo
132         _scratch_remount
133         od -t x1 $SCRATCH_MNT/foo
134
135         # Verify that there are no consistency errors.
136         _check_scratch_fs
137 }
138
139 # For any of the tests below, regardless of cow/nodatacow/compression, the
140 # results as observed by an application/user should be exactly the same.
141
142 echo "Testing with a cow file (default)"
143 test_btrfs_clone_same_file
144
145 _scratch_unmount
146
147 echo "Testing with a nocow file (-O nodatacow)"
148 test_btrfs_clone_same_file "nodatacow"
149
150 _scratch_unmount
151
152 echo "Testing with a cow file and lzo compression"
153 test_btrfs_clone_same_file "compress-force=lzo"
154
155 _scratch_unmount
156
157 echo "Testing with a cow file and zlib compression"
158 test_btrfs_clone_same_file "compress-force=zlib"
159
160 _scratch_unmount
161
162 echo "Testing with a nocow file and lzo compression"
163 test_btrfs_clone_same_file "nodatacow,compress-force=lzo"
164
165 _scratch_unmount
166
167 echo "Testing with a nocow file and zlib compression"
168 test_btrfs_clone_same_file "nodatacow,compress-force=zlib"
169
170 status=0
171 exit