ferrosys 0.2.0

Pure-Rust, general-purpose ext2/3/4 filesystem implementation (formatter, reader, resize-safe geometry)
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
//! The extent tree: mapping a file's logical blocks to physical runs.
//!
//! This module is pure — it turns allocated block ranges into extent leaves and
//! assembles them into a tree, with no I/O. An extent tree is a header followed by
//! entries: leaves at depth zero, index entries above. The root lives inline in the
//! inode's sixty-byte block area, where four entries fit; when the leaves outgrow it
//! the tree spills into external node blocks and the root becomes an index node.
//!
//! A run longer than a single leaf can address ([`MAX_EXTENT_LEN`] blocks) is split
//! across leaves; physically and logically adjacent runs coalesce.
//!
//! Building a tree is two steps, because the external nodes occupy blocks the caller
//! must allocate. [`plan_tree`] reports the shape — its depth and how many node
//! blocks it needs — and [`build_tree`] fills that shape in once the caller has the
//! blocks. Under `metadata_csum` each external node reserves a four-byte checksum at
//! [`tail_offset`]; the entry capacity is unchanged, because the tail occupies the
//! bytes left over after the last entry that fits.

use crate::geometry::BlockRange;
use crate::ondisk::{EXTENT_ENTRY_SIZE, ExtentHeader, ExtentIdx, ExtentLeaf, ParseError};

/// Largest block run a single extent leaf can map.
pub const MAX_EXTENT_LEN: u32 = 32768;

/// Deepest extent tree ext4 defines. A root at this depth indexes nodes one level
/// shallower, down to the leaves at depth zero.
pub const MAX_EXTENT_DEPTH: u16 = 5;

/// Logical blocks an extent tree can address: `ee_block` is a 32-bit field, so a
/// file spans at most this many blocks.
const MAX_LOGICAL_BLOCKS: u64 = 1 << 32;

/// A failure building or serializing an extent tree.
#[derive(Clone, Copy, PartialEq, Eq, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ExtentError {
    /// More entries than the target node holds.
    #[error("{entries} extent entries exceed the {capacity} that fit in the node")]
    #[non_exhaustive]
    TooManyEntries {
        /// Entries the node was asked to hold.
        entries: usize,
        /// Entries the node can hold.
        capacity: usize,
    },
    /// The node buffer is too small to hold an extent header.
    #[error("extent node buffer of {bytes} bytes cannot hold a header")]
    #[non_exhaustive]
    NodeTooSmall {
        /// Bytes the buffer holds.
        bytes: usize,
    },
    /// The leaves need a tree deeper than ext4 defines.
    #[error("extent tree of depth {depth} exceeds the maximum depth {MAX_EXTENT_DEPTH}")]
    #[non_exhaustive]
    TooDeep {
        /// Depth the leaves would need.
        depth: u16,
    },
    /// The file spans more logical blocks than a 32-bit `ee_block` addresses.
    #[error("file of {blocks} blocks exceeds the {MAX_LOGICAL_BLOCKS} an extent tree addresses")]
    #[non_exhaustive]
    FileTooLarge {
        /// Logical blocks the file needs.
        blocks: u64,
    },
    /// The supplied node blocks do not match the planned shape.
    #[error("extent tree needs {need} node blocks but {got} were supplied")]
    #[non_exhaustive]
    NodeBlockCount {
        /// Node blocks the shape requires.
        need: usize,
        /// Node blocks supplied.
        got: usize,
    },
    /// A leaf holds a run the on-disk `ee_len` field does not encode. The runs this
    /// module builds are always encodable, so this reaches a caller only for leaves it
    /// assembled itself.
    #[error(transparent)]
    Leaf(#[from] ParseError),
}

/// The parsed contents of one extent-tree node: its leaves at depth zero, or its
/// index entries above.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ExtentNode {
    /// A depth-zero node: the leaves that map logical runs to physical runs.
    Leaves(Vec<ExtentLeaf>),
    /// A node above depth zero: index entries pointing at deeper nodes.
    Index {
        /// Depth of this node.
        depth: u16,
        /// Index entries.
        entries: Vec<ExtentIdx>,
    },
}

