c0705dc73ecf2a7400d44b60e4d8e049c2b25728
[openafs.git] / src / afs / afs_analyze.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 /*
11  * Implements:
12  */
13 #include <afsconfig.h>
14 #include "afs/param.h"
15
16 RCSID
17     ("$Header$");
18
19 #include "afs/stds.h"
20 #include "afs/sysincludes.h"    /* Standard vendor system headers */
21
22 #ifndef UKERNEL
23 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_FBSD_ENV)
24 #include <net/if.h>
25 #include <netinet/in.h>
26 #endif
27
28 #ifdef AFS_SGI62_ENV
29 #include "h/hashing.h"
30 #endif
31 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_FBSD_ENV) && !defined(AFS_DARWIN60_ENV)
32 #include <netinet/in_var.h>
33 #endif
34 #endif /* !UKERNEL */
35
36 #include "afsincludes.h"        /* Afs-based standard headers */
37 #include "afs/afs_stats.h"      /* afs statistics */
38 #include "afs/afs_util.h"
39 #include "afs/unified_afs.h"
40
41 #if     defined(AFS_SUN56_ENV)
42 #include <inet/led.h>
43 #include <inet/common.h>
44 #if     defined(AFS_SUN58_ENV)
45 #include <netinet/ip6.h>
46 #endif
47 #include <inet/ip.h>
48 #endif
49
50
51 /* shouldn't do it this way, but for now will do */
52 #ifndef ERROR_TABLE_BASE_U
53 #define ERROR_TABLE_BASE_U      (5376L)
54 #endif /* ubik error base define */
55
56 /* shouldn't do it this way, but for now will do */
57 #ifndef ERROR_TABLE_BASE_uae
58 #define ERROR_TABLE_BASE_uae    (49733376L)
59 #endif /* unified afs error base define */
60
61 /* same hack for vlserver error base as for ubik error base */
62 #ifndef ERROR_TABLE_BASE_VL
63 #define ERROR_TABLE_BASE_VL     (363520L)
64 #define VL_NOENT                (363524L)
65 #endif /* vlserver error base define */
66
67
68 int afs_BusyWaitPeriod = 15;    /* poll every 15 seconds */
69
70 afs_int32 hm_retry_RO = 0;      /* don't wait */
71 afs_int32 hm_retry_RW = 0;      /* don't wait */
72 afs_int32 hm_retry_int = 0;     /* don't wait */
73
74 static int et2sys[512];
75
76 void
77 init_et_to_sys_error(void)
78 {
79     memset(&et2sys, 0, sizeof(et2sys));
80     et2sys[(UAEPERM - ERROR_TABLE_BASE_uae)] = EPERM;
81     et2sys[(UAENOENT - ERROR_TABLE_BASE_uae)] = ENOENT;
82     et2sys[(UAESRCH - ERROR_TABLE_BASE_uae)] = ESRCH;
83     et2sys[(UAEINTR - ERROR_TABLE_BASE_uae)] = EINTR;
84     et2sys[(UAEIO - ERROR_TABLE_BASE_uae)] = EIO;
85     et2sys[(UAENXIO - ERROR_TABLE_BASE_uae)] = ENXIO;
86     et2sys[(UAE2BIG - ERROR_TABLE_BASE_uae)] = E2BIG;
87     et2sys[(UAENOEXEC - ERROR_TABLE_BASE_uae)] = ENOEXEC;
88     et2sys[(UAEBADF - ERROR_TABLE_BASE_uae)] = EBADF;
89     et2sys[(UAECHILD - ERROR_TABLE_BASE_uae)] = ECHILD;
90     et2sys[(UAEAGAIN - ERROR_TABLE_BASE_uae)] = EAGAIN;
91     et2sys[(UAENOMEM - ERROR_TABLE_BASE_uae)] = ENOMEM;
92     et2sys[(UAEACCES - ERROR_TABLE_BASE_uae)] = EACCES;
93     et2sys[(UAEFAULT - ERROR_TABLE_BASE_uae)] = EFAULT;
94     et2sys[(UAENOTBLK - ERROR_TABLE_BASE_uae)] = ENOTBLK;
95     et2sys[(UAEBUSY - ERROR_TABLE_BASE_uae)] = EBUSY;
96     et2sys[(UAEEXIST - ERROR_TABLE_BASE_uae)] = EEXIST;
97     et2sys[(UAEXDEV - ERROR_TABLE_BASE_uae)] = EXDEV;
98     et2sys[(UAENODEV - ERROR_TABLE_BASE_uae)] = ENODEV;
99     et2sys[(UAENOTDIR - ERROR_TABLE_BASE_uae)] = ENOTDIR;
100     et2sys[(UAEISDIR - ERROR_TABLE_BASE_uae)] = EISDIR;
101     et2sys[(UAEINVAL - ERROR_TABLE_BASE_uae)] = EINVAL;
102     et2sys[(UAENFILE - ERROR_TABLE_BASE_uae)] = ENFILE;
103     et2sys[(UAEMFILE - ERROR_TABLE_BASE_uae)] = EMFILE;
104     et2sys[(UAENOTTY - ERROR_TABLE_BASE_uae)] = ENOTTY;
105     et2sys[(UAETXTBSY - ERROR_TABLE_BASE_uae)] = ETXTBSY;
106     et2sys[(UAEFBIG - ERROR_TABLE_BASE_uae)] = EFBIG;
107     et2sys[(UAENOSPC - ERROR_TABLE_BASE_uae)] = ENOSPC;
108     et2sys[(UAESPIPE - ERROR_TABLE_BASE_uae)] = ESPIPE;
109     et2sys[(UAEROFS - ERROR_TABLE_BASE_uae)] = EROFS;
110     et2sys[(UAEMLINK - ERROR_TABLE_BASE_uae)] = EMLINK;
111     et2sys[(UAEPIPE - ERROR_TABLE_BASE_uae)] = EPIPE;
112     et2sys[(UAEDOM - ERROR_TABLE_BASE_uae)] = EDOM;
113     et2sys[(UAERANGE - ERROR_TABLE_BASE_uae)] = ERANGE;
114     et2sys[(UAEDEADLK - ERROR_TABLE_BASE_uae)] = EDEADLK;
115     et2sys[(UAENAMETOOLONG - ERROR_TABLE_BASE_uae)] = ENAMETOOLONG;
116     et2sys[(UAENOLCK - ERROR_TABLE_BASE_uae)] = ENOLCK;
117     et2sys[(UAENOSYS - ERROR_TABLE_BASE_uae)] = ENOSYS;
118     et2sys[(UAENOTEMPTY - ERROR_TABLE_BASE_uae)] = ENOTEMPTY;
119     et2sys[(UAELOOP - ERROR_TABLE_BASE_uae)] = ELOOP;
120     et2sys[(UAEWOULDBLOCK - ERROR_TABLE_BASE_uae)] = EWOULDBLOCK;
121     et2sys[(UAENOMSG - ERROR_TABLE_BASE_uae)] = ENOMSG;
122     et2sys[(UAEIDRM - ERROR_TABLE_BASE_uae)] = EIDRM;
123     et2sys[(UAECHRNG - ERROR_TABLE_BASE_uae)] = ECHRNG;
124     et2sys[(UAEL2NSYNC - ERROR_TABLE_BASE_uae)] = EL2NSYNC;
125     et2sys[(UAEL3HLT - ERROR_TABLE_BASE_uae)] = EL3HLT;
126     et2sys[(UAEL3RST - ERROR_TABLE_BASE_uae)] = EL3RST;
127     et2sys[(UAELNRNG - ERROR_TABLE_BASE_uae)] = ELNRNG;
128     et2sys[(UAEUNATCH - ERROR_TABLE_BASE_uae)] = EUNATCH;
129     et2sys[(UAENOCSI - ERROR_TABLE_BASE_uae)] = ENOCSI;
130     et2sys[(UAEL2HLT - ERROR_TABLE_BASE_uae)] = EL2HLT;
131     et2sys[(UAEBADE - ERROR_TABLE_BASE_uae)] = EBADE;
132     et2sys[(UAEBADR - ERROR_TABLE_BASE_uae)] = EBADR;
133     et2sys[(UAEXFULL - ERROR_TABLE_BASE_uae)] = EXFULL;
134     et2sys[(UAENOANO - ERROR_TABLE_BASE_uae)] = ENOANO;
135     et2sys[(UAEBADRQC - ERROR_TABLE_BASE_uae)] = EBADRQC;
136     et2sys[(UAEBADSLT - ERROR_TABLE_BASE_uae)] = EBADSLT;
137     et2sys[(UAEBFONT - ERROR_TABLE_BASE_uae)] = EBFONT;
138     et2sys[(UAENOSTR - ERROR_TABLE_BASE_uae)] = ENOSTR;
139     et2sys[(UAENODATA - ERROR_TABLE_BASE_uae)] = ENODATA;
140     et2sys[(UAETIME - ERROR_TABLE_BASE_uae)] = ETIME;
141     et2sys[(UAENOSR - ERROR_TABLE_BASE_uae)] = ENOSR;
142     et2sys[(UAENONET - ERROR_TABLE_BASE_uae)] = ENONET;
143     et2sys[(UAENOPKG - ERROR_TABLE_BASE_uae)] = ENOPKG;
144     et2sys[(UAEREMOTE - ERROR_TABLE_BASE_uae)] = EREMOTE;
145     et2sys[(UAENOLINK - ERROR_TABLE_BASE_uae)] = ENOLINK;
146     et2sys[(UAEADV - ERROR_TABLE_BASE_uae)] = EADV;
147     et2sys[(UAESRMNT - ERROR_TABLE_BASE_uae)] = ESRMNT;
148     et2sys[(UAECOMM - ERROR_TABLE_BASE_uae)] = ECOMM;
149     et2sys[(UAEPROTO - ERROR_TABLE_BASE_uae)] = EPROTO;
150     et2sys[(UAEMULTIHOP - ERROR_TABLE_BASE_uae)] = EMULTIHOP;
151     et2sys[(UAEDOTDOT - ERROR_TABLE_BASE_uae)] = EDOTDOT;
152     et2sys[(UAEBADMSG - ERROR_TABLE_BASE_uae)] = EBADMSG;
153     et2sys[(UAEOVERFLOW - ERROR_TABLE_BASE_uae)] = EOVERFLOW;
154     et2sys[(UAENOTUNIQ - ERROR_TABLE_BASE_uae)] = ENOTUNIQ;
155     et2sys[(UAEBADFD - ERROR_TABLE_BASE_uae)] = EBADFD;
156     et2sys[(UAEREMCHG - ERROR_TABLE_BASE_uae)] = EREMCHG;
157     et2sys[(UAELIBACC - ERROR_TABLE_BASE_uae)] = ELIBACC;
158     et2sys[(UAELIBBAD - ERROR_TABLE_BASE_uae)] = ELIBBAD;
159     et2sys[(UAELIBSCN - ERROR_TABLE_BASE_uae)] = ELIBSCN;
160     et2sys[(UAELIBMAX - ERROR_TABLE_BASE_uae)] = ELIBMAX;
161     et2sys[(UAELIBEXEC - ERROR_TABLE_BASE_uae)] = ELIBEXEC;
162     et2sys[(UAEILSEQ - ERROR_TABLE_BASE_uae)] = EILSEQ;
163     et2sys[(UAERESTART - ERROR_TABLE_BASE_uae)] = ERESTART;
164     et2sys[(UAESTRPIPE - ERROR_TABLE_BASE_uae)] = ESTRPIPE;
165     et2sys[(UAEUSERS - ERROR_TABLE_BASE_uae)] = EUSERS;
166     et2sys[(UAENOTSOCK - ERROR_TABLE_BASE_uae)] = ENOTSOCK;
167     et2sys[(UAEDESTADDRREQ - ERROR_TABLE_BASE_uae)] = EDESTADDRREQ;
168     et2sys[(UAEMSGSIZE - ERROR_TABLE_BASE_uae)] = EMSGSIZE;
169     et2sys[(UAEPROTOTYPE - ERROR_TABLE_BASE_uae)] = EPROTOTYPE;
170     et2sys[(UAENOPROTOOPT - ERROR_TABLE_BASE_uae)] = ENOPROTOOPT;
171     et2sys[(UAEPROTONOSUPPORT - ERROR_TABLE_BASE_uae)] = EPROTONOSUPPORT;
172     et2sys[(UAESOCKTNOSUPPORT - ERROR_TABLE_BASE_uae)] = ESOCKTNOSUPPORT;
173     et2sys[(UAEOPNOTSUPP - ERROR_TABLE_BASE_uae)] = EOPNOTSUPP;
174     et2sys[(UAEPFNOSUPPORT - ERROR_TABLE_BASE_uae)] = EPFNOSUPPORT;
175     et2sys[(UAEAFNOSUPPORT - ERROR_TABLE_BASE_uae)] = EAFNOSUPPORT;
176     et2sys[(UAEADDRINUSE - ERROR_TABLE_BASE_uae)] = EADDRINUSE;
177     et2sys[(UAEADDRNOTAVAIL - ERROR_TABLE_BASE_uae)] = EADDRNOTAVAIL;
178     et2sys[(UAENETDOWN - ERROR_TABLE_BASE_uae)] = ENETDOWN;
179     et2sys[(UAENETUNREACH - ERROR_TABLE_BASE_uae)] = ENETUNREACH;
180     et2sys[(UAENETRESET - ERROR_TABLE_BASE_uae)] = ENETRESET;
181     et2sys[(UAECONNABORTED - ERROR_TABLE_BASE_uae)] = ECONNABORTED;
182     et2sys[(UAECONNRESET - ERROR_TABLE_BASE_uae)] = ECONNRESET;
183     et2sys[(UAENOBUFS - ERROR_TABLE_BASE_uae)] = ENOBUFS;
184     et2sys[(UAEISCONN - ERROR_TABLE_BASE_uae)] = EISCONN;
185     et2sys[(UAENOTCONN - ERROR_TABLE_BASE_uae)] = ENOTCONN;
186     et2sys[(UAESHUTDOWN - ERROR_TABLE_BASE_uae)] = ESHUTDOWN;
187     et2sys[(UAETOOMANYREFS - ERROR_TABLE_BASE_uae)] = ETOOMANYREFS;
188     et2sys[(UAETIMEDOUT - ERROR_TABLE_BASE_uae)] = ETIMEDOUT;
189     et2sys[(UAECONNREFUSED - ERROR_TABLE_BASE_uae)] = ECONNREFUSED;
190     et2sys[(UAEHOSTDOWN - ERROR_TABLE_BASE_uae)] = EHOSTDOWN;
191     et2sys[(UAEHOSTUNREACH - ERROR_TABLE_BASE_uae)] = EHOSTUNREACH;
192     et2sys[(UAEALREADY - ERROR_TABLE_BASE_uae)] = EALREADY;
193     et2sys[(UAEINPROGRESS - ERROR_TABLE_BASE_uae)] = EINPROGRESS;
194     et2sys[(UAESTALE - ERROR_TABLE_BASE_uae)] = ESTALE;
195     et2sys[(UAEUCLEAN - ERROR_TABLE_BASE_uae)] = EUCLEAN;
196     et2sys[(UAENOTNAM - ERROR_TABLE_BASE_uae)] = ENOTNAM;
197     et2sys[(UAENAVAIL - ERROR_TABLE_BASE_uae)] = ENAVAIL;
198     et2sys[(UAEISNAM - ERROR_TABLE_BASE_uae)] = EISNAM;
199     et2sys[(UAEREMOTEIO - ERROR_TABLE_BASE_uae)] = EREMOTEIO;
200     et2sys[(UAEDQUOT - ERROR_TABLE_BASE_uae)] = EDQUOT;
201     et2sys[(UAENOMEDIUM - ERROR_TABLE_BASE_uae)] = ENOMEDIUM;
202     et2sys[(UAEMEDIUMTYPE - ERROR_TABLE_BASE_uae)] = EMEDIUMTYPE;
203 }
204
205 static afs_int32
206 et_to_sys_error(afs_int32 in)
207 {
208     if (in < ERROR_TABLE_BASE_uae || in >= ERROR_TABLE_BASE_uae + 512)
209         return in;
210     if (et2sys[in - ERROR_TABLE_BASE_uae] != 0)
211         return et2sys[in - ERROR_TABLE_BASE_uae];
212     return in;
213 }
214
215 void
216 afs_CopyError(register struct vrequest *afrom, register struct vrequest *ato)
217 {
218     AFS_STATCNT(afs_CopyError);
219     if (!afrom->initd)
220         return;
221     afs_FinalizeReq(ato);
222     if (afrom->accessError)
223         ato->accessError = 1;
224     if (afrom->volumeError)
225         ato->volumeError = 1;
226     if (afrom->networkError)
227         ato->networkError = 1;
228     if (afrom->permWriteError)
229         ato->permWriteError = 1;
230
231 }
232
233 void
234 afs_FinalizeReq(register struct vrequest *areq)
235 {
236     AFS_STATCNT(afs_FinalizeReq);
237     if (areq->initd)
238         return;
239     areq->busyCount = 0;
240     areq->accessError = 0;
241     areq->volumeError = 0;
242     areq->networkError = 0;
243     areq->permWriteError = 0;
244     areq->initd = 1;
245
246 }
247
248 int
249 afs_CheckCode(afs_int32 acode, struct vrequest *areq, int where)
250 {
251     AFS_STATCNT(afs_CheckCode);
252     if (acode) {
253         afs_Trace2(afs_iclSetp, CM_TRACE_CHECKCODE, ICL_TYPE_INT32, acode,
254                    ICL_TYPE_INT32, where);
255     }
256     if ((acode & ~0xff) == ERROR_TABLE_BASE_uae)
257         acode = et_to_sys_error(acode);
258     if (!areq || !areq->initd)
259         return acode;
260     if (areq->networkError)
261         return ETIMEDOUT;
262     if (acode == 0)
263         return 0;
264     if (areq->accessError)
265         return EACCES;
266     if (areq->volumeError == VOLMISSING)
267         return ENODEV;
268     if (areq->volumeError == VOLBUSY)
269         return EWOULDBLOCK;
270     if (acode == VNOVNODE)
271         return ENOENT;
272     if (acode == VDISKFULL)
273         return ENOSPC;
274     if (acode == VOVERQUOTA)
275         return
276 #ifdef EDQUOT
277             EDQUOT
278 #else
279             ENOSPC
280 #endif
281             ;
282
283     return acode;
284
285 }                               /*afs_CheckCode */
286
287
288 #define VSleep(at)      afs_osi_Wait((at)*1000, 0, 0)
289
290
291 int lastcode;
292 /* returns:
293  * 0   if the vldb record for a specific volume is different from what
294  *     we have cached -- perhaps the volume has moved.
295  * 1   if the vldb record is the same
296  * 2   if we can't tell if it's the same or not. 
297  *
298  * If 0, the caller will probably start over at the beginning of our
299  * list of servers for this volume and try to find one that is up.  If
300  * not 0, we will probably just keep plugging with what we have
301  * cached.   If we fail to contact the VL server, we  should just keep
302  * trying with the information we have, rather than failing. */
303 #define DIFFERENT 0
304 #define SAME 1
305 #define DUNNO 2
306 static int
307 VLDB_Same(struct VenusFid *afid, struct vrequest *areq)
308 {
309     struct vrequest treq;
310     struct conn *tconn;
311     int i, type = 0;
312     union {
313         struct vldbentry tve;
314         struct nvldbentry ntve;
315         struct uvldbentry utve;
316     } *v;
317     struct volume *tvp;
318     struct cell *tcell;
319     char *bp, tbuf[CVBS];       /* biggest volume id is 2^32, ~ 4*10^9 */
320     unsigned int changed;
321     struct server *(oldhosts[NMAXNSERVERS]);
322
323     AFS_STATCNT(CheckVLDB);
324     afs_FinalizeReq(areq);
325
326     if ((i = afs_InitReq(&treq, afs_osi_credp)))
327         return DUNNO;
328     v = afs_osi_Alloc(sizeof(*v));
329     tcell = afs_GetCell(afid->Cell, READ_LOCK);
330     bp = afs_cv2string(&tbuf[CVBS], afid->Fid.Volume);
331     do {
332         VSleep(2);              /* Better safe than sorry. */
333         tconn =
334             afs_ConnByMHosts(tcell->cellHosts, tcell->vlport, tcell->cellNum,
335                              &treq, SHARED_LOCK);
336         if (tconn) {
337             if (tconn->srvr->server->flags & SNO_LHOSTS) {
338                 type = 0;
339                 RX_AFS_GUNLOCK();
340                 i = VL_GetEntryByNameO(tconn->id, bp, &v->tve);
341                 RX_AFS_GLOCK();
342             } else if (tconn->srvr->server->flags & SYES_LHOSTS) {
343                 type = 1;
344                 RX_AFS_GUNLOCK();
345                 i = VL_GetEntryByNameN(tconn->id, bp, &v->ntve);
346                 RX_AFS_GLOCK();
347             } else {
348                 type = 2;
349                 RX_AFS_GUNLOCK();
350                 i = VL_GetEntryByNameU(tconn->id, bp, &v->utve);
351                 RX_AFS_GLOCK();
352                 if (!(tconn->srvr->server->flags & SVLSRV_UUID)) {
353                     if (i == RXGEN_OPCODE) {
354                         type = 1;
355                         RX_AFS_GUNLOCK();
356                         i = VL_GetEntryByNameN(tconn->id, bp, &v->ntve);
357                         RX_AFS_GLOCK();
358                         if (i == RXGEN_OPCODE) {
359                             type = 0;
360                             tconn->srvr->server->flags |= SNO_LHOSTS;
361                             RX_AFS_GUNLOCK();
362                             i = VL_GetEntryByNameO(tconn->id, bp, &v->tve);
363                             RX_AFS_GLOCK();
364                         } else if (!i)
365                             tconn->srvr->server->flags |= SYES_LHOSTS;
366                     } else if (!i)
367                         tconn->srvr->server->flags |= SVLSRV_UUID;
368                 }
369                 lastcode = i;
370             }
371         } else
372             i = -1;
373     } while (afs_Analyze(tconn, i, NULL, &treq, -1,     /* no op code for this */
374                          SHARED_LOCK, tcell));
375
376     afs_PutCell(tcell, READ_LOCK);
377     afs_Trace2(afs_iclSetp, CM_TRACE_CHECKVLDB, ICL_TYPE_FID, &afid,
378                ICL_TYPE_INT32, i);
379
380     if (i) {
381         afs_osi_Free(v, sizeof(*v));
382         return DUNNO;
383     }
384     /* have info, copy into serverHost array */
385     changed = 0;
386     tvp = afs_FindVolume(afid, WRITE_LOCK);
387     if (tvp) {
388         ObtainWriteLock(&tvp->lock, 107);
389         for (i = 0; i < NMAXNSERVERS && tvp->serverHost[i]; i++) {
390             oldhosts[i] = tvp->serverHost[i];
391         }
392
393         if (type == 2) {
394             InstallUVolumeEntry(tvp, &v->utve, afid->Cell, tcell, &treq);
395         } else if (type == 1) {
396             InstallNVolumeEntry(tvp, &v->ntve, afid->Cell);
397         } else {
398             InstallVolumeEntry(tvp, &v->tve, afid->Cell);
399         }
400
401         if (i < NMAXNSERVERS && tvp->serverHost[i]) {
402             changed = 1;
403         }
404         for (--i; !changed && i >= 0; i--) {
405             if (tvp->serverHost[i] != oldhosts[i]) {
406                 changed = 1;    /* also happens if prefs change.  big deal. */
407             }
408         }
409
410         ReleaseWriteLock(&tvp->lock);
411         afs_PutVolume(tvp, WRITE_LOCK);
412     } else {                    /* can't find volume */
413         tvp = afs_GetVolume(afid, &treq, WRITE_LOCK);
414         if (tvp) {
415             afs_PutVolume(tvp, WRITE_LOCK);
416             afs_osi_Free(v, sizeof(*v));
417             return DIFFERENT;
418         } else {
419             afs_osi_Free(v, sizeof(*v));
420             return DUNNO;
421         }
422     }
423
424     afs_osi_Free(v, sizeof(*v));
425     return (changed ? DIFFERENT : SAME);
426 }                               /*VLDB_Same */
427
428
429 /*------------------------------------------------------------------------
430  * EXPORTED afs_Analyze
431  *
432  * Description:
433  *      Analyze the outcome of an RPC operation, taking whatever support
434  *      actions are necessary.
435  *
436  * Arguments:
437  *      aconn : Ptr to the relevant connection on which the call was made.
438  *      acode : The return code experienced by the RPC.
439  *      afid  : The FID of the file involved in the action.  This argument
440  *              may be null if none was involved.
441  *      areq  : The request record associated with this operation.
442  *      op    : which RPC we are analyzing.
443  *      cellp : pointer to a cell struct.  Must provide either fid or cell.
444  *
445  * Returns:
446  *      Non-zero value if the related RPC operation should be retried,
447  *      zero otherwise.
448  *
449  * Environment:
450  *      This routine is typically called in a do-while loop, causing the
451  *      embedded RPC operation to be called repeatedly if appropriate
452  *      until whatever error condition (if any) is intolerable.
453  *
454  * Side Effects:
455  *      As advertised.
456  *
457  * NOTE:
458  *      The retry return value is used by afs_StoreAllSegments to determine
459  *      if this is a temporary or permanent error.
460  *------------------------------------------------------------------------*/
461 int
462 afs_Analyze(register struct conn *aconn, afs_int32 acode,
463             struct VenusFid *afid, register struct vrequest *areq, int op,
464             afs_int32 locktype, struct cell *cellp)
465 {
466     afs_int32 i;
467     struct srvAddr *sa;
468     struct server *tsp;
469     struct volume *tvp;
470     afs_int32 shouldRetry = 0;
471     struct afs_stats_RPCErrors *aerrP;
472
473     AFS_STATCNT(afs_Analyze);
474     afs_Trace4(afs_iclSetp, CM_TRACE_ANALYZE, ICL_TYPE_INT32, op,
475                ICL_TYPE_POINTER, aconn, ICL_TYPE_INT32, acode, ICL_TYPE_LONG,
476                areq->uid);
477
478     aerrP = (struct afs_stats_RPCErrors *)0;
479
480     if ((op >= 0) && (op < AFS_STATS_NUM_FS_RPC_OPS))
481         aerrP = &(afs_stats_cmfullperf.rpc.fsRPCErrors[op]);
482
483     afs_FinalizeReq(areq);
484     if (!aconn && areq->busyCount) {    /* one RPC or more got VBUSY/VRESTARTING */
485
486         tvp = afs_FindVolume(afid, READ_LOCK);
487         if (tvp) {
488             afs_warnuser("afs: Waiting for busy volume %u (%s) in cell %s\n",
489                          (afid ? afid->Fid.Volume : 0),
490                          (tvp->name ? tvp->name : ""),
491                          ((tvp->serverHost[0]
492                            && tvp->serverHost[0]->cell) ? tvp->serverHost[0]->
493                           cell->cellName : ""));
494
495             for (i = 0; i < MAXHOSTS; i++) {
496                 if (tvp->status[i] != not_busy && tvp->status[i] != offline) {
497                     tvp->status[i] = not_busy;
498                 }
499                 if (tvp->status[i] == not_busy)
500                     shouldRetry = 1;
501             }
502             afs_PutVolume(tvp, READ_LOCK);
503         } else {
504             afs_warnuser("afs: Waiting for busy volume %u\n",
505                          (afid ? afid->Fid.Volume : 0));
506         }
507
508         if (areq->busyCount > 100) {
509             if (aerrP)
510                 (aerrP->err_Volume)++;
511             areq->volumeError = VOLBUSY;
512             shouldRetry = 0;
513         } else {
514             VSleep(afs_BusyWaitPeriod); /* poll periodically */
515         }
516         if (shouldRetry != 0)
517             areq->busyCount++;
518
519         return shouldRetry;     /* should retry */
520     }
521
522     if (!aconn) {
523         if (!areq->volumeError) {
524             if (aerrP)
525                 (aerrP->err_Network)++;
526             if (hm_retry_int && !(areq->flags & O_NONBLOCK) &&  /* "hard" mount */
527                 ((afid && afs_IsPrimaryCellNum(afid->Cell))
528                  || (cellp && afs_IsPrimaryCell(cellp)))) {
529                 if (!afid) {
530                     afs_warnuser
531                         ("afs: hard-mount waiting for a vlserver to return to service\n");
532                     VSleep(hm_retry_int);
533                     afs_CheckServers(1, cellp);
534                     shouldRetry = 1;
535                 } else {
536                     tvp = afs_FindVolume(afid, READ_LOCK);
537                     if (!tvp || (tvp->states & VRO)) {
538                         shouldRetry = hm_retry_RO;
539                     } else {
540                         shouldRetry = hm_retry_RW;
541                     }
542                     if (tvp)
543                         afs_PutVolume(tvp, READ_LOCK);
544                     if (shouldRetry) {
545                         afs_warnuser
546                             ("afs: hard-mount waiting for volume %u\n",
547                              afid->Fid.Volume);
548                         VSleep(hm_retry_int);
549                         afs_CheckServers(1, cellp);
550                     }
551                 }
552             } /* if (hm_retry_int ... */
553             else {
554                 areq->networkError = 1;
555             }
556         }
557         return shouldRetry;
558     }
559
560     /* Find server associated with this connection. */
561     sa = aconn->srvr;
562     tsp = sa->server;
563
564     /* Before we do anything with acode, make sure we translate it back to
565      * a system error */
566     if ((acode & ~0xff) == ERROR_TABLE_BASE_uae)
567         acode = et_to_sys_error(acode);
568
569     if (acode == 0) {
570         /* If we previously took an error, mark this volume not busy */
571         if (areq->volumeError) {
572             tvp = afs_FindVolume(afid, READ_LOCK);
573             if (tvp) {
574                 for (i = 0; i < MAXHOSTS; i++) {
575                     if (tvp->serverHost[i] == tsp) {
576                         tvp->status[i] = not_busy;
577                     }
578                 }
579                 afs_PutVolume(tvp, READ_LOCK);
580             }
581         }
582
583         afs_PutConn(aconn, locktype);
584         return 0;
585     }
586
587     /* If network troubles, mark server as having bogued out again. */
588     /* VRESTARTING is < 0 because of backward compatibility issues 
589      * with 3.4 file servers and older cache managers */
590 #ifdef AFS_64BIT_CLIENT
591     if (acode == -455)
592         acode = 455;
593 #endif /* AFS_64BIT_CLIENT */
594     if ((acode < 0) && (acode != VRESTARTING)) {
595         afs_ServerDown(sa);
596         ForceNewConnections(sa);        /*multi homed clients lock:afs_xsrvAddr? */
597         if (aerrP)
598             (aerrP->err_Server)++;
599     }
600
601     if (acode == VBUSY || acode == VRESTARTING) {
602         if (acode == VBUSY) {
603             areq->busyCount++;
604             if (aerrP)
605                 (aerrP->err_VolumeBusies)++;
606         } else
607             areq->busyCount = 1;
608
609         tvp = afs_FindVolume(afid, READ_LOCK);
610         if (tvp) {
611             for (i = 0; i < MAXHOSTS; i++) {
612                 if (tvp->serverHost[i] == tsp) {
613                     tvp->status[i] = rdwr_busy; /* can't tell which yet */
614                     /* to tell which, have to look at the op code. */
615                 }
616             }
617             afs_PutVolume(tvp, READ_LOCK);
618         } else {
619             afs_warnuser("afs: Waiting for busy volume %u in cell %s\n",
620                          (afid ? afid->Fid.Volume : 0), tsp->cell->cellName);
621             VSleep(afs_BusyWaitPeriod); /* poll periodically */
622         }
623         shouldRetry = 1;
624         acode = 0;
625     } else if (acode == VICETOKENDEAD
626                || (acode & ~0xff) == ERROR_TABLE_BASE_RXK) {
627         /* any rxkad error is treated as token expiration */
628         struct unixuser *tu;
629
630         /*
631          * I'm calling these errors protection errors, since they involve
632          * faulty authentication.
633          */
634         if (aerrP)
635             (aerrP->err_Protection)++;
636
637         tu = afs_FindUser(areq->uid, tsp->cell->cellNum, READ_LOCK);
638         if (tu) {
639             if ((acode == VICETOKENDEAD) || (acode == RXKADEXPIRED))
640                 afs_warnuser
641                     ("afs: Tokens for user of AFS id %d for cell %s have expired\n",
642                      tu->vid, aconn->srvr->server->cell->cellName);
643             else
644                 afs_warnuser
645                     ("afs: Tokens for user of AFS id %d for cell %s are discarded (rxkad error=%d)\n",
646                      tu->vid, aconn->srvr->server->cell->cellName, acode);
647             afs_PutUser(tu, READ_LOCK);
648         } else {
649             /* The else case shouldn't be possible and should probably be replaced by a panic? */
650             if ((acode == VICETOKENDEAD) || (acode == RXKADEXPIRED))
651                 afs_warnuser
652                     ("afs: Tokens for user %d for cell %s have expired\n",
653                      areq->uid, aconn->srvr->server->cell->cellName);
654             else
655                 afs_warnuser
656                     ("afs: Tokens for user %d for cell %s are discarded (rxkad error = %d)\n",
657                      areq->uid, aconn->srvr->server->cell->cellName, acode);
658         }
659         aconn->forceConnectFS = 0;      /* don't check until new tokens set */
660         aconn->user->states |= UTokensBad;
661         shouldRetry = 1;        /* Try again (as root). */
662     }
663     /* Check for access violation. */
664     else if (acode == EACCES) {
665         /* should mark access error in non-existent per-user global structure */
666         if (aerrP)
667             (aerrP->err_Protection)++;
668         areq->accessError = 1;
669         if (op == AFS_STATS_FS_RPCIDX_STOREDATA)
670             areq->permWriteError = 1;
671         shouldRetry = 0;
672     }
673     /* check for ubik errors; treat them like crashed servers */
674     else if (acode >= ERROR_TABLE_BASE_U && acode < ERROR_TABLE_BASE_U + 255) {
675         afs_ServerDown(sa);
676         if (aerrP)
677             (aerrP->err_Server)++;
678         shouldRetry = 1;        /* retryable (maybe one is working) */
679         VSleep(1);              /* just in case */
680     }
681     /* Check for bad volume data base / missing volume. */
682     else if (acode == VSALVAGE || acode == VOFFLINE || acode == VNOVOL
683              || acode == VNOSERVICE || acode == VMOVED) {
684         struct cell *tcell;
685         int same;
686
687         shouldRetry = 1;
688         areq->volumeError = VOLMISSING;
689         if (aerrP)
690             (aerrP->err_Volume)++;
691         if (afid && (tcell = afs_GetCell(afid->Cell, 0))) {
692             same = VLDB_Same(afid, areq);
693             tvp = afs_FindVolume(afid, READ_LOCK);
694             if (tvp) {
695                 for (i = 0; i < MAXHOSTS && tvp->serverHost[i]; i++) {
696                     if (tvp->serverHost[i] == tsp) {
697                         if (tvp->status[i] == end_not_busy)
698                             tvp->status[i] = offline;
699                         else
700                             tvp->status[i]++;
701                     } else if (!same) {
702                         tvp->status[i] = not_busy;      /* reset the others */
703                     }
704                 }
705                 afs_PutVolume(tvp, READ_LOCK);
706             }
707         }
708     } else if (acode >= ERROR_TABLE_BASE_VL && acode <= ERROR_TABLE_BASE_VL + 255) {    /* vlserver errors */
709         shouldRetry = 0;
710         areq->volumeError = VOLMISSING;
711     } else if (acode >= 0) {
712         if (aerrP)
713             (aerrP->err_Other)++;
714         if (op == AFS_STATS_FS_RPCIDX_STOREDATA)
715             areq->permWriteError = 1;
716         shouldRetry = 0;        /* Other random Vice error. */
717     } else if (acode == RX_MSGSIZE) {   /* same meaning as EMSGSIZE... */
718         VSleep(1);              /* Just a hack for desperate times. */
719         if (aerrP)
720             (aerrP->err_Other)++;
721         shouldRetry = 1;        /* packet was too big, please retry call */
722     }
723
724     if (acode < 0 && acode != RX_MSGSIZE && acode != VRESTARTING) {
725         /* If we get here, code < 0 and we have network/Server troubles.
726          * areq->networkError is not set here, since we always
727          * retry in case there is another server.  However, if we find
728          * no connection (aconn == 0) we set the networkError flag.
729          */
730         afs_MarkServerUpOrDown(sa, SRVR_ISDOWN);
731         if (aerrP)
732             (aerrP->err_Server)++;
733         VSleep(1);              /* Just a hack for desperate times. */
734         shouldRetry = 1;
735     }
736
737     /* now unlock the connection and return */
738     afs_PutConn(aconn, locktype);
739     return (shouldRetry);
740 }                               /*afs_Analyze */