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 3/6] btrfs: relocate only referenced bytes of a data extent
Date: Sun, 29 Nov 2026 14:31:16 +0100	[thread overview]
Message-ID: <20261129133125.3293-4-ismael@iodev.co.uk> (raw)
In-Reply-To: <20261129133125.3293-1-ismael@iodev.co.uk>

Instead of relocating extents whole, dead gaps included, let's collect
the referenced ranges of the extent from every leaf, so each file extent
item contributes the range it maps (and contiguous/overlapping ranges
get merged).

Compressed extents are atomic, so they are still copied whole, and so
is an extent whose references can't be found.

Referenced ranges are copied laid out back to back, so the result has
no dead bytes and each file extent item maps to the offset of its range
inside that allocation. Copies are grouped into the clusters that the
move stage already writes, so the clusters keep the per-member end and
offset they need.

replace_file_extents() no longer requires the new file extent offset to
be zero. It adds the file extent's offset and the offset of the range
inside the new allocation.

If a reference survives the update stage, the old block group still
has used bytes. Removing its chunk would leave that reference dangling,
so relocate_block_group() now fails with -EUCLEAN and the chunk is kept.

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

diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index c7b5a655e89f0..a9da7a6468fa5 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -133,6 +133,16 @@ struct file_extent_cluster {
 	u64 start;
 	u64 end;
 	u64 boundary[MAX_EXTENTS];
+	/* Last byte of the referenced range of each member. */
+	u64 member_end[MAX_EXTENTS];
+	/* Offset of each member inside its allocation. */
+	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.
+	 */
+	u64 group_start[MAX_EXTENTS];
+	u64 group_len[MAX_EXTENTS];
 	unsigned int nr;
 	u64 owning_root;
 };
@@ -156,6 +166,8 @@ struct reloc_control {
 	struct btrfs_backref_cache backref_cache;
 
 	struct file_extent_cluster cluster;
+	/* Where the next cluster's allocations should start looking. */
+	u64 alloc_hint;
 	/* tree blocks have been processed */
 	struct extent_io_tree processed_blocks;
 	/* map start of tree root to corresponding reloc tree */
@@ -867,14 +879,29 @@ int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
 /*
  * get new location of data
  */
+/*
+ * Find the relocated location of the referenced range [@bytenr, @bytenr +
+ * @num_bytes) of an old data extent.
+ *
+ * The data relocation inode holds one file extent per copied segment, at the
+ * segment's old address. Each one points into its new disk extent, at the
+ * segment's offset there. The
+ * range must lie entirely inside one such file extent. On return
+ * @new_bytenr and @new_disk_num_bytes describe the new disk extent, and
+ * @new_offset is where the range starts inside it.
+ */
 static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
+			    u64 *new_disk_num_bytes, u64 *new_offset,
 			    u64 bytenr, u64 num_bytes)
 {
 	struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
 	struct btrfs_fs_info *fs_info = root->fs_info;
 	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_file_extent_item *fi;
+	struct btrfs_key key;
 	struct extent_buffer *leaf;
+	u64 ino = btrfs_ino(BTRFS_I(reloc_inode));
+	u64 fi_len;
 	int ret;
 
 	path = btrfs_alloc_path();
@@ -882,49 +909,58 @@ static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
 		return -ENOMEM;
 
 	bytenr -= BTRFS_I(reloc_inode)->reloc_block_group_start;
-	ret = btrfs_lookup_file_extent(NULL, root, path,
-			btrfs_ino(BTRFS_I(reloc_inode)), bytenr, 0);
+	ret = btrfs_lookup_file_extent(NULL, root, path, ino, bytenr, 0);
 	if (ret < 0)
 		return ret;
-	if (ret > 0)
-		return -ENOENT;
+	if (ret > 0) {
+		/* The covering item is the one before the insertion point. */
+		if (path->slots[0] == 0)
+			return -ENOENT;
+		path->slots[0]--;
+	}
 
 	leaf = path->nodes[0];
+	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
+	if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
+		return -ENOENT;
 	fi = btrfs_item_ptr(leaf, path->slots[0],
 			    struct btrfs_file_extent_item);
 	if (unlikely(btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE)) {
 		btrfs_print_leaf(leaf);
 		btrfs_err(fs_info,
 	"unexpected inline file extent item for data reloc inode %llu key offset %llu",
-			  btrfs_ino(BTRFS_I(reloc_inode)), bytenr);
-		return -EUCLEAN;
-	}
-
-	/*
-	 * The cluster-boundary key searched above is always written by
-	 * relocation with offset 0: either by insert_prealloc_file_extent()
-	 * (memsets the stack item to 0) or by the front portion of a partial
-	 * writeback (offset=0 by construction). A non-zero value here means
-	 * the on-disk leaf does not match what relocation wrote, i.e.
-	 * corruption. The other encoding fields are caught earlier by
-	 * tree-checker's check_extent_data_item().
-	 */
-	if (unlikely(btrfs_file_extent_offset(leaf, fi))) {
-		btrfs_print_leaf(leaf);
-		btrfs_err(fs_info,
-"unexpected non-zero offset in file extent item for data reloc inode %llu key offset %llu offset %llu",
-			  btrfs_ino(BTRFS_I(reloc_inode)), bytenr,
-			  btrfs_file_extent_offset(leaf, fi));
+			  ino, bytenr);
 		return -EUCLEAN;
 	}
 
