1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Error types for Pepita kernel interfaces.
//!
//! This module provides error handling following the Iron Lotus principle
//! of explicit error handling - no panics in normal operation paths.
use core::fmt;
/// Result type alias for Pepita operations.
pub type Result<T> = core::result::Result<T, KernelError>;
/// Kernel error enumeration.
///
/// Represents all possible error conditions in Pepita kernel operations.
/// Each variant maps to a specific failure mode with clear semantics.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum KernelError {
// ========================================================================
// Memory Errors
// ========================================================================
/// Out of memory - allocation failed
OutOfMemory,
/// Invalid memory address provided
InvalidAddress,
/// Address alignment violation
AlignmentError,
/// Memory region overlap detected
OverlappingRegion,
// ========================================================================
// I/O Errors
// ========================================================================
/// I/O operation timed out
IoTimeout,
/// Device not ready for operation
DeviceNotReady,
/// Invalid I/O request parameters
InvalidRequest,
/// I/O operation was cancelled
Cancelled,
// ========================================================================
// ublk Errors
// ========================================================================
/// ublk request queue is full
UblkQueueFull,
/// Invalid ublk tag
UblkInvalidTag,
/// ublk device is busy
UblkDeviceBusy,
/// ublk device not found
UblkDeviceNotFound,
/// Invalid ublk device ID
UblkInvalidDeviceId,
/// ublk operation not permitted
UblkNotPermitted,
// ========================================================================
// io_uring Errors
// ========================================================================
/// io_uring submission queue full
IoUringSubmitFull,
/// io_uring completion queue overflow
IoUringCqOverflow,
/// Invalid io_uring opcode
IoUringInvalidOpcode,
// ========================================================================
// Block Layer Errors
// ========================================================================
/// Block device error
BlockError,
/// No tags available for request
NoTagsAvailable,
/// Invalid queue ID
InvalidQueueId,
// ========================================================================
// Generic Errors
// ========================================================================
/// Operation not supported
NotSupported,
/// Invalid argument
InvalidArgument,
/// Resource temporarily unavailable
WouldBlock,
/// Operation interrupted
Interrupted,
/// Resource is busy (e.g., lock contention)
ResourceBusy,
}
impl KernelError {
/// Convert error to POSIX-compatible errno value.
///
/// Returns negative errno values as per Linux kernel convention.
#[must_use]
pub const fn to_errno(self) -> i32 {
match self {
Self::OutOfMemory => -12, // ENOMEM
Self::InvalidAddress => -14, // EFAULT
Self::AlignmentError => -22, // EINVAL
Self::OverlappingRegion => -22, // EINVAL
Self::IoTimeout => -110, // ETIMEDOUT
Self::DeviceNotReady => -19, // ENODEV
Self::InvalidRequest => -22, // EINVAL
Self::Cancelled => -125, // ECANCELED
Self::UblkQueueFull => -11, // EAGAIN
Self::UblkInvalidTag => -22, // EINVAL
Self::UblkDeviceBusy => -16, // EBUSY
Self::UblkDeviceNotFound => -19, // ENODEV
Self::UblkInvalidDeviceId => -22, // EINVAL
Self::UblkNotPermitted => -1, // EPERM
Self::IoUringSubmitFull => -11, // EAGAIN
Self::IoUringCqOverflow => -75, // EOVERFLOW
Self::IoUringInvalidOpcode => -22, // EINVAL
Self::BlockError => -5, // EIO
Self::NoTagsAvailable => -11, // EAGAIN
Self::InvalidQueueId => -22, // EINVAL
Self::NotSupported => -95, // EOPNOTSUPP
Self::InvalidArgument => -22, // EINVAL
Self::WouldBlock => -11, // EAGAIN
Self::Interrupted => -4, // EINTR
Self::ResourceBusy => -16, // EBUSY
}
}
/// Create error from POSIX errno value.
///
/// # Arguments
///
/// * `errno` - Negative errno value
///
/// # Returns
///
/// Corresponding `KernelError` variant, or `InvalidArgument` for unknown values.
#[must_use]
pub const fn from_errno(errno: i32) -> Self {
match errno {
-12 => Self::OutOfMemory,
-14 => Self::InvalidAddress,
-110 => Self::IoTimeout,
-19 => Self::DeviceNotReady,
-125 => Self::Cancelled,
-16 => Self::UblkDeviceBusy,
-1 => Self::UblkNotPermitted,
-75 => Self::IoUringCqOverflow,
-5 => Self::BlockError,
-95 => Self::NotSupported,
-4 => Self::Interrupted,
-11 => Self::WouldBlock,
_ => Self::InvalidArgument,
}
}
/// Check if error is retriable.
///
/// Returns `true` if the operation should be retried after a delay.
#[must_use]
pub const fn is_retriable(self) -> bool {
matches!(
self,
Self::WouldBlock
| Self::UblkQueueFull
| Self::IoUringSubmitFull
| Self::NoTagsAvailable
| Self::UblkDeviceBusy
)
}
/// Check if error is a resource exhaustion error.
#[must_use]
pub const fn is_resource_error(self) -> bool {
matches!(
self,
Self::OutOfMemory | Self::UblkQueueFull | Self::NoTagsAvailable
)
}
}
impl fmt::Display for KernelError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::OutOfMemory => write!(f, "out of memory"),
Self::InvalidAddress => write!(f, "invalid address"),
Self::AlignmentError => write!(f, "alignment error"),
Self::OverlappingRegion => write!(f, "overlapping memory region"),
Self::IoTimeout => write!(f, "I/O timeout"),
Self::DeviceNotReady => write!(f, "device not ready"),
Self::InvalidRequest => write!(f, "invalid request"),
Self::Cancelled => write!(f, "operation cancelled"),
Self::UblkQueueFull => write!(f, "ublk queue full"),
Self::UblkInvalidTag => write!(f, "invalid ublk tag"),
Self::UblkDeviceBusy => write!(f, "ublk device busy"),
Self::UblkDeviceNotFound => write!(f, "ublk device not found"),
Self::UblkInvalidDeviceId => write!(f, "invalid ublk device ID"),
Self::UblkNotPermitted => write!(f, "ublk operation not permitted"),
Self::IoUringSubmitFull => write!(f, "io_uring submission queue full"),
Self::IoUringCqOverflow => write!(f, "io_uring completion queue overflow"),
Self::IoUringInvalidOpcode => write!(f, "invalid io_uring opcode"),
Self::BlockError => write!(f, "block device error"),
Self::NoTagsAvailable => write!(f, "no tags available"),
Self::InvalidQueueId => write!(f, "invalid queue ID"),
Self::NotSupported => write!(f, "operation not supported"),
Self::InvalidArgument => write!(f, "invalid argument"),
Self::WouldBlock => write!(f, "operation would block"),
Self::Interrupted => write!(f, "operation interrupted"),
Self::ResourceBusy => write!(f, "resource is busy"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for KernelError {}
// ============================================================================
// TESTS (EXTREME TDD)
// ============================================================================
#[cfg(test)]
mod tests {
use super::*;
// ------------------------------------------------------------------------
// Errno Conversion Tests
// ------------------------------------------------------------------------
#[test]
fn test_errno_roundtrip_common_errors() {
let errors = [
KernelError::OutOfMemory,
KernelError::IoTimeout,
KernelError::DeviceNotReady,
KernelError::Cancelled,
KernelError::UblkDeviceBusy,
KernelError::IoUringCqOverflow,
KernelError::BlockError,
KernelError::NotSupported,
KernelError::Interrupted,
KernelError::WouldBlock,
];
for error in errors {
let errno = error.to_errno();
let recovered = KernelError::from_errno(errno);
// Note: Some errors share the same errno, so we check errno matches
assert_eq!(
recovered.to_errno(),
errno,
"errno roundtrip failed for {:?}",
error
);
}
}
#[test]
fn test_errno_values_are_negative() {
let errors = [
KernelError::OutOfMemory,
KernelError::InvalidAddress,
KernelError::IoTimeout,
KernelError::UblkQueueFull,
KernelError::IoUringSubmitFull,
KernelError::BlockError,
];
for error in errors {
assert!(
error.to_errno() < 0,
"errno for {:?} should be negative",
error
);
}
}
#[test]
fn test_errno_specific_values() {
// Verify specific errno mappings match POSIX
assert_eq!(KernelError::OutOfMemory.to_errno(), -12); // ENOMEM
assert_eq!(KernelError::InvalidAddress.to_errno(), -14); // EFAULT
assert_eq!(KernelError::IoTimeout.to_errno(), -110); // ETIMEDOUT
assert_eq!(KernelError::UblkDeviceBusy.to_errno(), -16); // EBUSY
assert_eq!(KernelError::NotSupported.to_errno(), -95); // EOPNOTSUPP
}
#[test]
fn test_unknown_errno_returns_invalid_argument() {
assert_eq!(KernelError::from_errno(-9999), KernelError::InvalidArgument);
assert_eq!(KernelError::from_errno(0), KernelError::InvalidArgument);
assert_eq!(KernelError::from_errno(100), KernelError::InvalidArgument);
}
// ------------------------------------------------------------------------
// Retriable Error Tests
// ------------------------------------------------------------------------
#[test]
fn test_retriable_errors() {
assert!(KernelError::WouldBlock.is_retriable());
assert!(KernelError::UblkQueueFull.is_retriable());
assert!(KernelError::IoUringSubmitFull.is_retriable());
assert!(KernelError::NoTagsAvailable.is_retriable());
assert!(KernelError::UblkDeviceBusy.is_retriable());
}
#[test]
fn test_non_retriable_errors() {
assert!(!KernelError::OutOfMemory.is_retriable());
assert!(!KernelError::InvalidAddress.is_retriable());
assert!(!KernelError::InvalidRequest.is_retriable());
assert!(!KernelError::UblkDeviceNotFound.is_retriable());
assert!(!KernelError::NotSupported.is_retriable());
}
// ------------------------------------------------------------------------
// Resource Error Tests
// ------------------------------------------------------------------------
#[test]
fn test_resource_errors() {
assert!(KernelError::OutOfMemory.is_resource_error());
assert!(KernelError::UblkQueueFull.is_resource_error());
assert!(KernelError::NoTagsAvailable.is_resource_error());
}
#[test]
fn test_non_resource_errors() {
assert!(!KernelError::InvalidAddress.is_resource_error());
assert!(!KernelError::IoTimeout.is_resource_error());
assert!(!KernelError::NotSupported.is_resource_error());
}
// ------------------------------------------------------------------------
// Display Tests
// ------------------------------------------------------------------------
#[test]
fn test_display_not_empty() {
let errors = [
KernelError::OutOfMemory,
KernelError::InvalidAddress,
KernelError::IoTimeout,
KernelError::UblkQueueFull,
KernelError::IoUringSubmitFull,
KernelError::BlockError,
];
for error in errors {
let display = format!("{}", error);
assert!(!display.is_empty(), "display for {:?} is empty", error);
assert!(
!display.contains("KernelError"),
"display should be human-readable"
);
}
}
// ------------------------------------------------------------------------
// Property Tests
// ------------------------------------------------------------------------
#[test]
fn test_all_errors_have_errno() {
// Ensure all error variants have a valid errno mapping
let errors = [
KernelError::OutOfMemory,
KernelError::InvalidAddress,
KernelError::AlignmentError,
KernelError::OverlappingRegion,
KernelError::IoTimeout,
KernelError::DeviceNotReady,
KernelError::InvalidRequest,
KernelError::Cancelled,
KernelError::UblkQueueFull,
KernelError::UblkInvalidTag,
KernelError::UblkDeviceBusy,
KernelError::UblkDeviceNotFound,
KernelError::UblkInvalidDeviceId,
KernelError::UblkNotPermitted,
KernelError::IoUringSubmitFull,
KernelError::IoUringCqOverflow,
KernelError::IoUringInvalidOpcode,
KernelError::BlockError,
KernelError::NoTagsAvailable,
KernelError::InvalidQueueId,
KernelError::NotSupported,
KernelError::InvalidArgument,
KernelError::WouldBlock,
KernelError::Interrupted,
KernelError::ResourceBusy,
];
for error in errors {
let errno = error.to_errno();
assert!(errno < 0, "{:?} should have negative errno", error);
assert!(errno > -256, "{:?} errno {} is too negative", error, errno);
}
}
#[test]
fn test_error_is_copy() {
let error = KernelError::OutOfMemory;
let copy = error;
assert_eq!(error, copy);
}
#[test]
fn test_error_is_clone() {
let error = KernelError::IoTimeout;
#[allow(clippy::clone_on_copy)]
let cloned = error.clone();
assert_eq!(error, cloned);
}
#[test]
fn test_error_equality() {
assert_eq!(KernelError::OutOfMemory, KernelError::OutOfMemory);
assert_ne!(KernelError::OutOfMemory, KernelError::InvalidAddress);
}
}