xref: /linux/lib/errname.c (revision 2da68a77)
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
17 static const char *names_0[] = {
18 	E(E2BIG),
19 	E(EACCES),
20 	E(EADDRINUSE),
21 	E(EADDRNOTAVAIL),
22 	E(EADV),
23 	E(EAFNOSUPPORT),
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 #ifdef ECANCELLED
35 	E(ECANCELLED),
36 #endif
37 	E(ECHILD),
38 	E(ECHRNG),
39 	E(ECOMM),
40 	E(ECONNABORTED),
41 	E(ECONNRESET),
42 	E(EDEADLOCK),
43 	E(EDESTADDRREQ),
44 	E(EDOM),
45 	E(EDOTDOT),
46 #ifndef CONFIG_MIPS
47 	E(EDQUOT),
48 #endif
49 	E(EEXIST),
50 	E(EFAULT),
51 	E(EFBIG),
52 	E(EHOSTDOWN),
53 	E(EHOSTUNREACH),
54 	E(EHWPOISON),
55 	E(EIDRM),
56 	E(EILSEQ),
57 #ifdef EINIT
58 	E(EINIT),
59 #endif
60 	E(EINPROGRESS),
61 	E(EINTR),
62 	E(EINVAL),
63 	E(EIO),
64 	E(EISCONN),
65 	E(EISDIR),
66 	E(EISNAM),
67 	E(EKEYEXPIRED),
68 	E(EKEYREJECTED),
69 	E(EKEYREVOKED),
70 	E(EL2HLT),
71 	E(EL2NSYNC),
72 	E(EL3HLT),
73 	E(EL3RST),
74 	E(ELIBACC),
75 	E(ELIBBAD),
76 	E(ELIBEXEC),
77 	E(ELIBMAX),
78 	E(ELIBSCN),
79 	E(ELNRNG),
80 	E(ELOOP),
81 	E(EMEDIUMTYPE),
82 	E(EMFILE),
83 	E(EMLINK),
84 	E(EMSGSIZE),
85 	E(EMULTIHOP),
86 	E(ENAMETOOLONG),
87 	E(ENAVAIL),
88 	E(ENETDOWN),
89 	E(ENETRESET),
90 	E(ENETUNREACH),
91 	E(ENFILE),
92 	E(ENOANO),
93 	E(ENOBUFS),
94 	E(ENOCSI),
95 	E(ENODATA),
96 	E(ENODEV),
97 	E(ENOENT),
98 	E(ENOEXEC),
99 	E(ENOKEY),
100 	E(ENOLCK),
101 	E(ENOLINK),
102 	E(ENOMEDIUM),
103 	E(ENOMEM),
104 	E(ENOMSG),
105 	E(ENONET),
106 	E(ENOPKG),
107 	E(ENOPROTOOPT),
108 	E(ENOSPC),
109 	E(ENOSR),
110 	E(ENOSTR),
111 #ifdef ENOSYM
112 	E(ENOSYM),
113 #endif
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 #ifdef EREMOTERELEASE
145 	E(EREMOTERELEASE),
146 #endif
147 	E(ERESTART),
148 	E(ERFKILL),
149 	E(EROFS),
150 #ifdef ERREMOTE
151 	E(ERREMOTE),
152 #endif
153 	E(ESHUTDOWN),
154 	E(ESOCKTNOSUPPORT),
155 	E(ESPIPE),
156 	E(ESRCH),
157 	E(ESRMNT),
158 	E(ESTALE),
159 	E(ESTRPIPE),
160 	E(ETIME),
161 	E(ETIMEDOUT),
162 	E(ETOOMANYREFS),
163 	E(ETXTBSY),
164 	E(EUCLEAN),
165 	E(EUNATCH),
166 	E(EUSERS),
167 	E(EXDEV),
168 	E(EXFULL),
169 
170 	E(ECANCELED), /* ECANCELLED */
171 	E(EAGAIN), /* EWOULDBLOCK */
172 	E(ECONNREFUSED), /* EREFUSED */
173 	E(EDEADLK), /* EDEADLOCK */
174 };
175 #undef E
176 
177 #define E(err) [err - 512 + BUILD_BUG_ON_ZERO(err < 512 || err > 550)] = "-" #err
178 static 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 
201 static 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  */
217 const 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 }
225