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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
package btrfsmisc
import (
"fmt"
"os"
"strings"
"lukeshu.com/btrfs-tools/pkg/btrfs"
"lukeshu.com/btrfs-tools/pkg/btrfs/btrfsitem"
"lukeshu.com/btrfs-tools/pkg/util"
)
// PrintTree mimics btrfs-progs
// kernel-shared/print-tree.c:btrfs_print_tree() and
// kernel-shared/print-tree.c:btrfs_print_leaf()
func PrintTree(fs *btrfs.FS, root btrfs.LogicalAddr) error {
nodeRef, err := fs.ReadNode(root)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
}
if nodeRef == nil {
return nil
}
node := nodeRef.Data
printHeaderInfo(node)
if node.Head.Level > 0 { // internal
for _, item := range node.BodyInternal {
fmt.Printf("\t%v block %v gen %v\n",
FmtKey(item.Key),
item.BlockPtr,
item.Generation)
}
for _, item := range node.BodyInternal {
if err := PrintTree(fs, item.BlockPtr); err != nil {
return err
}
}
} else { // leaf
for i, item := range node.BodyLeaf {
fmt.Printf("\titem %v %v itemoff %v itemsize %v\n",
i,
FmtKey(item.Head.Key),
item.Head.DataOffset,
item.Head.DataSize)
switch body := item.Body.(type) {
case btrfsitem.FreeSpaceHeader:
fmt.Printf("\t\tlocation %v\n", FmtKey(body.Location))
fmt.Printf("\t\tcache generation %v entries %v bitmaps %v\n",
body.Generation, body.NumEntries, body.NumBitmaps)
case btrfsitem.Inode:
fmt.Printf(""+
"\t\tgeneration %v transid %v size %v nbytes %v\n"+
"\t\tblock group %v mode %o links %v uid %v gid %v rdev %v\n"+
"\t\tsequence %v flags %v\n",
body.Generation, body.TransID, body.Size, body.NumBytes,
body.BlockGroup, body.Mode, body.NLink, body.UID, body.GID, body.RDev,
body.Sequence, body.Flags)
fmt.Printf("\t\tatime %v\n", fmtTime(body.ATime))
fmt.Printf("\t\tctime %v\n", fmtTime(body.CTime))
fmt.Printf("\t\tmtime %v\n", fmtTime(body.MTime))
fmt.Printf("\t\totime %v\n", fmtTime(body.OTime))
case btrfsitem.InodeRefList:
for _, ref := range body {
fmt.Printf("\t\tindex %v namelen %v name: %s\n",
ref.Index, ref.NameLen, ref.Name)
}
//case btrfsitem.INODE_EXTREF_KEY:
// // TODO
case btrfsitem.DirList:
for _, dir := range body {
fmt.Printf("\t\tlocation %v type %v\n",
FmtKey(dir.Location), dir.Type)
fmt.Printf("\t\ttransid %v data_len %v name_len %v\n",
dir.TransID, dir.DataLen, dir.NameLen)
fmt.Printf("\t\tname: %s\n", dir.Name)
if len(dir.Data) > 0 {
fmt.Printf("\t\tdata %v\n", dir.Data)
}
}
//case btrfsitem.DIR_LOG_INDEX_KEY, btrfsitem.DIR_LOG_ITEM_KEY:
// // TODO
case btrfsitem.Root:
fmt.Printf("\t\tgeneration %v root_dirid %v bytenr %d byte_limit %v bytes_used %v\n",
body.Generation, body.RootDirID, body.ByteNr, body.ByteLimit, body.BytesUsed)
fmt.Printf("\t\tlast_snapshot %v flags %v refs %v\n",
body.LastSnapshot, body.Flags, body.Refs)
fmt.Printf("\t\tdrop_progress %v drop_level %v\n",
FmtKey(body.DropProgress), body.DropLevel)
fmt.Printf("\t\tlevel %v generation_v2 %v\n",
body.Level, body.GenerationV2)
if body.Generation == body.GenerationV2 {
fmt.Printf("\t\tuuid %v\n", body.UUID)
fmt.Printf("\t\tparent_uuid %v\n", body.ParentUUID)
fmt.Printf("\t\treceived_uuid %v\n", body.ReceivedUUID)
fmt.Printf("\t\tctransid %v otransid %v stransid %v rtransid %v\n",
body.CTransID, body.OTransID, body.STransID, body.RTransID)
fmt.Printf("\t\tctime %v\n", fmtTime(body.CTime))
fmt.Printf("\t\totime %v\n", fmtTime(body.OTime))
fmt.Printf("\t\tstime %v\n", fmtTime(body.STime))
fmt.Printf("\t\trtime %v\n", fmtTime(body.RTime))
}
//case btrfsitem.ROOT_REF_KEY:
// // TODO
//case btrfsitem.ROOT_BACKREF_KEY:
// // TODO
case btrfsitem.Extent:
fmt.Printf("\t\trefs %v gen %v flags %v\n",
body.Head.Refs, body.Head.Generation, body.Head.Flags)
if body.Head.Flags.Has(btrfsitem.EXTENT_FLAG_TREE_BLOCK) {
fmt.Printf("\t\ttree block %v level %v\n",
FmtKey(body.Info.Key), body.Info.Level)
}
printExtentInlineRefs(body.Refs)
case btrfsitem.Metadata:
fmt.Printf("\t\trefs %v gen %v flags %v\n",
body.Head.Refs, body.Head.Generation, body.Head.Flags)
fmt.Printf("\t\ttree block skinny level %v\n", item.Head.Key.Offset)
printExtentInlineRefs(body.Refs)
//case btrfsitem.EXTENT_DATA_REF_KEY:
// // TODO
//case btrfsitem.SHARED_DATA_REF_KEY:
// // TODO
//case btrfsitem.EXTENT_CSUM_KEY:
// // TODO
case btrfsitem.FileExtent:
fmt.Printf("\t\tgeneration %v type %v\n",
body.Generation, body.Type)
switch body.Type {
case btrfsitem.FILE_EXTENT_INLINE:
fmt.Printf("\t\tinline extent data size %v ram_bytes %v compression %v\n",
len(body.BodyInline), body.RAMBytes, body.Compression)
case btrfsitem.FILE_EXTENT_PREALLOC:
fmt.Printf("\t\tprealloc data disk byte %v nr %v\n",
body.BodyPrealloc.DiskByteNr,
body.BodyPrealloc.DiskNumBytes)
fmt.Printf("\t\tprealloc data offset %v nr %v\n",
body.BodyPrealloc.Offset,
body.BodyPrealloc.NumBytes)
case btrfsitem.FILE_EXTENT_REG:
fmt.Printf("\t\textent data disk byte %v nr %v\n",
body.BodyReg.DiskByteNr,
body.BodyReg.DiskNumBytes)
fmt.Printf("\t\textenti data offset %v nr %v ram %v\n",
body.BodyReg.Offset,
body.BodyReg.NumBytes,
body.RAMBytes)
fmt.Printf("\t\textent compression %v\n",
body.Compression)
default:
fmt.Printf("\t\t(error) unknown file extent type %v", body.Type)
}
case btrfsitem.BlockGroup:
fmt.Printf("\t\tblock group used %v chunk_objectid %v flags %v\n",
body.Used, body.ChunkObjectID, body.Flags)
case btrfsitem.FreeSpaceInfo:
fmt.Printf("\t\tfree space info extent count %v flags %v\n",
body.ExtentCount, body.Flags)
case btrfsitem.FreeSpaceBitmap:
fmt.Printf("\t\tfree space bitmap\n")
case btrfsitem.Chunk:
fmt.Printf("\t\tlength %v owner %d stripe_len %v type %v\n",
body.Head.Size, body.Head.Owner, body.Head.StripeLen, body.Head.Type)
fmt.Printf("\t\tio_align %v io_width %v sector_size %v\n",
body.Head.IOOptimalAlign, body.Head.IOOptimalWidth, body.Head.IOMinSize)
fmt.Printf("\t\tnum_stripes %v sub_stripes %v\n",
body.Head.NumStripes, body.Head.SubStripes)
for i, stripe := range body.Stripes {
fmt.Printf("\t\t\tstripe %v devid %d offset %d\n",
i, stripe.DeviceID, stripe.Offset)
fmt.Printf("\t\t\tdev_uuid %v\n",
stripe.DeviceUUID)
}
case btrfsitem.Dev:
fmt.Printf(""+
"\t\tdevid %d total_bytes %v bytes_used %v\n"+
"\t\tio_align %v io_width %v sector_size %v type %v\n"+
"\t\tgeneration %v start_offset %v dev_group %v\n"+
"\t\tseek_speed %v bandwidth %v\n"+
"\t\tuuid %v\n"+
"\t\tfsid %v\n",
body.DeviceID, body.NumBytes, body.NumBytesUsed,
body.IOOptimalAlign, body.IOOptimalWidth, body.IOMinSize, body.Type,
body.Generation, body.StartOffset, body.DevGroup,
body.SeekSpeed, body.Bandwidth,
body.DevUUID,
body.FSUUID)
case btrfsitem.DevExtent:
fmt.Printf(""+
"\t\tdev extent chunk_tree %v\n"+
"\t\tchunk_objectid %v chunk_offset %v length %v\n"+
"\t\tchunk_tree_uuid %v\n",
body.ChunkTree, body.ChunkObjectID, body.ChunkOffset, body.Length,
body.ChunkTreeUUID)
//case btrfsitem.QGROUP_STATUS_KEY:
// // TODO
//case btrfsitem.QGROUP_INFO_KEY:
// // TODO
//case btrfsitem.QGROUP_LIMIT_KEY:
// // TODO
case btrfsitem.UUIDMap:
for _, subvolID := range body {
fmt.Printf("\t\tsubvol_id %d\n",
subvolID)
}
//case btrfsitem.STRING_ITEM_KEY:
// // TODO
case btrfsitem.DevStats:
fmt.Printf("\t\tpersistent item objectid %v offset %v\n",
item.Head.Key.ObjectID.Format(item.Head.Key.ItemType), item.Head.Key.Offset)
switch item.Head.Key.ObjectID {
case btrfs.DEV_STATS_OBJECTID:
fmt.Printf("\t\tdevice stats\n")
fmt.Printf("\t\twrite_errs %v read_errs %v flush_errs %v corruption_errs %v generation %v\n",
body.Values[btrfsitem.DEV_STAT_WRITE_ERRS],
body.Values[btrfsitem.DEV_STAT_READ_ERRS],
body.Values[btrfsitem.DEV_STAT_FLUSH_ERRS],
body.Values[btrfsitem.DEV_STAT_CORRUPTION_ERRS],
body.Values[btrfsitem.DEV_STAT_GENERATION_ERRS])
default:
fmt.Printf("\t\tunknown persistent item objectid %v\n", item.Head.Key.ObjectID)
}
//case btrfsitem.TEMPORARY_ITEM_KEY:
// // TODO
case btrfsitem.Empty:
switch item.Head.Key.ItemType {
case btrfsitem.ORPHAN_ITEM_KEY: // 48
fmt.Printf("\t\torphan item\n")
case btrfsitem.TREE_BLOCK_REF_KEY: // 176
fmt.Printf("\t\ttree block backref\n")
case btrfsitem.SHARED_BLOCK_REF_KEY: // 182
fmt.Printf("\t\tshared block backref\n")
case btrfsitem.FREE_SPACE_EXTENT_KEY: // 199
fmt.Printf("\t\tfree space extent\n")
case btrfsitem.QGROUP_RELATION_KEY: // 246
// do nothing
//case btrfsitem.EXTENT_REF_V0_KEY:
// fmt.Printf("\t\textent ref v0 (deprecated)\n")
//case btrfsitem.CSUM_ITEM_KEY:
// fmt.Printf("\t\tcsum item\n")
default:
fmt.Printf("\t\t(error) unhandled empty item type: %v\n", item.Head.Key.ItemType)
}
case btrfsitem.Error:
fmt.Printf("\t\t(error) error item: %v\n", body.Err)
default:
fmt.Printf("\t\t(error) unhandled item type: %T\n", body)
}
}
}
return nil
}
// printHeaderInfo mimics btrfs-progs kernel-shared/print-tree.c:print_header_info()
func printHeaderInfo(node btrfs.Node) {
var typename string
if node.Head.Level > 0 { // internal node
typename = "node"
fmt.Printf("node %v level %v items %v free space %v",
node.Head.Addr,
node.Head.Level,
node.Head.NumItems,
node.MaxItems()-node.Head.NumItems)
} else { // leaf node
typename = "leaf"
fmt.Printf("leaf %d items %v free space %v",
node.Head.Addr,
node.Head.NumItems,
node.LeafFreeSpace())
}
fmt.Printf(" generation %v owner %v\n",
node.Head.Generation,
node.Head.Owner)
fmt.Printf("%v %d flags %v backref revision %v\n",
typename,
node.Head.Addr,
node.Head.Flags,
node.Head.BackrefRev)
fmt.Printf("checksum stored %v\n", node.Head.Checksum)
if calcSum, err := node.CalculateChecksum(); err != nil {
fmt.Printf("checksum calced %v\n", err)
} else {
fmt.Printf("checksum calced %v\n", calcSum)
}
fmt.Printf("fs uuid %v\n", node.Head.MetadataUUID)
fmt.Printf("chunk uuid %v\n", node.Head.ChunkTreeUUID)
}
// printExtentInlineRefs mimics part of btrfs-progs kernel-shared/print-tree.c:print_extent_item()
func printExtentInlineRefs(refs []btrfsitem.ExtentInlineRef) {
for _, ref := range refs {
switch subitem := ref.Body.(type) {
case btrfsitem.Empty:
switch ref.Type {
case btrfsitem.TREE_BLOCK_REF_KEY:
fmt.Printf("\t\ttree block backref root %v\n",
btrfs.ObjID(ref.Offset))
case btrfsitem.SHARED_BLOCK_REF_KEY:
fmt.Printf("\t\tshared block backref parent %v\n",
ref.Offset)
default:
fmt.Printf("\t\t(error) unexpected empty sub-item type: %v\n", ref.Type)
}
case btrfsitem.ExtentDataRef:
fmt.Printf("\t\textent data backref root %v objectid %v offset %v count %v\n",
subitem.Root, subitem.ObjectID, subitem.Offset, subitem.Count)
case btrfsitem.SharedDataRef:
fmt.Printf("\t\tshared data backref parent %v count %v\n",
ref.Offset, subitem.Count)
default:
fmt.Printf("\t\t(error) unexpected sub-item type: %T\n", subitem)
}
}
}
// mimics print-tree.c:btrfs_print_key()
func FmtKey(key btrfs.Key) string {
var out strings.Builder
fmt.Fprintf(&out, "key (%v %v", key.ObjectID.Format(key.ItemType), key.ItemType)
switch key.ItemType {
case btrfsitem.QGROUP_RELATION_KEY: //TODO, btrfsitem.QGROUP_INFO_KEY, btrfsitem.QGROUP_LIMIT_KEY:
panic("not implemented")
case btrfsitem.UUID_SUBVOL_KEY, btrfsitem.UUID_RECEIVED_SUBVOL_KEY:
fmt.Fprintf(&out, " %v)", btrfs.PhysicalAddr(key.Offset))
case btrfsitem.ROOT_ITEM_KEY:
fmt.Fprintf(&out, " %v)", btrfs.ObjID(key.Offset))
default:
if key.Offset == util.MaxUint64pp-1 {
fmt.Fprintf(&out, " -1)")
} else {
fmt.Fprintf(&out, " %v)", key.Offset)
}
}
return out.String()
}
func fmtTime(t btrfs.Time) string {
return fmt.Sprintf("%v.%v (%v)",
t.Sec, t.NSec, t.ToStd().Format("2006-01-02 15:04:05"))
}
|