aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-kernel / src / store / device.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! ADR 0015 geometry adapter for StoreDevice. |
| 17 | //! |
| 18 | //! Maps an underlying `BlockDevice` with a 512-byte or 4096-byte logical block size |
| 19 | //! to sovereign 4096-byte Store units. Rejects all other block sizes and forbids |
| 20 | //! read-modify-write emulation. |
| 21 | |
| 22 | use crate::block::{BlockDevice, BlockError}; |
| 23 | pub use crate::store::engine::StoreDevice; |
| 24 | pub use crate::store::v1::STORE_UNIT_BYTES; |
| 25 | |
| 26 | pub const STORE_UNIT_BLOCKS_512: u64 = 8; |
| 27 | pub const STORE_UNIT_BLOCKS_4096: u64 = 1; |
| 28 | |
| 29 | /// Errors returned by [`StoreDeviceAdapter`]. |
| 30 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 31 | pub enum StoreDeviceError { |
| 32 | /// Device has an unsupported logical block size. |
| 33 | UnsupportedBlockSize(u32), |
| 34 | /// Requested unit index or range is outside the provisioned region. |
| 35 | OutOfBounds, |
| 36 | /// Buffer length is not an exact multiple of the Store unit size (4096 bytes). |
| 37 | InvalidBufferSize(usize), |
| 38 | /// Calculation resulted in arithmetic overflow. |
| 39 | UnitOverflow, |
| 40 | /// Underlying block device error. |
| 41 | Block(BlockError), |
| 42 | } |
| 43 | |
| 44 | impl core::fmt::Display for StoreDeviceError { |
| 45 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 46 | match self { |
| 47 | Self::UnsupportedBlockSize(bs) => write!(f, "unsupported block size: {bs}"), |
| 48 | Self::OutOfBounds => write!(f, "unit index out of bounds"), |
| 49 | Self::InvalidBufferSize(len) => { |
| 50 | write!( |
| 51 | f, |
| 52 | "invalid buffer size {len}, must be multiple of {STORE_UNIT_BYTES}" |
| 53 | ) |
| 54 | } |
| 55 | Self::UnitOverflow => write!(f, "unit arithmetic overflow"), |
| 56 | Self::Block(err) => write!(f, "block device error: {err:?}"), |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl From<BlockError> for StoreDeviceError { |
| 62 | fn from(err: BlockError) -> Self { |
| 63 | Self::Block(err) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /// Geometry adapter exposing 4096-byte Store units over an underlying [`BlockDevice`]. |
| 68 | #[derive(Clone, Debug)] |
| 69 | pub struct StoreDeviceAdapter<D> { |
| 70 | device: D, |
| 71 | block_size: u32, |
| 72 | blocks_per_unit: u64, |
| 73 | region_units: u64, |
| 74 | } |
| 75 | |
| 76 | impl<D: BlockDevice> StoreDeviceAdapter<D> { |
| 77 | /// Constructs a new adapter over `device`. |
| 78 | /// |
| 79 | /// Validates that the device logical block size is exactly 512 or 4096 bytes. |
| 80 | /// Otherwise returns [`StoreDeviceError::UnsupportedBlockSize`]. |
| 81 | pub fn new(device: D) -> Result<Self, StoreDeviceError> { |
| 82 | let block_size = device.block_size(); |
| 83 | let blocks_per_unit = match block_size { |
| 84 | 512 => STORE_UNIT_BLOCKS_512, |
| 85 | 4096 => STORE_UNIT_BLOCKS_4096, |
| 86 | other => return Err(StoreDeviceError::UnsupportedBlockSize(other)), |
| 87 | }; |
| 88 | let region_units = device.block_count() / blocks_per_unit; |
| 89 | Ok(Self { |
| 90 | device, |
| 91 | block_size, |
| 92 | blocks_per_unit, |
| 93 | region_units, |
| 94 | }) |
| 95 | } |
| 96 | |
| 97 | /// Number of full 4096-byte Store units in this region. |
| 98 | #[inline] |
| 99 | pub fn region_units(&self) -> u64 { |
| 100 | self.region_units |
| 101 | } |
| 102 | |
| 103 | /// Underlying device logical block size (512 or 4096). |
| 104 | #[inline] |
| 105 | pub fn block_size(&self) -> u32 { |
| 106 | self.block_size |
| 107 | } |
| 108 | |
| 109 | /// Reads exactly one 4096-byte Store unit. |
| 110 | pub fn read_unit( |
| 111 | &mut self, |
| 112 | unit: u64, |
| 113 | out: &mut [u8; STORE_UNIT_BYTES], |
| 114 | ) -> Result<(), StoreDeviceError> { |
| 115 | self.read_units(unit, out) |
| 116 | } |
| 117 | |
| 118 | /// Writes exactly one 4096-byte Store unit. |
| 119 | pub fn write_unit( |
| 120 | &mut self, |
| 121 | unit: u64, |
| 122 | bytes: &[u8; STORE_UNIT_BYTES], |
| 123 | ) -> Result<(), StoreDeviceError> { |
| 124 | self.write_units(unit, bytes) |
| 125 | } |
| 126 | |
| 127 | /// Reads one or more contiguous 4096-byte Store units. |
| 128 | /// |
| 129 | /// Buffer length must be a multiple of 4096 bytes. |
| 130 | pub fn read_units(&mut self, start_unit: u64, out: &mut [u8]) -> Result<(), StoreDeviceError> { |
| 131 | if !out.len().is_multiple_of(STORE_UNIT_BYTES) { |
| 132 | return Err(StoreDeviceError::InvalidBufferSize(out.len())); |
| 133 | } |
| 134 | let count = (out.len() / STORE_UNIT_BYTES) as u64; |
| 135 | let end_unit = start_unit |
| 136 | .checked_add(count) |
| 137 | .ok_or(StoreDeviceError::UnitOverflow)?; |
| 138 | if end_unit > self.region_units { |
| 139 | return Err(StoreDeviceError::OutOfBounds); |
| 140 | } |
| 141 | if count == 0 { |
| 142 | return Ok(()); |
| 143 | } |
| 144 | let start_lba = start_unit |
| 145 | .checked_mul(self.blocks_per_unit) |
| 146 | .ok_or(StoreDeviceError::UnitOverflow)?; |
| 147 | self.device.read_blocks(start_lba, out)?; |
| 148 | Ok(()) |
| 149 | } |
| 150 | |
| 151 | /// Writes one or more contiguous 4096-byte Store units. |
| 152 | /// |
| 153 | /// Buffer length must be a multiple of 4096 bytes. |
| 154 | pub fn write_units(&mut self, start_unit: u64, bytes: &[u8]) -> Result<(), StoreDeviceError> { |
| 155 | if !bytes.len().is_multiple_of(STORE_UNIT_BYTES) { |
| 156 | return Err(StoreDeviceError::InvalidBufferSize(bytes.len())); |
| 157 | } |
| 158 | let count = (bytes.len() / STORE_UNIT_BYTES) as u64; |
| 159 | let end_unit = start_unit |
| 160 | .checked_add(count) |
| 161 | .ok_or(StoreDeviceError::UnitOverflow)?; |
| 162 | if end_unit > self.region_units { |
| 163 | return Err(StoreDeviceError::OutOfBounds); |
| 164 | } |
| 165 | if count == 0 { |
| 166 | return Ok(()); |
| 167 | } |
| 168 | let start_lba = start_unit |
| 169 | .checked_mul(self.blocks_per_unit) |
| 170 | .ok_or(StoreDeviceError::UnitOverflow)?; |
| 171 | self.device.write_blocks(start_lba, bytes)?; |
| 172 | Ok(()) |
| 173 | } |
| 174 | |
| 175 | /// Flushes dirty blocks to persistent storage. |
| 176 | pub fn flush(&mut self) -> Result<(), StoreDeviceError> { |
| 177 | self.device.flush().map_err(StoreDeviceError::Block) |
| 178 | } |
| 179 | |
| 180 | /// Consumes the adapter and returns the underlying block device. |
| 181 | pub fn into_inner(self) -> D { |
| 182 | self.device |
| 183 | } |
| 184 | |
| 185 | /// Borrows the underlying block device. |
| 186 | pub fn inner(&self) -> &D { |
| 187 | &self.device |
| 188 | } |
| 189 | |
| 190 | /// Mutably borrows the underlying block device. |
| 191 | pub fn inner_mut(&mut self) -> &mut D { |
| 192 | &mut self.device |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | impl<D: BlockDevice> StoreDevice for StoreDeviceAdapter<D> { |
| 197 | type Error = StoreDeviceError; |
| 198 | |
| 199 | fn region_units(&self) -> u64 { |
| 200 | self.region_units() |
| 201 | } |
| 202 | |
| 203 | fn read_unit( |
| 204 | &mut self, |
| 205 | unit: u64, |
| 206 | out: &mut [u8; STORE_UNIT_BYTES], |
| 207 | ) -> Result<(), Self::Error> { |
| 208 | self.read_unit(unit, out) |
| 209 | } |
| 210 | |
| 211 | fn write_unit(&mut self, unit: u64, bytes: &[u8; STORE_UNIT_BYTES]) -> Result<(), Self::Error> { |
| 212 | self.write_unit(unit, bytes) |
| 213 | } |
| 214 | |
| 215 | fn flush(&mut self) -> Result<(), Self::Error> { |
| 216 | self.flush() |
| 217 | } |
| 218 | } |