error.c 7.4 KB

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