windows-getaccessrights-20080805
[openafs.git] / src / WINNT / afsd / cm_dcache.c
index e5b34b1..f1f3288 100644 (file)
@@ -62,7 +62,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
     osi_hyper_t thyper;
     AFSVolSync volSync;
     AFSFid tfid;
-    struct rx_call *callp;
+    struct rx_call *rxcallp;
     struct rx_connection *rxconnp;
     osi_queueData_t *qdp;
     cm_buf_t *bufp;
@@ -81,9 +81,9 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
      * buffer, although more likely it will just return a new, empty, buffer.
      */
 
-    lock_ObtainMutex(&scp->mx);
+    lock_ObtainWrite(&scp->rw);
     if (scp->flags & CM_SCACHEFLAG_DELETED) {
-       lock_ReleaseMutex(&scp->mx);
+       lock_ReleaseWrite(&scp->rw);
        return CM_ERROR_NOSUCHFILE;
     }
 
@@ -92,13 +92,13 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
     code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
     if (code) {
         osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
-        lock_ReleaseMutex(&scp->mx);
+        lock_ReleaseWrite(&scp->rw);
         return code;
     }
 
     if (biod.length == 0) {
         osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
-        lock_ReleaseMutex(&scp->mx);
+        lock_ReleaseWrite(&scp->rw);
         cm_ReleaseBIOD(&biod, 1, 0);   /* should be a NOOP */
         return 0;
     }
@@ -143,7 +143,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
         require_64bit_ops = 1;
     }
         
-    lock_ReleaseMutex(&scp->mx);
+    lock_ReleaseWrite(&scp->rw);
 
     /* now we're ready to do the store operation */
     do {
@@ -153,7 +153,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
 
     retry:
         rxconnp = cm_GetRxConn(connp);
-        callp = rx_NewCall(rxconnp);
+        rxcallp = rx_NewCall(rxconnp);
         rx_PutConnection(rxconnp);
 
 #ifdef AFS_LARGEFILES
@@ -161,7 +161,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
             osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData64 scp 0x%p, offset 0x%x:%08x, length 0x%x",
                      scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
 
-            code = StartRXAFS_StoreData64(callp, &tfid, &inStatus,
+            code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
                                           biod.offset.QuadPart,
                                           nbytes,
                                           truncPos.QuadPart);
@@ -177,7 +177,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
                 osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, offset 0x%x:%08x, length 0x%x",
                          scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
 
-                code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
+                code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
                                             biod.offset.LowPart, nbytes, truncPos.LowPart);
                if (code)
                    osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
@@ -189,7 +189,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
         osi_Log4(afsd_logp, "CALL StartRXAFS_StoreData scp 0x%p, offset 0x%x:%08x, length 0x%x",
                  scp, biod.offset.HighPart, biod.offset.LowPart, nbytes);
 
-        code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
+        code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
                                     biod.offset.LowPart, nbytes, truncPos.LowPart);
        if (code)
            osi_Log1(afsd_logp, "CALL StartRXAFS_StoreData FAILURE, code 0x%x", code);
@@ -213,7 +213,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
                     wbytes = cm_data.buf_blockSize;
 
                 /* write out wbytes of data from bufferp */
