| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use crate::{
|
| 8 | alloc::{layout::LayoutError, AllocError},
|
| 9 | fmt,
|
| 10 | str::CStr,
|
| 11 | };
|
| 12 |
|
| 13 | use core::num::NonZeroI32;
|
| 14 | use core::num::TryFromIntError;
|
| 15 | use core::str::Utf8Error;
|
| 16 |
|
| 17 | |
| 18 | #[rustfmt::skip]
|
| 19 | pub mod code {
|
| 20 | macro_rules! declare_err {
|
| 21 | ($err:tt $(,)? $($doc:expr),+) => {
|
| 22 | $(
|
| 23 | #[doc = $doc]
|
| 24 | )*
|
| 25 | pub const $err: super::Error =
|
| 26 | match super::Error::try_from_errno(-(crate::bindings::$err as i32)) {
|
| 27 | Some(err) => err,
|
| 28 | None => panic!("Invalid errno in `declare_err!`"),
|
| 29 | };
|
| 30 | };
|
| 31 | }
|
| 32 |
|
| 33 | declare_err!(EPERM, "Operation not permitted.");
|
| 34 | declare_err!(ENOENT, "No such file or directory.");
|
| 35 | declare_err!(ESRCH, "No such process.");
|
| 36 | declare_err!(EINTR, "Interrupted system call.");
|
| 37 | declare_err!(EIO, "I/O error.");
|
| 38 | declare_err!(ENXIO, "No such device or address.");
|
| 39 | declare_err!(E2BIG, "Argument list too long.");
|
| 40 | declare_err!(ENOEXEC, "Exec format error.");
|
| 41 | declare_err!(EBADF, "Bad file number.");
|
| 42 | declare_err!(ECHILD, "No child processes.");
|
| 43 | declare_err!(EAGAIN, "Try again.");
|
| 44 | declare_err!(ENOMEM, "Out of memory.");
|
| 45 | declare_err!(EACCES, "Permission denied.");
|
| 46 | declare_err!(EFAULT, "Bad address.");
|
| 47 | declare_err!(ENOTBLK, "Block device required.");
|
| 48 | declare_err!(EBUSY, "Device or resource busy.");
|
| 49 | declare_err!(EEXIST, "File exists.");
|
| 50 | declare_err!(EXDEV, "Cross-device link.");
|
| 51 | declare_err!(ENODEV, "No such device.");
|
| 52 | declare_err!(ENOTDIR, "Not a directory.");
|
| 53 | declare_err!(EISDIR, "Is a directory.");
|
| 54 | declare_err!(EINVAL, "Invalid argument.");
|
| 55 | declare_err!(ENFILE, "File table overflow.");
|
| 56 | declare_err!(EMFILE, "Too many open files.");
|
| 57 | declare_err!(ENOTTY, "Not a typewriter.");
|
| 58 | declare_err!(ETXTBSY, "Text file busy.");
|
| 59 | declare_err!(EFBIG, "File too large.");
|
| 60 | declare_err!(ENOSPC, "No space left on device.");
|
| 61 | declare_err!(ESPIPE, "Illegal seek.");
|
| 62 | declare_err!(EROFS, "Read-only file system.");
|
| 63 | declare_err!(EMLINK, "Too many links.");
|
| 64 | declare_err!(EPIPE, "Broken pipe.");
|
| 65 | declare_err!(EDOM, "Math argument out of domain of func.");
|
| 66 | declare_err!(ERANGE, "Math result not representable.");
|
| 67 | declare_err!(EOVERFLOW, "Value too large for defined data type.");
|
| 68 | declare_err!(ETIMEDOUT, "Connection timed out.");
|
| 69 | declare_err!(ERESTARTSYS, "Restart the system call.");
|
| 70 | declare_err!(ERESTARTNOINTR, "System call was interrupted by a signal and will be restarted.");
|
| 71 | declare_err!(ERESTARTNOHAND, "Restart if no handler.");
|
| 72 | declare_err!(ENOIOCTLCMD, "No ioctl command.");
|
| 73 | declare_err!(ERESTART_RESTARTBLOCK, "Restart by calling sys_restart_syscall.");
|
| 74 | declare_err!(EPROBE_DEFER, "Driver requests probe retry.");
|
| 75 | declare_err!(EOPENSTALE, "Open found a stale dentry.");
|
| 76 | declare_err!(ENOPARAM, "Parameter not supported.");
|
| 77 | declare_err!(EBADHANDLE, "Illegal NFS file handle.");
|
| 78 | declare_err!(ENOTSYNC, "Update synchronization mismatch.");
|
| 79 | declare_err!(EBADCOOKIE, "Cookie is stale.");
|
| 80 | declare_err!(ENOTSUPP, "Operation is not supported.");
|
| 81 | declare_err!(ETOOSMALL, "Buffer or request is too small.");
|
| 82 | declare_err!(ESERVERFAULT, "An untranslatable error occurred.");
|
| 83 | declare_err!(EBADTYPE, "Type not supported by server.");
|
| 84 | declare_err!(EJUKEBOX, "Request initiated, but will not complete before timeout.");
|
| 85 | declare_err!(EIOCBQUEUED, "iocb queued, will get completion event.");
|
| 86 | declare_err!(ERECALLCONFLICT, "Conflict with recalled state.");
|
| 87 | declare_err!(ENOGRACE, "NFS file lock reclaim refused.");
|
| 88 | }
|
| 89 |
|
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | #[derive(Clone, Copy, PartialEq, Eq)]
|
| 99 | pub struct Error(NonZeroI32);
|
| 100 |
|
| 101 | impl Error {
|
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | pub fn from_errno(errno: crate::ffi::c_int) -> Error {
|
| 107 | if let Some(error) = Self::try_from_errno(errno) {
|
| 108 | error
|
| 109 | } else {
|
| 110 | |
| 111 | crate::pr_warn!(
|
| 112 | "attempted to create `Error` with out of range `errno`: {}\n",
|
| 113 | errno
|
| 114 | );
|
| 115 | code::EINVAL
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | |
| 120 | |
| 121 | |
| 122 | const fn try_from_errno(errno: crate::ffi::c_int) -> Option<Error> {
|
| 123 | if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 {
|
| 124 | return None;
|
| 125 | }
|
| 126 |
|
| 127 | |
| 128 | Some(unsafe { Error::from_errno_unchecked(errno) })
|
| 129 | }
|
| 130 |
|
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | const unsafe fn from_errno_unchecked(errno: crate::ffi::c_int) -> Error {
|
| 137 | |
| 138 | |
| 139 | |
| 140 | Error(unsafe { NonZeroI32::new_unchecked(errno) })
|
| 141 | }
|
| 142 |
|
| 143 | |
| 144 | pub fn to_errno(self) -> crate::ffi::c_int {
|
| 145 | self.0.get()
|
| 146 | }
|
| 147 |
|
| 148 | #[cfg(CONFIG_BLOCK)]
|
| 149 | pub(crate) fn to_blk_status(self) -> bindings::blk_status_t {
|
| 150 | |
| 151 | unsafe { bindings::errno_to_blk_status(self.0.get()) }
|
| 152 | }
|
| 153 |
|
| 154 | |
| 155 | pub fn to_ptr<T>(self) -> *mut T {
|
| 156 | |
| 157 | unsafe { bindings::ERR_PTR(self.0.get() as crate::ffi::c_long).cast() }
|
| 158 | }
|
| 159 |
|
| 160 | |
| 161 | #[cfg(not(any(test, testlib)))]
|
| 162 | pub fn name(&self) -> Option<&'static CStr> {
|
| 163 | |
| 164 | let ptr = unsafe { bindings::errname(-self.0.get()) };
|
| 165 | if ptr.is_null() {
|
| 166 | None
|
| 167 | } else {
|
| 168 | |
| 169 | Some(unsafe { CStr::from_char_ptr(ptr) })
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | #[cfg(any(test, testlib))]
|
| 179 | pub fn name(&self) -> Option<&'static CStr> {
|
| 180 | None
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | impl fmt::Debug for Error {
|
| 185 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 186 | match self.name() {
|
| 187 | |
| 188 | None => f.debug_tuple("Error").field(&-self.0).finish(),
|
| 189 | Some(name) => f
|
| 190 | .debug_tuple(
|
| 191 | |
| 192 | unsafe { core::str::from_utf8_unchecked(name.to_bytes()) },
|
| 193 | )
|
| 194 | .finish(),
|
| 195 | }
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | impl From<AllocError> for Error {
|
| 200 | fn from(_: AllocError) -> Error {
|
| 201 | code::ENOMEM
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | impl From<TryFromIntError> for Error {
|
| 206 | fn from(_: TryFromIntError) -> Error {
|
| 207 | code::EINVAL
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | impl From<Utf8Error> for Error {
|
| 212 | fn from(_: Utf8Error) -> Error {
|
| 213 | code::EINVAL
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | impl From<LayoutError> for Error {
|
| 218 | fn from(_: LayoutError) -> Error {
|
| 219 | code::ENOMEM
|
| 220 | }
|
| 221 | }
|
| 222 |
|
| 223 | impl From<fmt::Error> for Error {
|
| 224 | fn from(_: fmt::Error) -> Error {
|
| 225 | code::EINVAL
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | impl From<core::convert::Infallible> for Error {
|
| 230 | fn from(e: core::convert::Infallible) -> Error {
|
| 231 | match e {}
|
| 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 | pub type Result<T = (), E = Error> = core::result::Result<T, E>;
|
| 377 |
|
| 378 | |
| 379 | |
| 380 | pub fn to_result(err: crate::ffi::c_int) -> Result {
|
| 381 | if err < 0 {
|
| 382 | Err(Error::from_errno(err))
|
| 383 | } else {
|
| 384 | Ok(())
|
| 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 | pub fn from_err_ptr<T>(ptr: *mut T) -> Result<*mut T> {
|
| 410 | |
| 411 | let const_ptr: *const crate::ffi::c_void = ptr.cast();
|
| 412 | |
| 413 | if unsafe { bindings::IS_ERR(const_ptr) } {
|
| 414 | |
| 415 | let err = unsafe { bindings::PTR_ERR(const_ptr) };
|
| 416 |
|
| 417 | #[allow(clippy::unnecessary_cast)]
|
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | return Err(unsafe { Error::from_errno_unchecked(err as crate::ffi::c_int) });
|
| 428 | }
|
| 429 | Ok(ptr)
|
| 430 | }
|
| 431 |
|
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | pub fn from_result<T, F>(f: F) -> T
|
| 456 | where
|
| 457 | T: From<i16>,
|
| 458 | F: FnOnce() -> Result<T>,
|
| 459 | {
|
| 460 | match f() {
|
| 461 | Ok(v) => v,
|
| 462 | |
| 463 | |
| 464 | |
| 465 | Err(e) => T::from(e.to_errno() as i16),
|
| 466 | }
|
| 467 | }
|
| 468 |
|
| 469 | |
| 470 | pub const VTABLE_DEFAULT_ERROR: &str =
|
| 471 | "This function must not be called, see the #[vtable] documentation.";
|