From dc1eddc75a17687ee4d3bc301dd8b2ff253095fe Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 10 Mar 2023 19:31:50 -0700 Subject: cmd/btrfs-rec, btrfsutil: Delete OldRebuiltForrest --- lib/btrfsutil/rebuilt_forrest.go | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) (limited to 'lib/btrfsutil/rebuilt_forrest.go') diff --git a/lib/btrfsutil/rebuilt_forrest.go b/lib/btrfsutil/rebuilt_forrest.go index 79d8beb..0ff45a9 100644 --- a/lib/btrfsutil/rebuilt_forrest.go +++ b/lib/btrfsutil/rebuilt_forrest.go @@ -23,23 +23,8 @@ import ( // RebuiltForrest is an abstraction for rebuilding and accessing // potentially broken btrees. // -// It is conceptually a btrfstree.Forrest, and adds similar -// broken-tree handling to OldRebuiltForrest. However, it 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 -// rebuildtrees: -// -// - it consumes an already-read Graph instead of reading the graph -// itself -// -// - it does not use `btrfstree.Path` -// -// - it does not keep track of errors encountered in a tree -// -// Additionally, it provides some functionality that OldRebuiltForrest -// does not: +// Additionally, it provides some functionality on top of a vanilla +// btrfs.ReadableFS: // // - it provides a RebuiltForrest.RebuiltListRoots() method for // listing how trees have been repaired. -- cgit v1.2.3-2-g168b From 6630e28213a6c5f506d6a6d6f3e764a42c967163 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 5 Mar 2023 11:40:32 -0700 Subject: btrfsutil: RebuiltForrest: Add a .RebuiltAddRoots() method, cmd/btrfs-rec: Add a --trees flag --- lib/btrfsutil/rebuilt_forrest.go | 50 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) (limited to 'lib/btrfsutil/rebuilt_forrest.go') diff --git a/lib/btrfsutil/rebuilt_forrest.go b/lib/btrfsutil/rebuilt_forrest.go index 0ff45a9..4ce2435 100644 --- a/lib/btrfsutil/rebuilt_forrest.go +++ b/lib/btrfsutil/rebuilt_forrest.go @@ -26,11 +26,15 @@ import ( // Additionally, it provides some functionality on top of a vanilla // btrfs.ReadableFS: // +// - it provides a RebuiltTree.RebuiltAddRoot() method for repairing a +// tree. +// // - it provides a RebuiltForrest.RebuiltListRoots() method for // listing how trees have been repaired. // -// - it provides a RebuiltTree.RebuiltAddRoot() method for repairing a -// tree. +// - it provides a RebuiltForrest.RebuiltAddRoots() method for +// batch-loading the results from +// RebuiltForrest.RebuiltListroots(). // // - it provides several RebuiltTree methods that provide advice on // what roots should be added to a tree in order to repair it: @@ -260,6 +264,48 @@ func (ts *RebuiltForrest) RebuiltListRoots(ctx context.Context) map[btrfsprim.Ob return ret } +// RebuiltAddRoots takes a listing of the root nodes for trees (as +// returned by RebuiltListRoots), and augments the trees to include +// them. +func (ts *RebuiltForrest) RebuiltAddRoots(ctx context.Context, roots map[btrfsprim.ObjID]containers.Set[btrfsvol.LogicalAddr]) { + ctx = ts.treesMu.Lock(ctx) + defer ts.treesMu.Unlock() + + essentialTrees := []btrfsprim.ObjID{ + btrfsprim.ROOT_TREE_OBJECTID, + btrfsprim.UUID_TREE_OBJECTID, + } + + for _, treeID := range essentialTrees { + treeRoots, ok := roots[treeID] + if !ok { + continue + } + tree, err := ts.RebuiltTree(ctx, treeID) + if err != nil { + dlog.Errorf(ctx, "RebuiltForrest.RebuiltAddRoots: cannot load essential tree %v: %v", treeID, err) + return + } + for _, root := range maps.SortedKeys(treeRoots) { + tree.RebuiltAddRoot(ctx, root) + } + } + + for _, treeID := range maps.SortedKeys(roots) { + if slices.Contains(treeID, essentialTrees) { + continue + } + tree, err := ts.RebuiltTree(ctx, treeID) + if err != nil { + dlog.Errorf(ctx, "RebuiltForrest.RebuiltAddRoots: cannot load non-essential tree %v: %v", treeID, err) + continue + } + for _, root := range maps.SortedKeys(roots[treeID]) { + tree.RebuiltAddRoot(ctx, root) + } + } +} + // btrfs.ReadableFS interface ////////////////////////////////////////////////////////////////////////////////////////// var _ btrfs.ReadableFS = (*RebuiltForrest)(nil) -- cgit v1.2.3-2-g168b