djgpp-killer-20060801
[openafs.git] / src / WINNT / afsd / cm_utils.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #include <errno.h>
14 #include <windows.h>
15 #include <winsock2.h>
16 #ifndef EWOULDBLOCK
17 #define EWOULDBLOCK             WSAEWOULDBLOCK
18 #define EINPROGRESS             WSAEINPROGRESS
19 #define EALREADY                WSAEALREADY
20 #define ENOTSOCK                WSAENOTSOCK
21 #define EDESTADDRREQ            WSAEDESTADDRREQ
22 #define EMSGSIZE                WSAEMSGSIZE
23 #define EPROTOTYPE              WSAEPROTOTYPE
24 #define ENOPROTOOPT             WSAENOPROTOOPT
25 #define EPROTONOSUPPORT         WSAEPROTONOSUPPORT
26 #define ESOCKTNOSUPPORT         WSAESOCKTNOSUPPORT
27 #define EOPNOTSUPP              WSAEOPNOTSUPP
28 #define EPFNOSUPPORT            WSAEPFNOSUPPORT
29 #define EAFNOSUPPORT            WSAEAFNOSUPPORT
30 #define EADDRINUSE              WSAEADDRINUSE
31 #define EADDRNOTAVAIL           WSAEADDRNOTAVAIL
32 #define ENETDOWN                WSAENETDOWN
33 #define ENETUNREACH             WSAENETUNREACH
34 #define ENETRESET               WSAENETRESET
35 #define ECONNABORTED            WSAECONNABORTED
36 #define ECONNRESET              WSAECONNRESET
37 #define ENOBUFS                 WSAENOBUFS
38 #define EISCONN                 WSAEISCONN
39 #define ENOTCONN                WSAENOTCONN
40 #define ESHUTDOWN               WSAESHUTDOWN
41 #define ETOOMANYREFS            WSAETOOMANYREFS
42 #define ETIMEDOUT               WSAETIMEDOUT
43 #define ECONNREFUSED            WSAECONNREFUSED
44 #define ELOOP                   WSAELOOP
45 #ifdef ENAMETOOLONG
46 #undef ENAMETOOLONG
47 #endif
48 #define ENAMETOOLONG            WSAENAMETOOLONG
49 #define EHOSTDOWN               WSAEHOSTDOWN
50 #define EHOSTUNREACH            WSAEHOSTUNREACH
51 #ifdef ENOTEMPTY
52 #undef ENOTEMPTY
53 #endif 
54 #define ENOTEMPTY               WSAENOTEMPTY
55 #define EPROCLIM                WSAEPROCLIM
56 #define EUSERS                  WSAEUSERS
57 #define EDQUOT                  WSAEDQUOT
58 #define ESTALE                  WSAESTALE
59 #define EREMOTE                 WSAEREMOTE
60 #endif /* EWOULDBLOCK */
61 #include <afs/unified_afs.h>
62
63 #include <string.h>
64 #include <malloc.h>
65 #include "afsd.h"
66 #include <osi.h>
67 #include <rx/rx.h>
68
69
70 static osi_once_t cm_utilsOnce;
71
72 osi_rwlock_t cm_utilsLock;
73
74 cm_space_t *cm_spaceListp;
75
76 static int et2sys[512];
77
78 void
79 init_et_to_sys_error(void)
80 {
81     memset(&et2sys, 0, sizeof(et2sys));
82     et2sys[(UAEPERM - ERROR_TABLE_BASE_uae)] = EPERM;
83     et2sys[(UAENOENT - ERROR_TABLE_BASE_uae)] = ENOENT;
84     et2sys[(UAESRCH - ERROR_TABLE_BASE_uae)] = ESRCH;
85     et2sys[(UAEINTR - ERROR_TABLE_BASE_uae)] = EINTR;
86     et2sys[(UAEIO - ERROR_TABLE_BASE_uae)] = EIO;
87     et2sys[(UAENXIO - ERROR_TABLE_BASE_uae)] = ENXIO;
88     et2sys[(UAE2BIG - ERROR_TABLE_BASE_uae)] = E2BIG;
89     et2sys[(UAENOEXEC - ERROR_TABLE_BASE_uae)] = ENOEXEC;
90     et2sys[(UAEBADF - ERROR_TABLE_BASE_uae)] = EBADF;
91     et2sys[(UAECHILD - ERROR_TABLE_BASE_uae)] = ECHILD;
92     et2sys[(UAEAGAIN - ERROR_TABLE_BASE_uae)] = EAGAIN;
93     et2sys[(UAENOMEM - ERROR_TABLE_BASE_uae)] = ENOMEM;
94     et2sys[(UAEACCES - ERROR_TABLE_BASE_uae)] = EACCES;
95     et2sys[(UAEFAULT - ERROR_TABLE_BASE_uae)] = EFAULT;
96     et2sys[(UAENOTBLK - ERROR_TABLE_BASE_uae)] = ENOTBLK;
97     et2sys[(UAEBUSY - ERROR_TABLE_BASE_uae)] = EBUSY;
98     et2sys[(UAEEXIST - ERROR_TABLE_BASE_uae)] = EEXIST;
99     et2sys[(UAEXDEV - ERROR_TABLE_BASE_uae)] = EXDEV;
100     et2sys[(UAENODEV - ERROR_TABLE_BASE_uae)] = ENODEV;
101     et2sys[(UAENOTDIR - ERROR_TABLE_BASE_uae)] = ENOTDIR;
102     et2sys[(UAEISDIR - ERROR_TABLE_BASE_uae)] = EISDIR;
103     et2sys[(UAEINVAL - ERROR_TABLE_BASE_uae)] = EINVAL;
104     et2sys[(UAENFILE - ERROR_TABLE_BASE_uae)] = ENFILE;
105     et2sys[(UAEMFILE - ERROR_TABLE_BASE_uae)] = EMFILE;
106     et2sys[(UAENOTTY - ERROR_TABLE_BASE_uae)] = ENOTTY;
107     et2sys[(UAETXTBSY - ERROR_TABLE_BASE_uae)] = ETXTBSY;
108     et2sys[(UAEFBIG - ERROR_TABLE_BASE_uae)] = EFBIG;
109     et2sys[(UAENOSPC - ERROR_TABLE_BASE_uae)] = ENOSPC;
110     et2sys[(UAESPIPE - ERROR_TABLE_BASE_uae)] = ESPIPE;
111     et2sys[(UAEROFS - ERROR_TABLE_BASE_uae)] = EROFS;
112     et2sys[(UAEMLINK - ERROR_TABLE_BASE_uae)] = EMLINK;
113     et2sys[(UAEPIPE - ERROR_TABLE_BASE_uae)] = EPIPE;
114     et2sys[(UAEDOM - ERROR_TABLE_BASE_uae)] = EDOM;
115     et2sys[(UAERANGE - ERROR_TABLE_BASE_uae)] = ERANGE;
116     et2sys[(UAEDEADLK - ERROR_TABLE_BASE_uae)] = EDEADLK;
117     et2sys[(UAENAMETOOLONG - ERROR_TABLE_BASE_uae)] = ENAMETOOLONG;
118     et2sys[(UAENOLCK - ERROR_TABLE_BASE_uae)] = ENOLCK;
119     et2sys[(UAENOSYS - ERROR_TABLE_BASE_uae)] = ENOSYS;
120     et2sys[(UAENOTEMPTY - ERROR_TABLE_BASE_uae)] = ENOTEMPTY;
121     et2sys[(UAELOOP - ERROR_TABLE_BASE_uae)] = ELOOP;
122     et2sys[(UAEWOULDBLOCK - ERROR_TABLE_BASE_uae)] = EWOULDBLOCK;
123     et2sys[(UAENOMSG - ERROR_TABLE_BASE_uae)] = ENOMSG;
124     et2sys[(UAEIDRM - ERROR_TABLE_BASE_uae)] = EIDRM;
125     et2sys[(UAECHRNG - ERROR_TABLE_BASE_uae)] = ECHRNG;
126     et2sys[(UAEL2NSYNC - ERROR_TABLE_BASE_uae)] = EL2NSYNC;
127     et2sys[(UAEL3HLT - ERROR_TABLE_BASE_uae)] = EL3HLT;
128     et2sys[(UAEL3RST - ERROR_TABLE_BASE_uae)] = EL3RST;
129     et2sys[(UAELNRNG - ERROR_TABLE_BASE_uae)] = ELNRNG;
130     et2sys[(UAEUNATCH - ERROR_TABLE_BASE_uae)] = EUNATCH;
131     et2sys[(UAENOCSI - ERROR_TABLE_BASE_uae)] = ENOCSI;
132     et2sys[(UAEL2HLT - ERROR_TABLE_BASE_uae)] = EL2HLT;
133     et2sys[(UAEBADE - ERROR_TABLE_BASE_uae)] = EBADE;
134     et2sys[(UAEBADR - ERROR_TABLE_BASE_uae)] = EBADR;
135     et2sys[(UAEXFULL - ERROR_TABLE_BASE_uae)] = EXFULL;
136     et2sys[(UAENOANO - ERROR_TABLE_BASE_uae)] = ENOANO;
137     et2sys[(UAEBADRQC - ERROR_TABLE_BASE_uae)] = EBADRQC;
138     et2sys[(UAEBADSLT - ERROR_TABLE_BASE_uae)] = EBADSLT;
139     et2sys[(UAEBFONT - ERROR_TABLE_BASE_uae)] = EBFONT;
140     et2sys[(UAENOSTR - ERROR_TABLE_BASE_uae)] = ENOSTR;
141     et2sys[(UAENODATA - ERROR_TABLE_BASE_uae)] = ENODATA;
142     et2sys[(UAETIME - ERROR_TABLE_BASE_uae)] = ETIME;
143     et2sys[(UAENOSR - ERROR_TABLE_BASE_uae)] = ENOSR;
144     et2sys[(UAENONET - ERROR_TABLE_BASE_uae)] = ENONET;
145     et2sys[(UAENOPKG - ERROR_TABLE_BASE_uae)] = ENOPKG;
146     et2sys[(UAEREMOTE - ERROR_TABLE_BASE_uae)] = EREMOTE;
147     et2sys[(UAENOLINK - ERROR_TABLE_BASE_uae)] = ENOLINK;
148     et2sys[(UAEADV - ERROR_TABLE_BASE_uae)] = EADV;
149     et2sys[(UAESRMNT - ERROR_TABLE_BASE_uae)] = ESRMNT;
150     et2sys[(UAECOMM - ERROR_TABLE_BASE_uae)] = ECOMM;
151     et2sys[(UAEPROTO - ERROR_TABLE_BASE_uae)] = EPROTO;
152     et2sys[(UAEMULTIHOP - ERROR_TABLE_BASE_uae)] = EMULTIHOP;
153     et2sys[(UAEDOTDOT - ERROR_TABLE_BASE_uae)] = EDOTDOT;
154     et2sys[(UAEBADMSG - ERROR_TABLE_BASE_uae)] = EBADMSG;
155     et2sys[(UAEOVERFLOW - ERROR_TABLE_BASE_uae)] = EOVERFLOW;
156     et2sys[(UAENOTUNIQ - ERROR_TABLE_BASE_uae)] = ENOTUNIQ;
157     et2sys[(UAEBADFD - ERROR_TABLE_BASE_uae)] = EBADFD;
158     et2sys[(UAEREMCHG - ERROR_TABLE_BASE_uae)] = EREMCHG;
159     et2sys[(UAELIBACC - ERROR_TABLE_BASE_uae)] = ELIBACC;
160     et2sys[(UAELIBBAD - ERROR_TABLE_BASE_uae)] = ELIBBAD;
161     et2sys[(UAELIBSCN - ERROR_TABLE_BASE_uae)] = ELIBSCN;
162     et2sys[(UAELIBMAX - ERROR_TABLE_BASE_uae)] = ELIBMAX;
163     et2sys[(UAELIBEXEC - ERROR_TABLE_BASE_uae)] = ELIBEXEC;
164     et2sys[(UAEILSEQ - ERROR_TABLE_BASE_uae)] = EILSEQ;
165     et2sys[(UAERESTART - ERROR_TABLE_BASE_uae)] = ERESTART;
166     et2sys[(UAESTRPIPE - ERROR_TABLE_BASE_uae)] = ESTRPIPE;
167     et2sys[(UAEUSERS - ERROR_TABLE_BASE_uae)] = EUSERS;
168     et2sys[(UAENOTSOCK - ERROR_TABLE_BASE_uae)] = ENOTSOCK;
169     et2sys[(UAEDESTADDRREQ - ERROR_TABLE_BASE_uae)] = EDESTADDRREQ;
170     et2sys[(UAEMSGSIZE - ERROR_TABLE_BASE_uae)] = EMSGSIZE;
171     et2sys[(UAEPROTOTYPE - ERROR_TABLE_BASE_uae)] = EPROTOTYPE;
172     et2sys[(UAENOPROTOOPT - ERROR_TABLE_BASE_uae)] = ENOPROTOOPT;
173     et2sys[(UAEPROTONOSUPPORT - ERROR_TABLE_BASE_uae)] = EPROTONOSUPPORT;
174     et2sys[(UAESOCKTNOSUPPORT - ERROR_TABLE_BASE_uae)] = ESOCKTNOSUPPORT;
175     et2sys[(UAEOPNOTSUPP - ERROR_TABLE_BASE_uae)] = EOPNOTSUPP;
176     et2sys[(UAEPFNOSUPPORT - ERROR_TABLE_BASE_uae)] = EPFNOSUPPORT;
177     et2sys[(UAEAFNOSUPPORT - ERROR_TABLE_BASE_uae)] = EAFNOSUPPORT;
178     et2sys[(UAEADDRINUSE - ERROR_TABLE_BASE_uae)] = EADDRINUSE;
179     et2sys[(UAEADDRNOTAVAIL - ERROR_TABLE_BASE_uae)] = EADDRNOTAVAIL;
180     et2sys[(UAENETDOWN - ERROR_TABLE_BASE_uae)] = ENETDOWN;
181     et2sys[(UAENETUNREACH - ERROR_TABLE_BASE_uae)] = ENETUNREACH;
182     et2sys[(UAENETRESET - ERROR_TABLE_BASE_uae)] = ENETRESET;
183     et2sys[(UAECONNABORTED - ERROR_TABLE_BASE_uae)] = ECONNABORTED;
184     et2sys[(UAECONNRESET - ERROR_TABLE_BASE_uae)] = ECONNRESET;
185     et2sys[(UAENOBUFS - ERROR_TABLE_BASE_uae)] = ENOBUFS;
186     et2sys[(UAEISCONN - ERROR_TABLE_BASE_uae)] = EISCONN;
187     et2sys[(UAENOTCONN - ERROR_TABLE_BASE_uae)] = ENOTCONN;
188     et2sys[(UAESHUTDOWN - ERROR_TABLE_BASE_uae)] = ESHUTDOWN;
189     et2sys[(UAETOOMANYREFS - ERROR_TABLE_BASE_uae)] = ETOOMANYREFS;
190     et2sys[(UAETIMEDOUT - ERROR_TABLE_BASE_uae)] = ETIMEDOUT;
191     et2sys[(UAECONNREFUSED - ERROR_TABLE_BASE_uae)] = ECONNREFUSED;
192     et2sys[(UAEHOSTDOWN - ERROR_TABLE_BASE_uae)] = EHOSTDOWN;
193     et2sys[(UAEHOSTUNREACH - ERROR_TABLE_BASE_uae)] = EHOSTUNREACH;
194     et2sys[(UAEALREADY - ERROR_TABLE_BASE_uae)] = EALREADY;
195     et2sys[(UAEINPROGRESS - ERROR_TABLE_BASE_uae)] = EINPROGRESS;
196     et2sys[(UAESTALE - ERROR_TABLE_BASE_uae)] = ESTALE;
197     et2sys[(UAEUCLEAN - ERROR_TABLE_BASE_uae)] = EUCLEAN;
198     et2sys[(UAENOTNAM - ERROR_TABLE_BASE_uae)] = ENOTNAM;
199     et2sys[(UAENAVAIL - ERROR_TABLE_BASE_uae)] = ENAVAIL;
200     et2sys[(UAEISNAM - ERROR_TABLE_BASE_uae)] = EISNAM;
201     et2sys[(UAEREMOTEIO - ERROR_TABLE_BASE_uae)] = EREMOTEIO;
202     et2sys[(UAEDQUOT - ERROR_TABLE_BASE_uae)] = EDQUOT;
203     et2sys[(UAENOMEDIUM - ERROR_TABLE_BASE_uae)] = ENOMEDIUM;
204     et2sys[(UAEMEDIUMTYPE - ERROR_TABLE_BASE_uae)] = EMEDIUMTYPE;
205 }
206
207 static afs_int32
208 et_to_sys_error(afs_int32 in)
209 {
210     if (in < ERROR_TABLE_BASE_uae || in >= ERROR_TABLE_BASE_uae + 512)
211         return in;
212     if (et2sys[in - ERROR_TABLE_BASE_uae] != 0)
213         return et2sys[in - ERROR_TABLE_BASE_uae];
214     return in;
215 }
216
217 long cm_MapRPCError(long error, cm_req_t *reqp)
218 {
219     if (error == 0) 
220         return 0;
221
222     /* If we had to stop retrying, report our saved error code. */
223     if (reqp && error == CM_ERROR_TIMEDOUT) {
224         if (reqp->accessError)
225             return reqp->accessError;
226         if (reqp->volumeError)
227             return reqp->volumeError;
228         if (reqp->rpcError)
229             return reqp->rpcError;
230         return error;
231     }
232
233     error = et_to_sys_error(error);
234
235     if (error < 0) 
236         error = CM_ERROR_TIMEDOUT;
237     else if (error == 30) 
238         error = CM_ERROR_READONLY;
239     else if (error == 13) 
240         error = CM_ERROR_NOACCESS;
241     else if (error == 18) 
242         error = CM_ERROR_CROSSDEVLINK;
243     else if (error == 17) 
244         error = CM_ERROR_EXISTS;
245     else if (error == 20) 
246         error = CM_ERROR_NOTDIR;
247     else if (error == 2)        /* ENOENT */
248         error = CM_ERROR_NOSUCHFILE;
249     else if (error == 11        /* EAGAIN, most servers */
250              || error == 35)    /* EAGAIN, Digital UNIX */
251         error = CM_ERROR_WOULDBLOCK;
252     else if (error == VDISKFULL
253               || error == 28)   /* ENOSPC */ 
254         error = CM_ERROR_SPACE;
255     else if (error == VOVERQUOTA
256               || error == 49    /* EDQUOT on Solaris */
257               || error == 88    /* EDQUOT on AIX */
258               || error == 69    /* EDQUOT on Digital UNIX and HPUX */
259               || error == 122   /* EDQUOT on Linux */
260               || error == 1133) /* EDQUOT on Irix  */
261         error = CM_ERROR_QUOTA;
262     else if (error == VNOVNODE) {
263 #ifdef COMMENT
264         error = CM_ERROR_BADFD;
265 #else
266         error = CM_ERROR_RETRY;
267 #endif
268     } else if (error == 21)
269         return CM_ERROR_ISDIR;
270     return error;
271 }
272
273 long cm_MapRPCErrorRmdir(long error, cm_req_t *reqp)
274 {
275     if (error == 0) 
276         return 0;
277
278     /* If we had to stop retrying, report our saved error code. */
279     if (reqp && error == CM_ERROR_TIMEDOUT) {
280         if (reqp->accessError)
281             return reqp->accessError;
282         if (reqp->volumeError)
283             return reqp->volumeError;
284         if (reqp->rpcError)
285             return reqp->rpcError;
286         return error;
287     }
288
289     if (error < 0) 
290         error = CM_ERROR_TIMEDOUT;
291     else if (error == 30) 
292         error = CM_ERROR_READONLY;
293     else if (error == 20) 
294         error = CM_ERROR_NOTDIR;
295     else if (error == 13) 
296         error = CM_ERROR_NOACCESS;
297     else if (error == 2) 
298         error = CM_ERROR_NOSUCHFILE;
299     else if (error == 17                /* AIX */
300               || error == 66            /* SunOS 4, Digital UNIX */
301               || error == 93            /* Solaris 2, IRIX */
302               || error == 247)  /* HP/UX */
303         error = CM_ERROR_NOTEMPTY;
304     return error;
305 }       
306
307 long cm_MapVLRPCError(long error, cm_req_t *reqp)
308 {
309         if (error == 0) return 0;
310
311         /* If we had to stop retrying, report our saved error code. */
312         if (reqp && error == CM_ERROR_TIMEDOUT) {
313                 if (reqp->accessError)
314                         return reqp->accessError;
315                 if (reqp->volumeError)
316                         return reqp->volumeError;
317                 if (reqp->rpcError)
318                         return reqp->rpcError;
319                 return error;
320         }
321
322         if (error < 0) 
323             error = CM_ERROR_TIMEDOUT;
324         else if (error == VL_NOENT) 
325             error = CM_ERROR_NOSUCHVOLUME;
326         return error;
327 }
328
329 cm_space_t *cm_GetSpace(void)
330 {
331         cm_space_t *tsp;
332
333         if (osi_Once(&cm_utilsOnce)) {
334                 lock_InitializeRWLock(&cm_utilsLock, "cm_utilsLock");
335                 osi_EndOnce(&cm_utilsOnce);
336         }
337         
338         lock_ObtainWrite(&cm_utilsLock);
339         if (tsp = cm_spaceListp) {
340                 cm_spaceListp = tsp->nextp;
341         }
342         else tsp = (cm_space_t *) malloc(sizeof(cm_space_t));
343         (void) memset(tsp, 0, sizeof(cm_space_t));
344         lock_ReleaseWrite(&cm_utilsLock);
345         
346         return tsp;
347 }
348
349 void cm_FreeSpace(cm_space_t *tsp)
350 {
351         lock_ObtainWrite(&cm_utilsLock);
352         tsp->nextp = cm_spaceListp;
353         cm_spaceListp = tsp;
354         lock_ReleaseWrite(&cm_utilsLock);
355 }