error.c 7.4 KB

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