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);
89 * Generate an index into the hash table for a given Fid.
91 * \return The hash value.
94 afs_HashCBRFid(struct AFSFid *fid)
96 return (fid->Volume + fid->Vnode + fid->Unique) % CBRSIZE;
100 * Insert a CBR entry into the hash table.
101 * Must be called with afs_xvcb held.
106 afs_InsertHashCBR(struct afs_cbr *cbr)
108 int slot = afs_HashCBRFid(&cbr->fid);
110 cbr->hash_next = afs_cbrHashT[slot];
111 if (afs_cbrHashT[slot])
112 afs_cbrHashT[slot]->hash_pprev = &cbr->hash_next;
114 cbr->hash_pprev = &afs_cbrHashT[slot];
115 afs_cbrHashT[slot] = cbr;
120 * Flush the given vcache entry.
123 * afs_xvcache lock must be held for writing upon entry to
124 * prevent people from changing the vrefCount field, and to
125 * protect the lruq and hnext fields.
126 * LOCK: afs_FlushVCache afs_xvcache W
127 * REFCNT: vcache ref count must be zero on entry except for osf1
128 * RACE: lock is dropped and reobtained, permitting race in caller
130 * \param avc Pointer to vcache entry to flush.
131 * \param slept Pointer to int to set 1 if we sleep/drop locks, 0 if we don't.
135 afs_FlushVCache(struct vcache *avc, int *slept)
136 { /*afs_FlushVCache */
139 struct vcache **uvc, *wvc;
142 AFS_STATCNT(afs_FlushVCache);
143 afs_Trace2(afs_iclSetp, CM_TRACE_FLUSHV, ICL_TYPE_POINTER, avc,
144 ICL_TYPE_INT32, avc->f.states);
146 code = osi_VM_FlushVCache(avc, slept);
150 if (avc->f.states & CVFlushed) {
154 #if !defined(AFS_LINUX22_ENV)
155 if (avc->nextfree || !avc->vlruq.prev || !avc->vlruq.next) { /* qv afs.h */
156 refpanic("LRU vs. Free inconsistency");
159 avc->f.states |= CVFlushed;
160 /* pull the entry out of the lruq and put it on the free list */
161 QRemove(&avc->vlruq);
163 /* keep track of # of files that we bulk stat'd, but never used
164 * before they got recycled.
166 if (avc->f.states & CBulkStat)
169 /* remove entry from the hash chain */
170 i = VCHash(&avc->f.fid);
171 uvc = &afs_vhashT[i];
172 for (wvc = *uvc; wvc; uvc = &wvc->hnext, wvc = *uvc) {
175 avc->hnext = (struct vcache *)NULL;
180 /* remove entry from the volume hash table */
181 QRemove(&avc->vhashq);
184 osi_FreeSmallSpace(avc->mvid);
185 avc->mvid = (struct VenusFid *)0;
187 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
188 avc->linkData = NULL;
190 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
191 /* OK, there are no internal vrefCounts, so there shouldn't
192 * be any more refs here. */
194 #ifdef AFS_DARWIN80_ENV
195 vnode_clearfsnode(AFSTOV(avc));
196 vnode_removefsref(AFSTOV(avc));
198 avc->v->v_data = NULL; /* remove from vnode */
200 AFSTOV(avc) = NULL; /* also drop the ptr to vnode */
203 #ifdef AFS_SUN510_ENV
204 /* As we use private vnodes, cleanup is up to us */
205 vn_reinit(AFSTOV(avc));
207 afs_FreeAllAxs(&(avc->Access));
208 if (!afs_shuttingdown)
210 ObtainWriteLock(&afs_xcbhash, 460);
211 afs_DequeueCallback(avc); /* remove it from queued callbacks list */
212 avc->f.states &= ~(CStatd | CUnique);
213 ReleaseWriteLock(&afs_xcbhash);
214 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
215 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
217 osi_dnlc_purgevp(avc);
220 * Next, keep track of which vnodes we've deleted for create's
221 * optimistic synchronization algorithm
224 if (avc->f.fid.Fid.Vnode & 1)
230 #if !defined(AFS_LINUX22_ENV)
231 /* put the entry in the free list */
232 avc->nextfree = freeVCList;
234 if (avc->vlruq.prev || avc->vlruq.next) {
235 refpanic("LRU vs. Free inconsistency");
237 avc->f.states |= CVFlushed;
239 /* This should put it back on the vnode free list since usecount is 1 */
241 if (VREFCOUNT_GT(avc,0)) {
242 AFS_RELE(AFSTOV(avc));
243 afs_stats_cmperf.vcacheXAllocs--;
245 if (afs_norefpanic) {
246 afs_warn("flush vc refcnt < 1");
249 osi_Panic("flush vc refcnt < 1");
251 #endif /* AFS_LINUX22_ENV */
256 } /*afs_FlushVCache */
260 * The core of the inactive vnode op for all but IRIX.
266 afs_InactiveVCache(struct vcache *avc, afs_ucred_t *acred)
268 AFS_STATCNT(afs_inactive);
269 if (avc->f.states & CDirty) {
270 /* we can't keep trying to push back dirty data forever. Give up. */
271 afs_InvalidateAllSegments(avc); /* turns off dirty bit */
273 avc->f.states &= ~CMAPPED; /* mainly used by SunOS 4.0.x */
274 avc->f.states &= ~CDirty; /* Turn it off */
275 if (avc->f.states & CUnlinked) {
276 if (CheckLock(&afs_xvcache) || CheckLock(&afs_xdcache)) {
277 avc->f.states |= CUnlinkedDel;
280 afs_remunlink(avc, 1); /* ignore any return code */
287 * Allocate a callback return structure from the
288 * free list and return it.
290 * Environment: The alloc and free routines are both called with the afs_xvcb lock
291 * held, so we don't have to worry about blocking in osi_Alloc.
293 * \return The allocated afs_cbr.
295 static struct afs_cbr *afs_cbrSpace = 0;
296 /* if alloc limit below changes, fix me! */
297 static struct afs_cbr *afs_cbrHeads[16];
305 afs_osi_CancelWait(&AFS_WaitHandler); /* trigger FlushVCBs asap */
307 if (afs_stats_cmperf.CallBackAlloced >= sizeof(afs_cbrHeads)/sizeof(afs_cbrHeads[0])) {
308 /* don't allocate more than 16 * AFS_NCBRS for now */
309 tsp = (struct afs_cbr *)osi_AllocSmallSpace(sizeof(*tsp));
312 afs_stats_cmperf.CallBackFlushes++;
315 tsp = afs_osi_Alloc(AFS_NCBRS * sizeof(struct afs_cbr));
316 osi_Assert(tsp != NULL);
317 for (i = 0; i < AFS_NCBRS - 1; i++) {
318 tsp[i].next = &tsp[i + 1];
321 tsp[AFS_NCBRS - 1].next = 0;
322 tsp[AFS_NCBRS - 1].dynalloc = 0;
323 afs_cbrSpace = tsp->next;
324 afs_cbrHeads[afs_stats_cmperf.CallBackAlloced] = tsp;
325 afs_stats_cmperf.CallBackAlloced++;
329 afs_cbrSpace = tsp->next;
335 * Free a callback return structure, removing it from all lists.
337 * Environment: the xvcb lock is held over these calls.
339 * \param asp The address of the structure to free.
344 afs_FreeCBR(struct afs_cbr *asp)
346 *(asp->pprev) = asp->next;
348 asp->next->pprev = asp->pprev;
350 *(asp->hash_pprev) = asp->hash_next;
352 asp->hash_next->hash_pprev = asp->hash_pprev;
355 osi_FreeSmallSpace(asp);
357 asp->next = afs_cbrSpace;
364 FlushAllVCBs(struct rx_connection **rxconns, int nconns, int nservers,
365 struct afs_conn **conns, struct srvAddr **addrs)
370 results = afs_osi_Alloc(nservers * sizeof (afs_int32));
371 osi_Assert(results != NULL);
374 multi_Rx(rxconns,nconns)
376 multi_RXAFS_GiveUpAllCallBacks();
377 results[multi_i] = multi_error;
382 * Freeing the CBR will unlink it from the server's CBR list
383 * do it here, not in the loop, because a dynamic CBR will call
384 * into the memory management routines.
386 for ( i = 0 ; i < nconns ; i++ ) {
387 if (results[i] == 0) {
388 /* Unchain all of them */
389 while (addrs[i]->server->cbrs)
390 afs_FreeCBR(addrs[i]->server->cbrs);
393 afs_osi_Free(results, nservers * sizeof(afs_int32));
397 * Flush all queued callbacks to all servers.
399 * Environment: holds xvcb lock over RPC to guard against race conditions
400 * when a new callback is granted for the same file later on.
402 * \return 0 for success.
405 afs_FlushVCBs(afs_int32 lockit)
407 struct AFSFid *tfids;
408 struct AFSCallBack callBacks[1];
409 struct AFSCBFids fidArray;
410 struct AFSCBs cbArray;
412 struct afs_cbr *tcbrp;
416 struct vrequest treq;
418 int safety1, safety2, safety3;
421 if (AFS_IS_DISCONNECTED)
424 if ((code = afs_InitReq(&treq, afs_osi_credp)))
426 treq.flags |= O_NONBLOCK;
427 tfids = afs_osi_Alloc(sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
428 osi_Assert(tfids != NULL);
431 ObtainWriteLock(&afs_xvcb, 273);
434 * First, attempt a multi across everything, all addresses
435 * for all servers we know of.
439 afs_LoopServers(2, NULL, 0, FlushAllVCBs, NULL);
441 ObtainReadLock(&afs_xserver);
442 for (i = 0; i < NSERVERS; i++) {
443 for (safety1 = 0, tsp = afs_servers[i];
444 tsp && safety1 < afs_totalServers + 10;
445 tsp = tsp->next, safety1++) {
447 if (tsp->cbrs == (struct afs_cbr *)0)
450 /* otherwise, grab a block of AFS_MAXCBRSCALL from the list
451 * and make an RPC, over and over again.
453 tcount = 0; /* number found so far */
454 for (safety2 = 0; safety2 < afs_cacheStats; safety2++) {
455 if (tcount >= AFS_MAXCBRSCALL || !tsp->cbrs) {
456 /* if buffer is full, or we've queued all we're going
457 * to from this server, we should flush out the
460 fidArray.AFSCBFids_len = tcount;
461 fidArray.AFSCBFids_val = (struct AFSFid *)tfids;
462 cbArray.AFSCBs_len = 1;
463 cbArray.AFSCBs_val = callBacks;
464 memset(&callBacks[0], 0, sizeof(callBacks[0]));
465 callBacks[0].CallBackType = CB_EXCLUSIVE;
466 for (safety3 = 0; safety3 < AFS_MAXHOSTS * 2; safety3++) {
467 tc = afs_ConnByHost(tsp, tsp->cell->fsport,
468 tsp->cell->cellNum, &treq, 0,
472 (AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS);
475 RXAFS_GiveUpCallBacks(tc->id, &fidArray,
483 AFS_STATS_FS_RPCIDX_GIVEUPCALLBACKS, SHARED_LOCK,
488 /* ignore return code, since callbacks may have
489 * been returned anyway, we shouldn't leave them
490 * around to be returned again.
492 * Next, see if we are done with this server, and if so,
493 * break to deal with the next one.
499 /* if to flush full buffer */
500 /* if we make it here, we have an entry at the head of cbrs,
501 * which we should copy to the file ID array and then free.
504 tfids[tcount++] = tcbrp->fid;
506 /* Freeing the CBR will unlink it from the server's CBR list */
508 } /* while loop for this one server */
509 if (safety2 > afs_cacheStats) {
510 afs_warn("possible internal error afs_flushVCBs (%d)\n",
513 } /* for loop for this hash chain */
514 } /* loop through all hash chains */
515 if (safety1 > afs_totalServers + 2) {
517 ("AFS internal error (afs_flushVCBs) (%d > %d), continuing...\n",
518 safety1, afs_totalServers + 2);
520 osi_Panic("afs_flushVCBS safety1");
523 ReleaseReadLock(&afs_xserver);
525 ReleaseWriteLock(&afs_xvcb);
526 afs_osi_Free(tfids, sizeof(struct AFSFid) * AFS_MAXCBRSCALL);
531 * Queue a callback on the given fid.
534 * Locks the xvcb lock.
535 * Called when the xvcache lock is already held.
537 * \param avc vcache entry
538 * \return 1 if queued, 0 otherwise
542 afs_QueueVCB(struct vcache *avc)
546 struct afs_cbr *tcbp;
548 AFS_STATCNT(afs_QueueVCB);
550 ObtainWriteLock(&afs_xvcb, 274);
552 /* we can't really give back callbacks on RO files, since the
553 * server only tracks them on a per-volume basis, and we don't
554 * know whether we still have some other files from the same
556 if (!((avc->f.states & CRO) == 0 && avc->callback)) {
560 /* The callback is really just a struct server ptr. */
561 tsp = (struct server *)(avc->callback);
563 /* we now have a pointer to the server, so we just allocate
564 * a queue entry and queue it.
566 tcbp = afs_AllocCBR();
567 tcbp->fid = avc->f.fid.Fid;
569 tcbp->next = tsp->cbrs;
571 tsp->cbrs->pprev = &tcbp->next;
574 tcbp->pprev = &tsp->cbrs;
576 afs_InsertHashCBR(tcbp);
580 /* now release locks and return */
581 ReleaseWriteLock(&afs_xvcb);
587 * Remove a queued callback for a given Fid.
590 * Locks xvcb and xserver locks.
591 * Typically called with xdcache, xvcache and/or individual vcache
594 * \param afid The fid we want cleansed of queued callbacks.
599 afs_RemoveVCB(struct VenusFid *afid)
602 struct afs_cbr *cbr, *ncbr;
604 AFS_STATCNT(afs_RemoveVCB);
605 ObtainWriteLock(&afs_xvcb, 275);
607 slot = afs_HashCBRFid(&afid->Fid);
608 ncbr = afs_cbrHashT[slot];
612 ncbr = cbr->hash_next;
614 if (afid->Fid.Volume == cbr->fid.Volume &&
615 afid->Fid.Vnode == cbr->fid.Vnode &&
616 afid->Fid.Unique == cbr->fid.Unique) {
621 ReleaseWriteLock(&afs_xvcb);
625 afs_FlushReclaimedVcaches(void)
627 #if !defined(AFS_LINUX22_ENV)
630 struct vcache *tmpReclaimedVCList = NULL;
632 ObtainWriteLock(&afs_xvreclaim, 76);
633 while (ReclaimedVCList) {
634 tvc = ReclaimedVCList; /* take from free list */
635 ReclaimedVCList = tvc->nextfree;
636 tvc->nextfree = NULL;
637 code = afs_FlushVCache(tvc, &fv_slept);
639 /* Ok, so, if we got code != 0, uh, wtf do we do? */
640 /* Probably, build a temporary list and then put all back when we
641 get to the end of the list */
642 /* This is actually really crappy, but we need to not leak these.
643 We probably need a way to be smarter about this. */
644 tvc->nextfree = tmpReclaimedVCList;
645 tmpReclaimedVCList = tvc;
646 /* printf("Reclaim list flush %lx failed: %d\n", (unsigned long) tvc, code); */
648 if (tvc->f.states & (CVInit
649 #ifdef AFS_DARWIN80_ENV
653 tvc->f.states &= ~(CVInit
654 #ifdef AFS_DARWIN80_ENV
658 afs_osi_Wakeup(&tvc->f.states);
661 if (tmpReclaimedVCList)
662 ReclaimedVCList = tmpReclaimedVCList;
664 ReleaseWriteLock(&afs_xvreclaim);
669 afs_PostPopulateVCache(struct vcache *avc, struct VenusFid *afid, int seq)
672 * The proper value for mvstat (for root fids) is setup by the caller.
675 if (afid->Fid.Vnode == 1 && afid->Fid.Unique == 1)
678 if (afs_globalVFS == 0)
679 osi_Panic("afs globalvfs");
681 osi_PostPopulateVCache(avc);
684 osi_dnlc_purgedp(avc); /* this may be overkill */
685 memset(&(avc->callsort), 0, sizeof(struct afs_q));
687 avc->f.states &=~ CVInit;
689 avc->f.states |= CBulkFetching;
690 avc->f.m.Length = seq;
692 afs_osi_Wakeup(&avc->f.states);
696 afs_ShakeLooseVCaches(afs_int32 anumber)
700 struct afs_q *tq, *uq;
701 int fv_slept, defersleep = 0;
702 afs_int32 target = anumber;
706 for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
709 if (tvc->f.states & CVFlushed) {
710 refpanic("CVFlushed on VLRU");
711 /* In the other path, this was 2 * afs_cacheStats */
712 } else if (!afsd_dynamic_vcaches && i++ > afs_maxvcount) {
713 refpanic("Exceeded pool of AFS vnodes(VLRU cycle?)");
714 } else if (QNext(uq) != tq) {
715 refpanic("VLRU inconsistent");
716 } else if (tvc->f.states & CVInit) {
721 if (osi_TryEvictVCache(tvc, &fv_slept, defersleep))
729 continue; /* start over - may have raced. */
732 if (anumber && !defersleep) {
740 if (!afsd_dynamic_vcaches && anumber == target) {
741 afs_warn("afs_ShakeLooseVCaches: warning none freed, using %d of %d\n",
742 afs_vcount, afs_maxvcount);
748 /* Alloc new vnode. */
750 static struct vcache *
751 afs_AllocVCache(void)
755 tvc = osi_NewVnode();
760 if (afsd_dynamic_vcaches && afs_maxvcount < afs_vcount) {
761 afs_maxvcount = afs_vcount;
762 /*printf("peak vnodes: %d\n", afs_maxvcount);*/
765 afs_stats_cmperf.vcacheXAllocs++; /* count in case we have a leak */
767 /* If we create a new inode, we either give it a new slot number,
768 * or if one's available, use a slot number from the slot free list
770 if (afs_freeSlotList != NULL) {
771 struct afs_slotlist *tmp;
773 tvc->diskSlot = afs_freeSlotList->slot;
774 tmp = afs_freeSlotList;
775 afs_freeSlotList = tmp->next;
776 afs_osi_Free(tmp, sizeof(struct afs_slotlist));
778 tvc->diskSlot = afs_nextVcacheSlot++;
784 /* Pre populate a newly allocated vcache. On platforms where the actual
785 * vnode is attached to the vcache, this function is called before attachment,
786 * therefore it cannot perform any actions on the vnode itself */
789 afs_PrePopulateVCache(struct vcache *avc, struct VenusFid *afid,
790 struct server *serverp) {
793 slot = avc->diskSlot;
795 osi_PrePopulateVCache(avc);
797 avc->diskSlot = slot;
798 QZero(&avc->metadirty);
800 AFS_RWLOCK_INIT(&avc->lock, "vcache lock");
803 avc->linkData = NULL;
806 avc->execsOrWriters = 0;
808 avc->f.states = CVInit;
809 avc->last_looker = 0;
811 avc->asynchrony = -1;
815 avc->f.truncPos = AFS_NOTRUNC; /* don't truncate until we need to */
816 hzero(avc->f.m.DataVersion); /* in case we copy it into flushDV */
818 avc->callback = serverp; /* to minimize chance that clear
821 #if defined(AFS_CACHE_BYPASS)
822 avc->cachingStates = 0;
823 avc->cachingTransitions = 0;
828 * This routine is responsible for allocating a new cache entry
829 * from the free list. It formats the cache entry and inserts it
830 * into the appropriate hash tables. It must be called with
831 * afs_xvcache write-locked so as to prevent several processes from
832 * trying to create a new cache entry simultaneously.
834 * LOCK: afs_NewVCache afs_xvcache W
836 * \param afid The file id of the file whose cache entry is being created.
838 * \return The new vcache struct.
841 static_inline struct vcache *
842 afs_NewVCache_int(struct VenusFid *afid, struct server *serverp, int seq)
846 afs_int32 anumber = VCACHE_FREE;
848 AFS_STATCNT(afs_NewVCache);
850 afs_FlushReclaimedVcaches();
852 #if defined(AFS_LINUX22_ENV)
853 if(!afsd_dynamic_vcaches && afs_vcount >= afs_maxvcount) {
854 afs_ShakeLooseVCaches(anumber);
855 if (afs_vcount >= afs_maxvcount) {
856 afs_warn("afs_NewVCache - none freed\n");
860 tvc = afs_AllocVCache();
861 #else /* AFS_LINUX22_ENV */
862 /* pull out a free cache entry */
864 afs_ShakeLooseVCaches(anumber);
868 tvc = afs_AllocVCache();
870 tvc = freeVCList; /* take from free list */
871 freeVCList = tvc->nextfree;
872 tvc->nextfree = NULL;
873 afs_vcount++; /* balanced by FlushVCache */
874 } /* end of if (!freeVCList) */
876 #endif /* AFS_LINUX22_ENV */
878 #if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
880 panic("afs_NewVCache(): free vcache with vnode attached");
883 /* Populate the vcache with as much as we can. */
884 afs_PrePopulateVCache(tvc, afid, serverp);
886 /* Thread the vcache onto the VLRU */
891 tvc->hnext = afs_vhashT[i];
893 QAdd(&afs_vhashTV[j], &tvc->vhashq);
895 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
896 refpanic("NewVCache VLRU inconsistent");
898 QAdd(&VLRU, &tvc->vlruq); /* put in lruq */
899 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
900 refpanic("NewVCache VLRU inconsistent2");
902 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
903 refpanic("NewVCache VLRU inconsistent3");
905 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
906 refpanic("NewVCache VLRU inconsistent4");
910 /* it should now be safe to drop the xvcache lock - so attach an inode
911 * to this vcache, where necessary */
912 osi_AttachVnode(tvc, seq);
914 /* Get a reference count to hold this vcache for the VLRUQ. Note that
915 * we have to do this after attaching the vnode, because the reference
916 * count may be held in the vnode itself */
918 #if defined(AFS_LINUX22_ENV)
919 /* Hold it for the LRU (should make count 2) */
921 #elif !(defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV))
922 VREFCOUNT_SET(tvc, 1); /* us */
925 #if defined (AFS_FBSD_ENV)
926 if (tvc->f.states & CVInit)
928 afs_PostPopulateVCache(tvc, afid, seq);
935 afs_NewVCache(struct VenusFid *afid, struct server *serverp)
937 return afs_NewVCache_int(afid, serverp, 0);
941 afs_NewBulkVCache(struct VenusFid *afid, struct server *serverp, int seq)
943 return afs_NewVCache_int(afid, serverp, seq);
949 * LOCK: afs_FlushActiveVcaches afs_xvcache N
951 * \param doflocks : Do we handle flocks?
954 afs_FlushActiveVcaches(afs_int32 doflocks)
960 afs_ucred_t *cred = NULL;
961 struct vrequest treq, ureq;
962 struct AFSVolSync tsync;
965 AFS_STATCNT(afs_FlushActiveVcaches);
966 ObtainReadLock(&afs_xvcache);
967 for (i = 0; i < VCSIZE; i++) {
968 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
969 if (tvc->f.states & CVInit) continue;
970 #ifdef AFS_DARWIN80_ENV
971 if (tvc->f.states & CDeadVnode &&
972 (tvc->f.states & (CCore|CUnlinkedDel) ||
973 tvc->flockCount)) panic("Dead vnode has core/unlinkedel/flock");
975 if (doflocks && tvc->flockCount != 0) {
976 /* if this entry has an flock, send a keep-alive call out */
978 ReleaseReadLock(&afs_xvcache);
979 ObtainWriteLock(&tvc->lock, 51);
981 afs_InitReq(&treq, afs_osi_credp);
982 treq.flags |= O_NONBLOCK;
984 tc = afs_Conn(&tvc->f.fid, &treq, SHARED_LOCK);
986 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_EXTENDLOCK);
989 RXAFS_ExtendLock(tc->id,
990 (struct AFSFid *)&tvc->f.fid.Fid,
997 (tc, code, &tvc->f.fid, &treq,
998 AFS_STATS_FS_RPCIDX_EXTENDLOCK, SHARED_LOCK, NULL));
1000 ReleaseWriteLock(&tvc->lock);
1001 #ifdef AFS_DARWIN80_ENV
1003 ObtainReadLock(&afs_xvcache);
1005 ObtainReadLock(&afs_xvcache);
1010 if ((tvc->f.states & CCore) || (tvc->f.states & CUnlinkedDel)) {
1012 * Don't let it evaporate in case someone else is in
1013 * this code. Also, drop the afs_xvcache lock while
1014 * getting vcache locks.
1017 ReleaseReadLock(&afs_xvcache);
1018 #ifdef AFS_BOZONLOCK_ENV
1019 afs_BozonLock(&tvc->pvnLock, tvc);
1021 #if defined(AFS_SGI_ENV)
1023 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
1025 osi_Assert(VREFCOUNT_GT(tvc,0));
1026 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1028 ObtainWriteLock(&tvc->lock, 52);
1029 if (tvc->f.states & CCore) {
1030 tvc->f.states &= ~CCore;
1031 /* XXXX Find better place-holder for cred XXXX */
1032 cred = (afs_ucred_t *)tvc->linkData;
1033 tvc->linkData = NULL; /* XXX */
1034 afs_InitReq(&ureq, cred);
1035 afs_Trace2(afs_iclSetp, CM_TRACE_ACTCCORE,
1036 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32,
1037 tvc->execsOrWriters);
1038 code = afs_StoreOnLastReference(tvc, &ureq);
1039 ReleaseWriteLock(&tvc->lock);
1040 #ifdef AFS_BOZONLOCK_ENV
1041 afs_BozonUnlock(&tvc->pvnLock, tvc);
1043 hzero(tvc->flushDV);
1046 if (code && code != VNOVNODE) {
1047 afs_StoreWarn(code, tvc->f.fid.Fid.Volume,
1048 /* /dev/console */ 1);
1050 } else if (tvc->f.states & CUnlinkedDel) {
1054 ReleaseWriteLock(&tvc->lock);
1055 #ifdef AFS_BOZONLOCK_ENV
1056 afs_BozonUnlock(&tvc->pvnLock, tvc);
1058 #if defined(AFS_SGI_ENV)
1059 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1061 afs_remunlink(tvc, 0);
1062 #if defined(AFS_SGI_ENV)
1063 AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1066 /* lost (or won, perhaps) the race condition */
1067 ReleaseWriteLock(&tvc->lock);
1068 #ifdef AFS_BOZONLOCK_ENV
1069 afs_BozonUnlock(&tvc->pvnLock, tvc);
1072 #if defined(AFS_SGI_ENV)
1073 AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
1075 #ifdef AFS_DARWIN80_ENV
1078 AFS_RELE(AFSTOV(tvc));
1079 /* Matches write code setting CCore flag */
1082 ObtainReadLock(&afs_xvcache);
1084 ObtainReadLock(&afs_xvcache);
1087 AFS_RELE(AFSTOV(tvc));
1088 /* Matches write code setting CCore flag */
1095 ReleaseReadLock(&afs_xvcache);
1101 * Make sure a cache entry is up-to-date status-wise.
1103 * NOTE: everywhere that calls this can potentially be sped up
1104 * by checking CStatd first, and avoiding doing the InitReq
1105 * if this is up-to-date.
1107 * Anymore, the only places that call this KNOW already that the
1108 * vcache is not up-to-date, so we don't screw around.
1110 * \param avc : Ptr to vcache entry to verify.
1116 * Make sure a cache entry is up-to-date status-wise.
1118 * NOTE: everywhere that calls this can potentially be sped up
1119 * by checking CStatd first, and avoiding doing the InitReq
1120 * if this is up-to-date.
1122 * Anymore, the only places that call this KNOW already that the
1123 * vcache is not up-to-date, so we don't screw around.
1125 * \param avc Pointer to vcache entry to verify.
1128 * \return 0 for success or other error codes.
1131 afs_VerifyVCache2(struct vcache *avc, struct vrequest *areq)
1135 AFS_STATCNT(afs_VerifyVCache);
1137 /* otherwise we must fetch the status info */
1139 ObtainWriteLock(&avc->lock, 53);
1140 if (avc->f.states & CStatd) {
1141 ReleaseWriteLock(&avc->lock);
1144 ObtainWriteLock(&afs_xcbhash, 461);
1145 avc->f.states &= ~(CStatd | CUnique);
1146 avc->callback = NULL;
1147 afs_DequeueCallback(avc);
1148 ReleaseWriteLock(&afs_xcbhash);
1149 ReleaseWriteLock(&avc->lock);
1151 /* since we've been called back, or the callback has expired,
1152 * it's possible that the contents of this directory, or this
1153 * file's name have changed, thus invalidating the dnlc contents.
1155 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
1156 osi_dnlc_purgedp(avc);
1158 osi_dnlc_purgevp(avc);
1160 /* fetch the status info */
1161 tvc = afs_GetVCache(&avc->f.fid, areq, NULL, avc);
1164 /* Put it back; caller has already incremented vrefCount */
1168 } /*afs_VerifyVCache */
1172 * Simple copy of stat info into cache.
1174 * Callers:as of 1992-04-29, only called by WriteVCache
1176 * \param avc Ptr to vcache entry involved.
1177 * \param astat Ptr to stat info to copy.
1181 afs_SimpleVStat(struct vcache *avc,
1182 struct AFSFetchStatus *astat, struct vrequest *areq)
1185 AFS_STATCNT(afs_SimpleVStat);
1187 #ifdef AFS_64BIT_CLIENT
1188 FillInt64(length, astat->Length_hi, astat->Length);
1189 #else /* AFS_64BIT_CLIENT */
1190 length = astat->Length;
1191 #endif /* AFS_64BIT_CLIENT */
1193 #if defined(AFS_SGI_ENV)
1194 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1195 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1196 osi_Assert((valusema(&avc->vc_rwlock) <= 0)
1197 && (OSI_GET_LOCKID() == avc->vc_rwlockid));
1198 if (length < avc->f.m.Length) {
1199 vnode_t *vp = (vnode_t *) avc;
1201 osi_Assert(WriteLocked(&avc->lock));
1202 ReleaseWriteLock(&avc->lock);
1204 PTOSSVP(vp, (off_t) length, (off_t) MAXLONG);
1206 ObtainWriteLock(&avc->lock, 67);
1211 if (!afs_DirtyPages(avc)) {
1212 /* if actively writing the file, don't fetch over this value */
1213 afs_Trace3(afs_iclSetp, CM_TRACE_SIMPLEVSTAT, ICL_TYPE_POINTER, avc,
1214 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1215 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1216 avc->f.m.Length = length;
1217 avc->f.m.Date = astat->ClientModTime;
1219 avc->f.m.Owner = astat->Owner;
1220 avc->f.m.Group = astat->Group;
1221 avc->f.m.Mode = astat->UnixModeBits;
1222 if (vType(avc) == VREG) {
1223 avc->f.m.Mode |= S_IFREG;
1224 } else if (vType(avc) == VDIR) {
1225 avc->f.m.Mode |= S_IFDIR;
1226 } else if (vType(avc) == VLNK) {
1227 avc->f.m.Mode |= S_IFLNK;
1228 if ((avc->f.m.Mode & 0111) == 0)
1231 if (avc->f.states & CForeign) {
1232 struct axscache *ac;
1233 avc->f.anyAccess = astat->AnonymousAccess;
1235 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1237 * Caller has at least one bit not covered by anonymous, and
1238 * thus may have interesting rights.
1240 * HOWEVER, this is a really bad idea, because any access query
1241 * for bits which aren't covered by anonymous, on behalf of a user
1242 * who doesn't have any special rights, will result in an answer of
1243 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1244 * It's an especially bad idea under Ultrix, since (due to the lack of
1245 * a proper access() call) it must perform several afs_access() calls
1246 * in order to create magic mode bits that vary according to who makes
1247 * the call. In other words, _every_ stat() generates a test for
1250 #endif /* badidea */
1251 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1252 ac->axess = astat->CallerAccess;
1253 else /* not found, add a new one if possible */
1254 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1257 } /*afs_SimpleVStat */
1261 * Store the status info *only* back to the server for a
1264 * Environment: Must be called with a shared lock held on the vnode.
1266 * \param avc Ptr to the vcache entry.
1267 * \param astatus Ptr to the status info to store.
1268 * \param areq Ptr to the associated vrequest.
1270 * \return Operation status.
1274 afs_WriteVCache(struct vcache *avc,
1275 struct AFSStoreStatus *astatus,
1276 struct vrequest *areq)
1279 struct afs_conn *tc;
1280 struct AFSFetchStatus OutStatus;
1281 struct AFSVolSync tsync;
1283 AFS_STATCNT(afs_WriteVCache);
1284 afs_Trace2(afs_iclSetp, CM_TRACE_WVCACHE, ICL_TYPE_POINTER, avc,
1285 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
1287 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK);
1289 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1292 RXAFS_StoreStatus(tc->id, (struct AFSFid *)&avc->f.fid.Fid,
1293 astatus, &OutStatus, &tsync);
1298 } while (afs_Analyze
1299 (tc, code, &avc->f.fid, areq, AFS_STATS_FS_RPCIDX_STORESTATUS,
1300 SHARED_LOCK, NULL));
1302 UpgradeSToWLock(&avc->lock, 20);
1304 /* success, do the changes locally */
1305 afs_SimpleVStat(avc, &OutStatus, areq);
1307 * Update the date, too. SimpleVStat didn't do this, since
1308 * it thought we were doing this after fetching new status
1309 * over a file being written.
1311 avc->f.m.Date = OutStatus.ClientModTime;
1313 /* failure, set up to check with server next time */
1314 ObtainWriteLock(&afs_xcbhash, 462);
1315 afs_DequeueCallback(avc);
1316 avc->f.states &= ~(CStatd | CUnique); /* turn off stat valid flag */
1317 ReleaseWriteLock(&afs_xcbhash);
1318 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
1319 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
1321 ConvertWToSLock(&avc->lock);
1324 } /*afs_WriteVCache */
1327 * Store status info only locally, set the proper disconnection flags
1328 * and add to dirty list.
1330 * \param avc The vcache to be written locally.
1331 * \param astatus Get attr fields from local store.
1332 * \param attrs This one is only of the vs_size.
1334 * \note Must be called with a shared lock on the vnode
1337 afs_WriteVCacheDiscon(struct vcache *avc,
1338 struct AFSStoreStatus *astatus,
1339 struct vattr *attrs)
1342 afs_int32 flags = 0;
1344 UpgradeSToWLock(&avc->lock, 700);
1346 if (!astatus->Mask) {
1352 /* Set attributes. */
1353 if (astatus->Mask & AFS_SETMODTIME) {
1354 avc->f.m.Date = astatus->ClientModTime;
1355 flags |= VDisconSetTime;
1358 if (astatus->Mask & AFS_SETOWNER) {
1359 /* printf("Not allowed yet. \n"); */
1360 /*avc->f.m.Owner = astatus->Owner;*/
1363 if (astatus->Mask & AFS_SETGROUP) {
1364 /* printf("Not allowed yet. \n"); */
1365 /*avc->f.m.Group = astatus->Group;*/
1368 if (astatus->Mask & AFS_SETMODE) {
1369 avc->f.m.Mode = astatus->UnixModeBits;
1371 #if 0 /* XXX: Leaving this out, so it doesn't mess up the file type flag.*/
1373 if (vType(avc) == VREG) {
1374 avc->f.m.Mode |= S_IFREG;
1375 } else if (vType(avc) == VDIR) {
1376 avc->f.m.Mode |= S_IFDIR;
1377 } else if (vType(avc) == VLNK) {
1378 avc->f.m.Mode |= S_IFLNK;
1379 if ((avc->f.m.Mode & 0111) == 0)
1383 flags |= VDisconSetMode;
1384 } /* if(astatus.Mask & AFS_SETMODE) */
1386 } /* if (!astatus->Mask) */
1388 if (attrs->va_size > 0) {
1389 /* XXX: Do I need more checks? */
1390 /* Truncation operation. */
1391 flags |= VDisconTrunc;
1395 afs_DisconAddDirty(avc, flags, 1);
1397 /* XXX: How about the rest of the fields? */
1399 ConvertWToSLock(&avc->lock);
1405 * Copy astat block into vcache info
1407 * \note This code may get dataversion and length out of sync if the file has
1408 * been modified. This is less than ideal. I haven't thought about it sufficiently
1409 * to be certain that it is adequate.
1411 * \note Environment: Must be called under a write lock
1413 * \param avc Ptr to vcache entry.
1414 * \param astat Ptr to stat block to copy in.
1415 * \param areq Ptr to associated request.
1418 afs_ProcessFS(struct vcache *avc,
1419 struct AFSFetchStatus *astat, struct vrequest *areq)
1422 AFS_STATCNT(afs_ProcessFS);
1424 #ifdef AFS_64BIT_CLIENT
1425 FillInt64(length, astat->Length_hi, astat->Length);
1426 #else /* AFS_64BIT_CLIENT */
1427 length = astat->Length;
1428 #endif /* AFS_64BIT_CLIENT */
1429 /* WARNING: afs_DoBulkStat uses the Length field to store a sequence
1430 * number for each bulk status request. Under no circumstances
1431 * should afs_DoBulkStat store a sequence number if the new
1432 * length will be ignored when afs_ProcessFS is called with
1433 * new stats. If you change the following conditional then you
1434 * also need to change the conditional in afs_DoBulkStat. */
1436 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)
1437 && !AFS_VN_MAPPED((vnode_t *) avc)) {
1439 if ((avc->execsOrWriters <= 0) && !afs_DirtyPages(avc)) {
1441 /* if we're writing or mapping this file, don't fetch over these
1444 afs_Trace3(afs_iclSetp, CM_TRACE_PROCESSFS, ICL_TYPE_POINTER, avc,
1445 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
1446 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(length));
1447 avc->f.m.Length = length;
1448 avc->f.m.Date = astat->ClientModTime;
1450 hset64(avc->f.m.DataVersion, astat->dataVersionHigh, astat->DataVersion);
1451 avc->f.m.Owner = astat->Owner;
1452 avc->f.m.Mode = astat->UnixModeBits;
1453 avc->f.m.Group = astat->Group;
1454 avc->f.m.LinkCount = astat->LinkCount;
1455 if (astat->FileType == File) {
1456 vSetType(avc, VREG);
1457 avc->f.m.Mode |= S_IFREG;
1458 } else if (astat->FileType == Directory) {
1459 vSetType(avc, VDIR);
1460 avc->f.m.Mode |= S_IFDIR;
1461 } else if (astat->FileType == SymbolicLink) {
1462 if (afs_fakestat_enable && (avc->f.m.Mode & 0111) == 0) {
1463 vSetType(avc, VDIR);
1464 avc->f.m.Mode |= S_IFDIR;
1466 vSetType(avc, VLNK);
1467 avc->f.m.Mode |= S_IFLNK;
1469 if ((avc->f.m.Mode & 0111) == 0) {
1473 avc->f.anyAccess = astat->AnonymousAccess;
1475 if ((astat->CallerAccess & ~astat->AnonymousAccess))
1477 * Caller has at least one bit not covered by anonymous, and
1478 * thus may have interesting rights.
1480 * HOWEVER, this is a really bad idea, because any access query
1481 * for bits which aren't covered by anonymous, on behalf of a user
1482 * who doesn't have any special rights, will result in an answer of
1483 * the form "I don't know, lets make a FetchStatus RPC and find out!"
1484 * It's an especially bad idea under Ultrix, since (due to the lack of
1485 * a proper access() call) it must perform several afs_access() calls
1486 * in order to create magic mode bits that vary according to who makes
1487 * the call. In other words, _every_ stat() generates a test for
1490 #endif /* badidea */
1492 struct axscache *ac;
1493 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
1494 ac->axess = astat->CallerAccess;
1495 else /* not found, add a new one if possible */
1496 afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
1498 } /*afs_ProcessFS */
1502 * Get fid from server.
1505 * \param areq Request to be passed on.
1506 * \param name Name of ?? to lookup.
1507 * \param OutStatus Fetch status.
1512 * \return Success status of operation.
1515 afs_RemoteLookup(struct VenusFid *afid, struct vrequest *areq,
1516 char *name, struct VenusFid *nfid,
1517 struct AFSFetchStatus *OutStatusp,
1518 struct AFSCallBack *CallBackp, struct server **serverp,
1519 struct AFSVolSync *tsyncp)
1523 struct afs_conn *tc;
1524 struct AFSFetchStatus OutDirStatus;
1527 name = ""; /* XXX */
1529 tc = afs_Conn(afid, areq, SHARED_LOCK);
1532 *serverp = tc->parent->srvr->server;
1534 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_XLOOKUP);
1537 RXAFS_Lookup(tc->id, (struct AFSFid *)&afid->Fid, name,
1538 (struct AFSFid *)&nfid->Fid, OutStatusp,
1539 &OutDirStatus, CallBackp, tsyncp);
1544 } while (afs_Analyze
1545 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_XLOOKUP, SHARED_LOCK,
1555 * Given a file id and a vrequest structure, fetch the status
1556 * information associated with the file.
1558 * \param afid File ID.
1559 * \param areq Ptr to associated vrequest structure, specifying the
1560 * user whose authentication tokens will be used.
1561 * \param avc Caller may already have a vcache for this file, which is
1564 * \note Environment:
1565 * The cache entry is returned with an increased vrefCount field.
1566 * The entry must be discarded by calling afs_PutVCache when you
1567 * are through using the pointer to the cache entry.
1569 * You should not hold any locks when calling this function, except
1570 * locks on other vcache entries. If you lock more than one vcache
1571 * entry simultaneously, you should lock them in this order:
1573 * 1. Lock all files first, then directories.
1574 * 2. Within a particular type, lock entries in Fid.Vnode order.
1576 * This locking hierarchy is convenient because it allows locking
1577 * of a parent dir cache entry, given a file (to check its access
1578 * control list). It also allows renames to be handled easily by
1579 * locking directories in a constant order.
1581 * \note NB. NewVCache -> FlushVCache presently (4/10/95) drops the xvcache lock.
1583 * \note Might have a vcache structure already, which must
1584 * already be held by the caller
1587 afs_GetVCache(struct VenusFid *afid, struct vrequest *areq,
1588 afs_int32 * cached, struct vcache *avc)
1591 afs_int32 code, newvcache = 0;
1596 AFS_STATCNT(afs_GetVCache);
1599 *cached = 0; /* Init just in case */
1601 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1605 ObtainSharedLock(&afs_xvcache, 5);
1607 tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU | IS_SLOCK);
1609 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1610 ReleaseSharedLock(&afs_xvcache);
1611 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1618 osi_Assert((tvc->f.states & CVInit) == 0);
1619 /* If we are in readdir, return the vnode even if not statd */
1620 if ((tvc->f.states & CStatd) || afs_InReadDir(tvc)) {
1621 ReleaseSharedLock(&afs_xvcache);
1625 UpgradeSToWLock(&afs_xvcache, 21);
1627 /* no cache entry, better grab one */
1628 tvc = afs_NewVCache(afid, NULL);
1631 ConvertWToSLock(&afs_xvcache);
1634 ReleaseSharedLock(&afs_xvcache);
1638 afs_stats_cmperf.vcacheMisses++;
1641 ReleaseSharedLock(&afs_xvcache);
1643 ObtainWriteLock(&tvc->lock, 54);
1645 if (tvc->f.states & CStatd) {
1646 ReleaseWriteLock(&tvc->lock);
1649 #ifdef AFS_DARWIN80_ENV
1650 /* Darwin 8.0 only has bufs in nfs, so we shouldn't have to worry about them.
1653 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
1655 * XXX - I really don't like this. Should try to understand better.
1656 * It seems that sometimes, when we get called, we already hold the
1657 * lock on the vnode (e.g., from afs_getattr via afs_VerifyVCache).
1658 * We can't drop the vnode lock, because that could result in a race.
1659 * Sometimes, though, we get here and don't hold the vnode lock.
1660 * I hate code paths that sometimes hold locks and sometimes don't.
1661 * In any event, the dodge we use here is to check whether the vnode
1662 * is locked, and if it isn't, then we gain and drop it around the call
1663 * to vinvalbuf; otherwise, we leave it alone.
1666 struct vnode *vp = AFSTOV(tvc);
1669 #if defined(AFS_DARWIN_ENV)
1670 iheldthelock = VOP_ISLOCKED(vp);
1672 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, current_proc());
1673 /* this is messy. we can call fsync which will try to reobtain this */
1674 if (VTOAFS(vp) == tvc)
1675 ReleaseWriteLock(&tvc->lock);
1676 if (UBCINFOEXISTS(vp)) {
1677 vinvalbuf(vp, V_SAVE, &afs_osi_cred, current_proc(), PINOD, 0);
1679 if (VTOAFS(vp) == tvc)
1680 ObtainWriteLock(&tvc->lock, 954);
1682 VOP_UNLOCK(vp, LK_EXCLUSIVE, current_proc());
1683 #elif defined(AFS_FBSD80_ENV)
1684 iheldthelock = VOP_ISLOCKED(vp);
1685 if (!iheldthelock) {
1686 /* nosleep/sleep lock order reversal */
1687 int glocked = ISAFS_GLOCK();
1690 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1694 vinvalbuf(vp, V_SAVE, PINOD, 0); /* changed late in 8.0-CURRENT */
1697 #elif defined(AFS_FBSD60_ENV)
1698 iheldthelock = VOP_ISLOCKED(vp, curthread);
1700 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1702 vinvalbuf(vp, V_SAVE, curthread, PINOD, 0);
1705 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1706 #elif defined(AFS_FBSD_ENV)
1707 iheldthelock = VOP_ISLOCKED(vp, curthread);
1709 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
1710 vinvalbuf(vp, V_SAVE, osi_curcred(), curthread, PINOD, 0);
1712 VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
1713 #elif defined(AFS_OBSD_ENV)
1714 iheldthelock = VOP_ISLOCKED(vp, curproc);
1716 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
1717 uvm_vnp_uncache(vp);
1719 VOP_UNLOCK(vp, 0, curproc);
1720 #elif defined(AFS_NBSD40_ENV)
1721 iheldthelock = VOP_ISLOCKED(vp);
1722 if (!iheldthelock) {
1723 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1725 uvm_vnp_uncache(vp);
1733 ObtainWriteLock(&afs_xcbhash, 464);
1734 tvc->f.states &= ~CUnique;
1736 afs_DequeueCallback(tvc);
1737 ReleaseWriteLock(&afs_xcbhash);
1739 /* It is always appropriate to throw away all the access rights? */
1740 afs_FreeAllAxs(&(tvc->Access));
1741 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-volume info */
1743 if ((tvp->states & VForeign)) {
1745 tvc->f.states |= CForeign;
1746 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1747 && (tvp->rootUnique == afid->Fid.Unique)) {
1751 if (tvp->states & VRO)
1752 tvc->f.states |= CRO;
1753 if (tvp->states & VBackup)
1754 tvc->f.states |= CBackup;
1755 /* now copy ".." entry back out of volume structure, if necessary */
1756 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1758 tvc->mvid = (struct VenusFid *)
1759 osi_AllocSmallSpace(sizeof(struct VenusFid));
1760 *tvc->mvid = tvp->dotdot;
1762 afs_PutVolume(tvp, READ_LOCK);
1766 afs_RemoveVCB(afid);
1768 struct AFSFetchStatus OutStatus;
1770 if (afs_DynrootNewVnode(tvc, &OutStatus)) {
1771 afs_ProcessFS(tvc, &OutStatus, areq);
1772 tvc->f.states |= CStatd | CUnique;
1773 tvc->f.parent.vnode = OutStatus.ParentVnode;
1774 tvc->f.parent.unique = OutStatus.ParentUnique;
1778 if (AFS_IS_DISCONNECTED) {
1779 /* Nothing to do otherwise...*/
1781 /* printf("Network is down in afs_GetCache"); */
1783 code = afs_FetchStatus(tvc, afid, areq, &OutStatus);
1785 /* For the NFS translator's benefit, make sure
1786 * non-directory vnodes always have their parent FID set
1787 * correctly, even when created as a result of decoding an
1788 * NFS filehandle. It would be nice to also do this for
1789 * directories, but we can't because the fileserver fills
1790 * in the FID of the directory itself instead of that of
1793 if (!code && OutStatus.FileType != Directory &&
1794 !tvc->f.parent.vnode) {
1795 tvc->f.parent.vnode = OutStatus.ParentVnode;
1796 tvc->f.parent.unique = OutStatus.ParentUnique;
1797 /* XXX - SXW - It's conceivable we should mark ourselves
1798 * as dirty again here, incase we've been raced
1799 * out of the FetchStatus call.
1806 ReleaseWriteLock(&tvc->lock);
1812 ReleaseWriteLock(&tvc->lock);
1815 } /*afs_GetVCache */
1820 * Lookup a vcache by fid. Look inside the cache first, if not
1821 * there, lookup the file on the server, and then get it's fresh
1826 * \param cached Is element cached? If NULL, don't answer.
1830 * \return The found element or NULL.
1833 afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
1834 afs_int32 * cached, struct vcache *adp, char *aname)
1836 afs_int32 code, now, newvcache = 0;
1837 struct VenusFid nfid;
1840 struct AFSFetchStatus OutStatus;
1841 struct AFSCallBack CallBack;
1842 struct AFSVolSync tsync;
1843 struct server *serverp = 0;
1847 AFS_STATCNT(afs_GetVCache);
1849 *cached = 0; /* Init just in case */
1851 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1855 ObtainReadLock(&afs_xvcache);
1856 tvc = afs_FindVCache(afid, &retry, DO_STATS /* no vlru */ );
1859 ReleaseReadLock(&afs_xvcache);
1861 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1862 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1866 ObtainReadLock(&tvc->lock);
1868 if (tvc->f.states & CStatd) {
1872 ReleaseReadLock(&tvc->lock);
1875 tvc->f.states &= ~CUnique;
1877 ReleaseReadLock(&tvc->lock);
1879 ObtainReadLock(&afs_xvcache);
1882 ReleaseReadLock(&afs_xvcache);
1884 /* lookup the file */
1887 origCBs = afs_allCBs; /* if anything changes, we don't have a cb */
1889 if (AFS_IS_DISCONNECTED) {
1890 /* printf("Network is down in afs_LookupVcache\n"); */
1894 afs_RemoteLookup(&adp->f.fid, areq, aname, &nfid, &OutStatus,
1895 &CallBack, &serverp, &tsync);
1897 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1901 ObtainSharedLock(&afs_xvcache, 6);
1902 tvc = afs_FindVCache(&nfid, &retry, DO_VLRU | IS_SLOCK/* no xstats now */ );
1904 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
1905 ReleaseSharedLock(&afs_xvcache);
1906 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
1912 /* no cache entry, better grab one */
1913 UpgradeSToWLock(&afs_xvcache, 22);
1914 tvc = afs_NewVCache(&nfid, serverp);
1916 ConvertWToSLock(&afs_xvcache);
1919 ReleaseSharedLock(&afs_xvcache);
1924 ReleaseSharedLock(&afs_xvcache);
1925 ObtainWriteLock(&tvc->lock, 55);
1927 /* It is always appropriate to throw away all the access rights? */
1928 afs_FreeAllAxs(&(tvc->Access));
1929 tvp = afs_GetVolume(afid, areq, READ_LOCK); /* copy useful per-vol info */
1931 if ((tvp->states & VForeign)) {
1933 tvc->f.states |= CForeign;
1934 if (newvcache && (tvp->rootVnode == afid->Fid.Vnode)
1935 && (tvp->rootUnique == afid->Fid.Unique))
1938 if (tvp->states & VRO)
1939 tvc->f.states |= CRO;
1940 if (tvp->states & VBackup)
1941 tvc->f.states |= CBackup;
1942 /* now copy ".." entry back out of volume structure, if necessary */
1943 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
1945 tvc->mvid = (struct VenusFid *)
1946 osi_AllocSmallSpace(sizeof(struct VenusFid));
1947 *tvc->mvid = tvp->dotdot;
1952 ObtainWriteLock(&afs_xcbhash, 465);
1953 afs_DequeueCallback(tvc);
1954 tvc->f.states &= ~(CStatd | CUnique);
1955 ReleaseWriteLock(&afs_xcbhash);
1956 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
1957 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
1959 afs_PutVolume(tvp, READ_LOCK);
1960 ReleaseWriteLock(&tvc->lock);
1965 ObtainWriteLock(&afs_xcbhash, 466);
1966 if (origCBs == afs_allCBs) {
1967 if (CallBack.ExpirationTime) {
1968 tvc->callback = serverp;
1969 tvc->cbExpires = CallBack.ExpirationTime + now;
1970 tvc->f.states |= CStatd | CUnique;
1971 tvc->f.states &= ~CBulkFetching;
1972 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvp);
1973 } else if (tvc->f.states & CRO) {
1974 /* adapt gives us an hour. */
1975 tvc->cbExpires = 3600 + osi_Time();
1976 /*XXX*/ tvc->f.states |= CStatd | CUnique;
1977 tvc->f.states &= ~CBulkFetching;
1978 afs_QueueCallback(tvc, CBHash(3600), tvp);
1980 tvc->callback = NULL;
1981 afs_DequeueCallback(tvc);
1982 tvc->f.states &= ~(CStatd | CUnique);
1983 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
1984 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
1987 afs_DequeueCallback(tvc);
1988 tvc->f.states &= ~CStatd;
1989 tvc->f.states &= ~CUnique;
1990 tvc->callback = NULL;
1991 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
1992 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
1994 ReleaseWriteLock(&afs_xcbhash);
1996 afs_PutVolume(tvp, READ_LOCK);
1997 afs_ProcessFS(tvc, &OutStatus, areq);
1999 ReleaseWriteLock(&tvc->lock);
2005 afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
2006 afs_int32 * cached, struct volume *tvolp)
2008 afs_int32 code = 0, i, newvcache = 0, haveStatus = 0;
2009 afs_int32 getNewFid = 0;
2011 struct VenusFid nfid;
2013 struct server *serverp = 0;
2014 struct AFSFetchStatus OutStatus;
2015 struct AFSCallBack CallBack;
2016 struct AFSVolSync tsync;
2018 #ifdef AFS_DARWIN80_ENV
2025 if (!tvolp->rootVnode || getNewFid) {
2026 struct VenusFid tfid;
2029 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2030 origCBs = afs_allCBs; /* ignore InitCallBackState */
2032 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2037 /* ReleaseReadLock(&tvolp->lock); */
2038 ObtainWriteLock(&tvolp->lock, 56);
2039 tvolp->rootVnode = afid->Fid.Vnode = nfid.Fid.Vnode;
2040 tvolp->rootUnique = afid->Fid.Unique = nfid.Fid.Unique;
2041 ReleaseWriteLock(&tvolp->lock);
2042 /* ObtainReadLock(&tvolp->lock);*/
2045 afid->Fid.Vnode = tvolp->rootVnode;
2046 afid->Fid.Unique = tvolp->rootUnique;
2050 ObtainSharedLock(&afs_xvcache, 7);
2052 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2053 if (!FidCmp(&(tvc->f.fid), afid)) {
2054 if (tvc->f.states & CVInit) {
2055 ReleaseSharedLock(&afs_xvcache);
2056 afs_osi_Sleep(&tvc->f.states);
2059 #ifdef AFS_DARWIN80_ENV
2060 if (tvc->f.states & CDeadVnode) {
2061 if (!(tvc->f.states & CBulkFetching)) {
2062 ReleaseSharedLock(&afs_xvcache);
2063 afs_osi_Sleep(&tvc->f.states);
2068 if (vnode_get(tvp)) /* this bumps ref count */
2070 if (vnode_ref(tvp)) {
2072 /* AFSTOV(tvc) may be NULL */
2077 if (tvc->f.states & (CBulkFetching|CDeadVnode)) {
2079 vnode_recycle(AFSTOV(tvc));
2087 if (!haveStatus && (!tvc || !(tvc->f.states & CStatd))) {
2088 /* Mount point no longer stat'd or unknown. FID may have changed. */
2090 ReleaseSharedLock(&afs_xvcache);
2091 #ifdef AFS_DARWIN80_ENV
2094 vnode_put(AFSTOV(tvc));
2095 vnode_rele(AFSTOV(tvc));
2104 UpgradeSToWLock(&afs_xvcache, 23);
2105 /* no cache entry, better grab one */
2106 tvc = afs_NewVCache(afid, NULL);
2109 ReleaseWriteLock(&afs_xvcache);
2113 afs_stats_cmperf.vcacheMisses++;
2117 afs_stats_cmperf.vcacheHits++;
2118 #if defined(AFS_DARWIN80_ENV)
2119 /* we already bumped the ref count in the for loop above */
2120 #else /* AFS_DARWIN80_ENV */
2123 UpgradeSToWLock(&afs_xvcache, 24);
2124 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2125 refpanic("GRVC VLRU inconsistent0");
2127 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2128 refpanic("GRVC VLRU inconsistent1");
2130 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2131 refpanic("GRVC VLRU inconsistent2");
2133 QRemove(&tvc->vlruq); /* move to lruq head */
2134 QAdd(&VLRU, &tvc->vlruq);
2135 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2136 refpanic("GRVC VLRU inconsistent3");
2138 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2139 refpanic("GRVC VLRU inconsistent4");
2141 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2142 refpanic("GRVC VLRU inconsistent5");
2147 ReleaseWriteLock(&afs_xvcache);
2149 if (tvc->f.states & CStatd) {
2153 ObtainReadLock(&tvc->lock);
2154 tvc->f.states &= ~CUnique;
2155 tvc->callback = NULL; /* redundant, perhaps */
2156 ReleaseReadLock(&tvc->lock);
2159 ObtainWriteLock(&tvc->lock, 57);
2161 /* It is always appropriate to throw away all the access rights? */
2162 afs_FreeAllAxs(&(tvc->Access));
2165 tvc->f.states |= CForeign;
2166 if (tvolp->states & VRO)
2167 tvc->f.states |= CRO;
2168 if (tvolp->states & VBackup)
2169 tvc->f.states |= CBackup;
2170 /* now copy ".." entry back out of volume structure, if necessary */
2171 if (newvcache && (tvolp->rootVnode == afid->Fid.Vnode)
2172 && (tvolp->rootUnique == afid->Fid.Unique)) {
2175 if (tvc->mvstat == 2 && tvolp->dotdot.Fid.Volume != 0) {
2177 tvc->mvid = (struct VenusFid *)
2178 osi_AllocSmallSpace(sizeof(struct VenusFid));
2179 *tvc->mvid = tvolp->dotdot;
2183 afs_RemoveVCB(afid);
2186 struct VenusFid tfid;
2189 tfid.Fid.Vnode = 0; /* Means get rootfid of volume */
2190 origCBs = afs_allCBs; /* ignore InitCallBackState */
2192 afs_RemoteLookup(&tfid, areq, NULL, &nfid, &OutStatus, &CallBack,
2197 ObtainWriteLock(&afs_xcbhash, 467);
2198 afs_DequeueCallback(tvc);
2199 tvc->callback = NULL;
2200 tvc->f.states &= ~(CStatd | CUnique);
2201 ReleaseWriteLock(&afs_xcbhash);
2202 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2203 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2204 ReleaseWriteLock(&tvc->lock);
2209 ObtainWriteLock(&afs_xcbhash, 468);
2210 if (origCBs == afs_allCBs) {
2211 tvc->f.states |= CTruth;
2212 tvc->callback = serverp;
2213 if (CallBack.ExpirationTime != 0) {
2214 tvc->cbExpires = CallBack.ExpirationTime + start;
2215 tvc->f.states |= CStatd;
2216 tvc->f.states &= ~CBulkFetching;
2217 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), tvolp);
2218 } else if (tvc->f.states & CRO) {
2219 /* adapt gives us an hour. */
2220 tvc->cbExpires = 3600 + osi_Time();
2221 /*XXX*/ tvc->f.states |= CStatd;
2222 tvc->f.states &= ~CBulkFetching;
2223 afs_QueueCallback(tvc, CBHash(3600), tvolp);
2226 afs_DequeueCallback(tvc);
2227 tvc->callback = NULL;
2228 tvc->f.states &= ~(CStatd | CUnique);
2229 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2230 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2232 ReleaseWriteLock(&afs_xcbhash);
2233 afs_ProcessFS(tvc, &OutStatus, areq);
2235 ReleaseWriteLock(&tvc->lock);
2241 * Update callback status and (sometimes) attributes of a vnode.
2242 * Called after doing a fetch status RPC. Whilst disconnected, attributes
2243 * shouldn't be written to the vcache here.
2248 * \param Outsp Server status after rpc call.
2249 * \param acb Callback for this vnode.
2251 * \note The vcache must be write locked.
2254 afs_UpdateStatus(struct vcache *avc, struct VenusFid *afid,
2255 struct vrequest *areq, struct AFSFetchStatus *Outsp,
2256 struct AFSCallBack *acb, afs_uint32 start)
2258 struct volume *volp;
2261 /* Dont write status in vcache if resyncing after a disconnection. */
2262 afs_ProcessFS(avc, Outsp, areq);
2264 volp = afs_GetVolume(afid, areq, READ_LOCK);
2265 ObtainWriteLock(&afs_xcbhash, 469);
2266 avc->f.states |= CTruth;
2267 if (avc->callback /* check for race */ ) {
2268 if (acb->ExpirationTime != 0) {
2269 avc->cbExpires = acb->ExpirationTime + start;
2270 avc->f.states |= CStatd;
2271 avc->f.states &= ~CBulkFetching;
2272 afs_QueueCallback(avc, CBHash(acb->ExpirationTime), volp);
2273 } else if (avc->f.states & CRO) {
2274 /* ordinary callback on a read-only volume -- AFS 3.2 style */
2275 avc->cbExpires = 3600 + start;
2276 avc->f.states |= CStatd;
2277 avc->f.states &= ~CBulkFetching;
2278 afs_QueueCallback(avc, CBHash(3600), volp);
2280 afs_DequeueCallback(avc);
2281 avc->callback = NULL;
2282 avc->f.states &= ~(CStatd | CUnique);
2283 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
2284 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2287 afs_DequeueCallback(avc);
2288 avc->callback = NULL;
2289 avc->f.states &= ~(CStatd | CUnique);
2290 if ((avc->f.states & CForeign) || (avc->f.fid.Fid.Vnode & 1))
2291 osi_dnlc_purgedp(avc); /* if it (could be) a directory */
2293 ReleaseWriteLock(&afs_xcbhash);
2295 afs_PutVolume(volp, READ_LOCK);
2299 * Must be called with avc write-locked
2300 * don't absolutely have to invalidate the hint unless the dv has
2301 * changed, but be sure to get it right else there will be consistency bugs.
2304 afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
2305 struct vrequest * areq, struct AFSFetchStatus * Outsp)
2308 afs_uint32 start = 0;
2309 struct afs_conn *tc;
2310 struct AFSCallBack CallBack;
2311 struct AFSVolSync tsync;
2314 tc = afs_Conn(afid, areq, SHARED_LOCK);
2315 avc->dchint = NULL; /* invalidate hints */
2317 avc->callback = tc->parent->srvr->server;
2319 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
2322 RXAFS_FetchStatus(tc->id, (struct AFSFid *)&afid->Fid, Outsp,
2330 } while (afs_Analyze
2331 (tc, code, afid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
2332 SHARED_LOCK, NULL));
2335 afs_UpdateStatus(avc, afid, areq, Outsp, &CallBack, start);
2337 /* used to undo the local callback, but that's too extreme.
2338 * There are plenty of good reasons that fetchstatus might return
2339 * an error, such as EPERM. If we have the vnode cached, statd,
2340 * with callback, might as well keep track of the fact that we
2341 * don't have access...
2343 if (code == EPERM || code == EACCES) {
2344 struct axscache *ac;
2345 if (avc->Access && (ac = afs_FindAxs(avc->Access, areq->uid)))
2347 else /* not found, add a new one if possible */
2348 afs_AddAxs(avc->Access, areq->uid, 0);
2359 * Stuff some information into the vcache for the given file.
2362 * afid : File in question.
2363 * OutStatus : Fetch status on the file.
2364 * CallBack : Callback info.
2365 * tc : RPC connection involved.
2366 * areq : vrequest involved.
2369 * Nothing interesting.
2372 afs_StuffVcache(struct VenusFid *afid,
2373 struct AFSFetchStatus *OutStatus,
2374 struct AFSCallBack *CallBack, struct afs_conn *tc,
2375 struct vrequest *areq)
2377 afs_int32 code, i, newvcache = 0;
2379 struct AFSVolSync tsync;
2381 struct axscache *ac;
2384 AFS_STATCNT(afs_StuffVcache);
2385 #ifdef IFS_VCACHECOUNT
2390 ObtainSharedLock(&afs_xvcache, 8);
2392 tvc = afs_FindVCache(afid, &retry, DO_VLRU| IS_SLOCK /* no stats */ );
2394 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2395 ReleaseSharedLock(&afs_xvcache);
2396 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2402 /* no cache entry, better grab one */
2403 UpgradeSToWLock(&afs_xvcache, 25);
2404 tvc = afs_NewVCache(afid, NULL);
2406 ConvertWToSLock(&afs_xvcache);
2409 ReleaseSharedLock(&afs_xvcache);
2414 ReleaseSharedLock(&afs_xvcache);
2415 ObtainWriteLock(&tvc->lock, 58);
2417 tvc->f.states &= ~CStatd;
2418 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2419 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2421 /* Is it always appropriate to throw away all the access rights? */
2422 afs_FreeAllAxs(&(tvc->Access));
2424 /*Copy useful per-volume info */
2425 tvp = afs_GetVolume(afid, areq, READ_LOCK);
2427 if (newvcache && (tvp->states & VForeign))
2428 tvc->f.states |= CForeign;
2429 if (tvp->states & VRO)
2430 tvc->f.states |= CRO;
2431 if (tvp->states & VBackup)
2432 tvc->f.states |= CBackup;
2434 * Now, copy ".." entry back out of volume structure, if
2437 if (tvc->mvstat == 2 && tvp->dotdot.Fid.Volume != 0) {
2439 tvc->mvid = (struct VenusFid *)
2440 osi_AllocSmallSpace(sizeof(struct VenusFid));
2441 *tvc->mvid = tvp->dotdot;
2444 /* store the stat on the file */
2445 afs_RemoveVCB(afid);
2446 afs_ProcessFS(tvc, OutStatus, areq);
2447 tvc->callback = tc->srvr->server;
2449 /* we use osi_Time twice below. Ideally, we would use the time at which
2450 * the FetchStatus call began, instead, but we don't have it here. So we
2451 * make do with "now". In the CRO case, it doesn't really matter. In
2452 * the other case, we hope that the difference between "now" and when the
2453 * call actually began execution on the server won't be larger than the
2454 * padding which the server keeps. Subtract 1 second anyway, to be on
2455 * the safe side. Can't subtract more because we don't know how big
2456 * ExpirationTime is. Possible consistency problems may arise if the call
2457 * timeout period becomes longer than the server's expiration padding. */
2458 ObtainWriteLock(&afs_xcbhash, 470);
2459 if (CallBack->ExpirationTime != 0) {
2460 tvc->cbExpires = CallBack->ExpirationTime + osi_Time() - 1;
2461 tvc->f.states |= CStatd;
2462 tvc->f.states &= ~CBulkFetching;
2463 afs_QueueCallback(tvc, CBHash(CallBack->ExpirationTime), tvp);
2464 } else if (tvc->f.states & CRO) {
2465 /* old-fashioned AFS 3.2 style */
2466 tvc->cbExpires = 3600 + osi_Time();
2467 /*XXX*/ tvc->f.states |= CStatd;
2468 tvc->f.states &= ~CBulkFetching;
2469 afs_QueueCallback(tvc, CBHash(3600), tvp);
2471 afs_DequeueCallback(tvc);
2472 tvc->callback = NULL;
2473 tvc->f.states &= ~(CStatd | CUnique);
2474 if ((tvc->f.states & CForeign) || (tvc->f.fid.Fid.Vnode & 1))
2475 osi_dnlc_purgedp(tvc); /* if it (could be) a directory */
2477 ReleaseWriteLock(&afs_xcbhash);
2479 afs_PutVolume(tvp, READ_LOCK);
2481 /* look in per-pag cache */
2482 if (tvc->Access && (ac = afs_FindAxs(tvc->Access, areq->uid)))
2483 ac->axess = OutStatus->CallerAccess; /* substitute pags */
2484 else /* not found, add a new one if possible */
2485 afs_AddAxs(tvc->Access, areq->uid, OutStatus->CallerAccess);
2487 ReleaseWriteLock(&tvc->lock);
2488 afs_Trace4(afs_iclSetp, CM_TRACE_STUFFVCACHE, ICL_TYPE_POINTER, tvc,
2489 ICL_TYPE_POINTER, tvc->callback, ICL_TYPE_INT32,
2490 tvc->cbExpires, ICL_TYPE_INT32, tvc->cbExpires - osi_Time());
2492 * Release ref count... hope this guy stays around...
2495 } /*afs_StuffVcache */
2499 * Decrements the reference count on a cache entry.
2501 * \param avc Pointer to the cache entry to decrement.
2503 * \note Environment: Nothing interesting.
2506 afs_PutVCache(struct vcache *avc)
2508 AFS_STATCNT(afs_PutVCache);
2509 #ifdef AFS_DARWIN80_ENV
2510 vnode_put(AFSTOV(avc));
2514 * Can we use a read lock here?
2516 ObtainReadLock(&afs_xvcache);
2518 ReleaseReadLock(&afs_xvcache);
2520 } /*afs_PutVCache */
2524 * Reset a vcache entry, so local contents are ignored, and the
2525 * server will be reconsulted next time the vcache is used
2527 * \param avc Pointer to the cache entry to reset
2530 * \note avc must be write locked on entry
2533 afs_ResetVCache(struct vcache *avc, afs_ucred_t *acred)
2535 ObtainWriteLock(&afs_xcbhash, 456);
2536 afs_DequeueCallback(avc);
2537 avc->f.states &= ~(CStatd | CDirty); /* next reference will re-stat */
2538 ReleaseWriteLock(&afs_xcbhash);
2539 /* now find the disk cache entries */
2540 afs_TryToSmush(avc, acred, 1);
2541 osi_dnlc_purgedp(avc);
2542 if (avc->linkData && !(avc->f.states & CCore)) {
2543 afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
2544 avc->linkData = NULL;
2549 * Sleepa when searching for a vcache. Releases all the pending locks,
2550 * sleeps then obtains the previously released locks.
2552 * \param vcache Enter sleep state.
2553 * \param flag Determines what locks to use.
2558 findvc_sleep(struct vcache *avc, int flag)
2560 int fstates = avc->f.states;
2561 if (flag & IS_SLOCK) {
2562 ReleaseSharedLock(&afs_xvcache);
2564 if (flag & IS_WLOCK) {
2565 ReleaseWriteLock(&afs_xvcache);
2567 ReleaseReadLock(&afs_xvcache);
2570 if (flag & FIND_CDEAD) {
2571 ObtainWriteLock(&afs_xvcache, 342);
2572 afs_FlushReclaimedVcaches();
2573 if (fstates == avc->f.states) {
2574 ReleaseWriteLock(&afs_xvcache);
2575 afs_osi_Sleep(&avc->f.states);
2577 ReleaseWriteLock(&afs_xvcache);
2579 afs_osi_Sleep(&avc->f.states);
2580 if (flag & IS_SLOCK) {
2581 ObtainSharedLock(&afs_xvcache, 341);
2583 if (flag & IS_WLOCK) {
2584 ObtainWriteLock(&afs_xvcache, 343);
2586 ObtainReadLock(&afs_xvcache);
2592 * Add a reference on an existing vcache entry.
2594 * \param tvc Pointer to the vcache.
2596 * \note Environment: Must be called with at least one reference from
2597 * elsewhere on the vcache, even if that reference will be dropped.
2598 * The global lock is required.
2600 * \return 0 on success, -1 on failure.
2604 afs_RefVCache(struct vcache *tvc)
2606 #ifdef AFS_DARWIN80_ENV
2610 /* AFS_STATCNT(afs_RefVCache); */
2612 #ifdef AFS_DARWIN80_ENV
2616 if (vnode_ref(tvp)) {
2618 /* AFSTOV(tvc) may be NULL */
2627 } /*afs_RefVCache */
2630 * Find a vcache entry given a fid.
2632 * \param afid Pointer to the fid whose cache entry we desire.
2633 * \param retry (SGI-specific) tell the caller to drop the lock on xvcache,
2634 * unlock the vnode, and try again.
2635 * \param flag Bit 1 to specify whether to compute hit statistics. Not
2636 * set if FindVCache is called as part of internal bookkeeping.
2638 * \note Environment: Must be called with the afs_xvcache lock at least held at
2639 * the read level. In order to do the VLRU adjustment, the xvcache lock
2640 * must be shared-- we upgrade it here.
2644 afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
2649 #ifdef AFS_DARWIN80_ENV
2653 AFS_STATCNT(afs_FindVCache);
2657 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2658 if (FidMatches(afid, tvc)) {
2659 if (tvc->f.states & CVInit) {
2660 findvc_sleep(tvc, flag);
2663 #ifdef AFS_DARWIN80_ENV
2664 if (tvc->f.states & CDeadVnode) {
2665 if (!(flag & FIND_CDEAD)) {
2666 findvc_sleep(tvc, flag);
2673 if (vnode_ref(tvp)) {
2675 /* AFSTOV(tvc) may be NULL */
2680 if (tvc->f.states & (CBulkFetching|CDeadVnode)) {
2682 vnode_recycle(AFSTOV(tvc));
2690 /* should I have a read lock on the vnode here? */
2694 #if !defined(AFS_DARWIN80_ENV)
2695 osi_vnhold(tvc, retry); /* already held, above */
2696 if (retry && *retry)
2699 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
2700 tvc->f.states |= CUBCinit;
2702 if (UBCINFOMISSING(AFSTOV(tvc)) ||
2703 UBCINFORECLAIMED(AFSTOV(tvc))) {
2704 ubc_info_init(AFSTOV(tvc));
2707 tvc->f.states &= ~CUBCinit;
2710 * only move to front of vlru if we have proper vcache locking)
2712 if (flag & DO_VLRU) {
2713 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2714 refpanic("FindVC VLRU inconsistent1");
2716 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2717 refpanic("FindVC VLRU inconsistent1");
2719 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2720 refpanic("FindVC VLRU inconsistent2");
2722 UpgradeSToWLock(&afs_xvcache, 26);
2723 QRemove(&tvc->vlruq);
2724 QAdd(&VLRU, &tvc->vlruq);
2725 ConvertWToSLock(&afs_xvcache);
2726 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2727 refpanic("FindVC VLRU inconsistent1");
2729 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2730 refpanic("FindVC VLRU inconsistent2");
2732 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2733 refpanic("FindVC VLRU inconsistent3");
2739 if (flag & DO_STATS) {
2741 afs_stats_cmperf.vcacheHits++;
2743 afs_stats_cmperf.vcacheMisses++;
2744 if (afs_IsPrimaryCellNum(afid->Cell))
2745 afs_stats_cmperf.vlocalAccesses++;
2747 afs_stats_cmperf.vremoteAccesses++;
2750 } /*afs_FindVCache */
2753 * Find a vcache entry given a fid. Does a wildcard match on what we
2754 * have for the fid. If more than one entry, don't return anything.
2756 * \param avcp Fill in pointer if we found one and only one.
2757 * \param afid Pointer to the fid whose cache entry we desire.
2758 * \param retry (SGI-specific) tell the caller to drop the lock on xvcache,
2759 * unlock the vnode, and try again.
2760 * \param flags bit 1 to specify whether to compute hit statistics. Not
2761 * set if FindVCache is called as part of internal bookkeeping.
2763 * \note Environment: Must be called with the afs_xvcache lock at least held at
2764 * the read level. In order to do the VLRU adjustment, the xvcache lock
2765 * must be shared-- we upgrade it here.
2767 * \return Number of matches found.
2770 int afs_duplicate_nfs_fids = 0;
2773 afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
2777 afs_int32 count = 0;
2778 struct vcache *found_tvc = NULL;
2779 #ifdef AFS_DARWIN80_ENV
2783 AFS_STATCNT(afs_FindVCache);
2787 ObtainSharedLock(&afs_xvcache, 331);
2790 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
2791 /* Match only on what we have.... */
2792 if (((tvc->f.fid.Fid.Vnode & 0xffff) == afid->Fid.Vnode)
2793 && (tvc->f.fid.Fid.Volume == afid->Fid.Volume)
2794 && ((tvc->f.fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
2795 && (tvc->f.fid.Cell == afid->Cell)) {
2796 if (tvc->f.states & CVInit) {
2797 ReleaseSharedLock(&afs_xvcache);
2798 afs_osi_Sleep(&tvc->f.states);
2801 #ifdef AFS_DARWIN80_ENV
2802 if (tvc->f.states & CDeadVnode) {
2803 if (!(tvc->f.states & CBulkFetching)) {
2804 ReleaseSharedLock(&afs_xvcache);
2805 afs_osi_Sleep(&tvc->f.states);
2810 if (vnode_get(tvp)) {
2811 /* This vnode no longer exists. */
2814 if (vnode_ref(tvp)) {
2815 /* This vnode no longer exists. */
2817 /* AFSTOV(tvc) may be NULL */
2822 if (tvc->f.states & (CBulkFetching|CDeadVnode)) {
2824 vnode_recycle(AFSTOV(tvc));
2827 #endif /* AFS_DARWIN80_ENV */
2831 afs_duplicate_nfs_fids++;
2832 ReleaseSharedLock(&afs_xvcache);
2833 #ifdef AFS_DARWIN80_ENV
2834 /* Drop our reference counts. */
2835 vnode_put(AFSTOV(tvc));
2836 vnode_put(AFSTOV(found_tvc));
2845 /* should I have a read lock on the vnode here? */
2847 #ifndef AFS_DARWIN80_ENV
2848 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
2849 afs_int32 retry = 0;
2850 osi_vnhold(tvc, &retry);
2853 found_tvc = (struct vcache *)0;
2854 ReleaseSharedLock(&afs_xvcache);
2855 spunlock_psema(tvc->v.v_lock, retry, &tvc->v.v_sync, PINOD);
2859 osi_vnhold(tvc, (int *)0); /* already held, above */
2863 * We obtained the xvcache lock above.
2865 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2866 refpanic("FindVC VLRU inconsistent1");
2868 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2869 refpanic("FindVC VLRU inconsistent1");
2871 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2872 refpanic("FindVC VLRU inconsistent2");
2874 UpgradeSToWLock(&afs_xvcache, 568);
2875 QRemove(&tvc->vlruq);
2876 QAdd(&VLRU, &tvc->vlruq);
2877 ConvertWToSLock(&afs_xvcache);
2878 if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
2879 refpanic("FindVC VLRU inconsistent1");
2881 if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
2882 refpanic("FindVC VLRU inconsistent2");
2884 if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
2885 refpanic("FindVC VLRU inconsistent3");
2891 afs_stats_cmperf.vcacheHits++;
2893 afs_stats_cmperf.vcacheMisses++;
2894 if (afs_IsPrimaryCellNum(afid->Cell))
2895 afs_stats_cmperf.vlocalAccesses++;
2897 afs_stats_cmperf.vremoteAccesses++;
2899 *avcp = tvc; /* May be null */
2901 ReleaseSharedLock(&afs_xvcache);
2902 return (tvc ? 1 : 0);
2904 } /*afs_NFSFindVCache */
2910 * Initialize vcache related variables
2915 afs_vcacheInit(int astatSize)
2917 #if !defined(AFS_LINUX22_ENV)
2921 if (!afs_maxvcount) {
2922 afs_maxvcount = astatSize; /* no particular limit on linux? */
2924 #if !defined(AFS_LINUX22_ENV)
2928 AFS_RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
2929 LOCK_INIT(&afs_xvcb, "afs_xvcb");
2931 #if !defined(AFS_LINUX22_ENV)
2932 /* Allocate and thread the struct vcache entries */
2933 tvp = afs_osi_Alloc(astatSize * sizeof(struct vcache));
2934 osi_Assert(tvp != NULL);
2935 memset(tvp, 0, sizeof(struct vcache) * astatSize);
2937 Initial_freeVCList = tvp;
2938 freeVCList = &(tvp[0]);
2939 for (i = 0; i < astatSize - 1; i++) {
2940 tvp[i].nextfree = &(tvp[i + 1]);
2942 tvp[astatSize - 1].nextfree = NULL;
2943 # ifdef KERNEL_HAVE_PIN
2944 pin((char *)tvp, astatSize * sizeof(struct vcache)); /* XXX */
2948 #if defined(AFS_SGI_ENV)
2949 for (i = 0; i < astatSize; i++) {
2950 char name[METER_NAMSZ];
2951 struct vcache *tvc = &tvp[i];
2953 tvc->v.v_number = ++afsvnumbers;
2954 tvc->vc_rwlockid = OSI_NO_LOCKID;
2955 initnsema(&tvc->vc_rwlock, 1,
2956 makesname(name, "vrw", tvc->v.v_number));
2957 #ifndef AFS_SGI53_ENV
2958 initnsema(&tvc->v.v_sync, 0, makesname(name, "vsy", tvc->v.v_number));
2960 #ifndef AFS_SGI62_ENV
2961 initnlock(&tvc->v.v_lock, makesname(name, "vlk", tvc->v.v_number));
2962 #endif /* AFS_SGI62_ENV */
2966 for(i = 0; i < VCSIZE; ++i)
2967 QInit(&afs_vhashTV[i]);
2974 shutdown_vcache(void)
2977 struct afs_cbr *tsp;
2979 * XXX We may potentially miss some of the vcaches because if when
2980 * there are no free vcache entries and all the vcache entries are active
2981 * ones then we allocate an additional one - admittedly we almost never
2986 struct afs_q *tq, *uq = NULL;
2988 for (tq = VLRU.prev; tq != &VLRU; tq = uq) {
2992 osi_FreeSmallSpace(tvc->mvid);
2993 tvc->mvid = (struct VenusFid *)0;
2996 aix_gnode_rele(AFSTOV(tvc));
2998 if (tvc->linkData) {
2999 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3004 * Also free the remaining ones in the Cache
3006 for (i = 0; i < VCSIZE; i++) {
3007 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3009 osi_FreeSmallSpace(tvc->mvid);
3010 tvc->mvid = (struct VenusFid *)0;
3014 afs_osi_Free(tvc->v.v_gnode, sizeof(struct gnode));
3015 #ifdef AFS_AIX32_ENV
3018 vms_delete(tvc->segid);
3020 tvc->segid = tvc->vmh = NULL;
3021 if (VREFCOUNT_GT(tvc,0))
3022 osi_Panic("flushVcache: vm race");
3030 #if defined(AFS_SUN5_ENV)
3036 if (tvc->linkData) {
3037 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
3042 afs_FreeAllAxs(&(tvc->Access));
3048 * Free any leftover callback queue
3050 for (i = 0; i < afs_stats_cmperf.CallBackAlloced; i++) {
3051 tsp = afs_cbrHeads[i];
3052 afs_cbrHeads[i] = 0;
3053 afs_osi_Free((char *)tsp, AFS_NCBRS * sizeof(struct afs_cbr));
3057 #if !defined(AFS_LINUX22_ENV)
3058 afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3060 # ifdef KERNEL_HAVE_PIN
3061 unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
3064 freeVCList = Initial_freeVCList = 0;
3067 AFS_RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
3068 LOCK_INIT(&afs_xvcb, "afs_xvcb");
3070 for(i = 0; i < VCSIZE; ++i)
3071 QInit(&afs_vhashTV[i]);
3075 afs_DisconGiveUpCallbacks(void)
3081 ObtainWriteLock(&afs_xvcache, 1002); /* XXX - should be a unique number */
3083 /* Somehow, walk the set of vcaches, with each one coming out as tvc */
3084 for (i = 0; i < VCSIZE; i++) {
3085 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3086 if (afs_QueueVCB(tvc)) {
3087 tvc->callback = NULL;
3093 ReleaseWriteLock(&afs_xvcache);
3100 * Clear the Statd flag from all vcaches
3102 * This function removes the Statd flag from all vcaches. It's used by
3103 * disconnected mode to tidy up during reconnection
3107 afs_ClearAllStatdFlag(void)
3112 ObtainWriteLock(&afs_xvcache, 715);
3114 for (i = 0; i < VCSIZE; i++) {
3115 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
3116 tvc->f.states &= ~(CStatd|CUnique);
3119 ReleaseWriteLock(&afs_xvcache);