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