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 / block.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 | //! Minimal block device interface shared by storage drivers. |
| 17 | |
| 18 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 19 | pub enum BlockError { |
| 20 | InvalidInput, |
| 21 | OutOfRange, |
| 22 | DeviceError, |
| 23 | Timeout, |
| 24 | } |
| 25 | |
| 26 | pub trait BlockDevice { |
| 27 | fn block_size(&self) -> u32; |
| 28 | fn block_count(&self) -> u64; |
| 29 | fn read_blocks(&mut self, lba: u64, buffer: &mut [u8]) -> Result<(), BlockError>; |
| 30 | fn write_blocks(&mut self, lba: u64, buffer: &[u8]) -> Result<(), BlockError>; |
| 31 | fn flush(&mut self) -> Result<(), BlockError>; |
| 32 | } |
| 33 | |
| 34 | extern crate alloc; |
| 35 | use alloc::vec; |
| 36 | use alloc::vec::Vec; |
| 37 | |
| 38 | /// Volatile memory disk. `fail_after` simulates a torn write by persisting only |
| 39 | /// the permitted prefix of each write, then returning an I/O error. |
| 40 | #[derive(Clone)] |
| 41 | pub struct RamDisk { |
| 42 | bytes: Vec<u8>, |
| 43 | block_size: usize, |
| 44 | fail_after: Option<usize>, |
| 45 | fail_write: Option<(usize, usize)>, |
| 46 | write_count: usize, |
| 47 | failed: bool, |
| 48 | } |
| 49 | |
| 50 | impl RamDisk { |
| 51 | pub fn new(block_size: usize, block_count: usize) -> Result<Self, BlockError> { |
| 52 | if block_size < 64 || block_size > u32::MAX as usize || block_count < 4 { |
| 53 | return Err(BlockError::InvalidInput); |
| 54 | } |
| 55 | Ok(Self { |
| 56 | bytes: vec![ |
| 57 | 0; |
| 58 | block_size |
| 59 | .checked_mul(block_count) |
| 60 | .ok_or(BlockError::InvalidInput)? |
| 61 | ], |
| 62 | block_size, |
| 63 | fail_after: None, |
| 64 | fail_write: None, |
| 65 | write_count: 0, |
| 66 | failed: false, |
| 67 | }) |
| 68 | } |
| 69 | pub fn fail_after_blocks(&mut self, count: Option<usize>) { |
| 70 | self.fail_after = count; |
| 71 | self.fail_write = None; |
| 72 | self.write_count = 0; |
| 73 | self.failed = false; |
| 74 | } |
| 75 | /// Tear the selected write after persisting `bytes` of its input. |
| 76 | /// Every write from that point onward fails. |
| 77 | pub fn tear_write(&mut self, write_number: usize, bytes: usize) { |
| 78 | self.fail_after = None; |
| 79 | self.fail_write = Some((write_number.max(1), bytes)); |
| 80 | self.write_count = 0; |
| 81 | self.failed = false; |
| 82 | } |
| 83 | pub fn corrupt_byte(&mut self, offset: usize, value: u8) -> Result<(), BlockError> { |
| 84 | let byte = self.bytes.get_mut(offset).ok_or(BlockError::OutOfRange)?; |
| 85 | *byte = value; |
| 86 | Ok(()) |
| 87 | } |
| 88 | fn range(&self, lba: u64, len: usize) -> Result<core::ops::Range<usize>, BlockError> { |
| 89 | if !len.is_multiple_of(self.block_size) { |
| 90 | return Err(BlockError::InvalidInput); |
| 91 | } |
| 92 | let start = usize::try_from(lba) |
| 93 | .ok() |
| 94 | .and_then(|n| n.checked_mul(self.block_size)) |
| 95 | .ok_or(BlockError::OutOfRange)?; |
| 96 | let end = start.checked_add(len).ok_or(BlockError::OutOfRange)?; |
| 97 | if end > self.bytes.len() { |
| 98 | return Err(BlockError::OutOfRange); |
| 99 | } |
| 100 | Ok(start..end) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | impl BlockDevice for RamDisk { |
| 105 | fn block_size(&self) -> u32 { |
| 106 | self.block_size as u32 |
| 107 | } |
| 108 | fn block_count(&self) -> u64 { |
| 109 | (self.bytes.len() / self.block_size) as u64 |
| 110 | } |
| 111 | fn read_blocks(&mut self, lba: u64, out: &mut [u8]) -> Result<(), BlockError> { |
| 112 | let r = self.range(lba, out.len())?; |
| 113 | out.copy_from_slice(&self.bytes[r]); |
| 114 | Ok(()) |
| 115 | } |
| 116 | fn write_blocks(&mut self, lba: u64, data: &[u8]) -> Result<(), BlockError> { |
| 117 | let r = self.range(lba, data.len())?; |
| 118 | if self.failed { |
| 119 | return Err(BlockError::DeviceError); |
| 120 | } |
| 121 | if let Some(remaining) = self.fail_after { |
| 122 | let block_count = data.len() / self.block_size; |
| 123 | let allowed = remaining.min(block_count) * self.block_size; |
| 124 | self.bytes[r.start..r.start + allowed].copy_from_slice(&data[..allowed]); |
| 125 | if remaining < block_count { |
| 126 | self.fail_after = Some(0); |
| 127 | return Err(BlockError::DeviceError); |
| 128 | } |
| 129 | self.fail_after = Some(remaining - block_count); |
| 130 | return Ok(()); |
| 131 | } |
| 132 | self.write_count += 1; |
| 133 | if let Some((write_number, bytes)) = self.fail_write { |
| 134 | if self.write_count == write_number { |
| 135 | let allowed = bytes.min(data.len()); |
| 136 | self.bytes[r.start..r.start + allowed].copy_from_slice(&data[..allowed]); |
| 137 | self.failed = true; |
| 138 | return Err(BlockError::DeviceError); |
| 139 | } |
| 140 | } |
| 141 | self.bytes[r].copy_from_slice(data); |
| 142 | Ok(()) |
| 143 | } |
| 144 | fn flush(&mut self) -> Result<(), BlockError> { |
| 145 | Ok(()) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | #[cfg(test)] |
| 150 | mod tests { |
| 151 | use super::*; |
| 152 | |
| 153 | #[test] |
| 154 | fn ram_disk_rejects_block_size_that_would_truncate() { |
| 155 | if usize::BITS > 32 { |
| 156 | assert!(matches!( |
| 157 | RamDisk::new(u32::MAX as usize + 1, 4), |
| 158 | Err(BlockError::InvalidInput) |
| 159 | )); |
| 160 | } |
| 161 | } |
| 162 | } |