impl ExtentNode {
    /// Depth of this node: zero for leaves.
    #[must_use]
    pub fn depth(&self) -> u16 {
        match self {
            Self::Leaves(_) => 0,
            Self::Index { depth, .. } => *depth,
        }
    }

    /// Number of entries the node holds.
    #[must_use]
    pub fn len(&self) -> usize {
        match self {
            Self::Leaves(leaves) => leaves.len(),
            Self::Index { entries, .. } => entries.len(),
        }
    }

    /// Whether the node holds no entries.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// An external node and the physical block it occupies.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ExtentNodeBlock {
    /// Physical block holding this node.
    pub block: u64,
    /// The node's contents.
    pub node: ExtentNode,
}

/// A complete extent tree: the root that lives inline in the inode, and the external
/// nodes it points at.
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct ExtentTree {
    /// The inode's inline root. Holds the leaves directly for a depth-zero tree.
    pub root: ExtentNode,
    /// External node blocks, ordered from the leaf level upward. Empty for a
    /// depth-zero tree.
    pub nodes: Vec<ExtentNodeBlock>,
}

/// The shape of the tree a set of leaves needs.
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct TreeShape {
    /// Depth of the root: zero when the leaves fit inline in the inode.
    pub depth: u16,
    /// Node count at each level, from the leaf level upward. Empty at depth zero.
    pub level_nodes: Vec<usize>,
    /// Total external node blocks the tree occupies.
    pub node_blocks: usize,
}

/// The number of entries that fit in a node of `node_bytes` bytes: one 12-byte
/// header plus 12-byte entries.
///
/// `node_bytes` is a size the caller supplies, and a node smaller than its own header
/// holds no entries — so anything below [`EXTENT_ENTRY_SIZE`] is zero rather than a
/// length that overruns the node it describes.
#[must_use]
pub const fn node_capacity(node_bytes: usize) -> usize {
    node_bytes.saturating_sub(EXTENT_ENTRY_SIZE) / EXTENT_ENTRY_SIZE
}

/// Byte offset of an external node's checksum tail: past the header and every entry
/// the node can hold. The bytes a full node leaves over are exactly where ext4 keeps
/// the tail, so reserving it costs no entry capacity.
///
/// A node with no room for entries has none for a tail either, and the offset is then
/// past the node's own end — which the write it is handed to rejects, as it does any
/// offset a buffer does not reach.
#[must_use]
pub const fn tail_offset(node_bytes: usize) -> usize {
    EXTENT_ENTRY_SIZE * (node_capacity(node_bytes) + 1)
}

/// Turn a file's allocated ranges into extent leaves.
///
/// The ranges are the file's blocks in logical order: the first range maps logical
/// block 0 onward. Runs longer than [`MAX_EXTENT_LEN`] split across leaves;
/// logically and physically adjacent runs coalesce into one.
///
/// # Errors
///
/// [`ExtentError::FileTooLarge`] if the ranges span more logical blocks than a
/// 32-bit logical block number addresses.
pub fn build_leaves(ranges: &[BlockRange]) -> Result<Vec<ExtentLeaf>, ExtentError> {
    let total: u64 = ranges.iter().map(|r| r.len).sum();
    if total > MAX_LOGICAL_BLOCKS {
        return Err(ExtentError::FileTooLarge { blocks: total });
    }

    let mut leaves: Vec<ExtentLeaf> = Vec::new();
    let mut logical: u64 = 0;
    for range in ranges {
        let mut phys = range.start;
        let mut left = range.len;
        while left > 0 {
            let take = left.min(u64::from(MAX_EXTENT_LEN)) as u32;
            // Coalesce with the previous leaf when the runs abut on both axes and
            // the result still fits in one leaf.
            if let Some(last) = leaves.last_mut()
                && last.initialized
                && u64::from(last.block) + u64::from(last.len) == logical
                && last.start + u64::from(last.len) == phys
                && u32::from(last.len) + take <= MAX_EXTENT_LEN
            {
                last.len += take as u16;
            } else {
                leaves.push(ExtentLeaf {
                    block: logical as u32,
                    len: take as u16,
                    start: phys,
                    initialized: true,
                });
            }
            logical += u64::from(take);
            phys += u64::from(take);
            left -= u64::from(take);
        }
    }
    Ok(leaves)
}

