PageCacheState

Struct PageCacheState 

Source
#[repr(transparent)]
pub struct PageCacheState(LRUCache<(InodeNumber, FileBlockNumber), Slot, 512>);
Expand description

The global page cache state.

PageCacheState wraps an LRUCache mapping (InodeNumber, FileBlockNumber) to Slot entries. It enforces a bounded capacity of 512 slots, corresponding to 2 MiB of cached file data.

This state is protected by a Mutex inside PageCacheInner, allowing concurrent access from multiple threads with safe eviction.

Tuple Fields§

§0: LRUCache<(InodeNumber, FileBlockNumber), Slot, 512>

Implementations§

Source§

impl PageCacheState

Source

pub fn readahead(&mut self, file: RegularFile, fba: FileBlockNumber)

Perform readahead on sequential file blocks.

Reads up to 16 consecutive blocks after the given fba (file block address) into the cache.

Existing cached slots are not overwritten.

Source

pub fn insert(&mut self, id: (InodeNumber, FileBlockNumber), slot: Slot)

Insert a new Slot into the page cache.

Associates the given (inode, fba) pair with the slot. If the cache is at capacity, the least-recently-used slot will be automatically evicted (writing back its contents if dirty).

Source

pub fn do_read( &mut self, file: RegularFile, fba: FileBlockNumber, buf: &mut [u8; 4096], ) -> Result<bool, KernelError>

Read a file block into the provided buffer.

  • If the block is cached, copies directly from the page cache.
  • If the block is not cached, loads it synchronously from the file system, inserts it into the cache, and copies it into the buffer.

This method does not triggers the read-ahead requests.

Returns Ok(true) if there exists any byte read.

Source

pub fn do_write( &mut self, file: RegularFile, fba: FileBlockNumber, buf: &[u8; 4096], min_size: usize, ) -> Result<(), KernelError>

Write a file block through the page cache.

Updates (or inserts) the slot corresponding to the (file, fba) pair with the provided buffer. The slot is marked dirty, and at least min_size bytes are scheduled for write-back.

This method does not immediately flush to disk; explicit PageCacheState::do_writeback or eviction is required for persistence.

Source

pub fn do_mmap( &mut self, file: RegularFile, fba: FileBlockNumber, ) -> Result<Page, KernelError>

Provide a memory-mapped page for the given file block.

  • If the block is cached, returns a clone of the backing Page.
  • If not, loads the block into the cache and returns the new Page.

This allows direct access to the cached page memory.

Remove all slots associated with a given file.

Slots are dropped without flushing dirty data back to the file system. This is typically used during file unlink (deletion), where data persistence is no longer required.

Source

pub fn do_writeback(&mut self, file: RegularFile) -> Result<(), KernelError>

Write back all dirty slots belonging to the given file.

Ensures that all cached modifications to the file are persisted to the underlying file system.

Methods from Deref<Target = LRUCache<(InodeNumber, FileBlockNumber), Slot, 512>>§

Source

fn attach(&mut self, k: K) -> &mut Node<K, V>

Source

fn detach(&mut self, prev: Option<K>, next: Option<K>)

Source

pub fn get(&mut self, k: K) -> Option<&mut V>

Returns a mutable reference to the value corresponding to the key and update the last access time.

Source

pub fn get_or_insert_with<E>( &mut self, k: K, f: impl FnOnce() -> Result<V, E>, ) -> Result<&mut V, E>

Inserts the value computed with f into the LRUCache if it is not present, then returns a reference to the value in the LRUCache.

Source

fn __put(&mut self, k: K, v: V) -> &mut Node<K, V>

Source

pub fn put(&mut self, k: K, v: V)

Inserts a key-value pair into the LRUCache.

If the map did have this key present, the value is updated. The key is not updated, though; this matters for types that can be == without being identical.

If the cache size is overflowed after insertion, evict the oldest accessed entry.

Source

pub fn remove(&mut self, k: &K) -> Option<V>

Removes a key from the LRUCache, returning the stored value if the key was previously in the LRUCache.

The key may be any borrowed form of the LRUCache’s key type, but the ordering on the borrowed form must match the ordering on the key type.

Source

pub fn retain(&mut self, f: impl FnMut(&K, &mut V) -> bool)

Retains only the elements specified by the predicate. In other words, remove all pairs (k, v) for which f(&k, &mut v) returns false.

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>

Iterates over the key-value pairs in the LRUCache.

Trait Implementations§

Source§

impl Deref for PageCacheState

Source§

type Target = LRUCache<(InodeNumber, FileBlockNumber), Slot, 512>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for PageCacheState

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

§

impl Freeze for PageCacheState

§

impl !RefUnwindSafe for PageCacheState

§

impl Send for PageCacheState

§

impl Sync for PageCacheState

§

impl Unpin for PageCacheState

§

impl !UnwindSafe for PageCacheState

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.