From f76faa4b8debd9c94751a03dd65e46c80a340a82 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 3 Feb 2023 14:22:02 -0700 Subject: btrfstree: Add a FreeNodeRef function, use it --- lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go | 3 +++ lib/btrfsprogs/btrfsinspect/rebuildnodes/scan.go | 5 ++++- lib/btrfsprogs/btrfsinspect/scandevices.go | 1 + lib/btrfsprogs/btrfsutil/broken_btree.go | 9 +++++++++ lib/btrfsprogs/btrfsutil/skinny_paths.go | 1 + 5 files changed, 18 insertions(+), 1 deletion(-) (limited to 'lib/btrfsprogs') diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go index 149706d..b4ab645 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/keyio/keyio.go @@ -62,6 +62,9 @@ func NewHandle(file diskio.File[btrfsvol.LogicalAddr], sb btrfstree.Superblock) cache: containers.ARCache[btrfsvol.LogicalAddr, *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.Node]]{ MaxLen: textui.Tunable(8), + OnRemove: func(_ btrfsvol.LogicalAddr, nodeRef *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.Node]) { + btrfstree.FreeNodeRef(nodeRef) + }, }, } } diff --git a/lib/btrfsprogs/btrfsinspect/rebuildnodes/scan.go b/lib/btrfsprogs/btrfsinspect/rebuildnodes/scan.go index 7e19802..632ed70 100644 --- a/lib/btrfsprogs/btrfsinspect/rebuildnodes/scan.go +++ b/lib/btrfsprogs/btrfsinspect/rebuildnodes/scan.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -48,12 +48,15 @@ func ScanDevices(ctx context.Context, fs *btrfs.FS, scanResults btrfsinspect.Sca LAddr: containers.Optional[btrfsvol.LogicalAddr]{OK: true, Val: laddr}, }) if err != nil { + btrfstree.FreeNodeRef(nodeRef) return btrfstree.Superblock{}, graph.Graph{}, nil, err } nodeGraph.InsertNode(nodeRef) keyIO.InsertNode(nodeRef) + btrfstree.FreeNodeRef(nodeRef) + stats.N++ progressWriter.Set(stats) } diff --git a/lib/btrfsprogs/btrfsinspect/scandevices.go b/lib/btrfsprogs/btrfsinspect/scandevices.go index 0cfee5b..d54be71 100644 --- a/lib/btrfsprogs/btrfsinspect/scandevices.go +++ b/lib/btrfsprogs/btrfsinspect/scandevices.go @@ -245,6 +245,7 @@ func ScanOneDevice(ctx context.Context, dev *btrfs.Device, sb btrfstree.Superblo } minNextNode = pos + btrfsvol.PhysicalAddr(sb.NodeSize) } + btrfstree.FreeNodeRef(nodeRef) } } progress(devSize) diff --git a/lib/btrfsprogs/btrfsutil/broken_btree.go b/lib/btrfsprogs/btrfsutil/broken_btree.go index 7ea31ce..15641ab 100644 --- a/lib/btrfsprogs/btrfsutil/broken_btree.go +++ b/lib/btrfsprogs/btrfsutil/broken_btree.go @@ -237,11 +237,13 @@ func (bt *brokenTrees) TreeSearch(treeID btrfsprim.ObjID, fn func(btrfsprim.Key, itemPath := bt.arena.Inflate(indexItem.Value.Path) node, err := bt.inner.ReadNode(itemPath.Parent()) + defer btrfstree.FreeNodeRef(node) if err != nil { return btrfstree.Item{}, bt.addErrs(index, fn, err) } item := node.Data.BodyLeaf[itemPath.Node(-1).FromItemIdx] + item.Body = item.Body.CloneItem() // Since we were only asked to return 1 item, it isn't // necessary to augment this `nil` with bt.addErrs(). @@ -271,13 +273,16 @@ func (bt *brokenTrees) TreeSearchAll(treeID btrfsprim.ObjID, fn func(btrfsprim.K itemPath := bt.arena.Inflate(indexItems[i].Path) if node == nil || node.Addr != itemPath.Node(-2).ToNodeAddr { var err error + btrfstree.FreeNodeRef(node) node, err = bt.inner.ReadNode(itemPath.Parent()) if err != nil { + btrfstree.FreeNodeRef(node) return nil, bt.addErrs(index, fn, err) } } ret[i] = node.Data.BodyLeaf[itemPath.Node(-1).FromItemIdx] } + btrfstree.FreeNodeRef(node) return ret, bt.addErrs(index, fn, nil) } @@ -306,8 +311,10 @@ func (bt *brokenTrees) TreeWalk(ctx context.Context, treeID btrfsprim.ObjID, err itemPath := bt.arena.Inflate(indexItem.Value.Path) if node == nil || node.Addr != itemPath.Node(-2).ToNodeAddr { var err error + btrfstree.FreeNodeRef(node) node, err = bt.inner.ReadNode(itemPath.Parent()) if err != nil { + btrfstree.FreeNodeRef(node) errHandle(&btrfstree.TreeError{Path: itemPath, Err: err}) return true } @@ -319,6 +326,7 @@ func (bt *brokenTrees) TreeWalk(ctx context.Context, treeID btrfsprim.ObjID, err } return true }) + btrfstree.FreeNodeRef(node) } func (bt *brokenTrees) Superblock() (*btrfstree.Superblock, error) { @@ -339,6 +347,7 @@ func (bt *brokenTrees) Augment(treeID btrfsprim.ObjID, nodeAddr btrfsvol.Logical return nil, index.TreeRootErr } nodeRef, err := btrfstree.ReadNode[btrfsvol.LogicalAddr](bt.inner, *sb, nodeAddr, btrfstree.NodeExpectations{}) + defer btrfstree.FreeNodeRef(nodeRef) if err != nil { return nil, err } diff --git a/lib/btrfsprogs/btrfsutil/skinny_paths.go b/lib/btrfsprogs/btrfsutil/skinny_paths.go index 4c314ec..1695990 100644 --- a/lib/btrfsprogs/btrfsutil/skinny_paths.go +++ b/lib/btrfsprogs/btrfsutil/skinny_paths.go @@ -55,6 +55,7 @@ func (a *SkinnyPathArena) getItem(parent btrfstree.TreePath, itemIdx int) (btrfs } node, err := btrfstree.ReadNode(a.FS, a.SB, parent.Node(-1).ToNodeAddr, btrfstree.NodeExpectations{}) + defer btrfstree.FreeNodeRef(node) if err != nil { return btrfstree.TreePathElem{}, err } -- cgit v1.2.3-54-g00ecf