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);