Expand description
Buffer cache wrapping a BlockDevice.
CachedDevice is the single source of truth for block contents
within a mount session. Mirrors Linux’s buffer-cache role for
journaled filesystems: reads are served from the cache, writes
update the cache, and the cache holds journaled-but-not-yet-
checkpointed bytes so that subsequent reads see them before the
data area on disk catches up.
Design:
- LRU clean entries —
entriesholds blocks read from disk or written throughwrite_at. LRU-evictable; the disk has the same bytes so eviction is safe. - Pinned entries —
pinnedholds blocks whose bytes only exist in this map and the journal log on disk; the data area on disk still has the pre-commit content. Pinned entries are NEVER evicted, since evicting them would lose the only in-memory copy and the next allocator scan would re-read stale bytes from disk.Filesystem::commit_block_bufferpopulatespinnedafter a successful journal commit; theFilesystem::replay_journal_if_dirtyhook callsunpin_allwhen the journal has been checkpointed. - Write-through update —
write_atUPDATES the cache (not invalidates) and forwards to the inner device. This keeps the cache consistent with disk for direct writes (e.g.write_inode_raw) and means a read-after-write is satisfied from the cache without bouncing to disk. - Block-aligned reads only. Multi-block reads bypass the cache and pass through.
- Crash safety unchanged. Pinned bytes are also persisted in
the journal log (the caller invoked
populate_cacheafter a journal commit); on crash, replay applies them. Clean LRU entries match disk by construction. - No external LRU crate — hand-rolled to avoid pulling in GPL/LGPL deps and to keep the cache logic auditable.
Structs§
- Cached
Device - LRU-cached BlockDevice. Pass-through for is_writable + size_bytes; caches block-aligned reads, invalidates on writes.