Windows: add assertions to cm_scache.c
[openafs.git] / src / WINNT / afsd / cm_scache.c
index 530ad89..5719838 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
@@ -9,10 +9,10 @@
 
 #include <afsconfig.h>
 #include <afs/param.h>
-#include <roken.h>
-
 #include <afs/stds.h>
 
+#include <roken.h>
+
 #include <windows.h>
 #include <winsock2.h>
 #include <nb30.h>
@@ -66,7 +66,12 @@ void cm_AdjustScacheLRU(cm_scache_t *scp)
 {
     lock_AssertWrite(&cm_scacheLock);
     osi_QRemoveHT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
-    osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
+    if (scp->flags & CM_SCACHEFLAG_DELETED) {
+        /* Since it has been deleted make it the first to be recycled. */
+        osi_QAddT((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
+    } else {
+        osi_QAddH((osi_queue_t **) &cm_data.scacheLRUFirstp, (osi_queue_t **) &cm_data.scacheLRULastp, &scp->q);
+    }
 }
 
 /* call with cm_scacheLock write-locked and scp rw held */
@@ -75,7 +80,7 @@ void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
     cm_scache_t **lscpp;
     cm_scache_t *tscp;
     int i;
-       
+
     lock_AssertWrite(&cm_scacheLock);
     lock_AssertWrite(&scp->rw);
     if (scp->flags & CM_SCACHEFLAG_INHASH) {
@@ -87,7 +92,7 @@ void cm_RemoveSCacheFromHashTable(cm_scache_t *scp)
            if (tscp == scp) {
                *lscpp = scp->nextp;
                 scp->nextp = NULL;
-               scp->flags &= ~CM_SCACHEFLAG_INHASH;
+               _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_INHASH);
                break;
            }
        }
@@ -103,13 +108,13 @@ void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
         LARGE_INTEGER start, end;
 
         if (!dirlock && !lock_TryWrite(&scp->dirlock)) {
-            /* 
+            /*
              * We are not holding the dirlock and obtaining it
              * requires that we drop the scp->rw.  As a result
-             * we will leave the dirBplus tree intact but 
+             * we will leave the dirBplus tree intact but
              * invalidate the version number so that whatever
              * operation is currently active can safely complete
-             * but the contents will be ignored on the next 
+             * but the contents will be ignored on the next
              * directory operation.
              */
             scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
@@ -122,8 +127,8 @@ void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
         scp->dirBplus = NULL;
         scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
         QueryPerformanceCounter(&end);
-        
-        if (!dirlock) 
+
+        if (!dirlock)
             lock_ReleaseWrite(&scp->dirlock);
 
         bplus_free_time += (end.QuadPart - start.QuadPart);
@@ -134,6 +139,9 @@ void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 dirlock)
 /* called with cm_scacheLock and scp write-locked; recycles an existing scp. */
 long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
 {
+    lock_AssertWrite(&cm_scacheLock);
+    lock_AssertWrite(&scp->rw);
+
     if (scp->refCount != 0) {
        return -1;
     }
@@ -159,16 +167,16 @@ long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
            osi_QDFree(qdp);
            if (bufp) {
                lock_ObtainMutex(&bufp->mx);
-               bufp->cmFlags &= ~CM_BUF_CMSTORING;
-               bufp->flags &= ~CM_BUF_DIRTY;
+               _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMSTORING);
+               _InterlockedAnd(&bufp->flags, ~CM_BUF_DIRTY);
                 bufp->dirty_offset = 0;
                 bufp->dirty_length = 0;
-               bufp->flags |= CM_BUF_ERROR;
+               _InterlockedOr(&bufp->flags, CM_BUF_ERROR);
                bufp->error = VNOVNODE;
                bufp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
                bufp->dirtyCounter++;
                if (bufp->flags & CM_BUF_WAITING) {
-                   osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
+                   osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%p] bufp 0x%x", scp, bufp);
                    osi_Wakeup((long) &bufp);
                }
                lock_ReleaseMutex(&bufp->mx);
@@ -181,23 +189,23 @@ long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
            osi_QDFree(qdp);
            if (bufp) {
                lock_ObtainMutex(&bufp->mx);
-               bufp->cmFlags &= ~CM_BUF_CMFETCHING;
-               bufp->flags &= ~CM_BUF_DIRTY;
+               _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMFETCHING);
+               _InterlockedAnd(&bufp->flags, ~CM_BUF_DIRTY);
                 bufp->dirty_offset = 0;
                 bufp->dirty_length = 0;
-               bufp->flags |= CM_BUF_ERROR;
+               _InterlockedOr(&bufp->flags, CM_BUF_ERROR);
                bufp->error = VNOVNODE;
                bufp->dataVersion = CM_BUF_VERSION_BAD; /* bad */
                bufp->dirtyCounter++;
                if (bufp->flags & CM_BUF_WAITING) {
-                   osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%x] bufp 0x%x", scp, bufp);
+                   osi_Log2(afsd_logp, "CM RecycleSCache Waking [scp 0x%p] bufp 0x%x", scp, bufp);
                    osi_Wakeup((long) &bufp);
                }
                lock_ReleaseMutex(&bufp->mx);
                buf_Release(bufp);
            }
         }
-       buf_CleanDirtyBuffers(scp); 
+       buf_CleanDirtyBuffers(scp);
     } else {
        /* look for things that shouldn't still be set */
        osi_assertx(scp->bufWritesp == NULL, "non-null cm_scache_t bufWritesp");
@@ -208,13 +216,14 @@ long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
     /* invalidate so next merge works fine;
      * also initialize some flags */
     scp->fileType = 0;
-    scp->flags &= ~(CM_SCACHEFLAG_STATD
+    _InterlockedAnd(&scp->flags,
+                    ~(CM_SCACHEFLAG_STATD
                     | CM_SCACHEFLAG_DELETED
                     | CM_SCACHEFLAG_RO
                     | CM_SCACHEFLAG_PURERO
                     | CM_SCACHEFLAG_OVERQUOTA
                     | CM_SCACHEFLAG_OUTOFSPACE
-                    | CM_SCACHEFLAG_EACCESS);
+                    | CM_SCACHEFLAG_EACCESS));
     scp->serverModTime = 0;
     scp->dataVersion = CM_SCACHE_VERSION_BAD;
     scp->bufDataVersionLow = CM_SCACHE_VERSION_BAD;
@@ -267,10 +276,10 @@ long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags)
 }
 
 
