vector<BeesRangePair>
scan_next_build_tree_pairs(BeesContext &ctx, const BeesExtent &prepared,
- const BeesExtent &src_tree, optional<bool> want_prealloc = {})
+ const BeesExtent &src_tree, uint64_t reachable_size,
+ optional<bool> want_prealloc = {})
{
BEESNOTE("scan_next build_pairs " << to_hex(prepared.bytenr()) << (want_prealloc ? " prealloc" : " data"));
BEESTRACE("scan_next build_pairs " << to_hex(prepared.bytenr()) << (want_prealloc ? " prealloc" : " data"));
auto &layer = Borrower::current().layer();
const auto lsize = prepared.logical_size(layer);
- BEESTRACE("build dst scan_next_build_all_ref_slices, lsize = " << lsize << ", prepared extent = " << prepared);
+ // Enumerate dst refs only over the reachable extent. A btrfs
+ // EXTENT_DATA num_bytes is sector-aligned, so a ref spans into the
+ // past-EOF padding of its last block even though no valid file data
+ // lives there; clamping to reachable_size (the end of the last
+ // non-unreachable region = the mid-block EOF) keeps the sweep-join off
+ // that padding, so the past-EOF tail carries no ref on either side and
+ // needs no backing temp (its plan-tree hole is left bare).
+ // reachable_size == lsize in the common case (no trailing unreachable
+ // region), leaving behavior unchanged.
+ const auto dst_span = min(reachable_size, lsize);
+ BEESTRACE("build dst scan_next_build_all_ref_slices, lsize = " << lsize
+ << ", dst_span = " << dst_span << ", prepared extent = " << prepared);
// Build ALL dst ref slices from raw back-references so that
// every file reference to the dst extent gets its own
// FIDEDUPERANGE call. The old path used refs() which calls
// bees_visit_refs_impl and picks one "best" ref per position,
// missing partial refs that overlap with a full ref.
- const auto dst_refs = scan_next_build_all_ref_slices(ctx, prepared, lsize);
+ const auto dst_refs = scan_next_build_all_ref_slices(ctx, prepared, dst_span);
BEESTRACE("sweep_join_pairs");
return scan_next_sweep_join_pairs(ctx, dst_refs, src_tree, want_prealloc);
/// EOF. The backing hole temp is sized to reachable_size so the
/// EOF-tail hole's dedupe src ends at the temp's own EOF
/// (src_end == src_fsize). Hole parts beyond reachable_size are the
-/// past-EOF remainder: they are backed by their own separate zero
-/// temps (so their refs stay openable for the executor) and produce
-/// no dedupe pair, since the dst has no ref past its own EOF — a
-/// covered no-op that keeps the rebuilt tree spanning the full dst
-/// extent. Equals logical_size in the common case (no non-data
-/// mid-block EOF), giving the prior behavior.
+/// past-EOF remainder: they are left as bare HoleExtents (no backing
+/// temp), since the pair builder clamps dst-ref enumeration to
+/// reachable_size and never queries them — a ref-less no-op that keeps
+/// the rebuilt tree spanning the full dst extent. Equals logical_size
+/// in the common case (no non-data mid-block EOF), giving the prior
+/// behavior.
BeesExtent
scan_next_back_holes_with_temp(BeesContext &ctx, const BeesExtent &plan_tree,
uint64_t reachable_size, bool datacow)
return scan_next_make_temp_hole_tree(ctx, logical_size, datacow);
}
- // Create a temp hole extent covering only the reachable extent.
- const auto hole_temp = scan_next_make_temp_hole_tree(ctx, reachable_size, datacow);
+ // Shared temp hole covering the reachable extent, created lazily: an
+ // extent whose only hole is the past-EOF tail (kept bare below) then
+ // allocates no temp at all — the common shape for a single all-data
+ // block with a mid-block EOF. Its end == reachable_size, so a hole
+ // whose m_end == reachable_size (the EOF-tail hole) dedupes with
+ // src_end == src_fsize.
+ optional<BeesExtent> hole_temp;
+ const auto reachable_hole_temp = [&]() -> const BeesExtent & {
+ if (!hole_temp) {
+ hole_temp = scan_next_make_temp_hole_tree(ctx, reachable_size, datacow);
+ }
+ return *hole_temp;
+ };
// The materialized tree is a SuperExtent whose parts are
// action nodes (PostCopyExtent, DedupExtent, HoleExtent).
- // Replace HoleExtent parts with slices of the temp hole file.
- // Non-hole parts are preserved as-is.
+ // Replace reachable HoleExtent parts with slices of the temp hole
+ // file. Non-hole parts and the past-EOF tail are preserved as-is.
//
// We can't use visit() or tree_walk here because they remap
// child coordinates to the parent's space. Instead, iterate
// Reachable hole — replace with a slice of the shared
// reachable hole temp. m_begin/m_end are in parent
// space, which is where the hole sits in the temp file.
- // The temp file ends at reachable_size, so a hole whose
- // m_end == reachable_size (the EOF-tail hole) dedupes
- // with src_end == src_fsize.
parts.push_back(BeesTreeSuperPart {
- .m_child = BeesExtentSlice(hole_temp,
+ .m_child = BeesExtentSlice(reachable_hole_temp(),
part.m_begin, part.m_end),
.m_begin = part.m_begin,
.m_end = part.m_end,
});
} else {
// Past-EOF hole beyond reachable_size (the unreachable
- // remainder of a non-data region that held the file's
- // mid-block EOF). Back it with its own separate zero
- // temp rather than the shared reachable temp: the latter
- // ends at reachable_size, so slicing it here would run
- // past its end. A dedicated temp keeps the hole's refs
- // openable (the executor opens every part's refs) while
- // leaving the shared temp's EOF exactly at reachable_size.
- // The dst has no ref past its own EOF, so this part
- // produces no dedupe pair — a covered no-op.
- const auto part_size = part.m_end - part.m_begin;
- const auto past_eof_temp =
- scan_next_make_temp_hole_tree(ctx, part_size, datacow);
- parts.push_back(BeesTreeSuperPart {
- .m_child = BeesExtentSlice(past_eof_temp,
- 0, part_size),
- .m_begin = part.m_begin,
- .m_end = part.m_end,
- });
+ // remainder of a region that held the file's mid-block
+ // EOF). It carries no valid file data and the pair builder
+ // clamps dst-ref enumeration to reachable_size, so this part
+ // is never queried for a source and needs no backing temp.
+ // Keep it as the bare HoleExtent so the rebuilt tree still
+ // spans the full dst extent (the src_size == dst_lsize
+ // check) — a ref-less no-op.
+ parts.push_back(part);
}
}
THROW_CHECK2(runtime_error, src_size, dst_lsize,
src_size == dst_lsize);
- // Build dedupe pairs: prealloc first, then data.
+ // Build dedupe pairs: prealloc first, then data. Pass reachable_size
+ // so dst-ref enumeration stops at the mid-block EOF and never demands a
+ // source for the bare past-EOF tail hole.
auto prealloc_pairs = scan_next_build_tree_pairs(*ctx, dst,
- src_tree, true);
+ src_tree, reachable_size, true);
auto data_pairs = scan_next_build_tree_pairs(*ctx, dst,
- src_tree, false);
+ src_tree, reachable_size, false);
BEESLOGC(DEBUG, Plan, "scan_next_execute_dst_plan dst "
<< to_hex(dst.bytenr())