/// Work out the tree `leaves` leaves need: `inline_capacity` entries fit in the
/// inode's root, `node_capacity` in each external node.
///
/// A tree that fits inline has depth zero and needs no blocks. Otherwise the leaves
/// fill leaf-level node blocks, those blocks are indexed by a level above, and so on
/// until one level fits in the root.
///
/// # Errors
///
/// [`ExtentError::TooManyEntries`] if a node could hold fewer than two entries, so
/// no tree can converge; [`ExtentError::TooDeep`] if the leaves need a tree deeper
/// than [`MAX_EXTENT_DEPTH`].
pub fn plan_tree(
    leaves: usize,
    inline_capacity: usize,
    node_capacity: usize,
) -> Result<TreeShape, ExtentError> {
    if leaves <= inline_capacity {
        return Ok(TreeShape {
            depth: 0,
            level_nodes: Vec::new(),
            node_blocks: 0,
        });
    }
    // A node that holds one entry never reduces the level's width, so the tree
    // would grow without bound.
    if node_capacity < 2 || inline_capacity < 1 {
        return Err(ExtentError::TooManyEntries {
            entries: leaves,
            capacity: inline_capacity,
        });
    }

    let mut level_nodes = Vec::new();
    let mut width = leaves.div_ceil(node_capacity);
    level_nodes.push(width);
    while width > inline_capacity {
        width = width.div_ceil(node_capacity);
        level_nodes.push(width);
    }

    let depth = level_nodes.len() as u16;
    if depth > MAX_EXTENT_DEPTH {
        return Err(ExtentError::TooDeep { depth });
    }
    Ok(TreeShape {
        depth,
        node_blocks: level_nodes.iter().sum(),
        level_nodes,
    })
}

/// Assemble `leaves` into a tree rooted in the inode, spilling into `node_blocks` —
/// the physical blocks [`plan_tree`] asked for, consumed from the leaf level upward.
///
/// # Errors
///
/// [`ExtentError::NodeBlockCount`] if `node_blocks` does not match the planned
/// shape; the errors of [`plan_tree`].
pub fn build_tree(
    leaves: &[ExtentLeaf],
    inline_capacity: usize,
    node_capacity: usize,
    node_blocks: &[u64],
) -> Result<ExtentTree, ExtentError> {
    let shape = plan_tree(leaves.len(), inline_capacity, node_capacity)?;
    if node_blocks.len() != shape.node_blocks {
        return Err(ExtentError::NodeBlockCount {
            need: shape.node_blocks,
            got: node_blocks.len(),
        });
    }
    if shape.depth == 0 {
        return Ok(ExtentTree {
            root: ExtentNode::Leaves(leaves.to_vec()),
            nodes: Vec::new(),
        });
    }

    let mut nodes: Vec<ExtentNodeBlock> = Vec::with_capacity(shape.node_blocks);
    let mut taken = 0usize;

    // The leaf level: each node block holds a run of leaves, and the level above
    // indexes it by the first logical block that run covers.
    let mut children: Vec<ExtentIdx> = Vec::with_capacity(shape.level_nodes[0]);
    for chunk in leaves.chunks(node_capacity) {
        let block = node_blocks[taken];
        taken += 1;
        children.push(ExtentIdx {
            block: chunk[0].block,
            leaf: block,
        });
        nodes.push(ExtentNodeBlock {
            block,
            node: ExtentNode::Leaves(chunk.to_vec()),
        });
    }

    // Interior levels, each indexing the one below it.
    for depth in 1..shape.depth {
        let mut parents: Vec<ExtentIdx> = Vec::with_capacity(shape.level_nodes[depth as usize]);
        for chunk in children.chunks(node_capacity) {
            let block = node_blocks[taken];
            taken += 1;
            parents.push(ExtentIdx {
                block: chunk[0].block,
                leaf: block,
            });
            nodes.push(ExtentNodeBlock {
                block,
                node: ExtentNode::Index {
                    depth,
                    entries: chunk.to_vec(),
                },
            });
        }
        children = parents;
    }

    Ok(ExtentTree {
        root: ExtentNode::Index {
            depth: shape.depth,
            entries: children,
        },
        nodes,
    })
}

