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