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
//! bmap B+tree (`di_format == Btree`) walk — collect all data-fork extents.
//!
//! An inode whose data fork overflows the inline extent-list array is promoted
//! to `XFS_DINODE_FMT_BTREE`: the data fork then holds an `xfs_bmdr_block` ROOT
//! (a compact on-disk btree root), whose pointers reference filesystem blocks
//! holding full `xfs_btree_block` (bmbt) nodes. Interior nodes point deeper;
//! level-0 leaf blocks hold the same 16-byte `xfs_bmbt_rec` records the
//! extent-list format stores inline. Walking the tree in key order and
//! collecting every leaf record yields the file's complete extent map, which
//! then feeds the existing extent→file reconstruction unchanged.
//!
//! ## On-disk layout (VERBATIM from `fs/xfs/libxfs/xfs_format.h`)
//!
//! **Root** (`xfs_bmdr_block`, inline in the data fork):
//! ```text
//! bb_level : __be16 0 = leaf, > 0 = interior
//! bb_numrecs : __be16 valid records/keys in this block
//! ```
//! For a root with `bb_level > 0`, the header is followed by `keys[dmaxrecs]`
//! (`xfs_bmbt_key` = one `__be64 br_startoff`) then `ptrs[dmaxrecs]`
//! (`__be64` fsblock), where `dmaxrecs = (fork_len - 4) / (8 + 8)` — the max
//! keys/ptrs that fit in the fork. Only the first `bb_numrecs` are valid, but
//! **ptrs start at `4 + dmaxrecs*8`, not `4 + numrecs*8`** (verified against
//! `xfs_db`: a 336-byte v3 fork → `dmaxrecs = 20` → ptrs at fork offset 164).
//!
//! **Node/leaf block** (`xfs_btree_block`, in a filesystem block):
//! ```text
//! bb_magic : __be32 BMA3 (v5, CRC) / BMAP (v4)
//! bb_level : __be16
//! bb_numrecs : __be16
//! <long-form sibling/self/uuid/owner/crc header>
//! ```
//! The header length is the **long form** (bmbt uses 64-bit block pointers):
//! `XFS_BTREE_LBLOCK_LEN = 24` (v4, no CRC) / `XFS_BTREE_LBLOCK_CRC_LEN = 72`
//! (v5, CRC) — verified against a real `BMA3` leaf whose first `xfs_bmbt_rec`
//! begins at byte 72. A leaf (`bb_level == 0`) is followed by `recs[numrecs]`
//! (16-byte `xfs_bmbt_rec`). An interior block is followed by `keys[maxrecs]`
//! then `ptrs[maxrecs]`, `maxrecs = (blocksize - hdr) / (8 + 8)`.
//!
//! ## Safety
//!
//! The walk is bounded three ways so a hostile/corrupt tree can neither hang
//! nor exhaust memory: a maximum descent depth ([`MAX_BMBT_LEVELS`]), a cap on
//! the total pointers followed ([`MAX_BMBT_PTRS`]), and every block access is
//! bounds-checked (an out-of-image or wrong-magic pointer is skipped, not
//! panicked on). Records are collected, never the whole tree materialized.
use crate;
use crate;
use crateXfsError;
use crateBmbtRec;
use crateSuperblock;
/// `XFS_BMAP_MAGIC` — a v4 (non-CRC) bmbt block, ASCII `"BMAP"`.
pub const XFS_BMAP_MAGIC: u32 = 0x424d_4150;
/// `XFS_BMAP_CRC_MAGIC` — a v5 (CRC) bmbt block, ASCII `"BMA3"`.
pub const XFS_BMAP_CRC_MAGIC: u32 = 0x424d_4133;
/// `XFS_BTREE_LBLOCK_LEN` — long-form (64-bit-pointer) bmbt block header, v4.
const BMBT_BLOCK_LEN: usize = 24;
/// `XFS_BTREE_LBLOCK_CRC_LEN` — long-form CRC bmbt block header, v5.
const BMBT_BLOCK_CRC_LEN: usize = 72;
/// Size of an `xfs_bmbt_key` (`__be64 br_startoff`) / `xfs_bmbt_ptr` (`__be64`).
const KEY_LEN: usize = 8;
const PTR_LEN: usize = 8;
/// Size of a packed `xfs_bmbt_rec`.
const REC_LEN: usize = 16;
/// The `xfs_bmdr_block` root header length (`bb_level` + `bb_numrecs`).
const BMDR_HDR_LEN: usize = 4;
/// Verify the v5 CRC32c of an on-disk bmbt `block`, returning `Some(true/false)`
/// on a v5 (`BMA3`) block and `None` on a v4 (`BMAP`) block (no CRC).
///
/// The CRC field (`bb_u.l.bb_crc`) sits at offset 64 inside the 72-byte
/// long-form CRC header (`XFS_BTREE_LBLOCK_CRC_OFF`, VERBATIM from
/// `xfs_format.h`), covering the whole block. A block whose 32-bit magic at
/// offset 0 is neither `BMA3` nor `BMAP` is not a bmbt block, so it returns
/// `None` (no CRC claim — never a false mismatch). **Non-fatal and panic-free**:
/// a short/hostile `BMA3` block that cannot hold the CRC field verifies as
/// `Some(false)`.
/// Maximum bmbt tree depth followed. Real bmbt trees are at most a handful of
/// levels deep (the fanout is hundreds per block); a claimed depth beyond this
/// is corrupt/hostile and the walk stops rather than recurse unboundedly.
pub const MAX_BMBT_LEVELS: u16 = 32;
/// Maximum total block pointers the walk will follow across the whole tree — an
/// allocation/DoS cap so a fabricated wide tree cannot make the walk run for an
/// unbounded time or collect an unbounded number of records.
pub const MAX_BMBT_PTRS: usize = 1 << 20;
/// Walk a bmap B+tree given its inline `xfs_bmdr_block` root (the inode data
/// fork) and collect every leaf `xfs_bmbt_rec` in tree (`startoff`) order.
///
/// `root_fork` is the inode's raw data fork for an `InodeFormat::Btree` inode.
/// The returned extents are exactly what an extents-format inode would store
/// inline, so they feed the existing extent→file reconstruction unchanged.
///
/// Panic-free and bounded (see [`MAX_BMBT_LEVELS`] / [`MAX_BMBT_PTRS`]): an
/// out-of-image or wrong-magic pointer is skipped; a truncated block yields
/// only the records that fully fit.
///
/// # Errors
///
/// Currently infallible in practice (a malformed tree degrades to fewer/no
/// extents rather than erroring), but returns `Result` so a future hard-fail
/// mode (e.g. a strict/verifying walk) is a non-breaking addition.
/// Read the `numrecs` valid pointers from a bmdr ROOT fork (ptrs at the
/// `dmaxrecs`-based offset). Bounds-stopping: a ptr slot outside the fork ends
/// the list.
/// Descend into (or collect from) the on-disk bmbt block at filesystem block
/// `fsblock`. `expected_level` is the level this block should carry (from the
/// parent); it is advisory — the block's own `bb_level` governs the walk, and
/// the depth cap uses the recursion count.
/// Collect up to `numrecs` 16-byte `xfs_bmbt_rec` records starting at `hdr`.
/// Bounds-stopping: records past the block end are not read.
/// The byte slice of filesystem block `fsblock` within the image, or `None` if
/// it lies (wholly or partly) outside the image (bounds-checked, never panics).