DEVEL15-disconnected-20080523
[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             /* If we've got here and we're disconnected, then we can go
1903              * no further
1904              */
1905             if (AFS_IS_DISCONNECTED) {
1906                 code = ENETDOWN;
1907                 /*printf("Network is down in afs_GetCache");*/
1908             } else
1909                 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1910
1911             /* For the NFS translator's benefit, make sure
1912              * non-directory vnodes always have their parent FID set
1913              * correctly, even when created as a result of decoding an
1914              * NFS filehandle.  It would be nice to also do this for
1915              * directories, but we can't because the fileserver fills
1916              * in the FID of the directory itself instead of that of
1917              * its parent.
1918              */
1919             if (!code && OutStatus.FileType != Directory &&
1920                 !tvc->parentVnode) {
1921                 tvc->parentVnode  = OutStatus.ParentVnode;
1922                 tvc->parentUnique = OutStatus.ParentUnique;
1923             }
1924         }
1925     }
1926
1927     if (code) {
1928         ReleaseWriteLock(&tvc->lock);
1929
1930         afs_PutVCache(tvc);
1931         return NULL;
1932     }
1933
1934     ReleaseWriteLock(&tvc->lock);
1935     return tvc;
1936
1937 }                               /*afs_GetVCache */
1938
1939
1940
1941 struct vcache *
1942 afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1943                  afs_int32 * cached, struct vcache *adp, char *aname)
1944 {
1945     afs_int32 code, now, newvcache = 0;
1946     struct VenusFid nfid;
1947     register struct vcache *tvc;
1948     struct volume *tvp;
1949     struct AFSFetchStatus OutStatus;
1950     struct AFSCallBack CallBack;
1951     struct AFSVolSync tsync;
1952     struct server *serverp = 0;
1953     afs_int32 origCBs;
1954     afs_int32 retry;
1955
1956     AFS_STATCNT(afs_GetVCache);
1957     if (cached)
1958         *cached = 0;            /* Init just in case */
1959
1960 #if     defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1961   loop1:
1962 #endif
1963
1964     ObtainReadLock(&afs_xvcache);
1965     tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */ );
1966
1967     if (tvc) {
1968         ReleaseReadLock(&afs_xvcache);
1969         if (retry) {
1970 #if     defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1971             spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1972             goto loop1;
1973 #endif
1974         }
1975         ObtainReadLock(&tvc->lock);
1976
1977         if (tvc->states & CStatd) {
1978             if (cached) {
1979                 *cached = 1;
1980             }
1981             ReleaseReadLock(&tvc->lock);
1982             return tvc;
1983         }
1984         tvc->states &= ~CUnique;
1985
1986         ReleaseReadLock(&tvc->lock);
1987         afs_PutVCache(tvc);
1988         ObtainReadLock(&afs_xvcache);
1989     }
1990     /* if (tvc) */
1991     ReleaseReadLock(&afs_xvcache);
1992
1993     /* lookup the file */
1994     nfid = *afid;
1995     now = osi_Time();
1996     origCBs = afs_allCBs;       /* if anything changes, we don't have a cb */
1997     
1998     if (AFS_IS_DISCONNECTED) {
1999         /*printf("Network is down in afs_LookupVcache\n");*/
2000         code = ENETDOWN;
2001     } else 
2002         code =
2003             afs_RemoteLookup(&adp->fid, areq, aname, &nfid, &OutStatus, 
2004                              &CallBack, &serverp, &tsync);
2005
2006 #if     defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2007   loop2:
2008 #endif
2009
2010     ObtainSharedLock(&afs_xvcache, 6);
2011     tvc = afs_FindVCache(&nfid, &retry, DO_VLRU | IS_SLOCK/* no xstats now */ );
2012     if (tvc && retry) {
2013 #if     defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2014         ReleaseSharedLock(&afs_xvcache);
2015         spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2016         goto loop2;
2017 #endif
2018     }
2019
2020     if (!tvc) {
2021         /* no cache entry, better grab one */
2022         UpgradeSToWLock(&afs_xvcache, 22);
2023         tvc = afs_NewVCache(&nfid, serverp);
2024         newvcache = 1;
2025         ConvertWToSLock(&afs_xvcache);
2026         if (!tvc)
2027         {
2028                 ReleaseSharedLock(&afs_xvcache);
2029                 return NULL;
2030         }
2031     }
2032
2033     ReleaseSharedLock(&afs_xvcache);
2034     ObtainWriteLock(&tvc->lock, 55);
2035
2036     /* It is always appropriate to throw away all the access rights? */
2037     afs_FreeAllAxs(&(tvc->Access));
2038     tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
2039     if (tvp) {
2040         if ((tvp->states & VForeign)) {
2041             if (newvcache)
2042                 tvc->states |= CForeign;
2043             if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
2044                 && (tvp->rootUnique == afid->Fid.Unique))
2045                 tvc->mvstat = 2;
2046         }
2047         if (tvp->states & VRO)
2048             tvc->states |= CRO;
2049         if (tvp->states & VBackup)
2050             tvc->states |= CBackup;
2051         /* now copy ".." entry back out of volume structure, if necessary */
2052         if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2053             if (!tvc->mvid)
2054                 tvc->mvid = (struct VenusFid *)
2055                     osi_AllocSmallSpace(sizeof(struct VenusFid));
2056             *tvc->mvid = tvp->dotdot;
2057         }
2058     }
2059
2060     if (code) {
2061         ObtainWriteLock(&afs_xcbhash, 465);
2062         afs_DequeueCallback(tvc);
2063         tvc->states &= ~(CStatd | CUnique);
2064         ReleaseWriteLock(&afs_xcbhash);
2065         if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2066             osi_dnlc_purgedp(tvc);      /* if it (could be) a directory */
2067         if (tvp)
2068             afs_PutVolume(tvp, READ_LOCK);
2069         ReleaseWriteLock(&tvc->lock);
2070         afs_PutVCache(tvc);
2071         return NULL;
2072     }
2073
2074     ObtainWriteLock(&afs_xcbhash, 466);
2075     if (origCBs == afs_allCBs) {
2076         if (CallBack.ExpirationTime) {
2077             tvc->callback = serverp;
2078             tvc->cbExpires = CallBack.ExpirationTime + now;
2079             tvc->states |= CStatd | CUnique;
2080             tvc->states &= ~CBulkFetching;
2081             afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
2082         } else if (tvc->states & CRO) {
2083             /* adapt gives us an hour. */
2084             tvc->cbExpires = 3600 + osi_Time();
2085              /*XXX*/ tvc->states |= CStatd | CUnique;
2086             tvc->states &= ~CBulkFetching;
2087             afs_QueueCallback(tvc, CBHash(3600), tvp);
2088         } else {
2089             tvc->callback = NULL;
2090             afs_DequeueCallback(tvc);
2091             tvc->states &= ~(CStatd | CUnique);
2092             if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2093                 osi_dnlc_purgedp(tvc);  /* if it (could be) a directory */
2094         }
2095     } else {
2096         afs_DequeueCallback(tvc);
2097         tvc->states &= ~CStatd;
2098         tvc->states &= ~CUnique;
2099         tvc->callback = NULL;
2100         if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2101             osi_dnlc_purgedp(tvc);      /* if it (could be) a directory */
2102     }
2103     ReleaseWriteLock(&afs_xcbhash);
2104     if (tvp)
2105         afs_PutVolume(tvp, READ_LOCK);
2106     afs_ProcessFS(tvc, &OutStatus, areq);
2107
2108     ReleaseWriteLock(&tvc->lock);
2109     return tvc;
2110
2111 }
2112
2113 struct vcache *
2114 afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
2115                   afs_int32 * cached, struct volume *tvolp)
2116 {
2117     afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
2118     afs_int32 getNewFid = 0;
2119     afs_uint32 start;
2120     struct VenusFid nfid;
2121     register struct vcache *tvc;
2122     struct server *serverp = 0;
2123     struct AFSFetchStatus OutStatus;
2124     struct AFSCallBack CallBack;
2125     struct AFSVolSync tsync;
2126     int origCBs = 0;
2127 #ifdef  AFS_OSF_ENV
2128     int vg;
2129 #endif
2130 #ifdef AFS_DARWIN80_ENV
2131     vnode_t tvp;
2132 #endif
2133
2134     start = osi_Time();
2135
2136   newmtpt:
2137     if (!tvolp->rootVnode || getNewFid) {
2138         struct VenusFid tfid;
2139
2140         tfid = *afid;
2141         tfid.Fid.Vnode = 0;     /* Means get rootfid of volume */
2142         origCBs = afs_allCBs;   /* ignore InitCallBackState */
2143         code =
2144             afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2145                              &serverp, &tsync);
2146         if (code) {
2147             return NULL;
2148         }
2149 /*      ReleaseReadLock(&tvolp->lock);           */
2150         ObtainWriteLock(&tvolp->lock, 56);
2151         tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
2152         tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
2153         ReleaseWriteLock(&tvolp->lock);
2154 /*      ObtainReadLock(&tvolp->lock);*/
2155         haveStatus = 1;
2156     } else {
2157         afid->Fid.Vnode = tvolp->rootVnode;
2158         afid->Fid.Unique = tvolp->rootUnique;
2159     }
2160
2161  rootvc_loop:
2162     ObtainSharedLock(&afs_xvcache, 7);
2163     i = VCHash(afid);
2164     for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2165         if (!FidCmp(&(tvc->fid), afid)) {
2166             if (tvc->states & CVInit) {
2167                 ReleaseSharedLock(&afs_xvcache);
2168                 afs_osi_Sleep(&tvc->states);
2169                 goto rootvc_loop;
2170             }
2171 #ifdef  AFS_OSF_ENV
2172             /* Grab this vnode, possibly reactivating from the free list */
2173             /* for the present (95.05.25) everything on the hash table is
2174              * definitively NOT in the free list -- at least until afs_reclaim
2175              * can be safely implemented */
2176             AFS_GUNLOCK();
2177             vg = vget(AFSTOV(tvc));     /* this bumps ref count */
2178             AFS_GLOCK();
2179             if (vg)
2180                 continue;
2181 #endif /* AFS_OSF_ENV */
2182 #ifdef AFS_DARWIN80_ENV
2183             if (tvc->states & CDeadVnode) {
2184                 ReleaseSharedLock(&afs_xvcache);
2185                 afs_osi_Sleep(&tvc->states);
2186                 goto rootvc_loop;
2187             }
2188             tvp = AFSTOV(tvc);
2189             if (vnode_get(tvp))       /* this bumps ref count */
2190                 continue;
2191             if (vnode_ref(tvp)) {
2192                 AFS_GUNLOCK();
2193                 /* AFSTOV(tvc) may be NULL */
2194                 vnode_put(tvp);
2195                 AFS_GLOCK();
2196                 continue;
2197             }
2198 #endif
2199             break;
2200         }
2201     }
2202
2203     if (!haveStatus && (!tvc || !(tvc->states & CStatd))) {
2204         /* Mount point no longer stat'd or unknown. FID may have changed. */
2205 #ifdef AFS_OSF_ENV
2206         if (tvc)
2207             AFS_RELE(AFSTOV(tvc));
2208 #endif
2209         getNewFid = 1;
2210         ReleaseSharedLock(&afs_xvcache);
2211 #ifdef AFS_DARWIN80_ENV
2212         if (tvc) {
2213             AFS_GUNLOCK();
2214             vnode_put(AFSTOV(tvc));
2215             vnode_rele(AFSTOV(tvc));
2216             AFS_GLOCK();
2217         }
2218 #endif
2219         tvc = NULL;
2220         goto newmtpt;
2221     }
2222
2223     if (!tvc) {
2224         UpgradeSToWLock(&afs_xvcache, 23);
2225         /* no cache entry, better grab one */
2226         tvc = afs_NewVCache(afid, NULL);
2227         if (!tvc)
2228         {
2229                 ReleaseWriteLock(&afs_xvcache);
2230                 return NULL;
2231         }
2232         newvcache = 1;
2233         afs_stats_cmperf.vcacheMisses++;
2234     } else {
2235         if (cached)
2236             *cached = 1;
2237         afs_stats_cmperf.vcacheHits++;
2238 #if     defined(AFS_OSF_ENV) || defined(AFS_DARWIN80_ENV)
2239         /* we already bumped the ref count in the for loop above */
2240 #else /* AFS_OSF_ENV */
2241         osi_vnhold(tvc, 0);
2242 #endif
2243         UpgradeSToWLock(&afs_xvcache, 24);
2244         if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2245             refpanic("GRVC VLRU inconsistent0");
2246         }
2247         if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2248             refpanic("GRVC VLRU inconsistent1");
2249         }
2250         if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2251             refpanic("GRVC VLRU inconsistent2");
2252         }
2253         QRemove(&tvc->vlruq);   /* move to lruq head */
2254         QAdd(&VLRU, &tvc->vlruq);
2255         if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2256             refpanic("GRVC VLRU inconsistent3");
2257         }
2258         if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2259             refpanic("GRVC VLRU inconsistent4");
2260         }
2261         if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2262             refpanic("GRVC VLRU inconsistent5");
2263         }
2264         vcachegen++;
2265     }
2266
2267     ReleaseWriteLock(&afs_xvcache);
2268
2269     if (tvc->states & CStatd) {
2270         return tvc;
2271     } else {
2272
2273         ObtainReadLock(&tvc->lock);
2274         tvc->states &= ~CUnique;
2275         tvc->callback = NULL;   /* redundant, perhaps */
2276         ReleaseReadLock(&tvc->lock);
2277     }
2278
2279     ObtainWriteLock(&tvc->lock, 57);
2280
2281     /* It is always appropriate to throw away all the access rights? */
2282     afs_FreeAllAxs(&(tvc->Access));
2283
2284     if (newvcache)
2285         tvc->states |= CForeign;
2286     if (tvolp->states & VRO)
2287         tvc->states |= CRO;
2288     if (tvolp->states & VBackup)
2289         tvc->states |= CBackup;
2290     /* now copy ".." entry back out of volume structure, if necessary */
2291     if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2292         && (tvolp->rootUnique == afid->Fid.Unique)) {
2293         tvc->mvstat = 2;
2294     }
2295     if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2296         if (!tvc->mvid)
2297             tvc->mvid = (struct VenusFid *)
2298                 osi_AllocSmallSpace(sizeof(struct VenusFid));
2299         *tvc->mvid = tvolp->dotdot;
2300     }
2301
2302     /* stat the file */
2303     afs_RemoveVCB(afid);
2304
2305     if (!haveStatus) {
2306         struct VenusFid tfid;
2307
2308         tfid = *afid;
2309         tfid.Fid.Vnode = 0;     /* Means get rootfid of volume */
2310         origCBs = afs_allCBs;   /* ignore InitCallBackState */
2311         code =
2312             afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2313                              &serverp, &tsync);
2314     }
2315
2316     if (code) {
2317         ObtainWriteLock(&afs_xcbhash, 467);
2318         afs_DequeueCallback(tvc);
2319         tvc->callback = NULL;
2320         tvc->states &= ~(CStatd | CUnique);
2321         ReleaseWriteLock(&afs_xcbhash);
2322         if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2323             osi_dnlc_purgedp(tvc);      /* if it (could be) a directory */
2324         ReleaseWriteLock(&tvc->lock);
2325         afs_PutVCache(tvc);
2326         return NULL;
2327     }
2328
2329     ObtainWriteLock(&afs_xcbhash, 468);
2330     if (origCBs == afs_allCBs) {
2331         tvc->states |= CTruth;
2332         tvc->callback = serverp;
2333         if (CallBack.ExpirationTime != 0) {
2334             tvc->cbExpires = CallBack.ExpirationTime + start;
2335             tvc->states |= CStatd;
2336             tvc->states &= ~CBulkFetching;
2337             afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2338         } else if (tvc->states & CRO) {
2339             /* adapt gives us an hour. */
2340             tvc->cbExpires = 3600 + osi_Time();
2341              /*XXX*/ tvc->states |= CStatd;
2342             tvc->states &= ~CBulkFetching;
2343             afs_QueueCallback(tvc, CBHash(3600), tvolp);
2344         }
2345     } else {
2346         afs_DequeueCallback(tvc);
2347         tvc->callback = NULL;
2348         tvc->states &= ~(CStatd | CUnique);
2349         if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2350             osi_dnlc_purgedp(tvc);      /* if it (could be) a directory */
2351     }
2352     ReleaseWriteLock(&afs_xcbhash);
2353     afs_ProcessFS(tvc, &OutStatus, areq);
2354
2355     ReleaseWriteLock(&tvc->lock);
2356     return tvc;
2357 }
2358
2359
2360
2361 /*
2362  * must be called with avc write-locked
2363  * don't absolutely have to invalidate the hint unless the dv has
2364  * changed, but be sure to get it right else there will be consistency bugs.
2365  */
2366 afs_int32
2367 afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
2368                 struct vrequest * areq, struct AFSFetchStatus * Outsp)
2369 {
2370     int code;
2371     afs_uint32 start = 0;
2372     register struct conn *tc;
2373     struct AFSCallBack CallBack;
2374     struct AFSVolSync tsync;
2375     struct volume *volp;
2376     XSTATS_DECLS;
2377     do {
2378         tc = afs_Conn(afid, areq, SHARED_LOCK);
2379         avc->dchint = NULL;     /* invalidate hints */
2380         if (tc) {
2381             avc->callback = tc->srvr->server;
2382             start = osi_Time();
2383             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2384             RX_AFS_GUNLOCK();
2385             code =
2386                 RXAFS_FetchStatus(tc->id, (struct AFSFid *)&afid->Fid, Outsp,
2387                                   &CallBack, &tsync);
2388             RX_AFS_GLOCK();
2389
2390             XSTATS_END_TIME;
2391
2392         } else
2393             code = -1;
2394     } while (afs_Analyze
2395              (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2396               SHARED_LOCK, NULL));
2397
2398     if (!code) {
2399         afs_ProcessFS(avc, Outsp, areq);
2400         volp = afs_GetVolume(afid, areq, READ_LOCK);
2401         ObtainWriteLock(&afs_xcbhash, 469);
2402         avc->states |= CTruth;
2403         if (avc->callback /* check for race */ ) {
2404             if (CallBack.ExpirationTime != 0) {
2405                 avc->cbExpires = CallBack.ExpirationTime + start;
2406                 avc->states |= CStatd;
2407                 avc->states &= ~CBulkFetching;
2408                 afs_QueueCallback(avc, CBHash(CallBack.ExpirationTime), volp);
2409             } else if (avc->states & CRO) {     /* ordinary callback on a read-only volume -- AFS 3.2 style */
2410                 avc->cbExpires = 3600 + start;
2411                 avc->states |= CStatd;
2412                 avc->states &= ~CBulkFetching;
2413                 afs_QueueCallback(avc, CBHash(3600), volp);
2414             } else {
2415                 afs_DequeueCallback(avc);
2416                 avc->callback = NULL;
2417                 avc->states &= ~(CStatd | CUnique);
2418                 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2419                     osi_dnlc_purgedp(avc);      /* if it (could be) a directory */
2420             }
2421         } else {
2422             afs_DequeueCallback(avc);
2423             avc->callback = NULL;
2424             avc->states &= ~(CStatd | CUnique);
2425             if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2426                 osi_dnlc_purgedp(avc);  /* if it (could be) a directory */
2427         }
2428         ReleaseWriteLock(&afs_xcbhash);
2429         if (volp)
2430             afs_PutVolume(volp, READ_LOCK);
2431     } else {
2432         /* used to undo the local callback, but that's too extreme.
2433          * There are plenty of good reasons that fetchstatus might return
2434          * an error, such as EPERM.  If we have the vnode cached, statd,
2435          * with callback, might as well keep track of the fact that we
2436          * don't have access...
2437          */
2438         if (code == EPERM || code == EACCES) {
2439             struct axscache *ac;
2440             if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2441                 ac->axess = 0;
2442             else                /* not found, add a new one if possible */
2443                 afs_AddAxs(avc->Access, areq->uid, 0);
2444         }
2445     }
2446     return code;
2447 }
2448
2449 #if 0
2450 /*
2451  * afs_StuffVcache
2452  *
2453  * Description:
2454  *      Stuff some information into the vcache for the given file.
2455  *
2456  * Parameters:
2457  *      afid      : File in question.
2458  *      OutStatus : Fetch status on the file.
2459  *      CallBack  : Callback info.
2460  *      tc        : RPC connection involved.
2461  *      areq      : vrequest involved.
2462  *
2463  * Environment:
2464  *      Nothing interesting.
2465  */
2466 void
2467 afs_StuffVcache(register struct VenusFid *afid,
2468                 struct AFSFetchStatus *OutStatus,
2469                 struct AFSCallBack *CallBack, register struct conn *tc,
2470                 struct vrequest *areq)
2471 {
2472     register afs_int32 code, i, newvcache = 0;
2473     register struct vcache *tvc;
2474     struct AFSVolSync tsync;
2475     struct volume *tvp;
2476     struct axscache *ac;
2477     afs_int32 retry;
2478
2479     AFS_STATCNT(afs_StuffVcache);
2480 #ifdef IFS_VCACHECOUNT
2481     ifs_gvcachecall++;
2482 #endif
2483
2484   loop:
2485     ObtainSharedLock(&afs_xvcache, 8);
2486
2487     tvc = afs_FindVCache(afid, &retry, DO_VLRU| IS_SLOCK /* no stats */ );
2488     if (tvc && retry) {
2489 #if     defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2490         ReleaseSharedLock(&afs_xvcache);
2491         spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2492         goto loop;
2493 #endif
2494     }
2495
2496     if (!tvc) {
2497         /* no cache entry, better grab one */
2498         UpgradeSToWLock(&afs_xvcache, 25);
2499         tvc = afs_NewVCache(afid, NULL);
2500         newvcache = 1;
2501         ConvertWToSLock(&afs_xvcache);
2502         if (!tvc)
2503         {
2504                 ReleaseSharedLock(&afs_xvcache);
2505                 return NULL;
2506         }
2507     }
2508
2509     ReleaseSharedLock(&afs_xvcache);
2510     ObtainWriteLock(&tvc->lock, 58);
2511
2512     tvc->states &= ~CStatd;
2513     if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2514         osi_dnlc_purgedp(tvc);  /* if it (could be) a directory */
2515
2516     /* Is it always appropriate to throw away all the access rights? */
2517     afs_FreeAllAxs(&(tvc->Access));
2518
2519     /*Copy useful per-volume info */
2520     tvp = afs_GetVolume(afid, areq, READ_LOCK);
2521     if (tvp) {
2522         if (newvcache && (tvp->states & VForeign))
2523             tvc->states |= CForeign;
2524         if (tvp->states & VRO)
2525             tvc->states |= CRO;
2526         if (tvp->states & VBackup)
2527             tvc->states |= CBackup;
2528         /*
2529          * Now, copy ".." entry back out of volume structure, if
2530          * necessary
2531          */
2532         if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2533             if (!tvc->mvid)
2534                 tvc->mvid = (struct VenusFid *)
2535                     osi_AllocSmallSpace(sizeof(struct VenusFid));
2536             *tvc->mvid = tvp->dotdot;
2537         }
2538     }
2539     /* store the stat on the file */
2540     afs_RemoveVCB(afid);
2541     afs_ProcessFS(tvc, OutStatus, areq);
2542     tvc->callback = tc->srvr->server;
2543
2544     /* we use osi_Time twice below.  Ideally, we would use the time at which
2545      * the FetchStatus call began, instead, but we don't have it here.  So we
2546      * make do with "now".  In the CRO case, it doesn't really matter. In
2547      * the other case, we hope that the difference between "now" and when the
2548      * call actually began execution on the server won't be larger than the
2549      * padding which the server keeps.  Subtract 1 second anyway, to be on
2550      * the safe side.  Can't subtract more because we don't know how big
2551      * ExpirationTime is.  Possible consistency problems may arise if the call
2552      * timeout period becomes longer than the server's expiration padding.  */
2553     ObtainWriteLock(&afs_xcbhash, 470);
2554     if (CallBack->ExpirationTime != 0) {
2555         tvc->cbExpires = CallBack->ExpirationTime + osi_Time() - 1;
2556         tvc->states |= CStatd;
2557         tvc->states &= ~CBulkFetching;
2558         afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2559     } else if (tvc->states & CRO) {
2560         /* old-fashioned AFS 3.2 style */
2561         tvc->cbExpires = 3600 + osi_Time();
2562          /*XXX*/ tvc->states |= CStatd;
2563         tvc->states &= ~CBulkFetching;
2564         afs_QueueCallback(tvc, CBHash(3600), tvp);
2565     } else {
2566         afs_DequeueCallback(tvc);
2567         tvc->callback = NULL;
2568         tvc->states &= ~(CStatd | CUnique);
2569         if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2570             osi_dnlc_purgedp(tvc);      /* if it (could be) a directory */
2571     }
2572     ReleaseWriteLock(&afs_xcbhash);
2573     if (tvp)
2574         afs_PutVolume(tvp, READ_LOCK);
2575
2576     /* look in per-pag cache */
2577     if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2578         ac->axess = OutStatus->CallerAccess;    /* substitute pags */
2579     else                        /* not found, add a new one if possible */
2580         afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2581
2582     ReleaseWriteLock(&tvc->lock);
2583     afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2584                ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32,
2585                tvc->cbExpires, ICL_TYPE_INT32, tvc->cbExpires - osi_Time());
2586     /*
2587      * Release ref count... hope this guy stays around...
2588      */
2589     afs_PutVCache(tvc);
2590 }                               /*afs_StuffVcache */
2591 #endif
2592
2593 /*
2594  * afs_PutVCache
2595  *
2596  * Description:
2597  *      Decrements the reference count on a cache entry.
2598  *
2599  * Parameters:
2600  *      avc : Pointer to the cache entry to decrement.
2601  *
2602  * Environment:
2603  *      Nothing interesting.
2604  */
2605 void
2606 afs_PutVCache(register struct vcache *avc)
2607 {
2608     AFS_STATCNT(afs_PutVCache);
2609 #ifdef AFS_DARWIN80_ENV
2610     vnode_put(AFSTOV(avc));
2611     AFS_FAST_RELE(avc);
2612 #else
2613     /*
2614      * Can we use a read lock here?
2615      */
2616     ObtainReadLock(&afs_xvcache);
2617     AFS_FAST_RELE(avc);
2618     ReleaseReadLock(&afs_xvcache);
2619 #endif
2620 }                               /*afs_PutVCache */
2621
2622
2623 static void findvc_sleep(struct vcache *avc, int flag) {
2624     if (flag & IS_SLOCK) {
2625             ReleaseSharedLock(&afs_xvcache);
2626     } else {
2627         if (flag & IS_WLOCK) {
2628             ReleaseWriteLock(&afs_xvcache);
2629         } else {
2630             ReleaseReadLock(&afs_xvcache);
2631         }
2632     }
2633     afs_osi_Sleep(&avc->states);
2634     if (flag & IS_SLOCK) {
2635             ObtainSharedLock(&afs_xvcache, 341);
2636     } else {
2637         if (flag & IS_WLOCK) {
2638             ObtainWriteLock(&afs_xvcache, 343);
2639         } else {
2640             ObtainReadLock(&afs_xvcache);
2641         }
2642     }
2643 }
2644 /*
2645  * afs_FindVCache
2646  *
2647  * Description:
2648  *      Find a vcache entry given a fid.
2649  *
2650  * Parameters:
2651  *      afid : Pointer to the fid whose cache entry we desire.
2652  *      retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2653  *             unlock the vnode, and try again.
2654  *      flags: bit 1 to specify whether to compute hit statistics.  Not
2655  *             set if FindVCache is called as part of internal bookkeeping.
2656  *
2657  * Environment:
2658  *      Must be called with the afs_xvcache lock at least held at
2659  *      the read level.  In order to do the VLRU adjustment, the xvcache lock
2660  *      must be shared-- we upgrade it here.
2661  */
2662
2663 struct vcache *
2664 afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
2665 {
2666
2667     register struct vcache *tvc;
2668     afs_int32 i;
2669 #if defined( AFS_OSF_ENV)
2670     int vg;
2671 #endif
2672 #ifdef AFS_DARWIN80_ENV
2673     vnode_t tvp;
2674 #endif
2675
2676     AFS_STATCNT(afs_FindVCache);
2677
2678  findloop:
2679     i = VCHash(afid);
2680     for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2681         if (FidMatches(afid, tvc)) {
2682             if (tvc->states & CVInit) {
2683                 findvc_sleep(tvc, flag);
2684                 goto findloop;
2685             }
2686 #ifdef  AFS_OSF_ENV
2687             /* Grab this vnode, possibly reactivating from the free list */
2688             AFS_GUNLOCK();
2689             vg = vget(AFSTOV(tvc));
2690             AFS_GLOCK();
2691             if (vg)
2692                 continue;
2693 #endif /* AFS_OSF_ENV */
2694 #ifdef  AFS_DARWIN80_ENV
2695             if (tvc->states & CDeadVnode) {
2696                 findvc_sleep(tvc, flag);
2697                 goto findloop;
2698             }
2699             tvp = AFSTOV(tvc);
2700             if (vnode_get(tvp))
2701                 continue;
2702             if (vnode_ref(tvp)) {
2703                 AFS_GUNLOCK();
2704                 /* AFSTOV(tvc) may be NULL */
2705                 vnode_put(tvp);
2706                 AFS_GLOCK();
2707                 continue;
2708             }
2709 #endif
2710             break;
2711         }
2712     }
2713
2714     /* should I have a read lock on the vnode here? */
2715     if (tvc) {
2716         if (retry)
2717             *retry = 0;
2718 #if !defined(AFS_OSF_ENV) && !defined(AFS_DARWIN80_ENV)
2719         osi_vnhold(tvc, retry); /* already held, above */
2720         if (retry && *retry)
2721             return 0;
2722 #endif
2723 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
2724         tvc->states |= CUBCinit;
2725         AFS_GUNLOCK();
2726         if (UBCINFOMISSING(AFSTOV(tvc)) ||
2727             UBCINFORECLAIMED(AFSTOV(tvc))) {
2728           ubc_info_init(AFSTOV(tvc));
2729         }
2730         AFS_GLOCK();
2731         tvc->states &= ~CUBCinit;
2732 #endif
2733         /*
2734          * only move to front of vlru if we have proper vcache locking)
2735          */
2736         if (flag & DO_VLRU) {
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 inconsistent1");
2742             }
2743             if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2744                 refpanic("FindVC VLRU inconsistent2");
2745             }
2746             UpgradeSToWLock(&afs_xvcache, 26);
2747             QRemove(&tvc->vlruq);
2748             QAdd(&VLRU, &tvc->vlruq);
2749             ConvertWToSLock(&afs_xvcache);
2750             if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2751                 refpanic("FindVC VLRU inconsistent1");
2752             }
2753             if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2754                 refpanic("FindVC VLRU inconsistent2");
2755             }
2756             if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2757                 refpanic("FindVC VLRU inconsistent3");
2758             }
2759         }
2760         vcachegen++;
2761     }
2762
2763     if (flag & DO_STATS) {
2764         if (tvc)
2765             afs_stats_cmperf.vcacheHits++;
2766         else
2767             afs_stats_cmperf.vcacheMisses++;
2768         if (afs_IsPrimaryCellNum(afid->Cell))
2769             afs_stats_cmperf.vlocalAccesses++;
2770         else
2771             afs_stats_cmperf.vremoteAccesses++;
2772     }
2773     return tvc;
2774 }                               /*afs_FindVCache */
2775
2776 /*
2777  * afs_NFSFindVCache
2778  *
2779  * Description:
2780  *      Find a vcache entry given a fid. Does a wildcard match on what we
2781  *      have for the fid. If more than one entry, don't return anything.
2782  *
2783  * Parameters:
2784  *      avcp : Fill in pointer if we found one and only one.
2785  *      afid : Pointer to the fid whose cache entry we desire.
2786  *      retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2787  *             unlock the vnode, and try again.
2788  *      flags: bit 1 to specify whether to compute hit statistics.  Not
2789  *             set if FindVCache is called as part of internal bookkeeping.
2790  *
2791  * Environment:
2792  *      Must be called with the afs_xvcache lock at least held at
2793  *      the read level.  In order to do the VLRU adjustment, the xvcache lock
2794  *      must be shared-- we upgrade it here.
2795  *
2796  * Return value:
2797  *      number of matches found.
2798  */
2799
2800 int afs_duplicate_nfs_fids = 0;
2801
2802 afs_int32
2803 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2804 {
2805     register struct vcache *tvc;
2806     afs_int32 i;
2807     afs_int32 count = 0;
2808     struct vcache *found_tvc = NULL;
2809 #ifdef  AFS_OSF_ENV
2810     int vg;
2811 #endif
2812 #ifdef AFS_DARWIN80_ENV
2813     vnode_t tvp;
2814 #endif
2815
2816     AFS_STATCNT(afs_FindVCache);
2817
2818   loop:
2819
2820     ObtainSharedLock(&afs_xvcache, 331);
2821
2822     i = VCHash(afid);
2823     for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2824         /* Match only on what we have.... */
2825         if (((tvc->fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2826             && (tvc->fid.Fid.Volume == afid->Fid.Volume)
2827             && ((tvc->fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2828             && (tvc->fid.Cell == afid->Cell)) {
2829             if (tvc->states & CVInit) {
2830                 ReleaseSharedLock(&afs_xvcache);
2831                 afs_osi_Sleep(&tvc->states);
2832                 goto loop;
2833             }
2834 #ifdef  AFS_OSF_ENV
2835             /* Grab this vnode, possibly reactivating from the free list */
2836             AFS_GUNLOCK();
2837             vg = vget(AFSTOV(tvc));
2838             AFS_GLOCK();
2839             if (vg) {
2840                 /* This vnode no longer exists. */
2841                 continue;
2842             }
2843 #endif /* AFS_OSF_ENV */
2844 #ifdef  AFS_DARWIN80_ENV
2845             if (tvc->states & CDeadVnode) {
2846                 ReleaseSharedLock(&afs_xvcache);
2847                 afs_osi_Sleep(&tvc->states);
2848                 goto loop;
2849             }
2850             tvp = AFSTOV(tvc);
2851             if (vnode_get(tvp)) {
2852                 /* This vnode no longer exists. */
2853                 continue;
2854             }
2855             if (vnode_ref(tvp)) {
2856                 /* This vnode no longer exists. */
2857                 AFS_GUNLOCK();
2858                 /* AFSTOV(tvc) may be NULL */
2859                 vnode_put(tvp);
2860                 AFS_GLOCK();
2861                 continue;
2862             }
2863 #endif /* AFS_DARWIN80_ENV */
2864             count++;
2865             if (found_tvc) {
2866                 /* Duplicates */
2867 #ifdef AFS_OSF_ENV
2868                 /* Drop our reference counts. */
2869                 vrele(AFSTOV(tvc));
2870                 vrele(AFSTOV(found_tvc));
2871 #endif
2872                 afs_duplicate_nfs_fids++;
2873                 ReleaseSharedLock(&afs_xvcache);
2874 #ifdef AFS_DARWIN80_ENV
2875                 /* Drop our reference counts. */
2876                 vnode_put(AFSTOV(tvc));
2877                 vnode_put(AFSTOV(found_tvc));
2878 #endif
2879                 return count;
2880             }
2881             found_tvc = tvc;
2882         }
2883     }
2884
2885     tvc = found_tvc;
2886     /* should I have a read lock on the vnode here? */
2887     if (tvc) {
2888 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2889         afs_int32 retry = 0;
2890         osi_vnhold(tvc, &retry);
2891         if (retry) {
2892             count = 0;
2893             found_tvc = (struct vcache *)0;
2894             ReleaseSharedLock(&afs_xvcache);
2895             spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2896             goto loop;
2897         }
2898 #else
2899 #if !defined(AFS_OSF_ENV)
2900         osi_vnhold(tvc, (int *)0);      /* already held, above */
2901 #endif
2902 #endif
2903         /*
2904          * We obtained the xvcache lock above.
2905          */
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 inconsistent1");
2911         }
2912         if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2913             refpanic("FindVC VLRU inconsistent2");
2914         }
2915         UpgradeSToWLock(&afs_xvcache, 568);
2916         QRemove(&tvc->vlruq);
2917         QAdd(&VLRU, &tvc->vlruq);
2918         ConvertWToSLock(&afs_xvcache);
2919         if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2920             refpanic("FindVC VLRU inconsistent1");
2921         }
2922         if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2923             refpanic("FindVC VLRU inconsistent2");
2924         }
2925         if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2926             refpanic("FindVC VLRU inconsistent3");
2927         }
2928     }
2929     vcachegen++;
2930
2931     if (tvc)
2932         afs_stats_cmperf.vcacheHits++;
2933     else
2934         afs_stats_cmperf.vcacheMisses++;
2935     if (afs_IsPrimaryCellNum(afid->Cell))
2936         afs_stats_cmperf.vlocalAccesses++;
2937     else
2938         afs_stats_cmperf.vremoteAccesses++;
2939
2940     *avcp = tvc;                /* May be null */
2941
2942     ReleaseSharedLock(&afs_xvcache);
2943     return (tvc ? 1 : 0);
2944
2945 }                               /*afs_NFSFindVCache */
2946
2947
2948
2949
2950 /*
2951  * afs_vcacheInit
2952  *
2953  * Initialize vcache related variables
2954  */
2955 void
2956 afs_vcacheInit(int astatSize)
2957 {
2958     register struct vcache *tvp;
2959     int i;
2960 #if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
2961     if (!afs_maxvcount) {
2962 #if defined(AFS_LINUX22_ENV)
2963         afs_maxvcount = astatSize;      /* no particular limit on linux? */
2964 #elif defined(AFS_OSF30_ENV)
2965         afs_maxvcount = max_vnodes / 2; /* limit ourselves to half the total */
2966 #else
2967         afs_maxvcount = nvnode / 2;     /* limit ourselves to half the total */
2968 #endif
2969         if (astatSize < afs_maxvcount) {
2970             afs_maxvcount = astatSize;
2971         }
2972     }
2973 #else /* AFS_OSF_ENV */
2974     freeVCList = NULL;
2975 #endif
2976
2977     RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2978     LOCK_INIT(&afs_xvcb, "afs_xvcb");
2979
2980 #if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
2981     /* Allocate and thread the struct vcache entries */
2982     tvp = (struct vcache *)afs_osi_Alloc(astatSize * sizeof(struct vcache));
2983     memset((char *)tvp, 0, sizeof(struct vcache) * astatSize);
2984
2985     Initial_freeVCList = tvp;
2986     freeVCList = &(tvp[0]);
2987     for (i = 0; i < astatSize - 1; i++) {
2988         tvp[i].nextfree = &(tvp[i + 1]);
2989     }
2990     tvp[astatSize - 1].nextfree = NULL;
2991 #ifdef  KERNEL_HAVE_PIN
2992     pin((char *)tvp, astatSize * sizeof(struct vcache));        /* XXX */
2993 #endif
2994 #endif
2995
2996 #if defined(AFS_SGI_ENV)
2997     for (i = 0; i < astatSize; i++) {
2998         char name[METER_NAMSZ];
2999         struct vcache *tvc = &tvp[i];
3000
3001         tvc->v.v_number = ++afsvnumbers;
3002         tvc->vc_rwlockid = OSI_NO_LOCKID;
3003         initnsema(&tvc->vc_rwlock, 1,
3004                   makesname(name, "vrw", tvc->v.v_number));
3005 #ifndef AFS_SGI53_ENV
3006         initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
3007 #endif
3008 #ifndef AFS_SGI62_ENV
3009         initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
3010 #endif /* AFS_SGI62_ENV */
3011     }
3012 #endif
3013     QInit(&VLRU);
3014     for(i = 0; i < VCSIZE; ++i)
3015         QInit(&afs_vhashTV[i]);
3016 }
3017
3018 /*
3019  * shutdown_vcache
3020  *
3021  */
3022 void
3023 shutdown_vcache(void)
3024 {
3025     int i;
3026     struct afs_cbr *tsp, *nsp;
3027     /*
3028      * XXX We may potentially miss some of the vcaches because if when there're no
3029      * free vcache entries and all the vcache entries are active ones then we allocate
3030      * an additional one - admittedly we almost never had that occur.
3031      */
3032
3033     {
3034         register struct afs_q *tq, *uq;
3035         register struct vcache *tvc;
3036         for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
3037             tvc = QTOV(tq);
3038             uq = QPrev(tq);
3039             if (tvc->mvid) {
3040                 osi_FreeSmallSpace(tvc->mvid);
3041                 tvc->mvid = (struct VenusFid *)0;
3042             }
3043 #ifdef  AFS_AIX_ENV
3044             aix_gnode_rele(AFSTOV(tvc));
3045 #endif
3046             if (tvc->linkData) {
3047                 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3048                 tvc->linkData = 0;
3049             }
3050         }
3051         /*
3052          * Also free the remaining ones in the Cache
3053          */
3054         for (i = 0; i < VCSIZE; i++) {
3055             for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3056                 if (tvc->mvid) {
3057                     osi_FreeSmallSpace(tvc->mvid);
3058                     tvc->mvid = (struct VenusFid *)0;
3059                 }
3060 #ifdef  AFS_AIX_ENV
3061                 if (tvc->v.v_gnode)
3062                     afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
3063 #ifdef  AFS_AIX32_ENV
3064                 if (tvc->segid) {
3065                     AFS_GUNLOCK();
3066                     vms_delete(tvc->segid);
3067                     AFS_GLOCK();
3068                     tvc->segid = tvc->vmh = NULL;
3069                     if (VREFCOUNT_GT(tvc,0))
3070                         osi_Panic("flushVcache: vm race");
3071                 }
3072                 if (tvc->credp) {
3073                     crfree(tvc->credp);
3074                     tvc->credp = NULL;
3075                 }
3076 #endif
3077 #endif
3078 #if     defined(AFS_SUN5_ENV)
3079                 if (tvc->credp) {
3080                     crfree(tvc->credp);
3081                     tvc->credp = NULL;
3082                 }
3083 #endif
3084                 if (tvc->linkData) {
3085                     afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3086                     tvc->linkData = 0;
3087                 }
3088
3089                 afs_FreeAllAxs(&(tvc->Access));
3090             }
3091             afs_vhashT[i] = 0;
3092         }
3093     }
3094     /*
3095      * Free any leftover callback queue
3096      */
3097     for (tsp = afs_cbrSpace; tsp; tsp = nsp) {
3098         nsp = tsp->next;
3099         afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
3100     }
3101     afs_cbrSpace = 0;
3102
3103 #if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
3104     afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3105 #endif
3106 #ifdef  KERNEL_HAVE_PIN
3107     unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3108 #endif
3109
3110 #if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
3111     freeVCList = Initial_freeVCList = 0;
3112 #endif
3113     RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
3114     LOCK_INIT(&afs_xvcb, "afs_xvcb");
3115     QInit(&VLRU);
3116     for(i = 0; i < VCSIZE; ++i)
3117         QInit(&afs_vhashTV[i]);
3118 }
3119
3120 #ifdef AFS_DISCON_ENV
3121 void afs_DisconGiveUpCallbacks() {
3122     int i;
3123     struct vcache *tvc;
3124     int nq=0;
3125             
3126     ObtainWriteLock(&afs_xvcache, 1002); /* XXX - should be a unique number */
3127     
3128     /* Somehow, walk the set of vcaches, with each one coming out as tvc */
3129     for (i = 0; i < VCSIZE; i++) {
3130         for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3131             if ((tvc->states & CRO) == 0 && tvc->callback) {
3132                 /* XXX - should we check if the callback has expired here? */
3133                 afs_QueueVCB(tvc);
3134                 tvc->callback = NULL;
3135                 tvc->states &- ~(CStatd | CUnique);
3136                 nq++;
3137             }
3138         }
3139     }
3140     /*printf("%d callbacks to be discarded. queued ... ", nq);*/
3141     afs_FlushVCBs(0);
3142     
3143     ReleaseWriteLock(&afs_xvcache);
3144     /*printf("gone\n");*/
3145 }
3146
3147 #endif