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
|
// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package btrfsinspect
import (
"context"
"errors"
"fmt"
"sync"
"github.com/datawire/dlib/dgroup"
"github.com/datawire/dlib/dlog"
"git.lukeshu.com/btrfs-progs-ng/lib/binstruct"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsutil"
)
type ScanDevicesResult map[btrfsvol.DeviceID]ScanOneDeviceResult
func ScanDevices(ctx context.Context, fs *btrfs.FS, sb btrfs.Superblock) (ScanDevicesResult, error) {
grp := dgroup.NewGroup(ctx, dgroup.GroupConfig{})
var mu sync.Mutex
result := make(map[btrfsvol.DeviceID]ScanOneDeviceResult)
for id, dev := range fs.LV.PhysicalVolumes() {
id := id
dev := dev
grp.Go(dev.Name(), func(ctx context.Context) error {
devResult, err := ScanOneDevice(ctx, dev, sb)
if err != nil {
return err
}
mu.Lock()
result[id] = devResult
mu.Unlock()
return nil
})
}
if err := grp.Wait(); err != nil {
return nil, err
}
return result, nil
}
type ScanOneDeviceResult struct {
Checksums SumRun[btrfsvol.PhysicalAddr]
FoundNodes map[btrfsvol.LogicalAddr][]btrfsvol.PhysicalAddr
FoundChunks []btrfs.SysChunk
FoundBlockGroups []SysBlockGroup
FoundDevExtents []SysDevExtent
FoundExtentCSums []SysExtentCSum
}
type SysBlockGroup struct {
Key btrfs.Key
BG btrfsitem.BlockGroup
}
type SysDevExtent struct {
Key btrfs.Key
DevExt btrfsitem.DevExtent
}
type SysExtentCSum struct {
Key btrfs.Key
Generation btrfs.Generation
Sums btrfsitem.ExtentCSum
}
// ScanOneDevice mostly mimics btrfs-progs
// cmds/rescue-chunk-recover.c:scan_one_device().
func ScanOneDevice(ctx context.Context, dev *btrfs.Device, sb btrfs.Superblock) (ScanOneDeviceResult, error) {
result := ScanOneDeviceResult{
FoundNodes: make(map[btrfsvol.LogicalAddr][]btrfsvol.PhysicalAddr),
}
sbSize := btrfsvol.PhysicalAddr(binstruct.StaticSize(btrfs.Superblock{}))
devSize := dev.Size()
if sb.NodeSize < sb.SectorSize {
return result, fmt.Errorf("node_size(%v) < sector_size(%v)",
sb.NodeSize, sb.SectorSize)
}
if sb.SectorSize != btrfsitem.CSumBlockSize {
// TODO: probably handle this?
return result, fmt.Errorf("sector_size(%v) != btrfsitem.CSumBlockSize",
sb.SectorSize)
}
alg := sb.ChecksumType
csumSize := alg.Size()
numSums := int(devSize / btrfsitem.CSumBlockSize)
sums := make([]byte, numSums*csumSize)
lastProgress := -1
progress := func(pos btrfsvol.PhysicalAddr) {
pct := int(100 * float64(pos) / float64(devSize))
if pct != lastProgress || pos == devSize {
dlog.Infof(ctx, "... dev[%q] scanned %v%% (found: %v nodes, %v chunks, %v block groups, %v dev extents, %v sum items)",
dev.Name(), pct,
len(result.FoundNodes),
len(result.FoundChunks),
len(result.FoundBlockGroups),
len(result.FoundDevExtents),
len(result.FoundExtentCSums))
lastProgress = pct
}
}
var minNextNode btrfsvol.PhysicalAddr
for i := 0; i < numSums; i++ {
if ctx.Err() != nil {
return result, ctx.Err()
}
pos := btrfsvol.PhysicalAddr(i * btrfsitem.CSumBlockSize)
progress(pos)
sum, err := btrfsutil.ChecksumPhysical(dev, alg, pos)
if err != nil {
return result, err
}
copy(sums[i*csumSize:], sum[:csumSize])
checkForNode := pos >= minNextNode
if checkForNode {
for _, sbAddr := range btrfs.SuperblockAddrs {
if sbAddr <= pos && pos < sbAddr+sbSize {
checkForNode = false
break
}
}
}
if checkForNode {
nodeRef, err := btrfs.ReadNode[btrfsvol.PhysicalAddr](dev, sb, pos, nil)
if err != nil {
if !errors.Is(err, btrfs.ErrNotANode) {
dlog.Infof(ctx, "... dev[%q] error: %v", dev.Name(), err)
}
} else {
result.FoundNodes[nodeRef.Data.Head.Addr] = append(result.FoundNodes[nodeRef.Data.Head.Addr], nodeRef.Addr)
for i, item := range nodeRef.Data.BodyLeaf {
switch item.Key.ItemType {
case btrfsitem.CHUNK_ITEM_KEY:
chunk, ok := item.Body.(btrfsitem.Chunk)
if !ok {
dlog.Errorf(ctx, "... dev[%q] node@%v: item %v: error: type is CHUNK_ITEM_KEY, but struct is %T",
dev.Name(), nodeRef.Addr, i, item.Body)
continue
}
//dlog.Tracef(ctx, "... dev[%q] node@%v: item %v: found chunk",
// dev.Name(), nodeRef.Addr, i)
result.FoundChunks = append(result.FoundChunks, btrfs.SysChunk{
Key: item.Key,
Chunk: chunk,
})
case btrfsitem.BLOCK_GROUP_ITEM_KEY:
bg, ok := item.Body.(btrfsitem.BlockGroup)
if !ok {
dlog.Errorf(ctx, "... dev[%q] node@%v: item %v: error: type is BLOCK_GROUP_ITEM_KEY, but struct is %T",
dev.Name(), nodeRef.Addr, i, item.Body)
continue
}
//dlog.Tracef(ctx, "... dev[%q] node@%v: item %v: found block group",
// dev.Name(), nodeRef.Addr, i)
result.FoundBlockGroups = append(result.FoundBlockGroups, SysBlockGroup{
Key: item.Key,
BG: bg,
})
case btrfsitem.DEV_EXTENT_KEY:
devext, ok := item.Body.(btrfsitem.DevExtent)
if !ok {
dlog.Errorf(ctx, "... dev[%q] node@%v: item %v: error: type is DEV_EXTENT_KEY, but struct is %T",
dev.Name(), nodeRef.Addr, i, item.Body)
continue
}
//dlog.Tracef(ctx, "... dev[%q] node@%v: item %v: found dev extent",
// dev.Name(), nodeRef.Addr, i)
result.FoundDevExtents = append(result.FoundDevExtents, SysDevExtent{
Key: item.Key,
DevExt: devext,
})
case btrfsitem.EXTENT_CSUM_KEY:
sums, ok := item.Body.(btrfsitem.ExtentCSum)
if !ok {
dlog.Errorf(ctx, "... dev[%q] node@%v: item %v: error: type is EXTENT_CSUM_OBJECTID, but struct is %T",
dev.Name(), nodeRef.Addr, i, item.Body)
continue
}
//dlog.Tracef(ctx, "... dev[%q] node@%v: item %v: found csums",
// dev.Name(), nodeRef.Addr, i)
result.FoundExtentCSums = append(result.FoundExtentCSums, SysExtentCSum{
Key: item.Key,
Generation: nodeRef.Data.Head.Generation,
Sums: sums,
})
}
}
minNextNode = pos + btrfsvol.PhysicalAddr(sb.NodeSize)
}
}
}
progress(devSize)
return result, nil
}
|