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