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