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