xfs: ensure f_bfree returned by statfs() is non-negative
authorZheng Bin <zhengbin13@huawei.com>
Tue, 12 May 2020 18:48:35 +0000 (11:48 -0700)
committerDarrick J. Wong <darrick.wong@oracle.com>
Wed, 13 May 2020 22:32:45 +0000 (15:32 -0700)
Construct an img like this:

dd if=/dev/zero of=xfs.img bs=1M count=20
mkfs.xfs -d agcount=1 xfs.img
xfs_db -x xfs.img
sb 0
write fdblocks 0
agf 0
write freeblks 0
write longest 0
quit

mount it, df -h /mnt(xfs mount point), will show this:
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0       17M  -64Z  -32K 100% /mnt

Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Zheng Bin <zhengbin13@huawei.com>
Reviewed-by: Brian Foster <bfoster@redhat.com>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
fs/xfs/xfs_super.c

index e80bd2c4c279e30cd97bfba7078d2843aa0cd83b..aae469f73efeb7632085ab3965e9eb68b87bdd72 100644 (file)
@@ -807,7 +807,8 @@ xfs_fs_statfs(
        statp->f_blocks = sbp->sb_dblocks - lsize;
        spin_unlock(&mp->m_sb_lock);
 
-       statp->f_bfree = fdblocks - mp->m_alloc_set_aside;
+       /* make sure statp->f_bfree does not underflow */
+       statp->f_bfree = max_t(int64_t, fdblocks - mp->m_alloc_set_aside, 0);
        statp->f_bavail = statp->f_bfree;
 
        fakeinos = XFS_FSB_TO_INO(mp, statp->f_bfree);