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