xref: /linux/fs/afs/misc.c (revision b7019ac550eb3916f34d79db583e9b7ea2524afa)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* miscellaneous bits
3  *
4  * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/errno.h>
11 #include "internal.h"
12 #include "afs_fs.h"
13 
14 /*
15  * convert an AFS abort code to a Linux error number
16  */
17 int afs_abort_to_error(u32 abort_code)
18 {
19 	switch (abort_code) {
20 		/* Low errno codes inserted into abort namespace */
21 	case 13:		return -EACCES;
22 	case 27:		return -EFBIG;
23 	case 30:		return -EROFS;
24 
25 		/* VICE "special error" codes; 101 - 111 */
26 	case VSALVAGE:		return -EIO;
27 	case VNOVNODE:		return -ENOENT;
28 	case VNOVOL:		return -ENOMEDIUM;
29 	case VVOLEXISTS:	return -EEXIST;
30 	case VNOSERVICE:	return -EIO;
31 	case VOFFLINE:		return -ENOENT;
32 	case VONLINE:		return -EEXIST;
33 	case VDISKFULL:		return -ENOSPC;
34 	case VOVERQUOTA:	return -EDQUOT;
35 	case VBUSY:		return -EBUSY;
36 	case VMOVED:		return -ENXIO;
37 
38 		/* Volume Location server errors */
39 	case AFSVL_IDEXIST:		return -EEXIST;
40 	case AFSVL_IO:			return -EREMOTEIO;
41 	case AFSVL_NAMEEXIST:		return -EEXIST;
42 	case AFSVL_CREATEFAIL:		return -EREMOTEIO;
43 	case AFSVL_NOENT:		return -ENOMEDIUM;
44 	case AFSVL_EMPTY:		return -ENOMEDIUM;
45 	case AFSVL_ENTDELETED:		return -ENOMEDIUM;
46 	case AFSVL_BADNAME:		return -EINVAL;
47 	case AFSVL_BADINDEX:		return -EINVAL;
48 	case AFSVL_BADVOLTYPE:		return -EINVAL;
49 	case AFSVL_BADSERVER:		return -EINVAL;
50 	case AFSVL_BADPARTITION:	return -EINVAL;
51 	case AFSVL_REPSFULL:		return -EFBIG;
52 	case AFSVL_NOREPSERVER:		return -ENOENT;
53 	case AFSVL_DUPREPSERVER:	return -EEXIST;
54 	case AFSVL_RWNOTFOUND:		return -ENOENT;
55 	case AFSVL_BADREFCOUNT:		return -EINVAL;
56 	case AFSVL_SIZEEXCEEDED:	return -EINVAL;
57 	case AFSVL_BADENTRY:		return -EINVAL;
58 	case AFSVL_BADVOLIDBUMP:	return -EINVAL;
59 	case AFSVL_IDALREADYHASHED:	return -EINVAL;
60 	case AFSVL_ENTRYLOCKED:		return -EBUSY;
61 	case AFSVL_BADVOLOPER:		return -EBADRQC;
62 	case AFSVL_BADRELLOCKTYPE:	return -EINVAL;
63 	case AFSVL_RERELEASE:		return -EREMOTEIO;
64 	case AFSVL_BADSERVERFLAG:	return -EINVAL;
65 	case AFSVL_PERM:		return -EACCES;
66 	case AFSVL_NOMEM:		return -EREMOTEIO;
67 
68 		/* Unified AFS error table; ET "uae" == 0x2f6df00 */
69 	case 0x2f6df00:		return -EPERM;
70 	case 0x2f6df01:		return -ENOENT;
71 	case 0x2f6df04:		return -EIO;
72 	case 0x2f6df0a:		return -EAGAIN;
73 	case 0x2f6df0b:		return -ENOMEM;
74 	case 0x2f6df0c:		return -EACCES;
75 	case 0x2f6df0f:		return -EBUSY;
76 	case 0x2f6df10:		return -EEXIST;
77 	case 0x2f6df11:		return -EXDEV;
78 	case 0x2f6df12:		return -ENODEV;
79 	case 0x2f6df13:		return -ENOTDIR;
80 	case 0x2f6df14:		return -EISDIR;
81 	case 0x2f6df15:		return -EINVAL;
82 	case 0x2f6df1a:		return -EFBIG;
83 	case 0x2f6df1b:		return -ENOSPC;
84 	case 0x2f6df1d:		return -EROFS;
85 	case 0x2f6df1e:		return -EMLINK;
86 	case 0x2f6df20:		return -EDOM;
87 	case 0x2f6df21:		return -ERANGE;
88 	case 0x2f6df22:		return -EDEADLK;
89 	case 0x2f6df23:		return -ENAMETOOLONG;
90 	case 0x2f6df24:		return -ENOLCK;
91 	case 0x2f6df26:		return -ENOTEMPTY;
92 	case 0x2f6df28:		return -EWOULDBLOCK;
93 	case 0x2f6df69:		return -ENOTCONN;
94 	case 0x2f6df6c:		return -ETIMEDOUT;
95 	case 0x2f6df78:		return -EDQUOT;
96 
97 		/* RXKAD abort codes; from include/rxrpc/packet.h.  ET "RXK" == 0x1260B00 */
98 	case RXKADINCONSISTENCY: return -EPROTO;
99 	case RXKADPACKETSHORT:	return -EPROTO;
100 	case RXKADLEVELFAIL:	return -EKEYREJECTED;
101 	case RXKADTICKETLEN:	return -EKEYREJECTED;
102 	case RXKADOUTOFSEQUENCE: return -EPROTO;
103 	case RXKADNOAUTH:	return -EKEYREJECTED;
104 	case RXKADBADKEY:	return -EKEYREJECTED;
105 	case RXKADBADTICKET:	return -EKEYREJECTED;
106 	case RXKADUNKNOWNKEY:	return -EKEYREJECTED;
107 	case RXKADEXPIRED:	return -EKEYEXPIRED;
108 	case RXKADSEALEDINCON:	return -EKEYREJECTED;
109 	case RXKADDATALEN:	return -EKEYREJECTED;
110 	case RXKADILLEGALLEVEL:	return -EKEYREJECTED;
111 
112 	case RXGEN_OPCODE:	return -ENOTSUPP;
113 
114 	default:		return -EREMOTEIO;
115 	}
116 }
117 
118 /*
119  * Select the error to report from a set of errors.
120  */
121 void afs_prioritise_error(struct afs_error *e, int error, u32 abort_code)
122 {
123 	switch (error) {
124 	case 0:
125 		return;
126 	default:
127 		if (e->error == -ETIMEDOUT ||
128 		    e->error == -ETIME)
129 			return;
130 		/* Fall through */
131 	case -ETIMEDOUT:
132 	case -ETIME:
133 		if (e->error == -ENOMEM ||
134 		    e->error == -ENONET)
135 			return;
136 		/* Fall through */
137 	case -ENOMEM:
138 	case -ENONET:
139 		if (e->error == -ERFKILL)
140 			return;
141 		/* Fall through */
142 	case -ERFKILL:
143 		if (e->error == -EADDRNOTAVAIL)
144 			return;
145 		/* Fall through */
146 	case -EADDRNOTAVAIL:
147 		if (e->error == -ENETUNREACH)
148 			return;
149 		/* Fall through */
150 	case -ENETUNREACH:
151 		if (e->error == -EHOSTUNREACH)
152 			return;
153 		/* Fall through */
154 	case -EHOSTUNREACH:
155 		if (e->error == -EHOSTDOWN)
156 			return;
157 		/* Fall through */
158 	case -EHOSTDOWN:
159 		if (e->error == -ECONNREFUSED)
160 			return;
161 		/* Fall through */
162 	case -ECONNREFUSED:
163 		if (e->error == -ECONNRESET)
164 			return;
165 		/* Fall through */
166 	case -ECONNRESET: /* Responded, but call expired. */
167 		if (e->responded)
168 			return;
169 		e->error = error;
170 		return;
171 
172 	case -ECONNABORTED:
173 		e->responded = true;
174 		e->error = afs_abort_to_error(abort_code);
175 		return;
176 	}
177 }
178