Windows: refactor afs status cloning and clone when fs fetchdata bug detected
[openafs.git] / src / WINNT / afsd / cm_dcache.c
index 3b7ffb3..d15ecde 100644 (file)
@@ -26,7 +26,6 @@
 extern void afsi_log(char *pattern, ...);
 #endif
 
-osi_mutex_t cm_bufGetMutex;
 #ifdef AFS_FREELANCE_CLIENT
 extern osi_mutex_t cm_Freelance_Lock;
 #endif
@@ -53,16 +52,19 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
      * released by our caller.  Thus, we don't have to worry about holding
      * bufp->scp.
      */
-    long code;
+    long code, code1;
     cm_scache_t *scp = vscp;
     afs_int32 nbytes;
+#ifdef AFS_LARGEFILES
+    afs_int32 save_nbytes;
+#endif
     long temp;
     AFSFetchStatus outStatus;
     AFSStoreStatus inStatus;
     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;
@@ -72,6 +74,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
     osi_hyper_t truncPos;
     cm_bulkIO_t biod;          /* bulk IO descriptor */
     int require_64bit_ops = 0;
+    int call_was_64bit = 0;
 
     osi_assertx(userp != NULL, "null cm_user_t");
     osi_assertx(scp != NULL, "null cm_scache_t");
@@ -89,23 +92,25 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
 
     cm_AFSFidFromFid(&tfid, &scp->fid);
 
