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 / nvme / atomicity.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 | //! NVMe power-fail atomic-write qualification for the System Store root write. |
| 17 | //! |
| 18 | //! NVMe reports power-fail atomic write units as **0-based** logical-block |
| 19 | //! counts (decoded size = raw + 1). The effective guarantee for a namespace is |
| 20 | //! the namespace value (`NAWUPF`) when the namespace advertises one, otherwise |
| 21 | //! the controller value (`AWUPF`). A write is power-fail atomic only if it fits |
| 22 | //! entirely inside that unit AND does not cross an atomic boundary |
| 23 | //! (`NABSPF`/`NABO`). If the guarantee cannot be established, qualification |
| 24 | //! fails closed. |
| 25 | //! |
| 26 | //! Field byte offsets follow NVMe 1.4 Identify Controller (Figure 249) and |
| 27 | //! Identify Namespace (Figure 247). |
| 28 | |
| 29 | /// Identify Controller AWUPF byte offset (u16 LE, 0-based). |
| 30 | pub const ID_CTRL_AWUPF_OFFSET: usize = 528; |
| 31 | /// Identify Namespace NAWUPF byte offset (u16 LE, 0-based). |
| 32 | pub const ID_NS_NAWUPF_OFFSET: usize = 36; |
| 33 | /// Identify Namespace NABO byte offset (u16 LE, blocks). |
| 34 | pub const ID_NS_NABO_OFFSET: usize = 42; |
| 35 | /// Identify Namespace NABSPF byte offset (u16 LE, 0-based blocks). |
| 36 | pub const ID_NS_NABSPF_OFFSET: usize = 44; |
| 37 | /// Permanent System Store unit size the root activation write must cover. |
| 38 | pub const STORE_UNIT_BYTES: u64 = 4096; |
| 39 | |
| 40 | /// Read a little-endian u16 at `offset`, or `None` if the page is too short. |
| 41 | pub fn read_u16_le(bytes: &[u8], offset: usize) -> Option<u16> { |
| 42 | let end = offset.checked_add(2)?; |
| 43 | let slice = bytes.get(offset..end)?; |
| 44 | Some(u16::from_le_bytes([slice[0], slice[1]])) |
| 45 | } |
| 46 | |
| 47 | /// Decoded atomicity fields for one namespace. |
| 48 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 49 | pub struct AtomicityFields { |
| 50 | pub lba_bytes: u32, |
| 51 | /// Controller AWUPF raw value (0-based); `None` if not observed. |
| 52 | pub awupf_raw: Option<u16>, |
| 53 | /// Namespace NAWUPF raw value (0-based); `Some(0)`/`None` means the |
| 54 | /// namespace does not advertise its own power-fail unit. |
| 55 | pub nawupf_raw: Option<u16>, |
| 56 | /// Namespace atomic boundary size raw value (0-based blocks); 0 = none. |
| 57 | pub nabspf_raw: Option<u16>, |
| 58 | /// Namespace atomic boundary offset in logical blocks. |
| 59 | pub nabo_blocks: u16, |
| 60 | } |
| 61 | |
| 62 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 63 | pub enum AtomicityRefusal { |
| 64 | EmptyWrite, |
| 65 | ExceedsAtomicUnit { |
| 66 | required_blocks: u64, |
| 67 | guaranteed_blocks: u64, |
| 68 | }, |
| 69 | CrossesAtomicBoundary { |
| 70 | boundary_blocks: u64, |
| 71 | start_lba: u64, |
| 72 | lba_count: u64, |
| 73 | }, |
| 74 | } |
| 75 | |
| 76 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 77 | pub enum AtomicityUnknown { |
| 78 | NoPowerFailGuaranteeReported, |
| 79 | UnsupportedGeometry, |
| 80 | } |
| 81 | |
| 82 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 83 | pub enum AtomicityDecision { |
| 84 | Atomic { effective_blocks: u64 }, |
| 85 | NotAtomic(AtomicityRefusal), |
| 86 | Unknown(AtomicityUnknown), |
| 87 | } |
| 88 | |
| 89 | impl AtomicityFields { |
| 90 | /// Effective guaranteed power-fail atomic size in logical blocks. |
| 91 | pub fn effective_power_fail_blocks(&self) -> Option<u64> { |
| 92 | match self.nawupf_raw { |
| 93 | Some(raw) if raw > 0 => Some(u64::from(raw) + 1), |
| 94 | _ => self.awupf_raw.map(|raw| u64::from(raw) + 1), |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /// Atomic boundary size in logical blocks, if the namespace reports one. |
| 99 | pub fn boundary_blocks(&self) -> Option<u64> { |
| 100 | self.nabspf_raw |
| 101 | .filter(|raw| *raw > 0) |
| 102 | .map(|raw| u64::from(raw) + 1) |
| 103 | } |
| 104 | |
| 105 | fn region(&self, block: u64, size: u64) -> i128 { |
| 106 | let offset = i128::from(self.nabo_blocks % size as u16); |
| 107 | (i128::from(block) - offset).div_euclid(i128::from(size)) |
| 108 | } |
| 109 | |
| 110 | /// Deterministic power-fail atomicity predicate. |
| 111 | pub fn write_is_power_fail_atomic(&self, start_lba: u64, lba_count: u64) -> AtomicityDecision { |
| 112 | if lba_count == 0 { |
| 113 | return AtomicityDecision::NotAtomic(AtomicityRefusal::EmptyWrite); |
| 114 | } |
| 115 | let Some(guaranteed) = self.effective_power_fail_blocks() else { |
| 116 | return AtomicityDecision::Unknown(AtomicityUnknown::NoPowerFailGuaranteeReported); |
| 117 | }; |
| 118 | if lba_count > guaranteed { |
| 119 | return AtomicityDecision::NotAtomic(AtomicityRefusal::ExceedsAtomicUnit { |
| 120 | required_blocks: lba_count, |
| 121 | guaranteed_blocks: guaranteed, |
| 122 | }); |
| 123 | } |
| 124 | if let Some(size) = self.boundary_blocks() { |
| 125 | if self.region(start_lba, size) != self.region(start_lba + lba_count - 1, size) { |
| 126 | return AtomicityDecision::NotAtomic(AtomicityRefusal::CrossesAtomicBoundary { |
| 127 | boundary_blocks: size, |
| 128 | start_lba, |
| 129 | lba_count, |
| 130 | }); |
| 131 | } |
| 132 | } |
| 133 | AtomicityDecision::Atomic { |
| 134 | effective_blocks: guaranteed, |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /// (start LBA, LBA count) of a 4096-byte Store superblock unit. |
| 139 | pub fn store_unit_lba(&self, slot: u32) -> Option<(u64, u64)> { |
| 140 | let block_size = u64::from(self.lba_bytes); |
| 141 | if block_size == 0 || !STORE_UNIT_BYTES.is_multiple_of(block_size) { |
| 142 | return None; |
| 143 | } |
| 144 | let blocks = STORE_UNIT_BYTES / block_size; |
| 145 | Some((u64::from(slot).checked_mul(blocks)?, blocks)) |
| 146 | } |
| 147 | |
| 148 | /// Qualification decision for one Store superblock slot (0 = A, 1 = B). |
| 149 | pub fn store_root_decision(&self, slot: u32) -> AtomicityDecision { |
| 150 | match self.store_unit_lba(slot) { |
| 151 | Some((start, count)) => self.write_is_power_fail_atomic(start, count), |
| 152 | None => AtomicityDecision::Unknown(AtomicityUnknown::UnsupportedGeometry), |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | #[cfg(test)] |
| 158 | mod tests { |
| 159 | use super::*; |
| 160 | |
| 161 | fn fields( |
| 162 | lba_bytes: u32, |
| 163 | awupf_raw: Option<u16>, |
| 164 | nawupf_raw: Option<u16>, |
| 165 | nabspf_raw: Option<u16>, |
| 166 | nabo_blocks: u16, |
| 167 | ) -> AtomicityFields { |
| 168 | AtomicityFields { |
| 169 | lba_bytes, |
| 170 | awupf_raw, |
| 171 | nawupf_raw, |
| 172 | nabspf_raw, |
| 173 | nabo_blocks, |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn insufficient_controller_awupf_rejects() { |
| 179 | let f = fields(512, Some(6), None, None, 0); // decoded 7 blocks < 8 |
| 180 | assert_eq!( |
| 181 | f.write_is_power_fail_atomic(0, 8), |
| 182 | AtomicityDecision::NotAtomic(AtomicityRefusal::ExceedsAtomicUnit { |
| 183 | required_blocks: 8, |
| 184 | guaranteed_blocks: 7, |
| 185 | }) |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | #[test] |
| 190 | fn sufficient_decoded_awupf_accepts() { |
| 191 | let f = fields(512, Some(7), None, None, 0); // decoded 8 blocks |
| 192 | assert_eq!( |
| 193 | f.write_is_power_fail_atomic(0, 8), |
| 194 | AtomicityDecision::Atomic { |
| 195 | effective_blocks: 8 |
| 196 | } |
| 197 | ); |
| 198 | } |
| 199 | |
| 200 | #[test] |
| 201 | fn namespace_override_wins_over_controller() { |
| 202 | // NAWUPF 3h -> 4 blocks; controller AWUPF 15h -> 16 blocks. The |
| 203 | // namespace value governs, so an 8-block write is not atomic. |
| 204 | let f = fields(512, Some(15), Some(3), None, 0); |
| 205 | assert_eq!(f.effective_power_fail_blocks(), Some(4)); |
| 206 | assert_eq!( |
| 207 | f.write_is_power_fail_atomic(0, 8), |
| 208 | AtomicityDecision::NotAtomic(AtomicityRefusal::ExceedsAtomicUnit { |
| 209 | required_blocks: 8, |
| 210 | guaranteed_blocks: 4, |
| 211 | }) |
| 212 | ); |
| 213 | } |
| 214 | |
| 215 | #[test] |
| 216 | fn namespace_zero_falls_back_to_controller() { |
| 217 | // NVMe 1.4: NAWUPF 0h means the controller AWUPF applies. |
| 218 | let f = fields(512, Some(7), Some(0), None, 0); |
| 219 | assert_eq!(f.effective_power_fail_blocks(), Some(8)); |
| 220 | assert!(matches!( |
| 221 | f.write_is_power_fail_atomic(0, 8), |
| 222 | AtomicityDecision::Atomic { .. } |
| 223 | )); |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn controller_fallback_when_namespace_absent() { |
| 228 | let f = fields(512, Some(7), None, None, 0); |
| 229 | assert!(matches!( |
| 230 | f.write_is_power_fail_atomic(0, 8), |
| 231 | AtomicityDecision::Atomic { .. } |
| 232 | )); |
| 233 | } |
| 234 | |
| 235 | #[test] |
| 236 | fn sufficient_unit_but_crosses_boundary_rejects() { |
| 237 | let f = fields(512, Some(15), None, Some(3), 0); // boundary size 4 |
| 238 | assert_eq!( |
| 239 | f.write_is_power_fail_atomic(0, 8), |
| 240 | AtomicityDecision::NotAtomic(AtomicityRefusal::CrossesAtomicBoundary { |
| 241 | boundary_blocks: 4, |
| 242 | start_lba: 0, |
| 243 | lba_count: 8, |
| 244 | }) |
| 245 | ); |
| 246 | // A write inside one boundary region is fine. |
| 247 | assert!(matches!( |
| 248 | f.write_is_power_fail_atomic(0, 4), |
| 249 | AtomicityDecision::Atomic { .. } |
| 250 | )); |
| 251 | } |
| 252 | |
| 253 | #[test] |
| 254 | fn boundary_safe_write_accepts() { |
| 255 | let f = fields(512, Some(7), None, None, 0); |
| 256 | assert!(matches!( |
| 257 | f.write_is_power_fail_atomic(8, 8), |
| 258 | AtomicityDecision::Atomic { .. } |
| 259 | )); |
| 260 | } |
| 261 | |
| 262 | #[test] |
| 263 | fn boundary_offset_is_respected() { |
| 264 | // size 8, offset 4: [0,8) spans regions -1 and 0 -> crosses. |
| 265 | let f = fields(512, Some(15), None, Some(7), 4); |
| 266 | assert!(matches!( |
| 267 | f.write_is_power_fail_atomic(0, 8), |
| 268 | AtomicityDecision::NotAtomic(AtomicityRefusal::CrossesAtomicBoundary { .. }) |
| 269 | )); |
| 270 | // [4,12) lies wholly in region 0 -> atomic. |
| 271 | assert!(matches!( |
| 272 | f.write_is_power_fail_atomic(4, 8), |
| 273 | AtomicityDecision::Atomic { .. } |
| 274 | )); |
| 275 | } |
| 276 | |
| 277 | #[test] |
| 278 | fn store_root_512_geometry() { |
| 279 | let f = fields(512, Some(7), None, None, 0); |
| 280 | assert_eq!(f.store_unit_lba(0), Some((0, 8))); |
| 281 | assert_eq!(f.store_unit_lba(1), Some((8, 8))); |
| 282 | assert!(matches!( |
| 283 | f.store_root_decision(0), |
| 284 | AtomicityDecision::Atomic { .. } |
| 285 | )); |
| 286 | assert!(matches!( |
| 287 | f.store_root_decision(1), |
| 288 | AtomicityDecision::Atomic { .. } |
| 289 | )); |
| 290 | } |
| 291 | |
| 292 | #[test] |
| 293 | fn store_root_4096_geometry() { |
| 294 | let f = fields(4096, Some(0), None, None, 0); // 1 block |
| 295 | assert_eq!(f.store_unit_lba(0), Some((0, 1))); |
| 296 | assert_eq!(f.store_unit_lba(1), Some((1, 1))); |
| 297 | assert!(matches!( |
| 298 | f.store_root_decision(1), |
| 299 | AtomicityDecision::Atomic { .. } |
| 300 | )); |
| 301 | } |
| 302 | |
| 303 | #[test] |
| 304 | fn encoding_edge_zero_and_max() { |
| 305 | assert_eq!( |
| 306 | fields(512, Some(0), None, None, 0).effective_power_fail_blocks(), |
| 307 | Some(1) |
| 308 | ); |
| 309 | assert_eq!( |
| 310 | fields(512, Some(u16::MAX), None, None, 0).effective_power_fail_blocks(), |
| 311 | Some(65536) |
| 312 | ); |
| 313 | } |
| 314 | |
| 315 | #[test] |
| 316 | fn unknown_state_fails_closed() { |
| 317 | let f = fields(512, None, None, None, 0); |
| 318 | assert_eq!( |
| 319 | f.write_is_power_fail_atomic(0, 8), |
| 320 | AtomicityDecision::Unknown(AtomicityUnknown::NoPowerFailGuaranteeReported) |
| 321 | ); |
| 322 | } |
| 323 | |
| 324 | #[test] |
| 325 | fn unsupported_geometry_fails_closed() { |
| 326 | let f = fields(600, Some(15), None, None, 0); |
| 327 | assert_eq!( |
| 328 | f.store_root_decision(0), |
| 329 | AtomicityDecision::Unknown(AtomicityUnknown::UnsupportedGeometry) |
| 330 | ); |
| 331 | } |
| 332 | |
| 333 | #[test] |
| 334 | fn empty_write_rejects() { |
| 335 | let f = fields(512, Some(15), None, None, 0); |
| 336 | assert_eq!( |
| 337 | f.write_is_power_fail_atomic(0, 0), |
| 338 | AtomicityDecision::NotAtomic(AtomicityRefusal::EmptyWrite) |
| 339 | ); |
| 340 | } |
| 341 | |
| 342 | #[test] |
| 343 | fn read_u16_le_bounds() { |
| 344 | let bytes = [0x07u8, 0x00, 0xff]; |
| 345 | assert_eq!(read_u16_le(&bytes, 0), Some(7)); |
| 346 | assert_eq!(read_u16_le(&bytes, 2), None); |
| 347 | assert_eq!(read_u16_le(&bytes, 99), None); |
| 348 | } |
| 349 | } |