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