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"
19 #include "afs/sysincludes.h" /*Standard vendor system headers */
20 #include "afsincludes.h" /*AFS-based standard headers */
21 #include "afs/afs_stats.h" /* statistics */
22 #include "afs/afs_cbqueue.h"
23 #include "afs/afs_osidnlc.h"
25 /* Forward declarations. */
26 static void afs_GetDownD(int anumber, int *aneedSpace, afs_int32 buckethint);
27 static void 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 #if defined(LINUX_USE_FH)
58 struct fid cacheitems_fh;
59 int cacheitems_fh_type;
61 ino_t cacheInode; /*!< Inode for CacheItems file */
63 struct osi_file *afs_cacheInodep = 0; /*!< file for CacheItems inode */
64 struct afs_q afs_DLRU; /*!< dcache LRU */
65 afs_int32 afs_dhashsize = 1024;
66 afs_int32 *afs_dvhashTbl; /*!< Data cache hash table: hashed by FID + chunk number. */
67 afs_int32 *afs_dchashTbl; /*!< Data cache hash table: hashed by FID. */
68 afs_int32 *afs_dvnextTbl; /*!< Dcache hash table links */
69 afs_int32 *afs_dcnextTbl; /*!< Dcache hash table links */
70 struct dcache **afs_indexTable; /*!< Pointers to dcache entries */
71 afs_hyper_t *afs_indexTimes; /*!< Dcache entry Access times */
72 afs_int32 *afs_indexUnique; /*!< dcache entry Fid.Unique */
73 unsigned char *afs_indexFlags; /*!< (only one) Is there data there? */
74 afs_hyper_t afs_indexCounter; /*!< Fake time for marking index
76 afs_int32 afs_cacheFiles = 0; /*!< Size of afs_indexTable */
77 afs_int32 afs_cacheBlocks; /*!< 1K blocks in cache */
78 afs_int32 afs_cacheStats; /*!< Stat entries in cache */
79 afs_int32 afs_blocksUsed; /*!< Number of blocks in use */
80 afs_int32 afs_blocksDiscarded; /*!<Blocks freed but not truncated */
81 afs_int32 afs_fsfragsize = 1023; /*!< Underlying Filesystem minimum unit
82 *of disk allocation usually 1K
83 *this value is (truefrag -1 ) to
84 *save a bunch of subtracts... */
85 #ifdef AFS_64BIT_CLIENT
86 #ifdef AFS_VM_RDWR_ENV
87 afs_size_t afs_vmMappingEnd; /* !< For large files (>= 2GB) the VM
88 * mapping an 32bit addressing machines
89 * can only be used below the 2 GB
90 * line. From this point upwards we
91 * must do direct I/O into the cache
92 * files. The value should be on a
94 #endif /* AFS_VM_RDWR_ENV */
95 #endif /* AFS_64BIT_CLIENT */
97 /* The following is used to ensure that new dcache's aren't obtained when
98 * the cache is nearly full.
100 int afs_WaitForCacheDrain = 0;
101 int afs_TruncateDaemonRunning = 0;
102 int afs_CacheTooFull = 0;
104 afs_int32 afs_dcentries; /*!< In-memory dcache entries */
107 int dcacheDisabled = 0;
109 static int afs_UFSCacheFetchProc(struct rx_call *, struct osi_file *,
110 afs_size_t, struct dcache *,
111 struct vcache *, afs_size_t *,
112 afs_size_t *, afs_int32);
114 static int afs_UFSCacheStoreProc(struct rx_call *, struct osi_file *,
115 afs_int32, struct vcache *,
119 struct afs_cacheOps afs_UfsCacheOps = {
120 #if defined(LINUX_USE_FH)
131 afs_UFSCacheFetchProc,
132 afs_UFSCacheStoreProc,
138 struct afs_cacheOps afs_MemCacheOps = {
140 afs_MemCacheTruncate,
146 afs_MemCacheFetchProc,
147 afs_MemCacheStoreProc,
153 int cacheDiskType; /*Type of backing disk for cache */
154 struct afs_cacheOps *afs_cacheType;
157 * Where is this vcache's entry associated dcache located/
158 * \param avc The vcache entry.
159 * \return Bucket index:
164 afs_DCGetBucket(struct vcache *avc)
169 /* This should be replaced with some sort of user configurable function */
170 if (avc->states & CRO) {
172 } else if (avc->states & CBackup) {
182 * Readjust a dcache's size.
184 * \param adc The dcache to be adjusted.
185 * \param oldSize Old size for the dcache.
186 * \param newSize The new size to be adjusted to.
190 afs_DCAdjustSize(struct dcache *adc, afs_int32 oldSize, afs_int32 newSize)
192 afs_int32 adjustSize = newSize - oldSize;
200 afs_blocksUsed_0 += adjustSize;
201 afs_stats_cmperf.cacheBucket0_Discarded += oldSize;
204 afs_blocksUsed_1 += adjustSize;
205 afs_stats_cmperf.cacheBucket1_Discarded += oldSize;
208 afs_blocksUsed_2 += adjustSize;
209 afs_stats_cmperf.cacheBucket2_Discarded += oldSize;
217 * Move a dcache from one bucket to another.
219 * \param adc Operate on this dcache.
220 * \param size Size in bucket (?).
221 * \param newBucket Destination bucket.
225 afs_DCMoveBucket(struct dcache *adc, afs_int32 size, afs_int32 newBucket)
230 /* Substract size from old bucket. */
234 afs_blocksUsed_0 -= size;
237 afs_blocksUsed_1 -= size;
240 afs_blocksUsed_2 -= size;
244 /* Set new bucket and increase destination bucket size. */
245 adc->bucket = newBucket;
250 afs_blocksUsed_0 += size;
253 afs_blocksUsed_1 += size;
256 afs_blocksUsed_2 += size;
264 * Init split caches size.
269 afs_blocksUsed_0 = afs_blocksUsed_1 = afs_blocksUsed_2 = 0;
278 afs_DCWhichBucket(afs_int32 phase, afs_int32 bucket)
283 afs_pct1 = afs_blocksUsed_1 / (afs_cacheBlocks / 100);
284 afs_pct2 = afs_blocksUsed_2 / (afs_cacheBlocks / 100);
286 /* Short cut: if we don't know about it, try to kill it */
287 if (phase < 2 && afs_blocksUsed_0)
290 if (afs_pct1 > afs_tpct1)
292 if (afs_pct2 > afs_tpct2)
294 return 0; /* unlikely */
299 * Warn about failing to store a file.
301 * \param acode Associated error code.
302 * \param avolume Volume involved.
303 * \param aflags How to handle the output:
304 * aflags & 1: Print out on console
305 * aflags & 2: Print out on controlling tty
307 * \note Environment: Call this from close call when vnodeops is RCS unlocked.
311 afs_StoreWarn(register afs_int32 acode, afs_int32 avolume,
312 register afs_int32 aflags)
314 static char problem_fmt[] =
315 "afs: failed to store file in volume %d (%s)\n";
316 static char problem_fmt_w_error[] =
317 "afs: failed to store file in volume %d (error %d)\n";
318 static char netproblems[] = "network problems";
319 static char partfull[] = "partition full";
320 static char overquota[] = "over quota";
322 AFS_STATCNT(afs_StoreWarn);
328 afs_warn(problem_fmt, avolume, netproblems);
330 afs_warnuser(problem_fmt, avolume, netproblems);
331 } else if (acode == ENOSPC) {
336 afs_warn(problem_fmt, avolume, partfull);
338 afs_warnuser(problem_fmt, avolume, partfull);
341 /* EDQUOT doesn't exist on solaris and won't be sent by the server.
342 * Instead ENOSPC will be sent...
344 if (acode == EDQUOT) {
349 afs_warn(problem_fmt, avolume, overquota);
351 afs_warnuser(problem_fmt, avolume, overquota);
359 afs_warn(problem_fmt_w_error, avolume, acode);
361 afs_warnuser(problem_fmt_w_error, avolume, acode);
366 * Try waking up truncation daemon, if it's worth it.
369 afs_MaybeWakeupTruncateDaemon(void)
371 if (!afs_CacheTooFull && afs_CacheIsTooFull()) {
372 afs_CacheTooFull = 1;
373 if (!afs_TruncateDaemonRunning)
374 afs_osi_Wakeup((int *)afs_CacheTruncateDaemon);
375 } else if (!afs_TruncateDaemonRunning
376 && afs_blocksDiscarded > CM_MAXDISCARDEDCHUNKS) {
377 afs_osi_Wakeup((int *)afs_CacheTruncateDaemon);
384 * Keep statistics on run time for afs_CacheTruncateDaemon. This is a
385 * struct so we need only export one symbol for AIX.
387 static struct CTD_stats {
388 osi_timeval_t CTD_beforeSleep;
389 osi_timeval_t CTD_afterSleep;
390 osi_timeval_t CTD_sleepTime;
391 osi_timeval_t CTD_runTime;
395 u_int afs_min_cache = 0;
398 * Keeps the cache clean and free by truncating uneeded files, when used.
403 afs_CacheTruncateDaemon(void)
405 osi_timeval_t CTD_tmpTime;
409 PERCENT((100 - CM_DCACHECOUNTFREEPCT + CM_DCACHEEXTRAPCT), afs_cacheFiles);
411 (((10 * AFS_CHUNKSIZE(0)) + afs_fsfragsize) & ~afs_fsfragsize) >> 10;
413 osi_GetuTime(&CTD_stats.CTD_afterSleep);
414 afs_TruncateDaemonRunning = 1;
416 cb_lowat = PERCENT((CM_DCACHESPACEFREEPCT - CM_DCACHEEXTRAPCT), afs_cacheBlocks);
417 MObtainWriteLock(&afs_xdcache, 266);
418 if (afs_CacheTooFull) {
419 int space_needed, slots_needed;
420 /* if we get woken up, we should try to clean something out */
421 for (counter = 0; counter < 10; counter++) {
423 afs_blocksUsed - afs_blocksDiscarded - cb_lowat;
425 dc_hiwat - afs_freeDCCount - afs_discardDCCount;
426 afs_GetDownD(slots_needed, &space_needed, 0);
427 if ((space_needed <= 0) && (slots_needed <= 0)) {
430 if (afs_termState == AFSOP_STOP_TRUNCDAEMON)
433 if (!afs_CacheIsTooFull())
434 afs_CacheTooFull = 0;
435 } /* end of cache cleanup */
436 MReleaseWriteLock(&afs_xdcache);
439 * This is a defensive check to try to avoid starving threads
440 * that may need the global lock so thay can help free some
441 * cache space. If this thread won't be sleeping or truncating
442 * any cache files then give up the global lock so other
443 * threads get a chance to run.
445 if ((afs_termState != AFSOP_STOP_TRUNCDAEMON) && afs_CacheTooFull
446 && (!afs_blocksDiscarded || afs_WaitForCacheDrain)) {
447 afs_osi_Wait(100, 0, 0); /* 100 milliseconds */
451 * This is where we free the discarded cache elements.
453 while (afs_blocksDiscarded && !afs_WaitForCacheDrain
454 && (afs_termState != AFSOP_STOP_TRUNCDAEMON)) {
455 afs_FreeDiscardedDCache();
458 /* See if we need to continue to run. Someone may have
459 * signalled us while we were executing.
461 if (!afs_WaitForCacheDrain && !afs_CacheTooFull
462 && (afs_termState != AFSOP_STOP_TRUNCDAEMON)) {
463 /* Collect statistics on truncate daemon. */
464 CTD_stats.CTD_nSleeps++;
465 osi_GetuTime(&CTD_stats.CTD_beforeSleep);
466 afs_stats_GetDiff(CTD_tmpTime, CTD_stats.CTD_afterSleep,
467 CTD_stats.CTD_beforeSleep);
468 afs_stats_AddTo(CTD_stats.CTD_runTime, CTD_tmpTime);
470 afs_TruncateDaemonRunning = 0;
471 afs_osi_Sleep((int *)afs_CacheTruncateDaemon);
472 afs_TruncateDaemonRunning = 1;
474 osi_GetuTime(&CTD_stats.CTD_afterSleep);
475 afs_stats_GetDiff(CTD_tmpTime, CTD_stats.CTD_beforeSleep,
476 CTD_stats.CTD_afterSleep);
477 afs_stats_AddTo(CTD_stats.CTD_sleepTime, CTD_tmpTime);
479 if (afs_termState == AFSOP_STOP_TRUNCDAEMON) {
481 afs_termState = AFSOP_STOP_AFSDB;
483 afs_termState = AFSOP_STOP_RXEVENT;
485 afs_osi_Wakeup(&afs_termState);
493 * Make adjustment for the new size in the disk cache entry
495 * \note Major Assumptions Here:
496 * Assumes that frag size is an integral power of two, less one,
497 * and that this is a two's complement machine. I don't
498 * know of any filesystems which violate this assumption...
500 * \param adc Ptr to dcache entry.
501 * \param anewsize New size desired.
506 afs_AdjustSize(register struct dcache *adc, register afs_int32 newSize)
508 register afs_int32 oldSize;
510 AFS_STATCNT(afs_AdjustSize);
512 adc->dflags |= DFEntryMod;
513 oldSize = ((adc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10; /* round up */
514 adc->f.chunkBytes = newSize;
517 newSize = ((newSize + afs_fsfragsize) ^ afs_fsfragsize) >> 10; /* round up */
518 afs_DCAdjustSize(adc, oldSize, newSize);
519 if ((newSize > oldSize) && !AFS_IS_DISCONNECTED) {
521 /* We're growing the file, wakeup the daemon */
522 afs_MaybeWakeupTruncateDaemon();
524 afs_blocksUsed += (newSize - oldSize);
525 afs_stats_cmperf.cacheBlocksInUse = afs_blocksUsed; /* XXX */
530 * This routine is responsible for moving at least one entry (but up
531 * to some number of them) from the LRU queue to the free queue.
533 * \param anumber Number of entries that should ideally be moved.
534 * \param aneedSpace How much space we need (1K blocks);
537 * The anumber parameter is just a hint; at least one entry MUST be
538 * moved, or we'll panic. We must be called with afs_xdcache
539 * write-locked. We should try to satisfy both anumber and aneedspace,
540 * whichever is more demanding - need to do several things:
541 * 1. only grab up to anumber victims if aneedSpace <= 0, not
542 * the whole set of MAXATONCE.
543 * 2. dynamically choose MAXATONCE to reflect severity of
544 * demand: something like (*aneedSpace >> (logChunk - 9))
546 * \note N.B. if we're called with aneedSpace <= 0 and anumber > 0, that
547 * indicates that the cache is not properly configured/tuned or
548 * something. We should be able to automatically correct that problem.
551 #define MAXATONCE 16 /* max we can obtain at once */
553 afs_GetDownD(int anumber, int *aneedSpace, afs_int32 buckethint)
557 struct VenusFid *afid;
561 register struct vcache *tvc;
562 afs_uint32 victims[MAXATONCE];
563 struct dcache *victimDCs[MAXATONCE];
564 afs_hyper_t victimTimes[MAXATONCE]; /* youngest (largest LRU time) first */
565 afs_uint32 victimPtr; /* next free item in victim arrays */
566 afs_hyper_t maxVictimTime; /* youngest (largest LRU time) victim */
567 afs_uint32 maxVictimPtr; /* where it is */
572 #if defined(AFS_FBSD80_ENV) && !defined(UKERNEL)
573 vfslocked = VFS_LOCK_GIANT(afs_globalVFS);
576 AFS_STATCNT(afs_GetDownD);
578 if (CheckLock(&afs_xdcache) != -1)
579 osi_Panic("getdownd nolock");
580 /* decrement anumber first for all dudes in free list */
581 /* SHOULD always decrement anumber first, even if aneedSpace >0,
582 * because we should try to free space even if anumber <=0 */
583 if (!aneedSpace || *aneedSpace <= 0) {
584 anumber -= afs_freeDCCount;
586 #if defined(AFS_FBSD80_ENV) && !defined(UKERNEL)
587 VFS_UNLOCK_GIANT(vfslocked);
589 return; /* enough already free */
593 /* bounds check parameter */
594 if (anumber > MAXATONCE)
595 anumber = MAXATONCE; /* all we can do */
597 /* rewrite so phases include a better eligiblity for gc test*/
599 * The phase variable manages reclaims. Set to 0, the first pass,
600 * we don't reclaim active entries, or other than target bucket.
601 * Set to 1, we reclaim even active ones in target bucket.
602 * Set to 2, we reclaim any inactive one.
603 * Set to 3, we reclaim even active ones.
611 for (i = 0; i < afs_cacheFiles; i++)
612 /* turn off all flags */
613 afs_indexFlags[i] &= ~IFFlag;
615 while (anumber > 0 || (aneedSpace && *aneedSpace > 0)) {
616 /* find oldest entries for reclamation */
617 maxVictimPtr = victimPtr = 0;
618 hzero(maxVictimTime);
619 curbucket = afs_DCWhichBucket(phase, buckethint);
620 /* select victims from access time array */
621 for (i = 0; i < afs_cacheFiles; i++) {
622 if (afs_indexFlags[i] & (IFDataMod | IFFree | IFDiscarded)) {
623 /* skip if dirty or already free */
626 tdc = afs_indexTable[i];
627 if (tdc && (curbucket != tdc->bucket) && (phase < 4))
629 /* Wrong bucket; can't use it! */
632 if (tdc && (tdc->refCount != 0)) {
633 /* Referenced; can't use it! */
636 hset(vtime, afs_indexTimes[i]);
638 /* if we've already looked at this one, skip it */
639 if (afs_indexFlags[i] & IFFlag)
642 if (victimPtr < MAXATONCE) {
643 /* if there's at least one free victim slot left */
644 victims[victimPtr] = i;
645 hset(victimTimes[victimPtr], vtime);
646 if (hcmp(vtime, maxVictimTime) > 0) {
647 hset(maxVictimTime, vtime);
648 maxVictimPtr = victimPtr;
651 } else if (hcmp(vtime, maxVictimTime) < 0) {
653 * We're older than youngest victim, so we replace at
656 /* find youngest (largest LRU) victim */
659 osi_Panic("getdownd local");
661 hset(victimTimes[j], vtime);
662 /* recompute maxVictimTime */
663 hset(maxVictimTime, vtime);
664 for (j = 0; j < victimPtr; j++)
665 if (hcmp(maxVictimTime, victimTimes[j]) < 0) {
666 hset(maxVictimTime, victimTimes[j]);
672 /* now really reclaim the victims */
673 j = 0; /* flag to track if we actually got any of the victims */
674 /* first, hold all the victims, since we're going to release the lock
675 * during the truncate operation.
677 for (i = 0; i < victimPtr; i++) {
678 tdc = afs_GetDSlot(victims[i], 0);
679 /* We got tdc->tlock(R) here */
680 if (tdc->refCount == 1)
684 ReleaseReadLock(&tdc->tlock);
688 for (i = 0; i < victimPtr; i++) {
689 /* q is first elt in dcache entry */
691 /* now, since we're dropping the afs_xdcache lock below, we
692 * have to verify, before proceeding, that there are no other
693 * references to this dcache entry, even now. Note that we
694 * compare with 1, since we bumped it above when we called
695 * afs_GetDSlot to preserve the entry's identity.
697 if (tdc && tdc->refCount == 1) {
698 unsigned char chunkFlags;
699 afs_size_t tchunkoffset = 0;
701 /* xdcache is lower than the xvcache lock */
702 MReleaseWriteLock(&afs_xdcache);
703 MObtainReadLock(&afs_xvcache);
704 tvc = afs_FindVCache(afid, 0, 0 /* no stats, no vlru */ );
705 MReleaseReadLock(&afs_xvcache);
706 MObtainWriteLock(&afs_xdcache, 527);
708 if (tdc->refCount > 1)
711 tchunkoffset = AFS_CHUNKTOBASE(tdc->f.chunk);
712 chunkFlags = afs_indexFlags[tdc->index];
713 if (((phase & 1) == 0) && osi_Active(tvc))
715 if (((phase & 1) == 1) && osi_Active(tvc)
716 && (tvc->states & CDCLock)
717 && (chunkFlags & IFAnyPages))
719 if (chunkFlags & IFDataMod)
721 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
722 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, skip,
723 ICL_TYPE_INT32, tdc->index, ICL_TYPE_OFFSET,
724 ICL_HANDLE_OFFSET(tchunkoffset));
726 #if defined(AFS_SUN5_ENV)
728 * Now we try to invalidate pages. We do this only for
729 * Solaris. For other platforms, it's OK to recycle a
730 * dcache entry out from under a page, because the strategy
731 * function can call afs_GetDCache().
733 if (!skip && (chunkFlags & IFAnyPages)) {
736 MReleaseWriteLock(&afs_xdcache);
737 MObtainWriteLock(&tvc->vlock, 543);
738 if (tvc->multiPage) {
742 /* block locking pages */
743 tvc->vstates |= VPageCleaning;
744 /* block getting new pages */
746 MReleaseWriteLock(&tvc->vlock);
747 /* One last recheck */
748 MObtainWriteLock(&afs_xdcache, 333);
749 chunkFlags = afs_indexFlags[tdc->index];
750 if (tdc->refCount > 1 || (chunkFlags & IFDataMod)
751 || (osi_Active(tvc) && (tvc->states & CDCLock)
752 && (chunkFlags & IFAnyPages))) {
754 MReleaseWriteLock(&afs_xdcache);
757 MReleaseWriteLock(&afs_xdcache);
759 code = osi_VM_GetDownD(tvc, tdc);
761 MObtainWriteLock(&afs_xdcache, 269);
762 /* we actually removed all pages, clean and dirty */
764 afs_indexFlags[tdc->index] &=
765 ~(IFDirtyPages | IFAnyPages);
768 MReleaseWriteLock(&afs_xdcache);
770 MObtainWriteLock(&tvc->vlock, 544);
771 if (--tvc->activeV == 0
772 && (tvc->vstates & VRevokeWait)) {
773 tvc->vstates &= ~VRevokeWait;
774 afs_osi_Wakeup((char *)&tvc->vstates);
777 if (tvc->vstates & VPageCleaning) {
778 tvc->vstates &= ~VPageCleaning;
779 afs_osi_Wakeup((char *)&tvc->vstates);
782 MReleaseWriteLock(&tvc->vlock);
784 #endif /* AFS_SUN5_ENV */
786 MReleaseWriteLock(&afs_xdcache);
789 afs_PutVCache(tvc); /*XXX was AFS_FAST_RELE?*/
790 MObtainWriteLock(&afs_xdcache, 528);
791 if (afs_indexFlags[tdc->index] &
792 (IFDataMod | IFDirtyPages | IFAnyPages))
794 if (tdc->refCount > 1)
797 #if defined(AFS_SUN5_ENV)
799 /* no vnode, so IFDirtyPages is spurious (we don't
800 * sweep dcaches on vnode recycling, so we can have
801 * DIRTYPAGES set even when all pages are gone). Just
803 * Hold vcache lock to prevent vnode from being
804 * created while we're clearing IFDirtyPages.
806 afs_indexFlags[tdc->index] &=
807 ~(IFDirtyPages | IFAnyPages);
811 /* skip this guy and mark him as recently used */
812 afs_indexFlags[tdc->index] |= IFFlag;
813 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
814 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, 2,
815 ICL_TYPE_INT32, tdc->index, ICL_TYPE_OFFSET,
816 ICL_HANDLE_OFFSET(tchunkoffset));
818 /* flush this dude from the data cache and reclaim;
819 * first, make sure no one will care that we damage
820 * it, by removing it from all hash tables. Then,
821 * melt it down for parts. Note that any concurrent
822 * (new possibility!) calls to GetDownD won't touch
823 * this guy because his reference count is > 0. */
824 afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
825 ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, 3,
826 ICL_TYPE_INT32, tdc->index, ICL_TYPE_OFFSET,
827 ICL_HANDLE_OFFSET(tchunkoffset));
828 AFS_STATCNT(afs_gget);
829 afs_HashOutDCache(tdc, 1);
830 if (tdc->f.chunkBytes != 0) {
834 (tdc->f.chunkBytes + afs_fsfragsize) >> 10;
839 afs_DiscardDCache(tdc);
844 j = 1; /* we reclaimed at least one victim */
848 } /* end of for victims loop */
851 /* Phase is 0 and no one was found, so try phase 1 (ignore
852 * osi_Active flag) */
855 for (i = 0; i < afs_cacheFiles; i++)
856 /* turn off all flags */
857 afs_indexFlags[i] &= ~IFFlag;
860 /* found no one in phases 0-5, we're hosed */
864 } /* big while loop */
866 #if defined(AFS_FBSD80_ENV) && !defined(UKERNEL)
867 VFS_UNLOCK_GIANT(vfslocked);
876 * Remove adc from any hash tables that would allow it to be located
877 * again by afs_FindDCache or afs_GetDCache.
879 * \param adc Pointer to dcache entry to remove from hash tables.
881 * \note Locks: Must have the afs_xdcache lock write-locked to call this function.
885 afs_HashOutDCache(struct dcache *adc, int zap)
889 AFS_STATCNT(afs_glink);
891 /* we know this guy's in the LRUQ. We'll move dude into DCQ below */
893 /* if this guy is in the hash table, pull him out */
894 if (adc->f.fid.Fid.Volume != 0) {
895 /* remove entry from first hash chains */
896 i = DCHash(&adc->f.fid, adc->f.chunk);
897 us = afs_dchashTbl[i];
898 if (us == adc->index) {
899 /* first dude in the list */
900 afs_dchashTbl[i] = afs_dcnextTbl[adc->index];
902 /* somewhere on the chain */
903 while (us != NULLIDX) {
904 if (afs_dcnextTbl[us] == adc->index) {
905 /* found item pointing at the one to delete */
906 afs_dcnextTbl[us] = afs_dcnextTbl[adc->index];
909 us = afs_dcnextTbl[us];
912 osi_Panic("dcache hc");
914 /* remove entry from *other* hash chain */
915 i = DVHash(&adc->f.fid);
916 us = afs_dvhashTbl[i];
917 if (us == adc->index) {
918 /* first dude in the list */
919 afs_dvhashTbl[i] = afs_dvnextTbl[adc->index];
921 /* somewhere on the chain */
922 while (us != NULLIDX) {
923 if (afs_dvnextTbl[us] == adc->index) {
924 /* found item pointing at the one to delete */
925 afs_dvnextTbl[us] = afs_dvnextTbl[adc->index];
928 us = afs_dvnextTbl[us];
931 osi_Panic("dcache hv");
936 /* prevent entry from being found on a reboot (it is already out of
937 * the hash table, but after a crash, we just look at fid fields of
938 * stable (old) entries).
940 adc->f.fid.Fid.Volume = 0; /* invalid */
942 /* mark entry as modified */
943 adc->dflags |= DFEntryMod;
948 } /*afs_HashOutDCache */
951 * Flush the given dcache entry, pulling it from hash chains
952 * and truncating the associated cache file.
954 * \param adc Ptr to dcache entry to flush.
957 * This routine must be called with the afs_xdcache lock held
961 afs_FlushDCache(register struct dcache *adc)
963 AFS_STATCNT(afs_FlushDCache);
965 * Bump the number of cache files flushed.
967 afs_stats_cmperf.cacheFlushes++;
969 /* remove from all hash tables */
970 afs_HashOutDCache(adc, 1);
972 /* Free its space; special case null operation, since truncate operation
973 * in UFS is slow even in this case, and this allows us to pre-truncate
974 * these files at more convenient times with fewer locks set
975 * (see afs_GetDownD).
977 if (adc->f.chunkBytes != 0) {
978 afs_DiscardDCache(adc);
979 afs_MaybeWakeupTruncateDaemon();
984 if (afs_WaitForCacheDrain) {
985 if (afs_blocksUsed <=
986 PERCENT(CM_CACHESIZEDRAINEDPCT, afs_cacheBlocks)) {
987 afs_WaitForCacheDrain = 0;
988 afs_osi_Wakeup(&afs_WaitForCacheDrain);
991 } /*afs_FlushDCache */
995 * Put a dcache entry on the free dcache entry list.
997 * \param adc dcache entry to free.
999 * \note Environment: called with afs_xdcache lock write-locked.
1002 afs_FreeDCache(register struct dcache *adc)
1004 /* Thread on free list, update free list count and mark entry as
1005 * freed in its indexFlags element. Also, ensure DCache entry gets
1006 * written out (set DFEntryMod).
1009 afs_dvnextTbl[adc->index] = afs_freeDCList;
1010 afs_freeDCList = adc->index;
1012 afs_indexFlags[adc->index] |= IFFree;
1013 adc->dflags |= DFEntryMod;
1015 if (afs_WaitForCacheDrain) {
1016 if ((afs_blocksUsed - afs_blocksDiscarded) <=
1017 PERCENT(CM_CACHESIZEDRAINEDPCT, afs_cacheBlocks)) {
1018 afs_WaitForCacheDrain = 0;
1019 afs_osi_Wakeup(&afs_WaitForCacheDrain);
1022 } /* afs_FreeDCache */
1025 * Discard the cache element by moving it to the discardDCList.
1026 * This puts the cache element into a quasi-freed state, where
1027 * the space may be reused, but the file has not been truncated.
1029 * \note Major Assumptions Here:
1030 * Assumes that frag size is an integral power of two, less one,
1031 * and that this is a two's complement machine. I don't
1032 * know of any filesystems which violate this assumption...
1034 * \param adr Ptr to dcache entry.
1036 * \note Environment:
1037 * Must be called with afs_xdcache write-locked.
1041 afs_DiscardDCache(register struct dcache *adc)
1043 register afs_int32 size;
1045 AFS_STATCNT(afs_DiscardDCache);
1047 osi_Assert(adc->refCount == 1);
1049 size = ((adc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10; /* round up */
1050 afs_blocksDiscarded += size;
1051 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1053 afs_dvnextTbl[adc->index] = afs_discardDCList;
1054 afs_discardDCList = adc->index;
1055 afs_discardDCCount++;
1057 adc->f.fid.Fid.Volume = 0;
1058 adc->dflags |= DFEntryMod;
1059 afs_indexFlags[adc->index] |= IFDiscarded;
1061 if (afs_WaitForCacheDrain) {
1062 if ((afs_blocksUsed - afs_blocksDiscarded) <=
1063 PERCENT(CM_CACHESIZEDRAINEDPCT, afs_cacheBlocks)) {
1064 afs_WaitForCacheDrain = 0;
1065 afs_osi_Wakeup(&afs_WaitForCacheDrain);
1069 } /*afs_DiscardDCache */
1072 * Free the next element on the list of discarded cache elements.
1075 afs_FreeDiscardedDCache(void)
1077 register struct dcache *tdc;
1078 register struct osi_file *tfile;
1079 register afs_int32 size;
1081 AFS_STATCNT(afs_FreeDiscardedDCache);
1083 MObtainWriteLock(&afs_xdcache, 510);
1084 if (!afs_blocksDiscarded) {
1085 MReleaseWriteLock(&afs_xdcache);
1090 * Get an entry from the list of discarded cache elements
1092 tdc = afs_GetDSlot(afs_discardDCList, 0);
1093 osi_Assert(tdc->refCount == 1);
1094 ReleaseReadLock(&tdc->tlock);
1096 afs_discardDCList = afs_dvnextTbl[tdc->index];
1097 afs_dvnextTbl[tdc->index] = NULLIDX;
1098 afs_discardDCCount--;
1099 size = ((tdc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10; /* round up */
1100 afs_blocksDiscarded -= size;
1101 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1102 /* We can lock because we just took it off the free list */
1103 ObtainWriteLock(&tdc->lock, 626);
1104 MReleaseWriteLock(&afs_xdcache);
1107 * Truncate the element to reclaim its space
1109 #if defined(LINUX_USE_FH)
1110 tfile = afs_CFileOpen(&tdc->f.fh, tdc->f.fh_type);
1112 tfile = afs_CFileOpen(tdc->f.inode);
1114 afs_CFileTruncate(tfile, 0);
1115 afs_CFileClose(tfile);
1116 afs_AdjustSize(tdc, 0);
1117 afs_DCMoveBucket(tdc, 0, 0);
1120 * Free the element we just truncated
1122 MObtainWriteLock(&afs_xdcache, 511);
1123 afs_indexFlags[tdc->index] &= ~IFDiscarded;
1124 afs_FreeDCache(tdc);
1125 tdc->f.states &= ~(DRO|DBackup|DRW);
1126 ReleaseWriteLock(&tdc->lock);
1128 MReleaseWriteLock(&afs_xdcache);
1132 * Free as many entries from the list of discarded cache elements
1133 * as we need to get the free space down below CM_WAITFORDRAINPCT (98%).
1138 afs_MaybeFreeDiscardedDCache(void)
1141 AFS_STATCNT(afs_MaybeFreeDiscardedDCache);
1143 while (afs_blocksDiscarded
1144 && (afs_blocksUsed >
1145 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks))) {
1146 afs_FreeDiscardedDCache();
1152 * Try to free up a certain number of disk slots.
1154 * \param anumber Targeted number of disk slots to free up.
1156 * \note Environment:
1157 * Must be called with afs_xdcache write-locked.
1161 afs_GetDownDSlot(int anumber)
1163 struct afs_q *tq, *nq;
1168 AFS_STATCNT(afs_GetDownDSlot);
1169 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
1170 osi_Panic("diskless getdowndslot");
1172 if (CheckLock(&afs_xdcache) != -1)
1173 osi_Panic("getdowndslot nolock");
1175 /* decrement anumber first for all dudes in free list */
1176 for (tdc = afs_freeDSList; tdc; tdc = (struct dcache *)tdc->lruq.next)
1179 return; /* enough already free */
1181 for (cnt = 0, tq = afs_DLRU.prev; tq != &afs_DLRU && anumber > 0;
1183 tdc = (struct dcache *)tq; /* q is first elt in dcache entry */
1184 nq = QPrev(tq); /* in case we remove it */
1185 if (tdc->refCount == 0) {
1186 if ((ix = tdc->index) == NULLIDX)
1187 osi_Panic("getdowndslot");
1188 /* pull the entry out of the lruq and put it on the free list */
1189 QRemove(&tdc->lruq);
1191 /* write-through if modified */
1192 if (tdc->dflags & DFEntryMod) {
1193 #if defined(AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
1195 * ask proxy to do this for us - we don't have the stack space
1197 while (tdc->dflags & DFEntryMod) {
1200 s = SPLOCK(afs_sgibklock);
1201 if (afs_sgibklist == NULL) {
1202 /* if slot is free, grab it. */
1203 afs_sgibklist = tdc;
1204 SV_SIGNAL(&afs_sgibksync);
1206 /* wait for daemon to (start, then) finish. */
1207 SP_WAIT(afs_sgibklock, s, &afs_sgibkwait, PINOD);
1211 tdc->dflags &= ~DFEntryMod;
1212 afs_WriteDCache(tdc, 1);
1216 /* finally put the entry in the free list */
1217 afs_indexTable[ix] = NULL;
1218 afs_indexFlags[ix] &= ~IFEverUsed;
1219 tdc->index = NULLIDX;
1220 tdc->lruq.next = (struct afs_q *)afs_freeDSList;
1221 afs_freeDSList = tdc;
1225 } /*afs_GetDownDSlot */
1232 * Increment the reference count on a disk cache entry,
1233 * which already has a non-zero refcount. In order to
1234 * increment the refcount of a zero-reference entry, you
1235 * have to hold afs_xdcache.
1238 * adc : Pointer to the dcache entry to increment.
1241 * Nothing interesting.
1244 afs_RefDCache(struct dcache *adc)
1246 ObtainWriteLock(&adc->tlock, 627);
1247 if (adc->refCount < 0)
1248 osi_Panic("RefDCache: negative refcount");
1250 ReleaseWriteLock(&adc->tlock);
1259 * Decrement the reference count on a disk cache entry.
1262 * ad : Ptr to the dcache entry to decrement.
1265 * Nothing interesting.
1268 afs_PutDCache(register struct dcache *adc)
1270 AFS_STATCNT(afs_PutDCache);
1271 ObtainWriteLock(&adc->tlock, 276);
1272 if (adc->refCount <= 0)
1273 osi_Panic("putdcache");
1275 ReleaseWriteLock(&adc->tlock);
1284 * Try to discard all data associated with this file from the
1288 * avc : Pointer to the cache info for the file.
1291 * Both pvnLock and lock are write held.
1294 afs_TryToSmush(register struct vcache *avc, struct AFS_UCRED *acred, int sync)
1296 register struct dcache *tdc;
1299 AFS_STATCNT(afs_TryToSmush);
1300 afs_Trace2(afs_iclSetp, CM_TRACE_TRYTOSMUSH, ICL_TYPE_POINTER, avc,
1301 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
1302 sync = 1; /* XX Temp testing XX */
1304 #if defined(AFS_SUN5_ENV)
1305 ObtainWriteLock(&avc->vlock, 573);
1306 avc->activeV++; /* block new getpages */
1307 ReleaseWriteLock(&avc->vlock);
1310 /* Flush VM pages */
1311 osi_VM_TryToSmush(avc, acred, sync);
1314 * Get the hash chain containing all dce's for this fid
1316 i = DVHash(&avc->fid);
1317 MObtainWriteLock(&afs_xdcache, 277);
1318 for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
1319 i = afs_dvnextTbl[index]; /* next pointer this hash table */
1320 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1321 int releaseTlock = 1;
1322 tdc = afs_GetDSlot(index, NULL);
1323 if (!FidCmp(&tdc->f.fid, &avc->fid)) {
1325 if ((afs_indexFlags[index] & IFDataMod) == 0
1326 && tdc->refCount == 1) {
1327 ReleaseReadLock(&tdc->tlock);
1329 afs_FlushDCache(tdc);
1332 afs_indexTable[index] = 0;
1335 ReleaseReadLock(&tdc->tlock);
1339 #if defined(AFS_SUN5_ENV)
1340 ObtainWriteLock(&avc->vlock, 545);
1341 if (--avc->activeV == 0 && (avc->vstates & VRevokeWait)) {
1342 avc->vstates &= ~VRevokeWait;
1343 afs_osi_Wakeup((char *)&avc->vstates);
1345 ReleaseWriteLock(&avc->vlock);
1347 MReleaseWriteLock(&afs_xdcache);
1349 * It's treated like a callback so that when we do lookups we'll
1350 * invalidate the unique bit if any
1351 * trytoSmush occured during the lookup call
1357 * afs_DCacheMissingChunks
1360 * Given the cached info for a file, return the number of chunks that
1361 * are not available from the dcache.
1364 * avc: Pointer to the (held) vcache entry to look in.
1367 * The number of chunks which are not currently cached.
1370 * The vcache entry is held upon entry.
1374 afs_DCacheMissingChunks(struct vcache *avc)
1377 afs_size_t totalLength = 0;
1378 afs_uint32 totalChunks = 0;
1381 totalLength = avc->m.Length;
1382 if (avc->truncPos < totalLength)
1383 totalLength = avc->truncPos;
1385 /* Length is 0, no chunk missing. */
1386 if (totalLength == 0)
1389 /* If totalLength is a multiple of chunksize, the last byte appears
1390 * as being part of the next chunk, which does not exist.
1391 * Decrementing totalLength by one fixes that.
1394 totalChunks = (AFS_CHUNK(totalLength) + 1);
1397 printf("Should have %d chunks for %u bytes\n",
1398 totalChunks, (totalLength + 1));
1400 i = DVHash(&avc->fid);
1401 MObtainWriteLock(&afs_xdcache, 1001);
1402 for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
1403 i = afs_dvnextTbl[index];
1404 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1405 tdc = afs_GetDSlot(index, NULL);
1406 if (!FidCmp(&tdc->f.fid, &avc->fid)) {
1409 ReleaseReadLock(&tdc->tlock);
1413 MReleaseWriteLock(&afs_xdcache);
1415 /*printf("Missing %d chunks\n", totalChunks);*/
1417 return (totalChunks);
1424 * Given the cached info for a file and a byte offset into the
1425 * file, make sure the dcache entry for that file and containing
1426 * the given byte is available, returning it to our caller.
1429 * avc : Pointer to the (held) vcache entry to look in.
1430 * abyte : Which byte we want to get to.
1433 * Pointer to the dcache entry covering the file & desired byte,
1434 * or NULL if not found.
1437 * The vcache entry is held upon entry.
1441 afs_FindDCache(register struct vcache *avc, afs_size_t abyte)
1444 register afs_int32 i, index;
1445 register struct dcache *tdc = NULL;
1447 AFS_STATCNT(afs_FindDCache);
1448 chunk = AFS_CHUNK(abyte);
1451 * Hash on the [fid, chunk] and get the corresponding dcache index
1452 * after write-locking the dcache.
1454 i = DCHash(&avc->fid, chunk);
1455 MObtainWriteLock(&afs_xdcache, 278);
1456 for (index = afs_dchashTbl[i]; index != NULLIDX;) {
1457 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1458 tdc = afs_GetDSlot(index, NULL);
1459 ReleaseReadLock(&tdc->tlock);
1460 if (!FidCmp(&tdc->f.fid, &avc->fid) && chunk == tdc->f.chunk) {
1461 break; /* leaving refCount high for caller */
1465 index = afs_dcnextTbl[index];
1467 if (index != NULLIDX) {
1468 hset(afs_indexTimes[tdc->index], afs_indexCounter);
1469 hadd32(afs_indexCounter, 1);
1470 MReleaseWriteLock(&afs_xdcache);
1473 MReleaseWriteLock(&afs_xdcache);
1475 } /*afs_FindDCache */
1479 * afs_UFSCacheStoreProc
1482 * Called upon store.
1485 * acall : Ptr to the Rx call structure involved.
1486 * afile : Ptr to the related file descriptor.
1487 * alen : Size of the file in bytes.
1488 * avc : Ptr to the vcache entry.
1489 * shouldWake : is it "safe" to return early from close() ?
1490 * abytesToXferP : Set to the number of bytes to xfer.
1491 * NOTE: This parameter is only used if AFS_NOSTATS
1493 * abytesXferredP : Set to the number of bytes actually xferred.
1494 * NOTE: This parameter is only used if AFS_NOSTATS
1498 * Nothing interesting.
1501 afs_UFSCacheStoreProc(register struct rx_call *acall, struct osi_file *afile,
1502 register afs_int32 alen, struct vcache *avc,
1503 int *shouldWake, afs_size_t * abytesToXferP,
1504 afs_size_t * abytesXferredP)
1506 afs_int32 code, got;
1507 register char *tbuffer;
1510 AFS_STATCNT(UFS_CacheStoreProc);
1514 * In this case, alen is *always* the amount of data we'll be trying
1517 (*abytesToXferP) = alen;
1518 (*abytesXferredP) = 0;
1519 #endif /* AFS_NOSTATS */
1521 afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
1522 ICL_TYPE_FID, &(avc->fid), ICL_TYPE_OFFSET,
1523 ICL_HANDLE_OFFSET(avc->m.Length), ICL_TYPE_INT32, alen);
1524 tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1526 tlen = (alen > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : alen);
1527 got = afs_osi_Read(afile, -1, tbuffer, tlen);
1529 #if defined(KERNEL_HAVE_UERROR)
1530 || (got != tlen && getuerror())
1533 osi_FreeLargeSpace(tbuffer);
1536 afs_Trace2(afs_iclSetp, CM_TRACE_STOREPROC2, ICL_TYPE_OFFSET,
1537 ICL_HANDLE_OFFSET(*tbuffer), ICL_TYPE_INT32, got);
1539 code = rx_Write(acall, tbuffer, got); /* writing 0 bytes will
1540 * push a short packet. Is that really what we want, just because the
1541 * data didn't come back from the disk yet? Let's try it and see. */
1544 (*abytesXferredP) += code;
1545 #endif /* AFS_NOSTATS */
1547 code = rx_Error(acall);
1548 osi_FreeLargeSpace(tbuffer);
1549 return code ? code : -33;
1553 * If file has been locked on server, we can allow the store
1556 if (shouldWake && *shouldWake && (rx_GetRemoteStatus(acall) & 1)) {
1557 *shouldWake = 0; /* only do this once */
1561 afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
1562 ICL_TYPE_FID, &(avc->fid), ICL_TYPE_OFFSET,
1563 ICL_HANDLE_OFFSET(avc->m.Length), ICL_TYPE_INT32, alen);
1564 osi_FreeLargeSpace(tbuffer);
1567 } /* afs_UFSCacheStoreProc */
1571 * afs_UFSCacheFetchProc
1574 * Routine called on fetch; also tells people waiting for data
1575 * that more has arrived.
1578 * acall : Ptr to the Rx call structure.
1579 * afile : File descriptor for the cache file.
1580 * abase : Base offset to fetch.
1581 * adc : Ptr to the dcache entry for the file, write-locked.
1582 * avc : Ptr to the vcache entry for the file.
1583 * abytesToXferP : Set to the number of bytes to xfer.
1584 * NOTE: This parameter is only used if AFS_NOSTATS
1586 * abytesXferredP : Set to the number of bytes actually xferred.
1587 * NOTE: This parameter is only used if AFS_NOSTATS
1591 * Nothing interesting.
1595 afs_UFSCacheFetchProc(register struct rx_call *acall, struct osi_file *afile,
1596 afs_size_t abase, struct dcache *adc,
1597 struct vcache *avc, afs_size_t * abytesToXferP,
1598 afs_size_t * abytesXferredP, afs_int32 lengthFound)
1601 register afs_int32 code;
1602 register char *tbuffer;
1606 AFS_STATCNT(UFS_CacheFetchProc);
1607 osi_Assert(WriteLocked(&adc->lock));
1608 afile->offset = 0; /* Each time start from the beginning */
1609 length = lengthFound;
1611 (*abytesToXferP) = 0;
1612 (*abytesXferredP) = 0;
1613 #endif /* AFS_NOSTATS */
1614 tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1615 adc->validPos = abase;
1619 code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
1621 length = ntohl(length);
1622 if (code != sizeof(afs_int32)) {
1623 osi_FreeLargeSpace(tbuffer);
1624 code = rx_Error(acall);
1625 return (code ? code : -1); /* try to return code, not -1 */
1629 * The fetch protocol is extended for the AFS/DFS translator
1630 * to allow multiple blocks of data, each with its own length,
1631 * to be returned. As long as the top bit is set, there are more
1634 * We do not do this for AFS file servers because they sometimes
1635 * return large negative numbers as the transfer size.
1637 if (avc->states & CForeign) {
1638 moredata = length & 0x80000000;
1639 length &= ~0x80000000;
1644 (*abytesToXferP) += length;
1645 #endif /* AFS_NOSTATS */
1646 while (length > 0) {
1647 tlen = (length > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : length);
1648 #ifdef RX_KERNEL_TRACE
1649 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1653 code = rx_Read(acall, tbuffer, tlen);
1655 #ifdef RX_KERNEL_TRACE
1656 afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1660 (*abytesXferredP) += code;
1661 #endif /* AFS_NOSTATS */
1663 osi_FreeLargeSpace(tbuffer);
1664 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
1665 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
1666 ICL_TYPE_INT32, length);
1669 code = afs_osi_Write(afile, -1, tbuffer, tlen);
1671 osi_FreeLargeSpace(tbuffer);
1676 adc->validPos = abase;
1677 if (afs_osi_Wakeup(&adc->validPos) == 0)
1678 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
1679 __FILE__, ICL_TYPE_INT32, __LINE__,
1680 ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
1684 osi_FreeLargeSpace(tbuffer);
1687 } /* afs_UFSCacheFetchProc */
1690 * Get a fresh dcache from the free or discarded list.
1692 * \param avc Who's dcache is this going to be?
1693 * \param chunk The position where it will be placed in.
1694 * \param lock How are locks held.
1695 * \param ashFid If this dcache going to be used for a shadow dir,
1698 * \note Required locks:
1700 * - avc (R if (lock & 1) set and W otherwise)
1701 * \note It write locks the new dcache. The caller must unlock it.
1703 * \return The new dcache.
1705 struct dcache *afs_AllocDCache(struct vcache *avc,
1708 struct VenusFid *ashFid)
1710 struct dcache *tdc = NULL;
1711 afs_uint32 size = 0;
1712 struct osi_file *file;
1714 if (afs_discardDCList == NULLIDX
1715 || ((lock & 2) && afs_freeDCList != NULLIDX)) {
1717 afs_indexFlags[afs_freeDCList] &= ~IFFree;
1718 tdc = afs_GetDSlot(afs_freeDCList, 0);
1719 osi_Assert(tdc->refCount == 1);
1720 ReleaseReadLock(&tdc->tlock);
1721 ObtainWriteLock(&tdc->lock, 604);
1722 afs_freeDCList = afs_dvnextTbl[tdc->index];
1725 afs_indexFlags[afs_discardDCList] &= ~IFDiscarded;
1726 tdc = afs_GetDSlot(afs_discardDCList, 0);
1727 osi_Assert(tdc->refCount == 1);
1728 ReleaseReadLock(&tdc->tlock);
1729 ObtainWriteLock(&tdc->lock, 605);
1730 afs_discardDCList = afs_dvnextTbl[tdc->index];
1731 afs_discardDCCount--;
1733 ((tdc->f.chunkBytes +
1734 afs_fsfragsize) ^ afs_fsfragsize) >> 10;
1735 tdc->f.states &= ~(DRO|DBackup|DRW);
1736 afs_DCMoveBucket(tdc, size, 0);
1737 afs_blocksDiscarded -= size;
1738 afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1740 /* Truncate the chunk so zeroes get filled properly */
1741 #if defined(LINUX_USE_FH)
1742 file = afs_CFileOpen(&tdc->f.fh, tdc->f.fh_type);
1744 file = afs_CFileOpen(tdc->f.inode);
1746 afs_CFileTruncate(file, 0);
1747 afs_CFileClose(file);
1748 afs_AdjustSize(tdc, 0);
1754 * avc->lock(R) if setLocks
1755 * avc->lock(W) if !setLocks
1761 * Fill in the newly-allocated dcache record.
1763 afs_indexFlags[tdc->index] &= ~(IFDirtyPages | IFAnyPages);
1765 /* Use shadow fid if provided. */
1766 tdc->f.fid = *ashFid;
1768 /* Use normal vcache's fid otherwise. */
1769 tdc->f.fid = avc->fid;
1770 if (avc->states & CRO)
1771 tdc->f.states = DRO;
1772 else if (avc->states & CBackup)
1773 tdc->f.states = DBackup;
1775 tdc->f.states = DRW;
1776 afs_DCMoveBucket(tdc, 0, afs_DCGetBucket(avc));
1777 afs_indexUnique[tdc->index] = tdc->f.fid.Fid.Unique;
1779 hones(tdc->f.versionNo); /* invalid value */
1780 tdc->f.chunk = chunk;
1781 tdc->validPos = AFS_CHUNKTOBASE(chunk);
1783 if (tdc->lruq.prev == &tdc->lruq)
1784 osi_Panic("lruq 1");
1793 * This function is called to obtain a reference to data stored in
1794 * the disk cache, locating a chunk of data containing the desired
1795 * byte and returning a reference to the disk cache entry, with its
1796 * reference count incremented.
1800 * avc : Ptr to a vcache entry (unlocked)
1801 * abyte : Byte position in the file desired
1802 * areq : Request structure identifying the requesting user.
1803 * aflags : Settings as follows:
1805 * 2 : Return after creating entry.
1806 * 4 : called from afs_vnop_write.c
1807 * *alen contains length of data to be written.
1809 * aoffset : Set to the offset within the chunk where the resident
1811 * alen : Set to the number of bytes of data after the desired
1812 * byte (including the byte itself) which can be read
1816 * The vcache entry pointed to by avc is unlocked upon entry.
1820 struct AFSVolSync tsync;
1821 struct AFSFetchStatus OutStatus;
1822 struct AFSCallBack CallBack;
1826 * Update the vnode-to-dcache hint if we can get the vnode lock
1827 * right away. Assumes dcache entry is at least read-locked.
1830 updateV2DC(int lockVc, struct vcache *v, struct dcache *d, int src)
1832 if (!lockVc || 0 == NBObtainWriteLock(&v->lock, src)) {
1833 if (hsame(v->m.DataVersion, d->f.versionNo) && v->callback)
1836 ReleaseWriteLock(&v->lock);
1840 /* avc - Write-locked unless aflags & 1 */
1842 afs_GetDCache(register struct vcache *avc, afs_size_t abyte,
1843 register struct vrequest *areq, afs_size_t * aoffset,
1844 afs_size_t * alen, int aflags)
1846 register afs_int32 i, code, code1 = 0, shortcut;
1847 #if defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
1848 register afs_int32 adjustsize = 0;
1854 afs_size_t maxGoodLength; /* amount of good data at server */
1855 struct rx_call *tcall;
1856 afs_size_t Position = 0;
1857 #ifdef AFS_64BIT_CLIENT
1859 afs_size_t lengthFound; /* as returned from server */
1860 #endif /* AFS_64BIT_CLIENT */
1861 afs_int32 size, tlen; /* size of segment to transfer */
1862 struct tlocal1 *tsmall = 0;
1863 register struct dcache *tdc;
1864 register struct osi_file *file;
1865 register struct conn *tc;
1867 struct server *newCallback = NULL;
1868 char setNewCallback;
1869 char setVcacheStatus;
1870 char doVcacheUpdate;
1872 int doAdjustSize = 0;
1873 int doReallyAdjustSize = 0;
1874 int overWriteWholeChunk = 0;
1878 struct afs_stats_xferData *xferP; /* Ptr to this op's xfer struct */
1879 osi_timeval_t xferStartTime, /*FS xfer start time */
1880 xferStopTime; /*FS xfer stop time */
1881 afs_size_t bytesToXfer; /* # bytes to xfer */
1882 afs_size_t bytesXferred; /* # bytes actually xferred */
1883 struct afs_stats_AccessInfo *accP; /*Ptr to access record in stats */
1884 int fromReplica; /*Are we reading from a replica? */
1885 int numFetchLoops; /*# times around the fetch/analyze loop */
1886 #endif /* AFS_NOSTATS */
1888 AFS_STATCNT(afs_GetDCache);
1892 setLocks = aflags & 1;
1895 * Determine the chunk number and offset within the chunk corresponding
1896 * to the desired byte.
1898 if (avc->fid.Fid.Vnode & 1) { /* if (vType(avc) == VDIR) */
1901 chunk = AFS_CHUNK(abyte);
1904 /* come back to here if we waited for the cache to drain. */
1907 setNewCallback = setVcacheStatus = 0;
1911 ObtainWriteLock(&avc->lock, 616);
1913 ObtainReadLock(&avc->lock);
1918 * avc->lock(R) if setLocks && !slowPass
1919 * avc->lock(W) if !setLocks || slowPass
1924 /* check hints first! (might could use bcmp or some such...) */
1925 if ((tdc = avc->dchint)) {
1929 * The locking order between afs_xdcache and dcache lock matters.
1930 * The hint dcache entry could be anywhere, even on the free list.
1931 * Locking afs_xdcache ensures that noone is trying to pull dcache
1932 * entries from the free list, and thereby assuming them to be not
1933 * referenced and not locked.
1935 MObtainReadLock(&afs_xdcache);
1936 dcLocked = (0 == NBObtainSharedLock(&tdc->lock, 601));
1938 if (dcLocked && (tdc->index != NULLIDX)
1939 && !FidCmp(&tdc->f.fid, &avc->fid) && chunk == tdc->f.chunk
1940 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1941 /* got the right one. It might not be the right version, and it
1942 * might be fetching, but it's the right dcache entry.
1944 /* All this code should be integrated better with what follows:
1945 * I can save a good bit more time under a write lock if I do..
1947 ObtainWriteLock(&tdc->tlock, 603);
1949 ReleaseWriteLock(&tdc->tlock);
1951 MReleaseReadLock(&afs_xdcache);
1954 if (hsame(tdc->f.versionNo, avc->m.DataVersion)
1955 && !(tdc->dflags & DFFetching)) {
1957 afs_stats_cmperf.dcacheHits++;
1958 MObtainWriteLock(&afs_xdcache, 559);
1959 QRemove(&tdc->lruq);
1960 QAdd(&afs_DLRU, &tdc->lruq);
1961 MReleaseWriteLock(&afs_xdcache);
1964 * avc->lock(R) if setLocks && !slowPass
1965 * avc->lock(W) if !setLocks || slowPass
1972 ReleaseSharedLock(&tdc->lock);
1973 MReleaseReadLock(&afs_xdcache);
1981 * avc->lock(R) if setLocks && !slowPass
1982 * avc->lock(W) if !setLocks || slowPass
1983 * tdc->lock(S) if tdc
1986 if (!tdc) { /* If the hint wasn't the right dcache entry */
1988 * Hash on the [fid, chunk] and get the corresponding dcache index
1989 * after write-locking the dcache.
1994 * avc->lock(R) if setLocks && !slowPass
1995 * avc->lock(W) if !setLocks || slowPass
1998 i = DCHash(&avc->fid, chunk);
1999 /* check to make sure our space is fine */
2000 afs_MaybeWakeupTruncateDaemon();
2002 MObtainWriteLock(&afs_xdcache, 280);
2004 for (index = afs_dchashTbl[i]; index != NULLIDX;) {
2005 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
2006 tdc = afs_GetDSlot(index, NULL);
2007 ReleaseReadLock(&tdc->tlock);
2010 * avc->lock(R) if setLocks && !slowPass
2011 * avc->lock(W) if !setLocks || slowPass
2014 if (!FidCmp(&tdc->f.fid, &avc->fid) && chunk == tdc->f.chunk) {
2015 /* Move it up in the beginning of the list */
2016 if (afs_dchashTbl[i] != index) {
2017 afs_dcnextTbl[us] = afs_dcnextTbl[index];
2018 afs_dcnextTbl[index] = afs_dchashTbl[i];
2019 afs_dchashTbl[i] = index;
2021 MReleaseWriteLock(&afs_xdcache);
2022 ObtainSharedLock(&tdc->lock, 606);
2023 break; /* leaving refCount high for caller */
2029 index = afs_dcnextTbl[index];
2033 * If we didn't find the entry, we'll create one.
2035 if (index == NULLIDX) {
2038 * avc->lock(R) if setLocks
2039 * avc->lock(W) if !setLocks
2042 afs_Trace2(afs_iclSetp, CM_TRACE_GETDCACHE1, ICL_TYPE_POINTER,
2043 avc, ICL_TYPE_INT32, chunk);
2045 /* Make sure there is a free dcache entry for us to use */
2046 if (afs_discardDCList == NULLIDX && afs_freeDCList == NULLIDX) {
2049 avc->states |= CDCLock;
2050 /* just need slots */
2051 afs_GetDownD(5, (int *)0, afs_DCGetBucket(avc));
2053 avc->states &= ~CDCLock;
2054 if (afs_discardDCList != NULLIDX
2055 || afs_freeDCList != NULLIDX)
2057 /* If we can't get space for 5 mins we give up and panic */
2058 if (++downDCount > 300) {
2059 #if defined(AFS_CACHE_BYPASS)
2060 afs_warn("GetDCache calling osi_Panic: No space in five minutes.\n downDCount: %d\n aoffset: %d alen: %d\n", downDCount, aoffset, alen);
2062 osi_Panic("getdcache");
2064 MReleaseWriteLock(&afs_xdcache);
2067 * avc->lock(R) if setLocks
2068 * avc->lock(W) if !setLocks
2070 afs_osi_Wait(1000, 0, 0);
2075 tdc = afs_AllocDCache(avc, chunk, aflags, NULL);
2078 * Now add to the two hash chains - note that i is still set
2079 * from the above DCHash call.
2081 afs_dcnextTbl[tdc->index] = afs_dchashTbl[i];
2082 afs_dchashTbl[i] = tdc->index;
2083 i = DVHash(&avc->fid);
2084 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[i];
2085 afs_dvhashTbl[i] = tdc->index;
2086 tdc->dflags = DFEntryMod;
2088 afs_MaybeWakeupTruncateDaemon();
2089 MReleaseWriteLock(&afs_xdcache);
2090 ConvertWToSLock(&tdc->lock);
2095 /* vcache->dcache hint failed */
2098 * avc->lock(R) if setLocks && !slowPass
2099 * avc->lock(W) if !setLocks || slowPass
2102 afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE2, ICL_TYPE_POINTER, avc,
2103 ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
2104 hgetlo(tdc->f.versionNo), ICL_TYPE_INT32,
2105 hgetlo(avc->m.DataVersion));
2107 * Here we have the entry in tdc, with its refCount incremented.
2108 * Note: we don't use the S-lock on avc; it costs concurrency when
2109 * storing a file back to the server.
2113 * Not a newly created file so we need to check the file's length and
2114 * compare data versions since someone could have changed the data or we're
2115 * reading a file written elsewhere. We only want to bypass doing no-op
2116 * read rpcs on newly created files (dv of 0) since only then we guarantee
2117 * that this chunk's data hasn't been filled by another client.
2119 size = AFS_CHUNKSIZE(abyte);
2120 if (aflags & 4) /* called from write */
2122 else /* called from read */
2123 tlen = tdc->validPos - abyte;
2124 Position = AFS_CHUNKTOBASE(chunk);
2125 afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE3, ICL_TYPE_INT32, tlen,
2126 ICL_TYPE_INT32, aflags, ICL_TYPE_OFFSET,
2127 ICL_HANDLE_OFFSET(abyte), ICL_TYPE_OFFSET,
2128 ICL_HANDLE_OFFSET(Position));
2129 if ((aflags & 4) && (hiszero(avc->m.DataVersion)))
2131 if ((AFS_CHUNKTOBASE(chunk) >= avc->m.Length) ||
2132 ((aflags & 4) && (abyte == Position) && (tlen >= size)))
2133 overWriteWholeChunk = 1;
2134 if (doAdjustSize || overWriteWholeChunk) {
2135 #if defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
2137 #ifdef AFS_SGI64_ENV
2140 #else /* AFS_SGI64_ENV */
2143 #endif /* AFS_SGI64_ENV */
2144 #else /* AFS_SGI_ENV */
2147 #endif /* AFS_SGI_ENV */
2148 if (AFS_CHUNKTOBASE(chunk) + adjustsize >= avc->m.Length &&
2149 #else /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
2150 #if defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV)
2151 if ((doAdjustSize || (AFS_CHUNKTOBASE(chunk) >= avc->m.Length)) &&
2153 if (AFS_CHUNKTOBASE(chunk) >= avc->m.Length &&
2155 #endif /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
2156 !hsame(avc->m.DataVersion, tdc->f.versionNo))
2157 doReallyAdjustSize = 1;
2159 if (doReallyAdjustSize || overWriteWholeChunk) {
2160 /* no data in file to read at this position */
2161 UpgradeSToWLock(&tdc->lock, 607);
2163 #if defined(LINUX_USE_FH)
2164 file = afs_CFileOpen(&tdc->f.fh, tdc->f.fh_type);
2166 file = afs_CFileOpen(tdc->f.inode);
2168 afs_CFileTruncate(file, 0);
2169 afs_CFileClose(file);
2170 afs_AdjustSize(tdc, 0);
2171 hset(tdc->f.versionNo, avc->m.DataVersion);
2172 tdc->dflags |= DFEntryMod;
2174 ConvertWToSLock(&tdc->lock);
2179 * We must read in the whole chunk if the version number doesn't
2183 /* don't need data, just a unique dcache entry */
2184 ObtainWriteLock(&afs_xdcache, 608);
2185 hset(afs_indexTimes[tdc->index], afs_indexCounter);
2186 hadd32(afs_indexCounter, 1);
2187 ReleaseWriteLock(&afs_xdcache);
2189 updateV2DC(setLocks, avc, tdc, 553);
2190 if (vType(avc) == VDIR)
2193 *aoffset = AFS_CHUNKOFFSET(abyte);
2194 if (tdc->validPos < abyte)
2195 *alen = (afs_size_t) 0;
2197 *alen = tdc->validPos - abyte;
2198 ReleaseSharedLock(&tdc->lock);
2201 ReleaseWriteLock(&avc->lock);
2203 ReleaseReadLock(&avc->lock);
2205 return tdc; /* check if we're done */
2210 * avc->lock(R) if setLocks && !slowPass
2211 * avc->lock(W) if !setLocks || slowPass
2214 osi_Assert((setLocks && !slowPass) || WriteLocked(&avc->lock));
2216 setNewCallback = setVcacheStatus = 0;
2220 * avc->lock(R) if setLocks && !slowPass
2221 * avc->lock(W) if !setLocks || slowPass
2224 if (!hsame(avc->m.DataVersion, tdc->f.versionNo) && !overWriteWholeChunk) {
2226 * Version number mismatch.
2229 * If we are disconnected, then we can't do much of anything
2230 * because the data doesn't match the file.
2232 if (AFS_IS_DISCONNECTED) {
2233 ReleaseSharedLock(&tdc->lock);
2236 ReleaseWriteLock(&avc->lock);
2238 ReleaseReadLock(&avc->lock);
2240 /* Flush the Dcache */
2245 UpgradeSToWLock(&tdc->lock, 609);
2248 * If data ever existed for this vnode, and this is a text object,
2249 * do some clearing. Now, you'd think you need only do the flush
2250 * when VTEXT is on, but VTEXT is turned off when the text object
2251 * is freed, while pages are left lying around in memory marked
2252 * with this vnode. If we would reactivate (create a new text
2253 * object from) this vnode, we could easily stumble upon some of
2254 * these old pages in pagein. So, we always flush these guys.
2255 * Sun has a wonderful lack of useful invariants in this system.
2257 * avc->flushDV is the data version # of the file at the last text
2258 * flush. Clearly, at least, we don't have to flush the file more
2259 * often than it changes
2261 if (hcmp(avc->flushDV, avc->m.DataVersion) < 0) {
2263 * By here, the cache entry is always write-locked. We can
2264 * deadlock if we call osi_Flush with the cache entry locked...
2265 * Unlock the dcache too.
2267 ReleaseWriteLock(&tdc->lock);
2268 if (setLocks && !slowPass)
2269 ReleaseReadLock(&avc->lock);
2271 ReleaseWriteLock(&avc->lock);
2275 * Call osi_FlushPages in open, read/write, and map, since it
2276 * is too hard here to figure out if we should lock the
2279 if (setLocks && !slowPass)
2280 ObtainReadLock(&avc->lock);
2282 ObtainWriteLock(&avc->lock, 66);
2283 ObtainWriteLock(&tdc->lock, 610);
2288 * avc->lock(R) if setLocks && !slowPass
2289 * avc->lock(W) if !setLocks || slowPass
2293 /* Watch for standard race condition around osi_FlushText */
2294 if (hsame(avc->m.DataVersion, tdc->f.versionNo)) {
2295 updateV2DC(setLocks, avc, tdc, 569); /* set hint */
2296 afs_stats_cmperf.dcacheHits++;
2297 ConvertWToSLock(&tdc->lock);
2301 /* Sleep here when cache needs to be drained. */
2302 if (setLocks && !slowPass
2303 && (afs_blocksUsed >
2304 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks))) {
2305 /* Make sure truncate daemon is running */
2306 afs_MaybeWakeupTruncateDaemon();
2307 ObtainWriteLock(&tdc->tlock, 614);
2308 tdc->refCount--; /* we'll re-obtain the dcache when we re-try. */
2309 ReleaseWriteLock(&tdc->tlock);
2310 ReleaseWriteLock(&tdc->lock);
2311 ReleaseReadLock(&avc->lock);
2312 while ((afs_blocksUsed - afs_blocksDiscarded) >
2313 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
2314 afs_WaitForCacheDrain = 1;
2315 afs_osi_Sleep(&afs_WaitForCacheDrain);
2317 afs_MaybeFreeDiscardedDCache();
2318 /* need to check if someone else got the chunk first. */
2319 goto RetryGetDCache;
2322 /* Do not fetch data beyond truncPos. */
2323 maxGoodLength = avc->m.Length;
2324 if (avc->truncPos < maxGoodLength)
2325 maxGoodLength = avc->truncPos;
2326 Position = AFS_CHUNKBASE(abyte);
2327 if (vType(avc) == VDIR) {
2328 size = avc->m.Length;
2329 if (size > tdc->f.chunkBytes) {
2330 /* pre-reserve space for file */
2331 afs_AdjustSize(tdc, size);
2333 size = 999999999; /* max size for transfer */
2335 size = AFS_CHUNKSIZE(abyte); /* expected max size */
2336 /* don't read past end of good data on server */
2337 if (Position + size > maxGoodLength)
2338 size = maxGoodLength - Position;
2340 size = 0; /* Handle random races */
2341 if (size > tdc->f.chunkBytes) {
2342 /* pre-reserve space for file */
2343 afs_AdjustSize(tdc, size); /* changes chunkBytes */
2344 /* max size for transfer still in size */
2347 if (afs_mariner && !tdc->f.chunk)
2348 afs_MarinerLog("fetch$Fetching", avc); /* , Position, size, afs_indexCounter ); */
2350 * Right now, we only have one tool, and it's a hammer. So, we
2351 * fetch the whole file.
2353 DZap(tdc); /* pages in cache may be old */
2354 #if defined(LINUX_USE_FH)
2355 file = afs_CFileOpen(&tdc->f.fh, tdc->f.fh_type);
2357 file = afs_CFileOpen(tdc->f.inode);
2359 afs_RemoveVCB(&avc->fid);
2360 tdc->f.states |= DWriting;
2361 tdc->dflags |= DFFetching;
2362 tdc->validPos = Position; /* which is AFS_CHUNKBASE(abyte) */
2363 if (tdc->mflags & DFFetchReq) {
2364 tdc->mflags &= ~DFFetchReq;
2365 if (afs_osi_Wakeup(&tdc->validPos) == 0)
2366 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
2367 __FILE__, ICL_TYPE_INT32, __LINE__,
2368 ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
2372 (struct tlocal1 *)osi_AllocLargeSpace(sizeof(struct tlocal1));
2373 setVcacheStatus = 0;
2376 * Remember if we are doing the reading from a replicated volume,
2377 * and how many times we've zipped around the fetch/analyze loop.
2379 fromReplica = (avc->states & CRO) ? 1 : 0;
2381 accP = &(afs_stats_cmfullperf.accessinf);
2383 (accP->replicatedRefs)++;
2385 (accP->unreplicatedRefs)++;
2386 #endif /* AFS_NOSTATS */
2387 /* this is a cache miss */
2388 afs_Trace4(afs_iclSetp, CM_TRACE_FETCHPROC, ICL_TYPE_POINTER, avc,
2389 ICL_TYPE_FID, &(avc->fid), ICL_TYPE_OFFSET,
2390 ICL_HANDLE_OFFSET(Position), ICL_TYPE_INT32, size);
2393 afs_stats_cmperf.dcacheMisses++;
2396 * Dynamic root support: fetch data from local memory.
2398 if (afs_IsDynroot(avc)) {
2402 afs_GetDynroot(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2404 dynrootDir += Position;
2405 dynrootLen -= Position;
2406 if (size > dynrootLen)
2410 code = afs_CFileWrite(file, 0, dynrootDir, size);
2418 tdc->validPos = Position + size;
2419 afs_CFileTruncate(file, size); /* prune it */
2420 } else if (afs_IsDynrootMount(avc)) {
2424 afs_GetDynrootMount(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2426 dynrootDir += Position;
2427 dynrootLen -= Position;
2428 if (size > dynrootLen)
2432 code = afs_CFileWrite(file, 0, dynrootDir, size);
2440 tdc->validPos = Position + size;
2441 afs_CFileTruncate(file, size); /* prune it */
2444 * Not a dynamic vnode: do the real fetch.
2449 * avc->lock(R) if setLocks && !slowPass
2450 * avc->lock(W) if !setLocks || slowPass
2454 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
2456 afs_int32 length_hi, length, bytes;
2460 (accP->numReplicasAccessed)++;
2462 #endif /* AFS_NOSTATS */
2463 if (!setLocks || slowPass) {
2464 avc->callback = tc->srvr->server;
2466 newCallback = tc->srvr->server;
2471 tcall = rx_NewCall(tc->id);
2474 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
2475 #ifdef AFS_64BIT_CLIENT
2476 length_hi = code = 0;
2477 if (!afs_serverHasNo64Bit(tc)) {
2481 StartRXAFS_FetchData64(tcall,
2482 (struct AFSFid *)&avc->fid.
2483 Fid, Position, tsize);
2486 afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
2487 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32,
2491 rx_Read(tcall, (char *)&length_hi,
2494 if (bytes == sizeof(afs_int32)) {
2495 length_hi = ntohl(length_hi);
2498 code = rx_Error(tcall);
2500 code1 = rx_EndCall(tcall, code);
2502 tcall = (struct rx_call *)0;
2506 if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
2507 if (Position > 0x7FFFFFFF) {
2514 tcall = rx_NewCall(tc->id);
2516 StartRXAFS_FetchData(tcall, (struct AFSFid *)
2521 afs_serverSetNo64Bit(tc);
2526 rx_Read(tcall, (char *)&length,
2529 if (bytes == sizeof(afs_int32)) {
2530 length = ntohl(length);
2532 code = rx_Error(tcall);
2535 FillInt64(lengthFound, length_hi, length);
2536 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
2537 ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
2539 ICL_HANDLE_OFFSET(lengthFound));
2540 #else /* AFS_64BIT_CLIENT */
2543 StartRXAFS_FetchData(tcall,
2544 (struct AFSFid *)&avc->fid.Fid,
2550 rx_Read(tcall, (char *)&length,
2553 if (bytes == sizeof(afs_int32)) {
2554 length = ntohl(length);
2556 code = rx_Error(tcall);
2559 #endif /* AFS_64BIT_CLIENT */
2564 &(afs_stats_cmfullperf.rpc.
2565 fsXferTimes[AFS_STATS_FS_XFERIDX_FETCHDATA]);
2566 osi_GetuTime(&xferStartTime);
2569 afs_CacheFetchProc(tcall, file,
2570 (afs_size_t) Position, tdc,
2572 &bytesXferred, length);
2574 osi_GetuTime(&xferStopTime);
2575 (xferP->numXfers)++;
2577 (xferP->numSuccesses)++;
2578 afs_stats_XferSumBytes
2579 [AFS_STATS_FS_XFERIDX_FETCHDATA] +=
2581 (xferP->sumBytes) +=
2582 (afs_stats_XferSumBytes
2583 [AFS_STATS_FS_XFERIDX_FETCHDATA] >> 10);
2584 afs_stats_XferSumBytes
2585 [AFS_STATS_FS_XFERIDX_FETCHDATA] &= 0x3FF;
2586 if (bytesXferred < xferP->minBytes)
2587 xferP->minBytes = bytesXferred;
2588 if (bytesXferred > xferP->maxBytes)
2589 xferP->maxBytes = bytesXferred;
2592 * Tally the size of the object. Note: we tally the actual size,
2593 * NOT the number of bytes that made it out over the wire.
2595 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
2596 (xferP->count[0])++;
2597 else if (bytesToXfer <=
2598 AFS_STATS_MAXBYTES_BUCKET1)
2599 (xferP->count[1])++;
2600 else if (bytesToXfer <=
2601 AFS_STATS_MAXBYTES_BUCKET2)
2602 (xferP->count[2])++;
2603 else if (bytesToXfer <=
2604 AFS_STATS_MAXBYTES_BUCKET3)
2605 (xferP->count[3])++;
2606 else if (bytesToXfer <=
2607 AFS_STATS_MAXBYTES_BUCKET4)
2608 (xferP->count[4])++;
2609 else if (bytesToXfer <=
2610 AFS_STATS_MAXBYTES_BUCKET5)
2611 (xferP->count[5])++;
2612 else if (bytesToXfer <=
2613 AFS_STATS_MAXBYTES_BUCKET6)
2614 (xferP->count[6])++;
2615 else if (bytesToXfer <=
2616 AFS_STATS_MAXBYTES_BUCKET7)
2617 (xferP->count[7])++;
2619 (xferP->count[8])++;
2621 afs_stats_GetDiff(elapsedTime, xferStartTime,
2623 afs_stats_AddTo((xferP->sumTime), elapsedTime);
2624 afs_stats_SquareAddTo((xferP->sqrTime),
2626 if (afs_stats_TimeLessThan
2627 (elapsedTime, (xferP->minTime))) {
2628 afs_stats_TimeAssign((xferP->minTime),
2631 if (afs_stats_TimeGreaterThan
2632 (elapsedTime, (xferP->maxTime))) {
2633 afs_stats_TimeAssign((xferP->maxTime),
2639 afs_CacheFetchProc(tcall, file, Position, tdc,
2641 #endif /* AFS_NOSTATS */
2646 EndRXAFS_FetchData(tcall, &tsmall->OutStatus,
2654 code1 = rx_EndCall(tcall, code);
2663 /* callback could have been broken (or expired) in a race here,
2664 * but we return the data anyway. It's as good as we knew about
2665 * when we started. */
2667 * validPos is updated by CacheFetchProc, and can only be
2668 * modifed under a dcache write lock, which we've blocked out
2670 size = tdc->validPos - Position; /* actual segment size */
2673 afs_CFileTruncate(file, size); /* prune it */
2675 if (!setLocks || slowPass) {
2676 ObtainWriteLock(&afs_xcbhash, 453);
2677 afs_DequeueCallback(avc);
2678 avc->states &= ~(CStatd | CUnique);
2679 avc->callback = NULL;
2680 ReleaseWriteLock(&afs_xcbhash);
2681 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
2682 osi_dnlc_purgedp(avc);
2684 /* Something lost. Forget about performance, and go
2685 * back with a vcache write lock.
2687 afs_CFileTruncate(file, 0);
2688 afs_AdjustSize(tdc, 0);
2689 afs_CFileClose(file);
2690 osi_FreeLargeSpace(tsmall);
2692 ReleaseWriteLock(&tdc->lock);
2695 ReleaseReadLock(&avc->lock);
2697 goto RetryGetDCache;
2701 } while (afs_Analyze
2702 (tc, code, &avc->fid, areq,
2703 AFS_STATS_FS_RPCIDX_FETCHDATA, SHARED_LOCK, NULL));
2707 * avc->lock(R) if setLocks && !slowPass
2708 * avc->lock(W) if !setLocks || slowPass
2714 * In the case of replicated access, jot down info on the number of
2715 * attempts it took before we got through or gave up.
2718 if (numFetchLoops <= 1)
2719 (accP->refFirstReplicaOK)++;
2720 if (numFetchLoops > accP->maxReplicasPerRef)
2721 accP->maxReplicasPerRef = numFetchLoops;
2723 #endif /* AFS_NOSTATS */
2725 tdc->dflags &= ~DFFetching;
2726 if (afs_osi_Wakeup(&tdc->validPos) == 0)
2727 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
2728 __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
2729 tdc, ICL_TYPE_INT32, tdc->dflags);
2730 if (avc->execsOrWriters == 0)
2731 tdc->f.states &= ~DWriting;
2733 /* now, if code != 0, we have an error and should punt.
2734 * note that we have the vcache write lock, either because
2735 * !setLocks or slowPass.
2738 afs_CFileTruncate(file, 0);
2739 afs_AdjustSize(tdc, 0);
2740 afs_CFileClose(file);
2741 ZapDCE(tdc); /* sets DFEntryMod */
2742 if (vType(avc) == VDIR) {
2745 tdc->f.states &= ~(DRO|DBackup|DRW);
2746 afs_DCMoveBucket(tdc, 0, 0);
2747 ReleaseWriteLock(&tdc->lock);
2749 if (!afs_IsDynroot(avc)) {
2750 ObtainWriteLock(&afs_xcbhash, 454);
2751 afs_DequeueCallback(avc);
2752 avc->states &= ~(CStatd | CUnique);
2753 ReleaseWriteLock(&afs_xcbhash);
2754 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
2755 osi_dnlc_purgedp(avc);
2758 * avc->lock(W); assert(!setLocks || slowPass)
2760 osi_Assert(!setLocks || slowPass);
2762 tdc->f.states &= ~(DRO|DBackup|DRW);
2763 afs_DCMoveBucket(tdc, 0, 0);
2768 /* otherwise we copy in the just-fetched info */
2769 afs_CFileClose(file);
2770 afs_AdjustSize(tdc, size); /* new size */
2772 * Copy appropriate fields into vcache. Status is
2773 * copied later where we selectively acquire the
2774 * vcache write lock.
2777 afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2779 setVcacheStatus = 1;
2780 hset64(tdc->f.versionNo, tsmall->OutStatus.dataVersionHigh,
2781 tsmall->OutStatus.DataVersion);
2782 tdc->dflags |= DFEntryMod;
2783 afs_indexFlags[tdc->index] |= IFEverUsed;
2784 ConvertWToSLock(&tdc->lock);
2785 } /*Data version numbers don't match */
2788 * Data version numbers match.
2790 afs_stats_cmperf.dcacheHits++;
2791 } /*Data version numbers match */
2793 updateV2DC(setLocks, avc, tdc, 335); /* set hint */
2797 * avc->lock(R) if setLocks && !slowPass
2798 * avc->lock(W) if !setLocks || slowPass
2799 * tdc->lock(S) if tdc
2803 * See if this was a reference to a file in the local cell.
2805 if (afs_IsPrimaryCellNum(avc->fid.Cell))
2806 afs_stats_cmperf.dlocalAccesses++;
2808 afs_stats_cmperf.dremoteAccesses++;
2810 /* Fix up LRU info */
2813 MObtainWriteLock(&afs_xdcache, 602);
2814 hset(afs_indexTimes[tdc->index], afs_indexCounter);
2815 hadd32(afs_indexCounter, 1);
2816 MReleaseWriteLock(&afs_xdcache);
2818 /* return the data */
2819 if (vType(avc) == VDIR)
2822 *aoffset = AFS_CHUNKOFFSET(abyte);
2823 *alen = (tdc->f.chunkBytes - *aoffset);
2824 ReleaseSharedLock(&tdc->lock);
2829 * avc->lock(R) if setLocks && !slowPass
2830 * avc->lock(W) if !setLocks || slowPass
2833 /* Fix up the callback and status values in the vcache */
2835 if (setLocks && !slowPass) {
2838 * This is our dirty little secret to parallel fetches.
2839 * We don't write-lock the vcache while doing the fetch,
2840 * but potentially we'll need to update the vcache after
2841 * the fetch is done.
2843 * Drop the read lock and try to re-obtain the write
2844 * lock. If the vcache still has the same DV, it's
2845 * ok to go ahead and install the new data.
2847 afs_hyper_t currentDV, statusDV;
2849 hset(currentDV, avc->m.DataVersion);
2851 if (setNewCallback && avc->callback != newCallback)
2855 hset64(statusDV, tsmall->OutStatus.dataVersionHigh,
2856 tsmall->OutStatus.DataVersion);
2858 if (setVcacheStatus && avc->m.Length != tsmall->OutStatus.Length)
2860 if (setVcacheStatus && !hsame(currentDV, statusDV))
2864 ReleaseReadLock(&avc->lock);
2866 if (doVcacheUpdate) {
2867 ObtainWriteLock(&avc->lock, 615);
2868 if (!hsame(avc->m.DataVersion, currentDV)) {
2869 /* We lose. Someone will beat us to it. */
2871 ReleaseWriteLock(&avc->lock);
2876 /* With slow pass, we've already done all the updates */
2878 ReleaseWriteLock(&avc->lock);
2881 /* Check if we need to perform any last-minute fixes with a write-lock */
2882 if (!setLocks || doVcacheUpdate) {
2884 avc->callback = newCallback;
2885 if (tsmall && setVcacheStatus)
2886 afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2888 ReleaseWriteLock(&avc->lock);
2892 osi_FreeLargeSpace(tsmall);
2895 } /*afs_GetDCache */
2899 * afs_WriteThroughDSlots
2902 * Sweep through the dcache slots and write out any modified
2903 * in-memory data back on to our caching store.
2909 * The afs_xdcache is write-locked through this whole affair.
2912 afs_WriteThroughDSlots(void)
2914 register struct dcache *tdc;
2915 register afs_int32 i, touchedit = 0;
2917 struct afs_q DirtyQ, *tq;
2919 AFS_STATCNT(afs_WriteThroughDSlots);
2922 * Because of lock ordering, we can't grab dcache locks while
2923 * holding afs_xdcache. So we enter xdcache, get a reference
2924 * for every dcache entry, and exit xdcache.
2926 MObtainWriteLock(&afs_xdcache, 283);
2928 for (i = 0; i < afs_cacheFiles; i++) {
2929 tdc = afs_indexTable[i];
2931 /* Grab tlock in case the existing refcount isn't zero */
2932 if (tdc && !(afs_indexFlags[i] & (IFFree | IFDiscarded))) {
2933 ObtainWriteLock(&tdc->tlock, 623);
2935 ReleaseWriteLock(&tdc->tlock);
2937 QAdd(&DirtyQ, &tdc->dirty);
2940 MReleaseWriteLock(&afs_xdcache);
2943 * Now, for each dcache entry we found, check if it's dirty.
2944 * If so, get write-lock, get afs_xdcache, which protects
2945 * afs_cacheInodep, and flush it. Don't forget to put back
2949 #define DQTODC(q) ((struct dcache *)(((char *) (q)) - sizeof(struct afs_q)))
2951 for (tq = DirtyQ.prev; tq != &DirtyQ; tq = QPrev(tq)) {
2953 if (tdc->dflags & DFEntryMod) {
2956 wrLock = (0 == NBObtainWriteLock(&tdc->lock, 619));
2958 /* Now that we have the write lock, double-check */
2959 if (wrLock && (tdc->dflags & DFEntryMod)) {
2960 tdc->dflags &= ~DFEntryMod;
2961 MObtainWriteLock(&afs_xdcache, 620);
2962 afs_WriteDCache(tdc, 1);
2963 MReleaseWriteLock(&afs_xdcache);
2967 ReleaseWriteLock(&tdc->lock);
2973 MObtainWriteLock(&afs_xdcache, 617);
2974 if (!touchedit && (cacheDiskType != AFS_FCACHE_TYPE_MEM)) {
2975 /* Touch the file to make sure that the mtime on the file is kept
2976 * up-to-date to avoid losing cached files on cold starts because
2977 * their mtime seems old...
2979 struct afs_fheader theader;
2981 theader.magic = AFS_FHMAGIC;
2982 theader.firstCSize = AFS_FIRSTCSIZE;
2983 theader.otherCSize = AFS_OTHERCSIZE;
2984 theader.version = AFS_CI_VERSION;
2985 afs_osi_Write(afs_cacheInodep, 0, &theader, sizeof(theader));
2987 MReleaseWriteLock(&afs_xdcache);
2994 * Return a pointer to an freshly initialized dcache entry using
2995 * a memory-based cache. The tlock will be read-locked.
2998 * aslot : Dcache slot to look at.
2999 * tmpdc : Ptr to dcache entry.
3002 * Must be called with afs_xdcache write-locked.
3006 afs_MemGetDSlot(register afs_int32 aslot, register struct dcache *tmpdc)
3008 register struct dcache *tdc;
3011 AFS_STATCNT(afs_MemGetDSlot);
3012 if (CheckLock(&afs_xdcache) != -1)
3013 osi_Panic("getdslot nolock");
3014 if (aslot < 0 || aslot >= afs_cacheFiles)
3015 osi_Panic("getdslot slot %d (of %d)", aslot, afs_cacheFiles);
3016 tdc = afs_indexTable[aslot];
3018 QRemove(&tdc->lruq); /* move to queue head */
3019 QAdd(&afs_DLRU, &tdc->lruq);
3020 /* We're holding afs_xdcache, but get tlock in case refCount != 0 */
3021 ObtainWriteLock(&tdc->tlock, 624);
3023 ConvertWToRLock(&tdc->tlock);
3026 if (tmpdc == NULL) {
3027 if (!afs_freeDSList)
3028 afs_GetDownDSlot(4);
3029 if (!afs_freeDSList) {
3030 /* none free, making one is better than a panic */
3031 afs_stats_cmperf.dcacheXAllocs++; /* count in case we have a leak */
3032 tdc = (struct dcache *)afs_osi_Alloc(sizeof(struct dcache));
3033 #ifdef KERNEL_HAVE_PIN
3034 pin((char *)tdc, sizeof(struct dcache)); /* XXX */
3037 tdc = afs_freeDSList;
3038 afs_freeDSList = (struct dcache *)tdc->lruq.next;
3041 tdc->dflags = 0; /* up-to-date, not in free q */
3043 QAdd(&afs_DLRU, &tdc->lruq);
3044 if (tdc->lruq.prev == &tdc->lruq)
3045 osi_Panic("lruq 3");
3051 /* initialize entry */
3052 tdc->f.fid.Cell = 0;
3053 tdc->f.fid.Fid.Volume = 0;
3055 hones(tdc->f.versionNo);
3056 tdc->f.inode = aslot;
3057 tdc->dflags |= DFEntryMod;
3060 afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
3063 osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
3064 osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
3065 osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
3068 AFS_RWLOCK_INIT(&tdc->lock, "dcache lock");
3069 AFS_RWLOCK_INIT(&tdc->tlock, "dcache tlock");
3070 AFS_RWLOCK_INIT(&tdc->mflock, "dcache flock");
3071 ObtainReadLock(&tdc->tlock);
3074 afs_indexTable[aslot] = tdc;
3077 } /*afs_MemGetDSlot */
3079 unsigned int last_error = 0, lasterrtime = 0;
3085 * Return a pointer to an freshly initialized dcache entry using
3086 * a UFS-based disk cache. The dcache tlock will be read-locked.
3089 * aslot : Dcache slot to look at.
3090 * tmpdc : Ptr to dcache entry.
3093 * afs_xdcache lock write-locked.
3096 afs_UFSGetDSlot(register afs_int32 aslot, register struct dcache *tmpdc)
3098 register afs_int32 code;
3099 register struct dcache *tdc;
3103 AFS_STATCNT(afs_UFSGetDSlot);
3104 if (CheckLock(&afs_xdcache) != -1)
3105 osi_Panic("getdslot nolock");
3106 if (aslot < 0 || aslot >= afs_cacheFiles)
3107 osi_Panic("getdslot slot %d (of %d)", aslot, afs_cacheFiles);
3108 tdc = afs_indexTable[aslot];
3110 QRemove(&tdc->lruq); /* move to queue head */
3111 QAdd(&afs_DLRU, &tdc->lruq);
3112 /* Grab tlock in case refCount != 0 */
3113 ObtainWriteLock(&tdc->tlock, 625);
3115 ConvertWToRLock(&tdc->tlock);
3118 /* otherwise we should read it in from the cache file */
3120 * If we weren't passed an in-memory region to place the file info,
3121 * we have to allocate one.
3123 if (tmpdc == NULL) {
3124 if (!afs_freeDSList)
3125 afs_GetDownDSlot(4);
3126 if (!afs_freeDSList) {
3127 /* none free, making one is better than a panic */
3128 afs_stats_cmperf.dcacheXAllocs++; /* count in case we have a leak */
3129 tdc = (struct dcache *)afs_osi_Alloc(sizeof(struct dcache));
3130 #ifdef KERNEL_HAVE_PIN
3131 pin((char *)tdc, sizeof(struct dcache)); /* XXX */
3134 tdc = afs_freeDSList;
3135 afs_freeDSList = (struct dcache *)tdc->lruq.next;
3138 tdc->dflags = 0; /* up-to-date, not in free q */
3140 QAdd(&afs_DLRU, &tdc->lruq);
3141 if (tdc->lruq.prev == &tdc->lruq)
3142 osi_Panic("lruq 3");
3149 * Seek to the aslot'th entry and read it in.
3152 afs_osi_Read(afs_cacheInodep,
3153 sizeof(struct fcache) * aslot +
3154 sizeof(struct afs_fheader), (char *)(&tdc->f),
3155 sizeof(struct fcache));
3157 if (code != sizeof(struct fcache))
3159 if (!afs_CellNumValid(tdc->f.fid.Cell))
3163 tdc->f.fid.Cell = 0;
3164 tdc->f.fid.Fid.Volume = 0;
3166 hones(tdc->f.versionNo);
3167 tdc->dflags |= DFEntryMod;
3168 #if defined(KERNEL_HAVE_UERROR)
3169 last_error = getuerror();
3171 lasterrtime = osi_Time();
3172 afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
3173 tdc->f.states &= ~(DRO|DBackup|DRW);
3174 afs_DCMoveBucket(tdc, 0, 0);
3177 if (tdc->f.states & DRO) {
3178 afs_DCMoveBucket(tdc, 0, 2);
3179 } else if (tdc->f.states & DBackup) {
3180 afs_DCMoveBucket(tdc, 0, 1);
3182 afs_DCMoveBucket(tdc, 0, 1);
3188 if (tdc->f.chunk >= 0)
3189 tdc->validPos = AFS_CHUNKTOBASE(tdc->f.chunk) + tdc->f.chunkBytes;
3194 osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
3195 osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
3196 osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
3199 AFS_RWLOCK_INIT(&tdc->lock, "dcache lock");
3200 AFS_RWLOCK_INIT(&tdc->tlock, "dcache tlock");
3201 AFS_RWLOCK_INIT(&tdc->mflock, "dcache flock");
3202 ObtainReadLock(&tdc->tlock);
3205 * If we didn't read into a temporary dcache region, update the
3206 * slot pointer table.
3209 afs_indexTable[aslot] = tdc;
3212 } /*afs_UFSGetDSlot */
3217 * Write a particular dcache entry back to its home in the
3220 * \param adc Pointer to the dcache entry to write.
3221 * \param atime If true, set the modtime on the file to the current time.
3223 * \note Environment:
3224 * Must be called with the afs_xdcache lock at least read-locked,
3225 * and dcache entry at least read-locked.
3226 * The reference count is not changed.
3230 afs_WriteDCache(register struct dcache *adc, int atime)
3232 register afs_int32 code;
3234 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
3236 AFS_STATCNT(afs_WriteDCache);
3237 osi_Assert(WriteLocked(&afs_xdcache));
3239 adc->f.modTime = osi_Time();
3241 * Seek to the right dcache slot and write the in-memory image out to disk.
3243 afs_cellname_write();
3245 afs_osi_Write(afs_cacheInodep,
3246 sizeof(struct fcache) * adc->index +
3247 sizeof(struct afs_fheader), (char *)(&adc->f),
3248 sizeof(struct fcache));
3249 if (code != sizeof(struct fcache))
3257 * Wake up users of a particular file waiting for stores to take
3260 * \param avc Ptr to related vcache entry.
3262 * \note Environment:
3263 * Nothing interesting.
3266 afs_wakeup(register struct vcache *avc)
3269 register struct brequest *tb;
3271 AFS_STATCNT(afs_wakeup);
3272 for (i = 0; i < NBRS; i++, tb++) {
3273 /* if request is valid and for this file, we've found it */
3274 if (tb->refCount > 0 && avc == tb->vc) {
3277 * If CSafeStore is on, then we don't awaken the guy
3278 * waiting for the store until the whole store has finished.
3279 * Otherwise, we do it now. Note that if CSafeStore is on,
3280 * the BStore routine actually wakes up the user, instead
3282 * I think this is redundant now because this sort of thing
3283 * is already being handled by the higher-level code.
3285 if ((avc->states & CSafeStore) == 0) {
3287 tb->flags |= BUVALID;
3288 if (tb->flags & BUWAIT) {
3289 tb->flags &= ~BUWAIT;
3301 * Given a file name and inode, set up that file to be an
3302 * active member in the AFS cache. This also involves checking
3303 * the usability of its data.
3305 * \param afile Name of the cache file to initialize.
3306 * \param ainode Inode of the file.
3308 * \note Environment:
3309 * This function is called only during initialization.
3312 afs_InitCacheFile(char *afile, ino_t ainode)
3314 register afs_int32 code;
3315 #if defined(AFS_LINUX22_ENV)
3316 struct dentry *filevp;
3318 struct vnode *filevp;
3322 struct osi_file *tfile;
3323 struct osi_stat tstat;
3324 register struct dcache *tdc;
3325 #if defined(LINUX_USE_FH)
3326 int max_len = sizeof(struct fid);
3329 AFS_STATCNT(afs_InitCacheFile);
3330 index = afs_stats_cmperf.cacheNumEntries;
3331 if (index >= afs_cacheFiles)
3334 MObtainWriteLock(&afs_xdcache, 282);
3335 tdc = afs_GetDSlot(index, NULL);
3336 ReleaseReadLock(&tdc->tlock);
3337 MReleaseWriteLock(&afs_xdcache);
3339 ObtainWriteLock(&tdc->lock, 621);
3340 MObtainWriteLock(&afs_xdcache, 622);
3342 code = gop_lookupname(afile, AFS_UIOSYS, 0, &filevp);
3344 ReleaseWriteLock(&afs_xdcache);
3345 ReleaseWriteLock(&tdc->lock);
3350 * We have a VN_HOLD on filevp. Get the useful info out and
3351 * return. We make use of the fact that the cache is in the
3352 * UFS file system, and just record the inode number.
3354 #ifdef AFS_LINUX22_ENV
3355 #if defined(LINUX_USE_FH)
3356 tdc->f.fh_type = osi_get_fh(filevp, &tdc->f.fh, &max_len);
3358 tdc->f.inode = VTOI(filevp->d_inode)->i_number;
3362 tdc->f.inode = afs_vnodeToInumber(filevp);
3364 #endif /* AFS_LINUX22_ENV */
3366 tdc->f.inode = ainode;
3369 if ((tdc->f.states & DWriting) || tdc->f.fid.Fid.Volume == 0)
3371 #if defined(LINUX_USE_FH)
3372 tfile = osi_UFSOpen_fh(&tdc->f.fh, tdc->f.fh_type);
3374 tfile = osi_UFSOpen(tdc->f.inode);
3376 code = afs_osi_Stat(tfile, &tstat);
3378 osi_Panic("initcachefile stat");
3381 * If file size doesn't match the cache info file, it's probably bad.
3383 if (tdc->f.chunkBytes != tstat.size)
3385 tdc->f.chunkBytes = 0;
3388 * If file changed within T (120?) seconds of cache info file, it's
3389 * probably bad. In addition, if slot changed within last T seconds,
3390 * the cache info file may be incorrectly identified, and so slot
3393 if (cacheInfoModTime < tstat.mtime + 120)
3395 if (cacheInfoModTime < tdc->f.modTime + 120)
3397 /* In case write through is behind, make sure cache items entry is
3398 * at least as new as the chunk.
3400 if (tdc->f.modTime < tstat.mtime)
3403 tdc->f.fid.Fid.Volume = 0; /* not in the hash table */
3404 if (tstat.size != 0)
3405 osi_UFSTruncate(tfile, 0);
3406 tdc->f.states &= ~(DRO|DBackup|DRW);
3407 afs_DCMoveBucket(tdc, 0, 0);
3408 /* put entry in free cache slot list */
3409 afs_dvnextTbl[tdc->index] = afs_freeDCList;
3410 afs_freeDCList = index;
3412 afs_indexFlags[index] |= IFFree;
3413 afs_indexUnique[index] = 0;
3416 * We must put this entry in the appropriate hash tables.
3417 * Note that i is still set from the above DCHash call
3419 code = DCHash(&tdc->f.fid, tdc->f.chunk);
3420 afs_dcnextTbl[tdc->index] = afs_dchashTbl[code];
3421 afs_dchashTbl[code] = tdc->index;
3422 code = DVHash(&tdc->f.fid);
3423 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[code];
3424 afs_dvhashTbl[code] = tdc->index;
3425 afs_AdjustSize(tdc, tstat.size); /* adjust to new size */
3427 /* has nontrivial amt of data */
3428 afs_indexFlags[index] |= IFEverUsed;
3429 afs_stats_cmperf.cacheFilesReused++;
3431 * Initialize index times to file's mod times; init indexCounter
3434 hset32(afs_indexTimes[index], tstat.atime);
3435 if (hgetlo(afs_indexCounter) < tstat.atime) {
3436 hset32(afs_indexCounter, tstat.atime);
3438 afs_indexUnique[index] = tdc->f.fid.Fid.Unique;
3439 } /*File is not bad */
3441 osi_UFSClose(tfile);
3442 tdc->f.states &= ~DWriting;
3443 tdc->dflags &= ~DFEntryMod;
3444 /* don't set f.modTime; we're just cleaning up */
3445 afs_WriteDCache(tdc, 0);
3446 ReleaseWriteLock(&afs_xdcache);
3447 ReleaseWriteLock(&tdc->lock);
3449 afs_stats_cmperf.cacheNumEntries++;
3454 /*Max # of struct dcache's resident at any time*/
3456 * If 'dchint' is enabled then in-memory dcache min is increased because of
3462 * Initialize dcache related variables.
3472 afs_dcacheInit(int afiles, int ablocks, int aDentries, int achunk, int aflags)
3474 register struct dcache *tdp;
3478 afs_freeDCList = NULLIDX;
3479 afs_discardDCList = NULLIDX;
3480 afs_freeDCCount = 0;
3481 afs_freeDSList = NULL;
3482 hzero(afs_indexCounter);
3484 LOCK_INIT(&afs_xdcache, "afs_xdcache");
3490 if (achunk < 0 || achunk > 30)
3491 achunk = 13; /* Use default */
3492 AFS_SETCHUNKSIZE(achunk);