error.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to:
  25. * Free Software Foundation
  26. * 51 Franklin Street, Fifth Floor
  27. * Boston, MA 02111-1301 USA
  28. *
  29. */
  30. #include <linux/config.h>
  31. #include <linux/module.h>
  32. #include <linux/list.h>
  33. #include <linux/jhash.h>
  34. #include "debug.h"
  35. #include "error.h"
  36. /**
  37. * v9fs_error_init - preload
  38. * @errstr: error string
  39. *
  40. */
  41. int v9fs_error_init(void)
  42. {
  43. struct errormap *c;
  44. int bucket;
  45. /* initialize hash table */
  46. for (bucket = 0; bucket < ERRHASHSZ; bucket++)
  47. INIT_HLIST_HEAD(&hash_errmap[bucket]);
  48. /* load initial error map into hash table */
  49. for (c = errmap; c->name != NULL; c++) {
  50. c->namelen = strlen(c->name);
  51. bucket = jhash(c->name, c->namelen, 0) % ERRHASHSZ;
  52. INIT_HLIST_NODE(&c->list);
  53. hlist_add_head(&c->list, &hash_errmap[bucket]);
  54. }
  55. return 1;
  56. }
  57. /**
  58. * errstr2errno - convert error string to error number
  59. * @errstr: error string
  60. *
  61. */
  62. int v9fs_errstr2errno(char *errstr, int len)
  63. {
  64. int errno = 0;
  65. struct hlist_node *p = NULL;
  66. struct errormap *c = NULL;
  67. int bucket = jhash(errstr, len, 0) % ERRHASHSZ;
  68. hlist_for_each_entry(c, p, &hash_errmap[bucket], list) {
  69. if (c->namelen==len && !memcmp(c->name, errstr, len)) {
  70. errno = c->val;
  71. break;
  72. }
  73. }
  74. if (errno == 0) {
  75. /* TODO: if error isn't found, add it dynamically */
  76. printk(KERN_ERR "%s: errstr :%s: not found\n", __FUNCTION__,
  77. errstr);
  78. errno = 1;
  79. }
  80. return -errno;
  81. }