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