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 E(ENOSYS),
115 E(ENOTBLK),
116 E(ENOTCONN),
117 E(ENOTDIR),
118 E(ENOTEMPTY),
119 E(ENOTNAM),
120 E(ENOTRECOVERABLE),
121 E(ENOTSOCK),
122 E(ENOTTY),
123 E(ENOTUNIQ),
124 E(ENXIO),
125 E(EOPNOTSUPP),
126 E(EOVERFLOW),
127 E(EOWNERDEAD),
128 E(EPERM),
129 E(EPFNOSUPPORT),
130 E(EPIPE),
131#ifdef EPROCLIM
132 E(EPROCLIM),
133#endif
134 E(EPROTO),
135 E(EPROTONOSUPPORT),
136 E(EPROTOTYPE),
137 E(ERANGE),
138 E(EREMCHG),
139#ifdef EREMDEV
140 E(EREMDEV),
141#endif
142 E(EREMOTE),
143 E(EREMOTEIO),
144 E(ERESTART),
145 E(ERFKILL),
146 E(EROFS),
147#ifdef ERREMOTE
148 E(ERREMOTE),
149#endif
150 E(ESHUTDOWN),
151 E(ESOCKTNOSUPPORT),
152 E(ESPIPE),
153 E(ESRCH),
154 E(ESRMNT),
155 E(ESTALE),
156 E(ESTRPIPE),
157 E(ETIME),
158 E(ETIMEDOUT),
159 E(ETOOMANYREFS),
160 E(ETXTBSY),
161 E(EUCLEAN),
162 E(EUNATCH),
163 E(EUSERS),
164 E(EXDEV),
165 E(EXFULL),
166};
167#undef E
168
169#ifdef EREFUSED /* parisc */
170static_assert(EREFUSED == ECONNREFUSED);
171#endif
172#ifdef ECANCELLED /* parisc */
173static_assert(ECANCELLED == ECANCELED);
174#endif
175static_assert(EAGAIN == EWOULDBLOCK); /* everywhere */
176
177#define E(err) [err - 512 + BUILD_BUG_ON_ZERO(err < 512 || err > 550)] = "-" #err
178static const char *names_512[] = {
179 E(ERESTARTSYS),
180 E(ERESTARTNOINTR),
181 E(ERESTARTNOHAND),
182 E(ENOIOCTLCMD),
183 E(ERESTART_RESTARTBLOCK),
184 E(EPROBE_DEFER),
185 E(EOPENSTALE),
186 E(ENOPARAM),
187
188 E(EBADHANDLE),
189 E(ENOTSYNC),
190 E(EBADCOOKIE),
191 E(ENOTSUPP),
192 E(ETOOSMALL),
193 E(ESERVERFAULT),
194 E(EBADTYPE),
195 E(EJUKEBOX),
196 E(EIOCBQUEUED),
197 E(ERECALLCONFLICT),
198};
199#undef E
200
201static const char *__errname(unsigned err)
202{
203 if (err < ARRAY_SIZE(names_0))
204 return names_0[err];
205 if (err >= 512 && err - 512 < ARRAY_SIZE(names_512))
206 return names_512[err - 512];
207 /* But why? */
208 if (IS_ENABLED(CONFIG_MIPS) && err == EDQUOT) /* 1133 */
209 return "-EDQUOT";
210 return NULL;
211}
212
213/*
214 * errname(EIO) -> "EIO"
215 * errname(-EIO) -> "-EIO"
216 */
217const char *errname(int err)
218{
219 const char *name = __errname(abs(err));
220 if (!name)
221 return NULL;
222
223 return err > 0 ? name + 1 : name;
224}
225EXPORT_SYMBOL(errname);
226

source code of linux/lib/errname.c