afs: Avoid GetDCache panic on AllocDCache failure
[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[in] indexp  A pointer to the head of the dcache free list or discard
1125  *                    list (afs_freeDCList, or afs_discardDCList)
1126  *
1127  * @return A dcache from that list, or NULL if none could be retrieved.
1128  *
1129  * @pre afs_xdcache is write-locked
1130  */
1131 static struct dcache *
1132 afs_GetDSlotFromList(afs_int32 *indexp)
1133 {
1134     struct dcache *tdc;
1135
1136     for ( ; *indexp != NULLIDX; indexp = &afs_dvnextTbl[*indexp]) {
1137         tdc = afs_GetUnusedDSlot(*indexp);
1138         if (tdc) {
1139             osi_Assert(tdc->refCount == 1);
1140             ReleaseReadLock(&tdc->tlock);
1141             *indexp = afs_dvnextTbl[tdc->index];
1142             afs_dvnextTbl[tdc->index] = NULLIDX;
1143             return tdc;
1144         }
1145     }
1146     return NULL;
1147 }
1148
1149 /*!
1150  * Free the next element on the list of discarded cache elements.
1151  *
1152  * Returns -1 if we encountered an error preventing us from freeing a
1153  * discarded dcache, or 0 on success.
1154  */
1155 static int
1156 afs_FreeDiscardedDCache(void)
1157 {
1158     struct dcache *tdc;
1159     struct osi_file *tfile;
1160     afs_int32 size;
1161
1162     AFS_STATCNT(afs_FreeDiscardedDCache);
1163
1164     ObtainWriteLock(&afs_xdcache, 510);
1165     if (!afs_blocksDiscarded) {
1166         ReleaseWriteLock(&afs_xdcache);
1167         return 0;
1168     }
1169
1170     /*
1171      * Get an entry from the list of discarded cache elements
1172      */
1173     tdc = afs_GetDSlotFromList(&afs_discardDCList);
1174     if (!tdc) {
1175         ReleaseWriteLock(&afs_xdcache);
1176         return -1;
1177     }
1178
1179     afs_discardDCCount--;
1180     size = ((tdc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10;       /* round up */
1181     afs_blocksDiscarded -= size;
1182     afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1183     /* We can lock because we just took it off the free list */
1184     ObtainWriteLock(&tdc->lock, 626);
1185     ReleaseWriteLock(&afs_xdcache);
1186
1187     /*
1188      * Truncate the element to reclaim its space
1189      */
1190     tfile = afs_CFileOpen(&tdc->f.inode);
1191     osi_Assert(tfile);
1192     afs_CFileTruncate(tfile, 0);
1193     afs_CFileClose(tfile);
1194     afs_AdjustSize(tdc, 0);
1195     afs_DCMoveBucket(tdc, 0, 0);
1196
1197     /*
1198      * Free the element we just truncated
1199      */
1200     ObtainWriteLock(&afs_xdcache, 511);
1201     afs_indexFlags[tdc->index] &= ~IFDiscarded;
1202     afs_FreeDCache(tdc);
1203     tdc->f.states &= ~(DRO|DBackup|DRW);
1204     ReleaseWriteLock(&tdc->lock);
1205     afs_PutDCache(tdc);
1206     ReleaseWriteLock(&afs_xdcache);
1207
1208     return 0;
1209 }
1210
1211 /*!
1212  * Free as many entries from the list of discarded cache elements
1213  * as we need to get the free space down below CM_WAITFORDRAINPCT (98%).
1214  *
1215  * \return 0
1216  */
1217 int
1218 afs_MaybeFreeDiscardedDCache(void)
1219 {
1220
1221     AFS_STATCNT(afs_MaybeFreeDiscardedDCache);
1222
1223     while (afs_blocksDiscarded
1224            && (afs_blocksUsed >
1225                PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks))) {
1226         int code = afs_FreeDiscardedDCache();
1227         if (code) {
1228             /* Callers depend on us to get the afs_blocksDiscarded count down.
1229              * If we cannot do that, the callers can spin by calling us over
1230              * and over. Panic for now until we can figure out something
1231              * better. */
1232             osi_Panic("Error freeing discarded dcache");
1233         }
1234     }
1235     return 0;
1236 }
1237
1238 /*!
1239  * Try to free up a certain number of disk slots.
1240  *
1241  * \param anumber Targeted number of disk slots to free up.
1242  *
1243  * \note Environment:
1244  *      Must be called with afs_xdcache write-locked.
1245  *
1246  */
1247 static void
1248 afs_GetDownDSlot(int anumber)
1249 {
1250     struct afs_q *tq, *nq;
1251     struct dcache *tdc;
1252     int ix;
1253     unsigned int cnt;
1254
1255     AFS_STATCNT(afs_GetDownDSlot);
1256     if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
1257         osi_Panic("diskless getdowndslot");
1258
1259     if (CheckLock(&afs_xdcache) != -1)
1260         osi_Panic("getdowndslot nolock");
1261
1262     /* decrement anumber first for all dudes in free list */
1263     for (tdc = afs_freeDSList; tdc; tdc = (struct dcache *)tdc->lruq.next)
1264         anumber--;
1265     if (anumber <= 0)
1266         return;                 /* enough already free */
1267
1268     for (cnt = 0, tq = afs_DLRU.prev; tq != &afs_DLRU && anumber > 0;
1269          tq = nq, cnt++) {
1270         tdc = (struct dcache *)tq;      /* q is first elt in dcache entry */
1271         nq = QPrev(tq);         /* in case we remove it */
1272         if (tdc->refCount == 0) {
1273             if ((ix = tdc->index) == NULLIDX)
1274                 osi_Panic("getdowndslot");
1275             /* pull the entry out of the lruq and put it on the free list */
1276             QRemove(&tdc->lruq);
1277
1278             /* write-through if modified */
1279             if (tdc->dflags & DFEntryMod) {
1280 #if defined(AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
1281                 /*
1282                  * ask proxy to do this for us - we don't have the stack space
1283                  */
1284                 while (tdc->dflags & DFEntryMod) {
1285                     int s;
1286                     AFS_GUNLOCK();
1287                     s = SPLOCK(afs_sgibklock);
1288                     if (afs_sgibklist == NULL) {
1289                         /* if slot is free, grab it. */
1290                         afs_sgibklist = tdc;
1291                         SV_SIGNAL(&afs_sgibksync);
1292                     }
1293                     /* wait for daemon to (start, then) finish. */
1294                     SP_WAIT(afs_sgibklock, s, &afs_sgibkwait, PINOD);
1295                     AFS_GLOCK();
1296                 }
1297 #else
1298                 tdc->dflags &= ~DFEntryMod;
1299                 osi_Assert(afs_WriteDCache(tdc, 1) == 0);
1300 #endif
1301             }
1302
1303             /* finally put the entry in the free list */
1304             afs_indexTable[ix] = NULL;
1305             afs_indexFlags[ix] &= ~IFEverUsed;
1306             tdc->index = NULLIDX;
1307             tdc->lruq.next = (struct afs_q *)afs_freeDSList;
1308             afs_freeDSList = tdc;
1309             anumber--;
1310         }
1311     }
1312 }                               /*afs_GetDownDSlot */
1313
1314
1315 /*
1316  * afs_RefDCache
1317  *
1318  * Description:
1319  *      Increment the reference count on a disk cache entry,
1320  *      which already has a non-zero refcount.  In order to
1321  *      increment the refcount of a zero-reference entry, you
1322  *      have to hold afs_xdcache.
1323  *
1324  * Parameters:
1325  *      adc : Pointer to the dcache entry to increment.
1326  *
1327  * Environment:
1328  *      Nothing interesting.
1329  */
1330 int
1331 afs_RefDCache(struct dcache *adc)
1332 {
1333     ObtainWriteLock(&adc->tlock, 627);
1334     if (adc->refCount < 0)
1335         osi_Panic("RefDCache: negative refcount");
1336     adc->refCount++;
1337     ReleaseWriteLock(&adc->tlock);
1338     return 0;
1339 }
1340
1341
1342 /*
1343  * afs_PutDCache
1344  *
1345  * Description:
1346  *      Decrement the reference count on a disk cache entry.
1347  *
1348  * Parameters:
1349  *      ad : Ptr to the dcache entry to decrement.
1350  *
1351  * Environment:
1352  *      Nothing interesting.
1353  */
1354 int
1355 afs_PutDCache(struct dcache *adc)
1356 {
1357     AFS_STATCNT(afs_PutDCache);
1358     ObtainWriteLock(&adc->tlock, 276);
1359     if (adc->refCount <= 0)
1360         osi_Panic("putdcache");
1361     --adc->refCount;
1362     ReleaseWriteLock(&adc->tlock);
1363     return 0;
1364 }
1365
1366
1367 /*
1368  * afs_TryToSmush
1369  *
1370  * Description:
1371  *      Try to discard all data associated with this file from the
1372  *      cache.
1373  *
1374  * Parameters:
1375  *      avc : Pointer to the cache info for the file.
1376  *
1377  * Environment:
1378  *      Both pvnLock and lock are write held.
1379  */
1380 void
1381 afs_TryToSmush(struct vcache *avc, afs_ucred_t *acred, int sync)
1382 {
1383     struct dcache *tdc;
1384     int index;
1385     int i;
1386     AFS_STATCNT(afs_TryToSmush);
1387     afs_Trace2(afs_iclSetp, CM_TRACE_TRYTOSMUSH, ICL_TYPE_POINTER, avc,
1388                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
1389     sync = 1;                   /* XX Temp testing XX */
1390
1391 #if     defined(AFS_SUN5_ENV)
1392     ObtainWriteLock(&avc->vlock, 573);
1393     avc->activeV++;             /* block new getpages */
1394     ReleaseWriteLock(&avc->vlock);
1395 #endif
1396
1397     /* Flush VM pages */
1398     osi_VM_TryToSmush(avc, acred, sync);
1399
1400     /*
1401      * Get the hash chain containing all dce's for this fid
1402      */
1403     i = DVHash(&avc->f.fid);
1404     ObtainWriteLock(&afs_xdcache, 277);
1405     for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
1406         i = afs_dvnextTbl[index];       /* next pointer this hash table */
1407         if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
1408             int releaseTlock = 1;
1409             tdc = afs_GetValidDSlot(index);
1410             if (!tdc) {
1411                 /* afs_TryToSmush is best-effort; we may not actually discard
1412                  * everything, so failure to discard a dcache due to an i/o
1413                  * error is okay. */
1414                 continue;
1415             }
1416             if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
1417                 if (sync) {
1418                     if ((afs_indexFlags[index] & IFDataMod) == 0
1419                         && tdc->refCount == 1) {
1420                         ReleaseReadLock(&tdc->tlock);
1421                         releaseTlock = 0;
1422                         afs_FlushDCache(tdc);
1423                     }
1424                 } else
1425                     afs_indexTable[index] = 0;
1426             }
1427             if (releaseTlock)
1428                 ReleaseReadLock(&tdc->tlock);
1429             afs_PutDCache(tdc);
1430         }
1431     }
1432 #if     defined(AFS_SUN5_ENV)
1433     ObtainWriteLock(&avc->vlock, 545);
1434     if (--avc->activeV == 0 && (avc->vstates & VRevokeWait)) {
1435         avc->vstates &= ~VRevokeWait;
1436         afs_osi_Wakeup((char *)&avc->vstates);
1437     }
1438     ReleaseWriteLock(&avc->vlock);
1439 #endif
1440     ReleaseWriteLock(&afs_xdcache);
1441     /*
1442      * It's treated like a callback so that when we do lookups we'll
1443      * invalidate the unique bit if any
1444      * trytoSmush occured during the lookup call
1445      */
1446     afs_allCBs++;
1447 }
1448
1449 /*
1450  * afs_DCacheMissingChunks
1451  *
1452  * Description
1453  *      Given the cached info for a file, return the number of chunks that
1454  *      are not available from the dcache.
1455  *
1456  * Parameters:
1457  *      avc:    Pointer to the (held) vcache entry to look in.
1458  *
1459  * Returns:
1460  *      The number of chunks which are not currently cached.
1461  *
1462  * Environment:
1463  *      The vcache entry is held upon entry.
1464  */
1465
1466 int
1467 afs_DCacheMissingChunks(struct vcache *avc)
1468 {
1469     int i, index;
1470     afs_size_t totalLength = 0;
1471     afs_uint32 totalChunks = 0;
1472     struct dcache *tdc;
1473
1474     totalLength = avc->f.m.Length;
1475     if (avc->f.truncPos < totalLength)
1476         totalLength = avc->f.truncPos;
1477
1478     /* Length is 0, no chunk missing. */
1479     if (totalLength == 0)
1480         return 0;
1481
1482     /* If totalLength is a multiple of chunksize, the last byte appears
1483      * as being part of the next chunk, which does not exist.
1484      * Decrementing totalLength by one fixes that.
1485      */
1486     totalLength--;
1487     totalChunks = (AFS_CHUNK(totalLength) + 1);
1488
1489     /* If we're a directory, we only ever have one chunk, regardless of
1490      * the size of the dir.
1491      */
1492     if (avc->f.fid.Fid.Vnode & 1 || vType(avc) == VDIR)
1493         totalChunks = 1;
1494
1495     /*
1496      printf("Should have %d chunks for %u bytes\n",
1497                 totalChunks, (totalLength + 1));
1498     */
1499     i = DVHash(&avc->f.fid);
1500     ObtainWriteLock(&afs_xdcache, 1001);
1501     for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
1502         i = afs_dvnextTbl[index];
1503         if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
1504             tdc = afs_GetValidDSlot(index);
1505             if (tdc) {
1506                 if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
1507                     totalChunks--;
1508                 }
1509                 ReleaseReadLock(&tdc->tlock);
1510                 afs_PutDCache(tdc);
1511             }
1512         }
1513     }
1514     ReleaseWriteLock(&afs_xdcache);
1515
1516     /*printf("Missing %d chunks\n", totalChunks);*/
1517
1518     return (totalChunks);
1519 }
1520
1521 /*
1522  * afs_FindDCache
1523  *
1524  * Description:
1525  *      Given the cached info for a file and a byte offset into the
1526  *      file, make sure the dcache entry for that file and containing
1527  *      the given byte is available, returning it to our caller.
1528  *
1529  * Parameters:
1530  *      avc   : Pointer to the (held) vcache entry to look in.
1531  *      abyte : Which byte we want to get to.
1532  *
1533  * Returns:
1534  *      Pointer to the dcache entry covering the file & desired byte,
1535  *      or NULL if not found.
1536  *
1537  * Environment:
1538  *      The vcache entry is held upon entry.
1539  */
1540
1541 struct dcache *
1542 afs_FindDCache(struct vcache *avc, afs_size_t abyte)
1543 {
1544     afs_int32 chunk;
1545     afs_int32 i, index;
1546     struct dcache *tdc = NULL;
1547
1548     AFS_STATCNT(afs_FindDCache);
1549     chunk = AFS_CHUNK(abyte);
1550
1551     /*
1552      * Hash on the [fid, chunk] and get the corresponding dcache index
1553      * after write-locking the dcache.
1554      */
1555     i = DCHash(&avc->f.fid, chunk);
1556     ObtainWriteLock(&afs_xdcache, 278);
1557     for (index = afs_dchashTbl[i]; index != NULLIDX; index = afs_dcnextTbl[index]) {
1558         if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
1559             tdc = afs_GetValidDSlot(index);
1560             if (!tdc) {
1561                 /* afs_FindDCache is best-effort; we may not find the given
1562                  * file/offset, so if we cannot find the given dcache due to
1563                  * i/o errors, that is okay. */
1564                 continue;
1565             }
1566             ReleaseReadLock(&tdc->tlock);
1567             if (!FidCmp(&tdc->f.fid, &avc->f.fid) && chunk == tdc->f.chunk) {
1568                 break;          /* leaving refCount high for caller */
1569             }
1570             afs_PutDCache(tdc);
1571         }
1572     }
1573     if (index != NULLIDX) {
1574         hset(afs_indexTimes[tdc->index], afs_indexCounter);
1575         hadd32(afs_indexCounter, 1);
1576         ReleaseWriteLock(&afs_xdcache);
1577         return tdc;
1578     }
1579     ReleaseWriteLock(&afs_xdcache);
1580     return NULL;
1581 }                               /*afs_FindDCache */
1582
1583 /* only call these from afs_AllocDCache() */
1584 static struct dcache *
1585 afs_AllocFreeDSlot(void)
1586 {
1587     struct dcache *tdc;
1588
1589     tdc = afs_GetDSlotFromList(&afs_freeDCList);
1590     if (!tdc) {
1591         return NULL;
1592     }
1593     afs_indexFlags[tdc->index] &= ~IFFree;
1594     ObtainWriteLock(&tdc->lock, 604);
1595     afs_freeDCCount--;
1596
1597     return tdc;
1598 }
1599 static struct dcache *
1600 afs_AllocDiscardDSlot(afs_int32 lock)
1601 {
1602     struct dcache *tdc;
1603     afs_uint32 size = 0;
1604     struct osi_file *file;
1605
1606     tdc = afs_GetDSlotFromList(&afs_discardDCList);
1607     if (!tdc) {
1608         return NULL;
1609     }
1610     afs_indexFlags[tdc->index] &= ~IFDiscarded;
1611     ObtainWriteLock(&tdc->lock, 605);
1612     afs_discardDCCount--;
1613     size =
1614         ((tdc->f.chunkBytes +
1615           afs_fsfragsize) ^ afs_fsfragsize) >> 10;
1616     tdc->f.states &= ~(DRO|DBackup|DRW);
1617     afs_DCMoveBucket(tdc, size, 0);
1618     afs_blocksDiscarded -= size;
1619     afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1620     if ((lock & 2)) {
1621         /* Truncate the chunk so zeroes get filled properly */
1622         file = afs_CFileOpen(&tdc->f.inode);
1623         osi_Assert(file);
1624         afs_CFileTruncate(file, 0);
1625         afs_CFileClose(file);
1626         afs_AdjustSize(tdc, 0);
1627     }
1628
1629     return tdc;
1630 }
1631
1632 /*!
1633  * Get a fresh dcache from the free or discarded list.
1634  *
1635  * \param avc Who's dcache is this going to be?
1636  * \param chunk The position where it will be placed in.
1637  * \param lock How are locks held.
1638  * \param ashFid If this dcache going to be used for a shadow dir,
1639  *              this is it's fid.
1640  *
1641  * \note Required locks:
1642  *      - afs_xdcache (W)
1643  *      - avc (R if (lock & 1) set and W otherwise)
1644  * \note It write locks the new dcache. The caller must unlock it.
1645  *
1646  * \return The new dcache.
1647  */
1648 struct dcache *
1649 afs_AllocDCache(struct vcache *avc, afs_int32 chunk, afs_int32 lock,
1650                 struct VenusFid *ashFid)
1651 {
1652     struct dcache *tdc = NULL;
1653
1654     /* if (lock & 2), prefer 'free' dcaches; otherwise, prefer 'discard'
1655      * dcaches. In either case, try both if our first choice doesn't work. */
1656     if ((lock & 2)) {
1657         tdc = afs_AllocFreeDSlot();
1658         if (!tdc) {
1659             tdc = afs_AllocDiscardDSlot(lock);
1660         }
1661     } else {
1662         tdc = afs_AllocDiscardDSlot(lock);
1663         if (!tdc) {
1664             tdc = afs_AllocFreeDSlot();
1665         }
1666     }
1667     if (!tdc) {
1668         return NULL;
1669     }
1670
1671     /*
1672      * Locks held:
1673      * avc->lock(R) if setLocks
1674      * avc->lock(W) if !setLocks
1675      * tdc->lock(W)
1676      * afs_xdcache(W)
1677      */
1678
1679     /*
1680      * Fill in the newly-allocated dcache record.
1681      */
1682     afs_indexFlags[tdc->index] &= ~(IFDirtyPages | IFAnyPages);
1683     if (ashFid)
1684         /* Use shadow fid if provided. */
1685         tdc->f.fid = *ashFid;
1686     else
1687         /* Use normal vcache's fid otherwise. */
1688         tdc->f.fid = avc->f.fid;
1689     if (avc->f.states & CRO)
1690         tdc->f.states = DRO;
1691     else if (avc->f.states & CBackup)
1692         tdc->f.states = DBackup;
1693     else
1694         tdc->f.states = DRW;
1695     afs_DCMoveBucket(tdc, 0, afs_DCGetBucket(avc));
1696     afs_indexUnique[tdc->index] = tdc->f.fid.Fid.Unique;
1697     if (!ashFid)
1698         hones(tdc->f.versionNo);        /* invalid value */
1699     tdc->f.chunk = chunk;
1700     tdc->validPos = AFS_CHUNKTOBASE(chunk);
1701     /* XXX */
1702     if (tdc->lruq.prev == &tdc->lruq)
1703         osi_Panic("lruq 1");
1704
1705     return tdc;
1706 }
1707
1708 /*
1709  * afs_GetDCache
1710  *
1711  * Description:
1712  *      This function is called to obtain a reference to data stored in
1713  *      the disk cache, locating a chunk of data containing the desired
1714  *      byte and returning a reference to the disk cache entry, with its
1715  *      reference count incremented.
1716  *
1717  * Parameters:
1718  * IN:
1719  *      avc     : Ptr to a vcache entry (unlocked)
1720  *      abyte   : Byte position in the file desired
1721  *      areq    : Request structure identifying the requesting user.
1722  *      aflags  : Settings as follows:
1723  *                      1 : Set locks
1724  *                      2 : Return after creating entry.
1725  *                      4 : called from afs_vnop_write.c
1726  *                          *alen contains length of data to be written.
1727  * OUT:
1728  *      aoffset : Set to the offset within the chunk where the resident
1729  *                byte is located.
1730  *      alen    : Set to the number of bytes of data after the desired
1731  *                byte (including the byte itself) which can be read
1732  *                from this chunk.
1733  *
1734  * Environment:
1735  *      The vcache entry pointed to by avc is unlocked upon entry.
1736  */
1737
1738 /*
1739  * Update the vnode-to-dcache hint if we can get the vnode lock
1740  * right away.  Assumes dcache entry is at least read-locked.
1741  */
1742 void
1743 updateV2DC(int lockVc, struct vcache *v, struct dcache *d, int src)
1744 {
1745     if (!lockVc || 0 == NBObtainWriteLock(&v->lock, src)) {
1746         if (hsame(v->f.m.DataVersion, d->f.versionNo) && v->callback)
1747             v->dchint = d;
1748         if (lockVc)
1749             ReleaseWriteLock(&v->lock);
1750     }
1751 }
1752
1753 /* avc - Write-locked unless aflags & 1 */
1754 struct dcache *
1755 afs_GetDCache(struct vcache *avc, afs_size_t abyte,
1756               struct vrequest *areq, afs_size_t * aoffset,
1757               afs_size_t * alen, int aflags)
1758 {
1759     afs_int32 i, code, shortcut;
1760 #if     defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
1761     afs_int32 adjustsize = 0;
1762 #endif
1763     int setLocks;
1764     afs_int32 index;
1765     afs_int32 us;
1766     afs_int32 chunk;
1767     afs_size_t Position = 0;
1768     afs_int32 size, tlen;       /* size of segment to transfer */
1769     struct afs_FetchOutput *tsmall = 0;
1770     struct dcache *tdc;
1771     struct osi_file *file;
1772     struct afs_conn *tc;
1773     int downDCount = 0;
1774     struct server *newCallback = NULL;
1775     char setNewCallback;
1776     char setVcacheStatus;
1777     char doVcacheUpdate;
1778     char slowPass = 0;
1779     int doAdjustSize = 0;
1780     int doReallyAdjustSize = 0;
1781     int overWriteWholeChunk = 0;
1782     struct rx_connection *rxconn;
1783
1784 #ifndef AFS_NOSTATS
1785     struct afs_stats_AccessInfo *accP;  /*Ptr to access record in stats */
1786     int fromReplica;            /*Are we reading from a replica? */
1787     int numFetchLoops;          /*# times around the fetch/analyze loop */
1788 #endif /* AFS_NOSTATS */
1789
1790     AFS_STATCNT(afs_GetDCache);
1791     if (dcacheDisabled)
1792         return NULL;
1793
1794     setLocks = aflags & 1;
1795
1796     /*
1797      * Determine the chunk number and offset within the chunk corresponding
1798      * to the desired byte.
1799      */
1800     if (avc->f.fid.Fid.Vnode & 1) {     /* if (vType(avc) == VDIR) */
1801         chunk = 0;
1802     } else {
1803         chunk = AFS_CHUNK(abyte);
1804     }
1805
1806     /* come back to here if we waited for the cache to drain. */
1807   RetryGetDCache:
1808
1809     setNewCallback = setVcacheStatus = 0;
1810
1811     if (setLocks) {
1812         if (slowPass)
1813             ObtainWriteLock(&avc->lock, 616);
1814         else
1815             ObtainReadLock(&avc->lock);
1816     }
1817
1818     /*
1819      * Locks held:
1820      * avc->lock(R) if setLocks && !slowPass
1821      * avc->lock(W) if !setLocks || slowPass
1822      */
1823
1824     shortcut = 0;
1825
1826     /* check hints first! (might could use bcmp or some such...) */
1827     if ((tdc = avc->dchint)) {
1828         int dcLocked;
1829
1830         /*
1831          * The locking order between afs_xdcache and dcache lock matters.
1832          * The hint dcache entry could be anywhere, even on the free list.
1833          * Locking afs_xdcache ensures that noone is trying to pull dcache
1834          * entries from the free list, and thereby assuming them to be not
1835          * referenced and not locked.
1836          */
1837         ObtainReadLock(&afs_xdcache);
1838         dcLocked = (0 == NBObtainSharedLock(&tdc->lock, 601));
1839
1840         if (dcLocked && (tdc->index != NULLIDX)
1841             && !FidCmp(&tdc->f.fid, &avc->f.fid) && chunk == tdc->f.chunk
1842             && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1843             /* got the right one.  It might not be the right version, and it
1844              * might be fetching, but it's the right dcache entry.
1845              */
1846             /* All this code should be integrated better with what follows:
1847              * I can save a good bit more time under a write lock if I do..
1848              */
1849             ObtainWriteLock(&tdc->tlock, 603);
1850             tdc->refCount++;
1851             ReleaseWriteLock(&tdc->tlock);
1852
1853             ReleaseReadLock(&afs_xdcache);
1854             shortcut = 1;
1855
1856             if (hsame(tdc->f.versionNo, avc->f.m.DataVersion)
1857                 && !(tdc->dflags & DFFetching)) {
1858
1859                 afs_stats_cmperf.dcacheHits++;
1860                 ObtainWriteLock(&afs_xdcache, 559);
1861                 QRemove(&tdc->lruq);
1862                 QAdd(&afs_DLRU, &tdc->lruq);
1863                 ReleaseWriteLock(&afs_xdcache);
1864
1865                 /* Locks held:
1866                  * avc->lock(R) if setLocks && !slowPass
1867                  * avc->lock(W) if !setLocks || slowPass
1868                  * tdc->lock(S)
1869                  */
1870                 goto done;
1871             }
1872         } else {
1873             if (dcLocked)
1874                 ReleaseSharedLock(&tdc->lock);
1875             ReleaseReadLock(&afs_xdcache);
1876         }
1877
1878         if (!shortcut)
1879             tdc = 0;
1880     }
1881
1882     /* Locks held:
1883      * avc->lock(R) if setLocks && !slowPass
1884      * avc->lock(W) if !setLocks || slowPass
1885      * tdc->lock(S) if tdc
1886      */
1887
1888     if (!tdc) {                 /* If the hint wasn't the right dcache entry */
1889         int dslot_error = 0;
1890         /*
1891          * Hash on the [fid, chunk] and get the corresponding dcache index
1892          * after write-locking the dcache.
1893          */
1894       RetryLookup:
1895
1896         /* Locks held:
1897          * avc->lock(R) if setLocks && !slowPass
1898          * avc->lock(W) if !setLocks || slowPass
1899          */
1900
1901         i = DCHash(&avc->f.fid, chunk);
1902         /* check to make sure our space is fine */
1903         afs_MaybeWakeupTruncateDaemon();
1904
1905         ObtainWriteLock(&afs_xdcache, 280);
1906         us = NULLIDX;
1907         for (index = afs_dchashTbl[i]; index != NULLIDX; us = index, index = afs_dcnextTbl[index]) {
1908             if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
1909                 tdc = afs_GetValidDSlot(index);
1910                 if (!tdc) {
1911                     /* we got an i/o error when trying to get the given dslot,
1912                      * but do not bail out just yet; it is possible the dcache
1913                      * we're looking for is elsewhere, so it doesn't matter if
1914                      * we can't load this one. */
1915                     dslot_error = 1;
1916                     continue;
1917                 }
1918                 ReleaseReadLock(&tdc->tlock);
1919                 /*
1920                  * Locks held:
1921                  * avc->lock(R) if setLocks && !slowPass
1922                  * avc->lock(W) if !setLocks || slowPass
1923                  * afs_xdcache(W)
1924                  */
1925                 if (!FidCmp(&tdc->f.fid, &avc->f.fid) && chunk == tdc->f.chunk) {
1926                     /* Move it up in the beginning of the list */
1927                     if (afs_dchashTbl[i] != index) {
1928                         afs_dcnextTbl[us] = afs_dcnextTbl[index];
1929                         afs_dcnextTbl[index] = afs_dchashTbl[i];
1930                         afs_dchashTbl[i] = index;
1931                     }
1932                     ReleaseWriteLock(&afs_xdcache);
1933                     ObtainSharedLock(&tdc->lock, 606);
1934                     break;      /* leaving refCount high for caller */
1935                 }
1936                 afs_PutDCache(tdc);
1937                 tdc = 0;
1938             }
1939         }
1940
1941         /*
1942          * If we didn't find the entry, we'll create one.
1943          */
1944         if (index == NULLIDX) {
1945             /*
1946              * Locks held:
1947              * avc->lock(R) if setLocks
1948              * avc->lock(W) if !setLocks
1949              * afs_xdcache(W)
1950              */
1951             afs_Trace2(afs_iclSetp, CM_TRACE_GETDCACHE1, ICL_TYPE_POINTER,
1952                        avc, ICL_TYPE_INT32, chunk);
1953
1954             if (dslot_error) {
1955                 /* We couldn't find the dcache we want, but we hit some i/o
1956                  * errors when trying to find it, so we're not sure if the
1957                  * dcache we want is in the cache or not. Error out, so we
1958                  * don't try to possibly create 2 separate dcaches for the
1959                  * same exact data. */
1960                 ReleaseWriteLock(&afs_xdcache);
1961                 goto done;
1962             }
1963
1964             if (afs_discardDCList == NULLIDX && afs_freeDCList == NULLIDX) {
1965                 if (!setLocks)
1966                     avc->f.states |= CDCLock;
1967                 /* just need slots */
1968                 afs_GetDownD(5, (int *)0, afs_DCGetBucket(avc));
1969                 if (!setLocks)
1970                     avc->f.states &= ~CDCLock;
1971             }
1972             tdc = afs_AllocDCache(avc, chunk, aflags, NULL);
1973             if (!tdc) {
1974                 ReleaseWriteLock(&afs_xdcache);
1975
1976                 /* If we can't get space for 5 mins we give up and bail out */
1977                 if (++downDCount > 300) {
1978                     afs_warn("afs: Unable to get free cache space for file "
1979                              "%u:%u.%u.%u for 5 minutes; failing with an i/o error\n",
1980                              avc->f.fid.Cell,
1981                              avc->f.fid.Fid.Volume,
1982                              avc->f.fid.Fid.Vnode,
1983                              avc->f.fid.Fid.Unique);
1984                     goto done;
1985                 }
1986
1987                 /*
1988                  * Locks held:
1989                  * avc->lock(R) if setLocks
1990                  * avc->lock(W) if !setLocks
1991                  */
1992                 afs_osi_Wait(1000, 0, 0);
1993                 goto RetryLookup;
1994             }
1995
1996             /*
1997              * Locks held:
1998              * avc->lock(R) if setLocks
1999              * avc->lock(W) if !setLocks
2000              * tdc->lock(W)
2001              * afs_xdcache(W)
2002              */
2003
2004             /*
2005              * Now add to the two hash chains - note that i is still set
2006              * from the above DCHash call.
2007              */
2008             afs_dcnextTbl[tdc->index] = afs_dchashTbl[i];
2009             afs_dchashTbl[i] = tdc->index;
2010             i = DVHash(&avc->f.fid);
2011             afs_dvnextTbl[tdc->index] = afs_dvhashTbl[i];
2012             afs_dvhashTbl[i] = tdc->index;
2013             tdc->dflags = DFEntryMod;
2014             tdc->mflags = 0;
2015             afs_MaybeWakeupTruncateDaemon();
2016             ReleaseWriteLock(&afs_xdcache);
2017             ConvertWToSLock(&tdc->lock);
2018         }
2019     }
2020
2021
2022     /* vcache->dcache hint failed */
2023     /*
2024      * Locks held:
2025      * avc->lock(R) if setLocks && !slowPass
2026      * avc->lock(W) if !setLocks || slowPass
2027      * tdc->lock(S)
2028      */
2029     afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE2, ICL_TYPE_POINTER, avc,
2030                ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
2031                hgetlo(tdc->f.versionNo), ICL_TYPE_INT32,
2032                hgetlo(avc->f.m.DataVersion));
2033     /*
2034      * Here we have the entry in tdc, with its refCount incremented.
2035      * Note: we don't use the S-lock on avc; it costs concurrency when
2036      * storing a file back to the server.
2037      */
2038
2039     /*
2040      * Not a newly created file so we need to check the file's length and
2041      * compare data versions since someone could have changed the data or we're
2042      * reading a file written elsewhere. We only want to bypass doing no-op
2043      * read rpcs on newly created files (dv of 0) since only then we guarantee
2044      * that this chunk's data hasn't been filled by another client.
2045      */
2046     size = AFS_CHUNKSIZE(abyte);
2047     if (aflags & 4)             /* called from write */
2048         tlen = *alen;
2049     else                        /* called from read */
2050         tlen = tdc->validPos - abyte;
2051     Position = AFS_CHUNKTOBASE(chunk);
2052     afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE3, ICL_TYPE_INT32, tlen,
2053                ICL_TYPE_INT32, aflags, ICL_TYPE_OFFSET,
2054                ICL_HANDLE_OFFSET(abyte), ICL_TYPE_OFFSET,
2055                ICL_HANDLE_OFFSET(Position));
2056     if ((aflags & 4) && (hiszero(avc->f.m.DataVersion)))
2057         doAdjustSize = 1;
2058     if ((AFS_CHUNKTOBASE(chunk) >= avc->f.m.Length) ||
2059          ((aflags & 4) && (abyte == Position) && (tlen >= size)))
2060         overWriteWholeChunk = 1;
2061     if (doAdjustSize || overWriteWholeChunk) {
2062 #if     defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
2063 #ifdef  AFS_SGI_ENV
2064 #ifdef AFS_SGI64_ENV
2065         if (doAdjustSize)
2066             adjustsize = NBPP;
2067 #else /* AFS_SGI64_ENV */
2068         if (doAdjustSize)
2069             adjustsize = 8192;
2070 #endif /* AFS_SGI64_ENV */
2071 #else /* AFS_SGI_ENV */
2072         if (doAdjustSize)
2073             adjustsize = 4096;
2074 #endif /* AFS_SGI_ENV */
2075         if (AFS_CHUNKTOBASE(chunk) + adjustsize >= avc->f.m.Length &&
2076 #else /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
2077 #if     defined(AFS_SUN5_ENV)
2078         if ((doAdjustSize || (AFS_CHUNKTOBASE(chunk) >= avc->f.m.Length)) &&
2079 #else
2080         if (AFS_CHUNKTOBASE(chunk) >= avc->f.m.Length &&
2081 #endif
2082 #endif /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
2083             !hsame(avc->f.m.DataVersion, tdc->f.versionNo))
2084             doReallyAdjustSize = 1;
2085
2086         if (doReallyAdjustSize || overWriteWholeChunk) {
2087             /* no data in file to read at this position */
2088             UpgradeSToWLock(&tdc->lock, 607);
2089             file = afs_CFileOpen(&tdc->f.inode);
2090             osi_Assert(file);
2091             afs_CFileTruncate(file, 0);
2092             afs_CFileClose(file);
2093             afs_AdjustSize(tdc, 0);
2094             hset(tdc->f.versionNo, avc->f.m.DataVersion);
2095             tdc->dflags |= DFEntryMod;
2096
2097             ConvertWToSLock(&tdc->lock);
2098         }
2099     }
2100
2101     /*
2102      * We must read in the whole chunk if the version number doesn't
2103      * match.
2104      */
2105     if (aflags & 2) {
2106         /* don't need data, just a unique dcache entry */
2107         ObtainWriteLock(&afs_xdcache, 608);
2108         hset(afs_indexTimes[tdc->index], afs_indexCounter);
2109         hadd32(afs_indexCounter, 1);
2110         ReleaseWriteLock(&afs_xdcache);
2111
2112         updateV2DC(setLocks, avc, tdc, 553);
2113         if (vType(avc) == VDIR)
2114             *aoffset = abyte;
2115         else
2116             *aoffset = AFS_CHUNKOFFSET(abyte);
2117         if (tdc->validPos < abyte)
2118             *alen = (afs_size_t) 0;
2119         else
2120             *alen = tdc->validPos - abyte;
2121         ReleaseSharedLock(&tdc->lock);
2122         if (setLocks) {
2123             if (slowPass)
2124                 ReleaseWriteLock(&avc->lock);
2125             else
2126                 ReleaseReadLock(&avc->lock);
2127         }
2128         return tdc;             /* check if we're done */
2129     }
2130
2131     /*
2132      * Locks held:
2133      * avc->lock(R) if setLocks && !slowPass
2134      * avc->lock(W) if !setLocks || slowPass
2135      * tdc->lock(S)
2136      */
2137     osi_Assert((setLocks && !slowPass) || WriteLocked(&avc->lock));
2138
2139     setNewCallback = setVcacheStatus = 0;
2140
2141     /*
2142      * Locks held:
2143      * avc->lock(R) if setLocks && !slowPass
2144      * avc->lock(W) if !setLocks || slowPass
2145      * tdc->lock(S)
2146      */
2147     if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) && !overWriteWholeChunk) {
2148         /*
2149          * Version number mismatch.
2150          */
2151         /*
2152          * If we are disconnected, then we can't do much of anything
2153          * because the data doesn't match the file.
2154          */
2155         if (AFS_IS_DISCONNECTED) {
2156             ReleaseSharedLock(&tdc->lock);
2157             if (setLocks) {
2158                 if (slowPass)
2159                     ReleaseWriteLock(&avc->lock);
2160                 else
2161                     ReleaseReadLock(&avc->lock);
2162             }
2163             /* Flush the Dcache */
2164             afs_PutDCache(tdc);
2165
2166             return NULL;
2167         }
2168         UpgradeSToWLock(&tdc->lock, 609);
2169
2170         /*
2171          * If data ever existed for this vnode, and this is a text object,
2172          * do some clearing.  Now, you'd think you need only do the flush
2173          * when VTEXT is on, but VTEXT is turned off when the text object
2174          * is freed, while pages are left lying around in memory marked
2175          * with this vnode.  If we would reactivate (create a new text
2176          * object from) this vnode, we could easily stumble upon some of
2177          * these old pages in pagein.  So, we always flush these guys.
2178          * Sun has a wonderful lack of useful invariants in this system.
2179          *
2180          * avc->flushDV is the data version # of the file at the last text
2181          * flush.  Clearly, at least, we don't have to flush the file more
2182          * often than it changes
2183          */
2184         if (hcmp(avc->flushDV, avc->f.m.DataVersion) < 0) {
2185             /*
2186              * By here, the cache entry is always write-locked.  We can
2187              * deadlock if we call osi_Flush with the cache entry locked...
2188              * Unlock the dcache too.
2189              */
2190             ReleaseWriteLock(&tdc->lock);
2191             if (setLocks && !slowPass)
2192                 ReleaseReadLock(&avc->lock);
2193             else
2194                 ReleaseWriteLock(&avc->lock);
2195
2196             osi_FlushText(avc);
2197             /*
2198              * Call osi_FlushPages in open, read/write, and map, since it
2199              * is too hard here to figure out if we should lock the
2200              * pvnLock.
2201              */
2202             if (setLocks && !slowPass)
2203                 ObtainReadLock(&avc->lock);
2204             else
2205                 ObtainWriteLock(&avc->lock, 66);
2206             ObtainWriteLock(&tdc->lock, 610);
2207         }
2208
2209         /*
2210          * Locks held:
2211          * avc->lock(R) if setLocks && !slowPass
2212          * avc->lock(W) if !setLocks || slowPass
2213          * tdc->lock(W)
2214          */
2215
2216         /* Watch for standard race condition around osi_FlushText */
2217         if (hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
2218             updateV2DC(setLocks, avc, tdc, 569);        /* set hint */
2219             afs_stats_cmperf.dcacheHits++;
2220             ConvertWToSLock(&tdc->lock);
2221             goto done;
2222         }
2223
2224         /* Sleep here when cache needs to be drained. */
2225         if (setLocks && !slowPass
2226             && (afs_blocksUsed >
2227                 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks))) {
2228             /* Make sure truncate daemon is running */
2229             afs_MaybeWakeupTruncateDaemon();
2230             ObtainWriteLock(&tdc->tlock, 614);
2231             tdc->refCount--;    /* we'll re-obtain the dcache when we re-try. */
2232             ReleaseWriteLock(&tdc->tlock);
2233             ReleaseWriteLock(&tdc->lock);
2234             ReleaseReadLock(&avc->lock);
2235             while ((afs_blocksUsed - afs_blocksDiscarded) >
2236                    PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
2237                 afs_WaitForCacheDrain = 1;
2238                 afs_osi_Sleep(&afs_WaitForCacheDrain);
2239             }
2240             afs_MaybeFreeDiscardedDCache();
2241             /* need to check if someone else got the chunk first. */
2242             goto RetryGetDCache;
2243         }
2244
2245         Position = AFS_CHUNKBASE(abyte);
2246         if (vType(avc) == VDIR) {
2247             size = avc->f.m.Length;
2248             if (size > tdc->f.chunkBytes) {
2249                 /* pre-reserve space for file */
2250                 afs_AdjustSize(tdc, size);
2251             }
2252             size = 999999999;   /* max size for transfer */
2253         } else {
2254             afs_size_t maxGoodLength;
2255
2256             /* estimate how much data we're expecting back from the server,
2257              * and reserve space in the dcache entry for it */
2258
2259             maxGoodLength = avc->f.m.Length;
2260             if (avc->f.truncPos < maxGoodLength)
2261                 maxGoodLength = avc->f.truncPos;
2262
2263             size = AFS_CHUNKSIZE(abyte);        /* expected max size */
2264             if (Position > maxGoodLength) { /* If we're beyond EOF */
2265                 size = 0;
2266             } else if (Position + size > maxGoodLength) {
2267                 size = maxGoodLength - Position;
2268             }
2269             osi_Assert(size >= 0);
2270
2271             if (size > tdc->f.chunkBytes) {
2272                 /* pre-reserve estimated space for file */
2273                 afs_AdjustSize(tdc, size);      /* changes chunkBytes */
2274             }
2275
2276             if (size) {
2277                 /* For the actual fetch, do not limit the request to the
2278                  * length of the file. If this results in a read past EOF on
2279                  * the server, the server will just reply with less data than
2280                  * requested. If we limit ourselves to only requesting data up
2281                  * to the avc file length, we open ourselves up to races if the
2282                  * file is extended on the server at about the same time.
2283                  *
2284                  * However, we must restrict ourselves to the avc->f.truncPos
2285                  * length, since this represents an outstanding local
2286                  * truncation of the file that will be committed to the
2287                  * fileserver when we actually write the fileserver contents.
2288                  * If we do not restrict the fetch length based on
2289                  * avc->f.truncPos, a different truncate operation extending
2290                  * the file length could cause the old data after
2291                  * avc->f.truncPos to reappear, instead of extending the file
2292                  * with NUL bytes. */
2293                 size = AFS_CHUNKSIZE(abyte);
2294                 if (Position > avc->f.truncPos) {
2295                     size = 0;
2296                 } else if (Position + size > avc->f.truncPos) {
2297                     size = avc->f.truncPos - Position;
2298                 }
2299                 osi_Assert(size >= 0);
2300             }
2301         }
2302         if (afs_mariner && !tdc->f.chunk)
2303             afs_MarinerLog("fetch$Fetching", avc);      /* , Position, size, afs_indexCounter ); */
2304         /*
2305          * Right now, we only have one tool, and it's a hammer.  So, we
2306          * fetch the whole file.
2307          */
2308         DZap(tdc);      /* pages in cache may be old */
2309         file = afs_CFileOpen(&tdc->f.inode);
2310         if (!file) {
2311             /* We can't access the file in the disk cache backing this dcache;
2312              * bail out. */
2313             ReleaseWriteLock(&tdc->lock);
2314             afs_PutDCache(tdc);
2315             tdc = NULL;
2316             goto done;
2317         }
2318         afs_RemoveVCB(&avc->f.fid);
2319         tdc->f.states |= DWriting;
2320         tdc->dflags |= DFFetching;
2321         tdc->validPos = Position;       /*  which is AFS_CHUNKBASE(abyte) */
2322         if (tdc->mflags & DFFetchReq) {
2323             tdc->mflags &= ~DFFetchReq;
2324             if (afs_osi_Wakeup(&tdc->validPos) == 0)
2325                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
2326                            __FILE__, ICL_TYPE_INT32, __LINE__,
2327                            ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
2328                            tdc->dflags);
2329         }
2330         tsmall = osi_AllocLargeSpace(sizeof(struct afs_FetchOutput));
2331         setVcacheStatus = 0;
2332 #ifndef AFS_NOSTATS
2333         /*
2334          * Remember if we are doing the reading from a replicated volume,
2335          * and how many times we've zipped around the fetch/analyze loop.
2336          */
2337         fromReplica = (avc->f.states & CRO) ? 1 : 0;
2338         numFetchLoops = 0;
2339         accP = &(afs_stats_cmfullperf.accessinf);
2340         if (fromReplica)
2341             (accP->replicatedRefs)++;
2342         else
2343             (accP->unreplicatedRefs)++;
2344 #endif /* AFS_NOSTATS */
2345         /* this is a cache miss */
2346         afs_Trace4(afs_iclSetp, CM_TRACE_FETCHPROC, ICL_TYPE_POINTER, avc,
2347                    ICL_TYPE_FID, &(avc->f.fid), ICL_TYPE_OFFSET,
2348                    ICL_HANDLE_OFFSET(Position), ICL_TYPE_INT32, size);
2349
2350         if (size)
2351             afs_stats_cmperf.dcacheMisses++;
2352         code = 0;
2353         /*
2354          * Dynamic root support:  fetch data from local memory.
2355          */
2356         if (afs_IsDynroot(avc)) {
2357             char *dynrootDir;
2358             int dynrootLen;
2359
2360             afs_GetDynroot(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2361
2362             dynrootDir += Position;
2363             dynrootLen -= Position;
2364             if (size > dynrootLen)
2365                 size = dynrootLen;
2366             if (size < 0)
2367                 size = 0;
2368             code = afs_CFileWrite(file, 0, dynrootDir, size);
2369             afs_PutDynroot();
2370
2371             if (code == size)
2372                 code = 0;
2373             else
2374                 code = -1;
2375
2376             tdc->validPos = Position + size;
2377             afs_CFileTruncate(file, size);      /* prune it */
2378         } else if (afs_IsDynrootMount(avc)) {
2379             char *dynrootDir;
2380             int dynrootLen;
2381
2382             afs_GetDynrootMount(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2383
2384             dynrootDir += Position;
2385             dynrootLen -= Position;
2386             if (size > dynrootLen)
2387                 size = dynrootLen;
2388             if (size < 0)
2389                 size = 0;
2390             code = afs_CFileWrite(file, 0, dynrootDir, size);
2391             afs_PutDynroot();
2392
2393             if (code == size)
2394                 code = 0;
2395             else
2396                 code = -1;
2397
2398             tdc->validPos = Position + size;
2399             afs_CFileTruncate(file, size);      /* prune it */
2400         } else
2401             /*
2402              * Not a dynamic vnode:  do the real fetch.
2403              */
2404             do {
2405                 /*
2406                  * Locks held:
2407                  * avc->lock(R) if setLocks && !slowPass
2408                  * avc->lock(W) if !setLocks || slowPass
2409                  * tdc->lock(W)
2410                  */
2411
2412                 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK, &rxconn);
2413                 if (tc) {
2414 #ifndef AFS_NOSTATS
2415                     numFetchLoops++;
2416                     if (fromReplica)
2417                         (accP->numReplicasAccessed)++;
2418
2419 #endif /* AFS_NOSTATS */
2420                     if (!setLocks || slowPass) {
2421                         avc->callback = tc->parent->srvr->server;
2422                     } else {
2423                         newCallback = tc->parent->srvr->server;
2424                         setNewCallback = 1;
2425                     }
2426                     i = osi_Time();
2427                     code = afs_CacheFetchProc(tc, rxconn, file, Position, tdc,
2428                                                avc, size, tsmall);
2429                 } else
2430                    code = -1;
2431
2432                 if (code == 0) {
2433                     /* callback could have been broken (or expired) in a race here,
2434                      * but we return the data anyway.  It's as good as we knew about
2435                      * when we started. */
2436                     /*
2437                      * validPos is updated by CacheFetchProc, and can only be
2438                      * modifed under a dcache write lock, which we've blocked out
2439                      */
2440                     size = tdc->validPos - Position;    /* actual segment size */
2441                     if (size < 0)
2442                         size = 0;
2443                     afs_CFileTruncate(file, size);      /* prune it */
2444                 } else {
2445                     if (!setLocks || slowPass) {
2446                         afs_StaleVCacheFlags(avc, AFS_STALEVC_CLEARCB, CUnique);
2447                     } else {
2448                         /* Something lost.  Forget about performance, and go
2449                          * back with a vcache write lock.
2450                          */
2451                         afs_CFileTruncate(file, 0);
2452                         afs_AdjustSize(tdc, 0);
2453                         afs_CFileClose(file);
2454                         osi_FreeLargeSpace(tsmall);
2455                         tsmall = 0;
2456                         ReleaseWriteLock(&tdc->lock);
2457                         afs_PutDCache(tdc);
2458                         tdc = 0;
2459                         ReleaseReadLock(&avc->lock);
2460
2461                         if (tc) {
2462                             /* If we have a connection, we must put it back,
2463                              * since afs_Analyze will not be called here. */
2464                             afs_PutConn(tc, rxconn, SHARED_LOCK);
2465                         }
2466
2467                         slowPass = 1;
2468                         goto RetryGetDCache;
2469                     }
2470                 }
2471
2472             } while (afs_Analyze
2473                      (tc, rxconn, code, &avc->f.fid, areq,
2474                       AFS_STATS_FS_RPCIDX_FETCHDATA, SHARED_LOCK, NULL));
2475
2476         /*
2477          * Locks held:
2478          * avc->lock(R) if setLocks && !slowPass
2479          * avc->lock(W) if !setLocks || slowPass
2480          * tdc->lock(W)
2481          */
2482
2483 #ifndef AFS_NOSTATS
2484         /*
2485          * In the case of replicated access, jot down info on the number of
2486          * attempts it took before we got through or gave up.
2487          */
2488         if (fromReplica) {
2489             if (numFetchLoops <= 1)
2490                 (accP->refFirstReplicaOK)++;
2491             if (numFetchLoops > accP->maxReplicasPerRef)
2492                 accP->maxReplicasPerRef = numFetchLoops;
2493         }
2494 #endif /* AFS_NOSTATS */
2495
2496         tdc->dflags &= ~DFFetching;
2497         if (afs_osi_Wakeup(&tdc->validPos) == 0)
2498             afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
2499                        __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
2500                        tdc, ICL_TYPE_INT32, tdc->dflags);
2501         if (avc->execsOrWriters == 0)
2502             tdc->f.states &= ~DWriting;
2503
2504         /* now, if code != 0, we have an error and should punt.
2505          * note that we have the vcache write lock, either because
2506          * !setLocks or slowPass.
2507          */
2508         if (code) {
2509             afs_CFileTruncate(file, 0);
2510             afs_AdjustSize(tdc, 0);
2511             afs_CFileClose(file);
2512             ZapDCE(tdc);        /* sets DFEntryMod */
2513             if (vType(avc) == VDIR) {
2514                 DZap(tdc);
2515             }
2516             tdc->f.states &= ~(DRO|DBackup|DRW);
2517             afs_DCMoveBucket(tdc, 0, 0);
2518             ReleaseWriteLock(&tdc->lock);
2519             afs_PutDCache(tdc);
2520             if (!afs_IsDynroot(avc)) {
2521                 afs_StaleVCacheFlags(avc, 0, CUnique);
2522                 /*
2523                  * Locks held:
2524                  * avc->lock(W); assert(!setLocks || slowPass)
2525                  */
2526                 osi_Assert(!setLocks || slowPass);
2527             }
2528             tdc = NULL;
2529             goto done;
2530         }
2531
2532         /* otherwise we copy in the just-fetched info */
2533         afs_CFileClose(file);
2534         afs_AdjustSize(tdc, size);      /* new size */
2535         /*
2536          * Copy appropriate fields into vcache.  Status is
2537          * copied later where we selectively acquire the
2538          * vcache write lock.
2539          */
2540         if (slowPass)
2541             afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2542         else
2543             setVcacheStatus = 1;
2544         hset64(tdc->f.versionNo, tsmall->OutStatus.dataVersionHigh,
2545                tsmall->OutStatus.DataVersion);
2546         tdc->dflags |= DFEntryMod;
2547         afs_indexFlags[tdc->index] |= IFEverUsed;
2548         ConvertWToSLock(&tdc->lock);
2549     } /*Data version numbers don't match */
2550     else {
2551         /*
2552          * Data version numbers match.
2553          */
2554         afs_stats_cmperf.dcacheHits++;
2555     }                           /*Data version numbers match */
2556
2557     updateV2DC(setLocks, avc, tdc, 335);        /* set hint */
2558   done:
2559     /*
2560      * Locks held:
2561      * avc->lock(R) if setLocks && !slowPass
2562      * avc->lock(W) if !setLocks || slowPass
2563      * tdc->lock(S) if tdc
2564      */
2565
2566     /*
2567      * See if this was a reference to a file in the local cell.
2568      */
2569     if (afs_IsPrimaryCellNum(avc->f.fid.Cell))
2570         afs_stats_cmperf.dlocalAccesses++;
2571     else
2572         afs_stats_cmperf.dremoteAccesses++;
2573
2574     /* Fix up LRU info */
2575
2576     if (tdc) {
2577         ObtainWriteLock(&afs_xdcache, 602);
2578         hset(afs_indexTimes[tdc->index], afs_indexCounter);
2579         hadd32(afs_indexCounter, 1);
2580         ReleaseWriteLock(&afs_xdcache);
2581
2582         /* return the data */
2583         if (vType(avc) == VDIR)
2584             *aoffset = abyte;
2585         else
2586             *aoffset = AFS_CHUNKOFFSET(abyte);
2587         *alen = (tdc->f.chunkBytes - *aoffset);
2588         ReleaseSharedLock(&tdc->lock);
2589     }
2590
2591     /*
2592      * Locks held:
2593      * avc->lock(R) if setLocks && !slowPass
2594      * avc->lock(W) if !setLocks || slowPass
2595      */
2596
2597     /* Fix up the callback and status values in the vcache */
2598     doVcacheUpdate = 0;
2599     if (setLocks && !slowPass) {
2600         /* DCLOCKXXX
2601          *
2602          * This is our dirty little secret to parallel fetches.
2603          * We don't write-lock the vcache while doing the fetch,
2604          * but potentially we'll need to update the vcache after
2605          * the fetch is done.
2606          *
2607          * Drop the read lock and try to re-obtain the write
2608          * lock.  If the vcache still has the same DV, it's
2609          * ok to go ahead and install the new data.
2610          */
2611         afs_hyper_t currentDV, statusDV;
2612
2613         hset(currentDV, avc->f.m.DataVersion);
2614
2615         if (setNewCallback && avc->callback != newCallback)
2616             doVcacheUpdate = 1;
2617
2618         if (tsmall) {
2619             hset64(statusDV, tsmall->OutStatus.dataVersionHigh,
2620                    tsmall->OutStatus.DataVersion);
2621
2622             if (setVcacheStatus && avc->f.m.Length != tsmall->OutStatus.Length)
2623                 doVcacheUpdate = 1;
2624             if (setVcacheStatus && !hsame(currentDV, statusDV))
2625                 doVcacheUpdate = 1;
2626         }
2627
2628         ReleaseReadLock(&avc->lock);
2629
2630         if (doVcacheUpdate) {
2631             ObtainWriteLock(&avc->lock, 615);
2632             if (!hsame(avc->f.m.DataVersion, currentDV)) {
2633                 /* We lose.  Someone will beat us to it. */
2634                 doVcacheUpdate = 0;
2635                 ReleaseWriteLock(&avc->lock);
2636             }
2637         }
2638     }
2639
2640     /* With slow pass, we've already done all the updates */
2641     if (slowPass) {
2642         ReleaseWriteLock(&avc->lock);
2643     }
2644
2645     /* Check if we need to perform any last-minute fixes with a write-lock */
2646     if (!setLocks || doVcacheUpdate) {
2647         if (setNewCallback)
2648             avc->callback = newCallback;
2649         if (tsmall && setVcacheStatus)
2650             afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2651         if (setLocks)
2652             ReleaseWriteLock(&avc->lock);
2653     }
2654
2655     if (tsmall)
2656         osi_FreeLargeSpace(tsmall);
2657
2658     return tdc;
2659 }                               /*afs_GetDCache */
2660
2661
2662 /*
2663  * afs_WriteThroughDSlots
2664  *
2665  * Description:
2666  *      Sweep through the dcache slots and write out any modified
2667  *      in-memory data back on to our caching store.
2668  *
2669  * Parameters:
2670  *      None.
2671  *
2672  * Environment:
2673  *      The afs_xdcache is write-locked through this whole affair.
2674  */
2675 int
2676 afs_WriteThroughDSlots(void)
2677 {
2678     struct dcache *tdc;
2679     afs_int32 i, touchedit = 0;
2680     int code = 0;
2681
2682     struct afs_q DirtyQ, *tq;
2683
2684     AFS_STATCNT(afs_WriteThroughDSlots);
2685
2686     /*
2687      * Because of lock ordering, we can't grab dcache locks while
2688      * holding afs_xdcache.  So we enter xdcache, get a reference
2689      * for every dcache entry, and exit xdcache.
2690      */
2691     ObtainWriteLock(&afs_xdcache, 283);
2692     QInit(&DirtyQ);
2693     for (i = 0; i < afs_cacheFiles; i++) {
2694         tdc = afs_indexTable[i];
2695
2696         /* Grab tlock in case the existing refcount isn't zero */
2697         if (tdc && !(afs_indexFlags[i] & (IFFree | IFDiscarded))) {
2698             ObtainWriteLock(&tdc->tlock, 623);
2699             tdc->refCount++;
2700             ReleaseWriteLock(&tdc->tlock);
2701
2702             QAdd(&DirtyQ, &tdc->dirty);
2703         }
2704     }
2705     ReleaseWriteLock(&afs_xdcache);
2706
2707     /*
2708      * Now, for each dcache entry we found, check if it's dirty.
2709      * If so, get write-lock, get afs_xdcache, which protects
2710      * afs_cacheInodep, and flush it.  Don't forget to put back
2711      * the refcounts.
2712      */
2713
2714 #define DQTODC(q)       ((struct dcache *)(((char *) (q)) - sizeof(struct afs_q)))
2715
2716     for (tq = DirtyQ.prev; tq != &DirtyQ && code == 0; tq = QPrev(tq)) {
2717         tdc = DQTODC(tq);
2718         if (tdc->dflags & DFEntryMod) {
2719             int wrLock;
2720
2721             wrLock = (0 == NBObtainWriteLock(&tdc->lock, 619));
2722
2723             /* Now that we have the write lock, double-check */
2724             if (wrLock && (tdc->dflags & DFEntryMod)) {
2725                 tdc->dflags &= ~DFEntryMod;
2726                 ObtainWriteLock(&afs_xdcache, 620);
2727                 code = afs_WriteDCache(tdc, 1);
2728                 ReleaseWriteLock(&afs_xdcache);
2729                 if (code) {
2730                     /* We didn't successfully write out the dslot; make sure we
2731                      * try again later */
2732                     tdc->dflags |= DFEntryMod;
2733                 } else {
2734                     touchedit = 1;
2735                 }
2736             }
2737             if (wrLock)
2738                 ReleaseWriteLock(&tdc->lock);
2739         }
2740
2741         afs_PutDCache(tdc);
2742     }
2743
2744     if (code) {
2745         return code;
2746     }
2747
2748     ObtainWriteLock(&afs_xdcache, 617);
2749     if (!touchedit && (cacheDiskType != AFS_FCACHE_TYPE_MEM)) {
2750         /* Touch the file to make sure that the mtime on the file is kept
2751          * up-to-date to avoid losing cached files on cold starts because
2752          * their mtime seems old...
2753          */
2754         struct afs_fheader theader;
2755
2756         afs_InitFHeader(&theader);
2757         afs_osi_Write(afs_cacheInodep, 0, &theader, sizeof(theader));
2758     }
2759     ReleaseWriteLock(&afs_xdcache);
2760     return 0;
2761 }
2762
2763 /*
2764  * afs_MemGetDSlot
2765  *
2766  * Description:
2767  *      Return a pointer to an freshly initialized dcache entry using
2768  *      a memory-based cache.  The tlock will be read-locked.
2769  *
2770  * Parameters:
2771  *      aslot : Dcache slot to look at.
2772  *      type : What 'type' of dslot to get; see the dslot_state enum
2773  *
2774  * Environment:
2775  *      Must be called with afs_xdcache write-locked.
2776  */
2777
2778 struct dcache *
2779 afs_MemGetDSlot(afs_int32 aslot, dslot_state type)
2780 {
2781     struct dcache *tdc;
2782     int existing = 0;
2783
2784     AFS_STATCNT(afs_MemGetDSlot);
2785     if (CheckLock(&afs_xdcache) != -1)
2786         osi_Panic("getdslot nolock");
2787     if (aslot < 0 || aslot >= afs_cacheFiles)
2788         osi_Panic("getdslot slot %d (of %d)", aslot, afs_cacheFiles);
2789     tdc = afs_indexTable[aslot];
2790     if (tdc) {
2791         QRemove(&tdc->lruq);    /* move to queue head */
2792         QAdd(&afs_DLRU, &tdc->lruq);
2793         /* We're holding afs_xdcache, but get tlock in case refCount != 0 */
2794         ObtainWriteLock(&tdc->tlock, 624);
2795         tdc->refCount++;
2796         ConvertWToRLock(&tdc->tlock);
2797         return tdc;
2798     }
2799
2800     /* if we got here, the given slot is not in memory in our list of known
2801      * slots. for memcache, the only place a dslot can exist is in memory, so
2802      * if the caller is expecting to get back a known dslot, and we've reached
2803      * here, something is very wrong. DSLOT_NEW is the only type of dslot that
2804      * may not exist; for all others, the caller assumes the given dslot
2805      * already exists. so, 'type' had better be DSLOT_NEW here, or something is
2806      * very wrong. */
2807     osi_Assert(type == DSLOT_NEW);
2808
2809     if (!afs_freeDSList)
2810         afs_GetDownDSlot(4);
2811     if (!afs_freeDSList) {
2812         /* none free, making one is better than a panic */
2813         afs_stats_cmperf.dcacheXAllocs++;       /* count in case we have a leak */
2814         tdc = afs_osi_Alloc(sizeof(struct dcache));
2815         osi_Assert(tdc != NULL);
2816 #ifdef  KERNEL_HAVE_PIN
2817         pin((char *)tdc, sizeof(struct dcache));        /* XXX */
2818 #endif
2819     } else {
2820         tdc = afs_freeDSList;
2821         afs_freeDSList = (struct dcache *)tdc->lruq.next;
2822         existing = 1;
2823     }
2824     tdc->dflags = 0;    /* up-to-date, not in free q */
2825     tdc->mflags = 0;
2826     QAdd(&afs_DLRU, &tdc->lruq);
2827     if (tdc->lruq.prev == &tdc->lruq)
2828         osi_Panic("lruq 3");
2829
2830     /* initialize entry */
2831     tdc->f.fid.Cell = 0;
2832     tdc->f.fid.Fid.Volume = 0;
2833     tdc->f.chunk = -1;
2834     hones(tdc->f.versionNo);
2835     tdc->f.inode.mem = aslot;
2836     tdc->dflags |= DFEntryMod;
2837     tdc->refCount = 1;
2838     tdc->index = aslot;
2839     afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
2840
2841     if (existing) {
2842         osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
2843         osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
2844         osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
2845     }
2846
2847     AFS_RWLOCK_INIT(&tdc->lock, "dcache lock");
2848     AFS_RWLOCK_INIT(&tdc->tlock, "dcache tlock");
2849     AFS_RWLOCK_INIT(&tdc->mflock, "dcache flock");
2850     ObtainReadLock(&tdc->tlock);
2851
2852     afs_indexTable[aslot] = tdc;
2853     return tdc;
2854
2855 }                               /*afs_MemGetDSlot */
2856
2857 unsigned int last_error = 0, lasterrtime = 0;
2858
2859 /*
2860  * afs_UFSGetDSlot
2861  *
2862  * Description:
2863  *      Return a pointer to an freshly initialized dcache entry using
2864  *      a UFS-based disk cache.  The dcache tlock will be read-locked.
2865  *
2866  * Parameters:
2867  *      aslot : Dcache slot to look at.
2868  *      type : What 'type' of dslot to get; see the dslot_state enum
2869  *
2870  * Environment:
2871  *      afs_xdcache lock write-locked.
2872  */
2873 struct dcache *
2874 afs_UFSGetDSlot(afs_int32 aslot, dslot_state type)
2875 {
2876     afs_int32 code;
2877     struct dcache *tdc;
2878     int existing = 0;
2879     int entryok;
2880     int off;
2881
2882     AFS_STATCNT(afs_UFSGetDSlot);
2883     if (CheckLock(&afs_xdcache) != -1)
2884         osi_Panic("getdslot nolock");
2885     if (aslot < 0 || aslot >= afs_cacheFiles)
2886         osi_Panic("getdslot slot %d (of %d)", aslot, afs_cacheFiles);
2887     tdc = afs_indexTable[aslot];
2888     if (tdc) {
2889         QRemove(&tdc->lruq);    /* move to queue head */
2890         QAdd(&afs_DLRU, &tdc->lruq);
2891         /* Grab tlock in case refCount != 0 */
2892         ObtainWriteLock(&tdc->tlock, 625);
2893         tdc->refCount++;
2894         ConvertWToRLock(&tdc->tlock);
2895         return tdc;
2896     }
2897
2898     /* otherwise we should read it in from the cache file */
2899     if (!afs_freeDSList)
2900         afs_GetDownDSlot(4);
2901     if (!afs_freeDSList) {
2902         /* none free, making one is better than a panic */
2903         afs_stats_cmperf.dcacheXAllocs++;       /* count in case we have a leak */
2904         tdc = afs_osi_Alloc(sizeof(struct dcache));
2905         osi_Assert(tdc != NULL);
2906 #ifdef  KERNEL_HAVE_PIN
2907         pin((char *)tdc, sizeof(struct dcache));        /* XXX */
2908 #endif
2909     } else {
2910         tdc = afs_freeDSList;
2911         afs_freeDSList = (struct dcache *)tdc->lruq.next;
2912         existing = 1;
2913     }
2914     tdc->dflags = 0;    /* up-to-date, not in free q */
2915     tdc->mflags = 0;
2916     QAdd(&afs_DLRU, &tdc->lruq);
2917     if (tdc->lruq.prev == &tdc->lruq)
2918         osi_Panic("lruq 3");
2919
2920     /*
2921      * Seek to the aslot'th entry and read it in.
2922      */
2923     off = sizeof(struct fcache)*aslot + sizeof(struct afs_fheader);
2924     code =
2925         afs_osi_Read(afs_cacheInodep,
2926                      off, (char *)(&tdc->f),
2927                      sizeof(struct fcache));
2928     entryok = 1;
2929     if (code != sizeof(struct fcache)) {
2930         entryok = 0;
2931 #if defined(KERNEL_HAVE_UERROR)
2932         last_error = getuerror();
2933 #else
2934         last_error = code;
2935 #endif
2936         lasterrtime = osi_Time();
2937         if (type != DSLOT_NEW) {
2938             /* If we are requesting a non-DSLOT_NEW slot, this is an error.
2939              * non-DSLOT_NEW slots are supposed to already exist, so if we
2940              * failed to read in the slot, something is wrong. */
2941             struct osi_stat tstat;
2942             if (afs_osi_Stat(afs_cacheInodep, &tstat)) {
2943                 tstat.size = -1;
2944             }
2945             afs_warn("afs: disk cache read error in CacheItems slot %d "
2946                      "off %d/%d code %d/%d\n",
2947                      (int)aslot,
2948                      off, (int)tstat.size,
2949                      (int)code, (int)sizeof(struct fcache));
2950             /* put tdc back on the free dslot list */
2951             QRemove(&tdc->lruq);
2952             tdc->index = NULLIDX;
2953             tdc->lruq.next = (struct afs_q *)afs_freeDSList;
2954             afs_freeDSList = tdc;
2955             return NULL;
2956         }
2957     }
2958     if (!afs_CellNumValid(tdc->f.fid.Cell)) {
2959         entryok = 0;
2960         if (type == DSLOT_VALID) {
2961             osi_Panic("afs: needed valid dcache but index %d off %d has "
2962                       "invalid cell num %d\n",
2963                       (int)aslot, off, (int)tdc->f.fid.Cell);
2964         }
2965     }
2966
2967     if (type == DSLOT_VALID && tdc->f.fid.Fid.Volume == 0) {
2968         osi_Panic("afs: invalid zero-volume dcache entry at slot %d off %d",
2969                   (int)aslot, off);
2970     }
2971
2972     if (type == DSLOT_UNUSED) {
2973         /* the requested dslot is known to exist, but contain invalid data
2974          * (this happens when we're using a dslot from the free or discard
2975          * list). be sure not to re-use the data in it, so force invalidation.
2976          */
2977         entryok = 0;
2978     }
2979
2980     if (!entryok) {
2981         tdc->f.fid.Cell = 0;
2982         tdc->f.fid.Fid.Volume = 0;
2983         tdc->f.chunk = -1;
2984         hones(tdc->f.versionNo);
2985         tdc->dflags |= DFEntryMod;
2986         afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
2987         tdc->f.states &= ~(DRO|DBackup|DRW);
2988         afs_DCMoveBucket(tdc, 0, 0);
2989     } else {
2990         if (tdc->f.states & DRO) {
2991             afs_DCMoveBucket(tdc, 0, 2);
2992         } else if (tdc->f.states & DBackup) {
2993             afs_DCMoveBucket(tdc, 0, 1);
2994         } else {
2995             afs_DCMoveBucket(tdc, 0, 1);
2996         }
2997     }
2998     tdc->refCount = 1;
2999     tdc->index = aslot;
3000     if (tdc->f.chunk >= 0)
3001         tdc->validPos = AFS_CHUNKTOBASE(tdc->f.chunk) + tdc->f.chunkBytes;
3002     else
3003         tdc->validPos = 0;
3004
3005     if (existing) {
3006         osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
3007         osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
3008         osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
3009     }
3010
3011     AFS_RWLOCK_INIT(&tdc->lock, "dcache lock");
3012     AFS_RWLOCK_INIT(&tdc->tlock, "dcache tlock");
3013     AFS_RWLOCK_INIT(&tdc->mflock, "dcache flock");
3014     ObtainReadLock(&tdc->tlock);
3015
3016     /*
3017      * If we didn't read into a temporary dcache region, update the
3018      * slot pointer table.
3019      */
3020     afs_indexTable[aslot] = tdc;
3021     return tdc;
3022
3023 }                               /*afs_UFSGetDSlot */
3024
3025
3026
3027 /*!
3028  * Write a particular dcache entry back to its home in the
3029  * CacheInfo file.
3030  *
3031  * \param adc Pointer to the dcache entry to write.
3032  * \param atime If true, set the modtime on the file to the current time.
3033  *
3034  * \note Environment:
3035  *      Must be called with the afs_xdcache lock at least read-locked,
3036  *      and dcache entry at least read-locked.
3037  *      The reference count is not changed.
3038  */
3039
3040 int
3041 afs_WriteDCache(struct dcache *adc, int atime)
3042 {
3043     afs_int32 code;
3044
3045     if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
3046         return 0;
3047     AFS_STATCNT(afs_WriteDCache);
3048     osi_Assert(WriteLocked(&afs_xdcache));
3049     if (atime)
3050         adc->f.modTime = osi_Time();
3051
3052     if ((afs_indexFlags[adc->index] & (IFFree | IFDiscarded)) == 0 &&
3053         adc->f.fid.Fid.Volume == 0) {
3054         /* If a dcache slot is not on the free or discard list, it must be
3055          * in the hash table. Thus, the volume must be non-zero, since that
3056          * is how we determine whether or not to unhash the entry when kicking
3057          * it out of the cache. Do this check now, since otherwise this can
3058          * cause hash table corruption and a panic later on after we read the
3059          * entry back in. */
3060         osi_Panic("afs_WriteDCache zero volume index %d flags 0x%x\n",
3061                   adc->index, (unsigned)afs_indexFlags[adc->index]);
3062     }
3063
3064     /*
3065      * Seek to the right dcache slot and write the in-memory image out to disk.
3066      */
3067     afs_cellname_write();
3068     code =
3069         afs_osi_Write(afs_cacheInodep,
3070                       sizeof(struct fcache) * adc->index +
3071                       sizeof(struct afs_fheader), (char *)(&adc->f),
3072                       sizeof(struct fcache));
3073     if (code != sizeof(struct fcache)) {
3074         afs_warn("afs: failed to write to CacheItems off %ld code %d/%d\n",
3075                  (long)(sizeof(struct fcache) * adc->index + sizeof(struct afs_fheader)),
3076                  (int)code, (int)sizeof(struct fcache));
3077         return EIO;
3078     }
3079     return 0;
3080 }
3081
3082
3083
3084 /*!
3085  * Wake up users of a particular file waiting for stores to take
3086  * place.
3087  *
3088  * \param avc Ptr to related vcache entry.
3089  *
3090  * \note Environment:
3091  *      Nothing interesting.
3092  */
3093 int
3094 afs_wakeup(struct vcache *avc)
3095 {
3096     int i;
3097     struct brequest *tb;
3098     tb = afs_brs;
3099     AFS_STATCNT(afs_wakeup);
3100     for (i = 0; i < NBRS; i++, tb++) {
3101         /* if request is valid and for this file, we've found it */
3102         if (tb->refCount > 0 && avc == tb->vc) {
3103
3104             /*
3105              * If CSafeStore is on, then we don't awaken the guy
3106              * waiting for the store until the whole store has finished.
3107              * Otherwise, we do it now.  Note that if CSafeStore is on,
3108              * the BStore routine actually wakes up the user, instead
3109              * of us.
3110              * I think this is redundant now because this sort of thing
3111              * is already being handled by the higher-level code.
3112              */
3113             if ((avc->f.states & CSafeStore) == 0) {
3114                 tb->code_raw = tb->code_checkcode = 0;
3115                 tb->flags |= BUVALID;
3116                 if (tb->flags & BUWAIT) {
3117                     tb->flags &= ~BUWAIT;
3118                     afs_osi_Wakeup(tb);
3119                 }
3120             }
3121             break;
3122         }
3123     }
3124     return 0;
3125 }
3126
3127 /*!
3128  * Given a file name and inode, set up that file to be an
3129  * active member in the AFS cache.  This also involves checking
3130  * the usability of its data.
3131  *
3132  * \param afile Name of the cache file to initialize.
3133  * \param ainode Inode of the file.
3134  *
3135  * \note Environment:
3136  *      This function is called only during initialization.
3137  */
3138 int
3139 afs_InitCacheFile(char *afile, ino_t ainode)
3140 {
3141     afs_int32 code;
3142     afs_int32 index;
3143     int fileIsBad;
3144     struct osi_file *tfile;
3145     struct osi_stat tstat;
3146     struct dcache *tdc;
3147
3148     AFS_STATCNT(afs_InitCacheFile);
3149     index = afs_stats_cmperf.cacheNumEntries;
3150     if (index >= afs_cacheFiles)
3151         return EINVAL;
3152
3153     ObtainWriteLock(&afs_xdcache, 282);
3154     tdc = afs_GetNewDSlot(index);
3155     ReleaseReadLock(&tdc->tlock);
3156     ReleaseWriteLock(&afs_xdcache);
3157
3158     ObtainWriteLock(&tdc->lock, 621);
3159     ObtainWriteLock(&afs_xdcache, 622);
3160     if (!afile && !ainode) {
3161         tfile = NULL;
3162         fileIsBad = 1;
3163     } else {
3164         if (afile) {
3165             code = afs_LookupInodeByPath(afile, &tdc->f.inode.ufs, NULL);
3166             if (code) {
3167                 ReleaseWriteLock(&afs_xdcache);
3168                 ReleaseWriteLock(&tdc->lock);
3169                 afs_PutDCache(tdc);
3170                 return code;
3171             }
3172         } else {
3173             /* Add any other 'complex' inode types here ... */
3174 #if !defined(AFS_LINUX26_ENV) && !defined(AFS_CACHE_VNODE_PATH)
3175             tdc->f.inode.ufs = ainode;
3176 #else
3177             osi_Panic("Can't init cache with inode numbers when complex inodes are "
3178                       "in use\n");
3179 #endif
3180         }
3181         fileIsBad = 0;
3182         if ((tdc->f.states & DWriting) || tdc->f.fid.Fid.Volume == 0)
3183             fileIsBad = 1;
3184         tfile = osi_UFSOpen(&tdc->f.inode);
3185         if (!tfile) {
3186             ReleaseWriteLock(&afs_xdcache);
3187             ReleaseWriteLock(&tdc->lock);
3188             afs_PutDCache(tdc);
3189             return ENOENT;
3190         }
3191
3192         code = afs_osi_Stat(tfile, &tstat);
3193         if (code)
3194             osi_Panic("initcachefile stat");
3195
3196         /*
3197          * If file size doesn't match the cache info file, it's probably bad.
3198          */
3199         if (tdc->f.chunkBytes != tstat.size)
3200             fileIsBad = 1;
3201         /*
3202          * If file changed within T (120?) seconds of cache info file, it's
3203          * probably bad.  In addition, if slot changed within last T seconds,
3204          * the cache info file may be incorrectly identified, and so slot
3205          * may be bad.
3206          */
3207         if (cacheInfoModTime < tstat.mtime + 120)
3208             fileIsBad = 1;
3209         if (cacheInfoModTime < tdc->f.modTime + 120)
3210             fileIsBad = 1;
3211         /* In case write through is behind, make sure cache items entry is
3212          * at least as new as the chunk.
3213          */
3214         if (tdc->f.modTime < tstat.mtime)
3215             fileIsBad = 1;
3216     }
3217     tdc->f.chunkBytes = 0;
3218
3219     if (fileIsBad) {
3220         tdc->f.fid.Fid.Volume = 0;      /* not in the hash table */
3221         if (tfile && tstat.size != 0)
3222             osi_UFSTruncate(tfile, 0);
3223         tdc->f.states &= ~(DRO|DBackup|DRW);
3224         afs_DCMoveBucket(tdc, 0, 0);
3225         /* put entry in free cache slot list */
3226         afs_dvnextTbl[tdc->index] = afs_freeDCList;
3227         afs_freeDCList = index;
3228         afs_freeDCCount++;
3229         afs_indexFlags[index] |= IFFree;
3230         afs_indexUnique[index] = 0;
3231     } else {
3232         /*
3233          * We must put this entry in the appropriate hash tables.
3234          * Note that i is still set from the above DCHash call
3235          */
3236         code = DCHash(&tdc->f.fid, tdc->f.chunk);
3237         afs_dcnextTbl[tdc->index] = afs_dchashTbl[code];
3238         afs_dchashTbl[code] = tdc->index;
3239         code = DVHash(&tdc->f.fid);
3240         afs_dvnextTbl[tdc->index] = afs_dvhashTbl[code];
3241         afs_dvhashTbl[code] = tdc->index;
3242         afs_AdjustSize(tdc, tstat.size);        /* adjust to new size */
3243         if (tstat.size > 0)
3244             /* has nontrivial amt of data */
3245             afs_indexFlags[index] |= IFEverUsed;
3246         afs_stats_cmperf.cacheFilesReused++;
3247         /*
3248          * Initialize index times to file's mod times; init indexCounter
3249          * to max thereof
3250          */
3251         hset32(afs_indexTimes[index], tstat.atime);
3252         if (hgetlo(afs_indexCounter) < tstat.atime) {
3253             hset32(afs_indexCounter, tstat.atime);
3254         }
3255         afs_indexUnique[index] = tdc->f.fid.Fid.Unique;
3256     }                           /*File is not bad */
3257
3258     if (tfile)
3259         osi_UFSClose(tfile);
3260     tdc->f.states &= ~DWriting;
3261     tdc->dflags &= ~DFEntryMod;
3262     /* don't set f.modTime; we're just cleaning up */
3263     osi_Assert(afs_WriteDCache(tdc, 0) == 0);
3264     ReleaseWriteLock(&afs_xdcache);
3265     ReleaseWriteLock(&tdc->lock);
3266     afs_PutDCache(tdc);
3267     afs_stats_cmperf.cacheNumEntries++;
3268     return 0;
3269 }
3270
3271
3272 /*Max # of struct dcache's resident at any time*/
3273 /*
3274  * If 'dchint' is enabled then in-memory dcache min is increased because of
3275  * crashes...
3276  */
3277 #define DDSIZE 200
3278
3279 /*!
3280  * Initialize dcache related variables.
3281  *
3282  * \param afiles
3283  * \param ablocks
3284  * \param aDentries
3285  * \param achunk
3286  * \param aflags
3287  *
3288  */
3289 void
3290 afs_dcacheInit(int afiles, int ablocks, int aDentries, int achunk, int aflags)
3291 {
3292     struct dcache *tdp;
3293     int i;
3294     int code;
3295     int afs_dhashbits;
3296
3297     afs_freeDCList = NULLIDX;
3298     afs_discardDCList = NULLIDX;
3299     afs_freeDCCount = 0;
3300     afs_freeDSList = NULL;
3301     hzero(afs_indexCounter);
3302
3303     LOCK_INIT(&afs_xdcache, "afs_xdcache");
3304
3305     /*
3306      * Set chunk size
3307      */
3308     if (achunk) {
3309         if (achunk < 0 || achunk > 30)
3310             achunk = 13;        /* Use default */
3311         AFS_SETCHUNKSIZE(achunk);
3312     }
3313
3314     if (!aDentries)
3315         aDentries = DDSIZE;
3316
3317     /* afs_dhashsize defaults to 1024 */
3318     if (aDentries > 512)
3319         afs_dhashsize = 2048;
3320     /* Try to keep the average chain length around two unless the table
3321      * would be ridiculously big. */
3322     if (aDentries > 4096) {
3323         afs_dhashbits = opr_fls(aDentries) - 3;
3324         /* Cap the hash tables to 32k entries. */
3325         if (afs_dhashbits > 15)
3326             afs_dhashbits = 15;
3327         afs_dhashsize = opr_jhash_size(afs_dhashbits);
3328     }
3329     /* initialize hash tables */
3330     afs_dvhashTbl = afs_osi_Alloc(afs_dhashsize * sizeof(afs_int32));
3331     osi_Assert(afs_dvhashTbl != NULL);
3332     afs_dchashTbl = afs_osi_Alloc(afs_dhashsize * sizeof(afs_int32));
3333     osi_Assert(afs_dchashTbl != NULL);
3334     for (i = 0; i < afs_dhashsize; i++) {
3335         afs_dvhashTbl[i] = NULLIDX;
3336         afs_dchashTbl[i] = NULLIDX;
3337     }
3338     afs_dvnextTbl = afs_osi_Alloc(afiles * sizeof(afs_int32));
3339     osi_Assert(afs_dvnextTbl != NULL);
3340     afs_dcnextTbl = afs_osi_Alloc(afiles * sizeof(afs_int32));
3341     osi_Assert(afs_dcnextTbl != NULL);
3342     for (i = 0; i < afiles; i++) {
3343         afs_dvnextTbl[i] = NULLIDX;
3344         afs_dcnextTbl[i] = NULLIDX;
3345     }
3346
3347     /* Allocate and zero the pointer array to the dcache entries */
3348     afs_indexTable = afs_osi_Alloc(sizeof(struct dcache *) * afiles);
3349     osi_Assert(afs_indexTable != NULL);
3350     memset(afs_indexTable, 0, sizeof(struct dcache *) * afiles);
3351     afs_indexTimes = afs_osi_Alloc(afiles * sizeof(afs_hyper_t));
3352     osi_Assert(afs_indexTimes != NULL);
3353     memset(afs_indexTimes, 0, afiles * sizeof(afs_hyper_t));
3354     afs_indexUnique = afs_osi_Alloc(afiles * sizeof(afs_uint32));
3355     osi_Assert(afs_indexUnique != NULL);
3356     memset(afs_indexUnique, 0, afiles * sizeof(afs_uint32));
3357     afs_indexFlags = afs_osi_Alloc(afiles * sizeof(u_char));
3358     osi_Assert(afs_indexFlags != NULL);
3359     memset(afs_indexFlags, 0, afiles * sizeof(char));
3360
3361     /* Allocate and thread the struct dcache entries themselves */
3362     tdp = afs_Initial_freeDSList =
3363         afs_osi_Alloc(aDentries * sizeof(struct dcache));
3364     osi_Assert(tdp != NULL);
3365     memset(tdp, 0, aDentries * sizeof(struct dcache));
3366 #ifdef  KERNEL_HAVE_PIN
3367     pin((char *)afs_indexTable, sizeof(struct dcache *) * afiles);      /* XXX */
3368     pin((char *)afs_indexTimes, sizeof(afs_hyper_t) * afiles);  /* XXX */
3369     pin((char *)afs_indexFlags, sizeof(char) * afiles); /* XXX */
3370     pin((char *)afs_indexUnique, sizeof(afs_int32) * afiles);   /* XXX */
3371     pin((char *)tdp, aDentries * sizeof(struct dcache));        /* XXX */
3372     pin((char *)afs_dvhashTbl, sizeof(afs_int32) * afs_dhashsize);      /* XXX */
3373     pin((char *)afs_dchashTbl, sizeof(afs_int32) * afs_dhashsize);      /* XXX */
3374     pin((char *)afs_dcnextTbl, sizeof(afs_int32) * afiles);     /* XXX */
3375     pin((char *)afs_dvnextTbl, sizeof(afs_int32) * afiles);     /* XXX */
3376 #endif
3377
3378     afs_freeDSList = &tdp[0];
3379     for (i = 0; i < aDentries - 1; i++) {
3380         tdp[i].lruq.next = (struct afs_q *)(&tdp[i + 1]);
3381         AFS_RWLOCK_INIT(&tdp[i].lock, "dcache lock");
3382         AFS_RWLOCK_INIT(&tdp[i].tlock, "dcache tlock");
3383         AFS_RWLOCK_INIT(&tdp[i].mflock, "dcache flock");
3384     }
3385     tdp[aDentries - 1].lruq.next = (struct afs_q *)0;
3386     AFS_RWLOCK_INIT(&tdp[aDentries - 1].lock, "dcache lock");
3387     AFS_RWLOCK_INIT(&tdp[aDentries - 1].tlock, "dcache tlock");
3388     AFS_RWLOCK_INIT(&tdp[aDentries - 1].mflock, "dcache flock");
3389
3390     afs_stats_cmperf.cacheBlocksOrig = afs_stats_cmperf.cacheBlocksTotal =
3391         afs_cacheBlocks = ablocks;
3392     afs_ComputeCacheParms();    /* compute parms based on cache size */
3393
3394     afs_dcentries = aDentries;
3395     afs_blocksUsed = 0;
3396     afs_stats_cmperf.cacheBucket0_Discarded =
3397         afs_stats_cmperf.cacheBucket1_Discarded =
3398         afs_stats_cmperf.cacheBucket2_Discarded = 0;
3399     afs_DCSizeInit();
3400     QInit(&afs_DLRU);
3401
3402     if (aflags & AFSCALL_INIT_MEMCACHE) {
3403         /*
3404          * Use a memory cache instead of a disk cache
3405          */
3406         cacheDiskType = AFS_FCACHE_TYPE_MEM;
3407         afs_cacheType = &afs_MemCacheOps;
3408         afiles = (afiles < aDentries) ? afiles : aDentries;     /* min */
3409         ablocks = afiles * (AFS_FIRSTCSIZE / 1024);
3410         /* ablocks is reported in 1K blocks */
3411         code = afs_InitMemCache(afiles, AFS_FIRSTCSIZE, aflags);
3412         if (code != 0) {
3413             afs_warn("afsd: memory cache too large for available memory.\n");
3414             afs_warn("afsd: AFS files cannot be accessed.\n\n");
3415             dcacheDisabled = 1;
3416         } else
3417             afs_warn("Memory cache: Allocating %d dcache entries...",
3418                    aDentries);
3419     } else {
3420         cacheDiskType = AFS_FCACHE_TYPE_UFS;
3421         afs_cacheType = &afs_UfsCacheOps;
3422     }
3423 }
3424
3425 /*!
3426  * Shuts down the cache.
3427  *
3428  */
3429 void
3430 shutdown_dcache(void)
3431 {
3432     int i;
3433
3434 #ifdef AFS_CACHE_VNODE_PATH
3435     if (cacheDiskType != AFS_FCACHE_TYPE_MEM) {
3436         struct dcache *tdc;
3437         for (i = 0; i < afs_cacheFiles; i++) {
3438             tdc = afs_indexTable[i];
3439             if (tdc) {
3440                 afs_osi_FreeStr(tdc->f.inode.ufs);
3441             }
3442         }
3443     }
3444 #endif
3445
3446     afs_osi_Free(afs_dvnextTbl, afs_cacheFiles * sizeof(afs_int32));
3447     afs_osi_Free(afs_dcnextTbl, afs_cacheFiles * sizeof(afs_int32));
3448     afs_osi_Free(afs_indexTable, afs_cacheFiles * sizeof(struct dcache *));
3449     afs_osi_Free(afs_indexTimes, afs_cacheFiles * sizeof(afs_hyper_t));
3450     afs_osi_Free(afs_indexUnique, afs_cacheFiles * sizeof(afs_uint32));
3451     afs_osi_Free(afs_indexFlags, afs_cacheFiles * sizeof(u_char));
3452     afs_osi_Free(afs_Initial_freeDSList,
3453                  afs_dcentries * sizeof(struct dcache));
3454 #ifdef  KERNEL_HAVE_PIN
3455     unpin((char *)afs_dcnextTbl, afs_cacheFiles * sizeof(afs_int32));
3456     unpin((char *)afs_dvnextTbl, afs_cacheFiles * sizeof(afs_int32));
3457     unpin((char *)afs_indexTable, afs_cacheFiles * sizeof(struct dcache *));
3458     unpin((char *)afs_indexTimes, afs_cacheFiles * sizeof(afs_hyper_t));
3459     unpin((char *)afs_indexUnique, afs_cacheFiles * sizeof(afs_uint32));
3460     unpin((u_char *) afs_indexFlags, afs_cacheFiles * sizeof(u_char));
3461     unpin(afs_Initial_freeDSList, afs_dcentries * sizeof(struct dcache));
3462 #endif
3463
3464
3465     for (i = 0; i < afs_dhashsize; i++) {
3466         afs_dvhashTbl[i] = NULLIDX;
3467         afs_dchashTbl[i] = NULLIDX;
3468     }
3469
3470     afs_osi_Free(afs_dvhashTbl, afs_dhashsize * sizeof(afs_int32));
3471     afs_osi_Free(afs_dchashTbl, afs_dhashsize * sizeof(afs_int32));
3472
3473     afs_blocksUsed = afs_dcentries = 0;
3474     afs_stats_cmperf.cacheBucket0_Discarded =
3475         afs_stats_cmperf.cacheBucket1_Discarded =
3476         afs_stats_cmperf.cacheBucket2_Discarded = 0;
3477     hzero(afs_indexCounter);
3478
3479     afs_freeDCCount = 0;
3480     afs_freeDCList = NULLIDX;
3481     afs_discardDCList = NULLIDX;
3482     afs_freeDSList = afs_Initial_freeDSList = 0;
3483
3484     LOCK_INIT(&afs_xdcache, "afs_xdcache");
3485     QInit(&afs_DLRU);
3486
3487 }
3488
3489 /*!
3490  * Get a dcache ready for writing, respecting the current cache size limits
3491  *
3492  * len is required because afs_GetDCache with flag == 4 expects the length
3493  * field to be filled. It decides from this whether it's necessary to fetch
3494  * data into the chunk before writing or not (when the whole chunk is
3495  * overwritten!).
3496  *
3497  * \param avc           The vcache to fetch a dcache for
3498  * \param filePos       The start of the section to be written
3499  * \param len           The length of the section to be written
3500  * \param areq
3501  * \param noLock
3502  *
3503  * \return If successful, a reference counted dcache with tdc->lock held. Lock
3504  *         must be released and afs_PutDCache() called to free dcache.
3505  *         NULL on  failure
3506  *
3507  * \note avc->lock must be held on entry. Function may release and reobtain
3508  *       avc->lock and GLOCK.
3509  */
3510
3511 struct dcache *
3512 afs_ObtainDCacheForWriting(struct vcache *avc, afs_size_t filePos,
3513                            afs_size_t len, struct vrequest *areq,
3514                            int noLock)
3515 {
3516     struct dcache *tdc = NULL;
3517     afs_size_t offset;
3518
3519     /* read the cached info */
3520     if (noLock) {
3521         tdc = afs_FindDCache(avc, filePos);
3522         if (tdc)
3523             ObtainWriteLock(&tdc->lock, 657);
3524     } else if (afs_blocksUsed >
3525                PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
3526         tdc = afs_FindDCache(avc, filePos);
3527         if (tdc) {
3528             ObtainWriteLock(&tdc->lock, 658);
3529             if (!hsame(tdc->f.versionNo, avc->f.m.DataVersion)
3530                 || (tdc->dflags & DFFetching)) {
3531                 ReleaseWriteLock(&tdc->lock);
3532                 afs_PutDCache(tdc);
3533                 tdc = NULL;
3534             }
3535         }
3536         if (!tdc) {
3537             afs_MaybeWakeupTruncateDaemon();
3538             while (afs_blocksUsed >
3539                    PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
3540                 ReleaseWriteLock(&avc->lock);
3541                 if (afs_blocksUsed - afs_blocksDiscarded >
3542                     PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
3543                     afs_WaitForCacheDrain = 1;
3544                     afs_osi_Sleep(&afs_WaitForCacheDrain);
3545                 }
3546                 afs_MaybeFreeDiscardedDCache();
3547                 afs_MaybeWakeupTruncateDaemon();
3548                 ObtainWriteLock(&avc->lock, 509);
3549             }
3550             avc->f.states |= CDirty;
3551             tdc = afs_GetDCache(avc, filePos, areq, &offset, &len, 4);
3552             if (tdc)
3553                 ObtainWriteLock(&tdc->lock, 659);
3554         }
3555     } else {
3556         tdc = afs_GetDCache(avc, filePos, areq, &offset, &len, 4);
3557         if (tdc)
3558             ObtainWriteLock(&tdc->lock, 660);
3559     }
3560     if (tdc) {
3561         if (!(afs_indexFlags[tdc->index] & IFDataMod)) {
3562             afs_stats_cmperf.cacheCurrDirtyChunks++;
3563             afs_indexFlags[tdc->index] |= IFDataMod;    /* so it doesn't disappear */
3564         }
3565         if (!(tdc->f.states & DWriting)) {
3566             /* don't mark entry as mod if we don't have to */
3567             tdc->f.states |= DWriting;
3568             tdc->dflags |= DFEntryMod;
3569         }
3570     }
3571     return tdc;
3572 }
3573
3574 /*!
3575  * Make a shadow copy of a dir's dcache. It's used for disconnected
3576  * operations like remove/create/rename to keep the original directory data.
3577  * On reconnection, we can diff the original data with the server and get the
3578  * server changes and with the local data to get the local changes.
3579  *
3580  * \param avc The dir vnode.
3581  * \param adc The dir dcache.
3582  *
3583  * \return 0 for success.
3584  *
3585  * \note The vcache entry must be write locked.
3586  * \note The dcache entry must be read locked.
3587  */
3588 int
3589 afs_MakeShadowDir(struct vcache *avc, struct dcache *adc)
3590 {
3591     int i, code, ret_code = 0, written, trans_size;
3592     struct dcache *new_dc = NULL;
3593     struct osi_file *tfile_src, *tfile_dst;
3594     struct VenusFid shadow_fid;
3595     char *data;
3596
3597     /* Is this a dir? */
3598     if (vType(avc) != VDIR)
3599         return ENOTDIR;
3600
3601     if (avc->f.shadow.vnode || avc->f.shadow.unique)
3602         return EEXIST;
3603
3604     /* Generate a fid for the shadow dir. */
3605     shadow_fid.Cell = avc->f.fid.Cell;
3606     shadow_fid.Fid.Volume = avc->f.fid.Fid.Volume;
3607     afs_GenShadowFid(&shadow_fid);
3608
3609     ObtainWriteLock(&afs_xdcache, 716);
3610
3611     /* Get a fresh dcache. */
3612     new_dc = afs_AllocDCache(avc, 0, 0, &shadow_fid);
3613     osi_Assert(new_dc);
3614
3615     ObtainReadLock(&adc->mflock);
3616
3617     /* Set up the new fid. */
3618     /* Copy interesting data from original dir dcache. */
3619     new_dc->mflags = adc->mflags;
3620     new_dc->dflags = adc->dflags;
3621     new_dc->f.modTime = adc->f.modTime;
3622     new_dc->f.versionNo = adc->f.versionNo;
3623     new_dc->f.states = adc->f.states;
3624     new_dc->f.chunk= adc->f.chunk;
3625     new_dc->f.chunkBytes = adc->f.chunkBytes;
3626
3627     ReleaseReadLock(&adc->mflock);
3628
3629     /* Now add to the two hash chains */
3630     i = DCHash(&shadow_fid, 0);
3631     afs_dcnextTbl[new_dc->index] = afs_dchashTbl[i];
3632     afs_dchashTbl[i] = new_dc->index;
3633
3634     i = DVHash(&shadow_fid);
3635     afs_dvnextTbl[new_dc->index] = afs_dvhashTbl[i];
3636     afs_dvhashTbl[i] = new_dc->index;
3637
3638     ReleaseWriteLock(&afs_xdcache);
3639
3640     /* Alloc a 4k block. */
3641     data = afs_osi_Alloc(4096);
3642     if (!data) {
3643         afs_warn("afs_MakeShadowDir: could not alloc data\n");
3644         ret_code = ENOMEM;
3645         goto done;
3646     }
3647
3648     /* Open the files. */
3649     tfile_src = afs_CFileOpen(&adc->f.inode);
3650     tfile_dst = afs_CFileOpen(&new_dc->f.inode);
3651     osi_Assert(tfile_src);
3652     osi_Assert(tfile_dst);
3653
3654     /* And now copy dir dcache data into this dcache,
3655      * 4k at a time.
3656      */
3657     written = 0;
3658     while (written < adc->f.chunkBytes) {
3659         trans_size = adc->f.chunkBytes - written;
3660         if (trans_size > 4096)
3661             trans_size = 4096;
3662
3663         /* Read a chunk from the dcache. */
3664         code = afs_CFileRead(tfile_src, written, data, trans_size);
3665         if (code < trans_size) {
3666             ret_code = EIO;
3667             break;
3668         }
3669
3670         /* Write it to the new dcache. */
3671         code = afs_CFileWrite(tfile_dst, written, data, trans_size);
3672         if (code < trans_size) {
3673             ret_code = EIO;
3674             break;
3675         }
3676
3677         written+=trans_size;
3678     }
3679
3680     afs_CFileClose(tfile_dst);
3681     afs_CFileClose(tfile_src);
3682
3683     afs_osi_Free(data, 4096);
3684
3685     ReleaseWriteLock(&new_dc->lock);
3686     afs_PutDCache(new_dc);
3687
3688     if (!ret_code) {
3689         ObtainWriteLock(&afs_xvcache, 763);
3690         ObtainWriteLock(&afs_disconDirtyLock, 765);
3691         QAdd(&afs_disconShadow, &avc->shadowq);
3692         osi_Assert((afs_RefVCache(avc) == 0));
3693         ReleaseWriteLock(&afs_disconDirtyLock);
3694         ReleaseWriteLock(&afs_xvcache);
3695
3696         avc->f.shadow.vnode = shadow_fid.Fid.Vnode;
3697         avc->f.shadow.unique = shadow_fid.Fid.Unique;
3698     }
3699
3700 done:
3701     return ret_code;
3702 }
3703
3704 /*!
3705  * Delete the dcaches of a shadow dir.
3706  *
3707  * \param avc The vcache containing the shadow fid.
3708  *
3709  * \note avc must be write locked.
3710  */
3711 void
3712 afs_DeleteShadowDir(struct vcache *avc)
3713 {
3714     struct dcache *tdc;
3715     struct VenusFid shadow_fid;
3716
3717     shadow_fid.Cell = avc->f.fid.Cell;
3718     shadow_fid.Fid.Volume = avc->f.fid.Fid.Volume;
3719     shadow_fid.Fid.Vnode = avc->f.shadow.vnode;
3720     shadow_fid.Fid.Unique = avc->f.shadow.unique;
3721
3722     tdc = afs_FindDCacheByFid(&shadow_fid);
3723     if (tdc) {
3724         afs_HashOutDCache(tdc, 1);
3725         afs_DiscardDCache(tdc);
3726         afs_PutDCache(tdc);
3727     }
3728     avc->f.shadow.vnode = avc->f.shadow.unique = 0;
3729     ObtainWriteLock(&afs_disconDirtyLock, 708);
3730     QRemove(&avc->shadowq);
3731     ReleaseWriteLock(&afs_disconDirtyLock);
3732     afs_PutVCache(avc); /* Because we held it when we added to the queue */
3733 }
3734
3735 /*!
3736  * Populate a dcache with empty chunks up to a given file size,
3737  * used before extending a file in order to avoid 'holes' which
3738  * we can't access in disconnected mode.
3739  *
3740  * \param avc   The vcache which is being extended (locked)
3741  * \param alen  The new length of the file
3742  *
3743  */
3744 void
3745 afs_PopulateDCache(struct vcache *avc, afs_size_t apos, struct vrequest *areq)
3746 {
3747     struct dcache *tdc;
3748     afs_size_t len, offset;
3749     afs_int32 start, end;
3750
3751     /* We're doing this to deal with the situation where we extend
3752      * by writing after lseek()ing past the end of the file . If that
3753      * extension skips chunks, then those chunks won't be created, and
3754      * GetDCache will assume that they have to be fetched from the server.
3755      * So, for each chunk between the current file position, and the new
3756      * length we GetDCache for that chunk.
3757      */
3758
3759     if (AFS_CHUNK(apos) == 0 || apos <= avc->f.m.Length)
3760         return;
3761
3762     if (avc->f.m.Length == 0)
3763         start = 0;
3764     else
3765         start = AFS_CHUNK(avc->f.m.Length)+1;
3766
3767     end = AFS_CHUNK(apos);
3768
3769     while (start<end) {
3770         len = AFS_CHUNKTOSIZE(start);
3771         tdc = afs_GetDCache(avc, AFS_CHUNKTOBASE(start), areq, &offset, &len, 4);
3772         if (tdc)
3773             afs_PutDCache(tdc);
3774         start++;
3775     }
3776 }