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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package btrfstree
import (
"context"
"errors"
"fmt"
iofs "io/fs"
"math"
"github.com/datawire/dlib/derror"
"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/slices"
)
type RawTree struct {
Forrest TreeOperatorImpl
TreeRoot
}
func (tree *RawTree) TreeWalk(ctx context.Context, errHandle func(*TreeError), cbs TreeWalkHandler) {
path := Path{{
FromTree: tree.ID,
FromItemSlot: -1,
ToNodeAddr: tree.RootNode,
ToNodeGeneration: tree.Generation,
ToNodeLevel: tree.Level,
ToMaxKey: btrfsprim.MaxKey,
}}
tree.walk(ctx, path, errHandle, cbs)
}
func (tree *RawTree) walk(ctx context.Context, path Path, errHandle func(*TreeError), cbs TreeWalkHandler) {
if ctx.Err() != nil {
return
}
if path.Node(-1).ToNodeAddr == 0 {
return
}
if cbs.PreNode != nil {
if err := cbs.PreNode(path); err != nil {
if errors.Is(err, iofs.SkipDir) {
return
}
errHandle(&TreeError{Path: path, Err: err})
}
if ctx.Err() != nil {
return
}
}
node, err := tree.Forrest.ReadNode(path)
defer node.Free()
if ctx.Err() != nil {
return
}
if err != nil && node != nil && cbs.BadNode != nil {
// opportunity to fix the node
err = cbs.BadNode(path, node, err)
if errors.Is(err, iofs.SkipDir) {
return
}
}
if err != nil {
errHandle(&TreeError{Path: path, Err: err})
} else if cbs.Node != nil {
if err := cbs.Node(path, node); err != nil {
if errors.Is(err, iofs.SkipDir) {
return
}
errHandle(&TreeError{Path: path, Err: err})
}
}
if ctx.Err() != nil {
return
}
if node != nil {
for i, item := range node.BodyInterior {
toMaxKey := path.Node(-1).ToMaxKey
if i+1 < len(node.BodyInterior) {
toMaxKey = node.BodyInterior[i+1].Key.Mm()
}
itemPath := append(path, PathElem{
FromTree: node.Head.Owner,
FromItemSlot: i,
ToNodeAddr: item.BlockPtr,
ToNodeGeneration: item.Generation,
ToNodeLevel: node.Head.Level - 1,
ToKey: item.Key,
ToMaxKey: toMaxKey,
})
if cbs.PreKeyPointer != nil {
if err := cbs.PreKeyPointer(itemPath, item); err != nil {
if errors.Is(err, iofs.SkipDir) {
continue
}
errHandle(&TreeError{Path: itemPath, Err: err})
}
if ctx.Err() != nil {
return
}
}
tree.walk(ctx, itemPath, errHandle, cbs)
if cbs.PostKeyPointer != nil {
if err := cbs.PostKeyPointer(itemPath, item); err != nil {
errHandle(&TreeError{Path: itemPath, Err: err})
}
if ctx.Err() != nil {
return
}
}
}
for i, item := range node.BodyLeaf {
itemPath := append(path, PathElem{
FromTree: node.Head.Owner,
FromItemSlot: i,
ToKey: item.Key,
ToMaxKey: item.Key,
})
if errBody, isErr := item.Body.(*btrfsitem.Error); isErr {
if cbs.BadItem == nil {
errHandle(&TreeError{Path: itemPath, Err: errBody.Err})
} else {
if err := cbs.BadItem(itemPath, item); err != nil {
errHandle(&TreeError{Path: itemPath, Err: err})
}
if ctx.Err() != nil {
return
}
}
} else {
if cbs.Item != nil {
if err := cbs.Item(itemPath, item); err != nil {
errHandle(&TreeError{Path: itemPath, Err: err})
}
if ctx.Err() != nil {
return
}
}
}
}
}
if cbs.PostNode != nil {
if err := cbs.PostNode(path, node); err != nil {
if errors.Is(err, iofs.SkipDir) {
return
}
errHandle(&TreeError{Path: path, Err: err})
}
}
}
func (tree *RawTree) search(_ context.Context, fn func(btrfsprim.Key, uint32) int) (Path, *Node, error) {
path := Path{{
FromTree: tree.ID,
FromItemSlot: -1,
ToNodeAddr: tree.RootNode,
ToNodeGeneration: tree.Generation,
ToNodeLevel: tree.Level,
ToMaxKey: btrfsprim.MaxKey,
}}
for {
if path.Node(-1).ToNodeAddr == 0 {
return nil, nil, ErrNoItem
}
node, err := tree.Forrest.ReadNode(path)
if err != nil {
node.Free()
return nil, nil, err
}
switch {
case node.Head.Level > 0:
// interior node
// Search for the right-most node.BodyInterior item for which
// `fn(item.Key) >= 0`.
//
// + + + + 0 - - - -
//
// There may or may not be a value that returns '0'.
//
// i.e. find the highest value that isn't too high.
lastGood, ok := slices.SearchHighest(node.BodyInterior, func(kp KeyPointer) int {
return slices.Min(fn(kp.Key, math.MaxUint32), 0) // don't return >0; a key can't be "too low"
})
if !ok {
node.Free()
return nil, nil, ErrNoItem
}
toMaxKey := path.Node(-1).ToMaxKey
if lastGood+1 < len(node.BodyInterior) {
toMaxKey = node.BodyInterior[lastGood+1].Key.Mm()
}
path = append(path, PathElem{
FromTree: node.Head.Owner,
FromItemSlot: lastGood,
ToNodeAddr: node.BodyInterior[lastGood].BlockPtr,
ToNodeGeneration: node.BodyInterior[lastGood].Generation,
ToNodeLevel: node.Head.Level - 1,
ToKey: node.BodyInterior[lastGood].Key,
ToMaxKey: toMaxKey,
})
node.Free()
default:
// leaf node
// Search for a member of node.BodyLeaf for which
// `fn(item.Head.Key) == 0`.
//
// + + + + 0 - - - -
//
// Such an item might not exist; in this case, return (nil, ErrNoItem).
// Multiple such items might exist; in this case, it does not matter which
// is returned.
//
// Implement this search as a binary search.
slot, ok := slices.Search(node.BodyLeaf, func(item Item) int {
return fn(item.Key, item.BodySize)
})
if !ok {
node.Free()
return nil, nil, ErrNoItem
}
path = append(path, PathElem{
FromTree: node.Head.Owner,
FromItemSlot: slot,
ToKey: node.BodyLeaf[slot].Key,
ToMaxKey: node.BodyLeaf[slot].Key,
})
return path, node, nil
}
}
}
func (fs TreeOperatorImpl) prev(path Path, node *Node) (Path, *Node, error) {
var err error
path = path.DeepCopy()
// go up
for path.Node(-1).FromItemSlot < 1 {
path = path.Parent()
if len(path) == 0 {
return nil, nil, nil
}
}
// go left
path.Node(-1).FromItemSlot--
if path.Node(-1).ToNodeAddr != 0 {
if node.Head.Addr != path.Node(-2).ToNodeAddr {
node.Free()
node, err = fs.ReadNode(path.Parent())
if err != nil {
node.Free()
return nil, nil, err
}
path.Node(-1).ToNodeAddr = node.BodyInterior[path.Node(-1).FromItemSlot].BlockPtr
}
}
// go down
for path.Node(-1).ToNodeAddr != 0 {
if node.Head.Addr != path.Node(-1).ToNodeAddr {
node.Free()
node, err = fs.ReadNode(path)
if err != nil {
node.Free()
return nil, nil, err
}
}
if node.Head.Level > 0 {
path = append(path, PathElem{
FromTree: node.Head.Owner,
FromItemSlot: len(node.BodyInterior) - 1,
ToNodeAddr: node.BodyInterior[len(node.BodyInterior)-1].BlockPtr,
ToNodeGeneration: node.BodyInterior[len(node.BodyInterior)-1].Generation,
ToNodeLevel: node.Head.Level - 1,
ToKey: node.BodyInterior[len(node.BodyInterior)-1].Key,
ToMaxKey: path.Node(-1).ToMaxKey,
})
} else {
path = append(path, PathElem{
FromTree: node.Head.Owner,
FromItemSlot: len(node.BodyLeaf) - 1,
ToKey: node.BodyLeaf[len(node.BodyLeaf)-1].Key,
ToMaxKey: node.BodyLeaf[len(node.BodyLeaf)-1].Key,
})
}
}
// return
if node.Head.Addr != path.Node(-2).ToNodeAddr {
node.Free()
node, err = fs.ReadNode(path.Parent())
if err != nil {
node.Free()
return nil, nil, err
}
}
return path, node, nil
}
func (fs TreeOperatorImpl) next(path Path, node *Node) (Path, *Node, error) {
var err error
path = path.DeepCopy()
// go up
if node.Head.Addr != path.Node(-2).ToNodeAddr {
node.Free()
node, err = fs.ReadNode(path.Parent())
if err != nil {
node.Free()
return nil, nil, err
}
path.Node(-2).ToNodeLevel = node.Head.Level
}
for path.Node(-1).FromItemSlot+1 >= int(node.Head.NumItems) {
path = path.Parent()
if len(path) == 1 {
return nil, nil, nil
}
if node.Head.Addr != path.Node(-2).ToNodeAddr {
node.Free()
node, err = fs.ReadNode(path.Parent())
if err != nil {
node.Free()
return nil, nil, err
}
path.Node(-2).ToNodeLevel = node.Head.Level
}
}
// go right
path.Node(-1).FromItemSlot++
if path.Node(-1).ToNodeAddr != 0 {
if node.Head.Addr != path.Node(-2).ToNodeAddr {
node.Free()
node, err = fs.ReadNode(path.Parent())
if err != nil {
node.Free()
return nil, nil, err
}
path.Node(-1).ToNodeAddr = node.BodyInterior[path.Node(-1).FromItemSlot].BlockPtr
}
}
// go down
for path.Node(-1).ToNodeAddr != 0 {
if node.Head.Addr != path.Node(-1).ToNodeAddr {
node.Free()
node, err = fs.ReadNode(path)
if err != nil {
node.Free()
return nil, nil, err
}
path.Node(-1).ToNodeLevel = node.Head.Level
}
if node.Head.Level > 0 {
toMaxKey := path.Node(-1).ToMaxKey
if len(node.BodyInterior) > 1 {
toMaxKey = node.BodyInterior[1].Key.Mm()
}
path = append(path, PathElem{
FromTree: node.Head.Owner,
FromItemSlot: 0,
ToNodeAddr: node.BodyInterior[0].BlockPtr,
ToNodeGeneration: node.BodyInterior[0].Generation,
ToNodeLevel: node.Head.Level - 1,
ToKey: node.BodyInterior[0].Key,
ToMaxKey: toMaxKey,
})
} else {
path = append(path, PathElem{
FromTree: node.Head.Owner,
FromItemSlot: 0,
ToKey: node.BodyInterior[0].Key,
ToMaxKey: node.BodyInterior[0].Key,
})
}
}
// return
if node.Head.Addr != path.Node(-2).ToNodeAddr {
node.Free()
node, err = fs.ReadNode(path.Parent())
if err != nil {
node.Free()
return nil, nil, err
}
}
return path, node, nil
}
func (tree *RawTree) TreeSearch(ctx context.Context, searcher TreeSearcher) (Item, error) {
path, node, err := tree.search(ctx, searcher.Search)
if err != nil {
return Item{}, fmt.Errorf("item with %s: %w", searcher, err)
}
item := node.BodyLeaf[path.Node(-1).FromItemSlot]
item.Body = item.Body.CloneItem()
node.Free()
return item, nil
}
func (tree *RawTree) TreeLookup(ctx context.Context, key btrfsprim.Key) (Item, error) {
return tree.TreeSearch(ctx, SearchExactKey(key))
}
func (tree *RawTree) TreeSearchAll(ctx context.Context, searcher TreeSearcher) ([]Item, error) {
middlePath, middleNode, err := tree.search(ctx, searcher.Search)
if err != nil {
return nil, fmt.Errorf("items with %s: %w", searcher, err)
}
middleItem := middleNode.BodyLeaf[middlePath.Node(-1).FromItemSlot]
ret := []Item{middleItem}
var errs derror.MultiError
prevPath, prevNode := middlePath, middleNode
for {
prevPath, prevNode, err = tree.Forrest.prev(prevPath, prevNode)
if err != nil {
errs = append(errs, err)
break
}
if len(prevPath) == 0 {
break
}
prevItem := prevNode.BodyLeaf[prevPath.Node(-1).FromItemSlot]
if searcher.Search(prevItem.Key, prevItem.BodySize) != 0 {
break
}
item := prevItem
item.Body = item.Body.CloneItem()
ret = append(ret, item)
}
slices.Reverse(ret)
if prevNode.Head.Addr != middlePath.Node(-1).ToNodeAddr {
prevNode.Free()
middleNode, err = tree.Forrest.ReadNode(middlePath)
if err != nil {
middleNode.Free()
return nil, fmt.Errorf("items with %s: %w", searcher, err)
}
}
nextPath, nextNode := middlePath, middleNode
for {
nextPath, nextNode, err = tree.Forrest.next(nextPath, nextNode)
if err != nil {
errs = append(errs, err)
break
}
if len(nextPath) == 0 {
break
}
nextItem := nextNode.BodyLeaf[nextPath.Node(-1).FromItemSlot]
if searcher.Search(nextItem.Key, nextItem.BodySize) != 0 {
break
}
item := nextItem
item.Body = item.Body.CloneItem()
ret = append(ret, item)
}
nextNode.Free()
if errs != nil {
err = errs
}
return ret, err
}
|