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
|
// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package btrfsutil
import (
"context"
"testing"
"github.com/datawire/dlib/dlog"
"github.com/stretchr/testify/assert"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsprim"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
)
type rebuiltForrestCallbacks struct {
addedItem func(ctx context.Context, tree btrfsprim.ObjID, key btrfsprim.Key)
addedRoot func(ctx context.Context, tree btrfsprim.ObjID, root btrfsvol.LogicalAddr)
lookupRoot func(ctx context.Context, tree btrfsprim.ObjID) (offset btrfsprim.Generation, item btrfsitem.Root, ok bool)
lookupUUID func(ctx context.Context, uuid btrfsprim.UUID) (id btrfsprim.ObjID, ok bool)
}
func (cbs rebuiltForrestCallbacks) AddedItem(ctx context.Context, tree btrfsprim.ObjID, key btrfsprim.Key) {
cbs.addedItem(ctx, tree, key)
}
func (cbs rebuiltForrestCallbacks) AddedRoot(ctx context.Context, tree btrfsprim.ObjID, root btrfsvol.LogicalAddr) {
cbs.addedRoot(ctx, tree, root)
}
func (cbs rebuiltForrestCallbacks) LookupRoot(ctx context.Context, tree btrfsprim.ObjID) (offset btrfsprim.Generation, item btrfsitem.Root, ok bool) {
return cbs.lookupRoot(ctx, tree)
}
func (cbs rebuiltForrestCallbacks) LookupUUID(ctx context.Context, uuid btrfsprim.UUID) (id btrfsprim.ObjID, ok bool) {
return cbs.lookupUUID(ctx, uuid)
}
func TestRebuiltTreeCycles(t *testing.T) {
t.Parallel()
ctx := dlog.NewTestContext(t, true)
type mockRoot struct {
ID btrfsprim.ObjID
UUID btrfsprim.UUID
ParentUUID btrfsprim.UUID
ParentGen btrfsprim.Generation
}
roots := []mockRoot{
{
ID: 306,
UUID: btrfsprim.MustParseUUID("00000000-0000-0000-0000-000000000006"),
ParentUUID: btrfsprim.MustParseUUID("00000000-0000-0000-0000-000000000005"),
ParentGen: 1005,
},
{
ID: 305,
UUID: btrfsprim.MustParseUUID("00000000-0000-0000-0000-000000000005"),
ParentUUID: btrfsprim.MustParseUUID("00000000-0000-0000-0000-000000000004"),
ParentGen: 1004,
},
{
ID: 304,
UUID: btrfsprim.MustParseUUID("00000000-0000-0000-0000-000000000004"),
ParentUUID: btrfsprim.MustParseUUID("00000000-0000-0000-0000-000000000003"),
ParentGen: 1003,
},
{
ID: 303,
UUID: btrfsprim.MustParseUUID("00000000-0000-0000-0000-000000000003"),
ParentUUID: btrfsprim.MustParseUUID("00000000-0000-0000-0000-000000000005"),
ParentGen: 1002,
},
}
cbs := rebuiltForrestCallbacks{
addedItem: func(ctx context.Context, tree btrfsprim.ObjID, key btrfsprim.Key) {
// do nothing
},
addedRoot: func(ctx context.Context, tree btrfsprim.ObjID, root btrfsvol.LogicalAddr) {
// do nothing
},
lookupRoot: func(ctx context.Context, tree btrfsprim.ObjID) (offset btrfsprim.Generation, item btrfsitem.Root, ok bool) {
for _, root := range roots {
if root.ID == tree {
return root.ParentGen, btrfsitem.Root{
Generation: 2000,
UUID: root.UUID,
ParentUUID: root.ParentUUID,
}, true
}
}
return 0, btrfsitem.Root{}, false
},
lookupUUID: func(ctx context.Context, uuid btrfsprim.UUID) (id btrfsprim.ObjID, ok bool) {
for _, root := range roots {
if root.UUID == uuid {
return root.ID, true
}
}
return 0, false
},
}
rfs := NewRebuiltForrest(nil, Graph{}, cbs)
tree, err := rfs.RebuiltTree(ctx, 306)
assert.EqualError(t, err, `loop detected: [306 305 304 303 305]`)
assert.Nil(t, tree)
assert.NotNil(t, rfs.trees[305])
tree, err = rfs.RebuiltTree(ctx, 305)
assert.EqualError(t, err, `loop detected: [305 304 303 305]`)
assert.Nil(t, tree)
assert.NotNil(t, rfs.trees[304])
tree, err = rfs.RebuiltTree(ctx, 304)
assert.EqualError(t, err, `loop detected: [304 303 305 304]`)
assert.Nil(t, tree)
assert.NotNil(t, rfs.trees[303])
tree, err = rfs.RebuiltTree(ctx, 303)
assert.EqualError(t, err, `loop detected: [303 305 304 303]`)
assert.Nil(t, tree)
}
func TestRebuiltTreeParentErr(t *testing.T) {
t.Parallel()
ctx := dlog.NewTestContext(t, true)
type mockRoot struct {
ID btrfsprim.ObjID
UUID btrfsprim.UUID
ParentUUID btrfsprim.UUID
ParentGen btrfsprim.Generation
}
roots := []mockRoot{
{
ID: 305,
UUID: btrfsprim.MustParseUUID("00000000-0000-0000-0000-000000000005"),
ParentUUID: btrfsprim.MustParseUUID("00000000-0000-0000-0000-000000000004"),
ParentGen: 1004,
},
{
ID: 304,
UUID: btrfsprim.MustParseUUID("00000000-0000-0000-0000-000000000004"),
},
}
cbs := rebuiltForrestCallbacks{
addedItem: func(ctx context.Context, tree btrfsprim.ObjID, key btrfsprim.Key) {
// do nothing
},
addedRoot: func(ctx context.Context, tree btrfsprim.ObjID, root btrfsvol.LogicalAddr) {
// do nothing
},
lookupRoot: func(ctx context.Context, tree btrfsprim.ObjID) (offset btrfsprim.Generation, item btrfsitem.Root, ok bool) {
if tree == 304 {
// Force a fault.
return 0, btrfsitem.Root{}, false
}
for _, root := range roots {
if root.ID == tree {
return root.ParentGen, btrfsitem.Root{
Generation: 2000,
UUID: root.UUID,
ParentUUID: root.ParentUUID,
}, true
}
}
return 0, btrfsitem.Root{}, false
},
lookupUUID: func(ctx context.Context, uuid btrfsprim.UUID) (id btrfsprim.ObjID, ok bool) {
for _, root := range roots {
if root.UUID == uuid {
return root.ID, true
}
}
return 0, false
},
}
rfs := NewRebuiltForrest(nil, Graph{}, cbs)
tree, err := rfs.RebuiltTree(ctx, 305)
assert.EqualError(t, err, `failed to rebuild parent tree: 304: tree does not exist`)
assert.Nil(t, tree)
}
|