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"
17 #include "afs/sysincludes.h" /*Standard vendor system headers */
18 #include "afsincludes.h" /*AFS-based standard headers */
19 #include "afs/afs_stats.h" /* statistics */
20 #include "afs/afs_cbqueue.h"
21 #include "afs/afs_osidnlc.h"
25 /* Forward declarations. */
26 static void afs_GetDownD(int anumber, int *aneedSpace, afs_int32 buckethint);
27 static int afs_FreeDiscardedDCache(void);
28 static void afs_DiscardDCache(struct dcache *);
29 static void afs_FreeDCache(struct dcache *);
31 static afs_int32 afs_DCGetBucket(struct vcache *);
32 static void afs_DCAdjustSize(struct dcache *, afs_int32, afs_int32);
33 static void afs_DCMoveBucket(struct dcache *, afs_int32, afs_int32);
34 static void afs_DCSizeInit(void);
35 static afs_int32 afs_DCWhichBucket(afs_int32, afs_int32);
38 * --------------------- Exported definitions ---------------------
41 afs_int32 afs_blocksUsed_0; /*!< 1K blocks in cache - in theory is zero */
42 afs_int32 afs_blocksUsed_1; /*!< 1K blocks in cache */
43 afs_int32 afs_blocksUsed_2; /*!< 1K blocks in cache */
44 afs_int32 afs_pct1 = -1;
45 afs_int32 afs_pct2 = -1;
46 afs_uint32 afs_tpct1 = 0;
47 afs_uint32 afs_tpct2 = 0;
48 afs_uint32 splitdcache = 0;
50 afs_lock_t afs_xdcache; /*!< Lock: alloc new disk cache entries */
51 afs_int32 afs_freeDCList; /*!< Free list for disk cache entries */
52 afs_int32 afs_freeDCCount; /*!< Count of elts in freeDCList */
53 afs_int32 afs_discardDCList; /*!< Discarded disk cache entries */
54 afs_int32 afs_discardDCCount; /*!< Count of elts in discardDCList */
55 struct dcache *afs_freeDSList; /*!< Free list for disk slots */
56 struct dcache *afs_Initial_freeDSList; /*!< Initial list for above */
57 afs_dcache_id_t cacheInode; /*!< Inode for CacheItems file */
58 struct osi_file *afs_cacheInodep = 0; /*!< file for CacheItems inode */
59 struct afs_q afs_DLRU; /*!< dcache LRU */
60 afs_int32 afs_dhashsize = 1024;
61 afs_int32 *afs_dvhashTbl; /*!< Data cache hash table: hashed by FID + chunk number. */
62 afs_int32 *afs_dchashTbl; /*!< Data cache hash table: hashed by FID. */
63 afs_int32 *afs_dvnextTbl; /*!< Dcache hash table links */
64 afs_int32 *afs_dcnextTbl; /*!< Dcache hash table links */
65 struct dcache **afs_indexTable; /*!< Pointers to dcache entries */
66 afs_hyper_t *afs_indexTimes; /*!< Dcache entry Access times */
67 afs_int32 *afs_indexUnique; /*!< dcache entry Fid.Unique */
68 unsigned char *afs_indexFlags; /*!< (only one) Is there data there? */
69 afs_hyper_t afs_indexCounter; /*!< Fake time for marking index
71 afs_int32 afs_cacheFiles = 0; /*!< Size of afs_indexTable */
72 afs_int32 afs_cacheBlocks; /*!< 1K blocks in cache */
73 afs_int32 afs_cacheStats; /*!< Stat entries in cache */
74 afs_int32 afs_blocksUsed; /*!< Number of blocks in use */
75 afs_int32 afs_blocksDiscarded; /*!<Blocks freed but not truncated */
76 afs_int32 afs_fsfragsize = AFS_MIN_FRAGSIZE; /*!< Underlying Filesystem minimum unit
77 *of disk allocation usually 1K
78 *this value is (truefrag -1 ) to
79 *save a bunch of subtracts... */
80 #ifdef AFS_64BIT_CLIENT
81 #ifdef AFS_VM_RDWR_ENV
82 afs_size_t afs_vmMappingEnd; /* !< For large files (>= 2GB) the VM
83 * mapping an 32bit addressing machines
84 * can only be used below the 2 GB
85 * line. From this point upwards we
86 * must do direct I/O into the cache
87 * files. The value should be on a
89 #endif /* AFS_VM_RDWR_ENV */
90 #endif /* AFS_64BIT_CLIENT */
92 /* The following is used to ensure that new dcache's aren't obtained when
93 * the cache is nearly full.
95 int afs_WaitForCacheDrain = 0;
96 int afs_TruncateDaemonRunning = 0;
97 int afs_CacheTooFull = 0;
98 afs_uint32 afs_CacheTooFullCount = 0;
99 afs_uint32 afs_WaitForCacheDrainCount = 0;
101 afs_int32 afs_dcentries; /*!< In-memory dcache entries */
104 int dcacheDisabled = 0;
106 struct afs_cacheOps afs_UfsCacheOps = {
107 #ifndef HAVE_STRUCT_LABEL_SUPPORT
120 .truncate = osi_UFSTruncate,
121 .fread = afs_osi_Read,
122 .fwrite = afs_osi_Write,
123 .close = osi_UFSClose,
124 .vreadUIO = afs_UFSReadUIO,
125 .vwriteUIO = afs_UFSWriteUIO,
126 .GetDSlot = afs_UFSGetDSlot,
127 .GetVolSlot = afs_UFSGetVolSlot,
128 .HandleLink = afs_UFSHandleLink,
132 struct afs_cacheOps afs_MemCacheOps = {
133 #ifndef HAVE_STRUCT_LABEL_SUPPORT
135 afs_MemCacheTruncate,
145 .open = afs_MemCacheOpen,
146 .truncate = afs_MemCacheTruncate,
147 .fread = afs_MemReadBlk,
148 .fwrite = afs_MemWriteBlk,
149 .close = afs_MemCacheClose,
150 .vreadUIO = afs_MemReadUIO,
151 .vwriteUIO = afs_MemWriteUIO,
152 .GetDSlot = afs_MemGetDSlot,
153 .GetVolSlot = afs_MemGetVolSlot,
154 .HandleLink = afs_MemHandleLink,
158 int cacheDiskType; /*Type of backing disk for cache */
159 struct afs_cacheOps *afs_cacheType;
163 * The PFlush algorithm makes use of the fact that Fid.Unique is not used in
164 * below hash algorithms. Change it if need be so that flushing algorithm
165 * doesn't move things from one hash chain to another.
167 /*Vnode, Chunk -> Hash table index */
168 int DCHash(struct VenusFid *fid, afs_int32 chunk)
172 buf[0] = fid->Fid.Volume;
173 buf[1] = fid->Fid.Vnode;
175 return opr_jhash(buf, 3, 0) & (afs_dhashsize - 1);
177 /*Vnode -> Other hash table index */
178 int DVHash(struct VenusFid *fid)
180 return opr_jhash_int2(fid->Fid.Volume, fid->Fid.Vnode, 0) &
185 * Where is this vcache's entry associated dcache located/
186 * \param avc The vcache entry.
187 * \return Bucket index:
192 afs_DCGetBucket(struct vcache *avc)
197 /* This should be replaced with some sort of user configurable function */
198 if (avc->f.states & CRO) {
200 } else if (avc->f.states & CBackup) {
210 * Readjust a dcache's size.
212 * \param adc The dcache to be adjusted.
213 * \param oldSize Old size for the dcache.
214 * \param newSize The new size to be adjusted to.
218 afs_DCAdjustSize(struct dcache *adc, afs_int32 oldSize, afs_int32 newSize)
220 afs_int32 adjustSize = newSize - oldSize;
228 afs_blocksUsed_0 += adjustSize;
229 afs_stats_cmperf.cacheBucket0_Discarded += oldSize;
232 afs_blocksUsed_1 += adjustSize;
233 afs_stats_cmperf.cacheBucket1_Discarded += oldSize;
236 afs_blocksUsed_2 += adjustSize;
237 afs_stats_cmperf.cacheBucket2_Discarded += oldSize;
245 * Move a dcache from one bucket to another.
247 * \param adc Operate on this dcache.
248 * \param size Size in bucket (?).
249 * \param newBucket Destination bucket.
253 afs_DCMoveBucket(struct dcache *adc, afs_int32 size, afs_int32 newBucket)
258 /* Substract size from old bucket. */
262 afs_blocksUsed_0 -= size;
265 afs_blocksUsed_1 -= size;
268 afs_blocksUsed_2 -= size;
272 /* Set new bucket and increase destination bucket size. */
273 adc->bucket = newBucket;
278 afs_blocksUsed_0 += size;
281 afs_blocksUsed_1 += size;
284 afs_blocksUsed_2 += size;
292 * Init split caches size.
297 afs_blocksUsed_0 = afs_blocksUsed_1 = afs_blocksUsed_2 = 0;
306 afs_DCWhichBucket(afs_int32 phase, afs_int32 bucket)
311 afs_pct1 = afs_blocksUsed_1 / (afs_cacheBlocks / 100);
312 afs_pct2 = afs_blocksUsed_2 / (afs_cacheBlocks / 100);
314 /* Short cut: if we don't know about it, try to kill it */
315 if (phase < 2 && afs_blocksUsed_0)
318 if (afs_pct1 > afs_tpct1)
320 if (afs_pct2 > afs_tpct2)
322 return 0; /* unlikely */
327 * Warn about failing to store a file.
329 * \param acode Associated error code.
330 * \param avolume Volume involved.
331 * \param aflags How to handle the output:
332 * aflags & 1: Print out on console
333 * aflags & 2: Print out on controlling tty
335 * \note Environment: Call this from close call when vnodeops is RCS unlocked.
339 afs_StoreWarn(afs_int32 acode, afs_int32 avolume,
342 static char problem_fmt[] =
343 "afs: failed to store file in volume %d (%s)\n";
344 static char problem_fmt_w_error[] =
345 "afs: failed to store file in volume %d (error %d)\n";
346 static char netproblems[] = "network problems";
347 static char partfull[] = "partition full";
348 static char overquota[] = "over quota";
350 AFS_STATCNT(afs_StoreWarn);
356 afs_warn(problem_fmt, avolume, netproblems);
358 afs_warnuser(problem_fmt, avolume, netproblems);
359 } else if (acode == ENOSPC) {
364 afs_warn(problem_fmt, avolume, partfull);
366 afs_warnuser(problem_fmt, avolume, partfull);
369 /* EDQUOT doesn't exist on solaris and won't be sent by the server.
370 * Instead ENOSPC will be sent...
372 if (acode == EDQUOT) {
377 afs_warn(problem_fmt, avolume, overquota);
379 afs_warnuser(problem_fmt, avolume, overquota);
387 afs_warn(problem_fmt_w_error, avolume, acode);
389 afs_warnuser(problem_fmt_w_error, avolume, acode);
394 * Try waking up truncation daemon, if it's worth it.
397 afs_MaybeWakeupTruncateDaemon(void)
399 if (!afs_CacheTooFull && afs_CacheIsTooFull()) {
400 afs_CacheTooFullCount++;
401 afs_CacheTooFull = 1;
402 if (!afs_TruncateDaemonRunning)
403 afs_osi_Wakeup((int *)afs_CacheTruncateDaemon);
404 } else if (!afs_TruncateDaemonRunning
405 && afs_blocksDiscarded > CM_MAXDISCARDEDCHUNKS) {
406 afs_osi_Wakeup((int *)afs_CacheTruncateDaemon);
411 * Wait for cache drain if conditions warrant.
414 afs_MaybeWaitForCacheDrain(void)
416 if (afs_blocksUsed - afs_blocksDiscarded >
417 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
418 if (afs_WaitForCacheDrain == 0)
419 afs_WaitForCacheDrainCount++;
420 afs_WaitForCacheDrain = 1;
421 afs_osi_Sleep(&afs_WaitForCacheDrain);
428 * Keep statistics on run time for afs_CacheTruncateDaemon. This is a
429 * struct so we need only export one symbol for AIX.
431 static struct CTD_stats {
432 osi_timeval32_t CTD_beforeSleep;
433 osi_timeval32_t CTD_afterSleep;
434 osi_timeval32_t CTD_sleepTime;
435 osi_timeval32_t CTD_runTime;
439 u_int afs_min_cache = 0;
442 * If there are waiters for the cache to drain, wake them if
443 * the number of free or discarded cache blocks reaches the
444 * CM_CACHESIZEDDRAINEDPCT limit.
447 * This routine must be called with the afs_xdcache lock held
451 afs_WakeCacheWaitersIfDrained(void)
453 if (afs_WaitForCacheDrain) {
454 if ((afs_blocksUsed - afs_blocksDiscarded) <=
455 PERCENT(CM_CACHESIZEDRAINEDPCT, afs_cacheBlocks)) {
456 afs_WaitForCacheDrain = 0;
457 afs_osi_Wakeup(&afs_WaitForCacheDrain);
463 * Keeps the cache clean and free by truncating uneeded files, when used.
468 afs_CacheTruncateDaemon(void)
470 osi_timeval32_t CTD_tmpTime;
474 PERCENT((100 - CM_DCACHECOUNTFREEPCT + CM_DCACHEEXTRAPCT), afs_cacheFiles);
476 (((10 * AFS_CHUNKSIZE(0)) + afs_fsfragsize) & ~afs_fsfragsize) >> 10;
478 osi_GetTime(&CTD_stats.CTD_afterSleep);
479 afs_TruncateDaemonRunning = 1;
481 cb_lowat = PERCENT((CM_DCACHESPACEFREEPCT - CM_DCACHEEXTRAPCT), afs_cacheBlocks);
482 ObtainWriteLock(&afs_xdcache, 266);
483 if (afs_CacheTooFull || afs_WaitForCacheDrain) {
484 int space_needed, slots_needed;
485 /* if we get woken up, we should try to clean something out */
486 for (counter = 0; counter < 10; counter++) {
488 afs_blocksUsed - afs_blocksDiscarded - cb_lowat;
489 if (space_needed < 0)
492 dc_hiwat - afs_freeDCCount - afs_discardDCCount;
493 if (slots_needed < 0)
495 if (slots_needed || space_needed)
496 afs_GetDownD(slots_needed, &space_needed, 0);
497 if ((space_needed <= 0) && (slots_needed <= 0)) {
500 if (afs_termState == AFSOP_STOP_TRUNCDAEMON)
503 if (!afs_CacheIsTooFull()) {
504 afs_CacheTooFull = 0;
505 afs_WakeCacheWaitersIfDrained();
507 } /* end of cache cleanup */
508 ReleaseWriteLock(&afs_xdcache);
511 * This is a defensive check to try to avoid starving threads
512 * that may need the global lock so thay can help free some
513 * cache space. If this thread won't be sleeping or truncating
514 * any cache files then give up the global lock so other
515 * threads get a chance to run.
517 if ((afs_termState != AFSOP_STOP_TRUNCDAEMON) && afs_CacheTooFull
518 && (!afs_blocksDiscarded || afs_WaitForCacheDrain)) {
519 afs_osi_Wait(100, 0, 0); /* 100 milliseconds */
523 * This is where we free the discarded cache elements.
525 while (afs_blocksDiscarded && !afs_WaitForCacheDrain
526 && (afs_termState != AFSOP_STOP_TRUNCDAEMON)) {
527 int code = afs_FreeDiscardedDCache();
529 /* If we can't free any discarded dcache entries, that's okay.
530 * We're just doing this in the background; if someone needs
531 * discarded entries freed, they will try it themselves and/or
532 * signal us that the cache is too full. In any case, we'll
533 * try doing this again the next time we run through the loop.
539 /* See if we need to continue to run. Someone may have
540 * signalled us while we were executing.
542 if (!afs_WaitForCacheDrain && !afs_CacheTooFull
543 && (afs_termState != AFSOP_STOP_TRUNCDAEMON)) {
544 /* Collect statistics on truncate daemon. */
545 CTD_stats.CTD_nSleeps++;
546 osi_GetTime(&CTD_stats.CTD_beforeSleep);
547 afs_stats_GetDiff(CTD_tmpTime, CTD_stats.CTD_afterSleep,
548 CTD_stats.CTD_beforeSleep);
549 afs_stats_AddTo(CTD_stats.CTD_runTime, CTD_tmpTime);
551 afs_TruncateDaemonRunning = 0;
552 afs_osi_Sleep((int *)afs_CacheTruncateDaemon);
553 afs_TruncateDaemonRunning = 1;
555 osi_GetTime(&CTD_stats.CTD_afterSleep);
556 afs_stats_GetDiff(CTD_tmpTime, CTD_stats.CTD_beforeSleep,
557 CTD_stats.CTD_afterSleep);
558 afs_stats_AddTo(CTD_stats.CTD_sleepTime, CTD_tmpTime);
560 if (afs_termState == AFSOP_STOP_TRUNCDAEMON) {
561 afs_termState = AFSOP_STOP_AFSDB;
562 afs_osi_Wakeup(&afs_termState);
570 * Make adjustment for the new size in the disk cache entry
572 * \note Major Assumptions Here:
573 * Assumes that frag size is an integral power of two, less one,
574 * and that this is a two's complement machine. I don't
575 * know of any filesystems which violate this assumption...
577 * \param adc Ptr to dcache entry.
578 * \param anewsize New size desired.
583 afs_AdjustSize(struct dcache *adc, afs_int32 newSize)
587 AFS_STATCNT(afs_AdjustSize);
589 if (newSize > afs_OtherCSize && !(adc->f.fid.Fid.Vnode & 1)) {
590 /* No non-dir cache files should be larger than the chunk size.
591 * (Directory blobs are fetched in a single chunk file, so directories
592 * can be larger.) If someone is requesting that a chunk is larger than
593 * the chunk size, something strange is happening. Log a message about
594 * it, to give a hint to subsequent strange behavior, if any occurs. */
598 afs_warn("afs: Warning: dcache %d is very large (%d > %d). This "
599 "should not happen, but trying to continue regardless. If "
600 "AFS starts hanging or behaving strangely, this might be "
602 adc->index, newSize, afs_OtherCSize);
606 adc->dflags |= DFEntryMod;
607 oldSize = ((adc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10; /* round up */
608 adc->f.chunkBytes = newSize;
611 newSize = ((newSize + afs_fsfragsize) ^ afs_fsfragsize) >> 10; /* round up */
612 afs_DCAdjustSize(adc, oldSize, newSize);
613 if ((newSize > oldSize) && !AFS_IS_DISCONNECTED) {
615 /* We're growing the file, wakeup the daemon */
616 afs_MaybeWakeupTruncateDaemon();
618 afs_blocksUsed += (newSize - oldSize);
619 afs_stats_cmperf.cacheBlocksInUse = afs_blocksUsed; /* XXX */
624 * This routine is responsible for moving at least one entry (but up
625 * to some number of them) from the LRU queue to the free queue.
627 * \param anumber Number of entries that should ideally be moved.
628 * \param aneedSpace How much space we need (1K blocks);
631 * The anumber parameter is just a hint; at least one entry MUST be
632 * moved, or we'll panic. We must be called with afs_xdcache
633 * write-locked. We should try to satisfy both anumber and aneedspace,
634 * whichever is more demanding - need to do several things:
635 * 1. only grab up to anumber victims if aneedSpace <= 0, not
636 * the whole set of MAXATONCE.
637 * 2. dynamically choose MAXATONCE to reflect severity of
638 * demand: something like (*aneedSpace >> (logChunk - 9))
640 * \note N.B. if we're called with aneedSpace <= 0 and anumber > 0, that
641 * indicates that the cache is not properly configured/tuned or
642 * something. We should be able to automatically correct that problem.
645 #define MAXATONCE 16 /* max we can obtain at once */
647 afs_GetDownD(int anumber, int *aneedSpace, afs_int32 buckethint)
651 struct VenusFid *afid;
656 afs_uint32 victims[MAXATONCE];
657 struct dcache *victimDCs[MAXATONCE];
658 afs_hyper_t victimTimes[MAXATONCE]; /* youngest (largest LRU time) first */
659 afs_uint32 victimPtr; /* next free item in victim arrays */
660 afs_hyper_t maxVictimTime; /* youngest (largest LRU time) victim */
661 afs_uint32 maxVictimPtr; /* where it is */
665 AFS_STATCNT(afs_GetDownD);
667 if (CheckLock(&afs_xdcache) != -1)
668 osi_Panic("getdownd nolock");
669 /* decrement anumber first for all dudes in free list */
670 /* SHOULD always decrement anumber first, even if aneedSpace >0,
671 * because we should try to free space even if anumber <=0 */
672 if (!aneedSpace || *aneedSpace <= 0) {
673 anumber -= afs_freeDCCount;
675 return; /* enough already free */
679 /* bounds check parameter */
680 if (anumber > MAXATONCE)
681 anumber = MAXATONCE; /* all we can do */
683 /* rewrite so phases include a better eligiblity for gc test*/
685 * The phase variable manages reclaims. Set to 0, the first pass,
686 * we don't reclaim active entries, or other than target bucket.
687 * Set to 1, we reclaim even active ones in target bucket.
688 * Set to 2, we reclaim any inactive one.
689 * Set to 3, we reclaim even active ones. On Solaris, we also reclaim
690 * entries whose corresponding vcache has a nonempty multiPage list, when
699 for (i = 0; i < afs_cacheFiles; i++)
700 /* turn off all flags */
701 afs_indexFlags[i] &= ~IFFlag;
703 while (anumber > 0 || (aneedSpace && *aneedSpace > 0)) {
704 /* find oldest entries for reclamation */
705 maxVictimPtr = victimPtr = 0;
706 hzero(maxVictimTime);
707 curbucket = afs_DCWhichBucket(phase, buckethint);
708 /* select victims from access time array */
709 for (i = 0; i < afs_cacheFiles; i++) {
710 if (afs_indexFlags[i] & (IFDataMod | IFFree | IFDiscarded)) {
711 /* skip if dirty or already free */
714 tdc = afs_indexTable[i];
715 if (tdc && (curbucket != tdc->bucket) && (phase < 4))
717 /* Wrong bucket; can't use it! */
720 if (tdc && (tdc->refCount != 0)) {
721 /* Referenced; can't use it! */
724 hset(vtime, afs_indexTimes[i]);
726 /* if we've already looked at this one, skip it */
727 if (afs_indexFlags[i] & IFFlag)
730 if (victimPtr < MAXATONCE) {
731 /* if there's at least one free victim slot left */
732 victims[victimPtr] = i;
733 hset(victimTimes[victimPtr], vtime);
734 if (hcmp(vtime, maxVictimTime) > 0) {
735 hset(maxVictimTime, vtime);
736 maxVictimPtr = victimPtr;
739 } else if (hcmp(vtime, maxVictimTime) < 0) {
741 * We're older than youngest victim, so we replace at
744 /* find youngest (largest LRU) victim */
747 osi_Panic("getdownd local");
749 hset(victimTimes[j], vtime);
750 /* recompute maxVictimTime */
751 hset(maxVictimTime, vtime);
752 for (j = 0; j < victimPtr; j++)
753 if (hcmp(maxVictimTime, victimTimes[j]) < 0) {
754 hset(maxVictimTime, victimTimes[j]);
760 /* now really reclaim the victims */
761 j = 0; /* flag to track if we actually got any of the victims */
762 /* first, hold all the victims, since we're going to release the lock
763 * during the truncate operation.
765 for (i = 0; i < victimPtr; i++) {
766 tdc = afs_GetValidDSlot(victims[i]);
767 /* We got tdc->tlock(R) here */
768 if (tdc && tdc->refCount == 1)
773 ReleaseReadLock(&tdc->tlock);
778 for (i = 0; i < victimPtr; i++) {
779 /* q is first elt in dcache entry */
781 /* now, since we're dropping the afs_xdcache lock below, we
782 * have to verify, before proceeding, that there are no other
783 * references to this dcache entry, even now. Note that we
784 * compare with 1, since we bumped it above when we called
785 * afs_GetValidDSlot to preserve the entry's identity.
787 if (tdc && tdc->refCount == 1) {
788 unsigned char chunkFlags;
789 afs_size_t tchunkoffset = 0;
791 /* xdcache is lower than the xvcache lock */
792 ReleaseWriteLock(&afs_xdcache);
793 ObtainReadLock(&afs_xvcache);
794 tvc = afs_FindVCache(afid, 0, 0 /* no stats, no vlru */ );
795 ReleaseReadLock(&afs_xvcache);
796 ObtainWriteLock(&afs_xdcache, 527);
798 if (tdc->refCount > 1)
801 tchunkoffset = AFS_CHUNKTOBASE(tdc->f.chunk);
802 chunkFlags = afs_indexFlags[tdc->index];
803 if (((phase & 1) == 0) && osi_Active(tvc))
805 if (((phase & 1) == 1) && osi_Active(tvc)
806 && (tvc->f.states & CDCLock)
807 && (chunkFlags & IFAnyPages))
809 if (chunkFlags & IFDataMod)
811 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
812 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, skip,
813 ICL_TYPE_INT32, tdc->index, ICL_TYPE_OFFSET,
814 ICL_HANDLE_OFFSET(tchunkoffset));
816 #if defined(AFS_SUN5_ENV)
818 * Now we try to invalidate pages. We do this only for
819 * Solaris. For other platforms, it's OK to recycle a
820 * dcache entry out from under a page, because the strategy
821 * function can call afs_GetDCache().
823 if (!skip && (chunkFlags & IFAnyPages)) {
826 ReleaseWriteLock(&afs_xdcache);
827 ObtainWriteLock(&tvc->vlock, 543);
828 if (!QEmpty(&tvc->multiPage)) {
829 if (phase < 3 || osi_VM_MultiPageConflict(tvc, tdc)) {
834 /* block locking pages */
835 tvc->vstates |= VPageCleaning;
836 /* block getting new pages */
838 ReleaseWriteLock(&tvc->vlock);
839 /* One last recheck */
840 ObtainWriteLock(&afs_xdcache, 333);
841 chunkFlags = afs_indexFlags[tdc->index];
842 if (tdc->refCount > 1 || (chunkFlags & IFDataMod)
843 || (osi_Active(tvc) && (tvc->f.states & CDCLock)
844 && (chunkFlags & IFAnyPages))) {
846 ReleaseWriteLock(&afs_xdcache);
849 ReleaseWriteLock(&afs_xdcache);
851 code = osi_VM_GetDownD(tvc, tdc);
853 ObtainWriteLock(&afs_xdcache, 269);
854 /* we actually removed all pages, clean and dirty */
856 afs_indexFlags[tdc->index] &=
857 ~(IFDirtyPages | IFAnyPages);
860 ReleaseWriteLock(&afs_xdcache);
862 ObtainWriteLock(&tvc->vlock, 544);
863 if (--tvc->activeV == 0
864 && (tvc->vstates & VRevokeWait)) {
865 tvc->vstates &= ~VRevokeWait;
866 afs_osi_Wakeup((char *)&tvc->vstates);
869 if (tvc->vstates & VPageCleaning) {
870 tvc->vstates &= ~VPageCleaning;
871 afs_osi_Wakeup((char *)&tvc->vstates);
874 ReleaseWriteLock(&tvc->vlock);
876 #endif /* AFS_SUN5_ENV */
878 ReleaseWriteLock(&afs_xdcache);
881 afs_PutVCache(tvc); /*XXX was AFS_FAST_RELE?*/
882 ObtainWriteLock(&afs_xdcache, 528);
883 if (afs_indexFlags[tdc->index] &
884 (IFDataMod | IFDirtyPages | IFAnyPages))
886 if (tdc->refCount > 1)
889 #if defined(AFS_SUN5_ENV)
891 /* no vnode, so IFDirtyPages is spurious (we don't
892 * sweep dcaches on vnode recycling, so we can have
893 * DIRTYPAGES set even when all pages are gone). Just
895 * Hold vcache lock to prevent vnode from being
896 * created while we're clearing IFDirtyPages.
898 afs_indexFlags[tdc->index] &=
899 ~(IFDirtyPages | IFAnyPages);
903 /* skip this guy and mark him as recently used */
904 afs_indexFlags[tdc->index] |= IFFlag;
905 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
906 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, 2,
907 ICL_TYPE_INT32, tdc->index, ICL_TYPE_OFFSET,
908 ICL_HANDLE_OFFSET(tchunkoffset));
910 /* flush this dude from the data cache and reclaim;
911 * first, make sure no one will care that we damage
912 * it, by removing it from all hash tables. Then,
913 * melt it down for parts. Note that any concurrent
914 * (new possibility!) calls to GetDownD won't touch
915 * this guy because his reference count is > 0. */
916 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
917 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, 3,
918 ICL_TYPE_INT32, tdc->index, ICL_TYPE_OFFSET,
919 ICL_HANDLE_OFFSET(tchunkoffset));
920 AFS_STATCNT(afs_gget);
921 afs_HashOutDCache(tdc, 1);
922 if (tdc->f.chunkBytes != 0) {
926 (tdc->f.chunkBytes + afs_fsfragsize) >> 10;
931 afs_DiscardDCache(tdc);
936 j = 1; /* we reclaimed at least one victim */
941 } /* end of for victims loop */
944 /* Phase is 0 and no one was found, so try phase 1 (ignore
945 * osi_Active flag) */
948 for (i = 0; i < afs_cacheFiles; i++)
949 /* turn off all flags */
950 afs_indexFlags[i] &= ~IFFlag;
953 /* found no one in phases 0-5, we're hosed */
957 } /* big while loop */
965 * Remove adc from any hash tables that would allow it to be located
966 * again by afs_FindDCache or afs_GetDCache.
968 * \param adc Pointer to dcache entry to remove from hash tables.
970 * \note Locks: Must have the afs_xdcache lock write-locked to call this function.
974 afs_HashOutDCache(struct dcache *adc, int zap)
978 AFS_STATCNT(afs_glink);
980 /* we know this guy's in the LRUQ. We'll move dude into DCQ below */
982 /* if this guy is in the hash table, pull him out */
983 if (adc->f.fid.Fid.Volume != 0) {
984 /* remove entry from first hash chains */
985 i = DCHash(&adc->f.fid, adc->f.chunk);
986 us = afs_dchashTbl[i];
987 if (us == adc->index) {
988 /* first dude in the list */
989 afs_dchashTbl[i] = afs_dcnextTbl[adc->index];
991 /* somewhere on the chain */
992 while (us != NULLIDX) {
993 if (afs_dcnextTbl[us] == adc->index) {
994 /* found item pointing at the one to delete */
995 afs_dcnextTbl[us] = afs_dcnextTbl[adc->index];
998 us = afs_dcnextTbl[us];
1001 osi_Panic("dcache hc");
1003 /* remove entry from *other* hash chain */
1004 i = DVHash(&adc->f.fid);
1005 us = afs_dvhashTbl[i];
1006 if (us == adc->index) {
1007 /* first dude in the list */
1008 afs_dvhashTbl[i] = afs_dvnextTbl[adc->index];
1010 /* somewhere on the chain */
1011 while (us != NULLIDX) {
1012 if (afs_dvnextTbl[us] == adc->index) {
1013 /* found item pointing at the one to delete */
1014 afs_dvnextTbl[us] = afs_dvnextTbl[adc->index];
1017 us = afs_dvnextTbl[us];
1020 osi_Panic("dcache hv");
1025 /* prevent entry from being found on a reboot (it is already out of
1026 * the hash table, but after a crash, we just look at fid fields of
1027 * stable (old) entries).
1029 adc->f.fid.Fid.Volume = 0; /* invalid */
1031 /* mark entry as modified */
1032 adc->dflags |= DFEntryMod;
1037 } /*afs_HashOutDCache */
1040 * Flush the given dcache entry, pulling it from hash chains
1041 * and truncating the associated cache file.
1043 * \param adc Ptr to dcache entry to flush.
1045 * \note Environment:
1046 * This routine must be called with the afs_xdcache lock held
1050 afs_FlushDCache(struct dcache *adc)
1052 AFS_STATCNT(afs_FlushDCache);
1054 * Bump the number of cache files flushed.
1056 afs_stats_cmperf.cacheFlushes++;
1058 /* remove from all hash tables */
1059 afs_HashOutDCache(adc, 1);
1061 /* Free its space; special case null operation, since truncate operation
1062 * in UFS is slow even in this case, and this allows us to pre-truncate
1063 * these files at more convenient times with fewer locks set
1064 * (see afs_GetDownD).
1066 if (adc->f.chunkBytes != 0) {
1067 afs_DiscardDCache(adc);
1068 afs_MaybeWakeupTruncateDaemon();
1070 afs_FreeDCache(adc);
1072 } /*afs_FlushDCache */
1076 * Put a dcache entry on the free dcache entry list.
1078 * \param adc dcache entry to free.
1080 * \note Environment: called with afs_xdcache lock write-locked.
1083 afs_FreeDCache(struct dcache *adc)
1085 /* Thread on free list, update free list count and mark entry as
1086 * freed in its indexFlags element. Also, ensure DCache entry gets
1087 * written out (set DFEntryMod).
1090 afs_dvnextTbl[adc->index] = afs_freeDCList;
1091 afs_freeDCList = adc->index;
1093 afs_indexFlags[adc->index] |= IFFree;
1094 adc->dflags |= DFEntryMod;
1096 afs_WakeCacheWaitersIfDrained();
1097 } /* afs_FreeDCache */
1100 * Discard the cache element by moving it to the discardDCList.
1101 * This puts the cache element into a quasi-freed state, where
1102 * the space may be reused, but the file has not been truncated.
1104 * \note Major Assumptions Here:
1105 * Assumes that frag size is an integral power of two, less one,
1106 * and that this is a two's complement machine. I don't
1107 * know of any filesystems which violate this assumption...
1109 * \param adr Ptr to dcache entry.
1111 * \note Environment:
1112 * Must be called with afs_xdcache write-locked.
1116 afs_DiscardDCache(struct dcache *adc)
1120 AFS_STATCNT(afs_DiscardDCache);
1122 osi_Assert(adc->refCount == 1);
1124 size = ((adc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10; /* round up */
1125 afs_blocksDiscarded += size;
1126 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1128 afs_dvnextTbl[adc->index] = afs_discardDCList;
1129 afs_discardDCList = adc->index;
1130 afs_discardDCCount++;
1132 adc->f.fid.Fid.Volume = 0;
1133 adc->dflags |= DFEntryMod;
1134 afs_indexFlags[adc->index] |= IFDiscarded;
1136 afs_WakeCacheWaitersIfDrained();
1137 } /*afs_DiscardDCache */
1140 * Get a dcache entry from the discard or free list
1142 * @param[out] adc On success, a dcache from the given list. Otherwise, NULL.
1143 * @param[in] indexp A pointer to the head of the dcache free list or discard
1144 * list (afs_freeDCList, or afs_discardDCList)
1146 * @return 0 on success. If there are no dcache slots available, return ENOSPC.
1147 * If we encountered an error in disk i/o while trying to find a
1148 * dcache, return EIO.
1150 * @pre afs_xdcache is write-locked
1153 afs_GetDSlotFromList(struct dcache **adc, afs_int32 *indexp)
1159 if (*indexp == NULLIDX) {
1163 tdc = afs_GetUnusedDSlot(*indexp);
1168 osi_Assert(tdc->refCount == 1);
1169 ReleaseReadLock(&tdc->tlock);
1170 *indexp = afs_dvnextTbl[tdc->index];
1171 afs_dvnextTbl[tdc->index] = NULLIDX;
1178 * Free the next element on the list of discarded cache elements.
1180 * Returns -1 if we encountered an error preventing us from freeing a
1181 * discarded dcache, or 0 on success.
1184 afs_FreeDiscardedDCache(void)
1187 struct osi_file *tfile;
1190 AFS_STATCNT(afs_FreeDiscardedDCache);
1192 ObtainWriteLock(&afs_xdcache, 510);
1193 if (!afs_blocksDiscarded) {
1194 ReleaseWriteLock(&afs_xdcache);
1199 * Get an entry from the list of discarded cache elements
1201 (void)afs_GetDSlotFromList(&tdc, &afs_discardDCList);
1203 ReleaseWriteLock(&afs_xdcache);
1207 afs_discardDCCount--;
1208 size = ((tdc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10; /* round up */
1209 afs_blocksDiscarded -= size;
1210 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1211 /* We can lock because we just took it off the free list */
1212 ObtainWriteLock(&tdc->lock, 626);
1213 ReleaseWriteLock(&afs_xdcache);
1216 * Truncate the element to reclaim its space
1218 tfile = afs_CFileOpen(&tdc->f.inode);
1220 afs_CFileTruncate(tfile, 0);
1221 afs_CFileClose(tfile);
1222 afs_AdjustSize(tdc, 0);
1223 afs_DCMoveBucket(tdc, 0, 0);
1226 * Free the element we just truncated
1228 ObtainWriteLock(&afs_xdcache, 511);
1229 afs_indexFlags[tdc->index] &= ~IFDiscarded;
1230 afs_FreeDCache(tdc);
1231 tdc->f.states &= ~(DRO|DBackup|DRW);
1232 ReleaseWriteLock(&tdc->lock);
1234 ReleaseWriteLock(&afs_xdcache);
1240 * Free as many entries from the list of discarded cache elements
1241 * as we need to get the free space down below CM_WAITFORDRAINPCT (98%).
1246 afs_MaybeFreeDiscardedDCache(void)
1249 AFS_STATCNT(afs_MaybeFreeDiscardedDCache);
1251 while (afs_blocksDiscarded
1252 && (afs_blocksUsed >
1253 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks))) {
1254 int code = afs_FreeDiscardedDCache();
1256 /* Callers depend on us to get the afs_blocksDiscarded count down.
1257 * If we cannot do that, the callers can spin by calling us over
1258 * and over. Panic for now until we can figure out something
1260 osi_Panic("Error freeing discarded dcache");
1267 * Try to free up a certain number of disk slots.
1269 * \param anumber Targeted number of disk slots to free up.
1271 * \note Environment:
1272 * Must be called with afs_xdcache write-locked.
1276 afs_GetDownDSlot(int anumber)
1278 struct afs_q *tq, *nq;
1283 AFS_STATCNT(afs_GetDownDSlot);
1284 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
1285 osi_Panic("diskless getdowndslot");
1287 if (CheckLock(&afs_xdcache) != -1)
1288 osi_Panic("getdowndslot nolock");
1290 /* decrement anumber first for all dudes in free list */
1291 for (tdc = afs_freeDSList; tdc; tdc = (struct dcache *)tdc->lruq.next)
1294 return; /* enough already free */
1296 for (cnt = 0, tq = afs_DLRU.prev; tq != &afs_DLRU && anumber > 0;
1298 tdc = (struct dcache *)tq; /* q is first elt in dcache entry */
1299 nq = QPrev(tq); /* in case we remove it */
1300 if (tdc->refCount == 0) {
1301 if ((ix = tdc->index) == NULLIDX)
1302 osi_Panic("getdowndslot");
1304 /* write-through if modified */
1305 if (tdc->dflags & DFEntryMod) {
1306 #if defined(AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
1308 * ask proxy to do this for us - we don't have the stack space
1310 while (tdc->dflags & DFEntryMod) {
1313 s = SPLOCK(afs_sgibklock);
1314 if (afs_sgibklist == NULL) {
1315 /* if slot is free, grab it. */
1316 afs_sgibklist = tdc;
1317 SV_SIGNAL(&afs_sgibksync);
1319 /* wait for daemon to (start, then) finish. */
1320 SP_WAIT(afs_sgibklock, s, &afs_sgibkwait, PINOD);
1326 code = afs_WriteDCache(tdc, 1);
1329 * We couldn't flush it at this time; return early because
1330 * if afs_WriteDCache() failed once it is likely to
1331 * continue failing for subsequent dcaches.
1335 tdc->dflags &= ~DFEntryMod;
1339 /* pull the entry out of the lruq and put it on the free list */
1340 QRemove(&tdc->lruq);
1341 afs_indexTable[ix] = NULL;
1342 afs_indexFlags[ix] &= ~IFEverUsed;
1343 tdc->index = NULLIDX;
1344 tdc->lruq.next = (struct afs_q *)afs_freeDSList;
1345 afs_freeDSList = tdc;
1349 } /*afs_GetDownDSlot */
1356 * Increment the reference count on a disk cache entry,
1357 * which already has a non-zero refcount. In order to
1358 * increment the refcount of a zero-reference entry, you
1359 * have to hold afs_xdcache.
1362 * adc : Pointer to the dcache entry to increment.
1365 * Nothing interesting.
1368 afs_RefDCache(struct dcache *adc)
1370 ObtainWriteLock(&adc->tlock, 627);
1371 if (adc->refCount < 0)
1372 osi_Panic("RefDCache: negative refcount");
1374 ReleaseWriteLock(&adc->tlock);
1383 * Decrement the reference count on a disk cache entry.
1386 * ad : Ptr to the dcache entry to decrement.
1389 * Nothing interesting.
1392 afs_PutDCache(struct dcache *adc)
1394 AFS_STATCNT(afs_PutDCache);
1395 ObtainWriteLock(&adc->tlock, 276);
1396 if (adc->refCount <= 0)
1397 osi_Panic("putdcache");
1399 ReleaseWriteLock(&adc->tlock);
1408 * Try to discard all data associated with this file from the
1412 * avc : Pointer to the cache info for the file.
1415 * Both pvnLock and lock are write held.
1418 afs_TryToSmush(struct vcache *avc, afs_ucred_t *acred, int sync)
1423 AFS_STATCNT(afs_TryToSmush);
1424 afs_Trace2(afs_iclSetp, CM_TRACE_TRYTOSMUSH, ICL_TYPE_POINTER, avc,
1425 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
1426 sync = 1; /* XX Temp testing XX */
1428 #if defined(AFS_SUN5_ENV)
1429 ObtainWriteLock(&avc->vlock, 573);
1430 avc->activeV++; /* block new getpages */
1431 ReleaseWriteLock(&avc->vlock);
1434 /* Flush VM pages */
1435 osi_VM_TryToSmush(avc, acred, sync);
1438 * Get the hash chain containing all dce's for this fid
1440 i = DVHash(&avc->f.fid);
1441 ObtainWriteLock(&afs_xdcache, 277);
1442 for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
1443 i = afs_dvnextTbl[index]; /* next pointer this hash table */
1444 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
1445 int releaseTlock = 1;
1446 tdc = afs_GetValidDSlot(index);
1448 /* afs_TryToSmush is best-effort; we may not actually discard
1449 * everything, so failure to discard dcaches due to an i/o
1453 if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
1455 if ((afs_indexFlags[index] & IFDataMod) == 0
1456 && tdc->refCount == 1) {
1457 ReleaseReadLock(&tdc->tlock);
1459 afs_FlushDCache(tdc);
1462 afs_indexTable[index] = 0;
1465 ReleaseReadLock(&tdc->tlock);
1469 #if defined(AFS_SUN5_ENV)
1470 ObtainWriteLock(&avc->vlock, 545);
1471 if (--avc->activeV == 0 && (avc->vstates & VRevokeWait)) {
1472 avc->vstates &= ~VRevokeWait;
1473 afs_osi_Wakeup((char *)&avc->vstates);
1475 ReleaseWriteLock(&avc->vlock);
1477 ReleaseWriteLock(&afs_xdcache);
1479 * It's treated like a callback so that when we do lookups we'll
1480 * invalidate the unique bit if any
1481 * trytoSmush occured during the lookup call
1487 * afs_DCacheMissingChunks
1490 * Given the cached info for a file, return the number of chunks that
1491 * are not available from the dcache.
1494 * avc: Pointer to the (held) vcache entry to look in.
1497 * The number of chunks which are not currently cached.
1500 * The vcache entry is held upon entry.
1504 afs_DCacheMissingChunks(struct vcache *avc)
1507 afs_size_t totalLength = 0;
1508 afs_uint32 totalChunks = 0;
1511 totalLength = avc->f.m.Length;
1512 if (avc->f.truncPos < totalLength)
1513 totalLength = avc->f.truncPos;
1515 /* Length is 0, no chunk missing. */
1516 if (totalLength == 0)
1519 /* If totalLength is a multiple of chunksize, the last byte appears
1520 * as being part of the next chunk, which does not exist.
1521 * Decrementing totalLength by one fixes that.
1524 totalChunks = (AFS_CHUNK(totalLength) + 1);
1526 /* If we're a directory, we only ever have one chunk, regardless of
1527 * the size of the dir.
1529 if (avc->f.fid.Fid.Vnode & 1 || vType(avc) == VDIR)
1533 printf("Should have %d chunks for %u bytes\n",
1534 totalChunks, (totalLength + 1));
1536 i = DVHash(&avc->f.fid);
1537 ObtainWriteLock(&afs_xdcache, 1001);
1538 for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
1539 i = afs_dvnextTbl[index];
1540 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
1541 tdc = afs_GetValidDSlot(index);
1545 if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
1548 ReleaseReadLock(&tdc->tlock);
1552 ReleaseWriteLock(&afs_xdcache);
1554 /*printf("Missing %d chunks\n", totalChunks);*/
1556 return (totalChunks);
1563 * Given the cached info for a file and a byte offset into the
1564 * file, make sure the dcache entry for that file and containing
1565 * the given byte is available, returning it to our caller.
1568 * avc : Pointer to the (held) vcache entry to look in.
1569 * abyte : Which byte we want to get to.
1572 * Pointer to the dcache entry covering the file & desired byte,
1573 * or NULL if not found.
1576 * The vcache entry is held upon entry.
1580 afs_FindDCache(struct vcache *avc, afs_size_t abyte)
1584 struct dcache *tdc = NULL;
1586 AFS_STATCNT(afs_FindDCache);
1587 chunk = AFS_CHUNK(abyte);
1590 * Hash on the [fid, chunk] and get the corresponding dcache index
1591 * after write-locking the dcache.
1593 i = DCHash(&avc->f.fid, chunk);
1594 ObtainWriteLock(&afs_xdcache, 278);
1595 for (index = afs_dchashTbl[i]; index != NULLIDX; index = afs_dcnextTbl[index]) {
1596 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
1597 tdc = afs_GetValidDSlot(index);
1599 /* afs_FindDCache is best-effort; we may not find the given
1600 * file/offset, so if we cannot find the given dcache due to
1601 * i/o errors, that is okay. */
1605 ReleaseReadLock(&tdc->tlock);
1606 if (!FidCmp(&tdc->f.fid, &avc->f.fid) && chunk == tdc->f.chunk) {
1607 break; /* leaving refCount high for caller */
1612 if (index != NULLIDX) {
1613 hset(afs_indexTimes[tdc->index], afs_indexCounter);
1614 hadd32(afs_indexCounter, 1);
1615 ReleaseWriteLock(&afs_xdcache);
1618 ReleaseWriteLock(&afs_xdcache);
1620 } /*afs_FindDCache */
1622 /* only call these from afs_AllocDCache() */
1624 afs_AllocFreeDSlot(struct dcache **adc)
1629 code = afs_GetDSlotFromList(&tdc, &afs_freeDCList);
1633 afs_indexFlags[tdc->index] &= ~IFFree;
1634 ObtainWriteLock(&tdc->lock, 604);
1641 afs_AllocDiscardDSlot(struct dcache **adc, afs_int32 lock)
1645 afs_uint32 size = 0;
1646 struct osi_file *file;
1648 code = afs_GetDSlotFromList(&tdc, &afs_discardDCList);
1652 afs_indexFlags[tdc->index] &= ~IFDiscarded;
1653 ObtainWriteLock(&tdc->lock, 605);
1654 afs_discardDCCount--;
1656 ((tdc->f.chunkBytes +
1657 afs_fsfragsize) ^ afs_fsfragsize) >> 10;
1658 tdc->f.states &= ~(DRO|DBackup|DRW);
1659 afs_DCMoveBucket(tdc, size, 0);
1660 afs_blocksDiscarded -= size;
1661 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1663 /* Truncate the chunk so zeroes get filled properly */
1664 file = afs_CFileOpen(&tdc->f.inode);
1666 afs_CFileTruncate(file, 0);
1667 afs_CFileClose(file);
1668 afs_AdjustSize(tdc, 0);
1676 * Get a fresh dcache from the free or discarded list.
1678 * \param adc Set to the new dcache on success, and NULL on error.
1679 * \param avc Who's dcache is this going to be?
1680 * \param chunk The position where it will be placed in.
1681 * \param lock How are locks held.
1682 * \param ashFid If this dcache going to be used for a shadow dir,
1685 * \note Required locks:
1687 * - avc (R if (lock & 1) set and W otherwise)
1688 * \note It write locks the new dcache. The caller must unlock it.
1690 * \return If we're out of dslots, ENOSPC. If we encountered disk errors, EIO.
1691 * On success, return 0.
1694 afs_AllocDCache(struct dcache **adc, struct vcache *avc, afs_int32 chunk,
1695 afs_int32 lock, struct VenusFid *ashFid)
1698 struct dcache *tdc = NULL;
1702 /* if (lock & 2), prefer 'free' dcaches; otherwise, prefer 'discard'
1703 * dcaches. In either case, try both if our first choice doesn't work due
1706 code = afs_AllocFreeDSlot(&tdc);
1707 if (code == ENOSPC) {
1708 code = afs_AllocDiscardDSlot(&tdc, lock);
1711 code = afs_AllocDiscardDSlot(&tdc, lock);
1712 if (code == ENOSPC) {
1713 code = afs_AllocFreeDSlot(&tdc);
1722 * avc->lock(R) if setLocks
1723 * avc->lock(W) if !setLocks
1729 * Fill in the newly-allocated dcache record.
1731 afs_indexFlags[tdc->index] &= ~(IFDirtyPages | IFAnyPages);
1733 /* Use shadow fid if provided. */
1734 tdc->f.fid = *ashFid;
1736 /* Use normal vcache's fid otherwise. */
1737 tdc->f.fid = avc->f.fid;
1738 if (avc->f.states & CRO)
1739 tdc->f.states = DRO;
1740 else if (avc->f.states & CBackup)
1741 tdc->f.states = DBackup;
1743 tdc->f.states = DRW;
1744 afs_DCMoveBucket(tdc, 0, afs_DCGetBucket(avc));
1745 afs_indexUnique[tdc->index] = tdc->f.fid.Fid.Unique;
1747 hones(tdc->f.versionNo); /* invalid value */
1748 tdc->f.chunk = chunk;
1749 tdc->validPos = AFS_CHUNKTOBASE(chunk);
1751 if (tdc->lruq.prev == &tdc->lruq)
1752 osi_Panic("lruq 1");
1759 IsDCacheSizeOK(struct dcache *adc, struct vcache *avc, afs_int32 chunk_bytes,
1760 afs_size_t file_length, afs_uint32 versionNo, int from_net)
1762 afs_size_t expected_bytes;
1763 afs_size_t chunk_start = AFS_CHUNKTOBASE(adc->f.chunk);
1765 if (vType(avc) == VDIR) {
1767 * Directory blobs may be constructed locally (see afs_LocalHero), and
1768 * the size of the blob may differ slightly compared to what's on the
1769 * fileserver. So, skip size checks for directories.
1774 if ((avc->f.states & CDirty)) {
1776 * Our vcache may have writes that are local to our cache, but not yet
1777 * written to the fileserver. In such a situation, we may have dcaches
1778 * for that file that are "short". For example:
1780 * Say we have a file that is 0 bytes long. A process opens that file,
1781 * and writes some data to offset 5M (keeping the file open). Another
1782 * process comes along and reads data from offset 1M. We'll try to
1783 * fetch data at offset 1M, and the fileserver will respond with 0
1784 * bytes, since our locally-written data hasn't been written to the
1785 * fileserver yet (on the fileserver, the file is still 0-bytes long).
1786 * So our dcache at offset 1M will have 0 bytes.
1788 * So if CDirty is set, don't do any size/length checks at all, since
1789 * we have no idea if the avc length is valid.
1794 if (!from_net && (adc->f.states & DRW)) {
1796 * The dcache data we're looking at is from our local cache (not from a
1797 * fileserver), and it's for data in an RW volume. For cached RW data,
1798 * there are some edge cases that can cause the below length checks to
1799 * trigger false positives.
1801 * For example: if the local client writes 4 bytes to a new file at
1802 * offset 0, and then 4 bytes at offset 0x400000, the file will be
1803 * 0x400004 bytes long, but the first dcache chunk will only contain 4
1804 * bytes. If such a file is fetched from a fileserver, the first chunk
1805 * will have a full chunk of data (most of it zeroes), but on the
1806 * client that did the write, the sparse data will not appear in the
1809 * Such false positives should only be possible with RW data, since
1810 * non-RW data is never generated locally. So to avoid the false
1811 * positives, assume the dcache length is OK for RW data if the dcache
1812 * came from our local cache (and not directly from a fileserver).
1817 if (file_length < chunk_start) {
1821 expected_bytes = file_length - chunk_start;
1823 if (vType(avc) != VDIR && expected_bytes > AFS_CHUNKTOSIZE(adc->f.chunk)) {
1824 /* A non-dir chunk cannot have more bytes than the chunksize. */
1825 expected_bytes = AFS_CHUNKTOSIZE(adc->f.chunk);
1829 if (chunk_bytes != expected_bytes) {
1830 static const afs_uint32 one_hour = 60 * 60;
1831 static afs_uint32 last_warn;
1832 afs_uint32 now = osi_Time();
1834 if (now < last_warn) {
1835 /* clock went backwards */
1839 if (now - last_warn > one_hour) {
1840 unsigned int mtime = adc->f.modTime;
1846 * The dcache we're looking at didn't come from the cache, but is
1847 * being populated from the net. Don't print out its mtime in that
1848 * case; that would be misleading since that's the mtime from the
1849 * last time this dcache slot was written to.
1854 afs_warn("afs: Detected corrupt dcache for file %d.%u.%u.%u: chunk %d "
1855 "(offset %lu) has %d bytes, but it should have %lu bytes\n",
1857 adc->f.fid.Fid.Volume,
1858 adc->f.fid.Fid.Vnode,
1859 adc->f.fid.Fid.Unique,
1861 (unsigned long)chunk_start,
1863 (unsigned long)expected_bytes);
1864 afs_warn("afs: (dcache %p, file length %lu, DV %u, dcache mtime %u, "
1865 "index %d, dflags 0x%x, mflags 0x%x, states 0x%x, vcache "
1868 (unsigned long)file_length,
1872 (unsigned)adc->dflags,
1873 (unsigned)adc->mflags,
1874 (unsigned)adc->f.states,
1876 afs_warn("afs: Ignoring the dcache for now, but this may indicate "
1877 "corruption in the AFS cache, or a bug.\n");
1885 * Check if a dcache is "fresh". That is, if the dcache's DV matches the DV of
1886 * the vcache for that file, and the dcache looks "sane" (its length makes
1887 * sense, when considering the length of the given avc).
1889 * \param adc The dcache to check
1890 * \param avc The vcache for adc
1892 * \return 1 if the dcache is "fresh". 0 otherwise.
1895 afs_IsDCacheFresh(struct dcache *adc, struct vcache *avc)
1897 if (!hsame(adc->f.versionNo, avc->f.m.DataVersion)) {
1902 * If we've reached here, the DV in adc matches the DV of our avc. Check if
1903 * the number of bytes in adc agrees with the avc file length, as a sanity
1904 * check. If they don't match, we'll pretend the DVs don't match, so the
1905 * bad dcache data will not be used, and we'll probably re-fetch the chunk
1906 * data, replacing the bad chunk.
1909 if (!IsDCacheSizeOK(adc, avc, adc->f.chunkBytes, avc->f.m.Length,
1910 hgetlo(adc->f.versionNo), 0)) {
1921 * This function is called to obtain a reference to data stored in
1922 * the disk cache, locating a chunk of data containing the desired
1923 * byte and returning a reference to the disk cache entry, with its
1924 * reference count incremented.
1928 * avc : Ptr to a vcache entry (unlocked)
1929 * abyte : Byte position in the file desired
1930 * areq : Request structure identifying the requesting user.
1931 * aflags : Settings as follows:
1933 * 2 : Return after creating entry.
1934 * 4 : called from afs_vnop_write.c
1935 * *alen contains length of data to be written.
1937 * aoffset : Set to the offset within the chunk where the resident
1939 * alen : Set to the number of bytes of data after the desired
1940 * byte (including the byte itself) which can be read
1944 * The vcache entry pointed to by avc is unlocked upon entry.
1948 * Update the vnode-to-dcache hint if we can get the vnode lock
1949 * right away. Assumes dcache entry is at least read-locked.
1952 updateV2DC(int lockVc, struct vcache *v, struct dcache *d, int src)
1954 if (!lockVc || 0 == NBObtainWriteLock(&v->lock, src)) {
1955 if (afs_IsDCacheFresh(d, v) && v->callback)
1958 ReleaseWriteLock(&v->lock);
1962 /* avc - Write-locked unless aflags & 1 */
1964 afs_GetDCache(struct vcache *avc, afs_size_t abyte,
1965 struct vrequest *areq, afs_size_t * aoffset,
1966 afs_size_t * alen, int aflags)
1968 afs_int32 i, code, shortcut;
1969 #if defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
1970 afs_int32 adjustsize = 0;
1976 afs_size_t Position = 0;
1977 afs_int32 size, tlen; /* size of segment to transfer */
1978 struct afs_FetchOutput *tsmall = 0;
1980 struct osi_file *file;
1981 struct afs_conn *tc;
1983 struct server *newCallback = NULL;
1984 char setNewCallback;
1985 char setVcacheStatus;
1986 char doVcacheUpdate;
1988 int doAdjustSize = 0;
1989 int doReallyAdjustSize = 0;
1990 int overWriteWholeChunk = 0;
1991 struct rx_connection *rxconn;
1994 struct afs_stats_AccessInfo *accP; /*Ptr to access record in stats */
1995 int fromReplica; /*Are we reading from a replica? */
1996 int numFetchLoops; /*# times around the fetch/analyze loop */
1997 #endif /* AFS_NOSTATS */
1999 AFS_STATCNT(afs_GetDCache);
2003 setLocks = aflags & 1;
2006 * Determine the chunk number and offset within the chunk corresponding
2007 * to the desired byte.
2009 if (avc->f.fid.Fid.Vnode & 1) { /* if (vType(avc) == VDIR) */
2012 chunk = AFS_CHUNK(abyte);
2015 /* come back to here if we waited for the cache to drain. */
2018 setNewCallback = setVcacheStatus = 0;
2022 ObtainWriteLock(&avc->lock, 616);
2024 ObtainReadLock(&avc->lock);
2029 * avc->lock(R) if setLocks && !slowPass
2030 * avc->lock(W) if !setLocks || slowPass
2035 /* check hints first! (might could use bcmp or some such...) */
2036 if ((tdc = avc->dchint)) {
2040 * The locking order between afs_xdcache and dcache lock matters.
2041 * The hint dcache entry could be anywhere, even on the free list.
2042 * Locking afs_xdcache ensures that noone is trying to pull dcache
2043 * entries from the free list, and thereby assuming them to be not
2044 * referenced and not locked.
2046 ObtainReadLock(&afs_xdcache);
2047 dcLocked = (0 == NBObtainSharedLock(&tdc->lock, 601));
2049 if (dcLocked && (tdc->index != NULLIDX)
2050 && !FidCmp(&tdc->f.fid, &avc->f.fid) && chunk == tdc->f.chunk
2051 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
2052 /* got the right one. It might not be the right version, and it
2053 * might be fetching, but it's the right dcache entry.
2055 /* All this code should be integrated better with what follows:
2056 * I can save a good bit more time under a write lock if I do..
2058 ObtainWriteLock(&tdc->tlock, 603);
2060 ReleaseWriteLock(&tdc->tlock);
2062 ReleaseReadLock(&afs_xdcache);
2065 if (afs_IsDCacheFresh(tdc, avc)
2066 && !(tdc->dflags & DFFetching)) {
2068 afs_stats_cmperf.dcacheHits++;
2069 ObtainWriteLock(&afs_xdcache, 559);
2070 QRemove(&tdc->lruq);
2071 QAdd(&afs_DLRU, &tdc->lruq);
2072 ReleaseWriteLock(&afs_xdcache);
2075 * avc->lock(R) if setLocks && !slowPass
2076 * avc->lock(W) if !setLocks || slowPass
2083 ReleaseSharedLock(&tdc->lock);
2084 ReleaseReadLock(&afs_xdcache);
2092 * avc->lock(R) if setLocks && !slowPass
2093 * avc->lock(W) if !setLocks || slowPass
2094 * tdc->lock(S) if tdc
2097 if (!tdc) { /* If the hint wasn't the right dcache entry */
2098 int dslot_error = 0;
2100 * Hash on the [fid, chunk] and get the corresponding dcache index
2101 * after write-locking the dcache.
2106 * avc->lock(R) if setLocks && !slowPass
2107 * avc->lock(W) if !setLocks || slowPass
2110 i = DCHash(&avc->f.fid, chunk);
2111 /* check to make sure our space is fine */
2112 afs_MaybeWakeupTruncateDaemon();
2114 ObtainWriteLock(&afs_xdcache, 280);
2116 for (index = afs_dchashTbl[i]; index != NULLIDX; us = index, index = afs_dcnextTbl[index]) {
2117 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
2118 tdc = afs_GetValidDSlot(index);
2120 /* we got an i/o error when trying to get the given dslot.
2121 * it's possible the dslot we're looking for is elsewhere,
2122 * but most likely the disk cache is currently unusable, so
2123 * all afs_GetValidDSlot calls will fail, so just bail out. */
2128 ReleaseReadLock(&tdc->tlock);
2131 * avc->lock(R) if setLocks && !slowPass
2132 * avc->lock(W) if !setLocks || slowPass
2135 if (!FidCmp(&tdc->f.fid, &avc->f.fid) && chunk == tdc->f.chunk) {
2136 /* Move it up in the beginning of the list */
2137 if (afs_dchashTbl[i] != index) {
2138 afs_dcnextTbl[us] = afs_dcnextTbl[index];
2139 afs_dcnextTbl[index] = afs_dchashTbl[i];
2140 afs_dchashTbl[i] = index;
2142 ReleaseWriteLock(&afs_xdcache);
2143 ObtainSharedLock(&tdc->lock, 606);
2144 break; /* leaving refCount high for caller */
2152 * If we didn't find the entry, we'll create one.
2154 if (index == NULLIDX) {
2157 * avc->lock(R) if setLocks
2158 * avc->lock(W) if !setLocks
2161 afs_Trace2(afs_iclSetp, CM_TRACE_GETDCACHE1, ICL_TYPE_POINTER,
2162 avc, ICL_TYPE_INT32, chunk);
2165 /* We couldn't find the dcache we want, but we hit some i/o
2166 * errors when trying to find it, so we're not sure if the
2167 * dcache we want is in the cache or not. Error out, so we
2168 * don't try to possibly create 2 separate dcaches for the
2169 * same exact data. */
2170 ReleaseWriteLock(&afs_xdcache);
2174 if (afs_discardDCList == NULLIDX && afs_freeDCList == NULLIDX) {
2176 avc->f.states |= CDCLock;
2177 /* just need slots */
2178 afs_GetDownD(5, (int *)0, afs_DCGetBucket(avc));
2180 avc->f.states &= ~CDCLock;
2182 code = afs_AllocDCache(&tdc, avc, chunk, aflags, NULL);
2184 ReleaseWriteLock(&afs_xdcache);
2185 if (code == ENOSPC) {
2186 /* It looks like afs_AllocDCache failed because we don't
2187 * have any free dslots to use. Maybe if we wait a little
2188 * while, we'll be able to free up some slots, so try for 5
2189 * minutes, then bail out. */
2190 if (++downDCount > 300) {
2191 afs_warn("afs: Unable to get free cache space for file "
2192 "%u:%u.%u.%u for 5 minutes; failing with an i/o error\n",
2194 avc->f.fid.Fid.Volume,
2195 avc->f.fid.Fid.Vnode,
2196 avc->f.fid.Fid.Unique);
2199 afs_osi_Wait(1000, 0, 0);
2203 /* afs_AllocDCache failed, but not because we're out of free
2204 * dslots. Something must be screwy with the cache, so bail out
2205 * immediately without waiting. */
2206 afs_warn("afs: Error while alloc'ing cache slot for file "
2207 "%u:%u.%u.%u; failing with an i/o error\n",
2209 avc->f.fid.Fid.Volume,
2210 avc->f.fid.Fid.Vnode,
2211 avc->f.fid.Fid.Unique);
2217 * avc->lock(R) if setLocks
2218 * avc->lock(W) if !setLocks
2224 * Now add to the two hash chains - note that i is still set
2225 * from the above DCHash call.
2227 afs_dcnextTbl[tdc->index] = afs_dchashTbl[i];
2228 afs_dchashTbl[i] = tdc->index;
2229 i = DVHash(&avc->f.fid);
2230 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[i];
2231 afs_dvhashTbl[i] = tdc->index;
2232 tdc->dflags = DFEntryMod;
2234 afs_MaybeWakeupTruncateDaemon();
2235 ReleaseWriteLock(&afs_xdcache);
2236 ConvertWToSLock(&tdc->lock);
2241 /* vcache->dcache hint failed */
2244 * avc->lock(R) if setLocks && !slowPass
2245 * avc->lock(W) if !setLocks || slowPass
2248 afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE2, ICL_TYPE_POINTER, avc,
2249 ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
2250 hgetlo(tdc->f.versionNo), ICL_TYPE_INT32,
2251 hgetlo(avc->f.m.DataVersion));
2253 * Here we have the entry in tdc, with its refCount incremented.
2254 * Note: we don't use the S-lock on avc; it costs concurrency when
2255 * storing a file back to the server.
2259 * Not a newly created file so we need to check the file's length and
2260 * compare data versions since someone could have changed the data or we're
2261 * reading a file written elsewhere. We only want to bypass doing no-op
2262 * read rpcs on newly created files (dv of 0) since only then we guarantee
2263 * that this chunk's data hasn't been filled by another client.
2265 size = AFS_CHUNKSIZE(abyte);
2266 if (aflags & 4) /* called from write */
2268 else /* called from read */
2269 tlen = tdc->validPos - abyte;
2270 Position = AFS_CHUNKTOBASE(chunk);
2271 afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE3, ICL_TYPE_INT32, tlen,
2272 ICL_TYPE_INT32, aflags, ICL_TYPE_OFFSET,
2273 ICL_HANDLE_OFFSET(abyte), ICL_TYPE_OFFSET,
2274 ICL_HANDLE_OFFSET(Position));
2275 if ((aflags & 4) && (hiszero(avc->f.m.DataVersion)))
2277 if ((AFS_CHUNKTOBASE(chunk) >= avc->f.m.Length) ||
2278 ((aflags & 4) && (abyte == Position) && (tlen >= size)))
2279 overWriteWholeChunk = 1;
2280 if (doAdjustSize || overWriteWholeChunk) {
2281 #if defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
2283 #ifdef AFS_SGI64_ENV
2286 #else /* AFS_SGI64_ENV */
2289 #endif /* AFS_SGI64_ENV */
2290 #else /* AFS_SGI_ENV */
2293 #endif /* AFS_SGI_ENV */
2294 if (AFS_CHUNKTOBASE(chunk) + adjustsize >= avc->f.m.Length &&
2295 #else /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
2296 #if defined(AFS_SUN5_ENV)
2297 if ((doAdjustSize || (AFS_CHUNKTOBASE(chunk) >= avc->f.m.Length)) &&
2299 if (AFS_CHUNKTOBASE(chunk) >= avc->f.m.Length &&
2301 #endif /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
2302 !afs_IsDCacheFresh(tdc, avc))
2303 doReallyAdjustSize = 1;
2305 if (doReallyAdjustSize || overWriteWholeChunk) {
2306 /* no data in file to read at this position */
2307 UpgradeSToWLock(&tdc->lock, 607);
2308 file = afs_CFileOpen(&tdc->f.inode);
2310 afs_CFileTruncate(file, 0);
2311 afs_CFileClose(file);
2312 afs_AdjustSize(tdc, 0);
2313 hset(tdc->f.versionNo, avc->f.m.DataVersion);
2314 tdc->dflags |= DFEntryMod;
2316 ConvertWToSLock(&tdc->lock);
2321 * We must read in the whole chunk if the version number doesn't
2325 /* don't need data, just a unique dcache entry */
2326 ObtainWriteLock(&afs_xdcache, 608);
2327 hset(afs_indexTimes[tdc->index], afs_indexCounter);
2328 hadd32(afs_indexCounter, 1);
2329 ReleaseWriteLock(&afs_xdcache);
2331 updateV2DC(setLocks, avc, tdc, 553);
2332 if (vType(avc) == VDIR)
2335 *aoffset = AFS_CHUNKOFFSET(abyte);
2336 if (tdc->validPos < abyte)
2337 *alen = (afs_size_t) 0;
2339 *alen = tdc->validPos - abyte;
2340 ReleaseSharedLock(&tdc->lock);
2343 ReleaseWriteLock(&avc->lock);
2345 ReleaseReadLock(&avc->lock);
2347 return tdc; /* check if we're done */
2352 * avc->lock(R) if setLocks && !slowPass
2353 * avc->lock(W) if !setLocks || slowPass
2356 osi_Assert((setLocks && !slowPass) || WriteLocked(&avc->lock));
2358 setNewCallback = setVcacheStatus = 0;
2362 * avc->lock(R) if setLocks && !slowPass
2363 * avc->lock(W) if !setLocks || slowPass
2366 if (!afs_IsDCacheFresh(tdc, avc) && !overWriteWholeChunk) {
2368 * Version number mismatch.
2371 * If we are disconnected, then we can't do much of anything
2372 * because the data doesn't match the file.
2374 if (AFS_IS_DISCONNECTED) {
2375 ReleaseSharedLock(&tdc->lock);
2378 ReleaseWriteLock(&avc->lock);
2380 ReleaseReadLock(&avc->lock);
2382 /* Flush the Dcache */
2387 UpgradeSToWLock(&tdc->lock, 609);
2390 * If data ever existed for this vnode, and this is a text object,
2391 * do some clearing. Now, you'd think you need only do the flush
2392 * when VTEXT is on, but VTEXT is turned off when the text object
2393 * is freed, while pages are left lying around in memory marked
2394 * with this vnode. If we would reactivate (create a new text
2395 * object from) this vnode, we could easily stumble upon some of
2396 * these old pages in pagein. So, we always flush these guys.
2397 * Sun has a wonderful lack of useful invariants in this system.
2399 * avc->flushDV is the data version # of the file at the last text
2400 * flush. Clearly, at least, we don't have to flush the file more
2401 * often than it changes
2403 if (hcmp(avc->flushDV, avc->f.m.DataVersion) < 0) {
2405 * By here, the cache entry is always write-locked. We can
2406 * deadlock if we call osi_Flush with the cache entry locked...
2407 * Unlock the dcache too.
2409 ReleaseWriteLock(&tdc->lock);
2410 if (setLocks && !slowPass)
2411 ReleaseReadLock(&avc->lock);
2413 ReleaseWriteLock(&avc->lock);
2417 * Call osi_FlushPages in open, read/write, and map, since it
2418 * is too hard here to figure out if we should lock the
2421 if (setLocks && !slowPass)
2422 ObtainReadLock(&avc->lock);
2424 ObtainWriteLock(&avc->lock, 66);
2425 ObtainWriteLock(&tdc->lock, 610);
2430 * avc->lock(R) if setLocks && !slowPass
2431 * avc->lock(W) if !setLocks || slowPass
2435 /* Watch for standard race condition around osi_FlushText */
2436 if (afs_IsDCacheFresh(tdc, avc)) {
2437 updateV2DC(setLocks, avc, tdc, 569); /* set hint */
2438 afs_stats_cmperf.dcacheHits++;
2439 ConvertWToSLock(&tdc->lock);
2443 /* Sleep here when cache needs to be drained. */
2444 if (setLocks && !slowPass
2445 && (afs_blocksUsed >
2446 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks))) {
2447 /* Make sure truncate daemon is running */
2448 afs_MaybeWakeupTruncateDaemon();
2449 ObtainWriteLock(&tdc->tlock, 614);
2450 tdc->refCount--; /* we'll re-obtain the dcache when we re-try. */
2451 ReleaseWriteLock(&tdc->tlock);
2452 ReleaseWriteLock(&tdc->lock);
2453 ReleaseReadLock(&avc->lock);
2454 while ((afs_blocksUsed - afs_blocksDiscarded) >
2455 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
2456 afs_MaybeWaitForCacheDrain();
2458 afs_MaybeFreeDiscardedDCache();
2459 /* need to check if someone else got the chunk first. */
2460 goto RetryGetDCache;
2463 Position = AFS_CHUNKBASE(abyte);
2464 if (vType(avc) == VDIR) {
2465 size = avc->f.m.Length;
2466 if (size > tdc->f.chunkBytes) {
2467 /* pre-reserve space for file */
2468 afs_AdjustSize(tdc, size);
2470 size = 999999999; /* max size for transfer */
2472 afs_size_t maxGoodLength;
2474 /* estimate how much data we're expecting back from the server,
2475 * and reserve space in the dcache entry for it */
2477 maxGoodLength = avc->f.m.Length;
2478 if (avc->f.truncPos < maxGoodLength)
2479 maxGoodLength = avc->f.truncPos;
2481 size = AFS_CHUNKSIZE(abyte); /* expected max size */
2482 if (Position > maxGoodLength) { /* If we're beyond EOF */
2484 } else if (Position + size > maxGoodLength) {
2485 size = maxGoodLength - Position;
2487 osi_Assert(size >= 0);
2489 if (size > tdc->f.chunkBytes) {
2490 /* pre-reserve estimated space for file */
2491 afs_AdjustSize(tdc, size); /* changes chunkBytes */
2495 /* For the actual fetch, do not limit the request to the
2496 * length of the file. If this results in a read past EOF on
2497 * the server, the server will just reply with less data than
2498 * requested. If we limit ourselves to only requesting data up
2499 * to the avc file length, we open ourselves up to races if the
2500 * file is extended on the server at about the same time.
2502 * However, we must restrict ourselves to the avc->f.truncPos
2503 * length, since this represents an outstanding local
2504 * truncation of the file that will be committed to the
2505 * fileserver when we actually write the fileserver contents.
2506 * If we do not restrict the fetch length based on
2507 * avc->f.truncPos, a different truncate operation extending
2508 * the file length could cause the old data after
2509 * avc->f.truncPos to reappear, instead of extending the file
2510 * with NUL bytes. */
2511 size = AFS_CHUNKSIZE(abyte);
2512 if (Position > avc->f.truncPos) {
2514 } else if (Position + size > avc->f.truncPos) {
2515 size = avc->f.truncPos - Position;
2517 osi_Assert(size >= 0);
2520 if (afs_mariner && !tdc->f.chunk)
2521 afs_MarinerLog("fetch$Fetching", avc); /* , Position, size, afs_indexCounter ); */
2523 * Right now, we only have one tool, and it's a hammer. So, we
2524 * fetch the whole file.
2526 DZap(tdc); /* pages in cache may be old */
2527 file = afs_CFileOpen(&tdc->f.inode);
2529 /* We can't access the file in the disk cache backing this dcache;
2531 ReleaseWriteLock(&tdc->lock);
2536 afs_RemoveVCB(&avc->f.fid);
2537 tdc->f.states |= DWriting;
2538 tdc->dflags |= DFFetching;
2539 tdc->validPos = Position; /* which is AFS_CHUNKBASE(abyte) */
2540 if (tdc->mflags & DFFetchReq) {
2541 tdc->mflags &= ~DFFetchReq;
2542 if (afs_osi_Wakeup(&tdc->validPos) == 0)
2543 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
2544 __FILE__, ICL_TYPE_INT32, __LINE__,
2545 ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
2548 tsmall = osi_AllocLargeSpace(sizeof(struct afs_FetchOutput));
2549 setVcacheStatus = 0;
2552 * Remember if we are doing the reading from a replicated volume,
2553 * and how many times we've zipped around the fetch/analyze loop.
2555 fromReplica = (avc->f.states & CRO) ? 1 : 0;
2557 accP = &(afs_stats_cmfullperf.accessinf);
2559 (accP->replicatedRefs)++;
2561 (accP->unreplicatedRefs)++;
2562 #endif /* AFS_NOSTATS */
2563 /* this is a cache miss */
2564 afs_Trace4(afs_iclSetp, CM_TRACE_FETCHPROC, ICL_TYPE_POINTER, avc,
2565 ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
2566 ICL_HANDLE_OFFSET(Position), ICL_TYPE_INT32, size);
2569 afs_stats_cmperf.dcacheMisses++;
2572 * Dynamic root support: fetch data from local memory.
2574 if (afs_IsDynroot(avc)) {
2578 afs_GetDynroot(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2580 dynrootDir += Position;
2581 dynrootLen -= Position;
2582 if (size > dynrootLen)
2586 code = afs_CFileWrite(file, 0, dynrootDir, size);
2594 tdc->validPos = Position + size;
2595 afs_CFileTruncate(file, size); /* prune it */
2596 } else if (afs_IsDynrootMount(avc)) {
2600 afs_GetDynrootMount(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2602 dynrootDir += Position;
2603 dynrootLen -= Position;
2604 if (size > dynrootLen)
2608 code = afs_CFileWrite(file, 0, dynrootDir, size);
2616 tdc->validPos = Position + size;
2617 afs_CFileTruncate(file, size); /* prune it */
2620 * Not a dynamic vnode: do the real fetch.
2625 * avc->lock(R) if setLocks && !slowPass
2626 * avc->lock(W) if !setLocks || slowPass
2630 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK, &rxconn);
2635 (accP->numReplicasAccessed)++;
2637 #endif /* AFS_NOSTATS */
2638 if (!setLocks || slowPass) {
2639 avc->callback = tc->parent->srvr->server;
2641 newCallback = tc->parent->srvr->server;
2645 code = afs_CacheFetchProc(tc, rxconn, file, Position, tdc,
2651 /* callback could have been broken (or expired) in a race here,
2652 * but we return the data anyway. It's as good as we knew about
2653 * when we started. */
2655 * validPos is updated by CacheFetchProc, and can only be
2656 * modifed under a dcache write lock, which we've blocked out
2660 size = tdc->validPos - Position; /* actual segment size */
2663 afs_CFileTruncate(file, size); /* prune it */
2665 /* Check that the amount of data that we fetched for the
2666 * dcache makes sense. */
2667 FillInt64(length, tsmall->OutStatus.Length_hi, tsmall->OutStatus.Length);
2668 if (!IsDCacheSizeOK(tdc, avc, size,
2670 tsmall->OutStatus.DataVersion, 1)) {
2675 if (!setLocks || slowPass) {
2676 afs_StaleVCacheFlags(avc, AFS_STALEVC_CLEARCB, CUnique);
2678 /* Something lost. Forget about performance, and go
2679 * back with a vcache write lock.
2681 afs_CFileTruncate(file, 0);
2682 afs_AdjustSize(tdc, 0);
2683 afs_CFileClose(file);
2684 osi_FreeLargeSpace(tsmall);
2686 ReleaseWriteLock(&tdc->lock);
2691 * Call afs_Analyze to manage the connection references
2692 * and handle the error code (possibly mark servers
2693 * down, etc). We are going to retry getting the
2694 * dcache regardless, so we just ignore the retry hint
2695 * returned by afs_Analyze on this call.
2697 (void)afs_Analyze(tc, rxconn, code, &avc->f.fid, areq,
2698 AFS_STATS_FS_RPCIDX_FETCHDATA, SHARED_LOCK, NULL);
2700 ReleaseReadLock(&avc->lock);
2703 goto RetryGetDCache;
2707 } while (afs_Analyze
2708 (tc, rxconn, code, &avc->f.fid, areq,
2709 AFS_STATS_FS_RPCIDX_FETCHDATA, SHARED_LOCK, NULL));
2713 * avc->lock(R) if setLocks && !slowPass
2714 * avc->lock(W) if !setLocks || slowPass
2720 * In the case of replicated access, jot down info on the number of
2721 * attempts it took before we got through or gave up.
2724 if (numFetchLoops <= 1)
2725 (accP->refFirstReplicaOK)++;
2726 if (numFetchLoops > accP->maxReplicasPerRef)
2727 accP->maxReplicasPerRef = numFetchLoops;
2729 #endif /* AFS_NOSTATS */
2731 tdc->dflags &= ~DFFetching;
2732 if (afs_osi_Wakeup(&tdc->validPos) == 0)
2733 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
2734 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
2735 tdc, ICL_TYPE_INT32, tdc->dflags);
2736 if (avc->execsOrWriters == 0)
2737 tdc->f.states &= ~DWriting;
2739 /* now, if code != 0, we have an error and should punt.
2740 * note that we have the vcache write lock, either because
2741 * !setLocks or slowPass.
2744 afs_CFileTruncate(file, 0);
2745 afs_AdjustSize(tdc, 0);
2746 afs_CFileClose(file);
2747 ZapDCE(tdc); /* sets DFEntryMod */
2748 if (vType(avc) == VDIR) {
2751 tdc->f.states &= ~(DRO|DBackup|DRW);
2752 afs_DCMoveBucket(tdc, 0, 0);
2753 ReleaseWriteLock(&tdc->lock);
2755 if (!afs_IsDynroot(avc)) {
2756 afs_StaleVCacheFlags(avc, 0, CUnique);
2759 * avc->lock(W); assert(!setLocks || slowPass)
2761 osi_Assert(!setLocks || slowPass);
2767 /* otherwise we copy in the just-fetched info */
2768 afs_CFileClose(file);
2769 afs_AdjustSize(tdc, size); /* new size */
2771 * Copy appropriate fields into vcache. Status is
2772 * copied later where we selectively acquire the
2773 * vcache write lock.
2776 afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2778 setVcacheStatus = 1;
2779 hset64(tdc->f.versionNo, tsmall->OutStatus.dataVersionHigh,
2780 tsmall->OutStatus.DataVersion);
2781 tdc->dflags |= DFEntryMod;
2782 afs_indexFlags[tdc->index] |= IFEverUsed;
2783 ConvertWToSLock(&tdc->lock);
2784 } /*Data version numbers don't match */
2787 * Data version numbers match.
2789 afs_stats_cmperf.dcacheHits++;
2790 } /*Data version numbers match */
2792 updateV2DC(setLocks, avc, tdc, 335); /* set hint */
2796 * avc->lock(R) if setLocks && !slowPass
2797 * avc->lock(W) if !setLocks || slowPass
2798 * tdc->lock(S) if tdc
2802 * See if this was a reference to a file in the local cell.
2804 if (afs_IsPrimaryCellNum(avc->f.fid.Cell))
2805 afs_stats_cmperf.dlocalAccesses++;
2807 afs_stats_cmperf.dremoteAccesses++;
2809 /* Fix up LRU info */
2812 ObtainWriteLock(&afs_xdcache, 602);
2813 hset(afs_indexTimes[tdc->index], afs_indexCounter);
2814 hadd32(afs_indexCounter, 1);
2815 ReleaseWriteLock(&afs_xdcache);
2817 /* return the data */
2818 if (vType(avc) == VDIR)
2821 *aoffset = AFS_CHUNKOFFSET(abyte);
2822 *alen = (tdc->f.chunkBytes - *aoffset);
2823 ReleaseSharedLock(&tdc->lock);
2828 * avc->lock(R) if setLocks && !slowPass
2829 * avc->lock(W) if !setLocks || slowPass
2832 /* Fix up the callback and status values in the vcache */
2834 if (setLocks && !slowPass) {
2837 * This is our dirty little secret to parallel fetches.
2838 * We don't write-lock the vcache while doing the fetch,
2839 * but potentially we'll need to update the vcache after
2840 * the fetch is done.
2842 * Drop the read lock and try to re-obtain the write
2843 * lock. If the vcache still has the same DV, it's
2844 * ok to go ahead and install the new data.
2846 afs_hyper_t currentDV, statusDV;
2848 hset(currentDV, avc->f.m.DataVersion);
2850 if (setNewCallback && avc->callback != newCallback)
2854 hset64(statusDV, tsmall->OutStatus.dataVersionHigh,
2855 tsmall->OutStatus.DataVersion);
2857 if (setVcacheStatus && avc->f.m.Length != tsmall->OutStatus.Length)
2859 if (setVcacheStatus && !hsame(currentDV, statusDV))
2863 ReleaseReadLock(&avc->lock);
2865 if (doVcacheUpdate) {
2866 ObtainWriteLock(&avc->lock, 615);
2867 if (!hsame(avc->f.m.DataVersion, currentDV)) {
2868 /* We lose. Someone will beat us to it. */
2870 ReleaseWriteLock(&avc->lock);
2875 /* With slow pass, we've already done all the updates */
2877 ReleaseWriteLock(&avc->lock);
2880 /* Check if we need to perform any last-minute fixes with a write-lock */
2881 if (!setLocks || doVcacheUpdate) {
2883 avc->callback = newCallback;
2884 if (tsmall && setVcacheStatus)
2885 afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2887 ReleaseWriteLock(&avc->lock);
2891 osi_FreeLargeSpace(tsmall);
2894 } /*afs_GetDCache */
2898 * afs_WriteThroughDSlots
2901 * Sweep through the dcache slots and write out any modified
2902 * in-memory data back on to our caching store.
2908 * The afs_xdcache is write-locked through this whole affair.
2911 afs_WriteThroughDSlots(void)
2914 afs_int32 i, touchedit = 0;
2917 struct afs_q DirtyQ, *tq;
2919 AFS_STATCNT(afs_WriteThroughDSlots);
2922 * Because of lock ordering, we can't grab dcache locks while
2923 * holding afs_xdcache. So we enter xdcache, get a reference
2924 * for every dcache entry, and exit xdcache.
2926 ObtainWriteLock(&afs_xdcache, 283);
2928 for (i = 0; i < afs_cacheFiles; i++) {
2929 tdc = afs_indexTable[i];
2931 /* Grab tlock in case the existing refcount isn't zero */
2932 if (tdc && !(afs_indexFlags[i] & (IFFree | IFDiscarded))) {
2933 ObtainWriteLock(&tdc->tlock, 623);
2935 ReleaseWriteLock(&tdc->tlock);
2937 QAdd(&DirtyQ, &tdc->dirty);
2940 ReleaseWriteLock(&afs_xdcache);
2943 * Now, for each dcache entry we found, check if it's dirty.
2944 * If so, get write-lock, get afs_xdcache, which protects
2945 * afs_cacheInodep, and flush it. Don't forget to put back
2949 #define DQTODC(q) ((struct dcache *)(((char *) (q)) - sizeof(struct afs_q)))
2951 for (tq = DirtyQ.prev; tq != &DirtyQ && code == 0; tq = QPrev(tq)) {
2953 if (tdc->dflags & DFEntryMod) {
2956 wrLock = (0 == NBObtainWriteLock(&tdc->lock, 619));
2958 /* Now that we have the write lock, double-check */
2959 if (wrLock && (tdc->dflags & DFEntryMod)) {
2960 tdc->dflags &= ~DFEntryMod;
2961 ObtainWriteLock(&afs_xdcache, 620);
2962 code = afs_WriteDCache(tdc, 1);
2963 ReleaseWriteLock(&afs_xdcache);
2965 /* We didn't successfully write out the dslot; make sure we
2966 * try again later */
2967 tdc->dflags |= DFEntryMod;
2973 ReleaseWriteLock(&tdc->lock);
2983 ObtainWriteLock(&afs_xdcache, 617);
2984 if (!touchedit && (cacheDiskType != AFS_FCACHE_TYPE_MEM)) {
2985 /* Touch the file to make sure that the mtime on the file is kept
2986 * up-to-date to avoid losing cached files on cold starts because
2987 * their mtime seems old...
2989 struct afs_fheader theader;
2991 afs_InitFHeader(&theader);
2992 afs_osi_Write(afs_cacheInodep, 0, &theader, sizeof(theader));
2994 ReleaseWriteLock(&afs_xdcache);
3002 * Return a pointer to an freshly initialized dcache entry using
3003 * a memory-based cache. The tlock will be read-locked.
3006 * aslot : Dcache slot to look at.
3007 * type : What 'type' of dslot to get; see the dslot_state enum
3010 * Must be called with afs_xdcache write-locked.
3014 afs_MemGetDSlot(afs_int32 aslot, dslot_state type)
3019 AFS_STATCNT(afs_MemGetDSlot);
3020 if (CheckLock(&afs_xdcache) != -1)
3021 osi_Panic("getdslot nolock");
3022 if (aslot < 0 || aslot >= afs_cacheFiles)
3023 osi_Panic("getdslot slot %d (of %d)", aslot, afs_cacheFiles);
3024 tdc = afs_indexTable[aslot];
3026 QRemove(&tdc->lruq); /* move to queue head */
3027 QAdd(&afs_DLRU, &tdc->lruq);
3028 /* We're holding afs_xdcache, but get tlock in case refCount != 0 */
3029 ObtainWriteLock(&tdc->tlock, 624);
3031 ConvertWToRLock(&tdc->tlock);
3035 /* if we got here, the given slot is not in memory in our list of known
3036 * slots. for memcache, the only place a dslot can exist is in memory, so
3037 * if the caller is expecting to get back a known dslot, and we've reached
3038 * here, something is very wrong. DSLOT_NEW is the only type of dslot that
3039 * may not exist; for all others, the caller assumes the given dslot
3040 * already exists. so, 'type' had better be DSLOT_NEW here, or something is
3042 osi_Assert(type == DSLOT_NEW);
3044 if (!afs_freeDSList)
3045 afs_GetDownDSlot(4);
3046 if (!afs_freeDSList) {
3047 /* none free, making one is better than a panic */
3048 afs_stats_cmperf.dcacheXAllocs++; /* count in case we have a leak */
3049 tdc = afs_osi_Alloc(sizeof(struct dcache));
3050 osi_Assert(tdc != NULL);
3051 #ifdef KERNEL_HAVE_PIN
3052 pin((char *)tdc, sizeof(struct dcache)); /* XXX */
3055 tdc = afs_freeDSList;
3056 afs_freeDSList = (struct dcache *)tdc->lruq.next;
3059 tdc->dflags = 0; /* up-to-date, not in free q */
3061 QAdd(&afs_DLRU, &tdc->lruq);
3062 if (tdc->lruq.prev == &tdc->lruq)
3063 osi_Panic("lruq 3");
3065 /* initialize entry */
3066 tdc->f.fid.Cell = 0;
3067 tdc->f.fid.Fid.Volume = 0;
3069 hones(tdc->f.versionNo);
3070 tdc->f.inode.mem = aslot;
3071 tdc->dflags |= DFEntryMod;
3074 afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
3077 osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
3078 osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
3079 osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
3082 AFS_RWLOCK_INIT(&tdc->lock, "dcache lock");
3083 AFS_RWLOCK_INIT(&tdc->tlock, "dcache tlock");
3084 AFS_RWLOCK_INIT(&tdc->mflock, "dcache flock");
3085 ObtainReadLock(&tdc->tlock);
3087 afs_indexTable[aslot] = tdc;
3090 } /*afs_MemGetDSlot */
3093 LogCacheError(int aslot, int off, int code, int target_size)
3095 struct osi_stat tstat;
3098 if (afs_osi_Stat(afs_cacheInodep, &tstat)) {
3102 procname = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
3103 if (procname != NULL) {
3104 osi_procname(procname, AFS_SMALLOCSIZ);
3105 procname[AFS_SMALLOCSIZ-1] = '\0';
3108 afs_warn("afs: disk cache read error in CacheItems slot %d "
3109 "off %d/%d code %d/%d pid %d (%s)\n",
3110 aslot, off, (int)tstat.size, code, target_size,
3111 (int)MyPidxx2Pid(MyPidxx),
3112 procname ? procname : "");
3114 if (procname != NULL) {
3115 osi_FreeSmallSpace(procname);
3120 unsigned int last_error = 0, lasterrtime = 0;
3126 * Return a pointer to an freshly initialized dcache entry using
3127 * a UFS-based disk cache. The dcache tlock will be read-locked.
3130 * aslot : Dcache slot to look at.
3131 * type : What 'type' of dslot to get; see the dslot_state enum
3134 * afs_xdcache lock write-locked.
3137 afs_UFSGetDSlot(afs_int32 aslot, dslot_state type)
3145 AFS_STATCNT(afs_UFSGetDSlot);
3146 if (CheckLock(&afs_xdcache) != -1)
3147 osi_Panic("getdslot nolock");
3148 if (aslot < 0 || aslot >= afs_cacheFiles)
3149 osi_Panic("getdslot slot %d (of %d)", aslot, afs_cacheFiles);
3150 tdc = afs_indexTable[aslot];
3152 QRemove(&tdc->lruq); /* move to queue head */
3153 QAdd(&afs_DLRU, &tdc->lruq);
3154 /* Grab tlock in case refCount != 0 */
3155 ObtainWriteLock(&tdc->tlock, 625);
3157 ConvertWToRLock(&tdc->tlock);
3161 /* otherwise we should read it in from the cache file */
3162 if (!afs_freeDSList)
3163 afs_GetDownDSlot(4);
3164 if (!afs_freeDSList) {
3165 /* none free, making one is better than a panic */
3166 afs_stats_cmperf.dcacheXAllocs++; /* count in case we have a leak */
3167 tdc = afs_osi_Alloc(sizeof(struct dcache));
3168 osi_Assert(tdc != NULL);
3169 #ifdef KERNEL_HAVE_PIN
3170 pin((char *)tdc, sizeof(struct dcache)); /* XXX */
3173 tdc = afs_freeDSList;
3174 afs_freeDSList = (struct dcache *)tdc->lruq.next;
3177 tdc->dflags = 0; /* up-to-date, not in free q */
3179 QAdd(&afs_DLRU, &tdc->lruq);
3180 if (tdc->lruq.prev == &tdc->lruq)
3181 osi_Panic("lruq 3");
3184 * Seek to the aslot'th entry and read it in.
3186 off = sizeof(struct fcache)*aslot + sizeof(struct afs_fheader);
3188 afs_osi_Read(afs_cacheInodep,
3189 off, (char *)(&tdc->f),
3190 sizeof(struct fcache));
3192 if (code != sizeof(struct fcache)) {
3194 #if defined(KERNEL_HAVE_UERROR)
3195 last_error = getuerror();
3199 lasterrtime = osi_Time();
3200 if (type != DSLOT_NEW) {
3201 /* If we are requesting a non-DSLOT_NEW slot, this is an error.
3202 * non-DSLOT_NEW slots are supposed to already exist, so if we
3203 * failed to read in the slot, something is wrong. */
3204 LogCacheError(aslot, off, code, sizeof(struct fcache));
3206 /* put tdc back on the free dslot list */
3207 QRemove(&tdc->lruq);
3208 tdc->index = NULLIDX;
3209 tdc->lruq.next = (struct afs_q *)afs_freeDSList;
3210 afs_freeDSList = tdc;
3214 if (!afs_CellNumValid(tdc->f.fid.Cell)) {
3216 if (type == DSLOT_VALID) {
3217 osi_Panic("afs: needed valid dcache but index %d off %d has "
3218 "invalid cell num %d\n",
3219 (int)aslot, off, (int)tdc->f.fid.Cell);
3223 if (type == DSLOT_VALID && tdc->f.fid.Fid.Volume == 0) {
3224 osi_Panic("afs: invalid zero-volume dcache entry at slot %d off %d",
3228 if (type == DSLOT_UNUSED) {
3229 /* the requested dslot is known to exist, but contain invalid data
3230 * (this happens when we're using a dslot from the free or discard
3231 * list). be sure not to re-use the data in it, so force invalidation.
3237 tdc->f.fid.Cell = 0;
3238 tdc->f.fid.Fid.Volume = 0;
3240 hones(tdc->f.versionNo);
3241 tdc->dflags |= DFEntryMod;
3242 afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
3243 tdc->f.states &= ~(DRO|DBackup|DRW);
3244 afs_DCMoveBucket(tdc, 0, 0);
3246 if (tdc->f.states & DRO) {
3247 afs_DCMoveBucket(tdc, 0, 2);
3248 } else if (tdc->f.states & DBackup) {
3249 afs_DCMoveBucket(tdc, 0, 1);
3251 afs_DCMoveBucket(tdc, 0, 1);
3256 if (tdc->f.chunk >= 0)
3257 tdc->validPos = AFS_CHUNKTOBASE(tdc->f.chunk) + tdc->f.chunkBytes;
3262 osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
3263 osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
3264 osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
3267 AFS_RWLOCK_INIT(&tdc->lock, "dcache lock");
3268 AFS_RWLOCK_INIT(&tdc->tlock, "dcache tlock");
3269 AFS_RWLOCK_INIT(&tdc->mflock, "dcache flock");
3270 ObtainReadLock(&tdc->tlock);
3273 * If we didn't read into a temporary dcache region, update the
3274 * slot pointer table.
3276 afs_indexTable[aslot] = tdc;
3279 } /*afs_UFSGetDSlot */
3284 * Write a particular dcache entry back to its home in the
3287 * \param adc Pointer to the dcache entry to write.
3288 * \param atime If true, set the modtime on the file to the current time.
3290 * \note Environment:
3291 * Must be called with the afs_xdcache lock at least read-locked,
3292 * and dcache entry at least read-locked.
3293 * The reference count is not changed.
3297 afs_WriteDCache(struct dcache *adc, int atime)
3301 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
3303 AFS_STATCNT(afs_WriteDCache);
3304 osi_Assert(WriteLocked(&afs_xdcache));
3306 adc->f.modTime = osi_Time();
3308 if ((afs_indexFlags[adc->index] & (IFFree | IFDiscarded)) == 0 &&
3309 adc->f.fid.Fid.Volume == 0) {
3310 /* If a dcache slot is not on the free or discard list, it must be
3311 * in the hash table. Thus, the volume must be non-zero, since that
3312 * is how we determine whether or not to unhash the entry when kicking
3313 * it out of the cache. Do this check now, since otherwise this can
3314 * cause hash table corruption and a panic later on after we read the
3316 osi_Panic("afs_WriteDCache zero volume index %d flags 0x%x\n",
3317 adc->index, (unsigned)afs_indexFlags[adc->index]);
3321 * Seek to the right dcache slot and write the in-memory image out to disk.
3323 afs_cellname_write();
3325 afs_osi_Write(afs_cacheInodep,
3326 sizeof(struct fcache) * adc->index +
3327 sizeof(struct afs_fheader), (char *)(&adc->f),
3328 sizeof(struct fcache));
3329 if (code != sizeof(struct fcache)) {
3330 afs_warn("afs: failed to write to CacheItems off %ld code %d/%d\n",
3331 (long)(sizeof(struct fcache) * adc->index + sizeof(struct afs_fheader)),
3332 (int)code, (int)sizeof(struct fcache));
3341 * Wake up users of a particular file waiting for stores to take
3344 * \param avc Ptr to related vcache entry.
3346 * \note Environment:
3347 * Nothing interesting.
3350 afs_wakeup(struct vcache *avc)
3353 struct brequest *tb;
3355 AFS_STATCNT(afs_wakeup);
3356 for (i = 0; i < NBRS; i++, tb++) {
3357 /* if request is valid and for this file, we've found it */
3358 if (tb->refCount > 0 && avc == tb->vc) {
3361 * If CSafeStore is on, then we don't awaken the guy
3362 * waiting for the store until the whole store has finished.
3363 * Otherwise, we do it now. Note that if CSafeStore is on,
3364 * the BStore routine actually wakes up the user, instead
3366 * I think this is redundant now because this sort of thing
3367 * is already being handled by the higher-level code.
3369 if ((avc->f.states & CSafeStore) == 0) {
3370 tb->code_raw = tb->code_checkcode = 0;
3371 tb->flags |= BUVALID;
3372 if (tb->flags & BUWAIT) {
3373 tb->flags &= ~BUWAIT;
3384 * Given a file name and inode, set up that file to be an
3385 * active member in the AFS cache. This also involves checking
3386 * the usability of its data.
3388 * \param afile Name of the cache file to initialize.
3389 * \param ainode Inode of the file.
3391 * \note Environment:
3392 * This function is called only during initialization.
3395 afs_InitCacheFile(char *afile, ino_t ainode)
3400 struct osi_file *tfile;
3401 struct osi_stat tstat;
3404 AFS_STATCNT(afs_InitCacheFile);
3405 index = afs_stats_cmperf.cacheNumEntries;
3406 if (index >= afs_cacheFiles)
3409 ObtainWriteLock(&afs_xdcache, 282);
3410 tdc = afs_GetNewDSlot(index);
3411 ReleaseReadLock(&tdc->tlock);
3412 ReleaseWriteLock(&afs_xdcache);
3414 ObtainWriteLock(&tdc->lock, 621);
3415 ObtainWriteLock(&afs_xdcache, 622);
3416 if (!afile && !ainode) {
3421 code = afs_LookupInodeByPath(afile, &tdc->f.inode.ufs, NULL);
3423 ReleaseWriteLock(&afs_xdcache);
3424 ReleaseWriteLock(&tdc->lock);
3429 /* Add any other 'complex' inode types here ... */
3430 #if !defined(AFS_LINUX26_ENV) && !defined(AFS_CACHE_VNODE_PATH)
3431 tdc->f.inode.ufs = ainode;
3433 osi_Panic("Can't init cache with inode numbers when complex inodes are "
3438 if ((tdc->f.states & DWriting) || tdc->f.fid.Fid.Volume == 0)
3440 tfile = osi_UFSOpen(&tdc->f.inode);
3442 ReleaseWriteLock(&afs_xdcache);
3443 ReleaseWriteLock(&tdc->lock);
3448 code = afs_osi_Stat(tfile, &tstat);
3450 osi_Panic("initcachefile stat");
3453 * If file size doesn't match the cache info file, it's probably bad.
3455 if (tdc->f.chunkBytes != tstat.size)
3458 * If file changed within T (120?) seconds of cache info file, it's
3459 * probably bad. In addition, if slot changed within last T seconds,
3460 * the cache info file may be incorrectly identified, and so slot
3463 if (cacheInfoModTime < tstat.mtime + 120)
3465 if (cacheInfoModTime < tdc->f.modTime + 120)
3467 /* In case write through is behind, make sure cache items entry is
3468 * at least as new as the chunk.
3470 if (tdc->f.modTime < tstat.mtime)
3473 tdc->f.chunkBytes = 0;
3476 tdc->f.fid.Fid.Volume = 0; /* not in the hash table */
3477 if (tfile && tstat.size != 0)
3478 osi_UFSTruncate(tfile, 0);
3479 tdc->f.states &= ~(DRO|DBackup|DRW);
3480 afs_DCMoveBucket(tdc, 0, 0);
3481 /* put entry in free cache slot list */
3482 afs_dvnextTbl[tdc->index] = afs_freeDCList;
3483 afs_freeDCList = index;
3485 afs_indexFlags[index] |= IFFree;
3486 afs_indexUnique[index] = 0;
3489 * We must put this entry in the appropriate hash tables.
3490 * Note that i is still set from the above DCHash call
3492 code = DCHash(&tdc->f.fid, tdc->f.chunk);
3493 afs_dcnextTbl[tdc->index] = afs_dchashTbl[code];
3494 afs_dchashTbl[code] = tdc->index;
3495 code = DVHash(&tdc->f.fid);
3496 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[code];
3497 afs_dvhashTbl[code] = tdc->index;
3498 afs_AdjustSize(tdc, tstat.size); /* adjust to new size */
3500 /* has nontrivial amt of data */
3501 afs_indexFlags[index] |= IFEverUsed;
3502 afs_stats_cmperf.cacheFilesReused++;
3504 * Initialize index times to file's mod times; init indexCounter
3507 hset32(afs_indexTimes[index], tstat.atime);
3508 if (hgetlo(afs_indexCounter) < tstat.atime) {
3509 hset32(afs_indexCounter, tstat.atime);
3511 afs_indexUnique[index] = tdc->f.fid.Fid.Unique;
3512 } /*File is not bad */
3515 osi_UFSClose(tfile);
3516 tdc->f.states &= ~DWriting;
3517 tdc->dflags &= ~DFEntryMod;
3518 /* don't set f.modTime; we're just cleaning up */
3519 osi_Assert(afs_WriteDCache(tdc, 0) == 0);
3520 ReleaseWriteLock(&afs_xdcache);
3521 ReleaseWriteLock(&tdc->lock);
3523 afs_stats_cmperf.cacheNumEntries++;
3528 /*Max # of struct dcache's resident at any time*/
3530 * If 'dchint' is enabled then in-memory dcache min is increased because of