public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Boris Burkov <boris@bur.io>
To: linux-btrfs@vger.kernel.org, kernel-team@fb.com
Subject: [PATCH 2/5] btrfs: account for csum delayed_refs in delalloc
Date: Tue, 24 Mar 2026 17:41:50 -0700	[thread overview]
Message-ID: <36b185d69a6bf98d76a445dddfd6aa14629390fe.1774398665.git.boris@bur.io> (raw)
In-Reply-To: <cover.1774398665.git.boris@bur.io>

As ordered_extents complete, they not only produce direct delayed refs,
they also add csums via add_pending_csums. This produces some number of
metadata delayed_refs as the csum tree is cow-ed. These refs are counted
against the trans_handle delayed_rsv, thanks to trans->adding_csums.

As a result, just like we account for the extent tree and free space
tree when reserving delayed_refs for delalloc, we must also reserve for
the csum tree. This is mirrored by the non-delayed-ref metadata
reservation already accounting for csums.

This serves to ensure we have a proper worst case estimate for
delayed_rsv from delalloc.

Signed-off-by: Boris Burkov <boris@bur.io>
---
 fs/btrfs/delalloc-space.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/delalloc-space.c b/fs/btrfs/delalloc-space.c
index e2944ff4fe47..2eeafada96ec 100644
--- a/fs/btrfs/delalloc-space.c
+++ b/fs/btrfs/delalloc-space.c
@@ -255,6 +255,20 @@ static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
 						   qgroup_to_release);
 }
 
+/*
+ * ordered_extent completion will generate metadata delayed refs for
+ * the extent, free_space, and csum trees. btrfs_calc_delayed_ref_bytes()
+ * accounts for the former two, and we explicitly reserve for the latter.
+ * This ensures that we reserve enough delayed_ref space for each
+ * ordered_extent.
+ */
+static u64 delalloc_calc_delayed_refs_rsv(const struct btrfs_fs_info *fs_info,
+					  unsigned int nr_extents)
+{
+	return btrfs_calc_delayed_ref_bytes(fs_info, nr_extents) +
+		btrfs_calc_insert_metadata_size(fs_info, nr_extents);
+}
+
 static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
 						 struct btrfs_inode *inode)
 {
@@ -276,8 +290,8 @@ static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
 		reserve_size = btrfs_calc_insert_metadata_size(fs_info,
 						outstanding_extents);
 		reserve_size += btrfs_calc_metadata_size(fs_info, 1);
-		delayed_refs_size += btrfs_calc_delayed_ref_bytes(fs_info,
-						outstanding_extents);
+		delayed_refs_size +=
+			delalloc_calc_delayed_refs_rsv(fs_info, outstanding_extents);
 	}
 	if (!(inode->flags & BTRFS_INODE_NODATASUM)) {
 		u64 csum_leaves;
@@ -328,8 +342,7 @@ static void calc_inode_reservations(struct btrfs_inode *inode,
 	 */
 	*meta_reserve += inode_update;
 
-	*delayed_refs_reserve = btrfs_calc_delayed_ref_bytes(fs_info,
-							     nr_extents);
+	*delayed_refs_reserve = delalloc_calc_delayed_refs_rsv(fs_info, nr_extents);
 
 	*qgroup_reserve = nr_extents * fs_info->nodesize;
 }
-- 
2.53.0


  parent reply	other threads:[~2026-03-25  0:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-25  0:41 [PATCH 0/5] btrfs: improve stalls under sudden writeback Boris Burkov
2026-03-25  0:41 ` [PATCH 1/5] btrfs: reserve space for delayed_refs in delalloc Boris Burkov
2026-03-25 15:36   ` Filipe Manana
2026-03-25 18:39     ` Boris Burkov
2026-03-25 18:55       ` Filipe Manana
2026-03-25 22:24         ` Boris Burkov
2026-03-25  0:41 ` Boris Burkov [this message]
2026-03-25  0:41 ` [PATCH 3/5] btrfs: account for compression in delalloc extent reservation Boris Burkov
2026-03-25  0:41 ` [PATCH 4/5] btrfs: make inode->outstanding_extents a u64 Boris Burkov
2026-03-25  0:41 ` [PATCH 5/5] btrfs: cap shrink_delalloc iterations to 128M Boris Burkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=36b185d69a6bf98d76a445dddfd6aa14629390fe.1774398665.git.boris@bur.io \
    --to=boris@bur.io \
    --cc=kernel-team@fb.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox