From 6945af3e19a9c3ee52229463de4c136ebca44a7e Mon Sep 17 00:00:00 2001
From: Luke Shumaker <lukeshu@lukeshu.com>
Date: Sun, 8 Jan 2023 18:02:07 -0700
Subject: rebuildnodes/btrees: Move cache-flush code from tree.go to forrest.go

---
 lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/forrest.go | 11 ++++++++++-
 lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/tree.go    |  7 +------
 2 files changed, 11 insertions(+), 7 deletions(-)

(limited to 'lib')

diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/forrest.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/forrest.go
index 1d3b693..4f4e05d 100644
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/forrest.go
+++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/forrest.go
@@ -112,7 +112,7 @@ func (ts *RebuiltForrest) addTree(ctx context.Context, treeID btrfsprim.ObjID, s
 	defer func() {
 		if !ok {
 			// Store a negative cache of this.  tree.AddRoot() for the ROOT or UUID
-			// trees will invalidate the negative cache.
+			// trees will call .flushNegativeCache().
 			ts.trees.Store(treeID, nil)
 		}
 	}()
@@ -177,6 +177,15 @@ func (ts *RebuiltForrest) addTree(ctx context.Context, treeID btrfsprim.ObjID, s
 	return true
 }
 
+func (ts *RebuiltForrest) flushNegativeCache() {
+	ts.trees.Range(func(treeID btrfsprim.ObjID, tree *RebuiltTree) bool {
+		if tree == nil {
+			ts.trees.Delete(treeID)
+		}
+		return true
+	})
+}
+
 // ListRoots returns a listing of all initialized trees and their root
 // nodes.
 //
diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/tree.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/tree.go
index b13bb98..d64d7d2 100644
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/tree.go
+++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/tree.go
@@ -303,12 +303,7 @@ func (tree *RebuiltTree) AddRoot(ctx context.Context, rootNode btrfsvol.LogicalA
 	tree.forrest.excItems.Delete(tree.ID) // force re-gen
 
 	if (tree.ID == btrfsprim.ROOT_TREE_OBJECTID || tree.ID == btrfsprim.UUID_TREE_OBJECTID) && stats.AddedItems > 0 {
-		tree.forrest.trees.Range(func(otherTreeID btrfsprim.ObjID, otherTree *RebuiltTree) bool {
-			if otherTree == nil {
-				tree.forrest.trees.Delete(otherTreeID)
-			}
-			return true
-		})
+		tree.forrest.flushNegativeCache()
 	}
 }
 
-- 
cgit v1.2.3-2-g168b