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