/// Serialize `node` into the front of `buf` — the inode's inline block area or an
/// external node block — declaring a capacity of `eh_max` entries. `buf` beyond the
/// header and entries is left as the caller set it, which is where an external
/// node's checksum tail sits.
///
/// # Errors
///
/// [`ExtentError::NodeTooSmall`] if `buf` cannot hold a header;
/// [`ExtentError::TooManyEntries`] if the node's entries exceed `eh_max` or overrun
/// `buf`.
pub fn write_node(node: &ExtentNode, eh_max: usize, buf: &mut [u8]) -> Result<(), ExtentError> {
    if buf.len() < EXTENT_ENTRY_SIZE {
        return Err(ExtentError::NodeTooSmall { bytes: buf.len() });
    }
    let entries = node.len();
    if entries > eh_max || EXTENT_ENTRY_SIZE * (eh_max + 1) > buf.len() {
        return Err(ExtentError::TooManyEntries {
            entries,
            capacity: eh_max.min(node_capacity(buf.len())),
        });
    }
    let header = ExtentHeader {
        entries: entries as u16,
        max: eh_max as u16,
        depth: node.depth(),
        generation: 0,
    };
    buf[0..EXTENT_ENTRY_SIZE].copy_from_slice(&header.to_bytes());
    let mut put = |i: usize, bytes: [u8; EXTENT_ENTRY_SIZE]| {
        let off = EXTENT_ENTRY_SIZE * (i + 1);
        buf[off..off + EXTENT_ENTRY_SIZE].copy_from_slice(&bytes);
    };
    match node {
        ExtentNode::Leaves(leaves) => {
            for (i, leaf) in leaves.iter().enumerate() {
                put(i, leaf.to_bytes()?);
            }
        }
        ExtentNode::Index { entries, .. } => {
            for (i, idx) in entries.iter().enumerate() {
                put(i, idx.to_bytes());
            }
        }
    }
    Ok(())
}

