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