Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/build_bug.h>
0003 #include <linux/errno.h>
0004 #include <linux/errname.h>
0005 #include <linux/kernel.h>
0006 #include <linux/math.h>
0007 
0008 /*
0009  * Ensure these tables do not accidentally become gigantic if some
0010  * huge errno makes it in. On most architectures, the first table will
0011  * only have about 140 entries, but mips and parisc have more sparsely
0012  * allocated errnos (with EHWPOISON = 257 on parisc, and EDQUOT = 1133
0013  * on mips), so this wastes a bit of space on those - though we
0014  * special case the EDQUOT case.
0015  */
0016 #define E(err) [err + BUILD_BUG_ON_ZERO(err <= 0 || err > 300)] = "-" #err
0017 static const char *names_0[] = {
0018     E(E2BIG),
0019     E(EACCES),
0020     E(EADDRINUSE),
0021     E(EADDRNOTAVAIL),
0022     E(EADV),
0023     E(EAFNOSUPPORT),
0024     E(EALREADY),
0025     E(EBADE),
0026     E(EBADF),
0027     E(EBADFD),
0028     E(EBADMSG),
0029     E(EBADR),
0030     E(EBADRQC),
0031     E(EBADSLT),
0032     E(EBFONT),
0033     E(EBUSY),
0034 #ifdef ECANCELLED
0035     E(ECANCELLED),
0036 #endif
0037     E(ECHILD),
0038     E(ECHRNG),
0039     E(ECOMM),
0040     E(ECONNABORTED),
0041     E(ECONNRESET),
0042     E(EDEADLOCK),
0043     E(EDESTADDRREQ),
0044     E(EDOM),
0045     E(EDOTDOT),
0046 #ifndef CONFIG_MIPS
0047     E(EDQUOT),
0048 #endif
0049     E(EEXIST),
0050     E(EFAULT),
0051     E(EFBIG),
0052     E(EHOSTDOWN),
0053     E(EHOSTUNREACH),
0054     E(EHWPOISON),
0055     E(EIDRM),
0056     E(EILSEQ),
0057 #ifdef EINIT
0058     E(EINIT),
0059 #endif
0060     E(EINPROGRESS),
0061     E(EINTR),
0062     E(EINVAL),
0063     E(EIO),
0064     E(EISCONN),
0065     E(EISDIR),
0066     E(EISNAM),
0067     E(EKEYEXPIRED),
0068     E(EKEYREJECTED),
0069     E(EKEYREVOKED),
0070     E(EL2HLT),
0071     E(EL2NSYNC),
0072     E(EL3HLT),
0073     E(EL3RST),
0074     E(ELIBACC),
0075     E(ELIBBAD),
0076     E(ELIBEXEC),
0077     E(ELIBMAX),
0078     E(ELIBSCN),
0079     E(ELNRNG),
0080     E(ELOOP),
0081     E(EMEDIUMTYPE),
0082     E(EMFILE),
0083     E(EMLINK),
0084     E(EMSGSIZE),
0085     E(EMULTIHOP),
0086     E(ENAMETOOLONG),
0087     E(ENAVAIL),
0088     E(ENETDOWN),
0089     E(ENETRESET),
0090     E(ENETUNREACH),
0091     E(ENFILE),
0092     E(ENOANO),
0093     E(ENOBUFS),
0094     E(ENOCSI),
0095     E(ENODATA),
0096     E(ENODEV),
0097     E(ENOENT),
0098     E(ENOEXEC),
0099     E(ENOKEY),
0100     E(ENOLCK),
0101     E(ENOLINK),
0102     E(ENOMEDIUM),
0103     E(ENOMEM),
0104     E(ENOMSG),
0105     E(ENONET),
0106     E(ENOPKG),
0107     E(ENOPROTOOPT),
0108     E(ENOSPC),
0109     E(ENOSR),
0110     E(ENOSTR),
0111 #ifdef ENOSYM
0112     E(ENOSYM),
0113 #endif
0114     E(ENOSYS),
0115     E(ENOTBLK),
0116     E(ENOTCONN),
0117     E(ENOTDIR),
0118     E(ENOTEMPTY),
0119     E(ENOTNAM),
0120     E(ENOTRECOVERABLE),
0121     E(ENOTSOCK),
0122     E(ENOTTY),
0123     E(ENOTUNIQ),
0124     E(ENXIO),
0125     E(EOPNOTSUPP),
0126     E(EOVERFLOW),
0127     E(EOWNERDEAD),
0128     E(EPERM),
0129     E(EPFNOSUPPORT),
0130     E(EPIPE),
0131 #ifdef EPROCLIM
0132     E(EPROCLIM),
0133 #endif
0134     E(EPROTO),
0135     E(EPROTONOSUPPORT),
0136     E(EPROTOTYPE),
0137     E(ERANGE),
0138     E(EREMCHG),
0139 #ifdef EREMDEV
0140     E(EREMDEV),
0141 #endif
0142     E(EREMOTE),
0143     E(EREMOTEIO),
0144 #ifdef EREMOTERELEASE
0145     E(EREMOTERELEASE),
0146 #endif
0147     E(ERESTART),
0148     E(ERFKILL),
0149     E(EROFS),
0150 #ifdef ERREMOTE
0151     E(ERREMOTE),
0152 #endif
0153     E(ESHUTDOWN),
0154     E(ESOCKTNOSUPPORT),
0155     E(ESPIPE),
0156     E(ESRCH),
0157     E(ESRMNT),
0158     E(ESTALE),
0159     E(ESTRPIPE),
0160     E(ETIME),
0161     E(ETIMEDOUT),
0162     E(ETOOMANYREFS),
0163     E(ETXTBSY),
0164     E(EUCLEAN),
0165     E(EUNATCH),
0166     E(EUSERS),
0167     E(EXDEV),
0168     E(EXFULL),
0169 
0170     E(ECANCELED), /* ECANCELLED */
0171     E(EAGAIN), /* EWOULDBLOCK */
0172     E(ECONNREFUSED), /* EREFUSED */
0173     E(EDEADLK), /* EDEADLOCK */
0174 };
0175 #undef E
0176 
0177 #define E(err) [err - 512 + BUILD_BUG_ON_ZERO(err < 512 || err > 550)] = "-" #err
0178 static const char *names_512[] = {
0179     E(ERESTARTSYS),
0180     E(ERESTARTNOINTR),
0181     E(ERESTARTNOHAND),
0182     E(ENOIOCTLCMD),
0183     E(ERESTART_RESTARTBLOCK),
0184     E(EPROBE_DEFER),
0185     E(EOPENSTALE),
0186     E(ENOPARAM),
0187 
0188     E(EBADHANDLE),
0189     E(ENOTSYNC),
0190     E(EBADCOOKIE),
0191     E(ENOTSUPP),
0192     E(ETOOSMALL),
0193     E(ESERVERFAULT),
0194     E(EBADTYPE),
0195     E(EJUKEBOX),
0196     E(EIOCBQUEUED),
0197     E(ERECALLCONFLICT),
0198 };
0199 #undef E
0200 
0201 static const char *__errname(unsigned err)
0202 {
0203     if (err < ARRAY_SIZE(names_0))
0204         return names_0[err];
0205     if (err >= 512 && err - 512 < ARRAY_SIZE(names_512))
0206         return names_512[err - 512];
0207     /* But why? */
0208     if (IS_ENABLED(CONFIG_MIPS) && err == EDQUOT) /* 1133 */
0209         return "-EDQUOT";
0210     return NULL;
0211 }
0212 
0213 /*
0214  * errname(EIO) -> "EIO"
0215  * errname(-EIO) -> "-EIO"
0216  */
0217 const char *errname(int err)
0218 {
0219     const char *name = __errname(abs(err));
0220     if (!name)
0221         return NULL;
0222 
0223     return err > 0 ? name + 1 : name;
0224 }