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
|
// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package rebuildtrees
import (
"context"
"fmt"
"github.com/datawire/dlib/dlog"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsprim"
"git.lukeshu.com/btrfs-progs-ng/lib/containers"
)
type wantOffsetType int8
const (
offsetAny = wantOffsetType(iota)
offsetExact
offsetRange
offsetName
)
type Want struct {
ObjectID btrfsprim.ObjID
ItemType btrfsprim.ItemType
OffsetType wantOffsetType
OffsetLow uint64
OffsetHigh uint64
OffsetName string
}
func (a Want) Compare(b Want) int {
if d := containers.NativeCompare(a.ObjectID, b.ObjectID); d != 0 {
return d
}
if d := containers.NativeCompare(a.ItemType, b.ItemType); d != 0 {
return d
}
if d := containers.NativeCompare(a.OffsetType, b.OffsetType); d != 0 {
return d
}
if d := containers.NativeCompare(a.OffsetLow, b.OffsetLow); d != 0 {
return d
}
if d := containers.NativeCompare(a.OffsetHigh, b.OffsetHigh); d != 0 {
return d
}
if d := containers.NativeCompare(a.OffsetName, b.OffsetName); d != 0 {
return d
}
return 0
}
func (o Want) Key() btrfsprim.Key {
return btrfsprim.Key{
ObjectID: o.ObjectID,
ItemType: o.ItemType,
Offset: o.OffsetLow,
}
}
func wantFromKey(k btrfsprim.Key) Want {
return Want{
ObjectID: k.ObjectID,
ItemType: k.ItemType,
OffsetType: offsetExact,
OffsetLow: k.Offset,
}
}
func (o Want) String() string {
switch o.OffsetType {
case offsetAny:
return fmt.Sprintf("{%v %v ?}", o.ObjectID, o.ItemType)
case offsetExact:
return fmt.Sprintf("{%v %v %v}", o.ObjectID, o.ItemType, o.OffsetLow)
case offsetRange:
return fmt.Sprintf("{%v %v %v-%v}", o.ObjectID, o.ItemType, o.OffsetLow, o.OffsetHigh)
case offsetName:
return fmt.Sprintf("{%v %v name=%q}", o.ObjectID, o.ItemType, o.OffsetName)
default:
panic(fmt.Errorf("should not happen: OffsetType=%#v", o.OffsetType))
}
}
type WantWithTree struct {
TreeID btrfsprim.ObjID
Key Want
}
func (o WantWithTree) String() string {
return fmt.Sprintf("tree=%v key=%v", o.TreeID, o.Key)
}
const (
logFieldItemWant = "btrfsinspect.rebuild-nodes.rebuild.want"
logFieldTreeWant = "btrfsinspect.rebuild-nodes.rebuild.add-tree.want"
)
func withWant(ctx context.Context, logField, reason string, wantKey WantWithTree) context.Context {
ctx = dlog.WithField(ctx, logField+".reason", reason)
ctx = dlog.WithField(ctx, logField+".key", wantKey)
return ctx
}
|