From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=46.30.189.100 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795959629; b=ZT8Bacp58e7AK7cL1Hc7mPYnYR/gj53b4r5uuUjm+FhgQmzkk5ArV4at+ePW30YS7VWm PLjd7Jgq8E5Tc+5HrnelnXsBtrcAcBl5DTw44pGdwc1FRApbhiQY8ZqN6lCFJsH8Hk7eZ XxOTN5xtGiJGSRGs0FBPU0OTtBKhVn2zz9219NPVeLDgE6/jYHdeIf23EKMQG6RVpLos2 tUL1zhpRNYoMCWonrkEZ3tfuS5CKpKWNEGPgcZQfqXVELghHI/ktO29aluT4YgNhAg9YY IaJpwVsvQIrh7zVIfi80Gxj+DDJWEBUnaNKkLDpTr3Uw9vhd1PTXh/OeFHn2T5QFK6A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795959629; h=DMARC-Filter:Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=t6y99RRr+l05W9SHkjBtu7zbaCmsUfGITeOJjEy013c=; b=Baf+2G/Yhg9CLPAn3AuneUhUIeXeKcUuyU6IutgAb1gmtgFkzYS04Q+eo2iML6Zo0p10 BLi6Sn3A4lkQ8p1cBXF4o5koXkNB8xFjeGTI87BELFRRNu9ORoA453+Wh6RhXO5rCQBtU CG3WQWKIJDax3bXA27Iz3fowLZfDoOi5DiLAAhehKXZBcXSJ55iWpa0o+I/MB2qRVB9E9 IxczxD9LwKXvZ86xI0SmFOUMOVfs8SUcFhO47++zxG5V7p699rjb1XnbvNyAR+IYuLL1a LNEQ1X9EYUPaQJJaCsXC2Gz1iZWlj/V2q+6hjZmtLn2CRaUpvhvoYXc5BTT7EYxppYw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=iodev.co.uk; spf=pass smtp.mailfrom=iodev.co.uk; arc=none smtp.remote-ip=46.30.189.100 Received: from iodev.co.uk (iodev.co.uk [46.30.189.100]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DE5C1606A6A1; Sun, 29 Nov 2026 13:40:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DE5C1606A6A1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=iodev.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=iodev.co.uk Received: from localhost (unknown [79.116.217.174]) by iodev.co.uk (Postfix) with ESMTPSA id 0F5305F0B96; Sun, 29 Nov 2026 14:31:31 +0100 (CET) From: Ismael Luceno To: linux-btrfs@vger.kernel.org, David Sterba , Chris Mason Cc: Ismael Luceno , linux-kernel@vger.kernel.org, Qu Wenruo , Anand Jain , Filipe Manana Subject: [PATCH 1/6] btrfs: add prealloc helper for sharing one allocation Date: Sun, 29 Nov 2026 14:31:14 +0100 Message-ID: <20261129133125.3293-2-ismael@iodev.co.uk> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261129133125.3293-1-ismael@iodev.co.uk> References: <20261129133125.3293-1-ismael@iodev.co.uk> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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