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. It plays the part a buffer cache plays for a journaled filesystem: 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 — entries holds blocks read from disk or written through write_at. LRU-evictable; the disk has the same bytes so eviction is safe.
  • Pinned entries — pinned 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. A read-only mount’s journal replay (journal_apply::replay_into_cache) populates pinned; the Filesystem::replay_journal_if_dirty hook calls unpin_all when the journal has been checkpointed. A journaled commit pins nothing: it checkpoints before returning, and its write-through leaves clean entries (#328).
  • Write-through update — write_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 replayed them from it); 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.