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