/// Parse one extent-tree node from the front of `buf`, returning its leaves (depth
/// zero) or its index entries (above). The header's entry count bounds the parse,
/// so trailing bytes — an inode's unused inline area, or a node's checksum tail —
/// are ignored.
///
/// # Errors
///
/// [`crate::ondisk::ParseError`] if the node is shorter than a header, its magic is
/// wrong, or its entry count overruns `buf`.
pub fn parse_node(buf: &[u8]) -> Result<ExtentNode, crate::ondisk::ParseError> {
    use crate::ondisk::ParseError;
    if buf.len() < EXTENT_ENTRY_SIZE {
        return Err(ParseError::TooShort {
            structure: "ExtentHeader",
            need: EXTENT_ENTRY_SIZE,
            got: buf.len(),
        });
    }
    let header_bytes: &[u8; EXTENT_ENTRY_SIZE] = buf[0..EXTENT_ENTRY_SIZE]
        .try_into()
        .expect("header slice is exactly EXTENT_ENTRY_SIZE bytes");
    let header = ExtentHeader::from_bytes(header_bytes)?;
    let entries = header.entries as usize;
    let need = EXTENT_ENTRY_SIZE * (entries + 1);
    if buf.len() < need {
        return Err(ParseError::TooShort {
            structure: "ExtentNode",
            need,
            got: buf.len(),
        });
    }
    let entry = |i: usize| -> [u8; EXTENT_ENTRY_SIZE] {
        let off = EXTENT_ENTRY_SIZE * (i + 1);
        buf[off..off + EXTENT_ENTRY_SIZE]
            .try_into()
            .expect("entry slice is exactly EXTENT_ENTRY_SIZE bytes")
    };
    if header.depth == 0 {
        let leaves = (0..entries)
            .map(|i| ExtentLeaf::from_bytes(&entry(i)))
            .collect();
        Ok(ExtentNode::Leaves(leaves))
    } else {
        let idxs = (0..entries)
            .map(|i| ExtentIdx::from_bytes(&entry(i)))
            .collect();
        Ok(ExtentNode::Index {
            depth: header.depth,
            entries: idxs,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ondisk::Inode;

    /// Entries in the inode's sixty-byte inline area.
    const INLINE: usize = node_capacity(Inode::BLOCK_BYTES);
    /// Entries in a 4096-byte external node.
    const NODE: usize = node_capacity(4096);

    fn leaves_of(n: usize) -> Vec<ExtentLeaf> {
        // Physically disjoint single-block runs, so nothing coalesces.
        (0..n)
            .map(|i| ExtentLeaf {
                block: i as u32,
                len: 1,
                start: 100 + (i as u64) * 2,
                initialized: true,
            })
            .collect()
    }

    #[test]
    fn single_contiguous_run_is_one_leaf() {
        // lost+found: four contiguous blocks at 7 -> one leaf (0-3):7-10.
        let leaves = build_leaves(&[BlockRange { start: 7, len: 4 }]).unwrap();
        assert_eq!(
            leaves,
            vec![ExtentLeaf {
                block: 0,
                len: 4,
                start: 7,
                initialized: true,
            }]
        );
    }

    #[test]
    fn separate_ranges_stay_separate_leaves() {
        let leaves = build_leaves(&[
            BlockRange { start: 100, len: 2 },
            BlockRange { start: 200, len: 3 },
        ])
        .unwrap();
        assert_eq!(leaves.len(), 2);
        assert_eq!(leaves[1].block, 2);
        assert_eq!(leaves[1].start, 200);
    }

    #[test]
    fn contiguous_ranges_coalesce() {
        let leaves = build_leaves(&[
            BlockRange { start: 50, len: 4 },
            BlockRange { start: 54, len: 6 },
        ])
        .unwrap();
        assert_eq!(
            leaves,
            vec![ExtentLeaf {
                block: 0,
                len: 10,
                start: 50,
                initialized: true
            }]
        );
    }

    #[test]
    fn runs_longer_than_a_leaf_split() {
        // A run of 40000 blocks splits into 32768 + 7232.
        let leaves = build_leaves(&[BlockRange {
            start: 1000,
            len: 40000,
        }])
        .unwrap();
        assert_eq!(leaves.len(), 2);
        assert_eq!(leaves[0].len, 32768);
        assert_eq!(leaves[1].block, 32768);
        assert_eq!(leaves[1].len, 7232);
        assert_eq!(leaves[1].start, 1000 + 32768);
    }

    #[test]
    fn a_file_past_the_logical_limit_is_rejected() {
        // 2^32 blocks is the most a 32-bit ee_block addresses; one more is not.
        let ok = build_leaves(&[BlockRange {
            start: 0,
            len: MAX_LOGICAL_BLOCKS,
        }]);
        assert!(ok.is_ok());
        assert_eq!(
            build_leaves(&[BlockRange {
                start: 0,
                len: MAX_LOGICAL_BLOCKS + 1,
            }]),
            Err(ExtentError::FileTooLarge {
                blocks: MAX_LOGICAL_BLOCKS + 1
            })
        );
    }

    #[test]
    fn the_inline_area_holds_four_entries_and_a_node_holds_three_hundred_and_forty() {
        assert_eq!(INLINE, 4);
        assert_eq!(NODE, 340);
        // A full 4096-byte node ends its last entry exactly where the checksum tail
        // begins, and the tail's four bytes close the block.
        assert_eq!(tail_offset(4096), 4092);
        assert_eq!(tail_offset(4096) + 4, 4096);
    }

    #[test]
    fn a_node_smaller_than_its_header_holds_no_entries() {
        // `node_bytes` is a size the caller supplies, and one below a single header
        // describes no node at all. The capacity is zero, not a length that would
        // overrun the bytes it was measured against.
        assert_eq!(node_capacity(0), 0);
        assert_eq!(node_capacity(EXTENT_ENTRY_SIZE - 1), 0);
        assert_eq!(node_capacity(EXTENT_ENTRY_SIZE), 0);
        assert_eq!(node_capacity(2 * EXTENT_ENTRY_SIZE), 1);
        // With no entries there is nowhere in the node for a tail either, so the offset
        // lands past its end — where the write that takes it refuses, as it does any
        // offset the buffer does not reach.
        assert_eq!(tail_offset(0), EXTENT_ENTRY_SIZE);
        assert!(matches!(
            write_node(
                &ExtentNode::Leaves(Vec::new()),
                node_capacity(4),
                &mut [0u8; 4]
            ),
            Err(ExtentError::NodeTooSmall { .. })
        ));
    }

    #[test]
    fn leaves_that_fit_inline_need_no_tree() {
        let shape = plan_tree(INLINE, INLINE, NODE).unwrap();
        assert_eq!(shape.depth, 0);
        assert_eq!(shape.node_blocks, 0);

        let leaves = leaves_of(INLINE);
        let tree = build_tree(&leaves, INLINE, NODE, &[]).unwrap();
        assert_eq!(tree.root, ExtentNode::Leaves(leaves));
        assert!(tree.nodes.is_empty());
    }

    #[test]
    fn a_fifth_leaf_spills_into_one_external_node() {
        // Five leaves do not fit the inode's four slots, so the tree spills.
        let shape = plan_tree(INLINE + 1, INLINE, NODE).unwrap();
        assert_eq!(shape.depth, 1);
        assert_eq!(shape.level_nodes, vec![1]);
        assert_eq!(shape.node_blocks, 1);

        let leaves = leaves_of(INLINE + 1);
        let tree = build_tree(&leaves, INLINE, NODE, &[900]).unwrap();
        assert_eq!(
            tree.root,
            ExtentNode::Index {
                depth: 1,
                entries: vec![ExtentIdx {
                    block: 0,
                    leaf: 900
                }],
            }
        );
        assert_eq!(tree.nodes.len(), 1);
        assert_eq!(tree.nodes[0].block, 900);
        assert_eq!(tree.nodes[0].node, ExtentNode::Leaves(leaves));
    }

    #[test]
    fn a_depth_one_tree_fills_its_leaf_nodes_in_order() {
        let leaves = leaves_of(NODE + 1);
        let shape = plan_tree(leaves.len(), INLINE, NODE).unwrap();
        assert_eq!(shape.depth, 1);
        assert_eq!(shape.level_nodes, vec![2]);

        let tree = build_tree(&leaves, INLINE, NODE, &[900, 901]).unwrap();
        let ExtentNode::Index { entries, depth } = &tree.root else {
            panic!("root indexes the leaf nodes");
        };
        assert_eq!(*depth, 1);
        // Each index entry names the first logical block of the node it points at.
        assert_eq!(
            entries[0],
            ExtentIdx {
                block: 0,
                leaf: 900
            }
        );
        assert_eq!(
            entries[1],
            ExtentIdx {
                block: NODE as u32,
                leaf: 901
            }
        );
        assert_eq!(tree.nodes[0].node.len(), NODE);
        assert_eq!(tree.nodes[1].node.len(), 1);
    }

    #[test]
    fn a_tree_grows_to_depth_two_when_the_root_cannot_index_the_leaf_level() {
        // More leaf nodes than the inline root's four slots forces a level between.
        let leaves = leaves_of(NODE * (INLINE + 1));
        let shape = plan_tree(leaves.len(), INLINE, NODE).unwrap();
        assert_eq!(shape.depth, 2);
        assert_eq!(shape.level_nodes, vec![INLINE + 1, 1]);
        assert_eq!(shape.node_blocks, INLINE + 2);

        let blocks: Vec<u64> = (900..900 + shape.node_blocks as u64).collect();
        let tree = build_tree(&leaves, INLINE, NODE, &blocks).unwrap();

        // The root holds a single index entry pointing at the one interior node,
        // which is the last block allocated because levels fill from the leaves up.
        let ExtentNode::Index { entries, depth } = &tree.root else {
            panic!("root indexes the interior level");
        };
        assert_eq!(*depth, 2);
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].leaf, *blocks.last().unwrap());

        let interior = &tree.nodes.last().unwrap().node;
        assert_eq!(interior.depth(), 1);
        assert_eq!(interior.len(), INLINE + 1);
        // Every other node is a leaf node.
        assert!(tree.nodes[..INLINE + 1].iter().all(|n| n.node.depth() == 0));
    }

    #[test]
    fn every_leaf_appears_exactly_once_in_a_deep_tree() {
        // A narrow node capacity reaches depth two on a small leaf count: 100 leaves
        // fill 13 leaf nodes, which two interior nodes index, which the root indexes.
        let leaves = leaves_of(100);
        let shape = plan_tree(leaves.len(), INLINE, 8).unwrap();
        assert_eq!(shape.depth, 2);
        assert_eq!(shape.level_nodes, vec![13, 2]);

        let blocks: Vec<u64> = (900..900 + shape.node_blocks as u64).collect();
        let tree = build_tree(&leaves, INLINE, 8, &blocks).unwrap();

        // Reading the leaf nodes in allocation order recovers the file's leaves in
        // logical order, with none dropped and none repeated.
        let mut seen: Vec<ExtentLeaf> = Vec::new();
        for node in &tree.nodes {
            if let ExtentNode::Leaves(l) = &node.node {
                seen.extend(l.iter().copied());
            }
        }
        assert_eq!(seen, leaves, "leaves survive the split in logical order");

        // Every external node is pointed at exactly once, by the level above it.
        let mut pointed: Vec<u64> = Vec::new();
        for node in &tree.nodes {
            if let ExtentNode::Index { entries, .. } = &node.node {
                pointed.extend(entries.iter().map(|e| e.leaf));
            }
        }
        let ExtentNode::Index { entries, .. } = &tree.root else {
            panic!("root indexes the interior level");
        };
        pointed.extend(entries.iter().map(|e| e.leaf));
        pointed.sort_unstable();
        assert_eq!(pointed, blocks);
    }

    #[test]
    fn a_tree_deeper_than_ext4_defines_is_rejected() {
        // Capacity two at every level: depth grows like log2 of the leaf count.
        assert_eq!(plan_tree(4, 2, 2).unwrap().depth, 1);
        assert_eq!(plan_tree(64, 2, 2).unwrap().depth, 5);
        assert_eq!(plan_tree(128, 2, 2), Err(ExtentError::TooDeep { depth: 6 }));
    }

    #[test]
    fn a_node_that_cannot_narrow_a_level_is_rejected() {
        assert!(matches!(
            plan_tree(10, 4, 1),
            Err(ExtentError::TooManyEntries { .. })
        ));
    }

    #[test]
    fn build_tree_checks_the_node_blocks_against_the_plan() {
        let leaves = leaves_of(INLINE + 1);
        assert_eq!(
            build_tree(&leaves, INLINE, NODE, &[]),
            Err(ExtentError::NodeBlockCount { need: 1, got: 0 })
        );
    }

    #[test]
    fn inline_node_round_trips_through_the_inode_block_area() {
        let leaves = build_leaves(&[BlockRange { start: 6, len: 1 }]).unwrap();
        let mut block = [0u8; Inode::BLOCK_BYTES];
        write_node(&ExtentNode::Leaves(leaves.clone()), INLINE, &mut block).unwrap();
        match parse_node(&block).unwrap() {
            ExtentNode::Leaves(back) => assert_eq!(back, leaves),
            other => panic!("expected leaves, got {other:?}"),
        }
    }

    #[test]
    fn an_index_node_round_trips_through_a_block() {
        let node = ExtentNode::Index {
            depth: 1,
            entries: vec![
                ExtentIdx {
                    block: 0,
                    leaf: 900,
                },
                ExtentIdx {
                    block: 340,
                    leaf: 0x1_0000_0001,
                },
            ],
        };
        let mut buf = vec![0u8; 4096];
        write_node(&node, NODE, &mut buf).unwrap();
        // The declared capacity is the node's, not the entry count.
        assert_eq!(u16::from_le_bytes([buf[4], buf[5]]), NODE as u16);
        assert_eq!(parse_node(&buf).unwrap(), node);
    }

    #[test]
    fn write_node_rejects_more_entries_than_the_capacity() {
        let five = ExtentNode::Leaves(leaves_of(INLINE + 1));
        let mut block = [0u8; Inode::BLOCK_BYTES];
        assert_eq!(
            write_node(&five, INLINE, &mut block),
            Err(ExtentError::TooManyEntries {
                entries: 5,
                capacity: 4
            })
        );
    }

    #[test]
    fn parse_ignores_trailing_inline_bytes() {
        // Only the header's entry count is parsed; the inode's unused tail is left
        // untouched and does not become a phantom extent.
        let leaves = build_leaves(&[BlockRange { start: 6, len: 1 }]).unwrap();
        let mut block = [0xabu8; Inode::BLOCK_BYTES];
        write_node(&ExtentNode::Leaves(leaves), INLINE, &mut block).unwrap();
        match parse_node(&block).unwrap() {
            ExtentNode::Leaves(back) => assert_eq!(back.len(), 1),
            other => panic!("expected one leaf, got {other:?}"),
        }
    }
}