* [PATCH] xfs: test that statfs does not report negative free blocks
@ 2026-11-12 15:49 Hengyu Liang
2026-11-12 15:53 ` Christoph Hellwig
0 siblings, 1 reply; 4+ messages in thread
From: Hengyu Liang @ 2026-11-12 15:49 UTC (permalink / raw)
To: fstests
Cc: Zorro Lang, Darrick J. Wong, Christoph Hellwig, Carlos Maiolino,
linux-xfs
Add a regression test for the f_bfree underflow in statfs().
Since kernel commit dd324cb79e54 ("xfs: refactor xfs_fs_statfs"), when
the free block counter is below the number of blocks that are not
available for allocation, statfs() reports f_bfree and f_bavail close
to 2^64, and df prints negative sizes.
The test moves all free blocks into the reserve pool with the resblks
command, then deletes every other file of many single-block files. The
freed blocks refill the reserve pool first, while the free space btrees
grow.
Without the fix, f_bfree is reported as 2^64 - 6 on a filesystem with
4k blocks.
Link: https://lore.kernel.org/linux-xfs/20261112102018.3922571-1-hengyul@cs.unc.edu/
Signed-off-by: Hengyu Liang <hengyul@cs.unc.edu>
---
Tested in a VM on xfs for-next 48326a2382d8 (v7.4-rc2) with mkfs.xfs
6.18 defaults, "-m rmapbt=0,reflink=0" and "-b size=1024": the test
fails without the kernel fix and passes with it.
tests/xfs/842 | 57 +++++++++++++++++++++++++++++++++++++++++++++++
tests/xfs/842.out | 2 ++
2 files changed, 59 insertions(+)
create mode 100755 tests/xfs/842
create mode 100644 tests/xfs/842.out
diff --git a/tests/xfs/842 b/tests/xfs/842
new file mode 100755
index 00000000..c99dd4f3
--- /dev/null
+++ b/tests/xfs/842
@@ -0,0 +1,57 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 Hengyu Liang. All Rights Reserved.
+#
+# FS QA Test No. 842
+#
+# Make sure that statfs() does not report a negative or huge number of free
+# blocks when the free block counter drops below the blocks that are not
+# available for allocation.
+#
+# Move all free blocks into the reserve pool, then free many small extents.
+# The freed blocks refill the reserve pool first, so the free block counter
+# stays the same, while the free space btrees grow.
+#
+. ./common/preamble
+_begin_fstest auto quick
+
+_fixed_by_kernel_commit xxxxxxxxxxxx \
+ "xfs: fix f_bfree underflow in statfs()"
+
+_require_scratch
+_require_xfs_io_command "falloc"
+
+# Only the data device counters are checked.
+_disable_scratch_realtime
+
+_scratch_mkfs >> $seqres.full 2>&1
+_scratch_mount
+_require_xfs_scratch_non_zoned
+
+# A free space btree leaf holds about blksz / 8 records. Deleting every
+# other file of blksz / 2 single-block files adds blksz / 4 records, which
+# takes a few more leaf blocks.
+blksz=$(_get_block_size $SCRATCH_MNT)
+nr_files=$((blksz / 2))
+_require_fs_space $SCRATCH_MNT $((nr_files * blksz * 2 / 1024))
+
+for ((i = 0; i < nr_files; i++)); do
+ $XFS_IO_PROG -f -c "falloc 0 $blksz" $SCRATCH_MNT/s$i
+done
+
+# Ask for more blocks than the filesystem has, so that the reserve pool takes
+# all free blocks and stays short of its target.
+nr_blocks=$(stat -f -c '%b' $SCRATCH_MNT)
+$XFS_IO_PROG -x -c "resblks $((nr_blocks * 2))" $SCRATCH_MNT >> $seqres.full
+
+find $SCRATCH_MNT -name 's*[13579]' -delete
+_scratch_sync
+
+# Before the fix, f_bfree and f_bavail were 2^64 minus a few blocks. Some
+# versions of stat print that as a negative number.
+stat -f -c '%b %f %a' $SCRATCH_MNT | tee -a $seqres.full | $AWK_PROG '
+ $2 < 0 || $2 > $1 { print "f_bfree " $2 " out of range, f_blocks " $1 }
+ $3 < 0 || $3 > $1 { print "f_bavail " $3 " out of range, f_blocks " $1 }'
+
+echo "Silence is golden"
+_exit 0
diff --git a/tests/xfs/842.out b/tests/xfs/842.out
new file mode 100644
index 00000000..643d0528
--- /dev/null
+++ b/tests/xfs/842.out
@@ -0,0 +1,2 @@
+QA output created by 842
+Silence is golden
base-commit: 71641219a506ac62bfa231592394265ca812670e
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] xfs: test that statfs does not report negative free blocks
2026-11-12 15:49 [PATCH] xfs: test that statfs does not report negative free blocks Hengyu Liang
@ 2026-11-12 15:53 ` Christoph Hellwig
2026-11-12 17:33 ` Hengyu Liang
0 siblings, 1 reply; 4+ messages in thread
From: Christoph Hellwig @ 2026-11-12 15:53 UTC (permalink / raw)
To: Hengyu Liang
Cc: fstests, Zorro Lang, Darrick J. Wong, Christoph Hellwig,
Carlos Maiolino, linux-xfs
On Thu, Nov 12, 2026 at 10:49:17AM -0500, Hengyu Liang wrote:
> +# Only the data device counters are checked.
> +_disable_scratch_realtime
> +
> +_scratch_mkfs >> $seqres.full 2>&1
> +_scratch_mount
> +_require_xfs_scratch_non_zoned
Why do you need _require_xfs_scratch_non_zoned? If this test is always
working on the data volume due to disable_scratch_realtime above,
a zoned vs non-zoned RT volume shouldn't matter unless I'm missing
something.
Otherwise this looks good to me, thanks a lot!
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] xfs: test that statfs does not report negative free blocks
2026-11-12 15:53 ` Christoph Hellwig
@ 2026-11-12 17:33 ` Hengyu Liang
2026-11-12 17:40 ` Christoph Hellwig
0 siblings, 1 reply; 4+ messages in thread
From: Hengyu Liang @ 2026-11-12 17:33 UTC (permalink / raw)
To: Christoph Hellwig
Cc: fstests, Zorro Lang, Darrick J. Wong, Carlos Maiolino, linux-xfs
On Thu, Nov 12, 2026 at 04:53:31PM +0100, Christoph Hellwig wrote:
> On Thu, Nov 12, 2026 at 10:49:17AM -0500, Hengyu Liang wrote:
> > +# Only the data device counters are checked.
> > +_disable_scratch_realtime
> > +
> > +_scratch_mkfs >> $seqres.full 2>&1
> > +_scratch_mount
> > +_require_xfs_scratch_non_zoned
>
> Why do you need _require_xfs_scratch_non_zoned? If this test is always
> working on the data volume due to disable_scratch_realtime above,
> a zoned vs non-zoned RT volume shouldn't matter unless I'm missing
> something.
I copied it from xfs/306. _disable_scratch_realtime only unsets
SCRATCH_RTDEV and removes "-r zoned=1" from MKFS_OPTIONS. When
SCRATCH_DEV itself is a zoned device, mkfs still creates an internal
zoned RT section, and the root directory has rtinherit set, so the
files are created on the RT section. I tried this on a zoned null_blk
device without the check: all the fallocate calls fail with EOPNOTSUPP,
statfs on the mount point reports the RT counters, and the test fails
with and without the fix.
> Otherwise this looks good to me, thanks a lot!
Thanks for the review!
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] xfs: test that statfs does not report negative free blocks
2026-11-12 17:33 ` Hengyu Liang
@ 2026-11-12 17:40 ` Christoph Hellwig
0 siblings, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2026-11-12 17:40 UTC (permalink / raw)
To: Hengyu Liang
Cc: Christoph Hellwig, fstests, Zorro Lang, Darrick J. Wong,
Carlos Maiolino, linux-xfs
On Thu, Nov 12, 2026 at 12:33:03PM -0500, Hengyu Liang wrote:
> On Thu, Nov 12, 2026 at 04:53:31PM +0100, Christoph Hellwig wrote:
> > On Thu, Nov 12, 2026 at 10:49:17AM -0500, Hengyu Liang wrote:
> > > +# Only the data device counters are checked.
> > > +_disable_scratch_realtime
> > > +
> > > +_scratch_mkfs >> $seqres.full 2>&1
> > > +_scratch_mount
> > > +_require_xfs_scratch_non_zoned
> >
> > Why do you need _require_xfs_scratch_non_zoned? If this test is always
> > working on the data volume due to disable_scratch_realtime above,
> > a zoned vs non-zoned RT volume shouldn't matter unless I'm missing
> > something.
>
> I copied it from xfs/306. _disable_scratch_realtime only unsets
> SCRATCH_RTDEV and removes "-r zoned=1" from MKFS_OPTIONS. When
> SCRATCH_DEV itself is a zoned device, mkfs still creates an internal
> zoned RT section, and the root directory has rtinherit set, so the
> files are created on the RT section. I tried this on a zoned null_blk
> device without the check: all the fallocate calls fail with EOPNOTSUPP,
> statfs on the mount point reports the RT counters, and the test fails
> with and without the fix.
Oh, ok.
Looks good then:
Reviewed-by: Christoph Hellwig <hch@lst.de>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-12 17:40 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12 15:49 [PATCH] xfs: test that statfs does not report negative free blocks Hengyu Liang
2026-11-12 15:53 ` Christoph Hellwig
2026-11-12 17:33 ` Hengyu Liang
2026-11-12 17:40 ` Christoph Hellwig
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox