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