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);
166 avc->vlruq.prev = avc->vlruq.next = (struct afs_q *)0;
168 /* keep track of # of files that we bulk stat'd, but never used
169 * before they got recycled.
171 if (avc->states & CBulkStat)
174 /* remove entry from the hash chain */
175 i = VCHash(&avc->fid);
176 uvc = &afs_vhashT[i];
177 for (wvc = *uvc; wvc; uvc = &wvc->hnext, wvc = *uvc) {
180 avc->hnext = (struct vcache *)NULL;
185 /* remove entry from the volume hash table */
186 QRemove(&avc->vhashq);
189 osi_FreeSmallSpace(avc->mvid);
190 avc->mvid = (struct VenusFid *)0;
192 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
193 avc->linkData = NULL;
195 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
196 /* OK, there are no internal vrefCounts, so there shouldn't
197 * be any more refs here. */
199 #ifdef AFS_DARWIN80_ENV
200 vnode_clearfsnode(AFSTOV(avc));
201 vnode_removefsref(AFSTOV(avc));
203 avc->v->v_data = NULL; /* remove from vnode */
205 AFSTOV(avc) = NULL; /* also drop the ptr to vnode */
208 #ifdef AFS_SUN510_ENV
209 /* As we use private vnodes, cleanup is up to us */
210 vn_reinit(AFSTOV(avc));
212 afs_FreeAllAxs(&(avc->Access));
214 /* we can't really give back callbacks on RO files, since the
215 * server only tracks them on a per-volume basis, and we don't
216 * know whether we still have some other files from the same
218 if ((avc->states & CRO) == 0 && avc->callback) {
221 ObtainWriteLock(&afs_xcbhash, 460);
222 afs_DequeueCallback(avc); /* remove it from queued callbacks list */
223 avc->states &= ~(CStatd | CUnique);
224 ReleaseWriteLock(&afs_xcbhash);
225 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
226 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
228 osi_dnlc_purgevp(avc);
231 * Next, keep track of which vnodes we've deleted for create's
232 * optimistic synchronization algorithm
235 if (avc->fid.Fid.Vnode & 1)
240 #if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
241 /* put the entry in the free list */
242 avc->nextfree = freeVCList;
244 if (avc->vlruq.prev || avc->vlruq.next) {
245 refpanic("LRU vs. Free inconsistency");
247 avc->states |= CVFlushed;
249 /* This should put it back on the vnode free list since usecount is 1 */
252 if (VREFCOUNT_GT(avc,0)) {
253 #if defined(AFS_OSF_ENV)
254 VN_UNLOCK(AFSTOV(avc));
256 AFS_RELE(AFSTOV(avc));
258 if (afs_norefpanic) {
259 printf("flush vc refcnt < 1");
261 #if defined(AFS_OSF_ENV)
262 (void)vgone(avc, VX_NOSLEEP, NULL);
264 VN_UNLOCK(AFSTOV(avc));
267 osi_Panic("flush vc refcnt < 1");
269 #endif /* AFS_OSF_ENV */
274 VN_UNLOCK(AFSTOV(avc));
278 } /*afs_FlushVCache */
284 * The core of the inactive vnode op for all but IRIX.
287 afs_InactiveVCache(struct vcache *avc, struct AFS_UCRED *acred)
289 AFS_STATCNT(afs_inactive);
290 if (avc->states & CDirty) {
291 /* we can't keep trying to push back dirty data forever. Give up. */
292 afs_InvalidateAllSegments(avc); /* turns off dirty bit */
294 avc->states &= ~CMAPPED; /* mainly used by SunOS 4.0.x */
295 avc->states &= ~CDirty; /* Turn it off */
296 if (avc->states & CUnlinked) {
297 if (CheckLock(&afs_xvcache) || CheckLock(&afs_xdcache)) {
298 avc->states |= CUnlinkedDel;
301 afs_remunlink(avc, 1); /* ignore any return code */
310 * Description: allocate a callback return structure from the
311 * free list and return it.
313 * Env: The alloc and free routines are both called with the afs_xvcb lock
314 * held, so we don't have to worry about blocking in osi_Alloc.
316 static struct afs_cbr *afs_cbrSpace = 0;
320 register struct afs_cbr *tsp;
323 while (!afs_cbrSpace) {
324 if (afs_stats_cmperf.CallBackAlloced >= 2) {
325 /* don't allocate more than 2 * AFS_NCBRS for now */
327 afs_stats_cmperf.CallBackFlushes++;
331 (struct afs_cbr *)afs_osi_Alloc(AFS_NCBRS *
332 sizeof(struct afs_cbr));
333 for (i = 0; i < AFS_NCBRS - 1; i++) {
334 tsp[i].next = &tsp[i + 1];
336 tsp[AFS_NCBRS - 1].next = 0;
338 afs_stats_cmperf.CallBackAlloced++;
342 afs_cbrSpace = tsp->next;
349 * Description: free a callback return structure, removing it from all lists.
352 * asp -- the address of the structure to free.
354 * Environment: the xvcb lock is held over these calls.
357 afs_FreeCBR(register struct afs_cbr *asp)
359 *(asp->pprev) = asp->next;
361 asp->next->pprev = asp->pprev;
363 *(asp->hash_pprev) = asp->hash_next;
365 asp->hash_next->hash_pprev = asp->hash_pprev;
367 asp->next = afs_cbrSpace;
375 * Description: flush all queued callbacks to all servers.
379 * Environment: holds xvcb lock over RPC to guard against race conditions
380 * when a new callback is granted for the same file later on.
383 afs_FlushVCBs(afs_int32 lockit)
385 struct AFSFid *tfids;
386 struct AFSCallBack callBacks[1];
387 struct AFSCBFids fidArray;
388 struct AFSCBs cbArray;
390 struct afs_cbr *tcbrp;
394 struct vrequest treq;
396 int safety1, safety2, safety3;
398 if ((code = afs_InitReq(&treq, afs_osi_credp)))
400 treq.flags |= O_NONBLOCK;
401 tfids = afs_osi_Alloc(sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
404 MObtainWriteLock(&afs_xvcb, 273);
405 ObtainReadLock(&afs_xserver);
406 for (i = 0; i < NSERVERS; i++) {
407 for (safety1 = 0, tsp = afs_servers[i];
408 tsp && safety1 < afs_totalServers + 10;
409 tsp = tsp->next, safety1++) {
411 if (tsp->cbrs == (struct afs_cbr *)0)
414 /* otherwise, grab a block of AFS_MAXCBRSCALL from the list
415 * and make an RPC, over and over again.
417 tcount = 0; /* number found so far */
418 for (safety2 = 0; safety2 < afs_cacheStats; safety2++) {
419 if (tcount >= AFS_MAXCBRSCALL || !tsp->cbrs) {
420 /* if buffer is full, or we've queued all we're going
421 * to from this server, we should flush out the
424 fidArray.AFSCBFids_len = tcount;
425 fidArray.AFSCBFids_val = (struct AFSFid *)tfids;
426 cbArray.AFSCBs_len = 1;
427 cbArray.AFSCBs_val = callBacks;
428 memset(&callBacks[0], 0, sizeof(callBacks[0]));
429 callBacks[0].CallBackType = CB_EXCLUSIVE;
430 for (safety3 = 0; safety3 < MAXHOSTS * 2; safety3++) {
431 tc = afs_ConnByHost(tsp, tsp->cell->fsport,
432 tsp->cell->cellNum, &treq, 0,
436 (AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS);
439 RXAFS_GiveUpCallBacks(tc->id, &fidArray,
447 AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS, SHARED_LOCK,
452 /* ignore return code, since callbacks may have
453 * been returned anyway, we shouldn't leave them
454 * around to be returned again.
456 * Next, see if we are done with this server, and if so,
457 * break to deal with the next one.
463 /* if to flush full buffer */
464 /* if we make it here, we have an entry at the head of cbrs,
465 * which we should copy to the file ID array and then free.
468 tfids[tcount++] = tcbrp->fid;
470 /* Freeing the CBR will unlink it from the server's CBR list */
472 } /* while loop for this one server */
473 if (safety2 > afs_cacheStats) {
474 afs_warn("possible internal error afs_flushVCBs (%d)\n",
477 } /* for loop for this hash chain */
478 } /* loop through all hash chains */
479 if (safety1 > afs_totalServers + 2) {
481 ("AFS internal error (afs_flushVCBs) (%d > %d), continuing...\n",
482 safety1, afs_totalServers + 2);
484 osi_Panic("afs_flushVCBS safety1");
487 ReleaseReadLock(&afs_xserver);
489 MReleaseWriteLock(&afs_xvcb);
490 afs_osi_Free(tfids, sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
498 * Queue a callback on the given fid.
504 * Locks the xvcb lock.
505 * Called when the xvcache lock is already held.
509 afs_QueueVCB(struct vcache *avc)
512 struct afs_cbr *tcbp;
514 AFS_STATCNT(afs_QueueVCB);
515 /* The callback is really just a struct server ptr. */
516 tsp = (struct server *)(avc->callback);
518 /* we now have a pointer to the server, so we just allocate
519 * a queue entry and queue it.
521 MObtainWriteLock(&afs_xvcb, 274);
522 tcbp = afs_AllocCBR();
523 tcbp->fid = avc->fid.Fid;
525 tcbp->next = tsp->cbrs;
527 tsp->cbrs->pprev = &tcbp->next;
530 tcbp->pprev = &tsp->cbrs;
532 afs_InsertHashCBR(tcbp);
534 /* now release locks and return */
535 MReleaseWriteLock(&afs_xvcb);
544 * Remove a queued callback for a given Fid.
547 * afid: The fid we want cleansed of queued callbacks.
550 * Locks xvcb and xserver locks.
551 * Typically called with xdcache, xvcache and/or individual vcache
556 afs_RemoveVCB(struct VenusFid *afid)
559 struct afs_cbr *cbr, *ncbr;
561 AFS_STATCNT(afs_RemoveVCB);
562 MObtainWriteLock(&afs_xvcb, 275);
564 slot = afs_HashCBRFid(&afid->Fid);
565 ncbr = afs_cbrHashT[slot];
569 ncbr = cbr->hash_next;
571 if (afid->Fid.Volume == cbr->fid.Volume &&
572 afid->Fid.Vnode == cbr->fid.Vnode &&
573 afid->Fid.Unique == cbr->fid.Unique) {
578 MReleaseWriteLock(&afs_xvcb);
582 afs_FlushReclaimedVcaches(void)
584 #if !defined(AFS_LINUX22_ENV)
587 struct vcache *tmpReclaimedVCList = NULL;
589 ObtainWriteLock(&afs_xvreclaim, 76);
590 while (ReclaimedVCList) {
591 tvc = ReclaimedVCList; /* take from free list */
592 ReclaimedVCList = tvc->nextfree;
593 tvc->nextfree = NULL;
594 code = afs_FlushVCache(tvc, &fv_slept);
596 /* Ok, so, if we got code != 0, uh, wtf do we do? */
597 /* Probably, build a temporary list and then put all back when we
598 get to the end of the list */
599 /* This is actually really crappy, but we need to not leak these.
600 We probably need a way to be smarter about this. */
601 tvc->nextfree = tmpReclaimedVCList;
602 tmpReclaimedVCList = tvc;
603 printf("Reclaim list flush %lx failed: %d\n", (unsigned long) tvc, code);
606 if (tmpReclaimedVCList)
607 ReclaimedVCList = tmpReclaimedVCList;
609 ReleaseWriteLock(&afs_xvreclaim);
617 * This routine is responsible for allocating a new cache entry
618 * from the free list. It formats the cache entry and inserts it
619 * into the appropriate hash tables. It must be called with
620 * afs_xvcache write-locked so as to prevent several processes from
621 * trying to create a new cache entry simultaneously.
624 * afid : The file id of the file whose cache entry is being
627 /* LOCK: afs_NewVCache afs_xvcache W */
629 afs_NewVCache(struct VenusFid *afid, struct server *serverp)
633 afs_int32 anumber = VCACHE_FREE;
635 struct gnode *gnodepnt;
639 #endif /* AFS_OSF_ENV */
640 struct afs_q *tq, *uq;
643 AFS_STATCNT(afs_NewVCache);
645 afs_FlushReclaimedVcaches();
647 #if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
648 #if defined(AFS_OSF30_ENV) || defined(AFS_LINUX22_ENV)
649 if (afs_vcount >= afs_maxvcount)
652 * If we are using > 33 % of the total system vnodes for AFS vcache
653 * entries or we are using the maximum number of vcache entries,
654 * then free some. (if our usage is > 33% we should free some, if
655 * our usage is > afs_maxvcount, set elsewhere to 0.5*nvnode,
656 * we _must_ free some -- no choice).
658 if (((3 * afs_vcount) > nvnode) || (afs_vcount >= afs_maxvcount))
665 for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
668 if (tvc->states & CVFlushed) {
669 refpanic("CVFlushed on VLRU");
670 } else if (i++ > afs_maxvcount) {
671 refpanic("Exceeded pool of AFS vnodes(VLRU cycle?)");
672 } else if (QNext(uq) != tq) {
673 refpanic("VLRU inconsistent");
674 } else if (!VREFCOUNT_GT(tvc,0)) {
675 refpanic("refcnt 0 on VLRU");
678 #if defined(AFS_LINUX22_ENV)
679 if (tvc != afs_globalVp && VREFCOUNT(tvc) > 1 && tvc->opens == 0) {
680 struct dentry *dentry;
681 struct list_head *cur, *head;
684 #if defined(AFS_LINUX24_ENV)
685 spin_lock(&dcache_lock);
687 head = &(AFSTOV(tvc))->i_dentry;
691 while ((cur = cur->next) != head) {
692 dentry = list_entry(cur, struct dentry, d_alias);
694 if (d_unhashed(dentry))
699 #if defined(AFS_LINUX24_ENV)
700 spin_unlock(&dcache_lock);
702 if (d_invalidate(dentry) == -EBUSY) {
704 /* perhaps lock and try to continue? (use cur as head?) */
708 #if defined(AFS_LINUX24_ENV)
709 spin_lock(&dcache_lock);
713 #if defined(AFS_LINUX24_ENV)
714 spin_unlock(&dcache_lock);
722 if (VREFCOUNT_GT(tvc,0) && !VREFCOUNT_GT(tvc,1) &&
724 && (tvc->states & CUnlinkedDel) == 0) {
725 code = afs_FlushVCache(tvc, &fv_slept);
732 continue; /* start over - may have raced. */
738 if (anumber == VCACHE_FREE) {
739 printf("afs_NewVCache: warning none freed, using %d of %d\n",
740 afs_vcount, afs_maxvcount);
741 if (afs_vcount >= afs_maxvcount) {
742 printf("afs_NewVCache - none freed\n");
748 #if defined(AFS_LINUX22_ENV)
753 ip = new_inode(afs_globalVFS);
755 osi_Panic("afs_NewVCache: no more inodes");
757 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
760 tvc = afs_osi_Alloc(sizeof(struct vcache));
761 ip->u.generic_ip = tvc;
767 if (getnewvnode(MOUNT_AFS, &Afs_vnodeops, &nvc)) {
768 /* What should we do ???? */
769 osi_Panic("afs_NewVCache: no more vnodes");
774 tvc->nextfree = NULL;
777 #else /* AFS_OSF_ENV */
778 /* pull out a free cache entry */
781 for (tq = VLRU.prev; (anumber > 0) && (tq != &VLRU); tq = uq) {
785 if (tvc->states & CVFlushed) {
786 refpanic("CVFlushed on VLRU");
787 } else if (i++ > 2 * afs_cacheStats) { /* even allowing for a few xallocs... */
788 refpanic("Increase -stat parameter of afsd(VLRU cycle?)");
789 } else if (QNext(uq) != tq) {
790 refpanic("VLRU inconsistent");
791 } else if (tvc->states & CVInit) {
795 if (!VREFCOUNT_GT(tvc,0)
796 #if defined(AFS_DARWIN_ENV) && !defined(UKERNEL) && !defined(AFS_DARWIN80_ENV)
797 || ((VREFCOUNT(tvc) == 1) &&
798 (UBCINFOEXISTS(AFSTOV(tvc))))
800 && tvc->opens == 0 && (tvc->states & CUnlinkedDel) == 0) {
801 #if defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
802 #ifdef AFS_DARWIN80_ENV
803 vnode_t tvp = AFSTOV(tvc);
804 /* VREFCOUNT_GT only sees usecounts, not iocounts */
805 /* so this may fail to actually recycle the vnode now */
806 /* must call vnode_get to avoid races. */
807 if (vnode_get(tvp) == 0) {
809 /* must release lock, since vnode_put will immediately
810 reclaim if there are no other users */
811 ReleaseWriteLock(&afs_xvcache);
816 ObtainWriteLock(&afs_xvcache, 336);
818 /* we can't use the vnode_recycle return value to figure
819 * this out, since the iocount we have to hold makes it
821 if (AFSTOV(tvc) == tvp)
827 * vgone() reclaims the vnode, which calls afs_FlushVCache(),
828 * then it puts the vnode on the free list.
829 * If we don't do this we end up with a cleaned vnode that's
830 * not on the free list.
831 * XXX assume FreeBSD is the same for now.
840 code = afs_FlushVCache(tvc, &fv_slept);
848 continue; /* start over - may have raced. */
856 /* none free, making one is better than a panic */
857 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
858 tvc = (struct vcache *)afs_osi_Alloc(sizeof(struct vcache));
859 #if defined(AFS_DARWIN_ENV) && !defined(UKERNEL)
860 tvc->v = NULL; /* important to clean this, or use memset 0 */
862 #ifdef KERNEL_HAVE_PIN
863 pin((char *)tvc, sizeof(struct vcache)); /* XXX */
865 #if defined(AFS_SGI_ENV)
867 char name[METER_NAMSZ];
868 memset(tvc, 0, sizeof(struct vcache));
869 tvc->v.v_number = ++afsvnumbers;
870 tvc->vc_rwlockid = OSI_NO_LOCKID;
871 initnsema(&tvc->vc_rwlock, 1,
872 makesname(name, "vrw", tvc->v.v_number));
873 #ifndef AFS_SGI53_ENV
874 initnsema(&tvc->v.v_sync, 0,
875 makesname(name, "vsy", tvc->v.v_number));
877 #ifndef AFS_SGI62_ENV
878 initnlock(&tvc->v.v_lock,
879 makesname(name, "vlk", tvc->v.v_number));
882 #endif /* AFS_SGI_ENV */
884 tvc = freeVCList; /* take from free list */
885 freeVCList = tvc->nextfree;
886 tvc->nextfree = NULL;
888 #endif /* AFS_OSF_ENV */
890 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
892 panic("afs_NewVCache(): free vcache with vnode attached");
895 #if !defined(AFS_SGI_ENV) && !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
896 memset((char *)tvc, 0, sizeof(struct vcache));
901 RWLOCK_INIT(&tvc->lock, "vcache lock");
902 #if defined(AFS_SUN5_ENV)
903 RWLOCK_INIT(&tvc->vlock, "vcache vlock");
904 #endif /* defined(AFS_SUN5_ENV) */
906 tvc->parentVnode = 0;
908 tvc->linkData = NULL;
911 tvc->execsOrWriters = 0;
914 tvc->states = CVInit;
915 tvc->last_looker = 0;
917 tvc->asynchrony = -1;
920 tvc->flushDV.low = tvc->flushDV.high = AFS_MAXDV;
923 tvc->truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
924 hzero(tvc->m.DataVersion); /* in case we copy it into flushDV */
926 tvc->callback = serverp; /* to minimize chance that clear
932 tvc->hnext = afs_vhashT[i];
934 QAdd(&afs_vhashTV[j], &tvc->vhashq);
936 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
937 refpanic("NewVCache VLRU inconsistent");
939 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
940 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
941 refpanic("NewVCache VLRU inconsistent2");
943 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
944 refpanic("NewVCache VLRU inconsistent3");
946 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
947 refpanic("NewVCache VLRU inconsistent4");
950 /* it should now be safe to drop the xvcache lock */
952 ReleaseWriteLock(&afs_xvcache);
954 afs_nbsd_getnewvnode(tvc); /* includes one refcount */
956 ObtainWriteLock(&afs_xvcache,337);
957 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
959 #ifdef AFS_DARWIN_ENV
960 ReleaseWriteLock(&afs_xvcache);
962 afs_darwin_getnewvnode(tvc); /* includes one refcount */
964 ObtainWriteLock(&afs_xvcache,338);
965 #ifdef AFS_DARWIN80_ENV
966 LOCKINIT(tvc->rwlock);
968 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
975 ReleaseWriteLock(&afs_xvcache);
977 #if defined(AFS_FBSD60_ENV)
978 if (getnewvnode(MOUNT_AFS, afs_globalVFS, &afs_vnodeops, &vp))
979 #elif defined(AFS_FBSD50_ENV)
980 if (getnewvnode(MOUNT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
982 if (getnewvnode(VT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
984 panic("afs getnewvnode"); /* can't happen */
986 ObtainWriteLock(&afs_xvcache,339);
987 if (tvc->v != NULL) {
988 /* I'd like to know if this ever happens...
989 * We don't drop global for the rest of this function,
990 * so if we do lose the race, the other thread should
991 * have found the same vnode and finished initializing
992 * the vcache entry. Is it conceivable that this vcache
993 * entry could be recycled during this interval? If so,
994 * then there probably needs to be some sort of additional
995 * mutual exclusion (an Embryonic flag would suffice).
997 printf("afs_NewVCache: lost the race\n");
1001 tvc->v->v_data = tvc;
1002 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
1006 #if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
1007 /* Hold it for the LRU (should make count 2) */
1008 VN_HOLD(AFSTOV(tvc));
1009 #else /* AFS_OSF_ENV */
1010 #if !(defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV))
1011 VREFCOUNT_SET(tvc, 1); /* us */
1012 #endif /* AFS_XBSD_ENV */
1013 #endif /* AFS_OSF_ENV */
1014 #ifdef AFS_AIX32_ENV
1015 LOCK_INIT(&tvc->pvmlock, "vcache pvmlock");
1016 tvc->vmh = tvc->segid = NULL;
1019 #ifdef AFS_BOZONLOCK_ENV
1020 #if defined(AFS_SUN5_ENV)
1021 rw_init(&tvc->rwlock, "vcache rwlock", RW_DEFAULT, NULL);
1023 #if defined(AFS_SUN55_ENV)
1024 /* This is required if the kaio (kernel aynchronous io)
1025 ** module is installed. Inside the kernel, the function
1026 ** check_vp( common/os/aio.c) checks to see if the kernel has
1027 ** to provide asynchronous io for this vnode. This
1028 ** function extracts the device number by following the
1029 ** v_data field of the vnode. If we do not set this field
1030 ** then the system panics. The value of the v_data field
1031 ** is not really important for AFS vnodes because the kernel
1032 ** does not do asynchronous io for regular files. Hence,
1033 ** for the time being, we fill up the v_data field with the
1034 ** vnode pointer itself. */
1035 tvc->v.v_data = (char *)tvc;
1036 #endif /* AFS_SUN55_ENV */
1038 afs_BozonInit(&tvc->pvnLock, tvc);
1041 /* initialize vnode data, note vrefCount is v.v_count */
1043 /* Don't forget to free the gnode space */
1044 tvc->v.v_gnode = gnodepnt =
1045 (struct gnode *)osi_AllocSmallSpace(sizeof(struct gnode));
1046 memset((char *)gnodepnt, 0, sizeof(struct gnode));
1048 #ifdef AFS_SGI64_ENV
1049 memset((void *)&(tvc->vc_bhv_desc), 0, sizeof(tvc->vc_bhv_desc));
1050 bhv_desc_init(&(tvc->vc_bhv_desc), tvc, tvc, &Afs_vnodeops);
1051 #ifdef AFS_SGI65_ENV
1052 vn_bhv_head_init(&(tvc->v.v_bh), "afsvp");
1053 vn_bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
1055 bhv_head_init(&(tvc->v.v_bh));
1056 bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
1058 #ifdef AFS_SGI65_ENV
1059 tvc->v.v_mreg = tvc->v.v_mregb = (struct pregion *)tvc;
1060 #ifdef VNODE_TRACING
1061 tvc->v.v_trace = ktrace_alloc(VNODE_TRACE_SIZE, 0);
1063 init_bitlock(&tvc->v.v_pcacheflag, VNODE_PCACHE_LOCKBIT, "afs_pcache",
1065 init_mutex(&tvc->v.v_filocksem, MUTEX_DEFAULT, "afsvfl", (long)tvc);
1066 init_mutex(&tvc->v.v_buf_lock, MUTEX_DEFAULT, "afsvnbuf", (long)tvc);
1068 vnode_pcache_init(&tvc->v);
1069 #if defined(DEBUG) && defined(VNODE_INIT_BITLOCK)
1070 /* Above define is never true execpt in SGI test kernels. */
1071 init_bitlock(&(tvc->v.v_flag, VLOCK, "vnode", tvc->v.v_number);
1073 #ifdef INTR_KTHREADS
1074 AFS_VN_INIT_BUF_LOCK(&(tvc->v));
1077 SetAfsVnode(AFSTOV(tvc));
1078 #endif /* AFS_SGI64_ENV */
1080 * The proper value for mvstat (for root fids) is setup by the caller.
1083 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
1085 if (afs_globalVFS == 0)
1086 osi_Panic("afs globalvfs");
1087 #if !defined(AFS_LINUX22_ENV)
1088 vSetVfsp(tvc, afs_globalVFS);
1090 vSetType(tvc, VREG);
1092 tvc->v.v_vfsnext = afs_globalVFS->vfs_vnodes; /* link off vfs */
1093 tvc->v.v_vfsprev = NULL;
1094 afs_globalVFS->vfs_vnodes = &tvc->v;
1095 if (tvc->v.v_vfsnext != NULL)
1096 tvc->v.v_vfsnext->v_vfsprev = &tvc->v;
1097 tvc->v.v_next = gnodepnt->gn_vnode; /*Single vnode per gnode for us! */
1098 gnodepnt->gn_vnode = &tvc->v;
1100 #if defined(AFS_DUX40_ENV)
1101 insmntque(tvc, afs_globalVFS, &afs_ubcops);
1104 /* Is this needed??? */
1105 insmntque(tvc, afs_globalVFS);
1106 #endif /* AFS_OSF_ENV */
1107 #endif /* AFS_DUX40_ENV */
1108 #if defined(AFS_SGI_ENV)
1109 VN_SET_DPAGES(&(tvc->v), (struct pfdat *)NULL);
1110 osi_Assert((tvc->v.v_flag & VINACT) == 0);
1112 osi_Assert(VN_GET_PGCNT(&(tvc->v)) == 0);
1113 osi_Assert(tvc->mapcnt == 0 && tvc->vc_locktrips == 0);
1114 osi_Assert(tvc->vc_rwlockid == OSI_NO_LOCKID);
1115 osi_Assert(tvc->v.v_filocks == NULL);
1116 #if !defined(AFS_SGI65_ENV)
1117 osi_Assert(tvc->v.v_filocksem == NULL);
1119 osi_Assert(tvc->cred == NULL);
1120 #ifdef AFS_SGI64_ENV
1121 vnode_pcache_reinit(&tvc->v);
1122 tvc->v.v_rdev = NODEV;
1124 vn_initlist((struct vnlist *)&tvc->v);
1126 #endif /* AFS_SGI_ENV */
1128 osi_dnlc_purgedp(tvc); /* this may be overkill */
1129 memset((char *)&(tvc->callsort), 0, sizeof(struct afs_q));
1131 tvc->states &=~ CVInit;
1132 afs_osi_Wakeup(&tvc->states);
1136 } /*afs_NewVCache */
1140 * afs_FlushActiveVcaches
1146 * doflocks : Do we handle flocks?
1148 /* LOCK: afs_FlushActiveVcaches afs_xvcache N */
1150 afs_FlushActiveVcaches(register afs_int32 doflocks)
1152 register struct vcache *tvc;
1154 register struct conn *tc;
1155 register afs_int32 code;
1156 register struct AFS_UCRED *cred = NULL;
1157 struct vrequest treq, ureq;
1158 struct AFSVolSync tsync;
1161 AFS_STATCNT(afs_FlushActiveVcaches);
1162 ObtainReadLock(&afs_xvcache);
1163 for (i = 0; i < VCSIZE; i++) {
1164 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1165 if (tvc->states & CVInit) continue;
1166 #ifdef AFS_DARWIN80_ENV
1167 if (tvc->states & CDeadVnode &&
1168 (tvc->states & (CCore|CUnlinkedDel) ||
1169 tvc->flockCount)) panic("Dead vnode has core/unlinkedel/flock");
1171 if (doflocks && tvc->flockCount != 0) {
1172 /* if this entry has an flock, send a keep-alive call out */
1174 ReleaseReadLock(&afs_xvcache);
1175 ObtainWriteLock(&tvc->lock, 51);
1177 afs_InitReq(&treq, afs_osi_credp);
1178 treq.flags |= O_NONBLOCK;
1180 tc = afs_Conn(&tvc->fid, &treq, SHARED_LOCK);
1182 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
1185 RXAFS_ExtendLock(tc->id,
1186 (struct AFSFid *)&tvc->fid.Fid,
1192 } while (afs_Analyze
1193 (tc, code, &tvc->fid, &treq,
1194 AFS_STATS_FS_RPCIDX_EXTENDLOCK, SHARED_LOCK, NULL));
1196 ReleaseWriteLock(&tvc->lock);
1197 #ifdef AFS_DARWIN80_ENV
1199 ObtainReadLock(&afs_xvcache);
1201 ObtainReadLock(&afs_xvcache);
1206 if ((tvc->states & CCore) || (tvc->states & CUnlinkedDel)) {
1208 * Don't let it evaporate in case someone else is in
1209 * this code. Also, drop the afs_xvcache lock while
1210 * getting vcache locks.
1213 ReleaseReadLock(&afs_xvcache);
1214 #ifdef AFS_BOZONLOCK_ENV
1215 afs_BozonLock(&tvc->pvnLock, tvc);
1217 #if defined(AFS_SGI_ENV)
1219 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1221 osi_Assert(VREFCOUNT_GT(tvc,0));
1222 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1224 ObtainWriteLock(&tvc->lock, 52);
1225 if (tvc->states & CCore) {
1226 tvc->states &= ~CCore;
1227 /* XXXX Find better place-holder for cred XXXX */
1228 cred = (struct AFS_UCRED *)tvc->linkData;
1229 tvc->linkData = NULL; /* XXX */
1230 afs_InitReq(&ureq, cred);
1231 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1232 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
1233 tvc->execsOrWriters);
1234 code = afs_StoreOnLastReference(tvc, &ureq);
1235 ReleaseWriteLock(&tvc->lock);
1236 #ifdef AFS_BOZONLOCK_ENV
1237 afs_BozonUnlock(&tvc->pvnLock, tvc);
1239 hzero(tvc->flushDV);
1242 if (code && code != VNOVNODE) {
1243 afs_StoreWarn(code, tvc->fid.Fid.Volume,
1244 /* /dev/console */ 1);
1246 } else if (tvc->states & CUnlinkedDel) {
1250 ReleaseWriteLock(&tvc->lock);
1251 #ifdef AFS_BOZONLOCK_ENV
1252 afs_BozonUnlock(&tvc->pvnLock, tvc);
1254 #if defined(AFS_SGI_ENV)
1255 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1257 afs_remunlink(tvc, 0);
1258 #if defined(AFS_SGI_ENV)
1259 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1262 /* lost (or won, perhaps) the race condition */
1263 ReleaseWriteLock(&tvc->lock);
1264 #ifdef AFS_BOZONLOCK_ENV
1265 afs_BozonUnlock(&tvc->pvnLock, tvc);
1268 #if defined(AFS_SGI_ENV)
1269 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1271 #ifdef AFS_DARWIN80_ENV
1274 AFS_RELE(AFSTOV(tvc));
1275 /* Matches write code setting CCore flag */
1278 ObtainReadLock(&afs_xvcache);
1280 ObtainReadLock(&afs_xvcache);
1283 AFS_RELE(AFSTOV(tvc));
1284 /* Matches write code setting CCore flag */
1291 ReleaseReadLock(&afs_xvcache);
1300 * Make sure a cache entry is up-to-date status-wise.
1302 * NOTE: everywhere that calls this can potentially be sped up
1303 * by checking CStatd first, and avoiding doing the InitReq
1304 * if this is up-to-date.
1306 * Anymore, the only places that call this KNOW already that the
1307 * vcache is not up-to-date, so we don't screw around.
1310 * avc : Ptr to vcache entry to verify.
1315 afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1317 register struct vcache *tvc;
1319 AFS_STATCNT(afs_VerifyVCache);
1321 #if defined(AFS_OSF_ENV)
1322 ObtainReadLock(&avc->lock);
1323 if (afs_IsWired(avc)) {
1324 ReleaseReadLock(&avc->lock);
1327 ReleaseReadLock(&avc->lock);
1328 #endif /* AFS_OSF_ENV */
1329 /* otherwise we must fetch the status info */
1331 ObtainWriteLock(&avc->lock, 53);
1332 if (avc->states & CStatd) {
1333 ReleaseWriteLock(&avc->lock);
1336 ObtainWriteLock(&afs_xcbhash, 461);
1337 avc->states &= ~(CStatd | CUnique);
1338 avc->callback = NULL;
1339 afs_DequeueCallback(avc);
1340 ReleaseWriteLock(&afs_xcbhash);
1341 ReleaseWriteLock(&avc->lock);
1343 /* since we've been called back, or the callback has expired,
1344 * it's possible that the contents of this directory, or this
1345 * file's name have changed, thus invalidating the dnlc contents.
1347 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1348 osi_dnlc_purgedp(avc);
1350 osi_dnlc_purgevp(avc);
1352 /* fetch the status info */
1353 tvc = afs_GetVCache(&avc->fid, areq, NULL, avc);
1356 /* Put it back; caller has already incremented vrefCount */
1360 } /*afs_VerifyVCache */
1367 * Simple copy of stat info into cache.
1370 * avc : Ptr to vcache entry involved.
1371 * astat : Ptr to stat info to copy.
1374 * Nothing interesting.
1376 * Callers: as of 1992-04-29, only called by WriteVCache
1379 afs_SimpleVStat(register struct vcache *avc,
1380 register struct AFSFetchStatus *astat, struct vrequest *areq)
1383 AFS_STATCNT(afs_SimpleVStat);
1386 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1387 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1389 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1391 #ifdef AFS_64BIT_CLIENT
1392 FillInt64(length, astat->Length_hi, astat->Length);
1393 #else /* AFS_64BIT_CLIENT */
1394 length = astat->Length;
1395 #endif /* AFS_64BIT_CLIENT */
1396 #if defined(AFS_SGI_ENV)
1397 osi_Assert((valusema(&avc->vc_rwlock) <= 0)
1398 && (OSI_GET_LOCKID() == avc->vc_rwlockid));
1399 if (length < avc->m.Length) {
1400 vnode_t *vp = (vnode_t *) avc;
1402 osi_Assert(WriteLocked(&avc->lock));
1403 ReleaseWriteLock(&avc->lock);
1405 PTOSSVP(vp, (off_t) length, (off_t) MAXLONG);
1407 ObtainWriteLock(&avc->lock, 67);
1410 /* if writing the file, don't fetch over this value */
1411 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT, ICL_TYPE_POINTER, avc,
1412 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1413 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1414 avc->m.Length = length;
1415 avc->m.Date = astat->ClientModTime;
1417 avc->m.Owner = astat->Owner;
1418 avc->m.Group = astat->Group;
1419 avc->m.Mode = astat->UnixModeBits;
1420 if (vType(avc) == VREG) {
1421 avc->m.Mode |= S_IFREG;
1422 } else if (vType(avc) == VDIR) {
1423 avc->m.Mode |= S_IFDIR;
1424 } else if (vType(avc) == VLNK) {
1425 avc->m.Mode |= S_IFLNK;
1426 if ((avc->m.Mode & 0111) == 0)
1429 if (avc->states & CForeign) {
1430 struct axscache *ac;
1431 avc->anyAccess = astat->AnonymousAccess;
1433 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1435 * Caller has at least one bit not covered by anonymous, and
1436 * thus may have interesting rights.
1438 * HOWEVER, this is a really bad idea, because any access query
1439 * for bits which aren't covered by anonymous, on behalf of a user
1440 * who doesn't have any special rights, will result in an answer of
1441 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1442 * It's an especially bad idea under Ultrix, since (due to the lack of
1443 * a proper access() call) it must perform several afs_access() calls
1444 * in order to create magic mode bits that vary according to who makes
1445 * the call. In other words, _every_ stat() generates a test for
1448 #endif /* badidea */
1449 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1450 ac->axess = astat->CallerAccess;
1451 else /* not found, add a new one if possible */
1452 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1456 } /*afs_SimpleVStat */
1463 * Store the status info *only* back to the server for a
1467 * avc : Ptr to the vcache entry.
1468 * astatus : Ptr to the status info to store.
1469 * areq : Ptr to the associated vrequest.
1472 * Must be called with a shared lock held on the vnode.
1476 afs_WriteVCache(register struct vcache *avc,
1477 register struct AFSStoreStatus *astatus,
1478 struct vrequest *areq)
1482 struct AFSFetchStatus OutStatus;
1483 struct AFSVolSync tsync;
1485 AFS_STATCNT(afs_WriteVCache);
1486 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1487 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
1490 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
1492 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1495 RXAFS_StoreStatus(tc->id, (struct AFSFid *)&avc->fid.Fid,
1496 astatus, &OutStatus, &tsync);
1501 } while (afs_Analyze
1502 (tc, code, &avc->fid, areq, AFS_STATS_FS_RPCIDX_STORESTATUS,
1503 SHARED_LOCK, NULL));
1505 UpgradeSToWLock(&avc->lock, 20);
1507 /* success, do the changes locally */
1508 afs_SimpleVStat(avc, &OutStatus, areq);
1510 * Update the date, too. SimpleVStat didn't do this, since
1511 * it thought we were doing this after fetching new status
1512 * over a file being written.
1514 avc->m.Date = OutStatus.ClientModTime;
1516 /* failure, set up to check with server next time */
1517 ObtainWriteLock(&afs_xcbhash, 462);
1518 afs_DequeueCallback(avc);
1519 avc->states &= ~(CStatd | CUnique); /* turn off stat valid flag */
1520 ReleaseWriteLock(&afs_xcbhash);
1521 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1522 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
1524 ConvertWToSLock(&avc->lock);
1527 } /*afs_WriteVCache */
1533 * Copy astat block into vcache info
1536 * avc : Ptr to vcache entry.
1537 * astat : Ptr to stat block to copy in.
1538 * areq : Ptr to associated request.
1541 * Must be called under a write lock
1543 * Note: this code may get dataversion and length out of sync if the file has
1544 * been modified. This is less than ideal. I haven't thought about
1545 * it sufficiently to be certain that it is adequate.
1548 afs_ProcessFS(register struct vcache *avc,
1549 register struct AFSFetchStatus *astat, struct vrequest *areq)
1552 AFS_STATCNT(afs_ProcessFS);
1554 #ifdef AFS_64BIT_CLIENT
1555 FillInt64(length, astat->Length_hi, astat->Length);
1556 #else /* AFS_64BIT_CLIENT */
1557 length = astat->Length;
1558 #endif /* AFS_64BIT_CLIENT */
1559 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1560 * number for each bulk status request. Under no circumstances
1561 * should afs_DoBulkStat store a sequence number if the new
1562 * length will be ignored when afs_ProcessFS is called with
1563 * new stats. If you change the following conditional then you
1564 * also need to change the conditional in afs_DoBulkStat. */
1566 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1567 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1569 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1571 /* if we're writing or mapping this file, don't fetch over these
1574 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1575 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1576 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1577 avc->m.Length = length;
1578 avc->m.Date = astat->ClientModTime;
1580 hset64(avc->m.DataVersion, astat->dataVersionHigh, astat->DataVersion);
1581 avc->m.Owner = astat->Owner;
1582 avc->m.Mode = astat->UnixModeBits;
1583 avc->m.Group = astat->Group;
1584 avc->m.LinkCount = astat->LinkCount;
1585 if (astat->FileType == File) {
1586 vSetType(avc, VREG);
1587 avc->m.Mode |= S_IFREG;
1588 } else if (astat->FileType == Directory) {
1589 vSetType(avc, VDIR);
1590 avc->m.Mode |= S_IFDIR;
1591 } else if (astat->FileType == SymbolicLink) {
1592 if (afs_fakestat_enable && (avc->m.Mode & 0111) == 0) {
1593 vSetType(avc, VDIR);
1594 avc->m.Mode |= S_IFDIR;
1596 vSetType(avc, VLNK);
1597 avc->m.Mode |= S_IFLNK;
1599 if ((avc->m.Mode & 0111) == 0) {
1603 avc->anyAccess = astat->AnonymousAccess;
1605 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1607 * Caller has at least one bit not covered by anonymous, and
1608 * thus may have interesting rights.
1610 * HOWEVER, this is a really bad idea, because any access query
1611 * for bits which aren't covered by anonymous, on behalf of a user
1612 * who doesn't have any special rights, will result in an answer of
1613 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1614 * It's an especially bad idea under Ultrix, since (due to the lack of
1615 * a proper access() call) it must perform several afs_access() calls
1616 * in order to create magic mode bits that vary according to who makes
1617 * the call. In other words, _every_ stat() generates a test for
1620 #endif /* badidea */
1622 struct axscache *ac;
1623 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1624 ac->axess = astat->CallerAccess;
1625 else /* not found, add a new one if possible */
1626 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1628 } /*afs_ProcessFS */
1632 afs_RemoteLookup(register struct VenusFid *afid, struct vrequest *areq,
1633 char *name, struct VenusFid *nfid,
1634 struct AFSFetchStatus *OutStatusp,
1635 struct AFSCallBack *CallBackp, struct server **serverp,
1636 struct AFSVolSync *tsyncp)
1640 register struct conn *tc;
1641 struct AFSFetchStatus OutDirStatus;
1644 name = ""; /* XXX */
1646 tc = afs_Conn(afid, areq, SHARED_LOCK);
1649 *serverp = tc->srvr->server;
1651 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1654 RXAFS_Lookup(tc->id, (struct AFSFid *)&afid->Fid, name,
1655 (struct AFSFid *)&nfid->Fid, OutStatusp,
1656 &OutDirStatus, CallBackp, tsyncp);
1661 } while (afs_Analyze
1662 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_XLOOKUP, SHARED_LOCK,
1673 * Given a file id and a vrequest structure, fetch the status
1674 * information associated with the file.
1678 * areq : Ptr to associated vrequest structure, specifying the
1679 * user whose authentication tokens will be used.
1680 * avc : caller may already have a vcache for this file, which is
1684 * The cache entry is returned with an increased vrefCount field.
1685 * The entry must be discarded by calling afs_PutVCache when you
1686 * are through using the pointer to the cache entry.
1688 * You should not hold any locks when calling this function, except
1689 * locks on other vcache entries. If you lock more than one vcache
1690 * entry simultaneously, you should lock them in this order:
1692 * 1. Lock all files first, then directories.
1693 * 2. Within a particular type, lock entries in Fid.Vnode order.
1695 * This locking hierarchy is convenient because it allows locking
1696 * of a parent dir cache entry, given a file (to check its access
1697 * control list). It also allows renames to be handled easily by
1698 * locking directories in a constant order.
1699 * NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1701 /* might have a vcache structure already, which must
1702 * already be held by the caller */
1705 afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
1706 afs_int32 * cached, struct vcache *avc)
1709 afs_int32 code, newvcache = 0;
1710 register struct vcache *tvc;
1714 AFS_STATCNT(afs_GetVCache);
1717 *cached = 0; /* Init just in case */
1719 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1723 ObtainSharedLock(&afs_xvcache, 5);
1725 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU | IS_SLOCK);
1727 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1728 ReleaseSharedLock(&afs_xvcache);
1729 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1737 osi_Assert((tvc->states & CVInit) == 0);
1738 if (tvc->states & CStatd) {
1739 ReleaseSharedLock(&afs_xvcache);
1743 UpgradeSToWLock(&afs_xvcache, 21);
1745 /* no cache entry, better grab one */
1746 tvc = afs_NewVCache(afid, NULL);
1749 ConvertWToSLock(&afs_xvcache);
1752 ReleaseSharedLock(&afs_xvcache);
1756 afs_stats_cmperf.vcacheMisses++;
1759 ReleaseSharedLock(&afs_xvcache);
1761 ObtainWriteLock(&tvc->lock, 54);
1763 if (tvc->states & CStatd) {
1764 ReleaseWriteLock(&tvc->lock);
1767 #if defined(AFS_OSF_ENV)
1768 if (afs_IsWired(tvc)) {
1769 ReleaseWriteLock(&tvc->lock);
1772 #endif /* AFS_OSF_ENV */
1773 #ifdef AFS_DARWIN80_ENV
1774 /* Darwin 8.0 only has bufs in nfs, so we shouldn't have to worry about them.
1777 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
1779 * XXX - I really don't like this. Should try to understand better.
1780 * It seems that sometimes, when we get called, we already hold the
1781 * lock on the vnode (e.g., from afs_getattr via afs_VerifyVCache).
1782 * We can't drop the vnode lock, because that could result in a race.
1783 * Sometimes, though, we get here and don't hold the vnode lock.
1784 * I hate code paths that sometimes hold locks and sometimes don't.
1785 * In any event, the dodge we use here is to check whether the vnode
1786 * is locked, and if it isn't, then we gain and drop it around the call
1787 * to vinvalbuf; otherwise, we leave it alone.
1790 struct vnode *vp = AFSTOV(tvc);
1793 #if defined(AFS_DARWIN_ENV)
1794 iheldthelock = VOP_ISLOCKED(vp);
1796 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, current_proc());
1797 /* this is messy. we can call fsync which will try to reobtain this */
1798 if (VTOAFS(vp) == tvc)
1799 ReleaseWriteLock(&tvc->lock);
1800 if (UBCINFOEXISTS(vp)) {
1801 vinvalbuf(vp, V_SAVE, &afs_osi_cred, current_proc(), PINOD, 0);
1803 if (VTOAFS(vp) == tvc)
1804 ObtainWriteLock(&tvc->lock, 954);
1806 VOP_UNLOCK(vp, LK_EXCLUSIVE, current_proc());
1807 #elif defined(AFS_FBSD60_ENV)
1808 iheldthelock = VOP_ISLOCKED(vp, curthread);
1810 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1811 vinvalbuf(vp, V_SAVE, curthread, PINOD, 0);
1813 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1814 #elif defined(AFS_FBSD50_ENV)
1815 iheldthelock = VOP_ISLOCKED(vp, curthread);
1817 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1818 vinvalbuf(vp, V_SAVE, osi_curcred(), curthread, PINOD, 0);
1820 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1821 #elif defined(AFS_FBSD40_ENV)
1822 iheldthelock = VOP_ISLOCKED(vp, curproc);
1824 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1825 vinvalbuf(vp, V_SAVE, osi_curcred(), curproc, PINOD, 0);
1827 VOP_UNLOCK(vp, LK_EXCLUSIVE, curproc);
1828 #elif defined(AFS_OBSD_ENV)
1829 iheldthelock = VOP_ISLOCKED(vp, curproc);
1831 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1832 uvm_vnp_uncache(vp);
1834 VOP_UNLOCK(vp, 0, curproc);
1840 ObtainWriteLock(&afs_xcbhash, 464);
1841 tvc->states &= ~CUnique;
1843 afs_DequeueCallback(tvc);
1844 ReleaseWriteLock(&afs_xcbhash);
1846 /* It is always appropriate to throw away all the access rights? */
1847 afs_FreeAllAxs(&(tvc->Access));
1848 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1850 if ((tvp->states & VForeign)) {
1852 tvc->states |= CForeign;
1853 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1854 && (tvp->rootUnique == afid->Fid.Unique)) {
1858 if (tvp->states & VRO)
1860 if (tvp->states & VBackup)
1861 tvc->states |= CBackup;
1862 /* now copy ".." entry back out of volume structure, if necessary */
1863 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1865 tvc->mvid = (struct VenusFid *)
1866 osi_AllocSmallSpace(sizeof(struct VenusFid));
1867 *tvc->mvid = tvp->dotdot;
1869 afs_PutVolume(tvp, READ_LOCK);
1873 afs_RemoveVCB(afid);
1875 struct AFSFetchStatus OutStatus;
1877 if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1878 afs_ProcessFS(tvc, &OutStatus, areq);
1879 tvc->states |= CStatd | CUnique;
1882 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1887 ReleaseWriteLock(&tvc->lock);
1893 ReleaseWriteLock(&tvc->lock);
1896 } /*afs_GetVCache */
1901 afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1902 afs_int32 * cached, struct vcache *adp, char *aname)
1904 afs_int32 code, now, newvcache = 0;
1905 struct VenusFid nfid;
1906 register struct vcache *tvc;
1908 struct AFSFetchStatus OutStatus;
1909 struct AFSCallBack CallBack;
1910 struct AFSVolSync tsync;
1911 struct server *serverp = 0;
1915 AFS_STATCNT(afs_GetVCache);
1917 *cached = 0; /* Init just in case */
1919 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1923 ObtainReadLock(&afs_xvcache);
1924 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */ );
1927 ReleaseReadLock(&afs_xvcache);
1929 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1930 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1934 ObtainReadLock(&tvc->lock);
1936 if (tvc->states & CStatd) {
1940 ReleaseReadLock(&tvc->lock);
1943 tvc->states &= ~CUnique;
1945 ReleaseReadLock(&tvc->lock);
1947 ObtainReadLock(&afs_xvcache);
1950 ReleaseReadLock(&afs_xvcache);
1952 /* lookup the file */
1955 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
1957 afs_RemoteLookup(&adp->fid, areq, aname, &nfid, &OutStatus, &CallBack,
1960 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1964 ObtainSharedLock(&afs_xvcache, 6);
1965 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU | IS_SLOCK/* no xstats now */ );
1967 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1968 ReleaseSharedLock(&afs_xvcache);
1969 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1975 /* no cache entry, better grab one */
1976 UpgradeSToWLock(&afs_xvcache, 22);
1977 tvc = afs_NewVCache(&nfid, serverp);
1979 ConvertWToSLock(&afs_xvcache);
1982 ReleaseSharedLock(&afs_xvcache);
1987 ReleaseSharedLock(&afs_xvcache);
1988 ObtainWriteLock(&tvc->lock, 55);
1990 /* It is always appropriate to throw away all the access rights? */
1991 afs_FreeAllAxs(&(tvc->Access));
1992 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
1994 if ((tvp->states & VForeign)) {
1996 tvc->states |= CForeign;
1997 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1998 && (tvp->rootUnique == afid->Fid.Unique))
2001 if (tvp->states & VRO)
2003 if (tvp->states & VBackup)
2004 tvc->states |= CBackup;
2005 /* now copy ".." entry back out of volume structure, if necessary */
2006 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2008 tvc->mvid = (struct VenusFid *)
2009 osi_AllocSmallSpace(sizeof(struct VenusFid));
2010 *tvc->mvid = tvp->dotdot;
2015 ObtainWriteLock(&afs_xcbhash, 465);
2016 afs_DequeueCallback(tvc);
2017 tvc->states &= ~(CStatd | CUnique);
2018 ReleaseWriteLock(&afs_xcbhash);
2019 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2020 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2022 afs_PutVolume(tvp, READ_LOCK);
2023 ReleaseWriteLock(&tvc->lock);
2028 ObtainWriteLock(&afs_xcbhash, 466);
2029 if (origCBs == afs_allCBs) {
2030 if (CallBack.ExpirationTime) {
2031 tvc->callback = serverp;
2032 tvc->cbExpires = CallBack.ExpirationTime + now;
2033 tvc->states |= CStatd | CUnique;
2034 tvc->states &= ~CBulkFetching;
2035 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
2036 } else if (tvc->states & CRO) {
2037 /* adapt gives us an hour. */
2038 tvc->cbExpires = 3600 + osi_Time();
2039 /*XXX*/ tvc->states |= CStatd | CUnique;
2040 tvc->states &= ~CBulkFetching;
2041 afs_QueueCallback(tvc, CBHash(3600), tvp);
2043 tvc->callback = NULL;
2044 afs_DequeueCallback(tvc);
2045 tvc->states &= ~(CStatd | CUnique);
2046 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2047 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2050 afs_DequeueCallback(tvc);
2051 tvc->states &= ~CStatd;
2052 tvc->states &= ~CUnique;
2053 tvc->callback = NULL;
2054 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2055 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2057 ReleaseWriteLock(&afs_xcbhash);
2059 afs_PutVolume(tvp, READ_LOCK);
2060 afs_ProcessFS(tvc, &OutStatus, areq);
2062 ReleaseWriteLock(&tvc->lock);
2068 afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
2069 afs_int32 * cached, struct volume *tvolp)
2071 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
2072 afs_int32 getNewFid = 0;
2074 struct VenusFid nfid;
2075 register struct vcache *tvc;
2076 struct server *serverp = 0;
2077 struct AFSFetchStatus OutStatus;
2078 struct AFSCallBack CallBack;
2079 struct AFSVolSync tsync;
2088 if (!tvolp->rootVnode || getNewFid) {
2089 struct VenusFid tfid;
2092 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2093 origCBs = afs_allCBs; /* ignore InitCallBackState */
2095 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2100 /* ReleaseReadLock(&tvolp->lock); */
2101 ObtainWriteLock(&tvolp->lock, 56);
2102 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
2103 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
2104 ReleaseWriteLock(&tvolp->lock);
2105 /* ObtainReadLock(&tvolp->lock);*/
2108 afid->Fid.Vnode = tvolp->rootVnode;
2109 afid->Fid.Unique = tvolp->rootUnique;
2113 ObtainSharedLock(&afs_xvcache, 7);
2115 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2116 if (!FidCmp(&(tvc->fid), afid)) {
2117 if (tvc->states & CVInit) {
2118 ReleaseSharedLock(&afs_xvcache);
2119 afs_osi_Sleep(&tvc->states);
2123 /* Grab this vnode, possibly reactivating from the free list */
2124 /* for the present (95.05.25) everything on the hash table is
2125 * definitively NOT in the free list -- at least until afs_reclaim
2126 * can be safely implemented */
2128 vg = vget(AFSTOV(tvc)); /* this bumps ref count */
2132 #endif /* AFS_OSF_ENV */
2133 #ifdef AFS_DARWIN80_ENV
2134 if (tvc->states & CDeadVnode) {
2135 ReleaseSharedLock(&afs_xvcache);
2136 afs_osi_Sleep(&tvc->states);
2139 if (vnode_get(AFSTOV(tvc))) /* this bumps ref count */
2146 if (!haveStatus && (!tvc || !(tvc->states & CStatd))) {
2147 /* Mount point no longer stat'd or unknown. FID may have changed. */
2150 AFS_RELE(AFSTOV(tvc));
2153 ReleaseSharedLock(&afs_xvcache);
2154 #ifdef AFS_DARWIN80_ENV
2157 vnode_put(AFSTOV(tvc));
2166 UpgradeSToWLock(&afs_xvcache, 23);
2167 /* no cache entry, better grab one */
2168 tvc = afs_NewVCache(afid, NULL);
2171 ReleaseWriteLock(&afs_xvcache);
2175 afs_stats_cmperf.vcacheMisses++;
2179 afs_stats_cmperf.vcacheHits++;
2181 /* we already bumped the ref count in the for loop above */
2182 #else /* AFS_OSF_ENV */
2185 UpgradeSToWLock(&afs_xvcache, 24);
2186 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2187 refpanic("GRVC VLRU inconsistent0");
2189 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2190 refpanic("GRVC VLRU inconsistent1");
2192 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2193 refpanic("GRVC VLRU inconsistent2");
2195 QRemove(&tvc->vlruq); /* move to lruq head */
2196 QAdd(&VLRU, &tvc->vlruq);
2197 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2198 refpanic("GRVC VLRU inconsistent3");
2200 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2201 refpanic("GRVC VLRU inconsistent4");
2203 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2204 refpanic("GRVC VLRU inconsistent5");
2209 ReleaseWriteLock(&afs_xvcache);
2211 if (tvc->states & CStatd) {
2215 ObtainReadLock(&tvc->lock);
2216 tvc->states &= ~CUnique;
2217 tvc->callback = NULL; /* redundant, perhaps */
2218 ReleaseReadLock(&tvc->lock);
2221 ObtainWriteLock(&tvc->lock, 57);
2223 /* It is always appropriate to throw away all the access rights? */
2224 afs_FreeAllAxs(&(tvc->Access));
2227 tvc->states |= CForeign;
2228 if (tvolp->states & VRO)
2230 if (tvolp->states & VBackup)
2231 tvc->states |= CBackup;
2232 /* now copy ".." entry back out of volume structure, if necessary */
2233 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2234 && (tvolp->rootUnique == afid->Fid.Unique)) {
2237 if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2239 tvc->mvid = (struct VenusFid *)
2240 osi_AllocSmallSpace(sizeof(struct VenusFid));
2241 *tvc->mvid = tvolp->dotdot;
2245 afs_RemoveVCB(afid);
2248 struct VenusFid tfid;
2251 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2252 origCBs = afs_allCBs; /* ignore InitCallBackState */
2254 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2259 ObtainWriteLock(&afs_xcbhash, 467);
2260 afs_DequeueCallback(tvc);
2261 tvc->callback = NULL;
2262 tvc->states &= ~(CStatd | CUnique);
2263 ReleaseWriteLock(&afs_xcbhash);
2264 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2265 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2266 ReleaseWriteLock(&tvc->lock);
2271 ObtainWriteLock(&afs_xcbhash, 468);
2272 if (origCBs == afs_allCBs) {
2273 tvc->states |= CTruth;
2274 tvc->callback = serverp;
2275 if (CallBack.ExpirationTime != 0) {
2276 tvc->cbExpires = CallBack.ExpirationTime + start;
2277 tvc->states |= CStatd;
2278 tvc->states &= ~CBulkFetching;
2279 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2280 } else if (tvc->states & CRO) {
2281 /* adapt gives us an hour. */
2282 tvc->cbExpires = 3600 + osi_Time();
2283 /*XXX*/ tvc->states |= CStatd;
2284 tvc->states &= ~CBulkFetching;
2285 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2288 afs_DequeueCallback(tvc);
2289 tvc->callback = NULL;
2290 tvc->states &= ~(CStatd | CUnique);
2291 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2292 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2294 ReleaseWriteLock(&afs_xcbhash);
2295 afs_ProcessFS(tvc, &OutStatus, areq);
2297 ReleaseWriteLock(&tvc->lock);
2304 * must be called with avc write-locked
2305 * don't absolutely have to invalidate the hint unless the dv has
2306 * changed, but be sure to get it right else there will be consistency bugs.
2309 afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
2310 struct vrequest * areq, struct AFSFetchStatus * Outsp)
2313 afs_uint32 start = 0;
2314 register struct conn *tc;
2315 struct AFSCallBack CallBack;
2316 struct AFSVolSync tsync;
2317 struct volume *volp;
2320 tc = afs_Conn(afid, areq, SHARED_LOCK);
2321 avc->dchint = NULL; /* invalidate hints */
2323 avc->callback = tc->srvr->server;
2325 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2328 RXAFS_FetchStatus(tc->id, (struct AFSFid *)&afid->Fid, Outsp,
2336 } while (afs_Analyze
2337 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2338 SHARED_LOCK, NULL));
2341 afs_ProcessFS(avc, Outsp, areq);
2342 volp = afs_GetVolume(afid, areq, READ_LOCK);
2343 ObtainWriteLock(&afs_xcbhash, 469);
2344 avc->states |= CTruth;
2345 if (avc->callback /* check for race */ ) {
2346 if (CallBack.ExpirationTime != 0) {
2347 avc->cbExpires = CallBack.ExpirationTime + start;
2348 avc->states |= CStatd;
2349 avc->states &= ~CBulkFetching;
2350 afs_QueueCallback(avc, CBHash(CallBack.ExpirationTime), volp);
2351 } else if (avc->states & CRO) { /* ordinary callback on a read-only volume -- AFS 3.2 style */
2352 avc->cbExpires = 3600 + start;
2353 avc->states |= CStatd;
2354 avc->states &= ~CBulkFetching;
2355 afs_QueueCallback(avc, CBHash(3600), volp);
2357 afs_DequeueCallback(avc);
2358 avc->callback = NULL;
2359 avc->states &= ~(CStatd | CUnique);
2360 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2361 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2364 afs_DequeueCallback(avc);
2365 avc->callback = NULL;
2366 avc->states &= ~(CStatd | CUnique);
2367 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2368 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2370 ReleaseWriteLock(&afs_xcbhash);
2372 afs_PutVolume(volp, READ_LOCK);
2374 /* used to undo the local callback, but that's too extreme.
2375 * There are plenty of good reasons that fetchstatus might return
2376 * an error, such as EPERM. If we have the vnode cached, statd,
2377 * with callback, might as well keep track of the fact that we
2378 * don't have access...
2380 if (code == EPERM || code == EACCES) {
2381 struct axscache *ac;
2382 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2384 else /* not found, add a new one if possible */
2385 afs_AddAxs(avc->Access, areq->uid, 0);
2396 * Stuff some information into the vcache for the given file.
2399 * afid : File in question.
2400 * OutStatus : Fetch status on the file.
2401 * CallBack : Callback info.
2402 * tc : RPC connection involved.
2403 * areq : vrequest involved.
2406 * Nothing interesting.
2409 afs_StuffVcache(register struct VenusFid *afid,
2410 struct AFSFetchStatus *OutStatus,
2411 struct AFSCallBack *CallBack, register struct conn *tc,
2412 struct vrequest *areq)
2414 register afs_int32 code, i, newvcache = 0;
2415 register struct vcache *tvc;
2416 struct AFSVolSync tsync;
2418 struct axscache *ac;
2421 AFS_STATCNT(afs_StuffVcache);
2422 #ifdef IFS_VCACHECOUNT
2427 ObtainSharedLock(&afs_xvcache, 8);
2429 tvc = afs_FindVCache(afid, &retry, DO_VLRU| IS_SLOCK /* no stats */ );
2431 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2432 ReleaseSharedLock(&afs_xvcache);
2433 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2439 /* no cache entry, better grab one */
2440 UpgradeSToWLock(&afs_xvcache, 25);
2441 tvc = afs_NewVCache(afid, NULL);
2443 ConvertWToSLock(&afs_xvcache);
2446 ReleaseSharedLock(&afs_xvcache);
2451 ReleaseSharedLock(&afs_xvcache);
2452 ObtainWriteLock(&tvc->lock, 58);
2454 tvc->states &= ~CStatd;
2455 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2456 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2458 /* Is it always appropriate to throw away all the access rights? */
2459 afs_FreeAllAxs(&(tvc->Access));
2461 /*Copy useful per-volume info */
2462 tvp = afs_GetVolume(afid, areq, READ_LOCK);
2464 if (newvcache && (tvp->states & VForeign))
2465 tvc->states |= CForeign;
2466 if (tvp->states & VRO)
2468 if (tvp->states & VBackup)
2469 tvc->states |= CBackup;
2471 * Now, copy ".." entry back out of volume structure, if
2474 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2476 tvc->mvid = (struct VenusFid *)
2477 osi_AllocSmallSpace(sizeof(struct VenusFid));
2478 *tvc->mvid = tvp->dotdot;
2481 /* store the stat on the file */
2482 afs_RemoveVCB(afid);
2483 afs_ProcessFS(tvc, OutStatus, areq);
2484 tvc->callback = tc->srvr->server;
2486 /* we use osi_Time twice below. Ideally, we would use the time at which
2487 * the FetchStatus call began, instead, but we don't have it here. So we
2488 * make do with "now". In the CRO case, it doesn't really matter. In
2489 * the other case, we hope that the difference between "now" and when the
2490 * call actually began execution on the server won't be larger than the
2491 * padding which the server keeps. Subtract 1 second anyway, to be on
2492 * the safe side. Can't subtract more because we don't know how big
2493 * ExpirationTime is. Possible consistency problems may arise if the call
2494 * timeout period becomes longer than the server's expiration padding. */
2495 ObtainWriteLock(&afs_xcbhash, 470);
2496 if (CallBack->ExpirationTime != 0) {
2497 tvc->cbExpires = CallBack->ExpirationTime + osi_Time() - 1;
2498 tvc->states |= CStatd;
2499 tvc->states &= ~CBulkFetching;
2500 afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2501 } else if (tvc->states & CRO) {
2502 /* old-fashioned AFS 3.2 style */
2503 tvc->cbExpires = 3600 + osi_Time();
2504 /*XXX*/ tvc->states |= CStatd;
2505 tvc->states &= ~CBulkFetching;
2506 afs_QueueCallback(tvc, CBHash(3600), tvp);
2508 afs_DequeueCallback(tvc);
2509 tvc->callback = NULL;
2510 tvc->states &= ~(CStatd | CUnique);
2511 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2512 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2514 ReleaseWriteLock(&afs_xcbhash);
2516 afs_PutVolume(tvp, READ_LOCK);
2518 /* look in per-pag cache */
2519 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2520 ac->axess = OutStatus->CallerAccess; /* substitute pags */
2521 else /* not found, add a new one if possible */
2522 afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2524 ReleaseWriteLock(&tvc->lock);
2525 afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2526 ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32,
2527 tvc->cbExpires, ICL_TYPE_INT32, tvc->cbExpires - osi_Time());
2529 * Release ref count... hope this guy stays around...
2532 } /*afs_StuffVcache */
2539 * Decrements the reference count on a cache entry.
2542 * avc : Pointer to the cache entry to decrement.
2545 * Nothing interesting.
2548 afs_PutVCache(register struct vcache *avc)
2550 AFS_STATCNT(afs_PutVCache);
2551 #ifdef AFS_DARWIN80_ENV
2552 vnode_put(AFSTOV(avc));
2556 * Can we use a read lock here?
2558 ObtainReadLock(&afs_xvcache);
2560 ReleaseReadLock(&afs_xvcache);
2562 } /*afs_PutVCache */
2565 static void findvc_sleep(struct vcache *avc, int flag) {
2566 if (flag & IS_SLOCK) {
2567 ReleaseSharedLock(&afs_xvcache);
2569 if (flag & IS_WLOCK) {
2570 ReleaseWriteLock(&afs_xvcache);
2572 ReleaseReadLock(&afs_xvcache);
2575 afs_osi_Sleep(&avc->states);
2576 if (flag & IS_SLOCK) {
2577 ObtainSharedLock(&afs_xvcache, 341);
2579 if (flag & IS_WLOCK) {
2580 ObtainWriteLock(&afs_xvcache, 343);
2582 ObtainReadLock(&afs_xvcache);
2590 * Find a vcache entry given a fid.
2593 * afid : Pointer to the fid whose cache entry we desire.
2594 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2595 * unlock the vnode, and try again.
2596 * flags: bit 1 to specify whether to compute hit statistics. Not
2597 * set if FindVCache is called as part of internal bookkeeping.
2600 * Must be called with the afs_xvcache lock at least held at
2601 * the read level. In order to do the VLRU adjustment, the xvcache lock
2602 * must be shared-- we upgrade it here.
2606 afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
2609 register struct vcache *tvc;
2611 #if defined( AFS_OSF_ENV)
2615 AFS_STATCNT(afs_FindVCache);
2619 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2620 if (FidMatches(afid, tvc)) {
2621 if (tvc->states & CVInit) {
2622 findvc_sleep(tvc, flag);
2626 /* Grab this vnode, possibly reactivating from the free list */
2628 vg = vget(AFSTOV(tvc));
2632 #endif /* AFS_OSF_ENV */
2633 #ifdef AFS_DARWIN80_ENV
2634 if (tvc->states & CDeadVnode) {
2635 findvc_sleep(tvc, flag);
2638 if (vnode_get(AFSTOV(tvc)))
2645 /* should I have a read lock on the vnode here? */
2649 #if !defined(AFS_OSF_ENV)
2650 osi_vnhold(tvc, retry); /* already held, above */
2651 if (retry && *retry)
2654 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
2655 tvc->states |= CUBCinit;
2657 if (UBCINFOMISSING(AFSTOV(tvc)) ||
2658 UBCINFORECLAIMED(AFSTOV(tvc))) {
2659 ubc_info_init(AFSTOV(tvc));
2662 tvc->states &= ~CUBCinit;
2665 * only move to front of vlru if we have proper vcache locking)
2667 if (flag & DO_VLRU) {
2668 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2669 refpanic("FindVC VLRU inconsistent1");
2671 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2672 refpanic("FindVC VLRU inconsistent1");
2674 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2675 refpanic("FindVC VLRU inconsistent2");
2677 UpgradeSToWLock(&afs_xvcache, 26);
2678 QRemove(&tvc->vlruq);
2679 QAdd(&VLRU, &tvc->vlruq);
2680 ConvertWToSLock(&afs_xvcache);
2681 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2682 refpanic("FindVC VLRU inconsistent1");
2684 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2685 refpanic("FindVC VLRU inconsistent2");
2687 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2688 refpanic("FindVC VLRU inconsistent3");
2694 if (flag & DO_STATS) {
2696 afs_stats_cmperf.vcacheHits++;
2698 afs_stats_cmperf.vcacheMisses++;
2699 if (afs_IsPrimaryCellNum(afid->Cell))
2700 afs_stats_cmperf.vlocalAccesses++;
2702 afs_stats_cmperf.vremoteAccesses++;
2705 } /*afs_FindVCache */
2711 * Find a vcache entry given a fid. Does a wildcard match on what we
2712 * have for the fid. If more than one entry, don't return anything.
2715 * avcp : Fill in pointer if we found one and only one.
2716 * afid : Pointer to the fid whose cache entry we desire.
2717 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2718 * unlock the vnode, and try again.
2719 * flags: bit 1 to specify whether to compute hit statistics. Not
2720 * set if FindVCache is called as part of internal bookkeeping.
2723 * Must be called with the afs_xvcache lock at least held at
2724 * the read level. In order to do the VLRU adjustment, the xvcache lock
2725 * must be shared-- we upgrade it here.
2728 * number of matches found.
2731 int afs_duplicate_nfs_fids = 0;
2734 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2736 register struct vcache *tvc;
2738 afs_int32 count = 0;
2739 struct vcache *found_tvc = NULL;
2744 AFS_STATCNT(afs_FindVCache);
2748 ObtainSharedLock(&afs_xvcache, 331);
2751 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2752 /* Match only on what we have.... */
2753 if (((tvc->fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2754 && (tvc->fid.Fid.Volume == afid->Fid.Volume)
2755 && ((tvc->fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2756 && (tvc->fid.Cell == afid->Cell)) {
2757 if (tvc->states & CVInit) {
2758 ReleaseSharedLock(&afs_xvcache);
2759 afs_osi_Sleep(&tvc->states);
2763 /* Grab this vnode, possibly reactivating from the free list */
2765 vg = vget(AFSTOV(tvc));
2768 /* This vnode no longer exists. */
2771 #endif /* AFS_OSF_ENV */
2772 #ifdef AFS_DARWIN80_ENV
2773 if (tvc->states & CDeadVnode) {
2774 ReleaseSharedLock(&afs_xvcache);
2775 afs_osi_Sleep(&tvc->states);
2778 if (vnode_get(AFSTOV(tvc))) {
2779 /* This vnode no longer exists. */
2782 #endif /* AFS_DARWIN80_ENV */
2787 /* Drop our reference counts. */
2789 vrele(AFSTOV(found_tvc));
2791 afs_duplicate_nfs_fids++;
2792 ReleaseSharedLock(&afs_xvcache);
2793 #ifdef AFS_DARWIN80_ENV
2794 /* Drop our reference counts. */
2795 vnode_put(AFSTOV(tvc));
2796 vnode_put(AFSTOV(found_tvc));
2805 /* should I have a read lock on the vnode here? */
2807 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2808 afs_int32 retry = 0;
2809 osi_vnhold(tvc, &retry);
2812 found_tvc = (struct vcache *)0;
2813 ReleaseSharedLock(&afs_xvcache);
2814 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2818 #if !defined(AFS_OSF_ENV)
2819 osi_vnhold(tvc, (int *)0); /* already held, above */
2823 * We obtained the xvcache lock above.
2825 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2826 refpanic("FindVC VLRU inconsistent1");
2828 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2829 refpanic("FindVC VLRU inconsistent1");
2831 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2832 refpanic("FindVC VLRU inconsistent2");
2834 UpgradeSToWLock(&afs_xvcache, 568);
2835 QRemove(&tvc->vlruq);
2836 QAdd(&VLRU, &tvc->vlruq);
2837 ConvertWToSLock(&afs_xvcache);
2838 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2839 refpanic("FindVC VLRU inconsistent1");
2841 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2842 refpanic("FindVC VLRU inconsistent2");
2844 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2845 refpanic("FindVC VLRU inconsistent3");
2851 afs_stats_cmperf.vcacheHits++;
2853 afs_stats_cmperf.vcacheMisses++;
2854 if (afs_IsPrimaryCellNum(afid->Cell))
2855 afs_stats_cmperf.vlocalAccesses++;
2857 afs_stats_cmperf.vremoteAccesses++;
2859 *avcp = tvc; /* May be null */
2861 ReleaseSharedLock(&afs_xvcache);
2862 return (tvc ? 1 : 0);
2864 } /*afs_NFSFindVCache */
2872 * Initialize vcache related variables
2875 afs_vcacheInit(int astatSize)
2877 register struct vcache *tvp;
2879 #if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
2880 if (!afs_maxvcount) {
2881 #if defined(AFS_LINUX22_ENV)
2882 afs_maxvcount = astatSize; /* no particular limit on linux? */
2883 #elif defined(AFS_OSF30_ENV)
2884 afs_maxvcount = max_vnodes / 2; /* limit ourselves to half the total */
2886 afs_maxvcount = nvnode / 2; /* limit ourselves to half the total */
2888 if (astatSize < afs_maxvcount) {
2889 afs_maxvcount = astatSize;
2892 #else /* AFS_OSF_ENV */
2896 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2897 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2899 #if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
2900 /* Allocate and thread the struct vcache entries */
2901 tvp = (struct vcache *)afs_osi_Alloc(astatSize * sizeof(struct vcache));
2902 memset((char *)tvp, 0, sizeof(struct vcache) * astatSize);
2904 Initial_freeVCList = tvp;
2905 freeVCList = &(tvp[0]);
2906 for (i = 0; i < astatSize - 1; i++) {
2907 tvp[i].nextfree = &(tvp[i + 1]);
2909 tvp[astatSize - 1].nextfree = NULL;
2910 #ifdef KERNEL_HAVE_PIN
2911 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
2915 #if defined(AFS_SGI_ENV)
2916 for (i = 0; i < astatSize; i++) {
2917 char name[METER_NAMSZ];
2918 struct vcache *tvc = &tvp[i];
2920 tvc->v.v_number = ++afsvnumbers;
2921 tvc->vc_rwlockid = OSI_NO_LOCKID;
2922 initnsema(&tvc->vc_rwlock, 1,
2923 makesname(name, "vrw", tvc->v.v_number));
2924 #ifndef AFS_SGI53_ENV
2925 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
2927 #ifndef AFS_SGI62_ENV
2928 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
2929 #endif /* AFS_SGI62_ENV */
2933 for(i = 0; i < VCSIZE; ++i)
2934 QInit(&afs_vhashTV[i]);
2942 shutdown_vcache(void)
2945 struct afs_cbr *tsp, *nsp;
2947 * XXX We may potentially miss some of the vcaches because if when there're no
2948 * free vcache entries and all the vcache entries are active ones then we allocate
2949 * an additional one - admittedly we almost never had that occur.
2953 register struct afs_q *tq, *uq;
2954 register struct vcache *tvc;
2955 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
2959 osi_FreeSmallSpace(tvc->mvid);
2960 tvc->mvid = (struct VenusFid *)0;
2963 aix_gnode_rele(AFSTOV(tvc));
2965 if (tvc->linkData) {
2966 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
2971 * Also free the remaining ones in the Cache
2973 for (i = 0; i < VCSIZE; i++) {
2974 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2976 osi_FreeSmallSpace(tvc->mvid);
2977 tvc->mvid = (struct VenusFid *)0;
2981 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
2982 #ifdef AFS_AIX32_ENV
2985 vms_delete(tvc->segid);
2987 tvc->segid = tvc->vmh = NULL;
2988 if (VREFCOUNT_GT(tvc,0))
2989 osi_Panic("flushVcache: vm race");
2997 #if defined(AFS_SUN5_ENV)
3003 if (tvc->linkData) {
3004 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3008 afs_FreeAllAxs(&(tvc->Access));
3014 * Free any leftover callback queue
3016 for (tsp = afs_cbrSpace; tsp; tsp = nsp) {
3018 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
3022 #ifdef KERNEL_HAVE_PIN
3023 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3025 #if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
3026 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3029 #if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
3030 freeVCList = Initial_freeVCList = 0;
3032 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
3033 LOCK_INIT(&afs_xvcb, "afs_xvcb");
3035 for(i = 0; i < VCSIZE; ++i)
3036 QInit(&afs_vhashTV[i]);