-/* 
+/*
  * called with cm_scacheLock write-locked; find a vnode to recycle.
  * Can allocate a new one if desperate, or if below quota (cm_data.maxSCaches).
- * returns scp->mx held.
+ * returns scp->rw write-locked.
  */
 cm_scache_t *cm_GetNewSCache(void)
 {
@@ -282,7 +291,7 @@ cm_scache_t *cm_GetNewSCache(void)
     /* first pass - look for deleted objects */
     for ( scp = cm_data.scacheLRULastp;
          scp;
-         scp = (cm_scache_t *) osi_QPrev(&scp->q)) 
+         scp = (cm_scache_t *) osi_QPrev(&scp->q))
     {
        osi_assertx(scp >= cm_data.scacheBaseAddress && scp < (cm_scache_t *)cm_data.scacheHashTablep,
                     "invalid cm_scache_t address");
@@ -292,7 +301,7 @@ cm_scache_t *cm_GetNewSCache(void)
                 if (!lock_TryWrite(&scp->rw))
                     continue;
 
-               osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%x", scp);
+               osi_Log1(afsd_logp, "GetNewSCache attempting to recycle deleted scp 0x%p", scp);
                if (!cm_RecycleSCache(scp, CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS)) {
 
                    /* we found an entry, so return it */
@@ -303,9 +312,9 @@ cm_scache_t *cm_GetNewSCache(void)
 
                    /* and we're done */
                    return scp;
-               } 
+               }
                 lock_ReleaseWrite(&scp->rw);
-               osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%x", scp);
+               osi_Log1(afsd_logp, "GetNewSCache recycled failed scp 0x%p", scp);
            } else if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
                 if (!lock_TryWrite(&scp->rw))
                     continue;
@@ -319,10 +328,10 @@ cm_scache_t *cm_GetNewSCache(void)
                /* and we're done */
                return scp;
            }
-       }       
-    }  
+       }
+    }
     osi_Log0(afsd_logp, "GetNewSCache no deleted or recycled entries available for reuse");
-#endif 
+#endif
 
     if (cm_data.currentSCaches >= cm_data.maxSCaches) {
        /* There were no deleted scache objects that we could use.  Try to find
@@ -330,7 +339,7 @@ cm_scache_t *cm_GetNewSCache(void)
         */
         for ( scp = cm_data.scacheLRULastp;
               scp;
-              scp = (cm_scache_t *) osi_QPrev(&scp->q)) 
+              scp = (cm_scache_t *) osi_QPrev(&scp->q))
         {
             /* It is possible for the refCount to be zero and for there still
              * to be outstanding dirty buffers.  If there are dirty buffers,
@@ -354,13 +363,13 @@ cm_scache_t *cm_GetNewSCache(void)
                 } else {
                     osi_Log1(afsd_logp,"GetNewSCache dirty buffers exist scp 0x%x", scp);
                 }
-            }  
+            }
         }
         osi_Log1(afsd_logp, "GetNewSCache all scache entries in use (retry = %d)", retry);
 
         return NULL;
     }
-        
+
     /* if we get here, we should allocate a new scache entry.  We either are below
      * quota or we have a leak and need to allocate a new one to avoid panicing.
      */
@@ -379,15 +388,15 @@ cm_scache_t *cm_GetNewSCache(void)
 
     /* and put it in the LRU queue */
     osi_QAdd((osi_queue_t **) &cm_data.scacheLRUFirstp, &scp->q);
-    if (!cm_data.scacheLRULastp) 
+    if (!cm_data.scacheLRULastp)
         cm_data.scacheLRULastp = scp;
     cm_data.currentSCaches++;
     cm_dnlcPurgedp(scp); /* make doubly sure that this is not in dnlc */
-    cm_dnlcPurgevp(scp); 
+    cm_dnlcPurgevp(scp);
     scp->allNextp = cm_data.allSCachesp;
     cm_data.allSCachesp = scp;
     return scp;
-}       
+}
 
 void cm_SetFid(cm_fid_t *fidp, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique)
 {
@@ -405,11 +414,11 @@ __inline int cm_FidCmp(cm_fid_t *ap, cm_fid_t *bp)
         return 1;
     if (ap->vnode != bp->vnode)
         return 1;
-    if (ap->volume != bp->volume) 
+    if (ap->volume != bp->volume)
         return 1;
-    if (ap->unique != bp->unique) 
+    if (ap->unique != bp->unique)
         return 1;
-    if (ap->cell != bp->cell) 
+    if (ap->cell != bp->cell)
         return 1;
     return 0;
 }
@@ -448,7 +457,7 @@ cm_ValidateSCache(void)
         return -17;
     }
 
