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