+    /* Serialize StoreData RPC's; for rationale see cm_scache.c */
+    (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
+
     code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
     if (code) {
         osi_Log1(afsd_logp, "cm_SetupStoreBIOD code %x", code);
+        cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
         lock_ReleaseWrite(&scp->rw);
         return code;
     }
 
     if (biod.length == 0) {
         osi_Log0(afsd_logp, "cm_SetupStoreBIOD length 0");
+        cm_ReleaseBIOD(&biod, 1, 0, 1);        /* should be a NOOP */
+        cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
         lock_ReleaseWrite(&scp->rw);
-        cm_ReleaseBIOD(&biod, 1, 0);   /* should be a NOOP */
         return 0;
     }
 
-    /* Serialize StoreData RPC's; for rationale see cm_scache.c */
-    (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
-
     /* prepare the output status for the store */
     scp->mask |= CM_SCACHEMASK_CLIENTMODTIME;
     cm_StatusFromAttr(&inStatus, scp, NULL);
@@ -146,6 +151,9 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
     lock_ReleaseWrite(&scp->rw);
 
     /* now we're ready to do the store operation */
+#ifdef AFS_LARGEFILES
+    save_nbytes = nbytes;
+#endif
     do {
         code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
         if (code) 
@@ -153,15 +161,17 @@ 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
         if (SERVERHAS64BIT(connp)) {
+            call_was_64bit = 1;
+
             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);
@@ -170,6 +180,8 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
            else
                osi_Log0(afsd_logp, "CALL StartRXAFS_StoreData64 SUCCESS");
         } else {
+            call_was_64bit = 0;
+
             if (require_64bit_ops) {
                 osi_Log0(afsd_logp, "Skipping StartRXAFS_StoreData.  The operation requires large file support in the server.");
                 code = CM_ERROR_TOOBIG;
@@ -177,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);
@@ -189,7 +201,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);
@@ -201,22 +213,26 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
             /* write the data from the the list of buffers */
             qdp = NULL;
             while(nbytes > 0) {
-                if (qdp == NULL)
+                afs_uint32 buf_offset;
+                if (qdp == NULL) {
                     qdp = biod.bufListEndp;
-                else
+                    buf_offset = offsetp->LowPart % cm_data.buf_blockSize;
+                } else {
                     qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
+                    buf_offset = 0;
+                }
                 osi_assertx(qdp != NULL, "null osi_queueData_t");
                 bufp = osi_GetQData(qdp);
-                bufferp = bufp->datap;
+                bufferp = bufp->datap + buf_offset;
                 wbytes = nbytes;
-                if (wbytes > cm_data.buf_blockSize) 
-                    wbytes = cm_data.buf_blockSize;
+                if (wbytes > cm_data.buf_blockSize - buf_offset)
+                    wbytes = cm_data.buf_blockSize - buf_offset;
 
                 /* 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;
+                    code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
                     break;
                 } else {
                     osi_Log2(afsd_logp, "rx_Write succeeded bp 0x%p, %d",bufp,temp);
@@ -226,14 +242,14 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
         }      /* if RPC started successfully */
 
         if (code == 0) {
-            if (SERVERHAS64BIT(connp)) {
-                code = EndRXAFS_StoreData64(callp, &outStatus, &volSync);
+            if (call_was_64bit) {
+                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,15 +257,19 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
             }
         }
 
-        code = rx_EndCall(callp, code);
+        code1 = rx_EndCall(rxcallp, code);
 
 #ifdef AFS_LARGEFILES
-        if (code == RXGEN_OPCODE && SERVERHAS64BIT(connp)) {
+        if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
             SET_SERVERHASNO64BIT(connp);
+            qdp = NULL;
+            nbytes = save_nbytes;
             goto retry;
         }
 #endif
-                
+        /* Prefer StoreData error over rx_EndCall error */
+        if (code1 != 0)
+            code = code1;
     } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
 
     code = cm_MapRPCError(code, reqp);
@@ -262,6 +282,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
     /* now, clean up our state */
     lock_ObtainWrite(&scp->rw);
 
+    cm_ReleaseBIOD(&biod, 1, code, 1);
     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
 
     if (code == 0) {
@@ -285,7 +306,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
          * so that we see updates made by other machines.
          */
 
-        if (SERVERHAS64BIT(connp)) {
+        if (call_was_64bit) {
             t.LowPart = outStatus.Length;
             t.HighPart = outStatus.Length_hi;
         } else {
@@ -295,7 +316,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
         if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
             scp->mask &= ~CM_SCACHEMASK_LENGTH;
 
-        cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, CM_MERGEFLAG_STOREDATA);
+        cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
     } else {
         if (code == CM_ERROR_SPACE)
             scp->flags |= CM_SCACHEFLAG_OUTOFSPACE;
@@ -303,7 +324,6 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
             scp->flags |= CM_SCACHEFLAG_OVERQUOTA;
     }
     lock_ReleaseWrite(&scp->rw);
-    cm_ReleaseBIOD(&biod, 1, code);
 
     return code;
 }
@@ -319,12 +339,13 @@ long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
     AFSStoreStatus inStatus;
     AFSVolSync volSync;
     AFSFid tfid;
-    long code;
+    long code, code1;
     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;
+    int call_was_64bit = 0;
 
     /* Serialize StoreData RPC's; for rationale see cm_scache.c */
     (void) cm_SyncOp(scp, NULL, userp, reqp, 0,
@@ -360,41 +381,47 @@ 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,
+            call_was_64bit = 1;
+
+            code = StartRXAFS_StoreData64(rxcallp, &tfid, &inStatus,
                                           0, 0, truncPos.QuadPart);
         } else {
+            call_was_64bit = 0;
+
             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);
+            if (call_was_64bit)
+                code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
             else
-                code = EndRXAFS_StoreData(callp, &outStatus, &volSync);
+                code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
         }
-        code = rx_EndCall(callp, code);
+        code1 = rx_EndCall(rxcallp, code);
 
 #ifdef AFS_LARGEFILES
-        if (code == RXGEN_OPCODE && SERVERHAS64BIT(connp)) {
+        if ((code == RXGEN_OPCODE || code1 == RXGEN_OPCODE) && SERVERHAS64BIT(connp)) {
             SET_SERVERHASNO64BIT(connp);
             goto retry;
         }
 #endif
-
+        /* prefer StoreData error over rx_EndCall error */
+        if (code == 0 && code1 != 0)
+            code = code1;
     } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
     code = cm_MapRPCError(code, reqp);
         
@@ -409,7 +436,7 @@ long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
          * For explanation of handling of CM_SCACHEMASK_LENGTH,
          * see cm_BufWrite().
          */
-        if (SERVERHAS64BIT(connp)) {
+        if (call_was_64bit) {
             t.HighPart = outStatus.Length_hi;
             t.LowPart = outStatus.Length;
         } else {
@@ -418,7 +445,7 @@ long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
 
         if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
             scp->mask &= ~CM_SCACHEMASK_LENGTH;
-        cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, CM_MERGEFLAG_STOREDATA);
+        cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
     }
 
     return code;
@@ -483,7 +510,6 @@ long cm_ShutdownDCache(void)
 
 int cm_InitDCache(int newFile, long chunkSize, afs_uint64 nbuffers)
 {
-    lock_InitializeMutex(&cm_bufGetMutex, "buf_Get mutex");
     return buf_Init(newFile, &cm_bufOps, nbuffers);
 }
 
@@ -710,7 +736,7 @@ cm_BkgPrefetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, af
             mxheld = 0;
         }
 
-        code = buf_Get(scp, &offset, &bp);
+        code = buf_Get(scp, &offset, &req, &bp);
         if (code)
             break;
 
@@ -1092,7 +1118,6 @@ long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
      * sequence at a time.
      */
 
-    // lock_ObtainMutex(&cm_bufGetMutex);
     /* first hold all buffers, since we can't hold any locks in buf_Get */
     while (1) {
         /* stop at chunk boundary */
@@ -1103,9 +1128,8 @@ long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
         if (LargeIntegerGreaterThanOrEqualTo(pageBase, fileSize)) 
             break;
 
-        code = buf_Get(scp, &pageBase, &tbp);
+        code = buf_Get(scp, &pageBase, reqp, &tbp);
         if (code) {
-            //lock_ReleaseMutex(&cm_bufGetMutex);
             lock_ObtainWrite(&scp->rw);
             cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
             return code;
@@ -1121,8 +1145,6 @@ long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
     /* reserve a chunk's worth of buffers if possible */
     reserving = buf_TryReserveBuffers(cm_chunkSize / cm_data.buf_blockSize);
 
-    // lock_ReleaseMutex(&cm_bufGetMutex);
-
     pageBase = *offsetp;
     collected = pageBase.LowPart & (cm_chunkSize - 1);
 
@@ -1260,7 +1282,7 @@ long cm_SetupFetchBIOD(cm_scache_t *scp, osi_hyper_t *offsetp,
 /* release a bulk I/O structure that was setup by cm_SetupFetchBIOD or by
  * cm_SetupStoreBIOD
  */
-void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, int failed)
+void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, long code, int scp_locked)
 {
     cm_scache_t *scp;          /* do not release; not held in biop */
     cm_buf_t *bufp;
@@ -1291,6 +1313,8 @@ void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, int failed)
            osi_QDFree(qdp);
 
            /* now, mark I/O as done, unlock the buffer and release it */
+            if (scp_locked)
+                lock_ReleaseWrite(&scp->rw);
            lock_ObtainMutex(&bufp->mx);
            lock_ObtainWrite(&scp->rw);
            cm_SyncOpDone(scp, bufp, flags);
@@ -1301,23 +1325,57 @@ void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, int failed)
                    osi_Log2(afsd_logp, "cm_ReleaseBIOD Waking [scp 0x%p] bp 0x%p", scp, bufp);
                    osi_Wakeup((LONG_PTR) bufp);
                }
