diff options
Diffstat (limited to 'lib/btrfsutil')
-rw-r--r-- | lib/btrfsutil/graph.go | 6 | ||||
-rw-r--r-- | lib/btrfsutil/old_rebuilt_forrest.go | 17 | ||||
-rw-r--r-- | lib/btrfsutil/rebuilt_forrest.go | 10 | ||||
-rw-r--r-- | lib/btrfsutil/rebuilt_readitem.go | 34 | ||||
-rw-r--r-- | lib/btrfsutil/scan.go | 4 |
5 files changed, 26 insertions, 45 deletions
diff --git a/lib/btrfsutil/graph.go b/lib/btrfsutil/graph.go index 39e1cf2..ea33be7 100644 --- a/lib/btrfsutil/graph.go +++ b/lib/btrfsutil/graph.go @@ -17,7 +17,6 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfstree" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" "git.lukeshu.com/btrfs-progs-ng/lib/containers" - "git.lukeshu.com/btrfs-progs-ng/lib/diskio" "git.lukeshu.com/btrfs-progs-ng/lib/maps" "git.lukeshu.com/btrfs-progs-ng/lib/slices" "git.lukeshu.com/btrfs-progs-ng/lib/textui" @@ -199,7 +198,7 @@ func (g Graph) InsertNode(node *btrfstree.Node) { } } -func (g Graph) FinalCheck(ctx context.Context, fs diskio.File[btrfsvol.LogicalAddr], sb btrfstree.Superblock) error { +func (g Graph) FinalCheck(ctx context.Context, fs btrfstree.NodeSource) error { var stats textui.Portion[int] dlog.Info(ctx, "Checking keypointers for dead-ends...") @@ -208,9 +207,10 @@ func (g Graph) FinalCheck(ctx context.Context, fs diskio.File[btrfsvol.LogicalAd progressWriter.Set(stats) for laddr := range g.EdgesTo { if _, ok := g.Nodes[laddr]; !ok { - _, err := btrfstree.ReadNode[btrfsvol.LogicalAddr](fs, sb, laddr, btrfstree.NodeExpectations{ + node, err := fs.AcquireNode(ctx, laddr, btrfstree.NodeExpectations{ LAddr: containers.OptionalValue(laddr), }) + fs.ReleaseNode(node) if err == nil { progressWriter.Done() return fmt.Errorf("node@%v exists but was not in node scan results", laddr) diff --git a/lib/btrfsutil/old_rebuilt_forrest.go b/lib/btrfsutil/old_rebuilt_forrest.go index 5b99892..36abe6f 100644 --- a/lib/btrfsutil/old_rebuilt_forrest.go +++ b/lib/btrfsutil/old_rebuilt_forrest.go @@ -231,12 +231,7 @@ func (tree oldRebuiltTree) addErrs(fn func(btrfsprim.Key, uint32) int, err error } func (bt *OldRebuiltForrest) readNode(nodeInfo nodeInfo) *btrfstree.Node { - sb, err := bt.inner.Superblock() - if err != nil { - panic(fmt.Errorf("should not happen: i/o error: %w", err)) - } - - node, err := btrfstree.ReadNode[btrfsvol.LogicalAddr](bt.inner, *sb, nodeInfo.LAddr, btrfstree.NodeExpectations{ + node, err := bt.inner.AcquireNode(bt.ctx, nodeInfo.LAddr, btrfstree.NodeExpectations{ LAddr: containers.OptionalValue(nodeInfo.LAddr), Level: containers.OptionalValue(nodeInfo.Level), Generation: containers.OptionalValue(nodeInfo.Generation), @@ -286,7 +281,7 @@ func (tree oldRebuiltTree) treeSearch(_ context.Context, searcher btrfstree.Tree } node := tree.forrest.readNode(indexItem.Value.Node) - defer node.Free() + defer tree.forrest.inner.ReleaseNode(node) item := node.BodyLeaf[indexItem.Value.Slot] item.Body = item.Body.CloneItem() @@ -323,12 +318,12 @@ func (tree oldRebuiltTree) treeSubrange(_ context.Context, min int, searcher btr func(rbNode *containers.RBNode[oldRebuiltTreeValue]) bool { cnt++ if node == nil || node.Head.Addr != rbNode.Value.Node.LAddr { - node.Free() + tree.forrest.inner.ReleaseNode(node) node = tree.forrest.readNode(rbNode.Value.Node) } return handleFn(node.BodyLeaf[rbNode.Value.Slot]) }) - node.Free() + tree.forrest.inner.ReleaseNode(node) var err error if cnt < min { @@ -371,7 +366,7 @@ func (tree oldRebuiltTree) treeWalk(ctx context.Context, cbs btrfstree.TreeWalkH return false } if node == nil || node.Head.Addr != indexItem.Value.Node.LAddr { - node.Free() + tree.forrest.inner.ReleaseNode(node) node = tree.forrest.readNode(indexItem.Value.Node) } item := node.BodyLeaf[indexItem.Value.Slot] @@ -404,7 +399,7 @@ func (tree oldRebuiltTree) treeWalk(ctx context.Context, cbs btrfstree.TreeWalkH } return ctx.Err() == nil }) - node.Free() + tree.forrest.inner.ReleaseNode(node) } // Superblock implements btrfs.ReadableFS. diff --git a/lib/btrfsutil/rebuilt_forrest.go b/lib/btrfsutil/rebuilt_forrest.go index 811e1ac..60f3010 100644 --- a/lib/btrfsutil/rebuilt_forrest.go +++ b/lib/btrfsutil/rebuilt_forrest.go @@ -15,7 +15,6 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfstree" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" "git.lukeshu.com/btrfs-progs-ng/lib/containers" - "git.lukeshu.com/btrfs-progs-ng/lib/diskio" "git.lukeshu.com/btrfs-progs-ng/lib/slices" "git.lukeshu.com/btrfs-progs-ng/lib/textui" ) @@ -130,7 +129,7 @@ func (cb noopRebuiltForrestCallbacks) LookupUUID(ctx context.Context, uuid btrfs // NewRebuiltForrest(). type RebuiltForrest struct { // static - file diskio.File[btrfsvol.LogicalAddr] + file btrfstree.NodeSource sb btrfstree.Superblock graph Graph cb RebuiltForrestCallbacks @@ -142,13 +141,11 @@ type RebuiltForrest struct { leafs containers.Cache[btrfsprim.ObjID, map[btrfsvol.LogicalAddr]containers.Set[btrfsvol.LogicalAddr]] incItems containers.Cache[btrfsprim.ObjID, itemIndex] excItems containers.Cache[btrfsprim.ObjID, itemIndex] - - nodes containers.Cache[btrfsvol.LogicalAddr, btrfstree.Node] } // NewRebuiltForrest returns a new RebuiltForrest instance. The // RebuiltForrestCallbacks may be nil. -func NewRebuiltForrest(file diskio.File[btrfsvol.LogicalAddr], sb btrfstree.Superblock, graph Graph, cb RebuiltForrestCallbacks) *RebuiltForrest { +func NewRebuiltForrest(file btrfstree.NodeSource, sb btrfstree.Superblock, graph Graph, cb RebuiltForrestCallbacks) *RebuiltForrest { ret := &RebuiltForrest{ file: file, sb: sb, @@ -171,9 +168,6 @@ func NewRebuiltForrest(file diskio.File[btrfsvol.LogicalAddr], sb btrfstree.Supe containers.SourceFunc[btrfsprim.ObjID, itemIndex](func(ctx context.Context, treeID btrfsprim.ObjID, excItems *itemIndex) { *excItems = ret.trees[treeID].uncachedExcItems(ctx) })) - ret.nodes = containers.NewARCache[btrfsvol.LogicalAddr, btrfstree.Node](textui.Tunable(8), - containers.SourceFunc[btrfsvol.LogicalAddr, btrfstree.Node](ret.readNode)) - if ret.cb == nil { ret.cb = noopRebuiltForrestCallbacks{ forrest: ret, diff --git a/lib/btrfsutil/rebuilt_readitem.go b/lib/btrfsutil/rebuilt_readitem.go index d3a2253..73dc01e 100644 --- a/lib/btrfsutil/rebuilt_readitem.go +++ b/lib/btrfsutil/rebuilt_readitem.go @@ -8,8 +8,6 @@ import ( "context" "fmt" - "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" @@ -26,16 +24,20 @@ func (ptr ItemPtr) String() string { return fmt.Sprintf("node@%v[%v]", ptr.Node, ptr.Slot) } -func (ts *RebuiltForrest) readNode(ctx context.Context, laddr btrfsvol.LogicalAddr, out *btrfstree.Node) { - dlog.Debugf(ctx, "cache-miss node@%v, reading...", laddr) - - graphInfo, ok := ts.graph.Nodes[laddr] +func (ts *RebuiltForrest) readItem(ctx context.Context, ptr ItemPtr) btrfsitem.Item { + graphInfo, ok := ts.graph.Nodes[ptr.Node] if !ok { - panic(fmt.Errorf("should not happen: node@%v is not mentioned in the in-memory graph", laddr)) + panic(fmt.Errorf("should not happen: btrfsutil.RebuiltForrest.readItem called for node@%v not mentioned in the in-memory graph", ptr.Node)) + } + if graphInfo.Level != 0 { + panic(fmt.Errorf("should not happen: btrfsutil.RebuiltForrest.readItem called for non-leaf node@%v", ptr.Node)) + } + if ptr.Slot < 0 { + panic(fmt.Errorf("should not happen: btrfsutil.RebuiltForrest.readItem called for negative item slot: %v", ptr.Slot)) } - node, err := btrfstree.ReadNode[btrfsvol.LogicalAddr](ts.file, ts.sb, laddr, btrfstree.NodeExpectations{ - LAddr: containers.OptionalValue(laddr), + node, err := ts.file.AcquireNode(ctx, ptr.Node, btrfstree.NodeExpectations{ + LAddr: containers.OptionalValue(ptr.Node), Level: containers.OptionalValue(graphInfo.Level), Generation: containers.OptionalValue(graphInfo.Generation), Owner: func(treeID btrfsprim.ObjID, gen btrfsprim.Generation) error { @@ -49,22 +51,12 @@ func (ts *RebuiltForrest) readNode(ctx context.Context, laddr btrfsvol.LogicalAd MinItem: containers.OptionalValue(graphInfo.MinItem()), MaxItem: containers.OptionalValue(graphInfo.MaxItem()), }) + defer ts.file.ReleaseNode(node) if err != nil { panic(fmt.Errorf("should not happen: i/o error: %w", err)) } - *out = *node -} - -func (ts *RebuiltForrest) readItem(ctx context.Context, ptr ItemPtr) btrfsitem.Item { - if ts.graph.Nodes[ptr.Node].Level != 0 { - panic(fmt.Errorf("should not happen: btrfsutil.RebuiltForrest.readItem called for non-leaf node@%v", ptr.Node)) - } - if ptr.Slot < 0 { - panic(fmt.Errorf("should not happen: btrfsutil.RebuiltForrest.readItem called for negative item slot: %v", ptr.Slot)) - } - items := ts.nodes.Acquire(ctx, ptr.Node).BodyLeaf - defer ts.nodes.Release(ptr.Node) + items := node.BodyLeaf if ptr.Slot >= len(items) { panic(fmt.Errorf("should not happen: btrfsutil.RebuiltForrest.readItem called for out-of-bounds item slot: slot=%v len=%v", diff --git a/lib/btrfsutil/scan.go b/lib/btrfsutil/scan.go index 05b27d5..0e268e5 100644 --- a/lib/btrfsutil/scan.go +++ b/lib/btrfsutil/scan.go @@ -122,7 +122,7 @@ func ScanOneDevice[Stats comparable, Result any](ctx context.Context, dev *btrfs } if checkForNode { - node, err := btrfstree.ReadNode[btrfsvol.PhysicalAddr](dev, *sb, pos, btrfstree.NodeExpectations{}) + node, err := btrfstree.ReadNode[btrfsvol.PhysicalAddr](dev, *sb, pos) if err != nil { if !errors.Is(err, btrfstree.ErrNotANode) { dlog.Errorf(ctx, "error: %v", err) @@ -134,7 +134,7 @@ func ScanOneDevice[Stats comparable, Result any](ctx context.Context, dev *btrfs } minNextNode = pos + btrfsvol.PhysicalAddr(sb.NodeSize) } - node.Free() + node.RawFree() } } |