Skip to main content

Module block_cache

Module block_cache 

Source
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 entriesentries holds blocks read from disk or written through write_at. LRU-evictable; the disk has the same bytes so eviction is safe.
  • Pinned entriespinned holds 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_buffer populates pinned after a successful journal commit; the Filesystem::replay_journal_if_dirty hook calls unpin_all when the journal has been checkpointed.
  • Write-through updatewrite_at UPDATES 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_cache after 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§

CachedDevice
LRU-cached BlockDevice. Pass-through for is_writable + size_bytes; caches block-aligned reads, invalidates on writes.