2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* Copyright (C) 1994 Cazamar Systems, Inc. */
12 #include <afs/param.h>
23 #include "cm_memmap.h"
26 #define TRACE_BUFFER 1
29 extern void afsi_log(char *pattern, ...);
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.
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.
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.
43 * The mutexes in the buffer structures protect the remaining fields in the
44 * buffers, as well the data itself.
46 * The locking hierarchy here is this:
48 * - resv multiple simul. buffers reservation
49 * - lock buffer I/O flags
50 * - lock buffer's mutex
51 * - lock buf_globalLock
55 /* global debugging log */
56 osi_log_t *buf_logp = NULL;
58 /* Global lock protecting hash tables and free lists */
59 osi_rwlock_t buf_globalLock;
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
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
72 /* defaults setup; these variables may be manually assigned into
73 * before calling cm_Init, as a way of changing these defaults.
76 /* callouts for reading and writing data, etc */
77 cm_buf_ops_t *cm_buf_opsp;
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 */
87 /* set this to 1 when we are terminating to prevent access attempts */
88 static int buf_ShutdownFlag = 0;
90 void buf_HoldLocked(cm_buf_t *bp)
92 osi_assert(bp->magic == CM_BUF_MAGIC);
96 /* hold a reference to an already held buffer */
97 void buf_Hold(cm_buf_t *bp)
99 lock_ObtainWrite(&buf_globalLock);
101 lock_ReleaseWrite(&buf_globalLock);
104 /* code to drop reference count while holding buf_globalLock */
105 void buf_ReleaseLocked(cm_buf_t *bp)
107 /* ensure that we're in the LRU queue if our ref count is 0 */
108 osi_assert(bp->magic == CM_BUF_MAGIC);
110 if (bp->refCount == 0)
111 osi_panic("buf refcount 0",__FILE__,__LINE__);;
113 osi_assert(bp->refCount > 0);
115 if (--bp->refCount == 0) {
116 if (!(bp->flags & CM_BUF_INLRU)) {
117 osi_QAdd((osi_queue_t **) &cm_data.buf_freeListp, &bp->q);
119 /* watch for transition from empty to one element */
120 if (!cm_data.buf_freeListEndp)
121 cm_data.buf_freeListEndp = cm_data.buf_freeListp;
122 bp->flags |= CM_BUF_INLRU;
127 /* release a buffer. Buffer must be referenced, but unlocked. */
128 void buf_Release(cm_buf_t *bp)
130 lock_ObtainWrite(&buf_globalLock);
131 buf_ReleaseLocked(bp);
132 lock_ReleaseWrite(&buf_globalLock);
135 /* incremental sync daemon. Writes all dirty buffers every 5000 ms */
136 void buf_IncrSyncer(long parm)
139 long i; /* counter */
143 while (buf_ShutdownFlag == 0) {
145 i = SleepEx(5000, 1);
146 if (i != 0) continue;
151 /* now go through our percentage of the buffers */
152 for (bpp = &cm_data.buf_dirtyListp; bp = *bpp; ) {
154 /* all dirty buffers are held when they are added to the
155 * dirty list. No need for an additional hold.
158 if (bp->flags & CM_BUF_DIRTY) {
159 /* start cleaning the buffer; don't touch log pages since
160 * the log code counts on knowing exactly who is writing
161 * a log page at any given instant.
164 req.flags |= CM_REQ_NORETRY;
165 wasDirty |= buf_CleanAsync(bp, &req);
168 /* the buffer may or may not have been dirty
169 * and if dirty may or may not have been cleaned
170 * successfully. check the dirty flag again.
172 if (!(bp->flags & CM_BUF_DIRTY)) {
173 lock_ObtainMutex(&bp->mx);
174 if (!(bp->flags & CM_BUF_DIRTY)) {
175 /* remove the buffer from the dirty list */
176 lock_ObtainWrite(&buf_globalLock);
179 if (cm_data.buf_dirtyListp == NULL)
180 cm_data.buf_dirtyListEndp = NULL;
181 buf_ReleaseLocked(bp);
182 lock_ReleaseWrite(&buf_globalLock);
184 /* advance the pointer so we don't loop forever */
187 lock_ReleaseMutex(&bp->mx);
189 /* advance the pointer so we don't loop forever */
192 } /* for loop over a bunch of buffers */
193 } /* whole daemon's while loop */
197 buf_ValidateBuffers(void)
199 cm_buf_t * bp, *bpf, *bpa, *bpb;
200 afs_uint64 countb = 0, countf = 0, counta = 0;
202 if (cm_data.buf_freeListp == NULL && cm_data.buf_freeListEndp != NULL ||
203 cm_data.buf_freeListp != NULL && cm_data.buf_freeListEndp == NULL) {
204 afsi_log("cm_ValidateBuffers failure: inconsistent free list pointers");
205 fprintf(stderr, "cm_ValidateBuffers failure: inconsistent free list pointers\n");
209 for (bp = cm_data.buf_freeListEndp; bp; bp=(cm_buf_t *) osi_QPrev(&bp->q)) {
210 if (bp->magic != CM_BUF_MAGIC) {
211 afsi_log("cm_ValidateBuffers failure: bp->magic != CM_BUF_MAGIC");
212 fprintf(stderr, "cm_ValidateBuffers failure: bp->magic != CM_BUF_MAGIC\n");
218 if (countb > cm_data.buf_nbuffers) {
219 afsi_log("cm_ValidateBuffers failure: countb > cm_data.buf_nbuffers");
220 fprintf(stderr, "cm_ValidateBuffers failure: countb > cm_data.buf_nbuffers\n");
225 for (bp = cm_data.buf_freeListp; bp; bp=(cm_buf_t *) osi_QNext(&bp->q)) {
226 if (bp->magic != CM_BUF_MAGIC) {
227 afsi_log("cm_ValidateBuffers failure: bp->magic != CM_BUF_MAGIC");
228 fprintf(stderr, "cm_ValidateBuffers failure: bp->magic != CM_BUF_MAGIC\n");
234 if (countf > cm_data.buf_nbuffers) {
235 afsi_log("cm_ValidateBuffers failure: countf > cm_data.buf_nbuffers");
236 fprintf(stderr, "cm_ValidateBuffers failure: countf > cm_data.buf_nbuffers\n");
241 for (bp = cm_data.buf_allp; bp; bp=bp->allp) {
242 if (bp->magic != CM_BUF_MAGIC) {
243 afsi_log("cm_ValidateBuffers failure: bp->magic != CM_BUF_MAGIC");
244 fprintf(stderr, "cm_ValidateBuffers failure: bp->magic != CM_BUF_MAGIC\n");
250 if (counta > cm_data.buf_nbuffers) {
251 afsi_log("cm_ValidateBuffers failure: counta > cm_data.buf_nbuffers");
252 fprintf(stderr, "cm_ValidateBuffers failure: counta > cm_data.buf_nbuffers\n");
257 if (countb != countf) {
258 afsi_log("cm_ValidateBuffers failure: countb != countf");
259 fprintf(stderr, "cm_ValidateBuffers failure: countb != countf\n");
263 if (counta != cm_data.buf_nbuffers) {
264 afsi_log("cm_ValidateBuffers failure: counta != cm_data.buf_nbuffers");
265 fprintf(stderr, "cm_ValidateBuffers failure: counta != cm_data.buf_nbuffers\n");
272 void buf_Shutdown(void)
274 buf_ShutdownFlag = 1;
277 /* initialize the buffer package; called with no locks
278 * held during the initialization phase.
280 long buf_Init(int newFile, cm_buf_ops_t *opsp, afs_uint64 nbuffers)
282 static osi_once_t once;
291 cm_data.buf_nbuffers = nbuffers;
293 /* Have to be able to reserve a whole chunk */
294 if (((cm_data.buf_nbuffers - 3) * cm_data.buf_blockSize) < cm_chunkSize)
295 return CM_ERROR_TOOFEWBUFS;
298 /* recall for callouts */
301 if (osi_Once(&once)) {
302 /* initialize global locks */
303 lock_InitializeRWLock(&buf_globalLock, "Global buffer lock");
306 /* remember this for those who want to reset it */
307 cm_data.buf_nOrigBuffers = cm_data.buf_nbuffers;
309 /* lower hash size to a prime number */
310 cm_data.buf_hashSize = osi_PrimeLessThan(CM_BUF_HASHSIZE);
312 /* create hash table */
313 memset((void *)cm_data.buf_hashTablepp, 0, cm_data.buf_hashSize * sizeof(cm_buf_t *));
315 /* another hash table */
316 memset((void *)cm_data.buf_fileHashTablepp, 0, cm_data.buf_hashSize * sizeof(cm_buf_t *));
318 /* create buffer headers and put in free list */
319 bp = cm_data.bufHeaderBaseAddress;
320 data = cm_data.bufDataBaseAddress;
321 cm_data.buf_allp = NULL;
323 for (i=0; i<cm_data.buf_nbuffers; i++) {
324 osi_assert(bp >= cm_data.bufHeaderBaseAddress && bp < (cm_buf_t *)cm_data.bufDataBaseAddress);
325 osi_assert(data >= cm_data.bufDataBaseAddress && data < cm_data.bufEndOfData);
327 /* allocate and zero some storage */
328 memset(bp, 0, sizeof(cm_buf_t));
329 bp->magic = CM_BUF_MAGIC;
330 /* thread on list of all buffers */
331 bp->allp = cm_data.buf_allp;
332 cm_data.buf_allp = bp;
334 osi_QAdd((osi_queue_t **)&cm_data.buf_freeListp, &bp->q);
335 bp->flags |= CM_BUF_INLRU;
336 lock_InitializeMutex(&bp->mx, "Buffer mutex");
338 /* grab appropriate number of bytes from aligned zone */
341 /* setup last buffer pointer */
343 cm_data.buf_freeListEndp = bp;
347 data += cm_data.buf_blockSize;
350 /* none reserved at first */
351 cm_data.buf_reservedBufs = 0;
353 /* just for safety's sake */
354 cm_data.buf_maxReservedBufs = cm_data.buf_nbuffers - 3;
356 bp = cm_data.bufHeaderBaseAddress;
357 data = cm_data.bufDataBaseAddress;
359 for (i=0; i<cm_data.buf_nbuffers; i++) {
360 lock_InitializeMutex(&bp->mx, "Buffer mutex");
363 bp->waitRequests = 0;
364 bp->flags &= ~CM_BUF_WAITING;
370 buf_ValidateBufQueues();
374 /* init the buffer trace log */
375 buf_logp = osi_LogCreate("buffer", 1000);
376 osi_LogEnable(buf_logp);
381 /* and create the incr-syncer */
382 phandle = thrd_Create(0, 0,
383 (ThreadFunc) buf_IncrSyncer, 0, 0, &pid,
386 osi_assertx(phandle != NULL, "buf: can't create incremental sync proc");
387 CloseHandle(phandle);
391 buf_ValidateBufQueues();
396 /* add nbuffers to the buffer pool, if possible.
397 * Called with no locks held.
399 long buf_AddBuffers(afs_uint64 nbuffers)
401 /* The size of a virtual cache cannot be changed after it has
402 * been created. Subsequent calls to MapViewofFile() with
403 * an existing mapping object name would not allow the
404 * object to be resized. Return failure immediately.
406 * A similar problem now occurs with the persistent cache
407 * given that the memory mapped file now contains a complex
410 afsi_log("request to add %d buffers to the existing cache of size %d denied",
411 nbuffers, cm_data.buf_nbuffers);
413 return CM_ERROR_INVAL;
416 /* interface to set the number of buffers to an exact figure.
417 * Called with no locks held.
419 long buf_SetNBuffers(afs_uint64 nbuffers)
422 return CM_ERROR_INVAL;
423 if (nbuffers == cm_data.buf_nbuffers)
425 else if (nbuffers > cm_data.buf_nbuffers)
426 return buf_AddBuffers(nbuffers - cm_data.buf_nbuffers);
428 return CM_ERROR_INVAL;
431 /* wait for reading or writing to clear; called with write-locked
432 * buffer and unlocked scp and returns with locked buffer.
434 void buf_WaitIO(cm_scache_t * scp, cm_buf_t *bp)
439 osi_assert(scp->magic == CM_SCACHE_MAGIC);
440 osi_assert(bp->magic == CM_BUF_MAGIC);
443 /* if no IO is happening, we're done */
444 if (!(bp->flags & (CM_BUF_READING | CM_BUF_WRITING)))
447 /* otherwise I/O is happening, but some other thread is waiting for
448 * the I/O already. Wait for that guy to figure out what happened,
449 * and then check again.
451 if ( bp->flags & CM_BUF_WAITING ) {
454 osi_Log1(buf_logp, "buf_WaitIO CM_BUF_WAITING already set for 0x%p", bp);
456 osi_Log1(buf_logp, "buf_WaitIO CM_BUF_WAITING set for 0x%p", bp);
457 bp->flags |= CM_BUF_WAITING;
458 bp->waitCount = bp->waitRequests = 1;
460 osi_SleepM((LONG_PTR)bp, &bp->mx);
462 smb_UpdateServerPriority();
464 lock_ObtainMutex(&bp->mx);
465 osi_Log1(buf_logp, "buf_WaitIO conflict wait done for 0x%p", bp);
467 if (bp->waitCount == 0) {
468 osi_Log1(buf_logp, "buf_WaitIO CM_BUF_WAITING reset for 0x%p", bp);
469 bp->flags &= ~CM_BUF_WAITING;
470 bp->waitRequests = 0;
474 if (scp = cm_FindSCache(&bp->fid))
478 lock_ObtainMutex(&scp->mx);
479 if (scp->flags & CM_SCACHEFLAG_WAITING) {
480 osi_Log1(buf_logp, "buf_WaitIO waking scp 0x%p", scp);
481 osi_Wakeup((LONG_PTR)&scp->flags);
483 lock_ReleaseMutex(&scp->mx);
487 /* if we get here, the IO is done, but we may have to wakeup people waiting for
488 * the I/O to complete. Do so.
490 if (bp->flags & CM_BUF_WAITING) {
491 osi_Log1(buf_logp, "buf_WaitIO Waking bp 0x%p", bp);
492 osi_Wakeup((LONG_PTR) bp);
494 osi_Log1(buf_logp, "WaitIO finished wait for bp 0x%p", bp);
497 cm_ReleaseSCache(scp);
500 /* find a buffer, if any, for a particular file ID and offset. Assumes
501 * that buf_globalLock is write locked when called.
503 cm_buf_t *buf_FindLocked(struct cm_scache *scp, osi_hyper_t *offsetp)
508 i = BUF_HASH(&scp->fid, offsetp);
509 for(bp = cm_data.buf_hashTablepp[i]; bp; bp=bp->hashp) {
510 if (cm_FidCmp(&scp->fid, &bp->fid) == 0
511 && offsetp->LowPart == bp->offset.LowPart
512 && offsetp->HighPart == bp->offset.HighPart) {
518 /* return whatever we found, if anything */
522 /* find a buffer with offset *offsetp for vnode *scp. Called
523 * with no locks held.
525 cm_buf_t *buf_Find(struct cm_scache *scp, osi_hyper_t *offsetp)
529 lock_ObtainWrite(&buf_globalLock);
530 bp = buf_FindLocked(scp, offsetp);
531 lock_ReleaseWrite(&buf_globalLock);
536 /* start cleaning I/O on this buffer. Buffer must be write locked, and is returned
539 * Makes sure that there's only one person writing this block
540 * at any given time, and also ensures that the log is forced sufficiently far,
541 * if this buffer contains logged data.
543 * Returns non-zero if the buffer was dirty.
545 long buf_CleanAsyncLocked(cm_buf_t *bp, cm_req_t *reqp)
549 cm_scache_t * scp = NULL;
551 osi_assert(bp->magic == CM_BUF_MAGIC);
553 while ((bp->flags & CM_BUF_DIRTY) == CM_BUF_DIRTY) {
555 lock_ReleaseMutex(&bp->mx);
557 scp = cm_FindSCache(&bp->fid);
559 osi_Log2(buf_logp, "buf_CleanAsyncLocked starts I/O on scp 0x%p buf 0x%p", scp, bp);
560 code = (*cm_buf_opsp->Writep)(scp, &bp->offset,
561 cm_data.buf_blockSize, 0, bp->userp,
563 osi_Log3(buf_logp, "buf_CleanAsyncLocked I/O on scp 0x%p buf 0x%p, done=%d", scp, bp, code);
565 cm_ReleaseSCache(scp);
568 osi_Log1(buf_logp, "buf_CleanAsyncLocked unable to start I/O - scp not found buf 0x%p", bp);
569 code = CM_ERROR_NOSUCHFILE;
572 lock_ObtainMutex(&bp->mx);
573 /* if the Write routine returns No Such File, clear the dirty flag
574 * because we aren't going to be able to write this data to the file
577 if (code == CM_ERROR_NOSUCHFILE){
578 bp->flags &= ~CM_BUF_DIRTY;
579 bp->flags |= CM_BUF_ERROR;
580 bp->error = CM_ERROR_NOSUCHFILE;
581 bp->dataVersion = -1; /* bad */
586 /* Disk cache support */
587 /* write buffer to disk cache (synchronous for now) */
588 diskcache_Update(bp->dcp, bp->datap, cm_data.buf_blockSize, bp->dataVersion);
589 #endif /* DISKCACHE95 */
591 /* if we get here and retries are not permitted
592 * then we need to exit this loop regardless of
593 * whether or not we were able to clear the dirty bit
595 if (reqp->flags & CM_REQ_NORETRY)
599 if (!(bp->flags & CM_BUF_DIRTY)) {
600 /* remove buffer from dirty buffer queue */
604 /* do logging after call to GetLastError, or else */
606 /* if someone was waiting for the I/O that just completed or failed,
609 if (bp->flags & CM_BUF_WAITING) {
610 /* turn off flags and wakeup users */
611 osi_Log1(buf_logp, "buf_WaitIO Waking bp 0x%p", bp);
612 osi_Wakeup((LONG_PTR) bp);
617 /* Called with a zero-ref count buffer and with the buf_globalLock write locked.
618 * recycles the buffer, and leaves it ready for reuse with a ref count of 1.
619 * The buffer must already be clean, and no I/O should be happening to it.
621 void buf_Recycle(cm_buf_t *bp)
626 cm_buf_t *prevBp, *nextBp;
628 osi_assert(bp->magic == CM_BUF_MAGIC);
630 /* if we get here, we know that the buffer still has a 0 ref count,
631 * and that it is clean and has no currently pending I/O. This is
632 * the dude to return.
633 * Remember that as long as the ref count is 0, we know that we won't
634 * have any lock conflicts, so we can grab the buffer lock out of
635 * order in the locking hierarchy.
637 osi_Log2( buf_logp, "buf_Recycle recycles 0x%p, off 0x%x",
638 bp, bp->offset.LowPart);
640 osi_assert(bp->refCount == 0);
641 osi_assert(!(bp->flags & (CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)));
642 lock_AssertWrite(&buf_globalLock);
644 if (bp->flags & CM_BUF_INHASH) {
645 /* Remove from hash */
647 i = BUF_HASH(&bp->fid, &bp->offset);
648 lbpp = &(cm_data.buf_hashTablepp[i]);
649 for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = *lbpp) {
650 if (tbp == bp) break;
653 /* we better find it */
654 osi_assertx(tbp != NULL, "buf_Recycle: hash table screwup");
656 *lbpp = bp->hashp; /* hash out */
658 /* Remove from file hash */
660 i = BUF_FILEHASH(&bp->fid);
661 prevBp = bp->fileHashBackp;
662 nextBp = bp->fileHashp;
664 prevBp->fileHashp = nextBp;
666 cm_data.buf_fileHashTablepp[i] = nextBp;
668 nextBp->fileHashBackp = prevBp;
670 bp->flags &= ~CM_BUF_INHASH;
673 /* bump the soft reference counter now, to invalidate softRefs; no
674 * wakeup is required since people don't sleep waiting for this
679 /* make the fid unrecognizable */
680 memset(&bp->fid, 0, sizeof(cm_fid_t));
683 /* recycle a buffer, removing it from the free list, hashing in its new identity
684 * and returning it write-locked so that no one can use it. Called without
685 * any locks held, and can return an error if it loses the race condition and
686 * finds that someone else created the desired buffer.
688 * If success is returned, the buffer is returned write-locked.
690 * May be called with null scp and offsetp, if we're just trying to reclaim some
691 * space from the buffer pool. In that case, the buffer will be returned
692 * without being hashed into the hash table.
694 long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bufpp)
696 cm_buf_t *bp; /* buffer we're dealing with */
697 cm_buf_t *nextBp; /* next buffer in file hash chain */
701 cm_InitReq(&req); /* just in case */
704 buf_ValidateBufQueues();
709 lock_ObtainWrite(&buf_globalLock);
710 /* check to see if we lost the race */
712 if (bp = buf_FindLocked(scp, offsetp)) {
713 /* Do not call buf_ReleaseLocked() because we
714 * do not want to allow the buffer to be added
718 lock_ReleaseWrite(&buf_globalLock);
719 return CM_BUF_EXISTS;
723 /* does this fix the problem below? it's a simple solution. */
724 if (!cm_data.buf_freeListEndp)
726 lock_ReleaseWrite(&buf_globalLock);
731 /* for debugging, assert free list isn't empty, although we
732 * really should try waiting for a running tranasction to finish
733 * instead of this; or better, we should have a transaction
734 * throttler prevent us from entering this situation.
736 osi_assertx(cm_data.buf_freeListEndp != NULL, "buf_GetNewLocked: no free buffers");
738 /* look at all buffers in free list, some of which may temp.
739 * have high refcounts and which then should be skipped,
740 * starting cleaning I/O for those which are dirty. If we find
741 * a clean buffer, we rehash it, lock it and return it.
743 for(bp = cm_data.buf_freeListEndp; bp; bp=(cm_buf_t *) osi_QPrev(&bp->q)) {
744 /* check to see if it really has zero ref count. This
745 * code can bump refcounts, at least, so it may not be
748 if (bp->refCount > 0)
751 /* we don't have to lock buffer itself, since the ref
752 * count is 0 and we know it will stay zero as long as
753 * we hold the global lock.
756 /* don't recycle someone in our own chunk */
757 if (!cm_FidCmp(&bp->fid, &scp->fid)
758 && (bp->offset.LowPart & (-cm_chunkSize))
759 == (offsetp->LowPart & (-cm_chunkSize)))
762 /* if this page is being filled (!) or cleaned, see if
763 * the I/O has completed. If not, skip it, otherwise
764 * do the final processing for the I/O.
766 if (bp->flags & (CM_BUF_READING | CM_BUF_WRITING)) {
767 /* probably shouldn't do this much work while
768 * holding the big lock? Watch for contention
774 if (bp->flags & CM_BUF_DIRTY) {
775 /* if the buffer is dirty, start cleaning it and
776 * move on to the next buffer. We do this with
777 * just the lock required to minimize contention
781 lock_ReleaseWrite(&buf_globalLock);
783 /* grab required lock and clean; this only
784 * starts the I/O. By the time we're back,
785 * it'll still be marked dirty, but it will also
786 * have the WRITING flag set, so we won't get
789 buf_CleanAsync(bp, &req);
791 /* now put it back and go around again */
796 /* if we get here, we know that the buffer still has a 0
797 * ref count, and that it is clean and has no currently
798 * pending I/O. This is the dude to return.
799 * Remember that as long as the ref count is 0, we know
800 * that we won't have any lock conflicts, so we can grab
801 * the buffer lock out of order in the locking hierarchy.
805 /* clean up junk flags */
806 bp->flags &= ~(CM_BUF_EOF | CM_BUF_ERROR);
807 bp->dataVersion = -1; /* unknown so far */
809 /* now hash in as our new buffer, and give it the
810 * appropriate label, if requested.
813 bp->flags |= CM_BUF_INHASH;
818 bp->offset = *offsetp;
819 i = BUF_HASH(&scp->fid, offsetp);
820 bp->hashp = cm_data.buf_hashTablepp[i];
821 cm_data.buf_hashTablepp[i] = bp;
822 i = BUF_FILEHASH(&scp->fid);
823 nextBp = cm_data.buf_fileHashTablepp[i];
824 bp->fileHashp = nextBp;
825 bp->fileHashBackp = NULL;
827 nextBp->fileHashBackp = bp;
828 cm_data.buf_fileHashTablepp[i] = bp;
831 /* prepare to return it. Start by giving it a good
835 /* and since it has a non-zero ref count, we should move
836 * it from the lru queue. It better be still there,
837 * since we've held the global (big) lock since we found
840 osi_assertx(bp->flags & CM_BUF_INLRU,
841 "buf_GetNewLocked: LRU screwup");
842 if (cm_data.buf_freeListEndp == bp) {
843 /* we're the last guy in this queue, so maintain it */
844 cm_data.buf_freeListEndp = (cm_buf_t *) osi_QPrev(&bp->q);
846 osi_QRemove((osi_queue_t **) &cm_data.buf_freeListp, &bp->q);
847 bp->flags &= ~CM_BUF_INLRU;
849 /* finally, grab the mutex so that people don't use it
850 * before the caller fills it with data. Again, no one
851 * should have been able to get to this dude to lock it.
853 osi_assertx(lock_TryMutex(&bp->mx),
854 "buf_GetNewLocked: TryMutex failed");
856 lock_ReleaseWrite(&buf_globalLock);
860 buf_ValidateBufQueues();
863 } /* for all buffers in lru queue */
864 lock_ReleaseWrite(&buf_globalLock);
865 Sleep(100); /* give some time for a buffer to be freed */
866 } /* while loop over everything */
870 /* get a page, returning it held but unlocked. Doesn't fill in the page
871 * with I/O, since we're going to write the whole thing new.
873 long buf_GetNew(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bufpp)
877 osi_hyper_t pageOffset;
881 pageOffset.HighPart = offsetp->HighPart;
882 pageOffset.LowPart = offsetp->LowPart & ~(cm_data.buf_blockSize-1);
884 bp = buf_Find(scp, &pageOffset);
886 /* lock it and break out */
887 lock_ObtainMutex(&bp->mx);
891 /* otherwise, we have to create a page */
892 code = buf_GetNewLocked(scp, &pageOffset, &bp);
894 /* check if the buffer was created in a race condition branch.
895 * If so, go around so we can hold a reference to it.
897 if (code == CM_BUF_EXISTS)
900 /* something else went wrong */
904 /* otherwise, we have a locked buffer that we just created */
907 } /* big while loop */
910 if (bp->flags & CM_BUF_READING)
913 /* once it has been read once, we can unlock it and return it, still
914 * with its refcount held.
916 lock_ReleaseMutex(&bp->mx);
918 osi_Log3(buf_logp, "buf_GetNew returning bp 0x%p for scp 0x%p, offset 0x%x",
919 bp, scp, offsetp->LowPart);
923 /* get a page, returning it held but unlocked. Make sure it is complete */
924 /* The scp must be unlocked when passed to this function */
925 long buf_Get(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bufpp)
929 osi_hyper_t pageOffset;
930 unsigned long tcount;
935 #endif /* DISKCACHE95 */
938 pageOffset.HighPart = offsetp->HighPart;
939 pageOffset.LowPart = offsetp->LowPart & ~(cm_data.buf_blockSize-1);
943 buf_ValidateBufQueues();
946 bp = buf_Find(scp, &pageOffset);
948 /* lock it and break out */
949 lock_ObtainMutex(&bp->mx);
952 /* touch disk chunk to update LRU info */
953 diskcache_Touch(bp->dcp);
954 #endif /* DISKCACHE95 */
958 /* otherwise, we have to create a page */
959 code = buf_GetNewLocked(scp, &pageOffset, &bp);
960 /* bp->mx is now held */
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.
965 if (code == CM_BUF_EXISTS)
968 /* something else went wrong */
971 buf_ValidateBufQueues();
976 /* otherwise, we have a locked buffer that we just created */
979 } /* big while loop */
981 /* if we get here, we have a locked buffer that may have just been
982 * created, in which case it needs to be filled with data.
985 /* load the page; freshly created pages should be idle */
986 osi_assert(!(bp->flags & (CM_BUF_READING | CM_BUF_WRITING)));
988 /* setup offset, event */
989 bp->over.Offset = bp->offset.LowPart;
990 bp->over.OffsetHigh = bp->offset.HighPart;
992 /* start the I/O; may drop lock */
993 bp->flags |= CM_BUF_READING;
994 code = (*cm_buf_opsp->Readp)(bp, cm_data.buf_blockSize, &tcount, NULL);
997 code = diskcache_Get(&bp->fid, &bp->offset, bp->datap, cm_data.buf_blockSize, &bp->dataVersion, &tcount, &dcp);
998 bp->dcp = dcp; /* pointer to disk cache struct. */
999 #endif /* DISKCACHE95 */
1002 /* failure or queued */
1003 if (code != ERROR_IO_PENDING) {
1005 bp->flags |= CM_BUF_ERROR;
1006 bp->flags &= ~CM_BUF_READING;
1007 if (bp->flags & CM_BUF_WAITING) {
1008 osi_Log1(buf_logp, "buf_Get Waking bp 0x%p", bp);
1009 osi_Wakeup((LONG_PTR) bp);
1011 lock_ReleaseMutex(&bp->mx);
1014 buf_ValidateBufQueues();
1015 #endif /* TESTING */
1019 /* otherwise, I/O completed instantly and we're done, except
1020 * for padding the xfr out with 0s and checking for EOF
1022 if (tcount < (unsigned long) cm_data.buf_blockSize) {
1023 memset(bp->datap+tcount, 0, cm_data.buf_blockSize - tcount);
1025 bp->flags |= CM_BUF_EOF;
1027 bp->flags &= ~CM_BUF_READING;
1028 if (bp->flags & CM_BUF_WAITING) {
1029 osi_Log1(buf_logp, "buf_Get Waking bp 0x%p", bp);
1030 osi_Wakeup((LONG_PTR) bp);
1036 /* wait for reads, either that which we started above, or that someone
1037 * else started. We don't care if we return a buffer being cleaned.
1039 if (bp->flags & CM_BUF_READING)
1040 buf_WaitIO(scp, bp);
1042 /* once it has been read once, we can unlock it and return it, still
1043 * with its refcount held.
1045 lock_ReleaseMutex(&bp->mx);
1048 /* now remove from queue; will be put in at the head (farthest from
1049 * being recycled) when we're done in buf_Release.
1051 lock_ObtainWrite(&buf_globalLock);
1052 if (bp->flags & CM_BUF_INLRU) {
1053 if (cm_data.buf_freeListEndp == bp)
1054 cm_data.buf_freeListEndp = (cm_buf_t *) osi_QPrev(&bp->q);
1055 osi_QRemove((osi_queue_t **) &cm_data.buf_freeListp, &bp->q);
1056 bp->flags &= ~CM_BUF_INLRU;
1058 lock_ReleaseWrite(&buf_globalLock);
1060 osi_Log3(buf_logp, "buf_Get returning bp 0x%p for scp 0x%p, offset 0x%x",
1061 bp, scp, offsetp->LowPart);
1063 buf_ValidateBufQueues();
1064 #endif /* TESTING */
1068 /* count # of elements in the free list;
1069 * we don't bother doing the proper locking for accessing dataVersion or flags
1070 * since it is a pain, and this is really just an advisory call. If you need
1071 * to do better at some point, rewrite this function.
1073 long buf_CountFreeList(void)
1079 lock_ObtainRead(&buf_globalLock);
1080 for(bufp = cm_data.buf_freeListp; bufp; bufp = (cm_buf_t *) osi_QNext(&bufp->q)) {
1081 /* if the buffer doesn't have an identity, or if the buffer
1082 * has been invalidate (by having its DV stomped upon), then
1083 * count it as free, since it isn't really being utilized.
1085 if (!(bufp->flags & CM_BUF_INHASH) || bufp->dataVersion <= 0)
1088 lock_ReleaseRead(&buf_globalLock);
1092 /* clean a buffer synchronously */
1093 long buf_CleanAsync(cm_buf_t *bp, cm_req_t *reqp)
1096 osi_assert(bp->magic == CM_BUF_MAGIC);
1098 lock_ObtainMutex(&bp->mx);
1099 code = buf_CleanAsyncLocked(bp, reqp);
1100 lock_ReleaseMutex(&bp->mx);
1105 /* wait for a buffer's cleaning to finish */
1106 void buf_CleanWait(cm_scache_t * scp, cm_buf_t *bp)
1108 osi_assert(bp->magic == CM_BUF_MAGIC);
1110 lock_ObtainMutex(&bp->mx);
1111 if (bp->flags & CM_BUF_WRITING) {
1112 buf_WaitIO(scp, bp);
1114 lock_ReleaseMutex(&bp->mx);
1117 /* set the dirty flag on a buffer, and set associated write-ahead log,
1118 * if there is one. Allow one to be added to a buffer, but not changed.
1120 * The buffer must be locked before calling this routine.
1122 void buf_SetDirty(cm_buf_t *bp)
1124 osi_assert(bp->magic == CM_BUF_MAGIC);
1125 osi_assert(bp->refCount > 0);
1127 if (bp->flags & CM_BUF_DIRTY) {
1128 osi_Log1(buf_logp, "buf_SetDirty 0x%p already dirty", bp);
1130 osi_Log1(buf_logp, "buf_SetDirty 0x%p", bp);
1133 bp->flags |= CM_BUF_DIRTY;
1135 /* and turn off EOF flag, since it has associated data now */
1136 bp->flags &= ~CM_BUF_EOF;
1138 /* and add to the dirty list.
1139 * we obtain a hold on the buffer for as long as it remains
1140 * in the list. buffers are only removed from the list by
1141 * the buf_IncrSyncer function regardless of when else the
1142 * dirty flag might be cleared.
1144 * This should never happen but just in case there is a bug
1145 * elsewhere, never add to the dirty list if the buffer is
1148 lock_ObtainWrite(&buf_globalLock);
1149 if (bp->dirtyp == NULL && cm_data.buf_dirtyListEndp != bp) {
1151 if (!cm_data.buf_dirtyListp) {
1152 cm_data.buf_dirtyListp = cm_data.buf_dirtyListEndp = bp;
1154 cm_data.buf_dirtyListEndp->dirtyp = bp;
1155 cm_data.buf_dirtyListEndp = bp;
1159 lock_ReleaseWrite(&buf_globalLock);
1162 /* clean all buffers, reset log pointers and invalidate all buffers.
1163 * Called with no locks held, and returns with same.
1165 * This function is guaranteed to clean and remove the log ptr of all the
1166 * buffers that were dirty or had non-zero log ptrs before the call was
1167 * made. That's sufficient to clean up any garbage left around by recovery,
1168 * which is all we're counting on this for; there may be newly created buffers
1169 * added while we're running, but that should be OK.
1171 * In an environment where there are no transactions (artificially imposed, for
1172 * example, when switching the database to raw mode), this function is used to
1173 * make sure that all updates have been written to the disk. In that case, we don't
1174 * really require that we forget the log association between pages and logs, but
1175 * it also doesn't hurt. Since raw mode I/O goes through this buffer package, we don't
1176 * have to worry about invalidating data in the buffers.
1178 * This function is used at the end of recovery as paranoia to get the recovered
1179 * database out to disk. It removes all references to the recovery log and cleans
1182 long buf_CleanAndReset(void)
1188 lock_ObtainWrite(&buf_globalLock);
1189 for(i=0; i<cm_data.buf_hashSize; i++) {
1190 for(bp = cm_data.buf_hashTablepp[i]; bp; bp = bp->hashp) {
1191 if ((bp->flags & CM_BUF_DIRTY) == CM_BUF_DIRTY) {
1193 lock_ReleaseWrite(&buf_globalLock);
1195 /* now no locks are held; clean buffer and go on */
1197 req.flags |= CM_REQ_NORETRY;
1199 buf_CleanAsync(bp, &req);
1200 buf_CleanWait(NULL, bp);
1202 /* relock and release buffer */
1203 lock_ObtainWrite(&buf_globalLock);
1204 buf_ReleaseLocked(bp);
1206 } /* over one bucket */
1207 } /* for loop over all hash buckets */
1210 lock_ReleaseWrite(&buf_globalLock);
1213 buf_ValidateBufQueues();
1214 #endif /* TESTING */
1216 /* and we're done */
1220 /* called without global lock being held, reserves buffers for callers
1221 * that need more than one held (not locked) at once.
1223 void buf_ReserveBuffers(afs_uint64 nbuffers)
1225 lock_ObtainWrite(&buf_globalLock);
1227 if (cm_data.buf_reservedBufs + nbuffers > cm_data.buf_maxReservedBufs) {
1228 cm_data.buf_reserveWaiting = 1;
1229 osi_Log1(buf_logp, "buf_ReserveBuffers waiting for %d bufs", nbuffers);
1230 osi_SleepW((LONG_PTR) &cm_data.buf_reservedBufs, &buf_globalLock);
1231 lock_ObtainWrite(&buf_globalLock);
1234 cm_data.buf_reservedBufs += nbuffers;
1238 lock_ReleaseWrite(&buf_globalLock);
1241 int buf_TryReserveBuffers(afs_uint64 nbuffers)
1245 lock_ObtainWrite(&buf_globalLock);
1246 if (cm_data.buf_reservedBufs + nbuffers > cm_data.buf_maxReservedBufs) {
1250 cm_data.buf_reservedBufs += nbuffers;
1253 lock_ReleaseWrite(&buf_globalLock);
1257 /* called without global lock held, releases reservation held by
1258 * buf_ReserveBuffers.
1260 void buf_UnreserveBuffers(afs_uint64 nbuffers)
1262 lock_ObtainWrite(&buf_globalLock);
1263 cm_data.buf_reservedBufs -= nbuffers;
1264 if (cm_data.buf_reserveWaiting) {
1265 cm_data.buf_reserveWaiting = 0;
1266 osi_Wakeup((LONG_PTR) &cm_data.buf_reservedBufs);
1268 lock_ReleaseWrite(&buf_globalLock);
1271 /* truncate the buffers past sizep, zeroing out the page, if we don't
1272 * end on a page boundary.
1274 * Requires cm_bufCreateLock to be write locked.
1276 long buf_Truncate(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
1280 cm_buf_t *nbufp; /* next buffer, if didRelease */
1286 /* assert that cm_bufCreateLock is held in write mode */
1287 lock_AssertWrite(&scp->bufCreateLock);
1289 i = BUF_FILEHASH(&scp->fid);
1291 lock_ObtainWrite(&buf_globalLock);
1292 bufp = cm_data.buf_fileHashTablepp[i];
1294 lock_ReleaseWrite(&buf_globalLock);
1298 buf_HoldLocked(bufp);
1299 lock_ReleaseWrite(&buf_globalLock);
1300 for(; bufp; bufp = nbufp) {
1301 lock_ObtainMutex(&bufp->mx);
1303 bufEnd.HighPart = 0;
1304 bufEnd.LowPart = cm_data.buf_blockSize;
1305 bufEnd = LargeIntegerAdd(bufEnd, bufp->offset);
1307 if (cm_FidCmp(&bufp->fid, &scp->fid) == 0 &&
1308 LargeIntegerLessThan(*sizep, bufEnd)) {
1309 buf_WaitIO(scp, bufp);
1311 lock_ObtainMutex(&scp->mx);
1313 /* make sure we have a callback (so we have the right value for
1314 * the length), and wait for it to be safe to do a truncate.
1316 code = cm_SyncOp(scp, bufp, userp, reqp, 0,
1317 CM_SCACHESYNC_NEEDCALLBACK
1318 | CM_SCACHESYNC_GETSTATUS
1319 | CM_SCACHESYNC_SETSIZE
1320 | CM_SCACHESYNC_BUFLOCKED);
1323 lock_ObtainWrite(&buf_globalLock);
1324 /* if we succeeded in our locking, and this applies to the right
1325 * file, and the truncate request overlaps the buffer either
1326 * totally or partially, then do something.
1328 if (code == 0 && cm_FidCmp(&bufp->fid, &scp->fid) == 0
1329 && LargeIntegerLessThan(*sizep, bufEnd)) {
1332 /* destroy the buffer, turning off its dirty bit, if
1333 * we're truncating the whole buffer. Otherwise, set
1334 * the dirty bit, and clear out the tail of the buffer
1335 * if we just overlap some.
1337 if (LargeIntegerLessThanOrEqualTo(*sizep, bufp->offset)) {
1338 /* truncating the entire page */
1339 bufp->flags &= ~CM_BUF_DIRTY;
1340 bufp->dataVersion = -1; /* known bad */
1341 bufp->dirtyCounter++;
1344 /* don't set dirty, since dirty implies
1345 * currently up-to-date. Don't need to do this,
1346 * since we'll update the length anyway.
1348 * Zero out remainder of the page, in case we
1349 * seek and write past EOF, and make this data
1352 bufferPos = sizep->LowPart & (cm_data.buf_blockSize - 1);
1353 osi_assert(bufferPos != 0);
1354 memset(bufp->datap + bufferPos, 0,
1355 cm_data.buf_blockSize - bufferPos);
1359 cm_SyncOpDone( scp, bufp,
1360 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
1361 | CM_SCACHESYNC_SETSIZE | CM_SCACHESYNC_BUFLOCKED);
1363 lock_ReleaseMutex(&scp->mx);
1364 lock_ReleaseMutex(&bufp->mx);
1367 nbufp = bufp->fileHashp;
1369 buf_HoldLocked(nbufp);
1371 /* This forces the loop to end and the error code
1372 * to be returned. */
1375 buf_ReleaseLocked(bufp);
1376 lock_ReleaseWrite(&buf_globalLock);
1380 buf_ValidateBufQueues();
1381 #endif /* TESTING */
1387 long buf_FlushCleanPages(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
1390 cm_buf_t *bp; /* buffer we're hacking on */
1395 i = BUF_FILEHASH(&scp->fid);
1398 lock_ObtainWrite(&buf_globalLock);
1399 bp = cm_data.buf_fileHashTablepp[i];
1402 lock_ReleaseWrite(&buf_globalLock);
1404 for (; bp; bp = nbp) {
1405 didRelease = 0; /* haven't released this buffer yet */
1407 /* clean buffer synchronously */
1408 if (cm_FidCmp(&bp->fid, &scp->fid) == 0) {
1409 lock_ObtainMutex(&bp->mx);
1411 /* start cleaning the buffer, and wait for it to finish */
1412 buf_CleanAsyncLocked(bp, reqp);
1413 buf_WaitIO(scp, bp);
1414 lock_ReleaseMutex(&bp->mx);
1416 code = (*cm_buf_opsp->Stabilizep)(scp, userp, reqp);
1420 lock_ObtainWrite(&buf_globalLock);
1421 /* actually, we only know that buffer is clean if ref
1422 * count is 1, since we don't have buffer itself locked.
1424 if (!(bp->flags & CM_BUF_DIRTY)) {
1425 if (bp->refCount == 1) { /* bp is held above */
1426 nbp = bp->fileHashp;
1428 buf_HoldLocked(nbp);
1429 buf_ReleaseLocked(bp);
1434 lock_ReleaseWrite(&buf_globalLock);
1436 (*cm_buf_opsp->Unstabilizep)(scp, userp);
1441 lock_ObtainWrite(&buf_globalLock);
1442 nbp = bp->fileHashp;
1444 buf_HoldLocked(nbp);
1445 buf_ReleaseLocked(bp);
1446 lock_ReleaseWrite(&buf_globalLock);
1448 } /* for loop over a bunch of buffers */
1451 buf_ValidateBufQueues();
1452 #endif /* TESTING */
1458 long buf_CleanVnode(struct cm_scache *scp, cm_user_t *userp, cm_req_t *reqp)
1462 cm_buf_t *bp; /* buffer we're hacking on */
1463 cm_buf_t *nbp; /* next one */
1466 i = BUF_FILEHASH(&scp->fid);
1468 lock_ObtainWrite(&buf_globalLock);
1469 bp = cm_data.buf_fileHashTablepp[i];
1472 lock_ReleaseWrite(&buf_globalLock);
1473 for (; bp; bp = nbp) {
1474 /* clean buffer synchronously */
1475 if (cm_FidCmp(&bp->fid, &scp->fid) == 0) {
1478 lock_ObtainMutex(&bp->mx);
1480 cm_ReleaseUser(bp->userp);
1482 lock_ReleaseMutex(&bp->mx);
1484 wasDirty = buf_CleanAsync(bp, reqp);
1485 buf_CleanWait(scp, bp);
1486 lock_ObtainMutex(&bp->mx);
1487 if (bp->flags & CM_BUF_ERROR) {
1492 lock_ReleaseMutex(&bp->mx);
1495 lock_ObtainWrite(&buf_globalLock);
1496 nbp = bp->fileHashp;
1498 buf_HoldLocked(nbp);
1499 buf_ReleaseLocked(bp);
1500 lock_ReleaseWrite(&buf_globalLock);
1501 } /* for loop over a bunch of buffers */
1504 buf_ValidateBufQueues();
1505 #endif /* TESTING */
1513 buf_ValidateBufQueues(void)
1515 cm_buf_t * bp, *bpb, *bpf, *bpa;
1516 afs_uint32 countf=0, countb=0, counta=0;
1518 lock_ObtainRead(&buf_globalLock);
1519 for (bp = cm_data.buf_freeListEndp; bp; bp=(cm_buf_t *) osi_QPrev(&bp->q)) {
1520 if (bp->magic != CM_BUF_MAGIC)
1521 osi_panic("buf magic error",__FILE__,__LINE__);
1526 for (bp = cm_data.buf_freeListp; bp; bp=(cm_buf_t *) osi_QNext(&bp->q)) {
1527 if (bp->magic != CM_BUF_MAGIC)
1528 osi_panic("buf magic error",__FILE__,__LINE__);
1533 for (bp = cm_data.buf_allp; bp; bp=bp->allp) {
1534 if (bp->magic != CM_BUF_MAGIC)
1535 osi_panic("buf magic error",__FILE__,__LINE__);
1539 lock_ReleaseRead(&buf_globalLock);
1541 if (countb != countf)
1542 osi_panic("buf magic error",__FILE__,__LINE__);
1544 if (counta != cm_data.buf_nbuffers)
1545 osi_panic("buf magic error",__FILE__,__LINE__);
1547 #endif /* TESTING */
1549 /* dump the contents of the buf_hashTablepp. */
1550 int cm_DumpBufHashTable(FILE *outputFile, char *cookie, int lock)
1557 if (cm_data.buf_hashTablepp == NULL)
1561 lock_ObtainRead(&buf_globalLock);
1563 StringCbPrintfA(output, sizeof(output), "%s - dumping buf_HashTable - buf_hashSize=%d\n",
1564 cookie, cm_data.buf_hashSize);
1565 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1567 for (i = 0; i < cm_data.buf_hashSize; i++)
1569 for (bp = cm_data.buf_hashTablepp[i]; bp; bp=bp->hashp)
1573 StringCbPrintfA(output, sizeof(output), "vnode=%d, unique=%d), size=%d refCount=%d\n",
1574 cookie, (void *)bp, i, bp->fid.cell, bp->fid.volume,
1575 bp->fid.vnode, bp->fid.unique, bp->size, bp->refCount);
1576 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1581 StringCbPrintfA(output, sizeof(output), "%s - Done dumping buf_HashTable.\n", cookie);
1582 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
1585 lock_ReleaseRead(&buf_globalLock);
1589 void buf_ForceTrace(BOOL flush)
1598 len = GetTempPath(sizeof(buf)-10, buf);
1599 StringCbCopyA(&buf[len], sizeof(buf)-len, "/afs-buffer.log");
1600 handle = CreateFile(buf, GENERIC_WRITE, FILE_SHARE_READ,
1601 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1602 if (handle == INVALID_HANDLE_VALUE) {
1603 osi_panic("Cannot create log file", __FILE__, __LINE__);
1605 osi_LogPrint(buf_logp, handle);
1607 FlushFileBuffers(handle);
1608 CloseHandle(handle);
1611 long buf_DirtyBuffersExist(cm_fid_t *fidp)
1614 afs_uint32 bcount = 0;
1616 for (bp = cm_data.buf_allp; bp; bp=bp->allp, bcount++) {
1617 if (!cm_FidCmp(fidp, &bp->fid) && (bp->flags & CM_BUF_DIRTY))
1624 long buf_CleanDirtyBuffers(cm_scache_t *scp)
1627 afs_uint32 bcount = 0;
1628 cm_fid_t * fidp = &scp->fid;
1630 for (bp = cm_data.buf_allp; bp; bp=bp->allp, bcount++) {
1631 if (!cm_FidCmp(fidp, &bp->fid) && (bp->flags & CM_BUF_DIRTY)) {
1633 lock_ObtainMutex(&bp->mx);
1634 bp->cmFlags &= ~CM_BUF_CMSTORING;
1635 bp->flags &= ~CM_BUF_DIRTY;
1636 bp->flags |= CM_BUF_ERROR;
1637 bp->error = VNOVNODE;
1638 bp->dataVersion = -1; /* bad */
1640 if (bp->flags & CM_BUF_WAITING) {
1641 osi_Log2(buf_logp, "BUF CleanDirtyBuffers Waking [scp 0x%x] bp 0x%x", scp, bp);
1642 osi_Wakeup((long) &bp);
1644 lock_ReleaseMutex(&bp->mx);