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"
43 #include "afs/sysincludes.h" /*Standard vendor system headers*/
44 #include "afsincludes.h" /*AFS-based standard headers*/
45 #include "afs/afs_stats.h"
46 #include "afs/afs_cbqueue.h"
47 #include "afs/afs_osidnlc.h"
50 afs_int32 afs_maxvcount = 0; /* max number of vcache entries */
51 afs_int32 afs_vcount = 0; /* number of vcache in use now */
52 #endif /* AFS_OSF_ENV */
60 #endif /* AFS_SGI64_ENV */
62 /* Exported variables */
63 afs_rwlock_t afs_xvcache; /*Lock: alloc new stat cache entries*/
64 afs_lock_t afs_xvcb; /*Lock: fids on which there are callbacks*/
65 struct vcache *freeVCList; /*Free list for stat cache entries*/
66 struct vcache *Initial_freeVCList; /*Initial list for above*/
67 struct afs_q VLRU; /*vcache LRU*/
68 afs_int32 vcachegen = 0;
69 unsigned int afs_paniconwarn = 0;
70 struct vcache *afs_vhashT[VCSIZE];
71 afs_int32 afs_bulkStatsLost;
72 int afs_norefpanic = 0;
74 /* Forward declarations */
75 static afs_int32 afs_QueueVCB(struct vcache *avc);
82 * Flush the given vcache entry.
85 * avc : Pointer to vcache entry to flush.
86 * slept : Pointer to int to set 1 if we sleep/drop locks, 0 if we don't.
89 * afs_xvcache lock must be held for writing upon entry to
90 * prevent people from changing the vrefCount field, and to
91 * protect the lruq and hnext fields.
92 * LOCK: afs_FlushVCache afs_xvcache W
93 * REFCNT: vcache ref count must be zero on entry except for osf1
94 * RACE: lock is dropped and reobtained, permitting race in caller
97 int afs_FlushVCache(struct vcache *avc, int *slept)
100 register afs_int32 i, code;
101 register struct vcache **uvc, *wvc;
104 AFS_STATCNT(afs_FlushVCache);
105 afs_Trace2(afs_iclSetp, CM_TRACE_FLUSHV, ICL_TYPE_POINTER, avc,
106 ICL_TYPE_INT32, avc->states);
109 VN_LOCK(AFSTOV(avc));
113 code = osi_VM_FlushVCache(avc, slept);
117 if (avc->states & CVFlushed) {
121 if (avc->nextfree || !avc->vlruq.prev || !avc->vlruq.next) { /* qv afs.h */
122 refpanic ("LRU vs. Free inconsistency");
124 avc->states |= CVFlushed;
125 /* pull the entry out of the lruq and put it on the free list */
126 QRemove(&avc->vlruq);
127 avc->vlruq.prev = avc->vlruq.next = (struct afs_q *) 0;
129 /* keep track of # of files that we bulk stat'd, but never used
130 * before they got recycled.
132 if (avc->states & CBulkStat)
135 /* remove entry from the hash chain */
136 i = VCHash(&avc->fid);
137 uvc = &afs_vhashT[i];
138 for(wvc = *uvc; wvc; uvc = &wvc->hnext, wvc = *uvc) {
141 avc->hnext = (struct vcache *) NULL;
145 if (!wvc) osi_Panic("flushvcache"); /* not in correct hash bucket */
146 if (avc->mvid) osi_FreeSmallSpace(avc->mvid);
147 avc->mvid = (struct VenusFid*)0;
149 afs_osi_Free(avc->linkData, strlen(avc->linkData)+1);
150 avc->linkData = NULL;
152 #if defined(AFS_OBSD_ENV)
153 /* OK, there are no internal vrefCounts, so there shouldn't
154 be any more refs here. */
156 avc->v->v_data = NULL; /* remove from vnode */
157 avc->v = NULL; /* also drop the ptr to vnode */
160 afs_FreeAllAxs(&(avc->Access));
162 /* we can't really give back callbacks on RO files, since the
163 * server only tracks them on a per-volume basis, and we don't
164 * know whether we still have some other files from the same
166 if ((avc->states & CRO) == 0 && avc->callback) {
169 ObtainWriteLock(&afs_xcbhash, 460);
170 afs_DequeueCallback(avc); /* remove it from queued callbacks list */
171 avc->states &= ~(CStatd | CUnique);
172 ReleaseWriteLock(&afs_xcbhash);
173 afs_symhint_inval(avc);
174 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
175 osi_dnlc_purgedp (avc); /* if it (could be) a directory */
177 osi_dnlc_purgevp (avc);
180 * Next, keep track of which vnodes we've deleted for create's
181 * optimistic synchronization algorithm
184 if (avc->fid.Fid.Vnode & 1) afs_oddZaps++;
187 #if !defined(AFS_OSF_ENV)
188 /* put the entry in the free list */
189 avc->nextfree = freeVCList;
191 if (avc->vlruq.prev || avc->vlruq.next) {
192 refpanic ("LRU vs. Free inconsistency");
195 /* This should put it back on the vnode free list since usecount is 1 */
198 if (VREFCOUNT(avc) > 0) {
199 VN_UNLOCK(AFSTOV(avc));
200 AFS_RELE(AFSTOV(avc));
202 if (afs_norefpanic) {
203 printf ("flush vc refcnt < 1");
205 (void) vgone(avc, VX_NOSLEEP, NULL);
207 VN_UNLOCK(AFSTOV(avc));
209 else osi_Panic ("flush vc refcnt < 1");
211 #endif /* AFS_OSF_ENV */
212 avc->states |= CVFlushed;
217 VN_UNLOCK(AFSTOV(avc));
221 } /*afs_FlushVCache*/
227 * The core of the inactive vnode op for all but IRIX.
229 void afs_InactiveVCache(struct vcache *avc, struct AFS_UCRED *acred)
231 AFS_STATCNT(afs_inactive);
232 if (avc->states & CDirty) {
233 /* we can't keep trying to push back dirty data forever. Give up. */
234 afs_InvalidateAllSegments(avc); /* turns off dirty bit */
236 avc->states &= ~CMAPPED; /* mainly used by SunOS 4.0.x */
237 avc->states &= ~CDirty; /* Turn it off */
238 if (avc->states & CUnlinked) {
239 if (CheckLock(&afs_xvcache) || CheckLock(&afs_xdcache)) {
240 avc->states |= CUnlinkedDel;
243 afs_remunlink(avc, 1); /* ignore any return code */
252 * Description: allocate a callback return structure from the
253 * free list and return it.
255 * Env: The alloc and free routines are both called with the afs_xvcb lock
256 * held, so we don't have to worry about blocking in osi_Alloc.
258 static struct afs_cbr *afs_cbrSpace = 0;
259 struct afs_cbr *afs_AllocCBR(void)
261 register struct afs_cbr *tsp;
264 while (!afs_cbrSpace) {
265 if (afs_stats_cmperf.CallBackAlloced >= 2) {
266 /* don't allocate more than 2 * AFS_NCBRS for now */
268 afs_stats_cmperf.CallBackFlushes++;
272 tsp = (struct afs_cbr *) afs_osi_Alloc(AFS_NCBRS * sizeof(struct afs_cbr));
273 for(i=0; i < AFS_NCBRS-1; i++) {
274 tsp[i].next = &tsp[i+1];
276 tsp[AFS_NCBRS-1].next = 0;
278 afs_stats_cmperf.CallBackAlloced++;
282 afs_cbrSpace = tsp->next;
289 * Description: free a callback return structure.
292 * asp -- the address of the structure to free.
294 * Environment: the xvcb lock is held over these calls.
296 int afs_FreeCBR(register struct afs_cbr *asp)
298 asp->next = afs_cbrSpace;
306 * Description: flush all queued callbacks to all servers.
310 * Environment: holds xvcb lock over RPC to guard against race conditions
311 * when a new callback is granted for the same file later on.
313 afs_int32 afs_FlushVCBs (afs_int32 lockit)
315 struct AFSFid tfids[AFS_MAXCBRSCALL];
316 struct AFSCallBack callBacks[1];
317 struct AFSCBFids fidArray;
318 struct AFSCBs cbArray;
320 struct afs_cbr *tcbrp;
324 struct vrequest treq;
326 int safety1, safety2, safety3;
329 if ((code = afs_InitReq(&treq, &afs_osi_cred))) return code;
330 treq.flags |= O_NONBLOCK;
332 if (lockit) MObtainWriteLock(&afs_xvcb,273);
333 ObtainReadLock(&afs_xserver);
334 for(i=0; i<NSERVERS; i++) {
335 for(safety1 = 0, tsp = afs_servers[i];
336 tsp && safety1 < afs_totalServers+10; tsp=tsp->next, safety1++) {
338 if (tsp->cbrs == (struct afs_cbr *) 0) continue;
340 /* otherwise, grab a block of AFS_MAXCBRSCALL from the list
341 * and make an RPC, over and over again.
343 tcount = 0; /* number found so far */
344 for (safety2 = 0; safety2 < afs_cacheStats ; safety2++) {
345 if (tcount >= AFS_MAXCBRSCALL || !tsp->cbrs) {
346 /* if buffer is full, or we've queued all we're going
347 * to from this server, we should flush out the
350 fidArray.AFSCBFids_len = tcount;
351 fidArray.AFSCBFids_val = (struct AFSFid *) tfids;
352 cbArray.AFSCBs_len = 1;
353 cbArray.AFSCBs_val = callBacks;
354 callBacks[0].CallBackType = CB_EXCLUSIVE;
355 for (safety3 = 0; safety3 < MAXHOSTS*2; safety3++) {
356 tc = afs_ConnByHost(tsp, tsp->cell->fsport,
357 tsp->cell->cellNum, &treq, 0,
360 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS);
362 code = RXAFS_GiveUpCallBacks(tc->id, &fidArray,
368 if (!afs_Analyze(tc, code, 0, &treq,
369 AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS,
370 SHARED_LOCK, tsp->cell)) {
374 /* ignore return code, since callbacks may have
375 * been returned anyway, we shouldn't leave them
376 * around to be returned again.
378 * Next, see if we are done with this server, and if so,
379 * break to deal with the next one.
381 if (!tsp->cbrs) break;
383 } /* if to flush full buffer */
384 /* if we make it here, we have an entry at the head of cbrs,
385 * which we should copy to the file ID array and then free.
388 tfids[tcount++] = tcbrp->fid;
389 tsp->cbrs = tcbrp->next;
391 } /* while loop for this one server */
392 if (safety2 > afs_cacheStats) {
393 afs_warn("possible internal error afs_flushVCBs (%d)\n", safety2);
395 } /* for loop for this hash chain */
396 } /* loop through all hash chains */
397 if (safety1 > afs_totalServers+2) {
398 afs_warn("AFS internal error (afs_flushVCBs) (%d > %d), continuing...\n", safety1, afs_totalServers+2);
400 osi_Panic("afs_flushVCBS safety1");
403 ReleaseReadLock(&afs_xserver);
404 if (lockit) MReleaseWriteLock(&afs_xvcb);
412 * Queue a callback on the given fid.
418 * Locks the xvcb lock.
419 * Called when the xvcache lock is already held.
422 static afs_int32 afs_QueueVCB(struct vcache *avc)
424 register struct server *tsp;
425 register struct afs_cbr *tcbp;
427 AFS_STATCNT(afs_QueueVCB);
428 /* The callback is really just a struct server ptr. */
429 tsp = (struct server *)(avc->callback);
431 /* we now have a pointer to the server, so we just allocate
432 * a queue entry and queue it.
434 MObtainWriteLock(&afs_xvcb,274);
435 tcbp = afs_AllocCBR();
436 tcbp->fid = avc->fid.Fid;
437 tcbp->next = tsp->cbrs;
440 /* now release locks and return */
441 MReleaseWriteLock(&afs_xvcb);
450 * Remove a queued callback by looking through all the servers
451 * to see if any have this callback queued.
454 * afid: The fid we want cleansed of queued callbacks.
457 * Locks xvcb and xserver locks.
458 * Typically called with xdcache, xvcache and/or individual vcache
462 int afs_RemoveVCB(struct VenusFid *afid)
465 register struct server *tsp;
466 register struct afs_cbr *tcbrp;
467 struct afs_cbr **lcbrpp;
469 AFS_STATCNT(afs_RemoveVCB);
470 MObtainWriteLock(&afs_xvcb,275);
471 ObtainReadLock(&afs_xserver);
472 for(i=0;i<NSERVERS;i++) {
473 for(tsp=afs_servers[i]; tsp; tsp=tsp->next) {
474 /* if cell is known, and is wrong, then skip this server */
475 if (tsp->cell && tsp->cell->cellNum != afid->Cell) continue;
478 * Otherwise, iterate through file IDs we're sending to the
481 lcbrpp = &tsp->cbrs; /* first queued return callback */
482 for(tcbrp = *lcbrpp; tcbrp; lcbrpp = &tcbrp->next, tcbrp = *lcbrpp) {
483 if (afid->Fid.Volume == tcbrp->fid.Volume &&
484 afid->Fid.Unique == tcbrp->fid.Unique &&
485 afid->Fid.Vnode == tcbrp->fid.Vnode) {
486 *lcbrpp = tcbrp->next; /* unthread from list */
494 ReleaseReadLock(&afs_xserver);
495 MReleaseWriteLock(&afs_xvcb);
499 #ifdef AFS_LINUX22_ENV
501 static void __shrink_dcache_parent(struct dentry * parent)
503 struct dentry *this_parent = parent;
504 struct list_head *next;
506 LIST_HEAD(afs_dentry_unused);
509 next = this_parent->d_subdirs.next;
511 while (next != &this_parent->d_subdirs) {
512 struct list_head *tmp = next;
513 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
515 if (!atomic_read(&dentry->d_count)) {
516 list_del(&dentry->d_lru);
517 list_add(&dentry->d_lru, afs_dentry_unused.prev);
521 * Descend a level if the d_subdirs list is non-empty.
523 if (!list_empty(&dentry->d_subdirs)) {
524 this_parent = dentry;
529 * All done at this level ... ascend and resume the search.
531 if (this_parent != parent) {
532 next = this_parent->d_child.next;
533 this_parent = this_parent->d_parent;
538 struct dentry *dentry;
539 struct list_head *tmp;
541 tmp = afs_dentry_unused.prev;
543 if (tmp == &afs_dentry_unused)
546 dentry = list_entry(tmp, struct dentry, d_lru);
548 /* Unused dentry with a count? */
549 if (atomic_read(&dentry->d_count))
553 list_del_init(&dentry->d_hash); /* d_drop */
562 /* afs_TryFlushDcacheChildren -- Shakes loose vcache references held by
563 * children of the dentry
565 * LOCKS -- Called with afs_xvcache write locked. Drops and reaquires
566 * AFS_GLOCK, so it can call dput, which may call iput, but
567 * keeps afs_xvcache exclusively.
569 * Tree traversal algorithm from fs/dcache.c: select_parent()
571 static void afs_TryFlushDcacheChildren(struct vcache *tvc)
573 struct inode *ip = AFSTOI(tvc);
574 struct dentry *this_parent;
575 struct list_head *next;
576 struct list_head *cur;
577 struct list_head *head = &ip->i_dentry;
578 struct dentry *dentry;
582 #ifndef old_vcache_scheme
585 while ((cur = cur->next) != head) {
586 dentry = list_entry(cur, struct dentry, d_alias);
588 afs_Trace3(afs_iclSetp, CM_TRACE_TRYFLUSHDCACHECHILDREN,
589 ICL_TYPE_POINTER, ip,
590 ICL_TYPE_STRING, dentry->d_parent->d_name.name,
591 ICL_TYPE_STRING, dentry->d_name.name);
593 if (!list_empty(&dentry->d_hash) && !list_empty(&dentry->d_subdirs))
594 __shrink_dcache_parent(dentry);
596 if (!atomic_read(&dentry->d_count)) {
598 list_del_init(&dentry->d_hash); /* d_drop */
610 while ((cur = cur->next) != head) {
611 dentry = list_entry(cur, struct dentry, d_alias);
613 afs_Trace3(afs_iclSetp, CM_TRACE_TRYFLUSHDCACHECHILDREN,
614 ICL_TYPE_POINTER, ip,
615 ICL_TYPE_STRING, dentry->d_parent->d_name.name,
616 ICL_TYPE_STRING, dentry->d_name.name);
618 if (!DCOUNT(dentry)) {
631 #endif /* AFS_LINUX22_ENV */
637 * This routine is responsible for allocating a new cache entry
638 * from the free list. It formats the cache entry and inserts it
639 * into the appropriate hash tables. It must be called with
640 * afs_xvcache write-locked so as to prevent several processes from
641 * trying to create a new cache entry simultaneously.
644 * afid : The file id of the file whose cache entry is being
647 /* LOCK: afs_NewVCache afs_xvcache W */
648 struct vcache *afs_NewVCache(struct VenusFid *afid, struct server *serverp)
652 afs_int32 anumber = VCACHE_FREE;
654 struct gnode *gnodepnt;
657 struct vm_info * vm_info_ptr;
658 #endif /* AFS_MACH_ENV */
661 #endif /* AFS_OSF_ENV */
662 struct afs_q *tq, *uq;
665 AFS_STATCNT(afs_NewVCache);
668 if (afs_vcount >= afs_maxvcount) {
671 * If we are using > 33 % of the total system vnodes for AFS vcache
672 * entries or we are using the maximum number of vcache entries,
673 * then free some. (if our usage is > 33% we should free some, if
674 * our usage is > afs_maxvcount, set elsewhere to 0.5*nvnode,
675 * we _must_ free some -- no choice).
677 if ( (( 3 * afs_vcount ) > nvnode) || ( afs_vcount >= afs_maxvcount )) {
679 struct afs_q *tq, *uq;
680 int i; char *panicstr;
683 for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
686 if (tvc->states & CVFlushed)
687 refpanic ("CVFlushed on VLRU");
688 else if (i++ > afs_maxvcount)
689 refpanic ("Exceeded pool of AFS vnodes(VLRU cycle?)");
690 else if (QNext(uq) != tq)
691 refpanic ("VLRU inconsistent");
692 else if (VREFCOUNT(tvc) < 1)
693 refpanic ("refcnt 0 on VLRU");
695 if ( VREFCOUNT(tvc) == 1 && tvc->opens == 0
696 && (tvc->states & CUnlinkedDel) == 0) {
697 code = afs_FlushVCache(tvc, &fv_slept);
704 continue; /* start over - may have raced. */
709 if (anumber == VCACHE_FREE) {
710 printf("NewVCache: warning none freed, using %d of %d\n",
711 afs_vcount, afs_maxvcount);
712 if (afs_vcount >= afs_maxvcount) {
713 osi_Panic("NewVCache - none freed");
714 /* XXX instead of panicing, should do afs_maxvcount++
715 and magic up another one */
721 if (getnewvnode(MOUNT_AFS, &Afs_vnodeops, &nvc)) {
722 /* What should we do ???? */
723 osi_Panic("afs_NewVCache: no more vnodes");
728 tvc->nextfree = NULL;
730 #else /* AFS_OSF_ENV */
731 /* pull out a free cache entry */
734 for (tq = VLRU.prev; (anumber > 0) && (tq != &VLRU); tq = uq) {
738 if (tvc->states & CVFlushed) {
739 refpanic("CVFlushed on VLRU");
740 } else if (i++ > 2*afs_cacheStats) { /* even allowing for a few xallocs...*/
741 refpanic("Increase -stat parameter of afsd(VLRU cycle?)");
742 } else if (QNext(uq) != tq) {
743 refpanic("VLRU inconsistent");
746 #ifdef AFS_DARWIN_ENV
747 if (tvc->opens == 0 && ((tvc->states & CUnlinkedDel) == 0) &&
748 VREFCOUNT(tvc) == 1 && UBCINFOEXISTS(&tvc->v)) {
749 osi_VM_TryReclaim(tvc, &fv_slept);
753 continue; /* start over - may have raced. */
757 #if defined(AFS_FBSD_ENV)
758 if (VREFCOUNT(tvc) == 1 && tvc->opens == 0
759 && (tvc->states & CUnlinkedDel) == 0) {
760 if (!(VOP_LOCK(&tvc->v, LK_EXCLUSIVE, curproc))) {
761 if (VREFCOUNT(tvc) == 1 && tvc->opens == 0
762 && (tvc->states & CUnlinkedDel) == 0) {
764 AFS_GUNLOCK(); /* perhaps inline inactive for locking */
765 VOP_INACTIVE(&tvc->v, curproc);
768 VOP_UNLOCK(&tvc->v, 0, curproc);
773 #if defined(AFS_LINUX22_ENV)
774 if (tvc != afs_globalVp && VREFCOUNT(tvc) && tvc->opens == 0)
775 afs_TryFlushDcacheChildren(tvc);
778 if (VREFCOUNT(tvc) == 0 && tvc->opens == 0
779 && (tvc->states & CUnlinkedDel) == 0) {
780 code = afs_FlushVCache(tvc, &fv_slept);
787 continue; /* start over - may have raced. */
790 if (tq == uq ) break;
794 /* none free, making one is better than a panic */
795 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
796 tvc = (struct vcache *) afs_osi_Alloc(sizeof (struct vcache));
797 #ifdef KERNEL_HAVE_PIN
798 pin((char *)tvc, sizeof(struct vcache)); /* XXX */
801 /* In case it still comes here we need to fill this */
802 tvc->v.v_vm_info = VM_INFO_NULL;
803 vm_info_init(tvc->v.v_vm_info);
804 /* perhaps we should also do close_flush on non-NeXT mach systems;
805 * who knows; we don't currently have the sources.
807 #endif /* AFS_MACH_ENV */
808 #if defined(AFS_SGI_ENV)
809 { char name[METER_NAMSZ];
810 memset(tvc, 0, sizeof(struct vcache));
811 tvc->v.v_number = ++afsvnumbers;
812 tvc->vc_rwlockid = OSI_NO_LOCKID;
813 initnsema(&tvc->vc_rwlock, 1, makesname(name, "vrw", tvc->v.v_number));
814 #ifndef AFS_SGI53_ENV
815 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
817 #ifndef AFS_SGI62_ENV
818 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
821 #endif /* AFS_SGI_ENV */
824 tvc = freeVCList; /* take from free list */
825 freeVCList = tvc->nextfree;
826 tvc->nextfree = NULL;
828 #endif /* AFS_OSF_ENV */
831 vm_info_ptr = tvc->v.v_vm_info;
832 #endif /* AFS_MACH_ENV */
834 #if defined(AFS_OBSD_ENV)
836 panic("afs_NewVCache(): free vcache with vnode attached");
839 #if !defined(AFS_SGI_ENV) && !defined(AFS_OSF_ENV)
840 memset((char *)tvc, 0, sizeof(struct vcache));
845 RWLOCK_INIT(&tvc->lock, "vcache lock");
846 #if defined(AFS_SUN5_ENV)
847 RWLOCK_INIT(&tvc->vlock, "vcache vlock");
848 #endif /* defined(AFS_SUN5_ENV) */
851 tvc->v.v_vm_info = vm_info_ptr;
852 tvc->v.v_vm_info->pager = MEMORY_OBJECT_NULL;
853 #endif /* AFS_MACH_ENV */
855 afs_nbsd_getnewvnode(tvc); /* includes one refcount */
856 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
858 tvc->parentVnode = 0;
860 tvc->linkData = NULL;
863 tvc->execsOrWriters = 0;
867 tvc->last_looker = 0;
869 tvc->asynchrony = -1;
871 afs_symhint_inval(tvc);
873 tvc->flushDV.low = tvc->flushDV.high = AFS_MAXDV;
876 tvc->truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
877 hzero(tvc->m.DataVersion); /* in case we copy it into flushDV */
879 /* Hold it for the LRU (should make count 2) */
880 VN_HOLD(AFSTOV(tvc));
881 #else /* AFS_OSF_ENV */
883 VREFCOUNT_SET(tvc, 1); /* us */
884 #endif /* AFS_OBSD_ENV */
885 #endif /* AFS_OSF_ENV */
887 LOCK_INIT(&tvc->pvmlock, "vcache pvmlock");
888 tvc->vmh = tvc->segid = NULL;
891 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
892 #if defined(AFS_SUN5_ENV)
893 rw_init(&tvc->rwlock, "vcache rwlock", RW_DEFAULT, NULL);
895 #if defined(AFS_SUN55_ENV)
896 /* This is required if the kaio (kernel aynchronous io)
897 ** module is installed. Inside the kernel, the function
898 ** check_vp( common/os/aio.c) checks to see if the kernel has
899 ** to provide asynchronous io for this vnode. This
900 ** function extracts the device number by following the
901 ** v_data field of the vnode. If we do not set this field
902 ** then the system panics. The value of the v_data field
903 ** is not really important for AFS vnodes because the kernel
904 ** does not do asynchronous io for regular files. Hence,
905 ** for the time being, we fill up the v_data field with the
906 ** vnode pointer itself. */
907 tvc->v.v_data = (char *)tvc;
908 #endif /* AFS_SUN55_ENV */
910 afs_BozonInit(&tvc->pvnLock, tvc);
914 tvc->callback = serverp; /* to minimize chance that clear
916 /* initialize vnode data, note vrefCount is v.v_count */
918 /* Don't forget to free the gnode space */
919 tvc->v.v_gnode = gnodepnt = (struct gnode *) osi_AllocSmallSpace(sizeof(struct gnode));
920 memset((char *)gnodepnt, 0, sizeof(struct gnode));
923 memset((void*)&(tvc->vc_bhv_desc), 0, sizeof(tvc->vc_bhv_desc));
924 bhv_desc_init(&(tvc->vc_bhv_desc), tvc, tvc, &Afs_vnodeops);
926 vn_bhv_head_init(&(tvc->v.v_bh), "afsvp");
927 vn_bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
929 bhv_head_init(&(tvc->v.v_bh));
930 bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
933 tvc->v.v_mreg = tvc->v.v_mregb = (struct pregion*)tvc;
935 tvc->v.v_trace = ktrace_alloc(VNODE_TRACE_SIZE, 0);
937 init_bitlock(&tvc->v.v_pcacheflag, VNODE_PCACHE_LOCKBIT, "afs_pcache",
939 init_mutex(&tvc->v.v_filocksem, MUTEX_DEFAULT, "afsvfl", (long)tvc);
940 init_mutex(&tvc->v.v_buf_lock, MUTEX_DEFAULT, "afsvnbuf", (long)tvc);
942 vnode_pcache_init(&tvc->v);
943 #if defined(DEBUG) && defined(VNODE_INIT_BITLOCK)
944 /* Above define is never true execpt in SGI test kernels. */
945 init_bitlock(&(tvc->v.v_flag, VLOCK, "vnode", tvc->v.v_number);
948 AFS_VN_INIT_BUF_LOCK(&(tvc->v));
951 SetAfsVnode(AFSTOV(tvc));
952 #endif /* AFS_SGI64_ENV */
953 #ifdef AFS_DARWIN_ENV
954 tvc->v.v_ubcinfo = UBC_INFO_NULL;
955 lockinit(&tvc->rwlock, PINOD, "vcache rwlock", 0, 0);
956 cache_purge(AFSTOV(tvc));
959 /* VLISTNONE(&tvc->v); */
960 tvc->v.v_freelist.tqe_next=0;
961 tvc->v.v_freelist.tqe_prev=(struct vnode **)0xdeadb;
962 /*tvc->vrefCount++;*/
965 lockinit(&tvc->rwlock, PINOD, "vcache rwlock", 0, 0);
966 cache_purge(AFSTOV(tvc));
969 tvc->v.v_usecount++; /* steal an extra ref for now so vfree never happens */
970 /* This extra ref is dealt with above... */
973 * The proper value for mvstat (for root fids) is setup by the caller.
976 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
978 if (afs_globalVFS == 0) osi_Panic("afs globalvfs");
979 vSetVfsp(tvc, afs_globalVFS);
982 tvc->v.v_vfsnext = afs_globalVFS->vfs_vnodes; /* link off vfs */
983 tvc->v.v_vfsprev = NULL;
984 afs_globalVFS->vfs_vnodes = &tvc->v;
985 if (tvc->v.v_vfsnext != NULL)
986 tvc->v.v_vfsnext->v_vfsprev = &tvc->v;
987 tvc->v.v_next = gnodepnt->gn_vnode; /*Single vnode per gnode for us!*/
988 gnodepnt->gn_vnode = &tvc->v;
991 tvc->v.g_dev = ((struct mount *)afs_globalVFS->vfs_data)->m_dev;
993 #if defined(AFS_DUX40_ENV)
994 insmntque(tvc, afs_globalVFS, &afs_ubcops);
997 /* Is this needed??? */
998 insmntque(tvc, afs_globalVFS);
999 #endif /* AFS_OSF_ENV */
1000 #endif /* AFS_DUX40_ENV */
1001 #if defined(AFS_SGI_ENV)
1002 VN_SET_DPAGES(&(tvc->v), (struct pfdat*)NULL);
1003 osi_Assert((tvc->v.v_flag & VINACT) == 0);
1005 osi_Assert(VN_GET_PGCNT(&(tvc->v)) == 0);
1006 osi_Assert(tvc->mapcnt == 0 && tvc->vc_locktrips == 0);
1007 osi_Assert(tvc->vc_rwlockid == OSI_NO_LOCKID);
1008 osi_Assert(tvc->v.v_filocks == NULL);
1009 #if !defined(AFS_SGI65_ENV)
1010 osi_Assert(tvc->v.v_filocksem == NULL);
1012 osi_Assert(tvc->cred == NULL);
1013 #ifdef AFS_SGI64_ENV
1014 vnode_pcache_reinit(&tvc->v);
1015 tvc->v.v_rdev = NODEV;
1017 vn_initlist((struct vnlist *)&tvc->v);
1019 #endif /* AFS_SGI_ENV */
1020 #if defined(AFS_LINUX22_ENV)
1022 struct inode *ip = AFSTOI(tvc);
1023 sema_init(&ip->i_sem, 1);
1024 #if defined(AFS_LINUX24_ENV)
1025 sema_init(&ip->i_zombie, 1);
1026 init_waitqueue_head(&ip->i_wait);
1027 spin_lock_init(&ip->i_data.i_shared_lock);
1028 #ifdef STRUCT_ADDRESS_SPACE_HAS_PAGE_LOCK
1029 spin_lock_init(&ip->i_data.page_lock);
1031 INIT_LIST_HEAD(&ip->i_data.clean_pages);
1032 INIT_LIST_HEAD(&ip->i_data.dirty_pages);
1033 INIT_LIST_HEAD(&ip->i_data.locked_pages);
1034 INIT_LIST_HEAD(&ip->i_dirty_buffers);
1035 #ifdef STRUCT_INODE_HAS_I_DIRTY_DATA_BUFFERS
1036 INIT_LIST_HEAD(&ip->i_dirty_data_buffers);
1038 #ifdef STRUCT_INODE_HAS_I_DEVICES
1039 INIT_LIST_HEAD(&ip->i_devices);
1041 ip->i_data.host = (void*) ip;
1042 #ifdef STRUCT_ADDRESS_SPACE_HAS_GFP_MASK
1043 ip->i_data.gfp_mask = GFP_HIGHUSER;
1045 ip->i_mapping = &ip->i_data;
1046 #ifdef STRUCT_INODE_HAS_I_TRUNCATE_SEM
1047 init_rwsem(&ip->i_truncate_sem);
1050 sema_init(&ip->i_atomic_write, 1);
1051 init_waitqueue(&ip->i_wait);
1053 INIT_LIST_HEAD(&ip->i_hash);
1054 INIT_LIST_HEAD(&ip->i_dentry);
1055 if (afs_globalVFS) {
1056 ip->i_dev = afs_globalVFS->s_dev;
1057 ip->i_sb = afs_globalVFS;
1062 osi_dnlc_purgedp(tvc); /* this may be overkill */
1063 memset((char *)&(tvc->quick), 0, sizeof(struct vtodc));
1064 memset((char *)&(tvc->callsort), 0, sizeof(struct afs_q));
1068 tvc->hnext = afs_vhashT[i];
1069 afs_vhashT[i] = tvc;
1070 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1071 refpanic ("NewVCache VLRU inconsistent");
1073 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
1074 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1075 refpanic ("NewVCache VLRU inconsistent2");
1077 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
1078 refpanic ("NewVCache VLRU inconsistent3");
1080 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
1081 refpanic ("NewVCache VLRU inconsistent4");
1091 * afs_FlushActiveVcaches
1097 * doflocks : Do we handle flocks?
1099 /* LOCK: afs_FlushActiveVcaches afs_xvcache N */
1100 void afs_FlushActiveVcaches(register afs_int32 doflocks)
1102 register struct vcache *tvc;
1104 register struct conn *tc;
1105 register afs_int32 code;
1106 register struct AFS_UCRED *cred = NULL;
1107 struct vrequest treq, ureq;
1108 struct AFSVolSync tsync;
1112 AFS_STATCNT(afs_FlushActiveVcaches);
1113 ObtainReadLock(&afs_xvcache);
1114 for(i=0;i<VCSIZE;i++) {
1115 for(tvc = afs_vhashT[i]; tvc; tvc=tvc->hnext) {
1116 if (doflocks && tvc->flockCount != 0) {
1117 /* if this entry has an flock, send a keep-alive call out */
1119 ReleaseReadLock(&afs_xvcache);
1120 ObtainWriteLock(&tvc->lock,51);
1122 afs_InitReq(&treq, &afs_osi_cred);
1123 treq.flags |= O_NONBLOCK;
1125 tc = afs_Conn(&tvc->fid, &treq, SHARED_LOCK);
1127 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
1130 RXAFS_ExtendLock(tc->id,
1131 (struct AFSFid *) &tvc->fid.Fid,
1138 (afs_Analyze(tc, code, &tvc->fid, &treq,
1139 AFS_STATS_FS_RPCIDX_EXTENDLOCK,
1140 SHARED_LOCK, NULL));
1142 ReleaseWriteLock(&tvc->lock);
1143 ObtainReadLock(&afs_xvcache);
1147 if ((tvc->states & CCore) || (tvc->states & CUnlinkedDel)) {
1149 * Don't let it evaporate in case someone else is in
1150 * this code. Also, drop the afs_xvcache lock while
1151 * getting vcache locks.
1154 ReleaseReadLock(&afs_xvcache);
1155 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
1156 afs_BozonLock(&tvc->pvnLock, tvc);
1158 #if defined(AFS_SGI_ENV)
1160 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1162 osi_Assert(VREFCOUNT(tvc) > 0);
1163 AFS_RWLOCK((vnode_t *)tvc, VRWLOCK_WRITE);
1165 ObtainWriteLock(&tvc->lock,52);
1166 if (tvc->states & CCore) {
1167 tvc->states &= ~CCore;
1168 /* XXXX Find better place-holder for cred XXXX */
1169 cred = (struct AFS_UCRED *) tvc->linkData;
1170 tvc->linkData = NULL; /* XXX */
1171 afs_InitReq(&ureq, cred);
1172 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1173 ICL_TYPE_POINTER, tvc,
1174 ICL_TYPE_INT32, tvc->execsOrWriters);
1175 code = afs_StoreOnLastReference(tvc, &ureq);
1176 ReleaseWriteLock(&tvc->lock);
1177 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
1178 afs_BozonUnlock(&tvc->pvnLock, tvc);
1180 hzero(tvc->flushDV);
1183 if (code && code != VNOVNODE) {
1184 afs_StoreWarn(code, tvc->fid.Fid.Volume,
1185 /* /dev/console */ 1);
1187 } else if (tvc->states & CUnlinkedDel) {
1191 ReleaseWriteLock(&tvc->lock);
1192 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
1193 afs_BozonUnlock(&tvc->pvnLock, tvc);
1195 #if defined(AFS_SGI_ENV)
1196 AFS_RWUNLOCK((vnode_t *)tvc, VRWLOCK_WRITE);
1198 afs_remunlink(tvc, 0);
1199 #if defined(AFS_SGI_ENV)
1200 AFS_RWLOCK((vnode_t *)tvc, VRWLOCK_WRITE);
1204 /* lost (or won, perhaps) the race condition */
1205 ReleaseWriteLock(&tvc->lock);
1206 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
1207 afs_BozonUnlock(&tvc->pvnLock, tvc);
1210 #if defined(AFS_SGI_ENV)
1211 AFS_RWUNLOCK((vnode_t *)tvc, VRWLOCK_WRITE);
1213 ObtainReadLock(&afs_xvcache);
1219 AFS_RELE(AFSTOV(tvc));
1221 /* Matches write code setting CCore flag */
1225 #ifdef AFS_DARWIN_ENV
1226 if (VREFCOUNT(tvc) == 1 && UBCINFOEXISTS(&tvc->v)) {
1227 if (tvc->opens) panic("flushactive open, hasubc, but refcnt 1");
1228 osi_VM_TryReclaim(tvc,0);
1233 ReleaseReadLock(&afs_xvcache);
1241 * Make sure a cache entry is up-to-date status-wise.
1243 * NOTE: everywhere that calls this can potentially be sped up
1244 * by checking CStatd first, and avoiding doing the InitReq
1245 * if this is up-to-date.
1247 * Anymore, the only places that call this KNOW already that the
1248 * vcache is not up-to-date, so we don't screw around.
1251 * avc : Ptr to vcache entry to verify.
1255 int afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1257 register struct vcache *tvc;
1259 AFS_STATCNT(afs_VerifyVCache);
1261 #if defined(AFS_OSF_ENV)
1262 ObtainReadLock(&avc->lock);
1263 if (afs_IsWired(avc)) {
1264 ReleaseReadLock(&avc->lock);
1267 ReleaseReadLock(&avc->lock);
1268 #endif /* AFS_OSF_ENV */
1269 /* otherwise we must fetch the status info */
1271 ObtainWriteLock(&avc->lock,53);
1272 if (avc->states & CStatd) {
1273 ReleaseWriteLock(&avc->lock);
1276 ObtainWriteLock(&afs_xcbhash, 461);
1277 avc->states &= ~( CStatd | CUnique );
1278 avc->callback = NULL;
1279 afs_DequeueCallback(avc);
1280 ReleaseWriteLock(&afs_xcbhash);
1281 ReleaseWriteLock(&avc->lock);
1283 /* since we've been called back, or the callback has expired,
1284 * it's possible that the contents of this directory, or this
1285 * file's name have changed, thus invalidating the dnlc contents.
1287 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1288 osi_dnlc_purgedp (avc);
1290 osi_dnlc_purgevp (avc);
1292 /* fetch the status info */
1293 tvc = afs_GetVCache(&avc->fid, areq, NULL, avc);
1294 if (!tvc) return ENOENT;
1295 /* Put it back; caller has already incremented vrefCount */
1299 } /*afs_VerifyVCache*/
1306 * Simple copy of stat info into cache.
1309 * avc : Ptr to vcache entry involved.
1310 * astat : Ptr to stat info to copy.
1313 * Nothing interesting.
1315 * Callers: as of 1992-04-29, only called by WriteVCache
1317 static void afs_SimpleVStat(register struct vcache *avc,
1318 register struct AFSFetchStatus *astat, struct vrequest *areq)
1321 AFS_STATCNT(afs_SimpleVStat);
1324 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1325 && !AFS_VN_MAPPED((vnode_t*)avc))
1327 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc))
1331 #ifdef AFS_64BIT_ClIENT
1332 FillInt64(length, astat->Length_hi, astat->Length);
1333 #else /* AFS_64BIT_CLIENT */
1334 length = astat->Length;
1335 #endif /* AFS_64BIT_CLIENT */
1336 #if defined(AFS_SGI_ENV)
1337 osi_Assert((valusema(&avc->vc_rwlock) <= 0) &&
1338 (OSI_GET_LOCKID() == avc->vc_rwlockid));
1339 if (length < avc->m.Length) {
1340 vnode_t *vp = (vnode_t *)avc;
1342 osi_Assert(WriteLocked(&avc->lock));
1343 ReleaseWriteLock(&avc->lock);
1345 PTOSSVP(vp, (off_t)length, (off_t)MAXLONG);
1347 ObtainWriteLock(&avc->lock,67);
1350 /* if writing the file, don't fetch over this value */
1351 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT,
1352 ICL_TYPE_POINTER, avc,
1353 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1354 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1355 avc->m.Length = length;
1356 avc->m.Date = astat->ClientModTime;
1358 avc->m.Owner = astat->Owner;
1359 avc->m.Group = astat->Group;
1360 avc->m.Mode = astat->UnixModeBits;
1361 if (vType(avc) == VREG) {
1362 avc->m.Mode |= S_IFREG;
1364 else if (vType(avc) == VDIR) {
1365 avc->m.Mode |= S_IFDIR;
1367 else if (vType(avc) == VLNK) {
1371 avc->m.Mode |= S_IFLNK;
1372 if ((avc->m.Mode & 0111) == 0) avc->mvstat = 1;
1374 if (avc->states & CForeign) {
1375 struct axscache *ac;
1376 avc->anyAccess = astat->AnonymousAccess;
1378 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1380 * Caller has at least one bit not covered by anonymous, and
1381 * thus may have interesting rights.
1383 * HOWEVER, this is a really bad idea, because any access query
1384 * for bits which aren't covered by anonymous, on behalf of a user
1385 * who doesn't have any special rights, will result in an answer of
1386 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1387 * It's an especially bad idea under Ultrix, since (due to the lack of
1388 * a proper access() call) it must perform several afs_access() calls
1389 * in order to create magic mode bits that vary according to who makes
1390 * the call. In other words, _every_ stat() generates a test for
1393 #endif /* badidea */
1394 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1395 ac->axess = astat->CallerAccess;
1396 else /* not found, add a new one if possible */
1397 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1401 } /*afs_SimpleVStat*/
1408 * Store the status info *only* back to the server for a
1412 * avc : Ptr to the vcache entry.
1413 * astatus : Ptr to the status info to store.
1414 * areq : Ptr to the associated vrequest.
1417 * Must be called with a shared lock held on the vnode.
1420 int afs_WriteVCache(register struct vcache *avc,
1421 register struct AFSStoreStatus *astatus, struct vrequest *areq)
1425 struct AFSFetchStatus OutStatus;
1426 struct AFSVolSync tsync;
1429 AFS_STATCNT(afs_WriteVCache);
1430 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1431 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
1434 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
1436 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1438 code = RXAFS_StoreStatus(tc->id,
1439 (struct AFSFid *) &avc->fid.Fid,
1440 astatus, &OutStatus, &tsync);
1446 (afs_Analyze(tc, code, &avc->fid, areq,
1447 AFS_STATS_FS_RPCIDX_STORESTATUS,
1448 SHARED_LOCK, NULL));
1450 UpgradeSToWLock(&avc->lock,20);
1452 /* success, do the changes locally */
1453 afs_SimpleVStat(avc, &OutStatus, areq);
1455 * Update the date, too. SimpleVStat didn't do this, since
1456 * it thought we were doing this after fetching new status
1457 * over a file being written.
1459 avc->m.Date = OutStatus.ClientModTime;
1462 /* failure, set up to check with server next time */
1463 ObtainWriteLock(&afs_xcbhash, 462);
1464 afs_DequeueCallback(avc);
1465 avc->states &= ~( CStatd | CUnique); /* turn off stat valid flag */
1466 ReleaseWriteLock(&afs_xcbhash);
1467 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1468 osi_dnlc_purgedp (avc); /* if it (could be) a directory */
1470 ConvertWToSLock(&avc->lock);
1473 } /*afs_WriteVCache*/
1479 * Copy astat block into vcache info
1482 * avc : Ptr to vcache entry.
1483 * astat : Ptr to stat block to copy in.
1484 * areq : Ptr to associated request.
1487 * Must be called under a write lock
1489 * Note: this code may get dataversion and length out of sync if the file has
1490 * been modified. This is less than ideal. I haven't thought about
1491 * it sufficiently to be certain that it is adequate.
1493 void afs_ProcessFS(register struct vcache *avc, register struct AFSFetchStatus *astat,
1494 struct vrequest *areq)
1497 AFS_STATCNT(afs_ProcessFS);
1499 #ifdef AFS_64BIT_CLIENT
1500 FillInt64(length, astat->Length_hi, astat->Length);
1501 #else /* AFS_64BIT_CLIENT */
1502 length = astat->Length;
1503 #endif /* AFS_64BIT_CLIENT */
1504 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1505 * number for each bulk status request. Under no circumstances
1506 * should afs_DoBulkStat store a sequence number if the new
1507 * length will be ignored when afs_ProcessFS is called with
1508 * new stats. If you change the following conditional then you
1509 * also need to change the conditional in afs_DoBulkStat. */
1511 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1512 && !AFS_VN_MAPPED((vnode_t*)avc))
1514 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc))
1517 /* if we're writing or mapping this file, don't fetch over these
1520 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1521 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1522 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1523 avc->m.Length = length;
1524 avc->m.Date = astat->ClientModTime;
1526 hset64(avc->m.DataVersion, astat->dataVersionHigh, astat->DataVersion);
1527 avc->m.Owner = astat->Owner;
1528 avc->m.Mode = astat->UnixModeBits;
1529 avc->m.Group = astat->Group;
1530 avc->m.LinkCount = astat->LinkCount;
1531 if (astat->FileType == File) {
1532 vSetType(avc, VREG);
1533 avc->m.Mode |= S_IFREG;
1535 else if (astat->FileType == Directory) {
1536 vSetType(avc, VDIR);
1537 avc->m.Mode |= S_IFDIR;
1539 else if (astat->FileType == SymbolicLink) {
1540 if (afs_fakestat_enable && (avc->m.Mode & 0111) == 0) {
1541 vSetType(avc, VDIR);
1542 avc->m.Mode |= S_IFDIR;
1544 vSetType(avc, VLNK);
1545 avc->m.Mode |= S_IFLNK;
1547 if ((avc->m.Mode & 0111) == 0) {
1551 avc->anyAccess = astat->AnonymousAccess;
1553 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1555 * Caller has at least one bit not covered by anonymous, and
1556 * thus may have interesting rights.
1558 * HOWEVER, this is a really bad idea, because any access query
1559 * for bits which aren't covered by anonymous, on behalf of a user
1560 * who doesn't have any special rights, will result in an answer of
1561 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1562 * It's an especially bad idea under Ultrix, since (due to the lack of
1563 * a proper access() call) it must perform several afs_access() calls
1564 * in order to create magic mode bits that vary according to who makes
1565 * the call. In other words, _every_ stat() generates a test for
1568 #endif /* badidea */
1570 struct axscache *ac;
1571 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1572 ac->axess = astat->CallerAccess;
1573 else /* not found, add a new one if possible */
1574 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1577 #ifdef AFS_LINUX22_ENV
1578 vcache2inode(avc); /* Set the inode attr cache */
1580 #ifdef AFS_DARWIN_ENV
1581 osi_VM_Setup(avc,1);
1587 int afs_RemoteLookup(register struct VenusFid *afid, struct vrequest *areq,
1588 char *name, struct VenusFid *nfid, struct AFSFetchStatus *OutStatusp,
1589 struct AFSCallBack *CallBackp, struct server **serverp, struct AFSVolSync *tsyncp)
1593 register struct conn *tc;
1594 struct AFSFetchStatus OutDirStatus;
1597 if (!name) name = ""; /* XXX */
1599 tc = afs_Conn(afid, areq, SHARED_LOCK);
1601 if (serverp) *serverp = tc->srvr->server;
1603 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1605 code = RXAFS_Lookup(tc->id, (struct AFSFid *) &afid->Fid, name,
1606 (struct AFSFid *) &nfid->Fid,
1607 OutStatusp, &OutDirStatus, CallBackp, tsyncp);
1613 (afs_Analyze(tc, code, afid, areq,
1614 AFS_STATS_FS_RPCIDX_XLOOKUP,
1615 SHARED_LOCK, NULL));
1625 * Given a file id and a vrequest structure, fetch the status
1626 * information associated with the file.
1630 * areq : Ptr to associated vrequest structure, specifying the
1631 * user whose authentication tokens will be used.
1632 * avc : caller may already have a vcache for this file, which is
1636 * The cache entry is returned with an increased vrefCount field.
1637 * The entry must be discarded by calling afs_PutVCache when you
1638 * are through using the pointer to the cache entry.
1640 * You should not hold any locks when calling this function, except
1641 * locks on other vcache entries. If you lock more than one vcache
1642 * entry simultaneously, you should lock them in this order:
1644 * 1. Lock all files first, then directories.
1645 * 2. Within a particular type, lock entries in Fid.Vnode order.
1647 * This locking hierarchy is convenient because it allows locking
1648 * of a parent dir cache entry, given a file (to check its access
1649 * control list). It also allows renames to be handled easily by
1650 * locking directories in a constant order.
1651 * NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1653 /* might have a vcache structure already, which must
1654 * already be held by the caller */
1656 struct vcache *afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
1657 afs_int32 *cached, struct vcache *avc)
1660 afs_int32 code, newvcache=0;
1661 register struct vcache *tvc;
1665 AFS_STATCNT(afs_GetVCache);
1667 if (cached) *cached = 0; /* Init just in case */
1669 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1673 ObtainSharedLock(&afs_xvcache,5);
1675 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU );
1677 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1678 ReleaseSharedLock(&afs_xvcache);
1679 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1687 if (tvc->states & CStatd) {
1688 ReleaseSharedLock(&afs_xvcache);
1693 UpgradeSToWLock(&afs_xvcache,21);
1695 /* no cache entry, better grab one */
1696 tvc = afs_NewVCache(afid, NULL);
1699 ConvertWToSLock(&afs_xvcache);
1700 afs_stats_cmperf.vcacheMisses++;
1703 ReleaseSharedLock(&afs_xvcache);
1705 ObtainWriteLock(&tvc->lock,54);
1707 if (tvc->states & CStatd) {
1708 #ifdef AFS_LINUX22_ENV
1711 ReleaseWriteLock(&tvc->lock);
1712 #ifdef AFS_DARWIN_ENV
1713 osi_VM_Setup(tvc,0);
1718 #if defined(AFS_OSF_ENV)
1719 if (afs_IsWired(tvc)) {
1720 ReleaseWriteLock(&tvc->lock);
1723 #endif /* AFS_OSF_ENV */
1725 ObtainWriteLock(&afs_xcbhash, 464);
1726 tvc->states &= ~CUnique;
1728 afs_DequeueCallback(tvc);
1729 ReleaseWriteLock(&afs_xcbhash);
1731 /* It is always appropriate to throw away all the access rights? */
1732 afs_FreeAllAxs(&(tvc->Access));
1733 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1735 if ((tvp->states & VForeign)) {
1736 if (newvcache) tvc->states |= CForeign;
1737 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1738 && (tvp->rootUnique == afid->Fid.Unique)) {
1742 if (tvp->states & VRO) tvc->states |= CRO;
1743 if (tvp->states & VBackup) tvc->states |= CBackup;
1744 /* now copy ".." entry back out of volume structure, if necessary */
1745 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1747 tvc->mvid = (struct VenusFid *)
1748 osi_AllocSmallSpace(sizeof(struct VenusFid));
1749 *tvc->mvid = tvp->dotdot;
1751 afs_PutVolume(tvp, READ_LOCK);
1755 afs_RemoveVCB(afid);
1757 struct AFSFetchStatus OutStatus;
1759 if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1760 afs_ProcessFS(tvc, &OutStatus, areq);
1761 tvc->states |= CStatd | CUnique;
1764 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1769 ReleaseWriteLock(&tvc->lock);
1771 ObtainReadLock(&afs_xvcache);
1773 ReleaseReadLock(&afs_xvcache);
1777 ReleaseWriteLock(&tvc->lock);
1784 struct vcache *afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1785 afs_int32 *cached, struct vcache *adp, char *aname)
1787 afs_int32 code, now, newvcache=0;
1788 struct VenusFid nfid;
1789 register struct vcache *tvc;
1791 struct AFSFetchStatus OutStatus;
1792 struct AFSCallBack CallBack;
1793 struct AFSVolSync tsync;
1794 struct server *serverp = 0;
1798 AFS_STATCNT(afs_GetVCache);
1799 if (cached) *cached = 0; /* Init just in case */
1801 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1805 ObtainReadLock(&afs_xvcache);
1806 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */);
1809 ReleaseReadLock(&afs_xvcache);
1811 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1812 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1816 ObtainReadLock(&tvc->lock);
1818 if (tvc->states & CStatd) {
1822 ReleaseReadLock(&tvc->lock);
1825 tvc->states &= ~CUnique;
1827 ReleaseReadLock(&tvc->lock);
1828 ObtainReadLock(&afs_xvcache);
1832 ReleaseReadLock(&afs_xvcache);
1834 /* lookup the file */
1837 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
1838 code = afs_RemoteLookup(&adp->fid, areq, aname, &nfid, &OutStatus, &CallBack,
1841 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1845 ObtainSharedLock(&afs_xvcache,6);
1846 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU /* no xstats now*/);
1848 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1849 ReleaseSharedLock(&afs_xvcache);
1850 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1856 /* no cache entry, better grab one */
1857 UpgradeSToWLock(&afs_xvcache,22);
1858 tvc = afs_NewVCache(&nfid, NULL);
1860 ConvertWToSLock(&afs_xvcache);
1863 ReleaseSharedLock(&afs_xvcache);
1864 ObtainWriteLock(&tvc->lock,55);
1866 /* It is always appropriate to throw away all the access rights? */
1867 afs_FreeAllAxs(&(tvc->Access));
1868 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
1870 if ((tvp->states & VForeign)) {
1871 if (newvcache) tvc->states |= CForeign;
1872 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1873 && (tvp->rootUnique == afid->Fid.Unique))
1876 if (tvp->states & VRO) tvc->states |= CRO;
1877 if (tvp->states & VBackup) tvc->states |= CBackup;
1878 /* now copy ".." entry back out of volume structure, if necessary */
1879 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1881 tvc->mvid = (struct VenusFid *)
1882 osi_AllocSmallSpace(sizeof(struct VenusFid));
1883 *tvc->mvid = tvp->dotdot;
1888 ObtainWriteLock(&afs_xcbhash, 465);
1889 afs_DequeueCallback(tvc);
1890 tvc->states &= ~( CStatd | CUnique );
1891 ReleaseWriteLock(&afs_xcbhash);
1892 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1893 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
1895 afs_PutVolume(tvp, READ_LOCK);
1896 ReleaseWriteLock(&tvc->lock);
1897 ObtainReadLock(&afs_xvcache);
1899 ReleaseReadLock(&afs_xvcache);
1903 ObtainWriteLock(&afs_xcbhash, 466);
1904 if (origCBs == afs_allCBs) {
1905 if (CallBack.ExpirationTime) {
1906 tvc->callback = serverp;
1907 tvc->cbExpires = CallBack.ExpirationTime+now;
1908 tvc->states |= CStatd | CUnique;
1909 tvc->states &= ~CBulkFetching;
1910 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
1911 } else if (tvc->states & CRO) {
1912 /* adapt gives us an hour. */
1913 tvc->cbExpires = 3600+osi_Time(); /*XXX*/
1914 tvc->states |= CStatd | CUnique;
1915 tvc->states &= ~CBulkFetching;
1916 afs_QueueCallback(tvc, CBHash(3600), tvp);
1918 tvc->callback = NULL;
1919 afs_DequeueCallback(tvc);
1920 tvc->states &= ~(CStatd | CUnique);
1921 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1922 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
1925 afs_DequeueCallback(tvc);
1926 tvc->states &= ~CStatd;
1927 tvc->states &= ~CUnique;
1928 tvc->callback = NULL;
1929 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1930 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
1932 ReleaseWriteLock(&afs_xcbhash);
1934 afs_PutVolume(tvp, READ_LOCK);
1935 afs_ProcessFS(tvc, &OutStatus, areq);
1937 ReleaseWriteLock(&tvc->lock);
1942 struct vcache *afs_GetRootVCache(struct VenusFid *afid,
1943 struct vrequest *areq, afs_int32 *cached,
1944 struct volume *tvolp)
1946 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
1947 afs_int32 getNewFid = 0;
1949 struct VenusFid nfid;
1950 register struct vcache *tvc;
1951 struct server *serverp = 0;
1952 struct AFSFetchStatus OutStatus;
1953 struct AFSCallBack CallBack;
1954 struct AFSVolSync tsync;
1960 if (!tvolp->rootVnode || getNewFid) {
1961 struct VenusFid tfid;
1964 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
1965 origCBs = afs_allCBs; /* ignore InitCallBackState */
1966 code = afs_RemoteLookup(&tfid, areq, NULL, &nfid,
1967 &OutStatus, &CallBack, &serverp, &tsync);
1971 /* ReleaseReadLock(&tvolp->lock); */
1972 ObtainWriteLock(&tvolp->lock,56);
1973 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
1974 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
1975 ReleaseWriteLock(&tvolp->lock);
1976 /* ObtainReadLock(&tvolp->lock);*/
1979 afid->Fid.Vnode = tvolp->rootVnode;
1980 afid->Fid.Unique = tvolp->rootUnique;
1983 ObtainSharedLock(&afs_xvcache,7);
1985 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1986 if (!FidCmp(&(tvc->fid), afid)) {
1988 /* Grab this vnode, possibly reactivating from the free list */
1989 /* for the present (95.05.25) everything on the hash table is
1990 * definitively NOT in the free list -- at least until afs_reclaim
1991 * can be safely implemented */
1994 vg = vget(AFSTOV(tvc)); /* this bumps ref count */
1998 #endif /* AFS_OSF_ENV */
2003 if (!haveStatus && (!tvc || !(tvc->states & CStatd))) {
2004 /* Mount point no longer stat'd or unknown. FID may have changed. */
2009 tvc = (struct vcache*)0;
2011 ReleaseSharedLock(&afs_xvcache);
2016 UpgradeSToWLock(&afs_xvcache,23);
2017 /* no cache entry, better grab one */
2018 tvc = afs_NewVCache(afid, NULL);
2020 afs_stats_cmperf.vcacheMisses++;
2023 if (cached) *cached = 1;
2024 afs_stats_cmperf.vcacheHits++;
2026 /* we already bumped the ref count in the for loop above */
2027 #else /* AFS_OSF_ENV */
2030 UpgradeSToWLock(&afs_xvcache,24);
2031 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2032 refpanic ("GRVC VLRU inconsistent0");
2034 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2035 refpanic ("GRVC VLRU inconsistent1");
2037 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2038 refpanic ("GRVC VLRU inconsistent2");
2040 QRemove(&tvc->vlruq); /* move to lruq head */
2041 QAdd(&VLRU, &tvc->vlruq);
2042 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2043 refpanic ("GRVC VLRU inconsistent3");
2045 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2046 refpanic ("GRVC VLRU inconsistent4");
2048 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2049 refpanic ("GRVC VLRU inconsistent5");
2054 ReleaseWriteLock(&afs_xvcache);
2056 if (tvc->states & CStatd) {
2060 ObtainReadLock(&tvc->lock);
2061 tvc->states &= ~CUnique;
2062 tvc->callback = NULL; /* redundant, perhaps */
2063 ReleaseReadLock(&tvc->lock);
2066 ObtainWriteLock(&tvc->lock,57);
2068 /* It is always appropriate to throw away all the access rights? */
2069 afs_FreeAllAxs(&(tvc->Access));
2071 if (newvcache) tvc->states |= CForeign;
2072 if (tvolp->states & VRO) tvc->states |= CRO;
2073 if (tvolp->states & VBackup) tvc->states |= CBackup;
2074 /* now copy ".." entry back out of volume structure, if necessary */
2075 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2076 && (tvolp->rootUnique == afid->Fid.Unique)) {
2079 if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2081 tvc->mvid = (struct VenusFid *)osi_AllocSmallSpace(sizeof(struct VenusFid));
2082 *tvc->mvid = tvolp->dotdot;
2086 afs_RemoveVCB(afid);
2089 struct VenusFid tfid;
2092 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2093 origCBs = afs_allCBs; /* ignore InitCallBackState */
2094 code = afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus,
2095 &CallBack, &serverp, &tsync);
2099 ObtainWriteLock(&afs_xcbhash, 467);
2100 afs_DequeueCallback(tvc);
2101 tvc->callback = NULL;
2102 tvc->states &= ~(CStatd|CUnique);
2103 ReleaseWriteLock(&afs_xcbhash);
2104 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2105 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
2106 ReleaseWriteLock(&tvc->lock);
2107 ObtainReadLock(&afs_xvcache);
2109 ReleaseReadLock(&afs_xvcache);
2113 ObtainWriteLock(&afs_xcbhash, 468);
2114 if (origCBs == afs_allCBs) {
2115 tvc->states |= CTruth;
2116 tvc->callback = serverp;
2117 if (CallBack.ExpirationTime != 0) {
2118 tvc->cbExpires = CallBack.ExpirationTime+start;
2119 tvc->states |= CStatd;
2120 tvc->states &= ~CBulkFetching;
2121 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2122 } else if (tvc->states & CRO) {
2123 /* adapt gives us an hour. */
2124 tvc->cbExpires = 3600+osi_Time(); /*XXX*/
2125 tvc->states |= CStatd;
2126 tvc->states &= ~CBulkFetching;
2127 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2130 afs_DequeueCallback(tvc);
2131 tvc->callback = NULL;
2132 tvc->states &= ~(CStatd | CUnique);
2133 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2134 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
2136 ReleaseWriteLock(&afs_xcbhash);
2137 afs_ProcessFS(tvc, &OutStatus, areq);
2139 ReleaseWriteLock(&tvc->lock);
2146 * must be called with avc write-locked
2147 * don't absolutely have to invalidate the hint unless the dv has
2148 * changed, but be sure to get it right else there will be consistency bugs.
2150 afs_int32 afs_FetchStatus(struct vcache *avc, struct VenusFid *afid,
2151 struct vrequest *areq, struct AFSFetchStatus *Outsp)
2154 afs_uint32 start = 0;
2155 register struct conn *tc;
2156 struct AFSCallBack CallBack;
2157 struct AFSVolSync tsync;
2158 struct volume* volp;
2162 tc = afs_Conn(afid, areq, SHARED_LOCK);
2163 avc->quick.stamp = 0; avc->h1.dchint = NULL; /* invalidate hints */
2165 avc->callback = tc->srvr->server;
2167 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2169 code = RXAFS_FetchStatus(tc->id,
2170 (struct AFSFid *) &afid->Fid,
2171 Outsp, &CallBack, &tsync);
2179 (afs_Analyze(tc, code, afid, areq,
2180 AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2181 SHARED_LOCK, NULL));
2184 afs_ProcessFS(avc, Outsp, areq);
2185 volp = afs_GetVolume(afid, areq, READ_LOCK);
2186 ObtainWriteLock(&afs_xcbhash, 469);
2187 avc->states |= CTruth;
2188 if (avc->callback /* check for race */) {
2189 if (CallBack.ExpirationTime != 0) {
2190 avc->cbExpires = CallBack.ExpirationTime+start;
2191 avc->states |= CStatd;
2192 avc->states &= ~CBulkFetching;
2193 afs_QueueCallback(avc, CBHash(CallBack.ExpirationTime), volp);
2195 else if (avc->states & CRO)
2196 { /* ordinary callback on a read-only volume -- AFS 3.2 style */
2197 avc->cbExpires = 3600+start;
2198 avc->states |= CStatd;
2199 avc->states &= ~CBulkFetching;
2200 afs_QueueCallback(avc, CBHash(3600), volp);
2203 afs_DequeueCallback(avc);
2204 avc->callback = NULL;
2205 avc->states &= ~(CStatd|CUnique);
2206 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2207 osi_dnlc_purgedp (avc); /* if it (could be) a directory */
2211 afs_DequeueCallback(avc);
2212 avc->callback = NULL;
2213 avc->states &= ~(CStatd|CUnique);
2214 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2215 osi_dnlc_purgedp (avc); /* if it (could be) a directory */
2217 ReleaseWriteLock(&afs_xcbhash);
2219 afs_PutVolume(volp, READ_LOCK);
2221 else { /* used to undo the local callback, but that's too extreme.
2222 * There are plenty of good reasons that fetchstatus might return
2223 * an error, such as EPERM. If we have the vnode cached, statd,
2224 * with callback, might as well keep track of the fact that we
2225 * don't have access...
2227 if (code == EPERM || code == EACCES) {
2228 struct axscache *ac;
2229 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2231 else /* not found, add a new one if possible */
2232 afs_AddAxs(avc->Access, areq->uid, 0);
2243 * Stuff some information into the vcache for the given file.
2246 * afid : File in question.
2247 * OutStatus : Fetch status on the file.
2248 * CallBack : Callback info.
2249 * tc : RPC connection involved.
2250 * areq : vrequest involved.
2253 * Nothing interesting.
2255 void afs_StuffVcache(register struct VenusFid *afid,
2256 struct AFSFetchStatus *OutStatus, struct AFSCallBack *CallBack,
2257 register struct conn *tc, struct vrequest *areq)
2259 register afs_int32 code, i, newvcache=0;
2260 register struct vcache *tvc;
2261 struct AFSVolSync tsync;
2263 struct axscache *ac;
2266 AFS_STATCNT(afs_StuffVcache);
2267 #ifdef IFS_VCACHECOUNT
2272 ObtainSharedLock(&afs_xvcache,8);
2274 tvc = afs_FindVCache(afid, &retry, DO_VLRU /* no stats */);
2276 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2277 ReleaseSharedLock(&afs_xvcache);
2278 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2284 /* no cache entry, better grab one */
2285 UpgradeSToWLock(&afs_xvcache,25);
2286 tvc = afs_NewVCache(afid, NULL);
2288 ConvertWToSLock(&afs_xvcache);
2291 ReleaseSharedLock(&afs_xvcache);
2292 ObtainWriteLock(&tvc->lock,58);
2294 tvc->states &= ~CStatd;
2295 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2296 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
2298 /* Is it always appropriate to throw away all the access rights? */
2299 afs_FreeAllAxs(&(tvc->Access));
2301 /*Copy useful per-volume info*/
2302 tvp = afs_GetVolume(afid, areq, READ_LOCK);
2304 if (newvcache && (tvp->states & VForeign)) tvc->states |= CForeign;
2305 if (tvp->states & VRO) tvc->states |= CRO;
2306 if (tvp->states & VBackup) tvc->states |= CBackup;
2308 * Now, copy ".." entry back out of volume structure, if
2311 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2312 if (!tvc->mvid) tvc->mvid =
2313 (struct VenusFid *) osi_AllocSmallSpace(sizeof(struct VenusFid));
2314 *tvc->mvid = tvp->dotdot;
2317 /* store the stat on the file */
2318 afs_RemoveVCB(afid);
2319 afs_ProcessFS(tvc, OutStatus, areq);
2320 tvc->callback = tc->srvr->server;
2322 /* we use osi_Time twice below. Ideally, we would use the time at which
2323 * the FetchStatus call began, instead, but we don't have it here. So we
2324 * make do with "now". In the CRO case, it doesn't really matter. In
2325 * the other case, we hope that the difference between "now" and when the
2326 * call actually began execution on the server won't be larger than the
2327 * padding which the server keeps. Subtract 1 second anyway, to be on
2328 * the safe side. Can't subtract more because we don't know how big
2329 * ExpirationTime is. Possible consistency problems may arise if the call
2330 * timeout period becomes longer than the server's expiration padding. */
2331 ObtainWriteLock(&afs_xcbhash, 470);
2332 if (CallBack->ExpirationTime != 0) {
2333 tvc->cbExpires = CallBack->ExpirationTime+osi_Time()-1;
2334 tvc->states |= CStatd;
2335 tvc->states &= ~CBulkFetching;
2336 afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2338 else if (tvc->states & CRO) {
2339 /* old-fashioned AFS 3.2 style */
2340 tvc->cbExpires = 3600+osi_Time(); /*XXX*/
2341 tvc->states |= CStatd;
2342 tvc->states &= ~CBulkFetching;
2343 afs_QueueCallback(tvc, CBHash(3600), tvp);
2346 afs_DequeueCallback(tvc);
2347 tvc->callback = NULL;
2348 tvc->states &= ~(CStatd|CUnique);
2349 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2350 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
2352 ReleaseWriteLock(&afs_xcbhash);
2354 afs_PutVolume(tvp, READ_LOCK);
2356 /* look in per-pag cache */
2357 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2358 ac->axess = OutStatus->CallerAccess; /* substitute pags */
2359 else /* not found, add a new one if possible */
2360 afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2362 ReleaseWriteLock(&tvc->lock);
2363 afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2364 ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32, tvc->cbExpires,
2365 ICL_TYPE_INT32, tvc->cbExpires-osi_Time());
2367 * Release ref count... hope this guy stays around...
2370 } /*afs_StuffVcache*/
2377 * Decrements the reference count on a cache entry.
2380 * avc : Pointer to the cache entry to decrement.
2383 * Nothing interesting.
2385 void afs_PutVCache(register struct vcache *avc)
2387 AFS_STATCNT(afs_PutVCache);
2389 * Can we use a read lock here?
2391 ObtainReadLock(&afs_xvcache);
2393 ReleaseReadLock(&afs_xvcache);
2400 * Find a vcache entry given a fid.
2403 * afid : Pointer to the fid whose cache entry we desire.
2404 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2405 * unlock the vnode, and try again.
2406 * flags: bit 1 to specify whether to compute hit statistics. Not
2407 * set if FindVCache is called as part of internal bookkeeping.
2410 * Must be called with the afs_xvcache lock at least held at
2411 * the read level. In order to do the VLRU adjustment, the xvcache lock
2412 * must be shared-- we upgrade it here.
2415 struct vcache *afs_FindVCache(struct VenusFid *afid, afs_int32 *retry, afs_int32 flag)
2418 register struct vcache *tvc;
2421 AFS_STATCNT(afs_FindVCache);
2424 for(tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2425 if (FidMatches(afid, tvc)) {
2427 /* Grab this vnode, possibly reactivating from the free list */
2430 vg = vget(AFSTOV(tvc));
2434 #endif /* AFS_OSF_ENV */
2439 /* should I have a read lock on the vnode here? */
2441 if (retry) *retry = 0;
2442 #if !defined(AFS_OSF_ENV)
2443 osi_vnhold(tvc, retry); /* already held, above */
2444 if (retry && *retry)
2448 * only move to front of vlru if we have proper vcache locking)
2450 if (flag & DO_VLRU) {
2451 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2452 refpanic ("FindVC VLRU inconsistent1");
2454 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2455 refpanic ("FindVC VLRU inconsistent1");
2457 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2458 refpanic ("FindVC VLRU inconsistent2");
2460 UpgradeSToWLock(&afs_xvcache,26);
2461 QRemove(&tvc->vlruq);
2462 QAdd(&VLRU, &tvc->vlruq);
2463 ConvertWToSLock(&afs_xvcache);
2464 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2465 refpanic ("FindVC VLRU inconsistent1");
2467 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2468 refpanic ("FindVC VLRU inconsistent2");
2470 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2471 refpanic ("FindVC VLRU inconsistent3");
2477 if (flag & DO_STATS) {
2479 afs_stats_cmperf.vcacheHits++;
2481 afs_stats_cmperf.vcacheMisses++;
2482 if (afs_IsPrimaryCellNum(afid->Cell))
2483 afs_stats_cmperf.vlocalAccesses++;
2485 afs_stats_cmperf.vremoteAccesses++;
2488 #ifdef AFS_LINUX22_ENV
2489 if (tvc && (tvc->states & CStatd))
2490 vcache2inode(tvc); /* mainly to reset i_nlink */
2492 #ifdef AFS_DARWIN_ENV
2494 osi_VM_Setup(tvc, 0);
2497 } /*afs_FindVCache*/
2503 * Find a vcache entry given a fid. Does a wildcard match on what we
2504 * have for the fid. If more than one entry, don't return anything.
2507 * avcp : Fill in pointer if we found one and only one.
2508 * afid : Pointer to the fid whose cache entry we desire.
2509 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2510 * unlock the vnode, and try again.
2511 * flags: bit 1 to specify whether to compute hit statistics. Not
2512 * set if FindVCache is called as part of internal bookkeeping.
2515 * Must be called with the afs_xvcache lock at least held at
2516 * the read level. In order to do the VLRU adjustment, the xvcache lock
2517 * must be shared-- we upgrade it here.
2520 * number of matches found.
2523 int afs_duplicate_nfs_fids=0;
2525 afs_int32 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2527 register struct vcache *tvc;
2529 afs_int32 count = 0;
2530 struct vcache *found_tvc = NULL;
2532 AFS_STATCNT(afs_FindVCache);
2534 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2538 ObtainSharedLock(&afs_xvcache,331);
2541 for(tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2542 /* Match only on what we have.... */
2543 if (((tvc->fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2544 && (tvc->fid.Fid.Volume == afid->Fid.Volume)
2545 && ((tvc->fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2546 && (tvc->fid.Cell == afid->Cell)) {
2548 /* Grab this vnode, possibly reactivating from the free list */
2551 vg = vget(AFSTOV(tvc));
2554 /* This vnode no longer exists. */
2557 #endif /* AFS_OSF_ENV */
2562 /* Drop our reference counts. */
2564 vrele(AFSTOV(found_tvc));
2566 afs_duplicate_nfs_fids++;
2567 ReleaseSharedLock(&afs_xvcache);
2575 /* should I have a read lock on the vnode here? */
2577 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2578 afs_int32 retry = 0;
2579 osi_vnhold(tvc, &retry);
2582 found_tvc = (struct vcache*)0;
2583 ReleaseSharedLock(&afs_xvcache);
2584 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2588 #if !defined(AFS_OSF_ENV)
2589 osi_vnhold(tvc, (int*)0); /* already held, above */
2593 * We obtained the xvcache lock above.
2595 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2596 refpanic ("FindVC VLRU inconsistent1");
2598 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2599 refpanic ("FindVC VLRU inconsistent1");
2601 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2602 refpanic ("FindVC VLRU inconsistent2");
2604 UpgradeSToWLock(&afs_xvcache,568);
2605 QRemove(&tvc->vlruq);
2606 QAdd(&VLRU, &tvc->vlruq);
2607 ConvertWToSLock(&afs_xvcache);
2608 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2609 refpanic ("FindVC VLRU inconsistent1");
2611 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2612 refpanic ("FindVC VLRU inconsistent2");
2614 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2615 refpanic ("FindVC VLRU inconsistent3");
2620 if (tvc) afs_stats_cmperf.vcacheHits++;
2621 else afs_stats_cmperf.vcacheMisses++;
2622 if (afs_IsPrimaryCellNum(afid->Cell))
2623 afs_stats_cmperf.vlocalAccesses++;
2625 afs_stats_cmperf.vremoteAccesses++;
2627 *avcp = tvc; /* May be null */
2629 ReleaseSharedLock(&afs_xvcache);
2630 return (tvc ? 1 : 0);
2632 } /*afs_NFSFindVCache*/
2640 * Initialize vcache related variables
2642 void afs_vcacheInit(int astatSize)
2644 register struct vcache *tvp;
2646 #if defined(AFS_OSF_ENV)
2647 if (!afs_maxvcount) {
2648 #if defined(AFS_OSF30_ENV)
2649 afs_maxvcount = max_vnodes/2; /* limit ourselves to half the total */
2651 afs_maxvcount = nvnode/2; /* limit ourselves to half the total */
2653 if (astatSize < afs_maxvcount) {
2654 afs_maxvcount = astatSize;
2657 #else /* AFS_OSF_ENV */
2661 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2662 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2664 #if !defined(AFS_OSF_ENV)
2665 /* Allocate and thread the struct vcache entries */
2666 tvp = (struct vcache *) afs_osi_Alloc(astatSize * sizeof(struct vcache));
2667 memset((char *)tvp, 0, sizeof(struct vcache)*astatSize);
2669 Initial_freeVCList = tvp;
2670 freeVCList = &(tvp[0]);
2671 for(i=0; i < astatSize-1; i++) {
2672 tvp[i].nextfree = &(tvp[i+1]);
2674 tvp[astatSize-1].nextfree = NULL;
2675 #ifdef KERNEL_HAVE_PIN
2676 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
2681 #if defined(AFS_SGI_ENV)
2682 for(i=0; i < astatSize; i++) {
2683 char name[METER_NAMSZ];
2684 struct vcache *tvc = &tvp[i];
2686 tvc->v.v_number = ++afsvnumbers;
2687 tvc->vc_rwlockid = OSI_NO_LOCKID;
2688 initnsema(&tvc->vc_rwlock, 1, makesname(name, "vrw", tvc->v.v_number));
2689 #ifndef AFS_SGI53_ENV
2690 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
2692 #ifndef AFS_SGI62_ENV
2693 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
2694 #endif /* AFS_SGI62_ENV */
2707 void shutdown_vcache(void)
2710 struct afs_cbr *tsp, *nsp;
2712 * XXX We may potentially miss some of the vcaches because if when there're no
2713 * free vcache entries and all the vcache entries are active ones then we allocate
2714 * an additional one - admittedly we almost never had that occur.
2716 #if !defined(AFS_OSF_ENV)
2717 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2719 #ifdef KERNEL_HAVE_PIN
2720 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2724 register struct afs_q *tq, *uq;
2725 register struct vcache *tvc;
2726 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
2730 osi_FreeSmallSpace(tvc->mvid);
2731 tvc->mvid = (struct VenusFid*)0;
2734 aix_gnode_rele(AFSTOV(tvc));
2736 if (tvc->linkData) {
2737 afs_osi_Free(tvc->linkData, strlen(tvc->linkData)+1);
2742 * Also free the remaining ones in the Cache
2744 for (i=0; i < VCSIZE; i++) {
2745 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2747 osi_FreeSmallSpace(tvc->mvid);
2748 tvc->mvid = (struct VenusFid*)0;
2752 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
2753 #ifdef AFS_AIX32_ENV
2756 vms_delete(tvc->segid);
2758 tvc->segid = tvc->vmh = NULL;
2759 if (VREFCOUNT(tvc)) osi_Panic("flushVcache: vm race");
2767 #if defined(AFS_SUN5_ENV)
2773 if (tvc->linkData) {
2774 afs_osi_Free(tvc->linkData, strlen(tvc->linkData)+1);
2778 afs_FreeAllAxs(&(tvc->Access));
2784 * Free any leftover callback queue
2786 for (tsp = afs_cbrSpace; tsp; tsp = nsp ) {
2788 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
2792 #if !defined(AFS_OSF_ENV)
2793 freeVCList = Initial_freeVCList = 0;
2795 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2796 LOCK_INIT(&afs_xvcb, "afs_xvcb");