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
35 test_btrfs_clone_same_file()
42 _scratch_mkfs >/dev/null 2>&1
43 _scratch_mount $MOUNT_OPTIONS
45 BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
47 EXTENT_SIZE=$((2 * $BLOCK_SIZE))
49 # Create a file with 5 extents, 4 extents of 2 blocks each and 1 extent
52 $XFS_IO_PROG -f -c "pwrite -S 0x01 -b $EXTENT_SIZE $OFFSET $EXTENT_SIZE" $SCRATCH_MNT/foo \
53 | _filter_xfs_io_blocks_modified
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
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
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
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
77 # Initial file content.
78 od -t x1 $SCRATCH_MNT/foo | _filter_od
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
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
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
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
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
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
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
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
136 od -t x1 $SCRATCH_MNT/foo | _filter_od
138 # Verify that there are no consistency errors.
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.
145 echo "Testing with a cow file (default)"
146 test_btrfs_clone_same_file
150 echo "Testing with a nocow file (-O nodatacow)"
151 test_btrfs_clone_same_file "nodatacow"
155 echo "Testing with a cow file and lzo compression"
156 test_btrfs_clone_same_file "compress-force=lzo"
160 echo "Testing with a cow file and zlib compression"
161 test_btrfs_clone_same_file "compress-force=zlib"
165 echo "Testing with a nocow file and lzo compression"
166 test_btrfs_clone_same_file "nodatacow,compress-force=lzo"
170 echo "Testing with a nocow file and zlib compression"
171 test_btrfs_clone_same_file "nodatacow,compress-force=zlib"