-                temp = rx_Write(callp, bufferp, wbytes);
+                temp = rx_Write(rxcallp, bufferp, wbytes);
                 if (temp != wbytes) {
                     osi_Log3(afsd_logp, "rx_Write failed bp 0x%p, %d != %d",bufp,temp,wbytes);
                     code = -1;
@@ -227,13 +227,13 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
 
         if (code == 0) {
             if (SERVERHAS64BIT(connp)) {
-                code = EndRXAFS_StoreData64(callp, &outStatus, &volSync);
+                code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
                 if (code)
                     osi_Log2(afsd_logp, "EndRXAFS_StoreData64 FAILURE scp 0x%p code %lX", scp, code);
                else
                    osi_Log0(afsd_logp, "EndRXAFS_StoreData64 SUCCESS");
             } else {
-                code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
+                code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
                 if (code)
                     osi_Log2(afsd_logp, "EndRXAFS_StoreData FAILURE scp 0x%p code %lX",scp,code);
                else
@@ -241,7 +241,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
             }
         }
 
-        code = rx_EndCall(callp, code);
+        code = rx_EndCall(rxcallp, code);
 
 #ifdef AFS_LARGEFILES
         if (code == RXGEN_OPCODE && SERVERHAS64BIT(connp)) {
@@ -260,7 +260,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
         osi_Log1(afsd_logp, "CALL StoreData SUCCESS scp 0x%p", scp);
 
     /* now, clean up our state */
-    lock_ObtainMutex(&scp->mx);
+    lock_ObtainWrite(&scp->rw);
 
     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
 
@@ -302,7 +302,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
         else if (code == CM_ERROR_QUOTA)
             scp->flags |= CM_SCACHEFLAG_OVERQUOTA;
     }
-    lock_ReleaseMutex(&scp->mx);
+    lock_ReleaseWrite(&scp->rw);
     cm_ReleaseBIOD(&biod, 1, code);
 
     return code;
@@ -322,7 +322,7 @@ long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
     long code;
     osi_hyper_t truncPos;
     cm_conn_t *connp;
-    struct rx_call *callp;
+    struct rx_call *rxcallp;
     struct rx_connection *rxconnp;
     int require_64bit_ops = 0;
 
@@ -348,7 +348,7 @@ long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
         require_64bit_ops = 1;
     }
 
-    lock_ReleaseMutex(&scp->mx);
+    lock_ReleaseWrite(&scp->rw);
 
     cm_AFSFidFromFid(&tfid, &scp->fid);
 
@@ -360,33 +360,33 @@ long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
 
     retry:     
         rxconnp = cm_GetRxConn(connp);
-        callp = rx_NewCall(rxconnp);
+        rxcallp = rx_NewCall(rxconnp);
         rx_PutConnection(rxconnp);
 
 #ifdef AFS_LARGEFILES
         if (SERVERHAS64BIT(connp)) {
-            code = StartRXAFS_StoreData64(callp, &tfid, &inStatus,
+            code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
                                           0, 0, truncPos.QuadPart);
         } else {
             if (require_64bit_ops) {
                 code = CM_ERROR_TOOBIG;
             } else {
-                code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
+                code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
                                             0, 0, truncPos.LowPart);
             }
         }
 #else
-        code = StartRXAFS_StoreData(callp, &tfid, &inStatus,
+        code = StartRXAFS_StoreData(rxcallp, &tfid, &inStatus,
                                     0, 0, truncPos.LowPart);
 #endif
 
         if (code == 0) {
             if (SERVERHAS64BIT(connp))
-                code = EndRXAFS_StoreData64(callp, &outStatus, &volSync);
+                code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
             else
-                code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
+                code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
         }
-        code = rx_EndCall(callp, code);
+        code = rx_EndCall(rxcallp, code);
 
 #ifdef AFS_LARGEFILES
         if (code == RXGEN_OPCODE && SERVERHAS64BIT(connp)) {
@@ -399,7 +399,7 @@ long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
     code = cm_MapRPCError(code, reqp);
         
     /* now, clean up our state */
-    lock_ObtainMutex(&scp->mx);
+    lock_ObtainWrite(&scp->rw);
 
     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
 
@@ -440,11 +440,11 @@ long cm_BufStabilize(void *vscp, cm_user_t *userp, cm_req_t *reqp)
     cm_scache_t *scp = vscp;
     long code;
 
-    lock_ObtainMutex(&scp->mx);
+    lock_ObtainWrite(&scp->rw);
     code = cm_SyncOp(scp, NULL, userp, reqp, 0, 
                      CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
     if (code) {
-        lock_ReleaseMutex(&scp->mx);
+        lock_ReleaseWrite(&scp->rw);
         return code;
     }
         
@@ -458,7 +458,7 @@ long cm_BufUnstabilize(void *vscp, cm_user_t *userp)
         
     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_SETSIZE);
 
-    lock_ReleaseMutex(&scp->mx);
+    lock_ReleaseWrite(&scp->rw);
         
     /* always succeeds */
     return 0;
@@ -503,7 +503,7 @@ int cm_HaveBuffer(cm_scache_t *scp, cm_buf_t *bufp, int isBufLocked)
         return 0;
     if ((bufp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED)) == (CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED))
         return 1;
-    if (bufp->dataVersion == scp->dataVersion)
+    if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow)
         return 1;
     if (!isBufLocked) {
         code = lock_TryMutex(&bufp->mx);
@@ -547,14 +547,14 @@ long cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, osi_hyper_t *
     tlength = *length;
     tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
     stop = 0;
-    lock_ObtainMutex(&scp->mx);
+    lock_ObtainWrite(&scp->rw);
     while (LargeIntegerGreaterThanZero(tlength)) {
         /* get callback so we can do a meaningful dataVersion comparison */
         code = cm_SyncOp(scp, NULL, userp, reqp, 0,
                          CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
         if (code) {
             scp->flags &= ~CM_SCACHEFLAG_PREFETCHING;
-            lock_ReleaseMutex(&scp->mx);
+            lock_ReleaseWrite(&scp->rw);
             return code;
         }
                 
@@ -567,7 +567,7 @@ long cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, osi_hyper_t *
         /* We cheat slightly by not locking the bp mutex. */
         if (bp) {
             if ((bp->cmFlags & (CM_BUF_CMFETCHING | CM_BUF_CMSTORING)) == 0
-                 && bp->dataVersion != scp->dataVersion)
+                 && (bp->dataVersion < scp->bufDataVersionLow || bp->dataVersion > scp->dataVersion))
                 stop = 1;
             buf_Release(bp);
            bp = NULL;
@@ -598,7 +598,7 @@ long cm_CheckFetchRange(cm_scache_t *scp, osi_hyper_t *startBasep, osi_hyper_t *
         *realBasep = tbase;
         code = 0;
     }
-    lock_ReleaseMutex(&scp->mx);
+    lock_ReleaseWrite(&scp->rw);
     return code;
 }
 
@@ -630,10 +630,25 @@ cm_BkgStore(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_u
        osi_Log4(afsd_logp, "Finished BKG store scp 0x%p, offset 0x%x:%08x, code 0x%x", scp, p2, p1, code);
     }
 
-    lock_ObtainMutex(&scp->mx);
-    cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
-    lock_ReleaseMutex(&scp->mx);
-
+    /* 
+     * Keep the following list synchronized with the
+     * error code list in cm_BkgDaemon 
+     */
+    switch ( code ) {
+    case CM_ERROR_TIMEDOUT: /* or server restarting */
+    case CM_ERROR_RETRY:
+    case CM_ERROR_WOULDBLOCK:
+    case CM_ERROR_ALLBUSY:
+    case CM_ERROR_ALLDOWN:
+    case CM_ERROR_ALLOFFLINE:
+    case CM_ERROR_PARTIALWRITE:
+        break;  /* cm_BkgDaemon will re-insert the request in the queue */
+    case 0:
+    default:
+        lock_ObtainWrite(&scp->rw);
+        cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_ASYNCSTORE);
+        lock_ReleaseWrite(&scp->rw);
+    }
     return code;
 }
 
@@ -686,45 +701,44 @@ cm_BkgPrefetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, af
         
     osi_Log3(afsd_logp, "Starting BKG prefetch scp 0x%p, base 0x%x:%x", scp, p2, p1);
 
-    for (code = 0, offset = base;
+    for ( code = 0, offset = base;
           code == 0 && LargeIntegerLessThan(offset, end); 
-          offset = LargeIntegerAdd(offset, tblocksize))
+          offset = LargeIntegerAdd(offset, tblocksize) )
     {
         if (mxheld) {
-            lock_ReleaseMutex(&scp->mx);
+            lock_ReleaseWrite(&scp->rw);
             mxheld = 0;
         }
-        code = buf_Get(scp, &base, &bp);
-        if (!mxheld) {
-            lock_ObtainMutex(&scp->mx);
-            mxheld = 1;
-        }
 
-        if (code || (bp->cmFlags & CM_BUF_CMFETCHING)) {
-            code = 0;
-            if (bp) {
-                buf_Release(bp);
-                bp = NULL;
-            }
+        code = buf_Get(scp, &offset, &bp);
+        if (code)
             break;
+
+        if (bp->cmFlags & CM_BUF_CMFETCHING) {
+            /* skip this buffer as another thread is already fetching it */
+            buf_Release(bp);
+            bp = NULL;
+            continue;
+        }
+
+        if (!mxheld) {
+            lock_ObtainWrite(&scp->rw);
+            mxheld = 1;
         }
 
         code = cm_GetBuffer(scp, bp, NULL, userp, &req);
         if (code == 0)
             fetched = LargeIntegerAdd(fetched, tblocksize); 
+        buf_Release(bp);
     }
     
     if (!mxheld) {
-        lock_ObtainMutex(&scp->mx);
+        lock_ObtainWrite(&scp->rw);
         mxheld = 1;
     }
     cm_ClearPrefetchFlag(LargeIntegerGreaterThanZero(fetched) ? 0 : code, 
                          scp, &base, &fetched);
-    lock_ReleaseMutex(&scp->mx);
-    if (bp) {
-        buf_Release(bp);
-        bp = NULL;
-    }
+    lock_ReleaseWrite(&scp->rw);
 
     osi_Log4(afsd_logp, "Ending BKG prefetch scp 0x%p, code %d bytes 0x%x:%x", 
               scp, code, fetched.HighPart, fetched.LowPart);
@@ -734,7 +748,7 @@ cm_BkgPrefetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, af
 /* a read was issued to offsetp, and we have to determine whether we should
  * do a prefetch of the next chunk.
  */
-void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp,
+void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp, afs_uint32 count,
                          cm_user_t *userp, cm_req_t *reqp)
 {
     long code;
@@ -747,12 +761,12 @@ void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp,
     readBase.LowPart += (cm_chunkSize-1);
     readBase.LowPart &= (-cm_chunkSize);
 
-    readLength = ConvertLongToLargeInteger(cm_chunkSize);
+    readLength = ConvertLongToLargeInteger(count);
 
-    lock_ObtainMutex(&scp->mx);
+    lock_ObtainWrite(&scp->rw);
     if ((scp->flags & CM_SCACHEFLAG_PREFETCHING)
          || LargeIntegerLessThanOrEqualTo(readBase, scp->prefetch.base)) {
-        lock_ReleaseMutex(&scp->mx);
+        lock_ReleaseWrite(&scp->rw);
         return;
     }
     scp->flags |= CM_SCACHEFLAG_PREFETCHING;
@@ -763,7 +777,7 @@ void cm_ConsiderPrefetch(cm_scache_t *scp, osi_hyper_t *offsetp,
     if (LargeIntegerGreaterThan(scp->prefetch.end, readBase))
         readBase = scp->prefetch.end;
 
-    lock_ReleaseMutex(&scp->mx);
+    lock_ReleaseWrite(&scp->rw);
 
     code = cm_CheckFetchRange(scp, &readBase, &readLength, userp, reqp,
                               &realBase);
@@ -813,9 +827,9 @@ long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
     biop->reserved = 0;
 
     /* reserve a chunk's worth of buffers */
-    lock_ReleaseMutex(&scp->mx);
+    lock_ReleaseWrite(&scp->rw);
     buf_ReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
-    lock_ObtainMutex(&scp->mx);
+    lock_ObtainWrite(&scp->rw);
 
     bufp = NULL;
     for (temp = 0; temp < inSize; temp += cm_data.buf_blockSize) {
@@ -825,9 +839,9 @@ long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
         bufp = buf_Find(scp, &tbase);
         if (bufp) {
             /* get buffer mutex and scp mutex safely */
-            lock_ReleaseMutex(&scp->mx);
+            lock_ReleaseWrite(&scp->rw);
             lock_ObtainMutex(&bufp->mx);
-            lock_ObtainMutex(&scp->mx);
+            lock_ObtainWrite(&scp->rw);
 
             flags = CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_STOREDATA | CM_SCACHESYNC_BUFLOCKED;
             code = cm_SyncOp(scp, bufp, userp, reqp, 0, flags); 
@@ -901,9 +915,9 @@ long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
             break;
 
         /* try to lock it, and quit if we can't (simplifies locking) */
-        lock_ReleaseMutex(&scp->mx);
+        lock_ReleaseWrite(&scp->rw);
         code = lock_TryMutex(&bufp->mx);
-        lock_ObtainMutex(&scp->mx);
+        lock_ObtainWrite(&scp->rw);
         if (code == 0) {
             buf_Release(bufp);
            bufp = NULL;
@@ -961,9 +975,9 @@ long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
             break;
 
         /* try to lock it, and quit if we can't (simplifies locking) */
-        lock_ReleaseMutex(&scp->mx);
+        lock_ReleaseWrite(&scp->rw);
         code = lock_TryMutex(&bufp->mx);
-        lock_ObtainMutex(&scp->mx);
+        lock_ObtainWrite(&scp->rw);
         if (code == 0) {
             buf_Release(bufp);
            bufp = NULL;
@@ -1052,7 +1066,7 @@ long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
     /* copy out size, since it may change */
     fileSize = scp->serverLength;
         
-    lock_ReleaseMutex(&scp->mx);
+    lock_ReleaseWrite(&scp->rw);
 
     pageBase = *offsetp;
     collected = pageBase.LowPart & (cm_chunkSize - 1);
@@ -1092,7 +1106,7 @@ long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
         code = buf_Get(scp, &pageBase, &tbp);
         if (code) {
             //lock_ReleaseMutex(&cm_bufGetMutex);
-            lock_ObtainMutex(&scp->mx);
+            lock_ObtainWrite(&scp->rw);
             cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
             return code;
         }
@@ -1157,12 +1171,12 @@ long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
 
         /* now lock the buffer lock */
         lock_ObtainMutex(&tbp->mx);
-        lock_ObtainMutex(&scp->mx);
+        lock_ObtainWrite(&scp->rw);
 
         /* don't bother fetching over data that is already current */
-        if (tbp->dataVersion == scp->dataVersion) {
+        if (tbp->dataVersion <= scp->dataVersion && tbp->dataVersion >= scp->bufDataVersionLow) {
             /* we don't need this buffer, since it is current */
-            lock_ReleaseMutex(&scp->mx);
+            lock_ReleaseWrite(&scp->rw);
             lock_ReleaseMutex(&tbp->mx);
             break;
         }
@@ -1176,7 +1190,7 @@ long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
          */
         code = cm_SyncOp(scp, tbp, userp, reqp, 0, flags);
         if (code) {
-            lock_ReleaseMutex(&scp->mx);
+            lock_ReleaseWrite(&scp->rw);
             lock_ReleaseMutex(&tbp->mx);
             break;
         }
@@ -1184,13 +1198,13 @@ long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
         /* don't fetch over dirty buffers */
         if (tbp->flags & CM_BUF_DIRTY) {
             cm_SyncOpDone(scp, tbp, flags);
-            lock_ReleaseMutex(&scp->mx);
+            lock_ReleaseWrite(&scp->rw);
             lock_ReleaseMutex(&tbp->mx);
             break;
         }
 
         /* Release locks */
-        lock_ReleaseMutex(&scp->mx);
+        lock_ReleaseWrite(&scp->rw);
         lock_ReleaseMutex(&tbp->mx);
 
         /* add the buffer to the list */
@@ -1224,7 +1238,7 @@ long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
     }
 
     /* Caller expects this */
-    lock_ObtainMutex(&scp->mx);
+    lock_ObtainWrite(&scp->rw);
  
     /* if we got a failure setting up the first buffer, then we don't have
      * any side effects yet, and we also have failed an operation that the
@@ -1278,7 +1292,7 @@ void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, int failed)
 
            /* now, mark I/O as done, unlock the buffer and release it */
            lock_ObtainMutex(&bufp->mx);
-           lock_ObtainMutex(&scp->mx);
+           lock_ObtainWrite(&scp->rw);
            cm_SyncOpDone(scp, bufp, flags);
                 
            /* turn off writing and wakeup users */
@@ -1295,15 +1309,15 @@ void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, int failed)
                 }
            }
 
-           lock_ReleaseMutex(&scp->mx);
+           lock_ReleaseWrite(&scp->rw);
            lock_ReleaseMutex(&bufp->mx);
            buf_Release(bufp);
            bufp = NULL;
        }
     } else {
-       lock_ObtainMutex(&scp->mx);
+       lock_ObtainWrite(&scp->rw);
        cm_SyncOpDone(scp, NULL, flags);
-       lock_ReleaseMutex(&scp->mx);
+       lock_ReleaseWrite(&scp->rw);
     }
 
     /* clean things out */
@@ -1330,7 +1344,7 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
     cm_buf_t *tbufp;                   /* buf we're filling */
     osi_queueData_t *qdp;              /* q element we're scanning */
     AFSFid tfid;
-    struct rx_call *callp;
+    struct rx_call *rxcallp;
     struct rx_connection *rxconnp;
     cm_bulkIO_t biod;          /* bulk IO descriptor */
     cm_conn_t *connp;
@@ -1354,6 +1368,11 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
         osi_Log1(afsd_logp,"GetBuffer returns cm_data.rootSCachep=%x",cm_data.rootSCachep);
 #endif
 
+    if (cm_HaveCallback(scp) && bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow) {
+        /* We already have this buffer don't do extra work */
+        return 0;
+    }
+
     cm_AFSFidFromFid(&tfid, &scp->fid);
 
     code = cm_SetupFetchBIOD(scp, &bufp->offset, &biod, userp, reqp);
@@ -1370,24 +1389,33 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
      * We can lose a race condition and end up with biod.length zero, in
      * which case we just retry.
      */
-    if (bufp->dataVersion == scp->dataVersion || biod.length == 0) {
-        if ((bufp->dataVersion == -1 || bufp->dataVersion < scp->dataVersion) && 
+    if (bufp->dataVersion <= scp->dataVersion && bufp->dataVersion >= scp->bufDataVersionLow || biod.length == 0) {
+        if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow) && 
              LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->serverLength)) 
         {
-            osi_Log3(afsd_logp, "Bad DVs %I64d, %I64d or length 0x%x",
-                      bufp->dataVersion, scp->dataVersion, biod.length);
+            osi_Log4(afsd_logp, "Bad DVs 0x%x != (0x%x -> 0x%x) or length 0x%x",
+                     bufp->dataVersion, scp->bufDataVersionLow, scp->dataVersion, biod.length);
 
-            if (bufp->dataVersion == -1)
+            if (bufp->dataVersion == CM_BUF_VERSION_BAD)
                 memset(bufp->datap, 0, cm_data.buf_blockSize);
             bufp->dataVersion = scp->dataVersion;
         }
-        lock_ReleaseMutex(&scp->mx);
+        lock_ReleaseWrite(&scp->rw);
+        cm_ReleaseBIOD(&biod, 0, 0);
+        lock_ObtainWrite(&scp->rw);
+        return 0;
+    } else if ((bufp->dataVersion == CM_BUF_VERSION_BAD || bufp->dataVersion < scp->bufDataVersionLow)
+                && (scp->mask & CM_SCACHEMASK_TRUNCPOS) &&
+                LargeIntegerGreaterThanOrEqualTo(bufp->offset, scp->truncPos)) {
+        memset(bufp->datap, 0, cm_data.buf_blockSize);
+        bufp->dataVersion = scp->dataVersion;
+        lock_ReleaseWrite(&scp->rw);
         cm_ReleaseBIOD(&biod, 0, 0);
-        lock_ObtainMutex(&scp->mx);
+        lock_ObtainWrite(&scp->rw);
         return 0;
     }
 
