Windows: buf_RemoveFromRedirQueue in buf_Init
[openafs.git] / src / WINNT / afsd / cm_buf.c
index 7283271..0938239 100644 (file)
@@ -188,34 +188,9 @@ void buf_ReleaseDbg(cm_buf_t *bp, char *file, long line)
 void buf_Release(cm_buf_t *bp)
 #endif
 {
-    afs_int32 refCount;
-
-    /* ensure that we're in the LRU queue if our ref count is 0 */
-    osi_assertx(bp->magic == CM_BUF_MAGIC,"incorrect cm_buf_t magic");
-
-    refCount = InterlockedDecrement(&bp->refCount);
-#ifdef DEBUG_REFCOUNT
-    osi_Log2(afsd_logp,"buf_Release bp 0x%p ref %d", bp, refCount);
-    afsi_log("%s:%d buf_ReleaseLocked bp 0x%p, ref %d", file, line, bp, refCount);
-#endif
-#ifdef DEBUG
-    if (refCount < 0)
-       osi_panic("buf refcount 0",__FILE__,__LINE__);;
-#else
-    osi_assertx(refCount >= 0, "cm_buf_t refCount == 0");
-#endif
-    if (refCount == 0) {
-        lock_ObtainWrite(&buf_globalLock);
-        if (bp->refCount == 0 &&
-            !(bp->qFlags & (CM_BUF_QINLRU|CM_BUF_QREDIR))) {
-            osi_QAddH( (osi_queue_t **) &cm_data.buf_freeListp,
-                       (osi_queue_t **) &cm_data.buf_freeListEndp,
-                       &bp->q);
-            _InterlockedOr(&bp->qFlags, CM_BUF_QINLRU);
-            buf_IncrementFreeCount();
-        }
-        lock_ReleaseWrite(&buf_globalLock);
-    }
+    lock_ObtainRead(&buf_globalLock);
+    buf_ReleaseLocked(bp, FALSE);
+    lock_ReleaseRead(&buf_globalLock);
 }
 
 long
@@ -543,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;
@@ -555,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++;
             }
@@ -581,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
@@ -841,13 +810,12 @@ 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;
     }
 
     while ((bp->flags & CM_BUF_DIRTY) == CM_BUF_DIRTY) {
@@ -2156,6 +2124,33 @@ long buf_FlushCleanPages(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
 }
 
 /* Must be called with scp->rw held */
+long buf_InvalidateBuffers(cm_scache_t * scp)
+{
+    cm_buf_t * bp;
+    afs_uint32 i;
+    int found = 0;
+
+    lock_AssertAny(&scp->rw);
+
+    i = BUF_FILEHASH(&scp->fid);
+
+    lock_ObtainRead(&buf_globalLock);
+
+    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;
+            found = 1;
+        }
+    }
+    lock_ReleaseRead(&buf_globalLock);
+
+    if (found)
+        return 0;
+    else
+        return ENOENT;
+}
+
+/* Must be called with scp->rw held */
 long buf_ForceDataVersion(cm_scache_t * scp, afs_uint64 fromVersion, afs_uint64 toVersion)
 {
     cm_buf_t * bp;