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