PlanCost m_total_cost;
};
+/// Enforce the once-only rule at plan composition: each src block may
+/// be referenced at most once across the dedupe edges of a single dst
+/// plan. Overlapping src references within one plan stack multiple
+/// new refs onto the same physical blocks, which explodes reference
+/// counts on extremely common data; every observed violation has also
+/// been a planner bug pairing a dst with data the src never had
+/// (match transposition onto a non-identical src). Throws to reject
+/// the plan so it can never reach the kernel; callers log the
+/// exception and skip the extent.
+static void
+scan_next_enforce_src_once_only(
+ const BeesExtent &dst,
+ const vector<ExtentMatch> &matches)
+{
+ BEESTRACE("scan_next_enforce_src_once_only dst " << to_hex(dst.bytenr()));
+ // Key src extents by bytenr when they have one, node identity
+ // otherwise (synthetic extents), then check the src intervals
+ // under each key for pairwise overlap.
+ map<pair<uint64_t, const void *>, vector<pair<uint64_t, uint64_t>>> src_ranges;
+ for (const auto &m : matches) {
+ const auto bytenr = m.m_src.bytenr_opt();
+ const auto key = bytenr
+ ? make_pair(*bytenr, static_cast<const void *>(nullptr))
+ : make_pair(uint64_t(0),
+ static_cast<const void *>(m.m_src.extent_sp().get()));
+ src_ranges[key].emplace_back(m.m_src_begin, m.m_src_end);
+ }
+ for (auto &p : src_ranges) {
+ auto &ranges = p.second;
+ sort(ranges.begin(), ranges.end());
+ for (size_t i = 1; i < ranges.size(); ++i) {
+ if (ranges[i].first >= ranges[i - 1].second) {
+ continue;
+ }
+ BEESCOUNT(plan_src_overlap);
+ BEESLOGERR("once-only rule violation: dst "
+ << to_hex(dst.bytenr()) << " plan references src blocks more than once");
+ for (const auto &m : matches) {
+ BEESLOGERR(" match dst [" << to_hex(m.m_dst_begin)
+ << ".." << to_hex(m.m_dst_end)
+ << ") src [" << to_hex(m.m_src_begin)
+ << ".." << to_hex(m.m_src_end)
+ << ") src extent " << m.m_src);
+ }
+ THROW_ERROR(runtime_error,
+ "once-only rule violation: dst " << to_hex(dst.bytenr())
+ << " src ranges [" << to_hex(ranges[i - 1].first)
+ << ".." << to_hex(ranges[i - 1].second)
+ << ") and [" << to_hex(ranges[i].first)
+ << ".." << to_hex(ranges[i].second)
+ << ") overlap");
+ }
+ }
+}
+
/// Try to build a DstPlan for one dst extent given candidate sources.
/// Returns nullopt if the plan is not viable (no matches, cost too
/// high, or choose_match_plan rejects).
[](const auto &lhs, const auto &rhs) {
return lhs.m_dst_begin < rhs.m_dst_begin;
});
+ scan_next_enforce_src_once_only(dst, chosen->m_selected_matches);
+
const auto plan_tree = scan_next_compose_plan_tree(
dst, chosen->m_selected_matches);