errname.c 3.6 KB

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