From 84099499feb558a01253bd272563fa1271527a75 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 12 Mar 2023 17:09:51 -0600 Subject: Update identifiers and comments to reflect new file/package names --- cmd/btrfs-rec/inspect/mount/mount.go | 2 +- cmd/btrfs-rec/inspect/rebuildmappings/process.go | 4 +- .../rebuildmappings/process_matchsums_exact.go | 2 +- .../rebuildmappings/process_matchsums_fuzzy.go | 2 +- cmd/btrfs-rec/inspect/rebuildtrees/rebuild.go | 2 +- .../inspect/rebuildtrees/rebuild_treecb.go | 8 +- .../inspect/rebuildtrees/rebuild_wantcb.go | 12 +- cmd/btrfs-rec/inspect_lsfiles.go | 2 +- lib/btrfscheck/graph.go | 12 +- lib/btrfsutil/graph.go | 36 ++--- lib/btrfsutil/graph_loops.go | 4 +- lib/btrfsutil/old_rebuilt_forrest.go | 153 ++++++++++----------- lib/btrfsutil/rebuilt_forrest.go | 22 +-- 13 files changed, 128 insertions(+), 133 deletions(-) diff --git a/cmd/btrfs-rec/inspect/mount/mount.go b/cmd/btrfs-rec/inspect/mount/mount.go index 7e68ae6..da0bbb6 100644 --- a/cmd/btrfs-rec/inspect/mount/mount.go +++ b/cmd/btrfs-rec/inspect/mount/mount.go @@ -44,7 +44,7 @@ func MountRO(ctx context.Context, fs *btrfs.FS, mountpoint string, noChecksums b rootSubvol := &subvolume{ Subvolume: btrfs.Subvolume{ - FS: btrfsutil.NewBrokenTrees(ctx, fs), + FS: btrfsutil.NewOldRebuiltForrest(ctx, fs), TreeID: btrfsprim.FS_TREE_OBJECTID, NoChecksums: noChecksums, }, diff --git a/cmd/btrfs-rec/inspect/rebuildmappings/process.go b/cmd/btrfs-rec/inspect/rebuildmappings/process.go index 5f41f15..4ef4dac 100644 --- a/cmd/btrfs-rec/inspect/rebuildmappings/process.go +++ b/cmd/btrfs-rec/inspect/rebuildmappings/process.go @@ -161,14 +161,14 @@ func RebuildMappings(ctx context.Context, fs *btrfs.FS, scanResults ScanDevicesR dlog.Infof(_ctx, "5/6: Searching for %d block groups in checksum map (exact)...", len(bgs)) physicalSums := ExtractPhysicalSums(scanResults) logicalSums := ExtractLogicalSums(ctx, scanResults) - if err := matchBlockGroupSums(ctx, fs, bgs, physicalSums, logicalSums); err != nil { + if err := matchBlockGroupSumsExact(ctx, fs, bgs, physicalSums, logicalSums); err != nil { return err } dlog.Info(ctx, "... done searching for exact block groups") ctx = dlog.WithField(_ctx, "btrfsinspect.rebuild-mappings.step", "6/6") dlog.Infof(_ctx, "6/6: Searching for %d block groups in checksum map (fuzzy)...", len(bgs)) - if err := fuzzyMatchBlockGroupSums(ctx, fs, bgs, physicalSums, logicalSums); err != nil { + if err := matchBlockGroupSumsFuzzy(ctx, fs, bgs, physicalSums, logicalSums); err != nil { return err } dlog.Info(_ctx, "... done searching for fuzzy block groups") diff --git a/cmd/btrfs-rec/inspect/rebuildmappings/process_matchsums_exact.go b/cmd/btrfs-rec/inspect/rebuildmappings/process_matchsums_exact.go index a3e724e..533ae67 100644 --- a/cmd/btrfs-rec/inspect/rebuildmappings/process_matchsums_exact.go +++ b/cmd/btrfs-rec/inspect/rebuildmappings/process_matchsums_exact.go @@ -17,7 +17,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/maps" ) -func matchBlockGroupSums(ctx context.Context, +func matchBlockGroupSumsExact(ctx context.Context, fs *btrfs.FS, blockgroups map[btrfsvol.LogicalAddr]BlockGroup, physicalSums map[btrfsvol.DeviceID]btrfssum.SumRun[btrfsvol.PhysicalAddr], diff --git a/cmd/btrfs-rec/inspect/rebuildmappings/process_matchsums_fuzzy.go b/cmd/btrfs-rec/inspect/rebuildmappings/process_matchsums_fuzzy.go index 4724c12..d6ea338 100644 --- a/cmd/btrfs-rec/inspect/rebuildmappings/process_matchsums_fuzzy.go +++ b/cmd/btrfs-rec/inspect/rebuildmappings/process_matchsums_fuzzy.go @@ -37,7 +37,7 @@ func (a fuzzyRecord) Compare(b fuzzyRecord) int { } } -func fuzzyMatchBlockGroupSums(ctx context.Context, +func matchBlockGroupSumsFuzzy(ctx context.Context, fs *btrfs.FS, blockgroups map[btrfsvol.LogicalAddr]BlockGroup, physicalSums map[btrfsvol.DeviceID]btrfssum.SumRun[btrfsvol.PhysicalAddr], diff --git a/cmd/btrfs-rec/inspect/rebuildtrees/rebuild.go b/cmd/btrfs-rec/inspect/rebuildtrees/rebuild.go index 6492d9c..565cb06 100644 --- a/cmd/btrfs-rec/inspect/rebuildtrees/rebuild.go +++ b/cmd/btrfs-rec/inspect/rebuildtrees/rebuild.go @@ -213,7 +213,7 @@ func (o *rebuilder) processAddedItemQueue(ctx context.Context) error { progress.NumAugments = o.numAugments progress.NumAugmentTrees = len(o.augmentQueue) progressWriter.Set(progress) - } else if !btrfscheck.HandleWouldBeNoOp(key.ItemType) { + } else if !btrfscheck.HandleItemWouldBeNoOp(key.ItemType) { o.settledItemQueue.Insert(key) } } diff --git a/cmd/btrfs-rec/inspect/rebuildtrees/rebuild_treecb.go b/cmd/btrfs-rec/inspect/rebuildtrees/rebuild_treecb.go index d58689c..e6a0777 100644 --- a/cmd/btrfs-rec/inspect/rebuildtrees/rebuild_treecb.go +++ b/cmd/btrfs-rec/inspect/rebuildtrees/rebuild_treecb.go @@ -13,7 +13,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" ) -// AddedItem implements btrees.Callbacks. +// AddedItem implements btrfsutil.RebuiltForrestCallbacks. func (o *rebuilder) AddedItem(ctx context.Context, tree btrfsprim.ObjID, key btrfsprim.Key) { o.addedItemQueue.Insert(keyAndTree{ TreeID: tree, @@ -21,14 +21,14 @@ func (o *rebuilder) AddedItem(ctx context.Context, tree btrfsprim.ObjID, key btr }) } -// AddedRoot implements btrees.Callbacks. +// AddedRoot implements btrfsutil.RebuiltForrestCallbacks. func (o *rebuilder) AddedRoot(ctx context.Context, tree btrfsprim.ObjID, root btrfsvol.LogicalAddr) { if retries := o.retryItemQueue[tree]; retries != nil { o.addedItemQueue.InsertFrom(retries) } } -// LookupRoot implements btrees.Callbacks. +// LookupRoot implements btrfsutil.RebuiltForrestCallbacks. func (o *rebuilder) LookupRoot(ctx context.Context, tree btrfsprim.ObjID) (offset btrfsprim.Generation, item btrfsitem.Root, ok bool) { wantKey := WantWithTree{ TreeID: btrfsprim.ROOT_TREE_OBJECTID, @@ -59,7 +59,7 @@ func (o *rebuilder) LookupRoot(ctx context.Context, tree btrfsprim.ObjID) (offse } } -// LookupUUID implements btrees.Callbacks. +// LookupUUID implements btrfsutil.RebuiltForrestCallbacks. func (o *rebuilder) LookupUUID(ctx context.Context, uuid btrfsprim.UUID) (id btrfsprim.ObjID, ok bool) { wantKey := WantWithTree{ TreeID: btrfsprim.UUID_TREE_OBJECTID, diff --git a/cmd/btrfs-rec/inspect/rebuildtrees/rebuild_wantcb.go b/cmd/btrfs-rec/inspect/rebuildtrees/rebuild_wantcb.go index 06be630..4a5029e 100644 --- a/cmd/btrfs-rec/inspect/rebuildtrees/rebuild_wantcb.go +++ b/cmd/btrfs-rec/inspect/rebuildtrees/rebuild_wantcb.go @@ -19,12 +19,12 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/containers" ) -// fsErr implements rebuildCallbacks. +// FSErr implements btrfscheck.GraphCallbacks. func (o *rebuilder) FSErr(ctx context.Context, e error) { dlog.Errorf(ctx, "filesystem error: %v", e) } -// want implements rebuildCallbacks. +// Want implements btrfscheck.GraphCallbacks. func (o *rebuilder) Want(ctx context.Context, reason string, treeID btrfsprim.ObjID, objID btrfsprim.ObjID, typ btrfsprim.ItemType) { wantKey := WantWithTree{ TreeID: treeID, @@ -73,7 +73,7 @@ func (o *rebuilder) _want(ctx context.Context, wantKey WantWithTree) (key btrfsp return btrfsprim.Key{}, false } -// wantOff implements rebuildCallbacks. +// WantOff implements btrfscheck.GraphCallbacks. func (o *rebuilder) WantOff(ctx context.Context, reason string, treeID btrfsprim.ObjID, objID btrfsprim.ObjID, typ btrfsprim.ItemType, off uint64) { wantKey := WantWithTree{ TreeID: treeID, @@ -117,7 +117,7 @@ func (o *rebuilder) _wantOff(ctx context.Context, wantKey WantWithTree) (ok bool return false } -// wantDirIndex implements rebuildCallbacks. +// WantDirIndex implements btrfscheck.GraphCallbacks. func (o *rebuilder) WantDirIndex(ctx context.Context, reason string, treeID btrfsprim.ObjID, objID btrfsprim.ObjID, name []byte) { wantKey := WantWithTree{ TreeID: treeID, @@ -350,7 +350,7 @@ func (o *rebuilder) _wantRange( }) } -// func implements rebuildCallbacks. +// WantCSum implements btrfscheck.GraphCallbacks. // // interval is [beg, end) func (o *rebuilder) WantCSum(ctx context.Context, reason string, inodeTree, inode btrfsprim.ObjID, beg, end btrfsvol.LogicalAddr) { @@ -391,7 +391,7 @@ func (o *rebuilder) WantCSum(ctx context.Context, reason string, inodeTree, inod uint64(roundDown(beg, btrfssum.BlockSize)), uint64(roundUp(end, btrfssum.BlockSize))) } -// wantFileExt implements rebuildCallbacks. +// WantFileExt implements btrfscheck.GraphCallbacks. func (o *rebuilder) WantFileExt(ctx context.Context, reason string, treeID btrfsprim.ObjID, ino btrfsprim.ObjID, size int64) { o._wantRange( ctx, reason, diff --git a/cmd/btrfs-rec/inspect_lsfiles.go b/cmd/btrfs-rec/inspect_lsfiles.go index d8da481..4f985ff 100644 --- a/cmd/btrfs-rec/inspect_lsfiles.go +++ b/cmd/btrfs-rec/inspect_lsfiles.go @@ -48,7 +48,7 @@ func init() { ctx := cmd.Context() printSubvol(out, "", true, "/", &btrfs.Subvolume{ - FS: btrfsutil.NewBrokenTrees(ctx, fs), + FS: btrfsutil.NewOldRebuiltForrest(ctx, fs), TreeID: btrfsprim.FS_TREE_OBJECTID, }) diff --git a/lib/btrfscheck/graph.go b/lib/btrfscheck/graph.go index c35baf8..ea51818 100644 --- a/lib/btrfscheck/graph.go +++ b/lib/btrfscheck/graph.go @@ -14,7 +14,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" ) -type rebuildCallbacks interface { +type GraphCallbacks interface { FSErr(ctx context.Context, e error) Want(ctx context.Context, reason string, treeID btrfsprim.ObjID, objID btrfsprim.ObjID, typ btrfsprim.ItemType) WantOff(ctx context.Context, reason string, treeID btrfsprim.ObjID, objID btrfsprim.ObjID, typ btrfsprim.ItemType, off uint64) @@ -23,9 +23,9 @@ type rebuildCallbacks interface { WantFileExt(ctx context.Context, reason string, treeID btrfsprim.ObjID, ino btrfsprim.ObjID, size int64) } -// handleWouldBeNoOp returns whether or not a call to handleItem for a -// given item type would be a no-op. -func HandleWouldBeNoOp(typ btrfsprim.ItemType) bool { +// HandleItemWouldBeNoOp returns whether or not a call to HandleItem +// for a given item type would be a no-op. +func HandleItemWouldBeNoOp(typ btrfsprim.ItemType) bool { switch typ { case // btrfsitem.Dev btrfsprim.DEV_ITEM_KEY, @@ -45,7 +45,7 @@ func HandleWouldBeNoOp(typ btrfsprim.ItemType) bool { } } -func HandleItem(o rebuildCallbacks, ctx context.Context, treeID btrfsprim.ObjID, item btrfstree.Item) { +func HandleItem(o GraphCallbacks, ctx context.Context, treeID btrfsprim.ObjID, item btrfstree.Item) { // Notionally, just express the relationships shown in // https://btrfs.wiki.kernel.org/index.php/File:References.png (from the page // https://btrfs.wiki.kernel.org/index.php/Data_Structures ) @@ -186,7 +186,7 @@ func HandleItem(o rebuildCallbacks, ctx context.Context, treeID btrfsprim.ObjID, case btrfsitem.FILE_EXTENT_INLINE: // nothing case btrfsitem.FILE_EXTENT_REG, btrfsitem.FILE_EXTENT_PREALLOC: - // NB: o.wantCSum checks inodeBody.Flags.Has(btrfsitem.INODE_NODATASUM) for us. + // NB: o.WantCSum checks inodeBody.Flags.Has(btrfsitem.INODE_NODATASUM) for us. o.WantCSum(ctx, "data sum", treeID, item.Key.ObjectID, body.BodyExtent.DiskByteNr, diff --git a/lib/btrfsutil/graph.go b/lib/btrfsutil/graph.go index f294a28..b4a8b72 100644 --- a/lib/btrfsutil/graph.go +++ b/lib/btrfsutil/graph.go @@ -23,7 +23,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/textui" ) -type Node struct { +type GraphNode struct { Level uint8 Generation btrfsprim.Generation Owner btrfsprim.ObjID @@ -33,7 +33,7 @@ type Node struct { Items []btrfsprim.Key } -func (n Node) String() string { +func (n GraphNode) String() string { if reflect.ValueOf(n).IsZero() { return "{}" } @@ -43,9 +43,9 @@ func (n Node) String() string { n.MaxItem.ObjectID, n.MaxItem.ItemType, n.MaxItem.Offset) } -type Edge struct { +type GraphEdge struct { // It is invalid for both 'FromRoot' and 'FromNode' to be - // non-zero. If both are zero, then the Edge is from the + // non-zero. If both are zero, then the GraphEdge is from the // superblock. FromRoot btrfsvol.LogicalAddr FromNode btrfsvol.LogicalAddr @@ -59,7 +59,7 @@ type Edge struct { ToGeneration btrfsprim.Generation } -func (kp Edge) String() string { +func (kp GraphEdge) String() string { var from string switch { case kp.FromRoot != 0: @@ -80,13 +80,13 @@ func (kp Edge) String() string { } type Graph struct { - Nodes map[btrfsvol.LogicalAddr]Node + Nodes map[btrfsvol.LogicalAddr]GraphNode BadNodes map[btrfsvol.LogicalAddr]error - EdgesFrom map[btrfsvol.LogicalAddr][]*Edge - EdgesTo map[btrfsvol.LogicalAddr][]*Edge + EdgesFrom map[btrfsvol.LogicalAddr][]*GraphEdge + EdgesTo map[btrfsvol.LogicalAddr][]*GraphEdge } -func (g Graph) insertEdge(ptr *Edge) { +func (g Graph) insertEdge(ptr *GraphEdge) { if ptr.ToNode == 0 { panic("kp.ToNode should not be zero") } @@ -112,7 +112,7 @@ func (g Graph) insertTreeRoot(sb btrfstree.Superblock, treeID btrfsprim.ObjID) { if treeInfo.RootNode == 0 { return } - g.insertEdge(&Edge{ + g.insertEdge(&GraphEdge{ FromTree: treeID, ToNode: treeInfo.RootNode, ToLevel: treeInfo.Level, @@ -122,10 +122,10 @@ func (g Graph) insertTreeRoot(sb btrfstree.Superblock, treeID btrfsprim.ObjID) { func NewGraph(sb btrfstree.Superblock) *Graph { g := &Graph{ - Nodes: make(map[btrfsvol.LogicalAddr]Node), + Nodes: make(map[btrfsvol.LogicalAddr]GraphNode), BadNodes: make(map[btrfsvol.LogicalAddr]error), - EdgesFrom: make(map[btrfsvol.LogicalAddr][]*Edge), - EdgesTo: make(map[btrfsvol.LogicalAddr][]*Edge), + EdgesFrom: make(map[btrfsvol.LogicalAddr][]*GraphEdge), + EdgesTo: make(map[btrfsvol.LogicalAddr][]*GraphEdge), } // These 4 trees are mentioned directly in the superblock, so @@ -139,7 +139,7 @@ func NewGraph(sb btrfstree.Superblock) *Graph { } func (g Graph) InsertNode(nodeRef *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.Node]) { - nodeData := Node{ + nodeData := GraphNode{ Level: nodeRef.Data.Head.Level, Generation: nodeRef.Data.Head.Generation, Owner: nodeRef.Data.Head.Owner, @@ -155,14 +155,14 @@ func (g Graph) InsertNode(nodeRef *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.No cnt++ } } - kps := make([]Edge, 0, cnt) + kps := make([]GraphEdge, 0, cnt) keys := make([]btrfsprim.Key, len(nodeRef.Data.BodyLeaf)) nodeData.Items = keys g.Nodes[nodeRef.Addr] = nodeData for i, item := range nodeRef.Data.BodyLeaf { keys[i] = item.Key if itemBody, ok := item.Body.(*btrfsitem.Root); ok { - kps = append(kps, Edge{ + kps = append(kps, GraphEdge{ FromRoot: nodeRef.Addr, FromItem: i, FromTree: item.Key.ObjectID, @@ -175,9 +175,9 @@ func (g Graph) InsertNode(nodeRef *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.No } } else { g.Nodes[nodeRef.Addr] = nodeData - kps := make([]Edge, len(nodeRef.Data.BodyInternal)) + kps := make([]GraphEdge, len(nodeRef.Data.BodyInternal)) for i, kp := range nodeRef.Data.BodyInternal { - kps[i] = Edge{ + kps[i] = GraphEdge{ FromNode: nodeRef.Addr, FromItem: i, FromTree: nodeRef.Data.Head.Owner, diff --git a/lib/btrfsutil/graph_loops.go b/lib/btrfsutil/graph_loops.go index 9563d19..3382705 100644 --- a/lib/btrfsutil/graph_loops.go +++ b/lib/btrfsutil/graph_loops.go @@ -42,7 +42,7 @@ func (g Graph) renderNode(node btrfsvol.LogicalAddr) []string { } } -func (g Graph) renderEdge(kp Edge) []string { +func (g Graph) renderEdge(kp GraphEdge) []string { a := fmt.Sprintf("[%d]={", kp.FromItem) b := strings.Repeat(" ", len(a)) ret := []string{ @@ -110,7 +110,7 @@ func (g Graph) renderLoop(stack []btrfsvol.LogicalAddr) []string { return lines } -func checkNodeExpectations(kp Edge, toNode Node) error { +func checkNodeExpectations(kp GraphEdge, toNode GraphNode) error { var errs derror.MultiError if toNode.Level != kp.ToLevel { errs = append(errs, fmt.Errorf("kp.level=%v != node.level=%v", diff --git a/lib/btrfsutil/old_rebuilt_forrest.go b/lib/btrfsutil/old_rebuilt_forrest.go index b7663fa..2386803 100644 --- a/lib/btrfsutil/old_rebuilt_forrest.go +++ b/lib/btrfsutil/old_rebuilt_forrest.go @@ -21,60 +21,60 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/diskio" ) -type treeIndex struct { - TreeRootErr error - Items *containers.RBTree[treeIndexValue] - Errors *containers.IntervalTree[btrfsprim.Key, treeIndexError] +type oldRebuiltTree struct { + RootErr error + Items *containers.RBTree[oldRebuiltTreeValue] + Errors *containers.IntervalTree[btrfsprim.Key, oldRebuiltTreeError] } -type treeIndexError struct { +type oldRebuiltTreeError struct { Path SkinnyPath Err error } -type treeIndexValue struct { +type oldRebuiltTreeValue struct { Path SkinnyPath Key btrfsprim.Key ItemSize uint32 } // Compare implements containers.Ordered. -func (a treeIndexValue) Compare(b treeIndexValue) int { +func (a oldRebuiltTreeValue) Compare(b oldRebuiltTreeValue) int { return a.Key.Compare(b.Key) } -func newTreeIndex(arena *SkinnyPathArena) treeIndex { - return treeIndex{ - Items: new(containers.RBTree[treeIndexValue]), - Errors: &containers.IntervalTree[btrfsprim.Key, treeIndexError]{ - MinFn: func(err treeIndexError) btrfsprim.Key { +func newOldRebuiltTree(arena *SkinnyPathArena) oldRebuiltTree { + return oldRebuiltTree{ + Items: new(containers.RBTree[oldRebuiltTreeValue]), + Errors: &containers.IntervalTree[btrfsprim.Key, oldRebuiltTreeError]{ + MinFn: func(err oldRebuiltTreeError) btrfsprim.Key { return arena.Inflate(err.Path).Node(-1).ToKey }, - MaxFn: func(err treeIndexError) btrfsprim.Key { + MaxFn: func(err oldRebuiltTreeError) btrfsprim.Key { return arena.Inflate(err.Path).Node(-1).ToMaxKey }, }, } } -type brokenTrees struct { +type OldRebuiltForrest struct { ctx context.Context //nolint:containedctx // don't have an option while keeping the same API inner *btrfs.FS arena *SkinnyPathArena // btrfsprim.ROOT_TREE_OBJECTID - rootTreeMu sync.Mutex - rootTreeIndex *treeIndex + rootTreeMu sync.Mutex + rootTree *oldRebuiltTree // for all other trees - treeMu sync.Mutex - treeIndexes map[btrfsprim.ObjID]treeIndex + treesMu sync.Mutex + trees map[btrfsprim.ObjID]oldRebuiltTree } -var _ btrfstree.TreeOperator = (*brokenTrees)(nil) +var _ btrfstree.TreeOperator = (*OldRebuiltForrest)(nil) -// NewBrokenTrees wraps a *btrfs.FS to support looking up information -// from broken trees. +// NewOldRebuiltForrest wraps a *btrfs.FS to support looking up +// information from broken trees. // // Of the btrfstree.TreeOperator methods: // @@ -87,43 +87,38 @@ var _ btrfstree.TreeOperator = (*brokenTrees)(nil) // broken tree might not be), and a bad node may cause it to not // return a truncated list of results. // -// NewBrokenTrees attempts to remedy these deficiencies by using +// NewOldRebuiltForrest attempts to remedy these deficiencies by using // .TreeWalk to build an out-of-FS index of all of the items in the // tree, and re-implements TreeLookup, TreeSearch, and TreeSearchAll // using that index. -func NewBrokenTrees(ctx context.Context, inner *btrfs.FS) interface { - btrfstree.TreeOperator - Superblock() (*btrfstree.Superblock, error) - ReadAt(p []byte, off btrfsvol.LogicalAddr) (int, error) - Augment(treeID btrfsprim.ObjID, nodeAddr btrfsvol.LogicalAddr) ([]btrfsprim.Key, error) -} { - return &brokenTrees{ +func NewOldRebuiltForrest(ctx context.Context, inner *btrfs.FS) *OldRebuiltForrest { + return &OldRebuiltForrest{ ctx: ctx, inner: inner, } } -func (bt *brokenTrees) treeIndex(treeID btrfsprim.ObjID) treeIndex { +func (bt *OldRebuiltForrest) RebuiltTree(treeID btrfsprim.ObjID) oldRebuiltTree { var treeRoot *btrfstree.TreeRoot var sb *btrfstree.Superblock var err error if treeID == btrfsprim.ROOT_TREE_OBJECTID { bt.rootTreeMu.Lock() defer bt.rootTreeMu.Unlock() - if bt.rootTreeIndex != nil { - return *bt.rootTreeIndex + if bt.rootTree != nil { + return *bt.rootTree } sb, err = bt.inner.Superblock() if err == nil { treeRoot, err = btrfstree.LookupTreeRoot(bt.inner, *sb, treeID) } } else { - bt.treeMu.Lock() - defer bt.treeMu.Unlock() - if bt.treeIndexes == nil { - bt.treeIndexes = make(map[btrfsprim.ObjID]treeIndex) + bt.treesMu.Lock() + defer bt.treesMu.Unlock() + if bt.trees == nil { + bt.trees = make(map[btrfsprim.ObjID]oldRebuiltTree) } - if cacheEntry, exists := bt.treeIndexes[treeID]; exists { + if cacheEntry, exists := bt.trees[treeID]; exists { return cacheEntry } sb, err = bt.inner.Superblock() @@ -141,23 +136,23 @@ func (bt *brokenTrees) treeIndex(treeID btrfsprim.ObjID) treeIndex { SB: _sb, } } - cacheEntry := newTreeIndex(bt.arena) + cacheEntry := newOldRebuiltTree(bt.arena) if err != nil { - cacheEntry.TreeRootErr = err + cacheEntry.RootErr = err } else { dlog.Infof(bt.ctx, "indexing tree %v...", treeID) bt.rawTreeWalk(*treeRoot, cacheEntry, nil) dlog.Infof(bt.ctx, "... done indexing tree %v", treeID) } if treeID == btrfsprim.ROOT_TREE_OBJECTID { - bt.rootTreeIndex = &cacheEntry + bt.rootTree = &cacheEntry } else { - bt.treeIndexes[treeID] = cacheEntry + bt.trees[treeID] = cacheEntry } return cacheEntry } -func (bt *brokenTrees) rawTreeWalk(root btrfstree.TreeRoot, cacheEntry treeIndex, walked *[]btrfsprim.Key) { +func (bt *OldRebuiltForrest) rawTreeWalk(root btrfstree.TreeRoot, cacheEntry oldRebuiltTree, walked *[]btrfsprim.Key) { btrfstree.TreeOperatorImpl{NodeSource: bt.inner}.RawTreeWalk( bt.ctx, root, @@ -167,20 +162,20 @@ func (bt *brokenTrees) rawTreeWalk(root btrfstree.TreeRoot, cacheEntry treeIndex // indicates a bug in my item parser than a problem with the filesystem. panic(fmt.Errorf("TODO: error parsing item: %w", err)) } - cacheEntry.Errors.Insert(treeIndexError{ + cacheEntry.Errors.Insert(oldRebuiltTreeError{ Path: bt.arena.Deflate(err.Path), Err: err.Err, }) }, btrfstree.TreeWalkHandler{ Item: func(path btrfstree.TreePath, item btrfstree.Item) error { - if cacheEntry.Items.Search(func(v treeIndexValue) int { return item.Key.Compare(v.Key) }) != nil { + if cacheEntry.Items.Search(func(v oldRebuiltTreeValue) int { return item.Key.Compare(v.Key) }) != nil { // 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 key=%v in tree=%v", item.Key, root.TreeID)) } - cacheEntry.Items.Insert(treeIndexValue{ + cacheEntry.Items.Insert(oldRebuiltTreeValue{ Path: bt.arena.Deflate(path), Key: item.Key, ItemSize: item.BodySize, @@ -194,7 +189,7 @@ func (bt *brokenTrees) rawTreeWalk(root btrfstree.TreeRoot, cacheEntry treeIndex ) } -func (bt *brokenTrees) TreeLookup(treeID btrfsprim.ObjID, key btrfsprim.Key) (btrfstree.Item, error) { +func (bt *OldRebuiltForrest) TreeLookup(treeID btrfsprim.ObjID, key btrfsprim.Key) (btrfstree.Item, error) { item, err := bt.TreeSearch(treeID, btrfstree.KeySearch(key.Compare)) if err != nil { err = fmt.Errorf("item with key=%v: %w", key, err) @@ -202,11 +197,11 @@ func (bt *brokenTrees) TreeLookup(treeID btrfsprim.ObjID, key btrfsprim.Key) (bt return item, err } -func (bt *brokenTrees) addErrs(index treeIndex, fn func(btrfsprim.Key, uint32) int, err error) error { +func (bt *OldRebuiltForrest) addErrs(tree oldRebuiltTree, fn func(btrfsprim.Key, uint32) int, err error) error { var errs derror.MultiError - index.Errors.Subrange( + tree.Errors.Subrange( func(k btrfsprim.Key) int { return fn(k, 0) }, - func(v treeIndexError) bool { + func(v oldRebuiltTreeError) bool { errs = append(errs, &btrfstree.TreeError{ Path: bt.arena.Inflate(v.Path), Err: v.Err, @@ -222,24 +217,24 @@ func (bt *brokenTrees) addErrs(index treeIndex, fn func(btrfsprim.Key, uint32) i return errs } -func (bt *brokenTrees) TreeSearch(treeID btrfsprim.ObjID, fn func(btrfsprim.Key, uint32) int) (btrfstree.Item, error) { - index := bt.treeIndex(treeID) - if index.TreeRootErr != nil { - return btrfstree.Item{}, index.TreeRootErr +func (bt *OldRebuiltForrest) TreeSearch(treeID btrfsprim.ObjID, fn func(btrfsprim.Key, uint32) int) (btrfstree.Item, error) { + tree := bt.RebuiltTree(treeID) + if tree.RootErr != nil { + return btrfstree.Item{}, tree.RootErr } - indexItem := index.Items.Search(func(indexItem treeIndexValue) int { + indexItem := tree.Items.Search(func(indexItem oldRebuiltTreeValue) int { return fn(indexItem.Key, indexItem.ItemSize) }) if indexItem == nil { - return btrfstree.Item{}, bt.addErrs(index, fn, iofs.ErrNotExist) + return btrfstree.Item{}, bt.addErrs(tree, fn, iofs.ErrNotExist) } 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) + return btrfstree.Item{}, bt.addErrs(tree, fn, err) } item := node.Data.BodyLeaf[itemPath.Node(-1).FromItemIdx] @@ -250,21 +245,21 @@ func (bt *brokenTrees) TreeSearch(treeID btrfsprim.ObjID, fn func(btrfsprim.Key, return item, nil } -func (bt *brokenTrees) TreeSearchAll(treeID btrfsprim.ObjID, fn func(btrfsprim.Key, uint32) int) ([]btrfstree.Item, error) { - index := bt.treeIndex(treeID) - if index.TreeRootErr != nil { - return nil, index.TreeRootErr +func (bt *OldRebuiltForrest) TreeSearchAll(treeID btrfsprim.ObjID, fn func(btrfsprim.Key, uint32) int) ([]btrfstree.Item, error) { + tree := bt.RebuiltTree(treeID) + if tree.RootErr != nil { + return nil, tree.RootErr } - var indexItems []treeIndexValue - index.Items.Subrange( - func(indexItem treeIndexValue) int { return fn(indexItem.Key, indexItem.ItemSize) }, - func(node *containers.RBNode[treeIndexValue]) bool { + var indexItems []oldRebuiltTreeValue + tree.Items.Subrange( + func(indexItem oldRebuiltTreeValue) int { return fn(indexItem.Key, indexItem.ItemSize) }, + func(node *containers.RBNode[oldRebuiltTreeValue]) bool { indexItems = append(indexItems, node.Value) return true }) if len(indexItems) == 0 { - return nil, bt.addErrs(index, fn, iofs.ErrNotExist) + return nil, bt.addErrs(tree, fn, iofs.ErrNotExist) } ret := make([]btrfstree.Item, len(indexItems)) @@ -277,7 +272,7 @@ func (bt *brokenTrees) TreeSearchAll(treeID btrfsprim.ObjID, fn func(btrfsprim.K node, err = bt.inner.ReadNode(itemPath.Parent()) if err != nil { btrfstree.FreeNodeRef(node) - return nil, bt.addErrs(index, fn, err) + return nil, bt.addErrs(tree, fn, err) } } ret[i] = node.Data.BodyLeaf[itemPath.Node(-1).FromItemIdx] @@ -285,23 +280,23 @@ func (bt *brokenTrees) TreeSearchAll(treeID btrfsprim.ObjID, fn func(btrfsprim.K } btrfstree.FreeNodeRef(node) - return ret, bt.addErrs(index, fn, nil) + return ret, bt.addErrs(tree, fn, nil) } -func (bt *brokenTrees) TreeWalk(ctx context.Context, treeID btrfsprim.ObjID, errHandle func(*btrfstree.TreeError), cbs btrfstree.TreeWalkHandler) { - index := bt.treeIndex(treeID) - if index.TreeRootErr != nil { +func (bt *OldRebuiltForrest) TreeWalk(ctx context.Context, treeID btrfsprim.ObjID, errHandle func(*btrfstree.TreeError), cbs btrfstree.TreeWalkHandler) { + tree := bt.RebuiltTree(treeID) + if tree.RootErr != nil { errHandle(&btrfstree.TreeError{ Path: btrfstree.TreePath{{ FromTree: treeID, ToMaxKey: btrfsprim.MaxKey, }}, - Err: index.TreeRootErr, + Err: tree.RootErr, }) return } var node *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.Node] - index.Items.Range(func(indexItem *containers.RBNode[treeIndexValue]) bool { + tree.Items.Range(func(indexItem *containers.RBNode[oldRebuiltTreeValue]) bool { if ctx.Err() != nil { return false } @@ -330,22 +325,22 @@ func (bt *brokenTrees) TreeWalk(ctx context.Context, treeID btrfsprim.ObjID, err btrfstree.FreeNodeRef(node) } -func (bt *brokenTrees) Superblock() (*btrfstree.Superblock, error) { +func (bt *OldRebuiltForrest) Superblock() (*btrfstree.Superblock, error) { return bt.inner.Superblock() } -func (bt *brokenTrees) ReadAt(p []byte, off btrfsvol.LogicalAddr) (int, error) { +func (bt *OldRebuiltForrest) ReadAt(p []byte, off btrfsvol.LogicalAddr) (int, error) { return bt.inner.ReadAt(p, off) } -func (bt *brokenTrees) Augment(treeID btrfsprim.ObjID, nodeAddr btrfsvol.LogicalAddr) ([]btrfsprim.Key, error) { +func (bt *OldRebuiltForrest) Augment(treeID btrfsprim.ObjID, nodeAddr btrfsvol.LogicalAddr) ([]btrfsprim.Key, error) { sb, err := bt.Superblock() if err != nil { return nil, err } - index := bt.treeIndex(treeID) - if index.TreeRootErr != nil { - return nil, index.TreeRootErr + tree := bt.RebuiltTree(treeID) + if tree.RootErr != nil { + return nil, tree.RootErr } nodeRef, err := btrfstree.ReadNode[btrfsvol.LogicalAddr](bt.inner, *sb, nodeAddr, btrfstree.NodeExpectations{}) defer btrfstree.FreeNodeRef(nodeRef) @@ -358,6 +353,6 @@ func (bt *brokenTrees) Augment(treeID btrfsprim.ObjID, nodeAddr btrfsvol.Logical RootNode: nodeAddr, Level: nodeRef.Data.Head.Level, Generation: nodeRef.Data.Head.Generation, - }, index, &ret) + }, tree, &ret) return ret, nil } diff --git a/lib/btrfsutil/rebuilt_forrest.go b/lib/btrfsutil/rebuilt_forrest.go index 8d4b810..3dfb24c 100644 --- a/lib/btrfsutil/rebuilt_forrest.go +++ b/lib/btrfsutil/rebuilt_forrest.go @@ -18,7 +18,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/textui" ) -type Callbacks interface { +type RebuiltForrestCallbacks 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) @@ -29,23 +29,23 @@ type Callbacks interface { // 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. +// broken-tree handling to OldRebuiltForrest. However, the API is +// different than btrfstree.TreeOperator, and is much more efficient +// than OldRebuiltForrest. // // The efficiency improvements are possible because of the API // differences, which are necessary for how it is used in -// rebuildnodes: +// rebuildtrees: // -// - it consumes an already-read graph.Graph instead of reading the -// graph itself +// - it consumes an already-read 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: +// Additionally, it provides some functionality that OldRebuiltForrest +// does not: // // - it provides a .LeafToRoots() method to advise on what // additional roots should be added @@ -60,7 +60,7 @@ type RebuiltForrest struct { sb btrfstree.Superblock graph Graph keyIO *KeyIO - cb Callbacks + cb RebuiltForrestCallbacks // mutable treesMu nestedMutex @@ -72,7 +72,7 @@ type RebuiltForrest struct { // NewRebuiltForrest returns a new RebuiltForrest instance. All of // the callbacks must be non-nil. -func NewRebuiltForrest(sb btrfstree.Superblock, graph Graph, keyIO *KeyIO, cb Callbacks) *RebuiltForrest { +func NewRebuiltForrest(sb btrfstree.Superblock, graph Graph, keyIO *KeyIO, cb RebuiltForrestCallbacks) *RebuiltForrest { return &RebuiltForrest{ sb: sb, graph: graph, -- cgit v1.2.3-2-g168b