2 * Copyright 2000, International Business Machines Corporation and others.
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
19 * afs_FlushActiveVcaches
38 #include <afsconfig.h>
39 #include "afs/param.h"
44 #include "afs/sysincludes.h" /*Standard vendor system headers */
45 #include "afsincludes.h" /*AFS-based standard headers */
46 #include "afs/afs_stats.h"
47 #include "afs/afs_cbqueue.h"
48 #include "afs/afs_osidnlc.h"
50 #if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
51 afs_int32 afs_maxvcount = 0; /* max number of vcache entries */
52 afs_int32 afs_vcount = 0; /* number of vcache in use now */
53 #endif /* AFS_OSF_ENV */
61 #endif /* AFS_SGI64_ENV */
63 /* Exported variables */
64 afs_rwlock_t afs_xvcache; /*Lock: alloc new stat cache entries */
65 afs_rwlock_t afs_xvreclaim; /*Lock: entries reclaimed, not on free list */
66 afs_lock_t afs_xvcb; /*Lock: fids on which there are callbacks */
67 #if !defined(AFS_LINUX22_ENV)
68 static struct vcache *freeVCList; /*Free list for stat cache entries */
69 struct vcache *ReclaimedVCList; /*Reclaimed list for stat entries */
70 static struct vcache *Initial_freeVCList; /*Initial list for above */
72 struct afs_q VLRU; /*vcache LRU */
73 afs_int32 vcachegen = 0;
74 unsigned int afs_paniconwarn = 0;
75 struct vcache *afs_vhashT[VCSIZE];
76 struct afs_q afs_vhashTV[VCSIZE];
77 static struct afs_cbr *afs_cbrHashT[CBRSIZE];
78 afs_int32 afs_bulkStatsLost;
79 int afs_norefpanic = 0;
81 /* Forward declarations */
82 static afs_int32 afs_QueueVCB(struct vcache *avc);
87 * Generate an index into the hash table for a given Fid.
90 afs_HashCBRFid(struct AFSFid *fid)
92 return (fid->Volume + fid->Vnode + fid->Unique) % CBRSIZE;
98 * Insert a CBR entry into the hash table.
99 * Must be called with afs_xvcb held.
102 afs_InsertHashCBR(struct afs_cbr *cbr)
104 int slot = afs_HashCBRFid(&cbr->fid);
106 cbr->hash_next = afs_cbrHashT[slot];
107 if (afs_cbrHashT[slot])
108 afs_cbrHashT[slot]->hash_pprev = &cbr->hash_next;
110 cbr->hash_pprev = &afs_cbrHashT[slot];
111 afs_cbrHashT[slot] = cbr;
118 * Flush the given vcache entry.
121 * avc : Pointer to vcache entry to flush.
122 * slept : Pointer to int to set 1 if we sleep/drop locks, 0 if we don't.
125 * afs_xvcache lock must be held for writing upon entry to
126 * prevent people from changing the vrefCount field, and to
127 * protect the lruq and hnext fields.
128 * LOCK: afs_FlushVCache afs_xvcache W
129 * REFCNT: vcache ref count must be zero on entry except for osf1
130 * RACE: lock is dropped and reobtained, permitting race in caller
134 afs_FlushVCache(struct vcache *avc, int *slept)
135 { /*afs_FlushVCache */
138 struct vcache **uvc, *wvc;
141 AFS_STATCNT(afs_FlushVCache);
142 afs_Trace2(afs_iclSetp, CM_TRACE_FLUSHV, ICL_TYPE_POINTER, avc,
143 ICL_TYPE_INT32, avc->states);
146 VN_LOCK(AFSTOV(avc));
150 code = osi_VM_FlushVCache(avc, slept);
154 if (avc->states & CVFlushed) {
158 #if !defined(AFS_LINUX22_ENV)
159 if (avc->nextfree || !avc->vlruq.prev || !avc->vlruq.next) { /* qv afs.h */
160 refpanic("LRU vs. Free inconsistency");
163 avc->states |= CVFlushed;
164 /* pull the entry out of the lruq and put it on the free list */
165 QRemove(&avc->vlruq);
167 /* keep track of # of files that we bulk stat'd, but never used
168 * before they got recycled.
170 if (avc->states & CBulkStat)
173 /* remove entry from the hash chain */
174 i = VCHash(&avc->fid);
175 uvc = &afs_vhashT[i];
176 for (wvc = *uvc; wvc; uvc = &wvc->hnext, wvc = *uvc) {
179 avc->hnext = (struct vcache *)NULL;
184 /* remove entry from the volume hash table */
185 QRemove(&avc->vhashq);
188 osi_FreeSmallSpace(avc->mvid);
189 avc->mvid = (struct VenusFid *)0;
191 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
192 avc->linkData = NULL;
194 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
195 /* OK, there are no internal vrefCounts, so there shouldn't
196 * be any more refs here. */
198 #ifdef AFS_DARWIN80_ENV
199 vnode_clearfsnode(AFSTOV(avc));
200 vnode_removefsref(AFSTOV(avc));
202 avc->v->v_data = NULL; /* remove from vnode */
204 AFSTOV(avc) = NULL; /* also drop the ptr to vnode */
207 #ifdef AFS_SUN510_ENV
208 /* As we use private vnodes, cleanup is up to us */
209 vn_reinit(AFSTOV(avc));
211 afs_FreeAllAxs(&(avc->Access));
213 /* we can't really give back callbacks on RO files, since the
214 * server only tracks them on a per-volume basis, and we don't
215 * know whether we still have some other files from the same
217 if ((avc->states & CRO) == 0 && avc->callback) {
220 ObtainWriteLock(&afs_xcbhash, 460);
221 afs_DequeueCallback(avc); /* remove it from queued callbacks list */
222 avc->states &= ~(CStatd | CUnique);
223 ReleaseWriteLock(&afs_xcbhash);
224 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
225 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
227 osi_dnlc_purgevp(avc);
230 * Next, keep track of which vnodes we've deleted for create's
231 * optimistic synchronization algorithm
234 if (avc->fid.Fid.Vnode & 1)
239 #if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
240 /* put the entry in the free list */
241 avc->nextfree = freeVCList;
243 if (avc->vlruq.prev || avc->vlruq.next) {
244 refpanic("LRU vs. Free inconsistency");
246 avc->states |= CVFlushed;
248 /* This should put it back on the vnode free list since usecount is 1 */
251 if (VREFCOUNT_GT(avc,0)) {
252 #if defined(AFS_OSF_ENV)
253 VN_UNLOCK(AFSTOV(avc));
255 AFS_RELE(AFSTOV(avc));
257 if (afs_norefpanic) {
258 printf("flush vc refcnt < 1");
260 #if defined(AFS_OSF_ENV)
261 (void)vgone(avc, VX_NOSLEEP, NULL);
263 VN_UNLOCK(AFSTOV(avc));
266 osi_Panic("flush vc refcnt < 1");
268 #endif /* AFS_OSF_ENV */
273 VN_UNLOCK(AFSTOV(avc));
277 } /*afs_FlushVCache */
283 * The core of the inactive vnode op for all but IRIX.
286 afs_InactiveVCache(struct vcache *avc, struct AFS_UCRED *acred)
288 AFS_STATCNT(afs_inactive);
289 if (avc->states & CDirty) {
290 /* we can't keep trying to push back dirty data forever. Give up. */
291 afs_InvalidateAllSegments(avc); /* turns off dirty bit */
293 avc->states &= ~CMAPPED; /* mainly used by SunOS 4.0.x */
294 avc->states &= ~CDirty; /* Turn it off */
295 if (avc->states & CUnlinked) {
296 if (CheckLock(&afs_xvcache) || CheckLock(&afs_xdcache)) {
297 avc->states |= CUnlinkedDel;
300 afs_remunlink(avc, 1); /* ignore any return code */
309 * Description: allocate a callback return structure from the
310 * free list and return it.
312 * Env: The alloc and free routines are both called with the afs_xvcb lock
313 * held, so we don't have to worry about blocking in osi_Alloc.
315 static struct afs_cbr *afs_cbrSpace = 0;
319 register struct afs_cbr *tsp;
322 while (!afs_cbrSpace) {
323 if (afs_stats_cmperf.CallBackAlloced >= 2) {
324 /* don't allocate more than 2 * AFS_NCBRS for now */
326 afs_stats_cmperf.CallBackFlushes++;
330 (struct afs_cbr *)afs_osi_Alloc(AFS_NCBRS *
331 sizeof(struct afs_cbr));
332 for (i = 0; i < AFS_NCBRS - 1; i++) {
333 tsp[i].next = &tsp[i + 1];
335 tsp[AFS_NCBRS - 1].next = 0;
337 afs_stats_cmperf.CallBackAlloced++;
341 afs_cbrSpace = tsp->next;
348 * Description: free a callback return structure, removing it from all lists.
351 * asp -- the address of the structure to free.
353 * Environment: the xvcb lock is held over these calls.
356 afs_FreeCBR(register struct afs_cbr *asp)
358 *(asp->pprev) = asp->next;
360 asp->next->pprev = asp->pprev;
362 *(asp->hash_pprev) = asp->hash_next;
364 asp->hash_next->hash_pprev = asp->hash_pprev;
366 asp->next = afs_cbrSpace;
374 * Description: flush all queued callbacks to all servers.
378 * Environment: holds xvcb lock over RPC to guard against race conditions
379 * when a new callback is granted for the same file later on.
382 afs_FlushVCBs(afs_int32 lockit)
384 struct AFSFid *tfids;
385 struct AFSCallBack callBacks[1];
386 struct AFSCBFids fidArray;
387 struct AFSCBs cbArray;
389 struct afs_cbr *tcbrp;
393 struct vrequest treq;
395 int safety1, safety2, safety3;
397 if ((code = afs_InitReq(&treq, afs_osi_credp)))
399 treq.flags |= O_NONBLOCK;
400 tfids = afs_osi_Alloc(sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
403 MObtainWriteLock(&afs_xvcb, 273);
404 ObtainReadLock(&afs_xserver);
405 for (i = 0; i < NSERVERS; i++) {
406 for (safety1 = 0, tsp = afs_servers[i];
407 tsp && safety1 < afs_totalServers + 10;
408 tsp = tsp->next, safety1++) {
410 if (tsp->cbrs == (struct afs_cbr *)0)
413 /* otherwise, grab a block of AFS_MAXCBRSCALL from the list
414 * and make an RPC, over and over again.
416 tcount = 0; /* number found so far */
417 for (safety2 = 0; safety2 < afs_cacheStats; safety2++) {
418 if (tcount >= AFS_MAXCBRSCALL || !tsp->cbrs) {
419 /* if buffer is full, or we've queued all we're going
420 * to from this server, we should flush out the
423 fidArray.AFSCBFids_len = tcount;
424 fidArray.AFSCBFids_val = (struct AFSFid *)tfids;
425 cbArray.AFSCBs_len = 1;
426 cbArray.AFSCBs_val = callBacks;
427 memset(&callBacks[0], 0, sizeof(callBacks[0]));
428 callBacks[0].CallBackType = CB_EXCLUSIVE;
429 for (safety3 = 0; safety3 < MAXHOSTS * 2; safety3++) {
430 tc = afs_ConnByHost(tsp, tsp->cell->fsport,
431 tsp->cell->cellNum, &treq, 0,
435 (AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS);
438 RXAFS_GiveUpCallBacks(tc->id, &fidArray,
446 AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS, SHARED_LOCK,
451 /* ignore return code, since callbacks may have
452 * been returned anyway, we shouldn't leave them
453 * around to be returned again.
455 * Next, see if we are done with this server, and if so,
456 * break to deal with the next one.
462 /* if to flush full buffer */
463 /* if we make it here, we have an entry at the head of cbrs,
464 * which we should copy to the file ID array and then free.
467 tfids[tcount++] = tcbrp->fid;
469 /* Freeing the CBR will unlink it from the server's CBR list */
471 } /* while loop for this one server */
472 if (safety2 > afs_cacheStats) {
473 afs_warn("possible internal error afs_flushVCBs (%d)\n",
476 } /* for loop for this hash chain */
477 } /* loop through all hash chains */
478 if (safety1 > afs_totalServers + 2) {
480 ("AFS internal error (afs_flushVCBs) (%d > %d), continuing...\n",
481 safety1, afs_totalServers + 2);
483 osi_Panic("afs_flushVCBS safety1");
486 ReleaseReadLock(&afs_xserver);
488 MReleaseWriteLock(&afs_xvcb);
489 afs_osi_Free(tfids, sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
497 * Queue a callback on the given fid.
503 * Locks the xvcb lock.
504 * Called when the xvcache lock is already held.
508 afs_QueueVCB(struct vcache *avc)
511 struct afs_cbr *tcbp;
513 AFS_STATCNT(afs_QueueVCB);
514 /* The callback is really just a struct server ptr. */
515 tsp = (struct server *)(avc->callback);
517 /* we now have a pointer to the server, so we just allocate
518 * a queue entry and queue it.
520 MObtainWriteLock(&afs_xvcb, 274);
521 tcbp = afs_AllocCBR();
522 tcbp->fid = avc->fid.Fid;
524 tcbp->next = tsp->cbrs;
526 tsp->cbrs->pprev = &tcbp->next;
529 tcbp->pprev = &tsp->cbrs;
531 afs_InsertHashCBR(tcbp);
533 /* now release locks and return */
534 MReleaseWriteLock(&afs_xvcb);
543 * Remove a queued callback for a given Fid.
546 * afid: The fid we want cleansed of queued callbacks.
549 * Locks xvcb and xserver locks.
550 * Typically called with xdcache, xvcache and/or individual vcache
555 afs_RemoveVCB(struct VenusFid *afid)
558 struct afs_cbr *cbr, *ncbr;
560 AFS_STATCNT(afs_RemoveVCB);
561 MObtainWriteLock(&afs_xvcb, 275);
563 slot = afs_HashCBRFid(&afid->Fid);
564 ncbr = afs_cbrHashT[slot];
568 ncbr = cbr->hash_next;
570 if (afid->Fid.Volume == cbr->fid.Volume &&
571 afid->Fid.Vnode == cbr->fid.Vnode &&
572 afid->Fid.Unique == cbr->fid.Unique) {
577 MReleaseWriteLock(&afs_xvcb);
581 afs_FlushReclaimedVcaches(void)
583 #if !defined(AFS_LINUX22_ENV)
586 struct vcache *tmpReclaimedVCList = NULL;
588 ObtainWriteLock(&afs_xvreclaim, 76);
589 while (ReclaimedVCList) {
590 tvc = ReclaimedVCList; /* take from free list */
591 ReclaimedVCList = tvc->nextfree;
592 tvc->nextfree = NULL;
593 code = afs_FlushVCache(tvc, &fv_slept);
595 /* Ok, so, if we got code != 0, uh, wtf do we do? */
596 /* Probably, build a temporary list and then put all back when we
597 get to the end of the list */
598 /* This is actually really crappy, but we need to not leak these.
599 We probably need a way to be smarter about this. */
600 tvc->nextfree = tmpReclaimedVCList;
601 tmpReclaimedVCList = tvc;
602 printf("Reclaim list flush %lx failed: %d\n", (unsigned long) tvc, code);
604 if (tvc->states & (CVInit
605 #ifdef AFS_DARWIN80_ENV
609 tvc->states &= ~(CVInit
610 #ifdef AFS_DARWIN80_ENV
614 afs_osi_Wakeup(&tvc->states);
617 if (tmpReclaimedVCList)
618 ReclaimedVCList = tmpReclaimedVCList;
620 ReleaseWriteLock(&afs_xvreclaim);
628 * This routine is responsible for allocating a new cache entry
629 * from the free list. It formats the cache entry and inserts it
630 * into the appropriate hash tables. It must be called with
631 * afs_xvcache write-locked so as to prevent several processes from
632 * trying to create a new cache entry simultaneously.
635 * afid : The file id of the file whose cache entry is being
638 /* LOCK: afs_NewVCache afs_xvcache W */
640 afs_NewVCache(struct VenusFid *afid, struct server *serverp)
644 afs_int32 anumber = VCACHE_FREE;
646 struct gnode *gnodepnt;
650 #endif /* AFS_OSF_ENV */
651 struct afs_q *tq, *uq;
654 AFS_STATCNT(afs_NewVCache);
656 afs_FlushReclaimedVcaches();
658 #if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
659 #if defined(AFS_OSF30_ENV) || defined(AFS_LINUX22_ENV)
660 if (afs_vcount >= afs_maxvcount)
663 * If we are using > 33 % of the total system vnodes for AFS vcache
664 * entries or we are using the maximum number of vcache entries,
665 * then free some. (if our usage is > 33% we should free some, if
666 * our usage is > afs_maxvcount, set elsewhere to 0.5*nvnode,
667 * we _must_ free some -- no choice).
669 if (((3 * afs_vcount) > nvnode) || (afs_vcount >= afs_maxvcount))
676 for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
679 if (tvc->states & CVFlushed) {
680 refpanic("CVFlushed on VLRU");
681 } else if (i++ > afs_maxvcount) {
682 refpanic("Exceeded pool of AFS vnodes(VLRU cycle?)");
683 } else if (QNext(uq) != tq) {
684 refpanic("VLRU inconsistent");
685 } else if (!VREFCOUNT_GT(tvc,0)) {
686 refpanic("refcnt 0 on VLRU");
689 #if defined(AFS_LINUX22_ENV)
690 if (tvc != afs_globalVp && VREFCOUNT(tvc) > 1 && tvc->opens == 0) {
691 struct dentry *dentry;
692 struct list_head *cur, *head;
694 #if defined(AFS_LINUX24_ENV)
695 spin_lock(&dcache_lock);
697 head = &(AFSTOV(tvc))->i_dentry;
701 while ((cur = cur->next) != head) {
702 dentry = list_entry(cur, struct dentry, d_alias);
704 if (d_unhashed(dentry))
709 #if defined(AFS_LINUX24_ENV)
710 spin_unlock(&dcache_lock);
712 if (d_invalidate(dentry) == -EBUSY) {
714 /* perhaps lock and try to continue? (use cur as head?) */
718 #if defined(AFS_LINUX24_ENV)
719 spin_lock(&dcache_lock);
723 #if defined(AFS_LINUX24_ENV)
724 spin_unlock(&dcache_lock);
731 if (VREFCOUNT_GT(tvc,0) && !VREFCOUNT_GT(tvc,1) &&
733 && (tvc->states & CUnlinkedDel) == 0) {
734 code = afs_FlushVCache(tvc, &fv_slept);
741 continue; /* start over - may have raced. */
747 if (anumber == VCACHE_FREE) {
748 printf("afs_NewVCache: warning none freed, using %d of %d\n",
749 afs_vcount, afs_maxvcount);
750 if (afs_vcount >= afs_maxvcount) {
751 printf("afs_NewVCache - none freed\n");
757 #if defined(AFS_LINUX22_ENV)
762 ip = new_inode(afs_globalVFS);
764 osi_Panic("afs_NewVCache: no more inodes");
766 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
769 tvc = afs_osi_Alloc(sizeof(struct vcache));
770 ip->u.generic_ip = tvc;
776 if (getnewvnode(MOUNT_AFS, &Afs_vnodeops, &nvc)) {
777 /* What should we do ???? */
778 osi_Panic("afs_NewVCache: no more vnodes");
783 tvc->nextfree = NULL;
786 #else /* AFS_OSF_ENV */
787 /* pull out a free cache entry */
791 for (tq = VLRU.prev; (anumber > 0) && (tq != &VLRU); tq = uq) {
795 if (tvc->states & CVFlushed) {
796 refpanic("CVFlushed on VLRU");
797 } else if (i++ > 2 * afs_cacheStats) { /* even allowing for a few xallocs... */
798 refpanic("Increase -stat parameter of afsd(VLRU cycle?)");
799 } else if (QNext(uq) != tq) {
800 refpanic("VLRU inconsistent");
801 } else if (tvc->states & CVInit) {
805 if (!VREFCOUNT_GT(tvc,0)
806 #if defined(AFS_DARWIN_ENV) && !defined(UKERNEL) && !defined(AFS_DARWIN80_ENV)
807 || ((VREFCOUNT(tvc) == 1) &&
808 (UBCINFOEXISTS(AFSTOV(tvc))))
810 && tvc->opens == 0 && (tvc->states & CUnlinkedDel) == 0) {
811 #if defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
812 #ifdef AFS_DARWIN80_ENV
813 vnode_t tvp = AFSTOV(tvc);
814 /* VREFCOUNT_GT only sees usecounts, not iocounts */
815 /* so this may fail to actually recycle the vnode now */
816 /* must call vnode_get to avoid races. */
818 if (vnode_get(tvp) == 0) {
820 /* must release lock, since vnode_put will immediately
821 reclaim if there are no other users */
822 ReleaseWriteLock(&afs_xvcache);
827 ObtainWriteLock(&afs_xvcache, 336);
829 /* we can't use the vnode_recycle return value to figure
830 * this out, since the iocount we have to hold makes it
832 if (AFSTOV(tvc) == tvp) {
833 if (anumber > 0 && fv_slept) {
834 QRemove(&tvc->vlruq);
835 QAdd(&VLRU, &tvc->vlruq);
842 * vgone() reclaims the vnode, which calls afs_FlushVCache(),
843 * then it puts the vnode on the free list.
844 * If we don't do this we end up with a cleaned vnode that's
845 * not on the free list.
846 * XXX assume FreeBSD is the same for now.
855 code = afs_FlushVCache(tvc, &fv_slept);
865 continue; /* start over - may have raced. */
873 /* none free, making one is better than a panic */
874 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
875 tvc = (struct vcache *)afs_osi_Alloc(sizeof(struct vcache));
876 #if defined(AFS_DARWIN_ENV) && !defined(UKERNEL)
877 tvc->v = NULL; /* important to clean this, or use memset 0 */
879 #ifdef KERNEL_HAVE_PIN
880 pin((char *)tvc, sizeof(struct vcache)); /* XXX */
882 #if defined(AFS_SGI_ENV)
884 char name[METER_NAMSZ];
885 memset(tvc, 0, sizeof(struct vcache));
886 tvc->v.v_number = ++afsvnumbers;
887 tvc->vc_rwlockid = OSI_NO_LOCKID;
888 initnsema(&tvc->vc_rwlock, 1,
889 makesname(name, "vrw", tvc->v.v_number));
890 #ifndef AFS_SGI53_ENV
891 initnsema(&tvc->v.v_sync, 0,
892 makesname(name, "vsy", tvc->v.v_number));
894 #ifndef AFS_SGI62_ENV
895 initnlock(&tvc->v.v_lock,
896 makesname(name, "vlk", tvc->v.v_number));
899 #endif /* AFS_SGI_ENV */
901 tvc = freeVCList; /* take from free list */
902 freeVCList = tvc->nextfree;
903 tvc->nextfree = NULL;
905 #endif /* AFS_OSF_ENV */
907 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
909 panic("afs_NewVCache(): free vcache with vnode attached");
912 #if !defined(AFS_SGI_ENV) && !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
913 memset((char *)tvc, 0, sizeof(struct vcache));
918 RWLOCK_INIT(&tvc->lock, "vcache lock");
919 #if defined(AFS_SUN5_ENV)
920 RWLOCK_INIT(&tvc->vlock, "vcache vlock");
921 #endif /* defined(AFS_SUN5_ENV) */
923 tvc->parentVnode = 0;
925 tvc->linkData = NULL;
928 tvc->execsOrWriters = 0;
931 tvc->states = CVInit;
932 tvc->last_looker = 0;
934 tvc->asynchrony = -1;
937 tvc->flushDV.low = tvc->flushDV.high = AFS_MAXDV;
940 tvc->truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
941 hzero(tvc->m.DataVersion); /* in case we copy it into flushDV */
943 tvc->callback = serverp; /* to minimize chance that clear
949 tvc->hnext = afs_vhashT[i];
951 QAdd(&afs_vhashTV[j], &tvc->vhashq);
953 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
954 refpanic("NewVCache VLRU inconsistent");
956 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
957 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
958 refpanic("NewVCache VLRU inconsistent2");
960 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
961 refpanic("NewVCache VLRU inconsistent3");
963 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
964 refpanic("NewVCache VLRU inconsistent4");
967 /* it should now be safe to drop the xvcache lock */
969 ReleaseWriteLock(&afs_xvcache);
971 afs_nbsd_getnewvnode(tvc); /* includes one refcount */
973 ObtainWriteLock(&afs_xvcache,337);
974 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
976 #ifdef AFS_DARWIN_ENV
977 ReleaseWriteLock(&afs_xvcache);
979 afs_darwin_getnewvnode(tvc); /* includes one refcount */
981 ObtainWriteLock(&afs_xvcache,338);
982 #ifdef AFS_DARWIN80_ENV
983 LOCKINIT(tvc->rwlock);
985 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
992 ReleaseWriteLock(&afs_xvcache);
994 #if defined(AFS_FBSD60_ENV)
995 if (getnewvnode(MOUNT_AFS, afs_globalVFS, &afs_vnodeops, &vp))
996 #elif defined(AFS_FBSD50_ENV)
997 if (getnewvnode(MOUNT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
999 if (getnewvnode(VT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
1001 panic("afs getnewvnode"); /* can't happen */
1003 ObtainWriteLock(&afs_xvcache,339);
1004 if (tvc->v != NULL) {
1005 /* I'd like to know if this ever happens...
1006 * We don't drop global for the rest of this function,
1007 * so if we do lose the race, the other thread should
1008 * have found the same vnode and finished initializing
1009 * the vcache entry. Is it conceivable that this vcache
1010 * entry could be recycled during this interval? If so,
1011 * then there probably needs to be some sort of additional
1012 * mutual exclusion (an Embryonic flag would suffice).
1014 printf("afs_NewVCache: lost the race\n");
1018 tvc->v->v_data = tvc;
1019 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
1023 #if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
1024 /* Hold it for the LRU (should make count 2) */
1025 VN_HOLD(AFSTOV(tvc));
1026 #else /* AFS_OSF_ENV */
1027 #if !(defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV))
1028 VREFCOUNT_SET(tvc, 1); /* us */
1029 #endif /* AFS_XBSD_ENV */
1030 #endif /* AFS_OSF_ENV */
1031 #ifdef AFS_AIX32_ENV
1032 LOCK_INIT(&tvc->pvmlock, "vcache pvmlock");
1033 tvc->vmh = tvc->segid = NULL;
1036 #ifdef AFS_BOZONLOCK_ENV
1037 #if defined(AFS_SUN5_ENV)
1038 rw_init(&tvc->rwlock, "vcache rwlock", RW_DEFAULT, NULL);
1040 #if defined(AFS_SUN55_ENV)
1041 /* This is required if the kaio (kernel aynchronous io)
1042 ** module is installed. Inside the kernel, the function
1043 ** check_vp( common/os/aio.c) checks to see if the kernel has
1044 ** to provide asynchronous io for this vnode. This
1045 ** function extracts the device number by following the
1046 ** v_data field of the vnode. If we do not set this field
1047 ** then the system panics. The value of the v_data field
1048 ** is not really important for AFS vnodes because the kernel
1049 ** does not do asynchronous io for regular files. Hence,
1050 ** for the time being, we fill up the v_data field with the
1051 ** vnode pointer itself. */
1052 tvc->v.v_data = (char *)tvc;
1053 #endif /* AFS_SUN55_ENV */
1055 afs_BozonInit(&tvc->pvnLock, tvc);
1058 /* initialize vnode data, note vrefCount is v.v_count */
1060 /* Don't forget to free the gnode space */
1061 tvc->v.v_gnode = gnodepnt =
1062 (struct gnode *)osi_AllocSmallSpace(sizeof(struct gnode));
1063 memset((char *)gnodepnt, 0, sizeof(struct gnode));
1065 #ifdef AFS_SGI64_ENV
1066 memset((void *)&(tvc->vc_bhv_desc), 0, sizeof(tvc->vc_bhv_desc));
1067 bhv_desc_init(&(tvc->vc_bhv_desc), tvc, tvc, &Afs_vnodeops);
1068 #ifdef AFS_SGI65_ENV
1069 vn_bhv_head_init(&(tvc->v.v_bh), "afsvp");
1070 vn_bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
1072 bhv_head_init(&(tvc->v.v_bh));
1073 bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
1075 #ifdef AFS_SGI65_ENV
1076 tvc->v.v_mreg = tvc->v.v_mregb = (struct pregion *)tvc;
1077 #ifdef VNODE_TRACING
1078 tvc->v.v_trace = ktrace_alloc(VNODE_TRACE_SIZE, 0);
1080 init_bitlock(&tvc->v.v_pcacheflag, VNODE_PCACHE_LOCKBIT, "afs_pcache",
1082 init_mutex(&tvc->v.v_filocksem, MUTEX_DEFAULT, "afsvfl", (long)tvc);
1083 init_mutex(&tvc->v.v_buf_lock, MUTEX_DEFAULT, "afsvnbuf", (long)tvc);
1085 vnode_pcache_init(&tvc->v);
1086 #if defined(DEBUG) && defined(VNODE_INIT_BITLOCK)
1087 /* Above define is never true execpt in SGI test kernels. */
1088 init_bitlock(&(tvc->v.v_flag, VLOCK, "vnode", tvc->v.v_number);
1090 #ifdef INTR_KTHREADS
1091 AFS_VN_INIT_BUF_LOCK(&(tvc->v));
1094 SetAfsVnode(AFSTOV(tvc));
1095 #endif /* AFS_SGI64_ENV */
1097 * The proper value for mvstat (for root fids) is setup by the caller.
1100 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
1102 if (afs_globalVFS == 0)
1103 osi_Panic("afs globalvfs");
1104 #if !defined(AFS_LINUX22_ENV)
1105 vSetVfsp(tvc, afs_globalVFS);
1107 vSetType(tvc, VREG);
1109 tvc->v.v_vfsnext = afs_globalVFS->vfs_vnodes; /* link off vfs */
1110 tvc->v.v_vfsprev = NULL;
1111 afs_globalVFS->vfs_vnodes = &tvc->v;
1112 if (tvc->v.v_vfsnext != NULL)
1113 tvc->v.v_vfsnext->v_vfsprev = &tvc->v;
1114 tvc->v.v_next = gnodepnt->gn_vnode; /*Single vnode per gnode for us! */
1115 gnodepnt->gn_vnode = &tvc->v;
1117 #if defined(AFS_DUX40_ENV)
1118 insmntque(tvc, afs_globalVFS, &afs_ubcops);
1121 /* Is this needed??? */
1122 insmntque(tvc, afs_globalVFS);
1123 #endif /* AFS_OSF_ENV */
1124 #endif /* AFS_DUX40_ENV */
1125 #if defined(AFS_SGI_ENV)
1126 VN_SET_DPAGES(&(tvc->v), (struct pfdat *)NULL);
1127 osi_Assert((tvc->v.v_flag & VINACT) == 0);
1129 osi_Assert(VN_GET_PGCNT(&(tvc->v)) == 0);
1130 osi_Assert(tvc->mapcnt == 0 && tvc->vc_locktrips == 0);
1131 osi_Assert(tvc->vc_rwlockid == OSI_NO_LOCKID);
1132 osi_Assert(tvc->v.v_filocks == NULL);
1133 #if !defined(AFS_SGI65_ENV)
1134 osi_Assert(tvc->v.v_filocksem == NULL);
1136 osi_Assert(tvc->cred == NULL);
1137 #ifdef AFS_SGI64_ENV
1138 vnode_pcache_reinit(&tvc->v);
1139 tvc->v.v_rdev = NODEV;
1141 vn_initlist((struct vnlist *)&tvc->v);
1143 #endif /* AFS_SGI_ENV */
1145 osi_dnlc_purgedp(tvc); /* this may be overkill */
1146 memset((char *)&(tvc->callsort), 0, sizeof(struct afs_q));
1148 tvc->states &=~ CVInit;
1149 afs_osi_Wakeup(&tvc->states);
1153 } /*afs_NewVCache */
1157 * afs_FlushActiveVcaches
1163 * doflocks : Do we handle flocks?
1165 /* LOCK: afs_FlushActiveVcaches afs_xvcache N */
1167 afs_FlushActiveVcaches(register afs_int32 doflocks)
1169 register struct vcache *tvc;
1171 register struct conn *tc;
1172 register afs_int32 code;
1173 register struct AFS_UCRED *cred = NULL;
1174 struct vrequest treq, ureq;
1175 struct AFSVolSync tsync;
1178 AFS_STATCNT(afs_FlushActiveVcaches);
1179 ObtainReadLock(&afs_xvcache);
1180 for (i = 0; i < VCSIZE; i++) {
1181 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1182 if (tvc->states & CVInit) continue;
1183 #ifdef AFS_DARWIN80_ENV
1184 if (tvc->states & CDeadVnode &&
1185 (tvc->states & (CCore|CUnlinkedDel) ||
1186 tvc->flockCount)) panic("Dead vnode has core/unlinkedel/flock");
1188 if (doflocks && tvc->flockCount != 0) {
1189 /* if this entry has an flock, send a keep-alive call out */
1191 ReleaseReadLock(&afs_xvcache);
1192 ObtainWriteLock(&tvc->lock, 51);
1194 afs_InitReq(&treq, afs_osi_credp);
1195 treq.flags |= O_NONBLOCK;
1197 tc = afs_Conn(&tvc->fid, &treq, SHARED_LOCK);
1199 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
1202 RXAFS_ExtendLock(tc->id,
1203 (struct AFSFid *)&tvc->fid.Fid,
1209 } while (afs_Analyze
1210 (tc, code, &tvc->fid, &treq,
1211 AFS_STATS_FS_RPCIDX_EXTENDLOCK, SHARED_LOCK, NULL));
1213 ReleaseWriteLock(&tvc->lock);
1214 #ifdef AFS_DARWIN80_ENV
1216 ObtainReadLock(&afs_xvcache);
1218 ObtainReadLock(&afs_xvcache);
1223 if ((tvc->states & CCore) || (tvc->states & CUnlinkedDel)) {
1225 * Don't let it evaporate in case someone else is in
1226 * this code. Also, drop the afs_xvcache lock while
1227 * getting vcache locks.
1230 ReleaseReadLock(&afs_xvcache);
1231 #ifdef AFS_BOZONLOCK_ENV
1232 afs_BozonLock(&tvc->pvnLock, tvc);
1234 #if defined(AFS_SGI_ENV)
1236 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1238 osi_Assert(VREFCOUNT_GT(tvc,0));
1239 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1241 ObtainWriteLock(&tvc->lock, 52);
1242 if (tvc->states & CCore) {
1243 tvc->states &= ~CCore;
1244 /* XXXX Find better place-holder for cred XXXX */
1245 cred = (struct AFS_UCRED *)tvc->linkData;
1246 tvc->linkData = NULL; /* XXX */
1247 afs_InitReq(&ureq, cred);
1248 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1249 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
1250 tvc->execsOrWriters);
1251 code = afs_StoreOnLastReference(tvc, &ureq);
1252 ReleaseWriteLock(&tvc->lock);
1253 #ifdef AFS_BOZONLOCK_ENV
1254 afs_BozonUnlock(&tvc->pvnLock, tvc);
1256 hzero(tvc->flushDV);
1259 if (code && code != VNOVNODE) {
1260 afs_StoreWarn(code, tvc->fid.Fid.Volume,
1261 /* /dev/console */ 1);
1263 } else if (tvc->states & CUnlinkedDel) {
1267 ReleaseWriteLock(&tvc->lock);
1268 #ifdef AFS_BOZONLOCK_ENV
1269 afs_BozonUnlock(&tvc->pvnLock, tvc);
1271 #if defined(AFS_SGI_ENV)
1272 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1274 afs_remunlink(tvc, 0);
1275 #if defined(AFS_SGI_ENV)
1276 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1279 /* lost (or won, perhaps) the race condition */
1280 ReleaseWriteLock(&tvc->lock);
1281 #ifdef AFS_BOZONLOCK_ENV
1282 afs_BozonUnlock(&tvc->pvnLock, tvc);
1285 #if defined(AFS_SGI_ENV)
1286 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1288 #ifdef AFS_DARWIN80_ENV
1291 AFS_RELE(AFSTOV(tvc));
1292 /* Matches write code setting CCore flag */
1295 ObtainReadLock(&afs_xvcache);
1297 ObtainReadLock(&afs_xvcache);
1300 AFS_RELE(AFSTOV(tvc));
1301 /* Matches write code setting CCore flag */
1308 ReleaseReadLock(&afs_xvcache);
1317 * Make sure a cache entry is up-to-date status-wise.
1319 * NOTE: everywhere that calls this can potentially be sped up
1320 * by checking CStatd first, and avoiding doing the InitReq
1321 * if this is up-to-date.
1323 * Anymore, the only places that call this KNOW already that the
1324 * vcache is not up-to-date, so we don't screw around.
1327 * avc : Ptr to vcache entry to verify.
1332 afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1334 register struct vcache *tvc;
1336 AFS_STATCNT(afs_VerifyVCache);
1338 #if defined(AFS_OSF_ENV)
1339 ObtainReadLock(&avc->lock);
1340 if (afs_IsWired(avc)) {
1341 ReleaseReadLock(&avc->lock);
1344 ReleaseReadLock(&avc->lock);
1345 #endif /* AFS_OSF_ENV */
1346 /* otherwise we must fetch the status info */
1348 ObtainWriteLock(&avc->lock, 53);
1349 if (avc->states & CStatd) {
1350 ReleaseWriteLock(&avc->lock);
1353 ObtainWriteLock(&afs_xcbhash, 461);
1354 avc->states &= ~(CStatd | CUnique);
1355 avc->callback = NULL;
1356 afs_DequeueCallback(avc);
1357 ReleaseWriteLock(&afs_xcbhash);
1358 ReleaseWriteLock(&avc->lock);
1360 /* since we've been called back, or the callback has expired,
1361 * it's possible that the contents of this directory, or this
1362 * file's name have changed, thus invalidating the dnlc contents.
1364 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1365 osi_dnlc_purgedp(avc);
1367 osi_dnlc_purgevp(avc);
1369 /* fetch the status info */
1370 tvc = afs_GetVCache(&avc->fid, areq, NULL, avc);
1373 /* Put it back; caller has already incremented vrefCount */
1377 } /*afs_VerifyVCache */
1384 * Simple copy of stat info into cache.
1387 * avc : Ptr to vcache entry involved.
1388 * astat : Ptr to stat info to copy.
1391 * Nothing interesting.
1393 * Callers: as of 1992-04-29, only called by WriteVCache
1396 afs_SimpleVStat(register struct vcache *avc,
1397 register struct AFSFetchStatus *astat, struct vrequest *areq)
1400 AFS_STATCNT(afs_SimpleVStat);
1403 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1404 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1406 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1408 #ifdef AFS_64BIT_CLIENT
1409 FillInt64(length, astat->Length_hi, astat->Length);
1410 #else /* AFS_64BIT_CLIENT */
1411 length = astat->Length;
1412 #endif /* AFS_64BIT_CLIENT */
1413 #if defined(AFS_SGI_ENV)
1414 osi_Assert((valusema(&avc->vc_rwlock) <= 0)
1415 && (OSI_GET_LOCKID() == avc->vc_rwlockid));
1416 if (length < avc->m.Length) {
1417 vnode_t *vp = (vnode_t *) avc;
1419 osi_Assert(WriteLocked(&avc->lock));
1420 ReleaseWriteLock(&avc->lock);
1422 PTOSSVP(vp, (off_t) length, (off_t) MAXLONG);
1424 ObtainWriteLock(&avc->lock, 67);
1427 /* if writing the file, don't fetch over this value */
1428 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT, ICL_TYPE_POINTER, avc,
1429 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1430 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1431 avc->m.Length = length;
1432 avc->m.Date = astat->ClientModTime;
1434 avc->m.Owner = astat->Owner;
1435 avc->m.Group = astat->Group;
1436 avc->m.Mode = astat->UnixModeBits;
1437 if (vType(avc) == VREG) {
1438 avc->m.Mode |= S_IFREG;
1439 } else if (vType(avc) == VDIR) {
1440 avc->m.Mode |= S_IFDIR;
1441 } else if (vType(avc) == VLNK) {
1442 avc->m.Mode |= S_IFLNK;
1443 if ((avc->m.Mode & 0111) == 0)
1446 if (avc->states & CForeign) {
1447 struct axscache *ac;
1448 avc->anyAccess = astat->AnonymousAccess;
1450 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1452 * Caller has at least one bit not covered by anonymous, and
1453 * thus may have interesting rights.
1455 * HOWEVER, this is a really bad idea, because any access query
1456 * for bits which aren't covered by anonymous, on behalf of a user
1457 * who doesn't have any special rights, will result in an answer of
1458 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1459 * It's an especially bad idea under Ultrix, since (due to the lack of
1460 * a proper access() call) it must perform several afs_access() calls
1461 * in order to create magic mode bits that vary according to who makes
1462 * the call. In other words, _every_ stat() generates a test for
1465 #endif /* badidea */
1466 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1467 ac->axess = astat->CallerAccess;
1468 else /* not found, add a new one if possible */
1469 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1473 } /*afs_SimpleVStat */
1480 * Store the status info *only* back to the server for a
1484 * avc : Ptr to the vcache entry.
1485 * astatus : Ptr to the status info to store.
1486 * areq : Ptr to the associated vrequest.
1489 * Must be called with a shared lock held on the vnode.
1493 afs_WriteVCache(register struct vcache *avc,
1494 register struct AFSStoreStatus *astatus,
1495 struct vrequest *areq)
1499 struct AFSFetchStatus OutStatus;
1500 struct AFSVolSync tsync;
1502 AFS_STATCNT(afs_WriteVCache);
1503 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1504 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
1507 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
1509 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1512 RXAFS_StoreStatus(tc->id, (struct AFSFid *)&avc->fid.Fid,
1513 astatus, &OutStatus, &tsync);
1518 } while (afs_Analyze
1519 (tc, code, &avc->fid, areq, AFS_STATS_FS_RPCIDX_STORESTATUS,
1520 SHARED_LOCK, NULL));
1522 UpgradeSToWLock(&avc->lock, 20);
1524 /* success, do the changes locally */
1525 afs_SimpleVStat(avc, &OutStatus, areq);
1527 * Update the date, too. SimpleVStat didn't do this, since
1528 * it thought we were doing this after fetching new status
1529 * over a file being written.
1531 avc->m.Date = OutStatus.ClientModTime;
1533 /* failure, set up to check with server next time */
1534 ObtainWriteLock(&afs_xcbhash, 462);
1535 afs_DequeueCallback(avc);
1536 avc->states &= ~(CStatd | CUnique); /* turn off stat valid flag */
1537 ReleaseWriteLock(&afs_xcbhash);
1538 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1539 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
1541 ConvertWToSLock(&avc->lock);
1544 } /*afs_WriteVCache */
1550 * Copy astat block into vcache info
1553 * avc : Ptr to vcache entry.
1554 * astat : Ptr to stat block to copy in.
1555 * areq : Ptr to associated request.
1558 * Must be called under a write lock
1560 * Note: this code may get dataversion and length out of sync if the file has
1561 * been modified. This is less than ideal. I haven't thought about
1562 * it sufficiently to be certain that it is adequate.
1565 afs_ProcessFS(register struct vcache *avc,
1566 register struct AFSFetchStatus *astat, struct vrequest *areq)
1569 AFS_STATCNT(afs_ProcessFS);
1571 #ifdef AFS_64BIT_CLIENT
1572 FillInt64(length, astat->Length_hi, astat->Length);
1573 #else /* AFS_64BIT_CLIENT */
1574 length = astat->Length;
1575 #endif /* AFS_64BIT_CLIENT */
1576 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1577 * number for each bulk status request. Under no circumstances
1578 * should afs_DoBulkStat store a sequence number if the new
1579 * length will be ignored when afs_ProcessFS is called with
1580 * new stats. If you change the following conditional then you
1581 * also need to change the conditional in afs_DoBulkStat. */
1583 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1584 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1586 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1588 /* if we're writing or mapping this file, don't fetch over these
1591 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1592 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1593 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1594 avc->m.Length = length;
1595 avc->m.Date = astat->ClientModTime;
1597 hset64(avc->m.DataVersion, astat->dataVersionHigh, astat->DataVersion);
1598 avc->m.Owner = astat->Owner;
1599 avc->m.Mode = astat->UnixModeBits;
1600 avc->m.Group = astat->Group;
1601 avc->m.LinkCount = astat->LinkCount;
1602 if (astat->FileType == File) {
1603 vSetType(avc, VREG);
1604 avc->m.Mode |= S_IFREG;
1605 } else if (astat->FileType == Directory) {
1606 vSetType(avc, VDIR);
1607 avc->m.Mode |= S_IFDIR;
1608 } else if (astat->FileType == SymbolicLink) {
1609 if (afs_fakestat_enable && (avc->m.Mode & 0111) == 0) {
1610 vSetType(avc, VDIR);
1611 avc->m.Mode |= S_IFDIR;
1613 vSetType(avc, VLNK);
1614 avc->m.Mode |= S_IFLNK;
1616 if ((avc->m.Mode & 0111) == 0) {
1620 avc->anyAccess = astat->AnonymousAccess;
1622 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1624 * Caller has at least one bit not covered by anonymous, and
1625 * thus may have interesting rights.
1627 * HOWEVER, this is a really bad idea, because any access query
1628 * for bits which aren't covered by anonymous, on behalf of a user
1629 * who doesn't have any special rights, will result in an answer of
1630 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1631 * It's an especially bad idea under Ultrix, since (due to the lack of
1632 * a proper access() call) it must perform several afs_access() calls
1633 * in order to create magic mode bits that vary according to who makes
1634 * the call. In other words, _every_ stat() generates a test for
1637 #endif /* badidea */
1639 struct axscache *ac;
1640 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1641 ac->axess = astat->CallerAccess;
1642 else /* not found, add a new one if possible */
1643 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1645 } /*afs_ProcessFS */
1649 afs_RemoteLookup(register struct VenusFid *afid, struct vrequest *areq,
1650 char *name, struct VenusFid *nfid,
1651 struct AFSFetchStatus *OutStatusp,
1652 struct AFSCallBack *CallBackp, struct server **serverp,
1653 struct AFSVolSync *tsyncp)
1657 register struct conn *tc;
1658 struct AFSFetchStatus OutDirStatus;
1661 name = ""; /* XXX */
1663 tc = afs_Conn(afid, areq, SHARED_LOCK);
1666 *serverp = tc->srvr->server;
1668 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1671 RXAFS_Lookup(tc->id, (struct AFSFid *)&afid->Fid, name,
1672 (struct AFSFid *)&nfid->Fid, OutStatusp,
1673 &OutDirStatus, CallBackp, tsyncp);
1678 } while (afs_Analyze
1679 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_XLOOKUP, SHARED_LOCK,
1690 * Given a file id and a vrequest structure, fetch the status
1691 * information associated with the file.
1695 * areq : Ptr to associated vrequest structure, specifying the
1696 * user whose authentication tokens will be used.
1697 * avc : caller may already have a vcache for this file, which is
1701 * The cache entry is returned with an increased vrefCount field.
1702 * The entry must be discarded by calling afs_PutVCache when you
1703 * are through using the pointer to the cache entry.
1705 * You should not hold any locks when calling this function, except
1706 * locks on other vcache entries. If you lock more than one vcache
1707 * entry simultaneously, you should lock them in this order:
1709 * 1. Lock all files first, then directories.
1710 * 2. Within a particular type, lock entries in Fid.Vnode order.
1712 * This locking hierarchy is convenient because it allows locking
1713 * of a parent dir cache entry, given a file (to check its access
1714 * control list). It also allows renames to be handled easily by
1715 * locking directories in a constant order.
1716 * NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1718 /* might have a vcache structure already, which must
1719 * already be held by the caller */
1722 afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
1723 afs_int32 * cached, struct vcache *avc)
1726 afs_int32 code, newvcache = 0;
1727 register struct vcache *tvc;
1731 AFS_STATCNT(afs_GetVCache);
1734 *cached = 0; /* Init just in case */
1736 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1740 ObtainSharedLock(&afs_xvcache, 5);
1742 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU | IS_SLOCK);
1744 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1745 ReleaseSharedLock(&afs_xvcache);
1746 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1754 osi_Assert((tvc->states & CVInit) == 0);
1755 /* If we are in readdir, return the vnode even if not statd */
1756 if ((tvc->states & CStatd) || afs_InReadDir(tvc)) {
1757 ReleaseSharedLock(&afs_xvcache);
1761 UpgradeSToWLock(&afs_xvcache, 21);
1763 /* no cache entry, better grab one */
1764 tvc = afs_NewVCache(afid, NULL);
1767 ConvertWToSLock(&afs_xvcache);
1770 ReleaseSharedLock(&afs_xvcache);
1774 afs_stats_cmperf.vcacheMisses++;
1777 ReleaseSharedLock(&afs_xvcache);
1779 ObtainWriteLock(&tvc->lock, 54);
1781 if (tvc->states & CStatd) {
1782 ReleaseWriteLock(&tvc->lock);
1785 #if defined(AFS_OSF_ENV)
1786 if (afs_IsWired(tvc)) {
1787 ReleaseWriteLock(&tvc->lock);
1790 #endif /* AFS_OSF_ENV */
1791 #ifdef AFS_DARWIN80_ENV
1792 /* Darwin 8.0 only has bufs in nfs, so we shouldn't have to worry about them.
1795 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
1797 * XXX - I really don't like this. Should try to understand better.
1798 * It seems that sometimes, when we get called, we already hold the
1799 * lock on the vnode (e.g., from afs_getattr via afs_VerifyVCache).
1800 * We can't drop the vnode lock, because that could result in a race.
1801 * Sometimes, though, we get here and don't hold the vnode lock.
1802 * I hate code paths that sometimes hold locks and sometimes don't.
1803 * In any event, the dodge we use here is to check whether the vnode
1804 * is locked, and if it isn't, then we gain and drop it around the call
1805 * to vinvalbuf; otherwise, we leave it alone.
1808 struct vnode *vp = AFSTOV(tvc);
1811 #if defined(AFS_DARWIN_ENV)
1812 iheldthelock = VOP_ISLOCKED(vp);
1814 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, current_proc());
1815 /* this is messy. we can call fsync which will try to reobtain this */
1816 if (VTOAFS(vp) == tvc)
1817 ReleaseWriteLock(&tvc->lock);
1818 if (UBCINFOEXISTS(vp)) {
1819 vinvalbuf(vp, V_SAVE, &afs_osi_cred, current_proc(), PINOD, 0);
1821 if (VTOAFS(vp) == tvc)
1822 ObtainWriteLock(&tvc->lock, 954);
1824 VOP_UNLOCK(vp, LK_EXCLUSIVE, current_proc());
1825 #elif defined(AFS_FBSD60_ENV)
1826 iheldthelock = VOP_ISLOCKED(vp, curthread);
1828 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1829 vinvalbuf(vp, V_SAVE, curthread, PINOD, 0);
1831 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1832 #elif defined(AFS_FBSD50_ENV)
1833 iheldthelock = VOP_ISLOCKED(vp, curthread);
1835 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1836 vinvalbuf(vp, V_SAVE, osi_curcred(), curthread, PINOD, 0);
1838 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1839 #elif defined(AFS_FBSD40_ENV)
1840 iheldthelock = VOP_ISLOCKED(vp, curproc);
1842 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1843 vinvalbuf(vp, V_SAVE, osi_curcred(), curproc, PINOD, 0);
1845 VOP_UNLOCK(vp, LK_EXCLUSIVE, curproc);
1846 #elif defined(AFS_OBSD_ENV)
1847 iheldthelock = VOP_ISLOCKED(vp, curproc);
1849 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1850 uvm_vnp_uncache(vp);
1852 VOP_UNLOCK(vp, 0, curproc);
1858 ObtainWriteLock(&afs_xcbhash, 464);
1859 tvc->states &= ~CUnique;
1861 afs_DequeueCallback(tvc);
1862 ReleaseWriteLock(&afs_xcbhash);
1864 /* It is always appropriate to throw away all the access rights? */
1865 afs_FreeAllAxs(&(tvc->Access));
1866 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1868 if ((tvp->states & VForeign)) {
1870 tvc->states |= CForeign;
1871 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1872 && (tvp->rootUnique == afid->Fid.Unique)) {
1876 if (tvp->states & VRO)
1878 if (tvp->states & VBackup)
1879 tvc->states |= CBackup;
1880 /* now copy ".." entry back out of volume structure, if necessary */
1881 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1883 tvc->mvid = (struct VenusFid *)
1884 osi_AllocSmallSpace(sizeof(struct VenusFid));
1885 *tvc->mvid = tvp->dotdot;
1887 afs_PutVolume(tvp, READ_LOCK);
1891 afs_RemoveVCB(afid);
1893 struct AFSFetchStatus OutStatus;
1895 if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1896 afs_ProcessFS(tvc, &OutStatus, areq);
1897 tvc->states |= CStatd | CUnique;
1898 tvc->parentVnode = OutStatus.ParentVnode;
1899 tvc->parentUnique = OutStatus.ParentUnique;
1902 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1903 /* For the NFS translator's benefit, make sure
1904 * non-directory vnodes always have their parent FID set
1905 * correctly, even when created as a result of decoding an
1906 * NFS filehandle. It would be nice to also do this for
1907 * directories, but we can't because the fileserver fills
1908 * in the FID of the directory itself instead of that of
1911 if (!code && OutStatus.FileType != Directory &&
1912 !tvc->parentVnode) {
1913 tvc->parentVnode = OutStatus.ParentVnode;
1914 tvc->parentUnique = OutStatus.ParentUnique;
1920 ReleaseWriteLock(&tvc->lock);
1926 ReleaseWriteLock(&tvc->lock);
1929 } /*afs_GetVCache */
1934 afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1935 afs_int32 * cached, struct vcache *adp, char *aname)
1937 afs_int32 code, now, newvcache = 0;
1938 struct VenusFid nfid;
1939 register struct vcache *tvc;
1941 struct AFSFetchStatus OutStatus;
1942 struct AFSCallBack CallBack;
1943 struct AFSVolSync tsync;
1944 struct server *serverp = 0;
1948 AFS_STATCNT(afs_GetVCache);
1950 *cached = 0; /* Init just in case */
1952 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1956 ObtainReadLock(&afs_xvcache);
1957 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */ );
1960 ReleaseReadLock(&afs_xvcache);
1962 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1963 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1967 ObtainReadLock(&tvc->lock);
1969 if (tvc->states & CStatd) {
1973 ReleaseReadLock(&tvc->lock);
1976 tvc->states &= ~CUnique;
1978 ReleaseReadLock(&tvc->lock);
1980 ObtainReadLock(&afs_xvcache);
1983 ReleaseReadLock(&afs_xvcache);
1985 /* lookup the file */
1988 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
1990 afs_RemoteLookup(&adp->fid, areq, aname, &nfid, &OutStatus, &CallBack,
1993 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1997 ObtainSharedLock(&afs_xvcache, 6);
1998 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU | IS_SLOCK/* no xstats now */ );
2000 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2001 ReleaseSharedLock(&afs_xvcache);
2002 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2008 /* no cache entry, better grab one */
2009 UpgradeSToWLock(&afs_xvcache, 22);
2010 tvc = afs_NewVCache(&nfid, serverp);
2012 ConvertWToSLock(&afs_xvcache);
2015 ReleaseSharedLock(&afs_xvcache);
2020 ReleaseSharedLock(&afs_xvcache);
2021 ObtainWriteLock(&tvc->lock, 55);
2023 /* It is always appropriate to throw away all the access rights? */
2024 afs_FreeAllAxs(&(tvc->Access));
2025 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
2027 if ((tvp->states & VForeign)) {
2029 tvc->states |= CForeign;
2030 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
2031 && (tvp->rootUnique == afid->Fid.Unique))
2034 if (tvp->states & VRO)
2036 if (tvp->states & VBackup)
2037 tvc->states |= CBackup;
2038 /* now copy ".." entry back out of volume structure, if necessary */
2039 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2041 tvc->mvid = (struct VenusFid *)
2042 osi_AllocSmallSpace(sizeof(struct VenusFid));
2043 *tvc->mvid = tvp->dotdot;
2048 ObtainWriteLock(&afs_xcbhash, 465);
2049 afs_DequeueCallback(tvc);
2050 tvc->states &= ~(CStatd | CUnique);
2051 ReleaseWriteLock(&afs_xcbhash);
2052 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2053 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2055 afs_PutVolume(tvp, READ_LOCK);
2056 ReleaseWriteLock(&tvc->lock);
2061 ObtainWriteLock(&afs_xcbhash, 466);
2062 if (origCBs == afs_allCBs) {
2063 if (CallBack.ExpirationTime) {
2064 tvc->callback = serverp;
2065 tvc->cbExpires = CallBack.ExpirationTime + now;
2066 tvc->states |= CStatd | CUnique;
2067 tvc->states &= ~CBulkFetching;
2068 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
2069 } else if (tvc->states & CRO) {
2070 /* adapt gives us an hour. */
2071 tvc->cbExpires = 3600 + osi_Time();
2072 /*XXX*/ tvc->states |= CStatd | CUnique;
2073 tvc->states &= ~CBulkFetching;
2074 afs_QueueCallback(tvc, CBHash(3600), tvp);
2076 tvc->callback = NULL;
2077 afs_DequeueCallback(tvc);
2078 tvc->states &= ~(CStatd | CUnique);
2079 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2080 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2083 afs_DequeueCallback(tvc);
2084 tvc->states &= ~CStatd;
2085 tvc->states &= ~CUnique;
2086 tvc->callback = NULL;
2087 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2088 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2090 ReleaseWriteLock(&afs_xcbhash);
2092 afs_PutVolume(tvp, READ_LOCK);
2093 afs_ProcessFS(tvc, &OutStatus, areq);
2095 ReleaseWriteLock(&tvc->lock);
2101 afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
2102 afs_int32 * cached, struct volume *tvolp)
2104 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
2105 afs_int32 getNewFid = 0;
2107 struct VenusFid nfid;
2108 register struct vcache *tvc;
2109 struct server *serverp = 0;
2110 struct AFSFetchStatus OutStatus;
2111 struct AFSCallBack CallBack;
2112 struct AFSVolSync tsync;
2117 #ifdef AFS_DARWIN80_ENV
2124 if (!tvolp->rootVnode || getNewFid) {
2125 struct VenusFid tfid;
2128 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2129 origCBs = afs_allCBs; /* ignore InitCallBackState */
2131 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2136 /* ReleaseReadLock(&tvolp->lock); */
2137 ObtainWriteLock(&tvolp->lock, 56);
2138 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
2139 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
2140 ReleaseWriteLock(&tvolp->lock);
2141 /* ObtainReadLock(&tvolp->lock);*/
2144 afid->Fid.Vnode = tvolp->rootVnode;
2145 afid->Fid.Unique = tvolp->rootUnique;
2149 ObtainSharedLock(&afs_xvcache, 7);
2151 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2152 if (!FidCmp(&(tvc->fid), afid)) {
2153 if (tvc->states & CVInit) {
2154 ReleaseSharedLock(&afs_xvcache);
2155 afs_osi_Sleep(&tvc->states);
2159 /* Grab this vnode, possibly reactivating from the free list */
2160 /* for the present (95.05.25) everything on the hash table is
2161 * definitively NOT in the free list -- at least until afs_reclaim
2162 * can be safely implemented */
2164 vg = vget(AFSTOV(tvc)); /* this bumps ref count */
2168 #endif /* AFS_OSF_ENV */
2169 #ifdef AFS_DARWIN80_ENV
2170 if (tvc->states & CDeadVnode) {
2171 ReleaseSharedLock(&afs_xvcache);
2172 afs_osi_Sleep(&tvc->states);
2176 if (vnode_get(tvp)) /* this bumps ref count */
2178 if (vnode_ref(tvp)) {
2180 /* AFSTOV(tvc) may be NULL */
2190 if (!haveStatus && (!tvc || !(tvc->states & CStatd))) {
2191 /* Mount point no longer stat'd or unknown. FID may have changed. */
2194 AFS_RELE(AFSTOV(tvc));
2197 ReleaseSharedLock(&afs_xvcache);
2198 #ifdef AFS_DARWIN80_ENV
2201 vnode_put(AFSTOV(tvc));
2202 vnode_rele(AFSTOV(tvc));
2211 UpgradeSToWLock(&afs_xvcache, 23);
2212 /* no cache entry, better grab one */
2213 tvc = afs_NewVCache(afid, NULL);
2216 ReleaseWriteLock(&afs_xvcache);
2220 afs_stats_cmperf.vcacheMisses++;
2224 afs_stats_cmperf.vcacheHits++;
2225 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN80_ENV)
2226 /* we already bumped the ref count in the for loop above */
2227 #else /* AFS_OSF_ENV */
2230 UpgradeSToWLock(&afs_xvcache, 24);
2231 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2232 refpanic("GRVC VLRU inconsistent0");
2234 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2235 refpanic("GRVC VLRU inconsistent1");
2237 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2238 refpanic("GRVC VLRU inconsistent2");
2240 QRemove(&tvc->vlruq); /* move to lruq head */
2241 QAdd(&VLRU, &tvc->vlruq);
2242 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2243 refpanic("GRVC VLRU inconsistent3");
2245 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2246 refpanic("GRVC VLRU inconsistent4");
2248 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2249 refpanic("GRVC VLRU inconsistent5");
2254 ReleaseWriteLock(&afs_xvcache);
2256 if (tvc->states & CStatd) {
2260 ObtainReadLock(&tvc->lock);
2261 tvc->states &= ~CUnique;
2262 tvc->callback = NULL; /* redundant, perhaps */
2263 ReleaseReadLock(&tvc->lock);
2266 ObtainWriteLock(&tvc->lock, 57);
2268 /* It is always appropriate to throw away all the access rights? */
2269 afs_FreeAllAxs(&(tvc->Access));
2272 tvc->states |= CForeign;
2273 if (tvolp->states & VRO)
2275 if (tvolp->states & VBackup)
2276 tvc->states |= CBackup;
2277 /* now copy ".." entry back out of volume structure, if necessary */
2278 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2279 && (tvolp->rootUnique == afid->Fid.Unique)) {
2282 if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2284 tvc->mvid = (struct VenusFid *)
2285 osi_AllocSmallSpace(sizeof(struct VenusFid));
2286 *tvc->mvid = tvolp->dotdot;
2290 afs_RemoveVCB(afid);
2293 struct VenusFid tfid;
2296 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2297 origCBs = afs_allCBs; /* ignore InitCallBackState */
2299 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2304 ObtainWriteLock(&afs_xcbhash, 467);
2305 afs_DequeueCallback(tvc);
2306 tvc->callback = NULL;
2307 tvc->states &= ~(CStatd | CUnique);
2308 ReleaseWriteLock(&afs_xcbhash);
2309 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2310 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2311 ReleaseWriteLock(&tvc->lock);
2316 ObtainWriteLock(&afs_xcbhash, 468);
2317 if (origCBs == afs_allCBs) {
2318 tvc->states |= CTruth;
2319 tvc->callback = serverp;
2320 if (CallBack.ExpirationTime != 0) {
2321 tvc->cbExpires = CallBack.ExpirationTime + start;
2322 tvc->states |= CStatd;
2323 tvc->states &= ~CBulkFetching;
2324 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2325 } else if (tvc->states & CRO) {
2326 /* adapt gives us an hour. */
2327 tvc->cbExpires = 3600 + osi_Time();
2328 /*XXX*/ tvc->states |= CStatd;
2329 tvc->states &= ~CBulkFetching;
2330 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2333 afs_DequeueCallback(tvc);
2334 tvc->callback = NULL;
2335 tvc->states &= ~(CStatd | CUnique);
2336 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2337 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2339 ReleaseWriteLock(&afs_xcbhash);
2340 afs_ProcessFS(tvc, &OutStatus, areq);
2342 ReleaseWriteLock(&tvc->lock);
2349 * must be called with avc write-locked
2350 * don't absolutely have to invalidate the hint unless the dv has
2351 * changed, but be sure to get it right else there will be consistency bugs.
2354 afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
2355 struct vrequest * areq, struct AFSFetchStatus * Outsp)
2358 afs_uint32 start = 0;
2359 register struct conn *tc;
2360 struct AFSCallBack CallBack;
2361 struct AFSVolSync tsync;
2362 struct volume *volp;
2365 tc = afs_Conn(afid, areq, SHARED_LOCK);
2366 avc->dchint = NULL; /* invalidate hints */
2368 avc->callback = tc->srvr->server;
2370 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2373 RXAFS_FetchStatus(tc->id, (struct AFSFid *)&afid->Fid, Outsp,
2381 } while (afs_Analyze
2382 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2383 SHARED_LOCK, NULL));
2386 afs_ProcessFS(avc, Outsp, areq);
2387 volp = afs_GetVolume(afid, areq, READ_LOCK);
2388 ObtainWriteLock(&afs_xcbhash, 469);
2389 avc->states |= CTruth;
2390 if (avc->callback /* check for race */ ) {
2391 if (CallBack.ExpirationTime != 0) {
2392 avc->cbExpires = CallBack.ExpirationTime + start;
2393 avc->states |= CStatd;
2394 avc->states &= ~CBulkFetching;
2395 afs_QueueCallback(avc, CBHash(CallBack.ExpirationTime), volp);
2396 } else if (avc->states & CRO) { /* ordinary callback on a read-only volume -- AFS 3.2 style */
2397 avc->cbExpires = 3600 + start;
2398 avc->states |= CStatd;
2399 avc->states &= ~CBulkFetching;
2400 afs_QueueCallback(avc, CBHash(3600), volp);
2402 afs_DequeueCallback(avc);
2403 avc->callback = NULL;
2404 avc->states &= ~(CStatd | CUnique);
2405 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2406 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2409 afs_DequeueCallback(avc);
2410 avc->callback = NULL;
2411 avc->states &= ~(CStatd | CUnique);
2412 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2413 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2415 ReleaseWriteLock(&afs_xcbhash);
2417 afs_PutVolume(volp, READ_LOCK);
2419 /* used to undo the local callback, but that's too extreme.
2420 * There are plenty of good reasons that fetchstatus might return
2421 * an error, such as EPERM. If we have the vnode cached, statd,
2422 * with callback, might as well keep track of the fact that we
2423 * don't have access...
2425 if (code == EPERM || code == EACCES) {
2426 struct axscache *ac;
2427 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2429 else /* not found, add a new one if possible */
2430 afs_AddAxs(avc->Access, areq->uid, 0);
2441 * Stuff some information into the vcache for the given file.
2444 * afid : File in question.
2445 * OutStatus : Fetch status on the file.
2446 * CallBack : Callback info.
2447 * tc : RPC connection involved.
2448 * areq : vrequest involved.
2451 * Nothing interesting.
2454 afs_StuffVcache(register struct VenusFid *afid,
2455 struct AFSFetchStatus *OutStatus,
2456 struct AFSCallBack *CallBack, register struct conn *tc,
2457 struct vrequest *areq)
2459 register afs_int32 code, i, newvcache = 0;
2460 register struct vcache *tvc;
2461 struct AFSVolSync tsync;
2463 struct axscache *ac;
2466 AFS_STATCNT(afs_StuffVcache);
2467 #ifdef IFS_VCACHECOUNT
2472 ObtainSharedLock(&afs_xvcache, 8);
2474 tvc = afs_FindVCache(afid, &retry, DO_VLRU| IS_SLOCK /* no stats */ );
2476 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2477 ReleaseSharedLock(&afs_xvcache);
2478 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2484 /* no cache entry, better grab one */
2485 UpgradeSToWLock(&afs_xvcache, 25);
2486 tvc = afs_NewVCache(afid, NULL);
2488 ConvertWToSLock(&afs_xvcache);
2491 ReleaseSharedLock(&afs_xvcache);
2496 ReleaseSharedLock(&afs_xvcache);
2497 ObtainWriteLock(&tvc->lock, 58);
2499 tvc->states &= ~CStatd;
2500 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2501 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2503 /* Is it always appropriate to throw away all the access rights? */
2504 afs_FreeAllAxs(&(tvc->Access));
2506 /*Copy useful per-volume info */
2507 tvp = afs_GetVolume(afid, areq, READ_LOCK);
2509 if (newvcache && (tvp->states & VForeign))
2510 tvc->states |= CForeign;
2511 if (tvp->states & VRO)
2513 if (tvp->states & VBackup)
2514 tvc->states |= CBackup;
2516 * Now, copy ".." entry back out of volume structure, if
2519 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2521 tvc->mvid = (struct VenusFid *)
2522 osi_AllocSmallSpace(sizeof(struct VenusFid));
2523 *tvc->mvid = tvp->dotdot;
2526 /* store the stat on the file */
2527 afs_RemoveVCB(afid);
2528 afs_ProcessFS(tvc, OutStatus, areq);
2529 tvc->callback = tc->srvr->server;
2531 /* we use osi_Time twice below. Ideally, we would use the time at which
2532 * the FetchStatus call began, instead, but we don't have it here. So we
2533 * make do with "now". In the CRO case, it doesn't really matter. In
2534 * the other case, we hope that the difference between "now" and when the
2535 * call actually began execution on the server won't be larger than the
2536 * padding which the server keeps. Subtract 1 second anyway, to be on
2537 * the safe side. Can't subtract more because we don't know how big
2538 * ExpirationTime is. Possible consistency problems may arise if the call
2539 * timeout period becomes longer than the server's expiration padding. */
2540 ObtainWriteLock(&afs_xcbhash, 470);
2541 if (CallBack->ExpirationTime != 0) {
2542 tvc->cbExpires = CallBack->ExpirationTime + osi_Time() - 1;
2543 tvc->states |= CStatd;
2544 tvc->states &= ~CBulkFetching;
2545 afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2546 } else if (tvc->states & CRO) {
2547 /* old-fashioned AFS 3.2 style */
2548 tvc->cbExpires = 3600 + osi_Time();
2549 /*XXX*/ tvc->states |= CStatd;
2550 tvc->states &= ~CBulkFetching;
2551 afs_QueueCallback(tvc, CBHash(3600), tvp);
2553 afs_DequeueCallback(tvc);
2554 tvc->callback = NULL;
2555 tvc->states &= ~(CStatd | CUnique);
2556 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2557 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2559 ReleaseWriteLock(&afs_xcbhash);
2561 afs_PutVolume(tvp, READ_LOCK);
2563 /* look in per-pag cache */
2564 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2565 ac->axess = OutStatus->CallerAccess; /* substitute pags */
2566 else /* not found, add a new one if possible */
2567 afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2569 ReleaseWriteLock(&tvc->lock);
2570 afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2571 ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32,
2572 tvc->cbExpires, ICL_TYPE_INT32, tvc->cbExpires - osi_Time());
2574 * Release ref count... hope this guy stays around...
2577 } /*afs_StuffVcache */
2584 * Decrements the reference count on a cache entry.
2587 * avc : Pointer to the cache entry to decrement.
2590 * Nothing interesting.
2593 afs_PutVCache(register struct vcache *avc)
2595 AFS_STATCNT(afs_PutVCache);
2596 #ifdef AFS_DARWIN80_ENV
2597 vnode_put(AFSTOV(avc));
2601 * Can we use a read lock here?
2603 ObtainReadLock(&afs_xvcache);
2605 ReleaseReadLock(&afs_xvcache);
2607 } /*afs_PutVCache */
2610 static void findvc_sleep(struct vcache *avc, int flag) {
2611 if (flag & IS_SLOCK) {
2612 ReleaseSharedLock(&afs_xvcache);
2614 if (flag & IS_WLOCK) {
2615 ReleaseWriteLock(&afs_xvcache);
2617 ReleaseReadLock(&afs_xvcache);
2620 afs_osi_Sleep(&avc->states);
2621 if (flag & IS_SLOCK) {
2622 ObtainSharedLock(&afs_xvcache, 341);
2624 if (flag & IS_WLOCK) {
2625 ObtainWriteLock(&afs_xvcache, 343);
2627 ObtainReadLock(&afs_xvcache);
2635 * Find a vcache entry given a fid.
2638 * afid : Pointer to the fid whose cache entry we desire.
2639 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2640 * unlock the vnode, and try again.
2641 * flags: bit 1 to specify whether to compute hit statistics. Not
2642 * set if FindVCache is called as part of internal bookkeeping.
2645 * Must be called with the afs_xvcache lock at least held at
2646 * the read level. In order to do the VLRU adjustment, the xvcache lock
2647 * must be shared-- we upgrade it here.
2651 afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
2654 register struct vcache *tvc;
2656 #if defined( AFS_OSF_ENV)
2659 #ifdef AFS_DARWIN80_ENV
2663 AFS_STATCNT(afs_FindVCache);
2667 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2668 if (FidMatches(afid, tvc)) {
2669 if (tvc->states & CVInit) {
2670 findvc_sleep(tvc, flag);
2674 /* Grab this vnode, possibly reactivating from the free list */
2676 vg = vget(AFSTOV(tvc));
2680 #endif /* AFS_OSF_ENV */
2681 #ifdef AFS_DARWIN80_ENV
2682 if (tvc->states & CDeadVnode) {
2683 findvc_sleep(tvc, flag);
2689 if (vnode_ref(tvp)) {
2691 /* AFSTOV(tvc) may be NULL */
2701 /* should I have a read lock on the vnode here? */
2705 #if !defined(AFS_OSF_ENV) && !defined(AFS_DARWIN80_ENV)
2706 osi_vnhold(tvc, retry); /* already held, above */
2707 if (retry && *retry)
2710 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
2711 tvc->states |= CUBCinit;
2713 if (UBCINFOMISSING(AFSTOV(tvc)) ||
2714 UBCINFORECLAIMED(AFSTOV(tvc))) {
2715 ubc_info_init(AFSTOV(tvc));
2718 tvc->states &= ~CUBCinit;
2721 * only move to front of vlru if we have proper vcache locking)
2723 if (flag & DO_VLRU) {
2724 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2725 refpanic("FindVC VLRU inconsistent1");
2727 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2728 refpanic("FindVC VLRU inconsistent1");
2730 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2731 refpanic("FindVC VLRU inconsistent2");
2733 UpgradeSToWLock(&afs_xvcache, 26);
2734 QRemove(&tvc->vlruq);
2735 QAdd(&VLRU, &tvc->vlruq);
2736 ConvertWToSLock(&afs_xvcache);
2737 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2738 refpanic("FindVC VLRU inconsistent1");
2740 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2741 refpanic("FindVC VLRU inconsistent2");
2743 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2744 refpanic("FindVC VLRU inconsistent3");
2750 if (flag & DO_STATS) {
2752 afs_stats_cmperf.vcacheHits++;
2754 afs_stats_cmperf.vcacheMisses++;
2755 if (afs_IsPrimaryCellNum(afid->Cell))
2756 afs_stats_cmperf.vlocalAccesses++;
2758 afs_stats_cmperf.vremoteAccesses++;
2761 } /*afs_FindVCache */
2767 * Find a vcache entry given a fid. Does a wildcard match on what we
2768 * have for the fid. If more than one entry, don't return anything.
2771 * avcp : Fill in pointer if we found one and only one.
2772 * afid : Pointer to the fid whose cache entry we desire.
2773 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2774 * unlock the vnode, and try again.
2775 * flags: bit 1 to specify whether to compute hit statistics. Not
2776 * set if FindVCache is called as part of internal bookkeeping.
2779 * Must be called with the afs_xvcache lock at least held at
2780 * the read level. In order to do the VLRU adjustment, the xvcache lock
2781 * must be shared-- we upgrade it here.
2784 * number of matches found.
2787 int afs_duplicate_nfs_fids = 0;
2790 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2792 register struct vcache *tvc;
2794 afs_int32 count = 0;
2795 struct vcache *found_tvc = NULL;
2799 #ifdef AFS_DARWIN80_ENV
2803 AFS_STATCNT(afs_FindVCache);
2807 ObtainSharedLock(&afs_xvcache, 331);
2810 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2811 /* Match only on what we have.... */
2812 if (((tvc->fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2813 && (tvc->fid.Fid.Volume == afid->Fid.Volume)
2814 && ((tvc->fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2815 && (tvc->fid.Cell == afid->Cell)) {
2816 if (tvc->states & CVInit) {
2817 ReleaseSharedLock(&afs_xvcache);
2818 afs_osi_Sleep(&tvc->states);
2822 /* Grab this vnode, possibly reactivating from the free list */
2824 vg = vget(AFSTOV(tvc));
2827 /* This vnode no longer exists. */
2830 #endif /* AFS_OSF_ENV */
2831 #ifdef AFS_DARWIN80_ENV
2832 if (tvc->states & CDeadVnode) {
2833 ReleaseSharedLock(&afs_xvcache);
2834 afs_osi_Sleep(&tvc->states);
2838 if (vnode_get(tvp)) {
2839 /* This vnode no longer exists. */
2842 if (vnode_ref(tvp)) {
2843 /* This vnode no longer exists. */
2845 /* AFSTOV(tvc) may be NULL */
2850 #endif /* AFS_DARWIN80_ENV */
2855 /* Drop our reference counts. */
2857 vrele(AFSTOV(found_tvc));
2859 afs_duplicate_nfs_fids++;
2860 ReleaseSharedLock(&afs_xvcache);
2861 #ifdef AFS_DARWIN80_ENV
2862 /* Drop our reference counts. */
2863 vnode_put(AFSTOV(tvc));
2864 vnode_put(AFSTOV(found_tvc));
2873 /* should I have a read lock on the vnode here? */
2875 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2876 afs_int32 retry = 0;
2877 osi_vnhold(tvc, &retry);
2880 found_tvc = (struct vcache *)0;
2881 ReleaseSharedLock(&afs_xvcache);
2882 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2886 #if !defined(AFS_OSF_ENV)
2887 osi_vnhold(tvc, (int *)0); /* already held, above */
2891 * We obtained the xvcache lock above.
2893 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2894 refpanic("FindVC VLRU inconsistent1");
2896 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2897 refpanic("FindVC VLRU inconsistent1");
2899 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2900 refpanic("FindVC VLRU inconsistent2");
2902 UpgradeSToWLock(&afs_xvcache, 568);
2903 QRemove(&tvc->vlruq);
2904 QAdd(&VLRU, &tvc->vlruq);
2905 ConvertWToSLock(&afs_xvcache);
2906 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2907 refpanic("FindVC VLRU inconsistent1");
2909 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2910 refpanic("FindVC VLRU inconsistent2");
2912 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2913 refpanic("FindVC VLRU inconsistent3");
2919 afs_stats_cmperf.vcacheHits++;
2921 afs_stats_cmperf.vcacheMisses++;
2922 if (afs_IsPrimaryCellNum(afid->Cell))
2923 afs_stats_cmperf.vlocalAccesses++;
2925 afs_stats_cmperf.vremoteAccesses++;
2927 *avcp = tvc; /* May be null */
2929 ReleaseSharedLock(&afs_xvcache);
2930 return (tvc ? 1 : 0);
2932 } /*afs_NFSFindVCache */
2940 * Initialize vcache related variables
2943 afs_vcacheInit(int astatSize)
2945 register struct vcache *tvp;
2947 #if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
2948 if (!afs_maxvcount) {
2949 #if defined(AFS_LINUX22_ENV)
2950 afs_maxvcount = astatSize; /* no particular limit on linux? */
2951 #elif defined(AFS_OSF30_ENV)
2952 afs_maxvcount = max_vnodes / 2; /* limit ourselves to half the total */
2954 afs_maxvcount = nvnode / 2; /* limit ourselves to half the total */
2956 if (astatSize < afs_maxvcount) {
2957 afs_maxvcount = astatSize;
2960 #else /* AFS_OSF_ENV */
2964 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2965 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2967 #if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
2968 /* Allocate and thread the struct vcache entries */
2969 tvp = (struct vcache *)afs_osi_Alloc(astatSize * sizeof(struct vcache));
2970 memset((char *)tvp, 0, sizeof(struct vcache) * astatSize);
2972 Initial_freeVCList = tvp;
2973 freeVCList = &(tvp[0]);
2974 for (i = 0; i < astatSize - 1; i++) {
2975 tvp[i].nextfree = &(tvp[i + 1]);
2977 tvp[astatSize - 1].nextfree = NULL;
2978 #ifdef KERNEL_HAVE_PIN
2979 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
2983 #if defined(AFS_SGI_ENV)
2984 for (i = 0; i < astatSize; i++) {
2985 char name[METER_NAMSZ];
2986 struct vcache *tvc = &tvp[i];
2988 tvc->v.v_number = ++afsvnumbers;
2989 tvc->vc_rwlockid = OSI_NO_LOCKID;
2990 initnsema(&tvc->vc_rwlock, 1,
2991 makesname(name, "vrw", tvc->v.v_number));
2992 #ifndef AFS_SGI53_ENV
2993 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
2995 #ifndef AFS_SGI62_ENV
2996 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
2997 #endif /* AFS_SGI62_ENV */
3001 for(i = 0; i < VCSIZE; ++i)
3002 QInit(&afs_vhashTV[i]);
3010 shutdown_vcache(void)
3013 struct afs_cbr *tsp, *nsp;
3015 * XXX We may potentially miss some of the vcaches because if when there're no
3016 * free vcache entries and all the vcache entries are active ones then we allocate
3017 * an additional one - admittedly we almost never had that occur.
3021 register struct afs_q *tq, *uq;
3022 register struct vcache *tvc;
3023 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
3027 osi_FreeSmallSpace(tvc->mvid);
3028 tvc->mvid = (struct VenusFid *)0;
3031 aix_gnode_rele(AFSTOV(tvc));
3033 if (tvc->linkData) {
3034 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3039 * Also free the remaining ones in the Cache
3041 for (i = 0; i < VCSIZE; i++) {
3042 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3044 osi_FreeSmallSpace(tvc->mvid);
3045 tvc->mvid = (struct VenusFid *)0;
3049 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
3050 #ifdef AFS_AIX32_ENV
3053 vms_delete(tvc->segid);
3055 tvc->segid = tvc->vmh = NULL;
3056 if (VREFCOUNT_GT(tvc,0))
3057 osi_Panic("flushVcache: vm race");
3065 #if defined(AFS_SUN5_ENV)
3071 if (tvc->linkData) {
3072 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3076 afs_FreeAllAxs(&(tvc->Access));
3082 * Free any leftover callback queue
3084 for (tsp = afs_cbrSpace; tsp; tsp = nsp) {
3086 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
3090 #if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
3091 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3093 #ifdef KERNEL_HAVE_PIN
3094 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3097 #if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
3098 freeVCList = Initial_freeVCList = 0;
3100 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
3101 LOCK_INIT(&afs_xvcb, "afs_xvcb");
3103 for(i = 0; i < VCSIZE; ++i)
3104 QInit(&afs_vhashTV[i]);