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