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) {
796 code = afs_FlushVCache(tvc, &fv_slept);
803 continue; /* start over - may have raced. */
806 if (tq == uq ) break;
810 /* none free, making one is better than a panic */
811 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
812 tvc = (struct vcache *) afs_osi_Alloc(sizeof (struct vcache));
813 #ifdef KERNEL_HAVE_PIN
814 pin((char *)tvc, sizeof(struct vcache)); /* XXX */
817 /* In case it still comes here we need to fill this */
818 tvc->v.v_vm_info = VM_INFO_NULL;
819 vm_info_init(tvc->v.v_vm_info);
820 /* perhaps we should also do close_flush on non-NeXT mach systems;
821 * who knows; we don't currently have the sources.
823 #endif /* AFS_MACH_ENV */
824 #if defined(AFS_SGI_ENV)
825 { char name[METER_NAMSZ];
826 memset(tvc, 0, sizeof(struct vcache));
827 tvc->v.v_number = ++afsvnumbers;
828 tvc->vc_rwlockid = OSI_NO_LOCKID;
829 initnsema(&tvc->vc_rwlock, 1, makesname(name, "vrw", tvc->v.v_number));
830 #ifndef AFS_SGI53_ENV
831 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
833 #ifndef AFS_SGI62_ENV
834 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
837 #endif /* AFS_SGI_ENV */
840 tvc = freeVCList; /* take from free list */
841 freeVCList = tvc->nextfree;
842 tvc->nextfree = NULL;
844 #endif /* AFS_OSF_ENV */
847 vm_info_ptr = tvc->v.v_vm_info;
848 #endif /* AFS_MACH_ENV */
850 #if defined(AFS_OBSD_ENV)
852 panic("afs_NewVCache(): free vcache with vnode attached");
855 #if !defined(AFS_SGI_ENV) && !defined(AFS_OSF_ENV)
856 memset((char *)tvc, 0, sizeof(struct vcache));
861 RWLOCK_INIT(&tvc->lock, "vcache lock");
862 #if defined(AFS_SUN5_ENV)
863 RWLOCK_INIT(&tvc->vlock, "vcache vlock");
864 #endif /* defined(AFS_SUN5_ENV) */
867 tvc->v.v_vm_info = vm_info_ptr;
868 tvc->v.v_vm_info->pager = MEMORY_OBJECT_NULL;
869 #endif /* AFS_MACH_ENV */
871 afs_nbsd_getnewvnode(tvc); /* includes one refcount */
872 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
874 tvc->parentVnode = 0;
876 tvc->linkData = NULL;
879 tvc->execsOrWriters = 0;
883 tvc->last_looker = 0;
885 tvc->asynchrony = -1;
887 afs_symhint_inval(tvc);
889 tvc->flushDV.low = tvc->flushDV.high = AFS_MAXDV;
892 tvc->truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
893 hzero(tvc->m.DataVersion); /* in case we copy it into flushDV */
895 /* Hold it for the LRU (should make count 2) */
896 VN_HOLD(AFSTOV(tvc));
897 #else /* AFS_OSF_ENV */
899 VREFCOUNT_SET(tvc, 1); /* us */
900 #endif /* AFS_OBSD_ENV */
901 #endif /* AFS_OSF_ENV */
903 LOCK_INIT(&tvc->pvmlock, "vcache pvmlock");
904 tvc->vmh = tvc->segid = NULL;
907 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
908 #if defined(AFS_SUN5_ENV)
909 rw_init(&tvc->rwlock, "vcache rwlock", RW_DEFAULT, NULL);
911 #if defined(AFS_SUN55_ENV)
912 /* This is required if the kaio (kernel aynchronous io)
913 ** module is installed. Inside the kernel, the function
914 ** check_vp( common/os/aio.c) checks to see if the kernel has
915 ** to provide asynchronous io for this vnode. This
916 ** function extracts the device number by following the
917 ** v_data field of the vnode. If we do not set this field
918 ** then the system panics. The value of the v_data field
919 ** is not really important for AFS vnodes because the kernel
920 ** does not do asynchronous io for regular files. Hence,
921 ** for the time being, we fill up the v_data field with the
922 ** vnode pointer itself. */
923 tvc->v.v_data = (char *)tvc;
924 #endif /* AFS_SUN55_ENV */
926 afs_BozonInit(&tvc->pvnLock, tvc);
930 tvc->callback = serverp; /* to minimize chance that clear
932 /* initialize vnode data, note vrefCount is v.v_count */
934 /* Don't forget to free the gnode space */
935 tvc->v.v_gnode = gnodepnt = (struct gnode *) osi_AllocSmallSpace(sizeof(struct gnode));
936 memset((char *)gnodepnt, 0, sizeof(struct gnode));
939 memset((void*)&(tvc->vc_bhv_desc), 0, sizeof(tvc->vc_bhv_desc));
940 bhv_desc_init(&(tvc->vc_bhv_desc), tvc, tvc, &Afs_vnodeops);
942 vn_bhv_head_init(&(tvc->v.v_bh), "afsvp");
943 vn_bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
945 bhv_head_init(&(tvc->v.v_bh));
946 bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
949 tvc->v.v_mreg = tvc->v.v_mregb = (struct pregion*)tvc;
951 tvc->v.v_trace = ktrace_alloc(VNODE_TRACE_SIZE, 0);
953 init_bitlock(&tvc->v.v_pcacheflag, VNODE_PCACHE_LOCKBIT, "afs_pcache",
955 init_mutex(&tvc->v.v_filocksem, MUTEX_DEFAULT, "afsvfl", (long)tvc);
956 init_mutex(&tvc->v.v_buf_lock, MUTEX_DEFAULT, "afsvnbuf", (long)tvc);
958 vnode_pcache_init(&tvc->v);
959 #if defined(DEBUG) && defined(VNODE_INIT_BITLOCK)
960 /* Above define is never true execpt in SGI test kernels. */
961 init_bitlock(&(tvc->v.v_flag, VLOCK, "vnode", tvc->v.v_number);
964 AFS_VN_INIT_BUF_LOCK(&(tvc->v));
967 SetAfsVnode(AFSTOV(tvc));
968 #endif /* AFS_SGI64_ENV */
969 #ifdef AFS_DARWIN_ENV
970 tvc->v.v_ubcinfo = UBC_INFO_NULL;
971 lockinit(&tvc->rwlock, PINOD, "vcache rwlock", 0, 0);
972 cache_purge(AFSTOV(tvc));
975 /* VLISTNONE(&tvc->v); */
976 tvc->v.v_freelist.tqe_next=0;
977 tvc->v.v_freelist.tqe_prev=(struct vnode **)0xdeadb;
978 /*tvc->vrefCount++;*/
981 lockinit(&tvc->rwlock, PINOD, "vcache rwlock", 0, 0);
982 cache_purge(AFSTOV(tvc));
985 tvc->v.v_usecount++; /* steal an extra ref for now so vfree never happens */
986 /* This extra ref is dealt with above... */
989 * The proper value for mvstat (for root fids) is setup by the caller.
992 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
994 if (afs_globalVFS == 0) osi_Panic("afs globalvfs");
995 vSetVfsp(tvc, afs_globalVFS);
998 tvc->v.v_vfsnext = afs_globalVFS->vfs_vnodes; /* link off vfs */
999 tvc->v.v_vfsprev = NULL;
1000 afs_globalVFS->vfs_vnodes = &tvc->v;
1001 if (tvc->v.v_vfsnext != NULL)
1002 tvc->v.v_vfsnext->v_vfsprev = &tvc->v;
1003 tvc->v.v_next = gnodepnt->gn_vnode; /*Single vnode per gnode for us!*/
1004 gnodepnt->gn_vnode = &tvc->v;
1007 tvc->v.g_dev = ((struct mount *)afs_globalVFS->vfs_data)->m_dev;
1009 #if defined(AFS_DUX40_ENV)
1010 insmntque(tvc, afs_globalVFS, &afs_ubcops);
1013 /* Is this needed??? */
1014 insmntque(tvc, afs_globalVFS);
1015 #endif /* AFS_OSF_ENV */
1016 #endif /* AFS_DUX40_ENV */
1017 #if defined(AFS_SGI_ENV)
1018 VN_SET_DPAGES(&(tvc->v), (struct pfdat*)NULL);
1019 osi_Assert((tvc->v.v_flag & VINACT) == 0);
1021 osi_Assert(VN_GET_PGCNT(&(tvc->v)) == 0);
1022 osi_Assert(tvc->mapcnt == 0 && tvc->vc_locktrips == 0);
1023 osi_Assert(tvc->vc_rwlockid == OSI_NO_LOCKID);
1024 osi_Assert(tvc->v.v_filocks == NULL);
1025 #if !defined(AFS_SGI65_ENV)
1026 osi_Assert(tvc->v.v_filocksem == NULL);
1028 osi_Assert(tvc->cred == NULL);
1029 #ifdef AFS_SGI64_ENV
1030 vnode_pcache_reinit(&tvc->v);
1031 tvc->v.v_rdev = NODEV;
1033 vn_initlist((struct vnlist *)&tvc->v);
1035 #endif /* AFS_SGI_ENV */
1036 #if defined(AFS_LINUX22_ENV)
1038 struct inode *ip = AFSTOI(tvc);
1039 sema_init(&ip->i_sem, 1);
1040 #if defined(AFS_LINUX24_ENV)
1041 sema_init(&ip->i_zombie, 1);
1042 init_waitqueue_head(&ip->i_wait);
1043 spin_lock_init(&ip->i_data.i_shared_lock);
1044 #ifdef STRUCT_ADDRESS_SPACE_HAS_PAGE_LOCK
1045 spin_lock_init(&ip->i_data.page_lock);
1047 INIT_LIST_HEAD(&ip->i_data.clean_pages);
1048 INIT_LIST_HEAD(&ip->i_data.dirty_pages);
1049 INIT_LIST_HEAD(&ip->i_data.locked_pages);
1050 INIT_LIST_HEAD(&ip->i_dirty_buffers);
1051 #ifdef STRUCT_INODE_HAS_I_DIRTY_DATA_BUFFERS
1052 INIT_LIST_HEAD(&ip->i_dirty_data_buffers);
1054 #ifdef STRUCT_INODE_HAS_I_DEVICES
1055 INIT_LIST_HEAD(&ip->i_devices);
1057 ip->i_data.host = (void*) ip;
1058 #ifdef STRUCT_ADDRESS_SPACE_HAS_GFP_MASK
1059 ip->i_data.gfp_mask = GFP_HIGHUSER;
1061 ip->i_mapping = &ip->i_data;
1062 #ifdef STRUCT_INODE_HAS_I_TRUNCATE_SEM
1063 init_rwsem(&ip->i_truncate_sem);
1066 sema_init(&ip->i_atomic_write, 1);
1067 init_waitqueue(&ip->i_wait);
1069 INIT_LIST_HEAD(&ip->i_hash);
1070 INIT_LIST_HEAD(&ip->i_dentry);
1071 if (afs_globalVFS) {
1072 ip->i_dev = afs_globalVFS->s_dev;
1073 ip->i_sb = afs_globalVFS;
1078 osi_dnlc_purgedp(tvc); /* this may be overkill */
1079 memset((char *)&(tvc->quick), 0, sizeof(struct vtodc));
1080 memset((char *)&(tvc->callsort), 0, sizeof(struct afs_q));
1084 tvc->hnext = afs_vhashT[i];
1085 afs_vhashT[i] = tvc;
1086 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1087 refpanic ("NewVCache VLRU inconsistent");
1089 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
1090 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1091 refpanic ("NewVCache VLRU inconsistent2");
1093 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
1094 refpanic ("NewVCache VLRU inconsistent3");
1096 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
1097 refpanic ("NewVCache VLRU inconsistent4");
1107 * afs_FlushActiveVcaches
1113 * doflocks : Do we handle flocks?
1115 /* LOCK: afs_FlushActiveVcaches afs_xvcache N */
1116 void afs_FlushActiveVcaches(register afs_int32 doflocks)
1118 register struct vcache *tvc;
1120 register struct conn *tc;
1121 register afs_int32 code;
1122 register struct AFS_UCRED *cred = NULL;
1123 struct vrequest treq, ureq;
1124 struct AFSVolSync tsync;
1128 AFS_STATCNT(afs_FlushActiveVcaches);
1129 ObtainReadLock(&afs_xvcache);
1130 for(i=0;i<VCSIZE;i++) {
1131 for(tvc = afs_vhashT[i]; tvc; tvc=tvc->hnext) {
1132 if (doflocks && tvc->flockCount != 0) {
1133 /* if this entry has an flock, send a keep-alive call out */
1135 ReleaseReadLock(&afs_xvcache);
1136 ObtainWriteLock(&tvc->lock,51);
1138 afs_InitReq(&treq, &afs_osi_cred);
1139 treq.flags |= O_NONBLOCK;
1141 tc = afs_Conn(&tvc->fid, &treq, SHARED_LOCK);
1143 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
1146 RXAFS_ExtendLock(tc->id,
1147 (struct AFSFid *) &tvc->fid.Fid,
1154 (afs_Analyze(tc, code, &tvc->fid, &treq,
1155 AFS_STATS_FS_RPCIDX_EXTENDLOCK,
1156 SHARED_LOCK, NULL));
1158 ReleaseWriteLock(&tvc->lock);
1159 ObtainReadLock(&afs_xvcache);
1163 if ((tvc->states & CCore) || (tvc->states & CUnlinkedDel)) {
1165 * Don't let it evaporate in case someone else is in
1166 * this code. Also, drop the afs_xvcache lock while
1167 * getting vcache locks.
1170 ReleaseReadLock(&afs_xvcache);
1171 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
1172 afs_BozonLock(&tvc->pvnLock, tvc);
1174 #if defined(AFS_SGI_ENV)
1176 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1178 osi_Assert(VREFCOUNT(tvc) > 0);
1179 AFS_RWLOCK((vnode_t *)tvc, VRWLOCK_WRITE);
1181 ObtainWriteLock(&tvc->lock,52);
1182 if (tvc->states & CCore) {
1183 tvc->states &= ~CCore;
1184 /* XXXX Find better place-holder for cred XXXX */
1185 cred = (struct AFS_UCRED *) tvc->linkData;
1186 tvc->linkData = NULL; /* XXX */
1187 afs_InitReq(&ureq, cred);
1188 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1189 ICL_TYPE_POINTER, tvc,
1190 ICL_TYPE_INT32, tvc->execsOrWriters);
1191 code = afs_StoreOnLastReference(tvc, &ureq);
1192 ReleaseWriteLock(&tvc->lock);
1193 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
1194 afs_BozonUnlock(&tvc->pvnLock, tvc);
1196 hzero(tvc->flushDV);
1199 if (code && code != VNOVNODE) {
1200 afs_StoreWarn(code, tvc->fid.Fid.Volume,
1201 /* /dev/console */ 1);
1203 } else if (tvc->states & CUnlinkedDel) {
1207 ReleaseWriteLock(&tvc->lock);
1208 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
1209 afs_BozonUnlock(&tvc->pvnLock, tvc);
1211 #if defined(AFS_SGI_ENV)
1212 AFS_RWUNLOCK((vnode_t *)tvc, VRWLOCK_WRITE);
1214 afs_remunlink(tvc, 0);
1215 #if defined(AFS_SGI_ENV)
1216 AFS_RWLOCK((vnode_t *)tvc, VRWLOCK_WRITE);
1220 /* lost (or won, perhaps) the race condition */
1221 ReleaseWriteLock(&tvc->lock);
1222 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
1223 afs_BozonUnlock(&tvc->pvnLock, tvc);
1226 #if defined(AFS_SGI_ENV)
1227 AFS_RWUNLOCK((vnode_t *)tvc, VRWLOCK_WRITE);
1229 ObtainReadLock(&afs_xvcache);
1235 AFS_RELE(AFSTOV(tvc));
1237 /* Matches write code setting CCore flag */
1241 #ifdef AFS_DARWIN_ENV
1242 if (VREFCOUNT(tvc) == 1 && UBCINFOEXISTS(&tvc->v)) {
1243 if (tvc->opens) panic("flushactive open, hasubc, but refcnt 1");
1244 osi_VM_TryReclaim(tvc,0);
1249 ReleaseReadLock(&afs_xvcache);
1257 * Make sure a cache entry is up-to-date status-wise.
1259 * NOTE: everywhere that calls this can potentially be sped up
1260 * by checking CStatd first, and avoiding doing the InitReq
1261 * if this is up-to-date.
1263 * Anymore, the only places that call this KNOW already that the
1264 * vcache is not up-to-date, so we don't screw around.
1267 * avc : Ptr to vcache entry to verify.
1271 int afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1273 register struct vcache *tvc;
1275 AFS_STATCNT(afs_VerifyVCache);
1277 #if defined(AFS_OSF_ENV)
1278 ObtainReadLock(&avc->lock);
1279 if (afs_IsWired(avc)) {
1280 ReleaseReadLock(&avc->lock);
1283 ReleaseReadLock(&avc->lock);
1284 #endif /* AFS_OSF_ENV */
1285 /* otherwise we must fetch the status info */
1287 ObtainWriteLock(&avc->lock,53);
1288 if (avc->states & CStatd) {
1289 ReleaseWriteLock(&avc->lock);
1292 ObtainWriteLock(&afs_xcbhash, 461);
1293 avc->states &= ~( CStatd | CUnique );
1294 avc->callback = NULL;
1295 afs_DequeueCallback(avc);
1296 ReleaseWriteLock(&afs_xcbhash);
1297 ReleaseWriteLock(&avc->lock);
1299 /* since we've been called back, or the callback has expired,
1300 * it's possible that the contents of this directory, or this
1301 * file's name have changed, thus invalidating the dnlc contents.
1303 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1304 osi_dnlc_purgedp (avc);
1306 osi_dnlc_purgevp (avc);
1308 /* fetch the status info */
1309 tvc = afs_GetVCache(&avc->fid, areq, NULL, avc);
1310 if (!tvc) return ENOENT;
1311 /* Put it back; caller has already incremented vrefCount */
1315 } /*afs_VerifyVCache*/
1322 * Simple copy of stat info into cache.
1325 * avc : Ptr to vcache entry involved.
1326 * astat : Ptr to stat info to copy.
1329 * Nothing interesting.
1331 * Callers: as of 1992-04-29, only called by WriteVCache
1333 static void afs_SimpleVStat(register struct vcache *avc,
1334 register struct AFSFetchStatus *astat, struct vrequest *areq)
1337 AFS_STATCNT(afs_SimpleVStat);
1340 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1341 && !AFS_VN_MAPPED((vnode_t*)avc)) {
1343 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1345 #ifdef AFS_64BIT_ClIENT
1346 FillInt64(length, astat->Length_hi, astat->Length);
1347 #else /* AFS_64BIT_CLIENT */
1348 length = astat->Length;
1349 #endif /* AFS_64BIT_CLIENT */
1350 #if defined(AFS_SGI_ENV)
1351 osi_Assert((valusema(&avc->vc_rwlock) <= 0) &&
1352 (OSI_GET_LOCKID() == avc->vc_rwlockid));
1353 if (length < avc->m.Length) {
1354 vnode_t *vp = (vnode_t *)avc;
1356 osi_Assert(WriteLocked(&avc->lock));
1357 ReleaseWriteLock(&avc->lock);
1359 PTOSSVP(vp, (off_t)length, (off_t)MAXLONG);
1361 ObtainWriteLock(&avc->lock,67);
1364 /* if writing the file, don't fetch over this value */
1365 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT,
1366 ICL_TYPE_POINTER, avc,
1367 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1368 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1369 avc->m.Length = length;
1370 avc->m.Date = astat->ClientModTime;
1372 avc->m.Owner = astat->Owner;
1373 avc->m.Group = astat->Group;
1374 avc->m.Mode = astat->UnixModeBits;
1375 if (vType(avc) == VREG) {
1376 avc->m.Mode |= S_IFREG;
1378 else if (vType(avc) == VDIR) {
1379 avc->m.Mode |= S_IFDIR;
1381 else if (vType(avc) == VLNK) {
1382 avc->m.Mode |= S_IFLNK;
1383 if ((avc->m.Mode & 0111) == 0) avc->mvstat = 1;
1385 if (avc->states & CForeign) {
1386 struct axscache *ac;
1387 avc->anyAccess = astat->AnonymousAccess;
1389 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1391 * Caller has at least one bit not covered by anonymous, and
1392 * thus may have interesting rights.
1394 * HOWEVER, this is a really bad idea, because any access query
1395 * for bits which aren't covered by anonymous, on behalf of a user
1396 * who doesn't have any special rights, will result in an answer of
1397 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1398 * It's an especially bad idea under Ultrix, since (due to the lack of
1399 * a proper access() call) it must perform several afs_access() calls
1400 * in order to create magic mode bits that vary according to who makes
1401 * the call. In other words, _every_ stat() generates a test for
1404 #endif /* badidea */
1405 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1406 ac->axess = astat->CallerAccess;
1407 else /* not found, add a new one if possible */
1408 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1412 } /*afs_SimpleVStat*/
1419 * Store the status info *only* back to the server for a
1423 * avc : Ptr to the vcache entry.
1424 * astatus : Ptr to the status info to store.
1425 * areq : Ptr to the associated vrequest.
1428 * Must be called with a shared lock held on the vnode.
1431 int afs_WriteVCache(register struct vcache *avc,
1432 register struct AFSStoreStatus *astatus, struct vrequest *areq)
1436 struct AFSFetchStatus OutStatus;
1437 struct AFSVolSync tsync;
1440 AFS_STATCNT(afs_WriteVCache);
1441 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1442 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
1445 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
1447 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1449 code = RXAFS_StoreStatus(tc->id,
1450 (struct AFSFid *) &avc->fid.Fid,
1451 astatus, &OutStatus, &tsync);
1457 (afs_Analyze(tc, code, &avc->fid, areq,
1458 AFS_STATS_FS_RPCIDX_STORESTATUS,
1459 SHARED_LOCK, NULL));
1461 UpgradeSToWLock(&avc->lock,20);
1463 /* success, do the changes locally */
1464 afs_SimpleVStat(avc, &OutStatus, areq);
1466 * Update the date, too. SimpleVStat didn't do this, since
1467 * it thought we were doing this after fetching new status
1468 * over a file being written.
1470 avc->m.Date = OutStatus.ClientModTime;
1473 /* failure, set up to check with server next time */
1474 ObtainWriteLock(&afs_xcbhash, 462);
1475 afs_DequeueCallback(avc);
1476 avc->states &= ~( CStatd | CUnique); /* turn off stat valid flag */
1477 ReleaseWriteLock(&afs_xcbhash);
1478 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1479 osi_dnlc_purgedp (avc); /* if it (could be) a directory */
1481 ConvertWToSLock(&avc->lock);
1484 } /*afs_WriteVCache*/
1490 * Copy astat block into vcache info
1493 * avc : Ptr to vcache entry.
1494 * astat : Ptr to stat block to copy in.
1495 * areq : Ptr to associated request.
1498 * Must be called under a write lock
1500 * Note: this code may get dataversion and length out of sync if the file has
1501 * been modified. This is less than ideal. I haven't thought about
1502 * it sufficiently to be certain that it is adequate.
1504 void afs_ProcessFS(register struct vcache *avc, register struct AFSFetchStatus *astat,
1505 struct vrequest *areq)
1508 AFS_STATCNT(afs_ProcessFS);
1510 #ifdef AFS_64BIT_CLIENT
1511 FillInt64(length, astat->Length_hi, astat->Length);
1512 #else /* AFS_64BIT_CLIENT */
1513 length = astat->Length;
1514 #endif /* AFS_64BIT_CLIENT */
1515 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1516 * number for each bulk status request. Under no circumstances
1517 * should afs_DoBulkStat store a sequence number if the new
1518 * length will be ignored when afs_ProcessFS is called with
1519 * new stats. If you change the following conditional then you
1520 * also need to change the conditional in afs_DoBulkStat. */
1522 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1523 && !AFS_VN_MAPPED((vnode_t*)avc)) {
1525 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1527 /* if we're writing or mapping this file, don't fetch over these
1530 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1531 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1532 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1533 avc->m.Length = length;
1534 avc->m.Date = astat->ClientModTime;
1536 hset64(avc->m.DataVersion, astat->dataVersionHigh, astat->DataVersion);
1537 avc->m.Owner = astat->Owner;
1538 avc->m.Mode = astat->UnixModeBits;
1539 avc->m.Group = astat->Group;
1540 avc->m.LinkCount = astat->LinkCount;
1541 if (astat->FileType == File) {
1542 vSetType(avc, VREG);
1543 avc->m.Mode |= S_IFREG;
1545 else if (astat->FileType == Directory) {
1546 vSetType(avc, VDIR);
1547 avc->m.Mode |= S_IFDIR;
1549 else if (astat->FileType == SymbolicLink) {
1550 if (afs_fakestat_enable && (avc->m.Mode & 0111) == 0) {
1551 vSetType(avc, VDIR);
1552 avc->m.Mode |= S_IFDIR;
1554 vSetType(avc, VLNK);
1555 avc->m.Mode |= S_IFLNK;
1557 if ((avc->m.Mode & 0111) == 0) {
1561 avc->anyAccess = astat->AnonymousAccess;
1563 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1565 * Caller has at least one bit not covered by anonymous, and
1566 * thus may have interesting rights.
1568 * HOWEVER, this is a really bad idea, because any access query
1569 * for bits which aren't covered by anonymous, on behalf of a user
1570 * who doesn't have any special rights, will result in an answer of
1571 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1572 * It's an especially bad idea under Ultrix, since (due to the lack of
1573 * a proper access() call) it must perform several afs_access() calls
1574 * in order to create magic mode bits that vary according to who makes
1575 * the call. In other words, _every_ stat() generates a test for
1578 #endif /* badidea */
1580 struct axscache *ac;
1581 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1582 ac->axess = astat->CallerAccess;
1583 else /* not found, add a new one if possible */
1584 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1587 #ifdef AFS_LINUX22_ENV
1588 vcache2inode(avc); /* Set the inode attr cache */
1590 #ifdef AFS_DARWIN_ENV
1591 osi_VM_Setup(avc,1);
1597 int afs_RemoteLookup(register struct VenusFid *afid, struct vrequest *areq,
1598 char *name, struct VenusFid *nfid, struct AFSFetchStatus *OutStatusp,
1599 struct AFSCallBack *CallBackp, struct server **serverp, struct AFSVolSync *tsyncp)
1603 register struct conn *tc;
1604 struct AFSFetchStatus OutDirStatus;
1607 if (!name) name = ""; /* XXX */
1609 tc = afs_Conn(afid, areq, SHARED_LOCK);
1611 if (serverp) *serverp = tc->srvr->server;
1613 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1615 code = RXAFS_Lookup(tc->id, (struct AFSFid *) &afid->Fid, name,
1616 (struct AFSFid *) &nfid->Fid,
1617 OutStatusp, &OutDirStatus, CallBackp, tsyncp);
1623 (afs_Analyze(tc, code, afid, areq,
1624 AFS_STATS_FS_RPCIDX_XLOOKUP,
1625 SHARED_LOCK, NULL));
1635 * Given a file id and a vrequest structure, fetch the status
1636 * information associated with the file.
1640 * areq : Ptr to associated vrequest structure, specifying the
1641 * user whose authentication tokens will be used.
1642 * avc : caller may already have a vcache for this file, which is
1646 * The cache entry is returned with an increased vrefCount field.
1647 * The entry must be discarded by calling afs_PutVCache when you
1648 * are through using the pointer to the cache entry.
1650 * You should not hold any locks when calling this function, except
1651 * locks on other vcache entries. If you lock more than one vcache
1652 * entry simultaneously, you should lock them in this order:
1654 * 1. Lock all files first, then directories.
1655 * 2. Within a particular type, lock entries in Fid.Vnode order.
1657 * This locking hierarchy is convenient because it allows locking
1658 * of a parent dir cache entry, given a file (to check its access
1659 * control list). It also allows renames to be handled easily by
1660 * locking directories in a constant order.
1661 * NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1663 /* might have a vcache structure already, which must
1664 * already be held by the caller */
1666 struct vcache *afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
1667 afs_int32 *cached, struct vcache *avc)
1670 afs_int32 code, newvcache=0;
1671 register struct vcache *tvc;
1675 AFS_STATCNT(afs_GetVCache);
1677 if (cached) *cached = 0; /* Init just in case */
1679 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1683 ObtainSharedLock(&afs_xvcache,5);
1685 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU );
1687 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1688 ReleaseSharedLock(&afs_xvcache);
1689 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1697 if (tvc->states & CStatd) {
1698 ReleaseSharedLock(&afs_xvcache);
1703 UpgradeSToWLock(&afs_xvcache,21);
1705 /* no cache entry, better grab one */
1706 tvc = afs_NewVCache(afid, NULL);
1709 ConvertWToSLock(&afs_xvcache);
1710 afs_stats_cmperf.vcacheMisses++;
1713 ReleaseSharedLock(&afs_xvcache);
1715 ObtainWriteLock(&tvc->lock,54);
1717 if (tvc->states & CStatd) {
1718 #ifdef AFS_LINUX22_ENV
1721 ReleaseWriteLock(&tvc->lock);
1722 #ifdef AFS_DARWIN_ENV
1723 osi_VM_Setup(tvc,0);
1728 #if defined(AFS_OSF_ENV)
1729 if (afs_IsWired(tvc)) {
1730 ReleaseWriteLock(&tvc->lock);
1733 #endif /* AFS_OSF_ENV */
1735 VOP_LOCK(AFSTOV(tvc), LK_EXCLUSIVE | LK_RETRY, curproc);
1736 uvm_vnp_uncache(AFSTOV(tvc));
1737 VOP_UNLOCK(AFSTOV(tvc), 0, curproc);
1740 ObtainWriteLock(&afs_xcbhash, 464);
1741 tvc->states &= ~CUnique;
1743 afs_DequeueCallback(tvc);
1744 ReleaseWriteLock(&afs_xcbhash);
1746 /* It is always appropriate to throw away all the access rights? */
1747 afs_FreeAllAxs(&(tvc->Access));
1748 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1750 if ((tvp->states & VForeign)) {
1751 if (newvcache) tvc->states |= CForeign;
1752 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1753 && (tvp->rootUnique == afid->Fid.Unique)) {
1757 if (tvp->states & VRO) tvc->states |= CRO;
1758 if (tvp->states & VBackup) tvc->states |= CBackup;
1759 /* now copy ".." entry back out of volume structure, if necessary */
1760 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1762 tvc->mvid = (struct VenusFid *)
1763 osi_AllocSmallSpace(sizeof(struct VenusFid));
1764 *tvc->mvid = tvp->dotdot;
1766 afs_PutVolume(tvp, READ_LOCK);
1770 afs_RemoveVCB(afid);
1772 struct AFSFetchStatus OutStatus;
1774 if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1775 afs_ProcessFS(tvc, &OutStatus, areq);
1776 tvc->states |= CStatd | CUnique;
1779 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1784 ReleaseWriteLock(&tvc->lock);
1786 ObtainReadLock(&afs_xvcache);
1788 ReleaseReadLock(&afs_xvcache);
1792 ReleaseWriteLock(&tvc->lock);
1799 struct vcache *afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1800 afs_int32 *cached, struct vcache *adp, char *aname)
1802 afs_int32 code, now, newvcache=0;
1803 struct VenusFid nfid;
1804 register struct vcache *tvc;
1806 struct AFSFetchStatus OutStatus;
1807 struct AFSCallBack CallBack;
1808 struct AFSVolSync tsync;
1809 struct server *serverp = 0;
1813 AFS_STATCNT(afs_GetVCache);
1814 if (cached) *cached = 0; /* Init just in case */
1816 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1820 ObtainReadLock(&afs_xvcache);
1821 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */);
1824 ReleaseReadLock(&afs_xvcache);
1826 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1827 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1831 ObtainReadLock(&tvc->lock);
1833 if (tvc->states & CStatd) {
1837 ReleaseReadLock(&tvc->lock);
1840 tvc->states &= ~CUnique;
1842 ReleaseReadLock(&tvc->lock);
1843 ObtainReadLock(&afs_xvcache);
1847 ReleaseReadLock(&afs_xvcache);
1849 /* lookup the file */
1852 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
1853 code = afs_RemoteLookup(&adp->fid, areq, aname, &nfid, &OutStatus, &CallBack,
1856 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1860 ObtainSharedLock(&afs_xvcache,6);
1861 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU /* no xstats now*/);
1863 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1864 ReleaseSharedLock(&afs_xvcache);
1865 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1871 /* no cache entry, better grab one */
1872 UpgradeSToWLock(&afs_xvcache,22);
1873 tvc = afs_NewVCache(&nfid, NULL);
1875 ConvertWToSLock(&afs_xvcache);
1878 ReleaseSharedLock(&afs_xvcache);
1879 ObtainWriteLock(&tvc->lock,55);
1881 /* It is always appropriate to throw away all the access rights? */
1882 afs_FreeAllAxs(&(tvc->Access));
1883 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
1885 if ((tvp->states & VForeign)) {
1886 if (newvcache) tvc->states |= CForeign;
1887 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1888 && (tvp->rootUnique == afid->Fid.Unique))
1891 if (tvp->states & VRO) tvc->states |= CRO;
1892 if (tvp->states & VBackup) tvc->states |= CBackup;
1893 /* now copy ".." entry back out of volume structure, if necessary */
1894 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1896 tvc->mvid = (struct VenusFid *)
1897 osi_AllocSmallSpace(sizeof(struct VenusFid));
1898 *tvc->mvid = tvp->dotdot;
1903 ObtainWriteLock(&afs_xcbhash, 465);
1904 afs_DequeueCallback(tvc);
1905 tvc->states &= ~( CStatd | CUnique );
1906 ReleaseWriteLock(&afs_xcbhash);
1907 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1908 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
1910 afs_PutVolume(tvp, READ_LOCK);
1911 ReleaseWriteLock(&tvc->lock);
1912 ObtainReadLock(&afs_xvcache);
1914 ReleaseReadLock(&afs_xvcache);
1918 ObtainWriteLock(&afs_xcbhash, 466);
1919 if (origCBs == afs_allCBs) {
1920 if (CallBack.ExpirationTime) {
1921 tvc->callback = serverp;
1922 tvc->cbExpires = CallBack.ExpirationTime+now;
1923 tvc->states |= CStatd | CUnique;
1924 tvc->states &= ~CBulkFetching;
1925 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
1926 } else if (tvc->states & CRO) {
1927 /* adapt gives us an hour. */
1928 tvc->cbExpires = 3600+osi_Time(); /*XXX*/
1929 tvc->states |= CStatd | CUnique;
1930 tvc->states &= ~CBulkFetching;
1931 afs_QueueCallback(tvc, CBHash(3600), tvp);
1933 tvc->callback = NULL;
1934 afs_DequeueCallback(tvc);
1935 tvc->states &= ~(CStatd | CUnique);
1936 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1937 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
1940 afs_DequeueCallback(tvc);
1941 tvc->states &= ~CStatd;
1942 tvc->states &= ~CUnique;
1943 tvc->callback = NULL;
1944 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1945 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
1947 ReleaseWriteLock(&afs_xcbhash);
1949 afs_PutVolume(tvp, READ_LOCK);
1950 afs_ProcessFS(tvc, &OutStatus, areq);
1952 ReleaseWriteLock(&tvc->lock);
1957 struct vcache *afs_GetRootVCache(struct VenusFid *afid,
1958 struct vrequest *areq, afs_int32 *cached,
1959 struct volume *tvolp)
1961 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
1962 afs_int32 getNewFid = 0;
1964 struct VenusFid nfid;
1965 register struct vcache *tvc;
1966 struct server *serverp = 0;
1967 struct AFSFetchStatus OutStatus;
1968 struct AFSCallBack CallBack;
1969 struct AFSVolSync tsync;
1975 if (!tvolp->rootVnode || getNewFid) {
1976 struct VenusFid tfid;
1979 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
1980 origCBs = afs_allCBs; /* ignore InitCallBackState */
1981 code = afs_RemoteLookup(&tfid, areq, NULL, &nfid,
1982 &OutStatus, &CallBack, &serverp, &tsync);
1986 /* ReleaseReadLock(&tvolp->lock); */
1987 ObtainWriteLock(&tvolp->lock,56);
1988 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
1989 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
1990 ReleaseWriteLock(&tvolp->lock);
1991 /* ObtainReadLock(&tvolp->lock);*/
1994 afid->Fid.Vnode = tvolp->rootVnode;
1995 afid->Fid.Unique = tvolp->rootUnique;
1998 ObtainSharedLock(&afs_xvcache,7);
2000 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2001 if (!FidCmp(&(tvc->fid), afid)) {
2003 /* Grab this vnode, possibly reactivating from the free list */
2004 /* for the present (95.05.25) everything on the hash table is
2005 * definitively NOT in the free list -- at least until afs_reclaim
2006 * can be safely implemented */
2009 vg = vget(AFSTOV(tvc)); /* this bumps ref count */
2013 #endif /* AFS_OSF_ENV */
2018 if (!haveStatus && (!tvc || !(tvc->states & CStatd))) {
2019 /* Mount point no longer stat'd or unknown. FID may have changed. */
2022 AFS_RELE(AFSTOV(tvc));
2026 ReleaseSharedLock(&afs_xvcache);
2031 UpgradeSToWLock(&afs_xvcache,23);
2032 /* no cache entry, better grab one */
2033 tvc = afs_NewVCache(afid, NULL);
2035 afs_stats_cmperf.vcacheMisses++;
2038 if (cached) *cached = 1;
2039 afs_stats_cmperf.vcacheHits++;
2041 /* we already bumped the ref count in the for loop above */
2042 #else /* AFS_OSF_ENV */
2045 UpgradeSToWLock(&afs_xvcache,24);
2046 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2047 refpanic ("GRVC VLRU inconsistent0");
2049 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2050 refpanic ("GRVC VLRU inconsistent1");
2052 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2053 refpanic ("GRVC VLRU inconsistent2");
2055 QRemove(&tvc->vlruq); /* move to lruq head */
2056 QAdd(&VLRU, &tvc->vlruq);
2057 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2058 refpanic ("GRVC VLRU inconsistent3");
2060 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2061 refpanic ("GRVC VLRU inconsistent4");
2063 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2064 refpanic ("GRVC VLRU inconsistent5");
2069 ReleaseWriteLock(&afs_xvcache);
2071 if (tvc->states & CStatd) {
2075 ObtainReadLock(&tvc->lock);
2076 tvc->states &= ~CUnique;
2077 tvc->callback = NULL; /* redundant, perhaps */
2078 ReleaseReadLock(&tvc->lock);
2081 ObtainWriteLock(&tvc->lock,57);
2083 /* It is always appropriate to throw away all the access rights? */
2084 afs_FreeAllAxs(&(tvc->Access));
2086 if (newvcache) tvc->states |= CForeign;
2087 if (tvolp->states & VRO) tvc->states |= CRO;
2088 if (tvolp->states & VBackup) tvc->states |= CBackup;
2089 /* now copy ".." entry back out of volume structure, if necessary */
2090 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2091 && (tvolp->rootUnique == afid->Fid.Unique)) {
2094 if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2096 tvc->mvid = (struct VenusFid *)osi_AllocSmallSpace(sizeof(struct VenusFid));
2097 *tvc->mvid = tvolp->dotdot;
2101 afs_RemoveVCB(afid);
2104 struct VenusFid tfid;
2107 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2108 origCBs = afs_allCBs; /* ignore InitCallBackState */
2109 code = afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus,
2110 &CallBack, &serverp, &tsync);
2114 ObtainWriteLock(&afs_xcbhash, 467);
2115 afs_DequeueCallback(tvc);
2116 tvc->callback = NULL;
2117 tvc->states &= ~(CStatd|CUnique);
2118 ReleaseWriteLock(&afs_xcbhash);
2119 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2120 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
2121 ReleaseWriteLock(&tvc->lock);
2122 ObtainReadLock(&afs_xvcache);
2124 ReleaseReadLock(&afs_xvcache);
2128 ObtainWriteLock(&afs_xcbhash, 468);
2129 if (origCBs == afs_allCBs) {
2130 tvc->states |= CTruth;
2131 tvc->callback = serverp;
2132 if (CallBack.ExpirationTime != 0) {
2133 tvc->cbExpires = CallBack.ExpirationTime+start;
2134 tvc->states |= CStatd;
2135 tvc->states &= ~CBulkFetching;
2136 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2137 } else if (tvc->states & CRO) {
2138 /* adapt gives us an hour. */
2139 tvc->cbExpires = 3600+osi_Time(); /*XXX*/
2140 tvc->states |= CStatd;
2141 tvc->states &= ~CBulkFetching;
2142 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2145 afs_DequeueCallback(tvc);
2146 tvc->callback = NULL;
2147 tvc->states &= ~(CStatd | CUnique);
2148 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2149 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
2151 ReleaseWriteLock(&afs_xcbhash);
2152 afs_ProcessFS(tvc, &OutStatus, areq);
2154 ReleaseWriteLock(&tvc->lock);
2161 * must be called with avc write-locked
2162 * don't absolutely have to invalidate the hint unless the dv has
2163 * changed, but be sure to get it right else there will be consistency bugs.
2165 afs_int32 afs_FetchStatus(struct vcache *avc, struct VenusFid *afid,
2166 struct vrequest *areq, struct AFSFetchStatus *Outsp)
2169 afs_uint32 start = 0;
2170 register struct conn *tc;
2171 struct AFSCallBack CallBack;
2172 struct AFSVolSync tsync;
2173 struct volume* volp;
2177 tc = afs_Conn(afid, areq, SHARED_LOCK);
2178 avc->quick.stamp = 0; avc->h1.dchint = NULL; /* invalidate hints */
2180 avc->callback = tc->srvr->server;
2182 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2184 code = RXAFS_FetchStatus(tc->id,
2185 (struct AFSFid *) &afid->Fid,
2186 Outsp, &CallBack, &tsync);
2194 (afs_Analyze(tc, code, afid, areq,
2195 AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2196 SHARED_LOCK, NULL));
2199 afs_ProcessFS(avc, Outsp, areq);
2200 volp = afs_GetVolume(afid, areq, READ_LOCK);
2201 ObtainWriteLock(&afs_xcbhash, 469);
2202 avc->states |= CTruth;
2203 if (avc->callback /* check for race */) {
2204 if (CallBack.ExpirationTime != 0) {
2205 avc->cbExpires = CallBack.ExpirationTime+start;
2206 avc->states |= CStatd;
2207 avc->states &= ~CBulkFetching;
2208 afs_QueueCallback(avc, CBHash(CallBack.ExpirationTime), volp);
2210 else if (avc->states & CRO)
2211 { /* ordinary callback on a read-only volume -- AFS 3.2 style */
2212 avc->cbExpires = 3600+start;
2213 avc->states |= CStatd;
2214 avc->states &= ~CBulkFetching;
2215 afs_QueueCallback(avc, CBHash(3600), volp);
2218 afs_DequeueCallback(avc);
2219 avc->callback = NULL;
2220 avc->states &= ~(CStatd|CUnique);
2221 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2222 osi_dnlc_purgedp (avc); /* if it (could be) a directory */
2226 afs_DequeueCallback(avc);
2227 avc->callback = NULL;
2228 avc->states &= ~(CStatd|CUnique);
2229 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2230 osi_dnlc_purgedp (avc); /* if it (could be) a directory */
2232 ReleaseWriteLock(&afs_xcbhash);
2234 afs_PutVolume(volp, READ_LOCK);
2237 /* used to undo the local callback, but that's too extreme.
2238 * There are plenty of good reasons that fetchstatus might return
2239 * an error, such as EPERM. If we have the vnode cached, statd,
2240 * with callback, might as well keep track of the fact that we
2241 * don't have access...
2243 if (code == EPERM || code == EACCES) {
2244 struct axscache *ac;
2245 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2247 else /* not found, add a new one if possible */
2248 afs_AddAxs(avc->Access, areq->uid, 0);
2259 * Stuff some information into the vcache for the given file.
2262 * afid : File in question.
2263 * OutStatus : Fetch status on the file.
2264 * CallBack : Callback info.
2265 * tc : RPC connection involved.
2266 * areq : vrequest involved.
2269 * Nothing interesting.
2271 void afs_StuffVcache(register struct VenusFid *afid,
2272 struct AFSFetchStatus *OutStatus, struct AFSCallBack *CallBack,
2273 register struct conn *tc, struct vrequest *areq)
2275 register afs_int32 code, i, newvcache=0;
2276 register struct vcache *tvc;
2277 struct AFSVolSync tsync;
2279 struct axscache *ac;
2282 AFS_STATCNT(afs_StuffVcache);
2283 #ifdef IFS_VCACHECOUNT
2288 ObtainSharedLock(&afs_xvcache,8);
2290 tvc = afs_FindVCache(afid, &retry, DO_VLRU /* no stats */);
2292 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2293 ReleaseSharedLock(&afs_xvcache);
2294 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2300 /* no cache entry, better grab one */
2301 UpgradeSToWLock(&afs_xvcache,25);
2302 tvc = afs_NewVCache(afid, NULL);
2304 ConvertWToSLock(&afs_xvcache);
2307 ReleaseSharedLock(&afs_xvcache);
2308 ObtainWriteLock(&tvc->lock,58);
2310 tvc->states &= ~CStatd;
2311 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2312 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
2314 /* Is it always appropriate to throw away all the access rights? */
2315 afs_FreeAllAxs(&(tvc->Access));
2317 /*Copy useful per-volume info*/
2318 tvp = afs_GetVolume(afid, areq, READ_LOCK);
2320 if (newvcache && (tvp->states & VForeign)) tvc->states |= CForeign;
2321 if (tvp->states & VRO) tvc->states |= CRO;
2322 if (tvp->states & VBackup) tvc->states |= CBackup;
2324 * Now, copy ".." entry back out of volume structure, if
2327 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2328 if (!tvc->mvid) tvc->mvid =
2329 (struct VenusFid *) osi_AllocSmallSpace(sizeof(struct VenusFid));
2330 *tvc->mvid = tvp->dotdot;
2333 /* store the stat on the file */
2334 afs_RemoveVCB(afid);
2335 afs_ProcessFS(tvc, OutStatus, areq);
2336 tvc->callback = tc->srvr->server;
2338 /* we use osi_Time twice below. Ideally, we would use the time at which
2339 * the FetchStatus call began, instead, but we don't have it here. So we
2340 * make do with "now". In the CRO case, it doesn't really matter. In
2341 * the other case, we hope that the difference between "now" and when the
2342 * call actually began execution on the server won't be larger than the
2343 * padding which the server keeps. Subtract 1 second anyway, to be on
2344 * the safe side. Can't subtract more because we don't know how big
2345 * ExpirationTime is. Possible consistency problems may arise if the call
2346 * timeout period becomes longer than the server's expiration padding. */
2347 ObtainWriteLock(&afs_xcbhash, 470);
2348 if (CallBack->ExpirationTime != 0) {
2349 tvc->cbExpires = CallBack->ExpirationTime+osi_Time()-1;
2350 tvc->states |= CStatd;
2351 tvc->states &= ~CBulkFetching;
2352 afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2354 else if (tvc->states & CRO) {
2355 /* old-fashioned AFS 3.2 style */
2356 tvc->cbExpires = 3600+osi_Time(); /*XXX*/
2357 tvc->states |= CStatd;
2358 tvc->states &= ~CBulkFetching;
2359 afs_QueueCallback(tvc, CBHash(3600), tvp);
2362 afs_DequeueCallback(tvc);
2363 tvc->callback = NULL;
2364 tvc->states &= ~(CStatd|CUnique);
2365 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2366 osi_dnlc_purgedp (tvc); /* if it (could be) a directory */
2368 ReleaseWriteLock(&afs_xcbhash);
2370 afs_PutVolume(tvp, READ_LOCK);
2372 /* look in per-pag cache */
2373 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2374 ac->axess = OutStatus->CallerAccess; /* substitute pags */
2375 else /* not found, add a new one if possible */
2376 afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2378 ReleaseWriteLock(&tvc->lock);
2379 afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2380 ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32, tvc->cbExpires,
2381 ICL_TYPE_INT32, tvc->cbExpires-osi_Time());
2383 * Release ref count... hope this guy stays around...
2386 } /*afs_StuffVcache*/
2393 * Decrements the reference count on a cache entry.
2396 * avc : Pointer to the cache entry to decrement.
2399 * Nothing interesting.
2401 void afs_PutVCache(register struct vcache *avc)
2403 AFS_STATCNT(afs_PutVCache);
2405 * Can we use a read lock here?
2407 ObtainReadLock(&afs_xvcache);
2409 ReleaseReadLock(&afs_xvcache);
2416 * Find a vcache entry given a fid.
2419 * afid : Pointer to the fid whose cache entry we desire.
2420 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2421 * unlock the vnode, and try again.
2422 * flags: bit 1 to specify whether to compute hit statistics. Not
2423 * set if FindVCache is called as part of internal bookkeeping.
2426 * Must be called with the afs_xvcache lock at least held at
2427 * the read level. In order to do the VLRU adjustment, the xvcache lock
2428 * must be shared-- we upgrade it here.
2431 struct vcache *afs_FindVCache(struct VenusFid *afid, afs_int32 *retry, afs_int32 flag)
2434 register struct vcache *tvc;
2437 AFS_STATCNT(afs_FindVCache);
2440 for(tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2441 if (FidMatches(afid, tvc)) {
2443 /* Grab this vnode, possibly reactivating from the free list */
2446 vg = vget(AFSTOV(tvc));
2450 #endif /* AFS_OSF_ENV */
2455 /* should I have a read lock on the vnode here? */
2457 if (retry) *retry = 0;
2458 #if !defined(AFS_OSF_ENV)
2459 osi_vnhold(tvc, retry); /* already held, above */
2460 if (retry && *retry)
2464 * only move to front of vlru if we have proper vcache locking)
2466 if (flag & DO_VLRU) {
2467 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2468 refpanic ("FindVC VLRU inconsistent1");
2470 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2471 refpanic ("FindVC VLRU inconsistent1");
2473 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2474 refpanic ("FindVC VLRU inconsistent2");
2476 UpgradeSToWLock(&afs_xvcache,26);
2477 QRemove(&tvc->vlruq);
2478 QAdd(&VLRU, &tvc->vlruq);
2479 ConvertWToSLock(&afs_xvcache);
2480 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2481 refpanic ("FindVC VLRU inconsistent1");
2483 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2484 refpanic ("FindVC VLRU inconsistent2");
2486 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2487 refpanic ("FindVC VLRU inconsistent3");
2493 if (flag & DO_STATS) {
2495 afs_stats_cmperf.vcacheHits++;
2497 afs_stats_cmperf.vcacheMisses++;
2498 if (afs_IsPrimaryCellNum(afid->Cell))
2499 afs_stats_cmperf.vlocalAccesses++;
2501 afs_stats_cmperf.vremoteAccesses++;
2504 #ifdef AFS_LINUX22_ENV
2505 if (tvc && (tvc->states & CStatd))
2506 vcache2inode(tvc); /* mainly to reset i_nlink */
2508 #ifdef AFS_DARWIN_ENV
2510 osi_VM_Setup(tvc, 0);
2513 } /*afs_FindVCache*/
2519 * Find a vcache entry given a fid. Does a wildcard match on what we
2520 * have for the fid. If more than one entry, don't return anything.
2523 * avcp : Fill in pointer if we found one and only one.
2524 * afid : Pointer to the fid whose cache entry we desire.
2525 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2526 * unlock the vnode, and try again.
2527 * flags: bit 1 to specify whether to compute hit statistics. Not
2528 * set if FindVCache is called as part of internal bookkeeping.
2531 * Must be called with the afs_xvcache lock at least held at
2532 * the read level. In order to do the VLRU adjustment, the xvcache lock
2533 * must be shared-- we upgrade it here.
2536 * number of matches found.
2539 int afs_duplicate_nfs_fids=0;
2541 afs_int32 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2543 register struct vcache *tvc;
2545 afs_int32 count = 0;
2546 struct vcache *found_tvc = NULL;
2548 AFS_STATCNT(afs_FindVCache);
2550 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2554 ObtainSharedLock(&afs_xvcache,331);
2557 for(tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2558 /* Match only on what we have.... */
2559 if (((tvc->fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2560 && (tvc->fid.Fid.Volume == afid->Fid.Volume)
2561 && ((tvc->fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2562 && (tvc->fid.Cell == afid->Cell)) {
2564 /* Grab this vnode, possibly reactivating from the free list */
2567 vg = vget(AFSTOV(tvc));
2570 /* This vnode no longer exists. */
2573 #endif /* AFS_OSF_ENV */
2578 /* Drop our reference counts. */
2580 vrele(AFSTOV(found_tvc));
2582 afs_duplicate_nfs_fids++;
2583 ReleaseSharedLock(&afs_xvcache);
2591 /* should I have a read lock on the vnode here? */
2593 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2594 afs_int32 retry = 0;
2595 osi_vnhold(tvc, &retry);
2598 found_tvc = (struct vcache*)0;
2599 ReleaseSharedLock(&afs_xvcache);
2600 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2604 #if !defined(AFS_OSF_ENV)
2605 osi_vnhold(tvc, (int*)0); /* already held, above */
2609 * We obtained the xvcache lock above.
2611 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2612 refpanic ("FindVC VLRU inconsistent1");
2614 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2615 refpanic ("FindVC VLRU inconsistent1");
2617 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2618 refpanic ("FindVC VLRU inconsistent2");
2620 UpgradeSToWLock(&afs_xvcache,568);
2621 QRemove(&tvc->vlruq);
2622 QAdd(&VLRU, &tvc->vlruq);
2623 ConvertWToSLock(&afs_xvcache);
2624 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2625 refpanic ("FindVC VLRU inconsistent1");
2627 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2628 refpanic ("FindVC VLRU inconsistent2");
2630 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2631 refpanic ("FindVC VLRU inconsistent3");
2636 if (tvc) afs_stats_cmperf.vcacheHits++;
2637 else afs_stats_cmperf.vcacheMisses++;
2638 if (afs_IsPrimaryCellNum(afid->Cell))
2639 afs_stats_cmperf.vlocalAccesses++;
2641 afs_stats_cmperf.vremoteAccesses++;
2643 *avcp = tvc; /* May be null */
2645 ReleaseSharedLock(&afs_xvcache);
2646 return (tvc ? 1 : 0);
2648 } /*afs_NFSFindVCache*/
2656 * Initialize vcache related variables
2658 void afs_vcacheInit(int astatSize)
2660 register struct vcache *tvp;
2662 #if defined(AFS_OSF_ENV)
2663 if (!afs_maxvcount) {
2664 #if defined(AFS_OSF30_ENV)
2665 afs_maxvcount = max_vnodes/2; /* limit ourselves to half the total */
2667 afs_maxvcount = nvnode/2; /* limit ourselves to half the total */
2669 if (astatSize < afs_maxvcount) {
2670 afs_maxvcount = astatSize;
2673 #else /* AFS_OSF_ENV */
2677 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2678 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2680 #if !defined(AFS_OSF_ENV)
2681 /* Allocate and thread the struct vcache entries */
2682 tvp = (struct vcache *) afs_osi_Alloc(astatSize * sizeof(struct vcache));
2683 memset((char *)tvp, 0, sizeof(struct vcache)*astatSize);
2685 Initial_freeVCList = tvp;
2686 freeVCList = &(tvp[0]);
2687 for(i=0; i < astatSize-1; i++) {
2688 tvp[i].nextfree = &(tvp[i+1]);
2690 tvp[astatSize-1].nextfree = NULL;
2691 #ifdef KERNEL_HAVE_PIN
2692 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
2697 #if defined(AFS_SGI_ENV)
2698 for(i=0; i < astatSize; i++) {
2699 char name[METER_NAMSZ];
2700 struct vcache *tvc = &tvp[i];
2702 tvc->v.v_number = ++afsvnumbers;
2703 tvc->vc_rwlockid = OSI_NO_LOCKID;
2704 initnsema(&tvc->vc_rwlock, 1, makesname(name, "vrw", tvc->v.v_number));
2705 #ifndef AFS_SGI53_ENV
2706 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
2708 #ifndef AFS_SGI62_ENV
2709 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
2710 #endif /* AFS_SGI62_ENV */
2723 void shutdown_vcache(void)
2726 struct afs_cbr *tsp, *nsp;
2728 * XXX We may potentially miss some of the vcaches because if when there're no
2729 * free vcache entries and all the vcache entries are active ones then we allocate
2730 * an additional one - admittedly we almost never had that occur.
2732 #if !defined(AFS_OSF_ENV)
2733 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2735 #ifdef KERNEL_HAVE_PIN
2736 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2740 register struct afs_q *tq, *uq;
2741 register struct vcache *tvc;
2742 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
2746 osi_FreeSmallSpace(tvc->mvid);
2747 tvc->mvid = (struct VenusFid*)0;
2750 aix_gnode_rele(AFSTOV(tvc));
2752 if (tvc->linkData) {
2753 afs_osi_Free(tvc->linkData, strlen(tvc->linkData)+1);
2758 * Also free the remaining ones in the Cache
2760 for (i=0; i < VCSIZE; i++) {
2761 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2763 osi_FreeSmallSpace(tvc->mvid);
2764 tvc->mvid = (struct VenusFid*)0;
2768 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
2769 #ifdef AFS_AIX32_ENV
2772 vms_delete(tvc->segid);
2774 tvc->segid = tvc->vmh = NULL;
2775 if (VREFCOUNT(tvc)) osi_Panic("flushVcache: vm race");
2783 #if defined(AFS_SUN5_ENV)
2789 if (tvc->linkData) {
2790 afs_osi_Free(tvc->linkData, strlen(tvc->linkData)+1);
2794 afs_FreeAllAxs(&(tvc->Access));
2800 * Free any leftover callback queue
2802 for (tsp = afs_cbrSpace; tsp; tsp = nsp ) {
2804 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
2808 #if !defined(AFS_OSF_ENV)
2809 freeVCList = Initial_freeVCList = 0;
2811 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2812 LOCK_INIT(&afs_xvcb, "afs_xvcb");