error.c 6.8 KB

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