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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package rebuildnodes
import (
"context"
"fmt"
"strings"
"github.com/datawire/dlib/dlog"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsprim"
"git.lukeshu.com/btrfs-progs-ng/lib/containers"
"git.lukeshu.com/btrfs-progs-ng/lib/maps"
)
type uuidMap struct {
ObjID2UUID map[btrfsprim.ObjID]btrfsprim.UUID
UUID2ObjID map[btrfsprim.UUID]btrfsprim.ObjID
TreeParent map[btrfsprim.ObjID]btrfsprim.UUID
SeenTrees containers.Set[btrfsprim.ObjID]
}
func (m uuidMap) missingRootItems() containers.Set[btrfsprim.ObjID] {
missing := make(containers.Set[btrfsprim.ObjID])
for treeID := range m.SeenTrees {
if _, ok := m.ObjID2UUID[treeID]; !ok && treeID != btrfsprim.ROOT_TREE_OBJECTID {
missing.Insert(treeID)
continue
}
if _, ok := m.TreeParent[treeID]; !ok && treeID >= btrfsprim.FIRST_FREE_OBJECTID && treeID <= btrfsprim.LAST_FREE_OBJECTID {
missing.Insert(treeID)
}
}
return missing
}
// ParentTree implements btrfstree.NodeFile.
func (m uuidMap) ParentTree(tree btrfsprim.ObjID) (btrfsprim.ObjID, bool) {
if tree < btrfsprim.FIRST_FREE_OBJECTID || tree > btrfsprim.LAST_FREE_OBJECTID {
// no parent
return 0, true
}
parentUUID, ok := m.TreeParent[tree]
if !ok {
// could not look up parent info
return 0, false
}
if parentUUID == (btrfsprim.UUID{}) {
// no parent
return 0, true
}
parentObjID, ok := m.UUID2ObjID[parentUUID]
if !ok {
// could not look up parent info
return 0, false
}
return parentObjID, true
}
type fullAncestorLister struct {
uuidMap uuidMap
treeAncestors map[btrfsprim.ObjID]containers.Set[btrfsprim.ObjID]
memos map[btrfsprim.ObjID]containers.Set[btrfsprim.ObjID]
}
func newFullAncestorLister(uuidMap uuidMap, treeAncestors map[btrfsprim.ObjID]containers.Set[btrfsprim.ObjID]) fullAncestorLister {
return fullAncestorLister{
uuidMap: uuidMap,
treeAncestors: treeAncestors,
memos: make(map[btrfsprim.ObjID]containers.Set[btrfsprim.ObjID]),
}
}
type loopError []btrfsprim.ObjID
func (le loopError) Error() string {
var buf strings.Builder
buf.WriteString("loop: ")
for i, treeID := range le {
if i > 0 {
buf.WriteString("->")
}
fmt.Fprintf(&buf, "%d", treeID)
}
return buf.String()
}
func (fa fullAncestorLister) GetFullAncestors(child btrfsprim.ObjID) containers.Set[btrfsprim.ObjID] {
if memoized, ok := fa.memos[child]; ok {
if memoized == nil {
panic(loopError{child})
}
return memoized
}
fa.memos[child] = nil
defer func() {
if r := recover(); r != nil {
if le, ok := r.(loopError); ok {
r = append(loopError{child}, le...)
}
panic(r)
}
}()
ret := make(containers.Set[btrfsprim.ObjID])
defer func() {
fa.memos[child] = ret
}()
// Try to use '.uuidMap' instead of '.treeAncestors' if possible.
knownAncestors := make(containers.Set[btrfsprim.ObjID])
if parent, ok := fa.uuidMap.ParentTree(child); ok {
if parent == 0 {
return ret
}
knownAncestors.Insert(parent)
} else {
knownAncestors.InsertFrom(fa.treeAncestors[child])
}
for _, ancestor := range maps.SortedKeys(knownAncestors) {
ret.Insert(ancestor)
ret.InsertFrom(fa.GetFullAncestors(ancestor))
}
return ret
}
func (m uuidMap) considerAncestors(ctx context.Context, treeAncestors map[btrfsprim.ObjID]containers.Set[btrfsprim.ObjID]) {
if missing := m.missingRootItems(); len(missing) == 0 {
return
} else {
dlog.Infof(ctx, "Rebuilding %d root items...", len(missing))
}
fa := newFullAncestorLister(m, treeAncestors)
for _, missingRoot := range maps.SortedKeys(m.missingRootItems()) {
if _, ok := m.TreeParent[missingRoot]; ok {
// This one is incomplete because it doesn't have a UUID, not
// because it doesn't have a parent.
continue
}
potentialParents := make(containers.Set[btrfsprim.ObjID])
potentialParents.InsertFrom(fa.GetFullAncestors(missingRoot))
for _, ancestor := range maps.SortedKeys(fa.GetFullAncestors(missingRoot)) {
potentialParents.DeleteFrom(fa.GetFullAncestors(ancestor))
}
if len(potentialParents) == 1 {
parent := potentialParents.TakeOne()
dlog.Infof(ctx, "... the parent of %v is %v", missingRoot, parent)
parentUUID, ok := m.ObjID2UUID[parent]
if !ok {
dlog.Errorf(ctx, "... but can't synthesize a root item because UUID of %v is unknown", parent)
continue
}
m.TreeParent[missingRoot] = parentUUID
}
}
if missing := m.missingRootItems(); len(missing) > 0 {
dlog.Errorf(ctx, "... could not rebuild root items for %d trees: %v", len(missing), maps.SortedKeys(missing))
} else {
dlog.Info(ctx, "... rebuilt all root items")
}
}
|