DEVEL15-linux-fh-based-cache-20081108
[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 RCSID
17     ("$Header$");
18
19 #include "afs/sysincludes.h"    /*Standard vendor system headers */
20 #include "afsincludes.h"        /*AFS-based standard headers */
21 #include "afs/afs_stats.h"      /* statistics */
22 #include "afs/afs_cbqueue.h"
23 #include "afs/afs_osidnlc.h"
24
25 /* Forward declarations. */
26 static void afs_GetDownD(int anumber, int *aneedSpace, afs_int32 buckethint);
27 static void afs_FreeDiscardedDCache(void);
28 static void afs_DiscardDCache(struct dcache *);
29 static void afs_FreeDCache(struct dcache *);
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 #if defined(LINUX_USE_FH)
58 struct fid cacheitems_fh;
59 int cacheitems_fh_type;
60 #else
61 ino_t cacheInode;               /*!< Inode for CacheItems file */
62 #endif
63 struct osi_file *afs_cacheInodep = 0;   /*!< file for CacheItems inode */
64 struct afs_q afs_DLRU;          /*!< dcache LRU */
65 afs_int32 afs_dhashsize = 1024;
66 afs_int32 *afs_dvhashTbl;       /*!< Data cache hash table: hashed by FID + chunk number. */
67 afs_int32 *afs_dchashTbl;       /*!< Data cache hash table: hashed by FID. */
68 afs_int32 *afs_dvnextTbl;       /*!< Dcache hash table links */
69 afs_int32 *afs_dcnextTbl;       /*!< Dcache hash table links */
70 struct dcache **afs_indexTable; /*!< Pointers to dcache entries */
71 afs_hyper_t *afs_indexTimes;    /*!< Dcache entry Access times */
72 afs_int32 *afs_indexUnique;     /*!< dcache entry Fid.Unique */
73 unsigned char *afs_indexFlags;  /*!< (only one) Is there data there? */
74 afs_hyper_t afs_indexCounter;   /*!< Fake time for marking index
75                                  * entries */
76 afs_int32 afs_cacheFiles = 0;   /*!< Size of afs_indexTable */
77 afs_int32 afs_cacheBlocks;      /*!< 1K blocks in cache */
78 afs_int32 afs_cacheStats;       /*!< Stat entries in cache */
79 afs_int32 afs_blocksUsed;       /*!< Number of blocks in use */
80 afs_int32 afs_blocksDiscarded;  /*!<Blocks freed but not truncated */
81 afs_int32 afs_fsfragsize = 1023;        /*!< Underlying Filesystem minimum unit 
82                                          *of disk allocation usually 1K
83                                          *this value is (truefrag -1 ) to
84                                          *save a bunch of subtracts... */
85 #ifdef AFS_64BIT_CLIENT
86 #ifdef AFS_VM_RDWR_ENV
87 afs_size_t afs_vmMappingEnd;    /* !< For large files (>= 2GB) the VM
88                                  * mapping an 32bit addressing machines
89                                  * can only be used below the 2 GB
90                                  * line. From this point upwards we
91                                  * must do direct I/O into the cache
92                                  * files. The value should be on a
93                                  * chunk boundary. */
94 #endif /* AFS_VM_RDWR_ENV */
95 #endif /* AFS_64BIT_CLIENT */
96
97 /* The following is used to ensure that new dcache's aren't obtained when
98  * the cache is nearly full.
99  */
100 int afs_WaitForCacheDrain = 0;
101 int afs_TruncateDaemonRunning = 0;
102 int afs_CacheTooFull = 0;
103
104 afs_int32 afs_dcentries;        /*!< In-memory dcache entries */
105
106
107 int dcacheDisabled = 0;
108
109 static int afs_UFSCacheFetchProc(), afs_UFSCacheStoreProc();
110 struct afs_cacheOps afs_UfsCacheOps = {
111 #if defined(LINUX_USE_FH)
112     osi_UFSOpen_fh,
113 #else
114     osi_UFSOpen,
115 #endif
116     osi_UFSTruncate,
117     afs_osi_Read,
118     afs_osi_Write,
119     osi_UFSClose,
120     afs_UFSRead,
121     afs_UFSWrite,
122     afs_UFSCacheFetchProc,
123     afs_UFSCacheStoreProc,
124     afs_UFSGetDSlot,
125     afs_UFSGetVolSlot,
126     afs_UFSHandleLink,
127 };
128
129 struct afs_cacheOps afs_MemCacheOps = {
130     afs_MemCacheOpen,
131     afs_MemCacheTruncate,
132     afs_MemReadBlk,
133     afs_MemWriteBlk,
134     afs_MemCacheClose,
135     afs_MemRead,
136     afs_MemWrite,
137     afs_MemCacheFetchProc,
138     afs_MemCacheStoreProc,
139     afs_MemGetDSlot,
140     afs_MemGetVolSlot,
141     afs_MemHandleLink,
142 };
143
144 int cacheDiskType;              /*Type of backing disk for cache */
145 struct afs_cacheOps *afs_cacheType;
146
147 /*!
148  * Where is this vcache's entry associated dcache located/
149  * \param avc The vcache entry.
150  * \return Bucket index:
151  *      1 : main
152  *      2 : RO
153  */
154 static afs_int32
155 afs_DCGetBucket(struct vcache *avc) 
156 {
157     if (!splitdcache) 
158         return 1;
159     
160     /* This should be replaced with some sort of user configurable function */
161     if (avc->states & CRO) {
162         return 2;
163     } else if (avc->states & CBackup) {
164         return 1;
165     } else {
166         /* RW */
167     }
168     /* main bucket */
169     return 1;
170 }
171
172 /*!
173  * Readjust a dcache's size.
174  *
175  * \param adc The dcache to be adjusted.
176  * \param oldSize Old size for the dcache.
177  * \param newSize The new size to be adjusted to.
178  *
179  */
180 static void 
181 afs_DCAdjustSize(struct dcache *adc, afs_int32 oldSize, afs_int32 newSize)
182 {
183     afs_int32 adjustSize = newSize - oldSize;
184
185     if (!splitdcache) 
186         return;
187
188     switch (adc->bucket) 
189     {
190     case 0:
191         afs_blocksUsed_0 += adjustSize;
192         afs_stats_cmperf.cacheBucket0_Discarded += oldSize;
193         break;
194     case 1:
195         afs_blocksUsed_1 += adjustSize;
196         afs_stats_cmperf.cacheBucket1_Discarded += oldSize;
197         break;
198     case 2:
199         afs_blocksUsed_2 += adjustSize;
200         afs_stats_cmperf.cacheBucket2_Discarded += oldSize;
201         break;
202     }
203
204     return;
205 }
206
207 /*!
208  * Move a dcache from one bucket to another.
209  * 
210  * \param adc Operate on this dcache.
211  * \param size Size in bucket (?).
212  * \param newBucket Destination bucket.
213  *
214  */
215 static void 
216 afs_DCMoveBucket(struct dcache *adc, afs_int32 size, afs_int32 newBucket)
217 {
218     if (!splitdcache) 
219         return;
220
221     /* Substract size from old bucket. */       
222     switch (adc->bucket) 
223     {
224     case 0:
225         afs_blocksUsed_0 -= size;
226         break;
227     case 1:
228         afs_blocksUsed_1 -= size;
229         break;
230     case 2:
231         afs_blocksUsed_2 -= size;
232         break;
233     }
234
235     /* Set new bucket and increase destination bucket size. */
236     adc->bucket = newBucket;
237
238     switch (adc->bucket) 
239     {
240     case 0:
241         afs_blocksUsed_0 += size;
242         break;
243     case 1:
244         afs_blocksUsed_1 += size;
245         break;
246     case 2:
247         afs_blocksUsed_2 += size;
248         break;
249     }
250     
251     return;
252 }
253
254 /*!
255  * Init split caches size.
256  */
257 static void 
258 afs_DCSizeInit(void) 
259 {
260     afs_blocksUsed_0 = afs_blocksUsed_1 = afs_blocksUsed_2 = 0;
261 }
262
263
264 /*!
265  * \param phase
266  * \param bucket
267  */
268 static afs_int32
269 afs_DCWhichBucket(afs_int32 phase, afs_int32 bucket) 
270 {
271     if (!splitdcache) 
272         return 0;
273
274     afs_pct1 = afs_blocksUsed_1 / (afs_cacheBlocks / 100);
275     afs_pct2 = afs_blocksUsed_2 / (afs_cacheBlocks / 100);
276
277     /* Short cut: if we don't know about it, try to kill it */
278     if (phase < 2 && afs_blocksUsed_0) 
279         return 0;
280     
281     if (afs_pct1 > afs_tpct1) 
282         return 1;
283     if (afs_pct2 > afs_tpct2)
284         return 2;
285     return 0; /* unlikely */
286 }
287
288
289 /*!
290  * Warn about failing to store a file.
291  *
292  * \param acode Associated error code.
293  * \param avolume Volume involved.
294  * \param aflags How to handle the output:
295  *      aflags & 1: Print out on console
296  *      aflags & 2: Print out on controlling tty
297  *
298  * \note Environment: Call this from close call when vnodeops is RCS unlocked.
299  */
300
301 void
302 afs_StoreWarn(register afs_int32 acode, afs_int32 avolume,
303               register afs_int32 aflags)
304 {
305     static char problem_fmt[] =
306         "afs: failed to store file in volume %d (%s)\n";
307     static char problem_fmt_w_error[] =
308         "afs: failed to store file in volume %d (error %d)\n";
309     static char netproblems[] = "network problems";
310     static char partfull[] = "partition full";
311     static char overquota[] = "over quota";
312
313     AFS_STATCNT(afs_StoreWarn);
314     if (acode < 0) {
315         /*
316          * Network problems
317          */
318         if (aflags & 1)
319             afs_warn(problem_fmt, avolume, netproblems);
320         if (aflags & 2)
321             afs_warnuser(problem_fmt, avolume, netproblems);
322     } else if (acode == ENOSPC) {
323         /*
324          * Partition full
325          */
326         if (aflags & 1)
327             afs_warn(problem_fmt, avolume, partfull);
328         if (aflags & 2)
329             afs_warnuser(problem_fmt, avolume, partfull);
330     } else
331 #ifdef  EDQUOT
332         /* EDQUOT doesn't exist on solaris and won't be sent by the server.
333          * Instead ENOSPC will be sent...
334          */
335     if (acode == EDQUOT) {
336         /*
337          * Quota exceeded
338          */
339         if (aflags & 1)
340             afs_warn(problem_fmt, avolume, overquota);
341         if (aflags & 2)
342             afs_warnuser(problem_fmt, avolume, overquota);
343     } else
344 #endif
345     {
346         /*
347          * Unknown error
348          */
349         if (aflags & 1)
350             afs_warn(problem_fmt_w_error, avolume, acode);
351         if (aflags & 2)
352             afs_warnuser(problem_fmt_w_error, avolume, acode);
353     }
354 }                               /*afs_StoreWarn */
355
356 /*!
357  * Try waking up truncation daemon, if it's worth it.
358  */
359 void
360 afs_MaybeWakeupTruncateDaemon(void)
361 {
362     if (!afs_CacheTooFull && afs_CacheIsTooFull()) {
363         afs_CacheTooFull = 1;
364         if (!afs_TruncateDaemonRunning)
365             afs_osi_Wakeup((int *)afs_CacheTruncateDaemon);
366     } else if (!afs_TruncateDaemonRunning
367                && afs_blocksDiscarded > CM_MAXDISCARDEDCHUNKS) {
368         afs_osi_Wakeup((int *)afs_CacheTruncateDaemon);
369     }
370 }
371
372 /*!
373  * /struct CTD_stats
374  *
375  * Keep statistics on run time for afs_CacheTruncateDaemon. This is a
376  * struct so we need only export one symbol for AIX.
377  */
378 static struct CTD_stats {
379     osi_timeval_t CTD_beforeSleep;
380     osi_timeval_t CTD_afterSleep;
381     osi_timeval_t CTD_sleepTime;
382     osi_timeval_t CTD_runTime;
383     int CTD_nSleeps;
384 } CTD_stats;
385
386 u_int afs_min_cache = 0;
387
388 /*!
389  * Keeps the cache clean and free by truncating uneeded files, when used.
390  * \param  
391  * \return 
392  */
393 void
394 afs_CacheTruncateDaemon(void)
395 {
396     osi_timeval_t CTD_tmpTime;
397     u_int counter;
398     u_int cb_lowat;
399     u_int dc_hiwat =
400         PERCENT((100 - CM_DCACHECOUNTFREEPCT + CM_DCACHEEXTRAPCT), afs_cacheFiles);
401     afs_min_cache =
402         (((10 * AFS_CHUNKSIZE(0)) + afs_fsfragsize) & ~afs_fsfragsize) >> 10;
403
404     osi_GetuTime(&CTD_stats.CTD_afterSleep);
405     afs_TruncateDaemonRunning = 1;
406     while (1) {
407         cb_lowat = PERCENT((CM_DCACHESPACEFREEPCT - CM_DCACHEEXTRAPCT), afs_cacheBlocks);
408         MObtainWriteLock(&afs_xdcache, 266);
409         if (afs_CacheTooFull) {
410             int space_needed, slots_needed;
411             /* if we get woken up, we should try to clean something out */
412             for (counter = 0; counter < 10; counter++) {
413                 space_needed =
414                     afs_blocksUsed - afs_blocksDiscarded - cb_lowat;
415                 slots_needed =
416                     dc_hiwat - afs_freeDCCount - afs_discardDCCount;
417                 afs_GetDownD(slots_needed, &space_needed, 0);
418                 if ((space_needed <= 0) && (slots_needed <= 0)) {
419                     break;
420                 }
421                 if (afs_termState == AFSOP_STOP_TRUNCDAEMON)
422                     break;
423             }
424             if (!afs_CacheIsTooFull())
425                 afs_CacheTooFull = 0;
426         }       /* end of cache cleanup */
427         MReleaseWriteLock(&afs_xdcache);
428
429         /*
430          * This is a defensive check to try to avoid starving threads
431          * that may need the global lock so thay can help free some
432          * cache space. If this thread won't be sleeping or truncating
433          * any cache files then give up the global lock so other
434          * threads get a chance to run.
435          */
436         if ((afs_termState != AFSOP_STOP_TRUNCDAEMON) && afs_CacheTooFull
437             && (!afs_blocksDiscarded || afs_WaitForCacheDrain)) {
438             afs_osi_Wait(100, 0, 0);    /* 100 milliseconds */
439         }
440
441         /*
442          * This is where we free the discarded cache elements.
443          */
444         while (afs_blocksDiscarded && !afs_WaitForCacheDrain
445                && (afs_termState != AFSOP_STOP_TRUNCDAEMON)) {
446             afs_FreeDiscardedDCache();
447         }
448
449         /* See if we need to continue to run. Someone may have
450          * signalled us while we were executing.
451          */
452         if (!afs_WaitForCacheDrain && !afs_CacheTooFull
453             && (afs_termState != AFSOP_STOP_TRUNCDAEMON)) {
454             /* Collect statistics on truncate daemon. */
455             CTD_stats.CTD_nSleeps++;
456             osi_GetuTime(&CTD_stats.CTD_beforeSleep);
457             afs_stats_GetDiff(CTD_tmpTime, CTD_stats.CTD_afterSleep,
458                               CTD_stats.CTD_beforeSleep);
459             afs_stats_AddTo(CTD_stats.CTD_runTime, CTD_tmpTime);
460
461             afs_TruncateDaemonRunning = 0;
462             afs_osi_Sleep((int *)afs_CacheTruncateDaemon);
463             afs_TruncateDaemonRunning = 1;
464
465             osi_GetuTime(&CTD_stats.CTD_afterSleep);
466             afs_stats_GetDiff(CTD_tmpTime, CTD_stats.CTD_beforeSleep,
467                               CTD_stats.CTD_afterSleep);
468             afs_stats_AddTo(CTD_stats.CTD_sleepTime, CTD_tmpTime);
469         }
470         if (afs_termState == AFSOP_STOP_TRUNCDAEMON) {
471 #ifdef AFS_AFSDB_ENV
472             afs_termState = AFSOP_STOP_AFSDB;
473 #else
474             afs_termState = AFSOP_STOP_RXEVENT;
475 #endif
476             afs_osi_Wakeup(&afs_termState);
477             break;
478         }
479     }
480 }
481
482
483 /*!
484  * Make adjustment for the new size in the disk cache entry
485  *
486  * \note Major Assumptions Here:
487  *      Assumes that frag size is an integral power of two, less one,
488  *      and that this is a two's complement machine.  I don't
489  *      know of any filesystems which violate this assumption...
490  *
491  * \param adc Ptr to dcache entry.
492  * \param anewsize New size desired.
493  *
494  */
495
496 void
497 afs_AdjustSize(register struct dcache *adc, register afs_int32 newSize)
498 {
499     register afs_int32 oldSize;
500
501     AFS_STATCNT(afs_AdjustSize);
502
503     adc->dflags |= DFEntryMod;
504     oldSize = ((adc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10;    /* round up */
505     adc->f.chunkBytes = newSize;
506     if (!newSize)
507         adc->validPos = 0;
508     newSize = ((newSize + afs_fsfragsize) ^ afs_fsfragsize) >> 10;      /* round up */
509     afs_DCAdjustSize(adc, oldSize, newSize);
510     if ((newSize > oldSize) && !AFS_IS_DISCONNECTED) {
511
512         /* We're growing the file, wakeup the daemon */
513         afs_MaybeWakeupTruncateDaemon();
514     }
515     afs_blocksUsed += (newSize - oldSize);
516     afs_stats_cmperf.cacheBlocksInUse = afs_blocksUsed; /* XXX */
517 }
518
519
520 /*!
521  * This routine is responsible for moving at least one entry (but up
522  * to some number of them) from the LRU queue to the free queue.
523  *
524  * \param anumber Number of entries that should ideally be moved.
525  * \param aneedSpace How much space we need (1K blocks);
526  *
527  * \note Environment:
528  *      The anumber parameter is just a hint; at least one entry MUST be
529  *      moved, or we'll panic.  We must be called with afs_xdcache
530  *      write-locked.  We should try to satisfy both anumber and aneedspace,
531  *      whichever is more demanding - need to do several things:
532  *      1.  only grab up to anumber victims if aneedSpace <= 0, not
533  *          the whole set of MAXATONCE.
534  *      2.  dynamically choose MAXATONCE to reflect severity of
535  *          demand: something like (*aneedSpace >> (logChunk - 9)) 
536  *
537  *  \note N.B. if we're called with aneedSpace <= 0 and anumber > 0, that
538  *  indicates that the cache is not properly configured/tuned or
539  *  something. We should be able to automatically correct that problem.
540  */
541
542 #define MAXATONCE   16          /* max we can obtain at once */
543 static void
544 afs_GetDownD(int anumber, int *aneedSpace, afs_int32 buckethint)
545 {
546
547     struct dcache *tdc;
548     struct VenusFid *afid;
549     afs_int32 i, j;
550     afs_hyper_t vtime;
551     int skip, phase;
552     register struct vcache *tvc;
553     afs_uint32 victims[MAXATONCE];
554     struct dcache *victimDCs[MAXATONCE];
555     afs_hyper_t victimTimes[MAXATONCE]; /* youngest (largest LRU time) first */
556     afs_uint32 victimPtr;       /* next free item in victim arrays */
557     afs_hyper_t maxVictimTime;  /* youngest (largest LRU time) victim */
558     afs_uint32 maxVictimPtr;    /* where it is */
559     int discard;
560     int curbucket;
561     int vfslocked;
562
563 #if defined(AFS_FBSD80_ENV) && !defined(UKERNEL)
564     vfslocked = VFS_LOCK_GIANT(afs_globalVFS);
565 #endif
566
567     AFS_STATCNT(afs_GetDownD);
568
569     if (CheckLock(&afs_xdcache) != -1)
570         osi_Panic("getdownd nolock");
571     /* decrement anumber first for all dudes in free list */
572     /* SHOULD always decrement anumber first, even if aneedSpace >0, 
573      * because we should try to free space even if anumber <=0 */
574     if (!aneedSpace || *aneedSpace <= 0) {
575         anumber -= afs_freeDCCount;
576         if (anumber <= 0) {
577 #if defined(AFS_FBSD80_ENV) && !defined(UKERNEL)
578           VFS_UNLOCK_GIANT(vfslocked);
579 #endif
580             return;             /* enough already free */
581         }
582     }
583
584     /* bounds check parameter */
585     if (anumber > MAXATONCE)
586         anumber = MAXATONCE;    /* all we can do */
587
588     /* rewrite so phases include a better eligiblity for gc test*/
589     /*
590      * The phase variable manages reclaims.  Set to 0, the first pass,
591      * we don't reclaim active entries, or other than target bucket.  
592      * Set to 1, we reclaim even active ones in target bucket.
593      * Set to 2, we reclaim any inactive one.
594      * Set to 3, we reclaim even active ones.
595      */
596     if (splitdcache) {
597         phase = 0;
598     } else {
599         phase = 4;
600     }
601
602     for (i = 0; i < afs_cacheFiles; i++)
603         /* turn off all flags */
604         afs_indexFlags[i] &= ~IFFlag;
605
606     while (anumber > 0 || (aneedSpace && *aneedSpace > 0)) {
607         /* find oldest entries for reclamation */
608         maxVictimPtr = victimPtr = 0;
609         hzero(maxVictimTime);
610         curbucket = afs_DCWhichBucket(phase, buckethint);
611         /* select victims from access time array */
612         for (i = 0; i < afs_cacheFiles; i++) {
613             if (afs_indexFlags[i] & (IFDataMod | IFFree | IFDiscarded)) {
614                 /* skip if dirty or already free */
615                 continue;
616             }
617             tdc = afs_indexTable[i];
618             if (tdc && (curbucket != tdc->bucket) && (phase < 4))
619             {
620                 /* Wrong bucket; can't use it! */
621                 continue;
622             }
623             if (tdc && (tdc->refCount != 0)) {
624                 /* Referenced; can't use it! */
625                 continue;
626             }
627             hset(vtime, afs_indexTimes[i]);
628
629             /* if we've already looked at this one, skip it */
630             if (afs_indexFlags[i] & IFFlag)
631                 continue;
632
633             if (victimPtr < MAXATONCE) {
634                 /* if there's at least one free victim slot left */
635                 victims[victimPtr] = i;
636                 hset(victimTimes[victimPtr], vtime);
637                 if (hcmp(vtime, maxVictimTime) > 0) {
638                     hset(maxVictimTime, vtime);
639                     maxVictimPtr = victimPtr;
640                 }
641                 victimPtr++;
642             } else if (hcmp(vtime, maxVictimTime) < 0) {
643                 /*
644                  * We're older than youngest victim, so we replace at
645                  * least one victim
646                  */
647                 /* find youngest (largest LRU) victim */
648                 j = maxVictimPtr;
649                 if (j == victimPtr)
650                     osi_Panic("getdownd local");
651                 victims[j] = i;
652                 hset(victimTimes[j], vtime);
653                 /* recompute maxVictimTime */
654                 hset(maxVictimTime, vtime);
655                 for (j = 0; j < victimPtr; j++)
656                     if (hcmp(maxVictimTime, victimTimes[j]) < 0) {
657                         hset(maxVictimTime, victimTimes[j]);
658                         maxVictimPtr = j;
659                     }
660             }
661         }                       /* big for loop */
662
663         /* now really reclaim the victims */
664         j = 0;                  /* flag to track if we actually got any of the victims */
665         /* first, hold all the victims, since we're going to release the lock
666          * during the truncate operation.
667          */
668         for (i = 0; i < victimPtr; i++) {
669             tdc = afs_GetDSlot(victims[i], 0);
670             /* We got tdc->tlock(R) here */
671             if (tdc->refCount == 1)
672                 victimDCs[i] = tdc;
673             else
674                 victimDCs[i] = 0;
675             ReleaseReadLock(&tdc->tlock);
676             if (!victimDCs[i])
677                 afs_PutDCache(tdc);
678         }
679         for (i = 0; i < victimPtr; i++) {
680             /* q is first elt in dcache entry */
681             tdc = victimDCs[i];
682             /* now, since we're dropping the afs_xdcache lock below, we
683              * have to verify, before proceeding, that there are no other
684              * references to this dcache entry, even now.  Note that we
685              * compare with 1, since we bumped it above when we called
686              * afs_GetDSlot to preserve the entry's identity.
687              */
688             if (tdc && tdc->refCount == 1) {
689                 unsigned char chunkFlags;
690                 afs_size_t tchunkoffset = 0;
691                 afid = &tdc->f.fid;
692                 /* xdcache is lower than the xvcache lock */
693                 MReleaseWriteLock(&afs_xdcache);
694                 MObtainReadLock(&afs_xvcache);
695                 tvc = afs_FindVCache(afid, 0, 0 /* no stats, no vlru */ );
696                 MReleaseReadLock(&afs_xvcache);
697                 MObtainWriteLock(&afs_xdcache, 527);
698                 skip = 0;
699                 if (tdc->refCount > 1)
700                     skip = 1;
701                 if (tvc) {
702                     tchunkoffset = AFS_CHUNKTOBASE(tdc->f.chunk);
703                     chunkFlags = afs_indexFlags[tdc->index];
704                     if (((phase & 1) == 0) && osi_Active(tvc))
705                         skip = 1;
706                     if (((phase & 1) == 1) && osi_Active(tvc)
707                         && (tvc->states & CDCLock)
708                         && (chunkFlags & IFAnyPages))
709                         skip = 1;
710                     if (chunkFlags & IFDataMod)
711                         skip = 1;
712                     afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
713                                ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, skip,
714                                ICL_TYPE_INT32, tdc->index, ICL_TYPE_OFFSET,
715                                ICL_HANDLE_OFFSET(tchunkoffset));
716
717 #if     defined(AFS_SUN5_ENV)
718                     /*
719                      * Now we try to invalidate pages.  We do this only for
720                      * Solaris.  For other platforms, it's OK to recycle a
721                      * dcache entry out from under a page, because the strategy
722                      * function can call afs_GetDCache().
723                      */
724                     if (!skip && (chunkFlags & IFAnyPages)) {
725                         int code;
726
727                         MReleaseWriteLock(&afs_xdcache);
728                         MObtainWriteLock(&tvc->vlock, 543);
729                         if (tvc->multiPage) {
730                             skip = 1;
731                             goto endmultipage;
732                         }
733                         /* block locking pages */
734                         tvc->vstates |= VPageCleaning;
735                         /* block getting new pages */
736                         tvc->activeV++;
737                         MReleaseWriteLock(&tvc->vlock);
738                         /* One last recheck */
739                         MObtainWriteLock(&afs_xdcache, 333);
740                         chunkFlags = afs_indexFlags[tdc->index];
741                         if (tdc->refCount > 1 || (chunkFlags & IFDataMod)
742                             || (osi_Active(tvc) && (tvc->states & CDCLock)
743                                 && (chunkFlags & IFAnyPages))) {
744                             skip = 1;
745                             MReleaseWriteLock(&afs_xdcache);
746                             goto endputpage;
747                         }
748                         MReleaseWriteLock(&afs_xdcache);
749
750                         code = osi_VM_GetDownD(tvc, tdc);
751
752                         MObtainWriteLock(&afs_xdcache, 269);
753                         /* we actually removed all pages, clean and dirty */
754                         if (code == 0) {
755                             afs_indexFlags[tdc->index] &=
756                                 ~(IFDirtyPages | IFAnyPages);
757                         } else
758                             skip = 1;
759                         MReleaseWriteLock(&afs_xdcache);
760                       endputpage:
761                         MObtainWriteLock(&tvc->vlock, 544);
762                         if (--tvc->activeV == 0
763                             && (tvc->vstates & VRevokeWait)) {
764                             tvc->vstates &= ~VRevokeWait;
765                             afs_osi_Wakeup((char *)&tvc->vstates);
766
767                         }
768                         if (tvc->vstates & VPageCleaning) {
769                             tvc->vstates &= ~VPageCleaning;
770                             afs_osi_Wakeup((char *)&tvc->vstates);
771                         }
772                       endmultipage:
773                         MReleaseWriteLock(&tvc->vlock);
774                     } else
775 #endif /* AFS_SUN5_ENV */
776                     {
777                         MReleaseWriteLock(&afs_xdcache);
778                     }
779
780                     afs_PutVCache(tvc); /*XXX was AFS_FAST_RELE?*/
781                     MObtainWriteLock(&afs_xdcache, 528);
782                     if (afs_indexFlags[tdc->index] &
783                         (IFDataMod | IFDirtyPages | IFAnyPages))
784                         skip = 1;
785                     if (tdc->refCount > 1)
786                         skip = 1;
787                 }
788 #if     defined(AFS_SUN5_ENV)
789                 else {
790                     /* no vnode, so IFDirtyPages is spurious (we don't
791                      * sweep dcaches on vnode recycling, so we can have
792                      * DIRTYPAGES set even when all pages are gone).  Just
793                      * clear the flag.
794                      * Hold vcache lock to prevent vnode from being
795                      * created while we're clearing IFDirtyPages.
796                      */
797                     afs_indexFlags[tdc->index] &=
798                         ~(IFDirtyPages | IFAnyPages);
799                 }
800 #endif
801                 if (skip) {
802                     /* skip this guy and mark him as recently used */
803                     afs_indexFlags[tdc->index] |= IFFlag;
804                     afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
805                                ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, 2,
806                                ICL_TYPE_INT32, tdc->index, ICL_TYPE_OFFSET,
807                                ICL_HANDLE_OFFSET(tchunkoffset));
808                 } else {
809                     /* flush this dude from the data cache and reclaim;
810                      * first, make sure no one will care that we damage
811                      * it, by removing it from all hash tables.  Then,
812                      * melt it down for parts.  Note that any concurrent
813                      * (new possibility!) calls to GetDownD won't touch
814                      * this guy because his reference count is > 0. */
815                     afs_Trace4(afs_iclSetp, CM_TRACE_GETDOWND,
816                                ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, 3,
817                                ICL_TYPE_INT32, tdc->index, ICL_TYPE_OFFSET,
818                                ICL_HANDLE_OFFSET(tchunkoffset));
819                     AFS_STATCNT(afs_gget);
820                     afs_HashOutDCache(tdc, 1);
821                     if (tdc->f.chunkBytes != 0) {
822                         discard = 1;
823                         if (aneedSpace)
824                             *aneedSpace -=
825                                 (tdc->f.chunkBytes + afs_fsfragsize) >> 10;
826                     } else {
827                         discard = 0;
828                     }
829                     if (discard) {
830                         afs_DiscardDCache(tdc);
831                     } else {
832                         afs_FreeDCache(tdc);
833                     }
834                     anumber--;
835                     j = 1;      /* we reclaimed at least one victim */
836                 }
837             }
838             afs_PutDCache(tdc);
839         }                       /* end of for victims loop */
840
841         if (phase < 5) {
842             /* Phase is 0 and no one was found, so try phase 1 (ignore
843              * osi_Active flag) */
844             if (j == 0) {
845                 phase++;
846                 for (i = 0; i < afs_cacheFiles; i++)
847                     /* turn off all flags */
848                     afs_indexFlags[i] &= ~IFFlag;
849             }
850         } else {
851             /* found no one in phases 0-5, we're hosed */
852             if (victimPtr == 0)
853                 break;
854         }
855     }                           /* big while loop */
856
857 #if defined(AFS_FBSD80_ENV) && !defined(UKERNEL)
858     VFS_UNLOCK_GIANT(vfslocked);
859 #endif
860
861     return;
862
863 }                               /*afs_GetDownD */
864
865
866 /*!
867  * Remove adc from any hash tables that would allow it to be located
868  * again by afs_FindDCache or afs_GetDCache.
869  *
870  * \param adc Pointer to dcache entry to remove from hash tables.
871  *
872  * \note Locks: Must have the afs_xdcache lock write-locked to call this function.
873  *
874  */
875 int
876 afs_HashOutDCache(struct dcache *adc, int zap)
877 {
878     int i, us;
879
880     AFS_STATCNT(afs_glink);
881     if (zap)
882         /* we know this guy's in the LRUQ.  We'll move dude into DCQ below */
883         DZap(adc);
884     /* if this guy is in the hash table, pull him out */
885     if (adc->f.fid.Fid.Volume != 0) {
886         /* remove entry from first hash chains */
887         i = DCHash(&adc->f.fid, adc->f.chunk);
888         us = afs_dchashTbl[i];
889         if (us == adc->index) {
890             /* first dude in the list */
891             afs_dchashTbl[i] = afs_dcnextTbl[adc->index];
892         } else {
893             /* somewhere on the chain */
894             while (us != NULLIDX) {
895                 if (afs_dcnextTbl[us] == adc->index) {
896                     /* found item pointing at the one to delete */
897                     afs_dcnextTbl[us] = afs_dcnextTbl[adc->index];
898                     break;
899                 }
900                 us = afs_dcnextTbl[us];
901             }
902             if (us == NULLIDX)
903                 osi_Panic("dcache hc");
904         }
905         /* remove entry from *other* hash chain */
906         i = DVHash(&adc->f.fid);
907         us = afs_dvhashTbl[i];
908         if (us == adc->index) {
909             /* first dude in the list */
910             afs_dvhashTbl[i] = afs_dvnextTbl[adc->index];
911         } else {
912             /* somewhere on the chain */
913             while (us != NULLIDX) {
914                 if (afs_dvnextTbl[us] == adc->index) {
915                     /* found item pointing at the one to delete */
916                     afs_dvnextTbl[us] = afs_dvnextTbl[adc->index];
917                     break;
918                 }
919                 us = afs_dvnextTbl[us];
920             }
921             if (us == NULLIDX)
922                 osi_Panic("dcache hv");
923         }
924     }
925
926     if (zap) {
927         /* prevent entry from being found on a reboot (it is already out of
928          * the hash table, but after a crash, we just look at fid fields of
929          * stable (old) entries).
930          */
931          adc->f.fid.Fid.Volume = 0;     /* invalid */
932
933         /* mark entry as modified */
934         adc->dflags |= DFEntryMod;
935     }
936
937     /* all done */
938     return 0;
939 }                               /*afs_HashOutDCache */
940
941 /*!
942  * Flush the given dcache entry, pulling it from hash chains
943  * and truncating the associated cache file.
944  *
945  * \param adc Ptr to dcache entry to flush.
946  *
947  * \note Environment:
948  *      This routine must be called with the afs_xdcache lock held
949  *      (in write mode).
950  */
951 void
952 afs_FlushDCache(register struct dcache *adc)
953 {
954     AFS_STATCNT(afs_FlushDCache);
955     /*
956      * Bump the number of cache files flushed.
957      */
958     afs_stats_cmperf.cacheFlushes++;
959
960     /* remove from all hash tables */
961     afs_HashOutDCache(adc, 1);
962
963     /* Free its space; special case null operation, since truncate operation
964      * in UFS is slow even in this case, and this allows us to pre-truncate
965      * these files at more convenient times with fewer locks set
966      * (see afs_GetDownD).
967      */
968     if (adc->f.chunkBytes != 0) {
969         afs_DiscardDCache(adc);
970         afs_MaybeWakeupTruncateDaemon();
971     } else {
972         afs_FreeDCache(adc);
973     }
974
975     if (afs_WaitForCacheDrain) {
976         if (afs_blocksUsed <=
977             PERCENT(CM_CACHESIZEDRAINEDPCT, afs_cacheBlocks)) {
978             afs_WaitForCacheDrain = 0;
979             afs_osi_Wakeup(&afs_WaitForCacheDrain);
980         }
981     }
982 }                               /*afs_FlushDCache */
983
984
985 /*!
986  * Put a dcache entry on the free dcache entry list.
987  *
988  * \param adc dcache entry to free.
989  *
990  * \note Environment: called with afs_xdcache lock write-locked.
991  */
992 static void
993 afs_FreeDCache(register struct dcache *adc)
994 {
995     /* Thread on free list, update free list count and mark entry as
996      * freed in its indexFlags element.  Also, ensure DCache entry gets
997      * written out (set DFEntryMod).
998      */
999
1000     afs_dvnextTbl[adc->index] = afs_freeDCList;
1001     afs_freeDCList = adc->index;
1002     afs_freeDCCount++;
1003     afs_indexFlags[adc->index] |= IFFree;
1004     adc->dflags |= DFEntryMod;
1005
1006     if (afs_WaitForCacheDrain) {
1007         if ((afs_blocksUsed - afs_blocksDiscarded) <=
1008             PERCENT(CM_CACHESIZEDRAINEDPCT, afs_cacheBlocks)) {
1009             afs_WaitForCacheDrain = 0;
1010             afs_osi_Wakeup(&afs_WaitForCacheDrain);
1011         }
1012     }
1013 }                               /* afs_FreeDCache */
1014
1015 /*!
1016  * Discard the cache element by moving it to the discardDCList.
1017  * This puts the cache element into a quasi-freed state, where
1018  * the space may be reused, but the file has not been truncated.
1019  *
1020  * \note Major Assumptions Here:
1021  *      Assumes that frag size is an integral power of two, less one,
1022  *      and that this is a two's complement machine.  I don't
1023  *      know of any filesystems which violate this assumption...
1024  *
1025  * \param adr Ptr to dcache entry.
1026  *
1027  * \note Environment:
1028  *      Must be called with afs_xdcache write-locked.
1029  */
1030
1031 static void
1032 afs_DiscardDCache(register struct dcache *adc)
1033 {
1034     register afs_int32 size;
1035
1036     AFS_STATCNT(afs_DiscardDCache);
1037
1038     osi_Assert(adc->refCount == 1);
1039
1040     size = ((adc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10;       /* round up */
1041     afs_blocksDiscarded += size;
1042     afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1043
1044     afs_dvnextTbl[adc->index] = afs_discardDCList;
1045     afs_discardDCList = adc->index;
1046     afs_discardDCCount++;
1047
1048     adc->f.fid.Fid.Volume = 0;
1049     adc->dflags |= DFEntryMod;
1050     afs_indexFlags[adc->index] |= IFDiscarded;
1051
1052     if (afs_WaitForCacheDrain) {
1053         if ((afs_blocksUsed - afs_blocksDiscarded) <=
1054             PERCENT(CM_CACHESIZEDRAINEDPCT, afs_cacheBlocks)) {
1055             afs_WaitForCacheDrain = 0;
1056             afs_osi_Wakeup(&afs_WaitForCacheDrain);
1057         }
1058     }
1059
1060 }                               /*afs_DiscardDCache */
1061
1062 /*!
1063  * Free the next element on the list of discarded cache elements.
1064  */
1065 static void
1066 afs_FreeDiscardedDCache(void)
1067 {
1068     register struct dcache *tdc;
1069     register struct osi_file *tfile;
1070     register afs_int32 size;
1071
1072     AFS_STATCNT(afs_FreeDiscardedDCache);
1073
1074     MObtainWriteLock(&afs_xdcache, 510);
1075     if (!afs_blocksDiscarded) {
1076         MReleaseWriteLock(&afs_xdcache);
1077         return;
1078     }
1079
1080     /*
1081      * Get an entry from the list of discarded cache elements
1082      */
1083     tdc = afs_GetDSlot(afs_discardDCList, 0);
1084     osi_Assert(tdc->refCount == 1);
1085     ReleaseReadLock(&tdc->tlock);
1086
1087     afs_discardDCList = afs_dvnextTbl[tdc->index];
1088     afs_dvnextTbl[tdc->index] = NULLIDX;
1089     afs_discardDCCount--;
1090     size = ((tdc->f.chunkBytes + afs_fsfragsize) ^ afs_fsfragsize) >> 10;       /* round up */
1091     afs_blocksDiscarded -= size;
1092     afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1093     /* We can lock because we just took it off the free list */
1094     ObtainWriteLock(&tdc->lock, 626);
1095     MReleaseWriteLock(&afs_xdcache);
1096
1097     /*
1098      * Truncate the element to reclaim its space
1099      */
1100 #if defined(LINUX_USE_FH)
1101     tfile = afs_CFileOpen(&tdc->f.fh, tdc->f.fh_type);
1102 #else
1103     tfile = afs_CFileOpen(tdc->f.inode);
1104 #endif
1105     afs_CFileTruncate(tfile, 0);
1106     afs_CFileClose(tfile);
1107     afs_AdjustSize(tdc, 0);
1108     afs_DCMoveBucket(tdc, 0, 0);
1109
1110     /*
1111      * Free the element we just truncated
1112      */
1113     MObtainWriteLock(&afs_xdcache, 511);
1114     afs_indexFlags[tdc->index] &= ~IFDiscarded;
1115     afs_FreeDCache(tdc);
1116     tdc->f.states &= ~(DRO|DBackup|DRW);
1117     ReleaseWriteLock(&tdc->lock);
1118     afs_PutDCache(tdc);
1119     MReleaseWriteLock(&afs_xdcache);
1120 }
1121
1122 /*!
1123  * Free as many entries from the list of discarded cache elements
1124  * as we need to get the free space down below CM_WAITFORDRAINPCT (98%).
1125  *
1126  * \return 0
1127  */
1128 int
1129 afs_MaybeFreeDiscardedDCache(void)
1130 {
1131
1132     AFS_STATCNT(afs_MaybeFreeDiscardedDCache);
1133
1134     while (afs_blocksDiscarded
1135            && (afs_blocksUsed >
1136                PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks))) {
1137         afs_FreeDiscardedDCache();
1138     }
1139     return 0;
1140 }
1141
1142 /*!
1143  * Try to free up a certain number of disk slots.
1144  *
1145  * \param anumber Targeted number of disk slots to free up.
1146  *
1147  * \note Environment:
1148  *      Must be called with afs_xdcache write-locked.
1149  *
1150  */
1151 static void
1152 afs_GetDownDSlot(int anumber)
1153 {
1154     struct afs_q *tq, *nq;
1155     struct dcache *tdc;
1156     int ix;
1157     unsigned int cnt;
1158
1159     AFS_STATCNT(afs_GetDownDSlot);
1160     if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
1161         osi_Panic("diskless getdowndslot");
1162
1163     if (CheckLock(&afs_xdcache) != -1)
1164         osi_Panic("getdowndslot nolock");
1165
1166     /* decrement anumber first for all dudes in free list */
1167     for (tdc = afs_freeDSList; tdc; tdc = (struct dcache *)tdc->lruq.next)
1168         anumber--;
1169     if (anumber <= 0)
1170         return;                 /* enough already free */
1171
1172     for (cnt = 0, tq = afs_DLRU.prev; tq != &afs_DLRU && anumber > 0;
1173          tq = nq, cnt++) {
1174         tdc = (struct dcache *)tq;      /* q is first elt in dcache entry */
1175         nq = QPrev(tq);         /* in case we remove it */
1176         if (tdc->refCount == 0) {
1177             if ((ix = tdc->index) == NULLIDX)
1178                 osi_Panic("getdowndslot");
1179             /* pull the entry out of the lruq and put it on the free list */
1180             QRemove(&tdc->lruq);
1181
1182             /* write-through if modified */
1183             if (tdc->dflags & DFEntryMod) {
1184 #if defined(AFS_SGI_ENV) && defined(AFS_SGI_SHORTSTACK)
1185                 /*
1186                  * ask proxy to do this for us - we don't have the stack space
1187                  */
1188                 while (tdc->dflags & DFEntryMod) {
1189                     int s;
1190                     AFS_GUNLOCK();
1191                     s = SPLOCK(afs_sgibklock);
1192                     if (afs_sgibklist == NULL) {
1193                         /* if slot is free, grab it. */
1194                         afs_sgibklist = tdc;
1195                         SV_SIGNAL(&afs_sgibksync);
1196                     }
1197                     /* wait for daemon to (start, then) finish. */
1198                     SP_WAIT(afs_sgibklock, s, &afs_sgibkwait, PINOD);
1199                     AFS_GLOCK();
1200                 }
1201 #else
1202                 tdc->dflags &= ~DFEntryMod;
1203                 afs_WriteDCache(tdc, 1);
1204 #endif
1205             }
1206
1207             /* finally put the entry in the free list */
1208             afs_indexTable[ix] = NULL;
1209             afs_indexFlags[ix] &= ~IFEverUsed;
1210             tdc->index = NULLIDX;
1211             tdc->lruq.next = (struct afs_q *)afs_freeDSList;
1212             afs_freeDSList = tdc;
1213             anumber--;
1214         }
1215     }
1216 }                               /*afs_GetDownDSlot */
1217
1218
1219 /*
1220  * afs_RefDCache
1221  *
1222  * Description:
1223  *      Increment the reference count on a disk cache entry,
1224  *      which already has a non-zero refcount.  In order to
1225  *      increment the refcount of a zero-reference entry, you
1226  *      have to hold afs_xdcache.
1227  *
1228  * Parameters:
1229  *      adc : Pointer to the dcache entry to increment.
1230  *
1231  * Environment:
1232  *      Nothing interesting.
1233  */
1234 int
1235 afs_RefDCache(struct dcache *adc)
1236 {
1237     ObtainWriteLock(&adc->tlock, 627);
1238     if (adc->refCount < 0)
1239         osi_Panic("RefDCache: negative refcount");
1240     adc->refCount++;
1241     ReleaseWriteLock(&adc->tlock);
1242     return 0;
1243 }
1244
1245
1246 /*
1247  * afs_PutDCache
1248  *
1249  * Description:
1250  *      Decrement the reference count on a disk cache entry.
1251  *
1252  * Parameters:
1253  *      ad : Ptr to the dcache entry to decrement.
1254  *
1255  * Environment:
1256  *      Nothing interesting.
1257  */
1258 int
1259 afs_PutDCache(register struct dcache *adc)
1260 {
1261     AFS_STATCNT(afs_PutDCache);
1262     ObtainWriteLock(&adc->tlock, 276);
1263     if (adc->refCount <= 0)
1264         osi_Panic("putdcache");
1265     --adc->refCount;
1266     ReleaseWriteLock(&adc->tlock);
1267     return 0;
1268 }
1269
1270
1271 /*
1272  * afs_TryToSmush
1273  *
1274  * Description:
1275  *      Try to discard all data associated with this file from the
1276  *      cache.
1277  *
1278  * Parameters:
1279  *      avc : Pointer to the cache info for the file.
1280  *
1281  * Environment:
1282  *      Both pvnLock and lock are write held.
1283  */
1284 void
1285 afs_TryToSmush(register struct vcache *avc, struct AFS_UCRED *acred, int sync)
1286 {
1287     register struct dcache *tdc;
1288     register int index;
1289     register int i;
1290     AFS_STATCNT(afs_TryToSmush);
1291     afs_Trace2(afs_iclSetp, CM_TRACE_TRYTOSMUSH, ICL_TYPE_POINTER, avc,
1292                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
1293     sync = 1;                   /* XX Temp testing XX */
1294
1295 #if     defined(AFS_SUN5_ENV)
1296     ObtainWriteLock(&avc->vlock, 573);
1297     avc->activeV++;             /* block new getpages */
1298     ReleaseWriteLock(&avc->vlock);
1299 #endif
1300
1301     /* Flush VM pages */
1302     osi_VM_TryToSmush(avc, acred, sync);
1303
1304     /*
1305      * Get the hash chain containing all dce's for this fid
1306      */
1307     i = DVHash(&avc->fid);
1308     MObtainWriteLock(&afs_xdcache, 277);
1309     for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
1310         i = afs_dvnextTbl[index];       /* next pointer this hash table */
1311         if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1312             int releaseTlock = 1;
1313             tdc = afs_GetDSlot(index, NULL);
1314             if (!FidCmp(&tdc->f.fid, &avc->fid)) {
1315                 if (sync) {
1316                     if ((afs_indexFlags[index] & IFDataMod) == 0
1317                         && tdc->refCount == 1) {
1318                         ReleaseReadLock(&tdc->tlock);
1319                         releaseTlock = 0;
1320                         afs_FlushDCache(tdc);
1321                     }
1322                 } else
1323                     afs_indexTable[index] = 0;
1324             }
1325             if (releaseTlock)
1326                 ReleaseReadLock(&tdc->tlock);
1327             afs_PutDCache(tdc);
1328         }
1329     }
1330 #if     defined(AFS_SUN5_ENV)
1331     ObtainWriteLock(&avc->vlock, 545);
1332     if (--avc->activeV == 0 && (avc->vstates & VRevokeWait)) {
1333         avc->vstates &= ~VRevokeWait;
1334         afs_osi_Wakeup((char *)&avc->vstates);
1335     }
1336     ReleaseWriteLock(&avc->vlock);
1337 #endif
1338     MReleaseWriteLock(&afs_xdcache);
1339     /*
1340      * It's treated like a callback so that when we do lookups we'll 
1341      * invalidate the unique bit if any
1342      * trytoSmush occured during the lookup call
1343      */
1344     afs_allCBs++;
1345 }
1346
1347 /*
1348  * afs_DCacheMissingChunks
1349  *
1350  * Description
1351  *      Given the cached info for a file, return the number of chunks that
1352  *      are not available from the dcache.
1353  * 
1354  * Parameters:
1355  *      avc:    Pointer to the (held) vcache entry to look in.
1356  * 
1357  * Returns:
1358  *      The number of chunks which are not currently cached.
1359  * 
1360  * Environment:
1361  *      The vcache entry is held upon entry.
1362  */
1363
1364 int
1365 afs_DCacheMissingChunks(struct vcache *avc)
1366 {
1367     int i, index;
1368     afs_size_t totalLength = 0;
1369     afs_uint32 totalChunks = 0;
1370     struct dcache *tdc;
1371
1372     totalLength = avc->m.Length;
1373     if (avc->truncPos < totalLength)
1374         totalLength = avc->truncPos;
1375
1376     /* Length is 0, no chunk missing. */
1377     if (totalLength == 0)
1378         return 0;
1379
1380     /* If totalLength is a multiple of chunksize, the last byte appears
1381      * as being part of the next chunk, which does not exist.
1382      * Decrementing totalLength by one fixes that.
1383      */
1384     totalLength--;
1385     totalChunks = (AFS_CHUNK(totalLength) + 1);
1386
1387     /*
1388      printf("Should have %d chunks for %u bytes\n",
1389                 totalChunks, (totalLength + 1));
1390     */
1391     i = DVHash(&avc->fid);
1392     MObtainWriteLock(&afs_xdcache, 1001);
1393     for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
1394         i = afs_dvnextTbl[index];
1395         if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1396             tdc = afs_GetDSlot(index, NULL);
1397             if (!FidCmp(&tdc->f.fid, &avc->fid)) {
1398                 totalChunks--;
1399             }
1400             ReleaseReadLock(&tdc->tlock);
1401             afs_PutDCache(tdc);
1402         }
1403     }
1404     MReleaseWriteLock(&afs_xdcache);
1405
1406     /*printf("Missing %d chunks\n", totalChunks);*/
1407
1408     return (totalChunks);
1409 }
1410
1411 /*
1412  * afs_FindDCache
1413  *
1414  * Description:
1415  *      Given the cached info for a file and a byte offset into the
1416  *      file, make sure the dcache entry for that file and containing
1417  *      the given byte is available, returning it to our caller.
1418  *
1419  * Parameters:
1420  *      avc   : Pointer to the (held) vcache entry to look in.
1421  *      abyte : Which byte we want to get to.
1422  *
1423  * Returns:
1424  *      Pointer to the dcache entry covering the file & desired byte,
1425  *      or NULL if not found.
1426  *
1427  * Environment:
1428  *      The vcache entry is held upon entry.
1429  */
1430
1431 struct dcache *
1432 afs_FindDCache(register struct vcache *avc, afs_size_t abyte)
1433 {
1434     afs_int32 chunk;
1435     register afs_int32 i, index;
1436     register struct dcache *tdc = NULL;
1437
1438     AFS_STATCNT(afs_FindDCache);
1439     chunk = AFS_CHUNK(abyte);
1440
1441     /*
1442      * Hash on the [fid, chunk] and get the corresponding dcache index
1443      * after write-locking the dcache.
1444      */
1445     i = DCHash(&avc->fid, chunk);
1446     MObtainWriteLock(&afs_xdcache, 278);
1447     for (index = afs_dchashTbl[i]; index != NULLIDX;) {
1448         if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1449             tdc = afs_GetDSlot(index, NULL);
1450             ReleaseReadLock(&tdc->tlock);
1451             if (!FidCmp(&tdc->f.fid, &avc->fid) && chunk == tdc->f.chunk) {
1452                 break;          /* leaving refCount high for caller */
1453             }
1454             afs_PutDCache(tdc);
1455         }
1456         index = afs_dcnextTbl[index];
1457     }
1458     if (index != NULLIDX) {
1459         hset(afs_indexTimes[tdc->index], afs_indexCounter);
1460         hadd32(afs_indexCounter, 1);
1461         MReleaseWriteLock(&afs_xdcache);
1462         return tdc;
1463     } 
1464     MReleaseWriteLock(&afs_xdcache);
1465     return NULL;
1466 }                               /*afs_FindDCache */
1467
1468
1469 /*
1470  * afs_UFSCacheStoreProc
1471  *
1472  * Description:
1473  *      Called upon store.
1474  *
1475  * Parameters:
1476  *      acall : Ptr to the Rx call structure involved.
1477  *      afile : Ptr to the related file descriptor.
1478  *      alen  : Size of the file in bytes.
1479  *      avc   : Ptr to the vcache entry.
1480  *      shouldWake : is it "safe" to return early from close() ?
1481  *      abytesToXferP  : Set to the number of bytes to xfer.
1482  *                       NOTE: This parameter is only used if AFS_NOSTATS
1483  *                              is not defined.
1484  *      abytesXferredP : Set to the number of bytes actually xferred.
1485  *                       NOTE: This parameter is only used if AFS_NOSTATS
1486  *                              is not defined.
1487  *
1488  * Environment:
1489  *      Nothing interesting.
1490  */
1491 static int
1492 afs_UFSCacheStoreProc(register struct rx_call *acall, struct osi_file *afile,
1493                       register afs_int32 alen, struct vcache *avc,
1494                       int *shouldWake, afs_size_t * abytesToXferP,
1495                       afs_size_t * abytesXferredP)
1496 {
1497     afs_int32 code, got;
1498     register char *tbuffer;
1499     register int tlen;
1500
1501     AFS_STATCNT(UFS_CacheStoreProc);
1502
1503 #ifndef AFS_NOSTATS
1504     /*
1505      * In this case, alen is *always* the amount of data we'll be trying
1506      * to ship here.
1507      */
1508     (*abytesToXferP) = alen;
1509     (*abytesXferredP) = 0;
1510 #endif /* AFS_NOSTATS */
1511
1512     afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
1513                ICL_TYPE_FID, &(avc->fid), ICL_TYPE_OFFSET,
1514                ICL_HANDLE_OFFSET(avc->m.Length), ICL_TYPE_INT32, alen);
1515     tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1516     while (alen > 0) {
1517         tlen = (alen > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : alen);
1518         got = afs_osi_Read(afile, -1, tbuffer, tlen);
1519         if ((got < 0)
1520 #if defined(KERNEL_HAVE_UERROR)
1521             || (got != tlen && getuerror())
1522 #endif
1523             ) {
1524             osi_FreeLargeSpace(tbuffer);
1525             return EIO;
1526         }
1527         afs_Trace2(afs_iclSetp, CM_TRACE_STOREPROC2, ICL_TYPE_OFFSET,
1528                    ICL_HANDLE_OFFSET(*tbuffer), ICL_TYPE_INT32, got);
1529         RX_AFS_GUNLOCK();
1530         code = rx_Write(acall, tbuffer, got);   /* writing 0 bytes will
1531                                                  * push a short packet.  Is that really what we want, just because the
1532                                                  * data didn't come back from the disk yet?  Let's try it and see. */
1533         RX_AFS_GLOCK();
1534 #ifndef AFS_NOSTATS
1535         (*abytesXferredP) += code;
1536 #endif /* AFS_NOSTATS */
1537         if (code != got) {
1538             code = rx_Error(acall);
1539             osi_FreeLargeSpace(tbuffer);
1540             return code ? code : -33;
1541         }
1542         alen -= got;
1543         /*
1544          * If file has been locked on server, we can allow the store
1545          * to continue.
1546          */
1547         if (shouldWake && *shouldWake && (rx_GetRemoteStatus(acall) & 1)) {
1548             *shouldWake = 0;    /* only do this once */
1549             afs_wakeup(avc);
1550         }
1551     }
1552     afs_Trace4(afs_iclSetp, CM_TRACE_STOREPROC, ICL_TYPE_POINTER, avc,
1553                ICL_TYPE_FID, &(avc->fid), ICL_TYPE_OFFSET,
1554                ICL_HANDLE_OFFSET(avc->m.Length), ICL_TYPE_INT32, alen);
1555     osi_FreeLargeSpace(tbuffer);
1556     return 0;
1557
1558 }                               /* afs_UFSCacheStoreProc */
1559
1560
1561 /*
1562  * afs_UFSCacheFetchProc
1563  *
1564  * Description:
1565  *      Routine called on fetch; also tells people waiting for data
1566  *      that more has arrived.
1567  *
1568  * Parameters:
1569  *      acall : Ptr to the Rx call structure.
1570  *      afile : File descriptor for the cache file.
1571  *      abase : Base offset to fetch.
1572  *      adc   : Ptr to the dcache entry for the file, write-locked.
1573  *      avc   : Ptr to the vcache entry for the file.
1574  *      abytesToXferP  : Set to the number of bytes to xfer.
1575  *                       NOTE: This parameter is only used if AFS_NOSTATS
1576  *                              is not defined.
1577  *      abytesXferredP : Set to the number of bytes actually xferred.
1578  *                       NOTE: This parameter is only used if AFS_NOSTATS
1579  *                              is not defined.
1580  *
1581  * Environment:
1582  *      Nothing interesting.
1583  */
1584
1585 static int
1586 afs_UFSCacheFetchProc(register struct rx_call *acall, struct osi_file *afile,
1587                       afs_size_t abase, struct dcache *adc,
1588                       struct vcache *avc, afs_size_t * abytesToXferP,
1589                       afs_size_t * abytesXferredP, afs_int32 lengthFound)
1590 {
1591     afs_int32 length;
1592     register afs_int32 code;
1593     register char *tbuffer;
1594     register int tlen;
1595     int moredata = 0;
1596
1597     AFS_STATCNT(UFS_CacheFetchProc);
1598     osi_Assert(WriteLocked(&adc->lock));
1599     afile->offset = 0;          /* Each time start from the beginning */
1600     length = lengthFound;
1601 #ifndef AFS_NOSTATS
1602     (*abytesToXferP) = 0;
1603     (*abytesXferredP) = 0;
1604 #endif /* AFS_NOSTATS */
1605     tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1606     adc->validPos = abase;
1607     do {
1608         if (moredata) {
1609             RX_AFS_GUNLOCK();
1610             code = rx_Read(acall, (char *)&length, sizeof(afs_int32));
1611             RX_AFS_GLOCK();
1612             length = ntohl(length);
1613             if (code != sizeof(afs_int32)) {
1614                 osi_FreeLargeSpace(tbuffer);
1615                 code = rx_Error(acall);
1616                 return (code ? code : -1);      /* try to return code, not -1 */
1617             }
1618         }
1619         /*
1620          * The fetch protocol is extended for the AFS/DFS translator
1621          * to allow multiple blocks of data, each with its own length,
1622          * to be returned. As long as the top bit is set, there are more
1623          * blocks expected.
1624          *
1625          * We do not do this for AFS file servers because they sometimes
1626          * return large negative numbers as the transfer size.
1627          */
1628         if (avc->states & CForeign) {
1629             moredata = length & 0x80000000;
1630             length &= ~0x80000000;
1631         } else {
1632             moredata = 0;
1633         }
1634 #ifndef AFS_NOSTATS
1635         (*abytesToXferP) += length;
1636 #endif /* AFS_NOSTATS */
1637         while (length > 0) {
1638             tlen = (length > AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ : length);
1639 #ifdef RX_KERNEL_TRACE
1640             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1641                        "before rx_Read");
1642 #endif
1643             RX_AFS_GUNLOCK();
1644             code = rx_Read(acall, tbuffer, tlen);
1645             RX_AFS_GLOCK();
1646 #ifdef RX_KERNEL_TRACE
1647             afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1648                        "after rx_Read");
1649 #endif
1650 #ifndef AFS_NOSTATS
1651             (*abytesXferredP) += code;
1652 #endif /* AFS_NOSTATS */
1653             if (code != tlen) {
1654                 osi_FreeLargeSpace(tbuffer);
1655                 afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64READ,
1656                            ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
1657                            ICL_TYPE_INT32, length);
1658                 return -34;
1659             }
1660             code = afs_osi_Write(afile, -1, tbuffer, tlen);
1661             if (code != tlen) {
1662                 osi_FreeLargeSpace(tbuffer);
1663                 return EIO;
1664             }
1665             abase += tlen;
1666             length -= tlen;
1667             adc->validPos = abase;
1668             if (afs_osi_Wakeup(&adc->validPos) == 0)
1669                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
1670                            __FILE__, ICL_TYPE_INT32, __LINE__,
1671                            ICL_TYPE_POINTER, adc, ICL_TYPE_INT32,
1672                            adc->dflags);
1673         }
1674     } while (moredata);
1675     osi_FreeLargeSpace(tbuffer);
1676     return 0;
1677
1678 }                               /* afs_UFSCacheFetchProc */
1679
1680 /*!
1681  * Get a fresh dcache from the free or discarded list.
1682  *
1683  * \param avc Who's dcache is this going to be?
1684  * \param chunk The position where it will be placed in.
1685  * \param lock How are locks held.
1686  * \param ashFid If this dcache going to be used for a shadow dir,
1687  *              this is it's fid.
1688  *
1689  * \note Required locks:
1690  *      - afs_xdcache (W)
1691  *      - avc (R if (lock & 1) set and W otherwise)
1692  * \note It write locks the new dcache. The caller must unlock it.
1693  *
1694  * \return The new dcache.
1695  */
1696 struct dcache *afs_AllocDCache(struct vcache *avc,
1697                                 afs_int32 chunk,
1698                                 afs_int32 lock,
1699                                 struct VenusFid *ashFid)
1700 {
1701     struct dcache *tdc = NULL;
1702     afs_uint32 size = 0;
1703     struct osi_file *file;
1704
1705     if (afs_discardDCList == NULLIDX
1706         || ((lock & 2) && afs_freeDCList != NULLIDX)) {
1707
1708         afs_indexFlags[afs_freeDCList] &= ~IFFree;
1709         tdc = afs_GetDSlot(afs_freeDCList, 0);
1710         osi_Assert(tdc->refCount == 1);
1711         ReleaseReadLock(&tdc->tlock);
1712         ObtainWriteLock(&tdc->lock, 604);
1713         afs_freeDCList = afs_dvnextTbl[tdc->index];
1714         afs_freeDCCount--;
1715     } else {
1716         afs_indexFlags[afs_discardDCList] &= ~IFDiscarded;
1717         tdc = afs_GetDSlot(afs_discardDCList, 0);
1718         osi_Assert(tdc->refCount == 1);
1719         ReleaseReadLock(&tdc->tlock);
1720         ObtainWriteLock(&tdc->lock, 605);
1721         afs_discardDCList = afs_dvnextTbl[tdc->index];
1722         afs_discardDCCount--;
1723         size =
1724             ((tdc->f.chunkBytes +
1725               afs_fsfragsize) ^ afs_fsfragsize) >> 10;
1726         tdc->f.states &= ~(DRO|DBackup|DRW);
1727         afs_DCMoveBucket(tdc, size, 0);
1728         afs_blocksDiscarded -= size;
1729         afs_stats_cmperf.cacheBlocksDiscarded = afs_blocksDiscarded;
1730         if (lock & 2) {
1731             /* Truncate the chunk so zeroes get filled properly */
1732 #if defined(LINUX_USE_FH)
1733             file = afs_CFileOpen(&tdc->f.fh, tdc->f.fh_type);
1734 #else
1735             file = afs_CFileOpen(tdc->f.inode);
1736 #endif
1737             afs_CFileTruncate(file, 0);
1738             afs_CFileClose(file);
1739             afs_AdjustSize(tdc, 0);
1740         }
1741     }
1742
1743     /*
1744      * Locks held:
1745      * avc->lock(R) if setLocks
1746      * avc->lock(W) if !setLocks
1747      * tdc->lock(W)
1748      * afs_xdcache(W)
1749      */
1750
1751     /*
1752      * Fill in the newly-allocated dcache record.
1753      */
1754     afs_indexFlags[tdc->index] &= ~(IFDirtyPages | IFAnyPages);
1755     if (ashFid)
1756         /* Use shadow fid if provided. */
1757         tdc->f.fid = *ashFid;
1758     else
1759         /* Use normal vcache's fid otherwise. */
1760         tdc->f.fid = avc->fid;
1761     if (avc->states & CRO)
1762         tdc->f.states = DRO;
1763     else if (avc->states & CBackup)
1764         tdc->f.states = DBackup;
1765     else
1766         tdc->f.states = DRW;
1767     afs_DCMoveBucket(tdc, 0, afs_DCGetBucket(avc));
1768     afs_indexUnique[tdc->index] = tdc->f.fid.Fid.Unique;
1769     if (!ashFid)
1770         hones(tdc->f.versionNo);        /* invalid value */
1771     tdc->f.chunk = chunk;
1772     tdc->validPos = AFS_CHUNKTOBASE(chunk);
1773     /* XXX */
1774     if (tdc->lruq.prev == &tdc->lruq)
1775         osi_Panic("lruq 1");
1776
1777     return tdc;
1778 }
1779
1780 /*
1781  * afs_GetDCache
1782  *
1783  * Description:
1784  *      This function is called to obtain a reference to data stored in
1785  *      the disk cache, locating a chunk of data containing the desired
1786  *      byte and returning a reference to the disk cache entry, with its
1787  *      reference count incremented.
1788  *
1789  * Parameters:
1790  * IN:
1791  *      avc     : Ptr to a vcache entry (unlocked)
1792  *      abyte   : Byte position in the file desired
1793  *      areq    : Request structure identifying the requesting user.
1794  *      aflags  : Settings as follows:
1795  *                      1 : Set locks
1796  *                      2 : Return after creating entry.
1797  *                      4 : called from afs_vnop_write.c
1798  *                          *alen contains length of data to be written.
1799  * OUT:
1800  *      aoffset : Set to the offset within the chunk where the resident
1801  *                byte is located.
1802  *      alen    : Set to the number of bytes of data after the desired
1803  *                byte (including the byte itself) which can be read
1804  *                from this chunk.
1805  *
1806  * Environment:
1807  *      The vcache entry pointed to by avc is unlocked upon entry.
1808  */
1809
1810 struct tlocal1 {
1811     struct AFSVolSync tsync;
1812     struct AFSFetchStatus OutStatus;
1813     struct AFSCallBack CallBack;
1814 };
1815
1816 /*
1817  * Update the vnode-to-dcache hint if we can get the vnode lock
1818  * right away.  Assumes dcache entry is at least read-locked.
1819  */
1820 void
1821 updateV2DC(int lockVc, struct vcache *v, struct dcache *d, int src)
1822 {
1823     if (!lockVc || 0 == NBObtainWriteLock(&v->lock, src)) {
1824         if (hsame(v->m.DataVersion, d->f.versionNo) && v->callback)
1825             v->dchint = d;
1826         if (lockVc)
1827             ReleaseWriteLock(&v->lock);
1828     }
1829 }
1830
1831 /* avc - Write-locked unless aflags & 1 */
1832 struct dcache *
1833 afs_GetDCache(register struct vcache *avc, afs_size_t abyte,
1834               register struct vrequest *areq, afs_size_t * aoffset,
1835               afs_size_t * alen, int aflags)
1836 {
1837     register afs_int32 i, code, code1 = 0, shortcut;
1838 #if     defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
1839     register afs_int32 adjustsize = 0;
1840 #endif
1841     int setLocks;
1842     afs_int32 index;
1843     afs_int32 us;
1844     afs_int32 chunk;
1845     afs_size_t maxGoodLength;   /* amount of good data at server */
1846     struct rx_call *tcall;
1847     afs_size_t Position = 0;
1848 #ifdef AFS_64BIT_CLIENT
1849     afs_size_t tsize;
1850     afs_size_t lengthFound;     /* as returned from server */
1851 #endif /* AFS_64BIT_CLIENT */
1852     afs_int32 size, tlen;       /* size of segment to transfer */
1853     struct tlocal1 *tsmall = 0;
1854     register struct dcache *tdc;
1855     register struct osi_file *file;
1856     register struct conn *tc;
1857     int downDCount = 0;
1858     struct server *newCallback = NULL;
1859     char setNewCallback;
1860     char setVcacheStatus;
1861     char doVcacheUpdate;
1862     char slowPass = 0;
1863     int doAdjustSize = 0;
1864     int doReallyAdjustSize = 0;
1865     int overWriteWholeChunk = 0;
1866
1867     XSTATS_DECLS;
1868 #ifndef AFS_NOSTATS
1869     struct afs_stats_xferData *xferP;   /* Ptr to this op's xfer struct */
1870     osi_timeval_t xferStartTime,        /*FS xfer start time */
1871       xferStopTime;             /*FS xfer stop time */
1872     afs_size_t bytesToXfer;     /* # bytes to xfer */
1873     afs_size_t bytesXferred;    /* # bytes actually xferred */
1874     struct afs_stats_AccessInfo *accP;  /*Ptr to access record in stats */
1875     int fromReplica;            /*Are we reading from a replica? */
1876     int numFetchLoops;          /*# times around the fetch/analyze loop */
1877 #endif /* AFS_NOSTATS */
1878
1879     AFS_STATCNT(afs_GetDCache);
1880     if (dcacheDisabled)
1881         return NULL;
1882
1883     setLocks = aflags & 1;
1884
1885     /*
1886      * Determine the chunk number and offset within the chunk corresponding
1887      * to the desired byte.
1888      */
1889     if (avc->fid.Fid.Vnode & 1) {       /* if (vType(avc) == VDIR) */
1890         chunk = 0;
1891     } else {
1892         chunk = AFS_CHUNK(abyte);
1893     }
1894
1895     /* come back to here if we waited for the cache to drain. */
1896   RetryGetDCache:
1897
1898     setNewCallback = setVcacheStatus = 0;
1899
1900     if (setLocks) {
1901         if (slowPass)
1902             ObtainWriteLock(&avc->lock, 616);
1903         else
1904             ObtainReadLock(&avc->lock);
1905     }
1906
1907     /*
1908      * Locks held:
1909      * avc->lock(R) if setLocks && !slowPass
1910      * avc->lock(W) if !setLocks || slowPass
1911      */
1912
1913     shortcut = 0;
1914
1915     /* check hints first! (might could use bcmp or some such...) */
1916     if ((tdc = avc->dchint)) {
1917         int dcLocked;
1918
1919         /*
1920          * The locking order between afs_xdcache and dcache lock matters.
1921          * The hint dcache entry could be anywhere, even on the free list.
1922          * Locking afs_xdcache ensures that noone is trying to pull dcache
1923          * entries from the free list, and thereby assuming them to be not
1924          * referenced and not locked.
1925          */
1926         MObtainReadLock(&afs_xdcache);
1927         dcLocked = (0 == NBObtainSharedLock(&tdc->lock, 601));
1928
1929         if (dcLocked && (tdc->index != NULLIDX)
1930             && !FidCmp(&tdc->f.fid, &avc->fid) && chunk == tdc->f.chunk
1931             && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1932             /* got the right one.  It might not be the right version, and it 
1933              * might be fetching, but it's the right dcache entry.
1934              */
1935             /* All this code should be integrated better with what follows:
1936              * I can save a good bit more time under a write lock if I do..
1937              */
1938             ObtainWriteLock(&tdc->tlock, 603);
1939             tdc->refCount++;
1940             ReleaseWriteLock(&tdc->tlock);
1941
1942             MReleaseReadLock(&afs_xdcache);
1943             shortcut = 1;
1944
1945             if (hsame(tdc->f.versionNo, avc->m.DataVersion)
1946                 && !(tdc->dflags & DFFetching)) {
1947
1948                 afs_stats_cmperf.dcacheHits++;
1949                 MObtainWriteLock(&afs_xdcache, 559);
1950                 QRemove(&tdc->lruq);
1951                 QAdd(&afs_DLRU, &tdc->lruq);
1952                 MReleaseWriteLock(&afs_xdcache);
1953
1954                 /* Locks held:
1955                  * avc->lock(R) if setLocks && !slowPass
1956                  * avc->lock(W) if !setLocks || slowPass
1957                  * tdc->lock(S)
1958                  */
1959                 goto done;
1960             }
1961         } else {
1962             if (dcLocked)
1963                 ReleaseSharedLock(&tdc->lock);
1964             MReleaseReadLock(&afs_xdcache);
1965         }
1966
1967         if (!shortcut)
1968             tdc = 0;
1969     }
1970
1971     /* Locks held:
1972      * avc->lock(R) if setLocks && !slowPass
1973      * avc->lock(W) if !setLocks || slowPass
1974      * tdc->lock(S) if tdc
1975      */
1976
1977     if (!tdc) {                 /* If the hint wasn't the right dcache entry */
1978         /*
1979          * Hash on the [fid, chunk] and get the corresponding dcache index
1980          * after write-locking the dcache.
1981          */
1982       RetryLookup:
1983
1984         /* Locks held:
1985          * avc->lock(R) if setLocks && !slowPass
1986          * avc->lock(W) if !setLocks || slowPass
1987          */
1988
1989         i = DCHash(&avc->fid, chunk);
1990         /* check to make sure our space is fine */
1991         afs_MaybeWakeupTruncateDaemon();
1992
1993         MObtainWriteLock(&afs_xdcache, 280);
1994         us = NULLIDX;
1995         for (index = afs_dchashTbl[i]; index != NULLIDX;) {
1996             if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1997                 tdc = afs_GetDSlot(index, NULL);
1998                 ReleaseReadLock(&tdc->tlock);
1999                 /*
2000                  * Locks held:
2001                  * avc->lock(R) if setLocks && !slowPass
2002                  * avc->lock(W) if !setLocks || slowPass
2003                  * afs_xdcache(W)
2004                  */
2005                 if (!FidCmp(&tdc->f.fid, &avc->fid) && chunk == tdc->f.chunk) {
2006                     /* Move it up in the beginning of the list */
2007                     if (afs_dchashTbl[i] != index) {
2008                         afs_dcnextTbl[us] = afs_dcnextTbl[index];
2009                         afs_dcnextTbl[index] = afs_dchashTbl[i];
2010                         afs_dchashTbl[i] = index;
2011                     }
2012                     MReleaseWriteLock(&afs_xdcache);
2013                     ObtainSharedLock(&tdc->lock, 606);
2014                     break;      /* leaving refCount high for caller */
2015                 }
2016                 afs_PutDCache(tdc);
2017                 tdc = 0;
2018             }
2019             us = index;
2020             index = afs_dcnextTbl[index];
2021         }
2022
2023         /*
2024          * If we didn't find the entry, we'll create one.
2025          */
2026         if (index == NULLIDX) {
2027             /*
2028              * Locks held:
2029              * avc->lock(R) if setLocks
2030              * avc->lock(W) if !setLocks
2031              * afs_xdcache(W)
2032              */
2033             afs_Trace2(afs_iclSetp, CM_TRACE_GETDCACHE1, ICL_TYPE_POINTER,
2034                        avc, ICL_TYPE_INT32, chunk);
2035
2036             /* Make sure there is a free dcache entry for us to use */
2037             if (afs_discardDCList == NULLIDX && afs_freeDCList == NULLIDX) {
2038                 while (1) {
2039                     if (!setLocks)
2040                         avc->states |= CDCLock;
2041                     /* just need slots */
2042                     afs_GetDownD(5, (int *)0, afs_DCGetBucket(avc));
2043                     if (!setLocks)
2044                         avc->states &= ~CDCLock;
2045                     if (afs_discardDCList != NULLIDX
2046                         || afs_freeDCList != NULLIDX)
2047                         break;
2048                     /* If we can't get space for 5 mins we give up and panic */
2049                     if (++downDCount > 300) {
2050 #if defined(AFS_CACHE_BYPASS)
2051                         afs_warn("GetDCache calling osi_Panic: No space in five minutes.\n downDCount: %d\n aoffset: %d alen: %d\n", downDCount, aoffset, alen);
2052 #endif
2053                         osi_Panic("getdcache");
2054                     }
2055                     MReleaseWriteLock(&afs_xdcache);
2056                     /*
2057                      * Locks held:
2058                      * avc->lock(R) if setLocks
2059                      * avc->lock(W) if !setLocks
2060                      */
2061                     afs_osi_Wait(1000, 0, 0);
2062                     goto RetryLookup;
2063                 }
2064             }
2065
2066             tdc = afs_AllocDCache(avc, chunk, aflags, NULL);
2067
2068             /*
2069              * Now add to the two hash chains - note that i is still set
2070              * from the above DCHash call.
2071              */
2072             afs_dcnextTbl[tdc->index] = afs_dchashTbl[i];
2073             afs_dchashTbl[i] = tdc->index;
2074             i = DVHash(&avc->fid);
2075             afs_dvnextTbl[tdc->index] = afs_dvhashTbl[i];
2076             afs_dvhashTbl[i] = tdc->index;
2077             tdc->dflags = DFEntryMod;
2078             tdc->mflags = 0;
2079             afs_MaybeWakeupTruncateDaemon();
2080             MReleaseWriteLock(&afs_xdcache);
2081             ConvertWToSLock(&tdc->lock);
2082         }
2083     }
2084
2085
2086     /* vcache->dcache hint failed */
2087     /*
2088      * Locks held:
2089      * avc->lock(R) if setLocks && !slowPass
2090      * avc->lock(W) if !setLocks || slowPass
2091      * tdc->lock(S)
2092      */
2093     afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE2, ICL_TYPE_POINTER, avc,
2094                ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
2095                hgetlo(tdc->f.versionNo), ICL_TYPE_INT32,
2096                hgetlo(avc->m.DataVersion));
2097     /*
2098      * Here we have the entry in tdc, with its refCount incremented.
2099      * Note: we don't use the S-lock on avc; it costs concurrency when
2100      * storing a file back to the server.
2101      */
2102
2103     /*
2104      * Not a newly created file so we need to check the file's length and
2105      * compare data versions since someone could have changed the data or we're
2106      * reading a file written elsewhere. We only want to bypass doing no-op
2107      * read rpcs on newly created files (dv of 0) since only then we guarantee
2108      * that this chunk's data hasn't been filled by another client.
2109      */
2110     size = AFS_CHUNKSIZE(abyte);
2111     if (aflags & 4)             /* called from write */
2112         tlen = *alen;
2113     else                        /* called from read */
2114         tlen = tdc->validPos - abyte;
2115     Position = AFS_CHUNKTOBASE(chunk);
2116     afs_Trace4(afs_iclSetp, CM_TRACE_GETDCACHE3, ICL_TYPE_INT32, tlen,
2117                ICL_TYPE_INT32, aflags, ICL_TYPE_OFFSET,
2118                ICL_HANDLE_OFFSET(abyte), ICL_TYPE_OFFSET,
2119                ICL_HANDLE_OFFSET(Position));
2120     if ((aflags & 4) && (hiszero(avc->m.DataVersion)))
2121         doAdjustSize = 1;
2122     if ((AFS_CHUNKTOBASE(chunk) >= avc->m.Length) ||
2123          ((aflags & 4) && (abyte == Position) && (tlen >= size)))
2124         overWriteWholeChunk = 1;
2125     if (doAdjustSize || overWriteWholeChunk) {
2126 #if     defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV)
2127 #ifdef  AFS_SGI_ENV
2128 #ifdef AFS_SGI64_ENV
2129         if (doAdjustSize)
2130             adjustsize = NBPP;
2131 #else /* AFS_SGI64_ENV */
2132         if (doAdjustSize)
2133             adjustsize = 8192;
2134 #endif /* AFS_SGI64_ENV */
2135 #else /* AFS_SGI_ENV */
2136         if (doAdjustSize)
2137             adjustsize = 4096;
2138 #endif /* AFS_SGI_ENV */
2139         if (AFS_CHUNKTOBASE(chunk) + adjustsize >= avc->m.Length &&
2140 #else /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
2141 #if     defined(AFS_SUN5_ENV)  || defined(AFS_OSF_ENV)
2142         if ((doAdjustSize || (AFS_CHUNKTOBASE(chunk) >= avc->m.Length)) &&
2143 #else
2144         if (AFS_CHUNKTOBASE(chunk) >= avc->m.Length &&
2145 #endif
2146 #endif /* defined(AFS_AIX32_ENV) || defined(AFS_SGI_ENV) */
2147             !hsame(avc->m.DataVersion, tdc->f.versionNo))
2148             doReallyAdjustSize = 1;
2149
2150         if (doReallyAdjustSize || overWriteWholeChunk) {
2151             /* no data in file to read at this position */
2152             UpgradeSToWLock(&tdc->lock, 607);
2153
2154 #if defined(LINUX_USE_FH)
2155             file = afs_CFileOpen(&tdc->f.fh, tdc->f.fh_type);
2156 #else
2157             file = afs_CFileOpen(tdc->f.inode);
2158 #endif
2159             afs_CFileTruncate(file, 0);
2160             afs_CFileClose(file);
2161             afs_AdjustSize(tdc, 0);
2162             hset(tdc->f.versionNo, avc->m.DataVersion);
2163             tdc->dflags |= DFEntryMod;
2164
2165             ConvertWToSLock(&tdc->lock);
2166         }
2167     }
2168
2169     /*
2170      * We must read in the whole chunk if the version number doesn't
2171      * match.
2172      */
2173     if (aflags & 2) {
2174         /* don't need data, just a unique dcache entry */
2175         ObtainWriteLock(&afs_xdcache, 608);
2176         hset(afs_indexTimes[tdc->index], afs_indexCounter);
2177         hadd32(afs_indexCounter, 1);
2178         ReleaseWriteLock(&afs_xdcache);
2179
2180         updateV2DC(setLocks, avc, tdc, 553);
2181         if (vType(avc) == VDIR)
2182             *aoffset = abyte;
2183         else
2184             *aoffset = AFS_CHUNKOFFSET(abyte);
2185         if (tdc->validPos < abyte)
2186             *alen = (afs_size_t) 0;
2187         else
2188             *alen = tdc->validPos - abyte;
2189         ReleaseSharedLock(&tdc->lock);
2190         if (setLocks) {
2191             if (slowPass)
2192                 ReleaseWriteLock(&avc->lock);
2193             else
2194                 ReleaseReadLock(&avc->lock);
2195         }
2196         return tdc;             /* check if we're done */
2197     }
2198
2199     /*
2200      * Locks held:
2201      * avc->lock(R) if setLocks && !slowPass
2202      * avc->lock(W) if !setLocks || slowPass
2203      * tdc->lock(S)
2204      */
2205     osi_Assert((setLocks && !slowPass) || WriteLocked(&avc->lock));
2206
2207     setNewCallback = setVcacheStatus = 0;
2208
2209     /*
2210      * Locks held:
2211      * avc->lock(R) if setLocks && !slowPass
2212      * avc->lock(W) if !setLocks || slowPass
2213      * tdc->lock(S)
2214      */
2215     if (!hsame(avc->m.DataVersion, tdc->f.versionNo) && !overWriteWholeChunk) {
2216         /*
2217          * Version number mismatch.
2218          */
2219         /*
2220          * If we are disconnected, then we can't do much of anything
2221          * because the data doesn't match the file.
2222          */
2223         if (AFS_IS_DISCONNECTED) {
2224             ReleaseSharedLock(&tdc->lock);
2225             if (setLocks) {
2226                 if (slowPass)
2227                     ReleaseWriteLock(&avc->lock);
2228                 else
2229                     ReleaseReadLock(&avc->lock);
2230             }
2231             /* Flush the Dcache */
2232             afs_PutDCache(tdc);
2233
2234             return NULL;
2235         }
2236         UpgradeSToWLock(&tdc->lock, 609);
2237
2238         /*
2239          * If data ever existed for this vnode, and this is a text object,
2240          * do some clearing.  Now, you'd think you need only do the flush
2241          * when VTEXT is on, but VTEXT is turned off when the text object
2242          * is freed, while pages are left lying around in memory marked
2243          * with this vnode.  If we would reactivate (create a new text
2244          * object from) this vnode, we could easily stumble upon some of
2245          * these old pages in pagein.  So, we always flush these guys.
2246          * Sun has a wonderful lack of useful invariants in this system.
2247          *
2248          * avc->flushDV is the data version # of the file at the last text
2249          * flush.  Clearly, at least, we don't have to flush the file more
2250          * often than it changes
2251          */
2252         if (hcmp(avc->flushDV, avc->m.DataVersion) < 0) {
2253             /*
2254              * By here, the cache entry is always write-locked.  We can
2255              * deadlock if we call osi_Flush with the cache entry locked...
2256              * Unlock the dcache too.
2257              */
2258             ReleaseWriteLock(&tdc->lock);
2259             if (setLocks && !slowPass)
2260                 ReleaseReadLock(&avc->lock);
2261             else
2262                 ReleaseWriteLock(&avc->lock);
2263
2264             osi_FlushText(avc);
2265             /*
2266              * Call osi_FlushPages in open, read/write, and map, since it
2267              * is too hard here to figure out if we should lock the
2268              * pvnLock.
2269              */
2270             if (setLocks && !slowPass)
2271                 ObtainReadLock(&avc->lock);
2272             else
2273                 ObtainWriteLock(&avc->lock, 66);
2274             ObtainWriteLock(&tdc->lock, 610);
2275         }
2276
2277         /*
2278          * Locks held:
2279          * avc->lock(R) if setLocks && !slowPass
2280          * avc->lock(W) if !setLocks || slowPass
2281          * tdc->lock(W)
2282          */
2283
2284         /* Watch for standard race condition around osi_FlushText */
2285         if (hsame(avc->m.DataVersion, tdc->f.versionNo)) {
2286             updateV2DC(setLocks, avc, tdc, 569);        /* set hint */
2287             afs_stats_cmperf.dcacheHits++;
2288             ConvertWToSLock(&tdc->lock);
2289             goto done;
2290         }
2291
2292         /* Sleep here when cache needs to be drained. */
2293         if (setLocks && !slowPass
2294             && (afs_blocksUsed >
2295                 PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks))) {
2296             /* Make sure truncate daemon is running */
2297             afs_MaybeWakeupTruncateDaemon();
2298             ObtainWriteLock(&tdc->tlock, 614);
2299             tdc->refCount--;    /* we'll re-obtain the dcache when we re-try. */
2300             ReleaseWriteLock(&tdc->tlock);
2301             ReleaseWriteLock(&tdc->lock);
2302             ReleaseReadLock(&avc->lock);
2303             while ((afs_blocksUsed - afs_blocksDiscarded) >
2304                    PERCENT(CM_WAITFORDRAINPCT, afs_cacheBlocks)) {
2305                 afs_WaitForCacheDrain = 1;
2306                 afs_osi_Sleep(&afs_WaitForCacheDrain);
2307             }
2308             afs_MaybeFreeDiscardedDCache();
2309             /* need to check if someone else got the chunk first. */
2310             goto RetryGetDCache;
2311         }
2312
2313         /* Do not fetch data beyond truncPos. */
2314         maxGoodLength = avc->m.Length;
2315         if (avc->truncPos < maxGoodLength)
2316             maxGoodLength = avc->truncPos;
2317         Position = AFS_CHUNKBASE(abyte);
2318         if (vType(avc) == VDIR) {
2319             size = avc->m.Length;
2320             if (size > tdc->f.chunkBytes) {
2321                 /* pre-reserve space for file */
2322                 afs_AdjustSize(tdc, size);
2323             }
2324             size = 999999999;   /* max size for transfer */
2325         } else {
2326             size = AFS_CHUNKSIZE(abyte);        /* expected max size */
2327             /* don't read past end of good data on server */
2328             if (Position + size > maxGoodLength)
2329                 size = maxGoodLength - Position;
2330             if (size < 0)
2331                 size = 0;       /* Handle random races */
2332             if (size > tdc->f.chunkBytes) {
2333                 /* pre-reserve space for file */
2334                 afs_AdjustSize(tdc, size);      /* changes chunkBytes */
2335                 /* max size for transfer still in size */
2336             }
2337         }
2338         if (afs_mariner && !tdc->f.chunk)
2339             afs_MarinerLog("fetch$Fetching", avc);      /* , Position, size, afs_indexCounter ); */
2340         /*
2341          * Right now, we only have one tool, and it's a hammer.  So, we
2342          * fetch the whole file.
2343          */
2344         DZap(tdc);      /* pages in cache may be old */
2345 #if defined(LINUX_USE_FH)
2346         file = afs_CFileOpen(&tdc->f.fh, tdc->f.fh_type);
2347 #else
2348         file = afs_CFileOpen(tdc->f.inode);
2349 #endif
2350         afs_RemoveVCB(&avc->fid);
2351         tdc->f.states |= DWriting;
2352         tdc->dflags |= DFFetching;
2353         tdc->validPos = Position;       /*  which is AFS_CHUNKBASE(abyte) */
2354         if (tdc->mflags & DFFetchReq) {
2355             tdc->mflags &= ~DFFetchReq;
2356             if (afs_osi_Wakeup(&tdc->validPos) == 0)
2357                 afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
2358                            __FILE__, ICL_TYPE_INT32, __LINE__,
2359                            ICL_TYPE_POINTER, tdc, ICL_TYPE_INT32,
2360                            tdc->dflags);
2361         }
2362         tsmall =
2363             (struct tlocal1 *)osi_AllocLargeSpace(sizeof(struct tlocal1));
2364         setVcacheStatus = 0;
2365 #ifndef AFS_NOSTATS
2366         /*
2367          * Remember if we are doing the reading from a replicated volume,
2368          * and how many times we've zipped around the fetch/analyze loop.
2369          */
2370         fromReplica = (avc->states & CRO) ? 1 : 0;
2371         numFetchLoops = 0;
2372         accP = &(afs_stats_cmfullperf.accessinf);
2373         if (fromReplica)
2374             (accP->replicatedRefs)++;
2375         else
2376             (accP->unreplicatedRefs)++;
2377 #endif /* AFS_NOSTATS */
2378         /* this is a cache miss */
2379         afs_Trace4(afs_iclSetp, CM_TRACE_FETCHPROC, ICL_TYPE_POINTER, avc,
2380                    ICL_TYPE_FID, &(avc->fid), ICL_TYPE_OFFSET,
2381                    ICL_HANDLE_OFFSET(Position), ICL_TYPE_INT32, size);
2382
2383         if (size)
2384             afs_stats_cmperf.dcacheMisses++;
2385         code = 0;
2386         /*
2387          * Dynamic root support:  fetch data from local memory.
2388          */
2389         if (afs_IsDynroot(avc)) {
2390             char *dynrootDir;
2391             int dynrootLen;
2392
2393             afs_GetDynroot(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2394
2395             dynrootDir += Position;
2396             dynrootLen -= Position;
2397             if (size > dynrootLen)
2398                 size = dynrootLen;
2399             if (size < 0)
2400                 size = 0;
2401             code = afs_CFileWrite(file, 0, dynrootDir, size);
2402             afs_PutDynroot();
2403
2404             if (code == size)
2405                 code = 0;
2406             else
2407                 code = -1;
2408
2409             tdc->validPos = Position + size;
2410             afs_CFileTruncate(file, size);      /* prune it */
2411         } else if (afs_IsDynrootMount(avc)) {
2412             char *dynrootDir;
2413             int dynrootLen;
2414
2415             afs_GetDynrootMount(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2416
2417             dynrootDir += Position;
2418             dynrootLen -= Position;
2419             if (size > dynrootLen)
2420                 size = dynrootLen;
2421             if (size < 0)
2422                 size = 0;
2423             code = afs_CFileWrite(file, 0, dynrootDir, size);
2424             afs_PutDynroot();
2425
2426             if (code == size)
2427                 code = 0;
2428             else
2429                 code = -1;
2430
2431             tdc->validPos = Position + size;
2432             afs_CFileTruncate(file, size);      /* prune it */
2433         } else if (afs_IsDynrootMount(avc)) {
2434             char *dynrootDir;
2435             int dynrootLen;
2436
2437             afs_GetDynrootMount(&dynrootDir, &dynrootLen, &tsmall->OutStatus);
2438
2439             dynrootDir += Position;
2440             dynrootLen -= Position;
2441             if (size > dynrootLen)
2442                 size = dynrootLen;
2443             if (size < 0)
2444                 size = 0;
2445             code = afs_CFileWrite(file, 0, dynrootDir, size);
2446             afs_PutDynroot();
2447
2448             if (code == size)
2449                 code = 0;
2450             else
2451                 code = -1;
2452
2453             tdc->validPos = Position + size;
2454             afs_CFileTruncate(file, size);      /* prune it */
2455         } else
2456             /*
2457              * Not a dynamic vnode:  do the real fetch.
2458              */
2459             do {
2460                 /*
2461                  * Locks held:
2462                  * avc->lock(R) if setLocks && !slowPass
2463                  * avc->lock(W) if !setLocks || slowPass
2464                  * tdc->lock(W)
2465                  */
2466
2467                 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
2468                 if (tc) {
2469                     afs_int32 length_hi, length, bytes;
2470 #ifndef AFS_NOSTATS
2471                     numFetchLoops++;
2472                     if (fromReplica)
2473                         (accP->numReplicasAccessed)++;
2474
2475 #endif /* AFS_NOSTATS */
2476                     if (!setLocks || slowPass) {
2477                         avc->callback = tc->srvr->server;
2478                     } else {
2479                         newCallback = tc->srvr->server;
2480                         setNewCallback = 1;
2481                     }
2482                     i = osi_Time();
2483                     RX_AFS_GUNLOCK();
2484                     tcall = rx_NewCall(tc->id);
2485                     RX_AFS_GLOCK();
2486
2487                     XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHDATA);
2488 #ifdef AFS_64BIT_CLIENT
2489                     length_hi = code = 0;
2490                     if (!afs_serverHasNo64Bit(tc)) {
2491                         tsize = size;
2492                         RX_AFS_GUNLOCK();
2493                         code =
2494                             StartRXAFS_FetchData64(tcall,
2495                                                    (struct AFSFid *)&avc->fid.
2496                                                    Fid, Position, tsize);
2497                         if (code != 0) {
2498                             RX_AFS_GLOCK();
2499                             afs_Trace2(afs_iclSetp, CM_TRACE_FETCH64CODE,
2500                                        ICL_TYPE_POINTER, avc, ICL_TYPE_INT32,
2501                                        code);
2502                         } else {
2503                             bytes =
2504                                 rx_Read(tcall, (char *)&length_hi,
2505                                         sizeof(afs_int32));
2506                             RX_AFS_GLOCK();
2507                             if (bytes == sizeof(afs_int32)) {
2508                                 length_hi = ntohl(length_hi);
2509                             } else {
2510                                 length_hi = 0;
2511                                 code = rx_Error(tcall);
2512                                 RX_AFS_GUNLOCK();
2513                                 code1 = rx_EndCall(tcall, code);
2514                                 RX_AFS_GLOCK();
2515                                 tcall = (struct rx_call *)0;
2516                             }
2517                         }
2518                     }
2519                     if (code == RXGEN_OPCODE || afs_serverHasNo64Bit(tc)) {
2520                         if (Position > 0x7FFFFFFF) {
2521                             code = EFBIG;
2522                         } else {
2523                             afs_int32 pos;
2524                             pos = Position;
2525                             RX_AFS_GUNLOCK();
2526                             if (!tcall)
2527                                 tcall = rx_NewCall(tc->id);
2528                             code =
2529                                 StartRXAFS_FetchData(tcall, (struct AFSFid *)
2530                                                      &avc->fid.Fid, pos,
2531                                                      size);
2532                             RX_AFS_GLOCK();
2533                         }
2534                         afs_serverSetNo64Bit(tc);
2535                     }
2536                     if (code == 0) {
2537                         RX_AFS_GUNLOCK();
2538                         bytes =
2539                             rx_Read(tcall, (char *)&length,
2540                                     sizeof(afs_int32));
2541                         RX_AFS_GLOCK();
2542                         if (bytes == sizeof(afs_int32)) {
2543                             length = ntohl(length);
2544                         } else {
2545                             code = rx_Error(tcall);
2546                         }
2547                     }
2548                     FillInt64(lengthFound, length_hi, length);
2549                     afs_Trace3(afs_iclSetp, CM_TRACE_FETCH64LENG,
2550                                ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code,
2551                                ICL_TYPE_OFFSET,
2552                                ICL_HANDLE_OFFSET(lengthFound));
2553 #else /* AFS_64BIT_CLIENT */
2554                     RX_AFS_GUNLOCK();
2555                     code =
2556                         StartRXAFS_FetchData(tcall,
2557                                              (struct AFSFid *)&avc->fid.Fid,
2558                                              Position, size);
2559                     RX_AFS_GLOCK();
2560                     if (code == 0) {
2561                         RX_AFS_GUNLOCK();
2562                         bytes =
2563                             rx_Read(tcall, (char *)&length,
2564                                     sizeof(afs_int32));
2565                         RX_AFS_GLOCK();
2566                         if (bytes == sizeof(afs_int32)) {
2567                             length = ntohl(length);
2568                         } else {
2569                             code = rx_Error(tcall);
2570                         }
2571                     }
2572 #endif /* AFS_64BIT_CLIENT */
2573                     if (code == 0) {
2574
2575 #ifndef AFS_NOSTATS
2576                         xferP =
2577                             &(afs_stats_cmfullperf.rpc.
2578                               fsXferTimes[AFS_STATS_FS_XFERIDX_FETCHDATA]);
2579                         osi_GetuTime(&xferStartTime);
2580
2581                         code =
2582                             afs_CacheFetchProc(tcall, file,
2583                                                (afs_size_t) Position, tdc,
2584                                                avc, &bytesToXfer,
2585                                                &bytesXferred, length);
2586
2587                         osi_GetuTime(&xferStopTime);
2588                         (xferP->numXfers)++;
2589                         if (!code) {
2590                             (xferP->numSuccesses)++;
2591                             afs_stats_XferSumBytes
2592                                 [AFS_STATS_FS_XFERIDX_FETCHDATA] +=
2593                                 bytesXferred;
2594                             (xferP->sumBytes) +=
2595                                 (afs_stats_XferSumBytes
2596                                  [AFS_STATS_FS_XFERIDX_FETCHDATA] >> 10);
2597                             afs_stats_XferSumBytes
2598                                 [AFS_STATS_FS_XFERIDX_FETCHDATA] &= 0x3FF;
2599                             if (bytesXferred < xferP->minBytes)
2600                                 xferP->minBytes = bytesXferred;
2601                             if (bytesXferred > xferP->maxBytes)
2602                                 xferP->maxBytes = bytesXferred;
2603
2604                             /*
2605                              * Tally the size of the object.  Note: we tally the actual size,
2606                              * NOT the number of bytes that made it out over the wire.
2607                              */
2608                             if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
2609                                 (xferP->count[0])++;
2610                             else if (bytesToXfer <=
2611                                      AFS_STATS_MAXBYTES_BUCKET1)
2612                                 (xferP->count[1])++;
2613                             else if (bytesToXfer <=
2614                                      AFS_STATS_MAXBYTES_BUCKET2)
2615                                 (xferP->count[2])++;
2616                             else if (bytesToXfer <=
2617                                      AFS_STATS_MAXBYTES_BUCKET3)
2618                                 (xferP->count[3])++;
2619                             else if (bytesToXfer <=
2620                                      AFS_STATS_MAXBYTES_BUCKET4)
2621                                 (xferP->count[4])++;
2622                             else if (bytesToXfer <=
2623                                      AFS_STATS_MAXBYTES_BUCKET5)
2624                                 (xferP->count[5])++;
2625                             else if (bytesToXfer <=
2626                                      AFS_STATS_MAXBYTES_BUCKET6)
2627                                 (xferP->count[6])++;
2628                             else if (bytesToXfer <=
2629                                      AFS_STATS_MAXBYTES_BUCKET7)
2630                                 (xferP->count[7])++;
2631                             else
2632                                 (xferP->count[8])++;
2633
2634                             afs_stats_GetDiff(elapsedTime, xferStartTime,
2635                                               xferStopTime);
2636                             afs_stats_AddTo((xferP->sumTime), elapsedTime);
2637                             afs_stats_SquareAddTo((xferP->sqrTime),
2638                                                   elapsedTime);
2639                             if (afs_stats_TimeLessThan
2640                                 (elapsedTime, (xferP->minTime))) {
2641                                 afs_stats_TimeAssign((xferP->minTime),
2642                                                      elapsedTime);
2643                             }
2644                             if (afs_stats_TimeGreaterThan
2645                                 (elapsedTime, (xferP->maxTime))) {
2646                                 afs_stats_TimeAssign((xferP->maxTime),
2647                                                      elapsedTime);
2648                             }
2649                         }
2650 #else
2651                         code =
2652                             afs_CacheFetchProc(tcall, file, Position, tdc,
2653                                                avc, 0, 0, length);
2654 #endif /* AFS_NOSTATS */
2655                     }
2656                     if (code == 0) {
2657                         RX_AFS_GUNLOCK();
2658                         code =
2659                             EndRXAFS_FetchData(tcall, &tsmall->OutStatus,
2660                                                &tsmall->CallBack,
2661                                                &tsmall->tsync);
2662                         RX_AFS_GLOCK();
2663                     }
2664                     XSTATS_END_TIME;
2665                     RX_AFS_GUNLOCK();
2666                     if (tcall)
2667                         code1 = rx_EndCall(tcall, code);
2668                     RX_AFS_GLOCK();
2669                 } else {
2670                     code = -1;
2671                 }
2672                 if (!code && code1)
2673                     code = code1;
2674
2675                 if (code == 0) {
2676                     /* callback could have been broken (or expired) in a race here, 
2677                      * but we return the data anyway.  It's as good as we knew about
2678                      * when we started. */
2679                     /* 
2680                      * validPos is updated by CacheFetchProc, and can only be 
2681                      * modifed under a dcache write lock, which we've blocked out 
2682                      */
2683                     size = tdc->validPos - Position;    /* actual segment size */
2684                     if (size < 0)
2685                         size = 0;
2686                     afs_CFileTruncate(file, size);      /* prune it */
2687                 } else {
2688                     if (!setLocks || slowPass) {
2689                         ObtainWriteLock(&afs_xcbhash, 453);
2690                         afs_DequeueCallback(avc);
2691                         avc->states &= ~(CStatd | CUnique);
2692                         avc->callback = NULL;
2693                         ReleaseWriteLock(&afs_xcbhash);
2694                         if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
2695                             osi_dnlc_purgedp(avc);
2696                     } else {
2697                         /* Something lost.  Forget about performance, and go
2698                          * back with a vcache write lock.
2699                          */
2700                         afs_CFileTruncate(file, 0);
2701                         afs_AdjustSize(tdc, 0);
2702                         afs_CFileClose(file);
2703                         osi_FreeLargeSpace(tsmall);
2704                         tsmall = 0;
2705                         ReleaseWriteLock(&tdc->lock);
2706                         afs_PutDCache(tdc);
2707                         tdc = 0;
2708                         ReleaseReadLock(&avc->lock);
2709                         slowPass = 1;
2710                         goto RetryGetDCache;
2711                     }
2712                 }
2713
2714             } while (afs_Analyze
2715                      (tc, code, &avc->fid, areq,
2716                       AFS_STATS_FS_RPCIDX_FETCHDATA, SHARED_LOCK, NULL));
2717
2718         /*
2719          * Locks held:
2720          * avc->lock(R) if setLocks && !slowPass
2721          * avc->lock(W) if !setLocks || slowPass
2722          * tdc->lock(W)
2723          */
2724
2725 #ifndef AFS_NOSTATS
2726         /*
2727          * In the case of replicated access, jot down info on the number of
2728          * attempts it took before we got through or gave up.
2729          */
2730         if (fromReplica) {
2731             if (numFetchLoops <= 1)
2732                 (accP->refFirstReplicaOK)++;
2733             if (numFetchLoops > accP->maxReplicasPerRef)
2734                 accP->maxReplicasPerRef = numFetchLoops;
2735         }
2736 #endif /* AFS_NOSTATS */
2737
2738         tdc->dflags &= ~DFFetching;
2739         if (afs_osi_Wakeup(&tdc->validPos) == 0)
2740             afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAKE, ICL_TYPE_STRING,
2741                        __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER,
2742                        tdc, ICL_TYPE_INT32, tdc->dflags);
2743         if (avc->execsOrWriters == 0)
2744             tdc->f.states &= ~DWriting;
2745
2746         /* now, if code != 0, we have an error and should punt.
2747          * note that we have the vcache write lock, either because
2748          * !setLocks or slowPass.
2749          */
2750         if (code) {
2751             afs_CFileTruncate(file, 0);
2752             afs_AdjustSize(tdc, 0);
2753             afs_CFileClose(file);
2754             ZapDCE(tdc);        /* sets DFEntryMod */
2755             if (vType(avc) == VDIR) {
2756                 DZap(tdc);
2757             }
2758             tdc->f.states &= ~(DRO|DBackup|DRW);
2759             afs_DCMoveBucket(tdc, 0, 0);
2760             ReleaseWriteLock(&tdc->lock);
2761             afs_PutDCache(tdc);
2762             if (!afs_IsDynroot(avc)) {
2763                 ObtainWriteLock(&afs_xcbhash, 454);
2764                 afs_DequeueCallback(avc);
2765                 avc->states &= ~(CStatd | CUnique);
2766                 ReleaseWriteLock(&afs_xcbhash);
2767                 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
2768                     osi_dnlc_purgedp(avc);
2769                 /*
2770                  * Locks held:
2771                  * avc->lock(W); assert(!setLocks || slowPass)
2772                  */
2773                 osi_Assert(!setLocks || slowPass);
2774             }
2775             tdc->f.states &= ~(DRO|DBackup|DRW);
2776             afs_DCMoveBucket(tdc, 0, 0);
2777             tdc = NULL;
2778             goto done;
2779         }
2780
2781         /* otherwise we copy in the just-fetched info */
2782         afs_CFileClose(file);
2783         afs_AdjustSize(tdc, size);      /* new size */
2784         /*
2785          * Copy appropriate fields into vcache.  Status is
2786          * copied later where we selectively acquire the
2787          * vcache write lock.
2788          */
2789         if (slowPass)
2790             afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2791         else
2792             setVcacheStatus = 1;
2793         hset64(tdc->f.versionNo, tsmall->OutStatus.dataVersionHigh,
2794                tsmall->OutStatus.DataVersion);
2795         tdc->dflags |= DFEntryMod;
2796         afs_indexFlags[tdc->index] |= IFEverUsed;
2797         ConvertWToSLock(&tdc->lock);
2798     } /*Data version numbers don't match */
2799     else {
2800         /*
2801          * Data version numbers match.
2802          */
2803         afs_stats_cmperf.dcacheHits++;
2804     }                           /*Data version numbers match */
2805
2806     updateV2DC(setLocks, avc, tdc, 335);        /* set hint */
2807   done:
2808     /*
2809      * Locks held:
2810      * avc->lock(R) if setLocks && !slowPass
2811      * avc->lock(W) if !setLocks || slowPass
2812      * tdc->lock(S) if tdc
2813      */
2814
2815     /*
2816      * See if this was a reference to a file in the local cell.
2817      */
2818     if (afs_IsPrimaryCellNum(avc->fid.Cell))
2819         afs_stats_cmperf.dlocalAccesses++;
2820     else
2821         afs_stats_cmperf.dremoteAccesses++;
2822
2823     /* Fix up LRU info */
2824
2825     if (tdc) {
2826         MObtainWriteLock(&afs_xdcache, 602);
2827         hset(afs_indexTimes[tdc->index], afs_indexCounter);
2828         hadd32(afs_indexCounter, 1);
2829         MReleaseWriteLock(&afs_xdcache);
2830
2831         /* return the data */
2832         if (vType(avc) == VDIR)
2833             *aoffset = abyte;
2834         else
2835             *aoffset = AFS_CHUNKOFFSET(abyte);
2836         *alen = (tdc->f.chunkBytes - *aoffset);
2837         ReleaseSharedLock(&tdc->lock);
2838     }
2839
2840     /*
2841      * Locks held:
2842      * avc->lock(R) if setLocks && !slowPass
2843      * avc->lock(W) if !setLocks || slowPass
2844      */
2845
2846     /* Fix up the callback and status values in the vcache */
2847     doVcacheUpdate = 0;
2848     if (setLocks && !slowPass) {
2849         /* DCLOCKXXX
2850          *
2851          * This is our dirty little secret to parallel fetches.
2852          * We don't write-lock the vcache while doing the fetch,
2853          * but potentially we'll need to update the vcache after
2854          * the fetch is done.
2855          *
2856          * Drop the read lock and try to re-obtain the write
2857          * lock.  If the vcache still has the same DV, it's
2858          * ok to go ahead and install the new data.
2859          */
2860         afs_hyper_t currentDV, statusDV;
2861
2862         hset(currentDV, avc->m.DataVersion);
2863
2864         if (setNewCallback && avc->callback != newCallback)
2865             doVcacheUpdate = 1;
2866
2867         if (tsmall) {
2868             hset64(statusDV, tsmall->OutStatus.dataVersionHigh,
2869                    tsmall->OutStatus.DataVersion);
2870
2871             if (setVcacheStatus && avc->m.Length != tsmall->OutStatus.Length)
2872                 doVcacheUpdate = 1;
2873             if (setVcacheStatus && !hsame(currentDV, statusDV))
2874                 doVcacheUpdate = 1;
2875         }
2876
2877         ReleaseReadLock(&avc->lock);
2878
2879         if (doVcacheUpdate) {
2880             ObtainWriteLock(&avc->lock, 615);
2881             if (!hsame(avc->m.DataVersion, currentDV)) {
2882                 /* We lose.  Someone will beat us to it. */
2883                 doVcacheUpdate = 0;
2884                 ReleaseWriteLock(&avc->lock);
2885             }
2886         }
2887     }
2888
2889     /* With slow pass, we've already done all the updates */
2890     if (slowPass) {
2891         ReleaseWriteLock(&avc->lock);
2892     }
2893
2894     /* Check if we need to perform any last-minute fixes with a write-lock */
2895     if (!setLocks || doVcacheUpdate) {
2896         if (setNewCallback)
2897             avc->callback = newCallback;
2898         if (tsmall && setVcacheStatus)
2899             afs_ProcessFS(avc, &tsmall->OutStatus, areq);
2900         if (setLocks)
2901             ReleaseWriteLock(&avc->lock);
2902     }
2903
2904     if (tsmall)
2905         osi_FreeLargeSpace(tsmall);
2906
2907     return tdc;
2908 }                               /*afs_GetDCache */
2909
2910
2911 /*
2912  * afs_WriteThroughDSlots
2913  *
2914  * Description:
2915  *      Sweep through the dcache slots and write out any modified
2916  *      in-memory data back on to our caching store.
2917  *
2918  * Parameters:
2919  *      None.
2920  *
2921  * Environment:
2922  *      The afs_xdcache is write-locked through this whole affair.
2923  */
2924 void
2925 afs_WriteThroughDSlots(void)
2926 {
2927     register struct dcache *tdc;
2928     register afs_int32 i, touchedit = 0;
2929
2930     struct afs_q DirtyQ, *tq;
2931
2932     AFS_STATCNT(afs_WriteThroughDSlots);
2933
2934     /*
2935      * Because of lock ordering, we can't grab dcache locks while
2936      * holding afs_xdcache.  So we enter xdcache, get a reference
2937      * for every dcache entry, and exit xdcache.
2938      */
2939     MObtainWriteLock(&afs_xdcache, 283);
2940     QInit(&DirtyQ);
2941     for (i = 0; i < afs_cacheFiles; i++) {
2942         tdc = afs_indexTable[i];
2943
2944         /* Grab tlock in case the existing refcount isn't zero */
2945         if (tdc && !(afs_indexFlags[i] & (IFFree | IFDiscarded))) {
2946             ObtainWriteLock(&tdc->tlock, 623);
2947             tdc->refCount++;
2948             ReleaseWriteLock(&tdc->tlock);
2949
2950             QAdd(&DirtyQ, &tdc->dirty);
2951         }
2952     }
2953     MReleaseWriteLock(&afs_xdcache);
2954
2955     /*
2956      * Now, for each dcache entry we found, check if it's dirty.
2957      * If so, get write-lock, get afs_xdcache, which protects
2958      * afs_cacheInodep, and flush it.  Don't forget to put back
2959      * the refcounts.
2960      */
2961
2962 #define DQTODC(q)       ((struct dcache *)(((char *) (q)) - sizeof(struct afs_q)))
2963
2964     for (tq = DirtyQ.prev; tq != &DirtyQ; tq = QPrev(tq)) {
2965         tdc = DQTODC(tq);
2966         if (tdc->dflags & DFEntryMod) {
2967             int wrLock;
2968
2969             wrLock = (0 == NBObtainWriteLock(&tdc->lock, 619));
2970
2971             /* Now that we have the write lock, double-check */
2972             if (wrLock && (tdc->dflags & DFEntryMod)) {
2973                 tdc->dflags &= ~DFEntryMod;
2974                 MObtainWriteLock(&afs_xdcache, 620);
2975                 afs_WriteDCache(tdc, 1);
2976                 MReleaseWriteLock(&afs_xdcache);
2977                 touchedit = 1;
2978             }
2979             if (wrLock)
2980                 ReleaseWriteLock(&tdc->lock);
2981         }
2982
2983         afs_PutDCache(tdc);
2984     }
2985
2986     MObtainWriteLock(&afs_xdcache, 617);
2987     if (!touchedit && (cacheDiskType != AFS_FCACHE_TYPE_MEM)) {
2988         /* Touch the file to make sure that the mtime on the file is kept
2989          * up-to-date to avoid losing cached files on cold starts because
2990          * their mtime seems old...
2991          */
2992         struct afs_fheader theader;
2993
2994         theader.magic = AFS_FHMAGIC;
2995         theader.firstCSize = AFS_FIRSTCSIZE;
2996         theader.otherCSize = AFS_OTHERCSIZE;
2997         theader.version = AFS_CI_VERSION;
2998         afs_osi_Write(afs_cacheInodep, 0, &theader, sizeof(theader));
2999     }
3000     MReleaseWriteLock(&afs_xdcache);
3001 }
3002
3003 /*
3004  * afs_MemGetDSlot
3005  *
3006  * Description:
3007  *      Return a pointer to an freshly initialized dcache entry using
3008  *      a memory-based cache.  The tlock will be read-locked.
3009  *
3010  * Parameters:
3011  *      aslot : Dcache slot to look at.
3012  *      tmpdc : Ptr to dcache entry.
3013  *
3014  * Environment:
3015  *      Must be called with afs_xdcache write-locked.
3016  */
3017
3018 struct dcache *
3019 afs_MemGetDSlot(register afs_int32 aslot, register struct dcache *tmpdc)
3020 {
3021     register struct dcache *tdc;
3022     int existing = 0;
3023
3024     AFS_STATCNT(afs_MemGetDSlot);
3025     if (CheckLock(&afs_xdcache) != -1)
3026         osi_Panic("getdslot nolock");
3027     if (aslot < 0 || aslot >= afs_cacheFiles)
3028         osi_Panic("getdslot slot %d (of %d)", aslot, afs_cacheFiles);
3029     tdc = afs_indexTable[aslot];
3030     if (tdc) {
3031         QRemove(&tdc->lruq);    /* move to queue head */
3032         QAdd(&afs_DLRU, &tdc->lruq);
3033         /* We're holding afs_xdcache, but get tlock in case refCount != 0 */
3034         ObtainWriteLock(&tdc->tlock, 624);
3035         tdc->refCount++;
3036         ConvertWToRLock(&tdc->tlock);
3037         return tdc;
3038     }
3039     if (tmpdc == NULL) {
3040         if (!afs_freeDSList)
3041             afs_GetDownDSlot(4);
3042         if (!afs_freeDSList) {
3043             /* none free, making one is better than a panic */
3044             afs_stats_cmperf.dcacheXAllocs++;   /* count in case we have a leak */
3045             tdc = (struct dcache *)afs_osi_Alloc(sizeof(struct dcache));
3046 #ifdef  KERNEL_HAVE_PIN
3047             pin((char *)tdc, sizeof(struct dcache));    /* XXX */
3048 #endif
3049         } else {
3050             tdc = afs_freeDSList;
3051             afs_freeDSList = (struct dcache *)tdc->lruq.next;
3052             existing = 1;
3053         }
3054         tdc->dflags = 0;        /* up-to-date, not in free q */
3055         tdc->mflags = 0;
3056         QAdd(&afs_DLRU, &tdc->lruq);
3057         if (tdc->lruq.prev == &tdc->lruq)
3058             osi_Panic("lruq 3");
3059     } else {
3060         tdc = tmpdc;
3061         tdc->f.states = 0;
3062     }
3063
3064     /* initialize entry */
3065     tdc->f.fid.Cell = 0;
3066     tdc->f.fid.Fid.Volume = 0;
3067     tdc->f.chunk = -1;
3068     hones(tdc->f.versionNo);
3069     tdc->f.inode = aslot;
3070     tdc->dflags |= DFEntryMod;
3071     tdc->refCount = 1;
3072     tdc->index = aslot;
3073     afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
3074
3075     if (existing) {
3076         osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
3077         osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
3078         osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
3079     }
3080
3081     RWLOCK_INIT(&tdc->lock, "dcache lock");
3082     RWLOCK_INIT(&tdc->tlock, "dcache tlock");
3083     RWLOCK_INIT(&tdc->mflock, "dcache flock");
3084     ObtainReadLock(&tdc->tlock);
3085
3086     if (tmpdc == NULL)
3087         afs_indexTable[aslot] = tdc;
3088     return tdc;
3089
3090 }                               /*afs_MemGetDSlot */
3091
3092 unsigned int last_error = 0, lasterrtime = 0;
3093
3094 /*
3095  * afs_UFSGetDSlot
3096  *
3097  * Description:
3098  *      Return a pointer to an freshly initialized dcache entry using
3099  *      a UFS-based disk cache.  The dcache tlock will be read-locked.
3100  *
3101  * Parameters:
3102  *      aslot : Dcache slot to look at.
3103  *      tmpdc : Ptr to dcache entry.
3104  *
3105  * Environment:
3106  *      afs_xdcache lock write-locked.
3107  */
3108 struct dcache *
3109 afs_UFSGetDSlot(register afs_int32 aslot, register struct dcache *tmpdc)
3110 {
3111     register afs_int32 code;
3112     register struct dcache *tdc;
3113     int existing = 0;
3114     int entryok;
3115
3116     AFS_STATCNT(afs_UFSGetDSlot);
3117     if (CheckLock(&afs_xdcache) != -1)
3118         osi_Panic("getdslot nolock");
3119     if (aslot < 0 || aslot >= afs_cacheFiles)
3120         osi_Panic("getdslot slot %d (of %d)", aslot, afs_cacheFiles);
3121     tdc = afs_indexTable[aslot];
3122     if (tdc) {
3123         QRemove(&tdc->lruq);    /* move to queue head */
3124         QAdd(&afs_DLRU, &tdc->lruq);
3125         /* Grab tlock in case refCount != 0 */
3126         ObtainWriteLock(&tdc->tlock, 625);
3127         tdc->refCount++;
3128         ConvertWToRLock(&tdc->tlock);
3129         return tdc;
3130     }
3131     /* otherwise we should read it in from the cache file */
3132     /*
3133      * If we weren't passed an in-memory region to place the file info,
3134      * we have to allocate one.
3135      */
3136     if (tmpdc == NULL) {
3137         if (!afs_freeDSList)
3138             afs_GetDownDSlot(4);
3139         if (!afs_freeDSList) {
3140             /* none free, making one is better than a panic */
3141             afs_stats_cmperf.dcacheXAllocs++;   /* count in case we have a leak */
3142             tdc = (struct dcache *)afs_osi_Alloc(sizeof(struct dcache));
3143 #ifdef  KERNEL_HAVE_PIN
3144             pin((char *)tdc, sizeof(struct dcache));    /* XXX */
3145 #endif
3146         } else {
3147             tdc = afs_freeDSList;
3148             afs_freeDSList = (struct dcache *)tdc->lruq.next;
3149             existing = 1;
3150         }
3151         tdc->dflags = 0;        /* up-to-date, not in free q */
3152         tdc->mflags = 0;
3153         QAdd(&afs_DLRU, &tdc->lruq);
3154         if (tdc->lruq.prev == &tdc->lruq)
3155             osi_Panic("lruq 3");
3156     } else {
3157         tdc = tmpdc;
3158         tdc->f.states = 0;
3159     }
3160
3161     /*
3162      * Seek to the aslot'th entry and read it in.
3163      */
3164     code =
3165         afs_osi_Read(afs_cacheInodep,
3166                      sizeof(struct fcache) * aslot +
3167                      sizeof(struct afs_fheader), (char *)(&tdc->f),
3168                      sizeof(struct fcache));
3169     entryok = 1;
3170     if (code != sizeof(struct fcache))
3171         entryok = 0;
3172     if (!afs_CellNumValid(tdc->f.fid.Cell))
3173         entryok = 0;
3174
3175     if (!entryok) {
3176         tdc->f.fid.Cell = 0;
3177         tdc->f.fid.Fid.Volume = 0;
3178         tdc->f.chunk = -1;
3179         hones(tdc->f.versionNo);
3180         tdc->dflags |= DFEntryMod;
3181 #if defined(KERNEL_HAVE_UERROR)
3182         last_error = getuerror();
3183 #endif
3184         lasterrtime = osi_Time();
3185         afs_indexUnique[aslot] = tdc->f.fid.Fid.Unique;
3186         tdc->f.states &= ~(DRO|DBackup|DRW);
3187         afs_DCMoveBucket(tdc, 0, 0);
3188     } else {
3189         if (&tdc->f != 0) {
3190             if (tdc->f.states & DRO) {
3191                 afs_DCMoveBucket(tdc, 0, 2);
3192             } else if (tdc->f.states & DBackup) {
3193                 afs_DCMoveBucket(tdc, 0, 1);
3194             } else {
3195                 afs_DCMoveBucket(tdc, 0, 1); 
3196             }
3197         } 
3198     }
3199     tdc->refCount = 1;
3200     tdc->index = aslot;
3201     if (tdc->f.chunk >= 0)
3202         tdc->validPos = AFS_CHUNKTOBASE(tdc->f.chunk) + tdc->f.chunkBytes;
3203     else
3204         tdc->validPos = 0;
3205
3206     if (existing) {
3207         osi_Assert(0 == NBObtainWriteLock(&tdc->lock, 674));
3208         osi_Assert(0 == NBObtainWriteLock(&tdc->mflock, 675));
3209         osi_Assert(0 == NBObtainWriteLock(&tdc->tlock, 676));
3210     }
3211
3212     RWLOCK_INIT(&tdc->lock, "dcache lock");
3213     RWLOCK_INIT(&tdc->tlock, "dcache tlock");
3214     RWLOCK_INIT(&tdc->mflock, "dcache flock");
3215     ObtainReadLock(&tdc->tlock);
3216
3217     /*
3218      * If we didn't read into a temporary dcache region, update the
3219      * slot pointer table.
3220      */
3221     if (tmpdc == NULL)
3222         afs_indexTable[aslot] = tdc;
3223     return tdc;
3224
3225 }                               /*afs_UFSGetDSlot */
3226
3227
3228
3229 /*!
3230  * Write a particular dcache entry back to its home in the
3231  * CacheInfo file.
3232  *
3233  * \param adc Pointer to the dcache entry to write.
3234  * \param atime If true, set the modtime on the file to the current time.
3235  *
3236  * \note Environment:
3237  *      Must be called with the afs_xdcache lock at least read-locked,
3238  *      and dcache entry at least read-locked.
3239  *      The reference count is not changed.
3240  */
3241
3242 int
3243 afs_WriteDCache(register struct dcache *adc, int atime)
3244 {
3245     register afs_int32 code;
3246
3247     if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
3248         return 0;
3249     AFS_STATCNT(afs_WriteDCache);
3250     osi_Assert(WriteLocked(&afs_xdcache));
3251     if (atime)
3252         adc->f.modTime = osi_Time();
3253     /*
3254      * Seek to the right dcache slot and write the in-memory image out to disk.
3255      */
3256     afs_cellname_write();
3257     code =
3258         afs_osi_Write(afs_cacheInodep,
3259                       sizeof(struct fcache) * adc->index +
3260                       sizeof(struct afs_fheader), (char *)(&adc->f),
3261                       sizeof(struct fcache));
3262     if (code != sizeof(struct fcache))
3263         return EIO;
3264     return 0;
3265 }
3266
3267
3268
3269 /*!
3270  * Wake up users of a particular file waiting for stores to take
3271  * place.
3272  *
3273  * \param avc Ptr to related vcache entry.
3274  *
3275  * \note Environment:
3276  *      Nothing interesting.
3277  */
3278 int
3279 afs_wakeup(register struct vcache *avc)
3280 {
3281     register int i;
3282     register struct brequest *tb;
3283     tb = afs_brs;
3284     AFS_STATCNT(afs_wakeup);
3285     for (i = 0; i < NBRS; i++, tb++) {
3286         /* if request is valid and for this file, we've found it */
3287         if (tb->refCount > 0 && avc == tb->vc) {
3288
3289             /*
3290              * If CSafeStore is on, then we don't awaken the guy
3291              * waiting for the store until the whole store has finished.
3292              * Otherwise, we do it now.  Note that if CSafeStore is on,
3293              * the BStore routine actually wakes up the user, instead
3294              * of us.
3295              * I think this is redundant now because this sort of thing
3296              * is already being handled by the higher-level code.
3297              */
3298             if ((avc->states & CSafeStore) == 0) {
3299                 tb->code = 0;
3300                 tb->flags |= BUVALID;
3301                 if (tb->flags & BUWAIT) {
3302                     tb->flags &= ~BUWAIT;
3303                     afs_osi_Wakeup(tb);
3304                 }
3305             }
3306             break;
3307         }
3308     }
3309     return 0;
3310 }
3311
3312
3313 /*!
3314  * Given a file name and inode, set up that file to be an
3315  * active member in the AFS cache.  This also involves checking
3316  * the usability of its data.
3317  *
3318  * \param afile Name of the cache file to initialize.
3319  * \param ainode Inode of the file.
3320  *
3321  * \note Environment:
3322  *      This function is called only during initialization.
3323  */
3324 int
3325 afs_InitCacheFile(char *afile, ino_t ainode)
3326 {
3327     register afs_int32 code;
3328 #if defined(AFS_LINUX22_ENV)
3329     struct dentry *filevp;
3330 #else
3331     struct vnode *filevp;
3332 #endif
3333     afs_int32 index;
3334     int fileIsBad;
3335     struct osi_file *tfile;
3336     struct osi_stat tstat;
3337     register struct dcache *tdc;
3338 #if defined(LINUX_USE_FH)
3339     int max_len = sizeof(struct fid);
3340 #endif
3341
3342     AFS_STATCNT(afs_InitCacheFile);
3343     index = afs_stats_cmperf.cacheNumEntries;
3344     if (index >= afs_cacheFiles)
3345         return EINVAL;
3346
3347     MObtainWriteLock(&afs_xdcache, 282);
3348     tdc = afs_GetDSlot(index, NULL);
3349     ReleaseReadLock(&tdc->tlock);
3350     MReleaseWriteLock(&afs_xdcache);
3351
3352     ObtainWriteLock(&tdc->lock, 621);
3353     MObtainWriteLock(&afs_xdcache, 622);
3354     if (afile) {
3355         code = gop_lookupname(afile, AFS_UIOSYS, 0, &filevp);
3356         if (code) {
3357             ReleaseWriteLock(&afs_xdcache);
3358             ReleaseWriteLock(&tdc->lock);
3359             afs_PutDCache(tdc);
3360             return code;
3361         }
3362         /*
3363          * We have a VN_HOLD on filevp.  Get the useful info out and
3364          * return.  We make use of the fact that the cache is in the
3365          * UFS file system, and just record the inode number.
3366          */
3367 #ifdef AFS_LINUX22_ENV
3368 #if defined(LINUX_USE_FH)
3369         tdc->f.fh_type = osi_get_fh(filevp, &tdc->f.fh, &max_len);
3370 #else
3371         tdc->f.inode = VTOI(filevp->d_inode)->i_number;
3372 #endif
3373 #else
3374         tdc->f.inode = afs_vnodeToInumber(filevp);
3375         AFS_RELE(filevp);
3376 #endif /* AFS_LINUX22_ENV */
3377     } else {
3378         tdc->f.inode = ainode;
3379     }
3380     fileIsBad = 0;
3381     if ((tdc->f.states & DWriting) || tdc->f.fid.Fid.Volume == 0)
3382         fileIsBad = 1;
3383 #if defined(LINUX_USE_FH)
3384     tfile = osi_UFSOpen_fh(&tdc->f.fh, tdc->f.fh_type);
3385 #else
3386     tfile = osi_UFSOpen(ainode);
3387 #endif
3388     code = afs_osi_Stat(tfile, &tstat);
3389     if (code)
3390         osi_Panic("initcachefile stat");
3391
3392     /*
3393      * If file size doesn't match the cache info file, it's probably bad.
3394      */
3395     if (tdc->f.chunkBytes != tstat.size)
3396         fileIsBad = 1;
3397     tdc->f.chunkBytes = 0;
3398
3399     /*
3400      * If file changed within T (120?) seconds of cache info file, it's
3401      * probably bad.  In addition, if slot changed within last T seconds,
3402      * the cache info file may be incorrectly identified, and so slot
3403      * may be bad.
3404      */
3405     if (cacheInfoModTime < tstat.mtime + 120)
3406         fileIsBad = 1;
3407     if (cacheInfoModTime < tdc->f.modTime + 120)
3408         fileIsBad = 1;
3409     /* In case write through is behind, make sure cache items entry is
3410      * at least as new as the chunk.
3411      */
3412     if (tdc->f.modTime < tstat.mtime)
3413         fileIsBad = 1;
3414     if (fileIsBad) {
3415         tdc->f.fid.Fid.Volume = 0;      /* not in the hash table */
3416         if (tstat.size != 0)
3417             osi_UFSTruncate(tfile, 0);
3418         tdc->f.states &= ~(DRO|DBackup|DRW);
3419         afs_DCMoveBucket(tdc, 0, 0);
3420         /* put entry in free cache slot list */
3421         afs_dvnextTbl[tdc->index] = afs_freeDCList;
3422         afs_freeDCList = index;
3423         afs_freeDCCount++;
3424         afs_indexFlags[index] |= IFFree;
3425         afs_indexUnique[index] = 0;
3426     } else {
3427         /*
3428          * We must put this entry in the appropriate hash tables.
3429          * Note that i is still set from the above DCHash call
3430          */
3431         code = DCHash(&tdc->f.fid, tdc->f.chunk);
3432         afs_dcnextTbl[tdc->index] = afs_dchashTbl[code];
3433         afs_dchashTbl[code] = tdc->index;
3434         code = DVHash(&tdc->f.fid);
3435         afs_dvnextTbl[tdc->index] = afs_dvhashTbl[code];
3436         afs_dvhashTbl[code] = tdc->index;
3437         afs_AdjustSize(tdc, tstat.size);        /* adjust to new size */
3438         if (tstat.size > 0)
3439             /* has nontrivial amt of data */
3440             afs_indexFlags[index] |= IFEverUsed;
3441         afs_stats_cmperf.cacheFilesReused++;
3442         /*
3443          * Initialize index times to file's mod times; init indexCounter
3444          * to max thereof
3445          */
3446         hset32(afs_indexTimes[index], tstat.atime);
3447         if (hgetlo(afs_indexCounter) < tstat.atime) {
3448             hset32(afs_indexCounter, tstat.atime);
3449         }
3450         afs_indexUnique[index] = tdc->f.fid.Fid.Unique;
3451     }                           /*File is not bad */
3452
3453     osi_UFSClose(tfile);
3454     tdc->f.states &= ~DWriting;
3455     tdc->dflags &= ~DFEntryMod;
3456     /* don't set f.modTime; we're just cleaning up */
3457     afs_WriteDCache(tdc, 0);
3458     ReleaseWriteLock(&afs_xdcache);
3459     ReleaseWriteLock(&tdc->lock);
3460     afs_PutDCache(tdc);
3461     afs_stats_cmperf.cacheNumEntries++;
3462     return 0;
3463 }
3464
3465
3466 /*Max # of struct dcache's resident at any time*/
3467 /*
3468  * If 'dchint' is enabled then in-memory dcache min is increased because of
3469  * crashes...
3470  */
3471 #define DDSIZE 200
3472
3473 /*!
3474  * Initialize dcache related variables.
3475  *
3476  * \param afiles
3477  * \param ablocks 
3478  * \param aDentries
3479  * \param achunk
3480  * \param aflags
3481  *
3482  */
3483 void
3484 afs_dcacheInit(int afiles, int ablocks, int aDentries, int achunk, int aflags)
3485 {
3486     register struct dcache *tdp;
3487     int i;
3488     int code;
3489
3490     afs_freeDCList = NULLIDX;
3491     afs_discardDCList = NULLIDX;
3492     afs_freeDCCount = 0;
3493     afs_freeDSList = NULL;
3494     hzero(afs_indexCounter);
3495
3496     LOCK_INIT(&afs_xdcache, "afs_xdcache");
3497
3498     /*
3499      * Set chunk size
3500      */
3501     if (achunk) {
3502         if (achunk < 0 || achunk > 30)
3503             achunk = 13;        /* Use default */
3504         AFS_SETCHUNKSIZE(achunk);
3505     }
3506
3507     if (!aDentries)
3508         aDentries = DDSIZE;
3509
3510     if (aflags & AFSCALL_INIT_MEMCACHE) {
3511         /*
3512          * Use a memory cache instead of a disk cache
3513          */
3514         cacheDiskType = AFS_FCACHE_TYPE_MEM;
3515         afs_cacheType = &afs_MemCacheOps;
3516         afiles = (afiles < aDentries) ? afiles : aDentries;     /* min */
3517         ablocks = afiles * (AFS_FIRSTCSIZE / 1024);
3518         /* ablocks is reported in 1K blocks */
3519         code = afs_InitMemCache(afiles, AFS_FIRSTCSIZE, aflags);
3520         if (code != 0) {
3521             printf("afsd: memory cache too large for available memory.\n");
3522             printf("afsd: AFS files cannot be accessed.\n\n");
3523             dcacheDisabled = 1;
3524             afiles = ablocks = 0;
3525         } else
3526             printf("Memory cache: Allocating %d dcache entries...",
3527                    aDentries);
3528     } else {
3529         cacheDiskType = AFS_FCACHE_TYPE_UFS;
3530         afs_cacheType = &afs_UfsCacheOps;
3531     }
3532
3533     if (aDentries > 512)
3534         afs_dhashsize = 2048;
3535     /* initialize hash tables */
3536     afs_dvhashTbl =
3537         (afs_int32 *) afs_osi_Alloc(afs_dhashsize * sizeof(afs_int32));
3538     afs_dchashTbl =
3539         (afs_int32 *) afs_osi_Alloc(afs_dhashsize * sizeof(afs_int32));
3540     for (i = 0; i < afs_dhashsize; i++) {
3541         afs_dvhashTbl[i] = NULLIDX;
3542         afs_dchashTbl[i] = NULLIDX;
3543     }
3544     afs_dvnextTbl = (afs_int32 *) afs_osi_Alloc(afiles * sizeof(afs_int32));
3545     afs_dcnextTbl = (afs_int32 *) afs_osi_Alloc(afiles * sizeof(afs_int32));
3546     for (i = 0; i < afiles; i++) {
3547         afs_dvnextTbl[i] = NULLIDX;
3548         afs_dcnextTbl[i] = NULLIDX;
3549     }
3550
3551     /* Allocate and zero the pointer array to the dcache entries */
3552     afs_indexTable = (struct dcache **)
3553         afs_osi_Alloc(sizeof(struct dcache *) * afiles);
3554     memset((char *)afs_indexTable, 0, sizeof(struct dcache *) * afiles);
3555     afs_indexTimes =
3556         (afs_hyper_t *) afs_osi_Alloc(afiles * sizeof(afs_hyper_t));
3557     memset((char *)afs_indexTimes, 0, afiles * sizeof(afs_hyper_t));
3558     afs_indexUnique =
3559         (afs_int32 *) afs_osi_Alloc(afiles * sizeof(afs_uint32));
3560     memset((char *)afs_indexUnique, 0, afiles * sizeof(afs_uint32));
3561     afs_indexFlags = (u_char *) afs_osi_Alloc(afiles * sizeof(u_char));
3562     memset((char *)afs_indexFlags, 0, afiles * sizeof(char));
3563
3564     /* Allocate and thread the struct dcache entries themselves */
3565     tdp = afs_Initial_freeDSList =
3566         (struct dcache *)afs_osi_Alloc(aDentries * sizeof(struct dcache));
3567     memset((char *)tdp, 0, aDentries * sizeof(struct dcache));
3568 #ifdef  KERNEL_HAVE_PIN
3569     pin((char *)afs_indexTable, sizeof(struct dcache *) * afiles);      /* XXX */
3570     pin((char *)afs_indexTimes, sizeof(afs_hyper_t) * afiles);  /* XXX */
3571     pin((char *)afs_indexFlags, sizeof(char) * afiles); /* XXX */
3572     pin((char *)afs_indexUnique, sizeof(afs_int32) * afiles);   /* XXX */
3573     pin((char *)tdp, aDentries * sizeof(struct dcache));        /* XXX */
3574     pin((char *)afs_dvhashTbl, sizeof(afs_int32) * afs_dhashsize);      /* XXX */
3575     pin((char *)afs_dchashTbl, sizeof(afs_int32) * afs_dhashsize);      /* XXX */
3576     pin((char *)afs_dcnextTbl, sizeof(afs_int32) * afiles);     /* XXX */
3577     pin((char *)afs_dvnextTbl, sizeof(afs_int32) * afiles);     /* XXX */
3578 #endif
3579
3580     afs_freeDSList = &tdp[0];
3581     for (i = 0; i < aDentries - 1; i++) {
3582         tdp[i].lruq.next = (struct afs_q *)(&tdp[i + 1]);
3583         RWLOCK_INIT(&tdp[i].lock, "dcache lock");
3584         RWLOCK_INIT(&tdp[i].tlock, "dcache tlock");
3585         RWLOCK_INIT(&tdp[i].mflock, "dcache flock");
3586     }
3587     tdp[aDentries - 1].lruq.next = (struct afs_q *)0;
3588     RWLOCK_INIT(&tdp[aDentries - 1].lock, "dcache lock");
3589     RWLOCK_INIT(&tdp[aDentries - 1].tlock, "dcache tlock");
3590     RWLOCK_INIT(&tdp[aDentries - 1].mflock, "dcache flock");
3591
3592     afs_stats_cmperf.cacheBlocksOrig = afs_stats_cmperf.cacheBlocksTotal =
3593         afs_cacheBlocks = ablocks;
3594     afs_ComputeCacheParms();    /* compute parms based on cache size */
3595
3596     afs_dcentries = aDentries;
3597     afs_blocksUsed = 0;
3598     afs_stats_cmperf.cacheBucket0_Discarded = 
3599         afs_stats_cmperf.cacheBucket1_Discarded = 
3600         afs_stats_cmperf.cacheBucket2_Discarded = 0;
3601     afs_DCSizeInit();
3602     QInit(&afs_DLRU);
3603 }
3604
3605 /*!
3606  * Shuts down the cache.
3607  *
3608  */
3609 void
3610 shutdown_dcache(void)
3611 {
3612     int i;
3613
3614     afs_osi_Free(afs_dvnextTbl, afs_cacheFiles * sizeof(afs_int32));
3615     afs_osi_Free(afs_dcnextTbl, afs_cacheFiles * sizeof(afs_int32));
3616     afs_osi_Free(afs_indexTable, afs_cacheFiles * sizeof(struct dcache *));
3617     afs_osi_Free(afs_indexTimes, afs_cacheFiles * sizeof(afs_hyper_t));
3618     afs_osi_Free(afs_indexUnique, afs_cacheFiles * sizeof(afs_uint32));
3619     afs_osi_Free(afs_indexFlags, afs_cacheFiles * sizeof(u_char));
3620     afs_osi_Free(afs_Initial_freeDSList,
3621                  afs_dcentries * sizeof(struct dcache));
3622 #ifdef  KERNEL_HAVE_PIN
3623     unpin((char *)afs_dcnextTbl, afs_cacheFiles * sizeof(afs_int32));
3624     unpin((char *)afs_dvnextTbl, afs_cacheFiles * sizeof(afs_int32));
3625     unpin((char *)afs_indexTable, afs_cacheFiles * sizeof(struct dcache *));
3626     unpin((char *)afs_indexTimes, afs_cacheFiles * sizeof(afs_hyper_t));
3627     unpin((char *)afs_indexUnique, afs_cacheFiles * sizeof(afs_uint32));
3628     unpin((u_char *) afs_indexFlags, afs_cacheFiles * sizeof(u_char));
3629     unpin(afs_Initial_freeDSList, afs_dcentries * sizeof(struct dcache));
3630 #endif
3631
3632
3633     for (i = 0; i < afs_dhashsize; i++) {
3634         afs_dvhashTbl[i] = NULLIDX;
3635         afs_dchashTbl[i] = NULLIDX;
3636     }
3637
3638     afs_osi_Free(afs_dvhashTbl, afs_dhashsize * sizeof(afs_int32));
3639     afs_osi_Free(afs_dchashTbl, afs_dhashsize * sizeof(afs_int32));
3640
3641     afs_blocksUsed = afs_dcentries = 0;
3642     afs_stats_cmperf.cacheBucket0_Discarded = 
3643         afs_stats_cmperf.cacheBucket1_Discarded = 
3644         afs_stats_cmperf.cacheBucket2_Discarded = 0;
3645     hzero(afs_indexCounter);
3646
3647     afs_freeDCCount = 0;
3648     afs_freeDCList = NULLIDX;
3649     afs_discardDCList = NULLIDX;
3650     afs_freeDSList = afs_Initial_freeDSList = 0;
3651
3652     LOCK_INIT(&afs_xdcache, "afs_xdcache");
3653     QInit(&afs_DLRU);
3654
3655 }
3656
3657 #if defined(AFS_DISCON_ENV)
3658
3659 /*!
3660  * Make a shadow copy of a dir's dcaches. It's used for disconnected
3661  * operations like remove/create/rename to keep the original directory data.
3662  * On reconnection, we can diff the original data with the server and get the
3663  * server changes and with the local data to get the local changes.
3664  *
3665  * \param avc The dir vnode.
3666  *
3667  * \return 0 for success.
3668  *
3669  * \note The only lock allowed to be set is the dir's vcache entry, and it
3670  * must be set in write mode.
3671  * \note The vcache entry must be write locked.
3672  */
3673 int afs_MakeShadowDir(struct vcache *avc)
3674 {
3675     int j, i, index, code, ret_code = 0, offset, trans_size, block;
3676     struct dcache *tdc, *new_dc = NULL;
3677     struct osi_file *tfile_src, *tfile_dst;
3678     struct VenusFid shadow_fid;
3679     char *data;
3680     int lock_held = 0;
3681
3682     /* Is this a dir? */
3683     if (vType(avc) != VDIR)
3684         return ENOTDIR;
3685
3686     /* Generate a fid for the shadow dir. */
3687     shadow_fid.Cell = avc->fid.Cell;
3688     shadow_fid.Fid.Volume = avc->fid.Fid.Volume;
3689     afs_GenShadowFid(&shadow_fid);
3690
3691     /* For each dcache, do copy it into a new fresh one. */
3692     i = DVHash(&avc->fid);
3693     for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
3694         /* Making sure that this isn't going to get locked twice. */
3695         if (!lock_held) {
3696             /* XXX: Moved it from outside of the loop.
3697              * Maybe it's not quite okay because of the use of
3698              * dvhashTbl (once) in the for statement.
3699              */
3700             ObtainWriteLock(&afs_xdcache, 716);
3701             lock_held = 1;
3702         }
3703
3704         i = afs_dvnextTbl[index];
3705         if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
3706             tdc = afs_GetDSlot(index, NULL);
3707
3708             ReleaseReadLock(&tdc->tlock);
3709
3710             if (!FidCmp(&tdc->f.fid, &avc->fid)) {
3711
3712                 /* Got a dir's dcache. */
3713                 lock_held = 0;
3714
3715                 /* Get a fresh dcache. */
3716                 new_dc = afs_AllocDCache(avc, 0, 0, &shadow_fid);
3717
3718                 /* Unlock hash for now. Don't need it during operations on the
3719                  * dcache. Oh, and we can't use it because of the locking
3720                  * hierarchy...
3721                  */
3722                 /* XXX: So much for lock ierarchy, the afs_AllocDCache doesn't
3723                  * respect it.
3724                  */
3725                 //ReleaseWriteLock(&afs_xdcache);
3726
3727                 ObtainReadLock(&tdc->lock);
3728
3729                 /* Set up the new fid. */
3730                 /* Copy interesting data from original dir dcache. */
3731                 new_dc->mflags = tdc->mflags;
3732                 new_dc->dflags = tdc->dflags;
3733                 new_dc->f.modTime = tdc->f.modTime;
3734                 new_dc->f.versionNo = tdc->f.versionNo;
3735                 new_dc->f.states = tdc->f.states;
3736                 new_dc->f.chunk= tdc->f.chunk;
3737                 new_dc->f.chunkBytes = tdc->f.chunkBytes;
3738
3739                 /*
3740                  * Now add to the two hash chains - note that i is still set
3741                  * from the above DCHash call.
3742                  */
3743                 //ObtainWriteLock(&afs_xdcache, 713);
3744
3745                 j = DCHash(&shadow_fid, 0);
3746                 afs_dcnextTbl[new_dc->index] = afs_dchashTbl[j];
3747                 afs_dchashTbl[j] = new_dc->index;
3748
3749                 j = DVHash(&shadow_fid);
3750                 afs_dvnextTbl[new_dc->index] = afs_dvhashTbl[j];
3751                 afs_dvhashTbl[j] = new_dc->index;
3752                 afs_MaybeWakeupTruncateDaemon();
3753
3754                 ReleaseWriteLock(&afs_xdcache);
3755
3756                 /* Alloc a 4k block. */
3757                 data = (char *) afs_osi_Alloc(4096);
3758                 if (!data) {
3759                     printf("afs_MakeShadowDir: could not alloc data\n");
3760                     ret_code = ENOMEM;
3761                     goto done;
3762                 }
3763
3764                 /* Open the files. */
3765                 tfile_src = afs_CFileOpen(tdc->f.inode);
3766                 tfile_dst = afs_CFileOpen(new_dc->f.inode);
3767
3768                 /* Init no of blocks to be read and offset. */
3769                 block = (tdc->f.chunkBytes / 4096);
3770                 offset = 0;
3771
3772                 /* And now copy dir dcache data into this dcache,
3773                  * 4k at a time.
3774                  */
3775                 while (block >= 0) {
3776
3777                     /* Last chunk might have less bytes to transfer. */
3778                     if (!block) {
3779                         /* Last block. */
3780                         trans_size = (tdc->f.chunkBytes % 4096);
3781                         if (!trans_size)
3782                             /* An exact no of 4k blocks. */
3783                             break;
3784                     } else
3785                         trans_size = 4096;
3786
3787                     /* Read a chunk from the dcache. */
3788                     code = afs_CFileRead(tfile_src, offset, data, trans_size);
3789                     if (code < trans_size) {
3790                         /* Can't access file, stop doing stuff and return error. */
3791                         ret_code = EIO;
3792                         break;
3793                     }
3794
3795                     /* Write it to the new dcache. */
3796                     code = afs_CFileWrite(tfile_dst, offset, data, trans_size);
3797                     if (code < trans_size) {
3798                         ret_code = EIO;
3799                         break;
3800                     }
3801
3802                     block--;
3803                     offset += 4096;
3804                 }               /* while (block) */
3805
3806                 afs_CFileClose(tfile_dst);
3807                 afs_CFileClose(tfile_src);
3808
3809                 afs_osi_Free(data, 4096);
3810
3811                 ReleaseWriteLock(&new_dc->lock);
3812                 ReleaseReadLock(&tdc->lock);
3813
3814                 afs_PutDCache(new_dc);
3815             }                   /* if dcache fid match */
3816             afs_PutDCache(tdc);
3817         }                       /* if unuiquifier match */
3818     }
3819 done:
3820     if (lock_held)
3821         ReleaseWriteLock(&afs_xdcache);
3822
3823     if (!ret_code) {
3824         if (!avc->ddirty_flags) {
3825             ObtainWriteLock(&afs_DDirtyVCListLock, 763);
3826             AFS_DISCON_ADD_DIRTY(avc);
3827             ReleaseWriteLock(&afs_DDirtyVCListLock);
3828         }
3829         avc->shVnode = shadow_fid.Fid.Vnode;
3830         avc->shUnique = shadow_fid.Fid.Unique;
3831         avc->ddirty_flags |= VDisconShadowed;
3832     }
3833
3834     return ret_code;
3835 }
3836
3837 /*!
3838  * Delete the dcaches of a shadow dir.
3839  *
3840  * \param avc The vcache containing the shadow fid.
3841  *
3842  * \note avc must be write locked.
3843  */
3844 void afs_DeleteShadowDir(struct vcache *avc)
3845 {
3846     struct dcache *tdc;
3847     struct VenusFid shadow_fid;
3848
3849     shadow_fid.Cell = avc->fid.Cell;
3850     shadow_fid.Fid.Volume = avc->fid.Fid.Volume;
3851     shadow_fid.Fid.Vnode = avc->shVnode;
3852     shadow_fid.Fid.Unique = avc->shUnique;
3853
3854     tdc = afs_FindDCacheByFid(&shadow_fid);
3855     if (tdc) {
3856         afs_HashOutDCache(tdc, 1);
3857         afs_DiscardDCache(tdc);
3858         afs_PutDCache(tdc);
3859     }
3860     /* Remove shadowed dir flag. */
3861     avc->ddirty_flags &= ~VDisconShadowed;
3862 }
3863 #endif