2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
19 * afs_FlushActiveVcaches
22 * afs_WriteVCacheDiscon
40 #include <afsconfig.h>
41 #include "afs/param.h"
43 #include "afs/sysincludes.h" /*Standard vendor system headers */
44 #include "afsincludes.h" /*AFS-based standard headers */
45 #include "afs/afs_stats.h"
46 #include "afs/afs_cbqueue.h"
47 #include "afs/afs_osidnlc.h"
49 afs_int32 afs_maxvcount = 0; /* max number of vcache entries */
50 afs_int32 afs_vcount = 0; /* number of vcache in use now */
58 #endif /* AFS_SGI64_ENV */
60 /* Exported variables */
61 afs_rwlock_t afs_xvcdirty; /*Lock: discon vcache dirty list mgmt */
62 afs_rwlock_t afs_xvcache; /*Lock: alloc new stat cache entries */
63 afs_rwlock_t afs_xvreclaim; /*Lock: entries reclaimed, not on free list */
64 afs_lock_t afs_xvcb; /*Lock: fids on which there are callbacks */
65 #if !defined(AFS_LINUX22_ENV)
66 static struct vcache *freeVCList; /*Free list for stat cache entries */
67 struct vcache *ReclaimedVCList; /*Reclaimed list for stat entries */
68 static struct vcache *Initial_freeVCList; /*Initial list for above */
70 struct afs_q VLRU; /*vcache LRU */
71 afs_int32 vcachegen = 0;
72 unsigned int afs_paniconwarn = 0;
73 struct vcache *afs_vhashT[VCSIZE];
74 struct afs_q afs_vhashTV[VCSIZE];
75 static struct afs_cbr *afs_cbrHashT[CBRSIZE];
76 afs_int32 afs_bulkStatsLost;
77 int afs_norefpanic = 0;
80 /* Disk backed vcache definitions
81 * Both protected by xvcache */
82 static int afs_nextVcacheSlot = 0;
83 static struct afs_slotlist *afs_freeSlotList = NULL;
85 /* Forward declarations */
86 static afs_int32 afs_QueueVCB(struct vcache *avc, int *slept);
89 * Generate an index into the hash table for a given Fid.
91 * \return The hash value.
94 afs_HashCBRFid(struct AFSFid *fid)
96 return (fid->Volume + fid->Vnode + fid->Unique) % CBRSIZE;
100 * Insert a CBR entry into the hash table.
101 * Must be called with afs_xvcb held.
106 afs_InsertHashCBR(struct afs_cbr *cbr)
108 int slot = afs_HashCBRFid(&cbr->fid);
110 cbr->hash_next = afs_cbrHashT[slot];
111 if (afs_cbrHashT[slot])
112 afs_cbrHashT[slot]->hash_pprev = &cbr->hash_next;
114 cbr->hash_pprev = &afs_cbrHashT[slot];
115 afs_cbrHashT[slot] = cbr;
120 * Flush the given vcache entry.
123 * afs_xvcache lock must be held for writing upon entry to
124 * prevent people from changing the vrefCount field, and to
125 * protect the lruq and hnext fields.
126 * LOCK: afs_FlushVCache afs_xvcache W
127 * REFCNT: vcache ref count must be zero on entry except for osf1
128 * RACE: lock is dropped and reobtained, permitting race in caller
130 * \param avc Pointer to vcache entry to flush.
131 * \param slept Pointer to int to set 1 if we sleep/drop locks, 0 if we don't.
135 afs_FlushVCache(struct vcache *avc, int *slept)
136 { /*afs_FlushVCache */
139 struct vcache **uvc, *wvc;
141 /* NOTE: We must have nothing drop afs_xvcache until we have removed all
142 * possible references to this vcache. This means all hash tables, queues,
146 AFS_STATCNT(afs_FlushVCache);
147 afs_Trace2(afs_iclSetp, CM_TRACE_FLUSHV, ICL_TYPE_POINTER, avc,
148 ICL_TYPE_INT32, avc->f.states);
150 code = osi_VM_FlushVCache(avc);
154 if (avc->f.states & CVFlushed) {
158 #if !defined(AFS_LINUX22_ENV)
159 if (avc->nextfree || !avc->vlruq.prev || !avc->vlruq.next) { /* qv afs.h */
160 refpanic("LRU vs. Free inconsistency");
163 avc->f.states |= CVFlushed;
164 /* pull the entry out of the lruq and put it on the free list */
165 QRemove(&avc->vlruq);
167 /* keep track of # of files that we bulk stat'd, but never used
168 * before they got recycled.
170 if (avc->f.states & CBulkStat)
173 /* remove entry from the hash chain */
174 i = VCHash(&avc->f.fid);
175 uvc = &afs_vhashT[i];
176 for (wvc = *uvc; wvc; uvc = &wvc->hnext, wvc = *uvc) {
184 /* remove entry from the volume hash table */
185 QRemove(&avc->vhashq);
188 osi_FreeSmallSpace(avc->mvid);
189 avc->mvid = (struct VenusFid *)0;
191 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
192 avc->linkData = NULL;
194 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
195 /* OK, there are no internal vrefCounts, so there shouldn't
196 * be any more refs here. */
198 #ifdef AFS_DARWIN80_ENV
199 vnode_clearfsnode(AFSTOV(avc));
200 vnode_removefsref(AFSTOV(avc));
202 avc->v->v_data = NULL; /* remove from vnode */
204 AFSTOV(avc) = NULL; /* also drop the ptr to vnode */
207 #ifdef AFS_SUN510_ENV
208 /* As we use private vnodes, cleanup is up to us */
209 vn_reinit(AFSTOV(avc));
211 afs_FreeAllAxs(&(avc->Access));
212 ObtainWriteLock(&afs_xcbhash, 460);
213 afs_DequeueCallback(avc); /* remove it from queued callbacks list */
214 avc->f.states &= ~(CStatd | CUnique);
215 ReleaseWriteLock(&afs_xcbhash);
216 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
217 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
219 osi_dnlc_purgevp(avc);
221 /* By this point, the vcache has been removed from all global structures
222 * via which someone could try to use the vcache. It is okay to drop
223 * afs_xvcache at this point (if *slept is set). */
225 if (!afs_shuttingdown)
226 afs_QueueVCB(avc, slept);
229 * Next, keep track of which vnodes we've deleted for create's
230 * optimistic synchronization algorithm
233 if (avc->f.fid.Fid.Vnode & 1)
239 #if !defined(AFS_LINUX22_ENV)
240 /* put the entry in the free list */
241 avc->nextfree = freeVCList;
243 if (avc->vlruq.prev || avc->vlruq.next) {
244 refpanic("LRU vs. Free inconsistency");
246 avc->f.states |= CVFlushed;
248 /* This should put it back on the vnode free list since usecount is 1 */
250 if (VREFCOUNT_GT(avc,0)) {
251 AFS_RELE(AFSTOV(avc));
252 afs_stats_cmperf.vcacheXAllocs--;
254 if (afs_norefpanic) {
255 afs_warn("flush vc refcnt < 1");
258 osi_Panic("flush vc refcnt < 1");
260 #endif /* AFS_LINUX22_ENV */
265 } /*afs_FlushVCache */
269 * The core of the inactive vnode op for all but IRIX.
275 afs_InactiveVCache(struct vcache *avc, afs_ucred_t *acred)
277 AFS_STATCNT(afs_inactive);
278 if (avc->f.states & CDirty) {
279 /* we can't keep trying to push back dirty data forever. Give up. */
280 afs_InvalidateAllSegments(avc); /* turns off dirty bit */
282 avc->f.states &= ~CMAPPED; /* mainly used by SunOS 4.0.x */
283 avc->f.states &= ~CDirty; /* Turn it off */
284 if (avc->f.states & CUnlinked) {
285 if (CheckLock(&afs_xvcache) || CheckLock(&afs_xdcache)) {
286 avc->f.states |= CUnlinkedDel;
289 afs_remunlink(avc, 1); /* ignore any return code */
296 * Allocate a callback return structure from the
297 * free list and return it.
299 * Environment: The alloc and free routines are both called with the afs_xvcb lock
300 * held, so we don't have to worry about blocking in osi_Alloc.
302 * \return The allocated afs_cbr.
304 static struct afs_cbr *afs_cbrSpace = 0;
305 /* if alloc limit below changes, fix me! */
306 static struct afs_cbr *afs_cbrHeads[16];
313 while (!afs_cbrSpace) {
314 if (afs_stats_cmperf.CallBackAlloced >= sizeof(afs_cbrHeads)/sizeof(afs_cbrHeads[0])) {
315 /* don't allocate more than 16 * AFS_NCBRS for now */
317 afs_stats_cmperf.CallBackFlushes++;
320 tsp = afs_osi_Alloc(AFS_NCBRS * sizeof(struct afs_cbr));
321 osi_Assert(tsp != NULL);
322 for (i = 0; i < AFS_NCBRS - 1; i++) {
323 tsp[i].next = &tsp[i + 1];
325 tsp[AFS_NCBRS - 1].next = 0;
327 afs_cbrHeads[afs_stats_cmperf.CallBackAlloced] = tsp;
328 afs_stats_cmperf.CallBackAlloced++;
332 afs_cbrSpace = tsp->next;
337 * Free a callback return structure, removing it from all lists.
339 * Environment: the xvcb lock is held over these calls.
341 * \param asp The address of the structure to free.
346 afs_FreeCBR(struct afs_cbr *asp)
348 *(asp->pprev) = asp->next;
350 asp->next->pprev = asp->pprev;
352 *(asp->hash_pprev) = asp->hash_next;
354 asp->hash_next->hash_pprev = asp->hash_pprev;
356 asp->next = afs_cbrSpace;
362 FlushAllVCBs(int nconns, struct rx_connection **rxconns,
363 struct afs_conn **conns)
368 results = afs_osi_Alloc(nconns * sizeof (afs_int32));
369 osi_Assert(results != NULL);
372 multi_Rx(rxconns,nconns)
374 multi_RXAFS_GiveUpAllCallBacks();
375 results[multi_i] = multi_error;
380 * Freeing the CBR will unlink it from the server's CBR list
381 * do it here, not in the loop, because a dynamic CBR will call
382 * into the memory management routines.
384 for ( i = 0 ; i < nconns ; i++ ) {
385 if (results[i] == 0) {
386 /* Unchain all of them */
387 while (conns[i]->parent->srvr->server->cbrs)
388 afs_FreeCBR(conns[i]->parent->srvr->server->cbrs);
391 afs_osi_Free(results, nconns * sizeof(afs_int32));
395 * Flush all queued callbacks to all servers.
397 * Environment: holds xvcb lock over RPC to guard against race conditions
398 * when a new callback is granted for the same file later on.
400 * \return 0 for success.
403 afs_FlushVCBs(afs_int32 lockit)
405 struct AFSFid *tfids;
406 struct AFSCallBack callBacks[1];
407 struct AFSCBFids fidArray;
408 struct AFSCBs cbArray;
410 struct afs_cbr *tcbrp;
414 struct vrequest *treq = NULL;
416 int safety1, safety2, safety3;
419 if (AFS_IS_DISCONNECTED)
422 if ((code = afs_CreateReq(&treq, afs_osi_credp)))
424 treq->flags |= O_NONBLOCK;
425 tfids = afs_osi_Alloc(sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
426 osi_Assert(tfids != NULL);
429 ObtainWriteLock(&afs_xvcb, 273);
432 * First, attempt a multi across everything, all addresses
433 * for all servers we know of.
437 afs_LoopServers(AFS_LS_ALL, NULL, 0, FlushAllVCBs, NULL);
439 ObtainReadLock(&afs_xserver);
440 for (i = 0; i < NSERVERS; i++) {
441 for (safety1 = 0, tsp = afs_servers[i];
442 tsp && safety1 < afs_totalServers + 10;
443 tsp = tsp->next, safety1++) {
445 if (tsp->cbrs == (struct afs_cbr *)0)
448 /* otherwise, grab a block of AFS_MAXCBRSCALL from the list
449 * and make an RPC, over and over again.
451 tcount = 0; /* number found so far */
452 for (safety2 = 0; safety2 < afs_cacheStats; safety2++) {
453 if (tcount >= AFS_MAXCBRSCALL || !tsp->cbrs) {
454 struct rx_connection *rxconn;
455 /* if buffer is full, or we've queued all we're going
456 * to from this server, we should flush out the
459 fidArray.AFSCBFids_len = tcount;
460 fidArray.AFSCBFids_val = (struct AFSFid *)tfids;
461 cbArray.AFSCBs_len = 1;
462 cbArray.AFSCBs_val = callBacks;
463 memset(&callBacks[0], 0, sizeof(callBacks[0]));
464 callBacks[0].CallBackType = CB_EXCLUSIVE;
465 for (safety3 = 0; safety3 < AFS_MAXHOSTS * 2; safety3++) {
466 tc = afs_ConnByHost(tsp, tsp->cell->fsport,
467 tsp->cell->cellNum, treq, 0,
468 SHARED_LOCK, 0, &rxconn);
471 (AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS);
474 RXAFS_GiveUpCallBacks(rxconn, &fidArray,
481 (tc, rxconn, code, 0, treq,
482 AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS, SHARED_LOCK,
487 /* ignore return code, since callbacks may have
488 * been returned anyway, we shouldn't leave them
489 * around to be returned again.
491 * Next, see if we are done with this server, and if so,
492 * break to deal with the next one.
498 /* if to flush full buffer */
499 /* if we make it here, we have an entry at the head of cbrs,
500 * which we should copy to the file ID array and then free.
503 tfids[tcount++] = tcbrp->fid;
505 /* Freeing the CBR will unlink it from the server's CBR list */
507 } /* while loop for this one server */
508 if (safety2 > afs_cacheStats) {
509 afs_warn("possible internal error afs_flushVCBs (%d)\n",
512 } /* for loop for this hash chain */
513 } /* loop through all hash chains */
514 if (safety1 > afs_totalServers + 2) {
516 ("AFS internal error (afs_flushVCBs) (%d > %d), continuing...\n",
517 safety1, afs_totalServers + 2);
519 osi_Panic("afs_flushVCBS safety1");
522 ReleaseReadLock(&afs_xserver);
524 ReleaseWriteLock(&afs_xvcb);
525 afs_osi_Free(tfids, sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
526 afs_DestroyReq(treq);
531 * Queue a callback on the given fid.
534 * Locks the xvcb lock.
535 * Called when the xvcache lock is already held.
536 * RACE: afs_xvcache may be dropped and reacquired
538 * \param avc vcache entry
539 * \param slep Set to 1 if we dropped afs_xvcache
540 * \return 1 if queued, 0 otherwise
544 afs_QueueVCB(struct vcache *avc, int *slept)
548 struct afs_cbr *tcbp;
551 AFS_STATCNT(afs_QueueVCB);
553 ObtainWriteLock(&afs_xvcb, 274);
555 /* we can't really give back callbacks on RO files, since the
556 * server only tracks them on a per-volume basis, and we don't
557 * know whether we still have some other files from the same
559 if (!((avc->f.states & CRO) == 0 && avc->callback)) {
563 /* The callback is really just a struct server ptr. */
564 tsp = (struct server *)(avc->callback);
567 /* If we don't have CBR space, AllocCBR may block or hit the net for
568 * clearing up CBRs. Hitting the net may involve a fileserver
569 * needing to contact us, so we must drop xvcache so we don't block
570 * those requests from going through. */
571 reacquire = *slept = 1;
572 ReleaseWriteLock(&afs_xvcache);
575 /* we now have a pointer to the server, so we just allocate
576 * a queue entry and queue it.
578 tcbp = afs_AllocCBR();
579 tcbp->fid = avc->f.fid.Fid;
581 tcbp->next = tsp->cbrs;
583 tsp->cbrs->pprev = &tcbp->next;
586 tcbp->pprev = &tsp->cbrs;
588 afs_InsertHashCBR(tcbp);
592 /* now release locks and return */
593 ReleaseWriteLock(&afs_xvcb);
596 /* make sure this is after dropping xvcb, for locking order */
597 ObtainWriteLock(&afs_xvcache, 279);
604 * Remove a queued callback for a given Fid.
607 * Locks xvcb and xserver locks.
608 * Typically called with xdcache, xvcache and/or individual vcache
611 * \param afid The fid we want cleansed of queued callbacks.
616 afs_RemoveVCB(struct VenusFid *afid)
619 struct afs_cbr *cbr, *ncbr;
621 AFS_STATCNT(afs_RemoveVCB);
622 ObtainWriteLock(&afs_xvcb, 275);
624 slot = afs_HashCBRFid(&afid->Fid);
625 ncbr = afs_cbrHashT[slot];
629 ncbr = cbr->hash_next;
631 if (afid->Fid.Volume == cbr->fid.Volume &&
632 afid->Fid.Vnode == cbr->fid.Vnode &&
633 afid->Fid.Unique == cbr->fid.Unique) {
638 ReleaseWriteLock(&afs_xvcb);
642 afs_FlushReclaimedVcaches(void)
644 #if !defined(AFS_LINUX22_ENV)
647 struct vcache *tmpReclaimedVCList = NULL;
649 ObtainWriteLock(&afs_xvreclaim, 76);
650 while (ReclaimedVCList) {
651 tvc = ReclaimedVCList; /* take from free list */
652 ReclaimedVCList = tvc->nextfree;
653 tvc->nextfree = NULL;
654 code = afs_FlushVCache(tvc, &fv_slept);
656 /* Ok, so, if we got code != 0, uh, wtf do we do? */
657 /* Probably, build a temporary list and then put all back when we
658 get to the end of the list */
659 /* This is actually really crappy, but we need to not leak these.
660 We probably need a way to be smarter about this. */
661 tvc->nextfree = tmpReclaimedVCList;
662 tmpReclaimedVCList = tvc;
663 /* printf("Reclaim list flush %lx failed: %d\n", (unsigned long) tvc, code); */
665 if (tvc->f.states & (CVInit
666 #ifdef AFS_DARWIN80_ENV
670 tvc->f.states &= ~(CVInit
671 #ifdef AFS_DARWIN80_ENV
675 afs_osi_Wakeup(&tvc->f.states);
678 if (tmpReclaimedVCList)
679 ReclaimedVCList = tmpReclaimedVCList;
681 ReleaseWriteLock(&afs_xvreclaim);
686 afs_PostPopulateVCache(struct vcache *avc, struct VenusFid *afid, int seq)
689 * The proper value for mvstat (for root fids) is setup by the caller.
692 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
695 if (afs_globalVFS == 0)
696 osi_Panic("afs globalvfs");
698 osi_PostPopulateVCache(avc);
701 osi_dnlc_purgedp(avc); /* this may be overkill */
702 memset(&(avc->callsort), 0, sizeof(struct afs_q));
704 avc->f.states &=~ CVInit;
706 avc->f.states |= CBulkFetching;
707 avc->f.m.Length = seq;
709 afs_osi_Wakeup(&avc->f.states);
713 afs_ShakeLooseVCaches(afs_int32 anumber)
717 struct afs_q *tq, *uq;
718 int fv_slept, defersleep = 0;
720 afs_int32 target = anumber;
727 for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
730 if (tvc->f.states & CVFlushed) {
731 refpanic("CVFlushed on VLRU");
732 } else if (i++ > limit) {
733 afs_warn("afs_ShakeLooseVCaches: i %d limit %d afs_vcount %d afs_maxvcount %d\n",
734 (int)i, limit, (int)afs_vcount, (int)afs_maxvcount);
735 refpanic("Found too many AFS vnodes on VLRU (VLRU cycle?)");
736 } else if (QNext(uq) != tq) {
737 refpanic("VLRU inconsistent");
738 } else if (tvc->f.states & CVInit) {
743 if (osi_TryEvictVCache(tvc, &fv_slept, defersleep))
749 goto retry; /* start over - may have raced. */
752 if (anumber && !defersleep) {
759 if (!afsd_dynamic_vcaches && anumber == target) {
760 afs_warn("afs_ShakeLooseVCaches: warning none freed, using %d of %d\n",
761 afs_vcount, afs_maxvcount);
767 /* Alloc new vnode. */
769 static struct vcache *
770 afs_AllocVCache(void)
774 tvc = osi_NewVnode();
779 if (afsd_dynamic_vcaches && afs_maxvcount < afs_vcount) {
780 afs_maxvcount = afs_vcount;
781 /*printf("peak vnodes: %d\n", afs_maxvcount);*/
784 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
786 /* If we create a new inode, we either give it a new slot number,
787 * or if one's available, use a slot number from the slot free list
789 if (afs_freeSlotList != NULL) {
790 struct afs_slotlist *tmp;
792 tvc->diskSlot = afs_freeSlotList->slot;
793 tmp = afs_freeSlotList;
794 afs_freeSlotList = tmp->next;
795 afs_osi_Free(tmp, sizeof(struct afs_slotlist));
797 tvc->diskSlot = afs_nextVcacheSlot++;
803 /* Pre populate a newly allocated vcache. On platforms where the actual
804 * vnode is attached to the vcache, this function is called before attachment,
805 * therefore it cannot perform any actions on the vnode itself */
808 afs_PrePopulateVCache(struct vcache *avc, struct VenusFid *afid,
809 struct server *serverp) {
812 slot = avc->diskSlot;
814 osi_PrePopulateVCache(avc);
816 avc->diskSlot = slot;
817 QZero(&avc->metadirty);
819 AFS_RWLOCK_INIT(&avc->lock, "vcache lock");
822 avc->linkData = NULL;
825 avc->execsOrWriters = 0;
827 avc->f.states = CVInit;
828 avc->last_looker = 0;
830 avc->asynchrony = -1;
834 avc->f.truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
835 hzero(avc->f.m.DataVersion); /* in case we copy it into flushDV */
837 avc->callback = serverp; /* to minimize chance that clear
840 #if defined(AFS_CACHE_BYPASS)
841 avc->cachingStates = 0;
842 avc->cachingTransitions = 0;
847 afs_FlushAllVCaches(void)
850 struct vcache *tvc, *nvc;
852 ObtainWriteLock(&afs_xvcache, 867);
855 for (i = 0; i < VCSIZE; i++) {
856 for (tvc = afs_vhashT[i]; tvc; tvc = nvc) {
860 if (afs_FlushVCache(tvc, &slept)) {
861 afs_warn("Failed to flush vcache 0x%lx\n", (unsigned long)(uintptrsz)tvc);
869 ReleaseWriteLock(&afs_xvcache);
873 * This routine is responsible for allocating a new cache entry
874 * from the free list. It formats the cache entry and inserts it
875 * into the appropriate hash tables. It must be called with
876 * afs_xvcache write-locked so as to prevent several processes from
877 * trying to create a new cache entry simultaneously.
879 * LOCK: afs_NewVCache afs_xvcache W
881 * \param afid The file id of the file whose cache entry is being created.
883 * \return The new vcache struct.
886 static_inline struct vcache *
887 afs_NewVCache_int(struct VenusFid *afid, struct server *serverp, int seq)
891 afs_int32 anumber = VCACHE_FREE;
893 AFS_STATCNT(afs_NewVCache);
895 afs_FlushReclaimedVcaches();
897 #if defined(AFS_LINUX22_ENV)
898 if(!afsd_dynamic_vcaches && afs_vcount >= afs_maxvcount) {
899 afs_ShakeLooseVCaches(anumber);
900 if (afs_vcount >= afs_maxvcount) {
901 afs_warn("afs_NewVCache - none freed\n");
905 tvc = afs_AllocVCache();
906 #else /* AFS_LINUX22_ENV */
907 /* pull out a free cache entry */
909 afs_ShakeLooseVCaches(anumber);
913 tvc = afs_AllocVCache();
915 tvc = freeVCList; /* take from free list */
916 freeVCList = tvc->nextfree;
917 tvc->nextfree = NULL;
918 afs_vcount++; /* balanced by FlushVCache */
919 } /* end of if (!freeVCList) */
921 #endif /* AFS_LINUX22_ENV */
923 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
925 panic("afs_NewVCache(): free vcache with vnode attached");
928 /* Populate the vcache with as much as we can. */
929 afs_PrePopulateVCache(tvc, afid, serverp);
931 /* Thread the vcache onto the VLRU */
936 tvc->hnext = afs_vhashT[i];
938 QAdd(&afs_vhashTV[j], &tvc->vhashq);
940 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
941 refpanic("NewVCache VLRU inconsistent");
943 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
944 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
945 refpanic("NewVCache VLRU inconsistent2");
947 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
948 refpanic("NewVCache VLRU inconsistent3");
950 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
951 refpanic("NewVCache VLRU inconsistent4");
955 /* it should now be safe to drop the xvcache lock - so attach an inode
956 * to this vcache, where necessary */
957 osi_AttachVnode(tvc, seq);
959 /* Get a reference count to hold this vcache for the VLRUQ. Note that
960 * we have to do this after attaching the vnode, because the reference
961 * count may be held in the vnode itself */
963 #if defined(AFS_LINUX22_ENV)
964 /* Hold it for the LRU (should make count 2) */
966 #elif !(defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV))
967 VREFCOUNT_SET(tvc, 1); /* us */
970 #if defined (AFS_FBSD_ENV)
971 if (tvc->f.states & CVInit)
973 afs_PostPopulateVCache(tvc, afid, seq);
980 afs_NewVCache(struct VenusFid *afid, struct server *serverp)
982 return afs_NewVCache_int(afid, serverp, 0);
986 afs_NewBulkVCache(struct VenusFid *afid, struct server *serverp, int seq)
988 return afs_NewVCache_int(afid, serverp, seq);
994 * LOCK: afs_FlushActiveVcaches afs_xvcache N
996 * \param doflocks : Do we handle flocks?
999 afs_FlushActiveVcaches(afs_int32 doflocks)
1003 struct afs_conn *tc;
1005 afs_ucred_t *cred = NULL;
1006 struct vrequest *treq = NULL;
1007 struct AFSVolSync tsync;
1010 AFS_STATCNT(afs_FlushActiveVcaches);
1012 code = afs_CreateReq(&treq, afs_osi_credp);
1014 afs_warn("unable to alloc treq\n");
1018 ObtainReadLock(&afs_xvcache);
1019 for (i = 0; i < VCSIZE; i++) {
1020 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1021 if (tvc->f.states & CVInit) continue;
1022 #ifdef AFS_DARWIN80_ENV
1023 if (tvc->f.states & CDeadVnode &&
1024 (tvc->f.states & (CCore|CUnlinkedDel) ||
1025 tvc->flockCount)) panic("Dead vnode has core/unlinkedel/flock");
1027 if (doflocks && tvc->flockCount != 0) {
1028 struct rx_connection *rxconn;
1029 /* if this entry has an flock, send a keep-alive call out */
1031 ReleaseReadLock(&afs_xvcache);
1032 ObtainWriteLock(&tvc->lock, 51);
1034 code = afs_InitReq(treq, afs_osi_credp);
1037 break; /* shutting down: do not try to extend the lock */
1039 treq->flags |= O_NONBLOCK;
1041 tc = afs_Conn(&tvc->f.fid, treq, SHARED_LOCK, &rxconn);
1043 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
1046 RXAFS_ExtendLock(rxconn,
1047 (struct AFSFid *)&tvc->f.fid.Fid,
1053 } while (afs_Analyze
1054 (tc, rxconn, code, &tvc->f.fid, treq,
1055 AFS_STATS_FS_RPCIDX_EXTENDLOCK, SHARED_LOCK, NULL));
1057 ReleaseWriteLock(&tvc->lock);
1058 #ifdef AFS_DARWIN80_ENV
1060 ObtainReadLock(&afs_xvcache);
1062 ObtainReadLock(&afs_xvcache);
1067 if ((tvc->f.states & CCore) || (tvc->f.states & CUnlinkedDel)) {
1069 * Don't let it evaporate in case someone else is in
1070 * this code. Also, drop the afs_xvcache lock while
1071 * getting vcache locks.
1074 ReleaseReadLock(&afs_xvcache);
1075 #if defined(AFS_SGI_ENV)
1077 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1079 osi_Assert(VREFCOUNT_GT(tvc,0));
1080 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1082 ObtainWriteLock(&tvc->lock, 52);
1083 if (tvc->f.states & CCore) {
1084 tvc->f.states &= ~CCore;
1085 /* XXXX Find better place-holder for cred XXXX */
1086 cred = (afs_ucred_t *)tvc->linkData;
1087 tvc->linkData = NULL; /* XXX */
1088 code = afs_InitReq(treq, cred);
1089 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1090 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
1091 tvc->execsOrWriters);
1092 if (!code) { /* avoid store when shutting down */
1093 code = afs_StoreOnLastReference(tvc, treq);
1095 ReleaseWriteLock(&tvc->lock);
1096 hzero(tvc->flushDV);
1099 if (code && code != VNOVNODE) {
1100 afs_StoreWarn(code, tvc->f.fid.Fid.Volume,
1101 /* /dev/console */ 1);
1103 } else if (tvc->f.states & CUnlinkedDel) {
1107 ReleaseWriteLock(&tvc->lock);
1108 #if defined(AFS_SGI_ENV)
1109 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1111 afs_remunlink(tvc, 0);
1112 #if defined(AFS_SGI_ENV)
1113 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1116 /* lost (or won, perhaps) the race condition */
1117 ReleaseWriteLock(&tvc->lock);
1119 #if defined(AFS_SGI_ENV)
1120 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1122 #ifdef AFS_DARWIN80_ENV
1125 AFS_RELE(AFSTOV(tvc));
1126 /* Matches write code setting CCore flag */
1129 ObtainReadLock(&afs_xvcache);
1131 ObtainReadLock(&afs_xvcache);
1134 AFS_RELE(AFSTOV(tvc));
1135 /* Matches write code setting CCore flag */
1142 ReleaseReadLock(&afs_xvcache);
1143 afs_DestroyReq(treq);
1149 * Make sure a cache entry is up-to-date status-wise.
1151 * NOTE: everywhere that calls this can potentially be sped up
1152 * by checking CStatd first, and avoiding doing the InitReq
1153 * if this is up-to-date.
1155 * Anymore, the only places that call this KNOW already that the
1156 * vcache is not up-to-date, so we don't screw around.
1158 * \param avc : Ptr to vcache entry to verify.
1164 * Make sure a cache entry is up-to-date status-wise.
1166 * NOTE: everywhere that calls this can potentially be sped up
1167 * by checking CStatd first, and avoiding doing the InitReq
1168 * if this is up-to-date.
1170 * Anymore, the only places that call this KNOW already that the
1171 * vcache is not up-to-date, so we don't screw around.
1173 * \param avc Pointer to vcache entry to verify.
1176 * \return 0 for success or other error codes.
1179 afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1183 AFS_STATCNT(afs_VerifyVCache);
1185 /* otherwise we must fetch the status info */
1187 ObtainWriteLock(&avc->lock, 53);
1188 if (avc->f.states & CStatd) {
1189 ReleaseWriteLock(&avc->lock);
1192 ObtainWriteLock(&afs_xcbhash, 461);
1193 avc->f.states &= ~(CStatd | CUnique);
1194 avc->callback = NULL;
1195 afs_DequeueCallback(avc);
1196 ReleaseWriteLock(&afs_xcbhash);
1197 ReleaseWriteLock(&avc->lock);
1199 /* since we've been called back, or the callback has expired,
1200 * it's possible that the contents of this directory, or this
1201 * file's name have changed, thus invalidating the dnlc contents.
1203 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
1204 osi_dnlc_purgedp(avc);
1206 osi_dnlc_purgevp(avc);
1208 /* fetch the status info */
1209 tvc = afs_GetVCache(&avc->f.fid, areq, NULL, avc);
1212 /* Put it back; caller has already incremented vrefCount */
1216 } /*afs_VerifyVCache */
1220 * Simple copy of stat info into cache.
1222 * Callers:as of 1992-04-29, only called by WriteVCache
1224 * \param avc Ptr to vcache entry involved.
1225 * \param astat Ptr to stat info to copy.
1229 afs_SimpleVStat(struct vcache *avc,
1230 struct AFSFetchStatus *astat, struct vrequest *areq)
1233 AFS_STATCNT(afs_SimpleVStat);
1235 #ifdef AFS_64BIT_CLIENT
1236 FillInt64(length, astat->Length_hi, astat->Length);
1237 #else /* AFS_64BIT_CLIENT */
1238 length = astat->Length;
1239 #endif /* AFS_64BIT_CLIENT */
1241 #if defined(AFS_SGI_ENV)
1242 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1243 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1244 osi_Assert((valusema(&avc->vc_rwlock) <= 0)
1245 && (OSI_GET_LOCKID() == avc->vc_rwlockid));
1246 if (length < avc->f.m.Length) {
1247 vnode_t *vp = (vnode_t *) avc;
1249 osi_Assert(WriteLocked(&avc->lock));
1250 ReleaseWriteLock(&avc->lock);
1252 PTOSSVP(vp, (off_t) length, (off_t) MAXLONG);
1254 ObtainWriteLock(&avc->lock, 67);
1259 if (!afs_DirtyPages(avc)) {
1260 /* if actively writing the file, don't fetch over this value */
1261 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT, ICL_TYPE_POINTER, avc,
1262 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1263 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1264 avc->f.m.Length = length;
1265 avc->f.m.Date = astat->ClientModTime;
1267 avc->f.m.Owner = astat->Owner;
1268 avc->f.m.Group = astat->Group;
1269 avc->f.m.Mode = astat->UnixModeBits;
1270 if (vType(avc) == VREG) {
1271 avc->f.m.Mode |= S_IFREG;
1272 } else if (vType(avc) == VDIR) {
1273 avc->f.m.Mode |= S_IFDIR;
1274 } else if (vType(avc) == VLNK) {
1275 avc->f.m.Mode |= S_IFLNK;
1276 if ((avc->f.m.Mode & 0111) == 0)
1279 if (avc->f.states & CForeign) {
1280 struct axscache *ac;
1281 avc->f.anyAccess = astat->AnonymousAccess;
1283 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1285 * Caller has at least one bit not covered by anonymous, and
1286 * thus may have interesting rights.
1288 * HOWEVER, this is a really bad idea, because any access query
1289 * for bits which aren't covered by anonymous, on behalf of a user
1290 * who doesn't have any special rights, will result in an answer of
1291 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1292 * It's an especially bad idea under Ultrix, since (due to the lack of
1293 * a proper access() call) it must perform several afs_access() calls
1294 * in order to create magic mode bits that vary according to who makes
1295 * the call. In other words, _every_ stat() generates a test for
1298 #endif /* badidea */
1299 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1300 ac->axess = astat->CallerAccess;
1301 else /* not found, add a new one if possible */
1302 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1305 } /*afs_SimpleVStat */
1309 * Store the status info *only* back to the server for a
1312 * Environment: Must be called with a shared lock held on the vnode.
1314 * \param avc Ptr to the vcache entry.
1315 * \param astatus Ptr to the status info to store.
1316 * \param areq Ptr to the associated vrequest.
1318 * \return Operation status.
1322 afs_WriteVCache(struct vcache *avc,
1323 struct AFSStoreStatus *astatus,
1324 struct vrequest *areq)
1327 struct afs_conn *tc;
1328 struct AFSFetchStatus OutStatus;
1329 struct AFSVolSync tsync;
1330 struct rx_connection *rxconn;
1332 AFS_STATCNT(afs_WriteVCache);
1333 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1334 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
1336 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK, &rxconn);
1338 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1341 RXAFS_StoreStatus(rxconn, (struct AFSFid *)&avc->f.fid.Fid,
1342 astatus, &OutStatus, &tsync);
1347 } while (afs_Analyze
1348 (tc, rxconn, code, &avc->f.fid, areq, AFS_STATS_FS_RPCIDX_STORESTATUS,
1349 SHARED_LOCK, NULL));
1351 UpgradeSToWLock(&avc->lock, 20);
1353 /* success, do the changes locally */
1354 afs_SimpleVStat(avc, &OutStatus, areq);
1356 * Update the date, too. SimpleVStat didn't do this, since
1357 * it thought we were doing this after fetching new status
1358 * over a file being written.
1360 avc->f.m.Date = OutStatus.ClientModTime;
1362 /* failure, set up to check with server next time */
1363 ObtainWriteLock(&afs_xcbhash, 462);
1364 afs_DequeueCallback(avc);
1365 avc->f.states &= ~(CStatd | CUnique); /* turn off stat valid flag */
1366 ReleaseWriteLock(&afs_xcbhash);
1367 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
1368 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
1370 ConvertWToSLock(&avc->lock);
1373 } /*afs_WriteVCache */
1376 * Store status info only locally, set the proper disconnection flags
1377 * and add to dirty list.
1379 * \param avc The vcache to be written locally.
1380 * \param astatus Get attr fields from local store.
1381 * \param attrs This one is only of the vs_size.
1383 * \note Must be called with a shared lock on the vnode
1386 afs_WriteVCacheDiscon(struct vcache *avc,
1387 struct AFSStoreStatus *astatus,
1388 struct vattr *attrs)
1391 afs_int32 flags = 0;
1393 UpgradeSToWLock(&avc->lock, 700);
1395 if (!astatus->Mask) {
1401 /* Set attributes. */
1402 if (astatus->Mask & AFS_SETMODTIME) {
1403 avc->f.m.Date = astatus->ClientModTime;
1404 flags |= VDisconSetTime;
1407 if (astatus->Mask & AFS_SETOWNER) {
1408 /* printf("Not allowed yet. \n"); */
1409 /*avc->f.m.Owner = astatus->Owner;*/
1412 if (astatus->Mask & AFS_SETGROUP) {
1413 /* printf("Not allowed yet. \n"); */
1414 /*avc->f.m.Group = astatus->Group;*/
1417 if (astatus->Mask & AFS_SETMODE) {
1418 avc->f.m.Mode = astatus->UnixModeBits;
1420 #if 0 /* XXX: Leaving this out, so it doesn't mess up the file type flag.*/
1422 if (vType(avc) == VREG) {
1423 avc->f.m.Mode |= S_IFREG;
1424 } else if (vType(avc) == VDIR) {
1425 avc->f.m.Mode |= S_IFDIR;
1426 } else if (vType(avc) == VLNK) {
1427 avc->f.m.Mode |= S_IFLNK;
1428 if ((avc->f.m.Mode & 0111) == 0)
1432 flags |= VDisconSetMode;
1433 } /* if(astatus.Mask & AFS_SETMODE) */
1435 } /* if (!astatus->Mask) */
1437 if (attrs->va_size > 0) {
1438 /* XXX: Do I need more checks? */
1439 /* Truncation operation. */
1440 flags |= VDisconTrunc;
1444 afs_DisconAddDirty(avc, flags, 1);
1446 /* XXX: How about the rest of the fields? */
1448 ConvertWToSLock(&avc->lock);
1454 * Copy astat block into vcache info
1456 * \note This code may get dataversion and length out of sync if the file has
1457 * been modified. This is less than ideal. I haven't thought about it sufficiently
1458 * to be certain that it is adequate.
1460 * \note Environment: Must be called under a write lock
1462 * \param avc Ptr to vcache entry.
1463 * \param astat Ptr to stat block to copy in.
1464 * \param areq Ptr to associated request.
1467 afs_ProcessFS(struct vcache *avc,
1468 struct AFSFetchStatus *astat, struct vrequest *areq)
1471 AFS_STATCNT(afs_ProcessFS);
1473 #ifdef AFS_64BIT_CLIENT
1474 FillInt64(length, astat->Length_hi, astat->Length);
1475 #else /* AFS_64BIT_CLIENT */
1476 length = astat->Length;
1477 #endif /* AFS_64BIT_CLIENT */
1478 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1479 * number for each bulk status request. Under no circumstances
1480 * should afs_DoBulkStat store a sequence number if the new
1481 * length will be ignored when afs_ProcessFS is called with
1482 * new stats. If you change the following conditional then you
1483 * also need to change the conditional in afs_DoBulkStat. */
1485 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1486 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1488 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1490 /* if we're writing or mapping this file, don't fetch over these
1493 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1494 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1495 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1496 avc->f.m.Length = length;
1497 avc->f.m.Date = astat->ClientModTime;
1499 hset64(avc->f.m.DataVersion, astat->dataVersionHigh, astat->DataVersion);
1500 avc->f.m.Owner = astat->Owner;
1501 avc->f.m.Mode = astat->UnixModeBits;
1502 avc->f.m.Group = astat->Group;
1503 avc->f.m.LinkCount = astat->LinkCount;
1504 if (astat->FileType == File) {
1505 vSetType(avc, VREG);
1506 avc->f.m.Mode |= S_IFREG;
1507 } else if (astat->FileType == Directory) {
1508 vSetType(avc, VDIR);
1509 avc->f.m.Mode |= S_IFDIR;
1510 } else if (astat->FileType == SymbolicLink) {
1511 if (afs_fakestat_enable && (avc->f.m.Mode & 0111) == 0) {
1512 vSetType(avc, VDIR);
1513 avc->f.m.Mode |= S_IFDIR;
1515 vSetType(avc, VLNK);
1516 avc->f.m.Mode |= S_IFLNK;
1518 if ((avc->f.m.Mode & 0111) == 0) {
1522 avc->f.anyAccess = astat->AnonymousAccess;
1524 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1526 * Caller has at least one bit not covered by anonymous, and
1527 * thus may have interesting rights.
1529 * HOWEVER, this is a really bad idea, because any access query
1530 * for bits which aren't covered by anonymous, on behalf of a user
1531 * who doesn't have any special rights, will result in an answer of
1532 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1533 * It's an especially bad idea under Ultrix, since (due to the lack of
1534 * a proper access() call) it must perform several afs_access() calls
1535 * in order to create magic mode bits that vary according to who makes
1536 * the call. In other words, _every_ stat() generates a test for
1539 #endif /* badidea */
1541 struct axscache *ac;
1542 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1543 ac->axess = astat->CallerAccess;
1544 else /* not found, add a new one if possible */
1545 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1547 } /*afs_ProcessFS */
1551 * Get fid from server.
1554 * \param areq Request to be passed on.
1555 * \param name Name of ?? to lookup.
1556 * \param OutStatus Fetch status.
1561 * \return Success status of operation.
1564 afs_RemoteLookup(struct VenusFid *afid, struct vrequest *areq,
1565 char *name, struct VenusFid *nfid,
1566 struct AFSFetchStatus *OutStatusp,
1567 struct AFSCallBack *CallBackp, struct server **serverp,
1568 struct AFSVolSync *tsyncp)
1571 struct afs_conn *tc;
1572 struct rx_connection *rxconn;
1573 struct AFSFetchStatus OutDirStatus;
1576 name = ""; /* XXX */
1578 tc = afs_Conn(afid, areq, SHARED_LOCK, &rxconn);
1581 *serverp = tc->parent->srvr->server;
1582 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1585 RXAFS_Lookup(rxconn, (struct AFSFid *)&afid->Fid, name,
1586 (struct AFSFid *)&nfid->Fid, OutStatusp,
1587 &OutDirStatus, CallBackp, tsyncp);
1592 } while (afs_Analyze
1593 (tc, rxconn, code, afid, areq, AFS_STATS_FS_RPCIDX_XLOOKUP, SHARED_LOCK,
1603 * Given a file id and a vrequest structure, fetch the status
1604 * information associated with the file.
1606 * \param afid File ID.
1607 * \param areq Ptr to associated vrequest structure, specifying the
1608 * user whose authentication tokens will be used.
1609 * \param avc Caller may already have a vcache for this file, which is
1612 * \note Environment:
1613 * The cache entry is returned with an increased vrefCount field.
1614 * The entry must be discarded by calling afs_PutVCache when you
1615 * are through using the pointer to the cache entry.
1617 * You should not hold any locks when calling this function, except
1618 * locks on other vcache entries. If you lock more than one vcache
1619 * entry simultaneously, you should lock them in this order:
1621 * 1. Lock all files first, then directories.
1622 * 2. Within a particular type, lock entries in Fid.Vnode order.
1624 * This locking hierarchy is convenient because it allows locking
1625 * of a parent dir cache entry, given a file (to check its access
1626 * control list). It also allows renames to be handled easily by
1627 * locking directories in a constant order.
1629 * \note NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1631 * \note Might have a vcache structure already, which must
1632 * already be held by the caller
1635 afs_GetVCache(struct VenusFid *afid, struct vrequest *areq,
1636 afs_int32 * cached, struct vcache *avc)
1639 afs_int32 code, newvcache = 0;
1644 AFS_STATCNT(afs_GetVCache);
1647 *cached = 0; /* Init just in case */
1649 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1653 ObtainSharedLock(&afs_xvcache, 5);
1655 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU | IS_SLOCK);
1657 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1658 ReleaseSharedLock(&afs_xvcache);
1659 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1666 osi_Assert((tvc->f.states & CVInit) == 0);
1667 /* If we are in readdir, return the vnode even if not statd */
1668 if ((tvc->f.states & CStatd) || afs_InReadDir(tvc)) {
1669 ReleaseSharedLock(&afs_xvcache);
1673 UpgradeSToWLock(&afs_xvcache, 21);
1675 /* no cache entry, better grab one */
1676 tvc = afs_NewVCache(afid, NULL);
1679 ConvertWToSLock(&afs_xvcache);
1682 ReleaseSharedLock(&afs_xvcache);
1686 afs_stats_cmperf.vcacheMisses++;
1689 ReleaseSharedLock(&afs_xvcache);
1691 ObtainWriteLock(&tvc->lock, 54);
1693 if (tvc->f.states & CStatd) {
1694 ReleaseWriteLock(&tvc->lock);
1697 #ifdef AFS_DARWIN80_ENV
1698 /* Darwin 8.0 only has bufs in nfs, so we shouldn't have to worry about them.
1701 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
1703 * XXX - I really don't like this. Should try to understand better.
1704 * It seems that sometimes, when we get called, we already hold the
1705 * lock on the vnode (e.g., from afs_getattr via afs_VerifyVCache).
1706 * We can't drop the vnode lock, because that could result in a race.
1707 * Sometimes, though, we get here and don't hold the vnode lock.
1708 * I hate code paths that sometimes hold locks and sometimes don't.
1709 * In any event, the dodge we use here is to check whether the vnode
1710 * is locked, and if it isn't, then we gain and drop it around the call
1711 * to vinvalbuf; otherwise, we leave it alone.
1714 struct vnode *vp = AFSTOV(tvc);
1717 #if defined(AFS_DARWIN_ENV)
1718 iheldthelock = VOP_ISLOCKED(vp);
1720 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, current_proc());
1721 /* this is messy. we can call fsync which will try to reobtain this */
1722 if (VTOAFS(vp) == tvc)
1723 ReleaseWriteLock(&tvc->lock);
1724 if (UBCINFOEXISTS(vp)) {
1725 vinvalbuf(vp, V_SAVE, &afs_osi_cred, current_proc(), PINOD, 0);
1727 if (VTOAFS(vp) == tvc)
1728 ObtainWriteLock(&tvc->lock, 954);
1730 VOP_UNLOCK(vp, LK_EXCLUSIVE, current_proc());
1731 #elif defined(AFS_FBSD80_ENV)
1732 iheldthelock = VOP_ISLOCKED(vp);
1733 if (!iheldthelock) {
1734 /* nosleep/sleep lock order reversal */
1735 int glocked = ISAFS_GLOCK();
1738 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1742 vinvalbuf(vp, V_SAVE, PINOD, 0); /* changed late in 8.0-CURRENT */
1745 #elif defined(AFS_FBSD60_ENV)
1746 iheldthelock = VOP_ISLOCKED(vp, curthread);
1748 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1750 vinvalbuf(vp, V_SAVE, curthread, PINOD, 0);
1753 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1754 #elif defined(AFS_FBSD_ENV)
1755 iheldthelock = VOP_ISLOCKED(vp, curthread);
1757 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1758 vinvalbuf(vp, V_SAVE, osi_curcred(), curthread, PINOD, 0);
1760 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1761 #elif defined(AFS_OBSD_ENV)
1762 iheldthelock = VOP_ISLOCKED(vp, curproc);
1764 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1765 uvm_vnp_uncache(vp);
1767 VOP_UNLOCK(vp, 0, curproc);
1768 #elif defined(AFS_NBSD40_ENV)
1769 iheldthelock = VOP_ISLOCKED(vp);
1770 if (!iheldthelock) {
1771 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1773 uvm_vnp_uncache(vp);
1781 ObtainWriteLock(&afs_xcbhash, 464);
1782 tvc->f.states &= ~CUnique;
1784 afs_DequeueCallback(tvc);
1785 ReleaseWriteLock(&afs_xcbhash);
1787 /* It is always appropriate to throw away all the access rights? */
1788 afs_FreeAllAxs(&(tvc->Access));
1789 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1791 if ((tvp->states & VForeign)) {
1793 tvc->f.states |= CForeign;
1794 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1795 && (tvp->rootUnique == afid->Fid.Unique)) {
1799 if (tvp->states & VRO)
1800 tvc->f.states |= CRO;
1801 if (tvp->states & VBackup)
1802 tvc->f.states |= CBackup;
1803 /* now copy ".." entry back out of volume structure, if necessary */
1804 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1806 tvc->mvid = (struct VenusFid *)
1807 osi_AllocSmallSpace(sizeof(struct VenusFid));
1808 *tvc->mvid = tvp->dotdot;
1810 afs_PutVolume(tvp, READ_LOCK);
1814 afs_RemoveVCB(afid);
1816 struct AFSFetchStatus OutStatus;
1818 if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1819 afs_ProcessFS(tvc, &OutStatus, areq);
1820 tvc->f.states |= CStatd | CUnique;
1821 tvc->f.parent.vnode = OutStatus.ParentVnode;
1822 tvc->f.parent.unique = OutStatus.ParentUnique;
1826 if (AFS_IS_DISCONNECTED) {
1827 /* Nothing to do otherwise...*/
1829 /* printf("Network is down in afs_GetCache"); */
1831 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1833 /* For the NFS translator's benefit, make sure
1834 * non-directory vnodes always have their parent FID set
1835 * correctly, even when created as a result of decoding an
1836 * NFS filehandle. It would be nice to also do this for
1837 * directories, but we can't because the fileserver fills
1838 * in the FID of the directory itself instead of that of
1841 if (!code && OutStatus.FileType != Directory &&
1842 !tvc->f.parent.vnode) {
1843 tvc->f.parent.vnode = OutStatus.ParentVnode;
1844 tvc->f.parent.unique = OutStatus.ParentUnique;
1845 /* XXX - SXW - It's conceivable we should mark ourselves
1846 * as dirty again here, incase we've been raced
1847 * out of the FetchStatus call.
1854 ReleaseWriteLock(&tvc->lock);
1860 ReleaseWriteLock(&tvc->lock);
1863 } /*afs_GetVCache */
1868 * Lookup a vcache by fid. Look inside the cache first, if not
1869 * there, lookup the file on the server, and then get it's fresh
1874 * \param cached Is element cached? If NULL, don't answer.
1878 * \return The found element or NULL.
1881 afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1882 afs_int32 * cached, struct vcache *adp, char *aname)
1884 afs_int32 code, now, newvcache = 0;
1885 struct VenusFid nfid;
1888 struct AFSFetchStatus OutStatus;
1889 struct AFSCallBack CallBack;
1890 struct AFSVolSync tsync;
1891 struct server *serverp = 0;
1895 AFS_STATCNT(afs_GetVCache);
1897 *cached = 0; /* Init just in case */
1899 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1903 ObtainReadLock(&afs_xvcache);
1904 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */ );
1907 ReleaseReadLock(&afs_xvcache);
1909 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1910 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1914 ObtainReadLock(&tvc->lock);
1916 if (tvc->f.states & CStatd) {
1920 ReleaseReadLock(&tvc->lock);
1923 tvc->f.states &= ~CUnique;
1925 ReleaseReadLock(&tvc->lock);
1927 ObtainReadLock(&afs_xvcache);
1930 ReleaseReadLock(&afs_xvcache);
1932 /* lookup the file */
1935 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
1937 if (AFS_IS_DISCONNECTED) {
1938 /* printf("Network is down in afs_LookupVcache\n"); */
1942 afs_RemoteLookup(&adp->f.fid, areq, aname, &nfid, &OutStatus,
1943 &CallBack, &serverp, &tsync);
1945 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1949 ObtainSharedLock(&afs_xvcache, 6);
1950 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU | IS_SLOCK/* no xstats now */ );
1952 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1953 ReleaseSharedLock(&afs_xvcache);
1954 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1960 /* no cache entry, better grab one */
1961 UpgradeSToWLock(&afs_xvcache, 22);
1962 tvc = afs_NewVCache(&nfid, serverp);
1964 ConvertWToSLock(&afs_xvcache);
1967 ReleaseSharedLock(&afs_xvcache);
1972 ReleaseSharedLock(&afs_xvcache);
1973 ObtainWriteLock(&tvc->lock, 55);
1975 /* It is always appropriate to throw away all the access rights? */
1976 afs_FreeAllAxs(&(tvc->Access));
1977 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
1979 if ((tvp->states & VForeign)) {
1981 tvc->f.states |= CForeign;
1982 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1983 && (tvp->rootUnique == afid->Fid.Unique))
1986 if (tvp->states & VRO)
1987 tvc->f.states |= CRO;
1988 if (tvp->states & VBackup)
1989 tvc->f.states |= CBackup;
1990 /* now copy ".." entry back out of volume structure, if necessary */
1991 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1993 tvc->mvid = (struct VenusFid *)
1994 osi_AllocSmallSpace(sizeof(struct VenusFid));
1995 *tvc->mvid = tvp->dotdot;
2000 ObtainWriteLock(&afs_xcbhash, 465);
2001 afs_DequeueCallback(tvc);
2002 tvc->f.states &= ~(CStatd | CUnique);
2003 ReleaseWriteLock(&afs_xcbhash);
2004 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2005 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2007 afs_PutVolume(tvp, READ_LOCK);
2008 ReleaseWriteLock(&tvc->lock);
2013 ObtainWriteLock(&afs_xcbhash, 466);
2014 if (origCBs == afs_allCBs) {
2015 if (CallBack.ExpirationTime) {
2016 tvc->callback = serverp;
2017 tvc->cbExpires = CallBack.ExpirationTime + now;
2018 tvc->f.states |= CStatd | CUnique;
2019 tvc->f.states &= ~CBulkFetching;
2020 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
2021 } else if (tvc->f.states & CRO) {
2022 /* adapt gives us an hour. */
2023 tvc->cbExpires = 3600 + osi_Time();
2024 /*XXX*/ tvc->f.states |= CStatd | CUnique;
2025 tvc->f.states &= ~CBulkFetching;
2026 afs_QueueCallback(tvc, CBHash(3600), tvp);
2028 tvc->callback = NULL;
2029 afs_DequeueCallback(tvc);
2030 tvc->f.states &= ~(CStatd | CUnique);
2031 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2032 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2035 afs_DequeueCallback(tvc);
2036 tvc->f.states &= ~CStatd;
2037 tvc->f.states &= ~CUnique;
2038 tvc->callback = NULL;
2039 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2040 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2042 ReleaseWriteLock(&afs_xcbhash);
2044 afs_PutVolume(tvp, READ_LOCK);
2045 afs_ProcessFS(tvc, &OutStatus, areq);
2047 ReleaseWriteLock(&tvc->lock);
2053 afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
2054 afs_int32 * cached, struct volume *tvolp)
2056 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
2057 afs_int32 getNewFid = 0;
2059 struct VenusFid nfid;
2061 struct server *serverp = 0;
2062 struct AFSFetchStatus OutStatus;
2063 struct AFSCallBack CallBack;
2064 struct AFSVolSync tsync;
2066 #ifdef AFS_DARWIN80_ENV
2073 if (!tvolp->rootVnode || getNewFid) {
2074 struct VenusFid tfid;
2077 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2078 origCBs = afs_allCBs; /* ignore InitCallBackState */
2080 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2085 /* ReleaseReadLock(&tvolp->lock); */
2086 ObtainWriteLock(&tvolp->lock, 56);
2087 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
2088 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
2089 ReleaseWriteLock(&tvolp->lock);
2090 /* ObtainReadLock(&tvolp->lock);*/
2093 afid->Fid.Vnode = tvolp->rootVnode;
2094 afid->Fid.Unique = tvolp->rootUnique;
2098 ObtainSharedLock(&afs_xvcache, 7);
2100 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2101 if (!FidCmp(&(tvc->f.fid), afid)) {
2102 if (tvc->f.states & CVInit) {
2103 ReleaseSharedLock(&afs_xvcache);
2104 afs_osi_Sleep(&tvc->f.states);
2107 #ifdef AFS_DARWIN80_ENV
2108 if (tvc->f.states & CDeadVnode) {
2109 ReleaseSharedLock(&afs_xvcache);
2110 afs_osi_Sleep(&tvc->f.states);
2114 if (vnode_get(tvp)) /* this bumps ref count */
2116 if (vnode_ref(tvp)) {
2118 /* AFSTOV(tvc) may be NULL */
2128 if (!haveStatus && (!tvc || !(tvc->f.states & CStatd))) {
2129 /* Mount point no longer stat'd or unknown. FID may have changed. */
2131 ReleaseSharedLock(&afs_xvcache);
2132 #ifdef AFS_DARWIN80_ENV
2135 vnode_put(AFSTOV(tvc));
2136 vnode_rele(AFSTOV(tvc));
2145 UpgradeSToWLock(&afs_xvcache, 23);
2146 /* no cache entry, better grab one */
2147 tvc = afs_NewVCache(afid, NULL);
2150 ReleaseWriteLock(&afs_xvcache);
2154 afs_stats_cmperf.vcacheMisses++;
2158 afs_stats_cmperf.vcacheHits++;
2159 #if defined(AFS_DARWIN80_ENV)
2160 /* we already bumped the ref count in the for loop above */
2161 #else /* AFS_DARWIN80_ENV */
2164 UpgradeSToWLock(&afs_xvcache, 24);
2165 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2166 refpanic("GRVC VLRU inconsistent0");
2168 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2169 refpanic("GRVC VLRU inconsistent1");
2171 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2172 refpanic("GRVC VLRU inconsistent2");
2174 QRemove(&tvc->vlruq); /* move to lruq head */
2175 QAdd(&VLRU, &tvc->vlruq);
2176 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2177 refpanic("GRVC VLRU inconsistent3");
2179 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2180 refpanic("GRVC VLRU inconsistent4");
2182 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2183 refpanic("GRVC VLRU inconsistent5");
2188 ReleaseWriteLock(&afs_xvcache);
2190 if (tvc->f.states & CStatd) {
2194 ObtainReadLock(&tvc->lock);
2195 tvc->f.states &= ~CUnique;
2196 tvc->callback = NULL; /* redundant, perhaps */
2197 ReleaseReadLock(&tvc->lock);
2200 ObtainWriteLock(&tvc->lock, 57);
2202 /* It is always appropriate to throw away all the access rights? */
2203 afs_FreeAllAxs(&(tvc->Access));
2206 tvc->f.states |= CForeign;
2207 if (tvolp->states & VRO)
2208 tvc->f.states |= CRO;
2209 if (tvolp->states & VBackup)
2210 tvc->f.states |= CBackup;
2211 /* now copy ".." entry back out of volume structure, if necessary */
2212 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2213 && (tvolp->rootUnique == afid->Fid.Unique)) {
2216 if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2218 tvc->mvid = (struct VenusFid *)
2219 osi_AllocSmallSpace(sizeof(struct VenusFid));
2220 *tvc->mvid = tvolp->dotdot;
2224 afs_RemoveVCB(afid);
2227 struct VenusFid tfid;
2230 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2231 origCBs = afs_allCBs; /* ignore InitCallBackState */
2233 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2238 ObtainWriteLock(&afs_xcbhash, 467);
2239 afs_DequeueCallback(tvc);
2240 tvc->callback = NULL;
2241 tvc->f.states &= ~(CStatd | CUnique);
2242 ReleaseWriteLock(&afs_xcbhash);
2243 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2244 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2245 ReleaseWriteLock(&tvc->lock);
2250 ObtainWriteLock(&afs_xcbhash, 468);
2251 if (origCBs == afs_allCBs) {
2252 tvc->f.states |= CTruth;
2253 tvc->callback = serverp;
2254 if (CallBack.ExpirationTime != 0) {
2255 tvc->cbExpires = CallBack.ExpirationTime + start;
2256 tvc->f.states |= CStatd;
2257 tvc->f.states &= ~CBulkFetching;
2258 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2259 } else if (tvc->f.states & CRO) {
2260 /* adapt gives us an hour. */
2261 tvc->cbExpires = 3600 + osi_Time();
2262 /*XXX*/ tvc->f.states |= CStatd;
2263 tvc->f.states &= ~CBulkFetching;
2264 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2267 afs_DequeueCallback(tvc);
2268 tvc->callback = NULL;
2269 tvc->f.states &= ~(CStatd | CUnique);
2270 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2271 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2273 ReleaseWriteLock(&afs_xcbhash);
2274 afs_ProcessFS(tvc, &OutStatus, areq);
2276 ReleaseWriteLock(&tvc->lock);
2282 * Update callback status and (sometimes) attributes of a vnode.
2283 * Called after doing a fetch status RPC. Whilst disconnected, attributes
2284 * shouldn't be written to the vcache here.
2289 * \param Outsp Server status after rpc call.
2290 * \param acb Callback for this vnode.
2292 * \note The vcache must be write locked.
2295 afs_UpdateStatus(struct vcache *avc, struct VenusFid *afid,
2296 struct vrequest *areq, struct AFSFetchStatus *Outsp,
2297 struct AFSCallBack *acb, afs_uint32 start)
2299 struct volume *volp;
2302 /* Dont write status in vcache if resyncing after a disconnection. */
2303 afs_ProcessFS(avc, Outsp, areq);
2305 volp = afs_GetVolume(afid, areq, READ_LOCK);
2306 ObtainWriteLock(&afs_xcbhash, 469);
2307 avc->f.states |= CTruth;
2308 if (avc->callback /* check for race */ ) {
2309 if (acb->ExpirationTime != 0) {
2310 avc->cbExpires = acb->ExpirationTime + start;
2311 avc->f.states |= CStatd;
2312 avc->f.states &= ~CBulkFetching;
2313 afs_QueueCallback(avc, CBHash(acb->ExpirationTime), volp);
2314 } else if (avc->f.states & CRO) {
2315 /* ordinary callback on a read-only volume -- AFS 3.2 style */
2316 avc->cbExpires = 3600 + start;
2317 avc->f.states |= CStatd;
2318 avc->f.states &= ~CBulkFetching;
2319 afs_QueueCallback(avc, CBHash(3600), volp);
2321 afs_DequeueCallback(avc);
2322 avc->callback = NULL;
2323 avc->f.states &= ~(CStatd | CUnique);
2324 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
2325 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2328 afs_DequeueCallback(avc);
2329 avc->callback = NULL;
2330 avc->f.states &= ~(CStatd | CUnique);
2331 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
2332 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2334 ReleaseWriteLock(&afs_xcbhash);
2336 afs_PutVolume(volp, READ_LOCK);
2340 afs_BadFetchStatus(struct afs_conn *tc)
2342 int addr = ntohl(tc->parent->srvr->sa_ip);
2343 afs_warn("afs: Invalid AFSFetchStatus from server %u.%u.%u.%u\n",
2344 (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff,
2346 afs_warn("afs: This suggests the server may be sending bad data that "
2347 "can lead to availability issues or data corruption. The "
2348 "issue has been avoided for now, but it may not always be "
2349 "detectable. Please upgrade the server if possible.\n");
2353 * Check if a given AFSFetchStatus structure is sane.
2355 * @param[in] tc The server from which we received the status
2356 * @param[in] status The status we received
2358 * @return whether the given structure is valid or not
2359 * @retval 0 the structure is fine
2360 * @retval nonzero the structure looks like garbage; act as if we received
2361 * the returned error code from the server
2364 afs_CheckFetchStatus(struct afs_conn *tc, struct AFSFetchStatus *status)
2366 if (status->errorCode ||
2367 status->InterfaceVersion != 1 ||
2368 !(status->FileType > Invalid && status->FileType <= SymbolicLink) ||
2369 status->ParentVnode == 0 || status->ParentUnique == 0) {
2371 afs_warn("afs: FetchStatus ec %u iv %u ft %u pv %u pu %u\n",
2372 (unsigned)status->errorCode, (unsigned)status->InterfaceVersion,
2373 (unsigned)status->FileType, (unsigned)status->ParentVnode,
2374 (unsigned)status->ParentUnique);
2375 afs_BadFetchStatus(tc);
2383 * Must be called with avc write-locked
2384 * don't absolutely have to invalidate the hint unless the dv has
2385 * changed, but be sure to get it right else there will be consistency bugs.
2388 afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
2389 struct vrequest * areq, struct AFSFetchStatus * Outsp)
2392 afs_uint32 start = 0;
2393 struct afs_conn *tc;
2394 struct AFSCallBack CallBack;
2395 struct AFSVolSync tsync;
2396 struct rx_connection *rxconn;
2399 tc = afs_Conn(afid, areq, SHARED_LOCK, &rxconn);
2400 avc->dchint = NULL; /* invalidate hints */
2402 avc->callback = tc->parent->srvr->server;
2404 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2407 RXAFS_FetchStatus(rxconn, (struct AFSFid *)&afid->Fid, Outsp,
2414 code = afs_CheckFetchStatus(tc, Outsp);
2419 } while (afs_Analyze
2420 (tc, rxconn, code, afid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2421 SHARED_LOCK, NULL));
2424 afs_UpdateStatus(avc, afid, areq, Outsp, &CallBack, start);
2426 /* used to undo the local callback, but that's too extreme.
2427 * There are plenty of good reasons that fetchstatus might return
2428 * an error, such as EPERM. If we have the vnode cached, statd,
2429 * with callback, might as well keep track of the fact that we
2430 * don't have access...
2432 if (code == EPERM || code == EACCES) {
2433 struct axscache *ac;
2434 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2436 else /* not found, add a new one if possible */
2437 afs_AddAxs(avc->Access, areq->uid, 0);
2448 * Stuff some information into the vcache for the given file.
2451 * afid : File in question.
2452 * OutStatus : Fetch status on the file.
2453 * CallBack : Callback info.
2454 * tc : RPC connection involved.
2455 * areq : vrequest involved.
2458 * Nothing interesting.
2461 afs_StuffVcache(struct VenusFid *afid,
2462 struct AFSFetchStatus *OutStatus,
2463 struct AFSCallBack *CallBack, struct afs_conn *tc,
2464 struct vrequest *areq)
2466 afs_int32 code, i, newvcache = 0;
2468 struct AFSVolSync tsync;
2470 struct axscache *ac;
2473 AFS_STATCNT(afs_StuffVcache);
2474 #ifdef IFS_VCACHECOUNT
2479 ObtainSharedLock(&afs_xvcache, 8);
2481 tvc = afs_FindVCache(afid, &retry, DO_VLRU| IS_SLOCK /* no stats */ );
2483 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2484 ReleaseSharedLock(&afs_xvcache);
2485 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2491 /* no cache entry, better grab one */
2492 UpgradeSToWLock(&afs_xvcache, 25);
2493 tvc = afs_NewVCache(afid, NULL);
2495 ConvertWToSLock(&afs_xvcache);
2498 ReleaseSharedLock(&afs_xvcache);
2503 ReleaseSharedLock(&afs_xvcache);
2504 ObtainWriteLock(&tvc->lock, 58);
2506 tvc->f.states &= ~CStatd;
2507 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2508 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2510 /* Is it always appropriate to throw away all the access rights? */
2511 afs_FreeAllAxs(&(tvc->Access));
2513 /*Copy useful per-volume info */
2514 tvp = afs_GetVolume(afid, areq, READ_LOCK);
2516 if (newvcache && (tvp->states & VForeign))
2517 tvc->f.states |= CForeign;
2518 if (tvp->states & VRO)
2519 tvc->f.states |= CRO;
2520 if (tvp->states & VBackup)
2521 tvc->f.states |= CBackup;
2523 * Now, copy ".." entry back out of volume structure, if
2526 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2528 tvc->mvid = (struct VenusFid *)
2529 osi_AllocSmallSpace(sizeof(struct VenusFid));
2530 *tvc->mvid = tvp->dotdot;
2533 /* store the stat on the file */
2534 afs_RemoveVCB(afid);
2535 afs_ProcessFS(tvc, OutStatus, areq);
2536 tvc->callback = tc->srvr->server;
2538 /* we use osi_Time twice below. Ideally, we would use the time at which
2539 * the FetchStatus call began, instead, but we don't have it here. So we
2540 * make do with "now". In the CRO case, it doesn't really matter. In
2541 * the other case, we hope that the difference between "now" and when the
2542 * call actually began execution on the server won't be larger than the
2543 * padding which the server keeps. Subtract 1 second anyway, to be on
2544 * the safe side. Can't subtract more because we don't know how big
2545 * ExpirationTime is. Possible consistency problems may arise if the call
2546 * timeout period becomes longer than the server's expiration padding. */
2547 ObtainWriteLock(&afs_xcbhash, 470);
2548 if (CallBack->ExpirationTime != 0) {
2549 tvc->cbExpires = CallBack->ExpirationTime + osi_Time() - 1;
2550 tvc->f.states |= CStatd;
2551 tvc->f.states &= ~CBulkFetching;
2552 afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2553 } else if (tvc->f.states & CRO) {
2554 /* old-fashioned AFS 3.2 style */
2555 tvc->cbExpires = 3600 + osi_Time();
2556 /*XXX*/ tvc->f.states |= CStatd;
2557 tvc->f.states &= ~CBulkFetching;
2558 afs_QueueCallback(tvc, CBHash(3600), tvp);
2560 afs_DequeueCallback(tvc);
2561 tvc->callback = NULL;
2562 tvc->f.states &= ~(CStatd | CUnique);
2563 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2564 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2566 ReleaseWriteLock(&afs_xcbhash);
2568 afs_PutVolume(tvp, READ_LOCK);
2570 /* look in per-pag cache */
2571 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2572 ac->axess = OutStatus->CallerAccess; /* substitute pags */
2573 else /* not found, add a new one if possible */
2574 afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2576 ReleaseWriteLock(&tvc->lock);
2577 afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2578 ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32,
2579 tvc->cbExpires, ICL_TYPE_INT32, tvc->cbExpires - osi_Time());
2581 * Release ref count... hope this guy stays around...
2584 } /*afs_StuffVcache */
2588 * Decrements the reference count on a cache entry.
2590 * \param avc Pointer to the cache entry to decrement.
2592 * \note Environment: Nothing interesting.
2595 afs_PutVCache(struct vcache *avc)
2597 AFS_STATCNT(afs_PutVCache);
2598 #ifdef AFS_DARWIN80_ENV
2599 vnode_put(AFSTOV(avc));
2603 * Can we use a read lock here?
2605 ObtainReadLock(&afs_xvcache);
2607 ReleaseReadLock(&afs_xvcache);
2609 } /*afs_PutVCache */
2613 * Reset a vcache entry, so local contents are ignored, and the
2614 * server will be reconsulted next time the vcache is used
2616 * \param avc Pointer to the cache entry to reset
2618 * \param skipdnlc skip the dnlc purge for this vnode
2620 * \note avc must be write locked on entry
2622 * \note The caller should purge the dnlc when skipdnlc is set.
2625 afs_ResetVCache(struct vcache *avc, afs_ucred_t *acred, afs_int32 skipdnlc)
2627 ObtainWriteLock(&afs_xcbhash, 456);
2628 afs_DequeueCallback(avc);
2629 avc->f.states &= ~(CStatd | CDirty); /* next reference will re-stat */
2630 ReleaseWriteLock(&afs_xcbhash);
2631 /* now find the disk cache entries */
2632 afs_TryToSmush(avc, acred, 1);
2634 osi_dnlc_purgedp(avc);
2636 if (avc->linkData && !(avc->f.states & CCore)) {
2637 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
2638 avc->linkData = NULL;
2643 * Sleepa when searching for a vcache. Releases all the pending locks,
2644 * sleeps then obtains the previously released locks.
2646 * \param vcache Enter sleep state.
2647 * \param flag Determines what locks to use.
2652 findvc_sleep(struct vcache *avc, int flag)
2654 if (flag & IS_SLOCK) {
2655 ReleaseSharedLock(&afs_xvcache);
2657 if (flag & IS_WLOCK) {
2658 ReleaseWriteLock(&afs_xvcache);
2660 ReleaseReadLock(&afs_xvcache);
2663 afs_osi_Sleep(&avc->f.states);
2664 if (flag & IS_SLOCK) {
2665 ObtainSharedLock(&afs_xvcache, 341);
2667 if (flag & IS_WLOCK) {
2668 ObtainWriteLock(&afs_xvcache, 343);
2670 ObtainReadLock(&afs_xvcache);
2676 * Add a reference on an existing vcache entry.
2678 * \param tvc Pointer to the vcache.
2680 * \note Environment: Must be called with at least one reference from
2681 * elsewhere on the vcache, even if that reference will be dropped.
2682 * The global lock is required.
2684 * \return 0 on success, -1 on failure.
2688 afs_RefVCache(struct vcache *tvc)
2690 #ifdef AFS_DARWIN80_ENV
2694 /* AFS_STATCNT(afs_RefVCache); */
2696 #ifdef AFS_DARWIN80_ENV
2700 if (vnode_ref(tvp)) {
2702 /* AFSTOV(tvc) may be NULL */
2711 } /*afs_RefVCache */
2714 * Find a vcache entry given a fid.
2716 * \param afid Pointer to the fid whose cache entry we desire.
2717 * \param retry (SGI-specific) tell the caller to drop the lock on xvcache,
2718 * unlock the vnode, and try again.
2719 * \param flag Bit 1 to specify whether to compute hit statistics. Not
2720 * set if FindVCache is called as part of internal bookkeeping.
2722 * \note Environment: Must be called with the afs_xvcache lock at least held at
2723 * the read level. In order to do the VLRU adjustment, the xvcache lock
2724 * must be shared-- we upgrade it here.
2728 afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
2733 #ifdef AFS_DARWIN80_ENV
2734 struct vcache *deadvc = NULL, *livevc = NULL;
2738 AFS_STATCNT(afs_FindVCache);
2742 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2743 if (FidMatches(afid, tvc)) {
2744 if (tvc->f.states & CVInit) {
2745 findvc_sleep(tvc, flag);
2748 #ifdef AFS_DARWIN80_ENV
2749 if (tvc->f.states & CDeadVnode) {
2750 findvc_sleep(tvc, flag);
2758 /* should I have a read lock on the vnode here? */
2762 #if defined(AFS_DARWIN80_ENV)
2766 if (tvp && vnode_ref(tvp)) {
2768 /* AFSTOV(tvc) may be NULL */
2777 #elif defined(AFS_DARWIN_ENV)
2778 tvc->f.states |= CUBCinit;
2780 if (UBCINFOMISSING(AFSTOV(tvc)) ||
2781 UBCINFORECLAIMED(AFSTOV(tvc))) {
2782 ubc_info_init(AFSTOV(tvc));
2785 tvc->f.states &= ~CUBCinit;
2787 osi_vnhold(tvc, retry); /* already held, above */
2788 if (retry && *retry)
2792 * only move to front of vlru if we have proper vcache locking)
2794 if (flag & DO_VLRU) {
2795 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2796 refpanic("FindVC VLRU inconsistent1");
2798 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2799 refpanic("FindVC VLRU inconsistent1");
2801 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2802 refpanic("FindVC VLRU inconsistent2");
2804 UpgradeSToWLock(&afs_xvcache, 26);
2805 QRemove(&tvc->vlruq);
2806 QAdd(&VLRU, &tvc->vlruq);
2807 ConvertWToSLock(&afs_xvcache);
2808 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2809 refpanic("FindVC VLRU inconsistent1");
2811 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2812 refpanic("FindVC VLRU inconsistent2");
2814 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2815 refpanic("FindVC VLRU inconsistent3");
2821 if (flag & DO_STATS) {
2823 afs_stats_cmperf.vcacheHits++;
2825 afs_stats_cmperf.vcacheMisses++;
2826 if (afs_IsPrimaryCellNum(afid->Cell))
2827 afs_stats_cmperf.vlocalAccesses++;
2829 afs_stats_cmperf.vremoteAccesses++;
2832 } /*afs_FindVCache */
2835 * Find a vcache entry given a fid. Does a wildcard match on what we
2836 * have for the fid. If more than one entry, don't return anything.
2838 * \param avcp Fill in pointer if we found one and only one.
2839 * \param afid Pointer to the fid whose cache entry we desire.
2840 * \param retry (SGI-specific) tell the caller to drop the lock on xvcache,
2841 * unlock the vnode, and try again.
2842 * \param flags bit 1 to specify whether to compute hit statistics. Not
2843 * set if FindVCache is called as part of internal bookkeeping.
2845 * \note Environment: Must be called with the afs_xvcache lock at least held at
2846 * the read level. In order to do the VLRU adjustment, the xvcache lock
2847 * must be shared-- we upgrade it here.
2849 * \return Number of matches found.
2852 int afs_duplicate_nfs_fids = 0;
2855 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2859 afs_int32 count = 0;
2860 struct vcache *found_tvc = NULL;
2861 #ifdef AFS_DARWIN80_ENV
2865 AFS_STATCNT(afs_FindVCache);
2869 ObtainSharedLock(&afs_xvcache, 331);
2872 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2873 /* Match only on what we have.... */
2874 if (((tvc->f.fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2875 && (tvc->f.fid.Fid.Volume == afid->Fid.Volume)
2876 && ((tvc->f.fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2877 && (tvc->f.fid.Cell == afid->Cell)) {
2878 if (tvc->f.states & CVInit) {
2879 ReleaseSharedLock(&afs_xvcache);
2880 afs_osi_Sleep(&tvc->f.states);
2883 #ifdef AFS_DARWIN80_ENV
2884 if (tvc->f.states & CDeadVnode) {
2885 ReleaseSharedLock(&afs_xvcache);
2886 afs_osi_Sleep(&tvc->f.states);
2890 if (vnode_get(tvp)) {
2891 /* This vnode no longer exists. */
2894 if (vnode_ref(tvp)) {
2895 /* This vnode no longer exists. */
2897 /* AFSTOV(tvc) may be NULL */
2902 #endif /* AFS_DARWIN80_ENV */
2906 afs_duplicate_nfs_fids++;
2907 ReleaseSharedLock(&afs_xvcache);
2908 #ifdef AFS_DARWIN80_ENV
2909 /* Drop our reference counts. */
2910 vnode_put(AFSTOV(tvc));
2911 vnode_put(AFSTOV(found_tvc));
2920 /* should I have a read lock on the vnode here? */
2922 #ifndef AFS_DARWIN80_ENV
2923 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2924 afs_int32 retry = 0;
2925 osi_vnhold(tvc, &retry);
2928 found_tvc = (struct vcache *)0;
2929 ReleaseSharedLock(&afs_xvcache);
2930 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2934 osi_vnhold(tvc, (int *)0); /* already held, above */
2938 * We obtained the xvcache lock above.
2940 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2941 refpanic("FindVC VLRU inconsistent1");
2943 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2944 refpanic("FindVC VLRU inconsistent1");
2946 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2947 refpanic("FindVC VLRU inconsistent2");
2949 UpgradeSToWLock(&afs_xvcache, 568);
2950 QRemove(&tvc->vlruq);
2951 QAdd(&VLRU, &tvc->vlruq);
2952 ConvertWToSLock(&afs_xvcache);
2953 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2954 refpanic("FindVC VLRU inconsistent1");
2956 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2957 refpanic("FindVC VLRU inconsistent2");
2959 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2960 refpanic("FindVC VLRU inconsistent3");
2966 afs_stats_cmperf.vcacheHits++;
2968 afs_stats_cmperf.vcacheMisses++;
2969 if (afs_IsPrimaryCellNum(afid->Cell))
2970 afs_stats_cmperf.vlocalAccesses++;
2972 afs_stats_cmperf.vremoteAccesses++;
2974 *avcp = tvc; /* May be null */
2976 ReleaseSharedLock(&afs_xvcache);
2977 return (tvc ? 1 : 0);
2979 } /*afs_NFSFindVCache */
2985 * Initialize vcache related variables
2990 afs_vcacheInit(int astatSize)
2992 #if !defined(AFS_LINUX22_ENV)
2996 if (!afs_maxvcount) {
2997 afs_maxvcount = astatSize; /* no particular limit on linux? */
2999 #if !defined(AFS_LINUX22_ENV)
3003 AFS_RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
3004 LOCK_INIT(&afs_xvcb, "afs_xvcb");
3006 #if !defined(AFS_LINUX22_ENV)
3007 /* Allocate and thread the struct vcache entries */
3008 tvp = afs_osi_Alloc(astatSize * sizeof(struct vcache));
3009 osi_Assert(tvp != NULL);
3010 memset(tvp, 0, sizeof(struct vcache) * astatSize);
3012 Initial_freeVCList = tvp;
3013 freeVCList = &(tvp[0]);
3014 for (i = 0; i < astatSize - 1; i++) {
3015 tvp[i].nextfree = &(tvp[i + 1]);
3017 tvp[astatSize - 1].nextfree = NULL;
3018 # ifdef KERNEL_HAVE_PIN
3019 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
3023 #if defined(AFS_SGI_ENV)
3024 for (i = 0; i < astatSize; i++) {
3025 char name[METER_NAMSZ];
3026 struct vcache *tvc = &tvp[i];
3028 tvc->v.v_number = ++afsvnumbers;
3029 tvc->vc_rwlockid = OSI_NO_LOCKID;
3030 initnsema(&tvc->vc_rwlock, 1,
3031 makesname(name, "vrw", tvc->v.v_number));
3032 #ifndef AFS_SGI53_ENV
3033 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
3035 #ifndef AFS_SGI62_ENV
3036 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
3037 #endif /* AFS_SGI62_ENV */
3041 for(i = 0; i < VCSIZE; ++i)
3042 QInit(&afs_vhashTV[i]);
3049 shutdown_vcache(void)
3052 struct afs_cbr *tsp;
3054 * XXX We may potentially miss some of the vcaches because if when
3055 * there are no free vcache entries and all the vcache entries are active
3056 * ones then we allocate an additional one - admittedly we almost never
3061 struct afs_q *tq, *uq = NULL;
3063 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
3067 osi_FreeSmallSpace(tvc->mvid);
3068 tvc->mvid = (struct VenusFid *)0;
3071 aix_gnode_rele(AFSTOV(tvc));
3073 if (tvc->linkData) {
3074 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3079 * Also free the remaining ones in the Cache
3081 for (i = 0; i < VCSIZE; i++) {
3082 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3084 osi_FreeSmallSpace(tvc->mvid);
3085 tvc->mvid = (struct VenusFid *)0;
3089 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
3090 #ifdef AFS_AIX32_ENV
3093 vms_delete(tvc->segid);
3095 tvc->segid = tvc->vmh = NULL;
3096 if (VREFCOUNT_GT(tvc,0))
3097 osi_Panic("flushVcache: vm race");
3105 #if defined(AFS_SUN5_ENV)
3111 if (tvc->linkData) {
3112 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3117 afs_FreeAllAxs(&(tvc->Access));
3123 * Free any leftover callback queue
3125 for (i = 0; i < afs_stats_cmperf.CallBackAlloced; i++) {
3126 tsp = afs_cbrHeads[i];
3127 afs_cbrHeads[i] = 0;
3128 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
3132 #if !defined(AFS_LINUX22_ENV)
3133 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3135 # ifdef KERNEL_HAVE_PIN
3136 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3139 freeVCList = Initial_freeVCList = 0;
3142 AFS_RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
3143 LOCK_INIT(&afs_xvcb, "afs_xvcb");
3145 for(i = 0; i < VCSIZE; ++i)
3146 QInit(&afs_vhashTV[i]);
3150 afs_DisconGiveUpCallbacks(void)
3156 ObtainWriteLock(&afs_xvcache, 1002); /* XXX - should be a unique number */
3159 /* Somehow, walk the set of vcaches, with each one coming out as tvc */
3160 for (i = 0; i < VCSIZE; i++) {
3161 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3163 if (afs_QueueVCB(tvc, &slept)) {
3164 tvc->callback = NULL;
3173 ReleaseWriteLock(&afs_xvcache);
3180 * Clear the Statd flag from all vcaches
3182 * This function removes the Statd flag from all vcaches. It's used by
3183 * disconnected mode to tidy up during reconnection
3187 afs_ClearAllStatdFlag(void)
3192 ObtainWriteLock(&afs_xvcache, 715);
3194 for (i = 0; i < VCSIZE; i++) {
3195 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3196 tvc->f.states &= ~(CStatd|CUnique);
3199 ReleaseWriteLock(&afs_xvcache);