| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use crate::{alloc::AllocError, str::CStr};
|
| 8 |
|
| 9 | use alloc::alloc::LayoutError;
|
| 10 |
|
| 11 | use core::fmt;
|
| 12 | use core::num::TryFromIntError;
|
| 13 | use core::str::Utf8Error;
|
| 14 |
|
| 15 | |
| 16 | #[rustfmt::skip]
|
| 17 | pub mod code {
|
| 18 | macro_rules! declare_err {
|
| 19 | ($err:tt $(,)? $($doc:expr),+) => {
|
| 20 | $(
|
| 21 | #[doc = $doc]
|
| 22 | )*
|
| 23 | pub const $err: super::Error = super::Error(-(crate::bindings::$err as i32));
|
| 24 | };
|
| 25 | }
|
| 26 |
|
| 27 | declare_err!(EPERM, "Operation not permitted.");
|
| 28 | declare_err!(ENOENT, "No such file or directory.");
|
| 29 | declare_err!(ESRCH, "No such process.");
|
| 30 | declare_err!(EINTR, "Interrupted system call.");
|
| 31 | declare_err!(EIO, "I/O error.");
|
| 32 | declare_err!(ENXIO, "No such device or address.");
|
| 33 | declare_err!(E2BIG, "Argument list too long.");
|
| 34 | declare_err!(ENOEXEC, "Exec format error.");
|
| 35 | declare_err!(EBADF, "Bad file number.");
|
| 36 | declare_err!(ECHILD, "No child processes.");
|
| 37 | declare_err!(EAGAIN, "Try again.");
|
| 38 | declare_err!(ENOMEM, "Out of memory.");
|
| 39 | declare_err!(EACCES, "Permission denied.");
|
| 40 | declare_err!(EFAULT, "Bad address.");
|
| 41 | declare_err!(ENOTBLK, "Block device required.");
|
| 42 | declare_err!(EBUSY, "Device or resource busy.");
|
| 43 | declare_err!(EEXIST, "File exists.");
|
| 44 | declare_err!(EXDEV, "Cross-device link.");
|
| 45 | declare_err!(ENODEV, "No such device.");
|
| 46 | declare_err!(ENOTDIR, "Not a directory.");
|
| 47 | declare_err!(EISDIR, "Is a directory.");
|
| 48 | declare_err!(EINVAL, "Invalid argument.");
|
| 49 | declare_err!(ENFILE, "File table overflow.");
|
| 50 | declare_err!(EMFILE, "Too many open files.");
|
| 51 | declare_err!(ENOTTY, "Not a typewriter.");
|
| 52 | declare_err!(ETXTBSY, "Text file busy.");
|
| 53 | declare_err!(EFBIG, "File too large.");
|
| 54 | declare_err!(ENOSPC, "No space left on device.");
|
| 55 | declare_err!(ESPIPE, "Illegal seek.");
|
| 56 | declare_err!(EROFS, "Read-only file system.");
|
| 57 | declare_err!(EMLINK, "Too many links.");
|
| 58 | declare_err!(EPIPE, "Broken pipe.");
|
| 59 | declare_err!(EDOM, "Math argument out of domain of func.");
|
| 60 | declare_err!(ERANGE, "Math result not representable.");
|
| 61 | declare_err!(ERESTARTSYS, "Restart the system call.");
|
| 62 | declare_err!(ERESTARTNOINTR, "System call was interrupted by a signal and will be restarted.");
|
| 63 | declare_err!(ERESTARTNOHAND, "Restart if no handler.");
|
| 64 | declare_err!(ENOIOCTLCMD, "No ioctl command.");
|
| 65 | declare_err!(ERESTART_RESTARTBLOCK, "Restart by calling sys_restart_syscall.");
|
| 66 | declare_err!(EPROBE_DEFER, "Driver requests probe retry.");
|
| 67 | declare_err!(EOPENSTALE, "Open found a stale dentry.");
|
| 68 | declare_err!(ENOPARAM, "Parameter not supported.");
|
| 69 | declare_err!(EBADHANDLE, "Illegal NFS file handle.");
|
| 70 | declare_err!(ENOTSYNC, "Update synchronization mismatch.");
|
| 71 | declare_err!(EBADCOOKIE, "Cookie is stale.");
|
| 72 | declare_err!(ENOTSUPP, "Operation is not supported.");
|
| 73 | declare_err!(ETOOSMALL, "Buffer or request is too small.");
|
| 74 | declare_err!(ESERVERFAULT, "An untranslatable error occurred.");
|
| 75 | declare_err!(EBADTYPE, "Type not supported by server.");
|
| 76 | declare_err!(EJUKEBOX, "Request initiated, but will not complete before timeout.");
|
| 77 | declare_err!(EIOCBQUEUED, "iocb queued, will get completion event.");
|
| 78 | declare_err!(ERECALLCONFLICT, "Conflict with recalled state.");
|
| 79 | declare_err!(ENOGRACE, "NFS file lock reclaim refused.");
|
| 80 | }
|
| 81 |
|
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | #[derive(Clone, Copy, PartialEq, Eq)]
|
| 91 | pub struct Error(core::ffi::c_int);
|
| 92 |
|
| 93 | impl Error {
|
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | pub(crate) fn from_errno(errno: core::ffi::c_int) -> Error {
|
| 99 | if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 {
|
| 100 | |
| 101 | crate::pr_warn!(
|
| 102 | "attempted to create `Error` with out of range `errno`: {}",
|
| 103 | errno
|
| 104 | );
|
| 105 | return code::EINVAL;
|
| 106 | }
|
| 107 |
|
| 108 | |
| 109 | |
| 110 | Error(errno)
|
| 111 | }
|
| 112 |
|
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | unsafe fn from_errno_unchecked(errno: core::ffi::c_int) -> Error {
|
| 119 | |
| 120 | |
| 121 | Error(errno)
|
| 122 | }
|
| 123 |
|
| 124 | |
| 125 | pub fn to_errno(self) -> core::ffi::c_int {
|
| 126 | self.0
|
| 127 | }
|
| 128 |
|
| 129 | #[cfg(CONFIG_BLOCK)]
|
| 130 | pub(crate) fn to_blk_status(self) -> bindings::blk_status_t {
|
| 131 | |
| 132 | unsafe { bindings::errno_to_blk_status(self.0) }
|
| 133 | }
|
| 134 |
|
| 135 | |
| 136 | #[allow(dead_code)]
|
| 137 | pub(crate) fn to_ptr<T>(self) -> *mut T {
|
| 138 | #[cfg_attr(target_pointer_width = "32", allow(clippy::useless_conversion))]
|
| 139 | |
| 140 | unsafe {
|
| 141 | bindings::ERR_PTR(self.0.into()) as *mut _
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | |
| 146 | #[cfg(not(testlib))]
|
| 147 | pub fn name(&self) -> Option<&'static CStr> {
|
| 148 | |
| 149 | let ptr = unsafe { bindings::errname(-self.0) };
|
| 150 | if ptr.is_null() {
|
| 151 | None
|
| 152 | } else {
|
| 153 | |
| 154 | Some(unsafe { CStr::from_char_ptr(ptr) })
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | #[cfg(testlib)]
|
| 164 | pub fn name(&self) -> Option<&'static CStr> {
|
| 165 | None
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | impl fmt::Debug for Error {
|
| 170 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 171 | match self.name() {
|
| 172 | |
| 173 | None => f.debug_tuple("Error").field(&-self.0).finish(),
|
| 174 | |
| 175 | Some(name) => f
|
| 176 | .debug_tuple(unsafe { core::str::from_utf8_unchecked(name) })
|
| 177 | .finish(),
|
| 178 | }
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | impl From<AllocError> for Error {
|
| 183 | fn from(_: AllocError) -> Error {
|
| 184 | code::ENOMEM
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | impl From<TryFromIntError> for Error {
|
| 189 | fn from(_: TryFromIntError) -> Error {
|
| 190 | code::EINVAL
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | impl From<Utf8Error> for Error {
|
| 195 | fn from(_: Utf8Error) -> Error {
|
| 196 | code::EINVAL
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | impl From<LayoutError> for Error {
|
| 201 | fn from(_: LayoutError) -> Error {
|
| 202 | code::ENOMEM
|
| 203 | }
|
| 204 | }
|
| 205 |
|
| 206 | impl From<core::fmt::Error> for Error {
|
| 207 | fn from(_: core::fmt::Error) -> Error {
|
| 208 | code::EINVAL
|
| 209 | }
|
| 210 | }
|
| 211 |
|
| 212 | impl From<core::convert::Infallible> for Error {
|
| 213 | fn from(e: core::convert::Infallible) -> Error {
|
| 214 | match e {}
|
| 215 | }
|
| 216 | }
|
| 217 |
|
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | pub type Result<T = (), E = Error> = core::result::Result<T, E>;
|
| 239 |
|
| 240 | |
| 241 | |
| 242 | pub fn to_result(err: core::ffi::c_int) -> Result {
|
| 243 | if err < 0 {
|
| 244 | Err(Error::from_errno(err))
|
| 245 | } else {
|
| 246 | Ok(())
|
| 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 | #[allow(dead_code)]
|
| 273 | pub(crate) fn from_err_ptr<T>(ptr: *mut T) -> Result<*mut T> {
|
| 274 | |
| 275 | let const_ptr: *const core::ffi::c_void = ptr.cast();
|
| 276 | |
| 277 | if unsafe { bindings::IS_ERR(const_ptr) } {
|
| 278 | |
| 279 | let err = unsafe { bindings::PTR_ERR(const_ptr) };
|
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | #[allow(clippy::unnecessary_cast)]
|
| 290 | return Err(unsafe { Error::from_errno_unchecked(err as core::ffi::c_int) });
|
| 291 | }
|
| 292 | Ok(ptr)
|
| 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 | #[allow(dead_code)]
|
| 320 | pub(crate) fn from_result<T, F>(f: F) -> T
|
| 321 | where
|
| 322 | T: From<i16>,
|
| 323 | F: FnOnce() -> Result<T>,
|
| 324 | {
|
| 325 | match f() {
|
| 326 | Ok(v) => v,
|
| 327 | |
| 328 | |
| 329 | |
| 330 | Err(e) => T::from(e.to_errno() as i16),
|
| 331 | }
|
| 332 | }
|
| 333 |
|
| 334 | |
| 335 | pub const VTABLE_DEFAULT_ERROR: &str =
|
| 336 | "This function must not be called, see the #[vtable] documentation.";
|