From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
To: Ismael Luceno <ismael@iodev.co.uk>
Cc: linux-btrfs@vger.kernel.org, David Sterba <dsterba@suse.com>,
Chris Mason <mason@kernel.org>,
linux-kernel@vger.kernel.org, Qu Wenruo <wqu@suse.com>,
Anand Jain <asj@kernel.org>, Filipe Manana <fdmanana@suse.com>
Subject: Re: [PATCH 0/6] btrfs: trim unused space when relocating data extents
Date: Mon, 30 Nov 2026 01:52:13 -0500 [thread overview]
Message-ID: <aw0dHePYufjzt2-_@hungrycats.org> (raw)
In-Reply-To: <20261129133125.3293-1-ismael@iodev.co.uk>
On Sun, Nov 29, 2026 at 02:31:13PM +0100, Ismael Luceno wrote:
> A data balance copies every data extent whole, including bytes that no
> file references. Those dead bytes stay allocated, and an overwritten
> file keeps its old extents until every reference to them is gone.
>
> This series copies only the referenced bytes. For each old extent, the
> referenced ranges are collected and laid out in one new allocation, and
> each file extent is remapped to its own range. Dead bytes are not copied,
> so they are freed with the old block group.
Patch 4 walks the running transaction to find new refs, which needs the
tree mod log to be reliable, as in the LOGICAL_INO ioctl. Even when
it's working as designed, tree mod log is slow, and consumes increasing
amounts of memory as long as it exists. A patch in for-next adds a flag
to LOGICAL_INO_V2 to allow users to opt out of using the tree mod log.
Using only commit roots avoids all the bugs and most of the performance
issues associated with using the tree mod log--the missing tree mod log
sequence in these patches (as Sashiko pointed out), and two pre-existing
upstream that I know of.
Relocation already handles new reflinks using only the commit roots.
It runs a loop which picks up new refs that were added during each
UPDATE_DATA_PTRS pass. Each pass starts with a commit, so all delayed
refs in flight are committed, and can then be found in the commit root.
This repeats until there are no refs left in the commit root.
The transaction handle in patch 4 is still needed, to keep a commit from
switching the roots during the walk and the leaf scan, but it should not
be passed to the backref walk. relocate_block_group() already does this
for add_data_references() in UPDATE_DATA_PTRS.
New reflinks can only be created from reachable blocks, so any new
reflinks that appear should consist entirely of reachable blocks that
were copied in the MOVE_DATA_EXTENTS phase. Patch 3 already verifies
that: get_new_location() returns -ENOENT if the range was not copied, and
the CoW of the leaf aborts the transaction, before the loss of data can
become permanent. In theory, that situation will never happen.
In practice, there is a pre-existing bug that should show up when testing
reflinks and balance, because the locks held by a reflink don't block the
merge of reloc trees.
Reflinks are the only race between adding refs and balance I know of.
Snapshot metadata page CoW holds more locks, and it and the other cases
read the item and add the refs in a single transaction, or run during
log tree replay at mount.
btrfs_clone() copies a file extent item, and adds the new reference in a
transaction started later. If relocation merges the reloc tree in
between, the last reference of the extent X is gone before the reflink
adds its own:
Reflink | Relocation
------------------------------------+--------------------------------------
btrfs_clone() |
|- btrfs_search_slot() |
|- read_extent_buffer() |
| Item points to extent X |
|- btrfs_release_path() |
| No reference on X, no |
| transaction handle |
| | relocate_block_group()
| | |- merge_reloc_roots()
| | | \- merge_reloc_root()
| | | |- replace_path()
| | | | Source leaf replaced, the
| | | | old leaf pointing to X is
| | | | now in the reloc tree
| | | |
| | | \- invalidate_extent_cache()
| | | Source range is not
| | | locked, doesn't wait
| | |
| | |- btrfs_commit_current_transaction()
| | \- clean_dirty_subvols()
| | \- btrfs_drop_snapshot()
| | Reloc tree dropped, and the
| | last reference of X with it
| |
\- btrfs_replace_file_extents() |
\- btrfs_insert_replace_extent() |
\- btrfs_inc_extent_ref() |
X no longer exists. |
Too late. |
The reflink holds the i_mmap_lock of the source inode, but that only
stops replace_file_extents(), not replace_path().
Before commit 5d6f0e9890ed ("btrfs: stop locking the source extent range
during reflink"), invalidate_extent_cache() waited for the source range.
> Patches 2-6 support this change: a helper that preallocates one allocation
> for several file extents, NOCOW writeback into relocation extents, a scan
> of the running transaction for references, a retry for references that
> survive the update stage, and a group length derived from its members.
>
> Testing
>
> Measured in QEMU guests, one run per kernel. Base is f2805cb. Series is
> this six-patch series. Every run passed btrfs check. Timings are not
> reported, because they varied more between runs than between kernels.
>
> - Holes: a 64 MiB extent with 16 dead 1 MiB holes, then a balance. Data
> used 80 MiB -> 64 MiB (base -> series). Extents stay at 33.
> - Random overwrites: a 256 MiB file with 600 random 4 KiB overwrites, then
> a balance. Data used 258.32 MiB -> 258.32 MiB (base) and 258.32 MiB ->
> 256.00 MiB (series).
> - Read: a 64 MiB file written as 32 2 MiB chunks with 1 MiB overwritten in
> the middle of each, then a direct sequential read. Device read requests
> 136 -> 136 (base) and 136 -> 134 (series).
Test cases for compression and reflinks would be good too.
prev parent reply other threads:[~2026-11-30 6:52 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 13:31 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 ` [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
2026-11-30 1:20 ` Ismael Luceno
2026-11-30 4:43 ` Qu Wenruo
2026-11-30 6:52 ` Zygo Blaxell [this message]
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=aw0dHePYufjzt2-_@hungrycats.org \
--to=ce3g8jdj@umail.furryterror.org \
--cc=asj@kernel.org \
--cc=dsterba@suse.com \
--cc=fdmanana@suse.com \
--cc=ismael@iodev.co.uk \
--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®