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