windows-buf-mx-20080301
[openafs.git] / src / WINNT / afsd / cm_buf.c
index 7acdd4d..e914e5f 100644 (file)
@@ -89,31 +89,50 @@ static int buf_ShutdownFlag = 0;
 
 void buf_HoldLocked(cm_buf_t *bp)
 {
-    osi_assert(bp->magic == CM_BUF_MAGIC);
-    bp->refCount++;
+    osi_assertx(bp->magic == CM_BUF_MAGIC,"incorrect cm_buf_t magic");
+    InterlockedIncrement(&bp->refCount);
 }
 
 /* hold a reference to an already held buffer */
 void buf_Hold(cm_buf_t *bp)
 {
-    lock_ObtainWrite(&buf_globalLock);
+    lock_ObtainRead(&buf_globalLock);
     buf_HoldLocked(bp);
-    lock_ReleaseWrite(&buf_globalLock);
+    lock_ReleaseRead(&buf_globalLock);
 }
 
 /* code to drop reference count while holding buf_globalLock */
-void buf_ReleaseLocked(cm_buf_t *bp)
+void buf_ReleaseLocked(cm_buf_t *bp, afs_uint32 writeLocked)
 {
+    afs_int32 refCount;
+
+    if (writeLocked)
+        lock_AssertWrite(&buf_globalLock);
+    else
+        lock_AssertRead(&buf_globalLock);
+
     /* ensure that we're in the LRU queue if our ref count is 0 */
-    osi_assert(bp->magic == CM_BUF_MAGIC);
+    osi_assertx(bp->magic == CM_BUF_MAGIC,"incorrect cm_buf_t magic");
+
+    refCount = InterlockedDecrement(&bp->refCount);
 #ifdef DEBUG
-    if (bp->refCount == 0)
+    if (refCount < 0)
        osi_panic("buf refcount 0",__FILE__,__LINE__);;
 #else
-    osi_assert(bp->refCount > 0);
+    osi_assertx(refCount >= 0, "cm_buf_t refCount == 0");
 #endif
-    if (--bp->refCount == 0) {
-        if (!(bp->flags & CM_BUF_INLRU)) {
+    if (refCount == 0) {
+        /* 
+         * If we are read locked there could be a race condition
+         * with buf_Find() so we must obtain a write lock and
+         * double check that the refCount is actually zero
+         * before we remove the buffer from the LRU queue.
+         */
+        if (!writeLocked)
+            lock_ConvertRToW(&buf_globalLock);
+
+        if (bp->refCount == 0 &&
+            !(bp->flags & CM_BUF_INLRU)) {
             osi_QAdd((osi_queue_t **) &cm_data.buf_freeListp, &bp->q);
 
             /* watch for transition from empty to one element */
@@ -121,15 +140,40 @@ void buf_ReleaseLocked(cm_buf_t *bp)
                 cm_data.buf_freeListEndp = cm_data.buf_freeListp;
             bp->flags |= CM_BUF_INLRU;
         }
+
+        if (!writeLocked)
+            lock_ConvertWToR(&buf_globalLock);
     }
 }       
 
 /* release a buffer.  Buffer must be referenced, but unlocked. */
 void buf_Release(cm_buf_t *bp)
 {
-    lock_ObtainWrite(&buf_globalLock);
-    buf_ReleaseLocked(bp);
-    lock_ReleaseWrite(&buf_globalLock);
+    afs_int32 refCount;
+
+    /* ensure that we're in the LRU queue if our ref count is 0 */
+    osi_assertx(bp->magic == CM_BUF_MAGIC,"incorrect cm_buf_t magic");
+
+    refCount = InterlockedDecrement(&bp->refCount);
+#ifdef DEBUG
+    if (refCount < 0)
+       osi_panic("buf refcount 0",__FILE__,__LINE__);;
+#else
+    osi_assertx(refCount >= 0, "cm_buf_t refCount == 0");
+#endif
+    if (refCount == 0) {
+        lock_ObtainWrite(&buf_globalLock);
+        if (bp->refCount == 0 && 
+            !(bp->flags & CM_BUF_INLRU)) {
+            osi_QAdd((osi_queue_t **) &cm_data.buf_freeListp, &bp->q);
+
+            /* watch for transition from empty to one element */
+            if (!cm_data.buf_freeListEndp)
+                cm_data.buf_freeListEndp = cm_data.buf_freeListp;
+            bp->flags |= CM_BUF_INLRU;
+        }
+        lock_ReleaseWrite(&buf_globalLock);
+    }
 }
 
 /* incremental sync daemon.  Writes all dirty buffers every 5000 ms */
@@ -150,11 +194,10 @@ void buf_IncrSyncer(long parm)
 
         /* now go through our percentage of the buffers */
         for (bpp = &cm_data.buf_dirtyListp; bp = *bpp; ) {
-
            /* all dirty buffers are held when they are added to the
             * dirty list.  No need for an additional hold.
             */
-
+            lock_ObtainMutex(&bp->mx);
            if (bp->flags & CM_BUF_DIRTY) {
                /* start cleaning the buffer; don't touch log pages since
                 * the log code counts on knowing exactly who is writing
@@ -162,33 +205,27 @@ void buf_IncrSyncer(long parm)
                 */
                cm_InitReq(&req);
                req.flags |= CM_REQ_NORETRY;
-               wasDirty |= buf_CleanAsync(bp, &req);
+               wasDirty |= buf_CleanAsyncLocked(bp, &req);
            }
 
            /* the buffer may or may not have been dirty
             * and if dirty may or may not have been cleaned
             * successfully.  check the dirty flag again.  
             */
-           if (!(bp->flags & CM_BUF_DIRTY)) {
-               lock_ObtainMutex(&bp->mx);
-               if (!(bp->flags & CM_BUF_DIRTY)) {
-                   /* remove the buffer from the dirty list */
-                   lock_ObtainWrite(&buf_globalLock);
-                   *bpp = bp->dirtyp;
-                   bp->dirtyp = NULL;
-                   if (cm_data.buf_dirtyListp == NULL)
-                       cm_data.buf_dirtyListEndp = NULL;
-                   buf_ReleaseLocked(bp);
-                   lock_ReleaseWrite(&buf_globalLock);
-               } else {
-                   /* advance the pointer so we don't loop forever */
-                   bpp = &bp->dirtyp;
-               }
-               lock_ReleaseMutex(&bp->mx);
-           } else {
-               /* advance the pointer so we don't loop forever */
-               bpp = &bp->dirtyp;
-           }
+            if (!(bp->flags & CM_BUF_DIRTY)) {
+                /* remove the buffer from the dirty list */
+                lock_ObtainWrite(&buf_globalLock);
+                *bpp = bp->dirtyp;
+                bp->dirtyp = NULL;
+                if (cm_data.buf_dirtyListp == NULL)
+                    cm_data.buf_dirtyListEndp = NULL;
+                buf_ReleaseLocked(bp, TRUE);
+                lock_ReleaseWrite(&buf_globalLock);
+            } else {
+                /* advance the pointer so we don't loop forever */
+                bpp = &bp->dirtyp;
+            }
+            lock_ReleaseMutex(&bp->mx);
         }      /* for loop over a bunch of buffers */
     }          /* whole daemon's while loop */
 }