-    for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0; 
+    for ( scp = cm_data.scacheLRUFirstp, lscp = NULL, i = 0;
           scp;
           lscp = scp, scp = (cm_scache_t *) osi_QNext(&scp->q), i++ ) {
         if (scp->magic != CM_SCACHE_MAGIC) {
@@ -545,12 +554,12 @@ cm_SuspendSCache(void)
 
     cm_GiveUpAllCallbacksAllServersMulti(TRUE);
 
-    /* 
+    /*
      * After this call all servers are marked down.
      * Do not clear the callbacks, instead change the
      * expiration time so that the callbacks will be expired
      * when the servers are marked back up.  However, we
-     * want the callbacks to be preserved as long as the 
+     * want the callbacks to be preserved as long as the
      * servers are down.  That way if the machine resumes
      * without network, the stat cache item will still be
      * considered valid.
@@ -577,18 +586,24 @@ cm_SuspendSCache(void)
 long
 cm_ShutdownSCache(void)
 {
-    cm_scache_t * scp;
+    cm_scache_t * scp, * nextp;
+
+    cm_GiveUpAllCallbacksAllServersMulti(FALSE);
 
     lock_ObtainWrite(&cm_scacheLock);
 
     for ( scp = cm_data.allSCachesp; scp;
-          scp = scp->allNextp ) {
+          scp = nextp ) {
+        nextp = scp->allNextp;
+        lock_ReleaseWrite(&cm_scacheLock);
+#ifdef USE_BPLUS
+        lock_ObtainWrite(&scp->dirlock);
+#endif
+        lock_ObtainWrite(&scp->rw);
+        lock_ObtainWrite(&cm_scacheLock);
+
         if (scp->randomACLp) {
-            lock_ReleaseWrite(&cm_scacheLock);
-            lock_ObtainWrite(&scp->rw);
-            lock_ObtainWrite(&cm_scacheLock);
             cm_FreeAllACLEnts(scp);
-            lock_ReleaseWrite(&scp->rw);
         }
 
         if (scp->cbServerp) {
@@ -596,13 +611,15 @@ cm_ShutdownSCache(void)
             scp->cbServerp = NULL;
         }
         scp->cbExpires = 0;
-        scp->flags &= ~CM_SCACHEFLAG_CALLBACK;
+        _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_CALLBACK);
+        lock_ReleaseWrite(&scp->rw);
 
 #ifdef USE_BPLUS
         if (scp->dirBplus)
             freeBtree(scp->dirBplus);
         scp->dirBplus = NULL;
         scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
+        lock_ReleaseWrite(&scp->dirlock);
         lock_FinalizeRWLock(&scp->dirlock);
 #endif
         lock_FinalizeRWLock(&scp->rw);
@@ -610,15 +627,13 @@ cm_ShutdownSCache(void)
     }
     lock_ReleaseWrite(&cm_scacheLock);
 
-    cm_GiveUpAllCallbacksAllServersMulti(FALSE);
-
     return cm_dnlcShutdown();
 }
 
 void cm_InitSCache(int newFile, long maxSCaches)
 {
     static osi_once_t once;
-        
+
     if (osi_Once(&once)) {
         lock_InitializeRWLock(&cm_scacheLock, "cm_scacheLock", LOCK_HIERARCHY_SCACHE_GLOBAL);
         if ( newFile ) {
@@ -656,7 +671,7 @@ void cm_InitSCache(int newFile, long maxSCaches)
                 scp->dirDataVersion = CM_SCACHE_VERSION_BAD;
 #endif
                 scp->waitQueueT = NULL;
-                scp->flags &= ~CM_SCACHEFLAG_WAITING;
+                _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
             }
         }
         cm_allFileLocks = NULL;
@@ -711,17 +726,17 @@ long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
     int special = 0; // yj: boolean variable to test if file is on root.afs
     int isRoot = 0;
     extern cm_fid_t cm_rootFid;
-        
+
     hash = CM_SCACHE_HASH(fidp);
-        
+
     if (fidp->cell == 0)
         return CM_ERROR_INVAL;
 
 #ifdef AFS_FREELANCE_CLIENT
-    special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID && 
+    special = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
                fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
                !(fidp->vnode==0x1 && fidp->unique==0x1));
-    isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID && 
+    isRoot = (fidp->cell==AFS_FAKE_ROOT_CELL_ID &&
               fidp->volume==AFS_FAKE_ROOT_VOL_ID &&
               fidp->vnode==0x1 && fidp->unique==0x1);
 #endif
@@ -736,7 +751,7 @@ long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
            osi_Log1(afsd_logp,"cm_GetSCache (1) scp 0x%p", scp);
 #endif
 #ifdef AFS_FREELANCE_CLIENT
-            if (cm_freelanceEnabled && special && 
+            if (cm_freelanceEnabled && special &&
                 cm_data.fakeDirVersion != scp->dataVersion)
                 break;
 #endif
@@ -760,11 +775,11 @@ long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
     if (cm_freelanceEnabled && isRoot) {
         osi_Log0(afsd_logp,"cm_GetSCache Freelance and isRoot");
         /* freelance: if we are trying to get the root scp for the first
-         * time, we will just put in a place holder entry. 
+         * time, we will just put in a place holder entry.
          */
         volp = NULL;
     }
-         
+
     if (cm_freelanceEnabled && special) {
         lock_ReleaseWrite(&cm_scacheLock);
         osi_Log0(afsd_logp,"cm_GetSCache Freelance and special");
@@ -792,19 +807,19 @@ long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
         scp->dotdotFid.volume=AFS_FAKE_ROOT_VOL_ID;
         scp->dotdotFid.unique=1;
         scp->dotdotFid.vnode=1;
-        scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
+        _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
         if (!(scp->flags & CM_SCACHEFLAG_INHASH)) {
             scp->nextp = cm_data.scacheHashTablep[hash];
             cm_data.scacheHashTablep[hash] = scp;
-            scp->flags |= CM_SCACHEFLAG_INHASH;
+            _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
         }
         scp->refCount = 1;
-       osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%x", scp);
+       osi_Log1(afsd_logp,"cm_GetSCache (freelance) sets refCount to 1 scp 0x%p", scp);
 
         /* must be called after the scp->fid is set */
         cm_FreelanceFetchMountPointString(scp);
         cm_FreelanceFetchFileType(scp);
-        
+
         scp->length.LowPart = (DWORD)strlen(scp->mountPointStringp)+4;
         scp->length.HighPart = 0;
         scp->owner=0x0;
@@ -834,15 +849,15 @@ long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
     if (!cm_freelanceEnabled || !isRoot) {
         lock_ReleaseWrite(&cm_scacheLock);     /* for perf. reasons */
         cellp = cm_FindCellByID(fidp->cell, 0);
-        if (!cellp) 
+        if (!cellp)
             return CM_ERROR_NOSUCHCELL;
 
         code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
-        if (code) 
+        if (code)
             return code;
         lock_ObtainWrite(&cm_scacheLock);
     }
-        
+
     /* otherwise, we have the volume, now reverify that the scp doesn't
      * exist, and proceed.
      */
@@ -861,7 +876,7 @@ long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
             return 0;
         }
     }
