Windows: optimize InitCallback3 processing
[openafs.git] / src / WINNT / afsd / cm_callback.c
index df53cfd..5c326c4 100644 (file)
@@ -1,13 +1,16 @@
 /*
  * 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
  */
 
+#include <afsconfig.h>
 #include <afs/param.h>
+#include <roken.h>
+
 #include <afs/afs_args.h>
 #include <afs/stds.h>
 
 #include <stdlib.h>
 
 #include "afsd.h"
+#include "smb.h"
 #include <osi.h>
 #include <rx_pthread.h>
 
 #include <WINNT/syscfg.h>
 #include <WINNT/afsreg.h>
-#include <../afsrdr/kif.h>
 
-/*extern void afsi_log(char *pattern, ...);*/
+int
+SRXAFSCB_InitCallBackState3(struct rx_call *callp, afsUUID* serverUuid);
 
 /* read/write lock for all global storage in this module */
 osi_rwlock_t cm_callbackLock;
 
+afs_int32 cm_OfflineROIsValid = 0;
+
+afs_int32 cm_giveUpAllCBs = 1;
+
+afs_int32 cm_shutdown = 0;
+
 #ifdef AFS_FREELANCE_CLIENT
 extern osi_mutex_t cm_Freelance_Lock;
 #endif
@@ -99,7 +109,7 @@ void cm_CallbackNotifyChange(cm_scache_t *scp)
     DWORD dummyLen;
 
     /* why does this have to query the registry each time? */
-       if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
+       if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                       AFSREG_CLT_OPENAFS_SUBKEY,
                       0,
                       KEY_READ|KEY_QUERY_VALUE,
@@ -112,7 +122,7 @@ void cm_CallbackNotifyChange(cm_scache_t *scp)
     }
 
     if (dwDelay > 5000)    /* do not allow a delay of more then 5 seconds */
-        dwDelay = 5000;   
+        dwDelay = 5000;
 
     osi_Log3(afsd_logp, "CallbackNotifyChange FileType %d Flags %lX Delay %dms",
               scp->fileType, scp->flags, dwDelay);
@@ -122,35 +132,23 @@ void cm_CallbackNotifyChange(cm_scache_t *scp)
 
     /* for directories, this sends a change notification on the dir itself */
     if (scp->fileType == CM_SCACHETYPE_DIRECTORY) {
-#ifndef AFSIFS
         if (scp->flags & CM_SCACHEFLAG_ANYWATCH)
             smb_NotifyChange(0,
                              FILE_NOTIFY_GENERIC_DIRECTORY_FILTER,
                              scp, NULL, NULL, TRUE);
-#else
-        dc_break_callback(FID_HASH_FN(&scp->fid));
-#endif
     } else {
        /* and for files, this sends a change notification on the file's parent dir */
         cm_fid_t tfid;
         cm_scache_t *dscp;
 
-        tfid.cell = scp->fid.cell;
-        tfid.volume = scp->fid.volume;
-        tfid.vnode = scp->parentVnode;
-        tfid.unique = scp->parentUnique;
+        cm_SetFid(&tfid, scp->fid.cell, scp->fid.volume, scp->parentVnode, scp->parentUnique);
         dscp = cm_FindSCache(&tfid);
-#ifndef AFSIFS
         if ( dscp &&
              dscp->flags & CM_SCACHEFLAG_ANYWATCH )
             smb_NotifyChange( 0,
                               FILE_NOTIFY_GENERIC_FILE_FILTER,
-                              dscp,   NULL, NULL, TRUE);
-#else
+                              dscp, NULL, NULL, TRUE);
         if (dscp)
-            dc_break_callback(FID_HASH_FN(&dscp->fid));
-#endif
-        if (dscp) 
             cm_ReleaseSCache(dscp);
     }
 }
@@ -161,25 +159,22 @@ void cm_CallbackNotifyChange(cm_scache_t *scp)
  *
  * The callp parameter is currently unused.
  */
