[PATCH 1/6] btrfs: add prealloc helper for sharing one allocation
From: Ismael Luceno
Date: Sun Nov 29 2026 - 08:41:00 EST
Data relocation is going to copy only the referenced bytes of an extent
and lay them out back to back. The copied pieces then have to share one
disk allocation, while btrfs_prealloc_file_range() only handles a single
range with its own allocation.
Add btrfs_prealloc_file_range_shared(), which takes an array of pieces,
each a file range and its offset inside a single allocation of total_len
bytes. It reserves that allocation once, inserts one preallocated file
extent item per piece, updates the extent maps and i_size like the
existing helper, and frees the reservation again if nothing was inserted.
The qgroup reservation for every piece is released up front, because
only the first inserted item charges the extent with it. Releasing it
per piece would leak the reservation of every later piece.
Signed-off-by: Ismael Luceno <ismael@xxxxxxxxxxx>
---
fs/btrfs/btrfs_inode.h | 14 ++++
fs/btrfs/inode.c | 165 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 179 insertions(+)
diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index fbcf8ca05fb74..1c5a007eae8c4 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -593,6 +593,20 @@ int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size);
void btrfs_add_delayed_iput(struct btrfs_inode *inode);
void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info);
int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info);
+/*
+ * One piece of a shared preallocation: the file range starting at
+ * @file_start, @len bytes long, is backed by the allocation at @data_offset.
+ */
+struct btrfs_prealloc_piece {
+ u64 file_start;
+ u64 data_offset;
+ u64 len;
+};
+
+int btrfs_prealloc_file_range_shared(struct inode *inode,
+ const struct btrfs_prealloc_piece *pieces,
+ unsigned int nr_pieces, u64 total_len,
+ u64 *alloc_hint);
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,
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 4b29b82eb1804..7592588fec64c 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -9165,6 +9165,171 @@ static struct btrfs_trans_handle *insert_prealloc_file_extent(
return ERR_PTR(ret);
}
+/*
+ * Preallocate one extent of @total_len bytes and back several file ranges
+ * with it. Piece k covers [file_start, file_start + len) of the file and is
+ * stored at data_offset inside the allocation. The allocation is a single
+ * extent item, and every piece adds a reference to it.
+ *
+ * The caller must have reserved the data space. The allocation must be one
+ * contiguous extent, so this fails if the allocator can't provide one.
+ */
+int btrfs_prealloc_file_range_shared(struct inode *inode,
+ const struct btrfs_prealloc_piece *pieces,
+ unsigned int nr_pieces, u64 total_len,
+ u64 *alloc_hint)
+{
+ struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
+ struct btrfs_root *root = BTRFS_I(inode)->root;
+ struct btrfs_replace_extent_info extent_info = { 0 };
+ struct btrfs_trans_handle *trans;
+ struct btrfs_path *path;
+ struct btrfs_key ins;
+ u64 qgroup_pending = 0;
+ unsigned int i;
+ int ret;
+
+ if (!nr_pieces)
+ return 0;
+
+ ret = btrfs_reserve_extent(root, total_len, total_len, total_len, 0,
+ *alloc_hint, &ins, true, false);
+ if (ret)
+ return ret;
+
+ /*
+ * Release the qgroup reservation of every piece up front. Only the
+ * first insert consumes it, and it charges the whole allocation at once,
+ * so the pieces after it must not release anything of their own.
+ */
+ for (i = 0; i < nr_pieces; i++) {
+ u64 released = 0;
+
+ ret = btrfs_qgroup_release_data(BTRFS_I(inode),
+ pieces[i].file_start,
+ pieces[i].len, &released);
+ if (ret)
+ goto out_fail;
+ qgroup_pending += released;
+ }
+
+ extent_info.is_new_extent = true;
+ extent_info.disk_offset = ins.objectid;
+ extent_info.disk_len = total_len;
+
+ for (i = 0; i < nr_pieces; i++) {
+ const struct btrfs_prealloc_piece *p = &pieces[i];
+ struct btrfs_file_extent_item stack_fi;
+
+ memset(&stack_fi, 0, sizeof(stack_fi));
+ btrfs_set_stack_file_extent_type(&stack_fi,
+ BTRFS_FILE_EXTENT_PREALLOC);
+ btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, ins.objectid);
+ btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, total_len);
+ btrfs_set_stack_file_extent_offset(&stack_fi, p->data_offset);
+ btrfs_set_stack_file_extent_num_bytes(&stack_fi, p->len);
+ btrfs_set_stack_file_extent_ram_bytes(&stack_fi, p->len);
+ btrfs_set_stack_file_extent_compression(&stack_fi,
+ BTRFS_COMPRESS_NONE);
+
+ extent_info.data_offset = p->data_offset;
+ extent_info.data_len = p->len;
+ extent_info.file_offset = p->file_start;
+ extent_info.extent_buf = (char *)&stack_fi;
+ extent_info.qgroup_reserved = qgroup_pending;
+
+ path = btrfs_alloc_path();
+ if (!path) {
+ ret = -ENOMEM;
+ goto out_fail;
+ }
+ ret = btrfs_replace_file_extents(BTRFS_I(inode), path,
+ p->file_start,
+ p->file_start + p->len - 1,
+ &extent_info, true, &trans);
+ btrfs_free_path(path);
+ /* The first piece turned the reservation into an extent item. */
+ if (i == 0)
+ btrfs_dec_block_group_reservations(fs_info, ins.objectid);
+ if (ret)
+ goto out_fail;
+ /* The first insert charged the qgroup reservation. */
+ qgroup_pending = 0;
+
+ {
+ struct extent_map *em = btrfs_alloc_extent_map();
+
+ if (!em) {
+ btrfs_end_transaction(trans);
+ ret = -ENOMEM;
+ goto out_fail;
+ }
+ em->start = p->file_start;
+ em->len = p->len;
+ em->disk_bytenr = ins.objectid;
+ em->offset = p->data_offset;
+ em->disk_num_bytes = total_len;
+ em->ram_bytes = p->len;
+ em->flags |= EXTENT_FLAG_PREALLOC;
+ em->generation = trans->transid;
+
+ ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em,
+ true);
+ btrfs_free_extent_map(em);
+ }
+ if (ret) {
+ btrfs_abort_transaction(trans, ret);
+ btrfs_end_transaction(trans);
+ return ret;
+ }
+
+ inode_inc_iversion(inode);
+ inode_set_ctime_current(inode);
+ BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
+ /* Like btrfs_prealloc_file_range(), grow i_size to cover the piece. */
+ if (p->file_start + p->len > inode->i_size) {
+ u64 new_size = p->file_start + p->len;
+ u64 range_start = round_down(inode->i_size,
+ fs_info->sectorsize);
+ u64 range_end = round_up(new_size, fs_info->sectorsize);
+
+ ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
+ range_start, range_end - range_start);
+ if (ret) {
+ btrfs_abort_transaction(trans, ret);
+ btrfs_end_transaction(trans);
+ return ret;
+ }
+ i_size_write(inode, new_size);
+ btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
+ }
+ ret = btrfs_update_inode(trans, BTRFS_I(inode));
+ if (ret) {
+ btrfs_abort_transaction(trans, ret);
+ btrfs_end_transaction(trans);
+ return ret;
+ }
+ btrfs_end_transaction(trans);
+ }
+
+ *alloc_hint = ins.objectid + total_len;
+ return 0;
+
+out_fail:
+ /*
+ * Nothing was inserted yet, so the reservation can go back. Once an
+ * extent item exists the allocation has references and can't be freed
+ * here; the error then propagates and the balance fails.
+ */
+ if (qgroup_pending)
+ btrfs_qgroup_free_refroot(fs_info, btrfs_root_id(root),
+ qgroup_pending, BTRFS_QGROUP_RSV_DATA);
+ if (extent_info.insertions == 0)
+ btrfs_free_reserved_extent(fs_info, ins.objectid, total_len,
+ false);
+ 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,
--
2.56.0