error.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/config.h>
  30. #include <linux/module.h>
  31. #include <linux/list.h>
  32. #include <linux/jhash.h>
  33. #include "debug.h"
  34. #include "error.h"
  35. /**
  36. * v9fs_error_init - preload
  37. * @errstr: error string
  38. *
  39. */
  40. int v9fs_error_init(void)
  41. {
  42. struct errormap *c;
  43. int bucket;
  44. /* initialize hash table */
  45. for (bucket = 0; bucket < ERRHASHSZ; bucket++)
  46. INIT_HLIST_HEAD(&hash_errmap[bucket]);
  47. /* load initial error map into hash table */
  48. for (c = errmap; c->name != NULL; c++) {
  49. c->namelen = strlen(c->name);
  50. bucket = jhash(c->name, c->namelen, 0) % ERRHASHSZ;
  51. INIT_HLIST_NODE(&c->list);
  52. hlist_add_head(&c->list, &hash_errmap[bucket]);
  53. }
  54. return 1;
  55. }
  56. /**
  57. * errstr2errno - convert error string to error number
  58. * @errstr: error string
  59. *
  60. */
  61. int v9fs_errstr2errno(char *errstr, int len)
  62. {
  63. int errno = 0;
  64. struct hlist_node *p = NULL;
  65. struct errormap *c = NULL;
  66. int bucket = jhash(errstr, len, 0) % ERRHASHSZ;
  67. hlist_for_each_entry(c, p, &hash_errmap[bucket], list) {
  68. if (c->namelen==len && !memcmp(c->name, errstr, len)) {
  69. errno = c->val;
  70. break;
  71. }
  72. }
  73. if (errno == 0) {
  74. /* TODO: if error isn't found, add it dynamically */
  75. printk(KERN_ERR "%s: errstr :%s: not found\n", __FUNCTION__,
  76. errstr);
  77. errno = 1;
  78. }
  79. return -errno;
  80. }