error.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Error string handling
  4. *
  5. * Plan 9 uses error strings, Unix uses error numbers. These functions
  6. * try to help manage that and provide for dynamically adding error
  7. * mappings.
  8. *
  9. * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
  10. * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/list.h>
  15. #include <linux/jhash.h>
  16. #include <linux/errno.h>
  17. #include <linux/hashtable.h>
  18. #include <net/9p/9p.h>
  19. /**
  20. * struct errormap - map string errors from Plan 9 to Linux numeric ids
  21. * @name: string sent over 9P
  22. * @val: numeric id most closely representing @name
  23. * @namelen: length of string
  24. * @list: hash-table list for string lookup
  25. */
  26. struct errormap {
  27. char *name;
  28. int val;
  29. int namelen;
  30. struct hlist_node list;
  31. };
  32. #define ERRHASH_BITS 5
  33. static DEFINE_HASHTABLE(hash_errmap, ERRHASH_BITS);
  34. /* FixMe - reduce to a reasonable size */
  35. static struct errormap errmap[] = {
  36. {"Operation not permitted", EPERM},
  37. {"wstat prohibited", EPERM},
  38. {"No such file or directory", ENOENT},
  39. {"directory entry not found", ENOENT},
  40. {"file not found", ENOENT},
  41. {"Interrupted system call", EINTR},
  42. {"Input/output error", EIO},
  43. {"No such device or address", ENXIO},
  44. {"Argument list too long", E2BIG},
  45. {"Bad file descriptor", EBADF},
  46. {"Resource temporarily unavailable", EAGAIN},
  47. {"Cannot allocate memory", ENOMEM},
  48. {"Permission denied", EACCES},
  49. {"Bad address", EFAULT},
  50. {"Block device required", ENOTBLK},
  51. {"Device or resource busy", EBUSY},
  52. {"File exists", EEXIST},
  53. {"Invalid cross-device link", EXDEV},
  54. {"No such device", ENODEV},
  55. {"Not a directory", ENOTDIR},
  56. {"Is a directory", EISDIR},
  57. {"Invalid argument", EINVAL},
  58. {"Too many open files in system", ENFILE},
  59. {"Too many open files", EMFILE},
  60. {"Text file busy", ETXTBSY},
  61. {"File too large", EFBIG},
  62. {"No space left on device", ENOSPC},
  63. {"Illegal seek", ESPIPE},
  64. {"Read-only file system", EROFS},
  65. {"Too many links", EMLINK},
  66. {"Broken pipe", EPIPE},
  67. {"Numerical argument out of domain", EDOM},
  68. {"Numerical result out of range", ERANGE},
  69. {"Resource deadlock avoided", EDEADLK},
  70. {"File name too long", ENAMETOOLONG},
  71. {"No locks available", ENOLCK},
  72. {"Function not implemented", ENOSYS},
  73. {"Directory not empty", ENOTEMPTY},
  74. {"Too many levels of symbolic links", ELOOP},
  75. {"No message of desired type", ENOMSG},
  76. {"Identifier removed", EIDRM},
  77. {"No data available", ENODATA},
  78. {"Machine is not on the network", ENONET},
  79. {"Package not installed", ENOPKG},
  80. {"Object is remote", EREMOTE},
  81. {"Link has been severed", ENOLINK},
  82. {"Communication error on send", ECOMM},
  83. {"Protocol error", EPROTO},
  84. {"Bad message", EBADMSG},
  85. {"File descriptor in bad state", EBADFD},
  86. {"Streams pipe error", ESTRPIPE},
  87. {"Too many users", EUSERS},
  88. {"Socket operation on non-socket", ENOTSOCK},
  89. {"Message too long", EMSGSIZE},
  90. {"Protocol not available", ENOPROTOOPT},
  91. {"Protocol not supported", EPROTONOSUPPORT},
  92. {"Socket type not supported", ESOCKTNOSUPPORT},
  93. {"Operation not supported", EOPNOTSUPP},
  94. {"Protocol family not supported", EPFNOSUPPORT},
  95. {"Network is down", ENETDOWN},
  96. {"Network is unreachable", ENETUNREACH},
  97. {"Network dropped connection on reset", ENETRESET},
  98. {"Software caused connection abort", ECONNABORTED},
  99. {"Connection reset by peer", ECONNRESET},
  100. {"No buffer space available", ENOBUFS},
  101. {"Transport endpoint is already connected", EISCONN},
  102. {"Transport endpoint is not connected", ENOTCONN},
  103. {"Cannot send after transport endpoint shutdown", ESHUTDOWN},
  104. {"Connection timed out", ETIMEDOUT},
  105. {"Connection refused", ECONNREFUSED},
  106. {"Host is down", EHOSTDOWN},
  107. {"No route to host", EHOSTUNREACH},
  108. {"Operation already in progress", EALREADY},
  109. {"Operation now in progress", EINPROGRESS},
  110. {"Is a named type file", EISNAM},
  111. {"Remote I/O error", EREMOTEIO},
  112. {"Disk quota exceeded", EDQUOT},
  113. /* errors from fossil, vacfs, and u9fs */
  114. {"fid unknown or out of range", EBADF},
  115. {"permission denied", EACCES},
  116. {"file does not exist", ENOENT},
  117. {"authentication failed", ECONNREFUSED},
  118. {"bad offset in directory read", ESPIPE},
  119. {"bad use of fid", EBADF},
  120. {"wstat can't convert between files and directories", EPERM},
  121. {"directory is not empty", ENOTEMPTY},
  122. {"file exists", EEXIST},
  123. {"file already exists", EEXIST},
  124. {"file or directory already exists", EEXIST},
  125. {"fid already in use", EBADF},
  126. {"file in use", ETXTBSY},
  127. {"i/o error", EIO},
  128. {"file already open for I/O", ETXTBSY},
  129. {"illegal mode", EINVAL},
  130. {"illegal name", ENAMETOOLONG},
  131. {"not a directory", ENOTDIR},
  132. {"not a member of proposed group", EPERM},
  133. {"not owner", EACCES},
  134. {"only owner can change group in wstat", EACCES},
  135. {"read only file system", EROFS},
  136. {"no access to special file", EPERM},
  137. {"i/o count too large", EIO},
  138. {"unknown group", EINVAL},
  139. {"unknown user", EINVAL},
  140. {"bogus wstat buffer", EPROTO},
  141. {"exclusive use file already open", EAGAIN},
  142. {"corrupted directory entry", EIO},
  143. {"corrupted file entry", EIO},
  144. {"corrupted block label", EIO},
  145. {"corrupted meta data", EIO},
  146. {"illegal offset", EINVAL},
  147. {"illegal path element", ENOENT},
  148. {"root of file system is corrupted", EIO},
  149. {"corrupted super block", EIO},
  150. {"protocol botch", EPROTO},
  151. {"file system is full", ENOSPC},
  152. {"file is in use", EAGAIN},
  153. {"directory entry is not allocated", ENOENT},
  154. {"file is read only", EROFS},
  155. {"file has been removed", EIDRM},
  156. {"only support truncation to zero length", EPERM},
  157. {"cannot remove root", EPERM},
  158. {"file too big", EFBIG},
  159. {"venti i/o error", EIO},
  160. /* these are not errors */
  161. {"u9fs rhostsauth: no authentication required", 0},
  162. {"u9fs authnone: no authentication required", 0},
  163. {NULL, -1}
  164. };
  165. /**
  166. * p9_error_init - preload mappings into hash list
  167. *
  168. */
  169. int p9_error_init(void)
  170. {
  171. struct errormap *c;
  172. u32 hash;
  173. /* load initial error map into hash table */
  174. for (c = errmap; c->name; c++) {
  175. c->namelen = strlen(c->name);
  176. hash = jhash(c->name, c->namelen, 0);
  177. INIT_HLIST_NODE(&c->list);
  178. hash_add(hash_errmap, &c->list, hash);
  179. }
  180. return 1;
  181. }
  182. EXPORT_SYMBOL(p9_error_init);
  183. /**
  184. * p9_errstr2errno - convert error string to error number
  185. * @errstr: error string
  186. * @len: length of error string
  187. *
  188. */
  189. int p9_errstr2errno(char *errstr, int len)
  190. {
  191. int errno;
  192. struct errormap *c;
  193. u32 hash;
  194. errno = 0;
  195. c = NULL;
  196. hash = jhash(errstr, len, 0);
  197. hash_for_each_possible(hash_errmap, c, list, hash) {
  198. if (c->namelen == len && !memcmp(c->name, errstr, len)) {
  199. errno = c->val;
  200. break;
  201. }
  202. }
  203. if (errno == 0) {
  204. /* TODO: if error isn't found, add it dynamically */
  205. errstr[len] = 0;
  206. pr_err("%s: server reported unknown error %s\n",
  207. __func__, errstr);
  208. errno = ESERVERFAULT;
  209. }
  210. return -errno;
  211. }
  212. EXPORT_SYMBOL(p9_errstr2errno);