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
22 * afs_WriteVCacheDiscon
40 #include <afsconfig.h>
41 #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"
49 afs_int32 afs_maxvcount = 0; /* max number of vcache entries */
50 afs_int32 afs_vcount = 0; /* number of vcache in use now */
58 #endif /* AFS_SGI64_ENV */
60 /* Exported variables */
62 afs_rwlock_t afs_xvcdirty; /*Lock: discon vcache dirty list mgmt */
64 afs_rwlock_t afs_xvcache; /*Lock: alloc new stat cache entries */
65 afs_rwlock_t afs_xvreclaim; /*Lock: entries reclaimed, not on free list */
66 afs_lock_t afs_xvcb; /*Lock: fids on which there are callbacks */
67 #if !defined(AFS_LINUX22_ENV)
68 static struct vcache *freeVCList; /*Free list for stat cache entries */
69 struct vcache *ReclaimedVCList; /*Reclaimed list for stat entries */
70 static struct vcache *Initial_freeVCList; /*Initial list for above */
72 struct afs_q VLRU; /*vcache LRU */
73 afs_int32 vcachegen = 0;
74 unsigned int afs_paniconwarn = 0;
75 struct vcache *afs_vhashT[VCSIZE];
76 struct afs_q afs_vhashTV[VCSIZE];
77 static struct afs_cbr *afs_cbrHashT[CBRSIZE];
78 afs_int32 afs_bulkStatsLost;
79 int afs_norefpanic = 0;
82 /* Disk backed vcache definitions
83 * Both protected by xvcache */
85 static int afs_nextVcacheSlot = 0;
86 static struct afs_slotlist *afs_freeSlotList = NULL;
89 /* Forward declarations */
90 static afs_int32 afs_QueueVCB(struct vcache *avc);
93 * Generate an index into the hash table for a given Fid.
95 * \return The hash value.
98 afs_HashCBRFid(struct AFSFid *fid)
100 return (fid->Volume + fid->Vnode + fid->Unique) % CBRSIZE;
104 * Insert a CBR entry into the hash table.
105 * Must be called with afs_xvcb held.
110 afs_InsertHashCBR(struct afs_cbr *cbr)
112 int slot = afs_HashCBRFid(&cbr->fid);
114 cbr->hash_next = afs_cbrHashT[slot];
115 if (afs_cbrHashT[slot])
116 afs_cbrHashT[slot]->hash_pprev = &cbr->hash_next;
118 cbr->hash_pprev = &afs_cbrHashT[slot];
119 afs_cbrHashT[slot] = cbr;
124 * Flush the given vcache entry.
127 * afs_xvcache lock must be held for writing upon entry to
128 * prevent people from changing the vrefCount field, and to
129 * protect the lruq and hnext fields.
130 * LOCK: afs_FlushVCache afs_xvcache W
131 * REFCNT: vcache ref count must be zero on entry except for osf1
132 * RACE: lock is dropped and reobtained, permitting race in caller
134 * \param avc Pointer to vcache entry to flush.
135 * \param slept Pointer to int to set 1 if we sleep/drop locks, 0 if we don't.
139 afs_FlushVCache(struct vcache *avc, int *slept)
140 { /*afs_FlushVCache */
143 struct vcache **uvc, *wvc;
146 AFS_STATCNT(afs_FlushVCache);
147 afs_Trace2(afs_iclSetp, CM_TRACE_FLUSHV, ICL_TYPE_POINTER, avc,
148 ICL_TYPE_INT32, avc->f.states);
150 code = osi_VM_FlushVCache(avc, slept);
154 if (avc->f.states & CVFlushed) {
158 #if !defined(AFS_LINUX22_ENV)
159 if (avc->nextfree || !avc->vlruq.prev || !avc->vlruq.next) { /* qv afs.h */
160 refpanic("LRU vs. Free inconsistency");
163 avc->f.states |= CVFlushed;
164 /* pull the entry out of the lruq and put it on the free list */
165 QRemove(&avc->vlruq);
167 /* keep track of # of files that we bulk stat'd, but never used
168 * before they got recycled.
170 if (avc->f.states & CBulkStat)
173 /* remove entry from the hash chain */
174 i = VCHash(&avc->f.fid);
175 uvc = &afs_vhashT[i];
176 for (wvc = *uvc; wvc; uvc = &wvc->hnext, wvc = *uvc) {
179 avc->hnext = (struct vcache *)NULL;
184 /* remove entry from the volume hash table */
185 QRemove(&avc->vhashq);
188 osi_FreeSmallSpace(avc->mvid);
189 avc->mvid = (struct VenusFid *)0;
191 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
192 avc->linkData = NULL;
194 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
195 /* OK, there are no internal vrefCounts, so there shouldn't
196 * be any more refs here. */
198 #ifdef AFS_DARWIN80_ENV
199 vnode_clearfsnode(AFSTOV(avc));
200 vnode_removefsref(AFSTOV(avc));
202 avc->v->v_data = NULL; /* remove from vnode */
204 AFSTOV(avc) = NULL; /* also drop the ptr to vnode */
207 #ifdef AFS_SUN510_ENV
208 /* As we use private vnodes, cleanup is up to us */
209 vn_reinit(AFSTOV(avc));
211 afs_FreeAllAxs(&(avc->Access));
213 ObtainWriteLock(&afs_xcbhash, 460);
214 afs_DequeueCallback(avc); /* remove it from queued callbacks list */
215 avc->f.states &= ~(CStatd | CUnique);
216 ReleaseWriteLock(&afs_xcbhash);
217 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
218 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
220 osi_dnlc_purgevp(avc);
223 * Next, keep track of which vnodes we've deleted for create's
224 * optimistic synchronization algorithm
227 if (avc->f.fid.Fid.Vnode & 1)
233 #if !defined(AFS_LINUX22_ENV)
234 /* put the entry in the free list */
235 avc->nextfree = freeVCList;
237 if (avc->vlruq.prev || avc->vlruq.next) {
238 refpanic("LRU vs. Free inconsistency");
240 avc->f.states |= CVFlushed;
242 /* This should put it back on the vnode free list since usecount is 1 */
244 if (VREFCOUNT_GT(avc,0)) {
245 AFS_RELE(AFSTOV(avc));
246 afs_stats_cmperf.vcacheXAllocs--;
248 if (afs_norefpanic) {
249 afs_warn("flush vc refcnt < 1");
252 osi_Panic("flush vc refcnt < 1");
254 #endif /* AFS_LINUX22_ENV */
259 } /*afs_FlushVCache */
263 * The core of the inactive vnode op for all but IRIX.
269 afs_InactiveVCache(struct vcache *avc, afs_ucred_t *acred)
271 AFS_STATCNT(afs_inactive);
272 if (avc->f.states & CDirty) {
273 /* we can't keep trying to push back dirty data forever. Give up. */
274 afs_InvalidateAllSegments(avc); /* turns off dirty bit */
276 avc->f.states &= ~CMAPPED; /* mainly used by SunOS 4.0.x */
277 avc->f.states &= ~CDirty; /* Turn it off */
278 if (avc->f.states & CUnlinked) {
279 if (CheckLock(&afs_xvcache) || CheckLock(&afs_xdcache)) {
280 avc->f.states |= CUnlinkedDel;
283 afs_remunlink(avc, 1); /* ignore any return code */
290 * Allocate a callback return structure from the
291 * free list and return it.
293 * Environment: The alloc and free routines are both called with the afs_xvcb lock
294 * held, so we don't have to worry about blocking in osi_Alloc.
296 * \return The allocated afs_cbr.
298 static struct afs_cbr *afs_cbrSpace = 0;
299 /* if alloc limit below changes, fix me! */
300 static struct afs_cbr *afs_cbrHeads[2];
304 register struct afs_cbr *tsp;
307 while (!afs_cbrSpace) {
308 if (afs_stats_cmperf.CallBackAlloced >= 2) {
309 /* don't allocate more than 2 * AFS_NCBRS for now */
311 afs_stats_cmperf.CallBackFlushes++;
315 (struct afs_cbr *)afs_osi_Alloc(AFS_NCBRS *
316 sizeof(struct afs_cbr));
317 for (i = 0; i < AFS_NCBRS - 1; i++) {
318 tsp[i].next = &tsp[i + 1];
320 tsp[AFS_NCBRS - 1].next = 0;
322 afs_cbrHeads[afs_stats_cmperf.CallBackAlloced] = tsp;
323 afs_stats_cmperf.CallBackAlloced++;
327 afs_cbrSpace = tsp->next;
332 * Free a callback return structure, removing it from all lists.
334 * Environment: the xvcb lock is held over these calls.
336 * \param asp The address of the structure to free.
341 afs_FreeCBR(register struct afs_cbr *asp)
343 *(asp->pprev) = asp->next;
345 asp->next->pprev = asp->pprev;
347 *(asp->hash_pprev) = asp->hash_next;
349 asp->hash_next->hash_pprev = asp->hash_pprev;
351 asp->next = afs_cbrSpace;
357 * Flush all queued callbacks to all servers.
359 * Environment: holds xvcb lock over RPC to guard against race conditions
360 * when a new callback is granted for the same file later on.
362 * \return 0 for success.
365 afs_FlushVCBs(afs_int32 lockit)
367 struct AFSFid *tfids;
368 struct AFSCallBack callBacks[1];
369 struct AFSCBFids fidArray;
370 struct AFSCBs cbArray;
372 struct afs_cbr *tcbrp;
376 struct vrequest treq;
378 int safety1, safety2, safety3;
380 if ((code = afs_InitReq(&treq, afs_osi_credp)))
382 treq.flags |= O_NONBLOCK;
383 tfids = afs_osi_Alloc(sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
386 ObtainWriteLock(&afs_xvcb, 273);
387 ObtainReadLock(&afs_xserver);
388 for (i = 0; i < NSERVERS; i++) {
389 for (safety1 = 0, tsp = afs_servers[i];
390 tsp && safety1 < afs_totalServers + 10;
391 tsp = tsp->next, safety1++) {
393 if (tsp->cbrs == (struct afs_cbr *)0)
396 /* otherwise, grab a block of AFS_MAXCBRSCALL from the list
397 * and make an RPC, over and over again.
399 tcount = 0; /* number found so far */
400 for (safety2 = 0; safety2 < afs_cacheStats; safety2++) {
401 if (tcount >= AFS_MAXCBRSCALL || !tsp->cbrs) {
402 /* if buffer is full, or we've queued all we're going
403 * to from this server, we should flush out the
406 fidArray.AFSCBFids_len = tcount;
407 fidArray.AFSCBFids_val = (struct AFSFid *)tfids;
408 cbArray.AFSCBs_len = 1;
409 cbArray.AFSCBs_val = callBacks;
410 memset(&callBacks[0], 0, sizeof(callBacks[0]));
411 callBacks[0].CallBackType = CB_EXCLUSIVE;
412 for (safety3 = 0; safety3 < AFS_MAXHOSTS * 2; safety3++) {
413 tc = afs_ConnByHost(tsp, tsp->cell->fsport,
414 tsp->cell->cellNum, &treq, 0,
418 (AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS);
421 RXAFS_GiveUpCallBacks(tc->id, &fidArray,
429 AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS, SHARED_LOCK,
434 /* ignore return code, since callbacks may have
435 * been returned anyway, we shouldn't leave them
436 * around to be returned again.
438 * Next, see if we are done with this server, and if so,
439 * break to deal with the next one.
445 /* if to flush full buffer */
446 /* if we make it here, we have an entry at the head of cbrs,
447 * which we should copy to the file ID array and then free.
450 tfids[tcount++] = tcbrp->fid;
452 /* Freeing the CBR will unlink it from the server's CBR list */
454 } /* while loop for this one server */
455 if (safety2 > afs_cacheStats) {
456 afs_warn("possible internal error afs_flushVCBs (%d)\n",
459 } /* for loop for this hash chain */
460 } /* loop through all hash chains */
461 if (safety1 > afs_totalServers + 2) {
463 ("AFS internal error (afs_flushVCBs) (%d > %d), continuing...\n",
464 safety1, afs_totalServers + 2);
466 osi_Panic("afs_flushVCBS safety1");
469 ReleaseReadLock(&afs_xserver);
471 ReleaseWriteLock(&afs_xvcb);
472 afs_osi_Free(tfids, sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
477 * Queue a callback on the given fid.
480 * Locks the xvcb lock.
481 * Called when the xvcache lock is already held.
483 * \param avc vcache entry
484 * \return 1 if queued, 0 otherwise
488 afs_QueueVCB(struct vcache *avc)
492 struct afs_cbr *tcbp;
494 AFS_STATCNT(afs_QueueVCB);
496 ObtainWriteLock(&afs_xvcb, 274);
498 /* we can't really give back callbacks on RO files, since the
499 * server only tracks them on a per-volume basis, and we don't
500 * know whether we still have some other files from the same
502 if (!((avc->f.states & CRO) == 0 && avc->callback)) {
506 /* The callback is really just a struct server ptr. */
507 tsp = (struct server *)(avc->callback);
509 /* we now have a pointer to the server, so we just allocate
510 * a queue entry and queue it.
512 tcbp = afs_AllocCBR();
513 tcbp->fid = avc->f.fid.Fid;
515 tcbp->next = tsp->cbrs;
517 tsp->cbrs->pprev = &tcbp->next;
520 tcbp->pprev = &tsp->cbrs;
522 afs_InsertHashCBR(tcbp);
526 /* now release locks and return */
527 ReleaseWriteLock(&afs_xvcb);
533 * Remove a queued callback for a given Fid.
536 * Locks xvcb and xserver locks.
537 * Typically called with xdcache, xvcache and/or individual vcache
540 * \param afid The fid we want cleansed of queued callbacks.
545 afs_RemoveVCB(struct VenusFid *afid)
548 struct afs_cbr *cbr, *ncbr;
550 AFS_STATCNT(afs_RemoveVCB);
551 ObtainWriteLock(&afs_xvcb, 275);
553 slot = afs_HashCBRFid(&afid->Fid);
554 ncbr = afs_cbrHashT[slot];
558 ncbr = cbr->hash_next;
560 if (afid->Fid.Volume == cbr->fid.Volume &&
561 afid->Fid.Vnode == cbr->fid.Vnode &&
562 afid->Fid.Unique == cbr->fid.Unique) {
567 ReleaseWriteLock(&afs_xvcb);
571 afs_FlushReclaimedVcaches(void)
573 #if !defined(AFS_LINUX22_ENV)
576 struct vcache *tmpReclaimedVCList = NULL;
578 ObtainWriteLock(&afs_xvreclaim, 76);
579 while (ReclaimedVCList) {
580 tvc = ReclaimedVCList; /* take from free list */
581 ReclaimedVCList = tvc->nextfree;
582 tvc->nextfree = NULL;
583 code = afs_FlushVCache(tvc, &fv_slept);
585 /* Ok, so, if we got code != 0, uh, wtf do we do? */
586 /* Probably, build a temporary list and then put all back when we
587 get to the end of the list */
588 /* This is actually really crappy, but we need to not leak these.
589 We probably need a way to be smarter about this. */
590 tvc->nextfree = tmpReclaimedVCList;
591 tmpReclaimedVCList = tvc;
592 /* printf("Reclaim list flush %lx failed: %d\n", (unsigned long) tvc, code); */
594 if (tvc->f.states & (CVInit
595 #ifdef AFS_DARWIN80_ENV
599 tvc->f.states &= ~(CVInit
600 #ifdef AFS_DARWIN80_ENV
604 afs_osi_Wakeup(&tvc->f.states);
607 if (tmpReclaimedVCList)
608 ReclaimedVCList = tmpReclaimedVCList;
610 ReleaseWriteLock(&afs_xvreclaim);
615 afs_PostPopulateVCache(struct vcache *avc, struct VenusFid *afid, int seq)
618 * The proper value for mvstat (for root fids) is setup by the caller.
621 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
624 if (afs_globalVFS == 0)
625 osi_Panic("afs globalvfs");
627 osi_PostPopulateVCache(avc);
630 osi_dnlc_purgedp(avc); /* this may be overkill */
631 memset(&(avc->callsort), 0, sizeof(struct afs_q));
633 avc->f.states &=~ CVInit;
635 avc->f.states |= CBulkFetching;
636 avc->f.m.Length = seq;
638 afs_osi_Wakeup(&avc->f.states);
642 afs_ShakeLooseVCaches(afs_int32 anumber)
646 struct afs_q *tq, *uq;
648 afs_int32 target = anumber;
652 for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
655 if (tvc->f.states & CVFlushed) {
656 refpanic("CVFlushed on VLRU");
657 /* In the other path, this was 2 * afs_cacheStats */
658 } else if (!afsd_dynamic_vcaches && i++ > afs_maxvcount) {
659 refpanic("Exceeded pool of AFS vnodes(VLRU cycle?)");
660 } else if (QNext(uq) != tq) {
661 refpanic("VLRU inconsistent");
665 if (osi_TryEvictVCache(tvc, &fv_slept))
673 continue; /* start over - may have raced. */
678 if (!afsd_dynamic_vcaches && anumber == target) {
679 afs_warn("afs_ShakeLooseVCaches: warning none freed, using %d of %d\n",
680 afs_vcount, afs_maxvcount);
686 /* Alloc new vnode. */
688 static struct vcache *
689 afs_AllocVCache(void)
693 tvc = osi_NewVnode();
698 if (afsd_dynamic_vcaches && afs_maxvcount < afs_vcount) {
699 afs_maxvcount = afs_vcount;
700 /*printf("peak vnodes: %d\n", afs_maxvcount);*/
703 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
705 #ifdef AFS_DISCON_ENV
706 /* If we create a new inode, we either give it a new slot number,
707 * or if one's available, use a slot number from the slot free list
709 if (afs_freeSlotList != NULL) {
710 struct afs_slotlist *tmp;
712 tvc->diskSlot = afs_freeSlotList->slot;
713 tmp = afs_freeSlotList;
714 afs_freeSlotList = tmp->next;
715 afs_osi_Free(tmp, sizeof(struct afs_slotlist));
717 tvc->diskSlot = afs_nextVcacheSlot++;
724 /* Pre populate a newly allocated vcache. On platforms where the actual
725 * vnode is attached to the vcache, this function is called before attachment,
726 * therefore it cannot perform any actions on the vnode itself */
729 afs_PrePopulateVCache(struct vcache *avc, struct VenusFid *afid,
730 struct server *serverp) {
732 #if defined(AFS_DISCON_ENV)
734 slot = avc->diskSlot;
737 osi_PrePopulateVCache(avc);
739 #if defined(AFS_DISCON_ENV)
740 avc->diskSlot = slot;
741 QZero(&avc->metadirty);
744 AFS_RWLOCK_INIT(&avc->lock, "vcache lock");
747 avc->linkData = NULL;
750 avc->execsOrWriters = 0;
752 avc->f.states = CVInit;
753 avc->last_looker = 0;
755 avc->asynchrony = -1;
759 avc->f.truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
760 hzero(avc->f.m.DataVersion); /* in case we copy it into flushDV */
762 avc->callback = serverp; /* to minimize chance that clear
765 #if defined(AFS_CACHE_BYPASS)
766 avc->cachingStates = 0;
767 avc->cachingTransitions = 0;
772 * This routine is responsible for allocating a new cache entry
773 * from the free list. It formats the cache entry and inserts it
774 * into the appropriate hash tables. It must be called with
775 * afs_xvcache write-locked so as to prevent several processes from
776 * trying to create a new cache entry simultaneously.
778 * LOCK: afs_NewVCache afs_xvcache W
780 * \param afid The file id of the file whose cache entry is being created.
782 * \return The new vcache struct.
785 static_inline struct vcache *
786 afs_NewVCache_int(struct VenusFid *afid, struct server *serverp, int seq)
790 afs_int32 anumber = VCACHE_FREE;
792 AFS_STATCNT(afs_NewVCache);
794 afs_FlushReclaimedVcaches();
796 #if defined(AFS_LINUX22_ENV)
797 if(!afsd_dynamic_vcaches) {
798 afs_ShakeLooseVCaches(anumber);
799 if (afs_vcount >= afs_maxvcount) {
800 afs_warn("afs_NewVCache - none freed\n");
804 tvc = afs_AllocVCache();
805 #else /* AFS_LINUX22_ENV */
806 /* pull out a free cache entry */
808 afs_ShakeLooseVCaches(anumber);
812 tvc = afs_AllocVCache();
814 tvc = freeVCList; /* take from free list */
815 freeVCList = tvc->nextfree;
816 tvc->nextfree = NULL;
817 } /* end of if (!freeVCList) */
819 #endif /* AFS_LINUX22_ENV */
821 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
823 panic("afs_NewVCache(): free vcache with vnode attached");
826 /* Populate the vcache with as much as we can. */
827 afs_PrePopulateVCache(tvc, afid, serverp);
829 /* Thread the vcache onto the VLRU */
834 tvc->hnext = afs_vhashT[i];
836 QAdd(&afs_vhashTV[j], &tvc->vhashq);
838 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
839 refpanic("NewVCache VLRU inconsistent");
841 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
842 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
843 refpanic("NewVCache VLRU inconsistent2");
845 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
846 refpanic("NewVCache VLRU inconsistent3");
848 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
849 refpanic("NewVCache VLRU inconsistent4");
853 /* it should now be safe to drop the xvcache lock - so attach an inode
854 * to this vcache, where necessary */
855 osi_AttachVnode(tvc, seq);
857 /* Get a reference count to hold this vcache for the VLRUQ. Note that
858 * we have to do this after attaching the vnode, because the reference
859 * count may be held in the vnode itself */
861 #if defined(AFS_LINUX22_ENV)
862 /* Hold it for the LRU (should make count 2) */
863 VN_HOLD(AFSTOV(tvc));
864 #elif !(defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV))
865 VREFCOUNT_SET(tvc, 1); /* us */
868 afs_PostPopulateVCache(tvc, afid, seq);
875 afs_NewVCache(struct VenusFid *afid, struct server *serverp)
877 return afs_NewVCache_int(afid, serverp, 0);
881 afs_NewBulkVCache(struct VenusFid *afid, struct server *serverp, int seq)
883 return afs_NewVCache_int(afid, serverp, seq);
889 * LOCK: afs_FlushActiveVcaches afs_xvcache N
891 * \param doflocks : Do we handle flocks?
894 afs_FlushActiveVcaches(register afs_int32 doflocks)
896 register struct vcache *tvc;
898 register struct afs_conn *tc;
899 register afs_int32 code;
900 afs_ucred_t *cred = NULL;
901 struct vrequest treq, ureq;
902 struct AFSVolSync tsync;
905 AFS_STATCNT(afs_FlushActiveVcaches);
906 ObtainReadLock(&afs_xvcache);
907 for (i = 0; i < VCSIZE; i++) {
908 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
909 if (tvc->f.states & CVInit) continue;
910 #ifdef AFS_DARWIN80_ENV
911 if (tvc->f.states & CDeadVnode &&
912 (tvc->f.states & (CCore|CUnlinkedDel) ||
913 tvc->flockCount)) panic("Dead vnode has core/unlinkedel/flock");
915 if (doflocks && tvc->flockCount != 0) {
916 /* if this entry has an flock, send a keep-alive call out */
918 ReleaseReadLock(&afs_xvcache);
919 ObtainWriteLock(&tvc->lock, 51);
921 afs_InitReq(&treq, afs_osi_credp);
922 treq.flags |= O_NONBLOCK;
924 tc = afs_Conn(&tvc->f.fid, &treq, SHARED_LOCK);
926 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
929 RXAFS_ExtendLock(tc->id,
930 (struct AFSFid *)&tvc->f.fid.Fid,
937 (tc, code, &tvc->f.fid, &treq,
938 AFS_STATS_FS_RPCIDX_EXTENDLOCK, SHARED_LOCK, NULL));
940 ReleaseWriteLock(&tvc->lock);
941 #ifdef AFS_DARWIN80_ENV
943 ObtainReadLock(&afs_xvcache);
945 ObtainReadLock(&afs_xvcache);
950 if ((tvc->f.states & CCore) || (tvc->f.states & CUnlinkedDel)) {
952 * Don't let it evaporate in case someone else is in
953 * this code. Also, drop the afs_xvcache lock while
954 * getting vcache locks.
957 ReleaseReadLock(&afs_xvcache);
958 #ifdef AFS_BOZONLOCK_ENV
959 afs_BozonLock(&tvc->pvnLock, tvc);
961 #if defined(AFS_SGI_ENV)
963 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
965 osi_Assert(VREFCOUNT_GT(tvc,0));
966 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
968 ObtainWriteLock(&tvc->lock, 52);
969 if (tvc->f.states & CCore) {
970 tvc->f.states &= ~CCore;
971 /* XXXX Find better place-holder for cred XXXX */
972 cred = (afs_ucred_t *)tvc->linkData;
973 tvc->linkData = NULL; /* XXX */
974 afs_InitReq(&ureq, cred);
975 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
976 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
977 tvc->execsOrWriters);
978 code = afs_StoreOnLastReference(tvc, &ureq);
979 ReleaseWriteLock(&tvc->lock);
980 #ifdef AFS_BOZONLOCK_ENV
981 afs_BozonUnlock(&tvc->pvnLock, tvc);
986 if (code && code != VNOVNODE) {
987 afs_StoreWarn(code, tvc->f.fid.Fid.Volume,
988 /* /dev/console */ 1);
990 } else if (tvc->f.states & CUnlinkedDel) {
994 ReleaseWriteLock(&tvc->lock);
995 #ifdef AFS_BOZONLOCK_ENV
996 afs_BozonUnlock(&tvc->pvnLock, tvc);
998 #if defined(AFS_SGI_ENV)
999 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1001 afs_remunlink(tvc, 0);
1002 #if defined(AFS_SGI_ENV)
1003 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1006 /* lost (or won, perhaps) the race condition */
1007 ReleaseWriteLock(&tvc->lock);
1008 #ifdef AFS_BOZONLOCK_ENV
1009 afs_BozonUnlock(&tvc->pvnLock, tvc);
1012 #if defined(AFS_SGI_ENV)
1013 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1015 #ifdef AFS_DARWIN80_ENV
1018 AFS_RELE(AFSTOV(tvc));
1019 /* Matches write code setting CCore flag */
1022 ObtainReadLock(&afs_xvcache);
1024 ObtainReadLock(&afs_xvcache);
1027 AFS_RELE(AFSTOV(tvc));
1028 /* Matches write code setting CCore flag */
1035 ReleaseReadLock(&afs_xvcache);
1041 * Make sure a cache entry is up-to-date status-wise.
1043 * NOTE: everywhere that calls this can potentially be sped up
1044 * by checking CStatd first, and avoiding doing the InitReq
1045 * if this is up-to-date.
1047 * Anymore, the only places that call this KNOW already that the
1048 * vcache is not up-to-date, so we don't screw around.
1050 * \param avc : Ptr to vcache entry to verify.
1056 * Make sure a cache entry is up-to-date status-wise.
1058 * NOTE: everywhere that calls this can potentially be sped up
1059 * by checking CStatd first, and avoiding doing the InitReq
1060 * if this is up-to-date.
1062 * Anymore, the only places that call this KNOW already that the
1063 * vcache is not up-to-date, so we don't screw around.
1065 * \param avc Pointer to vcache entry to verify.
1068 * \return 0 for success or other error codes.
1071 afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1073 register struct vcache *tvc;
1075 AFS_STATCNT(afs_VerifyVCache);
1077 /* otherwise we must fetch the status info */
1079 ObtainWriteLock(&avc->lock, 53);
1080 if (avc->f.states & CStatd) {
1081 ReleaseWriteLock(&avc->lock);
1084 ObtainWriteLock(&afs_xcbhash, 461);
1085 avc->f.states &= ~(CStatd | CUnique);
1086 avc->callback = NULL;
1087 afs_DequeueCallback(avc);
1088 ReleaseWriteLock(&afs_xcbhash);
1089 ReleaseWriteLock(&avc->lock);
1091 /* since we've been called back, or the callback has expired,
1092 * it's possible that the contents of this directory, or this
1093 * file's name have changed, thus invalidating the dnlc contents.
1095 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
1096 osi_dnlc_purgedp(avc);
1098 osi_dnlc_purgevp(avc);
1100 /* fetch the status info */
1101 tvc = afs_GetVCache(&avc->f.fid, areq, NULL, avc);
1104 /* Put it back; caller has already incremented vrefCount */
1108 } /*afs_VerifyVCache */
1112 * Simple copy of stat info into cache.
1114 * Callers:as of 1992-04-29, only called by WriteVCache
1116 * \param avc Ptr to vcache entry involved.
1117 * \param astat Ptr to stat info to copy.
1121 afs_SimpleVStat(register struct vcache *avc,
1122 register struct AFSFetchStatus *astat, struct vrequest *areq)
1125 AFS_STATCNT(afs_SimpleVStat);
1127 #ifdef AFS_64BIT_CLIENT
1128 FillInt64(length, astat->Length_hi, astat->Length);
1129 #else /* AFS_64BIT_CLIENT */
1130 length = astat->Length;
1131 #endif /* AFS_64BIT_CLIENT */
1133 #if defined(AFS_SGI_ENV)
1134 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1135 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1136 osi_Assert((valusema(&avc->vc_rwlock) <= 0)
1137 && (OSI_GET_LOCKID() == avc->vc_rwlockid));
1138 if (length < avc->f.m.Length) {
1139 vnode_t *vp = (vnode_t *) avc;
1141 osi_Assert(WriteLocked(&avc->lock));
1142 ReleaseWriteLock(&avc->lock);
1144 PTOSSVP(vp, (off_t) length, (off_t) MAXLONG);
1146 ObtainWriteLock(&avc->lock, 67);
1151 if (!afs_DirtyPages(avc)) {
1152 /* if actively writing the file, don't fetch over this value */
1153 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT, ICL_TYPE_POINTER, avc,
1154 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1155 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1156 avc->f.m.Length = length;
1157 avc->f.m.Date = astat->ClientModTime;
1159 avc->f.m.Owner = astat->Owner;
1160 avc->f.m.Group = astat->Group;
1161 avc->f.m.Mode = astat->UnixModeBits;
1162 if (vType(avc) == VREG) {
1163 avc->f.m.Mode |= S_IFREG;
1164 } else if (vType(avc) == VDIR) {
1165 avc->f.m.Mode |= S_IFDIR;
1166 } else if (vType(avc) == VLNK) {
1167 avc->f.m.Mode |= S_IFLNK;
1168 if ((avc->f.m.Mode & 0111) == 0)
1171 if (avc->f.states & CForeign) {
1172 struct axscache *ac;
1173 avc->f.anyAccess = astat->AnonymousAccess;
1175 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1177 * Caller has at least one bit not covered by anonymous, and
1178 * thus may have interesting rights.
1180 * HOWEVER, this is a really bad idea, because any access query
1181 * for bits which aren't covered by anonymous, on behalf of a user
1182 * who doesn't have any special rights, will result in an answer of
1183 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1184 * It's an especially bad idea under Ultrix, since (due to the lack of
1185 * a proper access() call) it must perform several afs_access() calls
1186 * in order to create magic mode bits that vary according to who makes
1187 * the call. In other words, _every_ stat() generates a test for
1190 #endif /* badidea */
1191 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1192 ac->axess = astat->CallerAccess;
1193 else /* not found, add a new one if possible */
1194 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1197 } /*afs_SimpleVStat */
1201 * Store the status info *only* back to the server for a
1204 * Environment: Must be called with a shared lock held on the vnode.
1206 * \param avc Ptr to the vcache entry.
1207 * \param astatus Ptr to the status info to store.
1208 * \param areq Ptr to the associated vrequest.
1210 * \return Operation status.
1214 afs_WriteVCache(register struct vcache *avc,
1215 register struct AFSStoreStatus *astatus,
1216 struct vrequest *areq)
1219 struct afs_conn *tc;
1220 struct AFSFetchStatus OutStatus;
1221 struct AFSVolSync tsync;
1223 AFS_STATCNT(afs_WriteVCache);
1224 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1225 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
1227 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK);
1229 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1232 RXAFS_StoreStatus(tc->id, (struct AFSFid *)&avc->f.fid.Fid,
1233 astatus, &OutStatus, &tsync);
1238 } while (afs_Analyze
1239 (tc, code, &avc->f.fid, areq, AFS_STATS_FS_RPCIDX_STORESTATUS,
1240 SHARED_LOCK, NULL));
1242 UpgradeSToWLock(&avc->lock, 20);
1244 /* success, do the changes locally */
1245 afs_SimpleVStat(avc, &OutStatus, areq);
1247 * Update the date, too. SimpleVStat didn't do this, since
1248 * it thought we were doing this after fetching new status
1249 * over a file being written.
1251 avc->f.m.Date = OutStatus.ClientModTime;
1253 /* failure, set up to check with server next time */
1254 ObtainWriteLock(&afs_xcbhash, 462);
1255 afs_DequeueCallback(avc);
1256 avc->f.states &= ~(CStatd | CUnique); /* turn off stat valid flag */
1257 ReleaseWriteLock(&afs_xcbhash);
1258 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
1259 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
1261 ConvertWToSLock(&avc->lock);
1264 } /*afs_WriteVCache */
1265 #if defined(AFS_DISCON_ENV)
1268 * Store status info only locally, set the proper disconnection flags
1269 * and add to dirty list.
1271 * \param avc The vcache to be written locally.
1272 * \param astatus Get attr fields from local store.
1273 * \param attrs This one is only of the vs_size.
1275 * \note Must be called with a shared lock on the vnode
1278 afs_WriteVCacheDiscon(register struct vcache *avc,
1279 register struct AFSStoreStatus *astatus,
1280 struct vattr *attrs)
1283 afs_int32 flags = 0;
1285 UpgradeSToWLock(&avc->lock, 700);
1287 if (!astatus->Mask) {
1293 /* Set attributes. */
1294 if (astatus->Mask & AFS_SETMODTIME) {
1295 avc->f.m.Date = astatus->ClientModTime;
1296 flags |= VDisconSetTime;
1299 if (astatus->Mask & AFS_SETOWNER) {
1300 /* printf("Not allowed yet. \n"); */
1301 /*avc->f.m.Owner = astatus->Owner;*/
1304 if (astatus->Mask & AFS_SETGROUP) {
1305 /* printf("Not allowed yet. \n"); */
1306 /*avc->f.m.Group = astatus->Group;*/
1309 if (astatus->Mask & AFS_SETMODE) {
1310 avc->f.m.Mode = astatus->UnixModeBits;
1312 #if 0 /* XXX: Leaving this out, so it doesn't mess up the file type flag.*/
1314 if (vType(avc) == VREG) {
1315 avc->f.m.Mode |= S_IFREG;
1316 } else if (vType(avc) == VDIR) {
1317 avc->f.m.Mode |= S_IFDIR;
1318 } else if (vType(avc) == VLNK) {
1319 avc->f.m.Mode |= S_IFLNK;
1320 if ((avc->f.m.Mode & 0111) == 0)
1324 flags |= VDisconSetMode;
1325 } /* if(astatus.Mask & AFS_SETMODE) */
1327 } /* if (!astatus->Mask) */
1329 if (attrs->va_size > 0) {
1330 /* XXX: Do I need more checks? */
1331 /* Truncation operation. */
1332 flags |= VDisconTrunc;
1336 afs_DisconAddDirty(avc, flags, 1);
1338 /* XXX: How about the rest of the fields? */
1340 ConvertWToSLock(&avc->lock);
1348 * Copy astat block into vcache info
1350 * \note This code may get dataversion and length out of sync if the file has
1351 * been modified. This is less than ideal. I haven't thought about it sufficiently
1352 * to be certain that it is adequate.
1354 * \note Environment: Must be called under a write lock
1356 * \param avc Ptr to vcache entry.
1357 * \param astat Ptr to stat block to copy in.
1358 * \param areq Ptr to associated request.
1361 afs_ProcessFS(register struct vcache *avc,
1362 register struct AFSFetchStatus *astat, struct vrequest *areq)
1365 AFS_STATCNT(afs_ProcessFS);
1367 #ifdef AFS_64BIT_CLIENT
1368 FillInt64(length, astat->Length_hi, astat->Length);
1369 #else /* AFS_64BIT_CLIENT */
1370 length = astat->Length;
1371 #endif /* AFS_64BIT_CLIENT */
1372 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1373 * number for each bulk status request. Under no circumstances
1374 * should afs_DoBulkStat store a sequence number if the new
1375 * length will be ignored when afs_ProcessFS is called with
1376 * new stats. If you change the following conditional then you
1377 * also need to change the conditional in afs_DoBulkStat. */
1379 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1380 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1382 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1384 /* if we're writing or mapping this file, don't fetch over these
1387 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1388 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1389 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1390 avc->f.m.Length = length;
1391 avc->f.m.Date = astat->ClientModTime;
1393 hset64(avc->f.m.DataVersion, astat->dataVersionHigh, astat->DataVersion);
1394 avc->f.m.Owner = astat->Owner;
1395 avc->f.m.Mode = astat->UnixModeBits;
1396 avc->f.m.Group = astat->Group;
1397 avc->f.m.LinkCount = astat->LinkCount;
1398 if (astat->FileType == File) {
1399 vSetType(avc, VREG);
1400 avc->f.m.Mode |= S_IFREG;
1401 } else if (astat->FileType == Directory) {
1402 vSetType(avc, VDIR);
1403 avc->f.m.Mode |= S_IFDIR;
1404 } else if (astat->FileType == SymbolicLink) {
1405 if (afs_fakestat_enable && (avc->f.m.Mode & 0111) == 0) {
1406 vSetType(avc, VDIR);
1407 avc->f.m.Mode |= S_IFDIR;
1409 vSetType(avc, VLNK);
1410 avc->f.m.Mode |= S_IFLNK;
1412 if ((avc->f.m.Mode & 0111) == 0) {
1416 avc->f.anyAccess = astat->AnonymousAccess;
1418 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1420 * Caller has at least one bit not covered by anonymous, and
1421 * thus may have interesting rights.
1423 * HOWEVER, this is a really bad idea, because any access query
1424 * for bits which aren't covered by anonymous, on behalf of a user
1425 * who doesn't have any special rights, will result in an answer of
1426 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1427 * It's an especially bad idea under Ultrix, since (due to the lack of
1428 * a proper access() call) it must perform several afs_access() calls
1429 * in order to create magic mode bits that vary according to who makes
1430 * the call. In other words, _every_ stat() generates a test for
1433 #endif /* badidea */
1435 struct axscache *ac;
1436 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1437 ac->axess = astat->CallerAccess;
1438 else /* not found, add a new one if possible */
1439 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1441 } /*afs_ProcessFS */
1445 * Get fid from server.
1448 * \param areq Request to be passed on.
1449 * \param name Name of ?? to lookup.
1450 * \param OutStatus Fetch status.
1455 * \return Success status of operation.
1458 afs_RemoteLookup(register struct VenusFid *afid, struct vrequest *areq,
1459 char *name, struct VenusFid *nfid,
1460 struct AFSFetchStatus *OutStatusp,
1461 struct AFSCallBack *CallBackp, struct server **serverp,
1462 struct AFSVolSync *tsyncp)
1466 register struct afs_conn *tc;
1467 struct AFSFetchStatus OutDirStatus;
1470 name = ""; /* XXX */
1472 tc = afs_Conn(afid, areq, SHARED_LOCK);
1475 *serverp = tc->srvr->server;
1477 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1480 RXAFS_Lookup(tc->id, (struct AFSFid *)&afid->Fid, name,
1481 (struct AFSFid *)&nfid->Fid, OutStatusp,
1482 &OutDirStatus, CallBackp, tsyncp);
1487 } while (afs_Analyze
1488 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_XLOOKUP, SHARED_LOCK,
1498 * Given a file id and a vrequest structure, fetch the status
1499 * information associated with the file.
1501 * \param afid File ID.
1502 * \param areq Ptr to associated vrequest structure, specifying the
1503 * user whose authentication tokens will be used.
1504 * \param avc Caller may already have a vcache for this file, which is
1507 * \note Environment:
1508 * The cache entry is returned with an increased vrefCount field.
1509 * The entry must be discarded by calling afs_PutVCache when you
1510 * are through using the pointer to the cache entry.
1512 * You should not hold any locks when calling this function, except
1513 * locks on other vcache entries. If you lock more than one vcache
1514 * entry simultaneously, you should lock them in this order:
1516 * 1. Lock all files first, then directories.
1517 * 2. Within a particular type, lock entries in Fid.Vnode order.
1519 * This locking hierarchy is convenient because it allows locking
1520 * of a parent dir cache entry, given a file (to check its access
1521 * control list). It also allows renames to be handled easily by
1522 * locking directories in a constant order.
1524 * \note NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1526 * \note Might have a vcache structure already, which must
1527 * already be held by the caller
1530 afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
1531 afs_int32 * cached, struct vcache *avc)
1534 afs_int32 code, newvcache = 0;
1535 register struct vcache *tvc;
1539 AFS_STATCNT(afs_GetVCache);
1542 *cached = 0; /* Init just in case */
1544 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1548 ObtainSharedLock(&afs_xvcache, 5);
1550 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU | IS_SLOCK);
1552 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1553 ReleaseSharedLock(&afs_xvcache);
1554 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1561 osi_Assert((tvc->f.states & CVInit) == 0);
1562 /* If we are in readdir, return the vnode even if not statd */
1563 if ((tvc->f.states & CStatd) || afs_InReadDir(tvc)) {
1564 ReleaseSharedLock(&afs_xvcache);
1568 UpgradeSToWLock(&afs_xvcache, 21);
1570 /* no cache entry, better grab one */
1571 tvc = afs_NewVCache(afid, NULL);
1574 ConvertWToSLock(&afs_xvcache);
1577 ReleaseSharedLock(&afs_xvcache);
1581 afs_stats_cmperf.vcacheMisses++;
1584 ReleaseSharedLock(&afs_xvcache);
1586 ObtainWriteLock(&tvc->lock, 54);
1588 if (tvc->f.states & CStatd) {
1589 ReleaseWriteLock(&tvc->lock);
1592 #ifdef AFS_DARWIN80_ENV
1593 /* Darwin 8.0 only has bufs in nfs, so we shouldn't have to worry about them.
1596 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
1598 * XXX - I really don't like this. Should try to understand better.
1599 * It seems that sometimes, when we get called, we already hold the
1600 * lock on the vnode (e.g., from afs_getattr via afs_VerifyVCache).
1601 * We can't drop the vnode lock, because that could result in a race.
1602 * Sometimes, though, we get here and don't hold the vnode lock.
1603 * I hate code paths that sometimes hold locks and sometimes don't.
1604 * In any event, the dodge we use here is to check whether the vnode
1605 * is locked, and if it isn't, then we gain and drop it around the call
1606 * to vinvalbuf; otherwise, we leave it alone.
1609 struct vnode *vp = AFSTOV(tvc);
1612 #if defined(AFS_DARWIN_ENV)
1613 iheldthelock = VOP_ISLOCKED(vp);
1615 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, current_proc());
1616 /* this is messy. we can call fsync which will try to reobtain this */
1617 if (VTOAFS(vp) == tvc)
1618 ReleaseWriteLock(&tvc->lock);
1619 if (UBCINFOEXISTS(vp)) {
1620 vinvalbuf(vp, V_SAVE, &afs_osi_cred, current_proc(), PINOD, 0);
1622 if (VTOAFS(vp) == tvc)
1623 ObtainWriteLock(&tvc->lock, 954);
1625 VOP_UNLOCK(vp, LK_EXCLUSIVE, current_proc());
1626 #elif defined(AFS_FBSD80_ENV)
1627 iheldthelock = VOP_ISLOCKED(vp);
1628 if (!iheldthelock) {
1629 /* nosleep/sleep lock order reversal */
1630 int glocked = ISAFS_GLOCK();
1633 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1637 vinvalbuf(vp, V_SAVE, PINOD, 0); /* changed late in 8.0-CURRENT */
1640 #elif defined(AFS_FBSD60_ENV)
1641 iheldthelock = VOP_ISLOCKED(vp, curthread);
1643 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1644 vinvalbuf(vp, V_SAVE, curthread, PINOD, 0);
1646 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1647 #elif defined(AFS_FBSD50_ENV)
1648 iheldthelock = VOP_ISLOCKED(vp, curthread);
1650 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1651 vinvalbuf(vp, V_SAVE, osi_curcred(), curthread, PINOD, 0);
1653 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1654 #elif defined(AFS_FBSD40_ENV)
1655 iheldthelock = VOP_ISLOCKED(vp, curproc);
1657 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1658 vinvalbuf(vp, V_SAVE, osi_curcred(), curproc, PINOD, 0);
1660 VOP_UNLOCK(vp, LK_EXCLUSIVE, curproc);
1661 #elif defined(AFS_OBSD_ENV)
1662 iheldthelock = VOP_ISLOCKED(vp, curproc);
1664 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1665 uvm_vnp_uncache(vp);
1667 VOP_UNLOCK(vp, 0, curproc);
1668 #elif defined(AFS_NBSD40_ENV)
1669 iheldthelock = VOP_ISLOCKED(vp);
1670 if (!iheldthelock) {
1671 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1673 uvm_vnp_uncache(vp);
1681 ObtainWriteLock(&afs_xcbhash, 464);
1682 tvc->f.states &= ~CUnique;
1684 afs_DequeueCallback(tvc);
1685 ReleaseWriteLock(&afs_xcbhash);
1687 /* It is always appropriate to throw away all the access rights? */
1688 afs_FreeAllAxs(&(tvc->Access));
1689 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1691 if ((tvp->states & VForeign)) {
1693 tvc->f.states |= CForeign;
1694 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1695 && (tvp->rootUnique == afid->Fid.Unique)) {
1699 if (tvp->states & VRO)
1700 tvc->f.states |= CRO;
1701 if (tvp->states & VBackup)
1702 tvc->f.states |= CBackup;
1703 /* now copy ".." entry back out of volume structure, if necessary */
1704 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1706 tvc->mvid = (struct VenusFid *)
1707 osi_AllocSmallSpace(sizeof(struct VenusFid));
1708 *tvc->mvid = tvp->dotdot;
1710 afs_PutVolume(tvp, READ_LOCK);
1714 afs_RemoveVCB(afid);
1716 struct AFSFetchStatus OutStatus;
1718 if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1719 afs_ProcessFS(tvc, &OutStatus, areq);
1720 tvc->f.states |= CStatd | CUnique;
1721 tvc->f.parent.vnode = OutStatus.ParentVnode;
1722 tvc->f.parent.unique = OutStatus.ParentUnique;
1726 if (AFS_IS_DISCONNECTED) {
1727 /* Nothing to do otherwise...*/
1729 /* printf("Network is down in afs_GetCache"); */
1731 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1733 /* For the NFS translator's benefit, make sure
1734 * non-directory vnodes always have their parent FID set
1735 * correctly, even when created as a result of decoding an
1736 * NFS filehandle. It would be nice to also do this for
1737 * directories, but we can't because the fileserver fills
1738 * in the FID of the directory itself instead of that of
1741 if (!code && OutStatus.FileType != Directory &&
1742 !tvc->f.parent.vnode) {
1743 tvc->f.parent.vnode = OutStatus.ParentVnode;
1744 tvc->f.parent.unique = OutStatus.ParentUnique;
1745 /* XXX - SXW - It's conceivable we should mark ourselves
1746 * as dirty again here, incase we've been raced
1747 * out of the FetchStatus call.
1754 ReleaseWriteLock(&tvc->lock);
1760 ReleaseWriteLock(&tvc->lock);
1763 } /*afs_GetVCache */
1768 * Lookup a vcache by fid. Look inside the cache first, if not
1769 * there, lookup the file on the server, and then get it's fresh
1774 * \param cached Is element cached? If NULL, don't answer.
1778 * \return The found element or NULL.
1781 afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1782 afs_int32 * cached, struct vcache *adp, char *aname)
1784 afs_int32 code, now, newvcache = 0;
1785 struct VenusFid nfid;
1786 register struct vcache *tvc;
1788 struct AFSFetchStatus OutStatus;
1789 struct AFSCallBack CallBack;
1790 struct AFSVolSync tsync;
1791 struct server *serverp = 0;
1795 AFS_STATCNT(afs_GetVCache);
1797 *cached = 0; /* Init just in case */
1799 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1803 ObtainReadLock(&afs_xvcache);
1804 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */ );
1807 ReleaseReadLock(&afs_xvcache);
1809 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1810 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1814 ObtainReadLock(&tvc->lock);
1816 if (tvc->f.states & CStatd) {
1820 ReleaseReadLock(&tvc->lock);
1823 tvc->f.states &= ~CUnique;
1825 ReleaseReadLock(&tvc->lock);
1827 ObtainReadLock(&afs_xvcache);
1830 ReleaseReadLock(&afs_xvcache);
1832 /* lookup the file */
1835 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
1837 if (AFS_IS_DISCONNECTED) {
1838 /* printf("Network is down in afs_LookupVcache\n"); */
1842 afs_RemoteLookup(&adp->f.fid, areq, aname, &nfid, &OutStatus,
1843 &CallBack, &serverp, &tsync);
1845 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1849 ObtainSharedLock(&afs_xvcache, 6);
1850 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU | IS_SLOCK/* no xstats now */ );
1852 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1853 ReleaseSharedLock(&afs_xvcache);
1854 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1860 /* no cache entry, better grab one */
1861 UpgradeSToWLock(&afs_xvcache, 22);
1862 tvc = afs_NewVCache(&nfid, serverp);
1864 ConvertWToSLock(&afs_xvcache);
1867 ReleaseSharedLock(&afs_xvcache);
1872 ReleaseSharedLock(&afs_xvcache);
1873 ObtainWriteLock(&tvc->lock, 55);
1875 /* It is always appropriate to throw away all the access rights? */
1876 afs_FreeAllAxs(&(tvc->Access));
1877 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
1879 if ((tvp->states & VForeign)) {
1881 tvc->f.states |= CForeign;
1882 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1883 && (tvp->rootUnique == afid->Fid.Unique))
1886 if (tvp->states & VRO)
1887 tvc->f.states |= CRO;
1888 if (tvp->states & VBackup)
1889 tvc->f.states |= CBackup;
1890 /* now copy ".." entry back out of volume structure, if necessary */
1891 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1893 tvc->mvid = (struct VenusFid *)
1894 osi_AllocSmallSpace(sizeof(struct VenusFid));
1895 *tvc->mvid = tvp->dotdot;
1900 ObtainWriteLock(&afs_xcbhash, 465);
1901 afs_DequeueCallback(tvc);
1902 tvc->f.states &= ~(CStatd | CUnique);
1903 ReleaseWriteLock(&afs_xcbhash);
1904 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
1905 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
1907 afs_PutVolume(tvp, READ_LOCK);
1908 ReleaseWriteLock(&tvc->lock);
1913 ObtainWriteLock(&afs_xcbhash, 466);
1914 if (origCBs == afs_allCBs) {
1915 if (CallBack.ExpirationTime) {
1916 tvc->callback = serverp;
1917 tvc->cbExpires = CallBack.ExpirationTime + now;
1918 tvc->f.states |= CStatd | CUnique;
1919 tvc->f.states &= ~CBulkFetching;
1920 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
1921 } else if (tvc->f.states & CRO) {
1922 /* adapt gives us an hour. */
1923 tvc->cbExpires = 3600 + osi_Time();
1924 /*XXX*/ tvc->f.states |= CStatd | CUnique;
1925 tvc->f.states &= ~CBulkFetching;
1926 afs_QueueCallback(tvc, CBHash(3600), tvp);
1928 tvc->callback = NULL;
1929 afs_DequeueCallback(tvc);
1930 tvc->f.states &= ~(CStatd | CUnique);
1931 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
1932 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
1935 afs_DequeueCallback(tvc);
1936 tvc->f.states &= ~CStatd;
1937 tvc->f.states &= ~CUnique;
1938 tvc->callback = NULL;
1939 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
1940 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
1942 ReleaseWriteLock(&afs_xcbhash);
1944 afs_PutVolume(tvp, READ_LOCK);
1945 afs_ProcessFS(tvc, &OutStatus, areq);
1947 ReleaseWriteLock(&tvc->lock);
1953 afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
1954 afs_int32 * cached, struct volume *tvolp)
1956 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
1957 afs_int32 getNewFid = 0;
1959 struct VenusFid nfid;
1960 register struct vcache *tvc;
1961 struct server *serverp = 0;
1962 struct AFSFetchStatus OutStatus;
1963 struct AFSCallBack CallBack;
1964 struct AFSVolSync tsync;
1966 #ifdef AFS_DARWIN80_ENV
1973 if (!tvolp->rootVnode || getNewFid) {
1974 struct VenusFid tfid;
1977 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
1978 origCBs = afs_allCBs; /* ignore InitCallBackState */
1980 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
1985 /* ReleaseReadLock(&tvolp->lock); */
1986 ObtainWriteLock(&tvolp->lock, 56);
1987 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
1988 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
1989 ReleaseWriteLock(&tvolp->lock);
1990 /* ObtainReadLock(&tvolp->lock);*/
1993 afid->Fid.Vnode = tvolp->rootVnode;
1994 afid->Fid.Unique = tvolp->rootUnique;
1998 ObtainSharedLock(&afs_xvcache, 7);
2000 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2001 if (!FidCmp(&(tvc->f.fid), afid)) {
2002 if (tvc->f.states & CVInit) {
2003 ReleaseSharedLock(&afs_xvcache);
2004 afs_osi_Sleep(&tvc->f.states);
2007 #ifdef AFS_DARWIN80_ENV
2008 if (tvc->f.states & CDeadVnode) {
2009 if (!(tvc->f.states & CBulkFetching)) {
2010 ReleaseSharedLock(&afs_xvcache);
2011 afs_osi_Sleep(&tvc->f.states);
2016 if (vnode_get(tvp)) /* this bumps ref count */
2018 if (vnode_ref(tvp)) {
2020 /* AFSTOV(tvc) may be NULL */
2025 if (tvc->f.states & (CBulkFetching|CDeadVnode)) {
2027 vnode_recycle(AFSTOV(tvc));
2035 if (!haveStatus && (!tvc || !(tvc->f.states & CStatd))) {
2036 /* Mount point no longer stat'd or unknown. FID may have changed. */
2038 ReleaseSharedLock(&afs_xvcache);
2039 #ifdef AFS_DARWIN80_ENV
2042 vnode_put(AFSTOV(tvc));
2043 vnode_rele(AFSTOV(tvc));
2052 UpgradeSToWLock(&afs_xvcache, 23);
2053 /* no cache entry, better grab one */
2054 tvc = afs_NewVCache(afid, NULL);
2057 ReleaseWriteLock(&afs_xvcache);
2061 afs_stats_cmperf.vcacheMisses++;
2065 afs_stats_cmperf.vcacheHits++;
2066 #if defined(AFS_DARWIN80_ENV)
2067 /* we already bumped the ref count in the for loop above */
2068 #else /* AFS_DARWIN80_ENV */
2071 UpgradeSToWLock(&afs_xvcache, 24);
2072 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2073 refpanic("GRVC VLRU inconsistent0");
2075 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2076 refpanic("GRVC VLRU inconsistent1");
2078 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2079 refpanic("GRVC VLRU inconsistent2");
2081 QRemove(&tvc->vlruq); /* move to lruq head */
2082 QAdd(&VLRU, &tvc->vlruq);
2083 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2084 refpanic("GRVC VLRU inconsistent3");
2086 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2087 refpanic("GRVC VLRU inconsistent4");
2089 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2090 refpanic("GRVC VLRU inconsistent5");
2095 ReleaseWriteLock(&afs_xvcache);
2097 if (tvc->f.states & CStatd) {
2101 ObtainReadLock(&tvc->lock);
2102 tvc->f.states &= ~CUnique;
2103 tvc->callback = NULL; /* redundant, perhaps */
2104 ReleaseReadLock(&tvc->lock);
2107 ObtainWriteLock(&tvc->lock, 57);
2109 /* It is always appropriate to throw away all the access rights? */
2110 afs_FreeAllAxs(&(tvc->Access));
2113 tvc->f.states |= CForeign;
2114 if (tvolp->states & VRO)
2115 tvc->f.states |= CRO;
2116 if (tvolp->states & VBackup)
2117 tvc->f.states |= CBackup;
2118 /* now copy ".." entry back out of volume structure, if necessary */
2119 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2120 && (tvolp->rootUnique == afid->Fid.Unique)) {
2123 if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2125 tvc->mvid = (struct VenusFid *)
2126 osi_AllocSmallSpace(sizeof(struct VenusFid));
2127 *tvc->mvid = tvolp->dotdot;
2131 afs_RemoveVCB(afid);
2134 struct VenusFid tfid;
2137 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2138 origCBs = afs_allCBs; /* ignore InitCallBackState */
2140 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2145 ObtainWriteLock(&afs_xcbhash, 467);
2146 afs_DequeueCallback(tvc);
2147 tvc->callback = NULL;
2148 tvc->f.states &= ~(CStatd | CUnique);
2149 ReleaseWriteLock(&afs_xcbhash);
2150 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2151 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2152 ReleaseWriteLock(&tvc->lock);
2157 ObtainWriteLock(&afs_xcbhash, 468);
2158 if (origCBs == afs_allCBs) {
2159 tvc->f.states |= CTruth;
2160 tvc->callback = serverp;
2161 if (CallBack.ExpirationTime != 0) {
2162 tvc->cbExpires = CallBack.ExpirationTime + start;
2163 tvc->f.states |= CStatd;
2164 tvc->f.states &= ~CBulkFetching;
2165 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2166 } else if (tvc->f.states & CRO) {
2167 /* adapt gives us an hour. */
2168 tvc->cbExpires = 3600 + osi_Time();
2169 /*XXX*/ tvc->f.states |= CStatd;
2170 tvc->f.states &= ~CBulkFetching;
2171 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2174 afs_DequeueCallback(tvc);
2175 tvc->callback = NULL;
2176 tvc->f.states &= ~(CStatd | CUnique);
2177 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2178 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2180 ReleaseWriteLock(&afs_xcbhash);
2181 afs_ProcessFS(tvc, &OutStatus, areq);
2183 ReleaseWriteLock(&tvc->lock);
2189 * Update callback status and (sometimes) attributes of a vnode.
2190 * Called after doing a fetch status RPC. Whilst disconnected, attributes
2191 * shouldn't be written to the vcache here.
2196 * \param Outsp Server status after rpc call.
2197 * \param acb Callback for this vnode.
2199 * \note The vcache must be write locked.
2202 afs_UpdateStatus(struct vcache *avc, struct VenusFid *afid,
2203 struct vrequest *areq, struct AFSFetchStatus *Outsp,
2204 struct AFSCallBack *acb, afs_uint32 start)
2206 struct volume *volp;
2209 /* Dont write status in vcache if resyncing after a disconnection. */
2210 afs_ProcessFS(avc, Outsp, areq);
2212 volp = afs_GetVolume(afid, areq, READ_LOCK);
2213 ObtainWriteLock(&afs_xcbhash, 469);
2214 avc->f.states |= CTruth;
2215 if (avc->callback /* check for race */ ) {
2216 if (acb->ExpirationTime != 0) {
2217 avc->cbExpires = acb->ExpirationTime + start;
2218 avc->f.states |= CStatd;
2219 avc->f.states &= ~CBulkFetching;
2220 afs_QueueCallback(avc, CBHash(acb->ExpirationTime), volp);
2221 } else if (avc->f.states & CRO) {
2222 /* ordinary callback on a read-only volume -- AFS 3.2 style */
2223 avc->cbExpires = 3600 + start;
2224 avc->f.states |= CStatd;
2225 avc->f.states &= ~CBulkFetching;
2226 afs_QueueCallback(avc, CBHash(3600), volp);
2228 afs_DequeueCallback(avc);
2229 avc->callback = NULL;
2230 avc->f.states &= ~(CStatd | CUnique);
2231 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
2232 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2235 afs_DequeueCallback(avc);
2236 avc->callback = NULL;
2237 avc->f.states &= ~(CStatd | CUnique);
2238 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
2239 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2241 ReleaseWriteLock(&afs_xcbhash);
2243 afs_PutVolume(volp, READ_LOCK);
2247 * Must be called with avc write-locked
2248 * don't absolutely have to invalidate the hint unless the dv has
2249 * changed, but be sure to get it right else there will be consistency bugs.
2252 afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
2253 struct vrequest * areq, struct AFSFetchStatus * Outsp)
2256 afs_uint32 start = 0;
2257 register struct afs_conn *tc;
2258 struct AFSCallBack CallBack;
2259 struct AFSVolSync tsync;
2262 tc = afs_Conn(afid, areq, SHARED_LOCK);
2263 avc->dchint = NULL; /* invalidate hints */
2265 avc->callback = tc->srvr->server;
2267 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2270 RXAFS_FetchStatus(tc->id, (struct AFSFid *)&afid->Fid, Outsp,
2278 } while (afs_Analyze
2279 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2280 SHARED_LOCK, NULL));
2283 afs_UpdateStatus(avc, afid, areq, Outsp, &CallBack, start);
2285 /* used to undo the local callback, but that's too extreme.
2286 * There are plenty of good reasons that fetchstatus might return
2287 * an error, such as EPERM. If we have the vnode cached, statd,
2288 * with callback, might as well keep track of the fact that we
2289 * don't have access...
2291 if (code == EPERM || code == EACCES) {
2292 struct axscache *ac;
2293 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2295 else /* not found, add a new one if possible */
2296 afs_AddAxs(avc->Access, areq->uid, 0);
2307 * Stuff some information into the vcache for the given file.
2310 * afid : File in question.
2311 * OutStatus : Fetch status on the file.
2312 * CallBack : Callback info.
2313 * tc : RPC connection involved.
2314 * areq : vrequest involved.
2317 * Nothing interesting.
2320 afs_StuffVcache(register struct VenusFid *afid,
2321 struct AFSFetchStatus *OutStatus,
2322 struct AFSCallBack *CallBack, register struct afs_conn *tc,
2323 struct vrequest *areq)
2325 register afs_int32 code, i, newvcache = 0;
2326 register struct vcache *tvc;
2327 struct AFSVolSync tsync;
2329 struct axscache *ac;
2332 AFS_STATCNT(afs_StuffVcache);
2333 #ifdef IFS_VCACHECOUNT
2338 ObtainSharedLock(&afs_xvcache, 8);
2340 tvc = afs_FindVCache(afid, &retry, DO_VLRU| IS_SLOCK /* no stats */ );
2342 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2343 ReleaseSharedLock(&afs_xvcache);
2344 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2350 /* no cache entry, better grab one */
2351 UpgradeSToWLock(&afs_xvcache, 25);
2352 tvc = afs_NewVCache(afid, NULL);
2354 ConvertWToSLock(&afs_xvcache);
2357 ReleaseSharedLock(&afs_xvcache);
2362 ReleaseSharedLock(&afs_xvcache);
2363 ObtainWriteLock(&tvc->lock, 58);
2365 tvc->f.states &= ~CStatd;
2366 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2367 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2369 /* Is it always appropriate to throw away all the access rights? */
2370 afs_FreeAllAxs(&(tvc->Access));
2372 /*Copy useful per-volume info */
2373 tvp = afs_GetVolume(afid, areq, READ_LOCK);
2375 if (newvcache && (tvp->states & VForeign))
2376 tvc->f.states |= CForeign;
2377 if (tvp->states & VRO)
2378 tvc->f.states |= CRO;
2379 if (tvp->states & VBackup)
2380 tvc->f.states |= CBackup;
2382 * Now, copy ".." entry back out of volume structure, if
2385 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2387 tvc->mvid = (struct VenusFid *)
2388 osi_AllocSmallSpace(sizeof(struct VenusFid));
2389 *tvc->mvid = tvp->dotdot;
2392 /* store the stat on the file */
2393 afs_RemoveVCB(afid);
2394 afs_ProcessFS(tvc, OutStatus, areq);
2395 tvc->callback = tc->srvr->server;
2397 /* we use osi_Time twice below. Ideally, we would use the time at which
2398 * the FetchStatus call began, instead, but we don't have it here. So we
2399 * make do with "now". In the CRO case, it doesn't really matter. In
2400 * the other case, we hope that the difference between "now" and when the
2401 * call actually began execution on the server won't be larger than the
2402 * padding which the server keeps. Subtract 1 second anyway, to be on
2403 * the safe side. Can't subtract more because we don't know how big
2404 * ExpirationTime is. Possible consistency problems may arise if the call
2405 * timeout period becomes longer than the server's expiration padding. */
2406 ObtainWriteLock(&afs_xcbhash, 470);
2407 if (CallBack->ExpirationTime != 0) {
2408 tvc->cbExpires = CallBack->ExpirationTime + osi_Time() - 1;
2409 tvc->f.states |= CStatd;
2410 tvc->f.states &= ~CBulkFetching;
2411 afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2412 } else if (tvc->f.states & CRO) {
2413 /* old-fashioned AFS 3.2 style */
2414 tvc->cbExpires = 3600 + osi_Time();
2415 /*XXX*/ tvc->f.states |= CStatd;
2416 tvc->f.states &= ~CBulkFetching;
2417 afs_QueueCallback(tvc, CBHash(3600), tvp);
2419 afs_DequeueCallback(tvc);
2420 tvc->callback = NULL;
2421 tvc->f.states &= ~(CStatd | CUnique);
2422 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2423 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2425 ReleaseWriteLock(&afs_xcbhash);
2427 afs_PutVolume(tvp, READ_LOCK);
2429 /* look in per-pag cache */
2430 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2431 ac->axess = OutStatus->CallerAccess; /* substitute pags */
2432 else /* not found, add a new one if possible */
2433 afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2435 ReleaseWriteLock(&tvc->lock);
2436 afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2437 ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32,
2438 tvc->cbExpires, ICL_TYPE_INT32, tvc->cbExpires - osi_Time());
2440 * Release ref count... hope this guy stays around...
2443 } /*afs_StuffVcache */
2447 * Decrements the reference count on a cache entry.
2449 * \param avc Pointer to the cache entry to decrement.
2451 * \note Environment: Nothing interesting.
2454 afs_PutVCache(register struct vcache *avc)
2456 AFS_STATCNT(afs_PutVCache);
2457 #ifdef AFS_DARWIN80_ENV
2458 vnode_put(AFSTOV(avc));
2462 * Can we use a read lock here?
2464 ObtainReadLock(&afs_xvcache);
2466 ReleaseReadLock(&afs_xvcache);
2468 } /*afs_PutVCache */
2472 * Reset a vcache entry, so local contents are ignored, and the
2473 * server will be reconsulted next time the vcache is used
2475 * \param avc Pointer to the cache entry to reset
2478 * \note avc must be write locked on entry
2481 afs_ResetVCache(struct vcache *avc, afs_ucred_t *acred)
2483 ObtainWriteLock(&afs_xcbhash, 456);
2484 afs_DequeueCallback(avc);
2485 avc->f.states &= ~(CStatd | CDirty); /* next reference will re-stat */
2486 ReleaseWriteLock(&afs_xcbhash);
2487 /* now find the disk cache entries */
2488 afs_TryToSmush(avc, acred, 1);
2489 osi_dnlc_purgedp(avc);
2490 if (avc->linkData && !(avc->f.states & CCore)) {
2491 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
2492 avc->linkData = NULL;
2497 * Sleepa when searching for a vcache. Releases all the pending locks,
2498 * sleeps then obtains the previously released locks.
2500 * \param vcache Enter sleep state.
2501 * \param flag Determines what locks to use.
2506 findvc_sleep(struct vcache *avc, int flag)
2508 int fstates = avc->f.states;
2509 if (flag & IS_SLOCK) {
2510 ReleaseSharedLock(&afs_xvcache);
2512 if (flag & IS_WLOCK) {
2513 ReleaseWriteLock(&afs_xvcache);
2515 ReleaseReadLock(&afs_xvcache);
2518 if (flag & FIND_CDEAD) {
2519 ObtainWriteLock(&afs_xvcache, 342);
2520 afs_FlushReclaimedVcaches();
2521 if (fstates == avc->f.states) {
2522 ReleaseWriteLock(&afs_xvcache);
2523 afs_osi_Sleep(&avc->f.states);
2525 ReleaseWriteLock(&afs_xvcache);
2527 afs_osi_Sleep(&avc->f.states);
2528 if (flag & IS_SLOCK) {
2529 ObtainSharedLock(&afs_xvcache, 341);
2531 if (flag & IS_WLOCK) {
2532 ObtainWriteLock(&afs_xvcache, 343);
2534 ObtainReadLock(&afs_xvcache);
2539 * Find a vcache entry given a fid.
2541 * \param afid Pointer to the fid whose cache entry we desire.
2542 * \param retry (SGI-specific) tell the caller to drop the lock on xvcache,
2543 * unlock the vnode, and try again.
2544 * \param flag Bit 1 to specify whether to compute hit statistics. Not
2545 * set if FindVCache is called as part of internal bookkeeping.
2547 * \note Environment: Must be called with the afs_xvcache lock at least held at
2548 * the read level. In order to do the VLRU adjustment, the xvcache lock
2549 * must be shared-- we upgrade it here.
2553 afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
2556 register struct vcache *tvc;
2558 #ifdef AFS_DARWIN80_ENV
2562 AFS_STATCNT(afs_FindVCache);
2566 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2567 if (FidMatches(afid, tvc)) {
2568 if (tvc->f.states & CVInit) {
2569 findvc_sleep(tvc, flag);
2572 #ifdef AFS_DARWIN80_ENV
2573 if (tvc->f.states & CDeadVnode) {
2574 if (!(flag & FIND_CDEAD)) {
2575 findvc_sleep(tvc, flag);
2582 if (vnode_ref(tvp)) {
2584 /* AFSTOV(tvc) may be NULL */
2589 if (tvc->f.states & (CBulkFetching|CDeadVnode)) {
2591 vnode_recycle(AFSTOV(tvc));
2599 /* should I have a read lock on the vnode here? */
2603 #if !defined(AFS_DARWIN80_ENV)
2604 osi_vnhold(tvc, retry); /* already held, above */
2605 if (retry && *retry)
2608 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
2609 tvc->f.states |= CUBCinit;
2611 if (UBCINFOMISSING(AFSTOV(tvc)) ||
2612 UBCINFORECLAIMED(AFSTOV(tvc))) {
2613 ubc_info_init(AFSTOV(tvc));
2616 tvc->f.states &= ~CUBCinit;
2619 * only move to front of vlru if we have proper vcache locking)
2621 if (flag & DO_VLRU) {
2622 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2623 refpanic("FindVC VLRU inconsistent1");
2625 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2626 refpanic("FindVC VLRU inconsistent1");
2628 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2629 refpanic("FindVC VLRU inconsistent2");
2631 UpgradeSToWLock(&afs_xvcache, 26);
2632 QRemove(&tvc->vlruq);
2633 QAdd(&VLRU, &tvc->vlruq);
2634 ConvertWToSLock(&afs_xvcache);
2635 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2636 refpanic("FindVC VLRU inconsistent1");
2638 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2639 refpanic("FindVC VLRU inconsistent2");
2641 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2642 refpanic("FindVC VLRU inconsistent3");
2648 if (flag & DO_STATS) {
2650 afs_stats_cmperf.vcacheHits++;
2652 afs_stats_cmperf.vcacheMisses++;
2653 if (afs_IsPrimaryCellNum(afid->Cell))
2654 afs_stats_cmperf.vlocalAccesses++;
2656 afs_stats_cmperf.vremoteAccesses++;
2659 } /*afs_FindVCache */
2662 * Find a vcache entry given a fid. Does a wildcard match on what we
2663 * have for the fid. If more than one entry, don't return anything.
2665 * \param avcp Fill in pointer if we found one and only one.
2666 * \param afid Pointer to the fid whose cache entry we desire.
2667 * \param retry (SGI-specific) tell the caller to drop the lock on xvcache,
2668 * unlock the vnode, and try again.
2669 * \param flags bit 1 to specify whether to compute hit statistics. Not
2670 * set if FindVCache is called as part of internal bookkeeping.
2672 * \note Environment: Must be called with the afs_xvcache lock at least held at
2673 * the read level. In order to do the VLRU adjustment, the xvcache lock
2674 * must be shared-- we upgrade it here.
2676 * \return Number of matches found.
2679 int afs_duplicate_nfs_fids = 0;
2682 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2684 register struct vcache *tvc;
2686 afs_int32 count = 0;
2687 struct vcache *found_tvc = NULL;
2688 #ifdef AFS_DARWIN80_ENV
2692 AFS_STATCNT(afs_FindVCache);
2696 ObtainSharedLock(&afs_xvcache, 331);
2699 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2700 /* Match only on what we have.... */
2701 if (((tvc->f.fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2702 && (tvc->f.fid.Fid.Volume == afid->Fid.Volume)
2703 && ((tvc->f.fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2704 && (tvc->f.fid.Cell == afid->Cell)) {
2705 if (tvc->f.states & CVInit) {
2706 ReleaseSharedLock(&afs_xvcache);
2707 afs_osi_Sleep(&tvc->f.states);
2710 #ifdef AFS_DARWIN80_ENV
2711 if (tvc->f.states & CDeadVnode) {
2712 if (!(tvc->f.states & CBulkFetching)) {
2713 ReleaseSharedLock(&afs_xvcache);
2714 afs_osi_Sleep(&tvc->f.states);
2719 if (vnode_get(tvp)) {
2720 /* This vnode no longer exists. */
2723 if (vnode_ref(tvp)) {
2724 /* This vnode no longer exists. */
2726 /* AFSTOV(tvc) may be NULL */
2731 if (tvc->f.states & (CBulkFetching|CDeadVnode)) {
2733 vnode_recycle(AFSTOV(tvc));
2736 #endif /* AFS_DARWIN80_ENV */
2740 afs_duplicate_nfs_fids++;
2741 ReleaseSharedLock(&afs_xvcache);
2742 #ifdef AFS_DARWIN80_ENV
2743 /* Drop our reference counts. */
2744 vnode_put(AFSTOV(tvc));
2745 vnode_put(AFSTOV(found_tvc));
2754 /* should I have a read lock on the vnode here? */
2756 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2757 afs_int32 retry = 0;
2758 osi_vnhold(tvc, &retry);
2761 found_tvc = (struct vcache *)0;
2762 ReleaseSharedLock(&afs_xvcache);
2763 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2767 osi_vnhold(tvc, (int *)0); /* already held, above */
2770 * We obtained the xvcache lock above.
2772 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2773 refpanic("FindVC VLRU inconsistent1");
2775 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2776 refpanic("FindVC VLRU inconsistent1");
2778 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2779 refpanic("FindVC VLRU inconsistent2");
2781 UpgradeSToWLock(&afs_xvcache, 568);
2782 QRemove(&tvc->vlruq);
2783 QAdd(&VLRU, &tvc->vlruq);
2784 ConvertWToSLock(&afs_xvcache);
2785 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2786 refpanic("FindVC VLRU inconsistent1");
2788 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2789 refpanic("FindVC VLRU inconsistent2");
2791 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2792 refpanic("FindVC VLRU inconsistent3");
2798 afs_stats_cmperf.vcacheHits++;
2800 afs_stats_cmperf.vcacheMisses++;
2801 if (afs_IsPrimaryCellNum(afid->Cell))
2802 afs_stats_cmperf.vlocalAccesses++;
2804 afs_stats_cmperf.vremoteAccesses++;
2806 *avcp = tvc; /* May be null */
2808 ReleaseSharedLock(&afs_xvcache);
2809 return (tvc ? 1 : 0);
2811 } /*afs_NFSFindVCache */
2817 * Initialize vcache related variables
2822 afs_vcacheInit(int astatSize)
2824 #if !defined(AFS_LINUX22_ENV)
2825 register struct vcache *tvp;
2828 if (!afs_maxvcount) {
2829 afs_maxvcount = astatSize; /* no particular limit on linux? */
2831 #if !defined(AFS_LINUX22_ENV)
2835 AFS_RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2836 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2838 #if !defined(AFS_LINUX22_ENV)
2839 /* Allocate and thread the struct vcache entries */
2840 tvp = (struct vcache *)afs_osi_Alloc(astatSize * sizeof(struct vcache));
2841 memset(tvp, 0, sizeof(struct vcache) * astatSize);
2843 Initial_freeVCList = tvp;
2844 freeVCList = &(tvp[0]);
2845 for (i = 0; i < astatSize - 1; i++) {
2846 tvp[i].nextfree = &(tvp[i + 1]);
2848 tvp[astatSize - 1].nextfree = NULL;
2849 # ifdef KERNEL_HAVE_PIN
2850 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
2854 #if defined(AFS_SGI_ENV)
2855 for (i = 0; i < astatSize; i++) {
2856 char name[METER_NAMSZ];
2857 struct vcache *tvc = &tvp[i];
2859 tvc->v.v_number = ++afsvnumbers;
2860 tvc->vc_rwlockid = OSI_NO_LOCKID;
2861 initnsema(&tvc->vc_rwlock, 1,
2862 makesname(name, "vrw", tvc->v.v_number));
2863 #ifndef AFS_SGI53_ENV
2864 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
2866 #ifndef AFS_SGI62_ENV
2867 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
2868 #endif /* AFS_SGI62_ENV */
2872 for(i = 0; i < VCSIZE; ++i)
2873 QInit(&afs_vhashTV[i]);
2880 shutdown_vcache(void)
2883 struct afs_cbr *tsp;
2885 * XXX We may potentially miss some of the vcaches because if when
2886 * there are no free vcache entries and all the vcache entries are active
2887 * ones then we allocate an additional one - admittedly we almost never
2892 register struct afs_q *tq, *uq = NULL;
2893 register struct vcache *tvc;
2894 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
2898 osi_FreeSmallSpace(tvc->mvid);
2899 tvc->mvid = (struct VenusFid *)0;
2902 aix_gnode_rele(AFSTOV(tvc));
2904 if (tvc->linkData) {
2905 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
2910 * Also free the remaining ones in the Cache
2912 for (i = 0; i < VCSIZE; i++) {
2913 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2915 osi_FreeSmallSpace(tvc->mvid);
2916 tvc->mvid = (struct VenusFid *)0;
2920 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
2921 #ifdef AFS_AIX32_ENV
2924 vms_delete(tvc->segid);
2926 tvc->segid = tvc->vmh = NULL;
2927 if (VREFCOUNT_GT(tvc,0))
2928 osi_Panic("flushVcache: vm race");
2936 #if defined(AFS_SUN5_ENV)
2942 if (tvc->linkData) {
2943 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
2948 afs_FreeAllAxs(&(tvc->Access));
2954 * Free any leftover callback queue
2956 for (i = 0; i < afs_stats_cmperf.CallBackAlloced; i++) {
2957 tsp = afs_cbrHeads[i];
2958 afs_cbrHeads[i] = 0;
2959 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
2963 #if !defined(AFS_LINUX22_ENV)
2964 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2966 # ifdef KERNEL_HAVE_PIN
2967 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2970 freeVCList = Initial_freeVCList = 0;
2973 AFS_RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2974 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2976 for(i = 0; i < VCSIZE; ++i)
2977 QInit(&afs_vhashTV[i]);
2981 afs_DisconGiveUpCallbacks(void)
2987 ObtainWriteLock(&afs_xvcache, 1002); /* XXX - should be a unique number */
2989 /* Somehow, walk the set of vcaches, with each one coming out as tvc */
2990 for (i = 0; i < VCSIZE; i++) {
2991 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2992 if (afs_QueueVCB(tvc)) {
2993 tvc->callback = NULL;
2999 ReleaseWriteLock(&afs_xvcache);
3006 * Clear the Statd flag from all vcaches
3008 * This function removes the Statd flag from all vcaches. It's used by
3009 * disconnected mode to tidy up during reconnection
3013 afs_ClearAllStatdFlag(void)
3018 ObtainWriteLock(&afs_xvcache, 715);
3020 for (i = 0; i < VCSIZE; i++) {
3021 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3022 tvc->f.states &= ~(CStatd|CUnique);
3025 ReleaseWriteLock(&afs_xvcache);