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