summaryrefslogtreecommitdiff
path: root/cmd/btrfs-rec/inspect/rebuildmappings/scan.go
blob: 76d8a7599acfc4053a3b79d4bb3e76300da284e4 (plain)
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
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package rebuildmappings

import (
	"context"
	"fmt"
	"strings"

	"github.com/datawire/dlib/dlog"

	"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/btrfsprim"
	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfssum"
	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfstree"
	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
	"git.lukeshu.com/btrfs-progs-ng/lib/btrfsutil"
	"git.lukeshu.com/btrfs-progs-ng/lib/containers"
	"git.lukeshu.com/btrfs-progs-ng/lib/textui"
)

// Result types ////////////////////////////////////////////////////////////////

type ScanDevicesResult = map[btrfsvol.DeviceID]ScanOneDeviceResult

type ScanOneDeviceResult struct {
	Checksums        btrfssum.SumRun[btrfsvol.PhysicalAddr]
	FoundNodes       map[btrfsvol.LogicalAddr][]btrfsvol.PhysicalAddr
	FoundChunks      []FoundChunk
	FoundBlockGroups []FoundBlockGroup
	FoundDevExtents  []FoundDevExtent
	FoundExtentCSums []FoundExtentCSum
}

type FoundChunk = btrfstree.SysChunk

type FoundBlockGroup struct {
	Key btrfsprim.Key
	BG  btrfsitem.BlockGroup
}

type FoundDevExtent struct {
	Key    btrfsprim.Key
	DevExt btrfsitem.DevExtent
}

type FoundExtentCSum struct {
	Generation btrfsprim.Generation
	Sums       btrfsitem.ExtentCSum
}

// Compare implements containers.Ordered.
func (a FoundExtentCSum) Compare(b FoundExtentCSum) int {
	return containers.NativeCompare(a.Sums.Addr, b.Sums.Addr)
}

// Convenience functions for those types ///////////////////////////////////////

func ScanDevices(ctx context.Context, fs *btrfs.FS) (ScanDevicesResult, error) {
	return btrfsutil.ScanDevices[scanStats, ScanOneDeviceResult](ctx, fs, newDeviceScanner)
}

// ScanOneDevice mostly mimics btrfs-progs
// cmds/rescue-chunk-recover.c:scan_one_device().
func ScanOneDevice(ctx context.Context, dev *btrfs.Device) (ScanOneDeviceResult, error) {
	return btrfsutil.ScanOneDevice[scanStats, ScanOneDeviceResult](ctx, dev, newDeviceScanner)
}

// scanner implementation //////////////////////////////////////////////////////

type deviceScanner struct {
	alg    btrfssum.CSumType
	sums   strings.Builder
	result ScanOneDeviceResult
}

type scanStats struct {
	NumFoundNodes       int
	NumFoundChunks      int
	NumFoundBlockGroups int
	NumFoundDevExtents  int
	NumFoundExtentCSums int
}

func (s scanStats) String() string {
	return textui.Sprintf("found: %v nodes, %v chunks, %v block groups, %v dev extents, %v sum items",
		s.NumFoundNodes,
		s.NumFoundChunks,
		s.NumFoundBlockGroups,
		s.NumFoundDevExtents,
		s.NumFoundExtentCSums)
}

func (scanner *deviceScanner) ScanStats() scanStats {
	return scanStats{
		NumFoundNodes:       len(scanner.result.FoundNodes),
		NumFoundChunks:      len(scanner.result.FoundChunks),
		NumFoundBlockGroups: len(scanner.result.FoundBlockGroups),
		NumFoundDevExtents:  len(scanner.result.FoundDevExtents),
		NumFoundExtentCSums: len(scanner.result.FoundExtentCSums),
	}
}

func newDeviceScanner(_ context.Context, sb btrfstree.Superblock, _ btrfsvol.PhysicalAddr, numSectors int) btrfsutil.DeviceScanner[scanStats, ScanOneDeviceResult] {
	scanner := new(deviceScanner)
	scanner.alg = sb.ChecksumType
	scanner.result.FoundNodes = make(map[btrfsvol.LogicalAddr][]btrfsvol.PhysicalAddr)
	scanner.result.Checksums.ChecksumSize = scanner.alg.Size()
	scanner.sums.Grow(scanner.result.Checksums.ChecksumSize * numSectors)
	return scanner
}