-	if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi))
-		return -EINVAL;
+	/* The range must be covered by this one segment. */
+	fi_len = btrfs_file_extent_num_bytes(leaf, fi);
+	if (key.offset > bytenr || bytenr + num_bytes > key.offset + fi_len)
+		return -ENOENT;
 
 	*new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
+	*new_disk_num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
+	*new_offset = btrfs_file_extent_offset(leaf, fi) + (bytenr - key.offset);
 	return 0;
 }
 
+/*
+ * Map a referenced range of an old disk extent to its relocated location.
+ *
+ * @bytenr is the start of the old disk extent, @extent_offset is where the
+ * file extent starts inside it and @num_bytes is the file extent's length.
+ * See get_new_location() for the outputs.
+ */
+static int map_relocated_range(struct reloc_control *rc, u64 bytenr,
+			       u64 extent_offset, u64 num_bytes,
+			       u64 *new_bytenr, u64 *new_disk_num_bytes,
+			       u64 *new_extent_offset)
+{
+	return get_new_location(rc->data_inode, new_bytenr, new_disk_num_bytes,
+				new_extent_offset, bytenr + extent_offset,
+				num_bytes);
+}
+
 /*
  * update file extent items in the tree leaf to point to
  * the new locations.
@@ -942,6 +978,10 @@ int replace_file_extents(struct btrfs_trans_handle *trans,
 	u64 parent;
 	u64 bytenr;
 	u64 new_bytenr = 0;
+	u64 new_extent_offset = 0;
+	u64 new_disk_num_bytes = 0;
+	u64 extent_offset;
+	u64 fs_len;
 	u64 num_bytes;
 	u64 end;
 	u32 nritems;
@@ -1016,8 +1056,11 @@ int replace_file_extents(struct btrfs_trans_handle *trans,
 			}
 		}
 
-		ret = get_new_location(rc->data_inode, &new_bytenr,
-				       bytenr, num_bytes);
+		extent_offset = btrfs_file_extent_offset(leaf, fi);
+		fs_len = btrfs_file_extent_num_bytes(leaf, fi);
+		ret = map_relocated_range(rc, bytenr, extent_offset, fs_len,
+					  &new_bytenr, &new_disk_num_bytes,
+					  &new_extent_offset);
 		if (ret) {
 			/*
 			 * Don't have to abort since we've not changed anything
@@ -1027,15 +1070,17 @@ int replace_file_extents(struct btrfs_trans_handle *trans,
 		}
 
 		btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
+		btrfs_set_file_extent_disk_num_bytes(leaf, fi, new_disk_num_bytes);
+		btrfs_set_file_extent_offset(leaf, fi, new_extent_offset);
 
-		key.offset -= btrfs_file_extent_offset(leaf, fi);
 		ref.action = BTRFS_ADD_DELAYED_REF;
 		ref.bytenr = new_bytenr;
-		ref.num_bytes = num_bytes;
+		ref.num_bytes = new_disk_num_bytes;
 		ref.parent = parent;
 		ref.owning_root = btrfs_root_id(root);
 		ref.ref_root = btrfs_header_owner(leaf);
-		btrfs_init_data_ref(&ref, key.objectid, key.offset,
+		btrfs_init_data_ref(&ref, key.objectid,
+				    key.offset - new_extent_offset,
 				    btrfs_root_id(root), false);
 		ret = btrfs_inc_extent_ref(trans, &ref);
 		if (unlikely(ret)) {
@@ -1049,7 +1094,8 @@ int replace_file_extents(struct btrfs_trans_handle *trans,
 		ref.parent = parent;
 		ref.owning_root = btrfs_root_id(root);
 		ref.ref_root = btrfs_header_owner(leaf);
-		btrfs_init_data_ref(&ref, key.objectid, key.offset,
+		btrfs_init_data_ref(&ref, key.objectid,
+				    key.offset - extent_offset,
 				    btrfs_root_id(root), false);
 		ret = btrfs_free_extent(trans, &ref);
 		if (unlikely(ret)) {
@@ -2784,20 +2830,29 @@ int relocate_tree_blocks(struct btrfs_trans_handle *trans,
 	return ret;
 }
 
+/* Total bytes of the allocations a cluster needs. */
+static u64 cluster_allocation_bytes(const struct file_extent_cluster *cluster)
+{
+	u64 total = 0;
+	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];
+	return total;
+}
+
 static noinline_for_stack int prealloc_file_extent_cluster(struct reloc_control *rc)
 {
 	const struct file_extent_cluster *cluster = &rc->cluster;
 	struct btrfs_inode *inode = BTRFS_I(rc->data_inode);
-	u64 alloc_hint = 0;
-	u64 start;
-	u64 end;
+	struct btrfs_prealloc_piece *pieces;
 	u64 offset = inode->reloc_block_group_start;
-	u64 num_bytes;
-	int nr;
-	int ret = 0;
 	u64 prealloc_start = cluster->start - offset;
 	u64 prealloc_end = cluster->end - offset;
-	u64 cur_offset = prealloc_start;
+	u64 reserved;
+	unsigned int nr = 0;
+	int ret;
 
 	/*
 	 * For blocksize < folio size case (either bs < page size or large folios),
@@ -2815,36 +2870,57 @@ static noinline_for_stack int prealloc_file_extent_cluster(struct reloc_control
 		return ret;
 
 	BUG_ON(cluster->start != cluster->boundary[0]);
-	ret = btrfs_alloc_data_chunk_ondemand(inode,
-					      prealloc_end + 1 - prealloc_start);
+	reserved = cluster_allocation_bytes(cluster);
+	ret = btrfs_alloc_data_chunk_ondemand(inode, reserved);
 	if (ret)
 		return ret;
 
+	pieces = kmalloc_array(MAX_EXTENTS, sizeof(*pieces), GFP_NOFS);
+	if (!pieces) {
+		btrfs_free_reserved_data_space_noquota(inode, reserved);
+		return -ENOMEM;
+	}
+
 	btrfs_inode_lock(inode, 0);
-	for (nr = 0; nr < cluster->nr; nr++) {
+	while (nr < cluster->nr) {
 		struct extent_state *cached_state = NULL;
-
-		start = cluster->boundary[nr] - offset;
-		if (nr + 1 < cluster->nr)
-			end = cluster->boundary[nr + 1] - 1 - offset;
-		else
-			end = cluster->end - offset;
-
-		btrfs_lock_extent(&inode->io_tree, start, end, &cached_state);
-		num_bytes = end + 1 - start;
-		ret = btrfs_prealloc_file_range(&inode->vfs_inode, 0, start,
-						num_bytes, num_bytes,
-						end + 1, &alloc_hint, true);
-		cur_offset = end + 1;
-		btrfs_unlock_extent(&inode->io_tree, start, end, &cached_state);
-		if (ret)
+		unsigned int last = nr;
+		unsigned int n = 0;
+		u64 group = cluster->group_start[nr];
+		u64 group_len = cluster->group_len[nr];
+		u64 lock_end;
+		unsigned int i;
+
+		while (last < cluster->nr && cluster->group_start[last] == group)
+			last++;
+		for (i = nr; i < last; i++) {
+			pieces[n].file_start = cluster->boundary[i] - offset;
+			pieces[n].data_offset = cluster->data_offset[i];
+			pieces[n].len = cluster->member_end[i] + 1 -
+					cluster->boundary[i];
+			n++;
+		}
+		lock_end = pieces[n - 1].file_start + pieces[n - 1].len - 1;
+
+		btrfs_lock_extent(&inode->io_tree, pieces[0].file_start, lock_end,
+				  &cached_state);
+		ret = btrfs_prealloc_file_range_shared(&inode->vfs_inode, pieces,
+						       n, group_len, &rc->alloc_hint);
+		btrfs_unlock_extent(&inode->io_tree, pieces[0].file_start,
+				    lock_end, &cached_state);
+		if (ret) {
+			/* This group's bytes were consumed by the attempt. */
+			reserved -= group_len;
 			break;
+		}
+		reserved -= group_len;
+		nr = last;
 	}
 	btrfs_inode_unlock(inode, 0);
 
