ac5fa411440ab9a6915d4b287a66919373de24c2
[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 "../afs/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->cell, &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, code;
297    struct srvAddr *sa;
298    struct server *tsp;
299    struct volume *tvp;
300    afs_int32 shouldRetry = 0;
301    struct afs_stats_RPCErrors *aerrP;
302    XSTATS_DECLS;
303
304     AFS_STATCNT(afs_Analyze);
305     afs_Trace4(afs_iclSetp, CM_TRACE_ANALYZE, ICL_TYPE_INT32, op,
306                ICL_TYPE_POINTER, aconn,
307                ICL_TYPE_INT32, acode, ICL_TYPE_LONG, areq->uid);
308
309     aerrP = (struct afs_stats_RPCErrors *) 0;
310
311     if ((op >= 0) && (op < AFS_STATS_NUM_FS_RPC_OPS))
312       aerrP = &(afs_stats_cmfullperf.rpc.fsRPCErrors[op]);
313
314     afs_FinalizeReq(areq);
315     if (!aconn && areq->busyCount) { /* one RPC or more got VBUSY/VRESTARTING */
316
317       tvp = afs_FindVolume(afid, READ_LOCK);
318       if (tvp) {
319          afs_warnuser("afs: Waiting for busy volume %u (%s) in cell %s\n", 
320                       (afid ? afid->Fid.Volume : 0),
321                       (tvp->name ? tvp->name : ""),
322                       ((tvp->serverHost[0] && tvp->serverHost[0]->cell) ?
323                        tvp->serverHost[0]->cell->cellName : ""));
324
325          for (i=0; i < MAXHOSTS; i++) {
326             if (tvp->status[i] != not_busy && tvp->status[i] != offline) {
327                tvp->status[i] = not_busy; 
328             }
329             if (tvp->status[i] == not_busy)
330                  shouldRetry = 1;
331          }
332          afs_PutVolume(tvp, READ_LOCK);
333       } else {
334          afs_warnuser("afs: Waiting for busy volume %u\n", 
335                       (afid ? afid->Fid.Volume : 0));
336       }
337
338       if (areq->busyCount > 100) {
339         if (aerrP)
340           (aerrP->err_Volume)++;
341         areq->volumeError = VOLBUSY;
342         shouldRetry = 0;
343       } else {
344         VSleep(afs_BusyWaitPeriod);         /* poll periodically */
345       }
346       return shouldRetry; /* should retry */
347     }
348           
349     if (!aconn) {
350         if (!areq->volumeError) {
351             if (aerrP)
352                 (aerrP->err_Network)++;
353             if (hm_retry_int && !(areq->flags & O_NONBLOCK) &&  /* "hard" mount */
354                 ((afid && afid->Cell == LOCALCELL) || 
355                  (cellp && cellp->cell == LOCALCELL))) { 
356                 if (!afid) {
357                     afs_warnuser("afs: hard-mount waiting for a vlserver to return to service\n");
358                     VSleep(hm_retry_int);
359                     afs_CheckServers(1,cellp);
360                     shouldRetry=1;
361                 } else {
362                     tvp = afs_FindVolume(afid, READ_LOCK);
363                     if (!tvp || (tvp->states & VRO)) {
364                            shouldRetry = hm_retry_RO;
365                     } else { 
366                            shouldRetry = hm_retry_RW;
367                     }
368                     if (tvp)
369                         afs_PutVolume(tvp, READ_LOCK);
370                     if (shouldRetry) {
371                         afs_warnuser("afs: hard-mount waiting for volume %u\n",
372                                  afid->Fid.Volume);
373                         VSleep(hm_retry_int);
374                         afs_CheckServers(1,cellp);
375                     }
376                 }
377             } /* if (hm_retry_int ... */
378             else {
379                 areq->networkError = 1;
380             }
381         }
382         return shouldRetry;
383     }
384
385     /* Find server associated with this connection. */
386     sa = aconn->srvr;
387     tsp = sa->server;
388
389     if (acode == 0) {
390        /* If we previously took an error, mark this volume not busy */
391        if (areq->volumeError) {
392           tvp = afs_FindVolume(afid, READ_LOCK);
393           if (tvp) {
394              for (i=0; i<MAXHOSTS ; i++) {
395                 if (tvp->serverHost[i] == tsp) {
396                    tvp->status[i] = not_busy ;
397                 }
398              }
399              afs_PutVolume(tvp, READ_LOCK);
400           }
401        }
402
403        afs_PutConn(aconn, locktype);
404        return 0;
405     }
406
407     /* If network troubles, mark server as having bogued out again. */
408     /* VRESTARTING is < 0 because of backward compatibility issues 
409      * with 3.4 file servers and older cache managers */
410 #ifdef AFS_64BIT_CLIENT
411     if (acode == -455)
412         acode = 455;
413 #endif /* AFS_64BIT_CLIENT */
414     if ((acode < 0) && (acode != VRESTARTING)) { 
415         afs_ServerDown(sa);
416         ForceNewConnections(sa); /*multi homed clients lock:afs_xsrvAddr?*/
417         if (aerrP)
418             (aerrP->err_Server)++;
419     }
420
421     if (acode == VBUSY || acode == VRESTARTING) {
422         if (acode == VBUSY) {
423           areq->busyCount++;
424           if (aerrP)
425             (aerrP->err_VolumeBusies)++;
426         }
427         else areq->busyCount = 1;
428
429         tvp = afs_FindVolume(afid, READ_LOCK);
430         if (tvp) {
431           for (i=0; i < MAXHOSTS ; i++ ) {
432             if (tvp->serverHost[i] == tsp) {
433               tvp->status[i] = rdwr_busy ; /* can't tell which yet */
434               /* to tell which, have to look at the op code. */
435             }
436           }
437           afs_PutVolume(tvp, READ_LOCK);
438         }
439         else {
440           afs_warnuser("afs: Waiting for busy volume %u in cell %s\n",
441                        (afid? afid->Fid.Volume : 0), tsp->cell->cellName);
442           VSleep(afs_BusyWaitPeriod);       /* poll periodically */
443         }
444         shouldRetry = 1;
445         acode = 0;
446     }
447     else if (acode == VICETOKENDEAD || (acode & ~0xff) == ERROR_TABLE_BASE_RXK) {
448         /* any rxkad error is treated as token expiration */
449         struct unixuser *tu;
450
451         /*
452          * I'm calling these errors protection errors, since they involve
453          * faulty authentication.
454          */
455         if (aerrP)
456             (aerrP->err_Protection)++;
457
458         tu = afs_FindUser(areq->uid, tsp->cell->cell, READ_LOCK);
459         if (tu) {
460             if ((acode == VICETOKENDEAD) || (acode == RXKADEXPIRED))
461                 afs_warnuser("afs: Tokens for user of AFS id %d for cell %s have expired\n", 
462                         tu->vid, aconn->srvr->server->cell->cellName);
463             else
464                 afs_warnuser("afs: Tokens for user of AFS id %d for cell %s are discarded (rxkad error=%d)\n", 
465                         tu->vid, aconn->srvr->server->cell->cellName, acode);
466             afs_PutUser(tu, READ_LOCK); 
467         } else {
468             /* The else case shouldn't be possible and should probably be replaced by a panic? */
469             if ((acode == VICETOKENDEAD) || (acode == RXKADEXPIRED))
470                 afs_warnuser("afs: Tokens for user %d for cell %s have expired\n", 
471                         areq->uid, aconn->srvr->server->cell->cellName);
472             else
473                 afs_warnuser("afs: Tokens for user %d for cell %s are discarded (rxkad error = %d)\n", 
474                         areq->uid, aconn->srvr->server->cell->cellName, acode);
475         }
476         aconn->forceConnectFS = 0;       /* don't check until new tokens set */
477         aconn->user->states |= UTokensBad;
478         shouldRetry = 1;                        /* Try again (as root). */
479     }
480     /* Check for access violation. */
481     else if (acode == EACCES) {
482         /* should mark access error in non-existent per-user global structure */
483         if (aerrP)
484             (aerrP->err_Protection)++;
485         areq->accessError = 1;
486         if (op == AFS_STATS_FS_RPCIDX_STOREDATA)
487             areq->permWriteError = 1;
488         shouldRetry = 0;
489     }
490     /* check for ubik errors; treat them like crashed servers */
491     else if (acode >= ERROR_TABLE_BASE_U && acode < ERROR_TABLE_BASE_U+255) {
492         afs_ServerDown(sa);
493         if (aerrP)
494             (aerrP->err_Server)++;
495         shouldRetry = 1;                 /* retryable (maybe one is working) */
496         VSleep(1);                      /* just in case */
497     }
498     /* Check for bad volume data base / missing volume. */
499     else if (acode == VSALVAGE || acode == VOFFLINE 
500              || acode == VNOVOL || acode == VNOSERVICE || acode == VMOVED) {
501         struct cell *tcell;
502         int same; 
503
504         shouldRetry = 1;
505         areq->volumeError = VOLMISSING;
506         if (aerrP)
507              (aerrP->err_Volume)++;
508         if (afid && (tcell = afs_GetCell(afid->Cell, 0))) {
509            same = VLDB_Same(afid, areq);
510            tvp = afs_FindVolume(afid, READ_LOCK);
511            if (tvp) {
512               for (i=0; i < MAXHOSTS && tvp->serverHost[i]; i++ ) {
513                  if (tvp->serverHost[i] == tsp) {
514                     if (tvp->status[i] == end_not_busy)
515                        tvp->status[i] = offline ;
516                     else
517                        tvp->status[i]++;
518                  }
519                  else if (!same) {
520                     tvp->status[i] = not_busy; /* reset the others */
521                  }
522               }
523               afs_PutVolume(tvp, READ_LOCK);
524            }
525         }
526      }
527     else if (acode >= ERROR_TABLE_BASE_VL
528              && acode <= ERROR_TABLE_BASE_VL + 255) /* vlserver errors */ {
529        shouldRetry = 0;
530        areq->volumeError = VOLMISSING;
531     }
532     else if (acode >= 0) {
533         if (aerrP)
534             (aerrP->err_Other)++;
535         if (op == AFS_STATS_FS_RPCIDX_STOREDATA)
536             areq->permWriteError = 1;
537         shouldRetry = 0;                /* Other random Vice error. */
538     } else if (acode == RX_MSGSIZE) {   /* same meaning as EMSGSIZE... */
539         VSleep(1);                 /* Just a hack for desperate times. */
540         if (aerrP)
541             (aerrP->err_Other)++;
542         shouldRetry = 1;           /* packet was too big, please retry call */
543     } 
544
545     if (acode < 0  && acode != RX_MSGSIZE && acode != VRESTARTING) {
546         /* If we get here, code < 0 and we have network/Server troubles.
547          * areq->networkError is not set here, since we always
548          * retry in case there is another server.  However, if we find
549          * no connection (aconn == 0) we set the networkError flag.
550          */
551         afs_MarkServerUpOrDown(sa, SRVR_ISDOWN);
552         if (aerrP)
553             (aerrP->err_Server)++;
554         VSleep(1);              /* Just a hack for desperate times. */
555         shouldRetry = 1;
556     }
557     
558     /* now unlock the connection and return */
559     afs_PutConn(aconn, locktype);
560     return (shouldRetry);
561 } /*afs_Analyze*/
562