windows-buf-createlock-20080223
[openafs.git] / src / WINNT / afsd / cm_buf.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 /* Copyright (C) 1994 Cazamar Systems, Inc. */
11
12 #include <afs/param.h>
13 #include <afs/stds.h>
14
15 #include <windows.h>
16 #include <osi.h>
17 #include <stdio.h>
18 #include <assert.h>
19 #include <strsafe.h>
20 #include <math.h>
21
22 #include "afsd.h"
23 #include "cm_memmap.h"
24
25 #ifdef DEBUG
26 #define TRACE_BUFFER 1
27 #endif
28
29 extern void afsi_log(char *pattern, ...);
30
31 /* This module implements the buffer package used by the local transaction
32  * system (cm).  It is initialized by calling cm_Init, which calls buf_Init;
33  * it must be initalized before any of its main routines are called.
34  *
35  * Each buffer is hashed into a hash table by file ID and offset, and if its
36  * reference count is zero, it is also in a free list.
37  *
38  * There are two locks involved in buffer processing.  The global lock
39  * buf_globalLock protects all of the global variables defined in this module,
40  * the reference counts and hash pointers in the actual cm_buf_t structures,
41  * and the LRU queue pointers in the buffer structures.
42  *
43  * The mutexes in the buffer structures protect the remaining fields in the
44  * buffers, as well the data itself.
45  * 
46  * The locking hierarchy here is this:
47  * 
48  * - resv multiple simul. buffers reservation
49  * - lock buffer I/O flags
50  * - lock buffer's mutex
51  * - lock buf_globalLock
52  *
53  */
54
55 /* global debugging log */
56 osi_log_t *buf_logp = NULL;
57
58 /* Global lock protecting hash tables and free lists */
59 osi_rwlock_t buf_globalLock;
60
61 /* ptr to head of the free list (most recently used) and the
62  * tail (the guy to remove first).  We use osi_Q* functions
63  * to put stuff in buf_freeListp, and maintain the end
64  * pointer manually
65  */
66
67 /* a pointer to a list of all buffers, just so that we can find them
68  * easily for debugging, and for the incr syncer.  Locked under
69  * the global lock.
70  */
71
72 /* defaults setup; these variables may be manually assigned into
73  * before calling cm_Init, as a way of changing these defaults.
74  */
75
76 /* callouts for reading and writing data, etc */
77 cm_buf_ops_t *cm_buf_opsp;
78
79 #ifdef DISKCACHE95
80 /* for experimental disk caching support in Win95 client */
81 cm_buf_t *buf_diskFreeListp;
82 cm_buf_t *buf_diskFreeListEndp;
83 cm_buf_t *buf_diskAllp;
84 extern int cm_diskCacheEnabled;
85 #endif /* DISKCACHE95 */
86
87 /* set this to 1 when we are terminating to prevent access attempts */
88 static int buf_ShutdownFlag = 0;
89
90 void buf_HoldLocked(cm_buf_t *bp)
91 {
92     osi_assertx(bp->magic == CM_BUF_MAGIC,"incorrect cm_buf_t magic");
93     InterlockedIncrement(&bp->refCount);
94 }
95
96 /* hold a reference to an already held buffer */
97 void buf_Hold(cm_buf_t *bp)
98 {
99     lock_ObtainRead(&buf_globalLock);
100     buf_HoldLocked(bp);
101     lock_ReleaseRead(&buf_globalLock);
102 }
103
104 /* code to drop reference count while holding buf_globalLock */
105 void buf_ReleaseLocked(cm_buf_t *bp, afs_uint32 writeLocked)
106 {
107     afs_int32 refCount;
108
109     if (writeLocked)
110         lock_AssertWrite(&buf_globalLock);
111     else
112         lock_AssertRead(&buf_globalLock);
113
114     /* ensure that we're in the LRU queue if our ref count is 0 */
115     osi_assertx(bp->magic == CM_BUF_MAGIC,"incorrect cm_buf_t magic");
116
117     refCount = InterlockedDecrement(&bp->refCount);
118 #ifdef DEBUG
119     if (refCount < 0)
120         osi_panic("buf refcount 0",__FILE__,__LINE__);;
121 #else
122     osi_assertx(refCount >= 0, "cm_buf_t refCount == 0");
123 #endif
124     if (refCount == 0) {
125         /* 
126          * If we are read locked there could be a race condition
127          * with buf_Find() so we must obtain a write lock and
128          * double check that the refCount is actually zero
129          * before we remove the buffer from the LRU queue.
130          */
131         if (!writeLocked) {
132             lock_ReleaseRead(&buf_globalLock);
133             lock_ObtainWrite(&buf_globalLock);
134         }
135
136         if (bp->refCount == 0 &&
137             !(bp->flags & CM_BUF_INLRU)) {
138             osi_QAdd((osi_queue_t **) &cm_data.buf_freeListp, &bp->q);
139
140             /* watch for transition from empty to one element */
141             if (!cm_data.buf_freeListEndp)
142                 cm_data.buf_freeListEndp = cm_data.buf_freeListp;
143             bp->flags |= CM_BUF_INLRU;
144         }
145
146         if (!writeLocked) {
147             lock_ReleaseWrite(&buf_globalLock);
148             lock_ObtainRead(&buf_globalLock);
149         }
150     }
151 }       
152
153 /* release a buffer.  Buffer must be referenced, but unlocked. */
154 void buf_Release(cm_buf_t *bp)
155 {
156     afs_int32 refCount;
157
158     /* ensure that we're in the LRU queue if our ref count is 0 */
159     osi_assertx(bp->magic == CM_BUF_MAGIC,"incorrect cm_buf_t magic");
160
161     refCount = InterlockedDecrement(&bp->refCount);
162 #ifdef DEBUG
163     if (refCount < 0)
164         osi_panic("buf refcount 0",__FILE__,__LINE__);;
165 #else
166     osi_assertx(refCount >= 0, "cm_buf_t refCount == 0");
167 #endif
168     if (refCount == 0) {
169         lock_ObtainWrite(&buf_globalLock);
170         if (bp->refCount == 0 && 
171             !(bp->flags & CM_BUF_INLRU)) {
172             osi_QAdd((osi_queue_t **) &cm_data.buf_freeListp, &bp->q);
173
174             /* watch for transition from empty to one element */
175             if (!cm_data.buf_freeListEndp)
176                 cm_data.buf_freeListEndp = cm_data.buf_freeListp;
177             bp->flags |= CM_BUF_INLRU;
178         }
179         lock_ReleaseWrite(&buf_globalLock);
180     }
181 }
182
183 /* incremental sync daemon.  Writes all dirty buffers every 5000 ms */
184 void buf_IncrSyncer(long parm)
185 {
186     cm_buf_t **bpp, *bp;
187     long i;                             /* counter */
188     long wasDirty = 0;
189     cm_req_t req;
190
191     while (buf_ShutdownFlag == 0) {
192         if (!wasDirty) {
193             i = SleepEx(5000, 1);
194             if (i != 0) continue;
195         }
196
197         wasDirty = 0;
198
199         /* now go through our percentage of the buffers */
200         for (bpp = &cm_data.buf_dirtyListp; bp = *bpp; ) {
201
202             /* all dirty buffers are held when they are added to the
203              * dirty list.  No need for an additional hold.
204              */
205
206             if (bp->flags & CM_BUF_DIRTY) {
207                 /* start cleaning the buffer; don't touch log pages since
208                  * the log code counts on knowing exactly who is writing
209                  * a log page at any given instant.
210                  */
211                 cm_InitReq(&req);
212                 req.flags |= CM_REQ_NORETRY;
213                 wasDirty |= buf_CleanAsync(bp, &req);
214             }
215
216             /* the buffer may or may not have been dirty
217              * and if dirty may or may not have been cleaned
218              * successfully.  check the dirty flag again.  
219              */
220             if (!(bp->flags & CM_BUF_DIRTY)) {
221                 lock_ObtainMutex(&bp->mx);
222                 if (!(bp->flags & CM_BUF_DIRTY)) {
223                     /* remove the buffer from the dirty list */
224                     lock_ObtainWrite(&buf_globalLock);
225                     *bpp = bp->dirtyp;
226                     bp->dirtyp = NULL;
227                     if (cm_data.buf_dirtyListp == NULL)
228                         cm_data.buf_dirtyListEndp = NULL;
229                     buf_ReleaseLocked(bp, TRUE);
230                     lock_ReleaseWrite(&buf_globalLock);
231                 } else {
232                     /* advance the pointer so we don't loop forever */
233                     bpp = &bp->dirtyp;
234                 }
235                 lock_ReleaseMutex(&bp->mx);
236             } else {
237                 /* advance the pointer so we don't loop forever */
238                 bpp = &bp->dirtyp;
239             }
240         }       /* for loop over a bunch of buffers */
241     }           /* whole daemon's while loop */
242 }
243
244 long
245 buf_ValidateBuffers(void)
246 {
247     cm_buf_t * bp, *bpf, *bpa, *bpb;
248     afs_uint64 countb = 0, countf = 0, counta = 0;
249
250     if (cm_data.buf_freeListp == NULL && cm_data.buf_freeListEndp != NULL ||
251          cm_data.buf_freeListp != NULL && cm_data.buf_freeListEndp == NULL) {
252         afsi_log("cm_ValidateBuffers failure: inconsistent free list pointers");
253         fprintf(stderr, "cm_ValidateBuffers failure: inconsistent free list pointers\n");
254         return -9;                  
255     }
256
257     for (bp = cm_data.buf_freeListEndp; bp; bp=(cm_buf_t *) osi_QPrev(&bp->q)) { 
258         if (bp->magic != CM_BUF_MAGIC) {
259             afsi_log("cm_ValidateBuffers failure: bp->magic != CM_BUF_MAGIC");
260             fprintf(stderr, "cm_ValidateBuffers failure: bp->magic != CM_BUF_MAGIC\n");
261             return -1;                  
262         }
263         countb++;                                                                
264         bpb = bp;     
265
266         if (countb > cm_data.buf_nbuffers) {
267             afsi_log("cm_ValidateBuffers failure: countb > cm_data.buf_nbuffers");
268             fprintf(stderr, "cm_ValidateBuffers failure: countb > cm_data.buf_nbuffers\n");
269             return -6;                   
270         }
271     }
272
273     for (bp = cm_data.buf_freeListp; bp; bp=(cm_buf_t *) osi_QNext(&bp->q)) { 
274         if (bp->magic != CM_BUF_MAGIC) {
275             afsi_log("cm_ValidateBuffers failure: bp->magic != CM_BUF_MAGIC");
276             fprintf(stderr, "cm_ValidateBuffers failure: bp->magic != CM_BUF_MAGIC\n");
277             return -2;                  
278         }
279         countf++;                                                             
280         bpf = bp;    
281
282         if (countf > cm_data.buf_nbuffers) {
283             afsi_log("cm_ValidateBuffers failure: countf > cm_data.buf_nbuffers");
284             fprintf(stderr, "cm_ValidateBuffers failure: countf > cm_data.buf_nbuffers\n");
285             return -7;
286         }
287     }                                                                         
288                                                                               
289     for (bp = cm_data.buf_allp; bp; bp=bp->allp) {                            
290         if (bp->magic != CM_BUF_MAGIC) {
291             afsi_log("cm_ValidateBuffers failure: bp->magic != CM_BUF_MAGIC");
292             fprintf(stderr, "cm_ValidateBuffers failure: bp->magic != CM_BUF_MAGIC\n");
293             return -3;                  
294         }
295         counta++;                                                             
296         bpa = bp;                                                             
297
298         if (counta > cm_data.buf_nbuffers) {
299             afsi_log("cm_ValidateBuffers failure: counta > cm_data.buf_nbuffers");
300             fprintf(stderr, "cm_ValidateBuffers failure: counta > cm_data.buf_nbuffers\n");
301             return -8;                   
302         }
303     }                                                                         
304                                                                               
305     if (countb != countf) {
306         afsi_log("cm_ValidateBuffers failure: countb != countf");
307         fprintf(stderr, "cm_ValidateBuffers failure: countb != countf\n");
308         return -4;         
309     }
310                                                                               
311     if (counta != cm_data.buf_nbuffers) {
312         afsi_log("cm_ValidateBuffers failure: counta != cm_data.buf_nbuffers");
313         fprintf(stderr, "cm_ValidateBuffers failure: counta != cm_data.buf_nbuffers\n");
314         return -5;                       
315     }
316                                                                               
317     return 0;                                                                 
318 }
319
320 void buf_Shutdown(void)  
321 {                        
322     buf_ShutdownFlag = 1;
323 }                        
324
325 /* initialize the buffer package; called with no locks
326  * held during the initialization phase.
327  */
328 long buf_Init(int newFile, cm_buf_ops_t *opsp, afs_uint64 nbuffers)
329 {
330     static osi_once_t once;
331     cm_buf_t *bp;
332     thread_t phandle;
333     long i;
334     unsigned long pid;
335     char *data;
336
337     if ( newFile ) {
338         if (nbuffers) 
339             cm_data.buf_nbuffers = nbuffers;
340
341         /* Have to be able to reserve a whole chunk */
342         if (((cm_data.buf_nbuffers - 3) * cm_data.buf_blockSize) < cm_chunkSize)
343             return CM_ERROR_TOOFEWBUFS;
344     }
345
346     /* recall for callouts */
347     cm_buf_opsp = opsp;
348
349     if (osi_Once(&once)) {
350         /* initialize global locks */
351         lock_InitializeRWLock(&buf_globalLock, "Global buffer lock");
352
353         if ( newFile ) {
354             /* remember this for those who want to reset it */
355             cm_data.buf_nOrigBuffers = cm_data.buf_nbuffers;
356  
357             /* lower hash size to a prime number */
358             cm_data.buf_hashSize = osi_PrimeLessThan((afs_uint32)(cm_data.buf_nbuffers/7 + 1));
359  
360             /* create hash table */
361             memset((void *)cm_data.buf_scacheHashTablepp, 0, cm_data.buf_hashSize * sizeof(cm_buf_t *));
362             
363             /* another hash table */
364             memset((void *)cm_data.buf_fileHashTablepp, 0, cm_data.buf_hashSize * sizeof(cm_buf_t *));
365
366             /* create buffer headers and put in free list */
367             bp = cm_data.bufHeaderBaseAddress;
368             data = cm_data.bufDataBaseAddress;
369             cm_data.buf_allp = NULL;
370             
371             for (i=0; i<cm_data.buf_nbuffers; i++) {
372                 osi_assertx(bp >= cm_data.bufHeaderBaseAddress && bp < (cm_buf_t *)cm_data.bufDataBaseAddress, 
373                             "invalid cm_buf_t address");
374                 osi_assertx(data >= cm_data.bufDataBaseAddress && data < cm_data.bufEndOfData,
375                             "invalid cm_buf_t data address");
376                 
377                 /* allocate and zero some storage */
378                 memset(bp, 0, sizeof(cm_buf_t));
379                 bp->magic = CM_BUF_MAGIC;
380                 /* thread on list of all buffers */
381                 bp->allp = cm_data.buf_allp;
382                 cm_data.buf_allp = bp;
383                 
384                 osi_QAdd((osi_queue_t **)&cm_data.buf_freeListp, &bp->q);
385                 bp->flags |= CM_BUF_INLRU;
386                 lock_InitializeMutex(&bp->mx, "Buffer mutex");
387                 
388                 /* grab appropriate number of bytes from aligned zone */
389                 bp->datap = data;
390                 
391                 /* setup last buffer pointer */
392                 if (i == 0)
393                     cm_data.buf_freeListEndp = bp;
394                     
395                 /* next */
396                 bp++;
397                 data += cm_data.buf_blockSize;
398             }       
399  
400             /* none reserved at first */
401             cm_data.buf_reservedBufs = 0;
402  
403             /* just for safety's sake */
404             cm_data.buf_maxReservedBufs = cm_data.buf_nbuffers - 3;
405         } else {
406             bp = cm_data.bufHeaderBaseAddress;
407             data = cm_data.bufDataBaseAddress;
408             
409             for (i=0; i<cm_data.buf_nbuffers; i++) {
410                 lock_InitializeMutex(&bp->mx, "Buffer mutex");
411                 bp->userp = NULL;
412                 bp->waitCount = 0;
413                 bp->waitRequests = 0;
414                 bp->flags &= ~CM_BUF_WAITING;
415                 bp++;
416             }       
417         }
418  
419 #ifdef TESTING
420         buf_ValidateBufQueues();
421 #endif /* TESTING */
422
423 #ifdef TRACE_BUFFER
424         /* init the buffer trace log */
425         buf_logp = osi_LogCreate("buffer", 1000);
426         osi_LogEnable(buf_logp);
427 #endif
428
429         osi_EndOnce(&once);
430
431         /* and create the incr-syncer */
432         phandle = thrd_Create(0, 0,
433                                (ThreadFunc) buf_IncrSyncer, 0, 0, &pid,
434                                "buf_IncrSyncer");
435
436         osi_assertx(phandle != NULL, "buf: can't create incremental sync proc");
437         CloseHandle(phandle);
438     }
439
440 #ifdef TESTING
441     buf_ValidateBufQueues();
442 #endif /* TESTING */
443     return 0;
444 }
445
446 /* add nbuffers to the buffer pool, if possible.
447  * Called with no locks held.
448  */
449 long buf_AddBuffers(afs_uint64 nbuffers)
450 {
451     /* The size of a virtual cache cannot be changed after it has
452      * been created.  Subsequent calls to MapViewofFile() with
453      * an existing mapping object name would not allow the 
454      * object to be resized.  Return failure immediately.
455      *
456      * A similar problem now occurs with the persistent cache
457      * given that the memory mapped file now contains a complex
458      * data structure.
459      */
460     afsi_log("request to add %d buffers to the existing cache of size %d denied",
461               nbuffers, cm_data.buf_nbuffers);
462
463     return CM_ERROR_INVAL;
464 }       
465
466 /* interface to set the number of buffers to an exact figure.
467  * Called with no locks held.
468  */
469 long buf_SetNBuffers(afs_uint64 nbuffers)
470 {
471     if (nbuffers < 10) 
472         return CM_ERROR_INVAL;
473     if (nbuffers == cm_data.buf_nbuffers) 
474         return 0;
475     else if (nbuffers > cm_data.buf_nbuffers)
476         return buf_AddBuffers(nbuffers - cm_data.buf_nbuffers);
477     else 
478         return CM_ERROR_INVAL;
479 }
480
481 /* wait for reading or writing to clear; called with write-locked
482  * buffer and unlocked scp and returns with locked buffer.
483  */
484 void buf_WaitIO(cm_scache_t * scp, cm_buf_t *bp)
485 {
486     int release = 0;
487
488     if (scp)
489         osi_assertx(scp->magic == CM_SCACHE_MAGIC, "invalid cm_scache_t magic");
490     osi_assertx(bp->magic == CM_BUF_MAGIC, "invalid cm_buf_t magic");
491
492     while (1) {
493         /* if no IO is happening, we're done */
494         if (!(bp->flags & (CM_BUF_READING | CM_BUF_WRITING)))
495             break;
496                 
497         /* otherwise I/O is happening, but some other thread is waiting for
498          * the I/O already.  Wait for that guy to figure out what happened,
499          * and then check again.
500          */
501         if ( bp->flags & CM_BUF_WAITING ) {
502             bp->waitCount++;
503             bp->waitRequests++;
504             osi_Log1(buf_logp, "buf_WaitIO CM_BUF_WAITING already set for 0x%p", bp);
505         } else {
506             osi_Log1(buf_logp, "buf_WaitIO CM_BUF_WAITING set for 0x%p", bp);
507             bp->flags |= CM_BUF_WAITING;
508             bp->waitCount = bp->waitRequests = 1;
509         }
510         osi_SleepM((LONG_PTR)bp, &bp->mx);
511
512         smb_UpdateServerPriority();
513
514         lock_ObtainMutex(&bp->mx);
515         osi_Log1(buf_logp, "buf_WaitIO conflict wait done for 0x%p", bp);
516         bp->waitCount--;
517         if (bp->waitCount == 0) {
518             osi_Log1(buf_logp, "buf_WaitIO CM_BUF_WAITING reset for 0x%p", bp);
519             bp->flags &= ~CM_BUF_WAITING;
520             bp->waitRequests = 0;
521         }
522
523         if ( !scp ) {
524             if (scp = cm_FindSCache(&bp->fid))
525                  release = 1;
526         }
527         if ( scp ) {
528             lock_ObtainMutex(&scp->mx);
529             if (scp->flags & CM_SCACHEFLAG_WAITING) {
530                 osi_Log1(buf_logp, "buf_WaitIO waking scp 0x%p", scp);
531                 osi_Wakeup((LONG_PTR)&scp->flags);
532             }
533             lock_ReleaseMutex(&scp->mx);
534         }
535     }
536         
537     /* if we get here, the IO is done, but we may have to wakeup people waiting for
538      * the I/O to complete.  Do so.
539      */
540     if (bp->flags & CM_BUF_WAITING) {
541         osi_Log1(buf_logp, "buf_WaitIO Waking bp 0x%p", bp);
542         osi_Wakeup((LONG_PTR) bp);
543     }
544     osi_Log1(buf_logp, "WaitIO finished wait for bp 0x%p", bp);
545
546     if (scp && release)
547         cm_ReleaseSCache(scp);
548 }
549
550 /* find a buffer, if any, for a particular file ID and offset.  Assumes
551  * that buf_globalLock is write locked when called.
552  */
553 cm_buf_t *buf_FindLocked(struct cm_scache *scp, osi_hyper_t *offsetp)
554 {
555     afs_uint32 i;
556     cm_buf_t *bp;
557
558     i = BUF_HASH(&scp->fid, offsetp);
559     for(bp = cm_data.buf_scacheHashTablepp[i]; bp; bp=bp->hashp) {
560         if (cm_FidCmp(&scp->fid, &bp->fid) == 0
561              && offsetp->LowPart == bp->offset.LowPart
562              && offsetp->HighPart == bp->offset.HighPart) {
563             buf_HoldLocked(bp);
564             break;
565         }
566     }
567         
568     /* return whatever we found, if anything */
569     return bp;
570 }
571
572 /* find a buffer with offset *offsetp for vnode *scp.  Called
573  * with no locks held.
574  */
575 cm_buf_t *buf_Find(struct cm_scache *scp, osi_hyper_t *offsetp)
576 {
577     cm_buf_t *bp;
578
579     lock_ObtainRead(&buf_globalLock);
580     bp = buf_FindLocked(scp, offsetp);
581     lock_ReleaseRead(&buf_globalLock);
582
583     return bp;
584 }       
585
586 /* start cleaning I/O on this buffer.  Buffer must be write locked, and is returned
587  * write-locked.
588  *
589  * Makes sure that there's only one person writing this block
590  * at any given time, and also ensures that the log is forced sufficiently far,
591  * if this buffer contains logged data.
592  *
593  * Returns non-zero if the buffer was dirty.
594  */
595 long buf_CleanAsyncLocked(cm_buf_t *bp, cm_req_t *reqp)
596 {
597     long code = 0;
598     long isdirty = 0;
599     cm_scache_t * scp = NULL;
600     osi_hyper_t offset;
601
602     osi_assertx(bp->magic == CM_BUF_MAGIC, "invalid cm_buf_t magic");
603
604     while ((bp->flags & CM_BUF_DIRTY) == CM_BUF_DIRTY) {
605         isdirty = 1;
606         lock_ReleaseMutex(&bp->mx);
607
608         scp = cm_FindSCache(&bp->fid);
609         if (scp) {
610             osi_Log2(buf_logp, "buf_CleanAsyncLocked starts I/O on scp 0x%p buf 0x%p", scp, bp);
611
612             offset = bp->offset;
613             LargeIntegerAdd(offset, ConvertLongToLargeInteger(bp->dirty_offset));
614             code = (*cm_buf_opsp->Writep)(scp, &offset, bp->dirty_length, 0, bp->userp, reqp);
615             osi_Log3(buf_logp, "buf_CleanAsyncLocked I/O on scp 0x%p buf 0x%p, done=%d", scp, bp, code);
616
617             cm_ReleaseSCache(scp);
618             scp = NULL;
619         } else {
620             osi_Log1(buf_logp, "buf_CleanAsyncLocked unable to start I/O - scp not found buf 0x%p", bp);
621             code = CM_ERROR_NOSUCHFILE;
622         }    
623         
624         lock_ObtainMutex(&bp->mx);
625         /* if the Write routine returns No Such File, clear the dirty flag
626          * because we aren't going to be able to write this data to the file
627          * server.
628          */
629         if (code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BADFD){
630             bp->flags &= ~CM_BUF_DIRTY;
631             bp->flags |= CM_BUF_ERROR;
632             bp->dirty_offset = 0;
633             bp->dirty_length = 0;
634             bp->error = code;
635             bp->dataVersion = -1; /* bad */
636             bp->dirtyCounter++;
637         }
638
639 #ifdef DISKCACHE95
640         /* Disk cache support */
641         /* write buffer to disk cache (synchronous for now) */
642         diskcache_Update(bp->dcp, bp->datap, cm_data.buf_blockSize, bp->dataVersion);
643 #endif /* DISKCACHE95 */
644
645         /* if we get here and retries are not permitted 
646          * then we need to exit this loop regardless of 
647          * whether or not we were able to clear the dirty bit
648          */
649         if (reqp->flags & CM_REQ_NORETRY)
650             break;
651     };
652
653     if (!(bp->flags & CM_BUF_DIRTY)) {
654         /* remove buffer from dirty buffer queue */
655
656     }
657
658     /* do logging after call to GetLastError, or else */
659         
660     /* if someone was waiting for the I/O that just completed or failed,
661      * wake them up.
662      */
663     if (bp->flags & CM_BUF_WAITING) {
664         /* turn off flags and wakeup users */
665         osi_Log1(buf_logp, "buf_WaitIO Waking bp 0x%p", bp);
666         osi_Wakeup((LONG_PTR) bp);
667     }
668     return isdirty;
669 }
670
671 /* Called with a zero-ref count buffer and with the buf_globalLock write locked.
672  * recycles the buffer, and leaves it ready for reuse with a ref count of 1.
673  * The buffer must already be clean, and no I/O should be happening to it.
674  */
675 void buf_Recycle(cm_buf_t *bp)
676 {
677     afs_uint32 i;
678     cm_buf_t **lbpp;
679     cm_buf_t *tbp;
680     cm_buf_t *prevBp, *nextBp;
681
682     osi_assertx(bp->magic == CM_BUF_MAGIC, "invalid cm_buf_t magic");
683
684     /* if we get here, we know that the buffer still has a 0 ref count,
685      * and that it is clean and has no currently pending I/O.  This is
686      * the dude to return.
687      * Remember that as long as the ref count is 0, we know that we won't
688      * have any lock conflicts, so we can grab the buffer lock out of
689      * order in the locking hierarchy.
690      */
691     osi_Log3( buf_logp, "buf_Recycle recycles 0x%p, off 0x%x:%08x",
692               bp, bp->offset.HighPart, bp->offset.LowPart);
693
694     osi_assertx(bp->refCount == 0, "cm_buf_t refcount != 0");
695     osi_assertx(!(bp->flags & (CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)),
696                 "incorrect cm_buf_t flags");
697     lock_AssertWrite(&buf_globalLock);
698
699     if (bp->flags & CM_BUF_INHASH) {
700         /* Remove from hash */
701
702         i = BUF_HASH(&bp->fid, &bp->offset);
703         lbpp = &(cm_data.buf_scacheHashTablepp[i]);
704         for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = *lbpp) {
705             if (tbp == bp) 
706                 break;
707         }
708
709         /* we better find it */
710         osi_assertx(tbp != NULL, "buf_Recycle: hash table screwup");
711
712         *lbpp = bp->hashp;      /* hash out */
713         bp->hashp = NULL;
714
715         /* Remove from file hash */
716
717         i = BUF_FILEHASH(&bp->fid);
718         prevBp = bp->fileHashBackp;
719         bp->fileHashBackp = NULL;
720         nextBp = bp->fileHashp;
721         bp->fileHashp = NULL;
722         if (prevBp)
723             prevBp->fileHashp = nextBp;
724         else
725             cm_data.buf_fileHashTablepp[i] = nextBp;
726         if (nextBp)
727             nextBp->fileHashBackp = prevBp;
728
729         bp->flags &= ~CM_BUF_INHASH;
730     }
731
732     /* bump the soft reference counter now, to invalidate softRefs; no
733      * wakeup is required since people don't sleep waiting for this
734      * counter to change.
735      */
736     bp->idCounter++;
737
738     /* make the fid unrecognizable */
739     memset(&bp->fid, 0, sizeof(cm_fid_t));
740 }       
741
742 /* recycle a buffer, removing it from the free list, hashing in its new identity
743  * and returning it write-locked so that no one can use it.  Called without
744  * any locks held, and can return an error if it loses the race condition and 
745  * finds that someone else created the desired buffer.
746  *
747  * If success is returned, the buffer is returned write-locked.
748  *
749  * May be called with null scp and offsetp, if we're just trying to reclaim some
750  * space from the buffer pool.  In that case, the buffer will be returned
751  * without being hashed into the hash table.
752  */
753 long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bufpp)
754 {
755     cm_buf_t *bp;       /* buffer we're dealing with */
756     cm_buf_t *nextBp;   /* next buffer in file hash chain */
757     afs_uint32 i;       /* temp */
758     cm_req_t req;
759
760     cm_InitReq(&req);   /* just in case */
761
762 #ifdef TESTING
763     buf_ValidateBufQueues();
764 #endif /* TESTING */
765
766     while(1) {
767       retry:
768         lock_ObtainRead(&scp->bufCreateLock);
769         lock_ObtainWrite(&buf_globalLock);
770         /* check to see if we lost the race */
771         if (scp) {
772             if (bp = buf_FindLocked(scp, offsetp)) {
773                 /* Do not call buf_ReleaseLocked() because we 
774                  * do not want to allow the buffer to be added
775                  * to the free list.
776                  */
777                 bp->refCount--;
778                 lock_ReleaseWrite(&buf_globalLock);
779                 lock_ReleaseRead(&scp->bufCreateLock);
780                 return CM_BUF_EXISTS;
781             }
782         }
783
784         /* does this fix the problem below?  it's a simple solution. */
785         if (!cm_data.buf_freeListEndp)
786         {
787             lock_ReleaseWrite(&buf_globalLock);
788             lock_ReleaseRead(&scp->bufCreateLock);
789             osi_Log0(afsd_logp, "buf_GetNewLocked: Free Buffer List is empty - sleeping 200ms");
790             Sleep(200);
791             goto retry;
792         }
793
794         /* for debugging, assert free list isn't empty, although we
795          * really should try waiting for a running tranasction to finish
796          * instead of this; or better, we should have a transaction
797          * throttler prevent us from entering this situation.
798          */
799         osi_assertx(cm_data.buf_freeListEndp != NULL, "buf_GetNewLocked: no free buffers");
800
801         /* look at all buffers in free list, some of which may temp.
802          * have high refcounts and which then should be skipped,
803          * starting cleaning I/O for those which are dirty.  If we find
804          * a clean buffer, we rehash it, lock it and return it.
805          */
806         for(bp = cm_data.buf_freeListEndp; bp; bp=(cm_buf_t *) osi_QPrev(&bp->q)) {
807             /* check to see if it really has zero ref count.  This
808              * code can bump refcounts, at least, so it may not be
809              * zero.
810              */
811             if (bp->refCount > 0) 
812                 continue;
813                         
814             /* we don't have to lock buffer itself, since the ref
815              * count is 0 and we know it will stay zero as long as
816              * we hold the global lock.
817              */
818
819             /* don't recycle someone in our own chunk */
820             if (!cm_FidCmp(&bp->fid, &scp->fid)
821                  && (bp->offset.LowPart & (-cm_chunkSize))
822                  == (offsetp->LowPart & (-cm_chunkSize)))
823                 continue;
824
825             /* if this page is being filled (!) or cleaned, see if
826              * the I/O has completed.  If not, skip it, otherwise
827              * do the final processing for the I/O.
828              */
829             if (bp->flags & (CM_BUF_READING | CM_BUF_WRITING)) {
830                 /* probably shouldn't do this much work while
831                  * holding the big lock?  Watch for contention
832                  * here.
833                  */
834                 continue;
835             }
836                         
837             if (bp->flags & CM_BUF_DIRTY) {
838                 /* if the buffer is dirty, start cleaning it and
839                  * move on to the next buffer.  We do this with
840                  * just the lock required to minimize contention
841                  * on the big lock.
842                  */
843                 buf_HoldLocked(bp);
844                 lock_ReleaseWrite(&buf_globalLock);
845                 lock_ReleaseRead(&scp->bufCreateLock);
846
847                 /* grab required lock and clean; this only
848                  * starts the I/O.  By the time we're back,
849                  * it'll still be marked dirty, but it will also
850                  * have the WRITING flag set, so we won't get
851                  * back here.
852                  */
853                 buf_CleanAsync(bp, &req);
854
855                 /* now put it back and go around again */
856                 buf_Release(bp);
857                 goto retry;
858             }
859
860             /* if we get here, we know that the buffer still has a 0
861              * ref count, and that it is clean and has no currently
862              * pending I/O.  This is the dude to return.
863              * Remember that as long as the ref count is 0, we know
864              * that we won't have any lock conflicts, so we can grab
865              * the buffer lock out of order in the locking hierarchy.
866              */
867             buf_Recycle(bp);
868
869             /* clean up junk flags */
870             bp->flags &= ~(CM_BUF_EOF | CM_BUF_ERROR);
871             bp->dataVersion = -1;       /* unknown so far */
872
873             /* now hash in as our new buffer, and give it the
874              * appropriate label, if requested.
875              */
876             if (scp) {
877                 bp->flags |= CM_BUF_INHASH;
878                 bp->fid = scp->fid;
879 #ifdef DEBUG
880                 bp->scp = scp;
881 #endif
882                 bp->offset = *offsetp;
883                 i = BUF_HASH(&scp->fid, offsetp);
884                 bp->hashp = cm_data.buf_scacheHashTablepp[i];
885                 cm_data.buf_scacheHashTablepp[i] = bp;
886                 i = BUF_FILEHASH(&scp->fid);
887                 nextBp = cm_data.buf_fileHashTablepp[i];
888                 bp->fileHashp = nextBp;
889                 bp->fileHashBackp = NULL;
890                 if (nextBp)
891                     nextBp->fileHashBackp = bp;
892                 cm_data.buf_fileHashTablepp[i] = bp;
893             }
894
895             /* we should move it from the lru queue.  It better still be there,
896              * since we've held the global (big) lock since we found it there.
897              */
898             osi_assertx(bp->flags & CM_BUF_INLRU,
899                          "buf_GetNewLocked: LRU screwup");
900
901             if (cm_data.buf_freeListEndp == bp) {
902                 /* we're the last guy in this queue, so maintain it */
903                 cm_data.buf_freeListEndp = (cm_buf_t *) osi_QPrev(&bp->q);
904             }
905             osi_QRemove((osi_queue_t **) &cm_data.buf_freeListp, &bp->q);
906             bp->flags &= ~CM_BUF_INLRU;
907
908             /* grab the mutex so that people don't use it
909              * before the caller fills it with data.  Again, no one     
910              * should have been able to get to this dude to lock it.
911              */
912             if (!lock_TryMutex(&bp->mx)) {
913                 osi_Log2(afsd_logp, "buf_GetNewLocked bp 0x%p cannot be mutex locked.  refCount %d should be 0",
914                          bp, bp->refCount);
915                 osi_panic("buf_GetNewLocked: TryMutex failed",__FILE__,__LINE__);
916             }
917
918             /* prepare to return it.  Give it a refcount */
919             bp->refCount = 1;
920                         
921             lock_ReleaseWrite(&buf_globalLock);
922             lock_ReleaseRead(&scp->bufCreateLock);
923             *bufpp = bp;
924
925 #ifdef TESTING
926             buf_ValidateBufQueues();
927 #endif /* TESTING */
928             return 0;
929         } /* for all buffers in lru queue */
930         lock_ReleaseWrite(&buf_globalLock);
931         lock_ReleaseRead(&scp->bufCreateLock);
932         osi_Log0(afsd_logp, "buf_GetNewLocked: Free Buffer List has no buffers with a zero refcount - sleeping 100ms");
933         Sleep(100);             /* give some time for a buffer to be freed */
934     }   /* while loop over everything */
935     /* not reached */
936 } /* the proc */
937
938 /* get a page, returning it held but unlocked.  Doesn't fill in the page
939  * with I/O, since we're going to write the whole thing new.
940  */
941 long buf_GetNew(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bufpp)
942 {
943     cm_buf_t *bp;
944     long code;
945     osi_hyper_t pageOffset;
946     int created;
947
948     created = 0;
949     pageOffset.HighPart = offsetp->HighPart;
950     pageOffset.LowPart = offsetp->LowPart & ~(cm_data.buf_blockSize-1);
951     while (1) {
952         bp = buf_Find(scp, &pageOffset);
953         if (bp) {
954             /* lock it and break out */
955             lock_ObtainMutex(&bp->mx);
956             break;
957         }
958
959         /* otherwise, we have to create a page */
960         code = buf_GetNewLocked(scp, &pageOffset, &bp);
961
962         /* check if the buffer was created in a race condition branch.
963          * If so, go around so we can hold a reference to it. 
964          */
965         if (code == CM_BUF_EXISTS) 
966             continue;
967
968         /* something else went wrong */
969         if (code != 0) 
970             return code;
971
972         /* otherwise, we have a locked buffer that we just created */
973         created = 1;
974         break;
975     } /* big while loop */
976
977     /* wait for reads */
978     if (bp->flags & CM_BUF_READING)
979         buf_WaitIO(scp, bp);
980
981     /* once it has been read once, we can unlock it and return it, still
982      * with its refcount held.
983      */
984     lock_ReleaseMutex(&bp->mx);
985     *bufpp = bp;
986     osi_Log4(buf_logp, "buf_GetNew returning bp 0x%p for scp 0x%p, offset 0x%x:%08x",
987               bp, scp, offsetp->HighPart, offsetp->LowPart);
988     return 0;
989 }
990
991 /* get a page, returning it held but unlocked.  Make sure it is complete */
992 /* The scp must be unlocked when passed to this function */
993 long buf_Get(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bufpp)
994 {
995     cm_buf_t *bp;
996     long code;
997     osi_hyper_t pageOffset;
998     unsigned long tcount;
999     int created;
1000     long lcount = 0;
1001 #ifdef DISKCACHE95
1002     cm_diskcache_t *dcp;
1003 #endif /* DISKCACHE95 */
1004
1005     created = 0;
1006     pageOffset.HighPart = offsetp->HighPart;
1007     pageOffset.LowPart = offsetp->LowPart & ~(cm_data.buf_blockSize-1);
1008     while (1) {
1009         lcount++;
1010 #ifdef TESTING
1011         buf_ValidateBufQueues();
1012 #endif /* TESTING */
1013
1014         bp = buf_Find(scp, &pageOffset);
1015         if (bp) {
1016             /* lock it and break out */
1017             lock_ObtainMutex(&bp->mx);
1018
1019 #ifdef DISKCACHE95
1020             /* touch disk chunk to update LRU info */
1021             diskcache_Touch(bp->dcp);
1022 #endif /* DISKCACHE95 */
1023             break;
1024         }
1025
1026         /* otherwise, we have to create a page */
1027         code = buf_GetNewLocked(scp, &pageOffset, &bp);
1028         /* bp->mx is now held */
1029
1030         /* check if the buffer was created in a race condition branch.
1031          * If so, go around so we can hold a reference to it. 
1032          */
1033         if (code == CM_BUF_EXISTS) 
1034             continue;
1035
1036         /* something else went wrong */
1037         if (code != 0) { 
1038 #ifdef TESTING
1039             buf_ValidateBufQueues();
1040 #endif /* TESTING */
1041             return code;
1042         }
1043                 
1044         /* otherwise, we have a locked buffer that we just created */
1045         created = 1;
1046         break;
1047     } /* big while loop */
1048
1049     /* if we get here, we have a locked buffer that may have just been
1050      * created, in which case it needs to be filled with data.
1051      */
1052     if (created) {
1053         /* load the page; freshly created pages should be idle */
1054         osi_assertx(!(bp->flags & (CM_BUF_READING | CM_BUF_WRITING)), "incorrect cm_buf_t flags");
1055
1056         /* start the I/O; may drop lock */
1057         bp->flags |= CM_BUF_READING;
1058         code = (*cm_buf_opsp->Readp)(bp, cm_data.buf_blockSize, &tcount, NULL);
1059
1060 #ifdef DISKCACHE95
1061         code = diskcache_Get(&bp->fid, &bp->offset, bp->datap, cm_data.buf_blockSize, &bp->dataVersion, &tcount, &dcp);
1062         bp->dcp = dcp;    /* pointer to disk cache struct. */
1063 #endif /* DISKCACHE95 */
1064
1065         if (code != 0) {
1066             /* failure or queued */
1067             if (code != ERROR_IO_PENDING) {
1068                 bp->error = code;
1069                 bp->flags |= CM_BUF_ERROR;
1070                 bp->flags &= ~CM_BUF_READING;
1071                 if (bp->flags & CM_BUF_WAITING) {
1072                     osi_Log1(buf_logp, "buf_Get Waking bp 0x%p", bp);
1073                     osi_Wakeup((LONG_PTR) bp);
1074                 }
1075                 lock_ReleaseMutex(&bp->mx);
1076                 buf_Release(bp);
1077 #ifdef TESTING
1078                 buf_ValidateBufQueues();
1079 #endif /* TESTING */
1080                 return code;
1081             }
1082         } else {
1083             /* otherwise, I/O completed instantly and we're done, except
1084              * for padding the xfr out with 0s and checking for EOF
1085              */
1086             if (tcount < (unsigned long) cm_data.buf_blockSize) {
1087                 memset(bp->datap+tcount, 0, cm_data.buf_blockSize - tcount);
1088                 if (tcount == 0)
1089                     bp->flags |= CM_BUF_EOF;
1090             }
1091             bp->flags &= ~CM_BUF_READING;
1092             if (bp->flags & CM_BUF_WAITING) {
1093                 osi_Log1(buf_logp, "buf_Get Waking bp 0x%p", bp);
1094                 osi_Wakeup((LONG_PTR) bp);
1095             }
1096         }
1097
1098     } /* if created */
1099
1100     /* wait for reads, either that which we started above, or that someone
1101      * else started.  We don't care if we return a buffer being cleaned.
1102      */
1103     if (bp->flags & CM_BUF_READING)
1104         buf_WaitIO(scp, bp);
1105
1106     /* once it has been read once, we can unlock it and return it, still
1107      * with its refcount held.
1108      */
1109     lock_ReleaseMutex(&bp->mx);
1110     *bufpp = bp;
1111
1112     /* now remove from queue; will be put in at the head (farthest from
1113      * being recycled) when we're done in buf_Release.
1114      */
1115     lock_ObtainWrite(&buf_globalLock);
1116     if (bp->flags & CM_BUF_INLRU) {
1117         if (cm_data.buf_freeListEndp == bp)
1118             cm_data.buf_freeListEndp = (cm_buf_t *) osi_QPrev(&bp->q);
1119         osi_QRemove((osi_queue_t **) &cm_data.buf_freeListp, &bp->q);
1120         bp->flags &= ~CM_BUF_INLRU;
1121     }
1122     lock_ReleaseWrite(&buf_globalLock);
1123
1124     osi_Log4(buf_logp, "buf_Get returning bp 0x%p for scp 0x%p, offset 0x%x:%08x",
1125               bp, scp, offsetp->HighPart, offsetp->LowPart);
1126 #ifdef TESTING
1127     buf_ValidateBufQueues();
1128 #endif /* TESTING */
1129     return 0;
1130 }
1131
1132 /* count # of elements in the free list;
1133  * we don't bother doing the proper locking for accessing dataVersion or flags
1134  * since it is a pain, and this is really just an advisory call.  If you need
1135  * to do better at some point, rewrite this function.
1136  */
1137 long buf_CountFreeList(void)
1138 {
1139     long count;
1140     cm_buf_t *bufp;
1141
1142     count = 0;
1143     lock_ObtainRead(&buf_globalLock);
1144     for(bufp = cm_data.buf_freeListp; bufp; bufp = (cm_buf_t *) osi_QNext(&bufp->q)) {
1145         /* if the buffer doesn't have an identity, or if the buffer
1146          * has been invalidate (by having its DV stomped upon), then
1147          * count it as free, since it isn't really being utilized.
1148          */
1149         if (!(bufp->flags & CM_BUF_INHASH) || bufp->dataVersion <= 0)
1150             count++;
1151     }       
1152     lock_ReleaseRead(&buf_globalLock);
1153     return count;
1154 }
1155
1156 /* clean a buffer synchronously */
1157 long buf_CleanAsync(cm_buf_t *bp, cm_req_t *reqp)
1158 {
1159     long code;
1160     osi_assertx(bp->magic == CM_BUF_MAGIC, "invalid cm_buf_t magic");
1161
1162     lock_ObtainMutex(&bp->mx);
1163     code = buf_CleanAsyncLocked(bp, reqp);
1164     lock_ReleaseMutex(&bp->mx);
1165
1166     return code;
1167 }       
1168
1169 /* wait for a buffer's cleaning to finish */
1170 void buf_CleanWait(cm_scache_t * scp, cm_buf_t *bp)
1171 {
1172     osi_assertx(bp->magic == CM_BUF_MAGIC, "invalid cm_buf_t magic");
1173
1174     lock_ObtainMutex(&bp->mx);
1175     if (bp->flags & CM_BUF_WRITING) {
1176         buf_WaitIO(scp, bp);
1177     }
1178     lock_ReleaseMutex(&bp->mx);
1179 }       
1180
1181 /* set the dirty flag on a buffer, and set associated write-ahead log,
1182  * if there is one.  Allow one to be added to a buffer, but not changed.
1183  *
1184  * The buffer must be locked before calling this routine.
1185  */
1186 void buf_SetDirty(cm_buf_t *bp, afs_uint32 offset, afs_uint32 length)
1187 {
1188     osi_assertx(bp->magic == CM_BUF_MAGIC, "invalid cm_buf_t magic");
1189     osi_assertx(bp->refCount > 0, "cm_buf_t refcount 0");
1190
1191     if (bp->flags & CM_BUF_DIRTY) {
1192
1193         osi_Log1(buf_logp, "buf_SetDirty 0x%p already dirty", bp);
1194
1195         if (bp->dirty_offset <= offset) {
1196             if (bp->dirty_offset + bp->dirty_length >= offset + length) {
1197                 /* dirty_length remains the same */
1198             } else {
1199                 bp->dirty_length = offset + length - bp->dirty_offset;
1200             }
1201         } else /* bp->dirty_offset > offset */ {
1202             if (bp->dirty_offset + bp->dirty_length >= offset + length) {
1203                 bp->dirty_length = bp->dirty_offset + bp->dirty_length - offset;
1204             } else {
1205                 bp->dirty_length = length;
1206             }
1207             bp->dirty_offset = offset;
1208         }
1209     } else {
1210         osi_Log1(buf_logp, "buf_SetDirty 0x%p", bp);
1211
1212         /* set dirty bit */
1213         bp->flags |= CM_BUF_DIRTY;
1214
1215         /* and turn off EOF flag, since it has associated data now */
1216         bp->flags &= ~CM_BUF_EOF;
1217
1218         bp->dirty_offset = offset;
1219         bp->dirty_length = length;
1220
1221         /* and add to the dirty list.  
1222          * we obtain a hold on the buffer for as long as it remains 
1223          * in the list.  buffers are only removed from the list by 
1224          * the buf_IncrSyncer function regardless of when else the
1225          * dirty flag might be cleared.
1226          *
1227          * This should never happen but just in case there is a bug
1228          * elsewhere, never add to the dirty list if the buffer is 
1229          * already there.
1230          */
1231         lock_ObtainWrite(&buf_globalLock);
1232         if (bp->dirtyp == NULL && cm_data.buf_dirtyListEndp != bp) {
1233             buf_HoldLocked(bp);
1234             if (!cm_data.buf_dirtyListp) {
1235                 cm_data.buf_dirtyListp = cm_data.buf_dirtyListEndp = bp;
1236             } else {
1237                 cm_data.buf_dirtyListEndp->dirtyp = bp;
1238                 cm_data.buf_dirtyListEndp = bp;
1239             }
1240             bp->dirtyp = NULL;
1241         }
1242         lock_ReleaseWrite(&buf_globalLock);
1243     }
1244 }
1245
1246 /* clean all buffers, reset log pointers and invalidate all buffers.
1247  * Called with no locks held, and returns with same.
1248  *
1249  * This function is guaranteed to clean and remove the log ptr of all the
1250  * buffers that were dirty or had non-zero log ptrs before the call was
1251  * made.  That's sufficient to clean up any garbage left around by recovery,
1252  * which is all we're counting on this for; there may be newly created buffers
1253  * added while we're running, but that should be OK.
1254  *
1255  * In an environment where there are no transactions (artificially imposed, for
1256  * example, when switching the database to raw mode), this function is used to
1257  * make sure that all updates have been written to the disk.  In that case, we don't
1258  * really require that we forget the log association between pages and logs, but
1259  * it also doesn't hurt.  Since raw mode I/O goes through this buffer package, we don't
1260  * have to worry about invalidating data in the buffers.
1261  *
1262  * This function is used at the end of recovery as paranoia to get the recovered
1263  * database out to disk.  It removes all references to the recovery log and cleans
1264  * all buffers.
1265  */
1266 long buf_CleanAndReset(void)
1267 {
1268     afs_uint32 i;
1269     cm_buf_t *bp;
1270     cm_req_t req;
1271
1272     lock_ObtainRead(&buf_globalLock);
1273     for(i=0; i<cm_data.buf_hashSize; i++) {
1274         for(bp = cm_data.buf_scacheHashTablepp[i]; bp; bp = bp->hashp) {
1275             if ((bp->flags & CM_BUF_DIRTY) == CM_BUF_DIRTY) {
1276                 buf_HoldLocked(bp);
1277                 lock_ReleaseRead(&buf_globalLock);
1278
1279                 /* now no locks are held; clean buffer and go on */
1280                 cm_InitReq(&req);
1281                 req.flags |= CM_REQ_NORETRY;
1282
1283                 buf_CleanAsync(bp, &req);
1284                 buf_CleanWait(NULL, bp);
1285
1286                 /* relock and release buffer */
1287                 lock_ObtainRead(&buf_globalLock);
1288                 buf_ReleaseLocked(bp, FALSE);
1289             } /* dirty */
1290         } /* over one bucket */
1291     }   /* for loop over all hash buckets */
1292
1293     /* release locks */
1294     lock_ReleaseRead(&buf_globalLock);
1295
1296 #ifdef TESTING
1297     buf_ValidateBufQueues();
1298 #endif /* TESTING */
1299     
1300     /* and we're done */
1301     return 0;
1302 }       
1303
1304 /* called without global lock being held, reserves buffers for callers
1305  * that need more than one held (not locked) at once.
1306  */
1307 void buf_ReserveBuffers(afs_uint64 nbuffers)
1308 {
1309     lock_ObtainWrite(&buf_globalLock);
1310     while (1) {
1311         if (cm_data.buf_reservedBufs + nbuffers > cm_data.buf_maxReservedBufs) {
1312             cm_data.buf_reserveWaiting = 1;
1313             osi_Log1(buf_logp, "buf_ReserveBuffers waiting for %d bufs", nbuffers);
1314             osi_SleepW((LONG_PTR) &cm_data.buf_reservedBufs, &buf_globalLock);
1315             lock_ObtainWrite(&buf_globalLock);
1316         }
1317         else {
1318             cm_data.buf_reservedBufs += nbuffers;
1319             break;
1320         }
1321     }
1322     lock_ReleaseWrite(&buf_globalLock);
1323 }
1324
1325 int buf_TryReserveBuffers(afs_uint64 nbuffers)
1326 {
1327     int code;
1328
1329     lock_ObtainWrite(&buf_globalLock);
1330     if (cm_data.buf_reservedBufs + nbuffers > cm_data.buf_maxReservedBufs) {
1331         code = 0;
1332     }
1333     else {
1334         cm_data.buf_reservedBufs += nbuffers;
1335         code = 1;
1336     }
1337     lock_ReleaseWrite(&buf_globalLock);
1338     return code;
1339 }       
1340
1341 /* called without global lock held, releases reservation held by
1342  * buf_ReserveBuffers.
1343  */
1344 void buf_UnreserveBuffers(afs_uint64 nbuffers)
1345 {
1346     lock_ObtainWrite(&buf_globalLock);
1347     cm_data.buf_reservedBufs -= nbuffers;
1348     if (cm_data.buf_reserveWaiting) {
1349         cm_data.buf_reserveWaiting = 0;
1350         osi_Wakeup((LONG_PTR) &cm_data.buf_reservedBufs);
1351     }
1352     lock_ReleaseWrite(&buf_globalLock);
1353 }       
1354
1355 /* truncate the buffers past sizep, zeroing out the page, if we don't
1356  * end on a page boundary.
1357  *
1358  * Requires cm_bufCreateLock to be write locked.
1359  */
1360 long buf_Truncate(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
1361                    osi_hyper_t *sizep)
1362 {
1363     cm_buf_t *bufp;
1364     cm_buf_t *nbufp;                    /* next buffer, if didRelease */
1365     osi_hyper_t bufEnd;
1366     long code;
1367     long bufferPos;
1368     afs_uint32 i;
1369
1370     /* assert that cm_bufCreateLock is held in write mode */
1371     lock_AssertWrite(&scp->bufCreateLock);
1372
1373     i = BUF_FILEHASH(&scp->fid);
1374
1375     lock_ObtainRead(&buf_globalLock);
1376     bufp = cm_data.buf_fileHashTablepp[i];
1377     if (bufp == NULL) {
1378         lock_ReleaseRead(&buf_globalLock);
1379         return 0;
1380     }
1381
1382     buf_HoldLocked(bufp);
1383     lock_ReleaseRead(&buf_globalLock);
1384     while (bufp) {
1385         lock_ObtainMutex(&bufp->mx);
1386
1387         bufEnd.HighPart = 0;
1388         bufEnd.LowPart = cm_data.buf_blockSize;
1389         bufEnd = LargeIntegerAdd(bufEnd, bufp->offset);
1390
1391         if (cm_FidCmp(&bufp->fid, &scp->fid) == 0 &&
1392              LargeIntegerLessThan(*sizep, bufEnd)) {
1393             buf_WaitIO(scp, bufp);
1394         }
1395         lock_ObtainMutex(&scp->mx);
1396         
1397         /* make sure we have a callback (so we have the right value for
1398          * the length), and wait for it to be safe to do a truncate.
1399          */
1400         code = cm_SyncOp(scp, bufp, userp, reqp, 0,
1401                           CM_SCACHESYNC_NEEDCALLBACK
1402                           | CM_SCACHESYNC_GETSTATUS
1403                           | CM_SCACHESYNC_SETSIZE
1404                           | CM_SCACHESYNC_BUFLOCKED);
1405
1406         
1407         /* if we succeeded in our locking, and this applies to the right
1408          * file, and the truncate request overlaps the buffer either
1409          * totally or partially, then do something.
1410          */
1411         if (code == 0 && cm_FidCmp(&bufp->fid, &scp->fid) == 0
1412              && LargeIntegerLessThan(*sizep, bufEnd)) {
1413
1414
1415             /* destroy the buffer, turning off its dirty bit, if
1416              * we're truncating the whole buffer.  Otherwise, set
1417              * the dirty bit, and clear out the tail of the buffer
1418              * if we just overlap some.
1419              */
1420             if (LargeIntegerLessThanOrEqualTo(*sizep, bufp->offset)) {
1421                 /* truncating the entire page */
1422                 bufp->flags &= ~CM_BUF_DIRTY;
1423                 bufp->dirty_offset = 0;
1424                 bufp->dirty_length = 0;
1425                 bufp->dataVersion = -1; /* known bad */
1426                 bufp->dirtyCounter++;
1427             }
1428             else {
1429                 /* don't set dirty, since dirty implies
1430                  * currently up-to-date.  Don't need to do this,
1431                  * since we'll update the length anyway.
1432                  *
1433                  * Zero out remainder of the page, in case we
1434                  * seek and write past EOF, and make this data
1435                  * visible again.
1436                  */
1437                 bufferPos = sizep->LowPart & (cm_data.buf_blockSize - 1);
1438                 osi_assertx(bufferPos != 0, "non-zero bufferPos");
1439                 memset(bufp->datap + bufferPos, 0,
1440                         cm_data.buf_blockSize - bufferPos);
1441             }
1442         }
1443                 
1444         cm_SyncOpDone( scp, bufp, 
1445                        CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
1446                        | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_BUFLOCKED);
1447
1448         lock_ReleaseMutex(&scp->mx);
1449         lock_ReleaseMutex(&bufp->mx);
1450     
1451         if (!code) {
1452             nbufp = bufp->fileHashp;
1453             if (nbufp) 
1454                 buf_Hold(nbufp);
1455         } else {
1456             /* This forces the loop to end and the error code
1457              * to be returned. */
1458             nbufp = NULL;
1459         }
1460         buf_Release(bufp);
1461         bufp = nbufp;
1462     }
1463
1464 #ifdef TESTING
1465     buf_ValidateBufQueues();
1466 #endif /* TESTING */
1467
1468     /* done */
1469     return code;
1470 }
1471
1472 long buf_FlushCleanPages(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
1473 {
1474     long code;
1475     cm_buf_t *bp;               /* buffer we're hacking on */
1476     cm_buf_t *nbp;
1477     int didRelease;
1478     afs_uint32 i;
1479
1480     i = BUF_FILEHASH(&scp->fid);
1481
1482     code = 0;
1483     lock_ObtainRead(&buf_globalLock);
1484     bp = cm_data.buf_fileHashTablepp[i];
1485     if (bp) 
1486         buf_HoldLocked(bp);
1487     lock_ReleaseRead(&buf_globalLock);
1488     
1489     for (; bp; bp = nbp) {
1490         didRelease = 0; /* haven't released this buffer yet */
1491
1492         /* clean buffer synchronously */
1493         if (cm_FidCmp(&bp->fid, &scp->fid) == 0) {
1494             lock_ObtainMutex(&bp->mx);
1495
1496             /* start cleaning the buffer, and wait for it to finish */
1497             buf_CleanAsyncLocked(bp, reqp);
1498             buf_WaitIO(scp, bp);
1499             lock_ReleaseMutex(&bp->mx);
1500
1501             code = (*cm_buf_opsp->Stabilizep)(scp, userp, reqp);
1502             if (code && code != CM_ERROR_BADFD) 
1503                 goto skip;
1504
1505             /* if the scp's FID is bad its because we received VNOVNODE 
1506              * when attempting to FetchStatus before the write.  This
1507              * page therefore contains data that can no longer be stored.
1508              */
1509             lock_ObtainMutex(&bp->mx);
1510             bp->flags &= ~CM_BUF_DIRTY;
1511             bp->flags |= CM_BUF_ERROR;
1512             bp->error = code;
1513             bp->dirty_offset = 0;
1514             bp->dirty_length = 0;
1515             bp->dataVersion = -1;       /* known bad */
1516             bp->dirtyCounter++;
1517             lock_ReleaseMutex(&bp->mx);
1518
1519             /* actually, we only know that buffer is clean if ref
1520              * count is 1, since we don't have buffer itself locked.
1521              */
1522             if (!(bp->flags & CM_BUF_DIRTY)) {
1523                 lock_ObtainWrite(&buf_globalLock);
1524                 if (bp->refCount == 1) {        /* bp is held above */
1525                     nbp = bp->fileHashp;
1526                     if (nbp) 
1527                         buf_HoldLocked(nbp);
1528                     buf_ReleaseLocked(bp, TRUE);
1529                     didRelease = 1;
1530                     buf_Recycle(bp);
1531                 }
1532                 lock_ReleaseWrite(&buf_globalLock);
1533             }
1534
1535             if (code != CM_ERROR_BADFD)
1536                 (*cm_buf_opsp->Unstabilizep)(scp, userp);
1537         }
1538
1539       skip:
1540         if (!didRelease) {
1541             lock_ObtainRead(&buf_globalLock);
1542             nbp = bp->fileHashp;
1543             if (nbp)
1544                 buf_HoldLocked(nbp);
1545             buf_ReleaseLocked(bp, FALSE);
1546             lock_ReleaseRead(&buf_globalLock);
1547         }
1548     }   /* for loop over a bunch of buffers */
1549
1550 #ifdef TESTING
1551     buf_ValidateBufQueues();
1552 #endif /* TESTING */
1553
1554     /* done */
1555     return code;
1556 }       
1557
1558 /* Must be called with scp->mx held */
1559 long buf_ForceDataVersion(cm_scache_t * scp, afs_uint64 fromVersion, afs_uint64 toVersion)
1560 {
1561     cm_buf_t * bp;
1562     afs_uint32 i;
1563     int found = 0;
1564
1565     lock_AssertMutex(&scp->mx);
1566
1567     i = BUF_FILEHASH(&scp->fid);
1568
1569     lock_ObtainRead(&buf_globalLock);
1570
1571     for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp = bp->fileHashp) {
1572         if (cm_FidCmp(&bp->fid, &scp->fid) == 0) {
1573             if (bp->dataVersion == fromVersion) {
1574                 bp->dataVersion = toVersion;
1575                 found = 1;
1576             }
1577         }
1578     }
1579     lock_ReleaseRead(&buf_globalLock);
1580
1581     if (found)
1582         return 0;
1583     else
1584         return ENOENT;
1585 }
1586
1587 long buf_CleanVnode(struct cm_scache *scp, cm_user_t *userp, cm_req_t *reqp)
1588 {
1589     long code = 0;
1590     long wasDirty = 0;
1591     cm_buf_t *bp;               /* buffer we're hacking on */
1592     cm_buf_t *nbp;              /* next one */
1593     afs_uint32 i;
1594
1595     i = BUF_FILEHASH(&scp->fid);
1596
1597     lock_ObtainRead(&buf_globalLock);
1598     bp = cm_data.buf_fileHashTablepp[i];
1599     if (bp) 
1600         buf_HoldLocked(bp);
1601     lock_ReleaseRead(&buf_globalLock);
1602     for (; bp; bp = nbp) {
1603         /* clean buffer synchronously */
1604         if (cm_FidCmp(&bp->fid, &scp->fid) == 0) {
1605             if (userp) {
1606                 cm_HoldUser(userp);
1607                 lock_ObtainMutex(&bp->mx);
1608                 if (bp->userp) 
1609                     cm_ReleaseUser(bp->userp);
1610                 bp->userp = userp;
1611                 lock_ReleaseMutex(&bp->mx);
1612             }   
1613             wasDirty = buf_CleanAsync(bp, reqp);
1614             buf_CleanWait(scp, bp);
1615             lock_ObtainMutex(&bp->mx);
1616             if (bp->flags & CM_BUF_ERROR) {
1617                 code = bp->error;
1618                 if (code == 0) 
1619                     code = -1;
1620             }
1621             lock_ReleaseMutex(&bp->mx);
1622         }
1623
1624         lock_ObtainRead(&buf_globalLock);
1625         nbp = bp->fileHashp;
1626         if (nbp) 
1627             buf_HoldLocked(nbp);
1628         buf_ReleaseLocked(bp, FALSE);
1629         lock_ReleaseRead(&buf_globalLock);
1630     }   /* for loop over a bunch of buffers */
1631
1632 #ifdef TESTING
1633     buf_ValidateBufQueues();
1634 #endif /* TESTING */
1635
1636     /* done */
1637     return code;
1638 }
1639
1640 #ifdef TESTING
1641 void
1642 buf_ValidateBufQueues(void)
1643 {
1644     cm_buf_t * bp, *bpb, *bpf, *bpa;
1645     afs_uint32 countf=0, countb=0, counta=0;
1646
1647     lock_ObtainRead(&buf_globalLock);
1648     for (bp = cm_data.buf_freeListEndp; bp; bp=(cm_buf_t *) osi_QPrev(&bp->q)) {
1649         if (bp->magic != CM_BUF_MAGIC)
1650             osi_panic("buf magic error",__FILE__,__LINE__);
1651         countb++;
1652         bpb = bp;
1653     }
1654
1655     for (bp = cm_data.buf_freeListp; bp; bp=(cm_buf_t *) osi_QNext(&bp->q)) {
1656         if (bp->magic != CM_BUF_MAGIC)
1657             osi_panic("buf magic error",__FILE__,__LINE__);
1658         countf++;
1659         bpf = bp;
1660     }
1661
1662     for (bp = cm_data.buf_allp; bp; bp=bp->allp) {
1663         if (bp->magic != CM_BUF_MAGIC)
1664             osi_panic("buf magic error",__FILE__,__LINE__);
1665         counta++;
1666         bpa = bp;
1667     }
1668     lock_ReleaseRead(&buf_globalLock);
1669
1670     if (countb != countf)
1671         osi_panic("buf magic error",__FILE__,__LINE__);
1672
1673     if (counta != cm_data.buf_nbuffers)
1674         osi_panic("buf magic error",__FILE__,__LINE__);
1675 }
1676 #endif /* TESTING */
1677
1678 /* dump the contents of the buf_scacheHashTablepp. */
1679 int cm_DumpBufHashTable(FILE *outputFile, char *cookie, int lock)
1680 {
1681     int zilch;
1682     cm_buf_t *bp;
1683     char output[1024];
1684     afs_uint32 i;
1685   
1686     if (cm_data.buf_scacheHashTablepp == NULL)
1687         return -1;
1688
1689     if (lock)
1690         lock_ObtainRead(&buf_globalLock);
1691   
1692     StringCbPrintfA(output, sizeof(output), "%s - dumping buf_HashTable - buf_hashSize=%d\r\n", 
1693                     cookie, cm_data.buf_hashSize);
1694     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1695   
1696     for (i = 0; i < cm_data.buf_hashSize; i++)
1697     {
1698         for (bp = cm_data.buf_scacheHashTablepp[i]; bp; bp=bp->hashp) 
1699         {
1700             StringCbPrintfA(output, sizeof(output), 
1701                             "%s bp=0x%08X, hash=%d, fid (cell=%d, volume=%d, "
1702                             "vnode=%d, unique=%d), offset=%x:%08x, dv=%I64d, "
1703                             "flags=0x%x, cmFlags=0x%x, refCount=%d\r\n",
1704                              cookie, (void *)bp, i, bp->fid.cell, bp->fid.volume, 
1705                              bp->fid.vnode, bp->fid.unique, bp->offset.HighPart, 
1706                              bp->offset.LowPart, bp->dataVersion, bp->flags, 
1707                              bp->cmFlags, bp->refCount);
1708             WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1709         }
1710     }
1711   
1712     StringCbPrintfA(output, sizeof(output), "%s - Done dumping buf_HashTable.\r\n", cookie);
1713     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1714
1715     StringCbPrintfA(output, sizeof(output), "%s - dumping buf_freeListEndp\r\n", cookie);
1716     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1717     for(bp = cm_data.buf_freeListEndp; bp; bp=(cm_buf_t *) osi_QPrev(&bp->q)) {
1718         StringCbPrintfA(output, sizeof(output), 
1719                          "%s bp=0x%08X, fid (cell=%d, volume=%d, "
1720                          "vnode=%d, unique=%d), offset=%x:%08x, dv=%I64d, "
1721                          "flags=0x%x, cmFlags=0x%x, refCount=%d\r\n",
1722                          cookie, (void *)bp, bp->fid.cell, bp->fid.volume, 
1723                          bp->fid.vnode, bp->fid.unique, bp->offset.HighPart, 
1724                          bp->offset.LowPart, bp->dataVersion, bp->flags, 
1725                          bp->cmFlags, bp->refCount);
1726         WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1727     }
1728     StringCbPrintfA(output, sizeof(output), "%s - Done dumping buf_FreeListEndp.\r\n", cookie);
1729     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1730
1731     StringCbPrintfA(output, sizeof(output), "%s - dumping buf_dirtyListEndp\r\n", cookie);
1732     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1733     for(bp = cm_data.buf_dirtyListEndp; bp; bp=(cm_buf_t *) osi_QPrev(&bp->q)) {
1734         StringCbPrintfA(output, sizeof(output), 
1735                          "%s bp=0x%08X, fid (cell=%d, volume=%d, "
1736                          "vnode=%d, unique=%d), offset=%x:%08x, dv=%I64d, "
1737                          "flags=0x%x, cmFlags=0x%x, refCount=%d\r\n",
1738                          cookie, (void *)bp, bp->fid.cell, bp->fid.volume, 
1739                          bp->fid.vnode, bp->fid.unique, bp->offset.HighPart, 
1740                          bp->offset.LowPart, bp->dataVersion, bp->flags, 
1741                          bp->cmFlags, bp->refCount);
1742         WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1743     }
1744     StringCbPrintfA(output, sizeof(output), "%s - Done dumping buf_dirtyListEndp.\r\n", cookie);
1745     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1746
1747     if (lock)
1748         lock_ReleaseRead(&buf_globalLock);
1749     return 0;
1750 }
1751
1752 void buf_ForceTrace(BOOL flush)
1753 {
1754     HANDLE handle;
1755     int len;
1756     char buf[256];
1757
1758     if (!buf_logp) 
1759         return;
1760
1761     len = GetTempPath(sizeof(buf)-10, buf);
1762     StringCbCopyA(&buf[len], sizeof(buf)-len, "/afs-buffer.log");
1763     handle = CreateFile(buf, GENERIC_WRITE, FILE_SHARE_READ,
1764                             NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1765     if (handle == INVALID_HANDLE_VALUE) {
1766         osi_panic("Cannot create log file", __FILE__, __LINE__);
1767     }
1768     osi_LogPrint(buf_logp, handle);
1769     if (flush)
1770         FlushFileBuffers(handle);
1771     CloseHandle(handle);
1772 }
1773
1774 long buf_DirtyBuffersExist(cm_fid_t *fidp)
1775 {
1776     cm_buf_t *bp;
1777     afs_uint32 bcount = 0;
1778     afs_uint32 i;
1779
1780     i = BUF_FILEHASH(fidp);
1781
1782     for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=bp->allp, bcount++) {
1783         if (!cm_FidCmp(fidp, &bp->fid) && (bp->flags & CM_BUF_DIRTY))
1784             return 1;
1785     }
1786     return 0;
1787 }
1788
1789 #if 0
1790 long buf_CleanDirtyBuffers(cm_scache_t *scp)
1791 {
1792     cm_buf_t *bp;
1793     afs_uint32 bcount = 0;
1794     cm_fid_t * fidp = &scp->fid;
1795
1796     for (bp = cm_data.buf_allp; bp; bp=bp->allp, bcount++) {
1797         if (!cm_FidCmp(fidp, &bp->fid) && (bp->flags & CM_BUF_DIRTY)) {
1798             buf_Hold(bp);
1799             lock_ObtainMutex(&bp->mx);
1800             bp->cmFlags &= ~CM_BUF_CMSTORING;
1801             bp->flags &= ~CM_BUF_DIRTY;
1802             bp->dirty_offset = 0;
1803             bp->dirty_length = 0;
1804             bp->flags |= CM_BUF_ERROR;
1805             bp->error = VNOVNODE;
1806             bp->dataVersion = -1; /* bad */
1807             bp->dirtyCounter++;
1808             if (bp->flags & CM_BUF_WAITING) {
1809                 osi_Log2(buf_logp, "BUF CleanDirtyBuffers Waking [scp 0x%x] bp 0x%x", scp, bp);
1810                 osi_Wakeup((long) &bp);
1811             }
1812             lock_ReleaseMutex(&bp->mx);
1813             buf_Release(bp);
1814         }
1815     }
1816     return 0;
1817 }
1818 #endif