-               if (failed)
+               if (code) {
                    bufp->flags &= ~CM_BUF_WRITING;
-               else {
+                    switch (code) {
+                    case CM_ERROR_NOSUCHFILE:
+                    case CM_ERROR_BADFD:
+                    case CM_ERROR_NOACCESS:
+                    case CM_ERROR_QUOTA:
+                    case CM_ERROR_SPACE:
+                    case CM_ERROR_TOOBIG:
+                    case CM_ERROR_READONLY:
+                    case CM_ERROR_NOSUCHPATH:
+                        /*
+                         * Apply the fatal error to this buffer.
+                         */
+                        bufp->flags &= ~CM_BUF_DIRTY;
+                        bufp->flags |= CM_BUF_ERROR;
+                        bufp->dirty_offset = 0;
+                        bufp->dirty_length = 0;
+                        bufp->error = code;
+                        bufp->dataVersion = CM_BUF_VERSION_BAD;
+                        bufp->dirtyCounter++;
+                        break;
+                    case CM_ERROR_TIMEDOUT:
+                    case CM_ERROR_ALLDOWN:
+                    case CM_ERROR_ALLBUSY:
+                    case CM_ERROR_ALLOFFLINE:
+                    case CM_ERROR_CLOCKSKEW:
+                    default:
+                        /* do not mark the buffer in error state but do
+                        * not attempt to complete the rest either.
+                        */
+                        break;
+                    }
+               } else {
                    bufp->flags &= ~(CM_BUF_WRITING | CM_BUF_DIRTY);
                     bufp->dirty_offset = bufp->dirty_length = 0;
                 }
            }
 
