btrfs: fsync after hole punching with no-holes mode
[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
49 rm -f $seqres.full
50
51 test_btrfs_clone_same_file()
52 {
53         if [ -z $1 ]; then
54                 MOUNT_OPTIONS=""
55         else
56                 MOUNT_OPTIONS="-O $1"
57         fi
58         _scratch_mkfs >/dev/null 2>&1
59         _scratch_mount $MOUNT_OPTIONS
60
61         BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
62
63         EXTENT_SIZE=$((2 * $BLOCK_SIZE))
64
65         # Create a file with 5 extents, 4 extents of 2 blocks each and 1 extent
66         # of 16 blocks.
67         OFFSET=0
68         $XFS_IO_PROG -f -c "pwrite -S 0x01 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
69                 | _filter_xfs_io_blocks_modified
70         sync
71
72         OFFSET=$(($OFFSET + $EXTENT_SIZE))
73         $XFS_IO_PROG -c "pwrite -S 0x02 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
74                 | _filter_xfs_io_blocks_modified
75         sync
76
77         OFFSET=$(($OFFSET + $EXTENT_SIZE))
78         $XFS_IO_PROG -c "pwrite -S 0x03 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
79                 | _filter_xfs_io_blocks_modified
80         sync
81
82         OFFSET=$(($OFFSET + $EXTENT_SIZE))
83         $XFS_IO_PROG -c "pwrite -S 0x04 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
84                 | _filter_xfs_io_blocks_modified
85         sync
86
87         OFFSET=$(($OFFSET + $EXTENT_SIZE))
88         EXTENT_SIZE=$((16 * $BLOCK_SIZE))
89         $XFS_IO_PROG -c "pwrite -S 0x05 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
90                 | _filter_xfs_io_blocks_modified
91         sync
92
93         # Initial file content.
94         od -t x1 $SCRATCH_MNT/foo | _filter_od
95
96         # Same source and target ranges - must fail.
97         $CLONER_PROG -s $((2 * $BLOCK_SIZE)) -d $((2 * $BLOCK_SIZE)) \
98                      -l $((2 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/foo
99         # Check file content didn't change.
100         od -t x1 $SCRATCH_MNT/foo | _filter_od
101
102         # Intersection between source and target ranges - must fail too.
103         # $CLONER_PROG -s 4096 -d 8192 -l 8192 $SCRATCH_MNT/foo $SCRATCH_MNT/foo
104         $CLONER_PROG -s $((1 * $BLOCK_SIZE)) -d $((2 * $BLOCK_SIZE)) \
105                      -l $((2 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/foo
106         # Check file content didn't change.
107         od -t x1 $SCRATCH_MNT/foo | _filter_od
108
109         # Clone an entire extent from a higher range to a lower range.
110         $CLONER_PROG -s $((6 * $BLOCK_SIZE)) -d 0 -l $((2 * $BLOCK_SIZE)) \
111                      $SCRATCH_MNT/foo $SCRATCH_MNT/foo
112         # Check entire file, 0th and 1st blocks now have the same content
113         # as the 6th and 7th blocks.
114         od -t x1 $SCRATCH_MNT/foo | _filter_od
115
116         # Clone an entire extent from a lower range to a higher range.
117         $CLONER_PROG -s $((2 * $BLOCK_SIZE)) -d $((4 * $BLOCK_SIZE)) \
118                      -l $((2 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/foo
119         # Check entire file, 0th and 1st blocks now have the same content
120         # as the 6th and 7th block, and 4th and 5th blocks now has the same
121         # content as the 2nd and 3rd blocks.
122         od -t x1 $SCRATCH_MNT/foo | _filter_od
123
124         # Now clone 1 extent and an half into the file range starting
125         # at 16th block So we get the second half of the extent
126         # starting at 4th block and the whole extent starting at 6th
127         # block cloned into the middle of the 16 blocks extent that
128         # starts at 8th block. This makes the clone ioctl process more
129         # extent items from the b+tree and forces a split of the large
130         # 16-block extent at the end of the file.
131         $CLONER_PROG -s $((5 * $BLOCK_SIZE)) -d $((16 * $BLOCK_SIZE)) \
132                      -l $((3 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/foo
133
134         # Check entire file. Besides the previous changes, we now
135         # should have 1 block with the value 0x02 at 16th block, and 2
136         # blocks with value 0x04 starting at the 17th block . The
137         # block ranges [8, 16[ and [19, 24[ should remain with all
138         # bytes having the value 0x05.
139         od -t x1 $SCRATCH_MNT/foo | _filter_od
140
141         # Now update 2 blocks of data at offset 0. The extent at this
142         # position is a clone of the extent at 6th block. Check that
143         # writing to this offset doesn't change data at 6th block.
144         $XFS_IO_PROG -c "pwrite -S 0xff -b $((2 * $BLOCK_SIZE)) 0 $((2 * $BLOCK_SIZE))" \
145                      $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
146         od -t x1 $SCRATCH_MNT/foo | _filter_od
147
148         # Check that after defragmenting the file and re-mounting, the file
149         # content remains exactly the same as before.
150         _run_btrfs_util_prog filesystem defragment $SCRATCH_MNT/foo
151         _scratch_cycle_mount
152         od -t x1 $SCRATCH_MNT/foo | _filter_od
153
154         # Verify that there are no consistency errors.
155         _check_scratch_fs
156 }
157
158 # For any of the tests below, regardless of cow/nodatacow/compression, the
159 # results as observed by an application/user should be exactly the same.
160
161 echo "Testing with a cow file (default)"
162 test_btrfs_clone_same_file
163
164 _scratch_unmount
165
166 echo "Testing with a nocow file (-O nodatacow)"
167 test_btrfs_clone_same_file "nodatacow"
168
169 _scratch_unmount
170
171 echo "Testing with a cow file and lzo compression"
172 test_btrfs_clone_same_file "compress-force=lzo"
173
174 _scratch_unmount
175
176 echo "Testing with a cow file and zlib compression"
177 test_btrfs_clone_same_file "compress-force=zlib"
178
179 _scratch_unmount
180
181 echo "Testing with a nocow file and lzo compression"
182 test_btrfs_clone_same_file "nodatacow,compress-force=lzo"
183
184 _scratch_unmount
185
186 echo "Testing with a nocow file and zlib compression"
187 test_btrfs_clone_same_file "nodatacow,compress-force=zlib"
188
189 status=0
190 exit