From: Ernest Borowski btrfs_setsize() calls truncate_setsize() before btrfs_truncate(). btrfs_truncate() then writes back and waits for the ordered extents at and past the new EOF, whose completion calls btrfs_inode_safe_disk_i_size_write(inode, 0) and lowers disk_i_size to the new i_size. If btrfs_start_transaction() then fails with -EDQUOT near a qgroup limit, no extent items are dropped, yet btrfs_setsize() resets i_size to the lowered disk_i_size. The inode is left with extent items past EOF and no BTRFS_INODE_PREALLOC flag. btrfs_set_extent_delalloc() and btrfs_dirty_folio() assume such an inode has no extents past EOF and mark writes past EOF with EXTENT_DELALLOC_NEW without searching the tree. Since commit 2766ff61762c ("btrfs: update the number of bytes used by an inode atomically"), buffered COW completion adds EXTENT_DELALLOC_NEW bytes to nbytes without subtracting the extents it drops, so "btrfs check" reports "nbytes wrong" (seen in 11 of 360 btrfs/232 runs). Like unlink, truncate frees space and should not fail on qgroup limits. Start the truncate transactions with btrfs_start_transaction_fallback_global_rsv(), which skips qgroup enforcement and falls back to the global reserve. Rarer failures (an ordered extent I/O error or an exhausted global reserve) can still leave extent items past EOF. Fixes: 2766ff61762c ("btrfs: update the number of bytes used by an inode atomically") Cc: stable@vger.kernel.org # 5.11+ Signed-off-by: Ernest Borowski --- fs/btrfs/inode.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 906b1cf3860..42a6d3ce06b 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -7877,8 +7877,14 @@ static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback) /* * 1 for the truncate slack space * 1 for updating the inode. + * + * Like unlink, a truncate frees space, so do not enforce qgroup limits + * and use the global reserve if needed. Failing here would leave the + * file extent items past the new size in place, while btrfs_setsize() + * resets i_size to a disk_i_size that the ordered extents we waited + * for may have lowered to the new size. */ - trans = btrfs_start_transaction(root, 2); + trans = btrfs_start_transaction_fallback_global_rsv(root, 2); if (IS_ERR(trans)) { ret = PTR_ERR(trans); goto out; @@ -7928,7 +7934,7 @@ static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback) btrfs_end_transaction(trans); btrfs_btree_balance_dirty(fs_info); - trans = btrfs_start_transaction(root, 2); + trans = btrfs_start_transaction_fallback_global_rsv(root, 2); if (IS_ERR(trans)) { ret = PTR_ERR(trans); trans = NULL; @@ -7963,7 +7969,7 @@ static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback) inode->vfs_inode.i_size, (u64)-1); if (ret) goto out; - trans = btrfs_start_transaction(root, 1); + trans = btrfs_start_transaction_fallback_global_rsv(root, 1); if (IS_ERR(trans)) { ret = PTR_ERR(trans); goto out; -- 2.56.1