-           lock_ReleaseWrite(&scp->rw);
+            if (!scp_locked)
+                lock_ReleaseWrite(&scp->rw);
            lock_ReleaseMutex(&bufp->mx);
            buf_Release(bufp);
            bufp = NULL;
        }
     } else {
-       lock_ObtainWrite(&scp->rw);
+       if (!scp_locked)
+            lock_ObtainWrite(&scp->rw);
        cm_SyncOpDone(scp, NULL, flags);
-       lock_ReleaseWrite(&scp->rw);
+        if (!scp_locked)
+            lock_ReleaseWrite(&scp->rw);
     }
 
     /* clean things out */
@@ -1325,16 +1383,70 @@ void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, int failed)
     biop->bufListEndp = NULL;
 }   
 
+static int
+cm_CloneStatus(cm_scache_t *scp, cm_user_t *userp, int scp_locked,
+               AFSFetchStatus *afsStatusp, AFSVolSync *volSyncp)
+{
+    // setup the status based upon the scp data
+    afsStatusp->InterfaceVersion = 0x1;
+    switch (scp->fileType) {
+    case CM_SCACHETYPE_FILE:
+        afsStatusp->FileType = File;
+        break;
+    case CM_SCACHETYPE_DIRECTORY:
+        afsStatusp->FileType = Directory;
+        break;
+    case CM_SCACHETYPE_MOUNTPOINT:
+        afsStatusp->FileType = SymbolicLink;
+        break;
+    case CM_SCACHETYPE_SYMLINK:
+    case CM_SCACHETYPE_DFSLINK:
+        afsStatusp->FileType = SymbolicLink;
+        break;
+    default:
+        afsStatusp->FileType = -1;    /* an invalid value */
+    }
+    afsStatusp->LinkCount = scp->linkCount;
+    afsStatusp->Length = scp->length.LowPart;
+    afsStatusp->DataVersion = (afs_uint32)(scp->dataVersion & MAX_AFS_UINT32);
+    afsStatusp->Author = 0x1;
+    afsStatusp->Owner = scp->owner;
+    if (!scp_locked) {
+        lock_ObtainWrite(&scp->rw);
+        scp_locked = 1;
+    }
+    if (cm_FindACLCache(scp, userp, &afsStatusp->CallerAccess))
+        afsStatusp->CallerAccess = scp->anyAccess;
+    afsStatusp->AnonymousAccess = scp->anyAccess;
+    afsStatusp->UnixModeBits = scp->unixModeBits;
+    afsStatusp->ParentVnode = scp->parentVnode;
+    afsStatusp->ParentUnique = scp->parentUnique;
+    afsStatusp->ResidencyMask = 0;
+    afsStatusp->ClientModTime = scp->clientModTime;
+    afsStatusp->ServerModTime = scp->serverModTime;
+    afsStatusp->Group = scp->group;
+    afsStatusp->SyncCounter = 0;
+    afsStatusp->dataVersionHigh = (afs_uint32)(scp->dataVersion >> 32);
+    afsStatusp->lockCount = 0;
+    afsStatusp->Length_hi = scp->length.HighPart;
+    afsStatusp->errorCode = 0;
+
+    memset(volSyncp, 0, sizeof(AFSVolSync));
+    volSyncp->spare1 = scp->lastUpdateRO;
+
+    return scp_locked;
+}
+
 /* Fetch a buffer.  Called with scp locked.
  * The scp is locked on return.
  */
 long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp,
                   cm_req_t *reqp)
 {
-    long code;
-    afs_int32 nbytes;                  /* bytes in transfer */
-    afs_int32 nbytes_hi = 0;            /* high-order 32 bits of bytes in transfer */
-    afs_int64 length_found = 0;
+    long code=0, code1=0;
+    afs_uint32 nbytes;                 /* bytes in transfer */
+    afs_uint32 nbytes_hi = 0;            /* high-order 32 bits of bytes in transfer */
+    afs_uint64 length_found = 0;
     long rbytes;                       /* bytes in rx_Read call */
     long temp;
     AFSFetchStatus afsStatus;
@@ -1344,13 +1456,17 @@ 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;
     int getroot;
     afs_int32 t1,t2;
     int require_64bit_ops = 0;
+    int call_was_64bit = 0;
+    int fs_fetchdata_offset_bug = 0;
+    int first_read = 1;
+    int scp_locked = 1;
 
     /* now, the buffer may or may not be filled with good data (buf_GetNew
      * drops lots of locks, and may indeed return a properly initialized
@@ -1400,22 +1516,19 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
                 memset(bufp->datap, 0, cm_data.buf_blockSize);
             bufp->dataVersion = scp->dataVersion;
         }
-        lock_ReleaseWrite(&scp->rw);
-        cm_ReleaseBIOD(&biod, 0, 0);
-        lock_ObtainWrite(&scp->rw);
+        cm_ReleaseBIOD(&biod, 0, 0, 1);
         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_ObtainWrite(&scp->rw);
+        cm_ReleaseBIOD(&biod, 0, 0, 1);
         return 0;
     }
 
     lock_ReleaseWrite(&scp->rw);
+    scp_locked = 0;
 
     if (LargeIntegerGreaterThan(LargeIntegerAdd(biod.offset,
                                                 ConvertLongToLargeInteger(biod.length)),
@@ -1456,7 +1569,8 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
         afsStatus.lockCount = 0;
         afsStatus.Length_hi = 0;
         afsStatus.errorCode = 0;
-       
+       memset(&volSync, 0, sizeof(volSync));
+
         // once we're done setting up the status info,
         // we just fill the buffer pages with fakedata
         // from cm_FakeRootDir. Extra pages are set to
@@ -1492,36 +1606,74 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
 
 #endif /* AFS_FREELANCE_CLIENT */
 
-       /* now make the call */
+    /*
+     * if the requested offset is greater than the file length,
+     * the file server will return zero bytes of data and the
+     * current status for the file which we already have since
+     * we have just obtained a callback.  Instead, we can avoid
+     * the network round trip by allocating zeroed buffers and
+     * faking the status info.
+     */
+    if (biod.offset.QuadPart >= scp->length.QuadPart) {
+        osi_Log5(afsd_logp, "SKIP FetchData64 scp 0x%p, off 0x%x:%08x > length 0x%x:%08x",
+                 scp, biod.offset.HighPart, biod.offset.LowPart,
+                 scp->length.HighPart, scp->length.LowPart);
+
+        /* Clone the current status info */
+        scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
+
+        /* status info complete, fill pages with zeros */
+        for (qdp = biod.bufListEndp;
+             qdp;
+             qdp = (osi_queueData_t *) osi_QPrev(&qdp->q)) {
+            tbufp = osi_GetQData(qdp);
+            bufferp=tbufp->datap;
+            memset(bufferp, 0, cm_data.buf_blockSize);
+        }
+
+        /* no need to contact the file server */
+        goto fetchingcompleted;
+    }
+
+    if (scp_locked) {
+        lock_ReleaseWrite(&scp->rw);
+        scp_locked = 0;
+    }
+
+    /* now make the call */
     do {
         code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
         if (code) 
             continue;
 
         rxconnp = cm_GetRxConn(connp);
-        callp = rx_NewCall(rxconnp);
+        rxcallp = rx_NewCall(rxconnp);
         rx_PutConnection(rxconnp);
 
 #ifdef AFS_LARGEFILES
         nbytes = nbytes_hi = 0;
 
         if (SERVERHAS64BIT(connp)) {
+            call_was_64bit = 1;
+
             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_Read32(rxcallp, &nbytes_hi);
                 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;
+                    code1 = rx_EndCall(rxcallp, code);
+                    rxcallp = NULL;
                 }
             }
+        } else {
+            call_was_64bit = 0;
         }
 
         if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
