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