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