2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
7 # Test that a file fsync works after punching a hole for the same file range
8 # multiple times and that after log/journal replay the file's content is
11 # This test is motivated by a bug found in btrfs.
14 _begin_fstest auto quick prealloc metadata punch log
16 # Override the default cleanup function.
24 # Import common functions.
29 # real QA test starts here
32 _require_xfs_io_command "fpunch"
33 _require_xfs_io_command "fiemap"
34 _require_dm_target flakey
36 _scratch_mkfs >>$seqres.full 2>&1
37 _require_metadata_journaling $SCRATCH_DEV
41 BLOCK_SIZE=$(_get_file_block_size $SCRATCH_MNT)
43 # Create out test file with some data and then fsync it.
44 # We do the fsync only to make sure the last fsync we do in this test triggers
45 # the fast code path of btrfs' fsync implementation, a condition necessary to
46 # trigger the bug btrfs had.
47 $XFS_IO_PROG -f -c "pwrite -S 0xaa 0K $(($BLOCK_SIZE * 32))" \
49 $SCRATCH_MNT/foobar | _filter_xfs_io_blocks_modified
51 # Now punch a hole against the range [96K, 128K[.
52 $XFS_IO_PROG -c "fpunch $(($BLOCK_SIZE * 24)) $(($BLOCK_SIZE * 8))" $SCRATCH_MNT/foobar
54 # Punch another hole against a range that overlaps the previous range and ends
56 $XFS_IO_PROG -c "fpunch $(($BLOCK_SIZE * 16)) $(($BLOCK_SIZE * 32))" $SCRATCH_MNT/foobar
58 # Punch another hole against a range that overlaps the first range ([96K, 128K[)
60 $XFS_IO_PROG -c "fpunch $(($BLOCK_SIZE * 8)) $(($BLOCK_SIZE * 24))" $SCRATCH_MNT/foobar
62 # Fsync our file. We want to verify that, after a power failure and mounting the
63 # filesystem again, the file content reflects all the hole punch operations.
64 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foobar
66 echo "File digest before power failure:"
67 od -t x1 $SCRATCH_MNT/foobar | _filter_od
69 echo "Fiemap before power failure:"
70 $XFS_IO_PROG -c "fiemap -v" $SCRATCH_MNT/foobar | _filter_fiemap $BLOCK_SIZE
72 _flakey_drop_and_remount
74 echo "File digest after log replay:"
75 # Must match the same digest we got before the power failure.
76 od -t x1 $SCRATCH_MNT/foobar | _filter_od
78 echo "Fiemap after log replay:"
79 # Must match the same extent listing we got before the power failure.
80 $XFS_IO_PROG -c "fiemap -v" $SCRATCH_MNT/foobar | _filter_fiemap $BLOCK_SIZE