2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2020 SUSE Linux Products GmbH. All Rights Reserved.
7 # Test that if we fsync a file with prealloc extents that start before and
8 # after the file's size, we don't end up with missing parts of the extents
9 # and implicit file holes after a power failure. Test both without and with
10 # the NO_HOLES feature.
13 _begin_fstest auto quick log prealloc
15 # Override the default cleanup function.
23 # Import common functions.
27 # real QA test starts here
30 _require_xfs_io_command "falloc" "-k"
31 # fiemap needed by _count_extents()
32 _require_xfs_io_command "fiemap"
33 _require_btrfs_fs_feature "no_holes"
34 _require_btrfs_mkfs_feature "no-holes"
35 _require_dm_target flakey
39 # Create our test file with 2 consecutive prealloc extents, each with a size
40 # of 128Kb, and covering the range from 0 to 256Kb, with a file size of 0.
41 # Then fsync the file to record both extents in a log tree.
42 $XFS_IO_PROG -f -c "falloc -k 0 128K" $SCRATCH_MNT/foo
43 $XFS_IO_PROG -c "falloc -k 128K 128K" $SCRATCH_MNT/foo
44 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
46 # Now do a redudant extent allocation for the range from 0 to 64Kb. This will
47 # merely increase the file size from 0 to 64Kb. Instead we could also do a
48 # truncate to set the file size to 64Kb.
49 $XFS_IO_PROG -c "falloc 0 64K" $SCRATCH_MNT/foo
51 # Fsync the file, so we update the log with the new file size. Here btrfs
52 # used to incorrectly set the number of bytes to 64Kb for the prealloc extent
53 # that covers the file range from 0 to 128Kb.
54 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
56 # Now set the file size to 256K with a truncate and then fsync the file. We
57 # want to verify the log tree doesn't end up with an implicit hole for the
58 # file range from 64Kb to 128Kb. That would lead to an implicit hole after
59 # replaying the log and losing part of the prealloc extent, so a future write
60 # to anywhere in the file range from 64Kb to 128Kb would result in allocating
61 # a new extent and not use the extent previously allocated with fallocate().
62 $XFS_IO_PROG -c "truncate 256K" -c "fsync" $SCRATCH_MNT/foo
64 # Simulate a power failure and then mount again the filesystem to replay the
66 _flakey_drop_and_remount
68 # Unmount the filesystem and run 'btrfs check'/fsck to verify that we don't
69 # have a missing hole for the file range from 64K to 128K.
71 _check_scratch_fs $FLAKEY_DEV
75 # Now write to the file range from 0 to 128K. After this we should still have
76 # rwo extents in our file, corresponding to the 2 extents we allocated before
77 # using fallocate(). In particular writing to the file range from 64Kb to
78 # 128Kb should not have allocated a new extent.
79 $XFS_IO_PROG -c "pwrite -S 0xab 0 128K" $SCRATCH_MNT/foo | _filter_xfs_io
80 echo "File extent count after write: $(_count_extents $SCRATCH_MNT/foo)"
83 _scratch_mkfs -O ^no-holes >>$seqres.full 2>&1
84 _require_metadata_journaling $SCRATCH_DEV
88 echo "Testing without NO_HOLES feature"
94 _scratch_mkfs -O no-holes >>$seqres.full 2>&1
95 _require_metadata_journaling $SCRATCH_DEV
100 echo "Testing with the NO_HOLES feature"