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