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
|
// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package rebuildmappings
import (
"encoding/json"
"fmt"
"os"
"sort"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect"
"git.lukeshu.com/btrfs-progs-ng/lib/maps"
)
type BlockGroup struct {
LAddr btrfsvol.LogicalAddr
Size btrfsvol.AddrDelta
Flags btrfsvol.BlockGroupFlags
}
func ReadNodeScanResults(fs *btrfs.FS, filename string) (map[btrfsvol.LogicalAddr]BlockGroup, error) {
scanResultsBytes, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var scanResults btrfsinspect.ScanDevicesResult
if err := json.Unmarshal(scanResultsBytes, &scanResults); err != nil {
return nil, err
}
bgTree, err := ReduceScanResults(fs, scanResults)
if err != nil {
return nil, err
}
return bgTree, nil
}
func ReduceScanResults(fs *btrfs.FS, scanResults btrfsinspect.ScanDevicesResult) (map[btrfsvol.LogicalAddr]BlockGroup, error) {
// Reduce
bgSet := make(map[BlockGroup]struct{})
for _, found := range scanResults {
for _, bg := range found.FoundBlockGroups {
bgSet[BlockGroup{
LAddr: btrfsvol.LogicalAddr(bg.Key.ObjectID),
Size: btrfsvol.AddrDelta(bg.Key.Offset),
Flags: bg.BG.Flags,
}] = struct{}{}
}
}
// Sanity check
bgList := maps.Keys(bgSet)
sort.Slice(bgList, func(i, j int) bool {
return bgList[i].LAddr < bgList[j].LAddr
})
var pos btrfsvol.LogicalAddr
for _, bg := range bgList {
if bg.LAddr < pos || bg.Size < 0 {
return nil, fmt.Errorf("found block groups are inconsistent")
}
pos = bg.LAddr.Add(bg.Size)
}
// Return
bgMap := make(map[btrfsvol.LogicalAddr]BlockGroup, len(bgSet))
for bg := range bgSet {
bgMap[bg.LAddr] = bg
}
return bgMap, nil
}
|