2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
19 * afs_FlushActiveVcaches
38 #include <afsconfig.h>
39 #include "afs/param.h"
44 #include "afs/sysincludes.h" /*Standard vendor system headers */
45 #include "afsincludes.h" /*AFS-based standard headers */
46 #include "afs/afs_stats.h"
47 #include "afs/afs_cbqueue.h"
48 #include "afs/afs_osidnlc.h"
51 afs_int32 afs_maxvcount = 0; /* max number of vcache entries */
52 afs_int32 afs_vcount = 0; /* number of vcache in use now */
53 #endif /* AFS_OSF_ENV */
61 #endif /* AFS_SGI64_ENV */
63 /* Exported variables */
64 afs_rwlock_t afs_xvcache; /*Lock: alloc new stat cache entries */
65 afs_lock_t afs_xvcb; /*Lock: fids on which there are callbacks */
66 struct vcache *freeVCList; /*Free list for stat cache entries */
67 struct vcache *Initial_freeVCList; /*Initial list for above */
68 struct afs_q VLRU; /*vcache LRU */
69 afs_int32 vcachegen = 0;
70 unsigned int afs_paniconwarn = 0;
71 struct vcache *afs_vhashT[VCSIZE];
72 static struct afs_cbr *afs_cbrHashT[CBRSIZE];
73 afs_int32 afs_bulkStatsLost;
74 int afs_norefpanic = 0;
76 /* Forward declarations */
77 static afs_int32 afs_QueueVCB(struct vcache *avc);
82 * Generate an index into the hash table for a given Fid.
85 afs_HashCBRFid(struct AFSFid *fid)
87 return (fid->Volume + fid->Vnode + fid->Unique) % CBRSIZE;
93 * Insert a CBR entry into the hash table.
94 * Must be called with afs_xvcb held.
97 afs_InsertHashCBR(struct afs_cbr *cbr)
99 int slot = afs_HashCBRFid(&cbr->fid);
101 cbr->hash_next = afs_cbrHashT[slot];
102 if (afs_cbrHashT[slot])
103 afs_cbrHashT[slot]->hash_pprev = &cbr->hash_next;
105 cbr->hash_pprev = &afs_cbrHashT[slot];
106 afs_cbrHashT[slot] = cbr;
113 * Flush the given vcache entry.
116 * avc : Pointer to vcache entry to flush.
117 * slept : Pointer to int to set 1 if we sleep/drop locks, 0 if we don't.
120 * afs_xvcache lock must be held for writing upon entry to
121 * prevent people from changing the vrefCount field, and to
122 * protect the lruq and hnext fields.
123 * LOCK: afs_FlushVCache afs_xvcache W
124 * REFCNT: vcache ref count must be zero on entry except for osf1
125 * RACE: lock is dropped and reobtained, permitting race in caller
129 afs_FlushVCache(struct vcache *avc, int *slept)
130 { /*afs_FlushVCache */
132 register afs_int32 i, code;
133 register struct vcache **uvc, *wvc;
136 AFS_STATCNT(afs_FlushVCache);
137 afs_Trace2(afs_iclSetp, CM_TRACE_FLUSHV, ICL_TYPE_POINTER, avc,
138 ICL_TYPE_INT32, avc->states);
141 VN_LOCK(AFSTOV(avc));
145 code = osi_VM_FlushVCache(avc, slept);
149 if (avc->states & CVFlushed) {
153 if (avc->nextfree || !avc->vlruq.prev || !avc->vlruq.next) { /* qv afs.h */
154 refpanic("LRU vs. Free inconsistency");
156 avc->states |= CVFlushed;
157 /* pull the entry out of the lruq and put it on the free list */
158 QRemove(&avc->vlruq);
159 avc->vlruq.prev = avc->vlruq.next = (struct afs_q *)0;
161 /* keep track of # of files that we bulk stat'd, but never used
162 * before they got recycled.
164 if (avc->states & CBulkStat)
167 /* remove entry from the hash chain */
168 i = VCHash(&avc->fid);
169 uvc = &afs_vhashT[i];
170 for (wvc = *uvc; wvc; uvc = &wvc->hnext, wvc = *uvc) {
173 avc->hnext = (struct vcache *)NULL;
178 osi_Panic("flushvcache"); /* not in correct hash bucket */
180 osi_FreeSmallSpace(avc->mvid);
181 avc->mvid = (struct VenusFid *)0;
183 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
184 avc->linkData = NULL;
186 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
187 /* OK, there are no internal vrefCounts, so there shouldn't
188 * be any more refs here. */
190 avc->v->v_data = NULL; /* remove from vnode */
191 avc->v = NULL; /* also drop the ptr to vnode */
194 afs_FreeAllAxs(&(avc->Access));
196 /* we can't really give back callbacks on RO files, since the
197 * server only tracks them on a per-volume basis, and we don't
198 * know whether we still have some other files from the same
200 if ((avc->states & CRO) == 0 && avc->callback) {
203 ObtainWriteLock(&afs_xcbhash, 460);
204 afs_DequeueCallback(avc); /* remove it from queued callbacks list */
205 avc->states &= ~(CStatd | CUnique);
206 ReleaseWriteLock(&afs_xcbhash);
207 afs_symhint_inval(avc);
208 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
209 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
211 osi_dnlc_purgevp(avc);
214 * Next, keep track of which vnodes we've deleted for create's
215 * optimistic synchronization algorithm
218 if (avc->fid.Fid.Vnode & 1)
223 #if !defined(AFS_OSF_ENV)
224 /* put the entry in the free list */
225 avc->nextfree = freeVCList;
227 if (avc->vlruq.prev || avc->vlruq.next) {
228 refpanic("LRU vs. Free inconsistency");
231 /* This should put it back on the vnode free list since usecount is 1 */
234 if (VREFCOUNT(avc) > 0) {
235 VN_UNLOCK(AFSTOV(avc));
236 AFS_RELE(AFSTOV(avc));
238 if (afs_norefpanic) {
239 printf("flush vc refcnt < 1");
241 (void)vgone(avc, VX_NOSLEEP, NULL);
243 VN_UNLOCK(AFSTOV(avc));
245 osi_Panic("flush vc refcnt < 1");
247 #endif /* AFS_OSF_ENV */
248 avc->states |= CVFlushed;
253 VN_UNLOCK(AFSTOV(avc));
257 } /*afs_FlushVCache */
263 * The core of the inactive vnode op for all but IRIX.
266 afs_InactiveVCache(struct vcache *avc, struct AFS_UCRED *acred)
268 AFS_STATCNT(afs_inactive);
269 if (avc->states & CDirty) {
270 /* we can't keep trying to push back dirty data forever. Give up. */
271 afs_InvalidateAllSegments(avc); /* turns off dirty bit */
273 avc->states &= ~CMAPPED; /* mainly used by SunOS 4.0.x */
274 avc->states &= ~CDirty; /* Turn it off */
275 if (avc->states & CUnlinked) {
276 if (CheckLock(&afs_xvcache) || CheckLock(&afs_xdcache)) {
277 avc->states |= CUnlinkedDel;
280 afs_remunlink(avc, 1); /* ignore any return code */
289 * Description: allocate a callback return structure from the
290 * free list and return it.
292 * Env: The alloc and free routines are both called with the afs_xvcb lock
293 * held, so we don't have to worry about blocking in osi_Alloc.
295 static struct afs_cbr *afs_cbrSpace = 0;
299 register struct afs_cbr *tsp;
302 while (!afs_cbrSpace) {
303 if (afs_stats_cmperf.CallBackAlloced >= 2) {
304 /* don't allocate more than 2 * AFS_NCBRS for now */
306 afs_stats_cmperf.CallBackFlushes++;
310 (struct afs_cbr *)afs_osi_Alloc(AFS_NCBRS *
311 sizeof(struct afs_cbr));
312 for (i = 0; i < AFS_NCBRS - 1; i++) {
313 tsp[i].next = &tsp[i + 1];
315 tsp[AFS_NCBRS - 1].next = 0;
317 afs_stats_cmperf.CallBackAlloced++;
321 afs_cbrSpace = tsp->next;
328 * Description: free a callback return structure, removing it from all lists.
331 * asp -- the address of the structure to free.
333 * Environment: the xvcb lock is held over these calls.
336 afs_FreeCBR(register struct afs_cbr *asp)
338 *(asp->pprev) = asp->next;
340 asp->next->pprev = asp->pprev;
342 *(asp->hash_pprev) = asp->hash_next;
344 asp->hash_next->hash_pprev = asp->hash_pprev;
346 asp->next = afs_cbrSpace;
354 * Description: flush all queued callbacks to all servers.
358 * Environment: holds xvcb lock over RPC to guard against race conditions
359 * when a new callback is granted for the same file later on.
362 afs_FlushVCBs(afs_int32 lockit)
364 struct AFSFid *tfids;
365 struct AFSCallBack callBacks[1];
366 struct AFSCBFids fidArray;
367 struct AFSCBs cbArray;
369 struct afs_cbr *tcbrp;
373 struct vrequest treq;
375 int safety1, safety2, safety3;
377 if ((code = afs_InitReq(&treq, afs_osi_credp)))
379 treq.flags |= O_NONBLOCK;
380 tfids = afs_osi_Alloc(sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
383 MObtainWriteLock(&afs_xvcb, 273);
384 ObtainReadLock(&afs_xserver);
385 for (i = 0; i < NSERVERS; i++) {
386 for (safety1 = 0, tsp = afs_servers[i];
387 tsp && safety1 < afs_totalServers + 10;
388 tsp = tsp->next, safety1++) {
390 if (tsp->cbrs == (struct afs_cbr *)0)
393 /* otherwise, grab a block of AFS_MAXCBRSCALL from the list
394 * and make an RPC, over and over again.
396 tcount = 0; /* number found so far */
397 for (safety2 = 0; safety2 < afs_cacheStats; safety2++) {
398 if (tcount >= AFS_MAXCBRSCALL || !tsp->cbrs) {
399 /* if buffer is full, or we've queued all we're going
400 * to from this server, we should flush out the
403 fidArray.AFSCBFids_len = tcount;
404 fidArray.AFSCBFids_val = (struct AFSFid *)tfids;
405 cbArray.AFSCBs_len = 1;
406 cbArray.AFSCBs_val = callBacks;
407 memset(&callBacks[0], 0, sizeof(callBacks[0]));
408 callBacks[0].CallBackType = CB_EXCLUSIVE;
409 for (safety3 = 0; safety3 < MAXHOSTS * 2; safety3++) {
410 tc = afs_ConnByHost(tsp, tsp->cell->fsport,
411 tsp->cell->cellNum, &treq, 0,
415 (AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS);
418 RXAFS_GiveUpCallBacks(tc->id, &fidArray,
426 AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS, SHARED_LOCK,
431 /* ignore return code, since callbacks may have
432 * been returned anyway, we shouldn't leave them
433 * around to be returned again.
435 * Next, see if we are done with this server, and if so,
436 * break to deal with the next one.
442 /* if to flush full buffer */
443 /* if we make it here, we have an entry at the head of cbrs,
444 * which we should copy to the file ID array and then free.
447 tfids[tcount++] = tcbrp->fid;
449 /* Freeing the CBR will unlink it from the server's CBR list */
451 } /* while loop for this one server */
452 if (safety2 > afs_cacheStats) {
453 afs_warn("possible internal error afs_flushVCBs (%d)\n",
456 } /* for loop for this hash chain */
457 } /* loop through all hash chains */
458 if (safety1 > afs_totalServers + 2) {
460 ("AFS internal error (afs_flushVCBs) (%d > %d), continuing...\n",
461 safety1, afs_totalServers + 2);
463 osi_Panic("afs_flushVCBS safety1");
466 ReleaseReadLock(&afs_xserver);
468 MReleaseWriteLock(&afs_xvcb);
469 afs_osi_Free(tfids, sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
477 * Queue a callback on the given fid.
483 * Locks the xvcb lock.
484 * Called when the xvcache lock is already held.
488 afs_QueueVCB(struct vcache *avc)
491 struct afs_cbr *tcbp;
493 AFS_STATCNT(afs_QueueVCB);
494 /* The callback is really just a struct server ptr. */
495 tsp = (struct server *)(avc->callback);
497 /* we now have a pointer to the server, so we just allocate
498 * a queue entry and queue it.
500 MObtainWriteLock(&afs_xvcb, 274);
501 tcbp = afs_AllocCBR();
502 tcbp->fid = avc->fid.Fid;
504 tcbp->next = tsp->cbrs;
506 tsp->cbrs->pprev = &tcbp->next;
509 tcbp->pprev = &tsp->cbrs;
511 afs_InsertHashCBR(tcbp);
513 /* now release locks and return */
514 MReleaseWriteLock(&afs_xvcb);
523 * Remove a queued callback for a given Fid.
526 * afid: The fid we want cleansed of queued callbacks.
529 * Locks xvcb and xserver locks.
530 * Typically called with xdcache, xvcache and/or individual vcache
535 afs_RemoveVCB(struct VenusFid *afid)
538 struct afs_cbr *cbr, *ncbr;
540 AFS_STATCNT(afs_RemoveVCB);
541 MObtainWriteLock(&afs_xvcb, 275);
543 slot = afs_HashCBRFid(&afid->Fid);
544 ncbr = afs_cbrHashT[slot];
548 ncbr = cbr->hash_next;
550 if (afid->Fid.Volume == cbr->fid.Volume &&
551 afid->Fid.Vnode == cbr->fid.Vnode &&
552 afid->Fid.Unique == cbr->fid.Unique) {
557 MReleaseWriteLock(&afs_xvcb);
564 * This routine is responsible for allocating a new cache entry
565 * from the free list. It formats the cache entry and inserts it
566 * into the appropriate hash tables. It must be called with
567 * afs_xvcache write-locked so as to prevent several processes from
568 * trying to create a new cache entry simultaneously.
571 * afid : The file id of the file whose cache entry is being
574 /* LOCK: afs_NewVCache afs_xvcache W */
576 afs_NewVCache(struct VenusFid *afid, struct server *serverp)
580 afs_int32 anumber = VCACHE_FREE;
582 struct gnode *gnodepnt;
586 #endif /* AFS_OSF_ENV */
587 struct afs_q *tq, *uq;
590 AFS_STATCNT(afs_NewVCache);
593 if (afs_vcount >= afs_maxvcount) {
596 * If we are using > 33 % of the total system vnodes for AFS vcache
597 * entries or we are using the maximum number of vcache entries,
598 * then free some. (if our usage is > 33% we should free some, if
599 * our usage is > afs_maxvcount, set elsewhere to 0.5*nvnode,
600 * we _must_ free some -- no choice).
602 if (((3 * afs_vcount) > nvnode) || (afs_vcount >= afs_maxvcount)) {
604 struct afs_q *tq, *uq;
609 for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
612 if (tvc->states & CVFlushed)
613 refpanic("CVFlushed on VLRU");
614 else if (i++ > afs_maxvcount)
615 refpanic("Exceeded pool of AFS vnodes(VLRU cycle?)");
616 else if (QNext(uq) != tq)
617 refpanic("VLRU inconsistent");
618 else if (VREFCOUNT(tvc) < 1)
619 refpanic("refcnt 0 on VLRU");
621 if (VREFCOUNT(tvc) == 1 && tvc->opens == 0
622 && (tvc->states & CUnlinkedDel) == 0) {
623 code = afs_FlushVCache(tvc, &fv_slept);
630 continue; /* start over - may have raced. */
636 if (anumber == VCACHE_FREE) {
637 printf("NewVCache: warning none freed, using %d of %d\n",
638 afs_vcount, afs_maxvcount);
639 if (afs_vcount >= afs_maxvcount) {
640 osi_Panic("NewVCache - none freed");
641 /* XXX instead of panicing, should do afs_maxvcount++
642 * and magic up another one */
648 if (getnewvnode(MOUNT_AFS, &Afs_vnodeops, &nvc)) {
649 /* What should we do ???? */
650 osi_Panic("afs_NewVCache: no more vnodes");
655 tvc->nextfree = NULL;
657 #else /* AFS_OSF_ENV */
658 /* pull out a free cache entry */
661 for (tq = VLRU.prev; (anumber > 0) && (tq != &VLRU); tq = uq) {
665 if (tvc->states & CVFlushed) {
666 refpanic("CVFlushed on VLRU");
668 } else if (i++ > 2 * afs_cacheStats) { /* even allowing for a few xallocs... */
669 refpanic("Increase -stat parameter of afsd(VLRU cycle?)");
671 } else if (QNext(uq) != tq) {
672 refpanic("VLRU inconsistent");
674 #if defined(AFS_LINUX22_ENV)
675 if (tvc != afs_globalVp && VREFCOUNT(tvc) && tvc->opens == 0) {
676 struct dentry *dentry;
677 struct list_head *cur, *head = &(AFSTOI(tvc))->i_dentry;
682 #if defined(AFS_LINUX24_ENV)
683 spin_lock(&dcache_lock);
686 while ((cur = cur->next) != head) {
687 dentry = list_entry(cur, struct dentry, d_alias);
689 if (d_unhashed(dentry))
694 #if defined(AFS_LINUX24_ENV)
695 spin_unlock(&dcache_lock);
697 if (d_invalidate(dentry) == -EBUSY) {
699 /* perhaps lock and try to continue? (use cur as head?) */
705 #if defined(AFS_LINUX24_ENV)
706 spin_unlock(&dcache_lock);
714 if (((VREFCOUNT(tvc) == 0)
715 #if defined(AFS_DARWIN_ENV) && !defined(UKERNEL)
716 || ((VREFCOUNT(tvc) == 1) &&
717 (UBCINFOEXISTS(AFSTOV(tvc))))
719 ) && tvc->opens == 0 && (tvc->states & CUnlinkedDel) == 0) {
720 #if defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
722 * vgone() reclaims the vnode, which calls afs_FlushVCache(),
723 * then it puts the vnode on the free list.
724 * If we don't do this we end up with a cleaned vnode that's
725 * not on the free list.
726 * XXX assume FreeBSD is the same for now.
733 code = afs_FlushVCache(tvc, &fv_slept);
741 continue; /* start over - may have raced. */
749 /* none free, making one is better than a panic */
750 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
751 if (afs_cacheStats == afs_stats_cmperf.vcacheXAllocs) printf("would vlru cycle panic\n");
752 tvc = (struct vcache *)afs_osi_Alloc(sizeof(struct vcache));
753 #if defined(AFS_DARWIN_ENV) && !defined(UKERNEL)
754 tvc->v = NULL; /* important to clean this, or use memset 0 */
756 #ifdef KERNEL_HAVE_PIN
757 pin((char *)tvc, sizeof(struct vcache)); /* XXX */
759 #if defined(AFS_SGI_ENV)
761 char name[METER_NAMSZ];
762 memset(tvc, 0, sizeof(struct vcache));
763 tvc->v.v_number = ++afsvnumbers;
764 tvc->vc_rwlockid = OSI_NO_LOCKID;
765 initnsema(&tvc->vc_rwlock, 1,
766 makesname(name, "vrw", tvc->v.v_number));
767 #ifndef AFS_SGI53_ENV
768 initnsema(&tvc->v.v_sync, 0,
769 makesname(name, "vsy", tvc->v.v_number));
771 #ifndef AFS_SGI62_ENV
772 initnlock(&tvc->v.v_lock,
773 makesname(name, "vlk", tvc->v.v_number));
776 #endif /* AFS_SGI_ENV */
778 tvc = freeVCList; /* take from free list */
779 freeVCList = tvc->nextfree;
780 tvc->nextfree = NULL;
782 #endif /* AFS_OSF_ENV */
784 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
786 panic("afs_NewVCache(): free vcache with vnode attached");
789 #if !defined(AFS_SGI_ENV) && !defined(AFS_OSF_ENV)
790 memset((char *)tvc, 0, sizeof(struct vcache));
795 RWLOCK_INIT(&tvc->lock, "vcache lock");
796 #if defined(AFS_SUN5_ENV)
797 RWLOCK_INIT(&tvc->vlock, "vcache vlock");
798 #endif /* defined(AFS_SUN5_ENV) */
802 afs_nbsd_getnewvnode(tvc); /* includes one refcount */
804 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
806 #ifdef AFS_DARWIN_ENV
808 afs_darwin_getnewvnode(tvc); /* includes one refcount */
810 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
817 #ifdef AFS_FBSD50_ENV
818 if (getnewvnode(MOUNT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
820 if (getnewvnode(VT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
822 panic("afs getnewvnode"); /* can't happen */
824 if (tvc->v != NULL) {
825 /* I'd like to know if this ever happens...
826 * We don't drop global for the rest of this function,
827 * so if we do lose the race, the other thread should
828 * have found the same vnode and finished initializing
829 * the vcache entry. Is it conceivable that this vcache
830 * entry could be recycled during this interval? If so,
831 * then there probably needs to be some sort of additional
832 * mutual exclusion (an Embryonic flag would suffice).
834 printf("afs_NewVCache: lost the race\n");
838 tvc->v->v_data = tvc;
839 lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
842 tvc->parentVnode = 0;
844 tvc->linkData = NULL;
847 tvc->execsOrWriters = 0;
851 tvc->last_looker = 0;
853 tvc->asynchrony = -1;
855 afs_symhint_inval(tvc);
857 tvc->flushDV.low = tvc->flushDV.high = AFS_MAXDV;
860 tvc->truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
861 hzero(tvc->m.DataVersion); /* in case we copy it into flushDV */
862 #if defined(AFS_LINUX22_ENV)
864 struct inode *ip = AFSTOI(tvc);
865 #if defined(AFS_LINUX24_ENV)
866 struct address_space *mapping = &ip->i_data;
869 #if defined(AFS_LINUX26_ENV)
872 sema_init(&ip->i_sem, 1);
873 INIT_LIST_HEAD(&ip->i_hash);
874 INIT_LIST_HEAD(&ip->i_dentry);
875 #if defined(AFS_LINUX24_ENV)
876 sema_init(&ip->i_zombie, 1);
877 init_waitqueue_head(&ip->i_wait);
878 spin_lock_init(&ip->i_data.i_shared_lock);
879 #ifdef STRUCT_ADDRESS_SPACE_HAS_PAGE_LOCK
880 spin_lock_init(&ip->i_data.page_lock);
882 INIT_LIST_HEAD(&ip->i_data.clean_pages);
883 INIT_LIST_HEAD(&ip->i_data.dirty_pages);
884 INIT_LIST_HEAD(&ip->i_data.locked_pages);
885 INIT_LIST_HEAD(&ip->i_dirty_buffers);
886 #ifdef STRUCT_INODE_HAS_I_DIRTY_DATA_BUFFERS
887 INIT_LIST_HEAD(&ip->i_dirty_data_buffers);
889 #ifdef STRUCT_INODE_HAS_I_DEVICES
890 INIT_LIST_HEAD(&ip->i_devices);
892 #ifdef STRUCT_INODE_HAS_I_TRUNCATE_SEM
893 init_rwsem(&ip->i_truncate_sem);
895 #ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
896 init_rwsem(&ip->i_alloc_sem);
899 #else /* AFS_LINUX22_ENV */
900 sema_init(&ip->i_atomic_write, 1);
901 init_waitqueue(&ip->i_wait);
905 #if defined(AFS_LINUX24_ENV)
907 ip->i_mapping = mapping;
908 #ifdef STRUCT_ADDRESS_SPACE_HAS_GFP_MASK
909 ip->i_data.gfp_mask = GFP_HIGHUSER;
911 #if defined(AFS_LINUX26_ENV)
912 mapping_set_gfp_mask(mapping, GFP_HIGHUSER);
914 extern struct backing_dev_info afs_backing_dev_info;
916 mapping->backing_dev_info = &afs_backing_dev_info;
921 #if !defined(AFS_LINUX26_ENV)
923 ip->i_dev = afs_globalVFS->s_dev;
925 #ifdef STRUCT_INODE_HAS_I_SECURITY
926 ip->i_security = NULL;
927 if (security_inode_alloc(ip))
928 panic("Cannot allocate inode security");
931 ip->i_sb = afs_globalVFS;
932 put_inode_on_dummy_list(ip);
933 #ifdef STRUCT_INODE_HAS_I_SB_LIST
934 list_add(&ip->i_sb_list, &ip->i_sb->s_inodes);
936 #ifdef STRUCT_INODE_HAS_INOTIFY_LOCK
937 INIT_LIST_HEAD(&ip->inotify_watches);
938 spin_lock_init(&ip->inotify_lock);
944 /* Hold it for the LRU (should make count 2) */
945 VN_HOLD(AFSTOV(tvc));
946 #else /* AFS_OSF_ENV */
947 #if !(defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV))
948 VREFCOUNT_SET(tvc, 1); /* us */
949 #endif /* AFS_XBSD_ENV */
950 #endif /* AFS_OSF_ENV */
952 LOCK_INIT(&tvc->pvmlock, "vcache pvmlock");
953 tvc->vmh = tvc->segid = NULL;
956 #ifdef AFS_BOZONLOCK_ENV
957 #if defined(AFS_SUN5_ENV)
958 rw_init(&tvc->rwlock, "vcache rwlock", RW_DEFAULT, NULL);
960 #if defined(AFS_SUN55_ENV)
961 /* This is required if the kaio (kernel aynchronous io)
962 ** module is installed. Inside the kernel, the function
963 ** check_vp( common/os/aio.c) checks to see if the kernel has
964 ** to provide asynchronous io for this vnode. This
965 ** function extracts the device number by following the
966 ** v_data field of the vnode. If we do not set this field
967 ** then the system panics. The value of the v_data field
968 ** is not really important for AFS vnodes because the kernel
969 ** does not do asynchronous io for regular files. Hence,
970 ** for the time being, we fill up the v_data field with the
971 ** vnode pointer itself. */
972 tvc->v.v_data = (char *)tvc;
973 #endif /* AFS_SUN55_ENV */
975 afs_BozonInit(&tvc->pvnLock, tvc);
979 tvc->callback = serverp; /* to minimize chance that clear
981 /* initialize vnode data, note vrefCount is v.v_count */
983 /* Don't forget to free the gnode space */
984 tvc->v.v_gnode = gnodepnt =
985 (struct gnode *)osi_AllocSmallSpace(sizeof(struct gnode));
986 memset((char *)gnodepnt, 0, sizeof(struct gnode));
989 memset((void *)&(tvc->vc_bhv_desc), 0, sizeof(tvc->vc_bhv_desc));
990 bhv_desc_init(&(tvc->vc_bhv_desc), tvc, tvc, &Afs_vnodeops);
992 vn_bhv_head_init(&(tvc->v.v_bh), "afsvp");
993 vn_bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
995 bhv_head_init(&(tvc->v.v_bh));
996 bhv_insert_initial(&(tvc->v.v_bh), &(tvc->vc_bhv_desc));
999 tvc->v.v_mreg = tvc->v.v_mregb = (struct pregion *)tvc;
1000 #ifdef VNODE_TRACING
1001 tvc->v.v_trace = ktrace_alloc(VNODE_TRACE_SIZE, 0);
1003 init_bitlock(&tvc->v.v_pcacheflag, VNODE_PCACHE_LOCKBIT, "afs_pcache",
1005 init_mutex(&tvc->v.v_filocksem, MUTEX_DEFAULT, "afsvfl", (long)tvc);
1006 init_mutex(&tvc->v.v_buf_lock, MUTEX_DEFAULT, "afsvnbuf", (long)tvc);
1008 vnode_pcache_init(&tvc->v);
1009 #if defined(DEBUG) && defined(VNODE_INIT_BITLOCK)
1010 /* Above define is never true execpt in SGI test kernels. */
1011 init_bitlock(&(tvc->v.v_flag, VLOCK, "vnode", tvc->v.v_number);
1013 #ifdef INTR_KTHREADS
1014 AFS_VN_INIT_BUF_LOCK(&(tvc->v));
1017 SetAfsVnode(AFSTOV(tvc));
1018 #endif /* AFS_SGI64_ENV */
1020 * The proper value for mvstat (for root fids) is setup by the caller.
1023 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
1025 if (afs_globalVFS == 0)
1026 osi_Panic("afs globalvfs");
1027 vSetVfsp(tvc, afs_globalVFS);
1028 vSetType(tvc, VREG);
1030 tvc->v.v_vfsnext = afs_globalVFS->vfs_vnodes; /* link off vfs */
1031 tvc->v.v_vfsprev = NULL;
1032 afs_globalVFS->vfs_vnodes = &tvc->v;
1033 if (tvc->v.v_vfsnext != NULL)
1034 tvc->v.v_vfsnext->v_vfsprev = &tvc->v;
1035 tvc->v.v_next = gnodepnt->gn_vnode; /*Single vnode per gnode for us! */
1036 gnodepnt->gn_vnode = &tvc->v;
1038 #if defined(AFS_DUX40_ENV)
1039 insmntque(tvc, afs_globalVFS, &afs_ubcops);
1042 /* Is this needed??? */
1043 insmntque(tvc, afs_globalVFS);
1044 #endif /* AFS_OSF_ENV */
1045 #endif /* AFS_DUX40_ENV */
1046 #if defined(AFS_SGI_ENV)
1047 VN_SET_DPAGES(&(tvc->v), (struct pfdat *)NULL);
1048 osi_Assert((tvc->v.v_flag & VINACT) == 0);
1050 osi_Assert(VN_GET_PGCNT(&(tvc->v)) == 0);
1051 osi_Assert(tvc->mapcnt == 0 && tvc->vc_locktrips == 0);
1052 osi_Assert(tvc->vc_rwlockid == OSI_NO_LOCKID);
1053 osi_Assert(tvc->v.v_filocks == NULL);
1054 #if !defined(AFS_SGI65_ENV)
1055 osi_Assert(tvc->v.v_filocksem == NULL);
1057 osi_Assert(tvc->cred == NULL);
1058 #ifdef AFS_SGI64_ENV
1059 vnode_pcache_reinit(&tvc->v);
1060 tvc->v.v_rdev = NODEV;
1062 vn_initlist((struct vnlist *)&tvc->v);
1064 #endif /* AFS_SGI_ENV */
1066 osi_dnlc_purgedp(tvc); /* this may be overkill */
1067 memset((char *)&(tvc->quick), 0, sizeof(struct vtodc));
1068 memset((char *)&(tvc->callsort), 0, sizeof(struct afs_q));
1072 tvc->hnext = afs_vhashT[i];
1073 afs_vhashT[i] = tvc;
1074 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1075 refpanic("NewVCache VLRU inconsistent");
1077 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
1078 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1079 refpanic("NewVCache VLRU inconsistent2");
1081 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
1082 refpanic("NewVCache VLRU inconsistent3");
1084 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
1085 refpanic("NewVCache VLRU inconsistent4");
1091 } /*afs_NewVCache */
1095 * afs_FlushActiveVcaches
1101 * doflocks : Do we handle flocks?
1103 /* LOCK: afs_FlushActiveVcaches afs_xvcache N */
1105 afs_FlushActiveVcaches(register afs_int32 doflocks)
1107 register struct vcache *tvc;
1109 register struct conn *tc;
1110 register afs_int32 code;
1111 register struct AFS_UCRED *cred = NULL;
1112 struct vrequest treq, ureq;
1113 struct AFSVolSync tsync;
1116 AFS_STATCNT(afs_FlushActiveVcaches);
1117 ObtainReadLock(&afs_xvcache);
1118 for (i = 0; i < VCSIZE; i++) {
1119 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1120 if (doflocks && tvc->flockCount != 0) {
1121 /* if this entry has an flock, send a keep-alive call out */
1123 ReleaseReadLock(&afs_xvcache);
1124 ObtainWriteLock(&tvc->lock, 51);
1126 afs_InitReq(&treq, afs_osi_credp);
1127 treq.flags |= O_NONBLOCK;
1129 tc = afs_Conn(&tvc->fid, &treq, SHARED_LOCK);
1131 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
1134 RXAFS_ExtendLock(tc->id,
1135 (struct AFSFid *)&tvc->fid.Fid,
1141 } while (afs_Analyze
1142 (tc, code, &tvc->fid, &treq,
1143 AFS_STATS_FS_RPCIDX_EXTENDLOCK, SHARED_LOCK, NULL));
1145 ReleaseWriteLock(&tvc->lock);
1146 ObtainReadLock(&afs_xvcache);
1150 if ((tvc->states & CCore) || (tvc->states & CUnlinkedDel)) {
1152 * Don't let it evaporate in case someone else is in
1153 * this code. Also, drop the afs_xvcache lock while
1154 * getting vcache locks.
1157 ReleaseReadLock(&afs_xvcache);
1158 #ifdef AFS_BOZONLOCK_ENV
1159 afs_BozonLock(&tvc->pvnLock, tvc);
1161 #if defined(AFS_SGI_ENV)
1163 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1165 osi_Assert(VREFCOUNT(tvc) > 0);
1166 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1168 ObtainWriteLock(&tvc->lock, 52);
1169 if (tvc->states & CCore) {
1170 tvc->states &= ~CCore;
1171 /* XXXX Find better place-holder for cred XXXX */
1172 cred = (struct AFS_UCRED *)tvc->linkData;
1173 tvc->linkData = NULL; /* XXX */
1174 afs_InitReq(&ureq, cred);
1175 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1176 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
1177 tvc->execsOrWriters);
1178 code = afs_StoreOnLastReference(tvc, &ureq);
1179 ReleaseWriteLock(&tvc->lock);
1180 #ifdef AFS_BOZONLOCK_ENV
1181 afs_BozonUnlock(&tvc->pvnLock, tvc);
1183 hzero(tvc->flushDV);
1186 if (code && code != VNOVNODE) {
1187 afs_StoreWarn(code, tvc->fid.Fid.Volume,
1188 /* /dev/console */ 1);
1190 } else if (tvc->states & CUnlinkedDel) {
1194 ReleaseWriteLock(&tvc->lock);
1195 #ifdef AFS_BOZONLOCK_ENV
1196 afs_BozonUnlock(&tvc->pvnLock, tvc);
1198 #if defined(AFS_SGI_ENV)
1199 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1201 afs_remunlink(tvc, 0);
1202 #if defined(AFS_SGI_ENV)
1203 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1206 /* lost (or won, perhaps) the race condition */
1207 ReleaseWriteLock(&tvc->lock);
1208 #ifdef AFS_BOZONLOCK_ENV
1209 afs_BozonUnlock(&tvc->pvnLock, tvc);
1212 #if defined(AFS_SGI_ENV)
1213 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1215 ObtainReadLock(&afs_xvcache);
1218 AFS_RELE(AFSTOV(tvc));
1219 /* Matches write code setting CCore flag */
1225 ReleaseReadLock(&afs_xvcache);
1233 * Make sure a cache entry is up-to-date status-wise.
1235 * NOTE: everywhere that calls this can potentially be sped up
1236 * by checking CStatd first, and avoiding doing the InitReq
1237 * if this is up-to-date.
1239 * Anymore, the only places that call this KNOW already that the
1240 * vcache is not up-to-date, so we don't screw around.
1243 * avc : Ptr to vcache entry to verify.
1248 afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1250 register struct vcache *tvc;
1252 AFS_STATCNT(afs_VerifyVCache);
1254 #if defined(AFS_OSF_ENV)
1255 ObtainReadLock(&avc->lock);
1256 if (afs_IsWired(avc)) {
1257 ReleaseReadLock(&avc->lock);
1260 ReleaseReadLock(&avc->lock);
1261 #endif /* AFS_OSF_ENV */
1262 /* otherwise we must fetch the status info */
1264 ObtainWriteLock(&avc->lock, 53);
1265 if (avc->states & CStatd) {
1266 ReleaseWriteLock(&avc->lock);
1269 ObtainWriteLock(&afs_xcbhash, 461);
1270 avc->states &= ~(CStatd | CUnique);
1271 avc->callback = NULL;
1272 afs_DequeueCallback(avc);
1273 ReleaseWriteLock(&afs_xcbhash);
1274 ReleaseWriteLock(&avc->lock);
1276 /* since we've been called back, or the callback has expired,
1277 * it's possible that the contents of this directory, or this
1278 * file's name have changed, thus invalidating the dnlc contents.
1280 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1281 osi_dnlc_purgedp(avc);
1283 osi_dnlc_purgevp(avc);
1285 /* fetch the status info */
1286 tvc = afs_GetVCache(&avc->fid, areq, NULL, avc);
1289 /* Put it back; caller has already incremented vrefCount */
1293 } /*afs_VerifyVCache */
1300 * Simple copy of stat info into cache.
1303 * avc : Ptr to vcache entry involved.
1304 * astat : Ptr to stat info to copy.
1307 * Nothing interesting.
1309 * Callers: as of 1992-04-29, only called by WriteVCache
1312 afs_SimpleVStat(register struct vcache *avc,
1313 register struct AFSFetchStatus *astat, struct vrequest *areq)
1316 AFS_STATCNT(afs_SimpleVStat);
1319 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1320 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1322 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1324 #ifdef AFS_64BIT_CLIENT
1325 FillInt64(length, astat->Length_hi, astat->Length);
1326 #else /* AFS_64BIT_CLIENT */
1327 length = astat->Length;
1328 #endif /* AFS_64BIT_CLIENT */
1329 #if defined(AFS_SGI_ENV)
1330 osi_Assert((valusema(&avc->vc_rwlock) <= 0)
1331 && (OSI_GET_LOCKID() == avc->vc_rwlockid));
1332 if (length < avc->m.Length) {
1333 vnode_t *vp = (vnode_t *) avc;
1335 osi_Assert(WriteLocked(&avc->lock));
1336 ReleaseWriteLock(&avc->lock);
1338 PTOSSVP(vp, (off_t) length, (off_t) MAXLONG);
1340 ObtainWriteLock(&avc->lock, 67);
1343 /* if writing the file, don't fetch over this value */
1344 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT, ICL_TYPE_POINTER, avc,
1345 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1346 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1347 avc->m.Length = length;
1348 avc->m.Date = astat->ClientModTime;
1350 avc->m.Owner = astat->Owner;
1351 avc->m.Group = astat->Group;
1352 avc->m.Mode = astat->UnixModeBits;
1353 if (vType(avc) == VREG) {
1354 avc->m.Mode |= S_IFREG;
1355 } else if (vType(avc) == VDIR) {
1356 avc->m.Mode |= S_IFDIR;
1357 } else if (vType(avc) == VLNK) {
1358 avc->m.Mode |= S_IFLNK;
1359 if ((avc->m.Mode & 0111) == 0)
1362 if (avc->states & CForeign) {
1363 struct axscache *ac;
1364 avc->anyAccess = astat->AnonymousAccess;
1366 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1368 * Caller has at least one bit not covered by anonymous, and
1369 * thus may have interesting rights.
1371 * HOWEVER, this is a really bad idea, because any access query
1372 * for bits which aren't covered by anonymous, on behalf of a user
1373 * who doesn't have any special rights, will result in an answer of
1374 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1375 * It's an especially bad idea under Ultrix, since (due to the lack of
1376 * a proper access() call) it must perform several afs_access() calls
1377 * in order to create magic mode bits that vary according to who makes
1378 * the call. In other words, _every_ stat() generates a test for
1381 #endif /* badidea */
1382 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1383 ac->axess = astat->CallerAccess;
1384 else /* not found, add a new one if possible */
1385 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1389 } /*afs_SimpleVStat */
1396 * Store the status info *only* back to the server for a
1400 * avc : Ptr to the vcache entry.
1401 * astatus : Ptr to the status info to store.
1402 * areq : Ptr to the associated vrequest.
1405 * Must be called with a shared lock held on the vnode.
1409 afs_WriteVCache(register struct vcache *avc,
1410 register struct AFSStoreStatus *astatus,
1411 struct vrequest *areq)
1415 struct AFSFetchStatus OutStatus;
1416 struct AFSVolSync tsync;
1418 AFS_STATCNT(afs_WriteVCache);
1419 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1420 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
1423 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
1425 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1428 RXAFS_StoreStatus(tc->id, (struct AFSFid *)&avc->fid.Fid,
1429 astatus, &OutStatus, &tsync);
1434 } while (afs_Analyze
1435 (tc, code, &avc->fid, areq, AFS_STATS_FS_RPCIDX_STORESTATUS,
1436 SHARED_LOCK, NULL));
1438 UpgradeSToWLock(&avc->lock, 20);
1440 /* success, do the changes locally */
1441 afs_SimpleVStat(avc, &OutStatus, areq);
1443 * Update the date, too. SimpleVStat didn't do this, since
1444 * it thought we were doing this after fetching new status
1445 * over a file being written.
1447 avc->m.Date = OutStatus.ClientModTime;
1449 /* failure, set up to check with server next time */
1450 ObtainWriteLock(&afs_xcbhash, 462);
1451 afs_DequeueCallback(avc);
1452 avc->states &= ~(CStatd | CUnique); /* turn off stat valid flag */
1453 ReleaseWriteLock(&afs_xcbhash);
1454 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
1455 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
1457 ConvertWToSLock(&avc->lock);
1460 } /*afs_WriteVCache */
1466 * Copy astat block into vcache info
1469 * avc : Ptr to vcache entry.
1470 * astat : Ptr to stat block to copy in.
1471 * areq : Ptr to associated request.
1474 * Must be called under a write lock
1476 * Note: this code may get dataversion and length out of sync if the file has
1477 * been modified. This is less than ideal. I haven't thought about
1478 * it sufficiently to be certain that it is adequate.
1481 afs_ProcessFS(register struct vcache *avc,
1482 register struct AFSFetchStatus *astat, struct vrequest *areq)
1485 AFS_STATCNT(afs_ProcessFS);
1487 #ifdef AFS_64BIT_CLIENT
1488 FillInt64(length, astat->Length_hi, astat->Length);
1489 #else /* AFS_64BIT_CLIENT */
1490 length = astat->Length;
1491 #endif /* AFS_64BIT_CLIENT */
1492 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1493 * number for each bulk status request. Under no circumstances
1494 * should afs_DoBulkStat store a sequence number if the new
1495 * length will be ignored when afs_ProcessFS is called with
1496 * new stats. If you change the following conditional then you
1497 * also need to change the conditional in afs_DoBulkStat. */
1499 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1500 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1502 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1504 /* if we're writing or mapping this file, don't fetch over these
1507 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1508 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1509 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1510 avc->m.Length = length;
1511 avc->m.Date = astat->ClientModTime;
1513 hset64(avc->m.DataVersion, astat->dataVersionHigh, astat->DataVersion);
1514 avc->m.Owner = astat->Owner;
1515 avc->m.Mode = astat->UnixModeBits;
1516 avc->m.Group = astat->Group;
1517 avc->m.LinkCount = astat->LinkCount;
1518 if (astat->FileType == File) {
1519 vSetType(avc, VREG);
1520 avc->m.Mode |= S_IFREG;
1521 } else if (astat->FileType == Directory) {
1522 vSetType(avc, VDIR);
1523 avc->m.Mode |= S_IFDIR;
1524 } else if (astat->FileType == SymbolicLink) {
1525 if (afs_fakestat_enable && (avc->m.Mode & 0111) == 0) {
1526 vSetType(avc, VDIR);
1527 avc->m.Mode |= S_IFDIR;
1529 vSetType(avc, VLNK);
1530 avc->m.Mode |= S_IFLNK;
1532 if ((avc->m.Mode & 0111) == 0) {
1536 avc->anyAccess = astat->AnonymousAccess;
1538 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1540 * Caller has at least one bit not covered by anonymous, and
1541 * thus may have interesting rights.
1543 * HOWEVER, this is a really bad idea, because any access query
1544 * for bits which aren't covered by anonymous, on behalf of a user
1545 * who doesn't have any special rights, will result in an answer of
1546 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1547 * It's an especially bad idea under Ultrix, since (due to the lack of
1548 * a proper access() call) it must perform several afs_access() calls
1549 * in order to create magic mode bits that vary according to who makes
1550 * the call. In other words, _every_ stat() generates a test for
1553 #endif /* badidea */
1555 struct axscache *ac;
1556 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1557 ac->axess = astat->CallerAccess;
1558 else /* not found, add a new one if possible */
1559 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1561 #ifdef AFS_LINUX22_ENV
1562 vcache2inode(avc); /* Set the inode attr cache */
1565 } /*afs_ProcessFS */
1569 afs_RemoteLookup(register struct VenusFid *afid, struct vrequest *areq,
1570 char *name, struct VenusFid *nfid,
1571 struct AFSFetchStatus *OutStatusp,
1572 struct AFSCallBack *CallBackp, struct server **serverp,
1573 struct AFSVolSync *tsyncp)
1577 register struct conn *tc;
1578 struct AFSFetchStatus OutDirStatus;
1581 name = ""; /* XXX */
1583 tc = afs_Conn(afid, areq, SHARED_LOCK);
1586 *serverp = tc->srvr->server;
1588 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1591 RXAFS_Lookup(tc->id, (struct AFSFid *)&afid->Fid, name,
1592 (struct AFSFid *)&nfid->Fid, OutStatusp,
1593 &OutDirStatus, CallBackp, tsyncp);
1598 } while (afs_Analyze
1599 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_XLOOKUP, SHARED_LOCK,
1610 * Given a file id and a vrequest structure, fetch the status
1611 * information associated with the file.
1615 * areq : Ptr to associated vrequest structure, specifying the
1616 * user whose authentication tokens will be used.
1617 * avc : caller may already have a vcache for this file, which is
1621 * The cache entry is returned with an increased vrefCount field.
1622 * The entry must be discarded by calling afs_PutVCache when you
1623 * are through using the pointer to the cache entry.
1625 * You should not hold any locks when calling this function, except
1626 * locks on other vcache entries. If you lock more than one vcache
1627 * entry simultaneously, you should lock them in this order:
1629 * 1. Lock all files first, then directories.
1630 * 2. Within a particular type, lock entries in Fid.Vnode order.
1632 * This locking hierarchy is convenient because it allows locking
1633 * of a parent dir cache entry, given a file (to check its access
1634 * control list). It also allows renames to be handled easily by
1635 * locking directories in a constant order.
1636 * NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1638 /* might have a vcache structure already, which must
1639 * already be held by the caller */
1642 afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
1643 afs_int32 * cached, struct vcache *avc)
1646 afs_int32 code, newvcache = 0;
1647 register struct vcache *tvc;
1651 AFS_STATCNT(afs_GetVCache);
1654 *cached = 0; /* Init just in case */
1656 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1660 ObtainSharedLock(&afs_xvcache, 5);
1662 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU);
1664 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1665 ReleaseSharedLock(&afs_xvcache);
1666 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1674 if (tvc->states & CStatd) {
1675 ReleaseSharedLock(&afs_xvcache);
1679 UpgradeSToWLock(&afs_xvcache, 21);
1681 /* no cache entry, better grab one */
1682 tvc = afs_NewVCache(afid, NULL);
1685 ConvertWToSLock(&afs_xvcache);
1686 afs_stats_cmperf.vcacheMisses++;
1689 ReleaseSharedLock(&afs_xvcache);
1691 ObtainWriteLock(&tvc->lock, 54);
1693 if (tvc->states & CStatd) {
1694 #ifdef AFS_LINUX22_ENV
1697 ReleaseWriteLock(&tvc->lock);
1700 #if defined(AFS_OSF_ENV)
1701 if (afs_IsWired(tvc)) {
1702 ReleaseWriteLock(&tvc->lock);
1705 #endif /* AFS_OSF_ENV */
1707 VOP_LOCK(AFSTOV(tvc), LK_EXCLUSIVE | LK_RETRY, curproc);
1708 uvm_vnp_uncache(AFSTOV(tvc));
1709 VOP_UNLOCK(AFSTOV(tvc), 0, curproc);
1711 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
1713 * XXX - I really don't like this. Should try to understand better.
1714 * It seems that sometimes, when we get called, we already hold the
1715 * lock on the vnode (e.g., from afs_getattr via afs_VerifyVCache).
1716 * We can't drop the vnode lock, because that could result in a race.
1717 * Sometimes, though, we get here and don't hold the vnode lock.
1718 * I hate code paths that sometimes hold locks and sometimes don't.
1719 * In any event, the dodge we use here is to check whether the vnode
1720 * is locked, and if it isn't, then we gain and drop it around the call
1721 * to vinvalbuf; otherwise, we leave it alone.
1728 #ifdef AFS_FBSD50_ENV
1729 iheldthelock = VOP_ISLOCKED(vp, curthread);
1731 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1732 vinvalbuf(vp, V_SAVE, osi_curcred(), curthread, PINOD, 0);
1734 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1736 #ifdef AFS_DARWIN_ENV
1737 iheldthelock = VOP_ISLOCKED(vp);
1739 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, current_proc());
1740 /* this is messy. we can call fsync which will try to reobtain this */
1741 if (VTOAFS(vp) == tvc)
1742 ReleaseWriteLock(&tvc->lock);
1743 if (UBCINFOEXISTS(vp)) {
1744 vinvalbuf(vp, V_SAVE, &afs_osi_cred, current_proc(), PINOD, 0);
1746 if (VTOAFS(vp) == tvc)
1747 ObtainWriteLock(&tvc->lock, 954);
1749 VOP_UNLOCK(vp, LK_EXCLUSIVE, current_proc());
1751 iheldthelock = VOP_ISLOCKED(vp, curproc);
1753 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1754 vinvalbuf(vp, V_SAVE, osi_curcred(), curproc, PINOD, 0);
1756 VOP_UNLOCK(vp, LK_EXCLUSIVE, curproc);
1762 ObtainWriteLock(&afs_xcbhash, 464);
1763 tvc->states &= ~CUnique;
1765 afs_DequeueCallback(tvc);
1766 ReleaseWriteLock(&afs_xcbhash);
1768 /* It is always appropriate to throw away all the access rights? */
1769 afs_FreeAllAxs(&(tvc->Access));
1770 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1772 if ((tvp->states & VForeign)) {
1774 tvc->states |= CForeign;
1775 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1776 && (tvp->rootUnique == afid->Fid.Unique)) {
1780 if (tvp->states & VRO)
1782 if (tvp->states & VBackup)
1783 tvc->states |= CBackup;
1784 /* now copy ".." entry back out of volume structure, if necessary */
1785 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1787 tvc->mvid = (struct VenusFid *)
1788 osi_AllocSmallSpace(sizeof(struct VenusFid));
1789 *tvc->mvid = tvp->dotdot;
1791 afs_PutVolume(tvp, READ_LOCK);
1795 afs_RemoveVCB(afid);
1797 struct AFSFetchStatus OutStatus;
1799 if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1800 afs_ProcessFS(tvc, &OutStatus, areq);
1801 tvc->states |= CStatd | CUnique;
1804 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1809 ReleaseWriteLock(&tvc->lock);
1811 ObtainReadLock(&afs_xvcache);
1813 ReleaseReadLock(&afs_xvcache);
1817 ReleaseWriteLock(&tvc->lock);
1820 } /*afs_GetVCache */
1825 afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1826 afs_int32 * cached, struct vcache *adp, char *aname)
1828 afs_int32 code, now, newvcache = 0;
1829 struct VenusFid nfid;
1830 register struct vcache *tvc;
1832 struct AFSFetchStatus OutStatus;
1833 struct AFSCallBack CallBack;
1834 struct AFSVolSync tsync;
1835 struct server *serverp = 0;
1839 AFS_STATCNT(afs_GetVCache);
1841 *cached = 0; /* Init just in case */
1843 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1847 ObtainReadLock(&afs_xvcache);
1848 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */ );
1851 ReleaseReadLock(&afs_xvcache);
1853 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1854 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1858 ObtainReadLock(&tvc->lock);
1860 if (tvc->states & CStatd) {
1864 ReleaseReadLock(&tvc->lock);
1867 tvc->states &= ~CUnique;
1869 ReleaseReadLock(&tvc->lock);
1870 ObtainReadLock(&afs_xvcache);
1874 ReleaseReadLock(&afs_xvcache);
1876 /* lookup the file */
1879 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
1881 afs_RemoteLookup(&adp->fid, areq, aname, &nfid, &OutStatus, &CallBack,
1884 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1888 ObtainSharedLock(&afs_xvcache, 6);
1889 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU /* no xstats now */ );
1891 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1892 ReleaseSharedLock(&afs_xvcache);
1893 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1899 /* no cache entry, better grab one */
1900 UpgradeSToWLock(&afs_xvcache, 22);
1901 tvc = afs_NewVCache(&nfid, serverp);
1903 ConvertWToSLock(&afs_xvcache);
1906 ReleaseSharedLock(&afs_xvcache);
1907 ObtainWriteLock(&tvc->lock, 55);
1909 /* It is always appropriate to throw away all the access rights? */
1910 afs_FreeAllAxs(&(tvc->Access));
1911 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
1913 if ((tvp->states & VForeign)) {
1915 tvc->states |= CForeign;
1916 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1917 && (tvp->rootUnique == afid->Fid.Unique))
1920 if (tvp->states & VRO)
1922 if (tvp->states & VBackup)
1923 tvc->states |= CBackup;
1924 /* now copy ".." entry back out of volume structure, if necessary */
1925 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1927 tvc->mvid = (struct VenusFid *)
1928 osi_AllocSmallSpace(sizeof(struct VenusFid));
1929 *tvc->mvid = tvp->dotdot;
1934 ObtainWriteLock(&afs_xcbhash, 465);
1935 afs_DequeueCallback(tvc);
1936 tvc->states &= ~(CStatd | CUnique);
1937 ReleaseWriteLock(&afs_xcbhash);
1938 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1939 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
1941 afs_PutVolume(tvp, READ_LOCK);
1942 ReleaseWriteLock(&tvc->lock);
1943 ObtainReadLock(&afs_xvcache);
1945 ReleaseReadLock(&afs_xvcache);
1949 ObtainWriteLock(&afs_xcbhash, 466);
1950 if (origCBs == afs_allCBs) {
1951 if (CallBack.ExpirationTime) {
1952 tvc->callback = serverp;
1953 tvc->cbExpires = CallBack.ExpirationTime + now;
1954 tvc->states |= CStatd | CUnique;
1955 tvc->states &= ~CBulkFetching;
1956 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
1957 } else if (tvc->states & CRO) {
1958 /* adapt gives us an hour. */
1959 tvc->cbExpires = 3600 + osi_Time();
1960 /*XXX*/ tvc->states |= CStatd | CUnique;
1961 tvc->states &= ~CBulkFetching;
1962 afs_QueueCallback(tvc, CBHash(3600), tvp);
1964 tvc->callback = NULL;
1965 afs_DequeueCallback(tvc);
1966 tvc->states &= ~(CStatd | CUnique);
1967 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1968 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
1971 afs_DequeueCallback(tvc);
1972 tvc->states &= ~CStatd;
1973 tvc->states &= ~CUnique;
1974 tvc->callback = NULL;
1975 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
1976 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
1978 ReleaseWriteLock(&afs_xcbhash);
1980 afs_PutVolume(tvp, READ_LOCK);
1981 afs_ProcessFS(tvc, &OutStatus, areq);
1983 ReleaseWriteLock(&tvc->lock);
1989 afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
1990 afs_int32 * cached, struct volume *tvolp)
1992 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
1993 afs_int32 getNewFid = 0;
1995 struct VenusFid nfid;
1996 register struct vcache *tvc;
1997 struct server *serverp = 0;
1998 struct AFSFetchStatus OutStatus;
1999 struct AFSCallBack CallBack;
2000 struct AFSVolSync tsync;
2006 if (!tvolp->rootVnode || getNewFid) {
2007 struct VenusFid tfid;
2010 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2011 origCBs = afs_allCBs; /* ignore InitCallBackState */
2013 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2018 /* ReleaseReadLock(&tvolp->lock); */
2019 ObtainWriteLock(&tvolp->lock, 56);
2020 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
2021 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
2022 ReleaseWriteLock(&tvolp->lock);
2023 /* ObtainReadLock(&tvolp->lock);*/
2026 afid->Fid.Vnode = tvolp->rootVnode;
2027 afid->Fid.Unique = tvolp->rootUnique;
2030 ObtainSharedLock(&afs_xvcache, 7);
2032 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2033 if (!FidCmp(&(tvc->fid), afid)) {
2035 /* Grab this vnode, possibly reactivating from the free list */
2036 /* for the present (95.05.25) everything on the hash table is
2037 * definitively NOT in the free list -- at least until afs_reclaim
2038 * can be safely implemented */
2041 vg = vget(AFSTOV(tvc)); /* this bumps ref count */
2045 #endif /* AFS_OSF_ENV */
2050 if (!haveStatus && (!tvc || !(tvc->states & CStatd))) {
2051 /* Mount point no longer stat'd or unknown. FID may have changed. */
2054 AFS_RELE(AFSTOV(tvc));
2058 ReleaseSharedLock(&afs_xvcache);
2063 UpgradeSToWLock(&afs_xvcache, 23);
2064 /* no cache entry, better grab one */
2065 tvc = afs_NewVCache(afid, NULL);
2067 afs_stats_cmperf.vcacheMisses++;
2071 afs_stats_cmperf.vcacheHits++;
2073 /* we already bumped the ref count in the for loop above */
2074 #else /* AFS_OSF_ENV */
2077 UpgradeSToWLock(&afs_xvcache, 24);
2078 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2079 refpanic("GRVC VLRU inconsistent0");
2081 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2082 refpanic("GRVC VLRU inconsistent1");
2084 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2085 refpanic("GRVC VLRU inconsistent2");
2087 QRemove(&tvc->vlruq); /* move to lruq head */
2088 QAdd(&VLRU, &tvc->vlruq);
2089 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2090 refpanic("GRVC VLRU inconsistent3");
2092 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2093 refpanic("GRVC VLRU inconsistent4");
2095 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2096 refpanic("GRVC VLRU inconsistent5");
2101 ReleaseWriteLock(&afs_xvcache);
2103 if (tvc->states & CStatd) {
2107 ObtainReadLock(&tvc->lock);
2108 tvc->states &= ~CUnique;
2109 tvc->callback = NULL; /* redundant, perhaps */
2110 ReleaseReadLock(&tvc->lock);
2113 ObtainWriteLock(&tvc->lock, 57);
2115 /* It is always appropriate to throw away all the access rights? */
2116 afs_FreeAllAxs(&(tvc->Access));
2119 tvc->states |= CForeign;
2120 if (tvolp->states & VRO)
2122 if (tvolp->states & VBackup)
2123 tvc->states |= CBackup;
2124 /* now copy ".." entry back out of volume structure, if necessary */
2125 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2126 && (tvolp->rootUnique == afid->Fid.Unique)) {
2129 if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2131 tvc->mvid = (struct VenusFid *)
2132 osi_AllocSmallSpace(sizeof(struct VenusFid));
2133 *tvc->mvid = tvolp->dotdot;
2137 afs_RemoveVCB(afid);
2140 struct VenusFid tfid;
2143 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2144 origCBs = afs_allCBs; /* ignore InitCallBackState */
2146 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2151 ObtainWriteLock(&afs_xcbhash, 467);
2152 afs_DequeueCallback(tvc);
2153 tvc->callback = NULL;
2154 tvc->states &= ~(CStatd | CUnique);
2155 ReleaseWriteLock(&afs_xcbhash);
2156 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2157 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2158 ReleaseWriteLock(&tvc->lock);
2159 ObtainReadLock(&afs_xvcache);
2161 ReleaseReadLock(&afs_xvcache);
2165 ObtainWriteLock(&afs_xcbhash, 468);
2166 if (origCBs == afs_allCBs) {
2167 tvc->states |= CTruth;
2168 tvc->callback = serverp;
2169 if (CallBack.ExpirationTime != 0) {
2170 tvc->cbExpires = CallBack.ExpirationTime + start;
2171 tvc->states |= CStatd;
2172 tvc->states &= ~CBulkFetching;
2173 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2174 } else if (tvc->states & CRO) {
2175 /* adapt gives us an hour. */
2176 tvc->cbExpires = 3600 + osi_Time();
2177 /*XXX*/ tvc->states |= CStatd;
2178 tvc->states &= ~CBulkFetching;
2179 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2182 afs_DequeueCallback(tvc);
2183 tvc->callback = NULL;
2184 tvc->states &= ~(CStatd | CUnique);
2185 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2186 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2188 ReleaseWriteLock(&afs_xcbhash);
2189 afs_ProcessFS(tvc, &OutStatus, areq);
2191 ReleaseWriteLock(&tvc->lock);
2198 * must be called with avc write-locked
2199 * don't absolutely have to invalidate the hint unless the dv has
2200 * changed, but be sure to get it right else there will be consistency bugs.
2203 afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
2204 struct vrequest * areq, struct AFSFetchStatus * Outsp)
2207 afs_uint32 start = 0;
2208 register struct conn *tc;
2209 struct AFSCallBack CallBack;
2210 struct AFSVolSync tsync;
2211 struct volume *volp;
2214 tc = afs_Conn(afid, areq, SHARED_LOCK);
2215 avc->quick.stamp = 0;
2216 avc->h1.dchint = NULL; /* invalidate hints */
2218 avc->callback = tc->srvr->server;
2220 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2223 RXAFS_FetchStatus(tc->id, (struct AFSFid *)&afid->Fid, Outsp,
2231 } while (afs_Analyze
2232 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2233 SHARED_LOCK, NULL));
2236 afs_ProcessFS(avc, Outsp, areq);
2237 volp = afs_GetVolume(afid, areq, READ_LOCK);
2238 ObtainWriteLock(&afs_xcbhash, 469);
2239 avc->states |= CTruth;
2240 if (avc->callback /* check for race */ ) {
2241 if (CallBack.ExpirationTime != 0) {
2242 avc->cbExpires = CallBack.ExpirationTime + start;
2243 avc->states |= CStatd;
2244 avc->states &= ~CBulkFetching;
2245 afs_QueueCallback(avc, CBHash(CallBack.ExpirationTime), volp);
2246 } else if (avc->states & CRO) { /* ordinary callback on a read-only volume -- AFS 3.2 style */
2247 avc->cbExpires = 3600 + start;
2248 avc->states |= CStatd;
2249 avc->states &= ~CBulkFetching;
2250 afs_QueueCallback(avc, CBHash(3600), volp);
2252 afs_DequeueCallback(avc);
2253 avc->callback = NULL;
2254 avc->states &= ~(CStatd | CUnique);
2255 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2256 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2259 afs_DequeueCallback(avc);
2260 avc->callback = NULL;
2261 avc->states &= ~(CStatd | CUnique);
2262 if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
2263 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2265 ReleaseWriteLock(&afs_xcbhash);
2267 afs_PutVolume(volp, READ_LOCK);
2269 /* used to undo the local callback, but that's too extreme.
2270 * There are plenty of good reasons that fetchstatus might return
2271 * an error, such as EPERM. If we have the vnode cached, statd,
2272 * with callback, might as well keep track of the fact that we
2273 * don't have access...
2275 if (code == EPERM || code == EACCES) {
2276 struct axscache *ac;
2277 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2279 else /* not found, add a new one if possible */
2280 afs_AddAxs(avc->Access, areq->uid, 0);
2291 * Stuff some information into the vcache for the given file.
2294 * afid : File in question.
2295 * OutStatus : Fetch status on the file.
2296 * CallBack : Callback info.
2297 * tc : RPC connection involved.
2298 * areq : vrequest involved.
2301 * Nothing interesting.
2304 afs_StuffVcache(register struct VenusFid *afid,
2305 struct AFSFetchStatus *OutStatus,
2306 struct AFSCallBack *CallBack, register struct conn *tc,
2307 struct vrequest *areq)
2309 register afs_int32 code, i, newvcache = 0;
2310 register struct vcache *tvc;
2311 struct AFSVolSync tsync;
2313 struct axscache *ac;
2316 AFS_STATCNT(afs_StuffVcache);
2317 #ifdef IFS_VCACHECOUNT
2322 ObtainSharedLock(&afs_xvcache, 8);
2324 tvc = afs_FindVCache(afid, &retry, DO_VLRU /* no stats */ );
2326 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2327 ReleaseSharedLock(&afs_xvcache);
2328 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2334 /* no cache entry, better grab one */
2335 UpgradeSToWLock(&afs_xvcache, 25);
2336 tvc = afs_NewVCache(afid, NULL);
2338 ConvertWToSLock(&afs_xvcache);
2341 ReleaseSharedLock(&afs_xvcache);
2342 ObtainWriteLock(&tvc->lock, 58);
2344 tvc->states &= ~CStatd;
2345 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2346 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2348 /* Is it always appropriate to throw away all the access rights? */
2349 afs_FreeAllAxs(&(tvc->Access));
2351 /*Copy useful per-volume info */
2352 tvp = afs_GetVolume(afid, areq, READ_LOCK);
2354 if (newvcache && (tvp->states & VForeign))
2355 tvc->states |= CForeign;
2356 if (tvp->states & VRO)
2358 if (tvp->states & VBackup)
2359 tvc->states |= CBackup;
2361 * Now, copy ".." entry back out of volume structure, if
2364 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2366 tvc->mvid = (struct VenusFid *)
2367 osi_AllocSmallSpace(sizeof(struct VenusFid));
2368 *tvc->mvid = tvp->dotdot;
2371 /* store the stat on the file */
2372 afs_RemoveVCB(afid);
2373 afs_ProcessFS(tvc, OutStatus, areq);
2374 tvc->callback = tc->srvr->server;
2376 /* we use osi_Time twice below. Ideally, we would use the time at which
2377 * the FetchStatus call began, instead, but we don't have it here. So we
2378 * make do with "now". In the CRO case, it doesn't really matter. In
2379 * the other case, we hope that the difference between "now" and when the
2380 * call actually began execution on the server won't be larger than the
2381 * padding which the server keeps. Subtract 1 second anyway, to be on
2382 * the safe side. Can't subtract more because we don't know how big
2383 * ExpirationTime is. Possible consistency problems may arise if the call
2384 * timeout period becomes longer than the server's expiration padding. */
2385 ObtainWriteLock(&afs_xcbhash, 470);
2386 if (CallBack->ExpirationTime != 0) {
2387 tvc->cbExpires = CallBack->ExpirationTime + osi_Time() - 1;
2388 tvc->states |= CStatd;
2389 tvc->states &= ~CBulkFetching;
2390 afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2391 } else if (tvc->states & CRO) {
2392 /* old-fashioned AFS 3.2 style */
2393 tvc->cbExpires = 3600 + osi_Time();
2394 /*XXX*/ tvc->states |= CStatd;
2395 tvc->states &= ~CBulkFetching;
2396 afs_QueueCallback(tvc, CBHash(3600), tvp);
2398 afs_DequeueCallback(tvc);
2399 tvc->callback = NULL;
2400 tvc->states &= ~(CStatd | CUnique);
2401 if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
2402 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2404 ReleaseWriteLock(&afs_xcbhash);
2406 afs_PutVolume(tvp, READ_LOCK);
2408 /* look in per-pag cache */
2409 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2410 ac->axess = OutStatus->CallerAccess; /* substitute pags */
2411 else /* not found, add a new one if possible */
2412 afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2414 ReleaseWriteLock(&tvc->lock);
2415 afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2416 ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32,
2417 tvc->cbExpires, ICL_TYPE_INT32, tvc->cbExpires - osi_Time());
2419 * Release ref count... hope this guy stays around...
2422 } /*afs_StuffVcache */
2429 * Decrements the reference count on a cache entry.
2432 * avc : Pointer to the cache entry to decrement.
2435 * Nothing interesting.
2438 afs_PutVCache(register struct vcache *avc)
2440 AFS_STATCNT(afs_PutVCache);
2442 * Can we use a read lock here?
2444 ObtainReadLock(&afs_xvcache);
2446 ReleaseReadLock(&afs_xvcache);
2447 } /*afs_PutVCache */
2453 * Find a vcache entry given a fid.
2456 * afid : Pointer to the fid whose cache entry we desire.
2457 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2458 * unlock the vnode, and try again.
2459 * flags: bit 1 to specify whether to compute hit statistics. Not
2460 * set if FindVCache is called as part of internal bookkeeping.
2463 * Must be called with the afs_xvcache lock at least held at
2464 * the read level. In order to do the VLRU adjustment, the xvcache lock
2465 * must be shared-- we upgrade it here.
2469 afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
2472 register struct vcache *tvc;
2475 AFS_STATCNT(afs_FindVCache);
2478 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2479 if (FidMatches(afid, tvc)) {
2481 /* Grab this vnode, possibly reactivating from the free list */
2484 vg = vget(AFSTOV(tvc));
2488 #endif /* AFS_OSF_ENV */
2493 /* should I have a read lock on the vnode here? */
2497 #if !defined(AFS_OSF_ENV)
2498 osi_vnhold(tvc, retry); /* already held, above */
2499 if (retry && *retry)
2502 #ifdef AFS_DARWIN_ENV
2503 tvc->states |= CUBCinit;
2505 if (UBCINFOMISSING(AFSTOV(tvc)) ||
2506 UBCINFORECLAIMED(AFSTOV(tvc))) {
2507 ubc_info_init(AFSTOV(tvc));
2510 tvc->states &= ~CUBCinit;
2513 * only move to front of vlru if we have proper vcache locking)
2515 if (flag & DO_VLRU) {
2516 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2517 refpanic("FindVC VLRU inconsistent1");
2519 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2520 refpanic("FindVC VLRU inconsistent1");
2522 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2523 refpanic("FindVC VLRU inconsistent2");
2525 UpgradeSToWLock(&afs_xvcache, 26);
2526 QRemove(&tvc->vlruq);
2527 QAdd(&VLRU, &tvc->vlruq);
2528 ConvertWToSLock(&afs_xvcache);
2529 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2530 refpanic("FindVC VLRU inconsistent1");
2532 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2533 refpanic("FindVC VLRU inconsistent2");
2535 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2536 refpanic("FindVC VLRU inconsistent3");
2542 if (flag & DO_STATS) {
2544 afs_stats_cmperf.vcacheHits++;
2546 afs_stats_cmperf.vcacheMisses++;
2547 if (afs_IsPrimaryCellNum(afid->Cell))
2548 afs_stats_cmperf.vlocalAccesses++;
2550 afs_stats_cmperf.vremoteAccesses++;
2552 #ifdef AFS_LINUX22_ENV
2553 if (tvc && (tvc->states & CStatd))
2554 vcache2inode(tvc); /* mainly to reset i_nlink */
2557 } /*afs_FindVCache */
2563 * Find a vcache entry given a fid. Does a wildcard match on what we
2564 * have for the fid. If more than one entry, don't return anything.
2567 * avcp : Fill in pointer if we found one and only one.
2568 * afid : Pointer to the fid whose cache entry we desire.
2569 * retry: (SGI-specific) tell the caller to drop the lock on xvcache,
2570 * unlock the vnode, and try again.
2571 * flags: bit 1 to specify whether to compute hit statistics. Not
2572 * set if FindVCache is called as part of internal bookkeeping.
2575 * Must be called with the afs_xvcache lock at least held at
2576 * the read level. In order to do the VLRU adjustment, the xvcache lock
2577 * must be shared-- we upgrade it here.
2580 * number of matches found.
2583 int afs_duplicate_nfs_fids = 0;
2586 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2588 register struct vcache *tvc;
2590 afs_int32 count = 0;
2591 struct vcache *found_tvc = NULL;
2593 AFS_STATCNT(afs_FindVCache);
2595 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2599 ObtainSharedLock(&afs_xvcache, 331);
2602 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2603 /* Match only on what we have.... */
2604 if (((tvc->fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2605 && (tvc->fid.Fid.Volume == afid->Fid.Volume)
2606 && ((tvc->fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2607 && (tvc->fid.Cell == afid->Cell)) {
2609 /* Grab this vnode, possibly reactivating from the free list */
2612 vg = vget(AFSTOV(tvc));
2615 /* This vnode no longer exists. */
2618 #endif /* AFS_OSF_ENV */
2623 /* Drop our reference counts. */
2625 vrele(AFSTOV(found_tvc));
2627 afs_duplicate_nfs_fids++;
2628 ReleaseSharedLock(&afs_xvcache);
2636 /* should I have a read lock on the vnode here? */
2638 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2639 afs_int32 retry = 0;
2640 osi_vnhold(tvc, &retry);
2643 found_tvc = (struct vcache *)0;
2644 ReleaseSharedLock(&afs_xvcache);
2645 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2649 #if !defined(AFS_OSF_ENV)
2650 osi_vnhold(tvc, (int *)0); /* already held, above */
2654 * We obtained the xvcache lock above.
2656 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2657 refpanic("FindVC VLRU inconsistent1");
2659 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2660 refpanic("FindVC VLRU inconsistent1");
2662 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2663 refpanic("FindVC VLRU inconsistent2");
2665 UpgradeSToWLock(&afs_xvcache, 568);
2666 QRemove(&tvc->vlruq);
2667 QAdd(&VLRU, &tvc->vlruq);
2668 ConvertWToSLock(&afs_xvcache);
2669 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2670 refpanic("FindVC VLRU inconsistent1");
2672 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2673 refpanic("FindVC VLRU inconsistent2");
2675 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2676 refpanic("FindVC VLRU inconsistent3");
2682 afs_stats_cmperf.vcacheHits++;
2684 afs_stats_cmperf.vcacheMisses++;
2685 if (afs_IsPrimaryCellNum(afid->Cell))
2686 afs_stats_cmperf.vlocalAccesses++;
2688 afs_stats_cmperf.vremoteAccesses++;
2690 *avcp = tvc; /* May be null */
2692 ReleaseSharedLock(&afs_xvcache);
2693 return (tvc ? 1 : 0);
2695 } /*afs_NFSFindVCache */
2703 * Initialize vcache related variables
2706 afs_vcacheInit(int astatSize)
2708 register struct vcache *tvp;
2710 #if defined(AFS_OSF_ENV)
2711 if (!afs_maxvcount) {
2712 #if defined(AFS_OSF30_ENV)
2713 afs_maxvcount = max_vnodes / 2; /* limit ourselves to half the total */
2715 afs_maxvcount = nvnode / 2; /* limit ourselves to half the total */
2717 if (astatSize < afs_maxvcount) {
2718 afs_maxvcount = astatSize;
2721 #else /* AFS_OSF_ENV */
2725 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2726 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2728 #if !defined(AFS_OSF_ENV)
2729 #ifdef AFS_LINUX26_ENV
2730 printf("old style would have needed %d contiguous bytes\n", astatSize *
2731 sizeof(struct vcache));
2732 Initial_freeVCList = freeVCList = tvp = (struct vcache *)
2733 afs_osi_Alloc(sizeof(struct vcache));
2734 for (i = 0; i < astatSize; i++) {
2735 tvp->nextfree = (struct vcache *) afs_osi_Alloc(sizeof(struct vcache));
2736 tvp = tvp->nextfree;
2738 tvp->nextfree = NULL;
2740 /* Allocate and thread the struct vcache entries */
2741 tvp = (struct vcache *)afs_osi_Alloc(astatSize * sizeof(struct vcache));
2742 memset((char *)tvp, 0, sizeof(struct vcache) * astatSize);
2744 Initial_freeVCList = tvp;
2745 freeVCList = &(tvp[0]);
2746 for (i = 0; i < astatSize - 1; i++) {
2747 tvp[i].nextfree = &(tvp[i + 1]);
2749 tvp[astatSize - 1].nextfree = NULL;
2750 #ifdef KERNEL_HAVE_PIN
2751 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
2756 #if defined(AFS_SGI_ENV)
2757 for (i = 0; i < astatSize; i++) {
2758 char name[METER_NAMSZ];
2759 struct vcache *tvc = &tvp[i];
2761 tvc->v.v_number = ++afsvnumbers;
2762 tvc->vc_rwlockid = OSI_NO_LOCKID;
2763 initnsema(&tvc->vc_rwlock, 1,
2764 makesname(name, "vrw", tvc->v.v_number));
2765 #ifndef AFS_SGI53_ENV
2766 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
2768 #ifndef AFS_SGI62_ENV
2769 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
2770 #endif /* AFS_SGI62_ENV */
2784 shutdown_vcache(void)
2787 struct afs_cbr *tsp, *nsp;
2789 * XXX We may potentially miss some of the vcaches because if when there're no
2790 * free vcache entries and all the vcache entries are active ones then we allocate
2791 * an additional one - admittedly we almost never had that occur.
2795 register struct afs_q *tq, *uq;
2796 register struct vcache *tvc;
2797 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
2801 osi_FreeSmallSpace(tvc->mvid);
2802 tvc->mvid = (struct VenusFid *)0;
2805 aix_gnode_rele(AFSTOV(tvc));
2807 if (tvc->linkData) {
2808 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
2813 * Also free the remaining ones in the Cache
2815 for (i = 0; i < VCSIZE; i++) {
2816 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2818 osi_FreeSmallSpace(tvc->mvid);
2819 tvc->mvid = (struct VenusFid *)0;
2823 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
2824 #ifdef AFS_AIX32_ENV
2827 vms_delete(tvc->segid);
2829 tvc->segid = tvc->vmh = NULL;
2831 osi_Panic("flushVcache: vm race");
2839 #if defined(AFS_SUN5_ENV)
2845 if (tvc->linkData) {
2846 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
2850 afs_FreeAllAxs(&(tvc->Access));
2856 * Free any leftover callback queue
2858 for (tsp = afs_cbrSpace; tsp; tsp = nsp) {
2860 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
2864 #ifdef AFS_LINUX26_ENV
2866 struct vcache *tvp = Initial_freeVCList;
2868 struct vcache *next = tvp->nextfree;
2870 afs_osi_Free(tvp, sizeof(struct vcache));
2875 #ifdef KERNEL_HAVE_PIN
2876 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2878 #if !defined(AFS_OSF_ENV)
2879 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2883 #if !defined(AFS_OSF_ENV)
2884 freeVCList = Initial_freeVCList = 0;
2886 RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2887 LOCK_INIT(&afs_xvcb, "afs_xvcb");