viced: Move host quota calculation
[openafs.git] / src / viced / host.c
index 7d12aa3..c24841c 100644 (file)
 #include <afs/stds.h>
 
 #include <roken.h>
+#include <afs/opr.h>
+#include <opr/lock.h>
 
 #ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
 #endif
 
-#include <rx/xdr.h>
-#include <afs/afs_assert.h>
-#include <lwp.h>
-#include <lock.h>
 #include <afs/afsint.h>
 #define FSINT_COMMON_XG
 #include <afs/afscbint.h>
 #include <afs/nfs.h>
 #include <afs/errors.h>
 #include <afs/ihandle.h>
-#include <afs/vnode.h>
-#include <afs/volume.h>
-#ifdef AFS_ATHENA_STDENV
-#include <krb.h>
-#endif
 #include <afs/acl.h>
 #include <afs/ptclient.h>
 #include <afs/ptuser.h>
 #include "callback.h"
 #ifdef AFS_DEMAND_ATTACH_FS
 #include "../util/afsutil_prototypes.h"
-#include "../tviced/serialize_state.h"
+#include "serialize_state.h"
 #endif /* AFS_DEMAND_ATTACH_FS */
 
-#ifdef AFS_PTHREAD_ENV
 pthread_mutex_t host_glock_mutex;
-#endif /* AFS_PTHREAD_ENV */
 
 extern int Console;
 extern int CurrentConnections;
@@ -66,6 +57,7 @@ extern int LogLevel;
 extern struct afsconf_dir *confDir;    /* config dir object */
 extern int lwps;               /* the max number of server threads */
 extern afsUUID FS_HostUUID;
+extern char *FS_configPath;
 
 afsUUID nulluuid;
 int CEs = 0;                   /* active clients */
@@ -77,9 +69,18 @@ int rxcon_ident_key;
 int rxcon_client_key;
 
 static struct rx_securityClass *sc = NULL;
+static int h_quota_limit;
+
+/* arguments for PerHost_EnumerateClient enumeration */
+struct enumclient_args {
+    VolumeId vid;
+    int (*proc)(struct client *client, void *rock);
+    void *rock;
+};
 
 static void h_SetupCallbackConn_r(struct host * host);
 static int h_threadquota(int);
+static int initInterfaceAddr_r(struct host *, struct interfaceAddr *);
 
 #define CESPERBLOCK 73
 struct CEBlock {               /* block of CESPERBLOCK file entries */
@@ -115,7 +116,7 @@ GetCEBlock(void)
     struct CEBlock *block;
     int i;
 
-    block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
+    block = malloc(sizeof(struct CEBlock));
     if (!block) {
        ViceLog(0, ("Failed malloc in GetCEBlock\n"));
        ShutDownAndCore(PANIC);
@@ -200,18 +201,16 @@ GetHTBlock(void)
 
     if (HTBlocks == h_MAXHOSTTABLES) {
        ViceLog(0, ("h_MAXHOSTTABLES reached\n"));
-       ShutDownAndCore(PANIC);
+       return;
     }
 
-    block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
+    block = malloc(sizeof(struct HTBlock));
     if (!block) {
        ViceLog(0, ("Failed malloc in GetHTBlock\n"));
        ShutDownAndCore(PANIC);
     }
-#ifdef AFS_PTHREAD_ENV
     for (i = 0; i < (h_HTSPERBLOCK); i++)
-       CV_INIT(&block->entry[i].cond, "block entry", CV_DEFAULT, 0);
-#endif /* AFS_PTHREAD_ENV */
+       opr_cv_init(&block->entry[i].cond);
     for (i = 0; i < (h_HTSPERBLOCK); i++)
        Lock_Init(&block->entry[i].lock);
     for (i = 0; i < (h_HTSPERBLOCK - 1); i++)
@@ -233,7 +232,8 @@ GetHT(void)
 
     if (HTFree == NULL)
        GetHTBlock();
-    osi_Assert(HTFree != NULL);
+    if (HTFree == NULL)
+       return NULL;
     entry = HTFree;
     HTFree = entry->next;
     HTs++;
@@ -265,9 +265,11 @@ hpr_Initialize(struct ubik_client **uclient)
     afs_int32 i;
     char cellstr[64];
 
-    tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
+    tdir = afsconf_Open(FS_configPath);
     if (!tdir) {
-       ViceLog(0, ("hpr_Initialize: Could not open configuration directory: %s", AFSDIR_SERVER_ETC_DIRPATH));
+       ViceLog(0,
+               ("hpr_Initialize: Could not open configuration directory: %s",
+                FS_configPath));
        return -1;
     }
 
@@ -281,7 +283,7 @@ hpr_Initialize(struct ubik_client **uclient)
     code = afsconf_GetCellInfo(tdir, cellstr, "afsprot", &info);
     if (code) {
        ViceLog(0, ("hpr_Initialize: Could not locate cell %s in %s/%s",
-                   cellstr, confDir->name, AFSDIR_CELLSERVDB_FILE));
+                   cellstr, tdir->name, AFSDIR_CELLSERVDB_FILE));
        afsconf_Close(tdir);
        return code;
     }
@@ -336,22 +338,34 @@ hpr_End(struct ubik_client *uclient)
     return code;
 }
 
+static_inline int
+getThreadClient(struct ubik_client **client)
+{
+    int code;
+
+    *client = pthread_getspecific(viced_uclient_key);
+    if (*client != NULL)
+       return 0;
+
+    code = hpr_Initialize(client);
+    if (code)
+       return code;
+
+    opr_Verify(pthread_setspecific(viced_uclient_key, *client) == 0);
+
+    return 0;
+}
+
 int
 hpr_GetHostCPS(afs_int32 host, prlist *CPS)
 {
-#ifdef AFS_PTHREAD_ENV
     afs_int32 code;
     afs_int32 over;
-    struct ubik_client *uclient =
-       (struct ubik_client *)pthread_getspecific(viced_uclient_key);
+    struct ubik_client *uclient;
 
-    if (!uclient) {
-        code = hpr_Initialize(&uclient);
-       if (!code)
-           osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
-       else
-           return code;
-    }
+    code = getThreadClient(&uclient);
+    if (code)
+       return code;
 
     over = 0;
     code = ubik_PR_GetHostCPS(uclient, 0, host, CPS, &over);
@@ -365,76 +379,49 @@ hpr_GetHostCPS(afs_int32 host, prlist *CPS)
                 host);
     }
     return 0;
-#else
-    return pr_GetHostCPS(host, CPS);
-#endif
 }
 
 int
 hpr_NameToId(namelist *names, idlist *ids)
 {
-#ifdef AFS_PTHREAD_ENV
     afs_int32 code;
     afs_int32 i;
-    struct ubik_client *uclient =
-       (struct ubik_client *)pthread_getspecific(viced_uclient_key);
+    struct ubik_client *uclient;
 
-    if (!uclient) {
-        code = hpr_Initialize(&uclient);
-       if (!code)
-           osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
-       else
-           return code;
-    }
+    code = getThreadClient(&uclient);
+    if (code)
+       return code;
 
     for (i = 0; i < names->namelist_len; i++)
         stolower(names->namelist_val[i]);
     code = ubik_PR_NameToID(uclient, 0, names, ids);
     return code;
-#else
-    return pr_NameToId(names, ids);
-#endif
 }
 
 int
 hpr_IdToName(idlist *ids, namelist *names)
 {
-#ifdef AFS_PTHREAD_ENV
     afs_int32 code;
-    struct ubik_client *uclient =
-       (struct ubik_client *)pthread_getspecific(viced_uclient_key);
+    struct ubik_client *uclient;
 
-    if (!uclient) {
-        code = hpr_Initialize(&uclient);
-       if (!code)
-           osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
-       else
-           return code;
-    }
+    code = getThreadClient(&uclient);
+    if (code)
+       return code;
 
     code = ubik_PR_IDToName(uclient, 0, ids, names);
     return code;
-#else
-    return pr_IdToName(ids, names);
-#endif
 }
 
 int
 hpr_GetCPS(afs_int32 id, prlist *CPS)
 {
-#ifdef AFS_PTHREAD_ENV
     afs_int32 code;
     afs_int32 over;
-    struct ubik_client *uclient =
-       (struct ubik_client *)pthread_getspecific(viced_uclient_key);
+    struct ubik_client *uclient;
 
-    if (!uclient) {
-        code = hpr_Initialize(&uclient);
-       if (!code)
-           osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
-       else
-           return code;
-    }
+    code = getThreadClient(&uclient);
+    if (code)
+       return code;
 
     over = 0;
     code = ubik_PR_GetCPS(uclient, 0, id, CPS, &over);
@@ -447,9 +434,6 @@ hpr_GetCPS(afs_int32 id, prlist *CPS)
                 id);
     }
     return 0;
-#else
-    return pr_GetCPS(id, CPS);
-#endif
 }
 
 static short consolePort = 0;
@@ -491,7 +475,6 @@ h_NBLock_r(struct host *host)
 }
 
 
-#if FS_STATS_DETAILED
 /*------------------------------------------------------------------------
  * PRIVATE h_AddrInSameNetwork
  *
@@ -567,7 +550,6 @@ h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
        return (0);
 
 }                              /*h_AddrInSameNetwork */
-#endif /* FS_STATS_DETAILED */
 
 
 /* Assumptions: called with held host */
