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"
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_lock_t afs_xvcb; /*Lock: fids on which there are callbacks */
66 struct vcache *freeVCList; /*Free list for stat cache entries */
67 struct vcache *Initial_freeVCList; /*Initial list for above */
68 struct afs_q VLRU; /*vcache LRU */
69 afs_int32 vcachegen = 0;
70 unsigned int afs_paniconwarn = 0;
71 struct vcache *afs_vhashT[VCSIZE];
72 static struct afs_cbr *afs_cbrHashT[CBRSIZE];
73 afs_int32 afs_bulkStatsLost;
74 int afs_norefpanic = 0;
76 /* Forward declarations */
77 static afs_int32 afs_QueueVCB(struct vcache *avc);
82 * Generate an index into the hash table for a given Fid.
85 afs_HashCBRFid(struct AFSFid *fid)
87 return (fid->Volume + fid->Vnode + fid->Unique) % CBRSIZE;
93 * Insert a CBR entry into the hash table.
94 * Must be called with afs_xvcb held.
97 afs_InsertHashCBR(struct afs_cbr *cbr)
99 int slot = afs_HashCBRFid(&cbr->fid);
101 cbr->hash_next = afs_cbrHashT[slot];
102 if (afs_cbrHashT[slot])
103 afs_cbrHashT[slot]->hash_pprev = &cbr->hash_next;
105 cbr->hash_pprev = &afs_cbrHashT[slot];
106 afs_cbrHashT[slot] = cbr;
113 * Flush the given vcache entry.
116 * avc : Pointer to vcache entry to flush.
117 * slept : Pointer to int to set 1 if we sleep/drop locks, 0 if we don't.
120 * afs_xvcache lock must be held for writing upon entry to
121 * prevent people from changing the vrefCount field, and to
122 * protect the lruq and hnext fields.
123 * LOCK: afs_FlushVCache afs_xvcache W
124 * REFCNT: vcache ref count must be zero on entry except for osf1
125 * RACE: lock is dropped and reobtained, permitting race in caller
129 afs_FlushVCache(struct vcache *avc, int *slept)
130 { /*afs_FlushVCache */
132 register afs_int32 i, code;
133 register struct vcache **uvc, *wvc;
136 AFS_STATCNT(afs_FlushVCache);
137 afs_Trace2(afs_iclSetp, CM_TRACE_FLUSHV, ICL_TYPE_POINTER, avc,
138 ICL_TYPE_INT32, avc->states);
141 VN_LOCK(AFSTOV(avc));
145 code = osi_VM_FlushVCache(avc, slept);
149 if (avc->states & CVFlushed) {
153 if (avc->nextfree || !avc->vlruq.prev || !avc->vlruq.next) { /* qv afs.h */
154 refpanic("LRU vs. Free inconsistency");
156 avc->states |= CVFlushed;
157 /* pull the entry out of the lruq and put it on the free list */
158 QRemove(&avc->vlruq);
159 avc->vlruq.prev = avc->vlruq.next = (struct afs_q *)0;
161 /* keep track of # of files that we bulk stat'd, but never used
162 * before they got recycled.
164 if (avc->states & CBulkStat)
167 /* remove entry from the hash chain */
168 i = VCHash(&avc->fid);
169 uvc = &afs_vhashT[i];
170 for (wvc = *uvc; wvc; uvc = &wvc->hnext, wvc = *uvc) {
173 avc->hnext = (struct vcache *)NULL;
178 osi_Panic("flushvcache"); /* not in correct hash bucket */
180 osi_FreeSmallSpace(avc->mvid);
181 avc->mvid = (struct VenusFid *)0;
183 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
184 avc->linkData = NULL;
186 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
187 /* OK, there are no internal vrefCounts, so there shouldn't
188 * be any more refs here. */
190 #ifdef AFS_DARWIN_ENV
191 vnode_clearfsnode(AFSTOV(avc));
193 avc->v->v_data = NULL; /* remove from vnode */
195 AFSTOV(avc) = NULL; /* also drop the ptr to vnode */
198 afs_FreeAllAxs(&(avc->Access));
200 /* we can't really give back callbacks on RO files, since the
201 * server only tracks them on a per-volume basis, and we don't
202 * know whether we still have some other files from the same
204 if ((avc->states & CRO) == 0 && avc->callback) {
207 ObtainWriteLock(&afs_xcbhash, 460);
208 afs_DequeueCallback(avc); /* remove it from queued callbacks list */
209 avc->states &= ~(CStatd | CUnique);
210 ReleaseWriteLock(&afs_xcbhash);
211 afs_symhint_inval(avc);
212 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
213 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
215 osi_dnlc_purgevp(avc);
218 * Next, keep track of which vnodes we've deleted for create's
219 * optimistic synchronization algorithm
222 if (avc->fid.Fid.Vnode & 1)
227 #if !defined(AFS_OSF_ENV)
228 /* put the entry in the free list */
229 avc->nextfree = freeVCList;
231 if (avc->vlruq.prev || avc->vlruq.next) {
232 refpanic("LRU vs. Free inconsistency");
235 /* This should put it back on the vnode free list since usecount is 1 */
238 if (VREFCOUNT_GT(avc,0)) {
239 VN_UNLOCK(AFSTOV(avc));
240 AFS_RELE(AFSTOV(avc));
242 if (afs_norefpanic) {
243 printf("flush vc refcnt < 1");
245 (void)vgone(avc, VX_NOSLEEP, NULL);
247 VN_UNLOCK(AFSTOV(avc));
249 osi_Panic("flush vc refcnt < 1");
251 #endif /* AFS_OSF_ENV */
252 avc->states |= CVFlushed;
257 VN_UNLOCK(AFSTOV(avc));
261 } /*afs_FlushVCache */
267 * The core of the inactive vnode op for all but IRIX.
270 afs_InactiveVCache(struct vcache *avc, struct AFS_UCRED *acred)
272 AFS_STATCNT(afs_inactive);
273 if (avc->states & CDirty) {
274 /* we can't keep trying to push back dirty data forever. Give up. */
275 afs_InvalidateAllSegments(avc); /* turns off dirty bit */
277 avc->states &= ~CMAPPED; /* mainly used by SunOS 4.0.x */
278 avc->states &= ~CDirty; /* Turn it off */
279 if (avc->states & CUnlinked) {
280 if (CheckLock(&afs_xvcache) || CheckLock(&afs_xdcache)) {
281 avc->states |= CUnlinkedDel;
284 afs_remunlink(avc, 1); /* ignore any return code */
293 * Description: allocate a callback return structure from the
294 * free list and return it.
296 * Env: The alloc and free routines are both called with the afs_xvcb lock
297 * held, so we don't have to worry about blocking in osi_Alloc.
299 static struct afs_cbr *afs_cbrSpace = 0;
303 register struct afs_cbr *tsp;
306 while (!afs_cbrSpace) {
307 if (afs_stats_cmperf.CallBackAlloced >= 2) {
308 /* don't allocate more than 2 * AFS_NCBRS for now */
310 afs_stats_cmperf.CallBackFlushes++;
314 (struct afs_cbr *)afs_osi_Alloc(AFS_NCBRS *
315 sizeof(struct afs_cbr));
316 for (i = 0; i < AFS_NCBRS - 1; i++) {
317 tsp[i].next = &tsp[i + 1];
319 tsp[AFS_NCBRS - 1].next = 0;
321 afs_stats_cmperf.CallBackAlloced++;
325 afs_cbrSpace = tsp->next;
332 * Description: free a callback return structure, removing it from all lists.
335 * asp -- the address of the structure to free.
337 * Environment: the xvcb lock is held over these calls.
340 afs_FreeCBR(register struct afs_cbr *asp)
342 *(asp->pprev) = asp->next;
344 asp->next->pprev = asp->pprev;
346 *(asp->hash_pprev) = asp->hash_next;
348 asp->hash_next->hash_pprev = asp->hash_pprev;
350 asp->next = afs_cbrSpace;
358 * Description: flush all queued callbacks to all servers.
362 * Environment: holds xvcb lock over RPC to guard against race conditions
363 * when a new callback is granted for the same file later on.
366 afs_FlushVCBs(afs_int32 lockit)
368 struct AFSFid *tfids;
369 struct AFSCallBack callBacks[1];
370 struct AFSCBFids fidArray;
371 struct AFSCBs cbArray;
373 struct afs_cbr *tcbrp;
377 struct vrequest treq;
379 int safety1, safety2, safety3;
381 if ((code = afs_InitReq(&treq, afs_osi_credp)))
383 treq.flags |= O_NONBLOCK;
384 tfids = afs_osi_Alloc(sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
387 MObtainWriteLock(&afs_xvcb, 273);
388 ObtainReadLock(&afs_xserver);
389 for (i = 0; i < NSERVERS; i++) {
390 for (safety1 = 0, tsp = afs_servers[i];
391 tsp && safety1 < afs_totalServers + 10;
392 tsp = tsp->next, safety1++) {
394 if (tsp->cbrs == (struct afs_cbr *)0)
397 /* otherwise, grab a block of AFS_MAXCBRSCALL from the list
398 * and make an RPC, over and over again.
400 tcount = 0; /* number found so far */
401 for (safety2 = 0; safety2 < afs_cacheStats; safety2++) {
402 if (tcount >= AFS_MAXCBRSCALL || !tsp->cbrs) {
403 /* if buffer is full, or we've queued all we're going
404 * to from this server, we should flush out the
407 fidArray.AFSCBFids_len = tcount;
408 fidArray.AFSCBFids_val = (struct AFSFid *)tfids;
409 cbArray.AFSCBs_len = 1;
410 cbArray.AFSCBs_val = callBacks;
411 memset(&callBacks[0], 0, sizeof(callBacks[0]));
412 callBacks[0].CallBackType = CB_EXCLUSIVE;
413 for (safety3 = 0; safety3 < MAXHOSTS * 2; safety3++) {
414 tc = afs_ConnByHost(tsp, tsp->cell->fsport,
415 tsp->cell->cellNum, &treq, 0,
419 (AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS);
422 RXAFS_GiveUpCallBacks(tc->id, &fidArray,
430 AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS, SHARED_LOCK,
435 /* ignore return code, since callbacks may have
436 * been returned anyway, we shouldn't leave them
437 * around to be returned again.
439 * Next, see if we are done with this server, and if so,
440 * break to deal with the next one.
446 /* if to flush full buffer */
447 /* if we make it here, we have an entry at the head of cbrs,
448 * which we should copy to the file ID array and then free.
451 tfids[tcount++] = tcbrp->fid;
453 /* Freeing the CBR will unlink it from the server's CBR list */
455 } /* while loop for this one server */
456 if (safety2 > afs_cacheStats) {
457 afs_warn("possible internal error afs_flushVCBs (%d)\n",
460 } /* for loop for this hash chain */
461 } /* loop through all hash chains */
462 if (safety1 > afs_totalServers + 2) {
464 ("AFS internal error (afs_flushVCBs) (%d > %d), continuing...\n",
465 safety1, afs_totalServers + 2);
467 osi_Panic("afs_flushVCBS safety1");
470 ReleaseReadLock(&afs_xserver);
472 MReleaseWriteLock(&afs_xvcb);
473 afs_osi_Free(tfids, sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
481 * Queue a callback on the given fid.
487 * Locks the xvcb lock.
488 * Called when the xvcache lock is already held.
492 afs_QueueVCB(struct vcache *avc)
495 struct afs_cbr *tcbp;
497 AFS_STATCNT(afs_QueueVCB);
498 /* The callback is really just a struct server ptr. */
499 tsp = (struct server *)(avc->callback);
501 /* we now have a pointer to the server, so we just allocate
502 * a queue entry and queue it.
504 MObtainWriteLock(&afs_xvcb, 274);
505 tcbp = afs_AllocCBR();
506 tcbp->fid = avc->fid.Fid;
508 tcbp->next = tsp->cbrs;
510 tsp->cbrs->pprev = &tcbp->next;
513 tcbp->pprev = &tsp->cbrs;
515 afs_InsertHashCBR(tcbp);
517 /* now release locks and return */
518 MReleaseWriteLock(&afs_xvcb);
527 * Remove a queued callback for a given Fid.
530 * afid: The fid we want cleansed of queued callbacks.
533 * Locks xvcb and xserver locks.
534 * Typically called with xdcache, xvcache and/or individual vcache
539 afs_RemoveVCB(struct VenusFid *afid)
542 struct afs_cbr *cbr, *ncbr;
544 AFS_STATCNT(afs_RemoveVCB);
545 MObtainWriteLock(&afs_xvcb, 275);
547 slot = afs_HashCBRFid(&afid->Fid);
548 ncbr = afs_cbrHashT[slot];
552 ncbr = cbr->hash_next;
554 if (afid->Fid.Volume == cbr->fid.Volume &&
555 afid->Fid.Vnode == cbr->fid.Vnode &&
556 afid->Fid.Unique == cbr->fid.Unique) {
561 MReleaseWriteLock(&afs_xvcb);
568 * This routine is responsible for allocating a new cache entry
569 * from the free list. It formats the cache entry and inserts it
570 * into the appropriate hash tables. It must be called with
571 * afs_xvcache write-locked so as to prevent several processes from
572 * trying to create a new cache entry simultaneously.
575 * afid : The file id of the file whose cache entry is being
578 /* LOCK: afs_NewVCache afs_xvcache W */
580 afs_NewVCache(struct VenusFid *afid, struct server *serverp)
584 afs_int32 anumber = VCACHE_FREE;
586 struct gnode *gnodepnt;
590 #endif /* AFS_OSF_ENV */
591 struct afs_q *tq, *uq;
594 AFS_STATCNT(afs_NewVCache);
597 if (afs_vcount >= afs_maxvcount) {
600 * If we are using > 33 % of the total system vnodes for AFS vcache
601 * entries or we are using the maximum number of vcache entries,
602 * then free some. (if our usage is > 33% we should free some, if
603 * our usage is > afs_maxvcount, set elsewhere to 0.5*nvnode,
604 * we _must_ free some -- no choice).
606 if (((3 * afs_vcount) > nvnode) || (afs_vcount >= afs_maxvcount)) {
608 struct afs_q *tq, *uq;
613 for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
616 if (tvc->states & CVFlushed)
617 refpanic("CVFlushed on VLRU");
618 else if (i++ > afs_maxvcount)
619 refpanic("Exceeded pool of AFS vnodes(VLRU cycle?)");
620 else if (QNext(uq) != tq)
621 refpanic("VLRU inconsistent");
622 else if (!VREFCOUNT_GT(tvc,0))
623 refpanic("refcnt 0 on VLRU");
625 if (VREFCOUNT_GT(tvc,0) && !VREFCOUNT_GT(tvc,1) &&
627 && (tvc->states & CUnlinkedDel) == 0) {
628 code = afs_FlushVCache(tvc, &fv_slept);
635 continue; /* start over - may have raced. */
641 if (anumber == VCACHE_FREE) {
642 printf("NewVCache: warning none freed, using %d of %d\n",
643 afs_vcount, afs_maxvcount);
644 if (afs_vcount >= afs_maxvcount) {
645 osi_Panic("NewVCache - none freed");
646 /* XXX instead of panicing, should do afs_maxvcount++
647 * and magic up another one */
653 if (getnewvnode(MOUNT_AFS, &Afs_vnodeops, &nvc)) {
654 /* What should we do ???? */
655 osi_Panic("afs_NewVCache: no more vnodes");
660 tvc->nextfree = NULL;
662 #else /* AFS_OSF_ENV */
663 /* pull out a free cache entry */
666 for (tq = VLRU.prev; (anumber > 0) && (tq != &VLRU); tq = uq) {
670 if (tvc->states & CVFlushed) {
671 refpanic("CVFlushed on VLRU");
673 } else if (i++ > 2 * afs_cacheStats) { /* even allowing for a few xallocs... */
674 refpanic("Increase -stat parameter of afsd(VLRU cycle?)");
676 } else if (QNext(uq) != tq) {
677 refpanic("VLRU inconsistent");
679 #if defined(AFS_LINUX22_ENV)
680 if (tvc != afs_globalVp && VREFCOUNT(tvc) && tvc->opens == 0) {
681 struct dentry *dentry;
682 struct list_head *cur, *head = &(AFSTOI(tvc))->i_dentry;
687 #if defined(AFS_LINUX24_ENV)
688 spin_lock(&dcache_lock);
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?) */
710 #if defined(AFS_LINUX24_ENV)
711 spin_unlock(&dcache_lock);
720 if (!VREFCOUNT_GT(tvc,0)
721 #if defined(AFS_DARWIN_ENV) && !defined(UKERNEL) && !defined(AFS_DARWIN80_ENV)
722 || ((VREFCOUNT(tvc) == 1) &&
723 (UBCINFOEXISTS(AFSTOV(tvc))))
725 && tvc->opens == 0 && (tvc->states & CUnlinkedDel) == 0) {
726 #if defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
728 * vgone() reclaims the vnode, which calls afs_FlushVCache(),
729 * then it puts the vnode on the free list.
730 * If we don't do this we end up with a cleaned vnode that's
731 * not on the free list.
732 * XXX assume FreeBSD is the same for now.
739 code = afs_FlushVCache(tvc, &fv_slept);
747 continue; /* start over - may have raced. */
755 /* none free, making one is better than a panic */
756 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
757 if (afs_cacheStats == afs_stats_cmperf.vcacheXAllocs) printf("would vlru cycle panic\n");
758 tvc = (struct vcache *)afs_osi_Alloc(sizeof(struct vcache));
759 #if defined(AFS_DARWIN_ENV) && !defined(UKERNEL)
760 tvc->v = NULL; /* important to clean this, or use memset 0 */
762 #ifdef KERNEL_HAVE_PIN
763 pin((char *)tvc, sizeof(struct vcache)); /* XXX */
765 #if defined(AFS_SGI_ENV)
767 char name[METER_NAMSZ];
768 memset(tvc, 0, sizeof(struct vcache));
769 tvc->v.v_number = ++afsvnumbers;
770 tvc->vc_rwlockid = OSI_NO_LOCKID;
771 initnsema(&tvc->vc_rwlock, 1,
772 makesname(name, "vrw", tvc->v.v_number));
773 #ifndef AFS_SGI53_ENV
774 initnsema(&tvc->v.v_sync, 0,
775 makesname(name, "vsy", tvc->v.v_number));
777 #ifndef AFS_SGI62_ENV
778 initnlock(&tvc->v.v_lock,
779 makesname(name, "vlk", tvc->v.v_number));
782 #endif /* AFS_SGI_ENV */
784 tvc = freeVCList; /* take from free list */
785 freeVCList = tvc->nextfree;
786 tvc->nextfree = NULL;
788 #endif /* AFS_OSF_ENV */
790 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
792 panic("afs_NewVCache(): free vcache with vnode attached");
795 #if !defined(AFS_SGI_ENV) && !defined(AFS_OSF_ENV)
796 memset((char *)tvc, 0, sizeof(struct vcache));
801 RWLOCK_INIT(&tvc->lock, "vcache lock");
802 #if defined(AFS_SUN5_ENV)
803 RWLOCK_INIT(&tvc->vlock, "vcache vlock");
804 #endif /* defined(AFS_SUN5_ENV) */
808 afs_nbsd_getnewvnode(tvc); /* includes one refcount */
810 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
812 #ifdef AFS_DARWIN_ENV
814 afs_darwin_getnewvnode(tvc); /* includes one refcount */
816 #ifdef AFS_DARWIN80_ENV
817 LOCKINIT(tvc->rwlock);
819 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
827 #if defined(AFS_FBSD60_ENV)
828 if (getnewvnode(MOUNT_AFS, afs_globalVFS, &afs_vnodeops, &vp))
829 #elif defined(AFS_FBSD50_ENV)
830 if (getnewvnode(MOUNT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
832 if (getnewvnode(VT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
834 panic("afs getnewvnode"); /* can't happen */
836 if (tvc->v != NULL) {
837 /* I'd like to know if this ever happens...
838 * We don't drop global for the rest of this function,
839 * so if we do lose the race, the other thread should
840 * have found the same vnode and finished initializing
841 * the vcache entry. Is it conceivable that this vcache
842 * entry could be recycled during this interval? If so,
843 * then there probably needs to be some sort of additional
844 * mutual exclusion (an Embryonic flag would suffice).
846 printf("afs_NewVCache: lost the race\n");
850 tvc->v->v_data = tvc;
851 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
854 tvc->parentVnode = 0;
856 tvc->linkData = NULL;
859 tvc->execsOrWriters = 0;
863 tvc->last_looker = 0;
865 tvc->asynchrony = -1;
867 afs_symhint_inval(tvc);
869 tvc->flushDV.low = tvc->flushDV.high = AFS_MAXDV;
872 tvc->truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
873 hzero(tvc->m.DataVersion); /* in case we copy it into flushDV */
874 #if defined(AFS_LINUX22_ENV)
876 struct inode *ip = AFSTOI(tvc);
877 #if defined(AFS_LINUX24_ENV)
878 struct address_space *mapping = &ip->i_data;
881 #if defined(AFS_LINUX26_ENV)
884 sema_init(&ip->i_sem, 1);
885 INIT_LIST_HEAD(&ip->i_hash);
886 INIT_LIST_HEAD(&ip->i_dentry);
887 #if defined(AFS_LINUX24_ENV)
888 sema_init(&ip->i_zombie, 1);
889 init_waitqueue_head(&ip->i_wait);
890 spin_lock_init(&ip->i_data.i_shared_lock);
891 #ifdef STRUCT_ADDRESS_SPACE_HAS_PAGE_LOCK
892 spin_lock_init(&ip->i_data.page_lock);
894 INIT_LIST_HEAD(&ip->i_data.clean_pages);
895 INIT_LIST_HEAD(&ip->i_data.dirty_pages);
896 INIT_LIST_HEAD(&ip->i_data.locked_pages);
897 INIT_LIST_HEAD(&ip->i_dirty_buffers);
898 #ifdef STRUCT_INODE_HAS_I_DIRTY_DATA_BUFFERS
899 INIT_LIST_HEAD(&ip->i_dirty_data_buffers);
901 #ifdef STRUCT_INODE_HAS_I_DEVICES
902 INIT_LIST_HEAD(&ip->i_devices);
904 #ifdef STRUCT_INODE_HAS_I_TRUNCATE_SEM
905 init_rwsem(&ip->i_truncate_sem);
907 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
908 init_rwsem(&ip->i_alloc_sem);
911 #else /* AFS_LINUX22_ENV */
912 sema_init(&ip->i_atomic_write, 1);
913 init_waitqueue(&ip->i_wait);
917 #if defined(AFS_LINUX24_ENV)
919 ip->i_mapping = mapping;
920 #ifdef STRUCT_ADDRESS_SPACE_HAS_GFP_MASK
921 ip->i_data.gfp_mask = GFP_HIGHUSER;
923 #if defined(AFS_LINUX26_ENV)
924 mapping_set_gfp_mask(mapping, GFP_HIGHUSER);
926 extern struct backing_dev_info afs_backing_dev_info;
928 mapping->backing_dev_info = &afs_backing_dev_info;
933 #if !defined(AFS_LINUX26_ENV)
935 ip->i_dev = afs_globalVFS->s_dev;
937 #ifdef STRUCT_INODE_HAS_I_SECURITY
938 ip->i_security = NULL;
939 if (security_inode_alloc(ip))
940 panic("Cannot allocate inode security");
943 ip->i_sb = afs_globalVFS;
944 put_inode_on_dummy_list(ip);
945 #ifdef STRUCT_INODE_HAS_I_SB_LIST
946 list_add(&ip->i_sb_list, &ip->i_sb->s_inodes);
948 #if defined(STRUCT_INODE_HAS_INOTIFY_LOCK) || defined(STRUCT_INODE_HAS_INOTIFY_SEM)
949 INIT_LIST_HEAD(&ip->inotify_watches);
950 #if defined(STRUCT_INODE_HAS_INOTIFY_SEM)
951 sema_init(&ip->inotify_sem, 1);
953 spin_lock_init(&ip->inotify_lock);
960 /* Hold it for the LRU (should make count 2) */
961 VN_HOLD(AFSTOV(tvc));
962 #else /* AFS_OSF_ENV */
963 #if !(defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV))
964 VREFCOUNT_SET(tvc, 1); /* us */
965 #endif /* AFS_XBSD_ENV */
966 #endif /* AFS_OSF_ENV */
968 LOCK_INIT(&tvc->pvmlock, "vcache pvmlock");
969 tvc->vmh = tvc->segid = NULL;
972 #ifdef AFS_BOZONLOCK_ENV
973 #if defined(AFS_SUN5_ENV)
974 rw_init(&tvc->rwlock, "vcache rwlock", RW_DEFAULT, NULL);
976 #if defined(AFS_SUN55_ENV)
977 /* This is required if the kaio (kernel aynchronous io)
978 ** module is installed. Inside the kernel, the function
979 ** check_vp( common/os/aio.c) checks to see if the kernel has
980 ** to provide asynchronous io for this vnode. This
981 ** function extracts the device number by following the
982 ** v_data field of the vnode. If we do not set this field
983 ** then the system panics. The value of the v_data field
984 ** is not really important for AFS vnodes because the kernel
985 ** does not do asynchronous io for regular files. Hence,
986 ** for the time being, we fill up the v_data field with the
987 ** vnode pointer itself. */
988 tvc->v.v_data = (char *)tvc;
989 #endif /* AFS_SUN55_ENV */
991 afs_BozonInit(&tvc->pvnLock, tvc);
995 tvc->callback = serverp; /* to minimize chance that clear
997 /* initialize vnode data, note vrefCount is v.v_count */
999 /* Don't forget to free the gnode space */
1000 tvc->v.v_gnode = gnodepnt =
1001 (struct gnode *)osi_AllocSmallSpace(sizeof(struct gnode));
1002 memset((char *)gnodepnt, 0, sizeof(struct gnode));
1004 #ifdef AFS_SGI64_ENV
1005 memset((void *)&(tvc->vc_bhv_desc), 0, sizeof(tvc->vc_bhv_desc));
1006 bhv_desc_init(&(tvc->vc_bhv_desc), tvc, tvc, &Afs_vnodeops);
1007 #ifdef AFS_SGI65_ENV
1008 vn_bhv_head_init(&(tvc->v.v_bh), "afsvp");
1009 vn_bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
1011 bhv_head_init(&(tvc->v.v_bh));
1012 bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
1014 #ifdef AFS_SGI65_ENV
1015 tvc->v.v_mreg = tvc->v.v_mregb = (struct pregion *)tvc;
1016 #ifdef VNODE_TRACING
1017 tvc->v.v_trace = ktrace_alloc(VNODE_TRACE_SIZE, 0);
1019 init_bitlock(&tvc->v.v_pcacheflag, VNODE_PCACHE_LOCKBIT, "afs_pcache",
1021 init_mutex(&tvc->v.v_filocksem, MUTEX_DEFAULT, "afsvfl", (long)tvc);
1022 init_mutex(&tvc->v.v_buf_lock, MUTEX_DEFAULT, "afsvnbuf", (long)tvc);
1024 vnode_pcache_init(&tvc->v);
1025 #if defined(DEBUG) && defined(VNODE_INIT_BITLOCK)
1026 /* Above define is never true execpt in SGI test kernels. */
1027 init_bitlock(&(tvc->v.v_flag, VLOCK, "vnode", tvc->v.v_number);
1029 #ifdef INTR_KTHREADS
1030 AFS_VN_INIT_BUF_LOCK(&(tvc->v));
1033 SetAfsVnode(AFSTOV(tvc));
1034 #endif /* AFS_SGI64_ENV */
1036 * The proper value for mvstat (for root fids) is setup by the caller.
1039 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
1041 if (afs_globalVFS == 0)
1042 osi_Panic("afs globalvfs");
1043 vSetVfsp(tvc, afs_globalVFS);
1044 vSetType(tvc, VREG);
1046 tvc->v.v_vfsnext = afs_globalVFS->vfs_vnodes; /* link off vfs */
1047 tvc->v.v_vfsprev = NULL;
1048 afs_globalVFS->vfs_vnodes = &tvc->v;
1049 if (tvc->v.v_vfsnext != NULL)
1050 tvc->v.v_vfsnext->v_vfsprev = &tvc->v;
1051 tvc->v.v_next = gnodepnt->gn_vnode; /*Single vnode per gnode for us! */
1052 gnodepnt->gn_vnode = &tvc->v;
1054 #if defined(AFS_DUX40_ENV)
1055 insmntque(tvc, afs_globalVFS, &afs_ubcops);
1058 /* Is this needed??? */
1059 insmntque(tvc, afs_globalVFS);
1060 #endif /* AFS_OSF_ENV */
1061 #endif /* AFS_DUX40_ENV */
1062 #if defined(AFS_SGI_ENV)
1063 VN_SET_DPAGES(&(tvc->v), (struct pfdat *)NULL);
1064 osi_Assert((tvc->v.v_flag & VINACT) == 0);
1066 osi_Assert(VN_GET_PGCNT(&(tvc->v)) == 0);
1067 osi_Assert(tvc->mapcnt == 0 && tvc->vc_locktrips == 0);
1068 osi_Assert(tvc->vc_rwlockid == OSI_NO_LOCKID);
1069 osi_Assert(tvc->v.v_filocks == NULL);
1070 #if !defined(AFS_SGI65_ENV)
1071 osi_Assert(tvc->v.v_filocksem == NULL);
1073 osi_Assert(tvc->cred == NULL);
1074 #ifdef AFS_SGI64_ENV
1075 vnode_pcache_reinit(&tvc->v);
1076 tvc->v.v_rdev = NODEV;
1078 vn_initlist((struct vnlist *)&tvc->v);
1080 #endif /* AFS_SGI_ENV */
1082 osi_dnlc_purgedp(tvc); /* this may be overkill */
1083 memset((char *)&(tvc->quick), 0, sizeof(struct vtodc));
1084 memset((char *)&(tvc->callsort), 0, sizeof(struct afs_q));
1088 tvc->hnext = afs_vhashT[i];
1089 afs_vhashT[i] = tvc;
1090 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1091 refpanic("NewVCache VLRU inconsistent");
1093 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
1094 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1095 refpanic("NewVCache VLRU inconsistent2");
1097 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
1098 refpanic("NewVCache VLRU inconsistent3");
1100 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
1101 refpanic("NewVCache VLRU inconsistent4");
1107 } /*afs_NewVCache */
1111 * afs_FlushActiveVcaches
1117 * doflocks : Do we handle flocks?
1119 /* LOCK: afs_FlushActiveVcaches afs_xvcache N */
1121 afs_FlushActiveVcaches(register afs_int32 doflocks)
1123 register struct vcache *tvc;
1125 register struct conn *tc;
1126 register afs_int32 code;
1127 register struct AFS_UCRED *cred = NULL;
1128 struct vrequest treq, ureq;
1129 struct AFSVolSync tsync;
1132 AFS_STATCNT(afs_FlushActiveVcaches);
1133 ObtainReadLock(&afs_xvcache);
1134 for (i = 0; i < VCSIZE; i++) {
1135 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1136 if (doflocks && tvc->flockCount != 0) {
1137 /* if this entry has an flock, send a keep-alive call out */
1139 ReleaseReadLock(&afs_xvcache);
1140 ObtainWriteLock(&tvc->lock, 51);
1142 afs_InitReq(&treq, afs_osi_credp);
1143 treq.flags |= O_NONBLOCK;
1145 tc = afs_Conn(&tvc->fid, &treq, SHARED_LOCK);
1147 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
1150 RXAFS_ExtendLock(tc->id,
1151 (struct AFSFid *)&tvc->fid.Fid,
1157 } while (afs_Analyze
1158 (tc, code, &tvc->fid, &treq,
1159 AFS_STATS_FS_RPCIDX_EXTENDLOCK, SHARED_LOCK, NULL));
1161 ReleaseWriteLock(&tvc->lock);
1162 ObtainReadLock(&afs_xvcache);
1166 if ((tvc->states & CCore) || (tvc->states & CUnlinkedDel)) {
1168 * Don't let it evaporate in case someone else is in
1169 * this code. Also, drop the afs_xvcache lock while
1170 * getting vcache locks.
1173 ReleaseReadLock(&afs_xvcache);
1174 #ifdef AFS_BOZONLOCK_ENV
1175 afs_BozonLock(&tvc->pvnLock, tvc);
1177 #if defined(AFS_SGI_ENV)
1179 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1181 osi_Assert(VREFCOUNT_GT(tvc,0));
1182 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1184 ObtainWriteLock(&tvc->lock, 52);
1185 if (tvc->states & CCore) {
1186 tvc->states &= ~CCore;
1187 /* XXXX Find better place-holder for cred XXXX */
1188 cred = (struct AFS_UCRED *)tvc->linkData;
1189 tvc->linkData = NULL; /* XXX */
1190 afs_InitReq(&ureq, cred);
1191 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1192 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
1193 tvc->execsOrWriters);
1194 code = afs_StoreOnLastReference(tvc, &ureq);
1195 ReleaseWriteLock(&tvc->lock);
1196 #ifdef AFS_BOZONLOCK_ENV
1197 afs_BozonUnlock(&tvc->pvnLock, tvc);
1199 hzero(tvc->flushDV);
1202 if (code && code != VNOVNODE) {
1203 afs_StoreWarn(code, tvc->fid.Fid.Volume,
1204 /* /dev/console */ 1);
1206 } else if (tvc->states & CUnlinkedDel) {
1210 ReleaseWriteLock(&tvc->lock);
1211 #ifdef AFS_BOZONLOCK_ENV
1212 afs_BozonUnlock(&tvc->pvnLock, tvc);
1214 #if defined(AFS_SGI_ENV)
1215 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1217 afs_remunlink(tvc, 0);
1218 #if defined(AFS_SGI_ENV)
1219 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1222 /* lost (or won, perhaps) the race condition */
1223 ReleaseWriteLock(&tvc->lock);
1224 #ifdef AFS_BOZONLOCK_ENV
1225 afs_BozonUnlock(&tvc->pvnLock, tvc);
1228 #if defined(AFS_SGI_ENV)
1229 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1231 ObtainReadLock(&afs_xvcache);
1234 AFS_RELE(AFSTOV(tvc));
1235 /* Matches write code setting CCore flag */
1241 ReleaseReadLock(&afs_xvcache);
1249 * Make sure a cache entry is up-to-date status-wise.
1251 * NOTE: everywhere that calls this can potentially be sped up
1252 * by checking CStatd first, and avoiding doing the InitReq
1253 * if this is up-to-date.
1255 * Anymore, the only places that call this KNOW already that the
1256 * vcache is not up-to-date, so we don't screw around.
1259 * avc : Ptr to vcache entry to verify.
1264 afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1266 register struct vcache *tvc;
1268 AFS_STATCNT(afs_VerifyVCache);
1270 #if defined(AFS_OSF_ENV)
1271 ObtainReadLock(&avc->lock);
1272 if (afs_IsWired(avc)) {
1273 ReleaseReadLock(&avc->lock);
1276 ReleaseReadLock(&avc->lock);
1277 #endif /* AFS_OSF_ENV */
1278 /* otherwise we must fetch the status info */
1280 ObtainWriteLock(&avc->lock, 53);
1281 if (avc->states & CStatd) {
1282 ReleaseWriteLock(&avc->lock);
1285 ObtainWriteLock(&afs_xcbhash, 461);
1286 avc->states &= ~(CStatd | CUnique);
1287 avc->callback = NULL;
1288 afs_DequeueCallback(avc);
1289 ReleaseWriteLock(&afs_xcbhash);
1290 ReleaseWriteLock(&avc->lock);
1292 /* since we've been called back, or the callback has expired,
1293 * it's possible that the contents of this directory, or this
1294 * file's name have changed, thus invalidating the dnlc contents.
1296 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1297 osi_dnlc_purgedp(avc);
1299 osi_dnlc_purgevp(avc);
1301 /* fetch the status info */
1302 tvc = afs_GetVCache(&avc->fid, areq, NULL, avc);
1305 /* Put it back; caller has already incremented vrefCount */
1309 } /*afs_VerifyVCache */
1316 * Simple copy of stat info into cache.
1319 * avc : Ptr to vcache entry involved.
1320 * astat : Ptr to stat info to copy.
1323 * Nothing interesting.
1325 * Callers: as of 1992-04-29, only called by WriteVCache
1328 afs_SimpleVStat(register struct vcache *avc,
1329 register struct AFSFetchStatus *astat, struct vrequest *areq)
1332 AFS_STATCNT(afs_SimpleVStat);
1335 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1336 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1338 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1340 #ifdef AFS_64BIT_CLIENT
1341 FillInt64(length, astat->Length_hi, astat->Length);
1342 #else /* AFS_64BIT_CLIENT */
1343 length = astat->Length;
1344 #endif /* AFS_64BIT_CLIENT */
1345 #if defined(AFS_SGI_ENV)
1346 osi_Assert((valusema(&avc->vc_rwlock) <= 0)
1347 && (OSI_GET_LOCKID() == avc->vc_rwlockid));
1348 if (length < avc->m.Length) {
1349 vnode_t *vp = (vnode_t *) avc;
1351 osi_Assert(WriteLocked(&avc->lock));
1352 ReleaseWriteLock(&avc->lock);
1354 PTOSSVP(vp, (off_t) length, (off_t) MAXLONG);
1356 ObtainWriteLock(&avc->lock, 67);
1359 /* if writing the file, don't fetch over this value */
1360 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT, ICL_TYPE_POINTER, avc,
1361 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1362 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1363 avc->m.Length = length;
1364 avc->m.Date = astat->ClientModTime;
1366 avc->m.Owner = astat->Owner;
1367 avc->m.Group = astat->Group;
1368 avc->m.Mode = astat->UnixModeBits;
1369 if (vType(avc) == VREG) {
1370 avc->m.Mode |= S_IFREG;
1371 } else if (vType(avc) == VDIR) {
1372 avc->m.Mode |= S_IFDIR;
1373 } else if (vType(avc) == VLNK) {
1374 avc->m.Mode |= S_IFLNK;
1375 if ((avc->m.Mode & 0111) == 0)
1378 if (avc->states & CForeign) {
1379 struct axscache *ac;
1380 avc->anyAccess = astat->AnonymousAccess;
1382 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1384 * Caller has at least one bit not covered by anonymous, and
1385 * thus may have interesting rights.
1387 * HOWEVER, this is a really bad idea, because any access query
1388 * for bits which aren't covered by anonymous, on behalf of a user
1389 * who doesn't have any special rights, will result in an answer of
1390 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1391 * It's an especially bad idea under Ultrix, since (due to the lack of
1392 * a proper access() call) it must perform several afs_access() calls
1393 * in order to create magic mode bits that vary according to who makes
1394 * the call. In other words, _every_ stat() generates a test for
1397 #endif /* badidea */
1398 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1399 ac->axess = astat->CallerAccess;
1400 else /* not found, add a new one if possible */
1401 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1405 } /*afs_SimpleVStat */
1412 * Store the status info *only* back to the server for a
1416 * avc : Ptr to the vcache entry.
1417 * astatus : Ptr to the status info to store.
1418 * areq : Ptr to the associated vrequest.
1421 * Must be called with a shared lock held on the vnode.
1425 afs_WriteVCache(register struct vcache *avc,
1426 register struct AFSStoreStatus *astatus,
1427 struct vrequest *areq)
1431 struct AFSFetchStatus OutStatus;
1432 struct AFSVolSync tsync;
1434 AFS_STATCNT(afs_WriteVCache);
1435 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1436 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
1439 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
1441 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1444 RXAFS_StoreStatus(tc->id, (struct AFSFid *)&avc->fid.Fid,
1445 astatus, &OutStatus, &tsync);
1450 } while (afs_Analyze
1451 (tc, code, &avc->fid, areq, AFS_STATS_FS_RPCIDX_STORESTATUS,
1452 SHARED_LOCK, NULL));
1454 UpgradeSToWLock(&avc->lock, 20);
1456 /* success, do the changes locally */
1457 afs_SimpleVStat(avc, &OutStatus, areq);
1459 * Update the date, too. SimpleVStat didn't do this, since
1460 * it thought we were doing this after fetching new status
1461 * over a file being written.
1463 avc->m.Date = OutStatus.ClientModTime;
1465 /* failure, set up to check with server next time */
1466 ObtainWriteLock(&afs_xcbhash, 462);
1467 afs_DequeueCallback(avc);
1468 avc->states &= ~(CStatd | CUnique); /* turn off stat valid flag */
1469 ReleaseWriteLock(&afs_xcbhash);
1470 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1471 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
1473 ConvertWToSLock(&avc->lock);
1476 } /*afs_WriteVCache */
1482 * Copy astat block into vcache info
1485 * avc : Ptr to vcache entry.
1486 * astat : Ptr to stat block to copy in.
1487 * areq : Ptr to associated request.
1490 * Must be called under a write lock
1492 * Note: this code may get dataversion and length out of sync if the file has
1493 * been modified. This is less than ideal. I haven't thought about
1494 * it sufficiently to be certain that it is adequate.
1497 afs_ProcessFS(register struct vcache *avc,
1498 register struct AFSFetchStatus *astat, struct vrequest *areq)
1501 AFS_STATCNT(afs_ProcessFS);
1503 #ifdef AFS_64BIT_CLIENT
1504 FillInt64(length, astat->Length_hi, astat->Length);
1505 #else /* AFS_64BIT_CLIENT */
1506 length = astat->Length;
1507 #endif /* AFS_64BIT_CLIENT */
1508 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1509 * number for each bulk status request. Under no circumstances
1510 * should afs_DoBulkStat store a sequence number if the new
1511 * length will be ignored when afs_ProcessFS is called with
1512 * new stats. If you change the following conditional then you
1513 * also need to change the conditional in afs_DoBulkStat. */
1515 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1516 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1518 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1520 /* if we're writing or mapping this file, don't fetch over these
1523 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1524 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1525 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1526 avc->m.Length = length;
1527 avc->m.Date = astat->ClientModTime;
1529 hset64(avc->m.DataVersion, astat->dataVersionHigh, astat->DataVersion);
1530 avc->m.Owner = astat->Owner;
1531 avc->m.Mode = astat->UnixModeBits;
1532 avc->m.Group = astat->Group;
1533 avc->m.LinkCount = astat->LinkCount;
1534 if (astat->FileType == File) {
1535 vSetType(avc, VREG);
1536 avc->m.Mode |= S_IFREG;
1537 } else if (astat->FileType == Directory) {
1538 vSetType(avc, VDIR);
1539 avc->m.Mode |= S_IFDIR;
1540 } else if (astat->FileType == SymbolicLink) {
1541 if (afs_fakestat_enable && (avc->m.Mode & 0111) == 0) {
1542 vSetType(avc, VDIR);
1543 avc->m.Mode |= S_IFDIR;
1545 vSetType(avc, VLNK);
1546 avc->m.Mode |= S_IFLNK;
1548 if ((avc->m.Mode & 0111) == 0) {
1552 avc->anyAccess = astat->AnonymousAccess;
1554 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1556 * Caller has at least one bit not covered by anonymous, and
1557 * thus may have interesting rights.
1559 * HOWEVER, this is a really bad idea, because any access query
1560 * for bits which aren't covered by anonymous, on behalf of a user
1561 * who doesn't have any special rights, will result in an answer of
1562 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1563 * It's an especially bad idea under Ultrix, since (due to the lack of
1564 * a proper access() call) it must perform several afs_access() calls
1565 * in order to create magic mode bits that vary according to who makes
1566 * the call. In other words, _every_ stat() generates a test for
1569 #endif /* badidea */
1571 struct axscache *ac;
1572 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1573 ac->axess = astat->CallerAccess;
1574 else /* not found, add a new one if possible */
1575 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1577 #ifdef AFS_LINUX22_ENV
1578 vcache2inode(avc); /* Set the inode attr cache */
1581 } /*afs_ProcessFS */
1585 afs_RemoteLookup(register struct VenusFid *afid, struct vrequest *areq,
1586 char *name, struct VenusFid *nfid,
1587 struct AFSFetchStatus *OutStatusp,
1588 struct AFSCallBack *CallBackp, struct server **serverp,
1589 struct AFSVolSync *tsyncp)
1593 register struct conn *tc;
1594 struct AFSFetchStatus OutDirStatus;
1597 name = ""; /* XXX */
1599 tc = afs_Conn(afid, areq, SHARED_LOCK);
1602 *serverp = tc->srvr->server;
1604 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1607 RXAFS_Lookup(tc->id, (struct AFSFid *)&afid->Fid, name,
1608 (struct AFSFid *)&nfid->Fid, OutStatusp,
1609 &OutDirStatus, CallBackp, tsyncp);
1614 } while (afs_Analyze
1615 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_XLOOKUP, SHARED_LOCK,
1626 * Given a file id and a vrequest structure, fetch the status
1627 * information associated with the file.
1631 * areq : Ptr to associated vrequest structure, specifying the
1632 * user whose authentication tokens will be used.
1633 * avc : caller may already have a vcache for this file, which is
1637 * The cache entry is returned with an increased vrefCount field.
1638 * The entry must be discarded by calling afs_PutVCache when you
1639 * are through using the pointer to the cache entry.
1641 * You should not hold any locks when calling this function, except
1642 * locks on other vcache entries. If you lock more than one vcache
1643 * entry simultaneously, you should lock them in this order:
1645 * 1. Lock all files first, then directories.
1646 * 2. Within a particular type, lock entries in Fid.Vnode order.
1648 * This locking hierarchy is convenient because it allows locking
1649 * of a parent dir cache entry, given a file (to check its access
1650 * control list). It also allows renames to be handled easily by
1651 * locking directories in a constant order.
1652 * NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1654 /* might have a vcache structure already, which must
1655 * already be held by the caller */
1658 afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
1659 afs_int32 * cached, struct vcache *avc)
1662 afs_int32 code, newvcache = 0;
1663 register struct vcache *tvc;
1667 AFS_STATCNT(afs_GetVCache);
1670 *cached = 0; /* Init just in case */
1672 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1676 ObtainSharedLock(&afs_xvcache, 5);
1678 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU);
1680 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1681 ReleaseSharedLock(&afs_xvcache);
1682 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1690 if (tvc->states & CStatd) {
1691 ReleaseSharedLock(&afs_xvcache);
1695 UpgradeSToWLock(&afs_xvcache, 21);
1697 /* no cache entry, better grab one */
1698 tvc = afs_NewVCache(afid, NULL);
1701 ConvertWToSLock(&afs_xvcache);
1702 afs_stats_cmperf.vcacheMisses++;
1705 ReleaseSharedLock(&afs_xvcache);
1707 ObtainWriteLock(&tvc->lock, 54);
1709 if (tvc->states & CStatd) {
1710 #ifdef AFS_LINUX22_ENV
1713 ReleaseWriteLock(&tvc->lock);
1716 #if defined(AFS_OSF_ENV)
1717 if (afs_IsWired(tvc)) {
1718 ReleaseWriteLock(&tvc->lock);
1721 #endif /* AFS_OSF_ENV */
1722 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
1724 * XXX - I really don't like this. Should try to understand better.
1725 * It seems that sometimes, when we get called, we already hold the
1726 * lock on the vnode (e.g., from afs_getattr via afs_VerifyVCache).
1727 * We can't drop the vnode lock, because that could result in a race.
1728 * Sometimes, though, we get here and don't hold the vnode lock.
1729 * I hate code paths that sometimes hold locks and sometimes don't.
1730 * In any event, the dodge we use here is to check whether the vnode
1731 * is locked, and if it isn't, then we gain and drop it around the call
1732 * to vinvalbuf; otherwise, we leave it alone.
1735 struct vnode *vp = AFSTOV(tvc);
1738 #if defined(AFS_DARWIN_ENV)
1739 iheldthelock = VOP_ISLOCKED(vp);
1742 /* this is messy. we can call fsync which will try to reobtain this */
1743 if (VTOAFS(vp) == tvc)
1744 ReleaseWriteLock(&tvc->lock);
1745 if (UBCINFOEXISTS(vp)) {
1746 vinvalbuf(vp, V_SAVE, &afs_osi_cred, current_proc(), PINOD, 0);
1748 if (VTOAFS(vp) == tvc)
1749 ObtainWriteLock(&tvc->lock, 954);
1751 #ifdef AFS_DARWIN80_ENV
1754 VOP_UNLOCK(vp, LK_EXCLUSIVE, current_proc());
1756 #elif defined(AFS_FBSD60_ENV)
1757 iheldthelock = VOP_ISLOCKED(vp, curthread);
1759 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1760 vinvalbuf(vp, V_SAVE, curthread, PINOD, 0);
1762 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1763 #elif defined(AFS_FBSD50_ENV)
1764 iheldthelock = VOP_ISLOCKED(vp, curthread);
1766 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1767 vinvalbuf(vp, V_SAVE, osi_curcred(), curthread, PINOD, 0);
1769 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1770 #elif defined(AFS_FBSD40_ENV)
1771 iheldthelock = VOP_ISLOCKED(vp, curproc);
1773 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1774 vinvalbuf(vp, V_SAVE, osi_curcred(), curproc, PINOD, 0);
1776 VOP_UNLOCK(vp, LK_EXCLUSIVE, curproc);
1777 #elif defined(AFS_OBSD_ENV)
1778 iheldthelock = VOP_ISLOCKED(vp, curproc);
1780 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1781 uvm_vnp_uncache(vp);
1783 VOP_UNLOCK(vp, 0, curproc);
1788 ObtainWriteLock(&afs_xcbhash, 464);
1789 tvc->states &= ~CUnique;
1791 afs_DequeueCallback(tvc);
1792 ReleaseWriteLock(&afs_xcbhash);
1794 /* It is always appropriate to throw away all the access rights? */
1795 afs_FreeAllAxs(&(tvc->Access));
1796 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1798 if ((tvp->states & VForeign)) {
1800 tvc->states |= CForeign;
1801 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1802 && (tvp->rootUnique == afid->Fid.Unique)) {
1806 if (tvp->states & VRO)
1808 if (tvp->states & VBackup)
1809 tvc->states |= CBackup;
1810 /* now copy ".." entry back out of volume structure, if necessary */
1811 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1813 tvc->mvid = (struct VenusFid *)
1814 osi_AllocSmallSpace(sizeof(struct VenusFid));
1815 *tvc->mvid = tvp->dotdot;
1817 afs_PutVolume(tvp, READ_LOCK);
1821 afs_RemoveVCB(afid);
1823 struct AFSFetchStatus OutStatus;
1825 if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1826 afs_ProcessFS(tvc, &OutStatus, areq);
1827 tvc->states |= CStatd | CUnique;
1830 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1835 ReleaseWriteLock(&tvc->lock);
1837 ObtainReadLock(&afs_xvcache);
1839 ReleaseReadLock(&afs_xvcache);
1843 ReleaseWriteLock(&tvc->lock);
1846 } /*afs_GetVCache */
1851 afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1852 afs_int32 * cached, struct vcache *adp, char *aname)
1854 afs_int32 code, now, newvcache = 0;
1855 struct VenusFid nfid;
1856 register struct vcache *tvc;
1858 struct AFSFetchStatus OutStatus;
1859 struct AFSCallBack CallBack;
1860 struct AFSVolSync tsync;
1861 struct server *serverp = 0;
1865 AFS_STATCNT(afs_GetVCache);
1867 *cached = 0; /* Init just in case */
1869 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1873 ObtainReadLock(&afs_xvcache);
1874 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */ );
1877 ReleaseReadLock(&afs_xvcache);
1879 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1880 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1884 ObtainReadLock(&tvc->lock);
1886 if (tvc->states & CStatd) {
1890 ReleaseReadLock(&tvc->lock);
1893 tvc->states &= ~CUnique;
1895 ReleaseReadLock(&tvc->lock);
1896 ObtainReadLock(&afs_xvcache);
1900 ReleaseReadLock(&afs_xvcache);
1902 /* lookup the file */
1905 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
1907 afs_RemoteLookup(&adp->fid, areq, aname, &nfid, &OutStatus, &CallBack,
1910 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1914 ObtainSharedLock(&afs_xvcache, 6);
1915 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU /* no xstats now */ );
1917 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1918 ReleaseSharedLock(&afs_xvcache);
1919 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1925 /* no cache entry, better grab one */
1926 UpgradeSToWLock(&afs_xvcache, 22);
1927 tvc = afs_NewVCache(&nfid, serverp);
1929 ConvertWToSLock(&afs_xvcache);
1932 ReleaseSharedLock(&afs_xvcache);
1933 ObtainWriteLock(&tvc->lock, 55);
1935 /* It is always appropriate to throw away all the access rights? */
1936 afs_FreeAllAxs(&(tvc->Access));
1937 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
1939 if ((tvp->states & VForeign)) {
1941 tvc->states |= CForeign;
1942 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1943 && (tvp->rootUnique == afid->Fid.Unique))
1946 if (tvp->states & VRO)
1948 if (tvp->states & VBackup)
1949 tvc->states |= CBackup;
1950 /* now copy ".." entry back out of volume structure, if necessary */
1951 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1953 tvc->mvid = (struct VenusFid *)
1954 osi_AllocSmallSpace(sizeof(struct VenusFid));
1955 *tvc->mvid = tvp->dotdot;
1960 ObtainWriteLock(&afs_xcbhash, 465);
1961 afs_DequeueCallback(tvc);
1962 tvc->states &= ~(CStatd | CUnique);
1963 ReleaseWriteLock(&afs_xcbhash);
1964 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1965 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
1967 afs_PutVolume(tvp, READ_LOCK);
1968 ReleaseWriteLock(&tvc->lock);
1969 ObtainReadLock(&afs_xvcache);
1971 ReleaseReadLock(&afs_xvcache);
1975 ObtainWriteLock(&afs_xcbhash, 466);
1976 if (origCBs == afs_allCBs) {
1977 if (CallBack.ExpirationTime) {
1978 tvc->callback = serverp;
1979 tvc->cbExpires = CallBack.ExpirationTime + now;
1980 tvc->states |= CStatd | CUnique;
1981 tvc->states &= ~CBulkFetching;
1982 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
1983 } else if (tvc->states & CRO) {
1984 /* adapt gives us an hour. */
1985 tvc->cbExpires = 3600 + osi_Time();
1986 /*XXX*/ tvc->states |= CStatd | CUnique;
1987 tvc->states &= ~CBulkFetching;
1988 afs_QueueCallback(tvc, CBHash(3600), tvp);
1990 tvc->callback = NULL;
1991 afs_DequeueCallback(tvc);
1992 tvc->states &= ~(CStatd | CUnique);
1993 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1994 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
1997 afs_DequeueCallback(tvc);
1998 tvc->states &= ~CStatd;
1999 tvc->states &= ~CUnique;
2000 tvc->callback = NULL;
2001 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2002 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2004 ReleaseWriteLock(&afs_xcbhash);
2006 afs_PutVolume(tvp, READ_LOCK);
2007 afs_ProcessFS(tvc, &OutStatus, areq);
2009 ReleaseWriteLock(&tvc->lock);
2015 afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
2016 afs_int32 * cached, struct volume *tvolp)
2018 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
2019 afs_int32 getNewFid = 0;
2021 struct VenusFid nfid;
2022 register struct vcache *tvc;
2023 struct server *serverp = 0;
2024 struct AFSFetchStatus OutStatus;
2025 struct AFSCallBack CallBack;
2026 struct AFSVolSync tsync;
2032 if (!tvolp->rootVnode || getNewFid) {
2033 struct VenusFid tfid;
2036 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2037 origCBs = afs_allCBs; /* ignore InitCallBackState */
2039 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2044 /* ReleaseReadLock(&tvolp->lock); */
2045 ObtainWriteLock(&tvolp->lock, 56);
2046 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
2047 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
2048 ReleaseWriteLock(&tvolp->lock);
2049 /* ObtainReadLock(&tvolp->lock);*/
2052 afid->Fid.Vnode = tvolp->rootVnode;
2053 afid->Fid.Unique = tvolp->rootUnique;
2056 ObtainSharedLock(&afs_xvcache, 7);
2058 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2059 if (!FidCmp(&(tvc->fid), afid)) {
2061 /* Grab this vnode, possibly reactivating from the free list */
2062 /* for the present (95.05.25) everything on the hash table is
2063 * definitively NOT in the free list -- at least until afs_reclaim
2064 * can be safely implemented */
2067 vg = vget(AFSTOV(tvc)); /* this bumps ref count */
2071 #endif /* AFS_OSF_ENV */
2076 if (!haveStatus && (!tvc || !(tvc->states & CStatd))) {
2077 /* Mount point no longer stat'd or unknown. FID may have changed. */
2080 AFS_RELE(AFSTOV(tvc));
2084 ReleaseSharedLock(&afs_xvcache);
2089 UpgradeSToWLock(&afs_xvcache, 23);
2090 /* no cache entry, better grab one */
2091 tvc = afs_NewVCache(afid, NULL);
2093 afs_stats_cmperf.vcacheMisses++;
2097 afs_stats_cmperf.vcacheHits++;
2099 /* we already bumped the ref count in the for loop above */
2100 #else /* AFS_OSF_ENV */
2103 UpgradeSToWLock(&afs_xvcache, 24);
2104 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2105 refpanic("GRVC VLRU inconsistent0");
2107 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2108 refpanic("GRVC VLRU inconsistent1");
2110 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2111 refpanic("GRVC VLRU inconsistent2");
2113 QRemove(&tvc->vlruq); /* move to lruq head */
2114 QAdd(&VLRU, &tvc->vlruq);
2115 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2116 refpanic("GRVC VLRU inconsistent3");
2118 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2119 refpanic("GRVC VLRU inconsistent4");
2121 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2122 refpanic("GRVC VLRU inconsistent5");
2127 ReleaseWriteLock(&afs_xvcache);
2129 if (tvc->states & CStatd) {
2133 ObtainReadLock(&tvc->lock);
2134 tvc->states &= ~CUnique;
2135 tvc->callback = NULL; /* redundant, perhaps */
2136 ReleaseReadLock(&tvc->lock);
2139 ObtainWriteLock(&tvc->lock, 57);
2141 /* It is always appropriate to throw away all the access rights? */
2142 afs_FreeAllAxs(&(tvc->Access));
2145 tvc->states |= CForeign;
2146 if (tvolp->states & VRO)
2148 if (tvolp->states & VBackup)
2149 tvc->states |= CBackup;
2150 /* now copy ".." entry back out of volume structure, if necessary */
2151 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2152 && (tvolp->rootUnique == afid->Fid.Unique)) {
2155 if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2157 tvc->mvid = (struct VenusFid *)
2158 osi_AllocSmallSpace(sizeof(struct VenusFid));
2159 *tvc->mvid = tvolp->dotdot;
2163 afs_RemoveVCB(afid);
2166 struct VenusFid tfid;
2169 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2170 origCBs = afs_allCBs; /* ignore InitCallBackState */
2172 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2177 ObtainWriteLock(&afs_xcbhash, 467);
2178 afs_DequeueCallback(tvc);
2179 tvc->callback = NULL;
2180 tvc->states &= ~(CStatd | CUnique);
2181 ReleaseWriteLock(&afs_xcbhash);
2182 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2183 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2184 ReleaseWriteLock(&tvc->lock);
2185 ObtainReadLock(&afs_xvcache);
2187 ReleaseReadLock(&afs_xvcache);
2191 ObtainWriteLock(&afs_xcbhash, 468);
2192 if (origCBs == afs_allCBs) {
2193 tvc->states |= CTruth;
2194 tvc->callback = serverp;
2195 if (CallBack.ExpirationTime != 0) {
2196 tvc->cbExpires = CallBack.ExpirationTime + start;
2197 tvc->states |= CStatd;
2198 tvc->states &= ~CBulkFetching;
2199 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2200 } else if (tvc->states & CRO) {
2201 /* adapt gives us an hour. */
2202 tvc->cbExpires = 3600 + osi_Time();
2203 /*XXX*/ tvc->states |= CStatd;
2204 tvc->states &= ~CBulkFetching;
2205 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2208 afs_DequeueCallback(tvc);
2209 tvc->callback = NULL;
2210 tvc->states &= ~(CStatd | CUnique);
2211 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2212 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2214 ReleaseWriteLock(&afs_xcbhash);
2215 afs_ProcessFS(tvc, &OutStatus, areq);
2217 ReleaseWriteLock(&tvc->lock);
2224 * must be called with avc write-locked
2225 * don't absolutely have to invalidate the hint unless the dv has
2226 * changed, but be sure to get it right else there will be consistency bugs.
2229 afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
2230 struct vrequest * areq, struct AFSFetchStatus * Outsp)
2233 afs_uint32 start = 0;
2234 register struct conn *tc;
2235 struct AFSCallBack CallBack;
2236 struct AFSVolSync tsync;
2237 struct volume *volp;
2240 tc = afs_Conn(afid, areq, SHARED_LOCK);
2241 avc->quick.stamp = 0;
2242 avc->h1.dchint = NULL; /* invalidate hints */
2244 avc->callback = tc->srvr->server;
2246 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2249 RXAFS_FetchStatus(tc->id, (struct AFSFid *)&afid->Fid, Outsp,
2257 } while (afs_Analyze
2258 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2259 SHARED_LOCK, NULL));
2262 afs_ProcessFS(avc, Outsp, areq);
2263 volp = afs_GetVolume(afid, areq, READ_LOCK);
2264 ObtainWriteLock(&afs_xcbhash, 469);
2265 avc->states |= CTruth;
2266 if (avc->callback /* check for race */ ) {
2267 if (CallBack.ExpirationTime != 0) {
2268 avc->cbExpires = CallBack.ExpirationTime + start;
2269 avc->states |= CStatd;
2270 avc->states &= ~CBulkFetching;
2271 afs_QueueCallback(avc, CBHash(CallBack.ExpirationTime), volp);
2272 } else if (avc->states & CRO) { /* ordinary callback on a read-only volume -- AFS 3.2 style */
2273 avc->cbExpires = 3600 + start;
2274 avc->states |= CStatd;
2275 avc->states &= ~CBulkFetching;
2276 afs_QueueCallback(avc, CBHash(3600), volp);
2278 afs_DequeueCallback(avc);
2279 avc->callback = NULL;
2280 avc->states &= ~(CStatd | CUnique);
2281 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2282 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2285 afs_DequeueCallback(avc);
2286 avc->callback = NULL;
2287 avc->states &= ~(CStatd | CUnique);
2288 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2289 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2291 ReleaseWriteLock(&afs_xcbhash);
2293 afs_PutVolume(volp, READ_LOCK);
2295 /* used to undo the local callback, but that's too extreme.
2296 * There are plenty of good reasons that fetchstatus might return
2297 * an error, such as EPERM. If we have the vnode cached, statd,
2298 * with callback, might as well keep track of the fact that we
2299 * don't have access...
2301 if (code == EPERM || code == EACCES) {
2302 struct axscache *ac;
2303 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2305 else /* not found, add a new one if possible */
2306 afs_AddAxs(avc->Access, areq->uid, 0);
2317 * Stuff some information into the vcache for the given file.
2320 * afid : File in question.
2321 * OutStatus : Fetch status on the file.
2322 * CallBack : Callback info.
2323 * tc : RPC connection involved.
2324 * areq : vrequest involved.
2327 * Nothing interesting.
2330 afs_StuffVcache(register struct VenusFid *afid,
2331 struct AFSFetchStatus *OutStatus,
2332 struct AFSCallBack *CallBack, register struct conn *tc,
2333 struct vrequest *areq)
2335 register afs_int32 code, i, newvcache = 0;
2336 register struct vcache *tvc;
2337 struct AFSVolSync tsync;
2339 struct axscache *ac;
2342 AFS_STATCNT(afs_StuffVcache);
2343 #ifdef IFS_VCACHECOUNT
2348 ObtainSharedLock(&afs_xvcache, 8);
2350 tvc = afs_FindVCache(afid, &retry, DO_VLRU /* no stats */ );
2352 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2353 ReleaseSharedLock(&afs_xvcache);
2354 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2360 /* no cache entry, better grab one */
2361 UpgradeSToWLock(&afs_xvcache, 25);
2362 tvc = afs_NewVCache(afid, NULL);
2364 ConvertWToSLock(&afs_xvcache);
2367 ReleaseSharedLock(&afs_xvcache);
2368 ObtainWriteLock(&tvc->lock, 58);
2370 tvc->states &= ~CStatd;
2371 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2372 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2374 /* Is it always appropriate to throw away all the access rights? */
2375 afs_FreeAllAxs(&(tvc->Access));
2377 /*Copy useful per-volume info */
2378 tvp = afs_GetVolume(afid, areq, READ_LOCK);
2380 if (newvcache && (tvp->states & VForeign))
2381 tvc->states |= CForeign;
2382 if (tvp->states & VRO)
2384 if (tvp->states & VBackup)
2385 tvc->states |= CBackup;
2387 * Now, copy ".." entry back out of volume structure, if
2390 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2392 tvc->mvid = (struct VenusFid *)
2393 osi_AllocSmallSpace(sizeof(struct VenusFid));
2394 *tvc->mvid = tvp->dotdot;
2397 /* store the stat on the file */
2398 afs_RemoveVCB(afid);
2399 afs_ProcessFS(tvc, OutStatus, areq);
2400 tvc->callback = tc->srvr->server;
2402 /* we use osi_Time twice below. Ideally, we would use the time at which
2403 * the FetchStatus call began, instead, but we don't have it here. So we
2404 * make do with "now". In the CRO case, it doesn't really matter. In
2405 * the other case, we hope that the difference between "now" and when the
2406 * call actually began execution on the server won't be larger than the
2407 * padding which the server keeps. Subtract 1 second anyway, to be on
2408 * the safe side. Can't subtract more because we don't know how big
2409 * ExpirationTime is. Possible consistency problems may arise if the call
2410 * timeout period becomes longer than the server's expiration padding. */
2411 ObtainWriteLock(&afs_xcbhash, 470);
2412 if (CallBack->ExpirationTime != 0) {
2413 tvc->cbExpires = CallBack->ExpirationTime + osi_Time() - 1;
2414 tvc->states |= CStatd;
2415 tvc->states &= ~CBulkFetching;
2416 afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2417 } else if (tvc->states & CRO) {
2418 /* old-fashioned AFS 3.2 style */
2419 tvc->cbExpires = 3600 + osi_Time();
2420 /*XXX*/ tvc->states |= CStatd;
2421 tvc->states &= ~CBulkFetching;
2422 afs_QueueCallback(tvc, CBHash(3600), tvp);
2424 afs_DequeueCallback(tvc);
2425 tvc->callback = NULL;
2426 tvc->states &= ~(CStatd | CUnique);
2427 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2428 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2430 ReleaseWriteLock(&afs_xcbhash);
2432 afs_PutVolume(tvp, READ_LOCK);
2434 /* look in per-pag cache */
2435 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2436 ac->axess = OutStatus->CallerAccess; /* substitute pags */
2437 else /* not found, add a new one if possible */
2438 afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2440 ReleaseWriteLock(&tvc->lock);
2441 afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2442 ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32,
2443 tvc->cbExpires, ICL_TYPE_INT32, tvc->cbExpires - osi_Time());
2445 * Release ref count... hope this guy stays around...
2448 } /*afs_StuffVcache */
2455 * Decrements the reference count on a cache entry.
2458 * avc : Pointer to the cache entry to decrement.
2461 * Nothing interesting.
2464 afs_PutVCache(register struct vcache *avc)
2466 AFS_STATCNT(afs_PutVCache);
2468 * Can we use a read lock here?
2470 ObtainReadLock(&afs_xvcache);
2472 ReleaseReadLock(&afs_xvcache);
2473 } /*afs_PutVCache */
2479 * Find a vcache entry given a fid.
2482 * afid : Pointer to the fid whose cache entry we desire.
2483 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2484 * unlock the vnode, and try again.
2485 * flags: bit 1 to specify whether to compute hit statistics. Not
2486 * set if FindVCache is called as part of internal bookkeeping.
2489 * Must be called with the afs_xvcache lock at least held at
2490 * the read level. In order to do the VLRU adjustment, the xvcache lock
2491 * must be shared-- we upgrade it here.
2495 afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
2498 register struct vcache *tvc;
2501 AFS_STATCNT(afs_FindVCache);
2504 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2505 if (FidMatches(afid, tvc)) {
2507 /* Grab this vnode, possibly reactivating from the free list */
2510 vg = vget(AFSTOV(tvc));
2514 #endif /* AFS_OSF_ENV */
2519 /* should I have a read lock on the vnode here? */
2523 #if !defined(AFS_OSF_ENV)
2524 osi_vnhold(tvc, retry); /* already held, above */
2525 if (retry && *retry)
2528 #ifdef AFS_DARWIN_ENV
2529 tvc->states |= CUBCinit;
2531 if (UBCINFOMISSING(AFSTOV(tvc)) ||
2532 UBCINFORECLAIMED(AFSTOV(tvc))) {
2533 ubc_info_init(AFSTOV(tvc));
2536 tvc->states &= ~CUBCinit;
2539 * only move to front of vlru if we have proper vcache locking)
2541 if (flag & DO_VLRU) {
2542 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2543 refpanic("FindVC VLRU inconsistent1");
2545 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2546 refpanic("FindVC VLRU inconsistent1");
2548 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2549 refpanic("FindVC VLRU inconsistent2");
2551 UpgradeSToWLock(&afs_xvcache, 26);
2552 QRemove(&tvc->vlruq);
2553 QAdd(&VLRU, &tvc->vlruq);
2554 ConvertWToSLock(&afs_xvcache);
2555 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2556 refpanic("FindVC VLRU inconsistent1");
2558 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2559 refpanic("FindVC VLRU inconsistent2");
2561 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2562 refpanic("FindVC VLRU inconsistent3");
2568 if (flag & DO_STATS) {
2570 afs_stats_cmperf.vcacheHits++;
2572 afs_stats_cmperf.vcacheMisses++;
2573 if (afs_IsPrimaryCellNum(afid->Cell))
2574 afs_stats_cmperf.vlocalAccesses++;
2576 afs_stats_cmperf.vremoteAccesses++;
2578 #ifdef AFS_LINUX22_ENV
2579 if (tvc && (tvc->states & CStatd))
2580 vcache2inode(tvc); /* mainly to reset i_nlink */
2583 } /*afs_FindVCache */
2589 * Find a vcache entry given a fid. Does a wildcard match on what we
2590 * have for the fid. If more than one entry, don't return anything.
2593 * avcp : Fill in pointer if we found one and only one.
2594 * afid : Pointer to the fid whose cache entry we desire.
2595 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2596 * unlock the vnode, and try again.
2597 * flags: bit 1 to specify whether to compute hit statistics. Not
2598 * set if FindVCache is called as part of internal bookkeeping.
2601 * Must be called with the afs_xvcache lock at least held at
2602 * the read level. In order to do the VLRU adjustment, the xvcache lock
2603 * must be shared-- we upgrade it here.
2606 * number of matches found.
2609 int afs_duplicate_nfs_fids = 0;
2612 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2614 register struct vcache *tvc;
2616 afs_int32 count = 0;
2617 struct vcache *found_tvc = NULL;
2619 AFS_STATCNT(afs_FindVCache);
2621 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2625 ObtainSharedLock(&afs_xvcache, 331);
2628 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2629 /* Match only on what we have.... */
2630 if (((tvc->fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2631 && (tvc->fid.Fid.Volume == afid->Fid.Volume)
2632 && ((tvc->fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2633 && (tvc->fid.Cell == afid->Cell)) {
2635 /* Grab this vnode, possibly reactivating from the free list */
2638 vg = vget(AFSTOV(tvc));
2641 /* This vnode no longer exists. */
2644 #endif /* AFS_OSF_ENV */
2649 /* Drop our reference counts. */
2651 vrele(AFSTOV(found_tvc));
2653 afs_duplicate_nfs_fids++;
2654 ReleaseSharedLock(&afs_xvcache);
2662 /* should I have a read lock on the vnode here? */
2664 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2665 afs_int32 retry = 0;
2666 osi_vnhold(tvc, &retry);
2669 found_tvc = (struct vcache *)0;
2670 ReleaseSharedLock(&afs_xvcache);
2671 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2675 #if !defined(AFS_OSF_ENV)
2676 osi_vnhold(tvc, (int *)0); /* already held, above */
2680 * We obtained the xvcache lock above.
2682 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2683 refpanic("FindVC VLRU inconsistent1");
2685 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2686 refpanic("FindVC VLRU inconsistent1");
2688 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2689 refpanic("FindVC VLRU inconsistent2");
2691 UpgradeSToWLock(&afs_xvcache, 568);
2692 QRemove(&tvc->vlruq);
2693 QAdd(&VLRU, &tvc->vlruq);
2694 ConvertWToSLock(&afs_xvcache);
2695 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2696 refpanic("FindVC VLRU inconsistent1");
2698 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2699 refpanic("FindVC VLRU inconsistent2");
2701 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2702 refpanic("FindVC VLRU inconsistent3");
2708 afs_stats_cmperf.vcacheHits++;
2710 afs_stats_cmperf.vcacheMisses++;
2711 if (afs_IsPrimaryCellNum(afid->Cell))
2712 afs_stats_cmperf.vlocalAccesses++;
2714 afs_stats_cmperf.vremoteAccesses++;
2716 *avcp = tvc; /* May be null */
2718 ReleaseSharedLock(&afs_xvcache);
2719 return (tvc ? 1 : 0);
2721 } /*afs_NFSFindVCache */
2729 * Initialize vcache related variables
2732 afs_vcacheInit(int astatSize)
2734 register struct vcache *tvp;
2736 #if defined(AFS_OSF_ENV)
2737 if (!afs_maxvcount) {
2738 #if defined(AFS_OSF30_ENV)
2739 afs_maxvcount = max_vnodes / 2; /* limit ourselves to half the total */
2741 afs_maxvcount = nvnode / 2; /* limit ourselves to half the total */
2743 if (astatSize < afs_maxvcount) {
2744 afs_maxvcount = astatSize;
2747 #else /* AFS_OSF_ENV */
2751 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2752 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2754 #if !defined(AFS_OSF_ENV)
2755 #ifdef AFS_LINUX26_ENV
2756 printf("old style would have needed %d contiguous bytes\n", astatSize *
2757 sizeof(struct vcache));
2758 Initial_freeVCList = freeVCList = tvp = (struct vcache *)
2759 afs_osi_Alloc(sizeof(struct vcache));
2760 for (i = 0; i < astatSize; i++) {
2761 tvp->nextfree = (struct vcache *) afs_osi_Alloc(sizeof(struct vcache));
2762 tvp = tvp->nextfree;
2764 tvp->nextfree = NULL;
2766 /* Allocate and thread the struct vcache entries */
2767 tvp = (struct vcache *)afs_osi_Alloc(astatSize * sizeof(struct vcache));
2768 memset((char *)tvp, 0, sizeof(struct vcache) * astatSize);
2770 Initial_freeVCList = tvp;
2771 freeVCList = &(tvp[0]);
2772 for (i = 0; i < astatSize - 1; i++) {
2773 tvp[i].nextfree = &(tvp[i + 1]);
2775 tvp[astatSize - 1].nextfree = NULL;
2776 #ifdef KERNEL_HAVE_PIN
2777 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
2782 #if defined(AFS_SGI_ENV)
2783 for (i = 0; i < astatSize; i++) {
2784 char name[METER_NAMSZ];
2785 struct vcache *tvc = &tvp[i];
2787 tvc->v.v_number = ++afsvnumbers;
2788 tvc->vc_rwlockid = OSI_NO_LOCKID;
2789 initnsema(&tvc->vc_rwlock, 1,
2790 makesname(name, "vrw", tvc->v.v_number));
2791 #ifndef AFS_SGI53_ENV
2792 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
2794 #ifndef AFS_SGI62_ENV
2795 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
2796 #endif /* AFS_SGI62_ENV */
2810 shutdown_vcache(void)
2813 struct afs_cbr *tsp, *nsp;
2815 * XXX We may potentially miss some of the vcaches because if when there're no
2816 * free vcache entries and all the vcache entries are active ones then we allocate
2817 * an additional one - admittedly we almost never had that occur.
2821 register struct afs_q *tq, *uq;
2822 register struct vcache *tvc;
2823 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
2827 osi_FreeSmallSpace(tvc->mvid);
2828 tvc->mvid = (struct VenusFid *)0;
2831 aix_gnode_rele(AFSTOV(tvc));
2833 if (tvc->linkData) {
2834 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
2839 * Also free the remaining ones in the Cache
2841 for (i = 0; i < VCSIZE; i++) {
2842 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2844 osi_FreeSmallSpace(tvc->mvid);
2845 tvc->mvid = (struct VenusFid *)0;
2849 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
2850 #ifdef AFS_AIX32_ENV
2853 vms_delete(tvc->segid);
2855 tvc->segid = tvc->vmh = NULL;
2856 if (VREFCOUNT_GT(tvc,0))
2857 osi_Panic("flushVcache: vm race");
2865 #if defined(AFS_SUN5_ENV)
2871 if (tvc->linkData) {
2872 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
2876 afs_FreeAllAxs(&(tvc->Access));
2882 * Free any leftover callback queue
2884 for (tsp = afs_cbrSpace; tsp; tsp = nsp) {
2886 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
2890 #ifdef AFS_LINUX26_ENV
2892 struct vcache *tvp = Initial_freeVCList;
2894 struct vcache *next = tvp->nextfree;
2896 afs_osi_Free(tvp, sizeof(struct vcache));
2901 #ifdef KERNEL_HAVE_PIN
2902 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2904 #if !defined(AFS_OSF_ENV)
2905 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2909 #if !defined(AFS_OSF_ENV)
2910 freeVCList = Initial_freeVCList = 0;
2912 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2913 LOCK_INIT(&afs_xvcb, "afs_xvcb");