2 # FS QA Test No. btrfs/052
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
8 #-----------------------------------------------------------------------
9 # Copyright (c) 2014 Filipe Manana. All Rights Reserved.
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.
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.
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 #-----------------------------------------------------------------------
27 seqres=$RESULT_DIR/$seq
28 echo "QA output created by $seq"
31 status=1 # failure is the default!
32 trap "_cleanup; exit \$status" 0 1 2 3 15
39 # get standard environment, filters and checks
43 # real QA test starts here
51 test_btrfs_clone_same_file()
58 _scratch_mkfs >/dev/null 2>&1
59 _scratch_mount $MOUNT_OPTIONS
61 BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
63 EXTENT_SIZE=$((2 * $BLOCK_SIZE))
65 # Create a file with 5 extents, 4 extents of 2 blocks each and 1 extent
68 $XFS_IO_PROG -f -c "pwrite -S 0x01 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
69 | _filter_xfs_io_blocks_modified
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
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
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
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
93 # Initial file content.
94 od -t x1 $SCRATCH_MNT/foo | _filter_od
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
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
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
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
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
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
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
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
152 od -t x1 $SCRATCH_MNT/foo | _filter_od
154 # Verify that there are no consistency errors.
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.
161 echo "Testing with a cow file (default)"
162 test_btrfs_clone_same_file
166 echo "Testing with a nocow file (-O nodatacow)"
167 test_btrfs_clone_same_file "nodatacow"
171 echo "Testing with a cow file and lzo compression"
172 test_btrfs_clone_same_file "compress-force=lzo"
176 echo "Testing with a cow file and zlib compression"
177 test_btrfs_clone_same_file "compress-force=zlib"
181 echo "Testing with a nocow file and lzo compression"
182 test_btrfs_clone_same_file "nodatacow,compress-force=lzo"
186 echo "Testing with a nocow file and zlib compression"
187 test_btrfs_clone_same_file "nodatacow,compress-force=zlib"