@@ -581,12 +563,7 @@ h_gethostcps_r(struct host *host, afs_int32 now)
     while (host->hostFlags & HCPS_INPROGRESS) {
        slept = 1;              /* I did sleep */
        host->hostFlags |= HCPS_WAITING;        /* I am sleeping now */
-#ifdef AFS_PTHREAD_ENV
-       CV_WAIT(&host->cond, &host_glock_mutex);
-#else /* AFS_PTHREAD_ENV */
-       if ((code = LWP_WaitProcess(&(host->hostFlags))) != LWP_SUCCESS)
-           ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
-#endif /* AFS_PTHREAD_ENV */
+       opr_cv_wait(&host->cond, &host_glock_mutex);
     }
 
 
@@ -595,7 +572,7 @@ h_gethostcps_r(struct host *host, afs_int32 now)
        free(host->hcps.prlist_val);    /* this is for hostaclRefresh */
     host->hcps.prlist_val = NULL;
     host->hcps.prlist_len = 0;
-    host->cpsCall = slept ? (FT_ApproxTime()) : (now);
+    host->cpsCall = slept ? time(NULL) : (now);
 
     H_UNLOCK;
     code = hpr_GetHostCPS(ntohl(host->host), &host->hcps);
@@ -642,12 +619,7 @@ h_gethostcps_r(struct host *host, afs_int32 now)
     /* signal all who are waiting */
     if (host->hostFlags & HCPS_WAITING) {      /* somebody is waiting */
        host->hostFlags &= ~HCPS_WAITING;
-#ifdef AFS_PTHREAD_ENV
-       CV_BROADCAST(&host->cond);
-#else /* AFS_PTHREAD_ENV */
-       if ((code = LWP_NoYieldSignal(&(host->hostFlags))) != LWP_SUCCESS)
-           ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
-#endif /* AFS_PTHREAD_ENV */
+       opr_cv_broadcast(&host->cond);
     }
 }
 
@@ -674,16 +646,22 @@ h_flushhostcps(afs_uint32 hostaddr, afs_uint16 hport)
  */
 #define        DEF_ROPCONS 2115
 
-struct host *
+static struct host *
 h_Alloc_r(struct rx_connection *r_con)
 {
     struct servent *serverentry;
     struct host *host;
-#if FS_STATS_DETAILED
     afs_uint32 newHostAddr_HBO;        /*New host IP addr, in host byte order */
-#endif /* FS_STATS_DETAILED */
 
     host = GetHT();
+    if (!host)
+       return NULL;
+
+    h_Hold_r(host);
+    /* acquire the host lock withot dropping H_LOCK. we can do this here
+     * because we know we will not block; we just created this host and
+     * nobody else knows about it. */
+    ObtainWriteLock(&host->lock);
 
     host->host = rxr_HostOf(r_con);
     host->port = rxr_PortOf(r_con);
@@ -706,7 +684,7 @@ h_Alloc_r(struct rx_connection *r_con)
     /* Make a callback channel even for the console, on the off chance that it
      * makes a request that causes a break call back.  It shouldn't. */
     h_SetupCallbackConn_r(host);
-    host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
+    host->LastCall = host->cpsCall = host->ActiveCall = time(NULL);
     host->hostFlags = 0;
     host->hcps.prlist_val = NULL;
     host->hcps.prlist_len = 0;
@@ -716,10 +694,7 @@ h_Alloc_r(struct rx_connection *r_con)
     h_gethostcps(host);                /* do this under host hold/lock */
 #endif
     host->FirstClient = NULL;
-    h_Hold_r(host);
-    h_Lock_r(host);
     h_InsertList_r(host);      /* update global host List */
-#if FS_STATS_DETAILED
     /*
      * Compare the new host's IP address (in host byte order) with ours
      * (the File Server's), remembering if they are in the same network.
@@ -727,7 +702,6 @@ h_Alloc_r(struct rx_connection *r_con)
     newHostAddr_HBO = (afs_uint32) ntohl(host->host);
     host->InSameNetwork =
        h_AddrInSameNetwork(FS_HostAddr_HBO, newHostAddr_HBO);
-#endif /* FS_STATS_DETAILED */
     return host;
 
 }                              /*h_Alloc_r */
@@ -745,6 +719,7 @@ h_SetupCallbackConn_r(struct host * host)
        rx_NewConnection(host->host, host->port, 1, sc, 0);
     rx_SetConnDeadTime(host->callback_rxcon, 50);
     rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
