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