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