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(int nconns, struct rx_connection **rxconns,
354 struct afs_conn **conns)
359 results = afs_osi_Alloc(nconns * 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 (conns[i]->parent->srvr->server->cbrs)
379 afs_FreeCBR(conns[i]->parent->srvr->server->cbrs);
382 afs_osi_Free(results, nconns * 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(AFS_LS_ALL, 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 struct rx_connection *rxconn;
446 /* if buffer is full, or we've queued all we're going
447 * to from this server, we should flush out the
450 fidArray.AFSCBFids_len = tcount;
451 fidArray.AFSCBFids_val = (struct AFSFid *)tfids;
452 cbArray.AFSCBs_len = 1;
453 cbArray.AFSCBs_val = callBacks;
454 memset(&callBacks[0], 0, sizeof(callBacks[0]));
455 callBacks[0].CallBackType = CB_EXCLUSIVE;
456 for (safety3 = 0; safety3 < AFS_MAXHOSTS * 2; safety3++) {
457 tc = afs_ConnByHost(tsp, tsp->cell->fsport,
458 tsp->cell->cellNum, &treq, 0,
459 SHARED_LOCK, 0, &rxconn);
462 (AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS);
465 RXAFS_GiveUpCallBacks(rxconn, &fidArray,
472 (tc, rxconn, code, 0, &treq,
473 AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS, SHARED_LOCK,
478 /* ignore return code, since callbacks may have
479 * been returned anyway, we shouldn't leave them
480 * around to be returned again.
482 * Next, see if we are done with this server, and if so,
483 * break to deal with the next one.
489 /* if to flush full buffer */
490 /* if we make it here, we have an entry at the head of cbrs,
491 * which we should copy to the file ID array and then free.
494 tfids[tcount++] = tcbrp->fid;
496 /* Freeing the CBR will unlink it from the server's CBR list */
498 } /* while loop for this one server */
499 if (safety2 > afs_cacheStats) {
500 afs_warn("possible internal error afs_flushVCBs (%d)\n",
503 } /* for loop for this hash chain */
504 } /* loop through all hash chains */
505 if (safety1 > afs_totalServers + 2) {
507 ("AFS internal error (afs_flushVCBs) (%d > %d), continuing...\n",
508 safety1, afs_totalServers + 2);
510 osi_Panic("afs_flushVCBS safety1");
513 ReleaseReadLock(&afs_xserver);
515 ReleaseWriteLock(&afs_xvcb);
516 afs_osi_Free(tfids, sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
521 * Queue a callback on the given fid.
524 * Locks the xvcb lock.
525 * Called when the xvcache lock is already held.
526 * RACE: afs_xvcache may be dropped and reacquired
528 * \param avc vcache entry
529 * \param slep Set to 1 if we dropped afs_xvcache
530 * \return 1 if queued, 0 otherwise
534 afs_QueueVCB(struct vcache *avc, int *slept)
538 struct afs_cbr *tcbp;
541 AFS_STATCNT(afs_QueueVCB);
543 ObtainWriteLock(&afs_xvcb, 274);
545 /* we can't really give back callbacks on RO files, since the
546 * server only tracks them on a per-volume basis, and we don't
547 * know whether we still have some other files from the same
549 if (!((avc->f.states & CRO) == 0 && avc->callback)) {
553 /* The callback is really just a struct server ptr. */
554 tsp = (struct server *)(avc->callback);
557 /* If we don't have CBR space, AllocCBR may block or hit the net for
558 * clearing up CBRs. Hitting the net may involve a fileserver
559 * needing to contact us, so we must drop xvcache so we don't block
560 * those requests from going through. */
561 reacquire = *slept = 1;
562 ReleaseWriteLock(&afs_xvcache);
565 /* we now have a pointer to the server, so we just allocate
566 * a queue entry and queue it.
568 tcbp = afs_AllocCBR();
569 tcbp->fid = avc->f.fid.Fid;
571 tcbp->next = tsp->cbrs;
573 tsp->cbrs->pprev = &tcbp->next;
576 tcbp->pprev = &tsp->cbrs;
578 afs_InsertHashCBR(tcbp);
582 /* now release locks and return */
583 ReleaseWriteLock(&afs_xvcb);
586 /* make sure this is after dropping xvcb, for locking order */
587 ObtainWriteLock(&afs_xvcache, 279);
594 * Remove a queued callback for a given Fid.
597 * Locks xvcb and xserver locks.
598 * Typically called with xdcache, xvcache and/or individual vcache
601 * \param afid The fid we want cleansed of queued callbacks.
606 afs_RemoveVCB(struct VenusFid *afid)
609 struct afs_cbr *cbr, *ncbr;
611 AFS_STATCNT(afs_RemoveVCB);
612 ObtainWriteLock(&afs_xvcb, 275);
614 slot = afs_HashCBRFid(&afid->Fid);
615 ncbr = afs_cbrHashT[slot];
619 ncbr = cbr->hash_next;
621 if (afid->Fid.Volume == cbr->fid.Volume &&
622 afid->Fid.Vnode == cbr->fid.Vnode &&
623 afid->Fid.Unique == cbr->fid.Unique) {
628 ReleaseWriteLock(&afs_xvcb);
632 afs_FlushReclaimedVcaches(void)
634 #if !defined(AFS_LINUX22_ENV)
637 struct vcache *tmpReclaimedVCList = NULL;
639 ObtainWriteLock(&afs_xvreclaim, 76);
640 while (ReclaimedVCList) {
641 tvc = ReclaimedVCList; /* take from free list */
642 ReclaimedVCList = tvc->nextfree;
643 tvc->nextfree = NULL;
644 code = afs_FlushVCache(tvc, &fv_slept);
646 /* Ok, so, if we got code != 0, uh, wtf do we do? */
647 /* Probably, build a temporary list and then put all back when we
648 get to the end of the list */
649 /* This is actually really crappy, but we need to not leak these.
650 We probably need a way to be smarter about this. */
651 tvc->nextfree = tmpReclaimedVCList;
652 tmpReclaimedVCList = tvc;
653 /* printf("Reclaim list flush %lx failed: %d\n", (unsigned long) tvc, code); */
655 if (tvc->f.states & (CVInit
656 #ifdef AFS_DARWIN80_ENV
660 tvc->f.states &= ~(CVInit
661 #ifdef AFS_DARWIN80_ENV
665 afs_osi_Wakeup(&tvc->f.states);
668 if (tmpReclaimedVCList)
669 ReclaimedVCList = tmpReclaimedVCList;
671 ReleaseWriteLock(&afs_xvreclaim);
676 afs_PostPopulateVCache(struct vcache *avc, struct VenusFid *afid, int seq)
679 * The proper value for mvstat (for root fids) is setup by the caller.
682 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
685 if (afs_globalVFS == 0)
686 osi_Panic("afs globalvfs");
688 osi_PostPopulateVCache(avc);
691 osi_dnlc_purgedp(avc); /* this may be overkill */
692 memset(&(avc->callsort), 0, sizeof(struct afs_q));
694 avc->f.states &=~ CVInit;
696 avc->f.states |= CBulkFetching;
697 avc->f.m.Length = seq;
699 afs_osi_Wakeup(&avc->f.states);
703 afs_ShakeLooseVCaches(afs_int32 anumber)
707 struct afs_q *tq, *uq;
708 int fv_slept, defersleep = 0;
709 afs_int32 target = anumber;
713 for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
716 if (tvc->f.states & CVFlushed) {
717 refpanic("CVFlushed on VLRU");
718 /* In the other path, this was 2 * afs_cacheStats */
719 } else if (!afsd_dynamic_vcaches && i++ > afs_maxvcount) {
720 refpanic("Exceeded pool of AFS vnodes(VLRU cycle?)");
721 } else if (QNext(uq) != tq) {
722 refpanic("VLRU inconsistent");
723 } else if (tvc->f.states & CVInit) {
728 if (osi_TryEvictVCache(tvc, &fv_slept, defersleep))
736 continue; /* start over - may have raced. */
739 if (anumber && !defersleep) {
747 if (!afsd_dynamic_vcaches && anumber == target) {
748 afs_warn("afs_ShakeLooseVCaches: warning none freed, using %d of %d\n",
749 afs_vcount, afs_maxvcount);
755 /* Alloc new vnode. */
757 static struct vcache *
758 afs_AllocVCache(void)
762 tvc = osi_NewVnode();
767 if (afsd_dynamic_vcaches && afs_maxvcount < afs_vcount) {
768 afs_maxvcount = afs_vcount;
769 /*printf("peak vnodes: %d\n", afs_maxvcount);*/
772 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
774 /* If we create a new inode, we either give it a new slot number,
775 * or if one's available, use a slot number from the slot free list
777 if (afs_freeSlotList != NULL) {
778 struct afs_slotlist *tmp;
780 tvc->diskSlot = afs_freeSlotList->slot;
781 tmp = afs_freeSlotList;
782 afs_freeSlotList = tmp->next;
783 afs_osi_Free(tmp, sizeof(struct afs_slotlist));
785 tvc->diskSlot = afs_nextVcacheSlot++;
791 /* Pre populate a newly allocated vcache. On platforms where the actual
792 * vnode is attached to the vcache, this function is called before attachment,
793 * therefore it cannot perform any actions on the vnode itself */
796 afs_PrePopulateVCache(struct vcache *avc, struct VenusFid *afid,
797 struct server *serverp) {
800 slot = avc->diskSlot;
802 osi_PrePopulateVCache(avc);
804 avc->diskSlot = slot;
805 QZero(&avc->metadirty);
807 AFS_RWLOCK_INIT(&avc->lock, "vcache lock");
810 avc->linkData = NULL;
813 avc->execsOrWriters = 0;
815 avc->f.states = CVInit;
816 avc->last_looker = 0;
818 avc->asynchrony = -1;
822 avc->f.truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
823 hzero(avc->f.m.DataVersion); /* in case we copy it into flushDV */
825 avc->callback = serverp; /* to minimize chance that clear
828 #if defined(AFS_CACHE_BYPASS)
829 avc->cachingStates = 0;
830 avc->cachingTransitions = 0;
835 afs_FlushAllVCaches(void)
838 struct vcache *tvc, *nvc;
840 ObtainWriteLock(&afs_xvcache, 867);
843 for (i = 0; i < VCSIZE; i++) {
844 for (tvc = afs_vhashT[i]; tvc; tvc = nvc) {
848 if (afs_FlushVCache(tvc, &slept)) {
849 afs_warn("Failed to flush vcache 0x%lx\n", (unsigned long)(uintptrsz)tvc);
858 ReleaseWriteLock(&afs_xvcache);
862 * This routine is responsible for allocating a new cache entry
863 * from the free list. It formats the cache entry and inserts it
864 * into the appropriate hash tables. It must be called with
865 * afs_xvcache write-locked so as to prevent several processes from
866 * trying to create a new cache entry simultaneously.
868 * LOCK: afs_NewVCache afs_xvcache W
870 * \param afid The file id of the file whose cache entry is being created.
872 * \return The new vcache struct.
875 static_inline struct vcache *
876 afs_NewVCache_int(struct VenusFid *afid, struct server *serverp, int seq)
880 afs_int32 anumber = VCACHE_FREE;
882 AFS_STATCNT(afs_NewVCache);
884 afs_FlushReclaimedVcaches();
886 #if defined(AFS_LINUX22_ENV)
887 if(!afsd_dynamic_vcaches && afs_vcount >= afs_maxvcount) {
888 afs_ShakeLooseVCaches(anumber);
889 if (afs_vcount >= afs_maxvcount) {
890 afs_warn("afs_NewVCache - none freed\n");
894 tvc = afs_AllocVCache();
895 #else /* AFS_LINUX22_ENV */
896 /* pull out a free cache entry */
898 afs_ShakeLooseVCaches(anumber);
902 tvc = afs_AllocVCache();
904 tvc = freeVCList; /* take from free list */
905 freeVCList = tvc->nextfree;
906 tvc->nextfree = NULL;
907 afs_vcount++; /* balanced by FlushVCache */
908 } /* end of if (!freeVCList) */
910 #endif /* AFS_LINUX22_ENV */
912 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
914 panic("afs_NewVCache(): free vcache with vnode attached");
917 /* Populate the vcache with as much as we can. */
918 afs_PrePopulateVCache(tvc, afid, serverp);
920 /* Thread the vcache onto the VLRU */
925 tvc->hnext = afs_vhashT[i];
927 QAdd(&afs_vhashTV[j], &tvc->vhashq);
929 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
930 refpanic("NewVCache VLRU inconsistent");
932 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
933 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
934 refpanic("NewVCache VLRU inconsistent2");
936 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
937 refpanic("NewVCache VLRU inconsistent3");
939 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
940 refpanic("NewVCache VLRU inconsistent4");
944 /* it should now be safe to drop the xvcache lock - so attach an inode
945 * to this vcache, where necessary */
946 osi_AttachVnode(tvc, seq);
948 /* Get a reference count to hold this vcache for the VLRUQ. Note that
949 * we have to do this after attaching the vnode, because the reference
950 * count may be held in the vnode itself */
952 #if defined(AFS_LINUX22_ENV)
953 /* Hold it for the LRU (should make count 2) */
955 #elif !(defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV))
956 VREFCOUNT_SET(tvc, 1); /* us */
959 #if defined (AFS_FBSD_ENV)
960 if (tvc->f.states & CVInit)
962 afs_PostPopulateVCache(tvc, afid, seq);
969 afs_NewVCache(struct VenusFid *afid, struct server *serverp)
971 return afs_NewVCache_int(afid, serverp, 0);
975 afs_NewBulkVCache(struct VenusFid *afid, struct server *serverp, int seq)
977 return afs_NewVCache_int(afid, serverp, seq);
983 * LOCK: afs_FlushActiveVcaches afs_xvcache N
985 * \param doflocks : Do we handle flocks?
988 afs_FlushActiveVcaches(afs_int32 doflocks)
994 afs_ucred_t *cred = NULL;
995 struct vrequest treq, ureq;
996 struct AFSVolSync tsync;
999 AFS_STATCNT(afs_FlushActiveVcaches);
1000 ObtainReadLock(&afs_xvcache);
1001 for (i = 0; i < VCSIZE; i++) {
1002 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1003 if (tvc->f.states & CVInit) continue;
1004 #ifdef AFS_DARWIN80_ENV
1005 if (tvc->f.states & CDeadVnode &&
1006 (tvc->f.states & (CCore|CUnlinkedDel) ||
1007 tvc->flockCount)) panic("Dead vnode has core/unlinkedel/flock");
1009 if (doflocks && tvc->flockCount != 0) {
1010 struct rx_connection *rxconn;
1011 /* if this entry has an flock, send a keep-alive call out */
1013 ReleaseReadLock(&afs_xvcache);
1014 ObtainWriteLock(&tvc->lock, 51);
1016 afs_InitReq(&treq, afs_osi_credp);
1017 treq.flags |= O_NONBLOCK;
1019 tc = afs_Conn(&tvc->f.fid, &treq, SHARED_LOCK, &rxconn);
1021 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
1024 RXAFS_ExtendLock(rxconn,
1025 (struct AFSFid *)&tvc->f.fid.Fid,
1031 } while (afs_Analyze
1032 (tc, rxconn, code, &tvc->f.fid, &treq,
1033 AFS_STATS_FS_RPCIDX_EXTENDLOCK, SHARED_LOCK, NULL));
1035 ReleaseWriteLock(&tvc->lock);
1036 #ifdef AFS_DARWIN80_ENV
1038 ObtainReadLock(&afs_xvcache);
1040 ObtainReadLock(&afs_xvcache);
1045 if ((tvc->f.states & CCore) || (tvc->f.states & CUnlinkedDel)) {
1047 * Don't let it evaporate in case someone else is in
1048 * this code. Also, drop the afs_xvcache lock while
1049 * getting vcache locks.
1052 ReleaseReadLock(&afs_xvcache);
1053 #ifdef AFS_BOZONLOCK_ENV
1054 afs_BozonLock(&tvc->pvnLock, tvc);
1056 #if defined(AFS_SGI_ENV)
1058 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1060 osi_Assert(VREFCOUNT_GT(tvc,0));
1061 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1063 ObtainWriteLock(&tvc->lock, 52);
1064 if (tvc->f.states & CCore) {
1065 tvc->f.states &= ~CCore;
1066 /* XXXX Find better place-holder for cred XXXX */
1067 cred = (afs_ucred_t *)tvc->linkData;
1068 tvc->linkData = NULL; /* XXX */
1069 afs_InitReq(&ureq, cred);
1070 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1071 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
1072 tvc->execsOrWriters);
1073 code = afs_StoreOnLastReference(tvc, &ureq);
1074 ReleaseWriteLock(&tvc->lock);
1075 #ifdef AFS_BOZONLOCK_ENV
1076 afs_BozonUnlock(&tvc->pvnLock, tvc);
1078 hzero(tvc->flushDV);
1081 if (code && code != VNOVNODE) {
1082 afs_StoreWarn(code, tvc->f.fid.Fid.Volume,
1083 /* /dev/console */ 1);
1085 } else if (tvc->f.states & CUnlinkedDel) {
1089 ReleaseWriteLock(&tvc->lock);
1090 #ifdef AFS_BOZONLOCK_ENV
1091 afs_BozonUnlock(&tvc->pvnLock, tvc);
1093 #if defined(AFS_SGI_ENV)
1094 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1096 afs_remunlink(tvc, 0);
1097 #if defined(AFS_SGI_ENV)
1098 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1101 /* lost (or won, perhaps) the race condition */
1102 ReleaseWriteLock(&tvc->lock);
1103 #ifdef AFS_BOZONLOCK_ENV
1104 afs_BozonUnlock(&tvc->pvnLock, tvc);
1107 #if defined(AFS_SGI_ENV)
1108 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1110 #ifdef AFS_DARWIN80_ENV
1113 AFS_RELE(AFSTOV(tvc));
1114 /* Matches write code setting CCore flag */
1117 ObtainReadLock(&afs_xvcache);
1119 ObtainReadLock(&afs_xvcache);
1122 AFS_RELE(AFSTOV(tvc));
1123 /* Matches write code setting CCore flag */
1130 ReleaseReadLock(&afs_xvcache);
1136 * Make sure a cache entry is up-to-date status-wise.
1138 * NOTE: everywhere that calls this can potentially be sped up
1139 * by checking CStatd first, and avoiding doing the InitReq
1140 * if this is up-to-date.
1142 * Anymore, the only places that call this KNOW already that the
1143 * vcache is not up-to-date, so we don't screw around.
1145 * \param avc : Ptr to vcache entry to verify.
1151 * Make sure a cache entry is up-to-date status-wise.
1153 * NOTE: everywhere that calls this can potentially be sped up
1154 * by checking CStatd first, and avoiding doing the InitReq
1155 * if this is up-to-date.
1157 * Anymore, the only places that call this KNOW already that the
1158 * vcache is not up-to-date, so we don't screw around.
1160 * \param avc Pointer to vcache entry to verify.
1163 * \return 0 for success or other error codes.
1166 afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1170 AFS_STATCNT(afs_VerifyVCache);
1172 /* otherwise we must fetch the status info */
1174 ObtainWriteLock(&avc->lock, 53);
1175 if (avc->f.states & CStatd) {
1176 ReleaseWriteLock(&avc->lock);
1179 ObtainWriteLock(&afs_xcbhash, 461);
1180 avc->f.states &= ~(CStatd | CUnique);
1181 avc->callback = NULL;
1182 afs_DequeueCallback(avc);
1183 ReleaseWriteLock(&afs_xcbhash);
1184 ReleaseWriteLock(&avc->lock);
1186 /* since we've been called back, or the callback has expired,
1187 * it's possible that the contents of this directory, or this
1188 * file's name have changed, thus invalidating the dnlc contents.
1190 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
1191 osi_dnlc_purgedp(avc);
1193 osi_dnlc_purgevp(avc);
1195 /* fetch the status info */
1196 tvc = afs_GetVCache(&avc->f.fid, areq, NULL, avc);
1199 /* Put it back; caller has already incremented vrefCount */
1203 } /*afs_VerifyVCache */
1207 * Simple copy of stat info into cache.
1209 * Callers:as of 1992-04-29, only called by WriteVCache
1211 * \param avc Ptr to vcache entry involved.
1212 * \param astat Ptr to stat info to copy.
1216 afs_SimpleVStat(struct vcache *avc,
1217 struct AFSFetchStatus *astat, struct vrequest *areq)
1220 AFS_STATCNT(afs_SimpleVStat);
1222 #ifdef AFS_64BIT_CLIENT
1223 FillInt64(length, astat->Length_hi, astat->Length);
1224 #else /* AFS_64BIT_CLIENT */
1225 length = astat->Length;
1226 #endif /* AFS_64BIT_CLIENT */
1228 #if defined(AFS_SGI_ENV)
1229 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1230 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1231 osi_Assert((valusema(&avc->vc_rwlock) <= 0)
1232 && (OSI_GET_LOCKID() == avc->vc_rwlockid));
1233 if (length < avc->f.m.Length) {
1234 vnode_t *vp = (vnode_t *) avc;
1236 osi_Assert(WriteLocked(&avc->lock));
1237 ReleaseWriteLock(&avc->lock);
1239 PTOSSVP(vp, (off_t) length, (off_t) MAXLONG);
1241 ObtainWriteLock(&avc->lock, 67);
1246 if (!afs_DirtyPages(avc)) {
1247 /* if actively writing the file, don't fetch over this value */
1248 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT, ICL_TYPE_POINTER, avc,
1249 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1250 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1251 avc->f.m.Length = length;
1252 avc->f.m.Date = astat->ClientModTime;
1254 avc->f.m.Owner = astat->Owner;
1255 avc->f.m.Group = astat->Group;
1256 avc->f.m.Mode = astat->UnixModeBits;
1257 if (vType(avc) == VREG) {
1258 avc->f.m.Mode |= S_IFREG;
1259 } else if (vType(avc) == VDIR) {
1260 avc->f.m.Mode |= S_IFDIR;
1261 } else if (vType(avc) == VLNK) {
1262 avc->f.m.Mode |= S_IFLNK;
1263 if ((avc->f.m.Mode & 0111) == 0)
1266 if (avc->f.states & CForeign) {
1267 struct axscache *ac;
1268 avc->f.anyAccess = astat->AnonymousAccess;
1270 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1272 * Caller has at least one bit not covered by anonymous, and
1273 * thus may have interesting rights.
1275 * HOWEVER, this is a really bad idea, because any access query
1276 * for bits which aren't covered by anonymous, on behalf of a user
1277 * who doesn't have any special rights, will result in an answer of
1278 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1279 * It's an especially bad idea under Ultrix, since (due to the lack of
1280 * a proper access() call) it must perform several afs_access() calls
1281 * in order to create magic mode bits that vary according to who makes
1282 * the call. In other words, _every_ stat() generates a test for
1285 #endif /* badidea */
1286 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1287 ac->axess = astat->CallerAccess;
1288 else /* not found, add a new one if possible */
1289 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1292 } /*afs_SimpleVStat */
1296 * Store the status info *only* back to the server for a
1299 * Environment: Must be called with a shared lock held on the vnode.
1301 * \param avc Ptr to the vcache entry.
1302 * \param astatus Ptr to the status info to store.
1303 * \param areq Ptr to the associated vrequest.
1305 * \return Operation status.
1309 afs_WriteVCache(struct vcache *avc,
1310 struct AFSStoreStatus *astatus,
1311 struct vrequest *areq)
1314 struct afs_conn *tc;
1315 struct AFSFetchStatus OutStatus;
1316 struct AFSVolSync tsync;
1317 struct rx_connection *rxconn;
1319 AFS_STATCNT(afs_WriteVCache);
1320 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1321 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
1323 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK, &rxconn);
1325 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1328 RXAFS_StoreStatus(rxconn, (struct AFSFid *)&avc->f.fid.Fid,
1329 astatus, &OutStatus, &tsync);
1334 } while (afs_Analyze
1335 (tc, rxconn, code, &avc->f.fid, areq, AFS_STATS_FS_RPCIDX_STORESTATUS,
1336 SHARED_LOCK, NULL));
1338 UpgradeSToWLock(&avc->lock, 20);
1340 /* success, do the changes locally */
1341 afs_SimpleVStat(avc, &OutStatus, areq);
1343 * Update the date, too. SimpleVStat didn't do this, since
1344 * it thought we were doing this after fetching new status
1345 * over a file being written.
1347 avc->f.m.Date = OutStatus.ClientModTime;
1349 /* failure, set up to check with server next time */
1350 ObtainWriteLock(&afs_xcbhash, 462);
1351 afs_DequeueCallback(avc);
1352 avc->f.states &= ~(CStatd | CUnique); /* turn off stat valid flag */
1353 ReleaseWriteLock(&afs_xcbhash);
1354 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
1355 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
1357 ConvertWToSLock(&avc->lock);
1360 } /*afs_WriteVCache */
1363 * Store status info only locally, set the proper disconnection flags
1364 * and add to dirty list.
1366 * \param avc The vcache to be written locally.
1367 * \param astatus Get attr fields from local store.
1368 * \param attrs This one is only of the vs_size.
1370 * \note Must be called with a shared lock on the vnode
1373 afs_WriteVCacheDiscon(struct vcache *avc,
1374 struct AFSStoreStatus *astatus,
1375 struct vattr *attrs)
1378 afs_int32 flags = 0;
1380 UpgradeSToWLock(&avc->lock, 700);
1382 if (!astatus->Mask) {
1388 /* Set attributes. */
1389 if (astatus->Mask & AFS_SETMODTIME) {
1390 avc->f.m.Date = astatus->ClientModTime;
1391 flags |= VDisconSetTime;
1394 if (astatus->Mask & AFS_SETOWNER) {
1395 /* printf("Not allowed yet. \n"); */
1396 /*avc->f.m.Owner = astatus->Owner;*/
1399 if (astatus->Mask & AFS_SETGROUP) {
1400 /* printf("Not allowed yet. \n"); */
1401 /*avc->f.m.Group = astatus->Group;*/
1404 if (astatus->Mask & AFS_SETMODE) {
1405 avc->f.m.Mode = astatus->UnixModeBits;
1407 #if 0 /* XXX: Leaving this out, so it doesn't mess up the file type flag.*/
1409 if (vType(avc) == VREG) {
1410 avc->f.m.Mode |= S_IFREG;
1411 } else if (vType(avc) == VDIR) {
1412 avc->f.m.Mode |= S_IFDIR;
1413 } else if (vType(avc) == VLNK) {
1414 avc->f.m.Mode |= S_IFLNK;
1415 if ((avc->f.m.Mode & 0111) == 0)
1419 flags |= VDisconSetMode;
1420 } /* if(astatus.Mask & AFS_SETMODE) */
1422 } /* if (!astatus->Mask) */
1424 if (attrs->va_size > 0) {
1425 /* XXX: Do I need more checks? */
1426 /* Truncation operation. */
1427 flags |= VDisconTrunc;
1431 afs_DisconAddDirty(avc, flags, 1);
1433 /* XXX: How about the rest of the fields? */
1435 ConvertWToSLock(&avc->lock);
1441 * Copy astat block into vcache info
1443 * \note This code may get dataversion and length out of sync if the file has
1444 * been modified. This is less than ideal. I haven't thought about it sufficiently
1445 * to be certain that it is adequate.
1447 * \note Environment: Must be called under a write lock
1449 * \param avc Ptr to vcache entry.
1450 * \param astat Ptr to stat block to copy in.
1451 * \param areq Ptr to associated request.
1454 afs_ProcessFS(struct vcache *avc,
1455 struct AFSFetchStatus *astat, struct vrequest *areq)
1458 #ifdef AFS_DARWIN80_ENV
1461 AFS_STATCNT(afs_ProcessFS);
1463 #ifdef AFS_64BIT_CLIENT
1464 FillInt64(length, astat->Length_hi, astat->Length);
1465 #else /* AFS_64BIT_CLIENT */
1466 length = astat->Length;
1467 #endif /* AFS_64BIT_CLIENT */
1468 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1469 * number for each bulk status request. Under no circumstances
1470 * should afs_DoBulkStat store a sequence number if the new
1471 * length will be ignored when afs_ProcessFS is called with
1472 * new stats. If you change the following conditional then you
1473 * also need to change the conditional in afs_DoBulkStat. */
1475 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1476 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1478 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1480 /* if we're writing or mapping this file, don't fetch over these
1483 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1484 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1485 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1486 avc->f.m.Length = length;
1487 avc->f.m.Date = astat->ClientModTime;
1489 hset64(avc->f.m.DataVersion, astat->dataVersionHigh, astat->DataVersion);
1490 avc->f.m.Owner = astat->Owner;
1491 avc->f.m.Mode = astat->UnixModeBits;
1492 avc->f.m.Group = astat->Group;
1493 avc->f.m.LinkCount = astat->LinkCount;
1494 if (astat->FileType == File) {
1495 #ifdef AFS_DARWIN80_ENV
1496 if (avc->f.m.Type != VREG)
1499 vSetType(avc, VREG);
1500 avc->f.m.Mode |= S_IFREG;
1501 } else if (astat->FileType == Directory) {
1502 #ifdef AFS_DARWIN80_ENV
1503 if (avc->f.m.Type != VDIR)
1506 vSetType(avc, VDIR);
1507 avc->f.m.Mode |= S_IFDIR;
1508 } else if (astat->FileType == SymbolicLink) {
1509 if (afs_fakestat_enable && (avc->f.m.Mode & 0111) == 0) {
1510 #ifdef AFS_DARWIN80_ENV
1511 if (avc->f.m.Type != VDIR)
1514 vSetType(avc, VDIR);
1515 avc->f.m.Mode |= S_IFDIR;
1517 #ifdef AFS_DARWIN80_ENV
1518 if (avc->f.m.Type != VLNK)
1521 vSetType(avc, VLNK);
1522 avc->f.m.Mode |= S_IFLNK;
1524 if ((avc->f.m.Mode & 0111) == 0) {
1528 #ifdef AFS_DARWIN80_ENV
1530 printf("found mistyped vnode!\n");
1532 avc->f.anyAccess = astat->AnonymousAccess;
1534 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1536 * Caller has at least one bit not covered by anonymous, and
1537 * thus may have interesting rights.
1539 * HOWEVER, this is a really bad idea, because any access query
1540 * for bits which aren't covered by anonymous, on behalf of a user
1541 * who doesn't have any special rights, will result in an answer of
1542 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1543 * It's an especially bad idea under Ultrix, since (due to the lack of
1544 * a proper access() call) it must perform several afs_access() calls
1545 * in order to create magic mode bits that vary according to who makes
1546 * the call. In other words, _every_ stat() generates a test for
1549 #endif /* badidea */
1551 struct axscache *ac;
1552 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1553 ac->axess = astat->CallerAccess;
1554 else /* not found, add a new one if possible */
1555 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1557 } /*afs_ProcessFS */
1561 * Get fid from server.
1564 * \param areq Request to be passed on.
1565 * \param name Name of ?? to lookup.
1566 * \param OutStatus Fetch status.
1571 * \return Success status of operation.
1574 afs_RemoteLookup(struct VenusFid *afid, struct vrequest *areq,
1575 char *name, struct VenusFid *nfid,
1576 struct AFSFetchStatus *OutStatusp,
1577 struct AFSCallBack *CallBackp, struct server **serverp,
1578 struct AFSVolSync *tsyncp)
1581 struct afs_conn *tc;
1582 struct rx_connection *rxconn;
1583 struct AFSFetchStatus OutDirStatus;
1586 name = ""; /* XXX */
1588 tc = afs_Conn(afid, areq, SHARED_LOCK, &rxconn);
1591 *serverp = tc->parent->srvr->server;
1592 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1595 RXAFS_Lookup(rxconn, (struct AFSFid *)&afid->Fid, name,
1596 (struct AFSFid *)&nfid->Fid, OutStatusp,
1597 &OutDirStatus, CallBackp, tsyncp);
1602 } while (afs_Analyze
1603 (tc, rxconn, code, afid, areq, AFS_STATS_FS_RPCIDX_XLOOKUP, SHARED_LOCK,
1613 * Given a file id and a vrequest structure, fetch the status
1614 * information associated with the file.
1616 * \param afid File ID.
1617 * \param areq Ptr to associated vrequest structure, specifying the
1618 * user whose authentication tokens will be used.
1619 * \param avc Caller may already have a vcache for this file, which is
1622 * \note Environment:
1623 * The cache entry is returned with an increased vrefCount field.
1624 * The entry must be discarded by calling afs_PutVCache when you
1625 * are through using the pointer to the cache entry.
1627 * You should not hold any locks when calling this function, except
1628 * locks on other vcache entries. If you lock more than one vcache
1629 * entry simultaneously, you should lock them in this order:
1631 * 1. Lock all files first, then directories.
1632 * 2. Within a particular type, lock entries in Fid.Vnode order.
1634 * This locking hierarchy is convenient because it allows locking
1635 * of a parent dir cache entry, given a file (to check its access
1636 * control list). It also allows renames to be handled easily by
1637 * locking directories in a constant order.
1639 * \note NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1641 * \note Might have a vcache structure already, which must
1642 * already be held by the caller
1645 afs_GetVCache(struct VenusFid *afid, struct vrequest *areq,
1646 afs_int32 * cached, struct vcache *avc)
1649 afs_int32 code, newvcache = 0;
1654 AFS_STATCNT(afs_GetVCache);
1657 *cached = 0; /* Init just in case */
1659 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1663 ObtainSharedLock(&afs_xvcache, 5);
1665 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU | IS_SLOCK);
1667 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1668 ReleaseSharedLock(&afs_xvcache);
1669 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1676 osi_Assert((tvc->f.states & CVInit) == 0);
1677 /* If we are in readdir, return the vnode even if not statd */
1678 if ((tvc->f.states & CStatd) || afs_InReadDir(tvc)) {
1679 ReleaseSharedLock(&afs_xvcache);
1683 UpgradeSToWLock(&afs_xvcache, 21);
1685 /* no cache entry, better grab one */
1686 tvc = afs_NewVCache(afid, NULL);
1689 ConvertWToSLock(&afs_xvcache);
1692 ReleaseSharedLock(&afs_xvcache);
1696 afs_stats_cmperf.vcacheMisses++;
1699 ReleaseSharedLock(&afs_xvcache);
1701 ObtainWriteLock(&tvc->lock, 54);
1703 if (tvc->f.states & CStatd) {
1704 ReleaseWriteLock(&tvc->lock);
1707 #ifdef AFS_DARWIN80_ENV
1708 /* Darwin 8.0 only has bufs in nfs, so we shouldn't have to worry about them.
1711 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
1713 * XXX - I really don't like this. Should try to understand better.
1714 * It seems that sometimes, when we get called, we already hold the
1715 * lock on the vnode (e.g., from afs_getattr via afs_VerifyVCache).
1716 * We can't drop the vnode lock, because that could result in a race.
1717 * Sometimes, though, we get here and don't hold the vnode lock.
1718 * I hate code paths that sometimes hold locks and sometimes don't.
1719 * In any event, the dodge we use here is to check whether the vnode
1720 * is locked, and if it isn't, then we gain and drop it around the call
1721 * to vinvalbuf; otherwise, we leave it alone.
1724 struct vnode *vp = AFSTOV(tvc);
1727 #if defined(AFS_DARWIN_ENV)
1728 iheldthelock = VOP_ISLOCKED(vp);
1730 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, current_proc());
1731 /* this is messy. we can call fsync which will try to reobtain this */
1732 if (VTOAFS(vp) == tvc)
1733 ReleaseWriteLock(&tvc->lock);
1734 if (UBCINFOEXISTS(vp)) {
1735 vinvalbuf(vp, V_SAVE, &afs_osi_cred, current_proc(), PINOD, 0);
1737 if (VTOAFS(vp) == tvc)
1738 ObtainWriteLock(&tvc->lock, 954);
1740 VOP_UNLOCK(vp, LK_EXCLUSIVE, current_proc());
1741 #elif defined(AFS_FBSD80_ENV)
1742 iheldthelock = VOP_ISLOCKED(vp);
1743 if (!iheldthelock) {
1744 /* nosleep/sleep lock order reversal */
1745 int glocked = ISAFS_GLOCK();
1748 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1752 vinvalbuf(vp, V_SAVE, PINOD, 0); /* changed late in 8.0-CURRENT */
1755 #elif defined(AFS_FBSD60_ENV)
1756 iheldthelock = VOP_ISLOCKED(vp, curthread);
1758 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1760 vinvalbuf(vp, V_SAVE, curthread, PINOD, 0);
1763 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1764 #elif defined(AFS_FBSD_ENV)
1765 iheldthelock = VOP_ISLOCKED(vp, curthread);
1767 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1768 vinvalbuf(vp, V_SAVE, osi_curcred(), curthread, PINOD, 0);
1770 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1771 #elif defined(AFS_OBSD_ENV)
1772 iheldthelock = VOP_ISLOCKED(vp, curproc);
1774 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1775 uvm_vnp_uncache(vp);
1777 VOP_UNLOCK(vp, 0, curproc);
1778 #elif defined(AFS_NBSD40_ENV)
1779 iheldthelock = VOP_ISLOCKED(vp);
1780 if (!iheldthelock) {
1781 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1783 uvm_vnp_uncache(vp);
1791 ObtainWriteLock(&afs_xcbhash, 464);
1792 tvc->f.states &= ~CUnique;
1794 afs_DequeueCallback(tvc);
1795 ReleaseWriteLock(&afs_xcbhash);
1797 /* It is always appropriate to throw away all the access rights? */
1798 afs_FreeAllAxs(&(tvc->Access));
1799 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1801 if ((tvp->states & VForeign)) {
1803 tvc->f.states |= CForeign;
1804 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1805 && (tvp->rootUnique == afid->Fid.Unique)) {
1809 if (tvp->states & VRO)
1810 tvc->f.states |= CRO;
1811 if (tvp->states & VBackup)
1812 tvc->f.states |= CBackup;
1813 /* now copy ".." entry back out of volume structure, if necessary */
1814 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1816 tvc->mvid = (struct VenusFid *)
1817 osi_AllocSmallSpace(sizeof(struct VenusFid));
1818 *tvc->mvid = tvp->dotdot;
1820 afs_PutVolume(tvp, READ_LOCK);
1824 afs_RemoveVCB(afid);
1826 struct AFSFetchStatus OutStatus;
1828 if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1829 afs_ProcessFS(tvc, &OutStatus, areq);
1830 tvc->f.states |= CStatd | CUnique;
1831 tvc->f.parent.vnode = OutStatus.ParentVnode;
1832 tvc->f.parent.unique = OutStatus.ParentUnique;
1836 if (AFS_IS_DISCONNECTED) {
1837 /* Nothing to do otherwise...*/
1839 /* printf("Network is down in afs_GetCache"); */
1841 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1843 /* For the NFS translator's benefit, make sure
1844 * non-directory vnodes always have their parent FID set
1845 * correctly, even when created as a result of decoding an
1846 * NFS filehandle. It would be nice to also do this for
1847 * directories, but we can't because the fileserver fills
1848 * in the FID of the directory itself instead of that of
1851 if (!code && OutStatus.FileType != Directory &&
1852 !tvc->f.parent.vnode) {
1853 tvc->f.parent.vnode = OutStatus.ParentVnode;
1854 tvc->f.parent.unique = OutStatus.ParentUnique;
1855 /* XXX - SXW - It's conceivable we should mark ourselves
1856 * as dirty again here, incase we've been raced
1857 * out of the FetchStatus call.
1864 ReleaseWriteLock(&tvc->lock);
1870 ReleaseWriteLock(&tvc->lock);
1873 } /*afs_GetVCache */
1878 * Lookup a vcache by fid. Look inside the cache first, if not
1879 * there, lookup the file on the server, and then get it's fresh
1884 * \param cached Is element cached? If NULL, don't answer.
1888 * \return The found element or NULL.
1891 afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1892 afs_int32 * cached, struct vcache *adp, char *aname)
1894 afs_int32 code, now, newvcache = 0;
1895 struct VenusFid nfid;
1898 struct AFSFetchStatus OutStatus;
1899 struct AFSCallBack CallBack;
1900 struct AFSVolSync tsync;
1901 struct server *serverp = 0;
1905 AFS_STATCNT(afs_GetVCache);
1907 *cached = 0; /* Init just in case */
1909 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1913 ObtainReadLock(&afs_xvcache);
1914 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */ );
1917 ReleaseReadLock(&afs_xvcache);
1919 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1920 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1924 ObtainReadLock(&tvc->lock);
1926 if (tvc->f.states & CStatd) {
1930 ReleaseReadLock(&tvc->lock);
1933 tvc->f.states &= ~CUnique;
1935 ReleaseReadLock(&tvc->lock);
1937 ObtainReadLock(&afs_xvcache);
1940 ReleaseReadLock(&afs_xvcache);
1942 /* lookup the file */
1945 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
1947 if (AFS_IS_DISCONNECTED) {
1948 /* printf("Network is down in afs_LookupVcache\n"); */
1952 afs_RemoteLookup(&adp->f.fid, areq, aname, &nfid, &OutStatus,
1953 &CallBack, &serverp, &tsync);
1955 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1959 ObtainSharedLock(&afs_xvcache, 6);
1960 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU | IS_SLOCK/* no xstats now */ );
1962 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1963 ReleaseSharedLock(&afs_xvcache);
1964 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1970 /* no cache entry, better grab one */
1971 UpgradeSToWLock(&afs_xvcache, 22);
1972 tvc = afs_NewVCache(&nfid, serverp);
1974 ConvertWToSLock(&afs_xvcache);
1977 ReleaseSharedLock(&afs_xvcache);
1982 ReleaseSharedLock(&afs_xvcache);
1983 ObtainWriteLock(&tvc->lock, 55);
1985 /* It is always appropriate to throw away all the access rights? */
1986 afs_FreeAllAxs(&(tvc->Access));
1987 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
1989 if ((tvp->states & VForeign)) {
1991 tvc->f.states |= CForeign;
1992 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1993 && (tvp->rootUnique == afid->Fid.Unique))
1996 if (tvp->states & VRO)
1997 tvc->f.states |= CRO;
1998 if (tvp->states & VBackup)
1999 tvc->f.states |= CBackup;
2000 /* now copy ".." entry back out of volume structure, if necessary */
2001 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2003 tvc->mvid = (struct VenusFid *)
2004 osi_AllocSmallSpace(sizeof(struct VenusFid));
2005 *tvc->mvid = tvp->dotdot;
2010 ObtainWriteLock(&afs_xcbhash, 465);
2011 afs_DequeueCallback(tvc);
2012 tvc->f.states &= ~(CStatd | CUnique);
2013 ReleaseWriteLock(&afs_xcbhash);
2014 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2015 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2017 afs_PutVolume(tvp, READ_LOCK);
2018 ReleaseWriteLock(&tvc->lock);
2023 ObtainWriteLock(&afs_xcbhash, 466);
2024 if (origCBs == afs_allCBs) {
2025 if (CallBack.ExpirationTime) {
2026 tvc->callback = serverp;
2027 tvc->cbExpires = CallBack.ExpirationTime + now;
2028 tvc->f.states |= CStatd | CUnique;
2029 tvc->f.states &= ~CBulkFetching;
2030 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
2031 } else if (tvc->f.states & CRO) {
2032 /* adapt gives us an hour. */
2033 tvc->cbExpires = 3600 + osi_Time();
2034 /*XXX*/ tvc->f.states |= CStatd | CUnique;
2035 tvc->f.states &= ~CBulkFetching;
2036 afs_QueueCallback(tvc, CBHash(3600), tvp);
2038 tvc->callback = NULL;
2039 afs_DequeueCallback(tvc);
2040 tvc->f.states &= ~(CStatd | CUnique);
2041 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2042 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2045 afs_DequeueCallback(tvc);
2046 tvc->f.states &= ~CStatd;
2047 tvc->f.states &= ~CUnique;
2048 tvc->callback = NULL;
2049 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2050 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2052 ReleaseWriteLock(&afs_xcbhash);
2054 afs_PutVolume(tvp, READ_LOCK);
2055 afs_ProcessFS(tvc, &OutStatus, areq);
2057 ReleaseWriteLock(&tvc->lock);
2063 afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
2064 afs_int32 * cached, struct volume *tvolp)
2066 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
2067 afs_int32 getNewFid = 0;
2069 struct VenusFid nfid;
2071 struct server *serverp = 0;
2072 struct AFSFetchStatus OutStatus;
2073 struct AFSCallBack CallBack;
2074 struct AFSVolSync tsync;
2076 #ifdef AFS_DARWIN80_ENV
2083 if (!tvolp->rootVnode || getNewFid) {
2084 struct VenusFid tfid;
2087 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2088 origCBs = afs_allCBs; /* ignore InitCallBackState */
2090 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2095 /* ReleaseReadLock(&tvolp->lock); */
2096 ObtainWriteLock(&tvolp->lock, 56);
2097 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
2098 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
2099 ReleaseWriteLock(&tvolp->lock);
2100 /* ObtainReadLock(&tvolp->lock);*/
2103 afid->Fid.Vnode = tvolp->rootVnode;
2104 afid->Fid.Unique = tvolp->rootUnique;
2108 ObtainSharedLock(&afs_xvcache, 7);
2110 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2111 if (!FidCmp(&(tvc->f.fid), afid)) {
2112 if (tvc->f.states & CVInit) {
2113 ReleaseSharedLock(&afs_xvcache);
2114 afs_osi_Sleep(&tvc->f.states);
2117 #ifdef AFS_DARWIN80_ENV
2118 if (tvc->f.states & CDeadVnode) {
2119 ReleaseSharedLock(&afs_xvcache);
2120 afs_osi_Sleep(&tvc->f.states);
2124 if (vnode_get(tvp)) /* this bumps ref count */
2126 if (vnode_ref(tvp)) {
2128 /* AFSTOV(tvc) may be NULL */
2138 if (!haveStatus && (!tvc || !(tvc->f.states & CStatd))) {
2139 /* Mount point no longer stat'd or unknown. FID may have changed. */
2141 ReleaseSharedLock(&afs_xvcache);
2142 #ifdef AFS_DARWIN80_ENV
2145 vnode_put(AFSTOV(tvc));
2146 vnode_rele(AFSTOV(tvc));
2155 UpgradeSToWLock(&afs_xvcache, 23);
2156 /* no cache entry, better grab one */
2157 tvc = afs_NewVCache(afid, NULL);
2160 ReleaseWriteLock(&afs_xvcache);
2164 afs_stats_cmperf.vcacheMisses++;
2168 afs_stats_cmperf.vcacheHits++;
2169 #if defined(AFS_DARWIN80_ENV)
2170 /* we already bumped the ref count in the for loop above */
2171 #else /* AFS_DARWIN80_ENV */
2174 UpgradeSToWLock(&afs_xvcache, 24);
2175 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2176 refpanic("GRVC VLRU inconsistent0");
2178 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2179 refpanic("GRVC VLRU inconsistent1");
2181 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2182 refpanic("GRVC VLRU inconsistent2");
2184 QRemove(&tvc->vlruq); /* move to lruq head */
2185 QAdd(&VLRU, &tvc->vlruq);
2186 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2187 refpanic("GRVC VLRU inconsistent3");
2189 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2190 refpanic("GRVC VLRU inconsistent4");
2192 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2193 refpanic("GRVC VLRU inconsistent5");
2198 ReleaseWriteLock(&afs_xvcache);
2200 if (tvc->f.states & CStatd) {
2204 ObtainReadLock(&tvc->lock);
2205 tvc->f.states &= ~CUnique;
2206 tvc->callback = NULL; /* redundant, perhaps */
2207 ReleaseReadLock(&tvc->lock);
2210 ObtainWriteLock(&tvc->lock, 57);
2212 /* It is always appropriate to throw away all the access rights? */
2213 afs_FreeAllAxs(&(tvc->Access));
2216 tvc->f.states |= CForeign;
2217 if (tvolp->states & VRO)
2218 tvc->f.states |= CRO;
2219 if (tvolp->states & VBackup)
2220 tvc->f.states |= CBackup;
2221 /* now copy ".." entry back out of volume structure, if necessary */
2222 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2223 && (tvolp->rootUnique == afid->Fid.Unique)) {
2226 if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2228 tvc->mvid = (struct VenusFid *)
2229 osi_AllocSmallSpace(sizeof(struct VenusFid));
2230 *tvc->mvid = tvolp->dotdot;
2234 afs_RemoveVCB(afid);
2237 struct VenusFid tfid;
2240 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2241 origCBs = afs_allCBs; /* ignore InitCallBackState */
2243 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2248 ObtainWriteLock(&afs_xcbhash, 467);
2249 afs_DequeueCallback(tvc);
2250 tvc->callback = NULL;
2251 tvc->f.states &= ~(CStatd | CUnique);
2252 ReleaseWriteLock(&afs_xcbhash);
2253 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2254 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2255 ReleaseWriteLock(&tvc->lock);
2260 ObtainWriteLock(&afs_xcbhash, 468);
2261 if (origCBs == afs_allCBs) {
2262 tvc->f.states |= CTruth;
2263 tvc->callback = serverp;
2264 if (CallBack.ExpirationTime != 0) {
2265 tvc->cbExpires = CallBack.ExpirationTime + start;
2266 tvc->f.states |= CStatd;
2267 tvc->f.states &= ~CBulkFetching;
2268 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2269 } else if (tvc->f.states & CRO) {
2270 /* adapt gives us an hour. */
2271 tvc->cbExpires = 3600 + osi_Time();
2272 /*XXX*/ tvc->f.states |= CStatd;
2273 tvc->f.states &= ~CBulkFetching;
2274 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2277 afs_DequeueCallback(tvc);
2278 tvc->callback = NULL;
2279 tvc->f.states &= ~(CStatd | CUnique);
2280 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2281 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2283 ReleaseWriteLock(&afs_xcbhash);
2284 afs_ProcessFS(tvc, &OutStatus, areq);
2286 ReleaseWriteLock(&tvc->lock);
2292 * Update callback status and (sometimes) attributes of a vnode.
2293 * Called after doing a fetch status RPC. Whilst disconnected, attributes
2294 * shouldn't be written to the vcache here.
2299 * \param Outsp Server status after rpc call.
2300 * \param acb Callback for this vnode.
2302 * \note The vcache must be write locked.
2305 afs_UpdateStatus(struct vcache *avc, struct VenusFid *afid,
2306 struct vrequest *areq, struct AFSFetchStatus *Outsp,
2307 struct AFSCallBack *acb, afs_uint32 start)
2309 struct volume *volp;
2312 /* Dont write status in vcache if resyncing after a disconnection. */
2313 afs_ProcessFS(avc, Outsp, areq);
2315 volp = afs_GetVolume(afid, areq, READ_LOCK);
2316 ObtainWriteLock(&afs_xcbhash, 469);
2317 avc->f.states |= CTruth;
2318 if (avc->callback /* check for race */ ) {
2319 if (acb->ExpirationTime != 0) {
2320 avc->cbExpires = acb->ExpirationTime + start;
2321 avc->f.states |= CStatd;
2322 avc->f.states &= ~CBulkFetching;
2323 afs_QueueCallback(avc, CBHash(acb->ExpirationTime), volp);
2324 } else if (avc->f.states & CRO) {
2325 /* ordinary callback on a read-only volume -- AFS 3.2 style */
2326 avc->cbExpires = 3600 + start;
2327 avc->f.states |= CStatd;
2328 avc->f.states &= ~CBulkFetching;
2329 afs_QueueCallback(avc, CBHash(3600), volp);
2331 afs_DequeueCallback(avc);
2332 avc->callback = NULL;
2333 avc->f.states &= ~(CStatd | CUnique);
2334 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
2335 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2338 afs_DequeueCallback(avc);
2339 avc->callback = NULL;
2340 avc->f.states &= ~(CStatd | CUnique);
2341 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
2342 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2344 ReleaseWriteLock(&afs_xcbhash);
2346 afs_PutVolume(volp, READ_LOCK);
2350 * Check if a given AFSFetchStatus structure is sane.
2352 * @param[in] tc The server from which we received the status
2353 * @param[in] status The status we received
2355 * @return whether the given structure is valid or not
2356 * @retval 0 the structure is fine
2357 * @retval nonzero the structure looks like garbage; act as if we received
2358 * the returned error code from the server
2361 afs_CheckFetchStatus(struct afs_conn *tc, struct AFSFetchStatus *status)
2363 if (status->errorCode ||
2364 status->InterfaceVersion != 1 ||
2365 !(status->FileType > Invalid && status->FileType <= SymbolicLink) ||
2366 status->ParentVnode == 0 || status->ParentUnique == 0) {
2374 * Must be called with avc write-locked
2375 * don't absolutely have to invalidate the hint unless the dv has
2376 * changed, but be sure to get it right else there will be consistency bugs.
2379 afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
2380 struct vrequest * areq, struct AFSFetchStatus * Outsp)
2383 afs_uint32 start = 0;
2384 struct afs_conn *tc;
2385 struct AFSCallBack CallBack;
2386 struct AFSVolSync tsync;
2387 struct rx_connection *rxconn;
2390 tc = afs_Conn(afid, areq, SHARED_LOCK, &rxconn);
2391 avc->dchint = NULL; /* invalidate hints */
2393 avc->callback = tc->parent->srvr->server;
2395 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2398 RXAFS_FetchStatus(rxconn, (struct AFSFid *)&afid->Fid, Outsp,
2405 code = afs_CheckFetchStatus(tc, Outsp);
2410 } while (afs_Analyze
2411 (tc, rxconn, code, afid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2412 SHARED_LOCK, NULL));
2415 afs_UpdateStatus(avc, afid, areq, Outsp, &CallBack, start);
2417 /* used to undo the local callback, but that's too extreme.
2418 * There are plenty of good reasons that fetchstatus might return
2419 * an error, such as EPERM. If we have the vnode cached, statd,
2420 * with callback, might as well keep track of the fact that we
2421 * don't have access...
2423 if (code == EPERM || code == EACCES) {
2424 struct axscache *ac;
2425 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2427 else /* not found, add a new one if possible */
2428 afs_AddAxs(avc->Access, areq->uid, 0);
2439 * Stuff some information into the vcache for the given file.
2442 * afid : File in question.
2443 * OutStatus : Fetch status on the file.
2444 * CallBack : Callback info.
2445 * tc : RPC connection involved.
2446 * areq : vrequest involved.
2449 * Nothing interesting.
2452 afs_StuffVcache(struct VenusFid *afid,
2453 struct AFSFetchStatus *OutStatus,
2454 struct AFSCallBack *CallBack, struct afs_conn *tc,
2455 struct vrequest *areq)
2457 afs_int32 code, i, newvcache = 0;
2459 struct AFSVolSync tsync;
2461 struct axscache *ac;
2464 AFS_STATCNT(afs_StuffVcache);
2465 #ifdef IFS_VCACHECOUNT
2470 ObtainSharedLock(&afs_xvcache, 8);
2472 tvc = afs_FindVCache(afid, &retry, DO_VLRU| IS_SLOCK /* no stats */ );
2474 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2475 ReleaseSharedLock(&afs_xvcache);
2476 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2482 /* no cache entry, better grab one */
2483 UpgradeSToWLock(&afs_xvcache, 25);
2484 tvc = afs_NewVCache(afid, NULL);
2486 ConvertWToSLock(&afs_xvcache);
2489 ReleaseSharedLock(&afs_xvcache);
2494 ReleaseSharedLock(&afs_xvcache);
2495 ObtainWriteLock(&tvc->lock, 58);
2497 tvc->f.states &= ~CStatd;
2498 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2499 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2501 /* Is it always appropriate to throw away all the access rights? */
2502 afs_FreeAllAxs(&(tvc->Access));
2504 /*Copy useful per-volume info */
2505 tvp = afs_GetVolume(afid, areq, READ_LOCK);
2507 if (newvcache && (tvp->states & VForeign))
2508 tvc->f.states |= CForeign;
2509 if (tvp->states & VRO)
2510 tvc->f.states |= CRO;
2511 if (tvp->states & VBackup)
2512 tvc->f.states |= CBackup;
2514 * Now, copy ".." entry back out of volume structure, if
2517 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2519 tvc->mvid = (struct VenusFid *)
2520 osi_AllocSmallSpace(sizeof(struct VenusFid));
2521 *tvc->mvid = tvp->dotdot;
2524 /* store the stat on the file */
2525 afs_RemoveVCB(afid);
2526 afs_ProcessFS(tvc, OutStatus, areq);
2527 tvc->callback = tc->srvr->server;
2529 /* we use osi_Time twice below. Ideally, we would use the time at which
2530 * the FetchStatus call began, instead, but we don't have it here. So we
2531 * make do with "now". In the CRO case, it doesn't really matter. In
2532 * the other case, we hope that the difference between "now" and when the
2533 * call actually began execution on the server won't be larger than the
2534 * padding which the server keeps. Subtract 1 second anyway, to be on
2535 * the safe side. Can't subtract more because we don't know how big
2536 * ExpirationTime is. Possible consistency problems may arise if the call
2537 * timeout period becomes longer than the server's expiration padding. */
2538 ObtainWriteLock(&afs_xcbhash, 470);
2539 if (CallBack->ExpirationTime != 0) {
2540 tvc->cbExpires = CallBack->ExpirationTime + osi_Time() - 1;
2541 tvc->f.states |= CStatd;
2542 tvc->f.states &= ~CBulkFetching;
2543 afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2544 } else if (tvc->f.states & CRO) {
2545 /* old-fashioned AFS 3.2 style */
2546 tvc->cbExpires = 3600 + osi_Time();
2547 /*XXX*/ tvc->f.states |= CStatd;
2548 tvc->f.states &= ~CBulkFetching;
2549 afs_QueueCallback(tvc, CBHash(3600), tvp);
2551 afs_DequeueCallback(tvc);
2552 tvc->callback = NULL;
2553 tvc->f.states &= ~(CStatd | CUnique);
2554 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2555 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2557 ReleaseWriteLock(&afs_xcbhash);
2559 afs_PutVolume(tvp, READ_LOCK);
2561 /* look in per-pag cache */
2562 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2563 ac->axess = OutStatus->CallerAccess; /* substitute pags */
2564 else /* not found, add a new one if possible */
2565 afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2567 ReleaseWriteLock(&tvc->lock);
2568 afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2569 ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32,
2570 tvc->cbExpires, ICL_TYPE_INT32, tvc->cbExpires - osi_Time());
2572 * Release ref count... hope this guy stays around...
2575 } /*afs_StuffVcache */
2579 * Decrements the reference count on a cache entry.
2581 * \param avc Pointer to the cache entry to decrement.
2583 * \note Environment: Nothing interesting.
2586 afs_PutVCache(struct vcache *avc)
2588 AFS_STATCNT(afs_PutVCache);
2589 #ifdef AFS_DARWIN80_ENV
2590 vnode_put(AFSTOV(avc));
2594 * Can we use a read lock here?
2596 ObtainReadLock(&afs_xvcache);
2598 ReleaseReadLock(&afs_xvcache);
2600 } /*afs_PutVCache */
2604 * Reset a vcache entry, so local contents are ignored, and the
2605 * server will be reconsulted next time the vcache is used
2607 * \param avc Pointer to the cache entry to reset
2610 * \note avc must be write locked on entry
2613 afs_ResetVCache(struct vcache *avc, afs_ucred_t *acred)
2615 ObtainWriteLock(&afs_xcbhash, 456);
2616 afs_DequeueCallback(avc);
2617 avc->f.states &= ~(CStatd | CDirty); /* next reference will re-stat */
2618 ReleaseWriteLock(&afs_xcbhash);
2619 /* now find the disk cache entries */
2620 afs_TryToSmush(avc, acred, 1);
2621 osi_dnlc_purgedp(avc);
2622 if (avc->linkData && !(avc->f.states & CCore)) {
2623 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
2624 avc->linkData = NULL;
2629 * Sleepa when searching for a vcache. Releases all the pending locks,
2630 * sleeps then obtains the previously released locks.
2632 * \param vcache Enter sleep state.
2633 * \param flag Determines what locks to use.
2638 findvc_sleep(struct vcache *avc, int flag)
2640 if (flag & IS_SLOCK) {
2641 ReleaseSharedLock(&afs_xvcache);
2643 if (flag & IS_WLOCK) {
2644 ReleaseWriteLock(&afs_xvcache);
2646 ReleaseReadLock(&afs_xvcache);
2649 afs_osi_Sleep(&avc->f.states);
2650 if (flag & IS_SLOCK) {
2651 ObtainSharedLock(&afs_xvcache, 341);
2653 if (flag & IS_WLOCK) {
2654 ObtainWriteLock(&afs_xvcache, 343);
2656 ObtainReadLock(&afs_xvcache);
2662 * Add a reference on an existing vcache entry.
2664 * \param tvc Pointer to the vcache.
2666 * \note Environment: Must be called with at least one reference from
2667 * elsewhere on the vcache, even if that reference will be dropped.
2668 * The global lock is required.
2670 * \return 0 on success, -1 on failure.
2674 afs_RefVCache(struct vcache *tvc)
2676 #ifdef AFS_DARWIN80_ENV
2680 /* AFS_STATCNT(afs_RefVCache); */
2682 #ifdef AFS_DARWIN80_ENV
2686 if (vnode_ref(tvp)) {
2688 /* AFSTOV(tvc) may be NULL */
2697 } /*afs_RefVCache */
2700 * Find a vcache entry given a fid.
2702 * \param afid Pointer to the fid whose cache entry we desire.
2703 * \param retry (SGI-specific) tell the caller to drop the lock on xvcache,
2704 * unlock the vnode, and try again.
2705 * \param flag Bit 1 to specify whether to compute hit statistics. Not
2706 * set if FindVCache is called as part of internal bookkeeping.
2708 * \note Environment: Must be called with the afs_xvcache lock at least held at
2709 * the read level. In order to do the VLRU adjustment, the xvcache lock
2710 * must be shared-- we upgrade it here.
2714 afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
2719 #ifdef AFS_DARWIN80_ENV
2720 struct vcache *deadvc = NULL, *livevc = NULL;
2724 AFS_STATCNT(afs_FindVCache);
2728 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2729 if (FidMatches(afid, tvc)) {
2730 if (tvc->f.states & CVInit) {
2731 findvc_sleep(tvc, flag);
2734 #ifdef AFS_DARWIN80_ENV
2735 if (tvc->f.states & CDeadVnode) {
2736 findvc_sleep(tvc, flag);
2744 /* should I have a read lock on the vnode here? */
2748 #if defined(AFS_DARWIN80_ENV)
2752 if (tvp && vnode_ref(tvp)) {
2754 /* AFSTOV(tvc) may be NULL */
2763 #elif defined(AFS_DARWIN_ENV)
2764 tvc->f.states |= CUBCinit;
2766 if (UBCINFOMISSING(AFSTOV(tvc)) ||
2767 UBCINFORECLAIMED(AFSTOV(tvc))) {
2768 ubc_info_init(AFSTOV(tvc));
2771 tvc->f.states &= ~CUBCinit;
2773 osi_vnhold(tvc, retry); /* already held, above */
2774 if (retry && *retry)
2778 * only move to front of vlru if we have proper vcache locking)
2780 if (flag & DO_VLRU) {
2781 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2782 refpanic("FindVC VLRU inconsistent1");
2784 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2785 refpanic("FindVC VLRU inconsistent1");
2787 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2788 refpanic("FindVC VLRU inconsistent2");
2790 UpgradeSToWLock(&afs_xvcache, 26);
2791 QRemove(&tvc->vlruq);
2792 QAdd(&VLRU, &tvc->vlruq);
2793 ConvertWToSLock(&afs_xvcache);
2794 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2795 refpanic("FindVC VLRU inconsistent1");
2797 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2798 refpanic("FindVC VLRU inconsistent2");
2800 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2801 refpanic("FindVC VLRU inconsistent3");
2807 if (flag & DO_STATS) {
2809 afs_stats_cmperf.vcacheHits++;
2811 afs_stats_cmperf.vcacheMisses++;
2812 if (afs_IsPrimaryCellNum(afid->Cell))
2813 afs_stats_cmperf.vlocalAccesses++;
2815 afs_stats_cmperf.vremoteAccesses++;
2818 } /*afs_FindVCache */
2821 * Find a vcache entry given a fid. Does a wildcard match on what we
2822 * have for the fid. If more than one entry, don't return anything.
2824 * \param avcp Fill in pointer if we found one and only one.
2825 * \param afid Pointer to the fid whose cache entry we desire.
2826 * \param retry (SGI-specific) tell the caller to drop the lock on xvcache,
2827 * unlock the vnode, and try again.
2828 * \param flags bit 1 to specify whether to compute hit statistics. Not
2829 * set if FindVCache is called as part of internal bookkeeping.
2831 * \note Environment: Must be called with the afs_xvcache lock at least held at
2832 * the read level. In order to do the VLRU adjustment, the xvcache lock
2833 * must be shared-- we upgrade it here.
2835 * \return Number of matches found.
2838 int afs_duplicate_nfs_fids = 0;
2841 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2845 afs_int32 count = 0;
2846 struct vcache *found_tvc = NULL;
2847 #ifdef AFS_DARWIN80_ENV
2851 AFS_STATCNT(afs_FindVCache);
2855 ObtainSharedLock(&afs_xvcache, 331);
2858 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2859 /* Match only on what we have.... */
2860 if (((tvc->f.fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2861 && (tvc->f.fid.Fid.Volume == afid->Fid.Volume)
2862 && ((tvc->f.fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2863 && (tvc->f.fid.Cell == afid->Cell)) {
2864 if (tvc->f.states & CVInit) {
2865 ReleaseSharedLock(&afs_xvcache);
2866 afs_osi_Sleep(&tvc->f.states);
2869 #ifdef AFS_DARWIN80_ENV
2870 if (tvc->f.states & CDeadVnode) {
2871 ReleaseSharedLock(&afs_xvcache);
2872 afs_osi_Sleep(&tvc->f.states);
2876 if (vnode_get(tvp)) {
2877 /* This vnode no longer exists. */
2880 if (vnode_ref(tvp)) {
2881 /* This vnode no longer exists. */
2883 /* AFSTOV(tvc) may be NULL */
2888 #endif /* AFS_DARWIN80_ENV */
2892 afs_duplicate_nfs_fids++;
2893 ReleaseSharedLock(&afs_xvcache);
2894 #ifdef AFS_DARWIN80_ENV
2895 /* Drop our reference counts. */
2896 vnode_put(AFSTOV(tvc));
2897 vnode_put(AFSTOV(found_tvc));
2906 /* should I have a read lock on the vnode here? */
2908 #ifndef AFS_DARWIN80_ENV
2909 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2910 afs_int32 retry = 0;
2911 osi_vnhold(tvc, &retry);
2914 found_tvc = (struct vcache *)0;
2915 ReleaseSharedLock(&afs_xvcache);
2916 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2920 osi_vnhold(tvc, (int *)0); /* already held, above */
2924 * We obtained the xvcache lock above.
2926 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2927 refpanic("FindVC VLRU inconsistent1");
2929 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2930 refpanic("FindVC VLRU inconsistent1");
2932 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2933 refpanic("FindVC VLRU inconsistent2");
2935 UpgradeSToWLock(&afs_xvcache, 568);
2936 QRemove(&tvc->vlruq);
2937 QAdd(&VLRU, &tvc->vlruq);
2938 ConvertWToSLock(&afs_xvcache);
2939 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2940 refpanic("FindVC VLRU inconsistent1");
2942 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2943 refpanic("FindVC VLRU inconsistent2");
2945 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2946 refpanic("FindVC VLRU inconsistent3");
2952 afs_stats_cmperf.vcacheHits++;
2954 afs_stats_cmperf.vcacheMisses++;
2955 if (afs_IsPrimaryCellNum(afid->Cell))
2956 afs_stats_cmperf.vlocalAccesses++;
2958 afs_stats_cmperf.vremoteAccesses++;
2960 *avcp = tvc; /* May be null */
2962 ReleaseSharedLock(&afs_xvcache);
2963 return (tvc ? 1 : 0);
2965 } /*afs_NFSFindVCache */
2971 * Initialize vcache related variables
2976 afs_vcacheInit(int astatSize)
2978 #if !defined(AFS_LINUX22_ENV)
2982 if (!afs_maxvcount) {
2983 afs_maxvcount = astatSize; /* no particular limit on linux? */
2985 #if !defined(AFS_LINUX22_ENV)
2989 AFS_RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2990 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2992 #if !defined(AFS_LINUX22_ENV)
2993 /* Allocate and thread the struct vcache entries */
2994 tvp = afs_osi_Alloc(astatSize * sizeof(struct vcache));
2995 osi_Assert(tvp != NULL);
2996 memset(tvp, 0, sizeof(struct vcache) * astatSize);
2998 Initial_freeVCList = tvp;
2999 freeVCList = &(tvp[0]);
3000 for (i = 0; i < astatSize - 1; i++) {
3001 tvp[i].nextfree = &(tvp[i + 1]);
3003 tvp[astatSize - 1].nextfree = NULL;
3004 # ifdef KERNEL_HAVE_PIN
3005 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
3009 #if defined(AFS_SGI_ENV)
3010 for (i = 0; i < astatSize; i++) {
3011 char name[METER_NAMSZ];
3012 struct vcache *tvc = &tvp[i];
3014 tvc->v.v_number = ++afsvnumbers;
3015 tvc->vc_rwlockid = OSI_NO_LOCKID;
3016 initnsema(&tvc->vc_rwlock, 1,
3017 makesname(name, "vrw", tvc->v.v_number));
3018 #ifndef AFS_SGI53_ENV
3019 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
3021 #ifndef AFS_SGI62_ENV
3022 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
3023 #endif /* AFS_SGI62_ENV */
3027 for(i = 0; i < VCSIZE; ++i)
3028 QInit(&afs_vhashTV[i]);
3035 shutdown_vcache(void)
3038 struct afs_cbr *tsp;
3040 * XXX We may potentially miss some of the vcaches because if when
3041 * there are no free vcache entries and all the vcache entries are active
3042 * ones then we allocate an additional one - admittedly we almost never
3047 struct afs_q *tq, *uq = NULL;
3049 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
3053 osi_FreeSmallSpace(tvc->mvid);
3054 tvc->mvid = (struct VenusFid *)0;
3057 aix_gnode_rele(AFSTOV(tvc));
3059 if (tvc->linkData) {
3060 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3065 * Also free the remaining ones in the Cache
3067 for (i = 0; i < VCSIZE; i++) {
3068 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3070 osi_FreeSmallSpace(tvc->mvid);
3071 tvc->mvid = (struct VenusFid *)0;
3075 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
3076 #ifdef AFS_AIX32_ENV
3079 vms_delete(tvc->segid);
3081 tvc->segid = tvc->vmh = NULL;
3082 if (VREFCOUNT_GT(tvc,0))
3083 osi_Panic("flushVcache: vm race");
3091 #if defined(AFS_SUN5_ENV)
3097 if (tvc->linkData) {
3098 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3103 afs_FreeAllAxs(&(tvc->Access));
3109 * Free any leftover callback queue
3111 for (i = 0; i < afs_stats_cmperf.CallBackAlloced; i++) {
3112 tsp = afs_cbrHeads[i];
3113 afs_cbrHeads[i] = 0;
3114 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
3118 #if !defined(AFS_LINUX22_ENV)
3119 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3121 # ifdef KERNEL_HAVE_PIN
3122 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3125 freeVCList = Initial_freeVCList = 0;
3128 AFS_RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
3129 LOCK_INIT(&afs_xvcb, "afs_xvcb");
3131 for(i = 0; i < VCSIZE; ++i)
3132 QInit(&afs_vhashTV[i]);
3136 afs_DisconGiveUpCallbacks(void)
3142 ObtainWriteLock(&afs_xvcache, 1002); /* XXX - should be a unique number */
3145 /* Somehow, walk the set of vcaches, with each one coming out as tvc */
3146 for (i = 0; i < VCSIZE; i++) {
3147 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3149 if (afs_QueueVCB(tvc, &slept)) {
3150 tvc->callback = NULL;
3159 ReleaseWriteLock(&afs_xvcache);
3166 * Clear the Statd flag from all vcaches
3168 * This function removes the Statd flag from all vcaches. It's used by
3169 * disconnected mode to tidy up during reconnection
3173 afs_ClearAllStatdFlag(void)
3178 ObtainWriteLock(&afs_xvcache, 715);
3180 for (i = 0; i < VCSIZE; i++) {
3181 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3182 tvc->f.states &= ~(CStatd|CUnique);
3185 ReleaseWriteLock(&afs_xvcache);