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);
605 if (tvc->states & (CVInit
606 #ifdef AFS_DARWIN80_ENV
610 tvc->states &= ~(CVInit
611 #ifdef AFS_DARWIN80_ENV
615 afs_osi_Wakeup(&tvc->states);
618 if (tmpReclaimedVCList)
619 ReclaimedVCList = tmpReclaimedVCList;
621 ReleaseWriteLock(&afs_xvreclaim);
629 * This routine is responsible for allocating a new cache entry
630 * from the free list. It formats the cache entry and inserts it
631 * into the appropriate hash tables. It must be called with
632 * afs_xvcache write-locked so as to prevent several processes from
633 * trying to create a new cache entry simultaneously.
636 * afid : The file id of the file whose cache entry is being
639 /* LOCK: afs_NewVCache afs_xvcache W */
641 afs_NewVCache(struct VenusFid *afid, struct server *serverp)
645 afs_int32 anumber = VCACHE_FREE;
647 struct gnode *gnodepnt;
651 #endif /* AFS_OSF_ENV */
652 struct afs_q *tq, *uq;
655 AFS_STATCNT(afs_NewVCache);
657 afs_FlushReclaimedVcaches();
659 #if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
660 #if defined(AFS_OSF30_ENV) || defined(AFS_LINUX22_ENV)
661 if (afs_vcount >= afs_maxvcount)
664 * If we are using > 33 % of the total system vnodes for AFS vcache
665 * entries or we are using the maximum number of vcache entries,
666 * then free some. (if our usage is > 33% we should free some, if
667 * our usage is > afs_maxvcount, set elsewhere to 0.5*nvnode,
668 * we _must_ free some -- no choice).
670 if (((3 * afs_vcount) > nvnode) || (afs_vcount >= afs_maxvcount))
677 for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
680 if (tvc->states & CVFlushed) {
681 refpanic("CVFlushed on VLRU");
682 } else if (i++ > afs_maxvcount) {
683 refpanic("Exceeded pool of AFS vnodes(VLRU cycle?)");
684 } else if (QNext(uq) != tq) {
685 refpanic("VLRU inconsistent");
686 } else if (!VREFCOUNT_GT(tvc,0)) {
687 refpanic("refcnt 0 on VLRU");
690 #if defined(AFS_LINUX22_ENV)
691 if (tvc != afs_globalVp && VREFCOUNT(tvc) > 1 && tvc->opens == 0) {
692 struct dentry *dentry;
693 struct list_head *cur, *head;
695 #if defined(AFS_LINUX24_ENV)
696 spin_lock(&dcache_lock);
698 head = &(AFSTOV(tvc))->i_dentry;
702 while ((cur = cur->next) != head) {
703 dentry = list_entry(cur, struct dentry, d_alias);
705 if (d_unhashed(dentry))
710 #if defined(AFS_LINUX24_ENV)
711 spin_unlock(&dcache_lock);
713 if (d_invalidate(dentry) == -EBUSY) {
715 /* perhaps lock and try to continue? (use cur as head?) */
719 #if defined(AFS_LINUX24_ENV)
720 spin_lock(&dcache_lock);
724 #if defined(AFS_LINUX24_ENV)
725 spin_unlock(&dcache_lock);
732 if (VREFCOUNT_GT(tvc,0) && !VREFCOUNT_GT(tvc,1) &&
734 && (tvc->states & CUnlinkedDel) == 0) {
735 code = afs_FlushVCache(tvc, &fv_slept);
742 continue; /* start over - may have raced. */
748 if (anumber == VCACHE_FREE) {
749 printf("afs_NewVCache: warning none freed, using %d of %d\n",
750 afs_vcount, afs_maxvcount);
751 if (afs_vcount >= afs_maxvcount) {
752 printf("afs_NewVCache - none freed\n");
758 #if defined(AFS_LINUX22_ENV)
763 ip = new_inode(afs_globalVFS);
765 osi_Panic("afs_NewVCache: no more inodes");
767 #if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
770 tvc = afs_osi_Alloc(sizeof(struct vcache));
771 ip->u.generic_ip = tvc;
777 if (getnewvnode(MOUNT_AFS, &Afs_vnodeops, &nvc)) {
778 /* What should we do ???? */
779 osi_Panic("afs_NewVCache: no more vnodes");
784 tvc->nextfree = NULL;
787 #else /* AFS_OSF_ENV */
788 /* pull out a free cache entry */
792 for (tq = VLRU.prev; (anumber > 0) && (tq != &VLRU); tq = uq) {
796 if (tvc->states & CVFlushed) {
797 refpanic("CVFlushed on VLRU");
798 } else if (i++ > 2 * afs_cacheStats) { /* even allowing for a few xallocs... */
799 refpanic("Increase -stat parameter of afsd(VLRU cycle?)");
800 } else if (QNext(uq) != tq) {
801 refpanic("VLRU inconsistent");
802 } else if (tvc->states & CVInit) {
806 if (!VREFCOUNT_GT(tvc,0)
807 #if defined(AFS_DARWIN_ENV) && !defined(UKERNEL) && !defined(AFS_DARWIN80_ENV)
808 || ((VREFCOUNT(tvc) == 1) &&
809 (UBCINFOEXISTS(AFSTOV(tvc))))
811 && tvc->opens == 0 && (tvc->states & CUnlinkedDel) == 0) {
812 #if defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
813 #ifdef AFS_DARWIN80_ENV
814 vnode_t tvp = AFSTOV(tvc);
815 /* VREFCOUNT_GT only sees usecounts, not iocounts */
816 /* so this may fail to actually recycle the vnode now */
817 /* must call vnode_get to avoid races. */
819 if (vnode_get(tvp) == 0) {
821 /* must release lock, since vnode_put will immediately
822 reclaim if there are no other users */
823 ReleaseWriteLock(&afs_xvcache);
828 ObtainWriteLock(&afs_xvcache, 336);
830 /* we can't use the vnode_recycle return value to figure
831 * this out, since the iocount we have to hold makes it
833 if (AFSTOV(tvc) == tvp) {
834 if (anumber > 0 && fv_slept) {
835 QRemove(&tvc->vlruq);
836 QAdd(&VLRU, &tvc->vlruq);
843 * vgone() reclaims the vnode, which calls afs_FlushVCache(),
844 * then it puts the vnode on the free list.
845 * If we don't do this we end up with a cleaned vnode that's
846 * not on the free list.
847 * XXX assume FreeBSD is the same for now.
856 code = afs_FlushVCache(tvc, &fv_slept);
866 continue; /* start over - may have raced. */
874 /* none free, making one is better than a panic */
875 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
876 tvc = (struct vcache *)afs_osi_Alloc(sizeof(struct vcache));
877 #if defined(AFS_DARWIN_ENV) && !defined(UKERNEL)
878 tvc->v = NULL; /* important to clean this, or use memset 0 */
880 #ifdef KERNEL_HAVE_PIN
881 pin((char *)tvc, sizeof(struct vcache)); /* XXX */
883 #if defined(AFS_SGI_ENV)
885 char name[METER_NAMSZ];
886 memset(tvc, 0, sizeof(struct vcache));
887 tvc->v.v_number = ++afsvnumbers;
888 tvc->vc_rwlockid = OSI_NO_LOCKID;
889 initnsema(&tvc->vc_rwlock, 1,
890 makesname(name, "vrw", tvc->v.v_number));
891 #ifndef AFS_SGI53_ENV
892 initnsema(&tvc->v.v_sync, 0,
893 makesname(name, "vsy", tvc->v.v_number));
895 #ifndef AFS_SGI62_ENV
896 initnlock(&tvc->v.v_lock,
897 makesname(name, "vlk", tvc->v.v_number));
900 #endif /* AFS_SGI_ENV */
902 tvc = freeVCList; /* take from free list */
903 freeVCList = tvc->nextfree;
904 tvc->nextfree = NULL;
906 #endif /* AFS_OSF_ENV */
908 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
910 panic("afs_NewVCache(): free vcache with vnode attached");
913 #if !defined(AFS_SGI_ENV) && !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
914 memset((char *)tvc, 0, sizeof(struct vcache));
919 RWLOCK_INIT(&tvc->lock, "vcache lock");
920 #if defined(AFS_SUN5_ENV)
921 RWLOCK_INIT(&tvc->vlock, "vcache vlock");
922 #endif /* defined(AFS_SUN5_ENV) */
924 tvc->parentVnode = 0;
926 tvc->linkData = NULL;
929 tvc->execsOrWriters = 0;
932 tvc->states = CVInit;
933 tvc->last_looker = 0;
935 tvc->asynchrony = -1;
938 tvc->flushDV.low = tvc->flushDV.high = AFS_MAXDV;
941 tvc->truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
942 hzero(tvc->m.DataVersion); /* in case we copy it into flushDV */
944 tvc->callback = serverp; /* to minimize chance that clear
950 tvc->hnext = afs_vhashT[i];
952 QAdd(&afs_vhashTV[j], &tvc->vhashq);
954 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
955 refpanic("NewVCache VLRU inconsistent");
957 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
958 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
959 refpanic("NewVCache VLRU inconsistent2");
961 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
962 refpanic("NewVCache VLRU inconsistent3");
964 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
965 refpanic("NewVCache VLRU inconsistent4");
968 /* it should now be safe to drop the xvcache lock */
970 ReleaseWriteLock(&afs_xvcache);
972 afs_nbsd_getnewvnode(tvc); /* includes one refcount */
974 ObtainWriteLock(&afs_xvcache,337);
975 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
977 #ifdef AFS_DARWIN_ENV
978 ReleaseWriteLock(&afs_xvcache);
980 afs_darwin_getnewvnode(tvc); /* includes one refcount */
982 ObtainWriteLock(&afs_xvcache,338);
983 #ifdef AFS_DARWIN80_ENV
984 LOCKINIT(tvc->rwlock);
986 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
993 ReleaseWriteLock(&afs_xvcache);
995 #if defined(AFS_FBSD60_ENV)
996 if (getnewvnode(MOUNT_AFS, afs_globalVFS, &afs_vnodeops, &vp))
997 #elif defined(AFS_FBSD50_ENV)
998 if (getnewvnode(MOUNT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
1000 if (getnewvnode(VT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
1002 panic("afs getnewvnode"); /* can't happen */
1004 ObtainWriteLock(&afs_xvcache,339);
1005 if (tvc->v != NULL) {
1006 /* I'd like to know if this ever happens...
1007 * We don't drop global for the rest of this function,
1008 * so if we do lose the race, the other thread should
1009 * have found the same vnode and finished initializing
1010 * the vcache entry. Is it conceivable that this vcache
1011 * entry could be recycled during this interval? If so,
1012 * then there probably needs to be some sort of additional
1013 * mutual exclusion (an Embryonic flag would suffice).
1015 printf("afs_NewVCache: lost the race\n");
1019 tvc->v->v_data = tvc;
1020 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
1024 #if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
1025 /* Hold it for the LRU (should make count 2) */
1026 VN_HOLD(AFSTOV(tvc));
1027 #else /* AFS_OSF_ENV */
1028 #if !(defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV))
1029 VREFCOUNT_SET(tvc, 1); /* us */
1030 #endif /* AFS_XBSD_ENV */
1031 #endif /* AFS_OSF_ENV */
1032 #ifdef AFS_AIX32_ENV
1033 LOCK_INIT(&tvc->pvmlock, "vcache pvmlock");
1034 tvc->vmh = tvc->segid = NULL;
1037 #ifdef AFS_BOZONLOCK_ENV
1038 #if defined(AFS_SUN5_ENV)
1039 rw_init(&tvc->rwlock, "vcache rwlock", RW_DEFAULT, NULL);
1041 #if defined(AFS_SUN55_ENV)
1042 /* This is required if the kaio (kernel aynchronous io)
1043 ** module is installed. Inside the kernel, the function
1044 ** check_vp( common/os/aio.c) checks to see if the kernel has
1045 ** to provide asynchronous io for this vnode. This
1046 ** function extracts the device number by following the
1047 ** v_data field of the vnode. If we do not set this field
1048 ** then the system panics. The value of the v_data field
1049 ** is not really important for AFS vnodes because the kernel
1050 ** does not do asynchronous io for regular files. Hence,
1051 ** for the time being, we fill up the v_data field with the
1052 ** vnode pointer itself. */
1053 tvc->v.v_data = (char *)tvc;
1054 #endif /* AFS_SUN55_ENV */
1056 afs_BozonInit(&tvc->pvnLock, tvc);
1059 /* initialize vnode data, note vrefCount is v.v_count */
1061 /* Don't forget to free the gnode space */
1062 tvc->v.v_gnode = gnodepnt =
1063 (struct gnode *)osi_AllocSmallSpace(sizeof(struct gnode));
1064 memset((char *)gnodepnt, 0, sizeof(struct gnode));
1066 #ifdef AFS_SGI64_ENV
1067 memset((void *)&(tvc->vc_bhv_desc), 0, sizeof(tvc->vc_bhv_desc));
1068 bhv_desc_init(&(tvc->vc_bhv_desc), tvc, tvc, &Afs_vnodeops);
1069 #ifdef AFS_SGI65_ENV
1070 vn_bhv_head_init(&(tvc->v.v_bh), "afsvp");
1071 vn_bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
1073 bhv_head_init(&(tvc->v.v_bh));
1074 bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
1076 #ifdef AFS_SGI65_ENV
1077 tvc->v.v_mreg = tvc->v.v_mregb = (struct pregion *)tvc;
1078 #ifdef VNODE_TRACING
1079 tvc->v.v_trace = ktrace_alloc(VNODE_TRACE_SIZE, 0);
1081 init_bitlock(&tvc->v.v_pcacheflag, VNODE_PCACHE_LOCKBIT, "afs_pcache",
1083 init_mutex(&tvc->v.v_filocksem, MUTEX_DEFAULT, "afsvfl", (long)tvc);
1084 init_mutex(&tvc->v.v_buf_lock, MUTEX_DEFAULT, "afsvnbuf", (long)tvc);
1086 vnode_pcache_init(&tvc->v);
1087 #if defined(DEBUG) && defined(VNODE_INIT_BITLOCK)
1088 /* Above define is never true execpt in SGI test kernels. */
1089 init_bitlock(&(tvc->v.v_flag, VLOCK, "vnode", tvc->v.v_number);
1091 #ifdef INTR_KTHREADS
1092 AFS_VN_INIT_BUF_LOCK(&(tvc->v));
1095 SetAfsVnode(AFSTOV(tvc));
1096 #endif /* AFS_SGI64_ENV */
1098 * The proper value for mvstat (for root fids) is setup by the caller.
1101 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
1103 if (afs_globalVFS == 0)
1104 osi_Panic("afs globalvfs");
1105 #if !defined(AFS_LINUX22_ENV)
1106 vSetVfsp(tvc, afs_globalVFS);
1108 vSetType(tvc, VREG);
1110 tvc->v.v_vfsnext = afs_globalVFS->vfs_vnodes; /* link off vfs */
1111 tvc->v.v_vfsprev = NULL;
1112 afs_globalVFS->vfs_vnodes = &tvc->v;
1113 if (tvc->v.v_vfsnext != NULL)
1114 tvc->v.v_vfsnext->v_vfsprev = &tvc->v;
1115 tvc->v.v_next = gnodepnt->gn_vnode; /*Single vnode per gnode for us! */
1116 gnodepnt->gn_vnode = &tvc->v;
1118 #if defined(AFS_DUX40_ENV)
1119 insmntque(tvc, afs_globalVFS, &afs_ubcops);
1122 /* Is this needed??? */
1123 insmntque(tvc, afs_globalVFS);
1124 #endif /* AFS_OSF_ENV */
1125 #endif /* AFS_DUX40_ENV */
1126 #if defined(AFS_SGI_ENV)
1127 VN_SET_DPAGES(&(tvc->v), (struct pfdat *)NULL);
1128 osi_Assert((tvc->v.v_flag & VINACT) == 0);
1130 osi_Assert(VN_GET_PGCNT(&(tvc->v)) == 0);
1131 osi_Assert(tvc->mapcnt == 0 && tvc->vc_locktrips == 0);
1132 osi_Assert(tvc->vc_rwlockid == OSI_NO_LOCKID);
1133 osi_Assert(tvc->v.v_filocks == NULL);
1134 #if !defined(AFS_SGI65_ENV)
1135 osi_Assert(tvc->v.v_filocksem == NULL);
1137 osi_Assert(tvc->cred == NULL);
1138 #ifdef AFS_SGI64_ENV
1139 vnode_pcache_reinit(&tvc->v);
1140 tvc->v.v_rdev = NODEV;
1142 vn_initlist((struct vnlist *)&tvc->v);
1144 #endif /* AFS_SGI_ENV */
1146 osi_dnlc_purgedp(tvc); /* this may be overkill */
1147 memset((char *)&(tvc->callsort), 0, sizeof(struct afs_q));
1149 tvc->states &=~ CVInit;
1150 afs_osi_Wakeup(&tvc->states);
1154 } /*afs_NewVCache */
1158 * afs_FlushActiveVcaches
1164 * doflocks : Do we handle flocks?
1166 /* LOCK: afs_FlushActiveVcaches afs_xvcache N */
1168 afs_FlushActiveVcaches(register afs_int32 doflocks)
1170 register struct vcache *tvc;
1172 register struct conn *tc;
1173 register afs_int32 code;
1174 register struct AFS_UCRED *cred = NULL;
1175 struct vrequest treq, ureq;
1176 struct AFSVolSync tsync;
1179 AFS_STATCNT(afs_FlushActiveVcaches);
1180 ObtainReadLock(&afs_xvcache);
1181 for (i = 0; i < VCSIZE; i++) {
1182 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1183 if (tvc->states & CVInit) continue;
1184 #ifdef AFS_DARWIN80_ENV
1185 if (tvc->states & CDeadVnode &&
1186 (tvc->states & (CCore|CUnlinkedDel) ||
1187 tvc->flockCount)) panic("Dead vnode has core/unlinkedel/flock");
1189 if (doflocks && tvc->flockCount != 0) {
1190 /* if this entry has an flock, send a keep-alive call out */
1192 ReleaseReadLock(&afs_xvcache);
1193 ObtainWriteLock(&tvc->lock, 51);
1195 afs_InitReq(&treq, afs_osi_credp);
1196 treq.flags |= O_NONBLOCK;
1198 tc = afs_Conn(&tvc->fid, &treq, SHARED_LOCK);
1200 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
1203 RXAFS_ExtendLock(tc->id,
1204 (struct AFSFid *)&tvc->fid.Fid,
1210 } while (afs_Analyze
1211 (tc, code, &tvc->fid, &treq,
1212 AFS_STATS_FS_RPCIDX_EXTENDLOCK, SHARED_LOCK, NULL));
1214 ReleaseWriteLock(&tvc->lock);
1215 #ifdef AFS_DARWIN80_ENV
1217 ObtainReadLock(&afs_xvcache);
1219 ObtainReadLock(&afs_xvcache);
1224 if ((tvc->states & CCore) || (tvc->states & CUnlinkedDel)) {
1226 * Don't let it evaporate in case someone else is in
1227 * this code. Also, drop the afs_xvcache lock while
1228 * getting vcache locks.
1231 ReleaseReadLock(&afs_xvcache);
1232 #ifdef AFS_BOZONLOCK_ENV
1233 afs_BozonLock(&tvc->pvnLock, tvc);
1235 #if defined(AFS_SGI_ENV)
1237 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1239 osi_Assert(VREFCOUNT_GT(tvc,0));
1240 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1242 ObtainWriteLock(&tvc->lock, 52);
1243 if (tvc->states & CCore) {
1244 tvc->states &= ~CCore;
1245 /* XXXX Find better place-holder for cred XXXX */
1246 cred = (struct AFS_UCRED *)tvc->linkData;
1247 tvc->linkData = NULL; /* XXX */
1248 afs_InitReq(&ureq, cred);
1249 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1250 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
1251 tvc->execsOrWriters);
1252 code = afs_StoreOnLastReference(tvc, &ureq);
1253 ReleaseWriteLock(&tvc->lock);
1254 #ifdef AFS_BOZONLOCK_ENV
1255 afs_BozonUnlock(&tvc->pvnLock, tvc);
1257 hzero(tvc->flushDV);
1260 if (code && code != VNOVNODE) {
1261 afs_StoreWarn(code, tvc->fid.Fid.Volume,
1262 /* /dev/console */ 1);
1264 } else if (tvc->states & CUnlinkedDel) {
1268 ReleaseWriteLock(&tvc->lock);
1269 #ifdef AFS_BOZONLOCK_ENV
1270 afs_BozonUnlock(&tvc->pvnLock, tvc);
1272 #if defined(AFS_SGI_ENV)
1273 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1275 afs_remunlink(tvc, 0);
1276 #if defined(AFS_SGI_ENV)
1277 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1280 /* lost (or won, perhaps) the race condition */
1281 ReleaseWriteLock(&tvc->lock);
1282 #ifdef AFS_BOZONLOCK_ENV
1283 afs_BozonUnlock(&tvc->pvnLock, tvc);
1286 #if defined(AFS_SGI_ENV)
1287 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1289 #ifdef AFS_DARWIN80_ENV
1292 AFS_RELE(AFSTOV(tvc));
1293 /* Matches write code setting CCore flag */
1296 ObtainReadLock(&afs_xvcache);
1298 ObtainReadLock(&afs_xvcache);
1301 AFS_RELE(AFSTOV(tvc));
1302 /* Matches write code setting CCore flag */
1309 ReleaseReadLock(&afs_xvcache);
1318 * Make sure a cache entry is up-to-date status-wise.
1320 * NOTE: everywhere that calls this can potentially be sped up
1321 * by checking CStatd first, and avoiding doing the InitReq
1322 * if this is up-to-date.
1324 * Anymore, the only places that call this KNOW already that the
1325 * vcache is not up-to-date, so we don't screw around.
1328 * avc : Ptr to vcache entry to verify.
1333 afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1335 register struct vcache *tvc;
1337 AFS_STATCNT(afs_VerifyVCache);
1339 #if defined(AFS_OSF_ENV)
1340 ObtainReadLock(&avc->lock);
1341 if (afs_IsWired(avc)) {
1342 ReleaseReadLock(&avc->lock);
1345 ReleaseReadLock(&avc->lock);
1346 #endif /* AFS_OSF_ENV */
1347 /* otherwise we must fetch the status info */
1349 ObtainWriteLock(&avc->lock, 53);
1350 if (avc->states & CStatd) {
1351 ReleaseWriteLock(&avc->lock);
1354 ObtainWriteLock(&afs_xcbhash, 461);
1355 avc->states &= ~(CStatd | CUnique);
1356 avc->callback = NULL;
1357 afs_DequeueCallback(avc);
1358 ReleaseWriteLock(&afs_xcbhash);
1359 ReleaseWriteLock(&avc->lock);
1361 /* since we've been called back, or the callback has expired,
1362 * it's possible that the contents of this directory, or this
1363 * file's name have changed, thus invalidating the dnlc contents.
1365 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1366 osi_dnlc_purgedp(avc);
1368 osi_dnlc_purgevp(avc);
1370 /* fetch the status info */
1371 tvc = afs_GetVCache(&avc->fid, areq, NULL, avc);
1374 /* Put it back; caller has already incremented vrefCount */
1378 } /*afs_VerifyVCache */
1385 * Simple copy of stat info into cache.
1388 * avc : Ptr to vcache entry involved.
1389 * astat : Ptr to stat info to copy.
1392 * Nothing interesting.
1394 * Callers: as of 1992-04-29, only called by WriteVCache
1397 afs_SimpleVStat(register struct vcache *avc,
1398 register struct AFSFetchStatus *astat, struct vrequest *areq)
1401 AFS_STATCNT(afs_SimpleVStat);
1404 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1405 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1407 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1409 #ifdef AFS_64BIT_CLIENT
1410 FillInt64(length, astat->Length_hi, astat->Length);
1411 #else /* AFS_64BIT_CLIENT */
1412 length = astat->Length;
1413 #endif /* AFS_64BIT_CLIENT */
1414 #if defined(AFS_SGI_ENV)
1415 osi_Assert((valusema(&avc->vc_rwlock) <= 0)
1416 && (OSI_GET_LOCKID() == avc->vc_rwlockid));
1417 if (length < avc->m.Length) {
1418 vnode_t *vp = (vnode_t *) avc;
1420 osi_Assert(WriteLocked(&avc->lock));
1421 ReleaseWriteLock(&avc->lock);
1423 PTOSSVP(vp, (off_t) length, (off_t) MAXLONG);
1425 ObtainWriteLock(&avc->lock, 67);
1428 /* if writing the file, don't fetch over this value */
1429 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT, ICL_TYPE_POINTER, avc,
1430 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1431 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1432 avc->m.Length = length;
1433 avc->m.Date = astat->ClientModTime;
1435 avc->m.Owner = astat->Owner;
1436 avc->m.Group = astat->Group;
1437 avc->m.Mode = astat->UnixModeBits;
1438 if (vType(avc) == VREG) {
1439 avc->m.Mode |= S_IFREG;
1440 } else if (vType(avc) == VDIR) {
1441 avc->m.Mode |= S_IFDIR;
1442 } else if (vType(avc) == VLNK) {
1443 avc->m.Mode |= S_IFLNK;
1444 if ((avc->m.Mode & 0111) == 0)
1447 if (avc->states & CForeign) {
1448 struct axscache *ac;
1449 avc->anyAccess = astat->AnonymousAccess;
1451 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1453 * Caller has at least one bit not covered by anonymous, and
1454 * thus may have interesting rights.
1456 * HOWEVER, this is a really bad idea, because any access query
1457 * for bits which aren't covered by anonymous, on behalf of a user
1458 * who doesn't have any special rights, will result in an answer of
1459 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1460 * It's an especially bad idea under Ultrix, since (due to the lack of
1461 * a proper access() call) it must perform several afs_access() calls
1462 * in order to create magic mode bits that vary according to who makes
1463 * the call. In other words, _every_ stat() generates a test for
1466 #endif /* badidea */
1467 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1468 ac->axess = astat->CallerAccess;
1469 else /* not found, add a new one if possible */
1470 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1474 } /*afs_SimpleVStat */
1481 * Store the status info *only* back to the server for a
1485 * avc : Ptr to the vcache entry.
1486 * astatus : Ptr to the status info to store.
1487 * areq : Ptr to the associated vrequest.
1490 * Must be called with a shared lock held on the vnode.
1494 afs_WriteVCache(register struct vcache *avc,
1495 register struct AFSStoreStatus *astatus,
1496 struct vrequest *areq)
1500 struct AFSFetchStatus OutStatus;
1501 struct AFSVolSync tsync;
1503 AFS_STATCNT(afs_WriteVCache);
1504 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1505 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
1508 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
1510 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1513 RXAFS_StoreStatus(tc->id, (struct AFSFid *)&avc->fid.Fid,
1514 astatus, &OutStatus, &tsync);
1519 } while (afs_Analyze
1520 (tc, code, &avc->fid, areq, AFS_STATS_FS_RPCIDX_STORESTATUS,
1521 SHARED_LOCK, NULL));
1523 UpgradeSToWLock(&avc->lock, 20);
1525 /* success, do the changes locally */
1526 afs_SimpleVStat(avc, &OutStatus, areq);
1528 * Update the date, too. SimpleVStat didn't do this, since
1529 * it thought we were doing this after fetching new status
1530 * over a file being written.
1532 avc->m.Date = OutStatus.ClientModTime;
1534 /* failure, set up to check with server next time */
1535 ObtainWriteLock(&afs_xcbhash, 462);
1536 afs_DequeueCallback(avc);
1537 avc->states &= ~(CStatd | CUnique); /* turn off stat valid flag */
1538 ReleaseWriteLock(&afs_xcbhash);
1539 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1540 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
1542 ConvertWToSLock(&avc->lock);
1545 } /*afs_WriteVCache */
1551 * Copy astat block into vcache info
1554 * avc : Ptr to vcache entry.
1555 * astat : Ptr to stat block to copy in.
1556 * areq : Ptr to associated request.
1559 * Must be called under a write lock
1561 * Note: this code may get dataversion and length out of sync if the file has
1562 * been modified. This is less than ideal. I haven't thought about
1563 * it sufficiently to be certain that it is adequate.
1566 afs_ProcessFS(register struct vcache *avc,
1567 register struct AFSFetchStatus *astat, struct vrequest *areq)
1570 AFS_STATCNT(afs_ProcessFS);
1572 #ifdef AFS_64BIT_CLIENT
1573 FillInt64(length, astat->Length_hi, astat->Length);
1574 #else /* AFS_64BIT_CLIENT */
1575 length = astat->Length;
1576 #endif /* AFS_64BIT_CLIENT */
1577 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1578 * number for each bulk status request. Under no circumstances
1579 * should afs_DoBulkStat store a sequence number if the new
1580 * length will be ignored when afs_ProcessFS is called with
1581 * new stats. If you change the following conditional then you
1582 * also need to change the conditional in afs_DoBulkStat. */
1584 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1585 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1587 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1589 /* if we're writing or mapping this file, don't fetch over these
1592 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1593 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1594 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1595 avc->m.Length = length;
1596 avc->m.Date = astat->ClientModTime;
1598 hset64(avc->m.DataVersion, astat->dataVersionHigh, astat->DataVersion);
1599 avc->m.Owner = astat->Owner;
1600 avc->m.Mode = astat->UnixModeBits;
1601 avc->m.Group = astat->Group;
1602 avc->m.LinkCount = astat->LinkCount;
1603 if (astat->FileType == File) {
1604 vSetType(avc, VREG);
1605 avc->m.Mode |= S_IFREG;
1606 } else if (astat->FileType == Directory) {
1607 vSetType(avc, VDIR);
1608 avc->m.Mode |= S_IFDIR;
1609 } else if (astat->FileType == SymbolicLink) {
1610 if (afs_fakestat_enable && (avc->m.Mode & 0111) == 0) {
1611 vSetType(avc, VDIR);
1612 avc->m.Mode |= S_IFDIR;
1614 vSetType(avc, VLNK);
1615 avc->m.Mode |= S_IFLNK;
1617 if ((avc->m.Mode & 0111) == 0) {
1621 avc->anyAccess = astat->AnonymousAccess;
1623 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1625 * Caller has at least one bit not covered by anonymous, and
1626 * thus may have interesting rights.
1628 * HOWEVER, this is a really bad idea, because any access query
1629 * for bits which aren't covered by anonymous, on behalf of a user
1630 * who doesn't have any special rights, will result in an answer of
1631 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1632 * It's an especially bad idea under Ultrix, since (due to the lack of
1633 * a proper access() call) it must perform several afs_access() calls
1634 * in order to create magic mode bits that vary according to who makes
1635 * the call. In other words, _every_ stat() generates a test for
1638 #endif /* badidea */
1640 struct axscache *ac;
1641 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1642 ac->axess = astat->CallerAccess;
1643 else /* not found, add a new one if possible */
1644 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1646 } /*afs_ProcessFS */
1650 afs_RemoteLookup(register struct VenusFid *afid, struct vrequest *areq,
1651 char *name, struct VenusFid *nfid,
1652 struct AFSFetchStatus *OutStatusp,
1653 struct AFSCallBack *CallBackp, struct server **serverp,
1654 struct AFSVolSync *tsyncp)
1658 register struct conn *tc;
1659 struct AFSFetchStatus OutDirStatus;
1662 name = ""; /* XXX */
1664 tc = afs_Conn(afid, areq, SHARED_LOCK);
1667 *serverp = tc->srvr->server;
1669 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1672 RXAFS_Lookup(tc->id, (struct AFSFid *)&afid->Fid, name,
1673 (struct AFSFid *)&nfid->Fid, OutStatusp,
1674 &OutDirStatus, CallBackp, tsyncp);
1679 } while (afs_Analyze
1680 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_XLOOKUP, SHARED_LOCK,
1691 * Given a file id and a vrequest structure, fetch the status
1692 * information associated with the file.
1696 * areq : Ptr to associated vrequest structure, specifying the
1697 * user whose authentication tokens will be used.
1698 * avc : caller may already have a vcache for this file, which is
1702 * The cache entry is returned with an increased vrefCount field.
1703 * The entry must be discarded by calling afs_PutVCache when you
1704 * are through using the pointer to the cache entry.
1706 * You should not hold any locks when calling this function, except
1707 * locks on other vcache entries. If you lock more than one vcache
1708 * entry simultaneously, you should lock them in this order:
1710 * 1. Lock all files first, then directories.
1711 * 2. Within a particular type, lock entries in Fid.Vnode order.
1713 * This locking hierarchy is convenient because it allows locking
1714 * of a parent dir cache entry, given a file (to check its access
1715 * control list). It also allows renames to be handled easily by
1716 * locking directories in a constant order.
1717 * NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1719 /* might have a vcache structure already, which must
1720 * already be held by the caller */
1723 afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
1724 afs_int32 * cached, struct vcache *avc)
1727 afs_int32 code, newvcache = 0;
1728 register struct vcache *tvc;
1732 AFS_STATCNT(afs_GetVCache);
1735 *cached = 0; /* Init just in case */
1737 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1741 ObtainSharedLock(&afs_xvcache, 5);
1743 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU | IS_SLOCK);
1745 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1746 ReleaseSharedLock(&afs_xvcache);
1747 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1755 osi_Assert((tvc->states & CVInit) == 0);
1756 if (tvc->states & CStatd) {
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;
1900 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1905 ReleaseWriteLock(&tvc->lock);
1911 ReleaseWriteLock(&tvc->lock);
1914 } /*afs_GetVCache */
1919 afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1920 afs_int32 * cached, struct vcache *adp, char *aname)
1922 afs_int32 code, now, newvcache = 0;
1923 struct VenusFid nfid;
1924 register struct vcache *tvc;
1926 struct AFSFetchStatus OutStatus;
1927 struct AFSCallBack CallBack;
1928 struct AFSVolSync tsync;
1929 struct server *serverp = 0;
1933 AFS_STATCNT(afs_GetVCache);
1935 *cached = 0; /* Init just in case */
1937 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1941 ObtainReadLock(&afs_xvcache);
1942 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */ );
1945 ReleaseReadLock(&afs_xvcache);
1947 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1948 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1952 ObtainReadLock(&tvc->lock);
1954 if (tvc->states & CStatd) {
1958 ReleaseReadLock(&tvc->lock);
1961 tvc->states &= ~CUnique;
1963 ReleaseReadLock(&tvc->lock);
1965 ObtainReadLock(&afs_xvcache);
1968 ReleaseReadLock(&afs_xvcache);
1970 /* lookup the file */
1973 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
1975 afs_RemoteLookup(&adp->fid, areq, aname, &nfid, &OutStatus, &CallBack,
1978 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1982 ObtainSharedLock(&afs_xvcache, 6);
1983 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU | IS_SLOCK/* no xstats now */ );
1985 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1986 ReleaseSharedLock(&afs_xvcache);
1987 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1993 /* no cache entry, better grab one */
1994 UpgradeSToWLock(&afs_xvcache, 22);
1995 tvc = afs_NewVCache(&nfid, serverp);
1997 ConvertWToSLock(&afs_xvcache);
2000 ReleaseSharedLock(&afs_xvcache);
2005 ReleaseSharedLock(&afs_xvcache);
2006 ObtainWriteLock(&tvc->lock, 55);
2008 /* It is always appropriate to throw away all the access rights? */
2009 afs_FreeAllAxs(&(tvc->Access));
2010 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
2012 if ((tvp->states & VForeign)) {
2014 tvc->states |= CForeign;
2015 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
2016 && (tvp->rootUnique == afid->Fid.Unique))
2019 if (tvp->states & VRO)
2021 if (tvp->states & VBackup)
2022 tvc->states |= CBackup;
2023 /* now copy ".." entry back out of volume structure, if necessary */
2024 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2026 tvc->mvid = (struct VenusFid *)
2027 osi_AllocSmallSpace(sizeof(struct VenusFid));
2028 *tvc->mvid = tvp->dotdot;
2033 ObtainWriteLock(&afs_xcbhash, 465);
2034 afs_DequeueCallback(tvc);
2035 tvc->states &= ~(CStatd | CUnique);
2036 ReleaseWriteLock(&afs_xcbhash);
2037 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2038 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2040 afs_PutVolume(tvp, READ_LOCK);
2041 ReleaseWriteLock(&tvc->lock);
2046 ObtainWriteLock(&afs_xcbhash, 466);
2047 if (origCBs == afs_allCBs) {
2048 if (CallBack.ExpirationTime) {
2049 tvc->callback = serverp;
2050 tvc->cbExpires = CallBack.ExpirationTime + now;
2051 tvc->states |= CStatd | CUnique;
2052 tvc->states &= ~CBulkFetching;
2053 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
2054 } else if (tvc->states & CRO) {
2055 /* adapt gives us an hour. */
2056 tvc->cbExpires = 3600 + osi_Time();
2057 /*XXX*/ tvc->states |= CStatd | CUnique;
2058 tvc->states &= ~CBulkFetching;
2059 afs_QueueCallback(tvc, CBHash(3600), tvp);
2061 tvc->callback = NULL;
2062 afs_DequeueCallback(tvc);
2063 tvc->states &= ~(CStatd | CUnique);
2064 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2065 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2068 afs_DequeueCallback(tvc);
2069 tvc->states &= ~CStatd;
2070 tvc->states &= ~CUnique;
2071 tvc->callback = NULL;
2072 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2073 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2075 ReleaseWriteLock(&afs_xcbhash);
2077 afs_PutVolume(tvp, READ_LOCK);
2078 afs_ProcessFS(tvc, &OutStatus, areq);
2080 ReleaseWriteLock(&tvc->lock);
2086 afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
2087 afs_int32 * cached, struct volume *tvolp)
2089 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
2090 afs_int32 getNewFid = 0;
2092 struct VenusFid nfid;
2093 register struct vcache *tvc;
2094 struct server *serverp = 0;
2095 struct AFSFetchStatus OutStatus;
2096 struct AFSCallBack CallBack;
2097 struct AFSVolSync tsync;
2106 if (!tvolp->rootVnode || getNewFid) {
2107 struct VenusFid tfid;
2110 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2111 origCBs = afs_allCBs; /* ignore InitCallBackState */
2113 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2118 /* ReleaseReadLock(&tvolp->lock); */
2119 ObtainWriteLock(&tvolp->lock, 56);
2120 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
2121 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
2122 ReleaseWriteLock(&tvolp->lock);
2123 /* ObtainReadLock(&tvolp->lock);*/
2126 afid->Fid.Vnode = tvolp->rootVnode;
2127 afid->Fid.Unique = tvolp->rootUnique;
2131 ObtainSharedLock(&afs_xvcache, 7);
2133 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2134 if (!FidCmp(&(tvc->fid), afid)) {
2135 if (tvc->states & CVInit) {
2136 ReleaseSharedLock(&afs_xvcache);
2137 afs_osi_Sleep(&tvc->states);
2141 /* Grab this vnode, possibly reactivating from the free list */
2142 /* for the present (95.05.25) everything on the hash table is
2143 * definitively NOT in the free list -- at least until afs_reclaim
2144 * can be safely implemented */
2146 vg = vget(AFSTOV(tvc)); /* this bumps ref count */
2150 #endif /* AFS_OSF_ENV */
2151 #ifdef AFS_DARWIN80_ENV
2152 if (tvc->states & CDeadVnode) {
2153 ReleaseSharedLock(&afs_xvcache);
2154 afs_osi_Sleep(&tvc->states);
2157 if (vnode_get(AFSTOV(tvc))) /* this bumps ref count */
2164 if (!haveStatus && (!tvc || !(tvc->states & CStatd))) {
2165 /* Mount point no longer stat'd or unknown. FID may have changed. */
2168 AFS_RELE(AFSTOV(tvc));
2171 ReleaseSharedLock(&afs_xvcache);
2172 #ifdef AFS_DARWIN80_ENV
2175 vnode_put(AFSTOV(tvc));
2184 UpgradeSToWLock(&afs_xvcache, 23);
2185 /* no cache entry, better grab one */
2186 tvc = afs_NewVCache(afid, NULL);
2189 ReleaseWriteLock(&afs_xvcache);
2193 afs_stats_cmperf.vcacheMisses++;
2197 afs_stats_cmperf.vcacheHits++;
2199 /* we already bumped the ref count in the for loop above */
2200 #else /* AFS_OSF_ENV */
2203 UpgradeSToWLock(&afs_xvcache, 24);
2204 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2205 refpanic("GRVC VLRU inconsistent0");
2207 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2208 refpanic("GRVC VLRU inconsistent1");
2210 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2211 refpanic("GRVC VLRU inconsistent2");
2213 QRemove(&tvc->vlruq); /* move to lruq head */
2214 QAdd(&VLRU, &tvc->vlruq);
2215 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2216 refpanic("GRVC VLRU inconsistent3");
2218 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2219 refpanic("GRVC VLRU inconsistent4");
2221 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2222 refpanic("GRVC VLRU inconsistent5");
2227 ReleaseWriteLock(&afs_xvcache);
2229 if (tvc->states & CStatd) {
2233 ObtainReadLock(&tvc->lock);
2234 tvc->states &= ~CUnique;
2235 tvc->callback = NULL; /* redundant, perhaps */
2236 ReleaseReadLock(&tvc->lock);
2239 ObtainWriteLock(&tvc->lock, 57);
2241 /* It is always appropriate to throw away all the access rights? */
2242 afs_FreeAllAxs(&(tvc->Access));
2245 tvc->states |= CForeign;
2246 if (tvolp->states & VRO)
2248 if (tvolp->states & VBackup)
2249 tvc->states |= CBackup;
2250 /* now copy ".." entry back out of volume structure, if necessary */
2251 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2252 && (tvolp->rootUnique == afid->Fid.Unique)) {
2255 if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2257 tvc->mvid = (struct VenusFid *)
2258 osi_AllocSmallSpace(sizeof(struct VenusFid));
2259 *tvc->mvid = tvolp->dotdot;
2263 afs_RemoveVCB(afid);
2266 struct VenusFid tfid;
2269 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2270 origCBs = afs_allCBs; /* ignore InitCallBackState */
2272 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2277 ObtainWriteLock(&afs_xcbhash, 467);
2278 afs_DequeueCallback(tvc);
2279 tvc->callback = NULL;
2280 tvc->states &= ~(CStatd | CUnique);
2281 ReleaseWriteLock(&afs_xcbhash);
2282 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2283 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2284 ReleaseWriteLock(&tvc->lock);
2289 ObtainWriteLock(&afs_xcbhash, 468);
2290 if (origCBs == afs_allCBs) {
2291 tvc->states |= CTruth;
2292 tvc->callback = serverp;
2293 if (CallBack.ExpirationTime != 0) {
2294 tvc->cbExpires = CallBack.ExpirationTime + start;
2295 tvc->states |= CStatd;
2296 tvc->states &= ~CBulkFetching;
2297 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2298 } else if (tvc->states & CRO) {
2299 /* adapt gives us an hour. */
2300 tvc->cbExpires = 3600 + osi_Time();
2301 /*XXX*/ tvc->states |= CStatd;
2302 tvc->states &= ~CBulkFetching;
2303 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2306 afs_DequeueCallback(tvc);
2307 tvc->callback = NULL;
2308 tvc->states &= ~(CStatd | CUnique);
2309 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2310 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2312 ReleaseWriteLock(&afs_xcbhash);
2313 afs_ProcessFS(tvc, &OutStatus, areq);
2315 ReleaseWriteLock(&tvc->lock);
2322 * must be called with avc write-locked
2323 * don't absolutely have to invalidate the hint unless the dv has
2324 * changed, but be sure to get it right else there will be consistency bugs.
2327 afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
2328 struct vrequest * areq, struct AFSFetchStatus * Outsp)
2331 afs_uint32 start = 0;
2332 register struct conn *tc;
2333 struct AFSCallBack CallBack;
2334 struct AFSVolSync tsync;
2335 struct volume *volp;
2338 tc = afs_Conn(afid, areq, SHARED_LOCK);
2339 avc->dchint = NULL; /* invalidate hints */
2341 avc->callback = tc->srvr->server;
2343 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2346 RXAFS_FetchStatus(tc->id, (struct AFSFid *)&afid->Fid, Outsp,
2354 } while (afs_Analyze
2355 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2356 SHARED_LOCK, NULL));
2359 afs_ProcessFS(avc, Outsp, areq);
2360 volp = afs_GetVolume(afid, areq, READ_LOCK);
2361 ObtainWriteLock(&afs_xcbhash, 469);
2362 avc->states |= CTruth;
2363 if (avc->callback /* check for race */ ) {
2364 if (CallBack.ExpirationTime != 0) {
2365 avc->cbExpires = CallBack.ExpirationTime + start;
2366 avc->states |= CStatd;
2367 avc->states &= ~CBulkFetching;
2368 afs_QueueCallback(avc, CBHash(CallBack.ExpirationTime), volp);
2369 } else if (avc->states & CRO) { /* ordinary callback on a read-only volume -- AFS 3.2 style */
2370 avc->cbExpires = 3600 + start;
2371 avc->states |= CStatd;
2372 avc->states &= ~CBulkFetching;
2373 afs_QueueCallback(avc, CBHash(3600), volp);
2375 afs_DequeueCallback(avc);
2376 avc->callback = NULL;
2377 avc->states &= ~(CStatd | CUnique);
2378 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2379 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2382 afs_DequeueCallback(avc);
2383 avc->callback = NULL;
2384 avc->states &= ~(CStatd | CUnique);
2385 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2386 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2388 ReleaseWriteLock(&afs_xcbhash);
2390 afs_PutVolume(volp, READ_LOCK);
2392 /* used to undo the local callback, but that's too extreme.
2393 * There are plenty of good reasons that fetchstatus might return
2394 * an error, such as EPERM. If we have the vnode cached, statd,
2395 * with callback, might as well keep track of the fact that we
2396 * don't have access...
2398 if (code == EPERM || code == EACCES) {
2399 struct axscache *ac;
2400 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2402 else /* not found, add a new one if possible */
2403 afs_AddAxs(avc->Access, areq->uid, 0);
2414 * Stuff some information into the vcache for the given file.
2417 * afid : File in question.
2418 * OutStatus : Fetch status on the file.
2419 * CallBack : Callback info.
2420 * tc : RPC connection involved.
2421 * areq : vrequest involved.
2424 * Nothing interesting.
2427 afs_StuffVcache(register struct VenusFid *afid,
2428 struct AFSFetchStatus *OutStatus,
2429 struct AFSCallBack *CallBack, register struct conn *tc,
2430 struct vrequest *areq)
2432 register afs_int32 code, i, newvcache = 0;
2433 register struct vcache *tvc;
2434 struct AFSVolSync tsync;
2436 struct axscache *ac;
2439 AFS_STATCNT(afs_StuffVcache);
2440 #ifdef IFS_VCACHECOUNT
2445 ObtainSharedLock(&afs_xvcache, 8);
2447 tvc = afs_FindVCache(afid, &retry, DO_VLRU| IS_SLOCK /* no stats */ );
2449 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2450 ReleaseSharedLock(&afs_xvcache);
2451 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2457 /* no cache entry, better grab one */
2458 UpgradeSToWLock(&afs_xvcache, 25);
2459 tvc = afs_NewVCache(afid, NULL);
2461 ConvertWToSLock(&afs_xvcache);
2464 ReleaseSharedLock(&afs_xvcache);
2469 ReleaseSharedLock(&afs_xvcache);
2470 ObtainWriteLock(&tvc->lock, 58);
2472 tvc->states &= ~CStatd;
2473 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2474 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2476 /* Is it always appropriate to throw away all the access rights? */
2477 afs_FreeAllAxs(&(tvc->Access));
2479 /*Copy useful per-volume info */
2480 tvp = afs_GetVolume(afid, areq, READ_LOCK);
2482 if (newvcache && (tvp->states & VForeign))
2483 tvc->states |= CForeign;
2484 if (tvp->states & VRO)
2486 if (tvp->states & VBackup)
2487 tvc->states |= CBackup;
2489 * Now, copy ".." entry back out of volume structure, if
2492 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2494 tvc->mvid = (struct VenusFid *)
2495 osi_AllocSmallSpace(sizeof(struct VenusFid));
2496 *tvc->mvid = tvp->dotdot;
2499 /* store the stat on the file */
2500 afs_RemoveVCB(afid);
2501 afs_ProcessFS(tvc, OutStatus, areq);
2502 tvc->callback = tc->srvr->server;
2504 /* we use osi_Time twice below. Ideally, we would use the time at which
2505 * the FetchStatus call began, instead, but we don't have it here. So we
2506 * make do with "now". In the CRO case, it doesn't really matter. In
2507 * the other case, we hope that the difference between "now" and when the
2508 * call actually began execution on the server won't be larger than the
2509 * padding which the server keeps. Subtract 1 second anyway, to be on
2510 * the safe side. Can't subtract more because we don't know how big
2511 * ExpirationTime is. Possible consistency problems may arise if the call
2512 * timeout period becomes longer than the server's expiration padding. */
2513 ObtainWriteLock(&afs_xcbhash, 470);
2514 if (CallBack->ExpirationTime != 0) {
2515 tvc->cbExpires = CallBack->ExpirationTime + osi_Time() - 1;
2516 tvc->states |= CStatd;
2517 tvc->states &= ~CBulkFetching;
2518 afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2519 } else if (tvc->states & CRO) {
2520 /* old-fashioned AFS 3.2 style */
2521 tvc->cbExpires = 3600 + osi_Time();
2522 /*XXX*/ tvc->states |= CStatd;
2523 tvc->states &= ~CBulkFetching;
2524 afs_QueueCallback(tvc, CBHash(3600), tvp);
2526 afs_DequeueCallback(tvc);
2527 tvc->callback = NULL;
2528 tvc->states &= ~(CStatd | CUnique);
2529 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2530 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2532 ReleaseWriteLock(&afs_xcbhash);
2534 afs_PutVolume(tvp, READ_LOCK);
2536 /* look in per-pag cache */
2537 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2538 ac->axess = OutStatus->CallerAccess; /* substitute pags */
2539 else /* not found, add a new one if possible */
2540 afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2542 ReleaseWriteLock(&tvc->lock);
2543 afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2544 ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32,
2545 tvc->cbExpires, ICL_TYPE_INT32, tvc->cbExpires - osi_Time());
2547 * Release ref count... hope this guy stays around...
2550 } /*afs_StuffVcache */
2557 * Decrements the reference count on a cache entry.
2560 * avc : Pointer to the cache entry to decrement.
2563 * Nothing interesting.
2566 afs_PutVCache(register struct vcache *avc)
2568 AFS_STATCNT(afs_PutVCache);
2569 #ifdef AFS_DARWIN80_ENV
2570 vnode_put(AFSTOV(avc));
2574 * Can we use a read lock here?
2576 ObtainReadLock(&afs_xvcache);
2578 ReleaseReadLock(&afs_xvcache);
2580 } /*afs_PutVCache */
2583 static void findvc_sleep(struct vcache *avc, int flag) {
2584 if (flag & IS_SLOCK) {
2585 ReleaseSharedLock(&afs_xvcache);
2587 if (flag & IS_WLOCK) {
2588 ReleaseWriteLock(&afs_xvcache);
2590 ReleaseReadLock(&afs_xvcache);
2593 afs_osi_Sleep(&avc->states);
2594 if (flag & IS_SLOCK) {
2595 ObtainSharedLock(&afs_xvcache, 341);
2597 if (flag & IS_WLOCK) {
2598 ObtainWriteLock(&afs_xvcache, 343);
2600 ObtainReadLock(&afs_xvcache);
2608 * Find a vcache entry given a fid.
2611 * afid : Pointer to the fid whose cache entry we desire.
2612 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2613 * unlock the vnode, and try again.
2614 * flags: bit 1 to specify whether to compute hit statistics. Not
2615 * set if FindVCache is called as part of internal bookkeeping.
2618 * Must be called with the afs_xvcache lock at least held at
2619 * the read level. In order to do the VLRU adjustment, the xvcache lock
2620 * must be shared-- we upgrade it here.
2624 afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
2627 register struct vcache *tvc;
2629 #if defined( AFS_OSF_ENV)
2633 AFS_STATCNT(afs_FindVCache);
2637 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2638 if (FidMatches(afid, tvc)) {
2639 if (tvc->states & CVInit) {
2640 findvc_sleep(tvc, flag);
2644 /* Grab this vnode, possibly reactivating from the free list */
2646 vg = vget(AFSTOV(tvc));
2650 #endif /* AFS_OSF_ENV */
2651 #ifdef AFS_DARWIN80_ENV
2652 if (tvc->states & CDeadVnode) {
2653 findvc_sleep(tvc, flag);
2656 if (vnode_get(AFSTOV(tvc)))
2663 /* should I have a read lock on the vnode here? */
2667 #if !defined(AFS_OSF_ENV)
2668 osi_vnhold(tvc, retry); /* already held, above */
2669 if (retry && *retry)
2672 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
2673 tvc->states |= CUBCinit;
2675 if (UBCINFOMISSING(AFSTOV(tvc)) ||
2676 UBCINFORECLAIMED(AFSTOV(tvc))) {
2677 ubc_info_init(AFSTOV(tvc));
2680 tvc->states &= ~CUBCinit;
2683 * only move to front of vlru if we have proper vcache locking)
2685 if (flag & DO_VLRU) {
2686 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2687 refpanic("FindVC VLRU inconsistent1");
2689 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2690 refpanic("FindVC VLRU inconsistent1");
2692 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2693 refpanic("FindVC VLRU inconsistent2");
2695 UpgradeSToWLock(&afs_xvcache, 26);
2696 QRemove(&tvc->vlruq);
2697 QAdd(&VLRU, &tvc->vlruq);
2698 ConvertWToSLock(&afs_xvcache);
2699 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2700 refpanic("FindVC VLRU inconsistent1");
2702 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2703 refpanic("FindVC VLRU inconsistent2");
2705 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2706 refpanic("FindVC VLRU inconsistent3");
2712 if (flag & DO_STATS) {
2714 afs_stats_cmperf.vcacheHits++;
2716 afs_stats_cmperf.vcacheMisses++;
2717 if (afs_IsPrimaryCellNum(afid->Cell))
2718 afs_stats_cmperf.vlocalAccesses++;
2720 afs_stats_cmperf.vremoteAccesses++;
2723 } /*afs_FindVCache */
2729 * Find a vcache entry given a fid. Does a wildcard match on what we
2730 * have for the fid. If more than one entry, don't return anything.
2733 * avcp : Fill in pointer if we found one and only one.
2734 * afid : Pointer to the fid whose cache entry we desire.
2735 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2736 * unlock the vnode, and try again.
2737 * flags: bit 1 to specify whether to compute hit statistics. Not
2738 * set if FindVCache is called as part of internal bookkeeping.
2741 * Must be called with the afs_xvcache lock at least held at
2742 * the read level. In order to do the VLRU adjustment, the xvcache lock
2743 * must be shared-- we upgrade it here.
2746 * number of matches found.
2749 int afs_duplicate_nfs_fids = 0;
2752 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2754 register struct vcache *tvc;
2756 afs_int32 count = 0;
2757 struct vcache *found_tvc = NULL;
2762 AFS_STATCNT(afs_FindVCache);
2766 ObtainSharedLock(&afs_xvcache, 331);
2769 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2770 /* Match only on what we have.... */
2771 if (((tvc->fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2772 && (tvc->fid.Fid.Volume == afid->Fid.Volume)
2773 && ((tvc->fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2774 && (tvc->fid.Cell == afid->Cell)) {
2775 if (tvc->states & CVInit) {
2776 ReleaseSharedLock(&afs_xvcache);
2777 afs_osi_Sleep(&tvc->states);
2781 /* Grab this vnode, possibly reactivating from the free list */
2783 vg = vget(AFSTOV(tvc));
2786 /* This vnode no longer exists. */
2789 #endif /* AFS_OSF_ENV */
2790 #ifdef AFS_DARWIN80_ENV
2791 if (tvc->states & CDeadVnode) {
2792 ReleaseSharedLock(&afs_xvcache);
2793 afs_osi_Sleep(&tvc->states);
2796 if (vnode_get(AFSTOV(tvc))) {
2797 /* This vnode no longer exists. */
2800 #endif /* AFS_DARWIN80_ENV */
2805 /* Drop our reference counts. */
2807 vrele(AFSTOV(found_tvc));
2809 afs_duplicate_nfs_fids++;
2810 ReleaseSharedLock(&afs_xvcache);
2811 #ifdef AFS_DARWIN80_ENV
2812 /* Drop our reference counts. */
2813 vnode_put(AFSTOV(tvc));
2814 vnode_put(AFSTOV(found_tvc));
2823 /* should I have a read lock on the vnode here? */
2825 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2826 afs_int32 retry = 0;
2827 osi_vnhold(tvc, &retry);
2830 found_tvc = (struct vcache *)0;
2831 ReleaseSharedLock(&afs_xvcache);
2832 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2836 #if !defined(AFS_OSF_ENV)
2837 osi_vnhold(tvc, (int *)0); /* already held, above */
2841 * We obtained the xvcache lock above.
2843 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2844 refpanic("FindVC VLRU inconsistent1");
2846 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2847 refpanic("FindVC VLRU inconsistent1");
2849 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2850 refpanic("FindVC VLRU inconsistent2");
2852 UpgradeSToWLock(&afs_xvcache, 568);
2853 QRemove(&tvc->vlruq);
2854 QAdd(&VLRU, &tvc->vlruq);
2855 ConvertWToSLock(&afs_xvcache);
2856 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2857 refpanic("FindVC VLRU inconsistent1");
2859 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2860 refpanic("FindVC VLRU inconsistent2");
2862 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2863 refpanic("FindVC VLRU inconsistent3");
2869 afs_stats_cmperf.vcacheHits++;
2871 afs_stats_cmperf.vcacheMisses++;
2872 if (afs_IsPrimaryCellNum(afid->Cell))
2873 afs_stats_cmperf.vlocalAccesses++;
2875 afs_stats_cmperf.vremoteAccesses++;
2877 *avcp = tvc; /* May be null */
2879 ReleaseSharedLock(&afs_xvcache);
2880 return (tvc ? 1 : 0);
2882 } /*afs_NFSFindVCache */
2890 * Initialize vcache related variables
2893 afs_vcacheInit(int astatSize)
2895 register struct vcache *tvp;
2897 #if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
2898 if (!afs_maxvcount) {
2899 #if defined(AFS_LINUX22_ENV)
2900 afs_maxvcount = astatSize; /* no particular limit on linux? */
2901 #elif defined(AFS_OSF30_ENV)
2902 afs_maxvcount = max_vnodes / 2; /* limit ourselves to half the total */
2904 afs_maxvcount = nvnode / 2; /* limit ourselves to half the total */
2906 if (astatSize < afs_maxvcount) {
2907 afs_maxvcount = astatSize;
2910 #else /* AFS_OSF_ENV */
2914 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2915 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2917 #if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
2918 /* Allocate and thread the struct vcache entries */
2919 tvp = (struct vcache *)afs_osi_Alloc(astatSize * sizeof(struct vcache));
2920 memset((char *)tvp, 0, sizeof(struct vcache) * astatSize);
2922 Initial_freeVCList = tvp;
2923 freeVCList = &(tvp[0]);
2924 for (i = 0; i < astatSize - 1; i++) {
2925 tvp[i].nextfree = &(tvp[i + 1]);
2927 tvp[astatSize - 1].nextfree = NULL;
2928 #ifdef KERNEL_HAVE_PIN
2929 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
2933 #if defined(AFS_SGI_ENV)
2934 for (i = 0; i < astatSize; i++) {
2935 char name[METER_NAMSZ];
2936 struct vcache *tvc = &tvp[i];
2938 tvc->v.v_number = ++afsvnumbers;
2939 tvc->vc_rwlockid = OSI_NO_LOCKID;
2940 initnsema(&tvc->vc_rwlock, 1,
2941 makesname(name, "vrw", tvc->v.v_number));
2942 #ifndef AFS_SGI53_ENV
2943 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
2945 #ifndef AFS_SGI62_ENV
2946 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
2947 #endif /* AFS_SGI62_ENV */
2951 for(i = 0; i < VCSIZE; ++i)
2952 QInit(&afs_vhashTV[i]);
2960 shutdown_vcache(void)
2963 struct afs_cbr *tsp, *nsp;
2965 * XXX We may potentially miss some of the vcaches because if when there're no
2966 * free vcache entries and all the vcache entries are active ones then we allocate
2967 * an additional one - admittedly we almost never had that occur.
2971 register struct afs_q *tq, *uq;
2972 register struct vcache *tvc;
2973 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
2977 osi_FreeSmallSpace(tvc->mvid);
2978 tvc->mvid = (struct VenusFid *)0;
2981 aix_gnode_rele(AFSTOV(tvc));
2983 if (tvc->linkData) {
2984 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
2989 * Also free the remaining ones in the Cache
2991 for (i = 0; i < VCSIZE; i++) {
2992 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2994 osi_FreeSmallSpace(tvc->mvid);
2995 tvc->mvid = (struct VenusFid *)0;
2999 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
3000 #ifdef AFS_AIX32_ENV
3003 vms_delete(tvc->segid);
3005 tvc->segid = tvc->vmh = NULL;
3006 if (VREFCOUNT_GT(tvc,0))
3007 osi_Panic("flushVcache: vm race");
3015 #if defined(AFS_SUN5_ENV)
3021 if (tvc->linkData) {
3022 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3026 afs_FreeAllAxs(&(tvc->Access));
3032 * Free any leftover callback queue
3034 for (tsp = afs_cbrSpace; tsp; tsp = nsp) {
3036 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
3040 #ifdef KERNEL_HAVE_PIN
3041 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3043 #if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
3044 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3047 #if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
3048 freeVCList = Initial_freeVCList = 0;
3050 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
3051 LOCK_INIT(&afs_xvcb, "afs_xvcb");
3053 for(i = 0; i < VCSIZE; ++i)
3054 QInit(&afs_vhashTV[i]);