@@ -1529,16 +1681,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);
@@ -1546,14 +1698,25 @@ 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_Read32(rxcallp, &nbytes);
             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;
+                if (length_found > biod.length) {
+                    /*
+                     * prior to 1.4.12 and 1.5.65 the file server would return
+                     * (filesize - offset) if the requested offset was greater than
+                     * the filesize.  The correct return value would have been zero.
+                     * Force a retry by returning an RX_PROTOCOL_ERROR.  If the cause
+                     * is a race between two RPCs issues by this cache manager, the
+                     * correct thing will happen the second time.
+                     */
+                    osi_Log0(afsd_logp, "cm_GetBuffer length_found > biod.length");
+                    fs_fetchdata_offset_bug = 1;
+                }
             } else {
-                code = (callp->error < 0) ? callp->error : -1;
+                osi_Log1(afsd_logp, "cm_GetBuffer rx_Read32 returns %d != 4", temp);
+                code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
             }
         }
         /* for the moment, nbytes_hi will always be 0 if code == 0
@@ -1562,19 +1725,31 @@ 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_Read32(rxcallp, &nbytes);
             if (temp == sizeof(afs_int32)) {
-                nbytes = ntohl(nbytes);
-                if (nbytes > biod.length) 
-                    code = (callp->error < 0) ? callp->error : -1;
+                length_found = ntohl(nbytes);
+                if (length_found > biod.length) {
+                    /*
+                     * prior to 1.4.12 and 1.5.65 the file server would return
+                     * (filesize - offset) if the requested offset was greater than
+                     * the filesize.  The correct return value would have been zero.
+                     * Force a retry by returning an RX_PROTOCOL_ERROR.  If the cause
+                     * is a race between two RPCs issues by this cache manager, the
+                     * correct thing will happen the second time.
+                     */
+                    osi_Log0(afsd_logp, "cm_GetBuffer length_found > biod.length");
+                    fs_fetchdata_offset_bug = 1;
+                }
+            }
+            else {
+                osi_Log1(afsd_logp, "cm_GetBuffer rx_Read32 returns %d != 4", temp);
+                code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
             }
