Skip to content

Commit dcec59e

Browse files
committed
btrfs: use on-stack variable for block reserve in btrfs_evict_inode()
We can avoid potential memory allocation failure in btrfs_evict_inode() as the block reserve lifetime is limited to the scope of the function. This requires +48 bytes on stack. Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 508983c commit dcec59e

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

fs/btrfs/inode.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5435,7 +5435,7 @@ void btrfs_evict_inode(struct inode *inode)
54355435
struct btrfs_fs_info *fs_info;
54365436
struct btrfs_trans_handle *trans;
54375437
struct btrfs_root *root = BTRFS_I(inode)->root;
5438-
struct btrfs_block_rsv *rsv = NULL;
5438+
struct btrfs_block_rsv rsv;
54395439
int ret;
54405440

54415441
trace_btrfs_inode_evict(inode);
@@ -5483,11 +5483,9 @@ void btrfs_evict_inode(struct inode *inode)
54835483
*/
54845484
btrfs_kill_delayed_inode_items(BTRFS_I(inode));
54855485

5486-
rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
5487-
if (!rsv)
5488-
goto out;
5489-
rsv->size = btrfs_calc_metadata_size(fs_info, 1);
5490-
rsv->failfast = true;
5486+
btrfs_init_metadata_block_rsv(fs_info, &rsv, BTRFS_BLOCK_RSV_TEMP);
5487+
rsv.size = btrfs_calc_metadata_size(fs_info, 1);
5488+
rsv.failfast = true;
54915489

54925490
btrfs_i_size_write(BTRFS_I(inode), 0);
54935491

@@ -5499,11 +5497,11 @@ void btrfs_evict_inode(struct inode *inode)
54995497
.min_type = 0,
55005498
};
55015499

5502-
trans = evict_refill_and_join(root, rsv);
5500+
trans = evict_refill_and_join(root, &rsv);
55035501
if (IS_ERR(trans))
5504-
goto out;
5502+
goto out_release;
55055503

5506-
trans->block_rsv = rsv;
5504+
trans->block_rsv = &rsv;
55075505

55085506
ret = btrfs_truncate_inode_items(trans, root, &control);
55095507
trans->block_rsv = &fs_info->trans_block_rsv;
@@ -5515,7 +5513,7 @@ void btrfs_evict_inode(struct inode *inode)
55155513
*/
55165514
btrfs_btree_balance_dirty_nodelay(fs_info);
55175515
if (ret && ret != -ENOSPC && ret != -EAGAIN)
5518-
goto out;
5516+
goto out_release;
55195517
else if (!ret)
55205518
break;
55215519
}
@@ -5529,16 +5527,17 @@ void btrfs_evict_inode(struct inode *inode)
55295527
* If it turns out that we are dropping too many of these, we might want
55305528
* to add a mechanism for retrying these after a commit.
55315529
*/
5532-
trans = evict_refill_and_join(root, rsv);
5530+
trans = evict_refill_and_join(root, &rsv);
55335531
if (!IS_ERR(trans)) {
5534-
trans->block_rsv = rsv;
5532+
trans->block_rsv = &rsv;
55355533
btrfs_orphan_del(trans, BTRFS_I(inode));
55365534
trans->block_rsv = &fs_info->trans_block_rsv;
55375535
btrfs_end_transaction(trans);
55385536
}
55395537

5538+
out_release:
5539+
btrfs_block_rsv_release(fs_info, &rsv, (u64)-1, NULL);
55405540
out:
5541-
btrfs_free_block_rsv(fs_info, rsv);
55425541
/*
55435542
* If we didn't successfully delete, the orphan item will still be in
55445543
* the tree and we'll retry on the next mount. Again, we might also want

0 commit comments

Comments
 (0)