From 1ba83231195ea3b78ce545f4518f70c74819345b Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 31 Mar 2023 10:28:02 -0600 Subject: btrfsutil: Add a ReadGraph function, rebuildtrees: Clean up scan to match --- lib/btrfsutil/graph.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'lib/btrfsutil') diff --git a/lib/btrfsutil/graph.go b/lib/btrfsutil/graph.go index fe7fe70..7863e0d 100644 --- a/lib/btrfsutil/graph.go +++ b/lib/btrfsutil/graph.go @@ -12,6 +12,7 @@ import ( "github.com/datawire/dlib/dlog" + "git.lukeshu.com/btrfs-progs-ng/lib/btrfs" "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" @@ -283,3 +284,57 @@ func (g Graph) FinalCheck(ctx context.Context, fs btrfstree.NodeSource) error { return nil } + +func ReadGraph(_ctx context.Context, fs *btrfs.FS, nodeList []btrfsvol.LogicalAddr) (Graph, error) { + // read-superblock ///////////////////////////////////////////////////////////// + ctx := dlog.WithField(_ctx, "btrfs.util.read-graph.step", "read-superblock") + dlog.Info(ctx, "Reading superblock...") + sb, err := fs.Superblock() + if err != nil { + return Graph{}, err + } + + // read-roots ////////////////////////////////////////////////////////////////// + ctx = dlog.WithField(_ctx, "btrfs.util.read-graph.step", "read-roots") + graph := NewGraph(ctx, *sb) + + // read-nodes ////////////////////////////////////////////////////////////////// + ctx = dlog.WithField(_ctx, "btrfs.util.read-graph.step", "read-nodes") + dlog.Infof(ctx, "Reading node data from FS...") + var stats textui.Portion[int] + stats.D = len(nodeList) + progressWriter := textui.NewProgress[textui.Portion[int]]( + ctx, + dlog.LogLevelInfo, + textui.Tunable(1*time.Second)) + progressWriter.Set(stats) + for _, laddr := range nodeList { + if err := ctx.Err(); err != nil { + return Graph{}, err + } + node, err := fs.AcquireNode(ctx, laddr, btrfstree.NodeExpectations{ + LAddr: containers.OptionalValue(laddr), + }) + if err != nil { + fs.ReleaseNode(node) + return Graph{}, err + } + graph.InsertNode(node) + fs.ReleaseNode(node) + stats.N++ + progressWriter.Set(stats) + } + if stats.N != stats.D { + panic("should not happen") + } + progressWriter.Done() + dlog.Info(ctx, "... done reading node data") + + // check /////////////////////////////////////////////////////////////////////// + ctx = dlog.WithField(_ctx, "btrfs.util.read-graph.step", "check") + if err := graph.FinalCheck(ctx, fs); err != nil { + return Graph{}, err + } + + return graph, nil +} -- cgit v1.2.3-2-g168b