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
13 #include <afsconfig.h>
14 #include "../afs/param.h"
18 #include "../afs/sysincludes.h" /*Standard vendor system headers*/
19 #include "../afs/afsincludes.h" /*AFS-based standard headers*/
20 #include "../afs/afs_stats.h" /* statistics */
21 #include "../afs/afs_cbqueue.h"
22 #include "../afs/afs_osidnlc.h"
24 /* Forward declarations. */
25 static void afs_GetDownD(int anumber, int *aneedSpace);
26 static void afs_FreeDiscardedDCache(void);
27 static void afs_DiscardDCache(struct dcache *);
28 static void afs_FreeDCache(struct dcache *);
30 /* Imported variables */
31 extern afs_rwlock_t afs_xvcache;
32 extern afs_rwlock_t afs_xcbhash;
33 extern afs_int32 afs_mariner;
34 extern afs_int32 cacheInfoModTime; /*Last time cache info modified*/
38 * --------------------- Exported definitions ---------------------
40 afs_lock_t afs_xdcache; /*Lock: alloc new disk cache entries*/
41 afs_int32 afs_freeDCList; /*Free list for disk cache entries*/
42 afs_int32 afs_freeDCCount; /*Count of elts in freeDCList*/
43 afs_int32 afs_discardDCList; /*Discarded disk cache entries*/
44 afs_int32 afs_discardDCCount; /*Count of elts in discardDCList*/
45 struct dcache *afs_freeDSList; /*Free list for disk slots */
46 struct dcache *afs_Initial_freeDSList; /*Initial list for above*/
47 ino_t cacheInode; /*Inode for CacheItems file*/
48 struct osi_file *afs_cacheInodep = 0; /* file for CacheItems inode */
49 struct afs_q afs_DLRU; /*dcache LRU*/
50 afs_int32 afs_dhashsize = 1024;
51 afs_int32 *afs_dvhashTbl; /*Data cache hash table*/
52 afs_int32 *afs_dchashTbl; /*Data cache hash table*/
53 afs_int32 *afs_dvnextTbl; /*Dcache hash table links */
54 afs_int32 *afs_dcnextTbl; /*Dcache hash table links */
55 struct dcache **afs_indexTable; /*Pointers to dcache entries*/
56 afs_hyper_t *afs_indexTimes; /*Dcache entry Access times*/
57 afs_int32 *afs_indexUnique; /*dcache entry Fid.Unique */
58 unsigned char *afs_indexFlags; /*(only one) Is there data there?*/
59 afs_hyper_t afs_indexCounter; /*Fake time for marking index
61 afs_int32 afs_cacheFiles =0; /*Size of afs_indexTable*/
62 afs_int32 afs_cacheBlocks; /*1K blocks in cache*/
63 afs_int32 afs_cacheStats; /*Stat entries in cache*/
64 afs_int32 afs_blocksUsed; /*Number of blocks in use*/
65 afs_int32 afs_blocksDiscarded; /*Blocks freed but not truncated */
66 afs_int32 afs_fsfragsize = 1023; /*Underlying Filesystem minimum unit
67 *of disk allocation usually 1K
68 *this value is (truefrag -1 ) to
69 *save a bunch of subtracts... */
70 #ifdef AFS_64BIT_CLIENT
71 #ifdef AFS_VM_RDWR_ENV
72 afs_size_t afs_vmMappingEnd; /* for large files (>= 2GB) the VM
73 * mapping an 32bit addressing machines
74 * can only be used below the 2 GB
75 * line. From this point upwards we
76 * must do direct I/O into the cache
77 * files. The value should be on a
79 #endif /* AFS_VM_RDWR_ENV */
80 #endif /* AFS_64BIT_CLIENT */
82 /* The following is used to ensure that new dcache's aren't obtained when
83 * the cache is nearly full.
85 int afs_WaitForCacheDrain = 0;
86 int afs_TruncateDaemonRunning = 0;
87 int afs_CacheTooFull = 0;
89 afs_int32 afs_dcentries; /* In-memory dcache entries */
92 int dcacheDisabled = 0;
94 extern struct dcache *afs_UFSGetDSlot();
95 extern struct volume *afs_UFSGetVolSlot();
96 extern int osi_UFSTruncate(), afs_osi_Read(), afs_osi_Write(), osi_UFSClose();
97 extern int afs_UFSRead(), afs_UFSWrite();
98 static int afs_UFSCacheFetchProc(), afs_UFSCacheStoreProc();
99 extern int afs_UFSHandleLink();
100 struct afs_cacheOps afs_UfsCacheOps = {
108 afs_UFSCacheFetchProc,
109 afs_UFSCacheStoreProc,
115 extern void *afs_MemCacheOpen();
116 extern struct dcache *afs_MemGetDSlot();
117 extern struct volume *afs_MemGetVolSlot();
118 extern int afs_MemCacheTruncate(), afs_MemReadBlk(), afs_MemWriteBlk(), afs_MemCacheClose();
119 extern int afs_MemRead(), afs_MemWrite(), afs_MemCacheFetchProc(), afs_MemCacheStoreProc();
120 extern int afs_MemHandleLink();
121 struct afs_cacheOps afs_MemCacheOps = {
123 afs_MemCacheTruncate,
129 afs_MemCacheFetchProc,
130 afs_MemCacheStoreProc,
136 int cacheDiskType; /*Type of backing disk for cache*/
137 struct afs_cacheOps *afs_cacheType;
146 * Warn about failing to store a file.
149 * acode : Associated error code.
150 * avolume : Volume involved.
151 * aflags : How to handle the output:
152 * aflags & 1: Print out on console
153 * aflags & 2: Print out on controlling tty
156 * Call this from close call when vnodeops is RCS unlocked.
160 afs_StoreWarn(acode, avolume, aflags)
161 register afs_int32 acode;
163 register afs_int32 aflags;
167 static char problem_fmt[] =
168 "afs: failed to store file in volume %d (%s)\n";
169 static char problem_fmt_w_error[] =
170 "afs: failed to store file in volume %d (error %d)\n";
171 static char netproblems[] = "network problems";
172 static char partfull[] = "partition full";
173 static char overquota[] = "over quota";
174 static char unknownerr[] = "unknown error";
176 AFS_STATCNT(afs_StoreWarn);
182 afs_warn(problem_fmt, avolume, netproblems);
184 afs_warnuser(problem_fmt, avolume, netproblems);
187 if (acode == ENOSPC) {
192 afs_warn(problem_fmt, avolume, partfull);
194 afs_warnuser(problem_fmt, avolume, partfull);
198 /* EDQUOT doesn't exist on solaris and won't be sent by the server.
199 * Instead ENOSPC will be sent...
201 if (acode == EDQUOT) {
206 afs_warn(problem_fmt, avolume, overquota);
208 afs_warnuser(problem_fmt, avolume, overquota);
216 afs_warn(problem_fmt_w_error, avolume, acode);
218 afs_warnuser(problem_fmt_w_error, avolume, acode);
222 void afs_MaybeWakeupTruncateDaemon() {
223 if (!afs_CacheTooFull && afs_CacheIsTooFull()) {
224 afs_CacheTooFull = 1;
225 if (!afs_TruncateDaemonRunning)
226 afs_osi_Wakeup((char *)afs_CacheTruncateDaemon);
227 } else if (!afs_TruncateDaemonRunning &&
228 afs_blocksDiscarded > CM_MAXDISCARDEDCHUNKS) {
229 afs_osi_Wakeup((char *)afs_CacheTruncateDaemon);
233 /* Keep statistics on run time for afs_CacheTruncateDaemon. This is a
234 * struct so we need only export one symbol for AIX.
237 osi_timeval_t CTD_beforeSleep;
238 osi_timeval_t CTD_afterSleep;
239 osi_timeval_t CTD_sleepTime;
240 osi_timeval_t CTD_runTime;
244 u_int afs_min_cache = 0;
245 void afs_CacheTruncateDaemon() {
246 osi_timeval_t CTD_tmpTime;
249 u_int dc_hiwat = (100-CM_DCACHECOUNTFREEPCT+CM_DCACHEEXTRAPCT)*afs_cacheFiles/100;
250 afs_min_cache = (((10 * AFS_CHUNKSIZE(0)) + afs_fsfragsize) & ~afs_fsfragsize)>>10;
252 osi_GetuTime(&CTD_stats.CTD_afterSleep);
253 afs_TruncateDaemonRunning = 1;
255 cb_lowat = ((CM_DCACHESPACEFREEPCT-CM_DCACHEEXTRAPCT)
256 * afs_cacheBlocks) / 100;
257 MObtainWriteLock(&afs_xdcache,266);
258 if (afs_CacheTooFull) {
259 int space_needed, slots_needed;
260 /* if we get woken up, we should try to clean something out */
261 for (counter = 0; counter < 10; counter++) {
262 space_needed = afs_blocksUsed - afs_blocksDiscarded - cb_lowat;
263 slots_needed = dc_hiwat - afs_freeDCCount - afs_discardDCCount;
264 afs_GetDownD(slots_needed, &space_needed);
265 if ((space_needed <= 0) && (slots_needed <= 0)) {
268 if (afs_termState == AFSOP_STOP_TRUNCDAEMON)
271 if (!afs_CacheIsTooFull())
272 afs_CacheTooFull = 0;
274 MReleaseWriteLock(&afs_xdcache);
277 * This is a defensive check to try to avoid starving threads
278 * that may need the global lock so thay can help free some
279 * cache space. If this thread won't be sleeping or truncating
280 * any cache files then give up the global lock so other
281 * threads get a chance to run.
283 if ((afs_termState!=AFSOP_STOP_TRUNCDAEMON) && afs_CacheTooFull &&
284 (!afs_blocksDiscarded || afs_WaitForCacheDrain)) {
285 afs_osi_Wait(100, 0, 0); /* 100 milliseconds */
289 * This is where we free the discarded cache elements.
291 while(afs_blocksDiscarded && !afs_WaitForCacheDrain &&
292 (afs_termState!=AFSOP_STOP_TRUNCDAEMON))
294 afs_FreeDiscardedDCache();
297 /* See if we need to continue to run. Someone may have
298 * signalled us while we were executing.
300 if (!afs_WaitForCacheDrain && !afs_CacheTooFull &&
301 (afs_termState!=AFSOP_STOP_TRUNCDAEMON))
303 /* Collect statistics on truncate daemon. */
304 CTD_stats.CTD_nSleeps++;
305 osi_GetuTime(&CTD_stats.CTD_beforeSleep);
306 afs_stats_GetDiff(CTD_tmpTime, CTD_stats.CTD_afterSleep,
307 CTD_stats.CTD_beforeSleep);
308 afs_stats_AddTo(CTD_stats.CTD_runTime, CTD_tmpTime);
310 afs_TruncateDaemonRunning = 0;
311 afs_osi_Sleep((char *)afs_CacheTruncateDaemon);
312 afs_TruncateDaemonRunning = 1;
314 osi_GetuTime(&CTD_stats.CTD_afterSleep);
315 afs_stats_GetDiff(CTD_tmpTime, CTD_stats.CTD_beforeSleep,
316 CTD_stats.CTD_afterSleep);
317 afs_stats_AddTo(CTD_stats.CTD_sleepTime, CTD_tmpTime);
319 if (afs_termState == AFSOP_STOP_TRUNCDAEMON) {
321 afs_termState = AFSOP_STOP_AFSDB;
323 afs_termState = AFSOP_STOP_RXEVENT;
325 afs_osi_Wakeup(&afs_termState);
336 * Make adjustment for the new size in the disk cache entry
338 * Major Assumptions Here:
339 * Assumes that frag size is an integral power of two, less one,
340 * and that this is a two's complement machine. I don't
341 * know of any filesystems which violate this assumption...
344 * adc : Ptr to dcache entry.
345 * anewsize : New size desired.
349 afs_AdjustSize(adc, newSize)
350 register struct dcache *adc;
351 register afs_int32 newSize;
355 register afs_int32 oldSize;
357 AFS_STATCNT(afs_AdjustSize);
359 adc->dflags |= DFEntryMod;
360 oldSize = ((adc->f.chunkBytes + afs_fsfragsize)^afs_fsfragsize)>>10;/* round up */
361 adc->f.chunkBytes = newSize;
362 newSize = ((newSize + afs_fsfragsize)^afs_fsfragsize)>>10;/* round up */
363 if (newSize > oldSize) {
364 /* We're growing the file, wakeup the daemon */
365 afs_MaybeWakeupTruncateDaemon();
367 afs_blocksUsed += (newSize - oldSize);
368 afs_stats_cmperf.cacheBlocksInUse = afs_blocksUsed; /* XXX */
380 * This routine is responsible for moving at least one entry (but up
381 * to some number of them) from the LRU queue to the free queue.
384 * anumber : Number of entries that should ideally be moved.
385 * aneedSpace : How much space we need (1K blocks);
388 * The anumber parameter is just a hint; at least one entry MUST be
389 * moved, or we'll panic. We must be called with afs_xdcache
390 * write-locked. We should try to satisfy both anumber and aneedspace,
391 * whichever is more demanding - need to do several things:
392 * 1. only grab up to anumber victims if aneedSpace <= 0, not
393 * the whole set of MAXATONCE.
394 * 2. dynamically choose MAXATONCE to reflect severity of
395 * demand: something like (*aneedSpace >> (logChunk - 9))
396 * N.B. if we're called with aneedSpace <= 0 and anumber > 0, that
397 * indicates that the cache is not properly configured/tuned or
398 * something. We should be able to automatically correct that problem.
401 #define MAXATONCE 16 /* max we can obtain at once */
402 static void afs_GetDownD(int anumber, int *aneedSpace)
406 struct VenusFid *afid;
410 register struct vcache *tvc;
411 afs_uint32 victims[MAXATONCE];
412 struct dcache *victimDCs[MAXATONCE];
413 afs_hyper_t victimTimes[MAXATONCE];/* youngest (largest LRU time) first */
414 afs_uint32 victimPtr; /* next free item in victim arrays */
415 afs_hyper_t maxVictimTime; /* youngest (largest LRU time) victim */
416 afs_uint32 maxVictimPtr; /* where it is */
419 AFS_STATCNT(afs_GetDownD);
420 if (CheckLock(&afs_xdcache) != -1)
421 osi_Panic("getdownd nolock");
422 /* decrement anumber first for all dudes in free list */
423 /* SHOULD always decrement anumber first, even if aneedSpace >0,
424 * because we should try to free space even if anumber <=0 */
425 if (!aneedSpace || *aneedSpace <= 0) {
426 anumber -= afs_freeDCCount;
427 if (anumber <= 0) return; /* enough already free */
429 /* bounds check parameter */
430 if (anumber > MAXATONCE)
431 anumber = MAXATONCE; /* all we can do */
434 * The phase variable manages reclaims. Set to 0, the first pass,
435 * we don't reclaim active entries. Set to 1, we reclaim even active
439 for (i = 0; i < afs_cacheFiles; i++)
440 /* turn off all flags */
441 afs_indexFlags[i] &= ~IFFlag;
443 while (anumber > 0 || (aneedSpace && *aneedSpace >0)) {
444 /* find oldest entries for reclamation */
445 maxVictimPtr = victimPtr = 0;
446 hzero(maxVictimTime);
447 /* select victims from access time array */
448 for (i = 0; i < afs_cacheFiles; i++) {
449 if (afs_indexFlags[i] & (IFDataMod | IFFree | IFDiscarded)) {
450 /* skip if dirty or already free */
453 tdc = afs_indexTable[i];
454 if (tdc && (tdc->refCount != 0)) {
455 /* Referenced; can't use it! */
458 hset(vtime, afs_indexTimes[i]);
460 /* if we've already looked at this one, skip it */
461 if (afs_indexFlags[i] & IFFlag) continue;
463 if (victimPtr < MAXATONCE) {
464 /* if there's at least one free victim slot left */
465 victims[victimPtr] = i;
466 hset(victimTimes[victimPtr], vtime);
467 if (hcmp(vtime, maxVictimTime) > 0) {
468 hset(maxVictimTime, vtime);
469 maxVictimPtr = victimPtr;
473 else if (hcmp(vtime, maxVictimTime) < 0) {
475 * We're older than youngest victim, so we replace at
478 /* find youngest (largest LRU) victim */
480 if (j == victimPtr) osi_Panic("getdownd local");
482 hset(victimTimes[j], vtime);
483 /* recompute maxVictimTime */
484 hset(maxVictimTime, vtime);
485 for(j = 0; j < victimPtr; j++)
486 if (hcmp(maxVictimTime, victimTimes[j]) < 0) {
487 hset(maxVictimTime, victimTimes[j]);
493 /* now really reclaim the victims */
494 j = 0; /* flag to track if we actually got any of the victims */
495 /* first, hold all the victims, since we're going to release the lock
496 * during the truncate operation.
498 for(i=0; i < victimPtr; i++) {
499 tdc = afs_GetDSlot(victims[i], 0);
500 /* We got tdc->tlock(R) here */
501 if (tdc->refCount == 1)
505 ReleaseReadLock(&tdc->tlock);
506 if (!victimDCs[i]) afs_PutDCache(tdc);
508 for(i = 0; i < victimPtr; i++) {
509 /* q is first elt in dcache entry */
511 /* now, since we're dropping the afs_xdcache lock below, we
512 * have to verify, before proceeding, that there are no other
513 * references to this dcache entry, even now. Note that we
514 * compare with 1, since we bumped it above when we called
515 * afs_GetDSlot to preserve the entry's identity.
517 if (tdc && tdc->refCount == 1) {
518 unsigned char chunkFlags;
519 afs_size_t tchunkoffset;
521 /* xdcache is lower than the xvcache lock */
522 MReleaseWriteLock(&afs_xdcache);
523 MObtainReadLock(&afs_xvcache);
524 tvc = afs_FindVCache(afid, 0,0, 0, 0 /* no stats, no vlru */ );
525 MReleaseReadLock(&afs_xvcache);
526 MObtainWriteLock(&afs_xdcache, 527);
528 if (tdc->refCount > 1) skip = 1;
530 tchunkoffset = AFS_CHUNKTOBASE(tdc->f.chunk);
531 chunkFlags = afs_indexFlags[tdc->index];
532 if (phase == 0 && osi_Active(tvc)) skip = 1;
533 if (phase > 0 && osi_Active(tvc) && (tvc->states & CDCLock)
534 && (chunkFlags & IFAnyPages)) skip = 1;
535 if (chunkFlags & IFDataMod) skip = 1;
536 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
537 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, skip,
538 ICL_TYPE_INT32, tdc->index,
540 ICL_HANDLE_OFFSET(tchunkoffset));
542 #if defined(AFS_SUN5_ENV)
544 * Now we try to invalidate pages. We do this only for
545 * Solaris. For other platforms, it's OK to recycle a
546 * dcache entry out from under a page, because the strategy
547 * function can call afs_GetDCache().
549 if (!skip && (chunkFlags & IFAnyPages)) {
552 MReleaseWriteLock(&afs_xdcache);
553 MObtainWriteLock(&tvc->vlock, 543);
554 if (tvc->multiPage) {
558 /* block locking pages */
559 tvc->vstates |= VPageCleaning;
560 /* block getting new pages */
562 MReleaseWriteLock(&tvc->vlock);
563 /* One last recheck */
564 MObtainWriteLock(&afs_xdcache, 333);
565 chunkFlags = afs_indexFlags[tdc->index];
566 if (tdc->refCount > 1
567 || (chunkFlags & IFDataMod)
568 || (osi_Active(tvc) && (tvc->states & CDCLock)
569 && (chunkFlags & IFAnyPages))) {
571 MReleaseWriteLock(&afs_xdcache);
574 MReleaseWriteLock(&afs_xdcache);
576 code = osi_VM_GetDownD(tvc, tdc);
578 MObtainWriteLock(&afs_xdcache,269);
579 /* we actually removed all pages, clean and dirty */
581 afs_indexFlags[tdc->index] &= ~(IFDirtyPages| IFAnyPages);
584 MReleaseWriteLock(&afs_xdcache);
586 MObtainWriteLock(&tvc->vlock, 544);
587 if (--tvc->activeV == 0 && (tvc->vstates & VRevokeWait)) {
588 tvc->vstates &= ~VRevokeWait;
589 afs_osi_Wakeup((char *)&tvc->vstates);
592 if (tvc->vstates & VPageCleaning) {
593 tvc->vstates &= ~VPageCleaning;
594 afs_osi_Wakeup((char *)&tvc->vstates);
597 MReleaseWriteLock(&tvc->vlock);
599 #endif /* AFS_SUN5_ENV */
601 MReleaseWriteLock(&afs_xdcache);
605 MObtainWriteLock(&afs_xdcache, 528);
606 if (afs_indexFlags[tdc->index] &
607 (IFDataMod | IFDirtyPages | IFAnyPages)) skip = 1;
608 if (tdc->refCount > 1) skip = 1;
610 #if defined(AFS_SUN5_ENV)
612 /* no vnode, so IFDirtyPages is spurious (we don't
613 * sweep dcaches on vnode recycling, so we can have
614 * DIRTYPAGES set even when all pages are gone). Just
616 * Hold vcache lock to prevent vnode from being
617 * created while we're clearing IFDirtyPages.
619 afs_indexFlags[tdc->index] &= ~(IFDirtyPages | IFAnyPages);
623 /* skip this guy and mark him as recently used */
624 afs_indexFlags[tdc->index] |= IFFlag;
625 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
626 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, 2,
627 ICL_TYPE_INT32, tdc->index,
629 ICL_HANDLE_OFFSET(tchunkoffset));
632 /* flush this dude from the data cache and reclaim;
633 * first, make sure no one will care that we damage
634 * it, by removing it from all hash tables. Then,
635 * melt it down for parts. Note that any concurrent
636 * (new possibility!) calls to GetDownD won't touch
637 * this guy because his reference count is > 0. */
638 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
639 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, 3,
640 ICL_TYPE_INT32, tdc->index,
642 ICL_HANDLE_OFFSET(tchunkoffset));
644 AFS_STATCNT(afs_gget);
646 afs_HashOutDCache(tdc);
647 if (tdc->f.chunkBytes != 0) {
650 *aneedSpace -= (tdc->f.chunkBytes + afs_fsfragsize) >> 10;
655 afs_DiscardDCache(tdc);
660 j = 1; /* we reclaimed at least one victim */
667 /* Phase is 0 and no one was found, so try phase 1 (ignore
668 * osi_Active flag) */
671 for (i = 0; i < afs_cacheFiles; i++)
672 /* turn off all flags */
673 afs_indexFlags[i] &= ~IFFlag;
677 /* found no one in phase 1, we're hosed */
678 if (victimPtr == 0) break;
680 } /* big while loop */
687 * Description: remove adc from any hash tables that would allow it to be located
688 * again by afs_FindDCache or afs_GetDCache.
690 * Parameters: adc -- pointer to dcache entry to remove from hash tables.
692 * Locks: Must have the afs_xdcache lock write-locked to call this function.
694 afs_HashOutDCache(adc)
697 { /*afs_HashOutDCache*/
702 AFS_STATCNT(afs_glink);
704 /* we know this guy's in the LRUQ. We'll move dude into DCQ below */
706 /* if this guy is in the hash table, pull him out */
707 if (adc->f.fid.Fid.Volume != 0) {
708 /* remove entry from first hash chains */
709 i = DCHash(&adc->f.fid, adc->f.chunk);
710 us = afs_dchashTbl[i];
711 if (us == adc->index) {
712 /* first dude in the list */
713 afs_dchashTbl[i] = afs_dcnextTbl[adc->index];
716 /* somewhere on the chain */
717 while (us != NULLIDX) {
718 if (afs_dcnextTbl[us] == adc->index) {
719 /* found item pointing at the one to delete */
720 afs_dcnextTbl[us] = afs_dcnextTbl[adc->index];
723 us = afs_dcnextTbl[us];
725 if (us == NULLIDX) osi_Panic("dcache hc");
727 /* remove entry from *other* hash chain */
728 i = DVHash(&adc->f.fid);
729 us = afs_dvhashTbl[i];
730 if (us == adc->index) {
731 /* first dude in the list */
732 afs_dvhashTbl[i] = afs_dvnextTbl[adc->index];
735 /* somewhere on the chain */
736 while (us != NULLIDX) {
737 if (afs_dvnextTbl[us] == adc->index) {
738 /* found item pointing at the one to delete */
739 afs_dvnextTbl[us] = afs_dvnextTbl[adc->index];
742 us = afs_dvnextTbl[us];
744 if (us == NULLIDX) osi_Panic("dcache hv");
748 /* prevent entry from being found on a reboot (it is already out of
749 * the hash table, but after a crash, we just look at fid fields of
750 * stable (old) entries).
752 adc->f.fid.Fid.Volume = 0; /* invalid */
754 /* mark entry as modified */
755 adc->dflags |= DFEntryMod;
759 } /*afs_HashOutDCache */
766 * Flush the given dcache entry, pulling it from hash chains
767 * and truncating the associated cache file.
770 * adc: Ptr to dcache entry to flush.
773 * This routine must be called with the afs_xdcache lock held
779 register struct dcache *adc;
780 { /*afs_FlushDCache*/
782 AFS_STATCNT(afs_FlushDCache);
784 * Bump the number of cache files flushed.
786 afs_stats_cmperf.cacheFlushes++;
788 /* remove from all hash tables */
789 afs_HashOutDCache(adc);
791 /* Free its space; special case null operation, since truncate operation
792 * in UFS is slow even in this case, and this allows us to pre-truncate
793 * these files at more convenient times with fewer locks set
794 * (see afs_GetDownD).
796 if (adc->f.chunkBytes != 0) {
797 afs_DiscardDCache(adc);
798 afs_MaybeWakeupTruncateDaemon();
803 if (afs_WaitForCacheDrain) {
804 if (afs_blocksUsed <=
805 (CM_CACHESIZEDRAINEDPCT*afs_cacheBlocks)/100) {
806 afs_WaitForCacheDrain = 0;
807 afs_osi_Wakeup(&afs_WaitForCacheDrain);
810 } /*afs_FlushDCache*/
816 * Description: put a dcache entry on the free dcache entry list.
818 * Parameters: adc -- dcache entry to free
820 * Environment: called with afs_xdcache lock write-locked.
822 static void afs_FreeDCache(adc)
823 register struct dcache *adc;
825 /* Thread on free list, update free list count and mark entry as
826 * freed in its indexFlags element. Also, ensure DCache entry gets
827 * written out (set DFEntryMod).
830 afs_dvnextTbl[adc->index] = afs_freeDCList;
831 afs_freeDCList = adc->index;
833 afs_indexFlags[adc->index] |= IFFree;
834 adc->dflags |= DFEntryMod;
836 if (afs_WaitForCacheDrain) {
837 if ((afs_blocksUsed - afs_blocksDiscarded) <=
838 (CM_CACHESIZEDRAINEDPCT*afs_cacheBlocks)/100) {
839 afs_WaitForCacheDrain = 0;
840 afs_osi_Wakeup(&afs_WaitForCacheDrain);
849 * Discard the cache element by moving it to the discardDCList.
850 * This puts the cache element into a quasi-freed state, where
851 * the space may be reused, but the file has not been truncated.
853 * Major Assumptions Here:
854 * Assumes that frag size is an integral power of two, less one,
855 * and that this is a two's complement machine. I don't
856 * know of any filesystems which violate this assumption...
859 * adc : Ptr to dcache entry.
862 * Must be called with afs_xdcache write-locked.
866 afs_DiscardDCache(adc)
867 register struct dcache *adc;
869 { /*afs_DiscardDCache*/
871 register afs_int32 size;
873 AFS_STATCNT(afs_DiscardDCache);
875 osi_Assert(adc->refCount == 1);
877 size = ((adc->f.chunkBytes + afs_fsfragsize)^afs_fsfragsize)>>10;/* round up */
878 afs_blocksDiscarded += size;
879 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
881 afs_dvnextTbl[adc->index] = afs_discardDCList;
882 afs_discardDCList = adc->index;
883 afs_discardDCCount++;
885 adc->f.fid.Fid.Volume = 0;
886 adc->dflags |= DFEntryMod;
887 afs_indexFlags[adc->index] |= IFDiscarded;
889 if (afs_WaitForCacheDrain) {
890 if ((afs_blocksUsed - afs_blocksDiscarded) <=
891 (CM_CACHESIZEDRAINEDPCT*afs_cacheBlocks)/100) {
892 afs_WaitForCacheDrain = 0;
893 afs_osi_Wakeup(&afs_WaitForCacheDrain);
897 } /*afs_DiscardDCache*/
900 * afs_FreeDiscardedDCache
903 * Free the next element on the list of discarded cache elements.
906 afs_FreeDiscardedDCache()
908 register struct dcache *tdc;
909 register struct osi_file *tfile;
910 register afs_int32 size;
912 AFS_STATCNT(afs_FreeDiscardedDCache);
914 MObtainWriteLock(&afs_xdcache,510);
915 if (!afs_blocksDiscarded) {
916 MReleaseWriteLock(&afs_xdcache);
921 * Get an entry from the list of discarded cache elements
923 tdc = afs_GetDSlot(afs_discardDCList, 0);
924 osi_Assert(tdc->refCount == 1);
925 ReleaseReadLock(&tdc->tlock);
927 afs_discardDCList = afs_dvnextTbl[tdc->index];
928 afs_dvnextTbl[tdc->index] = NULLIDX;
929 afs_discardDCCount--;
930 size = ((tdc->f.chunkBytes + afs_fsfragsize)^afs_fsfragsize)>>10;/* round up */
931 afs_blocksDiscarded -= size;
932 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
933 /* We can lock because we just took it off the free list */
934 ObtainWriteLock(&tdc->lock, 626);
935 MReleaseWriteLock(&afs_xdcache);
938 * Truncate the element to reclaim its space
940 tfile = afs_CFileOpen(tdc->f.inode);
941 afs_CFileTruncate(tfile, 0);
942 afs_CFileClose(tfile);
943 afs_AdjustSize(tdc, 0);
946 * Free the element we just truncated
948 MObtainWriteLock(&afs_xdcache,511);
949 afs_indexFlags[tdc->index] &= ~IFDiscarded;
951 ReleaseWriteLock(&tdc->lock);
953 MReleaseWriteLock(&afs_xdcache);
957 * afs_MaybeFreeDiscardedDCache
960 * Free as many entries from the list of discarded cache elements
961 * as we need to get the free space down below CM_WAITFORDRAINPCT (98%).
966 afs_MaybeFreeDiscardedDCache()
969 AFS_STATCNT(afs_MaybeFreeDiscardedDCache);
971 while (afs_blocksDiscarded &&
972 (afs_blocksUsed > (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100)) {
973 afs_FreeDiscardedDCache();
982 * Try to free up a certain number of disk slots.
985 * anumber : Targeted number of disk slots to free up.
988 * Must be called with afs_xdcache write-locked.
990 #if defined(AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
991 extern SV_TYPE afs_sgibksync;
992 extern SV_TYPE afs_sgibkwait;
993 extern lock_t afs_sgibklock;
994 extern struct dcache *afs_sgibklist;
998 afs_GetDownDSlot(anumber)
1001 { /*afs_GetDownDSlot*/
1003 struct afs_q *tq, *nq;
1009 AFS_STATCNT(afs_GetDownDSlot);
1010 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
1011 osi_Panic("diskless getdowndslot");
1013 if (CheckLock(&afs_xdcache) != -1)
1014 osi_Panic("getdowndslot nolock");
1016 /* decrement anumber first for all dudes in free list */
1017 for(tdc = afs_freeDSList; tdc; tdc = (struct dcache *)tdc->lruq.next)
1020 return; /* enough already free */
1022 for(cnt=0, tq = afs_DLRU.prev; tq != &afs_DLRU && anumber > 0;
1024 tdc = (struct dcache *) tq; /* q is first elt in dcache entry */
1025 nq = QPrev(tq); /* in case we remove it */
1026 if (tdc->refCount == 0) {
1027 if ((ix=tdc->index) == NULLIDX) osi_Panic("getdowndslot");
1028 /* pull the entry out of the lruq and put it on the free list */
1029 QRemove(&tdc->lruq);
1031 /* write-through if modified */
1032 if (tdc->dflags & DFEntryMod) {
1033 #if defined(AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
1035 * ask proxy to do this for us - we don't have the stack space
1037 while (tdc->dflags & DFEntryMod) {
1040 s = SPLOCK(afs_sgibklock);
1041 if (afs_sgibklist == NULL) {
1042 /* if slot is free, grab it. */
1043 afs_sgibklist = tdc;
1044 SV_SIGNAL(&afs_sgibksync);
1046 /* wait for daemon to (start, then) finish. */
1047 SP_WAIT(afs_sgibklock, s, &afs_sgibkwait, PINOD);
1051 tdc->dflags &= ~DFEntryMod;
1052 afs_WriteDCache(tdc, 1);
1059 struct osi_file * f = (struct osi_file *)tdc->ihint;
1067 /* finally put the entry in the free list */
1068 afs_indexTable[ix] = (struct dcache *) 0;
1069 afs_indexFlags[ix] &= ~IFEverUsed;
1070 tdc->index = NULLIDX;
1071 tdc->lruq.next = (struct afs_q *) afs_freeDSList;
1072 afs_freeDSList = tdc;
1076 } /*afs_GetDownDSlot*/
1083 * Increment the reference count on a disk cache entry,
1084 * which already has a non-zero refcount. In order to
1085 * increment the refcount of a zero-reference entry, you
1086 * have to hold afs_xdcache.
1089 * adc : Pointer to the dcache entry to increment.
1092 * Nothing interesting.
1097 ObtainWriteLock(&adc->tlock, 627);
1098 if (adc->refCount < 0)
1099 osi_Panic("RefDCache: negative refcount");
1101 ReleaseWriteLock(&adc->tlock);
1110 * Decrement the reference count on a disk cache entry.
1113 * ad : Ptr to the dcache entry to decrement.
1116 * Nothing interesting.
1119 register struct dcache *adc;
1122 AFS_STATCNT(afs_PutDCache);
1123 ObtainWriteLock(&adc->tlock, 276);
1124 if (adc->refCount <= 0)
1125 osi_Panic("putdcache");
1127 ReleaseWriteLock(&adc->tlock);
1137 * Try to discard all data associated with this file from the
1141 * avc : Pointer to the cache info for the file.
1144 * Both pvnLock and lock are write held.
1147 afs_TryToSmush(avc, acred, sync)
1148 register struct vcache *avc;
1149 struct AFS_UCRED *acred;
1151 { /*afs_TryToSmush*/
1153 register struct dcache *tdc;
1156 AFS_STATCNT(afs_TryToSmush);
1157 afs_Trace2(afs_iclSetp, CM_TRACE_TRYTOSMUSH, ICL_TYPE_POINTER, avc,
1158 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
1159 sync = 1; /* XX Temp testing XX*/
1161 #if defined(AFS_SUN5_ENV)
1162 ObtainWriteLock(&avc->vlock, 573);
1163 avc->activeV++; /* block new getpages */
1164 ReleaseWriteLock(&avc->vlock);
1167 /* Flush VM pages */
1168 osi_VM_TryToSmush(avc, acred, sync);
1171 * Get the hash chain containing all dce's for this fid
1173 i = DVHash(&avc->fid);
1174 MObtainWriteLock(&afs_xdcache,277);
1175 for(index = afs_dvhashTbl[i]; index != NULLIDX; index=i) {
1176 i = afs_dvnextTbl[index]; /* next pointer this hash table */
1177 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1178 int releaseTlock = 1;
1179 tdc = afs_GetDSlot(index, (struct dcache *)0);
1180 if (!FidCmp(&tdc->f.fid, &avc->fid)) {
1182 if ((afs_indexFlags[index] & IFDataMod) == 0 &&
1183 tdc->refCount == 1) {
1184 ReleaseReadLock(&tdc->tlock);
1186 afs_FlushDCache(tdc);
1189 afs_indexTable[index] = 0;
1191 if (releaseTlock) ReleaseReadLock(&tdc->tlock);
1195 #if defined(AFS_SUN5_ENV)
1196 ObtainWriteLock(&avc->vlock, 545);
1197 if (--avc->activeV == 0 && (avc->vstates & VRevokeWait)) {
1198 avc->vstates &= ~VRevokeWait;
1199 afs_osi_Wakeup((char *)&avc->vstates);
1201 ReleaseWriteLock(&avc->vlock);
1203 MReleaseWriteLock(&afs_xdcache);
1205 * It's treated like a callback so that when we do lookups we'll invalidate the unique bit if any
1206 * trytoSmush occured during the lookup call
1209 } /*afs_TryToSmush*/
1215 * Given the cached info for a file and a byte offset into the
1216 * file, make sure the dcache entry for that file and containing
1217 * the given byte is available, returning it to our caller.
1220 * avc : Pointer to the (held) vcache entry to look in.
1221 * abyte : Which byte we want to get to.
1224 * Pointer to the dcache entry covering the file & desired byte,
1225 * or NULL if not found.
1228 * The vcache entry is held upon entry.
1231 struct dcache *afs_FindDCache(avc, abyte)
1232 register struct vcache *avc;
1235 { /*afs_FindDCache*/
1238 register afs_int32 i, index;
1239 register struct dcache *tdc;
1241 AFS_STATCNT(afs_FindDCache);
1242 chunk = AFS_CHUNK(abyte);
1245 * Hash on the [fid, chunk] and get the corresponding dcache index
1246 * after write-locking the dcache.
1248 i = DCHash(&avc->fid, chunk);
1249 MObtainWriteLock(&afs_xdcache,278);
1250 for(index = afs_dchashTbl[i]; index != NULLIDX;) {
1251 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1252 tdc = afs_GetDSlot(index, (struct dcache *)0);
1253 ReleaseReadLock(&tdc->tlock);
1254 if (!FidCmp(&tdc->f.fid, &avc->fid) && chunk == tdc->f.chunk) {
1255 break; /* leaving refCount high for caller */
1259 index = afs_dcnextTbl[index];
1261 MReleaseWriteLock(&afs_xdcache);
1262 if (index != NULLIDX) {
1263 hset(afs_indexTimes[tdc->index], afs_indexCounter);
1264 hadd32(afs_indexCounter, 1);
1268 return(struct dcache *) 0;
1270 } /*afs_FindDCache*/
1274 * afs_UFSCacheStoreProc
1277 * Called upon store.
1280 * acall : Ptr to the Rx call structure involved.
1281 * afile : Ptr to the related file descriptor.
1282 * alen : Size of the file in bytes.
1283 * avc : Ptr to the vcache entry.
1284 * shouldWake : is it "safe" to return early from close() ?
1285 * abytesToXferP : Set to the number of bytes to xfer.
1286 * NOTE: This parameter is only used if AFS_NOSTATS
1288 * abytesXferredP : Set to the number of bytes actually xferred.
1289 * NOTE: This parameter is only used if AFS_NOSTATS
1293 * Nothing interesting.
1295 static int afs_UFSCacheStoreProc(acall, afile, alen, avc, shouldWake,
1296 abytesToXferP, abytesXferredP)
1297 register struct rx_call *acall;
1298 struct osi_file *afile;
1299 register afs_int32 alen;
1300 afs_size_t *abytesToXferP;
1301 afs_size_t *abytesXferredP;
1304 { /* afs_UFSCacheStoreProc*/
1306 afs_int32 code, got;
1307 register char *tbuffer;
1310 AFS_STATCNT(UFS_CacheStoreProc);
1314 * In this case, alen is *always* the amount of data we'll be trying
1317 (*abytesToXferP) = alen;
1318 (*abytesXferredP) = 0;
1319 #endif /* AFS_NOSTATS */
1321 afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
1322 ICL_TYPE_FID, &(avc->fid),
1323 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1324 ICL_TYPE_INT32, alen);
1325 tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1327 tlen = (alen > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : alen);
1328 got = afs_osi_Read(afile, -1, tbuffer, tlen);
1330 #if !defined(AFS_SUN5_ENV) && !defined(AFS_OSF_ENV) && !defined(AFS_SGI64_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
1331 || (got != tlen && getuerror())
1334 osi_FreeLargeSpace(tbuffer);
1337 afs_Trace1(afs_iclSetp, CM_TRACE_STOREPROC2, ICL_TYPE_INT32, got);
1339 code = rx_Write(acall, tbuffer, got); /* writing 0 bytes will
1340 * push a short packet. Is that really what we want, just because the
1341 * data didn't come back from the disk yet? Let's try it and see. */
1344 (*abytesXferredP) += code;
1345 #endif /* AFS_NOSTATS */
1347 osi_FreeLargeSpace(tbuffer);
1352 * If file has been locked on server, we can allow the store
1355 if (shouldWake && *shouldWake && (rx_GetRemoteStatus(acall) & 1)) {
1356 *shouldWake = 0; /* only do this once */
1360 afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
1361 ICL_TYPE_FID, &(avc->fid),
1362 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1363 ICL_TYPE_INT32, alen);
1364 osi_FreeLargeSpace(tbuffer);
1367 } /* afs_UFSCacheStoreProc*/
1371 * afs_UFSCacheFetchProc
1374 * Routine called on fetch; also tells people waiting for data
1375 * that more has arrived.
1378 * acall : Ptr to the Rx call structure.
1379 * afile : File descriptor for the cache file.
1380 * abase : Base offset to fetch.
1381 * adc : Ptr to the dcache entry for the file, write-locked.
1382 * avc : Ptr to the vcache entry for the file.
1383 * abytesToXferP : Set to the number of bytes to xfer.
1384 * NOTE: This parameter is only used if AFS_NOSTATS
1386 * abytesXferredP : Set to the number of bytes actually xferred.
1387 * NOTE: This parameter is only used if AFS_NOSTATS
1391 * Nothing interesting.
1394 static int afs_UFSCacheFetchProc(acall, afile, abase, adc, avc,
1395 abytesToXferP, abytesXferredP, lengthFound)
1396 register struct rx_call *acall;
1398 afs_size_t *abytesToXferP;
1399 afs_size_t *abytesXferredP;
1402 struct osi_file *afile;
1403 afs_int32 lengthFound;
1404 { /*UFS_CacheFetchProc*/
1406 register afs_int32 code;
1407 register char *tbuffer;
1411 AFS_STATCNT(UFS_CacheFetchProc);
1412 osi_Assert(WriteLocked(&adc->lock));
1413 afile->offset = 0; /* Each time start from the beginning */
1414 length = lengthFound;
1416 (*abytesToXferP) = 0;
1417 (*abytesXferredP) = 0;
1418 #endif /* AFS_NOSTATS */
1419 tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1420 adc->validPos = abase;
1424 code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
1426 length = ntohl(length);
1427 if (code != sizeof(afs_int32)) {
1428 osi_FreeLargeSpace(tbuffer);
1429 code = rx_Error(acall);
1430 return (code?code:-1); /* try to return code, not -1 */
1434 * The fetch protocol is extended for the AFS/DFS translator
1435 * to allow multiple blocks of data, each with its own length,
1436 * to be returned. As long as the top bit is set, there are more
1439 * We do not do this for AFS file servers because they sometimes
1440 * return large negative numbers as the transfer size.
1442 if (avc->states & CForeign) {
1443 moredata = length & 0x80000000;
1444 length &= ~0x80000000;
1449 (*abytesToXferP) += length;
1450 #endif /* AFS_NOSTATS */
1451 while (length > 0) {
1452 tlen = (length > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : length);
1454 code = rx_Read(acall, tbuffer, tlen);
1457 (*abytesXferredP) += code;
1458 #endif /* AFS_NOSTATS */
1460 osi_FreeLargeSpace(tbuffer);
1461 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
1462 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
1463 ICL_TYPE_INT32, length);
1466 code = afs_osi_Write(afile, -1, tbuffer, tlen);
1468 osi_FreeLargeSpace(tbuffer);
1473 adc->validPos = abase;
1474 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE,
1475 ICL_TYPE_STRING, __FILE__,
1476 ICL_TYPE_INT32, __LINE__,
1477 ICL_TYPE_POINTER, adc,
1478 ICL_TYPE_INT32, adc->dflags);
1479 afs_osi_Wakeup(&adc->validPos);
1482 osi_FreeLargeSpace(tbuffer);
1485 } /* afs_UFSCacheFetchProc*/
1491 * This function is called to obtain a reference to data stored in
1492 * the disk cache, locating a chunk of data containing the desired
1493 * byte and returning a reference to the disk cache entry, with its
1494 * reference count incremented.
1498 * avc : Ptr to a vcache entry (unlocked)
1499 * abyte : Byte position in the file desired
1500 * areq : Request structure identifying the requesting user.
1501 * aflags : Settings as follows:
1503 * 2 : Return after creating entry.
1504 * 4 : called from afs_vnop_write.c
1505 * *alen contains length of data to be written.
1507 * aoffset : Set to the offset within the chunk where the resident
1509 * alen : Set to the number of bytes of data after the desired
1510 * byte (including the byte itself) which can be read
1514 * The vcache entry pointed to by avc is unlocked upon entry.
1518 struct AFSVolSync tsync;
1519 struct AFSFetchStatus OutStatus;
1520 struct AFSCallBack CallBack;
1524 * Update the vnode-to-dcache hint if we can get the vnode lock
1525 * right away. Assumes dcache entry is at least read-locked.
1527 void updateV2DC(int lockVc, struct vcache *v, struct dcache *d, int src) {
1528 if (!lockVc || 0 == NBObtainWriteLock(&v->lock,src)) {
1529 if (hsame(v->m.DataVersion, d->f.versionNo) && v->callback) {
1531 v->quick.stamp = d->stamp = MakeStamp();
1532 v->quick.minLoc = AFS_CHUNKTOBASE(d->f.chunk);
1533 /* Don't think I need these next two lines forever */
1534 v->quick.len = d->f.chunkBytes;
1537 if (lockVc) ReleaseWriteLock(&v->lock);
1541 struct dcache *afs_GetDCache(avc, abyte, areq, aoffset, alen, aflags)
1542 register struct vcache *avc; /* Write-locked unless aflags & 1 */
1544 afs_size_t *aoffset, *alen;
1546 register struct vrequest *areq;
1550 register afs_int32 i, code, code1, shortcut , adjustsize=0;
1555 afs_size_t maxGoodLength; /* amount of good data at server */
1556 struct rx_call *tcall;
1557 afs_size_t Position = 0;
1558 #ifdef AFS_64BIT_CLIENT
1560 #endif /* AFS_64BIT_CLIENT */
1561 afs_int32 size, tlen; /* size of segment to transfer */
1562 afs_size_t lengthFound; /* as returned from server */
1563 struct tlocal1 *tsmall = 0;
1564 register struct dcache *tdc;
1565 register struct osi_file *file;
1566 register struct conn *tc;
1568 struct server *newCallback;
1569 char setNewCallback;
1570 char setVcacheStatus;
1571 char doVcacheUpdate;
1573 int doAdjustSize = 0;
1574 int doReallyAdjustSize = 0;
1575 int overWriteWholeChunk = 0;
1579 struct afs_stats_xferData *xferP; /* Ptr to this op's xfer struct */
1580 osi_timeval_t xferStartTime, /*FS xfer start time*/
1581 xferStopTime; /*FS xfer stop time*/
1582 afs_size_t bytesToXfer; /* # bytes to xfer*/
1583 afs_size_t bytesXferred; /* # bytes actually xferred*/
1584 struct afs_stats_AccessInfo *accP; /*Ptr to access record in stats*/
1585 int fromReplica; /*Are we reading from a replica?*/
1586 int numFetchLoops; /*# times around the fetch/analyze loop*/
1587 #endif /* AFS_NOSTATS */
1589 AFS_STATCNT(afs_GetDCache);
1594 setLocks = aflags & 1;
1597 * Determine the chunk number and offset within the chunk corresponding
1598 * to the desired byte.
1600 if (avc->fid.Fid.Vnode & 1) { /* if (vType(avc) == VDIR) */
1604 chunk = AFS_CHUNK(abyte);
1607 /* come back to here if we waited for the cache to drain. */
1610 setNewCallback = setVcacheStatus = 0;
1614 ObtainWriteLock(&avc->lock, 616);
1616 ObtainReadLock(&avc->lock);
1621 * avc->lock(R) if setLocks && !slowPass
1622 * avc->lock(W) if !setLocks || slowPass
1627 /* check hints first! (might could use bcmp or some such...) */
1628 if (tdc = avc->h1.dchint) {
1632 * The locking order between afs_xdcache and dcache lock matters.
1633 * The hint dcache entry could be anywhere, even on the free list.
1634 * Locking afs_xdcache ensures that noone is trying to pull dcache
1635 * entries from the free list, and thereby assuming them to be not
1636 * referenced and not locked.
1638 MObtainReadLock(&afs_xdcache);
1639 dcLocked = (0 == NBObtainSharedLock(&tdc->lock, 601));
1642 (tdc->index != NULLIDX) && !FidCmp(&tdc->f.fid, &avc->fid) &&
1643 chunk == tdc->f.chunk &&
1644 !(afs_indexFlags[tdc->index] & (IFFree|IFDiscarded))) {
1645 /* got the right one. It might not be the right version, and it
1646 * might be fetching, but it's the right dcache entry.
1648 /* All this code should be integrated better with what follows:
1649 * I can save a good bit more time under a write lock if I do..
1651 ObtainWriteLock(&tdc->tlock, 603);
1653 ReleaseWriteLock(&tdc->tlock);
1655 MReleaseReadLock(&afs_xdcache);
1658 if (hsame(tdc->f.versionNo, avc->m.DataVersion) &&
1659 !(tdc->dflags & DFFetching)) {
1661 afs_stats_cmperf.dcacheHits++;
1662 MObtainWriteLock(&afs_xdcache, 559);
1663 QRemove(&tdc->lruq);
1664 QAdd(&afs_DLRU, &tdc->lruq);
1665 MReleaseWriteLock(&afs_xdcache);
1668 * avc->lock(R) if setLocks && !slowPass
1669 * avc->lock(W) if !setLocks || slowPass
1675 if (dcLocked) ReleaseSharedLock(&tdc->lock);
1676 MReleaseReadLock(&afs_xdcache);
1684 * avc->lock(R) if setLocks && !slowPass
1685 * avc->lock(W) if !setLocks || slowPass
1686 * tdc->lock(S) if tdc
1689 if (!tdc) { /* If the hint wasn't the right dcache entry */
1691 * Hash on the [fid, chunk] and get the corresponding dcache index
1692 * after write-locking the dcache.
1697 * avc->lock(R) if setLocks && !slowPass
1698 * avc->lock(W) if !setLocks || slowPass
1701 i = DCHash(&avc->fid, chunk);
1702 /* check to make sure our space is fine */
1703 afs_MaybeWakeupTruncateDaemon();
1705 MObtainWriteLock(&afs_xdcache,280);
1707 for (index = afs_dchashTbl[i]; index != NULLIDX; ) {
1708 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1709 tdc = afs_GetDSlot(index, (struct dcache *)0);
1710 ReleaseReadLock(&tdc->tlock);
1713 * avc->lock(R) if setLocks && !slowPass
1714 * avc->lock(W) if !setLocks || slowPass
1717 if (!FidCmp(&tdc->f.fid, &avc->fid) && chunk == tdc->f.chunk) {
1718 /* Move it up in the beginning of the list */
1719 if (afs_dchashTbl[i] != index) {
1720 afs_dcnextTbl[us] = afs_dcnextTbl[index];
1721 afs_dcnextTbl[index] = afs_dchashTbl[i];
1722 afs_dchashTbl[i] = index;
1724 MReleaseWriteLock(&afs_xdcache);
1725 ObtainSharedLock(&tdc->lock, 606);
1726 break; /* leaving refCount high for caller */
1732 index = afs_dcnextTbl[index];
1736 * If we didn't find the entry, we'll create one.
1738 if (index == NULLIDX) {
1741 * avc->lock(R) if setLocks
1742 * avc->lock(W) if !setLocks
1745 afs_Trace2(afs_iclSetp, CM_TRACE_GETDCACHE1, ICL_TYPE_POINTER,
1746 avc, ICL_TYPE_INT32, chunk);
1748 /* Make sure there is a free dcache entry for us to use */
1749 if (afs_discardDCList == NULLIDX && afs_freeDCList == NULLIDX) {
1751 if (!setLocks) avc->states |= CDCLock;
1752 afs_GetDownD(5, (int*)0); /* just need slots */
1753 if (!setLocks) avc->states &= ~CDCLock;
1754 if (afs_discardDCList != NULLIDX || afs_freeDCList != NULLIDX)
1756 /* If we can't get space for 5 mins we give up and panic */
1757 if (++downDCount > 300)
1758 osi_Panic("getdcache");
1759 MReleaseWriteLock(&afs_xdcache);
1762 * avc->lock(R) if setLocks
1763 * avc->lock(W) if !setLocks
1765 afs_osi_Wait(1000, 0, 0);
1770 if (afs_discardDCList == NULLIDX ||
1771 ((aflags & 2) && afs_freeDCList != NULLIDX)) {
1773 afs_indexFlags[afs_freeDCList] &= ~IFFree;
1774 tdc = afs_GetDSlot(afs_freeDCList, 0);
1775 osi_Assert(tdc->refCount == 1);
1776 ReleaseReadLock(&tdc->tlock);
1777 ObtainWriteLock(&tdc->lock, 604);
1778 afs_freeDCList = afs_dvnextTbl[tdc->index];
1781 afs_indexFlags[afs_discardDCList] &= ~IFDiscarded;
1782 tdc = afs_GetDSlot(afs_discardDCList, 0);
1783 osi_Assert(tdc->refCount == 1);
1784 ReleaseReadLock(&tdc->tlock);
1785 ObtainWriteLock(&tdc->lock, 605);
1786 afs_discardDCList = afs_dvnextTbl[tdc->index];
1787 afs_discardDCCount--;
1788 size = ((tdc->f.chunkBytes + afs_fsfragsize)^afs_fsfragsize)>>10;
1789 afs_blocksDiscarded -= size;
1790 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1792 /* Truncate the chunk so zeroes get filled properly */
1793 file = afs_CFileOpen(tdc->f.inode);
1794 afs_CFileTruncate(file, 0);
1795 afs_CFileClose(file);
1796 afs_AdjustSize(tdc, 0);
1802 * avc->lock(R) if setLocks
1803 * avc->lock(W) if !setLocks
1809 * Fill in the newly-allocated dcache record.
1811 afs_indexFlags[tdc->index] &= ~(IFDirtyPages | IFAnyPages);
1812 tdc->f.fid = avc->fid;
1813 afs_indexUnique[tdc->index] = tdc->f.fid.Fid.Unique;
1814 hones(tdc->f.versionNo); /* invalid value */
1815 tdc->f.chunk = chunk;
1816 tdc->validPos = AFS_CHUNKTOBASE(chunk);
1818 if (tdc->lruq.prev == &tdc->lruq) osi_Panic("lruq 1");
1821 * Now add to the two hash chains - note that i is still set
1822 * from the above DCHash call.
1824 afs_dcnextTbl[tdc->index] = afs_dchashTbl[i];
1825 afs_dchashTbl[i] = tdc->index;
1826 i = DVHash(&avc->fid);
1827 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[i];
1828 afs_dvhashTbl[i] = tdc->index;
1829 tdc->dflags = DFEntryMod;
1832 afs_MaybeWakeupTruncateDaemon();
1833 MReleaseWriteLock(&afs_xdcache);
1834 ConvertWToSLock(&tdc->lock);
1836 } /* vcache->dcache hint failed */
1840 * avc->lock(R) if setLocks && !slowPass
1841 * avc->lock(W) if !setLocks || slowPass
1845 afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE2, ICL_TYPE_POINTER, avc,
1846 ICL_TYPE_POINTER, tdc,
1847 ICL_TYPE_INT32, hgetlo(tdc->f.versionNo),
1848 ICL_TYPE_INT32, hgetlo(avc->m.DataVersion));
1850 * Here we have the entry in tdc, with its refCount incremented.
1851 * Note: we don't use the S-lock on avc; it costs concurrency when
1852 * storing a file back to the server.
1856 * Not a newly created file so we need to check the file's length and
1857 * compare data versions since someone could have changed the data or we're
1858 * reading a file written elsewhere. We only want to bypass doing no-op
1859 * read rpcs on newly created files (dv of 0) since only then we guarantee
1860 * that this chunk's data hasn't been filled by another client.
1862 size = AFS_CHUNKSIZE(abyte);
1863 if (aflags & 4) /* called from write */
1865 else /* called from read */
1866 tlen = tdc->validPos - abyte;
1867 Position = AFS_CHUNKTOBASE(chunk);
1868 afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE3,
1869 ICL_TYPE_INT32, tlen,
1870 ICL_TYPE_INT32, aflags,
1871 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(abyte),
1872 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(Position));
1873 if ((aflags & 4) && (hiszero(avc->m.DataVersion)))
1875 if ((aflags & 4) && (abyte == Position) && (tlen >= size))
1876 overWriteWholeChunk = 1;
1877 if (doAdjustSize || overWriteWholeChunk) {
1878 #if defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
1880 #ifdef AFS_SGI64_ENV
1881 if (doAdjustSize) adjustsize = NBPP;
1882 #else /* AFS_SGI64_ENV */
1883 if (doAdjustSize) adjustsize = 8192;
1884 #endif /* AFS_SGI64_ENV */
1885 #else /* AFS_SGI_ENV */
1886 if (doAdjustSize) adjustsize = 4096;
1887 #endif /* AFS_SGI_ENV */
1888 if (AFS_CHUNKTOBASE(chunk)+adjustsize >= avc->m.Length &&
1889 #else /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
1890 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV)
1891 if ((doAdjustSize || (AFS_CHUNKTOBASE(chunk) >= avc->m.Length)) &&
1893 if (AFS_CHUNKTOBASE(chunk) >= avc->m.Length &&
1895 #endif /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
1896 !hsame(avc->m.DataVersion, tdc->f.versionNo))
1897 doReallyAdjustSize = 1;
1899 if (doReallyAdjustSize || overWriteWholeChunk) {
1900 /* no data in file to read at this position */
1901 UpgradeSToWLock(&tdc->lock, 607);
1903 file = afs_CFileOpen(tdc->f.inode);
1904 afs_CFileTruncate(file, 0);
1905 afs_CFileClose(file);
1906 afs_AdjustSize(tdc, 0);
1907 hset(tdc->f.versionNo, avc->m.DataVersion);
1908 tdc->dflags |= DFEntryMod;
1910 ConvertWToSLock(&tdc->lock);
1915 * We must read in the whole chunk if the version number doesn't
1919 /* don't need data, just a unique dcache entry */
1920 ObtainWriteLock(&afs_xdcache, 608);
1921 hset(afs_indexTimes[tdc->index], afs_indexCounter);
1922 hadd32(afs_indexCounter, 1);
1923 ReleaseWriteLock(&afs_xdcache);
1925 updateV2DC(setLocks, avc, tdc, 553);
1926 if (vType(avc) == VDIR)
1929 *aoffset = AFS_CHUNKOFFSET(abyte);
1930 if (tdc->validPos < abyte)
1931 *alen = (afs_size_t) 0;
1933 *alen = tdc->validPos - abyte;
1934 ReleaseSharedLock(&tdc->lock);
1937 ReleaseWriteLock(&avc->lock);
1939 ReleaseReadLock(&avc->lock);
1941 return tdc; /* check if we're done */
1946 * avc->lock(R) if setLocks && !slowPass
1947 * avc->lock(W) if !setLocks || slowPass
1950 osi_Assert((setLocks && !slowPass) || WriteLocked(&avc->lock));
1952 setNewCallback = setVcacheStatus = 0;
1956 * avc->lock(R) if setLocks && !slowPass
1957 * avc->lock(W) if !setLocks || slowPass
1960 if (!hsame(avc->m.DataVersion, tdc->f.versionNo) && !overWriteWholeChunk) {
1962 * Version number mismatch.
1964 UpgradeSToWLock(&tdc->lock, 609);
1967 * If data ever existed for this vnode, and this is a text object,
1968 * do some clearing. Now, you'd think you need only do the flush
1969 * when VTEXT is on, but VTEXT is turned off when the text object
1970 * is freed, while pages are left lying around in memory marked
1971 * with this vnode. If we would reactivate (create a new text
1972 * object from) this vnode, we could easily stumble upon some of
1973 * these old pages in pagein. So, we always flush these guys.
1974 * Sun has a wonderful lack of useful invariants in this system.
1976 * avc->flushDV is the data version # of the file at the last text
1977 * flush. Clearly, at least, we don't have to flush the file more
1978 * often than it changes
1980 if (hcmp(avc->flushDV, avc->m.DataVersion) < 0) {
1982 * By here, the cache entry is always write-locked. We can
1983 * deadlock if we call osi_Flush with the cache entry locked...
1984 * Unlock the dcache too.
1986 ReleaseWriteLock(&tdc->lock);
1987 if (setLocks && !slowPass)
1988 ReleaseReadLock(&avc->lock);
1990 ReleaseWriteLock(&avc->lock);
1994 * Call osi_FlushPages in open, read/write, and map, since it
1995 * is too hard here to figure out if we should lock the
1998 if (setLocks && !slowPass)
1999 ObtainReadLock(&avc->lock);
2001 ObtainWriteLock(&avc->lock, 66);
2002 ObtainWriteLock(&tdc->lock, 610);
2007 * avc->lock(R) if setLocks && !slowPass
2008 * avc->lock(W) if !setLocks || slowPass
2012 /* Watch for standard race condition around osi_FlushText */
2013 if (hsame(avc->m.DataVersion, tdc->f.versionNo)) {
2014 updateV2DC(setLocks, avc, tdc, 569); /* set hint */
2015 afs_stats_cmperf.dcacheHits++;
2016 ConvertWToSLock(&tdc->lock);
2020 /* Sleep here when cache needs to be drained. */
2021 if (setLocks && !slowPass &&
2022 (afs_blocksUsed > (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100)) {
2023 /* Make sure truncate daemon is running */
2024 afs_MaybeWakeupTruncateDaemon();
2025 ObtainWriteLock(&tdc->tlock, 614);
2026 tdc->refCount--; /* we'll re-obtain the dcache when we re-try. */
2027 ReleaseWriteLock(&tdc->tlock);
2028 ReleaseWriteLock(&tdc->lock);
2029 ReleaseReadLock(&avc->lock);
2030 while ((afs_blocksUsed-afs_blocksDiscarded) >
2031 (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
2032 afs_WaitForCacheDrain = 1;
2033 afs_osi_Sleep(&afs_WaitForCacheDrain);
2035 afs_MaybeFreeDiscardedDCache();
2036 /* need to check if someone else got the chunk first. */
2037 goto RetryGetDCache;
2040 /* Do not fetch data beyond truncPos. */
2041 maxGoodLength = avc->m.Length;
2042 if (avc->truncPos < maxGoodLength) maxGoodLength = avc->truncPos;
2043 Position = AFS_CHUNKBASE(abyte);
2044 if (vType(avc) == VDIR) {
2045 size = avc->m.Length;
2046 if (size > tdc->f.chunkBytes) {
2047 /* pre-reserve space for file */
2048 afs_AdjustSize(tdc, size);
2050 size = 999999999; /* max size for transfer */
2053 size = AFS_CHUNKSIZE(abyte); /* expected max size */
2054 /* don't read past end of good data on server */
2055 if (Position + size > maxGoodLength)
2056 size = maxGoodLength - Position;
2057 if (size < 0) size = 0; /* Handle random races */
2058 if (size > tdc->f.chunkBytes) {
2059 /* pre-reserve space for file */
2060 afs_AdjustSize(tdc, size); /* changes chunkBytes */
2061 /* max size for transfer still in size */
2064 if (afs_mariner && !tdc->f.chunk)
2065 afs_MarinerLog("fetch$Fetching", avc); /* , Position, size, afs_indexCounter );*/
2067 * Right now, we only have one tool, and it's a hammer. So, we
2068 * fetch the whole file.
2070 DZap(&tdc->f.inode); /* pages in cache may be old */
2072 if (file = tdc->ihint) {
2073 if (tdc->f.inode == file->inum )
2080 file = osi_UFSOpen(tdc->f.inode);
2085 file = afs_CFileOpen(tdc->f.inode);
2086 afs_RemoveVCB(&avc->fid);
2087 tdc->f.states |= DWriting;
2088 tdc->dflags |= DFFetching;
2089 tdc->validPos = Position; /* which is AFS_CHUNKBASE(abyte) */
2090 if (tdc->mflags & DFFetchReq) {
2091 tdc->mflags &= ~DFFetchReq;
2092 afs_osi_Wakeup(&tdc->validPos);
2094 tsmall = (struct tlocal1 *) osi_AllocLargeSpace(sizeof(struct tlocal1));
2095 setVcacheStatus = 0;
2098 * Remember if we are doing the reading from a replicated volume,
2099 * and how many times we've zipped around the fetch/analyze loop.
2101 fromReplica = (avc->states & CRO) ? 1 : 0;
2103 accP = &(afs_stats_cmfullperf.accessinf);
2105 (accP->replicatedRefs)++;
2107 (accP->unreplicatedRefs)++;
2108 #endif /* AFS_NOSTATS */
2109 /* this is a cache miss */
2110 afs_Trace4(afs_iclSetp, CM_TRACE_FETCHPROC, ICL_TYPE_POINTER, avc,
2111 ICL_TYPE_FID, &(avc->fid),
2112 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(Position),
2113 ICL_TYPE_INT32, size);
2115 if (size) afs_stats_cmperf.dcacheMisses++;
2118 * Dynamic root support: fetch data from local memory.
2120 if (afs_IsDynroot(avc)) {
2124 afs_GetDynroot(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2126 dynrootDir += Position;
2127 dynrootLen -= Position;
2128 if (size > dynrootLen)
2130 if (size < 0) size = 0;
2131 code = afs_CFileWrite(file, 0, dynrootDir, size);
2139 tdc->validPos = Position + size;
2140 afs_CFileTruncate(file, size); /* prune it */
2143 * Not a dynamic vnode: do the real fetch.
2148 * avc->lock(R) if setLocks && !slowPass
2149 * avc->lock(W) if !setLocks || slowPass
2153 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
2155 afs_int32 length_hi, length, bytes;
2159 (accP->numReplicasAccessed)++;
2161 #endif /* AFS_NOSTATS */
2162 if (!setLocks || slowPass) {
2163 avc->callback = tc->srvr->server;
2165 newCallback = tc->srvr->server;
2170 tcall = rx_NewCall(tc->id);
2173 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
2174 #ifdef AFS_64BIT_CLIENT
2175 length_hi = code = 0;
2176 if (!afs_serverHasNo64Bit(tc)) {
2179 code = StartRXAFS_FetchData64(tcall,
2180 (struct AFSFid *) &avc->fid.Fid,
2184 afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
2185 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
2187 bytes = rx_Read(tcall, (char *)&length_hi, sizeof(afs_int32));
2189 afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
2190 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
2191 if (bytes == sizeof(afs_int32)) {
2192 length_hi = ntohl(length_hi);
2195 code = rx_Error(tcall);
2197 code1 = rx_EndCall(tcall, code);
2199 tcall = (struct rx_call *) 0;
2203 if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
2204 if (Position > 0x7FFFFFFF) {
2211 tcall = rx_NewCall(tc->id);
2212 code = StartRXAFS_FetchData(tcall,
2213 (struct AFSFid *) &avc->fid.Fid, pos, size);
2216 afs_serverSetNo64Bit(tc);
2220 bytes = rx_Read(tcall, (char *)&length, sizeof(afs_int32));
2222 if (bytes == sizeof(afs_int32)) {
2223 length = ntohl(length);
2225 code = rx_Error(tcall);
2228 FillInt64(lengthFound, length_hi, length);
2229 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
2230 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
2231 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(lengthFound));
2232 #else /* AFS_64BIT_CLIENT */
2234 code = StartRXAFS_FetchData(tcall,
2235 (struct AFSFid *) &avc->fid.Fid,
2240 bytes = rx_Read(tcall, (char *)&length, sizeof(afs_int32));
2242 if (bytes == sizeof(afs_int32)) {
2243 length = ntohl(length);
2245 code = rx_Error(tcall);
2248 #endif /* AFS_64BIT_CLIENT */
2252 xferP = &(afs_stats_cmfullperf.rpc.fsXferTimes[AFS_STATS_FS_XFERIDX_FETCHDATA]);
2253 osi_GetuTime(&xferStartTime);
2255 code = afs_CacheFetchProc(tcall, file,
2256 (afs_size_t) Position, tdc, avc,
2257 &bytesToXfer, &bytesXferred, length);
2259 osi_GetuTime(&xferStopTime);
2260 (xferP->numXfers)++;
2262 (xferP->numSuccesses)++;
2263 afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] += bytesXferred;
2264 (xferP->sumBytes) += (afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] >> 10);
2265 afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] &= 0x3FF;
2266 if (bytesXferred < xferP->minBytes)
2267 xferP->minBytes = bytesXferred;
2268 if (bytesXferred > xferP->maxBytes)
2269 xferP->maxBytes = bytesXferred;
2272 * Tally the size of the object. Note: we tally the actual size,
2273 * NOT the number of bytes that made it out over the wire.
2275 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
2276 (xferP->count[0])++;
2278 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET1)
2279 (xferP->count[1])++;
2281 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET2)
2282 (xferP->count[2])++;
2284 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET3)
2285 (xferP->count[3])++;
2287 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET4)
2288 (xferP->count[4])++;
2290 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET5)
2291 (xferP->count[5])++;
2293 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET6)
2294 (xferP->count[6])++;
2296 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET7)
2297 (xferP->count[7])++;
2299 (xferP->count[8])++;
2301 afs_stats_GetDiff(elapsedTime, xferStartTime, xferStopTime);
2302 afs_stats_AddTo((xferP->sumTime), elapsedTime);
2303 afs_stats_SquareAddTo((xferP->sqrTime), elapsedTime);
2304 if (afs_stats_TimeLessThan(elapsedTime, (xferP->minTime))) {
2305 afs_stats_TimeAssign((xferP->minTime), elapsedTime);
2307 if (afs_stats_TimeGreaterThan(elapsedTime, (xferP->maxTime))) {
2308 afs_stats_TimeAssign((xferP->maxTime), elapsedTime);
2312 code = afs_CacheFetchProc(tcall, file, Position, tdc, avc, 0, 0, length);
2313 #endif /* AFS_NOSTATS */
2317 code = EndRXAFS_FetchData(tcall,
2326 code1 = rx_EndCall(tcall, code);
2332 if ( !code && code1 )
2336 /* callback could have been broken (or expired) in a race here,
2337 * but we return the data anyway. It's as good as we knew about
2338 * when we started. */
2340 * validPos is updated by CacheFetchProc, and can only be
2341 * modifed under a dcache write lock, which we've blocked out
2343 size = tdc->validPos - Position; /* actual segment size */
2344 if (size < 0) size = 0;
2345 afs_CFileTruncate(file, size); /* prune it */
2348 if (!setLocks || slowPass) {
2349 ObtainWriteLock(&afs_xcbhash, 453);
2350 afs_DequeueCallback(avc);
2351 avc->states &= ~(CStatd | CUnique);
2352 avc->callback = (struct server *)0;
2353 ReleaseWriteLock(&afs_xcbhash);
2354 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
2355 osi_dnlc_purgedp(avc);
2357 /* Something lost. Forget about performance, and go
2358 * back with a vcache write lock.
2360 afs_CFileTruncate(file, 0);
2361 afs_AdjustSize(tdc, 0);
2362 afs_CFileClose(file);
2363 osi_FreeLargeSpace(tsmall);
2365 ReleaseWriteLock(&tdc->lock);
2368 ReleaseReadLock(&avc->lock);
2370 goto RetryGetDCache;
2375 (afs_Analyze(tc, code, &avc->fid, areq,
2376 AFS_STATS_FS_RPCIDX_FETCHDATA,
2377 SHARED_LOCK, (struct cell *)0));
2381 * avc->lock(R) if setLocks && !slowPass
2382 * avc->lock(W) if !setLocks || slowPass
2388 * In the case of replicated access, jot down info on the number of
2389 * attempts it took before we got through or gave up.
2392 if (numFetchLoops <= 1)
2393 (accP->refFirstReplicaOK)++;
2394 if (numFetchLoops > accP->maxReplicasPerRef)
2395 accP->maxReplicasPerRef = numFetchLoops;
2397 #endif /* AFS_NOSTATS */
2399 tdc->dflags &= ~DFFetching;
2400 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE,
2401 ICL_TYPE_STRING, __FILE__,
2402 ICL_TYPE_INT32, __LINE__,
2403 ICL_TYPE_POINTER, tdc,
2404 ICL_TYPE_INT32, tdc->dflags);
2405 afs_osi_Wakeup(&tdc->validPos);
2406 if (avc->execsOrWriters == 0) tdc->f.states &= ~DWriting;
2408 /* now, if code != 0, we have an error and should punt.
2409 * note that we have the vcache write lock, either because
2410 * !setLocks or slowPass.
2413 afs_CFileTruncate(file, 0);
2414 afs_AdjustSize(tdc, 0);
2415 afs_CFileClose(file);
2416 ZapDCE(tdc); /* sets DFEntryMod */
2417 if (vType(avc) == VDIR) {
2418 DZap(&tdc->f.inode);
2420 ReleaseWriteLock(&tdc->lock);
2422 ObtainWriteLock(&afs_xcbhash, 454);
2423 afs_DequeueCallback(avc);
2424 avc->states &= ~( CStatd | CUnique );
2425 ReleaseWriteLock(&afs_xcbhash);
2426 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
2427 osi_dnlc_purgedp(avc);
2430 * avc->lock(W); assert(!setLocks || slowPass)
2432 osi_Assert(!setLocks || slowPass);
2433 tdc = (struct dcache *) 0;
2437 /* otherwise we copy in the just-fetched info */
2438 afs_CFileClose(file);
2439 afs_AdjustSize(tdc, size); /* new size */
2441 * Copy appropriate fields into vcache. Status is
2442 * copied later where we selectively acquire the
2443 * vcache write lock.
2446 afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2448 setVcacheStatus = 1;
2449 hset64(tdc->f.versionNo, tsmall->OutStatus.dataVersionHigh, tsmall->OutStatus.DataVersion);
2450 tdc->dflags |= DFEntryMod;
2451 afs_indexFlags[tdc->index] |= IFEverUsed;
2452 ConvertWToSLock(&tdc->lock);
2453 } /*Data version numbers don't match*/
2456 * Data version numbers match.
2458 afs_stats_cmperf.dcacheHits++;
2459 } /*Data version numbers match*/
2461 updateV2DC(setLocks, avc, tdc, 335); /* set hint */
2465 * avc->lock(R) if setLocks && !slowPass
2466 * avc->lock(W) if !setLocks || slowPass
2467 * tdc->lock(S) if tdc
2471 * See if this was a reference to a file in the local cell.
2473 if (avc->fid.Cell == LOCALCELL)
2474 afs_stats_cmperf.dlocalAccesses++;
2476 afs_stats_cmperf.dremoteAccesses++;
2478 /* Fix up LRU info */
2481 MObtainWriteLock(&afs_xdcache, 602);
2482 hset(afs_indexTimes[tdc->index], afs_indexCounter);
2483 hadd32(afs_indexCounter, 1);
2484 MReleaseWriteLock(&afs_xdcache);
2486 /* return the data */
2487 if (vType(avc) == VDIR)
2490 *aoffset = AFS_CHUNKOFFSET(abyte);
2491 *alen = (tdc->f.chunkBytes - *aoffset);
2492 ReleaseSharedLock(&tdc->lock);
2497 * avc->lock(R) if setLocks && !slowPass
2498 * avc->lock(W) if !setLocks || slowPass
2501 /* Fix up the callback and status values in the vcache */
2503 if (setLocks && !slowPass) {
2506 * This is our dirty little secret to parallel fetches.
2507 * We don't write-lock the vcache while doing the fetch,
2508 * but potentially we'll need to update the vcache after
2509 * the fetch is done.
2511 * Drop the read lock and try to re-obtain the write
2512 * lock. If the vcache still has the same DV, it's
2513 * ok to go ahead and install the new data.
2515 afs_hyper_t currentDV, statusDV;
2517 hset(currentDV, avc->m.DataVersion);
2519 if (setNewCallback && avc->callback != newCallback)
2523 hset64(statusDV, tsmall->OutStatus.dataVersionHigh,
2524 tsmall->OutStatus.DataVersion);
2526 if (setVcacheStatus && avc->m.Length != tsmall->OutStatus.Length)
2528 if (setVcacheStatus && !hsame(currentDV, statusDV))
2532 ReleaseReadLock(&avc->lock);
2534 if (doVcacheUpdate) {
2535 ObtainWriteLock(&avc->lock, 615);
2536 if (!hsame(avc->m.DataVersion, currentDV)) {
2537 /* We lose. Someone will beat us to it. */
2539 ReleaseWriteLock(&avc->lock);
2544 /* With slow pass, we've already done all the updates */
2546 ReleaseWriteLock(&avc->lock);
2549 /* Check if we need to perform any last-minute fixes with a write-lock */
2550 if (!setLocks || doVcacheUpdate) {
2551 if (setNewCallback) avc->callback = newCallback;
2552 if (tsmall && setVcacheStatus) afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2553 if (setLocks) ReleaseWriteLock(&avc->lock);
2556 if (tsmall) osi_FreeLargeSpace(tsmall);
2563 * afs_WriteThroughDSlots
2566 * Sweep through the dcache slots and write out any modified
2567 * in-memory data back on to our caching store.
2573 * The afs_xdcache is write-locked through this whole affair.
2576 afs_WriteThroughDSlots()
2578 { /*afs_WriteThroughDSlots*/
2580 register struct dcache *tdc;
2581 register afs_int32 i, touchedit=0;
2582 struct dcache **ents;
2583 int entmax, entcount;
2585 AFS_STATCNT(afs_WriteThroughDSlots);
2588 * Because of lock ordering, we can't grab dcache locks while
2589 * holding afs_xdcache. So we enter xdcache, get a reference
2590 * for every dcache entry, and exit xdcache.
2592 MObtainWriteLock(&afs_xdcache,283);
2593 entmax = afs_cacheFiles;
2594 ents = afs_osi_Alloc(entmax * sizeof(struct dcache *));
2596 for(i = 0; i < afs_cacheFiles; i++) {
2597 tdc = afs_indexTable[i];
2599 /* Grab tlock in case the existing refcount isn't zero */
2600 if (tdc && !(afs_indexFlags[i] & (IFFree | IFDiscarded))) {
2601 ObtainWriteLock(&tdc->tlock, 623);
2603 ReleaseWriteLock(&tdc->tlock);
2605 ents[entcount++] = tdc;
2608 MReleaseWriteLock(&afs_xdcache);
2611 * Now, for each dcache entry we found, check if it's dirty.
2612 * If so, get write-lock, get afs_xdcache, which protects
2613 * afs_cacheInodep, and flush it. Don't forget to put back
2616 for (i = 0; i < entcount; i++) {
2619 if (tdc->dflags & DFEntryMod) {
2622 wrLock = (0 == NBObtainWriteLock(&tdc->lock, 619));
2624 /* Now that we have the write lock, double-check */
2625 if (wrLock && (tdc->dflags & DFEntryMod)) {
2626 tdc->dflags &= ~DFEntryMod;
2627 MObtainWriteLock(&afs_xdcache, 620);
2628 afs_WriteDCache(tdc, 1);
2629 MReleaseWriteLock(&afs_xdcache);
2632 if (wrLock) ReleaseWriteLock(&tdc->lock);
2637 afs_osi_Free(ents, entmax * sizeof(struct dcache *));
2639 MObtainWriteLock(&afs_xdcache, 617);
2640 if (!touchedit && (cacheDiskType != AFS_FCACHE_TYPE_MEM)) {
2641 /* Touch the file to make sure that the mtime on the file is kept
2642 * up-to-date to avoid losing cached files on cold starts because
2643 * their mtime seems old...
2645 struct afs_fheader theader;
2647 theader.magic = AFS_FHMAGIC;
2648 theader.firstCSize = AFS_FIRSTCSIZE;
2649 theader.otherCSize = AFS_OTHERCSIZE;
2650 theader.version = AFS_CI_VERSION;
2651 afs_osi_Write(afs_cacheInodep, 0, &theader, sizeof(theader));
2653 MReleaseWriteLock(&afs_xdcache);
2655 } /*afs_WriteThroughDSlots*/
2661 * Return a pointer to an freshly initialized dcache entry using
2662 * a memory-based cache. The tlock will be read-locked.
2665 * aslot : Dcache slot to look at.
2666 * tmpdc : Ptr to dcache entry.
2669 * Must be called with afs_xdcache write-locked.
2672 struct dcache *afs_MemGetDSlot(aslot, tmpdc)
2673 register afs_int32 aslot;
2674 register struct dcache *tmpdc;
2676 { /*afs_MemGetDSlot*/
2678 register afs_int32 code;
2679 register struct dcache *tdc;
2680 register char *tfile;
2683 AFS_STATCNT(afs_MemGetDSlot);
2684 if (CheckLock(&afs_xdcache) != -1) osi_Panic("getdslot nolock");
2685 if (aslot < 0 || aslot >= afs_cacheFiles) osi_Panic("getdslot slot");
2686 tdc = afs_indexTable[aslot];
2688 QRemove(&tdc->lruq); /* move to queue head */
2689 QAdd(&afs_DLRU, &tdc->lruq);
2690 /* We're holding afs_xdcache, but get tlock in case refCount != 0 */
2691 ObtainWriteLock(&tdc->tlock, 624);
2693 ConvertWToRLock(&tdc->tlock);
2696 if (tmpdc == (struct dcache *)0) {
2697 if (!afs_freeDSList) afs_GetDownDSlot(4);
2698 if (!afs_freeDSList) {
2699 /* none free, making one is better than a panic */
2700 afs_stats_cmperf.dcacheXAllocs++; /* count in case we have a leak */
2701 tdc = (struct dcache *) afs_osi_Alloc(sizeof (struct dcache));
2702 #ifdef AFS_AIX32_ENV
2703 pin((char *)tdc, sizeof(struct dcache)); /* XXX */
2706 tdc = afs_freeDSList;
2707 afs_freeDSList = (struct dcache *) tdc->lruq.next;
2710 tdc->dflags = 0; /* up-to-date, not in free q */
2712 QAdd(&afs_DLRU, &tdc->lruq);
2713 if (tdc->lruq.prev == &tdc->lruq) osi_Panic("lruq 3");
2720 /* initialize entry */
2721 tdc->f.fid.Cell = 0;
2722 tdc->f.fid.Fid.Volume = 0;
2724 hones(tdc->f.versionNo);
2725 tdc->f.inode = aslot;
2726 tdc->dflags |= DFEntryMod;
2729 afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
2732 osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
2733 osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
2734 osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
2737 RWLOCK_INIT(&tdc->lock, "dcache lock");
2738 RWLOCK_INIT(&tdc->tlock, "dcache tlock");
2739 RWLOCK_INIT(&tdc->mflock, "dcache flock");
2740 ObtainReadLock(&tdc->tlock);
2742 if (tmpdc == (struct dcache *)0)
2743 afs_indexTable[aslot] = tdc;
2746 } /*afs_MemGetDSlot*/
2748 unsigned int last_error = 0, lasterrtime = 0;
2754 * Return a pointer to an freshly initialized dcache entry using
2755 * a UFS-based disk cache. The dcache tlock will be read-locked.
2758 * aslot : Dcache slot to look at.
2759 * tmpdc : Ptr to dcache entry.
2762 * afs_xdcache lock write-locked.
2764 struct dcache *afs_UFSGetDSlot(aslot, tmpdc)
2765 register afs_int32 aslot;
2766 register struct dcache *tmpdc;
2768 { /*afs_UFSGetDSlot*/
2770 register afs_int32 code;
2771 register struct dcache *tdc;
2774 AFS_STATCNT(afs_UFSGetDSlot);
2775 if (CheckLock(&afs_xdcache) != -1) osi_Panic("getdslot nolock");
2776 if (aslot < 0 || aslot >= afs_cacheFiles) osi_Panic("getdslot slot");
2777 tdc = afs_indexTable[aslot];
2779 QRemove(&tdc->lruq); /* move to queue head */
2780 QAdd(&afs_DLRU, &tdc->lruq);
2781 /* Grab tlock in case refCount != 0 */
2782 ObtainWriteLock(&tdc->tlock, 625);
2784 ConvertWToRLock(&tdc->tlock);
2787 /* otherwise we should read it in from the cache file */
2789 * If we weren't passed an in-memory region to place the file info,
2790 * we have to allocate one.
2792 if (tmpdc == (struct dcache *)0) {
2793 if (!afs_freeDSList) afs_GetDownDSlot(4);
2794 if (!afs_freeDSList) {
2795 /* none free, making one is better than a panic */
2796 afs_stats_cmperf.dcacheXAllocs++; /* count in case we have a leak */
2797 tdc = (struct dcache *) afs_osi_Alloc(sizeof (struct dcache));
2798 #ifdef AFS_AIX32_ENV
2799 pin((char *)tdc, sizeof(struct dcache)); /* XXX */
2802 tdc = afs_freeDSList;
2803 afs_freeDSList = (struct dcache *) tdc->lruq.next;
2806 tdc->dflags = 0; /* up-to-date, not in free q */
2808 QAdd(&afs_DLRU, &tdc->lruq);
2809 if (tdc->lruq.prev == &tdc->lruq) osi_Panic("lruq 3");
2818 * Seek to the aslot'th entry and read it in.
2820 code = afs_osi_Read(afs_cacheInodep, sizeof(struct fcache) * aslot + sizeof(struct afs_fheader),
2821 (char *)(&tdc->f), sizeof(struct fcache));
2822 if (code != sizeof(struct fcache)) {
2823 tdc->f.fid.Cell = 0;
2824 tdc->f.fid.Fid.Volume = 0;
2826 hones(tdc->f.versionNo);
2827 tdc->dflags |= DFEntryMod;
2828 #if !defined(AFS_SUN5_ENV) && !defined(AFS_OSF_ENV) && !defined(AFS_SGI64_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
2829 last_error = getuerror();
2831 lasterrtime = osi_Time();
2832 afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
2838 osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
2839 osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
2840 osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
2843 RWLOCK_INIT(&tdc->lock, "dcache lock");
2844 RWLOCK_INIT(&tdc->tlock, "dcache tlock");
2845 RWLOCK_INIT(&tdc->mflock, "dcache flock");
2846 ObtainReadLock(&tdc->tlock);
2849 * If we didn't read into a temporary dcache region, update the
2850 * slot pointer table.
2852 if (tmpdc == (struct dcache *)0)
2853 afs_indexTable[aslot] = tdc;
2856 } /*afs_UFSGetDSlot*/
2864 * write a particular dcache entry back to its home in the
2868 * adc : Pointer to the dcache entry to write.
2869 * atime : If true, set the modtime on the file to the current time.
2872 * Must be called with the afs_xdcache lock at least read-locked,
2873 * and dcache entry at least read-locked.
2874 * The reference count is not changed.
2877 afs_WriteDCache(adc, atime)
2879 register struct dcache *adc;
2881 { /*afs_WriteDCache*/
2883 register struct osi_file *tfile;
2884 register afs_int32 code;
2886 if (cacheDiskType == AFS_FCACHE_TYPE_MEM) return 0;
2887 AFS_STATCNT(afs_WriteDCache);
2889 adc->f.modTime = osi_Time();
2891 * Seek to the right dcache slot and write the in-memory image out to disk.
2893 code = afs_osi_Write(afs_cacheInodep, sizeof(struct fcache) * adc->index + sizeof(struct afs_fheader),
2894 (char *)(&adc->f), sizeof(struct fcache));
2895 if (code != sizeof(struct fcache)) return EIO;
2898 } /*afs_WriteDCache*/
2906 * Wake up users of a particular file waiting for stores to take
2910 * avc : Ptr to related vcache entry.
2913 * Nothing interesting.
2917 register struct vcache *avc;
2922 register struct brequest *tb;
2924 AFS_STATCNT(afs_wakeup);
2925 for (i = 0; i < NBRS; i++, tb++) {
2926 /* if request is valid and for this file, we've found it */
2927 if (tb->refCount > 0 && avc == tb->vnode) {
2930 * If CSafeStore is on, then we don't awaken the guy
2931 * waiting for the store until the whole store has finished.
2932 * Otherwise, we do it now. Note that if CSafeStore is on,
2933 * the BStore routine actually wakes up the user, instead
2935 * I think this is redundant now because this sort of thing
2936 * is already being handled by the higher-level code.
2938 if ((avc->states & CSafeStore) == 0) {
2940 tb->flags |= BUVALID;
2941 if (tb->flags & BUWAIT) {
2942 tb->flags &= ~BUWAIT;
2958 * Given a file name and inode, set up that file to be an
2959 * active member in the AFS cache. This also involves checking
2960 * the usability of its data.
2963 * afile : Name of the cache file to initialize.
2964 * ainode : Inode of the file.
2967 * This function is called only during initialization.
2970 int afs_InitCacheFile(afile, ainode)
2974 { /*afs_InitCacheFile*/
2976 register afs_int32 code;
2977 #if defined(AFS_LINUX22_ENV)
2978 struct dentry *filevp;
2980 struct vnode *filevp;
2984 struct osi_file *tfile;
2985 struct osi_stat tstat;
2986 register struct dcache *tdc;
2988 AFS_STATCNT(afs_InitCacheFile);
2989 index = afs_stats_cmperf.cacheNumEntries;
2990 if (index >= afs_cacheFiles) return EINVAL;
2992 MObtainWriteLock(&afs_xdcache,282);
2993 tdc = afs_GetDSlot(index, (struct dcache *)0);
2994 ReleaseReadLock(&tdc->tlock);
2995 MReleaseWriteLock(&afs_xdcache);
2997 ObtainWriteLock(&tdc->lock, 621);
2998 MObtainWriteLock(&afs_xdcache, 622);
3000 code = gop_lookupname(afile,
3003 (struct vnode **) 0,
3006 ReleaseWriteLock(&afs_xdcache);
3007 ReleaseWriteLock(&tdc->lock);
3012 * We have a VN_HOLD on filevp. Get the useful info out and
3013 * return. We make use of the fact that the cache is in the
3014 * UFS file system, and just record the inode number.
3016 #ifdef AFS_LINUX22_ENV
3017 tdc->f.inode = VTOI(filevp->d_inode)->i_number;
3020 tdc->f.inode = afs_vnodeToInumber(filevp);
3024 AFS_RELE((struct vnode *)filevp);
3026 #endif /* AFS_LINUX22_ENV */
3029 tdc->f.inode = ainode;
3032 if ((tdc->f.states & DWriting) ||
3033 tdc->f.fid.Fid.Volume == 0) fileIsBad = 1;
3034 tfile = osi_UFSOpen(tdc->f.inode);
3035 code = afs_osi_Stat(tfile, &tstat);
3036 if (code) osi_Panic("initcachefile stat");
3039 * If file size doesn't match the cache info file, it's probably bad.
3041 if (tdc->f.chunkBytes != tstat.size) fileIsBad = 1;
3042 tdc->f.chunkBytes = 0;
3045 * If file changed within T (120?) seconds of cache info file, it's
3046 * probably bad. In addition, if slot changed within last T seconds,
3047 * the cache info file may be incorrectly identified, and so slot
3050 if (cacheInfoModTime < tstat.mtime + 120) fileIsBad = 1;
3051 if (cacheInfoModTime < tdc->f.modTime + 120) fileIsBad = 1;
3052 /* In case write through is behind, make sure cache items entry is
3053 * at least as new as the chunk.
3055 if (tdc->f.modTime < tstat.mtime) fileIsBad = 1;
3057 tdc->f.fid.Fid.Volume = 0; /* not in the hash table */
3058 if (tstat.size != 0)
3059 osi_UFSTruncate(tfile, 0);
3060 /* put entry in free cache slot list */
3061 afs_dvnextTbl[tdc->index] = afs_freeDCList;
3062 afs_freeDCList = index;
3064 afs_indexFlags[index] |= IFFree;
3065 afs_indexUnique[index] = 0;
3069 * We must put this entry in the appropriate hash tables.
3070 * Note that i is still set from the above DCHash call
3072 code = DCHash(&tdc->f.fid, tdc->f.chunk);
3073 afs_dcnextTbl[tdc->index] = afs_dchashTbl[code];
3074 afs_dchashTbl[code] = tdc->index;
3075 code = DVHash(&tdc->f.fid);
3076 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[code];
3077 afs_dvhashTbl[code] = tdc->index;
3078 afs_AdjustSize(tdc, tstat.size); /* adjust to new size */
3080 /* has nontrivial amt of data */
3081 afs_indexFlags[index] |= IFEverUsed;
3082 afs_stats_cmperf.cacheFilesReused++;
3084 * Initialize index times to file's mod times; init indexCounter
3087 hset32(afs_indexTimes[index], tstat.atime);
3088 if (hgetlo(afs_indexCounter) < tstat.atime) {
3089 hset32(afs_indexCounter, tstat.atime);
3091 afs_indexUnique[index] = tdc->f.fid.Fid.Unique;
3092 } /*File is not bad*/
3094 osi_UFSClose(tfile);
3095 tdc->f.states &= ~DWriting;
3096 tdc->dflags &= ~DFEntryMod;
3097 /* don't set f.modTime; we're just cleaning up */
3098 afs_WriteDCache(tdc, 0);
3099 ReleaseWriteLock(&afs_xdcache);
3100 ReleaseWriteLock(&tdc->lock);
3102 afs_stats_cmperf.cacheNumEntries++;
3105 } /*afs_InitCacheFile*/
3108 /*Max # of struct dcache's resident at any time*/
3110 * If 'dchint' is enabled then in-memory dcache min is increased because of
3119 * Initialize dcache related variables.
3121 void afs_dcacheInit(int afiles, int ablocks, int aDentries, int achunk,
3124 register struct dcache *tdp;
3128 afs_freeDCList = NULLIDX;
3129 afs_discardDCList = NULLIDX;
3130 afs_freeDCCount = 0;
3131 afs_freeDSList = (struct dcache *)0;
3132 hzero(afs_indexCounter);
3134 LOCK_INIT(&afs_xdcache, "afs_xdcache");
3140 if (achunk < 0 || achunk > 30)
3141 achunk = 13; /* Use default */
3142 AFS_SETCHUNKSIZE(achunk);
3148 if(aflags & AFSCALL_INIT_MEMCACHE) {
3150 * Use a memory cache instead of a disk cache
3152 cacheDiskType = AFS_FCACHE_TYPE_MEM;
3153 afs_cacheType = &afs_MemCacheOps;
3154 afiles = (afiles < aDentries) ? afiles : aDentries; /* min */
3155 ablocks = afiles * (AFS_FIRSTCSIZE/1024);
3156 /* ablocks is reported in 1K blocks */
3157 code = afs_InitMemCache(afiles * AFS_FIRSTCSIZE, AFS_FIRSTCSIZE, aflags);
3159 printf("afsd: memory cache too large for available memory.\n");
3160 printf("afsd: AFS files cannot be accessed.\n\n");
3162 afiles = ablocks = 0;
3165 printf("Memory cache: Allocating %d dcache entries...", aDentries);
3167 cacheDiskType = AFS_FCACHE_TYPE_UFS;
3168 afs_cacheType = &afs_UfsCacheOps;
3171 if (aDentries > 512)
3172 afs_dhashsize = 2048;
3173 /* initialize hash tables */
3174 afs_dvhashTbl = (afs_int32 *) afs_osi_Alloc(afs_dhashsize * sizeof(afs_int32));
3175 afs_dchashTbl = (afs_int32 *) afs_osi_Alloc(afs_dhashsize * sizeof(afs_int32));
3176 for(i=0;i< afs_dhashsize;i++) {
3177 afs_dvhashTbl[i] = NULLIDX;
3178 afs_dchashTbl[i] = NULLIDX;
3180 afs_dvnextTbl = (afs_int32 *) afs_osi_Alloc(afiles * sizeof(afs_int32));
3181 afs_dcnextTbl = (afs_int32 *) afs_osi_Alloc(afiles * sizeof(afs_int32));
3182 for(i=0;i< afiles;i++) {
3183 afs_dvnextTbl[i] = NULLIDX;
3184 afs_dcnextTbl[i] = NULLIDX;
3187 /* Allocate and zero the pointer array to the dcache entries */
3188 afs_indexTable = (struct dcache **)
3189 afs_osi_Alloc(sizeof(struct dcache *) * afiles);
3190 memset((char *)afs_indexTable, 0, sizeof(struct dcache *) * afiles);
3191 afs_indexTimes = (afs_hyper_t *) afs_osi_Alloc(afiles * sizeof(afs_hyper_t));
3192 memset((char *)afs_indexTimes, 0, afiles * sizeof(afs_hyper_t));
3193 afs_indexUnique = (afs_int32 *) afs_osi_Alloc(afiles * sizeof(afs_uint32));
3194 memset((char *)afs_indexUnique, 0, afiles * sizeof(afs_uint32));
3195 afs_indexFlags = (u_char *) afs_osi_Alloc(afiles * sizeof(u_char));
3196 memset((char *)afs_indexFlags, 0, afiles * sizeof(char));
3198 /* Allocate and thread the struct dcache entries themselves */
3199 tdp = afs_Initial_freeDSList =
3200 (struct dcache *) afs_osi_Alloc(aDentries * sizeof(struct dcache));
3201 memset((char *)tdp, 0, aDentries * sizeof(struct dcache));
3202 #ifdef AFS_AIX32_ENV
3203 pin((char *)afs_indexTable, sizeof(struct dcache *) * afiles);/* XXX */
3204 pin((char *)afs_indexTimes, sizeof(afs_hyper_t) * afiles); /* XXX */
3205 pin((char *)afs_indexFlags, sizeof(char) * afiles); /* XXX */
3206 pin((char *)afs_indexUnique, sizeof(afs_int32) * afiles); /* XXX */
3207 pin((char *)tdp, aDentries * sizeof(struct dcache)); /* XXX */
3208 pin((char *)afs_dvhashTbl, sizeof(afs_int32) * afs_dhashsize); /* XXX */
3209 pin((char *)afs_dchashTbl, sizeof(afs_int32) * afs_dhashsize); /* XXX */
3210 pin((char *)afs_dcnextTbl, sizeof(afs_int32) * afiles); /* XXX */
3211 pin((char *)afs_dvnextTbl, sizeof(afs_int32) * afiles); /* XXX */
3214 afs_freeDSList = &tdp[0];
3215 for(i=0; i < aDentries-1; i++) {
3216 tdp[i].lruq.next = (struct afs_q *) (&tdp[i+1]);
3218 tdp[aDentries-1].lruq.next = (struct afs_q *) 0;
3220 afs_stats_cmperf.cacheBlocksOrig = afs_stats_cmperf.cacheBlocksTotal = afs_cacheBlocks = ablocks;
3221 afs_ComputeCacheParms(); /* compute parms based on cache size */
3223 afs_dcentries = aDentries;
3232 void shutdown_dcache(void)
3236 afs_osi_Free(afs_dvnextTbl, afs_cacheFiles * sizeof(afs_int32));
3237 afs_osi_Free(afs_dcnextTbl, afs_cacheFiles * sizeof(afs_int32));
3238 afs_osi_Free(afs_indexTable, afs_cacheFiles * sizeof(struct dcache *));
3239 afs_osi_Free(afs_indexTimes, afs_cacheFiles * sizeof(afs_hyper_t));
3240 afs_osi_Free(afs_indexUnique, afs_cacheFiles * sizeof(afs_uint32));
3241 afs_osi_Free(afs_indexFlags, afs_cacheFiles * sizeof(u_char));
3242 afs_osi_Free(afs_Initial_freeDSList, afs_dcentries * sizeof(struct dcache));
3243 #ifdef AFS_AIX32_ENV
3244 unpin((char *)afs_dcnextTbl, afs_cacheFiles * sizeof(afs_int32));
3245 unpin((char *)afs_dvnextTbl, afs_cacheFiles * sizeof(afs_int32));
3246 unpin((char *)afs_indexTable, afs_cacheFiles * sizeof(struct dcache *));
3247 unpin((char *)afs_indexTimes, afs_cacheFiles * sizeof(afs_hyper_t));
3248 unpin((char *)afs_indexUnique, afs_cacheFiles * sizeof(afs_uint32));
3249 unpin((u_char *)afs_indexFlags, afs_cacheFiles * sizeof(u_char));
3250 unpin(afs_Initial_freeDSList, afs_dcentries * sizeof(struct dcache));
3254 for(i=0;i< afs_dhashsize;i++) {
3255 afs_dvhashTbl[i] = NULLIDX;
3256 afs_dchashTbl[i] = NULLIDX;
3260 afs_blocksUsed = afs_dcentries = 0;
3261 hzero(afs_indexCounter);
3263 afs_freeDCCount = 0;
3264 afs_freeDCList = NULLIDX;
3265 afs_discardDCList = NULLIDX;
3266 afs_freeDSList = afs_Initial_freeDSList = 0;
3268 LOCK_INIT(&afs_xdcache, "afs_xdcache");