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 seqres=$RESULT_DIR/$seq
14 echo "QA output created by $seq"
16 status=1 # failure is the default!
17 trap "_cleanup; exit \$status" 0 1 2 3 15
26 # get standard environment, filters and checks
31 # real QA test starts here
34 _require_xfs_io_command "falloc" "-k"
35 # fiemap needed by _count_extents()
36 _require_xfs_io_command "fiemap"
37 _require_btrfs_fs_feature "no_holes"
38 _require_btrfs_mkfs_feature "no-holes"
39 _require_dm_target flakey
45 # Create our test file with 2 consecutive prealloc extents, each with a size
46 # of 128Kb, and covering the range from 0 to 256Kb, with a file size of 0.
47 # Then fsync the file to record both extents in a log tree.
48 $XFS_IO_PROG -f -c "falloc -k 0 128K" $SCRATCH_MNT/foo
49 $XFS_IO_PROG -c "falloc -k 128K 128K" $SCRATCH_MNT/foo
50 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
52 # Now do a redudant extent allocation for the range from 0 to 64Kb. This will
53 # merely increase the file size from 0 to 64Kb. Instead we could also do a
54 # truncate to set the file size to 64Kb.
55 $XFS_IO_PROG -c "falloc 0 64K" $SCRATCH_MNT/foo
57 # Fsync the file, so we update the log with the new file size. Here btrfs
58 # used to incorrectly set the number of bytes to 64Kb for the prealloc extent
59 # that covers the file range from 0 to 128Kb.
60 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
62 # Now set the file size to 256K with a truncate and then fsync the file. We
63 # want to verify the log tree doesn't end up with an implicit hole for the
64 # file range from 64Kb to 128Kb. That would lead to an implicit hole after
65 # replaying the log and losing part of the prealloc extent, so a future write
66 # to anywhere in the file range from 64Kb to 128Kb would result in allocating
67 # a new extent and not use the extent previously allocated with fallocate().
68 $XFS_IO_PROG -c "truncate 256K" -c "fsync" $SCRATCH_MNT/foo
70 # Simulate a power failure and then mount again the filesystem to replay the
72 _flakey_drop_and_remount
74 # Unmount the filesystem and run 'btrfs check'/fsck to verify that we don't
75 # have a missing hole for the file range from 64K to 128K.
77 _check_scratch_fs $FLAKEY_DEV
81 # Now write to the file range from 0 to 128K. After this we should still have
82 # rwo extents in our file, corresponding to the 2 extents we allocated before
83 # using fallocate(). In particular writing to the file range from 64Kb to
84 # 128Kb should not have allocated a new extent.
85 $XFS_IO_PROG -c "pwrite -S 0xab 0 128K" $SCRATCH_MNT/foo | _filter_xfs_io
86 echo "File extent count after write: $(_count_extents $SCRATCH_MNT/foo)"
89 _scratch_mkfs -O ^no-holes >>$seqres.full 2>&1
90 _require_metadata_journaling $SCRATCH_DEV
94 echo "Testing without NO_HOLES feature"
100 _scratch_mkfs -O no-holes >>$seqres.full 2>&1
101 _require_metadata_journaling $SCRATCH_DEV
106 echo "Testing with the NO_HOLES feature"