Windows: Decr. used buf count flush and deleted files
[openafs.git] / src / WINNT / afsd / cm_buf.c
index e64b1f4..2b9cfcf 100644 (file)
@@ -518,6 +518,7 @@ long buf_Init(int newFile, cm_buf_ops_t *opsp, afs_uint64 nbuffers)
             bp = cm_data.bufHeaderBaseAddress;
             data = cm_data.bufDataBaseAddress;
 
+            lock_ObtainWrite(&buf_globalLock);
             for (i=0; i<cm_data.buf_nbuffers; i++) {
                 lock_InitializeMutex(&bp->mx, "Buffer mutex", LOCK_HIERARCHY_BUFFER);
                 bp->userp = NULL;
@@ -530,16 +531,13 @@ long buf_Init(int newFile, cm_buf_ops_t *opsp, afs_uint64 nbuffers)
                      * extent was not returned by the file system driver.
                      * clean up the mess.
                      */
+                    buf_RemoveFromRedirQueue(NULL, bp);
                     bp->dataVersion = CM_BUF_VERSION_BAD;
-                    _InterlockedAnd(&bp->qFlags, ~CM_BUF_QREDIR);
-                    osi_QRemoveHT( (osi_queue_t **) &cm_data.buf_redirListp,
-                                   (osi_queue_t **) &cm_data.buf_redirListEndp,
-                                   &bp->q);
-                    buf_DecrementRedirCount();
                     bp->redirq.nextp = bp->redirq.prevp = NULL;
                     bp->redirLastAccess = 0;
                     bp->redirReleaseRequested = 0;
-                    buf_Release(bp);
+                    buf_ReleaseLocked(bp, TRUE);
+                    InterlockedDecrement(&cm_data.buf_usedCount);
                 }
                 bp++;
             }
@@ -556,17 +554,15 @@ long buf_Init(int newFile, cm_buf_ops_t *opsp, afs_uint64 nbuffers)
                  * extent was not returned by the file system driver.
                  * clean up the mess.
                  */
+                buf_RemoveFromRedirQueue(NULL, bp);
                 bp->dataVersion = CM_BUF_VERSION_BAD;
-                _InterlockedAnd(&bp->qFlags, ~CM_BUF_QREDIR);
-                osi_QRemoveHT( (osi_queue_t **) &cm_data.buf_redirListp,
-                               (osi_queue_t **) &cm_data.buf_redirListEndp,
-                               &bp->q);
-                buf_DecrementRedirCount();
                 bp->redirq.nextp = bp->redirq.prevp = NULL;
                 bp->redirLastAccess = 0;
                 bp->redirReleaseRequested = 0;
-                buf_Release(bp);
+                buf_ReleaseLocked(bp, TRUE);
+                InterlockedDecrement(&cm_data.buf_usedCount);
             }
+            lock_ReleaseWrite(&buf_globalLock);
         }
 
 #ifdef TESTING
