openafs-kill-dead-code-20050403
[openafs.git] / src / afs / afs_vcache.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  * afs_FlushVCache
13  * afs_AllocCBR
14  * afs_FreeCBR
15  * afs_FlushVCBs
16  * afs_QueueVCB
17  * afs_RemoveVCB
18  * afs_NewVCache
19  * afs_FlushActiveVcaches
20  * afs_VerifyVCache2
21  * afs_WriteVCache
22  * afs_SimpleVStat
23  * afs_ProcessFS
24  * TellALittleWhiteLie
25  * afs_RemoteLookup
26  * afs_GetVCache
27  * afs_LookupVCache
28  * afs_GetRootVCache
29  * afs_FetchStatus
30  * afs_StuffVcache
31  * afs_PutVCache
32  * afs_FindVCache
33  * afs_NFSFindVCache
34  * afs_vcacheInit
35  * shutdown_vcache
36  *
37  */
38 #include <afsconfig.h>
39 #include "afs/param.h"
40
41 RCSID
42     ("$Header$");
43
44 #include "afs/sysincludes.h"    /*Standard vendor system headers */
45 #include "afsincludes.h"        /*AFS-based standard headers */
46 #include "afs/afs_stats.h"
47 #include "afs/afs_cbqueue.h"
48 #include "afs/afs_osidnlc.h"
49
50 #ifdef AFS_OSF_ENV
51 afs_int32 afs_maxvcount = 0;    /* max number of vcache entries */
52 afs_int32 afs_vcount = 0;       /* number of vcache in use now */
53 #endif /* AFS_OSF_ENV */
54
55 #ifdef AFS_SGI_ENV
56 int afsvnumbers = 0;
57 #endif
58
59 #ifdef AFS_SGI64_ENV
60 char *makesname();
61 #endif /* AFS_SGI64_ENV */
62
63 /* Exported variables */
64 afs_rwlock_t afs_xvcache;       /*Lock: alloc new stat cache entries */
65 afs_lock_t afs_xvcb;            /*Lock: fids on which there are callbacks */
66 struct vcache *freeVCList;      /*Free list for stat cache entries */
67 struct vcache *Initial_freeVCList;      /*Initial list for above */
68 struct afs_q VLRU;              /*vcache LRU */
69 afs_int32 vcachegen = 0;
70 unsigned int afs_paniconwarn = 0;
71 struct vcache *afs_vhashT[VCSIZE];
72 static struct afs_cbr *afs_cbrHashT[CBRSIZE];
73 afs_int32 afs_bulkStatsLost;
74 int afs_norefpanic = 0;
75
76 /* Forward declarations */
77 static afs_int32 afs_QueueVCB(struct vcache *avc);
78
79 /*
80  * afs_HashCBRFid
81  *
82  * Generate an index into the hash table for a given Fid.
83  */
84 static int
85 afs_HashCBRFid(struct AFSFid *fid)
86 {
87     return (fid->Volume + fid->Vnode + fid->Unique) % CBRSIZE;
88 }
89
90 /*
91  * afs_InsertHashCBR
92  *
93  * Insert a CBR entry into the hash table.
94  * Must be called with afs_xvcb held.
95  */
96 static void
97 afs_InsertHashCBR(struct afs_cbr *cbr)
98 {
99     int slot = afs_HashCBRFid(&cbr->fid);
100
101     cbr->hash_next = afs_cbrHashT[slot];
102     if (afs_cbrHashT[slot])
103         afs_cbrHashT[slot]->hash_pprev = &cbr->hash_next;
104
105     cbr->hash_pprev = &afs_cbrHashT[slot];
106     afs_cbrHashT[slot] = cbr;
107 }
108
109 /*
110  * afs_FlushVCache
111  *
112  * Description:
113  *      Flush the given vcache entry.
114  *
115  * Parameters:
116  *      avc : Pointer to vcache entry to flush.
117  *      slept : Pointer to int to set 1 if we sleep/drop locks, 0 if we don't.
118  *
119  * Environment:
120  *      afs_xvcache lock must be held for writing upon entry to
121  *      prevent people from changing the vrefCount field, and to
122  *      protect the lruq and hnext fields.
123  * LOCK: afs_FlushVCache afs_xvcache W
124  * REFCNT: vcache ref count must be zero on entry except for osf1
125  * RACE: lock is dropped and reobtained, permitting race in caller
126  */
127
128 int
129 afs_FlushVCache(struct vcache *avc, int *slept)
130 {                               /*afs_FlushVCache */
131
132     register afs_int32 i, code;
133     register struct vcache **uvc, *wvc;
134
135     *slept = 0;
136     AFS_STATCNT(afs_FlushVCache);
137     afs_Trace2(afs_iclSetp, CM_TRACE_FLUSHV, ICL_TYPE_POINTER, avc,
138                ICL_TYPE_INT32, avc->states);
139 #ifdef  AFS_OSF_ENV
140     AFS_GUNLOCK();
141     VN_LOCK(AFSTOV(avc));
142     AFS_GLOCK();
143 #endif
144
145     code = osi_VM_FlushVCache(avc, slept);
146     if (code)
147         goto bad;
148
149     if (avc->states & CVFlushed) {
150         code = EBUSY;
151         goto bad;
152     }
153     if (avc->nextfree || !avc->vlruq.prev || !avc->vlruq.next) {        /* qv afs.h */
154         refpanic("LRU vs. Free inconsistency");
155     }
156     avc->states |= CVFlushed;
157     /* pull the entry out of the lruq and put it on the free list */
158     QRemove(&avc->vlruq);
159     avc->vlruq.prev = avc->vlruq.next = (struct afs_q *)0;
160
161     /* keep track of # of files that we bulk stat'd, but never used
162      * before they got recycled.
163      */
164     if (avc->states & CBulkStat)
165         afs_bulkStatsLost++;
166     vcachegen++;
167     /* remove entry from the hash chain */
168     i = VCHash(&avc->fid);
169     uvc = &afs_vhashT[i];
170     for (wvc = *uvc; wvc; uvc = &wvc->hnext, wvc = *uvc) {
171         if (avc == wvc) {
172             *uvc = avc->hnext;
173             avc->hnext = (struct vcache *)NULL;
174             break;
175         }
176     }
177     if (!wvc)
178         osi_Panic("flushvcache");       /* not in correct hash bucket */
179     if (avc->mvid)
180         osi_FreeSmallSpace(avc->mvid);
181     avc->mvid = (struct VenusFid *)0;
182     if (avc->linkData) {
183         afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
184         avc->linkData = NULL;
185     }
186 #if defined(AFS_XBSD_ENV)
187     /* OK, there are no internal vrefCounts, so there shouldn't
188      * be any more refs here. */
189     if (avc->v) {
190         avc->v->v_data = NULL;  /* remove from vnode */
191         avc->v = NULL;          /* also drop the ptr to vnode */
192     }
193 #endif
194     afs_FreeAllAxs(&(avc->Access));
195
196     /* we can't really give back callbacks on RO files, since the
197      * server only tracks them on a per-volume basis, and we don't
198      * know whether we still have some other files from the same
199      * volume. */
200     if ((avc->states & CRO) == 0 && avc->callback) {
201         afs_QueueVCB(avc);
202     }
203     ObtainWriteLock(&afs_xcbhash, 460);
204     afs_DequeueCallback(avc);   /* remove it from queued callbacks list */
205     avc->states &= ~(CStatd | CUnique);
206     ReleaseWriteLock(&afs_xcbhash);
207     afs_symhint_inval(avc);
208     if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
209         osi_dnlc_purgedp(avc);  /* if it (could be) a directory */
210     else
211         osi_dnlc_purgevp(avc);
212
213     /*
214      * Next, keep track of which vnodes we've deleted for create's
215      * optimistic synchronization algorithm
216      */
217     afs_allZaps++;
218     if (avc->fid.Fid.Vnode & 1)
219         afs_oddZaps++;
220     else
221         afs_evenZaps++;
222
223 #if     !defined(AFS_OSF_ENV)
224     /* put the entry in the free list */
225     avc->nextfree = freeVCList;
226     freeVCList = avc;
227     if (avc->vlruq.prev || avc->vlruq.next) {
228         refpanic("LRU vs. Free inconsistency");
229     }
230 #else
231     /* This should put it back on the vnode free list since usecount is 1 */
232     afs_vcount--;
233     vSetType(avc, VREG);
234     if (VREFCOUNT(avc) > 0) {
235         VN_UNLOCK(AFSTOV(avc));
236         AFS_RELE(AFSTOV(avc));
237     } else {
238         if (afs_norefpanic) {
239             printf("flush vc refcnt < 1");
240             afs_norefpanic++;
241             (void)vgone(avc, VX_NOSLEEP, NULL);
242             AFS_GLOCK();
243             VN_UNLOCK(AFSTOV(avc));
244         } else
245             osi_Panic("flush vc refcnt < 1");
246     }
247 #endif /* AFS_OSF_ENV */
248     avc->states |= CVFlushed;
249     return 0;
250
251   bad:
252 #ifdef  AFS_OSF_ENV
253     VN_UNLOCK(AFSTOV(avc));
254 #endif
255     return code;
256
257 }                               /*afs_FlushVCache */
258
259 #ifndef AFS_SGI_ENV
260 /*
261  * afs_InactiveVCache
262  *
263  * The core of the inactive vnode op for all but IRIX.
264  */
265 void
266 afs_InactiveVCache(struct vcache *avc, struct AFS_UCRED *acred)
267 {
268     AFS_STATCNT(afs_inactive);
269     if (avc->states & CDirty) {
270         /* we can't keep trying to push back dirty data forever.  Give up. */
271         afs_InvalidateAllSegments(avc); /* turns off dirty bit */
272     }
273     avc->states &= ~CMAPPED;    /* mainly used by SunOS 4.0.x */
274     avc->states &= ~CDirty;     /* Turn it off */
275     if (avc->states & CUnlinked) {
276         if (CheckLock(&afs_xvcache) || CheckLock(&afs_xdcache)) {
277             avc->states |= CUnlinkedDel;
278             return;
279         }
280         afs_remunlink(avc, 1);  /* ignore any return code */
281     }
282
283 }
284 #endif
285
286 /*
287  * afs_AllocCBR
288  *
289  * Description: allocate a callback return structure from the
290  * free list and return it.
291  *
292  * Env: The alloc and free routines are both called with the afs_xvcb lock
293  * held, so we don't have to worry about blocking in osi_Alloc.
294  */
295 static struct afs_cbr *afs_cbrSpace = 0;
296 struct afs_cbr *
297 afs_AllocCBR(void)
298 {
299     register struct afs_cbr *tsp;
300     int i;
301
302     while (!afs_cbrSpace) {
303         if (afs_stats_cmperf.CallBackAlloced >= 2) {
304             /* don't allocate more than 2 * AFS_NCBRS for now */
305             afs_FlushVCBs(0);
306             afs_stats_cmperf.CallBackFlushes++;
307         } else {
308             /* try allocating */
309             tsp =
310                 (struct afs_cbr *)afs_osi_Alloc(AFS_NCBRS *
311                                                 sizeof(struct afs_cbr));
312             for (i = 0; i < AFS_NCBRS - 1; i++) {
313                 tsp[i].next = &tsp[i + 1];
314             }
315             tsp[AFS_NCBRS - 1].next = 0;
316             afs_cbrSpace = tsp;
317             afs_stats_cmperf.CallBackAlloced++;
318         }
319     }
320     tsp = afs_cbrSpace;
321     afs_cbrSpace = tsp->next;
322     return tsp;
323 }
324
325 /*
326  * afs_FreeCBR
327  *
328  * Description: free a callback return structure, removing it from all lists.
329  *
330  * Parameters:
331  *      asp -- the address of the structure to free.
332  *
333  * Environment: the xvcb lock is held over these calls.
334  */
335 int
336 afs_FreeCBR(register struct afs_cbr *asp)
337 {
338     *(asp->pprev) = asp->next;
339     if (asp->next)
340         asp->next->pprev = asp->pprev;
341
342     *(asp->hash_pprev) = asp->hash_next;
343     if (asp->hash_next)
344         asp->hash_next->hash_pprev = asp->hash_pprev;
345
346     asp->next = afs_cbrSpace;
347     afs_cbrSpace = asp;
348     return 0;
349 }
350
351 /*
352  * afs_FlushVCBs
353  *
354  * Description: flush all queued callbacks to all servers.
355  *
356  * Parameters: none.
357  *
358  * Environment: holds xvcb lock over RPC to guard against race conditions
359  *      when a new callback is granted for the same file later on.
360  */
361 afs_int32
362 afs_FlushVCBs(afs_int32 lockit)
363 {
364     struct AFSFid *tfids;
365     struct AFSCallBack callBacks[1];
366     struct AFSCBFids fidArray;
367     struct AFSCBs cbArray;
368     afs_int32 code;
369     struct afs_cbr *tcbrp;
370     int tcount;
371     struct server *tsp;
372     int i;
373     struct vrequest treq;
374     struct conn *tc;
375     int safety1, safety2, safety3;
376     XSTATS_DECLS;
377     if ((code = afs_InitReq(&treq, afs_osi_credp)))
378         return code;
379     treq.flags |= O_NONBLOCK;
380     tfids = afs_osi_Alloc(sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
381
382     if (lockit)
383         MObtainWriteLock(&afs_xvcb, 273);
384     ObtainReadLock(&afs_xserver);
385     for (i = 0; i < NSERVERS; i++) {
386         for (safety1 = 0, tsp = afs_servers[i];
387              tsp && safety1 < afs_totalServers + 10;
388              tsp = tsp->next, safety1++) {
389             /* don't have any */
390             if (tsp->cbrs == (struct afs_cbr *)0)
391                 continue;
392
393             /* otherwise, grab a block of AFS_MAXCBRSCALL from the list
394              * and make an RPC, over and over again.
395              */
396             tcount = 0;         /* number found so far */
397             for (safety2 = 0; safety2 < afs_cacheStats; safety2++) {
398                 if (tcount >= AFS_MAXCBRSCALL || !tsp->cbrs) {
399                     /* if buffer is full, or we've queued all we're going
400                      * to from this server, we should flush out the
401                      * callbacks.
402                      */
403                     fidArray.AFSCBFids_len = tcount;
404                     fidArray.AFSCBFids_val = (struct AFSFid *)tfids;
405                     cbArray.AFSCBs_len = 1;
406                     cbArray.AFSCBs_val = callBacks;
407                     memset(&callBacks[0], 0, sizeof(callBacks[0]));
408                     callBacks[0].CallBackType = CB_EXCLUSIVE;
409                     for (safety3 = 0; safety3 < MAXHOSTS * 2; safety3++) {
410                         tc = afs_ConnByHost(tsp, tsp->cell->fsport,
411                                             tsp->cell->cellNum, &treq, 0,
412                                             SHARED_LOCK);
413                         if (tc) {
414                             XSTATS_START_TIME
415                                 (AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS);
416                             RX_AFS_GUNLOCK();
417                             code =
418                                 RXAFS_GiveUpCallBacks(tc->id, &fidArray,
419                                                       &cbArray);
420                             RX_AFS_GLOCK();
421                             XSTATS_END_TIME;
422                         } else
423                             code = -1;
424                         if (!afs_Analyze
425                             (tc, code, 0, &treq,
426                              AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS, SHARED_LOCK,
427                              tsp->cell)) {
428                             break;
429                         }
430                     }
431                     /* ignore return code, since callbacks may have
432                      * been returned anyway, we shouldn't leave them
433                      * around to be returned again.
434                      *
435                      * Next, see if we are done with this server, and if so,
436                      * break to deal with the next one.
437                      */
438                     if (!tsp->cbrs)
439                         break;
440                     tcount = 0;
441                 }
442                 /* if to flush full buffer */
443                 /* if we make it here, we have an entry at the head of cbrs,
444                  * which we should copy to the file ID array and then free.
445                  */
446                 tcbrp = tsp->cbrs;
447                 tfids[tcount++] = tcbrp->fid;
448
449                 /* Freeing the CBR will unlink it from the server's CBR list */
450                 afs_FreeCBR(tcbrp);
451             }                   /* while loop for this one server */
452             if (safety2 > afs_cacheStats) {
453                 afs_warn("possible internal error afs_flushVCBs (%d)\n",
454                          safety2);
455             }
456         }                       /* for loop for this hash chain */
457     }                           /* loop through all hash chains */
458     if (safety1 > afs_totalServers + 2) {
459         afs_warn
460             ("AFS internal error (afs_flushVCBs) (%d > %d), continuing...\n",
461              safety1, afs_totalServers + 2);
462         if (afs_paniconwarn)
463             osi_Panic("afs_flushVCBS safety1");
464     }
465
466     ReleaseReadLock(&afs_xserver);
467     if (lockit)
468         MReleaseWriteLock(&afs_xvcb);
469     afs_osi_Free(tfids, sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
470     return 0;
471 }
472
473 /*
474  * afs_QueueVCB
475  *
476  * Description:
477  *      Queue a callback on the given fid.
478  *
479  * Parameters:
480  *      avc: vcache entry
481  *
482  * Environment:
483  *      Locks the xvcb lock.
484  *      Called when the xvcache lock is already held.
485  */
486
487 static afs_int32
488 afs_QueueVCB(struct vcache *avc)
489 {
490     struct server *tsp;
491     struct afs_cbr *tcbp;
492
493     AFS_STATCNT(afs_QueueVCB);
494     /* The callback is really just a struct server ptr. */
495     tsp = (struct server *)(avc->callback);
496
497     /* we now have a pointer to the server, so we just allocate
498      * a queue entry and queue it.
499      */
500     MObtainWriteLock(&afs_xvcb, 274);
501     tcbp = afs_AllocCBR();
502     tcbp->fid = avc->fid.Fid;
503
504     tcbp->next = tsp->cbrs;
505     if (tsp->cbrs)
506         tsp->cbrs->pprev = &tcbp->next;
507
508     tsp->cbrs = tcbp;
509     tcbp->pprev = &tsp->cbrs;
510
511     afs_InsertHashCBR(tcbp);
512
513     /* now release locks and return */
514     MReleaseWriteLock(&afs_xvcb);
515     return 0;
516 }
517
518
519 /*
520  * afs_RemoveVCB
521  *
522  * Description:
523  *      Remove a queued callback for a given Fid.
524  *
525  * Parameters:
526  *      afid: The fid we want cleansed of queued callbacks.
527  *
528  * Environment:
529  *      Locks xvcb and xserver locks.
530  *      Typically called with xdcache, xvcache and/or individual vcache
531  *      entries locked.
532  */
533
534 void
535 afs_RemoveVCB(struct VenusFid *afid)
536 {
537     int slot;
538     struct afs_cbr *cbr, *ncbr;
539
540     AFS_STATCNT(afs_RemoveVCB);
541     MObtainWriteLock(&afs_xvcb, 275);
542
543     slot = afs_HashCBRFid(&afid->Fid);
544     ncbr = afs_cbrHashT[slot];
545
546     while (ncbr) {
547         cbr = ncbr;
548         ncbr = cbr->hash_next;
549
550         if (afid->Fid.Volume == cbr->fid.Volume &&
551             afid->Fid.Vnode == cbr->fid.Vnode &&
552             afid->Fid.Unique == cbr->fid.Unique) {
553             afs_FreeCBR(cbr);
554         }
555     }
556
557     MReleaseWriteLock(&afs_xvcb);
558 }
559
560 /*
561  * afs_NewVCache
562  *
563  * Description:
564  *      This routine is responsible for allocating a new cache entry
565  *      from the free list.  It formats the cache entry and inserts it
566  *      into the appropriate hash tables.  It must be called with
567  *      afs_xvcache write-locked so as to prevent several processes from
568  *      trying to create a new cache entry simultaneously.
569  *
570  * Parameters:
571  *      afid  : The file id of the file whose cache entry is being
572  *              created.
573  */
574 /* LOCK: afs_NewVCache  afs_xvcache W */
575 struct vcache *
576 afs_NewVCache(struct VenusFid *afid, struct server *serverp)
577 {
578     struct vcache *tvc;
579     afs_int32 i;
580     afs_int32 anumber = VCACHE_FREE;
581 #ifdef  AFS_AIX_ENV
582     struct gnode *gnodepnt;
583 #endif
584 #ifdef  AFS_OSF_ENV
585     struct vcache *nvc;
586 #endif /* AFS_OSF_ENV */
587     struct afs_q *tq, *uq;
588     int code, fv_slept;
589
590     AFS_STATCNT(afs_NewVCache);
591 #ifdef  AFS_OSF_ENV
592 #ifdef  AFS_OSF30_ENV
593     if (afs_vcount >= afs_maxvcount) {
594 #else
595     /*
596      * If we are using > 33 % of the total system vnodes for AFS vcache
597      * entries or we are using the maximum number of vcache entries,
598      * then free some.  (if our usage is > 33% we should free some, if
599      * our usage is > afs_maxvcount, set elsewhere to 0.5*nvnode,
600      * we _must_ free some -- no choice).
601      */
602     if (((3 * afs_vcount) > nvnode) || (afs_vcount >= afs_maxvcount)) {
603 #endif
604         struct afs_q *tq, *uq;
605         int i;
606         char *panicstr;
607
608         i = 0;
609         for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
610             tvc = QTOV(tq);
611             uq = QPrev(tq);
612             if (tvc->states & CVFlushed)
613                 refpanic("CVFlushed on VLRU");
614             else if (i++ > afs_maxvcount)
615                 refpanic("Exceeded pool of AFS vnodes(VLRU cycle?)");
616             else if (QNext(uq) != tq)
617                 refpanic("VLRU inconsistent");
618             else if (VREFCOUNT(tvc) < 1)
619                 refpanic("refcnt 0 on VLRU");
620
621             if (VREFCOUNT(tvc) == 1 && tvc->opens == 0
622                 && (tvc->states & CUnlinkedDel) == 0) {
623                 code = afs_FlushVCache(tvc, &fv_slept);
624                 if (code == 0) {
625                     anumber--;
626                 }
627                 if (fv_slept) {
628                     uq = VLRU.prev;
629                     i = 0;
630                     continue;   /* start over - may have raced. */
631                 }
632             }
633             if (tq == uq)
634                 break;
635         }
636         if (anumber == VCACHE_FREE) {
637             printf("NewVCache: warning none freed, using %d of %d\n",
638                    afs_vcount, afs_maxvcount);
639             if (afs_vcount >= afs_maxvcount) {
640                 osi_Panic("NewVCache - none freed");
641                 /* XXX instead of panicing, should do afs_maxvcount++
642                  * and magic up another one */
643             }
644         }
645     }
646
647     AFS_GUNLOCK();
648     if (getnewvnode(MOUNT_AFS, &Afs_vnodeops, &nvc)) {
649         /* What should we do ???? */
650         osi_Panic("afs_NewVCache: no more vnodes");
651     }
652     AFS_GLOCK();
653
654     tvc = nvc;
655     tvc->nextfree = NULL;
656     afs_vcount++;
657 #else /* AFS_OSF_ENV */
658     /* pull out a free cache entry */
659     if (!freeVCList) {
660         i = 0;
661         for (tq = VLRU.prev; (anumber > 0) && (tq != &VLRU); tq = uq) {
662             tvc = QTOV(tq);
663             uq = QPrev(tq);
664
665             if (tvc->states & CVFlushed) {
666                 refpanic("CVFlushed on VLRU");
667             } else if (i++ > 2 * afs_cacheStats) {      /* even allowing for a few xallocs... */
668                 refpanic("Increase -stat parameter of afsd(VLRU cycle?)");
669             } else if (QNext(uq) != tq) {
670                 refpanic("VLRU inconsistent");
671             }
672 #ifdef AFS_DARWIN_ENV
673             if ((VREFCOUNT(tvc) < DARWIN_REFBASE) ||
674                 (VREFCOUNT(tvc) < 1 + DARWIN_REFBASE &&
675                  UBCINFOEXISTS(&tvc->v))) {
676                 VREFCOUNT_SET(tvc,
677                               DARWIN_REFBASE +
678                               (UBCINFOEXISTS(&tvc->v) ? 1 : 0));
679             }
680             if (tvc->opens == 0 && ((tvc->states & CUnlinkedDel) == 0)
681                 && VREFCOUNT(tvc) == DARWIN_REFBASE + 1
682                 && UBCINFOEXISTS(&tvc->v)) {
683                 osi_VM_TryReclaim(tvc, &fv_slept);
684                 if (fv_slept) {
685                     uq = VLRU.prev;
686                     i = 0;
687                     continue;   /* start over - may have raced. */
688                 }
689             }
690 #elif defined(AFS_LINUX22_ENV)
691             if (tvc != afs_globalVp && VREFCOUNT(tvc) && tvc->opens == 0) {
692                 struct dentry *dentry;
693                 struct list_head *cur, *head = &(AFSTOI(tvc))->i_dentry;
694                 AFS_FAST_HOLD(tvc);
695                 AFS_GUNLOCK();
696
697 restart:
698 #if defined(AFS_LINUX24_ENV)
699                 spin_lock(&dcache_lock);
700 #endif
701                 cur = head;
702                 while ((cur = cur->next) != head) {
703                     dentry = list_entry(cur, struct dentry, d_alias);
704
705                     if (d_unhashed(dentry))
706                         continue;
707
708                     dget_locked(dentry);
709
710 #if defined(AFS_LINUX24_ENV)
711                     spin_unlock(&dcache_lock);
712 #endif
713                     if (d_invalidate(dentry) == -EBUSY) {
714                         dput(dentry);
715                         /* perhaps lock and try to continue? (use cur as head?) */
716                         goto inuse;
717                     }
718                     dput(dentry);
719                     goto restart;
720                 }                   
721 #if defined(AFS_LINUX24_ENV)
722                 spin_unlock(&dcache_lock);
723 #endif
724             inuse:
725                 AFS_GLOCK();
726                 AFS_FAST_RELE(tvc);
727             }
728 #endif
729
730             if (VREFCOUNT(tvc) ==
731 #ifdef AFS_DARWIN_ENV
732                 DARWIN_REFBASE
733 #else
734                 0
735 #endif
736                 && tvc->opens == 0 && (tvc->states & CUnlinkedDel) == 0) {
737 #if defined(AFS_XBSD_ENV)
738                 /*
739                  * vgone() reclaims the vnode, which calls afs_FlushVCache(),
740                  * then it puts the vnode on the free list.
741                  * If we don't do this we end up with a cleaned vnode that's
742                  * not on the free list.
743                  * XXX assume FreeBSD is the same for now.
744                  */
745                 vgone(AFSTOV(tvc));
746                 code = fv_slept = 0;
747 #else
748                 code = afs_FlushVCache(tvc, &fv_slept);
749 #endif
750                 if (code == 0) {
751                     anumber--;
752                 }
753                 if (fv_slept) {
754                     uq = VLRU.prev;
755                     i = 0;
756                     continue;   /* start over - may have raced. */
757                 }
758             }
759             if (tq == uq)
760                 break;
761         }
762     }
763     if (!freeVCList) {
764         /* none free, making one is better than a panic */
765         afs_stats_cmperf.vcacheXAllocs++;       /* count in case we have a leak */
766         tvc = (struct vcache *)afs_osi_Alloc(sizeof(struct vcache));
767 #ifdef  KERNEL_HAVE_PIN
768         pin((char *)tvc, sizeof(struct vcache));        /* XXX */
769 #endif
770 #if defined(AFS_SGI_ENV)
771         {
772             char name[METER_NAMSZ];
773             memset(tvc, 0, sizeof(struct vcache));
774             tvc->v.v_number = ++afsvnumbers;
775             tvc->vc_rwlockid = OSI_NO_LOCKID;
776             initnsema(&tvc->vc_rwlock, 1,
777                       makesname(name, "vrw", tvc->v.v_number));
778 #ifndef AFS_SGI53_ENV
779             initnsema(&tvc->v.v_sync, 0,
780                       makesname(name, "vsy", tvc->v.v_number));
781 #endif
782 #ifndef AFS_SGI62_ENV
783             initnlock(&tvc->v.v_lock,
784                       makesname(name, "vlk", tvc->v.v_number));
785 #endif
786         }
787 #endif /* AFS_SGI_ENV */
788     } else {
789         tvc = freeVCList;       /* take from free list */
790         freeVCList = tvc->nextfree;
791         tvc->nextfree = NULL;
792     }
793 #endif /* AFS_OSF_ENV */
794
795 #if defined(AFS_XBSD_ENV)
796     if (tvc->v)
797         panic("afs_NewVCache(): free vcache with vnode attached");
798 #endif
799
800 #if !defined(AFS_SGI_ENV) && !defined(AFS_OSF_ENV)
801     memset((char *)tvc, 0, sizeof(struct vcache));
802 #else
803     tvc->uncred = 0;
804 #endif
805
806     RWLOCK_INIT(&tvc->lock, "vcache lock");
807 #if     defined(AFS_SUN5_ENV)
808     RWLOCK_INIT(&tvc->vlock, "vcache vlock");
809 #endif /* defined(AFS_SUN5_ENV) */
810
811 #ifdef AFS_OBSD_ENV
812     AFS_GUNLOCK();
813     afs_nbsd_getnewvnode(tvc);  /* includes one refcount */
814     AFS_GLOCK();
815     lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
816 #endif
817 #ifdef AFS_FBSD_ENV
818     {
819         struct vnode *vp;
820
821         AFS_GUNLOCK();
822 #ifdef AFS_FBSD50_ENV
823         if (getnewvnode(MOUNT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
824 #else
825         if (getnewvnode(VT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
826 #endif
827             panic("afs getnewvnode");   /* can't happen */
828         AFS_GLOCK();
829         if (tvc->v != NULL) {
830             /* I'd like to know if this ever happens...
831              * We don't drop global for the rest of this function,
832              * so if we do lose the race, the other thread should
833              * have found the same vnode and finished initializing
834              * the vcache entry.  Is it conceivable that this vcache
835              * entry could be recycled during this interval?  If so,
836              * then there probably needs to be some sort of additional
837              * mutual exclusion (an Embryonic flag would suffice).
838              * -GAW */
839             printf("afs_NewVCache: lost the race\n");
840             return (tvc);
841         }
842         tvc->v = vp;
843         tvc->v->v_data = tvc;
844         lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
845     }
846 #endif
847     tvc->parentVnode = 0;
848     tvc->mvid = NULL;
849     tvc->linkData = NULL;
850     tvc->cbExpires = 0;
851     tvc->opens = 0;
852     tvc->execsOrWriters = 0;
853     tvc->flockCount = 0;
854     tvc->anyAccess = 0;
855     tvc->states = 0;
856     tvc->last_looker = 0;
857     tvc->fid = *afid;
858     tvc->asynchrony = -1;
859     tvc->vc_error = 0;
860     afs_symhint_inval(tvc);
861 #ifdef AFS_TEXT_ENV
862     tvc->flushDV.low = tvc->flushDV.high = AFS_MAXDV;
863 #endif
864     hzero(tvc->mapDV);
865     tvc->truncPos = AFS_NOTRUNC;        /* don't truncate until we need to */
866     hzero(tvc->m.DataVersion);  /* in case we copy it into flushDV */
867 #if defined(AFS_LINUX22_ENV)
868     {
869         struct inode *ip = AFSTOI(tvc);
870 #if defined(AFS_LINUX24_ENV)
871         struct address_space *mapping = &ip->i_data;
872 #endif
873
874 #if defined(AFS_LINUX26_ENV)
875         inode_init_once(ip);
876 #else
877         sema_init(&ip->i_sem, 1);
878         INIT_LIST_HEAD(&ip->i_hash);
879         INIT_LIST_HEAD(&ip->i_dentry);
880 #if defined(AFS_LINUX24_ENV)
881         sema_init(&ip->i_zombie, 1);
882         init_waitqueue_head(&ip->i_wait);
883         spin_lock_init(&ip->i_data.i_shared_lock);
884 #ifdef STRUCT_ADDRESS_SPACE_HAS_PAGE_LOCK
885         spin_lock_init(&ip->i_data.page_lock);
886 #endif
887         INIT_LIST_HEAD(&ip->i_data.clean_pages);
888         INIT_LIST_HEAD(&ip->i_data.dirty_pages);
889         INIT_LIST_HEAD(&ip->i_data.locked_pages);
890         INIT_LIST_HEAD(&ip->i_dirty_buffers);
891 #ifdef STRUCT_INODE_HAS_I_DIRTY_DATA_BUFFERS
892         INIT_LIST_HEAD(&ip->i_dirty_data_buffers);
893 #endif
894 #ifdef STRUCT_INODE_HAS_I_DEVICES
895         INIT_LIST_HEAD(&ip->i_devices);
896 #endif
897 #ifdef STRUCT_INODE_HAS_I_TRUNCATE_SEM
898         init_rwsem(&ip->i_truncate_sem);
899 #endif
900 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
901         init_rwsem(&ip->i_alloc_sem);
902 #endif
903
904 #else /* AFS_LINUX22_ENV */
905         sema_init(&ip->i_atomic_write, 1);
906         init_waitqueue(&ip->i_wait);
907 #endif
908 #endif
909
910 #if defined(AFS_LINUX24_ENV)
911         mapping->host = ip;
912         ip->i_mapping = mapping;
913 #ifdef STRUCT_ADDRESS_SPACE_HAS_GFP_MASK
914         ip->i_data.gfp_mask = GFP_HIGHUSER;
915 #endif
916 #if defined(AFS_LINUX26_ENV)
917         mapping_set_gfp_mask(mapping, GFP_HIGHUSER);
918         {
919             extern struct backing_dev_info afs_backing_dev_info;
920
921             mapping->backing_dev_info = &afs_backing_dev_info;
922         }
923 #endif
924 #endif
925
926 #if !defined(AFS_LINUX26_ENV)
927         if (afs_globalVFS)
928             ip->i_dev = afs_globalVFS->s_dev;
929 #else
930 #ifdef STRUCT_INODE_HAS_I_SECURITY
931         ip->i_security = NULL;
932         if (security_inode_alloc(ip))
933             panic("Cannot allocate inode security");
934 #endif
935 #endif
936         ip->i_sb = afs_globalVFS;
937         put_inode_on_dummy_list(ip);
938 #ifdef STRUCT_INODE_HAS_I_SB_LIST
939         list_add(&ip->i_sb_list, &ip->i_sb->s_inodes);
940 #endif
941 #ifdef STRUCT_INODE_HAS_INOTIFY_LOCK
942         INIT_LIST_HEAD(&ip->inotify_watches); 
943         spin_lock_init(&ip->inotify_lock); 
944 #endif 
945     }
946 #endif
947
948 #ifdef  AFS_OSF_ENV
949     /* Hold it for the LRU (should make count 2) */
950     VN_HOLD(AFSTOV(tvc));
951 #else /* AFS_OSF_ENV */
952 #if !defined(AFS_XBSD_ENV)
953     VREFCOUNT_SET(tvc, 1);      /* us */
954 #endif /* AFS_XBSD_ENV */
955 #endif /* AFS_OSF_ENV */
956 #ifdef  AFS_AIX32_ENV
957     LOCK_INIT(&tvc->pvmlock, "vcache pvmlock");
958     tvc->vmh = tvc->segid = NULL;
959     tvc->credp = NULL;
960 #endif
961 #ifdef AFS_BOZONLOCK_ENV
962 #if     defined(AFS_SUN5_ENV)
963     rw_init(&tvc->rwlock, "vcache rwlock", RW_DEFAULT, NULL);
964
965 #if     defined(AFS_SUN55_ENV)
966     /* This is required if the kaio (kernel aynchronous io)
967      ** module is installed. Inside the kernel, the function
968      ** check_vp( common/os/aio.c) checks to see if the kernel has
969      ** to provide asynchronous io for this vnode. This
970      ** function extracts the device number by following the
971      ** v_data field of the vnode. If we do not set this field
972      ** then the system panics. The  value of the v_data field
973      ** is not really important for AFS vnodes because the kernel
974      ** does not do asynchronous io for regular files. Hence,
975      ** for the time being, we fill up the v_data field with the
976      ** vnode pointer itself. */
977     tvc->v.v_data = (char *)tvc;
978 #endif /* AFS_SUN55_ENV */
979 #endif
980     afs_BozonInit(&tvc->pvnLock, tvc);
981 #endif
982
983     tvc->Access = NULL;
984     tvc->callback = serverp;    /* to minimize chance that clear
985                                  * request is lost */
986     /* initialize vnode data, note vrefCount is v.v_count */
987 #ifdef  AFS_AIX_ENV
988     /* Don't forget to free the gnode space */
989     tvc->v.v_gnode = gnodepnt =
990         (struct gnode *)osi_AllocSmallSpace(sizeof(struct gnode));
991     memset((char *)gnodepnt, 0, sizeof(struct gnode));
992 #endif
993 #ifdef AFS_SGI64_ENV
994     memset((void *)&(tvc->vc_bhv_desc), 0, sizeof(tvc->vc_bhv_desc));
995     bhv_desc_init(&(tvc->vc_bhv_desc), tvc, tvc, &Afs_vnodeops);
996 #ifdef AFS_SGI65_ENV
997     vn_bhv_head_init(&(tvc->v.v_bh), "afsvp");
998     vn_bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
999 #else
1000     bhv_head_init(&(tvc->v.v_bh));
1001     bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
1002 #endif
1003 #ifdef AFS_SGI65_ENV
1004     tvc->v.v_mreg = tvc->v.v_mregb = (struct pregion *)tvc;
1005 #ifdef VNODE_TRACING
1006     tvc->v.v_trace = ktrace_alloc(VNODE_TRACE_SIZE, 0);
1007 #endif
1008     init_bitlock(&tvc->v.v_pcacheflag, VNODE_PCACHE_LOCKBIT, "afs_pcache",
1009                  tvc->v.v_number);
1010     init_mutex(&tvc->v.v_filocksem, MUTEX_DEFAULT, "afsvfl", (long)tvc);
1011     init_mutex(&tvc->v.v_buf_lock, MUTEX_DEFAULT, "afsvnbuf", (long)tvc);
1012 #endif
1013     vnode_pcache_init(&tvc->v);
1014 #if defined(DEBUG) && defined(VNODE_INIT_BITLOCK)
1015     /* Above define is never true execpt in SGI test kernels. */
1016     init_bitlock(&(tvc->v.v_flag, VLOCK, "vnode", tvc->v.v_number);
1017 #endif
1018 #ifdef INTR_KTHREADS
1019                  AFS_VN_INIT_BUF_LOCK(&(tvc->v));
1020 #endif
1021 #else
1022     SetAfsVnode(AFSTOV(tvc));
1023 #endif /* AFS_SGI64_ENV */
1024 #ifdef AFS_DARWIN_ENV
1025     tvc->v.v_ubcinfo = UBC_INFO_NULL;
1026     lockinit(&tvc->rwlock, PINOD, "vcache rwlock", 0, 0);
1027     cache_purge(AFSTOV(tvc));
1028     tvc->v.v_data = tvc;
1029     tvc->v.v_tag = VT_AFS;
1030     /* VLISTNONE(&tvc->v); */
1031     tvc->v.v_freelist.tqe_next = 0;
1032     tvc->v.v_freelist.tqe_prev = (struct vnode **)0xdeadb;
1033     tvc->vrefCount += DARWIN_REFBASE;
1034 #endif
1035     /*
1036      * The proper value for mvstat (for root fids) is setup by the caller.
1037      */
1038     tvc->mvstat = 0;
1039     if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
1040         tvc->mvstat = 2;
1041     if (afs_globalVFS == 0)
1042         osi_Panic("afs globalvfs");
1043     vSetVfsp(tvc, afs_globalVFS);
1044     vSetType(tvc, VREG);
1045 #ifdef  AFS_AIX_ENV
1046     tvc->v.v_vfsnext = afs_globalVFS->vfs_vnodes;       /* link off vfs */
1047     tvc->v.v_vfsprev = NULL;
1048     afs_globalVFS->vfs_vnodes = &tvc->v;
1049     if (tvc->v.v_vfsnext != NULL)
1050         tvc->v.v_vfsnext->v_vfsprev = &tvc->v;
1051     tvc->v.v_next = gnodepnt->gn_vnode; /*Single vnode per gnode for us! */
1052     gnodepnt->gn_vnode = &tvc->v;
1053 #endif
1054 #if     defined(AFS_DUX40_ENV)
1055     insmntque(tvc, afs_globalVFS, &afs_ubcops);
1056 #else
1057 #ifdef  AFS_OSF_ENV
1058     /* Is this needed??? */
1059     insmntque(tvc, afs_globalVFS);
1060 #endif /* AFS_OSF_ENV */
1061 #endif /* AFS_DUX40_ENV */
1062 #if defined(AFS_SGI_ENV)
1063     VN_SET_DPAGES(&(tvc->v), (struct pfdat *)NULL);
1064     osi_Assert((tvc->v.v_flag & VINACT) == 0);
1065     tvc->v.v_flag = 0;
1066     osi_Assert(VN_GET_PGCNT(&(tvc->v)) == 0);
1067     osi_Assert(tvc->mapcnt == 0 && tvc->vc_locktrips == 0);
1068     osi_Assert(tvc->vc_rwlockid == OSI_NO_LOCKID);
1069     osi_Assert(tvc->v.v_filocks == NULL);
1070 #if !defined(AFS_SGI65_ENV)
1071     osi_Assert(tvc->v.v_filocksem == NULL);
1072 #endif
1073     osi_Assert(tvc->cred == NULL);
1074 #ifdef AFS_SGI64_ENV
1075     vnode_pcache_reinit(&tvc->v);
1076     tvc->v.v_rdev = NODEV;
1077 #endif
1078     vn_initlist((struct vnlist *)&tvc->v);
1079     tvc->lastr = 0;
1080 #endif /* AFS_SGI_ENV */
1081     tvc->h1.dchint = 0;
1082     osi_dnlc_purgedp(tvc);      /* this may be overkill */
1083     memset((char *)&(tvc->quick), 0, sizeof(struct vtodc));
1084     memset((char *)&(tvc->callsort), 0, sizeof(struct afs_q));
1085     tvc->slocks = NULL;
1086     i = VCHash(afid);
1087
1088     tvc->hnext = afs_vhashT[i];
1089     afs_vhashT[i] = tvc;
1090     if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1091         refpanic("NewVCache VLRU inconsistent");
1092     }
1093     QAdd(&VLRU, &tvc->vlruq);   /* put in lruq */
1094     if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1095         refpanic("NewVCache VLRU inconsistent2");
1096     }
1097     if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
1098         refpanic("NewVCache VLRU inconsistent3");
1099     }
1100     if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
1101         refpanic("NewVCache VLRU inconsistent4");
1102     }
1103     vcachegen++;
1104
1105     return tvc;
1106
1107 }                               /*afs_NewVCache */
1108
1109
1110 /*
1111  * afs_FlushActiveVcaches
1112  *
1113  * Description:
1114  *      ???
1115  *
1116  * Parameters:
1117  *      doflocks : Do we handle flocks?
1118  */
1119 /* LOCK: afs_FlushActiveVcaches afs_xvcache N */
1120 void
1121 afs_FlushActiveVcaches(register afs_int32 doflocks)
1122 {
1123     register struct vcache *tvc;
1124     register int i;
1125     register struct conn *tc;
1126     register afs_int32 code;
1127     register struct AFS_UCRED *cred = NULL;
1128     struct vrequest treq, ureq;
1129     struct AFSVolSync tsync;
1130     int didCore;
1131     XSTATS_DECLS;
1132     AFS_STATCNT(afs_FlushActiveVcaches);
1133     ObtainReadLock(&afs_xvcache);
1134     for (i = 0; i < VCSIZE; i++) {
1135         for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1136             if (doflocks && tvc->flockCount != 0) {
1137                 /* if this entry has an flock, send a keep-alive call out */
1138                 osi_vnhold(tvc, 0);
1139                 ReleaseReadLock(&afs_xvcache);
1140                 ObtainWriteLock(&tvc->lock, 51);
1141                 do {
1142                     afs_InitReq(&treq, afs_osi_credp);
1143                     treq.flags |= O_NONBLOCK;
1144
1145                     tc = afs_Conn(&tvc->fid, &treq, SHARED_LOCK);
1146                     if (tc) {
1147                         XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
1148                         RX_AFS_GUNLOCK();
1149                         code =
1150                             RXAFS_ExtendLock(tc->id,
1151                                              (struct AFSFid *)&tvc->fid.Fid,
1152                                              &tsync);
1153                         RX_AFS_GLOCK();
1154                         XSTATS_END_TIME;
1155                     } else
1156                         code = -1;
1157                 } while (afs_Analyze
1158                          (tc, code, &tvc->fid, &treq,
1159                           AFS_STATS_FS_RPCIDX_EXTENDLOCK, SHARED_LOCK, NULL));
1160
1161                 ReleaseWriteLock(&tvc->lock);
1162                 ObtainReadLock(&afs_xvcache);
1163                 AFS_FAST_RELE(tvc);
1164             }
1165             didCore = 0;
1166             if ((tvc->states & CCore) || (tvc->states & CUnlinkedDel)) {
1167                 /*
1168                  * Don't let it evaporate in case someone else is in
1169                  * this code.  Also, drop the afs_xvcache lock while
1170                  * getting vcache locks.
1171                  */
1172                 osi_vnhold(tvc, 0);
1173                 ReleaseReadLock(&afs_xvcache);
1174 #ifdef AFS_BOZONLOCK_ENV
1175                 afs_BozonLock(&tvc->pvnLock, tvc);
1176 #endif
1177 #if defined(AFS_SGI_ENV)
1178                 /*
1179                  * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1180                  */
1181                 osi_Assert(VREFCOUNT(tvc) > 0);
1182                 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1183 #endif
1184                 ObtainWriteLock(&tvc->lock, 52);
1185                 if (tvc->states & CCore) {
1186                     tvc->states &= ~CCore;
1187                     /* XXXX Find better place-holder for cred XXXX */
1188                     cred = (struct AFS_UCRED *)tvc->linkData;
1189                     tvc->linkData = NULL;       /* XXX */
1190                     afs_InitReq(&ureq, cred);
1191                     afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1192                                ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
1193                                tvc->execsOrWriters);
1194                     code = afs_StoreOnLastReference(tvc, &ureq);
1195                     ReleaseWriteLock(&tvc->lock);
1196 #ifdef AFS_BOZONLOCK_ENV
1197                     afs_BozonUnlock(&tvc->pvnLock, tvc);
1198 #endif
1199                     hzero(tvc->flushDV);
1200                     osi_FlushText(tvc);
1201                     didCore = 1;
1202                     if (code && code != VNOVNODE) {
1203                         afs_StoreWarn(code, tvc->fid.Fid.Volume,
1204                                       /* /dev/console */ 1);
1205                     }
1206                 } else if (tvc->states & CUnlinkedDel) {
1207                     /*
1208                      * Ignore errors
1209                      */
1210                     ReleaseWriteLock(&tvc->lock);
1211 #ifdef AFS_BOZONLOCK_ENV
1212                     afs_BozonUnlock(&tvc->pvnLock, tvc);
1213 #endif
1214 #if defined(AFS_SGI_ENV)
1215                     AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1216 #endif
1217                     afs_remunlink(tvc, 0);
1218 #if defined(AFS_SGI_ENV)
1219                     AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1220 #endif
1221                 } else {
1222                     /* lost (or won, perhaps) the race condition */
1223                     ReleaseWriteLock(&tvc->lock);
1224 #ifdef AFS_BOZONLOCK_ENV
1225                     afs_BozonUnlock(&tvc->pvnLock, tvc);
1226 #endif
1227                 }
1228 #if defined(AFS_SGI_ENV)
1229                 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1230 #endif
1231                 ObtainReadLock(&afs_xvcache);
1232                 AFS_FAST_RELE(tvc);
1233                 if (didCore) {
1234                     AFS_RELE(AFSTOV(tvc));
1235                     /* Matches write code setting CCore flag */
1236                     crfree(cred);
1237                 }
1238             }
1239 #ifdef AFS_DARWIN_ENV
1240             if (VREFCOUNT(tvc) == 1 + DARWIN_REFBASE
1241                 && UBCINFOEXISTS(&tvc->v)) {
1242                 if (tvc->opens)
1243                     panic("flushactive open, hasubc, but refcnt 1");
1244                 osi_VM_TryReclaim(tvc, 0);
1245             }
1246 #endif
1247         }
1248     }
1249     ReleaseReadLock(&afs_xvcache);
1250 }
1251
1252
1253 /*
1254  * afs_VerifyVCache
1255  *
1256  * Description:
1257  *      Make sure a cache entry is up-to-date status-wise.
1258  *
1259  * NOTE: everywhere that calls this can potentially be sped up
1260  *       by checking CStatd first, and avoiding doing the InitReq
1261  *       if this is up-to-date.
1262  *
1263  *  Anymore, the only places that call this KNOW already that the
1264  *  vcache is not up-to-date, so we don't screw around.
1265  *
1266  * Parameters:
1267  *      avc  : Ptr to vcache entry to verify.
1268  *      areq : ???
1269  */
1270
1271 int
1272 afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1273 {
1274     register struct vcache *tvc;
1275
1276     AFS_STATCNT(afs_VerifyVCache);
1277
1278 #if defined(AFS_OSF_ENV)
1279     ObtainReadLock(&avc->lock);
1280     if (afs_IsWired(avc)) {
1281         ReleaseReadLock(&avc->lock);
1282         return 0;
1283     }
1284     ReleaseReadLock(&avc->lock);
1285 #endif /* AFS_OSF_ENV */
1286     /* otherwise we must fetch the status info */
1287
1288     ObtainWriteLock(&avc->lock, 53);
1289     if (avc->states & CStatd) {
1290         ReleaseWriteLock(&avc->lock);
1291         return 0;
1292     }
1293     ObtainWriteLock(&afs_xcbhash, 461);
1294     avc->states &= ~(CStatd | CUnique);
1295     avc->callback = NULL;
1296     afs_DequeueCallback(avc);
1297     ReleaseWriteLock(&afs_xcbhash);
1298     ReleaseWriteLock(&avc->lock);
1299
1300     /* since we've been called back, or the callback has expired,
1301      * it's possible that the contents of this directory, or this
1302      * file's name have changed, thus invalidating the dnlc contents.
1303      */
1304     if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1305         osi_dnlc_purgedp(avc);
1306     else
1307         osi_dnlc_purgevp(avc);
1308
1309     /* fetch the status info */
1310     tvc = afs_GetVCache(&avc->fid, areq, NULL, avc);
1311     if (!tvc)
1312         return ENOENT;
1313     /* Put it back; caller has already incremented vrefCount */
1314     afs_PutVCache(tvc);
1315     return 0;
1316
1317 }                               /*afs_VerifyVCache */
1318
1319
1320 /*
1321  * afs_SimpleVStat
1322  *
1323  * Description:
1324  *      Simple copy of stat info into cache.
1325  *
1326  * Parameters:
1327  *      avc   : Ptr to vcache entry involved.
1328  *      astat : Ptr to stat info to copy.
1329  *
1330  * Environment:
1331  *      Nothing interesting.
1332  *
1333  * Callers:  as of 1992-04-29, only called by WriteVCache
1334  */
1335 static void
1336 afs_SimpleVStat(register struct vcache *avc,
1337                 register struct AFSFetchStatus *astat, struct vrequest *areq)
1338 {
1339     afs_size_t length;
1340     AFS_STATCNT(afs_SimpleVStat);
1341
1342 #ifdef AFS_SGI_ENV
1343     if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1344         && !AFS_VN_MAPPED((vnode_t *) avc)) {
1345 #else
1346     if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1347 #endif
1348 #ifdef AFS_64BIT_CLIENT
1349         FillInt64(length, astat->Length_hi, astat->Length);
1350 #else /* AFS_64BIT_CLIENT */
1351         length = astat->Length;
1352 #endif /* AFS_64BIT_CLIENT */
1353 #if defined(AFS_SGI_ENV)
1354         osi_Assert((valusema(&avc->vc_rwlock) <= 0)
1355                    && (OSI_GET_LOCKID() == avc->vc_rwlockid));
1356         if (length < avc->m.Length) {
1357             vnode_t *vp = (vnode_t *) avc;
1358
1359             osi_Assert(WriteLocked(&avc->lock));
1360             ReleaseWriteLock(&avc->lock);
1361             AFS_GUNLOCK();
1362             PTOSSVP(vp, (off_t) length, (off_t) MAXLONG);
1363             AFS_GLOCK();
1364             ObtainWriteLock(&avc->lock, 67);
1365         }
1366 #endif
1367         /* if writing the file, don't fetch over this value */
1368         afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT, ICL_TYPE_POINTER, avc,
1369                    ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1370                    ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1371         avc->m.Length = length;
1372         avc->m.Date = astat->ClientModTime;
1373     }
1374     avc->m.Owner = astat->Owner;
1375     avc->m.Group = astat->Group;
1376     avc->m.Mode = astat->UnixModeBits;
1377     if (vType(avc) == VREG) {
1378         avc->m.Mode |= S_IFREG;
1379     } else if (vType(avc) == VDIR) {
1380         avc->m.Mode |= S_IFDIR;
1381     } else if (vType(avc) == VLNK) {
1382         avc->m.Mode |= S_IFLNK;
1383         if ((avc->m.Mode & 0111) == 0)
1384             avc->mvstat = 1;
1385     }
1386     if (avc->states & CForeign) {
1387         struct axscache *ac;
1388         avc->anyAccess = astat->AnonymousAccess;
1389 #ifdef badidea
1390         if ((astat->CallerAccess & ~astat->AnonymousAccess))
1391             /*   USED TO SAY :
1392              * Caller has at least one bit not covered by anonymous, and
1393              * thus may have interesting rights.
1394              *
1395              * HOWEVER, this is a really bad idea, because any access query
1396              * for bits which aren't covered by anonymous, on behalf of a user
1397              * who doesn't have any special rights, will result in an answer of
1398              * the form "I don't know, lets make a FetchStatus RPC and find out!"
1399              * It's an especially bad idea under Ultrix, since (due to the lack of
1400              * a proper access() call) it must perform several afs_access() calls
1401              * in order to create magic mode bits that vary according to who makes
1402              * the call.  In other words, _every_ stat() generates a test for
1403              * writeability...
1404              */
1405 #endif /* badidea */
1406             if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1407                 ac->axess = astat->CallerAccess;
1408             else                /* not found, add a new one if possible */
1409                 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1410     }
1411
1412
1413 }                               /*afs_SimpleVStat */
1414
1415
1416 /*
1417  * afs_WriteVCache
1418  *
1419  * Description:
1420  *      Store the status info *only* back to the server for a
1421  *      fid/vrequest.
1422  *
1423  * Parameters:
1424  *      avc     : Ptr to the vcache entry.
1425  *      astatus : Ptr to the status info to store.
1426  *      areq    : Ptr to the associated vrequest.
1427  *
1428  * Environment:
1429  *      Must be called with a shared lock held on the vnode.
1430  */
1431
1432 int
1433 afs_WriteVCache(register struct vcache *avc,
1434                 register struct AFSStoreStatus *astatus,
1435                 struct vrequest *areq)
1436 {
1437     afs_int32 code;
1438     struct conn *tc;
1439     struct AFSFetchStatus OutStatus;
1440     struct AFSVolSync tsync;
1441     XSTATS_DECLS;
1442     AFS_STATCNT(afs_WriteVCache);
1443     afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1444                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
1445
1446     do {
1447         tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
1448         if (tc) {
1449             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1450             RX_AFS_GUNLOCK();
1451             code =
1452                 RXAFS_StoreStatus(tc->id, (struct AFSFid *)&avc->fid.Fid,
1453                                   astatus, &OutStatus, &tsync);
1454             RX_AFS_GLOCK();
1455             XSTATS_END_TIME;
1456         } else
1457             code = -1;
1458     } while (afs_Analyze
1459              (tc, code, &avc->fid, areq, AFS_STATS_FS_RPCIDX_STORESTATUS,
1460               SHARED_LOCK, NULL));
1461
1462     UpgradeSToWLock(&avc->lock, 20);
1463     if (code == 0) {
1464         /* success, do the changes locally */
1465         afs_SimpleVStat(avc, &OutStatus, areq);
1466         /*
1467          * Update the date, too.  SimpleVStat didn't do this, since
1468          * it thought we were doing this after fetching new status
1469          * over a file being written.
1470          */
1471         avc->m.Date = OutStatus.ClientModTime;
1472     } else {
1473         /* failure, set up to check with server next time */
1474         ObtainWriteLock(&afs_xcbhash, 462);
1475         afs_DequeueCallback(avc);
1476         avc->states &= ~(CStatd | CUnique);     /* turn off stat valid flag */
1477         ReleaseWriteLock(&afs_xcbhash);
1478         if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1479             osi_dnlc_purgedp(avc);      /* if it (could be) a directory */
1480     }
1481     ConvertWToSLock(&avc->lock);
1482     return code;
1483
1484 }                               /*afs_WriteVCache */
1485
1486 /*
1487  * afs_ProcessFS
1488  *
1489  * Description:
1490  *      Copy astat block into vcache info
1491  *
1492  * Parameters:
1493  *      avc   : Ptr to vcache entry.
1494  *      astat : Ptr to stat block to copy in.
1495  *      areq  : Ptr to associated request.
1496  *
1497  * Environment:
1498  *      Must be called under a write lock
1499  *
1500  * Note: this code may get dataversion and length out of sync if the file has
1501  *       been modified.  This is less than ideal.  I haven't thought about
1502  *       it sufficiently to be certain that it is adequate.
1503  */
1504 void
1505 afs_ProcessFS(register struct vcache *avc,
1506               register struct AFSFetchStatus *astat, struct vrequest *areq)
1507 {
1508     afs_size_t length;
1509     AFS_STATCNT(afs_ProcessFS);
1510
1511 #ifdef AFS_64BIT_CLIENT
1512     FillInt64(length, astat->Length_hi, astat->Length);
1513 #else /* AFS_64BIT_CLIENT */
1514     length = astat->Length;
1515 #endif /* AFS_64BIT_CLIENT */
1516     /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1517      * number for each bulk status request. Under no circumstances
1518      * should afs_DoBulkStat store a sequence number if the new
1519      * length will be ignored when afs_ProcessFS is called with
1520      * new stats. If you change the following conditional then you
1521      * also need to change the conditional in afs_DoBulkStat.  */
1522 #ifdef AFS_SGI_ENV
1523     if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1524         && !AFS_VN_MAPPED((vnode_t *) avc)) {
1525 #else
1526     if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1527 #endif
1528         /* if we're writing or mapping this file, don't fetch over these
1529          *  values.
1530          */
1531         afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1532                    ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1533                    ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1534         avc->m.Length = length;
1535         avc->m.Date = astat->ClientModTime;
1536     }
1537     hset64(avc->m.DataVersion, astat->dataVersionHigh, astat->DataVersion);
1538     avc->m.Owner = astat->Owner;
1539     avc->m.Mode = astat->UnixModeBits;
1540     avc->m.Group = astat->Group;
1541     avc->m.LinkCount = astat->LinkCount;
1542     if (astat->FileType == File) {
1543         vSetType(avc, VREG);
1544         avc->m.Mode |= S_IFREG;
1545     } else if (astat->FileType == Directory) {
1546         vSetType(avc, VDIR);
1547         avc->m.Mode |= S_IFDIR;
1548     } else if (astat->FileType == SymbolicLink) {
1549         if (afs_fakestat_enable && (avc->m.Mode & 0111) == 0) {
1550             vSetType(avc, VDIR);
1551             avc->m.Mode |= S_IFDIR;
1552         } else {
1553             vSetType(avc, VLNK);
1554             avc->m.Mode |= S_IFLNK;
1555         }
1556         if ((avc->m.Mode & 0111) == 0) {
1557             avc->mvstat = 1;
1558         }
1559     }
1560     avc->anyAccess = astat->AnonymousAccess;
1561 #ifdef badidea
1562     if ((astat->CallerAccess & ~astat->AnonymousAccess))
1563         /*   USED TO SAY :
1564          * Caller has at least one bit not covered by anonymous, and
1565          * thus may have interesting rights.
1566          *
1567          * HOWEVER, this is a really bad idea, because any access query
1568          * for bits which aren't covered by anonymous, on behalf of a user
1569          * who doesn't have any special rights, will result in an answer of
1570          * the form "I don't know, lets make a FetchStatus RPC and find out!"
1571          * It's an especially bad idea under Ultrix, since (due to the lack of
1572          * a proper access() call) it must perform several afs_access() calls
1573          * in order to create magic mode bits that vary according to who makes
1574          * the call.  In other words, _every_ stat() generates a test for
1575          * writeability...
1576          */
1577 #endif /* badidea */
1578     {
1579         struct axscache *ac;
1580         if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1581             ac->axess = astat->CallerAccess;
1582         else                    /* not found, add a new one if possible */
1583             afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1584     }
1585 #ifdef AFS_LINUX22_ENV
1586     vcache2inode(avc);          /* Set the inode attr cache */
1587 #endif
1588 #ifdef AFS_DARWIN_ENV
1589     osi_VM_Setup(avc, 1);
1590 #endif
1591
1592 }                               /*afs_ProcessFS */
1593
1594
1595 int
1596 afs_RemoteLookup(register struct VenusFid *afid, struct vrequest *areq,
1597                  char *name, struct VenusFid *nfid,
1598                  struct AFSFetchStatus *OutStatusp,
1599                  struct AFSCallBack *CallBackp, struct server **serverp,
1600                  struct AFSVolSync *tsyncp)
1601 {
1602     afs_int32 code;
1603     afs_uint32 start;
1604     register struct conn *tc;
1605     struct AFSFetchStatus OutDirStatus;
1606     XSTATS_DECLS;
1607     if (!name)
1608         name = "";              /* XXX */
1609     do {
1610         tc = afs_Conn(afid, areq, SHARED_LOCK);
1611         if (tc) {
1612             if (serverp)
1613                 *serverp = tc->srvr->server;
1614             start = osi_Time();
1615             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1616             RX_AFS_GUNLOCK();
1617             code =
1618                 RXAFS_Lookup(tc->id, (struct AFSFid *)&afid->Fid, name,
1619                              (struct AFSFid *)&nfid->Fid, OutStatusp,
1620                              &OutDirStatus, CallBackp, tsyncp);
1621             RX_AFS_GLOCK();
1622             XSTATS_END_TIME;
1623         } else
1624             code = -1;
1625     } while (afs_Analyze
1626              (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_XLOOKUP, SHARED_LOCK,
1627               NULL));
1628
1629     return code;
1630 }
1631
1632
1633 /*
1634  * afs_GetVCache
1635  *
1636  * Description:
1637  *      Given a file id and a vrequest structure, fetch the status
1638  *      information associated with the file.
1639  *
1640  * Parameters:
1641  *      afid : File ID.
1642  *      areq : Ptr to associated vrequest structure, specifying the
1643  *              user whose authentication tokens will be used.
1644  *      avc  : caller may already have a vcache for this file, which is
1645  *             already held.
1646  *
1647  * Environment:
1648  *      The cache entry is returned with an increased vrefCount field.
1649  *      The entry must be discarded by calling afs_PutVCache when you
1650  *      are through using the pointer to the cache entry.
1651  *
1652  *      You should not hold any locks when calling this function, except
1653  *      locks on other vcache entries.  If you lock more than one vcache
1654  *      entry simultaneously, you should lock them in this order:
1655  *
1656  *          1. Lock all files first, then directories.
1657  *          2.  Within a particular type, lock entries in Fid.Vnode order.
1658  *
1659  *      This locking hierarchy is convenient because it allows locking
1660  *      of a parent dir cache entry, given a file (to check its access
1661  *      control list).  It also allows renames to be handled easily by
1662  *      locking directories in a constant order.
1663  * NB.  NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1664  */
1665    /* might have a vcache structure already, which must
1666     * already be held by the caller */
1667
1668 struct vcache *
1669 afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
1670               afs_int32 * cached, struct vcache *avc)
1671 {
1672
1673     afs_int32 code, newvcache = 0;
1674     register struct vcache *tvc;
1675     struct volume *tvp;
1676     afs_int32 retry;
1677
1678     AFS_STATCNT(afs_GetVCache);
1679
1680     if (cached)
1681         *cached = 0;            /* Init just in case */
1682
1683 #if     defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1684   loop:
1685 #endif
1686
1687     ObtainSharedLock(&afs_xvcache, 5);
1688
1689     tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU);
1690     if (tvc && retry) {
1691 #if     defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1692         ReleaseSharedLock(&afs_xvcache);
1693         spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1694         goto loop;
1695 #endif
1696     }
1697
1698     if (tvc) {
1699         if (cached)
1700             *cached = 1;
1701         if (tvc->states & CStatd) {
1702             ReleaseSharedLock(&afs_xvcache);
1703             return tvc;
1704         }
1705     } else {
1706         UpgradeSToWLock(&afs_xvcache, 21);
1707
1708         /* no cache entry, better grab one */
1709         tvc = afs_NewVCache(afid, NULL);
1710         newvcache = 1;
1711
1712         ConvertWToSLock(&afs_xvcache);
1713         afs_stats_cmperf.vcacheMisses++;
1714     }
1715
1716     ReleaseSharedLock(&afs_xvcache);
1717
1718     ObtainWriteLock(&tvc->lock, 54);
1719
1720     if (tvc->states & CStatd) {
1721 #ifdef AFS_LINUX22_ENV
1722         vcache2inode(tvc);
1723 #endif
1724         ReleaseWriteLock(&tvc->lock);
1725 #ifdef AFS_DARWIN_ENV
1726         osi_VM_Setup(tvc, 0);
1727 #endif
1728         return tvc;
1729     }
1730 #if defined(AFS_OSF_ENV)
1731     if (afs_IsWired(tvc)) {
1732         ReleaseWriteLock(&tvc->lock);
1733         return tvc;
1734     }
1735 #endif /* AFS_OSF_ENV */
1736 #ifdef AFS_OBSD_ENV
1737     VOP_LOCK(AFSTOV(tvc), LK_EXCLUSIVE | LK_RETRY, curproc);
1738     uvm_vnp_uncache(AFSTOV(tvc));
1739     VOP_UNLOCK(AFSTOV(tvc), 0, curproc);
1740 #endif
1741 #ifdef AFS_FBSD_ENV
1742     /*
1743      * XXX - I really don't like this.  Should try to understand better.
1744      * It seems that sometimes, when we get called, we already hold the
1745      * lock on the vnode (e.g., from afs_getattr via afs_VerifyVCache).
1746      * We can't drop the vnode lock, because that could result in a race.
1747      * Sometimes, though, we get here and don't hold the vnode lock.
1748      * I hate code paths that sometimes hold locks and sometimes don't.
1749      * In any event, the dodge we use here is to check whether the vnode
1750      * is locked, and if it isn't, then we gain and drop it around the call
1751      * to vinvalbuf; otherwise, we leave it alone.
1752      */
1753     {
1754         struct vnode *vp;
1755         int iheldthelock;
1756
1757         vp = AFSTOV(tvc);
1758 #ifdef AFS_FBSD50_ENV
1759         iheldthelock = VOP_ISLOCKED(vp, curthread);
1760         if (!iheldthelock)
1761             vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1762         vinvalbuf(vp, V_SAVE, osi_curcred(), curthread, PINOD, 0);
1763         if (!iheldthelock)
1764             VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1765 #else
1766         iheldthelock = VOP_ISLOCKED(vp, curproc);
1767         if (!iheldthelock)
1768             vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1769         vinvalbuf(vp, V_SAVE, osi_curcred(), curproc, PINOD, 0);
1770         if (!iheldthelock)
1771             VOP_UNLOCK(vp, LK_EXCLUSIVE, curproc);
1772 #endif
1773     }
1774 #endif
1775
1776     ObtainWriteLock(&afs_xcbhash, 464);
1777     tvc->states &= ~CUnique;
1778     tvc->callback = 0;
1779     afs_DequeueCallback(tvc);
1780     ReleaseWriteLock(&afs_xcbhash);
1781
1782     /* It is always appropriate to throw away all the access rights? */
1783     afs_FreeAllAxs(&(tvc->Access));
1784     tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1785     if (tvp) {
1786         if ((tvp->states & VForeign)) {
1787             if (newvcache)
1788                 tvc->states |= CForeign;
1789             if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1790                 && (tvp->rootUnique == afid->Fid.Unique)) {
1791                 tvc->mvstat = 2;
1792             }
1793         }
1794         if (tvp->states & VRO)
1795             tvc->states |= CRO;
1796         if (tvp->states & VBackup)
1797             tvc->states |= CBackup;
1798         /* now copy ".." entry back out of volume structure, if necessary */
1799         if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1800             if (!tvc->mvid)
1801                 tvc->mvid = (struct VenusFid *)
1802                     osi_AllocSmallSpace(sizeof(struct VenusFid));
1803             *tvc->mvid = tvp->dotdot;
1804         }
1805         afs_PutVolume(tvp, READ_LOCK);
1806     }
1807
1808     /* stat the file */
1809     afs_RemoveVCB(afid);
1810     {
1811         struct AFSFetchStatus OutStatus;
1812
1813         if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1814             afs_ProcessFS(tvc, &OutStatus, areq);
1815             tvc->states |= CStatd | CUnique;
1816             code = 0;
1817         } else {
1818             code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1819         }
1820     }
1821
1822     if (code) {
1823         ReleaseWriteLock(&tvc->lock);
1824
1825         ObtainReadLock(&afs_xvcache);
1826         AFS_FAST_RELE(tvc);
1827         ReleaseReadLock(&afs_xvcache);
1828         return NULL;
1829     }
1830
1831     ReleaseWriteLock(&tvc->lock);
1832     return tvc;
1833
1834 }                               /*afs_GetVCache */
1835
1836
1837
1838 struct vcache *
1839 afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1840                  afs_int32 * cached, struct vcache *adp, char *aname)
1841 {
1842     afs_int32 code, now, newvcache = 0;
1843     struct VenusFid nfid;
1844     register struct vcache *tvc;
1845     struct volume *tvp;
1846     struct AFSFetchStatus OutStatus;
1847     struct AFSCallBack CallBack;
1848     struct AFSVolSync tsync;
1849     struct server *serverp = 0;
1850     afs_int32 origCBs;
1851     afs_int32 retry;
1852
1853     AFS_STATCNT(afs_GetVCache);
1854     if (cached)
1855         *cached = 0;            /* Init just in case */
1856
1857 #if     defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1858   loop1:
1859 #endif
1860
1861     ObtainReadLock(&afs_xvcache);
1862     tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */ );
1863
1864     if (tvc) {
1865         ReleaseReadLock(&afs_xvcache);
1866         if (retry) {
1867 #if     defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1868             spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1869             goto loop1;
1870 #endif
1871         }
1872         ObtainReadLock(&tvc->lock);
1873
1874         if (tvc->states & CStatd) {
1875             if (cached) {
1876                 *cached = 1;
1877             }
1878             ReleaseReadLock(&tvc->lock);
1879             return tvc;
1880         }
1881         tvc->states &= ~CUnique;
1882
1883         ReleaseReadLock(&tvc->lock);
1884         ObtainReadLock(&afs_xvcache);
1885         AFS_FAST_RELE(tvc);
1886     }
1887     /* if (tvc) */
1888     ReleaseReadLock(&afs_xvcache);
1889
1890     /* lookup the file */
1891     nfid = *afid;
1892     now = osi_Time();
1893     origCBs = afs_allCBs;       /* if anything changes, we don't have a cb */
1894     code =
1895         afs_RemoteLookup(&adp->fid, areq, aname, &nfid, &OutStatus, &CallBack,
1896                          &serverp, &tsync);
1897
1898 #if     defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1899   loop2:
1900 #endif
1901
1902     ObtainSharedLock(&afs_xvcache, 6);
1903     tvc = afs_FindVCache(&nfid, &retry, DO_VLRU /* no xstats now */ );
1904     if (tvc && retry) {
1905 #if     defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1906         ReleaseSharedLock(&afs_xvcache);
1907         spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1908         goto loop2;
1909 #endif
1910     }
1911
1912     if (!tvc) {
1913         /* no cache entry, better grab one */
1914         UpgradeSToWLock(&afs_xvcache, 22);
1915         tvc = afs_NewVCache(&nfid, serverp);
1916         newvcache = 1;
1917         ConvertWToSLock(&afs_xvcache);
1918     }
1919
1920     ReleaseSharedLock(&afs_xvcache);
1921     ObtainWriteLock(&tvc->lock, 55);
1922
1923     /* It is always appropriate to throw away all the access rights? */
1924     afs_FreeAllAxs(&(tvc->Access));
1925     tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
1926     if (tvp) {
1927         if ((tvp->states & VForeign)) {
1928             if (newvcache)
1929                 tvc->states |= CForeign;
1930             if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1931                 && (tvp->rootUnique == afid->Fid.Unique))
1932                 tvc->mvstat = 2;
1933         }
1934         if (tvp->states & VRO)
1935             tvc->states |= CRO;
1936         if (tvp->states & VBackup)
1937             tvc->states |= CBackup;
1938         /* now copy ".." entry back out of volume structure, if necessary */
1939         if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1940             if (!tvc->mvid)
1941                 tvc->mvid = (struct VenusFid *)
1942                     osi_AllocSmallSpace(sizeof(struct VenusFid));
1943             *tvc->mvid = tvp->dotdot;
1944         }
1945     }
1946
1947     if (code) {
1948         ObtainWriteLock(&afs_xcbhash, 465);
1949         afs_DequeueCallback(tvc);
1950         tvc->states &= ~(CStatd | CUnique);
1951         ReleaseWriteLock(&afs_xcbhash);
1952         if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1953             osi_dnlc_purgedp(tvc);      /* if it (could be) a directory */
1954         if (tvp)
1955             afs_PutVolume(tvp, READ_LOCK);
1956         ReleaseWriteLock(&tvc->lock);
1957         ObtainReadLock(&afs_xvcache);
1958         AFS_FAST_RELE(tvc);
1959         ReleaseReadLock(&afs_xvcache);
1960         return NULL;
1961     }
1962
1963     ObtainWriteLock(&afs_xcbhash, 466);
1964     if (origCBs == afs_allCBs) {
1965         if (CallBack.ExpirationTime) {
1966             tvc->callback = serverp;
1967             tvc->cbExpires = CallBack.ExpirationTime + now;
1968             tvc->states |= CStatd | CUnique;
1969             tvc->states &= ~CBulkFetching;
1970             afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
1971         } else if (tvc->states & CRO) {
1972             /* adapt gives us an hour. */
1973             tvc->cbExpires = 3600 + osi_Time();
1974              /*XXX*/ tvc->states |= CStatd | CUnique;
1975             tvc->states &= ~CBulkFetching;
1976             afs_QueueCallback(tvc, CBHash(3600), tvp);
1977         } else {
1978             tvc->callback = NULL;
1979             afs_DequeueCallback(tvc);
1980             tvc->states &= ~(CStatd | CUnique);
1981             if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1982                 osi_dnlc_purgedp(tvc);  /* if it (could be) a directory */
1983         }
1984     } else {
1985         afs_DequeueCallback(tvc);
1986         tvc->states &= ~CStatd;
1987         tvc->states &= ~CUnique;
1988         tvc->callback = NULL;
1989         if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1990             osi_dnlc_purgedp(tvc);      /* if it (could be) a directory */
1991     }
1992     ReleaseWriteLock(&afs_xcbhash);
1993     if (tvp)
1994         afs_PutVolume(tvp, READ_LOCK);
1995     afs_ProcessFS(tvc, &OutStatus, areq);
1996
1997     ReleaseWriteLock(&tvc->lock);
1998     return tvc;
1999
2000 }
2001
2002 struct vcache *
2003 afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
2004                   afs_int32 * cached, struct volume *tvolp)
2005 {
2006     afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
2007     afs_int32 getNewFid = 0;
2008     afs_uint32 start;
2009     struct VenusFid nfid;
2010     register struct vcache *tvc;
2011     struct server *serverp = 0;
2012     struct AFSFetchStatus OutStatus;
2013     struct AFSCallBack CallBack;
2014     struct AFSVolSync tsync;
2015     int origCBs = 0;
2016
2017     start = osi_Time();
2018
2019   newmtpt:
2020     if (!tvolp->rootVnode || getNewFid) {
2021         struct VenusFid tfid;
2022
2023         tfid = *afid;
2024         tfid.Fid.Vnode = 0;     /* Means get rootfid of volume */
2025         origCBs = afs_allCBs;   /* ignore InitCallBackState */
2026         code =
2027             afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2028                              &serverp, &tsync);
2029         if (code) {
2030             return NULL;
2031         }
2032 /*      ReleaseReadLock(&tvolp->lock);           */
2033         ObtainWriteLock(&tvolp->lock, 56);
2034         tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
2035         tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
2036         ReleaseWriteLock(&tvolp->lock);
2037 /*      ObtainReadLock(&tvolp->lock);*/
2038         haveStatus = 1;
2039     } else {
2040         afid->Fid.Vnode = tvolp->rootVnode;
2041         afid->Fid.Unique = tvolp->rootUnique;
2042     }
2043
2044     ObtainSharedLock(&afs_xvcache, 7);
2045     i = VCHash(afid);
2046     for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2047         if (!FidCmp(&(tvc->fid), afid)) {
2048 #ifdef  AFS_OSF_ENV
2049             /* Grab this vnode, possibly reactivating from the free list */
2050             /* for the present (95.05.25) everything on the hash table is
2051              * definitively NOT in the free list -- at least until afs_reclaim
2052              * can be safely implemented */
2053             int vg;
2054             AFS_GUNLOCK();
2055             vg = vget(AFSTOV(tvc));     /* this bumps ref count */
2056             AFS_GLOCK();
2057             if (vg)
2058                 continue;
2059 #endif /* AFS_OSF_ENV */
2060 #ifdef AFS_DARWIN14_ENV
2061             /* It'd really suck if we allowed duplicate vcaches for the 
2062              * same fid to happen. Wonder if this will work? */
2063             struct vnode *vp = AFSTOV(tvc);
2064             if (vp->v_flag & (VXLOCK | VORECLAIM | VTERMINATE)) {
2065                 printf("precluded FindVCache on %x (%d:%d:%d)\n",
2066                        vp, tvc->fid.Fid.Volume, tvc->fid.Fid.Vnode,
2067                        tvc->fid.Fid.Unique);
2068                 simple_lock(&vp->v_interlock);
2069                 SET(vp->v_flag, VTERMWANT);
2070                 simple_unlock(&vp->v_interlock);
2071                 (void)tsleep((caddr_t) & vp->v_ubcinfo, PINOD, "vget1", 0);
2072                 printf("VTERMWANT ended on %x\n", vp);
2073                 continue;
2074             }
2075 #endif
2076             break;
2077         }
2078     }
2079
2080     if (!haveStatus && (!tvc || !(tvc->states & CStatd))) {
2081         /* Mount point no longer stat'd or unknown. FID may have changed. */
2082 #ifdef AFS_OSF_ENV
2083         if (tvc)
2084             AFS_RELE(AFSTOV(tvc));
2085 #endif
2086         tvc = NULL;
2087         getNewFid = 1;
2088         ReleaseSharedLock(&afs_xvcache);
2089         goto newmtpt;
2090     }
2091
2092     if (!tvc) {
2093         UpgradeSToWLock(&afs_xvcache, 23);
2094         /* no cache entry, better grab one */
2095         tvc = afs_NewVCache(afid, NULL);
2096         newvcache = 1;
2097         afs_stats_cmperf.vcacheMisses++;
2098     } else {
2099         if (cached)
2100             *cached = 1;
2101         afs_stats_cmperf.vcacheHits++;
2102 #ifdef  AFS_OSF_ENV
2103         /* we already bumped the ref count in the for loop above */
2104 #else /* AFS_OSF_ENV */
2105         osi_vnhold(tvc, 0);
2106 #endif
2107         UpgradeSToWLock(&afs_xvcache, 24);
2108         if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2109             refpanic("GRVC VLRU inconsistent0");
2110         }
2111         if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2112             refpanic("GRVC VLRU inconsistent1");
2113         }
2114         if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2115             refpanic("GRVC VLRU inconsistent2");
2116         }
2117         QRemove(&tvc->vlruq);   /* move to lruq head */
2118         QAdd(&VLRU, &tvc->vlruq);
2119         if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2120             refpanic("GRVC VLRU inconsistent3");
2121         }
2122         if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2123             refpanic("GRVC VLRU inconsistent4");
2124         }
2125         if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2126             refpanic("GRVC VLRU inconsistent5");
2127         }
2128         vcachegen++;
2129     }
2130
2131     ReleaseWriteLock(&afs_xvcache);
2132
2133     if (tvc->states & CStatd) {
2134         return tvc;
2135     } else {
2136
2137         ObtainReadLock(&tvc->lock);
2138         tvc->states &= ~CUnique;
2139         tvc->callback = NULL;   /* redundant, perhaps */
2140         ReleaseReadLock(&tvc->lock);
2141     }
2142
2143     ObtainWriteLock(&tvc->lock, 57);
2144
2145     /* It is always appropriate to throw away all the access rights? */
2146     afs_FreeAllAxs(&(tvc->Access));
2147
2148     if (newvcache)
2149         tvc->states |= CForeign;
2150     if (tvolp->states & VRO)
2151         tvc->states |= CRO;
2152     if (tvolp->states & VBackup)
2153         tvc->states |= CBackup;
2154     /* now copy ".." entry back out of volume structure, if necessary */
2155     if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2156         && (tvolp->rootUnique == afid->Fid.Unique)) {
2157         tvc->mvstat = 2;
2158     }
2159     if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2160         if (!tvc->mvid)
2161             tvc->mvid = (struct VenusFid *)
2162                 osi_AllocSmallSpace(sizeof(struct VenusFid));
2163         *tvc->mvid = tvolp->dotdot;
2164     }
2165
2166     /* stat the file */
2167     afs_RemoveVCB(afid);
2168
2169     if (!haveStatus) {
2170         struct VenusFid tfid;
2171
2172         tfid = *afid;
2173         tfid.Fid.Vnode = 0;     /* Means get rootfid of volume */
2174         origCBs = afs_allCBs;   /* ignore InitCallBackState */
2175         code =
2176             afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2177                              &serverp, &tsync);
2178     }
2179
2180     if (code) {
2181         ObtainWriteLock(&afs_xcbhash, 467);
2182         afs_DequeueCallback(tvc);
2183         tvc->callback = NULL;
2184         tvc->states &= ~(CStatd | CUnique);
2185         ReleaseWriteLock(&afs_xcbhash);
2186         if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2187             osi_dnlc_purgedp(tvc);      /* if it (could be) a directory */
2188         ReleaseWriteLock(&tvc->lock);
2189         ObtainReadLock(&afs_xvcache);
2190         AFS_FAST_RELE(tvc);
2191         ReleaseReadLock(&afs_xvcache);
2192         return NULL;
2193     }
2194
2195     ObtainWriteLock(&afs_xcbhash, 468);
2196     if (origCBs == afs_allCBs) {
2197         tvc->states |= CTruth;
2198         tvc->callback = serverp;
2199         if (CallBack.ExpirationTime != 0) {
2200             tvc->cbExpires = CallBack.ExpirationTime + start;
2201             tvc->states |= CStatd;
2202             tvc->states &= ~CBulkFetching;
2203             afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2204         } else if (tvc->states & CRO) {
2205             /* adapt gives us an hour. */
2206             tvc->cbExpires = 3600 + osi_Time();
2207              /*XXX*/ tvc->states |= CStatd;
2208             tvc->states &= ~CBulkFetching;
2209             afs_QueueCallback(tvc, CBHash(3600), tvolp);
2210         }
2211     } else {
2212         afs_DequeueCallback(tvc);
2213         tvc->callback = NULL;
2214         tvc->states &= ~(CStatd | CUnique);
2215         if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2216             osi_dnlc_purgedp(tvc);      /* if it (could be) a directory */
2217     }
2218     ReleaseWriteLock(&afs_xcbhash);
2219     afs_ProcessFS(tvc, &OutStatus, areq);
2220
2221     ReleaseWriteLock(&tvc->lock);
2222     return tvc;
2223 }
2224
2225
2226
2227 /*
2228  * must be called with avc write-locked
2229  * don't absolutely have to invalidate the hint unless the dv has
2230  * changed, but be sure to get it right else there will be consistency bugs.
2231  */
2232 afs_int32
2233 afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
2234                 struct vrequest * areq, struct AFSFetchStatus * Outsp)
2235 {
2236     int code;
2237     afs_uint32 start = 0;
2238     register struct conn *tc;
2239     struct AFSCallBack CallBack;
2240     struct AFSVolSync tsync;
2241     struct volume *volp;
2242     XSTATS_DECLS;
2243     do {
2244         tc = afs_Conn(afid, areq, SHARED_LOCK);
2245         avc->quick.stamp = 0;
2246         avc->h1.dchint = NULL;  /* invalidate hints */
2247         if (tc) {
2248             avc->callback = tc->srvr->server;
2249             start = osi_Time();
2250             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2251             RX_AFS_GUNLOCK();
2252             code =
2253                 RXAFS_FetchStatus(tc->id, (struct AFSFid *)&afid->Fid, Outsp,
2254                                   &CallBack, &tsync);
2255             RX_AFS_GLOCK();
2256
2257             XSTATS_END_TIME;
2258
2259         } else
2260             code = -1;
2261     } while (afs_Analyze
2262              (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2263               SHARED_LOCK, NULL));
2264
2265     if (!code) {
2266         afs_ProcessFS(avc, Outsp, areq);
2267         volp = afs_GetVolume(afid, areq, READ_LOCK);
2268         ObtainWriteLock(&afs_xcbhash, 469);
2269         avc->states |= CTruth;
2270         if (avc->callback /* check for race */ ) {
2271             if (CallBack.ExpirationTime != 0) {
2272                 avc->cbExpires = CallBack.ExpirationTime + start;
2273                 avc->states |= CStatd;
2274                 avc->states &= ~CBulkFetching;
2275                 afs_QueueCallback(avc, CBHash(CallBack.ExpirationTime), volp);
2276             } else if (avc->states & CRO) {     /* ordinary callback on a read-only volume -- AFS 3.2 style */
2277                 avc->cbExpires = 3600 + start;
2278                 avc->states |= CStatd;
2279                 avc->states &= ~CBulkFetching;
2280                 afs_QueueCallback(avc, CBHash(3600), volp);
2281             } else {
2282                 afs_DequeueCallback(avc);
2283                 avc->callback = NULL;
2284                 avc->states &= ~(CStatd | CUnique);
2285                 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2286                     osi_dnlc_purgedp(avc);      /* if it (could be) a directory */
2287             }
2288         } else {
2289             afs_DequeueCallback(avc);
2290             avc->callback = NULL;
2291             avc->states &= ~(CStatd | CUnique);
2292             if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2293                 osi_dnlc_purgedp(avc);  /* if it (could be) a directory */
2294         }
2295         ReleaseWriteLock(&afs_xcbhash);
2296         if (volp)
2297             afs_PutVolume(volp, READ_LOCK);
2298     } else {
2299         /* used to undo the local callback, but that's too extreme.
2300          * There are plenty of good reasons that fetchstatus might return
2301          * an error, such as EPERM.  If we have the vnode cached, statd,
2302          * with callback, might as well keep track of the fact that we
2303          * don't have access...
2304          */
2305         if (code == EPERM || code == EACCES) {
2306             struct axscache *ac;
2307             if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2308                 ac->axess = 0;
2309             else                /* not found, add a new one if possible */
2310                 afs_AddAxs(avc->Access, areq->uid, 0);
2311         }
2312     }
2313     return code;
2314 }
2315
2316 #if 0
2317 /*
2318  * afs_StuffVcache
2319  *
2320  * Description:
2321  *      Stuff some information into the vcache for the given file.
2322  *
2323  * Parameters:
2324  *      afid      : File in question.
2325  *      OutStatus : Fetch status on the file.
2326  *      CallBack  : Callback info.
2327  *      tc        : RPC connection involved.
2328  *      areq      : vrequest involved.
2329  *
2330  * Environment:
2331  *      Nothing interesting.
2332  */
2333 void
2334 afs_StuffVcache(register struct VenusFid *afid,
2335                 struct AFSFetchStatus *OutStatus,
2336                 struct AFSCallBack *CallBack, register struct conn *tc,
2337                 struct vrequest *areq)
2338 {
2339     register afs_int32 code, i, newvcache = 0;
2340     register struct vcache *tvc;
2341     struct AFSVolSync tsync;
2342     struct volume *tvp;
2343     struct axscache *ac;
2344     afs_int32 retry;
2345
2346     AFS_STATCNT(afs_StuffVcache);
2347 #ifdef IFS_VCACHECOUNT
2348     ifs_gvcachecall++;
2349 #endif
2350
2351   loop:
2352     ObtainSharedLock(&afs_xvcache, 8);
2353
2354     tvc = afs_FindVCache(afid, &retry, DO_VLRU /* no stats */ );
2355     if (tvc && retry) {
2356 #if     defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2357         ReleaseSharedLock(&afs_xvcache);
2358         spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2359         goto loop;
2360 #endif
2361     }
2362
2363     if (!tvc) {
2364         /* no cache entry, better grab one */
2365         UpgradeSToWLock(&afs_xvcache, 25);
2366         tvc = afs_NewVCache(afid, NULL);
2367         newvcache = 1;
2368         ConvertWToSLock(&afs_xvcache);
2369     }
2370
2371     ReleaseSharedLock(&afs_xvcache);
2372     ObtainWriteLock(&tvc->lock, 58);
2373
2374     tvc->states &= ~CStatd;
2375     if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2376         osi_dnlc_purgedp(tvc);  /* if it (could be) a directory */
2377
2378     /* Is it always appropriate to throw away all the access rights? */
2379     afs_FreeAllAxs(&(tvc->Access));
2380
2381     /*Copy useful per-volume info */
2382     tvp = afs_GetVolume(afid, areq, READ_LOCK);
2383     if (tvp) {
2384         if (newvcache && (tvp->states & VForeign))
2385             tvc->states |= CForeign;
2386         if (tvp->states & VRO)
2387             tvc->states |= CRO;
2388         if (tvp->states & VBackup)
2389             tvc->states |= CBackup;
2390         /*
2391          * Now, copy ".." entry back out of volume structure, if
2392          * necessary
2393          */
2394         if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2395             if (!tvc->mvid)
2396                 tvc->mvid = (struct VenusFid *)
2397                     osi_AllocSmallSpace(sizeof(struct VenusFid));
2398             *tvc->mvid = tvp->dotdot;
2399         }
2400     }
2401     /* store the stat on the file */
2402     afs_RemoveVCB(afid);
2403     afs_ProcessFS(tvc, OutStatus, areq);
2404     tvc->callback = tc->srvr->server;
2405
2406     /* we use osi_Time twice below.  Ideally, we would use the time at which
2407      * the FetchStatus call began, instead, but we don't have it here.  So we
2408      * make do with "now".  In the CRO case, it doesn't really matter. In
2409      * the other case, we hope that the difference between "now" and when the
2410      * call actually began execution on the server won't be larger than the
2411      * padding which the server keeps.  Subtract 1 second anyway, to be on
2412      * the safe side.  Can't subtract more because we don't know how big
2413      * ExpirationTime is.  Possible consistency problems may arise if the call
2414      * timeout period becomes longer than the server's expiration padding.  */
2415     ObtainWriteLock(&afs_xcbhash, 470);
2416     if (CallBack->ExpirationTime != 0) {
2417         tvc->cbExpires = CallBack->ExpirationTime + osi_Time() - 1;
2418         tvc->states |= CStatd;
2419         tvc->states &= ~CBulkFetching;
2420         afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2421     } else if (tvc->states & CRO) {
2422         /* old-fashioned AFS 3.2 style */
2423         tvc->cbExpires = 3600 + osi_Time();
2424          /*XXX*/ tvc->states |= CStatd;
2425         tvc->states &= ~CBulkFetching;
2426         afs_QueueCallback(tvc, CBHash(3600), tvp);
2427     } else {
2428         afs_DequeueCallback(tvc);
2429         tvc->callback = NULL;
2430         tvc->states &= ~(CStatd | CUnique);
2431         if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2432             osi_dnlc_purgedp(tvc);      /* if it (could be) a directory */
2433     }
2434     ReleaseWriteLock(&afs_xcbhash);
2435     if (tvp)
2436         afs_PutVolume(tvp, READ_LOCK);
2437
2438     /* look in per-pag cache */
2439     if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2440         ac->axess = OutStatus->CallerAccess;    /* substitute pags */
2441     else                        /* not found, add a new one if possible */
2442         afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2443
2444     ReleaseWriteLock(&tvc->lock);
2445     afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2446                ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32,
2447                tvc->cbExpires, ICL_TYPE_INT32, tvc->cbExpires - osi_Time());
2448     /*
2449      * Release ref count... hope this guy stays around...
2450      */
2451     afs_PutVCache(tvc);
2452 }                               /*afs_StuffVcache */
2453 #endif
2454
2455 /*
2456  * afs_PutVCache
2457  *
2458  * Description:
2459  *      Decrements the reference count on a cache entry.
2460  *
2461  * Parameters:
2462  *      avc : Pointer to the cache entry to decrement.
2463  *
2464  * Environment:
2465  *      Nothing interesting.
2466  */
2467 void
2468 afs_PutVCache(register struct vcache *avc)
2469 {
2470     AFS_STATCNT(afs_PutVCache);
2471     /*
2472      * Can we use a read lock here?
2473      */
2474     ObtainReadLock(&afs_xvcache);
2475     AFS_FAST_RELE(avc);
2476     ReleaseReadLock(&afs_xvcache);
2477 }                               /*afs_PutVCache */
2478
2479 /*
2480  * afs_FindVCache
2481  *
2482  * Description:
2483  *      Find a vcache entry given a fid.
2484  *
2485  * Parameters:
2486  *      afid : Pointer to the fid whose cache entry we desire.
2487  *      retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2488  *             unlock the vnode, and try again.
2489  *      flags: bit 1 to specify whether to compute hit statistics.  Not
2490  *             set if FindVCache is called as part of internal bookkeeping.
2491  *
2492  * Environment:
2493  *      Must be called with the afs_xvcache lock at least held at
2494  *      the read level.  In order to do the VLRU adjustment, the xvcache lock
2495  *      must be shared-- we upgrade it here.
2496  */
2497
2498 struct vcache *
2499 afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
2500 {
2501
2502     register struct vcache *tvc;
2503     afs_int32 i;
2504
2505     AFS_STATCNT(afs_FindVCache);
2506
2507     i = VCHash(afid);
2508     for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2509         if (FidMatches(afid, tvc)) {
2510 #ifdef  AFS_OSF_ENV
2511             /* Grab this vnode, possibly reactivating from the free list */
2512             int vg;
2513             AFS_GUNLOCK();
2514             vg = vget(AFSTOV(tvc));
2515             AFS_GLOCK();
2516             if (vg)
2517                 continue;
2518 #endif /* AFS_OSF_ENV */
2519             break;
2520         }
2521     }
2522
2523     /* should I have a read lock on the vnode here? */
2524     if (tvc) {
2525         if (retry)
2526             *retry = 0;
2527 #if !defined(AFS_OSF_ENV)
2528         osi_vnhold(tvc, retry); /* already held, above */
2529         if (retry && *retry)
2530             return 0;
2531 #endif
2532         /*
2533          * only move to front of vlru if we have proper vcache locking)
2534          */
2535         if (flag & DO_VLRU) {
2536             if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2537                 refpanic("FindVC VLRU inconsistent1");
2538             }
2539             if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2540                 refpanic("FindVC VLRU inconsistent1");
2541             }
2542             if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2543                 refpanic("FindVC VLRU inconsistent2");
2544             }
2545             UpgradeSToWLock(&afs_xvcache, 26);
2546             QRemove(&tvc->vlruq);
2547             QAdd(&VLRU, &tvc->vlruq);
2548             ConvertWToSLock(&afs_xvcache);
2549             if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2550                 refpanic("FindVC VLRU inconsistent1");
2551             }
2552             if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2553                 refpanic("FindVC VLRU inconsistent2");
2554             }
2555             if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2556                 refpanic("FindVC VLRU inconsistent3");
2557             }
2558         }
2559         vcachegen++;
2560     }
2561
2562     if (flag & DO_STATS) {
2563         if (tvc)
2564             afs_stats_cmperf.vcacheHits++;
2565         else
2566             afs_stats_cmperf.vcacheMisses++;
2567         if (afs_IsPrimaryCellNum(afid->Cell))
2568             afs_stats_cmperf.vlocalAccesses++;
2569         else
2570             afs_stats_cmperf.vremoteAccesses++;
2571     }
2572 #ifdef AFS_LINUX22_ENV
2573     if (tvc && (tvc->states & CStatd))
2574         vcache2inode(tvc);      /* mainly to reset i_nlink */
2575 #endif
2576 #ifdef AFS_DARWIN_ENV
2577     if (tvc)
2578         osi_VM_Setup(tvc, 0);
2579 #endif
2580     return tvc;
2581 }                               /*afs_FindVCache */
2582
2583 /*
2584  * afs_NFSFindVCache
2585  *
2586  * Description:
2587  *      Find a vcache entry given a fid. Does a wildcard match on what we
2588  *      have for the fid. If more than one entry, don't return anything.
2589  *
2590  * Parameters:
2591  *      avcp : Fill in pointer if we found one and only one.
2592  *      afid : Pointer to the fid whose cache entry we desire.
2593  *      retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2594  *             unlock the vnode, and try again.
2595  *      flags: bit 1 to specify whether to compute hit statistics.  Not
2596  *             set if FindVCache is called as part of internal bookkeeping.
2597  *
2598  * Environment:
2599  *      Must be called with the afs_xvcache lock at least held at
2600  *      the read level.  In order to do the VLRU adjustment, the xvcache lock
2601  *      must be shared-- we upgrade it here.
2602  *
2603  * Return value:
2604  *      number of matches found.
2605  */
2606
2607 int afs_duplicate_nfs_fids = 0;
2608
2609 afs_int32
2610 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2611 {
2612     register struct vcache *tvc;
2613     afs_int32 i;
2614     afs_int32 count = 0;
2615     struct vcache *found_tvc = NULL;
2616
2617     AFS_STATCNT(afs_FindVCache);
2618
2619 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2620   loop:
2621 #endif
2622
2623     ObtainSharedLock(&afs_xvcache, 331);
2624
2625     i = VCHash(afid);
2626     for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2627         /* Match only on what we have.... */
2628         if (((tvc->fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2629             && (tvc->fid.Fid.Volume == afid->Fid.Volume)
2630             && ((tvc->fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2631             && (tvc->fid.Cell == afid->Cell)) {
2632 #ifdef  AFS_OSF_ENV
2633             /* Grab this vnode, possibly reactivating from the free list */
2634             int vg;
2635             AFS_GUNLOCK();
2636             vg = vget(AFSTOV(tvc));
2637             AFS_GLOCK();
2638             if (vg) {
2639                 /* This vnode no longer exists. */
2640                 continue;
2641             }
2642 #endif /* AFS_OSF_ENV */
2643             count++;
2644             if (found_tvc) {
2645                 /* Duplicates */
2646 #ifdef AFS_OSF_ENV
2647                 /* Drop our reference counts. */
2648                 vrele(AFSTOV(tvc));
2649                 vrele(AFSTOV(found_tvc));
2650 #endif
2651                 afs_duplicate_nfs_fids++;
2652                 ReleaseSharedLock(&afs_xvcache);
2653                 return count;
2654             }
2655             found_tvc = tvc;
2656         }
2657     }
2658
2659     tvc = found_tvc;
2660     /* should I have a read lock on the vnode here? */
2661     if (tvc) {
2662 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2663         afs_int32 retry = 0;
2664         osi_vnhold(tvc, &retry);
2665         if (retry) {
2666             count = 0;
2667             found_tvc = (struct vcache *)0;
2668             ReleaseSharedLock(&afs_xvcache);
2669             spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2670             goto loop;
2671         }
2672 #else
2673 #if !defined(AFS_OSF_ENV)
2674         osi_vnhold(tvc, (int *)0);      /* already held, above */
2675 #endif
2676 #endif
2677         /*
2678          * We obtained the xvcache lock above.
2679          */
2680         if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2681             refpanic("FindVC VLRU inconsistent1");
2682         }
2683         if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2684             refpanic("FindVC VLRU inconsistent1");
2685         }
2686         if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2687             refpanic("FindVC VLRU inconsistent2");
2688         }
2689         UpgradeSToWLock(&afs_xvcache, 568);
2690         QRemove(&tvc->vlruq);
2691         QAdd(&VLRU, &tvc->vlruq);
2692         ConvertWToSLock(&afs_xvcache);
2693         if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2694             refpanic("FindVC VLRU inconsistent1");
2695         }
2696         if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2697             refpanic("FindVC VLRU inconsistent2");
2698         }
2699         if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2700             refpanic("FindVC VLRU inconsistent3");
2701         }
2702     }
2703     vcachegen++;
2704
2705     if (tvc)
2706         afs_stats_cmperf.vcacheHits++;
2707     else
2708         afs_stats_cmperf.vcacheMisses++;
2709     if (afs_IsPrimaryCellNum(afid->Cell))
2710         afs_stats_cmperf.vlocalAccesses++;
2711     else
2712         afs_stats_cmperf.vremoteAccesses++;
2713
2714     *avcp = tvc;                /* May be null */
2715
2716     ReleaseSharedLock(&afs_xvcache);
2717     return (tvc ? 1 : 0);
2718
2719 }                               /*afs_NFSFindVCache */
2720
2721
2722
2723
2724 /*
2725  * afs_vcacheInit
2726  *
2727  * Initialize vcache related variables
2728  */
2729 void
2730 afs_vcacheInit(int astatSize)
2731 {
2732     register struct vcache *tvp;
2733     int i;
2734 #if     defined(AFS_OSF_ENV)
2735     if (!afs_maxvcount) {
2736 #if     defined(AFS_OSF30_ENV)
2737         afs_maxvcount = max_vnodes / 2; /* limit ourselves to half the total */
2738 #else
2739         afs_maxvcount = nvnode / 2;     /* limit ourselves to half the total */
2740 #endif
2741         if (astatSize < afs_maxvcount) {
2742             afs_maxvcount = astatSize;
2743         }
2744     }
2745 #else /* AFS_OSF_ENV */
2746     freeVCList = NULL;
2747 #endif
2748
2749     RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2750     LOCK_INIT(&afs_xvcb, "afs_xvcb");
2751
2752 #if     !defined(AFS_OSF_ENV)
2753 #ifdef AFS_LINUX26_ENV
2754     printf("old style would have needed %d contiguous bytes\n", astatSize *
2755            sizeof(struct vcache));
2756     Initial_freeVCList = freeVCList = tvp = (struct vcache *)
2757         afs_osi_Alloc(sizeof(struct vcache));
2758     for (i = 0; i < astatSize; i++) {
2759         tvp->nextfree = (struct vcache *) afs_osi_Alloc(sizeof(struct vcache));
2760         tvp = tvp->nextfree;
2761     }
2762     tvp->nextfree = NULL;
2763 #else
2764     /* Allocate and thread the struct vcache entries */
2765     tvp = (struct vcache *)afs_osi_Alloc(astatSize * sizeof(struct vcache));
2766     memset((char *)tvp, 0, sizeof(struct vcache) * astatSize);
2767
2768     Initial_freeVCList = tvp;
2769     freeVCList = &(tvp[0]);
2770     for (i = 0; i < astatSize - 1; i++) {
2771         tvp[i].nextfree = &(tvp[i + 1]);
2772     }
2773     tvp[astatSize - 1].nextfree = NULL;
2774 #ifdef  KERNEL_HAVE_PIN
2775     pin((char *)tvp, astatSize * sizeof(struct vcache));        /* XXX */
2776 #endif
2777 #endif
2778 #endif
2779
2780 #if defined(AFS_SGI_ENV)
2781     for (i = 0; i < astatSize; i++) {
2782         char name[METER_NAMSZ];
2783         struct vcache *tvc = &tvp[i];
2784
2785         tvc->v.v_number = ++afsvnumbers;
2786         tvc->vc_rwlockid = OSI_NO_LOCKID;
2787         initnsema(&tvc->vc_rwlock, 1,
2788                   makesname(name, "vrw", tvc->v.v_number));
2789 #ifndef AFS_SGI53_ENV
2790         initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
2791 #endif
2792 #ifndef AFS_SGI62_ENV
2793         initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
2794 #endif /* AFS_SGI62_ENV */
2795     }
2796 #endif
2797
2798     QInit(&VLRU);
2799
2800
2801 }
2802
2803 /*
2804  * shutdown_vcache
2805  *
2806  */
2807 void
2808 shutdown_vcache(void)
2809 {
2810     int i;
2811     struct afs_cbr *tsp, *nsp;
2812     /*
2813      * XXX We may potentially miss some of the vcaches because if when there're no
2814      * free vcache entries and all the vcache entries are active ones then we allocate
2815      * an additional one - admittedly we almost never had that occur.
2816      */
2817
2818     {
2819         register struct afs_q *tq, *uq;
2820         register struct vcache *tvc;
2821         for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
2822             tvc = QTOV(tq);
2823             uq = QPrev(tq);
2824             if (tvc->mvid) {
2825                 osi_FreeSmallSpace(tvc->mvid);
2826                 tvc->mvid = (struct VenusFid *)0;
2827             }
2828 #ifdef  AFS_AIX_ENV
2829             aix_gnode_rele(AFSTOV(tvc));
2830 #endif
2831             if (tvc->linkData) {
2832                 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
2833                 tvc->linkData = 0;
2834             }
2835         }
2836         /*
2837          * Also free the remaining ones in the Cache
2838          */
2839         for (i = 0; i < VCSIZE; i++) {
2840             for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2841                 if (tvc->mvid) {
2842                     osi_FreeSmallSpace(tvc->mvid);
2843                     tvc->mvid = (struct VenusFid *)0;
2844                 }
2845 #ifdef  AFS_AIX_ENV
2846                 if (tvc->v.v_gnode)
2847                     afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
2848 #ifdef  AFS_AIX32_ENV
2849                 if (tvc->segid) {
2850                     AFS_GUNLOCK();
2851                     vms_delete(tvc->segid);
2852                     AFS_GLOCK();
2853                     tvc->segid = tvc->vmh = NULL;
2854                     if (VREFCOUNT(tvc))
2855                         osi_Panic("flushVcache: vm race");
2856                 }
2857                 if (tvc->credp) {
2858                     crfree(tvc->credp);
2859                     tvc->credp = NULL;
2860                 }
2861 #endif
2862 #endif
2863 #if     defined(AFS_SUN5_ENV)
2864                 if (tvc->credp) {
2865                     crfree(tvc->credp);
2866                     tvc->credp = NULL;
2867                 }
2868 #endif
2869                 if (tvc->linkData) {
2870                     afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
2871                     tvc->linkData = 0;
2872                 }
2873
2874                 afs_FreeAllAxs(&(tvc->Access));
2875             }
2876             afs_vhashT[i] = 0;
2877         }
2878     }
2879     /*
2880      * Free any leftover callback queue
2881      */
2882     for (tsp = afs_cbrSpace; tsp; tsp = nsp) {
2883         nsp = tsp->next;
2884         afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
2885     }
2886     afs_cbrSpace = 0;
2887
2888 #ifdef AFS_LINUX26_ENV
2889     {
2890         struct vcache *tvp = Initial_freeVCList;
2891         while (tvp) {
2892             struct vcache *next = tvp->nextfree;
2893             
2894             afs_osi_Free(tvp, sizeof(struct vcache));
2895             tvp = next;
2896         }
2897     }
2898 #else
2899 #ifdef  KERNEL_HAVE_PIN
2900     unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2901 #endif
2902 #if     !defined(AFS_OSF_ENV)
2903     afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2904 #endif
2905 #endif
2906
2907 #if     !defined(AFS_OSF_ENV)
2908     freeVCList = Initial_freeVCList = 0;
2909 #endif
2910     RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2911     LOCK_INIT(&afs_xvcb, "afs_xvcb");
2912     QInit(&VLRU);
2913
2914 }