-        
+
     /* now, if we don't have the fid, recycle something */
     scp = cm_GetNewSCache();    /* returns scp->rw held */
     if (scp == NULL) {
@@ -880,16 +895,16 @@ long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
 
     scp->fid = *fidp;
     if (!cm_freelanceEnabled || !isRoot) {
-        /* if this scache entry represents a volume root then we need 
-         * to copy the dotdotFipd from the volume structure where the 
+        /* if this scache entry represents a volume root then we need
+         * to copy the dotdotFipd from the volume structure where the
          * "master" copy is stored (defect 11489)
          */
         if (volp->vol[ROVOL].ID == fidp->volume) {
-           scp->flags |= (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO);
+           _InterlockedOr(&scp->flags, (CM_SCACHEFLAG_PURERO | CM_SCACHEFLAG_RO));
             if (scp->fid.vnode == 1 && scp->fid.unique == 1)
                 scp->dotdotFid = cm_VolumeStateByType(volp, ROVOL)->dotdotFid;
         } else if (volp->vol[BACKVOL].ID == fidp->volume) {
-           scp->flags |= CM_SCACHEFLAG_RO;
+           _InterlockedOr(&scp->flags, CM_SCACHEFLAG_RO);
             if (scp->fid.vnode == 1 && scp->fid.unique == 1)
                 scp->dotdotFid = cm_VolumeStateByType(volp, BACKVOL)->dotdotFid;
         } else {
@@ -901,21 +916,21 @@ long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
         cm_PutVolume(volp);
     scp->nextp = cm_data.scacheHashTablep[hash];
     cm_data.scacheHashTablep[hash] = scp;
-    scp->flags |= CM_SCACHEFLAG_INHASH;
+    _InterlockedOr(&scp->flags, CM_SCACHEFLAG_INHASH);
     lock_ReleaseWrite(&scp->rw);
     scp->refCount = 1;
 #ifdef DEBUG_REFCOUNT
-    afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%x", file, line, scp);
+    afsi_log("%s:%d cm_GetSCache sets refCount to 1 scp 0x%p", file, line, scp);
 #endif
-    osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%x", scp);
+    osi_Log1(afsd_logp,"cm_GetSCache sets refCount to 1 scp 0x%p", scp);
 
-    /* XXX - The following fields in the cm_scache are 
+    /* XXX - The following fields in the cm_scache are
      * uninitialized:
      *   fileType
      *   parentVnode
      *   parentUnique
      */
-        
+
     /* now we have a held scache entry; just return it */
     *outScpp = scp;
 #ifdef DEBUG_REFCOUNT
@@ -926,7 +941,7 @@ long cm_GetSCache(cm_fid_t *fidp, cm_scache_t **outScpp, cm_user_t *userp,
     return 0;
 }
 
-/* Returns a held reference to the scache's parent 
+/* Returns a held reference to the scache's parent
  * if it exists */
 cm_scache_t * cm_FindSCacheParent(cm_scache_t * scp)
 {
@@ -1019,7 +1034,7 @@ int cm_SyncOpCheckContinue(cm_scache_t * scp, afs_int32 flags, cm_buf_t * bufp)
 /* synchronize a fetch, store, read, write, fetch status or store status.
  * Called with scache mutex held, and returns with it held, but temporarily
  * drops it during the fetch.
- * 
+ *
  * At most one flag can be on in flags, if this is an RPC request.
  *
  * Also, if we're fetching or storing data, we must ensure that we have a buffer.
@@ -1256,7 +1271,7 @@ long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *req
             if ((flags & CM_SCACHESYNC_FORCECB) || !cm_HaveCallback(scp)) {
                 osi_Log1(afsd_logp, "CM SyncOp getting callback on scp 0x%p",
                           scp);
-                if (bufLocked) 
+                if (bufLocked)
                    lock_ReleaseMutex(&bufp->mx);
                 code = cm_GetCallback(scp, userp, reqp, (flags & CM_SCACHESYNC_FORCECB)?1:0);
                 if (bufLocked) {
@@ -1264,7 +1279,7 @@ long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *req
                     lock_ObtainMutex(&bufp->mx);
                     lock_ObtainWrite(&scp->rw);
                 }
-                if (code) 
+                if (code)
                     return code;
                flags &= ~CM_SCACHESYNC_FORCECB;        /* only force once */
                 continue;
@@ -1279,7 +1294,7 @@ long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *req
                 return CM_ERROR_READONLY;
 
             if (cm_HaveAccessRights(scp, userp, rights, &outRights)) {
-                if (~outRights & rights) 
+                if (~outRights & rights)
                    return CM_ERROR_NOACCESS;
             }
             else {
@@ -1291,7 +1306,7 @@ long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *req
                     lock_ObtainMutex(&bufp->mx);
                     lock_ObtainWrite(&scp->rw);
                 }
-                if (code) 
+                if (code)
                     return code;
                 continue;
             }
@@ -1301,10 +1316,10 @@ long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *req
         break;
 
       sleep:
-        /* first check if we're not supposed to wait: fail 
+        /* first check if we're not supposed to wait: fail
          * in this case, returning with everything still locked.
          */
-        if (flags & CM_SCACHESYNC_NOWAIT) 
+        if (flags & CM_SCACHESYNC_NOWAIT)
             return CM_ERROR_WOULDBLOCK;
 
         /* These are used for minidump debugging */
@@ -1317,21 +1332,21 @@ long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *req
         if ( scp->flags & CM_SCACHEFLAG_WAITING ) {
             scp->waitCount++;
             scp->waitRequests++;
-            osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests", 
+            osi_Log3(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING already set for 0x%p; %d threads; %d requests",
                      scp, scp->waitCount, scp->waitRequests);
         } else {
             osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING set for 0x%p", scp);
-            scp->flags |= CM_SCACHEFLAG_WAITING;
+            _InterlockedOr(&scp->flags, CM_SCACHEFLAG_WAITING);
             scp->waitCount = scp->waitRequests = 1;
         }
 
         cm_SyncOpAddToWaitQueue(scp, flags, bufp);
         wakeupCycle = 0;
         do {
-            if (bufLocked) 
+            if (bufLocked)
                 lock_ReleaseMutex(&bufp->mx);
             osi_SleepW((LONG_PTR) &scp->flags, &scp->rw);
-            if (bufLocked) 
+            if (bufLocked)
                 lock_ObtainMutex(&bufp->mx);
             lock_ObtainWrite(&scp->rw);
         } while (!cm_SyncOpCheckContinue(scp, flags, bufp));
@@ -1339,32 +1354,32 @@ long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *req
        cm_UpdateServerPriority();
 
         scp->waitCount--;
-        osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests", 
+        osi_Log3(afsd_logp, "CM SyncOp woke! scp 0x%p; still waiting %d threads of %d requests",
                  scp, scp->waitCount, scp->waitRequests);
         if (scp->waitCount == 0) {
             osi_Log1(afsd_logp, "CM SyncOp CM_SCACHEFLAG_WAITING reset for 0x%p", scp);
-            scp->flags &= ~CM_SCACHEFLAG_WAITING;
+            _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_WAITING);
             scp->waitRequests = 0;
         }
     } /* big while loop */
-        
+
     /* now, update the recorded state for RPC-type calls */
     if (flags & CM_SCACHESYNC_FETCHSTATUS)
-        scp->flags |= CM_SCACHEFLAG_FETCHING;
+        _InterlockedOr(&scp->flags, CM_SCACHEFLAG_FETCHING);
     if (flags & CM_SCACHESYNC_STORESTATUS)
-        scp->flags |= CM_SCACHEFLAG_STORING;
+        _InterlockedOr(&scp->flags, CM_SCACHEFLAG_STORING);
     if (flags & CM_SCACHESYNC_SETSIZE)
-        scp->flags |= CM_SCACHEFLAG_SIZESETTING;
+        _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESETTING);
     if (flags & CM_SCACHESYNC_STORESIZE)