@@ -680,7 +676,7 @@ void buf_WaitIO(cm_scache_t * scp, cm_buf_t *bp)
         }
         if ( scp ) {
             lock_ObtainRead(&scp->rw);
-            if (scp->flags & CM_SCACHEFLAG_WAITING) {
+            if (!osi_QIsEmpty(&scp->waitQueueH)) {
                 osi_Log1(buf_logp, "buf_WaitIO waking scp 0x%p", scp);
                 osi_Wakeup((LONG_PTR)&scp->flags);
             }
@@ -816,13 +812,30 @@ afs_uint32 buf_CleanLocked(cm_scache_t *scp, cm_buf_t *bp, cm_req_t *reqp,
      * that the cm_scache_t was recycled out of the cache even though
      * a cm_buf_t with the same FID is in the cache.
      */
-    if (scp == NULL) {
-        if ((scp = cm_FindSCache(&bp->fid)) ||
-            (cm_GetSCache(&bp->fid, &scp,
-                          bp->userp ? bp->userp : cm_rootUserp,
-                          reqp) == 0)) {
-            release_scp = 1;
+    if (scp == NULL &&
+        cm_GetSCache(&bp->fid, NULL, &scp,
+                     bp->userp ? bp->userp : cm_rootUserp,
+                     reqp) == 0)
+    {
+        release_scp = 1;
+
+        lock_ObtainWrite(&scp->rw);
+        code = cm_SyncOp(scp, NULL, bp->userp ? bp->userp : cm_rootUserp, reqp, 0,
+                         CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
+        if (code == 0) {
+            cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
         }
+        lock_ReleaseWrite(&scp->rw);
+    }
+
+    if (scp && (scp->flags & CM_SCACHEFLAG_DELETED)) {
+        _InterlockedAnd(&bp->flags, ~CM_BUF_DIRTY);
+        _InterlockedOr(&bp->flags, CM_BUF_ERROR);
+        bp->dirty_length = 0;
+        bp->error = code;
+        bp->dataVersion = CM_BUF_VERSION_BAD;
+        bp->dirtyCounter++;
+        InterlockedDecrement(&cm_data.buf_usedCount);
     }
 
     while ((bp->flags & CM_BUF_DIRTY) == CM_BUF_DIRTY) {
@@ -850,9 +863,8 @@ afs_uint32 buf_CleanLocked(cm_scache_t *scp, cm_buf_t *bp, cm_req_t *reqp,
              * to determine if it is appropriate to fill a full chunk of data
              * when storing to the file server.
              */
-            code = (*cm_buf_opsp->Writep)(scp, &offset,
-                                          bp->dirty_length,
-                                          flags, bp->userp, reqp);
+            code = (*cm_buf_opsp->Writep)(scp, &offset, bp->dirty_length, flags,
+                                          bp->userp ? bp->userp : cm_rootUserp, reqp);
             osi_Log3(buf_logp, "buf_CleanLocked I/O on scp 0x%p buf 0x%p, done=%d", scp, bp, code);
         }
         lock_ObtainMutex(&bp->mx);
@@ -862,14 +874,15 @@ afs_uint32 buf_CleanLocked(cm_scache_t *scp, cm_buf_t *bp, cm_req_t *reqp,
         */
        if (code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BADFD || code == CM_ERROR_NOACCESS ||
             code == CM_ERROR_QUOTA || code == CM_ERROR_SPACE || code == CM_ERROR_TOOBIG ||
-            code == CM_ERROR_READONLY || code == CM_ERROR_NOSUCHPATH){
+            code == CM_ERROR_READONLY || code == CM_ERROR_NOSUCHPATH || code == EIO ||
+            code == CM_ERROR_INVAL || code == CM_ERROR_INVAL_NET_RESP || code == CM_ERROR_UNKNOWN){
            _InterlockedAnd(&bp->flags, ~CM_BUF_DIRTY);
            _InterlockedOr(&bp->flags, CM_BUF_ERROR);
-            bp->dirty_offset = 0;
             bp->dirty_length = 0;
            bp->error = code;
            bp->dataVersion = CM_BUF_VERSION_BAD;
            bp->dirtyCounter++;
+            InterlockedDecrement(&cm_data.buf_usedCount);
             break;
        }
 
@@ -886,10 +899,14 @@ afs_uint32 buf_CleanLocked(cm_scache_t *scp, cm_buf_t *bp, cm_req_t *reqp,
        if (reqp->flags & CM_REQ_NORETRY)
            break;
 
-        /* Ditto if the hardDeadTimeout or idleTimeout was reached */
+        /*
+         * Ditto if the hardDeadTimeout or idleTimeout was reached
+         * Or a fatal error is received.
+         */
         if (code == CM_ERROR_TIMEDOUT || code == CM_ERROR_ALLDOWN ||
             code == CM_ERROR_ALLBUSY || code == CM_ERROR_ALLOFFLINE ||
-            code == CM_ERROR_CLOCKSKEW) {
+            code == CM_ERROR_CLOCKSKEW || code == CM_ERROR_INVAL_NET_RESP ||
+            code == CM_ERROR_INVAL || code == CM_ERROR_UNKNOWN || code == EIO) {
             break;
         }
     }
@@ -1246,7 +1263,7 @@ long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_req_t *req
     cm_buf_t *bp;      /* buffer we're dealing with */
     cm_buf_t *nextBp;  /* next buffer in file hash chain */
     afs_uint32 i;      /* temp */
-    afs_uint64 n_bufs, n_nonzero, n_busy, n_dirty, n_own;
+    afs_uint64 n_bufs, n_nonzero, n_busy, n_dirty, n_own, n_redir;
 
 #ifdef TESTING
     buf_ValidateBufQueues();
@@ -1259,6 +1276,7 @@ long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_req_t *req
         n_own = 0;
         n_busy = 0;
         n_dirty = 0;
+        n_redir = 0;
 
         lock_ObtainRead(&scp->bufCreateLock);
         lock_ObtainWrite(&buf_globalLock);
@@ -1337,13 +1355,15 @@ long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_req_t *req
                 continue;
             }
 
+            /* leave the buffer alone if held by the redirector */
+            if (bp->qFlags & CM_BUF_QREDIR) {
+                n_redir++;
+                continue;
+            }
+
             if (bp->flags & CM_BUF_DIRTY) {
                 n_dirty++;
 
-                /* leave the buffer alone if held by the redirector */
-                if (bp->qFlags & CM_BUF_QREDIR)
-                    continue;
-
                 /* protect against cleaning the same buffer more than once. */
                 if (cleaned)
                     continue;
@@ -1464,6 +1484,9 @@ long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_req_t *req
                osi_panic("buf_GetNewLocked: TryMutex failed",__FILE__,__LINE__);
            }
 
+            if ( cm_data.buf_usedCount < cm_data.buf_nbuffers)
+                InterlockedIncrement(&cm_data.buf_usedCount);
+
             lock_ReleaseWrite(&buf_globalLock);
             lock_ReleaseRead(&scp->bufCreateLock);
 
@@ -1477,7 +1500,7 @@ long buf_GetNewLocked(struct cm_scache *scp, osi_hyper_t *offsetp, cm_req_t *req
         lock_ReleaseWrite(&buf_globalLock);
         lock_ReleaseRead(&scp->bufCreateLock);
 
-       osi_Log1(afsd_logp, "buf_GetNewLocked: Free Buffer List has %u buffers none free", n_bufs);
+       osi_Log2(afsd_logp, "buf_GetNewLocked: Free Buffer List has %u buffers none free; redir %u", n_bufs, n_redir);
         osi_Log4(afsd_logp, "... nonzero %u; own %u; busy %u; dirty %u", n_nonzero, n_own, n_busy, n_dirty);
 
         if (RDR_Initialized) {
@@ -1690,6 +1713,7 @@ void buf_SetDirty(cm_buf_t *bp, cm_req_t *reqp, afs_uint32 offset, afs_uint32 le
 {
     osi_assertx(bp->magic == CM_BUF_MAGIC, "invalid cm_buf_t magic");
     osi_assertx(bp->refCount > 0, "cm_buf_t refcount 0");
+    osi_assertx(userp != NULL, "userp is NULL");
 
     if (length == 0)
         return;
@@ -1972,10 +1996,10 @@ long buf_Truncate(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
                 }
                 _InterlockedAnd(&bufp->flags, ~CM_BUF_DIRTY);
                 bufp->error = 0;
-                bufp->dirty_offset = 0;
                 bufp->dirty_length = 0;
                 bufp->dataVersion = CM_BUF_VERSION_BAD;        /* known bad */
                 bufp->dirtyCounter++;
+                InterlockedDecrement(&cm_data.buf_usedCount);
             }
             else {
                 /* don't set dirty, since dirty implies
@@ -2070,11 +2094,11 @@ long buf_FlushCleanPages(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
                 _InterlockedAnd(&bp->flags, ~CM_BUF_DIRTY);
                 _InterlockedOr(&bp->flags, CM_BUF_ERROR);
                 bp->error = CM_ERROR_BADFD;
-                bp->dirty_offset = 0;
                 bp->dirty_length = 0;
                 bp->dataVersion = CM_BUF_VERSION_BAD;  /* known bad */
                 bp->dirtyCounter++;
                 lock_ReleaseMutex(&bp->mx);
+                InterlockedDecrement(&cm_data.buf_usedCount);
             } else if (!(scp->flags & CM_SCACHEFLAG_RO)) {
                 if (code) {
                     goto skip;
@@ -2101,6 +2125,8 @@ long buf_FlushCleanPages(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
                             buf_HoldLocked(nbp);
                         buf_ReleaseLocked(bp, TRUE);
                         didRelease = 1;
+                        if (bp->dataVersion != CM_BUF_VERSION_BAD)
+                            InterlockedDecrement(&cm_data.buf_usedCount);
                         buf_Recycle(bp);
                     }
                 }
@@ -2146,6 +2172,7 @@ long buf_InvalidateBuffers(cm_scache_t * scp)
     for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp = bp->fileHashp) {
         if (cm_FidCmp(&bp->fid, &scp->fid) == 0) {
             bp->dataVersion = CM_BUF_VERSION_BAD;
+            InterlockedDecrement(&cm_data.buf_usedCount);
             found = 1;
         }
     }
@@ -2234,6 +2261,7 @@ long buf_CleanVnode(struct cm_scache *scp, cm_user_t *userp, cm_req_t *reqp)
 
                 switch (code) {
                 case CM_ERROR_NOSUCHFILE:
+                case CM_ERROR_INVAL:
                 case CM_ERROR_BADFD:
                 case CM_ERROR_NOACCESS:
                 case CM_ERROR_QUOTA:
@@ -2241,6 +2269,9 @@ long buf_CleanVnode(struct cm_scache *scp, cm_user_t *userp, cm_req_t *reqp)
                 case CM_ERROR_TOOBIG:
                 case CM_ERROR_READONLY:
                 case CM_ERROR_NOSUCHPATH:
+                case EIO:
+                case CM_ERROR_INVAL_NET_RESP:
+                case CM_ERROR_UNKNOWN:
                     /*
                      * Apply the previous fatal error to this buffer.
                      * Do not waste the time attempting to store to
@@ -2248,11 +2279,11 @@ long buf_CleanVnode(struct cm_scache *scp, cm_user_t *userp, cm_req_t *reqp)
                      */
                     _InterlockedAnd(&bp->flags, ~CM_BUF_DIRTY);
                     _InterlockedOr(&bp->flags, CM_BUF_ERROR);
-                    bp->dirty_offset = 0;
                     bp->dirty_length = 0;
                     bp->error = code;
                     bp->dataVersion = CM_BUF_VERSION_BAD;
                     bp->dirtyCounter++;
+                    InterlockedDecrement(&cm_data.buf_usedCount);
                     break;
                 case CM_ERROR_TIMEDOUT:
                 case CM_ERROR_ALLDOWN:
@@ -2443,7 +2474,7 @@ long buf_DirtyBuffersExist(cm_fid_t *fidp)
         }
     }
     lock_ReleaseRead(&buf_globalLock);
-    return 0;
+    return found;
 }
 
 long buf_RDRBuffersExist(cm_fid_t *fidp)
@@ -2525,7 +2556,6 @@ long buf_CleanDirtyBuffers(cm_scache_t *scp)
            lock_ObtainMutex(&bp->mx);
            _InterlockedAnd(&bp->cmFlags, ~CM_BUF_CMSTORING);
            _InterlockedAnd(&bp->flags, ~CM_BUF_DIRTY);
-            bp->dirty_offset = 0;
             bp->dirty_length = 0;
            _InterlockedOr(&bp->flags, CM_BUF_ERROR);
            bp->error = VNOVNODE;
@@ -2537,6 +2567,7 @@ long buf_CleanDirtyBuffers(cm_scache_t *scp)
            }
            lock_ReleaseMutex(&bp->mx);
            buf_Release(bp);
+            InterlockedDecrement(&cm_data.buf_usedCount);
        }
     }
     return 0;
@@ -2600,7 +2631,9 @@ buf_InsertToRedirQueue(cm_scache_t *scp, cm_buf_t *bufp)
 {
     lock_AssertWrite(&buf_globalLock);
 
-    lock_ObtainMutex(&scp->redirMx);
+    if (scp) {
+        lock_ObtainMutex(&scp->redirMx);
+    }
 
     if (bufp->qFlags & CM_BUF_QINLRU) {
         _InterlockedAnd(&bufp->qFlags, ~CM_BUF_QINLRU);
@@ -2621,9 +2654,9 @@ buf_InsertToRedirQueue(cm_scache_t *scp, cm_buf_t *bufp)
                    &bufp->redirq);
         scp->redirLastAccess = bufp->redirLastAccess;
         InterlockedIncrement(&scp->redirBufCount);
-    }
 
-    lock_ReleaseMutex(&scp->redirMx);
+        lock_ReleaseMutex(&scp->redirMx);
+    }
 }
 
 void
@@ -2634,21 +2667,24 @@ buf_RemoveFromRedirQueue(cm_scache_t *scp, cm_buf_t *bufp)
     if (!(bufp->qFlags & CM_BUF_QREDIR))
         return;
 
-    lock_ObtainMutex(&scp->redirMx);
+    if (scp) {
+        lock_ObtainMutex(&scp->redirMx);
+    }
 
     _InterlockedAnd(&bufp->qFlags, ~CM_BUF_QREDIR);
     osi_QRemoveHT( (osi_queue_t **) &cm_data.buf_redirListp,
                    (osi_queue_t **) &cm_data.buf_redirListEndp,
                    &bufp->q);
     buf_DecrementRedirCount();
+
     if (scp) {
         osi_QRemoveHT( (osi_queue_t **) &scp->redirQueueH,
                        (osi_queue_t **) &scp->redirQueueT,
                        &bufp->redirq);
+
         InterlockedDecrement(&scp->redirBufCount);
+        lock_ReleaseMutex(&scp->redirMx);
     }
-
-    lock_ReleaseMutex(&scp->redirMx);
 }
 
 void
@@ -2658,7 +2694,9 @@ buf_MoveToHeadOfRedirQueue(cm_scache_t *scp, cm_buf_t *bufp)
     if (!(bufp->qFlags & CM_BUF_QREDIR))
         return;
 
-    lock_ObtainMutex(&scp->redirMx);
+    if (scp) {
+        lock_ObtainMutex(&scp->redirMx);
+    }
 
     osi_QRemoveHT( (osi_queue_t **) &cm_data.buf_redirListp,
                    (osi_queue_t **) &cm_data.buf_redirListEndp,
@@ -2675,7 +2713,7 @@ buf_MoveToHeadOfRedirQueue(cm_scache_t *scp, cm_buf_t *bufp)
                    (osi_queue_t **) &scp->redirQueueT,
                    &bufp->redirq);
         scp->redirLastAccess = bufp->redirLastAccess;
-    }
 
-    lock_ReleaseMutex(&scp->redirMx);
+        lock_ReleaseMutex(&scp->redirMx);
+    }
 }