Skip to content

Commit 5a1e9bf

Browse files
josefbacikgregkh
authored andcommitted
btrfs: only free reserved extent if we didn't insert it
commit 49940bd upstream. 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]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent a746cfd commit 5a1e9bf

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
@@ -2945,6 +2945,7 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
29452945
bool truncated = false;
29462946
bool range_locked = false;
29472947
bool clear_new_delalloc_bytes = false;
2948+
bool clear_reserved_extent = true;
29482949

29492950
if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
29502951
!test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
@@ -3048,10 +3049,12 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
30483049
logical_len, logical_len,
30493050
compress_type, 0, 0,
30503051
BTRFS_FILE_EXTENT_REG);
3051-
if (!ret)
3052+
if (!ret) {
3053+
clear_reserved_extent = false;
30523054
btrfs_release_delalloc_bytes(fs_info,
30533055
ordered_extent->start,
30543056
ordered_extent->disk_len);
3057+
}
30553058
}
30563059
unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
30573060
ordered_extent->file_offset, ordered_extent->len,
@@ -3112,8 +3115,13 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
31123115
* wrong we need to return the space for this ordered extent
31133116
* back to the allocator. We only free the extent in the
31143117
* truncated case if we didn't write out the extent at all.
3118+
*
3119+
* If we made it past insert_reserved_file_extent before we
3120+
* errored out then we don't need to do this as the accounting
3121+
* has already been done.
31153122
*/
31163123
if ((ret || !logical_len) &&
3124+
clear_reserved_extent &&
31173125
!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
31183126
!test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
31193127
btrfs_free_reserved_extent(fs_info,

0 commit comments

Comments
 (0)