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