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