btrfs: Verify falloc on multiple holes won't leak qgroup reserved data space
[xfstests-dev.git] / tests / btrfs / 026
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FSQA Test No. 026
6 #
7 # Test that doing a direct IO write against a file range that contains one
8 # prealloc extent and one compressed extent works correctly.
9 #
10 seq=`basename $0`
11 seqres=$RESULT_DIR/$seq
12 echo "QA output created by $seq"
13 tmp=/tmp/$$
14 status=1        # failure is the default!
15 trap "_cleanup; exit \$status" 0 1 2 3 15
16
17 _cleanup()
18 {
19         cd /
20         rm -f $tmp.*
21 }
22
23 # get standard environment, filters and checks
24 . ./common/rc
25 . ./common/filter
26
27 # real QA test starts here
28 _supported_fs btrfs
29 _supported_os Linux
30 _require_scratch
31 _require_xfs_io_command "falloc"
32
33 rm -f $seqres.full
34
35 _scratch_mkfs >>$seqres.full 2>&1
36 _scratch_mount "-o compress"
37
38 # Create a compressed extent covering the range [700K, 800K[.
39 $XFS_IO_PROG -f -s -c "pwrite -S 0xaa -b 100K 700K 100K" $SCRATCH_MNT/foo \
40         | _filter_xfs_io
41
42 # Create prealloc extent covering the range [600K, 700K[.
43 $XFS_IO_PROG -c "falloc 600K 100K" $SCRATCH_MNT/foo
44
45 # Write 80K of data to the range [640K, 720K[ using direct IO. This range covers
46 # both the prealloc extent and the compressed extent. Because there's a
47 # compressed extent in the range we are writing to, the DIO write code path ends
48 # up only writing the first 60k of data, which goes to the prealloc extent, and
49 # then falls back to buffered IO for writing the remaining 20K of data - because
50 # that remaining data maps to a file range containing a compressed extent.
51 # When falling back to buffered IO, we used to trigger an assertion when
52 # releasing reserved space due to bad accounting of the inode's outstanding
53 # extents counter, which was set to 1 but we ended up decrementing it by 1 twice,
54 # once through the ordered extent for the 60K of data we wrote using direct IO,
55 # and once through the main direct IO handler (inode.cbtrfs_direct_IO()) because
56 # the direct IO write wrote less than 80K of data (60K).
57 $XFS_IO_PROG -d -c "pwrite -S 0xbb -b 80K 640K 80K" $SCRATCH_MNT/foo \
58         | _filter_xfs_io
59
60 # Now similar test as above but for very large write operations. This triggers
61 # special cases for an inode's outstanding extents accounting, as internally
62 # btrfs logically splits extents into 128Mb units.
63 $XFS_IO_PROG -f -s \
64         -c "pwrite -S 0xaa -b 128M 258M 128M" \
65         -c "falloc 0 258M" \
66         $SCRATCH_MNT/bar | _filter_xfs_io
67 $XFS_IO_PROG -d -c "pwrite -S 0xbb -b 256M 3M 256M" $SCRATCH_MNT/bar \
68         | _filter_xfs_io
69
70 # Now verify the file contents are correct and that they are the same even after
71 # unmounting and mounting the fs again (or evicting the page cache).
72 #
73 # For file foo, all bytes in the range [0, 640K[ must have a value of 0x00, all
74 # bytes in the range [640K, 720K[ must have a value of 0xbb and all bytes in the
75 # range [720K, 800K[ must have a value of 0xaa.
76 #
77 # For file bar, all bytes in the range [0, 3M[ must havea value of 0x00, all
78 # bytes in the range [3M, 259M[ must have a value of 0xbb and all bytes in the
79 # range [259M, 386M[ must have a value of 0xaa.
80 #
81 echo "File digests before remounting the file system:"
82 md5sum $SCRATCH_MNT/foo | _filter_scratch
83 md5sum $SCRATCH_MNT/bar | _filter_scratch
84 _scratch_cycle_mount
85 echo "File digests after remounting the file system:"
86 md5sum $SCRATCH_MNT/foo | _filter_scratch
87 md5sum $SCRATCH_MNT/bar | _filter_scratch
88
89 status=0
90 exit