-    lock_ReleaseMutex(&scp->mx);
+    lock_ReleaseWrite(&scp->rw);
 
     if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
                                                 ConvertLongToLargeInteger(biod.length)),
@@ -1395,10 +1423,9 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
         require_64bit_ops = 1;
     }
 
-#ifdef DISKCACHE95
-    DPRINTF("cm_GetBuffer: fetching data scpDV=%I64d bufDV=%I64d scp=%x bp=%x dcp=%x\n",
-            scp->dataVersion, bufp->dataVersion, scp, bufp, bufp->dcp);
-#endif /* DISKCACHE95 */
+    osi_Log2(afsd_logp, "cm_GetBuffer: fetching data scp %p bufp %p", scp, bufp);
+    osi_Log3(afsd_logp, "cm_GetBuffer: fetching data scpDV 0x%x scpDVLow 0x%x bufDV 0x%x",
+             scp->dataVersion, scp->bufDataVersionLow, bufp->dataVersion);
 
 #ifdef AFS_FREELANCE_CLIENT
 
@@ -1472,7 +1499,7 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
             continue;
 
         rxconnp = cm_GetRxConn(connp);
-        callp = rx_NewCall(rxconnp);
+        rxcallp = rx_NewCall(rxconnp);
         rx_PutConnection(rxconnp);
 
 #ifdef AFS_LARGEFILES
