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