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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package rebuildnodes
import (
"context"
"math"
"github.com/datawire/dlib/dlog"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsprim"
"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/btrfsprogs/btrfsinspect"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsutil"
"git.lukeshu.com/btrfs-progs-ng/lib/containers"
"git.lukeshu.com/btrfs-progs-ng/lib/diskio"
)
func RebuildNodes(ctx context.Context, fs *btrfs.FS, nodeScanResults btrfsinspect.ScanDevicesResult) (map[btrfsvol.LogicalAddr]*RebuiltNode, error) {
uuidMap, err := buildUUIDMap(ctx, fs, nodeScanResults)
if err != nil {
return nil, err
}
nfs := &RebuiltTrees{
inner: fs,
uuidMap: uuidMap,
}
foundRoots, err := lostAndFoundNodes(ctx, nfs, nodeScanResults)
if err != nil {
return nil, err
}
dlog.Info(ctx, "Initializing nodes to re-build...")
rebuiltNodes, err := reInitBrokenNodes(ctx, nfs, nodeScanResults, foundRoots)
if err != nil {
return nil, err
}
dlog.Infof(ctx, "Initialized %d nodes", len(rebuiltNodes))
dlog.Info(ctx, "Attaching lost+found nodes to rebuilt nodes...")
if err := reAttachNodes(ctx, nfs, foundRoots, rebuiltNodes); err != nil {
return nil, err
}
dlog.Info(ctx, "... done attaching")
return rebuiltNodes, nil
}
var maxKey = btrfsprim.Key{
ObjectID: math.MaxUint64,
ItemType: math.MaxUint8,
Offset: math.MaxUint64,
}
func keyMm(key btrfsprim.Key) btrfsprim.Key {
switch {
case key.Offset > 0:
key.Offset--
case key.ItemType > 0:
key.ItemType--
case key.ObjectID > 0:
key.ObjectID--
}
return key
}
func spanOfTreePath(fs _FS, path btrfstree.TreePath) (btrfsprim.Key, btrfsprim.Key) {
// tree root error
if len(path) == 0 {
return btrfsprim.Key{}, maxKey
}
// item error
if path.Node(-1).ToNodeAddr == 0 {
// If we got an item error, then the node is readable
node, _ := fs.ReadNode(path.Parent())
key := node.Data.BodyLeaf[path.Node(-1).FromItemIdx].Key
return key, key
}
// node error
//
// assume that path.Node(-1).ToNodeAddr is not readable, but that path.Node(-2).ToNodeAddr is.
if len(path) == 1 {
return btrfsprim.Key{}, maxKey
}
parentNode, _ := fs.ReadNode(path.Parent())
low := parentNode.Data.BodyInternal[path.Node(-1).FromItemIdx].Key
var high btrfsprim.Key
if path.Node(-1).FromItemIdx+1 < len(parentNode.Data.BodyInternal) {
high = keyMm(parentNode.Data.BodyInternal[path.Node(-1).FromItemIdx+1].Key)
} else {
parentPath := path.Parent().DeepCopy()
_, high = spanOfTreePath(fs, parentPath)
}
return low, high
}
func walkFromNode(ctx context.Context, fs _FS, nodeAddr btrfsvol.LogicalAddr, errHandle func(*btrfstree.TreeError), cbs btrfstree.TreeWalkHandler) {
sb, _ := fs.Superblock()
nodeRef, _ := btrfstree.ReadNode[btrfsvol.LogicalAddr](fs, *sb, nodeAddr, btrfstree.NodeExpectations{
LAddr: containers.Optional[btrfsvol.LogicalAddr]{OK: true, Val: nodeAddr},
})
if nodeRef == nil {
return
}
treeInfo := btrfstree.TreeRoot{
TreeID: nodeRef.Data.Head.Owner,
RootNode: nodeAddr,
Level: nodeRef.Data.Head.Level,
Generation: nodeRef.Data.Head.Generation,
}
btrfstree.TreeOperatorImpl{NodeSource: fs}.RawTreeWalk(ctx, treeInfo, errHandle, cbs)
}
func countNodes(nodeScanResults btrfsinspect.ScanDevicesResult) int {
var cnt int
for _, devResults := range nodeScanResults {
cnt += len(devResults.FoundNodes)
}
return cnt
}
func getChunkTreeUUID(ctx context.Context, fs _FS) (btrfsprim.UUID, bool) {
ctx, cancel := context.WithCancel(ctx)
var ret btrfsprim.UUID
var retOK bool
btrfsutil.WalkAllTrees(ctx, fs, btrfsutil.WalkAllTreesHandler{
TreeWalkHandler: btrfstree.TreeWalkHandler{
Node: func(path btrfstree.TreePath, node *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.Node]) error {
ret = node.Data.Head.ChunkTreeUUID
retOK = true
cancel()
return nil
},
},
Err: func(err *btrfsutil.WalkError) {
// do nothing
dlog.Errorf(ctx, "dbg err: %v", err)
},
})
return ret, retOK
}
type RebuiltNode struct {
Err error
MinKey, MaxKey btrfsprim.Key
btrfstree.Node
}
|