-	if (cur_offset < prealloc_end)
-		btrfs_free_reserved_data_space_noquota(inode,
-						       prealloc_end + 1 - cur_offset);
+	kfree(pieces);
+	if (reserved)
+		btrfs_free_reserved_data_space_noquota(inode, reserved);
 	return ret;
 }
 
@@ -2891,12 +2967,7 @@ ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE);
 static u64 get_cluster_boundary_end(const struct file_extent_cluster *cluster,
 				    int cluster_nr)
 {
-	/* Last extent, use cluster end directly */
-	if (cluster_nr >= cluster->nr - 1)
-		return cluster->end;
-
-	/* Use next boundary start*/
-	return cluster->boundary[cluster_nr + 1] - 1;
+	return cluster->member_end[cluster_nr];
 }
 
 static int relocate_one_folio(struct reloc_control *rc,
@@ -3095,15 +3166,176 @@ static int relocate_file_extent_cluster(struct reloc_control *rc)
 	return ret;
 }
 
-static noinline_for_stack int relocate_data_extent(struct reloc_control *rc,
-					   const struct btrfs_key *extent_key)
+/*
+ * One referenced range of an old data extent. @offset is relative to the
+ * start of the disk extent, not the file.
+ */
+struct extent_segment {
+	u64 offset;
+	u64 len;
+};
+
+struct extent_segments {
+	struct extent_segment *segs;
+	unsigned int nr;
+	unsigned int alloc;
+	/* Compressed extents are atomic and must be copied whole. */
+	bool compressed;
+};
+
+static int add_extent_segment(struct extent_segments *es, u64 offset, u64 len)
+{
+	struct extent_segment *new;
+	unsigned int nalloc;
+
+	if (es->nr == es->alloc) {
+		nalloc = max(16U, es->alloc * 2);
+		new = krealloc_array(es->segs, nalloc, sizeof(*new), GFP_NOFS);
+		if (!new)
+			return -ENOMEM;
+		es->segs = new;
+		es->alloc = nalloc;
+	}
+	es->segs[es->nr].offset = offset;
+	es->segs[es->nr].len = len;
+	es->nr++;
+	return 0;
+}
+
+static int cmp_extent_segment(const void *a, const void *b)
+{
+	const struct extent_segment *sa = a;
+	const struct extent_segment *sb = b;
+
+	if (sa->offset < sb->offset)
+		return -1;
+	if (sa->offset > sb->offset)
+		return 1;
+	return 0;
+}
+
+/*
+ * Sort the segments and merge the ones that overlap or touch, so each
+ * remaining segment is a maximal referenced range.
+ */
+static void normalize_extent_segments(struct extent_segments *es)
+{
+	unsigned int i;
+	unsigned int out = 0;
+
+	if (es->nr == 0)
+		return;
+	sort(es->segs, es->nr, sizeof(*es->segs), cmp_extent_segment, NULL);
+	for (i = 0; i < es->nr; i++) {
+		struct extent_segment *cur = &es->segs[i];
+
+		if (out) {
+			struct extent_segment *prev = &es->segs[out - 1];
+
+			if (prev->offset + prev->len >= cur->offset) {
+				u64 end = max(prev->offset + prev->len,
+					      cur->offset + cur->len);
+
+				prev->len = end - prev->offset;
+				continue;
+			}
+		}
+		es->segs[out++] = *cur;
+	}
+	es->nr = out;
+}
+
+/*
+ * Collect the referenced ranges of the data extent described by @extent_key
+ * from every leaf that holds a reference to it.
+ */
+static int collect_extent_segments(struct reloc_control *rc,
+				   const struct btrfs_key *extent_key,
+				   struct extent_segments *es)
+{
+	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
+	struct btrfs_backref_walk_ctx ctx = { 0 };
+	struct ulist_iterator leaf_uiter;
+	struct ulist_node *ref_node;
+	const u64 bytenr = extent_key->objectid;
+	const u64 num_bytes = extent_key->offset;
+	int ret;
+
+	ctx.bytenr = bytenr;
+	ctx.skip_inode_ref_list = true;
+	ctx.fs_info = fs_info;
+
+	ret = btrfs_find_all_leafs(&ctx);
+	if (ret < 0)
+		return ret;
+
+	ULIST_ITER_INIT(&leaf_uiter);
+	while ((ref_node = ulist_next(ctx.refs, &leaf_uiter))) {
+		struct btrfs_tree_parent_check check = { 0 };
+		struct extent_buffer *eb;
+		int slot;
+
+		eb = read_tree_block(fs_info, ref_node->val, &check);
+		if (IS_ERR(eb)) {
+			ret = PTR_ERR(eb);
+			break;
+		}
+		for (slot = 0; slot < btrfs_header_nritems(eb); slot++) {
+			struct btrfs_file_extent_item *fi;
+			struct btrfs_key key;
+
+			btrfs_item_key_to_cpu(eb, &key, slot);
+			if (key.type != BTRFS_EXTENT_DATA_KEY)
+				continue;
+			fi = btrfs_item_ptr(eb, slot,
+					    struct btrfs_file_extent_item);
+			if (btrfs_file_extent_type(eb, fi) ==
+			    BTRFS_FILE_EXTENT_INLINE)
+				continue;
+			if (btrfs_file_extent_disk_bytenr(eb, fi) != bytenr)
+				continue;
+			if (btrfs_file_extent_disk_num_bytes(eb, fi) !=
+			    num_bytes) {
+				ret = -EUCLEAN;
+				break;
+			}
+			if (btrfs_file_extent_compression(eb, fi) !=
+			    BTRFS_COMPRESS_NONE) {
+				es->compressed = true;
+				continue;
+			}
+			ret = add_extent_segment(es,
+					btrfs_file_extent_offset(eb, fi),
+					btrfs_file_extent_num_bytes(eb, fi));
+			if (ret)
+				break;
+		}
+		free_extent_buffer(eb);
+		if (ret)
+			break;
+	}
+	ulist_free(ctx.refs);
+	if (ret)
+		return ret;
+
+	normalize_extent_segments(es);
+	return 0;
+}
+
+/*
+ * Start a new allocation group in the cluster. A group must not be split, so
+ * flush the cluster first if the group does not follow the last member
+ * directly or does not fit in the remaining slots.
+ */
+static int cluster_begin_group(struct reloc_control *rc, u64 start,
+			       unsigned int count)
 {
-	struct inode *inode = rc->data_inode;
 	struct file_extent_cluster *cluster = &rc->cluster;
+	struct btrfs_root *root = BTRFS_I(rc->data_inode)->root;
 	int ret;
-	struct btrfs_root *root = BTRFS_I(inode)->root;
 
-	if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
+	if (cluster->nr > 0 &&
+	    (start != cluster->end + 1 || cluster->nr + count > MAX_EXTENTS)) {
 		ret = relocate_file_extent_cluster(rc);
 		if (ret)
 			return ret;
@@ -3137,24 +3369,87 @@ static noinline_for_stack int relocate_data_extent(struct reloc_control *rc,
 		/* And reset it back for the current extent's owning root. */
 		root->relocation_src_root = tmp;
 	}
+	return 0;
+}
 
-	if (!cluster->nr) {
-		cluster->start = extent_key->objectid;
-		cluster->owning_root = root->relocation_src_root;
-	}
-	else
-		BUG_ON(cluster->nr >= MAX_EXTENTS);
-	cluster->end = extent_key->objectid + extent_key->offset - 1;
-	cluster->boundary[cluster->nr] = extent_key->objectid;
+/* 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)
+{
+	struct file_extent_cluster *cluster = &rc->cluster;
+	unsigned int nr = cluster->nr;
+
+	BUG_ON(nr >= MAX_EXTENTS);
+	if (!nr) {
+		cluster->start = start;
+		cluster->owning_root = BTRFS_I(rc->data_inode)->root->relocation_src_root;
+	}
+	cluster->end = start + len - 1;
+	cluster->boundary[nr] = start;
+	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++;
+}
 
-	if (cluster->nr >= MAX_EXTENTS) {
-		ret = relocate_file_extent_cluster(rc);
+/*
+ * Relocate one data extent. Only the bytes that are referenced are copied.
+ * The referenced ranges of an extent are laid out back to back in one
+ * allocation, so the relocated extent has no dead bytes in it. Compressed
+ * extents are atomic and are copied whole, and so is an extent whose
+ * references can't be found, which keeps the old behaviour for it.
+ */
+static noinline_for_stack int relocate_data_extent(struct reloc_control *rc,
+						   const struct btrfs_key *extent_key)
+{
+	struct extent_segments es = { 0 };
+	struct extent_segment whole = { .offset = 0, .len = extent_key->offset };
+	u64 start = extent_key->objectid;
+	unsigned int i;
+	int ret;
+
+	ret = collect_extent_segments(rc, extent_key, &es);
+	if (ret)
+		goto out;
+
+	if (es.compressed || es.nr == 0) {
+		kfree(es.segs);
+		es.segs = &whole;
+		es.nr = 1;
+		es.alloc = 0;
+	}
+
+	/*
+	 * Split the segments into groups of at most MAX_EXTENTS pieces, each
+	 * with its own allocation.
+	 */
+	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)
-			return ret;
-		cluster->nr = 0;
+			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,
+					   data_offset);
+			data_offset += es.segs[j].len;
+		}
+		i += count;
 	}
-	return 0;
+out:
+	if (es.segs != &whole)
+		kfree(es.segs);
+	return ret;
 }
 
 /*
@@ -5426,7 +5721,16 @@ static int do_nonremap_reloc(struct btrfs_fs_info *fs_info, bool verbose,
 
 	WARN_ON(rc->block_group->pinned > 0);
 	WARN_ON(rc->block_group->reserved > 0);
-	WARN_ON(rc->block_group->used > 0);
+	/*
+	 * A live reference into the old block group would dangle once the
+	 * chunk is removed. Keep the chunk instead of removing it.
+	 */
+	if (rc->block_group->used > 0) {
+		btrfs_warn(fs_info,
+			  "relocation left %llu bytes referenced in block group %llu",
+			  rc->block_group->used, rc->block_group->start);
+		return -EUCLEAN;
+	}
 
 	return 0;
 }
-- 
2.56.0


  parent reply	other threads:[~2026-11-29 13:40 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 ` Ismael Luceno [this message]
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 ` [PATCH 6/6] btrfs: derive relocation group length from its members Ismael Luceno
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-4-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®