2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2014 Filipe Manana. All Rights Reserved.
5 # FS QA Test No. btrfs/052
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
12 _begin_fstest auto quick clone
14 # Override the default cleanup function.
20 # Import common functions.
23 # real QA test starts here
28 test_btrfs_clone_same_file()
35 _scratch_mkfs >/dev/null 2>&1
36 _scratch_mount $MOUNT_OPTIONS
38 BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
40 EXTENT_SIZE=$((2 * $BLOCK_SIZE))
42 # Create a file with 5 extents, 4 extents of 2 blocks each and 1 extent
45 $XFS_IO_PROG -f -c "pwrite -S 0x01 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
46 | _filter_xfs_io_blocks_modified
49 OFFSET=$(($OFFSET + $EXTENT_SIZE))
50 $XFS_IO_PROG -c "pwrite -S 0x02 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
51 | _filter_xfs_io_blocks_modified
54 OFFSET=$(($OFFSET + $EXTENT_SIZE))
55 $XFS_IO_PROG -c "pwrite -S 0x03 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
56 | _filter_xfs_io_blocks_modified
59 OFFSET=$(($OFFSET + $EXTENT_SIZE))
60 $XFS_IO_PROG -c "pwrite -S 0x04 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
61 | _filter_xfs_io_blocks_modified
64 OFFSET=$(($OFFSET + $EXTENT_SIZE))
65 EXTENT_SIZE=$((16 * $BLOCK_SIZE))
66 $XFS_IO_PROG -c "pwrite -S 0x05 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
67 | _filter_xfs_io_blocks_modified
70 # Initial file content.
71 od -t x1 $SCRATCH_MNT/foo | _filter_od
73 # Same source and target ranges - must fail.
74 $CLONER_PROG -s $((2 * $BLOCK_SIZE)) -d $((2 * $BLOCK_SIZE)) \
75 -l $((2 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/foo
76 # Check file content didn't change.
77 od -t x1 $SCRATCH_MNT/foo | _filter_od
79 # Intersection between source and target ranges - must fail too.
80 # $CLONER_PROG -s 4096 -d 8192 -l 8192 $SCRATCH_MNT/foo $SCRATCH_MNT/foo
81 $CLONER_PROG -s $((1 * $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
86 # Clone an entire extent from a higher range to a lower range.
87 $CLONER_PROG -s $((6 * $BLOCK_SIZE)) -d 0 -l $((2 * $BLOCK_SIZE)) \
88 $SCRATCH_MNT/foo $SCRATCH_MNT/foo
89 # Check entire file, 0th and 1st blocks now have the same content
90 # as the 6th and 7th blocks.
91 od -t x1 $SCRATCH_MNT/foo | _filter_od
93 # Clone an entire extent from a lower range to a higher range.
94 $CLONER_PROG -s $((2 * $BLOCK_SIZE)) -d $((4 * $BLOCK_SIZE)) \
95 -l $((2 * $BLOCK_SIZE)) $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 block, and 4th and 5th blocks now has the same
98 # content as the 2nd and 3rd blocks.
99 od -t x1 $SCRATCH_MNT/foo | _filter_od
101 # Now clone 1 extent and an half into the file range starting
102 # at 16th block So we get the second half of the extent
103 # starting at 4th block and the whole extent starting at 6th
104 # block cloned into the middle of the 16 blocks extent that
105 # starts at 8th block. This makes the clone ioctl process more
106 # extent items from the b+tree and forces a split of the large
107 # 16-block extent at the end of the file.
108 $CLONER_PROG -s $((5 * $BLOCK_SIZE)) -d $((16 * $BLOCK_SIZE)) \
109 -l $((3 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/foo
111 # Check entire file. Besides the previous changes, we now
112 # should have 1 block with the value 0x02 at 16th block, and 2
113 # blocks with value 0x04 starting at the 17th block . The
114 # block ranges [8, 16[ and [19, 24[ should remain with all
115 # bytes having the value 0x05.
116 od -t x1 $SCRATCH_MNT/foo | _filter_od
118 # Now update 2 blocks of data at offset 0. The extent at this
119 # position is a clone of the extent at 6th block. Check that
120 # writing to this offset doesn't change data at 6th block.
121 $XFS_IO_PROG -c "pwrite -S 0xff -b $((2 * $BLOCK_SIZE)) 0 $((2 * $BLOCK_SIZE))" \
122 $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
123 od -t x1 $SCRATCH_MNT/foo | _filter_od
125 # Check that after defragmenting the file and re-mounting, the file
126 # content remains exactly the same as before.
127 _run_btrfs_util_prog filesystem defragment $SCRATCH_MNT/foo
129 od -t x1 $SCRATCH_MNT/foo | _filter_od
131 # Verify that there are no consistency errors.
135 # For any of the tests below, regardless of cow/nodatacow/compression, the
136 # results as observed by an application/user should be exactly the same.
138 echo "Testing with a cow file (default)"
139 test_btrfs_clone_same_file
143 echo "Testing with a nocow file (-O nodatacow)"
144 test_btrfs_clone_same_file "nodatacow"
148 echo "Testing with a cow file and lzo compression"
149 test_btrfs_clone_same_file "compress-force=lzo"
153 echo "Testing with a cow file and zlib compression"
154 test_btrfs_clone_same_file "compress-force=zlib"
158 echo "Testing with a nocow file and lzo compression"
159 test_btrfs_clone_same_file "nodatacow,compress-force=lzo"
163 echo "Testing with a nocow file and zlib compression"
164 test_btrfs_clone_same_file "nodatacow,compress-force=zlib"