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