[PATCH v3 06/17] btrfs: drop the transaction handle from the prealloc helpers

From: Tal Zussman

Date: Thu Sep 17 2026 - 00:04:43 EST


The v1 space cache created its inode during the transaction commit, and
btrfs_prealloc_file_range_trans() existed so that preallocation could
reuse the open handle. It was the only caller passing a transaction, so
__btrfs_prealloc_file_range() and insert_prealloc_file_extent() now
always start their own. Fold the wrapper into
btrfs_prealloc_file_range() and drop the parameter.

Assisted-by: Claude:claude-fable-5-1
Signed-off-by: Tal Zussman <tz2294@xxxxxxxxxxxx>
---
fs/btrfs/inode.c | 47 ++++++++++-------------------------------------
1 file changed, 10 insertions(+), 37 deletions(-)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 9c395d075a65..0539f27776e1 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -9132,14 +9132,13 @@ static int btrfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
}

static struct btrfs_trans_handle *insert_prealloc_file_extent(
- struct btrfs_trans_handle *trans_in,
struct btrfs_inode *inode,
struct btrfs_key *ins,
u64 file_offset)
{
struct btrfs_file_extent_item stack_fi;
struct btrfs_replace_extent_info extent_info;
- struct btrfs_trans_handle *trans = trans_in;
+ struct btrfs_trans_handle *trans;
struct btrfs_path *path;
u64 start = ins->objectid;
u64 len = ins->offset;
@@ -9160,15 +9159,6 @@ static struct btrfs_trans_handle *insert_prealloc_file_extent(
if (ret < 0)
return ERR_PTR(ret);

- if (trans) {
- ret = insert_reserved_file_extent(trans, inode,
- file_offset, &stack_fi,
- true, qgroup_released);
- if (ret)
- goto free_qgroup;
- return trans;
- }
-
extent_info.disk_offset = start;
extent_info.disk_len = len;
extent_info.data_offset = 0;
@@ -9208,12 +9198,12 @@ static struct btrfs_trans_handle *insert_prealloc_file_extent(
return ERR_PTR(ret);
}

-static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
- u64 start, u64 num_bytes, u64 min_size,
- loff_t actual_len, u64 *alloc_hint,
- struct btrfs_trans_handle *trans)
+int btrfs_prealloc_file_range(struct inode *inode, int mode,
+ u64 start, u64 num_bytes, u64 min_size,
+ loff_t actual_len, u64 *alloc_hint)
{
struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
+ struct btrfs_trans_handle *trans;
struct extent_map *em;
struct btrfs_root *root = BTRFS_I(inode)->root;
struct btrfs_key ins;
@@ -9223,11 +9213,8 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
u64 cur_bytes;
u64 last_alloc = (u64)-1;
int ret = 0;
- bool own_trans = true;
u64 end = start + num_bytes - 1;

- if (trans)
- own_trans = false;
while (num_bytes > 0) {
cur_bytes = min_t(u64, num_bytes, SZ_256M);
cur_bytes = max(cur_bytes, min_size);
@@ -9253,8 +9240,8 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
clear_offset += ins.offset;

last_alloc = ins.offset;
- trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
- &ins, cur_offset);
+ trans = insert_prealloc_file_extent(BTRFS_I(inode), &ins,
+ cur_offset);
/*
* Now that we inserted the prealloc extent we can finally
* decrement the number of reservations in the block group.
@@ -9326,8 +9313,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
range_start, range_end - range_start);
if (ret) {
btrfs_abort_transaction(trans, ret);
- if (own_trans)
- btrfs_end_transaction(trans);
+ btrfs_end_transaction(trans);
break;
}

@@ -9339,15 +9325,11 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,

if (unlikely(ret)) {
btrfs_abort_transaction(trans, ret);
- if (own_trans)
- btrfs_end_transaction(trans);
+ btrfs_end_transaction(trans);
break;
}

- if (own_trans) {
- btrfs_end_transaction(trans);
- trans = NULL;
- }
+ btrfs_end_transaction(trans);
}
if (clear_offset < end)
btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
@@ -9355,15 +9337,6 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
return ret;
}

-int btrfs_prealloc_file_range(struct inode *inode, int mode,
- u64 start, u64 num_bytes, u64 min_size,
- loff_t actual_len, u64 *alloc_hint)
-{
- return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
- min_size, actual_len, alloc_hint,
- NULL);
-}
-
/*
* NOTE: in case you are adding MAY_EXEC check for directories:
* we are marking them with IOP_FASTPERM_MAY_EXEC, allowing path lookup to

--
2.39.5