-        scp->flags |= CM_SCACHEFLAG_SIZESTORING;
+        _InterlockedOr(&scp->flags, CM_SCACHEFLAG_SIZESTORING);
     if (flags & CM_SCACHESYNC_GETCALLBACK)
-        scp->flags |= CM_SCACHEFLAG_GETCALLBACK;
+        _InterlockedOr(&scp->flags, CM_SCACHEFLAG_GETCALLBACK);
     if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
-        scp->flags |= CM_SCACHEFLAG_DATASTORING;
+        _InterlockedOr(&scp->flags, CM_SCACHEFLAG_DATASTORING);
     if (flags & CM_SCACHESYNC_ASYNCSTORE)
-        scp->flags |= CM_SCACHEFLAG_ASYNCSTORING;
+        _InterlockedOr(&scp->flags, CM_SCACHEFLAG_ASYNCSTORING);
     if (flags & CM_SCACHESYNC_LOCK)
-        scp->flags |= CM_SCACHEFLAG_LOCKING;
+        _InterlockedOr(&scp->flags, CM_SCACHEFLAG_LOCKING);
 
     /* now update the buffer pointer */
     if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
@@ -1379,7 +1394,7 @@ long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *req
         osi_SetQData(qdp, bufp);
 
         buf_Hold(bufp);
-        bufp->cmFlags |= CM_BUF_CMFETCHING;
+        _InterlockedOr(&bufp->cmFlags, CM_BUF_CMFETCHING);
         osi_QAdd((osi_queue_t **) &scp->bufReadsp, &qdp->q);
     }
 
@@ -1397,13 +1412,13 @@ long cm_SyncOp(cm_scache_t *scp, cm_buf_t *bufp, cm_user_t *userp, cm_req_t *req
         qdp = osi_QDAlloc();
         osi_SetQData(qdp, bufp);
         buf_Hold(bufp);
-        bufp->cmFlags |= CM_BUF_CMSTORING;
+        _InterlockedOr(&bufp->cmFlags, CM_BUF_CMSTORING);
         osi_QAdd((osi_queue_t **) &scp->bufWritesp, &qdp->q);
     }
 
     if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
         /* mark the buffer as being written to. */
-        bufp->cmFlags |= CM_BUF_CMWRITING;
+        _InterlockedOr(&bufp->cmFlags, CM_BUF_CMWRITING);
     }
 
     return 0;
@@ -1421,21 +1436,21 @@ void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
 
     /* now, update the recorded state for RPC-type calls */
     if (flags & CM_SCACHESYNC_FETCHSTATUS)
-        scp->flags &= ~CM_SCACHEFLAG_FETCHING;
+        _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_FETCHING);
     if (flags & CM_SCACHESYNC_STORESTATUS)
-        scp->flags &= ~CM_SCACHEFLAG_STORING;
+        _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_STORING);
     if (flags & CM_SCACHESYNC_SETSIZE)
-        scp->flags &= ~CM_SCACHEFLAG_SIZESETTING;
+        _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESETTING);
     if (flags & CM_SCACHESYNC_STORESIZE)
