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