mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ismael Luceno <ismael@iodev.co.uk>
To: linux-btrfs@vger.kernel.org, David Sterba <dsterba@suse.com>,
	Chris Mason <mason@kernel.org>
Cc: Ismael Luceno <ismael@iodev.co.uk>,
	linux-kernel@vger.kernel.org, Qu Wenruo <wqu@suse.com>,
	Anand Jain <asj@kernel.org>, Filipe Manana <fdmanana@suse.com>
Subject: [PATCH 6/6] btrfs: derive relocation group length from its members
Date: Sun, 29 Nov 2026 14:31:19 +0100	[thread overview]
Message-ID: <20261129133125.3293-7-ismael@iodev.co.uk> (raw)
In-Reply-To: <20261129133125.3293-1-ismael@iodev.co.uk>

A relocation group is the set of members that share one allocation. Its
length was stored per member, and cluster_add_member() copied the same
value into every member of the group. Nothing needs a stored value: the
length is the sum of the members' lengths, and prealloc can compute it
when it issues the group.

Compute the group length in prealloc_file_extent_cluster() and the
cluster's allocation size from the members. No behaviour changes. This
lets the next change grow a group past the extents it started with.

Signed-off-by: Ismael Luceno <ismael@iodev.co.uk>
---
 fs/btrfs/relocation.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index 9bf0f9bc794c4..3b6dd9f9ec7e0 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -139,10 +139,9 @@ struct file_extent_cluster {
 	u64 data_offset[MAX_EXTENTS];
 	/*
 	 * Consecutive members with the same group_start are segments of one old
-	 * extent. They share one allocation of group_len bytes.
+	 * extent. They share one allocation, as big as the sum of their lengths.
 	 */
 	u64 group_start[MAX_EXTENTS];
-	u64 group_len[MAX_EXTENTS];
 	unsigned int nr;
 	u64 owning_root;
 };
@@ -2837,8 +2836,7 @@ static u64 cluster_allocation_bytes(const struct file_extent_cluster *cluster)
 	unsigned int i;
 
 	for (i = 0; i < cluster->nr; i++)
-		if (i == 0 || cluster->group_start[i] != cluster->group_start[i - 1])
-			total += cluster->group_len[i];
+		total += cluster->member_end[i] + 1 - cluster->boundary[i];
 	return total;
 }
 
@@ -2887,7 +2885,7 @@ static noinline_for_stack int prealloc_file_extent_cluster(struct reloc_control
 		unsigned int last = nr;
 		unsigned int n = 0;
 		u64 group = cluster->group_start[nr];
-		u64 group_len = cluster->group_len[nr];
+		u64 group_len = 0;
 		u64 lock_end;
 		unsigned int i;
 
@@ -2898,6 +2896,7 @@ static noinline_for_stack int prealloc_file_extent_cluster(struct reloc_control
 			pieces[n].data_offset = cluster->data_offset[i];
 			pieces[n].len = cluster->member_end[i] + 1 -
 					cluster->boundary[i];
+			group_len += pieces[n].len;
 			n++;
 		}
 		lock_end = pieces[n - 1].file_start + pieces[n - 1].len - 1;
@@ -3387,7 +3386,7 @@ static int cluster_begin_group(struct reloc_control *rc, u64 start,
 
 /* Append one referenced range to the cluster as a member. */
 static void cluster_add_member(struct reloc_control *rc, u64 start, u64 len,
-			       u64 group_start, u64 group_len, u64 data_offset)
+			       u64 group_start, u64 data_offset)
 {
 	struct file_extent_cluster *cluster = &rc->cluster;
 	unsigned int nr = cluster->nr;
@@ -3402,7 +3401,6 @@ static void cluster_add_member(struct reloc_control *rc, u64 start, u64 len,
 	cluster->member_end[nr] = start + len - 1;
 	cluster->data_offset[nr] = data_offset;
 	cluster->group_start[nr] = group_start;
-	cluster->group_len[nr] = group_len;
 	cluster->nr++;
 }
 
@@ -3440,20 +3438,16 @@ static noinline_for_stack int relocate_data_extent(struct reloc_control *rc,
 	for (i = 0; i < es.nr; ) {
 		unsigned int count = min_t(unsigned int, es.nr - i, MAX_EXTENTS);
 		u64 group_start = start + es.segs[i].offset;
-		u64 group_len = 0;
 		u64 data_offset = 0;
 		unsigned int j;
 
-		for (j = i; j < i + count; j++)
-			group_len += es.segs[j].len;
-
 		ret = cluster_begin_group(rc, group_start, count);
 		if (ret)
 			goto out;
 
 		for (j = i; j < i + count; j++) {
 			cluster_add_member(rc, start + es.segs[j].offset,
-					   es.segs[j].len, group_start, group_len,
+					   es.segs[j].len, group_start,
 					   data_offset);
 			data_offset += es.segs[j].len;
 		}
-- 
2.56.0


  parent reply	other threads:[~2026-11-29 13:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 13:31 [PATCH 0/6] btrfs: trim unused space when relocating data extents Ismael Luceno
2026-11-29 13:31 ` [PATCH 1/6] btrfs: add prealloc helper for sharing one allocation Ismael Luceno
2026-11-29 13:31 ` [PATCH 2/6] btrfs: allow NOCOW writeback into data relocation extents Ismael Luceno
2026-11-29 13:31 ` [PATCH 3/6] btrfs: relocate only referenced bytes of a data extent Ismael Luceno
2026-11-29 13:31 ` [PATCH 4/6] btrfs: scan the running transaction for data extent references Ismael Luceno
2026-11-29 13:31 ` [PATCH 5/6] btrfs: retry a block group that references survive the update stage Ismael Luceno
2026-11-29 13:31 ` Ismael Luceno [this message]
2026-11-29 21:00 ` [PATCH 0/6] btrfs: trim unused space when relocating data extents Qu Wenruo

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=20261129133125.3293-7-ismael@iodev.co.uk \
    --to=ismael@iodev.co.uk \
    --cc=asj@kernel.org \
    --cc=dsterba@suse.com \
    --cc=fdmanana@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mason@kernel.org \
    --cc=wqu@suse.com \
    /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

all inboxes | Powered by JetHome®