func (scanner *deviceScanner) ScanSector(_ context.Context, dev *btrfs.Device, paddr btrfsvol.PhysicalAddr) error {
	sum, err := btrfs.ChecksumPhysical(dev, scanner.alg, paddr)
	if err != nil {
		return err
	}
	scanner.sums.Write(sum[:scanner.result.Checksums.ChecksumSize])
	return nil
}

func (scanner *deviceScanner) ScanNode(ctx context.Context, addr btrfsvol.PhysicalAddr, node *btrfstree.Node) error {
	scanner.result.FoundNodes[node.Head.Addr] = append(scanner.result.FoundNodes[node.Head.Addr], addr)
	for i, item := range node.BodyLeaf {
		switch item.Key.ItemType {
		case btrfsitem.CHUNK_ITEM_KEY:
			switch itemBody := item.Body.(type) {
			case *btrfsitem.Chunk:
				dlog.Tracef(ctx, "node@%v: item %v: found chunk",
					addr, i)
				scanner.result.FoundChunks = append(scanner.result.FoundChunks, FoundChunk{
					Key:   item.Key,
					Chunk: *itemBody,
				})
			case *btrfsitem.Error:
				dlog.Errorf(ctx, "node@%v: item %v: error: malformed CHUNK_ITEM: %v",
					addr, i, itemBody.Err)
			default:
				panic(fmt.Errorf("should not happen: CHUNK_ITEM has unexpected item type: %T", itemBody))
			}
		case btrfsitem.BLOCK_GROUP_ITEM_KEY:
			switch itemBody := item.Body.(type) {
			case *btrfsitem.BlockGroup:
				dlog.Tracef(ctx, "node@%v: item %v: found block group",
					addr, i)
				scanner.result.FoundBlockGroups = append(scanner.result.FoundBlockGroups, FoundBlockGroup{
					Key: item.Key,
					BG:  *itemBody,
				})
			case *btrfsitem.Error:
				dlog.Errorf(ctx, "node@%v: item %v: error: malformed BLOCK_GROUP_ITEM: %v",
					addr, i, itemBody.Err)
			default:
				panic(fmt.Errorf("should not happen: BLOCK_GROUP_ITEM has unexpected item type: %T", itemBody))
			}
		case btrfsitem.DEV_EXTENT_KEY:
			switch itemBody := item.Body.(type) {
			case *btrfsitem.DevExtent:
				dlog.Tracef(ctx, "node@%v: item %v: found dev extent",
					addr, i)
				scanner.result.FoundDevExtents = append(scanner.result.FoundDevExtents, FoundDevExtent{
					Key:    item.Key,
					DevExt: *itemBody,
				})
			case *btrfsitem.Error:
				dlog.Errorf(ctx, "node@%v: item %v: error: malformed DEV_EXTENT: %v",
					addr, i, itemBody.Err)
			default:
				panic(fmt.Errorf("should not happen: DEV_EXTENT has unexpected item type: %T", itemBody))
			}
		case btrfsitem.EXTENT_CSUM_KEY:
			switch itemBody := item.Body.(type) {
			case *btrfsitem.ExtentCSum:
				dlog.Tracef(ctx, "node@%v: item %v: found csums",
					addr, i)
				scanner.result.FoundExtentCSums = append(scanner.result.FoundExtentCSums, FoundExtentCSum{
					Generation: node.Head.Generation,
					Sums:       *itemBody,
				})
			case *btrfsitem.Error:
				dlog.Errorf(ctx, "node@%v: item %v: error: malformed is EXTENT_CSUM: %v",
					addr, i, itemBody.Err)
			default:
				panic(fmt.Errorf("should not happen: EXTENT_CSUM has unexpected item type: %T", itemBody))
			}
		}
	}
	return nil
}

func (scanner *deviceScanner) ScanDone(_ context.Context) (ScanOneDeviceResult, error) {
	scanner.result.Checksums.Sums = btrfssum.ShortSum(scanner.sums.String())
	return scanner.result, nil
}