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
39 #include <afsconfig.h>
40 #include "afs/param.h"
42 #include "afs/sysincludes.h" /*Standard vendor system headers */
43 #include "afsincludes.h" /*AFS-based standard headers */
44 #include "afs/afs_stats.h"
45 #include "afs/afs_cbqueue.h"
46 #include "afs/afs_osidnlc.h"
48 afs_int32 afs_maxvcount = 0; /* max number of vcache entries */
49 afs_int32 afs_vcount = 0; /* number of vcache in use now */
57 #endif /* AFS_SGI64_ENV */
59 /* Exported variables */
60 afs_rwlock_t afs_xvcdirty; /*Lock: discon vcache dirty list mgmt */
61 afs_rwlock_t afs_xvcache; /*Lock: alloc new stat cache entries */
62 afs_rwlock_t afs_xvreclaim; /*Lock: entries reclaimed, not on free list */
63 afs_lock_t afs_xvcb; /*Lock: fids on which there are callbacks */
64 #if !defined(AFS_LINUX_ENV)
65 static struct vcache *freeVCList; /*Free list for stat cache entries */
66 struct vcache *ReclaimedVCList; /*Reclaimed list for stat entries */
67 static struct vcache *Initial_freeVCList; /*Initial list for above */
69 struct afs_q VLRU; /*vcache LRU */
70 afs_int32 vcachegen = 0;
71 unsigned int afs_paniconwarn = 0;
72 struct vcache *afs_vhashT[VCSIZE];
73 struct afs_q afs_vhashTV[VCSIZE];
74 static struct afs_cbr *afs_cbrHashT[CBRSIZE];
75 afs_int32 afs_bulkStatsLost;
76 int afs_norefpanic = 0;
79 /* Disk backed vcache definitions
80 * Both protected by xvcache */
81 static int afs_nextVcacheSlot = 0;
82 static struct afs_slotlist *afs_freeSlotList = NULL;
84 /* Forward declarations */
85 static afs_int32 afs_QueueVCB(struct vcache *avc, int *slept);
89 * The PFlush algorithm makes use of the fact that Fid.Unique is not used in
90 * below hash algorithms. Change it if need be so that flushing algorithm
91 * doesn't move things from one hash chain to another.
93 /* Don't hash on the cell; our callback-breaking code sometimes fails to compute
94 * the cell correctly, and only scans one hash bucket. */
95 int VCHash(struct VenusFid *fid)
97 return opr_jhash_int2(fid->Fid.Volume, fid->Fid.Vnode, 0) &
98 opr_jhash_mask(VCSIZEBITS);
100 /* Hash only on volume to speed up volume callbacks. */
101 int VCHashV(struct VenusFid *fid)
103 return opr_jhash_int(fid->Fid.Volume, 0) & opr_jhash_mask(VCSIZEBITS);
107 * Generate an index into the hash table for a given Fid.
109 * \return The hash value.
112 afs_HashCBRFid(struct AFSFid *fid)
114 return (fid->Volume + fid->Vnode + fid->Unique) % CBRSIZE;
118 * Insert a CBR entry into the hash table.
119 * Must be called with afs_xvcb held.
124 afs_InsertHashCBR(struct afs_cbr *cbr)
126 int slot = afs_HashCBRFid(&cbr->fid);
128 cbr->hash_next = afs_cbrHashT[slot];
129 if (afs_cbrHashT[slot])
130 afs_cbrHashT[slot]->hash_pprev = &cbr->hash_next;
132 cbr->hash_pprev = &afs_cbrHashT[slot];
133 afs_cbrHashT[slot] = cbr;
138 * Flush the given vcache entry.
141 * afs_xvcache lock must be held for writing upon entry to
142 * prevent people from changing the vrefCount field, and to
143 * protect the lruq and hnext fields.
144 * LOCK: afs_FlushVCache afs_xvcache W
145 * REFCNT: vcache ref count must be zero on entry except for osf1
146 * RACE: lock is dropped and reobtained, permitting race in caller
148 * \param avc Pointer to vcache entry to flush.
149 * \param slept Pointer to int to set 1 if we sleep/drop locks, 0 if we don't.
153 afs_FlushVCache(struct vcache *avc, int *slept)
154 { /*afs_FlushVCache */
157 struct vcache **uvc, *wvc;
159 /* NOTE: We must have nothing drop afs_xvcache until we have removed all
160 * possible references to this vcache. This means all hash tables, queues,
164 AFS_STATCNT(afs_FlushVCache);
165 afs_Trace2(afs_iclSetp, CM_TRACE_FLUSHV, ICL_TYPE_POINTER, avc,
166 ICL_TYPE_INT32, avc->f.states);
168 code = osi_VM_FlushVCache(avc);
172 if (avc->f.states & CVFlushed) {
176 #if !defined(AFS_LINUX_ENV)
177 if (avc->nextfree || !avc->vlruq.prev || !avc->vlruq.next) { /* qv afs.h */
178 refpanic("LRU vs. Free inconsistency");
181 avc->f.states |= CVFlushed;
182 /* pull the entry out of the lruq and put it on the free list */
183 QRemove(&avc->vlruq);
185 /* keep track of # of files that we bulk stat'd, but never used
186 * before they got recycled.
188 if (avc->f.states & CBulkStat)
191 /* remove entry from the hash chain */
192 i = VCHash(&avc->f.fid);
193 uvc = &afs_vhashT[i];
194 for (wvc = *uvc; wvc; uvc = &wvc->hnext, wvc = *uvc) {
202 /* remove entry from the volume hash table */
203 QRemove(&avc->vhashq);
205 #if defined(AFS_LINUX_ENV)
207 struct pagewriter *pw, *store;
208 struct list_head tofree;
210 INIT_LIST_HEAD(&tofree);
211 spin_lock(&avc->pagewriter_lock);
212 list_for_each_entry_safe(pw, store, &avc->pagewriters, link) {
214 /* afs_osi_Free may sleep so we need to defer it */
215 list_add_tail(&pw->link, &tofree);
217 spin_unlock(&avc->pagewriter_lock);
218 list_for_each_entry_safe(pw, store, &tofree, link) {
220 afs_osi_Free(pw, sizeof(struct pagewriter));
225 if (avc->mvid.target_root)
226 osi_FreeSmallSpace(avc->mvid.target_root);
227 avc->mvid.target_root = NULL;
229 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
230 avc->linkData = NULL;
232 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
233 /* OK, there are no internal vrefCounts, so there shouldn't
234 * be any more refs here. */
236 # ifdef AFS_DARWIN80_ENV
237 vnode_clearfsnode(AFSTOV(avc));
238 vnode_removefsref(AFSTOV(avc));
240 avc->v->v_data = NULL; /* remove from vnode */
242 AFSTOV(avc) = NULL; /* also drop the ptr to vnode */
246 #ifdef AFS_SUN511_ENV
251 #elif defined(AFS_SUN510_ENV)
252 /* As we use private vnodes, cleanup is up to us */
253 vn_reinit(AFSTOV(avc));
255 afs_FreeAllAxs(&(avc->Access));
256 afs_StaleVCacheFlags(avc, AFS_STALEVC_FILENAME, CUnique);
258 /* By this point, the vcache has been removed from all global structures
259 * via which someone could try to use the vcache. It is okay to drop
260 * afs_xvcache at this point (if *slept is set). */
262 if (afs_shuttingdown == AFS_RUNNING)
263 afs_QueueVCB(avc, slept);
266 * Next, keep track of which vnodes we've deleted for create's
267 * optimistic synchronization algorithm
270 if (avc->f.fid.Fid.Vnode & 1)
276 #if !defined(AFS_LINUX_ENV)
277 /* put the entry in the free list */
278 avc->nextfree = freeVCList;
280 if (avc->vlruq.prev || avc->vlruq.next) {
281 refpanic("LRU vs. Free inconsistency");
283 avc->f.states |= CVFlushed;
285 /* This should put it back on the vnode free list since usecount is 1 */
287 if (VREFCOUNT_GT(avc,0)) {
288 AFS_RELE(AFSTOV(avc));
289 afs_stats_cmperf.vcacheXAllocs--;
291 if (afs_norefpanic) {
292 afs_warn("flush vc refcnt < 1");
295 osi_Panic("flush vc refcnt < 1");
297 #endif /* AFS_LINUX_ENV */
302 } /*afs_FlushVCache */
306 * The core of the inactive vnode op for all but IRIX.
312 afs_InactiveVCache(struct vcache *avc, afs_ucred_t *acred)
314 AFS_STATCNT(afs_inactive);
316 ObtainWriteLock(&avc->lock, 68);
318 if (avc->f.states & CDirty) {
319 /* we can't keep trying to push back dirty data forever. Give up. */
320 afs_InvalidateAllSegments(avc); /* turns off dirty bit */
322 avc->f.states &= ~CMAPPED;
323 avc->f.states &= ~CDirty; /* Turn it off */
324 if (avc->f.states & CUnlinked) {
325 if (CheckLock(&afs_xvcache) || CheckLock(&afs_xdcache)) {
326 avc->f.states |= CUnlinkedDel;
327 ReleaseWriteLock(&avc->lock);
330 ReleaseWriteLock(&avc->lock);
331 afs_remunlink(avc, 1); /* ignore any return code */
334 ReleaseWriteLock(&avc->lock);
339 * Allocate a callback return structure from the
340 * free list and return it.
342 * Environment: The alloc and free routines are both called with the afs_xvcb lock
343 * held, so we don't have to worry about blocking in osi_Alloc.
345 * \return The allocated afs_cbr.
347 static struct afs_cbr *afs_cbrSpace = 0;
348 /* if alloc limit below changes, fix me! */
349 static struct afs_cbr *afs_cbrHeads[16];
356 while (!afs_cbrSpace) {
357 if (afs_stats_cmperf.CallBackAlloced >= sizeof(afs_cbrHeads)/sizeof(afs_cbrHeads[0])) {
358 /* don't allocate more than 16 * AFS_NCBRS for now */
360 afs_stats_cmperf.CallBackFlushes++;
363 tsp = afs_osi_Alloc(AFS_NCBRS * sizeof(struct afs_cbr));
364 osi_Assert(tsp != NULL);
365 for (i = 0; i < AFS_NCBRS - 1; i++) {
366 tsp[i].next = &tsp[i + 1];
368 tsp[AFS_NCBRS - 1].next = 0;
370 afs_cbrHeads[afs_stats_cmperf.CallBackAlloced] = tsp;
371 afs_stats_cmperf.CallBackAlloced++;
375 afs_cbrSpace = tsp->next;
380 * Free a callback return structure, removing it from all lists.
382 * Environment: the xvcb lock is held over these calls.
384 * \param asp The address of the structure to free.
389 afs_FreeCBR(struct afs_cbr *asp)
391 *(asp->pprev) = asp->next;
393 asp->next->pprev = asp->pprev;
395 *(asp->hash_pprev) = asp->hash_next;
397 asp->hash_next->hash_pprev = asp->hash_pprev;
399 asp->next = afs_cbrSpace;
405 FlushAllVCBs(int nconns, struct rx_connection **rxconns,
406 struct afs_conn **conns)
411 results = afs_osi_Alloc(nconns * sizeof (afs_int32));
412 osi_Assert(results != NULL);
415 multi_Rx(rxconns,nconns)
417 multi_RXAFS_GiveUpAllCallBacks();
418 results[multi_i] = multi_error;
423 * Freeing the CBR will unlink it from the server's CBR list
424 * do it here, not in the loop, because a dynamic CBR will call
425 * into the memory management routines.
427 for ( i = 0 ; i < nconns ; i++ ) {
428 if (results[i] == 0) {
429 /* Unchain all of them */
430 while (conns[i]->parent->srvr->server->cbrs)
431 afs_FreeCBR(conns[i]->parent->srvr->server->cbrs);
434 afs_osi_Free(results, nconns * sizeof(afs_int32));
438 * Flush all queued callbacks to all servers.
440 * Environment: holds xvcb lock over RPC to guard against race conditions
441 * when a new callback is granted for the same file later on.
443 * \return 0 for success.
446 afs_FlushVCBs(afs_int32 lockit)
448 struct AFSFid *tfids;
449 struct AFSCallBack callBacks[1];
450 struct AFSCBFids fidArray;
451 struct AFSCBs cbArray;
453 struct afs_cbr *tcbrp;
457 struct vrequest *treq = NULL;
459 int safety1, safety2, safety3;
462 if (AFS_IS_DISCONNECTED)
465 if ((code = afs_CreateReq(&treq, afs_osi_credp)))
467 treq->flags |= O_NONBLOCK;
468 tfids = afs_osi_Alloc(sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
469 osi_Assert(tfids != NULL);
472 ObtainWriteLock(&afs_xvcb, 273);
475 * First, attempt a multi across everything, all addresses
476 * for all servers we know of.
480 afs_LoopServers(AFS_LS_ALL, NULL, 0, FlushAllVCBs, NULL);
482 ObtainReadLock(&afs_xserver);
483 for (i = 0; i < NSERVERS; i++) {
484 for (safety1 = 0, tsp = afs_servers[i];
485 tsp && safety1 < afs_totalServers + 10;
486 tsp = tsp->next, safety1++) {
488 if (tsp->cbrs == (struct afs_cbr *)0)
491 /* otherwise, grab a block of AFS_MAXCBRSCALL from the list
492 * and make an RPC, over and over again.
494 tcount = 0; /* number found so far */
495 for (safety2 = 0; safety2 < afs_cacheStats; safety2++) {
496 if (tcount >= AFS_MAXCBRSCALL || !tsp->cbrs) {
497 struct rx_connection *rxconn;
498 /* if buffer is full, or we've queued all we're going
499 * to from this server, we should flush out the
502 fidArray.AFSCBFids_len = tcount;
503 fidArray.AFSCBFids_val = (struct AFSFid *)tfids;
504 cbArray.AFSCBs_len = 1;
505 cbArray.AFSCBs_val = callBacks;
506 memset(&callBacks[0], 0, sizeof(callBacks[0]));
507 callBacks[0].CallBackType = CB_EXCLUSIVE;
508 for (safety3 = 0; safety3 < AFS_MAXHOSTS * 2; safety3++) {
509 tc = afs_ConnByHost(tsp, tsp->cell->fsport,
510 tsp->cell->cellNum, treq, 0,
511 SHARED_LOCK, 0, &rxconn);
514 (AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS);
517 RXAFS_GiveUpCallBacks(rxconn, &fidArray,
524 (tc, rxconn, code, 0, treq,
525 AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS, SHARED_LOCK,
530 /* ignore return code, since callbacks may have
531 * been returned anyway, we shouldn't leave them
532 * around to be returned again.
534 * Next, see if we are done with this server, and if so,
535 * break to deal with the next one.
541 /* if to flush full buffer */
542 /* if we make it here, we have an entry at the head of cbrs,
543 * which we should copy to the file ID array and then free.
546 tfids[tcount++] = tcbrp->fid;
548 /* Freeing the CBR will unlink it from the server's CBR list */
550 } /* while loop for this one server */
551 if (safety2 > afs_cacheStats) {
552 afs_warn("possible internal error afs_flushVCBs (%d)\n",
555 } /* for loop for this hash chain */
556 } /* loop through all hash chains */
557 if (safety1 > afs_totalServers + 2) {
559 ("AFS internal error (afs_flushVCBs) (%d > %d), continuing...\n",
560 safety1, afs_totalServers + 2);
562 osi_Panic("afs_flushVCBS safety1");
565 ReleaseReadLock(&afs_xserver);
567 ReleaseWriteLock(&afs_xvcb);
568 afs_osi_Free(tfids, sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
569 afs_DestroyReq(treq);
574 * Queue a callback on the given fid.
577 * Locks the xvcb lock.
578 * Called when the xvcache lock is already held.
579 * RACE: afs_xvcache may be dropped and reacquired
581 * \param avc vcache entry
582 * \param slep Set to 1 if we dropped afs_xvcache
583 * \return 1 if queued, 0 otherwise
587 afs_QueueVCB(struct vcache *avc, int *slept)
591 struct afs_cbr *tcbp;
594 AFS_STATCNT(afs_QueueVCB);
596 ObtainWriteLock(&afs_xvcb, 274);
598 /* we can't really give back callbacks on RO files, since the
599 * server only tracks them on a per-volume basis, and we don't
600 * know whether we still have some other files from the same
602 if (!((avc->f.states & CRO) == 0 && avc->callback)) {
606 /* The callback is really just a struct server ptr. */
607 tsp = (struct server *)(avc->callback);
610 /* If we don't have CBR space, AllocCBR may block or hit the net for
611 * clearing up CBRs. Hitting the net may involve a fileserver
612 * needing to contact us, so we must drop xvcache so we don't block
613 * those requests from going through. */
614 reacquire = *slept = 1;
615 ReleaseWriteLock(&afs_xvcache);
618 /* we now have a pointer to the server, so we just allocate
619 * a queue entry and queue it.
621 tcbp = afs_AllocCBR();
622 tcbp->fid = avc->f.fid.Fid;
624 tcbp->next = tsp->cbrs;
626 tsp->cbrs->pprev = &tcbp->next;
629 tcbp->pprev = &tsp->cbrs;
631 afs_InsertHashCBR(tcbp);
635 /* now release locks and return */
636 ReleaseWriteLock(&afs_xvcb);
639 /* make sure this is after dropping xvcb, for locking order */
640 ObtainWriteLock(&afs_xvcache, 279);
647 * Remove a queued callback for a given Fid.
650 * Locks xvcb and xserver locks.
651 * Typically called with xdcache, xvcache and/or individual vcache
654 * \param afid The fid we want cleansed of queued callbacks.
659 afs_RemoveVCB(struct VenusFid *afid)
662 struct afs_cbr *cbr, *ncbr;
664 AFS_STATCNT(afs_RemoveVCB);
665 ObtainWriteLock(&afs_xvcb, 275);
667 slot = afs_HashCBRFid(&afid->Fid);
668 ncbr = afs_cbrHashT[slot];
672 ncbr = cbr->hash_next;
674 if (afid->Fid.Volume == cbr->fid.Volume &&
675 afid->Fid.Vnode == cbr->fid.Vnode &&
676 afid->Fid.Unique == cbr->fid.Unique) {
681 ReleaseWriteLock(&afs_xvcb);
685 afs_FlushReclaimedVcaches(void)
687 #if !defined(AFS_LINUX_ENV)
690 struct vcache *tmpReclaimedVCList = NULL;
692 ObtainWriteLock(&afs_xvreclaim, 76);
693 while (ReclaimedVCList) {
694 tvc = ReclaimedVCList; /* take from free list */
695 ReclaimedVCList = tvc->nextfree;
696 tvc->nextfree = NULL;
697 code = afs_FlushVCache(tvc, &fv_slept);
699 /* Ok, so, if we got code != 0, uh, wtf do we do? */
700 /* Probably, build a temporary list and then put all back when we
701 get to the end of the list */
702 /* This is actually really crappy, but we need to not leak these.
703 We probably need a way to be smarter about this. */
704 tvc->nextfree = tmpReclaimedVCList;
705 tmpReclaimedVCList = tvc;
706 /* printf("Reclaim list flush %lx failed: %d\n", (unsigned long) tvc, code); */
708 if (tvc->f.states & (CVInit
709 # ifdef AFS_DARWIN80_ENV
713 tvc->f.states &= ~(CVInit
714 # ifdef AFS_DARWIN80_ENV
718 afs_osi_Wakeup(&tvc->f.states);
721 if (tmpReclaimedVCList)
722 ReclaimedVCList = tmpReclaimedVCList;
724 ReleaseWriteLock(&afs_xvreclaim);
729 afs_PostPopulateVCache(struct vcache *avc, struct VenusFid *afid, int seq)
732 * The proper value for mvstat (for root fids) is setup by the caller.
734 avc->mvstat = AFS_MVSTAT_FILE;
735 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
736 avc->mvstat = AFS_MVSTAT_ROOT;
738 if (afs_globalVFS == 0)
739 osi_Panic("afs globalvfs");
741 osi_PostPopulateVCache(avc);
744 osi_dnlc_purgedp(avc); /* this may be overkill */
745 memset(&(avc->callsort), 0, sizeof(struct afs_q));
747 avc->f.states &=~ CVInit;
749 avc->f.states |= CBulkFetching;
750 avc->f.m.Length = seq;
752 afs_osi_Wakeup(&avc->f.states);
756 * afs_VCacheStressed() is intended to determine if the stat cache looks
757 * stressed / full-ish. Due to the different strategies of allocating vcaches
758 * on different platforms, the definition of "stressed" varies, and is somewhat
759 * arbitrary. We just try to make a reasonable guess here.
761 * Returns 1 if the stat cache looks stressed, and 0 otherwise.
765 afs_VCacheStressed(void)
767 if (afsd_dynamic_vcaches) {
769 * For dynamic vcaches, the number of vcaches in use can vary wildly.
770 * Consider us stressed if we're significantly above the configured
771 * threshold. VCACHE_DYNAMIC_STRESSED is the arbitrary point at which
772 * we're considered "significantly" over the threshold.
774 if (afs_vcount > afs_cacheStats + VCACHE_DYNAMIC_STRESSED) {
781 * For non-dynamic vcaches, we should never go above the configured
782 * limit, and ShakeLooseVCaches should try to get us to VCACHE_FREE
783 * under the limit. So if we're closer then VCACHE_FREE/2, then we're
784 * very close to the limit, so consider us stressed.
786 if (afs_vcount > afs_cacheStats || afs_cacheStats - afs_vcount < VCACHE_FREE/2) {
792 #else /* AFS_LINUX_ENV */
794 afs_VCacheStressed(void)
796 /* If we don't have any vcaches in the free list, then consider the stat
798 if (freeVCList != NULL) {
803 #endif /* AFS_LINUX_ENV */
806 afs_ShakeLooseVCaches(afs_int32 anumber)
808 /* Try not to run for more than about 3 seconds */
809 static const int DEADLINE = 3;
814 struct afs_q *tq, *uq;
815 int fv_slept, defersleep = 0;
817 afs_uint32 start = osi_Time();
824 for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
827 if (tvc->f.states & CVFlushed) {
828 refpanic("CVFlushed on VLRU");
829 } else if (i++ > limit) {
830 afs_warn("afs_ShakeLooseVCaches: i %d limit %d afs_vcount %d afs_maxvcount %d\n",
831 (int)i, limit, (int)afs_vcount, (int)afs_maxvcount);
832 refpanic("Found too many AFS vnodes on VLRU (VLRU cycle?)");
833 } else if (QNext(uq) != tq) {
834 refpanic("VLRU inconsistent");
835 } else if (tvc->f.states & CVInit) {
840 evicted = osi_TryEvictVCache(tvc, &fv_slept, defersleep);
847 afs_uint32 now = osi_Time();
852 if (now - start >= DEADLINE) {
853 static afs_uint32 last_warned;
854 /* Warn about this at most every VCACHE_STRESS_LOGINTERVAL secs */
855 if (now < last_warned ||
856 now - last_warned > VCACHE_STRESS_LOGINTERVAL) {
858 afs_warn("afs: Warning: it took us a long time (around "
859 "%d seconds) to try to trim our stat cache "
860 "down to a reasonable size. This may indicate "
861 "someone is accessing an excessive number of "
862 "files, or something is wrong with the AFS "
865 afs_warn("afs: Consider raising the afsd -stat parameter "
866 "(current setting: %d, current vcount: %d), or "
867 "figure out what is accessing so many files.\n",
868 afs_cacheStats, afs_vcount);
875 * This vcache was busy and we slept while trying to evict it.
876 * Move this busy vcache to the head of the VLRU so vcaches
877 * following this busy vcache can be evicted during the retry.
879 QRemove(&tvc->vlruq);
880 QAdd(&VLRU, &tvc->vlruq);
882 goto retry; /* start over - may have raced. */
885 if (anumber && !defersleep) {
893 if (afs_VCacheStressed()) {
895 * If it looks like we have too many vcaches, right after
896 * ShakeLooseVCaches has tried to trim down the number of vcaches, then
897 * maybe -stat should be increased. Log a warning, so if this is
898 * causing problems the user has a chance at noticing.
900 static afs_uint32 last_warned;
901 afs_uint32 now = osi_Time();
903 /* Warn about this at most once every VCACHE_STRESS_LOGINTERVAL secs */
904 if (now - last_warned > VCACHE_STRESS_LOGINTERVAL) {
906 afs_warn("afs: Warning: We are having trouble keeping the AFS stat "
907 "cache trimmed down under the configured limit (current "
908 "-stat setting: %d, current vcache usage: %d).\n",
909 afs_cacheStats, afs_vcount);
910 afs_warn("afs: If AFS access seems slow, consider raising the "
911 "-stat setting for afsd.\n");
918 /* Alloc new vnode. */
920 static struct vcache *
921 afs_AllocVCache(void)
925 tvc = osi_NewVnode();
933 if (afsd_dynamic_vcaches && afs_maxvcount < afs_vcount) {
934 afs_maxvcount = afs_vcount;
935 /*printf("peak vnodes: %d\n", afs_maxvcount);*/
938 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
940 /* If we create a new inode, we either give it a new slot number,
941 * or if one's available, use a slot number from the slot free list
943 if (afs_freeSlotList != NULL) {
944 struct afs_slotlist *tmp;
946 tvc->diskSlot = afs_freeSlotList->slot;
947 tmp = afs_freeSlotList;
948 afs_freeSlotList = tmp->next;
949 afs_osi_Free(tmp, sizeof(struct afs_slotlist));
951 tvc->diskSlot = afs_nextVcacheSlot++;
957 /* Pre populate a newly allocated vcache. On platforms where the actual
958 * vnode is attached to the vcache, this function is called before attachment,
959 * therefore it cannot perform any actions on the vnode itself */
962 afs_PrePopulateVCache(struct vcache *avc, struct VenusFid *afid,
963 struct server *serverp) {
967 slot = avc->diskSlot;
969 osi_PrePopulateVCache(avc);
971 avc->diskSlot = slot;
972 QZero(&avc->metadirty);
974 AFS_RWLOCK_INIT(&avc->lock, "vcache lock");
976 memset(&avc->mvid, 0, sizeof(avc->mvid));
977 avc->linkData = NULL;
980 avc->execsOrWriters = 0;
982 avc->f.states = CVInit;
983 avc->last_looker = 0;
985 avc->asynchrony = -1;
989 avc->f.truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
991 afs_SetDataVersion(avc, &zero); /* in case we copy it into flushDV */
993 avc->callback = serverp; /* to minimize chance that clear
996 #if defined(AFS_CACHE_BYPASS)
997 avc->cachingStates = 0;
998 avc->cachingTransitions = 0;
1003 afs_FlushAllVCaches(void)
1006 struct vcache *tvc, *nvc;
1008 ObtainWriteLock(&afs_xvcache, 867);
1011 for (i = 0; i < VCSIZE; i++) {
1012 for (tvc = afs_vhashT[i]; tvc; tvc = nvc) {
1016 if (afs_FlushVCache(tvc, &slept)) {
1017 afs_warn("Failed to flush vcache 0x%lx\n", (unsigned long)(uintptrsz)tvc);
1025 ReleaseWriteLock(&afs_xvcache);
1029 * This routine is responsible for allocating a new cache entry
1030 * from the free list. It formats the cache entry and inserts it
1031 * into the appropriate hash tables. It must be called with
1032 * afs_xvcache write-locked so as to prevent several processes from
1033 * trying to create a new cache entry simultaneously.
1035 * LOCK: afs_NewVCache afs_xvcache W
1037 * \param afid The file id of the file whose cache entry is being created.
1039 * \return The new vcache struct.
1042 static_inline struct vcache *
1043 afs_NewVCache_int(struct VenusFid *afid, struct server *serverp, int seq)
1047 afs_int32 anumber = VCACHE_FREE;
1049 AFS_STATCNT(afs_NewVCache);
1051 afs_FlushReclaimedVcaches();
1053 #if defined(AFS_LINUX_ENV)
1054 if(!afsd_dynamic_vcaches && afs_vcount >= afs_maxvcount) {
1055 afs_ShakeLooseVCaches(anumber);
1056 if (afs_vcount >= afs_maxvcount) {
1057 afs_warn("afs_NewVCache - none freed\n");
1061 tvc = afs_AllocVCache();
1065 #else /* AFS_LINUX_ENV */
1066 /* pull out a free cache entry */
1068 afs_ShakeLooseVCaches(anumber);
1072 tvc = afs_AllocVCache();
1077 tvc = freeVCList; /* take from free list */
1078 freeVCList = tvc->nextfree;
1079 tvc->nextfree = NULL;
1080 afs_vcount++; /* balanced by FlushVCache */
1081 } /* end of if (!freeVCList) */
1083 #endif /* AFS_LINUX_ENV */
1085 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
1087 panic("afs_NewVCache(): free vcache with vnode attached");
1090 /* Populate the vcache with as much as we can. */
1091 afs_PrePopulateVCache(tvc, afid, serverp);
1093 /* Thread the vcache onto the VLRU */
1098 tvc->hnext = afs_vhashT[i];
1099 afs_vhashT[i] = tvc;
1100 QAdd(&afs_vhashTV[j], &tvc->vhashq);
1102 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1103 refpanic("NewVCache VLRU inconsistent");
1105 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
1106 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1107 refpanic("NewVCache VLRU inconsistent2");
1109 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
1110 refpanic("NewVCache VLRU inconsistent3");
1112 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
1113 refpanic("NewVCache VLRU inconsistent4");
1117 /* it should now be safe to drop the xvcache lock - so attach an inode
1118 * to this vcache, where necessary */
1119 osi_AttachVnode(tvc, seq);
1121 /* Get a reference count to hold this vcache for the VLRUQ. Note that
1122 * we have to do this after attaching the vnode, because the reference
1123 * count may be held in the vnode itself */
1125 #if defined(AFS_LINUX_ENV)
1126 /* Hold it for the LRU (should make count 2) */
1127 osi_Assert(osi_vnhold(tvc) == 0);
1128 #elif !(defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV))
1129 VREFCOUNT_SET(tvc, 1); /* us */
1132 #if defined (AFS_FBSD_ENV)
1133 if (tvc->f.states & CVInit)
1135 afs_PostPopulateVCache(tvc, afid, seq);
1138 } /*afs_NewVCache */
1142 afs_NewVCache(struct VenusFid *afid, struct server *serverp)
1144 return afs_NewVCache_int(afid, serverp, 0);
1148 afs_NewBulkVCache(struct VenusFid *afid, struct server *serverp, int seq)
1150 return afs_NewVCache_int(afid, serverp, seq);
1156 * LOCK: afs_FlushActiveVcaches afs_xvcache N
1158 * \param doflocks : Do we handle flocks?
1161 afs_FlushActiveVcaches(afs_int32 doflocks)
1165 struct afs_conn *tc;
1167 afs_ucred_t *cred = NULL;
1168 struct vrequest *treq = NULL;
1169 struct AFSVolSync tsync;
1172 AFS_STATCNT(afs_FlushActiveVcaches);
1174 code = afs_CreateReq(&treq, afs_osi_credp);
1176 afs_warn("unable to alloc treq\n");
1180 ObtainReadLock(&afs_xvcache);
1181 for (i = 0; i < VCSIZE; i++) {
1182 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1183 if (tvc->f.states & CVInit) continue;
1184 #ifdef AFS_DARWIN80_ENV
1185 if (tvc->f.states & CDeadVnode &&
1186 (tvc->f.states & (CCore|CUnlinkedDel) ||
1187 tvc->flockCount)) panic("Dead vnode has core/unlinkedel/flock");
1189 if (doflocks && tvc->flockCount != 0) {
1190 struct rx_connection *rxconn;
1191 if (osi_vnhold(tvc) != 0) {
1194 /* if this entry has an flock, send a keep-alive call out */
1195 ReleaseReadLock(&afs_xvcache);
1196 ObtainWriteLock(&tvc->lock, 51);
1198 code = afs_InitReq(treq, afs_osi_credp);
1201 break; /* shutting down: do not try to extend the lock */
1203 treq->flags |= O_NONBLOCK;
1205 tc = afs_Conn(&tvc->f.fid, treq, SHARED_LOCK, &rxconn);
1207 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
1210 RXAFS_ExtendLock(rxconn,
1211 (struct AFSFid *)&tvc->f.fid.Fid,
1217 } while (afs_Analyze
1218 (tc, rxconn, code, &tvc->f.fid, treq,
1219 AFS_STATS_FS_RPCIDX_EXTENDLOCK, SHARED_LOCK, NULL));
1221 ReleaseWriteLock(&tvc->lock);
1222 #ifdef AFS_DARWIN80_ENV
1224 ObtainReadLock(&afs_xvcache);
1226 ObtainReadLock(&afs_xvcache);
1231 if ((tvc->f.states & CCore) || (tvc->f.states & CUnlinkedDel)) {
1233 * Don't let it evaporate in case someone else is in
1234 * this code. Also, drop the afs_xvcache lock while
1235 * getting vcache locks.
1237 if (osi_vnhold(tvc) != 0) {
1240 ReleaseReadLock(&afs_xvcache);
1241 #if defined(AFS_SGI_ENV)
1243 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1245 osi_Assert(VREFCOUNT_GT(tvc,0));
1246 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1248 ObtainWriteLock(&tvc->lock, 52);
1249 if (tvc->f.states & CCore) {
1250 tvc->f.states &= ~CCore;
1251 /* XXXX Find better place-holder for cred XXXX */
1252 cred = (afs_ucred_t *)tvc->linkData;
1253 tvc->linkData = NULL; /* XXX */
1254 code = afs_InitReq(treq, cred);
1255 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1256 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
1257 tvc->execsOrWriters);
1258 if (!code) { /* avoid store when shutting down */
1259 code = afs_StoreOnLastReference(tvc, treq);
1261 ReleaseWriteLock(&tvc->lock);
1262 hzero(tvc->flushDV);
1265 if (code && code != VNOVNODE) {
1266 afs_StoreWarn(code, tvc->f.fid.Fid.Volume,
1267 /* /dev/console */ 1);
1269 } else if (tvc->f.states & CUnlinkedDel) {
1273 ReleaseWriteLock(&tvc->lock);
1274 #if defined(AFS_SGI_ENV)
1275 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1277 afs_remunlink(tvc, 0);
1278 #if defined(AFS_SGI_ENV)
1279 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1282 /* lost (or won, perhaps) the race condition */
1283 ReleaseWriteLock(&tvc->lock);
1285 #if defined(AFS_SGI_ENV)
1286 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1288 #ifdef AFS_DARWIN80_ENV
1291 AFS_RELE(AFSTOV(tvc));
1292 /* Matches write code setting CCore flag */
1295 ObtainReadLock(&afs_xvcache);
1297 ObtainReadLock(&afs_xvcache);
1300 AFS_RELE(AFSTOV(tvc));
1301 /* Matches write code setting CCore flag */
1308 ReleaseReadLock(&afs_xvcache);
1309 afs_DestroyReq(treq);
1315 * Make sure a cache entry is up-to-date status-wise.
1317 * NOTE: everywhere that calls this can potentially be sped up
1318 * by checking CStatd first, and avoiding doing the InitReq
1319 * if this is up-to-date.
1321 * Anymore, the only places that call this KNOW already that the
1322 * vcache is not up-to-date, so we don't screw around.
1324 * \param avc : Ptr to vcache entry to verify.
1330 * Make sure a cache entry is up-to-date status-wise.
1332 * NOTE: everywhere that calls this can potentially be sped up
1333 * by checking CStatd first, and avoiding doing the InitReq
1334 * if this is up-to-date.
1336 * Anymore, the only places that call this KNOW already that the
1337 * vcache is not up-to-date, so we don't screw around.
1339 * \param avc Pointer to vcache entry to verify.
1342 * \return 0 for success or other error codes.
1345 afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1349 AFS_STATCNT(afs_VerifyVCache);
1351 /* otherwise we must fetch the status info */
1353 ObtainWriteLock(&avc->lock, 53);
1354 if (avc->f.states & CStatd) {
1355 ReleaseWriteLock(&avc->lock);
1358 afs_StaleVCacheFlags(avc, AFS_STALEVC_FILENAME | AFS_STALEVC_CLEARCB,
1360 ReleaseWriteLock(&avc->lock);
1362 /* fetch the status info */
1363 tvc = afs_GetVCache(&avc->f.fid, areq);
1366 /* Put it back; caller has already incremented vrefCount */
1370 } /*afs_VerifyVCache */
1374 * Simple copy of stat info into cache.
1376 * Callers:as of 1992-04-29, only called by WriteVCache
1378 * \param avc Ptr to vcache entry involved.
1379 * \param astat Ptr to stat info to copy.
1383 afs_SimpleVStat(struct vcache *avc,
1384 struct AFSFetchStatus *astat, struct vrequest *areq)
1387 AFS_STATCNT(afs_SimpleVStat);
1389 #ifdef AFS_64BIT_CLIENT
1390 FillInt64(length, astat->Length_hi, astat->Length);
1391 #else /* AFS_64BIT_CLIENT */
1392 length = astat->Length;
1393 #endif /* AFS_64BIT_CLIENT */
1395 #if defined(AFS_SGI_ENV)
1396 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1397 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1398 osi_Assert((valusema(&avc->vc_rwlock) <= 0)
1399 && (OSI_GET_LOCKID() == avc->vc_rwlockid));
1400 if (length < avc->f.m.Length) {
1401 vnode_t *vp = (vnode_t *) avc;
1403 osi_Assert(WriteLocked(&avc->lock));
1404 ReleaseWriteLock(&avc->lock);
1406 PTOSSVP(vp, (off_t) length, (off_t) MAXLONG);
1408 ObtainWriteLock(&avc->lock, 67);
1413 if (!afs_DirtyPages(avc)) {
1414 /* if actively writing the file, don't fetch over this value */
1415 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT, ICL_TYPE_POINTER, avc,
1416 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1417 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1418 avc->f.m.Length = length;
1419 avc->f.m.Date = astat->ClientModTime;
1421 avc->f.m.Owner = astat->Owner;
1422 avc->f.m.Group = astat->Group;
1423 avc->f.m.Mode = astat->UnixModeBits;
1424 if (vType(avc) == VREG) {
1425 avc->f.m.Mode |= S_IFREG;
1426 } else if (vType(avc) == VDIR) {
1427 avc->f.m.Mode |= S_IFDIR;
1428 } else if (vType(avc) == VLNK) {
1429 avc->f.m.Mode |= S_IFLNK;
1430 if ((avc->f.m.Mode & 0111) == 0)
1431 avc->mvstat = AFS_MVSTAT_MTPT;
1433 if (avc->f.states & CForeign) {
1434 struct axscache *ac;
1435 avc->f.anyAccess = astat->AnonymousAccess;
1437 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1439 * Caller has at least one bit not covered by anonymous, and
1440 * thus may have interesting rights.
1442 * HOWEVER, this is a really bad idea, because any access query
1443 * for bits which aren't covered by anonymous, on behalf of a user
1444 * who doesn't have any special rights, will result in an answer of
1445 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1446 * It's an especially bad idea under Ultrix, since (due to the lack of
1447 * a proper access() call) it must perform several afs_access() calls
1448 * in order to create magic mode bits that vary according to who makes
1449 * the call. In other words, _every_ stat() generates a test for
1452 #endif /* badidea */
1453 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1454 ac->axess = astat->CallerAccess;
1455 else /* not found, add a new one if possible */
1456 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1459 } /*afs_SimpleVStat */
1463 * Store the status info *only* back to the server for a
1466 * Environment: Must be called with a shared lock held on the vnode.
1468 * \param avc Ptr to the vcache entry.
1469 * \param astatus Ptr to the status info to store.
1470 * \param areq Ptr to the associated vrequest.
1472 * \return Operation status.
1476 afs_WriteVCache(struct vcache *avc,
1477 struct AFSStoreStatus *astatus,
1478 struct vrequest *areq)
1481 struct afs_conn *tc;
1482 struct AFSFetchStatus OutStatus;
1483 struct AFSVolSync tsync;
1484 struct rx_connection *rxconn;
1486 AFS_STATCNT(afs_WriteVCache);
1487 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1488 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
1490 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK, &rxconn);
1492 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1495 RXAFS_StoreStatus(rxconn, (struct AFSFid *)&avc->f.fid.Fid,
1496 astatus, &OutStatus, &tsync);
1501 } while (afs_Analyze
1502 (tc, rxconn, code, &avc->f.fid, areq, AFS_STATS_FS_RPCIDX_STORESTATUS,
1503 SHARED_LOCK, NULL));
1505 UpgradeSToWLock(&avc->lock, 20);
1507 /* success, do the changes locally */
1508 afs_SimpleVStat(avc, &OutStatus, areq);
1510 * Update the date, too. SimpleVStat didn't do this, since
1511 * it thought we were doing this after fetching new status
1512 * over a file being written.
1514 avc->f.m.Date = OutStatus.ClientModTime;
1516 /* failure, set up to check with server next time */
1517 afs_StaleVCacheFlags(avc, 0, CUnique);
1519 ConvertWToSLock(&avc->lock);
1522 } /*afs_WriteVCache */
1525 * Store status info only locally, set the proper disconnection flags
1526 * and add to dirty list.
1528 * \param avc The vcache to be written locally.
1529 * \param astatus Get attr fields from local store.
1530 * \param attrs This one is only of the vs_size.
1532 * \note Must be called with a shared lock on the vnode
1535 afs_WriteVCacheDiscon(struct vcache *avc,
1536 struct AFSStoreStatus *astatus,
1537 struct vattr *attrs)
1540 afs_int32 flags = 0;
1542 UpgradeSToWLock(&avc->lock, 700);
1544 if (!astatus->Mask) {
1550 /* Set attributes. */
1551 if (astatus->Mask & AFS_SETMODTIME) {
1552 avc->f.m.Date = astatus->ClientModTime;
1553 flags |= VDisconSetTime;
1556 if (astatus->Mask & AFS_SETOWNER) {
1557 /* printf("Not allowed yet. \n"); */
1558 /*avc->f.m.Owner = astatus->Owner;*/
1561 if (astatus->Mask & AFS_SETGROUP) {
1562 /* printf("Not allowed yet. \n"); */
1563 /*avc->f.m.Group = astatus->Group;*/
1566 if (astatus->Mask & AFS_SETMODE) {
1567 avc->f.m.Mode = astatus->UnixModeBits;
1569 flags |= VDisconSetMode;
1570 } /* if(astatus.Mask & AFS_SETMODE) */
1572 } /* if (!astatus->Mask) */
1574 if (attrs->va_size > 0) {
1575 /* XXX: Do I need more checks? */
1576 /* Truncation operation. */
1577 flags |= VDisconTrunc;
1581 afs_DisconAddDirty(avc, flags, 1);
1583 /* XXX: How about the rest of the fields? */
1585 ConvertWToSLock(&avc->lock);
1591 * Copy astat block into vcache info
1593 * \note This code may get dataversion and length out of sync if the file has
1594 * been modified. This is less than ideal. I haven't thought about it sufficiently
1595 * to be certain that it is adequate.
1597 * \note Environment: Must be called under a write lock
1599 * \param avc Ptr to vcache entry.
1600 * \param astat Ptr to stat block to copy in.
1601 * \param areq Ptr to associated request.
1604 afs_ProcessFS(struct vcache *avc,
1605 struct AFSFetchStatus *astat, struct vrequest *areq)
1609 AFS_STATCNT(afs_ProcessFS);
1611 #ifdef AFS_64BIT_CLIENT
1612 FillInt64(length, astat->Length_hi, astat->Length);
1613 #else /* AFS_64BIT_CLIENT */
1614 length = astat->Length;
1615 #endif /* AFS_64BIT_CLIENT */
1616 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1617 * number for each bulk status request. Under no circumstances
1618 * should afs_DoBulkStat store a sequence number if the new
1619 * length will be ignored when afs_ProcessFS is called with
1620 * new stats. If you change the following conditional then you
1621 * also need to change the conditional in afs_DoBulkStat. */
1623 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1624 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1626 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1628 /* if we're writing or mapping this file, don't fetch over these
1631 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1632 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1633 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1634 avc->f.m.Length = length;
1635 avc->f.m.Date = astat->ClientModTime;
1637 hset64(newDV, astat->dataVersionHigh, astat->DataVersion);
1638 afs_SetDataVersion(avc, &newDV);
1639 avc->f.m.Owner = astat->Owner;
1640 avc->f.m.Mode = astat->UnixModeBits;
1641 avc->f.m.Group = astat->Group;
1642 avc->f.m.LinkCount = astat->LinkCount;
1643 if (astat->FileType == File) {
1644 vSetType(avc, VREG);
1645 avc->f.m.Mode |= S_IFREG;
1646 } else if (astat->FileType == Directory) {
1647 vSetType(avc, VDIR);
1648 avc->f.m.Mode |= S_IFDIR;
1649 } else if (astat->FileType == SymbolicLink) {
1650 if (afs_fakestat_enable && (avc->f.m.Mode & 0111) == 0) {
1651 vSetType(avc, VDIR);
1652 avc->f.m.Mode |= S_IFDIR;
1654 vSetType(avc, VLNK);
1655 avc->f.m.Mode |= S_IFLNK;
1657 if ((avc->f.m.Mode & 0111) == 0) {
1658 avc->mvstat = AFS_MVSTAT_MTPT;
1661 avc->f.anyAccess = astat->AnonymousAccess;
1663 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1665 * Caller has at least one bit not covered by anonymous, and
1666 * thus may have interesting rights.
1668 * HOWEVER, this is a really bad idea, because any access query
1669 * for bits which aren't covered by anonymous, on behalf of a user
1670 * who doesn't have any special rights, will result in an answer of
1671 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1672 * It's an especially bad idea under Ultrix, since (due to the lack of
1673 * a proper access() call) it must perform several afs_access() calls
1674 * in order to create magic mode bits that vary according to who makes
1675 * the call. In other words, _every_ stat() generates a test for
1678 #endif /* badidea */
1680 struct axscache *ac;
1681 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1682 ac->axess = astat->CallerAccess;
1683 else /* not found, add a new one if possible */
1684 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1686 } /*afs_ProcessFS */
1690 * Get fid from server.
1693 * \param areq Request to be passed on.
1694 * \param name Name of ?? to lookup.
1695 * \param OutStatus Fetch status.
1700 * \return Success status of operation.
1703 afs_RemoteLookup(struct VenusFid *afid, struct vrequest *areq,
1704 char *name, struct VenusFid *nfid,
1705 struct AFSFetchStatus *OutStatusp,
1706 struct AFSCallBack *CallBackp, struct server **serverp,
1707 struct AFSVolSync *tsyncp)
1710 struct afs_conn *tc;
1711 struct rx_connection *rxconn;
1712 struct AFSFetchStatus OutDirStatus;
1715 name = ""; /* XXX */
1717 tc = afs_Conn(afid, areq, SHARED_LOCK, &rxconn);
1720 *serverp = tc->parent->srvr->server;
1721 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1724 RXAFS_Lookup(rxconn, (struct AFSFid *)&afid->Fid, name,
1725 (struct AFSFid *)&nfid->Fid, OutStatusp,
1726 &OutDirStatus, CallBackp, tsyncp);
1731 } while (afs_Analyze
1732 (tc, rxconn, code, afid, areq, AFS_STATS_FS_RPCIDX_XLOOKUP, SHARED_LOCK,
1742 * Given a file id and a vrequest structure, fetch the status
1743 * information associated with the file.
1745 * \param afid File ID.
1746 * \param areq Ptr to associated vrequest structure, specifying the
1747 * user whose authentication tokens will be used.
1749 * \note Environment:
1750 * The cache entry is returned with an increased vrefCount field.
1751 * The entry must be discarded by calling afs_PutVCache when you
1752 * are through using the pointer to the cache entry.
1754 * You should not hold any locks when calling this function, except
1755 * locks on other vcache entries. If you lock more than one vcache
1756 * entry simultaneously, you should lock them in this order:
1758 * 1. Lock all files first, then directories.
1759 * 2. Within a particular type, lock entries in Fid.Vnode order.
1761 * This locking hierarchy is convenient because it allows locking
1762 * of a parent dir cache entry, given a file (to check its access
1763 * control list). It also allows renames to be handled easily by
1764 * locking directories in a constant order.
1766 * \note NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1769 afs_GetVCache(struct VenusFid *afid, struct vrequest *areq)
1772 afs_int32 code, newvcache = 0;
1777 AFS_STATCNT(afs_GetVCache);
1779 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1783 ObtainSharedLock(&afs_xvcache, 5);
1785 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU | IS_SLOCK);
1787 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1788 ReleaseSharedLock(&afs_xvcache);
1789 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1794 osi_Assert((tvc->f.states & CVInit) == 0);
1795 /* If we are in readdir, return the vnode even if not statd */
1796 if ((tvc->f.states & CStatd) || afs_InReadDir(tvc)) {
1797 ReleaseSharedLock(&afs_xvcache);
1801 UpgradeSToWLock(&afs_xvcache, 21);
1803 /* no cache entry, better grab one */
1804 tvc = afs_NewVCache(afid, NULL);
1807 ConvertWToSLock(&afs_xvcache);
1810 ReleaseSharedLock(&afs_xvcache);
1814 afs_stats_cmperf.vcacheMisses++;
1817 ReleaseSharedLock(&afs_xvcache);
1819 ObtainWriteLock(&tvc->lock, 54);
1821 if (tvc->f.states & CStatd) {
1822 ReleaseWriteLock(&tvc->lock);
1825 #ifdef AFS_DARWIN80_ENV
1826 /* Darwin 8.0 only has bufs in nfs, so we shouldn't have to worry about them.
1829 # if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
1831 * XXX - I really don't like this. Should try to understand better.
1832 * It seems that sometimes, when we get called, we already hold the
1833 * lock on the vnode (e.g., from afs_getattr via afs_VerifyVCache).
1834 * We can't drop the vnode lock, because that could result in a race.
1835 * Sometimes, though, we get here and don't hold the vnode lock.
1836 * I hate code paths that sometimes hold locks and sometimes don't.
1837 * In any event, the dodge we use here is to check whether the vnode
1838 * is locked, and if it isn't, then we gain and drop it around the call
1839 * to vinvalbuf; otherwise, we leave it alone.
1842 struct vnode *vp = AFSTOV(tvc);
1845 # if defined(AFS_DARWIN_ENV)
1846 iheldthelock = VOP_ISLOCKED(vp);
1848 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, current_proc());
1849 /* this is messy. we can call fsync which will try to reobtain this */
1850 if (VTOAFS(vp) == tvc)
1851 ReleaseWriteLock(&tvc->lock);
1852 if (UBCINFOEXISTS(vp)) {
1853 vinvalbuf(vp, V_SAVE, &afs_osi_cred, current_proc(), PINOD, 0);
1855 if (VTOAFS(vp) == tvc)
1856 ObtainWriteLock(&tvc->lock, 954);
1858 VOP_UNLOCK(vp, LK_EXCLUSIVE, current_proc());
1859 # elif defined(AFS_FBSD_ENV)
1861 iheldthelock = VOP_ISLOCKED(vp);
1862 if (!iheldthelock) {
1863 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1865 vinvalbuf(vp, V_SAVE, PINOD, 0); /* changed late in 8.0-CURRENT */
1869 # elif defined(AFS_OBSD_ENV)
1870 iheldthelock = VOP_ISLOCKED(vp, curproc);
1872 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1873 uvm_vnp_uncache(vp);
1875 VOP_UNLOCK(vp, 0, curproc);
1876 # elif defined(AFS_NBSD40_ENV)
1877 iheldthelock = VOP_ISLOCKED(vp);
1878 if (!iheldthelock) {
1879 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1881 uvm_vnp_uncache(vp);
1889 afs_StaleVCacheFlags(tvc, AFS_STALEVC_NODNLC | AFS_STALEVC_CLEARCB,
1892 /* It is always appropriate to throw away all the access rights? */
1893 afs_FreeAllAxs(&(tvc->Access));
1894 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1896 if ((tvp->states & VForeign)) {
1898 tvc->f.states |= CForeign;
1899 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1900 && (tvp->rootUnique == afid->Fid.Unique)) {
1901 tvc->mvstat = AFS_MVSTAT_ROOT;
1904 if (tvp->states & VRO)
1905 tvc->f.states |= CRO;
1906 if (tvp->states & VBackup)
1907 tvc->f.states |= CBackup;
1908 /* now copy ".." entry back out of volume structure, if necessary */
1909 if (tvc->mvstat == AFS_MVSTAT_ROOT && tvp->dotdot.Fid.Volume != 0) {
1910 if (!tvc->mvid.parent)
1911 tvc->mvid.parent = (struct VenusFid *)
1912 osi_AllocSmallSpace(sizeof(struct VenusFid));
1913 *tvc->mvid.parent = tvp->dotdot;
1915 afs_PutVolume(tvp, READ_LOCK);
1919 afs_RemoveVCB(afid);
1921 struct AFSFetchStatus OutStatus;
1923 if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1924 afs_ProcessFS(tvc, &OutStatus, areq);
1925 tvc->f.states |= CStatd | CUnique;
1926 tvc->f.parent.vnode = OutStatus.ParentVnode;
1927 tvc->f.parent.unique = OutStatus.ParentUnique;
1931 if (AFS_IS_DISCONNECTED) {
1932 /* Nothing to do otherwise...*/
1934 /* printf("Network is down in afs_GetCache"); */
1936 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1938 /* For the NFS translator's benefit, make sure
1939 * non-directory vnodes always have their parent FID set
1940 * correctly, even when created as a result of decoding an
1941 * NFS filehandle. It would be nice to also do this for
1942 * directories, but we can't because the fileserver fills
1943 * in the FID of the directory itself instead of that of
1946 if (!code && OutStatus.FileType != Directory &&
1947 !tvc->f.parent.vnode) {
1948 tvc->f.parent.vnode = OutStatus.ParentVnode;
1949 tvc->f.parent.unique = OutStatus.ParentUnique;
1950 /* XXX - SXW - It's conceivable we should mark ourselves
1951 * as dirty again here, incase we've been raced
1952 * out of the FetchStatus call.
1959 ReleaseWriteLock(&tvc->lock);
1965 ReleaseWriteLock(&tvc->lock);
1968 } /*afs_GetVCache */
1973 * Lookup a vcache by fid. Look inside the cache first, if not
1974 * there, lookup the file on the server, and then get it's fresh
1982 * \return The found element or NULL.
1985 afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1986 struct vcache *adp, char *aname)
1988 afs_int32 code, now, newvcache = 0;
1989 struct VenusFid nfid;
1992 struct AFSFetchStatus OutStatus;
1993 struct AFSCallBack CallBack;
1994 struct AFSVolSync tsync;
1995 struct server *serverp = 0;
1999 AFS_STATCNT(afs_GetVCache);
2001 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2005 ObtainReadLock(&afs_xvcache);
2006 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */ );
2009 ReleaseReadLock(&afs_xvcache);
2011 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2012 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2016 ObtainReadLock(&tvc->lock);
2018 if (tvc->f.states & CStatd) {
2019 ReleaseReadLock(&tvc->lock);
2022 tvc->f.states &= ~CUnique;
2024 ReleaseReadLock(&tvc->lock);
2026 ObtainReadLock(&afs_xvcache);
2029 ReleaseReadLock(&afs_xvcache);
2031 /* lookup the file */
2034 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
2036 if (AFS_IS_DISCONNECTED) {
2037 /* printf("Network is down in afs_LookupVcache\n"); */
2041 afs_RemoteLookup(&adp->f.fid, areq, aname, &nfid, &OutStatus,
2042 &CallBack, &serverp, &tsync);
2044 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2048 ObtainSharedLock(&afs_xvcache, 6);
2049 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU | IS_SLOCK/* no xstats now */ );
2051 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2052 ReleaseSharedLock(&afs_xvcache);
2053 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2059 /* no cache entry, better grab one */
2060 UpgradeSToWLock(&afs_xvcache, 22);
2061 tvc = afs_NewVCache(&nfid, serverp);
2063 ConvertWToSLock(&afs_xvcache);
2066 ReleaseSharedLock(&afs_xvcache);
2071 ReleaseSharedLock(&afs_xvcache);
2072 ObtainWriteLock(&tvc->lock, 55);
2074 /* It is always appropriate to throw away all the access rights? */
2075 afs_FreeAllAxs(&(tvc->Access));
2076 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
2078 if ((tvp->states & VForeign)) {
2080 tvc->f.states |= CForeign;
2081 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
2082 && (tvp->rootUnique == afid->Fid.Unique))
2083 tvc->mvstat = AFS_MVSTAT_ROOT;
2085 if (tvp->states & VRO)
2086 tvc->f.states |= CRO;
2087 if (tvp->states & VBackup)
2088 tvc->f.states |= CBackup;
2089 /* now copy ".." entry back out of volume structure, if necessary */
2090 if (tvc->mvstat == AFS_MVSTAT_ROOT && tvp->dotdot.Fid.Volume != 0) {
2091 if (!tvc->mvid.parent)
2092 tvc->mvid.parent = (struct VenusFid *)
2093 osi_AllocSmallSpace(sizeof(struct VenusFid));
2094 *tvc->mvid.parent = tvp->dotdot;
2099 afs_StaleVCacheFlags(tvc, 0, CUnique);
2101 afs_PutVolume(tvp, READ_LOCK);
2102 ReleaseWriteLock(&tvc->lock);
2107 ObtainWriteLock(&afs_xcbhash, 466);
2108 if (origCBs == afs_allCBs) {
2109 if (CallBack.ExpirationTime) {
2110 tvc->callback = serverp;
2111 tvc->cbExpires = CallBack.ExpirationTime + now;
2112 tvc->f.states |= CStatd | CUnique;
2113 tvc->f.states &= ~CBulkFetching;
2114 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
2115 } else if (tvc->f.states & CRO) {
2116 /* adapt gives us an hour. */
2117 tvc->cbExpires = 3600 + osi_Time();
2118 /*XXX*/ tvc->f.states |= CStatd | CUnique;
2119 tvc->f.states &= ~CBulkFetching;
2120 afs_QueueCallback(tvc, CBHash(3600), tvp);
2122 afs_StaleVCacheFlags(tvc,
2123 AFS_STALEVC_CBLOCKED | AFS_STALEVC_CLEARCB,
2127 afs_StaleVCacheFlags(tvc,
2128 AFS_STALEVC_CBLOCKED | AFS_STALEVC_CLEARCB,
2131 ReleaseWriteLock(&afs_xcbhash);
2133 afs_PutVolume(tvp, READ_LOCK);
2134 afs_ProcessFS(tvc, &OutStatus, areq);
2136 ReleaseWriteLock(&tvc->lock);
2142 afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
2143 struct volume *tvolp)
2145 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
2146 afs_int32 getNewFid = 0;
2148 struct VenusFid nfid;
2150 struct server *serverp = 0;
2151 struct AFSFetchStatus OutStatus;
2152 struct AFSCallBack CallBack;
2153 struct AFSVolSync tsync;
2155 #ifdef AFS_DARWIN80_ENV
2162 if (!tvolp->rootVnode || getNewFid) {
2163 struct VenusFid tfid;
2166 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2167 origCBs = afs_allCBs; /* ignore InitCallBackState */
2169 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2174 /* ReleaseReadLock(&tvolp->lock); */
2175 ObtainWriteLock(&tvolp->lock, 56);
2176 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
2177 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
2178 ReleaseWriteLock(&tvolp->lock);
2179 /* ObtainReadLock(&tvolp->lock);*/
2182 afid->Fid.Vnode = tvolp->rootVnode;
2183 afid->Fid.Unique = tvolp->rootUnique;
2187 ObtainSharedLock(&afs_xvcache, 7);
2189 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2190 if (!FidCmp(&(tvc->f.fid), afid)) {
2191 if (tvc->f.states & CVInit) {
2192 ReleaseSharedLock(&afs_xvcache);
2193 afs_osi_Sleep(&tvc->f.states);
2196 #ifdef AFS_DARWIN80_ENV
2197 if (tvc->f.states & CDeadVnode) {
2198 ReleaseSharedLock(&afs_xvcache);
2199 afs_osi_Sleep(&tvc->f.states);
2203 if (vnode_get(tvp)) /* this bumps ref count */
2205 if (vnode_ref(tvp)) {
2207 /* AFSTOV(tvc) may be NULL */
2213 if (osi_vnhold(tvc) != 0) {
2221 if (!haveStatus && (!tvc || !(tvc->f.states & CStatd))) {
2222 /* Mount point no longer stat'd or unknown. FID may have changed. */
2224 #ifdef AFS_DARWIN80_ENV
2225 ReleaseSharedLock(&afs_xvcache);
2228 vnode_put(AFSTOV(tvc));
2229 vnode_rele(AFSTOV(tvc));
2236 ReleaseSharedLock(&afs_xvcache);
2243 UpgradeSToWLock(&afs_xvcache, 23);
2244 /* no cache entry, better grab one */
2245 tvc = afs_NewVCache(afid, NULL);
2248 ReleaseWriteLock(&afs_xvcache);
2252 afs_stats_cmperf.vcacheMisses++;
2254 afs_stats_cmperf.vcacheHits++;
2255 UpgradeSToWLock(&afs_xvcache, 24);
2256 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2257 refpanic("GRVC VLRU inconsistent0");
2259 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2260 refpanic("GRVC VLRU inconsistent1");
2262 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2263 refpanic("GRVC VLRU inconsistent2");
2265 QRemove(&tvc->vlruq); /* move to lruq head */
2266 QAdd(&VLRU, &tvc->vlruq);
2267 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2268 refpanic("GRVC VLRU inconsistent3");
2270 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2271 refpanic("GRVC VLRU inconsistent4");
2273 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2274 refpanic("GRVC VLRU inconsistent5");
2279 ReleaseWriteLock(&afs_xvcache);
2281 if (tvc->f.states & CStatd) {
2285 ObtainReadLock(&tvc->lock);
2286 tvc->f.states &= ~CUnique;
2287 tvc->callback = NULL; /* redundant, perhaps */
2288 ReleaseReadLock(&tvc->lock);
2291 ObtainWriteLock(&tvc->lock, 57);
2293 /* It is always appropriate to throw away all the access rights? */
2294 afs_FreeAllAxs(&(tvc->Access));
2297 tvc->f.states |= CForeign;
2298 if (tvolp->states & VRO)
2299 tvc->f.states |= CRO;
2300 if (tvolp->states & VBackup)
2301 tvc->f.states |= CBackup;
2302 /* now copy ".." entry back out of volume structure, if necessary */
2303 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2304 && (tvolp->rootUnique == afid->Fid.Unique)) {
2305 tvc->mvstat = AFS_MVSTAT_ROOT;
2307 if (tvc->mvstat == AFS_MVSTAT_ROOT && tvolp->dotdot.Fid.Volume != 0) {
2308 if (!tvc->mvid.parent)
2309 tvc->mvid.parent = (struct VenusFid *)
2310 osi_AllocSmallSpace(sizeof(struct VenusFid));
2311 *tvc->mvid.parent = tvolp->dotdot;
2315 afs_RemoveVCB(afid);
2318 struct VenusFid tfid;
2321 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2322 origCBs = afs_allCBs; /* ignore InitCallBackState */
2324 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2329 afs_StaleVCacheFlags(tvc, AFS_STALEVC_CLEARCB, CUnique);
2330 ReleaseWriteLock(&tvc->lock);
2335 ObtainWriteLock(&afs_xcbhash, 468);
2336 if (origCBs == afs_allCBs) {
2337 tvc->f.states |= CTruth;
2338 tvc->callback = serverp;
2339 if (CallBack.ExpirationTime != 0) {
2340 tvc->cbExpires = CallBack.ExpirationTime + start;
2341 tvc->f.states |= CStatd;
2342 tvc->f.states &= ~CBulkFetching;
2343 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2344 } else if (tvc->f.states & CRO) {
2345 /* adapt gives us an hour. */
2346 tvc->cbExpires = 3600 + osi_Time();
2347 /*XXX*/ tvc->f.states |= CStatd;
2348 tvc->f.states &= ~CBulkFetching;
2349 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2352 afs_StaleVCacheFlags(tvc, AFS_STALEVC_CBLOCKED | AFS_STALEVC_CLEARCB,
2355 ReleaseWriteLock(&afs_xcbhash);
2356 afs_ProcessFS(tvc, &OutStatus, areq);
2358 ReleaseWriteLock(&tvc->lock);
2364 * Update callback status and (sometimes) attributes of a vnode.
2365 * Called after doing a fetch status RPC. Whilst disconnected, attributes
2366 * shouldn't be written to the vcache here.
2371 * \param Outsp Server status after rpc call.
2372 * \param acb Callback for this vnode.
2374 * \note The vcache must be write locked.
2377 afs_UpdateStatus(struct vcache *avc, struct VenusFid *afid,
2378 struct vrequest *areq, struct AFSFetchStatus *Outsp,
2379 struct AFSCallBack *acb, afs_uint32 start)
2381 struct volume *volp;
2384 /* Dont write status in vcache if resyncing after a disconnection. */
2385 afs_ProcessFS(avc, Outsp, areq);
2387 volp = afs_GetVolume(afid, areq, READ_LOCK);
2388 ObtainWriteLock(&afs_xcbhash, 469);
2389 avc->f.states |= CTruth;
2390 if (avc->callback /* check for race */ ) {
2391 if (acb->ExpirationTime != 0) {
2392 avc->cbExpires = acb->ExpirationTime + start;
2393 avc->f.states |= CStatd;
2394 avc->f.states &= ~CBulkFetching;
2395 afs_QueueCallback(avc, CBHash(acb->ExpirationTime), volp);
2396 } else if (avc->f.states & CRO) {
2397 /* ordinary callback on a read-only volume -- AFS 3.2 style */
2398 avc->cbExpires = 3600 + start;
2399 avc->f.states |= CStatd;
2400 avc->f.states &= ~CBulkFetching;
2401 afs_QueueCallback(avc, CBHash(3600), volp);
2403 afs_StaleVCacheFlags(avc,
2404 AFS_STALEVC_CBLOCKED | AFS_STALEVC_CLEARCB,
2408 afs_StaleVCacheFlags(avc, AFS_STALEVC_CBLOCKED | AFS_STALEVC_CLEARCB,
2411 ReleaseWriteLock(&afs_xcbhash);
2413 afs_PutVolume(volp, READ_LOCK);
2417 afs_BadFetchStatus(struct afs_conn *tc)
2419 int addr = ntohl(tc->parent->srvr->sa_ip);
2420 afs_warn("afs: Invalid AFSFetchStatus from server %u.%u.%u.%u\n",
2421 (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff,
2423 afs_warn("afs: This suggests the server may be sending bad data that "
2424 "can lead to availability issues or data corruption. The "
2425 "issue has been avoided for now, but it may not always be "
2426 "detectable. Please upgrade the server if possible.\n");
2430 * Check if a given AFSFetchStatus structure is sane.
2432 * @param[in] tc The server from which we received the status
2433 * @param[in] status The status we received
2435 * @return whether the given structure is valid or not
2436 * @retval 0 the structure is fine
2437 * @retval nonzero the structure looks like garbage; act as if we received
2438 * the returned error code from the server
2441 afs_CheckFetchStatus(struct afs_conn *tc, struct AFSFetchStatus *status)
2443 if (status->errorCode ||
2444 status->InterfaceVersion != 1 ||
2445 !(status->FileType > Invalid && status->FileType <= SymbolicLink) ||
2446 status->ParentVnode == 0 || status->ParentUnique == 0) {
2448 afs_warn("afs: FetchStatus ec %u iv %u ft %u pv %u pu %u\n",
2449 (unsigned)status->errorCode, (unsigned)status->InterfaceVersion,
2450 (unsigned)status->FileType, (unsigned)status->ParentVnode,
2451 (unsigned)status->ParentUnique);
2452 afs_BadFetchStatus(tc);
2460 * Must be called with avc write-locked
2461 * don't absolutely have to invalidate the hint unless the dv has
2462 * changed, but be sure to get it right else there will be consistency bugs.
2465 afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
2466 struct vrequest * areq, struct AFSFetchStatus * Outsp)
2469 afs_uint32 start = 0;
2470 struct afs_conn *tc;
2471 struct AFSCallBack CallBack;
2472 struct AFSVolSync tsync;
2473 struct rx_connection *rxconn;
2476 tc = afs_Conn(afid, areq, SHARED_LOCK, &rxconn);
2477 avc->dchint = NULL; /* invalidate hints */
2479 avc->callback = tc->parent->srvr->server;
2481 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2484 RXAFS_FetchStatus(rxconn, (struct AFSFid *)&afid->Fid, Outsp,
2491 code = afs_CheckFetchStatus(tc, Outsp);
2496 } while (afs_Analyze
2497 (tc, rxconn, code, afid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2498 SHARED_LOCK, NULL));
2501 afs_UpdateStatus(avc, afid, areq, Outsp, &CallBack, start);
2503 /* used to undo the local callback, but that's too extreme.
2504 * There are plenty of good reasons that fetchstatus might return
2505 * an error, such as EPERM. If we have the vnode cached, statd,
2506 * with callback, might as well keep track of the fact that we
2507 * don't have access...
2509 if (code == EPERM || code == EACCES) {
2510 struct axscache *ac;
2511 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2513 else /* not found, add a new one if possible */
2514 afs_AddAxs(avc->Access, areq->uid, 0);
2521 * Decrements the reference count on a cache entry.
2523 * \param avc Pointer to the cache entry to decrement.
2525 * \note Environment: Nothing interesting.
2528 afs_PutVCache(struct vcache *avc)
2530 AFS_STATCNT(afs_PutVCache);
2531 #ifdef AFS_DARWIN80_ENV
2532 vnode_put(AFSTOV(avc));
2536 * Can we use a read lock here?
2538 ObtainReadLock(&afs_xvcache);
2540 ReleaseReadLock(&afs_xvcache);
2542 } /*afs_PutVCache */
2546 * Reset a vcache entry, so local contents are ignored, and the
2547 * server will be reconsulted next time the vcache is used
2549 * \param avc Pointer to the cache entry to reset
2551 * \param skipdnlc skip the dnlc purge for this vnode
2553 * \note avc must be write locked on entry
2555 * \note The caller should purge the dnlc when skipdnlc is set.
2558 afs_ResetVCache(struct vcache *avc, afs_ucred_t *acred, afs_int32 skipdnlc)
2560 afs_stalevc_flags_t flags = 0;
2562 flags |= AFS_STALEVC_NODNLC;
2565 afs_StaleVCacheFlags(avc, flags, CDirty); /* next reference will re-stat */
2566 /* now find the disk cache entries */
2567 afs_TryToSmush(avc, acred, 1);
2568 if (avc->linkData && !(avc->f.states & CCore)) {
2569 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
2570 avc->linkData = NULL;
2575 * Sleepa when searching for a vcache. Releases all the pending locks,
2576 * sleeps then obtains the previously released locks.
2578 * \param vcache Enter sleep state.
2579 * \param flag Determines what locks to use.
2584 findvc_sleep(struct vcache *avc, int flag)
2586 if (flag & IS_SLOCK) {
2587 ReleaseSharedLock(&afs_xvcache);
2589 if (flag & IS_WLOCK) {
2590 ReleaseWriteLock(&afs_xvcache);
2592 ReleaseReadLock(&afs_xvcache);
2595 afs_osi_Sleep(&avc->f.states);
2596 if (flag & IS_SLOCK) {
2597 ObtainSharedLock(&afs_xvcache, 341);
2599 if (flag & IS_WLOCK) {
2600 ObtainWriteLock(&afs_xvcache, 343);
2602 ObtainReadLock(&afs_xvcache);
2608 * Add a reference on an existing vcache entry.
2610 * \param tvc Pointer to the vcache.
2612 * \note Environment: Must be called with at least one reference from
2613 * elsewhere on the vcache, even if that reference will be dropped.
2614 * The global lock is required.
2616 * \return 0 on success, -1 on failure.
2620 afs_RefVCache(struct vcache *tvc)
2622 #ifdef AFS_DARWIN80_ENV
2626 /* AFS_STATCNT(afs_RefVCache); */
2628 #ifdef AFS_DARWIN80_ENV
2632 if (vnode_ref(tvp)) {
2634 /* AFSTOV(tvc) may be NULL */
2640 if (osi_vnhold(tvc) != 0) {
2645 } /*afs_RefVCache */
2648 * Find a vcache entry given a fid.
2650 * \param afid Pointer to the fid whose cache entry we desire.
2651 * \param retry (SGI-specific) tell the caller to drop the lock on xvcache,
2652 * unlock the vnode, and try again.
2653 * \param flag Bit 1 to specify whether to compute hit statistics. Not
2654 * set if FindVCache is called as part of internal bookkeeping.
2656 * \note Environment: Must be called with the afs_xvcache lock at least held at
2657 * the read level. In order to do the VLRU adjustment, the xvcache lock
2658 * must be shared-- we upgrade it here.
2662 afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
2667 #ifdef AFS_DARWIN80_ENV
2668 struct vcache *deadvc = NULL, *livevc = NULL;
2672 AFS_STATCNT(afs_FindVCache);
2676 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2677 if (FidMatches(afid, tvc)) {
2678 if (tvc->f.states & CVInit) {
2679 findvc_sleep(tvc, flag);
2682 #ifdef AFS_DARWIN80_ENV
2683 if (tvc->f.states & CDeadVnode) {
2684 findvc_sleep(tvc, flag);
2692 /* should I have a read lock on the vnode here? */
2694 #if defined(AFS_DARWIN80_ENV)
2698 if (tvp && vnode_ref(tvp)) {
2700 /* AFSTOV(tvc) may be NULL */
2709 #elif defined(AFS_DARWIN_ENV)
2710 tvc->f.states |= CUBCinit;
2712 if (UBCINFOMISSING(AFSTOV(tvc)) ||
2713 UBCINFORECLAIMED(AFSTOV(tvc))) {
2714 ubc_info_init(AFSTOV(tvc));
2717 tvc->f.states &= ~CUBCinit;
2719 if (osi_vnhold(tvc) != 0) {
2726 * only move to front of vlru if we have proper vcache locking)
2728 if (flag & DO_VLRU) {
2729 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2730 refpanic("FindVC VLRU inconsistent1");
2732 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2733 refpanic("FindVC VLRU inconsistent1");
2735 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2736 refpanic("FindVC VLRU inconsistent2");
2738 UpgradeSToWLock(&afs_xvcache, 26);
2739 QRemove(&tvc->vlruq);
2740 QAdd(&VLRU, &tvc->vlruq);
2741 ConvertWToSLock(&afs_xvcache);
2742 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2743 refpanic("FindVC VLRU inconsistent1");
2745 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2746 refpanic("FindVC VLRU inconsistent2");
2748 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2749 refpanic("FindVC VLRU inconsistent3");
2755 if (flag & DO_STATS) {
2757 afs_stats_cmperf.vcacheHits++;
2759 afs_stats_cmperf.vcacheMisses++;
2760 if (afs_IsPrimaryCellNum(afid->Cell))
2761 afs_stats_cmperf.vlocalAccesses++;
2763 afs_stats_cmperf.vremoteAccesses++;
2766 } /*afs_FindVCache */
2769 * Find a vcache entry given a fid. Does a wildcard match on what we
2770 * have for the fid. If more than one entry, don't return anything.
2772 * \param avcp Fill in pointer if we found one and only one.
2773 * \param afid Pointer to the fid whose cache entry we desire.
2774 * \param retry (SGI-specific) tell the caller to drop the lock on xvcache,
2775 * unlock the vnode, and try again.
2776 * \param flags bit 1 to specify whether to compute hit statistics. Not
2777 * set if FindVCache is called as part of internal bookkeeping.
2779 * \note Environment: Must be called with the afs_xvcache lock at least held at
2780 * the read level. In order to do the VLRU adjustment, the xvcache lock
2781 * must be shared-- we upgrade it here.
2783 * \return Number of matches found.
2786 int afs_duplicate_nfs_fids = 0;
2789 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2793 afs_int32 count = 0;
2794 struct vcache *found_tvc = NULL;
2795 #ifdef AFS_DARWIN80_ENV
2799 AFS_STATCNT(afs_FindVCache);
2803 ObtainSharedLock(&afs_xvcache, 331);
2806 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2807 /* Match only on what we have.... */
2808 if (((tvc->f.fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2809 && (tvc->f.fid.Fid.Volume == afid->Fid.Volume)
2810 && ((tvc->f.fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2811 && (tvc->f.fid.Cell == afid->Cell)) {
2812 if (tvc->f.states & CVInit) {
2813 ReleaseSharedLock(&afs_xvcache);
2814 afs_osi_Sleep(&tvc->f.states);
2817 #ifdef AFS_DARWIN80_ENV
2818 if (tvc->f.states & CDeadVnode) {
2819 ReleaseSharedLock(&afs_xvcache);
2820 afs_osi_Sleep(&tvc->f.states);
2824 if (vnode_get(tvp)) {
2825 /* This vnode no longer exists. */
2828 if (vnode_ref(tvp)) {
2829 /* This vnode no longer exists. */
2831 /* AFSTOV(tvc) may be NULL */
2837 if (osi_vnhold(tvc) != 0) {
2840 #endif /* AFS_DARWIN80_ENV */
2844 afs_duplicate_nfs_fids++;
2845 #ifndef AFS_DARWIN80_ENV
2847 AFS_FAST_RELE(found_tvc);
2849 ReleaseSharedLock(&afs_xvcache);
2850 #ifdef AFS_DARWIN80_ENV
2851 /* Drop our reference counts. */
2852 vnode_put(AFSTOV(tvc));
2853 vnode_put(AFSTOV(found_tvc));
2862 /* should I have a read lock on the vnode here? */
2865 * We obtained the xvcache lock above.
2867 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2868 refpanic("FindVC VLRU inconsistent1");
2870 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2871 refpanic("FindVC VLRU inconsistent1");
2873 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2874 refpanic("FindVC VLRU inconsistent2");
2876 UpgradeSToWLock(&afs_xvcache, 568);
2877 QRemove(&tvc->vlruq);
2878 QAdd(&VLRU, &tvc->vlruq);
2879 ConvertWToSLock(&afs_xvcache);
2880 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2881 refpanic("FindVC VLRU inconsistent1");
2883 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2884 refpanic("FindVC VLRU inconsistent2");
2886 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2887 refpanic("FindVC VLRU inconsistent3");
2893 afs_stats_cmperf.vcacheHits++;
2895 afs_stats_cmperf.vcacheMisses++;
2896 if (afs_IsPrimaryCellNum(afid->Cell))
2897 afs_stats_cmperf.vlocalAccesses++;
2899 afs_stats_cmperf.vremoteAccesses++;
2901 *avcp = tvc; /* May be null */
2903 ReleaseSharedLock(&afs_xvcache);
2904 return (tvc ? 1 : 0);
2906 } /*afs_NFSFindVCache */
2912 * Initialize vcache related variables
2917 afs_vcacheInit(int astatSize)
2919 #if !defined(AFS_LINUX_ENV)
2923 if (!afs_maxvcount) {
2924 afs_maxvcount = astatSize; /* no particular limit on linux? */
2926 #if !defined(AFS_LINUX_ENV)
2930 AFS_RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2931 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2933 #if !defined(AFS_LINUX_ENV)
2934 /* Allocate and thread the struct vcache entries */
2935 tvp = afs_osi_Alloc(astatSize * sizeof(struct vcache));
2936 osi_Assert(tvp != NULL);
2937 memset(tvp, 0, sizeof(struct vcache) * astatSize);
2939 Initial_freeVCList = tvp;
2940 freeVCList = &(tvp[0]);
2941 for (i = 0; i < astatSize - 1; i++) {
2942 tvp[i].nextfree = &(tvp[i + 1]);
2944 tvp[astatSize - 1].nextfree = NULL;
2945 # ifdef KERNEL_HAVE_PIN
2946 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
2950 #if defined(AFS_SGI_ENV)
2951 for (i = 0; i < astatSize; i++) {
2952 char name[METER_NAMSZ];
2953 struct vcache *tvc = &tvp[i];
2955 tvc->v.v_number = ++afsvnumbers;
2956 tvc->vc_rwlockid = OSI_NO_LOCKID;
2957 initnsema(&tvc->vc_rwlock, 1,
2958 makesname(name, "vrw", tvc->v.v_number));
2959 # ifndef AFS_SGI53_ENV
2960 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
2962 # ifndef AFS_SGI62_ENV
2963 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
2964 # endif /* AFS_SGI62_ENV */
2968 for(i = 0; i < VCSIZE; ++i)
2969 QInit(&afs_vhashTV[i]);
2976 shutdown_vcache(void)
2979 struct afs_cbr *tsp;
2981 * XXX We may potentially miss some of the vcaches because if when
2982 * there are no free vcache entries and all the vcache entries are active
2983 * ones then we allocate an additional one - admittedly we almost never
2988 struct afs_q *tq, *uq = NULL;
2990 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
2993 if (tvc->mvid.target_root) {
2994 osi_FreeSmallSpace(tvc->mvid.target_root);
2995 tvc->mvid.target_root = NULL;
2998 aix_gnode_rele(AFSTOV(tvc));
3000 if (tvc->linkData) {
3001 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3006 * Also free the remaining ones in the Cache
3008 for (i = 0; i < VCSIZE; i++) {
3009 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3010 if (tvc->mvid.target_root) {
3011 osi_FreeSmallSpace(tvc->mvid.target_root);
3012 tvc->mvid.target_root = NULL;
3016 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
3017 # ifdef AFS_AIX32_ENV
3020 vms_delete(tvc->segid);
3022 tvc->segid = tvc->vmh = NULL;
3023 if (VREFCOUNT_GT(tvc,0))
3024 osi_Panic("flushVcache: vm race");
3032 #if defined(AFS_SUN5_ENV)
3038 if (tvc->linkData) {
3039 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3044 afs_FreeAllAxs(&(tvc->Access));
3050 * Free any leftover callback queue
3052 for (i = 0; i < afs_stats_cmperf.CallBackAlloced; i++) {
3053 tsp = afs_cbrHeads[i];
3054 afs_cbrHeads[i] = 0;
3055 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
3059 #if !defined(AFS_LINUX_ENV)
3060 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3062 # ifdef KERNEL_HAVE_PIN
3063 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3066 freeVCList = Initial_freeVCList = 0;
3069 AFS_RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
3070 LOCK_INIT(&afs_xvcb, "afs_xvcb");
3072 for(i = 0; i < VCSIZE; ++i)
3073 QInit(&afs_vhashTV[i]);
3077 afs_DisconGiveUpCallbacks(void)
3083 ObtainWriteLock(&afs_xvcache, 1002); /* XXX - should be a unique number */
3086 /* Somehow, walk the set of vcaches, with each one coming out as tvc */
3087 for (i = 0; i < VCSIZE; i++) {
3088 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3090 if (afs_QueueVCB(tvc, &slept)) {
3091 tvc->callback = NULL;
3100 ReleaseWriteLock(&afs_xvcache);
3107 * Clear the Statd flag from all vcaches
3109 * This function removes the Statd flag from all vcaches. It's used by
3110 * disconnected mode to tidy up during reconnection
3114 afs_ClearAllStatdFlag(void)
3119 ObtainWriteLock(&afs_xvcache, 715);
3121 for (i = 0; i < VCSIZE; i++) {
3122 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3123 afs_StaleVCacheFlags(tvc, AFS_STALEVC_NODNLC | AFS_STALEVC_NOCB,
3127 ReleaseWriteLock(&afs_xvcache);
3131 * Mark a vcache as stale; our metadata for the relevant file may be out of
3134 * @post Any subsequent access to this vcache will cause us to fetch the
3135 * metadata for this vcache again.
3138 afs_StaleVCacheFlags(struct vcache *avc, afs_stalevc_flags_t flags,
3142 int do_filename = 0;
3144 int lock_cbhash = 1;
3146 if ((flags & AFS_STALEVC_NODNLC)) {
3149 if ((flags & AFS_STALEVC_FILENAME)) {
3152 if ((flags & AFS_STALEVC_CBLOCKED)) {
3155 if ((flags & AFS_STALEVC_NOCB)) {
3161 ObtainWriteLock(&afs_xcbhash, 486);
3164 afs_DequeueCallback(avc);
3168 avc->f.states &= ~cflags;
3171 ReleaseWriteLock(&afs_xcbhash);
3174 if ((flags & AFS_STALEVC_SKIP_DNLC_FOR_INIT_FLUSHED) &&
3175 (avc->f.states & (CVInit | CVFlushed))) {
3179 if (flags & AFS_STALEVC_CLEARCB) {
3180 avc->callback = NULL;
3184 if ((avc->f.fid.Fid.Vnode & 1) ||
3185 AFSTOV(avc) == NULL || vType(avc) == VDIR ||
3186 (avc->f.states & CForeign)) {
3187 /* This vcache is (or could be) a directory. */
3188 osi_dnlc_purgedp(avc);
3190 } else if (do_filename) {
3191 osi_dnlc_purgevp(avc);
3197 afs_SetDataVersion(struct vcache *avc, afs_hyper_t *avers)
3199 hset(avc->f.m.DataVersion, *avers);