Skip to content

Commit 49940bd

Browse files
josefbacikkdave
authored andcommitted
btrfs: only free reserved extent if we didn't insert it
When we insert the file extent once the ordered extent completes we free the reserved extent reservation as it'll have been migrated to the bytes_used counter. However if we error out after this step we'll still clear the reserved extent reservation, resulting in a negative accounting of the reserved bytes for the block group and space info. Fix this by only doing the free if we didn't successfully insert a file extent for this extent. CC: [email protected] # 4.14+ Reviewed-by: Omar Sandoval <[email protected]> Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: Josef Bacik <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent fb5c39d commit 49940bd

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

fs/btrfs/inode.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2941,6 +2941,7 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
29412941
bool truncated = false;
29422942
bool range_locked = false;
29432943
bool clear_new_delalloc_bytes = false;
2944+
bool clear_reserved_extent = true;
29442945

29452946
if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
29462947
!test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
@@ -3044,10 +3045,12 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
30443045
logical_len, logical_len,
30453046
compress_type, 0, 0,
30463047
BTRFS_FILE_EXTENT_REG);
3047-
if (!ret)
3048+
if (!ret) {
3049+
clear_reserved_extent = false;
30483050
btrfs_release_delalloc_bytes(fs_info,
30493051
ordered_extent->start,
30503052
ordered_extent->disk_len);
3053+
}
30513054
}
30523055
unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
30533056
ordered_extent->file_offset, ordered_extent->len,
@@ -3108,8 +3111,13 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
31083111
* wrong we need to return the space for this ordered extent
31093112
* back to the allocator. We only free the extent in the
31103113
* truncated case if we didn't write out the extent at all.
3114+
*
3115+
* If we made it past insert_reserved_file_extent before we
3116+
* errored out then we don't need to do this as the accounting
3117+
* has already been done.
31113118
*/
31123119
if ((ret || !logical_len) &&
3120+
clear_reserved_extent &&
31133121
!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
31143122
!test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
31153123
btrfs_free_reserved_extent(fs_info,

0 commit comments

Comments
 (0)