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