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