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 (!DCOUNT(dentry)) {
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)
545 #ifdef AFS_LINUX24_ENV
550 #endif /* AFS_LINUX24_ENV */
551 dentry = list_entry(tmp, struct dentry, d_lru);
553 #ifdef AFS_LINUX24_ENV
554 /* Unused dentry with a count? */
559 #ifdef AFS_LINUX24_ENV
560 list_del_init(&dentry->d_hash); /* d_drop */
562 list_del(&dentry->d_hash);
563 INIT_LIST_HEAD(&dentry->d_hash);
564 #endif /* AFS_LINUX24_ENV */
573 /* afs_TryFlushDcacheChildren -- Shakes loose vcache references held by
574 * children of the dentry
576 * LOCKS -- Called with afs_xvcache write locked. Drops and reaquires
577 * AFS_GLOCK, so it can call dput, which may call iput, but
578 * keeps afs_xvcache exclusively.
580 * Tree traversal algorithm from fs/dcache.c: select_parent()
582 static void afs_TryFlushDcacheChildren(struct vcache *tvc)
584 struct inode *ip = AFSTOI(tvc);
585 struct dentry *this_parent;
586 struct list_head *next;
587 struct list_head *cur;
588 struct list_head *head = &ip->i_dentry;
589 struct dentry *dentry;
593 #ifndef old_vcache_scheme
596 while ((cur = cur->next) != head) {
597 dentry = list_entry(cur, struct dentry, d_alias);
599 afs_Trace3(afs_iclSetp, CM_TRACE_TRYFLUSHDCACHECHILDREN,
600 ICL_TYPE_POINTER, ip,
601 ICL_TYPE_STRING, dentry->d_parent->d_name.name,
602 ICL_TYPE_STRING, dentry->d_name.name);
604 if (!list_empty(&dentry->d_hash) && !list_empty(&dentry->d_subdirs))
605 __shrink_dcache_parent(dentry);
607 if (!DCOUNT(dentry)) {
609 #ifdef AFS_LINUX24_ENV
610 list_del_init(&dentry->d_hash); /* d_drop */
612 list_del(&dentry->d_hash);
613 INIT_LIST_HEAD(&dentry->d_hash);
614 #endif /* AFS_LINUX24_ENV */
626 while ((cur = cur->next) != head) {
627 dentry = list_entry(cur, struct dentry, d_alias);
629 afs_Trace3(afs_iclSetp, CM_TRACE_TRYFLUSHDCACHECHILDREN,
630 ICL_TYPE_POINTER, ip,
631 ICL_TYPE_STRING, dentry->d_parent->d_name.name,
632 ICL_TYPE_STRING, dentry->d_name.name);
634 if (!DCOUNT(dentry)) {
647 #endif /* AFS_LINUX22_ENV */
653 * This routine is responsible for allocating a new cache entry
654 * from the free list. It formats the cache entry and inserts it
655 * into the appropriate hash tables. It must be called with
656 * afs_xvcache write-locked so as to prevent several processes from
657 * trying to create a new cache entry simultaneously.
660 * afid : The file id of the file whose cache entry is being
663 /* LOCK: afs_NewVCache afs_xvcache W */
664 struct vcache *afs_NewVCache(struct VenusFid *afid, struct server *serverp)
668 afs_int32 anumber = VCACHE_FREE;
670 struct gnode *gnodepnt;
673 struct vm_info * vm_info_ptr;
674 #endif /* AFS_MACH_ENV */
677 #endif /* AFS_OSF_ENV */
678 struct afs_q *tq, *uq;
681 AFS_STATCNT(afs_NewVCache);
684 if (afs_vcount >= afs_maxvcount) {
687 * If we are using > 33 % of the total system vnodes for AFS vcache
688 * entries or we are using the maximum number of vcache entries,
689 * then free some. (if our usage is > 33% we should free some, if
690 * our usage is > afs_maxvcount, set elsewhere to 0.5*nvnode,
691 * we _must_ free some -- no choice).
693 if ( (( 3 * afs_vcount ) > nvnode) || ( afs_vcount >= afs_maxvcount )) {
695 struct afs_q *tq, *uq;
696 int i; char *panicstr;
699 for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
702 if (tvc->states & CVFlushed)
703 refpanic ("CVFlushed on VLRU");
704 else if (i++ > afs_maxvcount)
705 refpanic ("Exceeded pool of AFS vnodes(VLRU cycle?)");
706 else if (QNext(uq) != tq)
707 refpanic ("VLRU inconsistent");
708 else if (VREFCOUNT(tvc) < 1)
709 refpanic ("refcnt 0 on VLRU");
711 if ( VREFCOUNT(tvc) == 1 && tvc->opens == 0
712 && (tvc->states & CUnlinkedDel) == 0) {
713 code = afs_FlushVCache(tvc, &fv_slept);
720 continue; /* start over - may have raced. */
725 if (anumber == VCACHE_FREE) {
726 printf("NewVCache: warning none freed, using %d of %d\n",
727 afs_vcount, afs_maxvcount);
728 if (afs_vcount >= afs_maxvcount) {
729 osi_Panic("NewVCache - none freed");
730 /* XXX instead of panicing, should do afs_maxvcount++
731 and magic up another one */
737 if (getnewvnode(MOUNT_AFS, &Afs_vnodeops, &nvc)) {
738 /* What should we do ???? */
739 osi_Panic("afs_NewVCache: no more vnodes");
744 tvc->nextfree = NULL;
746 #else /* AFS_OSF_ENV */
747 /* pull out a free cache entry */
750 for (tq = VLRU.prev; (anumber > 0) && (tq != &VLRU); tq = uq) {
754 if (tvc->states & CVFlushed) {
755 refpanic("CVFlushed on VLRU");
756 } else if (i++ > 2*afs_cacheStats) { /* even allowing for a few xallocs...*/
757 refpanic("Increase -stat parameter of afsd(VLRU cycle?)");
758 } else if (QNext(uq) != tq) {
759 refpanic("VLRU inconsistent");
762 #ifdef AFS_DARWIN_ENV
763 if (tvc->opens == 0 && ((tvc->states & CUnlinkedDel) == 0) &&
764 VREFCOUNT(tvc) == 1 && UBCINFOEXISTS(&tvc->v)) {
765 osi_VM_TryReclaim(tvc, &fv_slept);
769 continue; /* start over - may have raced. */
773 #if defined(AFS_FBSD_ENV)
774 if (VREFCOUNT(tvc) == 1 && tvc->opens == 0
775 && (tvc->states & CUnlinkedDel) == 0) {
776 if (!(VOP_LOCK(&tvc->v, LK_EXCLUSIVE, curproc))) {
777 if (VREFCOUNT(tvc) == 1 && tvc->opens == 0
778 && (tvc->states & CUnlinkedDel) == 0) {
780 AFS_GUNLOCK(); /* perhaps inline inactive for locking */
781 VOP_INACTIVE(&tvc->v, curproc);
784 VOP_UNLOCK(&tvc->v, 0, curproc);
789 #if defined(AFS_LINUX22_ENV)
790 if (tvc != afs_globalVp && VREFCOUNT(tvc) && tvc->opens == 0)
791 afs_TryFlushDcacheChildren(tvc);
794 if (VREFCOUNT(tvc) == 0 && tvc->opens == 0
795 && (tvc->states & CUnlinkedDel) == 0) {
798 * vgone() reclaims the vnode, which calls afs_FlushVCache(),
799 * then it puts the vnode on the free list.
800 * If we don't do this we end up with a cleaned vnode that's
801 * not on the free list.
806 code = afs_FlushVCache(tvc, &fv_slept);
814 continue; /* start over - may have raced. */
817 if (tq == uq ) break;
821 /* none free, making one is better than a panic */
822 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
823 tvc = (struct vcache *) afs_osi_Alloc(sizeof (struct vcache));
824 #ifdef KERNEL_HAVE_PIN
825 pin((char *)tvc, sizeof(struct vcache)); /* XXX */
828 /* In case it still comes here we need to fill this */
829 tvc->v.v_vm_info = VM_INFO_NULL;
830 vm_info_init(tvc->v.v_vm_info);
831 /* perhaps we should also do close_flush on non-NeXT mach systems;
832 * who knows; we don't currently have the sources.
834 #endif /* AFS_MACH_ENV */
835 #if defined(AFS_SGI_ENV)
836 { char name[METER_NAMSZ];
837 memset(tvc, 0, sizeof(struct vcache));
838 tvc->v.v_number = ++afsvnumbers;
839 tvc->vc_rwlockid = OSI_NO_LOCKID;
840 initnsema(&tvc->vc_rwlock, 1, makesname(name, "vrw", tvc->v.v_number));
841 #ifndef AFS_SGI53_ENV
842 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
844 #ifndef AFS_SGI62_ENV
845 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
848 #endif /* AFS_SGI_ENV */
851 tvc = freeVCList; /* take from free list */
852 freeVCList = tvc->nextfree;
853 tvc->nextfree = NULL;
855 #endif /* AFS_OSF_ENV */
858 vm_info_ptr = tvc->v.v_vm_info;
859 #endif /* AFS_MACH_ENV */
861 #if defined(AFS_OBSD_ENV)
863 panic("afs_NewVCache(): free vcache with vnode attached");
866 #if !defined(AFS_SGI_ENV) && !defined(AFS_OSF_ENV)
867 memset((char *)tvc, 0, sizeof(struct vcache));
872 RWLOCK_INIT(&tvc->lock, "vcache lock");
873 #if defined(AFS_SUN5_ENV)
874 RWLOCK_INIT(&tvc->vlock, "vcache vlock");
875 #endif /* defined(AFS_SUN5_ENV) */
878 tvc->v.v_vm_info = vm_info_ptr;
879 tvc->v.v_vm_info->pager = MEMORY_OBJECT_NULL;
880 #endif /* AFS_MACH_ENV */
882 afs_nbsd_getnewvnode(tvc); /* includes one refcount */
883 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
885 tvc->parentVnode = 0;
887 tvc->linkData = NULL;
890 tvc->execsOrWriters = 0;
894 tvc->last_looker = 0;
896 tvc->asynchrony = -1;
898 afs_symhint_inval(tvc);
900 tvc->flushDV.low = tvc->flushDV.high = AFS_MAXDV;
903 tvc->truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
904 hzero(tvc->m.DataVersion); /* in case we copy it into flushDV */
906 /* Hold it for the LRU (should make count 2) */
907 VN_HOLD(AFSTOV(tvc));
908 #else /* AFS_OSF_ENV */
910 VREFCOUNT_SET(tvc, 1); /* us */
911 #endif /* AFS_OBSD_ENV */
912 #endif /* AFS_OSF_ENV */
914 LOCK_INIT(&tvc->pvmlock, "vcache pvmlock");
915 tvc->vmh = tvc->segid = NULL;
918 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
919 #if defined(AFS_SUN5_ENV)
920 rw_init(&tvc->rwlock, "vcache rwlock", RW_DEFAULT, NULL);
922 #if defined(AFS_SUN55_ENV)
923 /* This is required if the kaio (kernel aynchronous io)
924 ** module is installed. Inside the kernel, the function
925 ** check_vp( common/os/aio.c) checks to see if the kernel has
926 ** to provide asynchronous io for this vnode. This
927 ** function extracts the device number by following the
928 ** v_data field of the vnode. If we do not set this field
929 ** then the system panics. The value of the v_data field
930 ** is not really important for AFS vnodes because the kernel
931 ** does not do asynchronous io for regular files. Hence,
932 ** for the time being, we fill up the v_data field with the
933 ** vnode pointer itself. */
934 tvc->v.v_data = (char *)tvc;
935 #endif /* AFS_SUN55_ENV */
937 afs_BozonInit(&tvc->pvnLock, tvc);
941 tvc->callback = serverp; /* to minimize chance that clear
943 /* initialize vnode data, note vrefCount is v.v_count */
945 /* Don't forget to free the gnode space */
946 tvc->v.v_gnode = gnodepnt = (struct gnode *) osi_AllocSmallSpace(sizeof(struct gnode));
947 memset((char *)gnodepnt, 0, sizeof(struct gnode));
950 memset((void*)&(tvc->vc_bhv_desc), 0, sizeof(tvc->vc_bhv_desc));
951 bhv_desc_init(&(tvc->vc_bhv_desc), tvc, tvc, &Afs_vnodeops);
953 vn_bhv_head_init(&(tvc->v.v_bh), "afsvp");
954 vn_bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
956 bhv_head_init(&(tvc->v.v_bh));
957 bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
960 tvc->v.v_mreg = tvc->v.v_mregb = (struct pregion*)tvc;
962 tvc->v.v_trace = ktrace_alloc(VNODE_TRACE_SIZE, 0);
964 init_bitlock(&tvc->v.v_pcacheflag, VNODE_PCACHE_LOCKBIT, "afs_pcache",
966 init_mutex(&tvc->v.v_filocksem, MUTEX_DEFAULT, "afsvfl", (long)tvc);
967 init_mutex(&tvc->v.v_buf_lock, MUTEX_DEFAULT, "afsvnbuf", (long)tvc);
969 vnode_pcache_init(&tvc->v);
970 #if defined(DEBUG) && defined(VNODE_INIT_BITLOCK)
971 /* Above define is never true execpt in SGI test kernels. */
972 init_bitlock(&(tvc->v.v_flag, VLOCK, "vnode", tvc->v.v_number);
975 AFS_VN_INIT_BUF_LOCK(&(tvc->v));
978 SetAfsVnode(AFSTOV(tvc));
979 #endif /* AFS_SGI64_ENV */
980 #ifdef AFS_DARWIN_ENV
981 tvc->v.v_ubcinfo = UBC_INFO_NULL;
982 lockinit(&tvc->rwlock, PINOD, "vcache rwlock", 0, 0);
983 cache_purge(AFSTOV(tvc));
986 /* VLISTNONE(&tvc->v); */
987 tvc->v.v_freelist.tqe_next=0;
988 tvc->v.v_freelist.tqe_prev=(struct vnode **)0xdeadb;
989 /*tvc->vrefCount++;*/
992 lockinit(&tvc->rwlock, PINOD, "vcache rwlock", 0, 0);
993 cache_purge(AFSTOV(tvc));
996 tvc->v.v_usecount++; /* steal an extra ref for now so vfree never happens */
997 /* This extra ref is dealt with above... */
1000 * The proper value for mvstat (for root fids) is setup by the caller.
1003 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
1005 if (afs_globalVFS == 0) osi_Panic("afs globalvfs");
1006 vSetVfsp(tvc, afs_globalVFS);
1007 vSetType(tvc, VREG);
1009 tvc->v.v_vfsnext = afs_globalVFS->vfs_vnodes; /* link off vfs */
1010 tvc->v.v_vfsprev = NULL;
1011 afs_globalVFS->vfs_vnodes = &tvc->v;
1012 if (tvc->v.v_vfsnext != NULL)
1013 tvc->v.v_vfsnext->v_vfsprev = &tvc->v;
1014 tvc->v.v_next = gnodepnt->gn_vnode; /*Single vnode per gnode for us!*/
1015 gnodepnt->gn_vnode = &tvc->v;
1018 tvc->v.g_dev = ((struct mount *)afs_globalVFS->vfs_data)->m_dev;
1020 #if defined(AFS_DUX40_ENV)
1021 insmntque(tvc, afs_globalVFS, &afs_ubcops);
1024 /* Is this needed??? */
1025 insmntque(tvc, afs_globalVFS);
1026 #endif /* AFS_OSF_ENV */
1027 #endif /* AFS_DUX40_ENV */
1028 #if defined(AFS_SGI_ENV)
1029 VN_SET_DPAGES(&(tvc->v), (struct pfdat*)NULL);
1030 osi_Assert((tvc->v.v_flag & VINACT) == 0);
1032 osi_Assert(VN_GET_PGCNT(&(tvc->v)) == 0);
1033 osi_Assert(tvc->mapcnt == 0 && tvc->vc_locktrips == 0);
1034 osi_Assert(tvc->vc_rwlockid == OSI_NO_LOCKID);
1035 osi_Assert(tvc->v.v_filocks == NULL);
1036 #if !defined(AFS_SGI65_ENV)
1037 osi_Assert(tvc->v.v_filocksem == NULL);
1039 osi_Assert(tvc->cred == NULL);
1040 #ifdef AFS_SGI64_ENV
1041 vnode_pcache_reinit(&tvc->v);
1042 tvc->v.v_rdev = NODEV;
1044 vn_initlist((struct vnlist *)&tvc->v);
1046 #endif /* AFS_SGI_ENV */
1047 #if defined(AFS_LINUX22_ENV)
1049 struct inode *ip = AFSTOI(tvc);
1050 sema_init(&ip->i_sem, 1);
1051 #if defined(AFS_LINUX24_ENV)
1052 sema_init(&ip->i_zombie, 1);
1053 init_waitqueue_head(&ip->i_wait);
1054 spin_lock_init(&ip->i_data.i_shared_lock);
1055 #ifdef STRUCT_ADDRESS_SPACE_HAS_PAGE_LOCK
1056 spin_lock_init(&ip->i_data.page_lock);
1058 INIT_LIST_HEAD(&ip->i_data.clean_pages);
1059 INIT_LIST_HEAD(&ip->i_data.dirty_pages);
1060 INIT_LIST_HEAD(&ip->i_data.locked_pages);
1061 INIT_LIST_HEAD(&ip->i_dirty_buffers);
1062 #ifdef STRUCT_INODE_HAS_I_DIRTY_DATA_BUFFERS
1063 INIT_LIST_HEAD(&ip->i_dirty_data_buffers);
1065 #ifdef STRUCT_INODE_HAS_I_DEVICES
1066 INIT_LIST_HEAD(&ip->i_devices);
1068 ip->i_data.host = (void*) ip;
1069 #ifdef STRUCT_ADDRESS_SPACE_HAS_GFP_MASK
1070 ip->i_data.gfp_mask = GFP_HIGHUSER;
1072 ip->i_mapping = &ip->i_data;
1073 #ifdef STRUCT_INODE_HAS_I_TRUNCATE_SEM
1074 init_rwsem(&ip->i_truncate_sem);
1077 sema_init(&ip->i_atomic_write, 1);
1078 init_waitqueue(&ip->i_wait);
1080 INIT_LIST_HEAD(&ip->i_hash);
1081 INIT_LIST_HEAD(&ip->i_dentry);
1082 if (afs_globalVFS) {
1083 ip->i_dev = afs_globalVFS->s_dev;
1084 ip->i_sb = afs_globalVFS;
1089 osi_dnlc_purgedp(tvc); /* this may be overkill */
1090 memset((char *)&(tvc->quick), 0, sizeof(struct vtodc));
1091 memset((char *)&(tvc->callsort), 0, sizeof(struct afs_q));
1095 tvc->hnext = afs_vhashT[i];
1096 afs_vhashT[i] = tvc;
1097 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1098 refpanic ("NewVCache VLRU inconsistent");
1100 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
1101 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1102 refpanic ("NewVCache VLRU inconsistent2");
1104 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
1105 refpanic ("NewVCache VLRU inconsistent3");
1107 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
1108 refpanic ("NewVCache VLRU inconsistent4");
1118 * afs_FlushActiveVcaches
1124 * doflocks : Do we handle flocks?
1126 /* LOCK: afs_FlushActiveVcaches afs_xvcache N */
1127 void afs_FlushActiveVcaches(register afs_int32 doflocks)
1129 register struct vcache *tvc;
1131 register struct conn *tc;
1132 register afs_int32 code;
1133 register struct AFS_UCRED *cred = NULL;
1134 struct vrequest treq, ureq;
1135 struct AFSVolSync tsync;
1139 AFS_STATCNT(afs_FlushActiveVcaches);
1140 ObtainReadLock(&afs_xvcache);
1141 for(i=0;i<VCSIZE;i++) {
1142 for(tvc = afs_vhashT[i]; tvc; tvc=tvc->hnext) {
1143 if (doflocks && tvc->flockCount != 0) {
1144 /* if this entry has an flock, send a keep-alive call out */
1146 ReleaseReadLock(&afs_xvcache);
1147 ObtainWriteLock(&tvc->lock,51);
1149 afs_InitReq(&treq, &afs_osi_cred);
1150 treq.flags |= O_NONBLOCK;
1152 tc = afs_Conn(&tvc->fid, &treq, SHARED_LOCK);
1154 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
1157 RXAFS_ExtendLock(tc->id,
1158 (struct AFSFid *) &tvc->fid.Fid,
1165 (afs_Analyze(tc, code, &tvc->fid, &treq,
1166 AFS_STATS_FS_RPCIDX_EXTENDLOCK,
1167 SHARED_LOCK, NULL));
1169 ReleaseWriteLock(&tvc->lock);
1170 ObtainReadLock(&afs_xvcache);
1174 if ((tvc->states & CCore) || (tvc->states & CUnlinkedDel)) {
1176 * Don't let it evaporate in case someone else is in
1177 * this code. Also, drop the afs_xvcache lock while
1178 * getting vcache locks.
1181 ReleaseReadLock(&afs_xvcache);
1182 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
1183 afs_BozonLock(&tvc->pvnLock, tvc);
1185 #if defined(AFS_SGI_ENV)
1187 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1189 osi_Assert(VREFCOUNT(tvc) > 0);
1190 AFS_RWLOCK((vnode_t *)tvc, VRWLOCK_WRITE);
1192 ObtainWriteLock(&tvc->lock,52);
1193 if (tvc->states & CCore) {
1194 tvc->states &= ~CCore;
1195 /* XXXX Find better place-holder for cred XXXX */
1196 cred = (struct AFS_UCRED *) tvc->linkData;
1197 tvc->linkData = NULL; /* XXX */
1198 afs_InitReq(&ureq, cred);
1199 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1200 ICL_TYPE_POINTER, tvc,
1201 ICL_TYPE_INT32, tvc->execsOrWriters);
1202 code = afs_StoreOnLastReference(tvc, &ureq);
1203 ReleaseWriteLock(&tvc->lock);
1204 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
1205 afs_BozonUnlock(&tvc->pvnLock, tvc);
1207 hzero(tvc->flushDV);
1210 if (code && code != VNOVNODE) {
1211 afs_StoreWarn(code, tvc->fid.Fid.Volume,
1212 /* /dev/console */ 1);
1214 } else if (tvc->states & CUnlinkedDel) {
1218 ReleaseWriteLock(&tvc->lock);
1219 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
1220 afs_BozonUnlock(&tvc->pvnLock, tvc);
1222 #if defined(AFS_SGI_ENV)
1223 AFS_RWUNLOCK((vnode_t *)tvc, VRWLOCK_WRITE);
1225 afs_remunlink(tvc, 0);
1226 #if defined(AFS_SGI_ENV)
1227 AFS_RWLOCK((vnode_t *)tvc, VRWLOCK_WRITE);
1231 /* lost (or won, perhaps) the race condition */
1232 ReleaseWriteLock(&tvc->lock);
1233 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
1234 afs_BozonUnlock(&tvc->pvnLock, tvc);
1237 #if defined(AFS_SGI_ENV)
1238 AFS_RWUNLOCK((vnode_t *)tvc, VRWLOCK_WRITE);
1240 ObtainReadLock(&afs_xvcache);
1246 AFS_RELE(AFSTOV(tvc));
1248 /* Matches write code setting CCore flag */
1252 #ifdef AFS_DARWIN_ENV
1253 if (VREFCOUNT(tvc) == 1 && UBCINFOEXISTS(&tvc->v)) {
1254 if (tvc->opens) panic("flushactive open, hasubc, but refcnt 1");
1255 osi_VM_TryReclaim(tvc,0);
1260 ReleaseReadLock(&afs_xvcache);
1268 * Make sure a cache entry is up-to-date status-wise.
1270 * NOTE: everywhere that calls this can potentially be sped up
1271 * by checking CStatd first, and avoiding doing the InitReq
1272 * if this is up-to-date.
1274 * Anymore, the only places that call this KNOW already that the
1275 * vcache is not up-to-date, so we don't screw around.
1278 * avc : Ptr to vcache entry to verify.
1282 int afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1284 register struct vcache *tvc;
1286 AFS_STATCNT(afs_VerifyVCache);
1288 #if defined(AFS_OSF_ENV)
1289 ObtainReadLock(&avc->lock);
1290 if (afs_IsWired(avc)) {
1291 ReleaseReadLock(&avc->lock);
1294 ReleaseReadLock(&avc->lock);
1295 #endif /* AFS_OSF_ENV */
1296 /* otherwise we must fetch the status info */
1298 ObtainWriteLock(&avc->lock,53);
1299 if (avc->states & CStatd) {
1300 ReleaseWriteLock(&avc->lock);
1303 ObtainWriteLock(&afs_xcbhash, 461);
1304 avc->states &= ~( CStatd | CUnique );
1305 avc->callback = NULL;
1306 afs_DequeueCallback(avc);
1307 ReleaseWriteLock(&afs_xcbhash);
1308 ReleaseWriteLock(&avc->lock);
1310 /* since we've been called back, or the callback has expired,
1311 * it's possible that the contents of this directory, or this
1312 * file's name have changed, thus invalidating the dnlc contents.
1314 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1315 osi_dnlc_purgedp (avc);
1317 osi_dnlc_purgevp (avc);
1319 /* fetch the status info */
1320 tvc = afs_GetVCache(&avc->fid, areq, NULL, avc);
1321 if (!tvc) return ENOENT;
1322 /* Put it back; caller has already incremented vrefCount */
1326 } /*afs_VerifyVCache*/
1333 * Simple copy of stat info into cache.
1336 * avc : Ptr to vcache entry involved.
1337 * astat : Ptr to stat info to copy.
1340 * Nothing interesting.
1342 * Callers: as of 1992-04-29, only called by WriteVCache
1344 static void afs_SimpleVStat(register struct vcache *avc,
1345 register struct AFSFetchStatus *astat, struct vrequest *areq)
1348 AFS_STATCNT(afs_SimpleVStat);
1351 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1352 && !AFS_VN_MAPPED((vnode_t*)avc)) {
1354 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1356 #ifdef AFS_64BIT_ClIENT
1357 FillInt64(length, astat->Length_hi, astat->Length);
1358 #else /* AFS_64BIT_CLIENT */
1359 length = astat->Length;
1360 #endif /* AFS_64BIT_CLIENT */
1361 #if defined(AFS_SGI_ENV)
1362 osi_Assert((valusema(&avc->vc_rwlock) <= 0) &&
1363 (OSI_GET_LOCKID() == avc->vc_rwlockid));
1364 if (length < avc->m.Length) {
1365 vnode_t *vp = (vnode_t *)avc;
1367 osi_Assert(WriteLocked(&avc->lock));
1368 ReleaseWriteLock(&avc->lock);
1370 PTOSSVP(vp, (off_t)length, (off_t)MAXLONG);
1372 ObtainWriteLock(&avc->lock,67);
1375 /* if writing the file, don't fetch over this value */
1376 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT,
1377 ICL_TYPE_POINTER, avc,
1378 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1379 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1380 avc->m.Length = length;
1381 avc->m.Date = astat->ClientModTime;
1383 avc->m.Owner = astat->Owner;
1384 avc->m.Group = astat->Group;
1385 avc->m.Mode = astat->UnixModeBits;
1386 if (vType(avc) == VREG) {
1387 avc->m.Mode |= S_IFREG;
1389 else if (vType(avc) == VDIR) {
1390 avc->m.Mode |= S_IFDIR;
1392 else if (vType(avc) == VLNK) {
1393 avc->m.Mode |= S_IFLNK;
1394 if ((avc->m.Mode & 0111) == 0) avc->mvstat = 1;
1396 if (avc->states & CForeign) {
1397 struct axscache *ac;
1398 avc->anyAccess = astat->AnonymousAccess;
1400 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1402 * Caller has at least one bit not covered by anonymous, and
1403 * thus may have interesting rights.
1405 * HOWEVER, this is a really bad idea, because any access query
1406 * for bits which aren't covered by anonymous, on behalf of a user
1407 * who doesn't have any special rights, will result in an answer of
1408 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1409 * It's an especially bad idea under Ultrix, since (due to the lack of
1410 * a proper access() call) it must perform several afs_access() calls
1411 * in order to create magic mode bits that vary according to who makes
1412 * the call. In other words, _every_ stat() generates a test for
1415 #endif /* badidea */
1416 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1417 ac->axess = astat->CallerAccess;
1418 else /* not found, add a new one if possible */
1419 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1423 } /*afs_SimpleVStat*/
1430 * Store the status info *only* back to the server for a
1434 * avc : Ptr to the vcache entry.
1435 * astatus : Ptr to the status info to store.
1436 * areq : Ptr to the associated vrequest.
1439 * Must be called with a shared lock held on the vnode.
1442 int afs_WriteVCache(register struct vcache *avc,
1443 register struct AFSStoreStatus *astatus, struct vrequest *areq)
1447 struct AFSFetchStatus OutStatus;
1448 struct AFSVolSync tsync;
1451 AFS_STATCNT(afs_WriteVCache);
1452 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1453 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
1456 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
1458 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1460 code = RXAFS_StoreStatus(tc->id,
1461 (struct AFSFid *) &avc->fid.Fid,
1462 astatus, &OutStatus, &tsync);
1468 (afs_Analyze(tc, code, &avc->fid, areq,
1469 AFS_STATS_FS_RPCIDX_STORESTATUS,
1470 SHARED_LOCK, NULL));
1472 UpgradeSToWLock(&avc->lock,20);
1474 /* success, do the changes locally */
1475 afs_SimpleVStat(avc, &OutStatus, areq);
1477 * Update the date, too. SimpleVStat didn't do this, since
1478 * it thought we were doing this after fetching new status
1479 * over a file being written.
1481 avc->m.Date = OutStatus.ClientModTime;
1484 /* failure, set up to check with server next time */
1485 ObtainWriteLock(&afs_xcbhash, 462);
1486 afs_DequeueCallback(avc);
1487 avc->states &= ~( CStatd | CUnique); /* turn off stat valid flag */
1488 ReleaseWriteLock(&afs_xcbhash);
1489 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1490 osi_dnlc_purgedp (avc); /* if it (could be) a directory */
1492 ConvertWToSLock(&avc->lock);
1495 } /*afs_WriteVCache*/
1501 * Copy astat block into vcache info
1504 * avc : Ptr to vcache entry.
1505 * astat : Ptr to stat block to copy in.
1506 * areq : Ptr to associated request.
1509 * Must be called under a write lock
1511 * Note: this code may get dataversion and length out of sync if the file has
1512 * been modified. This is less than ideal. I haven't thought about
1513 * it sufficiently to be certain that it is adequate.
1515 void afs_ProcessFS(register struct vcache *avc, register struct AFSFetchStatus *astat,
1516 struct vrequest *areq)
1519 AFS_STATCNT(afs_ProcessFS);
1521 #ifdef AFS_64BIT_CLIENT
1522 FillInt64(length, astat->Length_hi, astat->Length);
1523 #else /* AFS_64BIT_CLIENT */
1524 length = astat->Length;
1525 #endif /* AFS_64BIT_CLIENT */
1526 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1527 * number for each bulk status request. Under no circumstances
1528 * should afs_DoBulkStat store a sequence number if the new
1529 * length will be ignored when afs_ProcessFS is called with
1530 * new stats. If you change the following conditional then you
1531 * also need to change the conditional in afs_DoBulkStat. */
1533 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1534 && !AFS_VN_MAPPED((vnode_t*)avc)) {
1536 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1538 /* if we're writing or mapping this file, don't fetch over these
1541 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1542 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1543 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1544 avc->m.Length = length;
1545 avc->m.Date = astat->ClientModTime;
1547 hset64(avc->m.DataVersion, astat->dataVersionHigh, astat->DataVersion);
1548 avc->m.Owner = astat->Owner;
1549 avc->m.Mode = astat->UnixModeBits;
1550 avc->m.Group = astat->Group;
1551 avc->m.LinkCount = astat->LinkCount;
1552 if (astat->FileType == File) {
1553 vSetType(avc, VREG);
1554 avc->m.Mode |= S_IFREG;
1556 else if (astat->FileType == Directory) {
1557 vSetType(avc, VDIR);
1558 avc->m.Mode |= S_IFDIR;
1560 else if (astat->FileType == SymbolicLink) {
1561 if (afs_fakestat_enable && (avc->m.Mode & 0111) == 0) {
1562 vSetType(avc, VDIR);
1563 avc->m.Mode |= S_IFDIR;
1565 vSetType(avc, VLNK);
1566 avc->m.Mode |= S_IFLNK;
1568 if ((avc->m.Mode & 0111) == 0) {
1572 avc->anyAccess = astat->AnonymousAccess;
1574 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1576 * Caller has at least one bit not covered by anonymous, and
1577 * thus may have interesting rights.
1579 * HOWEVER, this is a really bad idea, because any access query
1580 * for bits which aren't covered by anonymous, on behalf of a user
1581 * who doesn't have any special rights, will result in an answer of
1582 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1583 * It's an especially bad idea under Ultrix, since (due to the lack of
1584 * a proper access() call) it must perform several afs_access() calls
1585 * in order to create magic mode bits that vary according to who makes
1586 * the call. In other words, _every_ stat() generates a test for
1589 #endif /* badidea */
1591 struct axscache *ac;
1592 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1593 ac->axess = astat->CallerAccess;
1594 else /* not found, add a new one if possible */
1595 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1598 #ifdef AFS_LINUX22_ENV
1599 vcache2inode(avc); /* Set the inode attr cache */
1601 #ifdef AFS_DARWIN_ENV
1602 osi_VM_Setup(avc,1);
1608 int afs_RemoteLookup(register struct VenusFid *afid, struct vrequest *areq,
1609 char *name, struct VenusFid *nfid, struct AFSFetchStatus *OutStatusp,
1610 struct AFSCallBack *CallBackp, struct server **serverp, struct AFSVolSync *tsyncp)
1614 register struct conn *tc;
1615 struct AFSFetchStatus OutDirStatus;
1618 if (!name) name = ""; /* XXX */
1620 tc = afs_Conn(afid, areq, SHARED_LOCK);
1622 if (serverp) *serverp = tc->srvr->server;
1624 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1626 code = RXAFS_Lookup(tc->id, (struct AFSFid *) &afid->Fid, name,
1627 (struct AFSFid *) &nfid->Fid,
1628 OutStatusp, &OutDirStatus, CallBackp, tsyncp);
1634 (afs_Analyze(tc, code, afid, areq,
1635 AFS_STATS_FS_RPCIDX_XLOOKUP,
1636 SHARED_LOCK, NULL));
1646 * Given a file id and a vrequest structure, fetch the status
1647 * information associated with the file.
1651 * areq : Ptr to associated vrequest structure, specifying the
1652 * user whose authentication tokens will be used.
1653 * avc : caller may already have a vcache for this file, which is
1657 * The cache entry is returned with an increased vrefCount field.
1658 * The entry must be discarded by calling afs_PutVCache when you
1659 * are through using the pointer to the cache entry.
1661 * You should not hold any locks when calling this function, except
1662 * locks on other vcache entries. If you lock more than one vcache
1663 * entry simultaneously, you should lock them in this order:
1665 * 1. Lock all files first, then directories.
1666 * 2. Within a particular type, lock entries in Fid.Vnode order.
1668 * This locking hierarchy is convenient because it allows locking
1669 * of a parent dir cache entry, given a file (to check its access
1670 * control list). It also allows renames to be handled easily by
1671 * locking directories in a constant order.
1672 * NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1674 /* might have a vcache structure already, which must
1675 * already be held by the caller */
1677 struct vcache *afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
1678 afs_int32 *cached, struct vcache *avc)
1681 afs_int32 code, newvcache=0;
1682 register struct vcache *tvc;
1686 AFS_STATCNT(afs_GetVCache);
1688 if (cached) *cached = 0; /* Init just in case */
1690 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1694 ObtainSharedLock(&afs_xvcache,5);
1696 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU );
1698 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1699 ReleaseSharedLock(&afs_xvcache);
1700 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1708 if (tvc->states & CStatd) {
1709 ReleaseSharedLock(&afs_xvcache);
1714 UpgradeSToWLock(&afs_xvcache,21);
1716 /* no cache entry, better grab one */
1717 tvc = afs_NewVCache(afid, NULL);
1720 ConvertWToSLock(&afs_xvcache);
1721 afs_stats_cmperf.vcacheMisses++;
1724 ReleaseSharedLock(&afs_xvcache);
1726 ObtainWriteLock(&tvc->lock,54);
1728 if (tvc->states & CStatd) {
1729 #ifdef AFS_LINUX22_ENV
1732 ReleaseWriteLock(&tvc->lock);
1733 #ifdef AFS_DARWIN_ENV
1734 osi_VM_Setup(tvc,0);
1739 #if defined(AFS_OSF_ENV)
1740 if (afs_IsWired(tvc)) {
1741 ReleaseWriteLock(&tvc->lock);
1744 #endif /* AFS_OSF_ENV */
1746 VOP_LOCK(AFSTOV(tvc), LK_EXCLUSIVE | LK_RETRY, curproc);
1747 uvm_vnp_uncache(AFSTOV(tvc));
1748 VOP_UNLOCK(AFSTOV(tvc), 0, curproc);
1751 ObtainWriteLock(&afs_xcbhash, 464);
1752 tvc->states &= ~CUnique;
1754 afs_DequeueCallback(tvc);
1755 ReleaseWriteLock(&afs_xcbhash);
1757 /* It is always appropriate to throw away all the access rights? */
1758 afs_FreeAllAxs(&(tvc->Access));
1759 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1761 if ((tvp->states & VForeign)) {
1762 if (newvcache) tvc->states |= CForeign;
1763 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1764 && (tvp->rootUnique == afid->Fid.Unique)) {
1768 if (tvp->states & VRO) tvc->states |= CRO;
1769 if (tvp->states & VBackup) tvc->states |= CBackup;
1770 /* now copy ".." entry back out of volume structure, if necessary */
1771 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1773 tvc->mvid = (struct VenusFid *)
1774 osi_AllocSmallSpace(sizeof(struct VenusFid));
1775 *tvc->mvid = tvp->dotdot;
1777 afs_PutVolume(tvp, READ_LOCK);
1781 afs_RemoveVCB(afid);
1783 struct AFSFetchStatus OutStatus;
1785 if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1786 afs_ProcessFS(tvc, &OutStatus, areq);
1787 tvc->states |= CStatd | CUnique;
1790 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1795 ReleaseWriteLock(&tvc->lock);
1797 ObtainReadLock(&afs_xvcache);
1799 ReleaseReadLock(&afs_xvcache);
1803 ReleaseWriteLock(&tvc->lock);
1810 struct vcache *afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1811 afs_int32 *cached, struct vcache *adp, char *aname)
1813 afs_int32 code, now, newvcache=0;
1814 struct VenusFid nfid;
1815 register struct vcache *tvc;
1817 struct AFSFetchStatus OutStatus;
1818 struct AFSCallBack CallBack;
1819 struct AFSVolSync tsync;
1820 struct server *serverp = 0;
1824 AFS_STATCNT(afs_GetVCache);
1825 if (cached) *cached = 0; /* Init just in case */
1827 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1831 ObtainReadLock(&afs_xvcache);
1832 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */);
1835 ReleaseReadLock(&afs_xvcache);
1837 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1838 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1842 ObtainReadLock(&tvc->lock);
1844 if (tvc->states & CStatd) {
1848 ReleaseReadLock(&tvc->lock);
1851 tvc->states &= ~CUnique;
1853 ReleaseReadLock(&tvc->lock);
1854 ObtainReadLock(&afs_xvcache);
1858 ReleaseReadLock(&afs_xvcache);
1860 /* lookup the file */
1863 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
1864 code = afs_RemoteLookup(&adp->fid, areq, aname, &nfid, &OutStatus, &CallBack,
1867 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1871 ObtainSharedLock(&afs_xvcache,6);
1872 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU /* no xstats now*/);
1874 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1875 ReleaseSharedLock(&afs_xvcache);
1876 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1882 /* no cache entry, better grab one */
1883 UpgradeSToWLock(&afs_xvcache,22);
1884 tvc = afs_NewVCache(&nfid, NULL);
1886 ConvertWToSLock(&afs_xvcache);
1889 ReleaseSharedLock(&afs_xvcache);
1890 ObtainWriteLock(&tvc->lock,55);
1892 /* It is always appropriate to throw away all the access rights? */
1893 afs_FreeAllAxs(&(tvc->Access));
1894 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
1896 if ((tvp->states & VForeign)) {
1897 if (newvcache) tvc->states |= CForeign;
1898 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1899 && (tvp->rootUnique == afid->Fid.Unique))
1902 if (tvp->states & VRO) tvc->states |= CRO;
1903 if (tvp->states & VBackup) tvc->states |= CBackup;
1904 /* now copy ".." entry back out of volume structure, if necessary */
1905 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1907 tvc->mvid = (struct VenusFid *)
1908 osi_AllocSmallSpace(sizeof(struct VenusFid));
1909 *tvc->mvid = tvp->dotdot;
1914 ObtainWriteLock(&afs_xcbhash, 465);
1915 afs_DequeueCallback(tvc);
1916 tvc->states &= ~( CStatd | CUnique );
1917 ReleaseWriteLock(&afs_xcbhash);
1918 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1919 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
1921 afs_PutVolume(tvp, READ_LOCK);
1922 ReleaseWriteLock(&tvc->lock);
1923 ObtainReadLock(&afs_xvcache);
1925 ReleaseReadLock(&afs_xvcache);
1929 ObtainWriteLock(&afs_xcbhash, 466);
1930 if (origCBs == afs_allCBs) {
1931 if (CallBack.ExpirationTime) {
1932 tvc->callback = serverp;
1933 tvc->cbExpires = CallBack.ExpirationTime+now;
1934 tvc->states |= CStatd | CUnique;
1935 tvc->states &= ~CBulkFetching;
1936 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
1937 } else if (tvc->states & CRO) {
1938 /* adapt gives us an hour. */
1939 tvc->cbExpires = 3600+osi_Time(); /*XXX*/
1940 tvc->states |= CStatd | CUnique;
1941 tvc->states &= ~CBulkFetching;
1942 afs_QueueCallback(tvc, CBHash(3600), tvp);
1944 tvc->callback = NULL;
1945 afs_DequeueCallback(tvc);
1946 tvc->states &= ~(CStatd | CUnique);
1947 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1948 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
1951 afs_DequeueCallback(tvc);
1952 tvc->states &= ~CStatd;
1953 tvc->states &= ~CUnique;
1954 tvc->callback = NULL;
1955 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1956 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
1958 ReleaseWriteLock(&afs_xcbhash);
1960 afs_PutVolume(tvp, READ_LOCK);
1961 afs_ProcessFS(tvc, &OutStatus, areq);
1963 ReleaseWriteLock(&tvc->lock);
1968 struct vcache *afs_GetRootVCache(struct VenusFid *afid,
1969 struct vrequest *areq, afs_int32 *cached,
1970 struct volume *tvolp)
1972 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
1973 afs_int32 getNewFid = 0;
1975 struct VenusFid nfid;
1976 register struct vcache *tvc;
1977 struct server *serverp = 0;
1978 struct AFSFetchStatus OutStatus;
1979 struct AFSCallBack CallBack;
1980 struct AFSVolSync tsync;
1986 if (!tvolp->rootVnode || getNewFid) {
1987 struct VenusFid tfid;
1990 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
1991 origCBs = afs_allCBs; /* ignore InitCallBackState */
1992 code = afs_RemoteLookup(&tfid, areq, NULL, &nfid,
1993 &OutStatus, &CallBack, &serverp, &tsync);
1997 /* ReleaseReadLock(&tvolp->lock); */
1998 ObtainWriteLock(&tvolp->lock,56);
1999 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
2000 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
2001 ReleaseWriteLock(&tvolp->lock);
2002 /* ObtainReadLock(&tvolp->lock);*/
2005 afid->Fid.Vnode = tvolp->rootVnode;
2006 afid->Fid.Unique = tvolp->rootUnique;
2009 ObtainSharedLock(&afs_xvcache,7);
2011 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2012 if (!FidCmp(&(tvc->fid), afid)) {
2014 /* Grab this vnode, possibly reactivating from the free list */
2015 /* for the present (95.05.25) everything on the hash table is
2016 * definitively NOT in the free list -- at least until afs_reclaim
2017 * can be safely implemented */
2020 vg = vget(AFSTOV(tvc)); /* this bumps ref count */
2024 #endif /* AFS_OSF_ENV */
2029 if (!haveStatus && (!tvc || !(tvc->states & CStatd))) {
2030 /* Mount point no longer stat'd or unknown. FID may have changed. */
2033 AFS_RELE(AFSTOV(tvc));
2037 ReleaseSharedLock(&afs_xvcache);
2042 UpgradeSToWLock(&afs_xvcache,23);
2043 /* no cache entry, better grab one */
2044 tvc = afs_NewVCache(afid, NULL);
2046 afs_stats_cmperf.vcacheMisses++;
2049 if (cached) *cached = 1;
2050 afs_stats_cmperf.vcacheHits++;
2052 /* we already bumped the ref count in the for loop above */
2053 #else /* AFS_OSF_ENV */
2056 UpgradeSToWLock(&afs_xvcache,24);
2057 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2058 refpanic ("GRVC VLRU inconsistent0");
2060 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2061 refpanic ("GRVC VLRU inconsistent1");
2063 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2064 refpanic ("GRVC VLRU inconsistent2");
2066 QRemove(&tvc->vlruq); /* move to lruq head */
2067 QAdd(&VLRU, &tvc->vlruq);
2068 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2069 refpanic ("GRVC VLRU inconsistent3");
2071 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2072 refpanic ("GRVC VLRU inconsistent4");
2074 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2075 refpanic ("GRVC VLRU inconsistent5");
2080 ReleaseWriteLock(&afs_xvcache);
2082 if (tvc->states & CStatd) {
2086 ObtainReadLock(&tvc->lock);
2087 tvc->states &= ~CUnique;
2088 tvc->callback = NULL; /* redundant, perhaps */
2089 ReleaseReadLock(&tvc->lock);
2092 ObtainWriteLock(&tvc->lock,57);
2094 /* It is always appropriate to throw away all the access rights? */
2095 afs_FreeAllAxs(&(tvc->Access));
2097 if (newvcache) tvc->states |= CForeign;
2098 if (tvolp->states & VRO) tvc->states |= CRO;
2099 if (tvolp->states & VBackup) tvc->states |= CBackup;
2100 /* now copy ".." entry back out of volume structure, if necessary */
2101 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2102 && (tvolp->rootUnique == afid->Fid.Unique)) {
2105 if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2107 tvc->mvid = (struct VenusFid *)osi_AllocSmallSpace(sizeof(struct VenusFid));
2108 *tvc->mvid = tvolp->dotdot;
2112 afs_RemoveVCB(afid);
2115 struct VenusFid tfid;
2118 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2119 origCBs = afs_allCBs; /* ignore InitCallBackState */
2120 code = afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus,
2121 &CallBack, &serverp, &tsync);
2125 ObtainWriteLock(&afs_xcbhash, 467);
2126 afs_DequeueCallback(tvc);
2127 tvc->callback = NULL;
2128 tvc->states &= ~(CStatd|CUnique);
2129 ReleaseWriteLock(&afs_xcbhash);
2130 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2131 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
2132 ReleaseWriteLock(&tvc->lock);
2133 ObtainReadLock(&afs_xvcache);
2135 ReleaseReadLock(&afs_xvcache);
2139 ObtainWriteLock(&afs_xcbhash, 468);
2140 if (origCBs == afs_allCBs) {
2141 tvc->states |= CTruth;
2142 tvc->callback = serverp;
2143 if (CallBack.ExpirationTime != 0) {
2144 tvc->cbExpires = CallBack.ExpirationTime+start;
2145 tvc->states |= CStatd;
2146 tvc->states &= ~CBulkFetching;
2147 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2148 } else if (tvc->states & CRO) {
2149 /* adapt gives us an hour. */
2150 tvc->cbExpires = 3600+osi_Time(); /*XXX*/
2151 tvc->states |= CStatd;
2152 tvc->states &= ~CBulkFetching;
2153 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2156 afs_DequeueCallback(tvc);
2157 tvc->callback = NULL;
2158 tvc->states &= ~(CStatd | CUnique);
2159 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2160 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
2162 ReleaseWriteLock(&afs_xcbhash);
2163 afs_ProcessFS(tvc, &OutStatus, areq);
2165 ReleaseWriteLock(&tvc->lock);
2172 * must be called with avc write-locked
2173 * don't absolutely have to invalidate the hint unless the dv has
2174 * changed, but be sure to get it right else there will be consistency bugs.
2176 afs_int32 afs_FetchStatus(struct vcache *avc, struct VenusFid *afid,
2177 struct vrequest *areq, struct AFSFetchStatus *Outsp)
2180 afs_uint32 start = 0;
2181 register struct conn *tc;
2182 struct AFSCallBack CallBack;
2183 struct AFSVolSync tsync;
2184 struct volume* volp;
2188 tc = afs_Conn(afid, areq, SHARED_LOCK);
2189 avc->quick.stamp = 0; avc->h1.dchint = NULL; /* invalidate hints */
2191 avc->callback = tc->srvr->server;
2193 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2195 code = RXAFS_FetchStatus(tc->id,
2196 (struct AFSFid *) &afid->Fid,
2197 Outsp, &CallBack, &tsync);
2205 (afs_Analyze(tc, code, afid, areq,
2206 AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2207 SHARED_LOCK, NULL));
2210 afs_ProcessFS(avc, Outsp, areq);
2211 volp = afs_GetVolume(afid, areq, READ_LOCK);
2212 ObtainWriteLock(&afs_xcbhash, 469);
2213 avc->states |= CTruth;
2214 if (avc->callback /* check for race */) {
2215 if (CallBack.ExpirationTime != 0) {
2216 avc->cbExpires = CallBack.ExpirationTime+start;
2217 avc->states |= CStatd;
2218 avc->states &= ~CBulkFetching;
2219 afs_QueueCallback(avc, CBHash(CallBack.ExpirationTime), volp);
2221 else if (avc->states & CRO)
2222 { /* ordinary callback on a read-only volume -- AFS 3.2 style */
2223 avc->cbExpires = 3600+start;
2224 avc->states |= CStatd;
2225 avc->states &= ~CBulkFetching;
2226 afs_QueueCallback(avc, CBHash(3600), volp);
2229 afs_DequeueCallback(avc);
2230 avc->callback = NULL;
2231 avc->states &= ~(CStatd|CUnique);
2232 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2233 osi_dnlc_purgedp (avc); /* if it (could be) a directory */
2237 afs_DequeueCallback(avc);
2238 avc->callback = NULL;
2239 avc->states &= ~(CStatd|CUnique);
2240 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2241 osi_dnlc_purgedp (avc); /* if it (could be) a directory */
2243 ReleaseWriteLock(&afs_xcbhash);
2245 afs_PutVolume(volp, READ_LOCK);
2248 /* used to undo the local callback, but that's too extreme.
2249 * There are plenty of good reasons that fetchstatus might return
2250 * an error, such as EPERM. If we have the vnode cached, statd,
2251 * with callback, might as well keep track of the fact that we
2252 * don't have access...
2254 if (code == EPERM || code == EACCES) {
2255 struct axscache *ac;
2256 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2258 else /* not found, add a new one if possible */
2259 afs_AddAxs(avc->Access, areq->uid, 0);
2270 * Stuff some information into the vcache for the given file.
2273 * afid : File in question.
2274 * OutStatus : Fetch status on the file.
2275 * CallBack : Callback info.
2276 * tc : RPC connection involved.
2277 * areq : vrequest involved.
2280 * Nothing interesting.
2282 void afs_StuffVcache(register struct VenusFid *afid,
2283 struct AFSFetchStatus *OutStatus, struct AFSCallBack *CallBack,
2284 register struct conn *tc, struct vrequest *areq)
2286 register afs_int32 code, i, newvcache=0;
2287 register struct vcache *tvc;
2288 struct AFSVolSync tsync;
2290 struct axscache *ac;
2293 AFS_STATCNT(afs_StuffVcache);
2294 #ifdef IFS_VCACHECOUNT
2299 ObtainSharedLock(&afs_xvcache,8);
2301 tvc = afs_FindVCache(afid, &retry, DO_VLRU /* no stats */);
2303 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2304 ReleaseSharedLock(&afs_xvcache);
2305 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2311 /* no cache entry, better grab one */
2312 UpgradeSToWLock(&afs_xvcache,25);
2313 tvc = afs_NewVCache(afid, NULL);
2315 ConvertWToSLock(&afs_xvcache);
2318 ReleaseSharedLock(&afs_xvcache);
2319 ObtainWriteLock(&tvc->lock,58);
2321 tvc->states &= ~CStatd;
2322 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2323 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
2325 /* Is it always appropriate to throw away all the access rights? */
2326 afs_FreeAllAxs(&(tvc->Access));
2328 /*Copy useful per-volume info*/
2329 tvp = afs_GetVolume(afid, areq, READ_LOCK);
2331 if (newvcache && (tvp->states & VForeign)) tvc->states |= CForeign;
2332 if (tvp->states & VRO) tvc->states |= CRO;
2333 if (tvp->states & VBackup) tvc->states |= CBackup;
2335 * Now, copy ".." entry back out of volume structure, if
2338 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2339 if (!tvc->mvid) tvc->mvid =
2340 (struct VenusFid *) osi_AllocSmallSpace(sizeof(struct VenusFid));
2341 *tvc->mvid = tvp->dotdot;
2344 /* store the stat on the file */
2345 afs_RemoveVCB(afid);
2346 afs_ProcessFS(tvc, OutStatus, areq);
2347 tvc->callback = tc->srvr->server;
2349 /* we use osi_Time twice below. Ideally, we would use the time at which
2350 * the FetchStatus call began, instead, but we don't have it here. So we
2351 * make do with "now". In the CRO case, it doesn't really matter. In
2352 * the other case, we hope that the difference between "now" and when the
2353 * call actually began execution on the server won't be larger than the
2354 * padding which the server keeps. Subtract 1 second anyway, to be on
2355 * the safe side. Can't subtract more because we don't know how big
2356 * ExpirationTime is. Possible consistency problems may arise if the call
2357 * timeout period becomes longer than the server's expiration padding. */
2358 ObtainWriteLock(&afs_xcbhash, 470);
2359 if (CallBack->ExpirationTime != 0) {
2360 tvc->cbExpires = CallBack->ExpirationTime+osi_Time()-1;
2361 tvc->states |= CStatd;
2362 tvc->states &= ~CBulkFetching;
2363 afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2365 else if (tvc->states & CRO) {
2366 /* old-fashioned AFS 3.2 style */
2367 tvc->cbExpires = 3600+osi_Time(); /*XXX*/
2368 tvc->states |= CStatd;
2369 tvc->states &= ~CBulkFetching;
2370 afs_QueueCallback(tvc, CBHash(3600), tvp);
2373 afs_DequeueCallback(tvc);
2374 tvc->callback = NULL;
2375 tvc->states &= ~(CStatd|CUnique);
2376 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2377 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
2379 ReleaseWriteLock(&afs_xcbhash);
2381 afs_PutVolume(tvp, READ_LOCK);
2383 /* look in per-pag cache */
2384 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2385 ac->axess = OutStatus->CallerAccess; /* substitute pags */
2386 else /* not found, add a new one if possible */
2387 afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2389 ReleaseWriteLock(&tvc->lock);
2390 afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2391 ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32, tvc->cbExpires,
2392 ICL_TYPE_INT32, tvc->cbExpires-osi_Time());
2394 * Release ref count... hope this guy stays around...
2397 } /*afs_StuffVcache*/
2404 * Decrements the reference count on a cache entry.
2407 * avc : Pointer to the cache entry to decrement.
2410 * Nothing interesting.
2412 void afs_PutVCache(register struct vcache *avc)
2414 AFS_STATCNT(afs_PutVCache);
2416 * Can we use a read lock here?
2418 ObtainReadLock(&afs_xvcache);
2420 ReleaseReadLock(&afs_xvcache);
2427 * Find a vcache entry given a fid.
2430 * afid : Pointer to the fid whose cache entry we desire.
2431 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2432 * unlock the vnode, and try again.
2433 * flags: bit 1 to specify whether to compute hit statistics. Not
2434 * set if FindVCache is called as part of internal bookkeeping.
2437 * Must be called with the afs_xvcache lock at least held at
2438 * the read level. In order to do the VLRU adjustment, the xvcache lock
2439 * must be shared-- we upgrade it here.
2442 struct vcache *afs_FindVCache(struct VenusFid *afid, afs_int32 *retry, afs_int32 flag)
2445 register struct vcache *tvc;
2448 AFS_STATCNT(afs_FindVCache);
2451 for(tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2452 if (FidMatches(afid, tvc)) {
2454 /* Grab this vnode, possibly reactivating from the free list */
2457 vg = vget(AFSTOV(tvc));
2461 #endif /* AFS_OSF_ENV */
2466 /* should I have a read lock on the vnode here? */
2468 if (retry) *retry = 0;
2469 #if !defined(AFS_OSF_ENV)
2470 osi_vnhold(tvc, retry); /* already held, above */
2471 if (retry && *retry)
2475 * only move to front of vlru if we have proper vcache locking)
2477 if (flag & DO_VLRU) {
2478 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2479 refpanic ("FindVC VLRU inconsistent1");
2481 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2482 refpanic ("FindVC VLRU inconsistent1");
2484 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2485 refpanic ("FindVC VLRU inconsistent2");
2487 UpgradeSToWLock(&afs_xvcache,26);
2488 QRemove(&tvc->vlruq);
2489 QAdd(&VLRU, &tvc->vlruq);
2490 ConvertWToSLock(&afs_xvcache);
2491 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2492 refpanic ("FindVC VLRU inconsistent1");
2494 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2495 refpanic ("FindVC VLRU inconsistent2");
2497 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2498 refpanic ("FindVC VLRU inconsistent3");
2504 if (flag & DO_STATS) {
2506 afs_stats_cmperf.vcacheHits++;
2508 afs_stats_cmperf.vcacheMisses++;
2509 if (afs_IsPrimaryCellNum(afid->Cell))
2510 afs_stats_cmperf.vlocalAccesses++;
2512 afs_stats_cmperf.vremoteAccesses++;
2515 #ifdef AFS_LINUX22_ENV
2516 if (tvc && (tvc->states & CStatd))
2517 vcache2inode(tvc); /* mainly to reset i_nlink */
2519 #ifdef AFS_DARWIN_ENV
2521 osi_VM_Setup(tvc, 0);
2524 } /*afs_FindVCache*/
2530 * Find a vcache entry given a fid. Does a wildcard match on what we
2531 * have for the fid. If more than one entry, don't return anything.
2534 * avcp : Fill in pointer if we found one and only one.
2535 * afid : Pointer to the fid whose cache entry we desire.
2536 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2537 * unlock the vnode, and try again.
2538 * flags: bit 1 to specify whether to compute hit statistics. Not
2539 * set if FindVCache is called as part of internal bookkeeping.
2542 * Must be called with the afs_xvcache lock at least held at
2543 * the read level. In order to do the VLRU adjustment, the xvcache lock
2544 * must be shared-- we upgrade it here.
2547 * number of matches found.
2550 int afs_duplicate_nfs_fids=0;
2552 afs_int32 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2554 register struct vcache *tvc;
2556 afs_int32 count = 0;
2557 struct vcache *found_tvc = NULL;
2559 AFS_STATCNT(afs_FindVCache);
2561 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2565 ObtainSharedLock(&afs_xvcache,331);
2568 for(tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2569 /* Match only on what we have.... */
2570 if (((tvc->fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2571 && (tvc->fid.Fid.Volume == afid->Fid.Volume)
2572 && ((tvc->fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2573 && (tvc->fid.Cell == afid->Cell)) {
2575 /* Grab this vnode, possibly reactivating from the free list */
2578 vg = vget(AFSTOV(tvc));
2581 /* This vnode no longer exists. */
2584 #endif /* AFS_OSF_ENV */
2589 /* Drop our reference counts. */
2591 vrele(AFSTOV(found_tvc));
2593 afs_duplicate_nfs_fids++;
2594 ReleaseSharedLock(&afs_xvcache);
2602 /* should I have a read lock on the vnode here? */
2604 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2605 afs_int32 retry = 0;
2606 osi_vnhold(tvc, &retry);
2609 found_tvc = (struct vcache*)0;
2610 ReleaseSharedLock(&afs_xvcache);
2611 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2615 #if !defined(AFS_OSF_ENV)
2616 osi_vnhold(tvc, (int*)0); /* already held, above */
2620 * We obtained the xvcache lock above.
2622 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2623 refpanic ("FindVC VLRU inconsistent1");
2625 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2626 refpanic ("FindVC VLRU inconsistent1");
2628 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2629 refpanic ("FindVC VLRU inconsistent2");
2631 UpgradeSToWLock(&afs_xvcache,568);
2632 QRemove(&tvc->vlruq);
2633 QAdd(&VLRU, &tvc->vlruq);
2634 ConvertWToSLock(&afs_xvcache);
2635 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2636 refpanic ("FindVC VLRU inconsistent1");
2638 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2639 refpanic ("FindVC VLRU inconsistent2");
2641 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2642 refpanic ("FindVC VLRU inconsistent3");
2647 if (tvc) afs_stats_cmperf.vcacheHits++;
2648 else afs_stats_cmperf.vcacheMisses++;
2649 if (afs_IsPrimaryCellNum(afid->Cell))
2650 afs_stats_cmperf.vlocalAccesses++;
2652 afs_stats_cmperf.vremoteAccesses++;
2654 *avcp = tvc; /* May be null */
2656 ReleaseSharedLock(&afs_xvcache);
2657 return (tvc ? 1 : 0);
2659 } /*afs_NFSFindVCache*/
2667 * Initialize vcache related variables
2669 void afs_vcacheInit(int astatSize)
2671 register struct vcache *tvp;
2673 #if defined(AFS_OSF_ENV)
2674 if (!afs_maxvcount) {
2675 #if defined(AFS_OSF30_ENV)
2676 afs_maxvcount = max_vnodes/2; /* limit ourselves to half the total */
2678 afs_maxvcount = nvnode/2; /* limit ourselves to half the total */
2680 if (astatSize < afs_maxvcount) {
2681 afs_maxvcount = astatSize;
2684 #else /* AFS_OSF_ENV */
2688 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2689 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2691 #if !defined(AFS_OSF_ENV)
2692 /* Allocate and thread the struct vcache entries */
2693 tvp = (struct vcache *) afs_osi_Alloc(astatSize * sizeof(struct vcache));
2694 memset((char *)tvp, 0, sizeof(struct vcache)*astatSize);
2696 Initial_freeVCList = tvp;
2697 freeVCList = &(tvp[0]);
2698 for(i=0; i < astatSize-1; i++) {
2699 tvp[i].nextfree = &(tvp[i+1]);
2701 tvp[astatSize-1].nextfree = NULL;
2702 #ifdef KERNEL_HAVE_PIN
2703 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
2708 #if defined(AFS_SGI_ENV)
2709 for(i=0; i < astatSize; i++) {
2710 char name[METER_NAMSZ];
2711 struct vcache *tvc = &tvp[i];
2713 tvc->v.v_number = ++afsvnumbers;
2714 tvc->vc_rwlockid = OSI_NO_LOCKID;
2715 initnsema(&tvc->vc_rwlock, 1, makesname(name, "vrw", tvc->v.v_number));
2716 #ifndef AFS_SGI53_ENV
2717 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
2719 #ifndef AFS_SGI62_ENV
2720 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
2721 #endif /* AFS_SGI62_ENV */
2734 void shutdown_vcache(void)
2737 struct afs_cbr *tsp, *nsp;
2739 * XXX We may potentially miss some of the vcaches because if when there're no
2740 * free vcache entries and all the vcache entries are active ones then we allocate
2741 * an additional one - admittedly we almost never had that occur.
2743 #if !defined(AFS_OSF_ENV)
2744 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2746 #ifdef KERNEL_HAVE_PIN
2747 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2751 register struct afs_q *tq, *uq;
2752 register struct vcache *tvc;
2753 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
2757 osi_FreeSmallSpace(tvc->mvid);
2758 tvc->mvid = (struct VenusFid*)0;
2761 aix_gnode_rele(AFSTOV(tvc));
2763 if (tvc->linkData) {
2764 afs_osi_Free(tvc->linkData, strlen(tvc->linkData)+1);
2769 * Also free the remaining ones in the Cache
2771 for (i=0; i < VCSIZE; i++) {
2772 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2774 osi_FreeSmallSpace(tvc->mvid);
2775 tvc->mvid = (struct VenusFid*)0;
2779 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
2780 #ifdef AFS_AIX32_ENV
2783 vms_delete(tvc->segid);
2785 tvc->segid = tvc->vmh = NULL;
2786 if (VREFCOUNT(tvc)) osi_Panic("flushVcache: vm race");
2794 #if defined(AFS_SUN5_ENV)
2800 if (tvc->linkData) {
2801 afs_osi_Free(tvc->linkData, strlen(tvc->linkData)+1);
2805 afs_FreeAllAxs(&(tvc->Access));
2811 * Free any leftover callback queue
2813 for (tsp = afs_cbrSpace; tsp; tsp = nsp ) {
2815 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
2819 #if !defined(AFS_OSF_ENV)
2820 freeVCList = Initial_freeVCList = 0;
2822 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2823 LOCK_INIT(&afs_xvcb, "afs_xvcb");