]> git.hungrycats.org Git - bees/commitdiff
extent-layer: merge repaired refs through the node interface
authorZygo Blaxell <bees@furryterror.org>
Tue, 1 Sep 2026 15:46:39 +0000 (11:46 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 19:00:05 +0000 (15:00 -0400)
repair_refs() reached for the concrete node type to read and publish
m_refs:

auto *node = dynamic_cast<BeesBtrfsExtentNode *>(extent.extent_sp().get());

The read never needed it -- refs() and refs(BeesExtentLayer &) are both
virtual on BeesBaseExtentNode.  Only publishing a merged list had no
base-class expression, so add one: merge_refs() appends refs whose
(root, inum, offset) the node does not already hold, under the node's
own lock, and returns how many were added.  Node types with no btrfs ref
list inherit a no-op, which is the right answer for them rather than
something a cast has to exclude by returning early.

Constructing the ref nodes stays in the layer.  The donors describe file
ranges but their nodes still name the dst extent they came from, so each
one is rebased onto this extent first, and setting a ref node's identity
is the layer's privilege -- it is the friend that refs_fetch() uses for
exactly this.

No functional change.

Assisted-by: Claude-Code:claude-opus-5
src/bees-extent-layer.cc
src/bees-extent-tree.cc
src/bees-extent-tree.h

index aed8fc41a7e2c2a4c7b99af491d527665b42c4aa..91f5a102c5f1811401df1267239b654f9d69de25 100644 (file)
@@ -1007,63 +1007,46 @@ BeesBtrfsExtentLayer::invalidate_refs(const BeesExtent &extent)
 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
index eb7cd4eed3c3dbaca75935ecd421a30045525340..fa3cbc92a0052b0b3aa297f87ce171aa3491e4ca 100644 (file)
@@ -1467,6 +1467,38 @@ BeesBtrfsExtentNode::mark_destroyed()
        m_destroyed = true;
 }
 
+size_t
+BeesBtrfsExtentNode::merge_refs(const vector<BeesRef> &new_refs)
+{
+       unique_lock<recursive_mutex> lock(m_lazy_mutex);
+       auto current = atomic_load(&m_refs);
+       vector<BeesRef> merged;
+       if (current) {
+               merged = *current;
+       }
+       size_t added = 0;
+       for (const auto &nr : new_refs) {
+               bool present = false;
+               for (const auto &have : merged) {
+                       if (have.root() == nr.root() && have.inum() == nr.inum() &&
+                           have.offset() == nr.offset()) {
+                               present = true;
+                               break;
+                       }
+               }
+               if (present) {
+                       continue;
+               }
+               merged.push_back(nr);
+               ++added;
+       }
+       if (added) {
+               atomic_store(&m_refs,
+                       make_shared<const vector<BeesRef>>(std::move(merged)));
+       }
+       return added;
+}
+
 bool
 BeesBtrfsExtentNode::destroyed() const
 {
index 223b9046b6ce86796646e9e407aecdd26e642044..6b4bb5276324960e24f03c17a601ca120732ec6d 100644 (file)
@@ -548,6 +548,13 @@ public:
        /// 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
@@ -809,6 +816,8 @@ public:
        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;