//! Read-only ownership decisions using the existing bounded P12 disk parser. //! Unknown allocated blocks are retained. This module has no repair/free API. use turkos_kernel::{journal_disk::{self, Block, BlockIo, Mount, Ownership}, turkfs}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Error { Disk(journal_disk::Error), Scope, PendingJournal, KnownBlock, Unallocated } impl From for Error { fn from(error: journal_disk::Error) -> Self { Self::Disk(error) } } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Retained { pub lba: u64, pub sha256: [u8;32], } pub struct Scan { capacity: u64, unknown: Block, bitmap: Block, pub inode: Block, pub ownership: Ownership, } impl Scan { pub fn open(io: &mut impl BlockIo, capacity: u64) -> Result { if !(32..=4096).contains(&capacity) { return Err(Error::Scope); } let mount = Mount::open(io, capacity)?; let plan = mount.plan(io)?; // This read-only decision is restricted to a quiescent empty journal. // Pending replay is reported to the caller, never repaired here. if plan.empty != 2 || plan.incomplete != 0 || plan.committed != 0 || plan.bitmap_changes != 0 { return Err(Error::PendingJournal); } Ok(Self {capacity, unknown:plan.unknown, bitmap:plan.bitmap, inode:plan.inode, ownership:plan.ownership}) } pub const fn bitmap(&self) -> &Block { &self.bitmap } pub fn retain_unknown(&self, io: &mut impl BlockIo, lba: u64) -> Result { if lba >= self.capacity { return Err(Error::Scope); } if !journal_disk::allocated(&self.bitmap, lba) { return Err(Error::Unallocated); } if !journal_disk::allocated(&self.unknown, lba) { return Err(Error::KnownBlock); } let mut bytes = [0;512]; io.read(lba, &mut bytes)?; Ok(Retained {lba,sha256:turkfs::file_sha256(&bytes)}) } }