xref: /openbmc/linux/rust/kernel/error.rs (revision 266def2a0f5bbe44b11902bd4c93b9c5d8b8d708)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Kernel errors.
4 //!
5 //! C header: [`include/uapi/asm-generic/errno-base.h`](../../../include/uapi/asm-generic/errno-base.h)
6 
7 use alloc::collections::TryReserveError;
8 
9 /// Contains the C-compatible error codes.
10 pub mod code {
11     macro_rules! declare_err {
12         ($err:tt $(,)? $($doc:expr),+) => {
13             $(
14             #[doc = $doc]
15             )*
16             pub const $err: super::Error = super::Error(-(crate::bindings::$err as i32));
17         };
18     }
19 
20     declare_err!(EPERM, "Operation not permitted.");
21     declare_err!(ENOENT, "No such file or directory.");
22     declare_err!(ESRCH, "No such process.");
23     declare_err!(EINTR, "Interrupted system call.");
24     declare_err!(EIO, "I/O error.");
25     declare_err!(ENXIO, "No such device or address.");
26     declare_err!(E2BIG, "Argument list too long.");
27     declare_err!(ENOEXEC, "Exec format error.");
28     declare_err!(EBADF, "Bad file number.");
29     declare_err!(ECHILD, "Exec format error.");
30     declare_err!(EAGAIN, "Try again.");
31     declare_err!(ENOMEM, "Out of memory.");
32     declare_err!(EACCES, "Permission denied.");
33     declare_err!(EFAULT, "Bad address.");
34     declare_err!(ENOTBLK, "Block device required.");
35     declare_err!(EBUSY, "Device or resource busy.");
36     declare_err!(EEXIST, "File exists.");
37     declare_err!(EXDEV, "Cross-device link.");
38     declare_err!(ENODEV, "No such device.");
39     declare_err!(ENOTDIR, "Not a directory.");
40     declare_err!(EISDIR, "Is a directory.");
41     declare_err!(EINVAL, "Invalid argument.");
42     declare_err!(ENFILE, "File table overflow.");
43     declare_err!(EMFILE, "Too many open files.");
44     declare_err!(ENOTTY, "Not a typewriter.");
45     declare_err!(ETXTBSY, "Text file busy.");
46     declare_err!(EFBIG, "File too large.");
47     declare_err!(ENOSPC, "No space left on device.");
48     declare_err!(ESPIPE, "Illegal seek.");
49     declare_err!(EROFS, "Read-only file system.");
50     declare_err!(EMLINK, "Too many links.");
51     declare_err!(EPIPE, "Broken pipe.");
52     declare_err!(EDOM, "Math argument out of domain of func.");
53     declare_err!(ERANGE, "Math result not representable.");
54 }
55 
56 /// Generic integer kernel error.
57 ///
58 /// The kernel defines a set of integer generic error codes based on C and
59 /// POSIX ones. These codes may have a more specific meaning in some contexts.
60 ///
61 /// # Invariants
62 ///
63 /// The value is a valid `errno` (i.e. `>= -MAX_ERRNO && < 0`).
64 #[derive(Clone, Copy, PartialEq, Eq)]
65 pub struct Error(core::ffi::c_int);
66 
67 impl Error {
68     /// Returns the kernel error code.
69     pub fn to_kernel_errno(self) -> core::ffi::c_int {
70         self.0
71     }
72 }
73 
74 impl From<TryReserveError> for Error {
75     fn from(_: TryReserveError) -> Error {
76         code::ENOMEM
77     }
78 }
79 
80 /// A [`Result`] with an [`Error`] error type.
81 ///
82 /// To be used as the return type for functions that may fail.
83 ///
84 /// # Error codes in C and Rust
85 ///
86 /// In C, it is common that functions indicate success or failure through
87 /// their return value; modifying or returning extra data through non-`const`
88 /// pointer parameters. In particular, in the kernel, functions that may fail
89 /// typically return an `int` that represents a generic error code. We model
90 /// those as [`Error`].
91 ///
92 /// In Rust, it is idiomatic to model functions that may fail as returning
93 /// a [`Result`]. Since in the kernel many functions return an error code,
94 /// [`Result`] is a type alias for a [`core::result::Result`] that uses
95 /// [`Error`] as its error type.
96 ///
97 /// Note that even if a function does not return anything when it succeeds,
98 /// it should still be modeled as returning a `Result` rather than
99 /// just an [`Error`].
100 pub type Result<T = ()> = core::result::Result<T, Error>;
101