2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2021 SUSE Linux Products GmbH. All Rights Reserved.
7 # Test that if we write to a range of a NOCOW file that has allocated extents
8 # and there is not enough available free space for allocating new data extents,
9 # the write succeeds. Test for direct IO and buffered IO writes.
10 # The patch that fixes the direct IO case has the following subject:
12 # "btrfs: fix ENOSPC failure when attempting direct IO write into NOCOW range"
15 _begin_fstest auto quick enospc
23 # Import common functions.
26 # real QA test starts here
33 # Use a small fixed size filesystem so that it's quick to fill it up.
34 # Make sure the fs size is > 256M, so that the mixed block groups feature is
35 # not enabled by _scatch_mkfs_sized(), because we later want to not have more
36 # space available for allocating data extents but still have enough metadata
37 # space free for the file writes.
38 fs_size=$((1024 * 1024 * 1024)) # 1G
39 _scratch_mkfs_sized $fs_size >>$seqres.full 2>&1
42 # Create our test file with the NOCOW attribute set.
43 touch $SCRATCH_MNT/foobar
44 $CHATTR_PROG +C $SCRATCH_MNT/foobar
46 # Now fill in all unallocated space with data for our test file.
47 # This will allocate a data block group that will be full and leave no (or a
48 # very small amount of) unallocated space in the device, so that it will not be
49 # possible to allocate a new block group later.
50 echo "Creating test file with initial data..."
51 $XFS_IO_PROG -c "pwrite -S 0xab -b 1M 0 900M" $SCRATCH_MNT/foobar | _filter_xfs_io
53 # Now try a direct IO write against file range [0, 10M[.
54 # This should succeed since this is a NOCOW file and an extent for the range was
55 # previously allocated.
56 echo "Trying direct IO write over allocated space..."
57 $XFS_IO_PROG -d -c "pwrite -S 0xcd -b 10M 0 10M" $SCRATCH_MNT/foobar | _filter_xfs_io
59 # Now try a buffered IO write against file range [10M, 20M[.
60 # This should also succeed since this is a NOCOW file and an extent for the range
61 # was previously allocated.
62 echo "Trying buffered IO write over allocated space..."
63 $XFS_IO_PROG -c "pwrite -S 0xef -b 10M 10M 10M" $SCRATCH_MNT/foobar | _filter_xfs_io
65 # Unmount and mount again the filesystem to clear any data from our file from the
69 # Now read the file and verify that all the writes we did before were durably
71 echo "File data after mounting again the filesystem:"
72 od -A d -t x1 $SCRATCH_MNT/foobar