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 seqres=$RESULT_DIR/$seq
13 echo "QA output created by $seq"
16 status=1 # failure is the default!
17 trap "_cleanup; exit \$status" 0 1 2 3 15
24 # get standard environment, filters and checks
28 # real QA test starts here
36 test_btrfs_clone_same_file()
43 _scratch_mkfs >/dev/null 2>&1
44 _scratch_mount $MOUNT_OPTIONS
46 BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
48 EXTENT_SIZE=$((2 * $BLOCK_SIZE))
50 # Create a file with 5 extents, 4 extents of 2 blocks each and 1 extent
53 $XFS_IO_PROG -f -c "pwrite -S 0x01 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
54 | _filter_xfs_io_blocks_modified
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
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
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
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
78 # Initial file content.
79 od -t x1 $SCRATCH_MNT/foo | _filter_od
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
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
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
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
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
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
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
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
137 od -t x1 $SCRATCH_MNT/foo | _filter_od
139 # Verify that there are no consistency errors.
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.
146 echo "Testing with a cow file (default)"
147 test_btrfs_clone_same_file
151 echo "Testing with a nocow file (-O nodatacow)"
152 test_btrfs_clone_same_file "nodatacow"
156 echo "Testing with a cow file and lzo compression"
157 test_btrfs_clone_same_file "compress-force=lzo"
161 echo "Testing with a cow file and zlib compression"
162 test_btrfs_clone_same_file "compress-force=zlib"
166 echo "Testing with a nocow file and lzo compression"
167 test_btrfs_clone_same_file "nodatacow,compress-force=lzo"
171 echo "Testing with a nocow file and zlib compression"
172 test_btrfs_clone_same_file "nodatacow,compress-force=zlib"