Windows: cm_SetupStoreBIOD use firstModOffset chunk
[openafs.git] / src / WINNT / afsd / cm_dcache.c
index b8158b5..9a7e82a 100644 (file)
@@ -22,6 +22,7 @@
 #include <osi.h>
 
 #include "afsd.h"
+#include "smb.h"
 
 #ifdef DEBUG
 extern void afsi_log(char *pattern, ...);
@@ -44,11 +45,10 @@ extern osi_mutex_t cm_Freelance_Lock;
 long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
                  cm_user_t *userp, cm_req_t *reqp)
 {
-    /* store the data back from this buffer; the buffer is locked and held,
-     * but the vnode involved isn't locked, yet.  It is held by its
-     * reference from the buffer, which won't change until the buffer is
-     * released by our caller.  Thus, we don't have to worry about holding
-     * bufp->scp.
+    /*
+     * store the data back from this buffer; the buffer is locked and held,
+     * but the vnode involved may or may not be locked depending on whether
+     * or not the CM_BUF_WRITE_SCP_LOCKED flag is set.
      */
     long code, code1;
     cm_scache_t *scp = vscp;
@@ -72,13 +72,15 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
     int require_64bit_ops = 0;
     int call_was_64bit = 0;
     int scp_locked = flags & CM_BUF_WRITE_SCP_LOCKED;
+    int storedata_excl = 0;
 
     osi_assertx(userp != NULL, "null cm_user_t");
     osi_assertx(scp != NULL, "null cm_scache_t");
 
     memset(&volSync, 0, sizeof(volSync));
 
-    /* now, the buffer may or may not be filled with good data (buf_GetNewLocked
+    /*
+     * now, the buffer may or may not be filled with good data (buf_GetNewLocked
      * drops lots of locks, and may indeed return a properly initialized
      * buffer, although more likely it will just return a new, empty, buffer.
      */
@@ -94,6 +96,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
 
     /* Serialize StoreData RPC's; for rationale see cm_scache.c */
     (void) cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA_EXCL);
+    storedata_excl = 1;
 
     code = cm_SetupStoreBIOD(scp, offsetp, length, &biod, userp, reqp);
     if (code) {
@@ -107,10 +110,8 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
     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);
-        if (!scp_locked)
-            lock_ReleaseWrite(&scp->rw);
-        return 0;
+        code = 0;
+        goto exit_storedata_excl;
     }
 
     /* prepare the output status for the store */
@@ -151,6 +152,7 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
         require_64bit_ops = 1;
     }
 
+    InterlockedIncrement(&scp->activeRPCs);
     lock_ReleaseWrite(&scp->rw);
 
     /* now we're ready to do the store operation */
@@ -307,6 +309,36 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
                 }
                 nbytes -= wbytes;
 #endif /* USE_RX_IOVEC */
+
+                /*
+                 * Rx supports an out of band signalling mechanism that permits
+                 * RPC specific status information to be communicated in the
+                 * reverse direction of the channel.  For RXAFS_StoreData, the
+                 * 0-bit is set once all of the permission checks have completed
+                 * and the volume/vnode locks have been obtained by the file
+                 * server.  The signal is intended to notify the Unix afs client
+                 * that is performing store-on-close that it is safe to permit
+                 * the close operation to complete while the store continues
+                 * in the background.  All of the callbacks have been broken
+                 * and the locks will not be dropped until the RPC completes
+                 * which prevents any other operation from being initiated on
+                 * the vnode until the store is finished.
+                 *
+                 * The Windows client does not perform store-on-close.  Instead
+                 * it uses the CM_SCACHESYNC_STOREDATA_EXCL request flag and
+                 * CM_SCACHEFLAG_DATASTORING scache state to ensure that store
+                 * operations are serialized.  The 0-bit signal permits the
+                 * CM_SCACHEFLAG_DATASTORING state to the dropped which in
+                 * turn permits another thread to prep its own BIOD in parallel.
+                 * This is safe because it is impossible for that second store
+                 * RPC to complete before this one does.
+                 */
+                if ( storedata_excl && (rx_GetRemoteStatus(rxcallp) & 1)) {
+                    lock_ObtainWrite(&scp->rw);
+                    cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
+                    lock_ReleaseWrite(&scp->rw);
+                    storedata_excl = 0;
+                }
             }  /* while more bytes to write */
         }      /* if RPC started successfully */
 
@@ -386,12 +418,16 @@ long cm_BufWrite(void *vscp, osi_hyper_t *offsetp, long length, long flags,
 
         cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
     } else {
+        InterlockedDecrement(&scp->activeRPCs);
         if (code == CM_ERROR_SPACE)
             _InterlockedOr(&scp->flags, CM_SCACHEFLAG_OUTOFSPACE);
         else if (code == CM_ERROR_QUOTA)
             _InterlockedOr(&scp->flags, CM_SCACHEFLAG_OVERQUOTA);
     }
