From: Ernest Borowski Since commit 2766ff61762c ("btrfs: update the number of bytes used by an inode atomically"), btrfs_drop_extents() returns the dropped bytes in drop_args.bytes_found instead of updating the inode's nbytes itself. When btrfs_drop_extents() hits -ENOSPC on its block reserve, btrfs_replace_file_extents() splits the work across several transactions (rounds), but only decrements nbytes when punching a hole or inserting an extent. If a reflink or dedupe source starts with a hole over many destination extents, rounds that end inside the hole insert nothing and never subtract their dropped bytes. stat() then reports too many blocks and "btrfs check" reports "nbytes wrong". Reflinking a source that is a hole except for its last 4 KiB over 3000 separate 4 KiB extents leaves nbytes 2801 blocks too high. Decrement nbytes also for rounds that drop extents without inserting one, and clear the round's count once it is accounted for, so the insertion after the loop only subtracts what its own round dropped. 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/file.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index 7dd2a7c9a47..fe57b9d0e40 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -2550,7 +2550,21 @@ int btrfs_replace_file_extents(struct btrfs_inode *inode, extent_info->data_len -= replace_len; extent_info->data_offset += replace_len; extent_info->file_offset += replace_len; + } else if (extent_info) { + /* + * We only dropped extents before the start of the new + * extent (a clone whose source range starts with a hole) + * and inserted nothing, so decrement the inode's byte + * count here, as it would not be done for this round. + */ + btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found); } + /* + * The bytes dropped in this round are accounted for, so the + * insertion after the loop must not decrement them again if + * this was the last round. + */ + drop_args.bytes_found = 0; /* * We are releasing our handle on the transaction, balance the -- 2.56.1