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
|
package main
import (
"fmt"
"os"
"strings"
"lukeshu.com/btrfs-tools/pkg/btrfs"
"lukeshu.com/btrfs-tools/pkg/btrfs/btrfsitem"
"lukeshu.com/btrfs-tools/pkg/util"
)
func main() {
if err := Main(os.Args[1]); err != nil {
fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
os.Exit(1)
}
}
const version = "5.17"
func Main(imgfilename string) (err error) {
maybeSetErr := func(_err error) {
if _err != nil && err == nil {
err = _err
}
}
fh, err := os.Open(imgfilename)
if err != nil {
return err
}
defer func() {
maybeSetErr(fh.Close())
}()
fs := &btrfs.FS{
Devices: []*btrfs.Device{
{
File: fh,
},
},
}
if err := fs.Init(); err != nil {
return err
}
superblock, err := fs.Superblock()
if err != nil {
return err
}
fmt.Printf("btrfs-progs v%s \n", version)
if superblock.Data.RootTree != 0 {
fmt.Printf("root tree\n")
if err := printTree(fs, superblock.Data.RootTree); err != nil {
return err
}
}
if superblock.Data.ChunkTree != 0 {
fmt.Printf("chunk tree\n")
if err := printTree(fs, superblock.Data.ChunkTree); err != nil {
return err
}
}
if superblock.Data.LogTree != 0 {
fmt.Printf("log root tree\n")
if err := printTree(fs, superblock.Data.LogTree); err != nil {
return err
}
}
if superblock.Data.BlockGroupRoot != 0 {
fmt.Printf("block group tree\n")
if err := printTree(fs, superblock.Data.BlockGroupRoot); err != nil {
return err
}
}
if err := fs.WalkTree(superblock.Data.RootTree, btrfs.WalkTreeHandler{
Item: func(key btrfs.Key, body btrfsitem.Item) error {
if key.ItemType != btrfsitem.ROOT_ITEM_KEY {
return nil
}
return printTree(fs, body.(btrfsitem.Root).ByteNr)
},
}); err != nil {
return err
}
return nil
}
// 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)
return nil
}
node := nodeRef.Data
printHeaderInfo(node)
if node.Head.Level > 0 { // internal
for _, item := range node.BodyInternal {
fmt.Printf("\t%s block %d gen %d\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 %d %s itemoff %d itemsize %d\n",
i,
fmtKey(item.Head.Key),
item.Head.DataOffset,
item.Head.DataSize)
switch body := item.Body.(type) {
case btrfsitem.Inode:
fmt.Printf(""+
"\t\tgeneration %d transid %d size %d nbytes %d\n"+
"\t\tblock group %d mode %o links %d uid %d gid %d rdev %d\n"+
"\t\tsequence %d 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 %s\n", fmtTime(body.ATime))
fmt.Printf("\t\tctime %s\n", fmtTime(body.CTime))
fmt.Printf("\t\tmtime %s\n", fmtTime(body.MTime))
fmt.Printf("\t\totime %s\n", fmtTime(body.OTime))
case btrfsitem.InodeRefList:
for _, ref := range body {
fmt.Printf("\t\tindex %d namelen %d 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 %s type %v\n",
fmtKey(dir.Location), dir.Type)
fmt.Printf("\t\ttransid %d data_len %d name_len %d\n",
dir.TransID, dir.DataLen, dir.NameLen)
fmt.Printf("\t\tname: %s\n", dir.Name)
if len(dir.Data) > 0 {
fmt.Printf("\t\tdata %s\n", dir.Data)
}
}
//case btrfsitem.DIR_LOG_INDEX_KEY, btrfsitem.DIR_LOG_ITEM_KEY:
// // TODO
case btrfsitem.Root:
fmt.Printf("\t\tgeneration %d root_dirid %d bytenr %d byte_limit %d bytes_used %d\n",
body.Generation, body.RootDirID, body.ByteNr, body.ByteLimit, body.BytesUsed)
fmt.Printf("\t\tlast_snapshot %d flags %s refs %d\n",
body.LastSnapshot, body.Flags, body.Refs)
fmt.Printf("\t\tdrop_progress %s drop_level %d\n",
fmtKey(body.DropProgress), body.DropLevel)
fmt.Printf("\t\tlevel %d generation_v2 %d\n",
body.Level, body.GenerationV2)
if body.Generation == body.GenerationV2 {
fmt.Printf("\t\tuuid %s\n", body.UUID)
fmt.Printf("\t\tparent_uuid %s\n", body.ParentUUID)
fmt.Printf("\t\treceived_uuid %s\n", body.ReceivedUUID)
fmt.Printf("\t\tctransid %d otransid %d stransid %d rtransid %d\n",
body.CTransID, body.OTransID, body.STransID, body.RTransID)
fmt.Printf("\t\tctime %s\n", fmtTime(body.CTime))
fmt.Printf("\t\totime %s\n", fmtTime(body.OTime))
fmt.Printf("\t\tstime %s\n", fmtTime(body.STime))
fmt.Printf("\t\trtime %s\n", fmtTime(body.RTime))
}
//case btrfsitem.ROOT_REF_KEY:
// // TODO
//case btrfsitem.ROOT_BACKREF_KEY:
// // TODO
//case btrfsitem.EXTENT_ITEM_KEY:
// // TODO
//case btrfsitem.METADATA_ITEM_KEY:
// // TODO
//case btrfsitem.TREE_BLOCK_REF_KEY:
// fmt.Printf("\t\ttree block backref\n")
//case btrfsitem.SHARED_BLOCK_REF_KEY:
// fmt.Printf("\t\tshared block backref\n")
//case btrfsitem.EXTENT_DATA_REF_KEY:
// // TODO
//case btrfsitem.SHARED_DATA_REF_KEY:
// // TODO
//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")
//case btrfsitem.EXTENT_CSUM_KEY:
// // TODO
//case btrfsitem.EXTENT_DATA_KEY:
// // TODO
//case btrfsitem.BLOCK_GROUP_ITEM_KEY:
// // TODO
//case btrfsitem.FREE_SPACE_INFO_KEY:
// // TODO
//case btrfsitem.FREE_SPACE_EXTENT_KEY:
// fmt.Printf("\t\tfree space extent\n")
//case btrfsitem.FREE_SPACE_BITMAP_KEY:
// fmt.Printf("\t\tfree space bitmap\n")
case btrfsitem.Chunk:
fmt.Printf("\t\tlength %d owner %d stripe_len %d type %v\n",
body.Size, body.Owner, body.StripeLen, body.Type)
fmt.Printf("\t\tio_align %d io_width %d sector_size %d\n",
body.IOOptimalAlign, body.IOOptimalWidth, body.IOMinSize)
fmt.Printf("\t\tnum_stripes %d sub_stripes %d\n",
body.NumStripes, body.SubStripes)
for i, stripe := range body.Stripes {
fmt.Printf("\t\t\tstripe %d devid %d offset %d\n",
i, stripe.DeviceID, stripe.Offset)
fmt.Printf("\t\t\tdev_uuid %s\n",
stripe.DeviceUUID)
}
case btrfsitem.Dev:
fmt.Printf(""+
"\t\tdevid %d total_bytes %d bytes_used %d\n"+
"\t\tio_align %d io_width %d sector_size %d type %d\n"+
"\t\tgeneration %d start_offset %d dev_group %d\n"+
"\t\tseek_speed %d bandwidth %d\n"+
"\t\tuuid %s\n"+
"\t\tfsid %s\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.DEV_EXTENT_KEY:
// // TODO
//case btrfsitem.QGROUP_STATUS_KEY:
// // TODO
//case btrfsitem.QGROUP_RELATION_KEY, btrfsitem.QGROUP_INFO_KEY:
// // TODO
//case btrfsitem.QGROUP_LIMIT_KEY:
// // TODO
//case btrfsitem.UUIDMap:
// // TODO
//case btrfsitem.STRING_ITEM_KEY:
// // TODO
//case btrfsitem.PERSISTENT_ITEM_KEY:
// // TODO
//case btrfsitem.TEMPORARY_ITEM_KEY:
// // TODO
case btrfsitem.Empty:
switch item.Head.Key.ItemType {
case btrfsitem.ORPHAN_ITEM_KEY:
fmt.Printf("\t\torphan item\n")
default:
return fmt.Errorf("unhandled empty item type: %v", 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 %d level %d items %d free space %d",
node.Head.Addr,
node.Head.Level,
node.Head.NumItems,
node.MaxItems()-node.Head.NumItems)
} else { // leaf node
typename = "leaf"
fmt.Printf("leaf %d items %d free space %d",
node.Head.Addr,
node.Head.NumItems,
node.LeafFreeSpace())
}
fmt.Printf(" generation %d owner %v\n",
node.Head.Generation,
node.Head.Owner)
fmt.Printf("%s %d flags %s backref revision %d\n",
typename,
node.Head.Addr,
node.Head.Flags,
node.Head.BackrefRev)
fmt.Printf("checksum stored %x\n", node.Head.Checksum)
if calcSum, err := node.CalculateChecksum(); err != nil {
fmt.Printf("checksum calced %v\n", err)
} else {
fmt.Printf("checksum calced %x\n", calcSum)
}
fmt.Printf("fs uuid %s\n", node.Head.MetadataUUID)
fmt.Printf("chunk uuid %s\n", node.Head.ChunkTreeUUID)
}
// mimics print-tree.c:btrfs_print_key()
func fmtKey(key btrfs.Key) string {
var out strings.Builder
fmt.Fprintf(&out, "key (%s %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, " 0x%016x)", 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, " %d)", key.Offset)
}
}
return out.String()
}
func fmtTime(t btrfs.Time) string {
return fmt.Sprintf("%d.%d (%s)",
t.Sec, t.NSec, t.ToStd().Format("2006-01-02 15:04:05"))
}
|