Windows: buf redir queue mgmt scp can be null
[openafs.git] / src / WINNT / afsd / cm_buf.c
index 5e9d3cf..d7a27c5 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,12 @@ 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);
                 }
                 bp++;
             }
@@ -556,17 +553,14 @@ 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);
             }
+            lock_ReleaseWrite(&buf_globalLock);
         }
 
 #ifdef TESTING
@@ -1245,7 +1239,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();
@@ -1258,6 +1252,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);
@@ -1336,13 +1331,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;
@@ -1476,7 +1473,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) {
@@ -2599,7 +2596,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);
@@ -2620,9 +2619,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
@@ -2633,21 +2632,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
@@ -2657,7 +2659,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,
@@ -2674,7 +2678,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);
+    }
 }