@@ -321,8 +358,10 @@ long buf_Init(int newFile, cm_buf_ops_t *opsp, afs_uint64 nbuffers)
             cm_data.buf_allp = NULL;
             
             for (i=0; i<cm_data.buf_nbuffers; i++) {
-                osi_assert(bp >= cm_data.bufHeaderBaseAddress && bp < (cm_buf_t *)cm_data.bufDataBaseAddress);
-                osi_assert(data >= cm_data.bufDataBaseAddress && data < cm_data.bufEndOfData);
+                osi_assertx(bp >= cm_data.bufHeaderBaseAddress && bp < (cm_buf_t *)cm_data.bufDataBaseAddress, 
+                            "invalid cm_buf_t address");
+                osi_assertx(data >= cm_data.bufDataBaseAddress && data < cm_data.bufEndOfData,
+                            "invalid cm_buf_t data address");
                 
                 /* allocate and zero some storage */
                 memset(bp, 0, sizeof(cm_buf_t));
@@ -436,8 +475,8 @@ void buf_WaitIO(cm_scache_t * scp, cm_buf_t *bp)
     int release = 0;
 
     if (scp)
-        osi_assert(scp->magic == CM_SCACHE_MAGIC);
-    osi_assert(bp->magic == CM_BUF_MAGIC);
+        osi_assertx(scp->magic == CM_SCACHE_MAGIC, "invalid cm_scache_t magic");
+    osi_assertx(bp->magic == CM_BUF_MAGIC, "invalid cm_buf_t magic");
 
     while (1) {
         /* if no IO is happening, we're done */
@@ -526,9 +565,9 @@ cm_buf_t *buf_Find(struct cm_scache *scp, osi_hyper_t *offsetp)
 {
     cm_buf_t *bp;
 
-    lock_ObtainWrite(&buf_globalLock);
+    lock_ObtainRead(&buf_globalLock);
     bp = buf_FindLocked(scp, offsetp);
-    lock_ReleaseWrite(&buf_globalLock);
+    lock_ReleaseRead(&buf_globalLock);
 
     return bp;
 }       
@@ -549,7 +588,7 @@ long buf_CleanAsyncLocked(cm_buf_t *bp, cm_req_t *reqp)
     cm_scache_t * scp = NULL;
     osi_hyper_t offset;
 
-    osi_assert(bp->magic == CM_BUF_MAGIC);
+    osi_assertx(bp->magic == CM_BUF_MAGIC, "invalid cm_buf_t magic");
 
     while ((bp->flags & CM_BUF_DIRTY) == CM_BUF_DIRTY) {
        isdirty = 1;
@@ -561,7 +600,16 @@ long buf_CleanAsyncLocked(cm_buf_t *bp, cm_req_t *reqp)
 
             offset = bp->offset;
             LargeIntegerAdd(offset, ConvertLongToLargeInteger(bp->dirty_offset));
-           code = (*cm_buf_opsp->Writep)(scp, &offset, bp->dirty_length, 0, bp->userp, reqp);
+           code = (*cm_buf_opsp->Writep)(scp, &offset, 
+#if 1
+                                           /* we might as well try to write all of the contiguous 
+                                            * dirty buffers in one RPC 
+                                            */
+                                           cm_chunkSize,
+#else
+                                          bp->dirty_length, 
+#endif
+                                          0, bp->userp, reqp);
            osi_Log3(buf_logp, "buf_CleanAsyncLocked I/O on scp 0x%p buf 0x%p, done=%d", scp, bp, code);
 
            cm_ReleaseSCache(scp);
@@ -576,13 +624,13 @@ long buf_CleanAsyncLocked(cm_buf_t *bp, cm_req_t *reqp)
         * because we aren't going to be able to write this data to the file
         * server.
         */
-       if (code == CM_ERROR_NOSUCHFILE){
+       if (code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BADFD){
            bp->flags &= ~CM_BUF_DIRTY;
            bp->flags |= CM_BUF_ERROR;
             bp->dirty_offset = 0;
             bp->dirty_length = 0;
-           bp->error = CM_ERROR_NOSUCHFILE;
-           bp->dataVersion = -1; /* bad */
+           bp->error = code;
+           bp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
            bp->dirtyCounter++;
        }
 
@@ -624,12 +672,12 @@ long buf_CleanAsyncLocked(cm_buf_t *bp, cm_req_t *reqp)
  */
 void buf_Recycle(cm_buf_t *bp)
 {
-    int i;
+    afs_uint32 i;
     cm_buf_t **lbpp;
     cm_buf_t *tbp;
     cm_buf_t *prevBp, *nextBp;
 
-    osi_assert(bp->magic == CM_BUF_MAGIC);
+    osi_assertx(bp->magic == CM_BUF_MAGIC, "invalid cm_buf_t magic");
 
     /* if we get here, we know that the buffer still has a 0 ref count,
      * and that it is clean and has no currently pending I/O.  This is
@@ -641,8 +689,9 @@ void buf_Recycle(cm_buf_t *bp)
     osi_Log3( buf_logp, "buf_Recycle recycles 0x%p, off 0x%x:%08x",
               bp, bp->offset.HighPart, bp->offset.LowPart);
 
-    osi_assert(bp->refCount == 0);
-    osi_assert(!(bp->flags & (CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)));
+    osi_assertx(bp->refCount == 0, "cm_buf_t refcount != 0");
+    osi_assertx(!(bp->flags & (CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)),
+                "incorrect cm_buf_t flags");
     lock_AssertWrite(&buf_globalLock);
 
     if (bp->flags & CM_BUF_INHASH) {
@@ -651,19 +700,23 @@ void buf_Recycle(cm_buf_t *bp)
         i = BUF_HASH(&bp->fid, &bp->offset);
         lbpp = &(cm_data.buf_scacheHashTablepp[i]);
         for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = *lbpp) {
-            if (tbp == bp) break;
+            if (tbp == bp) 
+                break;
         }
 
         /* we better find it */
         osi_assertx(tbp != NULL, "buf_Recycle: hash table screwup");
 
         *lbpp = bp->hashp;     /* hash out */
+        bp->hashp = NULL;
 
         /* Remove from file hash */
 
         i = BUF_FILEHASH(&bp->fid);
         prevBp = bp->fileHashBackp;
+        bp->fileHashBackp = NULL;
         nextBp = bp->fileHashp;
+        bp->fileHashp = NULL;
         if (prevBp)
             prevBp->fileHashp = nextBp;
         else
@@ -697,9 +750,9 @@ void buf_Recycle(cm_buf_t *bp)
  */
 long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bufpp)
 {
-    cm_buf_t *bp;              /* buffer we're dealing with */
+    cm_buf_t *bp;      /* buffer we're dealing with */
     cm_buf_t *nextBp;  /* next buffer in file hash chain */
-    long i;                    /* temp */
+    afs_uint32 i;      /* temp */
     cm_req_t req;
 
     cm_InitReq(&req);  /* just in case */
@@ -710,6 +763,7 @@ long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bu
 
     while(1) {
       retry:
+        lock_ObtainRead(&scp->bufCreateLock);
         lock_ObtainWrite(&buf_globalLock);
         /* check to see if we lost the race */
         if (scp) {
@@ -720,6 +774,7 @@ long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bu
                 */
                 bp->refCount--;
                 lock_ReleaseWrite(&buf_globalLock);
+                lock_ReleaseRead(&scp->bufCreateLock);
                 return CM_BUF_EXISTS;
             }
         }
@@ -728,6 +783,7 @@ long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bu
        if (!cm_data.buf_freeListEndp)
        {
            lock_ReleaseWrite(&buf_globalLock);
+            lock_ReleaseRead(&scp->bufCreateLock);
            osi_Log0(afsd_logp, "buf_GetNewLocked: Free Buffer List is empty - sleeping 200ms");
            Sleep(200);
            goto retry;
@@ -784,6 +840,7 @@ long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bu
                  */
                 buf_HoldLocked(bp);
                 lock_ReleaseWrite(&buf_globalLock);
+                lock_ReleaseRead(&scp->bufCreateLock);
 
                 /* grab required lock and clean; this only
                  * starts the I/O.  By the time we're back,
@@ -809,7 +866,7 @@ long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bu
 
             /* clean up junk flags */
             bp->flags &= ~(CM_BUF_EOF | CM_BUF_ERROR);
-            bp->dataVersion = -1;      /* unknown so far */
+            bp->dataVersion = CM_BUF_VERSION_BAD;      /* unknown so far */
 
             /* now hash in as our new buffer, and give it the
              * appropriate label, if requested.
@@ -860,6 +917,7 @@ long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bu
             bp->refCount = 1;
                         
             lock_ReleaseWrite(&buf_globalLock);
+            lock_ReleaseRead(&scp->bufCreateLock);
             *bufpp = bp;
 
 #ifdef TESTING
@@ -868,6 +926,7 @@ long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bu
             return 0;
         } /* for all buffers in lru queue */
         lock_ReleaseWrite(&buf_globalLock);
+        lock_ReleaseRead(&scp->bufCreateLock);
        osi_Log0(afsd_logp, "buf_GetNewLocked: Free Buffer List has no buffers with a zero refcount - sleeping 100ms");
        Sleep(100);             /* give some time for a buffer to be freed */
     }  /* while loop over everything */
@@ -990,7 +1049,7 @@ long buf_Get(struct cm_scache *scp, osi_hyper_t *offsetp, cm_buf_t **bufpp)
      */
     if (created) {
         /* load the page; freshly created pages should be idle */
-        osi_assert(!(bp->flags & (CM_BUF_READING | CM_BUF_WRITING)));
+        osi_assertx(!(bp->flags & (CM_BUF_READING | CM_BUF_WRITING)), "incorrect cm_buf_t flags");
 
         /* start the I/O; may drop lock */
         bp->flags |= CM_BUF_READING;
@@ -1096,7 +1155,7 @@ long buf_CountFreeList(void)
 long buf_CleanAsync(cm_buf_t *bp, cm_req_t *reqp)
 {
     long code;
-    osi_assert(bp->magic == CM_BUF_MAGIC);
+    osi_assertx(bp->magic == CM_BUF_MAGIC, "invalid cm_buf_t magic");
 
     lock_ObtainMutex(&bp->mx);
     code = buf_CleanAsyncLocked(bp, reqp);
@@ -1106,15 +1165,17 @@ long buf_CleanAsync(cm_buf_t *bp, cm_req_t *reqp)
 }       
 
 /* wait for a buffer's cleaning to finish */
-void buf_CleanWait(cm_scache_t * scp, cm_buf_t *bp)
+void buf_CleanWait(cm_scache_t * scp, cm_buf_t *bp, afs_uint32 locked)
 {
-    osi_assert(bp->magic == CM_BUF_MAGIC);
+    osi_assertx(bp->magic == CM_BUF_MAGIC, "invalid cm_buf_t magic");
 
-    lock_ObtainMutex(&bp->mx);
+    if (!locked)
+        lock_ObtainMutex(&bp->mx);
     if (bp->flags & CM_BUF_WRITING) {
         buf_WaitIO(scp, bp);
     }
-    lock_ReleaseMutex(&bp->mx);
+    if (!locked)
+        lock_ReleaseMutex(&bp->mx);
 }       
 
 /* set the dirty flag on a buffer, and set associated write-ahead log,
@@ -1124,10 +1185,9 @@ void buf_CleanWait(cm_scache_t * scp, cm_buf_t *bp)
  */
 void buf_SetDirty(cm_buf_t *bp, afs_uint32 offset, afs_uint32 length)
 {
-    osi_assert(bp->magic == CM_BUF_MAGIC);
-    osi_assert(bp->refCount > 0);
+    osi_assertx(bp->magic == CM_BUF_MAGIC, "invalid cm_buf_t magic");
+    osi_assertx(bp->refCount > 0, "cm_buf_t refcount 0");
 
-    lock_ObtainWrite(&buf_globalLock);
     if (bp->flags & CM_BUF_DIRTY) {
 
        osi_Log1(buf_logp, "buf_SetDirty 0x%p already dirty", bp);
@@ -1168,6 +1228,7 @@ void buf_SetDirty(cm_buf_t *bp, afs_uint32 offset, afs_uint32 length)
          * elsewhere, never add to the dirty list if the buffer is 
          * already there.
          */
+        lock_ObtainWrite(&buf_globalLock);
         if (bp->dirtyp == NULL && cm_data.buf_dirtyListEndp != bp) {
             buf_HoldLocked(bp);
             if (!cm_data.buf_dirtyListp) {
@@ -1178,8 +1239,8 @@ void buf_SetDirty(cm_buf_t *bp, afs_uint32 offset, afs_uint32 length)
             }
             bp->dirtyp = NULL;
         }
+        lock_ReleaseWrite(&buf_globalLock);
     }
-    lock_ReleaseWrite(&buf_globalLock);
 }
 
 /* clean all buffers, reset log pointers and invalidate all buffers.
@@ -1208,29 +1269,29 @@ long buf_CleanAndReset(void)
     cm_buf_t *bp;
     cm_req_t req;
 
-    lock_ObtainWrite(&buf_globalLock);
+    lock_ObtainRead(&buf_globalLock);
     for(i=0; i<cm_data.buf_hashSize; i++) {
         for(bp = cm_data.buf_scacheHashTablepp[i]; bp; bp = bp->hashp) {
             if ((bp->flags & CM_BUF_DIRTY) == CM_BUF_DIRTY) {
                 buf_HoldLocked(bp);
-                lock_ReleaseWrite(&buf_globalLock);
+                lock_ReleaseRead(&buf_globalLock);
 
                 /* now no locks are held; clean buffer and go on */
                 cm_InitReq(&req);
                req.flags |= CM_REQ_NORETRY;
 
                buf_CleanAsync(bp, &req);
-               buf_CleanWait(NULL, bp);
+               buf_CleanWait(NULL, bp, FALSE);
 
                 /* relock and release buffer */
-                lock_ObtainWrite(&buf_globalLock);
-                buf_ReleaseLocked(bp);
+                lock_ObtainRead(&buf_globalLock);
+                buf_ReleaseLocked(bp, FALSE);
             } /* dirty */
         } /* over one bucket */
     }  /* for loop over all hash buckets */
 
     /* release locks */
-    lock_ReleaseWrite(&buf_globalLock);
+    lock_ReleaseRead(&buf_globalLock);
 
 #ifdef TESTING
     buf_ValidateBufQueues();
@@ -1304,22 +1365,22 @@ long buf_Truncate(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
     osi_hyper_t bufEnd;
     long code;
     long bufferPos;
-    long i;
+    afs_uint32 i;
 
     /* assert that cm_bufCreateLock is held in write mode */
     lock_AssertWrite(&scp->bufCreateLock);
 
     i = BUF_FILEHASH(&scp->fid);
 
-    lock_ObtainWrite(&buf_globalLock);
+    lock_ObtainRead(&buf_globalLock);
     bufp = cm_data.buf_fileHashTablepp[i];
     if (bufp == NULL) {
-        lock_ReleaseWrite(&buf_globalLock);
+        lock_ReleaseRead(&buf_globalLock);
         return 0;
     }
 
     buf_HoldLocked(bufp);
-    lock_ReleaseWrite(&buf_globalLock);
+    lock_ReleaseRead(&buf_globalLock);
     while (bufp) {
         lock_ObtainMutex(&bufp->mx);
 
@@ -1343,7 +1404,6 @@ long buf_Truncate(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
                           | CM_SCACHESYNC_BUFLOCKED);
 
        
-       lock_ObtainWrite(&buf_globalLock);
        /* if we succeeded in our locking, and this applies to the right
          * file, and the truncate request overlaps the buffer either
          * totally or partially, then do something.
@@ -1362,7 +1422,7 @@ long buf_Truncate(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
                 bufp->flags &= ~CM_BUF_DIRTY;
                 bufp->dirty_offset = 0;
                 bufp->dirty_length = 0;
-                bufp->dataVersion = -1;        /* known bad */
+                bufp->dataVersion = CM_BUF_VERSION_BAD;        /* known bad */
                 bufp->dirtyCounter++;
             }
             else {
@@ -1375,7 +1435,7 @@ long buf_Truncate(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
                  * visible again.
                  */
                 bufferPos = sizep->LowPart & (cm_data.buf_blockSize - 1);
-                osi_assert(bufferPos != 0);
+                osi_assertx(bufferPos != 0, "non-zero bufferPos");
                 memset(bufp->datap + bufferPos, 0,
                         cm_data.buf_blockSize - bufferPos);
             }
@@ -1391,14 +1451,13 @@ long buf_Truncate(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
        if (!code) {
            nbufp = bufp->fileHashp;
            if (nbufp) 
-               buf_HoldLocked(nbufp);
+               buf_Hold(nbufp);
        } else {
            /* This forces the loop to end and the error code
             * to be returned. */
            nbufp = NULL;
        }
-       buf_ReleaseLocked(bufp);
-       lock_ReleaseWrite(&buf_globalLock);
+       buf_Release(bufp);
        bufp = nbufp;
     }
 
@@ -1416,16 +1475,16 @@ long buf_FlushCleanPages(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
     cm_buf_t *bp;              /* buffer we're hacking on */
     cm_buf_t *nbp;
     int didRelease;
-    long i;
+    afs_uint32 i;
 
     i = BUF_FILEHASH(&scp->fid);
 
     code = 0;
-    lock_ObtainWrite(&buf_globalLock);
+    lock_ObtainRead(&buf_globalLock);
     bp = cm_data.buf_fileHashTablepp[i];
     if (bp) 
         buf_HoldLocked(bp);
-    lock_ReleaseWrite(&buf_globalLock);
+    lock_ReleaseRead(&buf_globalLock);
     
     for (; bp; bp = nbp) {
         didRelease = 0;        /* haven't released this buffer yet */
@@ -1440,36 +1499,53 @@ long buf_FlushCleanPages(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
             lock_ReleaseMutex(&bp->mx);
 
             code = (*cm_buf_opsp->Stabilizep)(scp, userp, reqp);
-            if (code) 
+            if (code && code != CM_ERROR_BADFD) 
                 goto skip;
 
-            lock_ObtainWrite(&buf_globalLock);
+            if (code == CM_ERROR_BADFD) {
+                /* if the scp's FID is bad its because we received VNOVNODE 
+                 * when attempting to FetchStatus before the write.  This
+                 * page therefore contains data that can no longer be stored.
+                 */
+                lock_ObtainMutex(&bp->mx);
+                bp->flags &= ~CM_BUF_DIRTY;
+                bp->flags |= CM_BUF_ERROR;
+                bp->error = CM_ERROR_BADFD;
+                bp->dirty_offset = 0;
+                bp->dirty_length = 0;
+                bp->dataVersion = CM_BUF_VERSION_BAD;  /* known bad */
+                bp->dirtyCounter++;
+                lock_ReleaseMutex(&bp->mx);
+            }
+
             /* actually, we only know that buffer is clean if ref
              * count is 1, since we don't have buffer itself locked.
              */
             if (!(bp->flags & CM_BUF_DIRTY)) {
+                lock_ObtainWrite(&buf_globalLock);
                 if (bp->refCount == 1) {       /* bp is held above */
                     nbp = bp->fileHashp;
                     if (nbp) 
                         buf_HoldLocked(nbp);
-                    buf_ReleaseLocked(bp);
+                    buf_ReleaseLocked(bp, TRUE);
                     didRelease = 1;
                     buf_Recycle(bp);
                 }
+                lock_ReleaseWrite(&buf_globalLock);
             }
-            lock_ReleaseWrite(&buf_globalLock);
 
-            (*cm_buf_opsp->Unstabilizep)(scp, userp);
+           if (code == 0)
+               (*cm_buf_opsp->Unstabilizep)(scp, userp);
         }
 
       skip:
         if (!didRelease) {
-            lock_ObtainWrite(&buf_globalLock);
+            lock_ObtainRead(&buf_globalLock);
             nbp = bp->fileHashp;
            if (nbp)
                 buf_HoldLocked(nbp);
-            buf_ReleaseLocked(bp);
-            lock_ReleaseWrite(&buf_globalLock);
+            buf_ReleaseLocked(bp, FALSE);
+            lock_ReleaseRead(&buf_globalLock);
         }
     }  /* for loop over a bunch of buffers */
 
@@ -1482,16 +1558,17 @@ long buf_FlushCleanPages(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
 }       
 
 /* Must be called with scp->mx held */
-long buf_ForceDataVersion(cm_scache_t * scp, afs_uint32 fromVersion, afs_uint32 toVersion)
+long buf_ForceDataVersion(cm_scache_t * scp, afs_uint64 fromVersion, afs_uint64 toVersion)
 {
     cm_buf_t * bp;
-    cm_buf_t * nbp;
-    unsigned int i;
+    afs_uint32 i;
     int found = 0;
 
+    lock_AssertMutex(&scp->mx);
+
     i = BUF_FILEHASH(&scp->fid);
 
-    lock_ObtainWrite(&buf_globalLock);
+    lock_ObtainRead(&buf_globalLock);
 
     for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp = bp->fileHashp) {
         if (cm_FidCmp(&bp->fid, &scp->fid) == 0) {
@@ -1501,7 +1578,7 @@ long buf_ForceDataVersion(cm_scache_t * scp, afs_uint32 fromVersion, afs_uint32
             }
         }
     }
-    lock_ReleaseWrite(&buf_globalLock);
+    lock_ReleaseRead(&buf_globalLock);
 
     if (found)
         return 0;
@@ -1515,29 +1592,27 @@ long buf_CleanVnode(struct cm_scache *scp, cm_user_t *userp, cm_req_t *reqp)
     long wasDirty = 0;
     cm_buf_t *bp;              /* buffer we're hacking on */
     cm_buf_t *nbp;             /* next one */
-    long i;
+    afs_uint32 i;
 
     i = BUF_FILEHASH(&scp->fid);
 
-    lock_ObtainWrite(&buf_globalLock);
+    lock_ObtainRead(&buf_globalLock);
     bp = cm_data.buf_fileHashTablepp[i];
     if (bp) 
         buf_HoldLocked(bp);
-    lock_ReleaseWrite(&buf_globalLock);
+    lock_ReleaseRead(&buf_globalLock);
     for (; bp; bp = nbp) {
         /* clean buffer synchronously */
         if (cm_FidCmp(&bp->fid, &scp->fid) == 0) {
+            lock_ObtainMutex(&bp->mx);
             if (userp) {
                 cm_HoldUser(userp);
-                lock_ObtainMutex(&bp->mx);
                 if (bp->userp) 
                     cm_ReleaseUser(bp->userp);
                 bp->userp = userp;
-                lock_ReleaseMutex(&bp->mx);
             }   
-            wasDirty = buf_CleanAsync(bp, reqp);
-           buf_CleanWait(scp, bp);
-            lock_ObtainMutex(&bp->mx);
+            wasDirty = buf_CleanAsyncLocked(bp, reqp);
+           buf_CleanWait(scp, bp, TRUE);
             if (bp->flags & CM_BUF_ERROR) {
                code = bp->error;
                 if (code == 0) 
@@ -1546,12 +1621,12 @@ long buf_CleanVnode(struct cm_scache *scp, cm_user_t *userp, cm_req_t *reqp)
             lock_ReleaseMutex(&bp->mx);
         }
 
-        lock_ObtainWrite(&buf_globalLock);
+        lock_ObtainRead(&buf_globalLock);
         nbp = bp->fileHashp;
         if (nbp) 
             buf_HoldLocked(nbp);
-        buf_ReleaseLocked(bp);
-        lock_ReleaseWrite(&buf_globalLock);
+        buf_ReleaseLocked(bp, FALSE);
+        lock_ReleaseRead(&buf_globalLock);
     }  /* for loop over a bunch of buffers */
 
 #ifdef TESTING
@@ -1624,7 +1699,7 @@ int cm_DumpBufHashTable(FILE *outputFile, char *cookie, int lock)
         {
            StringCbPrintfA(output, sizeof(output), 
                            "%s bp=0x%08X, hash=%d, fid (cell=%d, volume=%d, "
-                           "vnode=%d, unique=%d), offset=%x:%08x, dv=%d, "
+                           "vnode=%d, unique=%d), offset=%x:%08x, dv=%I64d, "
                            "flags=0x%x, cmFlags=0x%x, refCount=%d\r\n",
                             cookie, (void *)bp, i, bp->fid.cell, bp->fid.volume, 
                             bp->fid.vnode, bp->fid.unique, bp->offset.HighPart, 
@@ -1642,7 +1717,7 @@ int cm_DumpBufHashTable(FILE *outputFile, char *cookie, int lock)
     for(bp = cm_data.buf_freeListEndp; bp; bp=(cm_buf_t *) osi_QPrev(&bp->q)) {
        StringCbPrintfA(output, sizeof(output), 
                         "%s bp=0x%08X, fid (cell=%d, volume=%d, "
-                        "vnode=%d, unique=%d), offset=%x:%08x, dv=%d, "
+                        "vnode=%d, unique=%d), offset=%x:%08x, dv=%I64d, "
                         "flags=0x%x, cmFlags=0x%x, refCount=%d\r\n",
                         cookie, (void *)bp, bp->fid.cell, bp->fid.volume, 
                         bp->fid.vnode, bp->fid.unique, bp->offset.HighPart, 
@@ -1658,7 +1733,7 @@ int cm_DumpBufHashTable(FILE *outputFile, char *cookie, int lock)
     for(bp = cm_data.buf_dirtyListEndp; bp; bp=(cm_buf_t *) osi_QPrev(&bp->q)) {
        StringCbPrintfA(output, sizeof(output), 
                         "%s bp=0x%08X, fid (cell=%d, volume=%d, "
-                        "vnode=%d, unique=%d), offset=%x:%08x, dv=%d, "
+                        "vnode=%d, unique=%d), offset=%x:%08x, dv=%I64d, "
                         "flags=0x%x, cmFlags=0x%x, refCount=%d\r\n",
                         cookie, (void *)bp, bp->fid.cell, bp->fid.volume, 
                         bp->fid.vnode, bp->fid.unique, bp->offset.HighPart, 
@@ -1728,7 +1803,7 @@ long buf_CleanDirtyBuffers(cm_scache_t *scp)
             bp->dirty_length = 0;
            bp->flags |= CM_BUF_ERROR;
            bp->error = VNOVNODE;
-           bp->dataVersion = -1; /* bad */
+           bp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
            bp->dirtyCounter++;
            if (bp->flags & CM_BUF_WAITING) {
                osi_Log2(buf_logp, "BUF CleanDirtyBuffers Waking [scp 0x%x] bp 0x%x", scp, bp);