1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package rebuildtrees
import (
"context"
"time"
"github.com/datawire/dlib/dlog"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
"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/btrfsutil"
"git.lukeshu.com/btrfs-progs-ng/lib/containers"
"git.lukeshu.com/btrfs-progs-ng/lib/textui"
)
func ScanDevices(ctx context.Context, fs *btrfs.FS, nodeList []btrfsvol.LogicalAddr) (btrfstree.Superblock, btrfsutil.Graph, *btrfsutil.KeyIO, error) {
dlog.Info(ctx, "Reading superblock...")
sb, err := fs.Superblock()
if err != nil {
return btrfstree.Superblock{}, btrfsutil.Graph{}, nil, err
}
dlog.Infof(ctx, "Reading node data from FS...")
var stats textui.Portion[int]
stats.D = len(nodeList)
progressWriter := textui.NewProgress[textui.Portion[int]](
dlog.WithField(ctx, "btrfs.inspect.rebuild-trees.read.substep", "read-nodes"),
dlog.LogLevelInfo, textui.Tunable(1*time.Second))
nodeGraph := btrfsutil.NewGraph(*sb)
keyIO := btrfsutil.NewKeyIO(fs, *sb)
progressWriter.Set(stats)
for _, laddr := range nodeList {
if err := ctx.Err(); err != nil {
return btrfstree.Superblock{}, btrfsutil.Graph{}, nil, err
}
nodeRef, err := btrfstree.ReadNode[btrfsvol.LogicalAddr](fs, *sb, laddr, btrfstree.NodeExpectations{
LAddr: containers.Optional[btrfsvol.LogicalAddr]{OK: true, Val: laddr},
})
if err != nil {
btrfstree.FreeNodeRef(nodeRef)
return btrfstree.Superblock{}, btrfsutil.Graph{}, nil, err
}
nodeGraph.InsertNode(nodeRef)
keyIO.InsertNode(nodeRef)
btrfstree.FreeNodeRef(nodeRef)
stats.N++
progressWriter.Set(stats)
}
if stats.N != stats.D {
panic("should not happen")
}
progressWriter.Done()
dlog.Info(ctx, "... done reading node data")
ctx = dlog.WithField(ctx, "btrfs.inspect.rebuild-trees.read.substep", "check")
if err := nodeGraph.FinalCheck(ctx, fs, *sb); err != nil {
return btrfstree.Superblock{}, btrfsutil.Graph{}, nil, err
}
keyIO.SetGraph(*nodeGraph)
return *sb, *nodeGraph, keyIO, nil
}
|