-
Notifications
You must be signed in to change notification settings - Fork 11
/
btrfs_tree.go
299 lines (275 loc) · 7.47 KB
/
btrfs_tree.go
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package btrfs
import (
"fmt"
"time"
"unsafe"
)
const (
_BTRFS_BLOCK_GROUP_TYPE_MASK = (blockGroupData |
blockGroupSystem |
blockGroupMetadata)
_BTRFS_BLOCK_GROUP_PROFILE_MASK = (blockGroupRaid0 |
blockGroupRaid1 |
blockGroupRaid5 |
blockGroupRaid6 |
blockGroupDup |
blockGroupRaid10)
_BTRFS_BLOCK_GROUP_MASK = _BTRFS_BLOCK_GROUP_TYPE_MASK | _BTRFS_BLOCK_GROUP_PROFILE_MASK
)
type rootRef struct {
DirID objectID
Sequence uint64
Name string
}
func (rootRef) btrfsSize() int { return 18 }
func asUint64(p []byte) uint64 {
return *(*uint64)(unsafe.Pointer(&p[0]))
}
func asUint32(p []byte) uint32 {
return *(*uint32)(unsafe.Pointer(&p[0]))
}
func asUint16(p []byte) uint16 {
return *(*uint16)(unsafe.Pointer(&p[0]))
}
func asRootRef(p []byte) rootRef {
const sz = 18
// assuming that it is highly unsafe to have sizeof(struct) > len(data)
// (*btrfs_root_ref)(unsafe.Pointer(&p[0])) and sizeof(btrfs_root_ref) == 24
ref := rootRef{
DirID: objectID(asUint64(p[0:])),
Sequence: asUint64(p[8:]),
}
if n := asUint16(p[16:]); n > 0 {
ref.Name = string(p[sz : sz+n : sz+n])
}
return ref
}
var treeKeyNames = map[treeKeyType]string{
inodeItemKey: "inodeItem",
inodeRefKey: "inodeRef",
inodeExtrefKey: "inodeExtref",
xattrItemKey: "xattrItemKey",
orphanItemKey: "orphanItem",
dirLogItemKey: "dirLogItem",
dirLogIndexKey: "dirLogIndex",
dirItemKey: "dirItem",
dirIndexKey: "dirIndex",
extentDataKey: "extentData",
extentCsumKey: "extentCsum",
rootItemKey: "rootItem",
rootBackrefKey: "rootBackref",
rootRefKey: "rootRef",
extentItemKey: "extentItem",
metadataItemKey: "metadataItem",
treeBlockRefKey: "treeBlockRef",
extentDataRefKey: "extentDataRef",
extentRefV0Key: "extentRefV0",
sharedBlockRefKey: "sharedBlockRef",
sharedDataRefKey: "sharedDataRef",
blockGroupItemKey: "blockGroupItem",
freeSpaceInfoKey: "freeSpaceInfo",
freeSpaceExtentKey: "freeSpaceExtent",
freeSpaceBitmapKey: "freeSpaceBitmap",
devExtentKey: "devExtent",
devItemKey: "devItem",
chunkItemKey: "chunkItem",
qgroupStatusKey: "qgroupStatus",
qgroupInfoKey: "qgroupInfo",
qgroupLimitKey: "qgroupLimit",
qgroupRelationKey: "qgroupRelation",
temporaryItemKey: "temporaryItem",
persistentItemKey: "persistentItem",
devReplaceKey: "devReplace",
uuidKeySubvol: "uuidKeySubvol",
uuidKeyReceivedSubvol: "uuidKeyReceivedSubvol",
stringItemKey: "stringItem",
}
func (t treeKeyType) String() string {
if name, ok := treeKeyNames[t]; ok {
return name
}
return fmt.Sprintf("%#x", int(t))
}
// btrfs_disk_key_raw is a raw bytes for btrfs_disk_key structure
type btrfs_disk_key_raw [17]byte
func (p btrfs_disk_key_raw) Decode() diskKey {
return diskKey{
ObjectID: asUint64(p[0:]),
Type: p[8],
Offset: asUint64(p[9:]),
}
}
type diskKey struct {
ObjectID uint64
Type byte
Offset uint64
}
// btrfs_timespec_raw is a raw bytes for btrfs_timespec structure.
type btrfs_timespec_raw [12]byte
func (t btrfs_timespec_raw) Decode() time.Time {
sec, nsec := asUint64(t[0:]), asUint32(t[8:])
return time.Unix(int64(sec), int64(nsec))
}
// timeBlock is a raw set of bytes for 4 time fields.
// It is used to keep correct alignment when accessing structures from btrfs.
type timeBlock [4]btrfs_timespec_raw
type btrfs_inode_item_raw struct {
generation uint64
transid uint64
size uint64
nbytes uint64
block_group uint64
nlink uint32
uid uint32
gid uint32
mode uint32
rdev uint64
flags uint64
sequence uint64
_ [4]uint64 // reserved
// atime btrfs_timespec
// ctime btrfs_timespec
// mtime btrfs_timespec
// otime btrfs_timespec
times timeBlock
}
func (v btrfs_inode_item_raw) Decode() inodeItem {
return inodeItem{
Gen: v.generation,
TransID: v.transid,
Size: v.size,
NBytes: v.nbytes,
BlockGroup: v.block_group,
NLink: v.nlink,
UID: v.uid,
GID: v.gid,
Mode: v.mode,
RDev: v.rdev,
Flags: v.flags,
Sequence: v.sequence,
ATime: v.times[0].Decode(),
CTime: v.times[1].Decode(),
MTime: v.times[2].Decode(),
OTime: v.times[3].Decode(),
}
}
type inodeItem struct {
Gen uint64 // nfs style generation number
TransID uint64 // transid that last touched this inode
Size uint64
NBytes uint64
BlockGroup uint64
NLink uint32
UID uint32
GID uint32
Mode uint32
RDev uint64
Flags uint64
Sequence uint64 // modification sequence number for NFS
ATime time.Time
CTime time.Time
MTime time.Time
OTime time.Time
}
func asRootItem(p []byte) *btrfs_root_item_raw {
return (*btrfs_root_item_raw)(unsafe.Pointer(&p[0]))
}
type btrfs_root_item_raw [439]byte
func (p btrfs_root_item_raw) Decode() rootItem {
const (
off2 = unsafe.Sizeof(btrfs_root_item_raw_p1{})
off3 = off2 + 23
)
p1 := (*btrfs_root_item_raw_p1)(unsafe.Pointer(&p[0]))
p2 := p[off2 : off2+23]
p2_k := (*btrfs_disk_key_raw)(unsafe.Pointer(&p[off2+4]))
p2_b := p2[4+17:]
p3 := (*btrfs_root_item_raw_p3)(unsafe.Pointer(&p[off3]))
return rootItem{
Inode: p1.inode.Decode(),
Gen: p1.generation,
RootDirID: p1.root_dirid,
ByteNr: p1.bytenr,
ByteLimit: p1.byte_limit,
BytesUsed: p1.bytes_used,
LastSnapshot: p1.last_snapshot,
Flags: p1.flags,
// from here, Go structure become misaligned with C structure
Refs: asUint32(p2[0:]),
DropProgress: p2_k.Decode(),
DropLevel: p2_b[0],
Level: p2_b[1],
// these fields are still misaligned by 1 bytes
// TODO(dennwc): it's a copy of Gen to check structure version; hide it maybe?
GenV2: p3.generation_v2,
UUID: p3.uuid,
ParentUUID: p3.parent_uuid,
ReceivedUUID: p3.received_uuid,
CTransID: p3.ctransid,
OTransID: p3.otransid,
STransID: p3.stransid,
RTransID: p3.rtransid,
CTime: p3.times[0].Decode(),
OTime: p3.times[1].Decode(),
STime: p3.times[2].Decode(),
RTime: p3.times[3].Decode(),
}
}
type rootItem struct {
Inode inodeItem
Gen uint64
RootDirID uint64
ByteNr uint64
ByteLimit uint64
BytesUsed uint64
LastSnapshot uint64
Flags uint64
Refs uint32
DropProgress diskKey
DropLevel uint8
Level uint8
GenV2 uint64
UUID UUID
ParentUUID UUID
ReceivedUUID UUID
CTransID uint64
OTransID uint64
STransID uint64
RTransID uint64
CTime time.Time
OTime time.Time
STime time.Time
RTime time.Time
}
type btrfs_root_item_raw_p1 struct {
inode btrfs_inode_item_raw
generation uint64
root_dirid uint64
bytenr uint64
byte_limit uint64
bytes_used uint64
last_snapshot uint64
flags uint64
}
type btrfs_root_item_raw_p2 struct {
refs uint32
drop_progress btrfs_disk_key_raw
drop_level uint8
level uint8
}
type btrfs_root_item_raw_p3 struct {
generation_v2 uint64
uuid UUID
parent_uuid UUID
received_uuid UUID
ctransid uint64
otransid uint64
stransid uint64
rtransid uint64
// ctime btrfs_timespec
// otime btrfs_timespec
// stime btrfs_timespec
// rtime btrfs_timespec
times timeBlock
_ [8]uint64 // reserved
}