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
|
package btrfs
import (
"bytes"
"fmt"
"reflect"
"lukeshu.com/btrfs-tools/pkg/binstruct"
)
type FS struct {
Devices []*Device
initErr error
uuid2dev map[UUID]*Device
chunks []SysChunk
}
func (fs *FS) Name() string {
sb, err := fs.Superblock()
if err != nil {
return fmt.Sprintf("fs_uuid=%s", "(unreadable)")
}
return fmt.Sprintf("fs_uuid=%s", sb.Data.FSUUID)
}
func (fs *FS) Size() (LogicalAddr, error) {
var ret LogicalAddr
for _, dev := range fs.Devices {
sz, err := dev.Size()
if err != nil {
return 0, fmt.Errorf("file %q: %w", dev.Name(), err)
}
ret += LogicalAddr(sz)
}
return ret, nil
}
func (fs *FS) Superblocks() ([]Ref[PhysicalAddr, Superblock], error) {
var ret []Ref[PhysicalAddr, Superblock]
for _, dev := range fs.Devices {
sbs, err := dev.Superblocks()
if err != nil {
return nil, fmt.Errorf("file %q: %w", dev.Name(), err)
}
ret = append(ret, sbs...)
}
return ret, nil
}
func (fs *FS) Superblock() (ret Ref[PhysicalAddr, Superblock], err error) {
sbs, err := fs.Superblocks()
if err != nil {
return ret, err
}
fname := ""
sbi := 0
for i, sb := range sbs {
if sb.File.Name() != fname {
fname = sb.File.Name()
sbi = 0
} else {
sbi++
}
if err := sb.Data.ValidateChecksum(); err != nil {
return ret, fmt.Errorf("file %q superblock %d: %w", sb.File.Name(), sbi, err)
}
if i > 0 {
// This is probably wrong, but lots of my
// multi-device code is probably wrong.
if !sb.Data.Equal(sbs[0].Data) {
return ret, fmt.Errorf("file %q superblock %d and file %q superblock %d disagree",
sbs[0].File.Name(), 0,
sb.File.Name(), sbi)
}
}
}
return sbs[0], nil
}
func (fs *FS) init() error {
if fs.uuid2dev != nil {
return fs.initErr
}
fs.uuid2dev = make(map[UUID]*Device, len(fs.Devices))
for _, dev := range fs.Devices {
sbs, err := dev.Superblocks()
if err != nil {
fs.initErr = fmt.Errorf("file %q: %w", dev.Name(), err)
return fs.initErr
}
a := sbs[0].Data
a.Checksum = CSum{}
a.Self = 0
for i, sb := range sbs[1:] {
b := sb.Data
b.Checksum = CSum{}
b.Self = 0
if !reflect.DeepEqual(a, b) {
fs.initErr = fmt.Errorf("file %q: superblock %d disagrees with superblock 0",
dev.Name(), i+1)
return fs.initErr
}
}
sb := sbs[0]
if other, exists := fs.uuid2dev[sb.Data.DevItem.DevUUID]; exists {
fs.initErr = fmt.Errorf("file %q and file %q have the same device ID: %v",
other.Name(), dev.Name(), sb.Data.DevItem.DevUUID)
return fs.initErr
}
fs.uuid2dev[sb.Data.DevItem.DevUUID] = dev
syschunks, err := sb.Data.ParseSysChunkArray()
if err != nil {
fs.initErr = fmt.Errorf("file %q: %w", dev.Name(), err)
return fs.initErr
}
for _, chunk := range syschunks {
fs.chunks = append(fs.chunks, chunk)
}
}
return nil
}
func (fs *FS) ReadAt(dat []byte, laddr LogicalAddr) (int, error) {
done := 0
for done < len(dat) {
n, err := fs.maybeShortReadAt(dat[done:], laddr+LogicalAddr(done))
done += n
if err != nil {
return done, err
}
}
return done, nil
}
func (fs *FS) maybeShortReadAt(dat []byte, laddr LogicalAddr) (int, error) {
if err := fs.init(); err != nil {
return 0, err
}
type physicalAddr struct {
Dev UUID
Addr PhysicalAddr
}
paddrs := make(map[physicalAddr]struct{})
for _, chunk := range fs.chunks {
if chunk.Offset <= uint64(laddr) && uint64(laddr) < chunk.Offset+uint64(chunk.Chunk.Size) {
offsetWithinChunk := uint64(laddr) - chunk.Offset
if offsetWithinChunk+uint64(len(dat)) > chunk.Chunk.Size {
dat = dat[:chunk.Chunk.Size-offsetWithinChunk]
}
for _, stripe := range chunk.Chunk.Stripes {
paddrs[physicalAddr{
Dev: stripe.DeviceUUID,
Addr: PhysicalAddr(stripe.Offset + offsetWithinChunk),
}] = struct{}{}
}
}
}
if len(paddrs) == 0 {
return 0, fmt.Errorf("could not map logical address %v", laddr)
}
buf := make([]byte, len(dat))
first := true
for paddr := range paddrs {
dev, ok := fs.uuid2dev[paddr.Dev]
if !ok {
return 0, fmt.Errorf("device=%s does not exist", paddr.Dev)
}
if _, err := dev.ReadAt(buf, paddr.Addr); err != nil {
return 0, fmt.Errorf("read device=%s paddr=%v: %w", paddr.Dev, paddr.Addr, err)
}
if first {
copy(dat, buf)
} else {
if !bytes.Equal(dat, buf) {
return 0, fmt.Errorf("inconsistent stripes at laddr=%v len=%d", laddr, len(dat))
}
}
}
return len(dat), nil
}
func (fs *FS) ReadNode(addr LogicalAddr) (Node, error) {
sb, err := fs.Superblock()
if err != nil {
return nil, err
}
nodeBuf := make([]byte, sb.Data.NodeSize)
if _, err := fs.ReadAt(nodeBuf, addr); err != nil {
return nil, err
}
var nodeHeader NodeHeader
if err := binstruct.Unmarshal(nodeBuf, &nodeHeader); err != nil {
return nil, fmt.Errorf("node@%d: %w", addr, err)
}
if !nodeHeader.MetadataUUID.Equal(sb.Data.EffectiveMetadataUUID()) {
return nil, fmt.Errorf("node@%d: does not look like a node", addr)
}
stored := nodeHeader.Checksum
calced := CRC32c(nodeBuf[0x20:])
if !calced.Equal(stored) {
return nil, fmt.Errorf("node@%d: checksum mismatch: stored=%s calculated=%s",
addr, stored, calced)
}
nodeHeader.Size = sb.Data.NodeSize
if nodeHeader.Level > 0 {
// internal node
nodeHeader.MaxItems = (sb.Data.NodeSize - 0x65) / 0x21
ret := &InternalNode{
Header: Ref[LogicalAddr, NodeHeader]{
File: fs,
Addr: addr,
Data: nodeHeader,
},
Body: nil,
}
for i := uint32(0); i < nodeHeader.NumItems; i++ {
itemOff := 0x65 + (0x21 * int(i))
var item KeyPointer
if err := binstruct.Unmarshal(nodeBuf[itemOff:], &item); err != nil {
return nil, fmt.Errorf("node@%d (internal): item %d: %w", addr, i, err)
}
ret.Body = append(ret.Body, Ref[LogicalAddr, KeyPointer]{
File: fs,
Addr: addr + LogicalAddr(itemOff),
Data: item,
})
}
return ret, nil
} else {
// leaf node
nodeHeader.MaxItems = (sb.Data.NodeSize - 0x65) / 0x19
ret := &LeafNode{
Header: Ref[LogicalAddr, NodeHeader]{
File: fs,
Addr: addr,
Data: nodeHeader,
},
Body: nil,
}
for i := uint32(0); i < nodeHeader.NumItems; i++ {
itemOff := 0x65 + (0x19 * int(i))
var item Item
if err := binstruct.Unmarshal(nodeBuf[itemOff:], &item); err != nil {
return nil, fmt.Errorf("node@%d (leaf): item %d: %w", addr, i, err)
}
dataOff := 0x65 + int(item.DataOffset)
dataSize := int(item.DataSize)
item.Data = Ref[LogicalAddr, []byte]{
File: fs,
Addr: addr + LogicalAddr(dataOff),
Data: nodeBuf[dataOff : dataOff+dataSize],
}
ret.Body = append(ret.Body, Ref[LogicalAddr, Item]{
File: fs,
Addr: addr + LogicalAddr(itemOff),
Data: item,
})
}
return ret, nil
}
}
|