2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2014 Filipe Manana. All Rights Reserved.
5 # FS QA Test No. btrfs/056
7 # Regression test for btrfs ioctl clone operation + fsync + log recovery.
8 # The issue was that doing an fsync after cloning into a file didn't gave any
9 # persistence guarantees as it should. What happened was that the in memory
10 # metadata (extent maps) weren't updated, which made the fsync code not able
11 # to detect that file data has been changed.
13 # This issue is fixed by the following linux kernel btrfs patch:
15 # Btrfs: make fsync work after cloning into a file
18 _begin_fstest auto quick clone log
20 # Override the default cleanup function.
27 # Import common functions.
31 # real QA test starts here
35 _require_btrfs_fs_feature "no_holes"
36 _require_btrfs_mkfs_feature "no-holes"
37 _require_dm_target flakey
39 test_btrfs_clone_fsync_log_recover()
41 _scratch_mkfs "$1" >/dev/null 2>&1
43 SAVE_MOUNT_OPTIONS="$MOUNT_OPTIONS"
44 MOUNT_OPTIONS="$MOUNT_OPTIONS $2"
47 BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
49 EXTENT_SIZE=$((2 * $BLOCK_SIZE))
51 # Create a file with 4 extents and 1 hole, all with a size of
53 # The hole is in the block range [4, 5].
54 $XFS_IO_PROG -s -f -c "pwrite -S 0x01 -b $EXTENT_SIZE 0 $EXTENT_SIZE" \
55 -c "pwrite -S 0x02 -b $EXTENT_SIZE $((2 * $BLOCK_SIZE)) $EXTENT_SIZE" \
56 -c "pwrite -S 0x04 -b $EXTENT_SIZE $((6 * $BLOCK_SIZE)) $EXTENT_SIZE" \
57 -c "pwrite -S 0x05 -b $EXTENT_SIZE $((8 * $BLOCK_SIZE)) $EXTENT_SIZE" \
58 $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
60 # Clone destination file, 1 extent of 24 blocks.
61 $XFS_IO_PROG -f -c "pwrite -S 0xff -b $((24 * $BLOCK_SIZE)) 0 $((24 * $BLOCK_SIZE))" \
62 -c "fsync" $SCRATCH_MNT/bar | _filter_xfs_io_blocks_modified
64 # Clone second half of the 2nd extent, the 2 block hole, the 3rd extent
65 # and the first half of the 4th extent into file bar.
66 $CLONER_PROG -s $((3 * $BLOCK_SIZE)) -d 0 -l $((6 * $BLOCK_SIZE)) \
67 $SCRATCH_MNT/foo $SCRATCH_MNT/bar
68 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/bar
70 # Test small files too consisting of 1 inline extent
71 EXTENT_SIZE=$(($BLOCK_SIZE - 48))
72 $XFS_IO_PROG -f -c "pwrite -S 0x00 -b $EXTENT_SIZE 0 $EXTENT_SIZE" -c "fsync" \
73 $SCRATCH_MNT/foo2 | _filter_xfs_io_blocks_modified
75 EXTENT_SIZE=$(($BLOCK_SIZE - 1048))
76 $XFS_IO_PROG -f -c "pwrite -S 0xcc -b $EXTENT_SIZE 0 $EXTENT_SIZE" -c "fsync" \
77 $SCRATCH_MNT/bar2 | _filter_xfs_io_blocks_modified
79 # Clone the entire foo2 file into bar2, overwriting all data in bar2
80 # and increasing its size.
81 EXTENT_SIZE=$(($BLOCK_SIZE - 48))
82 $CLONER_PROG -s 0 -d 0 -l $EXTENT_SIZE $SCRATCH_MNT/foo2 $SCRATCH_MNT/bar2
83 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/bar2
85 _flakey_drop_and_remount yes
87 # Verify the cloned range was persisted by fsync and the log recovery
88 # code did its work well.
89 echo "Verifying file bar content"
90 od -t x1 $SCRATCH_MNT/bar | _filter_od
92 echo "Verifying file bar2 content"
93 od -t x1 $SCRATCH_MNT/bar2 | _filter_od
97 # Verify that there are no consistency errors.
98 _check_scratch_fs $FLAKEY_DEV
101 MOUNT_OPTIONS="$SAVE_MOUNT_OPTIONS"
104 # Regardless of the NO_HOLES feature being enabled or not, the test results
105 # should be exactly the same for both cases.
107 echo "Testing without the NO_HOLES feature"
108 # As of btrfs-progs 3.14.x, the no-holes feature isn't enabled by default.
109 # But explicitly disable it at mkfs time as it might be enabled by default
110 # in future versions.
111 test_btrfs_clone_fsync_log_recover "-O ^no-holes"
113 echo "Testing without the NO_HOLES feature and compression (lzo)"
114 test_btrfs_clone_fsync_log_recover "-O ^no-holes" "-o compress-force=lzo"
116 echo "Testing with the NO_HOLES feature enabled"
117 test_btrfs_clone_fsync_log_recover "-O no-holes"
119 echo "Testing with the NO_HOLES feature enabled and compression (lzo)"
120 test_btrfs_clone_fsync_log_recover "-O no-holes" "-o compress-force=lzo"