blob: 981fe8ecd78045b315c1709502590d50dd6fb213 [file] [log] [blame]
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -07001/*
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
Eric Van Hensbergen42e8c502006-03-25 03:07:28 -080014 * it under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation.
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070016 *
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
30#include <linux/config.h>
31#include <linux/module.h>
32
33#include <linux/list.h>
34#include <linux/jhash.h>
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070035
36#include "debug.h"
37#include "error.h"
38
39/**
40 * v9fs_error_init - preload
41 * @errstr: error string
42 *
43 */
44
45int v9fs_error_init(void)
46{
47 struct errormap *c;
48 int bucket;
49
50 /* initialize hash table */
51 for (bucket = 0; bucket < ERRHASHSZ; bucket++)
52 INIT_HLIST_HEAD(&hash_errmap[bucket]);
53
54 /* load initial error map into hash table */
55 for (c = errmap; c->name != NULL; c++) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -080056 c->namelen = strlen(c->name);
57 bucket = jhash(c->name, c->namelen, 0) % ERRHASHSZ;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070058 INIT_HLIST_NODE(&c->list);
59 hlist_add_head(&c->list, &hash_errmap[bucket]);
60 }
61
62 return 1;
63}
64
65/**
66 * errstr2errno - convert error string to error number
67 * @errstr: error string
68 *
69 */
70
Latchesar Ionkov531b1092006-01-08 01:05:00 -080071int v9fs_errstr2errno(char *errstr, int len)
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070072{
73 int errno = 0;
74 struct hlist_node *p = NULL;
75 struct errormap *c = NULL;
Latchesar Ionkov531b1092006-01-08 01:05:00 -080076 int bucket = jhash(errstr, len, 0) % ERRHASHSZ;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070077
78 hlist_for_each_entry(c, p, &hash_errmap[bucket], list) {
Latchesar Ionkov531b1092006-01-08 01:05:00 -080079 if (c->namelen==len && !memcmp(c->name, errstr, len)) {
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070080 errno = c->val;
81 break;
82 }
83 }
84
85 if (errno == 0) {
86 /* TODO: if error isn't found, add it dynamically */
87 printk(KERN_ERR "%s: errstr :%s: not found\n", __FUNCTION__,
88 errstr);
89 errno = 1;
90 }
91
92 return -errno;
93}