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;
99 afs_int32 afs_dcentries; /*!< In-memory dcache entries */
102 int dcacheDisabled = 0;
104 struct afs_cacheOps afs_UfsCacheOps = {
105 #ifndef HAVE_STRUCT_LABEL_SUPPORT
118 .truncate = osi_UFSTruncate,
119 .fread = afs_osi_Read,
120 .fwrite = afs_osi_Write,
121 .close = osi_UFSClose,
122 .vreadUIO = afs_UFSReadUIO,
123 .vwriteUIO = afs_UFSWriteUIO,
124 .GetDSlot = afs_UFSGetDSlot,
125 .GetVolSlot = afs_UFSGetVolSlot,
126 .HandleLink = afs_UFSHandleLink,
130 struct afs_cacheOps afs_MemCacheOps = {
131 #ifndef HAVE_STRUCT_LABEL_SUPPORT
133 afs_MemCacheTruncate,
143 .open = afs_MemCacheOpen,
144 .truncate = afs_MemCacheTruncate,
145 .fread = afs_MemReadBlk,
146 .fwrite = afs_MemWriteBlk,
147 .close = afs_MemCacheClose,
148 .vreadUIO = afs_MemReadUIO,
149 .vwriteUIO = afs_MemWriteUIO,
150 .GetDSlot = afs_MemGetDSlot,
151 .GetVolSlot = afs_MemGetVolSlot,
152 .HandleLink = afs_MemHandleLink,
156 int cacheDiskType; /*Type of backing disk for cache */
157 struct afs_cacheOps *afs_cacheType;
161 * The PFlush algorithm makes use of the fact that Fid.Unique is not used in
162 * below hash algorithms. Change it if need be so that flushing algorithm
163 * doesn't move things from one hash chain to another.
165 /*Vnode, Chunk -> Hash table index */
166 int DCHash(struct VenusFid *fid, afs_int32 chunk)
170 buf[0] = fid->Fid.Volume;
171 buf[1] = fid->Fid.Vnode;
173 return opr_jhash(buf, 3, 0) & (afs_dhashsize - 1);
175 /*Vnode -> Other hash table index */
176 int DVHash(struct VenusFid *fid)
178 return opr_jhash_int2(fid->Fid.Volume, fid->Fid.Vnode, 0) &
183 * Where is this vcache's entry associated dcache located/
184 * \param avc The vcache entry.
185 * \return Bucket index:
190 afs_DCGetBucket(struct vcache *avc)
195 /* This should be replaced with some sort of user configurable function */
196 if (avc->f.states & CRO) {
198 } else if (avc->f.states & CBackup) {
208 * Readjust a dcache's size.
210 * \param adc The dcache to be adjusted.
211 * \param oldSize Old size for the dcache.
212 * \param newSize The new size to be adjusted to.
216 afs_DCAdjustSize(struct dcache *adc, afs_int32 oldSize, afs_int32 newSize)
218 afs_int32 adjustSize = newSize - oldSize;
226 afs_blocksUsed_0 += adjustSize;
227 afs_stats_cmperf.cacheBucket0_Discarded += oldSize;
230 afs_blocksUsed_1 += adjustSize;
231 afs_stats_cmperf.cacheBucket1_Discarded += oldSize;
234 afs_blocksUsed_2 += adjustSize;
235 afs_stats_cmperf.cacheBucket2_Discarded += oldSize;
243 * Move a dcache from one bucket to another.
245 * \param adc Operate on this dcache.
246 * \param size Size in bucket (?).
247 * \param newBucket Destination bucket.
251 afs_DCMoveBucket(struct dcache *adc, afs_int32 size, afs_int32 newBucket)
256 /* Substract size from old bucket. */
260 afs_blocksUsed_0 -= size;
263 afs_blocksUsed_1 -= size;
266 afs_blocksUsed_2 -= size;
270 /* Set new bucket and increase destination bucket size. */
271 adc->bucket = newBucket;
276 afs_blocksUsed_0 += size;
279 afs_blocksUsed_1 += size;
282 afs_blocksUsed_2 += size;
290 * Init split caches size.
295 afs_blocksUsed_0 = afs_blocksUsed_1 = afs_blocksUsed_2 = 0;
304 afs_DCWhichBucket(afs_int32 phase, afs_int32 bucket)
309 afs_pct1 = afs_blocksUsed_1 / (afs_cacheBlocks / 100);
310 afs_pct2 = afs_blocksUsed_2 / (afs_cacheBlocks / 100);
312 /* Short cut: if we don't know about it, try to kill it */
313 if (phase < 2 && afs_blocksUsed_0)
316 if (afs_pct1 > afs_tpct1)
318 if (afs_pct2 > afs_tpct2)
320 return 0; /* unlikely */
325 * Warn about failing to store a file.
327 * \param acode Associated error code.
328 * \param avolume Volume involved.
329 * \param aflags How to handle the output:
330 * aflags & 1: Print out on console
331 * aflags & 2: Print out on controlling tty
333 * \note Environment: Call this from close call when vnodeops is RCS unlocked.
337 afs_StoreWarn(afs_int32 acode, afs_int32 avolume,
340 static char problem_fmt[] =
341 "afs: failed to store file in volume %d (%s)\n";
342 static char problem_fmt_w_error[] =
343 "afs: failed to store file in volume %d (error %d)\n";
344 static char netproblems[] = "network problems";
345 static char partfull[] = "partition full";
346 static char overquota[] = "over quota";
348 AFS_STATCNT(afs_StoreWarn);
354 afs_warn(problem_fmt, avolume, netproblems);
356 afs_warnuser(problem_fmt, avolume, netproblems);
357 } else if (acode == ENOSPC) {
362 afs_warn(problem_fmt, avolume, partfull);
364 afs_warnuser(problem_fmt, avolume, partfull);
367 /* EDQUOT doesn't exist on solaris and won't be sent by the server.
368 * Instead ENOSPC will be sent...
370 if (acode == EDQUOT) {
375 afs_warn(problem_fmt, avolume, overquota);
377 afs_warnuser(problem_fmt, avolume, overquota);
385 afs_warn(problem_fmt_w_error, avolume, acode);
387 afs_warnuser(problem_fmt_w_error, avolume, acode);
392 * Try waking up truncation daemon, if it's worth it.
395 afs_MaybeWakeupTruncateDaemon(void)
397 if (!afs_CacheTooFull && afs_CacheIsTooFull()) {
398 afs_CacheTooFull = 1;
399 if (!afs_TruncateDaemonRunning)
400 afs_osi_Wakeup((int *)afs_CacheTruncateDaemon);
401 } else if (!afs_TruncateDaemonRunning
402 && afs_blocksDiscarded > CM_MAXDISCARDEDCHUNKS) {
403 afs_osi_Wakeup((int *)afs_CacheTruncateDaemon);
410 * Keep statistics on run time for afs_CacheTruncateDaemon. This is a
411 * struct so we need only export one symbol for AIX.
413 static struct CTD_stats {
414 osi_timeval_t CTD_beforeSleep;
415 osi_timeval_t CTD_afterSleep;
416 osi_timeval_t CTD_sleepTime;
417 osi_timeval_t CTD_runTime;
421 u_int afs_min_cache = 0;
424 * If there are waiters for the cache to drain, wake them if
425 * the number of free or discarded cache blocks reaches the
426 * CM_CACHESIZEDDRAINEDPCT limit.
429 * This routine must be called with the afs_xdcache lock held
433 afs_WakeCacheWaitersIfDrained(void)
435 if (afs_WaitForCacheDrain) {
436 if ((afs_blocksUsed - afs_blocksDiscarded) <=
437 PERCENT(CM_CACHESIZEDRAINEDPCT, afs_cacheBlocks)) {
438 afs_WaitForCacheDrain = 0;
439 afs_osi_Wakeup(&afs_WaitForCacheDrain);
445 * Keeps the cache clean and free by truncating uneeded files, when used.
450 afs_CacheTruncateDaemon(void)
452 osi_timeval_t CTD_tmpTime;
456 PERCENT((100 - CM_DCACHECOUNTFREEPCT + CM_DCACHEEXTRAPCT), afs_cacheFiles);
458 (((10 * AFS_CHUNKSIZE(0)) + afs_fsfragsize) & ~afs_fsfragsize) >> 10;
460 osi_GetuTime(&CTD_stats.CTD_afterSleep);
461 afs_TruncateDaemonRunning = 1;
463 cb_lowat = PERCENT((CM_DCACHESPACEFREEPCT - CM_DCACHEEXTRAPCT), afs_cacheBlocks);
464 ObtainWriteLock(&afs_xdcache, 266);
465 if (afs_CacheTooFull || afs_WaitForCacheDrain) {
466 int space_needed, slots_needed;
467 /* if we get woken up, we should try to clean something out */
468 for (counter = 0; counter < 10; counter++) {
470 afs_blocksUsed - afs_blocksDiscarded - cb_lowat;
471 if (space_needed < 0)
474 dc_hiwat - afs_freeDCCount - afs_discardDCCount;
475 if (slots_needed < 0)
477 if (slots_needed || space_needed)
478 afs_GetDownD(slots_needed, &space_needed, 0);
479 if ((space_needed <= 0) && (slots_needed <= 0)) {
482 if (afs_termState == AFSOP_STOP_TRUNCDAEMON)
485 if (!afs_CacheIsTooFull()) {
486 afs_CacheTooFull = 0;
487 afs_WakeCacheWaitersIfDrained();
489 } /* end of cache cleanup */
490 ReleaseWriteLock(&afs_xdcache);
493 * This is a defensive check to try to avoid starving threads
494 * that may need the global lock so thay can help free some
495 * cache space. If this thread won't be sleeping or truncating
496 * any cache files then give up the global lock so other
497 * threads get a chance to run.
499 if ((afs_termState != AFSOP_STOP_TRUNCDAEMON) && afs_CacheTooFull
500 && (!afs_blocksDiscarded || afs_WaitForCacheDrain)) {
501 afs_osi_Wait(100, 0, 0); /* 100 milliseconds */
505 * This is where we free the discarded cache elements.
507 while (afs_blocksDiscarded && !afs_WaitForCacheDrain
508 && (afs_termState != AFSOP_STOP_TRUNCDAEMON)) {
509 int code = afs_FreeDiscardedDCache();
511 /* If we can't free any discarded dcache entries, that's okay.
512 * We're just doing this in the background; if someone needs
513 * discarded entries freed, they will try it themselves and/or
514 * signal us that the cache is too full. In any case, we'll
515 * try doing this again the next time we run through the loop.
521 /* See if we need to continue to run. Someone may have
522 * signalled us while we were executing.
524 if (!afs_WaitForCacheDrain && !afs_CacheTooFull
525 && (afs_termState != AFSOP_STOP_TRUNCDAEMON)) {
526 /* Collect statistics on truncate daemon. */
527 CTD_stats.CTD_nSleeps++;
528 osi_GetuTime(&CTD_stats.CTD_beforeSleep);
529 afs_stats_GetDiff(CTD_tmpTime, CTD_stats.CTD_afterSleep,
530 CTD_stats.CTD_beforeSleep);
531 afs_stats_AddTo(CTD_stats.CTD_runTime, CTD_tmpTime);
533 afs_TruncateDaemonRunning = 0;
534 afs_osi_Sleep((int *)afs_CacheTruncateDaemon);
535 afs_TruncateDaemonRunning = 1;
537 osi_GetuTime(&CTD_stats.CTD_afterSleep);
538 afs_stats_GetDiff(CTD_tmpTime, CTD_stats.CTD_beforeSleep,
539 CTD_stats.CTD_afterSleep);
540 afs_stats_AddTo(CTD_stats.CTD_sleepTime, CTD_tmpTime);
542 if (afs_termState == AFSOP_STOP_TRUNCDAEMON) {
543 afs_termState = AFSOP_STOP_AFSDB;
544 afs_osi_Wakeup(&afs_termState);
552 * Make adjustment for the new size in the disk cache entry
554 * \note Major Assumptions Here:
555 * Assumes that frag size is an integral power of two, less one,
556 * and that this is a two's complement machine. I don't
557 * know of any filesystems which violate this assumption...
559 * \param adc Ptr to dcache entry.
560 * \param anewsize New size desired.
565 afs_AdjustSize(struct dcache *adc, afs_int32 newSize)
569 AFS_STATCNT(afs_AdjustSize);
571 if (newSize > afs_OtherCSize && !(adc->f.fid.Fid.Vnode & 1)) {
572 /* No non-dir cache files should be larger than the chunk size.
573 * (Directory blobs are fetched in a single chunk file, so directories
574 * can be larger.) If someone is requesting that a chunk is larger than
575 * the chunk size, something strange is happening. Log a message about
576 * it, to give a hint to subsequent strange behavior, if any occurs. */
580 afs_warn("afs: Warning: dcache %d is very large (%d > %d). This "
581 "should not happen, but trying to continue regardless. If "
582 "AFS starts hanging or behaving strangely, this might be "
584 adc->index, newSize, afs_OtherCSize);
588 adc->dflags |= DFEntryMod;
589 oldSize = ((adc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10; /* round up */
590 adc->f.chunkBytes = newSize;
593 newSize = ((newSize + afs_fsfragsize) ^ afs_fsfragsize) >> 10; /* round up */
594 afs_DCAdjustSize(adc, oldSize, newSize);
595 if ((newSize > oldSize) && !AFS_IS_DISCONNECTED) {
597 /* We're growing the file, wakeup the daemon */
598 afs_MaybeWakeupTruncateDaemon();
600 afs_blocksUsed += (newSize - oldSize);
601 afs_stats_cmperf.cacheBlocksInUse = afs_blocksUsed; /* XXX */
606 * This routine is responsible for moving at least one entry (but up
607 * to some number of them) from the LRU queue to the free queue.
609 * \param anumber Number of entries that should ideally be moved.
610 * \param aneedSpace How much space we need (1K blocks);
613 * The anumber parameter is just a hint; at least one entry MUST be
614 * moved, or we'll panic. We must be called with afs_xdcache
615 * write-locked. We should try to satisfy both anumber and aneedspace,
616 * whichever is more demanding - need to do several things:
617 * 1. only grab up to anumber victims if aneedSpace <= 0, not
618 * the whole set of MAXATONCE.
619 * 2. dynamically choose MAXATONCE to reflect severity of
620 * demand: something like (*aneedSpace >> (logChunk - 9))
622 * \note N.B. if we're called with aneedSpace <= 0 and anumber > 0, that
623 * indicates that the cache is not properly configured/tuned or
624 * something. We should be able to automatically correct that problem.
627 #define MAXATONCE 16 /* max we can obtain at once */
629 afs_GetDownD(int anumber, int *aneedSpace, afs_int32 buckethint)
633 struct VenusFid *afid;
638 afs_uint32 victims[MAXATONCE];
639 struct dcache *victimDCs[MAXATONCE];
640 afs_hyper_t victimTimes[MAXATONCE]; /* youngest (largest LRU time) first */
641 afs_uint32 victimPtr; /* next free item in victim arrays */
642 afs_hyper_t maxVictimTime; /* youngest (largest LRU time) victim */
643 afs_uint32 maxVictimPtr; /* where it is */
647 AFS_STATCNT(afs_GetDownD);
649 if (CheckLock(&afs_xdcache) != -1)
650 osi_Panic("getdownd nolock");
651 /* decrement anumber first for all dudes in free list */
652 /* SHOULD always decrement anumber first, even if aneedSpace >0,
653 * because we should try to free space even if anumber <=0 */
654 if (!aneedSpace || *aneedSpace <= 0) {
655 anumber -= afs_freeDCCount;
657 return; /* enough already free */
661 /* bounds check parameter */
662 if (anumber > MAXATONCE)
663 anumber = MAXATONCE; /* all we can do */
665 /* rewrite so phases include a better eligiblity for gc test*/
667 * The phase variable manages reclaims. Set to 0, the first pass,
668 * we don't reclaim active entries, or other than target bucket.
669 * Set to 1, we reclaim even active ones in target bucket.
670 * Set to 2, we reclaim any inactive one.
671 * Set to 3, we reclaim even active ones. On Solaris, we also reclaim
672 * entries whose corresponding vcache has a nonempty multiPage list, when
681 for (i = 0; i < afs_cacheFiles; i++)
682 /* turn off all flags */
683 afs_indexFlags[i] &= ~IFFlag;
685 while (anumber > 0 || (aneedSpace && *aneedSpace > 0)) {
686 /* find oldest entries for reclamation */
687 maxVictimPtr = victimPtr = 0;
688 hzero(maxVictimTime);
689 curbucket = afs_DCWhichBucket(phase, buckethint);
690 /* select victims from access time array */
691 for (i = 0; i < afs_cacheFiles; i++) {
692 if (afs_indexFlags[i] & (IFDataMod | IFFree | IFDiscarded)) {
693 /* skip if dirty or already free */
696 tdc = afs_indexTable[i];
697 if (tdc && (curbucket != tdc->bucket) && (phase < 4))
699 /* Wrong bucket; can't use it! */
702 if (tdc && (tdc->refCount != 0)) {
703 /* Referenced; can't use it! */
706 hset(vtime, afs_indexTimes[i]);
708 /* if we've already looked at this one, skip it */
709 if (afs_indexFlags[i] & IFFlag)
712 if (victimPtr < MAXATONCE) {
713 /* if there's at least one free victim slot left */
714 victims[victimPtr] = i;
715 hset(victimTimes[victimPtr], vtime);
716 if (hcmp(vtime, maxVictimTime) > 0) {
717 hset(maxVictimTime, vtime);
718 maxVictimPtr = victimPtr;
721 } else if (hcmp(vtime, maxVictimTime) < 0) {
723 * We're older than youngest victim, so we replace at
726 /* find youngest (largest LRU) victim */
729 osi_Panic("getdownd local");
731 hset(victimTimes[j], vtime);
732 /* recompute maxVictimTime */
733 hset(maxVictimTime, vtime);
734 for (j = 0; j < victimPtr; j++)
735 if (hcmp(maxVictimTime, victimTimes[j]) < 0) {
736 hset(maxVictimTime, victimTimes[j]);
742 /* now really reclaim the victims */
743 j = 0; /* flag to track if we actually got any of the victims */
744 /* first, hold all the victims, since we're going to release the lock
745 * during the truncate operation.
747 for (i = 0; i < victimPtr; i++) {
748 tdc = afs_GetValidDSlot(victims[i]);
749 /* We got tdc->tlock(R) here */
750 if (tdc && tdc->refCount == 1)
755 ReleaseReadLock(&tdc->tlock);
760 for (i = 0; i < victimPtr; i++) {
761 /* q is first elt in dcache entry */
763 /* now, since we're dropping the afs_xdcache lock below, we
764 * have to verify, before proceeding, that there are no other
765 * references to this dcache entry, even now. Note that we
766 * compare with 1, since we bumped it above when we called
767 * afs_GetValidDSlot to preserve the entry's identity.
769 if (tdc && tdc->refCount == 1) {
770 unsigned char chunkFlags;
771 afs_size_t tchunkoffset = 0;
773 /* xdcache is lower than the xvcache lock */
774 ReleaseWriteLock(&afs_xdcache);
775 ObtainReadLock(&afs_xvcache);
776 tvc = afs_FindVCache(afid, 0, 0 /* no stats, no vlru */ );
777 ReleaseReadLock(&afs_xvcache);
778 ObtainWriteLock(&afs_xdcache, 527);
780 if (tdc->refCount > 1)
783 tchunkoffset = AFS_CHUNKTOBASE(tdc->f.chunk);
784 chunkFlags = afs_indexFlags[tdc->index];
785 if (((phase & 1) == 0) && osi_Active(tvc))
787 if (((phase & 1) == 1) && osi_Active(tvc)
788 && (tvc->f.states & CDCLock)
789 && (chunkFlags & IFAnyPages))
791 if (chunkFlags & IFDataMod)
793 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
794 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, skip,
795 ICL_TYPE_INT32, tdc->index, ICL_TYPE_OFFSET,
796 ICL_HANDLE_OFFSET(tchunkoffset));
798 #if defined(AFS_SUN5_ENV)
800 * Now we try to invalidate pages. We do this only for
801 * Solaris. For other platforms, it's OK to recycle a
802 * dcache entry out from under a page, because the strategy
803 * function can call afs_GetDCache().
805 if (!skip && (chunkFlags & IFAnyPages)) {
808 ReleaseWriteLock(&afs_xdcache);
809 ObtainWriteLock(&tvc->vlock, 543);
810 if (!QEmpty(&tvc->multiPage)) {
811 if (phase < 3 || osi_VM_MultiPageConflict(tvc, tdc)) {
816 /* block locking pages */
817 tvc->vstates |= VPageCleaning;
818 /* block getting new pages */
820 ReleaseWriteLock(&tvc->vlock);
821 /* One last recheck */
822 ObtainWriteLock(&afs_xdcache, 333);
823 chunkFlags = afs_indexFlags[tdc->index];
824 if (tdc->refCount > 1 || (chunkFlags & IFDataMod)
825 || (osi_Active(tvc) && (tvc->f.states & CDCLock)
826 && (chunkFlags & IFAnyPages))) {
828 ReleaseWriteLock(&afs_xdcache);
831 ReleaseWriteLock(&afs_xdcache);
833 code = osi_VM_GetDownD(tvc, tdc);
835 ObtainWriteLock(&afs_xdcache, 269);
836 /* we actually removed all pages, clean and dirty */
838 afs_indexFlags[tdc->index] &=
839 ~(IFDirtyPages | IFAnyPages);
842 ReleaseWriteLock(&afs_xdcache);
844 ObtainWriteLock(&tvc->vlock, 544);
845 if (--tvc->activeV == 0
846 && (tvc->vstates & VRevokeWait)) {
847 tvc->vstates &= ~VRevokeWait;
848 afs_osi_Wakeup((char *)&tvc->vstates);
851 if (tvc->vstates & VPageCleaning) {
852 tvc->vstates &= ~VPageCleaning;
853 afs_osi_Wakeup((char *)&tvc->vstates);
856 ReleaseWriteLock(&tvc->vlock);
858 #endif /* AFS_SUN5_ENV */
860 ReleaseWriteLock(&afs_xdcache);
863 afs_PutVCache(tvc); /*XXX was AFS_FAST_RELE?*/
864 ObtainWriteLock(&afs_xdcache, 528);
865 if (afs_indexFlags[tdc->index] &
866 (IFDataMod | IFDirtyPages | IFAnyPages))
868 if (tdc->refCount > 1)
871 #if defined(AFS_SUN5_ENV)
873 /* no vnode, so IFDirtyPages is spurious (we don't
874 * sweep dcaches on vnode recycling, so we can have
875 * DIRTYPAGES set even when all pages are gone). Just
877 * Hold vcache lock to prevent vnode from being
878 * created while we're clearing IFDirtyPages.
880 afs_indexFlags[tdc->index] &=
881 ~(IFDirtyPages | IFAnyPages);
885 /* skip this guy and mark him as recently used */
886 afs_indexFlags[tdc->index] |= IFFlag;
887 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
888 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, 2,
889 ICL_TYPE_INT32, tdc->index, ICL_TYPE_OFFSET,
890 ICL_HANDLE_OFFSET(tchunkoffset));
892 /* flush this dude from the data cache and reclaim;
893 * first, make sure no one will care that we damage
894 * it, by removing it from all hash tables. Then,
895 * melt it down for parts. Note that any concurrent
896 * (new possibility!) calls to GetDownD won't touch
897 * this guy because his reference count is > 0. */
898 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
899 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, 3,
900 ICL_TYPE_INT32, tdc->index, ICL_TYPE_OFFSET,
901 ICL_HANDLE_OFFSET(tchunkoffset));
902 AFS_STATCNT(afs_gget);
903 afs_HashOutDCache(tdc, 1);
904 if (tdc->f.chunkBytes != 0) {
908 (tdc->f.chunkBytes + afs_fsfragsize) >> 10;
913 afs_DiscardDCache(tdc);
918 j = 1; /* we reclaimed at least one victim */
923 } /* end of for victims loop */
926 /* Phase is 0 and no one was found, so try phase 1 (ignore
927 * osi_Active flag) */
930 for (i = 0; i < afs_cacheFiles; i++)
931 /* turn off all flags */
932 afs_indexFlags[i] &= ~IFFlag;
935 /* found no one in phases 0-5, we're hosed */
939 } /* big while loop */
947 * Remove adc from any hash tables that would allow it to be located
948 * again by afs_FindDCache or afs_GetDCache.
950 * \param adc Pointer to dcache entry to remove from hash tables.
952 * \note Locks: Must have the afs_xdcache lock write-locked to call this function.
956 afs_HashOutDCache(struct dcache *adc, int zap)
960 AFS_STATCNT(afs_glink);
962 /* we know this guy's in the LRUQ. We'll move dude into DCQ below */
964 /* if this guy is in the hash table, pull him out */
965 if (adc->f.fid.Fid.Volume != 0) {
966 /* remove entry from first hash chains */
967 i = DCHash(&adc->f.fid, adc->f.chunk);
968 us = afs_dchashTbl[i];
969 if (us == adc->index) {
970 /* first dude in the list */
971 afs_dchashTbl[i] = afs_dcnextTbl[adc->index];
973 /* somewhere on the chain */
974 while (us != NULLIDX) {
975 if (afs_dcnextTbl[us] == adc->index) {
976 /* found item pointing at the one to delete */
977 afs_dcnextTbl[us] = afs_dcnextTbl[adc->index];
980 us = afs_dcnextTbl[us];
983 osi_Panic("dcache hc");
985 /* remove entry from *other* hash chain */
986 i = DVHash(&adc->f.fid);
987 us = afs_dvhashTbl[i];
988 if (us == adc->index) {
989 /* first dude in the list */
990 afs_dvhashTbl[i] = afs_dvnextTbl[adc->index];
992 /* somewhere on the chain */
993 while (us != NULLIDX) {
994 if (afs_dvnextTbl[us] == adc->index) {
995 /* found item pointing at the one to delete */
996 afs_dvnextTbl[us] = afs_dvnextTbl[adc->index];
999 us = afs_dvnextTbl[us];
1002 osi_Panic("dcache hv");
1007 /* prevent entry from being found on a reboot (it is already out of
1008 * the hash table, but after a crash, we just look at fid fields of
1009 * stable (old) entries).
1011 adc->f.fid.Fid.Volume = 0; /* invalid */
1013 /* mark entry as modified */
1014 adc->dflags |= DFEntryMod;
1019 } /*afs_HashOutDCache */
1022 * Flush the given dcache entry, pulling it from hash chains
1023 * and truncating the associated cache file.
1025 * \param adc Ptr to dcache entry to flush.
1027 * \note Environment:
1028 * This routine must be called with the afs_xdcache lock held
1032 afs_FlushDCache(struct dcache *adc)
1034 AFS_STATCNT(afs_FlushDCache);
1036 * Bump the number of cache files flushed.
1038 afs_stats_cmperf.cacheFlushes++;
1040 /* remove from all hash tables */
1041 afs_HashOutDCache(adc, 1);
1043 /* Free its space; special case null operation, since truncate operation
1044 * in UFS is slow even in this case, and this allows us to pre-truncate
1045 * these files at more convenient times with fewer locks set
1046 * (see afs_GetDownD).
1048 if (adc->f.chunkBytes != 0) {
1049 afs_DiscardDCache(adc);
1050 afs_MaybeWakeupTruncateDaemon();
1052 afs_FreeDCache(adc);
1054 } /*afs_FlushDCache */
1058 * Put a dcache entry on the free dcache entry list.
1060 * \param adc dcache entry to free.
1062 * \note Environment: called with afs_xdcache lock write-locked.
1065 afs_FreeDCache(struct dcache *adc)
1067 /* Thread on free list, update free list count and mark entry as
1068 * freed in its indexFlags element. Also, ensure DCache entry gets
1069 * written out (set DFEntryMod).
1072 afs_dvnextTbl[adc->index] = afs_freeDCList;
1073 afs_freeDCList = adc->index;
1075 afs_indexFlags[adc->index] |= IFFree;
1076 adc->dflags |= DFEntryMod;
1078 afs_WakeCacheWaitersIfDrained();
1079 } /* afs_FreeDCache */
1082 * Discard the cache element by moving it to the discardDCList.
1083 * This puts the cache element into a quasi-freed state, where
1084 * the space may be reused, but the file has not been truncated.
1086 * \note Major Assumptions Here:
1087 * Assumes that frag size is an integral power of two, less one,
1088 * and that this is a two's complement machine. I don't
1089 * know of any filesystems which violate this assumption...
1091 * \param adr Ptr to dcache entry.
1093 * \note Environment:
1094 * Must be called with afs_xdcache write-locked.
1098 afs_DiscardDCache(struct dcache *adc)
1102 AFS_STATCNT(afs_DiscardDCache);
1104 osi_Assert(adc->refCount == 1);
1106 size = ((adc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10; /* round up */
1107 afs_blocksDiscarded += size;
1108 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1110 afs_dvnextTbl[adc->index] = afs_discardDCList;
1111 afs_discardDCList = adc->index;
1112 afs_discardDCCount++;
1114 adc->f.fid.Fid.Volume = 0;
1115 adc->dflags |= DFEntryMod;
1116 afs_indexFlags[adc->index] |= IFDiscarded;
1118 afs_WakeCacheWaitersIfDrained();
1119 } /*afs_DiscardDCache */
1122 * Get a dcache entry from the discard or free list
1124 * @param[out] adc On success, a dcache from the given list. Otherwise, NULL.
1125 * @param[in] indexp A pointer to the head of the dcache free list or discard
1126 * list (afs_freeDCList, or afs_discardDCList)
1128 * @return 0 on success. If there are no dcache slots available, return ENOSPC.
1129 * If we encountered an error in disk i/o while trying to find a
1130 * dcache, return EIO.
1132 * @pre afs_xdcache is write-locked
1135 afs_GetDSlotFromList(struct dcache **adc, afs_int32 *indexp)
1141 if (*indexp == NULLIDX) {
1145 tdc = afs_GetUnusedDSlot(*indexp);
1150 osi_Assert(tdc->refCount == 1);
1151 ReleaseReadLock(&tdc->tlock);
1152 *indexp = afs_dvnextTbl[tdc->index];
1153 afs_dvnextTbl[tdc->index] = NULLIDX;
1160 * Free the next element on the list of discarded cache elements.
1162 * Returns -1 if we encountered an error preventing us from freeing a
1163 * discarded dcache, or 0 on success.
1166 afs_FreeDiscardedDCache(void)
1169 struct osi_file *tfile;
1172 AFS_STATCNT(afs_FreeDiscardedDCache);
1174 ObtainWriteLock(&afs_xdcache, 510);
1175 if (!afs_blocksDiscarded) {
1176 ReleaseWriteLock(&afs_xdcache);
1181 * Get an entry from the list of discarded cache elements
1183 (void)afs_GetDSlotFromList(&tdc, &afs_discardDCList);
1185 ReleaseWriteLock(&afs_xdcache);
1189 afs_discardDCCount--;
1190 size = ((tdc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10; /* round up */
1191 afs_blocksDiscarded -= size;
1192 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1193 /* We can lock because we just took it off the free list */
1194 ObtainWriteLock(&tdc->lock, 626);
1195 ReleaseWriteLock(&afs_xdcache);
1198 * Truncate the element to reclaim its space
1200 tfile = afs_CFileOpen(&tdc->f.inode);
1202 afs_CFileTruncate(tfile, 0);
1203 afs_CFileClose(tfile);
1204 afs_AdjustSize(tdc, 0);
1205 afs_DCMoveBucket(tdc, 0, 0);
1208 * Free the element we just truncated
1210 ObtainWriteLock(&afs_xdcache, 511);
1211 afs_indexFlags[tdc->index] &= ~IFDiscarded;
1212 afs_FreeDCache(tdc);
1213 tdc->f.states &= ~(DRO|DBackup|DRW);
1214 ReleaseWriteLock(&tdc->lock);
1216 ReleaseWriteLock(&afs_xdcache);
1222 * Free as many entries from the list of discarded cache elements
1223 * as we need to get the free space down below CM_WAITFORDRAINPCT (98%).
1228 afs_MaybeFreeDiscardedDCache(void)
1231 AFS_STATCNT(afs_MaybeFreeDiscardedDCache);
1233 while (afs_blocksDiscarded
1234 && (afs_blocksUsed >
1235 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks))) {
1236 int code = afs_FreeDiscardedDCache();
1238 /* Callers depend on us to get the afs_blocksDiscarded count down.
1239 * If we cannot do that, the callers can spin by calling us over
1240 * and over. Panic for now until we can figure out something
1242 osi_Panic("Error freeing discarded dcache");
1249 * Try to free up a certain number of disk slots.
1251 * \param anumber Targeted number of disk slots to free up.
1253 * \note Environment:
1254 * Must be called with afs_xdcache write-locked.
1258 afs_GetDownDSlot(int anumber)
1260 struct afs_q *tq, *nq;
1265 AFS_STATCNT(afs_GetDownDSlot);
1266 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
1267 osi_Panic("diskless getdowndslot");
1269 if (CheckLock(&afs_xdcache) != -1)
1270 osi_Panic("getdowndslot nolock");
1272 /* decrement anumber first for all dudes in free list */
1273 for (tdc = afs_freeDSList; tdc; tdc = (struct dcache *)tdc->lruq.next)
1276 return; /* enough already free */
1278 for (cnt = 0, tq = afs_DLRU.prev; tq != &afs_DLRU && anumber > 0;
1280 tdc = (struct dcache *)tq; /* q is first elt in dcache entry */
1281 nq = QPrev(tq); /* in case we remove it */
1282 if (tdc->refCount == 0) {
1283 if ((ix = tdc->index) == NULLIDX)
1284 osi_Panic("getdowndslot");
1286 /* write-through if modified */
1287 if (tdc->dflags & DFEntryMod) {
1288 #if defined(AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
1290 * ask proxy to do this for us - we don't have the stack space
1292 while (tdc->dflags & DFEntryMod) {
1295 s = SPLOCK(afs_sgibklock);
1296 if (afs_sgibklist == NULL) {
1297 /* if slot is free, grab it. */
1298 afs_sgibklist = tdc;
1299 SV_SIGNAL(&afs_sgibksync);
1301 /* wait for daemon to (start, then) finish. */
1302 SP_WAIT(afs_sgibklock, s, &afs_sgibkwait, PINOD);
1308 code = afs_WriteDCache(tdc, 1);
1311 * We couldn't flush it at this time; return early because
1312 * if afs_WriteDCache() failed once it is likely to
1313 * continue failing for subsequent dcaches.
1317 tdc->dflags &= ~DFEntryMod;
1321 /* pull the entry out of the lruq and put it on the free list */
1322 QRemove(&tdc->lruq);
1323 afs_indexTable[ix] = NULL;
1324 afs_indexFlags[ix] &= ~IFEverUsed;
1325 tdc->index = NULLIDX;
1326 tdc->lruq.next = (struct afs_q *)afs_freeDSList;
1327 afs_freeDSList = tdc;
1331 } /*afs_GetDownDSlot */
1338 * Increment the reference count on a disk cache entry,
1339 * which already has a non-zero refcount. In order to
1340 * increment the refcount of a zero-reference entry, you
1341 * have to hold afs_xdcache.
1344 * adc : Pointer to the dcache entry to increment.
1347 * Nothing interesting.
1350 afs_RefDCache(struct dcache *adc)
1352 ObtainWriteLock(&adc->tlock, 627);
1353 if (adc->refCount < 0)
1354 osi_Panic("RefDCache: negative refcount");
1356 ReleaseWriteLock(&adc->tlock);
1365 * Decrement the reference count on a disk cache entry.
1368 * ad : Ptr to the dcache entry to decrement.
1371 * Nothing interesting.
1374 afs_PutDCache(struct dcache *adc)
1376 AFS_STATCNT(afs_PutDCache);
1377 ObtainWriteLock(&adc->tlock, 276);
1378 if (adc->refCount <= 0)
1379 osi_Panic("putdcache");
1381 ReleaseWriteLock(&adc->tlock);
1390 * Try to discard all data associated with this file from the
1394 * avc : Pointer to the cache info for the file.
1397 * Both pvnLock and lock are write held.
1400 afs_TryToSmush(struct vcache *avc, afs_ucred_t *acred, int sync)
1405 AFS_STATCNT(afs_TryToSmush);
1406 afs_Trace2(afs_iclSetp, CM_TRACE_TRYTOSMUSH, ICL_TYPE_POINTER, avc,
1407 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
1408 sync = 1; /* XX Temp testing XX */
1410 #if defined(AFS_SUN5_ENV)
1411 ObtainWriteLock(&avc->vlock, 573);
1412 avc->activeV++; /* block new getpages */
1413 ReleaseWriteLock(&avc->vlock);
1416 /* Flush VM pages */
1417 osi_VM_TryToSmush(avc, acred, sync);
1420 * Get the hash chain containing all dce's for this fid
1422 i = DVHash(&avc->f.fid);
1423 ObtainWriteLock(&afs_xdcache, 277);
1424 for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
1425 i = afs_dvnextTbl[index]; /* next pointer this hash table */
1426 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
1427 int releaseTlock = 1;
1428 tdc = afs_GetValidDSlot(index);
1430 /* afs_TryToSmush is best-effort; we may not actually discard
1431 * everything, so failure to discard dcaches due to an i/o
1435 if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
1437 if ((afs_indexFlags[index] & IFDataMod) == 0
1438 && tdc->refCount == 1) {
1439 ReleaseReadLock(&tdc->tlock);
1441 afs_FlushDCache(tdc);
1444 afs_indexTable[index] = 0;
1447 ReleaseReadLock(&tdc->tlock);
1451 #if defined(AFS_SUN5_ENV)
1452 ObtainWriteLock(&avc->vlock, 545);
1453 if (--avc->activeV == 0 && (avc->vstates & VRevokeWait)) {
1454 avc->vstates &= ~VRevokeWait;
1455 afs_osi_Wakeup((char *)&avc->vstates);
1457 ReleaseWriteLock(&avc->vlock);
1459 ReleaseWriteLock(&afs_xdcache);
1461 * It's treated like a callback so that when we do lookups we'll
1462 * invalidate the unique bit if any
1463 * trytoSmush occured during the lookup call
1469 * afs_DCacheMissingChunks
1472 * Given the cached info for a file, return the number of chunks that
1473 * are not available from the dcache.
1476 * avc: Pointer to the (held) vcache entry to look in.
1479 * The number of chunks which are not currently cached.
1482 * The vcache entry is held upon entry.
1486 afs_DCacheMissingChunks(struct vcache *avc)
1489 afs_size_t totalLength = 0;
1490 afs_uint32 totalChunks = 0;
1493 totalLength = avc->f.m.Length;
1494 if (avc->f.truncPos < totalLength)
1495 totalLength = avc->f.truncPos;
1497 /* Length is 0, no chunk missing. */
1498 if (totalLength == 0)
1501 /* If totalLength is a multiple of chunksize, the last byte appears
1502 * as being part of the next chunk, which does not exist.
1503 * Decrementing totalLength by one fixes that.
1506 totalChunks = (AFS_CHUNK(totalLength) + 1);
1508 /* If we're a directory, we only ever have one chunk, regardless of
1509 * the size of the dir.
1511 if (avc->f.fid.Fid.Vnode & 1 || vType(avc) == VDIR)
1515 printf("Should have %d chunks for %u bytes\n",
1516 totalChunks, (totalLength + 1));
1518 i = DVHash(&avc->f.fid);
1519 ObtainWriteLock(&afs_xdcache, 1001);
1520 for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
1521 i = afs_dvnextTbl[index];
1522 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
1523 tdc = afs_GetValidDSlot(index);
1527 if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
1530 ReleaseReadLock(&tdc->tlock);
1534 ReleaseWriteLock(&afs_xdcache);
1536 /*printf("Missing %d chunks\n", totalChunks);*/
1538 return (totalChunks);
1545 * Given the cached info for a file and a byte offset into the
1546 * file, make sure the dcache entry for that file and containing
1547 * the given byte is available, returning it to our caller.
1550 * avc : Pointer to the (held) vcache entry to look in.
1551 * abyte : Which byte we want to get to.
1554 * Pointer to the dcache entry covering the file & desired byte,
1555 * or NULL if not found.
1558 * The vcache entry is held upon entry.
1562 afs_FindDCache(struct vcache *avc, afs_size_t abyte)
1566 struct dcache *tdc = NULL;
1568 AFS_STATCNT(afs_FindDCache);
1569 chunk = AFS_CHUNK(abyte);
1572 * Hash on the [fid, chunk] and get the corresponding dcache index
1573 * after write-locking the dcache.
1575 i = DCHash(&avc->f.fid, chunk);
1576 ObtainWriteLock(&afs_xdcache, 278);
1577 for (index = afs_dchashTbl[i]; index != NULLIDX; index = afs_dcnextTbl[index]) {
1578 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
1579 tdc = afs_GetValidDSlot(index);
1581 /* afs_FindDCache is best-effort; we may not find the given
1582 * file/offset, so if we cannot find the given dcache due to
1583 * i/o errors, that is okay. */
1587 ReleaseReadLock(&tdc->tlock);
1588 if (!FidCmp(&tdc->f.fid, &avc->f.fid) && chunk == tdc->f.chunk) {
1589 break; /* leaving refCount high for caller */
1594 if (index != NULLIDX) {
1595 hset(afs_indexTimes[tdc->index], afs_indexCounter);
1596 hadd32(afs_indexCounter, 1);
1597 ReleaseWriteLock(&afs_xdcache);
1600 ReleaseWriteLock(&afs_xdcache);
1602 } /*afs_FindDCache */
1604 /* only call these from afs_AllocDCache() */
1606 afs_AllocFreeDSlot(struct dcache **adc)
1611 code = afs_GetDSlotFromList(&tdc, &afs_freeDCList);
1615 afs_indexFlags[tdc->index] &= ~IFFree;
1616 ObtainWriteLock(&tdc->lock, 604);
1623 afs_AllocDiscardDSlot(struct dcache **adc, afs_int32 lock)
1627 afs_uint32 size = 0;
1628 struct osi_file *file;
1630 code = afs_GetDSlotFromList(&tdc, &afs_discardDCList);
1634 afs_indexFlags[tdc->index] &= ~IFDiscarded;
1635 ObtainWriteLock(&tdc->lock, 605);
1636 afs_discardDCCount--;
1638 ((tdc->f.chunkBytes +
1639 afs_fsfragsize) ^ afs_fsfragsize) >> 10;
1640 tdc->f.states &= ~(DRO|DBackup|DRW);
1641 afs_DCMoveBucket(tdc, size, 0);
1642 afs_blocksDiscarded -= size;
1643 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1645 /* Truncate the chunk so zeroes get filled properly */
1646 file = afs_CFileOpen(&tdc->f.inode);
1648 afs_CFileTruncate(file, 0);
1649 afs_CFileClose(file);
1650 afs_AdjustSize(tdc, 0);
1658 * Get a fresh dcache from the free or discarded list.
1660 * \param adc Set to the new dcache on success, and NULL on error.
1661 * \param avc Who's dcache is this going to be?
1662 * \param chunk The position where it will be placed in.
1663 * \param lock How are locks held.
1664 * \param ashFid If this dcache going to be used for a shadow dir,
1667 * \note Required locks:
1669 * - avc (R if (lock & 1) set and W otherwise)
1670 * \note It write locks the new dcache. The caller must unlock it.
1672 * \return If we're out of dslots, ENOSPC. If we encountered disk errors, EIO.
1673 * On success, return 0.
1676 afs_AllocDCache(struct dcache **adc, struct vcache *avc, afs_int32 chunk,
1677 afs_int32 lock, struct VenusFid *ashFid)
1680 struct dcache *tdc = NULL;
1684 /* if (lock & 2), prefer 'free' dcaches; otherwise, prefer 'discard'
1685 * dcaches. In either case, try both if our first choice doesn't work due
1688 code = afs_AllocFreeDSlot(&tdc);
1689 if (code == ENOSPC) {
1690 code = afs_AllocDiscardDSlot(&tdc, lock);
1693 code = afs_AllocDiscardDSlot(&tdc, lock);
1694 if (code == ENOSPC) {
1695 code = afs_AllocFreeDSlot(&tdc);
1704 * avc->lock(R) if setLocks
1705 * avc->lock(W) if !setLocks
1711 * Fill in the newly-allocated dcache record.
1713 afs_indexFlags[tdc->index] &= ~(IFDirtyPages | IFAnyPages);
1715 /* Use shadow fid if provided. */
1716 tdc->f.fid = *ashFid;
1718 /* Use normal vcache's fid otherwise. */
1719 tdc->f.fid = avc->f.fid;
1720 if (avc->f.states & CRO)
1721 tdc->f.states = DRO;
1722 else if (avc->f.states & CBackup)
1723 tdc->f.states = DBackup;
1725 tdc->f.states = DRW;
1726 afs_DCMoveBucket(tdc, 0, afs_DCGetBucket(avc));
1727 afs_indexUnique[tdc->index] = tdc->f.fid.Fid.Unique;
1729 hones(tdc->f.versionNo); /* invalid value */
1730 tdc->f.chunk = chunk;
1731 tdc->validPos = AFS_CHUNKTOBASE(chunk);
1733 if (tdc->lruq.prev == &tdc->lruq)
1734 osi_Panic("lruq 1");
1741 IsDCacheSizeOK(struct dcache *adc, struct vcache *avc, afs_int32 chunk_bytes,
1742 afs_size_t file_length, afs_uint32 versionNo, int from_net)
1744 afs_size_t expected_bytes;
1745 afs_size_t chunk_start = AFS_CHUNKTOBASE(adc->f.chunk);
1747 if (file_length < chunk_start) {
1751 expected_bytes = file_length - chunk_start;
1753 if (vType(avc) != VDIR && expected_bytes > AFS_CHUNKTOSIZE(adc->f.chunk)) {
1754 /* A non-dir chunk cannot have more bytes than the chunksize. */
1755 expected_bytes = AFS_CHUNKTOSIZE(adc->f.chunk);
1759 if (chunk_bytes != expected_bytes) {
1760 static const afs_uint32 one_hour = 60 * 60;
1761 static afs_uint32 last_warn;
1762 afs_uint32 now = osi_Time();
1764 if (now < last_warn) {
1765 /* clock went backwards */
1769 if (now - last_warn > one_hour) {
1770 unsigned int mtime = adc->f.modTime;
1776 * The dcache we're looking at didn't come from the cache, but is
1777 * being populated from the net. Don't print out its mtime in that
1778 * case; that would be misleading since that's the mtime from the
1779 * last time this dcache slot was written to.
1784 afs_warn("afs: Detected corrupt dcache for file %d.%u.%u.%u: chunk %d "
1785 "(offset %lu) has %d bytes, but it should have %lu bytes (file "
1786 "length %lu, DV %u, dcache mtime %u)\n",
1788 adc->f.fid.Fid.Volume,
1789 adc->f.fid.Fid.Vnode,
1790 adc->f.fid.Fid.Unique,
1792 (unsigned long)chunk_start,
1794 (unsigned long)expected_bytes,
1795 (unsigned long)file_length,
1798 afs_warn("afs: Ignoring the dcache for now, but this may indicate "
1799 "corruption in the AFS cache\n");
1807 * Check if a dcache is "fresh". That is, if the dcache's DV matches the DV of
1808 * the vcache for that file, and the dcache looks "sane" (its length makes
1809 * sense, when considering the length of the given avc).
1811 * \param adc The dcache to check
1812 * \param avc The vcache for adc
1814 * \return 1 if the dcache is "fresh". 0 otherwise.
1817 afs_IsDCacheFresh(struct dcache *adc, struct vcache *avc)
1819 if (!hsame(adc->f.versionNo, avc->f.m.DataVersion)) {
1824 * If we've reached here, the DV in adc matches the DV of our avc. Check if
1825 * the number of bytes in adc agrees with the avc file length, as a sanity
1826 * check. If they don't match, we'll pretend the DVs don't match, so the
1827 * bad dcache data will not be used, and we'll probably re-fetch the chunk
1828 * data, replacing the bad chunk.
1831 if (!IsDCacheSizeOK(adc, avc, adc->f.chunkBytes, avc->f.m.Length,
1832 hgetlo(adc->f.versionNo), 0)) {
1843 * This function is called to obtain a reference to data stored in
1844 * the disk cache, locating a chunk of data containing the desired
1845 * byte and returning a reference to the disk cache entry, with its
1846 * reference count incremented.
1850 * avc : Ptr to a vcache entry (unlocked)
1851 * abyte : Byte position in the file desired
1852 * areq : Request structure identifying the requesting user.
1853 * aflags : Settings as follows:
1855 * 2 : Return after creating entry.
1856 * 4 : called from afs_vnop_write.c
1857 * *alen contains length of data to be written.
1859 * aoffset : Set to the offset within the chunk where the resident
1861 * alen : Set to the number of bytes of data after the desired
1862 * byte (including the byte itself) which can be read
1866 * The vcache entry pointed to by avc is unlocked upon entry.
1870 * Update the vnode-to-dcache hint if we can get the vnode lock
1871 * right away. Assumes dcache entry is at least read-locked.
1874 updateV2DC(int lockVc, struct vcache *v, struct dcache *d, int src)
1876 if (!lockVc || 0 == NBObtainWriteLock(&v->lock, src)) {
1877 if (afs_IsDCacheFresh(d, v) && v->callback)
1880 ReleaseWriteLock(&v->lock);
1884 /* avc - Write-locked unless aflags & 1 */
1886 afs_GetDCache(struct vcache *avc, afs_size_t abyte,
1887 struct vrequest *areq, afs_size_t * aoffset,
1888 afs_size_t * alen, int aflags)
1890 afs_int32 i, code, shortcut;
1891 #if defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
1892 afs_int32 adjustsize = 0;
1898 afs_size_t Position = 0;
1899 afs_int32 size, tlen; /* size of segment to transfer */
1900 struct afs_FetchOutput *tsmall = 0;
1902 struct osi_file *file;
1903 struct afs_conn *tc;
1905 struct server *newCallback = NULL;
1906 char setNewCallback;
1907 char setVcacheStatus;
1908 char doVcacheUpdate;
1910 int doAdjustSize = 0;
1911 int doReallyAdjustSize = 0;
1912 int overWriteWholeChunk = 0;
1913 struct rx_connection *rxconn;
1916 struct afs_stats_AccessInfo *accP; /*Ptr to access record in stats */
1917 int fromReplica; /*Are we reading from a replica? */
1918 int numFetchLoops; /*# times around the fetch/analyze loop */
1919 #endif /* AFS_NOSTATS */
1921 AFS_STATCNT(afs_GetDCache);
1925 setLocks = aflags & 1;
1928 * Determine the chunk number and offset within the chunk corresponding
1929 * to the desired byte.
1931 if (avc->f.fid.Fid.Vnode & 1) { /* if (vType(avc) == VDIR) */
1934 chunk = AFS_CHUNK(abyte);
1937 /* come back to here if we waited for the cache to drain. */
1940 setNewCallback = setVcacheStatus = 0;
1944 ObtainWriteLock(&avc->lock, 616);
1946 ObtainReadLock(&avc->lock);
1951 * avc->lock(R) if setLocks && !slowPass
1952 * avc->lock(W) if !setLocks || slowPass
1957 /* check hints first! (might could use bcmp or some such...) */
1958 if ((tdc = avc->dchint)) {
1962 * The locking order between afs_xdcache and dcache lock matters.
1963 * The hint dcache entry could be anywhere, even on the free list.
1964 * Locking afs_xdcache ensures that noone is trying to pull dcache
1965 * entries from the free list, and thereby assuming them to be not
1966 * referenced and not locked.
1968 ObtainReadLock(&afs_xdcache);
1969 dcLocked = (0 == NBObtainSharedLock(&tdc->lock, 601));
1971 if (dcLocked && (tdc->index != NULLIDX)
1972 && !FidCmp(&tdc->f.fid, &avc->f.fid) && chunk == tdc->f.chunk
1973 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1974 /* got the right one. It might not be the right version, and it
1975 * might be fetching, but it's the right dcache entry.
1977 /* All this code should be integrated better with what follows:
1978 * I can save a good bit more time under a write lock if I do..
1980 ObtainWriteLock(&tdc->tlock, 603);
1982 ReleaseWriteLock(&tdc->tlock);
1984 ReleaseReadLock(&afs_xdcache);
1987 if (afs_IsDCacheFresh(tdc, avc)
1988 && !(tdc->dflags & DFFetching)) {
1990 afs_stats_cmperf.dcacheHits++;
1991 ObtainWriteLock(&afs_xdcache, 559);
1992 QRemove(&tdc->lruq);
1993 QAdd(&afs_DLRU, &tdc->lruq);
1994 ReleaseWriteLock(&afs_xdcache);
1997 * avc->lock(R) if setLocks && !slowPass
1998 * avc->lock(W) if !setLocks || slowPass
2005 ReleaseSharedLock(&tdc->lock);
2006 ReleaseReadLock(&afs_xdcache);
2014 * avc->lock(R) if setLocks && !slowPass
2015 * avc->lock(W) if !setLocks || slowPass
2016 * tdc->lock(S) if tdc
2019 if (!tdc) { /* If the hint wasn't the right dcache entry */
2020 int dslot_error = 0;
2022 * Hash on the [fid, chunk] and get the corresponding dcache index
2023 * after write-locking the dcache.
2028 * avc->lock(R) if setLocks && !slowPass
2029 * avc->lock(W) if !setLocks || slowPass
2032 i = DCHash(&avc->f.fid, chunk);
2033 /* check to make sure our space is fine */
2034 afs_MaybeWakeupTruncateDaemon();
2036 ObtainWriteLock(&afs_xdcache, 280);
2038 for (index = afs_dchashTbl[i]; index != NULLIDX; us = index, index = afs_dcnextTbl[index]) {
2039 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
2040 tdc = afs_GetValidDSlot(index);
2042 /* we got an i/o error when trying to get the given dslot.
2043 * it's possible the dslot we're looking for is elsewhere,
2044 * but most likely the disk cache is currently unusable, so
2045 * all afs_GetValidDSlot calls will fail, so just bail out. */
2050 ReleaseReadLock(&tdc->tlock);
2053 * avc->lock(R) if setLocks && !slowPass
2054 * avc->lock(W) if !setLocks || slowPass
2057 if (!FidCmp(&tdc->f.fid, &avc->f.fid) && chunk == tdc->f.chunk) {
2058 /* Move it up in the beginning of the list */
2059 if (afs_dchashTbl[i] != index) {
2060 afs_dcnextTbl[us] = afs_dcnextTbl[index];
2061 afs_dcnextTbl[index] = afs_dchashTbl[i];
2062 afs_dchashTbl[i] = index;
2064 ReleaseWriteLock(&afs_xdcache);
2065 ObtainSharedLock(&tdc->lock, 606);
2066 break; /* leaving refCount high for caller */
2074 * If we didn't find the entry, we'll create one.
2076 if (index == NULLIDX) {
2079 * avc->lock(R) if setLocks
2080 * avc->lock(W) if !setLocks
2083 afs_Trace2(afs_iclSetp, CM_TRACE_GETDCACHE1, ICL_TYPE_POINTER,
2084 avc, ICL_TYPE_INT32, chunk);
2087 /* We couldn't find the dcache we want, but we hit some i/o
2088 * errors when trying to find it, so we're not sure if the
2089 * dcache we want is in the cache or not. Error out, so we
2090 * don't try to possibly create 2 separate dcaches for the
2091 * same exact data. */
2092 ReleaseWriteLock(&afs_xdcache);
2096 if (afs_discardDCList == NULLIDX && afs_freeDCList == NULLIDX) {
2098 avc->f.states |= CDCLock;
2099 /* just need slots */
2100 afs_GetDownD(5, (int *)0, afs_DCGetBucket(avc));
2102 avc->f.states &= ~CDCLock;
2104 code = afs_AllocDCache(&tdc, avc, chunk, aflags, NULL);
2106 ReleaseWriteLock(&afs_xdcache);
2107 if (code == ENOSPC) {
2108 /* It looks like afs_AllocDCache failed because we don't
2109 * have any free dslots to use. Maybe if we wait a little
2110 * while, we'll be able to free up some slots, so try for 5
2111 * minutes, then bail out. */
2112 if (++downDCount > 300) {
2113 afs_warn("afs: Unable to get free cache space for file "
2114 "%u:%u.%u.%u for 5 minutes; failing with an i/o error\n",
2116 avc->f.fid.Fid.Volume,
2117 avc->f.fid.Fid.Vnode,
2118 avc->f.fid.Fid.Unique);
2121 afs_osi_Wait(1000, 0, 0);
2125 /* afs_AllocDCache failed, but not because we're out of free
2126 * dslots. Something must be screwy with the cache, so bail out
2127 * immediately without waiting. */
2128 afs_warn("afs: Error while alloc'ing cache slot for file "
2129 "%u:%u.%u.%u; failing with an i/o error\n",
2131 avc->f.fid.Fid.Volume,
2132 avc->f.fid.Fid.Vnode,
2133 avc->f.fid.Fid.Unique);
2139 * avc->lock(R) if setLocks
2140 * avc->lock(W) if !setLocks
2146 * Now add to the two hash chains - note that i is still set
2147 * from the above DCHash call.
2149 afs_dcnextTbl[tdc->index] = afs_dchashTbl[i];
2150 afs_dchashTbl[i] = tdc->index;
2151 i = DVHash(&avc->f.fid);
2152 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[i];
2153 afs_dvhashTbl[i] = tdc->index;
2154 tdc->dflags = DFEntryMod;
2156 afs_MaybeWakeupTruncateDaemon();
2157 ReleaseWriteLock(&afs_xdcache);
2158 ConvertWToSLock(&tdc->lock);
2163 /* vcache->dcache hint failed */
2166 * avc->lock(R) if setLocks && !slowPass
2167 * avc->lock(W) if !setLocks || slowPass
2170 afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE2, ICL_TYPE_POINTER, avc,
2171 ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
2172 hgetlo(tdc->f.versionNo), ICL_TYPE_INT32,
2173 hgetlo(avc->f.m.DataVersion));
2175 * Here we have the entry in tdc, with its refCount incremented.
2176 * Note: we don't use the S-lock on avc; it costs concurrency when
2177 * storing a file back to the server.
2181 * Not a newly created file so we need to check the file's length and
2182 * compare data versions since someone could have changed the data or we're
2183 * reading a file written elsewhere. We only want to bypass doing no-op
2184 * read rpcs on newly created files (dv of 0) since only then we guarantee
2185 * that this chunk's data hasn't been filled by another client.
2187 size = AFS_CHUNKSIZE(abyte);
2188 if (aflags & 4) /* called from write */
2190 else /* called from read */
2191 tlen = tdc->validPos - abyte;
2192 Position = AFS_CHUNKTOBASE(chunk);
2193 afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE3, ICL_TYPE_INT32, tlen,
2194 ICL_TYPE_INT32, aflags, ICL_TYPE_OFFSET,
2195 ICL_HANDLE_OFFSET(abyte), ICL_TYPE_OFFSET,
2196 ICL_HANDLE_OFFSET(Position));
2197 if ((aflags & 4) && (hiszero(avc->f.m.DataVersion)))
2199 if ((AFS_CHUNKTOBASE(chunk) >= avc->f.m.Length) ||
2200 ((aflags & 4) && (abyte == Position) && (tlen >= size)))
2201 overWriteWholeChunk = 1;
2202 if (doAdjustSize || overWriteWholeChunk) {
2203 #if defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
2205 #ifdef AFS_SGI64_ENV
2208 #else /* AFS_SGI64_ENV */
2211 #endif /* AFS_SGI64_ENV */
2212 #else /* AFS_SGI_ENV */
2215 #endif /* AFS_SGI_ENV */
2216 if (AFS_CHUNKTOBASE(chunk) + adjustsize >= avc->f.m.Length &&
2217 #else /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
2218 #if defined(AFS_SUN5_ENV)
2219 if ((doAdjustSize || (AFS_CHUNKTOBASE(chunk) >= avc->f.m.Length)) &&
2221 if (AFS_CHUNKTOBASE(chunk) >= avc->f.m.Length &&
2223 #endif /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
2224 !afs_IsDCacheFresh(tdc, avc))
2225 doReallyAdjustSize = 1;
2227 if (doReallyAdjustSize || overWriteWholeChunk) {
2228 /* no data in file to read at this position */
2229 UpgradeSToWLock(&tdc->lock, 607);
2230 file = afs_CFileOpen(&tdc->f.inode);
2232 afs_CFileTruncate(file, 0);
2233 afs_CFileClose(file);
2234 afs_AdjustSize(tdc, 0);
2235 hset(tdc->f.versionNo, avc->f.m.DataVersion);
2236 tdc->dflags |= DFEntryMod;
2238 ConvertWToSLock(&tdc->lock);
2243 * We must read in the whole chunk if the version number doesn't
2247 /* don't need data, just a unique dcache entry */
2248 ObtainWriteLock(&afs_xdcache, 608);
2249 hset(afs_indexTimes[tdc->index], afs_indexCounter);
2250 hadd32(afs_indexCounter, 1);
2251 ReleaseWriteLock(&afs_xdcache);
2253 updateV2DC(setLocks, avc, tdc, 553);
2254 if (vType(avc) == VDIR)
2257 *aoffset = AFS_CHUNKOFFSET(abyte);
2258 if (tdc->validPos < abyte)
2259 *alen = (afs_size_t) 0;
2261 *alen = tdc->validPos - abyte;
2262 ReleaseSharedLock(&tdc->lock);
2265 ReleaseWriteLock(&avc->lock);
2267 ReleaseReadLock(&avc->lock);
2269 return tdc; /* check if we're done */
2274 * avc->lock(R) if setLocks && !slowPass
2275 * avc->lock(W) if !setLocks || slowPass
2278 osi_Assert((setLocks && !slowPass) || WriteLocked(&avc->lock));
2280 setNewCallback = setVcacheStatus = 0;
2284 * avc->lock(R) if setLocks && !slowPass
2285 * avc->lock(W) if !setLocks || slowPass
2288 if (!afs_IsDCacheFresh(tdc, avc) && !overWriteWholeChunk) {
2290 * Version number mismatch.
2293 * If we are disconnected, then we can't do much of anything
2294 * because the data doesn't match the file.
2296 if (AFS_IS_DISCONNECTED) {
2297 ReleaseSharedLock(&tdc->lock);
2300 ReleaseWriteLock(&avc->lock);
2302 ReleaseReadLock(&avc->lock);
2304 /* Flush the Dcache */
2309 UpgradeSToWLock(&tdc->lock, 609);
2312 * If data ever existed for this vnode, and this is a text object,
2313 * do some clearing. Now, you'd think you need only do the flush
2314 * when VTEXT is on, but VTEXT is turned off when the text object
2315 * is freed, while pages are left lying around in memory marked
2316 * with this vnode. If we would reactivate (create a new text
2317 * object from) this vnode, we could easily stumble upon some of
2318 * these old pages in pagein. So, we always flush these guys.
2319 * Sun has a wonderful lack of useful invariants in this system.
2321 * avc->flushDV is the data version # of the file at the last text
2322 * flush. Clearly, at least, we don't have to flush the file more
2323 * often than it changes
2325 if (hcmp(avc->flushDV, avc->f.m.DataVersion) < 0) {
2327 * By here, the cache entry is always write-locked. We can
2328 * deadlock if we call osi_Flush with the cache entry locked...
2329 * Unlock the dcache too.
2331 ReleaseWriteLock(&tdc->lock);
2332 if (setLocks && !slowPass)
2333 ReleaseReadLock(&avc->lock);
2335 ReleaseWriteLock(&avc->lock);
2339 * Call osi_FlushPages in open, read/write, and map, since it
2340 * is too hard here to figure out if we should lock the
2343 if (setLocks && !slowPass)
2344 ObtainReadLock(&avc->lock);
2346 ObtainWriteLock(&avc->lock, 66);
2347 ObtainWriteLock(&tdc->lock, 610);
2352 * avc->lock(R) if setLocks && !slowPass
2353 * avc->lock(W) if !setLocks || slowPass
2357 /* Watch for standard race condition around osi_FlushText */
2358 if (afs_IsDCacheFresh(tdc, avc)) {
2359 updateV2DC(setLocks, avc, tdc, 569); /* set hint */
2360 afs_stats_cmperf.dcacheHits++;
2361 ConvertWToSLock(&tdc->lock);
2365 /* Sleep here when cache needs to be drained. */
2366 if (setLocks && !slowPass
2367 && (afs_blocksUsed >
2368 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks))) {
2369 /* Make sure truncate daemon is running */
2370 afs_MaybeWakeupTruncateDaemon();
2371 ObtainWriteLock(&tdc->tlock, 614);
2372 tdc->refCount--; /* we'll re-obtain the dcache when we re-try. */
2373 ReleaseWriteLock(&tdc->tlock);
2374 ReleaseWriteLock(&tdc->lock);
2375 ReleaseReadLock(&avc->lock);
2376 while ((afs_blocksUsed - afs_blocksDiscarded) >
2377 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
2378 afs_WaitForCacheDrain = 1;
2379 afs_osi_Sleep(&afs_WaitForCacheDrain);
2381 afs_MaybeFreeDiscardedDCache();
2382 /* need to check if someone else got the chunk first. */
2383 goto RetryGetDCache;
2386 Position = AFS_CHUNKBASE(abyte);
2387 if (vType(avc) == VDIR) {
2388 size = avc->f.m.Length;
2389 if (size > tdc->f.chunkBytes) {
2390 /* pre-reserve space for file */
2391 afs_AdjustSize(tdc, size);
2393 size = 999999999; /* max size for transfer */
2395 afs_size_t maxGoodLength;
2397 /* estimate how much data we're expecting back from the server,
2398 * and reserve space in the dcache entry for it */
2400 maxGoodLength = avc->f.m.Length;
2401 if (avc->f.truncPos < maxGoodLength)
2402 maxGoodLength = avc->f.truncPos;
2404 size = AFS_CHUNKSIZE(abyte); /* expected max size */
2405 if (Position > maxGoodLength) { /* If we're beyond EOF */
2407 } else if (Position + size > maxGoodLength) {
2408 size = maxGoodLength - Position;
2410 osi_Assert(size >= 0);
2412 if (size > tdc->f.chunkBytes) {
2413 /* pre-reserve estimated space for file */
2414 afs_AdjustSize(tdc, size); /* changes chunkBytes */
2418 /* For the actual fetch, do not limit the request to the
2419 * length of the file. If this results in a read past EOF on
2420 * the server, the server will just reply with less data than
2421 * requested. If we limit ourselves to only requesting data up
2422 * to the avc file length, we open ourselves up to races if the
2423 * file is extended on the server at about the same time.
2425 * However, we must restrict ourselves to the avc->f.truncPos
2426 * length, since this represents an outstanding local
2427 * truncation of the file that will be committed to the
2428 * fileserver when we actually write the fileserver contents.
2429 * If we do not restrict the fetch length based on
2430 * avc->f.truncPos, a different truncate operation extending
2431 * the file length could cause the old data after
2432 * avc->f.truncPos to reappear, instead of extending the file
2433 * with NUL bytes. */
2434 size = AFS_CHUNKSIZE(abyte);
2435 if (Position > avc->f.truncPos) {
2437 } else if (Position + size > avc->f.truncPos) {
2438 size = avc->f.truncPos - Position;
2440 osi_Assert(size >= 0);
2443 if (afs_mariner && !tdc->f.chunk)
2444 afs_MarinerLog("fetch$Fetching", avc); /* , Position, size, afs_indexCounter ); */
2446 * Right now, we only have one tool, and it's a hammer. So, we
2447 * fetch the whole file.
2449 DZap(tdc); /* pages in cache may be old */
2450 file = afs_CFileOpen(&tdc->f.inode);
2452 /* We can't access the file in the disk cache backing this dcache;
2454 ReleaseWriteLock(&tdc->lock);
2459 afs_RemoveVCB(&avc->f.fid);
2460 tdc->f.states |= DWriting;
2461 tdc->dflags |= DFFetching;
2462 tdc->validPos = Position; /* which is AFS_CHUNKBASE(abyte) */
2463 if (tdc->mflags & DFFetchReq) {
2464 tdc->mflags &= ~DFFetchReq;
2465 if (afs_osi_Wakeup(&tdc->validPos) == 0)
2466 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
2467 __FILE__, ICL_TYPE_INT32, __LINE__,
2468 ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
2471 tsmall = osi_AllocLargeSpace(sizeof(struct afs_FetchOutput));
2472 setVcacheStatus = 0;
2475 * Remember if we are doing the reading from a replicated volume,
2476 * and how many times we've zipped around the fetch/analyze loop.
2478 fromReplica = (avc->f.states & CRO) ? 1 : 0;
2480 accP = &(afs_stats_cmfullperf.accessinf);
2482 (accP->replicatedRefs)++;
2484 (accP->unreplicatedRefs)++;
2485 #endif /* AFS_NOSTATS */
2486 /* this is a cache miss */
2487 afs_Trace4(afs_iclSetp, CM_TRACE_FETCHPROC, ICL_TYPE_POINTER, avc,
2488 ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
2489 ICL_HANDLE_OFFSET(Position), ICL_TYPE_INT32, size);
2492 afs_stats_cmperf.dcacheMisses++;
2495 * Dynamic root support: fetch data from local memory.
2497 if (afs_IsDynroot(avc)) {
2501 afs_GetDynroot(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2503 dynrootDir += Position;
2504 dynrootLen -= Position;
2505 if (size > dynrootLen)
2509 code = afs_CFileWrite(file, 0, dynrootDir, size);
2517 tdc->validPos = Position + size;
2518 afs_CFileTruncate(file, size); /* prune it */
2519 } else if (afs_IsDynrootMount(avc)) {
2523 afs_GetDynrootMount(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2525 dynrootDir += Position;
2526 dynrootLen -= Position;
2527 if (size > dynrootLen)
2531 code = afs_CFileWrite(file, 0, dynrootDir, size);
2539 tdc->validPos = Position + size;
2540 afs_CFileTruncate(file, size); /* prune it */
2543 * Not a dynamic vnode: do the real fetch.
2548 * avc->lock(R) if setLocks && !slowPass
2549 * avc->lock(W) if !setLocks || slowPass
2553 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK, &rxconn);
2558 (accP->numReplicasAccessed)++;
2560 #endif /* AFS_NOSTATS */
2561 if (!setLocks || slowPass) {
2562 avc->callback = tc->parent->srvr->server;
2564 newCallback = tc->parent->srvr->server;
2568 code = afs_CacheFetchProc(tc, rxconn, file, Position, tdc,
2574 /* callback could have been broken (or expired) in a race here,
2575 * but we return the data anyway. It's as good as we knew about
2576 * when we started. */
2578 * validPos is updated by CacheFetchProc, and can only be
2579 * modifed under a dcache write lock, which we've blocked out
2581 size = tdc->validPos - Position; /* actual segment size */
2584 afs_CFileTruncate(file, size); /* prune it */
2586 /* Check that the amount of data that we fetched for the
2587 * dcache makes sense. */
2588 if (!IsDCacheSizeOK(tdc, avc, size,
2589 tsmall->OutStatus.Length,
2590 tsmall->OutStatus.DataVersion, 1)) {
2595 if (!setLocks || slowPass) {
2596 afs_StaleVCacheFlags(avc, AFS_STALEVC_CLEARCB, CUnique);
2598 /* Something lost. Forget about performance, and go
2599 * back with a vcache write lock.
2601 afs_CFileTruncate(file, 0);
2602 afs_AdjustSize(tdc, 0);
2603 afs_CFileClose(file);
2604 osi_FreeLargeSpace(tsmall);
2606 ReleaseWriteLock(&tdc->lock);
2611 * Call afs_Analyze to manage the connection references
2612 * and handle the error code (possibly mark servers
2613 * down, etc). We are going to retry getting the
2614 * dcache regardless, so we just ignore the retry hint
2615 * returned by afs_Analyze on this call.
2617 (void)afs_Analyze(tc, rxconn, code, &avc->f.fid, areq,
2618 AFS_STATS_FS_RPCIDX_FETCHDATA, SHARED_LOCK, NULL);
2620 ReleaseReadLock(&avc->lock);
2623 goto RetryGetDCache;
2627 } while (afs_Analyze
2628 (tc, rxconn, code, &avc->f.fid, areq,
2629 AFS_STATS_FS_RPCIDX_FETCHDATA, SHARED_LOCK, NULL));
2633 * avc->lock(R) if setLocks && !slowPass
2634 * avc->lock(W) if !setLocks || slowPass
2640 * In the case of replicated access, jot down info on the number of
2641 * attempts it took before we got through or gave up.
2644 if (numFetchLoops <= 1)
2645 (accP->refFirstReplicaOK)++;
2646 if (numFetchLoops > accP->maxReplicasPerRef)
2647 accP->maxReplicasPerRef = numFetchLoops;
2649 #endif /* AFS_NOSTATS */
2651 tdc->dflags &= ~DFFetching;
2652 if (afs_osi_Wakeup(&tdc->validPos) == 0)
2653 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
2654 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
2655 tdc, ICL_TYPE_INT32, tdc->dflags);
2656 if (avc->execsOrWriters == 0)
2657 tdc->f.states &= ~DWriting;
2659 /* now, if code != 0, we have an error and should punt.
2660 * note that we have the vcache write lock, either because
2661 * !setLocks or slowPass.
2664 afs_CFileTruncate(file, 0);
2665 afs_AdjustSize(tdc, 0);
2666 afs_CFileClose(file);
2667 ZapDCE(tdc); /* sets DFEntryMod */
2668 if (vType(avc) == VDIR) {
2671 tdc->f.states &= ~(DRO|DBackup|DRW);
2672 afs_DCMoveBucket(tdc, 0, 0);
2673 ReleaseWriteLock(&tdc->lock);
2675 if (!afs_IsDynroot(avc)) {
2676 afs_StaleVCacheFlags(avc, 0, CUnique);
2679 * avc->lock(W); assert(!setLocks || slowPass)
2681 osi_Assert(!setLocks || slowPass);
2687 /* otherwise we copy in the just-fetched info */
2688 afs_CFileClose(file);
2689 afs_AdjustSize(tdc, size); /* new size */
2691 * Copy appropriate fields into vcache. Status is
2692 * copied later where we selectively acquire the
2693 * vcache write lock.
2696 afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2698 setVcacheStatus = 1;
2699 hset64(tdc->f.versionNo, tsmall->OutStatus.dataVersionHigh,
2700 tsmall->OutStatus.DataVersion);
2701 tdc->dflags |= DFEntryMod;
2702 afs_indexFlags[tdc->index] |= IFEverUsed;
2703 ConvertWToSLock(&tdc->lock);
2704 } /*Data version numbers don't match */
2707 * Data version numbers match.
2709 afs_stats_cmperf.dcacheHits++;
2710 } /*Data version numbers match */
2712 updateV2DC(setLocks, avc, tdc, 335); /* set hint */
2716 * avc->lock(R) if setLocks && !slowPass
2717 * avc->lock(W) if !setLocks || slowPass
2718 * tdc->lock(S) if tdc
2722 * See if this was a reference to a file in the local cell.
2724 if (afs_IsPrimaryCellNum(avc->f.fid.Cell))
2725 afs_stats_cmperf.dlocalAccesses++;
2727 afs_stats_cmperf.dremoteAccesses++;
2729 /* Fix up LRU info */
2732 ObtainWriteLock(&afs_xdcache, 602);
2733 hset(afs_indexTimes[tdc->index], afs_indexCounter);
2734 hadd32(afs_indexCounter, 1);
2735 ReleaseWriteLock(&afs_xdcache);
2737 /* return the data */
2738 if (vType(avc) == VDIR)
2741 *aoffset = AFS_CHUNKOFFSET(abyte);
2742 *alen = (tdc->f.chunkBytes - *aoffset);
2743 ReleaseSharedLock(&tdc->lock);
2748 * avc->lock(R) if setLocks && !slowPass
2749 * avc->lock(W) if !setLocks || slowPass
2752 /* Fix up the callback and status values in the vcache */
2754 if (setLocks && !slowPass) {
2757 * This is our dirty little secret to parallel fetches.
2758 * We don't write-lock the vcache while doing the fetch,
2759 * but potentially we'll need to update the vcache after
2760 * the fetch is done.
2762 * Drop the read lock and try to re-obtain the write
2763 * lock. If the vcache still has the same DV, it's
2764 * ok to go ahead and install the new data.
2766 afs_hyper_t currentDV, statusDV;
2768 hset(currentDV, avc->f.m.DataVersion);
2770 if (setNewCallback && avc->callback != newCallback)
2774 hset64(statusDV, tsmall->OutStatus.dataVersionHigh,
2775 tsmall->OutStatus.DataVersion);
2777 if (setVcacheStatus && avc->f.m.Length != tsmall->OutStatus.Length)
2779 if (setVcacheStatus && !hsame(currentDV, statusDV))
2783 ReleaseReadLock(&avc->lock);
2785 if (doVcacheUpdate) {
2786 ObtainWriteLock(&avc->lock, 615);
2787 if (!hsame(avc->f.m.DataVersion, currentDV)) {
2788 /* We lose. Someone will beat us to it. */
2790 ReleaseWriteLock(&avc->lock);
2795 /* With slow pass, we've already done all the updates */
2797 ReleaseWriteLock(&avc->lock);
2800 /* Check if we need to perform any last-minute fixes with a write-lock */
2801 if (!setLocks || doVcacheUpdate) {
2803 avc->callback = newCallback;
2804 if (tsmall && setVcacheStatus)
2805 afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2807 ReleaseWriteLock(&avc->lock);
2811 osi_FreeLargeSpace(tsmall);
2814 } /*afs_GetDCache */
2818 * afs_WriteThroughDSlots
2821 * Sweep through the dcache slots and write out any modified
2822 * in-memory data back on to our caching store.
2828 * The afs_xdcache is write-locked through this whole affair.
2831 afs_WriteThroughDSlots(void)
2834 afs_int32 i, touchedit = 0;
2837 struct afs_q DirtyQ, *tq;
2839 AFS_STATCNT(afs_WriteThroughDSlots);
2842 * Because of lock ordering, we can't grab dcache locks while
2843 * holding afs_xdcache. So we enter xdcache, get a reference
2844 * for every dcache entry, and exit xdcache.
2846 ObtainWriteLock(&afs_xdcache, 283);
2848 for (i = 0; i < afs_cacheFiles; i++) {
2849 tdc = afs_indexTable[i];
2851 /* Grab tlock in case the existing refcount isn't zero */
2852 if (tdc && !(afs_indexFlags[i] & (IFFree | IFDiscarded))) {
2853 ObtainWriteLock(&tdc->tlock, 623);
2855 ReleaseWriteLock(&tdc->tlock);
2857 QAdd(&DirtyQ, &tdc->dirty);
2860 ReleaseWriteLock(&afs_xdcache);
2863 * Now, for each dcache entry we found, check if it's dirty.
2864 * If so, get write-lock, get afs_xdcache, which protects
2865 * afs_cacheInodep, and flush it. Don't forget to put back
2869 #define DQTODC(q) ((struct dcache *)(((char *) (q)) - sizeof(struct afs_q)))
2871 for (tq = DirtyQ.prev; tq != &DirtyQ && code == 0; tq = QPrev(tq)) {
2873 if (tdc->dflags & DFEntryMod) {
2876 wrLock = (0 == NBObtainWriteLock(&tdc->lock, 619));
2878 /* Now that we have the write lock, double-check */
2879 if (wrLock && (tdc->dflags & DFEntryMod)) {
2880 tdc->dflags &= ~DFEntryMod;
2881 ObtainWriteLock(&afs_xdcache, 620);
2882 code = afs_WriteDCache(tdc, 1);
2883 ReleaseWriteLock(&afs_xdcache);
2885 /* We didn't successfully write out the dslot; make sure we
2886 * try again later */
2887 tdc->dflags |= DFEntryMod;
2893 ReleaseWriteLock(&tdc->lock);
2903 ObtainWriteLock(&afs_xdcache, 617);
2904 if (!touchedit && (cacheDiskType != AFS_FCACHE_TYPE_MEM)) {
2905 /* Touch the file to make sure that the mtime on the file is kept
2906 * up-to-date to avoid losing cached files on cold starts because
2907 * their mtime seems old...
2909 struct afs_fheader theader;
2911 afs_InitFHeader(&theader);
2912 afs_osi_Write(afs_cacheInodep, 0, &theader, sizeof(theader));
2914 ReleaseWriteLock(&afs_xdcache);
2922 * Return a pointer to an freshly initialized dcache entry using
2923 * a memory-based cache. The tlock will be read-locked.
2926 * aslot : Dcache slot to look at.
2927 * type : What 'type' of dslot to get; see the dslot_state enum
2930 * Must be called with afs_xdcache write-locked.
2934 afs_MemGetDSlot(afs_int32 aslot, dslot_state type)
2939 AFS_STATCNT(afs_MemGetDSlot);
2940 if (CheckLock(&afs_xdcache) != -1)
2941 osi_Panic("getdslot nolock");
2942 if (aslot < 0 || aslot >= afs_cacheFiles)
2943 osi_Panic("getdslot slot %d (of %d)", aslot, afs_cacheFiles);
2944 tdc = afs_indexTable[aslot];
2946 QRemove(&tdc->lruq); /* move to queue head */
2947 QAdd(&afs_DLRU, &tdc->lruq);
2948 /* We're holding afs_xdcache, but get tlock in case refCount != 0 */
2949 ObtainWriteLock(&tdc->tlock, 624);
2951 ConvertWToRLock(&tdc->tlock);
2955 /* if we got here, the given slot is not in memory in our list of known
2956 * slots. for memcache, the only place a dslot can exist is in memory, so
2957 * if the caller is expecting to get back a known dslot, and we've reached
2958 * here, something is very wrong. DSLOT_NEW is the only type of dslot that
2959 * may not exist; for all others, the caller assumes the given dslot
2960 * already exists. so, 'type' had better be DSLOT_NEW here, or something is
2962 osi_Assert(type == DSLOT_NEW);
2964 if (!afs_freeDSList)
2965 afs_GetDownDSlot(4);
2966 if (!afs_freeDSList) {
2967 /* none free, making one is better than a panic */
2968 afs_stats_cmperf.dcacheXAllocs++; /* count in case we have a leak */
2969 tdc = afs_osi_Alloc(sizeof(struct dcache));
2970 osi_Assert(tdc != NULL);
2971 #ifdef KERNEL_HAVE_PIN
2972 pin((char *)tdc, sizeof(struct dcache)); /* XXX */
2975 tdc = afs_freeDSList;
2976 afs_freeDSList = (struct dcache *)tdc->lruq.next;
2979 tdc->dflags = 0; /* up-to-date, not in free q */
2981 QAdd(&afs_DLRU, &tdc->lruq);
2982 if (tdc->lruq.prev == &tdc->lruq)
2983 osi_Panic("lruq 3");
2985 /* initialize entry */
2986 tdc->f.fid.Cell = 0;
2987 tdc->f.fid.Fid.Volume = 0;
2989 hones(tdc->f.versionNo);
2990 tdc->f.inode.mem = aslot;
2991 tdc->dflags |= DFEntryMod;
2994 afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
2997 osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
2998 osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
2999 osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
3002 AFS_RWLOCK_INIT(&tdc->lock, "dcache lock");
3003 AFS_RWLOCK_INIT(&tdc->tlock, "dcache tlock");
3004 AFS_RWLOCK_INIT(&tdc->mflock, "dcache flock");
3005 ObtainReadLock(&tdc->tlock);
3007 afs_indexTable[aslot] = tdc;
3010 } /*afs_MemGetDSlot */
3012 unsigned int last_error = 0, lasterrtime = 0;
3018 * Return a pointer to an freshly initialized dcache entry using
3019 * a UFS-based disk cache. The dcache tlock will be read-locked.
3022 * aslot : Dcache slot to look at.
3023 * type : What 'type' of dslot to get; see the dslot_state enum
3026 * afs_xdcache lock write-locked.
3029 afs_UFSGetDSlot(afs_int32 aslot, dslot_state type)
3037 AFS_STATCNT(afs_UFSGetDSlot);
3038 if (CheckLock(&afs_xdcache) != -1)
3039 osi_Panic("getdslot nolock");
3040 if (aslot < 0 || aslot >= afs_cacheFiles)
3041 osi_Panic("getdslot slot %d (of %d)", aslot, afs_cacheFiles);
3042 tdc = afs_indexTable[aslot];
3044 QRemove(&tdc->lruq); /* move to queue head */
3045 QAdd(&afs_DLRU, &tdc->lruq);
3046 /* Grab tlock in case refCount != 0 */
3047 ObtainWriteLock(&tdc->tlock, 625);
3049 ConvertWToRLock(&tdc->tlock);
3053 /* otherwise we should read it in from the cache file */
3054 if (!afs_freeDSList)
3055 afs_GetDownDSlot(4);
3056 if (!afs_freeDSList) {
3057 /* none free, making one is better than a panic */
3058 afs_stats_cmperf.dcacheXAllocs++; /* count in case we have a leak */
3059 tdc = afs_osi_Alloc(sizeof(struct dcache));
3060 osi_Assert(tdc != NULL);
3061 #ifdef KERNEL_HAVE_PIN
3062 pin((char *)tdc, sizeof(struct dcache)); /* XXX */
3065 tdc = afs_freeDSList;
3066 afs_freeDSList = (struct dcache *)tdc->lruq.next;
3069 tdc->dflags = 0; /* up-to-date, not in free q */
3071 QAdd(&afs_DLRU, &tdc->lruq);
3072 if (tdc->lruq.prev == &tdc->lruq)
3073 osi_Panic("lruq 3");
3076 * Seek to the aslot'th entry and read it in.
3078 off = sizeof(struct fcache)*aslot + sizeof(struct afs_fheader);
3080 afs_osi_Read(afs_cacheInodep,
3081 off, (char *)(&tdc->f),
3082 sizeof(struct fcache));
3084 if (code != sizeof(struct fcache)) {
3086 #if defined(KERNEL_HAVE_UERROR)
3087 last_error = getuerror();
3091 lasterrtime = osi_Time();
3092 if (type != DSLOT_NEW) {
3093 /* If we are requesting a non-DSLOT_NEW slot, this is an error.
3094 * non-DSLOT_NEW slots are supposed to already exist, so if we
3095 * failed to read in the slot, something is wrong. */
3096 struct osi_stat tstat;
3097 if (afs_osi_Stat(afs_cacheInodep, &tstat)) {
3100 afs_warn("afs: disk cache read error in CacheItems slot %d "
3101 "off %d/%d code %d/%d\n",
3103 off, (int)tstat.size,
3104 (int)code, (int)sizeof(struct fcache));
3105 /* put tdc back on the free dslot list */
3106 QRemove(&tdc->lruq);
3107 tdc->index = NULLIDX;
3108 tdc->lruq.next = (struct afs_q *)afs_freeDSList;
3109 afs_freeDSList = tdc;
3113 if (!afs_CellNumValid(tdc->f.fid.Cell)) {
3115 if (type == DSLOT_VALID) {
3116 osi_Panic("afs: needed valid dcache but index %d off %d has "
3117 "invalid cell num %d\n",
3118 (int)aslot, off, (int)tdc->f.fid.Cell);
3122 if (type == DSLOT_VALID && tdc->f.fid.Fid.Volume == 0) {
3123 osi_Panic("afs: invalid zero-volume dcache entry at slot %d off %d",
3127 if (type == DSLOT_UNUSED) {
3128 /* the requested dslot is known to exist, but contain invalid data
3129 * (this happens when we're using a dslot from the free or discard
3130 * list). be sure not to re-use the data in it, so force invalidation.
3136 tdc->f.fid.Cell = 0;
3137 tdc->f.fid.Fid.Volume = 0;
3139 hones(tdc->f.versionNo);
3140 tdc->dflags |= DFEntryMod;
3141 afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
3142 tdc->f.states &= ~(DRO|DBackup|DRW);
3143 afs_DCMoveBucket(tdc, 0, 0);
3145 if (tdc->f.states & DRO) {
3146 afs_DCMoveBucket(tdc, 0, 2);
3147 } else if (tdc->f.states & DBackup) {
3148 afs_DCMoveBucket(tdc, 0, 1);
3150 afs_DCMoveBucket(tdc, 0, 1);
3155 if (tdc->f.chunk >= 0)
3156 tdc->validPos = AFS_CHUNKTOBASE(tdc->f.chunk) + tdc->f.chunkBytes;
3161 osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
3162 osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
3163 osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
3166 AFS_RWLOCK_INIT(&tdc->lock, "dcache lock");
3167 AFS_RWLOCK_INIT(&tdc->tlock, "dcache tlock");
3168 AFS_RWLOCK_INIT(&tdc->mflock, "dcache flock");
3169 ObtainReadLock(&tdc->tlock);
3172 * If we didn't read into a temporary dcache region, update the
3173 * slot pointer table.
3175 afs_indexTable[aslot] = tdc;
3178 } /*afs_UFSGetDSlot */
3183 * Write a particular dcache entry back to its home in the
3186 * \param adc Pointer to the dcache entry to write.
3187 * \param atime If true, set the modtime on the file to the current time.
3189 * \note Environment:
3190 * Must be called with the afs_xdcache lock at least read-locked,
3191 * and dcache entry at least read-locked.
3192 * The reference count is not changed.
3196 afs_WriteDCache(struct dcache *adc, int atime)
3200 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
3202 AFS_STATCNT(afs_WriteDCache);
3203 osi_Assert(WriteLocked(&afs_xdcache));
3205 adc->f.modTime = osi_Time();
3207 if ((afs_indexFlags[adc->index] & (IFFree | IFDiscarded)) == 0 &&
3208 adc->f.fid.Fid.Volume == 0) {
3209 /* If a dcache slot is not on the free or discard list, it must be
3210 * in the hash table. Thus, the volume must be non-zero, since that
3211 * is how we determine whether or not to unhash the entry when kicking
3212 * it out of the cache. Do this check now, since otherwise this can
3213 * cause hash table corruption and a panic later on after we read the
3215 osi_Panic("afs_WriteDCache zero volume index %d flags 0x%x\n",
3216 adc->index, (unsigned)afs_indexFlags[adc->index]);
3220 * Seek to the right dcache slot and write the in-memory image out to disk.
3222 afs_cellname_write();
3224 afs_osi_Write(afs_cacheInodep,
3225 sizeof(struct fcache) * adc->index +
3226 sizeof(struct afs_fheader), (char *)(&adc->f),
3227 sizeof(struct fcache));
3228 if (code != sizeof(struct fcache)) {
3229 afs_warn("afs: failed to write to CacheItems off %ld code %d/%d\n",
3230 (long)(sizeof(struct fcache) * adc->index + sizeof(struct afs_fheader)),
3231 (int)code, (int)sizeof(struct fcache));
3240 * Wake up users of a particular file waiting for stores to take
3243 * \param avc Ptr to related vcache entry.
3245 * \note Environment:
3246 * Nothing interesting.
3249 afs_wakeup(struct vcache *avc)
3252 struct brequest *tb;
3254 AFS_STATCNT(afs_wakeup);
3255 for (i = 0; i < NBRS; i++, tb++) {
3256 /* if request is valid and for this file, we've found it */
3257 if (tb->refCount > 0 && avc == tb->vc) {
3260 * If CSafeStore is on, then we don't awaken the guy
3261 * waiting for the store until the whole store has finished.
3262 * Otherwise, we do it now. Note that if CSafeStore is on,
3263 * the BStore routine actually wakes up the user, instead
3265 * I think this is redundant now because this sort of thing
3266 * is already being handled by the higher-level code.
3268 if ((avc->f.states & CSafeStore) == 0) {
3269 tb->code_raw = tb->code_checkcode = 0;
3270 tb->flags |= BUVALID;
3271 if (tb->flags & BUWAIT) {
3272 tb->flags &= ~BUWAIT;
3283 * Given a file name and inode, set up that file to be an
3284 * active member in the AFS cache. This also involves checking
3285 * the usability of its data.
3287 * \param afile Name of the cache file to initialize.
3288 * \param ainode Inode of the file.
3290 * \note Environment:
3291 * This function is called only during initialization.
3294 afs_InitCacheFile(char *afile, ino_t ainode)
3299 struct osi_file *tfile;
3300 struct osi_stat tstat;
3303 AFS_STATCNT(afs_InitCacheFile);
3304 index = afs_stats_cmperf.cacheNumEntries;
3305 if (index >= afs_cacheFiles)
3308 ObtainWriteLock(&afs_xdcache, 282);
3309 tdc = afs_GetNewDSlot(index);
3310 ReleaseReadLock(&tdc->tlock);
3311 ReleaseWriteLock(&afs_xdcache);
3313 ObtainWriteLock(&tdc->lock, 621);
3314 ObtainWriteLock(&afs_xdcache, 622);
3315 if (!afile && !ainode) {
3320 code = afs_LookupInodeByPath(afile, &tdc->f.inode.ufs, NULL);
3322 ReleaseWriteLock(&afs_xdcache);
3323 ReleaseWriteLock(&tdc->lock);
3328 /* Add any other 'complex' inode types here ... */
3329 #if !defined(AFS_LINUX26_ENV) && !defined(AFS_CACHE_VNODE_PATH)
3330 tdc->f.inode.ufs = ainode;
3332 osi_Panic("Can't init cache with inode numbers when complex inodes are "
3337 if ((tdc->f.states & DWriting) || tdc->f.fid.Fid.Volume == 0)
3339 tfile = osi_UFSOpen(&tdc->f.inode);
3341 ReleaseWriteLock(&afs_xdcache);
3342 ReleaseWriteLock(&tdc->lock);
3347 code = afs_osi_Stat(tfile, &tstat);
3349 osi_Panic("initcachefile stat");
3352 * If file size doesn't match the cache info file, it's probably bad.
3354 if (tdc->f.chunkBytes != tstat.size)
3357 * If file changed within T (120?) seconds of cache info file, it's
3358 * probably bad. In addition, if slot changed within last T seconds,
3359 * the cache info file may be incorrectly identified, and so slot
3362 if (cacheInfoModTime < tstat.mtime + 120)
3364 if (cacheInfoModTime < tdc->f.modTime + 120)
3366 /* In case write through is behind, make sure cache items entry is
3367 * at least as new as the chunk.
3369 if (tdc->f.modTime < tstat.mtime)
3372 tdc->f.chunkBytes = 0;
3375 tdc->f.fid.Fid.Volume = 0; /* not in the hash table */
3376 if (tfile && tstat.size != 0)
3377 osi_UFSTruncate(tfile, 0);
3378 tdc->f.states &= ~(DRO|DBackup|DRW);
3379 afs_DCMoveBucket(tdc, 0, 0);
3380 /* put entry in free cache slot list */
3381 afs_dvnextTbl[tdc->index] = afs_freeDCList;
3382 afs_freeDCList = index;
3384 afs_indexFlags[index] |= IFFree;
3385 afs_indexUnique[index] = 0;
3388 * We must put this entry in the appropriate hash tables.
3389 * Note that i is still set from the above DCHash call
3391 code = DCHash(&tdc->f.fid, tdc->f.chunk);
3392 afs_dcnextTbl[tdc->index] = afs_dchashTbl[code];
3393 afs_dchashTbl[code] = tdc->index;
3394 code = DVHash(&tdc->f.fid);
3395 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[code];
3396 afs_dvhashTbl[code] = tdc->index;
3397 afs_AdjustSize(tdc, tstat.size); /* adjust to new size */
3399 /* has nontrivial amt of data */
3400 afs_indexFlags[index] |= IFEverUsed;
3401 afs_stats_cmperf.cacheFilesReused++;
3403 * Initialize index times to file's mod times; init indexCounter
3406 hset32(afs_indexTimes[index], tstat.atime);
3407 if (hgetlo(afs_indexCounter) < tstat.atime) {
3408 hset32(afs_indexCounter, tstat.atime);
3410 afs_indexUnique[index] = tdc->f.fid.Fid.Unique;
3411 } /*File is not bad */
3414 osi_UFSClose(tfile);
3415 tdc->f.states &= ~DWriting;
3416 tdc->dflags &= ~DFEntryMod;
3417 /* don't set f.modTime; we're just cleaning up */
3418 osi_Assert(afs_WriteDCache(tdc, 0) == 0);
3419 ReleaseWriteLock(&afs_xdcache);
3420 ReleaseWriteLock(&tdc->lock);
3422 afs_stats_cmperf.cacheNumEntries++;
3427 /*Max # of struct dcache's resident at any time*/
3429 * If 'dchint' is enabled then in-memory dcache min is increased because of
3435 * Initialize dcache related variables.
3445 afs_dcacheInit(int afiles, int ablocks, int aDentries, int achunk, int aflags)
3452 afs_freeDCList = NULLIDX;
3453 afs_discardDCList = NULLIDX;
3454 afs_freeDCCount = 0;
3455 afs_freeDSList = NULL;
3456 hzero(afs_indexCounter);
3458 LOCK_INIT(&afs_xdcache, "afs_xdcache");
3464 if (achunk < 0 || achunk > 30)
3465 achunk = 13; /* Use default */
3466 AFS_SETCHUNKSIZE(achunk);
3472 /* afs_dhashsize defaults to 1024 */
3473 if (aDentries > 512)
3474 afs_dhashsize = 2048;
3475 /* Try to keep the average chain length around two unless the table
3476 * would be ridiculously big. */
3477 if (aDentries > 4096) {
3478 afs_dhashbits = opr_fls(aDentries) - 3;
3479 /* Cap the hash tables to 32k entries. */
3480 if (afs_dhashbits > 15)
3482 afs_dhashsize = opr_jhash_size(afs_dhashbits);
3484 /* initialize hash tables */
3485 afs_dvhashTbl = afs_osi_Alloc(afs_dhashsize * sizeof(afs_int32));
3486 osi_Assert(afs_dvhashTbl != NULL);
3487 afs_dchashTbl = afs_osi_Alloc(afs_dhashsize * sizeof(afs_int32));
3488 osi_Assert(afs_dchashTbl != NULL);
3489 for (i = 0; i < afs_dhashsize; i++) {
3490 afs_dvhashTbl[i] = NULLIDX;
3491 afs_dchashTbl[i] = NULLIDX;
3493 afs_dvnextTbl = afs_osi_Alloc(afiles * sizeof(afs_int32));
3494 osi_Assert(afs_dvnextTbl != NULL);
3495 afs_dcnextTbl = afs_osi_Alloc(afiles * sizeof(afs_int32));
3496 osi_Assert(afs_dcnextTbl != NULL);
3497 for (i = 0; i < afiles; i++) {
3498 afs_dvnextTbl[i] = NULLIDX;
3499 afs_dcnextTbl[i] = NULLIDX;
3502 /* Allocate and zero the pointer array to the dcache entries */
3503 afs_indexTable = afs_osi_Alloc(sizeof(struct dcache *) * afiles);
3504 osi_Assert(afs_indexTable != NULL);
3505 memset(afs_indexTable, 0, sizeof(struct dcache *) * afiles);
3506 afs_indexTimes = afs_osi_Alloc(afiles * sizeof(afs_hyper_t));
3507 osi_Assert(afs_indexTimes != NULL);
3508 memset(afs_indexTimes, 0, afiles * sizeof(afs_hyper_t));
3509 afs_indexUnique = afs_osi_Alloc(afiles * sizeof(afs_uint32));
3510 osi_Assert(afs_indexUnique != NULL);
3511 memset(afs_indexUnique, 0, afiles * sizeof(afs_uint32));
3512 afs_indexFlags = afs_osi_Alloc(afiles * sizeof(u_char));
3513 osi_Assert(afs_indexFlags != NULL);
3514 memset(afs_indexFlags, 0, afiles * sizeof(char));
3516 /* Allocate and thread the struct dcache entries themselves */
3517 tdp = afs_Initial_freeDSList =
3518 afs_osi_Alloc(aDentries * sizeof(struct dcache));
3519 osi_Assert(tdp != NULL);
3520 memset(tdp, 0, aDentries * sizeof(struct dcache));
3521 #ifdef KERNEL_HAVE_PIN
3522 pin((char *)afs_indexTable, sizeof(struct dcache *) * afiles); /* XXX */
3523 pin((char *)afs_indexTimes, sizeof(afs_hyper_t) * afiles); /* XXX */
3524 pin((char *)afs_indexFlags, sizeof(char) * afiles); /* XXX */
3525 pin((char *)afs_indexUnique, sizeof(afs_int32) * afiles); /* XXX */
3526 pin((char *)tdp, aDentries * sizeof(struct dcache)); /* XXX */
3527 pin((char *)afs_dvhashTbl, sizeof(afs_int32) * afs_dhashsize); /* XXX */
3528 pin((char *)afs_dchashTbl, sizeof(afs_int32) * afs_dhashsize); /* XXX */
3529 pin((char *)afs_dcnextTbl, sizeof(afs_int32) * afiles); /* XXX */
3530 pin((char *)afs_dvnextTbl, sizeof(afs_int32) * afiles); /* XXX */
3533 afs_freeDSList = &tdp[0];
3534 for (i = 0; i < aDentries - 1; i++) {
3535 tdp[i].lruq.next = (struct afs_q *)(&tdp[i + 1]);
3536 AFS_RWLOCK_INIT(&tdp[i].lock, "dcache lock");
3537 AFS_RWLOCK_INIT(&tdp[i].tlock, "dcache tlock");
3538 AFS_RWLOCK_INIT(&tdp[i].mflock, "dcache flock");
3540 tdp[aDentries - 1].lruq.next = (struct afs_q *)0;
3541 AFS_RWLOCK_INIT(&tdp[aDentries - 1].lock, "dcache lock");
3542 AFS_RWLOCK_INIT(&tdp[aDentries - 1].tlock, "dcache tlock");
3543 AFS_RWLOCK_INIT(&tdp[aDentries - 1].mflock, "dcache flock");
3545 afs_stats_cmperf.cacheBlocksOrig = afs_stats_cmperf.cacheBlocksTotal =
3546 afs_cacheBlocks = ablocks;
3547 afs_ComputeCacheParms(); /* compute parms based on cache size */
3549 afs_dcentries = aDentries;
3551 afs_stats_cmperf.cacheBucket0_Discarded =
3552 afs_stats_cmperf.cacheBucket1_Discarded =
3553 afs_stats_cmperf.cacheBucket2_Discarded = 0;
3557 if (aflags & AFSCALL_INIT_MEMCACHE) {
3559 * Use a memory cache instead of a disk cache
3561 cacheDiskType = AFS_FCACHE_TYPE_MEM;
3562 afs_cacheType = &afs_MemCacheOps;
3563 afiles = (afiles < aDentries) ? afiles : aDentries; /* min */
3564 ablocks = afiles * (AFS_FIRSTCSIZE / 1024);
3565 /* ablocks is reported in 1K blocks */
3566 code = afs_InitMemCache(afiles, AFS_FIRSTCSIZE, aflags);
3568 afs_warn("afsd: memory cache too large for available memory.\n");
3569 afs_warn("afsd: AFS files cannot be accessed.\n\n");
3573 afs_warn("Memory cache: Allocating %d dcache entries...",
3576 cacheDiskType = AFS_FCACHE_TYPE_UFS;
3577 afs_cacheType = &afs_UfsCacheOps;
3583 * Shuts down the cache.
3587 shutdown_dcache(void)
3591 #ifdef AFS_CACHE_VNODE_PATH
3592 if (cacheDiskType != AFS_FCACHE_TYPE_MEM) {
3594 for (i = 0; i < afs_cacheFiles; i++) {
3595 tdc = afs_indexTable[i];
3597 afs_osi_FreeStr(tdc->f.inode.ufs);
3603 afs_osi_Free(afs_dvnextTbl, afs_cacheFiles * sizeof(afs_int32));
3604 afs_osi_Free(afs_dcnextTbl, afs_cacheFiles * sizeof(afs_int32));
3605 afs_osi_Free(afs_indexTable, afs_cacheFiles * sizeof(struct dcache *));
3606 afs_osi_Free(afs_indexTimes, afs_cacheFiles * sizeof(afs_hyper_t));
3607 afs_osi_Free(afs_indexUnique, afs_cacheFiles * sizeof(afs_uint32));
3608 afs_osi_Free(afs_indexFlags, afs_cacheFiles * sizeof(u_char));
3609 afs_osi_Free(afs_Initial_freeDSList,
3610 afs_dcentries * sizeof(struct dcache));
3611 #ifdef KERNEL_HAVE_PIN
3612 unpin((char *)afs_dcnextTbl, afs_cacheFiles * sizeof(afs_int32));
3613 unpin((char *)afs_dvnextTbl, afs_cacheFiles * sizeof(afs_int32));
3614 unpin((char *)afs_indexTable, afs_cacheFiles * sizeof(struct dcache *));
3615 unpin((char *)afs_indexTimes, afs_cacheFiles * sizeof(afs_hyper_t));
3616 unpin((char *)afs_indexUnique, afs_cacheFiles * sizeof(afs_uint32));
3617 unpin((u_char *) afs_indexFlags, afs_cacheFiles * sizeof(u_char));
3618 unpin(afs_Initial_freeDSList, afs_dcentries * sizeof(struct dcache));
3622 for (i = 0; i < afs_dhashsize; i++) {
3623 afs_dvhashTbl[i] = NULLIDX;
3624 afs_dchashTbl[i] = NULLIDX;
3627 afs_osi_Free(afs_dvhashTbl, afs_dhashsize * sizeof(afs_int32));
3628 afs_osi_Free(afs_dchashTbl, afs_dhashsize * sizeof(afs_int32));
3630 afs_blocksUsed = afs_dcentries = 0;
3631 afs_stats_cmperf.cacheBucket0_Discarded =
3632 afs_stats_cmperf.cacheBucket1_Discarded =
3633 afs_stats_cmperf.cacheBucket2_Discarded = 0;
3634 hzero(afs_indexCounter);
3636 afs_freeDCCount = 0;
3637 afs_freeDCList = NULLIDX;
3638 afs_discardDCList = NULLIDX;
3639 afs_freeDSList = afs_Initial_freeDSList = 0;
3641 LOCK_INIT(&afs_xdcache, "afs_xdcache");
3647 * Get a dcache ready for writing, respecting the current cache size limits
3649 * len is required because afs_GetDCache with flag == 4 expects the length
3650 * field to be filled. It decides from this whether it's necessary to fetch
3651 * data into the chunk before writing or not (when the whole chunk is
3654 * \param avc The vcache to fetch a dcache for
3655 * \param filePos The start of the section to be written
3656 * \param len The length of the section to be written
3660 * \return If successful, a reference counted dcache with tdc->lock held. Lock
3661 * must be released and afs_PutDCache() called to free dcache.
3664 * \note avc->lock must be held on entry. Function may release and reobtain
3665 * avc->lock and GLOCK.
3669 afs_ObtainDCacheForWriting(struct vcache *avc, afs_size_t filePos,
3670 afs_size_t len, struct vrequest *areq,
3673 struct dcache *tdc = NULL;
3676 /* read the cached info */
3678 tdc = afs_FindDCache(avc, filePos);
3680 ObtainWriteLock(&tdc->lock, 657);
3681 } else if (afs_blocksUsed >
3682 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
3683 tdc = afs_FindDCache(avc, filePos);
3685 ObtainWriteLock(&tdc->lock, 658);
3686 if (!afs_IsDCacheFresh(tdc, avc)
3687 || (tdc->dflags & DFFetching)) {
3688 ReleaseWriteLock(&tdc->lock);
3694 afs_MaybeWakeupTruncateDaemon();
3695 while (afs_blocksUsed >
3696 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
3697 ReleaseWriteLock(&avc->lock);
3698 if (afs_blocksUsed - afs_blocksDiscarded >
3699 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
3700 afs_WaitForCacheDrain = 1;
3701 afs_osi_Sleep(&afs_WaitForCacheDrain);
3703 afs_MaybeFreeDiscardedDCache();
3704 afs_MaybeWakeupTruncateDaemon();
3705 ObtainWriteLock(&avc->lock, 509);
3707 avc->f.states |= CDirty;
3708 tdc = afs_GetDCache(avc, filePos, areq, &offset, &len, 4);
3710 ObtainWriteLock(&tdc->lock, 659);
3713 tdc = afs_GetDCache(avc, filePos, areq, &offset, &len, 4);
3715 ObtainWriteLock(&tdc->lock, 660);
3718 if (!(afs_indexFlags[tdc->index] & IFDataMod)) {
3719 afs_stats_cmperf.cacheCurrDirtyChunks++;
3720 afs_indexFlags[tdc->index] |= IFDataMod; /* so it doesn't disappear */
3722 if (!(tdc->f.states & DWriting)) {
3723 /* don't mark entry as mod if we don't have to */
3724 tdc->f.states |= DWriting;
3725 tdc->dflags |= DFEntryMod;
3732 * Make a shadow copy of a dir's dcache. It's used for disconnected
3733 * operations like remove/create/rename to keep the original directory data.
3734 * On reconnection, we can diff the original data with the server and get the
3735 * server changes and with the local data to get the local changes.
3737 * \param avc The dir vnode.
3738 * \param adc The dir dcache.
3740 * \return 0 for success.
3742 * \note The vcache entry must be write locked.
3743 * \note The dcache entry must be read locked.
3746 afs_MakeShadowDir(struct vcache *avc, struct dcache *adc)
3748 int i, code, ret_code = 0, written, trans_size;
3749 struct dcache *new_dc = NULL;
3750 struct osi_file *tfile_src, *tfile_dst;
3751 struct VenusFid shadow_fid;
3754 /* Is this a dir? */
3755 if (vType(avc) != VDIR)
3758 if (avc->f.shadow.vnode || avc->f.shadow.unique)
3761 /* Generate a fid for the shadow dir. */
3762 shadow_fid.Cell = avc->f.fid.Cell;
3763 shadow_fid.Fid.Volume = avc->f.fid.Fid.Volume;
3764 afs_GenShadowFid(&shadow_fid);
3766 ObtainWriteLock(&afs_xdcache, 716);
3768 /* Get a fresh dcache. */
3769 (void)afs_AllocDCache(&new_dc, avc, 0, 0, &shadow_fid);
3772 ObtainReadLock(&adc->mflock);
3774 /* Set up the new fid. */
3775 /* Copy interesting data from original dir dcache. */
3776 new_dc->mflags = adc->mflags;
3777 new_dc->dflags = adc->dflags;
3778 new_dc->f.modTime = adc->f.modTime;
3779 new_dc->f.versionNo = adc->f.versionNo;
3780 new_dc->f.states = adc->f.states;
3781 new_dc->f.chunk= adc->f.chunk;
3782 new_dc->f.chunkBytes = adc->f.chunkBytes;
3784 ReleaseReadLock(&adc->mflock);
3786 /* Now add to the two hash chains */
3787 i = DCHash(&shadow_fid, 0);
3788 afs_dcnextTbl[new_dc->index] = afs_dchashTbl[i];
3789 afs_dchashTbl[i] = new_dc->index;
3791 i = DVHash(&shadow_fid);
3792 afs_dvnextTbl[new_dc->index] = afs_dvhashTbl[i];
3793 afs_dvhashTbl[i] = new_dc->index;
3795 ReleaseWriteLock(&afs_xdcache);
3797 /* Alloc a 4k block. */
3798 data = afs_osi_Alloc(4096);
3800 afs_warn("afs_MakeShadowDir: could not alloc data\n");
3805 /* Open the files. */
3806 tfile_src = afs_CFileOpen(&adc->f.inode);
3807 tfile_dst = afs_CFileOpen(&new_dc->f.inode);
3808 osi_Assert(tfile_src);
3809 osi_Assert(tfile_dst);
3811 /* And now copy dir dcache data into this dcache,
3815 while (written < adc->f.chunkBytes) {
3816 trans_size = adc->f.chunkBytes - written;
3817 if (trans_size > 4096)
3820 /* Read a chunk from the dcache. */
3821 code = afs_CFileRead(tfile_src, written, data, trans_size);
3822 if (code < trans_size) {
3827 /* Write it to the new dcache. */
3828 code = afs_CFileWrite(tfile_dst, written, data, trans_size);
3829 if (code < trans_size) {
3834 written+=trans_size;
3837 afs_CFileClose(tfile_dst);
3838 afs_CFileClose(tfile_src);
3840 afs_osi_Free(data, 4096);
3842 ReleaseWriteLock(&new_dc->lock);
3843 afs_PutDCache(new_dc);
3846 ObtainWriteLock(&afs_xvcache, 763);
3847 ObtainWriteLock(&afs_disconDirtyLock, 765);
3848 QAdd(&afs_disconShadow, &avc->shadowq);
3849 osi_Assert((afs_RefVCache(avc) == 0));
3850 ReleaseWriteLock(&afs_disconDirtyLock);
3851 ReleaseWriteLock(&afs_xvcache);
3853 avc->f.shadow.vnode = shadow_fid.Fid.Vnode;
3854 avc->f.shadow.unique = shadow_fid.Fid.Unique;
3862 * Delete the dcaches of a shadow dir.
3864 * \param avc The vcache containing the shadow fid.
3866 * \note avc must be write locked.
3869 afs_DeleteShadowDir(struct vcache *avc)
3872 struct VenusFid shadow_fid;
3874 shadow_fid.Cell = avc->f.fid.Cell;
3875 shadow_fid.Fid.Volume = avc->f.fid.Fid.Volume;
3876 shadow_fid.Fid.Vnode = avc->f.shadow.vnode;
3877 shadow_fid.Fid.Unique = avc->f.shadow.unique;
3879 tdc = afs_FindDCacheByFid(&shadow_fid);
3881 afs_HashOutDCache(tdc, 1);
3882 afs_DiscardDCache(tdc);
3885 avc->f.shadow.vnode = avc->f.shadow.unique = 0;
3886 ObtainWriteLock(&afs_disconDirtyLock, 708);
3887 QRemove(&avc->shadowq);
3888 ReleaseWriteLock(&afs_disconDirtyLock);
3889 afs_PutVCache(avc); /* Because we held it when we added to the queue */
3893 * Populate a dcache with empty chunks up to a given file size,
3894 * used before extending a file in order to avoid 'holes' which
3895 * we can't access in disconnected mode.
3897 * \param avc The vcache which is being extended (locked)
3898 * \param alen The new length of the file
3902 afs_PopulateDCache(struct vcache *avc, afs_size_t apos, struct vrequest *areq)
3905 afs_size_t len, offset;
3906 afs_int32 start, end;
3908 /* We're doing this to deal with the situation where we extend
3909 * by writing after lseek()ing past the end of the file . If that
3910 * extension skips chunks, then those chunks won't be created, and
3911 * GetDCache will assume that they have to be fetched from the server.
3912 * So, for each chunk between the current file position, and the new
3913 * length we GetDCache for that chunk.
3916 if (AFS_CHUNK(apos) == 0 || apos <= avc->f.m.Length)
3919 if (avc->f.m.Length == 0)
3922 start = AFS_CHUNK(avc->f.m.Length)+1;
3924 end = AFS_CHUNK(apos);
3927 len = AFS_CHUNKTOSIZE(start);
3928 tdc = afs_GetDCache(avc, AFS_CHUNKTOBASE(start), areq, &offset, &len, 4);