Expand description
Block + inode bitmap allocator — planning layer.
Phase 4 write path scaffolding. This module produces typed
[AllocationPlan] values describing what bits to flip in which bitmap
block + the updated free-counter deltas. It does NOT write to disk;
E11 (journaled writes) will apply the plans atomically under a
JBD2 transaction.
Rationale: separating allocation (pure function over bitmap bytes) from commit (journaled block write) makes tests trivial and keeps the read-only mount path untouched. Block device traits stay read-only in Phase 1; the write trait lives at the commit boundary.
§Block bitmap layout
One bit per block in the group. Bit i = block group_start + i.
A 4 KiB block bitmap covers 32768 blocks (one block group on a
4 KiB-block fs). Bits are packed LSB-first within each byte: bit 0 of
byte 0 represents the first block in the group.
§Inode bitmap layout
Same LSB-first packing. Bit i = inode (group_idx * inodes_per_group) + i + 1
(inode numbers are 1-based).
§Orlov allocator (directories)
Linux ext4 chooses a group for new directories using the Orlov heuristic:
prefer groups whose (free_blocks, free_inodes, used_dirs) triple is
“below average” — distributing directories evenly across groups so sibling
files end up near their parent dir. We implement a simplified variant:
iterate groups starting from hint, prefer one whose used_dirs is below
the fleet average and has the most free_inodes.
Structs§
- BgdCounter
Update - A change to one block-group descriptor’s free-counter and/or
used_dirs_count. Applied together with the matching
BitmapWrite. - Bitmap
Write - A change to one bitmap block: flip bits
bit_start .. bit_start + countfrom 0 (free) to 1 (used). The new bitmap bytes are NOT materialised here — only the semantic description is. - Block
Allocation Plan - Complete plan for one block-allocation request.
- Inode
Allocation Plan - Complete plan for one inode-allocation request.
- Superblock
Counter Update - A change to the superblock free-counter totals.
Functions§
- apply_
bitmap_ write - Apply a
BitmapWriteto a bitmap buffer in place. - bit_
is_ set - Test bit
idxin a bitmap (LSB-first within each byte). - find_
first_ free - Find the first free (0-valued) bit at or after
start, searching up tomax_bitstotal. ReturnsNoneif none found. - find_
free_ run - Find the first run of
countconsecutive free bits at or afterstart, within the firstmax_bitsbits of the bitmap. Returns the starting bit index of the run, orNoneif no such run exists. - plan_
block_ allocation - Plan allocation of
countcontiguous blocks. - plan_
inode_ allocation - Plan allocation of a single inode. For directories, uses a simplified
Orlov heuristic to spread dirs across groups; for regular files, prefers
the
hint_group(typically the parent directory’s group).