@@ -1482,17 +1509,17 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
             osi_Log4(afsd_logp, "CALL FetchData64 scp 0x%p, off 0x%x:%08x, size 0x%x",
                      scp, biod.offset.HighPart, biod.offset.LowPart, biod.length);
 
-            code = StartRXAFS_FetchData64(callp, &tfid, biod.offset.QuadPart, biod.length);
+            code = StartRXAFS_FetchData64(rxcallp, &tfid, biod.offset.QuadPart, biod.length);
 
             if (code == 0) {
-                temp = rx_Read(callp, (char *) &nbytes_hi, sizeof(afs_int32));
+                temp = rx_Read(rxcallp, (char *) &nbytes_hi, sizeof(afs_int32));
                 if (temp == sizeof(afs_int32)) {
                     nbytes_hi = ntohl(nbytes_hi);
                 } else {
                     nbytes_hi = 0;
-                   code = callp->error;
-                    rx_EndCall(callp, code);
-                    callp = NULL;
+                   code = rxcallp->error;
+                    rx_EndCall(rxcallp, code);
+                    rxcallp = NULL;
                 }
             }
         }
@@ -1502,16 +1529,16 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
                 osi_Log0(afsd_logp, "Skipping FetchData.  Operation requires FetchData64");
                 code = CM_ERROR_TOOBIG;
             } else {
-                if (!callp) {
+                if (!rxcallp) {
                     rxconnp = cm_GetRxConn(connp);
-                    callp = rx_NewCall(rxconnp);
+                    rxcallp = rx_NewCall(rxconnp);
                     rx_PutConnection(rxconnp);
                 }
 
                 osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
                          scp, biod.offset.LowPart, biod.length);
 
-                code = StartRXAFS_FetchData(callp, &tfid, biod.offset.LowPart,
+                code = StartRXAFS_FetchData(rxcallp, &tfid, biod.offset.LowPart,
                                             biod.length);
 
                 SET_SERVERHASNO64BIT(connp);
@@ -1519,14 +1546,14 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
         }
 
         if (code == 0) {
-            temp  = rx_Read(callp, (char *)&nbytes, sizeof(afs_int32));
+            temp  = rx_Read(rxcallp, (char *)&nbytes, sizeof(afs_int32));
             if (temp == sizeof(afs_int32)) {
                 nbytes = ntohl(nbytes);
                 FillInt64(length_found, nbytes_hi, nbytes);
                 if (length_found > biod.length) 
-                    code = (callp->error < 0) ? callp->error : -1;
+                    code = (rxcallp->error < 0) ? rxcallp->error : -1;
             } else {
-                code = (callp->error < 0) ? callp->error : -1;
+                code = (rxcallp->error < 0) ? rxcallp->error : -1;
             }
         }
         /* for the moment, nbytes_hi will always be 0 if code == 0
@@ -1535,19 +1562,19 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
         osi_Log3(afsd_logp, "CALL FetchData scp 0x%p, off 0x%x, size 0x%x",
                  scp, biod.offset.LowPart, biod.length);
 
-        code = StartRXAFS_FetchData(callp, &tfid, biod.offset.LowPart,
+        code = StartRXAFS_FetchData(rxcallp, &tfid, biod.offset.LowPart,
                                     biod.length);
 
         /* now copy the data out of the pipe and put it in the buffer */
         if (code == 0) {
-            temp  = rx_Read(callp, (char *)&nbytes, sizeof(afs_int32));
+            temp  = rx_Read(rxcallp, (char *)&nbytes, sizeof(afs_int32));
             if (temp == sizeof(afs_int32)) {
                 nbytes = ntohl(nbytes);
                 if (nbytes > biod.length) 
-                    code = (callp->error < 0) ? callp->error : -1;
+                    code = (rxcallp->error < 0) ? rxcallp->error : -1;
             }
             else 
-                code = (callp->error < 0) ? callp->error : -1;
+                code = (rxcallp->error < 0) ? rxcallp->error : -1;
         }
 #endif
 
@@ -1575,9 +1602,9 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
 
                 /* read rbytes of data */
                 rbytes = (nbytes > cm_data.buf_blockSize? cm_data.buf_blockSize : nbytes);
-                temp = rx_Read(callp, bufferp, rbytes);
+                temp = rx_Read(rxcallp, bufferp, rbytes);
                 if (temp < rbytes) {
-                    code = (callp->error < 0) ? callp->error : -1;
+                    code = (rxcallp->error < 0) ? rxcallp->error : -1;
                     break;
                 }
 
@@ -1587,7 +1614,7 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
                  * this buffer will start a prefetch.
                  */
                 tbufp->cmFlags |= CM_BUF_CMFULLYFETCHED;
-                lock_ObtainMutex(&scp->mx);
+                lock_ObtainWrite(&scp->rw);
                 if (scp->flags & CM_SCACHEFLAG_WAITING) {
                     osi_Log1(afsd_logp, "CM GetBuffer Waking scp 0x%p", scp);
                     osi_Wakeup((LONG_PTR) &scp->flags);
@@ -1597,7 +1624,7 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
                     *cpffp = 1;
                     cm_ClearPrefetchFlag(0, scp, &biod.offset, &tlength);
                 }
-                lock_ReleaseMutex(&scp->mx);
+                lock_ReleaseWrite(&scp->rw);
 
                 /* and adjust counters */
                 nbytes -= temp;
@@ -1642,9 +1669,9 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
 
         if (code == 0) {
             if (SERVERHAS64BIT(connp))
-                code = EndRXAFS_FetchData64(callp, &afsStatus, &callback, &volSync);
+                code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
             else
-                code = EndRXAFS_FetchData(callp, &afsStatus, &callback, &volSync);
+                code = EndRXAFS_FetchData(rxcallp, &afsStatus, &callback, &volSync);
         } else {
             if (SERVERHAS64BIT(connp))
                 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
@@ -1652,8 +1679,8 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
                 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
         }
 
-        if (callp)
-            code = rx_EndCall(callp, code);
+        if (rxcallp)
+            code = rx_EndCall(rxcallp, code);
 
         if (code == RXKADUNKNOWNKEY)
             osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
@@ -1665,10 +1692,8 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
   fetchingcompleted:
     code = cm_MapRPCError(code, reqp);
 
-    lock_ObtainMutex(&scp->mx);
+    lock_ObtainWrite(&scp->rw);
     
-    cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
-
     /* we know that no one else has changed the buffer, since we still have
      * the fetching flag on the buffers, and we have the scp locked again.
      * Copy in the version # into the buffer if we got code 0 back from the
@@ -1692,9 +1717,9 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
     }
 
     /* release scatter/gather I/O structure (buffers, locks) */
-    lock_ReleaseMutex(&scp->mx);
+    lock_ReleaseWrite(&scp->rw);
     cm_ReleaseBIOD(&biod, 0, code);
-    lock_ObtainMutex(&scp->mx);
+    lock_ObtainWrite(&scp->rw);
 
     if (code == 0) 
         cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, 0);