]> git.hungrycats.org Git - bees/commitdiff
extent-layer: do not hash refs that point into a temp file
authorZygo Blaxell <bees@furryterror.org>
Tue, 1 Sep 2026 03:16:28 +0000 (23:16 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 19:00:05 +0000 (15:00 -0400)
A temp file's extents are ordinary btrfs extents, so an extent scan can
pick one up like any other.  That happens when a temp file outlives a
transaction boundary and reaches the scan queue, and it was observed on
a 176G test corpus: an extent entered the scan queue as a start_extent
whose only ref named a temp file inode at offset 0x1000, the reserved
first block temp files are truncated back to.  The same ref claimed
19.699M of an 8.824M extent, the inconsistent-ref-coverage shape.

Refs into temp files do not stay valid.  resize() truncates the file
back to its reserved block and the space is rewritten with unrelated
data on the next use.  No extent Exclusion covers that: the mutation is
a truncate of a file, not a dedupe of the locked extent, so holding the
extent lock does not prevent the refs from going stale underneath.
block_map_fetch then preads whatever the next user of the temp file
wrote and publishes those hashes under the scanned extent's bytenr.

Every temp file is already registered on the blacklist by its
BeesFileId, so recognising these refs costs a lookup.  Skip them in the
data-coverage pass: they add no coverage, and pass 2 does not pread a
range with no usable ref.

Assisted-by: Claude-Code:claude-opus-5
src/bees-extent-layer.cc

index fd30cd2bea0b0591fecc69b5a555095bd8898e67..90c10dcbaa877fa039efaebc0efd26ca7d3c282e 100644 (file)
@@ -644,6 +644,22 @@ BeesBtrfsExtentLayer::block_map_fetch(BeesBtrfsExtentNode &node,
                const auto first = ranged_cast<size_t>(ext_begin / BLOCK_SIZE_SUMS);
                const auto last = ranged_cast<size_t>(
                        (ext_end + BLOCK_SIZE_SUMS - 1) / BLOCK_SIZE_SUMS);
+               // A temp file's extents are ordinary btrfs extents and an extent
+               // scan can pick them up like any other -- a temp file that
+               // outlives a transaction boundary reaches the scan queue.  That
+               // matters because temp files are recycled: resize() truncates
+               // back to the reserved block and the space is rewritten with
+               // unrelated data.  No extent Exclusion covers that, since the
+               // mutation is a truncate of a file rather than a dedupe of the
+               // locked extent, so a ref into a recycled temp file reads
+               // whatever the next user wrote.  Every temp file is on the
+               // blacklist, so skip those refs instead of hashing them: they
+               // contribute no data coverage and pass 2 never preads them.
+               if (m_ctx.is_blacklisted(BeesFileId(ref.root(), ref.inum()))) {
+                       BEESCOUNT(block_map_ref_tempfile);
+                       continue;
+               }
+
                if (*prealloc) {
                        for (size_t i = first; i < min(last, block_count); ++i) {
                                if (blocks[i].state == BeesTreeBlockState::unreachable) {