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 5/6] btrfs: retry a block group that references survive the update stage
Date: Sun, 29 Nov 2026 14:31:18 +0100 [thread overview]
Message-ID: <20261129133125.3293-6-ismael@iodev.co.uk> (raw)
In-Reply-To: <20261129133125.3293-1-ismael@iodev.co.uk>
A reference added during the update stage can survive it. Its bytes were
not moved, and the block group keeps used bytes. Failing the balance
right away is safe, since the chunk is kept, but it throws away the work
already done for a case that is rare.
Move the leftover bytes again instead. The pass runs with a fresh data
reloc inode, because the mappings of the first pass are keyed by the same
logical offsets. Up to RELOC_MAX_LEFTOVER_PASSES extra passes are made.
If the block group still has used bytes after them, it is kept and the
balance fails with -EUCLEAN, with an error message naming the block group.
Signed-off-by: Ismael Luceno <ismael@iodev.co.uk>
---
fs/btrfs/relocation.c | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index 75300c21fe06e..9bf0f9bc794c4 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -5751,6 +5751,9 @@ static int do_nonremap_reloc(struct btrfs_fs_info *fs_info, bool verbose,
/*
* function to relocate all extents in a block group.
*/
+/* Extra passes over a block group that a reference left behind. */
+#define RELOC_MAX_LEFTOVER_PASSES 3
+
int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
bool verbose)
{
@@ -5873,7 +5876,30 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
btrfs_delete_unused_bgs(fs_info);
} else {
+ unsigned int pass = 0;
+
ret = do_nonremap_reloc(fs_info, verbose, rc);
+ /*
+ * A reference added during the update stage can survive it. Its
+ * bytes were not moved, so move what is left again with a fresh
+ * data inode. Only this rare case pays for another pass.
+ */
+ while (ret == -EUCLEAN && pass++ < RELOC_MAX_LEFTOVER_PASSES) {
+ iput(rc->data_inode);
+ rc->data_inode = create_reloc_inode(rc->block_group);
+ if (IS_ERR(rc->data_inode)) {
+ ret = PTR_ERR(rc->data_inode);
+ rc->data_inode = NULL;
+ break;
+ }
+ rc->stage = MOVE_DATA_EXTENTS;
+ rc->found_file_extent = false;
+ ret = do_nonremap_reloc(fs_info, verbose, rc);
+ }
+ if (ret == -EUCLEAN)
+ btrfs_err(fs_info,
+ "block group %llu kept: references still point into it after %d passes",
+ rc->block_group->start, RELOC_MAX_LEFTOVER_PASSES + 1);
}
out:
--
2.56.0
next prev 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 ` [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 ` Ismael Luceno [this message]
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-6-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®