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