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
|
// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package graph
import (
"fmt"
"io"
"strings"
"github.com/datawire/dlib/derror"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsprim"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
"git.lukeshu.com/btrfs-progs-ng/lib/containers"
"git.lukeshu.com/btrfs-progs-ng/lib/maps"
"git.lukeshu.com/btrfs-progs-ng/lib/slices"
)
func (g Graph) ShowLoops(out io.Writer) {
orphans := make(containers.Set[btrfsvol.LogicalAddr])
for node := range g.Nodes {
if len(g.EdgesTo[node]) == 0 {
orphans.Insert(node)
}
}
loopWalk(out, g, 0)
for _, orphan := range maps.SortedKeys(orphans) {
loopWalk(out, g, orphan)
}
}
func loopWalk(out io.Writer, scanData Graph, stack ...btrfsvol.LogicalAddr) {
for _, kp := range scanData.EdgesFrom[stack[len(stack)-1]] {
childStack := append(stack, kp.ToNode)
if slices.Contains(kp.ToNode, stack) {
loopRender(out, scanData, childStack...)
} else {
loopWalk(out, scanData, childStack...)
}
}
}
func nodeRender(scanData Graph, node btrfsvol.LogicalAddr) []string {
if node == 0 {
return []string{"root"}
} else if nodeData, ok := scanData.Nodes[node]; ok {
return []string{
fmt.Sprintf("{addr: %v,", node),
fmt.Sprintf(" level: %v,", nodeData.Level),
fmt.Sprintf(" gen: %v,", nodeData.Generation),
fmt.Sprintf(" num_items: %v,", nodeData.NumItems),
fmt.Sprintf(" min_item: {%d,%v,%d},",
nodeData.MinItem.ObjectID,
nodeData.MinItem.ItemType,
nodeData.MinItem.Offset),
fmt.Sprintf(" max_item: {%d,%v,%d}}",
nodeData.MaxItem.ObjectID,
nodeData.MaxItem.ItemType,
nodeData.MaxItem.Offset),
}
} else if nodeErr, ok := scanData.BadNodes[node]; ok {
return []string{
fmt.Sprintf("{addr:%v,", node),
fmt.Sprintf(" err:%q}", nodeErr.Error()),
}
} else {
panic("should not happen")
}
}
func edgeRender(scanData Graph, kp Edge) []string {
a := fmt.Sprintf("[%d]={", kp.FromItem)
b := strings.Repeat(" ", len(a))
ret := []string{
a + fmt.Sprintf("ToNode: %v,", kp.ToNode),
b + fmt.Sprintf("ToLevel: %v,", kp.ToLevel),
b + fmt.Sprintf("ToGen: %v,", kp.ToGeneration),
b + fmt.Sprintf("ToKey: {%d,%v,%d}}",
kp.ToKey.ObjectID,
kp.ToKey.ItemType,
kp.ToKey.Offset),
}
var err error
if toNode, ok := scanData.Nodes[kp.ToNode]; !ok {
err = scanData.BadNodes[kp.ToNode]
} else {
err = checkNodeExpectations(kp, toNode)
}
if err != nil {
c := strings.Repeat(" ", len(a)-1)
ret = append(ret,
c+"^",
c+"`-err="+strings.ReplaceAll(err.Error(), "\n", "\n"+c+" "),
)
}
return ret
}
func loopRender(out io.Writer, scanData Graph, stack ...btrfsvol.LogicalAddr) {
var lines []string
add := func(suffixes []string) {
curLen := 0
for _, line := range lines {
if len(line) > curLen {
curLen = len(line)
}
}
for i, suffix := range suffixes {
if len(lines) <= i {
lines = append(lines, "")
}
if len(lines[i]) < curLen {
if i == 0 {
lines[i] += strings.Repeat("-", curLen-len(lines[i])-1) + ">"
} else {
lines[i] += strings.Repeat(" ", curLen-len(lines[i]))
}
}
lines[i] += suffix
}
}
for i, node := range stack {
if i > 0 {
for _, kp := range scanData.EdgesTo[node] {
if kp.FromNode == stack[i-1] {
add(edgeRender(scanData, *kp))
break
}
}
}
add(nodeRender(scanData, node))
}
fmt.Fprintln(out, "loop:")
for _, line := range lines {
fmt.Fprintln(out, " "+line)
}
}
func checkNodeExpectations(kp Edge, toNode Node) error {
var errs derror.MultiError
if toNode.Level != kp.ToLevel {
errs = append(errs, fmt.Errorf("kp.level=%v != node.level=%v",
kp.ToLevel, toNode.Level))
}
if toNode.Generation != kp.ToGeneration {
errs = append(errs, fmt.Errorf("kp.generation=%v != node.generation=%v",
kp.ToGeneration, toNode.Generation))
}
if toNode.NumItems == 0 {
errs = append(errs, fmt.Errorf("node.num_items=0"))
} else if kp.ToKey != (btrfsprim.Key{}) && toNode.MinItem != kp.ToKey {
errs = append(errs, fmt.Errorf("kp.key=%v != node.items[0].key=%v",
kp.ToKey, toNode.MinItem))
}
if len(errs) > 0 {
return errs
}
return nil
}
|