void
BeesBtrfsExtentLayer::repair_refs(const BeesExtent &extent, const vector<BeesRef> &donors)
{
- const auto bytenr_o = extent.bytenr_opt();
- if (!bytenr_o || donors.empty()) {
- return;
- }
- auto *node = dynamic_cast<BeesBtrfsExtentNode *>(extent.extent_sp().get());
- if (!node) {
+ if (donors.empty() || !extent.bytenr_opt()) {
return;
}
- // Refetch first so the repair is applied on top of whatever btrfs is
- // willing to report right now.
- auto fetched = atomic_load(&node->m_refs);
- if (!fetched) {
- fetched = refs_fetch(*node);
- }
+ // Populate the list first, so the repair is applied on top of whatever
+ // btrfs is willing to report right now. Virtual: fetches on a miss.
+ extent.refs(*this);
// LOGICAL_INO answers from the commit root whenever it cannot attach
// to a running transaction (fs/btrfs/backref.c: btrfs_attach_transaction
// returns -ENOENT, find_parent_nodes then sets search_commit_root and
// skips delayed refs). A dedupe performed since the last commit is
// therefore invisible to a refetch that happens to land in a lull, and
- // nothing in the result distinguishes that case from a genuinely
- // short ref list. We know what we just created, so do not ask: the
- // donors are the dst refs the dedupe redirected onto this extent.
- // Adding one that btrfs already reported is harmless (skipped as a
- // duplicate); adding one belonging to a different src is harmless too,
- // because extent_data_fetch leaves a foreign ref undetermined and
- // every caller skips it.
- auto merged = *fetched;
- size_t added = 0;
+ // nothing in the result distinguishes that case from a genuinely short
+ // ref list. We know what we just created, so do not ask: the donors
+ // are the dst refs the dedupe redirected onto this extent. Adding one
+ // btrfs already reported is skipped as a duplicate; adding one
+ // belonging to a different src is harmless, because extent_data_fetch
+ // leaves a foreign ref undetermined and every caller skips it.
+ //
+ // The donors describe file ranges, but their ref nodes still name the
+ // dst extent they came from, so rebase each onto this extent before
+ // handing it over. Building ref nodes is the layer's job -- it is the
+ // friend that may set their identity, exactly as refs_fetch does.
+ auto ext_sp = extent.extent_sp();
+ vector<BeesRef> rebased;
+ rebased.reserve(donors.size());
for (const auto &donor : donors) {
- const auto root = donor.root();
- const auto inum = donor.inum();
- const auto offset = donor.offset();
- bool present = false;
- for (const auto &have : merged) {
- if (have.root() == root && have.inum() == inum &&
- have.offset() == offset) {
- present = true;
- break;
- }
- }
- if (present) {
- continue;
- }
- auto ref_node = make_shared<BeesBtrfsRefNode>(root, inum, offset);
- ref_node->m_bytenr = *bytenr_o;
- ref_node->m_extent = extent.extent_sp();
- merged.push_back(BeesRef(static_pointer_cast<BeesBaseRefNode>(ref_node)));
- ++added;
- }
- if (!added) {
- return;
+ auto ref_node = make_shared<BeesBtrfsRefNode>(
+ donor.root(), donor.inum(), donor.offset());
+ ref_node->m_bytenr = extent.bytenr();
+ ref_node->m_extent = ext_sp;
+ rebased.push_back(BeesRef(static_pointer_cast<BeesBaseRefNode>(ref_node)));
}
+
+ // The node owns its ref list and does the merge under its own lock,
+ // skipping any identity it already holds. Node types with no btrfs
+ // ref list inherit a no-op.
+ const auto added = ext_sp->merge_refs(rebased);
BEESCOUNTADD(ref_repaired, added);
- atomic_store(&node->m_refs,
- make_shared<const vector<BeesRef>>(std::move(merged)));
}
void
/// block map, and scalar identity intact. Default: no-op.
virtual void invalidate_refs() {}
+ /// Add @p donors to this node's cached ref list, skipping any whose
+ /// (root, inum, offset) is already present, and return how many were
+ /// added. Used to restore refs a dedupe created that a refetch does
+ /// not report. Default: no-op returning 0, for node types that hold
+ /// no btrfs ref list of their own.
+ virtual size_t merge_refs(const vector<BeesRef> &) { return 0; }
+
/// Mark this extent retired: a dedupe consumed it as dst, so its
/// refs were redirected to the src and it must take no further part
/// in dedupe. Retirement is one-way, and deliberately does NOT drop
void mark_destroyed() override;
bool destroyed() const override;
+ size_t merge_refs(const vector<BeesRef> &donors) override;
+
ssize_t pread(BeesExtentLayer *layerp, void *buf, size_t len,
off_t logical_offset, const BeesPreadLogFn &log_fn = {}) const override;