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 */
61 afs_rwlock_t afs_xvcdirty; /*Lock: discon vcache dirty list mgmt */
62 afs_rwlock_t afs_xvcache; /*Lock: alloc new stat cache entries */
63 afs_rwlock_t afs_xvreclaim; /*Lock: entries reclaimed, not on free list */
64 afs_lock_t afs_xvcb; /*Lock: fids on which there are callbacks */
65 #if !defined(AFS_LINUX22_ENV)
66 static struct vcache *freeVCList; /*Free list for stat cache entries */
67 struct vcache *ReclaimedVCList; /*Reclaimed list for stat entries */
68 static struct vcache *Initial_freeVCList; /*Initial list for above */
70 struct afs_q VLRU; /*vcache LRU */
71 afs_int32 vcachegen = 0;
72 unsigned int afs_paniconwarn = 0;
73 struct vcache *afs_vhashT[VCSIZE];
74 struct afs_q afs_vhashTV[VCSIZE];
75 static struct afs_cbr *afs_cbrHashT[CBRSIZE];
76 afs_int32 afs_bulkStatsLost;
77 int afs_norefpanic = 0;
80 /* Disk backed vcache definitions
81 * Both protected by xvcache */
82 static int afs_nextVcacheSlot = 0;
83 static struct afs_slotlist *afs_freeSlotList = NULL;
85 /* Forward declarations */
86 static afs_int32 afs_QueueVCB(struct vcache *avc, int *slept);
89 * Generate an index into the hash table for a given Fid.
91 * \return The hash value.
94 afs_HashCBRFid(struct AFSFid *fid)
96 return (fid->Volume + fid->Vnode + fid->Unique) % CBRSIZE;
100 * Insert a CBR entry into the hash table.
101 * Must be called with afs_xvcb held.
106 afs_InsertHashCBR(struct afs_cbr *cbr)
108 int slot = afs_HashCBRFid(&cbr->fid);
110 cbr->hash_next = afs_cbrHashT[slot];
111 if (afs_cbrHashT[slot])
112 afs_cbrHashT[slot]->hash_pprev = &cbr->hash_next;
114 cbr->hash_pprev = &afs_cbrHashT[slot];
115 afs_cbrHashT[slot] = cbr;
120 * Flush the given vcache entry.
123 * afs_xvcache lock must be held for writing upon entry to
124 * prevent people from changing the vrefCount field, and to
125 * protect the lruq and hnext fields.
126 * LOCK: afs_FlushVCache afs_xvcache W
127 * REFCNT: vcache ref count must be zero on entry except for osf1
128 * RACE: lock is dropped and reobtained, permitting race in caller
130 * \param avc Pointer to vcache entry to flush.
131 * \param slept Pointer to int to set 1 if we sleep/drop locks, 0 if we don't.
135 afs_FlushVCache(struct vcache *avc, int *slept)
136 { /*afs_FlushVCache */
139 struct vcache **uvc, *wvc;
142 AFS_STATCNT(afs_FlushVCache);
143 afs_Trace2(afs_iclSetp, CM_TRACE_FLUSHV, ICL_TYPE_POINTER, avc,
144 ICL_TYPE_INT32, avc->f.states);
146 code = osi_VM_FlushVCache(avc, slept);
150 if (avc->f.states & CVFlushed) {
154 #if !defined(AFS_LINUX22_ENV)
155 if (avc->nextfree || !avc->vlruq.prev || !avc->vlruq.next) { /* qv afs.h */
156 refpanic("LRU vs. Free inconsistency");
159 avc->f.states |= CVFlushed;
160 /* pull the entry out of the lruq and put it on the free list */
161 QRemove(&avc->vlruq);
163 /* keep track of # of files that we bulk stat'd, but never used
164 * before they got recycled.
166 if (avc->f.states & CBulkStat)
169 /* remove entry from the hash chain */
170 i = VCHash(&avc->f.fid);
171 uvc = &afs_vhashT[i];
172 for (wvc = *uvc; wvc; uvc = &wvc->hnext, wvc = *uvc) {
175 avc->hnext = (struct vcache *)NULL;
180 /* remove entry from the volume hash table */
181 QRemove(&avc->vhashq);
184 osi_FreeSmallSpace(avc->mvid);
185 avc->mvid = (struct VenusFid *)0;
187 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
188 avc->linkData = NULL;
190 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
191 /* OK, there are no internal vrefCounts, so there shouldn't
192 * be any more refs here. */
194 #ifdef AFS_DARWIN80_ENV
195 vnode_clearfsnode(AFSTOV(avc));
196 vnode_removefsref(AFSTOV(avc));
198 avc->v->v_data = NULL; /* remove from vnode */
200 AFSTOV(avc) = NULL; /* also drop the ptr to vnode */
203 #ifdef AFS_SUN510_ENV
204 /* As we use private vnodes, cleanup is up to us */
205 vn_reinit(AFSTOV(avc));
207 afs_FreeAllAxs(&(avc->Access));
208 if (!afs_shuttingdown)
209 afs_QueueVCB(avc, slept);
210 ObtainWriteLock(&afs_xcbhash, 460);
211 afs_DequeueCallback(avc); /* remove it from queued callbacks list */
212 avc->f.states &= ~(CStatd | CUnique);
213 ReleaseWriteLock(&afs_xcbhash);
214 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
215 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
217 osi_dnlc_purgevp(avc);
220 * Next, keep track of which vnodes we've deleted for create's
221 * optimistic synchronization algorithm
224 if (avc->f.fid.Fid.Vnode & 1)
230 #if !defined(AFS_LINUX22_ENV)
231 /* put the entry in the free list */
232 avc->nextfree = freeVCList;
234 if (avc->vlruq.prev || avc->vlruq.next) {
235 refpanic("LRU vs. Free inconsistency");
237 avc->f.states |= CVFlushed;
239 /* This should put it back on the vnode free list since usecount is 1 */
241 if (VREFCOUNT_GT(avc,0)) {
242 AFS_RELE(AFSTOV(avc));
243 afs_stats_cmperf.vcacheXAllocs--;
245 if (afs_norefpanic) {
246 afs_warn("flush vc refcnt < 1");
249 osi_Panic("flush vc refcnt < 1");
251 #endif /* AFS_LINUX22_ENV */
256 } /*afs_FlushVCache */
260 * The core of the inactive vnode op for all but IRIX.
266 afs_InactiveVCache(struct vcache *avc, afs_ucred_t *acred)
268 AFS_STATCNT(afs_inactive);
269 if (avc->f.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->f.states &= ~CMAPPED; /* mainly used by SunOS 4.0.x */
274 avc->f.states &= ~CDirty; /* Turn it off */
275 if (avc->f.states & CUnlinked) {
276 if (CheckLock(&afs_xvcache) || CheckLock(&afs_xdcache)) {
277 avc->f.states |= CUnlinkedDel;
280 afs_remunlink(avc, 1); /* ignore any return code */
287 * Allocate a callback return structure from the
288 * free list and return it.
290 * Environment: The alloc and free routines are both called with the afs_xvcb lock
291 * held, so we don't have to worry about blocking in osi_Alloc.
293 * \return The allocated afs_cbr.
295 static struct afs_cbr *afs_cbrSpace = 0;
296 /* if alloc limit below changes, fix me! */
297 static struct afs_cbr *afs_cbrHeads[16];
304 while (!afs_cbrSpace) {
305 if (afs_stats_cmperf.CallBackAlloced >= sizeof(afs_cbrHeads)/sizeof(afs_cbrHeads[0])) {
306 /* don't allocate more than 16 * AFS_NCBRS for now */
308 afs_stats_cmperf.CallBackFlushes++;
311 tsp = afs_osi_Alloc(AFS_NCBRS * sizeof(struct afs_cbr));
312 osi_Assert(tsp != NULL);
313 for (i = 0; i < AFS_NCBRS - 1; i++) {
314 tsp[i].next = &tsp[i + 1];
316 tsp[AFS_NCBRS - 1].next = 0;
318 afs_cbrHeads[afs_stats_cmperf.CallBackAlloced] = tsp;
319 afs_stats_cmperf.CallBackAlloced++;
323 afs_cbrSpace = tsp->next;
328 * Free a callback return structure, removing it from all lists.
330 * Environment: the xvcb lock is held over these calls.
332 * \param asp The address of the structure to free.
337 afs_FreeCBR(struct afs_cbr *asp)
339 *(asp->pprev) = asp->next;
341 asp->next->pprev = asp->pprev;
343 *(asp->hash_pprev) = asp->hash_next;
345 asp->hash_next->hash_pprev = asp->hash_pprev;
347 asp->next = afs_cbrSpace;
353 FlushAllVCBs(struct rx_connection **rxconns, int nconns, int nservers,
354 struct afs_conn **conns, struct srvAddr **addrs)
359 results = afs_osi_Alloc(nservers * sizeof (afs_int32));
360 osi_Assert(results != NULL);
363 multi_Rx(rxconns,nconns)
365 multi_RXAFS_GiveUpAllCallBacks();
366 results[multi_i] = multi_error;
371 * Freeing the CBR will unlink it from the server's CBR list
372 * do it here, not in the loop, because a dynamic CBR will call
373 * into the memory management routines.
375 for ( i = 0 ; i < nconns ; i++ ) {
376 if (results[i] == 0) {
377 /* Unchain all of them */
378 while (addrs[i]->server->cbrs)
379 afs_FreeCBR(addrs[i]->server->cbrs);
382 afs_osi_Free(results, nservers * sizeof(afs_int32));
386 * Flush all queued callbacks to all servers.
388 * Environment: holds xvcb lock over RPC to guard against race conditions
389 * when a new callback is granted for the same file later on.
391 * \return 0 for success.
394 afs_FlushVCBs(afs_int32 lockit)
396 struct AFSFid *tfids;
397 struct AFSCallBack callBacks[1];
398 struct AFSCBFids fidArray;
399 struct AFSCBs cbArray;
401 struct afs_cbr *tcbrp;
405 struct vrequest treq;
407 int safety1, safety2, safety3;
410 if (AFS_IS_DISCONNECTED)
413 if ((code = afs_InitReq(&treq, afs_osi_credp)))
415 treq.flags |= O_NONBLOCK;
416 tfids = afs_osi_Alloc(sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
417 osi_Assert(tfids != NULL);
420 ObtainWriteLock(&afs_xvcb, 273);
423 * First, attempt a multi across everything, all addresses
424 * for all servers we know of.
428 afs_LoopServers(2, NULL, 0, FlushAllVCBs, NULL);
430 ObtainReadLock(&afs_xserver);
431 for (i = 0; i < NSERVERS; i++) {
432 for (safety1 = 0, tsp = afs_servers[i];
433 tsp && safety1 < afs_totalServers + 10;
434 tsp = tsp->next, safety1++) {
436 if (tsp->cbrs == (struct afs_cbr *)0)
439 /* otherwise, grab a block of AFS_MAXCBRSCALL from the list
440 * and make an RPC, over and over again.
442 tcount = 0; /* number found so far */
443 for (safety2 = 0; safety2 < afs_cacheStats; safety2++) {
444 if (tcount >= AFS_MAXCBRSCALL || !tsp->cbrs) {
445 /* if buffer is full, or we've queued all we're going
446 * to from this server, we should flush out the
449 fidArray.AFSCBFids_len = tcount;
450 fidArray.AFSCBFids_val = (struct AFSFid *)tfids;
451 cbArray.AFSCBs_len = 1;
452 cbArray.AFSCBs_val = callBacks;
453 memset(&callBacks[0], 0, sizeof(callBacks[0]));
454 callBacks[0].CallBackType = CB_EXCLUSIVE;
455 for (safety3 = 0; safety3 < AFS_MAXHOSTS * 2; safety3++) {
456 tc = afs_ConnByHost(tsp, tsp->cell->fsport,
457 tsp->cell->cellNum, &treq, 0,
461 (AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS);
464 RXAFS_GiveUpCallBacks(tc->id, &fidArray,
472 AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS, SHARED_LOCK,
477 /* ignore return code, since callbacks may have
478 * been returned anyway, we shouldn't leave them
479 * around to be returned again.
481 * Next, see if we are done with this server, and if so,
482 * break to deal with the next one.
488 /* if to flush full buffer */
489 /* if we make it here, we have an entry at the head of cbrs,
490 * which we should copy to the file ID array and then free.
493 tfids[tcount++] = tcbrp->fid;
495 /* Freeing the CBR will unlink it from the server's CBR list */
497 } /* while loop for this one server */
498 if (safety2 > afs_cacheStats) {
499 afs_warn("possible internal error afs_flushVCBs (%d)\n",
502 } /* for loop for this hash chain */
503 } /* loop through all hash chains */
504 if (safety1 > afs_totalServers + 2) {
506 ("AFS internal error (afs_flushVCBs) (%d > %d), continuing...\n",
507 safety1, afs_totalServers + 2);
509 osi_Panic("afs_flushVCBS safety1");
512 ReleaseReadLock(&afs_xserver);
514 ReleaseWriteLock(&afs_xvcb);
515 afs_osi_Free(tfids, sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
520 * Queue a callback on the given fid.
523 * Locks the xvcb lock.
524 * Called when the xvcache lock is already held.
525 * RACE: afs_xvcache may be dropped and reacquired
527 * \param avc vcache entry
528 * \param slep Set to 1 if we dropped afs_xvcache
529 * \return 1 if queued, 0 otherwise
533 afs_QueueVCB(struct vcache *avc, int *slept)
537 struct afs_cbr *tcbp;
540 AFS_STATCNT(afs_QueueVCB);
542 ObtainWriteLock(&afs_xvcb, 274);
544 /* we can't really give back callbacks on RO files, since the
545 * server only tracks them on a per-volume basis, and we don't
546 * know whether we still have some other files from the same
548 if (!((avc->f.states & CRO) == 0 && avc->callback)) {
552 /* The callback is really just a struct server ptr. */
553 tsp = (struct server *)(avc->callback);
556 /* If we don't have CBR space, AllocCBR may block or hit the net for
557 * clearing up CBRs. Hitting the net may involve a fileserver
558 * needing to contact us, so we must drop xvcache so we don't block
559 * those requests from going through. */
560 reacquire = *slept = 1;
561 ReleaseWriteLock(&afs_xvcache);
564 /* we now have a pointer to the server, so we just allocate
565 * a queue entry and queue it.
567 tcbp = afs_AllocCBR();
568 tcbp->fid = avc->f.fid.Fid;
570 tcbp->next = tsp->cbrs;
572 tsp->cbrs->pprev = &tcbp->next;
575 tcbp->pprev = &tsp->cbrs;
577 afs_InsertHashCBR(tcbp);
581 /* now release locks and return */
582 ReleaseWriteLock(&afs_xvcb);
585 /* make sure this is after dropping xvcb, for locking order */
586 ObtainWriteLock(&afs_xvcache, 279);
593 * Remove a queued callback for a given Fid.
596 * Locks xvcb and xserver locks.
597 * Typically called with xdcache, xvcache and/or individual vcache
600 * \param afid The fid we want cleansed of queued callbacks.
605 afs_RemoveVCB(struct VenusFid *afid)
608 struct afs_cbr *cbr, *ncbr;
610 AFS_STATCNT(afs_RemoveVCB);
611 ObtainWriteLock(&afs_xvcb, 275);
613 slot = afs_HashCBRFid(&afid->Fid);
614 ncbr = afs_cbrHashT[slot];
618 ncbr = cbr->hash_next;
620 if (afid->Fid.Volume == cbr->fid.Volume &&
621 afid->Fid.Vnode == cbr->fid.Vnode &&
622 afid->Fid.Unique == cbr->fid.Unique) {
627 ReleaseWriteLock(&afs_xvcb);
631 afs_FlushReclaimedVcaches(void)
633 #if !defined(AFS_LINUX22_ENV)
636 struct vcache *tmpReclaimedVCList = NULL;
638 ObtainWriteLock(&afs_xvreclaim, 76);
639 while (ReclaimedVCList) {
640 tvc = ReclaimedVCList; /* take from free list */
641 ReclaimedVCList = tvc->nextfree;
642 tvc->nextfree = NULL;
643 code = afs_FlushVCache(tvc, &fv_slept);
645 /* Ok, so, if we got code != 0, uh, wtf do we do? */
646 /* Probably, build a temporary list and then put all back when we
647 get to the end of the list */
648 /* This is actually really crappy, but we need to not leak these.
649 We probably need a way to be smarter about this. */
650 tvc->nextfree = tmpReclaimedVCList;
651 tmpReclaimedVCList = tvc;
652 /* printf("Reclaim list flush %lx failed: %d\n", (unsigned long) tvc, code); */
654 if (tvc->f.states & (CVInit
655 #ifdef AFS_DARWIN80_ENV
659 tvc->f.states &= ~(CVInit
660 #ifdef AFS_DARWIN80_ENV
664 afs_osi_Wakeup(&tvc->f.states);
667 if (tmpReclaimedVCList)
668 ReclaimedVCList = tmpReclaimedVCList;
670 ReleaseWriteLock(&afs_xvreclaim);
675 afs_PostPopulateVCache(struct vcache *avc, struct VenusFid *afid, int seq)
678 * The proper value for mvstat (for root fids) is setup by the caller.
681 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
684 if (afs_globalVFS == 0)
685 osi_Panic("afs globalvfs");
687 osi_PostPopulateVCache(avc);
690 osi_dnlc_purgedp(avc); /* this may be overkill */
691 memset(&(avc->callsort), 0, sizeof(struct afs_q));
693 avc->f.states &=~ CVInit;
695 avc->f.states |= CBulkFetching;
696 avc->f.m.Length = seq;
698 afs_osi_Wakeup(&avc->f.states);
702 afs_ShakeLooseVCaches(afs_int32 anumber)
706 struct afs_q *tq, *uq;
707 int fv_slept, defersleep = 0;
708 afs_int32 target = anumber;
712 for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
715 if (tvc->f.states & CVFlushed) {
716 refpanic("CVFlushed on VLRU");
717 /* In the other path, this was 2 * afs_cacheStats */
718 } else if (!afsd_dynamic_vcaches && i++ > afs_maxvcount) {
719 refpanic("Exceeded pool of AFS vnodes(VLRU cycle?)");
720 } else if (QNext(uq) != tq) {
721 refpanic("VLRU inconsistent");
722 } else if (tvc->f.states & CVInit) {
727 if (osi_TryEvictVCache(tvc, &fv_slept, defersleep))
735 continue; /* start over - may have raced. */
738 if (anumber && !defersleep) {
746 if (!afsd_dynamic_vcaches && anumber == target) {
747 afs_warn("afs_ShakeLooseVCaches: warning none freed, using %d of %d\n",
748 afs_vcount, afs_maxvcount);
754 /* Alloc new vnode. */
756 static struct vcache *
757 afs_AllocVCache(void)
761 tvc = osi_NewVnode();
766 if (afsd_dynamic_vcaches && afs_maxvcount < afs_vcount) {
767 afs_maxvcount = afs_vcount;
768 /*printf("peak vnodes: %d\n", afs_maxvcount);*/
771 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
773 /* If we create a new inode, we either give it a new slot number,
774 * or if one's available, use a slot number from the slot free list
776 if (afs_freeSlotList != NULL) {
777 struct afs_slotlist *tmp;
779 tvc->diskSlot = afs_freeSlotList->slot;
780 tmp = afs_freeSlotList;
781 afs_freeSlotList = tmp->next;
782 afs_osi_Free(tmp, sizeof(struct afs_slotlist));
784 tvc->diskSlot = afs_nextVcacheSlot++;
790 /* Pre populate a newly allocated vcache. On platforms where the actual
791 * vnode is attached to the vcache, this function is called before attachment,
792 * therefore it cannot perform any actions on the vnode itself */
795 afs_PrePopulateVCache(struct vcache *avc, struct VenusFid *afid,
796 struct server *serverp) {
799 slot = avc->diskSlot;
801 osi_PrePopulateVCache(avc);
803 avc->diskSlot = slot;
804 QZero(&avc->metadirty);
806 AFS_RWLOCK_INIT(&avc->lock, "vcache lock");
809 avc->linkData = NULL;
812 avc->execsOrWriters = 0;
814 avc->f.states = CVInit;
815 avc->last_looker = 0;
817 avc->asynchrony = -1;
821 avc->f.truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
822 hzero(avc->f.m.DataVersion); /* in case we copy it into flushDV */
824 avc->callback = serverp; /* to minimize chance that clear
827 #if defined(AFS_CACHE_BYPASS)
828 avc->cachingStates = 0;
829 avc->cachingTransitions = 0;
834 * This routine is responsible for allocating a new cache entry
835 * from the free list. It formats the cache entry and inserts it
836 * into the appropriate hash tables. It must be called with
837 * afs_xvcache write-locked so as to prevent several processes from
838 * trying to create a new cache entry simultaneously.
840 * LOCK: afs_NewVCache afs_xvcache W
842 * \param afid The file id of the file whose cache entry is being created.
844 * \return The new vcache struct.
847 static_inline struct vcache *
848 afs_NewVCache_int(struct VenusFid *afid, struct server *serverp, int seq)
852 afs_int32 anumber = VCACHE_FREE;
854 AFS_STATCNT(afs_NewVCache);
856 afs_FlushReclaimedVcaches();
858 #if defined(AFS_LINUX22_ENV)
859 if(!afsd_dynamic_vcaches && afs_vcount >= afs_maxvcount) {
860 afs_ShakeLooseVCaches(anumber);
861 if (afs_vcount >= afs_maxvcount) {
862 afs_warn("afs_NewVCache - none freed\n");
866 tvc = afs_AllocVCache();
867 #else /* AFS_LINUX22_ENV */
868 /* pull out a free cache entry */
870 afs_ShakeLooseVCaches(anumber);
874 tvc = afs_AllocVCache();
876 tvc = freeVCList; /* take from free list */
877 freeVCList = tvc->nextfree;
878 tvc->nextfree = NULL;
879 afs_vcount++; /* balanced by FlushVCache */
880 } /* end of if (!freeVCList) */
882 #endif /* AFS_LINUX22_ENV */
884 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
886 panic("afs_NewVCache(): free vcache with vnode attached");
889 /* Populate the vcache with as much as we can. */
890 afs_PrePopulateVCache(tvc, afid, serverp);
892 /* Thread the vcache onto the VLRU */
897 tvc->hnext = afs_vhashT[i];
899 QAdd(&afs_vhashTV[j], &tvc->vhashq);
901 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
902 refpanic("NewVCache VLRU inconsistent");
904 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
905 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
906 refpanic("NewVCache VLRU inconsistent2");
908 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
909 refpanic("NewVCache VLRU inconsistent3");
911 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
912 refpanic("NewVCache VLRU inconsistent4");
916 /* it should now be safe to drop the xvcache lock - so attach an inode
917 * to this vcache, where necessary */
918 osi_AttachVnode(tvc, seq);
920 /* Get a reference count to hold this vcache for the VLRUQ. Note that
921 * we have to do this after attaching the vnode, because the reference
922 * count may be held in the vnode itself */
924 #if defined(AFS_LINUX22_ENV)
925 /* Hold it for the LRU (should make count 2) */
927 #elif !(defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV))
928 VREFCOUNT_SET(tvc, 1); /* us */
931 #if defined (AFS_FBSD_ENV)
932 if (tvc->f.states & CVInit)
934 afs_PostPopulateVCache(tvc, afid, seq);
941 afs_NewVCache(struct VenusFid *afid, struct server *serverp)
943 return afs_NewVCache_int(afid, serverp, 0);
947 afs_NewBulkVCache(struct VenusFid *afid, struct server *serverp, int seq)
949 return afs_NewVCache_int(afid, serverp, seq);
955 * LOCK: afs_FlushActiveVcaches afs_xvcache N
957 * \param doflocks : Do we handle flocks?
960 afs_FlushActiveVcaches(afs_int32 doflocks)
966 afs_ucred_t *cred = NULL;
967 struct vrequest treq, ureq;
968 struct AFSVolSync tsync;
971 AFS_STATCNT(afs_FlushActiveVcaches);
972 ObtainReadLock(&afs_xvcache);
973 for (i = 0; i < VCSIZE; i++) {
974 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
975 if (tvc->f.states & CVInit) continue;
976 #ifdef AFS_DARWIN80_ENV
977 if (tvc->f.states & CDeadVnode &&
978 (tvc->f.states & (CCore|CUnlinkedDel) ||
979 tvc->flockCount)) panic("Dead vnode has core/unlinkedel/flock");
981 if (doflocks && tvc->flockCount != 0) {
982 /* if this entry has an flock, send a keep-alive call out */
984 ReleaseReadLock(&afs_xvcache);
985 ObtainWriteLock(&tvc->lock, 51);
987 afs_InitReq(&treq, afs_osi_credp);
988 treq.flags |= O_NONBLOCK;
990 tc = afs_Conn(&tvc->f.fid, &treq, SHARED_LOCK);
992 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
995 RXAFS_ExtendLock(tc->id,
996 (struct AFSFid *)&tvc->f.fid.Fid,
1002 } while (afs_Analyze
1003 (tc, code, &tvc->f.fid, &treq,
1004 AFS_STATS_FS_RPCIDX_EXTENDLOCK, SHARED_LOCK, NULL));
1006 ReleaseWriteLock(&tvc->lock);
1007 #ifdef AFS_DARWIN80_ENV
1009 ObtainReadLock(&afs_xvcache);
1011 ObtainReadLock(&afs_xvcache);
1016 if ((tvc->f.states & CCore) || (tvc->f.states & CUnlinkedDel)) {
1018 * Don't let it evaporate in case someone else is in
1019 * this code. Also, drop the afs_xvcache lock while
1020 * getting vcache locks.
1023 ReleaseReadLock(&afs_xvcache);
1024 #ifdef AFS_BOZONLOCK_ENV
1025 afs_BozonLock(&tvc->pvnLock, tvc);
1027 #if defined(AFS_SGI_ENV)
1029 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1031 osi_Assert(VREFCOUNT_GT(tvc,0));
1032 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1034 ObtainWriteLock(&tvc->lock, 52);
1035 if (tvc->f.states & CCore) {
1036 tvc->f.states &= ~CCore;
1037 /* XXXX Find better place-holder for cred XXXX */
1038 cred = (afs_ucred_t *)tvc->linkData;
1039 tvc->linkData = NULL; /* XXX */
1040 afs_InitReq(&ureq, cred);
1041 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1042 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
1043 tvc->execsOrWriters);
1044 code = afs_StoreOnLastReference(tvc, &ureq);
1045 ReleaseWriteLock(&tvc->lock);
1046 #ifdef AFS_BOZONLOCK_ENV
1047 afs_BozonUnlock(&tvc->pvnLock, tvc);
1049 hzero(tvc->flushDV);
1052 if (code && code != VNOVNODE) {
1053 afs_StoreWarn(code, tvc->f.fid.Fid.Volume,
1054 /* /dev/console */ 1);
1056 } else if (tvc->f.states & CUnlinkedDel) {
1060 ReleaseWriteLock(&tvc->lock);
1061 #ifdef AFS_BOZONLOCK_ENV
1062 afs_BozonUnlock(&tvc->pvnLock, tvc);
1064 #if defined(AFS_SGI_ENV)
1065 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1067 afs_remunlink(tvc, 0);
1068 #if defined(AFS_SGI_ENV)
1069 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1072 /* lost (or won, perhaps) the race condition */
1073 ReleaseWriteLock(&tvc->lock);
1074 #ifdef AFS_BOZONLOCK_ENV
1075 afs_BozonUnlock(&tvc->pvnLock, tvc);
1078 #if defined(AFS_SGI_ENV)
1079 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1081 #ifdef AFS_DARWIN80_ENV
1084 AFS_RELE(AFSTOV(tvc));
1085 /* Matches write code setting CCore flag */
1088 ObtainReadLock(&afs_xvcache);
1090 ObtainReadLock(&afs_xvcache);
1093 AFS_RELE(AFSTOV(tvc));
1094 /* Matches write code setting CCore flag */
1101 ReleaseReadLock(&afs_xvcache);
1107 * Make sure a cache entry is up-to-date status-wise.
1109 * NOTE: everywhere that calls this can potentially be sped up
1110 * by checking CStatd first, and avoiding doing the InitReq
1111 * if this is up-to-date.
1113 * Anymore, the only places that call this KNOW already that the
1114 * vcache is not up-to-date, so we don't screw around.
1116 * \param avc : Ptr to vcache entry to verify.
1122 * Make sure a cache entry is up-to-date status-wise.
1124 * NOTE: everywhere that calls this can potentially be sped up
1125 * by checking CStatd first, and avoiding doing the InitReq
1126 * if this is up-to-date.
1128 * Anymore, the only places that call this KNOW already that the
1129 * vcache is not up-to-date, so we don't screw around.
1131 * \param avc Pointer to vcache entry to verify.
1134 * \return 0 for success or other error codes.
1137 afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1141 AFS_STATCNT(afs_VerifyVCache);
1143 /* otherwise we must fetch the status info */
1145 ObtainWriteLock(&avc->lock, 53);
1146 if (avc->f.states & CStatd) {
1147 ReleaseWriteLock(&avc->lock);
1150 ObtainWriteLock(&afs_xcbhash, 461);
1151 avc->f.states &= ~(CStatd | CUnique);
1152 avc->callback = NULL;
1153 afs_DequeueCallback(avc);
1154 ReleaseWriteLock(&afs_xcbhash);
1155 ReleaseWriteLock(&avc->lock);
1157 /* since we've been called back, or the callback has expired,
1158 * it's possible that the contents of this directory, or this
1159 * file's name have changed, thus invalidating the dnlc contents.
1161 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
1162 osi_dnlc_purgedp(avc);
1164 osi_dnlc_purgevp(avc);
1166 /* fetch the status info */
1167 tvc = afs_GetVCache(&avc->f.fid, areq, NULL, avc);
1170 /* Put it back; caller has already incremented vrefCount */
1174 } /*afs_VerifyVCache */
1178 * Simple copy of stat info into cache.
1180 * Callers:as of 1992-04-29, only called by WriteVCache
1182 * \param avc Ptr to vcache entry involved.
1183 * \param astat Ptr to stat info to copy.
1187 afs_SimpleVStat(struct vcache *avc,
1188 struct AFSFetchStatus *astat, struct vrequest *areq)
1191 AFS_STATCNT(afs_SimpleVStat);
1193 #ifdef AFS_64BIT_CLIENT
1194 FillInt64(length, astat->Length_hi, astat->Length);
1195 #else /* AFS_64BIT_CLIENT */
1196 length = astat->Length;
1197 #endif /* AFS_64BIT_CLIENT */
1199 #if defined(AFS_SGI_ENV)
1200 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1201 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1202 osi_Assert((valusema(&avc->vc_rwlock) <= 0)
1203 && (OSI_GET_LOCKID() == avc->vc_rwlockid));
1204 if (length < avc->f.m.Length) {
1205 vnode_t *vp = (vnode_t *) avc;
1207 osi_Assert(WriteLocked(&avc->lock));
1208 ReleaseWriteLock(&avc->lock);
1210 PTOSSVP(vp, (off_t) length, (off_t) MAXLONG);
1212 ObtainWriteLock(&avc->lock, 67);
1217 if (!afs_DirtyPages(avc)) {
1218 /* if actively writing the file, don't fetch over this value */
1219 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT, ICL_TYPE_POINTER, avc,
1220 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1221 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1222 avc->f.m.Length = length;
1223 avc->f.m.Date = astat->ClientModTime;
1225 avc->f.m.Owner = astat->Owner;
1226 avc->f.m.Group = astat->Group;
1227 avc->f.m.Mode = astat->UnixModeBits;
1228 if (vType(avc) == VREG) {
1229 avc->f.m.Mode |= S_IFREG;
1230 } else if (vType(avc) == VDIR) {
1231 avc->f.m.Mode |= S_IFDIR;
1232 } else if (vType(avc) == VLNK) {
1233 avc->f.m.Mode |= S_IFLNK;
1234 if ((avc->f.m.Mode & 0111) == 0)
1237 if (avc->f.states & CForeign) {
1238 struct axscache *ac;
1239 avc->f.anyAccess = astat->AnonymousAccess;
1241 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1243 * Caller has at least one bit not covered by anonymous, and
1244 * thus may have interesting rights.
1246 * HOWEVER, this is a really bad idea, because any access query
1247 * for bits which aren't covered by anonymous, on behalf of a user
1248 * who doesn't have any special rights, will result in an answer of
1249 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1250 * It's an especially bad idea under Ultrix, since (due to the lack of
1251 * a proper access() call) it must perform several afs_access() calls
1252 * in order to create magic mode bits that vary according to who makes
1253 * the call. In other words, _every_ stat() generates a test for
1256 #endif /* badidea */
1257 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1258 ac->axess = astat->CallerAccess;
1259 else /* not found, add a new one if possible */
1260 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1263 } /*afs_SimpleVStat */
1267 * Store the status info *only* back to the server for a
1270 * Environment: Must be called with a shared lock held on the vnode.
1272 * \param avc Ptr to the vcache entry.
1273 * \param astatus Ptr to the status info to store.
1274 * \param areq Ptr to the associated vrequest.
1276 * \return Operation status.
1280 afs_WriteVCache(struct vcache *avc,
1281 struct AFSStoreStatus *astatus,
1282 struct vrequest *areq)
1285 struct afs_conn *tc;
1286 struct AFSFetchStatus OutStatus;
1287 struct AFSVolSync tsync;
1289 AFS_STATCNT(afs_WriteVCache);
1290 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1291 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
1293 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK);
1295 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1298 RXAFS_StoreStatus(tc->id, (struct AFSFid *)&avc->f.fid.Fid,
1299 astatus, &OutStatus, &tsync);
1304 } while (afs_Analyze
1305 (tc, code, &avc->f.fid, areq, AFS_STATS_FS_RPCIDX_STORESTATUS,
1306 SHARED_LOCK, NULL));
1308 UpgradeSToWLock(&avc->lock, 20);
1310 /* success, do the changes locally */
1311 afs_SimpleVStat(avc, &OutStatus, areq);
1313 * Update the date, too. SimpleVStat didn't do this, since
1314 * it thought we were doing this after fetching new status
1315 * over a file being written.
1317 avc->f.m.Date = OutStatus.ClientModTime;
1319 /* failure, set up to check with server next time */
1320 ObtainWriteLock(&afs_xcbhash, 462);
1321 afs_DequeueCallback(avc);
1322 avc->f.states &= ~(CStatd | CUnique); /* turn off stat valid flag */
1323 ReleaseWriteLock(&afs_xcbhash);
1324 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
1325 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
1327 ConvertWToSLock(&avc->lock);
1330 } /*afs_WriteVCache */
1333 * Store status info only locally, set the proper disconnection flags
1334 * and add to dirty list.
1336 * \param avc The vcache to be written locally.
1337 * \param astatus Get attr fields from local store.
1338 * \param attrs This one is only of the vs_size.
1340 * \note Must be called with a shared lock on the vnode
1343 afs_WriteVCacheDiscon(struct vcache *avc,
1344 struct AFSStoreStatus *astatus,
1345 struct vattr *attrs)
1348 afs_int32 flags = 0;
1350 UpgradeSToWLock(&avc->lock, 700);
1352 if (!astatus->Mask) {
1358 /* Set attributes. */
1359 if (astatus->Mask & AFS_SETMODTIME) {
1360 avc->f.m.Date = astatus->ClientModTime;
1361 flags |= VDisconSetTime;
1364 if (astatus->Mask & AFS_SETOWNER) {
1365 /* printf("Not allowed yet. \n"); */
1366 /*avc->f.m.Owner = astatus->Owner;*/
1369 if (astatus->Mask & AFS_SETGROUP) {
1370 /* printf("Not allowed yet. \n"); */
1371 /*avc->f.m.Group = astatus->Group;*/
1374 if (astatus->Mask & AFS_SETMODE) {
1375 avc->f.m.Mode = astatus->UnixModeBits;
1377 #if 0 /* XXX: Leaving this out, so it doesn't mess up the file type flag.*/
1379 if (vType(avc) == VREG) {
1380 avc->f.m.Mode |= S_IFREG;
1381 } else if (vType(avc) == VDIR) {
1382 avc->f.m.Mode |= S_IFDIR;
1383 } else if (vType(avc) == VLNK) {
1384 avc->f.m.Mode |= S_IFLNK;
1385 if ((avc->f.m.Mode & 0111) == 0)
1389 flags |= VDisconSetMode;
1390 } /* if(astatus.Mask & AFS_SETMODE) */
1392 } /* if (!astatus->Mask) */
1394 if (attrs->va_size > 0) {
1395 /* XXX: Do I need more checks? */
1396 /* Truncation operation. */
1397 flags |= VDisconTrunc;
1401 afs_DisconAddDirty(avc, flags, 1);
1403 /* XXX: How about the rest of the fields? */
1405 ConvertWToSLock(&avc->lock);
1411 * Copy astat block into vcache info
1413 * \note This code may get dataversion and length out of sync if the file has
1414 * been modified. This is less than ideal. I haven't thought about it sufficiently
1415 * to be certain that it is adequate.
1417 * \note Environment: Must be called under a write lock
1419 * \param avc Ptr to vcache entry.
1420 * \param astat Ptr to stat block to copy in.
1421 * \param areq Ptr to associated request.
1424 afs_ProcessFS(struct vcache *avc,
1425 struct AFSFetchStatus *astat, struct vrequest *areq)
1428 AFS_STATCNT(afs_ProcessFS);
1430 #ifdef AFS_64BIT_CLIENT
1431 FillInt64(length, astat->Length_hi, astat->Length);
1432 #else /* AFS_64BIT_CLIENT */
1433 length = astat->Length;
1434 #endif /* AFS_64BIT_CLIENT */
1435 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1436 * number for each bulk status request. Under no circumstances
1437 * should afs_DoBulkStat store a sequence number if the new
1438 * length will be ignored when afs_ProcessFS is called with
1439 * new stats. If you change the following conditional then you
1440 * also need to change the conditional in afs_DoBulkStat. */
1442 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1443 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1445 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1447 /* if we're writing or mapping this file, don't fetch over these
1450 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1451 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1452 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1453 avc->f.m.Length = length;
1454 avc->f.m.Date = astat->ClientModTime;
1456 hset64(avc->f.m.DataVersion, astat->dataVersionHigh, astat->DataVersion);
1457 avc->f.m.Owner = astat->Owner;
1458 avc->f.m.Mode = astat->UnixModeBits;
1459 avc->f.m.Group = astat->Group;
1460 avc->f.m.LinkCount = astat->LinkCount;
1461 if (astat->FileType == File) {
1462 vSetType(avc, VREG);
1463 avc->f.m.Mode |= S_IFREG;
1464 } else if (astat->FileType == Directory) {
1465 vSetType(avc, VDIR);
1466 avc->f.m.Mode |= S_IFDIR;
1467 } else if (astat->FileType == SymbolicLink) {
1468 if (afs_fakestat_enable && (avc->f.m.Mode & 0111) == 0) {
1469 vSetType(avc, VDIR);
1470 avc->f.m.Mode |= S_IFDIR;
1472 vSetType(avc, VLNK);
1473 avc->f.m.Mode |= S_IFLNK;
1475 if ((avc->f.m.Mode & 0111) == 0) {
1479 avc->f.anyAccess = astat->AnonymousAccess;
1481 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1483 * Caller has at least one bit not covered by anonymous, and
1484 * thus may have interesting rights.
1486 * HOWEVER, this is a really bad idea, because any access query
1487 * for bits which aren't covered by anonymous, on behalf of a user
1488 * who doesn't have any special rights, will result in an answer of
1489 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1490 * It's an especially bad idea under Ultrix, since (due to the lack of
1491 * a proper access() call) it must perform several afs_access() calls
1492 * in order to create magic mode bits that vary according to who makes
1493 * the call. In other words, _every_ stat() generates a test for
1496 #endif /* badidea */
1498 struct axscache *ac;
1499 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1500 ac->axess = astat->CallerAccess;
1501 else /* not found, add a new one if possible */
1502 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1504 } /*afs_ProcessFS */
1508 * Get fid from server.
1511 * \param areq Request to be passed on.
1512 * \param name Name of ?? to lookup.
1513 * \param OutStatus Fetch status.
1518 * \return Success status of operation.
1521 afs_RemoteLookup(struct VenusFid *afid, struct vrequest *areq,
1522 char *name, struct VenusFid *nfid,
1523 struct AFSFetchStatus *OutStatusp,
1524 struct AFSCallBack *CallBackp, struct server **serverp,
1525 struct AFSVolSync *tsyncp)
1528 struct afs_conn *tc;
1529 struct AFSFetchStatus OutDirStatus;
1532 name = ""; /* XXX */
1534 tc = afs_Conn(afid, areq, SHARED_LOCK);
1537 *serverp = tc->parent->srvr->server;
1538 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1541 RXAFS_Lookup(tc->id, (struct AFSFid *)&afid->Fid, name,
1542 (struct AFSFid *)&nfid->Fid, OutStatusp,
1543 &OutDirStatus, CallBackp, tsyncp);
1548 } while (afs_Analyze
1549 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_XLOOKUP, SHARED_LOCK,
1559 * Given a file id and a vrequest structure, fetch the status
1560 * information associated with the file.
1562 * \param afid File ID.
1563 * \param areq Ptr to associated vrequest structure, specifying the
1564 * user whose authentication tokens will be used.
1565 * \param avc Caller may already have a vcache for this file, which is
1568 * \note Environment:
1569 * The cache entry is returned with an increased vrefCount field.
1570 * The entry must be discarded by calling afs_PutVCache when you
1571 * are through using the pointer to the cache entry.
1573 * You should not hold any locks when calling this function, except
1574 * locks on other vcache entries. If you lock more than one vcache
1575 * entry simultaneously, you should lock them in this order:
1577 * 1. Lock all files first, then directories.
1578 * 2. Within a particular type, lock entries in Fid.Vnode order.
1580 * This locking hierarchy is convenient because it allows locking
1581 * of a parent dir cache entry, given a file (to check its access
1582 * control list). It also allows renames to be handled easily by
1583 * locking directories in a constant order.
1585 * \note NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1587 * \note Might have a vcache structure already, which must
1588 * already be held by the caller
1591 afs_GetVCache(struct VenusFid *afid, struct vrequest *areq,
1592 afs_int32 * cached, struct vcache *avc)
1595 afs_int32 code, newvcache = 0;
1600 AFS_STATCNT(afs_GetVCache);
1603 *cached = 0; /* Init just in case */
1605 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1609 ObtainSharedLock(&afs_xvcache, 5);
1611 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU | IS_SLOCK);
1613 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1614 ReleaseSharedLock(&afs_xvcache);
1615 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1622 osi_Assert((tvc->f.states & CVInit) == 0);
1623 /* If we are in readdir, return the vnode even if not statd */
1624 if ((tvc->f.states & CStatd) || afs_InReadDir(tvc)) {
1625 ReleaseSharedLock(&afs_xvcache);
1629 UpgradeSToWLock(&afs_xvcache, 21);
1631 /* no cache entry, better grab one */
1632 tvc = afs_NewVCache(afid, NULL);
1635 ConvertWToSLock(&afs_xvcache);
1638 ReleaseSharedLock(&afs_xvcache);
1642 afs_stats_cmperf.vcacheMisses++;
1645 ReleaseSharedLock(&afs_xvcache);
1647 ObtainWriteLock(&tvc->lock, 54);
1649 if (tvc->f.states & CStatd) {
1650 ReleaseWriteLock(&tvc->lock);
1653 #ifdef AFS_DARWIN80_ENV
1654 /* Darwin 8.0 only has bufs in nfs, so we shouldn't have to worry about them.
1657 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
1659 * XXX - I really don't like this. Should try to understand better.
1660 * It seems that sometimes, when we get called, we already hold the
1661 * lock on the vnode (e.g., from afs_getattr via afs_VerifyVCache).
1662 * We can't drop the vnode lock, because that could result in a race.
1663 * Sometimes, though, we get here and don't hold the vnode lock.
1664 * I hate code paths that sometimes hold locks and sometimes don't.
1665 * In any event, the dodge we use here is to check whether the vnode
1666 * is locked, and if it isn't, then we gain and drop it around the call
1667 * to vinvalbuf; otherwise, we leave it alone.
1670 struct vnode *vp = AFSTOV(tvc);
1673 #if defined(AFS_DARWIN_ENV)
1674 iheldthelock = VOP_ISLOCKED(vp);
1676 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, current_proc());
1677 /* this is messy. we can call fsync which will try to reobtain this */
1678 if (VTOAFS(vp) == tvc)
1679 ReleaseWriteLock(&tvc->lock);
1680 if (UBCINFOEXISTS(vp)) {
1681 vinvalbuf(vp, V_SAVE, &afs_osi_cred, current_proc(), PINOD, 0);
1683 if (VTOAFS(vp) == tvc)
1684 ObtainWriteLock(&tvc->lock, 954);
1686 VOP_UNLOCK(vp, LK_EXCLUSIVE, current_proc());
1687 #elif defined(AFS_FBSD80_ENV)
1688 iheldthelock = VOP_ISLOCKED(vp);
1689 if (!iheldthelock) {
1690 /* nosleep/sleep lock order reversal */
1691 int glocked = ISAFS_GLOCK();
1694 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1698 vinvalbuf(vp, V_SAVE, PINOD, 0); /* changed late in 8.0-CURRENT */
1701 #elif defined(AFS_FBSD60_ENV)
1702 iheldthelock = VOP_ISLOCKED(vp, curthread);
1704 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1706 vinvalbuf(vp, V_SAVE, curthread, PINOD, 0);
1709 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1710 #elif defined(AFS_FBSD_ENV)
1711 iheldthelock = VOP_ISLOCKED(vp, curthread);
1713 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1714 vinvalbuf(vp, V_SAVE, osi_curcred(), curthread, PINOD, 0);
1716 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1717 #elif defined(AFS_OBSD_ENV)
1718 iheldthelock = VOP_ISLOCKED(vp, curproc);
1720 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1721 uvm_vnp_uncache(vp);
1723 VOP_UNLOCK(vp, 0, curproc);
1724 #elif defined(AFS_NBSD40_ENV)
1725 iheldthelock = VOP_ISLOCKED(vp);
1726 if (!iheldthelock) {
1727 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1729 uvm_vnp_uncache(vp);
1737 ObtainWriteLock(&afs_xcbhash, 464);
1738 tvc->f.states &= ~CUnique;
1740 afs_DequeueCallback(tvc);
1741 ReleaseWriteLock(&afs_xcbhash);
1743 /* It is always appropriate to throw away all the access rights? */
1744 afs_FreeAllAxs(&(tvc->Access));
1745 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1747 if ((tvp->states & VForeign)) {
1749 tvc->f.states |= CForeign;
1750 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1751 && (tvp->rootUnique == afid->Fid.Unique)) {
1755 if (tvp->states & VRO)
1756 tvc->f.states |= CRO;
1757 if (tvp->states & VBackup)
1758 tvc->f.states |= CBackup;
1759 /* now copy ".." entry back out of volume structure, if necessary */
1760 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1762 tvc->mvid = (struct VenusFid *)
1763 osi_AllocSmallSpace(sizeof(struct VenusFid));
1764 *tvc->mvid = tvp->dotdot;
1766 afs_PutVolume(tvp, READ_LOCK);
1770 afs_RemoveVCB(afid);
1772 struct AFSFetchStatus OutStatus;
1774 if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1775 afs_ProcessFS(tvc, &OutStatus, areq);
1776 tvc->f.states |= CStatd | CUnique;
1777 tvc->f.parent.vnode = OutStatus.ParentVnode;
1778 tvc->f.parent.unique = OutStatus.ParentUnique;
1782 if (AFS_IS_DISCONNECTED) {
1783 /* Nothing to do otherwise...*/
1785 /* printf("Network is down in afs_GetCache"); */
1787 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1789 /* For the NFS translator's benefit, make sure
1790 * non-directory vnodes always have their parent FID set
1791 * correctly, even when created as a result of decoding an
1792 * NFS filehandle. It would be nice to also do this for
1793 * directories, but we can't because the fileserver fills
1794 * in the FID of the directory itself instead of that of
1797 if (!code && OutStatus.FileType != Directory &&
1798 !tvc->f.parent.vnode) {
1799 tvc->f.parent.vnode = OutStatus.ParentVnode;
1800 tvc->f.parent.unique = OutStatus.ParentUnique;
1801 /* XXX - SXW - It's conceivable we should mark ourselves
1802 * as dirty again here, incase we've been raced
1803 * out of the FetchStatus call.
1810 ReleaseWriteLock(&tvc->lock);
1816 ReleaseWriteLock(&tvc->lock);
1819 } /*afs_GetVCache */
1824 * Lookup a vcache by fid. Look inside the cache first, if not
1825 * there, lookup the file on the server, and then get it's fresh
1830 * \param cached Is element cached? If NULL, don't answer.
1834 * \return The found element or NULL.
1837 afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1838 afs_int32 * cached, struct vcache *adp, char *aname)
1840 afs_int32 code, now, newvcache = 0;
1841 struct VenusFid nfid;
1844 struct AFSFetchStatus OutStatus;
1845 struct AFSCallBack CallBack;
1846 struct AFSVolSync tsync;
1847 struct server *serverp = 0;
1851 AFS_STATCNT(afs_GetVCache);
1853 *cached = 0; /* Init just in case */
1855 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1859 ObtainReadLock(&afs_xvcache);
1860 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */ );
1863 ReleaseReadLock(&afs_xvcache);
1865 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1866 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1870 ObtainReadLock(&tvc->lock);
1872 if (tvc->f.states & CStatd) {
1876 ReleaseReadLock(&tvc->lock);
1879 tvc->f.states &= ~CUnique;
1881 ReleaseReadLock(&tvc->lock);
1883 ObtainReadLock(&afs_xvcache);
1886 ReleaseReadLock(&afs_xvcache);
1888 /* lookup the file */
1891 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
1893 if (AFS_IS_DISCONNECTED) {
1894 /* printf("Network is down in afs_LookupVcache\n"); */
1898 afs_RemoteLookup(&adp->f.fid, areq, aname, &nfid, &OutStatus,
1899 &CallBack, &serverp, &tsync);
1901 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1905 ObtainSharedLock(&afs_xvcache, 6);
1906 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU | IS_SLOCK/* no xstats now */ );
1908 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1909 ReleaseSharedLock(&afs_xvcache);
1910 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1916 /* no cache entry, better grab one */
1917 UpgradeSToWLock(&afs_xvcache, 22);
1918 tvc = afs_NewVCache(&nfid, serverp);
1920 ConvertWToSLock(&afs_xvcache);
1923 ReleaseSharedLock(&afs_xvcache);
1928 ReleaseSharedLock(&afs_xvcache);
1929 ObtainWriteLock(&tvc->lock, 55);
1931 /* It is always appropriate to throw away all the access rights? */
1932 afs_FreeAllAxs(&(tvc->Access));
1933 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
1935 if ((tvp->states & VForeign)) {
1937 tvc->f.states |= CForeign;
1938 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1939 && (tvp->rootUnique == afid->Fid.Unique))
1942 if (tvp->states & VRO)
1943 tvc->f.states |= CRO;
1944 if (tvp->states & VBackup)
1945 tvc->f.states |= CBackup;
1946 /* now copy ".." entry back out of volume structure, if necessary */
1947 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1949 tvc->mvid = (struct VenusFid *)
1950 osi_AllocSmallSpace(sizeof(struct VenusFid));
1951 *tvc->mvid = tvp->dotdot;
1956 ObtainWriteLock(&afs_xcbhash, 465);
1957 afs_DequeueCallback(tvc);
1958 tvc->f.states &= ~(CStatd | CUnique);
1959 ReleaseWriteLock(&afs_xcbhash);
1960 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
1961 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
1963 afs_PutVolume(tvp, READ_LOCK);
1964 ReleaseWriteLock(&tvc->lock);
1969 ObtainWriteLock(&afs_xcbhash, 466);
1970 if (origCBs == afs_allCBs) {
1971 if (CallBack.ExpirationTime) {
1972 tvc->callback = serverp;
1973 tvc->cbExpires = CallBack.ExpirationTime + now;
1974 tvc->f.states |= CStatd | CUnique;
1975 tvc->f.states &= ~CBulkFetching;
1976 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
1977 } else if (tvc->f.states & CRO) {
1978 /* adapt gives us an hour. */
1979 tvc->cbExpires = 3600 + osi_Time();
1980 /*XXX*/ tvc->f.states |= CStatd | CUnique;
1981 tvc->f.states &= ~CBulkFetching;
1982 afs_QueueCallback(tvc, CBHash(3600), tvp);
1984 tvc->callback = NULL;
1985 afs_DequeueCallback(tvc);
1986 tvc->f.states &= ~(CStatd | CUnique);
1987 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
1988 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
1991 afs_DequeueCallback(tvc);
1992 tvc->f.states &= ~CStatd;
1993 tvc->f.states &= ~CUnique;
1994 tvc->callback = NULL;
1995 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
1996 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
1998 ReleaseWriteLock(&afs_xcbhash);
2000 afs_PutVolume(tvp, READ_LOCK);
2001 afs_ProcessFS(tvc, &OutStatus, areq);
2003 ReleaseWriteLock(&tvc->lock);
2009 afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
2010 afs_int32 * cached, struct volume *tvolp)
2012 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
2013 afs_int32 getNewFid = 0;
2015 struct VenusFid nfid;
2017 struct server *serverp = 0;
2018 struct AFSFetchStatus OutStatus;
2019 struct AFSCallBack CallBack;
2020 struct AFSVolSync tsync;
2022 #ifdef AFS_DARWIN80_ENV
2029 if (!tvolp->rootVnode || getNewFid) {
2030 struct VenusFid tfid;
2033 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2034 origCBs = afs_allCBs; /* ignore InitCallBackState */
2036 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2041 /* ReleaseReadLock(&tvolp->lock); */
2042 ObtainWriteLock(&tvolp->lock, 56);
2043 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
2044 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
2045 ReleaseWriteLock(&tvolp->lock);
2046 /* ObtainReadLock(&tvolp->lock);*/
2049 afid->Fid.Vnode = tvolp->rootVnode;
2050 afid->Fid.Unique = tvolp->rootUnique;
2054 ObtainSharedLock(&afs_xvcache, 7);
2056 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2057 if (!FidCmp(&(tvc->f.fid), afid)) {
2058 if (tvc->f.states & CVInit) {
2059 ReleaseSharedLock(&afs_xvcache);
2060 afs_osi_Sleep(&tvc->f.states);
2063 #ifdef AFS_DARWIN80_ENV
2064 if (tvc->f.states & CDeadVnode) {
2065 if (!(tvc->f.states & CBulkFetching)) {
2066 ReleaseSharedLock(&afs_xvcache);
2067 afs_osi_Sleep(&tvc->f.states);
2072 if (vnode_get(tvp)) /* this bumps ref count */
2074 if (vnode_ref(tvp)) {
2076 /* AFSTOV(tvc) may be NULL */
2081 if (tvc->f.states & (CBulkFetching|CDeadVnode)) {
2083 vnode_recycle(AFSTOV(tvc));
2091 if (!haveStatus && (!tvc || !(tvc->f.states & CStatd))) {
2092 /* Mount point no longer stat'd or unknown. FID may have changed. */
2094 ReleaseSharedLock(&afs_xvcache);
2095 #ifdef AFS_DARWIN80_ENV
2098 vnode_put(AFSTOV(tvc));
2099 vnode_rele(AFSTOV(tvc));
2108 UpgradeSToWLock(&afs_xvcache, 23);
2109 /* no cache entry, better grab one */
2110 tvc = afs_NewVCache(afid, NULL);
2113 ReleaseWriteLock(&afs_xvcache);
2117 afs_stats_cmperf.vcacheMisses++;
2121 afs_stats_cmperf.vcacheHits++;
2122 #if defined(AFS_DARWIN80_ENV)
2123 /* we already bumped the ref count in the for loop above */
2124 #else /* AFS_DARWIN80_ENV */
2127 UpgradeSToWLock(&afs_xvcache, 24);
2128 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2129 refpanic("GRVC VLRU inconsistent0");
2131 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2132 refpanic("GRVC VLRU inconsistent1");
2134 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2135 refpanic("GRVC VLRU inconsistent2");
2137 QRemove(&tvc->vlruq); /* move to lruq head */
2138 QAdd(&VLRU, &tvc->vlruq);
2139 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2140 refpanic("GRVC VLRU inconsistent3");
2142 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2143 refpanic("GRVC VLRU inconsistent4");
2145 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2146 refpanic("GRVC VLRU inconsistent5");
2151 ReleaseWriteLock(&afs_xvcache);
2153 if (tvc->f.states & CStatd) {
2157 ObtainReadLock(&tvc->lock);
2158 tvc->f.states &= ~CUnique;
2159 tvc->callback = NULL; /* redundant, perhaps */
2160 ReleaseReadLock(&tvc->lock);
2163 ObtainWriteLock(&tvc->lock, 57);
2165 /* It is always appropriate to throw away all the access rights? */
2166 afs_FreeAllAxs(&(tvc->Access));
2169 tvc->f.states |= CForeign;
2170 if (tvolp->states & VRO)
2171 tvc->f.states |= CRO;
2172 if (tvolp->states & VBackup)
2173 tvc->f.states |= CBackup;
2174 /* now copy ".." entry back out of volume structure, if necessary */
2175 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2176 && (tvolp->rootUnique == afid->Fid.Unique)) {
2179 if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2181 tvc->mvid = (struct VenusFid *)
2182 osi_AllocSmallSpace(sizeof(struct VenusFid));
2183 *tvc->mvid = tvolp->dotdot;
2187 afs_RemoveVCB(afid);
2190 struct VenusFid tfid;
2193 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2194 origCBs = afs_allCBs; /* ignore InitCallBackState */
2196 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2201 ObtainWriteLock(&afs_xcbhash, 467);
2202 afs_DequeueCallback(tvc);
2203 tvc->callback = NULL;
2204 tvc->f.states &= ~(CStatd | CUnique);
2205 ReleaseWriteLock(&afs_xcbhash);
2206 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2207 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2208 ReleaseWriteLock(&tvc->lock);
2213 ObtainWriteLock(&afs_xcbhash, 468);
2214 if (origCBs == afs_allCBs) {
2215 tvc->f.states |= CTruth;
2216 tvc->callback = serverp;
2217 if (CallBack.ExpirationTime != 0) {
2218 tvc->cbExpires = CallBack.ExpirationTime + start;
2219 tvc->f.states |= CStatd;
2220 tvc->f.states &= ~CBulkFetching;
2221 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2222 } else if (tvc->f.states & CRO) {
2223 /* adapt gives us an hour. */
2224 tvc->cbExpires = 3600 + osi_Time();
2225 /*XXX*/ tvc->f.states |= CStatd;
2226 tvc->f.states &= ~CBulkFetching;
2227 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2230 afs_DequeueCallback(tvc);
2231 tvc->callback = NULL;
2232 tvc->f.states &= ~(CStatd | CUnique);
2233 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2234 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2236 ReleaseWriteLock(&afs_xcbhash);
2237 afs_ProcessFS(tvc, &OutStatus, areq);
2239 ReleaseWriteLock(&tvc->lock);
2245 * Update callback status and (sometimes) attributes of a vnode.
2246 * Called after doing a fetch status RPC. Whilst disconnected, attributes
2247 * shouldn't be written to the vcache here.
2252 * \param Outsp Server status after rpc call.
2253 * \param acb Callback for this vnode.
2255 * \note The vcache must be write locked.
2258 afs_UpdateStatus(struct vcache *avc, struct VenusFid *afid,
2259 struct vrequest *areq, struct AFSFetchStatus *Outsp,
2260 struct AFSCallBack *acb, afs_uint32 start)
2262 struct volume *volp;
2265 /* Dont write status in vcache if resyncing after a disconnection. */
2266 afs_ProcessFS(avc, Outsp, areq);
2268 volp = afs_GetVolume(afid, areq, READ_LOCK);
2269 ObtainWriteLock(&afs_xcbhash, 469);
2270 avc->f.states |= CTruth;
2271 if (avc->callback /* check for race */ ) {
2272 if (acb->ExpirationTime != 0) {
2273 avc->cbExpires = acb->ExpirationTime + start;
2274 avc->f.states |= CStatd;
2275 avc->f.states &= ~CBulkFetching;
2276 afs_QueueCallback(avc, CBHash(acb->ExpirationTime), volp);
2277 } else if (avc->f.states & CRO) {
2278 /* ordinary callback on a read-only volume -- AFS 3.2 style */
2279 avc->cbExpires = 3600 + start;
2280 avc->f.states |= CStatd;
2281 avc->f.states &= ~CBulkFetching;
2282 afs_QueueCallback(avc, CBHash(3600), volp);
2284 afs_DequeueCallback(avc);
2285 avc->callback = NULL;
2286 avc->f.states &= ~(CStatd | CUnique);
2287 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
2288 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2291 afs_DequeueCallback(avc);
2292 avc->callback = NULL;
2293 avc->f.states &= ~(CStatd | CUnique);
2294 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
2295 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2297 ReleaseWriteLock(&afs_xcbhash);
2299 afs_PutVolume(volp, READ_LOCK);
2303 * Must be called with avc write-locked
2304 * don't absolutely have to invalidate the hint unless the dv has
2305 * changed, but be sure to get it right else there will be consistency bugs.
2308 afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
2309 struct vrequest * areq, struct AFSFetchStatus * Outsp)
2312 afs_uint32 start = 0;
2313 struct afs_conn *tc;
2314 struct AFSCallBack CallBack;
2315 struct AFSVolSync tsync;
2318 tc = afs_Conn(afid, areq, SHARED_LOCK);
2319 avc->dchint = NULL; /* invalidate hints */
2321 avc->callback = tc->parent->srvr->server;
2323 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2326 RXAFS_FetchStatus(tc->id, (struct AFSFid *)&afid->Fid, Outsp,
2334 } while (afs_Analyze
2335 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2336 SHARED_LOCK, NULL));
2339 afs_UpdateStatus(avc, afid, areq, Outsp, &CallBack, start);
2341 /* used to undo the local callback, but that's too extreme.
2342 * There are plenty of good reasons that fetchstatus might return
2343 * an error, such as EPERM. If we have the vnode cached, statd,
2344 * with callback, might as well keep track of the fact that we
2345 * don't have access...
2347 if (code == EPERM || code == EACCES) {
2348 struct axscache *ac;
2349 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2351 else /* not found, add a new one if possible */
2352 afs_AddAxs(avc->Access, areq->uid, 0);
2363 * Stuff some information into the vcache for the given file.
2366 * afid : File in question.
2367 * OutStatus : Fetch status on the file.
2368 * CallBack : Callback info.
2369 * tc : RPC connection involved.
2370 * areq : vrequest involved.
2373 * Nothing interesting.
2376 afs_StuffVcache(struct VenusFid *afid,
2377 struct AFSFetchStatus *OutStatus,
2378 struct AFSCallBack *CallBack, struct afs_conn *tc,
2379 struct vrequest *areq)
2381 afs_int32 code, i, newvcache = 0;
2383 struct AFSVolSync tsync;
2385 struct axscache *ac;
2388 AFS_STATCNT(afs_StuffVcache);
2389 #ifdef IFS_VCACHECOUNT
2394 ObtainSharedLock(&afs_xvcache, 8);
2396 tvc = afs_FindVCache(afid, &retry, DO_VLRU| IS_SLOCK /* no stats */ );
2398 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2399 ReleaseSharedLock(&afs_xvcache);
2400 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2406 /* no cache entry, better grab one */
2407 UpgradeSToWLock(&afs_xvcache, 25);
2408 tvc = afs_NewVCache(afid, NULL);
2410 ConvertWToSLock(&afs_xvcache);
2413 ReleaseSharedLock(&afs_xvcache);
2418 ReleaseSharedLock(&afs_xvcache);
2419 ObtainWriteLock(&tvc->lock, 58);
2421 tvc->f.states &= ~CStatd;
2422 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2423 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2425 /* Is it always appropriate to throw away all the access rights? */
2426 afs_FreeAllAxs(&(tvc->Access));
2428 /*Copy useful per-volume info */
2429 tvp = afs_GetVolume(afid, areq, READ_LOCK);
2431 if (newvcache && (tvp->states & VForeign))
2432 tvc->f.states |= CForeign;
2433 if (tvp->states & VRO)
2434 tvc->f.states |= CRO;
2435 if (tvp->states & VBackup)
2436 tvc->f.states |= CBackup;
2438 * Now, copy ".." entry back out of volume structure, if
2441 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2443 tvc->mvid = (struct VenusFid *)
2444 osi_AllocSmallSpace(sizeof(struct VenusFid));
2445 *tvc->mvid = tvp->dotdot;
2448 /* store the stat on the file */
2449 afs_RemoveVCB(afid);
2450 afs_ProcessFS(tvc, OutStatus, areq);
2451 tvc->callback = tc->srvr->server;
2453 /* we use osi_Time twice below. Ideally, we would use the time at which
2454 * the FetchStatus call began, instead, but we don't have it here. So we
2455 * make do with "now". In the CRO case, it doesn't really matter. In
2456 * the other case, we hope that the difference between "now" and when the
2457 * call actually began execution on the server won't be larger than the
2458 * padding which the server keeps. Subtract 1 second anyway, to be on
2459 * the safe side. Can't subtract more because we don't know how big
2460 * ExpirationTime is. Possible consistency problems may arise if the call
2461 * timeout period becomes longer than the server's expiration padding. */
2462 ObtainWriteLock(&afs_xcbhash, 470);
2463 if (CallBack->ExpirationTime != 0) {
2464 tvc->cbExpires = CallBack->ExpirationTime + osi_Time() - 1;
2465 tvc->f.states |= CStatd;
2466 tvc->f.states &= ~CBulkFetching;
2467 afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2468 } else if (tvc->f.states & CRO) {
2469 /* old-fashioned AFS 3.2 style */
2470 tvc->cbExpires = 3600 + osi_Time();
2471 /*XXX*/ tvc->f.states |= CStatd;
2472 tvc->f.states &= ~CBulkFetching;
2473 afs_QueueCallback(tvc, CBHash(3600), tvp);
2475 afs_DequeueCallback(tvc);
2476 tvc->callback = NULL;
2477 tvc->f.states &= ~(CStatd | CUnique);
2478 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2479 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2481 ReleaseWriteLock(&afs_xcbhash);
2483 afs_PutVolume(tvp, READ_LOCK);
2485 /* look in per-pag cache */
2486 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2487 ac->axess = OutStatus->CallerAccess; /* substitute pags */
2488 else /* not found, add a new one if possible */
2489 afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2491 ReleaseWriteLock(&tvc->lock);
2492 afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2493 ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32,
2494 tvc->cbExpires, ICL_TYPE_INT32, tvc->cbExpires - osi_Time());
2496 * Release ref count... hope this guy stays around...
2499 } /*afs_StuffVcache */
2503 * Decrements the reference count on a cache entry.
2505 * \param avc Pointer to the cache entry to decrement.
2507 * \note Environment: Nothing interesting.
2510 afs_PutVCache(struct vcache *avc)
2512 AFS_STATCNT(afs_PutVCache);
2513 #ifdef AFS_DARWIN80_ENV
2514 vnode_put(AFSTOV(avc));
2518 * Can we use a read lock here?
2520 ObtainReadLock(&afs_xvcache);
2522 ReleaseReadLock(&afs_xvcache);
2524 } /*afs_PutVCache */
2528 * Reset a vcache entry, so local contents are ignored, and the
2529 * server will be reconsulted next time the vcache is used
2531 * \param avc Pointer to the cache entry to reset
2534 * \note avc must be write locked on entry
2537 afs_ResetVCache(struct vcache *avc, afs_ucred_t *acred)
2539 ObtainWriteLock(&afs_xcbhash, 456);
2540 afs_DequeueCallback(avc);
2541 avc->f.states &= ~(CStatd | CDirty); /* next reference will re-stat */
2542 ReleaseWriteLock(&afs_xcbhash);
2543 /* now find the disk cache entries */
2544 afs_TryToSmush(avc, acred, 1);
2545 osi_dnlc_purgedp(avc);
2546 if (avc->linkData && !(avc->f.states & CCore)) {
2547 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
2548 avc->linkData = NULL;
2553 * Sleepa when searching for a vcache. Releases all the pending locks,
2554 * sleeps then obtains the previously released locks.
2556 * \param vcache Enter sleep state.
2557 * \param flag Determines what locks to use.
2562 findvc_sleep(struct vcache *avc, int flag)
2564 int fstates = avc->f.states;
2565 if (flag & IS_SLOCK) {
2566 ReleaseSharedLock(&afs_xvcache);
2568 if (flag & IS_WLOCK) {
2569 ReleaseWriteLock(&afs_xvcache);
2571 ReleaseReadLock(&afs_xvcache);
2574 if (flag & FIND_CDEAD) {
2575 ObtainWriteLock(&afs_xvcache, 342);
2576 afs_FlushReclaimedVcaches();
2577 if (fstates == avc->f.states) {
2578 ReleaseWriteLock(&afs_xvcache);
2579 afs_osi_Sleep(&avc->f.states);
2581 ReleaseWriteLock(&afs_xvcache);
2583 afs_osi_Sleep(&avc->f.states);
2584 if (flag & IS_SLOCK) {
2585 ObtainSharedLock(&afs_xvcache, 341);
2587 if (flag & IS_WLOCK) {
2588 ObtainWriteLock(&afs_xvcache, 343);
2590 ObtainReadLock(&afs_xvcache);
2596 * Add a reference on an existing vcache entry.
2598 * \param tvc Pointer to the vcache.
2600 * \note Environment: Must be called with at least one reference from
2601 * elsewhere on the vcache, even if that reference will be dropped.
2602 * The global lock is required.
2604 * \return 0 on success, -1 on failure.
2608 afs_RefVCache(struct vcache *tvc)
2610 #ifdef AFS_DARWIN80_ENV
2614 /* AFS_STATCNT(afs_RefVCache); */
2616 #ifdef AFS_DARWIN80_ENV
2620 if (vnode_ref(tvp)) {
2622 /* AFSTOV(tvc) may be NULL */
2631 } /*afs_RefVCache */
2634 * Find a vcache entry given a fid.
2636 * \param afid Pointer to the fid whose cache entry we desire.
2637 * \param retry (SGI-specific) tell the caller to drop the lock on xvcache,
2638 * unlock the vnode, and try again.
2639 * \param flag Bit 1 to specify whether to compute hit statistics. Not
2640 * set if FindVCache is called as part of internal bookkeeping.
2642 * \note Environment: Must be called with the afs_xvcache lock at least held at
2643 * the read level. In order to do the VLRU adjustment, the xvcache lock
2644 * must be shared-- we upgrade it here.
2648 afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
2653 #ifdef AFS_DARWIN80_ENV
2654 struct vcache *deadvc = NULL, *livevc = NULL;
2658 AFS_STATCNT(afs_FindVCache);
2662 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2663 if (FidMatches(afid, tvc)) {
2664 #ifdef AFS_DARWIN80_ENV
2665 if (flag & FIND_CDEAD) {
2666 if (tvc->f.states & (CDeadVnode|CBulkFetching)) {
2671 if (tvc->f.states & CDeadVnode)
2672 if ((tvc->f.states & CBulkFetching) &&
2673 !(flag & FIND_BULKDEAD))
2677 if (tvc->f.states & CVInit) {
2678 findvc_sleep(tvc, flag);
2681 #ifdef AFS_DARWIN80_ENV
2682 if (tvc->f.states & CDeadVnode) {
2683 findvc_sleep(tvc, flag);
2686 if (flag & FIND_CDEAD) {
2694 #ifdef AFS_DARWIN80_ENV
2695 if (flag & FIND_CDEAD) {
2696 if (livevc && deadvc) {
2697 /* discard deadvc */
2699 vnode_recycle(AFSTOV(deadvc));
2700 vnode_put(AFSTOV(deadvc));
2701 vnode_rele(AFSTOV(deadvc));
2706 /* return what's left */
2707 tvc = livevc ? livevc : deadvc;
2711 /* should I have a read lock on the vnode here? */
2715 #if defined(AFS_DARWIN80_ENV)
2719 if (tvp && vnode_ref(tvp)) {
2721 /* AFSTOV(tvc) may be NULL */
2726 if (tvp && (tvc->f.states & (CBulkFetching|CDeadVnode))) {
2728 vnode_recycle(AFSTOV(tvc));
2735 #elif defined(AFS_DARWIN_ENV)
2736 tvc->f.states |= CUBCinit;
2738 if (UBCINFOMISSING(AFSTOV(tvc)) ||
2739 UBCINFORECLAIMED(AFSTOV(tvc))) {
2740 ubc_info_init(AFSTOV(tvc));
2743 tvc->f.states &= ~CUBCinit;
2745 osi_vnhold(tvc, retry); /* already held, above */
2746 if (retry && *retry)
2750 * only move to front of vlru if we have proper vcache locking)
2752 if (flag & DO_VLRU) {
2753 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2754 refpanic("FindVC VLRU inconsistent1");
2756 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2757 refpanic("FindVC VLRU inconsistent1");
2759 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2760 refpanic("FindVC VLRU inconsistent2");
2762 UpgradeSToWLock(&afs_xvcache, 26);
2763 QRemove(&tvc->vlruq);
2764 QAdd(&VLRU, &tvc->vlruq);
2765 ConvertWToSLock(&afs_xvcache);
2766 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2767 refpanic("FindVC VLRU inconsistent1");
2769 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2770 refpanic("FindVC VLRU inconsistent2");
2772 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2773 refpanic("FindVC VLRU inconsistent3");
2779 if (flag & DO_STATS) {
2781 afs_stats_cmperf.vcacheHits++;
2783 afs_stats_cmperf.vcacheMisses++;
2784 if (afs_IsPrimaryCellNum(afid->Cell))
2785 afs_stats_cmperf.vlocalAccesses++;
2787 afs_stats_cmperf.vremoteAccesses++;
2790 } /*afs_FindVCache */
2793 * Find a vcache entry given a fid. Does a wildcard match on what we
2794 * have for the fid. If more than one entry, don't return anything.
2796 * \param avcp Fill in pointer if we found one and only one.
2797 * \param afid Pointer to the fid whose cache entry we desire.
2798 * \param retry (SGI-specific) tell the caller to drop the lock on xvcache,
2799 * unlock the vnode, and try again.
2800 * \param flags bit 1 to specify whether to compute hit statistics. Not
2801 * set if FindVCache is called as part of internal bookkeeping.
2803 * \note Environment: Must be called with the afs_xvcache lock at least held at
2804 * the read level. In order to do the VLRU adjustment, the xvcache lock
2805 * must be shared-- we upgrade it here.
2807 * \return Number of matches found.
2810 int afs_duplicate_nfs_fids = 0;
2813 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2817 afs_int32 count = 0;
2818 struct vcache *found_tvc = NULL;
2819 #ifdef AFS_DARWIN80_ENV
2823 AFS_STATCNT(afs_FindVCache);
2827 ObtainSharedLock(&afs_xvcache, 331);
2830 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2831 /* Match only on what we have.... */
2832 if (((tvc->f.fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2833 && (tvc->f.fid.Fid.Volume == afid->Fid.Volume)
2834 && ((tvc->f.fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2835 && (tvc->f.fid.Cell == afid->Cell)) {
2836 if (tvc->f.states & CVInit) {
2837 ReleaseSharedLock(&afs_xvcache);
2838 afs_osi_Sleep(&tvc->f.states);
2841 #ifdef AFS_DARWIN80_ENV
2842 if (tvc->f.states & CDeadVnode) {
2843 if (!(tvc->f.states & CBulkFetching)) {
2844 ReleaseSharedLock(&afs_xvcache);
2845 afs_osi_Sleep(&tvc->f.states);
2850 if (vnode_get(tvp)) {
2851 /* This vnode no longer exists. */
2854 if (vnode_ref(tvp)) {
2855 /* This vnode no longer exists. */
2857 /* AFSTOV(tvc) may be NULL */
2862 if (tvc->f.states & (CBulkFetching|CDeadVnode)) {
2864 vnode_recycle(AFSTOV(tvc));
2867 #endif /* AFS_DARWIN80_ENV */
2871 afs_duplicate_nfs_fids++;
2872 ReleaseSharedLock(&afs_xvcache);
2873 #ifdef AFS_DARWIN80_ENV
2874 /* Drop our reference counts. */
2875 vnode_put(AFSTOV(tvc));
2876 vnode_put(AFSTOV(found_tvc));
2885 /* should I have a read lock on the vnode here? */
2887 #ifndef AFS_DARWIN80_ENV
2888 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2889 afs_int32 retry = 0;
2890 osi_vnhold(tvc, &retry);
2893 found_tvc = (struct vcache *)0;
2894 ReleaseSharedLock(&afs_xvcache);
2895 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2899 osi_vnhold(tvc, (int *)0); /* already held, above */
2903 * We obtained the xvcache lock above.
2905 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2906 refpanic("FindVC VLRU inconsistent1");
2908 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2909 refpanic("FindVC VLRU inconsistent1");
2911 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2912 refpanic("FindVC VLRU inconsistent2");
2914 UpgradeSToWLock(&afs_xvcache, 568);
2915 QRemove(&tvc->vlruq);
2916 QAdd(&VLRU, &tvc->vlruq);
2917 ConvertWToSLock(&afs_xvcache);
2918 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2919 refpanic("FindVC VLRU inconsistent1");
2921 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2922 refpanic("FindVC VLRU inconsistent2");
2924 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2925 refpanic("FindVC VLRU inconsistent3");
2931 afs_stats_cmperf.vcacheHits++;
2933 afs_stats_cmperf.vcacheMisses++;
2934 if (afs_IsPrimaryCellNum(afid->Cell))
2935 afs_stats_cmperf.vlocalAccesses++;
2937 afs_stats_cmperf.vremoteAccesses++;
2939 *avcp = tvc; /* May be null */
2941 ReleaseSharedLock(&afs_xvcache);
2942 return (tvc ? 1 : 0);
2944 } /*afs_NFSFindVCache */
2950 * Initialize vcache related variables
2955 afs_vcacheInit(int astatSize)
2957 #if !defined(AFS_LINUX22_ENV)
2961 if (!afs_maxvcount) {
2962 afs_maxvcount = astatSize; /* no particular limit on linux? */
2964 #if !defined(AFS_LINUX22_ENV)
2968 AFS_RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2969 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2971 #if !defined(AFS_LINUX22_ENV)
2972 /* Allocate and thread the struct vcache entries */
2973 tvp = afs_osi_Alloc(astatSize * sizeof(struct vcache));
2974 osi_Assert(tvp != NULL);
2975 memset(tvp, 0, sizeof(struct vcache) * astatSize);
2977 Initial_freeVCList = tvp;
2978 freeVCList = &(tvp[0]);
2979 for (i = 0; i < astatSize - 1; i++) {
2980 tvp[i].nextfree = &(tvp[i + 1]);
2982 tvp[astatSize - 1].nextfree = NULL;
2983 # ifdef KERNEL_HAVE_PIN
2984 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
2988 #if defined(AFS_SGI_ENV)
2989 for (i = 0; i < astatSize; i++) {
2990 char name[METER_NAMSZ];
2991 struct vcache *tvc = &tvp[i];
2993 tvc->v.v_number = ++afsvnumbers;
2994 tvc->vc_rwlockid = OSI_NO_LOCKID;
2995 initnsema(&tvc->vc_rwlock, 1,
2996 makesname(name, "vrw", tvc->v.v_number));
2997 #ifndef AFS_SGI53_ENV
2998 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
3000 #ifndef AFS_SGI62_ENV
3001 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
3002 #endif /* AFS_SGI62_ENV */
3006 for(i = 0; i < VCSIZE; ++i)
3007 QInit(&afs_vhashTV[i]);
3014 shutdown_vcache(void)
3017 struct afs_cbr *tsp;
3019 * XXX We may potentially miss some of the vcaches because if when
3020 * there are no free vcache entries and all the vcache entries are active
3021 * ones then we allocate an additional one - admittedly we almost never
3026 struct afs_q *tq, *uq = NULL;
3028 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
3032 osi_FreeSmallSpace(tvc->mvid);
3033 tvc->mvid = (struct VenusFid *)0;
3036 aix_gnode_rele(AFSTOV(tvc));
3038 if (tvc->linkData) {
3039 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3044 * Also free the remaining ones in the Cache
3046 for (i = 0; i < VCSIZE; i++) {
3047 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3049 osi_FreeSmallSpace(tvc->mvid);
3050 tvc->mvid = (struct VenusFid *)0;
3054 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
3055 #ifdef AFS_AIX32_ENV
3058 vms_delete(tvc->segid);
3060 tvc->segid = tvc->vmh = NULL;
3061 if (VREFCOUNT_GT(tvc,0))
3062 osi_Panic("flushVcache: vm race");
3070 #if defined(AFS_SUN5_ENV)
3076 if (tvc->linkData) {
3077 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3082 afs_FreeAllAxs(&(tvc->Access));
3088 * Free any leftover callback queue
3090 for (i = 0; i < afs_stats_cmperf.CallBackAlloced; i++) {
3091 tsp = afs_cbrHeads[i];
3092 afs_cbrHeads[i] = 0;
3093 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
3097 #if !defined(AFS_LINUX22_ENV)
3098 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3100 # ifdef KERNEL_HAVE_PIN
3101 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3104 freeVCList = Initial_freeVCList = 0;
3107 AFS_RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
3108 LOCK_INIT(&afs_xvcb, "afs_xvcb");
3110 for(i = 0; i < VCSIZE; ++i)
3111 QInit(&afs_vhashTV[i]);
3115 afs_DisconGiveUpCallbacks(void)
3121 ObtainWriteLock(&afs_xvcache, 1002); /* XXX - should be a unique number */
3124 /* Somehow, walk the set of vcaches, with each one coming out as tvc */
3125 for (i = 0; i < VCSIZE; i++) {
3126 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3128 if (afs_QueueVCB(tvc, &slept)) {
3129 tvc->callback = NULL;
3138 ReleaseWriteLock(&afs_xvcache);
3145 * Clear the Statd flag from all vcaches
3147 * This function removes the Statd flag from all vcaches. It's used by
3148 * disconnected mode to tidy up during reconnection
3152 afs_ClearAllStatdFlag(void)
3157 ObtainWriteLock(&afs_xvcache, 715);
3159 for (i = 0; i < VCSIZE; i++) {
3160 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3161 tvc->f.states &= ~(CStatd|CUnique);
3164 ReleaseWriteLock(&afs_xvcache);