1// SPDX-License-Identifier: GPL-2.0
2#include <linux/build_bug.h>
3#include <linux/errno.h>
4#include <linux/errname.h>
5#include <linux/kernel.h>
6#include <linux/math.h>
7
8/*
9 * Ensure these tables do not accidentally become gigantic if some
10 * huge errno makes it in. On most architectures, the first table will
11 * only have about 140 entries, but mips and parisc have more sparsely
12 * allocated errnos (with EHWPOISON = 257 on parisc, and EDQUOT = 1133
13 * on mips), so this wastes a bit of space on those - though we
14 * special case the EDQUOT case.
15 */
16#define E(err) [err + BUILD_BUG_ON_ZERO(err <= 0 || err > 300)] = "-" #err
17static const char *names_0[] = {
18 E(E2BIG),
19 E(EACCES),
20 E(EADDRINUSE),
21 E(EADDRNOTAVAIL),
22 E(EADV),
23 E(EAFNOSUPPORT),
24 E(EAGAIN), /* EWOULDBLOCK */
25 E(EALREADY),
26 E(EBADE),
27 E(EBADF),
28 E(EBADFD),
29 E(EBADMSG),
30 E(EBADR),
31 E(EBADRQC),
32 E(EBADSLT),
33 E(EBFONT),
34 E(EBUSY),
35 E(ECANCELED), /* ECANCELLED */
36 E(ECHILD),
37 E(ECHRNG),
38 E(ECOMM),
39 E(ECONNABORTED),
40 E(ECONNREFUSED), /* EREFUSED */
41 E(ECONNRESET),
42 E(EDEADLK), /* EDEADLOCK */
43#if EDEADLK != EDEADLOCK /* mips, sparc, powerpc */
44 E(EDEADLOCK),
45#endif
46 E(EDESTADDRREQ),
47 E(EDOM),
48 E(EDOTDOT),
49#ifndef CONFIG_MIPS
50 E(EDQUOT),
51#endif
52 E(EEXIST),
53 E(EFAULT),
54 E(EFBIG),
55 E(EHOSTDOWN),
56 E(EHOSTUNREACH),
57 E(EHWPOISON),
58 E(EIDRM),
59 E(EILSEQ),
60#ifdef EINIT
61 E(EINIT),
62#endif
63 E(EINPROGRESS),
64 E(EINTR),
65 E(EINVAL),
66 E(EIO),
67 E(EISCONN),
68 E(EISDIR),
69 E(EISNAM),
70 E(EKEYEXPIRED),
71 E(EKEYREJECTED),
72 E(EKEYREVOKED),
73 E(EL2HLT),
74 E(EL2NSYNC),
75 E(EL3HLT),
76 E(EL3RST),
77 E(ELIBACC),
78 E(ELIBBAD),
79 E(ELIBEXEC),
80 E(ELIBMAX),
81 E(ELIBSCN),
82 E(ELNRNG),
83 E(ELOOP),
84 E(EMEDIUMTYPE),
85 E(EMFILE),
86 E(EMLINK),
87 E(EMSGSIZE),
88 E(EMULTIHOP),
89 E(ENAMETOOLONG),
90 E(ENAVAIL),
91 E(ENETDOWN),
92 E(ENETRESET),
93 E(ENETUNREACH),
94 E(ENFILE),
95 E(ENOANO),
96 E(ENOBUFS),
97 E(ENOCSI),
98 E(ENODATA),
99 E(ENODEV),
100 E(ENOENT),
101 E(ENOEXEC),
102 E(ENOKEY),
103 E(ENOLCK),
104 E(ENOLINK),
105 E(ENOMEDIUM),
106 E(ENOMEM),
107 E(ENOMSG),
108 E(ENONET),
109 E(ENOPKG),
110 E(ENOPROTOOPT),
111 E(ENOSPC),
112 E(ENOSR),
113 E(ENOSTR),
114#ifdef ENOSYM
115 E(ENOSYM),
116#endif
117 E(ENOSYS),
118 E(ENOTBLK),
119 E(ENOTCONN),
120 E(ENOTDIR),
121 E(ENOTEMPTY),
122 E(ENOTNAM),
123 E(ENOTRECOVERABLE),
124 E(ENOTSOCK),
125 E(ENOTTY),
126 E(ENOTUNIQ),
127 E(ENXIO),
128 E(EOPNOTSUPP),
129 E(EOVERFLOW),
130 E(EOWNERDEAD),
131 E(EPERM),
132 E(EPFNOSUPPORT),
133 E(EPIPE),
134#ifdef EPROCLIM
135 E(EPROCLIM),
136#endif
137 E(EPROTO),
138 E(EPROTONOSUPPORT),
139 E(EPROTOTYPE),
140 E(ERANGE),
141 E(EREMCHG),
142#ifdef EREMDEV
143 E(EREMDEV),
144#endif
145 E(EREMOTE),
146 E(EREMOTEIO),
147#ifdef EREMOTERELEASE
148 E(EREMOTERELEASE),
149#endif
150 E(ERESTART),
151 E(ERFKILL),
152 E(EROFS),
153#ifdef ERREMOTE
154 E(ERREMOTE),
155#endif
156 E(ESHUTDOWN),
157 E(ESOCKTNOSUPPORT),
158 E(ESPIPE),
159 E(ESRCH),
160 E(ESRMNT),
161 E(ESTALE),
162 E(ESTRPIPE),
163 E(ETIME),
164 E(ETIMEDOUT),
165 E(ETOOMANYREFS),
166 E(ETXTBSY),
167 E(EUCLEAN),
168 E(EUNATCH),
169 E(EUSERS),
170 E(EXDEV),
171 E(EXFULL),
172};
173#undef E
174
175#ifdef EREFUSED /* parisc */
176static_assert(EREFUSED == ECONNREFUSED);
177#endif
178#ifdef ECANCELLED /* parisc */
179static_assert(ECANCELLED == ECANCELED);
180#endif
181static_assert(EAGAIN == EWOULDBLOCK); /* everywhere */
182
183#define E(err) [err - 512 + BUILD_BUG_ON_ZERO(err < 512 || err > 550)] = "-" #err
184static const char *names_512[] = {
185 E(ERESTARTSYS),
186 E(ERESTARTNOINTR),
187 E(ERESTARTNOHAND),
188 E(ENOIOCTLCMD),
189 E(ERESTART_RESTARTBLOCK),
190 E(EPROBE_DEFER),
191 E(EOPENSTALE),
192 E(ENOPARAM),
193
194 E(EBADHANDLE),
195 E(ENOTSYNC),
196 E(EBADCOOKIE),
197 E(ENOTSUPP),
198 E(ETOOSMALL),
199 E(ESERVERFAULT),
200 E(EBADTYPE),
201 E(EJUKEBOX),
202 E(EIOCBQUEUED),
203 E(ERECALLCONFLICT),
204};
205#undef E
206
207static const char *__errname(unsigned err)
208{
209 if (err < ARRAY_SIZE(names_0))
210 return names_0[err];
211 if (err >= 512 && err - 512 < ARRAY_SIZE(names_512))
212 return names_512[err - 512];
213 /* But why? */
214 if (IS_ENABLED(CONFIG_MIPS) && err == EDQUOT) /* 1133 */
215 return "-EDQUOT";
216 return NULL;
217}
218
219/*
220 * errname(EIO) -> "EIO"
221 * errname(-EIO) -> "-EIO"
222 */
223const char *errname(int err)
224{
225 const char *name = __errname(abs(err));
226 if (!name)
227 return NULL;
228
229 return err > 0 ? name + 1 : name;
230}
231EXPORT_SYMBOL(errname);
232

source code of linux/lib/errname.c