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