+    rx_SetConnSecondsUntilNatPing(host->callback_rxcon, 20);
 }
 
 /* h_Lookup_r
@@ -765,7 +740,7 @@ h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, struct host **hostp)
   restart:
     for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
        host = chain->hostPtr;
-       osi_Assert(host);
+       opr_Assert(host);
        if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
            && chain->port == hport) {
            if ((host->hostFlags & HWHO_INPROGRESS) &&
@@ -782,7 +757,7 @@ h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, struct host **hostp)
                goto restart;
            }
            h_Unlock_r(host);
-           now = FT_ApproxTime();      /* always evaluate "now" */
+           now = time(NULL);   /* always evaluate "now" */
            if (host->hcpsfailed || (host->cpsCall + hostaclRefresh < now)) {
                /*
                 * Every hostaclRefresh period (def 2 hrs) get the new
@@ -812,7 +787,7 @@ h_LookupUuid_r(afsUUID * uuidp)
 
     for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
        host = chain->hostPtr;
-       osi_Assert(host);
+       opr_Assert(host);
        if (!(host->hostFlags & HOSTDELETED) && host->interface
            && afs_uuid_equal(&host->interface->uuid, uuidp)) {
             return host;
@@ -832,6 +807,11 @@ h_TossStuff_r(struct host *host)
 {
     struct client **cp, *client;
     int code;
+    int wasdeleted = 0;
+
+    if ((host->hostFlags & HOSTDELETED)) {
+       wasdeleted = 1;
+    }
 
     /* make sure host doesn't go away over h_NBLock_r */
     h_Hold_r(host);
@@ -844,9 +824,13 @@ h_TossStuff_r(struct host *host)
     /* if somebody still has this host locked */
     if (code != 0) {
        char hoststr[16];
-       ViceLog(0,
-               ("Warning:  h_TossStuff_r failed; Host %" AFS_PTR_FMT " (%s:%d) was locked.\n",
-                host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+       if (wasdeleted) {
+           /* someone locked the host while HOSTDELETED was set; that is bad */
+           ViceLog(0, ("Warning:  h_TossStuff_r failed; Host %" AFS_PTR_FMT
+                       " (%s:%d flags 0x%x) was locked.\n",
+                       host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
+                       (unsigned)host->hostFlags));
+       }
        return;
     } else {
        h_Unlock_r(host);
@@ -857,9 +841,13 @@ h_TossStuff_r(struct host *host)
      * reacquire H_LOCK */
     if (host->refCount > 0) {
        char hoststr[16];
-       ViceLog(0,
-               ("Warning:  h_TossStuff_r failed; Host %" AFS_PTR_FMT " (%s:%d) was held.\n",
-                host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+       if (wasdeleted) {
+           /* someone grabbed a ref while HOSTDELETED was set; that is bad */
+           ViceLog(0, ("Warning:  h_TossStuff_r failed; Host %" AFS_PTR_FMT
+                       " (%s:%d flags 0x%x) was held.\n",
+                       host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
+                       (unsigned)host->hostFlags));
+       }
        return;
     }
 
@@ -952,23 +940,18 @@ h_TossStuff_r(struct host *host)
 
 
 
-/* h_Enumerate: Calls (*proc)(host, held, param) for at least each host in the
+/* h_Enumerate: Calls (*proc)(host, param) for at least each host in the
  * system at the start of the enumeration (perhaps more).  Hosts may be deleted
  * (have delete flag set); ditto for clients.  refCount is always incremented
- * before (*proc) is called.  The param flags is passed to (*proc) as the
- * param flags, permitting (*proc) to stop the enumeration (BAIL).
- *
- * Needed?  Why not always h_Hold_r and h_Release_r in (*proc), or even -never-
- * h_Hold_r or h_Release_r in (*proc)?
+ * before (*proc) is called.
  *
- * **The proc should return 0 if the host should be released, 1 if it should
- * be held after enumeration.
+ * The return value of the proc is a set of flags. The proc should set
+ * H_ENUMERATE_BAIL(foo) if the enumeration of hosts should be stopped early.
  */
 void
-h_Enumerate(int (*proc) (struct host*, int, void *), void *param)
+h_Enumerate(int (*proc) (struct host*, void *), void *param)
 {
     struct host *host, **list;
-    int *flags;
     int i, count;
     int totalCount;
 
@@ -977,15 +960,9 @@ h_Enumerate(int (*proc) (struct host*, int, void *), void *param)
        H_UNLOCK;
        return;
     }
-    list = (struct host **)malloc(hostCount * sizeof(struct host *));
+    list = malloc(hostCount * sizeof(struct host *));
     if (!list) {
-       ViceLog(0, ("Failed malloc in h_Enumerate (list)\n"));
-       osi_Panic("Failed malloc in h_Enumerate (list)\n");
-    }
-    flags = (int *)malloc(hostCount * sizeof(int));
-    if (!flags) {
-       ViceLog(0, ("Failed malloc in h_Enumerate (flags)\n"));
-       osi_Panic("Failed malloc in h_Enumerate (flags)\n");
+       ViceLogThenPanic(0, ("Failed malloc in h_Enumerate (list)\n"));
     }
     for (totalCount = count = 0, host = hostList;
          host && totalCount < hostCount;
@@ -1005,43 +982,51 @@ h_Enumerate(int (*proc) (struct host*, int, void *), void *param)
     }
     H_UNLOCK;
     for (i = 0; i < count; i++) {
-       flags[i] = (*proc) (list[i], flags[i], param);
+       int flags;
+       flags = (*proc) (list[i], param);
        H_LOCK;
        h_Release_r(list[i]);
        H_UNLOCK;
        /* bail out of the enumeration early */
-       if (H_ENUMERATE_ISSET_BAIL(flags[i]))
+       if (H_ENUMERATE_ISSET_BAIL(flags)) {
            break;
+       } else if (flags) {
+           ViceLog(0, ("h_Enumerate got back invalid return value %d\n", flags));
+           ShutDownAndCore(PANIC);
+       }
     }
-    free((void *)list);
-    free((void *)flags);
+    if (i < count-1) {
+       /* we bailed out of enumerating hosts early; we still have holds on
+        * some of the hosts in 'list', so release them */
+       i++;
+       H_LOCK;
+       for ( ; i < count; i++) {
+           h_Release_r(list[i]);
+       }
+       H_UNLOCK;
+    }
+    free(list);
 }      /* h_Enumerate */
 
 
 /* h_Enumerate_r (revised):
- * Calls (*proc)(host, flags, param) for each host in hostList, starting
+ * Calls (*proc)(host, param) for each host in hostList, starting
  * at enumstart. Called only under H_LOCK.  Hosts may be deleted (have
  * delete flag set); ditto for clients.  refCount is always incremented
- * before (*proc) is called.  The param flags is passed to (*proc) as the
- * param flags, permitting (*proc) to stop the enumeration (BAIL).
- *
- * Needed?  Why not always h_Hold_r and h_Release_r in (*proc), or even -never-
- * h_Hold_r or h_Release_r in (*proc)?
+ * before (*proc) is called.
  *
  * @note Assumes that hostList is only prepended to, that a host is never
  *       inserted into the middle. Otherwise this would not be guaranteed to
  *       terminate.
  *
- * **The proc should return 0 if the host should be released, 1 if it should
- * be held after enumeration.
+ * The return value of the proc is a set of flags. The proc should set
+ * H_ENUMERATE_BAIL(foo) if the enumeration of hosts should be stopped early.
  */
 void
-h_Enumerate_r(int (*proc) (struct host *, int, void *),
+h_Enumerate_r(int (*proc) (struct host *, void *),
              struct host *enumstart, void *param)
 {
     struct host *host, *next;
-    int flags = 0;
-    int nflags = 0;
     int count;
     int origHostCount;
 
@@ -1080,7 +1065,7 @@ h_Enumerate_r(int (*proc) (struct host *, int, void *),
      * h_Release_r */
     origHostCount = hostCount;
 
-    for (count = 0, host = enumstart; host && count < origHostCount; host = next, flags = nflags, count++) {
+    for (count = 0, host = enumstart; host && count < origHostCount; host = next, count++) {
        next = host->next;
 
        /* find the next non-deleted host */
@@ -1092,14 +1077,21 @@ h_Enumerate_r(int (*proc) (struct host *, int, void *),
                ShutDownAndCore(PANIC);
            }
        }
-       if (next && !H_ENUMERATE_ISSET_BAIL(flags))
+       if (next)
            h_Hold_r(next);
 
        if (!(host->hostFlags & HOSTDELETED)) {
-           flags = (*proc) (host, flags, param);
+           int flags;
+           flags = (*proc) (host, param);
            if (H_ENUMERATE_ISSET_BAIL(flags)) {
                h_Release_r(host); /* this might free up the host */
+               if (next) {
+                   h_Release_r(next);
+               }
                break;
+           } else if (flags) {
+               ViceLog(0, ("h_Enumerate_r got back invalid return value %d\n", flags));
+               ShutDownAndCore(PANIC);
            }
        }
        h_Release_r(host); /* this might free up the host */
@@ -1144,10 +1136,9 @@ h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
     }
 
     /* insert into beginning of list for this bucket */
-    chain = (struct h_UuidHashChain *)malloc(sizeof(struct h_UuidHashChain));
+    chain = malloc(sizeof(struct h_UuidHashChain));
     if (!chain) {
-       ViceLog(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
-       osi_Panic("Failed malloc in h_AddHostToUuidHashTable_r\n");
+       ViceLogThenPanic(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
     }
     chain->hostPtr = host;
     chain->next = hostUuidHashTable[index];
@@ -1179,7 +1170,7 @@ h_DeleteHostFromUuidHashTable_r(struct host *host)
      if (LogLevel >= 125)
         afsUUID_to_string(&host->interface->uuid, uuid1, 127);
      for (uhp = &hostUuidHashTable[index]; (uth = *uhp); uhp = &uth->next) {
-         osi_Assert(uth->hostPtr);
+         opr_Assert(uth->hostPtr);
         if (uth->hostPtr == host) {
             ViceLog(125,
                     ("h_DeleteHostFromUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s %s:%d)\n",
@@ -1210,8 +1201,8 @@ invalidateInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
     struct Interface *interface;
     char hoststr[16], hoststr2[16];
 
-    osi_Assert(host);
-    osi_Assert(host->interface);
+    opr_Assert(host);
+    opr_Assert(host->interface);
 
     ViceLog(125, ("invalidateInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n",
                  host, afs_inet_ntoa_r(host->host, hoststr),
@@ -1308,12 +1299,7 @@ removeAddress_r(struct host *host, afs_uint32 addr, afs_uint16 port)
                     rxconn = NULL;
                 }
 
-                if (!sc)
-                    sc = rxnull_NewClientSecurityObject();
-                host->callback_rxcon =
-                    rx_NewConnection(host->host, host->port, 1, sc, 0);
-                rx_SetConnDeadTime(host->callback_rxcon, 50);
-                rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
+               h_SetupCallbackConn_r(host);
             }
         } else {
             /* not the primary addr/port, just invalidate it */
@@ -1331,10 +1317,9 @@ createHostAddrHashChain_r(int index, afs_uint32 addr, afs_uint16 port, struct ho
     char hoststr[16];
 
     /* insert into beginning of list for this bucket */
-    chain = (struct h_AddrHashChain *)malloc(sizeof(struct h_AddrHashChain));
+    chain = malloc(sizeof(struct h_AddrHashChain));
     if (!chain) {
-       ViceLog(0, ("Failed malloc in h_AddHostToAddrHashTable_r\n"));
-       osi_Panic("Failed malloc in h_AddHostToAddrHashTable_r\n");
+       ViceLogThenPanic(0, ("Failed malloc in h_AddHostToAddrHashTable_r\n"));
     }
     chain->hostPtr = host;
     chain->next = hostAddrHashTable[index];
@@ -1370,7 +1355,7 @@ reconcileHosts_r(afs_uint32 addr, afs_uint16 port, struct host *newHost,
             AFS_PTR_FMT, afs_inet_ntoa_r(addr, hoststr), ntohs(port),
             newHost, oldHost));
 
-    osi_Assert(oldHost != newHost);
+    opr_Assert(oldHost != newHost);
     caps.Capabilities_val = NULL;
 
     if (!sc) {
@@ -1520,8 +1505,8 @@ addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
     struct Interface *interface;
     char hoststr[16], hoststr2[16];
 
-    osi_Assert(host);
-    osi_Assert(host->interface);
+    opr_Assert(host);
+    opr_Assert(host->interface);
 
     /*
      * Make sure this address is on the list of known addresses
@@ -1551,11 +1536,10 @@ addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
                  ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
                  ntohs(port)));
 
-    interface = (struct Interface *)
-       malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
+    interface = malloc(sizeof(struct Interface)
+                      + (sizeof(struct AddrPort) * number));
     if (!interface) {
-       ViceLog(0, ("Failed malloc in addInterfaceAddr_r\n"));
-       osi_Panic("Failed malloc in addInterfaceAddr_r\n");
+       ViceLogThenPanic(0, ("Failed malloc in addInterfaceAddr_r\n"));
     }
     interface->numberOfInterfaces = number + 1;
     interface->uuid = host->interface->uuid;
@@ -1587,8 +1571,8 @@ removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
     struct Interface *interface;
     char hoststr[16], hoststr2[16];
 
-    osi_Assert(host);
-    osi_Assert(host->interface);
+    opr_Assert(host);
+    opr_Assert(host->interface);
 
     ViceLog(125, ("removeInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n",
                  host, afs_inet_ntoa_r(host->host, hoststr),
@@ -1623,18 +1607,8 @@ removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
 static int
 h_threadquota(int waiting)
 {
-    if (lwps > 64) {
-       if (waiting > 5)
-           return 1;
-    } else if (lwps > 32) {
-       if (waiting > 4)
-           return 1;
-    } else if (lwps > 16) {
-       if (waiting > 3)
-           return 1;
-    } else {
-       if (waiting > 2)
-           return 1;
+    if (waiting > h_quota_limit) {
+       return 1;
     }
     return 0;
 }
@@ -1734,6 +1708,7 @@ h_GetHost_r(struct rx_connection *tcon)
             cb_in = rx_NewConnection(haddr, hport, 1, sc, 0);
             rx_SetConnDeadTime(cb_in, 50);
             rx_SetConnHardDeadTime(cb_in, AFS_HARDDEADTIME);
+           rx_SetConnSecondsUntilNatPing(cb_in, 20);
 
             code =
                 RXAFSCB_TellMeAboutYourself(cb_in, &interf, &caps);
@@ -1745,10 +1720,9 @@ h_GetHost_r(struct rx_connection *tcon)
        H_LOCK;
        if ((code == RXGEN_OPCODE) ||
            ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
-           identP = (struct Identity *)malloc(sizeof(struct Identity));
+           identP = malloc(sizeof(struct Identity));
            if (!identP) {
-               ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
-               osi_Panic("Failed malloc in h_GetHost_r\n");
+               ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
            }
            identP->valid = 0;
            rx_SetSpecific(tcon, rxcon_ident_key, identP);
@@ -1787,10 +1761,9 @@ h_GetHost_r(struct rx_connection *tcon)
            }
        } else if (code == 0) {
            interfValid = 1;
-           identP = (struct Identity *)malloc(sizeof(struct Identity));
+           identP = malloc(sizeof(struct Identity));
            if (!identP) {
-               ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
-               osi_Panic("Failed malloc in h_GetHost_r\n");
+               ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
            }
            identP->valid = 1;
            identP->uuid = interf.uuid;
@@ -1955,7 +1928,9 @@ h_GetHost_r(struct rx_connection *tcon)
        }
     } else {
        host = h_Alloc_r(tcon); /* returned held and locked */
-       h_gethostcps_r(host, FT_ApproxTime());
+       if (!host)
+           goto gethost_out;
+       h_gethostcps_r(host, time(NULL));
        if (!(host->Console & 1)) {
            int pident = 0;
            cb_conn = host->callback_rxcon;
@@ -1972,14 +1947,12 @@ h_GetHost_r(struct rx_connection *tcon)
            if ((code == RXGEN_OPCODE) ||
                ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
                if (!identP)
-                   identP =
-                       (struct Identity *)malloc(sizeof(struct Identity));
+                   identP = malloc(sizeof(struct Identity));
                else
                    pident = 1;
 
                if (!identP) {
-                   ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
-                   osi_Panic("Failed malloc in h_GetHost_r\n");
+                   ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
                }
                identP->valid = 0;
                if (!pident)
@@ -1991,14 +1964,12 @@ h_GetHost_r(struct rx_connection *tcon)
                code = 0;
            } else if (code == 0) {
                if (!identP)
-                   identP =
-                       (struct Identity *)malloc(sizeof(struct Identity));
+                   identP = malloc(sizeof(struct Identity));
                else
                    pident = 1;
 
                if (!identP) {
-                   ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
-                   osi_Panic("Failed malloc in h_GetHost_r\n");
+                   ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
                }
                identP->valid = 1;
                interfValid = 1;
@@ -2034,6 +2005,17 @@ h_GetHost_r(struct rx_connection *tcon)
                if (oldHost) {
                    int probefail = 0;
 
+                   /* This is a new address for an existing host. Update
+                    * the list of interfaces for the existing host and
+                    * delete the host structure we just allocated. */
+
+                   /* mark the duplicate host as deleted before we do
+                    * anything. The probing code below may try to change
+                    * "oldHost" to the same IP address as "host" currently
+                    * has, and we do not want a pseudo-"collision" to be
+                    * noticed. */
+                   host->hostFlags |= HOSTDELETED;
+
                    oldHost->hostFlags |= HWHO_INPROGRESS;
 
                     if (oldHost->interface) {
@@ -2060,20 +2042,22 @@ h_GetHost_r(struct rx_connection *tcon)
                                         oldHost,
                                          afs_inet_ntoa_r(oldHost->host, hoststr),
                                         ntohs(oldHost->port),code2));
-                           MultiProbeAlternateAddress_r(oldHost);
-                            probefail = 1;
+
+                           if (MultiProbeAlternateAddress_r(oldHost)) {
+                               /* If MultiProbeAlternateAddress_r succeeded,
+                                * it updated oldHost->host and oldHost->port
+                                * to an address that responded successfully to
+                                * a ProbeUuid, so it is as if the ProbeUuid
+                                * call above returned success. So, only set
+                                * 'probefail' if MultiProbeAlternateAddress_r
+                                * fails. */
+                               probefail = 1;
+                           }
                         }
                     } else {
                         probefail = 1;
                     }
 
-                   /* This is a new address for an existing host. Update
-                    * the list of interfaces for the existing host and
-                    * delete the host structure we just allocated. */
-
-                    /* prevent warnings while manipulating interface lists */
-                   host->hostFlags |= HOSTDELETED;
-
                    if (oldHost->host != haddr || oldHost->port != hport) {
                        struct rx_connection *rxconn;
 
@@ -2117,7 +2101,6 @@ h_GetHost_r(struct rx_connection *tcon)
                                }
                            }
                        }
-                       h_AddHostToAddrHashTable_r(haddr, hport, oldHost);
                        oldHost->host = haddr;
                        oldHost->port = hport;
                        rxconn = oldHost->callback_rxcon;
@@ -2135,7 +2118,9 @@ h_GetHost_r(struct rx_connection *tcon)
                    /* the new host is held and locked */
                } else {
                    /* This really is a new host */
-                   h_AddHostToUuidHashTable_r(&identP->uuid, host);
+                   opr_Assert(interfValid == 1);
+                   initInterfaceAddr_r(host, &interf);
+
                    cb_conn = host->callback_rxcon;
                    rx_GetConnection(cb_conn);
                    H_UNLOCK;
@@ -2150,8 +2135,6 @@ h_GetHost_r(struct rx_connection *tcon)
                                ("InitCallBackState3 success on host %" AFS_PTR_FMT " (%s:%d)\n",
                                 host, afs_inet_ntoa_r(host->host, hoststr),
                                 ntohs(host->port)));
-                       osi_Assert(interfValid == 1);
-                       initInterfaceAddr_r(host, &interf);
                    }
                }
            }
@@ -2191,87 +2174,31 @@ h_GetHost_r(struct rx_connection *tcon)
 }                              /*h_GetHost_r */
 
 
-static char localcellname[PR_MAXNAMELEN + 1];
-char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
-int  num_lrealms = -1;
-
 /* not reentrant */
 void
-h_InitHostPackage(void)
+h_InitHostPackage(int hquota)
 {
-    memset(&nulluuid, 0, sizeof(afsUUID));
-    afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
-    if (num_lrealms == -1) {
-       int i;
-       for (i=0; i<AFS_NUM_LREALMS; i++) {
-           if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
-               break;
-       }
+    opr_Assert(hquota > 0);
+    h_quota_limit = hquota;
 
-       if (i == 0) {
-           ViceLog(0,
-                   ("afs_krb_get_lrealm failed, using %s.\n",
-                    localcellname));
-           strncpy(local_realms[0], localcellname, AFS_REALM_SZ);
-           num_lrealms = i =1;
-       } else {
-           num_lrealms = i;
-       }
-
-       /* initialize the rest of the local realms to nullstring for debugging */
-       for (; i<AFS_NUM_LREALMS; i++)
-           local_realms[i][0] = '\0';
-    }
+    memset(&nulluuid, 0, sizeof(afsUUID));
     rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
     rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
-    MUTEX_INIT(&host_glock_mutex, "host glock", MUTEX_DEFAULT, 0);
+    opr_mutex_init(&host_glock_mutex);
 }
 
 static int
-MapName_r(char *aname, char *acell, afs_int32 * aval)
+MapName_r(char *uname, afs_int32 * aval)
 {
     namelist lnames;
     idlist lids;
     afs_int32 code;
-    afs_int32 anamelen, cnamelen;
-    int foreign = 0;
-    char *tname;
-
-    anamelen = strlen(aname);
-    if (anamelen >= PR_MAXNAMELEN)
-       return -1;              /* bad name -- caller interprets this as anonymous, but retries later */
 
     lnames.namelist_len = 1;
-    lnames.namelist_val = (prname *) aname;    /* don't malloc in the common case */
+    lnames.namelist_val = (prname *) uname;
     lids.idlist_len = 0;
     lids.idlist_val = NULL;
 
-    cnamelen = strlen(acell);
-    if (cnamelen) {
-       if (afs_is_foreign_ticket_name(aname, NULL, acell, localcellname)) {
-           ViceLog(2,
-                   ("MapName: cell is foreign.  cell=%s, localcell=%s, localrealms={%s,%s,%s,%s}\n",
-                   acell, localcellname, local_realms[0],local_realms[1],local_realms[2],local_realms[3]));
-           if ((anamelen + cnamelen + 1) >= PR_MAXNAMELEN) {
-               ViceLog(2,
-                       ("MapName: Name too long, using AnonymousID for %s@%s\n",
-                        aname, acell));
-               *aval = AnonymousID;
-               return 0;
-           }
-           foreign = 1;        /* attempt cross-cell authentication */
-           tname = (char *)malloc(PR_MAXNAMELEN);
-           if (!tname) {
-               ViceLog(0, ("Failed malloc in MapName_r\n"));
-               osi_Panic("Failed malloc in MapName_r\n");
-           }
-           strcpy(tname, aname);
-           tname[anamelen] = '@';
-           strcpy(tname + anamelen + 1, acell);
-           lnames.namelist_val = (prname *) tname;
-       }
-    }
-
     H_UNLOCK;
     code = hpr_NameToId(&lnames, &lids);
     H_LOCK;
@@ -2291,46 +2218,148 @@ MapName_r(char *aname, char *acell, afs_int32 * aval)
            code = -1;
        }
     }
-
-    if (foreign) {
-       free(lnames.namelist_val);      /* We allocated this above, so we must free it now. */
-    }
     return code;
 }
 
 /*MapName*/
 
 
-/* NOTE: this returns the client with a Write lock and a refCount */
-struct client *
-h_ID2Client(afs_int32 vid)
+static int
+PerHost_EnumerateClient(struct host *host, void *arock)
 {
+    struct enumclient_args *args = arock;
     struct client *client;
-    struct host *host;
-    int count;
+    int code;
 
-    H_LOCK;
-    for (count = 0, host = hostList; host && count < hostCount; host = host->next, count++) {
-       if (host->hostFlags & HOSTDELETED)
-           continue;
-       for (client = host->FirstClient; client; client = client->next) {
-           if (!client->deleted && client->ViceId == vid) {
-               client->refCount++;
-               H_UNLOCK;
-               ObtainWriteLock(&client->lock);
-               return client;
+    for (client = host->FirstClient; client; client = client->next) {
+       if (!client->deleted && client->ViceId == args->vid) {
+
+           client->refCount++;
+           H_UNLOCK;
+
+           code = (*args->proc)(client, args->rock);
+
+           H_LOCK;
+           h_ReleaseClient_r(client);
+
+           if (code) {
+               return H_ENUMERATE_BAIL(0);
            }
        }
     }
-    if (count != hostCount) {
-       ViceLog(0, ("h_ID2Client found %d of %d hosts\n", count, hostCount));
-    } else if (host != NULL) {
-       ViceLog(0, ("h_ID2Client found more than %d hosts\n", hostCount));
-       ShutDownAndCore(PANIC);
-    }
 
+    return 0;
+}
+
+void
+h_EnumerateClients(VolumeId vid,
+                   int (*proc)(struct client *client, void *rock),
+                   void *rock)
+{
+    struct enumclient_args args;
+    args.vid = vid;
+    args.proc = proc;
+    args.rock = rock;
+
+    H_LOCK;
+    h_Enumerate_r(PerHost_EnumerateClient, hostList, &args);
     H_UNLOCK;
-    return NULL;
+}
+
+static int
+format_vname(char *vname, int usize, const char *tname, const char *tinst,
+            const char *tcell, afs_int32 islocal)
+{
+    int len;
+
+    len = strlcpy(vname, tname, usize);
+    if (len >= usize)
+       return -1;
+    if (tinst[0]) {
+       len = strlcat(vname, ".", usize);
+       if (len >= usize)
+           return -1;
+       len = strlcat(vname, tinst, usize);
+       if (len >= usize)
+           return -1;
+    }
+    if (tcell[0] && !islocal) {
+       len = strlcat(vname, "@", usize);
+       if (len >= usize)
+           return -1;
+       len = strlcat(vname, tcell, usize);
+       if (len >= usize)
+           return -1;
+    }
+    return 0;
+}
+
+static int
+getPeerDetails(struct rx_connection *conn,
+              afs_int32 *viceid, afs_int32 *expTime, int authClass)
+{
+    int code;
+#if (64-MAXKTCNAMELEN)
+    ticket name length != 64
+#endif
+    char tname[64];
+    char tinst[64];
+    char tcell[MAXKTCREALMLEN];
+    char uname[PR_MAXNAMELEN];
+
+    *viceid = AnonymousID;
+    *expTime = 0x7fffffff;
+
+    ViceLog(5,
+           ("FindClient: authenticating connection: authClass=%d\n",
+            authClass));
+    if (authClass == 1) {
+       /* A bcrypt tickets, no longer supported */
+       ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
+       return 0;
+    }
+
+    if (authClass == 2) {
+       /* an rxkad ticket */
+       afs_int32 kvno;
+       afs_int32 islocal;
+
+       /* kerberos ticket */
+       code = rxkad_GetServerInfo(conn, /*level */ 0, (afs_uint32 *)expTime,
+                                  tname, tinst, tcell, &kvno);
+       if (code) {
+           ViceLog(1, ("Failed to get rxkad ticket info\n"));
+           return 0;
+       }
+
+       ViceLog(5,
+               ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
+                tname, tinst, tcell, *expTime, kvno));
+       code = afsconf_IsLocalRealmMatch(confDir, &islocal, tname, tinst, tcell);
+
+       if (code) {
+           ViceLog(0, ("FindClient: local realm check failed; code=%d", code));
+           return 0;
+       }
+
+       code = format_vname(uname, sizeof(uname), tname, tinst, tcell, islocal);
+       if (code) {
+           ViceLog(0, ("FindClient: uname truncated."));
+           return 0;
+       }
+
+       /* translate the name to a vice id */
+       code = MapName_r(uname, viceid);
+       if (code) {
+           ViceLog(1, ("failed to map name=%s -> code=%d\n", uname,
+                       code));
+           return code; /* Actually flag this is a failure */
+       }
+
+       return 0;
+    }
+
+    return 0;
 }
 
 /*
@@ -2342,9 +2371,12 @@ h_ID2Client(afs_int32 vid)
  *
  * The refCount on client->host is returned incremented.  h_ReleaseClient_r
  * does not decrement the refCount on client->host.
+ *
+ * *a_viceid is set to the user's ViceId, even if we don't return a client
+ * struct.
  */
 struct client *
-h_FindClient_r(struct rx_connection *tcon)
+h_FindClient_r(struct rx_connection *tcon, afs_int32 *a_viceid)
 {
     struct client *client;
     struct host *host = NULL;
@@ -2353,24 +2385,22 @@ h_FindClient_r(struct rx_connection *tcon)
     afs_int32 expTime;
     afs_int32 code;
     int authClass;
-#if (64-MAXKTCNAMELEN)
-    ticket name length != 64
-#endif
-    char tname[64];
-    char tinst[64];
-    char uname[PR_MAXNAMELEN];
-    char tcell[MAXKTCREALMLEN];
+
     int fail = 0;
     int created = 0;
 
     client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
-    if (client && client->sid == rxr_CidOf(tcon)
-       && client->VenusEpoch == rxr_GetEpoch(tcon)
-       && !(client->host->hostFlags & HOSTDELETED)) {
+    if (client && client->sid == rx_GetConnectionId(tcon)
+       && client->VenusEpoch == rx_GetConnectionEpoch(tcon)
+       && !(client->host->hostFlags & HOSTDELETED)
+       && !client->deleted) {
 
+       if (a_viceid) {
+           *a_viceid = client->ViceId;
+       }
        client->refCount++;
        h_Hold_r(client->host);
-       if (!client->deleted && client->prfail != 2) {
+       if (client->prfail != 2) {
            /* Could add shared lock on client here */
            /* note that we don't have to lock entry in this path to
             * ensure CPS is initialized, since we don't call rx_SetSpecific
@@ -2386,52 +2416,14 @@ h_FindClient_r(struct rx_connection *tcon)
        client = NULL;
     }
 
-    authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
-    ViceLog(5,
-           ("FindClient: authenticating connection: authClass=%d\n",
-            authClass));
-    if (authClass == 1) {
-       /* A bcrypt tickets, no longer supported */
-       ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
-       viceid = AnonymousID;
-       expTime = 0x7fffffff;
-    } else if (authClass == 2) {
-       afs_int32 kvno;
+    authClass = rx_SecurityClassOf(tcon);
 
-       /* kerberos ticket */
-       code = rxkad_GetServerInfo(tcon, /*level */ 0, (afs_uint32 *)&expTime,
-                                  tname, tinst, tcell, &kvno);
-       if (code) {
-           ViceLog(1, ("Failed to get rxkad ticket info\n"));
-           viceid = AnonymousID;
-           expTime = 0x7fffffff;
-       } else {
-           int ilen = strlen(tinst);
-           ViceLog(5,
-                   ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
-                    tname, tinst, tcell, expTime, kvno));
-           strncpy(uname, tname, sizeof(uname));
-           if (ilen) {
-               if (strlen(uname) + 1 + ilen >= sizeof(uname))
-                   goto bad_name;
-               strcat(uname, ".");
-               strcat(uname, tinst);
-           }
-           /* translate the name to a vice id */
-           code = MapName_r(uname, tcell, &viceid);
-           if (code) {
-             bad_name:
-               ViceLog(1,
-                       ("failed to map name=%s, cell=%s -> code=%d\n", uname,
-                        tcell, code));
-               fail = 1;
-               viceid = AnonymousID;
-               expTime = 0x7fffffff;
-           }
-       }
-    } else {
-       viceid = AnonymousID;   /* unknown security class */
-       expTime = 0x7fffffff;
+    code = getPeerDetails(tcon, &viceid, &expTime, authClass);
+    if (code)
+       fail = 1;
+
+    if (a_viceid) {
+       *a_viceid = viceid;
     }
 
     if (!client) { /* loop */
@@ -2443,8 +2435,8 @@ h_FindClient_r(struct rx_connection *tcon)
     retryfirstclient:
        /* First try to find the client structure */
        for (client = host->FirstClient; client; client = client->next) {
-           if (!client->deleted && (client->sid == rxr_CidOf(tcon))
-               && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
+           if (!client->deleted && (client->sid == rx_GetConnectionId(tcon))
+               && (client->VenusEpoch == rx_GetConnectionEpoch(tcon))) {
                client->refCount++;
                H_UNLOCK;
                ObtainWriteLock(&client->lock);
@@ -2463,8 +2455,8 @@ h_FindClient_r(struct rx_connection *tcon)
             }
            /* Retry to find the client structure */
            for (client = host->FirstClient; client; client = client->next) {
-               if (!client->deleted && (client->sid == rxr_CidOf(tcon))
-                   && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
+               if (!client->deleted && (client->sid == rx_GetConnectionId(tcon))
+                   && (client->VenusEpoch == rx_GetConnectionEpoch(tcon))) {
                    h_Unlock_r(host);
                    goto retryfirstclient;
                }
@@ -2474,14 +2466,12 @@ h_FindClient_r(struct rx_connection *tcon)
            ObtainWriteLock(&client->lock);
            client->refCount = 1;
            client->host = host;
-#if FS_STATS_DETAILED
            client->InSameNetwork = host->InSameNetwork;
-#endif /* FS_STATS_DETAILED */
            client->ViceId = viceid;
            client->expTime = expTime;  /* rx only */
            client->authClass = authClass;      /* rx only */
-           client->sid = rxr_CidOf(tcon);
-           client->VenusEpoch = rxr_GetEpoch(tcon);
+           client->sid = rx_GetConnectionId(tcon);
+           client->VenusEpoch = rx_GetConnectionEpoch(tcon);
            client->CPS.prlist_val = NULL;
            client->CPS.prlist_len = 0;
            h_Unlock_r(host);
@@ -2542,8 +2532,10 @@ h_FindClient_r(struct rx_connection *tcon)
      * the RPC from the other client structure's rock.
      */
     oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
-    if (oldClient && oldClient != client && oldClient->sid == rxr_CidOf(tcon)
-       && oldClient->VenusEpoch == rxr_GetEpoch(tcon)) {
+    if (oldClient && oldClient != client
+       && oldClient->sid == rx_GetConnectionId(tcon)
+       && oldClient->VenusEpoch == rx_GetConnectionEpoch(tcon)
+       && !(oldClient->host->hostFlags & HOSTDELETED)) {
        char hoststr[16];
        if (!oldClient->deleted) {
            /* if we didn't create it, it's not ours to put back */
@@ -2567,17 +2559,25 @@ h_FindClient_r(struct rx_connection *tcon)
                created = 0;
            }
            oldClient->refCount++;
+
+           h_Hold_r(oldClient->host);
+           h_Release_r(client->host);
+
            H_UNLOCK;
            ObtainWriteLock(&oldClient->lock);
            H_LOCK;
            client = oldClient;
+           host = oldClient->host;
        } else {
-           ViceLog(0, ("FindClient: deleted client %p(%x) already had "
-                       "conn %p (host %s:%d), stolen by client %p(%x)\n",
-                       oldClient, oldClient->sid, tcon,
-                       afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
-                       ntohs(rxr_PortOf(tcon)),
-                       client, client->sid));
+           ViceLog(0, ("FindClient: deleted client %p(%x ref %d host %p href "
+                       "%d) already had conn %p (host %s:%d, cid %x), stolen "
+                       "by client %p(%x, ref %d host %p href %d)\n",
+                       oldClient, oldClient->sid, oldClient->refCount,
+                       oldClient->host, oldClient->host->refCount, tcon,
+                       afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
+                       ntohs(rxr_PortOf(tcon)), rx_GetConnectionId(tcon),
+                       client, client->sid, client->refCount,
+                       client->host, client->host->refCount));
            /* rx_SetSpecific will be done immediately below */
        }
     }
@@ -2618,7 +2618,7 @@ h_FindClient_r(struct rx_connection *tcon)
 int
 h_ReleaseClient_r(struct client *client)
 {
-    osi_Assert(client->refCount > 0);
+    opr_Assert(client->refCount > 0);
     client->refCount--;
     return 0;
 }
@@ -2648,10 +2648,11 @@ GetClient(struct rx_connection *tcon, struct client **cp)
        H_UNLOCK;
        return VBUSY;
     }
-    if (rxr_CidOf(tcon) != client->sid || rxr_GetEpoch(tcon) != client->VenusEpoch) {
+    if (rx_GetConnectionId(tcon) != client->sid
+       || rx_GetConnectionEpoch(tcon) != client->VenusEpoch) {
        ViceLog(0,
                ("GetClient: tcon %p tcon sid %d client sid %d\n",
-                tcon, rxr_CidOf(tcon), client->sid));
+                tcon, rx_GetConnectionId(tcon), client->sid));
        H_UNLOCK;
        return VBUSY;
     }
@@ -2663,6 +2664,16 @@ GetClient(struct rx_connection *tcon, struct client **cp)
        H_UNLOCK;
        return VICETOKENDEAD;
     }
+    if (client->deleted) {
+       ViceLog(0, ("GetClient: got deleted client, connection will appear "
+                   "anonymous; tcon %p cid %x client %p ref %d host %p "
+                   "(%s:%d) href %d ViceId %d\n",
+                   tcon, rx_GetConnectionId(tcon), client, client->refCount,
+                   client->host,
+                   afs_inet_ntoa_r(client->host->host, hoststr),
+                   (int)ntohs(client->host->port), client->host->refCount,
+                   (int)client->ViceId));
+    }
 
     client->refCount++;
     *cp = client;
@@ -2693,10 +2704,9 @@ h_UserName(struct client *client)
     idlist lids;
 
     lids.idlist_len = 1;
-    lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
+    lids.idlist_val = malloc(1 * sizeof(afs_int32));
     if (!lids.idlist_val) {
-       ViceLog(0, ("Failed malloc in h_UserName\n"));
-       osi_Panic("Failed malloc in h_UserName\n");
+       ViceLogThenPanic(0, ("Failed malloc in h_UserName\n"));
     }
     lnames.namelist_len = 0;
     lnames.namelist_val = (prname *) 0;
@@ -2724,7 +2734,7 @@ h_PrintStats(void)
 
 
 static int
-h_PrintClient(struct host *host, int flags, void *rock)
+h_PrintClient(struct host *host, void *rock)
 {
     StreamHandle_t *file = (StreamHandle_t *)rock;
     struct client *client;
@@ -2739,11 +2749,11 @@ h_PrintClient(struct host *host, int flags, void *rock)
     LastCall = host->LastCall;
     if (host->hostFlags & HOSTDELETED) {
        H_UNLOCK;
-       return flags;
+       return 0;
     }
-    strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y",
+    strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
             localtime_r(&LastCall, &tm));
-    snprintf(tmpStr, sizeof tmpStr, "Host %s:%d down = %d, LastCall %s",
+    snprintf(tmpStr, sizeof tmpStr, "Host %s:%d down = %d, LastCall %s\n",
             afs_inet_ntoa_r(host->host, hoststr),
             ntohs(host->port), (host->hostFlags & VENUSDOWN),
             tbuffer);
@@ -2751,14 +2761,14 @@ h_PrintClient(struct host *host, int flags, void *rock)
     for (client = host->FirstClient; client; client = client->next) {
        if (!client->deleted) {
            expTime = client->expTime;
-           strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y",
+           strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
                     localtime_r(&expTime, &tm));
            snprintf(tmpStr, sizeof tmpStr,
-                    "    user id=%d,  name=%s, sl=%s till %s",
+                    "    user id=%d,  name=%s, sl=%s till %s\n",
                     client->ViceId, h_UserName(client),
                     client->authClass ? "Authenticated"
                                       : "Not authenticated",
-                    client->authClass ? tbuffer : "No Limit\n");
+                    client->authClass ? tbuffer : "No Limit");
            (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
            snprintf(tmpStr, sizeof tmpStr, "      CPS-%d is [",
                         client->CPS.prlist_len);
@@ -2775,7 +2785,7 @@ h_PrintClient(struct host *host, int flags, void *rock)
        }
     }
     H_UNLOCK;
-    return flags;
+    return 0;
 
 }                              /*h_PrintClient */
 
@@ -2801,10 +2811,10 @@ h_PrintClients(void)
                 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
        return;
     }
-    now = FT_ApproxTime();
-    strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y",
+    now = time(NULL);
+    strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
             localtime_r(&now, &tm));
-    snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n",
+    snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n\n",
             tbuffer);
     (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
     h_Enumerate(h_PrintClient, (char *)file);
@@ -2816,7 +2826,7 @@ h_PrintClients(void)
 
 
 static int
-h_DumpHost(struct host *host, int flags, void *rock)
+h_DumpHost(struct host *host, void *rock)
 {
     StreamHandle_t *file = (StreamHandle_t *)rock;
 
@@ -2855,7 +2865,7 @@ h_DumpHost(struct host *host, int flags, void *rock)
     (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
 
     H_UNLOCK;
-    return flags;
+    return 0;
 
 }                              /*h_DumpHost */
 
@@ -2875,10 +2885,10 @@ h_DumpHosts(void)
                 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
        return;
     }
-    now = FT_ApproxTime();
-    strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y",
+    now = time(NULL);
+    strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
             localtime_r(&now, &tm));
-    snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n", tbuffer);
+    snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n\n", tbuffer);
     (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
     h_Enumerate(h_DumpHost, (char *)file);
     STREAM_REALLYCLOSE(file);
@@ -2894,16 +2904,47 @@ h_DumpHosts(void)
 static int h_stateFillHeader(struct host_state_header * hdr);
 static int h_stateCheckHeader(struct host_state_header * hdr);
 static int h_stateAllocMap(struct fs_dump_state * state);
-static int h_stateSaveHost(struct host * host, int flags, void *rock);
+static int h_stateSaveHost(struct host * host, void *rock);
 static int h_stateRestoreHost(struct fs_dump_state * state);
-static int h_stateRestoreIndex(struct host * h, int flags, void *rock);
-static int h_stateVerifyHost(struct host * h, int flags, void *rock);
+static int h_stateRestoreIndex(struct host * h, void *rock);
+static int h_stateVerifyHost(struct host * h, void *rock);
 static int h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h,
                                  afs_uint32 addr, afs_uint16 port, int valid);
 static int h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h);
 static void h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out);
 static void h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out);
 
+/**
+ * Is this host busy?
+ *
+ * This is just a hint and should not be trusted; this should probably only be
+ * used by the host state serialization code when trying to detect if a host
+ * can be sanely serialized to disk or not. If this function returns 1, the
+ * host may be in an invalid state and thus should not be saved to disk.
+ */
+static int
+h_isBusy_r(struct host *host)
+{
+    struct Lock *hostLock = &host->lock;
+    int locked = 0;
+
+    LOCK_LOCK(hostLock);
+    if (hostLock->excl_locked || hostLock->readers_reading) {
+       locked = 1;
+    }
+    LOCK_UNLOCK(hostLock);
+
+    if (locked) {
+       return 1;
+    }
+
+    if ((host->hostFlags & HWHO_INPROGRESS) || !(host->hostFlags & ALTADDR)) {
+       /* We shouldn't hit this if the host wasn't locked, but just in case... */
+       return 1;
+    }
+
+    return 0;
+}
 
 /* this procedure saves all host state to disk for fast startup */
 int
@@ -2990,13 +3031,13 @@ h_stateRestoreIndices(struct fs_dump_state * state)
 }
 
 static int
-h_stateRestoreIndex(struct host * h, int flags, void *rock)
+h_stateRestoreIndex(struct host * h, void *rock)
 {
     struct fs_dump_state *state = (struct fs_dump_state *)rock;
     if (cb_OldToNew(state, h->cblist, &h->cblist)) {
-       return H_ENUMERATE_BAIL(flags);
+       return H_ENUMERATE_BAIL(0);
     }
-    return flags;
+    return 0;
 }
 
 int
@@ -3007,14 +3048,14 @@ h_stateVerify(struct fs_dump_state * state)
 }
 
 static int
-h_stateVerifyHost(struct host * h, int flags, void* rock)
+h_stateVerifyHost(struct host * h, void* rock)
 {
     struct fs_dump_state *state = (struct fs_dump_state *)rock;
     int i;
 
     if (h == NULL) {
        ViceLog(0, ("h_stateVerifyHost: error: NULL host pointer in linked list\n"));
-       return H_ENUMERATE_BAIL(flags);
+       return H_ENUMERATE_BAIL(0);
     }
 
     if (h->interface) {
@@ -3036,7 +3077,7 @@ h_stateVerifyHost(struct host * h, int flags, void* rock)
        state->bail = 1;
     }
 
-    return flags;
+    return 0;
 }
 
 /**
@@ -3125,7 +3166,7 @@ h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h,
 static int
 h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
 {
-    int ret = 0, found = 0;
+    int ret = 0;
     struct host *host = NULL;
     struct h_UuidHashChain *chain;
     afsUUID * uuidp = &h->interface->uuid;
@@ -3148,7 +3189,6 @@ h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
                            h->index, host->index));
                state->flags.warnings_generated = 1;
            }
-           found = 1;
            goto done;
        }
        if (chain_len > FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN) {
@@ -3160,16 +3200,16 @@ h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
        chain_len++;
     }
 
-    if (!found) {
-       afsUUID_to_string(uuidp, tmp, sizeof(tmp));
-       if (state->mode == FS_STATE_LOAD_MODE) {
-           ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
-           ret = 1;
-           goto done;
-       } else {
-           ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
-           state->flags.warnings_generated = 1;
-       }
+    /* Fall through, so host not found */
+
+    afsUUID_to_string(uuidp, tmp, sizeof(tmp));
+    if (state->mode == FS_STATE_LOAD_MODE) {
+       ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
+       ret = 1;
+       goto done;
+    } else {
+       ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
+       state->flags.warnings_generated = 1;
     }
 
  done:
@@ -3214,7 +3254,7 @@ h_stateAllocMap(struct fs_dump_state * state)
 
 /* function called by h_Enumerate to save a host to disk */
 static int
-h_stateSaveHost(struct host * host, int flags, void* rock)
+h_stateSaveHost(struct host * host, void* rock)
 {
     struct fs_dump_state *state = (struct fs_dump_state *) rock;
     int if_len=0, hcps_len=0;
@@ -3225,6 +3265,16 @@ h_stateSaveHost(struct host * host, int flags, void* rock)
     struct iovec iov[4];
     int iovcnt = 2;
 
+    if (h_isBusy_r(host)) {
+       char hoststr[16];
+       ViceLog(1, ("Not saving host %s:%d to disk; host appears busy\n",
+                   afs_inet_ntoa_r(host->host, hoststr), (int)ntohs(host->port)));
+       /* Make sure we don't try to save callbacks to disk for this host, or
+        * we'll get confused on restore */
+       DeleteAllCallBacks_r(host, 1);
+       return 0;
+    }
+
     memset(&hdr, 0, sizeof(hdr));
 
     if (state->h_hdr->index_max < host->index) {
@@ -3235,8 +3285,8 @@ h_stateSaveHost(struct host * host, int flags, void* rock)
     if (host->interface) {
        if_len = sizeof(struct Interface) +
            ((host->interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
-       ifp = (struct Interface *) malloc(if_len);
-       osi_Assert(ifp != NULL);
+       ifp = malloc(if_len);
+       opr_Assert(ifp != NULL);
        memcpy(ifp, host->interface, if_len);
        hdr.interfaces = host->interface->numberOfInterfaces;
        iov[iovcnt].iov_base = (char *) ifp;
@@ -3246,8 +3296,8 @@ h_stateSaveHost(struct host * host, int flags, void* rock)
     if (host->hcps.prlist_val) {
        hdr.hcps = host->hcps.prlist_len;
        hcps_len = hdr.hcps * sizeof(afs_int32);
-       hcps = (afs_int32 *) malloc(hcps_len);
-       osi_Assert(hcps != NULL);
+       hcps = malloc(hcps_len);
+       opr_Assert(hcps != NULL);
        memcpy(hcps, host->hcps.prlist_val, hcps_len);
        iov[iovcnt].iov_base = (char *) hcps;
        iov[iovcnt].iov_len = hcps_len;
@@ -3280,9 +3330,9 @@ h_stateSaveHost(struct host * host, int flags, void* rock)
     if (hcps)
        free(hcps);
     if (state->bail) {
-       return H_ENUMERATE_BAIL(flags);
+       return H_ENUMERATE_BAIL(0);
     }
-    return flags;
+    return 0;
 }
 
 /* restores a host from disk */
@@ -3318,16 +3368,16 @@ h_stateRestoreHost(struct fs_dump_state * state)
     if (hdr.interfaces) {
        ifp_len = sizeof(struct Interface) +
            ((hdr.interfaces-1) * sizeof(struct AddrPort));
-       ifp = (struct Interface *) malloc(ifp_len);
-       osi_Assert(ifp != NULL);
+       ifp = malloc(ifp_len);
+       opr_Assert(ifp != NULL);
        iov[iovcnt].iov_base = (char *) ifp;
        iov[iovcnt].iov_len = ifp_len;
        iovcnt++;
     }
     if (hdr.hcps) {
        hcps_len = hdr.hcps * sizeof(afs_int32);
-       hcps = (afs_int32 *) malloc(hcps_len);
-       osi_Assert(hcps != NULL);
+       hcps = malloc(hcps_len);
+       opr_Assert(hcps != NULL);
        iov[iovcnt].iov_base = (char *) hcps;
        iov[iovcnt].iov_len = hcps_len;
        iovcnt++;
@@ -3347,12 +3397,23 @@ h_stateRestoreHost(struct fs_dump_state * state)
 
     if (!hdr.hcps && hdsk.hcps_valid) {
        /* valid, zero-length host cps ; does this ever happen? */
-       hcps = (afs_int32 *) malloc(sizeof(afs_int32));
-       osi_Assert(hcps != NULL);
+       hcps = malloc(sizeof(afs_int32));
+       opr_Assert(hcps != NULL);
     }
 
+    if ((hdsk.hostFlags & HWHO_INPROGRESS) || !(hdsk.hostFlags & ALTADDR)) {
+       char hoststr[16];
+       ViceLog(0, ("h_stateRestoreHost: skipping host %s:%d due to invalid flags 0x%x\n",
+                   afs_inet_ntoa_r(hdsk.host, hoststr), (int)ntohs(hdsk.port),
+                   (unsigned)hdsk.hostFlags));
+       bail = 0;
+       state->h_map.entries[hdsk.index].valid = FS_STATE_IDX_SKIPPED;
+       goto done;
+    }
+
+    /* for restoring state, we better be able to get a host! */
     host = GetHT();
-    osi_Assert(host != NULL);
+    opr_Assert(host != NULL);
 
     if (ifp) {
        host->interface = ifp;
@@ -3382,6 +3443,7 @@ h_stateRestoreHost(struct fs_dump_state * state)
     h_InsertList_r(host);
 
     /* setup host id map entry */
+    state->h_map.entries[hdsk.index].valid = FS_STATE_IDX_VALID;
     state->h_map.entries[hdsk.index].old_idx = hdsk.index;
     state->h_map.entries[hdsk.index].new_idx = host->index;
 
@@ -3408,9 +3470,7 @@ h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out)
     out->ActiveCall = in->ActiveCall;
     out->cpsCall = in->cpsCall;
     out->cblist = in->cblist;
-#ifdef FS_STATS_DETAILED
     out->InSameNetwork = in->InSameNetwork;
-#endif
 
     /* special fields we save, but are not memcpy'd back on restore */
     out->index = in->index;
@@ -3431,9 +3491,7 @@ h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out)
     out->ActiveCall = in->ActiveCall;
     out->cpsCall = in->cpsCall;
     out->cblist = in->cblist;
-#ifdef FS_STATS_DETAILED
     out->InSameNetwork = in->InSameNetwork;
-#endif
 }
 
 /* index translation routines */
@@ -3447,7 +3505,8 @@ h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
     if (old >= state->h_map.len) {
        ViceLog(0, ("h_OldToNew: index %d is out of range\n", old));
        ret = 1;
-    } else if (state->h_map.entries[old].old_idx != old) { /* sanity check */
+    } else if (state->h_map.entries[old].valid != FS_STATE_IDX_VALID ||
+               state->h_map.entries[old].old_idx != old) { /* sanity check */
        ViceLog(0, ("h_OldToNew: index %d points to an invalid host record\n", old));
        ret = 1;
     } else {
@@ -3699,115 +3758,9 @@ static struct AFSFid zerofid;
  * Since it can serialize them, and pile up, it should be a separate LWP
  * from other events.
  */
-#if 0
-static int
-CheckHost(struct host *host, int flags, void *rock)
-{
-    struct client *client;
-    struct rx_connection *cb_conn = NULL;
-    int code;
-
-#ifdef AFS_DEMAND_ATTACH_FS
-    /* kill the checkhost lwp ASAP during shutdown */
-    FS_STATE_RDLOCK;
-    if (fs_state.mode == FS_MODE_SHUTDOWN) {
-       FS_STATE_UNLOCK;
-       return H_ENUMERATE_BAIL(flags);
-    }
-    FS_STATE_UNLOCK;
-#endif
-
-    /* Host is held by h_Enumerate */
-    H_LOCK;
-    for (client = host->FirstClient; client; client = client->next) {
-       if (client->refCount == 0 && client->LastCall < clientdeletetime) {
-           client->deleted = 1;
-           host->hostFlags |= CLIENTDELETED;
-       }
-    }
-    if (host->LastCall < checktime) {
-       h_Lock_r(host);
-       if (!(host->hostFlags & HOSTDELETED)) {
-            host->hostFlags |= HWHO_INPROGRESS;
-           cb_conn = host->callback_rxcon;
-           rx_GetConnection(cb_conn);
-           if (host->LastCall < clientdeletetime) {
-               host->hostFlags |= HOSTDELETED;
-               if (!(host->hostFlags & VENUSDOWN)) {
-                   host->hostFlags &= ~ALTADDR;        /* alternate address invalid */
-                   if (host->interface) {
-                       H_UNLOCK;
-                       code =
-                           RXAFSCB_InitCallBackState3(cb_conn,
-                                                      &FS_HostUUID);
-                       H_LOCK;
-                   } else {
-                       H_UNLOCK;
-                       code =
-                           RXAFSCB_InitCallBackState(cb_conn);
-                       H_LOCK;
-                   }
-                   host->hostFlags |= ALTADDR; /* alternate addresses valid */
-                   if (code) {
-                       char hoststr[16];
-                       (void)afs_inet_ntoa_r(host->host, hoststr);
-                       ViceLog(0,
-                               ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
-                                hoststr, ntohs(host->port)));
-                       host->hostFlags |= VENUSDOWN;
-                   }
-                   /* Note:  it's safe to delete hosts even if they have call
-                    * back state, because break delayed callbacks (called when a
-                    * message is received from the workstation) will always send a
-                    * break all call backs to the workstation if there is no
-                    * callback.
-                    */
-               }
-           } else {
-               if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
-                   char hoststr[16];
-                   (void)afs_inet_ntoa_r(host->host, hoststr);
-                   if (host->interface) {
-                       afsUUID uuid = host->interface->uuid;
-                       H_UNLOCK;
-                       code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
-                       H_LOCK;
-                       if (code) {
-                           if (MultiProbeAlternateAddress_r(host)) {
-                               ViceLog(0,("CheckHost: Probing all interfaces of host %s:%d failed, code %d\n",
-                                           hoststr, ntohs(host->port), code));
-                               host->hostFlags |= VENUSDOWN;
-                           }
-                       }
-                   } else {
-                       H_UNLOCK;
-                       code = RXAFSCB_Probe(cb_conn);
-                       H_LOCK;
-                       if (code) {
-                           ViceLog(0,
-                                   ("CheckHost: Probe failed for host %s:%d, code %d\n",
-                                    hoststr, ntohs(host->port), code));
-                           host->hostFlags |= VENUSDOWN;
-                       }
-                   }
-               }
-           }
-           H_UNLOCK;
-           rx_PutConnection(cb_conn);
-           cb_conn=NULL;
-           H_LOCK;
-            host->hostFlags &= ~HWHO_INPROGRESS;
-       }
-       h_Unlock_r(host);
-    }
-    H_UNLOCK;
-    return held;
-
-}                              /*CheckHost */
-#endif
 
 int
-CheckHost_r(struct host *host, int flags, void *dummy)
+CheckHost_r(struct host *host, void *dummy)
 {
     struct client *client;
     struct rx_connection *cb_conn = NULL;
@@ -3818,7 +3771,7 @@ CheckHost_r(struct host *host, int flags, void *dummy)
     FS_STATE_RDLOCK;
     if (fs_state.mode == FS_MODE_SHUTDOWN) {
        FS_STATE_UNLOCK;
-       return H_ENUMERATE_BAIL(flags);
+       return H_ENUMERATE_BAIL(0);
     }
     FS_STATE_UNLOCK;
 #endif
@@ -3905,7 +3858,7 @@ CheckHost_r(struct host *host, int flags, void *dummy)
        }
        h_Unlock_r(host);
     }
-    return flags;
+    return 0;
 
 }                              /*CheckHost_r */
 
@@ -3922,7 +3875,7 @@ CheckHost_r(struct host *host, int flags, void *dummy)
 void
 h_CheckHosts(void)
 {
-    afs_uint32 now = FT_ApproxTime();
+    afs_uint32 now = time(NULL);
 
     memset(&zerofid, 0, sizeof(zerofid));
     /*
@@ -3944,7 +3897,7 @@ h_CheckHosts(void)
  *
  * The addresses in the interfaceAddr list are in host byte order.
  */
-int
+static int
 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
 {
     int i, j;
@@ -3957,8 +3910,8 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
     char uuidstr[128];
     afs_uint16 port7001 = htons(7001);
 
-    osi_Assert(host);
-    osi_Assert(interf);
+    opr_Assert(host);
+    opr_Assert(interf);
 
     number = interf->numberOfInterfaces;
     myAddr = host->host;       /* current interface address */
@@ -4003,7 +3956,7 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
 
     /*
      * Convert IP addresses to network byte order, and remove
-     * duplicate IP addresses from the interface list, and
+     * duplicate and loopback IP addresses from the interface list, and
      * determine whether or not the incoming addr/port is
      * listed.  Note that if the address matches it is not
      * truly a match because the port number for the entries
@@ -4011,6 +3964,9 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
      * for this connection might not be 7001.
      */
     for (i = 0, count = 0, found = 0; i < number; i++) {
+       if (rx_IsLoopbackAddr(interf->addr_in[i])) {
+           continue;
+       }
        interf->addr_in[i] = htonl(interf->addr_in[i]);
        for (j = 0; j < count; j++) {
            if (interf->addr_in[j] == interf->addr_in[i])
@@ -4029,20 +3985,17 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
      * Allocate and initialize an interface structure for this host.
      */
     if (found) {
-       interface = (struct Interface *)
-           malloc(sizeof(struct Interface) +
-                  (sizeof(struct AddrPort) * (count - 1)));
+       interface = malloc(sizeof(struct Interface) +
+                          (sizeof(struct AddrPort) * (count - 1)));
        if (!interface) {
-           ViceLog(0, ("Failed malloc in initInterfaceAddr_r 1\n"));
-           osi_Panic("Failed malloc in initInterfaceAddr_r 1\n");
+           ViceLogThenPanic(0, ("Failed malloc in initInterfaceAddr_r 1\n"));
        }
        interface->numberOfInterfaces = count;
     } else {
-       interface = (struct Interface *)
-           malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
+       interface = malloc(sizeof(struct Interface) +
+                          (sizeof(struct AddrPort) * count));
        if (!interface) {
-           ViceLog(0, ("Failed malloc in initInterfaceAddr_r 2\n"));
-           osi_Panic("Failed malloc in initInterfaceAddr_r 2\n");
+           ViceLogThenPanic(0, ("Failed malloc in initInterfaceAddr_r 2\n"));
        }
        interface->numberOfInterfaces = count + 1;
        interface->interface[count].addr = myAddr;
@@ -4064,9 +4017,11 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
 
     interface->uuid = interf->uuid;
 
-    osi_Assert(!host->interface);
+    opr_Assert(!host->interface);
     host->interface = interface;
 
+    h_AddHostToUuidHashTable_r(&interface->uuid, host);
+
     if (LogLevel >= 125) {
        afsUUID_to_string(&interface->uuid, uuidstr, 127);
 
@@ -4095,7 +4050,7 @@ h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port,
 
     for (hp = &hostAddrHashTable[h_HashIndex(addr)]; (th = *hp);
         hp = &th->next) {
-        osi_Assert(th->hostPtr);
+        opr_Assert(th->hostPtr);
         if (th->hostPtr == host && th->addr == addr && th->port == port) {
            ViceLog(125, ("h_DeleteHostFromAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d)\n",
                          host, afs_inet_ntoa_r(host->host, hoststr),