-void cm_RevokeCallback(struct rx_call *callp, AFSFid *fidp)
+void cm_RevokeCallback(struct rx_call *callp, cm_cell_t * cellp, AFSFid *fidp)
 {
     cm_fid_t tfid;
     cm_scache_t *scp;
     long hash;
-        
-    /* don't bother setting cell, since we won't be checking it (to aid
-     * in working with multi-homed servers: we don't know the cell if we
-     * don't recognize the IP address).
-     */
-    tfid.cell = 0;
+
+    tfid.cell = cellp ? cellp->cellID : 0;
     tfid.volume = fidp->Volume;
     tfid.vnode = fidp->Vnode;
     tfid.unique = fidp->Unique;
+    CM_FID_GEN_HASH(&tfid);
     hash = CM_SCACHE_HASH(&tfid);
 
     osi_Log3(afsd_logp, "RevokeCallback vol %u vn %u uniq %u",
              fidp->Volume, fidp->Vnode, fidp->Unique);
-        
+
     /* do this first, so that if we're executing a callback granting call
      * at this moment, we kill it before it can be merged in.  Otherwise,
      * it could complete while we're doing the scan below, and get missed
@@ -191,21 +186,28 @@ void cm_RevokeCallback(struct rx_call *callp, AFSFid *fidp)
     /* do all in the hash bucket, since we don't know how many we'll find with
      * varying cells.
      */
-    for (scp = cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
+    for (scp = cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
         if (scp->fid.volume == tfid.volume &&
              scp->fid.vnode == tfid.vnode &&
              scp->fid.unique == tfid.unique &&
-             scp->cbExpires > 0 && 
-             scp->cbServerp != NULL)
+             (cellp == NULL || scp->fid.cell == cellp->cellID) &&
+             cm_HaveCallback(scp))
         {
             cm_HoldSCacheNoLock(scp);
             lock_ReleaseWrite(&cm_scacheLock);
-            osi_Log4(afsd_logp, "RevokeCallback Discarding SCache scp 0x%p vol %u vn %u uniq %u", 
+            osi_Log4(afsd_logp, "RevokeCallback Discarding SCache scp 0x%p vol %u vn %u uniq %u",
                      scp, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
-            lock_ObtainMutex(&scp->mx);
+
+            lock_ObtainWrite(&scp->rw);
             cm_DiscardSCache(scp);
-            lock_ReleaseMutex(&scp->mx);
+            lock_ReleaseWrite(&scp->rw);
+
+            if (RDR_Initialized)
+                RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
+                                     scp->fid.hash, scp->fileType, AFS_INVALIDATE_CALLBACK);
+
             cm_CallbackNotifyChange(scp);
+
             lock_ObtainWrite(&cm_scacheLock);
             cm_ReleaseSCacheNoLock(scp);
         }
@@ -216,51 +218,85 @@ void cm_RevokeCallback(struct rx_call *callp, AFSFid *fidp)
              fidp->Volume, fidp->Vnode, fidp->Unique);
 }
 
+static __inline void
+cm_callbackDiscardROVolumeByFID(cm_fid_t *fidp)
+{
+    cm_volume_t *volp = cm_GetVolumeByFID(fidp);
+    if (volp) {
+        cm_PutVolume(volp);
+        if (volp->cbExpiresRO) {
+            volp->cbExpiresRO = 0;
+            volp->cbIssuedRO = 0;
+            if (volp->cbServerpRO) {
+                cm_PutServer(volp->cbServerpRO);
+                volp->cbServerpRO = NULL;
+            }
+            volp->creationDateRO = 0;
+        }
+    }
+}
+
 /* called to revoke a volume callback, which is typically issued when a volume
  * is moved from one server to another.
  *
  * Called with no locks held.
  */
-void cm_RevokeVolumeCallback(struct rx_call *callp, AFSFid *fidp)
+void cm_RevokeVolumeCallback(struct rx_call *callp, cm_cell_t *cellp, AFSFid *fidp)
 {
-    long hash;
+    unsigned long hash;
     cm_scache_t *scp;
     cm_fid_t tfid;
 
-    osi_Log1(afsd_logp, "RevokeVolumeCallback vol %d", fidp->Volume);
+    osi_Log1(afsd_logp, "RevokeVolumeCallback vol %u", fidp->Volume);
 
     /* do this first, so that if we're executing a callback granting call
      * at this moment, we kill it before it can be merged in.  Otherwise,
      * it could complete while we're doing the scan below, and get missed
      * by both the scan and by this code.
      */
-    tfid.cell = tfid.vnode = tfid.unique = 0;
+    tfid.cell = cellp ? cellp->cellID : 0;
     tfid.volume = fidp->Volume;
+    tfid.vnode = tfid.unique = 0;
     cm_RecordRacingRevoke(&tfid, CM_RACINGFLAG_CANCELVOL);
 
-
     lock_ObtainWrite(&cm_scacheLock);
-    for (hash = 0; hash < cm_data.hashTableSize; hash++) {
-        for(scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
+    for (hash = 0; hash < cm_data.scacheHashTableSize; hash++) {
+        for(scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
             if (scp->fid.volume == fidp->Volume &&
+                (cellp == NULL || scp->fid.cell == cellp->cellID) &&
                  scp->cbExpires > 0 &&
                  scp->cbServerp != NULL) {
                 cm_HoldSCacheNoLock(scp);
                 lock_ReleaseWrite(&cm_scacheLock);
-                lock_ObtainMutex(&scp->mx);
-                osi_Log4(afsd_logp, "RevokeVolumeCallback Discarding SCache scp 0x%p vol %u vn %u uniq %u", 
-                          scp, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
+
+                lock_ObtainWrite(&scp->rw);
+                osi_Log5(afsd_logp, "RevokeVolumeCallback Discarding SCache scp 0x%p vol %u vn %u uniq %u",
+                          scp, scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
+                osi_Log2(afsd_logp, ".... dv 0x%x:%x",
+                          (afs_uint32)((scp->dataVersion >> 32) & 0xFFFFFFFF),
+                          (afs_uint32)(scp->dataVersion & 0xFFFFFFFF));
+
                 cm_DiscardSCache(scp);
-                lock_ReleaseMutex(&scp->mx);
+                lock_ReleaseWrite(&scp->rw);
+
+                if (RDR_Initialized)
+                    RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
+                                         scp->fid.hash, scp->fileType, AFS_INVALIDATE_CALLBACK);
+
                 cm_CallbackNotifyChange(scp);
                 lock_ObtainWrite(&cm_scacheLock);
                 cm_ReleaseSCacheNoLock(scp);
+                if (scp->flags & CM_SCACHEFLAG_PURERO)
+                    cm_callbackDiscardROVolumeByFID(&scp->fid);
             }
         }      /* search one hash bucket */
     }  /* search all hash buckets */
 
     lock_ReleaseWrite(&cm_scacheLock);
 
+    if (cellp && RDR_Initialized)
+        RDR_InvalidateVolume(cellp->cellID, fidp->Volume, AFS_INVALIDATE_CALLBACK);
+
     osi_Log1(afsd_logp, "RevokeVolumeCallback Complete vol %d", fidp->Volume);
 }
 
@@ -339,30 +375,46 @@ SRXAFSCB_CallBack(struct rx_call *callp, AFSCBFids *fidsArrayp, AFSCBs *cbsArray
     struct rx_peer *peerp;
     unsigned long host = 0;
     unsigned short port = 0;
+    cm_server_t *tsp = NULL;
+    cm_cell_t* cellp = NULL;
 
-    MUTEX_ENTER(&callp->lock);
+    if (cm_shutdown)
+        return 1;
 
     if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
         host = rx_HostOf(peerp);
         port = rx_PortOf(peerp);
+
+        tsp = cm_FindServerByIP(host, port, CM_SERVER_FILE, FALSE);
+        if (tsp) {
+            cellp = tsp->cellp;
+            cm_PutServer(tsp);
+        }
+
+        if (!cellp)
+            osi_Log2(afsd_logp, "SRXAFSCB_CallBack from host 0x%x port %d",
+                     ntohl(host),
+                     ntohs(port));
+        else
+            osi_Log3(afsd_logp, "SRXAFSCB_CallBack from host 0x%x port %d for cell %s",
+                     ntohl(host),
+                     ntohs(port),
+                     cellp->name /* does not need to be saved, doesn't change */);
+    } else {
+        osi_Log0(afsd_logp, "SRXAFSCB_CallBack from unknown host");
     }
 
-    osi_Log2(afsd_logp, "SRXAFSCB_CallBack from host 0x%x port %d",
-              ntohl(host),
-              ntohs(port));
 
     for (i=0; i < (long) fidsArrayp->AFSCBFids_len; i++) {
         tfidp = &fidsArrayp->AFSCBFids_val[i];
-                
+
         if (tfidp->Volume == 0)
             continue;   /* means don't do anything */
         else if (tfidp->Vnode == 0)
-            cm_RevokeVolumeCallback(callp, tfidp);
+            cm_RevokeVolumeCallback(callp, cellp, tfidp);
         else
-            cm_RevokeCallback(callp, tfidp);
+            cm_RevokeCallback(callp, cellp, tfidp);
     }
-
-    MUTEX_EXIT(&callp->lock);
     return 0;
 }
 
@@ -392,96 +444,12 @@ SRXAFSCB_CallBack(struct rx_call *callp, AFSCBFids *fidsArrayp, AFSCBs *cbsArray
 int
 SRXAFSCB_InitCallBackState(struct rx_call *callp)
 {
-    struct sockaddr_in taddr;
-    cm_server_t *tsp;
-    cm_scache_t *scp;
-    int hash;
-    int discarded;
-    struct rx_connection *connp;
-    struct rx_peer *peerp;
-    unsigned long host = 0;
-    unsigned short port = 0;
+    if (cm_shutdown)
+        return 1;
 
-    MUTEX_ENTER(&callp->lock);
+    osi_Log0(afsd_logp, "SRXAFSCB_InitCallBackState ->");
 
-    if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
-        host = rx_HostOf(peerp);
-        port = rx_PortOf(peerp);
-    }
-
-    osi_Log2(afsd_logp, "SRXAFSCB_InitCallBackState from host 0x%x port %d",
-              ntohl(host),
-              ntohs(port));
-
-    if ((rx_ConnectionOf(callp)) && (rx_PeerOf(rx_ConnectionOf(callp)))) {
-       taddr.sin_family = AF_INET;
-       taddr.sin_addr.s_addr = rx_HostOf(rx_PeerOf(rx_ConnectionOf(callp)));
-
-       tsp = cm_FindServer(&taddr, CM_SERVER_FILE);
-
-       osi_Log1(afsd_logp, "Init Callback State server %x", tsp);
-       
-       /* record the callback in the racing revokes structure.  This
-        * shouldn't be necessary, since we shouldn't be making callback
-        * granting calls while we're going to get an initstate call,
-        * but there probably are some obscure races, so better safe
-        * than sorry.
-        *
-        * We do this first since we don't hold the cm_scacheLock and vnode
-        * locks over the entire callback scan operation below.  The
-        * big loop below is guaranteed to hit any callback already
-        * processed.  The call to RecordRacingRevoke is guaranteed
-        * to kill any callback that is currently being returned.
-        * Anything that sneaks past both must start
-        * after the call to RecordRacingRevoke.
-        */
-       cm_RecordRacingRevoke(NULL, CM_RACINGFLAG_CANCELALL);
-       
-       /* now search all vnodes looking for guys with this callback, if we
-        * found it, or guys with any callbacks, if we didn't find the server
-        * (that's how multihomed machines will appear and how we'll handle
-        * them, albeit a little inefficiently).  That is, we're discarding all
-        * callbacks from all hosts if we get an initstate call from an unknown
-        * host.  Since these calls are rare, and multihomed servers
-        * are "rare," hopefully this won't be a problem.
-        */
-       lock_ObtainWrite(&cm_scacheLock);
-       for (hash = 0; hash < cm_data.hashTableSize; hash++) {
-            for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
-                cm_HoldSCacheNoLock(scp);
-                lock_ReleaseWrite(&cm_scacheLock);
-                lock_ObtainMutex(&scp->mx);
-                discarded = 0;
-                if (scp->cbExpires > 0 && scp->cbServerp != NULL) {
-                    /* we have a callback, now decide if we should clear it */
-                    if (scp->cbServerp == tsp || tsp == NULL) {
-                        osi_Log4(afsd_logp, "InitCallbackState Discarding SCache scp 0x%p vol %u vn %u uniq %u", 
-                                  scp, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
-                        cm_DiscardSCache(scp);
-                        discarded = 1;
-                    }
-                }
-                lock_ReleaseMutex(&scp->mx);
-                if (discarded)
-                    cm_CallbackNotifyChange(scp);
-                lock_ObtainWrite(&cm_scacheLock);
-                cm_ReleaseSCacheNoLock(scp);
-            }  /* search one hash bucket */
-       }       /* search all hash buckets */
-       
-       lock_ReleaseWrite(&cm_scacheLock);
-       
-       if (tsp) {
-           /* reset the No flags on the server */
-           cm_SetServerNo64Bit(tsp, 0);
-           cm_SetServerNoInlineBulk(tsp, 0);
-
-           /* we're done with the server structure */
-            cm_PutServer(tsp);
-       }
-    }
-    MUTEX_EXIT(&callp->lock);
-    return 0;
+    return SRXAFSCB_InitCallBackState3(callp, NULL);
 }
 
 /*------------------------------------------------------------------------
@@ -512,7 +480,8 @@ SRXAFSCB_Probe(struct rx_call *callp)
     unsigned long host = 0;
     unsigned short port = 0;
 
-    MUTEX_ENTER(&callp->lock);
+    if (cm_shutdown)
+        return 1;
 
     if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
         host = rx_HostOf(peerp);
@@ -523,7 +492,6 @@ SRXAFSCB_Probe(struct rx_call *callp)
               ntohl(host),
               ntohs(port));
 
-    MUTEX_EXIT(&callp->lock);
     return 0;
 }
 
@@ -554,13 +522,12 @@ SRXAFSCB_Probe(struct rx_call *callp)
 
 extern osi_rwlock_t cm_aclLock;
 extern osi_rwlock_t buf_globalLock;
-extern osi_rwlock_t cm_callbackLock;
 extern osi_rwlock_t cm_cellLock;
 extern osi_rwlock_t cm_connLock;
-extern osi_rwlock_t cm_daemonLock;
 extern osi_rwlock_t cm_dnlcLock;
 extern osi_rwlock_t cm_scacheLock;
 extern osi_rwlock_t cm_serverLock;
+extern osi_rwlock_t cm_syscfgLock;
 extern osi_rwlock_t cm_userLock;
 extern osi_rwlock_t cm_utilsLock;
 extern osi_rwlock_t cm_volumeLock;
@@ -568,7 +535,6 @@ extern osi_rwlock_t smb_globalLock;
 extern osi_rwlock_t smb_rctLock;
 
 extern osi_mutex_t cm_Freelance_Lock;
-extern osi_mutex_t cm_bufGetMutex;
 extern osi_mutex_t cm_Afsdsbmt_Lock;
 extern osi_mutex_t tokenEventLock;
 extern osi_mutex_t  smb_ListenerLock;
@@ -586,18 +552,17 @@ static struct _ltable {
     {"buf_globalLock",   (char*)&buf_globalLock,        LOCKTYPE_RW},
     {"cm_serverLock",    (char*)&cm_serverLock,         LOCKTYPE_RW},
     {"cm_callbackLock",  (char*)&cm_callbackLock,       LOCKTYPE_RW},
+    {"cm_syscfgLock",    (char*)&cm_syscfgLock,         LOCKTYPE_RW},
     {"cm_aclLock",       (char*)&cm_aclLock,            LOCKTYPE_RW},
     {"cm_cellLock",      (char*)&cm_cellLock,           LOCKTYPE_RW},
     {"cm_connLock",      (char*)&cm_connLock,           LOCKTYPE_RW},
     {"cm_userLock",      (char*)&cm_userLock,           LOCKTYPE_RW},
     {"cm_volumeLock",    (char*)&cm_volumeLock,         LOCKTYPE_RW},
-    {"cm_daemonLock",    (char*)&cm_daemonLock,         LOCKTYPE_RW},
     {"cm_dnlcLock",      (char*)&cm_dnlcLock,           LOCKTYPE_RW},
     {"cm_utilsLock",     (char*)&cm_utilsLock,          LOCKTYPE_RW},
     {"smb_globalLock",   (char*)&smb_globalLock,        LOCKTYPE_RW},
     {"smb_rctLock",      (char*)&smb_rctLock,           LOCKTYPE_RW},
     {"cm_Freelance_Lock",(char*)&cm_Freelance_Lock,     LOCKTYPE_MUTEX},
-    {"cm_bufGetMutex",   (char*)&cm_bufGetMutex,        LOCKTYPE_MUTEX},
     {"cm_Afsdsbmt_Lock", (char*)&cm_Afsdsbmt_Lock,      LOCKTYPE_MUTEX},
     {"tokenEventLock",   (char*)&tokenEventLock,        LOCKTYPE_MUTEX},
     {"smb_ListenerLock", (char*)&smb_ListenerLock,      LOCKTYPE_MUTEX},
@@ -618,14 +583,15 @@ SRXAFSCB_GetLock(struct rx_call *callp, long index, AFSDBLock *lockp)
     unsigned long host = 0;
     unsigned short port = 0;
 
-    MUTEX_ENTER(&callp->lock);
+    if (cm_shutdown)
+        return 1;
 
     if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
         host = rx_HostOf(peerp);
         port = rx_PortOf(peerp);
     }
 
-    osi_Log3(afsd_logp, "SRXAFSCB_GetLock(%d) from host 0x%x port %d", 
+    osi_Log3(afsd_logp, "SRXAFSCB_GetLock(%d) from host 0x%x port %d",
              index, ntohl(host), ntohs(port));
 
     nentries = sizeof(ltable) / sizeof(struct _ltable);
@@ -662,7 +628,6 @@ SRXAFSCB_GetLock(struct rx_call *callp, long index, AFSDBLock *lockp)
         code = 0;
     }
 
-    MUTEX_EXIT(&callp->lock);
     return code;
 }
 
@@ -670,7 +635,7 @@ SRXAFSCB_GetLock(struct rx_call *callp, long index, AFSDBLock *lockp)
 int
 SRXAFSCB_GetCE(struct rx_call *callp, long index, AFSDBCacheEntry *cep)
 {
-    int i;
+    afs_uint32 i;
     cm_scache_t * scp;
     int code;
     struct rx_connection *connp;
@@ -678,7 +643,8 @@ SRXAFSCB_GetCE(struct rx_call *callp, long index, AFSDBCacheEntry *cep)
     unsigned long host = 0;
     unsigned short port = 0;
 
-    MUTEX_ENTER(&callp->lock);
+    if (cm_shutdown)
+        return 1;
 
     if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
         host = rx_HostOf(peerp);
@@ -689,8 +655,8 @@ SRXAFSCB_GetCE(struct rx_call *callp, long index, AFSDBCacheEntry *cep)
              ntohl(host), ntohs(port));
 
     lock_ObtainRead(&cm_scacheLock);
-    for (i = 0; i < cm_data.hashTableSize; i++) {
-        for (scp = cm_data.hashTablep[i]; scp; scp = scp->nextp) {
+    for (i = 0; i < cm_data.scacheHashTableSize; i++) {
+        for (scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp) {
             if (index == 0)
                 goto searchDone;
             index--;
@@ -714,16 +680,25 @@ SRXAFSCB_GetCE(struct rx_call *callp, long index, AFSDBCacheEntry *cep)
     cep->netFid.Vnode = scp->fid.vnode;
     cep->netFid.Unique = scp->fid.unique;
     cep->lock.waitStates = 0;
-    cep->lock.exclLocked = scp->mx.flags;
+    cep->lock.exclLocked = scp->rw.flags;
     cep->lock.readersReading = 0;
-    cep->lock.numWaiting = scp->mx.waiters;
+    cep->lock.numWaiting = scp->rw.waiters;
     cep->lock.pid_last_reader = 0;
     cep->lock.pid_writer = 0;
     cep->lock.src_indicator = 0;
     cep->Length = scp->length.LowPart;
-    cep->DataVersion = scp->dataVersion;
+    cep->DataVersion = (afs_uint32)(scp->dataVersion & 0xFFFFFFFF);
     cep->callback = afs_data_pointer_to_int32(scp->cbServerp);
-    cep->cbExpires = scp->cbExpires;
+    if (scp->flags & CM_SCACHEFLAG_PURERO) {
+        cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
+        if (volp) {
+            cep->cbExpires = volp->cbExpiresRO;
+            cm_PutVolume(volp);
+        }
+    } else {
+        /* TODO: deal with time_t below */
+        cep->cbExpires = (afs_int32) scp->cbExpires;
+    }
     cep->refCount = scp->refCount;
     cep->opens = scp->openReads;
     cep->writers = scp->openWrites;
@@ -768,7 +743,6 @@ SRXAFSCB_GetCE(struct rx_call *callp, long index, AFSDBCacheEntry *cep)
   fcnDone:
     lock_ReleaseRead(&cm_scacheLock);
 
-    MUTEX_EXIT(&callp->lock);
     return (code);
 }
 
@@ -776,7 +750,7 @@ SRXAFSCB_GetCE(struct rx_call *callp, long index, AFSDBCacheEntry *cep)
 int
 SRXAFSCB_GetCE64(struct rx_call *callp, long index, AFSDBCacheEntry64 *cep)
 {
-    int i;
+    afs_uint32 i;
     cm_scache_t * scp;
     int code;
     struct rx_connection *connp;
@@ -784,7 +758,8 @@ SRXAFSCB_GetCE64(struct rx_call *callp, long index, AFSDBCacheEntry64 *cep)
     unsigned long host = 0;
     unsigned short port = 0;
 
-    MUTEX_ENTER(&callp->lock);
+    if (cm_shutdown)
+        return 1;
 
     if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
         host = rx_HostOf(peerp);
@@ -795,8 +770,8 @@ SRXAFSCB_GetCE64(struct rx_call *callp, long index, AFSDBCacheEntry64 *cep)
              ntohl(host), ntohs(port));
 
     lock_ObtainRead(&cm_scacheLock);
-    for (i = 0; i < cm_data.hashTableSize; i++) {
-        for (scp = cm_data.hashTablep[i]; scp; scp = scp->nextp) {
+    for (i = 0; i < cm_data.scacheHashTableSize; i++) {
+        for (scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp) {
             if (index == 0)
                 goto searchDone;
             index--;
@@ -820,21 +795,25 @@ SRXAFSCB_GetCE64(struct rx_call *callp, long index, AFSDBCacheEntry64 *cep)
     cep->netFid.Vnode = scp->fid.vnode;
     cep->netFid.Unique = scp->fid.unique;
     cep->lock.waitStates = 0;
-    cep->lock.exclLocked = scp->mx.flags;
+    cep->lock.exclLocked = scp->rw.flags;
     cep->lock.readersReading = 0;
-    cep->lock.numWaiting = scp->mx.waiters;
+    cep->lock.numWaiting = scp->rw.waiters;
     cep->lock.pid_last_reader = 0;
     cep->lock.pid_writer = 0;
     cep->lock.src_indicator = 0;
-#if !defined(AFS_64BIT_ENV)
-    cep->Length.high = scp->length.HighPart;
-    cep->Length.low = scp->length.LowPart;
-#else
     cep->Length = (afs_int64) scp->length.QuadPart;
-#endif
-    cep->DataVersion = scp->dataVersion;
+    cep->DataVersion = (afs_uint32)(scp->dataVersion & 0xFFFFFFFF);
     cep->callback = afs_data_pointer_to_int32(scp->cbServerp);
-    cep->cbExpires = scp->cbExpires;
+    if (scp->flags & CM_SCACHEFLAG_PURERO) {
+        cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
+        if (volp) {
+            cep->cbExpires = volp->cbExpiresRO;
+            cm_PutVolume(volp);
+        }
+    } else {
+        /* TODO: handle time_t */
+        cep->cbExpires = (afs_int32) scp->cbExpires;
+    }
     cep->refCount = scp->refCount;
     cep->opens = scp->openReads;
     cep->writers = scp->openWrites;
@@ -879,7 +858,6 @@ SRXAFSCB_GetCE64(struct rx_call *callp, long index, AFSDBCacheEntry64 *cep)
   fcnDone:
     lock_ReleaseRead(&cm_scacheLock);
 
-    MUTEX_EXIT(&callp->lock);
     return (code);
 }
 
@@ -892,7 +870,8 @@ SRXAFSCB_XStatsVersion(struct rx_call *callp, long *vp)
     unsigned long host = 0;
     unsigned short port = 0;
 
-    MUTEX_ENTER(&callp->lock);
+    if (cm_shutdown)
+        return 1;
 
     if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
         host = rx_HostOf(peerp);
@@ -903,7 +882,6 @@ SRXAFSCB_XStatsVersion(struct rx_call *callp, long *vp)
              ntohl(host), ntohs(port));
     *vp = -1;
 
-    MUTEX_EXIT(&callp->lock);
     return RXGEN_OPCODE;
 }
 
@@ -917,7 +895,8 @@ SRXAFSCB_GetXStats(struct rx_call *callp, long cvn, long coln, long *srvp, long
     unsigned long host = 0;
     unsigned short port = 0;
 
-    MUTEX_ENTER(&callp->lock);
+    if (cm_shutdown)
+        return 1;
 
     if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
         host = rx_HostOf(peerp);
@@ -927,16 +906,18 @@ SRXAFSCB_GetXStats(struct rx_call *callp, long cvn, long coln, long *srvp, long
     osi_Log2(afsd_logp, "SRXAFSCB_GetXStats from host 0x%x port %d - not implemented",
              ntohl(host), ntohs(port));
 
-    MUTEX_EXIT(&callp->lock);
     return RXGEN_OPCODE;
 }
 
 int
 SRXAFSCB_InitCallBackState2(struct rx_call *callp, struct interfaceAddr* addr)
 {
+    if (cm_shutdown)
+        return 1;
+
     osi_Log0(afsd_logp, "SRXAFSCB_InitCallBackState2 ->");
 
-    return SRXAFSCB_InitCallBackState(callp);
+    return SRXAFSCB_InitCallBackState3(callp, NULL);
 }
 
 /* debug interface */
@@ -944,45 +925,50 @@ int
 SRXAFSCB_WhoAreYou(struct rx_call *callp, struct interfaceAddr* addr)
 {
     int i;
-    int cm_noIPAddr;         /* number of client network interfaces */
-    int cm_IPAddr[CM_MAXINTERFACE_ADDR];    /* client's IP address in host order */
-    int cm_SubnetMask[CM_MAXINTERFACE_ADDR];/* client's subnet mask in host order*/
-    int cm_NetMtu[CM_MAXINTERFACE_ADDR];    /* client's MTU sizes */
-    int cm_NetFlags[CM_MAXINTERFACE_ADDR];  /* network flags */
     long code;
     struct rx_connection *connp;
     struct rx_peer *peerp;
     unsigned long host = 0;
     unsigned short port = 0;
 
-    MUTEX_ENTER(&callp->lock);
+    if (cm_shutdown)
+        return 1;
 
     if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
         host = rx_HostOf(peerp);
         port = rx_PortOf(peerp);
     }
 
-    /* get network related info */
-    cm_noIPAddr = CM_MAXINTERFACE_ADDR;
-    code = syscfg_GetIFInfo(&cm_noIPAddr,
-                             cm_IPAddr, cm_SubnetMask,
-                             cm_NetMtu, cm_NetFlags);
-
-    /* return all network interface addresses */
     osi_Log2(afsd_logp, "SRXAFSCB_WhoAreYou from host 0x%x port %d",
               ntohl(host),
               ntohs(port));
 
+    lock_ObtainRead(&cm_syscfgLock);
+    if (cm_LanAdapterChangeDetected) {
+        lock_ConvertRToW(&cm_syscfgLock);
+        if (cm_LanAdapterChangeDetected) {
+            /* get network related info */
+            cm_noIPAddr = CM_MAXINTERFACE_ADDR;
+            code = syscfg_GetIFInfo(&cm_noIPAddr,
+                                     cm_IPAddr, cm_SubnetMask,
+                                     cm_NetMtu, cm_NetFlags);
+            cm_LanAdapterChangeDetected = 0;
+        }
+        lock_ConvertWToR(&cm_syscfgLock);
+    }
+
+    /* return all network interface addresses */
     addr->numberOfInterfaces = cm_noIPAddr;
     addr->uuid = cm_data.Uuid;
     for ( i=0; i < cm_noIPAddr; i++ ) {
         addr->addr_in[i] = cm_IPAddr[i];
         addr->subnetmask[i] = cm_SubnetMask[i];
-        addr->mtu[i] = (rx_mtu == -1 || (rx_mtu != -1 && cm_NetMtu[i] < rx_mtu)) ? 
+        addr->mtu[i] = (rx_mtu == -1 || (rx_mtu != -1 && cm_NetMtu[i] < rx_mtu)) ?
             cm_NetMtu[i] : rx_mtu;
     }
 
-    MUTEX_EXIT(&callp->lock);
+    lock_ReleaseRead(&cm_syscfgLock);
+
     return 0;
 }
 
@@ -991,13 +977,138 @@ SRXAFSCB_InitCallBackState3(struct rx_call *callp, afsUUID* serverUuid)
 {
     char *p = NULL;
 
-    if (UuidToString((UUID *)serverUuid, &p) == RPC_S_OK) {
-        osi_Log1(afsd_logp, "SRXAFSCB_InitCallBackState3 %s ->",p);
-        RpcStringFree(&p);
-    } else
-        osi_Log0(afsd_logp, "SRXAFSCB_InitCallBackState3 - no server Uuid ->");
+    cm_server_t *tsp = NULL;
+    cm_scache_t *scp = NULL;
+    cm_cell_t* cellp = NULL;
+    afs_uint32 hash;
+    int discarded;
+    cm_fid_t discardFid;
+    afs_uint32 discardType;
+    struct rx_connection *connp;
+    struct rx_peer *peerp;
+    unsigned long host = 0;
+    unsigned short port = 0;
+
+    if (cm_shutdown)
+        return 1;
+
+    if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
+        host = rx_HostOf(peerp);
+        port = rx_PortOf(peerp);
+
+        if (serverUuid) {
+            if (UuidToString((UUID *)serverUuid, &p) == RPC_S_OK) {
+                osi_Log1(afsd_logp, "SRXAFSCB_InitCallBackState3 Uuid%s ->",osi_LogSaveString(afsd_logp,p));
+                RpcStringFree(&p);
+            }
+
+            tsp = cm_FindServerByUuid(serverUuid, CM_SERVER_FILE, FALSE);
+        }
+        if (!tsp)
+            tsp = cm_FindServerByIP(host, port, CM_SERVER_FILE, FALSE);
+        if (tsp) {
+            cellp = tsp->cellp;
+            cm_PutServer(tsp);
+        }
+
+        if (!cellp)
+            osi_Log2(afsd_logp, "SRXAFSCB_InitCallBackState3 from host 0x%x port %d",
+                     ntohl(host),
+                     ntohs(port));
+        else
+            osi_Log3(afsd_logp, "SRXAFSCB_InitCallBackState3 from host 0x%x port %d for cell %s",
+                     ntohl(host),
+                     ntohs(port),
+                     cellp->name /* does not need to be saved, doesn't change */);
+    } else {
+        osi_Log0(afsd_logp, "SRXAFSCB_InitCallBackState3 from unknown host");
+    }
+
+    if (connp && peerp) {
+       tsp = cm_FindServerByIP(host, port, CM_SERVER_FILE, FALSE);
+
+       osi_Log1(afsd_logp, "InitCallbackState3 server %x", tsp);
+
+       /* record the callback in the racing revokes structure.  This
+        * shouldn't be necessary, since we shouldn't be making callback
+        * granting calls while we're going to get an initstate call,
+        * but there probably are some obscure races, so better safe
+        * than sorry.
+        *
+        * We do this first since we don't hold the cm_scacheLock and vnode
+        * locks over the entire callback scan operation below.  The
+        * big loop below is guaranteed to hit any callback already
+        * processed.  The call to RecordRacingRevoke is guaranteed
+        * to kill any callback that is currently being returned.
+        * Anything that sneaks past both must start
+        * after the call to RecordRacingRevoke.
+        */
+        if (cellp) {
+            cm_fid_t fid;
+
+            fid.cell = cellp->cellID;
+            fid.volume = fid.vnode = fid.unique = 0;
 
-    return SRXAFSCB_InitCallBackState(callp);
+            cm_RecordRacingRevoke(&fid, CM_RACINGFLAG_CANCELALL);
+        } else {
+            cm_RecordRacingRevoke(NULL, CM_RACINGFLAG_CANCELALL);
+        }
+
+       /* now search all vnodes looking for guys with this callback, if we
+        * found it, or guys with any callbacks, if we didn't find the server
+        * (that's how multihomed machines will appear and how we'll handle
+        * them, albeit a little inefficiently).  That is, we're discarding all
+        * callbacks from all hosts if we get an initstate call from an unknown
+        * host.  Since these calls are rare, and multihomed servers
+        * are "rare," hopefully this won't be a problem.
+        */
+       lock_ObtainWrite(&cm_scacheLock);
+       for (hash = 0; hash < cm_data.scacheHashTableSize; hash++) {
+            for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
+                if (scp->cbExpires > 0 && scp->cbServerp != NULL) {
+                    cm_HoldSCacheNoLock(scp);
+                    lock_ReleaseWrite(&cm_scacheLock);
+                    discarded = 0;
+                    lock_ObtainWrite(&scp->rw);
+                    if (scp->cbExpires > 0 && scp->cbServerp != NULL) {
+                        /* we have a callback, now decide if we should clear it */
+                        if (cm_ServerEqual(scp->cbServerp, tsp)) {
+                            osi_Log4(afsd_logp, "InitCallbackState3 Discarding SCache scp 0x%p vol %u vn %u uniq %u",
+                                     scp, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
+                            discardFid = scp->fid;
+                            discardType = scp->fileType;
+                            cm_DiscardSCache(scp);
+                            discarded = 1;
+                        }
+                    }
+                    lock_ReleaseWrite(&scp->rw);
+                    if (discarded) {
+                        cm_CallbackNotifyChange(scp);
+                        if (RDR_Initialized)
+                            RDR_InvalidateObject(discardFid.cell, discardFid.volume, discardFid.vnode, discardFid.unique,
+                                                 discardFid.hash, discardType, AFS_INVALIDATE_EXPIRED);
+                    }
+                    lock_ObtainWrite(&cm_scacheLock);
+                    cm_ReleaseSCacheNoLock(scp);
+
+                    if (discarded && (scp->flags & CM_SCACHEFLAG_PURERO))
+                        cm_callbackDiscardROVolumeByFID(&scp->fid);
+                }
+            }  /* search one hash bucket */
+       }       /* search all hash buckets */
+
+       lock_ReleaseWrite(&cm_scacheLock);
+
+       if (tsp) {
+           /* reset the No flags on the server */
+           cm_SetServerNo64Bit(tsp, 0);
+           cm_SetServerNoInlineBulk(tsp, 0);
+
+           /* we're done with the server structure */
+            cm_PutServer(tsp);
+       }
+    }
+    return 0;
 }
 
 /* debug interface */
@@ -1011,7 +1122,8 @@ SRXAFSCB_ProbeUuid(struct rx_call *callp, afsUUID* clientUuid)
     char *p,*q;
     int code = 0;
 
-    MUTEX_ENTER(&callp->lock);
+    if (cm_shutdown)
+        return 1;
 
     if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
         host = rx_HostOf(peerp);
@@ -1021,8 +1133,8 @@ SRXAFSCB_ProbeUuid(struct rx_call *callp, afsUUID* clientUuid)
     if ( !afs_uuid_equal(&cm_data.Uuid, clientUuid) ) {
         UuidToString((UUID *)&cm_data.Uuid, &p);
         UuidToString((UUID *)clientUuid, &q);
-        osi_Log4(afsd_logp, "SRXAFSCB_ProbeUuid %s != %s from host 0x%x port %d", 
-                  osi_LogSaveString(afsd_logp,p), 
+        osi_Log4(afsd_logp, "SRXAFSCB_ProbeUuid %s != %s from host 0x%x port %d",
+                  osi_LogSaveString(afsd_logp,p),
                   osi_LogSaveString(afsd_logp,q),
                   ntohl(host),
                   ntohs(port));
@@ -1035,54 +1147,40 @@ SRXAFSCB_ProbeUuid(struct rx_call *callp, afsUUID* clientUuid)
                   ntohl(host),
                   ntohs(port));
 
-    MUTEX_EXIT(&callp->lock);
     return code;
 }
 
 /* debug interface */
-int 
-SRXAFSCB_GetCellByNum(struct rx_call *callp, afs_int32 a_cellnum,
-                      char **a_name, serverList *a_hosts)
+static int
+GetCellCommon(afs_int32 a_cellnum, char **a_name, serverList *a_hosts)
 {
     afs_int32 sn;
     cm_cell_t * cellp;
-    cm_serverRef_t * serverRefp; 
-    struct rx_connection *connp;
-    struct rx_peer *peerp;
-    unsigned long host = 0;
-    unsigned short port = 0;
-
-    MUTEX_ENTER(&callp->lock);
+    cm_serverRef_t * serverRefp;
+    size_t len;
 
-    if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
-        host = rx_HostOf(peerp);
-        port = rx_PortOf(peerp);
-    }
-
-    osi_Log3(afsd_logp, "SRXAFSCB_GetCellByNum(%d) from host 0x%x port %d",
-             a_cellnum, ntohl(host), ntohs(port));
-
-    a_hosts->serverList_val = 0;
-    a_hosts->serverList_len = 0;
-
-    cellp = cm_FindCellByID(a_cellnum);
+    cellp = cm_FindCellByID(a_cellnum, CM_FLAG_NOPROBE);
     if (!cellp) {
-        *a_name = strdup("");
-        MUTEX_EXIT(&callp->lock);
+        *a_name = (char *)xdr_alloc(sizeof(char));
+        if (*a_name)
+            *a_name = '\0';
         return 0;
     }
 
     lock_ObtainRead(&cm_serverLock);
-    *a_name = strdup(cellp->name);
+    len = strlen(cellp->name)+1;
+    *a_name = (char *)xdr_alloc(len);
+    if (*a_name)
+        memcpy(*a_name, cellp->name, len);
 
-    for ( sn = 0, serverRefp = cellp->vlServersp; 
+    for ( sn = 0, serverRefp = cellp->vlServersp;
           sn < AFSMAXCELLHOSTS && serverRefp;
           sn++, serverRefp = serverRefp->next);
 
     a_hosts->serverList_len = sn;
-    a_hosts->serverList_val = (afs_int32 *)osi_Alloc(sn * sizeof(afs_int32));
+    a_hosts->serverList_val = (afs_int32 *)xdr_alloc(sn * sizeof(afs_int32));
 
-    for ( sn = 0, serverRefp = cellp->vlServersp; 
+    for ( sn = 0, serverRefp = cellp->vlServersp;
           sn < AFSMAXCELLHOSTS && serverRefp;
           sn++, serverRefp = serverRefp->next)
     {
@@ -1090,64 +1188,98 @@ SRXAFSCB_GetCellByNum(struct rx_call *callp, afs_int32 a_cellnum,
     }
 
     lock_ReleaseRead(&cm_serverLock);
-    MUTEX_EXIT(&callp->lock);
     return 0;
 }
 
 /* debug interface */
-int 
-SRXAFSCB_TellMeAboutYourself( struct rx_call *callp, 
+int
+SRXAFSCB_GetCellByNum(struct rx_call *callp, afs_int32 a_cellnum,
+                      char **a_name, serverList *a_hosts)
+{
+    struct rx_connection *connp;
+    struct rx_peer *peerp;
+    unsigned long host = 0;
+    unsigned short port = 0;
+    int rc;
+
+    if (cm_shutdown)
+        return 1;
+
+    if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
+        host = rx_HostOf(peerp);
+        port = rx_PortOf(peerp);
+    }
+
+    osi_Log3(afsd_logp, "SRXAFSCB_GetCellByNum(%d) from host 0x%x port %d",
+             a_cellnum, ntohl(host), ntohs(port));
+
+    a_hosts->serverList_val = 0;
+    a_hosts->serverList_len = 0;
+
+
+    rc = GetCellCommon(a_cellnum, a_name, a_hosts);
+
+    return rc;
+}
+
+/* debug interface */
+int
+SRXAFSCB_TellMeAboutYourself( struct rx_call *callp,
                               struct interfaceAddr *addr,
                               Capabilities * capabilities)
 {
     int i;
-    afs_int32 *dataBuffP;
+    afs_uint32 *dataBuffP;
     afs_int32 dataBytes;
-    int cm_noIPAddr;         /* number of client network interfaces */
-    int cm_IPAddr[CM_MAXINTERFACE_ADDR];    /* client's IP address in host order */
-    int cm_SubnetMask[CM_MAXINTERFACE_ADDR];/* client's subnet mask in host order*/
-    int cm_NetMtu[CM_MAXINTERFACE_ADDR];    /* client's MTU sizes */
-    int cm_NetFlags[CM_MAXINTERFACE_ADDR];  /* network flags */
     long code;
     struct rx_connection *connp;
     struct rx_peer *peerp;
     unsigned long host = 0;
     unsigned short port = 0;
 
-    MUTEX_ENTER(&callp->lock);
+    if (cm_shutdown)
+        return 1;
 
     if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
         host = rx_HostOf(peerp);
         port = rx_PortOf(peerp);
     }
 
-    /* get network related info */
-    cm_noIPAddr = CM_MAXINTERFACE_ADDR;
-    code = syscfg_GetIFInfo(&cm_noIPAddr,
-                             cm_IPAddr, cm_SubnetMask,
-                             cm_NetMtu, cm_NetFlags);
-
     osi_Log2(afsd_logp, "SRXAFSCB_TellMeAboutYourself from host 0x%x port %d",
               ntohl(host),
               ntohs(port));
 
+    lock_ObtainRead(&cm_syscfgLock);
+    if (cm_LanAdapterChangeDetected) {
+        lock_ConvertRToW(&cm_syscfgLock);
+        if (cm_LanAdapterChangeDetected) {
+            /* get network related info */
+            cm_noIPAddr = CM_MAXINTERFACE_ADDR;
+            code = syscfg_GetIFInfo(&cm_noIPAddr,
+                                     cm_IPAddr, cm_SubnetMask,
+                                     cm_NetMtu, cm_NetFlags);
+            cm_LanAdapterChangeDetected = 0;
+        }
+        lock_ConvertWToR(&cm_syscfgLock);
+    }
+
     /* return all network interface addresses */
     addr->numberOfInterfaces = cm_noIPAddr;
     addr->uuid = cm_data.Uuid;
     for ( i=0; i < cm_noIPAddr; i++ ) {
         addr->addr_in[i] = cm_IPAddr[i];
         addr->subnetmask[i] = cm_SubnetMask[i];
-        addr->mtu[i] = (rx_mtu == -1 || (rx_mtu != -1 && cm_NetMtu[i] < rx_mtu)) ? 
+        addr->mtu[i] = (rx_mtu == -1 || (rx_mtu != -1 && cm_NetMtu[i] < rx_mtu)) ?
             cm_NetMtu[i] : rx_mtu;
     }
+    lock_ReleaseRead(&cm_syscfgLock);
 
-    dataBytes = 1 * sizeof(afs_int32);
-    dataBuffP = (afs_int32 *) osi_Alloc(dataBytes);
+    dataBytes = 1 * sizeof(afs_uint32);
+    dataBuffP = (afs_uint32 *) xdr_alloc(dataBytes);
     dataBuffP[0] = CLIENT_CAPABILITY_ERRORTRANS;
-    capabilities->Capabilities_len = dataBytes / sizeof(afs_int32);
+    capabilities->Capabilities_len = dataBytes / sizeof(afs_uint32);
     capabilities->Capabilities_val = dataBuffP;
 
-    MUTEX_EXIT(&callp->lock);
     return 0;
 }
 
@@ -1184,7 +1316,8 @@ int SRXAFSCB_GetServerPrefs(
     unsigned long host = 0;
     unsigned short port = 0;
 
-    MUTEX_ENTER(&callp->lock);
+    if (cm_shutdown)
+        return 1;
 
     if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
         host = rx_HostOf(peerp);
@@ -1198,7 +1331,6 @@ int SRXAFSCB_GetServerPrefs(
     *a_srvr_addr = 0xffffffff;
     *a_srvr_rank = 0xffffffff;
 
-    MUTEX_EXIT(&callp->lock);
     return 0;
 }
 
@@ -1224,16 +1356,17 @@ int SRXAFSCB_GetServerPrefs(
  *      As advertised.
  *------------------------------------------------------------------------*/
 
-int SRXAFSCB_GetCellServDB(struct rx_call *callp, afs_int32 index, char **a_name, 
+int SRXAFSCB_GetCellServDB(struct rx_call *callp, afs_int32 index, char **a_name,
                            serverList *a_hosts)
 {
-    char *t_name;
     struct rx_connection *connp;
     struct rx_peer *peerp;
     unsigned long host = 0;
     unsigned short port = 0;
+    int rc;
 
-    MUTEX_ENTER(&callp->lock);
+    if (cm_shutdown)
+        return 1;
 
     if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
         host = rx_HostOf(peerp);
@@ -1243,12 +1376,14 @@ int SRXAFSCB_GetCellServDB(struct rx_call *callp, afs_int32 index, char **a_name
     osi_Log2(afsd_logp, "SRXAFSCB_GetCellServDB from host 0x%x port %d - not implemented",
              ntohl(host), ntohs(port));
 
-    t_name = (char *)malloc(AFSNAMEMAX);
-    t_name[0] = '\0';
-    *a_name = t_name;
-    a_hosts->serverList_len = 0;
-
-    MUTEX_EXIT(&callp->lock);
+#ifdef AFS_FREELANCE_CLIENT
+    if (cm_freelanceEnabled && index == 0) {
+        rc = GetCellCommon(AFS_FAKE_ROOT_CELL_ID, a_name, a_hosts);
+    } else
+#endif
+    {
+        rc = GetCellCommon(index+1, a_name, a_hosts);
+    }
     return 0;
 }
 
@@ -1279,8 +1414,10 @@ int SRXAFSCB_GetLocalCell(struct rx_call *callp, char **a_name)
     struct rx_peer *peerp;
     unsigned long host = 0;
     unsigned short port = 0;
+    size_t len;
 
-    MUTEX_ENTER(&callp->lock);
+    if (cm_shutdown)
+        return 1;
 
     if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
         host = rx_HostOf(peerp);
@@ -1291,15 +1428,16 @@ int SRXAFSCB_GetLocalCell(struct rx_call *callp, char **a_name)
              ntohl(host), ntohs(port));
 
     if (cm_data.rootCellp) {
-       t_name = (char *)malloc(strlen(cm_data.rootCellp->name)+1);
-        strcpy(t_name, cm_data.rootCellp->name);
+        len = strlen(cm_data.rootCellp->name) + 1;
+        t_name = (char *)xdr_alloc(len);
+        if (t_name)
+            memcpy(t_name, cm_data.rootCellp->name, len);
     } else {
-       t_name = (char *)malloc(1);
+       t_name = (char *)xdr_alloc(1);
        t_name[0] = '\0';
     }
     *a_name = t_name;
 
-    MUTEX_EXIT(&callp->lock);
     return 0;
 }
 
@@ -1338,7 +1476,7 @@ static void afs_MarshallCacheConfig(
     *(ptr++) = config->memCache;
 
 }
+
 
 /*------------------------------------------------------------------------
  * EXPORTED SRXAFSCB_GetCacheConfig
@@ -1379,7 +1517,8 @@ int SRXAFSCB_GetCacheConfig(struct rx_call *callp,
     unsigned long host = 0;
     unsigned short port = 0;
 
-    MUTEX_ENTER(&callp->lock);
+    if (cm_shutdown)
+        return 1;
 
     if ((connp = rx_ConnectionOf(callp)) && (peerp = rx_PeerOf(connp))) {
         host = rx_HostOf(peerp);
@@ -1393,7 +1532,7 @@ int SRXAFSCB_GetCacheConfig(struct rx_call *callp,
      * Currently only support version 1
      */
     allocsize = sizeof(cm_initparams_v1);
-    t_config = (afs_uint32 *)malloc(allocsize);
+    t_config = (afs_uint32 *)xdr_alloc(allocsize);
 
     afs_MarshallCacheConfig(callerVersion, &cm_initParams, t_config);
 
@@ -1402,20 +1541,19 @@ int SRXAFSCB_GetCacheConfig(struct rx_call *callp,
 #ifndef SIZE_MAX
 #define SIZE_MAX UINT_MAX
 #endif
-    osi_assert(allocsize < SIZE_MAX);
+    osi_assertx(allocsize < SIZE_MAX, "allocsize >= SIZE_MAX");
 #endif
     *configCount = (afs_uint32)allocsize;
     config->cacheConfig_val = t_config;
     config->cacheConfig_len = (*configCount)/sizeof(afs_uint32);
 
-    MUTEX_EXIT(&callp->lock);
     return 0;
 }
 
 /* called by afsd without any locks to initialize this module */
 void cm_InitCallback(void)
 {
-    lock_InitializeRWLock(&cm_callbackLock, "cm_callbackLock");
+    lock_InitializeRWLock(&cm_callbackLock, "cm_callbackLock", LOCK_HIERARCHY_CALLBACK_GLOBAL);
     cm_activeCallbackGrantingCalls = 0;
 }
 
@@ -1426,54 +1564,45 @@ void cm_InitCallback(void)
 int cm_HaveCallback(cm_scache_t *scp)
 {
 #ifdef AFS_FREELANCE_CLIENT
-    // yj: we handle callbacks specially for callbacks on the root directory
-    // Since it's local, we almost always say that we have callback on it
-    // The only time we send back a 0 is if we're need to initialize or
-    // reinitialize the fake directory
-
-    // There are 2 state variables cm_fakeGettingCallback and cm_fakeDirCallback
-    // cm_fakeGettingCallback is 1 if we're in the process of initialization and
-    // hence should return false. it's 0 otherwise
-    // cm_fakeDirCallback is 0 if we haven't loaded the fake directory, it's 1
-    // if the fake directory is loaded and this is the first time cm_HaveCallback
-    // is called since then. We return false in this case to allow cm_GetCallback
-    // to be called because cm_GetCallback has some initialization work to do.
-    // If cm_fakeDirCallback is 2, then it means that the fake directory is in
-    // good shape and we simply return true, provided no change is detected.
-    int fdc, fgc;
-
-    if (cm_freelanceEnabled && 
-         scp->fid.cell==AFS_FAKE_ROOT_CELL_ID && scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
-        /* if it's something on /afs */
-        if (!(scp->fid.vnode==0x1 && scp->fid.unique==0x1)) {
-            /* if it's not root.afs */
-           return 1;
+    if (cm_freelanceEnabled &&
+        scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
+        scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
+        if (cm_getLocalMountPointChange()) {
+            cm_clearLocalMountPointChange();
+            lock_ReleaseWrite(&scp->rw);
+            cm_reInitLocalMountPoints();
+            lock_ObtainWrite(&scp->rw);
         }
-
-        lock_ObtainMutex(&cm_Freelance_Lock);
-        fdc = cm_fakeDirCallback;
-        fgc = cm_fakeGettingCallback;
-        lock_ReleaseMutex(&cm_Freelance_Lock);
-           
-        if (fdc==1) {  // first call since init
-            return 0;
-        } else if (fdc==2 && !fgc) {   // we're in good shape
-            if (cm_getLocalMountPointChange()) {       // check for changes
-                cm_clearLocalMountPointChange(); // clear the changefile
-                lock_ReleaseMutex(&scp->mx);      // this is re-locked in reInitLocalMountPoints
-                cm_reInitLocalMountPoints();   // start reinit
-                lock_ObtainMutex(&scp->mx);      // now get the lock back 
-                return 0;
+        return (cm_data.fakeDirVersion == scp->dataVersion);
+    }
+#endif
+    if (cm_readonlyVolumeVersioning &&
+        (scp->flags & CM_SCACHEFLAG_PURERO)) {
+        cm_volume_t *volp = cm_GetVolumeByFID(&scp->fid);
+        if (volp) {
+            int haveCB = 0;
+            if (cm_OfflineROIsValid) {
+                switch (cm_GetVolumeStatus(volp, scp->fid.volume)) {
+                case vl_offline:
+                case vl_alldown:
+                case vl_unknown:
+                    haveCB = 1;
+                    break;
+                }
+            }
+            if (cm_readonlyVolumeVersioning &&
+                !haveCB &&
+                volp->creationDateRO == scp->volumeCreationDate &&
+                volp->cbServerpRO != NULL) {
+                haveCB = 1;
             }
-            return 1;                  // no change
+            cm_PutVolume(volp);
+            return haveCB;
         }
-        return 0;
     }
-#endif
-
     if (scp->cbServerp != NULL)
        return 1;
-    else 
+    else
         return 0;
 }
 
@@ -1489,7 +1618,7 @@ int cm_HaveCallback(cm_scache_t *scp)
  * When we're back from the call, we look at all of the callback revokes with
  * counter numbers greater than the one we recorded in our caller's structure,
  * and replay those that are higher than when we started the call.
- * 
+ *
  * We free all the structures in the queue when the count of the # of outstanding
  * callback-granting calls drops to zero.
  *
@@ -1501,7 +1630,7 @@ void cm_StartCallbackGrantingCall(cm_scache_t *scp, cm_callbackRequest_t *cbrp)
     lock_ObtainWrite(&cm_callbackLock);
     cbrp->callbackCount = cm_callbackCount;
     cm_activeCallbackGrantingCalls++;
-    cbrp->startTime = osi_Time();
+    cbrp->startTime = time(NULL);
     cbrp->serverp = NULL;
     lock_ReleaseWrite(&cm_callbackLock);
 }
@@ -1509,52 +1638,42 @@ void cm_StartCallbackGrantingCall(cm_scache_t *scp, cm_callbackRequest_t *cbrp)
 /* Called at the end of a callback-granting call, to remove the callback
  * info from the scache entry, if necessary.
  *
- * Called with scp locked, so we can discard the callbacks easily with
+ * Called with scp write locked, so we can discard the callbacks easily with
  * this locking hierarchy.
+ *
+ * Performs cleanup of the stack allocated cm_callbackRequest_t object.
+ *
+ * Returns 0 on success and non-zero if the callback lost a race.
  */
-void cm_EndCallbackGrantingCall(cm_scache_t *scp, cm_callbackRequest_t *cbrp,
-                                AFSCallBack *cbp, long flags)
+int
+cm_EndCallbackGrantingCall(cm_scache_t *scp, cm_callbackRequest_t *cbrp,
+                           AFSCallBack *cbp, AFSVolSync *volSyncp, long flags)
 {
     cm_racingRevokes_t *revp;          /* where we are */
     cm_racingRevokes_t *nrevp;         /* where we'll be next */
-    int freeFlag;
-    cm_server_t * serverp = 0;
-    int discardScp = 0;
+    int freeRacingRevokes;
+    int freeServer;
+    cm_server_t * serverp = NULL;
+    int lostRace = 0;
 
     lock_ObtainWrite(&cm_callbackLock);
     if (flags & CM_CALLBACK_MAINTAINCOUNT) {
-        osi_assert(cm_activeCallbackGrantingCalls > 0);
+        osi_assertx(cm_activeCallbackGrantingCalls > 0,
+                    "CM_CALLBACK_MAINTAINCOUNT && cm_activeCallbackGrantingCalls == 0");
+        freeServer = 0;
     }
     else {
-        osi_assert(cm_activeCallbackGrantingCalls-- > 0);
-    }
-    if (cm_activeCallbackGrantingCalls == 0) 
-        freeFlag = 1;
-    else 
-        freeFlag = 0;
-
-    /* record the callback; we'll clear it below if we really lose it */
-    if (cbrp) {
-       if (scp) {
-            if (scp->cbServerp != cbrp->serverp) {
-                serverp = scp->cbServerp;
-                if (!freeFlag)
-                    cm_GetServer(cbrp->serverp);
-                scp->cbServerp = cbrp->serverp;
-            } else {
-                if (freeFlag)
-                    serverp = cbrp->serverp;
-            }
-            scp->cbExpires = cbrp->startTime + cbp->ExpirationTime;
-        } else {
-            if (freeFlag)
-                serverp = cbrp->serverp;
-        }
-        if (freeFlag)
-            cbrp->serverp = NULL;
+        osi_assertx(cm_activeCallbackGrantingCalls-- > 0,
+                    "!CM_CALLBACK_MAINTAINCOUNT && cm_activeCallbackGrantingCalls == 0");
+        freeServer = 1;
     }
+    if (cm_activeCallbackGrantingCalls == 0)
+        freeRacingRevokes = 1;
+    else
+        freeRacingRevokes = 0;
 
-    /* a callback was actually revoked during our granting call, so
+    /*
+     * a callback was revoked during our granting call, so
      * run down the list of revoked fids, looking for ours.
      * If activeCallbackGrantingCalls is zero, free the elements, too.
      *
@@ -1575,30 +1694,79 @@ void cm_EndCallbackGrantingCall(cm_scache_t *scp, cm_callbackRequest_t *cbrp,
                   ((revp->flags & CM_RACINGFLAG_CANCELVOL) &&
                     scp->fid.volume == revp->fid.volume)
                   ||
-                  (revp->flags & CM_RACINGFLAG_CANCELALL))) {
+                  ((revp->flags & CM_RACINGFLAG_CANCELALL) &&
+                   (revp->fid.cell == 0 || scp->fid.cell == revp->fid.cell)))) {
             /* this one matches */
             osi_Log4(afsd_logp,
                       "Racing revoke scp 0x%p old cbc %d rev cbc %d cur cbc %d",
                       scp,
                       cbrp->callbackCount, revp->callbackCount,
                       cm_callbackCount);
-            discardScp = 1;
+            lostRace = 1;
+            if ((scp->flags & CM_SCACHEFLAG_PURERO) &&
+                 (revp->flags & CM_RACINGFLAG_ALL))
+                cm_callbackDiscardROVolumeByFID(&scp->fid);
         }
-        if (freeFlag) 
+        if (freeRacingRevokes)
             free(revp);
     }
 
     /* if we freed the list, zap the pointer to it */
-    if (freeFlag) 
+    if (freeRacingRevokes)
         cm_racingRevokesp = NULL;
-
     lock_ReleaseWrite(&cm_callbackLock);
 
-    if ( discardScp ) {
-        cm_DiscardSCache(scp);
-        lock_ReleaseMutex(&scp->mx);
-        cm_CallbackNotifyChange(scp);
-        lock_ObtainMutex(&scp->mx);
+    /* record the callback if we didn't lose it */
+    if (!lostRace && scp) {
+        if (cbrp) {
+            if (!cm_ServerEqual(scp->cbServerp, cbrp->serverp)) {
+                serverp = scp->cbServerp;
+                scp->cbServerp = cbrp->serverp;
+                if (freeServer) {
+                    cbrp->serverp = NULL;
+                } else {
+                    cm_GetServer(scp->cbServerp);
+                }
+            } else if (freeServer) {
+                serverp = cbrp->serverp;
+                cbrp->serverp = NULL;
+            }
+            scp->cbExpires = cbrp->startTime + cbp->ExpirationTime;
+            scp->cbIssued = time(NULL);
+        }
+
+        if (scp->flags & CM_SCACHEFLAG_PURERO) {
+            cm_volume_t * volp = cm_GetVolumeByFID(&scp->fid);
+            if (volp) {
+                if (volSyncp) {
+                    lock_ObtainWrite(&cm_scacheLock);
+                    volp->cbExpiresRO = scp->cbExpires;
+                    volp->cbIssuedRO = scp->cbIssued;
+                    volp->creationDateRO = volSyncp->spare1;
+                    if (volp->cbServerpRO != scp->cbServerp) {
+                        if (volp->cbServerpRO)
+                            cm_PutServer(volp->cbServerpRO);
+                        cm_GetServer(scp->cbServerp);
+                        volp->cbServerpRO = scp->cbServerp;
+                    }
+                    lock_ReleaseWrite(&cm_scacheLock);
+                }
+                cm_PutVolume(volp);
+            }
+        }
+    }
+    else
+    {
+        /*
+         * No need to discard the cm_scache object or notify the
+         * afs redirector or smb client about a change if we did lose
+         * the race.  That was done when the callback was processed in
+         * cm_RevokeCallback().
+         */
+        if (cbrp && freeServer) {
+            serverp = cbrp->serverp;
+            cbrp->serverp = NULL;
+        }
     }
 
     if ( serverp ) {
@@ -1606,6 +1774,8 @@ void cm_EndCallbackGrantingCall(cm_scache_t *scp, cm_callbackRequest_t *cbrp,
         cm_FreeServer(serverp);
         lock_ReleaseWrite(&cm_serverLock);
     }
+
+    return lostRace;
 }
 
 /* if flags is 1, we want to force the code to make one call, anyway.
@@ -1614,7 +1784,7 @@ void cm_EndCallbackGrantingCall(cm_scache_t *scp, cm_callbackRequest_t *cbrp,
 long cm_GetCallback(cm_scache_t *scp, struct cm_user *userp,
                     struct cm_req *reqp, long flags)
 {
-    long code;
+    long code = 0;
     cm_conn_t *connp = NULL;
     AFSFetchStatus afsStatus;
     AFSVolSync volSync;
@@ -1622,137 +1792,273 @@ long cm_GetCallback(cm_scache_t *scp, struct cm_user *userp,
     AFSFid tfid;
     cm_callbackRequest_t cbr;
     int mustCall;
-    long sflags;
     cm_fid_t sfid;
-    struct rx_connection * callp = NULL;
+    struct rx_connection * rxconnp = NULL;
+    int syncop_done = 0;
+
+    memset(&volSync, 0, sizeof(volSync));
 
-    osi_Log4(afsd_logp, "GetCallback scp 0x%p cell %d vol %d flags %lX", 
+    osi_Log4(afsd_logp, "GetCallback scp 0x%p cell %d vol %d flags %lX",
              scp, scp->fid.cell, scp->fid.volume, flags);
 
 #ifdef AFS_FREELANCE_CLIENT
     // The case where a callback is needed on /afs is handled
     // specially. We need to fetch the status by calling
-    // cm_MergeStatus and mark that cm_fakeDirCallback is 2
-    if (cm_freelanceEnabled) {
-        if (scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
-             scp->fid.volume==AFS_FAKE_ROOT_VOL_ID &&
-             scp->fid.unique==0x1 &&
-             scp->fid.vnode==0x1) {
-            
-            // Start by indicating that we're in the process
-            // of fetching the callback
-            lock_ObtainMutex(&cm_Freelance_Lock);
-            osi_Log0(afsd_logp,"cm_getGetCallback fakeGettingCallback=1");
-            cm_fakeGettingCallback = 1;
-            lock_ReleaseMutex(&cm_Freelance_Lock);
-
-            // Fetch the status info 
-            cm_MergeStatus(scp, &afsStatus, &volSync, userp, 0);
-
-            // Indicate that the callback is not done
-            lock_ObtainMutex(&cm_Freelance_Lock);
-            osi_Log0(afsd_logp,"cm_getGetCallback fakeDirCallback=2");
-            cm_fakeDirCallback = 2;
-
-            // Indicate that we're no longer fetching the callback
-            osi_Log0(afsd_logp,"cm_getGetCallback fakeGettingCallback=0");
-            cm_fakeGettingCallback = 0;
-            lock_ReleaseMutex(&cm_Freelance_Lock);
-
-            return 0;
-        }
+    // cm_MergeStatus
+    if (cm_freelanceEnabled &&
+        (scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
+         scp->fid.volume==AFS_FAKE_ROOT_VOL_ID)) {
 
-        if (scp->fid.cell==AFS_FAKE_ROOT_CELL_ID && scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
-            osi_Log0(afsd_logp,"cm_getcallback should NEVER EVER get here... ");
+        code = cm_SyncOp(scp, NULL, userp, reqp, 0,
+                          CM_SCACHESYNC_FETCHSTATUS | CM_SCACHESYNC_GETCALLBACK);
+        if (code)
+            goto done;
+        syncop_done = 1;
+
+        if (scp->dataVersion != cm_data.fakeDirVersion) {
+            memset(&afsStatus, 0, sizeof(afsStatus));
+            memset(&volSync, 0, sizeof(volSync));
+            InterlockedIncrement(&scp->activeRPCs);
+
+            // Fetch the status info
+            cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, 0);
         }
+        goto done;
     }
 #endif /* AFS_FREELANCE_CLIENT */
-       
+
     mustCall = (flags & 1);
     cm_AFSFidFromFid(&tfid, &scp->fid);
     while (1) {
-        if (!mustCall && cm_HaveCallback(scp)) {
-            osi_Log3(afsd_logp, "GetCallback Complete scp 0x%p cell %d vol %d", 
-                      scp, scp->fid.cell, scp->fid.volume);
-            return 0;
-        }
+        if (!mustCall && cm_HaveCallback(scp))
+           break;
 
         /* turn off mustCall, since it has now forced us past the check above */
         mustCall = 0;
 
         /* otherwise, we have to make an RPC to get the status */
-        sflags = CM_SCACHESYNC_FETCHSTATUS | CM_SCACHESYNC_GETCALLBACK;
-        cm_SyncOp(scp, NULL, userp, reqp, 0, sflags);
+       if (!syncop_done) {
+           code = cm_SyncOp(scp, NULL, userp, reqp, 0,
+                            CM_SCACHESYNC_FETCHSTATUS | CM_SCACHESYNC_GETCALLBACK);
+           if (code)
+               break;
+           syncop_done = 1;
+       }
         cm_StartCallbackGrantingCall(scp, &cbr);
         sfid = scp->fid;
-        lock_ReleaseMutex(&scp->mx);
-               
+        lock_ReleaseWrite(&scp->rw);
+
         /* now make the RPC */
-        osi_Log4(afsd_logp, "CALL FetchStatus scp 0x%p vol %u vn %u uniq %u", 
+        InterlockedIncrement(&scp->activeRPCs);
+        osi_Log4(afsd_logp, "CALL FetchStatus scp 0x%p vol %u vn %u uniq %u",
                  scp, sfid.volume, sfid.vnode, sfid.unique);
         do {
-            code = cm_Conn(&sfid, userp, reqp, &connp);
-            if (code) 
+            code = cm_ConnFromFID(&sfid, userp, reqp, &connp);
+            if (code)
                 continue;
 
-            callp = cm_GetRxConn(connp);
-            code = RXAFS_FetchStatus(callp, &tfid,
+            rxconnp = cm_GetRxConn(connp);
+            code = RXAFS_FetchStatus(rxconnp, &tfid,
                                      &afsStatus, &callback, &volSync);
-            rx_PutConnection(callp);
+            rx_PutConnection(rxconnp);
 
-        } while (cm_Analyze(connp, userp, reqp, &sfid, &volSync, NULL,
+        } while (cm_Analyze(connp, userp, reqp, &sfid, 0, &volSync, NULL,
                             &cbr, code));
         code = cm_MapRPCError(code, reqp);
         if (code)
-            osi_Log4(afsd_logp, "CALL FetchStatus FAILURE code 0x%x scp 0x%p vol %u vn %u", 
+            osi_Log4(afsd_logp, "CALL FetchStatus FAILURE code 0x%x scp 0x%p vol %u vn %u",
                      code, scp, scp->fid.volume, scp->fid.vnode);
         else
-            osi_Log4(afsd_logp, "CALL FetchStatus SUCCESS scp 0x%p vol %u vn %u uniq %u", 
+            osi_Log4(afsd_logp, "CALL FetchStatus SUCCESS scp 0x%p vol %u vn %u uniq %u",
                      scp, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
 
-        lock_ObtainMutex(&scp->mx);
+        lock_ObtainWrite(&scp->rw);
         if (code == 0) {
-            cm_EndCallbackGrantingCall(scp, &cbr, &callback, 0);
-            cm_MergeStatus(scp, &afsStatus, &volSync, userp, 0);
+            int lostRace = cm_EndCallbackGrantingCall(scp, &cbr, &callback, &volSync, 0);
+            if (!lostRace)
+                cm_MergeStatus(NULL, scp, &afsStatus, &volSync, userp, reqp, 0);
         } else {
-            cm_EndCallbackGrantingCall(NULL, &cbr, NULL, 0);
+            cm_EndCallbackGrantingCall(NULL, &cbr, NULL, NULL, 0);
+            InterlockedDecrement(&scp->activeRPCs);
         }
-        cm_SyncOpDone(scp, NULL, sflags);
 
-        /* now check to see if we got an error */
-        if (code) {
-            osi_Log2(afsd_logp, "GetCallback Failed code 0x%x scp 0x%p -->",code, scp);
-            osi_Log4(afsd_logp, "            cell %u vol %u vn %u uniq %u",
-                     scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
-            return code;
+        /* if we got an error, return to caller */
+        if (code)
+           break;
+    }
+
+  done:
+    if (syncop_done)
+       cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_FETCHSTATUS | CM_SCACHESYNC_GETCALLBACK);
+
+    if (code) {
+       osi_Log2(afsd_logp, "GetCallback Failed code 0x%x scp 0x%p -->",code, scp);
+       osi_Log4(afsd_logp, "            cell %u vol %u vn %u uniq %u",
+                scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
+    } else {
+       osi_Log3(afsd_logp, "GetCallback Complete scp 0x%p cell %d vol %d",
+                 scp, scp->fid.cell, scp->fid.volume);
+    }
+
+    return code;
+}
+
+
+/*
+ * cm_CBServersDownTime() returns 1 if the downTime parameter is valid.
+ *
+ * Servers with multiple interfaces have multiple cm_server_t objects
+ * which share the same UUID.  If one interface is down but others are up,
+ * the server should not be considered down.  The returned downTime should
+ * be the largest non-zero value if down or zero if up.  If the cbServerp
+ * is down, it is updated to refer to an interface that is up (if one exists).
+ *
+ * called with cm_scacheLock held
+ */
+static long
+cm_CBServersDownTime(cm_scache_t *scp, cm_volume_t *volp, time_t * pdownTime)
+{
+    cm_vol_state_t *statep;
+    cm_serverRef_t *tsrp;
+    int alldown = 1;
+    time_t downTime = 0;
+    cm_server_t * upserver = NULL;
+    cm_server_t * downserver;
+
+    *pdownTime = 0;
+
+    if (scp->cbServerp == NULL)
+        return 1;
+
+    if (!(scp->cbServerp->flags & CM_SERVERFLAG_DOWN))
+        return 1;
+
+    statep = cm_VolumeStateByID(volp, scp->fid.volume);
+    if (statep) {
+        for (tsrp = statep->serversp; tsrp; tsrp=tsrp->next) {
+            if (tsrp->status == srv_deleted)
+                continue;
+
+            if (!cm_ServerEqual(tsrp->server, scp->cbServerp))
+                continue;
+
+            if (!(tsrp->server->flags & CM_SERVERFLAG_DOWN)) {
+                alldown = 0;
+                if (!upserver) {
+                    upserver = tsrp->server;
+                    cm_GetServer(upserver);
+                }
+            }
+
+            if (tsrp->server->downTime > downTime)
+                downTime = tsrp->server->downTime;
         }
+    } else {
+        downTime = scp->cbServerp->downTime;
     }
+
+    /* if the cbServerp does not match the current volume server list
+     * we report the callback server as up so the callback can be
+     * expired.
+     */
+
+    if (alldown) {
+        *pdownTime = downTime;
+    } else {
+        lock_ObtainWrite(&scp->rw);
+        downserver = scp->cbServerp;
+        scp->cbServerp = upserver;
+        lock_ReleaseWrite(&scp->rw);
+        cm_PutServer(downserver);
+    }
+    return 1;
 }
 
 /* called periodically by cm_daemon to shut down use of expired callbacks */
 void cm_CheckCBExpiration(void)
 {
-    int i;
+    afs_uint32 i;
     cm_scache_t *scp;
-    time_t now;
-        
+    cm_volume_t *volp;
+    enum volstatus volstate;
+    time_t now, downTime;
+
     osi_Log0(afsd_logp, "CheckCBExpiration");
 
-    now = osi_Time();
+    now = time(NULL);
     lock_ObtainWrite(&cm_scacheLock);
-    for (i=0; i<cm_data.hashTableSize; i++) {
-        for (scp = cm_data.hashTablep[i]; scp; scp=scp->nextp) {
+    for (i=0; i<cm_data.scacheHashTableSize; i++) {
+        for (scp = cm_data.scacheHashTablep[i]; scp; scp=scp->nextp) {
+            volp = NULL;
             cm_HoldSCacheNoLock(scp);
-            if (scp->cbExpires > 0 && (scp->cbServerp == NULL || now > scp->cbExpires)) {
-                lock_ReleaseWrite(&cm_scacheLock);
-                osi_Log4(afsd_logp, "Callback Expiration Discarding SCache scp 0x%p vol %u vn %u uniq %u", 
-                          scp, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
-                lock_ObtainMutex(&scp->mx);
-                cm_DiscardSCache(scp);
-                lock_ReleaseMutex(&scp->mx);
-                cm_CallbackNotifyChange(scp);
-                lock_ObtainWrite(&cm_scacheLock);
+            lock_ReleaseWrite(&cm_scacheLock);
+
+            /*
+             * If this is not a PURERO object and there is no callback
+             * or it hasn't expired, there is nothing to do
+             */
+            if (!(scp->flags & CM_SCACHEFLAG_PURERO) &&
+                (scp->cbServerp == NULL || scp->cbExpires == 0 || now < scp->cbExpires))
+                goto scp_complete;
+
+            /*
+             * Determine the volume state and update the callback info
+             * to the latest if it is a PURERO object.
+             */
+            volp = cm_GetVolumeByFID(&scp->fid);
+            volstate = vl_unknown;
+            downTime = 0;
+            if (volp) {
+                if ((scp->flags & CM_SCACHEFLAG_PURERO) &&
+                    volp->cbExpiresRO > scp->cbExpires && scp->cbExpires > 0)
+                {
+                    lock_ObtainWrite(&scp->rw);
+                    scp->cbExpires = volp->cbExpiresRO;
+                    scp->cbIssued = volp->cbIssuedRO;
+                    if (volp->cbServerpRO != scp->cbServerp) {
+                        if (scp->cbServerp)
+                            cm_PutServer(scp->cbServerp);
+                        cm_GetServer(volp->cbServerpRO);
+                        scp->cbServerp = volp->cbServerpRO;
+                    }
+                    lock_ReleaseWrite(&scp->rw);
+                }
+                volstate = cm_GetVolumeStatus(volp, scp->fid.volume);
             }
+
+            /* If there is no callback or it hasn't expired, there is nothing to do */
+            if (scp->cbServerp == NULL || scp->cbExpires == 0 || now < scp->cbExpires)
+                goto scp_complete;
+
+            /* If the volume is known not to be online, do not expire the callback */
+            if (volstate != vl_online)
+                goto scp_complete;
+
+            /*
+             * If all the servers are down and the callback expired after the
+             * issuing server went down, do not expire the callback
+             */
+            if (cm_CBServersDownTime(scp, volp, &downTime) && downTime && downTime < scp->cbExpires)
+                goto scp_complete;
+
+            /* The callback has expired, discard the status info */
+            osi_Log4(afsd_logp, "Callback Expiration Discarding SCache scp 0x%p vol %u vn %u uniq %u",
+                     scp, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
+            lock_ObtainWrite(&scp->rw);
+            cm_DiscardSCache(scp);
+            lock_ReleaseWrite(&scp->rw);
+
+            if (RDR_Initialized)
+                RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
+                                     scp->fid.hash, scp->fileType, AFS_INVALIDATE_EXPIRED);
+
+            cm_CallbackNotifyChange(scp);
+
+          scp_complete:
+            if (volp)
+                cm_PutVolume(volp);
+
+            lock_ObtainWrite(&cm_scacheLock);
             cm_ReleaseSCacheNoLock(scp);
         }
     }
@@ -1761,3 +2067,183 @@ void cm_CheckCBExpiration(void)
     osi_Log0(afsd_logp, "CheckCBExpiration Complete");
 }
 
+
+void
+cm_GiveUpAllCallbacks(cm_server_t *tsp, afs_int32 markDown)
+{
+    long code;
+    cm_conn_t *connp;
+    struct rx_connection * rxconnp;
+
+    if ((tsp->type == CM_SERVER_FILE) && !(tsp->flags & CM_SERVERFLAG_DOWN))
+    {
+        code = cm_ConnByServer(tsp, cm_rootUserp, FALSE, &connp);
+        if (code == 0) {
+            rxconnp = cm_GetRxConn(connp);
+            rx_SetConnDeadTime(rxconnp, 10);
+            code = RXAFS_GiveUpAllCallBacks(rxconnp);
+            rx_SetConnDeadTime(rxconnp, ConnDeadtimeout);
+            rx_PutConnection(rxconnp);
+        }
+
+        if (markDown) {
+            cm_server_vols_t * tsrvp;
+            cm_volume_t * volp;
+            int i;
+
+            cm_ForceNewConnections(tsp);
+
+            lock_ObtainMutex(&tsp->mx);
+            if (!(tsp->flags & CM_SERVERFLAG_DOWN)) {
+                _InterlockedOr(&tsp->flags, CM_SERVERFLAG_DOWN);
+                tsp->downTime = time(NULL);
+            }
+            /* Now update the volume status */
+            for (tsrvp = tsp->vols; tsrvp; tsrvp = tsrvp->nextp) {
+                for (i=0; i<NUM_SERVER_VOLS; i++) {
+                    if (tsrvp->ids[i] != 0) {
+                        cm_req_t req;
+
+                        cm_InitReq(&req);
+                        lock_ReleaseMutex(&tsp->mx);
+                        code = cm_FindVolumeByID(tsp->cellp, tsrvp->ids[i], cm_rootUserp,
+                                                 &req, CM_GETVOL_FLAG_NO_LRU_UPDATE | CM_GETVOL_FLAG_NO_RESET, &volp);
+                        lock_ObtainMutex(&tsp->mx);
+                        if (code == 0) {
+                            cm_UpdateVolumeStatus(volp, tsrvp->ids[i]);
+                            cm_PutVolume(volp);
+                        }
+                    }
+                }
+            }
+            lock_ReleaseMutex(&tsp->mx);
+        }
+    }
+}
+
+void
+cm_GiveUpAllCallbacksAllServers(afs_int32 markDown)
+{
+    cm_server_t *tsp;
+
+    if (!cm_giveUpAllCBs)
+        return;
+
+    lock_ObtainRead(&cm_serverLock);
+    for (tsp = cm_allServersp; tsp; tsp = tsp->allNextp) {
+        cm_GetServerNoLock(tsp);
+        lock_ReleaseRead(&cm_serverLock);
+        cm_GiveUpAllCallbacks(tsp, markDown);
+        lock_ObtainRead(&cm_serverLock);
+        cm_PutServerNoLock(tsp);
+    }
+    lock_ReleaseRead(&cm_serverLock);
+}
+
+void
+cm_GiveUpAllCallbacksAllServersMulti(afs_int32 markDown)
+{
+    long code;
+    cm_conn_t **conns = NULL;
+    struct rx_connection **rxconns = NULL;
+    afs_int32 i, nconns = 0, maxconns;
+    cm_server_t ** serversp, *tsp;
+    afs_int32 *results;
+    time_t start, *deltas;
+
+    maxconns = cm_numFileServers;
+    if (maxconns == 0)
+        return;
+
+    conns = (cm_conn_t **)malloc(maxconns * sizeof(cm_conn_t *));
+    rxconns = (struct rx_connection **)malloc(maxconns * sizeof(struct rx_connection *));
+    deltas = (time_t *)malloc(maxconns * sizeof (time_t));
+    results = (afs_int32 *)malloc(maxconns * sizeof (afs_int32));
+    serversp = (cm_server_t **)malloc(maxconns * sizeof(cm_server_t *));
+
+    lock_ObtainRead(&cm_serverLock);
+    for (nconns=0, tsp = cm_allServersp; tsp && nconns < maxconns; tsp = tsp->allNextp) {
+        if (tsp->type != CM_SERVER_FILE ||
+            (tsp->flags & CM_SERVERFLAG_DOWN) ||
+            tsp->cellp == NULL          /* SetPrefs only */)
+            continue;
+
+        cm_GetServerNoLock(tsp);
+        lock_ReleaseRead(&cm_serverLock);
+
+        serversp[nconns] = tsp;
+        code = cm_ConnByServer(tsp, cm_rootUserp, FALSE, &conns[nconns]);
+        if (code) {
+            lock_ObtainRead(&cm_serverLock);
+            cm_PutServerNoLock(tsp);
+            continue;
+        }
+        lock_ObtainRead(&cm_serverLock);
+        rxconns[nconns] = cm_GetRxConn(conns[nconns]);
+        rx_SetConnDeadTime(rxconns[nconns], 10);
+
+        nconns++;
+    }
+    lock_ReleaseRead(&cm_serverLock);
+
+    if (nconns) {
+        /* Perform the multi call */
+        start = time(NULL);
+        multi_Rx(rxconns,nconns)
+        {
+            multi_RXAFS_GiveUpAllCallBacks();
+            results[multi_i]=multi_error;
+        } multi_End;
+    }
+
+    /* Process results of servers that support RXAFS_GetCapabilities */
+    for (i=0; i<nconns; i++) {
+        rx_SetConnDeadTime(rxconns[i], ConnDeadtimeout);
+        rx_PutConnection(rxconns[i]);
+        cm_PutConn(conns[i]);
+
+        tsp = serversp[i];
+        cm_GCConnections(tsp);
+
+        if (markDown) {
+            cm_server_vols_t * tsrvp;
+            cm_volume_t * volp;
+            int i;
+
+            cm_ForceNewConnections(tsp);
+
+            lock_ObtainMutex(&tsp->mx);
+            if (!(tsp->flags & CM_SERVERFLAG_DOWN)) {
+                _InterlockedOr(&tsp->flags, CM_SERVERFLAG_DOWN);
+                tsp->downTime = time(NULL);
+            }
+            /* Now update the volume status */
+            for (tsrvp = tsp->vols; tsrvp; tsrvp = tsrvp->nextp) {
+                for (i=0; i<NUM_SERVER_VOLS; i++) {
+                    if (tsrvp->ids[i] != 0) {
+                        cm_req_t req;
+
+                        cm_InitReq(&req);
+                        lock_ReleaseMutex(&tsp->mx);
+                        code = cm_FindVolumeByID(tsp->cellp, tsrvp->ids[i], cm_rootUserp,
+                                                 &req, CM_GETVOL_FLAG_NO_LRU_UPDATE | CM_GETVOL_FLAG_NO_RESET, &volp);
+                        lock_ObtainMutex(&tsp->mx);
+                        if (code == 0) {
+                            cm_UpdateVolumeStatus(volp, tsrvp->ids[i]);
+                            cm_PutVolume(volp);
+                        }
+                    }
+                }
+            }
+            lock_ReleaseMutex(&tsp->mx);
+        }
+    }
+
+    free(conns);
+    free(rxconns);
+    free(deltas);
+    free(results);
+    free(serversp);
+}
+
+