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