summaryrefslogtreecommitdiff
path: root/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-28 14:05:27 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-03-14 19:45:07 -0600
commit8c8c6c27552f8554ba014c34d684cb90538ef65b (patch)
treef3a53ed194e29b516b52770e4949a1e508fad6a7 /lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees
parent34bf167ef33c57b4d6767273f1d265971a4693b9 (diff)
Move files around [ci-skip]
Diffstat (limited to 'lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees')
-rw-r--r--lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/forrest.go208
-rw-r--r--lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/nestedlock.go45
-rw-r--r--lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/tree.go357
3 files changed, 0 insertions, 610 deletions
diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/forrest.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/forrest.go
deleted file mode 100644
index dbbc6eb..0000000
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/forrest.go
+++ /dev/null
@@ -1,208 +0,0 @@
-// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
-//
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-package btrees
-
-import (
- "context"
-
- "github.com/datawire/dlib/dlog"
-
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsprim"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfstree"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
- pkggraph "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect/rebuildnodes/graph"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio"
- "git.lukeshu.com/btrfs-progs-ng/lib/containers"
- "git.lukeshu.com/btrfs-progs-ng/lib/slices"
- "git.lukeshu.com/btrfs-progs-ng/lib/textui"
-)
-
-type Callbacks interface {
- AddedItem(ctx context.Context, tree btrfsprim.ObjID, key btrfsprim.Key)
- AddedRoot(ctx context.Context, tree btrfsprim.ObjID, root btrfsvol.LogicalAddr)
- LookupRoot(ctx context.Context, tree btrfsprim.ObjID) (offset btrfsprim.Generation, item btrfsitem.Root, ok bool)
- LookupUUID(ctx context.Context, uuid btrfsprim.UUID) (id btrfsprim.ObjID, ok bool)
-}
-
-// RebuiltForrest is an abstraction for rebuilding and accessing
-// potentially broken btrees.
-//
-// It is conceptually a btrfstree.TreeOperator, and adds similar
-// broken-tree handling to btrfsutil.BrokenForrest. However, the API
-// is different thant btrfstree.TreeOperator, and is much more
-// efficient than btrfsutil.BrokenForrest.
-//
-// The efficiency improvements are possible because of the API
-// differences, which are necessary for how it is used in
-// rebuildnodes:
-//
-// - it consumes an already-read graph.Graph instead of reading the
-// graph itself
-//
-// - it does not use `btrfstree.TreePath`
-//
-// - it does not keep track of errors encountered in a tree
-//
-// Additionally, it provides some functionality that
-// btrfsutil.BrokenForrest does not:
-//
-// - it provides a .LeafToRoots() method to advise on what
-// additional roots should be added
-//
-// - it provides a .COWDistance() method to compare how related two
-// trees are
-//
-// A zero RebuiltForrest is invalid; it must be initialized with
-// NewRebuiltForrest().
-type RebuiltForrest struct {
- // static
- sb btrfstree.Superblock
- graph pkggraph.Graph
- keyIO *keyio.Handle
- cb Callbacks
-
- // mutable
- treesMu nestedMutex
- trees map[btrfsprim.ObjID]*RebuiltTree // must hold .treesMu to access
- leafs containers.ARCache[btrfsprim.ObjID, map[btrfsvol.LogicalAddr]containers.Set[btrfsvol.LogicalAddr]]
- incItems containers.ARCache[btrfsprim.ObjID, *itemIndex]
- excItems containers.ARCache[btrfsprim.ObjID, *itemIndex]
-}
-
-// NewRebuiltForrest returns a new RebuiltForrest instance. All of
-// the callbacks must be non-nil.
-func NewRebuiltForrest(sb btrfstree.Superblock, graph pkggraph.Graph, keyIO *keyio.Handle, cb Callbacks) *RebuiltForrest {
- return &RebuiltForrest{
- sb: sb,
- graph: graph,
- keyIO: keyIO,
- cb: cb,
-
- trees: make(map[btrfsprim.ObjID]*RebuiltTree),
- leafs: containers.ARCache[btrfsprim.ObjID, map[btrfsvol.LogicalAddr]containers.Set[btrfsvol.LogicalAddr]]{
- MaxLen: textui.Tunable(8),
- },
- incItems: containers.ARCache[btrfsprim.ObjID, *itemIndex]{
- MaxLen: textui.Tunable(8),
- },
- excItems: containers.ARCache[btrfsprim.ObjID, *itemIndex]{
- MaxLen: textui.Tunable(8),
- },
- }
-}
-
-// Tree returns a given tree, initializing it if nescessary. If it is
-// unable to initialize the tree, then nil is returned, and nothing is
-// done to the forrest.
-//
-// The tree is initialized with the normal root node of the tree.
-func (ts *RebuiltForrest) Tree(ctx context.Context, treeID btrfsprim.ObjID) *RebuiltTree {
- ctx = ts.treesMu.Lock(ctx)
- defer ts.treesMu.Unlock()
- if !ts.addTree(ctx, treeID, nil) {
- return nil
- }
- return ts.trees[treeID]
-}
-
-func (ts *RebuiltForrest) addTree(ctx context.Context, treeID btrfsprim.ObjID, stack []btrfsprim.ObjID) (ok bool) {
- if tree, ok := ts.trees[treeID]; ok {
- return tree != nil
- }
- defer func() {
- if !ok {
- // Store a negative cache of this. tree.AddRoot() for the ROOT or UUID
- // trees will call .flushNegativeCache().
- ts.trees[treeID] = nil
- }
- }()
- stack = append(stack, treeID)
- ctx = dlog.WithField(ctx, "btrfsinspect.rebuild-nodes.rebuild.add-tree", stack)
- dlog.Info(ctx, "adding tree...")
- if slices.Contains(treeID, stack[:len(stack)-1]) {
- dlog.Errorf(ctx, "failed to add tree: loop detected: %v", stack)
- return false
- }
-
- tree := &RebuiltTree{
- ID: treeID,
- Roots: make(containers.Set[btrfsvol.LogicalAddr]),
- forrest: ts,
- }
- var root btrfsvol.LogicalAddr
- switch treeID {
- case btrfsprim.ROOT_TREE_OBJECTID:
- root = ts.sb.RootTree
- case btrfsprim.CHUNK_TREE_OBJECTID:
- root = ts.sb.ChunkTree
- case btrfsprim.TREE_LOG_OBJECTID:
- root = ts.sb.LogTree
- case btrfsprim.BLOCK_GROUP_TREE_OBJECTID:
- root = ts.sb.BlockGroupRoot
- default:
- if !ts.addTree(ctx, btrfsprim.ROOT_TREE_OBJECTID, stack) {
- dlog.Error(ctx, "failed to add tree: add ROOT_TREE")
- return false
- }
- rootOff, rootItem, ok := ts.cb.LookupRoot(ctx, treeID)
- if !ok {
- dlog.Error(ctx, "failed to add tree: lookup ROOT_ITEM")
- return false
- }
- root = rootItem.ByteNr
- tree.UUID = rootItem.UUID
- if rootItem.ParentUUID != (btrfsprim.UUID{}) {
- tree.ParentGen = rootOff
- if !ts.addTree(ctx, btrfsprim.UUID_TREE_OBJECTID, stack) {
- return false
- }
- parentID, ok := ts.cb.LookupUUID(ctx, rootItem.ParentUUID)
- if !ok {
- dlog.Error(ctx, "failed to add tree: lookup UUID")
- return false
- }
- if !ts.addTree(ctx, parentID, stack) {
- dlog.Error(ctx, "failed to add tree: add parent tree")
- return false
- }
- tree.Parent = ts.trees[parentID]
- }
- }
-
- ts.trees[treeID] = tree
- if root != 0 {
- tree.AddRoot(ctx, root)
- }
-
- return true
-}
-
-func (ts *RebuiltForrest) flushNegativeCache(ctx context.Context) {
- _ = ts.treesMu.Lock(ctx)
- defer ts.treesMu.Unlock()
- for treeID, tree := range ts.trees {
- if tree == nil {
- delete(ts.trees, treeID)
- }
- }
-}
-
-// ListRoots returns a listing of all initialized trees and their root
-// nodes.
-//
-// Do not mutate the set of roots for a tree; it is a pointer to the
-// RebuiltForrest's internal set!
-func (ts *RebuiltForrest) ListRoots(ctx context.Context) map[btrfsprim.ObjID]containers.Set[btrfsvol.LogicalAddr] {
- _ = ts.treesMu.Lock(ctx)
- defer ts.treesMu.Unlock()
- ret := make(map[btrfsprim.ObjID]containers.Set[btrfsvol.LogicalAddr])
- for treeID, tree := range ts.trees {
- if tree != nil {
- ret[treeID] = tree.Roots
- }
- }
- return ret
-}
diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/nestedlock.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/nestedlock.go
deleted file mode 100644
index c1ffa18..0000000
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/nestedlock.go
+++ /dev/null
@@ -1,45 +0,0 @@
-// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com>
-//
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-package btrees
-
-import (
- "context"
- "sync"
-)
-
-// A nestedMutex is like a sync.Mutex, but while it is locked by call
-// 'A', may be simultaneously locked by subsequent calls if the
-// subsequent calls use a Context descended from the one returned by
-// the 'A' call to .Lock().
-type nestedMutex struct {
- inner sync.Mutex
- depth int
-}
-
-type nestedMutexCtxKey struct{}
-
-// Lock locks the mutex. It is invalid to use a Context returned from
-// Lock in a different goroutine than the one it was created in. It
-// is invalid to use a Context returned from Lock after the mutex has
-// subsequently become unlocked.
-func (m *nestedMutex) Lock(ctx context.Context) context.Context {
- if other, ok := ctx.Value(nestedMutexCtxKey{}).(*nestedMutex); ok && other == m {
- m.depth++
- return ctx
- }
- m.inner.Lock()
- return context.WithValue(ctx, nestedMutexCtxKey{}, m)
-}
-
-// Unlock unlocks the mutex. It is invalid to call Unlock if the
-// mutex is not already locked. It is invalid to call Unlock from
-// multiple goroutines simultaneously.
-func (m *nestedMutex) Unlock() {
- if m.depth > 0 {
- m.depth--
- } else {
- m.inner.Unlock()
- }
-}
diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/tree.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/tree.go
deleted file mode 100644
index 39d8871..0000000
--- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/btrees/tree.go
+++ /dev/null
@@ -1,357 +0,0 @@
-// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
-//
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-package btrees
-
-import (
- "context"
- "fmt"
- "sync"
- "time"
-
- "github.com/datawire/dlib/dlog"
-
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsprim"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio"
- "git.lukeshu.com/btrfs-progs-ng/lib/containers"
- "git.lukeshu.com/btrfs-progs-ng/lib/maps"
- "git.lukeshu.com/btrfs-progs-ng/lib/slices"
- "git.lukeshu.com/btrfs-progs-ng/lib/textui"
-)
-
-type RebuiltTree struct {
- // static
- ID btrfsprim.ObjID
- UUID btrfsprim.UUID
- Parent *RebuiltTree
- ParentGen btrfsprim.Generation // offset of this tree's root item
- forrest *RebuiltForrest
-
- // mutable
- mu sync.RWMutex
- Roots containers.Set[btrfsvol.LogicalAddr]
- // There are 3 more mutable "members" that are protected by
- // `mu`; but they live in a shared LRUcache. They are all
- // derived from tree.Roots, which is why it's OK if they get
- // evicted.
- //
- // 1. tree.leafToRoots() = tree.forrest.leafs.Load(tree.ID)
- // 2. tree.Items() = tree.forrest.incItems.Load(tree.ID)
- // 3. tree.PotentialItems() = tree.forrest.excItems.Load(tree.ID)
-}
-
-// LRU member 1: .leafToRoots() ////////////////////////////////////////////////////////////////////////////////////////
-
-// leafToRoots returns all leafs (lvl=0) in the filesystem that pass
-// .isOwnerOK, whether or not they're in the tree.
-func (tree *RebuiltTree) leafToRoots(ctx context.Context) map[btrfsvol.LogicalAddr]containers.Set[btrfsvol.LogicalAddr] {
- return containers.LoadOrElse[btrfsprim.ObjID, map[btrfsvol.LogicalAddr]containers.Set[btrfsvol.LogicalAddr]](&tree.forrest.leafs, tree.ID, func(btrfsprim.ObjID) map[btrfsvol.LogicalAddr]containers.Set[btrfsvol.LogicalAddr] {
- ctx = dlog.WithField(ctx, "btrfsinspect.rebuild-nodes.rebuild.index-nodes", fmt.Sprintf("tree=%v", tree.ID))
-
- nodeToRoots := make(map[btrfsvol.LogicalAddr]containers.Set[btrfsvol.LogicalAddr])
-
- var stats textui.Portion[int]
- stats.D = len(tree.forrest.graph.Nodes)
- progressWriter := textui.NewProgress[textui.Portion[int]](ctx, dlog.LogLevelInfo, textui.Tunable(1*time.Second))
- progress := func() {
- stats.N = len(nodeToRoots)
- progressWriter.Set(stats)
- }
-
- progress()
- for _, node := range maps.SortedKeys(tree.forrest.graph.Nodes) {
- tree.indexNode(ctx, node, nodeToRoots, progress, nil)
- }
- progressWriter.Done()
-
- ret := make(map[btrfsvol.LogicalAddr]containers.Set[btrfsvol.LogicalAddr])
- for node, roots := range nodeToRoots {
- if tree.forrest.graph.Nodes[node].Level == 0 && len(roots) > 0 {
- ret[node] = roots
- }
- }
- return ret
- })
-}
-
-func (tree *RebuiltTree) indexNode(ctx context.Context, node btrfsvol.LogicalAddr, index map[btrfsvol.LogicalAddr]containers.Set[btrfsvol.LogicalAddr], progress func(), stack []btrfsvol.LogicalAddr) {
- defer progress()
- if err := ctx.Err(); err != nil {
- return
- }
- if _, done := index[node]; done {
- return
- }
- if slices.Contains(node, stack) {
- // This is a panic because tree.forrest.graph.FinalCheck() should
- // have already checked for loops.
- panic("loop")
- }
- if !tree.isOwnerOK(tree.forrest.graph.Nodes[node].Owner, tree.forrest.graph.Nodes[node].Generation) {
- index[node] = nil
- return
- }
-
- // tree.leafToRoots
- stack = append(stack, node)
- var roots containers.Set[btrfsvol.LogicalAddr]
- for _, kp := range tree.forrest.graph.EdgesTo[node] {
- if !tree.isOwnerOK(tree.forrest.graph.Nodes[kp.FromNode].Owner, tree.forrest.graph.Nodes[kp.FromNode].Generation) {
- continue
- }
- tree.indexNode(ctx, kp.FromNode, index, progress, stack)
- if len(index[kp.FromNode]) > 0 {
- if roots == nil {
- roots = make(containers.Set[btrfsvol.LogicalAddr])
- }
- roots.InsertFrom(index[kp.FromNode])
- }
- }
- if roots == nil {
- roots = containers.NewSet[btrfsvol.LogicalAddr](node)
- }
- index[node] = roots
-}
-
-// isOwnerOK returns whether it is permissible for a node with
-// .Head.Owner=owner to be in this tree.
-func (tree *RebuiltTree) isOwnerOK(owner btrfsprim.ObjID, gen btrfsprim.Generation) bool {
- for {
- if owner == tree.ID {
- return true
- }
- if tree.Parent == nil || gen >= tree.ParentGen {
- return false
- }
- tree = tree.Parent
- }
-}
-
-// LRU members 2 and 3: .Items() and .PotentialItems() /////////////////////////////////////////////////////////////////
-
-// Items returns a map of the items contained in this tree.
-//
-// Do not mutate the returned map; it is a pointer to the
-// RebuiltTree's internal map!
-func (tree *RebuiltTree) Items(ctx context.Context) *containers.SortedMap[btrfsprim.Key, keyio.ItemPtr] {
- ctx = dlog.WithField(ctx, "btrfsinspect.rebuild-nodes.rebuild.index-inc-items", fmt.Sprintf("tree=%v", tree.ID))
- return tree.items(ctx, &tree.forrest.incItems, tree.Roots.HasAny)
-}
-
-// PotentialItems returns a map of items that could be added to this
-// tree with .AddRoot().
-//
-// Do not mutate the returned map; it is a pointer to the
-// RebuiltTree's internal map!
-func (tree *RebuiltTree) PotentialItems(ctx context.Context) *containers.SortedMap[btrfsprim.Key, keyio.ItemPtr] {
- ctx = dlog.WithField(ctx, "btrfsinspect.rebuild-nodes.rebuild.index-exc-items", fmt.Sprintf("tree=%v", tree.ID))
- return tree.items(ctx, &tree.forrest.excItems,
- func(roots containers.Set[btrfsvol.LogicalAddr]) bool {
- return !tree.Roots.HasAny(roots)
- })
-}
-
-type itemIndex = containers.SortedMap[btrfsprim.Key, keyio.ItemPtr]
-
-type itemStats struct {
- Leafs textui.Portion[int]
- NumItems int
- NumDups int
-}
-
-func (s itemStats) String() string {
- return textui.Sprintf("%v (%v items, %v dups)",
- s.Leafs, s.NumItems, s.NumDups)
-}
-
-func (tree *RebuiltTree) items(ctx context.Context, cache containers.Map[btrfsprim.ObjID, *itemIndex],
- leafFn func(roots containers.Set[btrfsvol.LogicalAddr]) bool,
-) *containers.SortedMap[btrfsprim.Key, keyio.ItemPtr] {
- tree.mu.RLock()
- defer tree.mu.RUnlock()
-
- return containers.LoadOrElse(cache, tree.ID, func(btrfsprim.ObjID) *itemIndex {
- var leafs []btrfsvol.LogicalAddr
- for leaf, roots := range tree.leafToRoots(ctx) {
- if leafFn(roots) {
- leafs = append(leafs, leaf)
- }
- }
- slices.Sort(leafs)
-
- var stats itemStats
- stats.Leafs.D = len(leafs)
- progressWriter := textui.NewProgress[itemStats](ctx, dlog.LogLevelInfo, textui.Tunable(1*time.Second))
-
- index := new(containers.SortedMap[btrfsprim.Key, keyio.ItemPtr])
- for i, leaf := range leafs {
- stats.Leafs.N = i
- progressWriter.Set(stats)
- for j, itemKey := range tree.forrest.graph.Nodes[leaf].Items {
- newPtr := keyio.ItemPtr{
- Node: leaf,
- Idx: j,
- }
- if oldPtr, exists := index.Load(itemKey); !exists {
- index.Store(itemKey, newPtr)
- stats.NumItems++
- } else {
- if tree.ShouldReplace(oldPtr.Node, newPtr.Node) {
- index.Store(itemKey, newPtr)
- }
- stats.NumDups++
- }
- progressWriter.Set(stats)
- }
- }
- if stats.Leafs.N > 0 {
- stats.Leafs.N = len(leafs)
- progressWriter.Set(stats)
- progressWriter.Done()
- }
-
- return index
- })
-}
-
-func (tree *RebuiltTree) ShouldReplace(oldNode, newNode btrfsvol.LogicalAddr) bool {
- oldDist, _ := tree.COWDistance(tree.forrest.graph.Nodes[oldNode].Owner)
- newDist, _ := tree.COWDistance(tree.forrest.graph.Nodes[newNode].Owner)
- switch {
- case newDist < oldDist:
- // Replace the old one with the new lower-dist one.
- return true
- case newDist > oldDist:
- // Retain the old lower-dist one.
- return false
- default:
- oldGen := tree.forrest.graph.Nodes[oldNode].Generation
- newGen := tree.forrest.graph.Nodes[newNode].Generation
- switch {
- case newGen > oldGen:
- // Replace the old one with the new higher-gen one.
- return true
- case newGen < oldGen:
- // Retain the old higher-gen one.
- return false
- default:
- // TODO: This is a panic because I'm not really sure what the
- // best way to handle this is, and so if this happens I want the
- // program to crash and force me to figure out how to handle it.
- panic(fmt.Errorf("dup nodes in tree=%v: old=%v=%v ; new=%v=%v",
- tree.ID,
- oldNode, tree.forrest.graph.Nodes[oldNode],
- newNode, tree.forrest.graph.Nodes[newNode]))
- }
- }
-}
-
-// .AddRoot() //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-type rootStats struct {
- Leafs textui.Portion[int]
- AddedLeafs int
- AddedItems int
-}
-
-func (s rootStats) String() string {
- return textui.Sprintf("%v (added %v leafs, added %v items)",
- s.Leafs, s.AddedLeafs, s.AddedItems)
-}
-
-// AddRoot adds an additional root node to the tree. It is useful to
-// call .AddRoot() to re-attach part of the tree that has been broken
-// off.
-func (tree *RebuiltTree) AddRoot(ctx context.Context, rootNode btrfsvol.LogicalAddr) {
- tree.mu.Lock()
- defer tree.mu.Unlock()
- ctx = dlog.WithField(ctx, "btrfsinspect.rebuild-nodes.rebuild.add-root", fmt.Sprintf("tree=%v rootNode=%v", tree.ID, rootNode))
- dlog.Info(ctx, "adding root...")
-
- leafToRoots := tree.leafToRoots(ctx)
-
- var stats rootStats
- stats.Leafs.D = len(leafToRoots)
- progressWriter := textui.NewProgress[rootStats](ctx, dlog.LogLevelInfo, textui.Tunable(1*time.Second))
- for i, leaf := range maps.SortedKeys(leafToRoots) {
- stats.Leafs.N = i
- progressWriter.Set(stats)
-
- if tree.Roots.HasAny(leafToRoots[leaf]) || !leafToRoots[leaf].Has(rootNode) {
- continue
- }
-
- stats.AddedLeafs++
- progressWriter.Set(stats)
-
- for _, itemKey := range tree.forrest.graph.Nodes[leaf].Items {
- tree.forrest.cb.AddedItem(ctx, tree.ID, itemKey)
- stats.AddedItems++
- progressWriter.Set(stats)
- }
- }
- stats.Leafs.N = len(leafToRoots)
- progressWriter.Set(stats)
- progressWriter.Done()
-
- tree.Roots.Insert(rootNode)
- tree.forrest.incItems.Delete(tree.ID) // force re-gen
- 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.flushNegativeCache(ctx)
- }
- tree.forrest.cb.AddedRoot(ctx, tree.ID, rootNode)
-}
-
-// main public API /////////////////////////////////////////////////////////////////////////////////////////////////////
-
-// COWDistance returns how many COW-snapshots down the 'tree' is from
-// the 'parent'.
-func (tree *RebuiltTree) COWDistance(parentID btrfsprim.ObjID) (dist int, ok bool) {
- for {
- if parentID == tree.ID {
- return dist, true
- }
- if tree.Parent == nil {
- return 0, false
- }
- tree = tree.Parent
- dist++
- }
-}
-
-// ReadItem reads an item from a tree.
-func (tree *RebuiltTree) ReadItem(ctx context.Context, key btrfsprim.Key) btrfsitem.Item {
- ptr, ok := tree.Items(ctx).Load(key)
- if !ok {
- panic(fmt.Errorf("should not happen: btrees.RebuiltTree.ReadItem called for not-included key: %v", key))
- }
- return tree.forrest.keyIO.ReadItem(ctx, ptr)
-}
-
-// LeafToRoots returns the list of potential roots (to pass to
-// .AddRoot) that include a given leaf-node.
-func (tree *RebuiltTree) LeafToRoots(ctx context.Context, leaf btrfsvol.LogicalAddr) containers.Set[btrfsvol.LogicalAddr] {
- if tree.forrest.graph.Nodes[leaf].Level != 0 {
- panic(fmt.Errorf("should not happen: (tree=%v).LeafToRoots(leaf=%v): not a leaf",
- tree.ID, leaf))
- }
- tree.mu.RLock()
- defer tree.mu.RUnlock()
- ret := make(containers.Set[btrfsvol.LogicalAddr])
- for root := range tree.leafToRoots(ctx)[leaf] {
- if tree.Roots.Has(root) {
- panic(fmt.Errorf("should not happen: (tree=%v).LeafToRoots(leaf=%v): tree contains root=%v but not leaf",
- tree.ID, leaf, root))
- }
- ret.Insert(root)
- }
- if len(ret) == 0 {
- return nil
- }
- return ret
-}