-            else 
-                code = (callp->error < 0) ? callp->error : -1;
         }
 #endif
 
@@ -1586,27 +1761,38 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
             }
             else 
                 bufferp = NULL;
-            /* fill nbytes of data from the pipe into the pages.
+            /* fill length_found of data from the pipe into the pages.
              * When we stop, qdp will point at the last page we're
              * dealing with, and bufferp will tell us where we
              * stopped.  We'll need this info below when we clear
              * the remainder of the last page out (and potentially
              * clear later pages out, if we fetch past EOF).
              */
-            while (nbytes > 0) {
+            while (length_found > 0) {
                 /* assert that there are still more buffers;
-                 * our check above for nbytes being less than
+                 * our check above for length_found being less than
                  * biod.length should ensure this.
                  */
                 osi_assertx(bufferp != NULL, "null cm_buf_t");
 
                 /* read rbytes of data */
-                rbytes = (nbytes > cm_data.buf_blockSize? cm_data.buf_blockSize : nbytes);
-                temp = rx_Read(callp, bufferp, rbytes);
+                rbytes = (afs_uint32)(length_found > cm_data.buf_blockSize ? cm_data.buf_blockSize : length_found);
+                temp = rx_Read(rxcallp, bufferp, rbytes);
                 if (temp < rbytes) {
-                    code = (callp->error < 0) ? callp->error : -1;
+                    /*
+                     * If the file server returned (filesize - offset),
+                     * then the first rx_Read will return zero octets of data.
+                     * If it does, do not treat it as an error.  Correct the
+                     * length_found and continue as if the file server said
+                     * it was sending us zero octets of data.
+                     */
+                    if (fs_fetchdata_offset_bug && first_read)
+                        length_found = 0;
+                    else
+                        code = (rxcallp->error < 0) ? rxcallp->error : RX_PROTOCOL_ERROR;
                     break;
                 }
+                first_read = 0;
 
                 /* allow read-while-fetching.
                  * if this is the last buffer, clear the
@@ -1627,10 +1813,10 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
                 lock_ReleaseWrite(&scp->rw);
 
                 /* and adjust counters */
-                nbytes -= temp;
+                length_found -= temp;
 
                 /* and move to the next buffer */
-                if (nbytes != 0) {
+                if (length_found != 0) {
                     qdp = (osi_queueData_t *) osi_QPrev(&qdp->q);
                     if (qdp) {
                         tbufp = osi_GetQData(qdp);
@@ -1668,23 +1854,35 @@ 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);
+            if (call_was_64bit)
+                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))
+            if (call_was_64bit)
                 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData64 skipped due to error %d", code);
             else
                 osi_Log1(afsd_logp, "CALL EndRXAFS_FetchData skipped due to error %d", code);
         }
 
-        if (callp)
-            code = rx_EndCall(callp, code);
+        if (rxcallp)
+            code1 = rx_EndCall(rxcallp, code);
 
-        if (code == RXKADUNKNOWNKEY)
+        if (code1 == RXKADUNKNOWNKEY)
             osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
 
+        /* If we are avoiding a file server bug, ignore the error state */
+        if (fs_fetchdata_offset_bug && first_read && length_found == 0 && code == -451) {
+            /* Clone the current status info and clear the error state */
+            scp_locked = cm_CloneStatus(scp, userp, scp_locked, &afsStatus, &volSync);
+            if (scp_locked) {
+                lock_ReleaseWrite(&scp->rw);
+                scp_locked = 0;
+            }
+            code = 0;
+        /* Prefer the error value from FetchData over rx_EndCall */
+        } else if (code == 0 && code1 != 0)
+            code = code1;
         osi_Log0(afsd_logp, "CALL FetchData DONE");
 
     } while (cm_Analyze(connp, userp, reqp, &scp->fid, &volSync, NULL, NULL, code));
@@ -1692,10 +1890,9 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
   fetchingcompleted:
     code = cm_MapRPCError(code, reqp);
 
-    lock_ObtainWrite(&scp->rw);
+    if (!scp_locked)
+        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
@@ -1719,12 +1916,10 @@ 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_ReleaseWrite(&scp->rw);
-    cm_ReleaseBIOD(&biod, 0, code);
-    lock_ObtainWrite(&scp->rw);
+    cm_ReleaseBIOD(&biod, 0, code, 1);
 
     if (code == 0) 
-        cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, 0);
+        cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, 0);
     
     return code;
 }