Windows: rx_Write failures must be processed
[openafs.git] / src / WINNT / afsd / cm_dcache.c
index 89f4963..2b22338 100644 (file)
@@ -52,9 +52,12 @@ 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;
@@ -71,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");
@@ -88,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);
@@ -145,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) 
@@ -157,6 +166,8 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
 
 #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);
 
@@ -169,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;
@@ -200,16 +213,20 @@ 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(rxcallp, bufferp, wbytes);
@@ -225,7 +242,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
         }      /* if RPC started successfully */
 
         if (code == 0) {
-            if (SERVERHAS64BIT(connp)) {
+            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);
@@ -240,15 +257,19 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
             }
         }
 
-        code = rx_EndCall(rxcallp, 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);
@@ -261,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) {
@@ -284,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 {
@@ -294,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;
@@ -302,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;
 }
@@ -318,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 *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,
@@ -364,9 +386,13 @@ long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
 
 #ifdef AFS_LARGEFILES
         if (SERVERHAS64BIT(connp)) {
+            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 {
@@ -380,20 +406,22 @@ long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
 #endif
 
         if (code == 0) {
-            if (SERVERHAS64BIT(connp))
+            if (call_was_64bit)
                 code = EndRXAFS_StoreData64(rxcallp, &outStatus, &volSync);
             else
                 code = EndRXAFS_StoreData(rxcallp, &outStatus, &volSync);
         }
-        code = rx_EndCall(rxcallp, 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);
         
@@ -408,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 {
@@ -417,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;
@@ -708,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;
 
@@ -1100,7 +1128,7 @@ 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_ObtainWrite(&scp->rw);
             cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
@@ -1254,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;
@@ -1285,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);
@@ -1295,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,10 +1389,10 @@ void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, int failed)
 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, code1;
+    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;
@@ -1345,6 +1409,7 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
     int getroot;
     afs_int32 t1,t2;
     int require_64bit_ops = 0;
+    int call_was_64bit = 0;
 
     /* 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
@@ -1394,18 +1459,14 @@ 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;
     }
 
@@ -1500,6 +1561,8 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
         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);
 
@@ -1512,10 +1575,12 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
                 } else {
                     nbytes_hi = 0;
                    code = rxcallp->error;
-                    rx_EndCall(rxcallp, code);
+                    code1 = rx_EndCall(rxcallp, code);
                     rxcallp = NULL;
                 }
             }
+        } else {
+            call_was_64bit = 0;
         }
 
         if (code == RXGEN_OPCODE || !SERVERHAS64BIT(connp)) {
@@ -1662,23 +1727,26 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
         }
 
         if (code == 0) {
-            if (SERVERHAS64BIT(connp))
+            if (call_was_64bit)
                 code = EndRXAFS_FetchData64(rxcallp, &afsStatus, &callback, &volSync);
             else
                 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 (rxcallp)
-            code = rx_EndCall(rxcallp, code);
+            code1 = rx_EndCall(rxcallp, code);
 
-        if (code == RXKADUNKNOWNKEY)
+        if (code1 == RXKADUNKNOWNKEY)
             osi_Log0(afsd_logp, "CALL EndCall returns RXKADUNKNOWNKEY");
 
+        /* Prefer the error value from FetchData over rx_EndCall */
+        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));
@@ -1711,12 +1779,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;
 }