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