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