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