-    cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
+
+  exit_storedata_excl:
+    if (storedata_excl)
+        cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
 
     if (!scp_locked)
         lock_ReleaseWrite(&scp->rw);
@@ -444,6 +480,7 @@ long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
         require_64bit_ops = 1;
     }
 
+    InterlockedIncrement(&scp->activeRPCs);
     lock_ReleaseWrite(&scp->rw);
 
     cm_AFSFidFromFid(&tfid, &scp->fid);
@@ -536,6 +573,8 @@ long cm_StoreMini(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
         if (LargeIntegerGreaterThanOrEqualTo(t, scp->length))
             _InterlockedAnd(&scp->mask, ~CM_SCACHEMASK_LENGTH);
         cm_MergeStatus(NULL, scp, &outStatus, &volSync, userp, reqp, CM_MERGEFLAG_STOREDATA);
+    } else {
+        InterlockedDecrement(&scp->activeRPCs);
     }
     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STOREDATA_EXCL);
 
@@ -732,12 +771,12 @@ cm_BkgStore(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_u
     osi_hyper_t toffset;
     long length;
     long code = 0;
+    afs_uint32 req_flags = reqp->flags;
 
     if (scp->flags & CM_SCACHEFLAG_DELETED) {
        osi_Log4(afsd_logp, "Skipping BKG store - Deleted scp 0x%p, offset 0x%x:%08x, length 0x%x", scp, p2, p1, p3);
     } else {
        /* Retries will be performed by the BkgDaemon thread if appropriate */
-        afs_uint32 req_flags = reqp->flags;
        reqp->flags |= CM_REQ_NORETRY;
 
        toffset.LowPart = p1;
@@ -1049,8 +1088,8 @@ long cm_SetupStoreBIOD(cm_scache_t *scp, osi_hyper_t *inOffsetp, long inSize,
     biop->offset = firstModOffset;
     bufp = NULL;       /* this buffer and reference added to the queue */
 
-    /* compute the window surrounding *inOffsetp of size cm_chunkSize */
-    scanStart = *inOffsetp;
+    /* compute the window surrounding firstModOffset of size cm_chunkSize */
+    scanStart = firstModOffset;
     scanStart.LowPart &= (-cm_chunkSize);
     thyper = ConvertLongToLargeInteger(cm_chunkSize);
     scanEnd = LargeIntegerAdd(scanStart, thyper);
@@ -1420,6 +1459,7 @@ void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, long code, int scp_locked)
     osi_queueData_t *qdp;
     osi_queueData_t *nqdp;
     int flags;
+    int reportErrorToRedir = 0;
 
     /* Give back reserved buffers */
     if (biop->reserved)
@@ -1477,6 +1517,7 @@ void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, long code, int scp_locked)
                         bufp->error = code;
                         bufp->dataVersion = CM_BUF_VERSION_BAD;
                         bufp->dirtyCounter++;
+                        reportErrorToRedir = 1;
                         break;
                     case CM_ERROR_TIMEDOUT:
                     case CM_ERROR_ALLDOWN:
@@ -1501,6 +1542,12 @@ void cm_ReleaseBIOD(cm_bulkIO_t *biop, int isStore, long code, int scp_locked)
            buf_Release(bufp);
            bufp = NULL;
        }
+
+        if (RDR_Initialized && reportErrorToRedir) {
+            DWORD status;
+            smb_MapNTError(cm_MapRPCError(code, biop->reqp), &status, TRUE);
+            RDR_SetFileStatus( &scp->fid, status);
+        }
     } else {
        if (!scp_locked)
             lock_ObtainWrite(&scp->rw);
@@ -1660,6 +1707,7 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
         return 0;
     }
 
+    InterlockedIncrement(&scp->activeRPCs);
     lock_ReleaseWrite(&scp->rw);
     scp_locked = 0;
 
@@ -2103,6 +2151,8 @@ long cm_GetBuffer(cm_scache_t *scp, cm_buf_t *bufp, int *cpffp, cm_user_t *userp
 
     if (code == 0)
         cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
+    else
+        InterlockedDecrement(&scp->activeRPCs);
 
     return code;
 }
@@ -2164,6 +2214,7 @@ long cm_GetData(cm_scache_t *scp, osi_hyper_t *offsetp, char *datap, int data_le
         require_64bit_ops = 1;
     }
 
+    InterlockedIncrement(&scp->activeRPCs);
     osi_Log2(afsd_logp, "cm_GetData: fetching data scp %p DV 0x%x", scp, scp->dataVersion);
 
 #ifdef AFS_FREELANCE_CLIENT
@@ -2452,6 +2503,8 @@ long cm_GetData(cm_scache_t *scp, osi_hyper_t *offsetp, char *datap, int data_le
 
     if (code == 0)
         cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, CM_MERGEFLAG_FETCHDATA);
+    else
+        InterlockedDecrement(&scp->activeRPCs);
 
     return code;
 }