-        scp->flags &= ~CM_SCACHEFLAG_SIZESTORING;
+        _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_SIZESTORING);
     if (flags & CM_SCACHESYNC_GETCALLBACK)
-        scp->flags &= ~CM_SCACHEFLAG_GETCALLBACK;
+        _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_GETCALLBACK);
     if (flags & CM_SCACHESYNC_STOREDATA_EXCL)
-        scp->flags &= ~CM_SCACHEFLAG_DATASTORING;
+        _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_DATASTORING);
     if (flags & CM_SCACHESYNC_ASYNCSTORE)
-        scp->flags &= ~CM_SCACHEFLAG_ASYNCSTORING;
+        _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_ASYNCSTORING);
     if (flags & CM_SCACHESYNC_LOCK)
-        scp->flags &= ~CM_SCACHEFLAG_LOCKING;
+        _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_LOCKING);
 
     /* now update the buffer pointer */
     if (bufp && (flags & CM_SCACHESYNC_FETCHDATA)) {
@@ -1444,7 +1459,7 @@ void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
        /* ensure that the buffer is in the I/O list */
         for (qdp = scp->bufReadsp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
             tbufp = osi_GetQData(qdp);
-            if (tbufp == bufp) 
+            if (tbufp == bufp)
                break;
         }
        if (qdp) {
@@ -1452,7 +1467,7 @@ void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
            osi_QDFree(qdp);
            release = 1;
        }
-        bufp->cmFlags &= ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED);
+        _InterlockedAnd(&bufp->cmFlags, ~(CM_BUF_CMFETCHING | CM_BUF_CMFULLYFETCHED));
         if (bufp->flags & CM_BUF_WAITING) {
             osi_Log2(afsd_logp, "CM SyncOpDone FetchData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
             osi_Wakeup((LONG_PTR) &bufp);
@@ -1467,7 +1482,7 @@ void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
         /* ensure that the buffer is in the I/O list */
         for (qdp = scp->bufWritesp; qdp; qdp = (osi_queueData_t *) osi_QNext(&qdp->q)) {
             tbufp = osi_GetQData(qdp);
-            if (tbufp == bufp) 
+            if (tbufp == bufp)
                break;
         }
        if (qdp) {
@@ -1475,7 +1490,7 @@ void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
            osi_QDFree(qdp);
            release = 1;
        }
-        bufp->cmFlags &= ~CM_BUF_CMSTORING;
+        _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMSTORING);
         if (bufp->flags & CM_BUF_WAITING) {
             osi_Log2(afsd_logp, "CM SyncOpDone StoreData Waking [scp 0x%p] bufp 0x%p", scp, bufp);
             osi_Wakeup((LONG_PTR) &bufp);
@@ -1486,7 +1501,7 @@ void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
 
     if (bufp && (flags & CM_SCACHESYNC_WRITE)) {
         osi_assertx(bufp->cmFlags & CM_BUF_CMWRITING, "!CM_BUF_CMWRITING");
-        bufp->cmFlags &= ~CM_BUF_CMWRITING;
+        _InterlockedAnd(&bufp->cmFlags, ~CM_BUF_CMWRITING);
     }
 
     /* and wakeup anyone who is waiting */
@@ -1494,7 +1509,7 @@ void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
         osi_Log3(afsd_logp, "CM SyncOpDone 0x%x Waking scp 0x%p bufp 0x%p", flags, scp, bufp);
         osi_Wakeup((LONG_PTR) &scp->flags);
     }
-}       
+}
 
 /* merge in a response from an RPC.  The scp must be locked, and the callback
  * is optional.
@@ -1509,8 +1524,8 @@ void cm_SyncOpDone(cm_scache_t *scp, cm_buf_t *bufp, afs_uint32 flags)
  * handled after the callback breaking is done, but only one of whose calls
  * started before that, can cause old info to be merged from the first call.
  */
