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_LINUX22_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_LINUX22_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_LINUX26_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_LINUX22_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_LINUX22_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);
315 if (avc->f.states & CDirty) {
316 /* we can't keep trying to push back dirty data forever. Give up. */
317 afs_InvalidateAllSegments(avc); /* turns off dirty bit */
319 avc->f.states &= ~CMAPPED;
320 avc->f.states &= ~CDirty; /* Turn it off */
321 if (avc->f.states & CUnlinked) {
322 if (CheckLock(&afs_xvcache) || CheckLock(&afs_xdcache)) {
323 avc->f.states |= CUnlinkedDel;
326 afs_remunlink(avc, 1); /* ignore any return code */
333 * Allocate a callback return structure from the
334 * free list and return it.
336 * Environment: The alloc and free routines are both called with the afs_xvcb lock
337 * held, so we don't have to worry about blocking in osi_Alloc.
339 * \return The allocated afs_cbr.
341 static struct afs_cbr *afs_cbrSpace = 0;
342 /* if alloc limit below changes, fix me! */
343 static struct afs_cbr *afs_cbrHeads[16];
350 while (!afs_cbrSpace) {
351 if (afs_stats_cmperf.CallBackAlloced >= sizeof(afs_cbrHeads)/sizeof(afs_cbrHeads[0])) {
352 /* don't allocate more than 16 * AFS_NCBRS for now */
354 afs_stats_cmperf.CallBackFlushes++;
357 tsp = afs_osi_Alloc(AFS_NCBRS * sizeof(struct afs_cbr));
358 osi_Assert(tsp != NULL);
359 for (i = 0; i < AFS_NCBRS - 1; i++) {
360 tsp[i].next = &tsp[i + 1];
362 tsp[AFS_NCBRS - 1].next = 0;
364 afs_cbrHeads[afs_stats_cmperf.CallBackAlloced] = tsp;
365 afs_stats_cmperf.CallBackAlloced++;
369 afs_cbrSpace = tsp->next;
374 * Free a callback return structure, removing it from all lists.
376 * Environment: the xvcb lock is held over these calls.
378 * \param asp The address of the structure to free.
383 afs_FreeCBR(struct afs_cbr *asp)
385 *(asp->pprev) = asp->next;
387 asp->next->pprev = asp->pprev;
389 *(asp->hash_pprev) = asp->hash_next;
391 asp->hash_next->hash_pprev = asp->hash_pprev;
393 asp->next = afs_cbrSpace;
399 FlushAllVCBs(int nconns, struct rx_connection **rxconns,
400 struct afs_conn **conns)
405 results = afs_osi_Alloc(nconns * sizeof (afs_int32));
406 osi_Assert(results != NULL);
409 multi_Rx(rxconns,nconns)
411 multi_RXAFS_GiveUpAllCallBacks();
412 results[multi_i] = multi_error;
417 * Freeing the CBR will unlink it from the server's CBR list
418 * do it here, not in the loop, because a dynamic CBR will call
419 * into the memory management routines.
421 for ( i = 0 ; i < nconns ; i++ ) {
422 if (results[i] == 0) {
423 /* Unchain all of them */
424 while (conns[i]->parent->srvr->server->cbrs)
425 afs_FreeCBR(conns[i]->parent->srvr->server->cbrs);
428 afs_osi_Free(results, nconns * sizeof(afs_int32));
432 * Flush all queued callbacks to all servers.
434 * Environment: holds xvcb lock over RPC to guard against race conditions
435 * when a new callback is granted for the same file later on.
437 * \return 0 for success.
440 afs_FlushVCBs(afs_int32 lockit)
442 struct AFSFid *tfids;
443 struct AFSCallBack callBacks[1];
444 struct AFSCBFids fidArray;
445 struct AFSCBs cbArray;
447 struct afs_cbr *tcbrp;
451 struct vrequest *treq = NULL;
453 int safety1, safety2, safety3;
456 if (AFS_IS_DISCONNECTED)
459 if ((code = afs_CreateReq(&treq, afs_osi_credp)))
461 treq->flags |= O_NONBLOCK;
462 tfids = afs_osi_Alloc(sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
463 osi_Assert(tfids != NULL);
466 ObtainWriteLock(&afs_xvcb, 273);
469 * First, attempt a multi across everything, all addresses
470 * for all servers we know of.
474 afs_LoopServers(AFS_LS_ALL, NULL, 0, FlushAllVCBs, NULL);
476 ObtainReadLock(&afs_xserver);
477 for (i = 0; i < NSERVERS; i++) {
478 for (safety1 = 0, tsp = afs_servers[i];
479 tsp && safety1 < afs_totalServers + 10;
480 tsp = tsp->next, safety1++) {
482 if (tsp->cbrs == (struct afs_cbr *)0)
485 /* otherwise, grab a block of AFS_MAXCBRSCALL from the list
486 * and make an RPC, over and over again.
488 tcount = 0; /* number found so far */
489 for (safety2 = 0; safety2 < afs_cacheStats; safety2++) {
490 if (tcount >= AFS_MAXCBRSCALL || !tsp->cbrs) {
491 struct rx_connection *rxconn;
492 /* if buffer is full, or we've queued all we're going
493 * to from this server, we should flush out the
496 fidArray.AFSCBFids_len = tcount;
497 fidArray.AFSCBFids_val = (struct AFSFid *)tfids;
498 cbArray.AFSCBs_len = 1;
499 cbArray.AFSCBs_val = callBacks;
500 memset(&callBacks[0], 0, sizeof(callBacks[0]));
501 callBacks[0].CallBackType = CB_EXCLUSIVE;
502 for (safety3 = 0; safety3 < AFS_MAXHOSTS * 2; safety3++) {
503 tc = afs_ConnByHost(tsp, tsp->cell->fsport,
504 tsp->cell->cellNum, treq, 0,
505 SHARED_LOCK, 0, &rxconn);
508 (AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS);
511 RXAFS_GiveUpCallBacks(rxconn, &fidArray,
518 (tc, rxconn, code, 0, treq,
519 AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS, SHARED_LOCK,
524 /* ignore return code, since callbacks may have
525 * been returned anyway, we shouldn't leave them
526 * around to be returned again.
528 * Next, see if we are done with this server, and if so,
529 * break to deal with the next one.
535 /* if to flush full buffer */
536 /* if we make it here, we have an entry at the head of cbrs,
537 * which we should copy to the file ID array and then free.
540 tfids[tcount++] = tcbrp->fid;
542 /* Freeing the CBR will unlink it from the server's CBR list */
544 } /* while loop for this one server */
545 if (safety2 > afs_cacheStats) {
546 afs_warn("possible internal error afs_flushVCBs (%d)\n",
549 } /* for loop for this hash chain */
550 } /* loop through all hash chains */
551 if (safety1 > afs_totalServers + 2) {
553 ("AFS internal error (afs_flushVCBs) (%d > %d), continuing...\n",
554 safety1, afs_totalServers + 2);
556 osi_Panic("afs_flushVCBS safety1");
559 ReleaseReadLock(&afs_xserver);
561 ReleaseWriteLock(&afs_xvcb);
562 afs_osi_Free(tfids, sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
563 afs_DestroyReq(treq);
568 * Queue a callback on the given fid.
571 * Locks the xvcb lock.
572 * Called when the xvcache lock is already held.
573 * RACE: afs_xvcache may be dropped and reacquired
575 * \param avc vcache entry
576 * \param slep Set to 1 if we dropped afs_xvcache
577 * \return 1 if queued, 0 otherwise
581 afs_QueueVCB(struct vcache *avc, int *slept)
585 struct afs_cbr *tcbp;
588 AFS_STATCNT(afs_QueueVCB);
590 ObtainWriteLock(&afs_xvcb, 274);
592 /* we can't really give back callbacks on RO files, since the
593 * server only tracks them on a per-volume basis, and we don't
594 * know whether we still have some other files from the same
596 if (!((avc->f.states & CRO) == 0 && avc->callback)) {
600 /* The callback is really just a struct server ptr. */
601 tsp = (struct server *)(avc->callback);
604 /* If we don't have CBR space, AllocCBR may block or hit the net for
605 * clearing up CBRs. Hitting the net may involve a fileserver
606 * needing to contact us, so we must drop xvcache so we don't block
607 * those requests from going through. */
608 reacquire = *slept = 1;
609 ReleaseWriteLock(&afs_xvcache);
612 /* we now have a pointer to the server, so we just allocate
613 * a queue entry and queue it.
615 tcbp = afs_AllocCBR();
616 tcbp->fid = avc->f.fid.Fid;
618 tcbp->next = tsp->cbrs;
620 tsp->cbrs->pprev = &tcbp->next;
623 tcbp->pprev = &tsp->cbrs;
625 afs_InsertHashCBR(tcbp);
629 /* now release locks and return */
630 ReleaseWriteLock(&afs_xvcb);
633 /* make sure this is after dropping xvcb, for locking order */
634 ObtainWriteLock(&afs_xvcache, 279);
641 * Remove a queued callback for a given Fid.
644 * Locks xvcb and xserver locks.
645 * Typically called with xdcache, xvcache and/or individual vcache
648 * \param afid The fid we want cleansed of queued callbacks.
653 afs_RemoveVCB(struct VenusFid *afid)
656 struct afs_cbr *cbr, *ncbr;
658 AFS_STATCNT(afs_RemoveVCB);
659 ObtainWriteLock(&afs_xvcb, 275);
661 slot = afs_HashCBRFid(&afid->Fid);
662 ncbr = afs_cbrHashT[slot];
666 ncbr = cbr->hash_next;
668 if (afid->Fid.Volume == cbr->fid.Volume &&
669 afid->Fid.Vnode == cbr->fid.Vnode &&
670 afid->Fid.Unique == cbr->fid.Unique) {
675 ReleaseWriteLock(&afs_xvcb);
679 afs_FlushReclaimedVcaches(void)
681 #if !defined(AFS_LINUX22_ENV)
684 struct vcache *tmpReclaimedVCList = NULL;
686 ObtainWriteLock(&afs_xvreclaim, 76);
687 while (ReclaimedVCList) {
688 tvc = ReclaimedVCList; /* take from free list */
689 ReclaimedVCList = tvc->nextfree;
690 tvc->nextfree = NULL;
691 code = afs_FlushVCache(tvc, &fv_slept);
693 /* Ok, so, if we got code != 0, uh, wtf do we do? */
694 /* Probably, build a temporary list and then put all back when we
695 get to the end of the list */
696 /* This is actually really crappy, but we need to not leak these.
697 We probably need a way to be smarter about this. */
698 tvc->nextfree = tmpReclaimedVCList;
699 tmpReclaimedVCList = tvc;
700 /* printf("Reclaim list flush %lx failed: %d\n", (unsigned long) tvc, code); */
702 if (tvc->f.states & (CVInit
703 # ifdef AFS_DARWIN80_ENV
707 tvc->f.states &= ~(CVInit
708 # ifdef AFS_DARWIN80_ENV
712 afs_osi_Wakeup(&tvc->f.states);
715 if (tmpReclaimedVCList)
716 ReclaimedVCList = tmpReclaimedVCList;
718 ReleaseWriteLock(&afs_xvreclaim);
723 afs_PostPopulateVCache(struct vcache *avc, struct VenusFid *afid, int seq)
726 * The proper value for mvstat (for root fids) is setup by the caller.
728 avc->mvstat = AFS_MVSTAT_FILE;
729 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
730 avc->mvstat = AFS_MVSTAT_ROOT;
732 if (afs_globalVFS == 0)
733 osi_Panic("afs globalvfs");
735 osi_PostPopulateVCache(avc);
738 osi_dnlc_purgedp(avc); /* this may be overkill */
739 memset(&(avc->callsort), 0, sizeof(struct afs_q));
741 avc->f.states &=~ CVInit;
743 avc->f.states |= CBulkFetching;
744 avc->f.m.Length = seq;
746 afs_osi_Wakeup(&avc->f.states);
750 afs_ShakeLooseVCaches(afs_int32 anumber)
755 struct afs_q *tq, *uq;
756 int fv_slept, defersleep = 0;
758 afs_int32 target = anumber;
765 for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
768 if (tvc->f.states & CVFlushed) {
769 refpanic("CVFlushed on VLRU");
770 } else if (i++ > limit) {
771 afs_warn("afs_ShakeLooseVCaches: i %d limit %d afs_vcount %d afs_maxvcount %d\n",
772 (int)i, limit, (int)afs_vcount, (int)afs_maxvcount);
773 refpanic("Found too many AFS vnodes on VLRU (VLRU cycle?)");
774 } else if (QNext(uq) != tq) {
775 refpanic("VLRU inconsistent");
776 } else if (tvc->f.states & CVInit) {
781 evicted = osi_TryEvictVCache(tvc, &fv_slept, defersleep);
791 * This vcache was busy and we slept while trying to evict it.
792 * Move this busy vcache to the head of the VLRU so vcaches
793 * following this busy vcache can be evicted during the retry.
795 QRemove(&tvc->vlruq);
796 QAdd(&VLRU, &tvc->vlruq);
798 goto retry; /* start over - may have raced. */
801 if (anumber && !defersleep) {
808 if (!afsd_dynamic_vcaches && anumber == target) {
809 afs_warn("afs_ShakeLooseVCaches: warning none freed, using %d of %d\n",
810 afs_vcount, afs_maxvcount);
816 /* Alloc new vnode. */
818 static struct vcache *
819 afs_AllocVCache(void)
823 tvc = osi_NewVnode();
828 if (afsd_dynamic_vcaches && afs_maxvcount < afs_vcount) {
829 afs_maxvcount = afs_vcount;
830 /*printf("peak vnodes: %d\n", afs_maxvcount);*/
833 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
835 /* If we create a new inode, we either give it a new slot number,
836 * or if one's available, use a slot number from the slot free list
838 if (afs_freeSlotList != NULL) {
839 struct afs_slotlist *tmp;
841 tvc->diskSlot = afs_freeSlotList->slot;
842 tmp = afs_freeSlotList;
843 afs_freeSlotList = tmp->next;
844 afs_osi_Free(tmp, sizeof(struct afs_slotlist));
846 tvc->diskSlot = afs_nextVcacheSlot++;
852 /* Pre populate a newly allocated vcache. On platforms where the actual
853 * vnode is attached to the vcache, this function is called before attachment,
854 * therefore it cannot perform any actions on the vnode itself */
857 afs_PrePopulateVCache(struct vcache *avc, struct VenusFid *afid,
858 struct server *serverp) {
862 slot = avc->diskSlot;
864 osi_PrePopulateVCache(avc);
866 avc->diskSlot = slot;
867 QZero(&avc->metadirty);
869 AFS_RWLOCK_INIT(&avc->lock, "vcache lock");
871 memset(&avc->mvid, 0, sizeof(avc->mvid));
872 avc->linkData = NULL;
875 avc->execsOrWriters = 0;
877 avc->f.states = CVInit;
878 avc->last_looker = 0;
880 avc->asynchrony = -1;
884 avc->f.truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
886 afs_SetDataVersion(avc, &zero); /* in case we copy it into flushDV */
888 avc->callback = serverp; /* to minimize chance that clear
891 #if defined(AFS_CACHE_BYPASS)
892 avc->cachingStates = 0;
893 avc->cachingTransitions = 0;
898 afs_FlushAllVCaches(void)
901 struct vcache *tvc, *nvc;
903 ObtainWriteLock(&afs_xvcache, 867);
906 for (i = 0; i < VCSIZE; i++) {
907 for (tvc = afs_vhashT[i]; tvc; tvc = nvc) {
911 if (afs_FlushVCache(tvc, &slept)) {
912 afs_warn("Failed to flush vcache 0x%lx\n", (unsigned long)(uintptrsz)tvc);
920 ReleaseWriteLock(&afs_xvcache);
924 * This routine is responsible for allocating a new cache entry
925 * from the free list. It formats the cache entry and inserts it
926 * into the appropriate hash tables. It must be called with
927 * afs_xvcache write-locked so as to prevent several processes from
928 * trying to create a new cache entry simultaneously.
930 * LOCK: afs_NewVCache afs_xvcache W
932 * \param afid The file id of the file whose cache entry is being created.
934 * \return The new vcache struct.
937 static_inline struct vcache *
938 afs_NewVCache_int(struct VenusFid *afid, struct server *serverp, int seq)
942 afs_int32 anumber = VCACHE_FREE;
944 AFS_STATCNT(afs_NewVCache);
946 afs_FlushReclaimedVcaches();
948 #if defined(AFS_LINUX22_ENV)
949 if(!afsd_dynamic_vcaches && afs_vcount >= afs_maxvcount) {
950 afs_ShakeLooseVCaches(anumber);
951 if (afs_vcount >= afs_maxvcount) {
952 afs_warn("afs_NewVCache - none freed\n");
956 tvc = afs_AllocVCache();
957 #else /* AFS_LINUX22_ENV */
958 /* pull out a free cache entry */
960 afs_ShakeLooseVCaches(anumber);
964 tvc = afs_AllocVCache();
966 tvc = freeVCList; /* take from free list */
967 freeVCList = tvc->nextfree;
968 tvc->nextfree = NULL;
969 afs_vcount++; /* balanced by FlushVCache */
970 } /* end of if (!freeVCList) */
972 #endif /* AFS_LINUX22_ENV */
974 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
976 panic("afs_NewVCache(): free vcache with vnode attached");
979 /* Populate the vcache with as much as we can. */
980 afs_PrePopulateVCache(tvc, afid, serverp);
982 /* Thread the vcache onto the VLRU */
987 tvc->hnext = afs_vhashT[i];
989 QAdd(&afs_vhashTV[j], &tvc->vhashq);
991 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
992 refpanic("NewVCache VLRU inconsistent");
994 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
995 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
996 refpanic("NewVCache VLRU inconsistent2");
998 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
999 refpanic("NewVCache VLRU inconsistent3");
1001 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
1002 refpanic("NewVCache VLRU inconsistent4");
1006 /* it should now be safe to drop the xvcache lock - so attach an inode
1007 * to this vcache, where necessary */
1008 osi_AttachVnode(tvc, seq);
1010 /* Get a reference count to hold this vcache for the VLRUQ. Note that
1011 * we have to do this after attaching the vnode, because the reference
1012 * count may be held in the vnode itself */
1014 #if defined(AFS_LINUX22_ENV)
1015 /* Hold it for the LRU (should make count 2) */
1016 osi_Assert(osi_vnhold(tvc) == 0);
1017 #elif !(defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV))
1018 VREFCOUNT_SET(tvc, 1); /* us */
1021 #if defined (AFS_FBSD_ENV)
1022 if (tvc->f.states & CVInit)
1024 afs_PostPopulateVCache(tvc, afid, seq);
1027 } /*afs_NewVCache */
1031 afs_NewVCache(struct VenusFid *afid, struct server *serverp)
1033 return afs_NewVCache_int(afid, serverp, 0);
1037 afs_NewBulkVCache(struct VenusFid *afid, struct server *serverp, int seq)
1039 return afs_NewVCache_int(afid, serverp, seq);
1045 * LOCK: afs_FlushActiveVcaches afs_xvcache N
1047 * \param doflocks : Do we handle flocks?
1050 afs_FlushActiveVcaches(afs_int32 doflocks)
1054 struct afs_conn *tc;
1056 afs_ucred_t *cred = NULL;
1057 struct vrequest *treq = NULL;
1058 struct AFSVolSync tsync;
1061 AFS_STATCNT(afs_FlushActiveVcaches);
1063 code = afs_CreateReq(&treq, afs_osi_credp);
1065 afs_warn("unable to alloc treq\n");
1069 ObtainReadLock(&afs_xvcache);
1070 for (i = 0; i < VCSIZE; i++) {
1071 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1072 if (tvc->f.states & CVInit) continue;
1073 #ifdef AFS_DARWIN80_ENV
1074 if (tvc->f.states & CDeadVnode &&
1075 (tvc->f.states & (CCore|CUnlinkedDel) ||
1076 tvc->flockCount)) panic("Dead vnode has core/unlinkedel/flock");
1078 if (doflocks && tvc->flockCount != 0) {
1079 struct rx_connection *rxconn;
1080 if (osi_vnhold(tvc) != 0) {
1083 /* if this entry has an flock, send a keep-alive call out */
1084 ReleaseReadLock(&afs_xvcache);
1085 ObtainWriteLock(&tvc->lock, 51);
1087 code = afs_InitReq(treq, afs_osi_credp);
1090 break; /* shutting down: do not try to extend the lock */
1092 treq->flags |= O_NONBLOCK;
1094 tc = afs_Conn(&tvc->f.fid, treq, SHARED_LOCK, &rxconn);
1096 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
1099 RXAFS_ExtendLock(rxconn,
1100 (struct AFSFid *)&tvc->f.fid.Fid,
1106 } while (afs_Analyze
1107 (tc, rxconn, code, &tvc->f.fid, treq,
1108 AFS_STATS_FS_RPCIDX_EXTENDLOCK, SHARED_LOCK, NULL));
1110 ReleaseWriteLock(&tvc->lock);
1111 #ifdef AFS_DARWIN80_ENV
1113 ObtainReadLock(&afs_xvcache);
1115 ObtainReadLock(&afs_xvcache);
1120 if ((tvc->f.states & CCore) || (tvc->f.states & CUnlinkedDel)) {
1122 * Don't let it evaporate in case someone else is in
1123 * this code. Also, drop the afs_xvcache lock while
1124 * getting vcache locks.
1126 if (osi_vnhold(tvc) != 0) {
1129 ReleaseReadLock(&afs_xvcache);
1130 #if defined(AFS_SGI_ENV)
1132 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1134 osi_Assert(VREFCOUNT_GT(tvc,0));
1135 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1137 ObtainWriteLock(&tvc->lock, 52);
1138 if (tvc->f.states & CCore) {
1139 tvc->f.states &= ~CCore;
1140 /* XXXX Find better place-holder for cred XXXX */
1141 cred = (afs_ucred_t *)tvc->linkData;
1142 tvc->linkData = NULL; /* XXX */
1143 code = afs_InitReq(treq, cred);
1144 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1145 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
1146 tvc->execsOrWriters);
1147 if (!code) { /* avoid store when shutting down */
1148 code = afs_StoreOnLastReference(tvc, treq);
1150 ReleaseWriteLock(&tvc->lock);
1151 hzero(tvc->flushDV);
1154 if (code && code != VNOVNODE) {
1155 afs_StoreWarn(code, tvc->f.fid.Fid.Volume,
1156 /* /dev/console */ 1);
1158 } else if (tvc->f.states & CUnlinkedDel) {
1162 ReleaseWriteLock(&tvc->lock);
1163 #if defined(AFS_SGI_ENV)
1164 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1166 afs_remunlink(tvc, 0);
1167 #if defined(AFS_SGI_ENV)
1168 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1171 /* lost (or won, perhaps) the race condition */
1172 ReleaseWriteLock(&tvc->lock);
1174 #if defined(AFS_SGI_ENV)
1175 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1177 #ifdef AFS_DARWIN80_ENV
1180 AFS_RELE(AFSTOV(tvc));
1181 /* Matches write code setting CCore flag */
1184 ObtainReadLock(&afs_xvcache);
1186 ObtainReadLock(&afs_xvcache);
1189 AFS_RELE(AFSTOV(tvc));
1190 /* Matches write code setting CCore flag */
1197 ReleaseReadLock(&afs_xvcache);
1198 afs_DestroyReq(treq);
1204 * Make sure a cache entry is up-to-date status-wise.
1206 * NOTE: everywhere that calls this can potentially be sped up
1207 * by checking CStatd first, and avoiding doing the InitReq
1208 * if this is up-to-date.
1210 * Anymore, the only places that call this KNOW already that the
1211 * vcache is not up-to-date, so we don't screw around.
1213 * \param avc : Ptr to vcache entry to verify.
1219 * Make sure a cache entry is up-to-date status-wise.
1221 * NOTE: everywhere that calls this can potentially be sped up
1222 * by checking CStatd first, and avoiding doing the InitReq
1223 * if this is up-to-date.
1225 * Anymore, the only places that call this KNOW already that the
1226 * vcache is not up-to-date, so we don't screw around.
1228 * \param avc Pointer to vcache entry to verify.
1231 * \return 0 for success or other error codes.
1234 afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1238 AFS_STATCNT(afs_VerifyVCache);
1240 /* otherwise we must fetch the status info */
1242 ObtainWriteLock(&avc->lock, 53);
1243 if (avc->f.states & CStatd) {
1244 ReleaseWriteLock(&avc->lock);
1247 afs_StaleVCacheFlags(avc, AFS_STALEVC_FILENAME | AFS_STALEVC_CLEARCB,
1249 ReleaseWriteLock(&avc->lock);
1251 /* fetch the status info */
1252 tvc = afs_GetVCache(&avc->f.fid, areq);
1255 /* Put it back; caller has already incremented vrefCount */
1259 } /*afs_VerifyVCache */
1263 * Simple copy of stat info into cache.
1265 * Callers:as of 1992-04-29, only called by WriteVCache
1267 * \param avc Ptr to vcache entry involved.
1268 * \param astat Ptr to stat info to copy.
1272 afs_SimpleVStat(struct vcache *avc,
1273 struct AFSFetchStatus *astat, struct vrequest *areq)
1276 AFS_STATCNT(afs_SimpleVStat);
1278 #ifdef AFS_64BIT_CLIENT
1279 FillInt64(length, astat->Length_hi, astat->Length);
1280 #else /* AFS_64BIT_CLIENT */
1281 length = astat->Length;
1282 #endif /* AFS_64BIT_CLIENT */
1284 #if defined(AFS_SGI_ENV)
1285 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1286 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1287 osi_Assert((valusema(&avc->vc_rwlock) <= 0)
1288 && (OSI_GET_LOCKID() == avc->vc_rwlockid));
1289 if (length < avc->f.m.Length) {
1290 vnode_t *vp = (vnode_t *) avc;
1292 osi_Assert(WriteLocked(&avc->lock));
1293 ReleaseWriteLock(&avc->lock);
1295 PTOSSVP(vp, (off_t) length, (off_t) MAXLONG);
1297 ObtainWriteLock(&avc->lock, 67);
1302 if (!afs_DirtyPages(avc)) {
1303 /* if actively writing the file, don't fetch over this value */
1304 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT, ICL_TYPE_POINTER, avc,
1305 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1306 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1307 avc->f.m.Length = length;
1308 avc->f.m.Date = astat->ClientModTime;
1310 avc->f.m.Owner = astat->Owner;
1311 avc->f.m.Group = astat->Group;
1312 avc->f.m.Mode = astat->UnixModeBits;
1313 if (vType(avc) == VREG) {
1314 avc->f.m.Mode |= S_IFREG;
1315 } else if (vType(avc) == VDIR) {
1316 avc->f.m.Mode |= S_IFDIR;
1317 } else if (vType(avc) == VLNK) {
1318 avc->f.m.Mode |= S_IFLNK;
1319 if ((avc->f.m.Mode & 0111) == 0)
1320 avc->mvstat = AFS_MVSTAT_MTPT;
1322 if (avc->f.states & CForeign) {
1323 struct axscache *ac;
1324 avc->f.anyAccess = astat->AnonymousAccess;
1326 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1328 * Caller has at least one bit not covered by anonymous, and
1329 * thus may have interesting rights.
1331 * HOWEVER, this is a really bad idea, because any access query
1332 * for bits which aren't covered by anonymous, on behalf of a user
1333 * who doesn't have any special rights, will result in an answer of
1334 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1335 * It's an especially bad idea under Ultrix, since (due to the lack of
1336 * a proper access() call) it must perform several afs_access() calls
1337 * in order to create magic mode bits that vary according to who makes
1338 * the call. In other words, _every_ stat() generates a test for
1341 #endif /* badidea */
1342 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1343 ac->axess = astat->CallerAccess;
1344 else /* not found, add a new one if possible */
1345 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1348 } /*afs_SimpleVStat */
1352 * Store the status info *only* back to the server for a
1355 * Environment: Must be called with a shared lock held on the vnode.
1357 * \param avc Ptr to the vcache entry.
1358 * \param astatus Ptr to the status info to store.
1359 * \param areq Ptr to the associated vrequest.
1361 * \return Operation status.
1365 afs_WriteVCache(struct vcache *avc,
1366 struct AFSStoreStatus *astatus,
1367 struct vrequest *areq)
1370 struct afs_conn *tc;
1371 struct AFSFetchStatus OutStatus;
1372 struct AFSVolSync tsync;
1373 struct rx_connection *rxconn;
1375 AFS_STATCNT(afs_WriteVCache);
1376 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1377 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
1379 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK, &rxconn);
1381 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1384 RXAFS_StoreStatus(rxconn, (struct AFSFid *)&avc->f.fid.Fid,
1385 astatus, &OutStatus, &tsync);
1390 } while (afs_Analyze
1391 (tc, rxconn, code, &avc->f.fid, areq, AFS_STATS_FS_RPCIDX_STORESTATUS,
1392 SHARED_LOCK, NULL));
1394 UpgradeSToWLock(&avc->lock, 20);
1396 /* success, do the changes locally */
1397 afs_SimpleVStat(avc, &OutStatus, areq);
1399 * Update the date, too. SimpleVStat didn't do this, since
1400 * it thought we were doing this after fetching new status
1401 * over a file being written.
1403 avc->f.m.Date = OutStatus.ClientModTime;
1405 /* failure, set up to check with server next time */
1406 afs_StaleVCacheFlags(avc, 0, CUnique);
1408 ConvertWToSLock(&avc->lock);
1411 } /*afs_WriteVCache */
1414 * Store status info only locally, set the proper disconnection flags
1415 * and add to dirty list.
1417 * \param avc The vcache to be written locally.
1418 * \param astatus Get attr fields from local store.
1419 * \param attrs This one is only of the vs_size.
1421 * \note Must be called with a shared lock on the vnode
1424 afs_WriteVCacheDiscon(struct vcache *avc,
1425 struct AFSStoreStatus *astatus,
1426 struct vattr *attrs)
1429 afs_int32 flags = 0;
1431 UpgradeSToWLock(&avc->lock, 700);
1433 if (!astatus->Mask) {
1439 /* Set attributes. */
1440 if (astatus->Mask & AFS_SETMODTIME) {
1441 avc->f.m.Date = astatus->ClientModTime;
1442 flags |= VDisconSetTime;
1445 if (astatus->Mask & AFS_SETOWNER) {
1446 /* printf("Not allowed yet. \n"); */
1447 /*avc->f.m.Owner = astatus->Owner;*/
1450 if (astatus->Mask & AFS_SETGROUP) {
1451 /* printf("Not allowed yet. \n"); */
1452 /*avc->f.m.Group = astatus->Group;*/
1455 if (astatus->Mask & AFS_SETMODE) {
1456 avc->f.m.Mode = astatus->UnixModeBits;
1458 flags |= VDisconSetMode;
1459 } /* if(astatus.Mask & AFS_SETMODE) */
1461 } /* if (!astatus->Mask) */
1463 if (attrs->va_size > 0) {
1464 /* XXX: Do I need more checks? */
1465 /* Truncation operation. */
1466 flags |= VDisconTrunc;
1470 afs_DisconAddDirty(avc, flags, 1);
1472 /* XXX: How about the rest of the fields? */
1474 ConvertWToSLock(&avc->lock);
1480 * Copy astat block into vcache info
1482 * \note This code may get dataversion and length out of sync if the file has
1483 * been modified. This is less than ideal. I haven't thought about it sufficiently
1484 * to be certain that it is adequate.
1486 * \note Environment: Must be called under a write lock
1488 * \param avc Ptr to vcache entry.
1489 * \param astat Ptr to stat block to copy in.
1490 * \param areq Ptr to associated request.
1493 afs_ProcessFS(struct vcache *avc,
1494 struct AFSFetchStatus *astat, struct vrequest *areq)
1498 AFS_STATCNT(afs_ProcessFS);
1500 #ifdef AFS_64BIT_CLIENT
1501 FillInt64(length, astat->Length_hi, astat->Length);
1502 #else /* AFS_64BIT_CLIENT */
1503 length = astat->Length;
1504 #endif /* AFS_64BIT_CLIENT */
1505 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1506 * number for each bulk status request. Under no circumstances
1507 * should afs_DoBulkStat store a sequence number if the new
1508 * length will be ignored when afs_ProcessFS is called with
1509 * new stats. If you change the following conditional then you
1510 * also need to change the conditional in afs_DoBulkStat. */
1512 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1513 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1515 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1517 /* if we're writing or mapping this file, don't fetch over these
1520 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1521 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1522 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1523 avc->f.m.Length = length;
1524 avc->f.m.Date = astat->ClientModTime;
1526 hset64(newDV, astat->dataVersionHigh, astat->DataVersion);
1527 afs_SetDataVersion(avc, &newDV);
1528 avc->f.m.Owner = astat->Owner;
1529 avc->f.m.Mode = astat->UnixModeBits;
1530 avc->f.m.Group = astat->Group;
1531 avc->f.m.LinkCount = astat->LinkCount;
1532 if (astat->FileType == File) {
1533 vSetType(avc, VREG);
1534 avc->f.m.Mode |= S_IFREG;
1535 } else if (astat->FileType == Directory) {
1536 vSetType(avc, VDIR);
1537 avc->f.m.Mode |= S_IFDIR;
1538 } else if (astat->FileType == SymbolicLink) {
1539 if (afs_fakestat_enable && (avc->f.m.Mode & 0111) == 0) {
1540 vSetType(avc, VDIR);
1541 avc->f.m.Mode |= S_IFDIR;
1543 vSetType(avc, VLNK);
1544 avc->f.m.Mode |= S_IFLNK;
1546 if ((avc->f.m.Mode & 0111) == 0) {
1547 avc->mvstat = AFS_MVSTAT_MTPT;
1550 avc->f.anyAccess = astat->AnonymousAccess;
1552 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1554 * Caller has at least one bit not covered by anonymous, and
1555 * thus may have interesting rights.
1557 * HOWEVER, this is a really bad idea, because any access query
1558 * for bits which aren't covered by anonymous, on behalf of a user
1559 * who doesn't have any special rights, will result in an answer of
1560 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1561 * It's an especially bad idea under Ultrix, since (due to the lack of
1562 * a proper access() call) it must perform several afs_access() calls
1563 * in order to create magic mode bits that vary according to who makes
1564 * the call. In other words, _every_ stat() generates a test for
1567 #endif /* badidea */
1569 struct axscache *ac;
1570 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1571 ac->axess = astat->CallerAccess;
1572 else /* not found, add a new one if possible */
1573 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1575 } /*afs_ProcessFS */
1579 * Get fid from server.
1582 * \param areq Request to be passed on.
1583 * \param name Name of ?? to lookup.
1584 * \param OutStatus Fetch status.
1589 * \return Success status of operation.
1592 afs_RemoteLookup(struct VenusFid *afid, struct vrequest *areq,
1593 char *name, struct VenusFid *nfid,
1594 struct AFSFetchStatus *OutStatusp,
1595 struct AFSCallBack *CallBackp, struct server **serverp,
1596 struct AFSVolSync *tsyncp)
1599 struct afs_conn *tc;
1600 struct rx_connection *rxconn;
1601 struct AFSFetchStatus OutDirStatus;
1604 name = ""; /* XXX */
1606 tc = afs_Conn(afid, areq, SHARED_LOCK, &rxconn);
1609 *serverp = tc->parent->srvr->server;
1610 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1613 RXAFS_Lookup(rxconn, (struct AFSFid *)&afid->Fid, name,
1614 (struct AFSFid *)&nfid->Fid, OutStatusp,
1615 &OutDirStatus, CallBackp, tsyncp);
1620 } while (afs_Analyze
1621 (tc, rxconn, code, afid, areq, AFS_STATS_FS_RPCIDX_XLOOKUP, SHARED_LOCK,
1631 * Given a file id and a vrequest structure, fetch the status
1632 * information associated with the file.
1634 * \param afid File ID.
1635 * \param areq Ptr to associated vrequest structure, specifying the
1636 * user whose authentication tokens will be used.
1638 * \note Environment:
1639 * The cache entry is returned with an increased vrefCount field.
1640 * The entry must be discarded by calling afs_PutVCache when you
1641 * are through using the pointer to the cache entry.
1643 * You should not hold any locks when calling this function, except
1644 * locks on other vcache entries. If you lock more than one vcache
1645 * entry simultaneously, you should lock them in this order:
1647 * 1. Lock all files first, then directories.
1648 * 2. Within a particular type, lock entries in Fid.Vnode order.
1650 * This locking hierarchy is convenient because it allows locking
1651 * of a parent dir cache entry, given a file (to check its access
1652 * control list). It also allows renames to be handled easily by
1653 * locking directories in a constant order.
1655 * \note NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1658 afs_GetVCache(struct VenusFid *afid, struct vrequest *areq)
1661 afs_int32 code, newvcache = 0;
1666 AFS_STATCNT(afs_GetVCache);
1668 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1672 ObtainSharedLock(&afs_xvcache, 5);
1674 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU | IS_SLOCK);
1676 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1677 ReleaseSharedLock(&afs_xvcache);
1678 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1683 osi_Assert((tvc->f.states & CVInit) == 0);
1684 /* If we are in readdir, return the vnode even if not statd */
1685 if ((tvc->f.states & CStatd) || afs_InReadDir(tvc)) {
1686 ReleaseSharedLock(&afs_xvcache);
1690 UpgradeSToWLock(&afs_xvcache, 21);
1692 /* no cache entry, better grab one */
1693 tvc = afs_NewVCache(afid, NULL);
1696 ConvertWToSLock(&afs_xvcache);
1699 ReleaseSharedLock(&afs_xvcache);
1703 afs_stats_cmperf.vcacheMisses++;
1706 ReleaseSharedLock(&afs_xvcache);
1708 ObtainWriteLock(&tvc->lock, 54);
1710 if (tvc->f.states & CStatd) {
1711 ReleaseWriteLock(&tvc->lock);
1714 #ifdef AFS_DARWIN80_ENV
1715 /* Darwin 8.0 only has bufs in nfs, so we shouldn't have to worry about them.
1718 # if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
1720 * XXX - I really don't like this. Should try to understand better.
1721 * It seems that sometimes, when we get called, we already hold the
1722 * lock on the vnode (e.g., from afs_getattr via afs_VerifyVCache).
1723 * We can't drop the vnode lock, because that could result in a race.
1724 * Sometimes, though, we get here and don't hold the vnode lock.
1725 * I hate code paths that sometimes hold locks and sometimes don't.
1726 * In any event, the dodge we use here is to check whether the vnode
1727 * is locked, and if it isn't, then we gain and drop it around the call
1728 * to vinvalbuf; otherwise, we leave it alone.
1731 struct vnode *vp = AFSTOV(tvc);
1734 # if defined(AFS_DARWIN_ENV)
1735 iheldthelock = VOP_ISLOCKED(vp);
1737 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, current_proc());
1738 /* this is messy. we can call fsync which will try to reobtain this */
1739 if (VTOAFS(vp) == tvc)
1740 ReleaseWriteLock(&tvc->lock);
1741 if (UBCINFOEXISTS(vp)) {
1742 vinvalbuf(vp, V_SAVE, &afs_osi_cred, current_proc(), PINOD, 0);
1744 if (VTOAFS(vp) == tvc)
1745 ObtainWriteLock(&tvc->lock, 954);
1747 VOP_UNLOCK(vp, LK_EXCLUSIVE, current_proc());
1748 # elif defined(AFS_FBSD_ENV)
1750 iheldthelock = VOP_ISLOCKED(vp);
1751 if (!iheldthelock) {
1752 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1754 vinvalbuf(vp, V_SAVE, PINOD, 0); /* changed late in 8.0-CURRENT */
1758 # elif defined(AFS_OBSD_ENV)
1759 iheldthelock = VOP_ISLOCKED(vp, curproc);
1761 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1762 uvm_vnp_uncache(vp);
1764 VOP_UNLOCK(vp, 0, curproc);
1765 # elif defined(AFS_NBSD40_ENV)
1766 iheldthelock = VOP_ISLOCKED(vp);
1767 if (!iheldthelock) {
1768 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1770 uvm_vnp_uncache(vp);
1778 afs_StaleVCacheFlags(tvc, AFS_STALEVC_NODNLC | AFS_STALEVC_CLEARCB,
1781 /* It is always appropriate to throw away all the access rights? */
1782 afs_FreeAllAxs(&(tvc->Access));
1783 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1785 if ((tvp->states & VForeign)) {
1787 tvc->f.states |= CForeign;
1788 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1789 && (tvp->rootUnique == afid->Fid.Unique)) {
1790 tvc->mvstat = AFS_MVSTAT_ROOT;
1793 if (tvp->states & VRO)
1794 tvc->f.states |= CRO;
1795 if (tvp->states & VBackup)
1796 tvc->f.states |= CBackup;
1797 /* now copy ".." entry back out of volume structure, if necessary */
1798 if (tvc->mvstat == AFS_MVSTAT_ROOT && tvp->dotdot.Fid.Volume != 0) {
1799 if (!tvc->mvid.parent)
1800 tvc->mvid.parent = (struct VenusFid *)
1801 osi_AllocSmallSpace(sizeof(struct VenusFid));
1802 *tvc->mvid.parent = tvp->dotdot;
1804 afs_PutVolume(tvp, READ_LOCK);
1808 afs_RemoveVCB(afid);
1810 struct AFSFetchStatus OutStatus;
1812 if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1813 afs_ProcessFS(tvc, &OutStatus, areq);
1814 tvc->f.states |= CStatd | CUnique;
1815 tvc->f.parent.vnode = OutStatus.ParentVnode;
1816 tvc->f.parent.unique = OutStatus.ParentUnique;
1820 if (AFS_IS_DISCONNECTED) {
1821 /* Nothing to do otherwise...*/
1823 /* printf("Network is down in afs_GetCache"); */
1825 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1827 /* For the NFS translator's benefit, make sure
1828 * non-directory vnodes always have their parent FID set
1829 * correctly, even when created as a result of decoding an
1830 * NFS filehandle. It would be nice to also do this for
1831 * directories, but we can't because the fileserver fills
1832 * in the FID of the directory itself instead of that of
1835 if (!code && OutStatus.FileType != Directory &&
1836 !tvc->f.parent.vnode) {
1837 tvc->f.parent.vnode = OutStatus.ParentVnode;
1838 tvc->f.parent.unique = OutStatus.ParentUnique;
1839 /* XXX - SXW - It's conceivable we should mark ourselves
1840 * as dirty again here, incase we've been raced
1841 * out of the FetchStatus call.
1848 ReleaseWriteLock(&tvc->lock);
1854 ReleaseWriteLock(&tvc->lock);
1857 } /*afs_GetVCache */
1862 * Lookup a vcache by fid. Look inside the cache first, if not
1863 * there, lookup the file on the server, and then get it's fresh
1871 * \return The found element or NULL.
1874 afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1875 struct vcache *adp, char *aname)
1877 afs_int32 code, now, newvcache = 0;
1878 struct VenusFid nfid;
1881 struct AFSFetchStatus OutStatus;
1882 struct AFSCallBack CallBack;
1883 struct AFSVolSync tsync;
1884 struct server *serverp = 0;
1888 AFS_STATCNT(afs_GetVCache);
1890 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1894 ObtainReadLock(&afs_xvcache);
1895 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */ );
1898 ReleaseReadLock(&afs_xvcache);
1900 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1901 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1905 ObtainReadLock(&tvc->lock);
1907 if (tvc->f.states & CStatd) {
1908 ReleaseReadLock(&tvc->lock);
1911 tvc->f.states &= ~CUnique;
1913 ReleaseReadLock(&tvc->lock);
1915 ObtainReadLock(&afs_xvcache);
1918 ReleaseReadLock(&afs_xvcache);
1920 /* lookup the file */
1923 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
1925 if (AFS_IS_DISCONNECTED) {
1926 /* printf("Network is down in afs_LookupVcache\n"); */
1930 afs_RemoteLookup(&adp->f.fid, areq, aname, &nfid, &OutStatus,
1931 &CallBack, &serverp, &tsync);
1933 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1937 ObtainSharedLock(&afs_xvcache, 6);
1938 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU | IS_SLOCK/* no xstats now */ );
1940 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1941 ReleaseSharedLock(&afs_xvcache);
1942 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1948 /* no cache entry, better grab one */
1949 UpgradeSToWLock(&afs_xvcache, 22);
1950 tvc = afs_NewVCache(&nfid, serverp);
1952 ConvertWToSLock(&afs_xvcache);
1955 ReleaseSharedLock(&afs_xvcache);
1960 ReleaseSharedLock(&afs_xvcache);
1961 ObtainWriteLock(&tvc->lock, 55);
1963 /* It is always appropriate to throw away all the access rights? */
1964 afs_FreeAllAxs(&(tvc->Access));
1965 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
1967 if ((tvp->states & VForeign)) {
1969 tvc->f.states |= CForeign;
1970 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1971 && (tvp->rootUnique == afid->Fid.Unique))
1972 tvc->mvstat = AFS_MVSTAT_ROOT;
1974 if (tvp->states & VRO)
1975 tvc->f.states |= CRO;
1976 if (tvp->states & VBackup)
1977 tvc->f.states |= CBackup;
1978 /* now copy ".." entry back out of volume structure, if necessary */
1979 if (tvc->mvstat == AFS_MVSTAT_ROOT && tvp->dotdot.Fid.Volume != 0) {
1980 if (!tvc->mvid.parent)
1981 tvc->mvid.parent = (struct VenusFid *)
1982 osi_AllocSmallSpace(sizeof(struct VenusFid));
1983 *tvc->mvid.parent = tvp->dotdot;
1988 afs_StaleVCacheFlags(tvc, 0, CUnique);
1990 afs_PutVolume(tvp, READ_LOCK);
1991 ReleaseWriteLock(&tvc->lock);
1996 ObtainWriteLock(&afs_xcbhash, 466);
1997 if (origCBs == afs_allCBs) {
1998 if (CallBack.ExpirationTime) {
1999 tvc->callback = serverp;
2000 tvc->cbExpires = CallBack.ExpirationTime + now;
2001 tvc->f.states |= CStatd | CUnique;
2002 tvc->f.states &= ~CBulkFetching;
2003 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
2004 } else if (tvc->f.states & CRO) {
2005 /* adapt gives us an hour. */
2006 tvc->cbExpires = 3600 + osi_Time();
2007 /*XXX*/ tvc->f.states |= CStatd | CUnique;
2008 tvc->f.states &= ~CBulkFetching;
2009 afs_QueueCallback(tvc, CBHash(3600), tvp);
2011 afs_StaleVCacheFlags(tvc,
2012 AFS_STALEVC_CBLOCKED | AFS_STALEVC_CLEARCB,
2016 afs_StaleVCacheFlags(tvc,
2017 AFS_STALEVC_CBLOCKED | AFS_STALEVC_CLEARCB,
2020 ReleaseWriteLock(&afs_xcbhash);
2022 afs_PutVolume(tvp, READ_LOCK);
2023 afs_ProcessFS(tvc, &OutStatus, areq);
2025 ReleaseWriteLock(&tvc->lock);
2031 afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
2032 struct volume *tvolp)
2034 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
2035 afs_int32 getNewFid = 0;
2037 struct VenusFid nfid;
2039 struct server *serverp = 0;
2040 struct AFSFetchStatus OutStatus;
2041 struct AFSCallBack CallBack;
2042 struct AFSVolSync tsync;
2044 #ifdef AFS_DARWIN80_ENV
2051 if (!tvolp->rootVnode || getNewFid) {
2052 struct VenusFid tfid;
2055 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2056 origCBs = afs_allCBs; /* ignore InitCallBackState */
2058 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2063 /* ReleaseReadLock(&tvolp->lock); */
2064 ObtainWriteLock(&tvolp->lock, 56);
2065 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
2066 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
2067 ReleaseWriteLock(&tvolp->lock);
2068 /* ObtainReadLock(&tvolp->lock);*/
2071 afid->Fid.Vnode = tvolp->rootVnode;
2072 afid->Fid.Unique = tvolp->rootUnique;
2076 ObtainSharedLock(&afs_xvcache, 7);
2078 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2079 if (!FidCmp(&(tvc->f.fid), afid)) {
2080 if (tvc->f.states & CVInit) {
2081 ReleaseSharedLock(&afs_xvcache);
2082 afs_osi_Sleep(&tvc->f.states);
2085 #ifdef AFS_DARWIN80_ENV
2086 if (tvc->f.states & CDeadVnode) {
2087 ReleaseSharedLock(&afs_xvcache);
2088 afs_osi_Sleep(&tvc->f.states);
2092 if (vnode_get(tvp)) /* this bumps ref count */
2094 if (vnode_ref(tvp)) {
2096 /* AFSTOV(tvc) may be NULL */
2102 if (osi_vnhold(tvc) != 0) {
2110 if (!haveStatus && (!tvc || !(tvc->f.states & CStatd))) {
2111 /* Mount point no longer stat'd or unknown. FID may have changed. */
2113 #ifdef AFS_DARWIN80_ENV
2114 ReleaseSharedLock(&afs_xvcache);
2117 vnode_put(AFSTOV(tvc));
2118 vnode_rele(AFSTOV(tvc));
2125 ReleaseSharedLock(&afs_xvcache);
2132 UpgradeSToWLock(&afs_xvcache, 23);
2133 /* no cache entry, better grab one */
2134 tvc = afs_NewVCache(afid, NULL);
2137 ReleaseWriteLock(&afs_xvcache);
2141 afs_stats_cmperf.vcacheMisses++;
2143 afs_stats_cmperf.vcacheHits++;
2144 UpgradeSToWLock(&afs_xvcache, 24);
2145 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2146 refpanic("GRVC VLRU inconsistent0");
2148 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2149 refpanic("GRVC VLRU inconsistent1");
2151 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2152 refpanic("GRVC VLRU inconsistent2");
2154 QRemove(&tvc->vlruq); /* move to lruq head */
2155 QAdd(&VLRU, &tvc->vlruq);
2156 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2157 refpanic("GRVC VLRU inconsistent3");
2159 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2160 refpanic("GRVC VLRU inconsistent4");
2162 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2163 refpanic("GRVC VLRU inconsistent5");
2168 ReleaseWriteLock(&afs_xvcache);
2170 if (tvc->f.states & CStatd) {
2174 ObtainReadLock(&tvc->lock);
2175 tvc->f.states &= ~CUnique;
2176 tvc->callback = NULL; /* redundant, perhaps */
2177 ReleaseReadLock(&tvc->lock);
2180 ObtainWriteLock(&tvc->lock, 57);
2182 /* It is always appropriate to throw away all the access rights? */
2183 afs_FreeAllAxs(&(tvc->Access));
2186 tvc->f.states |= CForeign;
2187 if (tvolp->states & VRO)
2188 tvc->f.states |= CRO;
2189 if (tvolp->states & VBackup)
2190 tvc->f.states |= CBackup;
2191 /* now copy ".." entry back out of volume structure, if necessary */
2192 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2193 && (tvolp->rootUnique == afid->Fid.Unique)) {
2194 tvc->mvstat = AFS_MVSTAT_ROOT;
2196 if (tvc->mvstat == AFS_MVSTAT_ROOT && tvolp->dotdot.Fid.Volume != 0) {
2197 if (!tvc->mvid.parent)
2198 tvc->mvid.parent = (struct VenusFid *)
2199 osi_AllocSmallSpace(sizeof(struct VenusFid));
2200 *tvc->mvid.parent = tvolp->dotdot;
2204 afs_RemoveVCB(afid);
2207 struct VenusFid tfid;
2210 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2211 origCBs = afs_allCBs; /* ignore InitCallBackState */
2213 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2218 afs_StaleVCacheFlags(tvc, AFS_STALEVC_CLEARCB, CUnique);
2219 ReleaseWriteLock(&tvc->lock);
2224 ObtainWriteLock(&afs_xcbhash, 468);
2225 if (origCBs == afs_allCBs) {
2226 tvc->f.states |= CTruth;
2227 tvc->callback = serverp;
2228 if (CallBack.ExpirationTime != 0) {
2229 tvc->cbExpires = CallBack.ExpirationTime + start;
2230 tvc->f.states |= CStatd;
2231 tvc->f.states &= ~CBulkFetching;
2232 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2233 } else if (tvc->f.states & CRO) {
2234 /* adapt gives us an hour. */
2235 tvc->cbExpires = 3600 + osi_Time();
2236 /*XXX*/ tvc->f.states |= CStatd;
2237 tvc->f.states &= ~CBulkFetching;
2238 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2241 afs_StaleVCacheFlags(tvc, AFS_STALEVC_CBLOCKED | AFS_STALEVC_CLEARCB,
2244 ReleaseWriteLock(&afs_xcbhash);
2245 afs_ProcessFS(tvc, &OutStatus, areq);
2247 ReleaseWriteLock(&tvc->lock);
2253 * Update callback status and (sometimes) attributes of a vnode.
2254 * Called after doing a fetch status RPC. Whilst disconnected, attributes
2255 * shouldn't be written to the vcache here.
2260 * \param Outsp Server status after rpc call.
2261 * \param acb Callback for this vnode.
2263 * \note The vcache must be write locked.
2266 afs_UpdateStatus(struct vcache *avc, struct VenusFid *afid,
2267 struct vrequest *areq, struct AFSFetchStatus *Outsp,
2268 struct AFSCallBack *acb, afs_uint32 start)
2270 struct volume *volp;
2273 /* Dont write status in vcache if resyncing after a disconnection. */
2274 afs_ProcessFS(avc, Outsp, areq);
2276 volp = afs_GetVolume(afid, areq, READ_LOCK);
2277 ObtainWriteLock(&afs_xcbhash, 469);
2278 avc->f.states |= CTruth;
2279 if (avc->callback /* check for race */ ) {
2280 if (acb->ExpirationTime != 0) {
2281 avc->cbExpires = acb->ExpirationTime + start;
2282 avc->f.states |= CStatd;
2283 avc->f.states &= ~CBulkFetching;
2284 afs_QueueCallback(avc, CBHash(acb->ExpirationTime), volp);
2285 } else if (avc->f.states & CRO) {
2286 /* ordinary callback on a read-only volume -- AFS 3.2 style */
2287 avc->cbExpires = 3600 + start;
2288 avc->f.states |= CStatd;
2289 avc->f.states &= ~CBulkFetching;
2290 afs_QueueCallback(avc, CBHash(3600), volp);
2292 afs_StaleVCacheFlags(avc,
2293 AFS_STALEVC_CBLOCKED | AFS_STALEVC_CLEARCB,
2297 afs_StaleVCacheFlags(avc, AFS_STALEVC_CBLOCKED | AFS_STALEVC_CLEARCB,
2300 ReleaseWriteLock(&afs_xcbhash);
2302 afs_PutVolume(volp, READ_LOCK);
2306 afs_BadFetchStatus(struct afs_conn *tc)
2308 int addr = ntohl(tc->parent->srvr->sa_ip);
2309 afs_warn("afs: Invalid AFSFetchStatus from server %u.%u.%u.%u\n",
2310 (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff,
2312 afs_warn("afs: This suggests the server may be sending bad data that "
2313 "can lead to availability issues or data corruption. The "
2314 "issue has been avoided for now, but it may not always be "
2315 "detectable. Please upgrade the server if possible.\n");
2319 * Check if a given AFSFetchStatus structure is sane.
2321 * @param[in] tc The server from which we received the status
2322 * @param[in] status The status we received
2324 * @return whether the given structure is valid or not
2325 * @retval 0 the structure is fine
2326 * @retval nonzero the structure looks like garbage; act as if we received
2327 * the returned error code from the server
2330 afs_CheckFetchStatus(struct afs_conn *tc, struct AFSFetchStatus *status)
2332 if (status->errorCode ||
2333 status->InterfaceVersion != 1 ||
2334 !(status->FileType > Invalid && status->FileType <= SymbolicLink) ||
2335 status->ParentVnode == 0 || status->ParentUnique == 0) {
2337 afs_warn("afs: FetchStatus ec %u iv %u ft %u pv %u pu %u\n",
2338 (unsigned)status->errorCode, (unsigned)status->InterfaceVersion,
2339 (unsigned)status->FileType, (unsigned)status->ParentVnode,
2340 (unsigned)status->ParentUnique);
2341 afs_BadFetchStatus(tc);
2349 * Must be called with avc write-locked
2350 * don't absolutely have to invalidate the hint unless the dv has
2351 * changed, but be sure to get it right else there will be consistency bugs.
2354 afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
2355 struct vrequest * areq, struct AFSFetchStatus * Outsp)
2358 afs_uint32 start = 0;
2359 struct afs_conn *tc;
2360 struct AFSCallBack CallBack;
2361 struct AFSVolSync tsync;
2362 struct rx_connection *rxconn;
2365 tc = afs_Conn(afid, areq, SHARED_LOCK, &rxconn);
2366 avc->dchint = NULL; /* invalidate hints */
2368 avc->callback = tc->parent->srvr->server;
2370 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2373 RXAFS_FetchStatus(rxconn, (struct AFSFid *)&afid->Fid, Outsp,
2380 code = afs_CheckFetchStatus(tc, Outsp);
2385 } while (afs_Analyze
2386 (tc, rxconn, code, afid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2387 SHARED_LOCK, NULL));
2390 afs_UpdateStatus(avc, afid, areq, Outsp, &CallBack, start);
2392 /* used to undo the local callback, but that's too extreme.
2393 * There are plenty of good reasons that fetchstatus might return
2394 * an error, such as EPERM. If we have the vnode cached, statd,
2395 * with callback, might as well keep track of the fact that we
2396 * don't have access...
2398 if (code == EPERM || code == EACCES) {
2399 struct axscache *ac;
2400 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2402 else /* not found, add a new one if possible */
2403 afs_AddAxs(avc->Access, areq->uid, 0);
2410 * Decrements the reference count on a cache entry.
2412 * \param avc Pointer to the cache entry to decrement.
2414 * \note Environment: Nothing interesting.
2417 afs_PutVCache(struct vcache *avc)
2419 AFS_STATCNT(afs_PutVCache);
2420 #ifdef AFS_DARWIN80_ENV
2421 vnode_put(AFSTOV(avc));
2425 * Can we use a read lock here?
2427 ObtainReadLock(&afs_xvcache);
2429 ReleaseReadLock(&afs_xvcache);
2431 } /*afs_PutVCache */
2435 * Reset a vcache entry, so local contents are ignored, and the
2436 * server will be reconsulted next time the vcache is used
2438 * \param avc Pointer to the cache entry to reset
2440 * \param skipdnlc skip the dnlc purge for this vnode
2442 * \note avc must be write locked on entry
2444 * \note The caller should purge the dnlc when skipdnlc is set.
2447 afs_ResetVCache(struct vcache *avc, afs_ucred_t *acred, afs_int32 skipdnlc)
2449 afs_stalevc_flags_t flags = 0;
2451 flags |= AFS_STALEVC_NODNLC;
2454 afs_StaleVCacheFlags(avc, flags, CDirty); /* next reference will re-stat */
2455 /* now find the disk cache entries */
2456 afs_TryToSmush(avc, acred, 1);
2457 if (avc->linkData && !(avc->f.states & CCore)) {
2458 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
2459 avc->linkData = NULL;
2464 * Sleepa when searching for a vcache. Releases all the pending locks,
2465 * sleeps then obtains the previously released locks.
2467 * \param vcache Enter sleep state.
2468 * \param flag Determines what locks to use.
2473 findvc_sleep(struct vcache *avc, int flag)
2475 if (flag & IS_SLOCK) {
2476 ReleaseSharedLock(&afs_xvcache);
2478 if (flag & IS_WLOCK) {
2479 ReleaseWriteLock(&afs_xvcache);
2481 ReleaseReadLock(&afs_xvcache);
2484 afs_osi_Sleep(&avc->f.states);
2485 if (flag & IS_SLOCK) {
2486 ObtainSharedLock(&afs_xvcache, 341);
2488 if (flag & IS_WLOCK) {
2489 ObtainWriteLock(&afs_xvcache, 343);
2491 ObtainReadLock(&afs_xvcache);
2497 * Add a reference on an existing vcache entry.
2499 * \param tvc Pointer to the vcache.
2501 * \note Environment: Must be called with at least one reference from
2502 * elsewhere on the vcache, even if that reference will be dropped.
2503 * The global lock is required.
2505 * \return 0 on success, -1 on failure.
2509 afs_RefVCache(struct vcache *tvc)
2511 #ifdef AFS_DARWIN80_ENV
2515 /* AFS_STATCNT(afs_RefVCache); */
2517 #ifdef AFS_DARWIN80_ENV
2521 if (vnode_ref(tvp)) {
2523 /* AFSTOV(tvc) may be NULL */
2529 if (osi_vnhold(tvc) != 0) {
2534 } /*afs_RefVCache */
2537 * Find a vcache entry given a fid.
2539 * \param afid Pointer to the fid whose cache entry we desire.
2540 * \param retry (SGI-specific) tell the caller to drop the lock on xvcache,
2541 * unlock the vnode, and try again.
2542 * \param flag Bit 1 to specify whether to compute hit statistics. Not
2543 * set if FindVCache is called as part of internal bookkeeping.
2545 * \note Environment: Must be called with the afs_xvcache lock at least held at
2546 * the read level. In order to do the VLRU adjustment, the xvcache lock
2547 * must be shared-- we upgrade it here.
2551 afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
2556 #ifdef AFS_DARWIN80_ENV
2557 struct vcache *deadvc = NULL, *livevc = NULL;
2561 AFS_STATCNT(afs_FindVCache);
2565 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2566 if (FidMatches(afid, tvc)) {
2567 if (tvc->f.states & CVInit) {
2568 findvc_sleep(tvc, flag);
2571 #ifdef AFS_DARWIN80_ENV
2572 if (tvc->f.states & CDeadVnode) {
2573 findvc_sleep(tvc, flag);
2581 /* should I have a read lock on the vnode here? */
2583 #if defined(AFS_DARWIN80_ENV)
2587 if (tvp && vnode_ref(tvp)) {
2589 /* AFSTOV(tvc) may be NULL */
2598 #elif defined(AFS_DARWIN_ENV)
2599 tvc->f.states |= CUBCinit;
2601 if (UBCINFOMISSING(AFSTOV(tvc)) ||
2602 UBCINFORECLAIMED(AFSTOV(tvc))) {
2603 ubc_info_init(AFSTOV(tvc));
2606 tvc->f.states &= ~CUBCinit;
2608 if (osi_vnhold(tvc) != 0) {
2615 * only move to front of vlru if we have proper vcache locking)
2617 if (flag & DO_VLRU) {
2618 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2619 refpanic("FindVC VLRU inconsistent1");
2621 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2622 refpanic("FindVC VLRU inconsistent1");
2624 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2625 refpanic("FindVC VLRU inconsistent2");
2627 UpgradeSToWLock(&afs_xvcache, 26);
2628 QRemove(&tvc->vlruq);
2629 QAdd(&VLRU, &tvc->vlruq);
2630 ConvertWToSLock(&afs_xvcache);
2631 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2632 refpanic("FindVC VLRU inconsistent1");
2634 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2635 refpanic("FindVC VLRU inconsistent2");
2637 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2638 refpanic("FindVC VLRU inconsistent3");
2644 if (flag & DO_STATS) {
2646 afs_stats_cmperf.vcacheHits++;
2648 afs_stats_cmperf.vcacheMisses++;
2649 if (afs_IsPrimaryCellNum(afid->Cell))
2650 afs_stats_cmperf.vlocalAccesses++;
2652 afs_stats_cmperf.vremoteAccesses++;
2655 } /*afs_FindVCache */
2658 * Find a vcache entry given a fid. Does a wildcard match on what we
2659 * have for the fid. If more than one entry, don't return anything.
2661 * \param avcp Fill in pointer if we found one and only one.
2662 * \param afid Pointer to the fid whose cache entry we desire.
2663 * \param retry (SGI-specific) tell the caller to drop the lock on xvcache,
2664 * unlock the vnode, and try again.
2665 * \param flags bit 1 to specify whether to compute hit statistics. Not
2666 * set if FindVCache is called as part of internal bookkeeping.
2668 * \note Environment: Must be called with the afs_xvcache lock at least held at
2669 * the read level. In order to do the VLRU adjustment, the xvcache lock
2670 * must be shared-- we upgrade it here.
2672 * \return Number of matches found.
2675 int afs_duplicate_nfs_fids = 0;
2678 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2682 afs_int32 count = 0;
2683 struct vcache *found_tvc = NULL;
2684 #ifdef AFS_DARWIN80_ENV
2688 AFS_STATCNT(afs_FindVCache);
2692 ObtainSharedLock(&afs_xvcache, 331);
2695 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2696 /* Match only on what we have.... */
2697 if (((tvc->f.fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2698 && (tvc->f.fid.Fid.Volume == afid->Fid.Volume)
2699 && ((tvc->f.fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2700 && (tvc->f.fid.Cell == afid->Cell)) {
2701 if (tvc->f.states & CVInit) {
2702 ReleaseSharedLock(&afs_xvcache);
2703 afs_osi_Sleep(&tvc->f.states);
2706 #ifdef AFS_DARWIN80_ENV
2707 if (tvc->f.states & CDeadVnode) {
2708 ReleaseSharedLock(&afs_xvcache);
2709 afs_osi_Sleep(&tvc->f.states);
2713 if (vnode_get(tvp)) {
2714 /* This vnode no longer exists. */
2717 if (vnode_ref(tvp)) {
2718 /* This vnode no longer exists. */
2720 /* AFSTOV(tvc) may be NULL */
2726 if (osi_vnhold(tvc) != 0) {
2729 #endif /* AFS_DARWIN80_ENV */
2733 afs_duplicate_nfs_fids++;
2734 #ifndef AFS_DARWIN80_ENV
2736 AFS_FAST_RELE(found_tvc);
2738 ReleaseSharedLock(&afs_xvcache);
2739 #ifdef AFS_DARWIN80_ENV
2740 /* Drop our reference counts. */
2741 vnode_put(AFSTOV(tvc));
2742 vnode_put(AFSTOV(found_tvc));
2751 /* should I have a read lock on the vnode here? */
2754 * We obtained the xvcache lock above.
2756 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2757 refpanic("FindVC VLRU inconsistent1");
2759 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2760 refpanic("FindVC VLRU inconsistent1");
2762 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2763 refpanic("FindVC VLRU inconsistent2");
2765 UpgradeSToWLock(&afs_xvcache, 568);
2766 QRemove(&tvc->vlruq);
2767 QAdd(&VLRU, &tvc->vlruq);
2768 ConvertWToSLock(&afs_xvcache);
2769 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2770 refpanic("FindVC VLRU inconsistent1");
2772 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2773 refpanic("FindVC VLRU inconsistent2");
2775 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2776 refpanic("FindVC VLRU inconsistent3");
2782 afs_stats_cmperf.vcacheHits++;
2784 afs_stats_cmperf.vcacheMisses++;
2785 if (afs_IsPrimaryCellNum(afid->Cell))
2786 afs_stats_cmperf.vlocalAccesses++;
2788 afs_stats_cmperf.vremoteAccesses++;
2790 *avcp = tvc; /* May be null */
2792 ReleaseSharedLock(&afs_xvcache);
2793 return (tvc ? 1 : 0);
2795 } /*afs_NFSFindVCache */
2801 * Initialize vcache related variables
2806 afs_vcacheInit(int astatSize)
2808 #if !defined(AFS_LINUX22_ENV)
2812 if (!afs_maxvcount) {
2813 afs_maxvcount = astatSize; /* no particular limit on linux? */
2815 #if !defined(AFS_LINUX22_ENV)
2819 AFS_RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2820 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2822 #if !defined(AFS_LINUX22_ENV)
2823 /* Allocate and thread the struct vcache entries */
2824 tvp = afs_osi_Alloc(astatSize * sizeof(struct vcache));
2825 osi_Assert(tvp != NULL);
2826 memset(tvp, 0, sizeof(struct vcache) * astatSize);
2828 Initial_freeVCList = tvp;
2829 freeVCList = &(tvp[0]);
2830 for (i = 0; i < astatSize - 1; i++) {
2831 tvp[i].nextfree = &(tvp[i + 1]);
2833 tvp[astatSize - 1].nextfree = NULL;
2834 # ifdef KERNEL_HAVE_PIN
2835 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
2839 #if defined(AFS_SGI_ENV)
2840 for (i = 0; i < astatSize; i++) {
2841 char name[METER_NAMSZ];
2842 struct vcache *tvc = &tvp[i];
2844 tvc->v.v_number = ++afsvnumbers;
2845 tvc->vc_rwlockid = OSI_NO_LOCKID;
2846 initnsema(&tvc->vc_rwlock, 1,
2847 makesname(name, "vrw", tvc->v.v_number));
2848 # ifndef AFS_SGI53_ENV
2849 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
2851 # ifndef AFS_SGI62_ENV
2852 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
2853 # endif /* AFS_SGI62_ENV */
2857 for(i = 0; i < VCSIZE; ++i)
2858 QInit(&afs_vhashTV[i]);
2865 shutdown_vcache(void)
2868 struct afs_cbr *tsp;
2870 * XXX We may potentially miss some of the vcaches because if when
2871 * there are no free vcache entries and all the vcache entries are active
2872 * ones then we allocate an additional one - admittedly we almost never
2877 struct afs_q *tq, *uq = NULL;
2879 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
2882 if (tvc->mvid.target_root) {
2883 osi_FreeSmallSpace(tvc->mvid.target_root);
2884 tvc->mvid.target_root = NULL;
2887 aix_gnode_rele(AFSTOV(tvc));
2889 if (tvc->linkData) {
2890 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
2895 * Also free the remaining ones in the Cache
2897 for (i = 0; i < VCSIZE; i++) {
2898 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2899 if (tvc->mvid.target_root) {
2900 osi_FreeSmallSpace(tvc->mvid.target_root);
2901 tvc->mvid.target_root = NULL;
2905 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
2906 # ifdef AFS_AIX32_ENV
2909 vms_delete(tvc->segid);
2911 tvc->segid = tvc->vmh = NULL;
2912 if (VREFCOUNT_GT(tvc,0))
2913 osi_Panic("flushVcache: vm race");
2921 #if defined(AFS_SUN5_ENV)
2927 if (tvc->linkData) {
2928 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
2933 afs_FreeAllAxs(&(tvc->Access));
2939 * Free any leftover callback queue
2941 for (i = 0; i < afs_stats_cmperf.CallBackAlloced; i++) {
2942 tsp = afs_cbrHeads[i];
2943 afs_cbrHeads[i] = 0;
2944 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
2948 #if !defined(AFS_LINUX22_ENV)
2949 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2951 # ifdef KERNEL_HAVE_PIN
2952 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
2955 freeVCList = Initial_freeVCList = 0;
2958 AFS_RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2959 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2961 for(i = 0; i < VCSIZE; ++i)
2962 QInit(&afs_vhashTV[i]);
2966 afs_DisconGiveUpCallbacks(void)
2972 ObtainWriteLock(&afs_xvcache, 1002); /* XXX - should be a unique number */
2975 /* Somehow, walk the set of vcaches, with each one coming out as tvc */
2976 for (i = 0; i < VCSIZE; i++) {
2977 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2979 if (afs_QueueVCB(tvc, &slept)) {
2980 tvc->callback = NULL;
2989 ReleaseWriteLock(&afs_xvcache);
2996 * Clear the Statd flag from all vcaches
2998 * This function removes the Statd flag from all vcaches. It's used by
2999 * disconnected mode to tidy up during reconnection
3003 afs_ClearAllStatdFlag(void)
3008 ObtainWriteLock(&afs_xvcache, 715);
3010 for (i = 0; i < VCSIZE; i++) {
3011 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3012 afs_StaleVCacheFlags(tvc, AFS_STALEVC_NODNLC | AFS_STALEVC_NOCB,
3016 ReleaseWriteLock(&afs_xvcache);
3020 * Mark a vcache as stale; our metadata for the relevant file may be out of
3023 * @post Any subsequent access to this vcache will cause us to fetch the
3024 * metadata for this vcache again.
3027 afs_StaleVCacheFlags(struct vcache *avc, afs_stalevc_flags_t flags,
3031 int do_filename = 0;
3033 int lock_cbhash = 1;
3035 if ((flags & AFS_STALEVC_NODNLC)) {
3038 if ((flags & AFS_STALEVC_FILENAME)) {
3041 if ((flags & AFS_STALEVC_CBLOCKED)) {
3044 if ((flags & AFS_STALEVC_NOCB)) {
3050 ObtainWriteLock(&afs_xcbhash, 486);
3053 afs_DequeueCallback(avc);
3057 avc->f.states &= ~cflags;
3060 ReleaseWriteLock(&afs_xcbhash);
3063 if ((flags & AFS_STALEVC_SKIP_DNLC_FOR_INIT_FLUSHED) &&
3064 (avc->f.states & (CVInit | CVFlushed))) {
3068 if (flags & AFS_STALEVC_CLEARCB) {
3069 avc->callback = NULL;
3073 if ((avc->f.fid.Fid.Vnode & 1) ||
3074 AFSTOV(avc) == NULL || vType(avc) == VDIR ||
3075 (avc->f.states & CForeign)) {
3076 /* This vcache is (or could be) a directory. */
3077 osi_dnlc_purgedp(avc);
3079 } else if (do_filename) {
3080 osi_dnlc_purgevp(avc);
3086 afs_SetDataVersion(struct vcache *avc, afs_hyper_t *avers)
3088 hset(avc->f.m.DataVersion, *avers);