Skip to content

Commit d3914d6

Browse files
fdmananakdave
authored andcommitted
btrfs: fix invalid data space release when truncating block in NOCOW mode
If when truncating a block we fail to reserve data space and then we proceed anyway because we can do a NOCOW write, if we later get an error when trying to get the folio from the inode's mapping, we end up releasing data space that we haven't reserved, screwing up the bytes_may_use counter from the data space_info, eventually resulting in an underflow when all other reservations done by other tasks are released, if any, or right away if there are no other reservations at the moment. This is because when we get an error when trying to grab the block's folio we call btrfs_delalloc_release_space(), which releases metadata (which we have reserved) and data (which we haven't reserved). Fix this by calling btrfs_delalloc_release_space() only if we did reserve data space, that is, if we aren't falling back to NOCOW, meaning the local variable @only_release_metadata has a false value, otherwise release only metadata by calling btrfs_delalloc_release_metadata(). Fixes: 6d4572a ("btrfs: allow btrfs_truncate_block() to fallback to nocow for data space reservation") Reviewed-by: Qu Wenruo <[email protected]> Signed-off-by: Filipe Manana <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent c16b984 commit d3914d6

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

fs/btrfs/inode.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4931,8 +4931,11 @@ int btrfs_truncate_block(struct btrfs_inode *inode, u64 offset, u64 start, u64 e
49314931
folio = __filemap_get_folio(mapping, index,
49324932
FGP_LOCK | FGP_ACCESSED | FGP_CREAT, mask);
49334933
if (IS_ERR(folio)) {
4934-
btrfs_delalloc_release_space(inode, data_reserved, block_start,
4935-
blocksize, true);
4934+
if (only_release_metadata)
4935+
btrfs_delalloc_release_metadata(inode, blocksize, true);
4936+
else
4937+
btrfs_delalloc_release_space(inode, data_reserved,
4938+
block_start, blocksize, true);
49364939
btrfs_delalloc_release_extents(inode, blocksize);
49374940
ret = -ENOMEM;
49384941
goto out;

0 commit comments

Comments
 (0)