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