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 "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 *);
31 * --------------------- Exported definitions ---------------------
33 afs_lock_t afs_xdcache; /*Lock: alloc new disk cache entries*/
34 afs_int32 afs_freeDCList; /*Free list for disk cache entries*/
35 afs_int32 afs_freeDCCount; /*Count of elts in freeDCList*/
36 afs_int32 afs_discardDCList; /*Discarded disk cache entries*/
37 afs_int32 afs_discardDCCount; /*Count of elts in discardDCList*/
38 struct dcache *afs_freeDSList; /*Free list for disk slots */
39 struct dcache *afs_Initial_freeDSList; /*Initial list for above*/
40 ino_t cacheInode; /*Inode for CacheItems file*/
41 struct osi_file *afs_cacheInodep = 0; /* file for CacheItems inode */
42 struct afs_q afs_DLRU; /*dcache LRU*/
43 afs_int32 afs_dhashsize = 1024;
44 afs_int32 *afs_dvhashTbl; /*Data cache hash table*/
45 afs_int32 *afs_dchashTbl; /*Data cache hash table*/
46 afs_int32 *afs_dvnextTbl; /*Dcache hash table links */
47 afs_int32 *afs_dcnextTbl; /*Dcache hash table links */
48 struct dcache **afs_indexTable; /*Pointers to dcache entries*/
49 afs_hyper_t *afs_indexTimes; /*Dcache entry Access times*/
50 afs_int32 *afs_indexUnique; /*dcache entry Fid.Unique */
51 unsigned char *afs_indexFlags; /*(only one) Is there data there?*/
52 afs_hyper_t afs_indexCounter; /*Fake time for marking index
54 afs_int32 afs_cacheFiles =0; /*Size of afs_indexTable*/
55 afs_int32 afs_cacheBlocks; /*1K blocks in cache*/
56 afs_int32 afs_cacheStats; /*Stat entries in cache*/
57 afs_int32 afs_blocksUsed; /*Number of blocks in use*/
58 afs_int32 afs_blocksDiscarded; /*Blocks freed but not truncated */
59 afs_int32 afs_fsfragsize = 1023; /*Underlying Filesystem minimum unit
60 *of disk allocation usually 1K
61 *this value is (truefrag -1 ) to
62 *save a bunch of subtracts... */
63 #ifdef AFS_64BIT_CLIENT
64 #ifdef AFS_VM_RDWR_ENV
65 afs_size_t afs_vmMappingEnd; /* for large files (>= 2GB) the VM
66 * mapping an 32bit addressing machines
67 * can only be used below the 2 GB
68 * line. From this point upwards we
69 * must do direct I/O into the cache
70 * files. The value should be on a
72 #endif /* AFS_VM_RDWR_ENV */
73 #endif /* AFS_64BIT_CLIENT */
75 /* The following is used to ensure that new dcache's aren't obtained when
76 * the cache is nearly full.
78 int afs_WaitForCacheDrain = 0;
79 int afs_TruncateDaemonRunning = 0;
80 int afs_CacheTooFull = 0;
82 afs_int32 afs_dcentries; /* In-memory dcache entries */
85 int dcacheDisabled = 0;
87 static int afs_UFSCacheFetchProc(), afs_UFSCacheStoreProc();
88 struct afs_cacheOps afs_UfsCacheOps = {
96 afs_UFSCacheFetchProc,
97 afs_UFSCacheStoreProc,
103 struct afs_cacheOps afs_MemCacheOps = {
105 afs_MemCacheTruncate,
111 afs_MemCacheFetchProc,
112 afs_MemCacheStoreProc,
118 int cacheDiskType; /*Type of backing disk for cache*/
119 struct afs_cacheOps *afs_cacheType;
128 * Warn about failing to store a file.
131 * acode : Associated error code.
132 * avolume : Volume involved.
133 * aflags : How to handle the output:
134 * aflags & 1: Print out on console
135 * aflags & 2: Print out on controlling tty
138 * Call this from close call when vnodeops is RCS unlocked.
141 void afs_StoreWarn(register afs_int32 acode, afs_int32 avolume, register afs_int32 aflags)
143 static char problem_fmt[] =
144 "afs: failed to store file in volume %d (%s)\n";
145 static char problem_fmt_w_error[] =
146 "afs: failed to store file in volume %d (error %d)\n";
147 static char netproblems[] = "network problems";
148 static char partfull[] = "partition full";
149 static char overquota[] = "over quota";
151 AFS_STATCNT(afs_StoreWarn);
157 afs_warn(problem_fmt, avolume, netproblems);
159 afs_warnuser(problem_fmt, avolume, netproblems);
162 if (acode == ENOSPC) {
167 afs_warn(problem_fmt, avolume, partfull);
169 afs_warnuser(problem_fmt, avolume, partfull);
173 /* EDQUOT doesn't exist on solaris and won't be sent by the server.
174 * Instead ENOSPC will be sent...
176 if (acode == EDQUOT) {
181 afs_warn(problem_fmt, avolume, overquota);
183 afs_warnuser(problem_fmt, avolume, overquota);
191 afs_warn(problem_fmt_w_error, avolume, acode);
193 afs_warnuser(problem_fmt_w_error, avolume, acode);
197 void afs_MaybeWakeupTruncateDaemon(void)
199 if (!afs_CacheTooFull && afs_CacheIsTooFull()) {
200 afs_CacheTooFull = 1;
201 if (!afs_TruncateDaemonRunning)
202 afs_osi_Wakeup((int *)afs_CacheTruncateDaemon);
203 } else if (!afs_TruncateDaemonRunning &&
204 afs_blocksDiscarded > CM_MAXDISCARDEDCHUNKS) {
205 afs_osi_Wakeup((int *)afs_CacheTruncateDaemon);
209 /* Keep statistics on run time for afs_CacheTruncateDaemon. This is a
210 * struct so we need only export one symbol for AIX.
212 static struct CTD_stats {
213 osi_timeval_t CTD_beforeSleep;
214 osi_timeval_t CTD_afterSleep;
215 osi_timeval_t CTD_sleepTime;
216 osi_timeval_t CTD_runTime;
220 u_int afs_min_cache = 0;
221 void afs_CacheTruncateDaemon(void)
223 osi_timeval_t CTD_tmpTime;
226 u_int dc_hiwat = (100-CM_DCACHECOUNTFREEPCT+CM_DCACHEEXTRAPCT)*afs_cacheFiles/100;
227 afs_min_cache = (((10 * AFS_CHUNKSIZE(0)) + afs_fsfragsize) & ~afs_fsfragsize)>>10;
229 osi_GetuTime(&CTD_stats.CTD_afterSleep);
230 afs_TruncateDaemonRunning = 1;
232 cb_lowat = ((CM_DCACHESPACEFREEPCT-CM_DCACHEEXTRAPCT)
233 * afs_cacheBlocks) / 100;
234 MObtainWriteLock(&afs_xdcache,266);
235 if (afs_CacheTooFull) {
236 int space_needed, slots_needed;
237 /* if we get woken up, we should try to clean something out */
238 for (counter = 0; counter < 10; counter++) {
239 space_needed = afs_blocksUsed - afs_blocksDiscarded - cb_lowat;
240 slots_needed = dc_hiwat - afs_freeDCCount - afs_discardDCCount;
241 afs_GetDownD(slots_needed, &space_needed);
242 if ((space_needed <= 0) && (slots_needed <= 0)) {
245 if (afs_termState == AFSOP_STOP_TRUNCDAEMON)
248 if (!afs_CacheIsTooFull())
249 afs_CacheTooFull = 0;
251 MReleaseWriteLock(&afs_xdcache);
254 * This is a defensive check to try to avoid starving threads
255 * that may need the global lock so thay can help free some
256 * cache space. If this thread won't be sleeping or truncating
257 * any cache files then give up the global lock so other
258 * threads get a chance to run.
260 if ((afs_termState!=AFSOP_STOP_TRUNCDAEMON) && afs_CacheTooFull &&
261 (!afs_blocksDiscarded || afs_WaitForCacheDrain)) {
262 afs_osi_Wait(100, 0, 0); /* 100 milliseconds */
266 * This is where we free the discarded cache elements.
268 while(afs_blocksDiscarded && !afs_WaitForCacheDrain &&
269 (afs_termState!=AFSOP_STOP_TRUNCDAEMON))
271 afs_FreeDiscardedDCache();
274 /* See if we need to continue to run. Someone may have
275 * signalled us while we were executing.
277 if (!afs_WaitForCacheDrain && !afs_CacheTooFull &&
278 (afs_termState!=AFSOP_STOP_TRUNCDAEMON))
280 /* Collect statistics on truncate daemon. */
281 CTD_stats.CTD_nSleeps++;
282 osi_GetuTime(&CTD_stats.CTD_beforeSleep);
283 afs_stats_GetDiff(CTD_tmpTime, CTD_stats.CTD_afterSleep,
284 CTD_stats.CTD_beforeSleep);
285 afs_stats_AddTo(CTD_stats.CTD_runTime, CTD_tmpTime);
287 afs_TruncateDaemonRunning = 0;
288 afs_osi_Sleep((int *)afs_CacheTruncateDaemon);
289 afs_TruncateDaemonRunning = 1;
291 osi_GetuTime(&CTD_stats.CTD_afterSleep);
292 afs_stats_GetDiff(CTD_tmpTime, CTD_stats.CTD_beforeSleep,
293 CTD_stats.CTD_afterSleep);
294 afs_stats_AddTo(CTD_stats.CTD_sleepTime, CTD_tmpTime);
296 if (afs_termState == AFSOP_STOP_TRUNCDAEMON) {
298 afs_termState = AFSOP_STOP_AFSDB;
300 afs_termState = AFSOP_STOP_RXEVENT;
302 afs_osi_Wakeup(&afs_termState);
313 * Make adjustment for the new size in the disk cache entry
315 * Major Assumptions Here:
316 * Assumes that frag size is an integral power of two, less one,
317 * and that this is a two's complement machine. I don't
318 * know of any filesystems which violate this assumption...
321 * adc : Ptr to dcache entry.
322 * anewsize : New size desired.
325 void afs_AdjustSize(register struct dcache *adc, register afs_int32 newSize)
327 register afs_int32 oldSize;
329 AFS_STATCNT(afs_AdjustSize);
331 adc->dflags |= DFEntryMod;
332 oldSize = ((adc->f.chunkBytes + afs_fsfragsize)^afs_fsfragsize)>>10;/* round up */
333 adc->f.chunkBytes = newSize;
334 newSize = ((newSize + afs_fsfragsize)^afs_fsfragsize)>>10;/* round up */
335 if (newSize > oldSize) {
336 /* We're growing the file, wakeup the daemon */
337 afs_MaybeWakeupTruncateDaemon();
339 afs_blocksUsed += (newSize - oldSize);
340 afs_stats_cmperf.cacheBlocksInUse = afs_blocksUsed; /* XXX */
348 * This routine is responsible for moving at least one entry (but up
349 * to some number of them) from the LRU queue to the free queue.
352 * anumber : Number of entries that should ideally be moved.
353 * aneedSpace : How much space we need (1K blocks);
356 * The anumber parameter is just a hint; at least one entry MUST be
357 * moved, or we'll panic. We must be called with afs_xdcache
358 * write-locked. We should try to satisfy both anumber and aneedspace,
359 * whichever is more demanding - need to do several things:
360 * 1. only grab up to anumber victims if aneedSpace <= 0, not
361 * the whole set of MAXATONCE.
362 * 2. dynamically choose MAXATONCE to reflect severity of
363 * demand: something like (*aneedSpace >> (logChunk - 9))
364 * N.B. if we're called with aneedSpace <= 0 and anumber > 0, that
365 * indicates that the cache is not properly configured/tuned or
366 * something. We should be able to automatically correct that problem.
369 #define MAXATONCE 16 /* max we can obtain at once */
370 static void afs_GetDownD(int anumber, int *aneedSpace)
374 struct VenusFid *afid;
378 register struct vcache *tvc;
379 afs_uint32 victims[MAXATONCE];
380 struct dcache *victimDCs[MAXATONCE];
381 afs_hyper_t victimTimes[MAXATONCE];/* youngest (largest LRU time) first */
382 afs_uint32 victimPtr; /* next free item in victim arrays */
383 afs_hyper_t maxVictimTime; /* youngest (largest LRU time) victim */
384 afs_uint32 maxVictimPtr; /* where it is */
387 AFS_STATCNT(afs_GetDownD);
388 if (CheckLock(&afs_xdcache) != -1)
389 osi_Panic("getdownd nolock");
390 /* decrement anumber first for all dudes in free list */
391 /* SHOULD always decrement anumber first, even if aneedSpace >0,
392 * because we should try to free space even if anumber <=0 */
393 if (!aneedSpace || *aneedSpace <= 0) {
394 anumber -= afs_freeDCCount;
395 if (anumber <= 0) return; /* enough already free */
397 /* bounds check parameter */
398 if (anumber > MAXATONCE)
399 anumber = MAXATONCE; /* all we can do */
402 * The phase variable manages reclaims. Set to 0, the first pass,
403 * we don't reclaim active entries. Set to 1, we reclaim even active
407 for (i = 0; i < afs_cacheFiles; i++)
408 /* turn off all flags */
409 afs_indexFlags[i] &= ~IFFlag;
411 while (anumber > 0 || (aneedSpace && *aneedSpace >0)) {
412 /* find oldest entries for reclamation */
413 maxVictimPtr = victimPtr = 0;
414 hzero(maxVictimTime);
415 /* select victims from access time array */
416 for (i = 0; i < afs_cacheFiles; i++) {
417 if (afs_indexFlags[i] & (IFDataMod | IFFree | IFDiscarded)) {
418 /* skip if dirty or already free */
421 tdc = afs_indexTable[i];
422 if (tdc && (tdc->refCount != 0)) {
423 /* Referenced; can't use it! */
426 hset(vtime, afs_indexTimes[i]);
428 /* if we've already looked at this one, skip it */
429 if (afs_indexFlags[i] & IFFlag) continue;
431 if (victimPtr < MAXATONCE) {
432 /* if there's at least one free victim slot left */
433 victims[victimPtr] = i;
434 hset(victimTimes[victimPtr], vtime);
435 if (hcmp(vtime, maxVictimTime) > 0) {
436 hset(maxVictimTime, vtime);
437 maxVictimPtr = victimPtr;
441 else if (hcmp(vtime, maxVictimTime) < 0) {
443 * We're older than youngest victim, so we replace at
446 /* find youngest (largest LRU) victim */
448 if (j == victimPtr) osi_Panic("getdownd local");
450 hset(victimTimes[j], vtime);
451 /* recompute maxVictimTime */
452 hset(maxVictimTime, vtime);
453 for(j = 0; j < victimPtr; j++)
454 if (hcmp(maxVictimTime, victimTimes[j]) < 0) {
455 hset(maxVictimTime, victimTimes[j]);
461 /* now really reclaim the victims */
462 j = 0; /* flag to track if we actually got any of the victims */
463 /* first, hold all the victims, since we're going to release the lock
464 * during the truncate operation.
466 for(i=0; i < victimPtr; i++) {
467 tdc = afs_GetDSlot(victims[i], 0);
468 /* We got tdc->tlock(R) here */
469 if (tdc->refCount == 1)
473 ReleaseReadLock(&tdc->tlock);
474 if (!victimDCs[i]) afs_PutDCache(tdc);
476 for(i = 0; i < victimPtr; i++) {
477 /* q is first elt in dcache entry */
479 /* now, since we're dropping the afs_xdcache lock below, we
480 * have to verify, before proceeding, that there are no other
481 * references to this dcache entry, even now. Note that we
482 * compare with 1, since we bumped it above when we called
483 * afs_GetDSlot to preserve the entry's identity.
485 if (tdc && tdc->refCount == 1) {
486 unsigned char chunkFlags;
487 afs_size_t tchunkoffset;
489 /* xdcache is lower than the xvcache lock */
490 MReleaseWriteLock(&afs_xdcache);
491 MObtainReadLock(&afs_xvcache);
492 tvc = afs_FindVCache(afid, 0, 0 /* no stats, no vlru */ );
493 MReleaseReadLock(&afs_xvcache);
494 MObtainWriteLock(&afs_xdcache, 527);
496 if (tdc->refCount > 1) skip = 1;
498 tchunkoffset = AFS_CHUNKTOBASE(tdc->f.chunk);
499 chunkFlags = afs_indexFlags[tdc->index];
500 if (phase == 0 && osi_Active(tvc)) skip = 1;
501 if (phase > 0 && osi_Active(tvc) && (tvc->states & CDCLock)
502 && (chunkFlags & IFAnyPages)) skip = 1;
503 if (chunkFlags & IFDataMod) skip = 1;
504 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
505 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, skip,
506 ICL_TYPE_INT32, tdc->index,
508 ICL_HANDLE_OFFSET(tchunkoffset));
510 #if defined(AFS_SUN5_ENV)
512 * Now we try to invalidate pages. We do this only for
513 * Solaris. For other platforms, it's OK to recycle a
514 * dcache entry out from under a page, because the strategy
515 * function can call afs_GetDCache().
517 if (!skip && (chunkFlags & IFAnyPages)) {
520 MReleaseWriteLock(&afs_xdcache);
521 MObtainWriteLock(&tvc->vlock, 543);
522 if (tvc->multiPage) {
526 /* block locking pages */
527 tvc->vstates |= VPageCleaning;
528 /* block getting new pages */
530 MReleaseWriteLock(&tvc->vlock);
531 /* One last recheck */
532 MObtainWriteLock(&afs_xdcache, 333);
533 chunkFlags = afs_indexFlags[tdc->index];
534 if (tdc->refCount > 1
535 || (chunkFlags & IFDataMod)
536 || (osi_Active(tvc) && (tvc->states & CDCLock)
537 && (chunkFlags & IFAnyPages))) {
539 MReleaseWriteLock(&afs_xdcache);
542 MReleaseWriteLock(&afs_xdcache);
544 code = osi_VM_GetDownD(tvc, tdc);
546 MObtainWriteLock(&afs_xdcache,269);
547 /* we actually removed all pages, clean and dirty */
549 afs_indexFlags[tdc->index] &= ~(IFDirtyPages| IFAnyPages);
552 MReleaseWriteLock(&afs_xdcache);
554 MObtainWriteLock(&tvc->vlock, 544);
555 if (--tvc->activeV == 0 && (tvc->vstates & VRevokeWait)) {
556 tvc->vstates &= ~VRevokeWait;
557 afs_osi_Wakeup((char *)&tvc->vstates);
560 if (tvc->vstates & VPageCleaning) {
561 tvc->vstates &= ~VPageCleaning;
562 afs_osi_Wakeup((char *)&tvc->vstates);
565 MReleaseWriteLock(&tvc->vlock);
567 #endif /* AFS_SUN5_ENV */
569 MReleaseWriteLock(&afs_xdcache);
573 MObtainWriteLock(&afs_xdcache, 528);
574 if (afs_indexFlags[tdc->index] &
575 (IFDataMod | IFDirtyPages | IFAnyPages)) skip = 1;
576 if (tdc->refCount > 1) skip = 1;
578 #if defined(AFS_SUN5_ENV)
580 /* no vnode, so IFDirtyPages is spurious (we don't
581 * sweep dcaches on vnode recycling, so we can have
582 * DIRTYPAGES set even when all pages are gone). Just
584 * Hold vcache lock to prevent vnode from being
585 * created while we're clearing IFDirtyPages.
587 afs_indexFlags[tdc->index] &= ~(IFDirtyPages | IFAnyPages);
591 /* skip this guy and mark him as recently used */
592 afs_indexFlags[tdc->index] |= IFFlag;
593 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
594 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, 2,
595 ICL_TYPE_INT32, tdc->index,
597 ICL_HANDLE_OFFSET(tchunkoffset));
600 /* flush this dude from the data cache and reclaim;
601 * first, make sure no one will care that we damage
602 * it, by removing it from all hash tables. Then,
603 * melt it down for parts. Note that any concurrent
604 * (new possibility!) calls to GetDownD won't touch
605 * this guy because his reference count is > 0. */
606 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
607 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, 3,
608 ICL_TYPE_INT32, tdc->index,
610 ICL_HANDLE_OFFSET(tchunkoffset));
612 AFS_STATCNT(afs_gget);
614 afs_HashOutDCache(tdc);
615 if (tdc->f.chunkBytes != 0) {
618 *aneedSpace -= (tdc->f.chunkBytes + afs_fsfragsize) >> 10;
623 afs_DiscardDCache(tdc);
628 j = 1; /* we reclaimed at least one victim */
635 /* Phase is 0 and no one was found, so try phase 1 (ignore
636 * osi_Active flag) */
639 for (i = 0; i < afs_cacheFiles; i++)
640 /* turn off all flags */
641 afs_indexFlags[i] &= ~IFFlag;
645 /* found no one in phase 1, we're hosed */
646 if (victimPtr == 0) break;
648 } /* big while loop */
655 * Description: remove adc from any hash tables that would allow it to be located
656 * again by afs_FindDCache or afs_GetDCache.
658 * Parameters: adc -- pointer to dcache entry to remove from hash tables.
660 * Locks: Must have the afs_xdcache lock write-locked to call this function.
662 int afs_HashOutDCache(struct dcache *adc)
667 AFS_STATCNT(afs_glink);
669 /* we know this guy's in the LRUQ. We'll move dude into DCQ below */
671 /* if this guy is in the hash table, pull him out */
672 if (adc->f.fid.Fid.Volume != 0) {
673 /* remove entry from first hash chains */
674 i = DCHash(&adc->f.fid, adc->f.chunk);
675 us = afs_dchashTbl[i];
676 if (us == adc->index) {
677 /* first dude in the list */
678 afs_dchashTbl[i] = afs_dcnextTbl[adc->index];
681 /* somewhere on the chain */
682 while (us != NULLIDX) {
683 if (afs_dcnextTbl[us] == adc->index) {
684 /* found item pointing at the one to delete */
685 afs_dcnextTbl[us] = afs_dcnextTbl[adc->index];
688 us = afs_dcnextTbl[us];
690 if (us == NULLIDX) osi_Panic("dcache hc");
692 /* remove entry from *other* hash chain */
693 i = DVHash(&adc->f.fid);
694 us = afs_dvhashTbl[i];
695 if (us == adc->index) {
696 /* first dude in the list */
697 afs_dvhashTbl[i] = afs_dvnextTbl[adc->index];
700 /* somewhere on the chain */
701 while (us != NULLIDX) {
702 if (afs_dvnextTbl[us] == adc->index) {
703 /* found item pointing at the one to delete */
704 afs_dvnextTbl[us] = afs_dvnextTbl[adc->index];
707 us = afs_dvnextTbl[us];
709 if (us == NULLIDX) osi_Panic("dcache hv");
713 /* prevent entry from being found on a reboot (it is already out of
714 * the hash table, but after a crash, we just look at fid fields of
715 * stable (old) entries).
717 adc->f.fid.Fid.Volume = 0; /* invalid */
719 /* mark entry as modified */
720 adc->dflags |= DFEntryMod;
724 } /*afs_HashOutDCache */
731 * Flush the given dcache entry, pulling it from hash chains
732 * and truncating the associated cache file.
735 * adc: Ptr to dcache entry to flush.
738 * This routine must be called with the afs_xdcache lock held
742 void afs_FlushDCache(register struct dcache *adc)
744 AFS_STATCNT(afs_FlushDCache);
746 * Bump the number of cache files flushed.
748 afs_stats_cmperf.cacheFlushes++;
750 /* remove from all hash tables */
751 afs_HashOutDCache(adc);
753 /* Free its space; special case null operation, since truncate operation
754 * in UFS is slow even in this case, and this allows us to pre-truncate
755 * these files at more convenient times with fewer locks set
756 * (see afs_GetDownD).
758 if (adc->f.chunkBytes != 0) {
759 afs_DiscardDCache(adc);
760 afs_MaybeWakeupTruncateDaemon();
765 if (afs_WaitForCacheDrain) {
766 if (afs_blocksUsed <=
767 (CM_CACHESIZEDRAINEDPCT*afs_cacheBlocks)/100) {
768 afs_WaitForCacheDrain = 0;
769 afs_osi_Wakeup(&afs_WaitForCacheDrain);
772 } /*afs_FlushDCache*/
778 * Description: put a dcache entry on the free dcache entry list.
780 * Parameters: adc -- dcache entry to free
782 * Environment: called with afs_xdcache lock write-locked.
784 static void afs_FreeDCache(register struct dcache *adc)
786 /* Thread on free list, update free list count and mark entry as
787 * freed in its indexFlags element. Also, ensure DCache entry gets
788 * written out (set DFEntryMod).
791 afs_dvnextTbl[adc->index] = afs_freeDCList;
792 afs_freeDCList = adc->index;
794 afs_indexFlags[adc->index] |= IFFree;
795 adc->dflags |= DFEntryMod;
797 if (afs_WaitForCacheDrain) {
798 if ((afs_blocksUsed - afs_blocksDiscarded) <=
799 (CM_CACHESIZEDRAINEDPCT*afs_cacheBlocks)/100) {
800 afs_WaitForCacheDrain = 0;
801 afs_osi_Wakeup(&afs_WaitForCacheDrain);
810 * Discard the cache element by moving it to the discardDCList.
811 * This puts the cache element into a quasi-freed state, where
812 * the space may be reused, but the file has not been truncated.
814 * Major Assumptions Here:
815 * Assumes that frag size is an integral power of two, less one,
816 * and that this is a two's complement machine. I don't
817 * know of any filesystems which violate this assumption...
820 * adc : Ptr to dcache entry.
823 * Must be called with afs_xdcache write-locked.
826 static void afs_DiscardDCache(register struct dcache *adc)
828 register afs_int32 size;
830 AFS_STATCNT(afs_DiscardDCache);
832 osi_Assert(adc->refCount == 1);
834 size = ((adc->f.chunkBytes + afs_fsfragsize)^afs_fsfragsize)>>10;/* round up */
835 afs_blocksDiscarded += size;
836 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
838 afs_dvnextTbl[adc->index] = afs_discardDCList;
839 afs_discardDCList = adc->index;
840 afs_discardDCCount++;
842 adc->f.fid.Fid.Volume = 0;
843 adc->dflags |= DFEntryMod;
844 afs_indexFlags[adc->index] |= IFDiscarded;
846 if (afs_WaitForCacheDrain) {
847 if ((afs_blocksUsed - afs_blocksDiscarded) <=
848 (CM_CACHESIZEDRAINEDPCT*afs_cacheBlocks)/100) {
849 afs_WaitForCacheDrain = 0;
850 afs_osi_Wakeup(&afs_WaitForCacheDrain);
854 } /*afs_DiscardDCache*/
857 * afs_FreeDiscardedDCache
860 * Free the next element on the list of discarded cache elements.
862 static void afs_FreeDiscardedDCache(void)
864 register struct dcache *tdc;
865 register struct osi_file *tfile;
866 register afs_int32 size;
868 AFS_STATCNT(afs_FreeDiscardedDCache);
870 MObtainWriteLock(&afs_xdcache,510);
871 if (!afs_blocksDiscarded) {
872 MReleaseWriteLock(&afs_xdcache);
877 * Get an entry from the list of discarded cache elements
879 tdc = afs_GetDSlot(afs_discardDCList, 0);
880 osi_Assert(tdc->refCount == 1);
881 ReleaseReadLock(&tdc->tlock);
883 afs_discardDCList = afs_dvnextTbl[tdc->index];
884 afs_dvnextTbl[tdc->index] = NULLIDX;
885 afs_discardDCCount--;
886 size = ((tdc->f.chunkBytes + afs_fsfragsize)^afs_fsfragsize)>>10;/* round up */
887 afs_blocksDiscarded -= size;
888 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
889 /* We can lock because we just took it off the free list */
890 ObtainWriteLock(&tdc->lock, 626);
891 MReleaseWriteLock(&afs_xdcache);
894 * Truncate the element to reclaim its space
896 tfile = afs_CFileOpen(tdc->f.inode);
897 afs_CFileTruncate(tfile, 0);
898 afs_CFileClose(tfile);
899 afs_AdjustSize(tdc, 0);
902 * Free the element we just truncated
904 MObtainWriteLock(&afs_xdcache,511);
905 afs_indexFlags[tdc->index] &= ~IFDiscarded;
907 ReleaseWriteLock(&tdc->lock);
909 MReleaseWriteLock(&afs_xdcache);
913 * afs_MaybeFreeDiscardedDCache
916 * Free as many entries from the list of discarded cache elements
917 * as we need to get the free space down below CM_WAITFORDRAINPCT (98%).
922 int afs_MaybeFreeDiscardedDCache(void)
925 AFS_STATCNT(afs_MaybeFreeDiscardedDCache);
927 while (afs_blocksDiscarded &&
928 (afs_blocksUsed > (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100)) {
929 afs_FreeDiscardedDCache();
938 * Try to free up a certain number of disk slots.
941 * anumber : Targeted number of disk slots to free up.
944 * Must be called with afs_xdcache write-locked.
946 static void afs_GetDownDSlot(int anumber)
948 struct afs_q *tq, *nq;
953 AFS_STATCNT(afs_GetDownDSlot);
954 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
955 osi_Panic("diskless getdowndslot");
957 if (CheckLock(&afs_xdcache) != -1)
958 osi_Panic("getdowndslot nolock");
960 /* decrement anumber first for all dudes in free list */
961 for(tdc = afs_freeDSList; tdc; tdc = (struct dcache *)tdc->lruq.next)
964 return; /* enough already free */
966 for(cnt=0, tq = afs_DLRU.prev; tq != &afs_DLRU && anumber > 0;
968 tdc = (struct dcache *) tq; /* q is first elt in dcache entry */
969 nq = QPrev(tq); /* in case we remove it */
970 if (tdc->refCount == 0) {
971 if ((ix=tdc->index) == NULLIDX) osi_Panic("getdowndslot");
972 /* pull the entry out of the lruq and put it on the free list */
975 /* write-through if modified */
976 if (tdc->dflags & DFEntryMod) {
977 #if defined(AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
979 * ask proxy to do this for us - we don't have the stack space
981 while (tdc->dflags & DFEntryMod) {
984 s = SPLOCK(afs_sgibklock);
985 if (afs_sgibklist == NULL) {
986 /* if slot is free, grab it. */
988 SV_SIGNAL(&afs_sgibksync);
990 /* wait for daemon to (start, then) finish. */
991 SP_WAIT(afs_sgibklock, s, &afs_sgibkwait, PINOD);
995 tdc->dflags &= ~DFEntryMod;
996 afs_WriteDCache(tdc, 1);
1003 struct osi_file * f = (struct osi_file *)tdc->ihint;
1011 /* finally put the entry in the free list */
1012 afs_indexTable[ix] = NULL;
1013 afs_indexFlags[ix] &= ~IFEverUsed;
1014 tdc->index = NULLIDX;
1015 tdc->lruq.next = (struct afs_q *) afs_freeDSList;
1016 afs_freeDSList = tdc;
1020 } /*afs_GetDownDSlot*/
1027 * Increment the reference count on a disk cache entry,
1028 * which already has a non-zero refcount. In order to
1029 * increment the refcount of a zero-reference entry, you
1030 * have to hold afs_xdcache.
1033 * adc : Pointer to the dcache entry to increment.
1036 * Nothing interesting.
1038 int afs_RefDCache(struct dcache *adc)
1040 ObtainWriteLock(&adc->tlock, 627);
1041 if (adc->refCount < 0)
1042 osi_Panic("RefDCache: negative refcount");
1044 ReleaseWriteLock(&adc->tlock);
1053 * Decrement the reference count on a disk cache entry.
1056 * ad : Ptr to the dcache entry to decrement.
1059 * Nothing interesting.
1061 int afs_PutDCache(register struct dcache *adc)
1063 AFS_STATCNT(afs_PutDCache);
1064 ObtainWriteLock(&adc->tlock, 276);
1065 if (adc->refCount <= 0)
1066 osi_Panic("putdcache");
1068 ReleaseWriteLock(&adc->tlock);
1077 * Try to discard all data associated with this file from the
1081 * avc : Pointer to the cache info for the file.
1084 * Both pvnLock and lock are write held.
1086 void afs_TryToSmush(register struct vcache *avc, struct AFS_UCRED *acred,
1089 register struct dcache *tdc;
1092 AFS_STATCNT(afs_TryToSmush);
1093 afs_Trace2(afs_iclSetp, CM_TRACE_TRYTOSMUSH, ICL_TYPE_POINTER, avc,
1094 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
1095 sync = 1; /* XX Temp testing XX*/
1097 #if defined(AFS_SUN5_ENV)
1098 ObtainWriteLock(&avc->vlock, 573);
1099 avc->activeV++; /* block new getpages */
1100 ReleaseWriteLock(&avc->vlock);
1103 /* Flush VM pages */
1104 osi_VM_TryToSmush(avc, acred, sync);
1107 * Get the hash chain containing all dce's for this fid
1109 i = DVHash(&avc->fid);
1110 MObtainWriteLock(&afs_xdcache,277);
1111 for(index = afs_dvhashTbl[i]; index != NULLIDX; index=i) {
1112 i = afs_dvnextTbl[index]; /* next pointer this hash table */
1113 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1114 int releaseTlock = 1;
1115 tdc = afs_GetDSlot(index, NULL);
1116 if (!FidCmp(&tdc->f.fid, &avc->fid)) {
1118 if ((afs_indexFlags[index] & IFDataMod) == 0 &&
1119 tdc->refCount == 1) {
1120 ReleaseReadLock(&tdc->tlock);
1122 afs_FlushDCache(tdc);
1125 afs_indexTable[index] = 0;
1127 if (releaseTlock) ReleaseReadLock(&tdc->tlock);
1131 #if defined(AFS_SUN5_ENV)
1132 ObtainWriteLock(&avc->vlock, 545);
1133 if (--avc->activeV == 0 && (avc->vstates & VRevokeWait)) {
1134 avc->vstates &= ~VRevokeWait;
1135 afs_osi_Wakeup((char *)&avc->vstates);
1137 ReleaseWriteLock(&avc->vlock);
1139 MReleaseWriteLock(&afs_xdcache);
1141 * It's treated like a callback so that when we do lookups we'll invalidate the unique bit if any
1142 * trytoSmush occured during the lookup call
1151 * Given the cached info for a file and a byte offset into the
1152 * file, make sure the dcache entry for that file and containing
1153 * the given byte is available, returning it to our caller.
1156 * avc : Pointer to the (held) vcache entry to look in.
1157 * abyte : Which byte we want to get to.
1160 * Pointer to the dcache entry covering the file & desired byte,
1161 * or NULL if not found.
1164 * The vcache entry is held upon entry.
1167 struct dcache *afs_FindDCache(register struct vcache *avc, afs_size_t abyte)
1170 register afs_int32 i, index;
1171 register struct dcache *tdc;
1173 AFS_STATCNT(afs_FindDCache);
1174 chunk = AFS_CHUNK(abyte);
1177 * Hash on the [fid, chunk] and get the corresponding dcache index
1178 * after write-locking the dcache.
1180 i = DCHash(&avc->fid, chunk);
1181 MObtainWriteLock(&afs_xdcache,278);
1182 for(index = afs_dchashTbl[i]; index != NULLIDX;) {
1183 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1184 tdc = afs_GetDSlot(index, NULL);
1185 ReleaseReadLock(&tdc->tlock);
1186 if (!FidCmp(&tdc->f.fid, &avc->fid) && chunk == tdc->f.chunk) {
1187 break; /* leaving refCount high for caller */
1191 index = afs_dcnextTbl[index];
1193 MReleaseWriteLock(&afs_xdcache);
1194 if (index != NULLIDX) {
1195 hset(afs_indexTimes[tdc->index], afs_indexCounter);
1196 hadd32(afs_indexCounter, 1);
1202 } /*afs_FindDCache*/
1206 * afs_UFSCacheStoreProc
1209 * Called upon store.
1212 * acall : Ptr to the Rx call structure involved.
1213 * afile : Ptr to the related file descriptor.
1214 * alen : Size of the file in bytes.
1215 * avc : Ptr to the vcache entry.
1216 * shouldWake : is it "safe" to return early from close() ?
1217 * abytesToXferP : Set to the number of bytes to xfer.
1218 * NOTE: This parameter is only used if AFS_NOSTATS
1220 * abytesXferredP : Set to the number of bytes actually xferred.
1221 * NOTE: This parameter is only used if AFS_NOSTATS
1225 * Nothing interesting.
1227 static int afs_UFSCacheStoreProc(register struct rx_call *acall,
1228 struct osi_file *afile, register afs_int32 alen, struct vcache *avc,
1229 int *shouldWake, afs_size_t *abytesToXferP, afs_size_t *abytesXferredP)
1231 afs_int32 code, got;
1232 register char *tbuffer;
1235 AFS_STATCNT(UFS_CacheStoreProc);
1239 * In this case, alen is *always* the amount of data we'll be trying
1242 (*abytesToXferP) = alen;
1243 (*abytesXferredP) = 0;
1244 #endif /* AFS_NOSTATS */
1246 afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
1247 ICL_TYPE_FID, &(avc->fid),
1248 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1249 ICL_TYPE_INT32, alen);
1250 tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1252 tlen = (alen > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : alen);
1253 got = afs_osi_Read(afile, -1, tbuffer, tlen);
1255 #if defined(KERNEL_HAVE_UERROR)
1256 || (got != tlen && getuerror())
1259 osi_FreeLargeSpace(tbuffer);
1262 afs_Trace2(afs_iclSetp, CM_TRACE_STOREPROC2,
1263 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(*tbuffer),
1264 ICL_TYPE_INT32, got);
1266 code = rx_Write(acall, tbuffer, got); /* writing 0 bytes will
1267 * push a short packet. Is that really what we want, just because the
1268 * data didn't come back from the disk yet? Let's try it and see. */
1271 (*abytesXferredP) += code;
1272 #endif /* AFS_NOSTATS */
1274 osi_FreeLargeSpace(tbuffer);
1279 * If file has been locked on server, we can allow the store
1282 if (shouldWake && *shouldWake && (rx_GetRemoteStatus(acall) & 1)) {
1283 *shouldWake = 0; /* only do this once */
1287 afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
1288 ICL_TYPE_FID, &(avc->fid),
1289 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
1290 ICL_TYPE_INT32, alen);
1291 osi_FreeLargeSpace(tbuffer);
1294 } /* afs_UFSCacheStoreProc*/
1298 * afs_UFSCacheFetchProc
1301 * Routine called on fetch; also tells people waiting for data
1302 * that more has arrived.
1305 * acall : Ptr to the Rx call structure.
1306 * afile : File descriptor for the cache file.
1307 * abase : Base offset to fetch.
1308 * adc : Ptr to the dcache entry for the file, write-locked.
1309 * avc : Ptr to the vcache entry for the file.
1310 * abytesToXferP : Set to the number of bytes to xfer.
1311 * NOTE: This parameter is only used if AFS_NOSTATS
1313 * abytesXferredP : Set to the number of bytes actually xferred.
1314 * NOTE: This parameter is only used if AFS_NOSTATS
1318 * Nothing interesting.
1321 static int afs_UFSCacheFetchProc(register struct rx_call *acall,
1322 struct osi_file *afile, afs_size_t abase, struct dcache *adc,
1323 struct vcache *avc, afs_size_t *abytesToXferP,
1324 afs_size_t *abytesXferredP, afs_int32 lengthFound)
1327 register afs_int32 code;
1328 register char *tbuffer;
1332 AFS_STATCNT(UFS_CacheFetchProc);
1333 osi_Assert(WriteLocked(&adc->lock));
1334 afile->offset = 0; /* Each time start from the beginning */
1335 length = lengthFound;
1337 (*abytesToXferP) = 0;
1338 (*abytesXferredP) = 0;
1339 #endif /* AFS_NOSTATS */
1340 tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1341 adc->validPos = abase;
1345 code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
1347 length = ntohl(length);
1348 if (code != sizeof(afs_int32)) {
1349 osi_FreeLargeSpace(tbuffer);
1350 code = rx_Error(acall);
1351 return (code?code:-1); /* try to return code, not -1 */
1355 * The fetch protocol is extended for the AFS/DFS translator
1356 * to allow multiple blocks of data, each with its own length,
1357 * to be returned. As long as the top bit is set, there are more
1360 * We do not do this for AFS file servers because they sometimes
1361 * return large negative numbers as the transfer size.
1363 if (avc->states & CForeign) {
1364 moredata = length & 0x80000000;
1365 length &= ~0x80000000;
1370 (*abytesToXferP) += length;
1371 #endif /* AFS_NOSTATS */
1372 while (length > 0) {
1373 tlen = (length > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : length);
1374 #ifdef RX_KERNEL_TRACE
1375 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP,
1376 ICL_TYPE_STRING, "before rx_Read");
1379 code = rx_Read(acall, tbuffer, tlen);
1381 #ifdef RX_KERNEL_TRACE
1382 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP,
1383 ICL_TYPE_STRING, "after rx_Read");
1386 (*abytesXferredP) += code;
1387 #endif /* AFS_NOSTATS */
1389 osi_FreeLargeSpace(tbuffer);
1390 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
1391 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
1392 ICL_TYPE_INT32, length);
1395 code = afs_osi_Write(afile, -1, tbuffer, tlen);
1397 osi_FreeLargeSpace(tbuffer);
1402 adc->validPos = abase;
1403 if (afs_osi_Wakeup(&adc->validPos) == 0)
1404 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE,
1405 ICL_TYPE_STRING, __FILE__,
1406 ICL_TYPE_INT32, __LINE__,
1407 ICL_TYPE_POINTER, adc,
1408 ICL_TYPE_INT32, adc->dflags);
1411 osi_FreeLargeSpace(tbuffer);
1414 } /* afs_UFSCacheFetchProc*/
1420 * This function is called to obtain a reference to data stored in
1421 * the disk cache, locating a chunk of data containing the desired
1422 * byte and returning a reference to the disk cache entry, with its
1423 * reference count incremented.
1427 * avc : Ptr to a vcache entry (unlocked)
1428 * abyte : Byte position in the file desired
1429 * areq : Request structure identifying the requesting user.
1430 * aflags : Settings as follows:
1432 * 2 : Return after creating entry.
1433 * 4 : called from afs_vnop_write.c
1434 * *alen contains length of data to be written.
1436 * aoffset : Set to the offset within the chunk where the resident
1438 * alen : Set to the number of bytes of data after the desired
1439 * byte (including the byte itself) which can be read
1443 * The vcache entry pointed to by avc is unlocked upon entry.
1447 struct AFSVolSync tsync;
1448 struct AFSFetchStatus OutStatus;
1449 struct AFSCallBack CallBack;
1453 * Update the vnode-to-dcache hint if we can get the vnode lock
1454 * right away. Assumes dcache entry is at least read-locked.
1456 void updateV2DC(int lockVc, struct vcache *v, struct dcache *d, int src)
1458 if (!lockVc || 0 == NBObtainWriteLock(&v->lock,src)) {
1459 if (hsame(v->m.DataVersion, d->f.versionNo) && v->callback) {
1461 v->quick.stamp = d->stamp = MakeStamp();
1462 v->quick.minLoc = AFS_CHUNKTOBASE(d->f.chunk);
1463 /* Don't think I need these next two lines forever */
1464 v->quick.len = d->f.chunkBytes;
1467 if (lockVc) ReleaseWriteLock(&v->lock);
1471 /* avc - Write-locked unless aflags & 1 */
1472 struct dcache *afs_GetDCache(register struct vcache *avc, afs_size_t abyte,
1473 register struct vrequest *areq, afs_size_t *aoffset, afs_size_t *alen,
1476 register afs_int32 i, code, code1=0, shortcut;
1477 #if defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
1478 register afs_int32 adjustsize = 0;
1484 afs_size_t maxGoodLength; /* amount of good data at server */
1485 struct rx_call *tcall;
1486 afs_size_t Position = 0;
1487 #ifdef AFS_64BIT_CLIENT
1489 afs_size_t lengthFound; /* as returned from server */
1490 #endif /* AFS_64BIT_CLIENT */
1491 afs_int32 size, tlen; /* size of segment to transfer */
1492 struct tlocal1 *tsmall = 0;
1493 register struct dcache *tdc;
1494 register struct osi_file *file;
1495 register struct conn *tc;
1497 struct server *newCallback;
1498 char setNewCallback;
1499 char setVcacheStatus;
1500 char doVcacheUpdate;
1502 int doAdjustSize = 0;
1503 int doReallyAdjustSize = 0;
1504 int overWriteWholeChunk = 0;
1508 struct afs_stats_xferData *xferP; /* Ptr to this op's xfer struct */
1509 osi_timeval_t xferStartTime, /*FS xfer start time*/
1510 xferStopTime; /*FS xfer stop time*/
1511 afs_size_t bytesToXfer; /* # bytes to xfer*/
1512 afs_size_t bytesXferred; /* # bytes actually xferred*/
1513 struct afs_stats_AccessInfo *accP; /*Ptr to access record in stats*/
1514 int fromReplica; /*Are we reading from a replica?*/
1515 int numFetchLoops; /*# times around the fetch/analyze loop*/
1516 #endif /* AFS_NOSTATS */
1518 AFS_STATCNT(afs_GetDCache);
1523 setLocks = aflags & 1;
1526 * Determine the chunk number and offset within the chunk corresponding
1527 * to the desired byte.
1529 if (avc->fid.Fid.Vnode & 1) { /* if (vType(avc) == VDIR) */
1533 chunk = AFS_CHUNK(abyte);
1536 /* come back to here if we waited for the cache to drain. */
1539 setNewCallback = setVcacheStatus = 0;
1543 ObtainWriteLock(&avc->lock, 616);
1545 ObtainReadLock(&avc->lock);
1550 * avc->lock(R) if setLocks && !slowPass
1551 * avc->lock(W) if !setLocks || slowPass
1556 /* check hints first! (might could use bcmp or some such...) */
1557 if ((tdc = avc->h1.dchint)) {
1561 * The locking order between afs_xdcache and dcache lock matters.
1562 * The hint dcache entry could be anywhere, even on the free list.
1563 * Locking afs_xdcache ensures that noone is trying to pull dcache
1564 * entries from the free list, and thereby assuming them to be not
1565 * referenced and not locked.
1567 MObtainReadLock(&afs_xdcache);
1568 dcLocked = (0 == NBObtainSharedLock(&tdc->lock, 601));
1571 (tdc->index != NULLIDX) && !FidCmp(&tdc->f.fid, &avc->fid) &&
1572 chunk == tdc->f.chunk &&
1573 !(afs_indexFlags[tdc->index] & (IFFree|IFDiscarded))) {
1574 /* got the right one. It might not be the right version, and it
1575 * might be fetching, but it's the right dcache entry.
1577 /* All this code should be integrated better with what follows:
1578 * I can save a good bit more time under a write lock if I do..
1580 ObtainWriteLock(&tdc->tlock, 603);
1582 ReleaseWriteLock(&tdc->tlock);
1584 MReleaseReadLock(&afs_xdcache);
1587 if (hsame(tdc->f.versionNo, avc->m.DataVersion) &&
1588 !(tdc->dflags & DFFetching)) {
1590 afs_stats_cmperf.dcacheHits++;
1591 MObtainWriteLock(&afs_xdcache, 559);
1592 QRemove(&tdc->lruq);
1593 QAdd(&afs_DLRU, &tdc->lruq);
1594 MReleaseWriteLock(&afs_xdcache);
1597 * avc->lock(R) if setLocks && !slowPass
1598 * avc->lock(W) if !setLocks || slowPass
1604 if (dcLocked) ReleaseSharedLock(&tdc->lock);
1605 MReleaseReadLock(&afs_xdcache);
1613 * avc->lock(R) if setLocks && !slowPass
1614 * avc->lock(W) if !setLocks || slowPass
1615 * tdc->lock(S) if tdc
1618 if (!tdc) { /* If the hint wasn't the right dcache entry */
1620 * Hash on the [fid, chunk] and get the corresponding dcache index
1621 * after write-locking the dcache.
1626 * avc->lock(R) if setLocks && !slowPass
1627 * avc->lock(W) if !setLocks || slowPass
1630 i = DCHash(&avc->fid, chunk);
1631 /* check to make sure our space is fine */
1632 afs_MaybeWakeupTruncateDaemon();
1634 MObtainWriteLock(&afs_xdcache,280);
1636 for (index = afs_dchashTbl[i]; index != NULLIDX; ) {
1637 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1638 tdc = afs_GetDSlot(index, NULL);
1639 ReleaseReadLock(&tdc->tlock);
1642 * avc->lock(R) if setLocks && !slowPass
1643 * avc->lock(W) if !setLocks || slowPass
1646 if (!FidCmp(&tdc->f.fid, &avc->fid) && chunk == tdc->f.chunk) {
1647 /* Move it up in the beginning of the list */
1648 if (afs_dchashTbl[i] != index) {
1649 afs_dcnextTbl[us] = afs_dcnextTbl[index];
1650 afs_dcnextTbl[index] = afs_dchashTbl[i];
1651 afs_dchashTbl[i] = index;
1653 MReleaseWriteLock(&afs_xdcache);
1654 ObtainSharedLock(&tdc->lock, 606);
1655 break; /* leaving refCount high for caller */
1661 index = afs_dcnextTbl[index];
1665 * If we didn't find the entry, we'll create one.
1667 if (index == NULLIDX) {
1670 * avc->lock(R) if setLocks
1671 * avc->lock(W) if !setLocks
1674 afs_Trace2(afs_iclSetp, CM_TRACE_GETDCACHE1, ICL_TYPE_POINTER,
1675 avc, ICL_TYPE_INT32, chunk);
1677 /* Make sure there is a free dcache entry for us to use */
1678 if (afs_discardDCList == NULLIDX && afs_freeDCList == NULLIDX) {
1680 if (!setLocks) avc->states |= CDCLock;
1681 afs_GetDownD(5, (int*)0); /* just need slots */
1682 if (!setLocks) avc->states &= ~CDCLock;
1683 if (afs_discardDCList != NULLIDX || afs_freeDCList != NULLIDX)
1685 /* If we can't get space for 5 mins we give up and panic */
1686 if (++downDCount > 300)
1687 osi_Panic("getdcache");
1688 MReleaseWriteLock(&afs_xdcache);
1691 * avc->lock(R) if setLocks
1692 * avc->lock(W) if !setLocks
1694 afs_osi_Wait(1000, 0, 0);
1699 if (afs_discardDCList == NULLIDX ||
1700 ((aflags & 2) && afs_freeDCList != NULLIDX)) {
1702 afs_indexFlags[afs_freeDCList] &= ~IFFree;
1703 tdc = afs_GetDSlot(afs_freeDCList, 0);
1704 osi_Assert(tdc->refCount == 1);
1705 ReleaseReadLock(&tdc->tlock);
1706 ObtainWriteLock(&tdc->lock, 604);
1707 afs_freeDCList = afs_dvnextTbl[tdc->index];
1710 afs_indexFlags[afs_discardDCList] &= ~IFDiscarded;
1711 tdc = afs_GetDSlot(afs_discardDCList, 0);
1712 osi_Assert(tdc->refCount == 1);
1713 ReleaseReadLock(&tdc->tlock);
1714 ObtainWriteLock(&tdc->lock, 605);
1715 afs_discardDCList = afs_dvnextTbl[tdc->index];
1716 afs_discardDCCount--;
1717 size = ((tdc->f.chunkBytes + afs_fsfragsize)^afs_fsfragsize)>>10;
1718 afs_blocksDiscarded -= size;
1719 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1721 /* Truncate the chunk so zeroes get filled properly */
1722 file = afs_CFileOpen(tdc->f.inode);
1723 afs_CFileTruncate(file, 0);
1724 afs_CFileClose(file);
1725 afs_AdjustSize(tdc, 0);
1731 * avc->lock(R) if setLocks
1732 * avc->lock(W) if !setLocks
1738 * Fill in the newly-allocated dcache record.
1740 afs_indexFlags[tdc->index] &= ~(IFDirtyPages | IFAnyPages);
1741 tdc->f.fid = avc->fid;
1742 afs_indexUnique[tdc->index] = tdc->f.fid.Fid.Unique;
1743 hones(tdc->f.versionNo); /* invalid value */
1744 tdc->f.chunk = chunk;
1745 tdc->validPos = AFS_CHUNKTOBASE(chunk);
1747 if (tdc->lruq.prev == &tdc->lruq) osi_Panic("lruq 1");
1750 * Now add to the two hash chains - note that i is still set
1751 * from the above DCHash call.
1753 afs_dcnextTbl[tdc->index] = afs_dchashTbl[i];
1754 afs_dchashTbl[i] = tdc->index;
1755 i = DVHash(&avc->fid);
1756 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[i];
1757 afs_dvhashTbl[i] = tdc->index;
1758 tdc->dflags = DFEntryMod;
1761 afs_MaybeWakeupTruncateDaemon();
1762 MReleaseWriteLock(&afs_xdcache);
1763 ConvertWToSLock(&tdc->lock);
1765 } /* vcache->dcache hint failed */
1769 * avc->lock(R) if setLocks && !slowPass
1770 * avc->lock(W) if !setLocks || slowPass
1774 afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE2, ICL_TYPE_POINTER, avc,
1775 ICL_TYPE_POINTER, tdc,
1776 ICL_TYPE_INT32, hgetlo(tdc->f.versionNo),
1777 ICL_TYPE_INT32, hgetlo(avc->m.DataVersion));
1779 * Here we have the entry in tdc, with its refCount incremented.
1780 * Note: we don't use the S-lock on avc; it costs concurrency when
1781 * storing a file back to the server.
1785 * Not a newly created file so we need to check the file's length and
1786 * compare data versions since someone could have changed the data or we're
1787 * reading a file written elsewhere. We only want to bypass doing no-op
1788 * read rpcs on newly created files (dv of 0) since only then we guarantee
1789 * that this chunk's data hasn't been filled by another client.
1791 size = AFS_CHUNKSIZE(abyte);
1792 if (aflags & 4) /* called from write */
1794 else /* called from read */
1795 tlen = tdc->validPos - abyte;
1796 Position = AFS_CHUNKTOBASE(chunk);
1797 afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE3,
1798 ICL_TYPE_INT32, tlen,
1799 ICL_TYPE_INT32, aflags,
1800 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(abyte),
1801 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(Position));
1802 if ((aflags & 4) && (hiszero(avc->m.DataVersion)))
1804 if ((aflags & 4) && (abyte == Position) && (tlen >= size))
1805 overWriteWholeChunk = 1;
1806 if (doAdjustSize || overWriteWholeChunk) {
1807 #if defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
1809 #ifdef AFS_SGI64_ENV
1810 if (doAdjustSize) adjustsize = NBPP;
1811 #else /* AFS_SGI64_ENV */
1812 if (doAdjustSize) adjustsize = 8192;
1813 #endif /* AFS_SGI64_ENV */
1814 #else /* AFS_SGI_ENV */
1815 if (doAdjustSize) adjustsize = 4096;
1816 #endif /* AFS_SGI_ENV */
1817 if (AFS_CHUNKTOBASE(chunk)+adjustsize >= avc->m.Length &&
1818 #else /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
1819 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV)
1820 if ((doAdjustSize || (AFS_CHUNKTOBASE(chunk) >= avc->m.Length)) &&
1822 if (AFS_CHUNKTOBASE(chunk) >= avc->m.Length &&
1824 #endif /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
1825 !hsame(avc->m.DataVersion, tdc->f.versionNo))
1826 doReallyAdjustSize = 1;
1828 if (doReallyAdjustSize || overWriteWholeChunk) {
1829 /* no data in file to read at this position */
1830 UpgradeSToWLock(&tdc->lock, 607);
1832 file = afs_CFileOpen(tdc->f.inode);
1833 afs_CFileTruncate(file, 0);
1834 afs_CFileClose(file);
1835 afs_AdjustSize(tdc, 0);
1836 hset(tdc->f.versionNo, avc->m.DataVersion);
1837 tdc->dflags |= DFEntryMod;
1839 ConvertWToSLock(&tdc->lock);
1844 * We must read in the whole chunk if the version number doesn't
1848 /* don't need data, just a unique dcache entry */
1849 ObtainWriteLock(&afs_xdcache, 608);
1850 hset(afs_indexTimes[tdc->index], afs_indexCounter);
1851 hadd32(afs_indexCounter, 1);
1852 ReleaseWriteLock(&afs_xdcache);
1854 updateV2DC(setLocks, avc, tdc, 553);
1855 if (vType(avc) == VDIR)
1858 *aoffset = AFS_CHUNKOFFSET(abyte);
1859 if (tdc->validPos < abyte)
1860 *alen = (afs_size_t) 0;
1862 *alen = tdc->validPos - abyte;
1863 ReleaseSharedLock(&tdc->lock);
1866 ReleaseWriteLock(&avc->lock);
1868 ReleaseReadLock(&avc->lock);
1870 return tdc; /* check if we're done */
1875 * avc->lock(R) if setLocks && !slowPass
1876 * avc->lock(W) if !setLocks || slowPass
1879 osi_Assert((setLocks && !slowPass) || WriteLocked(&avc->lock));
1881 setNewCallback = setVcacheStatus = 0;
1885 * avc->lock(R) if setLocks && !slowPass
1886 * avc->lock(W) if !setLocks || slowPass
1889 if (!hsame(avc->m.DataVersion, tdc->f.versionNo) && !overWriteWholeChunk) {
1891 * Version number mismatch.
1893 UpgradeSToWLock(&tdc->lock, 609);
1896 * If data ever existed for this vnode, and this is a text object,
1897 * do some clearing. Now, you'd think you need only do the flush
1898 * when VTEXT is on, but VTEXT is turned off when the text object
1899 * is freed, while pages are left lying around in memory marked
1900 * with this vnode. If we would reactivate (create a new text
1901 * object from) this vnode, we could easily stumble upon some of
1902 * these old pages in pagein. So, we always flush these guys.
1903 * Sun has a wonderful lack of useful invariants in this system.
1905 * avc->flushDV is the data version # of the file at the last text
1906 * flush. Clearly, at least, we don't have to flush the file more
1907 * often than it changes
1909 if (hcmp(avc->flushDV, avc->m.DataVersion) < 0) {
1911 * By here, the cache entry is always write-locked. We can
1912 * deadlock if we call osi_Flush with the cache entry locked...
1913 * Unlock the dcache too.
1915 ReleaseWriteLock(&tdc->lock);
1916 if (setLocks && !slowPass)
1917 ReleaseReadLock(&avc->lock);
1919 ReleaseWriteLock(&avc->lock);
1923 * Call osi_FlushPages in open, read/write, and map, since it
1924 * is too hard here to figure out if we should lock the
1927 if (setLocks && !slowPass)
1928 ObtainReadLock(&avc->lock);
1930 ObtainWriteLock(&avc->lock, 66);
1931 ObtainWriteLock(&tdc->lock, 610);
1936 * avc->lock(R) if setLocks && !slowPass
1937 * avc->lock(W) if !setLocks || slowPass
1941 /* Watch for standard race condition around osi_FlushText */
1942 if (hsame(avc->m.DataVersion, tdc->f.versionNo)) {
1943 updateV2DC(setLocks, avc, tdc, 569); /* set hint */
1944 afs_stats_cmperf.dcacheHits++;
1945 ConvertWToSLock(&tdc->lock);
1949 /* Sleep here when cache needs to be drained. */
1950 if (setLocks && !slowPass &&
1951 (afs_blocksUsed > (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100)) {
1952 /* Make sure truncate daemon is running */
1953 afs_MaybeWakeupTruncateDaemon();
1954 ObtainWriteLock(&tdc->tlock, 614);
1955 tdc->refCount--; /* we'll re-obtain the dcache when we re-try. */
1956 ReleaseWriteLock(&tdc->tlock);
1957 ReleaseWriteLock(&tdc->lock);
1958 ReleaseReadLock(&avc->lock);
1959 while ((afs_blocksUsed-afs_blocksDiscarded) >
1960 (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
1961 afs_WaitForCacheDrain = 1;
1962 afs_osi_Sleep(&afs_WaitForCacheDrain);
1964 afs_MaybeFreeDiscardedDCache();
1965 /* need to check if someone else got the chunk first. */
1966 goto RetryGetDCache;
1969 /* Do not fetch data beyond truncPos. */
1970 maxGoodLength = avc->m.Length;
1971 if (avc->truncPos < maxGoodLength) maxGoodLength = avc->truncPos;
1972 Position = AFS_CHUNKBASE(abyte);
1973 if (vType(avc) == VDIR) {
1974 size = avc->m.Length;
1975 if (size > tdc->f.chunkBytes) {
1976 /* pre-reserve space for file */
1977 afs_AdjustSize(tdc, size);
1979 size = 999999999; /* max size for transfer */
1982 size = AFS_CHUNKSIZE(abyte); /* expected max size */
1983 /* don't read past end of good data on server */
1984 if (Position + size > maxGoodLength)
1985 size = maxGoodLength - Position;
1986 if (size < 0) size = 0; /* Handle random races */
1987 if (size > tdc->f.chunkBytes) {
1988 /* pre-reserve space for file */
1989 afs_AdjustSize(tdc, size); /* changes chunkBytes */
1990 /* max size for transfer still in size */
1993 if (afs_mariner && !tdc->f.chunk)
1994 afs_MarinerLog("fetch$Fetching", avc); /* , Position, size, afs_indexCounter );*/
1996 * Right now, we only have one tool, and it's a hammer. So, we
1997 * fetch the whole file.
1999 DZap(&tdc->f.inode); /* pages in cache may be old */
2001 if (file = tdc->ihint) {
2002 if (tdc->f.inode == file->inum )
2009 file = osi_UFSOpen(tdc->f.inode);
2014 file = afs_CFileOpen(tdc->f.inode);
2015 afs_RemoveVCB(&avc->fid);
2016 tdc->f.states |= DWriting;
2017 tdc->dflags |= DFFetching;
2018 tdc->validPos = Position; /* which is AFS_CHUNKBASE(abyte) */
2019 if (tdc->mflags & DFFetchReq) {
2020 tdc->mflags &= ~DFFetchReq;
2021 if (afs_osi_Wakeup(&tdc->validPos) == 0)
2022 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE,
2023 ICL_TYPE_STRING, __FILE__,
2024 ICL_TYPE_INT32, __LINE__,
2025 ICL_TYPE_POINTER, tdc,
2026 ICL_TYPE_INT32, tdc->dflags);
2028 tsmall = (struct tlocal1 *) osi_AllocLargeSpace(sizeof(struct tlocal1));
2029 setVcacheStatus = 0;
2032 * Remember if we are doing the reading from a replicated volume,
2033 * and how many times we've zipped around the fetch/analyze loop.
2035 fromReplica = (avc->states & CRO) ? 1 : 0;
2037 accP = &(afs_stats_cmfullperf.accessinf);
2039 (accP->replicatedRefs)++;
2041 (accP->unreplicatedRefs)++;
2042 #endif /* AFS_NOSTATS */
2043 /* this is a cache miss */
2044 afs_Trace4(afs_iclSetp, CM_TRACE_FETCHPROC, ICL_TYPE_POINTER, avc,
2045 ICL_TYPE_FID, &(avc->fid),
2046 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(Position),
2047 ICL_TYPE_INT32, size);
2049 if (size) afs_stats_cmperf.dcacheMisses++;
2052 * Dynamic root support: fetch data from local memory.
2054 if (afs_IsDynroot(avc)) {
2058 afs_GetDynroot(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2060 dynrootDir += Position;
2061 dynrootLen -= Position;
2062 if (size > dynrootLen)
2064 if (size < 0) size = 0;
2065 code = afs_CFileWrite(file, 0, dynrootDir, size);
2073 tdc->validPos = Position + size;
2074 afs_CFileTruncate(file, size); /* prune it */
2077 * Not a dynamic vnode: do the real fetch.
2082 * avc->lock(R) if setLocks && !slowPass
2083 * avc->lock(W) if !setLocks || slowPass
2087 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
2089 afs_int32 length_hi, length, bytes;
2093 (accP->numReplicasAccessed)++;
2095 #endif /* AFS_NOSTATS */
2096 if (!setLocks || slowPass) {
2097 avc->callback = tc->srvr->server;
2099 newCallback = tc->srvr->server;
2104 tcall = rx_NewCall(tc->id);
2107 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
2108 #ifdef AFS_64BIT_CLIENT
2109 length_hi = code = 0;
2110 if (!afs_serverHasNo64Bit(tc)) {
2113 code = StartRXAFS_FetchData64(tcall,
2114 (struct AFSFid *) &avc->fid.Fid,
2118 afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
2119 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
2121 bytes = rx_Read(tcall, (char *)&length_hi, sizeof(afs_int32));
2123 if (bytes == sizeof(afs_int32)) {
2124 length_hi = ntohl(length_hi);
2127 code = rx_Error(tcall);
2129 code1 = rx_EndCall(tcall, code);
2131 tcall = (struct rx_call *) 0;
2135 if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
2136 if (Position > 0x7FFFFFFF) {
2143 tcall = rx_NewCall(tc->id);
2144 code = StartRXAFS_FetchData(tcall,
2145 (struct AFSFid *) &avc->fid.Fid, pos, size);
2148 afs_serverSetNo64Bit(tc);
2152 bytes = rx_Read(tcall, (char *)&length, sizeof(afs_int32));
2154 if (bytes == sizeof(afs_int32)) {
2155 length = ntohl(length);
2157 code = rx_Error(tcall);
2160 FillInt64(lengthFound, length_hi, length);
2161 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
2162 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
2163 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(lengthFound));
2164 #else /* AFS_64BIT_CLIENT */
2166 code = StartRXAFS_FetchData(tcall,
2167 (struct AFSFid *) &avc->fid.Fid,
2172 bytes = rx_Read(tcall, (char *)&length, sizeof(afs_int32));
2174 if (bytes == sizeof(afs_int32)) {
2175 length = ntohl(length);
2177 code = rx_Error(tcall);
2180 #endif /* AFS_64BIT_CLIENT */
2184 xferP = &(afs_stats_cmfullperf.rpc.fsXferTimes[AFS_STATS_FS_XFERIDX_FETCHDATA]);
2185 osi_GetuTime(&xferStartTime);
2187 code = afs_CacheFetchProc(tcall, file,
2188 (afs_size_t) Position, tdc, avc,
2189 &bytesToXfer, &bytesXferred, length);
2191 osi_GetuTime(&xferStopTime);
2192 (xferP->numXfers)++;
2194 (xferP->numSuccesses)++;
2195 afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] += bytesXferred;
2196 (xferP->sumBytes) += (afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] >> 10);
2197 afs_stats_XferSumBytes[AFS_STATS_FS_XFERIDX_FETCHDATA] &= 0x3FF;
2198 if (bytesXferred < xferP->minBytes)
2199 xferP->minBytes = bytesXferred;
2200 if (bytesXferred > xferP->maxBytes)
2201 xferP->maxBytes = bytesXferred;
2204 * Tally the size of the object. Note: we tally the actual size,
2205 * NOT the number of bytes that made it out over the wire.
2207 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
2208 (xferP->count[0])++;
2210 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET1)
2211 (xferP->count[1])++;
2213 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET2)
2214 (xferP->count[2])++;
2216 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET3)
2217 (xferP->count[3])++;
2219 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET4)
2220 (xferP->count[4])++;
2222 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET5)
2223 (xferP->count[5])++;
2225 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET6)
2226 (xferP->count[6])++;
2228 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET7)
2229 (xferP->count[7])++;
2231 (xferP->count[8])++;
2233 afs_stats_GetDiff(elapsedTime, xferStartTime, xferStopTime);
2234 afs_stats_AddTo((xferP->sumTime), elapsedTime);
2235 afs_stats_SquareAddTo((xferP->sqrTime), elapsedTime);
2236 if (afs_stats_TimeLessThan(elapsedTime, (xferP->minTime))) {
2237 afs_stats_TimeAssign((xferP->minTime), elapsedTime);
2239 if (afs_stats_TimeGreaterThan(elapsedTime, (xferP->maxTime))) {
2240 afs_stats_TimeAssign((xferP->maxTime), elapsedTime);
2244 code = afs_CacheFetchProc(tcall, file, Position, tdc, avc, 0, 0, length);
2245 #endif /* AFS_NOSTATS */
2249 code = EndRXAFS_FetchData(tcall,
2258 code1 = rx_EndCall(tcall, code);
2264 if ( !code && code1 )
2268 /* callback could have been broken (or expired) in a race here,
2269 * but we return the data anyway. It's as good as we knew about
2270 * when we started. */
2272 * validPos is updated by CacheFetchProc, and can only be
2273 * modifed under a dcache write lock, which we've blocked out
2275 size = tdc->validPos - Position; /* actual segment size */
2276 if (size < 0) size = 0;
2277 afs_CFileTruncate(file, size); /* prune it */
2280 if (!setLocks || slowPass) {
2281 ObtainWriteLock(&afs_xcbhash, 453);
2282 afs_DequeueCallback(avc);
2283 avc->states &= ~(CStatd | CUnique);
2284 avc->callback = NULL;
2285 ReleaseWriteLock(&afs_xcbhash);
2286 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
2287 osi_dnlc_purgedp(avc);
2289 /* Something lost. Forget about performance, and go
2290 * back with a vcache write lock.
2292 afs_CFileTruncate(file, 0);
2293 afs_AdjustSize(tdc, 0);
2294 afs_CFileClose(file);
2295 osi_FreeLargeSpace(tsmall);
2297 ReleaseWriteLock(&tdc->lock);
2300 ReleaseReadLock(&avc->lock);
2302 goto RetryGetDCache;
2307 (afs_Analyze(tc, code, &avc->fid, areq,
2308 AFS_STATS_FS_RPCIDX_FETCHDATA,
2309 SHARED_LOCK, NULL));
2313 * avc->lock(R) if setLocks && !slowPass
2314 * avc->lock(W) if !setLocks || slowPass
2320 * In the case of replicated access, jot down info on the number of
2321 * attempts it took before we got through or gave up.
2324 if (numFetchLoops <= 1)
2325 (accP->refFirstReplicaOK)++;
2326 if (numFetchLoops > accP->maxReplicasPerRef)
2327 accP->maxReplicasPerRef = numFetchLoops;
2329 #endif /* AFS_NOSTATS */
2331 tdc->dflags &= ~DFFetching;
2332 if (afs_osi_Wakeup(&tdc->validPos) == 0)
2333 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE,
2334 ICL_TYPE_STRING, __FILE__,
2335 ICL_TYPE_INT32, __LINE__,
2336 ICL_TYPE_POINTER, tdc,
2337 ICL_TYPE_INT32, tdc->dflags);
2338 if (avc->execsOrWriters == 0) tdc->f.states &= ~DWriting;
2340 /* now, if code != 0, we have an error and should punt.
2341 * note that we have the vcache write lock, either because
2342 * !setLocks or slowPass.
2345 afs_CFileTruncate(file, 0);
2346 afs_AdjustSize(tdc, 0);
2347 afs_CFileClose(file);
2348 ZapDCE(tdc); /* sets DFEntryMod */
2349 if (vType(avc) == VDIR) {
2350 DZap(&tdc->f.inode);
2352 ReleaseWriteLock(&tdc->lock);
2354 ObtainWriteLock(&afs_xcbhash, 454);
2355 afs_DequeueCallback(avc);
2356 avc->states &= ~( CStatd | CUnique );
2357 ReleaseWriteLock(&afs_xcbhash);
2358 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
2359 osi_dnlc_purgedp(avc);
2362 * avc->lock(W); assert(!setLocks || slowPass)
2364 osi_Assert(!setLocks || slowPass);
2369 /* otherwise we copy in the just-fetched info */
2370 afs_CFileClose(file);
2371 afs_AdjustSize(tdc, size); /* new size */
2373 * Copy appropriate fields into vcache. Status is
2374 * copied later where we selectively acquire the
2375 * vcache write lock.
2378 afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2380 setVcacheStatus = 1;
2381 hset64(tdc->f.versionNo, tsmall->OutStatus.dataVersionHigh, tsmall->OutStatus.DataVersion);
2382 tdc->dflags |= DFEntryMod;
2383 afs_indexFlags[tdc->index] |= IFEverUsed;
2384 ConvertWToSLock(&tdc->lock);
2385 } /*Data version numbers don't match*/
2388 * Data version numbers match.
2390 afs_stats_cmperf.dcacheHits++;
2391 } /*Data version numbers match*/
2393 updateV2DC(setLocks, avc, tdc, 335); /* set hint */
2397 * avc->lock(R) if setLocks && !slowPass
2398 * avc->lock(W) if !setLocks || slowPass
2399 * tdc->lock(S) if tdc
2403 * See if this was a reference to a file in the local cell.
2405 if (afs_IsPrimaryCellNum(avc->fid.Cell))
2406 afs_stats_cmperf.dlocalAccesses++;
2408 afs_stats_cmperf.dremoteAccesses++;
2410 /* Fix up LRU info */
2413 MObtainWriteLock(&afs_xdcache, 602);
2414 hset(afs_indexTimes[tdc->index], afs_indexCounter);
2415 hadd32(afs_indexCounter, 1);
2416 MReleaseWriteLock(&afs_xdcache);
2418 /* return the data */
2419 if (vType(avc) == VDIR)
2422 *aoffset = AFS_CHUNKOFFSET(abyte);
2423 *alen = (tdc->f.chunkBytes - *aoffset);
2424 ReleaseSharedLock(&tdc->lock);
2429 * avc->lock(R) if setLocks && !slowPass
2430 * avc->lock(W) if !setLocks || slowPass
2433 /* Fix up the callback and status values in the vcache */
2435 if (setLocks && !slowPass) {
2438 * This is our dirty little secret to parallel fetches.
2439 * We don't write-lock the vcache while doing the fetch,
2440 * but potentially we'll need to update the vcache after
2441 * the fetch is done.
2443 * Drop the read lock and try to re-obtain the write
2444 * lock. If the vcache still has the same DV, it's
2445 * ok to go ahead and install the new data.
2447 afs_hyper_t currentDV, statusDV;
2449 hset(currentDV, avc->m.DataVersion);
2451 if (setNewCallback && avc->callback != newCallback)
2455 hset64(statusDV, tsmall->OutStatus.dataVersionHigh,
2456 tsmall->OutStatus.DataVersion);
2458 if (setVcacheStatus && avc->m.Length != tsmall->OutStatus.Length)
2460 if (setVcacheStatus && !hsame(currentDV, statusDV))
2464 ReleaseReadLock(&avc->lock);
2466 if (doVcacheUpdate) {
2467 ObtainWriteLock(&avc->lock, 615);
2468 if (!hsame(avc->m.DataVersion, currentDV)) {
2469 /* We lose. Someone will beat us to it. */
2471 ReleaseWriteLock(&avc->lock);
2476 /* With slow pass, we've already done all the updates */
2478 ReleaseWriteLock(&avc->lock);
2481 /* Check if we need to perform any last-minute fixes with a write-lock */
2482 if (!setLocks || doVcacheUpdate) {
2483 if (setNewCallback) avc->callback = newCallback;
2484 if (tsmall && setVcacheStatus) afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2485 if (setLocks) ReleaseWriteLock(&avc->lock);
2488 if (tsmall) osi_FreeLargeSpace(tsmall);
2495 * afs_WriteThroughDSlots
2498 * Sweep through the dcache slots and write out any modified
2499 * in-memory data back on to our caching store.
2505 * The afs_xdcache is write-locked through this whole affair.
2507 void afs_WriteThroughDSlots(void)
2509 register struct dcache *tdc;
2510 register afs_int32 i, touchedit=0;
2511 struct dcache **ents;
2512 int entmax, entcount;
2514 AFS_STATCNT(afs_WriteThroughDSlots);
2517 * Because of lock ordering, we can't grab dcache locks while
2518 * holding afs_xdcache. So we enter xdcache, get a reference
2519 * for every dcache entry, and exit xdcache.
2521 MObtainWriteLock(&afs_xdcache,283);
2522 entmax = afs_cacheFiles;
2523 ents = afs_osi_Alloc(entmax * sizeof(struct dcache *));
2525 for(i = 0; i < afs_cacheFiles; i++) {
2526 tdc = afs_indexTable[i];
2528 /* Grab tlock in case the existing refcount isn't zero */
2529 if (tdc && !(afs_indexFlags[i] & (IFFree | IFDiscarded))) {
2530 ObtainWriteLock(&tdc->tlock, 623);
2532 ReleaseWriteLock(&tdc->tlock);
2534 ents[entcount++] = tdc;
2537 MReleaseWriteLock(&afs_xdcache);
2540 * Now, for each dcache entry we found, check if it's dirty.
2541 * If so, get write-lock, get afs_xdcache, which protects
2542 * afs_cacheInodep, and flush it. Don't forget to put back
2545 for (i = 0; i < entcount; i++) {
2548 if (tdc->dflags & DFEntryMod) {
2551 wrLock = (0 == NBObtainWriteLock(&tdc->lock, 619));
2553 /* Now that we have the write lock, double-check */
2554 if (wrLock && (tdc->dflags & DFEntryMod)) {
2555 tdc->dflags &= ~DFEntryMod;
2556 MObtainWriteLock(&afs_xdcache, 620);
2557 afs_WriteDCache(tdc, 1);
2558 MReleaseWriteLock(&afs_xdcache);
2561 if (wrLock) ReleaseWriteLock(&tdc->lock);
2566 afs_osi_Free(ents, entmax * sizeof(struct dcache *));
2568 MObtainWriteLock(&afs_xdcache, 617);
2569 if (!touchedit && (cacheDiskType != AFS_FCACHE_TYPE_MEM)) {
2570 /* Touch the file to make sure that the mtime on the file is kept
2571 * up-to-date to avoid losing cached files on cold starts because
2572 * their mtime seems old...
2574 struct afs_fheader theader;
2576 theader.magic = AFS_FHMAGIC;
2577 theader.firstCSize = AFS_FIRSTCSIZE;
2578 theader.otherCSize = AFS_OTHERCSIZE;
2579 theader.version = AFS_CI_VERSION;
2580 afs_osi_Write(afs_cacheInodep, 0, &theader, sizeof(theader));
2582 MReleaseWriteLock(&afs_xdcache);
2589 * Return a pointer to an freshly initialized dcache entry using
2590 * a memory-based cache. The tlock will be read-locked.
2593 * aslot : Dcache slot to look at.
2594 * tmpdc : Ptr to dcache entry.
2597 * Must be called with afs_xdcache write-locked.
2600 struct dcache *afs_MemGetDSlot(register afs_int32 aslot, register struct dcache *tmpdc)
2602 register struct dcache *tdc;
2605 AFS_STATCNT(afs_MemGetDSlot);
2606 if (CheckLock(&afs_xdcache) != -1) osi_Panic("getdslot nolock");
2607 if (aslot < 0 || aslot >= afs_cacheFiles) osi_Panic("getdslot slot");
2608 tdc = afs_indexTable[aslot];
2610 QRemove(&tdc->lruq); /* move to queue head */
2611 QAdd(&afs_DLRU, &tdc->lruq);
2612 /* We're holding afs_xdcache, but get tlock in case refCount != 0 */
2613 ObtainWriteLock(&tdc->tlock, 624);
2615 ConvertWToRLock(&tdc->tlock);
2618 if (tmpdc == NULL) {
2619 if (!afs_freeDSList) afs_GetDownDSlot(4);
2620 if (!afs_freeDSList) {
2621 /* none free, making one is better than a panic */
2622 afs_stats_cmperf.dcacheXAllocs++; /* count in case we have a leak */
2623 tdc = (struct dcache *) afs_osi_Alloc(sizeof (struct dcache));
2624 #ifdef KERNEL_HAVE_PIN
2625 pin((char *)tdc, sizeof(struct dcache)); /* XXX */
2628 tdc = afs_freeDSList;
2629 afs_freeDSList = (struct dcache *) tdc->lruq.next;
2632 tdc->dflags = 0; /* up-to-date, not in free q */
2634 QAdd(&afs_DLRU, &tdc->lruq);
2635 if (tdc->lruq.prev == &tdc->lruq) osi_Panic("lruq 3");
2642 /* initialize entry */
2643 tdc->f.fid.Cell = 0;
2644 tdc->f.fid.Fid.Volume = 0;
2646 hones(tdc->f.versionNo);
2647 tdc->f.inode = aslot;
2648 tdc->dflags |= DFEntryMod;
2651 afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
2654 osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
2655 osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
2656 osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
2659 RWLOCK_INIT(&tdc->lock, "dcache lock");
2660 RWLOCK_INIT(&tdc->tlock, "dcache tlock");
2661 RWLOCK_INIT(&tdc->mflock, "dcache flock");
2662 ObtainReadLock(&tdc->tlock);
2665 afs_indexTable[aslot] = tdc;
2668 } /*afs_MemGetDSlot*/
2670 unsigned int last_error = 0, lasterrtime = 0;
2676 * Return a pointer to an freshly initialized dcache entry using
2677 * a UFS-based disk cache. The dcache tlock will be read-locked.
2680 * aslot : Dcache slot to look at.
2681 * tmpdc : Ptr to dcache entry.
2684 * afs_xdcache lock write-locked.
2686 struct dcache *afs_UFSGetDSlot(register afs_int32 aslot, register struct dcache *tmpdc)
2688 register afs_int32 code;
2689 register struct dcache *tdc;
2693 AFS_STATCNT(afs_UFSGetDSlot);
2694 if (CheckLock(&afs_xdcache) != -1) osi_Panic("getdslot nolock");
2695 if (aslot < 0 || aslot >= afs_cacheFiles) osi_Panic("getdslot slot");
2696 tdc = afs_indexTable[aslot];
2698 QRemove(&tdc->lruq); /* move to queue head */
2699 QAdd(&afs_DLRU, &tdc->lruq);
2700 /* Grab tlock in case refCount != 0 */
2701 ObtainWriteLock(&tdc->tlock, 625);
2703 ConvertWToRLock(&tdc->tlock);
2706 /* otherwise we should read it in from the cache file */
2708 * If we weren't passed an in-memory region to place the file info,
2709 * we have to allocate one.
2711 if (tmpdc == NULL) {
2712 if (!afs_freeDSList) afs_GetDownDSlot(4);
2713 if (!afs_freeDSList) {
2714 /* none free, making one is better than a panic */
2715 afs_stats_cmperf.dcacheXAllocs++; /* count in case we have a leak */
2716 tdc = (struct dcache *) afs_osi_Alloc(sizeof (struct dcache));
2717 #ifdef KERNEL_HAVE_PIN
2718 pin((char *)tdc, sizeof(struct dcache)); /* XXX */
2721 tdc = afs_freeDSList;
2722 afs_freeDSList = (struct dcache *) tdc->lruq.next;
2725 tdc->dflags = 0; /* up-to-date, not in free q */
2727 QAdd(&afs_DLRU, &tdc->lruq);
2728 if (tdc->lruq.prev == &tdc->lruq) osi_Panic("lruq 3");
2737 * Seek to the aslot'th entry and read it in.
2739 code = afs_osi_Read(afs_cacheInodep, sizeof(struct fcache) * aslot +
2740 sizeof(struct afs_fheader),
2741 (char *)(&tdc->f), sizeof(struct fcache));
2743 if (code != sizeof(struct fcache))
2745 if (!afs_CellNumValid(tdc->f.fid.Cell))
2749 tdc->f.fid.Cell = 0;
2750 tdc->f.fid.Fid.Volume = 0;
2752 hones(tdc->f.versionNo);
2753 tdc->dflags |= DFEntryMod;
2754 #if defined(KERNEL_HAVE_UERROR)
2755 last_error = getuerror();
2757 lasterrtime = osi_Time();
2758 afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
2764 osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
2765 osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
2766 osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
2769 RWLOCK_INIT(&tdc->lock, "dcache lock");
2770 RWLOCK_INIT(&tdc->tlock, "dcache tlock");
2771 RWLOCK_INIT(&tdc->mflock, "dcache flock");
2772 ObtainReadLock(&tdc->tlock);
2775 * If we didn't read into a temporary dcache region, update the
2776 * slot pointer table.
2779 afs_indexTable[aslot] = tdc;
2782 } /*afs_UFSGetDSlot*/
2790 * write a particular dcache entry back to its home in the
2794 * adc : Pointer to the dcache entry to write.
2795 * atime : If true, set the modtime on the file to the current time.
2798 * Must be called with the afs_xdcache lock at least read-locked,
2799 * and dcache entry at least read-locked.
2800 * The reference count is not changed.
2803 int afs_WriteDCache(register struct dcache *adc, int atime)
2805 register afs_int32 code;
2807 if (cacheDiskType == AFS_FCACHE_TYPE_MEM) return 0;
2808 AFS_STATCNT(afs_WriteDCache);
2810 adc->f.modTime = osi_Time();
2812 * Seek to the right dcache slot and write the in-memory image out to disk.
2814 afs_cellname_write();
2815 code = afs_osi_Write(afs_cacheInodep, sizeof(struct fcache) * adc->index +
2816 sizeof(struct afs_fheader),
2817 (char *)(&adc->f), sizeof(struct fcache));
2818 if (code != sizeof(struct fcache)) return EIO;
2828 * Wake up users of a particular file waiting for stores to take
2832 * avc : Ptr to related vcache entry.
2835 * Nothing interesting.
2838 int afs_wakeup(register struct vcache *avc)
2841 register struct brequest *tb;
2843 AFS_STATCNT(afs_wakeup);
2844 for (i = 0; i < NBRS; i++, tb++) {
2845 /* if request is valid and for this file, we've found it */
2846 if (tb->refCount > 0 && avc == tb->vnode) {
2849 * If CSafeStore is on, then we don't awaken the guy
2850 * waiting for the store until the whole store has finished.
2851 * Otherwise, we do it now. Note that if CSafeStore is on,
2852 * the BStore routine actually wakes up the user, instead
2854 * I think this is redundant now because this sort of thing
2855 * is already being handled by the higher-level code.
2857 if ((avc->states & CSafeStore) == 0) {
2859 tb->flags |= BUVALID;
2860 if (tb->flags & BUWAIT) {
2861 tb->flags &= ~BUWAIT;
2876 * Given a file name and inode, set up that file to be an
2877 * active member in the AFS cache. This also involves checking
2878 * the usability of its data.
2881 * afile : Name of the cache file to initialize.
2882 * ainode : Inode of the file.
2885 * This function is called only during initialization.
2888 int afs_InitCacheFile(char *afile, ino_t ainode)
2890 register afs_int32 code;
2891 #if defined(AFS_LINUX22_ENV)
2892 struct dentry *filevp;
2894 struct vnode *filevp;
2898 struct osi_file *tfile;
2899 struct osi_stat tstat;
2900 register struct dcache *tdc;
2902 AFS_STATCNT(afs_InitCacheFile);
2903 index = afs_stats_cmperf.cacheNumEntries;
2904 if (index >= afs_cacheFiles) return EINVAL;
2906 MObtainWriteLock(&afs_xdcache,282);
2907 tdc = afs_GetDSlot(index, NULL);
2908 ReleaseReadLock(&tdc->tlock);
2909 MReleaseWriteLock(&afs_xdcache);
2911 ObtainWriteLock(&tdc->lock, 621);
2912 MObtainWriteLock(&afs_xdcache, 622);
2914 code = gop_lookupname(afile,
2920 ReleaseWriteLock(&afs_xdcache);
2921 ReleaseWriteLock(&tdc->lock);
2926 * We have a VN_HOLD on filevp. Get the useful info out and
2927 * return. We make use of the fact that the cache is in the
2928 * UFS file system, and just record the inode number.
2930 #ifdef AFS_LINUX22_ENV
2931 tdc->f.inode = VTOI(filevp->d_inode)->i_number;
2934 tdc->f.inode = afs_vnodeToInumber(filevp);
2938 AFS_RELE((struct vnode *)filevp);
2940 #endif /* AFS_LINUX22_ENV */
2943 tdc->f.inode = ainode;
2946 if ((tdc->f.states & DWriting) ||
2947 tdc->f.fid.Fid.Volume == 0) fileIsBad = 1;
2948 tfile = osi_UFSOpen(tdc->f.inode);
2949 code = afs_osi_Stat(tfile, &tstat);
2950 if (code) osi_Panic("initcachefile stat");
2953 * If file size doesn't match the cache info file, it's probably bad.
2955 if (tdc->f.chunkBytes != tstat.size) fileIsBad = 1;
2956 tdc->f.chunkBytes = 0;
2959 * If file changed within T (120?) seconds of cache info file, it's
2960 * probably bad. In addition, if slot changed within last T seconds,
2961 * the cache info file may be incorrectly identified, and so slot
2964 if (cacheInfoModTime < tstat.mtime + 120) fileIsBad = 1;
2965 if (cacheInfoModTime < tdc->f.modTime + 120) fileIsBad = 1;
2966 /* In case write through is behind, make sure cache items entry is
2967 * at least as new as the chunk.
2969 if (tdc->f.modTime < tstat.mtime) fileIsBad = 1;
2971 tdc->f.fid.Fid.Volume = 0; /* not in the hash table */
2972 if (tstat.size != 0)
2973 osi_UFSTruncate(tfile, 0);
2974 /* put entry in free cache slot list */
2975 afs_dvnextTbl[tdc->index] = afs_freeDCList;
2976 afs_freeDCList = index;
2978 afs_indexFlags[index] |= IFFree;
2979 afs_indexUnique[index] = 0;
2983 * We must put this entry in the appropriate hash tables.
2984 * Note that i is still set from the above DCHash call
2986 code = DCHash(&tdc->f.fid, tdc->f.chunk);
2987 afs_dcnextTbl[tdc->index] = afs_dchashTbl[code];
2988 afs_dchashTbl[code] = tdc->index;
2989 code = DVHash(&tdc->f.fid);
2990 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[code];
2991 afs_dvhashTbl[code] = tdc->index;
2992 afs_AdjustSize(tdc, tstat.size); /* adjust to new size */
2994 /* has nontrivial amt of data */
2995 afs_indexFlags[index] |= IFEverUsed;
2996 afs_stats_cmperf.cacheFilesReused++;
2998 * Initialize index times to file's mod times; init indexCounter
3001 hset32(afs_indexTimes[index], tstat.atime);
3002 if (hgetlo(afs_indexCounter) < tstat.atime) {
3003 hset32(afs_indexCounter, tstat.atime);
3005 afs_indexUnique[index] = tdc->f.fid.Fid.Unique;
3006 } /*File is not bad*/
3008 osi_UFSClose(tfile);
3009 tdc->f.states &= ~DWriting;
3010 tdc->dflags &= ~DFEntryMod;
3011 /* don't set f.modTime; we're just cleaning up */
3012 afs_WriteDCache(tdc, 0);
3013 ReleaseWriteLock(&afs_xdcache);
3014 ReleaseWriteLock(&tdc->lock);
3016 afs_stats_cmperf.cacheNumEntries++;
3021 /*Max # of struct dcache's resident at any time*/
3023 * If 'dchint' is enabled then in-memory dcache min is increased because of
3032 * Initialize dcache related variables.
3034 void afs_dcacheInit(int afiles, int ablocks, int aDentries, int achunk,
3037 register struct dcache *tdp;
3041 afs_freeDCList = NULLIDX;
3042 afs_discardDCList = NULLIDX;
3043 afs_freeDCCount = 0;
3044 afs_freeDSList = NULL;
3045 hzero(afs_indexCounter);
3047 LOCK_INIT(&afs_xdcache, "afs_xdcache");
3053 if (achunk < 0 || achunk > 30)
3054 achunk = 13; /* Use default */
3055 AFS_SETCHUNKSIZE(achunk);
3061 if(aflags & AFSCALL_INIT_MEMCACHE) {
3063 * Use a memory cache instead of a disk cache
3065 afs_int64 cachebytes;
3067 cacheDiskType = AFS_FCACHE_TYPE_MEM;
3068 afs_cacheType = &afs_MemCacheOps;
3069 afiles = (afiles < aDentries) ? afiles : aDentries; /* min */
3070 ablocks = afiles * (AFS_FIRSTCSIZE/1024);
3071 /* ablocks is reported in 1K blocks */
3073 cachebytes = afiles;
3074 cachebytes *= AFS_FIRSTCSIZE;
3075 code = afs_InitMemCache(cachebytes, AFS_FIRSTCSIZE, aflags);
3077 printf("afsd: memory cache too large for available memory.\n");
3078 printf("afsd: AFS files cannot be accessed.\n\n");
3080 afiles = ablocks = 0;
3083 printf("Memory cache: Allocating %d dcache entries...", aDentries);
3085 cacheDiskType = AFS_FCACHE_TYPE_UFS;
3086 afs_cacheType = &afs_UfsCacheOps;
3089 if (aDentries > 512)
3090 afs_dhashsize = 2048;
3091 /* initialize hash tables */
3092 afs_dvhashTbl = (afs_int32 *) afs_osi_Alloc(afs_dhashsize * sizeof(afs_int32));
3093 afs_dchashTbl = (afs_int32 *) afs_osi_Alloc(afs_dhashsize * sizeof(afs_int32));
3094 for(i=0;i< afs_dhashsize;i++) {
3095 afs_dvhashTbl[i] = NULLIDX;
3096 afs_dchashTbl[i] = NULLIDX;
3098 afs_dvnextTbl = (afs_int32 *) afs_osi_Alloc(afiles * sizeof(afs_int32));
3099 afs_dcnextTbl = (afs_int32 *) afs_osi_Alloc(afiles * sizeof(afs_int32));
3100 for(i=0;i< afiles;i++) {
3101 afs_dvnextTbl[i] = NULLIDX;
3102 afs_dcnextTbl[i] = NULLIDX;
3105 /* Allocate and zero the pointer array to the dcache entries */
3106 afs_indexTable = (struct dcache **)
3107 afs_osi_Alloc(sizeof(struct dcache *) * afiles);
3108 memset((char *)afs_indexTable, 0, sizeof(struct dcache *) * afiles);
3109 afs_indexTimes = (afs_hyper_t *) afs_osi_Alloc(afiles * sizeof(afs_hyper_t));
3110 memset((char *)afs_indexTimes, 0, afiles * sizeof(afs_hyper_t));
3111 afs_indexUnique = (afs_int32 *) afs_osi_Alloc(afiles * sizeof(afs_uint32));
3112 memset((char *)afs_indexUnique, 0, afiles * sizeof(afs_uint32));
3113 afs_indexFlags = (u_char *) afs_osi_Alloc(afiles * sizeof(u_char));
3114 memset((char *)afs_indexFlags, 0, afiles * sizeof(char));
3116 /* Allocate and thread the struct dcache entries themselves */
3117 tdp = afs_Initial_freeDSList =
3118 (struct dcache *) afs_osi_Alloc(aDentries * sizeof(struct dcache));
3119 memset((char *)tdp, 0, aDentries * sizeof(struct dcache));
3120 #ifdef KERNEL_HAVE_PIN
3121 pin((char *)afs_indexTable, sizeof(struct dcache *) * afiles);/* XXX */
3122 pin((char *)afs_indexTimes, sizeof(afs_hyper_t) * afiles); /* XXX */
3123 pin((char *)afs_indexFlags, sizeof(char) * afiles); /* XXX */
3124 pin((char *)afs_indexUnique, sizeof(afs_int32) * afiles); /* XXX */
3125 pin((char *)tdp, aDentries * sizeof(struct dcache)); /* XXX */
3126 pin((char *)afs_dvhashTbl, sizeof(afs_int32) * afs_dhashsize); /* XXX */
3127 pin((char *)afs_dchashTbl, sizeof(afs_int32) * afs_dhashsize); /* XXX */
3128 pin((char *)afs_dcnextTbl, sizeof(afs_int32) * afiles); /* XXX */
3129 pin((char *)afs_dvnextTbl, sizeof(afs_int32) * afiles); /* XXX */
3132 afs_freeDSList = &tdp[0];
3133 for(i=0; i < aDentries-1; i++) {
3134 tdp[i].lruq.next = (struct afs_q *) (&tdp[i+1]);
3136 tdp[aDentries-1].lruq.next = (struct afs_q *) 0;
3138 afs_stats_cmperf.cacheBlocksOrig = afs_stats_cmperf.cacheBlocksTotal = afs_cacheBlocks = ablocks;
3139 afs_ComputeCacheParms(); /* compute parms based on cache size */
3141 afs_dcentries = aDentries;
3150 void shutdown_dcache(void)
3154 afs_osi_Free(afs_dvnextTbl, afs_cacheFiles * sizeof(afs_int32));
3155 afs_osi_Free(afs_dcnextTbl, afs_cacheFiles * sizeof(afs_int32));
3156 afs_osi_Free(afs_indexTable, afs_cacheFiles * sizeof(struct dcache *));
3157 afs_osi_Free(afs_indexTimes, afs_cacheFiles * sizeof(afs_hyper_t));
3158 afs_osi_Free(afs_indexUnique, afs_cacheFiles * sizeof(afs_uint32));
3159 afs_osi_Free(afs_indexFlags, afs_cacheFiles * sizeof(u_char));
3160 afs_osi_Free(afs_Initial_freeDSList, afs_dcentries * sizeof(struct dcache));
3161 #ifdef KERNEL_HAVE_PIN
3162 unpin((char *)afs_dcnextTbl, afs_cacheFiles * sizeof(afs_int32));
3163 unpin((char *)afs_dvnextTbl, afs_cacheFiles * sizeof(afs_int32));
3164 unpin((char *)afs_indexTable, afs_cacheFiles * sizeof(struct dcache *));
3165 unpin((char *)afs_indexTimes, afs_cacheFiles * sizeof(afs_hyper_t));
3166 unpin((char *)afs_indexUnique, afs_cacheFiles * sizeof(afs_uint32));
3167 unpin((u_char *)afs_indexFlags, afs_cacheFiles * sizeof(u_char));
3168 unpin(afs_Initial_freeDSList, afs_dcentries * sizeof(struct dcache));
3172 for(i=0;i< afs_dhashsize;i++) {
3173 afs_dvhashTbl[i] = NULLIDX;
3174 afs_dchashTbl[i] = NULLIDX;
3178 afs_blocksUsed = afs_dcentries = 0;
3179 hzero(afs_indexCounter);
3181 afs_freeDCCount = 0;
3182 afs_freeDCList = NULLIDX;
3183 afs_discardDCList = NULLIDX;
3184 afs_freeDSList = afs_Initial_freeDSList = 0;
3186 LOCK_INIT(&afs_xdcache, "afs_xdcache");