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