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