-void cm_MergeStatus(cm_scache_t *dscp, 
-                   cm_scache_t *scp, AFSFetchStatus *statusp, 
+void cm_MergeStatus(cm_scache_t *dscp,
+                   cm_scache_t *scp, AFSFetchStatus *statusp,
                    AFSVolSync *volsyncp,
                     cm_user_t *userp, cm_req_t *reqp, afs_uint32 flags)
 {
@@ -1518,6 +1533,8 @@ void cm_MergeStatus(cm_scache_t *dscp,
     struct cm_volume *volp = NULL;
     struct cm_cell *cellp = NULL;
 
+    lock_AssertWrite(&scp->rw);
+
     // yj: i want to create some fake status for the /afs directory and the
     // entries under that directory
 #ifdef AFS_FREELANCE_CLIENT
@@ -1554,9 +1571,9 @@ void cm_MergeStatus(cm_scache_t *dscp,
     }
 #endif /* AFS_FREELANCE_CLIENT */
 
-    if (statusp->errorCode != 0) {     
-       scp->flags |= CM_SCACHEFLAG_EACCESS;
-       osi_Log2(afsd_logp, "Merge, Failure scp %x code 0x%x", scp, statusp->errorCode);
+    if (statusp->errorCode != 0) {
+        _InterlockedOr(&scp->flags, CM_SCACHEFLAG_EACCESS);
+       osi_Log2(afsd_logp, "Merge, Failure scp 0x%p code 0x%x", scp, statusp->errorCode);
 
        scp->fileType = 0;      /* unknown */
 
@@ -1584,14 +1601,14 @@ void cm_MergeStatus(cm_scache_t *dscp,
        }
        goto done;
     } else {
-       scp->flags &= ~CM_SCACHEFLAG_EACCESS;
+       _InterlockedAnd(&scp->flags, ~CM_SCACHEFLAG_EACCESS);
     }
 
     dataVersion = statusp->dataVersionHigh;
     dataVersion <<= 32;
     dataVersion |= statusp->DataVersion;
 
-    if (!(flags & CM_MERGEFLAG_FORCE) && 
+    if (!(flags & CM_MERGEFLAG_FORCE) &&
         dataVersion < scp->dataVersion &&
         scp->dataVersion != CM_SCACHE_VERSION_BAD) {
 
@@ -1603,7 +1620,7 @@ void cm_MergeStatus(cm_scache_t *dscp,
                       scp->cbServerp->addr.sin_addr.s_addr,
                       volp ? volp->namep : "(unknown)");
         }
-        osi_Log3(afsd_logp, "Bad merge, scp %x, scp dv %d, RPC dv %d",
+        osi_Log3(afsd_logp, "Bad merge, scp 0x%p, scp dv %d, RPC dv %d",
                   scp, scp->dataVersion, dataVersion);
         /* we have a number of data fetch/store operations running
          * concurrently, and we can tell which one executed last at the
@@ -1634,7 +1651,7 @@ void cm_MergeStatus(cm_scache_t *dscp,
          */
         if (!(scp->flags & CM_SCACHEFLAG_RO))
             goto done;
-    }       
+    }
 
     if (cm_readonlyVolumeVersioning)
         scp->volumeCreationDate = volsyncp->spare1;       /* volume creation date */
@@ -1666,9 +1683,9 @@ void cm_MergeStatus(cm_scache_t *dscp,
             scp->fileType = CM_SCACHETYPE_MOUNTPOINT;
         else
             scp->fileType = CM_SCACHETYPE_SYMLINK;
-    }       
+    }
     else {
-        osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp %x", statusp->FileType, scp);
+        osi_Log2(afsd_logp, "Merge, Invalid File Type (%d), scp 0x%p", statusp->FileType, scp);
         scp->fileType = CM_SCACHETYPE_INVALID; /* invalid */
     }
     /* and other stuff */
@@ -1688,7 +1705,7 @@ void cm_MergeStatus(cm_scache_t *dscp,
     if (scp->dataVersion != 0 &&
         (!(flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion != scp->dataVersion ||
          (flags & (CM_MERGEFLAG_DIROP|CM_MERGEFLAG_STOREDATA)) && dataVersion - scp->dataVersion > 1)) {
-        /* 
+        /*
          * We now know that all of the data buffers that we have associated
          * with this scp are invalid.  Subsequent operations will go faster
          * if the buffers are removed from the hash tables.
@@ -1708,7 +1725,7 @@ void cm_MergeStatus(cm_scache_t *dscp,
                for (bp = cm_data.buf_fileHashTablepp[i]; bp; bp=nextBp)
        {
             nextBp = bp->fileHashp;
-            /* 
+            /*
              * if the buffer belongs to this stat cache entry
              * and the buffer mutex can be obtained, check the
              * reference count and if it is zero, remove the buffer
@@ -1718,7 +1735,7 @@ void cm_MergeStatus(cm_scache_t *dscp,
              */
             if (cm_FidCmp(&scp->fid, &bp->fid) == 0 &&
                  lock_TryMutex(&bp->mx)) {
-                if (bp->refCount == 0 && 
+                if (bp->refCount == 0 &&
                     !(bp->flags & CM_BUF_READING | CM_BUF_WRITING | CM_BUF_DIRTY)) {
                     prevBp = bp->fileHashBackp;
                     bp->fileHashBackp = bp->fileHashp = NULL;
@@ -1732,14 +1749,14 @@ void cm_MergeStatus(cm_scache_t *dscp,
                     j = BUF_HASH(&bp->fid, &bp->offset);
                     lbpp = &(cm_data.buf_scacheHashTablepp[j]);
                     for(tbp = *lbpp; tbp; lbpp = &tbp->hashp, tbp = *lbpp) {
-                        if (tbp == bp) 
+                        if (tbp == bp)
                             break;
                     }
 
                     *lbpp = bp->hashp; /* hash out */
                     bp->hashp = NULL;
 
-                    bp->qFlags &= ~CM_BUF_QINHASH;
+                    _InterlockedAnd(&bp->qFlags, ~CM_BUF_QINHASH);
                 }
                 lock_ReleaseMutex(&bp->mx);
             }
@@ -1756,22 +1773,22 @@ void cm_MergeStatus(cm_scache_t *dscp,
     if (scp->dataVersion != dataVersion && !(flags & CM_MERGEFLAG_FETCHDATA))
         scp->mountPointStringp[0] = '\0';
 
-    /* We maintain a range of buffer dataVersion values which are considered 
+    /* We maintain a range of buffer dataVersion values which are considered
      * valid.  This avoids the need to update the dataVersion on each buffer
-     * object during an uncontested storeData operation.  As a result this 
+     * object during an uncontested storeData operation.  As a result this
      * merge status no longer has performance characteristics derived from
      * the size of the file.
      */
-    if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > 1) || 
+    if (((flags & CM_MERGEFLAG_STOREDATA) && dataVersion - scp->dataVersion > 1) ||
          (!(flags & CM_MERGEFLAG_STOREDATA) && scp->dataVersion != dataVersion) ||
          scp->bufDataVersionLow == 0)
         scp->bufDataVersionLow = dataVersion;
-    
+
     scp->dataVersion = dataVersion;
 
-    /* 
+    /*
      * If someone is waiting for status information, we can wake them up
-     * now even though the entity that issued the FetchStatus may not 
+     * now even though the entity that issued the FetchStatus may not
      * have completed yet.
      */
     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS);
@@ -1817,7 +1834,7 @@ void cm_DiscardSCache(cm_scache_t *scp)
     }
     scp->cbExpires = 0;
     scp->volumeCreationDate = 0;
-    scp->flags &= ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_LOCAL);
+    _InterlockedAnd(&scp->flags, ~(CM_SCACHEFLAG_CALLBACK | CM_SCACHEFLAG_LOCAL));
     cm_dnlcPurgedp(scp);
     cm_dnlcPurgevp(scp);
     cm_FreeAllACLEnts(scp);
@@ -1834,14 +1851,14 @@ void cm_AFSFidFromFid(AFSFid *afsFidp, cm_fid_t *fidp)
     afsFidp->Volume = fidp->volume;
     afsFidp->Vnode = fidp->vnode;
     afsFidp->Unique = fidp->unique;
-}       
+}
 
 #ifdef DEBUG_REFCOUNT
 void cm_HoldSCacheNoLockDbg(cm_scache_t *scp, char * file, long line)
 #else
 void cm_HoldSCacheNoLock(cm_scache_t *scp)
 #endif
-{     
+{
     afs_int32 refCount;
 
     osi_assertx(scp != NULL, "null cm_scache_t");
@@ -1898,7 +1915,7 @@ void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
         long      lockstate;
 
         lockstate = lock_GetRWLockState(&cm_scacheLock);
-        if (lockstate != OSI_RWLOCK_WRITEHELD) 
+        if (lockstate != OSI_RWLOCK_WRITEHELD)
             lock_ReleaseRead(&cm_scacheLock);
         else
             lock_ReleaseWrite(&cm_scacheLock);
@@ -1910,10 +1927,10 @@ void cm_ReleaseSCacheNoLock(cm_scache_t *scp)
         if (refCount == 0 && deleted) {
             lock_ObtainWrite(&cm_scacheLock);
             cm_RecycleSCache(scp, 0);
-            if (lockstate != OSI_RWLOCK_WRITEHELD) 
+            if (lockstate != OSI_RWLOCK_WRITEHELD)
                 lock_ConvertWToR(&cm_scacheLock);
         } else {
-            if (lockstate != OSI_RWLOCK_WRITEHELD) 
+            if (lockstate != OSI_RWLOCK_WRITEHELD)
                 lock_ObtainRead(&cm_scacheLock);
             else
                 lock_ObtainWrite(&cm_scacheLock);
@@ -1927,7 +1944,7 @@ void cm_ReleaseSCacheDbg(cm_scache_t *scp, char * file, long line)
 #else
 void cm_ReleaseSCache(cm_scache_t *scp)
 #endif
-{     
+{
     afs_int32 refCount;
 
     osi_assertx(scp != NULL, "null cm_scache_t");
@@ -1964,9 +1981,9 @@ int cm_FindFileType(cm_fid_t *fidp)
 {
     long hash;
     cm_scache_t *scp;
-        
+
     hash = CM_SCACHE_HASH(fidp);
-        
+
     osi_assertx(fidp->cell != 0, "unassigned cell value");
 
     lock_ObtainWrite(&cm_scacheLock);
@@ -1980,9 +1997,9 @@ int cm_FindFileType(cm_fid_t *fidp)
     return 0;
 }
 
-/* dump all scp's that have reference count > 0 to a file. 
- * cookie is used to identify this batch for easy parsing, 
- * and it a string provided by a caller 
+/* dump all scp's that have reference count > 0 to a file.
+ * cookie is used to identify this batch for easy parsing,
+ * and it a string provided by a caller
  */
 int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
 {
@@ -1991,14 +2008,14 @@ int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
     osi_queue_t *q;
     char output[2048];
     int i;
-  
+
     if (lock)
         lock_ObtainRead(&cm_scacheLock);
-  
+
     sprintf(output, "%s - dumping all scache - cm_data.currentSCaches=%d, cm_data.maxSCaches=%d\r\n", cookie, cm_data.currentSCaches, cm_data.maxSCaches);
     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
-  
-    for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp) 
+
+    for (scp = cm_data.allSCachesp; scp; scp = scp->allNextp)
     {
         time_t t;
         char *srvStr = NULL;
@@ -2053,7 +2070,7 @@ int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
                         cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
                         lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
                 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
-            }       
+            }
 
             sprintf(output, "  %s - done dumping scp locks\r\n", cookie);
             WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
@@ -2070,18 +2087,18 @@ int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
         if (cdrot)
             free(cdrot);
     }
-  
+
     sprintf(output, "%s - Done dumping all scache.\r\n", cookie);
     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
     sprintf(output, "%s - dumping cm_data.scacheHashTable - cm_data.scacheHashTableSize=%d\r\n",
             cookie, cm_data.scacheHashTableSize);
     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
-  
+
     for (i = 0; i < cm_data.scacheHashTableSize; i++)
     {
-        for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp) 
+        for(scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp)
         {
-            sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n", 
+            sprintf(output, "%s scp=0x%p, hash=%d, fid (cell=%d, volume=%d, vnode=%d, unique=%d)\r\n",
                     cookie, scp, i, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
             WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
         }
@@ -2095,17 +2112,17 @@ int cm_DumpSCache(FILE *outputFile, char *cookie, int lock)
 
     for (q = cm_allFileLocks; q; q = osi_QNext(q)) {
         cm_file_lock_t * lockp = (cm_file_lock_t *)q;
-        sprintf(output, "%s filelockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x key=0x%I64x flags=0x%x update=0x%I64u\r\n", 
-                 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length, 
+        sprintf(output, "%s filelockp=0x%p scp=0x%p, cm_userp=0x%p offset=0x%I64x len=0x%08I64x type=0x%x key=0x%I64x flags=0x%x update=0x%I64u\r\n",
+                 cookie, lockp, lockp->scp, lockp->userp, lockp->range.offset, lockp->range.length,
                  lockp->lockType, lockp->key, lockp->flags, (afs_uint64)lockp->lastUpdate);
         WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
-    }       
+    }
 
     sprintf(output, "%s - done dumping all file locks\r\n", cookie);
     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
 
     if (lock)
-        lock_ReleaseRead(&cm_scacheLock);       
-    return (0);     
+        lock_ReleaseRead(&cm_scacheLock);
+    return (0);
 }