]> git.hungrycats.org Git - bees/commitdiff
scan-next: restrict prefer-canonical-src pivot to strict whole-extent duplicates
authorZygo Blaxell <bees@furryterror.org>
Tue, 1 Sep 2026 23:46:05 +0000 (19:46 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 19:00:05 +0000 (15:00 -0400)
The pivot in BeesStartAsSrcPlan::finalize() re-centers the dedupe star
on a higher-ref candidate, and build_onto_center() then transposes the
other dsts' matches onto the center by swapping only the src extent
identity, keeping the start-extent offsets.  That transposition is
sound only when the center is byte-identical to the start at the same
offsets over the start's whole extent.

The eligibility test accepted any candidate whose own extent was
covered by a single dedupe op against the start.  A small candidate
fully matched by a window inside a larger start (or a larger candidate
covering the start elsewhere) qualifies under that test without
satisfying the transposition's premise.  Every match transposed onto
such a center indexes bytes the center never had; the resulting plans
execute as dedupe requests the kernel rejects with
BTRFS_SAME_DATA_DIFFERS ("NO Dedup!"), deterministically, on data
with self-similar or repeated content where windowed whole-candidate
coverage is common.  Diagnosed by stamping block maps with build
sequence numbers: bad matches carried a src map built under the start
extent's bytenr while naming the pivot center as m_src.

Tighten the test to the strict form: equal logical sizes and a
full-length match anchored at offset zero on both sides.

Assisted-by: Claude-Code:claude-opus-5
Assisted-by: Claude-Code:claude-opus-5-5
src/bees-scan-next.cc

index 7de53f10f63f68300468b9ff3225ced3c3716965..228b61858f83c57625198dc0bd29fd8d1b800df4 100644 (file)
@@ -2604,11 +2604,34 @@ BeesStartAsSrcPlan::process_candidate(
        Impl::Entry entry;
        entry.m_candidate = candidate;
        entry.m_matches = matches->m_candidate_as_dst;
-       // A single dedupe op covering the whole extent (no copy or hole) means the
-       // candidate is a whole-extent duplicate of the start — eligible to become
-       // the star's center in finalize() (prefer-canonical-src).
-       entry.m_whole_extent =
-               chosen->rewrite_ops() == 1 && chosen->dedupe_ops() == 1;
+       // A single dedupe op covering the whole extent (no copy or hole) means
+       // the candidate is fully covered by one match run against the start.
+       // That alone is NOT the whole-extent-duplicate property finalize()
+       // needs: build_onto_center() transposes other dsts' matches onto the
+       // center with their start-extent offsets preserved, which is only
+       // sound when the center is byte-identical to the start at the same
+       // offsets over the start's whole extent.  Require the strict form:
+       // equal logical sizes and a full-length match anchored at offset zero
+       // on both sides.  A candidate merely covered by a window inside a
+       // larger start (or a larger candidate covering the start elsewhere)
+       // must not become the center — transposed matches would index bytes
+       // the center never had, and each such plan executes as a dedupe
+       // request the kernel rejects with BTRFS_SAME_DATA_DIFFERS.
+       entry.m_whole_extent = false;
+       if (chosen->rewrite_ops() == 1 && chosen->dedupe_ops() == 1) {
+               auto &layer = Borrower::current().layer();
+               const auto start_ls = m_start.logical_size(layer);
+               const auto cand_ls = candidate.logical_size(layer);
+               if (start_ls == cand_ls) {
+                       for (const auto &m : entry.m_matches) {
+                               if (m.m_dst_begin == 0 && m.m_dst_end == cand_ls &&
+                                   m.m_src_begin == 0 && m.m_src_end == start_ls) {
+                                       entry.m_whole_extent = true;
+                                       break;
+                               }
+                       }
+               }
+       }
        entry.m_entry_cost.m_bytes_freed =
                static_cast<uint64_t>(chosen->good_blocks())
                * static_cast<uint64_t>(BLOCK_SIZE_SUMS);