openbsd-20021030
[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("$Header$");
17
18 #include "afs/stds.h"
19 #include "afs/sysincludes.h"    /* Standard vendor system headers */
20
21 #ifndef UKERNEL
22 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_FBSD_ENV)
23 #include <net/if.h>
24 #include <netinet/in.h>
25 #endif
26
27 #ifdef AFS_SGI62_ENV
28 #include "h/hashing.h"
29 #endif
30 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_FBSD_ENV) && !defined(AFS_DARWIN60_ENV)
31 #include <netinet/in_var.h>
32 #endif
33 #endif /* !UKERNEL */
34
35 #include "afsincludes.h"        /* Afs-based standard headers */
36 #include "afs/afs_stats.h"   /* afs statistics */
37 #include "afs/afs_util.h"
38
39 #if     defined(AFS_SUN56_ENV)
40 #include <inet/led.h>
41 #include <inet/common.h>
42 #if     defined(AFS_SUN58_ENV)
43 #include <netinet/ip6.h>
44 #endif
45 #include <inet/ip.h>
46 #endif
47
48
49 /* shouldn't do it this way, but for now will do */
50 #ifndef ERROR_TABLE_BASE_U
51 #define ERROR_TABLE_BASE_U      (5376L)
52 #endif /* ubik error base define */
53
54 /* same hack for vlserver error base as for ubik error base */
55 #ifndef ERROR_TABLE_BASE_VL
56 #define ERROR_TABLE_BASE_VL     (363520L)
57 #define VL_NOENT                (363524L)
58 #endif /* vlserver error base define */
59
60
61 int afs_BusyWaitPeriod = 15; /* poll every 15 seconds */
62
63 afs_int32 hm_retry_RO=0;    /* don't wait */
64 afs_int32 hm_retry_RW=0;    /* don't wait */
65 afs_int32 hm_retry_int=0;   /* don't wait */
66
67 void afs_CopyError(register struct vrequest *afrom, register struct vrequest *ato)
68 {
69     AFS_STATCNT(afs_CopyError);
70     if (!afrom->initd)
71         return;
72     afs_FinalizeReq(ato);
73     if (afrom->accessError)
74         ato->accessError = 1;
75     if (afrom->volumeError)
76         ato->volumeError = 1;
77     if (afrom->networkError)
78         ato->networkError = 1;
79     if (afrom->permWriteError)
80         ato->permWriteError = 1;
81
82 }
83
84 void afs_FinalizeReq(register struct vrequest *areq)
85 {
86     AFS_STATCNT(afs_FinalizeReq);
87     if (areq->initd)
88         return;
89     areq->busyCount = 0;
90     areq->accessError = 0;
91     areq->volumeError = 0;
92     areq->networkError = 0;
93     areq->permWriteError = 0;
94     areq->initd = 1;
95
96 }
97
98 int afs_CheckCode(afs_int32 acode, struct vrequest *areq, int where)
99 {
100     AFS_STATCNT(afs_CheckCode);
101     if (acode) { 
102         afs_Trace2(afs_iclSetp, CM_TRACE_CHECKCODE,
103                    ICL_TYPE_INT32, acode, ICL_TYPE_INT32, where);
104     }
105     if (!areq || !areq->initd)
106         return acode;
107     if (areq->networkError)
108         return ETIMEDOUT;
109     if (acode == 0)
110         return 0;
111     if (areq->accessError)
112         return EACCES;
113     if (areq->volumeError == VOLMISSING)
114         return ENODEV;
115     if (areq->volumeError == VOLBUSY)
116         return EWOULDBLOCK;
117     if (acode == VNOVNODE)
118         return ENOENT;
119     return acode;
120
121 } /*afs_CheckCode*/
122
123
124 #define VSleep(at)      afs_osi_Wait((at)*1000, 0, 0)
125
126
127 int lastcode;
128 /* returns:
129  * 0   if the vldb record for a specific volume is different from what
130  *     we have cached -- perhaps the volume has moved.
131  * 1   if the vldb record is the same
132  * 2   if we can't tell if it's the same or not. 
133  *
134  * If 0, the caller will probably start over at the beginning of our
135  * list of servers for this volume and try to find one that is up.  If
136  * not 0, we will probably just keep plugging with what we have
137  * cached.   If we fail to contact the VL server, we  should just keep
138  * trying with the information we have, rather than failing. */
139 #define DIFFERENT 0
140 #define SAME 1
141 #define DUNNO 2
142 static int VLDB_Same (struct VenusFid *afid, struct vrequest *areq)
143 {
144     struct vrequest treq;
145     struct conn *tconn;
146     int i, type=0;
147     union { 
148       struct vldbentry tve;
149       struct nvldbentry ntve;
150       struct uvldbentry utve;
151     } v;
152     struct volume *tvp;
153     struct cell *tcell;
154     char *bp, tbuf[CVBS]; /* biggest volume id is 2^32, ~ 4*10^9 */
155     unsigned int changed;
156     struct server *(oldhosts[NMAXNSERVERS]);
157
158     AFS_STATCNT(CheckVLDB);
159     afs_FinalizeReq(areq);
160
161     if ((i = afs_InitReq(&treq, &afs_osi_cred))) return DUNNO;
162     tcell = afs_GetCell(afid->Cell, READ_LOCK);
163     bp = afs_cv2string(&tbuf[CVBS], afid->Fid.Volume);
164     do {
165         VSleep(2);      /* Better safe than sorry. */
166         tconn = afs_ConnByMHosts(tcell->cellHosts, tcell->vlport,
167                                  tcell->cellNum, &treq, SHARED_LOCK);
168         if (tconn) {
169             if (tconn->srvr->server->flags & SNO_LHOSTS) {
170                 type = 0;
171                 RX_AFS_GUNLOCK();
172                 i = VL_GetEntryByNameO(tconn->id, bp, &v.tve);
173                 RX_AFS_GLOCK();
174             } else if (tconn->srvr->server->flags & SYES_LHOSTS) {
175                 type = 1;
176                 RX_AFS_GUNLOCK();
177                 i = VL_GetEntryByNameN(tconn->id, bp, &v.ntve);
178                 RX_AFS_GLOCK();
179             } else {
180                 type = 2;
181                 RX_AFS_GUNLOCK();
182                 i = VL_GetEntryByNameU(tconn->id, bp, &v.utve);
183                 RX_AFS_GLOCK();
184                 if (!(tconn->srvr->server->flags & SVLSRV_UUID)) {
185                     if (i == RXGEN_OPCODE) {
186                         type = 1;
187                         RX_AFS_GUNLOCK();
188                         i = VL_GetEntryByNameN(tconn->id, bp, &v.ntve);
189                         RX_AFS_GLOCK();
190                         if (i == RXGEN_OPCODE) {
191                             type = 0;
192                             tconn->srvr->server->flags |= SNO_LHOSTS;
193                             RX_AFS_GUNLOCK();
194                             i = VL_GetEntryByNameO(tconn->id, bp, &v.tve);
195                             RX_AFS_GLOCK();
196                         } else if (!i)
197                             tconn->srvr->server->flags |= SYES_LHOSTS;
198                     } else if (!i)
199                             tconn->srvr->server->flags |= SVLSRV_UUID;
200                 }
201             lastcode = i;
202             }
203         } else
204             i = -1;
205     } while (afs_Analyze(tconn, i, NULL, &treq,
206                          -1, /* no op code for this */
207                          SHARED_LOCK, tcell));
208
209     afs_PutCell(tcell, READ_LOCK);
210     afs_Trace2(afs_iclSetp, CM_TRACE_CHECKVLDB, ICL_TYPE_FID, &afid,
211                ICL_TYPE_INT32, i);
212
213     if (i) {
214         return DUNNO;
215     }
216     /* have info, copy into serverHost array */
217     changed = 0;
218     tvp = afs_FindVolume(afid, WRITE_LOCK);
219     if (tvp) {
220        ObtainWriteLock(&tvp->lock,107); 
221        for (i=0; i < NMAXNSERVERS && tvp->serverHost[i]; i++) {
222            oldhosts[i] = tvp->serverHost[i];
223        }
224
225        if (type == 2) {
226           InstallUVolumeEntry(tvp, &v.utve, afid->Cell, tcell, &treq);
227        }
228        else if (type == 1) {
229           InstallNVolumeEntry(tvp, &v.ntve, afid->Cell);
230        }
231        else {
232           InstallVolumeEntry(tvp, &v.tve, afid->Cell);
233        }
234
235        if (i < NMAXNSERVERS && tvp->serverHost[i]) {
236             changed = 1;
237        }
238        for (--i;!changed && i >= 0; i--) {
239           if (tvp->serverHost[i] != oldhosts[i]) {
240              changed = 1; /* also happens if prefs change.  big deal. */
241           }
242        }
243
244        ReleaseWriteLock(&tvp->lock);
245        afs_PutVolume(tvp, WRITE_LOCK);
246     }
247     else {      /* can't find volume */
248       tvp = afs_GetVolume(afid, &treq, WRITE_LOCK);
249       if (tvp) {
250         afs_PutVolume(tvp, WRITE_LOCK);
251         return DIFFERENT;
252       }
253       else return DUNNO;
254     }
255
256     return (changed ? DIFFERENT : SAME);
257 } /*VLDB_Same */
258
259
260 /*------------------------------------------------------------------------
261  * EXPORTED afs_Analyze
262  *
263  * Description:
264  *      Analyze the outcome of an RPC operation, taking whatever support
265  *      actions are necessary.
266  *
267  * Arguments:
268  *      aconn : Ptr to the relevant connection on which the call was made.
269  *      acode : The return code experienced by the RPC.
270  *      afid  : The FID of the file involved in the action.  This argument
271  *              may be null if none was involved.
272  *      areq  : The request record associated with this operation.
273  *      op    : which RPC we are analyzing.
274  *      cellp : pointer to a cell struct.  Must provide either fid or cell.
275  *
276  * Returns:
277  *      Non-zero value if the related RPC operation should be retried,
278  *      zero otherwise.
279  *
280  * Environment:
281  *      This routine is typically called in a do-while loop, causing the
282  *      embedded RPC operation to be called repeatedly if appropriate
283  *      until whatever error condition (if any) is intolerable.
284  *
285  * Side Effects:
286  *      As advertised.
287  *
288  * NOTE:
289  *      The retry return value is used by afs_StoreAllSegments to determine
290  *      if this is a temporary or permanent error.
291  *------------------------------------------------------------------------*/
292 int afs_Analyze(register struct conn *aconn, afs_int32 acode, 
293         struct VenusFid *afid, register struct vrequest *areq, int op,
294         afs_int32 locktype, struct cell *cellp)
295 {
296     afs_int32 i;
297     struct srvAddr *sa;
298     struct server *tsp;
299     struct volume *tvp;
300     afs_int32 shouldRetry = 0;
301     struct afs_stats_RPCErrors *aerrP;
302
303     AFS_STATCNT(afs_Analyze);
304     afs_Trace4(afs_iclSetp, CM_TRACE_ANALYZE, ICL_TYPE_INT32, op,
305                ICL_TYPE_POINTER, aconn,
306                ICL_TYPE_INT32, acode, ICL_TYPE_LONG, areq->uid);
307
308     aerrP = (struct afs_stats_RPCErrors *) 0;
309
310     if ((op >= 0) && (op < AFS_STATS_NUM_FS_RPC_OPS))
311       aerrP = &(afs_stats_cmfullperf.rpc.fsRPCErrors[op]);
312
313     afs_FinalizeReq(areq);
314     if (!aconn && areq->busyCount) { /* one RPC or more got VBUSY/VRESTARTING */
315
316       tvp = afs_FindVolume(afid, READ_LOCK);
317       if (tvp) {
318          afs_warnuser("afs: Waiting for busy volume %u (%s) in cell %s\n", 
319                       (afid ? afid->Fid.Volume : 0),
320                       (tvp->name ? tvp->name : ""),
321                       ((tvp->serverHost[0] && tvp->serverHost[0]->cell) ?
322                        tvp->serverHost[0]->cell->cellName : ""));
323
324          for (i=0; i < MAXHOSTS; i++) {
325             if (tvp->status[i] != not_busy && tvp->status[i] != offline) {
326                tvp->status[i] = not_busy; 
327             }
328             if (tvp->status[i] == not_busy)
329                  shouldRetry = 1;
330          }
331          afs_PutVolume(tvp, READ_LOCK);
332       } else {
333          afs_warnuser("afs: Waiting for busy volume %u\n", 
334                       (afid ? afid->Fid.Volume : 0));
335       }
336
337       if (areq->busyCount > 100) {
338         if (aerrP)
339           (aerrP->err_Volume)++;
340         areq->volumeError = VOLBUSY;
341         shouldRetry = 0;
342       } else {
343         VSleep(afs_BusyWaitPeriod);         /* poll periodically */
344       }
345       return shouldRetry; /* should retry */
346     }
347           
348     if (!aconn) {
349         if (!areq->volumeError) {
350             if (aerrP)
351                 (aerrP->err_Network)++;
352             if (hm_retry_int && !(areq->flags & O_NONBLOCK) &&  /* "hard" mount */
353                 ((afid && afs_IsPrimaryCellNum(afid->Cell)) || 
354                  (cellp && afs_IsPrimaryCell(cellp)))) { 
355                 if (!afid) {
356                     afs_warnuser("afs: hard-mount waiting for a vlserver to return to service\n");
357                     VSleep(hm_retry_int);
358                     afs_CheckServers(1,cellp);
359                     shouldRetry=1;
360                 } else {
361                     tvp = afs_FindVolume(afid, READ_LOCK);
362                     if (!tvp || (tvp->states & VRO)) {
363                            shouldRetry = hm_retry_RO;
364                     } else { 
365                            shouldRetry = hm_retry_RW;
366                     }
367                     if (tvp)
368                         afs_PutVolume(tvp, READ_LOCK);
369                     if (shouldRetry) {
370                         afs_warnuser("afs: hard-mount waiting for volume %u\n",
371                                  afid->Fid.Volume);
372                         VSleep(hm_retry_int);
373                         afs_CheckServers(1,cellp);
374                     }
375                 }
376             } /* if (hm_retry_int ... */
377             else {
378                 areq->networkError = 1;
379             }
380         }
381         return shouldRetry;
382     }
383
384     /* Find server associated with this connection. */
385     sa = aconn->srvr;
386     tsp = sa->server;
387
388     if (acode == 0) {
389        /* If we previously took an error, mark this volume not busy */
390        if (areq->volumeError) {
391           tvp = afs_FindVolume(afid, READ_LOCK);
392           if (tvp) {
393              for (i=0; i<MAXHOSTS ; i++) {
394                 if (tvp->serverHost[i] == tsp) {
395                    tvp->status[i] = not_busy ;
396                 }
397              }
398              afs_PutVolume(tvp, READ_LOCK);
399           }
400        }
401
402        afs_PutConn(aconn, locktype);
403        return 0;
404     }
405
406     /* If network troubles, mark server as having bogued out again. */
407     /* VRESTARTING is < 0 because of backward compatibility issues 
408      * with 3.4 file servers and older cache managers */
409 #ifdef AFS_64BIT_CLIENT
410     if (acode == -455)
411         acode = 455;
412 #endif /* AFS_64BIT_CLIENT */
413     if ((acode < 0) && (acode != VRESTARTING)) { 
414         afs_ServerDown(sa);
415         ForceNewConnections(sa); /*multi homed clients lock:afs_xsrvAddr?*/
416         if (aerrP)
417             (aerrP->err_Server)++;
418     }
419
420     if (acode == VBUSY || acode == VRESTARTING) {
421         if (acode == VBUSY) {
422           areq->busyCount++;
423           if (aerrP)
424             (aerrP->err_VolumeBusies)++;
425         }
426         else areq->busyCount = 1;
427
428         tvp = afs_FindVolume(afid, READ_LOCK);
429         if (tvp) {
430           for (i=0; i < MAXHOSTS ; i++ ) {
431             if (tvp->serverHost[i] == tsp) {
432               tvp->status[i] = rdwr_busy ; /* can't tell which yet */
433               /* to tell which, have to look at the op code. */
434             }
435           }
436           afs_PutVolume(tvp, READ_LOCK);
437         }
438         else {
439           afs_warnuser("afs: Waiting for busy volume %u in cell %s\n",
440                        (afid? afid->Fid.Volume : 0), tsp->cell->cellName);
441           VSleep(afs_BusyWaitPeriod);       /* poll periodically */
442         }
443         shouldRetry = 1;
444         acode = 0;
445     }
446     else if (acode == VICETOKENDEAD || (acode & ~0xff) == ERROR_TABLE_BASE_RXK) {
447         /* any rxkad error is treated as token expiration */
448         struct unixuser *tu;
449
450         /*
451          * I'm calling these errors protection errors, since they involve
452          * faulty authentication.
453          */
454         if (aerrP)
455             (aerrP->err_Protection)++;
456
457         tu = afs_FindUser(areq->uid, tsp->cell->cellNum, READ_LOCK);
458         if (tu) {
459             if ((acode == VICETOKENDEAD) || (acode == RXKADEXPIRED))
460                 afs_warnuser("afs: Tokens for user of AFS id %d for cell %s have expired\n", 
461                         tu->vid, aconn->srvr->server->cell->cellName);
462             else
463                 afs_warnuser("afs: Tokens for user of AFS id %d for cell %s are discarded (rxkad error=%d)\n", 
464                         tu->vid, aconn->srvr->server->cell->cellName, acode);
465             afs_PutUser(tu, READ_LOCK); 
466         } else {
467             /* The else case shouldn't be possible and should probably be replaced by a panic? */
468             if ((acode == VICETOKENDEAD) || (acode == RXKADEXPIRED))
469                 afs_warnuser("afs: Tokens for user %d for cell %s have expired\n", 
470                         areq->uid, aconn->srvr->server->cell->cellName);
471             else
472                 afs_warnuser("afs: Tokens for user %d for cell %s are discarded (rxkad error = %d)\n", 
473                         areq->uid, aconn->srvr->server->cell->cellName, acode);
474         }
475         aconn->forceConnectFS = 0;       /* don't check until new tokens set */
476         aconn->user->states |= UTokensBad;
477         shouldRetry = 1;                        /* Try again (as root). */
478     }
479     /* Check for access violation. */
480     else if (acode == EACCES) {
481         /* should mark access error in non-existent per-user global structure */
482         if (aerrP)
483             (aerrP->err_Protection)++;
484         areq->accessError = 1;
485         if (op == AFS_STATS_FS_RPCIDX_STOREDATA)
486             areq->permWriteError = 1;
487         shouldRetry = 0;
488     }
489     /* check for ubik errors; treat them like crashed servers */
490     else if (acode >= ERROR_TABLE_BASE_U && acode < ERROR_TABLE_BASE_U+255) {
491         afs_ServerDown(sa);
492         if (aerrP)
493             (aerrP->err_Server)++;
494         shouldRetry = 1;                 /* retryable (maybe one is working) */
495         VSleep(1);                      /* just in case */
496     }
497     /* Check for bad volume data base / missing volume. */
498     else if (acode == VSALVAGE || acode == VOFFLINE 
499              || acode == VNOVOL || acode == VNOSERVICE || acode == VMOVED) {
500         struct cell *tcell;
501         int same; 
502
503         shouldRetry = 1;
504         areq->volumeError = VOLMISSING;
505         if (aerrP)
506              (aerrP->err_Volume)++;
507         if (afid && (tcell = afs_GetCell(afid->Cell, 0))) {
508            same = VLDB_Same(afid, areq);
509            tvp = afs_FindVolume(afid, READ_LOCK);
510            if (tvp) {
511               for (i=0; i < MAXHOSTS && tvp->serverHost[i]; i++ ) {
512                  if (tvp->serverHost[i] == tsp) {
513                     if (tvp->status[i] == end_not_busy)
514                        tvp->status[i] = offline ;
515                     else
516                        tvp->status[i]++;
517                  }
518                  else if (!same) {
519                     tvp->status[i] = not_busy; /* reset the others */
520                  }
521               }
522               afs_PutVolume(tvp, READ_LOCK);
523            }
524         }
525      }
526     else if (acode >= ERROR_TABLE_BASE_VL
527              && acode <= ERROR_TABLE_BASE_VL + 255) /* vlserver errors */ {
528        shouldRetry = 0;
529        areq->volumeError = VOLMISSING;
530     }
531     else if (acode >= 0) {
532         if (aerrP)
533             (aerrP->err_Other)++;
534         if (op == AFS_STATS_FS_RPCIDX_STOREDATA)
535             areq->permWriteError = 1;
536         shouldRetry = 0;                /* Other random Vice error. */
537     } else if (acode == RX_MSGSIZE) {   /* same meaning as EMSGSIZE... */
538         VSleep(1);                 /* Just a hack for desperate times. */
539         if (aerrP)
540             (aerrP->err_Other)++;
541         shouldRetry = 1;           /* packet was too big, please retry call */
542     } 
543
544     if (acode < 0  && acode != RX_MSGSIZE && acode != VRESTARTING) {
545         /* If we get here, code < 0 and we have network/Server troubles.
546          * areq->networkError is not set here, since we always
547          * retry in case there is another server.  However, if we find
548          * no connection (aconn == 0) we set the networkError flag.
549          */
550         afs_MarkServerUpOrDown(sa, SRVR_ISDOWN);
551         if (aerrP)
552             (aerrP->err_Server)++;
553         VSleep(1);              /* Just a hack for desperate times. */
554         shouldRetry = 1;
555     }
556     
557     /* now unlock the connection and return */
558     afs_PutConn(aconn, locktype);
559     return (shouldRetry);
560 } /*afs_Analyze*/
561