Skip to content

Commit 857cc2f

Browse files
Josef BacikChris Mason
authored andcommitted
Btrfs: free reserved space on error in a few places
While trying to track down a reserved space leak I noticed a few places where we won't properly clean up reserved space if we have an error, this patch fixes those up. Thanks, Signed-off-by: Josef Bacik <[email protected]> Signed-off-by: Chris Mason <[email protected]>
1 parent 0be5dc6 commit 857cc2f

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

fs/btrfs/extent-tree.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,8 +2234,12 @@ static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
22342234
{
22352235
int ret = 0;
22362236

2237-
if (trans->aborted)
2237+
if (trans->aborted) {
2238+
if (insert_reserved)
2239+
btrfs_pin_extent(root, node->bytenr,
2240+
node->num_bytes, 1);
22382241
return 0;
2242+
}
22392243

22402244
if (btrfs_delayed_ref_is_head(node)) {
22412245
struct btrfs_delayed_ref_head *head;
@@ -2411,6 +2415,14 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
24112415
btrfs_free_delayed_extent_op(extent_op);
24122416

24132417
if (ret) {
2418+
/*
2419+
* Need to reset must_insert_reserved if
2420+
* there was an error so the abort stuff
2421+
* can cleanup the reserved space
2422+
* properly.
2423+
*/
2424+
if (must_insert_reserved)
2425+
locked_ref->must_insert_reserved = 1;
24142426
btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
24152427
spin_lock(&delayed_refs->lock);
24162428
btrfs_delayed_ref_unlock(locked_ref);
@@ -6731,13 +6743,18 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
67316743
size += sizeof(*block_info);
67326744

67336745
path = btrfs_alloc_path();
6734-
if (!path)
6746+
if (!path) {
6747+
btrfs_free_and_pin_reserved_extent(root, ins->objectid,
6748+
root->leafsize);
67356749
return -ENOMEM;
6750+
}
67366751

67376752
path->leave_spinning = 1;
67386753
ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
67396754
ins, size);
67406755
if (ret) {
6756+
btrfs_free_and_pin_reserved_extent(root, ins->objectid,
6757+
root->leafsize);
67416758
btrfs_free_path(path);
67426759
return ret;
67436760
}

fs/btrfs/inode.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8498,6 +8498,8 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
84988498
ins.offset, 0, 0, 0,
84998499
BTRFS_FILE_EXTENT_PREALLOC);
85008500
if (ret) {
8501+
btrfs_free_reserved_extent(root, ins.objectid,
8502+
ins.offset);
85018503
btrfs_abort_transaction(trans, root, ret);
85028504
if (own_trans)
85038505
btrfs_end_transaction(trans, root);

0 commit comments

Comments
 (0)