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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package rebuildnodes
import (
"archive/zip"
"context"
"errors"
"fmt"
"html"
"io"
iofs "io/fs"
"strings"
"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"
"git.lukeshu.com/btrfs-progs-ng/lib/maps"
)
func ptrTo[T any](x T) *T {
return &x
}
func getCliques(uuidMap uuidMap, treeAncestors map[btrfsprim.ObjID]containers.Set[btrfsprim.ObjID]) map[btrfsprim.ObjID]*containers.Set[btrfsprim.ObjID] {
cliques := make(map[btrfsprim.ObjID]*containers.Set[btrfsprim.ObjID])
lister := newFullAncestorLister(uuidMap, treeAncestors)
for _, treeID := range maps.SortedKeys(uuidMap.SeenTrees) {
clique := ptrTo(make(containers.Set[btrfsprim.ObjID]))
clique.Insert(treeID)
clique.InsertFrom(lister.GetFullAncestors(treeID))
for _, id := range maps.SortedKeys(*clique) {
if existingClique, ok := cliques[id]; ok {
clique.InsertFrom(*existingClique)
}
cliques[id] = clique
}
}
return cliques
}
func getCliqueID(cliques map[btrfsprim.ObjID]*containers.Set[btrfsprim.ObjID], treeID btrfsprim.ObjID) btrfsprim.ObjID {
clique, ok := cliques[treeID]
if !ok {
panic(fmt.Errorf("tree ID %v was not in .SeenTrees", treeID))
}
return maps.SortedKeys(*clique)[0]
}
func VisualizeNodes(ctx context.Context, out io.Writer, fs *btrfs.FS, nodeScanResults btrfsinspect.ScanDevicesResult) error {
uuidMap, err := buildUUIDMap(ctx, fs, nodeScanResults)
if err != nil {
return err
}
nfs := &RebuiltTrees{
inner: fs,
uuidMap: uuidMap,
}
orphanedNodes, _, treeAncestors, err := classifyNodes(ctx, nfs, nodeScanResults)
if err != nil {
return err
}
uuidMap.considerAncestors(ctx, treeAncestors)
////////////////////////////////////////////////////////////////////////////////////////////
cliques := getCliques(uuidMap, treeAncestors)
dlog.Info(ctx, "Building graphviz graph...")
nodes := make(map[btrfsprim.ObjID]containers.Set[string]) // grouped by treeID
edges := make(map[btrfsprim.ObjID]containers.Set[string]) // grouped by cliqueID
visitedNodes := make(containers.Set[btrfsvol.LogicalAddr])
var isOrphan bool
nodeHandler := func(path btrfstree.TreePath, nodeRef *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.Node], err error) error {
addr := path.Node(-1).ToNodeAddr
// Node
var treeID btrfsprim.ObjID
var nodeStr string
if err != nil && (errors.Is(err, btrfstree.ErrNotANode) || errors.As(err, new(*btrfstree.IOError))) {
treeID = 0
nodeStr = fmt.Sprintf(`n%d [shape=star color=red label="%v"]`, addr, addr)
} else {
treeID = nodeRef.Data.Head.Owner
var buf strings.Builder
fmt.Fprintf(&buf, `n%d [shape=record label="%v\ngen=%v\nlvl=%v|{`,
addr,
nodeRef.Data.Head.Addr,
nodeRef.Data.Head.Generation,
nodeRef.Data.Head.Level)
if nodeRef.Data.Head.NumItems == 0 {
buf.WriteString("(no items)")
} else {
for i := uint32(0); i < nodeRef.Data.Head.NumItems; i++ {
if i == 0 {
fmt.Fprintf(&buf, "<p%[1]d>%[1]d", i)
} else {
fmt.Fprintf(&buf, "|<p%[1]d>%[1]d", i)
}
}
}
buf.WriteString(`}"]`)
nodeStr = buf.String()
}
if _, ok := nodes[treeID]; !ok {
nodes[treeID] = make(containers.Set[string])
nodes[treeID].Insert(fmt.Sprintf(`t%d [label="%s"]`, treeID, html.EscapeString(treeID.String())))
}
nodes[treeID].Insert(nodeStr)
// Edge
var edge strings.Builder
if len(path) == 1 {
if isOrphan {
edge.WriteString("orphanRoot")
} else {
fmt.Fprintf(&edge, "t%d", path[0].FromTree)
}
} else {
fmt.Fprintf(&edge, "n%d:p%d", path.Node(-2).ToNodeAddr, path.Node(-1).FromItemIdx)
}
fmt.Fprintf(&edge, ` -> n%d [label="`, addr)
if path.Node(-1).FromItemIdx >= 0 {
fmt.Fprintf(&edge, "%d: key=(%d,%v,%d) gen=%v",
path.Node(-1).FromItemIdx,
path.Node(-1).ToKey.ObjectID,
path.Node(-1).ToKey.ItemType,
path.Node(-1).ToKey.Offset,
path.Node(-1).ToNodeGeneration)
}
if err != nil {
fmt.Fprintf(&edge, `\n\n%s" color=red]`, html.EscapeString(err.Error()))
} else {
edge.WriteString(`"]`)
}
cliqueID := getCliqueID(cliques, path[0].FromTree)
if treeID != 0 && getCliqueID(cliques, treeID) != cliqueID {
panic(fmt.Errorf("tree %v is not in clique %v", treeID, maps.SortedKeys(*cliques[cliqueID])))
}
if !cliques[cliqueID].Has(cliqueID) {
panic(fmt.Errorf("clique %v does not contain supposed-member %v", maps.SortedKeys(*cliques[cliqueID]), cliqueID))
}
if _, ok := edges[cliqueID]; !ok {
edges[cliqueID] = make(containers.Set[string])
}
edges[cliqueID].Insert(edge.String())
// Return
if visitedNodes.Has(addr) {
return iofs.SkipDir
}
visitedNodes.Insert(addr)
return err
}
walkHandler := btrfstree.TreeWalkHandler{
Node: func(path btrfstree.TreePath, nodeRef *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.Node]) error {
return nodeHandler(path, nodeRef, nil)
},
BadNode: func(path btrfstree.TreePath, nodeRef *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.Node], err error) error {
return nodeHandler(path, nodeRef, err)
},
}
btrfsutil.WalkAllTrees(ctx, nfs, btrfsutil.WalkAllTreesHandler{
TreeWalkHandler: walkHandler,
Err: func(err *btrfsutil.WalkError) {
// do nothing
},
})
isOrphan = true
for _, potentialRoot := range maps.SortedKeys(orphanedNodes) {
walkFromNode(ctx, nfs, potentialRoot,
func(err *btrfstree.TreeError) {
// do nothing
},
walkHandler,
)
}
dlog.Info(ctx, "... done building")
////////////////////////////////////////////////////////////////////////////////////////////
dlog.Info(ctx, "Writing graphviz output...")
cliqueIDs := maps.SortedKeys(edges)
zw := zip.NewWriter(out)
for _, cliqueID := range cliqueIDs {
if err := func() (err error) {
buf, err := zw.Create(fmt.Sprintf("%d.dot", cliqueID))
if err != nil {
return err
}
if _, err := fmt.Fprintf(buf, "strict digraph clique%d {\n", cliqueID); err != nil {
return err
}
clique := cliques[cliqueID]
for _, treeID := range maps.SortedKeys(*clique) {
if _, err := fmt.Fprintf(buf, " subgraph cluster_tree%d {\n", treeID); err != nil {
return err
}
for _, node := range maps.SortedKeys(nodes[treeID]) {
if _, err := fmt.Fprintf(buf, " %s;\n", node); err != nil {
return err
}
}
if _, err := fmt.Fprintln(buf, " }"); err != nil {
return err
}
}
for _, edge := range maps.SortedKeys(edges[cliqueID]) {
if _, err := fmt.Fprintf(buf, " %s;\n", edge); err != nil {
return err
}
}
if _, err := fmt.Fprintln(buf, "}"); err != nil {
return err
}
return nil
}(); err != nil {
return err
}
}
if err := zw.Close(); err != nil {
return err
}
dlog.Info(ctx, "... done writing")
return nil
}
|