4716cb777e15de009a3aa505a12d852264c48b2d
[openafs.git] / src / afs / afs_dcache.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 /*
11  * Implements:
12  */
13 #include <afsconfig.h>
14 #include "afs/param.h"
15
16
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"
22
23 #include <opr/ffs.h>
24
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 *);
30 /* For split cache */
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);
36
37 /*
38  * --------------------- Exported definitions ---------------------
39  */
40 /* For split cache */
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;
49
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
70                                  * entries */
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
88                                  * chunk boundary. */
89 #endif /* AFS_VM_RDWR_ENV */
90 #endif /* AFS_64BIT_CLIENT */
91
92 /* The following is used to ensure that new dcache's aren't obtained when
93  * the cache is nearly full.
94  */
95 int afs_WaitForCacheDrain = 0;
96 int afs_TruncateDaemonRunning = 0;
97 int afs_CacheTooFull = 0;
98
99 afs_int32 afs_dcentries;        /*!< In-memory dcache entries */
100
101
102 int dcacheDisabled = 0;
103
104 struct afs_cacheOps afs_UfsCacheOps = {
105 #ifndef HAVE_STRUCT_LABEL_SUPPORT
106     osi_UFSOpen,
107     osi_UFSTruncate,
108     afs_osi_Read,
109     afs_osi_Write,
110     osi_UFSClose,
111     afs_UFSReadUIO,
112     afs_UFSWriteUIO,
113     afs_UFSGetDSlot,
114     afs_UFSGetVolSlot,
115     afs_UFSHandleLink,
116 #else
117     .open       = osi_UFSOpen,
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,
127 #endif
128 };
129
130 struct afs_cacheOps afs_MemCacheOps = {
131 #ifndef HAVE_STRUCT_LABEL_SUPPORT
132     afs_MemCacheOpen,
133     afs_MemCacheTruncate,
134     afs_MemReadBlk,
135     afs_MemWriteBlk,
136     afs_MemCacheClose,
137     afs_MemReadUIO,
138     afs_MemWriteUIO,
139     afs_MemGetDSlot,
140     afs_MemGetVolSlot,
141     afs_MemHandleLink,
142 #else
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,
153 #endif
154 };
155
156 int cacheDiskType;              /*Type of backing disk for cache */
157 struct afs_cacheOps *afs_cacheType;
158
159
160 /*
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.
164  */
165 /*Vnode, Chunk -> Hash table index */
166 int DCHash(struct VenusFid *fid, afs_int32 chunk)
167 {
168     afs_uint32 buf[3];
169
170     buf[0] = fid->Fid.Volume;
171     buf[1] = fid->Fid.Vnode;
172     buf[2] = chunk;
173     return opr_jhash(buf, 3, 0) & (afs_dhashsize - 1);
174 }
175 /*Vnode -> Other hash table index */
176 int DVHash(struct VenusFid *fid)
177 {
178     return opr_jhash_int2(fid->Fid.Volume, fid->Fid.Vnode, 0) &
179         (afs_dhashsize - 1);
180 }
181
182 /*!
183  * Where is this vcache's entry associated dcache located/
184  * \param avc The vcache entry.
185  * \return Bucket index:
186  *      1 : main
187  *      2 : RO
188  */
189 static afs_int32
190 afs_DCGetBucket(struct vcache *avc)
191 {
192     if (!splitdcache)
193         return 1;
194
195     /* This should be replaced with some sort of user configurable function */
196     if (avc->f.states & CRO) {
197         return 2;
198     } else if (avc->f.states & CBackup) {
199         return 1;
200     } else {
201         /* RW */
202     }
203     /* main bucket */
204     return 1;
205 }
206
207 /*!
208  * Readjust a dcache's size.
209  *
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.
213  *
214  */
215 static void
216 afs_DCAdjustSize(struct dcache *adc, afs_int32 oldSize, afs_int32 newSize)
217 {
218     afs_int32 adjustSize = newSize - oldSize;
219
220     if (!splitdcache)
221         return;
222
223     switch (adc->bucket)
224     {
225     case 0:
226         afs_blocksUsed_0 += adjustSize;
227         afs_stats_cmperf.cacheBucket0_Discarded += oldSize;
228         break;
229     case 1:
230         afs_blocksUsed_1 += adjustSize;
231         afs_stats_cmperf.cacheBucket1_Discarded += oldSize;
232         break;
233     case 2:
234         afs_blocksUsed_2 += adjustSize;
235         afs_stats_cmperf.cacheBucket2_Discarded += oldSize;
236         break;
237     }
238
239     return;
240 }
241
242 /*!
243  * Move a dcache from one bucket to another.
244  *
245  * \param adc Operate on this dcache.
246  * \param size Size in bucket (?).
247  * \param newBucket Destination bucket.
248  *
249  */
250 static void
251 afs_DCMoveBucket(struct dcache *adc, afs_int32 size, afs_int32 newBucket)
252 {
253     if (!splitdcache)
254         return;
255
256     /* Substract size from old bucket. */
257     switch (adc->bucket)
258     {
259     case 0:
260         afs_blocksUsed_0 -= size;
261         break;
262     case 1:
263         afs_blocksUsed_1 -= size;
264         break;
265     case 2:
266         afs_blocksUsed_2 -= size;
267         break;
268     }
269
270     /* Set new bucket and increase destination bucket size. */
271     adc->bucket = newBucket;
272
273     switch (adc->bucket)
274     {
275     case 0:
276         afs_blocksUsed_0 += size;
277         break;
278     case 1:
279         afs_blocksUsed_1 += size;
280         break;
281     case 2:
282         afs_blocksUsed_2 += size;
283         break;
284     }
285
286     return;
287 }
288
289 /*!
290  * Init split caches size.
291  */
292 static void
293 afs_DCSizeInit(void)
294 {
295     afs_blocksUsed_0 = afs_blocksUsed_1 = afs_blocksUsed_2 = 0;
296 }
297
298
299 /*!
300  * \param phase
301  * \param bucket
302  */
303 static afs_int32
304 afs_DCWhichBucket(afs_int32 phase, afs_int32 bucket)
305 {
306     if (!splitdcache)
307         return 0;
308
309     afs_pct1 = afs_blocksUsed_1 / (afs_cacheBlocks / 100);
310     afs_pct2 = afs_blocksUsed_2 / (afs_cacheBlocks / 100);
311
312     /* Short cut: if we don't know about it, try to kill it */
313     if (phase < 2 && afs_blocksUsed_0)
314         return 0;
315
316     if (afs_pct1 > afs_tpct1)
317         return 1;
318     if (afs_pct2 > afs_tpct2)
319         return 2;
320     return 0; /* unlikely */
321 }
322
323
324 /*!
325  * Warn about failing to store a file.
326  *
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
332  *
333  * \note Environment: Call this from close call when vnodeops is RCS unlocked.
334  */
335
336 void
337 afs_StoreWarn(afs_int32 acode, afs_int32 avolume,
338               afs_int32 aflags)
339 {
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";
347
348     AFS_STATCNT(afs_StoreWarn);
349     if (acode < 0) {
350         /*
351          * Network problems
352          */
353         if (aflags & 1)
354             afs_warn(problem_fmt, avolume, netproblems);
355         if (aflags & 2)
356             afs_warnuser(problem_fmt, avolume, netproblems);
357     } else if (acode == ENOSPC) {
358         /*
359          * Partition full
360          */
361         if (aflags & 1)
362             afs_warn(problem_fmt, avolume, partfull);
363         if (aflags & 2)
364             afs_warnuser(problem_fmt, avolume, partfull);
365     } else
366 #ifdef  EDQUOT
367         /* EDQUOT doesn't exist on solaris and won't be sent by the server.
368          * Instead ENOSPC will be sent...
369          */
370     if (acode == EDQUOT) {
371         /*
372          * Quota exceeded
373          */
374         if (aflags & 1)
375             afs_warn(problem_fmt, avolume, overquota);
376         if (aflags & 2)
377             afs_warnuser(problem_fmt, avolume, overquota);
378     } else
379 #endif
380     {
381         /*
382          * Unknown error
383          */
384         if (aflags & 1)
385             afs_warn(problem_fmt_w_error, avolume, acode);
386         if (aflags & 2)
387             afs_warnuser(problem_fmt_w_error, avolume, acode);
388     }
389 }                               /*afs_StoreWarn */
390
391 /*!
392  * Try waking up truncation daemon, if it's worth it.
393  */
394 void
395 afs_MaybeWakeupTruncateDaemon(void)
396 {
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);
404     }
405 }
406
407 /*!
408  * /struct CTD_stats
409  *
410  * Keep statistics on run time for afs_CacheTruncateDaemon. This is a
411  * struct so we need only export one symbol for AIX.
412  */
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;
418     int CTD_nSleeps;
419 } CTD_stats;
420
421 u_int afs_min_cache = 0;
422
423 /*!
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.
427  *
428  * \note Environment:
429  *      This routine must be called with the afs_xdcache lock held
430  *      (in write mode).
431  */
432 static void
433 afs_WakeCacheWaitersIfDrained(void)
434 {
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);
440         }
441     }
442 }
443
444 /*!
445  * Keeps the cache clean and free by truncating uneeded files, when used.
446  * \param
447  * \return
448  */
449 void
450 afs_CacheTruncateDaemon(void)
451 {
452     osi_timeval_t CTD_tmpTime;
453     u_int counter;
454     u_int cb_lowat;
455     u_int dc_hiwat =
456         PERCENT((100 - CM_DCACHECOUNTFREEPCT + CM_DCACHEEXTRAPCT), afs_cacheFiles);
457     afs_min_cache =
458         (((10 * AFS_CHUNKSIZE(0)) + afs_fsfragsize) & ~afs_fsfragsize) >> 10;
459
460     osi_GetuTime(&CTD_stats.CTD_afterSleep);
461     afs_TruncateDaemonRunning = 1;
462     while (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++) {
469                 space_needed =
470                     afs_blocksUsed - afs_blocksDiscarded - cb_lowat;
471                 if (space_needed < 0)
472                     space_needed = 0;
473                 slots_needed =
474                     dc_hiwat - afs_freeDCCount - afs_discardDCCount;
475                 if (slots_needed < 0)
476                     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)) {
480                     break;
481                 }
482                 if (afs_termState == AFSOP_STOP_TRUNCDAEMON)
483                     break;
484             }
485             if (!afs_CacheIsTooFull()) {
486                 afs_CacheTooFull = 0;
487                 afs_WakeCacheWaitersIfDrained();
488             }
489         }       /* end of cache cleanup */
490         ReleaseWriteLock(&afs_xdcache);
491
492         /*
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.
498          */
499         if ((afs_termState != AFSOP_STOP_TRUNCDAEMON) && afs_CacheTooFull
500             && (!afs_blocksDiscarded || afs_WaitForCacheDrain)) {
501             afs_osi_Wait(100, 0, 0);    /* 100 milliseconds */
502         }
503
504         /*
505          * This is where we free the discarded cache elements.
506          */
507         while (afs_blocksDiscarded && !afs_WaitForCacheDrain
508                && (afs_termState != AFSOP_STOP_TRUNCDAEMON)) {
509             int code = afs_FreeDiscardedDCache();
510             if (code) {
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.
516                  */
517                 break;
518             }
519         }
520
521         /* See if we need to continue to run. Someone may have
522          * signalled us while we were executing.
523          */
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);
532
533             afs_TruncateDaemonRunning = 0;
534             afs_osi_Sleep((int *)afs_CacheTruncateDaemon);
535             afs_TruncateDaemonRunning = 1;
536
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);
541         }
542         if (afs_termState == AFSOP_STOP_TRUNCDAEMON) {
543             afs_termState = AFSOP_STOP_AFSDB;
544             afs_osi_Wakeup(&afs_termState);
545             break;
546         }
547     }
548 }
549
550
551 /*!
552  * Make adjustment for the new size in the disk cache entry
553  *
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...
558  *
559  * \param adc Ptr to dcache entry.
560  * \param anewsize New size desired.
561  *
562  */
563
564 void
565 afs_AdjustSize(struct dcache *adc, afs_int32 newSize)
566 {
567     afs_int32 oldSize;
568
569     AFS_STATCNT(afs_AdjustSize);
570
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. */
577         static int warned;
578         if (!warned) {
579             warned = 1;
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 "
583                      "why.\n",
584                      adc->index, newSize, afs_OtherCSize);
585         }
586     }
587
588     adc->dflags |= DFEntryMod;
589     oldSize = ((adc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10;    /* round up */
590     adc->f.chunkBytes = newSize;
591     if (!newSize)
592         adc->validPos = 0;
593     newSize = ((newSize + afs_fsfragsize) ^ afs_fsfragsize) >> 10;      /* round up */
594     afs_DCAdjustSize(adc, oldSize, newSize);
595     if ((newSize > oldSize) && !AFS_IS_DISCONNECTED) {
596
597         /* We're growing the file, wakeup the daemon */
598         afs_MaybeWakeupTruncateDaemon();
599     }
600     afs_blocksUsed += (newSize - oldSize);
601     afs_stats_cmperf.cacheBlocksInUse = afs_blocksUsed; /* XXX */
602 }
603
604
605 /*!
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.
608  *
609  * \param anumber Number of entries that should ideally be moved.
610  * \param aneedSpace How much space we need (1K blocks);
611  *
612  * \note Environment:
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))
621  *
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.
625  */
626
627 #define MAXATONCE   16          /* max we can obtain at once */
628 static void
629 afs_GetDownD(int anumber, int *aneedSpace, afs_int32 buckethint)
630 {
631
632     struct dcache *tdc;
633     struct VenusFid *afid;
634     afs_int32 i, j;
635     afs_hyper_t vtime;
636     int skip, phase;
637     struct vcache *tvc;
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 */
644     int discard;
645     int curbucket;
646
647     AFS_STATCNT(afs_GetDownD);
648
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;
656         if (anumber <= 0) {
657             return;             /* enough already free */
658         }
659     }
660
661     /* bounds check parameter */
662     if (anumber > MAXATONCE)
663         anumber = MAXATONCE;    /* all we can do */
664
665     /* rewrite so phases include a better eligiblity for gc test*/
666     /*
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
673      * possible.
674      */
675     if (splitdcache) {
676         phase = 0;
677     } else {
678         phase = 4;
679     }
680
681     for (i = 0; i < afs_cacheFiles; i++)
682         /* turn off all flags */
683         afs_indexFlags[i] &= ~IFFlag;
684
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 */
694                 continue;
695             }
696             tdc = afs_indexTable[i];
697             if (tdc && (curbucket != tdc->bucket) && (phase < 4))
698             {
699                 /* Wrong bucket; can't use it! */
700                 continue;
701             }
702             if (tdc && (tdc->refCount != 0)) {
703                 /* Referenced; can't use it! */
704                 continue;
705             }
706             hset(vtime, afs_indexTimes[i]);
707
708             /* if we've already looked at this one, skip it */
709             if (afs_indexFlags[i] & IFFlag)
710                 continue;
711
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;
719                 }
720                 victimPtr++;
721             } else if (hcmp(vtime, maxVictimTime) < 0) {
722                 /*
723                  * We're older than youngest victim, so we replace at
724                  * least one victim
725                  */
726                 /* find youngest (largest LRU) victim */
727                 j = maxVictimPtr;
728                 if (j == victimPtr)
729                     osi_Panic("getdownd local");
730                 victims[j] = i;
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]);
737                         maxVictimPtr = j;
738                     }
739             }
740         }                       /* big for loop */
741
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.
746          */
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)
751                 victimDCs[i] = tdc;
752             else
753                 victimDCs[i] = 0;
754             if (tdc) {
755                 ReleaseReadLock(&tdc->tlock);
756                 if (!victimDCs[i])
757                     afs_PutDCache(tdc);
758             }
759         }
760         for (i = 0; i < victimPtr; i++) {
761             /* q is first elt in dcache entry */
762             tdc = victimDCs[i];
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.
768              */
769             if (tdc && tdc->refCount == 1) {
770                 unsigned char chunkFlags;
771                 afs_size_t tchunkoffset = 0;
772                 afid = &tdc->f.fid;
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);
779                 skip = 0;
780                 if (tdc->refCount > 1)
781                     skip = 1;
782                 if (tvc) {
783                     tchunkoffset = AFS_CHUNKTOBASE(tdc->f.chunk);
784                     chunkFlags = afs_indexFlags[tdc->index];
785                     if (((phase & 1) == 0) && osi_Active(tvc))
786                         skip = 1;
787                     if (((phase & 1) == 1) && osi_Active(tvc)
788                         && (tvc->f.states & CDCLock)
789                         && (chunkFlags & IFAnyPages))
790                         skip = 1;
791                     if (chunkFlags & IFDataMod)
792                         skip = 1;
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));
797
798 #if     defined(AFS_SUN5_ENV)
799                     /*
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().
804                      */
805                     if (!skip && (chunkFlags & IFAnyPages)) {
806                         int code;
807
808                         ReleaseWriteLock(&afs_xdcache);
809                         ObtainWriteLock(&tvc->vlock, 543);
810                         if (!QEmpty(&tvc->multiPage)) {
811                             if (phase < 3 || osi_VM_MultiPageConflict(tvc, tdc)) {
812                                 skip = 1;
813                                 goto endmultipage;
814                             }
815                         }
816                         /* block locking pages */
817                         tvc->vstates |= VPageCleaning;
818                         /* block getting new pages */
819                         tvc->activeV++;
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))) {
827                             skip = 1;
828                             ReleaseWriteLock(&afs_xdcache);
829                             goto endputpage;
830                         }
831                         ReleaseWriteLock(&afs_xdcache);
832
833                         code = osi_VM_GetDownD(tvc, tdc);
834
835                         ObtainWriteLock(&afs_xdcache, 269);
836                         /* we actually removed all pages, clean and dirty */
837                         if (code == 0) {
838                             afs_indexFlags[tdc->index] &=
839                                 ~(IFDirtyPages | IFAnyPages);
840                         } else
841                             skip = 1;
842                         ReleaseWriteLock(&afs_xdcache);
843                       endputpage:
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);
849
850                         }
851                         if (tvc->vstates & VPageCleaning) {
852                             tvc->vstates &= ~VPageCleaning;
853                             afs_osi_Wakeup((char *)&tvc->vstates);
854                         }
855                       endmultipage:
856                         ReleaseWriteLock(&tvc->vlock);
857                     } else
858 #endif /* AFS_SUN5_ENV */
859                     {
860                         ReleaseWriteLock(&afs_xdcache);
861                     }
862
863                     afs_PutVCache(tvc); /*XXX was AFS_FAST_RELE?*/
864                     ObtainWriteLock(&afs_xdcache, 528);
865                     if (afs_indexFlags[tdc->index] &
866                         (IFDataMod | IFDirtyPages | IFAnyPages))
867                         skip = 1;
868                     if (tdc->refCount > 1)
869                         skip = 1;
870                 }
871 #if     defined(AFS_SUN5_ENV)
872                 else {
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
876                      * clear the flag.
877                      * Hold vcache lock to prevent vnode from being
878                      * created while we're clearing IFDirtyPages.
879                      */
880                     afs_indexFlags[tdc->index] &=
881                         ~(IFDirtyPages | IFAnyPages);
882                 }
883 #endif
884                 if (skip) {
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));
891                 } else {
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) {
905                         discard = 1;
906                         if (aneedSpace)
907                             *aneedSpace -=
908                                 (tdc->f.chunkBytes + afs_fsfragsize) >> 10;
909                     } else {
910                         discard = 0;
911                     }
912                     if (discard) {
913                         afs_DiscardDCache(tdc);
914                     } else {
915                         afs_FreeDCache(tdc);
916                     }
917                     anumber--;
918                     j = 1;      /* we reclaimed at least one victim */
919                 }
920             }
921             if (tdc)
922                 afs_PutDCache(tdc);
923         }                       /* end of for victims loop */
924
925         if (phase < 5) {
926             /* Phase is 0 and no one was found, so try phase 1 (ignore
927              * osi_Active flag) */
928             if (j == 0) {
929                 phase++;
930                 for (i = 0; i < afs_cacheFiles; i++)
931                     /* turn off all flags */
932                     afs_indexFlags[i] &= ~IFFlag;
933             }
934         } else {
935             /* found no one in phases 0-5, we're hosed */
936             if (victimPtr == 0)
937                 break;
938         }
939     }                           /* big while loop */
940
941     return;
942
943 }                               /*afs_GetDownD */
944
945
946 /*!
947  * Remove adc from any hash tables that would allow it to be located
948  * again by afs_FindDCache or afs_GetDCache.
949  *
950  * \param adc Pointer to dcache entry to remove from hash tables.
951  *
952  * \note Locks: Must have the afs_xdcache lock write-locked to call this function.
953  *
954  */
955 int
956 afs_HashOutDCache(struct dcache *adc, int zap)
957 {
958     int i, us;
959
960     AFS_STATCNT(afs_glink);
961     if (zap)
962         /* we know this guy's in the LRUQ.  We'll move dude into DCQ below */
963         DZap(adc);
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];
972         } else {
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];
978                     break;
979                 }
980                 us = afs_dcnextTbl[us];
981             }
982             if (us == NULLIDX)
983                 osi_Panic("dcache hc");
984         }
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];
991         } else {
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];
997                     break;
998                 }
999                 us = afs_dvnextTbl[us];
1000             }
1001             if (us == NULLIDX)
1002                 osi_Panic("dcache hv");
1003         }
1004     }
1005
1006     if (zap) {
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).
1010          */
1011          adc->f.fid.Fid.Volume = 0;     /* invalid */
1012
1013         /* mark entry as modified */
1014         adc->dflags |= DFEntryMod;
1015     }
1016
1017     /* all done */
1018     return 0;
1019 }                               /*afs_HashOutDCache */
1020
1021 /*!
1022  * Flush the given dcache entry, pulling it from hash chains
1023  * and truncating the associated cache file.
1024  *
1025  * \param adc Ptr to dcache entry to flush.
1026  *
1027  * \note Environment:
1028  *      This routine must be called with the afs_xdcache lock held
1029  *      (in write mode).
1030  */
1031 void
1032 afs_FlushDCache(struct dcache *adc)
1033 {
1034     AFS_STATCNT(afs_FlushDCache);
1035     /*
1036      * Bump the number of cache files flushed.
1037      */
1038     afs_stats_cmperf.cacheFlushes++;
1039
1040     /* remove from all hash tables */
1041     afs_HashOutDCache(adc, 1);
1042
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).
1047      */
1048     if (adc->f.chunkBytes != 0) {
1049         afs_DiscardDCache(adc);
1050         afs_MaybeWakeupTruncateDaemon();
1051     } else {
1052         afs_FreeDCache(adc);
1053     }
1054 }                               /*afs_FlushDCache */
1055
1056
1057 /*!
1058  * Put a dcache entry on the free dcache entry list.
1059  *
1060  * \param adc dcache entry to free.
1061  *
1062  * \note Environment: called with afs_xdcache lock write-locked.
1063  */
1064 static void
1065 afs_FreeDCache(struct dcache *adc)
1066 {
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).
1070      */
1071
1072     afs_dvnextTbl[adc->index] = afs_freeDCList;
1073     afs_freeDCList = adc->index;
1074     afs_freeDCCount++;
1075     afs_indexFlags[adc->index] |= IFFree;
1076     adc->dflags |= DFEntryMod;
1077
1078     afs_WakeCacheWaitersIfDrained();
1079 }                               /* afs_FreeDCache */
1080
1081 /*!
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.
1085  *
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...
1090  *
1091  * \param adr Ptr to dcache entry.
1092  *
1093  * \note Environment:
1094  *      Must be called with afs_xdcache write-locked.
1095  */
1096
1097 static void
1098 afs_DiscardDCache(struct dcache *adc)
1099 {
1100     afs_int32 size;
1101
1102     AFS_STATCNT(afs_DiscardDCache);
1103
1104     osi_Assert(adc->refCount == 1);
1105
1106     size = ((adc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10;       /* round up */
1107     afs_blocksDiscarded += size;
1108     afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1109
1110     afs_dvnextTbl[adc->index] = afs_discardDCList;
1111     afs_discardDCList = adc->index;
1112     afs_discardDCCount++;
1113
1114     adc->f.fid.Fid.Volume = 0;
1115     adc->dflags |= DFEntryMod;
1116     afs_indexFlags[adc->index] |= IFDiscarded;
1117
1118     afs_WakeCacheWaitersIfDrained();
1119 }                               /*afs_DiscardDCache */
1120
1121 /**
1122  * Get a dcache entry from the discard or free list
1123  *
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)
1127  *
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.
1131  *
1132  * @pre afs_xdcache is write-locked
1133  */
1134 static int
1135 afs_GetDSlotFromList(struct dcache **adc, afs_int32 *indexp)
1136 {
1137     struct dcache *tdc;
1138
1139     *adc = NULL;
1140
1141     if (*indexp == NULLIDX) {
1142         return ENOSPC;
1143     }
1144
1145     tdc = afs_GetUnusedDSlot(*indexp);
1146     if (tdc == NULL) {
1147         return EIO;
1148     }
1149
1150     osi_Assert(tdc->refCount == 1);
1151     ReleaseReadLock(&tdc->tlock);
1152     *indexp = afs_dvnextTbl[tdc->index];
1153     afs_dvnextTbl[tdc->index] = NULLIDX;
1154
1155     *adc = tdc;
1156     return 0;
1157 }
1158
1159 /*!
1160  * Free the next element on the list of discarded cache elements.
1161  *
1162  * Returns -1 if we encountered an error preventing us from freeing a
1163  * discarded dcache, or 0 on success.
1164  */
1165 static int
1166 afs_FreeDiscardedDCache(void)
1167 {
1168     struct dcache *tdc;
1169     struct osi_file *tfile;
1170     afs_int32 size;
1171
1172     AFS_STATCNT(afs_FreeDiscardedDCache);
1173
1174     ObtainWriteLock(&afs_xdcache, 510);
1175     if (!afs_blocksDiscarded) {
1176         ReleaseWriteLock(&afs_xdcache);
1177         return 0;
1178     }
1179
1180     /*
1181      * Get an entry from the list of discarded cache elements
1182      */
1183     (void)afs_GetDSlotFromList(&tdc, &afs_discardDCList);
1184     if (!tdc) {
1185         ReleaseWriteLock(&afs_xdcache);
1186         return -1;
1187     }
1188
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);
1196
1197     /*
1198      * Truncate the element to reclaim its space
1199      */
1200     tfile = afs_CFileOpen(&tdc->f.inode);
1201     osi_Assert(tfile);
1202     afs_CFileTruncate(tfile, 0);
1203     afs_CFileClose(tfile);
1204     afs_AdjustSize(tdc, 0);
1205     afs_DCMoveBucket(tdc, 0, 0);
1206
1207     /*
1208      * Free the element we just truncated
1209      */
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);
1215     afs_PutDCache(tdc);
1216     ReleaseWriteLock(&afs_xdcache);
1217
1218     return 0;
1219 }
1220
1221 /*!
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%).
1224  *
1225  * \return 0
1226  */
1227 int
1228 afs_MaybeFreeDiscardedDCache(void)
1229 {
1230
1231     AFS_STATCNT(afs_MaybeFreeDiscardedDCache);
1232
1233     while (afs_blocksDiscarded
1234            && (afs_blocksUsed >
1235                PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks))) {
1236         int code = afs_FreeDiscardedDCache();
1237         if (code) {
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
1241              * better. */
1242             osi_Panic("Error freeing discarded dcache");
1243         }
1244     }
1245     return 0;
1246 }
1247
1248 /*!
1249  * Try to free up a certain number of disk slots.
1250  *
1251  * \param anumber Targeted number of disk slots to free up.
1252  *
1253  * \note Environment:
1254  *      Must be called with afs_xdcache write-locked.
1255  *
1256  */
1257 static void
1258 afs_GetDownDSlot(int anumber)
1259 {
1260     struct afs_q *tq, *nq;
1261     struct dcache *tdc;
1262     int ix;
1263     unsigned int cnt;
1264
1265     AFS_STATCNT(afs_GetDownDSlot);
1266     if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
1267         osi_Panic("diskless getdowndslot");
1268
1269     if (CheckLock(&afs_xdcache) != -1)
1270         osi_Panic("getdowndslot nolock");
1271
1272     /* decrement anumber first for all dudes in free list */
1273     for (tdc = afs_freeDSList; tdc; tdc = (struct dcache *)tdc->lruq.next)
1274         anumber--;
1275     if (anumber <= 0)
1276         return;                 /* enough already free */
1277
1278     for (cnt = 0, tq = afs_DLRU.prev; tq != &afs_DLRU && anumber > 0;
1279          tq = nq, cnt++) {
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");
1285
1286             /* write-through if modified */
1287             if (tdc->dflags & DFEntryMod) {
1288 #if defined(AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
1289                 /*
1290                  * ask proxy to do this for us - we don't have the stack space
1291                  */
1292                 while (tdc->dflags & DFEntryMod) {
1293                     int s;
1294                     AFS_GUNLOCK();
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);
1300                     }
1301                     /* wait for daemon to (start, then) finish. */
1302                     SP_WAIT(afs_sgibklock, s, &afs_sgibkwait, PINOD);
1303                     AFS_GLOCK();
1304                 }
1305 #else
1306                 int code;
1307
1308                 code = afs_WriteDCache(tdc, 1);
1309                 if (code) {
1310                     /*
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.
1314                      */
1315                     return;
1316                 }
1317                 tdc->dflags &= ~DFEntryMod;
1318 #endif
1319             }
1320
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;
1328             anumber--;
1329         }
1330     }
1331 }                               /*afs_GetDownDSlot */
1332
1333
1334 /*
1335  * afs_RefDCache
1336  *
1337  * Description:
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.
1342  *
1343  * Parameters:
1344  *      adc : Pointer to the dcache entry to increment.
1345  *
1346  * Environment:
1347  *      Nothing interesting.
1348  */
1349 int
1350 afs_RefDCache(struct dcache *adc)
1351 {
1352     ObtainWriteLock(&adc->tlock, 627);
1353     if (adc->refCount < 0)
1354         osi_Panic("RefDCache: negative refcount");
1355     adc->refCount++;
1356     ReleaseWriteLock(&adc->tlock);
1357     return 0;
1358 }
1359
1360
1361 /*
1362  * afs_PutDCache
1363  *
1364  * Description:
1365  *      Decrement the reference count on a disk cache entry.
1366  *
1367  * Parameters:
1368  *      ad : Ptr to the dcache entry to decrement.
1369  *
1370  * Environment:
1371  *      Nothing interesting.
1372  */
1373 int
1374 afs_PutDCache(struct dcache *adc)
1375 {
1376     AFS_STATCNT(afs_PutDCache);
1377     ObtainWriteLock(&adc->tlock, 276);
1378     if (adc->refCount <= 0)
1379         osi_Panic("putdcache");
1380     --adc->refCount;
1381     ReleaseWriteLock(&adc->tlock);
1382     return 0;
1383 }
1384
1385
1386 /*
1387  * afs_TryToSmush
1388  *
1389  * Description:
1390  *      Try to discard all data associated with this file from the
1391  *      cache.
1392  *
1393  * Parameters:
1394  *      avc : Pointer to the cache info for the file.
1395  *
1396  * Environment:
1397  *      Both pvnLock and lock are write held.
1398  */
1399 void
1400 afs_TryToSmush(struct vcache *avc, afs_ucred_t *acred, int sync)
1401 {
1402     struct dcache *tdc;
1403     int index;
1404     int i;
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 */
1409
1410 #if     defined(AFS_SUN5_ENV)
1411     ObtainWriteLock(&avc->vlock, 573);
1412     avc->activeV++;             /* block new getpages */
1413     ReleaseWriteLock(&avc->vlock);
1414 #endif
1415
1416     /* Flush VM pages */
1417     osi_VM_TryToSmush(avc, acred, sync);
1418
1419     /*
1420      * Get the hash chain containing all dce's for this fid
1421      */
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);
1429             if (!tdc) {
1430                 /* afs_TryToSmush is best-effort; we may not actually discard
1431                  * everything, so failure to discard dcaches due to an i/o
1432                  * error is okay. */
1433                 break;
1434             }
1435             if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
1436                 if (sync) {
1437                     if ((afs_indexFlags[index] & IFDataMod) == 0
1438                         && tdc->refCount == 1) {
1439                         ReleaseReadLock(&tdc->tlock);
1440                         releaseTlock = 0;
1441                         afs_FlushDCache(tdc);
1442                     }
1443                 } else
1444                     afs_indexTable[index] = 0;
1445             }
1446             if (releaseTlock)
1447                 ReleaseReadLock(&tdc->tlock);
1448             afs_PutDCache(tdc);
1449         }
1450     }
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);
1456     }
1457     ReleaseWriteLock(&avc->vlock);
1458 #endif
1459     ReleaseWriteLock(&afs_xdcache);
1460     /*
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
1464      */
1465     afs_allCBs++;
1466 }
1467
1468 /*
1469  * afs_DCacheMissingChunks
1470  *
1471  * Description
1472  *      Given the cached info for a file, return the number of chunks that
1473  *      are not available from the dcache.
1474  *
1475  * Parameters:
1476  *      avc:    Pointer to the (held) vcache entry to look in.
1477  *
1478  * Returns:
1479  *      The number of chunks which are not currently cached.
1480  *
1481  * Environment:
1482  *      The vcache entry is held upon entry.
1483  */
1484
1485 int
1486 afs_DCacheMissingChunks(struct vcache *avc)
1487 {
1488     int i, index;
1489     afs_size_t totalLength = 0;
1490     afs_uint32 totalChunks = 0;
1491     struct dcache *tdc;
1492
1493     totalLength = avc->f.m.Length;
1494     if (avc->f.truncPos < totalLength)
1495         totalLength = avc->f.truncPos;
1496
1497     /* Length is 0, no chunk missing. */
1498     if (totalLength == 0)
1499         return 0;
1500
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.
1504      */
1505     totalLength--;
1506     totalChunks = (AFS_CHUNK(totalLength) + 1);
1507
1508     /* If we're a directory, we only ever have one chunk, regardless of
1509      * the size of the dir.
1510      */
1511     if (avc->f.fid.Fid.Vnode & 1 || vType(avc) == VDIR)
1512         totalChunks = 1;
1513
1514     /*
1515      printf("Should have %d chunks for %u bytes\n",
1516                 totalChunks, (totalLength + 1));
1517     */
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);
1524             if (!tdc) {
1525                 break;
1526             }
1527             if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
1528                 totalChunks--;
1529             }
1530             ReleaseReadLock(&tdc->tlock);
1531             afs_PutDCache(tdc);
1532         }
1533     }
1534     ReleaseWriteLock(&afs_xdcache);
1535
1536     /*printf("Missing %d chunks\n", totalChunks);*/
1537
1538     return (totalChunks);
1539 }
1540
1541 /*
1542  * afs_FindDCache
1543  *
1544  * Description:
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.
1548  *
1549  * Parameters:
1550  *      avc   : Pointer to the (held) vcache entry to look in.
1551  *      abyte : Which byte we want to get to.
1552  *
1553  * Returns:
1554  *      Pointer to the dcache entry covering the file & desired byte,
1555  *      or NULL if not found.
1556  *
1557  * Environment:
1558  *      The vcache entry is held upon entry.
1559  */
1560
1561 struct dcache *
1562 afs_FindDCache(struct vcache *avc, afs_size_t abyte)
1563 {
1564     afs_int32 chunk;
1565     afs_int32 i, index;
1566     struct dcache *tdc = NULL;
1567
1568     AFS_STATCNT(afs_FindDCache);
1569     chunk = AFS_CHUNK(abyte);
1570
1571     /*
1572      * Hash on the [fid, chunk] and get the corresponding dcache index
1573      * after write-locking the dcache.
1574      */
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);
1580             if (!tdc) {
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. */
1584                 index = NULLIDX;
1585                 break;
1586             }
1587             ReleaseReadLock(&tdc->tlock);
1588             if (!FidCmp(&tdc->f.fid, &avc->f.fid) && chunk == tdc->f.chunk) {
1589                 break;          /* leaving refCount high for caller */
1590             }
1591             afs_PutDCache(tdc);
1592         }
1593     }
1594     if (index != NULLIDX) {
1595         hset(afs_indexTimes[tdc->index], afs_indexCounter);
1596         hadd32(afs_indexCounter, 1);
1597         ReleaseWriteLock(&afs_xdcache);
1598         return tdc;
1599     }
1600     ReleaseWriteLock(&afs_xdcache);
1601     return NULL;
1602 }                               /*afs_FindDCache */
1603
1604 /* only call these from afs_AllocDCache() */
1605 static int
1606 afs_AllocFreeDSlot(struct dcache **adc)
1607 {
1608     int code;
1609     struct dcache *tdc;
1610
1611     code = afs_GetDSlotFromList(&tdc, &afs_freeDCList);
1612     if (code) {
1613         return code;
1614     }
1615     afs_indexFlags[tdc->index] &= ~IFFree;
1616     ObtainWriteLock(&tdc->lock, 604);
1617     afs_freeDCCount--;
1618
1619     *adc = tdc;
1620     return 0;
1621 }
1622 static int
1623 afs_AllocDiscardDSlot(struct dcache **adc, afs_int32 lock)
1624 {
1625     int code;
1626     struct dcache *tdc;
1627     afs_uint32 size = 0;
1628     struct osi_file *file;
1629
1630     code = afs_GetDSlotFromList(&tdc, &afs_discardDCList);
1631     if (code) {
1632         return code;
1633     }
1634     afs_indexFlags[tdc->index] &= ~IFDiscarded;
1635     ObtainWriteLock(&tdc->lock, 605);
1636     afs_discardDCCount--;
1637     size =
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;
1644     if ((lock & 2)) {
1645         /* Truncate the chunk so zeroes get filled properly */
1646         file = afs_CFileOpen(&tdc->f.inode);
1647         osi_Assert(file);
1648         afs_CFileTruncate(file, 0);
1649         afs_CFileClose(file);
1650         afs_AdjustSize(tdc, 0);
1651     }
1652
1653     *adc = tdc;
1654     return 0;
1655 }
1656
1657 /*!
1658  * Get a fresh dcache from the free or discarded list.
1659  *
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,
1665  *              this is it's fid.
1666  *
1667  * \note Required locks:
1668  *      - afs_xdcache (W)
1669  *      - avc (R if (lock & 1) set and W otherwise)
1670  * \note It write locks the new dcache. The caller must unlock it.
1671  *
1672  * \return If we're out of dslots, ENOSPC. If we encountered disk errors, EIO.
1673  *         On success, return 0.
1674  */
1675 static int
1676 afs_AllocDCache(struct dcache **adc, struct vcache *avc, afs_int32 chunk,
1677                 afs_int32 lock, struct VenusFid *ashFid)
1678 {
1679     int code;
1680     struct dcache *tdc = NULL;
1681
1682     *adc = NULL;
1683
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
1686      * to ENOSPC. */
1687     if ((lock & 2)) {
1688         code = afs_AllocFreeDSlot(&tdc);
1689         if (code == ENOSPC) {
1690             code = afs_AllocDiscardDSlot(&tdc, lock);
1691         }
1692     } else {
1693         code = afs_AllocDiscardDSlot(&tdc, lock);
1694         if (code == ENOSPC) {
1695             code = afs_AllocFreeDSlot(&tdc);
1696         }
1697     }
1698     if (code) {
1699         return code;
1700     }
1701
1702     /*
1703      * Locks held:
1704      * avc->lock(R) if setLocks
1705      * avc->lock(W) if !setLocks
1706      * tdc->lock(W)
1707      * afs_xdcache(W)
1708      */
1709
1710     /*
1711      * Fill in the newly-allocated dcache record.
1712      */
1713     afs_indexFlags[tdc->index] &= ~(IFDirtyPages | IFAnyPages);
1714     if (ashFid)
1715         /* Use shadow fid if provided. */
1716         tdc->f.fid = *ashFid;
1717     else
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;
1724     else
1725         tdc->f.states = DRW;
1726     afs_DCMoveBucket(tdc, 0, afs_DCGetBucket(avc));
1727     afs_indexUnique[tdc->index] = tdc->f.fid.Fid.Unique;
1728     if (!ashFid)
1729         hones(tdc->f.versionNo);        /* invalid value */
1730     tdc->f.chunk = chunk;
1731     tdc->validPos = AFS_CHUNKTOBASE(chunk);
1732     /* XXX */
1733     if (tdc->lruq.prev == &tdc->lruq)
1734         osi_Panic("lruq 1");
1735
1736     *adc = tdc;
1737     return 0;
1738 }
1739
1740 static int
1741 IsDCacheSizeOK(struct dcache *adc, struct vcache *avc, afs_int32 chunk_bytes,
1742                afs_size_t file_length, afs_uint32 versionNo, int from_net)
1743 {
1744     afs_size_t expected_bytes;
1745     afs_size_t chunk_start = AFS_CHUNKTOBASE(adc->f.chunk);
1746
1747     if (vType(avc) == VDIR) {
1748         /*
1749          * Directory blobs may be constructed locally (see afs_LocalHero), and
1750          * the size of the blob may differ slightly compared to what's on the
1751          * fileserver. So, skip size checks for directories.
1752          */
1753         return 1;
1754     }
1755
1756     if ((avc->f.states & CDirty)) {
1757         /*
1758          * Our vcache may have writes that are local to our cache, but not yet
1759          * written to the fileserver. In such a situation, we may have dcaches
1760          * for that file that are "short". For example:
1761          *
1762          * Say we have a file that is 0 bytes long. A process opens that file,
1763          * and writes some data to offset 5M (keeping the file open). Another
1764          * process comes along and reads data from offset 1M. We'll try to
1765          * fetch data at offset 1M, and the fileserver will respond with 0
1766          * bytes, since our locally-written data hasn't been written to the
1767          * fileserver yet (on the fileserver, the file is still 0-bytes long).
1768          * So our dcache at offset 1M will have 0 bytes.
1769          *
1770          * So if CDirty is set, don't do any size/length checks at all, since
1771          * we have no idea if the avc length is valid.
1772          */
1773         return 1;
1774     }
1775
1776     if (!from_net && (adc->f.states & DRW)) {
1777         /*
1778          * The dcache data we're looking at is from our local cache (not from a
1779          * fileserver), and it's for data in an RW volume. For cached RW data,
1780          * there are some edge cases that can cause the below length checks to
1781          * trigger false positives.
1782          *
1783          * For example: if the local client writes 4 bytes to a new file at
1784          * offset 0, and then 4 bytes at offset 0x400000, the file will be
1785          * 0x400004 bytes long, but the first dcache chunk will only contain 4
1786          * bytes. If such a file is fetched from a fileserver, the first chunk
1787          * will have a full chunk of data (most of it zeroes), but on the
1788          * client that did the write, the sparse data will not appear in the
1789          * dcache.
1790          *
1791          * Such false positives should only be possible with RW data, since
1792          * non-RW data is never generated locally. So to avoid the false
1793          * positives, assume the dcache length is OK for RW data if the dcache
1794          * came from our local cache (and not directly from a fileserver).
1795          */
1796         return 1;
1797     }
1798
1799     if (file_length < chunk_start) {
1800         expected_bytes = 0;
1801
1802     } else {
1803         expected_bytes = file_length - chunk_start;
1804
1805         if (vType(avc) != VDIR && expected_bytes > AFS_CHUNKTOSIZE(adc->f.chunk)) {
1806             /* A non-dir chunk cannot have more bytes than the chunksize. */
1807             expected_bytes = AFS_CHUNKTOSIZE(adc->f.chunk);
1808         }
1809     }
1810
1811     if (chunk_bytes != expected_bytes) {
1812         static const afs_uint32 one_hour = 60 * 60;
1813         static afs_uint32 last_warn;
1814         afs_uint32 now = osi_Time();
1815
1816         if (now < last_warn) {
1817             /* clock went backwards */
1818             last_warn = now;
1819         }
1820
1821         if (now - last_warn > one_hour) {
1822             unsigned int mtime = adc->f.modTime;
1823
1824             last_warn = now;
1825
1826             if (from_net) {
1827                 /*
1828                  * The dcache we're looking at didn't come from the cache, but is
1829                  * being populated from the net. Don't print out its mtime in that
1830                  * case; that would be misleading since that's the mtime from the
1831                  * last time this dcache slot was written to.
1832                  */
1833                 mtime = 0;
1834             }
1835
1836             afs_warn("afs: Detected corrupt dcache for file %d.%u.%u.%u: chunk %d "
1837                      "(offset %lu) has %d bytes, but it should have %lu bytes\n",
1838                      adc->f.fid.Cell,
1839                      adc->f.fid.Fid.Volume,
1840                      adc->f.fid.Fid.Vnode,
1841                      adc->f.fid.Fid.Unique,
1842                      adc->f.chunk,
1843                      (unsigned long)chunk_start,
1844                      chunk_bytes,
1845                      (unsigned long)expected_bytes);
1846             afs_warn("afs: (dcache %p, file length %lu, DV %u, dcache mtime %u, "
1847                      "index %d, dflags 0x%x, mflags 0x%x, states 0x%x, vcache "
1848                      "states 0x%x)\n",
1849                      adc,
1850                      (unsigned long)file_length,
1851                      versionNo,
1852                      mtime,
1853                      adc->index,
1854                      (unsigned)adc->dflags,
1855                      (unsigned)adc->mflags,
1856                      (unsigned)adc->f.states,
1857                      avc->f.states);
1858             afs_warn("afs: Ignoring the dcache for now, but this may indicate "
1859                      "corruption in the AFS cache, or a bug.\n");
1860         }
1861         return 0;
1862     }
1863     return 1;
1864 }
1865
1866 /*!
1867  * Check if a dcache is "fresh". That is, if the dcache's DV matches the DV of
1868  * the vcache for that file, and the dcache looks "sane" (its length makes
1869  * sense, when considering the length of the given avc).
1870  *
1871  * \param adc The dcache to check
1872  * \param avc The vcache for adc
1873  *
1874  * \return 1 if the dcache is "fresh". 0 otherwise.
1875  */
1876 int
1877 afs_IsDCacheFresh(struct dcache *adc, struct vcache *avc)
1878 {
1879     if (!hsame(adc->f.versionNo, avc->f.m.DataVersion)) {
1880         return 0;
1881     }
1882
1883     /*
1884      * If we've reached here, the DV in adc matches the DV of our avc. Check if
1885      * the number of bytes in adc agrees with the avc file length, as a sanity
1886      * check. If they don't match, we'll pretend the DVs don't match, so the
1887      * bad dcache data will not be used, and we'll probably re-fetch the chunk
1888      * data, replacing the bad chunk.
1889      */
1890
1891     if (!IsDCacheSizeOK(adc, avc, adc->f.chunkBytes, avc->f.m.Length,
1892                         hgetlo(adc->f.versionNo), 0)) {
1893         return 0;
1894     }
1895
1896     return 1;
1897 }
1898
1899 /*
1900  * afs_GetDCache
1901  *
1902  * Description:
1903  *      This function is called to obtain a reference to data stored in
1904  *      the disk cache, locating a chunk of data containing the desired
1905  *      byte and returning a reference to the disk cache entry, with its
1906  *      reference count incremented.
1907  *
1908  * Parameters:
1909  * IN:
1910  *      avc     : Ptr to a vcache entry (unlocked)
1911  *      abyte   : Byte position in the file desired
1912  *      areq    : Request structure identifying the requesting user.
1913  *      aflags  : Settings as follows:
1914  *                      1 : Set locks
1915  *                      2 : Return after creating entry.
1916  *                      4 : called from afs_vnop_write.c
1917  *                          *alen contains length of data to be written.
1918  * OUT:
1919  *      aoffset : Set to the offset within the chunk where the resident
1920  *                byte is located.
1921  *      alen    : Set to the number of bytes of data after the desired
1922  *                byte (including the byte itself) which can be read
1923  *                from this chunk.
1924  *
1925  * Environment:
1926  *      The vcache entry pointed to by avc is unlocked upon entry.
1927  */
1928
1929 /*
1930  * Update the vnode-to-dcache hint if we can get the vnode lock
1931  * right away.  Assumes dcache entry is at least read-locked.
1932  */
1933 void
1934 updateV2DC(int lockVc, struct vcache *v, struct dcache *d, int src)
1935 {
1936     if (!lockVc || 0 == NBObtainWriteLock(&v->lock, src)) {
1937         if (afs_IsDCacheFresh(d, v) && v->callback)
1938             v->dchint = d;
1939         if (lockVc)
1940             ReleaseWriteLock(&v->lock);
1941     }
1942 }
1943
1944 /* avc - Write-locked unless aflags & 1 */
1945 struct dcache *
1946 afs_GetDCache(struct vcache *avc, afs_size_t abyte,
1947               struct vrequest *areq, afs_size_t * aoffset,
1948               afs_size_t * alen, int aflags)
1949 {
1950     afs_int32 i, code, shortcut;
1951 #if     defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
1952     afs_int32 adjustsize = 0;
1953 #endif
1954     int setLocks;
1955     afs_int32 index;
1956     afs_int32 us;
1957     afs_int32 chunk;
1958     afs_size_t Position = 0;
1959     afs_int32 size, tlen;       /* size of segment to transfer */
1960     struct afs_FetchOutput *tsmall = 0;
1961     struct dcache *tdc;
1962     struct osi_file *file;
1963     struct afs_conn *tc;
1964     int downDCount = 0;
1965     struct server *newCallback = NULL;
1966     char setNewCallback;
1967     char setVcacheStatus;
1968     char doVcacheUpdate;
1969     char slowPass = 0;
1970     int doAdjustSize = 0;
1971     int doReallyAdjustSize = 0;
1972     int overWriteWholeChunk = 0;
1973     struct rx_connection *rxconn;
1974
1975 #ifndef AFS_NOSTATS
1976     struct afs_stats_AccessInfo *accP;  /*Ptr to access record in stats */
1977     int fromReplica;            /*Are we reading from a replica? */
1978     int numFetchLoops;          /*# times around the fetch/analyze loop */
1979 #endif /* AFS_NOSTATS */
1980
1981     AFS_STATCNT(afs_GetDCache);
1982     if (dcacheDisabled)
1983         return NULL;
1984
1985     setLocks = aflags & 1;
1986
1987     /*
1988      * Determine the chunk number and offset within the chunk corresponding
1989      * to the desired byte.
1990      */
1991     if (avc->f.fid.Fid.Vnode & 1) {     /* if (vType(avc) == VDIR) */
1992         chunk = 0;
1993     } else {
1994         chunk = AFS_CHUNK(abyte);
1995     }
1996
1997     /* come back to here if we waited for the cache to drain. */
1998   RetryGetDCache:
1999
2000     setNewCallback = setVcacheStatus = 0;
2001
2002     if (setLocks) {
2003         if (slowPass)
2004             ObtainWriteLock(&avc->lock, 616);
2005         else
2006             ObtainReadLock(&avc->lock);
2007     }
2008
2009     /*
2010      * Locks held:
2011      * avc->lock(R) if setLocks && !slowPass
2012      * avc->lock(W) if !setLocks || slowPass
2013      */
2014
2015     shortcut = 0;
2016
2017     /* check hints first! (might could use bcmp or some such...) */
2018     if ((tdc = avc->dchint)) {
2019         int dcLocked;
2020
2021         /*
2022          * The locking order between afs_xdcache and dcache lock matters.
2023          * The hint dcache entry could be anywhere, even on the free list.
2024          * Locking afs_xdcache ensures that noone is trying to pull dcache
2025          * entries from the free list, and thereby assuming them to be not
2026          * referenced and not locked.
2027          */
2028         ObtainReadLock(&afs_xdcache);
2029         dcLocked = (0 == NBObtainSharedLock(&tdc->lock, 601));
2030
2031         if (dcLocked && (tdc->index != NULLIDX)
2032             && !FidCmp(&tdc->f.fid, &avc->f.fid) && chunk == tdc->f.chunk
2033             && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
2034             /* got the right one.  It might not be the right version, and it
2035              * might be fetching, but it's the right dcache entry.
2036              */
2037             /* All this code should be integrated better with what follows:
2038              * I can save a good bit more time under a write lock if I do..
2039              */
2040             ObtainWriteLock(&tdc->tlock, 603);
2041             tdc->refCount++;
2042             ReleaseWriteLock(&tdc->tlock);
2043
2044             ReleaseReadLock(&afs_xdcache);
2045             shortcut = 1;
2046
2047             if (afs_IsDCacheFresh(tdc, avc)
2048                 && !(tdc->dflags & DFFetching)) {
2049
2050                 afs_stats_cmperf.dcacheHits++;
2051                 ObtainWriteLock(&afs_xdcache, 559);
2052                 QRemove(&tdc->lruq);
2053                 QAdd(&afs_DLRU, &tdc->lruq);
2054                 ReleaseWriteLock(&afs_xdcache);
2055
2056                 /* Locks held:
2057                  * avc->lock(R) if setLocks && !slowPass
2058                  * avc->lock(W) if !setLocks || slowPass
2059                  * tdc->lock(S)
2060                  */
2061                 goto done;
2062             }
2063         } else {
2064             if (dcLocked)
2065                 ReleaseSharedLock(&tdc->lock);
2066             ReleaseReadLock(&afs_xdcache);
2067         }
2068
2069         if (!shortcut)
2070             tdc = 0;
2071     }
2072
2073     /* Locks held:
2074      * avc->lock(R) if setLocks && !slowPass
2075      * avc->lock(W) if !setLocks || slowPass
2076      * tdc->lock(S) if tdc
2077      */
2078
2079     if (!tdc) {                 /* If the hint wasn't the right dcache entry */
2080         int dslot_error = 0;
2081         /*
2082          * Hash on the [fid, chunk] and get the corresponding dcache index
2083          * after write-locking the dcache.
2084          */
2085       RetryLookup:
2086
2087         /* Locks held:
2088          * avc->lock(R) if setLocks && !slowPass
2089          * avc->lock(W) if !setLocks || slowPass
2090          */
2091
2092         i = DCHash(&avc->f.fid, chunk);
2093         /* check to make sure our space is fine */
2094         afs_MaybeWakeupTruncateDaemon();
2095
2096         ObtainWriteLock(&afs_xdcache, 280);
2097         us = NULLIDX;
2098         for (index = afs_dchashTbl[i]; index != NULLIDX; us = index, index = afs_dcnextTbl[index]) {
2099             if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
2100                 tdc = afs_GetValidDSlot(index);
2101                 if (!tdc) {
2102                     /* we got an i/o error when trying to get the given dslot.
2103                      * it's possible the dslot we're looking for is elsewhere,
2104                      * but most likely the disk cache is currently unusable, so
2105                      * all afs_GetValidDSlot calls will fail, so just bail out. */
2106                     dslot_error = 1;
2107                     index = NULLIDX;
2108                     break;
2109                 }
2110                 ReleaseReadLock(&tdc->tlock);
2111                 /*
2112                  * Locks held:
2113                  * avc->lock(R) if setLocks && !slowPass
2114                  * avc->lock(W) if !setLocks || slowPass
2115                  * afs_xdcache(W)
2116                  */
2117                 if (!FidCmp(&tdc->f.fid, &avc->f.fid) && chunk == tdc->f.chunk) {
2118                     /* Move it up in the beginning of the list */
2119                     if (afs_dchashTbl[i] != index) {
2120                         afs_dcnextTbl[us] = afs_dcnextTbl[index];
2121                         afs_dcnextTbl[index] = afs_dchashTbl[i];
2122                         afs_dchashTbl[i] = index;
2123                     }
2124                     ReleaseWriteLock(&afs_xdcache);
2125                     ObtainSharedLock(&tdc->lock, 606);
2126                     break;      /* leaving refCount high for caller */
2127                 }
2128                 afs_PutDCache(tdc);
2129                 tdc = 0;
2130             }
2131         }
2132
2133         /*
2134          * If we didn't find the entry, we'll create one.
2135          */
2136         if (index == NULLIDX) {
2137             /*
2138              * Locks held:
2139              * avc->lock(R) if setLocks
2140              * avc->lock(W) if !setLocks
2141              * afs_xdcache(W)
2142              */
2143             afs_Trace2(afs_iclSetp, CM_TRACE_GETDCACHE1, ICL_TYPE_POINTER,
2144                        avc, ICL_TYPE_INT32, chunk);
2145
2146             if (dslot_error) {
2147                 /* We couldn't find the dcache we want, but we hit some i/o
2148                  * errors when trying to find it, so we're not sure if the
2149                  * dcache we want is in the cache or not. Error out, so we
2150                  * don't try to possibly create 2 separate dcaches for the
2151                  * same exact data. */
2152                 ReleaseWriteLock(&afs_xdcache);
2153                 goto done;
2154             }
2155
2156             if (afs_discardDCList == NULLIDX && afs_freeDCList == NULLIDX) {
2157                 if (!setLocks)
2158                     avc->f.states |= CDCLock;
2159                 /* just need slots */
2160                 afs_GetDownD(5, (int *)0, afs_DCGetBucket(avc));
2161                 if (!setLocks)
2162                     avc->f.states &= ~CDCLock;
2163             }
2164             code = afs_AllocDCache(&tdc, avc, chunk, aflags, NULL);
2165             if (code) {
2166                 ReleaseWriteLock(&afs_xdcache);
2167                 if (code == ENOSPC) {
2168                     /* It looks like afs_AllocDCache failed because we don't
2169                      * have any free dslots to use. Maybe if we wait a little
2170                      * while, we'll be able to free up some slots, so try for 5
2171                      * minutes, then bail out. */
2172                     if (++downDCount > 300) {
2173                         afs_warn("afs: Unable to get free cache space for file "
2174                                  "%u:%u.%u.%u for 5 minutes; failing with an i/o error\n",
2175                                  avc->f.fid.Cell,
2176                                  avc->f.fid.Fid.Volume,
2177                                  avc->f.fid.Fid.Vnode,
2178                                  avc->f.fid.Fid.Unique);
2179                         goto done;
2180                     }
2181                     afs_osi_Wait(1000, 0, 0);
2182                     goto RetryLookup;
2183                 }
2184
2185                 /* afs_AllocDCache failed, but not because we're out of free
2186                  * dslots. Something must be screwy with the cache, so bail out
2187                  * immediately without waiting. */
2188                 afs_warn("afs: Error while alloc'ing cache slot for file "
2189                          "%u:%u.%u.%u; failing with an i/o error\n",
2190                          avc->f.fid.Cell,
2191                          avc->f.fid.Fid.Volume,
2192                          avc->f.fid.Fid.Vnode,
2193                          avc->f.fid.Fid.Unique);
2194                 goto done;
2195             }
2196
2197             /*
2198              * Locks held:
2199              * avc->lock(R) if setLocks
2200              * avc->lock(W) if !setLocks
2201              * tdc->lock(W)
2202              * afs_xdcache(W)
2203              */
2204
2205             /*
2206              * Now add to the two hash chains - note that i is still set
2207              * from the above DCHash call.
2208              */
2209             afs_dcnextTbl[tdc->index] = afs_dchashTbl[i];
2210             afs_dchashTbl[i] = tdc->index;
2211             i = DVHash(&avc->f.fid);
2212             afs_dvnextTbl[tdc->index] = afs_dvhashTbl[i];
2213             afs_dvhashTbl[i] = tdc->index;
2214             tdc->dflags = DFEntryMod;
2215             tdc->mflags = 0;
2216             afs_MaybeWakeupTruncateDaemon();
2217             ReleaseWriteLock(&afs_xdcache);
2218             ConvertWToSLock(&tdc->lock);
2219         }
2220     }
2221
2222
2223     /* vcache->dcache hint failed */
2224     /*
2225      * Locks held:
2226      * avc->lock(R) if setLocks && !slowPass
2227      * avc->lock(W) if !setLocks || slowPass
2228      * tdc->lock(S)
2229      */
2230     afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE2, ICL_TYPE_POINTER, avc,
2231                ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
2232                hgetlo(tdc->f.versionNo), ICL_TYPE_INT32,
2233                hgetlo(avc->f.m.DataVersion));
2234     /*
2235      * Here we have the entry in tdc, with its refCount incremented.
2236      * Note: we don't use the S-lock on avc; it costs concurrency when
2237      * storing a file back to the server.
2238      */
2239
2240     /*
2241      * Not a newly created file so we need to check the file's length and
2242      * compare data versions since someone could have changed the data or we're
2243      * reading a file written elsewhere. We only want to bypass doing no-op
2244      * read rpcs on newly created files (dv of 0) since only then we guarantee
2245      * that this chunk's data hasn't been filled by another client.
2246      */
2247     size = AFS_CHUNKSIZE(abyte);
2248     if (aflags & 4)             /* called from write */
2249         tlen = *alen;
2250     else                        /* called from read */
2251         tlen = tdc->validPos - abyte;
2252     Position = AFS_CHUNKTOBASE(chunk);
2253     afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE3, ICL_TYPE_INT32, tlen,
2254                ICL_TYPE_INT32, aflags, ICL_TYPE_OFFSET,
2255                ICL_HANDLE_OFFSET(abyte), ICL_TYPE_OFFSET,
2256                ICL_HANDLE_OFFSET(Position));
2257     if ((aflags & 4) && (hiszero(avc->f.m.DataVersion)))
2258         doAdjustSize = 1;
2259     if ((AFS_CHUNKTOBASE(chunk) >= avc->f.m.Length) ||
2260          ((aflags & 4) && (abyte == Position) && (tlen >= size)))
2261         overWriteWholeChunk = 1;
2262     if (doAdjustSize || overWriteWholeChunk) {
2263 #if     defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
2264 #ifdef  AFS_SGI_ENV
2265 #ifdef AFS_SGI64_ENV
2266         if (doAdjustSize)
2267             adjustsize = NBPP;
2268 #else /* AFS_SGI64_ENV */
2269         if (doAdjustSize)
2270             adjustsize = 8192;
2271 #endif /* AFS_SGI64_ENV */
2272 #else /* AFS_SGI_ENV */
2273         if (doAdjustSize)
2274             adjustsize = 4096;
2275 #endif /* AFS_SGI_ENV */
2276         if (AFS_CHUNKTOBASE(chunk) + adjustsize >= avc->f.m.Length &&
2277 #else /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
2278 #if     defined(AFS_SUN5_ENV)
2279         if ((doAdjustSize || (AFS_CHUNKTOBASE(chunk) >= avc->f.m.Length)) &&
2280 #else
2281         if (AFS_CHUNKTOBASE(chunk) >= avc->f.m.Length &&
2282 #endif
2283 #endif /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
2284             !afs_IsDCacheFresh(tdc, avc))
2285             doReallyAdjustSize = 1;
2286
2287         if (doReallyAdjustSize || overWriteWholeChunk) {
2288             /* no data in file to read at this position */
2289             UpgradeSToWLock(&tdc->lock, 607);
2290             file = afs_CFileOpen(&tdc->f.inode);
2291             osi_Assert(file);
2292             afs_CFileTruncate(file, 0);
2293             afs_CFileClose(file);
2294             afs_AdjustSize(tdc, 0);
2295             hset(tdc->f.versionNo, avc->f.m.DataVersion);
2296             tdc->dflags |= DFEntryMod;
2297
2298             ConvertWToSLock(&tdc->lock);
2299         }
2300     }
2301
2302     /*
2303      * We must read in the whole chunk if the version number doesn't
2304      * match.
2305      */
2306     if (aflags & 2) {
2307         /* don't need data, just a unique dcache entry */
2308         ObtainWriteLock(&afs_xdcache, 608);
2309         hset(afs_indexTimes[tdc->index], afs_indexCounter);
2310         hadd32(afs_indexCounter, 1);
2311         ReleaseWriteLock(&afs_xdcache);
2312
2313         updateV2DC(setLocks, avc, tdc, 553);
2314         if (vType(avc) == VDIR)
2315             *aoffset = abyte;
2316         else
2317             *aoffset = AFS_CHUNKOFFSET(abyte);
2318         if (tdc->validPos < abyte)
2319             *alen = (afs_size_t) 0;
2320         else
2321             *alen = tdc->validPos - abyte;
2322         ReleaseSharedLock(&tdc->lock);
2323         if (setLocks) {
2324             if (slowPass)
2325                 ReleaseWriteLock(&avc->lock);
2326             else
2327                 ReleaseReadLock(&avc->lock);
2328         }
2329         return tdc;             /* check if we're done */
2330     }
2331
2332     /*
2333      * Locks held:
2334      * avc->lock(R) if setLocks && !slowPass
2335      * avc->lock(W) if !setLocks || slowPass
2336      * tdc->lock(S)
2337      */
2338     osi_Assert((setLocks && !slowPass) || WriteLocked(&avc->lock));
2339
2340     setNewCallback = setVcacheStatus = 0;
2341
2342     /*
2343      * Locks held:
2344      * avc->lock(R) if setLocks && !slowPass
2345      * avc->lock(W) if !setLocks || slowPass
2346      * tdc->lock(S)
2347      */
2348     if (!afs_IsDCacheFresh(tdc, avc) && !overWriteWholeChunk) {
2349         /*
2350          * Version number mismatch.
2351          */
2352         /*
2353          * If we are disconnected, then we can't do much of anything
2354          * because the data doesn't match the file.
2355          */
2356         if (AFS_IS_DISCONNECTED) {
2357             ReleaseSharedLock(&tdc->lock);
2358             if (setLocks) {
2359                 if (slowPass)
2360                     ReleaseWriteLock(&avc->lock);
2361                 else
2362                     ReleaseReadLock(&avc->lock);
2363             }
2364             /* Flush the Dcache */
2365             afs_PutDCache(tdc);
2366
2367             return NULL;
2368         }
2369         UpgradeSToWLock(&tdc->lock, 609);
2370
2371         /*
2372          * If data ever existed for this vnode, and this is a text object,
2373          * do some clearing.  Now, you'd think you need only do the flush
2374          * when VTEXT is on, but VTEXT is turned off when the text object
2375          * is freed, while pages are left lying around in memory marked
2376          * with this vnode.  If we would reactivate (create a new text
2377          * object from) this vnode, we could easily stumble upon some of
2378          * these old pages in pagein.  So, we always flush these guys.
2379          * Sun has a wonderful lack of useful invariants in this system.
2380          *
2381          * avc->flushDV is the data version # of the file at the last text
2382          * flush.  Clearly, at least, we don't have to flush the file more
2383          * often than it changes
2384          */
2385         if (hcmp(avc->flushDV, avc->f.m.DataVersion) < 0) {
2386             /*
2387              * By here, the cache entry is always write-locked.  We can
2388              * deadlock if we call osi_Flush with the cache entry locked...
2389              * Unlock the dcache too.
2390              */
2391             ReleaseWriteLock(&tdc->lock);
2392             if (setLocks && !slowPass)
2393                 ReleaseReadLock(&avc->lock);
2394             else
2395                 ReleaseWriteLock(&avc->lock);
2396
2397             osi_FlushText(avc);
2398             /*
2399              * Call osi_FlushPages in open, read/write, and map, since it
2400              * is too hard here to figure out if we should lock the
2401              * pvnLock.
2402              */
2403             if (setLocks && !slowPass)
2404                 ObtainReadLock(&avc->lock);
2405             else
2406                 ObtainWriteLock(&avc->lock, 66);
2407             ObtainWriteLock(&tdc->lock, 610);
2408         }
2409
2410         /*
2411          * Locks held:
2412          * avc->lock(R) if setLocks && !slowPass
2413          * avc->lock(W) if !setLocks || slowPass
2414          * tdc->lock(W)
2415          */
2416
2417         /* Watch for standard race condition around osi_FlushText */
2418         if (afs_IsDCacheFresh(tdc, avc)) {
2419             updateV2DC(setLocks, avc, tdc, 569);        /* set hint */
2420             afs_stats_cmperf.dcacheHits++;
2421             ConvertWToSLock(&tdc->lock);
2422             goto done;
2423         }
2424
2425         /* Sleep here when cache needs to be drained. */
2426         if (setLocks && !slowPass
2427             && (afs_blocksUsed >
2428                 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks))) {
2429             /* Make sure truncate daemon is running */
2430             afs_MaybeWakeupTruncateDaemon();
2431             ObtainWriteLock(&tdc->tlock, 614);
2432             tdc->refCount--;    /* we'll re-obtain the dcache when we re-try. */
2433             ReleaseWriteLock(&tdc->tlock);
2434             ReleaseWriteLock(&tdc->lock);
2435             ReleaseReadLock(&avc->lock);
2436             while ((afs_blocksUsed - afs_blocksDiscarded) >
2437                    PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
2438                 afs_WaitForCacheDrain = 1;
2439                 afs_osi_Sleep(&afs_WaitForCacheDrain);
2440             }
2441             afs_MaybeFreeDiscardedDCache();
2442             /* need to check if someone else got the chunk first. */
2443             goto RetryGetDCache;
2444         }
2445
2446         Position = AFS_CHUNKBASE(abyte);
2447         if (vType(avc) == VDIR) {
2448             size = avc->f.m.Length;
2449             if (size > tdc->f.chunkBytes) {
2450                 /* pre-reserve space for file */
2451                 afs_AdjustSize(tdc, size);
2452             }
2453             size = 999999999;   /* max size for transfer */
2454         } else {
2455             afs_size_t maxGoodLength;
2456
2457             /* estimate how much data we're expecting back from the server,
2458              * and reserve space in the dcache entry for it */
2459
2460             maxGoodLength = avc->f.m.Length;
2461             if (avc->f.truncPos < maxGoodLength)
2462                 maxGoodLength = avc->f.truncPos;
2463
2464             size = AFS_CHUNKSIZE(abyte);        /* expected max size */
2465             if (Position > maxGoodLength) { /* If we're beyond EOF */
2466                 size = 0;
2467             } else if (Position + size > maxGoodLength) {
2468                 size = maxGoodLength - Position;
2469             }
2470             osi_Assert(size >= 0);
2471
2472             if (size > tdc->f.chunkBytes) {
2473                 /* pre-reserve estimated space for file */
2474                 afs_AdjustSize(tdc, size);      /* changes chunkBytes */
2475             }
2476
2477             if (size) {
2478                 /* For the actual fetch, do not limit the request to the
2479                  * length of the file. If this results in a read past EOF on
2480                  * the server, the server will just reply with less data than
2481                  * requested. If we limit ourselves to only requesting data up
2482                  * to the avc file length, we open ourselves up to races if the
2483                  * file is extended on the server at about the same time.
2484                  *
2485                  * However, we must restrict ourselves to the avc->f.truncPos
2486                  * length, since this represents an outstanding local
2487                  * truncation of the file that will be committed to the
2488                  * fileserver when we actually write the fileserver contents.
2489                  * If we do not restrict the fetch length based on
2490                  * avc->f.truncPos, a different truncate operation extending
2491                  * the file length could cause the old data after
2492                  * avc->f.truncPos to reappear, instead of extending the file
2493                  * with NUL bytes. */
2494                 size = AFS_CHUNKSIZE(abyte);
2495                 if (Position > avc->f.truncPos) {
2496                     size = 0;
2497                 } else if (Position + size > avc->f.truncPos) {
2498                     size = avc->f.truncPos - Position;
2499                 }
2500                 osi_Assert(size >= 0);
2501             }
2502         }
2503         if (afs_mariner && !tdc->f.chunk)
2504             afs_MarinerLog("fetch$Fetching", avc);      /* , Position, size, afs_indexCounter ); */
2505         /*
2506          * Right now, we only have one tool, and it's a hammer.  So, we
2507          * fetch the whole file.
2508          */
2509         DZap(tdc);      /* pages in cache may be old */
2510         file = afs_CFileOpen(&tdc->f.inode);
2511         if (!file) {
2512             /* We can't access the file in the disk cache backing this dcache;
2513              * bail out. */
2514             ReleaseWriteLock(&tdc->lock);
2515             afs_PutDCache(tdc);
2516             tdc = NULL;
2517             goto done;
2518         }
2519         afs_RemoveVCB(&avc->f.fid);
2520         tdc->f.states |= DWriting;
2521         tdc->dflags |= DFFetching;
2522         tdc->validPos = Position;       /*  which is AFS_CHUNKBASE(abyte) */
2523         if (tdc->mflags & DFFetchReq) {
2524             tdc->mflags &= ~DFFetchReq;
2525             if (afs_osi_Wakeup(&tdc->validPos) == 0)
2526                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
2527                            __FILE__, ICL_TYPE_INT32, __LINE__,
2528                            ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
2529                            tdc->dflags);
2530         }
2531         tsmall = osi_AllocLargeSpace(sizeof(struct afs_FetchOutput));
2532         setVcacheStatus = 0;
2533 #ifndef AFS_NOSTATS
2534         /*
2535          * Remember if we are doing the reading from a replicated volume,
2536          * and how many times we've zipped around the fetch/analyze loop.
2537          */
2538         fromReplica = (avc->f.states & CRO) ? 1 : 0;
2539         numFetchLoops = 0;
2540         accP = &(afs_stats_cmfullperf.accessinf);
2541         if (fromReplica)
2542             (accP->replicatedRefs)++;
2543         else
2544             (accP->unreplicatedRefs)++;
2545 #endif /* AFS_NOSTATS */
2546         /* this is a cache miss */
2547         afs_Trace4(afs_iclSetp, CM_TRACE_FETCHPROC, ICL_TYPE_POINTER, avc,
2548                    ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
2549                    ICL_HANDLE_OFFSET(Position), ICL_TYPE_INT32, size);
2550
2551         if (size)
2552             afs_stats_cmperf.dcacheMisses++;
2553         code = 0;
2554         /*
2555          * Dynamic root support:  fetch data from local memory.
2556          */
2557         if (afs_IsDynroot(avc)) {
2558             char *dynrootDir;
2559             int dynrootLen;
2560
2561             afs_GetDynroot(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2562
2563             dynrootDir += Position;
2564             dynrootLen -= Position;
2565             if (size > dynrootLen)
2566                 size = dynrootLen;
2567             if (size < 0)
2568                 size = 0;
2569             code = afs_CFileWrite(file, 0, dynrootDir, size);
2570             afs_PutDynroot();
2571
2572             if (code == size)
2573                 code = 0;
2574             else
2575                 code = -1;
2576
2577             tdc->validPos = Position + size;
2578             afs_CFileTruncate(file, size);      /* prune it */
2579         } else if (afs_IsDynrootMount(avc)) {
2580             char *dynrootDir;
2581             int dynrootLen;
2582
2583             afs_GetDynrootMount(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2584
2585             dynrootDir += Position;
2586             dynrootLen -= Position;
2587             if (size > dynrootLen)
2588                 size = dynrootLen;
2589             if (size < 0)
2590                 size = 0;
2591             code = afs_CFileWrite(file, 0, dynrootDir, size);
2592             afs_PutDynroot();
2593
2594             if (code == size)
2595                 code = 0;
2596             else
2597                 code = -1;
2598
2599             tdc->validPos = Position + size;
2600             afs_CFileTruncate(file, size);      /* prune it */
2601         } else
2602             /*
2603              * Not a dynamic vnode:  do the real fetch.
2604              */
2605             do {
2606                 /*
2607                  * Locks held:
2608                  * avc->lock(R) if setLocks && !slowPass
2609                  * avc->lock(W) if !setLocks || slowPass
2610                  * tdc->lock(W)
2611                  */
2612
2613                 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK, &rxconn);
2614                 if (tc) {
2615 #ifndef AFS_NOSTATS
2616                     numFetchLoops++;
2617                     if (fromReplica)
2618                         (accP->numReplicasAccessed)++;
2619
2620 #endif /* AFS_NOSTATS */
2621                     if (!setLocks || slowPass) {
2622                         avc->callback = tc->parent->srvr->server;
2623                     } else {
2624                         newCallback = tc->parent->srvr->server;
2625                         setNewCallback = 1;
2626                     }
2627                     i = osi_Time();
2628                     code = afs_CacheFetchProc(tc, rxconn, file, Position, tdc,
2629                                                avc, size, tsmall);
2630                 } else
2631                    code = -1;
2632
2633                 if (code == 0) {
2634                     /* callback could have been broken (or expired) in a race here,
2635                      * but we return the data anyway.  It's as good as we knew about
2636                      * when we started. */
2637                     /*
2638                      * validPos is updated by CacheFetchProc, and can only be
2639                      * modifed under a dcache write lock, which we've blocked out
2640                      */
2641                     afs_size_t length;
2642
2643                     size = tdc->validPos - Position;    /* actual segment size */
2644                     if (size < 0)
2645                         size = 0;
2646                     afs_CFileTruncate(file, size);      /* prune it */
2647
2648                     /* Check that the amount of data that we fetched for the
2649                      * dcache makes sense. */
2650                     FillInt64(length, tsmall->OutStatus.Length_hi, tsmall->OutStatus.Length);
2651                     if (!IsDCacheSizeOK(tdc, avc, size,
2652                                         length,
2653                                         tsmall->OutStatus.DataVersion, 1)) {
2654                         code = EIO;
2655                     }
2656                 }
2657                 if (code) {
2658                     if (!setLocks || slowPass) {
2659                         afs_StaleVCacheFlags(avc, AFS_STALEVC_CLEARCB, CUnique);
2660                     } else {
2661                         /* Something lost.  Forget about performance, and go
2662                          * back with a vcache write lock.
2663                          */
2664                         afs_CFileTruncate(file, 0);
2665                         afs_AdjustSize(tdc, 0);
2666                         afs_CFileClose(file);
2667                         osi_FreeLargeSpace(tsmall);
2668                         tsmall = 0;
2669                         ReleaseWriteLock(&tdc->lock);
2670                         afs_PutDCache(tdc);
2671                         tdc = 0;
2672
2673                         /*
2674                          * Call afs_Analyze to manage the connection references
2675                          * and handle the error code (possibly mark servers
2676                          * down, etc). We are going to retry getting the
2677                          * dcache regardless, so we just ignore the retry hint
2678                          * returned by afs_Analyze on this call.
2679                          */
2680                         (void)afs_Analyze(tc, rxconn, code, &avc->f.fid, areq,
2681                                           AFS_STATS_FS_RPCIDX_FETCHDATA, SHARED_LOCK, NULL);
2682
2683                         ReleaseReadLock(&avc->lock);
2684
2685                         slowPass = 1;
2686                         goto RetryGetDCache;
2687                     }
2688                 }
2689
2690             } while (afs_Analyze
2691                      (tc, rxconn, code, &avc->f.fid, areq,
2692                       AFS_STATS_FS_RPCIDX_FETCHDATA, SHARED_LOCK, NULL));
2693
2694         /*
2695          * Locks held:
2696          * avc->lock(R) if setLocks && !slowPass
2697          * avc->lock(W) if !setLocks || slowPass
2698          * tdc->lock(W)
2699          */
2700
2701 #ifndef AFS_NOSTATS
2702         /*
2703          * In the case of replicated access, jot down info on the number of
2704          * attempts it took before we got through or gave up.
2705          */
2706         if (fromReplica) {
2707             if (numFetchLoops <= 1)
2708                 (accP->refFirstReplicaOK)++;
2709             if (numFetchLoops > accP->maxReplicasPerRef)
2710                 accP->maxReplicasPerRef = numFetchLoops;
2711         }
2712 #endif /* AFS_NOSTATS */
2713
2714         tdc->dflags &= ~DFFetching;
2715         if (afs_osi_Wakeup(&tdc->validPos) == 0)
2716             afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
2717                        __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
2718                        tdc, ICL_TYPE_INT32, tdc->dflags);
2719         if (avc->execsOrWriters == 0)
2720             tdc->f.states &= ~DWriting;
2721
2722         /* now, if code != 0, we have an error and should punt.
2723          * note that we have the vcache write lock, either because
2724          * !setLocks or slowPass.
2725          */
2726         if (code) {
2727             afs_CFileTruncate(file, 0);
2728             afs_AdjustSize(tdc, 0);
2729             afs_CFileClose(file);
2730             ZapDCE(tdc);        /* sets DFEntryMod */
2731             if (vType(avc) == VDIR) {
2732                 DZap(tdc);
2733             }
2734             tdc->f.states &= ~(DRO|DBackup|DRW);
2735             afs_DCMoveBucket(tdc, 0, 0);
2736             ReleaseWriteLock(&tdc->lock);
2737             afs_PutDCache(tdc);
2738             if (!afs_IsDynroot(avc)) {
2739                 afs_StaleVCacheFlags(avc, 0, CUnique);
2740                 /*
2741                  * Locks held:
2742                  * avc->lock(W); assert(!setLocks || slowPass)
2743                  */
2744                 osi_Assert(!setLocks || slowPass);
2745             }
2746             tdc = NULL;
2747             goto done;
2748         }
2749
2750         /* otherwise we copy in the just-fetched info */
2751         afs_CFileClose(file);
2752         afs_AdjustSize(tdc, size);      /* new size */
2753         /*
2754          * Copy appropriate fields into vcache.  Status is
2755          * copied later where we selectively acquire the
2756          * vcache write lock.
2757          */
2758         if (slowPass)
2759             afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2760         else
2761             setVcacheStatus = 1;
2762         hset64(tdc->f.versionNo, tsmall->OutStatus.dataVersionHigh,
2763                tsmall->OutStatus.DataVersion);
2764         tdc->dflags |= DFEntryMod;
2765         afs_indexFlags[tdc->index] |= IFEverUsed;
2766         ConvertWToSLock(&tdc->lock);
2767     } /*Data version numbers don't match */
2768     else {
2769         /*
2770          * Data version numbers match.
2771          */
2772         afs_stats_cmperf.dcacheHits++;
2773     }                           /*Data version numbers match */
2774
2775     updateV2DC(setLocks, avc, tdc, 335);        /* set hint */
2776   done:
2777     /*
2778      * Locks held:
2779      * avc->lock(R) if setLocks && !slowPass
2780      * avc->lock(W) if !setLocks || slowPass
2781      * tdc->lock(S) if tdc
2782      */
2783
2784     /*
2785      * See if this was a reference to a file in the local cell.
2786      */
2787     if (afs_IsPrimaryCellNum(avc->f.fid.Cell))
2788         afs_stats_cmperf.dlocalAccesses++;
2789     else
2790         afs_stats_cmperf.dremoteAccesses++;
2791
2792     /* Fix up LRU info */
2793
2794     if (tdc) {
2795         ObtainWriteLock(&afs_xdcache, 602);
2796         hset(afs_indexTimes[tdc->index], afs_indexCounter);
2797         hadd32(afs_indexCounter, 1);
2798         ReleaseWriteLock(&afs_xdcache);
2799
2800         /* return the data */
2801         if (vType(avc) == VDIR)
2802             *aoffset = abyte;
2803         else
2804             *aoffset = AFS_CHUNKOFFSET(abyte);
2805         *alen = (tdc->f.chunkBytes - *aoffset);
2806         ReleaseSharedLock(&tdc->lock);
2807     }
2808
2809     /*
2810      * Locks held:
2811      * avc->lock(R) if setLocks && !slowPass
2812      * avc->lock(W) if !setLocks || slowPass
2813      */
2814
2815     /* Fix up the callback and status values in the vcache */
2816     doVcacheUpdate = 0;
2817     if (setLocks && !slowPass) {
2818         /* DCLOCKXXX
2819          *
2820          * This is our dirty little secret to parallel fetches.
2821          * We don't write-lock the vcache while doing the fetch,
2822          * but potentially we'll need to update the vcache after
2823          * the fetch is done.
2824          *
2825          * Drop the read lock and try to re-obtain the write
2826          * lock.  If the vcache still has the same DV, it's
2827          * ok to go ahead and install the new data.
2828          */
2829         afs_hyper_t currentDV, statusDV;
2830
2831         hset(currentDV, avc->f.m.DataVersion);
2832
2833         if (setNewCallback && avc->callback != newCallback)
2834             doVcacheUpdate = 1;
2835
2836         if (tsmall) {
2837             hset64(statusDV, tsmall->OutStatus.dataVersionHigh,
2838                    tsmall->OutStatus.DataVersion);
2839
2840             if (setVcacheStatus && avc->f.m.Length != tsmall->OutStatus.Length)
2841                 doVcacheUpdate = 1;
2842             if (setVcacheStatus && !hsame(currentDV, statusDV))
2843                 doVcacheUpdate = 1;
2844         }
2845
2846         ReleaseReadLock(&avc->lock);
2847
2848         if (doVcacheUpdate) {
2849             ObtainWriteLock(&avc->lock, 615);
2850             if (!hsame(avc->f.m.DataVersion, currentDV)) {
2851                 /* We lose.  Someone will beat us to it. */
2852                 doVcacheUpdate = 0;
2853                 ReleaseWriteLock(&avc->lock);
2854             }
2855         }
2856     }
2857
2858     /* With slow pass, we've already done all the updates */
2859     if (slowPass) {
2860         ReleaseWriteLock(&avc->lock);
2861     }
2862
2863     /* Check if we need to perform any last-minute fixes with a write-lock */
2864     if (!setLocks || doVcacheUpdate) {
2865         if (setNewCallback)
2866             avc->callback = newCallback;
2867         if (tsmall && setVcacheStatus)
2868             afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2869         if (setLocks)
2870             ReleaseWriteLock(&avc->lock);
2871     }
2872
2873     if (tsmall)
2874         osi_FreeLargeSpace(tsmall);
2875
2876     return tdc;
2877 }                               /*afs_GetDCache */
2878
2879
2880 /*
2881  * afs_WriteThroughDSlots
2882  *
2883  * Description:
2884  *      Sweep through the dcache slots and write out any modified
2885  *      in-memory data back on to our caching store.
2886  *
2887  * Parameters:
2888  *      None.
2889  *
2890  * Environment:
2891  *      The afs_xdcache is write-locked through this whole affair.
2892  */
2893 int
2894 afs_WriteThroughDSlots(void)
2895 {
2896     struct dcache *tdc;
2897     afs_int32 i, touchedit = 0;
2898     int code = 0;
2899
2900     struct afs_q DirtyQ, *tq;
2901
2902     AFS_STATCNT(afs_WriteThroughDSlots);
2903
2904     /*
2905      * Because of lock ordering, we can't grab dcache locks while
2906      * holding afs_xdcache.  So we enter xdcache, get a reference
2907      * for every dcache entry, and exit xdcache.
2908      */
2909     ObtainWriteLock(&afs_xdcache, 283);
2910     QInit(&DirtyQ);
2911     for (i = 0; i < afs_cacheFiles; i++) {
2912         tdc = afs_indexTable[i];
2913
2914         /* Grab tlock in case the existing refcount isn't zero */
2915         if (tdc && !(afs_indexFlags[i] & (IFFree | IFDiscarded))) {
2916             ObtainWriteLock(&tdc->tlock, 623);
2917             tdc->refCount++;
2918             ReleaseWriteLock(&tdc->tlock);
2919
2920             QAdd(&DirtyQ, &tdc->dirty);
2921         }
2922     }
2923     ReleaseWriteLock(&afs_xdcache);
2924
2925     /*
2926      * Now, for each dcache entry we found, check if it's dirty.
2927      * If so, get write-lock, get afs_xdcache, which protects
2928      * afs_cacheInodep, and flush it.  Don't forget to put back
2929      * the refcounts.
2930      */
2931
2932 #define DQTODC(q)       ((struct dcache *)(((char *) (q)) - sizeof(struct afs_q)))
2933
2934     for (tq = DirtyQ.prev; tq != &DirtyQ && code == 0; tq = QPrev(tq)) {
2935         tdc = DQTODC(tq);
2936         if (tdc->dflags & DFEntryMod) {
2937             int wrLock;
2938
2939             wrLock = (0 == NBObtainWriteLock(&tdc->lock, 619));
2940
2941             /* Now that we have the write lock, double-check */
2942             if (wrLock && (tdc->dflags & DFEntryMod)) {
2943                 tdc->dflags &= ~DFEntryMod;
2944                 ObtainWriteLock(&afs_xdcache, 620);
2945                 code = afs_WriteDCache(tdc, 1);
2946                 ReleaseWriteLock(&afs_xdcache);
2947                 if (code) {
2948                     /* We didn't successfully write out the dslot; make sure we
2949                      * try again later */
2950                     tdc->dflags |= DFEntryMod;
2951                 } else {
2952                     touchedit = 1;
2953                 }
2954             }
2955             if (wrLock)
2956                 ReleaseWriteLock(&tdc->lock);
2957         }
2958
2959         afs_PutDCache(tdc);
2960     }
2961
2962     if (code) {
2963         return code;
2964     }
2965
2966     ObtainWriteLock(&afs_xdcache, 617);
2967     if (!touchedit && (cacheDiskType != AFS_FCACHE_TYPE_MEM)) {
2968         /* Touch the file to make sure that the mtime on the file is kept
2969          * up-to-date to avoid losing cached files on cold starts because
2970          * their mtime seems old...
2971          */
2972         struct afs_fheader theader;
2973
2974         afs_InitFHeader(&theader);
2975         afs_osi_Write(afs_cacheInodep, 0, &theader, sizeof(theader));
2976     }
2977     ReleaseWriteLock(&afs_xdcache);
2978     return 0;
2979 }
2980
2981 /*
2982  * afs_MemGetDSlot
2983  *
2984  * Description:
2985  *      Return a pointer to an freshly initialized dcache entry using
2986  *      a memory-based cache.  The tlock will be read-locked.
2987  *
2988  * Parameters:
2989  *      aslot : Dcache slot to look at.
2990  *      type : What 'type' of dslot to get; see the dslot_state enum
2991  *
2992  * Environment:
2993  *      Must be called with afs_xdcache write-locked.
2994  */
2995
2996 struct dcache *
2997 afs_MemGetDSlot(afs_int32 aslot, dslot_state type)
2998 {
2999     struct dcache *tdc;
3000     int existing = 0;
3001
3002     AFS_STATCNT(afs_MemGetDSlot);
3003     if (CheckLock(&afs_xdcache) != -1)
3004         osi_Panic("getdslot nolock");
3005     if (aslot < 0 || aslot >= afs_cacheFiles)
3006         osi_Panic("getdslot slot %d (of %d)", aslot, afs_cacheFiles);
3007     tdc = afs_indexTable[aslot];
3008     if (tdc) {
3009         QRemove(&tdc->lruq);    /* move to queue head */
3010         QAdd(&afs_DLRU, &tdc->lruq);
3011         /* We're holding afs_xdcache, but get tlock in case refCount != 0 */
3012         ObtainWriteLock(&tdc->tlock, 624);
3013         tdc->refCount++;
3014         ConvertWToRLock(&tdc->tlock);
3015         return tdc;
3016     }
3017
3018     /* if we got here, the given slot is not in memory in our list of known
3019      * slots. for memcache, the only place a dslot can exist is in memory, so
3020      * if the caller is expecting to get back a known dslot, and we've reached
3021      * here, something is very wrong. DSLOT_NEW is the only type of dslot that
3022      * may not exist; for all others, the caller assumes the given dslot
3023      * already exists. so, 'type' had better be DSLOT_NEW here, or something is
3024      * very wrong. */
3025     osi_Assert(type == DSLOT_NEW);
3026
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 = afs_osi_Alloc(sizeof(struct dcache));
3033         osi_Assert(tdc != NULL);
3034 #ifdef  KERNEL_HAVE_PIN
3035         pin((char *)tdc, sizeof(struct dcache));        /* XXX */
3036 #endif
3037     } else {
3038         tdc = afs_freeDSList;
3039         afs_freeDSList = (struct dcache *)tdc->lruq.next;
3040         existing = 1;
3041     }
3042     tdc->dflags = 0;    /* up-to-date, not in free q */
3043     tdc->mflags = 0;
3044     QAdd(&afs_DLRU, &tdc->lruq);
3045     if (tdc->lruq.prev == &tdc->lruq)
3046         osi_Panic("lruq 3");
3047
3048     /* initialize entry */
3049     tdc->f.fid.Cell = 0;
3050     tdc->f.fid.Fid.Volume = 0;
3051     tdc->f.chunk = -1;
3052     hones(tdc->f.versionNo);
3053     tdc->f.inode.mem = aslot;
3054     tdc->dflags |= DFEntryMod;
3055     tdc->refCount = 1;
3056     tdc->index = aslot;
3057     afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
3058
3059     if (existing) {
3060         osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
3061         osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
3062         osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
3063     }
3064
3065     AFS_RWLOCK_INIT(&tdc->lock, "dcache lock");
3066     AFS_RWLOCK_INIT(&tdc->tlock, "dcache tlock");
3067     AFS_RWLOCK_INIT(&tdc->mflock, "dcache flock");
3068     ObtainReadLock(&tdc->tlock);
3069
3070     afs_indexTable[aslot] = tdc;
3071     return tdc;
3072
3073 }                               /*afs_MemGetDSlot */
3074
3075 unsigned int last_error = 0, lasterrtime = 0;
3076
3077 /*
3078  * afs_UFSGetDSlot
3079  *
3080  * Description:
3081  *      Return a pointer to an freshly initialized dcache entry using
3082  *      a UFS-based disk cache.  The dcache tlock will be read-locked.
3083  *
3084  * Parameters:
3085  *      aslot : Dcache slot to look at.
3086  *      type : What 'type' of dslot to get; see the dslot_state enum
3087  *
3088  * Environment:
3089  *      afs_xdcache lock write-locked.
3090  */
3091 struct dcache *
3092 afs_UFSGetDSlot(afs_int32 aslot, dslot_state type)
3093 {
3094     afs_int32 code;
3095     struct dcache *tdc;
3096     int existing = 0;
3097     int entryok;
3098     int off;
3099
3100     AFS_STATCNT(afs_UFSGetDSlot);
3101     if (CheckLock(&afs_xdcache) != -1)
3102         osi_Panic("getdslot nolock");
3103     if (aslot < 0 || aslot >= afs_cacheFiles)
3104         osi_Panic("getdslot slot %d (of %d)", aslot, afs_cacheFiles);
3105     tdc = afs_indexTable[aslot];
3106     if (tdc) {
3107         QRemove(&tdc->lruq);    /* move to queue head */
3108         QAdd(&afs_DLRU, &tdc->lruq);
3109         /* Grab tlock in case refCount != 0 */
3110         ObtainWriteLock(&tdc->tlock, 625);
3111         tdc->refCount++;
3112         ConvertWToRLock(&tdc->tlock);
3113         return tdc;
3114     }
3115
3116     /* otherwise we should read it in from the cache file */
3117     if (!afs_freeDSList)
3118         afs_GetDownDSlot(4);
3119     if (!afs_freeDSList) {
3120         /* none free, making one is better than a panic */
3121         afs_stats_cmperf.dcacheXAllocs++;       /* count in case we have a leak */
3122         tdc = afs_osi_Alloc(sizeof(struct dcache));
3123         osi_Assert(tdc != NULL);
3124 #ifdef  KERNEL_HAVE_PIN
3125         pin((char *)tdc, sizeof(struct dcache));        /* XXX */
3126 #endif
3127     } else {
3128         tdc = afs_freeDSList;
3129         afs_freeDSList = (struct dcache *)tdc->lruq.next;
3130         existing = 1;
3131     }
3132     tdc->dflags = 0;    /* up-to-date, not in free q */
3133     tdc->mflags = 0;
3134     QAdd(&afs_DLRU, &tdc->lruq);
3135     if (tdc->lruq.prev == &tdc->lruq)
3136         osi_Panic("lruq 3");
3137
3138     /*
3139      * Seek to the aslot'th entry and read it in.
3140      */
3141     off = sizeof(struct fcache)*aslot + sizeof(struct afs_fheader);
3142     code =
3143         afs_osi_Read(afs_cacheInodep,
3144                      off, (char *)(&tdc->f),
3145                      sizeof(struct fcache));
3146     entryok = 1;
3147     if (code != sizeof(struct fcache)) {
3148         entryok = 0;
3149 #if defined(KERNEL_HAVE_UERROR)
3150         last_error = getuerror();
3151 #else
3152         last_error = code;
3153 #endif
3154         lasterrtime = osi_Time();
3155         if (type != DSLOT_NEW) {
3156             /* If we are requesting a non-DSLOT_NEW slot, this is an error.
3157              * non-DSLOT_NEW slots are supposed to already exist, so if we
3158              * failed to read in the slot, something is wrong. */
3159             struct osi_stat tstat;
3160             if (afs_osi_Stat(afs_cacheInodep, &tstat)) {
3161                 tstat.size = -1;
3162             }
3163             afs_warn("afs: disk cache read error in CacheItems slot %d "
3164                      "off %d/%d code %d/%d\n",
3165                      (int)aslot,
3166                      off, (int)tstat.size,
3167                      (int)code, (int)sizeof(struct fcache));
3168             /* put tdc back on the free dslot list */
3169             QRemove(&tdc->lruq);
3170             tdc->index = NULLIDX;
3171             tdc->lruq.next = (struct afs_q *)afs_freeDSList;
3172             afs_freeDSList = tdc;
3173             return NULL;
3174         }
3175     }
3176     if (!afs_CellNumValid(tdc->f.fid.Cell)) {
3177         entryok = 0;
3178         if (type == DSLOT_VALID) {
3179             osi_Panic("afs: needed valid dcache but index %d off %d has "
3180                       "invalid cell num %d\n",
3181                       (int)aslot, off, (int)tdc->f.fid.Cell);
3182         }
3183     }
3184
3185     if (type == DSLOT_VALID && tdc->f.fid.Fid.Volume == 0) {
3186         osi_Panic("afs: invalid zero-volume dcache entry at slot %d off %d",
3187                   (int)aslot, off);
3188     }
3189
3190     if (type == DSLOT_UNUSED) {
3191         /* the requested dslot is known to exist, but contain invalid data
3192          * (this happens when we're using a dslot from the free or discard
3193          * list). be sure not to re-use the data in it, so force invalidation.
3194          */
3195         entryok = 0;
3196     }
3197
3198     if (!entryok) {
3199         tdc->f.fid.Cell = 0;
3200         tdc->f.fid.Fid.Volume = 0;
3201         tdc->f.chunk = -1;
3202         hones(tdc->f.versionNo);
3203         tdc->dflags |= DFEntryMod;
3204         afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
3205         tdc->f.states &= ~(DRO|DBackup|DRW);
3206         afs_DCMoveBucket(tdc, 0, 0);
3207     } else {
3208         if (tdc->f.states & DRO) {
3209             afs_DCMoveBucket(tdc, 0, 2);
3210         } else if (tdc->f.states & DBackup) {
3211             afs_DCMoveBucket(tdc, 0, 1);
3212         } else {
3213             afs_DCMoveBucket(tdc, 0, 1);
3214         }
3215     }
3216     tdc->refCount = 1;
3217     tdc->index = aslot;
3218     if (tdc->f.chunk >= 0)
3219         tdc->validPos = AFS_CHUNKTOBASE(tdc->f.chunk) + tdc->f.chunkBytes;
3220     else
3221         tdc->validPos = 0;
3222
3223     if (existing) {
3224         osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
3225         osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
3226         osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
3227     }
3228
3229     AFS_RWLOCK_INIT(&tdc->lock, "dcache lock");
3230     AFS_RWLOCK_INIT(&tdc->tlock, "dcache tlock");
3231     AFS_RWLOCK_INIT(&tdc->mflock, "dcache flock");
3232     ObtainReadLock(&tdc->tlock);
3233
3234     /*
3235      * If we didn't read into a temporary dcache region, update the
3236      * slot pointer table.
3237      */
3238     afs_indexTable[aslot] = tdc;
3239     return tdc;
3240
3241 }                               /*afs_UFSGetDSlot */
3242
3243
3244
3245 /*!
3246  * Write a particular dcache entry back to its home in the
3247  * CacheInfo file.
3248  *
3249  * \param adc Pointer to the dcache entry to write.
3250  * \param atime If true, set the modtime on the file to the current time.
3251  *
3252  * \note Environment:
3253  *      Must be called with the afs_xdcache lock at least read-locked,
3254  *      and dcache entry at least read-locked.
3255  *      The reference count is not changed.
3256  */
3257
3258 int
3259 afs_WriteDCache(struct dcache *adc, int atime)
3260 {
3261     afs_int32 code;
3262
3263     if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
3264         return 0;
3265     AFS_STATCNT(afs_WriteDCache);
3266     osi_Assert(WriteLocked(&afs_xdcache));
3267     if (atime)
3268         adc->f.modTime = osi_Time();
3269
3270     if ((afs_indexFlags[adc->index] & (IFFree | IFDiscarded)) == 0 &&
3271         adc->f.fid.Fid.Volume == 0) {
3272         /* If a dcache slot is not on the free or discard list, it must be
3273          * in the hash table. Thus, the volume must be non-zero, since that
3274          * is how we determine whether or not to unhash the entry when kicking
3275          * it out of the cache. Do this check now, since otherwise this can
3276          * cause hash table corruption and a panic later on after we read the
3277          * entry back in. */
3278         osi_Panic("afs_WriteDCache zero volume index %d flags 0x%x\n",
3279                   adc->index, (unsigned)afs_indexFlags[adc->index]);
3280     }
3281
3282     /*
3283      * Seek to the right dcache slot and write the in-memory image out to disk.
3284      */
3285     afs_cellname_write();
3286     code =
3287         afs_osi_Write(afs_cacheInodep,
3288                       sizeof(struct fcache) * adc->index +
3289                       sizeof(struct afs_fheader), (char *)(&adc->f),
3290                       sizeof(struct fcache));
3291     if (code != sizeof(struct fcache)) {
3292         afs_warn("afs: failed to write to CacheItems off %ld code %d/%d\n",
3293                  (long)(sizeof(struct fcache) * adc->index + sizeof(struct afs_fheader)),
3294                  (int)code, (int)sizeof(struct fcache));
3295         return EIO;
3296     }
3297     return 0;
3298 }
3299
3300
3301
3302 /*!
3303  * Wake up users of a particular file waiting for stores to take
3304  * place.
3305  *
3306  * \param avc Ptr to related vcache entry.
3307  *
3308  * \note Environment:
3309  *      Nothing interesting.
3310  */
3311 int
3312 afs_wakeup(struct vcache *avc)
3313 {
3314     int i;
3315     struct brequest *tb;
3316     tb = afs_brs;
3317     AFS_STATCNT(afs_wakeup);
3318     for (i = 0; i < NBRS; i++, tb++) {
3319         /* if request is valid and for this file, we've found it */
3320         if (tb->refCount > 0 && avc == tb->vc) {
3321
3322             /*
3323              * If CSafeStore is on, then we don't awaken the guy
3324              * waiting for the store until the whole store has finished.
3325              * Otherwise, we do it now.  Note that if CSafeStore is on,
3326              * the BStore routine actually wakes up the user, instead
3327              * of us.
3328              * I think this is redundant now because this sort of thing
3329              * is already being handled by the higher-level code.
3330              */
3331             if ((avc->f.states & CSafeStore) == 0) {
3332                 tb->code_raw = tb->code_checkcode = 0;
3333                 tb->flags |= BUVALID;
3334                 if (tb->flags & BUWAIT) {
3335                     tb->flags &= ~BUWAIT;
3336                     afs_osi_Wakeup(tb);
3337                 }
3338             }
3339             break;
3340         }
3341     }
3342     return 0;
3343 }
3344
3345 /*!
3346  * Given a file name and inode, set up that file to be an
3347  * active member in the AFS cache.  This also involves checking
3348  * the usability of its data.
3349  *
3350  * \param afile Name of the cache file to initialize.
3351  * \param ainode Inode of the file.
3352  *
3353  * \note Environment:
3354  *      This function is called only during initialization.
3355  */
3356 int
3357 afs_InitCacheFile(char *afile, ino_t ainode)
3358 {
3359     afs_int32 code;
3360     afs_int32 index;
3361     int fileIsBad;
3362     struct osi_file *tfile;
3363     struct osi_stat tstat;
3364     struct dcache *tdc;
3365
3366     AFS_STATCNT(afs_InitCacheFile);
3367     index = afs_stats_cmperf.cacheNumEntries;
3368     if (index >= afs_cacheFiles)
3369         return EINVAL;
3370
3371     ObtainWriteLock(&afs_xdcache, 282);
3372     tdc = afs_GetNewDSlot(index);
3373     ReleaseReadLock(&tdc->tlock);
3374     ReleaseWriteLock(&afs_xdcache);
3375
3376     ObtainWriteLock(&tdc->lock, 621);
3377     ObtainWriteLock(&afs_xdcache, 622);
3378     if (!afile && !ainode) {
3379         tfile = NULL;
3380         fileIsBad = 1;
3381     } else {
3382         if (afile) {
3383             code = afs_LookupInodeByPath(afile, &tdc->f.inode.ufs, NULL);
3384             if (code) {
3385                 ReleaseWriteLock(&afs_xdcache);
3386                 ReleaseWriteLock(&tdc->lock);
3387                 afs_PutDCache(tdc);
3388                 return code;
3389             }
3390         } else {
3391             /* Add any other 'complex' inode types here ... */
3392 #if !defined(AFS_LINUX26_ENV) && !defined(AFS_CACHE_VNODE_PATH)
3393             tdc->f.inode.ufs = ainode;
3394 #else
3395             osi_Panic("Can't init cache with inode numbers when complex inodes are "
3396                       "in use\n");
3397 #endif
3398         }
3399         fileIsBad = 0;
3400         if ((tdc->f.states & DWriting) || tdc->f.fid.Fid.Volume == 0)
3401             fileIsBad = 1;
3402         tfile = osi_UFSOpen(&tdc->f.inode);
3403         if (!tfile) {
3404             ReleaseWriteLock(&afs_xdcache);
3405             ReleaseWriteLock(&tdc->lock);
3406             afs_PutDCache(tdc);
3407             return ENOENT;
3408         }
3409
3410         code = afs_osi_Stat(tfile, &tstat);
3411         if (code)
3412             osi_Panic("initcachefile stat");
3413
3414         /*
3415          * If file size doesn't match the cache info file, it's probably bad.
3416          */
3417         if (tdc->f.chunkBytes != tstat.size)
3418             fileIsBad = 1;
3419         /*
3420          * If file changed within T (120?) seconds of cache info file, it's
3421          * probably bad.  In addition, if slot changed within last T seconds,
3422          * the cache info file may be incorrectly identified, and so slot
3423          * may be bad.
3424          */
3425         if (cacheInfoModTime < tstat.mtime + 120)
3426             fileIsBad = 1;
3427         if (cacheInfoModTime < tdc->f.modTime + 120)
3428             fileIsBad = 1;
3429         /* In case write through is behind, make sure cache items entry is
3430          * at least as new as the chunk.
3431          */
3432         if (tdc->f.modTime < tstat.mtime)
3433             fileIsBad = 1;
3434     }
3435     tdc->f.chunkBytes = 0;
3436
3437     if (fileIsBad) {
3438         tdc->f.fid.Fid.Volume = 0;      /* not in the hash table */
3439         if (tfile && tstat.size != 0)
3440             osi_UFSTruncate(tfile, 0);
3441         tdc->f.states &= ~(DRO|DBackup|DRW);
3442         afs_DCMoveBucket(tdc, 0, 0);
3443         /* put entry in free cache slot list */
3444         afs_dvnextTbl[tdc->index] = afs_freeDCList;
3445         afs_freeDCList = index;
3446         afs_freeDCCount++;
3447         afs_indexFlags[index] |= IFFree;
3448         afs_indexUnique[index] = 0;
3449     } else {
3450         /*
3451          * We must put this entry in the appropriate hash tables.
3452          * Note that i is still set from the above DCHash call
3453          */
3454         code = DCHash(&tdc->f.fid, tdc->f.chunk);
3455         afs_dcnextTbl[tdc->index] = afs_dchashTbl[code];
3456         afs_dchashTbl[code] = tdc->index;
3457         code = DVHash(&tdc->f.fid);
3458         afs_dvnextTbl[tdc->index] = afs_dvhashTbl[code];
3459         afs_dvhashTbl[code] = tdc->index;
3460         afs_AdjustSize(tdc, tstat.size);        /* adjust to new size */
3461         if (tstat.size > 0)
3462             /* has nontrivial amt of data */
3463             afs_indexFlags[index] |= IFEverUsed;
3464         afs_stats_cmperf.cacheFilesReused++;
3465         /*
3466          * Initialize index times to file's mod times; init indexCounter
3467          * to max thereof
3468          */
3469         hset32(afs_indexTimes[index], tstat.atime);
3470         if (hgetlo(afs_indexCounter) < tstat.atime) {
3471             hset32(afs_indexCounter, tstat.atime);
3472         }
3473         afs_indexUnique[index] = tdc->f.fid.Fid.Unique;
3474     }                           /*File is not bad */
3475
3476     if (tfile)
3477         osi_UFSClose(tfile);
3478     tdc->f.states &= ~DWriting;
3479     tdc->dflags &= ~DFEntryMod;
3480     /* don't set f.modTime; we're just cleaning up */
3481     osi_Assert(afs_WriteDCache(tdc, 0) == 0);
3482     ReleaseWriteLock(&afs_xdcache);
3483     ReleaseWriteLock(&tdc->lock);
3484     afs_PutDCache(tdc);
3485     afs_stats_cmperf.cacheNumEntries++;
3486     return 0;
3487 }
3488
3489
3490 /*Max # of struct dcache's resident at any time*/
3491 /*
3492  * If 'dchint' is enabled then in-memory dcache min is increased because of
3493  * crashes...
3494  */
3495 #define DDSIZE 200
3496
3497 /*!
3498  * Initialize dcache related variables.
3499  *
3500  * \param afiles
3501  * \param ablocks
3502  * \param aDentries
3503  * \param achunk
3504  * \param aflags
3505  *
3506  */
3507 int
3508 afs_dcacheInit(int afiles, int ablocks, int aDentries, int achunk, int aflags)
3509 {
3510     struct dcache *tdp;
3511     int i;
3512     int code;
3513     int afs_dhashbits;
3514
3515     afs_freeDCList = NULLIDX;
3516     afs_discardDCList = NULLIDX;
3517     afs_freeDCCount = 0;
3518     afs_freeDSList = NULL;
3519     hzero(afs_indexCounter);
3520
3521     LOCK_INIT(&afs_xdcache, "afs_xdcache");
3522
3523     /*
3524      * Set chunk size
3525      */
3526     if (achunk) {
3527         if (achunk < 0 || achunk > 30)
3528             achunk = 13;        /* Use default */
3529         AFS_SETCHUNKSIZE(achunk);
3530     }
3531
3532     if (!aDentries)
3533         aDentries = DDSIZE;
3534
3535     /* afs_dhashsize defaults to 1024 */
3536     if (aDentries > 512)
3537         afs_dhashsize = 2048;
3538     /* Try to keep the average chain length around two unless the table
3539      * would be ridiculously big. */
3540     if (aDentries > 4096) {
3541         afs_dhashbits = opr_fls(aDentries) - 3;
3542         /* Cap the hash tables to 32k entries. */
3543         if (afs_dhashbits > 15)
3544             afs_dhashbits = 15;
3545         afs_dhashsize = opr_jhash_size(afs_dhashbits);
3546     }
3547     /* initialize hash tables */
3548     afs_dvhashTbl = afs_osi_Alloc(afs_dhashsize * sizeof(afs_int32));
3549     osi_Assert(afs_dvhashTbl != NULL);
3550     afs_dchashTbl = afs_osi_Alloc(afs_dhashsize * sizeof(afs_int32));
3551     osi_Assert(afs_dchashTbl != NULL);
3552     for (i = 0; i < afs_dhashsize; i++) {
3553         afs_dvhashTbl[i] = NULLIDX;
3554         afs_dchashTbl[i] = NULLIDX;
3555     }
3556     afs_dvnextTbl = afs_osi_Alloc(afiles * sizeof(afs_int32));
3557     osi_Assert(afs_dvnextTbl != NULL);
3558     afs_dcnextTbl = afs_osi_Alloc(afiles * sizeof(afs_int32));
3559     osi_Assert(afs_dcnextTbl != NULL);
3560     for (i = 0; i < afiles; i++) {
3561         afs_dvnextTbl[i] = NULLIDX;
3562         afs_dcnextTbl[i] = NULLIDX;
3563     }
3564
3565     /* Allocate and zero the pointer array to the dcache entries */
3566     afs_indexTable = afs_osi_Alloc(sizeof(struct dcache *) * afiles);
3567     osi_Assert(afs_indexTable != NULL);
3568     memset(afs_indexTable, 0, sizeof(struct dcache *) * afiles);
3569     afs_indexTimes = afs_osi_Alloc(afiles * sizeof(afs_hyper_t));
3570     osi_Assert(afs_indexTimes != NULL);
3571     memset(afs_indexTimes, 0, afiles * sizeof(afs_hyper_t));
3572     afs_indexUnique = afs_osi_Alloc(afiles * sizeof(afs_uint32));
3573     osi_Assert(afs_indexUnique != NULL);
3574     memset(afs_indexUnique, 0, afiles * sizeof(afs_uint32));
3575     afs_indexFlags = afs_osi_Alloc(afiles * sizeof(u_char));
3576     osi_Assert(afs_indexFlags != NULL);
3577     memset(afs_indexFlags, 0, afiles * sizeof(char));
3578
3579     /* Allocate and thread the struct dcache entries themselves */
3580     tdp = afs_Initial_freeDSList =
3581         afs_osi_Alloc(aDentries * sizeof(struct dcache));
3582     osi_Assert(tdp != NULL);
3583     memset(tdp, 0, aDentries * sizeof(struct dcache));
3584 #ifdef  KERNEL_HAVE_PIN
3585     pin((char *)afs_indexTable, sizeof(struct dcache *) * afiles);      /* XXX */
3586     pin((char *)afs_indexTimes, sizeof(afs_hyper_t) * afiles);  /* XXX */
3587     pin((char *)afs_indexFlags, sizeof(char) * afiles); /* XXX */
3588     pin((char *)afs_indexUnique, sizeof(afs_int32) * afiles);   /* XXX */
3589     pin((char *)tdp, aDentries * sizeof(struct dcache));        /* XXX */
3590     pin((char *)afs_dvhashTbl, sizeof(afs_int32) * afs_dhashsize);      /* XXX */
3591     pin((char *)afs_dchashTbl, sizeof(afs_int32) * afs_dhashsize);      /* XXX */
3592     pin((char *)afs_dcnextTbl, sizeof(afs_int32) * afiles);     /* XXX */
3593     pin((char *)afs_dvnextTbl, sizeof(afs_int32) * afiles);     /* XXX */
3594 #endif
3595
3596     afs_freeDSList = &tdp[0];
3597     for (i = 0; i < aDentries - 1; i++) {
3598         tdp[i].lruq.next = (struct afs_q *)(&tdp[i + 1]);
3599         AFS_RWLOCK_INIT(&tdp[i].lock, "dcache lock");
3600         AFS_RWLOCK_INIT(&tdp[i].tlock, "dcache tlock");
3601         AFS_RWLOCK_INIT(&tdp[i].mflock, "dcache flock");
3602     }
3603     tdp[aDentries - 1].lruq.next = (struct afs_q *)0;
3604     AFS_RWLOCK_INIT(&tdp[aDentries - 1].lock, "dcache lock");
3605     AFS_RWLOCK_INIT(&tdp[aDentries - 1].tlock, "dcache tlock");
3606     AFS_RWLOCK_INIT(&tdp[aDentries - 1].mflock, "dcache flock");
3607
3608     afs_stats_cmperf.cacheBlocksOrig = afs_stats_cmperf.cacheBlocksTotal =
3609         afs_cacheBlocks = ablocks;
3610     afs_ComputeCacheParms();    /* compute parms based on cache size */
3611
3612     afs_dcentries = aDentries;
3613     afs_blocksUsed = 0;
3614     afs_stats_cmperf.cacheBucket0_Discarded =
3615         afs_stats_cmperf.cacheBucket1_Discarded =
3616         afs_stats_cmperf.cacheBucket2_Discarded = 0;
3617     afs_DCSizeInit();
3618     QInit(&afs_DLRU);
3619
3620     if (aflags & AFSCALL_INIT_MEMCACHE) {
3621         /*
3622          * Use a memory cache instead of a disk cache
3623          */
3624         cacheDiskType = AFS_FCACHE_TYPE_MEM;
3625         afs_cacheType = &afs_MemCacheOps;
3626         afiles = (afiles < aDentries) ? afiles : aDentries;     /* min */
3627         ablocks = afiles * (AFS_FIRSTCSIZE / 1024);
3628         /* ablocks is reported in 1K blocks */
3629         code = afs_InitMemCache(afiles, AFS_FIRSTCSIZE, aflags);
3630         if (code != 0) {
3631             afs_warn("afsd: memory cache too large for available memory.\n");
3632             afs_warn("afsd: AFS files cannot be accessed.\n\n");
3633             dcacheDisabled = 1;
3634             return code;
3635         } else
3636             afs_warn("Memory cache: Allocating %d dcache entries...",
3637                    aDentries);
3638     } else {
3639         cacheDiskType = AFS_FCACHE_TYPE_UFS;
3640         afs_cacheType = &afs_UfsCacheOps;
3641     }
3642     return 0;
3643 }
3644
3645 /*!
3646  * Shuts down the cache.
3647  *
3648  */
3649 void
3650 shutdown_dcache(void)
3651 {
3652     int i;
3653
3654 #ifdef AFS_CACHE_VNODE_PATH
3655     if (cacheDiskType != AFS_FCACHE_TYPE_MEM) {
3656         struct dcache *tdc;
3657         for (i = 0; i < afs_cacheFiles; i++) {
3658             tdc = afs_indexTable[i];
3659             if (tdc) {
3660                 afs_osi_FreeStr(tdc->f.inode.ufs);
3661             }
3662         }
3663     }
3664 #endif
3665
3666     afs_osi_Free(afs_dvnextTbl, afs_cacheFiles * sizeof(afs_int32));
3667     afs_osi_Free(afs_dcnextTbl, afs_cacheFiles * sizeof(afs_int32));
3668     afs_osi_Free(afs_indexTable, afs_cacheFiles * sizeof(struct dcache *));
3669     afs_osi_Free(afs_indexTimes, afs_cacheFiles * sizeof(afs_hyper_t));
3670     afs_osi_Free(afs_indexUnique, afs_cacheFiles * sizeof(afs_uint32));
3671     afs_osi_Free(afs_indexFlags, afs_cacheFiles * sizeof(u_char));
3672     afs_osi_Free(afs_Initial_freeDSList,
3673                  afs_dcentries * sizeof(struct dcache));
3674 #ifdef  KERNEL_HAVE_PIN
3675     unpin((char *)afs_dcnextTbl, afs_cacheFiles * sizeof(afs_int32));
3676     unpin((char *)afs_dvnextTbl, afs_cacheFiles * sizeof(afs_int32));
3677     unpin((char *)afs_indexTable, afs_cacheFiles * sizeof(struct dcache *));
3678     unpin((char *)afs_indexTimes, afs_cacheFiles * sizeof(afs_hyper_t));
3679     unpin((char *)afs_indexUnique, afs_cacheFiles * sizeof(afs_uint32));
3680     unpin((u_char *) afs_indexFlags, afs_cacheFiles * sizeof(u_char));
3681     unpin(afs_Initial_freeDSList, afs_dcentries * sizeof(struct dcache));
3682 #endif
3683
3684
3685     for (i = 0; i < afs_dhashsize; i++) {
3686         afs_dvhashTbl[i] = NULLIDX;
3687         afs_dchashTbl[i] = NULLIDX;
3688     }
3689
3690     afs_osi_Free(afs_dvhashTbl, afs_dhashsize * sizeof(afs_int32));
3691     afs_osi_Free(afs_dchashTbl, afs_dhashsize * sizeof(afs_int32));
3692
3693     afs_blocksUsed = afs_dcentries = 0;
3694     afs_stats_cmperf.cacheBucket0_Discarded =
3695         afs_stats_cmperf.cacheBucket1_Discarded =
3696         afs_stats_cmperf.cacheBucket2_Discarded = 0;
3697     hzero(afs_indexCounter);
3698
3699     afs_freeDCCount = 0;
3700     afs_freeDCList = NULLIDX;
3701     afs_discardDCList = NULLIDX;
3702     afs_freeDSList = afs_Initial_freeDSList = 0;
3703
3704     LOCK_INIT(&afs_xdcache, "afs_xdcache");
3705     QInit(&afs_DLRU);
3706
3707 }
3708
3709 /*!
3710  * Get a dcache ready for writing, respecting the current cache size limits
3711  *
3712  * len is required because afs_GetDCache with flag == 4 expects the length
3713  * field to be filled. It decides from this whether it's necessary to fetch
3714  * data into the chunk before writing or not (when the whole chunk is
3715  * overwritten!).
3716  *
3717  * \param avc           The vcache to fetch a dcache for
3718  * \param filePos       The start of the section to be written
3719  * \param len           The length of the section to be written
3720  * \param areq
3721  * \param noLock
3722  *
3723  * \return If successful, a reference counted dcache with tdc->lock held. Lock
3724  *         must be released and afs_PutDCache() called to free dcache.
3725  *         NULL on  failure
3726  *
3727  * \note avc->lock must be held on entry. Function may release and reobtain
3728  *       avc->lock and GLOCK.
3729  */
3730
3731 struct dcache *
3732 afs_ObtainDCacheForWriting(struct vcache *avc, afs_size_t filePos,
3733                            afs_size_t len, struct vrequest *areq,
3734                            int noLock)
3735 {
3736     struct dcache *tdc = NULL;
3737     afs_size_t offset;
3738
3739     /* read the cached info */
3740     if (noLock) {
3741         tdc = afs_FindDCache(avc, filePos);
3742         if (tdc)
3743             ObtainWriteLock(&tdc->lock, 657);
3744     } else if (afs_blocksUsed >
3745                PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
3746         tdc = afs_FindDCache(avc, filePos);
3747         if (tdc) {
3748             ObtainWriteLock(&tdc->lock, 658);
3749             if (!afs_IsDCacheFresh(tdc, avc)
3750                 || (tdc->dflags & DFFetching)) {
3751                 ReleaseWriteLock(&tdc->lock);
3752                 afs_PutDCache(tdc);
3753                 tdc = NULL;
3754             }
3755         }
3756         if (!tdc) {
3757             afs_MaybeWakeupTruncateDaemon();
3758             while (afs_blocksUsed >
3759                    PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
3760                 ReleaseWriteLock(&avc->lock);
3761                 if (afs_blocksUsed - afs_blocksDiscarded >
3762                     PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
3763                     afs_WaitForCacheDrain = 1;
3764                     afs_osi_Sleep(&afs_WaitForCacheDrain);
3765                 }
3766                 afs_MaybeFreeDiscardedDCache();
3767                 afs_MaybeWakeupTruncateDaemon();
3768                 ObtainWriteLock(&avc->lock, 509);
3769             }
3770             avc->f.states |= CDirty;
3771             tdc = afs_GetDCache(avc, filePos, areq, &offset, &len, 4);
3772             if (tdc)
3773                 ObtainWriteLock(&tdc->lock, 659);
3774         }
3775     } else {
3776         tdc = afs_GetDCache(avc, filePos, areq, &offset, &len, 4);
3777         if (tdc)
3778             ObtainWriteLock(&tdc->lock, 660);
3779     }
3780     if (tdc) {
3781         if (!(afs_indexFlags[tdc->index] & IFDataMod)) {
3782             afs_stats_cmperf.cacheCurrDirtyChunks++;
3783             afs_indexFlags[tdc->index] |= IFDataMod;    /* so it doesn't disappear */
3784         }
3785         if (!(tdc->f.states & DWriting)) {
3786             /* don't mark entry as mod if we don't have to */
3787             tdc->f.states |= DWriting;
3788             tdc->dflags |= DFEntryMod;
3789         }
3790     }
3791     return tdc;
3792 }
3793
3794 /*!
3795  * Make a shadow copy of a dir's dcache. It's used for disconnected
3796  * operations like remove/create/rename to keep the original directory data.
3797  * On reconnection, we can diff the original data with the server and get the
3798  * server changes and with the local data to get the local changes.
3799  *
3800  * \param avc The dir vnode.
3801  * \param adc The dir dcache.
3802  *
3803  * \return 0 for success.
3804  *
3805  * \note The vcache entry must be write locked.
3806  * \note The dcache entry must be read locked.
3807  */
3808 int
3809 afs_MakeShadowDir(struct vcache *avc, struct dcache *adc)
3810 {
3811     int i, code, ret_code = 0, written, trans_size;
3812     struct dcache *new_dc = NULL;
3813     struct osi_file *tfile_src, *tfile_dst;
3814     struct VenusFid shadow_fid;
3815     char *data;
3816
3817     /* Is this a dir? */
3818     if (vType(avc) != VDIR)
3819         return ENOTDIR;
3820
3821     if (avc->f.shadow.vnode || avc->f.shadow.unique)
3822         return EEXIST;
3823
3824     /* Generate a fid for the shadow dir. */
3825     shadow_fid.Cell = avc->f.fid.Cell;
3826     shadow_fid.Fid.Volume = avc->f.fid.Fid.Volume;
3827     afs_GenShadowFid(&shadow_fid);
3828
3829     ObtainWriteLock(&afs_xdcache, 716);
3830
3831     /* Get a fresh dcache. */
3832     (void)afs_AllocDCache(&new_dc, avc, 0, 0, &shadow_fid);
3833     osi_Assert(new_dc);
3834
3835     ObtainReadLock(&adc->mflock);
3836
3837     /* Set up the new fid. */
3838     /* Copy interesting data from original dir dcache. */
3839     new_dc->mflags = adc->mflags;
3840     new_dc->dflags = adc->dflags;
3841     new_dc->f.modTime = adc->f.modTime;
3842     new_dc->f.versionNo = adc->f.versionNo;
3843     new_dc->f.states = adc->f.states;
3844     new_dc->f.chunk= adc->f.chunk;
3845     new_dc->f.chunkBytes = adc->f.chunkBytes;
3846
3847     ReleaseReadLock(&adc->mflock);
3848
3849     /* Now add to the two hash chains */
3850     i = DCHash(&shadow_fid, 0);
3851     afs_dcnextTbl[new_dc->index] = afs_dchashTbl[i];
3852     afs_dchashTbl[i] = new_dc->index;
3853
3854     i = DVHash(&shadow_fid);
3855     afs_dvnextTbl[new_dc->index] = afs_dvhashTbl[i];
3856     afs_dvhashTbl[i] = new_dc->index;
3857
3858     ReleaseWriteLock(&afs_xdcache);
3859
3860     /* Alloc a 4k block. */
3861     data = afs_osi_Alloc(4096);
3862     if (!data) {
3863         afs_warn("afs_MakeShadowDir: could not alloc data\n");
3864         ret_code = ENOMEM;
3865         goto done;
3866     }
3867
3868     /* Open the files. */
3869     tfile_src = afs_CFileOpen(&adc->f.inode);
3870     tfile_dst = afs_CFileOpen(&new_dc->f.inode);
3871     osi_Assert(tfile_src);
3872     osi_Assert(tfile_dst);
3873
3874     /* And now copy dir dcache data into this dcache,
3875      * 4k at a time.
3876      */
3877     written = 0;
3878     while (written < adc->f.chunkBytes) {
3879         trans_size = adc->f.chunkBytes - written;
3880         if (trans_size > 4096)
3881             trans_size = 4096;
3882
3883         /* Read a chunk from the dcache. */
3884         code = afs_CFileRead(tfile_src, written, data, trans_size);
3885         if (code < trans_size) {
3886             ret_code = EIO;
3887             break;
3888         }
3889
3890         /* Write it to the new dcache. */
3891         code = afs_CFileWrite(tfile_dst, written, data, trans_size);
3892         if (code < trans_size) {
3893             ret_code = EIO;
3894             break;
3895         }
3896
3897         written+=trans_size;
3898     }
3899
3900     afs_CFileClose(tfile_dst);
3901     afs_CFileClose(tfile_src);
3902
3903     afs_osi_Free(data, 4096);
3904
3905     ReleaseWriteLock(&new_dc->lock);
3906     afs_PutDCache(new_dc);
3907
3908     if (!ret_code) {
3909         ObtainWriteLock(&afs_xvcache, 763);
3910         ObtainWriteLock(&afs_disconDirtyLock, 765);
3911         QAdd(&afs_disconShadow, &avc->shadowq);
3912         osi_Assert((afs_RefVCache(avc) == 0));
3913         ReleaseWriteLock(&afs_disconDirtyLock);
3914         ReleaseWriteLock(&afs_xvcache);
3915
3916         avc->f.shadow.vnode = shadow_fid.Fid.Vnode;
3917         avc->f.shadow.unique = shadow_fid.Fid.Unique;
3918     }
3919
3920 done:
3921     return ret_code;
3922 }
3923
3924 /*!
3925  * Delete the dcaches of a shadow dir.
3926  *
3927  * \param avc The vcache containing the shadow fid.
3928  *
3929  * \note avc must be write locked.
3930  */
3931 void
3932 afs_DeleteShadowDir(struct vcache *avc)
3933 {
3934     struct dcache *tdc;
3935     struct VenusFid shadow_fid;
3936
3937     shadow_fid.Cell = avc->f.fid.Cell;
3938     shadow_fid.Fid.Volume = avc->f.fid.Fid.Volume;
3939     shadow_fid.Fid.Vnode = avc->f.shadow.vnode;
3940     shadow_fid.Fid.Unique = avc->f.shadow.unique;
3941
3942     tdc = afs_FindDCacheByFid(&shadow_fid);
3943     if (tdc) {
3944         afs_HashOutDCache(tdc, 1);
3945         afs_DiscardDCache(tdc);
3946         afs_PutDCache(tdc);
3947     }
3948     avc->f.shadow.vnode = avc->f.shadow.unique = 0;
3949     ObtainWriteLock(&afs_disconDirtyLock, 708);
3950     QRemove(&avc->shadowq);
3951     ReleaseWriteLock(&afs_disconDirtyLock);
3952     afs_PutVCache(avc); /* Because we held it when we added to the queue */
3953 }
3954
3955 /*!
3956  * Populate a dcache with empty chunks up to a given file size,
3957  * used before extending a file in order to avoid 'holes' which
3958  * we can't access in disconnected mode.
3959  *
3960  * \param avc   The vcache which is being extended (locked)
3961  * \param alen  The new length of the file
3962  *
3963  */
3964 void
3965 afs_PopulateDCache(struct vcache *avc, afs_size_t apos, struct vrequest *areq)
3966 {
3967     struct dcache *tdc;
3968     afs_size_t len, offset;
3969     afs_int32 start, end;
3970
3971     /* We're doing this to deal with the situation where we extend
3972      * by writing after lseek()ing past the end of the file . If that
3973      * extension skips chunks, then those chunks won't be created, and
3974      * GetDCache will assume that they have to be fetched from the server.
3975      * So, for each chunk between the current file position, and the new
3976      * length we GetDCache for that chunk.
3977      */
3978
3979     if (AFS_CHUNK(apos) == 0 || apos <= avc->f.m.Length)
3980         return;
3981
3982     if (avc->f.m.Length == 0)
3983         start = 0;
3984     else
3985         start = AFS_CHUNK(avc->f.m.Length)+1;
3986
3987     end = AFS_CHUNK(apos);
3988
3989     while (start<end) {
3990         len = AFS_CHUNKTOSIZE(start);
3991         tdc = afs_GetDCache(avc, AFS_CHUNKTOBASE(start), areq, &offset, &len, 4);
3992         if (tdc)
3993             afs_PutDCache(tdc);
3994         start++;
3995     }
3996 }