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