diff --git a/services/pddb/src/backend/fastspace.rs b/services/pddb/src/backend/fastspace.rs index 4f5276dc8..5e8c86a67 100644 --- a/services/pddb/src/backend/fastspace.rs +++ b/services/pddb/src/backend/fastspace.rs @@ -7,8 +7,9 @@ use crate::*; /// entries per page of storage for the free_pool, or 4k * 1000 ~ 4MiB per page, when PhysAddr is a u32 pub(crate) const FASTSPACE_PAGES: usize = 2; +#[derive(Debug)] #[repr(u8)] -pub(crate) enum SpaceState { +pub enum SpaceState { /// pages that are completely un-spoken for Free = 0, /// pages that are in the process of being used, but the journal has yet to be committed @@ -19,6 +20,21 @@ pub(crate) enum SpaceState { /// pages that are no longer used and need to be erased Dirty = 3, } +impl From for SpaceState { + fn from(arg: u8) -> Self { + match arg & 0x3 { + 0 => SpaceState::Free, + 1 => SpaceState::MaybeUsed, + 2 => SpaceState::Used, + _ => SpaceState::Dirty, + } + } +} +impl From for u8 { + fn from(arg: SpaceState) -> Self { + arg as u8 + } +} /// FastSpace tracks a limited set of physical pages /// An optimal implementation of this would fill whole pages with the free_pool array. diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index 182961ec0..9f674630f 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -189,7 +189,7 @@ impl PddbOs { /// needs to run in a fairly memory-constrained environment, keeping in mind that if the PDDB /// structures were to be extended to run on say, an external USB drive with gigabytes of space, /// we cannot afford to naively allocate vectors that count every single page. - fn collect_fastspace(&self) -> Vec:: { + fn collect_fastspace(&mut self) -> Vec:: { let mut free_pool = Vec::::new(); let max_entries = FASTSPACE_PAGES * PAGE_SIZE / core::mem::size_of::(); free_pool.reserve_exact(max_entries); @@ -197,7 +197,7 @@ impl PddbOs { // WARNING: this could get really big for a very large filesystem. It's capped at ~100k for // Precursor's ~100MiB storage increment. let mut page_heap = BinaryHeap::new(); - for (_, basismap) in self.v2p_map { + for (_, basismap) in &self.v2p_map { for (_, pp) in basismap { page_heap.push(Reverse(pp.page_number())); } @@ -228,7 +228,8 @@ impl PddbOs { } else { // page is free, but we have no space in the pool. // pick a random page from the pool, and replace it with the current page - free_pool[self.trng_cache_u32() as usize % free_pool.len()] = page_candidate; + let index = self.trng_cache_u32() as usize % free_pool.len(); + free_pool[index] = page_candidate; } } // 3. shuffle the contents of free_pool. This is important in the case that the @@ -248,8 +249,15 @@ impl PddbOs { let deniable_free_pages = if deniable_free_pages == 0 { 1 } else { deniable_free_pages }; free_pool.truncate(deniable_free_pages); - /// TODO: translate the free_pool into a PhysPage vector with the right bitfields attached to it. - free_pool + /// 5. Take the free_pool and annotate it for writing to disk + let mut page_pool = Vec::::new(); + for page in free_pool { + let mut pp = PhysPage(0); + pp.set_page_number(page as PhysAddr); + pp.set_space_state(SpaceState::Free); + page_pool.push(pp); + } + page_pool } /// this function is dangerous in that calling it will completely erase all of the previous data diff --git a/services/pddb/src/backend/types.rs b/services/pddb/src/backend/types.rs index 363ef053a..d571f41a8 100644 --- a/services/pddb/src/backend/types.rs +++ b/services/pddb/src/backend/types.rs @@ -2,6 +2,7 @@ use core::num::{NonZeroU32, NonZeroU64}; use core::convert::TryFrom; use core::ops::Add; use super::PAGE_SIZE; +use crate::SpaceState; use bitfield::bitfield; /// We should be able to change this to a u64 and everything should "just work", but @@ -17,10 +18,11 @@ bitfield! { pub struct PhysPage(PhysAddr); impl Debug; pub page_number, set_page_number: BITFIELD_PAGE_WIDTH - 1, 0; + // these flags are used by the live page table; they are ignored by FastSpace pub clean, set_clean: BITFIELD_PAGE_WIDTH + 0; pub valid, set_valid: BITFIELD_PAGE_WIDTH + 1; // these are only used by the FastSpace mechanism; they have no meaning in other contexts - pub space_state, set_space_state: BITFIELD_PAGE_WIDTH + 3, BITFIELD_PAGE_WIDTH + 2; + pub u8, from into SpaceState, space_state, set_space_state: BITFIELD_PAGE_WIDTH + 3, BITFIELD_PAGE_WIDTH + 2; }